-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsub.asm
525 lines (416 loc) · 10.8 KB
/
sub.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;
.module SUB
;
_SUBX = OUSER+0 ; don't change
_SUBY = OUSER+1 ; don't change
_SCRADL = OUSER+2
_SCRADH = OUSER+3
_COLO = OUSER+4
_ROWO = OUSER+5
_COLLTAB = OUSER+6
_EXPLOCT = OUSER+6 ; overloads follow
_WAIT = OUSER+7
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;
; The submarine object.
;
subfunction:
submvfunc = $+1
call playermovesub ; listen to controls by default, can be changed though
ld a,(iy+_SUBX) ; pixel -> char conversion
cp $ff
DIEZ
_drawsub:
ld a,(finescroll)
and 6 ; 2 pixel scroll
ld c,a
ld a,(iy+_SUBX) ; pixel -> char conversion
add a,c
srl a
srl a
srl a
ld l,a
ld h,0
ld de,(scrollpos)
add hl,de
ld (subcharx),hl ; expose this for the enemies
ld a,(iy+_SUBY) ; div by 8 to get character line then mul by 600
srl a
srl a
srl a
call mulby600 ; de = a * 600
add hl,de ; character offset relative to visible window
ld de,D_BUFFER
add hl,de
ld (iy+_SCRADL),l ; sub's address in the display memory
ld (iy+_SCRADH),h
; find the character codes that appear under the sub in its new position
; use the map cache as the code source because the display is dirty at this point
; copy the pixel data corresponding to the characters under the sub
; to a cache of 6x8 bytes - effectively a tiny bitmap
;
; on-screen: $b0 $b2 $b4
; $b1 $b3 $b5
;
; it's arranged like this because the rendering of the sub char is
; easier using columns, even if it makes other things harder.
res 6,h ; point hl at mapcache in high memory
set 7,h ; hl is source pointer for character data
push hl
ld de,charcache ; b0
call copycharx
ld (iy+_COLLTAB),a ; collision index 0, store character code
ld de,charcache+16 ; ... char b2
call copycharx
ld (iy+_COLLTAB+4),a ; coll. idx. 2
ld de,charcache+32 ; b4
call copycharx
ld (iy+_COLLTAB+8),a ; c.i. 4
pop hl
ld bc,600
add hl,bc
ld de,charcache+8 ; b1
call copycharx
ld (iy+_COLLTAB+2),a ; c.i 1
ld de,charcache+24 ; b3
call copycharx
ld (iy+_COLLTAB+6),a ; c.i 3
ld de,charcache+40 ; b5
call copycharx
ld (iy+_COLLTAB+10),a ; c.i 5
xor a ; zero out collision bits
ld (iy+_COLLTAB+1),a
ld (iy+_COLLTAB+3),a
ld (iy+_COLLTAB+5),a
ld (iy+_COLLTAB+7),a
ld (iy+_COLLTAB+9),a
ld (iy+_COLLTAB+11),a
ld a,_COLLTAB+1
ld (iy+_COLO),a
; now we've effectively built our tiny bitmap and cleared out the collision bits,
; we can render the sub into it and collect any collision pixels
;
; choose which set of 3 pre-scrolled sub tiles to use.
ld a,(finescroll)
and 6 ; 2 pixel scroll
ld c,a
ld a,(iy+_SUBX) ; pixel offset 0..7
add a,c
and 7
ld c,a
add a,a
add a,c ; * 3, a is offset to set of 3 characters
sla a
sla a
sla a ; * 8, a is offset to 1st byte of sub char data
; get pointers to sub pixel data within the character set
ld h,subpix / 256 ; form address in character set $22xx
ld l,a
ld de,charcache ; pointer to 1st byte within column of 16 rows inside mini bitmap
ld a,(iy+_SUBY) ; that we will render to
and 7
or e
ld e,a
ld b,3 ; 3 characters
_rendersub:
push bc
; copy 8 sub pixels into bg bitmap
ld b,8 ; 8 lines
ld a,(iy+_SUBY) ; keep track of the row number that we're rendering into,
and 7 ; so that we can track collision data on a per character cell basis
ld (iy+_ROWO),a
ld a,(iy+_COLO) ; init collision indices
ld (colidx1),a
ld (colidx2),a
_column:
ld c,(hl) ; get sub pixels
ld a,(de) ; get bg pixels
push af
and c ; merge sub into background
ld (de),a
pop af ; get bg pixels
or c ; or together
xor $ff ; any 0 bits are collisions
ld c,a
colidx1 = $+2
colidx2 = $+6
ld a,(iy+0) ; update collision bits for this background cell
or c
ld (iy+0),a
ld a,(iy+_ROWO) ; when rendering hits row 8 bump collision data index
inc a
ld (iy+_ROWO),a
cp 8
jr nz,{+}
ld a,(colidx1)
inc a
inc a
ld (colidx1),a
ld (colidx2),a
+:
inc hl
inc de
djnz _column
; step across the dest bitmap to the next column
; we know the address won't overflow the bottom 8 bits
ld a,8
add a,e
ld e,a
ld a,(iy+_COLO) ; iy+ouser progression: +1, (+3), +5, (+7), +9, (+11)
inc a
inc a
inc a
inc a
ld (iy+_COLO),a
pop bc
djnz _rendersub
; all rendered. now draw the min i bitmap containing the sub to the screen
; b0 b2 b4
; b1 b3 b5
ld e,(iy+_SCRADL)
ld d,(iy+_SCRADH)
ld hl,(dlp)
ld (hl),e
inc hl
ld (hl),d
inc hl
ld (hl),$b0
inc hl
inc de
ld (hl),e
inc hl
ld (hl),d
inc hl
ld (hl),$b2
inc hl
inc de
ld (hl),e
inc hl
ld (hl),d
inc hl
ld (hl),$b4
inc hl
ld a,(iy+_SUBY)
cp $48
jr nc,_nosecondline
push hl
ld hl,600-2
add hl,de
ex de,hl
pop hl
ld (hl),e
inc hl
ld (hl),d
inc hl
ld (hl),$b1
inc hl
inc de
ld (hl),e
inc hl
ld (hl),d
inc hl
ld (hl),$b3
inc hl
inc de
ld (hl),e
inc hl
ld (hl),d
inc hl
ld (hl),$b5
inc hl
_nosecondline:
ld (dlp),hl
YIELD
ld d,_COLLTAB
ld e,_COLLTAB+1
ld b,6
_checkcoll:
ld a,d
ld (chkidx1),a
ld a,e
ld (chkidx2),a
chkidx1 = $+2
chkidx2 = $+5
ld c,(iy+0) ; character cell content
ld a,(iy+0) ; pixel collision data
and a ; clears carry
call nz,_testcollision ; test the collision
.ifndef INVINCIBLE
jr c,_subdead
.endif
inc d
inc d
inc e
inc e
djnz _checkcoll
; no collision - asphyxiation?
ld a,(collision) ; set z flag ahead of call to updateair
and a
ld a,(iy+_SUBY) ; only update air if sub not dead/celebrating boss defeat
#ifndef INVINCIBLE
call z,updateair
#endif
ld a,(airlevel)
or a
jp nz,subfunction
; sub is dead, explo-o-o-o-ode
_subdead:
ld a,1
ld (collision),a
; trigger some explosions
ld (iy+_EXPLOCT),6
ld hl,explooff
ld a,(FrameCounter)
and 6
add a,l
ld l,a
adc a,h
sub l
ld h,a
_subsubexplo:
ld e,(hl)
inc hl
ld d,(hl)
inc hl
ld (iy+_COLO),l
ld (iy+_COLO+1),h
ld l,(iy+_SCRADL)
ld h,(iy+_SCRADH)
add hl,de
push hl
ld bc,explosion
call objectafterthis
ex de,hl
pop de
ld (hl),e
inc hl
ld (hl),d
ld a,r
and 3
add a,5
ld (iy+_WAIT),a
-: YIELD
dec (iy+_WAIT)
jr nz,{-}
ld l,(iy+_COLO)
ld h,(iy+_COLO+1)
dec (iy+_EXPLOCT)
jr nz,_subsubexplo
ld a,SFX_SUBDEAD
call AFXPLAY
DIE
_testcollision:
ld a,c
and a
ret z
ld a,(collision) ; (causes no collisions when boss just died)
or c
cp $a0 ; no collision with chars $a0 or greater
ret
explooff:
.word 0,601,600,2,1,602
.word 0,601,600,2,1,602
exploptr:
.word 0
.align 16
charcache:
.fill 48
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;
; Move the sub using the user controls.
;
playermovesub:
ld a,(fire) ; is fire held? if not, carry on as normal
cp $ff
jr nz,{+}
ld a,(FrameCounter) ; if fire is held, only allow movement every other frame
and 1
jr z,_checkfire
+: push iy
pop hl
ld de,_SUBY
add hl,de
ld a,(up) ; min y = 6
bit 0,a
jr z,_checkdown
cp 3
jr z,_checkdown
cp 7
jr z,_checkdown
ld a,(hl)
cp 7
jr c,_checkdown
cp 0
jr z,_checkdown
dec (hl)
cp 7 ; play the sub surfacing sound if we hit line 6
ld a,SFX_SUBSURFACE
call z,AFXPLAY
_checkdown:
ld a,(down) ; max y = $48
bit 0,a
jr z,_checkleft
cp 3
jr z,_checkleft
cp 7
jr z,_checkleft
ld a,(hl)
cp $48
jr nc,_checkleft
inc (hl)
_checkleft:
push iy
pop hl
ld de,_SUBX
add hl,de
ld a,(left) ; min x = 0
and 1
jr z,_checkright
ld a,(hl)
and a
jr z,_checkright
dec (hl)
_checkright:
ld a,(right) ; max x = 160
and 1
jr z,_checkfire
ld a,(hl)
cp $a0
jr nc,_checkfire
inc (hl)
_checkfire:
ld a,(fire)
cp 1
ret nz
ld a,(bulletCount)
or a
ret nz
ld bc,obullet
call getobject
call initobject
call insertobject_beforethis
ldi ; copy pixel x & y
ldi
ret
;-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
;
; Move the sub after killing the boss using remote control.
;
bossexit:
ld a,(iy+_SUBY)
cp 5*8
jr z,_testx
jr c,_godown
dec a
dec a ; cheaper than a JR
_godown:
inc a
ld (iy+_SUBY),a
ret
_testx:
ld a,(iy+_SUBX)
inc a
ret z
ld (iy+_SUBX),a
ret