From bd48cca8a22596136f0f476057616da5168e9409 Mon Sep 17 00:00:00 2001 From: DizzyEggg Date: Tue, 12 Sep 2023 10:35:05 +0200 Subject: [PATCH 1/2] ai delay timer --- include/battle.h | 2 ++ include/battle_ai_main.h | 2 +- include/config/debug.h | 1 + src/battle_ai_main.c | 6 ++++-- src/battle_controller_player.c | 13 +++++++++++++ src/battle_main.c | 6 +++--- 6 files changed, 24 insertions(+), 6 deletions(-) diff --git a/include/battle.h b/include/battle.h index f65c816ba..eb878b2a5 100644 --- a/include/battle.h +++ b/include/battle.h @@ -679,6 +679,8 @@ struct BattleStruct bool8 trainerSlideMegaEvolutionMsgDone; bool8 trainerSlideZMoveMsgDone; bool8 trainerSlideBeforeFirstTurnMsgDone; + u32 aiDelayTimer; // Counts number of frames AI takes to choose an action. + u32 aiDelayFrames; // Number of frames it took to choose an action. }; // The palaceFlags member of struct BattleStruct contains 1 flag per move to indicate which moves the AI should consider, diff --git a/include/battle_ai_main.h b/include/battle_ai_main.h index 6e35c04a2..f8eb2b77b 100644 --- a/include/battle_ai_main.h +++ b/include/battle_ai_main.h @@ -27,7 +27,7 @@ u8 BattleAI_ChooseMoveOrAction(void); void Ai_InitPartyStruct(void); void Ai_UpdateSwitchInData(u32 battler); void Ai_UpdateFaintData(u32 battler); -void GetAiLogicData(void); +void SetAiLogicDataForTurn(void); extern u8 sBattler_AI; diff --git a/include/config/debug.h b/include/config/debug.h index 43d743801..eea676b30 100644 --- a/include/config/debug.h +++ b/include/config/debug.h @@ -9,6 +9,7 @@ // Battle Debug Menu #define DEBUG_BATTLE_MENU TRUE // If set to TRUE, enables a debug menu to use in battles by pressing the Select button. +#define DEBUG_AI_DELAY_TIMER FALSE // If set to TRUE, displays the number of frames it takes for the AI to choose a move. Replaces the "What will PKMN do" text. Useful for devs or anyone who modifies the AI code and wants to see if it doesn't take too long to run. // Pokémon Debug #define DEBUG_POKEMON_MENU TRUE // Enables a debug menu for pokemon sprites and icons, accessed by pressing SELECT in the summary screen. diff --git a/src/battle_ai_main.c b/src/battle_ai_main.c index 069312f68..a6cab5a9b 100644 --- a/src/battle_ai_main.c +++ b/src/battle_ai_main.c @@ -1,4 +1,5 @@ #include "global.h" +#include "main.h" #include "malloc.h" #include "battle.h" #include "battle_anim.h" @@ -335,7 +336,7 @@ void Ai_UpdateFaintData(u32 battler) aiMon->isFainted = TRUE; } -static void SetBattlerAiData(u8 battler) +static void SetBattlerAiData(u32 battler) { AI_DATA->abilities[battler] = AI_GetAbility(battler); AI_DATA->items[battler] = gBattleMons[battler].item; @@ -346,12 +347,13 @@ static void SetBattlerAiData(u8 battler) AI_DATA->moveLimitations[battler] = CheckMoveLimitations(battler, 0, MOVE_LIMITATIONS_ALL); } -void GetAiLogicData(void) +void SetAiLogicDataForTurn(void) { u32 battlerAtk, battlerDef, i, move; u8 effectiveness; s32 dmg; + gBattleStruct->aiDelayTimer = gMain.vblankCounter1; memset(AI_DATA, 0, sizeof(struct AiLogicData)); if (!(gBattleTypeFlags & BATTLE_TYPE_HAS_AI) && !IsWildMonSmart()) diff --git a/src/battle_controller_player.c b/src/battle_controller_player.c index 79de66952..d20fbbb51 100644 --- a/src/battle_controller_player.c +++ b/src/battle_controller_player.c @@ -1889,6 +1889,19 @@ static void HandleChooseActionAfterDma3(u32 battler) { gBattle_BG0_X = 0; gBattle_BG0_Y = DISPLAY_HEIGHT; + if (gBattleStruct->aiDelayTimer != 0) + { + gBattleStruct->aiDelayFrames = gMain.vblankCounter1 - gBattleStruct->aiDelayTimer; + 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); + BattlePutTextOnWindow(gDisplayedStringBattle, B_WIN_ACTION_PROMPT); + } + #endif // DEBUG_AI_DELAY_TIMER + } gBattlerControllerFuncs[battler] = HandleInputChooseAction; } } diff --git a/src/battle_main.c b/src/battle_main.c index db550e58f..7f985f16e 100644 --- a/src/battle_main.c +++ b/src/battle_main.c @@ -3724,7 +3724,7 @@ static void DoBattleIntro(void) gBattleStruct->switchInAbilitiesCounter = 0; gBattleStruct->switchInItemsCounter = 0; gBattleStruct->overworldWeatherDone = FALSE; - GetAiLogicData(); // get assumed abilities, hold effects, etc of all battlers + SetAiLogicDataForTurn(); // get assumed abilities, hold effects, etc of all battlers Ai_InitPartyStruct(); // Save mons party counts, and first 2/4 mons on the battlefield. gBattleMainFunc = TryDoEventsBeforeFirstTurn; } @@ -3857,7 +3857,7 @@ static void TryDoEventsBeforeFirstTurn(void) gRandomTurnNumber = Random(); - GetAiLogicData(); // get assumed abilities, hold effects, etc of all battlers + SetAiLogicDataForTurn(); // get assumed abilities, hold effects, etc of all battlers if (gBattleTypeFlags & BATTLE_TYPE_ARENA) { @@ -3949,7 +3949,7 @@ void BattleTurnPassed(void) *(&gBattleStruct->absentBattlerFlags) = gAbsentBattlerFlags; BattlePutTextOnWindow(gText_EmptyString3, B_WIN_MSG); - GetAiLogicData(); // get assumed abilities, hold effects, etc of all battlers + SetAiLogicDataForTurn(); // get assumed abilities, hold effects, etc of all battlers gBattleMainFunc = HandleTurnActionSelectionState; gRandomTurnNumber = Random(); From 2d9cbef684e877a010318aa15e68f49901ba93a4 Mon Sep 17 00:00:00 2001 From: DizzyEggg Date: Tue, 12 Sep 2023 20:19:14 +0200 Subject: [PATCH 2/2] exclude wild battles from ai lag meter --- src/battle_ai_main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/battle_ai_main.c b/src/battle_ai_main.c index a6cab5a9b..a8946bcec 100644 --- a/src/battle_ai_main.c +++ b/src/battle_ai_main.c @@ -353,12 +353,13 @@ void SetAiLogicDataForTurn(void) u8 effectiveness; s32 dmg; - gBattleStruct->aiDelayTimer = gMain.vblankCounter1; memset(AI_DATA, 0, sizeof(struct AiLogicData)); if (!(gBattleTypeFlags & BATTLE_TYPE_HAS_AI) && !IsWildMonSmart()) return; + gBattleStruct->aiDelayTimer = gMain.vblankCounter1; + // get/assume all battler data for (i = 0; i < gBattlersCount; i++) {