map seed calculation (#3)

This commit is contained in:
Héctor Giménez
2023-10-21 14:26:02 +09:00
committed by GitHub
parent 14ca6ddbf0
commit 77413a8aaa
3 changed files with 44 additions and 30 deletions

View File

@@ -0,0 +1,37 @@
package utils
import "math"
const mapHashDivisor = 1 << 16
func GetMapSeed(initHashSeed, endHashSeed uint) (uint, bool) {
var gameSeedXor uint = 0
seed, found := reverseMapSeedHash(endHashSeed)
if found {
gameSeedXor = initHashSeed ^ seed
}
if gameSeedXor == 0 {
return 0, false
}
return seed, true
}
func reverseMapSeedHash(hash uint) (uint, bool) {
incrementalValue := uint(1)
for startValue := uint(0); startValue < math.MaxUint; startValue += incrementalValue {
seedResult := (uint(startValue)*0x6AC690C5 + 666) & 0xFFFFFFFF
if seedResult == hash {
return startValue, true
}
if incrementalValue == 1 && (seedResult%mapHashDivisor) == (hash%mapHashDivisor) {
incrementalValue = mapHashDivisor
}
}
return 0, false
}