Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pret
GitHub Repository: pret/pokered
Path: blob/master/engine/pokemon/evos_moves.asm
1271 views
1
; try to evolve the mon in [wWhichPokemon]
2
TryEvolvingMon:
3
ld hl, wCanEvolveFlags
4
xor a
5
ld [hl], a
6
ld a, [wWhichPokemon]
7
ld c, a
8
ld b, FLAG_SET
9
call Evolution_FlagAction
10
11
; this is only called after battle
12
; it is supposed to do level up evolutions, though there is a bug that allows item evolutions to occur
13
EvolutionAfterBattle:
14
ldh a, [hTileAnimations]
15
push af
16
xor a
17
ld [wEvolutionOccurred], a
18
dec a
19
ld [wWhichPokemon], a
20
push hl
21
push bc
22
push de
23
ld hl, wPartyCount
24
push hl
25
26
Evolution_PartyMonLoop: ; loop over party mons
27
ld hl, wWhichPokemon
28
inc [hl]
29
pop hl
30
inc hl
31
ld a, [hl]
32
cp $ff ; have we reached the end of the party?
33
jp z, .done
34
ld [wEvoOldSpecies], a
35
push hl
36
ld a, [wWhichPokemon]
37
ld c, a
38
ld hl, wCanEvolveFlags
39
ld b, FLAG_TEST
40
call Evolution_FlagAction
41
ld a, c
42
and a ; is the mon's bit set?
43
jp z, Evolution_PartyMonLoop ; if not, go to the next mon
44
ld a, [wEvoOldSpecies]
45
dec a
46
ld b, 0
47
ld hl, EvosMovesPointerTable
48
add a
49
rl b
50
ld c, a
51
add hl, bc
52
ld a, [hli]
53
ld h, [hl]
54
ld l, a
55
push hl
56
ld a, [wCurPartySpecies]
57
push af
58
xor a ; PLAYER_PARTY_DATA
59
ld [wMonDataLocation], a
60
call LoadMonData
61
pop af
62
ld [wCurPartySpecies], a
63
pop hl
64
65
.evoEntryLoop ; loop over evolution entries
66
ld a, [hli]
67
and a ; have we reached the end of the evolution data?
68
jr z, Evolution_PartyMonLoop
69
ld b, a ; evolution type
70
cp EVOLVE_TRADE
71
jr z, .checkTradeEvo
72
; not trade evolution
73
ld a, [wLinkState]
74
cp LINK_STATE_TRADING
75
jr z, Evolution_PartyMonLoop ; if trading, go the next mon
76
ld a, b
77
cp EVOLVE_ITEM
78
jr z, .checkItemEvo
79
ld a, [wForceEvolution]
80
and a
81
jr nz, Evolution_PartyMonLoop
82
ld a, b
83
cp EVOLVE_LEVEL
84
jr z, .checkLevel
85
.checkTradeEvo
86
ld a, [wLinkState]
87
cp LINK_STATE_TRADING
88
jp nz, .nextEvoEntry1 ; if not trading, go to the next evolution entry
89
ld a, [hli] ; level requirement
90
ld b, a
91
ld a, [wLoadedMonLevel]
92
cp b ; is the mon's level greater than the evolution requirement?
93
jp c, Evolution_PartyMonLoop ; if so, go the next mon
94
jr .doEvolution
95
.checkItemEvo
96
ld a, [hli]
97
; Bug: Wild encounters can cause stone evolutions without
98
; having any stones available. This was fixed in Yellow.
99
ld b, a ; evolution item
100
ld a, [wCurItem] ; same as [wCurPartySpecies]
101
cp b ; was the evolution item in this entry used?
102
jp nz, .nextEvoEntry1 ; if not, go to the next evolution entry
103
.checkLevel
104
ld a, [hli] ; level requirement
105
ld b, a
106
ld a, [wLoadedMonLevel]
107
cp b ; is the mon's level greater than the evolution requirement?
108
jp c, .nextEvoEntry2 ; if so, go the next evolution entry
109
.doEvolution
110
ld [wCurEnemyLevel], a
111
ld a, 1
112
ld [wEvolutionOccurred], a
113
push hl
114
ld a, [hl]
115
ld [wEvoNewSpecies], a
116
ld a, [wWhichPokemon]
117
ld hl, wPartyMonNicks
118
call GetPartyMonName
119
call CopyToStringBuffer
120
ld hl, IsEvolvingText
121
call PrintText
122
ld c, 50
123
call DelayFrames
124
xor a
125
ldh [hAutoBGTransferEnabled], a
126
hlcoord 0, 0
127
lb bc, 12, 20
128
call ClearScreenArea
129
ld a, $1
130
ldh [hAutoBGTransferEnabled], a
131
ld a, $ff
132
ld [wUpdateSpritesEnabled], a
133
call ClearSprites
134
callfar EvolveMon
135
jp c, CancelledEvolution
136
ld hl, EvolvedText
137
call PrintText
138
pop hl
139
ld a, [hl]
140
ld [wCurSpecies], a
141
ld [wLoadedMonSpecies], a
142
ld [wEvoNewSpecies], a
143
ld a, MONSTER_NAME
144
ld [wNameListType], a
145
ld a, BANK(TrainerNames) ; bank is not used for monster names
146
ld [wPredefBank], a
147
call GetName
148
push hl
149
ld hl, IntoText
150
call PrintText_NoCreatingTextBox
151
ld a, SFX_GET_ITEM_2
152
call PlaySoundWaitForCurrent
153
call WaitForSoundToFinish
154
ld c, 40
155
call DelayFrames
156
call ClearScreen
157
call RenameEvolvedMon
158
ld a, [wPokedexNum]
159
push af
160
ld a, [wCurSpecies]
161
ld [wPokedexNum], a
162
predef IndexToPokedex
163
ld a, [wPokedexNum]
164
dec a
165
ld hl, BaseStats
166
ld bc, BASE_DATA_SIZE
167
call AddNTimes
168
ld de, wMonHeader
169
call CopyData
170
ld a, [wCurSpecies]
171
ld [wMonHIndex], a
172
pop af
173
ld [wPokedexNum], a
174
ld hl, wLoadedMonHPExp - 1
175
ld de, wLoadedMonStats
176
ld b, $1
177
call CalcStats
178
ld a, [wWhichPokemon]
179
ld hl, wPartyMon1
180
ld bc, wPartyMon2 - wPartyMon1
181
call AddNTimes
182
ld e, l
183
ld d, h
184
push hl
185
push bc
186
ld bc, wPartyMon1MaxHP - wPartyMon1
187
add hl, bc
188
ld a, [hli]
189
ld b, a
190
ld c, [hl]
191
ld hl, wLoadedMonMaxHP + 1
192
ld a, [hld]
193
sub c
194
ld c, a
195
ld a, [hl]
196
sbc b
197
ld b, a
198
ld hl, wLoadedMonHP + 1
199
ld a, [hl]
200
add c
201
ld [hld], a
202
ld a, [hl]
203
adc b
204
ld [hl], a
205
dec hl
206
pop bc
207
call CopyData
208
ld a, [wCurSpecies]
209
ld [wPokedexNum], a
210
xor a
211
ld [wMonDataLocation], a
212
call LearnMoveFromLevelUp
213
pop hl
214
predef SetPartyMonTypes
215
ld a, [wIsInBattle]
216
and a
217
call z, Evolution_ReloadTilesetTilePatterns
218
predef IndexToPokedex
219
ld a, [wPokedexNum]
220
dec a
221
ld c, a
222
ld b, FLAG_SET
223
ld hl, wPokedexOwned
224
push bc
225
call Evolution_FlagAction
226
pop bc
227
ld hl, wPokedexSeen
228
call Evolution_FlagAction
229
pop de
230
pop hl
231
ld a, [wLoadedMonSpecies]
232
ld [hl], a
233
push hl
234
ld l, e
235
ld h, d
236
jr .nextEvoEntry2
237
238
.nextEvoEntry1
239
inc hl
240
241
.nextEvoEntry2
242
inc hl
243
jp .evoEntryLoop
244
245
.done
246
pop de
247
pop bc
248
pop hl
249
pop af
250
ldh [hTileAnimations], a
251
ld a, [wLinkState]
252
cp LINK_STATE_TRADING
253
ret z
254
ld a, [wIsInBattle]
255
and a
256
ret nz
257
ld a, [wEvolutionOccurred]
258
and a
259
call nz, PlayDefaultMusic
260
ret
261
262
RenameEvolvedMon:
263
; Renames the mon to its new, evolved form's standard name unless it had a
264
; nickname, in which case the nickname is kept.
265
ASSERT wCurSpecies == wNameListIndex ; save+restore wCurSpecies while using wNameListIndex
266
ld a, [wCurSpecies]
267
push af
268
ld a, [wMonHIndex]
269
ld [wNameListIndex], a
270
call GetName
271
pop af
272
ld [wCurSpecies], a
273
ld hl, wNameBuffer
274
ld de, wStringBuffer
275
.compareNamesLoop
276
ld a, [de]
277
inc de
278
cp [hl]
279
inc hl
280
ret nz
281
cp "@"
282
jr nz, .compareNamesLoop
283
ld a, [wWhichPokemon]
284
ld bc, NAME_LENGTH
285
ld hl, wPartyMonNicks
286
call AddNTimes
287
push hl
288
call GetName
289
ld hl, wNameBuffer
290
pop de
291
jp CopyData
292
293
CancelledEvolution:
294
ld hl, StoppedEvolvingText
295
call PrintText
296
call ClearScreen
297
pop hl
298
call Evolution_ReloadTilesetTilePatterns
299
jp Evolution_PartyMonLoop
300
301
EvolvedText:
302
text_far _EvolvedText
303
text_end
304
305
IntoText:
306
text_far _IntoText
307
text_end
308
309
StoppedEvolvingText:
310
text_far _StoppedEvolvingText
311
text_end
312
313
IsEvolvingText:
314
text_far _IsEvolvingText
315
text_end
316
317
Evolution_ReloadTilesetTilePatterns:
318
ld a, [wLinkState]
319
cp LINK_STATE_TRADING
320
ret z
321
jp ReloadTilesetTilePatterns
322
323
LearnMoveFromLevelUp:
324
ld hl, EvosMovesPointerTable
325
ld a, [wPokedexNum] ; species
326
ld [wCurPartySpecies], a
327
dec a
328
ld bc, 0
329
ld hl, EvosMovesPointerTable
330
add a
331
rl b
332
ld c, a
333
add hl, bc
334
ld a, [hli]
335
ld h, [hl]
336
ld l, a
337
.skipEvolutionDataLoop ; loop to skip past the evolution data, which comes before the move data
338
ld a, [hli]
339
and a ; have we reached the end of the evolution data?
340
jr nz, .skipEvolutionDataLoop ; if not, jump back up
341
.learnSetLoop ; loop over the learn set until we reach a move that is learnt at the current level or the end of the list
342
ld a, [hli]
343
and a ; have we reached the end of the learn set?
344
jr z, .done ; if we've reached the end of the learn set, jump
345
ld b, a ; level the move is learnt at
346
ld a, [wCurEnemyLevel]
347
cp b ; is the move learnt at the mon's current level?
348
ld a, [hli] ; move ID
349
jr nz, .learnSetLoop
350
ld d, a ; ID of move to learn
351
ld a, [wMonDataLocation]
352
and a
353
jr nz, .next
354
; If [wMonDataLocation] is 0 (PLAYER_PARTY_DATA), get the address of the mon's
355
; current moves in party data. Every call to this function sets
356
; [wMonDataLocation] to 0 because other data locations are not supported.
357
; If it is not 0, this function will not work properly.
358
ld hl, wPartyMon1Moves
359
ld a, [wWhichPokemon]
360
ld bc, wPartyMon2 - wPartyMon1
361
call AddNTimes
362
.next
363
ld b, NUM_MOVES
364
.checkCurrentMovesLoop ; check if the move to learn is already known
365
ld a, [hli]
366
cp d
367
jr z, .done ; if already known, jump
368
dec b
369
jr nz, .checkCurrentMovesLoop
370
ld a, d
371
ld [wMoveNum], a
372
ld [wNamedObjectIndex], a
373
call GetMoveName
374
call CopyToStringBuffer
375
predef LearnMove
376
.done
377
ld a, [wCurPartySpecies]
378
ld [wPokedexNum], a
379
ret
380
381
; writes the moves a mon has at level [wCurEnemyLevel] to [de]
382
; move slots are being filled up sequentially and shifted if all slots are full
383
WriteMonMoves:
384
call GetPredefRegisters
385
push hl
386
push de
387
push bc
388
ld hl, EvosMovesPointerTable
389
ld b, 0
390
ld a, [wCurPartySpecies]
391
dec a
392
add a
393
rl b
394
ld c, a
395
add hl, bc
396
ld a, [hli]
397
ld h, [hl]
398
ld l, a
399
.skipEvoEntriesLoop
400
ld a, [hli]
401
and a
402
jr nz, .skipEvoEntriesLoop
403
jr .firstMove
404
.nextMove
405
pop de
406
.nextMove2
407
inc hl
408
.firstMove
409
ld a, [hli] ; read level of next move in learnset
410
and a
411
jp z, .done ; end of list
412
ld b, a
413
ld a, [wCurEnemyLevel]
414
cp b
415
jp c, .done ; mon level < move level (assumption: learnset is sorted by level)
416
ld a, [wLearningMovesFromDayCare]
417
and a
418
jr z, .skipMinLevelCheck
419
ld a, [wDayCareStartLevel]
420
cp b
421
jr nc, .nextMove2 ; min level >= move level
422
423
.skipMinLevelCheck
424
425
; check if the move is already known
426
push de
427
ld c, NUM_MOVES
428
.alreadyKnowsCheckLoop
429
ld a, [de]
430
inc de
431
cp [hl]
432
jr z, .nextMove
433
dec c
434
jr nz, .alreadyKnowsCheckLoop
435
436
; try to find an empty move slot
437
pop de
438
push de
439
ld c, NUM_MOVES
440
.findEmptySlotLoop
441
ld a, [de]
442
and a
443
jr z, .writeMoveToSlot2
444
inc de
445
dec c
446
jr nz, .findEmptySlotLoop
447
448
; no empty move slots found
449
pop de
450
push de
451
push hl
452
ld h, d
453
ld l, e
454
call WriteMonMoves_ShiftMoveData ; shift all moves one up (deleting move 1)
455
ld a, [wLearningMovesFromDayCare]
456
and a
457
jr z, .writeMoveToSlot
458
459
; shift PP as well if learning moves from day care
460
push de
461
ld bc, wPartyMon1PP - (wPartyMon1Moves + 3)
462
add hl, bc
463
ld d, h
464
ld e, l
465
call WriteMonMoves_ShiftMoveData ; shift all move PP data one up
466
pop de
467
468
.writeMoveToSlot
469
pop hl
470
.writeMoveToSlot2
471
ld a, [hl]
472
ld [de], a
473
ld a, [wLearningMovesFromDayCare]
474
and a
475
jr z, .nextMove
476
477
; write move PP value if learning moves from day care
478
push hl
479
ld a, [hl]
480
ld hl, wPartyMon1PP - wPartyMon1Moves
481
add hl, de
482
push hl
483
dec a
484
ld hl, Moves
485
ld bc, MOVE_LENGTH
486
call AddNTimes
487
ld de, wBuffer
488
ld a, BANK(Moves)
489
call FarCopyData
490
ld a, [wBuffer + 5]
491
pop hl
492
ld [hl], a
493
pop hl
494
jr .nextMove
495
496
.done
497
pop bc
498
pop de
499
pop hl
500
ret
501
502
; shifts all move data one up (freeing 4th move slot)
503
WriteMonMoves_ShiftMoveData:
504
ld c, NUM_MOVES - 1
505
.loop
506
inc de
507
ld a, [de]
508
ld [hli], a
509
dec c
510
jr nz, .loop
511
ret
512
513
Evolution_FlagAction:
514
predef_jump FlagActionPredef
515
516
INCLUDE "data/pokemon/evos_moves.asm"
517
518