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"
31 //// MinimapUpdateThread
34 MinimapUpdateThread::~MinimapUpdateThread()
36 for (auto &it : m_blocks_cache) {
40 for (auto &q : m_update_queue) {
45 bool MinimapUpdateThread::pushBlockUpdate(v3s16 pos, MinimapMapblock *data)
47 MutexAutoLock lock(m_queue_mutex);
49 // Find if block is already in queue.
50 // If it is, update the data and quit.
51 for (QueuedMinimapUpdate &q : m_update_queue) {
60 QueuedMinimapUpdate q;
63 m_update_queue.push_back(q);
68 bool MinimapUpdateThread::popBlockUpdate(QueuedMinimapUpdate *update)
70 MutexAutoLock lock(m_queue_mutex);
72 if (m_update_queue.empty())
75 *update = m_update_queue.front();
76 m_update_queue.pop_front();
81 void MinimapUpdateThread::enqueueBlock(v3s16 pos, MinimapMapblock *data)
83 pushBlockUpdate(pos, data);
88 void MinimapUpdateThread::doUpdate()
90 QueuedMinimapUpdate update;
92 while (popBlockUpdate(&update)) {
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));
98 delete result.first->second;
99 result.first->second = update.data;
102 std::map<v3s16, MinimapMapblock *>::iterator it;
103 it = m_blocks_cache.find(update.pos);
104 if (it != m_blocks_cache.end()) {
106 m_blocks_cache.erase(it);
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;
120 void MinimapUpdateThread::getMap(v3s16 pos, s16 size, s16 height)
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);
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;
133 mmpixel.n = MapNode(CONTENT_AIR);
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())
145 const MinimapMapblock &block = *pblock->second;
147 v3s16 block_node_min(blockpos * MAP_BLOCKSIZE);
148 v3s16 block_node_max(block_node_min + MAP_BLOCKSIZE - 1);
150 v3s16 range_min = componentwise_max(block_node_min, pos_min);
151 v3s16 range_max = componentwise_min(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; ++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];
161 v3s16 inmap_pos = pos - pos_min;
162 MinimapPixel &out_pixel =
163 data->minimap_scan[inmap_pos.X + inmap_pos.Z * size];
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;
178 Minimap::Minimap(Client *client)
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();
187 m_current_mode_index = 0;
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;
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);
203 // Initialize minimap data
204 data = new MinimapData;
205 data->map_invalidated = true;
207 data->minimap_shape_round = g_settings->getBool("minimap_shape_round");
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");
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");
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");
230 // Create mesh buffer for minimap
231 m_meshbuffer = getMinimapMeshBuffer();
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();
241 m_minimap_update_thread->stop();
242 m_minimap_update_thread->wait();
244 m_meshbuffer->drop();
246 data->minimap_mask_round->drop();
247 data->minimap_mask_square->drop();
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);
255 for (MinimapMarker *m : m_markers)
260 delete m_minimap_update_thread;
263 void Minimap::addBlock(v3s16 pos, MinimapMapblock *data)
265 m_minimap_update_thread->enqueueBlock(pos, data);
268 void Minimap::toggleMinimapShape()
270 MutexAutoLock lock(m_mutex);
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();
277 void Minimap::setMinimapShape(MinimapShape shape)
279 MutexAutoLock lock(m_mutex);
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;
286 g_settings->setBool("minimap_shape_round", data->minimap_shape_round);
287 m_minimap_update_thread->deferUpdate();
290 MinimapShape Minimap::getMinimapShape()
292 if (data->minimap_shape_round) {
293 return MINIMAP_SHAPE_ROUND;
296 return MINIMAP_SHAPE_SQUARE;
299 void Minimap::setModeIndex(size_t index)
301 MutexAutoLock lock(m_mutex);
303 if (index < m_modes.size()) {
304 data->mode = m_modes[index];
305 m_current_mode_index = index;
307 data->mode = MinimapModeDef{MINIMAP_TYPE_OFF, gettext("Minimap hidden"), 0, 0, ""};
308 m_current_mode_index = 0;
311 data->map_invalidated = true;
313 if (m_minimap_update_thread)
314 m_minimap_update_thread->deferUpdate();
317 void Minimap::addMode(MinimapModeDef mode)
320 if (mode.type == MINIMAP_TYPE_TEXTURE) {
321 if (mode.texture.empty())
329 // Build a default standard label
330 if (mode.label == "") {
332 case MINIMAP_TYPE_OFF:
333 mode.label = gettext("Minimap hidden");
335 case MINIMAP_TYPE_SURFACE:
336 mode.label = gettext("Minimap in surface mode, Zoom x%d");
337 if (mode.map_size > 0)
338 zoom = 256 / mode.map_size;
340 case MINIMAP_TYPE_RADAR:
341 mode.label = gettext("Minimap in radar mode, Zoom x%d");
342 if (mode.map_size > 0)
343 zoom = 512 / mode.map_size;
345 case MINIMAP_TYPE_TEXTURE:
346 mode.label = gettext("Minimap in texture mode");
352 // else: Custom labels need mod-provided client-side translation
355 char label_buf[1024];
356 porting::mt_snprintf(label_buf, sizeof(label_buf),
357 mode.label.c_str(), zoom);
358 mode.label = label_buf;
361 m_modes.push_back(mode);
364 void Minimap::addMode(MinimapType type, u16 size, std::string label,
365 std::string texture, u16 scale)
370 mode.map_size = size;
371 mode.texture = texture;
374 case MINIMAP_TYPE_SURFACE:
375 mode.scan_height = m_surface_mode_scan_height;
377 case MINIMAP_TYPE_RADAR:
378 mode.scan_height = 32;
381 mode.scan_height = 0;
386 void Minimap::nextMode()
390 m_current_mode_index++;
391 if (m_current_mode_index >= m_modes.size())
392 m_current_mode_index = 0;
394 setModeIndex(m_current_mode_index);
397 void Minimap::setPos(v3s16 pos)
399 bool do_update = false;
402 MutexAutoLock lock(m_mutex);
404 if (pos != data->old_pos) {
405 data->old_pos = data->pos;
412 m_minimap_update_thread->deferUpdate();
415 void Minimap::setAngle(f32 angle)
420 void Minimap::blitMinimapPixelsToImageRadar(video::IImage *map_image)
422 video::SColor c(240, 0, 0, 0);
423 for (s16 x = 0; x < data->mode.map_size; x++)
424 for (s16 z = 0; z < data->mode.map_size; z++) {
425 MinimapPixel *mmpixel = &data->minimap_scan[x + z * data->mode.map_size];
427 if (mmpixel->air_count > 0)
428 c.setGreen(core::clamp(core::round32(32 + mmpixel->air_count * 8), 0, 255));
432 map_image->setPixel(x, data->mode.map_size - z - 1, c);
436 void Minimap::blitMinimapPixelsToImageSurface(
437 video::IImage *map_image, video::IImage *heightmap_image)
439 // This variable creation/destruction has a 1% cost on rendering minimap
440 video::SColor tilecolor;
441 for (s16 x = 0; x < data->mode.map_size; x++)
442 for (s16 z = 0; z < data->mode.map_size; z++) {
443 MinimapPixel *mmpixel = &data->minimap_scan[x + z * data->mode.map_size];
445 const ContentFeatures &f = m_ndef->get(mmpixel->n);
446 const TileDef *tile = &f.tiledef[0];
448 // Color of the 0th tile (mostly this is the topmost)
450 tilecolor = tile->color;
452 mmpixel->n.getColor(f, &tilecolor);
454 tilecolor.setRed(tilecolor.getRed() * f.minimap_color.getRed() / 255);
455 tilecolor.setGreen(tilecolor.getGreen() * f.minimap_color.getGreen() / 255);
456 tilecolor.setBlue(tilecolor.getBlue() * f.minimap_color.getBlue() / 255);
457 tilecolor.setAlpha(240);
459 map_image->setPixel(x, data->mode.map_size - z - 1, tilecolor);
461 u32 h = mmpixel->height;
462 heightmap_image->setPixel(x,data->mode.map_size - z - 1,
463 video::SColor(255, h, h, h));
467 video::ITexture *Minimap::getMinimapTexture()
469 // update minimap textures when new scan is ready
470 if (data->map_invalidated && data->mode.type != MINIMAP_TYPE_TEXTURE)
471 return data->texture;
473 // create minimap and heightmap images in memory
474 core::dimension2d<u32> dim(data->mode.map_size, data->mode.map_size);
475 video::IImage *map_image = driver->createImage(video::ECF_A8R8G8B8, dim);
476 video::IImage *heightmap_image = driver->createImage(video::ECF_A8R8G8B8, dim);
477 video::IImage *minimap_image = driver->createImage(video::ECF_A8R8G8B8,
478 core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
480 // Blit MinimapPixels to images
481 switch(data->mode.type) {
482 case MINIMAP_TYPE_OFF:
484 case MINIMAP_TYPE_SURFACE:
485 blitMinimapPixelsToImageSurface(map_image, heightmap_image);
487 case MINIMAP_TYPE_RADAR:
488 blitMinimapPixelsToImageRadar(map_image);
490 case MINIMAP_TYPE_TEXTURE:
491 // Want to use texture source, to : 1 find texture, 2 cache it
492 video::ITexture* texture = m_tsrc->getTexture(data->mode.texture);
493 video::IImage* image = driver->createImageFromData(
494 texture->getColorFormat(), texture->getSize(),
495 texture->lock(video::ETLM_READ_ONLY), true, false);
498 auto dim = image->getDimension();
500 map_image->fill(video::SColor(255, 0, 0, 0));
502 image->copyTo(map_image,
503 irr::core::vector2d<int> {
504 ((data->mode.map_size - (static_cast<int>(dim.Width))) >> 1)
505 - data->pos.X / data->mode.scale,
506 ((data->mode.map_size - (static_cast<int>(dim.Height))) >> 1)
507 + data->pos.Z / data->mode.scale
512 map_image->copyToScaling(minimap_image);
515 video::IImage *minimap_mask = data->minimap_shape_round ?
516 data->minimap_mask_round : data->minimap_mask_square;
519 for (s16 y = 0; y < MINIMAP_MAX_SY; y++)
520 for (s16 x = 0; x < MINIMAP_MAX_SX; x++) {
521 const video::SColor &mask_col = minimap_mask->getPixel(x, y);
522 if (!mask_col.getAlpha())
523 minimap_image->setPixel(x, y, video::SColor(0,0,0,0));
528 driver->removeTexture(data->texture);
529 if (data->heightmap_texture)
530 driver->removeTexture(data->heightmap_texture);
532 data->texture = driver->addTexture("minimap__", minimap_image);
533 data->heightmap_texture =
534 driver->addTexture("minimap_heightmap__", heightmap_image);
535 minimap_image->drop();
536 heightmap_image->drop();
538 data->map_invalidated = true;
540 return data->texture;
543 v3f Minimap::getYawVec()
545 if (data->minimap_shape_round) {
547 std::cos(m_angle * core::DEGTORAD),
548 std::sin(m_angle * core::DEGTORAD),
552 return v3f(1.0, 0.0, 1.0);
555 scene::SMeshBuffer *Minimap::getMinimapMeshBuffer()
557 scene::SMeshBuffer *buf = new scene::SMeshBuffer();
558 buf->Vertices.set_used(4);
559 buf->Indices.set_used(6);
560 static const video::SColor c(255, 255, 255, 255);
562 buf->Vertices[0] = video::S3DVertex(-1, -1, 0, 0, 0, 1, c, 0, 1);
563 buf->Vertices[1] = video::S3DVertex(-1, 1, 0, 0, 0, 1, c, 0, 0);
564 buf->Vertices[2] = video::S3DVertex( 1, 1, 0, 0, 0, 1, c, 1, 0);
565 buf->Vertices[3] = video::S3DVertex( 1, -1, 0, 0, 0, 1, c, 1, 1);
577 void Minimap::drawMinimap()
579 // Non hud managed minimap drawing (legacy minimap)
580 v2u32 screensize = RenderingEngine::getWindowSize();
581 const u32 size = 0.25 * screensize.Y;
583 drawMinimap(core::rect<s32>(
584 screensize.X - size - 10, 10,
585 screensize.X - 10, size + 10));
588 void Minimap::drawMinimap(core::rect<s32> rect) {
590 video::ITexture *minimap_texture = getMinimapTexture();
591 if (!minimap_texture)
594 if (data->mode.type == MINIMAP_TYPE_OFF)
597 updateActiveMarkers();
599 core::rect<s32> oldViewPort = driver->getViewPort();
600 core::matrix4 oldProjMat = driver->getTransform(video::ETS_PROJECTION);
601 core::matrix4 oldViewMat = driver->getTransform(video::ETS_VIEW);
603 driver->setViewPort(rect);
604 driver->setTransform(video::ETS_PROJECTION, core::matrix4());
605 driver->setTransform(video::ETS_VIEW, core::matrix4());
607 core::matrix4 matrix;
608 matrix.makeIdentity();
610 video::SMaterial &material = m_meshbuffer->getMaterial();
611 material.setFlag(video::EMF_TRILINEAR_FILTER, true);
612 material.Lighting = false;
613 material.TextureLayer[0].Texture = minimap_texture;
614 material.TextureLayer[1].Texture = data->heightmap_texture;
616 if (m_enable_shaders && data->mode.type == MINIMAP_TYPE_SURFACE) {
617 u16 sid = m_shdrsrc->getShader("minimap_shader", TILE_MATERIAL_ALPHA);
618 material.MaterialType = m_shdrsrc->getShaderInfo(sid).material;
620 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
623 if (data->minimap_shape_round)
624 matrix.setRotationDegrees(core::vector3df(0, 0, 360 - m_angle));
627 driver->setTransform(video::ETS_WORLD, matrix);
628 driver->setMaterial(material);
629 driver->drawMeshBuffer(m_meshbuffer);
632 video::ITexture *minimap_overlay = data->minimap_shape_round ?
633 data->minimap_overlay_round : data->minimap_overlay_square;
634 material.TextureLayer[0].Texture = minimap_overlay;
635 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
636 driver->setMaterial(material);
637 driver->drawMeshBuffer(m_meshbuffer);
639 // Draw player marker on minimap
640 if (data->minimap_shape_round) {
641 matrix.setRotationDegrees(core::vector3df(0, 0, 0));
643 matrix.setRotationDegrees(core::vector3df(0, 0, m_angle));
646 material.TextureLayer[0].Texture = data->player_marker;
647 driver->setTransform(video::ETS_WORLD, matrix);
648 driver->setMaterial(material);
649 driver->drawMeshBuffer(m_meshbuffer);
651 // Reset transformations
652 driver->setTransform(video::ETS_VIEW, oldViewMat);
653 driver->setTransform(video::ETS_PROJECTION, oldProjMat);
654 driver->setViewPort(oldViewPort);
656 // Draw player markers
657 v2s32 s_pos(rect.UpperLeftCorner.X, rect.UpperLeftCorner.Y);
658 core::dimension2di imgsize(data->object_marker_red->getOriginalSize());
659 core::rect<s32> img_rect(0, 0, imgsize.Width, imgsize.Height);
660 static const video::SColor col(255, 255, 255, 255);
661 static const video::SColor c[4] = {col, col, col, col};
662 f32 sin_angle = std::sin(m_angle * core::DEGTORAD);
663 f32 cos_angle = std::cos(m_angle * core::DEGTORAD);
664 s32 marker_size2 = 0.025 * (float)rect.getWidth();;
665 for (std::list<v2f>::const_iterator
666 i = m_active_markers.begin();
667 i != m_active_markers.end(); ++i) {
669 if (data->minimap_shape_round) {
670 f32 t1 = posf.X * cos_angle - posf.Y * sin_angle;
671 f32 t2 = posf.X * sin_angle + posf.Y * cos_angle;
675 posf.X = (posf.X + 0.5) * (float)rect.getWidth();
676 posf.Y = (posf.Y + 0.5) * (float)rect.getHeight();
677 core::rect<s32> dest_rect(
678 s_pos.X + posf.X - marker_size2,
679 s_pos.Y + posf.Y - marker_size2,
680 s_pos.X + posf.X + marker_size2,
681 s_pos.Y + posf.Y + marker_size2);
682 driver->draw2DImage(data->object_marker_red, dest_rect,
683 img_rect, &dest_rect, &c[0], true);
687 MinimapMarker* Minimap::addMarker(scene::ISceneNode *parent_node)
689 MinimapMarker *m = new MinimapMarker(parent_node);
690 m_markers.push_back(m);
694 void Minimap::removeMarker(MinimapMarker **m)
696 m_markers.remove(*m);
701 void Minimap::updateActiveMarkers()
703 video::IImage *minimap_mask = data->minimap_shape_round ?
704 data->minimap_mask_round : data->minimap_mask_square;
706 m_active_markers.clear();
707 v3f cam_offset = intToFloat(client->getCamera()->getOffset(), BS);
708 v3s16 pos_offset = data->pos - v3s16(data->mode.map_size / 2,
709 data->mode.scan_height / 2,
710 data->mode.map_size / 2);
712 for (MinimapMarker *marker : m_markers) {
713 v3s16 pos = floatToInt(marker->parent_node->getAbsolutePosition() +
714 cam_offset, BS) - pos_offset;
715 if (pos.X < 0 || pos.X > data->mode.map_size ||
716 pos.Y < 0 || pos.Y > data->mode.scan_height ||
717 pos.Z < 0 || pos.Z > data->mode.map_size) {
720 pos.X = ((float)pos.X / data->mode.map_size) * MINIMAP_MAX_SX;
721 pos.Z = ((float)pos.Z / data->mode.map_size) * MINIMAP_MAX_SY;
722 const video::SColor &mask_col = minimap_mask->getPixel(pos.X, pos.Z);
723 if (!mask_col.getAlpha()) {
727 m_active_markers.emplace_back(((float)pos.X / (float)MINIMAP_MAX_SX) - 0.5,
728 (1.0 - (float)pos.Z / (float)MINIMAP_MAX_SY) - 0.5);
736 void MinimapMapblock::getMinimapNodes(VoxelManipulator *vmanip, const v3s16 &pos)
739 for (s16 x = 0; x < MAP_BLOCKSIZE; x++)
740 for (s16 z = 0; z < MAP_BLOCKSIZE; z++) {
742 bool surface_found = false;
743 MinimapPixel *mmpixel = &data[z * MAP_BLOCKSIZE + x];
745 for (s16 y = MAP_BLOCKSIZE -1; y >= 0; y--) {
747 MapNode n = vmanip->getNodeNoEx(pos + p);
748 if (!surface_found && n.getContent() != CONTENT_AIR) {
751 surface_found = true;
752 } else if (n.getContent() == CONTENT_AIR) {
758 mmpixel->n = MapNode(CONTENT_AIR);
760 mmpixel->air_count = air_count;