From 60bb8704adba8178cfd7731f6759c3c9e94eaf91 Mon Sep 17 00:00:00 2001 From: Jeremy Woo <71725118+woo1127@users.noreply.github.com> Date: Wed, 10 Apr 2024 04:10:56 +0800 Subject: [PATCH] Allow AI civilian unit to consider other triggerable uniques (#11372) * Enable AI civilian unit to handle other triggerable uniques * Automation of AI Great Writer * Automation of AI with GainFreeBuildings unique * Stop AI unit to blinkly invoke the first action anymore --- .../automation/unit/CivilianUnitAutomation.kt | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/core/src/com/unciv/logic/automation/unit/CivilianUnitAutomation.kt b/core/src/com/unciv/logic/automation/unit/CivilianUnitAutomation.kt index 81f63dccb4..dc859fc745 100644 --- a/core/src/com/unciv/logic/automation/unit/CivilianUnitAutomation.kt +++ b/core/src/com/unciv/logic/automation/unit/CivilianUnitAutomation.kt @@ -5,8 +5,11 @@ import com.unciv.logic.civilization.managers.ReligionState 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.UniqueTriggerActivation import com.unciv.models.ruleset.unique.UniqueType +import com.unciv.ui.screens.worldscreen.unit.actions.UnitActionModifiers import com.unciv.ui.screens.worldscreen.unit.actions.UnitActions +import com.unciv.ui.screens.worldscreen.unit.actions.UnitActionsFromUniques object CivilianUnitAutomation { @@ -76,9 +79,13 @@ object CivilianUnitAutomation { val isLateGame = isLateGame(unit.civ) // Great scientist -> Hurry research if late game + // Great writer -> Hurry policy if late game if (isLateGame) { val hurriedResearch = UnitActions.invokeUnitAction(unit, UnitActionType.HurryResearch) if (hurriedResearch) return + + val hurriedPolicy = UnitActions.invokeUnitAction(unit, UnitActionType.HurryPolicy) + if (hurriedPolicy) return } // Great merchant -> Conduct trade mission if late game and if not at war. @@ -105,7 +112,6 @@ object CivilianUnitAutomation { return } - // This has to come after the individual abilities for the great people that can also place // instant improvements (e.g. great scientist). if (unit.hasUnique(UniqueType.ConstructImprovementInstantly)) { @@ -117,6 +123,29 @@ object CivilianUnitAutomation { UnitActions.invokeUnitAction(unit, UnitActionType.StartGoldenAge) } + if (unit.hasUnique(UniqueType.GainFreeBuildings)) { + val unique = unit.getMatchingUniques(UniqueType.GainFreeBuildings).first() + val buildingName = unique.params[0] + // Choose the city that is closest in distance and does not have the building constructed. + val cityToGainBuilding = unit.civ.cities.filter { + !it.cityConstructions.containsBuildingOrEquivalent(buildingName) + && (unit.movement.canMoveTo(it.getCenterTile()) || unit.currentTile == it.getCenterTile()) + }.minByOrNull { + val path = unit.movement.getShortestPath(it.getCenterTile()) + path.size + } + + if (cityToGainBuilding != null) { + if (unit.currentTile == cityToGainBuilding.getCenterTile()) { + UniqueTriggerActivation.triggerUnique(unique, unit.civ, unit = unit, tile = unit.currentTile) + UnitActionModifiers.activateSideEffects(unit, unique) + return + } + else unit.movement.headTowards(cityToGainBuilding.getCenterTile()) + } + return + } + // TODO: The AI tends to have a lot of great generals. Maybe there should be a cutoff // (depending on number of cities) and after that they should just be used to start golden // ages?