Save party mons ai

This commit is contained in:
DizzyEggg 2022-08-23 01:07:25 +02:00
parent eb0cc91dce
commit 327782646b
10 changed files with 377 additions and 179 deletions

View File

@ -107,7 +107,7 @@ LIBPATH := -L ../../tools/agbcc/lib
LIB := $(LIBPATH) -lgcc -lc -L../../libagbsyscall -lagbsyscall
else
CC1 = $(shell $(PATH_MODERNCC) --print-prog-name=cc1) -quiet
override CFLAGS += -mthumb -mthumb-interwork -O2 -mabi=apcs-gnu -mtune=arm7tdmi -march=armv4t -fno-toplevel-reorder -Wno-pointer-to-int-cast
override CFLAGS += -mthumb -mthumb-interwork -O0 -mabi=apcs-gnu -mtune=arm7tdmi -march=armv4t -fno-toplevel-reorder -Wno-pointer-to-int-cast
ROM := $(MODERN_ROM_NAME)
OBJ_DIR := $(MODERN_OBJ_DIR_NAME)
LIBPATH := -L "$(dir $(shell $(PATH_MODERNCC) -mthumb -print-file-name=libgcc.a))" -L "$(dir $(shell $(PATH_MODERNCC) -mthumb -print-file-name=libnosys.a))" -L "$(dir $(shell $(PATH_MODERNCC) -mthumb -print-file-name=libc.a))"

BIN
NO$GBA.EXE - Shortcut.lnk Normal file

Binary file not shown.

View File

@ -245,6 +245,26 @@ struct AI_SavedBattleMon
u16 species;
};
struct AiPartyMon
{
u16 species;
u16 item;
u16 heldEffect;
u16 ability;
u16 gender;
u16 level;
u16 moves[MAX_MON_MOVES];
bool8 isFainted;
u8 switchInCount; // Counts how many times this Pokemon has been sent out or switched into in a battle.
bool8 wasSentInBattle;
};
struct AIPartyData // Opposing battlers - party mons.
{
struct AiPartyMon mons[2][PARTY_SIZE]; // 2 parties(player, opponent). Used to save information on opposing party.
u8 count[2];
};
struct AiLogicData
{
u16 abilities[MAX_BATTLERS_COUNT];
@ -313,6 +333,7 @@ struct BattleResources
struct StatsArray* beforeLvlUp;
struct AI_ThinkingStruct *ai;
struct AiLogicData *aiData;
struct AIPartyData *aiParty;
struct BattleHistory *battleHistory;
u8 bufferA[MAX_BATTLERS_COUNT][0x200];
u8 bufferB[MAX_BATTLERS_COUNT][0x200];
@ -320,6 +341,7 @@ struct BattleResources
#define AI_THINKING_STRUCT ((struct AI_ThinkingStruct *)(gBattleResources->ai))
#define AI_DATA ((struct AiLogicData *)(gBattleResources->aiData))
#define AI_PARTY ((struct AIPartyData *)(gBattleResources->aiParty))
#define BATTLE_HISTORY ((struct BattleHistory *)(gBattleResources->battleHistory))
struct BattleResults

View File

@ -24,6 +24,8 @@ void BattleAI_SetupItems(void);
void BattleAI_SetupFlags(void);
void BattleAI_SetupAIData(u8 defaultScoreMoves);
u8 BattleAI_ChooseMoveOrAction(void);
void Ai_InitPartyStruct(void);
void Ai_UpdateSwitchInData(u32 battler);
void GetAiLogicData(void);
extern u8 sBattler_AI;

View File

@ -237,6 +237,66 @@ u8 ComputeBattleAiScores(u8 battler)
return BattleAI_ChooseMoveOrAction();
}
static void CopyBattlerDataToAIParty(u32 bPosition, u32 side)
{
u32 battler = GetBattlerAtPosition(bPosition);
struct AiPartyMon *aiMon = &AI_PARTY->mons[side][gBattlerPartyIndexes[battler]];
struct BattlePokemon *bMon = &gBattleMons[battler];
aiMon->species = bMon->species;
aiMon->level = bMon->level;
aiMon->gender = GetGenderFromSpeciesAndPersonality(bMon->species, bMon->personality);
aiMon->isFainted = FALSE;
aiMon->wasSentInBattle = TRUE;
aiMon->switchInCount++;
}
void Ai_InitPartyStruct(void)
{
AI_PARTY->count[B_SIDE_PLAYER] = gPlayerPartyCount;
AI_PARTY->count[B_SIDE_OPPONENT] = gEnemyPartyCount;
// Save first 2 or 4(in doubles) mons
CopyBattlerDataToAIParty(B_POSITION_PLAYER_LEFT, B_SIDE_PLAYER);
if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE)
CopyBattlerDataToAIParty(B_POSITION_PLAYER_RIGHT, B_SIDE_PLAYER);
// If player's partner is AI, save opponent mons
if (gBattleTypeFlags & BATTLE_TYPE_INGAME_PARTNER)
{
CopyBattlerDataToAIParty(B_POSITION_OPPONENT_LEFT, B_SIDE_OPPONENT);
CopyBattlerDataToAIParty(B_POSITION_OPPONENT_RIGHT, B_SIDE_OPPONENT);
}
}
void Ai_UpdateSwitchInData(u32 battler)
{
u32 i;
u32 side = GetBattlerSide(battler);
struct AiPartyMon *aiMon = &AI_PARTY->mons[side][gBattlerPartyIndexes[battler]];
// See if the switched-in mon has been already in battle
if (aiMon->wasSentInBattle)
{
if (aiMon->ability)
BATTLE_HISTORY->abilities[battler] = aiMon->ability;
if (aiMon->heldEffect)
BATTLE_HISTORY->itemEffects[battler] = aiMon->heldEffect;
for (i = 0; i < MAX_MON_MOVES; i++)
{
if (aiMon->moves[i])
BATTLE_HISTORY->usedMoves[battler][i] = aiMon->moves[i];
}
}
else // If not, copy the newly switched-in mon in battle and clear battle history.
{
ClearBattlerMoveHistory(battler);
ClearBattlerAbilityHistory(battler);
ClearBattlerItemEffectHistory(battler);
CopyBattlerDataToAIParty(GetBattlerPosition(battler), side);
}
}
static void SetBattlerAiData(u8 battlerId)
{
AI_DATA->abilities[battlerId] = AI_GetAbility(battlerId);

View File

@ -499,6 +499,7 @@ void RecordKnownMove(u8 battlerId, u32 move)
if (BATTLE_HISTORY->usedMoves[battlerId][i] == MOVE_NONE)
{
BATTLE_HISTORY->usedMoves[battlerId][i] = move;
AI_PARTY->mons[GetBattlerSide(battlerId)][gBattlerPartyIndexes[battlerId]].moves[i] = move;
break;
}
}
@ -507,6 +508,7 @@ void RecordKnownMove(u8 battlerId, u32 move)
void RecordAbilityBattle(u8 battlerId, u16 abilityId)
{
BATTLE_HISTORY->abilities[battlerId] = abilityId;
AI_PARTY->mons[GetBattlerSide(battlerId)][gBattlerPartyIndexes[battlerId]].ability = abilityId;
}
void ClearBattlerAbilityHistory(u8 battlerId)
@ -517,6 +519,7 @@ void ClearBattlerAbilityHistory(u8 battlerId)
void RecordItemEffectBattle(u8 battlerId, u8 itemEffect)
{
BATTLE_HISTORY->itemEffects[battlerId] = itemEffect;
AI_PARTY->mons[GetBattlerSide(battlerId)][gBattlerPartyIndexes[battlerId]].heldEffect = itemEffect;
}
void ClearBattlerItemEffectHistory(u8 battlerId)

View File

@ -43,8 +43,8 @@ struct BattleDebugModifyArrows
u16 minValue;
u16 maxValue;
int currValue;
u8 currentDigit;
u8 maxDigits;
u8 currentDigit:4;
u8 maxDigits:4;
u8 charDigits[MAX_MODIFY_DIGITS];
void *modifiedValPtr;
u8 typeOfVal;
@ -52,7 +52,9 @@ struct BattleDebugModifyArrows
struct BattleDebugMenu
{
u8 battlerId;
u8 battlerId:2;
u8 aiBattlerId:2;
u8 battlerWindowId;
u8 mainListWindowId;
@ -72,11 +74,16 @@ struct BattleDebugMenu
const struct BitfieldInfo *bitfield;
bool8 battlerWasChanged[MAX_BATTLERS_COUNT];
u8 aiBattlerId;
u8 aiViewState;
u8 aiIconSpriteIds[MAX_BATTLERS_COUNT];
u8 aiMonSpriteId;
u8 aiMovesWindowId;
union
{
u8 aiIconSpriteIds[MAX_BATTLERS_COUNT];
u8 aiPartyIcons[PARTY_SIZE];
} spriteIds;
};
struct __attribute__((__packed__)) BitfieldInfo
@ -102,6 +109,7 @@ enum
LIST_ITEM_AI,
LIST_ITEM_AI_MOVES_PTS,
LIST_ITEM_AI_INFO,
LIST_ITEM_AI_PARTY,
LIST_ITEM_VARIOUS,
LIST_ITEM_COUNT
};
@ -234,6 +242,7 @@ static const u8 sText_Unknown[] = _("Unknown");
static const u8 sText_InLove[] = _("In Love");
static const u8 sText_AIMovePts[] = _("AI Pts/Dmg");
static const u8 sText_AiKnowledge[] = _("AI Info");
static const u8 sText_AiParty[] = _("AI Party");
static const u8 sText_EffectOverride[] = _("Effect Override");
static const u8 sText_EmptyString[] = _("");
@ -340,6 +349,7 @@ static const struct ListMenuItem sMainListItems[] =
{sText_AI, LIST_ITEM_AI},
{sText_AIMovePts, LIST_ITEM_AI_MOVES_PTS},
{sText_AiKnowledge, LIST_ITEM_AI_INFO},
{sText_AiParty, LIST_ITEM_AI_PARTY},
{sText_Various, LIST_ITEM_VARIOUS},
};
@ -610,6 +620,7 @@ static void UpdateMonData(struct BattleDebugMenu *data);
static u8 *GetSideStatusValue(struct BattleDebugMenu *data, bool32 changeStatus, bool32 statusTrue);
static bool32 TryMoveDigit(struct BattleDebugModifyArrows *modArrows, bool32 moveUp);
static void SwitchToDebugView(u8 taskId);
static void SwitchToDebugViewFromAiParty(u8 taskId);
// code
static struct BattleDebugMenu *GetStructPtr(u8 taskId)
@ -725,9 +736,9 @@ static void PutMovesPointsText(struct BattleDebugMenu *data)
AddTextPrinterParameterized(data->aiMovesWindowId, 1, text, 0, i * 15, 0, NULL);
for (count = 0, j = 0; j < MAX_BATTLERS_COUNT; j++)
{
if (data->aiIconSpriteIds[j] == 0xFF)
if (data->spriteIds.aiIconSpriteIds[j] == 0xFF)
continue;
battlerDef = gSprites[data->aiIconSpriteIds[j]].data[0];
battlerDef = gSprites[data->spriteIds.aiIconSpriteIds[j]].data[0];
ConvertIntToDecimalStringN(text,
gBattleStruct->aiFinalScore[data->aiBattlerId][battlerDef][i],
STR_CONV_MODE_RIGHT_ALIGN, 3);
@ -772,20 +783,20 @@ static void Task_ShowAiPoints(u8 taskId)
if (i != data->aiBattlerId && IsBattlerAlive(i))
{
#ifndef POKEMON_EXPANSION
data->aiIconSpriteIds[i] = CreateMonIcon(gBattleMons[i].species,
data->spriteIds.aiIconSpriteIds[i] = CreateMonIcon(gBattleMons[i].species,
SpriteCallbackDummy,
95 + (count * 60), 17, 0, 0, FALSE);
#else
data->aiIconSpriteIds[i] = CreateMonIcon(gBattleMons[i].species,
data->spriteIds.aiIconSpriteIds[i] = CreateMonIcon(gBattleMons[i].species,
SpriteCallbackDummy,
95 + (count * 60), 17, 0, 0);
#endif
gSprites[data->aiIconSpriteIds[i]].data[0] = i; // battler id
gSprites[data->spriteIds.aiIconSpriteIds[i]].data[0] = i; // battler id
count++;
}
else
{
data->aiIconSpriteIds[i] = 0xFF;
data->spriteIds.aiIconSpriteIds[i] = 0xFF;
}
}
#ifndef POKEMON_EXPANSION
@ -837,6 +848,7 @@ static const u8 *const sAiInfoItemNames[] =
sText_HeldItem,
sText_HoldEffect,
};
static void PutAiInfoText(struct BattleDebugMenu *data)
{
u32 i, j, count;
@ -869,6 +881,31 @@ static void PutAiInfoText(struct BattleDebugMenu *data)
free(text);
}
static void PutAiPartyText(struct BattleDebugMenu *data)
{
u32 i, j, count, maxWidth;
u8 *text = malloc(0x50), *txtPtr;
struct AiPartyMon *aiMons = AI_PARTY->mons[GET_BATTLER_SIDE(data->aiBattlerId)];
FillWindowPixelBuffer(data->aiMovesWindowId, 0x11);
count = AI_PARTY->count[GET_BATTLER_SIDE(data->aiBattlerId)];
for (i = 0; i < count; i++)
{
txtPtr = StringCopyN(text, gAbilityNames[aiMons[i].ability], 7); // The screen is too small to fit the whole string, so we need to drop the last letters.
*txtPtr = EOS;
AddTextPrinterParameterized5(data->aiMovesWindowId, FONT_SMALL_NARROW, text, i * 41, 0, 0, NULL, 0, 0);
for (j = 0; j < MAX_MON_MOVES; j++)
{
txtPtr = StringCopyN(text, gMoveNames[aiMons[i].moves[j]], 8);
*txtPtr = EOS;
AddTextPrinterParameterized5(data->aiMovesWindowId, FONT_SMALL_NARROW, text, i * 41, 20 + j * 15, 0, NULL, 0, 0);
}
}
CopyWindowToVram(data->aiMovesWindowId, 3);
free(text);
}
static void Task_ShowAiKnowledge(u8 taskId)
{
u32 i, count;
@ -895,20 +932,20 @@ static void Task_ShowAiKnowledge(u8 taskId)
if (GET_BATTLER_SIDE(i) == B_SIDE_PLAYER && IsBattlerAlive(i))
{
#ifndef POKEMON_EXPANSION
data->aiIconSpriteIds[i] = CreateMonIcon(gBattleMons[i].species,
data->spriteIds.aiIconSpriteIds[i] = CreateMonIcon(gBattleMons[i].species,
SpriteCallbackDummy,
95 + (count * 80), 17, 0, 0, FALSE);
#else
data->aiIconSpriteIds[i] = CreateMonIcon(gBattleMons[i].species,
data->spriteIds.aiIconSpriteIds[i] = CreateMonIcon(gBattleMons[i].species,
SpriteCallbackDummy,
95 + (count * 80), 17, 0, 0);
#endif
gSprites[data->aiIconSpriteIds[i]].data[0] = i; // battler id
gSprites[data->spriteIds.aiIconSpriteIds[i]].data[0] = i; // battler id
count++;
}
else
{
data->aiIconSpriteIds[i] = 0xFF;
data->spriteIds.aiIconSpriteIds[i] = 0xFF;
}
}
#ifndef POKEMON_EXPANSION
@ -947,12 +984,82 @@ static void Task_ShowAiKnowledge(u8 taskId)
}
}
static void Task_ShowAiParty(u8 taskId)
{
u32 i;
struct WindowTemplate winTemplate;
struct AiPartyMon *aiMons;
struct BattleDebugMenu *data = GetStructPtr(taskId);
switch (data->aiViewState)
{
case 0:
HideBg(0);
ShowBg(1);
LoadMonIconPalettes();
data->aiBattlerId = data->battlerId;
aiMons = AI_PARTY->mons[GET_BATTLER_SIDE(data->aiBattlerId)];
for (i = 0; i < AI_PARTY->count[GET_BATTLER_SIDE(data->aiBattlerId)]; i++)
{
u16 species = SPECIES_OLD_UNOWN_B; // Question mark
if (aiMons[i].wasSentInBattle && aiMons[i].species)
species = aiMons[i].species;
data->spriteIds.aiPartyIcons[i] = CreateMonIcon(species, SpriteCallbackDummy, (i * 41) - 5 + 20, 7, 0, 0, FALSE);
}
for (; i < PARTY_SIZE; i++)
data->spriteIds.aiPartyIcons[i] = 0xFF;
data->aiViewState++;
break;
// Put text
case 1:
winTemplate = CreateWindowTemplate(1, 0, 4, 30, 14, 15, 0x200);
data->aiMovesWindowId = AddWindow(&winTemplate);
PutWindowTilemap(data->aiMovesWindowId);
PutAiPartyText(data);
data->aiViewState++;
break;
// Input
case 2:
if (gMain.newKeys & (SELECT_BUTTON | B_BUTTON))
{
SwitchToDebugViewFromAiParty(taskId);
HideBg(1);
ShowBg(0);
return;
}
break;
}
}
static void SwitchToAiInfoView(u8 taskId)
{
gTasks[taskId].func = Task_ShowAiKnowledge;
GetStructPtr(taskId)->aiViewState = 0;
}
static void SwitchToAiPartyView(u8 taskId)
{
gTasks[taskId].func = Task_ShowAiParty;
GetStructPtr(taskId)->aiViewState = 0;
}
static void SwitchToDebugViewFromAiParty(u8 taskId)
{
u32 i;
struct BattleDebugMenu *data = GetStructPtr(taskId);
FreeMonIconPalettes();
for (i = 0; i < PARTY_SIZE; i++)
{
if (data->spriteIds.aiPartyIcons[i] != 0xFF)
FreeAndDestroyMonIconSprite(&gSprites[data->spriteIds.aiPartyIcons[i]]);
}
RemoveWindow(data->aiMovesWindowId);
gTasks[taskId].func = Task_DebugMenuProcessInput;
}
static void SwitchToDebugView(u8 taskId)
{
u32 i;
@ -961,8 +1068,8 @@ static void SwitchToDebugView(u8 taskId)
FreeMonIconPalettes();
for (i = 0; i < MAX_BATTLERS_COUNT; i++)
{
if (data->aiIconSpriteIds[i] != 0xFF)
FreeAndDestroyMonIconSprite(&gSprites[data->aiIconSpriteIds[i]]);
if (data->spriteIds.aiIconSpriteIds[i] != 0xFF)
FreeAndDestroyMonIconSprite(&gSprites[data->spriteIds.aiIconSpriteIds[i]]);
}
FreeAndDestroyMonPicSprite(data->aiMonSpriteId);
RemoveWindow(data->aiMovesWindowId);
@ -1019,6 +1126,11 @@ static void Task_DebugMenuProcessInput(u8 taskId)
SwitchToAiInfoView(taskId);
return;
}
else if (listItemId == LIST_ITEM_AI_PARTY && gMain.newKeys & A_BUTTON)
{
SwitchToAiPartyView(taskId);
return;
}
data->currentMainListItemId = listItemId;
// Create the secondary menu list.

View File

@ -3115,9 +3115,7 @@ void SwitchInClearSetData(void)
gSpecialStatuses[gActiveBattler].physicalDmg = 0;
gSpecialStatuses[gActiveBattler].specialDmg = 0;
ClearBattlerMoveHistory(gActiveBattler);
ClearBattlerAbilityHistory(gActiveBattler);
ClearBattlerItemEffectHistory(gActiveBattler);
Ai_UpdateSwitchInData(gActiveBattler);
}
void FaintClearSetData(void)
@ -3572,7 +3570,8 @@ static void DoBattleIntro(void)
gBattleStruct->switchInAbilitiesCounter = 0;
gBattleStruct->switchInItemsCounter = 0;
gBattleStruct->overworldWeatherDone = FALSE;
GetAiLogicData(); // 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;
}
break;
@ -3707,8 +3706,6 @@ static void TryDoEventsBeforeFirstTurn(void)
gRandomTurnNumber = Random();
GetAiLogicData(); // get assumed abilities, hold effects, etc of all battlers
if (gBattleTypeFlags & BATTLE_TYPE_ARENA)
{
StopCryAndClearCrySongs();

View File

@ -27,6 +27,7 @@ void AllocateBattleResources(void)
gBattleResources->beforeLvlUp = AllocZeroed(sizeof(*gBattleResources->beforeLvlUp));
gBattleResources->ai = AllocZeroed(sizeof(*gBattleResources->ai));
gBattleResources->aiData = AllocZeroed(sizeof(*gBattleResources->aiData));
gBattleResources->aiParty = AllocZeroed(sizeof(*gBattleResources->aiParty));
gBattleResources->battleHistory = AllocZeroed(sizeof(*gBattleResources->battleHistory));
gLinkBattleSendBuffer = AllocZeroed(BATTLE_BUFFER_LINK_SIZE);
@ -59,6 +60,7 @@ void FreeBattleResources(void)
FREE_AND_SET_NULL(gBattleResources->beforeLvlUp);
FREE_AND_SET_NULL(gBattleResources->ai);
FREE_AND_SET_NULL(gBattleResources->aiData);
FREE_AND_SET_NULL(gBattleResources->aiParty);
FREE_AND_SET_NULL(gBattleResources->battleHistory);
FREE_AND_SET_NULL(gBattleResources);

BIN
vbalink.ini Normal file

Binary file not shown.