Add map hash

This commit is contained in:
Miloslav Ciz 2025-01-09 15:34:14 +01:00
parent 33daa484e9
commit c259e35348
4 changed files with 28 additions and 6 deletions

25
map.h
View file

@ -151,7 +151,8 @@ struct
uint8_t environment;
uint8_t checkpointCount;
// TODO: name, desc? possibly as a single '\n' separated string?
uint32_t hash; ///< Hash of the processed binary map.
} LCR_currentMap;
void LCR_makeMapBlock(uint8_t type, uint8_t x, uint8_t y, uint8_t z,
@ -353,6 +354,25 @@ int _LCR_mapCharToCoord(char c)
return -1;
}
void _LCR_mapComputeHash(void)
{
LCR_LOG1("computing map hash");
const uint8_t *data = LCR_currentMap.startPos;
LCR_currentMap.hash = 11 + LCR_currentMap.environment;
for (int i = 0; i < LCR_currentMap.blockCount * LCR_BLOCK_SIZE + 4; ++i)
{
LCR_currentMap.hash = LCR_currentMap.hash * 101 + *data;
data = i != 3 ? data + 1 : LCR_currentMap.blocks;
}
LCR_currentMap.hash *= 251;
LCR_currentMap.hash = ((LCR_currentMap.hash << 19) |
(LCR_currentMap.hash >> 13)) * 113;
}
uint8_t LCR_mapLoadFromStr(char (*getNextCharFunc)(void))
{
LCR_LOG0("loading map string");
@ -368,6 +388,7 @@ uint8_t LCR_mapLoadFromStr(char (*getNextCharFunc)(void))
LCR_currentMap.checkpointCount = 0;
LCR_currentMap.blockCount = 0;
LCR_currentMap.environment = 0;
LCR_currentMap.hash = 0;
c = getNextCharFunc();
@ -495,6 +516,8 @@ uint8_t LCR_mapLoadFromStr(char (*getNextCharFunc)(void))
for (int i = 0; i < LCR_MAP_BLOCK_CACHE_SIZE; ++i)
_LCR_mapBlockCache[i] = 0xffffffff;
_LCR_mapComputeHash();
LCR_LOG2("map loaded")
LCR_mapReset();