Skip to content

Commit

Permalink
viableMove name change + 2 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexOn1ine committed Oct 5, 2023
1 parent 989e055 commit 07b57b0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
26 changes: 14 additions & 12 deletions src/battle_ai_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3169,9 +3169,9 @@ static u32 GetAIMostDamagingMoveId(u32 battlerAtk, u32 battlerDef)

static s32 AI_CompareDamagingMoves(u32 battlerAtk, u32 battlerDef, u32 currId)
{
u32 i, bestViableMove;
u32 i, bestViableMoveSc0re;
bool32 multipleBestMoves = FALSE;
u32 viableMoves[MAX_MON_MOVES] = {100, 100, 100, 100};
u32 viableMoveScores[MAX_MON_MOVES];
s32 noOfHits[MAX_MON_MOVES];
s32 score = 0;
s32 leastHits = 1000, leastHitsId = 0;
Expand All @@ -3188,11 +3188,13 @@ static s32 AI_CompareDamagingMoves(u32 battlerAtk, u32 battlerDef, u32 currId)
leastHits = noOfHits[i];
leastHitsId = i;
}
viableMoveScores[i] = 100;
isPowerfulIgnoredEffect[i] = IsInIgnoredPowerfulMoveEffects(gBattleMoves[moves[i]].effect);
}
else
{
noOfHits[i] = -1;
viableMoveScores[i] = 0;
isPowerfulIgnoredEffect[i] = FALSE;
}
/*
Expand All @@ -3218,26 +3220,26 @@ static s32 AI_CompareDamagingMoves(u32 battlerAtk, u32 battlerDef, u32 currId)
multipleBestMoves = TRUE;
// We need to make sure it's the current move which is objectively better.
if (isPowerfulIgnoredEffect[i] && !isPowerfulIgnoredEffect[currId])
viableMoves[i] -= 3;
viableMoveScores[i] -= 3;
else if (!isPowerfulIgnoredEffect[i] && isPowerfulIgnoredEffect[currId])
viableMoves[currId] -= 3;
viableMoveScores[currId] -= 3;

switch (CompareMoveAccuracies(battlerAtk, battlerDef, currId, i))
{
case 0:
viableMoves[i] -= 2;
viableMoveScores[i] -= 2;
break;
case 1:
viableMoves[currId] -= 2;
viableMoveScores[currId] -= 2;
break;
}
switch (AI_WhichMoveBetter(moves[currId], moves[i], battlerAtk, battlerDef, noOfHits[currId]))
{
case 0:
viableMoves[i] -= 1;
viableMoveScores[i] -= 1;
break;
case 1:
viableMoves[currId] -= 1;
viableMoveScores[currId] -= 1;
break;
}
}
Expand All @@ -3247,14 +3249,14 @@ static s32 AI_CompareDamagingMoves(u32 battlerAtk, u32 battlerDef, u32 currId)
ADJUST_SCORE(1);
else
{
bestViableMove = 0;
bestViableMoveSc0re = 0;
for (i = 0; i < MAX_MON_MOVES; i++)
{
if (viableMoves[i] > bestViableMove)
bestViableMove = viableMoves[i];
if (viableMoveScores[i] > bestViableMoveSc0re)
bestViableMoveSc0re = viableMoveScores[i];
}
// Unless a better move was found increase score of current move
if (viableMoves[currId] == bestViableMove)
if (viableMoveScores[currId] == bestViableMoveSc0re)
ADJUST_SCORE(1);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/data/trainer_parties.h
Original file line number Diff line number Diff line change
Expand Up @@ -3207,10 +3207,10 @@ static const struct TrainerMon sParty_Drake[] = {
static const struct TrainerMon sParty_Roxanne1[] = {
{
.iv = TRAINER_PARTY_IVS(12, 12, 12, 12, 12, 12),
.lvl = 12,
.species = SPECIES_GEODUDE,
.lvl = 100,
.species = SPECIES_VICTINI,
.heldItem = ITEM_NONE,
.moves = {MOVE_TACKLE, MOVE_DEFENSE_CURL, MOVE_ROCK_THROW, MOVE_ROCK_TOMB}
.moves = {MOVE_SLUDGE_BOMB, MOVE_THUNDERBOLT, MOVE_TAKE_DOWN}
},
{
.iv = TRAINER_PARTY_IVS(12, 12, 12, 12, 12, 12),
Expand Down
27 changes: 27 additions & 0 deletions test/battle/ai.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,33 @@ AI_SINGLE_BATTLE_TEST("AI prefers a weaker move over a one with a downside effec
}
}

AI_SINGLE_BATTLE_TEST("AI prefers moves with the best possible score, chosen randomly if tied")
{
GIVEN {
AI_FLAGS(AI_FLAG_CHECK_BAD_MOVE | AI_FLAG_CHECK_VIABILITY | AI_FLAG_TRY_TO_FAINT);
PLAYER(SPECIES_WOBBUFFET) { HP(5); };
PLAYER(SPECIES_WOBBUFFET);
OPPONENT(SPECIES_WOBBUFFET) { Moves(MOVE_THUNDERBOLT, MOVE_SLUDGE_BOMB, MOVE_TAKE_DOWN); }
} WHEN {
TURN { EXPECT_MOVES(opponent, MOVE_THUNDERBOLT, MOVE_SLUDGE_BOMB); SEND_OUT(player, 1); }
}
SCENE {
MESSAGE("Wobbuffet fainted!");
}
}

AI_SINGLE_BATTLE_TEST("AI can choose a stat boosting move randomly")
{
GIVEN {
AI_FLAGS(AI_FLAG_CHECK_BAD_MOVE | AI_FLAG_CHECK_VIABILITY | AI_FLAG_TRY_TO_FAINT);
PLAYER(SPECIES_WOBBUFFET);
PLAYER(SPECIES_WOBBUFFET);
OPPONENT(SPECIES_WOBBUFFET) { Moves(MOVE_THUNDERBOLT, MOVE_SLUDGE_BOMB, MOVE_SWORDS_DANCE); }
} WHEN {
TURN { EXPECT_MOVES(opponent, MOVE_THUNDERBOLT, MOVE_SLUDGE_BOMB, MOVE_SWORDS_DANCE); }
}
}

AI_SINGLE_BATTLE_TEST("AI chooses the safest option to faint the target, taking into account accuracy and move effect")
{
u16 move1 = MOVE_NONE, move2 = MOVE_NONE, move3 = MOVE_NONE, move4 = MOVE_NONE;
Expand Down

0 comments on commit 07b57b0

Please sign in to comment.