From 01327265a3c59fc0e26aa9b269e831ded2037c01 Mon Sep 17 00:00:00 2001 From: Asdow <20314541+Asdow@users.noreply.github.com> Date: Sun, 4 Feb 2024 16:10:26 +0200 Subject: [PATCH] Check if action should stay inprogress state Several actions tended to get stuck in an invalid state, where it was completed, but aiData.bActionInProgress was still TRUE resulting in an AI deadlock. If such a situation is found, Actiondone is now called, preventing the deadlock --- TacticalAI/AIMain.cpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/TacticalAI/AIMain.cpp b/TacticalAI/AIMain.cpp index 1aa88ee3f..ff85945f3 100644 --- a/TacticalAI/AIMain.cpp +++ b/TacticalAI/AIMain.cpp @@ -440,6 +440,27 @@ void DebugQuestInfo(STR szOutput) } } +static INT16 ShouldActionStayInProgress(SOLDIERTYPE* pSoldier) +{ + // this here should never happen, but it seems to (turns sometimes hang!) + if ((pSoldier->aiData.bAction == AI_ACTION_CHANGE_FACING) && (pSoldier->pathing.bDesiredDirection != pSoldier->aiData.usActionData)) + { + // don't try to pay any more APs for this, it was paid for once already! + pSoldier->pathing.bDesiredDirection = (INT8)pSoldier->aiData.usActionData; // turn to face direction in actionData + return(TRUE); + } + else if ((pSoldier->aiData.bAction == AI_ACTION_CHANGE_FACING) && (pSoldier->pathing.bDesiredDirection == pSoldier->aiData.usActionData)) + { + return(FALSE); + } + else if (pSoldier->aiData.bAction == AI_ACTION_END_TURN || pSoldier->aiData.bAction == AI_ACTION_NONE) + { + return(FALSE); + } + // needs more time to complete action + return(TRUE); +} + BOOLEAN InitAI( void ) { @@ -992,6 +1013,12 @@ void HandleSoldierAI( SOLDIERTYPE *pSoldier ) // FIXME - this function is named { ActionDone(pSoldier); } + + if (!ShouldActionStayInProgress(pSoldier)) + { + DebugAI(AI_MSG_INFO, pSoldier, String("Action % s was stuck as being in progress. Canceling action", wszAction[pSoldier->aiData.bAction])); + ActionDone(pSoldier); + } } /********* End of new overall AI system @@ -1460,7 +1487,7 @@ void FreeUpNPCFromRoofClimb(SOLDIERTYPE *pSoldier ) void ActionDone(SOLDIERTYPE *pSoldier) { // if an action is currently selected - if (pSoldier->aiData.bAction != AI_ACTION_NONE) + //if (pSoldier->aiData.bAction != AI_ACTION_NONE) { if (pSoldier->flags.uiStatusFlags & SOLDIER_MONSTER) {