summaryrefslogtreecommitdiff
path: root/home/pokemon.asm
blob: d2add97e109145f0baa2bbf233a65d1ff52914f8 (plain)
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
INCLUDE "constants.asm"

if DEBUG
SECTION "3A4B", ROM0[$3A4B]
else
SECTION "3A4B", ROM0[$3A0F]
endc
GetMonHeader:: ; 3a4b (0:3a4b)
; copies the base stat data of a pokemon to wMonHeader
; INPUT:
; [wCurSpecies] = pokemon ID in dex order
	push bc
	push de
	push hl
	ldh a, [hROMBank]
	push af
	ld a, BANK(MonBaseStats)
	call Bankswitch
	ld a, [wCurSpecies]
	cp DEX_FD
	jr z, .egg
	dec a
	ld bc, MonBaseStatsEnd - MonBaseStats
	ld hl, MonBaseStats
	call AddNTimes
	ld de, wMonHeader
	ld bc, MonBaseStatsEnd - MonBaseStats
	call CopyBytes
	jr .done
.egg
	ld de, EggPicFront
	ln b, 5, 5 ; egg sprite dimension
	ld hl, wMonHSpriteDim
	ld [hl], b
	ld hl, wMonHFrontSprite
	ld [hl], e
	inc hl
	ld [hl], d
	jr .done
.done
	ld a, [wCurSpecies]
	ld [wMonHIndex], a
	pop af
	call Bankswitch
	pop hl
	pop de
	pop bc
	ret

if DEBUG
SECTION "3AED", ROM0[$3AED]
else
SECTION "3AED", ROM0[$3AB1]
endc

UncompressMonSprite:: ; 3aed (0:3aed)
; Uncompresses the front or back sprite of the specified mon
; assumes the corresponding mon header is already loaded
; hl contains offset to sprite pointer ($b for front or $d for back)
	ld a, [wMonDexIndex]
	and a
	ret z
	ld bc, wMonHeader
	add hl, bc
	cp DEX_ANNON
	jr z, .uncompress_annon
	ld a, [hli]
	ld [wSpriteInputPtr], a
	ld a, [hl]
	ld [wSpriteInputPtr + 1], a
	ld hl, MonSpriteBankList
	ld a, [wMonDexIndex]
	ld b, a
	; get Pokémon picture bank pointer from list
.loop
	ld a, BANK(MonSpriteBankList)
	call GetFarByte
	inc hl
	inc hl
	cp b
	jr c, .loop
	dec hl
	ld a, BANK(MonSpriteBankList)
	call GetFarByte
	jp UncompressSpriteData
.uncompress_annon
	ld a, [hli]
	ld h, [hl]
	ld l, a
	ld a, [wAnnonID]
	dec a
	add a
	add a
	ld c, a
	ld b, $00
	add hl, bc
	ld a, BANK(AnnonPicPtrs)
	call GetFarByte
	ld [wSpriteInputPtr], a
	inc hl
	ld a, BANK(AnnonPicPtrs)
	call GetFarByte
	ld [wSpriteInputPtr + 1], a
	ld a, BANK(AnnonPics)
	jp UncompressSpriteData

LoadMonFrontSprite:: ; 3b3f
; Uncompress Pokémon Front Sprite for
; mon currently loaded in wMonHeader
; to 0x9000
; de: destination location
; returns the sprite dimension in c
	push de
	ld hl, wMonHFrontSprite - wMonHeader
	call UncompressMonSprite
	ld hl, wMonHSpriteDim
	ld a, [hl]
	ld c, a
	pop de
	; fallthrough

LoadUncompressedSpriteData:: ; 3b4c (0:3b4c)
; postprocesses uncompressed sprite chunks to a 2bpp sprite and loads it into video ram
; calculates alignment parameters to place both sprite chunks in the center of the 7*7 tile sprite buffers
; de: destination location
; a,c:  sprite dimensions (in tiles of 8x8 each)
	push de
	and $0f
	ldh [hSpriteWidth], a ; each byte contains 8 pixels (in 1bpp), so tiles=bytes for width
	ld b, a
	ld a, $07
	sub b      ; 7-w
	inc a      ; 8-w
	srl a      ; (8-w)/2     ; horizontal center (in tiles, rounded up)
	ld b, a
	add a
	add a
	add a
	sub b      ; 7*((8-w)/2) ; skip for horizontal center (in tiles)
	ldh [hSpriteOffset], a
	ld a, c
	swap a
	and $0f
	ld b, a
	add a
	add a
	add a      ; 8*tiles is height in bytes
	ldh [hSpriteHeight], a
	ld a, $07
	sub b      ; 7-h         ; skip for vertical center (in tiles, relative to current column)
	ld b, a
	ldh a, [hSpriteOffset]
	add b      ; 7*((8-w)/2) + 7-h ; combined overall offset (in tiles)
	add a
	add a
	add a      ; 8*(7*((8-w)/2) + 7-h) ; combined overall offset (in bytes)
	ldh [hSpriteOffset], a
	ld a, $00
	call OpenSRAM
	ld hl, sSpriteBuffer0
	call ZeroSpriteBuffer   ; zero buffer 0
	ld de, sSpriteBuffer1
	ld hl, sSpriteBuffer0
	call AlignSpriteDataCentered    ; copy and align buffer 1 to 0 (containing the MSB of the 2bpp sprite)
	ld hl, sSpriteBuffer1
	call ZeroSpriteBuffer   ; zero buffer 1
	ld de, sSpriteBuffer2
	ld hl, sSpriteBuffer1
	call AlignSpriteDataCentered    ; copy and align buffer 2 to 1 (containing the LSB of the 2bpp sprite)
	call CloseSRAM
	pop de
	call InterlaceMergeSpriteBuffers
	ret

ZeroSpriteBuffer:: ; 3ba1 (0:3ba1)
; fills the sprite buffer (pointed to in hl) with zeros
	ld bc, SPRITEBUFFERSIZE
	xor a
	jp ByteFill

AlignSpriteDataCentered:: ; 3ba8 (0:3ba8)
; copies and aligns the sprite data properly inside the sprite buffer
; sprite buffers are 7*7 tiles in size, the loaded sprite is centered within this area
	ldh a, [hSpriteOffset]
	ld c, a
	ld b, $00
	add hl, bc
	ldh a, [hSpriteWidth]
	ld b, a
	ldh a, [hSpriteHeight]
	ld c, a
.columnLoop
	push bc
	push hl
.columnInnerLoop
	ld a, [de]
	inc de
	ld [hli], a
	dec c
	jr nz, .columnInnerLoop
	pop hl
	ld bc, 7*8    ; 7 tiles
	add hl, bc    ; advance one full column
	pop bc
	dec b
	jr nz, .columnLoop
	ret

InterlaceMergeSpriteBuffers:: ; 3bc6 (0:3bc6)
; combines the (7*7 tiles, 1bpp) sprite chunks in buffer 0 and 1 into a 2bpp sprite located in buffer 1 through 2
; in the resulting sprite, the rows of the two source sprites are interlaced
; de: output address
	ld a, $00
	call OpenSRAM
	push de
	call _InterlaceMergeSpriteBuffers
	pop hl
	ld de, sSpriteBuffer1
	ld c, (2 * SPRITEBUFFERSIZE) / 16 ; $31, number of 16 byte chunks to be copied
	ldh a, [hROMBank]
	ld b, a
	call Get2bpp
	call CloseSRAM
	ret

_InterlaceMergeSpriteBuffers:: ; 3bdf (0:3bdf)
; actual implementation of InterlaceMergeSpriteBuffers
; sprite flipping is now done during interlace merge loop
; and not as second loop after regular interlace merge
; to save time
	ld a, [wSpriteFlipped]
	and a
	jr nz, .flipped
	ld hl, sSpriteBuffer2 + (SPRITEBUFFERSIZE - 1) ; destination: end of buffer 2
	ld de, sSpriteBuffer1 + (SPRITEBUFFERSIZE - 1) ; source 2: end of buffer 1
	ld bc, sSpriteBuffer0 + (SPRITEBUFFERSIZE - 1) ; source 1: end of buffer 0
	ld a, SPRITEBUFFERSIZE/2 ; $c4
	ldh [hSpriteInterlaceCounter], a
.interlaceLoop
	ld a, [de]
	dec de
	ld [hld], a   ; write byte of source 2
	ld a, [bc]
	dec bc
	ld [hld], a   ; write byte of source 1
	ld a, [de]
	dec de
	ld [hld], a   ; write byte of source 2
	ld a, [bc]
	dec bc
	ld [hld], a   ; write byte of source 1
	ldh a, [hSpriteInterlaceCounter]
	dec a
	ldh [hSpriteInterlaceCounter], a
	jr nz, .interlaceLoop
	ld a, [wSpriteFlipped] ; always notFlipped; flip routine was optimized
	and a
	jr z, .notFlipped
	ld bc, 2*SPRITEBUFFERSIZE
	ld hl, sSpriteBuffer1
.swapLoop
	swap [hl]    ; if flipped swap nybbles in all bytes
	inc hl
	dec bc
	ld a, b
	or c
	jr nz, .swapLoop
.notFlipped
	ret
.flipped
	ld hl, sSpriteBuffer2 + (SPRITEBUFFERSIZE - 1) ; destination: end of buffer 2
	ld de, sSpriteBuffer1 + (SPRITEBUFFERSIZE - 1) ; source 2: end of buffer 1
	ld bc, sSpriteBuffer0 + (SPRITEBUFFERSIZE - 1) ; source 1: end of buffer 0
	ld a, SPRITEBUFFERSIZE / 2 ; $c4
	ldh [hSpriteInterlaceCounter], a
.interlaceLoopFlipped
	ld a, [de]
	dec de
	swap a
	ld [hld], a   ; write byte of source 2
	ld a, [bc]
	dec bc
	swap a
	ld [hld], a   ; write byte of source 1
	ld a, [de]
	dec de
	swap a
	ld [hld], a   ; write byte of source 2
	ld a, [bc]
	dec bc
	swap a
	ld [hld], a   ; write byte of source 1
	ldh a, [hSpriteInterlaceCounter]
	dec a
	ldh [hSpriteInterlaceCounter], a
	jr nz, .interlaceLoopFlipped
	ret