8fe8b466d40a4cd985372d45d7717688b29618c1
[oweals/minetest.git] / src / minimap.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2015 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "minimap.h"
21 #include "logoutputbuffer.h"
22 #include "jthread/jmutexautolock.h"
23 #include "jthread/jsemaphore.h"
24 #include "clientmap.h"
25 #include "settings.h"
26 #include "nodedef.h"
27 #include "porting.h"
28 #include "util/numeric.h"
29 #include "util/string.h"
30 #include <math.h>
31
32 QueuedMinimapUpdate::QueuedMinimapUpdate():
33         pos(-1337,-1337,-1337),
34         data(NULL)
35 {
36 }
37
38 QueuedMinimapUpdate::~QueuedMinimapUpdate()
39 {
40         delete data;
41 }
42
43 MinimapUpdateQueue::MinimapUpdateQueue()
44 {
45 }
46
47 MinimapUpdateQueue::~MinimapUpdateQueue()
48 {
49         JMutexAutoLock lock(m_mutex);
50
51         for (std::list<QueuedMinimapUpdate*>::iterator
52                         i = m_queue.begin();
53                         i != m_queue.end(); ++i) {
54                 QueuedMinimapUpdate *q = *i;
55                 delete q;
56         }
57 }
58
59 bool MinimapUpdateQueue::addBlock(v3s16 pos, MinimapMapblock *data)
60 {
61         DSTACK(__FUNCTION_NAME);
62
63         JMutexAutoLock lock(m_mutex);
64
65         /*
66                 Find if block is already in queue.
67                 If it is, update the data and quit.
68         */
69         for (std::list<QueuedMinimapUpdate*>::iterator
70                         i = m_queue.begin();
71                         i != m_queue.end(); ++i) {
72                 QueuedMinimapUpdate *q = *i;
73                 if (q->pos == pos) {
74                         delete q->data;
75                         q->data = data;
76                         return false;
77                 }
78         }
79
80         /*
81                 Add the block
82         */
83         QueuedMinimapUpdate *q = new QueuedMinimapUpdate;
84         q->pos = pos;
85         q->data = data;
86         m_queue.push_back(q);
87         return true;
88 }
89
90 QueuedMinimapUpdate * MinimapUpdateQueue::pop()
91 {
92         JMutexAutoLock lock(m_mutex);
93
94         for (std::list<QueuedMinimapUpdate*>::iterator
95                         i = m_queue.begin();
96                         i != m_queue.end(); i++) {
97                 QueuedMinimapUpdate *q = *i;
98                 m_queue.erase(i);
99                 return q;
100         }
101         return NULL;
102 }
103
104 /*
105         Minimap update thread
106 */
107
108 void MinimapUpdateThread::enqueue_Block(v3s16 pos, MinimapMapblock *data)
109 {
110         m_queue.addBlock(pos, data);
111         deferUpdate();
112 }
113
114 void MinimapUpdateThread::doUpdate()
115 {
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);
120                 if (q->data) {
121                         m_blocks_cache[q->pos] = q->data;
122                 } else if (it != m_blocks_cache.end()) {
123                         delete it->second;
124                         m_blocks_cache.erase(it);
125                 }
126         }
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;
131                 }
132         }
133 }
134
135 MinimapUpdateThread::~MinimapUpdateThread()
136 {
137         for (std::map<v3s16, MinimapMapblock *>::iterator
138                         it = m_blocks_cache.begin();
139                         it != m_blocks_cache.end(); it++) {
140                 delete it->second;
141         }
142 }
143
144 MinimapPixel *MinimapUpdateThread::getMinimapPixel(v3s16 pos, s16 height, s16 &pixel_height)
145 {
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;
157                                 return pixel;
158                         }
159                 }
160                 pixel_height -= MAP_BLOCKSIZE;
161         }
162         return NULL;
163 }
164
165 s16 MinimapUpdateThread::getAirCount(v3s16 pos, s16 height)
166 {
167         s16 air_count = 0;
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;
177                 }
178         }
179         return air_count;
180 }
181
182 void MinimapUpdateThread::getMap(v3s16 pos, s16 size, s16 height, bool radar)
183 {
184         v3s16 p = v3s16 (pos.X - size / 2, pos.Y, pos.Z - size / 2);
185
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];
190                         if (!radar) {
191                                 s16 pixel_height = 0;
192                                 MinimapPixel* cached_pixel =
193                                         getMinimapPixel(v3s16(p.X + x, p.Y, p.Z + z), height, pixel_height);
194                                 if (cached_pixel) {
195                                         id = cached_pixel->id;
196                                         minimap_pixel->height = pixel_height;
197                                 }
198                         } else {
199                                 minimap_pixel->air_count = getAirCount (v3s16(p.X + x, p.Y, p.Z + z), height);
200                         }
201                         minimap_pixel->id = id;
202                 }
203         }
204 }
205
206 Mapper::Mapper(IrrlichtDevice *device, Client *client)
207 {
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();
214
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;
219         } else {
220                 m_surface_mode_scan_height = 128;
221         }
222         data = new MinimapData;
223         data->mode = MINIMAP_MODE_OFF;
224         data->radar = false;
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();
245 }
246
247 Mapper::~Mapper()
248 {
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);
258         delete data;
259         delete m_minimap_update_thread;
260 }
261
262 void Mapper::addBlock (v3s16 pos, MinimapMapblock *data)
263 {
264         m_minimap_update_thread->enqueue_Block(pos, data);
265 }
266
267 MinimapMode Mapper::getMinimapMode()
268 {
269         return data->mode;
270 }
271
272 void Mapper::toggleMinimapShape()
273 {
274         data->minimap_shape_round = !data->minimap_shape_round;
275         g_settings->setBool(("minimap_shape_round"), data->minimap_shape_round);
276 }
277
278 void Mapper::setMinimapMode(MinimapMode mode)
279 {
280         static const u16 modeDefs[7 * 3] = {
281                 0, 0, 0,
282                 0, m_surface_mode_scan_height, 256,
283                 0, m_surface_mode_scan_height, 128,
284                 0, m_surface_mode_scan_height, 64,
285                 1, 32, 128,
286                 1, 32, 64,
287                 1, 32, 32};
288
289         JMutexAutoLock lock(m_mutex);
290         data->radar = (bool)modeDefs[(int)mode * 3];
291         data->scan_height = modeDefs[(int)mode * 3 + 1];
292         data->map_size = modeDefs[(int)mode * 3 + 2];
293         data->mode = mode;
294         m_minimap_update_thread->deferUpdate();
295 }
296
297 void Mapper::setPos(v3s16 pos)
298 {
299         JMutexAutoLock lock(m_mutex);
300         if (pos != data->old_pos) {
301                 data->old_pos = data->pos;
302                 data->pos = pos;
303                 m_minimap_update_thread->deferUpdate();
304         }
305 }
306
307 video::ITexture *Mapper::getMinimapTexture()
308 {
309         // update minimap textures when new scan is ready
310         if (!data->map_invalidated) {
311                 // create minimap and heightmap image
312                 core::dimension2d<u32> dim(data->map_size,data->map_size);
313                 video::IImage *map_image = driver->createImage(video::ECF_A8R8G8B8, dim);
314                 video::IImage *heightmap_image = driver->createImage(video::ECF_A8R8G8B8, dim);
315                 video::IImage *minimap_image = driver->createImage(video::ECF_A8R8G8B8,
316                         core::dimension2d<u32>(512, 512));
317
318                 video::SColor c;
319                 if (!data->radar) {
320                         // surface mode
321                         for (s16 x = 0; x < data->map_size; x++) {
322                                 for (s16 z = 0; z < data->map_size; z++) {
323                                         MinimapPixel* minimap_pixel = &data->minimap_scan[x + z * data->map_size];
324                                         const ContentFeatures &f = client->getNodeDefManager()->get(minimap_pixel->id);
325                                         c = f.minimap_color;
326                                         c.setAlpha(240);
327                                         map_image->setPixel(x, data->map_size - z -1, c);
328                                         u32 h = minimap_pixel->height;
329                                         heightmap_image->setPixel(x,data->map_size -z -1,
330                                                 video::SColor(255, h, h, h));
331                                 }
332                         }
333                 } else {
334                         // radar mode
335                         c = video::SColor (240, 0 , 0, 0);
336                         for (s16 x = 0; x < data->map_size; x++) {
337                                 for (s16 z = 0; z < data->map_size; z++) {
338                                         MinimapPixel* minimap_pixel = &data->minimap_scan[x + z * data->map_size];
339                                         if (minimap_pixel->air_count > 0) {
340                                                 c.setGreen(core::clamp(core::round32(32 + minimap_pixel->air_count * 8), 0, 255));
341                                         } else {
342                                                 c.setGreen(0);
343                                         }
344                                         map_image->setPixel(x, data->map_size - z -1, c);
345                                 }
346                         }
347                 }
348
349                 map_image->copyToScaling(minimap_image);
350                 map_image->drop();
351
352                 video::IImage *minimap_mask;
353                 if (data->minimap_shape_round) {
354                         minimap_mask = data->minimap_mask_round;
355                 } else {
356                         minimap_mask = data->minimap_mask_square;
357                 }
358                 for (s16 x = 0; x < 512; x++) {
359                         for (s16 y = 0; y < 512; y++) {
360                                 video::SColor mask_col = minimap_mask->getPixel(x, y);
361                                 if (!mask_col.getAlpha()) {
362                                         minimap_image->setPixel(x, y, video::SColor(0,0,0,0));
363                                 }
364                         }
365                 }
366
367                 if (data->texture) {
368                         driver->removeTexture(data->texture);
369                 }
370                 if (data->heightmap_texture) {
371                         driver->removeTexture(data->heightmap_texture);
372                 }
373                 data->texture = driver->addTexture("minimap__", minimap_image);
374                 data->heightmap_texture = driver->addTexture("minimap_heightmap__", heightmap_image);
375                 minimap_image->drop();
376                 heightmap_image->drop();
377
378                 data->map_invalidated = true;
379         }
380         return data->texture;
381 }
382
383 v3f Mapper::getYawVec()
384 {
385         if (data->minimap_shape_round) {
386                 return v3f(cos(player->getYaw()* core::DEGTORAD),
387                         sin(player->getYaw()* core::DEGTORAD), 1.0);
388         } else {
389                 return v3f(1.0, 0.0, 1.0);
390         }
391 }
392
393 scene::SMeshBuffer *Mapper::getMinimapMeshBuffer()
394 {
395         scene::SMeshBuffer *buf = new scene::SMeshBuffer();
396         buf->Vertices.set_used(4);
397         buf->Indices .set_used(6);
398         video::SColor c(255, 255, 255, 255);
399
400         buf->Vertices[0] = video::S3DVertex(-1, -1, 0, 0, 0, 1, c, 0, 1);
401         buf->Vertices[1] = video::S3DVertex(-1,  1, 0, 0, 0, 1, c, 0, 0);
402         buf->Vertices[2] = video::S3DVertex( 1,  1, 0, 0, 0, 1, c, 1, 0);
403         buf->Vertices[3] = video::S3DVertex( 1, -1, 0, 0, 0, 1, c, 1, 1);
404
405         buf->Indices[0] = 0;
406         buf->Indices[1] = 1;
407         buf->Indices[2] = 2;
408         buf->Indices[3] = 2;
409         buf->Indices[4] = 3;
410         buf->Indices[5] = 0;
411
412         return buf;
413 }
414
415 void Mapper::drawMinimap()
416 {
417         v2u32 screensize = porting::getWindowSize();
418         u32 size = 0.25 * screensize.Y;
419         video::ITexture* minimap_texture = getMinimapTexture();
420         core::matrix4 matrix;
421
422         core::rect<s32> oldViewPort = driver->getViewPort();
423         driver->setViewPort(core::rect<s32>(screensize.X - size - 10, 10,
424                 screensize.X - 10, size + 10));
425         core::matrix4 oldProjMat = driver->getTransform(video::ETS_PROJECTION);
426         driver->setTransform(video::ETS_PROJECTION, core::matrix4());
427         core::matrix4 oldViewMat = driver->getTransform(video::ETS_VIEW);
428         driver->setTransform(video::ETS_VIEW, core::matrix4());
429         matrix.makeIdentity();
430
431         if (minimap_texture) {
432                 video::SMaterial& material = m_meshbuffer->getMaterial();
433                 material.setFlag(video::EMF_TRILINEAR_FILTER, true);
434                 material.Lighting = false;
435                 material.TextureLayer[0].Texture = minimap_texture;
436                 material.TextureLayer[1].Texture = data->heightmap_texture;
437                 if (m_enable_shaders && !data->radar) {
438                         u16 sid = shdrsrc->getShader("minimap_shader", 1, 1);
439                         material.MaterialType = shdrsrc->getShaderInfo(sid).material;
440                 } else {
441                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
442                 }
443
444                 if (data->minimap_shape_round)
445                         matrix.setRotationDegrees(core::vector3df(0, 0, 360 - player->getYaw()));
446                 driver->setTransform(video::ETS_WORLD, matrix);
447                 driver->setMaterial(material);
448                 driver->drawMeshBuffer(m_meshbuffer);
449                 video::ITexture *minimap_overlay;
450                 if (data->minimap_shape_round) {
451                         minimap_overlay = data->minimap_overlay_round;
452                 } else {
453                         minimap_overlay = data->minimap_overlay_square;
454                 }
455                 material.TextureLayer[0].Texture = minimap_overlay;
456                 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
457                 driver->setMaterial(material);
458                 driver->drawMeshBuffer(m_meshbuffer);
459
460                 if (!data->minimap_shape_round) {
461                         matrix.setRotationDegrees(core::vector3df(0, 0, player->getYaw()));
462                         driver->setTransform(video::ETS_WORLD, matrix);
463                         material.TextureLayer[0].Texture = data->player_marker;
464                         driver->setMaterial(material);
465                         driver->drawMeshBuffer(m_meshbuffer);
466                 }
467         }
468
469         driver->setTransform(video::ETS_VIEW, oldViewMat);
470         driver->setTransform(video::ETS_PROJECTION, oldProjMat);
471         driver->setViewPort(oldViewPort);
472 }