From 02305754739a18ad1dae8791ea991e9b3c94b1bc Mon Sep 17 00:00:00 2001 From: Xander Lenstra <71121390+xlenstra@users.noreply.github.com> Date: Sun, 17 Apr 2022 07:01:22 +0200 Subject: [PATCH] Tested and reworked the 'unit upgrades for free' unique till it worked (#6555) --- core/src/com/unciv/logic/map/MapUnit.kt | 3 +- .../ruleset/unique/UniqueTriggerActivation.kt | 2 +- .../unciv/ui/worldscreen/unit/UnitActions.kt | 52 +++++++++++++------ 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/core/src/com/unciv/logic/map/MapUnit.kt b/core/src/com/unciv/logic/map/MapUnit.kt index 7eef0d6871..5345d44bcb 100644 --- a/core/src/com/unciv/logic/map/MapUnit.kt +++ b/core/src/com/unciv/logic/map/MapUnit.kt @@ -513,8 +513,9 @@ class MapUnit { if (name == unitToUpgradeTo.name) return false val rejectionReasons = unitToUpgradeTo.getRejectionReasons(civInfo) if (rejectionReasons.isEmpty()) return true + if (ignoreRequired && rejectionReasons.filterTechPolicyEraWonderRequirements().isEmpty()) return true - if (rejectionReasons.size == 1 && rejectionReasons.contains(RejectionReason.ConsumesResources)) { + if (rejectionReasons.contains(RejectionReason.ConsumesResources)) { // We need to remove the unit from the civ for this check, // because if the unit requires, say, horses, and so does its upgrade, // and the civ currently has 0 horses, we need to see if the upgrade will be buildable diff --git a/core/src/com/unciv/models/ruleset/unique/UniqueTriggerActivation.kt b/core/src/com/unciv/models/ruleset/unique/UniqueTriggerActivation.kt index e33d608531..2c143b390a 100644 --- a/core/src/com/unciv/models/ruleset/unique/UniqueTriggerActivation.kt +++ b/core/src/com/unciv/models/ruleset/unique/UniqueTriggerActivation.kt @@ -494,7 +494,7 @@ object UniqueTriggerActivation { return true } OneTimeUnitUpgrade -> { - val upgradeAction = UnitActions.getUpgradeAction(unit, true) + val upgradeAction = UnitActions.getFreeUpgradeAction(unit) ?: return false upgradeAction.action!!() if (notification != null) diff --git a/core/src/com/unciv/ui/worldscreen/unit/UnitActions.kt b/core/src/com/unciv/ui/worldscreen/unit/UnitActions.kt index 06142f6173..0650dd9a0a 100644 --- a/core/src/com/unciv/ui/worldscreen/unit/UnitActions.kt +++ b/core/src/com/unciv/ui/worldscreen/unit/UnitActions.kt @@ -311,14 +311,14 @@ object UnitActions { if (upgradeAction != null) actionList += upgradeAction } - fun getUpgradeAction(unit: MapUnit, isFree: Boolean = false): UnitAction? { + fun getUpgradeAction(unit: MapUnit): UnitAction? { val tile = unit.currentTile - if (unit.baseUnit().upgradesTo == null || !unit.canUpgrade()) return null - if (tile.getOwner() != unit.civInfo && !isFree) return null - val goldCostOfUpgrade = - if (isFree) 0 - else unit.getCostOfUpgrade() + if (unit.baseUnit().upgradesTo == null) return null + if (!unit.canUpgrade()) return null + if (tile.getOwner() != unit.civInfo) return null + val upgradedUnit = unit.getUnitToUpgradeTo() + val goldCostOfUpgrade = unit.getCostOfUpgrade() return UnitAction(UnitActionType.Upgrade, title = "Upgrade to [${upgradedUnit.name}] ([$goldCostOfUpgrade] gold)", @@ -339,12 +339,35 @@ object UnitActions { newUnit.currentMovement = 0f } }.takeIf { - isFree || - ( - unit.civInfo.gold >= goldCostOfUpgrade - && unit.currentMovement > 0 - && !unit.isEmbarked() - ) + unit.civInfo.gold >= goldCostOfUpgrade + && unit.currentMovement > 0 + && !unit.isEmbarked() + } + ) + } + + fun getFreeUpgradeAction(unit: MapUnit): UnitAction? { + if (unit.baseUnit().upgradesTo == null) return null + val upgradedUnit = unit.civInfo.getEquivalentUnit(unit.baseUnit().upgradesTo!!) + if (!unit.canUpgrade(upgradedUnit, true)) return null + + return UnitAction(UnitActionType.Upgrade, + title = "Upgrade to [${upgradedUnit.name}] (FREE)", + action = { + val unitTile = unit.getTile() + unit.destroy() + val newUnit = unit.civInfo.placeUnitNearTile(unitTile.position, upgradedUnit.name) + + /** We were UNABLE to place the new unit, which means that the unit failed to upgrade! + * The only known cause of this currently is "land units upgrading to water units" which fail to be placed. + */ + if (newUnit == null) { + val readdedUnit = unit.civInfo.placeUnitNearTile(unitTile.position, unit.name) + unit.copyStatisticsTo(readdedUnit!!) + } else { // Managed to upgrade + unit.copyStatisticsTo(newUnit) + newUnit.currentMovement = 0f + } } ) } @@ -357,9 +380,8 @@ object UnitActions { else -> return null } val upgradedUnit = - unit.civInfo.getEquivalentUnit( - unit.civInfo.gameInfo.ruleSet.units[upgradedUnitName]!! - ) + unit.civInfo.getEquivalentUnit(unit.civInfo.gameInfo.ruleSet.units[upgradedUnitName]!!) + if (!unit.canUpgrade(upgradedUnit,true)) return null return UnitAction(UnitActionType.Upgrade,