From 226bd85c840db5fde236d9fa63688753599ada4a Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Sun, 23 Apr 2023 09:40:33 +0300 Subject: [PATCH] Resolved #9249 - Solved AI Great Scientist crash --- .../unciv/logic/automation/unit/UnitAutomation.kt | 15 ++++++++------- core/src/com/unciv/models/UnitAction.kt | 1 + 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/core/src/com/unciv/logic/automation/unit/UnitAutomation.kt b/core/src/com/unciv/logic/automation/unit/UnitAutomation.kt index 0ba6de4f6b..0a5ed3a5ec 100644 --- a/core/src/com/unciv/logic/automation/unit/UnitAutomation.kt +++ b/core/src/com/unciv/logic/automation/unit/UnitAutomation.kt @@ -274,11 +274,13 @@ object UnitAutomation { val isLateGame = isLateGame(unit.civ) // Great scientist -> Hurry research if late game - if (UnitActions.getUnitActions(unit).any { it.type == UnitActionType.HurryResearch } - && isLateGame) { - UnitActions.getUnitActions(unit) - .first { it.type == UnitActionType.HurryResearch }.action!!.invoke() - return + if (isLateGame) { + val hurryResearch = UnitActions.getUnitActions(unit) + .firstOrNull { it.type == UnitActionType.HurryResearch }?.action + if (hurryResearch != null) { + hurryResearch() + return + } } // Great merchant -> Conduct trade mission if late game and if not at war. @@ -286,8 +288,7 @@ object UnitAutomation { // also have more influence. if (unit.hasUnique(UniqueType.CanTradeWithCityStateForGoldAndInfluence) // Don't wander around with the great merchant when at war. Barbs might also be a - // problem, but hopefully by the time we have a great merchant, they're under - // control. + // problem, but hopefully by the time we have a great merchant, they're under control. && !unit.civ.isAtWar() && isLateGame ) { diff --git a/core/src/com/unciv/models/UnitAction.kt b/core/src/com/unciv/models/UnitAction.kt index 7e4cca8679..cfa8feaa44 100644 --- a/core/src/com/unciv/models/UnitAction.kt +++ b/core/src/com/unciv/models/UnitAction.kt @@ -18,6 +18,7 @@ data class UnitAction( val title: String = type.value, val isCurrentAction: Boolean = false, val uncivSound: UncivSound = type.uncivSound, + /** Action is Null is this unit *can* execute the action but *not right now* - it's embarked, out of moves, etc */ val action: (() -> Unit)? = null ) { fun getIcon(): Actor {