]> git.lizzy.rs Git - dragonfireclient.git/blob - src/minimap.cpp
Document zoom_fov in settingtypes.txt and minetest.conf.example
[dragonfireclient.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 "threading/mutex_auto_lock.h"
22 #include "threading/semaphore.h"
23 #include "clientmap.h"
24 #include "settings.h"
25 #include "nodedef.h"
26 #include "porting.h"
27 #include "util/numeric.h"
28 #include "util/string.h"
29 #include <math.h>
30
31
32 ////
33 //// MinimapUpdateThread
34 ////
35
36 MinimapUpdateThread::~MinimapUpdateThread()
37 {
38         for (std::map<v3s16, MinimapMapblock *>::iterator
39                         it = m_blocks_cache.begin();
40                         it != m_blocks_cache.end(); ++it) {
41                 delete it->second;
42         }
43
44         for (std::deque<QueuedMinimapUpdate>::iterator
45                         it = m_update_queue.begin();
46                         it != m_update_queue.end(); ++it) {
47                 QueuedMinimapUpdate &q = *it;
48                 delete q.data;
49         }
50 }
51
52 bool MinimapUpdateThread::pushBlockUpdate(v3s16 pos, MinimapMapblock *data)
53 {
54         MutexAutoLock lock(m_queue_mutex);
55
56         // Find if block is already in queue.
57         // If it is, update the data and quit.
58         for (std::deque<QueuedMinimapUpdate>::iterator
59                         it = m_update_queue.begin();
60                         it != m_update_queue.end(); ++it) {
61                 QueuedMinimapUpdate &q = *it;
62                 if (q.pos == pos) {
63                         delete q.data;
64                         q.data = data;
65                         return false;
66                 }
67         }
68
69         // Add the block
70         QueuedMinimapUpdate q;
71         q.pos  = pos;
72         q.data = data;
73         m_update_queue.push_back(q);
74
75         return true;
76 }
77
78 bool MinimapUpdateThread::popBlockUpdate(QueuedMinimapUpdate *update)
79 {
80         MutexAutoLock lock(m_queue_mutex);
81
82         if (m_update_queue.empty())
83                 return false;
84
85         *update = m_update_queue.front();
86         m_update_queue.pop_front();
87
88         return true;
89 }
90
91 void MinimapUpdateThread::enqueueBlock(v3s16 pos, MinimapMapblock *data)
92 {
93         pushBlockUpdate(pos, data);
94         deferUpdate();
95 }
96
97
98 void MinimapUpdateThread::doUpdate()
99 {
100         QueuedMinimapUpdate update;
101
102         while (popBlockUpdate(&update)) {
103                 if (update.data) {
104                         // Swap two values in the map using single lookup
105                         std::pair<std::map<v3s16, MinimapMapblock*>::iterator, bool>
106                             result = m_blocks_cache.insert(std::make_pair(update.pos, update.data));
107                         if (result.second == false) {
108                                 delete result.first->second;
109                                 result.first->second = update.data;
110                         }
111                 } else {
112                         std::map<v3s16, MinimapMapblock *>::iterator it;
113                         it = m_blocks_cache.find(update.pos);
114                         if (it != m_blocks_cache.end()) {
115                                 delete it->second;
116                                 m_blocks_cache.erase(it);
117                         }
118                 }
119         }
120
121         if (data->map_invalidated && data->mode != MINIMAP_MODE_OFF) {
122                 getMap(data->pos, data->map_size, data->scan_height, data->is_radar);
123                 data->map_invalidated = false;
124         }
125 }
126
127 MinimapPixel *MinimapUpdateThread::getMinimapPixel(v3s16 pos,
128         s16 scan_height, s16 *pixel_height)
129 {
130         s16 height = scan_height - MAP_BLOCKSIZE;
131         v3s16 blockpos_max, blockpos_min, relpos;
132
133         getNodeBlockPosWithOffset(
134                 v3s16(pos.X, pos.Y - scan_height / 2, pos.Z),
135                 blockpos_min, relpos);
136         getNodeBlockPosWithOffset(
137                 v3s16(pos.X, pos.Y + scan_height / 2, pos.Z),
138                 blockpos_max, relpos);
139
140         for (s16 i = blockpos_max.Y; i > blockpos_min.Y - 1; i--) {
141                 std::map<v3s16, MinimapMapblock *>::iterator it =
142                         m_blocks_cache.find(v3s16(blockpos_max.X, i, blockpos_max.Z));
143                 if (it != m_blocks_cache.end()) {
144                         MinimapMapblock *mmblock = it->second;
145                         MinimapPixel *pixel = &mmblock->data[relpos.Z * MAP_BLOCKSIZE + relpos.X];
146                         if (pixel->id != CONTENT_AIR) {
147                                 *pixel_height = height + pixel->height;
148                                 return pixel;
149                         }
150                 }
151
152                 height -= MAP_BLOCKSIZE;
153         }
154
155         return NULL;
156 }
157
158 s16 MinimapUpdateThread::getAirCount(v3s16 pos, s16 height)
159 {
160         s16 air_count = 0;
161         v3s16 blockpos_max, blockpos_min, relpos;
162
163         getNodeBlockPosWithOffset(
164                 v3s16(pos.X, pos.Y - height / 2, pos.Z),
165                 blockpos_min, relpos);
166         getNodeBlockPosWithOffset(
167                 v3s16(pos.X, pos.Y + height / 2, pos.Z),
168                 blockpos_max, relpos);
169
170         for (s16 i = blockpos_max.Y; i > blockpos_min.Y - 1; i--) {
171                 std::map<v3s16, MinimapMapblock *>::iterator it =
172                         m_blocks_cache.find(v3s16(blockpos_max.X, i, blockpos_max.Z));
173                 if (it != m_blocks_cache.end()) {
174                         MinimapMapblock *mmblock = it->second;
175                         MinimapPixel *pixel = &mmblock->data[relpos.Z * MAP_BLOCKSIZE + relpos.X];
176                         air_count += pixel->air_count;
177                 }
178         }
179
180         return air_count;
181 }
182
183 void MinimapUpdateThread::getMap(v3s16 pos, s16 size, s16 height, bool is_radar)
184 {
185         v3s16 p = v3s16(pos.X - size / 2, pos.Y, pos.Z - size / 2);
186
187         for (s16 x = 0; x < size; x++)
188         for (s16 z = 0; z < size; z++) {
189                 u16 id = CONTENT_AIR;
190                 MinimapPixel *mmpixel = &data->minimap_scan[x + z * size];
191
192                 if (!is_radar) {
193                         s16 pixel_height = 0;
194                         MinimapPixel *cached_pixel =
195                                 getMinimapPixel(v3s16(p.X + x, p.Y, p.Z + z), height, &pixel_height);
196                         if (cached_pixel) {
197                                 id = cached_pixel->id;
198                                 mmpixel->height = pixel_height;
199                         }
200                 } else {
201                         mmpixel->air_count = getAirCount(v3s16(p.X + x, p.Y, p.Z + z), height);
202                 }
203
204                 mmpixel->id = id;
205         }
206 }
207
208 ////
209 //// Mapper
210 ////
211
212 Mapper::Mapper(IrrlichtDevice *device, Client *client)
213 {
214         this->client    = client;
215         this->driver    = device->getVideoDriver();
216         this->m_tsrc    = client->getTextureSource();
217         this->m_shdrsrc = client->getShaderSource();
218         this->m_ndef    = client->getNodeDefManager();
219
220         m_angle = 0.f;
221
222         // Initialize static settings
223         m_enable_shaders = g_settings->getBool("enable_shaders");
224         m_surface_mode_scan_height =
225                 g_settings->getBool("minimap_double_scan_height") ? 256 : 128;
226
227         // Initialize minimap data
228         data = new MinimapData;
229         data->mode              = MINIMAP_MODE_OFF;
230         data->is_radar          = false;
231         data->map_invalidated   = true;
232         data->heightmap_image   = NULL;
233         data->minimap_image     = NULL;
234         data->texture           = NULL;
235         data->heightmap_texture = NULL;
236         data->minimap_shape_round = g_settings->getBool("minimap_shape_round");
237
238         // Get round minimap textures
239         data->minimap_mask_round = driver->createImage(
240                 m_tsrc->getTexture("minimap_mask_round.png"),
241                 core::position2d<s32>(0, 0),
242                 core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
243         data->minimap_overlay_round = m_tsrc->getTexture("minimap_overlay_round.png");
244
245         // Get square minimap textures
246         data->minimap_mask_square = driver->createImage(
247                 m_tsrc->getTexture("minimap_mask_square.png"),
248                 core::position2d<s32>(0, 0),
249                 core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
250         data->minimap_overlay_square = m_tsrc->getTexture("minimap_overlay_square.png");
251
252         // Create player marker texture
253         data->player_marker = m_tsrc->getTexture("player_marker.png");
254         // Create object marker texture
255         data->object_marker_red = m_tsrc->getTexture("object_marker_red.png");
256
257         // Create mesh buffer for minimap
258         m_meshbuffer = getMinimapMeshBuffer();
259
260         // Initialize and start thread
261         m_minimap_update_thread = new MinimapUpdateThread();
262         m_minimap_update_thread->data = data;
263         m_minimap_update_thread->start();
264 }
265
266 Mapper::~Mapper()
267 {
268         m_minimap_update_thread->stop();
269         m_minimap_update_thread->wait();
270
271         m_meshbuffer->drop();
272
273         data->minimap_mask_round->drop();
274         data->minimap_mask_square->drop();
275
276         driver->removeTexture(data->texture);
277         driver->removeTexture(data->heightmap_texture);
278         driver->removeTexture(data->minimap_overlay_round);
279         driver->removeTexture(data->minimap_overlay_square);
280         driver->removeTexture(data->object_marker_red);
281
282         delete data;
283         delete m_minimap_update_thread;
284 }
285
286 void Mapper::addBlock(v3s16 pos, MinimapMapblock *data)
287 {
288         m_minimap_update_thread->enqueueBlock(pos, data);
289 }
290
291 MinimapMode Mapper::getMinimapMode()
292 {
293         return data->mode;
294 }
295
296 void Mapper::toggleMinimapShape()
297 {
298         MutexAutoLock lock(m_mutex);
299
300         data->minimap_shape_round = !data->minimap_shape_round;
301         g_settings->setBool("minimap_shape_round", data->minimap_shape_round);
302         m_minimap_update_thread->deferUpdate();
303 }
304
305 void Mapper::setMinimapMode(MinimapMode mode)
306 {
307         static const MinimapModeDef modedefs[MINIMAP_MODE_COUNT] = {
308                 {false, 0, 0},
309                 {false, m_surface_mode_scan_height, 256},
310                 {false, m_surface_mode_scan_height, 128},
311                 {false, m_surface_mode_scan_height, 64},
312                 {true, 32, 128},
313                 {true, 32, 64},
314                 {true, 32, 32}
315         };
316
317         if (mode >= MINIMAP_MODE_COUNT)
318                 return;
319
320         MutexAutoLock lock(m_mutex);
321
322         data->is_radar    = modedefs[mode].is_radar;
323         data->scan_height = modedefs[mode].scan_height;
324         data->map_size    = modedefs[mode].map_size;
325         data->mode        = mode;
326
327         m_minimap_update_thread->deferUpdate();
328 }
329
330 void Mapper::setPos(v3s16 pos)
331 {
332         bool do_update = false;
333
334         {
335                 MutexAutoLock lock(m_mutex);
336
337                 if (pos != data->old_pos) {
338                         data->old_pos = data->pos;
339                         data->pos = pos;
340                         do_update = true;
341                 }
342         }
343
344         if (do_update)
345                 m_minimap_update_thread->deferUpdate();
346 }
347
348 void Mapper::setAngle(f32 angle)
349 {
350         m_angle = angle;
351 }
352
353 void Mapper::blitMinimapPixelsToImageRadar(video::IImage *map_image)
354 {
355         for (s16 x = 0; x < data->map_size; x++)
356         for (s16 z = 0; z < data->map_size; z++) {
357                 MinimapPixel *mmpixel = &data->minimap_scan[x + z * data->map_size];
358
359                 video::SColor c(240, 0, 0, 0);
360                 if (mmpixel->air_count > 0)
361                         c.setGreen(core::clamp(core::round32(32 + mmpixel->air_count * 8), 0, 255));
362
363                 map_image->setPixel(x, data->map_size - z - 1, c);
364         }
365 }
366
367 void Mapper::blitMinimapPixelsToImageSurface(
368         video::IImage *map_image, video::IImage *heightmap_image)
369 {
370         for (s16 x = 0; x < data->map_size; x++)
371         for (s16 z = 0; z < data->map_size; z++) {
372                 MinimapPixel *mmpixel = &data->minimap_scan[x + z * data->map_size];
373
374                 video::SColor c = m_ndef->get(mmpixel->id).minimap_color;
375                 c.setAlpha(240);
376
377                 map_image->setPixel(x, data->map_size - z - 1, c);
378
379                 u32 h = mmpixel->height;
380                 heightmap_image->setPixel(x,data->map_size - z - 1,
381                         video::SColor(255, h, h, h));
382         }
383 }
384
385 video::ITexture *Mapper::getMinimapTexture()
386 {
387         // update minimap textures when new scan is ready
388         if (data->map_invalidated)
389                 return data->texture;
390
391         // create minimap and heightmap images in memory
392         core::dimension2d<u32> dim(data->map_size, data->map_size);
393         video::IImage *map_image       = driver->createImage(video::ECF_A8R8G8B8, dim);
394         video::IImage *heightmap_image = driver->createImage(video::ECF_A8R8G8B8, dim);
395         video::IImage *minimap_image   = driver->createImage(video::ECF_A8R8G8B8,
396                 core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
397
398         // Blit MinimapPixels to images
399         if (data->is_radar)
400                 blitMinimapPixelsToImageRadar(map_image);
401         else
402                 blitMinimapPixelsToImageSurface(map_image, heightmap_image);
403
404         map_image->copyToScaling(minimap_image);
405         map_image->drop();
406
407         video::IImage *minimap_mask = data->minimap_shape_round ?
408                 data->minimap_mask_round : data->minimap_mask_square;
409
410         if (minimap_mask) {
411                 for (s16 y = 0; y < MINIMAP_MAX_SY; y++)
412                 for (s16 x = 0; x < MINIMAP_MAX_SX; x++) {
413                         video::SColor mask_col = minimap_mask->getPixel(x, y);
414                         if (!mask_col.getAlpha())
415                                 minimap_image->setPixel(x, y, video::SColor(0,0,0,0));
416                 }
417         }
418
419         if (data->texture)
420                 driver->removeTexture(data->texture);
421         if (data->heightmap_texture)
422                 driver->removeTexture(data->heightmap_texture);
423
424         data->texture = driver->addTexture("minimap__", minimap_image);
425         data->heightmap_texture =
426                 driver->addTexture("minimap_heightmap__", heightmap_image);
427         minimap_image->drop();
428         heightmap_image->drop();
429
430         data->map_invalidated = true;
431
432         return data->texture;
433 }
434
435 v3f Mapper::getYawVec()
436 {
437         if (data->minimap_shape_round) {
438                 return v3f(
439                         cos(m_angle * core::DEGTORAD),
440                         sin(m_angle * core::DEGTORAD),
441                         1.0);
442         } else {
443                 return v3f(1.0, 0.0, 1.0);
444         }
445 }
446
447 scene::SMeshBuffer *Mapper::getMinimapMeshBuffer()
448 {
449         scene::SMeshBuffer *buf = new scene::SMeshBuffer();
450         buf->Vertices.set_used(4);
451         buf->Indices.set_used(6);
452         video::SColor c(255, 255, 255, 255);
453
454         buf->Vertices[0] = video::S3DVertex(-1, -1, 0, 0, 0, 1, c, 0, 1);
455         buf->Vertices[1] = video::S3DVertex(-1,  1, 0, 0, 0, 1, c, 0, 0);
456         buf->Vertices[2] = video::S3DVertex( 1,  1, 0, 0, 0, 1, c, 1, 0);
457         buf->Vertices[3] = video::S3DVertex( 1, -1, 0, 0, 0, 1, c, 1, 1);
458
459         buf->Indices[0] = 0;
460         buf->Indices[1] = 1;
461         buf->Indices[2] = 2;
462         buf->Indices[3] = 2;
463         buf->Indices[4] = 3;
464         buf->Indices[5] = 0;
465
466         return buf;
467 }
468
469 void Mapper::drawMinimap()
470 {
471         video::ITexture *minimap_texture = getMinimapTexture();
472         if (!minimap_texture)
473                 return;
474
475         updateActiveMarkers();
476         v2u32 screensize = porting::getWindowSize();
477         const u32 size = 0.25 * screensize.Y;
478
479         core::rect<s32> oldViewPort = driver->getViewPort();
480         core::matrix4 oldProjMat = driver->getTransform(video::ETS_PROJECTION);
481         core::matrix4 oldViewMat = driver->getTransform(video::ETS_VIEW);
482
483         driver->setViewPort(core::rect<s32>(
484                 screensize.X - size - 10, 10,
485                 screensize.X - 10, size + 10));
486         driver->setTransform(video::ETS_PROJECTION, core::matrix4());
487         driver->setTransform(video::ETS_VIEW, core::matrix4());
488
489         core::matrix4 matrix;
490         matrix.makeIdentity();
491
492         video::SMaterial &material = m_meshbuffer->getMaterial();
493         material.setFlag(video::EMF_TRILINEAR_FILTER, true);
494         material.Lighting = false;
495         material.TextureLayer[0].Texture = minimap_texture;
496         material.TextureLayer[1].Texture = data->heightmap_texture;
497
498         if (m_enable_shaders && !data->is_radar) {
499                 u16 sid = m_shdrsrc->getShader("minimap_shader", 1, 1);
500                 material.MaterialType = m_shdrsrc->getShaderInfo(sid).material;
501         } else {
502                 material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
503         }
504
505         if (data->minimap_shape_round)
506                 matrix.setRotationDegrees(core::vector3df(0, 0, 360 - m_angle));
507
508         // Draw minimap
509         driver->setTransform(video::ETS_WORLD, matrix);
510         driver->setMaterial(material);
511         driver->drawMeshBuffer(m_meshbuffer);
512
513         // Draw overlay
514         video::ITexture *minimap_overlay = data->minimap_shape_round ?
515                 data->minimap_overlay_round : data->minimap_overlay_square;
516         material.TextureLayer[0].Texture = minimap_overlay;
517         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
518         driver->setMaterial(material);
519         driver->drawMeshBuffer(m_meshbuffer);
520
521         // If round minimap, draw player marker
522         if (!data->minimap_shape_round) {
523                 matrix.setRotationDegrees(core::vector3df(0, 0, m_angle));
524                 material.TextureLayer[0].Texture = data->player_marker;
525
526                 driver->setTransform(video::ETS_WORLD, matrix);
527                 driver->setMaterial(material);
528                 driver->drawMeshBuffer(m_meshbuffer);
529         }
530
531         // Reset transformations
532         driver->setTransform(video::ETS_VIEW, oldViewMat);
533         driver->setTransform(video::ETS_PROJECTION, oldProjMat);
534         driver->setViewPort(oldViewPort);
535
536         // Draw player markers
537         v2s32 s_pos(screensize.X - size - 10, 10);
538         core::dimension2di imgsize(data->object_marker_red->getOriginalSize());
539         core::rect<s32> img_rect(0, 0, imgsize.Width, imgsize.Height);
540         static const video::SColor col(255, 255, 255, 255);
541         static const video::SColor c[4] = {col, col, col, col};
542         f32 sin_angle = sin(m_angle * core::DEGTORAD);
543         f32 cos_angle = cos(m_angle * core::DEGTORAD);
544         s32 marker_size2 =  0.025 * (float)size;
545         for (std::list<v2f>::const_iterator
546                         i = m_active_markers.begin();
547                         i != m_active_markers.end(); ++i) {
548                 v2f posf = *i;
549                 if (data->minimap_shape_round) {
550                         f32 t1 = posf.X * cos_angle - posf.Y * sin_angle;
551                         f32 t2 = posf.X * sin_angle + posf.Y * cos_angle;
552                         posf.X = t1;
553                         posf.Y = t2;
554                 }
555                 posf.X = (posf.X + 0.5) * (float)size;
556                 posf.Y = (posf.Y + 0.5) * (float)size;
557                 core::rect<s32> dest_rect(
558                         s_pos.X + posf.X - marker_size2,
559                         s_pos.Y + posf.Y - marker_size2,
560                         s_pos.X + posf.X + marker_size2,
561                         s_pos.Y + posf.Y + marker_size2);
562                 driver->draw2DImage(data->object_marker_red, dest_rect,
563                         img_rect, &dest_rect, &c[0], true);
564         }
565 }
566
567 void Mapper::updateActiveMarkers ()
568 {
569         video::IImage *minimap_mask = data->minimap_shape_round ?
570                 data->minimap_mask_round : data->minimap_mask_square;
571
572         std::list<Nametag *> *nametags = client->getCamera()->getNametags();
573
574         m_active_markers.clear();
575
576         for (std::list<Nametag *>::const_iterator
577                         i = nametags->begin();
578                         i != nametags->end(); ++i) {
579                 Nametag *nametag = *i;
580                 v3s16 pos = floatToInt(nametag->parent_node->getPosition() +
581                         intToFloat(client->getCamera()->getOffset(), BS), BS);
582                 pos -= data->pos - v3s16(data->map_size / 2,
583                                 data->scan_height / 2,
584                                 data->map_size / 2);
585                 if (pos.X < 0 || pos.X > data->map_size ||
586                                 pos.Y < 0 || pos.Y > data->scan_height ||
587                                 pos.Z < 0 || pos.Z > data->map_size) {
588                         continue;
589                 }
590                 pos.X = ((float)pos.X / data->map_size) * MINIMAP_MAX_SX;
591                 pos.Z = ((float)pos.Z / data->map_size) * MINIMAP_MAX_SY;
592                 video::SColor mask_col = minimap_mask->getPixel(pos.X, pos.Z);
593                 if (!mask_col.getAlpha()) {
594                         continue;
595                 }
596                 m_active_markers.push_back(v2f(((float)pos.X / (float)MINIMAP_MAX_SX) - 0.5,
597                         (1.0 - (float)pos.Z / (float)MINIMAP_MAX_SY) - 0.5));
598         }
599 }
600
601 ////
602 //// MinimapMapblock
603 ////
604
605 void MinimapMapblock::getMinimapNodes(VoxelManipulator *vmanip, v3s16 pos)
606 {
607
608         for (s16 x = 0; x < MAP_BLOCKSIZE; x++)
609         for (s16 z = 0; z < MAP_BLOCKSIZE; z++) {
610                 s16 air_count = 0;
611                 bool surface_found = false;
612                 MinimapPixel *mmpixel = &data[z * MAP_BLOCKSIZE + x];
613
614                 for (s16 y = MAP_BLOCKSIZE -1; y >= 0; y--) {
615                         v3s16 p(x, y, z);
616                         MapNode n = vmanip->getNodeNoEx(pos + p);
617                         if (!surface_found && n.getContent() != CONTENT_AIR) {
618                                 mmpixel->height = y;
619                                 mmpixel->id = n.getContent();
620                                 surface_found = true;
621                         } else if (n.getContent() == CONTENT_AIR) {
622                                 air_count++;
623                         }
624                 }
625
626                 if (!surface_found)
627                         mmpixel->id = CONTENT_AIR;
628
629                 mmpixel->air_count = air_count;
630         }
631 }