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