From 03e09d85ee0c57e52a20f9dc3be210c4986aa24c Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Thu, 28 Sep 2023 15:45:21 +0300 Subject: [PATCH] refactor: introduced UnitActions.invokeUnitAction() to unify automation logic for unit actions --- .../automation/unit/SpecificUnitAutomation.kt | 29 +++++-------------- .../logic/automation/unit/UnitAutomation.kt | 14 ++------- .../worldscreen/unit/actions/UnitActions.kt | 9 ++++++ 3 files changed, 19 insertions(+), 33 deletions(-) diff --git a/core/src/com/unciv/logic/automation/unit/SpecificUnitAutomation.kt b/core/src/com/unciv/logic/automation/unit/SpecificUnitAutomation.kt index 3c3cdf14c5..609d4cc26f 100644 --- a/core/src/com/unciv/logic/automation/unit/SpecificUnitAutomation.kt +++ b/core/src/com/unciv/logic/automation/unit/SpecificUnitAutomation.kt @@ -216,11 +216,8 @@ object SpecificUnitAutomation { unit.movement.headTowards(chosenTile) if (unit.currentTile == chosenTile) { if (unit.currentTile.isPillaged()) - UnitActions.getUnitActions(unit).firstOrNull { it.type == UnitActionType.Repair } - ?.action?.invoke() - else - UnitActionsFromUniques.getImprovementConstructionActions(unit, unit.currentTile) - .firstOrNull()?.action?.invoke() + UnitActions.invokeUnitAction(unit, UnitActionType.Repair) + else UnitActions.invokeUnitAction(unit, UnitActionType.Create) return true } return unitTileBeforeMovement != unit.currentTile @@ -251,12 +248,8 @@ object SpecificUnitAutomation { .minByOrNull { it.second }?.first ?: return false - val conductTradeMissionAction = UnitActions.getUnitActions(unit) - .firstOrNull { it.type == UnitActionType.ConductTradeMission } - if (conductTradeMissionAction?.action != null) { - conductTradeMissionAction.action.invoke() - return true - } + val conductedTradeMission = UnitActions.invokeUnitAction(unit, UnitActionType.ConductTradeMission) + if (conductedTradeMission) return true val unitTileBeforeMovement = unit.currentTile unit.movement.headTowards(closestCityStateTile) @@ -295,12 +288,8 @@ object SpecificUnitAutomation { wonderToHurry.name ) unit.showAdditionalActions = false // make sure getUnitActions doesn't skip the important ones - val unitAction = UnitActions.getUnitActions(unit).firstOrNull { - it.type == UnitActionType.HurryBuilding || it.type == UnitActionType.HurryWonder - } ?: return false - if (unitAction.action == null) return false - unitAction.action.invoke() - return true + return UnitActions.invokeUnitAction(unit, UnitActionType.HurryBuilding) + || UnitActions.invokeUnitAction(unit, UnitActionType.HurryWonder) } // Walk towards the city. @@ -630,11 +619,7 @@ object SpecificUnitAutomation { return } - val foundReligionAction = UnitActions.getUnitActions(unit) - .firstOrNull { it.type == UnitActionType.FoundReligion } - ?.action ?: return - - foundReligionAction() + UnitActions.invokeUnitAction(unit, UnitActionType.FoundReligion) } fun enhanceReligion(unit: MapUnit) { diff --git a/core/src/com/unciv/logic/automation/unit/UnitAutomation.kt b/core/src/com/unciv/logic/automation/unit/UnitAutomation.kt index 31cf999926..f863bb2bb2 100644 --- a/core/src/com/unciv/logic/automation/unit/UnitAutomation.kt +++ b/core/src/com/unciv/logic/automation/unit/UnitAutomation.kt @@ -303,12 +303,8 @@ object UnitAutomation { val isLateGame = isLateGame(unit.civ) // Great scientist -> Hurry research if late game if (isLateGame) { - val hurryResearch = UnitActions.getUnitActions(unit) - .firstOrNull { it.type == UnitActionType.HurryResearch }?.action - if (hurryResearch != null) { - hurryResearch() - return - } + val hurriedResearch = UnitActions.invokeUnitAction(unit, UnitActionType.HurryResearch) + if (hurriedResearch) return } // Great merchant -> Conduct trade mission if late game and if not at war. @@ -345,7 +341,7 @@ object UnitAutomation { val improvementCanBePlacedEventually = SpecificUnitAutomation.automateImprovementPlacer(unit) if (!improvementCanBePlacedEventually) - startGoldenAgeIfHasAbility(unit) + UnitActions.invokeUnitAction(unit, UnitActionType.StartGoldenAge) } // TODO: The AI tends to have a lot of great generals. Maybe there should be a cutoff @@ -361,10 +357,6 @@ object UnitAutomation { return researchCompletePercent >= 0.8f } - private fun startGoldenAgeIfHasAbility(unit: MapUnit) { - UnitActions.getUnitActions(unit).firstOrNull { it.type == UnitActionType.StartGoldenAge }?.action?.invoke() - } - /** @return true only if the unit has 0 movement left */ private fun tryAttacking(unit: MapUnit): Boolean { repeat(unit.maxAttacksPerTurn() - unit.attacksThisTurn) { diff --git a/core/src/com/unciv/ui/screens/worldscreen/unit/actions/UnitActions.kt b/core/src/com/unciv/ui/screens/worldscreen/unit/actions/UnitActions.kt index 6020f1c778..bb4970fec4 100644 --- a/core/src/com/unciv/ui/screens/worldscreen/unit/actions/UnitActions.kt +++ b/core/src/com/unciv/ui/screens/worldscreen/unit/actions/UnitActions.kt @@ -21,6 +21,15 @@ object UnitActions { else getNormalActions(unit) } + /** Returns whether the action was invoked */ + fun invokeUnitAction(unit: MapUnit, unitActionType: UnitActionType): Boolean { + val unitAction = getNormalActions(unit).firstOrNull { it.type == unitActionType } + ?: getAdditionalActions(unit).firstOrNull { it.type == unitActionType } + val internalAction = unitAction?.action ?: return false + internalAction.invoke() + return true + } + private fun getNormalActions(unit: MapUnit): List { val tile = unit.getTile() val actionList = ArrayList()