Tested and reworked the 'unit upgrades for free' unique till it worked (#6555)

This commit is contained in:
Xander Lenstra 2022-04-17 07:01:22 +02:00 committed by GitHub
parent d3b17117da
commit 0230575473
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 17 deletions

View File

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

View File

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

View File

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