]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/minimap.cpp
Merge branch 'master' of https://github.com/minetest/minetest
[dragonfireclient.git] / src / client / 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 <cmath>
22 #include "client.h"
23 #include "clientmap.h"
24 #include "settings.h"
25 #include "shader.h"
26 #include "mapblock.h"
27 #include "client/renderingengine.h"
28 #include "gettext.h"
29
30 ////
31 //// MinimapUpdateThread
32 ////
33
34 MinimapUpdateThread::~MinimapUpdateThread()
35 {
36         for (auto &it : m_blocks_cache) {
37                 delete it.second;
38         }
39
40         for (auto &q : m_update_queue) {
41                 delete q.data;
42         }
43 }
44
45 bool MinimapUpdateThread::pushBlockUpdate(v3s16 pos, MinimapMapblock *data)
46 {
47         MutexAutoLock lock(m_queue_mutex);
48
49         // Find if block is already in queue.
50         // If it is, update the data and quit.
51         for (QueuedMinimapUpdate &q : m_update_queue) {
52                 if (q.pos == pos) {
53                         delete q.data;
54                         q.data = data;
55                         return false;
56                 }
57         }
58
59         // Add the block
60         QueuedMinimapUpdate q;
61         q.pos  = pos;
62         q.data = data;
63         m_update_queue.push_back(q);
64
65         return true;
66 }
67
68 bool MinimapUpdateThread::popBlockUpdate(QueuedMinimapUpdate *update)
69 {
70         MutexAutoLock lock(m_queue_mutex);
71
72         if (m_update_queue.empty())
73                 return false;
74
75         *update = m_update_queue.front();
76         m_update_queue.pop_front();
77
78         return true;
79 }
80
81 void MinimapUpdateThread::enqueueBlock(v3s16 pos, MinimapMapblock *data)
82 {
83         pushBlockUpdate(pos, data);
84         deferUpdate();
85 }
86
87
88 void MinimapUpdateThread::doUpdate()
89 {
90         QueuedMinimapUpdate update;
91
92         while (popBlockUpdate(&update)) {
93                 if (update.data) {
94                         // Swap two values in the map using single lookup
95                         std::pair<std::map<v3s16, MinimapMapblock*>::iterator, bool>
96                             result = m_blocks_cache.insert(std::make_pair(update.pos, update.data));
97                         if (!result.second) {
98                                 delete result.first->second;
99                                 result.first->second = update.data;
100                         }
101                 } else {
102                         std::map<v3s16, MinimapMapblock *>::iterator it;
103                         it = m_blocks_cache.find(update.pos);
104                         if (it != m_blocks_cache.end()) {
105                                 delete it->second;
106                                 m_blocks_cache.erase(it);
107                         }
108                 }
109         }
110
111
112         if (data->map_invalidated && (
113                                 data->mode.type == MINIMAP_TYPE_RADAR ||
114                                 data->mode.type == MINIMAP_TYPE_SURFACE)) {
115                 getMap(data->pos, data->mode.map_size, data->mode.scan_height);
116                 data->map_invalidated = false;
117         }
118 }
119
120 void MinimapUpdateThread::getMap(v3s16 pos, s16 size, s16 height)
121 {
122         v3s16 pos_min(pos.X - size / 2, pos.Y - height / 2, pos.Z - size / 2);
123         v3s16 pos_max(pos_min.X + size - 1, pos.Y + height / 2, pos_min.Z + size - 1);
124         v3s16 blockpos_min = getNodeBlockPos(pos_min);
125         v3s16 blockpos_max = getNodeBlockPos(pos_max);
126
127 // clear the map
128         for (int z = 0; z < size; z++)
129         for (int x = 0; x < size; x++) {
130                 MinimapPixel &mmpixel = data->minimap_scan[x + z * size];
131                 mmpixel.air_count = 0;
132                 mmpixel.height = 0;
133                 mmpixel.n = MapNode(CONTENT_AIR);
134         }
135
136 // draw the map
137         v3s16 blockpos;
138         for (blockpos.Z = blockpos_min.Z; blockpos.Z <= blockpos_max.Z; ++blockpos.Z)
139         for (blockpos.Y = blockpos_min.Y; blockpos.Y <= blockpos_max.Y; ++blockpos.Y)
140         for (blockpos.X = blockpos_min.X; blockpos.X <= blockpos_max.X; ++blockpos.X) {
141                 std::map<v3s16, MinimapMapblock *>::const_iterator pblock =
142                         m_blocks_cache.find(blockpos);
143                 if (pblock == m_blocks_cache.end())
144                         continue;
145                 const MinimapMapblock &block = *pblock->second;
146
147                 v3s16 block_node_min(blockpos * MAP_BLOCKSIZE);
148                 v3s16 block_node_max(block_node_min + MAP_BLOCKSIZE - 1);
149                 // clip
150                 v3s16 range_min = componentwise_max(block_node_min, pos_min);
151                 v3s16 range_max = componentwise_min(block_node_max, pos_max);
152
153                 v3s16 pos;
154                 pos.Y = range_min.Y;
155                 for (pos.Z = range_min.Z; pos.Z <= range_max.Z; ++pos.Z)
156                 for (pos.X = range_min.X; pos.X <= range_max.X; ++pos.X) {
157                         v3s16 inblock_pos = pos - block_node_min;
158                         const MinimapPixel &in_pixel =
159                                 block.data[inblock_pos.Z * MAP_BLOCKSIZE + inblock_pos.X];
160
161                         v3s16 inmap_pos = pos - pos_min;
162                         MinimapPixel &out_pixel =
163                                 data->minimap_scan[inmap_pos.X + inmap_pos.Z * size];
164
165                         out_pixel.air_count += in_pixel.air_count;
166                         if (in_pixel.n.param0 != CONTENT_AIR) {
167                                 out_pixel.n = in_pixel.n;
168                                 out_pixel.height = inmap_pos.Y + in_pixel.height;
169                         }
170                 }
171         }
172 }
173
174 ////
175 //// Mapper
176 ////
177
178 Minimap::Minimap(Client *client)
179 {
180         this->client    = client;
181         this->driver    = RenderingEngine::get_video_driver();
182         this->m_tsrc    = client->getTextureSource();
183         this->m_shdrsrc = client->getShaderSource();
184         this->m_ndef    = client->getNodeDefManager();
185
186         m_angle = 0.f;
187         m_current_mode_index = 0;
188
189         // Initialize static settings
190         m_enable_shaders = g_settings->getBool("enable_shaders");
191         m_surface_mode_scan_height =
192                 g_settings->getBool("minimap_double_scan_height") ? 256 : 128;
193
194         // Initialize minimap modes
195         addMode(MINIMAP_TYPE_OFF);
196         addMode(MINIMAP_TYPE_SURFACE, 256);
197         addMode(MINIMAP_TYPE_SURFACE, 128);
198         addMode(MINIMAP_TYPE_SURFACE, 64);
199         addMode(MINIMAP_TYPE_RADAR,   512);
200         addMode(MINIMAP_TYPE_RADAR,   256);
201         addMode(MINIMAP_TYPE_RADAR,   128);
202
203         // Initialize minimap data
204         data = new MinimapData;
205         data->map_invalidated = true;
206
207         data->minimap_shape_round = g_settings->getBool("minimap_shape_round");
208
209         // Get round minimap textures
210         data->minimap_mask_round = driver->createImage(
211                 m_tsrc->getTexture("minimap_mask_round.png"),
212                 core::position2d<s32>(0, 0),
213                 core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
214         data->minimap_overlay_round = m_tsrc->getTexture("minimap_overlay_round.png");
215
216         // Get square minimap textures
217         data->minimap_mask_square = driver->createImage(
218                 m_tsrc->getTexture("minimap_mask_square.png"),
219                 core::position2d<s32>(0, 0),
220                 core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
221         data->minimap_overlay_square = m_tsrc->getTexture("minimap_overlay_square.png");
222
223         // Create player marker texture
224         data->player_marker = m_tsrc->getTexture("player_marker.png");
225         // Create object marker texture
226         data->object_marker_red = m_tsrc->getTexture("object_marker_red.png");
227
228         setModeIndex(0);
229
230         // Create mesh buffer for minimap
231         m_meshbuffer = getMinimapMeshBuffer();
232
233         // Initialize and start thread
234         m_minimap_update_thread = new MinimapUpdateThread();
235         m_minimap_update_thread->data = data;
236         m_minimap_update_thread->start();
237 }
238
239 Minimap::~Minimap()
240 {
241         m_minimap_update_thread->stop();
242         m_minimap_update_thread->wait();
243
244         m_meshbuffer->drop();
245
246         data->minimap_mask_round->drop();
247         data->minimap_mask_square->drop();
248
249         driver->removeTexture(data->texture);
250         driver->removeTexture(data->heightmap_texture);
251         driver->removeTexture(data->minimap_overlay_round);
252         driver->removeTexture(data->minimap_overlay_square);
253         driver->removeTexture(data->object_marker_red);
254
255         for (MinimapMarker *m : m_markers)
256                 delete m;
257         m_markers.clear();
258
259         delete data;
260         delete m_minimap_update_thread;
261 }
262
263 void Minimap::addBlock(v3s16 pos, MinimapMapblock *data)
264 {
265         m_minimap_update_thread->enqueueBlock(pos, data);
266 }
267
268 void Minimap::toggleMinimapShape()
269 {
270         MutexAutoLock lock(m_mutex);
271
272         data->minimap_shape_round = !data->minimap_shape_round;
273         g_settings->setBool("minimap_shape_round", data->minimap_shape_round);
274         m_minimap_update_thread->deferUpdate();
275 }
276
277 void Minimap::setMinimapShape(MinimapShape shape)
278 {
279         MutexAutoLock lock(m_mutex);
280
281         if (shape == MINIMAP_SHAPE_SQUARE)
282                 data->minimap_shape_round = false;
283         else if (shape == MINIMAP_SHAPE_ROUND)
284                 data->minimap_shape_round = true;
285
286         g_settings->setBool("minimap_shape_round", data->minimap_shape_round);
287         m_minimap_update_thread->deferUpdate();
288 }
289
290 MinimapShape Minimap::getMinimapShape()
291 {
292         if (data->minimap_shape_round) {
293                 return MINIMAP_SHAPE_ROUND;
294         }
295
296         return MINIMAP_SHAPE_SQUARE;
297 }
298
299 void Minimap::setModeIndex(size_t index)
300 {
301         MutexAutoLock lock(m_mutex);
302
303         if (index < m_modes.size()) {
304                 data->mode = m_modes[index];
305                 m_current_mode_index = index;
306         } else {
307                 data->mode = {MINIMAP_TYPE_OFF, gettext("Minimap hidden"), 0, 0, "", 0};
308                 m_current_mode_index = 0;
309         }
310
311         data->map_invalidated = true;
312
313         if (m_minimap_update_thread)
314                 m_minimap_update_thread->deferUpdate();
315 }
316
317 void Minimap::addMode(MinimapModeDef mode)
318 {
319         // Check validity
320         if (mode.type == MINIMAP_TYPE_TEXTURE) {
321                 if (mode.texture.empty())
322                         return;
323                 if (mode.scale < 1)
324                         mode.scale = 1;
325         }
326
327         int zoom = -1;
328
329         // Build a default standard label
330         if (mode.label == "") {
331                 switch (mode.type) {
332                         case MINIMAP_TYPE_OFF:
333                                 mode.label = gettext("Minimap hidden");
334                                 break;
335                         case MINIMAP_TYPE_SURFACE:
336                                 mode.label = gettext("Minimap in surface mode, Zoom x%d");
337                                 if (mode.map_size > 0)
338                                         zoom = 256 / mode.map_size;
339                                 break;
340                         case MINIMAP_TYPE_RADAR:
341                                 mode.label = gettext("Minimap in radar mode, Zoom x%d");
342                                 if (mode.map_size > 0)
343                                         zoom = 512 / mode.map_size;
344                                 break;
345                         case MINIMAP_TYPE_TEXTURE:
346                                 mode.label = gettext("Minimap in texture mode");
347                                 break;
348                         default:
349                                 break;
350                 }
351         }
352         // else: Custom labels need mod-provided client-side translation
353
354         if (zoom >= 0) {
355                 char label_buf[1024];
356                 porting::mt_snprintf(label_buf, sizeof(label_buf),
357                         mode.label.c_str(), zoom);
358                 mode.label = label_buf;
359         }
360
361         m_modes.push_back(mode);
362 }
363
364 void Minimap::addMode(MinimapType type, u16 size, std::string label,
365                 std::string texture, u16 scale)
366 {
367         MinimapModeDef mode;
368         mode.type = type;
369         mode.label = label;
370         mode.map_size = size;
371         mode.texture = texture;
372         mode.scale = scale;
373         switch (type) {
374                 case MINIMAP_TYPE_SURFACE:
375                         mode.scan_height = m_surface_mode_scan_height;
376                         break;
377                 case MINIMAP_TYPE_RADAR:
378                         mode.scan_height = 32;
379                         break;
380                 default:
381                         mode.scan_height = 0;
382         }
383         addMode(mode);
384 }
385
386 void Minimap::nextMode()
387 {
388         if (m_modes.empty())
389                 return;
390         m_current_mode_index++;
391         if (m_current_mode_index >= m_modes.size())
392                 m_current_mode_index = 0;
393
394         setModeIndex(m_current_mode_index);
395 }
396
397 void Minimap::setPos(v3s16 pos)
398 {
399         bool do_update = false;
400
401         {
402                 MutexAutoLock lock(m_mutex);
403
404                 if (pos != data->old_pos) {
405                         data->old_pos = data->pos;
406                         data->pos = pos;
407                         do_update = true;
408                 }
409         }
410
411         if (do_update)
412                 m_minimap_update_thread->deferUpdate();
413 }
414
415 void Minimap::setAngle(f32 angle)
416 {
417         m_angle = angle;
418 }
419
420 void Minimap::blitMinimapPixelsToImageRadar(video::IImage *map_image)
421 {
422         video::SColor c(240, 0, 0, 0);
423         for (s16 x = 0; x < data->mode.map_size; x++)
424         for (s16 z = 0; z < data->mode.map_size; z++) {
425                 MinimapPixel *mmpixel = &data->minimap_scan[x + z * data->mode.map_size];
426
427                 if (mmpixel->air_count > 0)
428                         c.setGreen(core::clamp(core::round32(32 + mmpixel->air_count * 8), 0, 255));
429                 else
430                         c.setGreen(0);
431
432                 map_image->setPixel(x, data->mode.map_size - z - 1, c);
433         }
434 }
435
436 void Minimap::blitMinimapPixelsToImageSurface(
437         video::IImage *map_image, video::IImage *heightmap_image)
438 {
439         // This variable creation/destruction has a 1% cost on rendering minimap
440         video::SColor tilecolor;
441         for (s16 x = 0; x < data->mode.map_size; x++)
442         for (s16 z = 0; z < data->mode.map_size; z++) {
443                 MinimapPixel *mmpixel = &data->minimap_scan[x + z * data->mode.map_size];
444
445                 const ContentFeatures &f = m_ndef->get(mmpixel->n);
446                 const TileDef *tile = &f.tiledef[0];
447
448                 // Color of the 0th tile (mostly this is the topmost)
449                 if(tile->has_color)
450                         tilecolor = tile->color;
451                 else
452                         mmpixel->n.getColor(f, &tilecolor);
453
454                 tilecolor.setRed(tilecolor.getRed() * f.minimap_color.getRed() / 255);
455                 tilecolor.setGreen(tilecolor.getGreen() * f.minimap_color.getGreen() / 255);
456                 tilecolor.setBlue(tilecolor.getBlue() * f.minimap_color.getBlue() / 255);
457                 tilecolor.setAlpha(240);
458
459                 map_image->setPixel(x, data->mode.map_size - z - 1, tilecolor);
460
461                 u32 h = mmpixel->height;
462                 heightmap_image->setPixel(x,data->mode.map_size - z - 1,
463                         video::SColor(255, h, h, h));
464         }
465 }
466
467 video::ITexture *Minimap::getMinimapTexture()
468 {
469         // update minimap textures when new scan is ready
470         if (data->map_invalidated && data->mode.type != MINIMAP_TYPE_TEXTURE)
471                 return data->texture;
472
473         // create minimap and heightmap images in memory
474         core::dimension2d<u32> dim(data->mode.map_size, data->mode.map_size);
475         video::IImage *map_image       = driver->createImage(video::ECF_A8R8G8B8, dim);
476         video::IImage *heightmap_image = driver->createImage(video::ECF_A8R8G8B8, dim);
477         video::IImage *minimap_image   = driver->createImage(video::ECF_A8R8G8B8,
478                 core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
479
480         // Blit MinimapPixels to images
481         switch(data->mode.type) {
482         case MINIMAP_TYPE_OFF:
483                 break;
484         case MINIMAP_TYPE_SURFACE:
485                 blitMinimapPixelsToImageSurface(map_image, heightmap_image);
486                 break;
487         case MINIMAP_TYPE_RADAR:
488                 blitMinimapPixelsToImageRadar(map_image);
489                 break;
490         case MINIMAP_TYPE_TEXTURE:
491                 // Want to use texture source, to : 1 find texture, 2 cache it
492                 video::ITexture* texture = m_tsrc->getTexture(data->mode.texture);
493                 video::IImage* image = driver->createImageFromData(
494                          texture->getColorFormat(), texture->getSize(),
495                          texture->lock(video::ETLM_READ_ONLY), true, false);
496                 texture->unlock();
497
498                 auto dim = image->getDimension();
499
500                 map_image->fill(video::SColor(255, 0, 0, 0));
501
502                 image->copyTo(map_image,
503                         irr::core::vector2d<int> {
504                                 ((data->mode.map_size - (static_cast<int>(dim.Width))) >> 1)
505                                         - data->pos.X / data->mode.scale,
506                                 ((data->mode.map_size - (static_cast<int>(dim.Height))) >> 1)
507                                         + data->pos.Z / data->mode.scale
508                         });
509                 image->drop();
510         }
511
512         map_image->copyToScaling(minimap_image);
513         map_image->drop();
514
515         video::IImage *minimap_mask = data->minimap_shape_round ?
516                 data->minimap_mask_round : data->minimap_mask_square;
517
518         if (minimap_mask) {
519                 for (s16 y = 0; y < MINIMAP_MAX_SY; y++)
520                 for (s16 x = 0; x < MINIMAP_MAX_SX; x++) {
521                         const video::SColor &mask_col = minimap_mask->getPixel(x, y);
522                         if (!mask_col.getAlpha())
523                                 minimap_image->setPixel(x, y, video::SColor(0,0,0,0));
524                 }
525         }
526
527         if (data->texture)
528                 driver->removeTexture(data->texture);
529         if (data->heightmap_texture)
530                 driver->removeTexture(data->heightmap_texture);
531
532         data->texture = driver->addTexture("minimap__", minimap_image);
533         data->heightmap_texture =
534                 driver->addTexture("minimap_heightmap__", heightmap_image);
535         minimap_image->drop();
536         heightmap_image->drop();
537
538         data->map_invalidated = true;
539
540         return data->texture;
541 }
542
543 v3f Minimap::getYawVec()
544 {
545         if (data->minimap_shape_round) {
546                 return v3f(
547                         std::cos(m_angle * core::DEGTORAD),
548                         std::sin(m_angle * core::DEGTORAD),
549                         1.0);
550         }
551
552         return v3f(1.0, 0.0, 1.0);
553 }
554
555 scene::SMeshBuffer *Minimap::getMinimapMeshBuffer()
556 {
557         scene::SMeshBuffer *buf = new scene::SMeshBuffer();
558         buf->Vertices.set_used(4);
559         buf->Indices.set_used(6);
560         static const video::SColor c(255, 255, 255, 255);
561
562         buf->Vertices[0] = video::S3DVertex(-1, -1, 0, 0, 0, 1, c, 0, 1);
563         buf->Vertices[1] = video::S3DVertex(-1,  1, 0, 0, 0, 1, c, 0, 0);
564         buf->Vertices[2] = video::S3DVertex( 1,  1, 0, 0, 0, 1, c, 1, 0);
565         buf->Vertices[3] = video::S3DVertex( 1, -1, 0, 0, 0, 1, c, 1, 1);
566
567         buf->Indices[0] = 0;
568         buf->Indices[1] = 1;
569         buf->Indices[2] = 2;
570         buf->Indices[3] = 2;
571         buf->Indices[4] = 3;
572         buf->Indices[5] = 0;
573
574         return buf;
575 }
576
577 void Minimap::drawMinimap()
578 {
579         // Non hud managed minimap drawing (legacy minimap)
580         v2u32 screensize = RenderingEngine::getWindowSize();
581         const u32 size = 0.25 * screensize.Y;
582
583         drawMinimap(core::rect<s32>(
584                 screensize.X - size * 2 - 10, 10,
585                 screensize.X - size - 10, size + 10));
586 }
587
588 void Minimap::drawMinimap(core::rect<s32> rect) {
589
590         video::ITexture *minimap_texture = getMinimapTexture();
591         if (!minimap_texture)
592                 return;
593
594         if (data->mode.type == MINIMAP_TYPE_OFF)
595                 return;
596
597         updateActiveMarkers();
598
599         core::rect<s32> oldViewPort = driver->getViewPort();
600         core::matrix4 oldProjMat = driver->getTransform(video::ETS_PROJECTION);
601         core::matrix4 oldViewMat = driver->getTransform(video::ETS_VIEW);
602
603         driver->setViewPort(rect);
604         driver->setTransform(video::ETS_PROJECTION, core::matrix4());
605         driver->setTransform(video::ETS_VIEW, core::matrix4());
606
607         core::matrix4 matrix;
608         matrix.makeIdentity();
609
610         video::SMaterial &material = m_meshbuffer->getMaterial();
611         material.setFlag(video::EMF_TRILINEAR_FILTER, true);
612         material.Lighting = false;
613         material.TextureLayer[0].Texture = minimap_texture;
614         material.TextureLayer[1].Texture = data->heightmap_texture;
615
616         if (m_enable_shaders && data->mode.type == MINIMAP_TYPE_SURFACE) {
617                 u16 sid = m_shdrsrc->getShader("minimap_shader", TILE_MATERIAL_ALPHA);
618                 material.MaterialType = m_shdrsrc->getShaderInfo(sid).material;
619         } else {
620                 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
621         }
622
623         if (data->minimap_shape_round)
624                 matrix.setRotationDegrees(core::vector3df(0, 0, 360 - m_angle));
625
626         // Draw minimap
627         driver->setTransform(video::ETS_WORLD, matrix);
628         driver->setMaterial(material);
629         driver->drawMeshBuffer(m_meshbuffer);
630
631         // Draw overlay
632         video::ITexture *minimap_overlay = data->minimap_shape_round ?
633                 data->minimap_overlay_round : data->minimap_overlay_square;
634         material.TextureLayer[0].Texture = minimap_overlay;
635         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
636         driver->setMaterial(material);
637         driver->drawMeshBuffer(m_meshbuffer);
638
639         // Draw player marker on minimap
640         if (data->minimap_shape_round) {
641                 matrix.setRotationDegrees(core::vector3df(0, 0, 0));
642         } else {
643                 matrix.setRotationDegrees(core::vector3df(0, 0, m_angle));
644         }
645
646         material.TextureLayer[0].Texture = data->player_marker;
647         driver->setTransform(video::ETS_WORLD, matrix);
648         driver->setMaterial(material);
649         driver->drawMeshBuffer(m_meshbuffer);
650
651         // Reset transformations
652         driver->setTransform(video::ETS_VIEW, oldViewMat);
653         driver->setTransform(video::ETS_PROJECTION, oldProjMat);
654         driver->setViewPort(oldViewPort);
655
656         // Draw player markers
657         v2s32 s_pos(rect.UpperLeftCorner.X, rect.UpperLeftCorner.Y);
658         core::dimension2di imgsize(data->object_marker_red->getOriginalSize());
659         core::rect<s32> img_rect(0, 0, imgsize.Width, imgsize.Height);
660         static const video::SColor col(255, 255, 255, 255);
661         static const video::SColor c[4] = {col, col, col, col};
662         f32 sin_angle = std::sin(m_angle * core::DEGTORAD);
663         f32 cos_angle = std::cos(m_angle * core::DEGTORAD);
664         s32 marker_size2 =  0.025 * (float)rect.getWidth();;
665         for (std::list<v2f>::const_iterator
666                         i = m_active_markers.begin();
667                         i != m_active_markers.end(); ++i) {
668                 v2f posf = *i;
669                 if (data->minimap_shape_round) {
670                         f32 t1 = posf.X * cos_angle - posf.Y * sin_angle;
671                         f32 t2 = posf.X * sin_angle + posf.Y * cos_angle;
672                         posf.X = t1;
673                         posf.Y = t2;
674                 }
675                 posf.X = (posf.X + 0.5) * (float)rect.getWidth();
676                 posf.Y = (posf.Y + 0.5) * (float)rect.getHeight();
677                 core::rect<s32> dest_rect(
678                         s_pos.X + posf.X - marker_size2,
679                         s_pos.Y + posf.Y - marker_size2,
680                         s_pos.X + posf.X + marker_size2,
681                         s_pos.Y + posf.Y + marker_size2);
682                 driver->draw2DImage(data->object_marker_red, dest_rect,
683                         img_rect, &dest_rect, &c[0], true);
684         }
685 }
686
687 MinimapMarker* Minimap::addMarker(scene::ISceneNode *parent_node)
688 {
689         MinimapMarker *m = new MinimapMarker(parent_node);
690         m_markers.push_back(m);
691         return m;
692 }
693
694 void Minimap::removeMarker(MinimapMarker **m)
695 {
696         m_markers.remove(*m);
697         delete *m;
698         *m = nullptr;
699 }
700
701 void Minimap::updateActiveMarkers()
702 {
703         video::IImage *minimap_mask = data->minimap_shape_round ?
704                 data->minimap_mask_round : data->minimap_mask_square;
705
706         m_active_markers.clear();
707         v3f cam_offset = intToFloat(client->getCamera()->getOffset(), BS);
708         v3s16 pos_offset = data->pos - v3s16(data->mode.map_size / 2,
709                         data->mode.scan_height / 2,
710                         data->mode.map_size / 2);
711
712         for (MinimapMarker *marker : m_markers) {
713                 v3s16 pos = floatToInt(marker->parent_node->getAbsolutePosition() +
714                         cam_offset, BS) - pos_offset;
715                 if (pos.X < 0 || pos.X > data->mode.map_size ||
716                                 pos.Y < 0 || pos.Y > data->mode.scan_height ||
717                                 pos.Z < 0 || pos.Z > data->mode.map_size) {
718                         continue;
719                 }
720                 pos.X = ((float)pos.X / data->mode.map_size) * MINIMAP_MAX_SX;
721                 pos.Z = ((float)pos.Z / data->mode.map_size) * MINIMAP_MAX_SY;
722                 const video::SColor &mask_col = minimap_mask->getPixel(pos.X, pos.Z);
723                 if (!mask_col.getAlpha()) {
724                         continue;
725                 }
726
727                 m_active_markers.emplace_back(((float)pos.X / (float)MINIMAP_MAX_SX) - 0.5,
728                         (1.0 - (float)pos.Z / (float)MINIMAP_MAX_SY) - 0.5);
729         }
730 }
731
732 ////
733 //// MinimapMapblock
734 ////
735
736 void MinimapMapblock::getMinimapNodes(VoxelManipulator *vmanip, const v3s16 &pos)
737 {
738
739         for (s16 x = 0; x < MAP_BLOCKSIZE; x++)
740         for (s16 z = 0; z < MAP_BLOCKSIZE; z++) {
741                 s16 air_count = 0;
742                 bool surface_found = false;
743                 MinimapPixel *mmpixel = &data[z * MAP_BLOCKSIZE + x];
744
745                 for (s16 y = MAP_BLOCKSIZE -1; y >= 0; y--) {
746                         v3s16 p(x, y, z);
747                         MapNode n = vmanip->getNodeNoEx(pos + p);
748                         if (!surface_found && n.getContent() != CONTENT_AIR) {
749                                 mmpixel->height = y;
750                                 mmpixel->n = n;
751                                 surface_found = true;
752                         } else if (n.getContent() == CONTENT_AIR) {
753                                 air_count++;
754                         }
755                 }
756
757                 if (!surface_found)
758                         mmpixel->n = MapNode(CONTENT_AIR);
759
760                 mmpixel->air_count = air_count;
761         }
762 }