Skip to content

Commit

Permalink
Minimap: Optimise
Browse files Browse the repository at this point in the history
  • Loading branch information
numberZero authored and paramat committed Mar 11, 2017
1 parent ab371cc commit 25a24c0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 80 deletions.
122 changes: 48 additions & 74 deletions src/minimap.cpp
Expand Up @@ -120,89 +120,63 @@ void MinimapUpdateThread::doUpdate()
} }


if (data->map_invalidated && data->mode != MINIMAP_MODE_OFF) { if (data->map_invalidated && data->mode != MINIMAP_MODE_OFF) {
getMap(data->pos, data->map_size, data->scan_height, data->is_radar); getMap(data->pos, data->map_size, data->scan_height);
data->map_invalidated = false; data->map_invalidated = false;
} }
} }


MinimapPixel *MinimapUpdateThread::getMinimapPixel(v3s16 pos, void MinimapUpdateThread::getMap(v3s16 pos, s16 size, s16 height)
s16 scan_height, s16 *pixel_height)
{ {
s16 height = scan_height - MAP_BLOCKSIZE; v3s16 region(size, 0, size);
v3s16 blockpos_max, blockpos_min, relpos; v3s16 pos_min(pos.X - size / 2, pos.Y - height / 2, pos.Z - size / 2);

v3s16 pos_max(pos_min.X + size - 1, pos.Y + height / 2, pos_min.Z + size - 1);
getNodeBlockPosWithOffset( v3s16 blockpos_min = getNodeBlockPos(pos_min);
v3s16(pos.X, pos.Y - scan_height / 2, pos.Z), v3s16 blockpos_max = getNodeBlockPos(pos_max);
blockpos_min, relpos);
getNodeBlockPosWithOffset( // clear the map
v3s16(pos.X, pos.Y + scan_height / 2, pos.Z), for (int z = 0; z < size; z++)
blockpos_max, relpos); for (int x = 0; x < size; x++) {

MinimapPixel &mmpixel = data->minimap_scan[x + z * size];
for (s16 i = blockpos_max.Y; i > blockpos_min.Y - 1; i--) { mmpixel.air_count = 0;
std::map<v3s16, MinimapMapblock *>::iterator it = mmpixel.height = 0;
m_blocks_cache.find(v3s16(blockpos_max.X, i, blockpos_max.Z)); mmpixel.n = MapNode(CONTENT_AIR);
if (it != m_blocks_cache.end()) {
MinimapMapblock *mmblock = it->second;
MinimapPixel *pixel = &mmblock->data[relpos.Z * MAP_BLOCKSIZE + relpos.X];
if (pixel->n.param0 != CONTENT_AIR) {
*pixel_height = height + pixel->height;
return pixel;
}
}

height -= MAP_BLOCKSIZE;
} }


return NULL; // draw the map
} v3s16 blockpos;

for (blockpos.Z = blockpos_min.Z; blockpos.Z <= blockpos_max.Z; ++blockpos.Z)
s16 MinimapUpdateThread::getAirCount(v3s16 pos, s16 height) for (blockpos.Y = blockpos_min.Y; blockpos.Y <= blockpos_max.Y; ++blockpos.Y)
{ for (blockpos.X = blockpos_min.X; blockpos.X <= blockpos_max.X; ++blockpos.X) {
s16 air_count = 0; std::map<v3s16, MinimapMapblock *>::const_iterator pblock =
v3s16 blockpos_max, blockpos_min, relpos; m_blocks_cache.find(blockpos);

if (pblock == m_blocks_cache.end())
getNodeBlockPosWithOffset( continue;
v3s16(pos.X, pos.Y - height / 2, pos.Z), const MinimapMapblock &block = *pblock->second;
blockpos_min, relpos);
getNodeBlockPosWithOffset( v3s16 block_node_min(blockpos * MAP_BLOCKSIZE);
v3s16(pos.X, pos.Y + height / 2, pos.Z), v3s16 block_node_max(block_node_min + MAP_BLOCKSIZE - 1);
blockpos_max, relpos); // clip

v3s16 range_min = componentwise_max(block_node_min, pos_min);
for (s16 i = blockpos_max.Y; i > blockpos_min.Y - 1; i--) { v3s16 range_max = componentwise_min(block_node_max, pos_max);
std::map<v3s16, MinimapMapblock *>::iterator it =
m_blocks_cache.find(v3s16(blockpos_max.X, i, blockpos_max.Z)); v3s16 pos;
if (it != m_blocks_cache.end()) { pos.Y = range_min.Y;
MinimapMapblock *mmblock = it->second; for (pos.Z = range_min.Z; pos.Z <= range_max.Z; ++pos.Z)
MinimapPixel *pixel = &mmblock->data[relpos.Z * MAP_BLOCKSIZE + relpos.X]; for (pos.X = range_min.X; pos.X <= range_max.X; ++pos.X) {
air_count += pixel->air_count; v3s16 inblock_pos = pos - block_node_min;
} const MinimapPixel &in_pixel =
} block.data[inblock_pos.Z * MAP_BLOCKSIZE + inblock_pos.X];


return air_count; v3s16 inmap_pos = pos - pos_min;
} MinimapPixel &out_pixel =

data->minimap_scan[inmap_pos.X + inmap_pos.Z * size];
void MinimapUpdateThread::getMap(v3s16 pos, s16 size, s16 height, bool is_radar)
{ out_pixel.air_count += in_pixel.air_count;
v3s16 p = v3s16(pos.X - size / 2, pos.Y, pos.Z - size / 2); if (in_pixel.n.param0 != CONTENT_AIR) {

out_pixel.n = in_pixel.n;
for (s16 x = 0; x < size; x++) out_pixel.height = inmap_pos.Y + in_pixel.height;
for (s16 z = 0; z < size; z++) {
MapNode n(CONTENT_AIR);
MinimapPixel *mmpixel = &data->minimap_scan[x + z * size];

if (!is_radar) {
s16 pixel_height = 0;
MinimapPixel *cached_pixel =
getMinimapPixel(v3s16(p.X + x, p.Y, p.Z + z), height, &pixel_height);
if (cached_pixel) {
n = cached_pixel->n;
mmpixel->height = pixel_height;
} }
} else {
mmpixel->air_count = getAirCount(v3s16(p.X + x, p.Y, p.Z + z), height);
} }

mmpixel->n = n;
} }
} }


Expand Down
7 changes: 1 addition & 6 deletions src/minimap.h
Expand Up @@ -96,13 +96,8 @@ class MinimapUpdateThread : public UpdateThread {
MinimapUpdateThread() : UpdateThread("Minimap") {} MinimapUpdateThread() : UpdateThread("Minimap") {}
virtual ~MinimapUpdateThread(); virtual ~MinimapUpdateThread();


void getMap(v3s16 pos, s16 size, s16 height, bool radar); void getMap(v3s16 pos, s16 size, s16 height);
MinimapPixel *getMinimapPixel(v3s16 pos, s16 height, s16 *pixel_height);
s16 getAirCount(v3s16 pos, s16 height);
video::SColor getColorFromId(u16 id);

void enqueueBlock(v3s16 pos, MinimapMapblock *data); void enqueueBlock(v3s16 pos, MinimapMapblock *data);

bool pushBlockUpdate(v3s16 pos, MinimapMapblock *data); bool pushBlockUpdate(v3s16 pos, MinimapMapblock *data);
bool popBlockUpdate(QueuedMinimapUpdate *update); bool popBlockUpdate(QueuedMinimapUpdate *update);


Expand Down
10 changes: 10 additions & 0 deletions src/util/numeric.h
Expand Up @@ -149,6 +149,16 @@ inline void sortBoxVerticies(v3s16 &p1, v3s16 &p2) {
SWAP(s16, p1.Z, p2.Z); SWAP(s16, p1.Z, p2.Z);
} }


inline v3s16 componentwise_min(const v3s16 &a, const v3s16 &b)
{
return v3s16(MYMIN(a.X, b.X), MYMIN(a.Y, b.Y), MYMIN(a.Z, b.Z));
}

inline v3s16 componentwise_max(const v3s16 &a, const v3s16 &b)
{
return v3s16(MYMAX(a.X, b.X), MYMAX(a.Y, b.Y), MYMAX(a.Z, b.Z));
}



/** Returns \p f wrapped to the range [-360, 360] /** Returns \p f wrapped to the range [-360, 360]
* *
Expand Down

0 comments on commit 25a24c0

Please sign in to comment.