Skip to content

Commit e234d8b

Browse files
committedJul 8, 2015
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
1 parent ba15c98 commit e234d8b

File tree

5 files changed

+429
-377
lines changed

5 files changed

+429
-377
lines changed
 

‎src/client.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -525,18 +525,23 @@ void Client::step(float dtime)
525525
while (!m_mesh_update_thread.m_queue_out.empty())
526526
{
527527
num_processed_meshes++;
528+
529+
MinimapMapblock *minimap_mapblock = NULL;
530+
bool do_mapper_update = true;
531+
528532
MeshUpdateResult r = m_mesh_update_thread.m_queue_out.pop_frontNoEx();
529533
MapBlock *block = m_env.getMap().getBlockNoCreateNoEx(r.p);
530-
MinimapMapblock *minimap_mapblock = NULL;
531534
if (block) {
532535
// Delete the old mesh
533-
if (block->mesh != NULL) {
536+
if (block->mesh != NULL) {
534537
delete block->mesh;
535538
block->mesh = NULL;
536539
}
537540

538-
if (r.mesh)
541+
if (r.mesh) {
539542
minimap_mapblock = r.mesh->getMinimapMapblock();
543+
do_mapper_update = (minimap_mapblock != NULL);
544+
}
540545

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

553-
m_mapper->addBlock(r.p, minimap_mapblock);
558+
if (do_mapper_update)
559+
m_mapper->addBlock(r.p, minimap_mapblock);
554560

555561
if (r.ack_block_to_server) {
556562
/*

‎src/game.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -4045,10 +4045,11 @@ void Game::updateFrame(std::vector<aabb3f> &highlight_boxes,
40454045
}
40464046

40474047
/*
4048-
Update minimap pos
4048+
Update minimap pos and rotation
40494049
*/
40504050
if (flags.show_minimap && flags.show_hud) {
40514051
mapper->setPos(floatToInt(player->getPosition(), BS));
4052+
mapper->setAngle(player->getYaw());
40524053
}
40534054

40544055
/*

‎src/mapblock_mesh.cpp

+6-26
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ static void updateAllFastFaceRows(MeshMakeData *data,
10291029

10301030
MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
10311031
m_mesh(new scene::SMesh()),
1032-
m_minimap_mapblock(new MinimapMapblock),
1032+
m_minimap_mapblock(NULL),
10331033
m_gamedef(data->m_gamedef),
10341034
m_tsrc(m_gamedef->getTextureSource()),
10351035
m_shdrsrc(m_gamedef->getShaderSource()),
@@ -1044,29 +1044,9 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset):
10441044
m_enable_highlighting = g_settings->getBool("enable_node_highlighting");
10451045

10461046
if (g_settings->getBool("enable_minimap")) {
1047-
v3s16 blockpos_nodes = data->m_blockpos * MAP_BLOCKSIZE;
1048-
for(s16 x = 0; x < MAP_BLOCKSIZE; x++) {
1049-
for(s16 z = 0; z < MAP_BLOCKSIZE; z++) {
1050-
s16 air_count = 0;
1051-
bool surface_found = false;
1052-
MinimapPixel* minimap_pixel = &m_minimap_mapblock->data[x + z * MAP_BLOCKSIZE];
1053-
for(s16 y = MAP_BLOCKSIZE -1; y > -1; y--) {
1054-
v3s16 p(x, y, z);
1055-
MapNode n = data->m_vmanip.getNodeNoEx(blockpos_nodes + p);
1056-
if (!surface_found && n.getContent() != CONTENT_AIR) {
1057-
minimap_pixel->height = y;
1058-
minimap_pixel->id = n.getContent();
1059-
surface_found = true;
1060-
} else if (n.getContent() == CONTENT_AIR) {
1061-
air_count++;
1062-
}
1063-
}
1064-
if (!surface_found) {
1065-
minimap_pixel->id = CONTENT_AIR;
1066-
}
1067-
minimap_pixel->air_count = air_count;
1068-
}
1069-
}
1047+
m_minimap_mapblock = new MinimapMapblock;
1048+
m_minimap_mapblock->getMinimapNodes(
1049+
&data->m_vmanip, data->m_blockpos * MAP_BLOCKSIZE);
10701050
}
10711051

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

11851165
if(m_enable_highlighting && p.tile.material_flags & MATERIAL_FLAG_HIGHLIGHTED)
1186-
m_highlighted_materials.push_back(i);
1166+
m_highlighted_materials.push_back(i);
11871167

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

0 commit comments

Comments
 (0)
Please sign in to comment.