]> git.lizzy.rs Git - minetest.git/blobdiff - src/minimap.cpp
FormSpec : Add an auto vertical scrollbar to the textarea
[minetest.git] / src / minimap.cpp
index 02660f969fe892e2f89ced604a52b39deb12a866..59753e24694e4bcf946d614d9d8cb776299ff68f 100644 (file)
@@ -18,420 +18,430 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 */
 
 #include "minimap.h"
-#include "logoutputbuffer.h"
-#include "jthread/jmutexautolock.h"
-#include "jthread/jsemaphore.h"
+#include "client.h"
 #include "clientmap.h"
 #include "settings.h"
-#include "nodedef.h"
-#include "porting.h"
-#include "util/numeric.h"
-#include "util/string.h"
-#include <math.h>
-
-QueuedMinimapUpdate::QueuedMinimapUpdate():
-       pos(-1337,-1337,-1337),
-       data(NULL)
-{
-}
+#include "shader.h"
+#include "mapblock.h"
+#include "client/renderingengine.h"
 
-QueuedMinimapUpdate::~QueuedMinimapUpdate()
-{
-       delete data;
-}
 
-MinimapUpdateQueue::MinimapUpdateQueue()
-{
-}
+////
+//// MinimapUpdateThread
+////
 
-MinimapUpdateQueue::~MinimapUpdateQueue()
+MinimapUpdateThread::~MinimapUpdateThread()
 {
-       JMutexAutoLock lock(m_mutex);
+       for (auto &it : m_blocks_cache) {
+               delete it.second;
+       }
 
-       for (std::list<QueuedMinimapUpdate*>::iterator
-                       i = m_queue.begin();
-                       i != m_queue.end(); ++i) {
-               QueuedMinimapUpdate *q = *i;
-               delete q;
+       for (auto &q : m_update_queue) {
+               delete q.data;
        }
 }
 
-bool MinimapUpdateQueue::addBlock(v3s16 pos, MinimapMapblock *data)
+bool MinimapUpdateThread::pushBlockUpdate(v3s16 pos, MinimapMapblock *data)
 {
-       DSTACK(__FUNCTION_NAME);
-
-       JMutexAutoLock lock(m_mutex);
-
-       /*
-               Find if block is already in queue.
-               If it is, update the data and quit.
-       */
-       for (std::list<QueuedMinimapUpdate*>::iterator
-                       i = m_queue.begin();
-                       i != m_queue.end(); ++i) {
-               QueuedMinimapUpdate *q = *i;
-               if (q->pos == pos) {
-                       delete q->data;
-                       q->data = data;
+       MutexAutoLock lock(m_queue_mutex);
+
+       // Find if block is already in queue.
+       // If it is, update the data and quit.
+       for (QueuedMinimapUpdate &q : m_update_queue) {
+               if (q.pos == pos) {
+                       delete q.data;
+                       q.data = data;
                        return false;
                }
        }
 
-       /*
-               Add the block
-       */
-       QueuedMinimapUpdate *q = new QueuedMinimapUpdate;
-       q->pos = pos;
-       q->data = data;
-       m_queue.push_back(q);
+       // Add the block
+       QueuedMinimapUpdate q;
+       q.pos  = pos;
+       q.data = data;
+       m_update_queue.push_back(q);
+
        return true;
 }
 
-QueuedMinimapUpdate * MinimapUpdateQueue::pop()
+bool MinimapUpdateThread::popBlockUpdate(QueuedMinimapUpdate *update)
 {
-       JMutexAutoLock lock(m_mutex);
-
-       for (std::list<QueuedMinimapUpdate*>::iterator
-                       i = m_queue.begin();
-                       i != m_queue.end(); i++) {
-               QueuedMinimapUpdate *q = *i;
-               m_queue.erase(i);
-               return q;
-       }
-       return NULL;
-}
+       MutexAutoLock lock(m_queue_mutex);
 
-/*
-       Minimap update thread
-*/
+       if (m_update_queue.empty())
+               return false;
 
-void MinimapUpdateThread::Stop()
-{
-       JThread::Stop();
+       *update = m_update_queue.front();
+       m_update_queue.pop_front();
 
-       // give us a nudge
-       m_queue_sem.Post();
+       return true;
 }
 
-void MinimapUpdateThread::enqueue_Block(v3s16 pos, MinimapMapblock *data)
+void MinimapUpdateThread::enqueueBlock(v3s16 pos, MinimapMapblock *data)
 {
-       if (m_queue.addBlock(pos, data))
-               // we had to allocate a new block
-               m_queue_sem.Post();
+       pushBlockUpdate(pos, data);
+       deferUpdate();
 }
 
-void MinimapUpdateThread::forceUpdate()
-{
-       m_queue_sem.Post();
-}
 
-void *MinimapUpdateThread::Thread()
+void MinimapUpdateThread::doUpdate()
 {
-       ThreadStarted();
-
-       log_register_thread("MinimapUpdateThread");
-
-       DSTACK(__FUNCTION_NAME);
-
-       BEGIN_DEBUG_EXCEPTION_HANDLER
-
-       porting::setThreadName("MinimapUpdateThread");
-
-       while (!StopRequested()) {
-
-               m_queue_sem.Wait();
-               if (StopRequested()) break;
-
-               while (m_queue.size()) {
-                       QueuedMinimapUpdate *q = m_queue.pop();
-                       if (!q)
-                               break;
+       QueuedMinimapUpdate update;
+
+       while (popBlockUpdate(&update)) {
+               if (update.data) {
+                       // Swap two values in the map using single lookup
+                       std::pair<std::map<v3s16, MinimapMapblock*>::iterator, bool>
+                           result = m_blocks_cache.insert(std::make_pair(update.pos, update.data));
+                       if (!result.second) {
+                               delete result.first->second;
+                               result.first->second = update.data;
+                       }
+               } else {
                        std::map<v3s16, MinimapMapblock *>::iterator it;
-                       it = m_blocks_cache.find(q->pos);
-                       if (q->data) {
-                               m_blocks_cache[q->pos] = q->data;
-                       } else if (it != m_blocks_cache.end()) {
+                       it = m_blocks_cache.find(update.pos);
+                       if (it != m_blocks_cache.end()) {
                                delete it->second;
                                m_blocks_cache.erase(it);
                        }
                }
-
-               if (data->map_invalidated) {
-                       if (data->mode != MINIMAP_MODE_OFF) {
-                               getMap(data->pos, data->map_size, data->scan_height, data->radar);
-                               data->map_invalidated = false;
-                       }
-               }
        }
-       END_DEBUG_EXCEPTION_HANDLER(errorstream)
 
-       return NULL;
+       if (data->map_invalidated && data->mode != MINIMAP_MODE_OFF) {
+               getMap(data->pos, data->map_size, data->scan_height);
+               data->map_invalidated = false;
+       }
 }
 
-MinimapUpdateThread::~MinimapUpdateThread()
+void MinimapUpdateThread::getMap(v3s16 pos, s16 size, s16 height)
 {
-       for (std::map<v3s16, MinimapMapblock *>::iterator
-                       it = m_blocks_cache.begin();
-                       it != m_blocks_cache.end(); it++) {
-               delete it->second;
+       v3s16 pos_min(pos.X - size / 2, pos.Y - height / 2, pos.Z - size / 2);
+       v3s16 pos_max(pos_min.X + size - 1, pos.Y + height / 2, pos_min.Z + size - 1);
+       v3s16 blockpos_min = getNodeBlockPos(pos_min);
+       v3s16 blockpos_max = getNodeBlockPos(pos_max);
+
+// clear the map
+       for (int z = 0; z < size; z++)
+       for (int x = 0; x < size; x++) {
+               MinimapPixel &mmpixel = data->minimap_scan[x + z * size];
+               mmpixel.air_count = 0;
+               mmpixel.height = 0;
+               mmpixel.n = MapNode(CONTENT_AIR);
        }
-}
 
-MinimapPixel *MinimapUpdateThread::getMinimapPixel (v3s16 pos, s16 height, s16 &pixel_height)
-{
-       pixel_height = height - MAP_BLOCKSIZE;
-       v3s16 blockpos_max, blockpos_min, relpos;
-       getNodeBlockPosWithOffset(v3s16(pos.X, pos.Y - height / 2, pos.Z), blockpos_min, relpos);
-       getNodeBlockPosWithOffset(v3s16(pos.X, pos.Y + height / 2, pos.Z), blockpos_max, relpos);
-       std::map<v3s16, MinimapMapblock *>::iterator it;
-       for (s16 i = blockpos_max.Y; i > blockpos_min.Y - 1; i--) {
-               it = m_blocks_cache.find(v3s16(blockpos_max.X, i, blockpos_max.Z));
-               if (it != m_blocks_cache.end()) {
-                       MinimapPixel *pixel = &it->second->data[relpos.X + relpos.Z * MAP_BLOCKSIZE];
-                       if (pixel->id != CONTENT_AIR) {
-                               pixel_height += pixel->height;
-                               return pixel;
+// draw the map
+       v3s16 blockpos;
+       for (blockpos.Z = blockpos_min.Z; blockpos.Z <= blockpos_max.Z; ++blockpos.Z)
+       for (blockpos.Y = blockpos_min.Y; blockpos.Y <= blockpos_max.Y; ++blockpos.Y)
+       for (blockpos.X = blockpos_min.X; blockpos.X <= blockpos_max.X; ++blockpos.X) {
+               std::map<v3s16, MinimapMapblock *>::const_iterator pblock =
+                       m_blocks_cache.find(blockpos);
+               if (pblock == m_blocks_cache.end())
+                       continue;
+               const MinimapMapblock &block = *pblock->second;
+
+               v3s16 block_node_min(blockpos * MAP_BLOCKSIZE);
+               v3s16 block_node_max(block_node_min + MAP_BLOCKSIZE - 1);
+               // clip
+               v3s16 range_min = componentwise_max(block_node_min, pos_min);
+               v3s16 range_max = componentwise_min(block_node_max, pos_max);
+
+               v3s16 pos;
+               pos.Y = range_min.Y;
+               for (pos.Z = range_min.Z; pos.Z <= range_max.Z; ++pos.Z)
+               for (pos.X = range_min.X; pos.X <= range_max.X; ++pos.X) {
+                       v3s16 inblock_pos = pos - block_node_min;
+                       const MinimapPixel &in_pixel =
+                               block.data[inblock_pos.Z * MAP_BLOCKSIZE + inblock_pos.X];
+
+                       v3s16 inmap_pos = pos - pos_min;
+                       MinimapPixel &out_pixel =
+                               data->minimap_scan[inmap_pos.X + inmap_pos.Z * size];
+
+                       out_pixel.air_count += in_pixel.air_count;
+                       if (in_pixel.n.param0 != CONTENT_AIR) {
+                               out_pixel.n = in_pixel.n;
+                               out_pixel.height = inmap_pos.Y + in_pixel.height;
                        }
                }
-               pixel_height -= MAP_BLOCKSIZE;
        }
-       return NULL;
 }
 
-s16 MinimapUpdateThread::getAirCount (v3s16 pos, s16 height)
-{
-       s16 air_count = 0;
-       v3s16 blockpos_max, blockpos_min, relpos;
-       getNodeBlockPosWithOffset(v3s16(pos.X, pos.Y - height / 2, pos.Z), blockpos_min, relpos);
-       getNodeBlockPosWithOffset(v3s16(pos.X, pos.Y + height / 2, pos.Z), blockpos_max, relpos);
-       std::map<v3s16, MinimapMapblock *>::iterator it;
-       for (s16 i = blockpos_max.Y; i > blockpos_min.Y - 1; i--) {
-               it = m_blocks_cache.find(v3s16(blockpos_max.X, i, blockpos_max.Z));
-               if (it != m_blocks_cache.end()) {
-                       MinimapPixel *pixel = &it->second->data[relpos.X + relpos.Z * MAP_BLOCKSIZE];
-                       air_count += pixel->air_count;
-               }
-       }
-       return air_count;
-}
+////
+//// Mapper
+////
 
-void MinimapUpdateThread::getMap (v3s16 pos, s16 size, s16 height, bool radar)
+Minimap::Minimap(Client *client)
 {
-       v3s16 p = v3s16 (pos.X - size / 2, pos.Y, pos.Z - size / 2);
-
-       for (s16 x = 0; x < size; x++) {
-               for (s16 z = 0; z < size; z++){
-                       u16 id = CONTENT_AIR;
-                       MinimapPixel* minimap_pixel = &data->minimap_scan[x + z * size];
-                       if (!radar) {
-                               s16 pixel_height = 0;
-                               MinimapPixel* cached_pixel =
-                                       getMinimapPixel(v3s16(p.X + x, p.Y, p.Z + z), height, pixel_height);
-                               if (cached_pixel) {
-                                       id = cached_pixel->id;
-                                       minimap_pixel->height = pixel_height;
-                               }
-                       } else {
-                               minimap_pixel->air_count = getAirCount (v3s16(p.X + x, p.Y, p.Z + z), height);
-                       }
-                       minimap_pixel->id = id;
-               }
-       }
-}
+       this->client    = client;
+       this->driver    = RenderingEngine::get_video_driver();
+       this->m_tsrc    = client->getTextureSource();
+       this->m_shdrsrc = client->getShaderSource();
+       this->m_ndef    = client->getNodeDefManager();
 
-Mapper::Mapper(IrrlichtDevice *device, Client *client)
-{
-       this->device = device;
-       this->client = client;
-       this->driver = device->getVideoDriver();
-       this->tsrc = client->getTextureSource();
-       this->player = client->getEnv().getLocalPlayer();
-       this->shdrsrc = client->getShaderSource();
+       m_angle = 0.f;
 
+       // Initialize static settings
        m_enable_shaders = g_settings->getBool("enable_shaders");
-       m_enable_shaders = g_settings->getBool("enable_shaders");
-       if (g_settings->getBool("minimap_double_scan_height")) {
-               m_surface_mode_scan_height = 256;
-       } else {
-               m_surface_mode_scan_height = 128;
-       }
+       m_surface_mode_scan_height =
+               g_settings->getBool("minimap_double_scan_height") ? 256 : 128;
+
+       // Initialize minimap data
        data = new MinimapData;
-       data->mode = MINIMAP_MODE_OFF;
-       data->radar = false;
-       data->map_invalidated = true;
-       data->heightmap_image = NULL;
-       data->minimap_image = NULL;
-       data->texture = NULL;
+       data->mode              = MINIMAP_MODE_OFF;
+       data->is_radar          = false;
+       data->map_invalidated   = true;
+       data->texture           = NULL;
+       data->heightmap_texture = NULL;
        data->minimap_shape_round = g_settings->getBool("minimap_shape_round");
-       std::string fname1 = "minimap_mask_round.png";
-       std::string fname2 = "minimap_overlay_round.png";
-       data->minimap_mask_round = driver->createImage (tsrc->getTexture(fname1),
-                       core::position2d<s32>(0,0), core::dimension2d<u32>(512,512));
-       data->minimap_overlay_round = tsrc->getTexture(fname2);
-       fname1 = "minimap_mask_square.png";
-       fname2 = "minimap_overlay_square.png";
-       data->minimap_mask_square = driver->createImage (tsrc->getTexture(fname1),
-                       core::position2d<s32>(0,0), core::dimension2d<u32>(512,512));
-       data->minimap_overlay_square = tsrc->getTexture(fname2);
-       data->player_marker = tsrc->getTexture("player_marker.png");
+
+       // Get round minimap textures
+       data->minimap_mask_round = driver->createImage(
+               m_tsrc->getTexture("minimap_mask_round.png"),
+               core::position2d<s32>(0, 0),
+               core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
+       data->minimap_overlay_round = m_tsrc->getTexture("minimap_overlay_round.png");
+
+       // Get square minimap textures
+       data->minimap_mask_square = driver->createImage(
+               m_tsrc->getTexture("minimap_mask_square.png"),
+               core::position2d<s32>(0, 0),
+               core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
+       data->minimap_overlay_square = m_tsrc->getTexture("minimap_overlay_square.png");
+
+       // Create player marker texture
+       data->player_marker = m_tsrc->getTexture("player_marker.png");
+       // Create object marker texture
+       data->object_marker_red = m_tsrc->getTexture("object_marker_red.png");
+
+       // Create mesh buffer for minimap
        m_meshbuffer = getMinimapMeshBuffer();
-       m_minimap_update_thread = new MinimapUpdateThread(device, client);
+
+       // Initialize and start thread
+       m_minimap_update_thread = new MinimapUpdateThread();
        m_minimap_update_thread->data = data;
-       m_minimap_update_thread->Start();
+       m_minimap_update_thread->start();
 }
 
-Mapper::~Mapper()
+Minimap::~Minimap()
 {
-       m_minimap_update_thread->Stop();
-       m_minimap_update_thread->Wait();
+       m_minimap_update_thread->stop();
+       m_minimap_update_thread->wait();
+
        m_meshbuffer->drop();
+
        data->minimap_mask_round->drop();
        data->minimap_mask_square->drop();
+
        driver->removeTexture(data->texture);
        driver->removeTexture(data->heightmap_texture);
        driver->removeTexture(data->minimap_overlay_round);
        driver->removeTexture(data->minimap_overlay_square);
+       driver->removeTexture(data->object_marker_red);
+
        delete data;
        delete m_minimap_update_thread;
 }
 
-void Mapper::addBlock (v3s16 pos, MinimapMapblock *data)
+void Minimap::addBlock(v3s16 pos, MinimapMapblock *data)
 {
-       m_minimap_update_thread->enqueue_Block(pos, data);
+       m_minimap_update_thread->enqueueBlock(pos, data);
 }
 
-MinimapMode Mapper::getMinimapMode()
+void Minimap::toggleMinimapShape()
 {
-       return data->mode;
+       MutexAutoLock lock(m_mutex);
+
+       data->minimap_shape_round = !data->minimap_shape_round;
+       g_settings->setBool("minimap_shape_round", data->minimap_shape_round);
+       m_minimap_update_thread->deferUpdate();
 }
 
-void Mapper::toggleMinimapShape()
+void Minimap::setMinimapShape(MinimapShape shape)
 {
-       data->minimap_shape_round = !data->minimap_shape_round;
-       g_settings->setBool(("minimap_shape_round"), data->minimap_shape_round);
+       MutexAutoLock lock(m_mutex);
+
+       if (shape == MINIMAP_SHAPE_SQUARE)
+               data->minimap_shape_round = false;
+       else if (shape == MINIMAP_SHAPE_ROUND)
+               data->minimap_shape_round = true;
+
+       g_settings->setBool("minimap_shape_round", data->minimap_shape_round);
+       m_minimap_update_thread->deferUpdate();
+}
+
+MinimapShape Minimap::getMinimapShape()
+{
+       if (data->minimap_shape_round) {
+               return MINIMAP_SHAPE_ROUND;
+       }
+
+       return MINIMAP_SHAPE_SQUARE;
 }
 
-void Mapper::setMinimapMode(MinimapMode mode)
+void Minimap::setMinimapMode(MinimapMode mode)
 {
-       static const u16 modeDefs[7 * 3] = {
-               0, 0, 0,
-               0, m_surface_mode_scan_height, 256,
-               0, m_surface_mode_scan_height, 128,
-               0, m_surface_mode_scan_height, 64,
-               1, 32, 128,
-               1, 32, 64,
-               1, 32, 32};
-
-       JMutexAutoLock lock(m_mutex);
-       data->radar = (bool)modeDefs[(int)mode * 3];
-       data->scan_height = modeDefs[(int)mode * 3 + 1];
-       data->map_size = modeDefs[(int)mode * 3 + 2];
-       data->mode = mode;
-       m_minimap_update_thread->forceUpdate();
+       static const MinimapModeDef modedefs[MINIMAP_MODE_COUNT] = {
+               {false, 0, 0},
+               {false, m_surface_mode_scan_height, 256},
+               {false, m_surface_mode_scan_height, 128},
+               {false, m_surface_mode_scan_height, 64},
+               {true, 32, 128},
+               {true, 32, 64},
+               {true, 32, 32}
+       };
+
+       if (mode >= MINIMAP_MODE_COUNT)
+               return;
+
+       MutexAutoLock lock(m_mutex);
+
+       data->is_radar    = modedefs[mode].is_radar;
+       data->scan_height = modedefs[mode].scan_height;
+       data->map_size    = modedefs[mode].map_size;
+       data->mode        = mode;
+
+       m_minimap_update_thread->deferUpdate();
 }
 
-void Mapper::setPos(v3s16 pos)
+void Minimap::setPos(v3s16 pos)
 {
-       JMutexAutoLock lock(m_mutex);
-       if (pos != data->old_pos) {
-               data->old_pos = data->pos;
-               data->pos = pos;
-               m_minimap_update_thread->forceUpdate();
+       bool do_update = false;
+
+       {
+               MutexAutoLock lock(m_mutex);
+
+               if (pos != data->old_pos) {
+                       data->old_pos = data->pos;
+                       data->pos = pos;
+                       do_update = true;
+               }
        }
+
+       if (do_update)
+               m_minimap_update_thread->deferUpdate();
+}
+
+void Minimap::setAngle(f32 angle)
+{
+       m_angle = angle;
 }
 
-video::ITexture *Mapper::getMinimapTexture()
+void Minimap::blitMinimapPixelsToImageRadar(video::IImage *map_image)
+{
+       video::SColor c(240, 0, 0, 0);
+       for (s16 x = 0; x < data->map_size; x++)
+       for (s16 z = 0; z < data->map_size; z++) {
+               MinimapPixel *mmpixel = &data->minimap_scan[x + z * data->map_size];
+
+               if (mmpixel->air_count > 0)
+                       c.setGreen(core::clamp(core::round32(32 + mmpixel->air_count * 8), 0, 255));
+               else
+                       c.setGreen(0);
+
+               map_image->setPixel(x, data->map_size - z - 1, c);
+       }
+}
+
+void Minimap::blitMinimapPixelsToImageSurface(
+       video::IImage *map_image, video::IImage *heightmap_image)
+{
+       // This variable creation/destruction has a 1% cost on rendering minimap
+       video::SColor tilecolor;
+       for (s16 x = 0; x < data->map_size; x++)
+       for (s16 z = 0; z < data->map_size; z++) {
+               MinimapPixel *mmpixel = &data->minimap_scan[x + z * data->map_size];
+
+               const ContentFeatures &f = m_ndef->get(mmpixel->n);
+               const TileDef *tile = &f.tiledef[0];
+
+               // Color of the 0th tile (mostly this is the topmost)
+               if(tile->has_color)
+                       tilecolor = tile->color;
+               else
+                       mmpixel->n.getColor(f, &tilecolor);
+
+               tilecolor.setRed(tilecolor.getRed() * f.minimap_color.getRed() / 255);
+               tilecolor.setGreen(tilecolor.getGreen() * f.minimap_color.getGreen() / 255);
+               tilecolor.setBlue(tilecolor.getBlue() * f.minimap_color.getBlue() / 255);
+               tilecolor.setAlpha(240);
+
+               map_image->setPixel(x, data->map_size - z - 1, tilecolor);
+
+               u32 h = mmpixel->height;
+               heightmap_image->setPixel(x,data->map_size - z - 1,
+                       video::SColor(255, h, h, h));
+       }
+}
+
+video::ITexture *Minimap::getMinimapTexture()
 {
        // update minimap textures when new scan is ready
-       if (!data->map_invalidated) {
-               // create minimap and heightmap image
-               core::dimension2d<u32> dim(data->map_size,data->map_size);
-               video::IImage *map_image = driver->createImage(video::ECF_A8R8G8B8, dim);
-               video::IImage *heightmap_image = driver->createImage(video::ECF_A8R8G8B8, dim);
-               video::IImage *minimap_image = driver->createImage(video::ECF_A8R8G8B8,
-                       core::dimension2d<u32>(512, 512));
-
-               video::SColor c;
-               if (!data->radar) {
-                       // surface mode
-                       for (s16 x = 0; x < data->map_size; x++) {
-                               for (s16 z = 0; z < data->map_size; z++) {
-                                       MinimapPixel* minimap_pixel = &data->minimap_scan[x + z * data->map_size];
-                                       const ContentFeatures &f = client->getNodeDefManager()->get(minimap_pixel->id);
-                                       c = f.minimap_color;
-                                       c.setAlpha(240);
-                                       map_image->setPixel(x, data->map_size - z -1, c);
-                                       u32 h = minimap_pixel->height;
-                                       heightmap_image->setPixel(x,data->map_size -z -1,
-                                               video::SColor(255, h, h, h));
-                               }
-                       }
-               } else {
-                       // radar mode
-                       c = video::SColor (240, 0 , 0, 0);
-                       for (s16 x = 0; x < data->map_size; x++) {
-                               for (s16 z = 0; z < data->map_size; z++) {
-                                       MinimapPixel* minimap_pixel = &data->minimap_scan[x + z * data->map_size];
-                                       if (minimap_pixel->air_count > 0) {
-                                               c.setGreen(core::clamp(core::round32(32 + minimap_pixel->air_count * 8), 0, 255));
-                                       } else {
-                                               c.setGreen(0);
-                                       }
-                                       map_image->setPixel(x, data->map_size - z -1, c);
-                               }
-                       }
+       if (data->map_invalidated)
+               return data->texture;
+
+       // create minimap and heightmap images in memory
+       core::dimension2d<u32> dim(data->map_size, data->map_size);
+       video::IImage *map_image       = driver->createImage(video::ECF_A8R8G8B8, dim);
+       video::IImage *heightmap_image = driver->createImage(video::ECF_A8R8G8B8, dim);
+       video::IImage *minimap_image   = driver->createImage(video::ECF_A8R8G8B8,
+               core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
+
+       // Blit MinimapPixels to images
+       if (data->is_radar)
+               blitMinimapPixelsToImageRadar(map_image);
+       else
+               blitMinimapPixelsToImageSurface(map_image, heightmap_image);
+
+       map_image->copyToScaling(minimap_image);
+       map_image->drop();
+
+       video::IImage *minimap_mask = data->minimap_shape_round ?
+               data->minimap_mask_round : data->minimap_mask_square;
+
+       if (minimap_mask) {
+               for (s16 y = 0; y < MINIMAP_MAX_SY; y++)
+               for (s16 x = 0; x < MINIMAP_MAX_SX; x++) {
+                       const video::SColor &mask_col = minimap_mask->getPixel(x, y);
+                       if (!mask_col.getAlpha())
+                               minimap_image->setPixel(x, y, video::SColor(0,0,0,0));
                }
+       }
 
-               map_image->copyToScaling(minimap_image);
-               map_image->drop();
+       if (data->texture)
+               driver->removeTexture(data->texture);
+       if (data->heightmap_texture)
+               driver->removeTexture(data->heightmap_texture);
 
-               video::IImage *minimap_mask;
-               if (data->minimap_shape_round) {
-                       minimap_mask = data->minimap_mask_round;
-               } else {
-                       minimap_mask = data->minimap_mask_square;
-               }
-               for (s16 x = 0; x < 512; x++) {
-                       for (s16 y = 0; y < 512; y++) {
-                               video::SColor mask_col = minimap_mask->getPixel(x, y);
-                               if (!mask_col.getAlpha()) {
-                                       minimap_image->setPixel(x, y, video::SColor(0,0,0,0));
-                               }
-                       }
-               }
+       data->texture = driver->addTexture("minimap__", minimap_image);
+       data->heightmap_texture =
+               driver->addTexture("minimap_heightmap__", heightmap_image);
+       minimap_image->drop();
+       heightmap_image->drop();
 
-               if (data->texture) {
-                       driver->removeTexture(data->texture);
-               }
-               if (data->heightmap_texture) {
-                       driver->removeTexture(data->heightmap_texture);
-               }
-               data->texture = driver->addTexture("minimap__", minimap_image);
-               data->heightmap_texture = driver->addTexture("minimap_heightmap__", heightmap_image);
-               minimap_image->drop();
-               heightmap_image->drop();
+       data->map_invalidated = true;
 
-               data->map_invalidated = true;
-       }
        return data->texture;
 }
 
-v3f Mapper::getYawVec()
+v3f Minimap::getYawVec()
 {
        if (data->minimap_shape_round) {
-               return v3f(cos(player->getYaw()* core::DEGTORAD),
-                       sin(player->getYaw()* core::DEGTORAD), 1.0);
-       } else {
-               return v3f(1.0, 0.0, 1.0);
+               return v3f(
+                       cos(m_angle * core::DEGTORAD),
+                       sin(m_angle * core::DEGTORAD),
+                       1.0);
        }
+
+       return v3f(1.0, 0.0, 1.0);
 }
 
-scene::SMeshBuffer *Mapper::getMinimapMeshBuffer()
+scene::SMeshBuffer *Minimap::getMinimapMeshBuffer()
 {
        scene::SMeshBuffer *buf = new scene::SMeshBuffer();
        buf->Vertices.set_used(4);
-       buf->Indices .set_used(6);
-       video::SColor c(255, 255, 255, 255);
+       buf->Indices.set_used(6);
+       static const video::SColor c(255, 255, 255, 255);
 
        buf->Vertices[0] = video::S3DVertex(-1, -1, 0, 0, 0, 1, c, 0, 1);
        buf->Vertices[1] = video::S3DVertex(-1,  1, 0, 0, 0, 1, c, 0, 0);
@@ -448,61 +458,164 @@ scene::SMeshBuffer *Mapper::getMinimapMeshBuffer()
        return buf;
 }
 
-void Mapper::drawMinimap()
+void Minimap::drawMinimap()
 {
-       v2u32 screensize = porting::getWindowSize();
-       u32 size = 0.25 * screensize.Y;
-       video::ITexture* minimap_texture = getMinimapTexture();
-       core::matrix4 matrix;
+       video::ITexture *minimap_texture = getMinimapTexture();
+       if (!minimap_texture)
+               return;
+
+       updateActiveMarkers();
+       v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
+       const u32 size = 0.25 * screensize.Y;
 
        core::rect<s32> oldViewPort = driver->getViewPort();
-       driver->setViewPort(core::rect<s32>(screensize.X - size - 10, 10,
-               screensize.X - 10, size + 10));
        core::matrix4 oldProjMat = driver->getTransform(video::ETS_PROJECTION);
-       driver->setTransform(video::ETS_PROJECTION, core::matrix4());
        core::matrix4 oldViewMat = driver->getTransform(video::ETS_VIEW);
+
+       driver->setViewPort(core::rect<s32>(
+               screensize.X - size - 10, 10,
+               screensize.X - 10, size + 10));
+       driver->setTransform(video::ETS_PROJECTION, core::matrix4());
        driver->setTransform(video::ETS_VIEW, core::matrix4());
+
+       core::matrix4 matrix;
        matrix.makeIdentity();
 
-       if (minimap_texture) {
-               video::SMaterial& material = m_meshbuffer->getMaterial();
-               material.setFlag(video::EMF_TRILINEAR_FILTER, true);
-               material.Lighting = false;
-               material.TextureLayer[0].Texture = minimap_texture;
-               material.TextureLayer[1].Texture = data->heightmap_texture;
-               if (m_enable_shaders && !data->radar) {
-                       u16 sid = shdrsrc->getShader("minimap_shader", 1, 1);
-                       material.MaterialType = shdrsrc->getShaderInfo(sid).material;
-               } else {
-                       material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
-               }
+       video::SMaterial &material = m_meshbuffer->getMaterial();
+       material.setFlag(video::EMF_TRILINEAR_FILTER, true);
+       material.Lighting = false;
+       material.TextureLayer[0].Texture = minimap_texture;
+       material.TextureLayer[1].Texture = data->heightmap_texture;
+
+       if (m_enable_shaders && !data->is_radar) {
+               u16 sid = m_shdrsrc->getShader("minimap_shader", 1, 1);
+               material.MaterialType = m_shdrsrc->getShaderInfo(sid).material;
+       } else {
+               material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
+       }
+
+       if (data->minimap_shape_round)
+               matrix.setRotationDegrees(core::vector3df(0, 0, 360 - m_angle));
+
+       // Draw minimap
+       driver->setTransform(video::ETS_WORLD, matrix);
+       driver->setMaterial(material);
+       driver->drawMeshBuffer(m_meshbuffer);
+
+       // Draw overlay
+       video::ITexture *minimap_overlay = data->minimap_shape_round ?
+               data->minimap_overlay_round : data->minimap_overlay_square;
+       material.TextureLayer[0].Texture = minimap_overlay;
+       material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
+       driver->setMaterial(material);
+       driver->drawMeshBuffer(m_meshbuffer);
+
+       // If round minimap, draw player marker
+       if (!data->minimap_shape_round) {
+               matrix.setRotationDegrees(core::vector3df(0, 0, m_angle));
+               material.TextureLayer[0].Texture = data->player_marker;
 
-               if (data->minimap_shape_round)
-                       matrix.setRotationDegrees(core::vector3df(0, 0, 360 - player->getYaw()));
                driver->setTransform(video::ETS_WORLD, matrix);
                driver->setMaterial(material);
                driver->drawMeshBuffer(m_meshbuffer);
-               video::ITexture *minimap_overlay;
+       }
+
+       // Reset transformations
+       driver->setTransform(video::ETS_VIEW, oldViewMat);
+       driver->setTransform(video::ETS_PROJECTION, oldProjMat);
+       driver->setViewPort(oldViewPort);
+
+       // Draw player markers
+       v2s32 s_pos(screensize.X - size - 10, 10);
+       core::dimension2di imgsize(data->object_marker_red->getOriginalSize());
+       core::rect<s32> img_rect(0, 0, imgsize.Width, imgsize.Height);
+       static const video::SColor col(255, 255, 255, 255);
+       static const video::SColor c[4] = {col, col, col, col};
+       f32 sin_angle = sin(m_angle * core::DEGTORAD);
+       f32 cos_angle = cos(m_angle * core::DEGTORAD);
+       s32 marker_size2 =  0.025 * (float)size;
+       for (std::list<v2f>::const_iterator
+                       i = m_active_markers.begin();
+                       i != m_active_markers.end(); ++i) {
+               v2f posf = *i;
                if (data->minimap_shape_round) {
-                       minimap_overlay = data->minimap_overlay_round;
-               } else {
-                       minimap_overlay = data->minimap_overlay_square;
+                       f32 t1 = posf.X * cos_angle - posf.Y * sin_angle;
+                       f32 t2 = posf.X * sin_angle + posf.Y * cos_angle;
+                       posf.X = t1;
+                       posf.Y = t2;
                }
-               material.TextureLayer[0].Texture = minimap_overlay;
-               material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
-               driver->setMaterial(material);
-               driver->drawMeshBuffer(m_meshbuffer);
+               posf.X = (posf.X + 0.5) * (float)size;
+               posf.Y = (posf.Y + 0.5) * (float)size;
+               core::rect<s32> dest_rect(
+                       s_pos.X + posf.X - marker_size2,
+                       s_pos.Y + posf.Y - marker_size2,
+                       s_pos.X + posf.X + marker_size2,
+                       s_pos.Y + posf.Y + marker_size2);
+               driver->draw2DImage(data->object_marker_red, dest_rect,
+                       img_rect, &dest_rect, &c[0], true);
+       }
+}
 
-               if (!data->minimap_shape_round) {
-                       matrix.setRotationDegrees(core::vector3df(0, 0, player->getYaw()));
-                       driver->setTransform(video::ETS_WORLD, matrix);
-                       material.TextureLayer[0].Texture = data->player_marker;
-                       driver->setMaterial(material);
-                       driver->drawMeshBuffer(m_meshbuffer);
+void Minimap::updateActiveMarkers()
+{
+       video::IImage *minimap_mask = data->minimap_shape_round ?
+               data->minimap_mask_round : data->minimap_mask_square;
+
+       const std::list<Nametag *> &nametags = client->getCamera()->getNametags();
+
+       m_active_markers.clear();
+
+       for (Nametag *nametag : nametags) {
+               v3s16 pos = floatToInt(nametag->parent_node->getPosition() +
+                       intToFloat(client->getCamera()->getOffset(), BS), BS);
+               pos -= data->pos - v3s16(data->map_size / 2,
+                               data->scan_height / 2,
+                               data->map_size / 2);
+               if (pos.X < 0 || pos.X > data->map_size ||
+                               pos.Y < 0 || pos.Y > data->scan_height ||
+                               pos.Z < 0 || pos.Z > data->map_size) {
+                       continue;
                }
+               pos.X = ((float)pos.X / data->map_size) * MINIMAP_MAX_SX;
+               pos.Z = ((float)pos.Z / data->map_size) * MINIMAP_MAX_SY;
+               const video::SColor &mask_col = minimap_mask->getPixel(pos.X, pos.Z);
+               if (!mask_col.getAlpha()) {
+                       continue;
+               }
+
+               m_active_markers.emplace_back(((float)pos.X / (float)MINIMAP_MAX_SX) - 0.5,
+                       (1.0 - (float)pos.Z / (float)MINIMAP_MAX_SY) - 0.5);
        }
+}
 
-       driver->setTransform(video::ETS_VIEW, oldViewMat);
-       driver->setTransform(video::ETS_PROJECTION, oldProjMat);
-       driver->setViewPort(oldViewPort);
+////
+//// MinimapMapblock
+////
+
+void MinimapMapblock::getMinimapNodes(VoxelManipulator *vmanip, const v3s16 &pos)
+{
+
+       for (s16 x = 0; x < MAP_BLOCKSIZE; x++)
+       for (s16 z = 0; z < MAP_BLOCKSIZE; z++) {
+               s16 air_count = 0;
+               bool surface_found = false;
+               MinimapPixel *mmpixel = &data[z * MAP_BLOCKSIZE + x];
+
+               for (s16 y = MAP_BLOCKSIZE -1; y >= 0; y--) {
+                       v3s16 p(x, y, z);
+                       MapNode n = vmanip->getNodeNoEx(pos + p);
+                       if (!surface_found && n.getContent() != CONTENT_AIR) {
+                               mmpixel->height = y;
+                               mmpixel->n = n;
+                               surface_found = true;
+                       } else if (n.getContent() == CONTENT_AIR) {
+                               air_count++;
+                       }
+               }
+
+               if (!surface_found)
+                       mmpixel->n = MapNode(CONTENT_AIR);
+
+               mmpixel->air_count = air_count;
+       }
 }