Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pret
GitHub Repository: pret/pokered
Path: blob/master/engine/overworld/pathfinding.asm
1271 views
1
FindPathToPlayer:
2
xor a
3
ld hl, hFindPathNumSteps
4
ld [hli], a ; hFindPathNumSteps
5
ld [hli], a ; hFindPathFlags
6
ld [hli], a ; hFindPathYProgress
7
ld [hl], a ; hFindPathXProgress
8
ld hl, wNPCMovementDirections2
9
ld de, $0
10
.loop
11
ldh a, [hFindPathYProgress]
12
ld b, a
13
ldh a, [hNPCPlayerYDistance] ; Y distance in steps
14
call CalcDifference
15
ld d, a
16
and a
17
jr nz, .stillHasYProgress
18
ldh a, [hFindPathFlags]
19
set BIT_PATH_FOUND_Y, a
20
ldh [hFindPathFlags], a
21
.stillHasYProgress
22
ldh a, [hFindPathXProgress]
23
ld b, a
24
ldh a, [hNPCPlayerXDistance] ; X distance in steps
25
call CalcDifference
26
ld e, a
27
and a
28
jr nz, .stillHasXProgress
29
ldh a, [hFindPathFlags]
30
set BIT_PATH_FOUND_X, a
31
ldh [hFindPathFlags], a
32
.stillHasXProgress
33
ldh a, [hFindPathFlags]
34
cp (1 << BIT_PATH_FOUND_X) | (1 << BIT_PATH_FOUND_Y)
35
jr z, .done
36
; Compare whether the X distance between the player and the current of the path
37
; is greater or if the Y distance is. Then, try to reduce whichever is greater.
38
ld a, e
39
cp d
40
jr c, .yDistanceGreater
41
; x distance is greater
42
ldh a, [hNPCPlayerRelativePosFlags]
43
bit BIT_PLAYER_LOWER_X, a
44
jr nz, .playerIsLeftOfNPC
45
ld d, NPC_MOVEMENT_RIGHT
46
jr .next1
47
.playerIsLeftOfNPC
48
ld d, NPC_MOVEMENT_LEFT
49
.next1
50
ldh a, [hFindPathXProgress]
51
add 1
52
ldh [hFindPathXProgress], a
53
jr .storeDirection
54
.yDistanceGreater
55
ldh a, [hNPCPlayerRelativePosFlags]
56
bit BIT_PLAYER_LOWER_Y, a
57
jr nz, .playerIsAboveNPC
58
ld d, NPC_MOVEMENT_DOWN
59
jr .next2
60
.playerIsAboveNPC
61
ld d, NPC_MOVEMENT_UP
62
.next2
63
ldh a, [hFindPathYProgress]
64
add 1
65
ldh [hFindPathYProgress], a
66
.storeDirection
67
ld a, d
68
ld [hli], a
69
ldh a, [hFindPathNumSteps]
70
inc a
71
ldh [hFindPathNumSteps], a
72
jp .loop
73
.done
74
ld [hl], $ff
75
ret
76
77
CalcPositionOfPlayerRelativeToNPC:
78
xor a
79
ldh [hNPCPlayerRelativePosFlags], a
80
ld a, [wSpritePlayerStateData1YPixels]
81
ld d, a
82
ld a, [wSpritePlayerStateData1XPixels]
83
ld e, a
84
ld hl, wSpriteStateData1
85
ldh a, [hNPCSpriteOffset]
86
add l
87
add SPRITESTATEDATA1_YPIXELS
88
ld l, a
89
jr nc, .noCarry
90
inc h
91
.noCarry
92
ld a, d
93
ld b, a
94
ld a, [hli] ; NPC sprite screen Y position in pixels
95
call CalcDifference
96
jr nc, .NPCSouthOfOrAlignedWithPlayer
97
.NPCNorthOfPlayer
98
push hl
99
ld hl, hNPCPlayerRelativePosFlags
100
bit BIT_PLAYER_LOWER_Y, [hl]
101
set BIT_PLAYER_LOWER_Y, [hl]
102
pop hl
103
jr .divideYDistance
104
.NPCSouthOfOrAlignedWithPlayer
105
push hl
106
ld hl, hNPCPlayerRelativePosFlags
107
bit BIT_PLAYER_LOWER_Y, [hl]
108
res BIT_PLAYER_LOWER_Y, [hl]
109
pop hl
110
.divideYDistance
111
push hl
112
ld hl, hDividend2
113
ld [hli], a
114
ld a, 16
115
ld [hli], a
116
call DivideBytes ; divide Y absolute distance by 16
117
ld a, [hl] ; quotient
118
ldh [hNPCPlayerYDistance], a
119
pop hl
120
inc hl
121
ld b, e
122
ld a, [hl] ; NPC sprite screen X position in pixels
123
call CalcDifference
124
jr nc, .NPCEastOfOrAlignedWithPlayer
125
.NPCWestOfPlayer
126
push hl
127
ld hl, hNPCPlayerRelativePosFlags
128
bit BIT_PLAYER_LOWER_X, [hl]
129
set BIT_PLAYER_LOWER_X, [hl]
130
pop hl
131
jr .divideXDistance
132
.NPCEastOfOrAlignedWithPlayer
133
push hl
134
ld hl, hNPCPlayerRelativePosFlags
135
bit BIT_PLAYER_LOWER_X, [hl]
136
res BIT_PLAYER_LOWER_X, [hl]
137
pop hl
138
.divideXDistance
139
ldh [hDividend2], a
140
ld a, 16
141
ldh [hDivisor2], a
142
call DivideBytes ; divide X absolute distance by 16
143
ldh a, [hQuotient2]
144
ldh [hNPCPlayerXDistance], a
145
ldh a, [hNPCPlayerRelativePosPerspective]
146
and a
147
ret z
148
ldh a, [hNPCPlayerRelativePosFlags]
149
cpl
150
and $3
151
ldh [hNPCPlayerRelativePosFlags], a
152
ret
153
154
ConvertNPCMovementDirectionsToJoypadMasks:
155
ldh a, [hNPCMovementDirections2Index]
156
ld [wNPCMovementDirections2Index], a
157
dec a
158
ld de, wSimulatedJoypadStatesEnd
159
ld hl, wNPCMovementDirections2
160
add l
161
ld l, a
162
jr nc, .loop
163
inc h
164
.loop
165
ld a, [hld]
166
call ConvertNPCMovementDirectionToJoypadMask
167
ld [de], a
168
inc de
169
ldh a, [hNPCMovementDirections2Index]
170
dec a
171
ldh [hNPCMovementDirections2Index], a
172
jr nz, .loop
173
ret
174
175
ConvertNPCMovementDirectionToJoypadMask:
176
push hl
177
ld b, a
178
ld hl, NPCMovementDirectionsToJoypadMasksTable
179
.loop
180
ld a, [hli]
181
cp $ff
182
jr z, .done
183
cp b
184
jr z, .loadJoypadMask
185
inc hl
186
jr .loop
187
.loadJoypadMask
188
ld a, [hl]
189
.done
190
pop hl
191
ret
192
193
NPCMovementDirectionsToJoypadMasksTable:
194
db NPC_MOVEMENT_UP, PAD_UP
195
db NPC_MOVEMENT_DOWN, PAD_DOWN
196
db NPC_MOVEMENT_LEFT, PAD_LEFT
197
db NPC_MOVEMENT_RIGHT, PAD_RIGHT
198
db $ff
199
200
; unreferenced
201
ret
202
203