Skip to content
Permalink
Browse files

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
  • Loading branch information
kwolekr committed Jul 8, 2015
1 parent ba15c98 commit e234d8b3786d5adfbdf3d1a2123c3e8d91c21880
Showing with 429 additions and 377 deletions.
  1. +10 −4 src/client.cpp
  2. +2 −1 src/game.cpp
  3. +6 −26 src/mapblock_mesh.cpp
  4. +348 −267 src/minimap.cpp
  5. +63 −79 src/minimap.h
@@ -525,18 +525,23 @@ void Client::step(float dtime)
while (!m_mesh_update_thread.m_queue_out.empty())
{
num_processed_meshes++;

MinimapMapblock *minimap_mapblock = NULL;
bool do_mapper_update = true;

MeshUpdateResult r = m_mesh_update_thread.m_queue_out.pop_frontNoEx();
MapBlock *block = m_env.getMap().getBlockNoCreateNoEx(r.p);
MinimapMapblock *minimap_mapblock = NULL;
if (block) {
// Delete the old mesh
if (block->mesh != NULL) {
if (block->mesh != NULL) {
delete block->mesh;
block->mesh = NULL;
}

if (r.mesh)
if (r.mesh) {
minimap_mapblock = r.mesh->getMinimapMapblock();
do_mapper_update = (minimap_mapblock != NULL);
}

if (r.mesh && r.mesh->getMesh()->getMeshBufferCount() == 0) {
delete r.mesh;
@@ -550,7 +555,8 @@ void Client::step(float dtime)
minimap_mapblock = NULL;
}

m_mapper->addBlock(r.p, minimap_mapblock);
if (do_mapper_update)
m_mapper->addBlock(r.p, minimap_mapblock);

if (r.ack_block_to_server) {
/*
@@ -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) {
mapper->setPos(floatToInt(player->getPosition(), BS));
mapper->setAngle(player->getYaw());
}

/*
@@ -1029,7 +1029,7 @@ static void updateAllFastFaceRows(MeshMakeData *data,

MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
m_mesh(new scene::SMesh()),
m_minimap_mapblock(new MinimapMapblock),
m_minimap_mapblock(NULL),
m_gamedef(data->m_gamedef),
m_tsrc(m_gamedef->getTextureSource()),
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");

if (g_settings->getBool("enable_minimap")) {
v3s16 blockpos_nodes = data->m_blockpos * MAP_BLOCKSIZE;
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* 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;
}
}
m_minimap_mapblock = new MinimapMapblock;
m_minimap_mapblock->getMinimapNodes(
&data->m_vmanip, data->m_blockpos * MAP_BLOCKSIZE);
}

// 4-21ms for MAP_BLOCKSIZE=16 (NOTE: probably outdated)
@@ -1183,7 +1163,7 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
}

if(m_enable_highlighting && p.tile.material_flags & MATERIAL_FLAG_HIGHLIGHTED)
m_highlighted_materials.push_back(i);
m_highlighted_materials.push_back(i);

for(u32 j = 0; j < p.vertices.size(); j++)
{
@@ -1400,7 +1380,7 @@ bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_rat
// Node highlighting
if (m_enable_highlighting) {
u8 day = m_highlight_mesh_color.getRed();
u8 night = m_highlight_mesh_color.getGreen();
u8 night = m_highlight_mesh_color.getGreen();
video::SColor hc;
finalColorBlend(hc, day, night, daynight_ratio);
float sin_r = 0.07 * sin(1.5 * time);

0 comments on commit e234d8b

Please sign in to comment.