Add block cache

This commit is contained in:
Miloslav Ciz 2024-11-21 00:16:00 +01:00
parent ac5bddd9f4
commit f248981676
3 changed files with 86 additions and 9 deletions

32
map.h
View file

@ -81,7 +81,7 @@
#define LCR_BLOCK_RAMP_12 0x07 ///< plain ramp, 1/2 size
#define LCR_BLOCK_RAMP_14 0x08 ///< plain ramp, 1/4 size
#define LCR_BLOCK_RAMP_CURVED_PLAT 0x09 ///< curved ramp with top platgform
#define LCR_BLOCK_RAMP_CURVED 0x0a ///< curv. ramp without top platf.
#define LCR_BLOCK_RAMP_CURVED 0x0a ///< curv. ramp without top platf.
#define LCR_BLOCK_RAMP_CURVED_WALL 0x0b ///< curved ramp plus small wall
#define LCR_BLOCK_RAMP_STEEP 0x0c ///< extremely steep ramp
#define LCR_BLOCK_CORNER 0x0d ///< diagonal corner
@ -112,6 +112,17 @@
but makes a hollow one */
#define LCR_BLOCK_START 0x83 ///< specifies start block position
#define LCR_MAP_BLOCK_CACHE_SIZE (8 * 2) /// do not change
/**
Cache for accelerating LCR_mapGetBlockAtFast, consists of 8 2-item records,
the first record item stores block coord number, the second one stores the
value returned by LCR_mapGetBlockAtFast (-1 is 0xffffffff). The record index
depends on the block coords: lowest bit is x % 2, middle bit y % 2, highest
one z % 2.
*/
uint32_t _LCR_mapBlockCache[LCR_MAP_BLOCK_CACHE_SIZE];
struct
{
uint16_t blockCount;
@ -390,7 +401,12 @@ uint8_t LCR_mapLoad(const uint8_t *map)
map += LCR_BLOCK_SIZE;
}
LCR_LOG2("clearing map block cache")
for (int i = 0; i < LCR_MAP_BLOCK_CACHE_SIZE; ++i)
_LCR_mapBlockCache[i] = 0xffffffff;
LCR_LOG2("map loaded")
return 1;
@ -406,6 +422,14 @@ int LCR_mapGetBlockAtFast(uint8_t x, uint8_t y, uint8_t z,
// binary search (the blocks are sorted)
uint32_t n = LCR_mapBlockCoordsToCoordNumber(x,y,z);
uint8_t cacheIndex = 2 * ((x % 2) | ((y % 2) << 1) | ((z % 2) << 2));
if (_LCR_mapBlockCache[cacheIndex] == n)
return
(_LCR_mapBlockCache[cacheIndex + 1] != 0xffffffff) ?
((int) _LCR_mapBlockCache[cacheIndex + 1]) : -1;
_LCR_mapBlockCache[cacheIndex] = n;
while (start <= end)
{
@ -419,9 +443,13 @@ int LCR_mapGetBlockAtFast(uint8_t x, uint8_t y, uint8_t z,
else if (n2 > n)
end = m - 1;
else
{
_LCR_mapBlockCache[cacheIndex + 1] = m;
return m;
}
}
_LCR_mapBlockCache[cacheIndex + 1] = 0xffffffff;
return -1;
}