Skip to content

Commit 25a24c0

Browse files
numberZeroparamat
authored andcommittedMar 11, 2017
Minimap: Optimise
1 parent ab371cc commit 25a24c0

File tree

3 files changed

+59
-80
lines changed

3 files changed

+59
-80
lines changed
 

‎src/minimap.cpp

+48-74
Original file line numberDiff line numberDiff line change
@@ -120,89 +120,63 @@ void MinimapUpdateThread::doUpdate()
120120
}
121121

122122
if (data->map_invalidated && data->mode != MINIMAP_MODE_OFF) {
123-
getMap(data->pos, data->map_size, data->scan_height, data->is_radar);
123+
getMap(data->pos, data->map_size, data->scan_height);
124124
data->map_invalidated = false;
125125
}
126126
}
127127

128-
MinimapPixel *MinimapUpdateThread::getMinimapPixel(v3s16 pos,
129-
s16 scan_height, s16 *pixel_height)
128+
void MinimapUpdateThread::getMap(v3s16 pos, s16 size, s16 height)
130129
{
131-
s16 height = scan_height - MAP_BLOCKSIZE;
132-
v3s16 blockpos_max, blockpos_min, relpos;
133-
134-
getNodeBlockPosWithOffset(
135-
v3s16(pos.X, pos.Y - scan_height / 2, pos.Z),
136-
blockpos_min, relpos);
137-
getNodeBlockPosWithOffset(
138-
v3s16(pos.X, pos.Y + scan_height / 2, pos.Z),
139-
blockpos_max, relpos);
140-
141-
for (s16 i = blockpos_max.Y; i > blockpos_min.Y - 1; i--) {
142-
std::map<v3s16, MinimapMapblock *>::iterator it =
143-
m_blocks_cache.find(v3s16(blockpos_max.X, i, blockpos_max.Z));
144-
if (it != m_blocks_cache.end()) {
145-
MinimapMapblock *mmblock = it->second;
146-
MinimapPixel *pixel = &mmblock->data[relpos.Z * MAP_BLOCKSIZE + relpos.X];
147-
if (pixel->n.param0 != CONTENT_AIR) {
148-
*pixel_height = height + pixel->height;
149-
return pixel;
150-
}
151-
}
152-
153-
height -= MAP_BLOCKSIZE;
130+
v3s16 region(size, 0, size);
131+
v3s16 pos_min(pos.X - size / 2, pos.Y - height / 2, pos.Z - size / 2);
132+
v3s16 pos_max(pos_min.X + size - 1, pos.Y + height / 2, pos_min.Z + size - 1);
133+
v3s16 blockpos_min = getNodeBlockPos(pos_min);
134+
v3s16 blockpos_max = getNodeBlockPos(pos_max);
135+
136+
// clear the map
137+
for (int z = 0; z < size; z++)
138+
for (int x = 0; x < size; x++) {
139+
MinimapPixel &mmpixel = data->minimap_scan[x + z * size];
140+
mmpixel.air_count = 0;
141+
mmpixel.height = 0;
142+
mmpixel.n = MapNode(CONTENT_AIR);
154143
}
155144

156-
return NULL;
157-
}
158-
159-
s16 MinimapUpdateThread::getAirCount(v3s16 pos, s16 height)
160-
{
161-
s16 air_count = 0;
162-
v3s16 blockpos_max, blockpos_min, relpos;
163-
164-
getNodeBlockPosWithOffset(
165-
v3s16(pos.X, pos.Y - height / 2, pos.Z),
166-
blockpos_min, relpos);
167-
getNodeBlockPosWithOffset(
168-
v3s16(pos.X, pos.Y + height / 2, pos.Z),
169-
blockpos_max, relpos);
170-
171-
for (s16 i = blockpos_max.Y; i > blockpos_min.Y - 1; i--) {
172-
std::map<v3s16, MinimapMapblock *>::iterator it =
173-
m_blocks_cache.find(v3s16(blockpos_max.X, i, blockpos_max.Z));
174-
if (it != m_blocks_cache.end()) {
175-
MinimapMapblock *mmblock = it->second;
176-
MinimapPixel *pixel = &mmblock->data[relpos.Z * MAP_BLOCKSIZE + relpos.X];
177-
air_count += pixel->air_count;
178-
}
179-
}
180-
181-
return air_count;
182-
}
183-
184-
void MinimapUpdateThread::getMap(v3s16 pos, s16 size, s16 height, bool is_radar)
185-
{
186-
v3s16 p = v3s16(pos.X - size / 2, pos.Y, pos.Z - size / 2);
187-
188-
for (s16 x = 0; x < size; x++)
189-
for (s16 z = 0; z < size; z++) {
190-
MapNode n(CONTENT_AIR);
191-
MinimapPixel *mmpixel = &data->minimap_scan[x + z * size];
192-
193-
if (!is_radar) {
194-
s16 pixel_height = 0;
195-
MinimapPixel *cached_pixel =
196-
getMinimapPixel(v3s16(p.X + x, p.Y, p.Z + z), height, &pixel_height);
197-
if (cached_pixel) {
198-
n = cached_pixel->n;
199-
mmpixel->height = pixel_height;
145+
// draw the map
146+
v3s16 blockpos;
147+
for (blockpos.Z = blockpos_min.Z; blockpos.Z <= blockpos_max.Z; ++blockpos.Z)
148+
for (blockpos.Y = blockpos_min.Y; blockpos.Y <= blockpos_max.Y; ++blockpos.Y)
149+
for (blockpos.X = blockpos_min.X; blockpos.X <= blockpos_max.X; ++blockpos.X) {
150+
std::map<v3s16, MinimapMapblock *>::const_iterator pblock =
151+
m_blocks_cache.find(blockpos);
152+
if (pblock == m_blocks_cache.end())
153+
continue;
154+
const MinimapMapblock &block = *pblock->second;
155+
156+
v3s16 block_node_min(blockpos * MAP_BLOCKSIZE);
157+
v3s16 block_node_max(block_node_min + MAP_BLOCKSIZE - 1);
158+
// clip
159+
v3s16 range_min = componentwise_max(block_node_min, pos_min);
160+
v3s16 range_max = componentwise_min(block_node_max, pos_max);
161+
162+
v3s16 pos;
163+
pos.Y = range_min.Y;
164+
for (pos.Z = range_min.Z; pos.Z <= range_max.Z; ++pos.Z)
165+
for (pos.X = range_min.X; pos.X <= range_max.X; ++pos.X) {
166+
v3s16 inblock_pos = pos - block_node_min;
167+
const MinimapPixel &in_pixel =
168+
block.data[inblock_pos.Z * MAP_BLOCKSIZE + inblock_pos.X];
169+
170+
v3s16 inmap_pos = pos - pos_min;
171+
MinimapPixel &out_pixel =
172+
data->minimap_scan[inmap_pos.X + inmap_pos.Z * size];
173+
174+
out_pixel.air_count += in_pixel.air_count;
175+
if (in_pixel.n.param0 != CONTENT_AIR) {
176+
out_pixel.n = in_pixel.n;
177+
out_pixel.height = inmap_pos.Y + in_pixel.height;
200178
}
201-
} else {
202-
mmpixel->air_count = getAirCount(v3s16(p.X + x, p.Y, p.Z + z), height);
203179
}
204-
205-
mmpixel->n = n;
206180
}
207181
}
208182

‎src/minimap.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,8 @@ class MinimapUpdateThread : public UpdateThread {
9696
MinimapUpdateThread() : UpdateThread("Minimap") {}
9797
virtual ~MinimapUpdateThread();
9898

99-
void getMap(v3s16 pos, s16 size, s16 height, bool radar);
100-
MinimapPixel *getMinimapPixel(v3s16 pos, s16 height, s16 *pixel_height);
101-
s16 getAirCount(v3s16 pos, s16 height);
102-
video::SColor getColorFromId(u16 id);
103-
99+
void getMap(v3s16 pos, s16 size, s16 height);
104100
void enqueueBlock(v3s16 pos, MinimapMapblock *data);
105-
106101
bool pushBlockUpdate(v3s16 pos, MinimapMapblock *data);
107102
bool popBlockUpdate(QueuedMinimapUpdate *update);
108103

‎src/util/numeric.h

+10
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ inline void sortBoxVerticies(v3s16 &p1, v3s16 &p2) {
149149
SWAP(s16, p1.Z, p2.Z);
150150
}
151151

152+
inline v3s16 componentwise_min(const v3s16 &a, const v3s16 &b)
153+
{
154+
return v3s16(MYMIN(a.X, b.X), MYMIN(a.Y, b.Y), MYMIN(a.Z, b.Z));
155+
}
156+
157+
inline v3s16 componentwise_max(const v3s16 &a, const v3s16 &b)
158+
{
159+
return v3s16(MYMAX(a.X, b.X), MYMAX(a.Y, b.Y), MYMAX(a.Z, b.Z));
160+
}
161+
152162

153163
/** Returns \p f wrapped to the range [-360, 360]
154164
*

0 commit comments

Comments
 (0)
Please sign in to comment.