]> git.lizzy.rs Git - dragonfireclient.git/blob - src/minimap.cpp
Fix client crashing when connecting to server
[dragonfireclient.git] / src / minimap.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2015 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "minimap.h"
21 #include "threading/mutex_auto_lock.h"
22 #include "threading/semaphore.h"
23 #include "clientmap.h"
24 #include "settings.h"
25 #include "nodedef.h"
26 #include "porting.h"
27 #include "util/numeric.h"
28 #include "util/string.h"
29 #include <math.h>
30
31
32 ////
33 //// MinimapUpdateThread
34 ////
35
36 MinimapUpdateThread::~MinimapUpdateThread()
37 {
38         for (std::map<v3s16, MinimapMapblock *>::iterator
39                         it = m_blocks_cache.begin();
40                         it != m_blocks_cache.end(); ++it) {
41                 delete it->second;
42         }
43
44         for (std::deque<QueuedMinimapUpdate>::iterator
45                         it = m_update_queue.begin();
46                         it != m_update_queue.end(); ++it) {
47                 QueuedMinimapUpdate &q = *it;
48                 delete q.data;
49         }
50 }
51
52 bool MinimapUpdateThread::pushBlockUpdate(v3s16 pos, MinimapMapblock *data)
53 {
54         MutexAutoLock lock(m_queue_mutex);
55
56         // Find if block is already in queue.
57         // If it is, update the data and quit.
58         for (std::deque<QueuedMinimapUpdate>::iterator
59                         it = m_update_queue.begin();
60                         it != m_update_queue.end(); ++it) {
61                 QueuedMinimapUpdate &q = *it;
62                 if (q.pos == pos) {
63                         delete q.data;
64                         q.data = data;
65                         return false;
66                 }
67         }
68
69         // Add the block
70         QueuedMinimapUpdate q;
71         q.pos  = pos;
72         q.data = data;
73         m_update_queue.push_back(q);
74
75         return true;
76 }
77
78 bool MinimapUpdateThread::popBlockUpdate(QueuedMinimapUpdate *update)
79 {
80         MutexAutoLock lock(m_queue_mutex);
81
82         if (m_update_queue.empty())
83                 return false;
84
85         *update = m_update_queue.front();
86         m_update_queue.pop_front();
87
88         return true;
89 }
90
91 void MinimapUpdateThread::enqueueBlock(v3s16 pos, MinimapMapblock *data)
92 {
93         pushBlockUpdate(pos, data);
94         deferUpdate();
95 }
96
97
98 void MinimapUpdateThread::doUpdate()
99 {
100         QueuedMinimapUpdate update;
101
102         while (popBlockUpdate(&update)) {
103                 if (update.data) {
104                         // Swap two values in the map using single lookup
105                         std::pair<std::map<v3s16, MinimapMapblock*>::iterator, bool>
106                             result = m_blocks_cache.insert(std::make_pair(update.pos, update.data));
107                         if (result.second == false) {
108                                 delete result.first->second;
109                                 result.first->second = update.data;
110                         }
111                 } else {
112                         std::map<v3s16, MinimapMapblock *>::iterator it;
113                         it = m_blocks_cache.find(update.pos);
114                         if (it != m_blocks_cache.end()) {
115                                 delete it->second;
116                                 m_blocks_cache.erase(it);
117                         }
118                 }
119         }
120
121         if (data->map_invalidated && data->mode != MINIMAP_MODE_OFF) {
122                 getMap(data->pos, data->map_size, data->scan_height, data->is_radar);
123                 data->map_invalidated = false;
124         }
125 }
126
127 MinimapPixel *MinimapUpdateThread::getMinimapPixel(v3s16 pos,
128         s16 scan_height, s16 *pixel_height)
129 {
130         s16 height = scan_height - MAP_BLOCKSIZE;
131         v3s16 blockpos_max, blockpos_min, relpos;
132
133         getNodeBlockPosWithOffset(
134                 v3s16(pos.X, pos.Y - scan_height / 2, pos.Z),
135                 blockpos_min, relpos);
136         getNodeBlockPosWithOffset(
137                 v3s16(pos.X, pos.Y + scan_height / 2, pos.Z),
138                 blockpos_max, relpos);
139
140         for (s16 i = blockpos_max.Y; i > blockpos_min.Y - 1; i--) {
141                 std::map<v3s16, MinimapMapblock *>::iterator it =
142                         m_blocks_cache.find(v3s16(blockpos_max.X, i, blockpos_max.Z));
143                 if (it != m_blocks_cache.end()) {
144                         MinimapMapblock *mmblock = it->second;
145                         MinimapPixel *pixel = &mmblock->data[relpos.Z * MAP_BLOCKSIZE + relpos.X];
146                         if (pixel->id != CONTENT_AIR) {
147                                 *pixel_height = height + pixel->height;
148                                 return pixel;
149                         }
150                 }
151
152                 height -= MAP_BLOCKSIZE;
153         }
154
155         return NULL;
156 }
157
158 s16 MinimapUpdateThread::getAirCount(v3s16 pos, s16 height)
159 {
160         s16 air_count = 0;
161         v3s16 blockpos_max, blockpos_min, relpos;
162
163         getNodeBlockPosWithOffset(
164                 v3s16(pos.X, pos.Y - height / 2, pos.Z),
165                 blockpos_min, relpos);
166         getNodeBlockPosWithOffset(
167                 v3s16(pos.X, pos.Y + height / 2, pos.Z),
168                 blockpos_max, relpos);
169
170         for (s16 i = blockpos_max.Y; i > blockpos_min.Y - 1; i--) {
171                 std::map<v3s16, MinimapMapblock *>::iterator it =
172                         m_blocks_cache.find(v3s16(blockpos_max.X, i, blockpos_max.Z));
173                 if (it != m_blocks_cache.end()) {
174                         MinimapMapblock *mmblock = it->second;
175                         MinimapPixel *pixel = &mmblock->data[relpos.Z * MAP_BLOCKSIZE + relpos.X];
176                         air_count += pixel->air_count;
177                 }
178         }
179
180         return air_count;
181 }
182
183 void MinimapUpdateThread::getMap(v3s16 pos, s16 size, s16 height, bool is_radar)
184 {
185         v3s16 p = v3s16(pos.X - size / 2, pos.Y, pos.Z - size / 2);
186
187         for (s16 x = 0; x < size; x++)
188         for (s16 z = 0; z < size; z++) {
189                 u16 id = CONTENT_AIR;
190                 MinimapPixel *mmpixel = &data->minimap_scan[x + z * size];
191
192                 if (!is_radar) {
193                         s16 pixel_height = 0;
194                         MinimapPixel *cached_pixel =
195                                 getMinimapPixel(v3s16(p.X + x, p.Y, p.Z + z), height, &pixel_height);
196                         if (cached_pixel) {
197                                 id = cached_pixel->id;
198                                 mmpixel->height = pixel_height;
199                         }
200                 } else {
201                         mmpixel->air_count = getAirCount(v3s16(p.X + x, p.Y, p.Z + z), height);
202                 }
203
204                 mmpixel->id = id;
205         }
206 }
207
208 ////
209 //// Mapper
210 ////
211
212 Mapper::Mapper(IrrlichtDevice *device, Client *client)
213 {
214         this->driver    = device->getVideoDriver();
215         this->m_tsrc    = client->getTextureSource();
216         this->m_shdrsrc = client->getShaderSource();
217         this->m_ndef    = client->getNodeDefManager();
218
219         m_angle = 0.f;
220
221         // Initialize static settings
222         m_enable_shaders = g_settings->getBool("enable_shaders");
223         m_surface_mode_scan_height =
224                 g_settings->getBool("minimap_double_scan_height") ? 256 : 128;
225
226         // Initialize minimap data
227         data = new MinimapData;
228         data->mode              = MINIMAP_MODE_OFF;
229         data->is_radar          = false;
230         data->map_invalidated   = true;
231         data->heightmap_image   = NULL;
232         data->minimap_image     = NULL;
233         data->texture           = NULL;
234         data->heightmap_texture = NULL;
235         data->minimap_shape_round = g_settings->getBool("minimap_shape_round");
236
237         // Get round minimap textures
238         data->minimap_mask_round = driver->createImage(
239                 m_tsrc->getTexture("minimap_mask_round.png"),
240                 core::position2d<s32>(0, 0),
241                 core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
242         data->minimap_overlay_round = m_tsrc->getTexture("minimap_overlay_round.png");
243
244         // Get square minimap textures
245         data->minimap_mask_square = driver->createImage(
246                 m_tsrc->getTexture("minimap_mask_square.png"),
247                 core::position2d<s32>(0, 0),
248                 core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
249         data->minimap_overlay_square = m_tsrc->getTexture("minimap_overlay_square.png");
250
251         // Create player marker texture
252         data->player_marker = m_tsrc->getTexture("player_marker.png");
253
254         // Create mesh buffer for minimap
255         m_meshbuffer = getMinimapMeshBuffer();
256
257         // Initialize and start thread
258         m_minimap_update_thread = new MinimapUpdateThread();
259         m_minimap_update_thread->data = data;
260         m_minimap_update_thread->start();
261 }
262
263 Mapper::~Mapper()
264 {
265         m_minimap_update_thread->stop();
266         m_minimap_update_thread->wait();
267
268         m_meshbuffer->drop();
269
270         data->minimap_mask_round->drop();
271         data->minimap_mask_square->drop();
272
273         driver->removeTexture(data->texture);
274         driver->removeTexture(data->heightmap_texture);
275         driver->removeTexture(data->minimap_overlay_round);
276         driver->removeTexture(data->minimap_overlay_square);
277
278         delete data;
279         delete m_minimap_update_thread;
280 }
281
282 void Mapper::addBlock(v3s16 pos, MinimapMapblock *data)
283 {
284         m_minimap_update_thread->enqueueBlock(pos, data);
285 }
286
287 MinimapMode Mapper::getMinimapMode()
288 {
289         return data->mode;
290 }
291
292 void Mapper::toggleMinimapShape()
293 {
294         MutexAutoLock lock(m_mutex);
295
296         data->minimap_shape_round = !data->minimap_shape_round;
297         g_settings->setBool("minimap_shape_round", data->minimap_shape_round);
298         m_minimap_update_thread->deferUpdate();
299 }
300
301 void Mapper::setMinimapMode(MinimapMode mode)
302 {
303         static const MinimapModeDef modedefs[MINIMAP_MODE_COUNT] = {
304                 {false, 0, 0},
305                 {false, m_surface_mode_scan_height, 256},
306                 {false, m_surface_mode_scan_height, 128},
307                 {false, m_surface_mode_scan_height, 64},
308                 {true, 32, 128},
309                 {true, 32, 64},
310                 {true, 32, 32}
311         };
312
313         if (mode >= MINIMAP_MODE_COUNT)
314                 return;
315
316         MutexAutoLock lock(m_mutex);
317
318         data->is_radar    = modedefs[mode].is_radar;
319         data->scan_height = modedefs[mode].scan_height;
320         data->map_size    = modedefs[mode].map_size;
321         data->mode        = mode;
322
323         m_minimap_update_thread->deferUpdate();
324 }
325
326 void Mapper::setPos(v3s16 pos)
327 {
328         bool do_update = false;
329
330         {
331                 MutexAutoLock lock(m_mutex);
332
333                 if (pos != data->old_pos) {
334                         data->old_pos = data->pos;
335                         data->pos = pos;
336                         do_update = true;
337                 }
338         }
339
340         if (do_update)
341                 m_minimap_update_thread->deferUpdate();
342 }
343
344 void Mapper::setAngle(f32 angle)
345 {
346         m_angle = angle;
347 }
348
349 void Mapper::blitMinimapPixelsToImageRadar(video::IImage *map_image)
350 {
351         for (s16 x = 0; x < data->map_size; x++)
352         for (s16 z = 0; z < data->map_size; z++) {
353                 MinimapPixel *mmpixel = &data->minimap_scan[x + z * data->map_size];
354
355                 video::SColor c(240, 0, 0, 0);
356                 if (mmpixel->air_count > 0)
357                         c.setGreen(core::clamp(core::round32(32 + mmpixel->air_count * 8), 0, 255));
358
359                 map_image->setPixel(x, data->map_size - z - 1, c);
360         }
361 }
362
363 void Mapper::blitMinimapPixelsToImageSurface(
364         video::IImage *map_image, video::IImage *heightmap_image)
365 {
366         for (s16 x = 0; x < data->map_size; x++)
367         for (s16 z = 0; z < data->map_size; z++) {
368                 MinimapPixel *mmpixel = &data->minimap_scan[x + z * data->map_size];
369
370                 video::SColor c = m_ndef->get(mmpixel->id).minimap_color;
371                 c.setAlpha(240);
372
373                 map_image->setPixel(x, data->map_size - z - 1, c);
374
375                 u32 h = mmpixel->height;
376                 heightmap_image->setPixel(x,data->map_size - z - 1,
377                         video::SColor(255, h, h, h));
378         }
379 }
380
381 video::ITexture *Mapper::getMinimapTexture()
382 {
383         // update minimap textures when new scan is ready
384         if (data->map_invalidated)
385                 return data->texture;
386
387         // create minimap and heightmap images in memory
388         core::dimension2d<u32> dim(data->map_size, data->map_size);
389         video::IImage *map_image       = driver->createImage(video::ECF_A8R8G8B8, dim);
390         video::IImage *heightmap_image = driver->createImage(video::ECF_A8R8G8B8, dim);
391         video::IImage *minimap_image   = driver->createImage(video::ECF_A8R8G8B8,
392                 core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
393
394         // Blit MinimapPixels to images
395         if (data->is_radar)
396                 blitMinimapPixelsToImageRadar(map_image);
397         else
398                 blitMinimapPixelsToImageSurface(map_image, heightmap_image);
399
400         map_image->copyToScaling(minimap_image);
401         map_image->drop();
402
403         video::IImage *minimap_mask = data->minimap_shape_round ?
404                 data->minimap_mask_round : data->minimap_mask_square;
405
406         if (minimap_mask) {
407                 for (s16 y = 0; y < MINIMAP_MAX_SY; y++)
408                 for (s16 x = 0; x < MINIMAP_MAX_SX; x++) {
409                         video::SColor mask_col = minimap_mask->getPixel(x, y);
410                         if (!mask_col.getAlpha())
411                                 minimap_image->setPixel(x, y, video::SColor(0,0,0,0));
412                 }
413         }
414
415         if (data->texture)
416                 driver->removeTexture(data->texture);
417         if (data->heightmap_texture)
418                 driver->removeTexture(data->heightmap_texture);
419
420         data->texture = driver->addTexture("minimap__", minimap_image);
421         data->heightmap_texture =
422                 driver->addTexture("minimap_heightmap__", heightmap_image);
423         minimap_image->drop();
424         heightmap_image->drop();
425
426         data->map_invalidated = true;
427
428         return data->texture;
429 }
430
431 v3f Mapper::getYawVec()
432 {
433         if (data->minimap_shape_round) {
434                 return v3f(
435                         cos(m_angle * core::DEGTORAD),
436                         sin(m_angle * core::DEGTORAD),
437                         1.0);
438         } else {
439                 return v3f(1.0, 0.0, 1.0);
440         }
441 }
442
443 scene::SMeshBuffer *Mapper::getMinimapMeshBuffer()
444 {
445         scene::SMeshBuffer *buf = new scene::SMeshBuffer();
446         buf->Vertices.set_used(4);
447         buf->Indices.set_used(6);
448         video::SColor c(255, 255, 255, 255);
449
450         buf->Vertices[0] = video::S3DVertex(-1, -1, 0, 0, 0, 1, c, 0, 1);
451         buf->Vertices[1] = video::S3DVertex(-1,  1, 0, 0, 0, 1, c, 0, 0);
452         buf->Vertices[2] = video::S3DVertex( 1,  1, 0, 0, 0, 1, c, 1, 0);
453         buf->Vertices[3] = video::S3DVertex( 1, -1, 0, 0, 0, 1, c, 1, 1);
454
455         buf->Indices[0] = 0;
456         buf->Indices[1] = 1;
457         buf->Indices[2] = 2;
458         buf->Indices[3] = 2;
459         buf->Indices[4] = 3;
460         buf->Indices[5] = 0;
461
462         return buf;
463 }
464
465 void Mapper::drawMinimap()
466 {
467         video::ITexture *minimap_texture = getMinimapTexture();
468         if (!minimap_texture)
469                 return;
470
471         v2u32 screensize = porting::getWindowSize();
472         const u32 size = 0.25 * screensize.Y;
473
474         core::rect<s32> oldViewPort = driver->getViewPort();
475         core::matrix4 oldProjMat = driver->getTransform(video::ETS_PROJECTION);
476         core::matrix4 oldViewMat = driver->getTransform(video::ETS_VIEW);
477
478         driver->setViewPort(core::rect<s32>(
479                 screensize.X - size - 10, 10,
480                 screensize.X - 10, size + 10));
481         driver->setTransform(video::ETS_PROJECTION, core::matrix4());
482         driver->setTransform(video::ETS_VIEW, core::matrix4());
483
484         core::matrix4 matrix;
485         matrix.makeIdentity();
486
487         video::SMaterial &material = m_meshbuffer->getMaterial();
488         material.setFlag(video::EMF_TRILINEAR_FILTER, true);
489         material.Lighting = false;
490         material.TextureLayer[0].Texture = minimap_texture;
491         material.TextureLayer[1].Texture = data->heightmap_texture;
492
493         if (m_enable_shaders && !data->is_radar) {
494                 u16 sid = m_shdrsrc->getShader("minimap_shader", 1, 1);
495                 material.MaterialType = m_shdrsrc->getShaderInfo(sid).material;
496         } else {
497                 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
498         }
499
500         if (data->minimap_shape_round)
501                 matrix.setRotationDegrees(core::vector3df(0, 0, 360 - m_angle));
502
503         // Draw minimap
504         driver->setTransform(video::ETS_WORLD, matrix);
505         driver->setMaterial(material);
506         driver->drawMeshBuffer(m_meshbuffer);
507
508         // Draw overlay
509         video::ITexture *minimap_overlay = data->minimap_shape_round ?
510                 data->minimap_overlay_round : data->minimap_overlay_square;
511         material.TextureLayer[0].Texture = minimap_overlay;
512         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
513         driver->setMaterial(material);
514         driver->drawMeshBuffer(m_meshbuffer);
515
516         // If round minimap, draw player marker
517         if (!data->minimap_shape_round) {
518                 matrix.setRotationDegrees(core::vector3df(0, 0, m_angle));
519                 material.TextureLayer[0].Texture = data->player_marker;
520
521                 driver->setTransform(video::ETS_WORLD, matrix);
522                 driver->setMaterial(material);
523                 driver->drawMeshBuffer(m_meshbuffer);
524         }
525
526         // Reset transformations
527         driver->setTransform(video::ETS_VIEW, oldViewMat);
528         driver->setTransform(video::ETS_PROJECTION, oldProjMat);
529         driver->setViewPort(oldViewPort);
530 }
531
532 ////
533 //// MinimapMapblock
534 ////
535
536 void MinimapMapblock::getMinimapNodes(VoxelManipulator *vmanip, v3s16 pos)
537 {
538
539         for (s16 x = 0; x < MAP_BLOCKSIZE; x++)
540         for (s16 z = 0; z < MAP_BLOCKSIZE; z++) {
541                 s16 air_count = 0;
542                 bool surface_found = false;
543                 MinimapPixel *mmpixel = &data[z * MAP_BLOCKSIZE + x];
544
545                 for (s16 y = MAP_BLOCKSIZE -1; y >= 0; y--) {
546                         v3s16 p(x, y, z);
547                         MapNode n = vmanip->getNodeNoEx(pos + p);
548                         if (!surface_found && n.getContent() != CONTENT_AIR) {
549                                 mmpixel->height = y;
550                                 mmpixel->id = n.getContent();
551                                 surface_found = true;
552                         } else if (n.getContent() == CONTENT_AIR) {
553                                 air_count++;
554                         }
555                 }
556
557                 if (!surface_found)
558                         mmpixel->id = CONTENT_AIR;
559
560                 mmpixel->air_count = air_count;
561         }
562 }