-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathdamage_calculation.asm
448 lines (418 loc) · 9.55 KB
/
damage_calculation.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
; stores in wDamage, wAIMinDamage and wAIMaxDamage the calculated damage
; done to the defending Pokémon by a given card and attack
; input:
; a = attack index to take into account
; [hTempPlayAreaLocation_ff9d] = location of attacking card to consider
EstimateDamage_VersusDefendingCard:
ld [wSelectedAttack], a
ld e, a
ldh a, [hTempPlayAreaLocation_ff9d]
add DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
ld d, a
call CopyAttackDataAndDamage_FromDeckIndex
ld a, [wLoadedAttackCategory]
cp POKEMON_POWER
jr nz, .is_attack
; is a Pokémon Power
; set wDamage, wAIMinDamage and wAIMaxDamage to zero
ld hl, wDamage
xor a
ld [hli], a
ld [hl], a
ld [wAIMinDamage], a
ld [wAIMaxDamage], a
ld e, a
ld d, a
ret
.is_attack
; set wAIMinDamage and wAIMaxDamage to damage of attack
; these values take into account the range of damage
; that the attack can span (e.g. min and max number of hits)
ld a, [wDamage]
ld [wAIMinDamage], a
ld [wAIMaxDamage], a
ld a, EFFECTCMDTYPE_AI
call TryExecuteEffectCommandFunction
ld a, [wAIMinDamage]
ld hl, wAIMaxDamage
or [hl]
jr nz, .calculation
ld a, [wDamage]
ld [wAIMinDamage], a
ld [wAIMaxDamage], a
.calculation
; if temp. location is active, damage calculation can be done directly...
ldh a, [hTempPlayAreaLocation_ff9d]
or a
jr z, CalculateDamage_VersusDefendingPokemon
; ...otherwise substatuses need to be temporarily reset to account
; for the switching, to obtain the right damage calculation...
; reset substatus1
ld a, DUELVARS_ARENA_CARD_SUBSTATUS1
call GetTurnDuelistVariable
push af
push hl
ld [hl], $00
; reset substatus2
ld l, DUELVARS_ARENA_CARD_SUBSTATUS2
ld a, [hl]
push af
push hl
ld [hl], $00
; reset changed resistance
ld l, DUELVARS_ARENA_CARD_CHANGED_RESISTANCE
ld a, [hl]
push af
push hl
ld [hl], $00
call CalculateDamage_VersusDefendingPokemon
; ...and subsequently recovered to continue the duel normally
pop hl
pop af
ld [hl], a
pop hl
pop af
ld [hl], a
pop hl
pop af
ld [hl], a
ret
; calculates the damage that will be dealt to the player's active card
; using the card that is located in hTempPlayAreaLocation_ff9d
; taking into account weakness/resistance/pluspowers/defenders/etc
; and outputs the result capped at a max of $ff
; input:
; [wAIMinDamage] = base damage
; [wAIMaxDamage] = base damage
; [wDamage] = base damage
; [hTempPlayAreaLocation_ff9d] = turn holder's card location as the attacker
CalculateDamage_VersusDefendingPokemon:
ld hl, wAIMinDamage
call .Calculate
ld hl, wAIMaxDamage
call .Calculate
ld hl, wDamage
.Calculate:
ld e, [hl]
ld d, $00
push hl
; load this card's data
ldh a, [hTempPlayAreaLocation_ff9d]
add DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call LoadCardDataToBuffer2_FromDeckIndex
ld a, [wLoadedCard2ID]
ld [wTempTurnDuelistCardID], a
; load player's arena card data
call SwapTurn
ld a, DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call LoadCardDataToBuffer2_FromDeckIndex
ld a, [wLoadedCard2ID]
ld [wTempNonTurnDuelistCardID], a
call SwapTurn
push de
call HandleNoDamageOrEffectSubstatus
pop de
jr nc, .vulnerable
; invulnerable to damage
ld de, 0
jr .done
.vulnerable
ldh a, [hTempPlayAreaLocation_ff9d]
or a
call z, HandleDoubleDamageSubstatus
; skips the weak/res checks if unaffected.
bit UNAFFECTED_BY_WEAKNESS_RESISTANCE_F, d
res UNAFFECTED_BY_WEAKNESS_RESISTANCE_F, d
jr nz, .not_resistant
; handle weakness
ldh a, [hTempPlayAreaLocation_ff9d]
call GetPlayAreaCardColor
call TranslateColorToWR
ld b, a
call SwapTurn
call GetArenaCardWeakness
call SwapTurn
and b
jr z, .not_weak
; double de
sla e
rl d
.not_weak
; handle resistance
call SwapTurn
call GetArenaCardResistance
call SwapTurn
and b
jr z, .not_resistant
ld hl, -30
add hl, de
ld e, l
ld d, h
.not_resistant
; apply pluspower and defender boosts
ldh a, [hTempPlayAreaLocation_ff9d]
add CARD_LOCATION_ARENA
ld b, a
call ApplyAttachedPluspower
call SwapTurn
ld b, CARD_LOCATION_ARENA
call ApplyAttachedDefender
call HandleDamageReduction
; test if de underflowed
bit 7, d
jr z, .no_underflow
ld de, 0
.no_underflow
ld a, DUELVARS_ARENA_CARD_STATUS
call GetTurnDuelistVariable
and DOUBLE_POISONED
jr z, .not_poisoned
ld c, 20
and DOUBLE_POISONED & (POISONED ^ $ff)
jr nz, .add_poison
ld c, 10
.add_poison
ld a, c
add e
ld e, a
ld a, $00
adc d
ld d, a
.not_poisoned
call SwapTurn
.done
pop hl
ld [hl], e
ld a, d
or a
ret z
; cap damage
ld a, $ff
ld [hl], a
ret
; stores in wDamage, wAIMinDamage and wAIMaxDamage the calculated damage
; done to the Pokémon at hTempPlayAreaLocation_ff9d
; by the defending Pokémon, using the attack index at a
; input:
; a = attack index
; [hTempPlayAreaLocation_ff9d] = location of card to calculate
; damage as the receiver
EstimateDamage_FromDefendingPokemon:
call SwapTurn
ld [wSelectedAttack], a
ld e, a
ld a, DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
ld d, a
call CopyAttackDataAndDamage_FromDeckIndex
call SwapTurn
ld a, [wLoadedAttackCategory]
cp POKEMON_POWER
jr nz, .is_attack
; is a Pokémon Power
; set wDamage, wAIMinDamage and wAIMaxDamage to zero
ld hl, wDamage
xor a
ld [hli], a
ld [hl], a
ld [wAIMinDamage], a
ld [wAIMaxDamage], a
ld e, a
ld d, a
ret
.is_attack
; set wAIMinDamage and wAIMaxDamage to damage of attack
; these values take into account the range of damage
; that the attack can span (e.g. min and max number of hits)
ld a, [wDamage]
ld [wAIMinDamage], a
ld [wAIMaxDamage], a
call SwapTurn
ldh a, [hTempPlayAreaLocation_ff9d]
push af
xor a ; PLAY_AREA_ARENA
ldh [hTempPlayAreaLocation_ff9d], a
ld a, EFFECTCMDTYPE_AI
call TryExecuteEffectCommandFunction
pop af
ldh [hTempPlayAreaLocation_ff9d], a
call SwapTurn
ld a, [wAIMinDamage]
ld hl, wAIMaxDamage
or [hl]
jr nz, .calculation
ld a, [wDamage]
ld [wAIMinDamage], a
ld [wAIMaxDamage], a
.calculation
; if temp. location is active, damage calculation can be done directly...
ldh a, [hTempPlayAreaLocation_ff9d]
or a
jr z, CalculateDamage_FromDefendingPokemon
; ...otherwise substatuses need to be temporarily reset to account
; for the switching, to obtain the right damage calculation...
ld a, DUELVARS_ARENA_CARD_SUBSTATUS1
call GetTurnDuelistVariable
push af
push hl
ld [hl], $00
; reset substatus2
ld l, DUELVARS_ARENA_CARD_SUBSTATUS2
ld a, [hl]
push af
push hl
ld [hl], $00
; reset changed resistance
ld l, DUELVARS_ARENA_CARD_CHANGED_RESISTANCE
ld a, [hl]
push af
push hl
ld [hl], $00
call CalculateDamage_FromDefendingPokemon
; ...and subsequently recovered to continue the duel normally
pop hl
pop af
ld [hl], a
pop hl
pop af
ld [hl], a
pop hl
pop af
ld [hl], a
ret
; similar to CalculateDamage_VersusDefendingPokemon but reversed,
; calculating damage of the defending Pokémon versus
; the card located in hTempPlayAreaLocation_ff9d
; taking into account weakness/resistance/pluspowers/defenders/etc
; and poison damage for two turns
; and outputs the result capped at a max of $ff
; input:
; [wAIMinDamage] = base damage
; [wAIMaxDamage] = base damage
; [wDamage] = base damage
; [hTempPlayAreaLocation_ff9d] = location of card to calculate
; damage as the receiver
CalculateDamage_FromDefendingPokemon:
ld hl, wAIMinDamage
call .CalculateDamage
ld hl, wAIMaxDamage
call .CalculateDamage
ld hl, wDamage
; fallthrough
.CalculateDamage
ld e, [hl]
ld d, $00
push hl
; load player active card's data
call SwapTurn
ld a, DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call LoadCardDataToBuffer2_FromDeckIndex
ld a, [wLoadedCard2ID]
ld [wTempTurnDuelistCardID], a
call SwapTurn
; load opponent's card data
ldh a, [hTempPlayAreaLocation_ff9d]
add DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call LoadCardDataToBuffer2_FromDeckIndex
ld a, [wLoadedCard2ID]
ld [wTempNonTurnDuelistCardID], a
call SwapTurn
call HandleDoubleDamageSubstatus
bit UNAFFECTED_BY_WEAKNESS_RESISTANCE_F, d
res UNAFFECTED_BY_WEAKNESS_RESISTANCE_F, d
jr nz, .not_resistant
; handle weakness
call GetArenaCardColor
call TranslateColorToWR
ld b, a
call SwapTurn
ldh a, [hTempPlayAreaLocation_ff9d]
or a
jr nz, .bench_weak
ld a, DUELVARS_ARENA_CARD_CHANGED_WEAKNESS
call GetTurnDuelistVariable
or a
jr nz, .unchanged_weak
.bench_weak
ldh a, [hTempPlayAreaLocation_ff9d]
add DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call LoadCardDataToBuffer2_FromDeckIndex
ld a, [wLoadedCard2Weakness]
.unchanged_weak
and b
jr z, .not_weak
; double de
sla e
rl d
.not_weak
; handle resistance
ldh a, [hTempPlayAreaLocation_ff9d]
or a
jr nz, .bench_res
ld a, DUELVARS_ARENA_CARD_CHANGED_RESISTANCE
call GetTurnDuelistVariable
or a
jr nz, .unchanged_res
.bench_res
ldh a, [hTempPlayAreaLocation_ff9d]
add DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call LoadCardDataToBuffer2_FromDeckIndex
ld a, [wLoadedCard2Resistance]
.unchanged_res
and b
jr z, .not_resistant
ld hl, -30
add hl, de
ld e, l
ld d, h
.not_resistant
; apply pluspower and defender boosts
call SwapTurn
ld b, CARD_LOCATION_ARENA
call ApplyAttachedPluspower
call SwapTurn
ldh a, [hTempPlayAreaLocation_ff9d]
add CARD_LOCATION_ARENA
ld b, a
call ApplyAttachedDefender
ldh a, [hTempPlayAreaLocation_ff9d]
or a
call z, HandleDamageReduction
bit 7, d
jr z, .no_underflow
ld de, $0
.no_underflow
ldh a, [hTempPlayAreaLocation_ff9d]
or a
jr nz, .done
ld a, DUELVARS_ARENA_CARD_STATUS
call GetTurnDuelistVariable
and DOUBLE_POISONED
jr z, .done
ld c, 40
and DOUBLE_POISONED & (POISONED ^ $ff)
jr nz, .add_poison
ld c, 20
.add_poison
ld a, c
add e
ld e, a
ld a, $00
adc d
ld d, a
.done
pop hl
ld [hl], e
ld a, d
or a
ret z
ld a, $ff
ld [hl], a
ret