From 926a7437da327a49fe261813ee397e0b12160a8a Mon Sep 17 00:00:00 2001 From: Isaac Aronson Date: Sun, 30 Mar 2025 17:08:29 -0500 Subject: [PATCH 1/3] Add AI thinking cycle count display to frame count --- include/battle.h | 5 +++-- include/global.h | 2 +- src/battle_ai_main.c | 7 +++++++ src/battle_controller_player.c | 11 ++++++++--- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/include/battle.h b/include/battle.h index 6f77ea88cf..cdb21340b1 100644 --- a/include/battle.h +++ b/include/battle.h @@ -797,8 +797,9 @@ struct BattleStruct u8 itemMoveIndex[MAX_BATTLERS_COUNT]; u8 pledgeMove:1; u8 isSkyBattle:1; - u32 aiDelayTimer; // Counts number of frames AI takes to choose an action. - u32 aiDelayFrames; // Number of frames it took to choose an action. + s32 aiDelayTimer; // Counts number of frames AI takes to choose an action. + s32 aiDelayFrames; // Number of frames it took to choose an action. + s32 aiDelayCycles; // Number of cycles it took to choose an action. u8 timesGotHit[NUM_BATTLE_SIDES][PARTY_SIZE]; u8 transformZeroToHero[NUM_BATTLE_SIDES]; u8 stickySyrupdBy[MAX_BATTLERS_COUNT]; diff --git a/include/global.h b/include/global.h index c033ab9b35..06bfc99a6f 100644 --- a/include/global.h +++ b/include/global.h @@ -131,7 +131,7 @@ #define FEATURE_FLAG_ASSERT(flag, id) STATIC_ASSERT(flag > TEMP_FLAGS_END || flag == 0, id) -#ifndef NDEBUG +#if !(defined (NDEBUG)) || DEBUG_AI_DELAY_TIMER static inline void CycleCountStart() { REG_TM2CNT_H = 0; diff --git a/src/battle_ai_main.c b/src/battle_ai_main.c index e04ca63e68..cb5e7a0851 100644 --- a/src/battle_ai_main.c +++ b/src/battle_ai_main.c @@ -473,6 +473,9 @@ void SetAiLogicDataForTurn(struct AiLogicData *aiData) battlersCount = gBattlersCount; AI_DATA->aiCalcInProgress = TRUE; + #if DEBUG_AI_DELAY_TIMER + CycleCountStart(); + #endif for (battlerAtk = 0; battlerAtk < battlersCount; battlerAtk++) { if (!IsBattlerAlive(battlerAtk)) @@ -488,6 +491,10 @@ void SetAiLogicDataForTurn(struct AiLogicData *aiData) SetBattlerAiMovesData(aiData, battlerAtk, battlersCount, weather); } + #if DEBUG_AI_DELAY_TIMER + // We add to existing to compound multiple calls + gBattleStruct->aiDelayCycles += CycleCountEnd(); + #endif AI_DATA->aiCalcInProgress = FALSE; } diff --git a/src/battle_controller_player.c b/src/battle_controller_player.c index 96404e031e..7ea7136dfa 100644 --- a/src/battle_controller_player.c +++ b/src/battle_controller_player.c @@ -2013,9 +2013,14 @@ static void HandleChooseActionAfterDma3(u32 battler) gBattleStruct->aiDelayTimer = 0; #if DEBUG_AI_DELAY_TIMER { - static const u8 sText_AIDelay[] = _("AI delay:\n{B_BUFF1} frames"); - PREPARE_HWORD_NUMBER_BUFFER(gBattleTextBuff1, 3, gBattleStruct->aiDelayFrames); - BattleStringExpandPlaceholdersToDisplayedString(sText_AIDelay); + static const u8 sFramesText[] = _(" frames thinking\n"); + static const u8 sCyclesText[] = _(" cycles"); + ConvertIntToDecimalStringN(gDisplayedStringBattle, gBattleStruct->aiDelayFrames, STR_CONV_MODE_RIGHT_ALIGN, 3); + u8* end = StringAppend(gDisplayedStringBattle, sFramesText); + ConvertIntToDecimalStringN(end, gBattleStruct->aiDelayCycles, STR_CONV_MODE_RIGHT_ALIGN, 8); + // Clear old result once read out + gBattleStruct->aiDelayCycles = 0; + StringAppend(gDisplayedStringBattle, sCyclesText); BattlePutTextOnWindow(gDisplayedStringBattle, B_WIN_ACTION_PROMPT); } #endif // DEBUG_AI_DELAY_TIMER From 692f7968fc2b2855d8d4dc5a024476b1d23fc869 Mon Sep 17 00:00:00 2001 From: aronson Date: Mon, 31 Mar 2025 12:06:12 -0500 Subject: [PATCH 2/3] Use non-preproc guards on AI frame count Co-authored-by: hedara90 <90hedara@gmail.com> --- src/battle_ai_main.c | 12 +++++------- src/battle_controller_player.c | 3 +-- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/battle_ai_main.c b/src/battle_ai_main.c index cb5e7a0851..b823e6e23e 100644 --- a/src/battle_ai_main.c +++ b/src/battle_ai_main.c @@ -473,9 +473,8 @@ void SetAiLogicDataForTurn(struct AiLogicData *aiData) battlersCount = gBattlersCount; AI_DATA->aiCalcInProgress = TRUE; - #if DEBUG_AI_DELAY_TIMER - CycleCountStart(); - #endif + if (DEBUG_AI_DELAY_TIMER) + CycleCountStart(); for (battlerAtk = 0; battlerAtk < battlersCount; battlerAtk++) { if (!IsBattlerAlive(battlerAtk)) @@ -491,10 +490,9 @@ void SetAiLogicDataForTurn(struct AiLogicData *aiData) SetBattlerAiMovesData(aiData, battlerAtk, battlersCount, weather); } - #if DEBUG_AI_DELAY_TIMER - // We add to existing to compound multiple calls - gBattleStruct->aiDelayCycles += CycleCountEnd(); - #endif + if (DEBUG_AI_DELAY_TIMER) + // We add to existing to compound multiple calls + gBattleStruct->aiDelayCycles += CycleCountEnd(); AI_DATA->aiCalcInProgress = FALSE; } diff --git a/src/battle_controller_player.c b/src/battle_controller_player.c index 7ea7136dfa..5ff81bf673 100644 --- a/src/battle_controller_player.c +++ b/src/battle_controller_player.c @@ -2011,7 +2011,7 @@ static void HandleChooseActionAfterDma3(u32 battler) { gBattleStruct->aiDelayFrames = gMain.vblankCounter1 - gBattleStruct->aiDelayTimer; gBattleStruct->aiDelayTimer = 0; - #if DEBUG_AI_DELAY_TIMER + if (DEBUG_AI_DELAY_TIMER) { static const u8 sFramesText[] = _(" frames thinking\n"); static const u8 sCyclesText[] = _(" cycles"); @@ -2023,7 +2023,6 @@ static void HandleChooseActionAfterDma3(u32 battler) StringAppend(gDisplayedStringBattle, sCyclesText); BattlePutTextOnWindow(gDisplayedStringBattle, B_WIN_ACTION_PROMPT); } - #endif // DEBUG_AI_DELAY_TIMER } gBattlerControllerFuncs[battler] = HandleInputChooseAction; } From 74e84da571a3f30f3df502f3013771380448394c Mon Sep 17 00:00:00 2001 From: Isaac Aronson Date: Mon, 31 Mar 2025 13:11:02 -0500 Subject: [PATCH 3/3] Remove NDEBUG guard on cycle counter functions --- include/global.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/global.h b/include/global.h index 06bfc99a6f..a6e494f03a 100644 --- a/include/global.h +++ b/include/global.h @@ -131,7 +131,7 @@ #define FEATURE_FLAG_ASSERT(flag, id) STATIC_ASSERT(flag > TEMP_FLAGS_END || flag == 0, id) -#if !(defined (NDEBUG)) || DEBUG_AI_DELAY_TIMER +// NOTE: This uses hardware timers 2 and 3; this will not work during active link connections or with the eReader static inline void CycleCountStart() { REG_TM2CNT_H = 0; @@ -154,7 +154,6 @@ static inline u32 CycleCountEnd() // return result return REG_TM2CNT_L | (REG_TM3CNT_L << 16u); } -#endif struct Coords8 {