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::enqueue_Block(v3s16 pos, MinimapMapblock *data)
110 m_queue.addBlock(pos, data);
114 void MinimapUpdateThread::doUpdate()
116 while (m_queue.size()) {
117 QueuedMinimapUpdate *q = m_queue.pop();
118 std::map<v3s16, MinimapMapblock *>::iterator it;
119 it = m_blocks_cache.find(q->pos);
121 m_blocks_cache[q->pos] = q->data;
122 } else if (it != m_blocks_cache.end()) {
124 m_blocks_cache.erase(it);
127 if (data->map_invalidated) {
128 if (data->mode != MINIMAP_MODE_OFF) {
129 getMap(data->pos, data->map_size, data->scan_height, data->radar);
130 data->map_invalidated = false;
135 MinimapUpdateThread::~MinimapUpdateThread()
137 for (std::map<v3s16, MinimapMapblock *>::iterator
138 it = m_blocks_cache.begin();
139 it != m_blocks_cache.end(); it++) {
144 MinimapPixel *MinimapUpdateThread::getMinimapPixel(v3s16 pos, s16 height, s16 &pixel_height)
146 pixel_height = height - MAP_BLOCKSIZE;
147 v3s16 blockpos_max, blockpos_min, relpos;
148 getNodeBlockPosWithOffset(v3s16(pos.X, pos.Y - height / 2, pos.Z), blockpos_min, relpos);
149 getNodeBlockPosWithOffset(v3s16(pos.X, pos.Y + height / 2, pos.Z), blockpos_max, relpos);
150 std::map<v3s16, MinimapMapblock *>::iterator it;
151 for (s16 i = blockpos_max.Y; i > blockpos_min.Y - 1; i--) {
152 it = m_blocks_cache.find(v3s16(blockpos_max.X, i, blockpos_max.Z));
153 if (it != m_blocks_cache.end()) {
154 MinimapPixel *pixel = &it->second->data[relpos.X + relpos.Z * MAP_BLOCKSIZE];
155 if (pixel->id != CONTENT_AIR) {
156 pixel_height += pixel->height;
160 pixel_height -= MAP_BLOCKSIZE;
165 s16 MinimapUpdateThread::getAirCount(v3s16 pos, s16 height)
168 v3s16 blockpos_max, blockpos_min, relpos;
169 getNodeBlockPosWithOffset(v3s16(pos.X, pos.Y - height / 2, pos.Z), blockpos_min, relpos);
170 getNodeBlockPosWithOffset(v3s16(pos.X, pos.Y + height / 2, pos.Z), blockpos_max, relpos);
171 std::map<v3s16, MinimapMapblock *>::iterator it;
172 for (s16 i = blockpos_max.Y; i > blockpos_min.Y - 1; i--) {
173 it = m_blocks_cache.find(v3s16(blockpos_max.X, i, blockpos_max.Z));
174 if (it != m_blocks_cache.end()) {
175 MinimapPixel *pixel = &it->second->data[relpos.X + relpos.Z * MAP_BLOCKSIZE];
176 air_count += pixel->air_count;
182 void MinimapUpdateThread::getMap(v3s16 pos, s16 size, s16 height, bool radar)
184 v3s16 p = v3s16 (pos.X - size / 2, pos.Y, pos.Z - size / 2);
186 for (s16 x = 0; x < size; x++) {
187 for (s16 z = 0; z < size; z++){
188 u16 id = CONTENT_AIR;
189 MinimapPixel* minimap_pixel = &data->minimap_scan[x + z * size];
191 s16 pixel_height = 0;
192 MinimapPixel* cached_pixel =
193 getMinimapPixel(v3s16(p.X + x, p.Y, p.Z + z), height, pixel_height);
195 id = cached_pixel->id;
196 minimap_pixel->height = pixel_height;
199 minimap_pixel->air_count = getAirCount (v3s16(p.X + x, p.Y, p.Z + z), height);
201 minimap_pixel->id = id;
206 Mapper::Mapper(IrrlichtDevice *device, Client *client)
208 this->device = device;
209 this->client = client;
210 this->driver = device->getVideoDriver();
211 this->tsrc = client->getTextureSource();
212 this->player = client->getEnv().getLocalPlayer();
213 this->shdrsrc = client->getShaderSource();
215 m_enable_shaders = g_settings->getBool("enable_shaders");
216 m_enable_shaders = g_settings->getBool("enable_shaders");
217 if (g_settings->getBool("minimap_double_scan_height")) {
218 m_surface_mode_scan_height = 256;
220 m_surface_mode_scan_height = 128;
222 data = new MinimapData;
223 data->mode = MINIMAP_MODE_OFF;
225 data->map_invalidated = true;
226 data->heightmap_image = NULL;
227 data->minimap_image = NULL;
228 data->texture = NULL;
229 data->minimap_shape_round = g_settings->getBool("minimap_shape_round");
230 std::string fname1 = "minimap_mask_round.png";
231 std::string fname2 = "minimap_overlay_round.png";
232 data->minimap_mask_round = driver->createImage (tsrc->getTexture(fname1),
233 core::position2d<s32>(0,0), core::dimension2d<u32>(512,512));
234 data->minimap_overlay_round = tsrc->getTexture(fname2);
235 fname1 = "minimap_mask_square.png";
236 fname2 = "minimap_overlay_square.png";
237 data->minimap_mask_square = driver->createImage (tsrc->getTexture(fname1),
238 core::position2d<s32>(0,0), core::dimension2d<u32>(512,512));
239 data->minimap_overlay_square = tsrc->getTexture(fname2);
240 data->player_marker = tsrc->getTexture("player_marker.png");
241 m_meshbuffer = getMinimapMeshBuffer();
242 m_minimap_update_thread = new MinimapUpdateThread(device, client);
243 m_minimap_update_thread->data = data;
244 m_minimap_update_thread->Start();
249 m_minimap_update_thread->Stop();
250 m_minimap_update_thread->Wait();
251 m_meshbuffer->drop();
252 data->minimap_mask_round->drop();
253 data->minimap_mask_square->drop();
254 driver->removeTexture(data->texture);
255 driver->removeTexture(data->heightmap_texture);
256 driver->removeTexture(data->minimap_overlay_round);
257 driver->removeTexture(data->minimap_overlay_square);
259 delete m_minimap_update_thread;
262 void Mapper::addBlock (v3s16 pos, MinimapMapblock *data)
264 m_minimap_update_thread->enqueue_Block(pos, data);
267 MinimapMode Mapper::getMinimapMode()
272 void Mapper::toggleMinimapShape()
274 data->minimap_shape_round = !data->minimap_shape_round;
275 g_settings->setBool(("minimap_shape_round"), data->minimap_shape_round);
276 m_minimap_update_thread->deferUpdate();
279 void Mapper::setMinimapMode(MinimapMode mode)
281 static const u16 modeDefs[7 * 3] = {
283 0, m_surface_mode_scan_height, 256,
284 0, m_surface_mode_scan_height, 128,
285 0, m_surface_mode_scan_height, 64,
290 JMutexAutoLock lock(m_mutex);
291 data->radar = (bool)modeDefs[(int)mode * 3];
292 data->scan_height = modeDefs[(int)mode * 3 + 1];
293 data->map_size = modeDefs[(int)mode * 3 + 2];
295 m_minimap_update_thread->deferUpdate();
298 void Mapper::setPos(v3s16 pos)
300 JMutexAutoLock lock(m_mutex);
301 if (pos != data->old_pos) {
302 data->old_pos = data->pos;
304 m_minimap_update_thread->deferUpdate();
308 video::ITexture *Mapper::getMinimapTexture()
310 // update minimap textures when new scan is ready
311 if (!data->map_invalidated) {
312 // create minimap and heightmap image
313 core::dimension2d<u32> dim(data->map_size,data->map_size);
314 video::IImage *map_image = driver->createImage(video::ECF_A8R8G8B8, dim);
315 video::IImage *heightmap_image = driver->createImage(video::ECF_A8R8G8B8, dim);
316 video::IImage *minimap_image = driver->createImage(video::ECF_A8R8G8B8,
317 core::dimension2d<u32>(512, 512));
322 for (s16 x = 0; x < data->map_size; x++) {
323 for (s16 z = 0; z < data->map_size; z++) {
324 MinimapPixel* minimap_pixel = &data->minimap_scan[x + z * data->map_size];
325 const ContentFeatures &f = client->getNodeDefManager()->get(minimap_pixel->id);
328 map_image->setPixel(x, data->map_size - z -1, c);
329 u32 h = minimap_pixel->height;
330 heightmap_image->setPixel(x,data->map_size -z -1,
331 video::SColor(255, h, h, h));
336 c = video::SColor (240, 0 , 0, 0);
337 for (s16 x = 0; x < data->map_size; x++) {
338 for (s16 z = 0; z < data->map_size; z++) {
339 MinimapPixel* minimap_pixel = &data->minimap_scan[x + z * data->map_size];
340 if (minimap_pixel->air_count > 0) {
341 c.setGreen(core::clamp(core::round32(32 + minimap_pixel->air_count * 8), 0, 255));
345 map_image->setPixel(x, data->map_size - z -1, c);
350 map_image->copyToScaling(minimap_image);
353 video::IImage *minimap_mask;
354 if (data->minimap_shape_round) {
355 minimap_mask = data->minimap_mask_round;
357 minimap_mask = data->minimap_mask_square;
359 for (s16 x = 0; x < 512; x++) {
360 for (s16 y = 0; y < 512; y++) {
361 video::SColor mask_col = minimap_mask->getPixel(x, y);
362 if (!mask_col.getAlpha()) {
363 minimap_image->setPixel(x, y, video::SColor(0,0,0,0));
369 driver->removeTexture(data->texture);
371 if (data->heightmap_texture) {
372 driver->removeTexture(data->heightmap_texture);
374 data->texture = driver->addTexture("minimap__", minimap_image);
375 data->heightmap_texture = driver->addTexture("minimap_heightmap__", heightmap_image);
376 minimap_image->drop();
377 heightmap_image->drop();
379 data->map_invalidated = true;
381 return data->texture;
384 v3f Mapper::getYawVec()
386 if (data->minimap_shape_round) {
387 return v3f(cos(player->getYaw()* core::DEGTORAD),
388 sin(player->getYaw()* core::DEGTORAD), 1.0);
390 return v3f(1.0, 0.0, 1.0);
394 scene::SMeshBuffer *Mapper::getMinimapMeshBuffer()
396 scene::SMeshBuffer *buf = new scene::SMeshBuffer();
397 buf->Vertices.set_used(4);
398 buf->Indices .set_used(6);
399 video::SColor c(255, 255, 255, 255);
401 buf->Vertices[0] = video::S3DVertex(-1, -1, 0, 0, 0, 1, c, 0, 1);
402 buf->Vertices[1] = video::S3DVertex(-1, 1, 0, 0, 0, 1, c, 0, 0);
403 buf->Vertices[2] = video::S3DVertex( 1, 1, 0, 0, 0, 1, c, 1, 0);
404 buf->Vertices[3] = video::S3DVertex( 1, -1, 0, 0, 0, 1, c, 1, 1);
416 void Mapper::drawMinimap()
418 v2u32 screensize = porting::getWindowSize();
419 u32 size = 0.25 * screensize.Y;
420 video::ITexture* minimap_texture = getMinimapTexture();
421 core::matrix4 matrix;
423 core::rect<s32> oldViewPort = driver->getViewPort();
424 driver->setViewPort(core::rect<s32>(screensize.X - size - 10, 10,
425 screensize.X - 10, size + 10));
426 core::matrix4 oldProjMat = driver->getTransform(video::ETS_PROJECTION);
427 driver->setTransform(video::ETS_PROJECTION, core::matrix4());
428 core::matrix4 oldViewMat = driver->getTransform(video::ETS_VIEW);
429 driver->setTransform(video::ETS_VIEW, core::matrix4());
430 matrix.makeIdentity();
432 if (minimap_texture) {
433 video::SMaterial& material = m_meshbuffer->getMaterial();
434 material.setFlag(video::EMF_TRILINEAR_FILTER, true);
435 material.Lighting = false;
436 material.TextureLayer[0].Texture = minimap_texture;
437 material.TextureLayer[1].Texture = data->heightmap_texture;
438 if (m_enable_shaders && !data->radar) {
439 u16 sid = shdrsrc->getShader("minimap_shader", 1, 1);
440 material.MaterialType = shdrsrc->getShaderInfo(sid).material;
442 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
445 if (data->minimap_shape_round)
446 matrix.setRotationDegrees(core::vector3df(0, 0, 360 - player->getYaw()));
447 driver->setTransform(video::ETS_WORLD, matrix);
448 driver->setMaterial(material);
449 driver->drawMeshBuffer(m_meshbuffer);
450 video::ITexture *minimap_overlay;
451 if (data->minimap_shape_round) {
452 minimap_overlay = data->minimap_overlay_round;
454 minimap_overlay = data->minimap_overlay_square;
456 material.TextureLayer[0].Texture = minimap_overlay;
457 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
458 driver->setMaterial(material);
459 driver->drawMeshBuffer(m_meshbuffer);
461 if (!data->minimap_shape_round) {
462 matrix.setRotationDegrees(core::vector3df(0, 0, player->getYaw()));
463 driver->setTransform(video::ETS_WORLD, matrix);
464 material.TextureLayer[0].Texture = data->player_marker;
465 driver->setMaterial(material);
466 driver->drawMeshBuffer(m_meshbuffer);
470 driver->setTransform(video::ETS_VIEW, oldViewMat);
471 driver->setTransform(video::ETS_PROJECTION, oldProjMat);
472 driver->setViewPort(oldViewPort);