3 Copyright (C) 2010-2015 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
23 #include "clientmap.h"
27 #include "client/renderingengine.h"
30 //// MinimapUpdateThread
33 MinimapUpdateThread::~MinimapUpdateThread()
35 for (auto &it : m_blocks_cache) {
39 for (auto &q : m_update_queue) {
44 bool MinimapUpdateThread::pushBlockUpdate(v3s16 pos, MinimapMapblock *data)
46 MutexAutoLock lock(m_queue_mutex);
48 // Find if block is already in queue.
49 // If it is, update the data and quit.
50 for (QueuedMinimapUpdate &q : m_update_queue) {
59 QueuedMinimapUpdate q;
62 m_update_queue.push_back(q);
67 bool MinimapUpdateThread::popBlockUpdate(QueuedMinimapUpdate *update)
69 MutexAutoLock lock(m_queue_mutex);
71 if (m_update_queue.empty())
74 *update = m_update_queue.front();
75 m_update_queue.pop_front();
80 void MinimapUpdateThread::enqueueBlock(v3s16 pos, MinimapMapblock *data)
82 pushBlockUpdate(pos, data);
86 void MinimapUpdateThread::doUpdate()
88 QueuedMinimapUpdate update;
90 while (popBlockUpdate(&update)) {
92 // Swap two values in the map using single lookup
93 std::pair<std::map<v3s16, MinimapMapblock *>::iterator, bool>
94 result = m_blocks_cache.insert(std::make_pair(
95 update.pos, update.data));
97 delete result.first->second;
98 result.first->second = update.data;
101 std::map<v3s16, MinimapMapblock *>::iterator it;
102 it = m_blocks_cache.find(update.pos);
103 if (it != m_blocks_cache.end()) {
105 m_blocks_cache.erase(it);
110 if (data->map_invalidated && data->mode != MINIMAP_MODE_OFF) {
111 getMap(data->pos, data->map_size, data->scan_height);
112 data->map_invalidated = false;
116 void MinimapUpdateThread::getMap(v3s16 pos, s16 size, s16 height)
118 v3s16 pos_min(pos.X - size / 2, pos.Y - height / 2, pos.Z - size / 2);
119 v3s16 pos_max(pos_min.X + size - 1, pos.Y + height / 2, pos_min.Z + size - 1);
120 v3s16 blockpos_min = getNodeBlockPos(pos_min);
121 v3s16 blockpos_max = getNodeBlockPos(pos_max);
124 for (int z = 0; z < size; z++)
125 for (int x = 0; x < size; x++) {
126 MinimapPixel &mmpixel = data->minimap_scan[x + z * size];
127 mmpixel.air_count = 0;
129 mmpixel.n = MapNode(CONTENT_AIR);
134 for (blockpos.Z = blockpos_min.Z; blockpos.Z <= blockpos_max.Z; ++blockpos.Z)
135 for (blockpos.Y = blockpos_min.Y; blockpos.Y <= blockpos_max.Y;
137 for (blockpos.X = blockpos_min.X; blockpos.X <= blockpos_max.X;
139 std::map<v3s16, MinimapMapblock *>::const_iterator
140 pblock = m_blocks_cache.find(blockpos);
141 if (pblock == m_blocks_cache.end())
143 const MinimapMapblock &block = *pblock->second;
145 v3s16 block_node_min(blockpos * MAP_BLOCKSIZE);
146 v3s16 block_node_max(block_node_min + MAP_BLOCKSIZE - 1);
148 v3s16 range_min = componentwise_max(
149 block_node_min, pos_min);
150 v3s16 range_max = componentwise_min(
151 block_node_max, pos_max);
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;
158 v3s16 inblock_pos = pos - block_node_min;
159 const MinimapPixel &in_pixel =
160 block.data[inblock_pos.Z * MAP_BLOCKSIZE +
163 v3s16 inmap_pos = pos - pos_min;
164 MinimapPixel &out_pixel =
165 data->minimap_scan[inmap_pos.X +
168 out_pixel.air_count += in_pixel.air_count;
169 if (in_pixel.n.param0 != CONTENT_AIR) {
170 out_pixel.n = in_pixel.n;
183 Minimap::Minimap(Client *client)
185 this->client = client;
186 this->driver = RenderingEngine::get_video_driver();
187 this->m_tsrc = client->getTextureSource();
188 this->m_shdrsrc = client->getShaderSource();
189 this->m_ndef = client->getNodeDefManager();
193 // Initialize static settings
194 m_enable_shaders = g_settings->getBool("enable_shaders");
195 m_surface_mode_scan_height =
196 g_settings->getBool("minimap_double_scan_height") ? 256 : 128;
198 // Initialize minimap data
199 data = new MinimapData;
200 data->mode = MINIMAP_MODE_OFF;
201 data->is_radar = false;
202 data->map_invalidated = true;
203 data->texture = NULL;
204 data->heightmap_texture = NULL;
205 data->minimap_shape_round = g_settings->getBool("minimap_shape_round");
207 // Get round minimap textures
208 data->minimap_mask_round = driver->createImage(
209 m_tsrc->getTexture("minimap_mask_round.png"),
210 core::position2d<s32>(0, 0),
211 core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
212 data->minimap_overlay_round = m_tsrc->getTexture("minimap_overlay_round.png");
214 // Get square minimap textures
215 data->minimap_mask_square = driver->createImage(
216 m_tsrc->getTexture("minimap_mask_square.png"),
217 core::position2d<s32>(0, 0),
218 core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
219 data->minimap_overlay_square = m_tsrc->getTexture("minimap_overlay_square.png");
221 // Create player marker texture
222 data->player_marker = m_tsrc->getTexture("player_marker.png");
223 // Create object marker texture
224 data->object_marker_red = m_tsrc->getTexture("object_marker_red.png");
226 // Create mesh buffer for minimap
227 m_meshbuffer = getMinimapMeshBuffer();
229 // Initialize and start thread
230 m_minimap_update_thread = new MinimapUpdateThread();
231 m_minimap_update_thread->data = data;
232 m_minimap_update_thread->start();
237 m_minimap_update_thread->stop();
238 m_minimap_update_thread->wait();
240 m_meshbuffer->drop();
242 data->minimap_mask_round->drop();
243 data->minimap_mask_square->drop();
245 driver->removeTexture(data->texture);
246 driver->removeTexture(data->heightmap_texture);
247 driver->removeTexture(data->minimap_overlay_round);
248 driver->removeTexture(data->minimap_overlay_square);
249 driver->removeTexture(data->object_marker_red);
252 delete m_minimap_update_thread;
255 void Minimap::addBlock(v3s16 pos, MinimapMapblock *data)
257 m_minimap_update_thread->enqueueBlock(pos, data);
260 void Minimap::toggleMinimapShape()
262 MutexAutoLock lock(m_mutex);
264 data->minimap_shape_round = !data->minimap_shape_round;
265 g_settings->setBool("minimap_shape_round", data->minimap_shape_round);
266 m_minimap_update_thread->deferUpdate();
269 void Minimap::setMinimapShape(MinimapShape shape)
271 MutexAutoLock lock(m_mutex);
273 if (shape == MINIMAP_SHAPE_SQUARE)
274 data->minimap_shape_round = false;
275 else if (shape == MINIMAP_SHAPE_ROUND)
276 data->minimap_shape_round = true;
278 g_settings->setBool("minimap_shape_round", data->minimap_shape_round);
279 m_minimap_update_thread->deferUpdate();
282 MinimapShape Minimap::getMinimapShape()
284 if (data->minimap_shape_round) {
285 return MINIMAP_SHAPE_ROUND;
288 return MINIMAP_SHAPE_SQUARE;
291 void Minimap::setMinimapMode(MinimapMode mode)
293 static const MinimapModeDef modedefs[MINIMAP_MODE_COUNT] = {{false, 0, 0},
294 {false, m_surface_mode_scan_height, 256},
295 {false, m_surface_mode_scan_height, 128},
296 {false, m_surface_mode_scan_height, 64}, {true, 32, 128},
297 {true, 32, 64}, {true, 32, 32}};
299 if (mode >= MINIMAP_MODE_COUNT)
302 MutexAutoLock lock(m_mutex);
304 data->is_radar = modedefs[mode].is_radar;
305 data->scan_height = modedefs[mode].scan_height;
306 data->map_size = modedefs[mode].map_size;
309 m_minimap_update_thread->deferUpdate();
312 void Minimap::setPos(v3s16 pos)
314 bool do_update = false;
317 MutexAutoLock lock(m_mutex);
319 if (pos != data->old_pos) {
320 data->old_pos = data->pos;
327 m_minimap_update_thread->deferUpdate();
330 void Minimap::setAngle(f32 angle)
335 void Minimap::blitMinimapPixelsToImageRadar(video::IImage *map_image)
337 video::SColor c(240, 0, 0, 0);
338 for (s16 x = 0; x < data->map_size; x++)
339 for (s16 z = 0; z < data->map_size; z++) {
340 MinimapPixel *mmpixel =
341 &data->minimap_scan[x + z * data->map_size];
343 if (mmpixel->air_count > 0)
344 c.setGreen(core::clamp(
346 mmpixel->air_count * 8),
351 map_image->setPixel(x, data->map_size - z - 1, c);
355 void Minimap::blitMinimapPixelsToImageSurface(
356 video::IImage *map_image, video::IImage *heightmap_image)
358 // This variable creation/destruction has a 1% cost on rendering minimap
359 video::SColor tilecolor;
360 for (s16 x = 0; x < data->map_size; x++)
361 for (s16 z = 0; z < data->map_size; z++) {
362 MinimapPixel *mmpixel =
363 &data->minimap_scan[x + z * data->map_size];
365 const ContentFeatures &f = m_ndef->get(mmpixel->n);
366 const TileDef *tile = &f.tiledef[0];
368 // Color of the 0th tile (mostly this is the topmost)
370 tilecolor = tile->color;
372 mmpixel->n.getColor(f, &tilecolor);
374 tilecolor.setRed(tilecolor.getRed() * f.minimap_color.getRed() /
376 tilecolor.setGreen(tilecolor.getGreen() *
377 f.minimap_color.getGreen() / 255);
378 tilecolor.setBlue(tilecolor.getBlue() *
379 f.minimap_color.getBlue() / 255);
380 tilecolor.setAlpha(240);
382 map_image->setPixel(x, data->map_size - z - 1, tilecolor);
384 u32 h = mmpixel->height;
385 heightmap_image->setPixel(x, data->map_size - z - 1,
386 video::SColor(255, h, h, h));
390 video::ITexture *Minimap::getMinimapTexture()
392 // update minimap textures when new scan is ready
393 if (data->map_invalidated)
394 return data->texture;
396 // create minimap and heightmap images in memory
397 core::dimension2d<u32> dim(data->map_size, data->map_size);
398 video::IImage *map_image = driver->createImage(video::ECF_A8R8G8B8, dim);
399 video::IImage *heightmap_image = driver->createImage(video::ECF_A8R8G8B8, dim);
400 video::IImage *minimap_image = driver->createImage(video::ECF_A8R8G8B8,
401 core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
403 // Blit MinimapPixels to images
405 blitMinimapPixelsToImageRadar(map_image);
407 blitMinimapPixelsToImageSurface(map_image, heightmap_image);
409 map_image->copyToScaling(minimap_image);
412 video::IImage *minimap_mask = data->minimap_shape_round
413 ? data->minimap_mask_round
414 : data->minimap_mask_square;
417 for (s16 y = 0; y < MINIMAP_MAX_SY; y++)
418 for (s16 x = 0; x < MINIMAP_MAX_SX; x++) {
419 const video::SColor &mask_col =
420 minimap_mask->getPixel(x, y);
421 if (!mask_col.getAlpha())
422 minimap_image->setPixel(
423 x, y, video::SColor(0, 0, 0, 0));
428 driver->removeTexture(data->texture);
429 if (data->heightmap_texture)
430 driver->removeTexture(data->heightmap_texture);
432 data->texture = driver->addTexture("minimap__", minimap_image);
433 data->heightmap_texture =
434 driver->addTexture("minimap_heightmap__", heightmap_image);
435 minimap_image->drop();
436 heightmap_image->drop();
438 data->map_invalidated = true;
440 return data->texture;
443 v3f Minimap::getYawVec()
445 if (data->minimap_shape_round) {
446 return v3f(std::cos(m_angle * core::DEGTORAD),
447 std::sin(m_angle * core::DEGTORAD), 1.0);
450 return v3f(1.0, 0.0, 1.0);
453 scene::SMeshBuffer *Minimap::getMinimapMeshBuffer()
455 scene::SMeshBuffer *buf = new scene::SMeshBuffer();
456 buf->Vertices.set_used(4);
457 buf->Indices.set_used(6);
458 static const video::SColor c(255, 255, 255, 255);
460 buf->Vertices[0] = video::S3DVertex(-1, -1, 0, 0, 0, 1, c, 0, 1);
461 buf->Vertices[1] = video::S3DVertex(-1, 1, 0, 0, 0, 1, c, 0, 0);
462 buf->Vertices[2] = video::S3DVertex(1, 1, 0, 0, 0, 1, c, 1, 0);
463 buf->Vertices[3] = video::S3DVertex(1, -1, 0, 0, 0, 1, c, 1, 1);
475 void Minimap::drawMinimap()
477 video::ITexture *minimap_texture = getMinimapTexture();
478 if (!minimap_texture)
481 updateActiveMarkers();
482 v2u32 screensize = RenderingEngine::get_instance()->getWindowSize();
483 const u32 size = 0.25 * screensize.Y;
485 core::rect<s32> oldViewPort = driver->getViewPort();
486 core::matrix4 oldProjMat = driver->getTransform(video::ETS_PROJECTION);
487 core::matrix4 oldViewMat = driver->getTransform(video::ETS_VIEW);
489 driver->setViewPort(core::rect<s32>(
490 screensize.X - size - 10, 10, screensize.X - 10, size + 10));
491 driver->setTransform(video::ETS_PROJECTION, core::matrix4());
492 driver->setTransform(video::ETS_VIEW, core::matrix4());
494 core::matrix4 matrix;
495 matrix.makeIdentity();
497 video::SMaterial &material = m_meshbuffer->getMaterial();
498 material.setFlag(video::EMF_TRILINEAR_FILTER, true);
499 material.Lighting = false;
500 material.TextureLayer[0].Texture = minimap_texture;
501 material.TextureLayer[1].Texture = data->heightmap_texture;
503 if (m_enable_shaders && !data->is_radar) {
504 u16 sid = m_shdrsrc->getShader("minimap_shader", 1, 1);
505 material.MaterialType = m_shdrsrc->getShaderInfo(sid).material;
507 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
510 if (data->minimap_shape_round)
511 matrix.setRotationDegrees(core::vector3df(0, 0, 360 - m_angle));
514 driver->setTransform(video::ETS_WORLD, matrix);
515 driver->setMaterial(material);
516 driver->drawMeshBuffer(m_meshbuffer);
519 video::ITexture *minimap_overlay = data->minimap_shape_round
520 ? data->minimap_overlay_round
521 : data->minimap_overlay_square;
522 material.TextureLayer[0].Texture = minimap_overlay;
523 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
524 driver->setMaterial(material);
525 driver->drawMeshBuffer(m_meshbuffer);
527 // Draw player marker on minimap
528 if (data->minimap_shape_round) {
529 matrix.setRotationDegrees(core::vector3df(0, 0, 0));
531 matrix.setRotationDegrees(core::vector3df(0, 0, m_angle));
534 material.TextureLayer[0].Texture = data->player_marker;
535 driver->setTransform(video::ETS_WORLD, matrix);
536 driver->setMaterial(material);
537 driver->drawMeshBuffer(m_meshbuffer);
539 // Reset transformations
540 driver->setTransform(video::ETS_VIEW, oldViewMat);
541 driver->setTransform(video::ETS_PROJECTION, oldProjMat);
542 driver->setViewPort(oldViewPort);
544 // Draw player markers
545 v2s32 s_pos(screensize.X - size - 10, 10);
546 core::dimension2di imgsize(data->object_marker_red->getOriginalSize());
547 core::rect<s32> img_rect(0, 0, imgsize.Width, imgsize.Height);
548 static const video::SColor col(255, 255, 255, 255);
549 static const video::SColor c[4] = {col, col, col, col};
550 f32 sin_angle = std::sin(m_angle * core::DEGTORAD);
551 f32 cos_angle = std::cos(m_angle * core::DEGTORAD);
552 s32 marker_size2 = 0.025 * (float)size;
553 for (std::list<v2f>::const_iterator i = m_active_markers.begin();
554 i != m_active_markers.end(); ++i) {
556 if (data->minimap_shape_round) {
557 f32 t1 = posf.X * cos_angle - posf.Y * sin_angle;
558 f32 t2 = posf.X * sin_angle + posf.Y * cos_angle;
562 posf.X = (posf.X + 0.5) * (float)size;
563 posf.Y = (posf.Y + 0.5) * (float)size;
564 core::rect<s32> dest_rect(s_pos.X + posf.X - marker_size2,
565 s_pos.Y + posf.Y - marker_size2,
566 s_pos.X + posf.X + marker_size2,
567 s_pos.Y + posf.Y + marker_size2);
568 driver->draw2DImage(data->object_marker_red, dest_rect, img_rect,
569 &dest_rect, &c[0], true);
573 void Minimap::updateActiveMarkers()
575 video::IImage *minimap_mask = data->minimap_shape_round
576 ? data->minimap_mask_round
577 : data->minimap_mask_square;
579 const std::list<Nametag *> &nametags = client->getCamera()->getNametags();
581 m_active_markers.clear();
583 for (Nametag *nametag : nametags) {
584 v3s16 pos = floatToInt(
585 nametag->parent_node->getAbsolutePosition() +
586 intToFloat(client->getCamera()->getOffset(),
589 pos -= data->pos - v3s16(data->map_size / 2, data->scan_height / 2,
591 if (pos.X < 0 || pos.X > data->map_size || pos.Y < 0 ||
592 pos.Y > data->scan_height || pos.Z < 0 ||
593 pos.Z > data->map_size) {
596 pos.X = ((float)pos.X / data->map_size) * MINIMAP_MAX_SX;
597 pos.Z = ((float)pos.Z / data->map_size) * MINIMAP_MAX_SY;
598 const video::SColor &mask_col = minimap_mask->getPixel(pos.X, pos.Z);
599 if (!mask_col.getAlpha()) {
603 m_active_markers.emplace_back(
604 ((float)pos.X / (float)MINIMAP_MAX_SX) - 0.5,
605 (1.0 - (float)pos.Z / (float)MINIMAP_MAX_SY) - 0.5);
613 void MinimapMapblock::getMinimapNodes(VoxelManipulator *vmanip, const v3s16 &pos)
616 for (s16 x = 0; x < MAP_BLOCKSIZE; x++)
617 for (s16 z = 0; z < MAP_BLOCKSIZE; z++) {
619 bool surface_found = false;
620 MinimapPixel *mmpixel = &data[z * MAP_BLOCKSIZE + x];
622 for (s16 y = MAP_BLOCKSIZE - 1; y >= 0; y--) {
624 MapNode n = vmanip->getNodeNoEx(pos + p);
625 if (!surface_found && n.getContent() != CONTENT_AIR) {
628 surface_found = true;
629 } else if (n.getContent() == CONTENT_AIR) {
635 mmpixel->n = MapNode(CONTENT_AIR);
637 mmpixel->air_count = air_count;