From 61fe6d0eeee514351daa05204abba27540949c1a Mon Sep 17 00:00:00 2001 From: yairm210 Date: Wed, 24 Jul 2024 16:29:47 +0300 Subject: [PATCH] Fix endless loop when many units can transfer movement to each other --- core/src/com/unciv/logic/map/mapunit/MapUnit.kt | 15 +++++++++------ core/src/com/unciv/models/ruleset/Ruleset.kt | 6 +++--- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/core/src/com/unciv/logic/map/mapunit/MapUnit.kt b/core/src/com/unciv/logic/map/mapunit/MapUnit.kt index 1743189148..b557092f87 100644 --- a/core/src/com/unciv/logic/map/mapunit/MapUnit.kt +++ b/core/src/com/unciv/logic/map/mapunit/MapUnit.kt @@ -308,7 +308,7 @@ class MapUnit : IsPartOfGameInfoSerialization { return false } - fun getMaxMovement(): Int { + fun getMaxMovement(ignoreOtherUnit: Boolean = false): Int { var movement = if (isEmbarked()) 2 else baseUnit.movement @@ -321,11 +321,14 @@ class MapUnit : IsPartOfGameInfoSerialization { // Hakkapeliitta movement boost // For every double-stacked tile, check if our cohabitant can boost our speed // (a test `count() > 1` is no optimization - two iterations of a sequence instead of one) - for (boostingUnit in currentTile.getUnits()) { - if (boostingUnit == this) continue - if (boostingUnit.getMatchingUniques(UniqueType.TransferMovement) - .none { matchesFilter(it.params[0]) }) continue - movement = movement.coerceAtLeast(boostingUnit.getMaxMovement()) + if (!ignoreOtherUnit) { // if both units can boost the other, we avoid an endless loop + for (boostingUnit in currentTile.getUnits()) { + if (boostingUnit == this) continue + if (boostingUnit.getMatchingUniques(UniqueType.TransferMovement) + .none { matchesFilter(it.params[0]) } + ) continue + movement = movement.coerceAtLeast(boostingUnit.getMaxMovement(true)) + } } return movement diff --git a/core/src/com/unciv/models/ruleset/Ruleset.kt b/core/src/com/unciv/models/ruleset/Ruleset.kt index d4abaebf79..3ca060e5f6 100644 --- a/core/src/com/unciv/models/ruleset/Ruleset.kt +++ b/core/src/com/unciv/models/ruleset/Ruleset.kt @@ -150,13 +150,13 @@ class Ruleset { private inline fun createHashmap(items: Array): LinkedHashMap { val hashMap = LinkedHashMap(items.size) for (item in items) { - val name = try { item.name } + val itemName = try { item.name } catch (ex: Exception) { throw Exception("${T::class.simpleName} is missing a name!") } - hashMap[item.name] = item - (item as? IRulesetObject)?.originRuleset = name + hashMap[itemName] = item + (item as? IRulesetObject)?.originRuleset = name // RULESET name } return hashMap }