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.
21 #include "threading/mutex_auto_lock.h"
22 #include "threading/semaphore.h"
23 #include "clientmap.h"
27 #include "util/numeric.h"
28 #include "util/string.h"
31 #include "client/renderingengine.h"
35 //// MinimapUpdateThread
38 MinimapUpdateThread::~MinimapUpdateThread()
40 for (std::map<v3s16, MinimapMapblock *>::iterator
41 it = m_blocks_cache.begin();
42 it != m_blocks_cache.end(); ++it) {
46 for (std::deque<QueuedMinimapUpdate>::iterator
47 it = m_update_queue.begin();
48 it != m_update_queue.end(); ++it) {
49 QueuedMinimapUpdate &q = *it;
54 bool MinimapUpdateThread::pushBlockUpdate(v3s16 pos, MinimapMapblock *data)
56 MutexAutoLock lock(m_queue_mutex);
58 // Find if block is already in queue.
59 // If it is, update the data and quit.
60 for (std::deque<QueuedMinimapUpdate>::iterator
61 it = m_update_queue.begin();
62 it != m_update_queue.end(); ++it) {
63 QueuedMinimapUpdate &q = *it;
72 QueuedMinimapUpdate q;
75 m_update_queue.push_back(q);
80 bool MinimapUpdateThread::popBlockUpdate(QueuedMinimapUpdate *update)
82 MutexAutoLock lock(m_queue_mutex);
84 if (m_update_queue.empty())
87 *update = m_update_queue.front();
88 m_update_queue.pop_front();
93 void MinimapUpdateThread::enqueueBlock(v3s16 pos, MinimapMapblock *data)
95 pushBlockUpdate(pos, data);
100 void MinimapUpdateThread::doUpdate()
102 QueuedMinimapUpdate update;
104 while (popBlockUpdate(&update)) {
106 // Swap two values in the map using single lookup
107 std::pair<std::map<v3s16, MinimapMapblock*>::iterator, bool>
108 result = m_blocks_cache.insert(std::make_pair(update.pos, update.data));
109 if (!result.second) {
110 delete result.first->second;
111 result.first->second = update.data;
114 std::map<v3s16, MinimapMapblock *>::iterator it;
115 it = m_blocks_cache.find(update.pos);
116 if (it != m_blocks_cache.end()) {
118 m_blocks_cache.erase(it);
123 if (data->map_invalidated && data->mode != MINIMAP_MODE_OFF) {
124 getMap(data->pos, data->map_size, data->scan_height);
125 data->map_invalidated = false;
129 void MinimapUpdateThread::getMap(v3s16 pos, s16 size, s16 height)
131 v3s16 region(size, 0, size);
132 v3s16 pos_min(pos.X - size / 2, pos.Y - height / 2, pos.Z - size / 2);
133 v3s16 pos_max(pos_min.X + size - 1, pos.Y + height / 2, pos_min.Z + size - 1);
134 v3s16 blockpos_min = getNodeBlockPos(pos_min);
135 v3s16 blockpos_max = getNodeBlockPos(pos_max);
138 for (int z = 0; z < size; z++)
139 for (int x = 0; x < size; x++) {
140 MinimapPixel &mmpixel = data->minimap_scan[x + z * size];
141 mmpixel.air_count = 0;
143 mmpixel.n = MapNode(CONTENT_AIR);
148 for (blockpos.Z = blockpos_min.Z; blockpos.Z <= blockpos_max.Z; ++blockpos.Z)
149 for (blockpos.Y = blockpos_min.Y; blockpos.Y <= blockpos_max.Y; ++blockpos.Y)
150 for (blockpos.X = blockpos_min.X; blockpos.X <= blockpos_max.X; ++blockpos.X) {
151 std::map<v3s16, MinimapMapblock *>::const_iterator pblock =
152 m_blocks_cache.find(blockpos);
153 if (pblock == m_blocks_cache.end())
155 const MinimapMapblock &block = *pblock->second;
157 v3s16 block_node_min(blockpos * MAP_BLOCKSIZE);
158 v3s16 block_node_max(block_node_min + MAP_BLOCKSIZE - 1);
160 v3s16 range_min = componentwise_max(block_node_min, pos_min);
161 v3s16 range_max = componentwise_min(block_node_max, pos_max);
165 for (pos.Z = range_min.Z; pos.Z <= range_max.Z; ++pos.Z)
166 for (pos.X = range_min.X; pos.X <= range_max.X; ++pos.X) {
167 v3s16 inblock_pos = pos - block_node_min;
168 const MinimapPixel &in_pixel =
169 block.data[inblock_pos.Z * MAP_BLOCKSIZE + inblock_pos.X];
171 v3s16 inmap_pos = pos - pos_min;
172 MinimapPixel &out_pixel =
173 data->minimap_scan[inmap_pos.X + inmap_pos.Z * size];
175 out_pixel.air_count += in_pixel.air_count;
176 if (in_pixel.n.param0 != CONTENT_AIR) {
177 out_pixel.n = in_pixel.n;
178 out_pixel.height = inmap_pos.Y + in_pixel.height;
188 Minimap::Minimap(Client *client)
190 this->client = client;
191 this->driver = RenderingEngine::get_video_driver();
192 this->m_tsrc = client->getTextureSource();
193 this->m_shdrsrc = client->getShaderSource();
194 this->m_ndef = client->getNodeDefManager();
198 // Initialize static settings
199 m_enable_shaders = g_settings->getBool("enable_shaders");
200 m_surface_mode_scan_height =
201 g_settings->getBool("minimap_double_scan_height") ? 256 : 128;
203 // Initialize minimap data
204 data = new MinimapData;
205 data->mode = MINIMAP_MODE_OFF;
206 data->is_radar = false;
207 data->map_invalidated = true;
208 data->texture = NULL;
209 data->heightmap_texture = NULL;
210 data->minimap_shape_round = g_settings->getBool("minimap_shape_round");
212 // Get round minimap textures
213 data->minimap_mask_round = driver->createImage(
214 m_tsrc->getTexture("minimap_mask_round.png"),
215 core::position2d<s32>(0, 0),
216 core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
217 data->minimap_overlay_round = m_tsrc->getTexture("minimap_overlay_round.png");
219 // Get square minimap textures
220 data->minimap_mask_square = driver->createImage(
221 m_tsrc->getTexture("minimap_mask_square.png"),
222 core::position2d<s32>(0, 0),
223 core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
224 data->minimap_overlay_square = m_tsrc->getTexture("minimap_overlay_square.png");
226 // Create player marker texture
227 data->player_marker = m_tsrc->getTexture("player_marker.png");
228 // Create object marker texture
229 data->object_marker_red = m_tsrc->getTexture("object_marker_red.png");
231 // Create mesh buffer for minimap
232 m_meshbuffer = getMinimapMeshBuffer();
234 // Initialize and start thread
235 m_minimap_update_thread = new MinimapUpdateThread();
236 m_minimap_update_thread->data = data;
237 m_minimap_update_thread->start();
242 m_minimap_update_thread->stop();
243 m_minimap_update_thread->wait();
245 m_meshbuffer->drop();
247 data->minimap_mask_round->drop();
248 data->minimap_mask_square->drop();
250 driver->removeTexture(data->texture);
251 driver->removeTexture(data->heightmap_texture);
252 driver->removeTexture(data->minimap_overlay_round);
253 driver->removeTexture(data->minimap_overlay_square);
254 driver->removeTexture(data->object_marker_red);
257 delete m_minimap_update_thread;
260 void Minimap::addBlock(v3s16 pos, MinimapMapblock *data)
262 m_minimap_update_thread->enqueueBlock(pos, data);
265 void Minimap::toggleMinimapShape()
267 MutexAutoLock lock(m_mutex);
269 data->minimap_shape_round = !data->minimap_shape_round;
270 g_settings->setBool("minimap_shape_round", data->minimap_shape_round);
271 m_minimap_update_thread->deferUpdate();
274 void Minimap::setMinimapShape(MinimapShape shape)
276 MutexAutoLock lock(m_mutex);
278 if (shape == MINIMAP_SHAPE_SQUARE)
279 data->minimap_shape_round = false;
280 else if (shape == MINIMAP_SHAPE_ROUND)
281 data->minimap_shape_round = true;
283 g_settings->setBool("minimap_shape_round", data->minimap_shape_round);
284 m_minimap_update_thread->deferUpdate();
287 MinimapShape Minimap::getMinimapShape()
289 if (data->minimap_shape_round) {
290 return MINIMAP_SHAPE_ROUND;
292 return MINIMAP_SHAPE_SQUARE;
296 void Minimap::setMinimapMode(MinimapMode mode)
298 static const MinimapModeDef modedefs[MINIMAP_MODE_COUNT] = {
300 {false, m_surface_mode_scan_height, 256},
301 {false, m_surface_mode_scan_height, 128},
302 {false, m_surface_mode_scan_height, 64},
308 if (mode >= MINIMAP_MODE_COUNT)
311 MutexAutoLock lock(m_mutex);
313 data->is_radar = modedefs[mode].is_radar;
314 data->scan_height = modedefs[mode].scan_height;
315 data->map_size = modedefs[mode].map_size;
318 m_minimap_update_thread->deferUpdate();
321 void Minimap::setPos(v3s16 pos)
323 bool do_update = false;
326 MutexAutoLock lock(m_mutex);
328 if (pos != data->old_pos) {
329 data->old_pos = data->pos;
336 m_minimap_update_thread->deferUpdate();
339 void Minimap::setAngle(f32 angle)
344 void Minimap::blitMinimapPixelsToImageRadar(video::IImage *map_image)
346 video::SColor c(240, 0, 0, 0);
347 for (s16 x = 0; x < data->map_size; x++)
348 for (s16 z = 0; z < data->map_size; z++) {
349 MinimapPixel *mmpixel = &data->minimap_scan[x + z * data->map_size];
351 if (mmpixel->air_count > 0)
352 c.setGreen(core::clamp(core::round32(32 + mmpixel->air_count * 8), 0, 255));
356 map_image->setPixel(x, data->map_size - z - 1, c);
360 void Minimap::blitMinimapPixelsToImageSurface(
361 video::IImage *map_image, video::IImage *heightmap_image)
363 // This variable creation/destruction has a 1% cost on rendering minimap
364 video::SColor tilecolor;
365 for (s16 x = 0; x < data->map_size; x++)
366 for (s16 z = 0; z < data->map_size; z++) {
367 MinimapPixel *mmpixel = &data->minimap_scan[x + z * data->map_size];
369 const ContentFeatures &f = m_ndef->get(mmpixel->n);
370 const TileDef *tile = &f.tiledef[0];
372 // Color of the 0th tile (mostly this is the topmost)
374 tilecolor = tile->color;
376 mmpixel->n.getColor(f, &tilecolor);
378 tilecolor.setRed(tilecolor.getRed() * f.minimap_color.getRed() / 255);
379 tilecolor.setGreen(tilecolor.getGreen() * f.minimap_color.getGreen() / 255);
380 tilecolor.setBlue(tilecolor.getBlue() * f.minimap_color.getBlue() / 255);
381 tilecolor.setAlpha(240);
383 map_image->setPixel(x, data->map_size - z - 1, tilecolor);
385 u32 h = mmpixel->height;
386 heightmap_image->setPixel(x,data->map_size - z - 1,
387 video::SColor(255, h, h, h));
391 video::ITexture *Minimap::getMinimapTexture()
393 // update minimap textures when new scan is ready
394 if (data->map_invalidated)
395 return data->texture;
397 // create minimap and heightmap images in memory
398 core::dimension2d<u32> dim(data->map_size, data->map_size);
399 video::IImage *map_image = driver->createImage(video::ECF_A8R8G8B8, dim);
400 video::IImage *heightmap_image = driver->createImage(video::ECF_A8R8G8B8, dim);
401 video::IImage *minimap_image = driver->createImage(video::ECF_A8R8G8B8,
402 core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
404 // Blit MinimapPixels to images
406 blitMinimapPixelsToImageRadar(map_image);
408 blitMinimapPixelsToImageSurface(map_image, heightmap_image);
410 map_image->copyToScaling(minimap_image);
413 video::IImage *minimap_mask = data->minimap_shape_round ?
414 data->minimap_mask_round : 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 = minimap_mask->getPixel(x, y);
420 if (!mask_col.getAlpha())
421 minimap_image->setPixel(x, y, video::SColor(0,0,0,0));
426 driver->removeTexture(data->texture);
427 if (data->heightmap_texture)
428 driver->removeTexture(data->heightmap_texture);
430 data->texture = driver->addTexture("minimap__", minimap_image);
431 data->heightmap_texture =
432 driver->addTexture("minimap_heightmap__", heightmap_image);
433 minimap_image->drop();
434 heightmap_image->drop();
436 data->map_invalidated = true;
438 return data->texture;
441 v3f Minimap::getYawVec()
443 if (data->minimap_shape_round) {
445 cos(m_angle * core::DEGTORAD),
446 sin(m_angle * core::DEGTORAD),
449 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,
491 screensize.X - 10, size + 10));
492 driver->setTransform(video::ETS_PROJECTION, core::matrix4());
493 driver->setTransform(video::ETS_VIEW, core::matrix4());
495 core::matrix4 matrix;
496 matrix.makeIdentity();
498 video::SMaterial &material = m_meshbuffer->getMaterial();
499 material.setFlag(video::EMF_TRILINEAR_FILTER, true);
500 material.Lighting = false;
501 material.TextureLayer[0].Texture = minimap_texture;
502 material.TextureLayer[1].Texture = data->heightmap_texture;
504 if (m_enable_shaders && !data->is_radar) {
505 u16 sid = m_shdrsrc->getShader("minimap_shader", 1, 1);
506 material.MaterialType = m_shdrsrc->getShaderInfo(sid).material;
508 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
511 if (data->minimap_shape_round)
512 matrix.setRotationDegrees(core::vector3df(0, 0, 360 - m_angle));
515 driver->setTransform(video::ETS_WORLD, matrix);
516 driver->setMaterial(material);
517 driver->drawMeshBuffer(m_meshbuffer);
520 video::ITexture *minimap_overlay = data->minimap_shape_round ?
521 data->minimap_overlay_round : 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 // If round minimap, draw player marker
528 if (!data->minimap_shape_round) {
529 matrix.setRotationDegrees(core::vector3df(0, 0, m_angle));
530 material.TextureLayer[0].Texture = data->player_marker;
532 driver->setTransform(video::ETS_WORLD, matrix);
533 driver->setMaterial(material);
534 driver->drawMeshBuffer(m_meshbuffer);
537 // Reset transformations
538 driver->setTransform(video::ETS_VIEW, oldViewMat);
539 driver->setTransform(video::ETS_PROJECTION, oldProjMat);
540 driver->setViewPort(oldViewPort);
542 // Draw player markers
543 v2s32 s_pos(screensize.X - size - 10, 10);
544 core::dimension2di imgsize(data->object_marker_red->getOriginalSize());
545 core::rect<s32> img_rect(0, 0, imgsize.Width, imgsize.Height);
546 static const video::SColor col(255, 255, 255, 255);
547 static const video::SColor c[4] = {col, col, col, col};
548 f32 sin_angle = sin(m_angle * core::DEGTORAD);
549 f32 cos_angle = cos(m_angle * core::DEGTORAD);
550 s32 marker_size2 = 0.025 * (float)size;
551 for (std::list<v2f>::const_iterator
552 i = m_active_markers.begin();
553 i != m_active_markers.end(); ++i) {
555 if (data->minimap_shape_round) {
556 f32 t1 = posf.X * cos_angle - posf.Y * sin_angle;
557 f32 t2 = posf.X * sin_angle + posf.Y * cos_angle;
561 posf.X = (posf.X + 0.5) * (float)size;
562 posf.Y = (posf.Y + 0.5) * (float)size;
563 core::rect<s32> dest_rect(
564 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,
569 img_rect, &dest_rect, &c[0], true);
573 void Minimap::updateActiveMarkers()
575 video::IImage *minimap_mask = data->minimap_shape_round ?
576 data->minimap_mask_round : data->minimap_mask_square;
578 const std::list<Nametag *> &nametags = client->getCamera()->getNametags();
580 m_active_markers.clear();
582 for (std::list<Nametag *>::const_iterator i = nametags.begin();
583 i != nametags.end(); ++i) {
584 v3s16 pos = floatToInt((*i)->parent_node->getPosition() +
585 intToFloat(client->getCamera()->getOffset(), BS), BS);
586 pos -= data->pos - v3s16(data->map_size / 2,
587 data->scan_height / 2,
589 if (pos.X < 0 || pos.X > data->map_size ||
590 pos.Y < 0 || pos.Y > data->scan_height ||
591 pos.Z < 0 || pos.Z > data->map_size) {
594 pos.X = ((float)pos.X / data->map_size) * MINIMAP_MAX_SX;
595 pos.Z = ((float)pos.Z / data->map_size) * MINIMAP_MAX_SY;
596 const video::SColor &mask_col = minimap_mask->getPixel(pos.X, pos.Z);
597 if (!mask_col.getAlpha()) {
600 m_active_markers.push_back(v2f(((float)pos.X / (float)MINIMAP_MAX_SX) - 0.5,
601 (1.0 - (float)pos.Z / (float)MINIMAP_MAX_SY) - 0.5));
609 void MinimapMapblock::getMinimapNodes(VoxelManipulator *vmanip, v3s16 pos)
612 for (s16 x = 0; x < MAP_BLOCKSIZE; x++)
613 for (s16 z = 0; z < MAP_BLOCKSIZE; z++) {
615 bool surface_found = false;
616 MinimapPixel *mmpixel = &data[z * MAP_BLOCKSIZE + x];
618 for (s16 y = MAP_BLOCKSIZE -1; y >= 0; y--) {
620 MapNode n = vmanip->getNodeNoEx(pos + p);
621 if (!surface_found && n.getContent() != CONTENT_AIR) {
624 surface_found = true;
625 } else if (n.getContent() == CONTENT_AIR) {
631 mmpixel->n = MapNode(CONTENT_AIR);
633 mmpixel->air_count = air_count;