2015-11-28 20:13:40 +00:00
|
|
|
AIChooseMove: ; 440ce
|
2018-01-23 22:39:09 +00:00
|
|
|
; Score each move in wEnemyMonMoves starting from wBuffer1. Lower is better.
|
2015-11-28 20:13:40 +00:00
|
|
|
; Pick the move with the lowest score.
|
|
|
|
|
|
|
|
; Wildmons attack at random.
|
|
|
|
ld a, [wBattleMode]
|
|
|
|
dec a
|
|
|
|
ret z
|
|
|
|
|
|
|
|
ld a, [wLinkMode]
|
|
|
|
and a
|
|
|
|
ret nz
|
|
|
|
|
|
|
|
; No use picking a move if there's no choice.
|
2017-12-24 17:47:30 +00:00
|
|
|
farcall CheckEnemyLockedIn
|
2015-11-28 20:13:40 +00:00
|
|
|
ret nz
|
|
|
|
|
|
|
|
|
|
|
|
; The default score is 20. Unusable moves are given a score of 80.
|
|
|
|
ld a, 20
|
2018-01-23 22:39:09 +00:00
|
|
|
ld hl, wBuffer1
|
2015-11-28 20:13:40 +00:00
|
|
|
ld [hli], a
|
2016-05-10 16:31:49 +00:00
|
|
|
ld [hli], a
|
|
|
|
ld [hli], a
|
2015-11-28 20:13:40 +00:00
|
|
|
ld [hl], a
|
|
|
|
|
|
|
|
; Don't pick disabled moves.
|
2018-01-23 22:39:09 +00:00
|
|
|
ld a, [wEnemyDisabledMove]
|
2015-11-28 20:13:40 +00:00
|
|
|
and a
|
|
|
|
jr z, .CheckPP
|
|
|
|
|
2018-01-23 22:39:09 +00:00
|
|
|
ld hl, wEnemyMonMoves
|
2015-11-28 20:13:40 +00:00
|
|
|
ld c, 0
|
2016-04-10 18:42:14 +00:00
|
|
|
.CheckDisabledMove:
|
2015-11-28 20:13:40 +00:00
|
|
|
cp [hl]
|
|
|
|
jr z, .ScoreDisabledMove
|
|
|
|
inc c
|
|
|
|
inc hl
|
|
|
|
jr .CheckDisabledMove
|
2016-04-10 18:42:14 +00:00
|
|
|
.ScoreDisabledMove:
|
2018-01-23 22:39:09 +00:00
|
|
|
ld hl, wBuffer1
|
2015-11-28 20:13:40 +00:00
|
|
|
ld b, 0
|
|
|
|
add hl, bc
|
|
|
|
ld [hl], 80
|
|
|
|
|
|
|
|
; Don't pick moves with 0 PP.
|
2016-04-10 18:42:14 +00:00
|
|
|
.CheckPP:
|
2018-01-23 22:39:09 +00:00
|
|
|
ld hl, wBuffer1 - 1
|
|
|
|
ld de, wEnemyMonPP
|
2015-11-28 20:13:40 +00:00
|
|
|
ld b, 0
|
2016-04-10 18:42:14 +00:00
|
|
|
.CheckMovePP:
|
2015-11-28 20:13:40 +00:00
|
|
|
inc b
|
|
|
|
ld a, b
|
2018-01-23 22:39:09 +00:00
|
|
|
cp wEnemyMonMovesEnd - wEnemyMonMoves + 1
|
2015-11-28 20:13:40 +00:00
|
|
|
jr z, .ApplyLayers
|
|
|
|
inc hl
|
|
|
|
ld a, [de]
|
|
|
|
inc de
|
2018-01-11 17:00:01 +00:00
|
|
|
and PP_MASK
|
2015-11-28 20:13:40 +00:00
|
|
|
jr nz, .CheckMovePP
|
|
|
|
ld [hl], 80
|
|
|
|
jr .CheckMovePP
|
|
|
|
|
|
|
|
|
|
|
|
; Apply AI scoring layers depending on the trainer class.
|
2016-04-10 18:42:14 +00:00
|
|
|
.ApplyLayers:
|
2015-11-28 20:13:40 +00:00
|
|
|
ld hl, TrainerClassAttributes + TRNATTR_AI_MOVE_WEIGHTS
|
|
|
|
|
2018-01-30 19:05:25 +00:00
|
|
|
; If we have a battle in BattleTower just load the Attributes of the first trainer class in wTrainerClass (Falkner)
|
2015-11-28 20:13:40 +00:00
|
|
|
; so we have always the same AI, regardless of the loaded class of trainer
|
2018-01-23 22:39:09 +00:00
|
|
|
ld a, [wInBattleTowerBattle]
|
2015-11-28 20:13:40 +00:00
|
|
|
bit 0, a
|
|
|
|
jr nz, .battle_tower_skip
|
|
|
|
|
2018-01-23 22:39:09 +00:00
|
|
|
ld a, [wTrainerClass]
|
2015-11-28 20:13:40 +00:00
|
|
|
dec a
|
|
|
|
ld bc, 7 ; Trainer2AI - Trainer1AI
|
|
|
|
call AddNTimes
|
|
|
|
|
|
|
|
.battle_tower_skip
|
|
|
|
lb bc, CHECK_FLAG, 0
|
|
|
|
push bc
|
|
|
|
push hl
|
|
|
|
|
2016-04-10 18:42:14 +00:00
|
|
|
.CheckLayer:
|
2015-11-28 20:13:40 +00:00
|
|
|
pop hl
|
|
|
|
pop bc
|
|
|
|
|
|
|
|
ld a, c
|
|
|
|
cp 16 ; up to 16 scoring layers
|
|
|
|
jr z, .DecrementScores
|
|
|
|
|
|
|
|
push bc
|
|
|
|
ld d, BANK(TrainerClassAttributes)
|
2018-01-16 19:30:10 +00:00
|
|
|
predef SmallFarFlagAction
|
2015-11-28 20:13:40 +00:00
|
|
|
ld d, c
|
|
|
|
pop bc
|
|
|
|
|
|
|
|
inc c
|
|
|
|
push bc
|
|
|
|
push hl
|
|
|
|
|
|
|
|
ld a, d
|
|
|
|
and a
|
|
|
|
jr z, .CheckLayer
|
|
|
|
|
|
|
|
ld hl, AIScoringPointers
|
|
|
|
dec c
|
|
|
|
ld b, 0
|
|
|
|
add hl, bc
|
2016-05-04 15:46:23 +00:00
|
|
|
add hl, bc
|
2015-11-28 20:13:40 +00:00
|
|
|
ld a, [hli]
|
|
|
|
ld h, [hl]
|
|
|
|
ld l, a
|
|
|
|
ld a, BANK(AIScoring)
|
|
|
|
call FarCall_hl
|
|
|
|
|
|
|
|
jr .CheckLayer
|
|
|
|
|
|
|
|
; Decrement the scores of all moves one by one until one reaches 0.
|
2016-04-10 18:42:14 +00:00
|
|
|
.DecrementScores:
|
2018-01-23 22:39:09 +00:00
|
|
|
ld hl, wBuffer1
|
|
|
|
ld de, wEnemyMonMoves
|
|
|
|
ld c, wEnemyMonMovesEnd - wEnemyMonMoves
|
2015-11-28 20:13:40 +00:00
|
|
|
|
2016-04-10 18:42:14 +00:00
|
|
|
.DecrementNextScore:
|
2015-11-28 20:13:40 +00:00
|
|
|
; If the enemy has no moves, this will infinite.
|
|
|
|
ld a, [de]
|
|
|
|
inc de
|
|
|
|
and a
|
|
|
|
jr z, .DecrementScores
|
|
|
|
|
|
|
|
; We are done whenever a score reaches 0
|
|
|
|
dec [hl]
|
|
|
|
jr z, .PickLowestScoreMoves
|
|
|
|
|
|
|
|
; If we just decremented the fourth move's score, go back to the first move
|
|
|
|
inc hl
|
|
|
|
dec c
|
|
|
|
jr z, .DecrementScores
|
|
|
|
|
|
|
|
jr .DecrementNextScore
|
|
|
|
|
|
|
|
; In order to avoid bias towards the moves located first in memory, increment the scores
|
|
|
|
; that were decremented one more time than the rest (in case there was a tie).
|
|
|
|
; This means that the minimum score will be 1.
|
2016-04-10 18:42:14 +00:00
|
|
|
.PickLowestScoreMoves:
|
2015-11-28 20:13:40 +00:00
|
|
|
ld a, c
|
|
|
|
|
|
|
|
.move_loop
|
|
|
|
inc [hl]
|
|
|
|
dec hl
|
|
|
|
inc a
|
|
|
|
cp NUM_MOVES + 1
|
|
|
|
jr nz, .move_loop
|
|
|
|
|
2018-01-23 22:39:09 +00:00
|
|
|
ld hl, wBuffer1
|
|
|
|
ld de, wEnemyMonMoves
|
2015-11-28 20:13:40 +00:00
|
|
|
ld c, NUM_MOVES
|
|
|
|
|
2016-04-10 18:42:14 +00:00
|
|
|
; Give a score of 0 to a blank move
|
2015-11-28 20:13:40 +00:00
|
|
|
.loop2
|
|
|
|
ld a, [de]
|
|
|
|
and a
|
|
|
|
jr nz, .skip_load
|
|
|
|
ld [hl], a
|
|
|
|
|
2016-04-10 18:42:14 +00:00
|
|
|
; Disregard the move if its score is not 1
|
2015-11-28 20:13:40 +00:00
|
|
|
.skip_load
|
|
|
|
ld a, [hl]
|
|
|
|
dec a
|
|
|
|
jr z, .keep
|
|
|
|
xor a
|
|
|
|
ld [hli], a
|
|
|
|
jr .after_toss
|
|
|
|
|
|
|
|
.keep
|
|
|
|
ld a, [de]
|
|
|
|
ld [hli], a
|
|
|
|
.after_toss
|
|
|
|
inc de
|
|
|
|
dec c
|
|
|
|
jr nz, .loop2
|
|
|
|
|
2016-04-10 18:42:14 +00:00
|
|
|
; Randomly choose one of the moves with a score of 1
|
|
|
|
.ChooseMove:
|
2018-01-23 22:39:09 +00:00
|
|
|
ld hl, wBuffer1
|
2015-11-28 20:13:40 +00:00
|
|
|
call Random
|
2018-01-16 22:27:50 +00:00
|
|
|
maskbits NUM_MOVES
|
2015-11-28 20:13:40 +00:00
|
|
|
ld c, a
|
|
|
|
ld b, 0
|
|
|
|
add hl, bc
|
|
|
|
ld a, [hl]
|
|
|
|
and a
|
|
|
|
jr z, .ChooseMove
|
|
|
|
|
2018-01-23 22:39:09 +00:00
|
|
|
ld [wCurEnemyMove], a
|
2015-11-28 20:13:40 +00:00
|
|
|
ld a, c
|
2018-01-23 22:39:09 +00:00
|
|
|
ld [wCurEnemyMoveNum], a
|
2015-11-28 20:13:40 +00:00
|
|
|
ret
|
|
|
|
; 441af
|
|
|
|
|
|
|
|
|
|
|
|
AIScoringPointers: ; 441af
|
2018-01-26 01:34:33 +00:00
|
|
|
; entries correspond to AI_* constants
|
2015-11-28 20:13:40 +00:00
|
|
|
dw AI_Basic
|
|
|
|
dw AI_Setup
|
|
|
|
dw AI_Types
|
|
|
|
dw AI_Offensive
|
|
|
|
dw AI_Smart
|
|
|
|
dw AI_Opportunist
|
|
|
|
dw AI_Aggressive
|
|
|
|
dw AI_Cautious
|
|
|
|
dw AI_Status
|
|
|
|
dw AI_Risky
|
|
|
|
dw AI_None
|
|
|
|
dw AI_None
|
|
|
|
dw AI_None
|
|
|
|
dw AI_None
|
|
|
|
dw AI_None
|
|
|
|
dw AI_None
|
|
|
|
; 441cf
|