2014-09-10 03:55:19 +00:00
_GetSpritePosition1: ; 567f9 (15:67f9)
2014-05-22 22:13:20 +00:00
ld hl , wSpriteStateData1
ld de , $ 4
2014-09-13 07:50:56 +00:00
ld a , [ wSpriteIndex ]
2014-09-10 03:55:19 +00:00
ld [ H_SPRITEINDEX ], a
call GetSpriteDataPointer
2014-05-22 22:13:20 +00:00
ld a , [ hli ]
ld [ $ ffeb ], a
inc hl
ld a , [ hl ]
ld [ $ ffec ], a
ld de , $ fe
add hl , de
ld a , [ hli ]
ld [ $ ffed ], a
ld a , [ hl ]
ld [ $ ffee ], a
ret
2014-09-10 03:55:19 +00:00
_GetSpritePosition2: ; 56819 (15:6819)
2014-05-22 22:13:20 +00:00
ld hl , wSpriteStateData1
2014-09-10 03:55:19 +00:00
ld de , $ 4
2014-09-13 07:50:56 +00:00
ld a , [ wSpriteIndex ]
2014-09-10 03:55:19 +00:00
ld [ H_SPRITEINDEX ], a
call GetSpriteDataPointer
ld a , [ hli ] ; c1x4 (screen Y pos)
2014-05-26 21:04:56 +00:00
ld [ wd130 ], a
2014-05-22 22:13:20 +00:00
inc hl
2014-09-10 03:55:19 +00:00
ld a , [ hl ] ; c1x6 (screen X pos)
2014-05-26 21:04:56 +00:00
ld [ wd131 ], a
2014-09-10 03:55:19 +00:00
ld de , $ 104 - $ 6
2014-05-22 22:13:20 +00:00
add hl , de
2014-09-10 03:55:19 +00:00
ld a , [ hli ] ; c2x4 (map Y pos)
2014-05-26 21:04:56 +00:00
ld [ wd132 ], a
2014-09-10 03:55:19 +00:00
ld a , [ hl ] ; c2x5 (map X pos)
2014-05-26 21:04:56 +00:00
ld [ wd133 ], a
2014-05-22 22:13:20 +00:00
ret
2014-09-10 03:55:19 +00:00
_SetSpritePosition1: ; 5683d (15:683d)
2014-05-22 22:13:20 +00:00
ld hl , wSpriteStateData1
ld de , $ 4
2014-09-13 07:50:56 +00:00
ld a , [ wSpriteIndex ]
2014-09-10 03:55:19 +00:00
ld [ H_SPRITEINDEX ], a
call GetSpriteDataPointer
ld a , [ $ ffeb ] ; c1x4 (screen Y pos)
2014-05-22 22:13:20 +00:00
ld [ hli ], a
inc hl
2014-09-10 03:55:19 +00:00
ld a , [ $ ffec ] ; c1x6 (screen X pos)
2014-05-22 22:13:20 +00:00
ld [ hl ], a
2014-09-10 03:55:19 +00:00
ld de , $ 104 - $ 6
2014-05-22 22:13:20 +00:00
add hl , de
2014-09-10 03:55:19 +00:00
ld a , [ $ ffed ] ; c2x4 (map Y pos)
2014-05-22 22:13:20 +00:00
ld [ hli ], a
2014-09-10 03:55:19 +00:00
ld a , [ $ ffee ] ; c2x5 (map X pos)
2014-05-22 22:13:20 +00:00
ld [ hl ], a
ret
2014-09-10 03:55:19 +00:00
_SetSpritePosition2: ; 5685d (15:685d)
2014-05-22 22:13:20 +00:00
ld hl , wSpriteStateData1
ld de , $ 0004
2014-09-13 07:50:56 +00:00
ld a , [ wSpriteIndex ]
2014-09-10 03:55:19 +00:00
ld [ H_SPRITEINDEX ], a
call GetSpriteDataPointer
2014-05-26 21:04:56 +00:00
ld a , [ wd130 ]
2014-05-22 22:13:20 +00:00
ld [ hli ], a
inc hl
2014-05-26 21:04:56 +00:00
ld a , [ wd131 ]
2014-05-22 22:13:20 +00:00
ld [ hl ], a
ld de , $ 00 fe
add hl , de
2014-05-26 21:04:56 +00:00
ld a , [ wd132 ]
2014-05-22 22:13:20 +00:00
ld [ hli ], a
2014-05-26 21:04:56 +00:00
ld a , [ wd133 ]
2014-05-22 22:13:20 +00:00
ld [ hl ], a
ret
TrainerWalkUpToPlayer: ; 56881 (15:6881)
2014-09-13 07:50:56 +00:00
ld a , [ wSpriteIndex ]
2014-05-22 22:13:20 +00:00
swap a
2014-05-26 21:04:56 +00:00
ld [ wTrainerSpriteOffset ], a ; wWhichTrade
2014-05-22 22:13:20 +00:00
call ReadTrainerScreenPosition
ld a , [ wTrainerFacingDirection ]
and a
jr z , .facingDown
cp $ 4
jr z , .facingUp
cp $ 8
jr z , .facingLeft
jr .facingRight
.facingDown
ld a , [ wTrainerScreenY ]
ld b , a
ld a , $ 3 c ; (fixed) player screen Y pos
call CalcDifference
cp $ 10 ; trainer is right above player
ret z
swap a
dec a
ld c , a ; bc = steps yet to go to reach player
xor a
ld b , a ; a = direction to go to
jr .writeWalkScript
.facingUp
ld a , [ wTrainerScreenY ]
ld b , a
ld a , $ 3 c ; (fixed) player screen Y pos
call CalcDifference
cp $ 10 ; trainer is right below player
ret z
swap a
dec a
ld c , a ; bc = steps yet to go to reach player
ld b , $ 0
ld a , $ 40 ; a = direction to go to
jr .writeWalkScript
.facingRight
ld a , [ wTrainerScreenX ]
ld b , a
ld a , $ 40 ; (fixed) player screen X pos
call CalcDifference
cp $ 10 ; trainer is directly left of player
ret z
swap a
dec a
ld c , a ; bc = steps yet to go to reach player
ld b , $ 0
ld a , $ c0 ; a = direction to go to
jr .writeWalkScript
.facingLeft
2014-05-26 21:04:56 +00:00
ld a , [ wTrainerScreenX ]
2014-05-22 22:13:20 +00:00
ld b , a
ld a , $ 40 ; (fixed) player screen X pos
call CalcDifference
cp $ 10 ; trainer is directly right of player
ret z
swap a
dec a
ld c , a ; bc = steps yet to go to reach player
ld b , $ 0
ld a , $ 80 ; a = direction to go to
.writeWalkScript
2014-09-10 03:55:19 +00:00
ld hl , wNPCMovementDirections2
ld de , wNPCMovementDirections2
2014-05-22 22:13:20 +00:00
call FillMemory ; write the necessary steps to reach player
ld [ hl ], $ ff ; write end of list sentinel
2014-09-13 07:50:56 +00:00
ld a , [ wSpriteIndex ]
2014-09-10 03:55:19 +00:00
ld [ H_SPRITEINDEX ], a
2014-05-22 22:13:20 +00:00
jp MoveSprite_
2014-09-10 03:55:19 +00:00
; input: de = offset within sprite entry
; output: de = pointer to sprite data
GetSpriteDataPointer: ; 56903 (15:6903)
2014-05-22 22:13:20 +00:00
push de
add hl , de
2014-09-10 03:55:19 +00:00
ld a , [ H_SPRITEINDEX ]
2014-05-22 22:13:20 +00:00
swap a
ld d , $ 0
ld e , a
add hl , de
pop de
ret
; tests if this trainer is in the right position to engage the player and do so if she is.
2014-06-16 20:03:05 +00:00
TrainerEngage: ; 5690f (15:690f)
2014-05-22 22:13:20 +00:00
push hl
push de
2014-05-26 21:04:56 +00:00
ld a , [ wTrainerSpriteOffset ] ; wWhichTrade
2014-05-22 22:13:20 +00:00
add $ 2
ld d , $ 0
ld e , a
ld hl , wSpriteStateData1
add hl , de
ld a , [ hl ] ; c1x2: sprite image index
sub $ ff
jr nz , .spriteOnScreen ; test if sprite is on screen
jp .noEngage
.spriteOnScreen
2014-05-26 21:04:56 +00:00
ld a , [ wTrainerSpriteOffset ] ; wWhichTrade
2014-05-22 22:13:20 +00:00
add $ 9
ld d , $ 0
ld e , a
ld hl , wSpriteStateData1
add hl , de
ld a , [ hl ] ; c1x9: facing direction
2014-05-26 21:04:56 +00:00
ld [ wTrainerFacingDirection ], a
2014-05-22 22:13:20 +00:00
call ReadTrainerScreenPosition
2014-05-26 21:04:56 +00:00
ld a , [ wTrainerScreenY ] ; sprite screen Y pos
2014-05-22 22:13:20 +00:00
ld b , a
ld a , $ 3 c
cp b
jr z , .linedUpY
2014-05-26 21:04:56 +00:00
ld a , [ wTrainerScreenX ] ; sprite screen X pos
2014-05-22 22:13:20 +00:00
ld b , a
ld a , $ 40
cp b
jr z , .linedUpX
xor a
jp .noEngage
.linedUpY
2014-05-26 21:04:56 +00:00
ld a , [ wTrainerScreenX ] ; sprite screen X pos
2014-05-22 22:13:20 +00:00
ld b , a
ld a , $ 40 ; (fixed) player X position
call CalcDifference ; calc distance
jr z , .noEngage ; exact same position as player
call Ch eckSpriteCanSeePlayer
jr c , .engage
xor a
jr .noEngage
.linedUpX
2014-05-26 21:04:56 +00:00
ld a , [ wTrainerScreenY ] ; sprite screen Y pos
2014-05-22 22:13:20 +00:00
ld b , a
ld a , $ 3 c ; (fixed) player Y position
call CalcDifference ; calc distance
jr z , .noEngage ; exact same position as player
call Ch eckSpriteCanSeePlayer
jr c , .engage
xor a
jp .noEngage
.engage
call Ch eckPlayerIsInFrontOfSprite
2014-05-26 21:04:56 +00:00
ld a , [ wTrainerSpriteOffset ] ; wWhichTrade
2014-05-22 22:13:20 +00:00
and a
jr z , .noEngage
ld hl , wFlags_0xcd60
set 0 , [ hl ]
call EngageMapTrainer
ld a , $ ff
.noEngage: ; 56988 (15:6988)
2014-05-26 21:04:56 +00:00
ld [ wTrainerSpriteOffset ], a ; wWhichTrade
2014-05-22 22:13:20 +00:00
pop de
pop hl
ret
2014-05-26 21:04:56 +00:00
; reads trainer's Y position to wTrainerScreenY and X position to wTrainerScreenX
2014-05-22 22:13:20 +00:00
ReadTrainerScreenPosition: ; 5698e (15:698e)
2014-05-26 21:04:56 +00:00
ld a , [ wTrainerSpriteOffset ] ; wWhichTrade
2014-05-22 22:13:20 +00:00
add $ 4
ld d , $ 0
ld e , a
ld hl , wSpriteStateData1
add hl , de
ld a , [ hl ]
2014-05-26 21:04:56 +00:00
ld [ wTrainerScreenY ], a
ld a , [ wTrainerSpriteOffset ] ; wWhichTrade
2014-05-22 22:13:20 +00:00
add $ 6
ld d , $ 0
ld e , a
ld hl , wSpriteStateData1
add hl , de
ld a , [ hl ]
2014-05-26 21:04:56 +00:00
ld [ wTrainerScreenX ], a
2014-05-22 22:13:20 +00:00
ret
; checks if the sprite is properly lined up with the player with respect to the direction it's looking. Also checks the distance between player and sprite
; note that this does not necessarily mean the sprite is seeing the player, he could be behind it's back
; a: distance player to sprite
CheckSpriteCanSeePlayer: ; 569af (15:69af)
ld b , a
ld a , [ wTrainerEngageDistance ] ; sprite line of sight (engage distance)
cp b
jr nc , .checkIfLinedUp
jr .notInLine ; player too far away
.checkIfLinedUp
2014-05-26 21:04:56 +00:00
ld a , [ wTrainerFacingDirection ] ; sprite facing direction
2014-05-22 22:13:20 +00:00
cp $ 0 ; down
jr z , .checkXCoord
cp $ 4 ; up
jr z , .checkXCoord
cp $ 8 ; left
jr z , .checkYCoord
cp $ c ; right
jr z , .checkYCoord
jr .notInLine
.checkXCoord
2014-05-26 21:04:56 +00:00
ld a , [ wTrainerScreenX ] ; sprite screen X position
2014-05-22 22:13:20 +00:00
ld b , a
cp $ 40
jr z , .inLine
jr .notInLine
.checkYCoord
2014-05-26 21:04:56 +00:00
ld a , [ wTrainerScreenY ] ; sprite screen Y position
2014-05-22 22:13:20 +00:00
ld b , a
cp $ 3 c
jr nz , .notInLine
.inLine
scf
ret
.notInLine
and a
ret
; tests if the player is in front of the sprite (rather than behind it)
CheckPlayerIsInFrontOfSprite: ; 569e3 (15:69e3)
2014-05-26 21:04:56 +00:00
ld a , [ W_CURMAP ] ; W_CURMAP
2014-05-22 22:13:20 +00:00
cp POWER_PLANT
jp z , .engage ; XXX not sure why bypass this for power plant (maybe to get voltorb fake items to work?)
2014-05-26 21:04:56 +00:00
ld a , [ wTrainerSpriteOffset ] ; wWhichTrade
2014-05-22 22:13:20 +00:00
add $ 4
ld d , $ 0
ld e , a
ld hl , wSpriteStateData1
add hl , de
ld a , [ hl ] ; c1x4 (sprite screen Y pos)
cp $ fc
jr nz , .notOnTopmostTile ; special case if sprite is on topmost tile (Y = $fc (-4)), make it come down a block
ld a , $ c
.notOnTopmostTile
2014-05-26 21:04:56 +00:00
ld [ wTrainerScreenY ], a
ld a , [ wTrainerSpriteOffset ] ; wWhichTrade
2014-05-22 22:13:20 +00:00
add $ 6
ld d , $ 0
ld e , a
ld hl , wSpriteStateData1
add hl , de
ld a , [ hl ] ; c1x6 (sprite screen X pos)
2014-05-26 21:04:56 +00:00
ld [ wTrainerScreenX ], a
ld a , [ wTrainerFacingDirection ] ; facing direction
2014-05-22 22:13:20 +00:00
cp $ 0
jr nz , .notFacingDown
2014-05-26 21:04:56 +00:00
ld a , [ wTrainerScreenY ] ; sprite screen Y pos
2014-05-22 22:13:20 +00:00
cp $ 3 c
jr c , .engage ; sprite above player
jr .noEngage ; sprite below player
.notFacingDown
cp $ 4
jr nz , .notFacingUp
2014-05-26 21:04:56 +00:00
ld a , [ wTrainerScreenY ] ; sprite screen Y pos
2014-05-22 22:13:20 +00:00
cp $ 3 c
jr nc , .engage ; sprite below player
jr .noEngage ; sprite above player
.notFacingUp
cp $ 8
jr nz , .notFacingLeft
2014-05-26 21:04:56 +00:00
ld a , [ wTrainerScreenX ] ; sprite screen X pos
2014-05-22 22:13:20 +00:00
cp $ 40
jr nc , .engage ; sprite right of player
jr .noEngage ; sprite left of player
.notFacingLeft
2014-05-26 21:04:56 +00:00
ld a , [ wTrainerScreenX ] ; sprite screen X pos
2014-05-22 22:13:20 +00:00
cp $ 40
jr nc , .noEngage ; sprite right of player
.engage
ld a , $ ff
jr .done
.noEngage
xor a
.done
2014-05-26 21:04:56 +00:00
ld [ wTrainerSpriteOffset ], a ; wWhichTrade
2014-05-22 22:13:20 +00:00
ret