]> 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 = MinimapModeDef{MINIMAP_TYPE_OFF, N_("Minimap hidden"), 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 = N_("Minimap hidden");
334                                 break;
335                         case MINIMAP_TYPE_SURFACE:
336                                 mode.label = N_("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 = N_("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 = N_("Minimap in texture mode");
347                                 break;
348                         default:
349                                 break;
350                 }
351         }
352
353         if (zoom >= 0) {
354                 char label_buf[1024];
355                 porting::mt_snprintf(label_buf, sizeof(label_buf),
356                         mode.label.c_str(), zoom);
357                 mode.label = label_buf;
358         }
359
360         m_modes.push_back(mode);
361 }
362
363 void Minimap::addMode(MinimapType type, u16 size, std::string label,
364                 std::string texture, u16 scale)
365 {
366         MinimapModeDef mode;
367         mode.type = type;
368         mode.label = label;
369         mode.map_size = size;
370         mode.texture = texture;
371         mode.scale = scale;
372         switch (type) {
373                 case MINIMAP_TYPE_SURFACE:
374                         mode.scan_height = m_surface_mode_scan_height;
375                         break;
376                 case MINIMAP_TYPE_RADAR:
377                         mode.scan_height = 32;
378                         break;
379                 default:
380                         mode.scan_height = 0;
381         }
382         addMode(mode);
383 }
384
385 void Minimap::nextMode()
386 {
387         if (m_modes.empty())
388                 return;
389         m_current_mode_index++;
390         if (m_current_mode_index >= m_modes.size())
391                 m_current_mode_index = 0;
392
393         setModeIndex(m_current_mode_index);
394 }
395
396 void Minimap::setPos(v3s16 pos)
397 {
398         bool do_update = false;
399
400         {
401                 MutexAutoLock lock(m_mutex);
402
403                 if (pos != data->old_pos) {
404                         data->old_pos = data->pos;
405                         data->pos = pos;
406                         do_update = true;
407                 }
408         }
409
410         if (do_update)
411                 m_minimap_update_thread->deferUpdate();
412 }
413
414 void Minimap::setAngle(f32 angle)
415 {
416         m_angle = angle;
417 }
418
419 void Minimap::blitMinimapPixelsToImageRadar(video::IImage *map_image)
420 {
421         video::SColor c(240, 0, 0, 0);
422         for (s16 x = 0; x < data->mode.map_size; x++)
423         for (s16 z = 0; z < data->mode.map_size; z++) {
424                 MinimapPixel *mmpixel = &data->minimap_scan[x + z * data->mode.map_size];
425
426                 if (mmpixel->air_count > 0)
427                         c.setGreen(core::clamp(core::round32(32 + mmpixel->air_count * 8), 0, 255));
428                 else
429                         c.setGreen(0);
430
431                 map_image->setPixel(x, data->mode.map_size - z - 1, c);
432         }
433 }
434
435 void Minimap::blitMinimapPixelsToImageSurface(
436         video::IImage *map_image, video::IImage *heightmap_image)
437 {
438         // This variable creation/destruction has a 1% cost on rendering minimap
439         video::SColor tilecolor;
440         for (s16 x = 0; x < data->mode.map_size; x++)
441         for (s16 z = 0; z < data->mode.map_size; z++) {
442                 MinimapPixel *mmpixel = &data->minimap_scan[x + z * data->mode.map_size];
443
444                 const ContentFeatures &f = m_ndef->get(mmpixel->n);
445                 const TileDef *tile = &f.tiledef[0];
446
447                 // Color of the 0th tile (mostly this is the topmost)
448                 if(tile->has_color)
449                         tilecolor = tile->color;
450                 else
451                         mmpixel->n.getColor(f, &tilecolor);
452
453                 tilecolor.setRed(tilecolor.getRed() * f.minimap_color.getRed() / 255);
454                 tilecolor.setGreen(tilecolor.getGreen() * f.minimap_color.getGreen() / 255);
455                 tilecolor.setBlue(tilecolor.getBlue() * f.minimap_color.getBlue() / 255);
456                 tilecolor.setAlpha(240);
457
458                 map_image->setPixel(x, data->mode.map_size - z - 1, tilecolor);
459
460                 u32 h = mmpixel->height;
461                 heightmap_image->setPixel(x,data->mode.map_size - z - 1,
462                         video::SColor(255, h, h, h));
463         }
464 }
465
466 video::ITexture *Minimap::getMinimapTexture()
467 {
468         // update minimap textures when new scan is ready
469         if (data->map_invalidated && data->mode.type != MINIMAP_TYPE_TEXTURE)
470                 return data->texture;
471
472         // create minimap and heightmap images in memory
473         core::dimension2d<u32> dim(data->mode.map_size, data->mode.map_size);
474         video::IImage *map_image       = driver->createImage(video::ECF_A8R8G8B8, dim);
475         video::IImage *heightmap_image = driver->createImage(video::ECF_A8R8G8B8, dim);
476         video::IImage *minimap_image   = driver->createImage(video::ECF_A8R8G8B8,
477                 core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
478
479         // Blit MinimapPixels to images
480         switch(data->mode.type) {
481         case MINIMAP_TYPE_OFF:
482                 break;
483         case MINIMAP_TYPE_SURFACE:
484                 blitMinimapPixelsToImageSurface(map_image, heightmap_image);
485                 break;
486         case MINIMAP_TYPE_RADAR:
487                 blitMinimapPixelsToImageRadar(map_image);
488                 break;
489         case MINIMAP_TYPE_TEXTURE:
490                 // Want to use texture source, to : 1 find texture, 2 cache it
491                 video::ITexture* texture = m_tsrc->getTexture(data->mode.texture);
492                 video::IImage* image = driver->createImageFromData(
493                          texture->getColorFormat(), texture->getSize(), texture->lock(), true, false);
494                 texture->unlock();
495
496                 auto dim = image->getDimension();
497
498                 map_image->fill(video::SColor(255, 0, 0, 0));
499
500                 image->copyTo(map_image,
501                         irr::core::vector2d<int> {
502                                 ((data->mode.map_size - (static_cast<int>(dim.Width))) >> 1)
503                                         - data->pos.X / data->mode.scale,
504                                 ((data->mode.map_size - (static_cast<int>(dim.Height))) >> 1)
505                                         + data->pos.Z / data->mode.scale
506                         });
507                 image->drop();
508         }
509
510         map_image->copyToScaling(minimap_image);
511         map_image->drop();
512
513         video::IImage *minimap_mask = data->minimap_shape_round ?
514                 data->minimap_mask_round : data->minimap_mask_square;
515
516         if (minimap_mask) {
517                 for (s16 y = 0; y < MINIMAP_MAX_SY; y++)
518                 for (s16 x = 0; x < MINIMAP_MAX_SX; x++) {
519                         const video::SColor &mask_col = minimap_mask->getPixel(x, y);
520                         if (!mask_col.getAlpha())
521                                 minimap_image->setPixel(x, y, video::SColor(0,0,0,0));
522                 }
523         }
524
525         if (data->texture)
526                 driver->removeTexture(data->texture);
527         if (data->heightmap_texture)
528                 driver->removeTexture(data->heightmap_texture);
529
530         data->texture = driver->addTexture("minimap__", minimap_image);
531         data->heightmap_texture =
532                 driver->addTexture("minimap_heightmap__", heightmap_image);
533         minimap_image->drop();
534         heightmap_image->drop();
535
536         data->map_invalidated = true;
537
538         return data->texture;
539 }
540
541 v3f Minimap::getYawVec()
542 {
543         if (data->minimap_shape_round) {
544                 return v3f(
545                         std::cos(m_angle * core::DEGTORAD),
546                         std::sin(m_angle * core::DEGTORAD),
547                         1.0);
548         }
549
550         return v3f(1.0, 0.0, 1.0);
551 }
552
553 scene::SMeshBuffer *Minimap::getMinimapMeshBuffer()
554 {
555         scene::SMeshBuffer *buf = new scene::SMeshBuffer();
556         buf->Vertices.set_used(4);
557         buf->Indices.set_used(6);
558         static const video::SColor c(255, 255, 255, 255);
559
560         buf->Vertices[0] = video::S3DVertex(-1, -1, 0, 0, 0, 1, c, 0, 1);
561         buf->Vertices[1] = video::S3DVertex(-1,  1, 0, 0, 0, 1, c, 0, 0);
562         buf->Vertices[2] = video::S3DVertex( 1,  1, 0, 0, 0, 1, c, 1, 0);
563         buf->Vertices[3] = video::S3DVertex( 1, -1, 0, 0, 0, 1, c, 1, 1);
564
565         buf->Indices[0] = 0;
566         buf->Indices[1] = 1;
567         buf->Indices[2] = 2;
568         buf->Indices[3] = 2;
569         buf->Indices[4] = 3;
570         buf->Indices[5] = 0;
571
572         return buf;
573 }
574
575 void Minimap::drawMinimap()
576 {
577         // Non hud managed minimap drawing (legacy minimap)
578         v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
579         const u32 size = 0.25 * screensize.Y;
580
581         drawMinimap(core::rect<s32>(
582                 screensize.X - size * 2 - 10, 10,
583                 screensize.X - size - 10, size + 10));
584 }
585
586 void Minimap::drawMinimap(core::rect<s32> rect) {
587
588         video::ITexture *minimap_texture = getMinimapTexture();
589         if (!minimap_texture)
590                 return;
591
592         if (data->mode.type == MINIMAP_TYPE_OFF)
593                 return;
594
595         updateActiveMarkers();
596
597         core::rect<s32> oldViewPort = driver->getViewPort();
598         core::matrix4 oldProjMat = driver->getTransform(video::ETS_PROJECTION);
599         core::matrix4 oldViewMat = driver->getTransform(video::ETS_VIEW);
600
601         driver->setViewPort(rect);
602         driver->setTransform(video::ETS_PROJECTION, core::matrix4());
603         driver->setTransform(video::ETS_VIEW, core::matrix4());
604
605         core::matrix4 matrix;
606         matrix.makeIdentity();
607
608         video::SMaterial &material = m_meshbuffer->getMaterial();
609         material.setFlag(video::EMF_TRILINEAR_FILTER, true);
610         material.Lighting = false;
611         material.TextureLayer[0].Texture = minimap_texture;
612         material.TextureLayer[1].Texture = data->heightmap_texture;
613
614         if (m_enable_shaders && data->mode.type == MINIMAP_TYPE_SURFACE) {
615                 u16 sid = m_shdrsrc->getShader("minimap_shader", TILE_MATERIAL_ALPHA);
616                 material.MaterialType = m_shdrsrc->getShaderInfo(sid).material;
617         } else {
618                 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
619         }
620
621         if (data->minimap_shape_round)
622                 matrix.setRotationDegrees(core::vector3df(0, 0, 360 - m_angle));
623
624         // Draw minimap
625         driver->setTransform(video::ETS_WORLD, matrix);
626         driver->setMaterial(material);
627         driver->drawMeshBuffer(m_meshbuffer);
628
629         // Draw overlay
630         video::ITexture *minimap_overlay = data->minimap_shape_round ?
631                 data->minimap_overlay_round : data->minimap_overlay_square;
632         material.TextureLayer[0].Texture = minimap_overlay;
633         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
634         driver->setMaterial(material);
635         driver->drawMeshBuffer(m_meshbuffer);
636
637         // Draw player marker on minimap
638         if (data->minimap_shape_round) {
639                 matrix.setRotationDegrees(core::vector3df(0, 0, 0));
640         } else {
641                 matrix.setRotationDegrees(core::vector3df(0, 0, m_angle));
642         }
643
644         material.TextureLayer[0].Texture = data->player_marker;
645         driver->setTransform(video::ETS_WORLD, matrix);
646         driver->setMaterial(material);
647         driver->drawMeshBuffer(m_meshbuffer);
648
649         // Reset transformations
650         driver->setTransform(video::ETS_VIEW, oldViewMat);
651         driver->setTransform(video::ETS_PROJECTION, oldProjMat);
652         driver->setViewPort(oldViewPort);
653
654         // Draw player markers
655         v2s32 s_pos(rect.UpperLeftCorner.X, rect.UpperLeftCorner.Y);
656         core::dimension2di imgsize(data->object_marker_red->getOriginalSize());
657         core::rect<s32> img_rect(0, 0, imgsize.Width, imgsize.Height);
658         static const video::SColor col(255, 255, 255, 255);
659         static const video::SColor c[4] = {col, col, col, col};
660         f32 sin_angle = std::sin(m_angle * core::DEGTORAD);
661         f32 cos_angle = std::cos(m_angle * core::DEGTORAD);
662         s32 marker_size2 =  0.025 * (float)rect.getWidth();;
663         for (std::list<v2f>::const_iterator
664                         i = m_active_markers.begin();
665                         i != m_active_markers.end(); ++i) {
666                 v2f posf = *i;
667                 if (data->minimap_shape_round) {
668                         f32 t1 = posf.X * cos_angle - posf.Y * sin_angle;
669                         f32 t2 = posf.X * sin_angle + posf.Y * cos_angle;
670                         posf.X = t1;
671                         posf.Y = t2;
672                 }
673                 posf.X = (posf.X + 0.5) * (float)rect.getWidth();
674                 posf.Y = (posf.Y + 0.5) * (float)rect.getHeight();
675                 core::rect<s32> dest_rect(
676                         s_pos.X + posf.X - marker_size2,
677                         s_pos.Y + posf.Y - marker_size2,
678                         s_pos.X + posf.X + marker_size2,
679                         s_pos.Y + posf.Y + marker_size2);
680                 driver->draw2DImage(data->object_marker_red, dest_rect,
681                         img_rect, &dest_rect, &c[0], true);
682         }
683 }
684
685 MinimapMarker* Minimap::addMarker(scene::ISceneNode *parent_node)
686 {
687         MinimapMarker *m = new MinimapMarker(parent_node);
688         m_markers.push_back(m);
689         return m;
690 }
691
692 void Minimap::removeMarker(MinimapMarker **m)
693 {
694         m_markers.remove(*m);
695         delete *m;
696         *m = nullptr;
697 }
698
699 void Minimap::updateActiveMarkers()
700 {
701         video::IImage *minimap_mask = data->minimap_shape_round ?
702                 data->minimap_mask_round : data->minimap_mask_square;
703
704         m_active_markers.clear();
705         v3f cam_offset = intToFloat(client->getCamera()->getOffset(), BS);
706         v3s16 pos_offset = data->pos - v3s16(data->mode.map_size / 2,
707                         data->mode.scan_height / 2,
708                         data->mode.map_size / 2);
709
710         for (MinimapMarker *marker : m_markers) {
711                 v3s16 pos = floatToInt(marker->parent_node->getAbsolutePosition() +
712                         cam_offset, BS) - pos_offset;
713                 if (pos.X < 0 || pos.X > data->mode.map_size ||
714                                 pos.Y < 0 || pos.Y > data->mode.scan_height ||
715                                 pos.Z < 0 || pos.Z > data->mode.map_size) {
716                         continue;
717                 }
718                 pos.X = ((float)pos.X / data->mode.map_size) * MINIMAP_MAX_SX;
719                 pos.Z = ((float)pos.Z / data->mode.map_size) * MINIMAP_MAX_SY;
720                 const video::SColor &mask_col = minimap_mask->getPixel(pos.X, pos.Z);
721                 if (!mask_col.getAlpha()) {
722                         continue;
723                 }
724
725                 m_active_markers.emplace_back(((float)pos.X / (float)MINIMAP_MAX_SX) - 0.5,
726                         (1.0 - (float)pos.Z / (float)MINIMAP_MAX_SY) - 0.5);
727         }
728 }
729
730 ////
731 //// MinimapMapblock
732 ////
733
734 void MinimapMapblock::getMinimapNodes(VoxelManipulator *vmanip, const v3s16 &pos)
735 {
736
737         for (s16 x = 0; x < MAP_BLOCKSIZE; x++)
738         for (s16 z = 0; z < MAP_BLOCKSIZE; z++) {
739                 s16 air_count = 0;
740                 bool surface_found = false;
741                 MinimapPixel *mmpixel = &data[z * MAP_BLOCKSIZE + x];
742
743                 for (s16 y = MAP_BLOCKSIZE -1; y >= 0; y--) {
744                         v3s16 p(x, y, z);
745                         MapNode n = vmanip->getNodeNoEx(pos + p);
746                         if (!surface_found && n.getContent() != CONTENT_AIR) {
747                                 mmpixel->height = y;
748                                 mmpixel->n = n;
749                                 surface_found = true;
750                         } else if (n.getContent() == CONTENT_AIR) {
751                                 air_count++;
752                         }
753                 }
754
755                 if (!surface_found)
756                         mmpixel->n = MapNode(CONTENT_AIR);
757
758                 mmpixel->air_count = air_count;
759         }
760 }