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) unit.movement.headTowards(chosenTile)
if (unit.currentTile == chosenTile) { if (unit.currentTile == chosenTile) {
if (unit.currentTile.isPillaged()) if (unit.currentTile.isPillaged())
UnitActions.getUnitActions(unit).firstOrNull { it.type == UnitActionType.Repair } UnitActions.invokeUnitAction(unit, UnitActionType.Repair)
?.action?.invoke() else UnitActions.invokeUnitAction(unit, UnitActionType.Create)
else
UnitActionsFromUniques.getImprovementConstructionActions(unit, unit.currentTile)
.firstOrNull()?.action?.invoke()
return true return true
} }
return unitTileBeforeMovement != unit.currentTile return unitTileBeforeMovement != unit.currentTile
@ -251,12 +248,8 @@ object SpecificUnitAutomation {
.minByOrNull { it.second }?.first .minByOrNull { it.second }?.first
?: return false ?: return false
val conductTradeMissionAction = UnitActions.getUnitActions(unit) val conductedTradeMission = UnitActions.invokeUnitAction(unit, UnitActionType.ConductTradeMission)
.firstOrNull { it.type == UnitActionType.ConductTradeMission } if (conductedTradeMission) return true
if (conductTradeMissionAction?.action != null) {
conductTradeMissionAction.action.invoke()
return true
}
val unitTileBeforeMovement = unit.currentTile val unitTileBeforeMovement = unit.currentTile
unit.movement.headTowards(closestCityStateTile) unit.movement.headTowards(closestCityStateTile)
@ -295,12 +288,8 @@ object SpecificUnitAutomation {
wonderToHurry.name wonderToHurry.name
) )
unit.showAdditionalActions = false // make sure getUnitActions doesn't skip the important ones unit.showAdditionalActions = false // make sure getUnitActions doesn't skip the important ones
val unitAction = UnitActions.getUnitActions(unit).firstOrNull { return UnitActions.invokeUnitAction(unit, UnitActionType.HurryBuilding)
it.type == UnitActionType.HurryBuilding || it.type == UnitActionType.HurryWonder || UnitActions.invokeUnitAction(unit, UnitActionType.HurryWonder)
} ?: return false
if (unitAction.action == null) return false
unitAction.action.invoke()
return true
} }
// Walk towards the city. // Walk towards the city.
@ -630,11 +619,7 @@ object SpecificUnitAutomation {
return return
} }
val foundReligionAction = UnitActions.getUnitActions(unit) UnitActions.invokeUnitAction(unit, UnitActionType.FoundReligion)
.firstOrNull { it.type == UnitActionType.FoundReligion }
?.action ?: return
foundReligionAction()
} }
fun enhanceReligion(unit: MapUnit) { fun enhanceReligion(unit: MapUnit) {

View File

@ -303,12 +303,8 @@ object UnitAutomation {
val isLateGame = isLateGame(unit.civ) val isLateGame = isLateGame(unit.civ)
// Great scientist -> Hurry research if late game // Great scientist -> Hurry research if late game
if (isLateGame) { if (isLateGame) {
val hurryResearch = UnitActions.getUnitActions(unit) val hurriedResearch = UnitActions.invokeUnitAction(unit, UnitActionType.HurryResearch)
.firstOrNull { it.type == UnitActionType.HurryResearch }?.action if (hurriedResearch) return
if (hurryResearch != null) {
hurryResearch()
return
}
} }
// Great merchant -> Conduct trade mission if late game and if not at war. // Great merchant -> Conduct trade mission if late game and if not at war.
@ -345,7 +341,7 @@ object UnitAutomation {
val improvementCanBePlacedEventually = val improvementCanBePlacedEventually =
SpecificUnitAutomation.automateImprovementPlacer(unit) SpecificUnitAutomation.automateImprovementPlacer(unit)
if (!improvementCanBePlacedEventually) 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 // 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 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 */ /** @return true only if the unit has 0 movement left */
private fun tryAttacking(unit: MapUnit): Boolean { private fun tryAttacking(unit: MapUnit): Boolean {
repeat(unit.maxAttacksPerTurn() - unit.attacksThisTurn) { repeat(unit.maxAttacksPerTurn() - unit.attacksThisTurn) {

View File

@ -21,6 +21,15 @@ object UnitActions {
else getNormalActions(unit) 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> { private fun getNormalActions(unit: MapUnit): List<UnitAction> {
val tile = unit.getTile() val tile = unit.getTile()
val actionList = ArrayList<UnitAction>() val actionList = ArrayList<UnitAction>()