From c259e35348ee7beeab02fca646ceca15c532864b Mon Sep 17 00:00:00 2001 From: Miloslav Ciz Date: Thu, 9 Jan 2025 15:34:14 +0100 Subject: [PATCH] Add map hash --- TODO.txt | 1 + assets.h | 6 +++--- map.h | 25 ++++++++++++++++++++++++- racing.h | 2 -- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/TODO.txt b/TODO.txt index bd01f01..02786ca 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,5 +1,6 @@ =========== GENERAL ============== +- the horizon on background seems too low? maybe add setting to shift it a bit? - add argc/argv to gameInit? could be used to quickly start maps, verify replays etc. - maybe each map could have a target time embedded: when beaten, the map would diff --git a/assets.h b/assets.h index cf11b5f..ed417b8 100644 --- a/assets.h +++ b/assets.h @@ -40,16 +40,16 @@ static const char *LCR_texts[] = static const char *LCR_internalResourceFile = "Mtestmap;" - "0 :*H1k0J" + "1 :*H1k0" ":=s0s0 :fd190" // big concrete - ":+v0n0" ":+w0o0" + ":+v0n0" ":!x0n0" - ":=s0B0 :fd910" // concrete wall ":^s1A0 :fk110" // ramps before wall + ":=s0B0 :fd910" // concrete wall ":nu0j1 :ly0j1 :AA0j1 " diff --git a/map.h b/map.h index a548f8c..96f3806 100644 --- a/map.h +++ b/map.h @@ -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(); diff --git a/racing.h b/racing.h index 5057d44..8bd4ef5 100644 --- a/racing.h +++ b/racing.h @@ -45,11 +45,9 @@ typedef int32_t LCR_GameUnit; ///< abstract game unit #define LCR_CAR_DRIFT_THRESHOLD_1 (LCR_GAME_UNIT / 4) #define LCR_CAR_DRIFT_THRESHOLD_0 (LCR_GAME_UNIT / 200) - #define LCR_CAR_CRASH_SPEED_DIFF 25 #define LCR_CAR_CRASH_SPEED_THRESHOLD 70 - // multipliers (in 8ths) of friction and acceleration on concrete: #define LCR_CAR_GRASS_FACTOR 5 #define LCR_CAR_DIRT_FACTOR 3