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