refactor: introduced UnitActions.invokeUnitAction() to unify automation logic for unit actions

This commit is contained in:
Yair Morgenstern 2023-09-28 15:45:21 +03:00
parent d1b2d652e3
commit 03e09d85ee
3 changed files with 19 additions and 33 deletions

View File

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

View File

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

View File

@ -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<UnitAction> {
val tile = unit.getTile()
val actionList = ArrayList<UnitAction>()