Fix endless loop when many units can transfer movement to each other

This commit is contained in:
yairm210 2024-07-24 16:29:47 +03:00
parent b0c9724433
commit 61fe6d0eee
2 changed files with 12 additions and 9 deletions

View File

@ -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

View File

@ -150,13 +150,13 @@ class Ruleset {
private inline fun <reified T : INamed> createHashmap(items: Array<T>): LinkedHashMap<String, T> {
val hashMap = LinkedHashMap<String, T>(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
}