Cleaned up Unit Actions

This commit is contained in:
Yair Morgenstern
2020-02-10 18:43:18 +02:00
parent 14f89b6799
commit a514e462cc

View File

@ -53,7 +53,127 @@ class UnitActions {
)
}
if (!unit.type.isAirUnit()) {
addExplorationActions(unit, actionList)
addPromoteAction(unit, actionList)
addUnitUpgradeAction(unit, tile, actionList, worldScreen)
addPillageAction(unit, tile, actionList)
addSetupAction(unit, actionList)
addFoundCityAction(unit, actionList, tile)
addWorkerActions(unit, actionList, tile, worldScreen, unitTable)
addConstructRoadsAction(unit, tile, actionList)
addCreateWaterImprovements(unit, tile, actionList)
addGreatPersonActions(unit, actionList, tile)
addDisbandAction(actionList, unit, worldScreen)
return actionList
}
private fun addDisbandAction(actionList: ArrayList<UnitAction>, unit: MapUnit, worldScreen: WorldScreen) {
actionList += UnitAction(
type = UnitActionType.DisbandUnit,
canAct = unit.currentMovement > 0,
action = {
val disbandText = if (unit.currentTile.getOwner() == unit.civInfo)
"Disband this unit for [${unit.baseUnit.getDisbandGold()}] gold?".tr()
else "Do you really want to disband this unit?".tr()
YesNoPopup(disbandText, { unit.disband(); worldScreen.shouldUpdate = true }).open()
})
}
private fun addCreateWaterImprovements(unit: MapUnit, tile: TileInfo, actionList: ArrayList<UnitAction>) {
for (improvement in listOf("Fishing Boats", "Oil well")) {
if (unit.hasUnique("May create improvements on water resources") && tile.resource != null
&& tile.isWater // because fishing boats can enter cities, and if there's oil in the city... ;)
&& tile.improvement == null
&& tile.getTileResource().improvement == improvement
&& unit.civInfo.tech.isResearched(unit.civInfo.gameInfo.ruleSet.tileImprovements[improvement]!!.techRequired!!)
)
actionList += UnitAction(
type = UnitActionType.Create,
title = "Create [$improvement]",
canAct = unit.currentMovement > 0,
action = {
tile.improvement = improvement
unit.destroy()
})
}
}
private fun addConstructRoadsAction(unit: MapUnit, tile: TileInfo, actionList: ArrayList<UnitAction>) {
if (unit.hasUnique("Can construct roads")
&& tile.roadStatus == RoadStatus.None
&& tile.improvementInProgress != "Road"
&& tile.isLand
&& unit.civInfo.tech.isResearched(RoadStatus.Road.improvement(unit.civInfo.gameInfo.ruleSet)!!.techRequired!!))
actionList += UnitAction(
type = UnitActionType.ConstructRoad,
canAct = unit.currentMovement > 0,
action = {
tile.improvementInProgress = "Road"
tile.turnsToImprovement = 4
})
}
private fun addFoundCityAction(unit: MapUnit, actionList: ArrayList<UnitAction>, tile: TileInfo) {
if (!unit.hasUnique("Founds a new city") || unit.isEmbarked()) return
actionList += UnitAction(
type = UnitActionType.FoundCity,
canAct = unit.currentMovement > 0 && !tile.getTilesInDistance(3).any { it.isCityCenter() },
uncivSound = UncivSound.Chimes,
action = {
UncivGame.Current.settings.addCompletedTutorialTask("Found city")
unit.civInfo.addCity(tile.position)
tile.improvement = null
unit.destroy()
})
}
private fun addPromoteAction(unit: MapUnit, actionList: ArrayList<UnitAction>) {
if (unit.type.isCivilian() || !unit.promotions.canBePromoted()) return
// promotion does not consume movement points, so we can do it always
actionList += UnitAction(
type = UnitActionType.Promote,
canAct = true,
uncivSound = UncivSound.Promote,
action = {
UncivGame.Current.setScreen(PromotionPickerScreen(unit))
})
}
private fun addSetupAction(unit: MapUnit, actionList: ArrayList<UnitAction>) {
if (!unit.hasUnique("Must set up to ranged attack") || unit.isEmbarked()) return
val isSetUp = unit.action == "Set Up"
actionList += UnitAction(
type = UnitActionType.SetUp,
canAct = unit.currentMovement > 0 && !isSetUp,
isCurrentAction = isSetUp,
uncivSound = UncivSound.Setup,
action = {
unit.action = Constants.unitActionSetUp
unit.useMovementPoints(1f)
})
}
private fun addPillageAction(unit: MapUnit, tile: TileInfo, actionList: ArrayList<UnitAction>) {
if (unit.type.isCivilian() || tile.improvement == null) return
actionList += UnitAction(
type = UnitActionType.Pillage,
canAct = unit.currentMovement > 0 && canPillage(unit, tile),
action = {
// http://well-of-souls.com/civ/civ5_improvements.html says that naval improvements are destroyed upon pilllage
// and I can't find any other sources so I'll go with that
if (tile.isLand) {
tile.improvementInProgress = tile.improvement
tile.turnsToImprovement = 2
}
tile.improvement = null
if (!unit.hasUnique("No movement cost to pillage")) unit.useMovementPoints(1f)
unit.healBy(25)
})
}
private fun addExplorationActions(unit: MapUnit, actionList: ArrayList<UnitAction>) {
if (unit.type.isAirUnit()) return
if (unit.action != Constants.unitActionExplore) {
actionList += UnitAction(
type = UnitActionType.Explore,
@ -71,19 +191,9 @@ class UnitActions {
}
}
if (!unit.type.isCivilian() && unit.promotions.canBePromoted()) {
// promotion does not consume movement points, so we can do it always
actionList += UnitAction(
type = UnitActionType.Promote,
canAct = true,
uncivSound = UncivSound.Promote,
action = {
UncivGame.Current.setScreen(PromotionPickerScreen(unit))
})
}
if (unit.baseUnit().upgradesTo != null && tile.getOwner() == unit.civInfo) {
if (unit.canUpgrade()) {
private fun addUnitUpgradeAction(unit: MapUnit, tile: TileInfo, actionList: ArrayList<UnitAction>, worldScreen: WorldScreen) {
if (unit.baseUnit().upgradesTo == null || tile.getOwner() != unit.civInfo) return
if (!unit.canUpgrade()) return
val goldCostOfUpgrade = unit.getCostOfUpgrade()
val upgradedUnit = unit.getUnitToUpgradeTo()
@ -110,52 +220,9 @@ class UnitActions {
worldScreen.shouldUpdate = true
})
}
}
if (!unit.type.isCivilian() && tile.improvement != null) {
actionList += UnitAction(
type = UnitActionType.Pillage,
canAct = unit.currentMovement > 0 && canPillage(unit, tile),
action = {
// http://well-of-souls.com/civ/civ5_improvements.html says that naval improvements are destroyed upon pilllage
// and I can't find any other sources so I'll go with that
if (tile.isLand) {
tile.improvementInProgress = tile.improvement
tile.turnsToImprovement = 2
}
tile.improvement = null
if (!unit.hasUnique("No movement cost to pillage")) unit.useMovementPoints(1f)
unit.healBy(25)
})
}
if (unit.hasUnique("Must set up to ranged attack") && !unit.isEmbarked()) {
val setUp = unit.action == "Set Up"
actionList += UnitAction(
type = UnitActionType.SetUp,
canAct = unit.currentMovement > 0 && !setUp,
isCurrentAction = setUp,
uncivSound = UncivSound.Setup,
action = {
unit.action = Constants.unitActionSetUp
unit.useMovementPoints(1f)
})
}
if (unit.hasUnique("Founds a new city") && !unit.isEmbarked()) {
actionList += UnitAction(
type = UnitActionType.FoundCity,
canAct = unit.currentMovement > 0 && !tile.getTilesInDistance(3).any { it.isCityCenter() },
uncivSound = UncivSound.Chimes,
action = {
UncivGame.Current.settings.addCompletedTutorialTask("Found city")
unit.civInfo.addCity(tile.position)
tile.improvement = null
unit.destroy()
})
}
if (unit.hasUnique("Can build improvements on tiles") && !unit.isEmbarked()) {
private fun addWorkerActions(unit: MapUnit, actionList: ArrayList<UnitAction>, tile: TileInfo, worldScreen: WorldScreen, unitTable: UnitTable) {
if (!unit.hasUnique("Can build improvements on tiles") || unit.isEmbarked()) return
actionList += UnitAction(
type = UnitActionType.ConstructImprovement,
canAct = unit.currentMovement > 0
@ -183,36 +250,7 @@ class UnitActions {
}
}
if (unit.hasUnique("Can construct roads")
&& tile.roadStatus == RoadStatus.None
&& tile.improvementInProgress != "Road"
&& tile.isLand
&& unit.civInfo.tech.isResearched(RoadStatus.Road.improvement(unit.civInfo.gameInfo.ruleSet)!!.techRequired!!))
actionList += UnitAction(
type = UnitActionType.ConstructRoad,
canAct = unit.currentMovement > 0,
action = {
tile.improvementInProgress = "Road"
tile.turnsToImprovement = 4
})
for (improvement in listOf("Fishing Boats", "Oil well")) {
if (unit.hasUnique("May create improvements on water resources") && tile.resource != null
&& tile.isWater // because fishing boats can enter cities, and if there's oil in the city... ;)
&& tile.improvement == null
&& tile.getTileResource().improvement == improvement
&& unit.civInfo.tech.isResearched(unit.civInfo.gameInfo.ruleSet.tileImprovements[improvement]!!.techRequired!!)
)
actionList += UnitAction(
type = UnitActionType.Create,
title = "Create [$improvement]",
canAct = unit.currentMovement > 0,
action = {
tile.improvement = improvement
unit.destroy()
})
}
private fun addGreatPersonActions(unit: MapUnit, actionList: ArrayList<UnitAction>, tile: TileInfo) {
for (unique in unit.getUniques().filter { it.startsWith("Can build improvement: ") }) {
val improvementName = unique.replace("Can build improvement: ", "")
actionList += UnitAction(
@ -293,18 +331,6 @@ class UnitActions {
unit.destroy()
})
}
actionList += UnitAction(
type = UnitActionType.DisbandUnit,
canAct = unit.currentMovement > 0,
action = {
val disbandText = if (unit.currentTile.getOwner() == unit.civInfo)
"Disband this unit for [${unit.baseUnit.getDisbandGold()}] gold?".tr()
else "Do you really want to disband this unit?".tr()
YesNoPopup(disbandText, { unit.disband(); worldScreen.shouldUpdate = true }).open()
})
return actionList
}
private fun addFortifyActions(actionList: ArrayList<UnitAction>, unit: MapUnit, unitTable: UnitTable) {