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 "logoutputbuffer.h"
22 #include "jthread/jmutexautolock.h"
23 #include "jthread/jsemaphore.h"
24 #include "clientmap.h"
28 #include "util/numeric.h"
29 #include "util/string.h"
32 QueuedMinimapUpdate::QueuedMinimapUpdate():
33 pos(-1337,-1337,-1337),
38 QueuedMinimapUpdate::~QueuedMinimapUpdate()
43 MinimapUpdateQueue::MinimapUpdateQueue()
47 MinimapUpdateQueue::~MinimapUpdateQueue()
49 JMutexAutoLock lock(m_mutex);
51 for (std::list<QueuedMinimapUpdate*>::iterator
53 i != m_queue.end(); ++i) {
54 QueuedMinimapUpdate *q = *i;
59 bool MinimapUpdateQueue::addBlock(v3s16 pos, MinimapMapblock *data)
61 DSTACK(__FUNCTION_NAME);
63 JMutexAutoLock lock(m_mutex);
66 Find if block is already in queue.
67 If it is, update the data and quit.
69 for (std::list<QueuedMinimapUpdate*>::iterator
71 i != m_queue.end(); ++i) {
72 QueuedMinimapUpdate *q = *i;
83 QueuedMinimapUpdate *q = new QueuedMinimapUpdate;
90 QueuedMinimapUpdate * MinimapUpdateQueue::pop()
92 JMutexAutoLock lock(m_mutex);
94 for (std::list<QueuedMinimapUpdate*>::iterator
96 i != m_queue.end(); i++) {
97 QueuedMinimapUpdate *q = *i;
105 Minimap update thread
108 void MinimapUpdateThread::Stop()
116 void MinimapUpdateThread::enqueue_Block(v3s16 pos, MinimapMapblock *data)
118 if (m_queue.addBlock(pos, data))
119 // we had to allocate a new block
123 void MinimapUpdateThread::forceUpdate()
128 void *MinimapUpdateThread::Thread()
132 log_register_thread("MinimapUpdateThread");
134 DSTACK(__FUNCTION_NAME);
136 BEGIN_DEBUG_EXCEPTION_HANDLER
138 porting::setThreadName("MinimapUpdateThread");
140 while (!StopRequested()) {
143 if (StopRequested()) break;
145 while (m_queue.size()) {
146 QueuedMinimapUpdate *q = m_queue.pop();
149 std::map<v3s16, MinimapMapblock *>::iterator it;
150 it = m_blocks_cache.find(q->pos);
152 m_blocks_cache[q->pos] = q->data;
153 } else if (it != m_blocks_cache.end()) {
155 m_blocks_cache.erase(it);
159 if (data->map_invalidated) {
160 if (data->mode != MINIMAP_MODE_OFF) {
161 getMap(data->pos, data->map_size, data->scan_height, data->radar);
162 data->map_invalidated = false;
166 END_DEBUG_EXCEPTION_HANDLER(errorstream)
171 MinimapUpdateThread::~MinimapUpdateThread()
173 for (std::map<v3s16, MinimapMapblock *>::iterator
174 it = m_blocks_cache.begin();
175 it != m_blocks_cache.end(); it++) {
180 MinimapPixel *MinimapUpdateThread::getMinimapPixel (v3s16 pos, s16 height, s16 &pixel_height)
182 pixel_height = height - MAP_BLOCKSIZE;
183 v3s16 blockpos_max, blockpos_min, relpos;
184 getNodeBlockPosWithOffset(v3s16(pos.X, pos.Y - height / 2, pos.Z), blockpos_min, relpos);
185 getNodeBlockPosWithOffset(v3s16(pos.X, pos.Y + height / 2, pos.Z), blockpos_max, relpos);
186 std::map<v3s16, MinimapMapblock *>::iterator it;
187 for (s16 i = blockpos_max.Y; i > blockpos_min.Y - 1; i--) {
188 it = m_blocks_cache.find(v3s16(blockpos_max.X, i, blockpos_max.Z));
189 if (it != m_blocks_cache.end()) {
190 MinimapPixel *pixel = &it->second->data[relpos.X + relpos.Z * MAP_BLOCKSIZE];
191 if (pixel->id != CONTENT_AIR) {
192 pixel_height += pixel->height;
196 pixel_height -= MAP_BLOCKSIZE;
201 s16 MinimapUpdateThread::getAirCount (v3s16 pos, s16 height)
204 v3s16 blockpos_max, blockpos_min, relpos;
205 getNodeBlockPosWithOffset(v3s16(pos.X, pos.Y - height / 2, pos.Z), blockpos_min, relpos);
206 getNodeBlockPosWithOffset(v3s16(pos.X, pos.Y + height / 2, pos.Z), blockpos_max, relpos);
207 std::map<v3s16, MinimapMapblock *>::iterator it;
208 for (s16 i = blockpos_max.Y; i > blockpos_min.Y - 1; i--) {
209 it = m_blocks_cache.find(v3s16(blockpos_max.X, i, blockpos_max.Z));
210 if (it != m_blocks_cache.end()) {
211 MinimapPixel *pixel = &it->second->data[relpos.X + relpos.Z * MAP_BLOCKSIZE];
212 air_count += pixel->air_count;
218 void MinimapUpdateThread::getMap (v3s16 pos, s16 size, s16 height, bool radar)
220 v3s16 p = v3s16 (pos.X - size / 2, pos.Y, pos.Z - size / 2);
222 for (s16 x = 0; x < size; x++) {
223 for (s16 z = 0; z < size; z++){
224 u16 id = CONTENT_AIR;
225 MinimapPixel* minimap_pixel = &data->minimap_scan[x + z * size];
227 s16 pixel_height = 0;
228 MinimapPixel* cached_pixel =
229 getMinimapPixel(v3s16(p.X + x, p.Y, p.Z + z), height, pixel_height);
231 id = cached_pixel->id;
232 minimap_pixel->height = pixel_height;
235 minimap_pixel->air_count = getAirCount (v3s16(p.X + x, p.Y, p.Z + z), height);
237 minimap_pixel->id = id;
242 Mapper::Mapper(IrrlichtDevice *device, Client *client)
244 this->device = device;
245 this->client = client;
246 this->driver = device->getVideoDriver();
247 this->tsrc = client->getTextureSource();
248 this->player = client->getEnv().getLocalPlayer();
249 this->shdrsrc = client->getShaderSource();
251 m_enable_shaders = g_settings->getBool("enable_shaders");
252 m_enable_shaders = g_settings->getBool("enable_shaders");
253 if (g_settings->getBool("minimap_double_scan_height")) {
254 m_surface_mode_scan_height = 256;
256 m_surface_mode_scan_height = 128;
258 data = new MinimapData;
259 data->mode = MINIMAP_MODE_OFF;
261 data->map_invalidated = true;
262 data->heightmap_image = NULL;
263 data->minimap_image = NULL;
264 data->texture = NULL;
265 data->minimap_shape_round = g_settings->getBool("minimap_shape_round");
266 std::string fname1 = "minimap_mask_round.png";
267 std::string fname2 = "minimap_overlay_round.png";
268 data->minimap_mask_round = driver->createImage (tsrc->getTexture(fname1),
269 core::position2d<s32>(0,0), core::dimension2d<u32>(512,512));
270 data->minimap_overlay_round = tsrc->getTexture(fname2);
271 fname1 = "minimap_mask_square.png";
272 fname2 = "minimap_overlay_square.png";
273 data->minimap_mask_square = driver->createImage (tsrc->getTexture(fname1),
274 core::position2d<s32>(0,0), core::dimension2d<u32>(512,512));
275 data->minimap_overlay_square = tsrc->getTexture(fname2);
276 data->player_marker = tsrc->getTexture("player_marker.png");
277 m_meshbuffer = getMinimapMeshBuffer();
278 m_minimap_update_thread = new MinimapUpdateThread(device, client);
279 m_minimap_update_thread->data = data;
280 m_minimap_update_thread->Start();
285 m_minimap_update_thread->Stop();
286 m_minimap_update_thread->Wait();
287 m_meshbuffer->drop();
288 data->minimap_mask_round->drop();
289 data->minimap_mask_square->drop();
290 driver->removeTexture(data->texture);
291 driver->removeTexture(data->heightmap_texture);
292 driver->removeTexture(data->minimap_overlay_round);
293 driver->removeTexture(data->minimap_overlay_square);
295 delete m_minimap_update_thread;
298 void Mapper::addBlock (v3s16 pos, MinimapMapblock *data)
300 m_minimap_update_thread->enqueue_Block(pos, data);
303 MinimapMode Mapper::getMinimapMode()
308 void Mapper::toggleMinimapShape()
310 data->minimap_shape_round = !data->minimap_shape_round;
311 g_settings->setBool(("minimap_shape_round"), data->minimap_shape_round);
314 void Mapper::setMinimapMode(MinimapMode mode)
316 static const u16 modeDefs[7 * 3] = {
318 0, m_surface_mode_scan_height, 256,
319 0, m_surface_mode_scan_height, 128,
320 0, m_surface_mode_scan_height, 64,
325 JMutexAutoLock lock(m_mutex);
326 data->radar = (bool)modeDefs[(int)mode * 3];
327 data->scan_height = modeDefs[(int)mode * 3 + 1];
328 data->map_size = modeDefs[(int)mode * 3 + 2];
330 m_minimap_update_thread->forceUpdate();
333 void Mapper::setPos(v3s16 pos)
335 JMutexAutoLock lock(m_mutex);
336 if (pos != data->old_pos) {
337 data->old_pos = data->pos;
339 m_minimap_update_thread->forceUpdate();
343 video::ITexture *Mapper::getMinimapTexture()
345 // update minimap textures when new scan is ready
346 if (!data->map_invalidated) {
347 // create minimap and heightmap image
348 core::dimension2d<u32> dim(data->map_size,data->map_size);
349 video::IImage *map_image = driver->createImage(video::ECF_A8R8G8B8, dim);
350 video::IImage *heightmap_image = driver->createImage(video::ECF_A8R8G8B8, dim);
351 video::IImage *minimap_image = driver->createImage(video::ECF_A8R8G8B8,
352 core::dimension2d<u32>(512, 512));
357 for (s16 x = 0; x < data->map_size; x++) {
358 for (s16 z = 0; z < data->map_size; z++) {
359 MinimapPixel* minimap_pixel = &data->minimap_scan[x + z * data->map_size];
360 const ContentFeatures &f = client->getNodeDefManager()->get(minimap_pixel->id);
363 map_image->setPixel(x, data->map_size - z -1, c);
364 u32 h = minimap_pixel->height;
365 heightmap_image->setPixel(x,data->map_size -z -1,
366 video::SColor(255, h, h, h));
371 c = video::SColor (240, 0 , 0, 0);
372 for (s16 x = 0; x < data->map_size; x++) {
373 for (s16 z = 0; z < data->map_size; z++) {
374 MinimapPixel* minimap_pixel = &data->minimap_scan[x + z * data->map_size];
375 if (minimap_pixel->air_count > 0) {
376 c.setGreen(core::clamp(core::round32(32 + minimap_pixel->air_count * 8), 0, 255));
380 map_image->setPixel(x, data->map_size - z -1, c);
385 map_image->copyToScaling(minimap_image);
388 video::IImage *minimap_mask;
389 if (data->minimap_shape_round) {
390 minimap_mask = data->minimap_mask_round;
392 minimap_mask = data->minimap_mask_square;
394 for (s16 x = 0; x < 512; x++) {
395 for (s16 y = 0; y < 512; y++) {
396 video::SColor mask_col = minimap_mask->getPixel(x, y);
397 if (!mask_col.getAlpha()) {
398 minimap_image->setPixel(x, y, video::SColor(0,0,0,0));
404 driver->removeTexture(data->texture);
406 if (data->heightmap_texture) {
407 driver->removeTexture(data->heightmap_texture);
409 data->texture = driver->addTexture("minimap__", minimap_image);
410 data->heightmap_texture = driver->addTexture("minimap_heightmap__", heightmap_image);
411 minimap_image->drop();
412 heightmap_image->drop();
414 data->map_invalidated = true;
416 return data->texture;
419 v3f Mapper::getYawVec()
421 if (data->minimap_shape_round) {
422 return v3f(cos(player->getYaw()* core::DEGTORAD),
423 sin(player->getYaw()* core::DEGTORAD), 1.0);
425 return v3f(1.0, 0.0, 1.0);
429 scene::SMeshBuffer *Mapper::getMinimapMeshBuffer()
431 scene::SMeshBuffer *buf = new scene::SMeshBuffer();
432 buf->Vertices.set_used(4);
433 buf->Indices .set_used(6);
434 video::SColor c(255, 255, 255, 255);
436 buf->Vertices[0] = video::S3DVertex(-1, -1, 0, 0, 0, 1, c, 0, 1);
437 buf->Vertices[1] = video::S3DVertex(-1, 1, 0, 0, 0, 1, c, 0, 0);
438 buf->Vertices[2] = video::S3DVertex( 1, 1, 0, 0, 0, 1, c, 1, 0);
439 buf->Vertices[3] = video::S3DVertex( 1, -1, 0, 0, 0, 1, c, 1, 1);
451 void Mapper::drawMinimap()
453 v2u32 screensize = porting::getWindowSize();
454 u32 size = 0.25 * screensize.Y;
455 video::ITexture* minimap_texture = getMinimapTexture();
456 core::matrix4 matrix;
458 core::rect<s32> oldViewPort = driver->getViewPort();
459 driver->setViewPort(core::rect<s32>(screensize.X - size - 10, 10,
460 screensize.X - 10, size + 10));
461 core::matrix4 oldProjMat = driver->getTransform(video::ETS_PROJECTION);
462 driver->setTransform(video::ETS_PROJECTION, core::matrix4());
463 core::matrix4 oldViewMat = driver->getTransform(video::ETS_VIEW);
464 driver->setTransform(video::ETS_VIEW, core::matrix4());
465 matrix.makeIdentity();
467 if (minimap_texture) {
468 video::SMaterial& material = m_meshbuffer->getMaterial();
469 material.setFlag(video::EMF_TRILINEAR_FILTER, true);
470 material.Lighting = false;
471 material.TextureLayer[0].Texture = minimap_texture;
472 material.TextureLayer[1].Texture = data->heightmap_texture;
473 if (m_enable_shaders && !data->radar) {
474 u16 sid = shdrsrc->getShader("minimap_shader", 1, 1);
475 material.MaterialType = shdrsrc->getShaderInfo(sid).material;
477 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
480 if (data->minimap_shape_round)
481 matrix.setRotationDegrees(core::vector3df(0, 0, 360 - player->getYaw()));
482 driver->setTransform(video::ETS_WORLD, matrix);
483 driver->setMaterial(material);
484 driver->drawMeshBuffer(m_meshbuffer);
485 video::ITexture *minimap_overlay;
486 if (data->minimap_shape_round) {
487 minimap_overlay = data->minimap_overlay_round;
489 minimap_overlay = data->minimap_overlay_square;
491 material.TextureLayer[0].Texture = minimap_overlay;
492 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
493 driver->setMaterial(material);
494 driver->drawMeshBuffer(m_meshbuffer);
496 if (!data->minimap_shape_round) {
497 matrix.setRotationDegrees(core::vector3df(0, 0, player->getYaw()));
498 driver->setTransform(video::ETS_WORLD, matrix);
499 material.TextureLayer[0].Texture = data->player_marker;
500 driver->setMaterial(material);
501 driver->drawMeshBuffer(m_meshbuffer);
505 driver->setTransform(video::ETS_VIEW, oldViewMat);
506 driver->setTransform(video::ETS_PROJECTION, oldProjMat);
507 driver->setViewPort(oldViewPort);