Clean-up Minimap code

- Fixed race conditions
- Fixed null dereference
- Fixed out-of-bounds array access
- MinimapMapblock is now allocated and added to update queue only when enabled
- Removed dependency on LocalPlayer
- Fixed code style
- Simplified expressions and program logic
- Cleaned minimap object interfaces
This commit is contained in:
kwolekr 2015-07-08 00:45:24 -04:00
parent ba15c98e4d
commit e234d8b378
5 changed files with 450 additions and 398 deletions

View File

@ -525,9 +525,12 @@ void Client::step(float dtime)
while (!m_mesh_update_thread.m_queue_out.empty()) while (!m_mesh_update_thread.m_queue_out.empty())
{ {
num_processed_meshes++; num_processed_meshes++;
MinimapMapblock *minimap_mapblock = NULL;
bool do_mapper_update = true;
MeshUpdateResult r = m_mesh_update_thread.m_queue_out.pop_frontNoEx(); MeshUpdateResult r = m_mesh_update_thread.m_queue_out.pop_frontNoEx();
MapBlock *block = m_env.getMap().getBlockNoCreateNoEx(r.p); MapBlock *block = m_env.getMap().getBlockNoCreateNoEx(r.p);
MinimapMapblock *minimap_mapblock = NULL;
if (block) { if (block) {
// Delete the old mesh // Delete the old mesh
if (block->mesh != NULL) { if (block->mesh != NULL) {
@ -535,8 +538,10 @@ void Client::step(float dtime)
block->mesh = NULL; block->mesh = NULL;
} }
if (r.mesh) if (r.mesh) {
minimap_mapblock = r.mesh->getMinimapMapblock(); minimap_mapblock = r.mesh->getMinimapMapblock();
do_mapper_update = (minimap_mapblock != NULL);
}
if (r.mesh && r.mesh->getMesh()->getMeshBufferCount() == 0) { if (r.mesh && r.mesh->getMesh()->getMeshBufferCount() == 0) {
delete r.mesh; delete r.mesh;
@ -550,6 +555,7 @@ void Client::step(float dtime)
minimap_mapblock = NULL; minimap_mapblock = NULL;
} }
if (do_mapper_update)
m_mapper->addBlock(r.p, minimap_mapblock); m_mapper->addBlock(r.p, minimap_mapblock);
if (r.ack_block_to_server) { if (r.ack_block_to_server) {

View File

@ -4045,10 +4045,11 @@ void Game::updateFrame(std::vector<aabb3f> &highlight_boxes,
} }
/* /*
Update minimap pos Update minimap pos and rotation
*/ */
if (flags.show_minimap && flags.show_hud) { if (flags.show_minimap && flags.show_hud) {
mapper->setPos(floatToInt(player->getPosition(), BS)); mapper->setPos(floatToInt(player->getPosition(), BS));
mapper->setAngle(player->getYaw());
} }
/* /*

View File

@ -1029,7 +1029,7 @@ static void updateAllFastFaceRows(MeshMakeData *data,
MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset): MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
m_mesh(new scene::SMesh()), m_mesh(new scene::SMesh()),
m_minimap_mapblock(new MinimapMapblock), m_minimap_mapblock(NULL),
m_gamedef(data->m_gamedef), m_gamedef(data->m_gamedef),
m_tsrc(m_gamedef->getTextureSource()), m_tsrc(m_gamedef->getTextureSource()),
m_shdrsrc(m_gamedef->getShaderSource()), m_shdrsrc(m_gamedef->getShaderSource()),
@ -1044,29 +1044,9 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
m_enable_highlighting = g_settings->getBool("enable_node_highlighting"); m_enable_highlighting = g_settings->getBool("enable_node_highlighting");
if (g_settings->getBool("enable_minimap")) { if (g_settings->getBool("enable_minimap")) {
v3s16 blockpos_nodes = data->m_blockpos * MAP_BLOCKSIZE; m_minimap_mapblock = new MinimapMapblock;
for(s16 x = 0; x < MAP_BLOCKSIZE; x++) { m_minimap_mapblock->getMinimapNodes(
for(s16 z = 0; z < MAP_BLOCKSIZE; z++) { &data->m_vmanip, data->m_blockpos * MAP_BLOCKSIZE);
s16 air_count = 0;
bool surface_found = false;
MinimapPixel* minimap_pixel = &m_minimap_mapblock->data[x + z * MAP_BLOCKSIZE];
for(s16 y = MAP_BLOCKSIZE -1; y > -1; y--) {
v3s16 p(x, y, z);
MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes + p);
if (!surface_found && n.getContent() != CONTENT_AIR) {
minimap_pixel->height = y;
minimap_pixel->id = n.getContent();
surface_found = true;
} else if (n.getContent() == CONTENT_AIR) {
air_count++;
}
}
if (!surface_found) {
minimap_pixel->id = CONTENT_AIR;
}
minimap_pixel->air_count = air_count;
}
}
} }
// 4-21ms for MAP_BLOCKSIZE=16 (NOTE: probably outdated) // 4-21ms for MAP_BLOCKSIZE=16 (NOTE: probably outdated)

View File

@ -18,6 +18,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/ */
#include "minimap.h" #include "minimap.h"
#include <math.h>
#include "logoutputbuffer.h" #include "logoutputbuffer.h"
#include "jthread/jmutexautolock.h" #include "jthread/jmutexautolock.h"
#include "jthread/jsemaphore.h" #include "jthread/jsemaphore.h"
@ -27,138 +28,125 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "porting.h" #include "porting.h"
#include "util/numeric.h" #include "util/numeric.h"
#include "util/string.h" #include "util/string.h"
#include <math.h>
QueuedMinimapUpdate::QueuedMinimapUpdate():
pos(-1337,-1337,-1337),
data(NULL)
{
}
QueuedMinimapUpdate::~QueuedMinimapUpdate() ////
{ //// MinimapUpdateThread
delete data; ////
}
MinimapUpdateQueue::MinimapUpdateQueue()
{
}
MinimapUpdateQueue::~MinimapUpdateQueue()
{
JMutexAutoLock lock(m_mutex);
for (std::list<QueuedMinimapUpdate*>::iterator
i = m_queue.begin();
i != m_queue.end(); ++i) {
QueuedMinimapUpdate *q = *i;
delete q;
}
}
bool MinimapUpdateQueue::addBlock(v3s16 pos, MinimapMapblock *data)
{
DSTACK(__FUNCTION_NAME);
JMutexAutoLock lock(m_mutex);
/*
Find if block is already in queue.
If it is, update the data and quit.
*/
for (std::list<QueuedMinimapUpdate*>::iterator
i = m_queue.begin();
i != m_queue.end(); ++i) {
QueuedMinimapUpdate *q = *i;
if (q->pos == pos) {
delete q->data;
q->data = data;
return false;
}
}
/*
Add the block
*/
QueuedMinimapUpdate *q = new QueuedMinimapUpdate;
q->pos = pos;
q->data = data;
m_queue.push_back(q);
return true;
}
QueuedMinimapUpdate * MinimapUpdateQueue::pop()
{
JMutexAutoLock lock(m_mutex);
for (std::list<QueuedMinimapUpdate*>::iterator
i = m_queue.begin();
i != m_queue.end(); i++) {
QueuedMinimapUpdate *q = *i;
m_queue.erase(i);
return q;
}
return NULL;
}
/*
Minimap update thread
*/
void MinimapUpdateThread::enqueue_Block(v3s16 pos, MinimapMapblock *data)
{
m_queue.addBlock(pos, data);
deferUpdate();
}
void MinimapUpdateThread::doUpdate()
{
while (m_queue.size()) {
QueuedMinimapUpdate *q = m_queue.pop();
std::map<v3s16, MinimapMapblock *>::iterator it;
it = m_blocks_cache.find(q->pos);
if (q->data) {
m_blocks_cache[q->pos] = q->data;
} else if (it != m_blocks_cache.end()) {
delete it->second;
m_blocks_cache.erase(it);
}
}
if (data->map_invalidated) {
if (data->mode != MINIMAP_MODE_OFF) {
getMap(data->pos, data->map_size, data->scan_height, data->radar);
data->map_invalidated = false;
}
}
}
MinimapUpdateThread::~MinimapUpdateThread() MinimapUpdateThread::~MinimapUpdateThread()
{ {
for (std::map<v3s16, MinimapMapblock *>::iterator for (std::map<v3s16, MinimapMapblock *>::iterator
it = m_blocks_cache.begin(); it = m_blocks_cache.begin();
it != m_blocks_cache.end(); it++) { it != m_blocks_cache.end(); ++it) {
delete it->second; delete it->second;
} }
for (std::deque<QueuedMinimapUpdate>::iterator
it = m_update_queue.begin();
it != m_update_queue.end(); ++it) {
QueuedMinimapUpdate &q = *it;
delete q.data;
}
} }
MinimapPixel *MinimapUpdateThread::getMinimapPixel(v3s16 pos, s16 height, s16 &pixel_height) bool MinimapUpdateThread::pushBlockUpdate(v3s16 pos, MinimapMapblock *data)
{ {
pixel_height = height - MAP_BLOCKSIZE; JMutexAutoLock lock(m_queue_mutex);
v3s16 blockpos_max, blockpos_min, relpos;
getNodeBlockPosWithOffset(v3s16(pos.X, pos.Y - height / 2, pos.Z), blockpos_min, relpos); // Find if block is already in queue.
getNodeBlockPosWithOffset(v3s16(pos.X, pos.Y + height / 2, pos.Z), blockpos_max, relpos); // If it is, update the data and quit.
for (std::deque<QueuedMinimapUpdate>::iterator
it = m_update_queue.begin();
it != m_update_queue.end(); ++it) {
QueuedMinimapUpdate &q = *it;
if (q.pos == pos) {
delete q.data;
q.data = data;
return false;
}
}
// Add the block
QueuedMinimapUpdate q;
q.pos = pos;
q.data = data;
m_update_queue.push_back(q);
return true;
}
bool MinimapUpdateThread::popBlockUpdate(QueuedMinimapUpdate *update)
{
JMutexAutoLock lock(m_queue_mutex);
if (m_update_queue.empty())
return false;
*update = m_update_queue.front();
m_update_queue.pop_front();
return true;
}
void MinimapUpdateThread::enqueueBlock(v3s16 pos, MinimapMapblock *data)
{
pushBlockUpdate(pos, data);
deferUpdate();
}
void MinimapUpdateThread::doUpdate()
{
QueuedMinimapUpdate update;
while (popBlockUpdate(&update)) {
if (update.data) {
m_blocks_cache[update.pos] = update.data;
} else {
std::map<v3s16, MinimapMapblock *>::iterator it; std::map<v3s16, MinimapMapblock *>::iterator it;
for (s16 i = blockpos_max.Y; i > blockpos_min.Y - 1; i--) { it = m_blocks_cache.find(update.pos);
it = m_blocks_cache.find(v3s16(blockpos_max.X, i, blockpos_max.Z));
if (it != m_blocks_cache.end()) { if (it != m_blocks_cache.end()) {
MinimapPixel *pixel = &it->second->data[relpos.X + relpos.Z * MAP_BLOCKSIZE]; delete it->second;
m_blocks_cache.erase(it);
}
}
}
if (data->map_invalidated && data->mode != MINIMAP_MODE_OFF) {
getMap(data->pos, data->map_size, data->scan_height, data->is_radar);
data->map_invalidated = false;
}
}
MinimapPixel *MinimapUpdateThread::getMinimapPixel(v3s16 pos,
s16 scan_height, s16 *pixel_height)
{
s16 height = scan_height - MAP_BLOCKSIZE;
v3s16 blockpos_max, blockpos_min, relpos;
getNodeBlockPosWithOffset(
v3s16(pos.X, pos.Y - scan_height / 2, pos.Z),
blockpos_min, relpos);
getNodeBlockPosWithOffset(
v3s16(pos.X, pos.Y + scan_height / 2, pos.Z),
blockpos_max, relpos);
for (s16 i = blockpos_max.Y; i > blockpos_min.Y - 1; i--) {
std::map<v3s16, MinimapMapblock *>::iterator it =
m_blocks_cache.find(v3s16(blockpos_max.X, i, blockpos_max.Z));
if (it != m_blocks_cache.end()) {
MinimapMapblock *mmblock = it->second;
MinimapPixel *pixel = &mmblock->data[relpos.Z * MAP_BLOCKSIZE + relpos.X];
if (pixel->id != CONTENT_AIR) { if (pixel->id != CONTENT_AIR) {
pixel_height += pixel->height; *pixel_height = height + pixel->height;
return pixel; return pixel;
} }
} }
pixel_height -= MAP_BLOCKSIZE;
height -= MAP_BLOCKSIZE;
} }
return NULL; return NULL;
} }
@ -166,80 +154,100 @@ s16 MinimapUpdateThread::getAirCount(v3s16 pos, s16 height)
{ {
s16 air_count = 0; s16 air_count = 0;
v3s16 blockpos_max, blockpos_min, relpos; v3s16 blockpos_max, blockpos_min, relpos;
getNodeBlockPosWithOffset(v3s16(pos.X, pos.Y - height / 2, pos.Z), blockpos_min, relpos);
getNodeBlockPosWithOffset(v3s16(pos.X, pos.Y + height / 2, pos.Z), blockpos_max, relpos); getNodeBlockPosWithOffset(
std::map<v3s16, MinimapMapblock *>::iterator it; v3s16(pos.X, pos.Y - height / 2, pos.Z),
blockpos_min, relpos);
getNodeBlockPosWithOffset(
v3s16(pos.X, pos.Y + height / 2, pos.Z),
blockpos_max, relpos);
for (s16 i = blockpos_max.Y; i > blockpos_min.Y - 1; i--) { for (s16 i = blockpos_max.Y; i > blockpos_min.Y - 1; i--) {
it = m_blocks_cache.find(v3s16(blockpos_max.X, i, blockpos_max.Z)); std::map<v3s16, MinimapMapblock *>::iterator it =
m_blocks_cache.find(v3s16(blockpos_max.X, i, blockpos_max.Z));
if (it != m_blocks_cache.end()) { if (it != m_blocks_cache.end()) {
MinimapPixel *pixel = &it->second->data[relpos.X + relpos.Z * MAP_BLOCKSIZE]; MinimapMapblock *mmblock = it->second;
MinimapPixel *pixel = &mmblock->data[relpos.Z * MAP_BLOCKSIZE + relpos.X];
air_count += pixel->air_count; air_count += pixel->air_count;
} }
} }
return air_count; return air_count;
} }
void MinimapUpdateThread::getMap(v3s16 pos, s16 size, s16 height, bool radar) void MinimapUpdateThread::getMap(v3s16 pos, s16 size, s16 height, bool is_radar)
{ {
v3s16 p = v3s16(pos.X - size / 2, pos.Y, pos.Z - size / 2); v3s16 p = v3s16(pos.X - size / 2, pos.Y, pos.Z - size / 2);
for (s16 x = 0; x < size; x++) { for (s16 x = 0; x < size; x++)
for (s16 z = 0; z < size; z++) { for (s16 z = 0; z < size; z++) {
u16 id = CONTENT_AIR; u16 id = CONTENT_AIR;
MinimapPixel* minimap_pixel = &data->minimap_scan[x + z * size]; MinimapPixel *mmpixel = &data->minimap_scan[x + z * size];
if (!radar) {
if (!is_radar) {
s16 pixel_height = 0; s16 pixel_height = 0;
MinimapPixel *cached_pixel = MinimapPixel *cached_pixel =
getMinimapPixel(v3s16(p.X + x, p.Y, p.Z + z), height, pixel_height); getMinimapPixel(v3s16(p.X + x, p.Y, p.Z + z), height, &pixel_height);
if (cached_pixel) { if (cached_pixel) {
id = cached_pixel->id; id = cached_pixel->id;
minimap_pixel->height = pixel_height; mmpixel->height = pixel_height;
} }
} else { } else {
minimap_pixel->air_count = getAirCount (v3s16(p.X + x, p.Y, p.Z + z), height); mmpixel->air_count = getAirCount(v3s16(p.X + x, p.Y, p.Z + z), height);
}
minimap_pixel->id = id;
} }
mmpixel->id = id;
} }
} }
////
//// Mapper
////
Mapper::Mapper(IrrlichtDevice *device, Client *client) Mapper::Mapper(IrrlichtDevice *device, Client *client)
{ {
this->device = device;
this->client = client;
this->driver = device->getVideoDriver(); this->driver = device->getVideoDriver();
this->tsrc = client->getTextureSource(); this->m_tsrc = client->getTextureSource();
this->player = client->getEnv().getLocalPlayer(); this->m_shdrsrc = client->getShaderSource();
this->shdrsrc = client->getShaderSource(); this->m_ndef = client->getNodeDefManager();
// Initialize static settings
m_enable_shaders = g_settings->getBool("enable_shaders"); m_enable_shaders = g_settings->getBool("enable_shaders");
m_enable_shaders = g_settings->getBool("enable_shaders"); m_surface_mode_scan_height =
if (g_settings->getBool("minimap_double_scan_height")) { g_settings->getBool("minimap_double_scan_height") ? 256 : 128;
m_surface_mode_scan_height = 256;
} else { // Initialize minimap data
m_surface_mode_scan_height = 128;
}
data = new MinimapData; data = new MinimapData;
data->mode = MINIMAP_MODE_OFF; data->mode = MINIMAP_MODE_OFF;
data->radar = false; data->is_radar = false;
data->map_invalidated = true; data->map_invalidated = true;
data->heightmap_image = NULL; data->heightmap_image = NULL;
data->minimap_image = NULL; data->minimap_image = NULL;
data->texture = NULL; data->texture = NULL;
data->minimap_shape_round = g_settings->getBool("minimap_shape_round"); data->minimap_shape_round = g_settings->getBool("minimap_shape_round");
std::string fname1 = "minimap_mask_round.png";
std::string fname2 = "minimap_overlay_round.png"; // Get round minimap textures
data->minimap_mask_round = driver->createImage (tsrc->getTexture(fname1), data->minimap_mask_round = driver->createImage(
core::position2d<s32>(0,0), core::dimension2d<u32>(512,512)); m_tsrc->getTexture("minimap_mask_round.png"),
data->minimap_overlay_round = tsrc->getTexture(fname2); core::position2d<s32>(0, 0),
fname1 = "minimap_mask_square.png"; core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
fname2 = "minimap_overlay_square.png"; data->minimap_overlay_round = m_tsrc->getTexture("minimap_overlay_round.png");
data->minimap_mask_square = driver->createImage (tsrc->getTexture(fname1),
core::position2d<s32>(0,0), core::dimension2d<u32>(512,512)); // Get square minimap textures
data->minimap_overlay_square = tsrc->getTexture(fname2); data->minimap_mask_square = driver->createImage(
data->player_marker = tsrc->getTexture("player_marker.png"); m_tsrc->getTexture("minimap_mask_square.png"),
core::position2d<s32>(0, 0),
core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
data->minimap_overlay_square = m_tsrc->getTexture("minimap_overlay_square.png");
// Create player marker texture
data->player_marker = m_tsrc->getTexture("player_marker.png");
// Create mesh buffer for minimap
m_meshbuffer = getMinimapMeshBuffer(); m_meshbuffer = getMinimapMeshBuffer();
m_minimap_update_thread = new MinimapUpdateThread(device, client);
// Initialize and start thread
m_minimap_update_thread = new MinimapUpdateThread();
m_minimap_update_thread->data = data; m_minimap_update_thread->data = data;
m_minimap_update_thread->Start(); m_minimap_update_thread->Start();
} }
@ -248,20 +256,24 @@ Mapper::~Mapper()
{ {
m_minimap_update_thread->Stop(); m_minimap_update_thread->Stop();
m_minimap_update_thread->Wait(); m_minimap_update_thread->Wait();
m_meshbuffer->drop(); m_meshbuffer->drop();
data->minimap_mask_round->drop(); data->minimap_mask_round->drop();
data->minimap_mask_square->drop(); data->minimap_mask_square->drop();
driver->removeTexture(data->texture); driver->removeTexture(data->texture);
driver->removeTexture(data->heightmap_texture); driver->removeTexture(data->heightmap_texture);
driver->removeTexture(data->minimap_overlay_round); driver->removeTexture(data->minimap_overlay_round);
driver->removeTexture(data->minimap_overlay_square); driver->removeTexture(data->minimap_overlay_square);
delete data; delete data;
delete m_minimap_update_thread; delete m_minimap_update_thread;
} }
void Mapper::addBlock(v3s16 pos, MinimapMapblock *data) void Mapper::addBlock(v3s16 pos, MinimapMapblock *data)
{ {
m_minimap_update_thread->enqueue_Block(pos, data); m_minimap_update_thread->enqueueBlock(pos, data);
} }
MinimapMode Mapper::getMinimapMode() MinimapMode Mapper::getMinimapMode()
@ -271,121 +283,150 @@ MinimapMode Mapper::getMinimapMode()
void Mapper::toggleMinimapShape() void Mapper::toggleMinimapShape()
{ {
JMutexAutoLock lock(m_mutex);
data->minimap_shape_round = !data->minimap_shape_round; data->minimap_shape_round = !data->minimap_shape_round;
g_settings->setBool(("minimap_shape_round"), data->minimap_shape_round); g_settings->setBool("minimap_shape_round", data->minimap_shape_round);
m_minimap_update_thread->deferUpdate(); m_minimap_update_thread->deferUpdate();
} }
void Mapper::setMinimapMode(MinimapMode mode) void Mapper::setMinimapMode(MinimapMode mode)
{ {
static const u16 modeDefs[7 * 3] = { static const MinimapModeDef modedefs[MINIMAP_MODE_COUNT] = {
0, 0, 0, {false, 0, 0},
0, m_surface_mode_scan_height, 256, {false, m_surface_mode_scan_height, 256},
0, m_surface_mode_scan_height, 128, {false, m_surface_mode_scan_height, 128},
0, m_surface_mode_scan_height, 64, {false, m_surface_mode_scan_height, 64},
1, 32, 128, {true, 32, 128},
1, 32, 64, {true, 32, 64},
1, 32, 32}; {true, 32, 32}
};
if (mode >= MINIMAP_MODE_COUNT)
return;
JMutexAutoLock lock(m_mutex); JMutexAutoLock lock(m_mutex);
data->radar = (bool)modeDefs[(int)mode * 3];
data->scan_height = modeDefs[(int)mode * 3 + 1]; data->is_radar = modedefs[mode].is_radar;
data->map_size = modeDefs[(int)mode * 3 + 2]; data->scan_height = modedefs[mode].scan_height;
data->map_size = modedefs[mode].map_size;
data->mode = mode; data->mode = mode;
m_minimap_update_thread->deferUpdate(); m_minimap_update_thread->deferUpdate();
} }
void Mapper::setPos(v3s16 pos) void Mapper::setPos(v3s16 pos)
{
bool do_update = false;
{ {
JMutexAutoLock lock(m_mutex); JMutexAutoLock lock(m_mutex);
if (pos != data->old_pos) { if (pos != data->old_pos) {
data->old_pos = data->pos; data->old_pos = data->pos;
data->pos = pos; data->pos = pos;
do_update = true;
}
}
if (do_update)
m_minimap_update_thread->deferUpdate(); m_minimap_update_thread->deferUpdate();
} }
void Mapper::setAngle(f32 angle)
{
m_angle = angle;
}
void Mapper::blitMinimapPixelsToImageRadar(video::IImage *map_image)
{
for (s16 x = 0; x < data->map_size; x++)
for (s16 z = 0; z < data->map_size; z++) {
MinimapPixel *mmpixel = &data->minimap_scan[x + z * data->map_size];
video::SColor c(240, 0, 0, 0);
if (mmpixel->air_count > 0)
c.setGreen(core::clamp(core::round32(32 + mmpixel->air_count * 8), 0, 255));
map_image->setPixel(x, data->map_size - z - 1, c);
}
}
void Mapper::blitMinimapPixelsToImageSurface(
video::IImage *map_image, video::IImage *heightmap_image)
{
for (s16 x = 0; x < data->map_size; x++)
for (s16 z = 0; z < data->map_size; z++) {
MinimapPixel *mmpixel = &data->minimap_scan[x + z * data->map_size];
video::SColor c = m_ndef->get(mmpixel->id).minimap_color;
c.setAlpha(240);
map_image->setPixel(x, data->map_size - z - 1, c);
u32 h = mmpixel->height;
heightmap_image->setPixel(x,data->map_size - z - 1,
video::SColor(255, h, h, h));
}
} }
video::ITexture *Mapper::getMinimapTexture() video::ITexture *Mapper::getMinimapTexture()
{ {
// update minimap textures when new scan is ready // update minimap textures when new scan is ready
if (!data->map_invalidated) { if (data->map_invalidated)
// create minimap and heightmap image return data->texture;
// create minimap and heightmap images in memory
core::dimension2d<u32> dim(data->map_size, data->map_size); core::dimension2d<u32> dim(data->map_size, data->map_size);
video::IImage *map_image = driver->createImage(video::ECF_A8R8G8B8, dim); video::IImage *map_image = driver->createImage(video::ECF_A8R8G8B8, dim);
video::IImage *heightmap_image = driver->createImage(video::ECF_A8R8G8B8, dim); video::IImage *heightmap_image = driver->createImage(video::ECF_A8R8G8B8, dim);
video::IImage *minimap_image = driver->createImage(video::ECF_A8R8G8B8, video::IImage *minimap_image = driver->createImage(video::ECF_A8R8G8B8,
core::dimension2d<u32>(512, 512)); core::dimension2d<u32>(MINIMAP_MAX_SX, MINIMAP_MAX_SY));
video::SColor c; // Blit MinimapPixels to images
if (!data->radar) { if (data->is_radar)
// surface mode blitMinimapPixelsToImageRadar(map_image);
for (s16 x = 0; x < data->map_size; x++) { else
for (s16 z = 0; z < data->map_size; z++) { blitMinimapPixelsToImageSurface(map_image, heightmap_image);
MinimapPixel* minimap_pixel = &data->minimap_scan[x + z * data->map_size];
const ContentFeatures &f = client->getNodeDefManager()->get(minimap_pixel->id);
c = f.minimap_color;
c.setAlpha(240);
map_image->setPixel(x, data->map_size - z -1, c);
u32 h = minimap_pixel->height;
heightmap_image->setPixel(x,data->map_size -z -1,
video::SColor(255, h, h, h));
}
}
} else {
// radar mode
c = video::SColor (240, 0 , 0, 0);
for (s16 x = 0; x < data->map_size; x++) {
for (s16 z = 0; z < data->map_size; z++) {
MinimapPixel* minimap_pixel = &data->minimap_scan[x + z * data->map_size];
if (minimap_pixel->air_count > 0) {
c.setGreen(core::clamp(core::round32(32 + minimap_pixel->air_count * 8), 0, 255));
} else {
c.setGreen(0);
}
map_image->setPixel(x, data->map_size - z -1, c);
}
}
}
map_image->copyToScaling(minimap_image); map_image->copyToScaling(minimap_image);
map_image->drop(); map_image->drop();
video::IImage *minimap_mask; video::IImage *minimap_mask = data->minimap_shape_round ?
if (data->minimap_shape_round) { data->minimap_mask_round : data->minimap_mask_square;
minimap_mask = data->minimap_mask_round;
} else { if (minimap_mask) {
minimap_mask = data->minimap_mask_square; for (s16 y = 0; y < MINIMAP_MAX_SY; y++)
} for (s16 x = 0; x < MINIMAP_MAX_SX; x++) {
for (s16 x = 0; x < 512; x++) {
for (s16 y = 0; y < 512; y++) {
video::SColor mask_col = minimap_mask->getPixel(x, y); video::SColor mask_col = minimap_mask->getPixel(x, y);
if (!mask_col.getAlpha()) { if (!mask_col.getAlpha())
minimap_image->setPixel(x, y, video::SColor(0,0,0,0)); minimap_image->setPixel(x, y, video::SColor(0,0,0,0));
} }
} }
}
if (data->texture) { if (data->texture)
driver->removeTexture(data->texture); driver->removeTexture(data->texture);
} if (data->heightmap_texture)
if (data->heightmap_texture) {
driver->removeTexture(data->heightmap_texture); driver->removeTexture(data->heightmap_texture);
}
data->texture = driver->addTexture("minimap__", minimap_image); data->texture = driver->addTexture("minimap__", minimap_image);
data->heightmap_texture = driver->addTexture("minimap_heightmap__", heightmap_image); data->heightmap_texture =
driver->addTexture("minimap_heightmap__", heightmap_image);
minimap_image->drop(); minimap_image->drop();
heightmap_image->drop(); heightmap_image->drop();
data->map_invalidated = true; data->map_invalidated = true;
}
return data->texture; return data->texture;
} }
v3f Mapper::getYawVec() v3f Mapper::getYawVec()
{ {
if (data->minimap_shape_round) { if (data->minimap_shape_round) {
return v3f(cos(player->getYaw()* core::DEGTORAD), return v3f(
sin(player->getYaw()* core::DEGTORAD), 1.0); cos(m_angle * core::DEGTORAD),
sin(m_angle * core::DEGTORAD),
1.0);
} else { } else {
return v3f(1.0, 0.0, 1.0); return v3f(1.0, 0.0, 1.0);
} }
@ -415,59 +456,99 @@ scene::SMeshBuffer *Mapper::getMinimapMeshBuffer()
void Mapper::drawMinimap() void Mapper::drawMinimap()
{ {
v2u32 screensize = porting::getWindowSize();
u32 size = 0.25 * screensize.Y;
video::ITexture *minimap_texture = getMinimapTexture(); video::ITexture *minimap_texture = getMinimapTexture();
core::matrix4 matrix; if (!minimap_texture)
return;
v2u32 screensize = porting::getWindowSize();
const u32 size = 0.25 * screensize.Y;
core::rect<s32> oldViewPort = driver->getViewPort(); core::rect<s32> oldViewPort = driver->getViewPort();
driver->setViewPort(core::rect<s32>(screensize.X - size - 10, 10,
screensize.X - 10, size + 10));
core::matrix4 oldProjMat = driver->getTransform(video::ETS_PROJECTION); core::matrix4 oldProjMat = driver->getTransform(video::ETS_PROJECTION);
driver->setTransform(video::ETS_PROJECTION, core::matrix4());
core::matrix4 oldViewMat = driver->getTransform(video::ETS_VIEW); core::matrix4 oldViewMat = driver->getTransform(video::ETS_VIEW);
driver->setViewPort(core::rect<s32>(
screensize.X - size - 10, 10,
screensize.X - 10, size + 10));
driver->setTransform(video::ETS_PROJECTION, core::matrix4());
driver->setTransform(video::ETS_VIEW, core::matrix4()); driver->setTransform(video::ETS_VIEW, core::matrix4());
core::matrix4 matrix;
matrix.makeIdentity(); matrix.makeIdentity();
if (minimap_texture) {
video::SMaterial &material = m_meshbuffer->getMaterial(); video::SMaterial &material = m_meshbuffer->getMaterial();
material.setFlag(video::EMF_TRILINEAR_FILTER, true); material.setFlag(video::EMF_TRILINEAR_FILTER, true);
material.Lighting = false; material.Lighting = false;
material.TextureLayer[0].Texture = minimap_texture; material.TextureLayer[0].Texture = minimap_texture;
material.TextureLayer[1].Texture = data->heightmap_texture; material.TextureLayer[1].Texture = data->heightmap_texture;
if (m_enable_shaders && !data->radar) {
u16 sid = shdrsrc->getShader("minimap_shader", 1, 1); if (m_enable_shaders && !data->is_radar) {
material.MaterialType = shdrsrc->getShaderInfo(sid).material; u16 sid = m_shdrsrc->getShader("minimap_shader", 1, 1);
material.MaterialType = m_shdrsrc->getShaderInfo(sid).material;
} else { } else {
material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
} }
if (data->minimap_shape_round) if (data->minimap_shape_round)
matrix.setRotationDegrees(core::vector3df(0, 0, 360 - player->getYaw())); matrix.setRotationDegrees(core::vector3df(0, 0, 360 - m_angle));
// Draw minimap
driver->setTransform(video::ETS_WORLD, matrix); driver->setTransform(video::ETS_WORLD, matrix);
driver->setMaterial(material); driver->setMaterial(material);
driver->drawMeshBuffer(m_meshbuffer); driver->drawMeshBuffer(m_meshbuffer);
video::ITexture *minimap_overlay;
if (data->minimap_shape_round) { // Draw overlay
minimap_overlay = data->minimap_overlay_round; video::ITexture *minimap_overlay = data->minimap_shape_round ?
} else { data->minimap_overlay_round : data->minimap_overlay_square;
minimap_overlay = data->minimap_overlay_square;
}
material.TextureLayer[0].Texture = minimap_overlay; material.TextureLayer[0].Texture = minimap_overlay;
material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
driver->setMaterial(material); driver->setMaterial(material);
driver->drawMeshBuffer(m_meshbuffer); driver->drawMeshBuffer(m_meshbuffer);
// If round minimap, draw player marker
if (!data->minimap_shape_round) { if (!data->minimap_shape_round) {
matrix.setRotationDegrees(core::vector3df(0, 0, player->getYaw())); matrix.setRotationDegrees(core::vector3df(0, 0, m_angle));
driver->setTransform(video::ETS_WORLD, matrix);
material.TextureLayer[0].Texture = data->player_marker; material.TextureLayer[0].Texture = data->player_marker;
driver->setTransform(video::ETS_WORLD, matrix);
driver->setMaterial(material); driver->setMaterial(material);
driver->drawMeshBuffer(m_meshbuffer); driver->drawMeshBuffer(m_meshbuffer);
} }
}
// Reset transformations
driver->setTransform(video::ETS_VIEW, oldViewMat); driver->setTransform(video::ETS_VIEW, oldViewMat);
driver->setTransform(video::ETS_PROJECTION, oldProjMat); driver->setTransform(video::ETS_PROJECTION, oldProjMat);
driver->setViewPort(oldViewPort); driver->setViewPort(oldViewPort);
} }
////
//// MinimapMapblock
////
void MinimapMapblock::getMinimapNodes(VoxelManipulator *vmanip, v3s16 pos)
{
for (s16 x = 0; x < MAP_BLOCKSIZE; x++)
for (s16 z = 0; z < MAP_BLOCKSIZE; z++) {
s16 air_count = 0;
bool surface_found = false;
MinimapPixel *mmpixel = &data[z * MAP_BLOCKSIZE + x];
for (s16 y = MAP_BLOCKSIZE -1; y >= 0; y--) {
v3s16 p(x, y, z);
MapNode n = vmanip->getNodeNoEx(pos + p);
if (!surface_found && n.getContent() != CONTENT_AIR) {
mmpixel->height = y;
mmpixel->id = n.getContent();
surface_found = true;
} else if (n.getContent() == CONTENT_AIR) {
air_count++;
}
}
if (!surface_found)
mmpixel->id = CONTENT_AIR;
mmpixel->air_count = air_count;
}
}

View File

@ -20,14 +20,17 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#ifndef MINIMAP_HEADER #ifndef MINIMAP_HEADER
#define MINIMAP_HEADER #define MINIMAP_HEADER
#include <map>
#include <string>
#include <vector>
#include "irrlichttypes_extrabloated.h" #include "irrlichttypes_extrabloated.h"
#include "client.h" #include "client.h"
#include "voxel.h" #include "voxel.h"
#include "jthread/jmutex.h" #include "jthread/jmutex.h"
#include "jthread/jsemaphore.h" #include "jthread/jsemaphore.h"
#include <map>
#include <string> #define MINIMAP_MAX_SX 512
#include <vector> #define MINIMAP_MAX_SY 512
enum MinimapMode { enum MinimapMode {
MINIMAP_MODE_OFF, MINIMAP_MODE_OFF,
@ -36,31 +39,37 @@ enum MinimapMode {
MINIMAP_MODE_SURFACEx4, MINIMAP_MODE_SURFACEx4,
MINIMAP_MODE_RADARx1, MINIMAP_MODE_RADARx1,
MINIMAP_MODE_RADARx2, MINIMAP_MODE_RADARx2,
MINIMAP_MODE_RADARx4 MINIMAP_MODE_RADARx4,
MINIMAP_MODE_COUNT,
}; };
struct MinimapPixel struct MinimapModeDef {
{ bool is_radar;
u16 scan_height;
u16 map_size;
};
struct MinimapPixel {
u16 id; u16 id;
u16 height; u16 height;
u16 air_count; u16 air_count;
u16 light; u16 light;
}; };
struct MinimapMapblock struct MinimapMapblock {
{ void getMinimapNodes(VoxelManipulator *vmanip, v3s16 pos);
MinimapPixel data[MAP_BLOCKSIZE * MAP_BLOCKSIZE]; MinimapPixel data[MAP_BLOCKSIZE * MAP_BLOCKSIZE];
}; };
struct MinimapData struct MinimapData {
{ bool is_radar;
bool radar;
MinimapMode mode; MinimapMode mode;
v3s16 pos; v3s16 pos;
v3s16 old_pos; v3s16 old_pos;
u16 scan_height; u16 scan_height;
u16 map_size; u16 map_size;
MinimapPixel minimap_scan[512 * 512]; MinimapPixel minimap_scan[MINIMAP_MAX_SX * MINIMAP_MAX_SY];
bool map_invalidated; bool map_invalidated;
bool minimap_shape_round; bool minimap_shape_round;
video::IImage *minimap_image; video::IImage *minimap_image;
@ -74,104 +83,79 @@ struct MinimapData
video::ITexture *player_marker; video::ITexture *player_marker;
}; };
struct QueuedMinimapUpdate struct QueuedMinimapUpdate {
{
v3s16 pos; v3s16 pos;
MinimapMapblock *data; MinimapMapblock *data;
QueuedMinimapUpdate();
~QueuedMinimapUpdate();
}; };
/* class MinimapUpdateThread : public UpdateThread {
A thread-safe queue of minimap mapblocks update tasks
*/
class MinimapUpdateQueue
{
public: public:
MinimapUpdateQueue(); virtual ~MinimapUpdateThread();
~MinimapUpdateQueue();
bool addBlock(v3s16 pos, MinimapMapblock *data);
QueuedMinimapUpdate *pop();
u32 size()
{
JMutexAutoLock lock(m_mutex);
return m_queue.size();
}
private:
std::list<QueuedMinimapUpdate*> m_queue;
JMutex m_mutex;
};
class MinimapUpdateThread : public UpdateThread
{
private:
MinimapUpdateQueue m_queue;
std::map<v3s16, MinimapMapblock *> m_blocks_cache;
protected:
const char *getName()
{ return "MinimapUpdateThread"; }
virtual void doUpdate();
public:
MinimapUpdateThread(IrrlichtDevice *device, Client *client)
{
this->device = device;
this->client = client;
this->driver = device->getVideoDriver();
this->tsrc = client->getTextureSource();
}
~MinimapUpdateThread();
void getMap(v3s16 pos, s16 size, s16 height, bool radar); void getMap(v3s16 pos, s16 size, s16 height, bool radar);
MinimapPixel *getMinimapPixel (v3s16 pos, s16 height, s16 &pixel_height); MinimapPixel *getMinimapPixel(v3s16 pos, s16 height, s16 *pixel_height);
s16 getAirCount(v3s16 pos, s16 height); s16 getAirCount(v3s16 pos, s16 height);
video::SColor getColorFromId(u16 id); video::SColor getColorFromId(u16 id);
void enqueue_Block(v3s16 pos, MinimapMapblock *data); void enqueueBlock(v3s16 pos, MinimapMapblock *data);
IrrlichtDevice *device;
Client *client; bool pushBlockUpdate(v3s16 pos, MinimapMapblock *data);
video::IVideoDriver *driver; bool popBlockUpdate(QueuedMinimapUpdate *update);
ITextureSource *tsrc;
MinimapData *data; MinimapData *data;
protected:
const char *getName()
{
return "MinimapUpdateThread";
}
virtual void doUpdate();
private:
JMutex m_queue_mutex;
std::deque<QueuedMinimapUpdate> m_update_queue;
std::map<v3s16, MinimapMapblock *> m_blocks_cache;
}; };
class Mapper class Mapper {
{
private:
MinimapUpdateThread *m_minimap_update_thread;
video::ITexture *minimap_texture;
scene::SMeshBuffer *m_meshbuffer;
bool m_enable_shaders;
u16 m_surface_mode_scan_height;
JMutex m_mutex;
public: public:
Mapper(IrrlichtDevice *device, Client *client); Mapper(IrrlichtDevice *device, Client *client);
~Mapper(); ~Mapper();
void addBlock(v3s16 pos, MinimapMapblock *data); void addBlock(v3s16 pos, MinimapMapblock *data);
void setPos(v3s16 pos);
video::ITexture* getMinimapTexture();
v3f getYawVec(); v3f getYawVec();
MinimapMode getMinimapMode(); MinimapMode getMinimapMode();
void setPos(v3s16 pos);
void setAngle(f32 angle);
void setMinimapMode(MinimapMode mode); void setMinimapMode(MinimapMode mode);
void toggleMinimapShape(); void toggleMinimapShape();
video::ITexture *getMinimapTexture();
void blitMinimapPixelsToImageRadar(video::IImage *map_image);
void blitMinimapPixelsToImageSurface(video::IImage *map_image,
video::IImage *heightmap_image);
scene::SMeshBuffer *getMinimapMeshBuffer(); scene::SMeshBuffer *getMinimapMeshBuffer();
void drawMinimap(); void drawMinimap();
IrrlichtDevice *device;
Client *client;
video::IVideoDriver *driver; video::IVideoDriver *driver;
LocalPlayer *player;
ITextureSource *tsrc;
IShaderSource *shdrsrc;
MinimapData *data; MinimapData *data;
private:
ITextureSource *m_tsrc;
IShaderSource *m_shdrsrc;
INodeDefManager *m_ndef;
MinimapUpdateThread *m_minimap_update_thread;
scene::SMeshBuffer *m_meshbuffer;
bool m_enable_shaders;
u16 m_surface_mode_scan_height;
f32 m_angle;
JMutex m_mutex;
}; };
#endif #endif