summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--constants/map_object_constants.asm2
-rwxr-xr-xengine/events/std_collision.asm2
-rwxr-xr-xengine/overworld/player_movement.asm10
-rw-r--r--home.asm218
-rw-r--r--home/map.asm8
-rwxr-xr-xhome/trainers.asm254
-rw-r--r--wram.asm8
7 files changed, 272 insertions, 230 deletions
diff --git a/constants/map_object_constants.asm b/constants/map_object_constants.asm
index 55a3f358..2c28dd7e 100644
--- a/constants/map_object_constants.asm
+++ b/constants/map_object_constants.asm
@@ -111,7 +111,7 @@ ABSOLUTE_TILE_ID EQU 1 << ABSOLUTE_TILE_ID_F
const MAPOBJECT_FLAG_HI ; d
const MAPOBJECT_E ; unused
const MAPOBJECT_F ; unused
-OBJECT_LENGTH EQU const_value
+MAPOBJECT_LENGTH EQU const_value
; SpriteMovementData struct members (see data/sprites/map_objects.asm)
const_def
diff --git a/engine/events/std_collision.asm b/engine/events/std_collision.asm
index 932f4db8..bb44687c 100755
--- a/engine/events/std_collision.asm
+++ b/engine/events/std_collision.asm
@@ -12,7 +12,7 @@ CheckFacingTileForStdScript::
ld a, [hli]
ld [wcf2b], a
ld a, [hli]
- ld [wTempTrainerHeader], a
+ ld [wTempTrainer], a
ld a, BANK(Script_JumpStdFromRAM)
ld hl, Script_JumpStdFromRAM
call CallScript
diff --git a/engine/overworld/player_movement.asm b/engine/overworld/player_movement.asm
index c919f000..3fa38cc0 100755
--- a/engine/overworld/player_movement.asm
+++ b/engine/overworld/player_movement.asm
@@ -87,7 +87,7 @@ Function1002d: ; 1002d (4:402d)
ld a, [wcf2e]
cp $ff
jr z, .asm_100b2
- ld a, [wTempTrainerHeader]
+ ld a, [wTempTrainer]
and a
jr nz, .asm_100ad
call Function103ee
@@ -367,7 +367,7 @@ Function10226: ; 10226 (4:4226)
cp [hl]
jr nz, .asm_10259
ld a, $1
- ld [wTempTrainerHeader], a
+ ld [wTempTrainer], a
ld a, [wcf2e]
cp $ff
jr z, .asm_10259
@@ -476,7 +476,7 @@ Function102b3: ; 102b3 (4:42b3)
Function102bf: ; 102bf (4:42bf)
ld a, $0
- ld [wTempTrainerHeaderEnd], a
+ ld [wTempTrainerEnd], a
ld a, $50
ld [wcf2d], a
xor a
@@ -485,7 +485,7 @@ Function102bf: ; 102bf (4:42bf)
Function102cb: ; 102cb (4:42cb)
call Function10404
ret nc
- ld a, [wTempTrainerHeaderEnd]
+ ld a, [wTempTrainerEnd]
.asm_102d2
cp $0
ret z
@@ -705,7 +705,7 @@ Function103f9: ; 103f9 (4:43f9)
ret
Function10404: ; 10404 (4:4404)
- ld a, [wTempTrainerHeaderEnd]
+ ld a, [wTempTrainerEnd]
cp $0
jr z, .asm_10420
cp $f0
diff --git a/home.asm b/home.asm
index 6daab178..4f9f2a71 100644
--- a/home.asm
+++ b/home.asm
@@ -259,223 +259,7 @@ INCLUDE "home/pokedex_flags.asm"
INCLUDE "home/names.asm"
INCLUDE "home/scrolling_menu.asm"
INCLUDE "home/stone_queue.asm"
-
-CheckTrainerBattle2::
- ldh a, [hROMBank]
- push af
- call SwitchToMapScriptsBank
- call CheckTrainerBattle
- pop bc
- ld a, b
- rst Bankswitch
- ret
-
-CheckTrainerBattle:: ; 3851 (0:3851)
- ld a, $2
- ld de, wMap2Object
-.asm_3856
- push af
- push de
- ld hl, $1
- add hl, de
- ld a, [hl]
- and a
- jr z, .asm_389b
- ld hl, $8
- add hl, de
- ld a, [hl]
- and $f
- cp $2
- jr nz, .asm_389b
- ld hl, MBC3SRamEnable
- add hl, de
- ld a, [hl]
- cp $ff
- jr z, .asm_389b
- call GetObjectStruct
- call FacingPlayerDistance_bc
- jr nc, .asm_389b
- ld hl, $9
- add hl, de
- ld a, [hl]
- cp b
- jr c, .asm_389b
- push bc
- push de
- ld hl, $a
- add hl, de
- ld a, [hli]
- ld h, [hl]
- ld l, a
- ld e, [hl]
- inc hl
- ld d, [hl]
- ld b, $2
- call EventFlagAction
- ld a, c
- pop de
- pop bc
- and a
- jr z, .asm_38aa
-.asm_389b
- pop de
- ld hl, $10
- add hl, de
- ld d, h
- ld e, l
- pop af
- inc a
- cp $10
- jr nz, .asm_3856
- xor a
- ret
-
-.asm_38aa
- pop de
- pop af
- ldh [hLastTalked], a
- ld a, b
- ld [wcf2a], a
- ld a, c
- ld [wcf2b], a
- jr continue_trainer_function
-
-TalkToTrainer::
- ld a, $1
- ld [wcf2a], a
- ld a, $ff
- ld [wcf2b], a
-continue_trainer_function
- call GetMapScriptHeaderBank
- ld [wcf29], a
- ldh a, [hLastTalked]
- call GetMapObject
- ld hl, $a
- add hl, bc
- ld a, [wcf29]
- call GetFarHalfword
- ld de, wcf2c
- ld bc, $d
- ld a, [wcf29]
- call FarCopyBytes
- xor a
- ld [wcf38], a
- scf
- ret
-
-FacingPlayerDistance_bc:: ; 38e9 (0:38e9)
- push de
- call FacingPlayerDistance
- ld b, d
- ld c, e
- pop de
- ret
-
-FacingPlayerDistance:: ; 38f1 (0:38f1)
- ld hl, $10
- add hl, bc
- ld d, [hl]
- ld hl, $11
- add hl, bc
- ld e, [hl]
- ld a, [wPlayerStandingMapX]
- cp d
- jr z, .asm_3909
- ld a, [wPlayerStandingMapY]
- cp e
- jr z, .asm_391d
- and a
- ret
-
-.asm_3909
- ld a, [wPlayerStandingMapY]
- sub e
- jr z, .asm_3937
- jr nc, .asm_3918
- cpl
- inc a
- ld d, a
- ld e, $4
- jr .asm_392f
-
-.asm_3918
- ld d, a
- ld e, $0
- jr .asm_392f
-
-.asm_391d
- ld a, [wPlayerStandingMapX]
- sub d
- jr z, .asm_3937
- jr nc, .asm_392c
- cpl
- inc a
- ld d, a
- ld e, $8
- jr .asm_392f
-
-.asm_392c
- ld d, a
- ld e, $c
-.asm_392f
- call GetSpriteDirection
- cp e
- jr nz, .asm_3937
- scf
- ret
-
-.asm_3937
- and a
- ret
-
-CheckTrainerFlag::
- push bc
- ld hl, $1
- add hl, bc
- ld a, [hl]
- call GetMapObject
- ld hl, $a
- add hl, bc
- ld a, [hli]
- ld h, [hl]
- ld l, a
- call GetMapScriptHeaderBank
- call GetFarHalfword
- ld d, h
- ld e, l
- push de
- ld b, $2
- call EventFlagAction
- pop de
- ld a, c
- and a
- pop bc
- ret
-
-PrintWinLossText::
- ld a, [wBattleType]
- cp $1
- jr which_battle_end_text
-
-PrintWinText::
- ld hl, wWinTextPointer
- jr continue_battle_end_text
-
-which_battle_end_text
- ld a, [wBattleResult]
- ld hl, wWinTextPointer
- and a
- jr z, continue_battle_end_text
- ld hl, wLossTextPointer
-continue_battle_end_text
- ld a, [hli]
- ld h, [hl]
- ld l, a
- call GetMapScriptHeaderBank
- call FarPrintText
- call WaitBGMap
- call WaitPressAorB_BlinkCursor
- ret
+INCLUDE "home/trainers.asm"
DrawBattleHPBar::
push hl
diff --git a/home/map.asm b/home/map.asm
index d1c522d8..5a35ffab 100644
--- a/home/map.asm
+++ b/home/map.asm
@@ -947,7 +947,7 @@ ClearObjectStructs:: ; 2550 (0:2550)
ret
RestoreFacingAfterWarp::
- call GetMapScriptHeaderBank
+ call GetMapScriptsBank
rst Bankswitch
ld hl, wd08f
ld a, [hli]
@@ -1253,7 +1253,7 @@ CallMapScript::
ld a, [wd15f]
and a
ret nz
- call GetMapScriptHeaderBank
+ call GetMapScriptsBank
jr CallScript
RunMapCallback:: ; 2764 (0:2764)
@@ -1263,7 +1263,7 @@ RunMapCallback:: ; 2764 (0:2764)
call SwitchToMapScriptsBank
call FindCallback
jr nc, .asm_2779
- call GetMapScriptHeaderBank
+ call GetMapScriptsBank
ld b, a
ld d, h
ld e, l
@@ -2256,7 +2256,7 @@ SwitchToMapScriptsBank:: ; 2d2b (0:2d2b)
rst Bankswitch
ret
-GetMapScriptHeaderBank:: ; 2d30 (0:2d30)
+GetMapScriptsBank:: ; 2d30 (0:2d30)
ld a, [wd08c]
ret
diff --git a/home/trainers.asm b/home/trainers.asm
new file mode 100755
index 00000000..c86c8aeb
--- /dev/null
+++ b/home/trainers.asm
@@ -0,0 +1,254 @@
+CheckTrainerBattle::
+ ldh a, [hROMBank]
+ push af
+
+ call SwitchToMapScriptsBank
+ call _CheckTrainerBattle
+
+ pop bc
+ ld a, b
+ rst Bankswitch
+ ret
+
+_CheckTrainerBattle::
+; Check if any trainer on the map sees the player and wants to battle.
+
+; Skip the player object.
+ ld a, 2
+ ld de, wMap2Object
+
+.loop
+
+; Start a battle if the object:
+ push af
+ push de
+
+; Has a sprite
+ ld hl, MAPOBJECT_SPRITE
+ add hl, de
+ ld a, [hl]
+ and a
+ jr z, .next
+
+; Is a trainer
+ ld hl, MAPOBJECT_COLOR
+ add hl, de
+ ld a, [hl]
+ and $f
+ cp OBJECTTYPE_TRAINER
+ jr nz, .next
+
+; Is visible on the map
+ ld hl, MAPOBJECT_OBJECT_STRUCT_ID
+ add hl, de
+ ld a, [hl]
+ cp -1
+ jr z, .next
+
+; Is facing the player...
+ call GetObjectStruct
+ call FacingPlayerDistance_bc
+ jr nc, .next
+
+; ...within their sight range
+ ld hl, MAPOBJECT_RANGE
+ add hl, de
+ ld a, [hl]
+ cp b
+ jr c, .next
+
+; And hasn't already been beaten
+ push bc
+ push de
+ ld hl, MAPOBJECT_SCRIPT_POINTER
+ add hl, de
+ ld a, [hli]
+ ld h, [hl]
+ ld l, a
+ ld e, [hl]
+ inc hl
+ ld d, [hl]
+ ld b, CHECK_FLAG
+ call EventFlagAction
+ ld a, c
+ pop de
+ pop bc
+ and a
+ jr z, .startbattle
+
+.next
+ pop de
+ ld hl, MAPOBJECT_LENGTH
+ add hl, de
+ ld d, h
+ ld e, l
+
+ pop af
+ inc a
+ cp NUM_OBJECTS
+ jr nz, .loop
+ xor a
+ ret
+
+.startbattle
+ pop de
+ pop af
+ ldh [hLastTalked], a
+ ld a, b
+ ld [wSeenTrainerDistance], a
+ ld a, c
+ ld [wSeenTrainerDirection], a
+ jr LoadTrainer_continue
+
+TalkToTrainer::
+ ld a, 1
+ ld [wSeenTrainerDistance], a
+ ld a, -1
+ ld [wSeenTrainerDirection], a
+
+LoadTrainer_continue::
+ call GetMapScriptsBank
+ ld [wSeenTrainerBank], a
+
+ ldh a, [hLastTalked]
+ call GetMapObject
+
+ ld hl, MAPOBJECT_SCRIPT_POINTER
+ add hl, bc
+ ld a, [wSeenTrainerBank]
+ call GetFarHalfword
+ ld de, wTempTrainer
+ ld bc, wTempTrainerEnd - wTempTrainer
+ ld a, [wSeenTrainerBank]
+ call FarCopyBytes
+ xor a
+ ld [wRunningTrainerBattleScript], a
+ scf
+ ret
+
+FacingPlayerDistance_bc::
+ push de
+ call FacingPlayerDistance
+ ld b, d
+ ld c, e
+ pop de
+ ret
+
+FacingPlayerDistance::
+; Return carry if the sprite at bc is facing the player,
+; its distance in d, and its direction in e.
+
+ ld hl, OBJECT_NEXT_MAP_X ; x
+ add hl, bc
+ ld d, [hl]
+
+ ld hl, OBJECT_NEXT_MAP_Y ; y
+ add hl, bc
+ ld e, [hl]
+
+ ld a, [wPlayerStandingMapX]
+ cp d
+ jr z, .CheckY
+
+ ld a, [wPlayerStandingMapY]
+ cp e
+ jr z, .CheckX
+
+ and a
+ ret
+
+.CheckY:
+ ld a, [wPlayerStandingMapY]
+ sub e
+ jr z, .NotFacing
+ jr nc, .Above
+
+; Below
+ cpl
+ inc a
+ ld d, a
+ ld e, OW_UP
+ jr .CheckFacing
+
+.Above:
+ ld d, a
+ ld e, OW_DOWN
+ jr .CheckFacing
+
+.CheckX:
+ ld a, [wPlayerStandingMapX]
+ sub d
+ jr z, .NotFacing
+ jr nc, .Left
+
+; Right
+ cpl
+ inc a
+ ld d, a
+ ld e, OW_LEFT
+ jr .CheckFacing
+
+.Left:
+ ld d, a
+ ld e, OW_RIGHT
+
+.CheckFacing:
+ call GetSpriteDirection
+ cp e
+ jr nz, .NotFacing
+ scf
+ ret
+
+.NotFacing:
+ and a
+ ret
+
+CheckTrainerFlag::
+ push bc
+ ld hl, OBJECT_MAP_OBJECT_INDEX
+ add hl, bc
+ ld a, [hl]
+ call GetMapObject
+ ld hl, MAPOBJECT_SCRIPT_POINTER
+ add hl, bc
+ ld a, [hli]
+ ld h, [hl]
+ ld l, a
+ call GetMapScriptsBank
+ call GetFarHalfword
+ ld d, h
+ ld e, l
+ push de
+ ld b, CHECK_FLAG
+ call EventFlagAction
+ pop de
+ ld a, c
+ and a
+ pop bc
+ ret
+
+PrintWinLossText::
+ ld a, [wBattleType]
+ cp BATTLETYPE_CANLOSE
+ jr .canlose ; ??????????
+
+; unused
+ ld hl, wWinTextPointer
+ jr .ok
+
+.canlose
+ ld a, [wBattleResult]
+ ld hl, wWinTextPointer
+ and a ; WIN?
+ jr z, .ok
+ ld hl, wLossTextPointer
+
+.ok
+ ld a, [hli]
+ ld h, [hl]
+ ld l, a
+ call GetMapScriptsBank
+ call FarPrintText
+ call WaitBGMap
+ call WaitPressAorB_BlinkCursor
+ ret \ No newline at end of file
diff --git a/wram.asm b/wram.asm
index 2c16d97f..e652a518 100644
--- a/wram.asm
+++ b/wram.asm
@@ -2328,10 +2328,13 @@ wcf25:: ds 1 ; cf25
wcf26:: ds 1 ; cf26
wcf27:: ds 1 ; cf27
wcf28:: ds 1 ; cf28
+wSeenTrainerBank::
wcf29:: ds 1 ; cf29
+wSeenTrainerDistance::
wcf2a:: ds 1 ; cf2a
+wSeenTrainerDirection::
wcf2b:: ds 1 ; cf2b
-wTempTrainerHeader::
+wTempTrainer::
wcf2c:: ds 1 ; cf2c
wcf2d:: ds 1 ; cf2d
wcf2e:: ds 1 ; cf2e
@@ -2342,8 +2345,9 @@ wWinTextPointer:: dw ; cf32
wLossTextPointer:: dw ; cf34
wcf36:: ds 1 ; cf36
wcf37:: ds 1 ; cf37
+wRunningTrainerBattleScript::
wcf38:: ds 1 ; cf38
-wTempTrainerHeaderEnd::
+wTempTrainerEnd::
wcf39:: ds 1 ; cf39
wcf3a:: ds 1 ; cf3a
wcf3b:: ds 1 ; cf3b