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 "threading/mutex_auto_lock.h"
22 #include "threading/semaphore.h"
23 #include "clientmap.h"
27 #include "util/numeric.h"
28 #include "util/string.h"
33 //// MinimapUpdateThread
36 MinimapUpdateThread::~MinimapUpdateThread()
38 for (std::map
<v3s16
, MinimapMapblock
*>::iterator
39 it
= m_blocks_cache
.begin();
40 it
!= m_blocks_cache
.end(); ++it
) {
44 for (std::deque
<QueuedMinimapUpdate
>::iterator
45 it
= m_update_queue
.begin();
46 it
!= m_update_queue
.end(); ++it
) {
47 QueuedMinimapUpdate
&q
= *it
;
52 bool MinimapUpdateThread::pushBlockUpdate(v3s16 pos
, MinimapMapblock
*data
)
54 MutexAutoLock
lock(m_queue_mutex
);
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
;
70 QueuedMinimapUpdate q
;
73 m_update_queue
.push_back(q
);
78 bool MinimapUpdateThread::popBlockUpdate(QueuedMinimapUpdate
*update
)
80 MutexAutoLock
lock(m_queue_mutex
);
82 if (m_update_queue
.empty())
85 *update
= m_update_queue
.front();
86 m_update_queue
.pop_front();
91 void MinimapUpdateThread::enqueueBlock(v3s16 pos
, MinimapMapblock
*data
)
93 pushBlockUpdate(pos
, data
);
98 void MinimapUpdateThread::doUpdate()
100 QueuedMinimapUpdate update
;
102 while (popBlockUpdate(&update
)) {
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
;
112 std::map
<v3s16
, MinimapMapblock
*>::iterator it
;
113 it
= m_blocks_cache
.find(update
.pos
);
114 if (it
!= m_blocks_cache
.end()) {
116 m_blocks_cache
.erase(it
);
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;
127 MinimapPixel
*MinimapUpdateThread::getMinimapPixel(v3s16 pos
,
128 s16 scan_height
, s16
*pixel_height
)
130 s16 height
= scan_height
- MAP_BLOCKSIZE
;
131 v3s16 blockpos_max
, blockpos_min
, relpos
;
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
);
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
;
152 height
-= MAP_BLOCKSIZE
;
158 s16
MinimapUpdateThread::getAirCount(v3s16 pos
, s16 height
)
161 v3s16 blockpos_max
, blockpos_min
, relpos
;
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
);
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
;
183 void MinimapUpdateThread::getMap(v3s16 pos
, s16 size
, s16 height
, bool is_radar
)
185 v3s16 p
= v3s16(pos
.X
- size
/ 2, pos
.Y
, pos
.Z
- size
/ 2);
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
];
193 s16 pixel_height
= 0;
194 MinimapPixel
*cached_pixel
=
195 getMinimapPixel(v3s16(p
.X
+ x
, p
.Y
, p
.Z
+ z
), height
, &pixel_height
);
197 id
= cached_pixel
->id
;
198 mmpixel
->height
= pixel_height
;
201 mmpixel
->air_count
= getAirCount(v3s16(p
.X
+ x
, p
.Y
, p
.Z
+ z
), height
);
212 Mapper::Mapper(IrrlichtDevice
*device
, Client
*client
)
214 this->driver
= device
->getVideoDriver();
215 this->m_tsrc
= client
->getTextureSource();
216 this->m_shdrsrc
= client
->getShaderSource();
217 this->m_ndef
= client
->getNodeDefManager();
219 // Initialize static settings
220 m_enable_shaders
= g_settings
->getBool("enable_shaders");
221 m_surface_mode_scan_height
=
222 g_settings
->getBool("minimap_double_scan_height") ? 256 : 128;
224 // Initialize minimap data
225 data
= new MinimapData
;
226 data
->mode
= MINIMAP_MODE_OFF
;
227 data
->is_radar
= false;
228 data
->map_invalidated
= true;
229 data
->heightmap_image
= NULL
;
230 data
->minimap_image
= NULL
;
231 data
->texture
= NULL
;
232 data
->heightmap_texture
= NULL
;
233 data
->minimap_shape_round
= g_settings
->getBool("minimap_shape_round");
235 // Get round minimap textures
236 data
->minimap_mask_round
= driver
->createImage(
237 m_tsrc
->getTexture("minimap_mask_round.png"),
238 core::position2d
<s32
>(0, 0),
239 core::dimension2d
<u32
>(MINIMAP_MAX_SX
, MINIMAP_MAX_SY
));
240 data
->minimap_overlay_round
= m_tsrc
->getTexture("minimap_overlay_round.png");
242 // Get square minimap textures
243 data
->minimap_mask_square
= driver
->createImage(
244 m_tsrc
->getTexture("minimap_mask_square.png"),
245 core::position2d
<s32
>(0, 0),
246 core::dimension2d
<u32
>(MINIMAP_MAX_SX
, MINIMAP_MAX_SY
));
247 data
->minimap_overlay_square
= m_tsrc
->getTexture("minimap_overlay_square.png");
249 // Create player marker texture
250 data
->player_marker
= m_tsrc
->getTexture("player_marker.png");
252 // Create mesh buffer for minimap
253 m_meshbuffer
= getMinimapMeshBuffer();
255 // Initialize and start thread
256 m_minimap_update_thread
= new MinimapUpdateThread();
257 m_minimap_update_thread
->data
= data
;
258 m_minimap_update_thread
->start();
263 m_minimap_update_thread
->stop();
264 m_minimap_update_thread
->wait();
266 m_meshbuffer
->drop();
268 data
->minimap_mask_round
->drop();
269 data
->minimap_mask_square
->drop();
271 driver
->removeTexture(data
->texture
);
272 driver
->removeTexture(data
->heightmap_texture
);
273 driver
->removeTexture(data
->minimap_overlay_round
);
274 driver
->removeTexture(data
->minimap_overlay_square
);
277 delete m_minimap_update_thread
;
280 void Mapper::addBlock(v3s16 pos
, MinimapMapblock
*data
)
282 m_minimap_update_thread
->enqueueBlock(pos
, data
);
285 MinimapMode
Mapper::getMinimapMode()
290 void Mapper::toggleMinimapShape()
292 MutexAutoLock
lock(m_mutex
);
294 data
->minimap_shape_round
= !data
->minimap_shape_round
;
295 g_settings
->setBool("minimap_shape_round", data
->minimap_shape_round
);
296 m_minimap_update_thread
->deferUpdate();
299 void Mapper::setMinimapMode(MinimapMode mode
)
301 static const MinimapModeDef modedefs
[MINIMAP_MODE_COUNT
] = {
303 {false, m_surface_mode_scan_height
, 256},
304 {false, m_surface_mode_scan_height
, 128},
305 {false, m_surface_mode_scan_height
, 64},
311 if (mode
>= MINIMAP_MODE_COUNT
)
314 MutexAutoLock
lock(m_mutex
);
316 data
->is_radar
= modedefs
[mode
].is_radar
;
317 data
->scan_height
= modedefs
[mode
].scan_height
;
318 data
->map_size
= modedefs
[mode
].map_size
;
321 m_minimap_update_thread
->deferUpdate();
324 void Mapper::setPos(v3s16 pos
)
326 bool do_update
= false;
329 MutexAutoLock
lock(m_mutex
);
331 if (pos
!= data
->old_pos
) {
332 data
->old_pos
= data
->pos
;
339 m_minimap_update_thread
->deferUpdate();
342 void Mapper::setAngle(f32 angle
)
347 void Mapper::blitMinimapPixelsToImageRadar(video::IImage
*map_image
)
349 for (s16 x
= 0; x
< data
->map_size
; x
++)
350 for (s16 z
= 0; z
< data
->map_size
; z
++) {
351 MinimapPixel
*mmpixel
= &data
->minimap_scan
[x
+ z
* data
->map_size
];
353 video::SColor
c(240, 0, 0, 0);
354 if (mmpixel
->air_count
> 0)
355 c
.setGreen(core::clamp(core::round32(32 + mmpixel
->air_count
* 8), 0, 255));
357 map_image
->setPixel(x
, data
->map_size
- z
- 1, c
);
361 void Mapper::blitMinimapPixelsToImageSurface(
362 video::IImage
*map_image
, video::IImage
*heightmap_image
)
364 for (s16 x
= 0; x
< data
->map_size
; x
++)
365 for (s16 z
= 0; z
< data
->map_size
; z
++) {
366 MinimapPixel
*mmpixel
= &data
->minimap_scan
[x
+ z
* data
->map_size
];
368 video::SColor c
= m_ndef
->get(mmpixel
->id
).minimap_color
;
371 map_image
->setPixel(x
, data
->map_size
- z
- 1, c
);
373 u32 h
= mmpixel
->height
;
374 heightmap_image
->setPixel(x
,data
->map_size
- z
- 1,
375 video::SColor(255, h
, h
, h
));
379 video::ITexture
*Mapper::getMinimapTexture()
381 // update minimap textures when new scan is ready
382 if (data
->map_invalidated
)
383 return data
->texture
;
385 // create minimap and heightmap images in memory
386 core::dimension2d
<u32
> dim(data
->map_size
, data
->map_size
);
387 video::IImage
*map_image
= driver
->createImage(video::ECF_A8R8G8B8
, dim
);
388 video::IImage
*heightmap_image
= driver
->createImage(video::ECF_A8R8G8B8
, dim
);
389 video::IImage
*minimap_image
= driver
->createImage(video::ECF_A8R8G8B8
,
390 core::dimension2d
<u32
>(MINIMAP_MAX_SX
, MINIMAP_MAX_SY
));
392 // Blit MinimapPixels to images
394 blitMinimapPixelsToImageRadar(map_image
);
396 blitMinimapPixelsToImageSurface(map_image
, heightmap_image
);
398 map_image
->copyToScaling(minimap_image
);
401 video::IImage
*minimap_mask
= data
->minimap_shape_round
?
402 data
->minimap_mask_round
: data
->minimap_mask_square
;
405 for (s16 y
= 0; y
< MINIMAP_MAX_SY
; y
++)
406 for (s16 x
= 0; x
< MINIMAP_MAX_SX
; x
++) {
407 video::SColor mask_col
= minimap_mask
->getPixel(x
, y
);
408 if (!mask_col
.getAlpha())
409 minimap_image
->setPixel(x
, y
, video::SColor(0,0,0,0));
414 driver
->removeTexture(data
->texture
);
415 if (data
->heightmap_texture
)
416 driver
->removeTexture(data
->heightmap_texture
);
418 data
->texture
= driver
->addTexture("minimap__", minimap_image
);
419 data
->heightmap_texture
=
420 driver
->addTexture("minimap_heightmap__", heightmap_image
);
421 minimap_image
->drop();
422 heightmap_image
->drop();
424 data
->map_invalidated
= true;
426 return data
->texture
;
429 v3f
Mapper::getYawVec()
431 if (data
->minimap_shape_round
) {
433 cos(m_angle
* core::DEGTORAD
),
434 sin(m_angle
* core::DEGTORAD
),
437 return v3f(1.0, 0.0, 1.0);
441 scene::SMeshBuffer
*Mapper::getMinimapMeshBuffer()
443 scene::SMeshBuffer
*buf
= new scene::SMeshBuffer();
444 buf
->Vertices
.set_used(4);
445 buf
->Indices
.set_used(6);
446 video::SColor
c(255, 255, 255, 255);
448 buf
->Vertices
[0] = video::S3DVertex(-1, -1, 0, 0, 0, 1, c
, 0, 1);
449 buf
->Vertices
[1] = video::S3DVertex(-1, 1, 0, 0, 0, 1, c
, 0, 0);
450 buf
->Vertices
[2] = video::S3DVertex( 1, 1, 0, 0, 0, 1, c
, 1, 0);
451 buf
->Vertices
[3] = video::S3DVertex( 1, -1, 0, 0, 0, 1, c
, 1, 1);
463 void Mapper::drawMinimap()
465 video::ITexture
*minimap_texture
= getMinimapTexture();
466 if (!minimap_texture
)
469 v2u32 screensize
= porting::getWindowSize();
470 const u32 size
= 0.25 * screensize
.Y
;
472 core::rect
<s32
> oldViewPort
= driver
->getViewPort();
473 core::matrix4 oldProjMat
= driver
->getTransform(video::ETS_PROJECTION
);
474 core::matrix4 oldViewMat
= driver
->getTransform(video::ETS_VIEW
);
476 driver
->setViewPort(core::rect
<s32
>(
477 screensize
.X
- size
- 10, 10,
478 screensize
.X
- 10, size
+ 10));
479 driver
->setTransform(video::ETS_PROJECTION
, core::matrix4());
480 driver
->setTransform(video::ETS_VIEW
, core::matrix4());
482 core::matrix4 matrix
;
483 matrix
.makeIdentity();
485 video::SMaterial
&material
= m_meshbuffer
->getMaterial();
486 material
.setFlag(video::EMF_TRILINEAR_FILTER
, true);
487 material
.Lighting
= false;
488 material
.TextureLayer
[0].Texture
= minimap_texture
;
489 material
.TextureLayer
[1].Texture
= data
->heightmap_texture
;
491 if (m_enable_shaders
&& !data
->is_radar
) {
492 u16 sid
= m_shdrsrc
->getShader("minimap_shader", 1, 1);
493 material
.MaterialType
= m_shdrsrc
->getShaderInfo(sid
).material
;
495 material
.MaterialType
= video::EMT_TRANSPARENT_ALPHA_CHANNEL
;
498 if (data
->minimap_shape_round
)
499 matrix
.setRotationDegrees(core::vector3df(0, 0, 360 - m_angle
));
502 driver
->setTransform(video::ETS_WORLD
, matrix
);
503 driver
->setMaterial(material
);
504 driver
->drawMeshBuffer(m_meshbuffer
);
507 video::ITexture
*minimap_overlay
= data
->minimap_shape_round
?
508 data
->minimap_overlay_round
: data
->minimap_overlay_square
;
509 material
.TextureLayer
[0].Texture
= minimap_overlay
;
510 material
.MaterialType
= video::EMT_TRANSPARENT_ALPHA_CHANNEL
;
511 driver
->setMaterial(material
);
512 driver
->drawMeshBuffer(m_meshbuffer
);
514 // If round minimap, draw player marker
515 if (!data
->minimap_shape_round
) {
516 matrix
.setRotationDegrees(core::vector3df(0, 0, m_angle
));
517 material
.TextureLayer
[0].Texture
= data
->player_marker
;
519 driver
->setTransform(video::ETS_WORLD
, matrix
);
520 driver
->setMaterial(material
);
521 driver
->drawMeshBuffer(m_meshbuffer
);
524 // Reset transformations
525 driver
->setTransform(video::ETS_VIEW
, oldViewMat
);
526 driver
->setTransform(video::ETS_PROJECTION
, oldProjMat
);
527 driver
->setViewPort(oldViewPort
);
534 void MinimapMapblock::getMinimapNodes(VoxelManipulator
*vmanip
, v3s16 pos
)
537 for (s16 x
= 0; x
< MAP_BLOCKSIZE
; x
++)
538 for (s16 z
= 0; z
< MAP_BLOCKSIZE
; z
++) {
540 bool surface_found
= false;
541 MinimapPixel
*mmpixel
= &data
[z
* MAP_BLOCKSIZE
+ x
];
543 for (s16 y
= MAP_BLOCKSIZE
-1; y
>= 0; y
--) {
545 MapNode n
= vmanip
->getNodeNoEx(pos
+ p
);
546 if (!surface_found
&& n
.getContent() != CONTENT_AIR
) {
548 mmpixel
->id
= n
.getContent();
549 surface_found
= true;
550 } else if (n
.getContent() == CONTENT_AIR
) {
556 mmpixel
->id
= CONTENT_AIR
;
558 mmpixel
->air_count
= air_count
;