Minimap update
[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
124 void *MinimapUpdateThread::Thread()
125 {
126         ThreadStarted();
127
128         log_register_thread("MinimapUpdateThread");
129
130         DSTACK(__FUNCTION_NAME);
131
132         BEGIN_DEBUG_EXCEPTION_HANDLER
133
134         porting::setThreadName("MinimapUpdateThread");
135
136         while (!StopRequested()) {
137
138                 m_queue_sem.Wait();
139                 if (StopRequested()) break;
140
141                 while (m_queue.size()) {
142                         QueuedMinimapUpdate *q = m_queue.pop();
143                         if (!q)
144                                 break;
145                         std::map<v3s16, MinimapMapblock *>::iterator it;
146                         it = m_blocks_cache.find(q->pos);
147                         if (q->data) {
148                                 m_blocks_cache[q->pos] = q->data;
149                         } else if (it != m_blocks_cache.end()) {
150                                 delete it->second;
151                                 m_blocks_cache.erase(it);
152                         }
153                 }
154
155                 if (data->map_invalidated) {
156                         if (data->mode != MINIMAP_MODE_OFF) {
157                                 getMap(data->pos, data->map_size, data->scan_height, data->radar);
158                                 data->map_invalidated = false;
159                         }
160                 }
161         }
162         END_DEBUG_EXCEPTION_HANDLER(errorstream)
163
164         return NULL;
165 }
166
167 MinimapUpdateThread::~MinimapUpdateThread()
168 {
169         for (std::map<v3s16, MinimapMapblock *>::iterator
170                         it = m_blocks_cache.begin();
171                         it != m_blocks_cache.end(); it++) {
172                 delete it->second;
173         }
174 }
175
176 MinimapPixel *MinimapUpdateThread::getMinimapPixel (v3s16 pos, s16 height, s16 &pixel_height)
177 {
178         pixel_height = height - MAP_BLOCKSIZE;
179         v3s16 blockpos_max, blockpos_min, relpos;
180         getNodeBlockPosWithOffset(v3s16(pos.X, pos.Y - height / 2, pos.Z), blockpos_min, relpos);
181         getNodeBlockPosWithOffset(v3s16(pos.X, pos.Y + height / 2, pos.Z), blockpos_max, relpos);
182         std::map<v3s16, MinimapMapblock *>::iterator it;
183         for (s16 i = blockpos_max.Y; i > blockpos_min.Y - 1; i--) {
184                 it = m_blocks_cache.find(v3s16(blockpos_max.X, i, blockpos_max.Z));
185                 if (it != m_blocks_cache.end()) {
186                         MinimapPixel *pixel = &it->second->data[relpos.X + relpos.Z * MAP_BLOCKSIZE];
187                         if (pixel->id != CONTENT_AIR) {
188                                 pixel_height += pixel->height;
189                                 return pixel;
190                         }
191                 }
192                 pixel_height -= MAP_BLOCKSIZE;
193         }
194         return NULL;
195 }
196
197 s16 MinimapUpdateThread::getAirCount (v3s16 pos, s16 height)
198 {
199         s16 air_count = 0;
200         v3s16 blockpos_max, blockpos_min, relpos;
201         getNodeBlockPosWithOffset(v3s16(pos.X, pos.Y - height / 2, pos.Z), blockpos_min, relpos);
202         getNodeBlockPosWithOffset(v3s16(pos.X, pos.Y + height / 2, pos.Z), blockpos_max, relpos);
203         std::map<v3s16, MinimapMapblock *>::iterator it;
204         for (s16 i = blockpos_max.Y; i > blockpos_min.Y - 1; i--) {
205                 it = m_blocks_cache.find(v3s16(blockpos_max.X, i, blockpos_max.Z));
206                 if (it != m_blocks_cache.end()) {
207                         MinimapPixel *pixel = &it->second->data[relpos.X + relpos.Z * MAP_BLOCKSIZE];
208                         air_count += pixel->air_count;
209                 }
210         }
211         return air_count;
212 }
213
214 void MinimapUpdateThread::getMap (v3s16 pos, s16 size, s16 height, bool radar)
215 {
216         v3s16 p = v3s16 (pos.X - size / 2, pos.Y, pos.Z - size / 2);
217
218         for (s16 x = 0; x < size; x++) {
219                 for (s16 z = 0; z < size; z++){
220                         u16 id = CONTENT_AIR;
221                         MinimapPixel* minimap_pixel = &data->minimap_scan[x + z * size];
222                         if (!radar) {
223                                 s16 pixel_height = 0;
224                                 MinimapPixel* cached_pixel =
225                                         getMinimapPixel(v3s16(p.X + x, p.Y, p.Z + z), height, pixel_height);
226                                 if (cached_pixel) {
227                                         id = cached_pixel->id;
228                                         minimap_pixel->height = pixel_height;
229                                 }
230                         } else {
231                                 minimap_pixel->air_count = getAirCount (v3s16(p.X + x, p.Y, p.Z + z), height);
232                         }
233                         minimap_pixel->id = id;
234                 }
235         }
236 }
237
238 Mapper::Mapper(IrrlichtDevice *device, Client *client)
239 {
240         this->device = device;
241         this->client = client;
242         this->driver = device->getVideoDriver();
243         this->tsrc = client->getTextureSource();
244         this->player = client->getEnv().getLocalPlayer();
245         this->shdrsrc = client->getShaderSource();
246
247         m_enable_shaders = g_settings->getBool("enable_shaders");
248         m_enable_shaders = g_settings->getBool("enable_shaders");
249         if (g_settings->getBool("minimap_double_scan_height")) {
250                 m_surface_mode_scan_height = 256;
251         } else {
252                 m_surface_mode_scan_height = 128;
253         }
254         data = new MinimapData;
255         data->mode = MINIMAP_MODE_OFF;
256         data->radar = false;
257         data->map_invalidated = true;
258         data->heightmap_image = NULL;
259         data->minimap_image = NULL;
260         data->texture = NULL;
261         data->minimap_shape_round = g_settings->getBool("minimap_shape_round");
262         std::string fname1 = "minimap_mask_round.png";
263         std::string fname2 = "minimap_overlay_round.png";
264         data->minimap_mask_round = driver->createImage (tsrc->getTexture(fname1),
265                         core::position2d<s32>(0,0), core::dimension2d<u32>(512,512));
266         data->minimap_overlay_round = tsrc->getTexture(fname2);
267         fname1 = "minimap_mask_square.png";
268         fname2 = "minimap_overlay_square.png";
269         data->minimap_mask_square = driver->createImage (tsrc->getTexture(fname1),
270                         core::position2d<s32>(0,0), core::dimension2d<u32>(512,512));
271         data->minimap_overlay_square = tsrc->getTexture(fname2);
272         data->player_marker = tsrc->getTexture("player_marker.png");
273         m_meshbuffer = getMinimapMeshBuffer();
274         m_minimap_update_thread = new MinimapUpdateThread(device, client);
275         m_minimap_update_thread->data = data;
276         m_minimap_update_thread->Start();
277 }
278
279 Mapper::~Mapper()
280 {
281         m_minimap_update_thread->Stop();
282         m_minimap_update_thread->Wait();
283         m_meshbuffer->drop();
284         data->minimap_mask_round->drop();
285         data->minimap_mask_square->drop();
286         driver->removeTexture(data->texture);
287         driver->removeTexture(data->heightmap_texture);
288         driver->removeTexture(data->minimap_overlay_round);
289         driver->removeTexture(data->minimap_overlay_square);
290         delete data;
291         delete m_minimap_update_thread;
292 }
293
294 void Mapper::addBlock (v3s16 pos, MinimapMapblock *data)
295 {
296         m_minimap_update_thread->enqueue_Block(pos, data);
297 }
298
299 MinimapMode Mapper::getMinimapMode()
300 {
301         return data->mode;
302 }
303
304 void Mapper::toggleMinimapShape()
305 {
306         data->minimap_shape_round = !data->minimap_shape_round;
307         g_settings->setBool(("minimap_shape_round"), data->minimap_shape_round);
308 }
309
310 void Mapper::setMinimapMode(MinimapMode mode)
311 {
312         static const u16 modeDefs[7 * 3] = {
313                 0, 0, 0,
314                 0, m_surface_mode_scan_height, 256,
315                 0, m_surface_mode_scan_height, 128,
316                 0, m_surface_mode_scan_height, 64,
317                 1, 32, 128,
318                 1, 32, 64,
319                 1, 32, 32};
320
321         JMutexAutoLock lock(m_mutex);
322         data->radar = (bool)modeDefs[(int)mode * 3];
323         data->scan_height = modeDefs[(int)mode * 3 + 1];
324         data->map_size = modeDefs[(int)mode * 3 + 2];
325         data->mode = mode;
326 }
327
328 void Mapper::setPos(v3s16 pos)
329 {
330         JMutexAutoLock lock(m_mutex);
331         data->pos = pos;
332 }
333
334 video::ITexture *Mapper::getMinimapTexture()
335 {
336         // update minimap textures when new scan is ready
337         if (!data->map_invalidated) {
338                 // create minimap and heightmap image
339                 core::dimension2d<u32> dim(data->map_size,data->map_size);
340                 video::IImage *map_image = driver->createImage(video::ECF_A8R8G8B8, dim);
341                 video::IImage *heightmap_image = driver->createImage(video::ECF_A8R8G8B8, dim);
342                 video::IImage *minimap_image = driver->createImage(video::ECF_A8R8G8B8,
343                         core::dimension2d<u32>(512, 512));
344
345                 video::SColor c;
346                 if (!data->radar) {
347                         // surface mode
348                         for (s16 x = 0; x < data->map_size; x++) {
349                                 for (s16 z = 0; z < data->map_size; z++) {
350                                         MinimapPixel* minimap_pixel = &data->minimap_scan[x + z * data->map_size];
351                                         const ContentFeatures &f = client->getNodeDefManager()->get(minimap_pixel->id);
352                                         c = f.minimap_color;
353                                         c.setAlpha(240);
354                                         map_image->setPixel(x, data->map_size - z -1, c);
355                                         u32 h = minimap_pixel->height;
356                                         heightmap_image->setPixel(x,data->map_size -z -1,
357                                                 video::SColor(255, h, h, h));
358                                 }
359                         }
360                 } else {
361                         // radar mode
362                         c = video::SColor (240, 0 , 0, 0);
363                         for (s16 x = 0; x < data->map_size; x++) {
364                                 for (s16 z = 0; z < data->map_size; z++) {
365                                         MinimapPixel* minimap_pixel = &data->minimap_scan[x + z * data->map_size];
366                                         if (minimap_pixel->air_count > 0) {
367                                                 c.setGreen(core::clamp(core::round32(32 + minimap_pixel->air_count * 8), 0, 255));
368                                         } else {
369                                                 c.setGreen(0);
370                                         }
371                                         map_image->setPixel(x, data->map_size - z -1, c);
372                                 }
373                         }
374                 }
375
376                 map_image->copyToScaling(minimap_image);
377                 map_image->drop();
378
379                 video::IImage *minimap_mask;
380                 if (data->minimap_shape_round) {
381                         minimap_mask = data->minimap_mask_round;
382                 } else {
383                         minimap_mask = data->minimap_mask_square;
384                 }
385                 for (s16 x = 0; x < 512; x++) {
386                         for (s16 y = 0; y < 512; y++) {
387                                 video::SColor mask_col = minimap_mask->getPixel(x, y);
388                                 if (!mask_col.getAlpha()) {
389                                         minimap_image->setPixel(x, y, video::SColor(0,0,0,0));
390                                 }
391                         }
392                 }
393
394                 if (data->texture) {
395                         driver->removeTexture(data->texture);
396                 }
397                 if (data->heightmap_texture) {
398                         driver->removeTexture(data->heightmap_texture);
399                 }
400                 data->texture = driver->addTexture("minimap__", minimap_image);
401                 data->heightmap_texture = driver->addTexture("minimap_heightmap__", heightmap_image);
402                 minimap_image->drop();
403                 heightmap_image->drop();
404
405                 data->map_invalidated = true;
406         }
407         return data->texture;
408 }
409
410 v3f Mapper::getYawVec()
411 {
412         if (data->minimap_shape_round) {
413                 return v3f(cos(player->getYaw()* core::DEGTORAD),
414                         sin(player->getYaw()* core::DEGTORAD), 1.0);
415         } else {
416                 return v3f(1.0, 0.0, 1.0);
417         }
418 }
419
420 scene::SMeshBuffer *Mapper::getMinimapMeshBuffer()
421 {
422         scene::SMeshBuffer *buf = new scene::SMeshBuffer();
423         buf->Vertices.set_used(4);
424         buf->Indices .set_used(6);
425         video::SColor c(255, 255, 255, 255);
426
427         buf->Vertices[0] = video::S3DVertex(-1, -1, 0, 0, 0, 1, c, 0, 1);
428         buf->Vertices[1] = video::S3DVertex(-1,  1, 0, 0, 0, 1, c, 0, 0);
429         buf->Vertices[2] = video::S3DVertex( 1,  1, 0, 0, 0, 1, c, 1, 0);
430         buf->Vertices[3] = video::S3DVertex( 1, -1, 0, 0, 0, 1, c, 1, 1);
431
432         buf->Indices[0] = 0;
433         buf->Indices[1] = 1;
434         buf->Indices[2] = 2;
435         buf->Indices[3] = 2;
436         buf->Indices[4] = 3;
437         buf->Indices[5] = 0;
438
439         return buf;
440 }
441
442 void Mapper::drawMinimap()
443 {
444         v2u32 screensize = porting::getWindowSize();
445         u32 size = 0.25 * screensize.Y;
446         video::ITexture* minimap_texture = getMinimapTexture();
447         core::matrix4 matrix;
448
449         core::rect<s32> oldViewPort = driver->getViewPort();
450         driver->setViewPort(core::rect<s32>(screensize.X - size - 10, 10,
451                 screensize.X - 10, size + 10));
452         core::matrix4 oldProjMat = driver->getTransform(video::ETS_PROJECTION);
453         driver->setTransform(video::ETS_PROJECTION, core::matrix4());
454         core::matrix4 oldViewMat = driver->getTransform(video::ETS_VIEW);
455         driver->setTransform(video::ETS_VIEW, core::matrix4());
456         matrix.makeIdentity();
457
458         if (minimap_texture) {
459                 video::SMaterial& material = m_meshbuffer->getMaterial();
460                 material.setFlag(video::EMF_TRILINEAR_FILTER, true);
461                 material.Lighting = false;
462                 material.TextureLayer[0].Texture = minimap_texture;
463                 material.TextureLayer[1].Texture = data->heightmap_texture;
464                 if (m_enable_shaders && !data->radar) {
465                         u16 sid = shdrsrc->getShader("minimap_shader", 1, 1);
466                         material.MaterialType = shdrsrc->getShaderInfo(sid).material;
467                 } else {
468                         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
469                 }
470
471                 if (data->minimap_shape_round)
472                         matrix.setRotationDegrees(core::vector3df(0, 0, 360 - player->getYaw()));
473                 driver->setTransform(video::ETS_WORLD, matrix);
474                 driver->setMaterial(material);
475                 driver->drawMeshBuffer(m_meshbuffer);
476                 video::ITexture *minimap_overlay;
477                 if (data->minimap_shape_round) {
478                         minimap_overlay = data->minimap_overlay_round;
479                 } else {
480                         minimap_overlay = data->minimap_overlay_square;
481                 }
482                 material.TextureLayer[0].Texture = minimap_overlay;
483                 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
484                 driver->setMaterial(material);
485                 driver->drawMeshBuffer(m_meshbuffer);
486
487                 if (!data->minimap_shape_round) {
488                         matrix.setRotationDegrees(core::vector3df(0, 0, player->getYaw()));
489                         driver->setTransform(video::ETS_WORLD, matrix);
490                         material.TextureLayer[0].Texture = data->player_marker;
491                         driver->setMaterial(material);
492                         driver->drawMeshBuffer(m_meshbuffer);
493                 }
494         }
495
496         driver->setTransform(video::ETS_VIEW, oldViewMat);
497         driver->setTransform(video::ETS_PROJECTION, oldProjMat);
498         driver->setViewPort(oldViewPort);
499 }