02660f969fe892e2f89ced604a52b39deb12a866
[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::Stop()
109 {
110         JThread::Stop();
111
112         // give us a nudge
113         m_queue_sem.Post();
114 }
115
116 void MinimapUpdateThread::enqueue_Block(v3s16 pos, MinimapMapblock *data)
117 {
118         if (m_queue.addBlock(pos, data))
119                 // we had to allocate a new block
120                 m_queue_sem.Post();
121 }
122
123 void MinimapUpdateThread::forceUpdate()
124 {
125         m_queue_sem.Post();
126 }
127
128 void *MinimapUpdateThread::Thread()
129 {
130         ThreadStarted();
131
132         log_register_thread("MinimapUpdateThread");
133
134         DSTACK(__FUNCTION_NAME);
135
136         BEGIN_DEBUG_EXCEPTION_HANDLER
137
138         porting::setThreadName("MinimapUpdateThread");
139
140         while (!StopRequested()) {
141
142                 m_queue_sem.Wait();
143                 if (StopRequested()) break;
144
145                 while (m_queue.size()) {
146                         QueuedMinimapUpdate *q = m_queue.pop();
147                         if (!q)
148                                 break;
149                         std::map<v3s16, MinimapMapblock *>::iterator it;
150                         it = m_blocks_cache.find(q->pos);
151                         if (q->data) {
152                                 m_blocks_cache[q->pos] = q->data;
153                         } else if (it != m_blocks_cache.end()) {
154                                 delete it->second;
155                                 m_blocks_cache.erase(it);
156                         }
157                 }
158
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;
163                         }
164                 }
165         }
166         END_DEBUG_EXCEPTION_HANDLER(errorstream)
167
168         return NULL;
169 }
170
171 MinimapUpdateThread::~MinimapUpdateThread()
172 {
173         for (std::map<v3s16, MinimapMapblock *>::iterator
174                         it = m_blocks_cache.begin();
175                         it != m_blocks_cache.end(); it++) {
176                 delete it->second;
177         }
178 }
179
180 MinimapPixel *MinimapUpdateThread::getMinimapPixel (v3s16 pos, s16 height, s16 &pixel_height)
181 {
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;
193                                 return pixel;
194                         }
195                 }
196                 pixel_height -= MAP_BLOCKSIZE;
197         }
198         return NULL;
199 }
200
201 s16 MinimapUpdateThread::getAirCount (v3s16 pos, s16 height)
202 {
203         s16 air_count = 0;
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;
213                 }
214         }
215         return air_count;
216 }
217
218 void MinimapUpdateThread::getMap (v3s16 pos, s16 size, s16 height, bool radar)
219 {
220         v3s16 p = v3s16 (pos.X - size / 2, pos.Y, pos.Z - size / 2);
221
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];
226                         if (!radar) {
227                                 s16 pixel_height = 0;
228                                 MinimapPixel* cached_pixel =
229                                         getMinimapPixel(v3s16(p.X + x, p.Y, p.Z + z), height, pixel_height);
230                                 if (cached_pixel) {
231                                         id = cached_pixel->id;
232                                         minimap_pixel->height = pixel_height;
233                                 }
234                         } else {
235                                 minimap_pixel->air_count = getAirCount (v3s16(p.X + x, p.Y, p.Z + z), height);
236                         }
237                         minimap_pixel->id = id;
238                 }
239         }
240 }
241
242 Mapper::Mapper(IrrlichtDevice *device, Client *client)
243 {
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();
250
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;
255         } else {
256                 m_surface_mode_scan_height = 128;
257         }
258         data = new MinimapData;
259         data->mode = MINIMAP_MODE_OFF;
260         data->radar = false;
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();
281 }
282
283 Mapper::~Mapper()
284 {
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);
294         delete data;
295         delete m_minimap_update_thread;
296 }
297
298 void Mapper::addBlock (v3s16 pos, MinimapMapblock *data)
299 {
300         m_minimap_update_thread->enqueue_Block(pos, data);
301 }
302
303 MinimapMode Mapper::getMinimapMode()
304 {
305         return data->mode;
306 }
307
308 void Mapper::toggleMinimapShape()
309 {
310         data->minimap_shape_round = !data->minimap_shape_round;
311         g_settings->setBool(("minimap_shape_round"), data->minimap_shape_round);
312 }
313
314 void Mapper::setMinimapMode(MinimapMode mode)
315 {
316         static const u16 modeDefs[7 * 3] = {
317                 0, 0, 0,
318                 0, m_surface_mode_scan_height, 256,
319                 0, m_surface_mode_scan_height, 128,
320                 0, m_surface_mode_scan_height, 64,
321                 1, 32, 128,
322                 1, 32, 64,
323                 1, 32, 32};
324
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];
329         data->mode = mode;
330         m_minimap_update_thread->forceUpdate();
331 }
332
333 void Mapper::setPos(v3s16 pos)
334 {
335         JMutexAutoLock lock(m_mutex);
336         if (pos != data->old_pos) {
337                 data->old_pos = data->pos;
338                 data->pos = pos;
339                 m_minimap_update_thread->forceUpdate();
340         }
341 }
342
343 video::ITexture *Mapper::getMinimapTexture()
344 {
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));
353
354                 video::SColor c;
355                 if (!data->radar) {
356                         // surface mode
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);
361                                         c = f.minimap_color;
362                                         c.setAlpha(240);
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));
367                                 }
368                         }
369                 } else {
370                         // radar mode
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));
377                                         } else {
378                                                 c.setGreen(0);
379                                         }
380                                         map_image->setPixel(x, data->map_size - z -1, c);
381                                 }
382                         }
383                 }
384
385                 map_image->copyToScaling(minimap_image);
386                 map_image->drop();
387
388                 video::IImage *minimap_mask;
389                 if (data->minimap_shape_round) {
390                         minimap_mask = data->minimap_mask_round;
391                 } else {
392                         minimap_mask = data->minimap_mask_square;
393                 }
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));
399                                 }
400                         }
401                 }
402
403                 if (data->texture) {
404                         driver->removeTexture(data->texture);
405                 }
406                 if (data->heightmap_texture) {
407                         driver->removeTexture(data->heightmap_texture);
408                 }
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();
413
414                 data->map_invalidated = true;
415         }
416         return data->texture;
417 }
418
419 v3f Mapper::getYawVec()
420 {
421         if (data->minimap_shape_round) {
422                 return v3f(cos(player->getYaw()* core::DEGTORAD),
423                         sin(player->getYaw()* core::DEGTORAD), 1.0);
424         } else {
425                 return v3f(1.0, 0.0, 1.0);
426         }
427 }
428
429 scene::SMeshBuffer *Mapper::getMinimapMeshBuffer()
430 {
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);
435
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);
440
441         buf->Indices[0] = 0;
442         buf->Indices[1] = 1;
443         buf->Indices[2] = 2;
444         buf->Indices[3] = 2;
445         buf->Indices[4] = 3;
446         buf->Indices[5] = 0;
447
448         return buf;
449 }
450
451 void Mapper::drawMinimap()
452 {
453         v2u32 screensize = porting::getWindowSize();
454         u32 size = 0.25 * screensize.Y;
455         video::ITexture* minimap_texture = getMinimapTexture();
456         core::matrix4 matrix;
457
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();
466
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;
476                 } else {
477                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
478                 }
479
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;
488                 } else {
489                         minimap_overlay = data->minimap_overlay_square;
490                 }
491                 material.TextureLayer[0].Texture = minimap_overlay;
492                 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
493                 driver->setMaterial(material);
494                 driver->drawMeshBuffer(m_meshbuffer);
495
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);
502                 }
503         }
504
505         driver->setTransform(video::ETS_VIEW, oldViewMat);
506         driver->setTransform(video::ETS_PROJECTION, oldProjMat);
507         driver->setViewPort(oldViewPort);
508 }