perf: minor memory perf

This commit is contained in:
yairm210 2024-09-08 16:31:07 +03:00
parent 2647b24e09
commit 563925bbf5
2 changed files with 10 additions and 10 deletions

View File

@ -76,8 +76,8 @@ class WorkerAutomation(
// Must be called before any getPriority checks to guarantee the local road cache is processed
val citiesToConnect = roadBetweenCitiesAutomation.getNearbyCitiesToConnect(unit)
// Shortcut, we are working a suitable tile, and we're better off minimizing worker-turns by finishing everything on this tile
if (!dangerousTiles.contains(currentTile) && getFullPriority(unit.getTile(), unit, localUniqueCache) >= 2
&& currentTile.improvementInProgress != null) {
if (currentTile.improvementInProgress != null && !dangerousTiles.contains(currentTile)
&& getFullPriority(unit.getTile(), unit, localUniqueCache) >= 2) {
return
}
val tileToWork = findTileToWork(unit, dangerousTiles, localUniqueCache)

View File

@ -12,6 +12,13 @@ import kotlin.random.Random
object Conditionals {
private fun getStateBasedRandom(state: StateForConditionals, unique: Unique?): Float {
var seed = state.gameInfo?.turns?.hashCode() ?: 0
seed = seed * 31 + (unique?.hashCode() ?: 0)
seed = seed * 31 + state.hashCode()
return Random(seed).nextFloat()
}
fun conditionalApplies(
unique: Unique?,
conditional: Unique,
@ -21,13 +28,6 @@ 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 {
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 {
if (state.gameInfo == null) return false
@ -103,7 +103,7 @@ object Conditionals {
}
return when (conditional.type) {
UniqueType.ConditionalChance -> stateBasedRandom.nextFloat() < conditional.params[0].toFloat() / 100f
UniqueType.ConditionalChance -> getStateBasedRandom(state, unique) < conditional.params[0].toFloat() / 100f
UniqueType.ConditionalEveryTurns -> checkOnGameInfo { turns % conditional.params[0].toInt() == 0 }
UniqueType.ConditionalBeforeTurns -> checkOnGameInfo { turns < conditional.params[0].toInt() }
UniqueType.ConditionalAfterTurns -> checkOnGameInfo { turns >= conditional.params[0].toInt() }