]> 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 ee29c58bac376c295d009679cd1937c4b9df56ca..59753e24694e4bcf946d614d9d8cb776299ff68f 100644 (file)
@@ -18,16 +18,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 */
 
 #include "minimap.h"
-#include "threading/mutex_auto_lock.h"
-#include "threading/semaphore.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 "shader.h"
 #include "mapblock.h"
-#include <math.h>
+#include "client/renderingengine.h"
 
 
 ////
@@ -36,16 +32,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 MinimapUpdateThread::~MinimapUpdateThread()
 {
-       for (std::map<v3s16, MinimapMapblock *>::iterator
-                       it = m_blocks_cache.begin();
-                       it != m_blocks_cache.end(); ++it) {
-               delete it->second;
+       for (auto &it : m_blocks_cache) {
+               delete it.second;
        }
 
-       for (std::deque<QueuedMinimapUpdate>::iterator
-                       it = m_update_queue.begin();
-                       it != m_update_queue.end(); ++it) {
-               QueuedMinimapUpdate &q = *it;
+       for (auto &q : m_update_queue) {
                delete q.data;
        }
 }
@@ -56,10 +47,7 @@ bool MinimapUpdateThread::pushBlockUpdate(v3s16 pos, MinimapMapblock *data)
 
        // Find if block is already in queue.
        // If it is, update the data and quit.
-       for (std::deque<QueuedMinimapUpdate>::iterator
-                       it = m_update_queue.begin();
-                       it != m_update_queue.end(); ++it) {
-               QueuedMinimapUpdate &q = *it;
+       for (QueuedMinimapUpdate &q : m_update_queue) {
                if (q.pos == pos) {
                        delete q.data;
                        q.data = data;
@@ -105,7 +93,7 @@ void MinimapUpdateThread::doUpdate()
                        // 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 == false) {
+                       if (!result.second) {
                                delete result.first->second;
                                result.first->second = update.data;
                        }
@@ -127,7 +115,6 @@ void MinimapUpdateThread::doUpdate()
 
 void MinimapUpdateThread::getMap(v3s16 pos, s16 size, s16 height)
 {
-       v3s16 region(size, 0, size);
        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);
@@ -184,10 +171,10 @@ void MinimapUpdateThread::getMap(v3s16 pos, s16 size, s16 height)
 //// Mapper
 ////
 
-Minimap::Minimap(IrrlichtDevice *device, Client *client)
+Minimap::Minimap(Client *client)
 {
        this->client    = client;
-       this->driver    = device->getVideoDriver();
+       this->driver    = RenderingEngine::get_video_driver();
        this->m_tsrc    = client->getTextureSource();
        this->m_shdrsrc = client->getShaderSource();
        this->m_ndef    = client->getNodeDefManager();
@@ -204,8 +191,6 @@ Minimap::Minimap(IrrlichtDevice *device, Client *client)
        data->mode              = MINIMAP_MODE_OFF;
        data->is_radar          = false;
        data->map_invalidated   = true;
-       data->heightmap_image   = NULL;
-       data->minimap_image     = NULL;
        data->texture           = NULL;
        data->heightmap_texture = NULL;
        data->minimap_shape_round = g_settings->getBool("minimap_shape_round");
@@ -272,6 +257,28 @@ void Minimap::toggleMinimapShape()
        m_minimap_update_thread->deferUpdate();
 }
 
+void Minimap::setMinimapShape(MinimapShape shape)
+{
+       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 Minimap::setMinimapMode(MinimapMode mode)
 {
        static const MinimapModeDef modedefs[MINIMAP_MODE_COUNT] = {
@@ -322,13 +329,15 @@ void Minimap::setAngle(f32 angle)
 
 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];
 
-               video::SColor c(240, 0, 0, 0);
                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);
        }
@@ -337,21 +346,23 @@ void Minimap::blitMinimapPixelsToImageRadar(video::IImage *map_image)
 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)
-               video::SColor tilecolor;
                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.setGreen(tilecolor.getGreen() * f.minimap_color.getGreen() / 255);
                tilecolor.setBlue(tilecolor.getBlue() * f.minimap_color.getBlue() / 255);
                tilecolor.setAlpha(240);
 
@@ -391,7 +402,7 @@ video::ITexture *Minimap::getMinimapTexture()
        if (minimap_mask) {
                for (s16 y = 0; y < MINIMAP_MAX_SY; y++)
                for (s16 x = 0; x < MINIMAP_MAX_SX; x++) {
-                       video::SColor mask_col = minimap_mask->getPixel(x, y);
+                       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));
                }
@@ -420,9 +431,9 @@ v3f Minimap::getYawVec()
                        cos(m_angle * core::DEGTORAD),
                        sin(m_angle * core::DEGTORAD),
                        1.0);
-       } else {
-               return v3f(1.0, 0.0, 1.0);
        }
+
+       return v3f(1.0, 0.0, 1.0);
 }
 
 scene::SMeshBuffer *Minimap::getMinimapMeshBuffer()
@@ -430,7 +441,7 @@ 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);
+       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);
@@ -454,7 +465,7 @@ void Minimap::drawMinimap()
                return;
 
        updateActiveMarkers();
-       v2u32 screensize = porting::getWindowSize();
+       v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
        const u32 size = 0.25 * screensize.Y;
 
        core::rect<s32> oldViewPort = driver->getViewPort();
@@ -550,14 +561,11 @@ void Minimap::updateActiveMarkers()
        video::IImage *minimap_mask = data->minimap_shape_round ?
                data->minimap_mask_round : data->minimap_mask_square;
 
-       std::list<Nametag *> *nametags = client->getCamera()->getNametags();
+       const std::list<Nametag *> &nametags = client->getCamera()->getNametags();
 
        m_active_markers.clear();
 
-       for (std::list<Nametag *>::const_iterator
-                       i = nametags->begin();
-                       i != nametags->end(); ++i) {
-               Nametag *nametag = *i;
+       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,
@@ -570,12 +578,13 @@ void Minimap::updateActiveMarkers()
                }
                pos.X = ((float)pos.X / data->map_size) * MINIMAP_MAX_SX;
                pos.Z = ((float)pos.Z / data->map_size) * MINIMAP_MAX_SY;
-               video::SColor mask_col = minimap_mask->getPixel(pos.X, pos.Z);
+               const video::SColor &mask_col = minimap_mask->getPixel(pos.X, pos.Z);
                if (!mask_col.getAlpha()) {
                        continue;
                }
-               m_active_markers.push_back(v2f(((float)pos.X / (float)MINIMAP_MAX_SX) - 0.5,
-                       (1.0 - (float)pos.Z / (float)MINIMAP_MAX_SY) - 0.5));
+
+               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);
        }
 }
 
@@ -583,7 +592,7 @@ void Minimap::updateActiveMarkers()
 //// MinimapMapblock
 ////
 
-void MinimapMapblock::getMinimapNodes(VoxelManipulator *vmanip, v3s16 pos)
+void MinimapMapblock::getMinimapNodes(VoxelManipulator *vmanip, const v3s16 &pos)
 {
 
        for (s16 x = 0; x < MAP_BLOCKSIZE; x++)