Better state-based random - ensure repeatability while increasing diversity (less "bunching" of so-called "random" results)

This commit is contained in:
yairm210 2024-07-21 11:37:16 +03:00
parent bb5c98ab19
commit c765287333
2 changed files with 10 additions and 5 deletions

View File

@ -20,7 +20,12 @@ object Conditionals {
if (conditional.type?.targetTypes?.any { it.modifierType == UniqueTarget.ModifierType.Other } == true)
return true // not a filtering condition, includes e.g. ModifierHiddenFromUsers
val stateBasedRandom by lazy { Random(state.hashCode() * 31 + (state.gameInfo?.turns?.hashCode() ?: 0)) }
val stateBasedRandom by lazy {
var seed = state.gameInfo?.turns?.hashCode() ?: 0
seed = seed * 31 + (unique?.hashCode() ?: 0)
seed = seed * 31 + state.hashCode()
Random(seed)
}
/** Helper to simplify conditional tests requiring gameInfo */
fun checkOnGameInfo(predicate: (GameInfo.() -> Boolean)): Boolean {

View File

@ -106,10 +106,10 @@ data class StateForConditionals(
fun CombatAction?.hash() = this?.name?.hashCode() ?: 0
fun Region?.hash() = this?.rect?.hashCode() ?: 0
var result = civInfo.hash()
result = 31 * result + city.hash()
result = 31 * result + unit.hash()
result = 31 * result + tile.hash()
var result = relevantCiv.hash()
result = 31 * result + relevantCity.hash()
result = 31 * result + relevantUnit.hash()
result = 31 * result + relevantTile.hash()
result = 31 * result + ourCombatant.hash()
result = 31 * result + theirCombatant.hash()
result = 31 * result + attackedTile.hash()