Add Transform Action for mods (#8120)

* Initial version of code

* Enable cost=0 for buttons

* Add Conditional to Transform

* Fix translation file

* Add icon
Allow other Conditionals

* Updated icon

* Add credit

* switch hotkey

* Fix icon

* Remove Conditional Costs

* Remove vestigal Stats costs for Transform

* Minor cleanup
This commit is contained in:
itanasi 2023-01-14 10:43:48 -08:00 committed by GitHub
parent 9fee185f5d
commit 22f9936a34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 74 additions and 2 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 159 KiB

View File

@ -946,6 +946,8 @@ Add in capital =
Add to [comment] =
Upgrade to [unitType] ([goldCost] gold) =
Upgrade to [unitType]\n([goldCost] gold, [resources]) =
Transform to [unitType] =
Transform to [unitType]\n([resources]) =
Found city =
Promote =
Health =
@ -1330,6 +1332,7 @@ Can upgrade from [unit] =
Can upgrade from: =
Upgrades to [upgradedUnit] =
Obsolete with [obsoleteTech] =
Can Transform to [upgradedUnit] =
Occurs on [listOfTerrains] =
Occurs on: =
Placed on [terrainType] =

View File

@ -1235,7 +1235,7 @@ class CivilizationInfo : IsPartOfGameInfoSerialization {
Stat.Culture -> policies.storedCulture
Stat.Science -> {
if (tech.currentTechnology() == null) 0
else tech.remainingScienceToTech(tech.currentTechnology()!!.name)
else tech.researchOfTech(tech.currentTechnology()!!.name)
}
Stat.Gold -> gold
Stat.Faith -> religionManager.storedFaith

View File

@ -83,6 +83,8 @@ enum class UnitActionType(
{ ImageGetter.getUnitActionPortrait("Promote") }, 'o', false, UncivSound.Promote),
Upgrade("Upgrade",
{ ImageGetter.getUnitActionPortrait("Upgrade") }, 'u', UncivSound.Upgrade),
Transform("Transform",
{ ImageGetter.getUnitActionPortrait("Transform") }, 'k', UncivSound.Upgrade),
Pillage("Pillage",
{ ImageGetter.getUnitActionPortrait("Pillage") }, 'p', false),
Paradrop("Paradrop",

View File

@ -480,7 +480,7 @@ enum class UniqueType(val text: String, vararg targets: UniqueTarget, val flags:
CanHurryResearch("Can hurry technology research", UniqueTarget.Unit),
CanTradeWithCityStateForGoldAndInfluence("Can undertake a trade mission with City-State, giving a large sum of gold and [amount] Influence", UniqueTarget.Unit),
CanTransform("Can transform to [unit]", UniqueTarget.Unit),
//endregion

View File

@ -18,6 +18,7 @@ import com.unciv.models.UncivSound
import com.unciv.models.UnitAction
import com.unciv.models.UnitActionType
import com.unciv.models.ruleset.Building
import com.unciv.models.ruleset.unique.StateForConditionals
import com.unciv.models.ruleset.unique.UniqueTriggerActivation
import com.unciv.models.ruleset.unique.UniqueType
import com.unciv.models.stats.Stat
@ -57,6 +58,7 @@ object UnitActions {
addPromoteAction(unit, actionList)
addUnitUpgradeAction(unit, actionList)
addTransformAction(unit, actionList)
addPillageAction(unit, actionList, worldScreen)
addParadropAction(unit, actionList)
addAirSweepAction(unit, actionList)
@ -472,6 +474,70 @@ object UnitActions {
fun getAncientRuinsUpgradeAction(unit: MapUnit) =
getUpgradeAction(unit, isFree = true, isSpecial = true)
private fun addTransformAction(
unit: MapUnit,
actionList: ArrayList<UnitAction>,
maxSteps: Int = Int.MAX_VALUE
) {
val upgradeAction = getTransformAction(unit)
if (upgradeAction != null) actionList += upgradeAction
}
/** */
private fun getTransformAction(
unit: MapUnit
): ArrayList<UnitAction>? {
if (!unit.baseUnit().hasUnique(UniqueType.CanTransform)) return null // can't upgrade to anything
val unitTile = unit.getTile()
val civInfo = unit.civInfo
val transformList = ArrayList<UnitAction>()
for (unique in unit.baseUnit().getMatchingUniques(UniqueType.CanTransform,
StateForConditionals(unit = unit, civInfo = civInfo, tile = unitTile))) {
val upgradedUnit = civInfo.getEquivalentUnit(unique.params[0])
// don't show if haven't researched/is obsolete
if (!unit.canUpgrade(unitToUpgradeTo = upgradedUnit)) continue
// Check _new_ resource requirements
// Using Counter to aggregate is a bit exaggerated, but - respect the mad modder.
val resourceRequirementsDelta = Counter<String>()
for ((resource, amount) in unit.baseUnit().getResourceRequirements())
resourceRequirementsDelta.add(resource, -amount)
for ((resource, amount) in upgradedUnit.getResourceRequirements())
resourceRequirementsDelta.add(resource, amount)
val newResourceRequirementsString = resourceRequirementsDelta.entries
.filter { it.value > 0 }
.joinToString { "${it.value} {${it.key}}".tr() }
val title = if (newResourceRequirementsString.isEmpty())
"Transform to [${upgradedUnit.name}]"
else "Transform to [${upgradedUnit.name}]\n([$newResourceRequirementsString])"
transformList.add(UnitAction(UnitActionType.Transform,
title = title,
action = {
unit.destroy()
val newUnit = 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 resurrectedUnit = civInfo.placeUnitNearTile(unitTile.position, unit.name)!!
unit.copyStatisticsTo(resurrectedUnit)
} else { // Managed to upgrade
unit.copyStatisticsTo(newUnit)
newUnit.currentMovement = 0f
}
}.takeIf {
unit.currentMovement > 0
&& !unit.isEmbarked()
&& unit.canUpgrade(unitToUpgradeTo = upgradedUnit)
}
) )
}
return transformList
}
private fun addBuildingImprovementsAction(unit: MapUnit, actionList: ArrayList<UnitAction>, tile: TileInfo, worldScreen: WorldScreen, unitTable: UnitTable) {
if (!unit.hasUniqueToBuildImprovements) return
if (unit.isEmbarked()) return

View File

@ -768,6 +768,7 @@ Unless otherwise specified, all the following are from [the Noun Project](https:
- [Trade](https://www.flaticon.com/free-icon/trade_4257019) created by Smashicons for Conduct Trade Mission
- [Nothing](https://www.flaticon.com/free-icon/nothing_5084125) created by Freepik for Nothing construction process
- Icon for Unique created by [vegeta1k95](https://github.com/vegeta1k95)
- [Transform] created by letstalkaboutdune
### Main menu