standardized pillage, sleep actions

This commit is contained in:
Yair Morgenstern
2023-12-08 13:24:21 +02:00
parent 840f3c2093
commit fad06b3aec
4 changed files with 50 additions and 49 deletions

View File

@ -14,8 +14,10 @@ import com.unciv.logic.civilization.NotificationCategory
import com.unciv.logic.civilization.diplomacy.DiplomaticStatus
import com.unciv.logic.map.mapunit.MapUnit
import com.unciv.logic.map.tile.Tile
import com.unciv.models.UnitActionType
import com.unciv.models.ruleset.unique.StateForConditionals
import com.unciv.models.ruleset.unique.UniqueType
import com.unciv.ui.screens.worldscreen.unit.actions.UnitActions
import com.unciv.ui.screens.worldscreen.unit.actions.UnitActionsPillage
import com.unciv.ui.screens.worldscreen.unit.actions.UnitActionsUpgrade
@ -360,7 +362,7 @@ object UnitAutomation {
if (unit.getTile() != tileToPillage)
unit.movement.moveToTile(tileToPillage)
UnitActionsPillage.getPillageAction(unit)?.action?.invoke()
UnitActions.invokeUnitAction(unit, UnitActionType.Pillage)
return unit.currentMovement == 0f
}

View File

@ -68,9 +68,8 @@ object UnitActions {
if (shouldAutomationBePrimaryAction(unit))
actionList += getAutomateActions(unit, unit.currentTile)
if (unit.isMoving()) {
if (unit.isMoving())
actionList += UnitAction(UnitActionType.StopMovement) { unit.action = null }
}
if (unit.isExploring())
actionList += UnitAction(UnitActionType.StopExploration) { unit.action = null }
if (unit.isAutomated())
@ -79,13 +78,13 @@ object UnitActions {
unit.automated = false
}
addPromoteAction(unit, actionList)
UnitActionsUpgrade.addUnitUpgradeAction(unit, actionList)
UnitActionsPillage.addPillageAction(unit, actionList)
addSleepActions(actionList, unit, false)
actionList += getPromoteActions(unit, unit.currentTile)
actionList += UnitActionsUpgrade.getUnitUpgradeActions(unit, unit.currentTile)
actionList += UnitActionsPillage.getPillageActions(unit, unit.currentTile)
actionList += getSleepActions(unit, tile)
actionList += getSleepUntilHealedActions(unit, tile)
addFortifyActions(actionList, unit, false)
if (unit.isMilitary()) addExplorationActions(unit, actionList)
addWaitAction(unit, actionList)
@ -104,7 +103,6 @@ object UnitActions {
GUI.getMap().setCenterPosition(unit.getMovementDestination().position, true)
}
}
addSleepActions(actionList, unit, true)
addFortifyActions(actionList, unit, true)
if (!shouldAutomationBePrimaryAction(unit))
actionList += getAutomateActions(unit, unit.currentTile)
@ -163,13 +161,14 @@ object UnitActions {
}
private fun addPromoteAction(unit: MapUnit, actionList: ArrayList<UnitAction>) {
if (unit.isCivilian() || !unit.promotions.canBePromoted()) return
private fun getPromoteActions(unit: MapUnit, tile: Tile): List<UnitAction> {
if (unit.isCivilian() || !unit.promotions.canBePromoted()) return listOf()
// promotion does not consume movement points, but is not allowed if a unit has exhausted its movement or has attacked
actionList += UnitAction(UnitActionType.Promote,
return listOf(UnitAction(UnitActionType.Promote,
action = {
UncivGame.Current.pushScreen(PromotionPickerScreen(unit))
}.takeIf { unit.currentMovement > 0 && unit.attacksThisTurn == 0 })
}.takeIf { unit.currentMovement > 0 && unit.attacksThisTurn == 0 }
))
}
private fun addExplorationActions(unit: MapUnit, actionList: ArrayList<UnitAction>) {
@ -212,29 +211,28 @@ object UnitActions {
action = { unit.fortify() }.takeIf { !isFortified })
}
private fun addSleepActions(
actionList: ArrayList<UnitAction>,
unit: MapUnit,
showingAdditionalActions: Boolean
) {
if (unit.isFortified() || unit.canFortify() || unit.currentMovement == 0f) return
// If this unit is working on an improvement, it cannot sleep
if (unit.currentTile.hasImprovementInProgress()
&& unit.canBuildImprovement(unit.currentTile.getTileImprovementInProgress()!!)
) return
val isSleeping = unit.isSleeping()
val isDamaged = unit.health < 100
fun shouldHaveSleepAction(unit: MapUnit, tile: Tile): Boolean {
if (unit.isFortified() || unit.canFortify() || unit.currentMovement == 0f) return false
if (tile.hasImprovementInProgress()
&& unit.canBuildImprovement(tile.getTileImprovementInProgress()!!)
) return false
return true
}
private fun getSleepActions(unit: MapUnit, tile: Tile): List<UnitAction> {
if (!shouldHaveSleepAction(unit, tile)) return listOf()
if (unit.health < 100) return listOf()
return listOf(UnitAction(UnitActionType.Sleep,
action = { unit.action = UnitActionType.Sleep.value }.takeIf { !unit.isSleeping() }
))
}
if (isDamaged && !showingAdditionalActions) {
actionList += UnitAction(UnitActionType.SleepUntilHealed,
action = { unit.action = UnitActionType.SleepUntilHealed.value }
.takeIf { !unit.isSleepingUntilHealed() && unit.canHealInCurrentTile() }
)
} else if (isDamaged || !showingAdditionalActions) {
actionList += UnitAction(UnitActionType.Sleep,
action = { unit.action = UnitActionType.Sleep.value }.takeIf { !isSleeping }
)
}
private fun getSleepUntilHealedActions(unit: MapUnit, tile: Tile): List<UnitAction> {
if (!shouldHaveSleepAction(unit, tile)) return listOf()
if (unit.health < 100) return listOf()
return listOf(UnitAction(UnitActionType.SleepUntilHealed,
action = { unit.action = UnitActionType.SleepUntilHealed.value }
.takeIf { !unit.isSleepingUntilHealed() && unit.canHealInCurrentTile() }
))
}
private fun addGiftAction(unit: MapUnit, actionList: ArrayList<UnitAction>, tile: Tile) {

View File

@ -16,29 +16,28 @@ import kotlin.random.Random
object UnitActionsPillage {
fun addPillageAction(unit: MapUnit, actionList: ArrayList<UnitAction>) {
val pillageAction = getPillageAction(unit)
?: return
fun getPillageActions(unit: MapUnit, tile: Tile): List<UnitAction> {
val pillageAction = getPillageAction(unit, tile)
?: return listOf()
if (pillageAction.action == null)
actionList += pillageAction
else actionList += UnitAction(UnitActionType.Pillage, pillageAction.title) {
return listOf(pillageAction)
else return listOf(UnitAction(UnitActionType.Pillage, pillageAction.title) {
if (!GUI.getWorldScreen().hasOpenPopups()) {
val pillageText = "Are you sure you want to pillage this [${unit.currentTile.getImprovementToPillageName()!!}]?"
val pillageText = "Are you sure you want to pillage this [${tile.getImprovementToPillageName()!!}]?"
ConfirmPopup(
GUI.getWorldScreen(),
pillageText,
"Pillage",
true
) {
(pillageAction.action)()
pillageAction.action()
GUI.setUpdateWorldOnNextRender()
}.open()
}
}
})
}
fun getPillageAction(unit: MapUnit): UnitAction? {
val tile = unit.currentTile
fun getPillageAction(unit: MapUnit, tile: Tile): UnitAction? {
val improvementName = unit.currentTile.getImprovementToPillageName()
if (unit.isCivilian() || improvementName == null || tile.getOwner() == unit.civ) return null
return UnitAction(

View File

@ -1,6 +1,7 @@
package com.unciv.ui.screens.worldscreen.unit.actions
import com.unciv.logic.map.mapunit.MapUnit
import com.unciv.logic.map.tile.Tile
import com.unciv.models.Counter
import com.unciv.models.UnitAction
import com.unciv.models.UpgradeUnitAction
@ -10,12 +11,13 @@ import com.unciv.models.translations.tr
object UnitActionsUpgrade {
internal fun addUnitUpgradeAction(
internal fun getUnitUpgradeActions(
unit: MapUnit,
actionList: ArrayList<UnitAction>
) {
tile: Tile
): List<UnitAction> {
val upgradeAction = getUpgradeAction(unit)
if (upgradeAction != null) actionList += upgradeAction
if (upgradeAction != null) return listOf(upgradeAction)
return listOf()
}
/** Common implementation for [getUpgradeAction], [getFreeUpgradeAction] and [getAncientRuinsUpgradeAction] */