From 563925bbf520dd3ba57f9d300fd5ad231f082a7a Mon Sep 17 00:00:00 2001 From: yairm210 Date: Sun, 8 Sep 2024 16:31:07 +0300 Subject: [PATCH] perf: minor memory perf --- .../logic/automation/unit/WorkerAutomation.kt | 4 ++-- .../unciv/models/ruleset/unique/Conditionals.kt | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/core/src/com/unciv/logic/automation/unit/WorkerAutomation.kt b/core/src/com/unciv/logic/automation/unit/WorkerAutomation.kt index 50c100f743..ccbc794558 100644 --- a/core/src/com/unciv/logic/automation/unit/WorkerAutomation.kt +++ b/core/src/com/unciv/logic/automation/unit/WorkerAutomation.kt @@ -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) diff --git a/core/src/com/unciv/models/ruleset/unique/Conditionals.kt b/core/src/com/unciv/models/ruleset/unique/Conditionals.kt index e5944c8893..cc413dbec2 100644 --- a/core/src/com/unciv/models/ruleset/unique/Conditionals.kt +++ b/core/src/com/unciv/models/ruleset/unique/Conditionals.kt @@ -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() }