Fix physics bug

This commit is contained in:
Miloslav Ciz 2025-04-28 18:27:32 +02:00
parent 9dfbeae0d4
commit 5e73098460
4 changed files with 49 additions and 47 deletions

25
map.h
View file

@ -136,9 +136,9 @@
/**
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.
value returned by LCR_mapGetBlockAtFast (-1 is 0xffffffff, unknown is
0xfffffffe). 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];
@ -160,6 +160,9 @@ struct
void LCR_makeMapBlock(uint8_t type, uint8_t x, uint8_t y, uint8_t z,
uint8_t material, uint8_t transform, uint8_t block[LCR_BLOCK_SIZE])
{
x &= 0x3f;
y &= 0x3f;
z &= 0x3f;
block[0] = type;
block[1] = x | (y << 6);
block[2] = (y >> 2) | (z << 4);
@ -409,13 +412,19 @@ int LCR_mapGetBlockAtFast(uint8_t x, uint8_t y, uint8_t z,
{
// binary search (the blocks are sorted)
if ((x >= 64) | (y >= 64) | (z >= 64))
return -1;
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;
switch (_LCR_mapBlockCache[cacheIndex + 1])
{
case 0xffffffff: return -1; break;
case 0xfffffffe: break;
default: return ((int) _LCR_mapBlockCache[cacheIndex + 1]); break;
}
_LCR_mapBlockCache[cacheIndex] = n;
@ -710,7 +719,7 @@ uint8_t LCR_mapLoadFromStr(char (*getNextCharFunc)(void), const char *name)
LCR_LOG2("clearing map block cache")
for (int i = 0; i < LCR_MAP_BLOCK_CACHE_SIZE; ++i)
_LCR_mapBlockCache[i] = 0xffffffff;
_LCR_mapBlockCache[i] = 0xfffffffe;
_LCR_mapComputeHash();