-
Notifications
You must be signed in to change notification settings - Fork 15
/
actor.s
1407 lines (1294 loc) · 43.8 KB
/
actor.s
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
AD_FLAGS = $00
AD_HEALTH = $01
AD_UPDATE = $02
AD_DRAW = $04
AD_GRAVITY = $06
AD_SIDEOFFSET = $07
AD_TOPOFFSET = $08
AD_DAMAGEMOD = $09
AD_TAKEDAMAGE = $0a
GRP_HEROES = $00
GRP_ENEMIES = $01
GRP_NEUTRAL = $02
AF_GROUPFLAGS = $03
AF_CLIMB = $04
AF_NOINTERPOLATION = $40
AF_NOREMOVECHECK = $80
NO_BOUNDS = $ff
NO_MODIFY = 8
ORG_TEMP = $00 ;Temporary actor, may be overwritten by global or leveldata
ORG_GLOBAL = $40 ;Global important actor
ORG_LEVELDATA = $80 ;Leveldata actor, added/removed at level change
ORG_NONPERSISTENT = $ff
ORG_LEVELNUM = $3f
LVLACTSEARCH = 24
MAX_IMPULSESPEED = 6*8
DF_LETHAL = $01
DF_IMPULSEBITS = $7e
DF_HITFLASH = $ff
STATE_UPRIGHT = 0
STATE_CLIMB = 1
USESCRIPT = $8000
; Draw all actors as part of the first (non-interpolated) frame, then add new actors for next frame's update
;
; Parameters: X,Y lowbyte subtract for actor render
; Returns: -
; Modifies: A,X,Y,several ZP vars
DrawActors: stx DA_SprSubXL+1
sty DA_SprSubYL+1
lda #$4c ;Enable sprite draw operation
sta DLS_ComplexNoConnect
lda #$06
sta DLS_SimpleConnectDone
ldx cacheFrame
stx DSpr_LastCacheFrame+1
DA_IncCacheFrame:
inx ;Increment framenumber for sprite cache
beq DA_IncCacheFrame ;(framenumber is never 0)
stx cacheFrame
DA_CheckCacheAge:
txa
and #MAX_CACHESPRITES-1
tay
txa
sec ;If age stored in cache is older than significant, reset
sbc cacheSprAge,y ;to prevent overflow error (check one sprite per frame)
bpl DA_CacheAgeOK
lda #$00
sta cacheSprAge,y
DA_CacheAgeOK: ldx #$00 ;Reset amount of used sprites
stx numSpr
stx numBounds
DA_Loop: ldy actT,x
beq DA_Next
lda actDataTblLo-1,y
sta actLo
lda actDataTblHi-1,y
sta actHi
lda actXL,x ;Convert actor coordinates to screen + store current position for interpolation
sta actPrevXL,x
sec
DA_SprSubXL: sbc #$00
sta xLo
lda actXH,x
DA_StorePrevXH: sta actPrevXH,x
DA_SprSubXH: sbc #$00
cmp #MAX_ACTX ;Skip if significantly outside
bcs DA_Outside
tay
lda xLo
lsr
lsr
lsr
lsr
and #$07
ora xCoordTbl,y
sta xLo ;X pos
lda actYL,x
sta actPrevYL,x
sec
DA_SprSubYL: sbc #$00
sta yLo
lda actYH,x
DA_StorePrevYH: sta actPrevYH,x
DA_SprSubYH: sbc #$00
cmp #MAX_ACTY
bcs DA_Outside
tay
lda yLo
lsr
lsr
lsr
and #$0f
ora yCoordTbl,y
sta yLo ;Y pos
stx actIndex
jsr ActorDrawCall ;Perform the draw call (produces sprites)
ldx actIndex
bpl DA_Next
DA_Outside: lda #NO_BOUNDS ;Mark as having no bounds
sta actBounds,x
DA_Next: inx
cpx #MAX_ACT
bcc DA_Loop
ldx numBounds ;Make bounds endmark to simplify collision checks
lda #$ff
sta boundsAct,x
DA_FillSprites: ldx numSpr ;If less sprites used than last frame, set unused Y-coords to max.
DA_FillSpritesLoop:
sta sprY,x
inx
DA_LastSprIndex:cpx #MAX_SPR
bcc DA_FillSpritesLoop
lda numSpr
sta DA_LastSprIndex+1 ;Store used sprite count for next frame's cleanup
lda #$60 ;Disable sprite render now
sta DLS_ComplexNoConnect
sta DLS_SimpleConnectDone
; Add actors to screen. Also perform line-of-sight check for one actor at a time if requested
;
; Parameters: -
; Returns: -
; Modifies: A,X,Y,various
AddActors: lda mapX
clc
adc #23
sta AA_RightCmp+1
sta UA_RemoveRightCmp+1
lda mapX
sbc #$02 ;C=0, subtract one more
bcs AA_LeftLimitOK
lda #$00
AA_LeftLimitOK: sta AA_LeftCmp+1
sta UA_RemoveLeftCmp+1
lda mapY
clc
adc #13
sta AA_BottomCmp+1
sta UA_RemoveBottomCmp+1
sbc #13 ;C=0, subtract one more
bcs AA_TopLimitOK
lda #$00
AA_TopLimitOK: sta AA_TopCmp+1
sta UA_RemoveTopCmp+1
AA_Start: ldx #$00
AA_Loop: ldy lvlActT,x
beq AA_Skip
lda lvlActOrg,x ;Must be either a current level's leveldata actor,
bmi AA_LevelOK ;or a global/temp actor with matching level
and #ORG_LEVELNUM
cmp levelNum
bne AA_Skip
AA_LevelOK: lda lvlActZ,x
cmp zoneNum
bne AA_Skip
lda lvlActX,x
AA_LeftCmp: cmp #$00
bcc AA_Skip
AA_RightCmp: cmp #$00
bcs AA_Skip
lda lvlActY,x
AA_TopCmp: cmp #$00
bcc AA_Skip
cpy #$80 ;No bottom adjust for items
bcs AA_BottomCmp
adc actBottomAdjustTbl-1,y ;Bottom check adjust for tall actors (C=1 here, so they must be one less)
AA_BottomCmp: cmp #$00
bcs AA_Skip
stx zpDestLo
jsr AddLevelActor
ldx zpDestLo
AA_Skip: inx
AA_EndCmp: cpx #LVLACTSEARCH
bne AA_Loop
cpx #MAX_LVLACT
bcc AA_IndexNotOver
ldx #$00
clc
AA_IndexNotOver:stx AA_Start+1 ;Store search start & endpositions for next frame
txa
adc #LVLACTSEARCH
sta AA_EndCmp+1
AA_LineCheckActor:
ldx #ACTI_LASTNPC
dex
bne AA_LineNotOver
ldx #ACTI_LASTNPC
AA_LineNotOver: stx AA_LineCheckActor+1
lda actTargetFlags,x ;Actor has requested linecheck?
bpl AA_LineCheckSkip
jmp DoLineCheck ;Perform one per frame
AA_LineCheckSkip:
rts
; Update actors, interpolate sprites & scan for levelobject at player
;
; Parameters: X,Y lowbyte subtract for actor render
; Returns: -
; Modifies: A,X,Y,various
UpdateActors: txa ;Calculate scrolling change from last frame
eor #$ff
sec
adc DA_SprSubXL+1
and #$7f
lsr
lsr
lsr
lsr
cmp #$04
bcc UA_ScrollXNotNeg
ora #$f8
UA_ScrollXNotNeg:
sta IA_ScrollXAdjust+1
tya
eor #$ff
sec
adc DA_SprSubYL+1
and #$7f
lsr
lsr
lsr
cmp #$08
bcc UA_ScrollYNotNeg
ora #$f0
UA_ScrollYNotNeg:
sta IA_ScrollYAdjust+1
ldx #MAX_ACT-1
stx Irq5_CharAnimFlag+1 ;Enable char animation
bne UA_ActorLoop
UA_Skip: dex
bpl UA_ActorLoop
jmp UA_ActorsDone
UA_Remove: jsr RemoveLevelActor ;Returns with A=0
dex
bmi UA_ActorsDone
UA_ActorLoop: ldy actT,x
beq UA_Skip
stx actIndex
lda actFlags,x ;Perform remove check?
sta currFlags
asl
bpl UA_AllowInterpolation
sta actPrevYH,x ;High bit set in prevYH = interpolation disabled
UA_AllowInterpolation:
bcs UA_NoRemoveCheck
lda actXH,x
UA_RemoveLeftCmp:
cmp #$00
bcc UA_Remove
UA_RemoveRightCmp:
cmp #$00
bcs UA_Remove
lda actYH,x
UA_RemoveTopCmp:cmp #$00
bcc UA_Remove
adc actBottomAdjustTbl-1,y ;Bottom check adjust for tall actors (C=1 here, so they must be one less)
bmi UA_NoRemoveCheck
UA_RemoveBottomCmp:
cmp #$00
bcs UA_Remove
UA_NoRemoveCheck:
lda actDataTblLo-1,y
sta actLo
lda actDataTblHi-1,y
sta actHi
ldy #AD_UPDATE+1
UA_UpdateCall: jsr ActorCall ;Perform the update call
dex
bpl UA_ActorLoop
UA_ActorsDone: stx IA_LastAct+1
inx ;X=0, fall through to InterpolateActors
; Interpolate sprites according to actor & scroll movement
;
; Parameters: -
; Returns: -
; Modifies: A,X,Y,various
InterpolateActors:
IA_SprLoop: lda sprC,x ;Process flickering
cmp #COLOR_FLICKER
bcc IA_NoFlicker
eor #COLOR_INVISIBLE ;If sprite is invisible on this frame,
sta sprC,x ;no need to calculate & add offset
bmi IA_Next
IA_NoFlicker: and #COLOR_OVERLAY ;Portrait overlay sprite that shouldn't scroll?
bne IA_Next
ldy sprAct,x
IA_LastAct: cpy #$00
beq IA_SameAct
IA_CalculateMove:
sty IA_LastAct+1
sec
lda actPrevYH,y ;If interpolation disable marked with high bit in prevYH,
bmi IA_NoInterpolation ;use only the scrolling offset
lda actXL,y ;Calculate average movement of actor in X-direction
sbc actPrevXL,y ;C=1 from the actor index compare, which is increasing
pha
lda actXH,y
sbc actPrevXH,y
sta zpSrcLo
pla
asl
lsr zpSrcLo
ror
lsr
lsr
lsr
lsr
lsr
bit zpSrcLo ;Sign is now in bit 6
bvc IA_XMovePos
ora #$f8
IA_XMovePos:
IA_ScrollXAdjust:
adc #$00 ;Add scrolling
sta zpDestLo ;Store offset for sprite movement
lda actYL,y ;Calculate average movement of actor in Y-direction
sec ;(max. 31 pixels)
sbc actPrevYL,y
pha
lda actYH,y
sbc actPrevYH,y
sta zpSrcLo
pla
asl
lsr zpSrcLo
ror
lsr
lsr
lsr
lsr
bit zpSrcLo
bvc IA_YMovePos
ora #$f0
IA_YMovePos:
IA_ScrollYAdjust:
adc #$00 ;Add scrolling
IA_StoreYAdjust:sta zpDestHi
IA_SameAct: lda sprX,x ;Now add the calculated interpolation offsets
clc
adc zpDestLo
sta sprX,x
lda sprY,x
clc
adc zpDestHi
sta sprY,x
IA_Next: inx
cpx numSpr
bcc IA_SprLoop
bcs IA_Done
IA_NoInterpolation:
lda IA_ScrollXAdjust+1 ;No interpolation: skip average movement calculation,
sta zpDestLo ;use just scrolling offset
lda IA_ScrollYAdjust+1
bcs IA_StoreYAdjust
IA_Done:
; Scan levelobject at player
;
; Parameters: -
; Returns: -
; Modifies: A,X,Y,various
ScanLevelObjects:
ldx actXH+ACTI_PLAYER ;Rescan objects if player occupies different block now
ldy actYH+ACTI_PLAYER
SLO_LastX: cpx #$ff
bne SLO_Rescan
SLO_LastY: cpy #$ff
beq SLO_SkipScan
SLO_Rescan: stx SLO_LastX+1
sty SLO_LastY+1
lda #<zoneLvlObjList
sta SLO_ScanLoop+1
SLO_ScanLoop: ldy zoneLvlObjList
bmi SLO_ScanDone ;Reached endmark?
inc SLO_ScanLoop+1
lda actXH+ACTI_PLAYER
cmp lvlObjX,y
bcc SLO_ScanLoop ;Player to the left of levelobject
lda lvlObjSize,y
and #$03
clc
adc lvlObjX,y
cmp actXH+ACTI_PLAYER
bcc SLO_ScanLoop ;Player to the right of levelobject
lda actYH+ACTI_PLAYER
cmp lvlObjY,y
bcc SLO_ScanLoop ;Player above levelobject
lda lvlObjSize,y
and #$0c
lsr
lsr
sec
adc lvlObjY,y
cmp actYH+ACTI_PLAYER
bcc SLO_ScanLoop ;Player (too much) below levelobject
SLO_ScanDone: sty atObj
SLO_SkipScan: rts
; Perform actor draw call. Set color override for damage flash
;
; Parameters: X actor index, Y ycoord table index, actLo-actHi data structure
; Returns: -
; Modifies: A,X,Y,various
ActorDrawCall: lda numBounds
sta actBounds,x ;Store bounds startindex
cpy #$10
lda #$00
sta actColorOr
ror
sta actYMSB
ldy actDmgFlags,x
iny
bne ADC_NoDamageFlash
inc actDmgFlags,x
inc actColorOr
lda #$f0
skip2
ADC_NoDamageFlash:
lda #$ff
sta actColorAnd
ldy #AD_DRAW+1 ;Then draw
; Perform actor call
;
; Parameters: X actor index, Y offset to highbyte in actor data structure, actLo-actHi data structure
; Returns: -
; Modifies: A,(potentially X),Y,various
ActorCall: lda (actLo),y
bpl AC_NoScript
AC_UseScript: stx ES_Param+1
and #$7f
tax
dey
lda (actLo),y
; Execute loadable code (script)
;
; Parameters: A script entrypoint, X script file
; Returns: -
; Modifies: A,X,Y,various
ExecScript: ldy fileHi+C_FIRSTSCRIPT,x
beq ES_NotInMemory
ES_InMemory: sty zpDestHi ;Fast path when the resource file is already in memory
ldy fileLo+C_FIRSTSCRIPT,x
sty zpDestLo
asl
tay
lda (zpDestLo),y
sta zpSrcLo
iny
lda (zpDestLo),y
sta zpSrcHi
lda #$00
sta fileAge+C_FIRSTSCRIPT,x ;Remember to reset age
ES_Param: ldx #$00
jmp (zpSrcLo)
ES_NotInMemory: jsr GetScriptResource
jmp ES_Param
AC_NoScript: sta AC_Jump+2
dey
lda (actLo),y
sta AC_Jump+1
AC_Jump: jmp $1000
; Move actor in X-direction
;
; Parameters: X actor index, A speed
; Returns: -
; Modifies: A
MoveActorXNegOrPos:
bpl MoveActorX
MoveActorXNeg: clc
eor #$ff
adc #$01
MoveActorX: cmp #$80
bcc MAX_Pos
MAX_Neg: clc
adc actXL,x
bpl MAX_Done
dec actXH,x
MAX_Over: and #$7f
MAX_Done: sta actXL,x
rts
MAX_Pos: adc actXL,x
bpl MAX_Done
inc actXH,x
bcc MAX_Over ;C cannot possibly be 1 here
; Move actor in Y-direction
;
; Parameters: X actor index, A speed
; Returns: -
; Modifies: A
MoveActorYNegOrPos:
bpl MoveActorY
MoveActorYNeg: clc
eor #$ff
adc #$01
MoveActorY: cmp #$80
bcc MAY_Pos
MAY_Neg: clc
adc actYL,x
bpl MAY_Done
dec actYH,x
MAY_Over: and #$7f
MAY_Done: sta actYL,x
rts
MAY_Pos: adc actYL,x
bpl MAY_Done
inc actYH,x
bcc MAY_Over
; Apply impulse from last damage to actor
;
; Parameters: X actor index, A damage flags + impulse
; Returns: -
; Modifies: A,Y,zpSrcLo
ApplyDamageImpulse:
cmp #$80 ;Dir bit to carry
and #DF_IMPULSEBITS
ldy #MAX_IMPULSESPEED
; Accelerate actor in X-direction with either positive or negative acceleration
;
; Parameters: X actor index, A absolute acceleration, Y absolute speed limit, C direction (0 = right, 1 = left)
; Returns:
; Modifies: A,Y,zpSrcLo
AccActorXNegOrPos:
bcc AccActorXNoClc
; Accelerate actor in negative X-direction
;
; Parameters: X actor index, A absolute acceleration, Y absolute speed limit
; Returns:
; Modifies: A,Y,zpSrcLo
AccActorXNeg: sec
AccActorXNegNoSec:
sty zpSrcLo
sbc actSX,x
bmi AAX_NegDone
cmp zpSrcLo
bcc AAX_NegDone2
tya
AAX_NegDone: clc
AAX_NegDone2: eor #$ff
adc #$01
AAX_Done: sta actSX,x
AAX_Done2: rts
; Accelerate actor in positive X-direction
;
; Parameters: X actor index, A acceleration, Y speed limit
; Returns: -
; Modifies: A,zpSrcLo
AccActorX: clc
AccActorXNoClc: sty zpSrcLo
adc actSX,x
bmi AAX_Done ;If speed negative, can not have reached limit yet
cmp zpSrcLo
bcc AAX_Done
tya
bcs AAX_Done
; Brake X-speed of an actor towards zero
;
; Parameters: X Actor index, A deceleration (always positive)
; Returns: -
; Modifies: A, zpSrcLo
BrakeActorX: sta zpSrcLo
lda actSX,x
beq AAX_Done2
bmi BAct_XNeg
BAct_XPos: sec
sbc zpSrcLo
bpl AAX_Done
BAct_XZero: lda #$00
beq AAX_Done
BAct_XNeg: clc
adc zpSrcLo
bpl BAct_XZero
bmi AAX_Done
; Accelerate actor in Y-direction with either positive or negative acceleration
;
; Parameters: X actor index, A absolute acceleration, Y absolute speed limit, C direction (0 = down, 1 = up)
; Returns:
; Modifies: A,Y,zpSrcLo
AccActorYNegOrPos:
bcc AccActorYNoClc
; Accelerate actor in negative Y-direction
;
; Parameters: X actor index, A absolute acceleration, Y absolute speed limit
; Returns:
; Modifies: A,Y,zpSrcLo
AccActorYNeg: sec
AccActorYNegNoSec:
sty zpSrcLo
sbc actSY,x
bmi AAY_NegDone
cmp zpSrcLo
bcc AAY_NegDone2
tya
AAY_NegDone: clc
AAY_NegDone2: eor #$ff
adc #$01
AAY_Done: sta actSY,x
AAY_Done2: rts
; Accelerate actor in positive Y-direction
;
; Parameters: X actor index, A acceleration, Y speed limit
; Returns: -
; Modifies: A,zpSrcLo
ApplyGravity: ldy #AD_GRAVITY
lda (actLo),y
ldy #COMMON_MAX_YSPEED ;Gravity acceleration
AccActorY: clc
AccActorYNoClc: sty zpSrcLo
adc actSY,x
bmi AAY_Done ;If speed negative, can not have reached limit yet
cmp zpSrcLo
bcc AAY_Done
tya
bcs AAY_Done
; Brake Y-speed of an actor towards zero
;
; Parameters: X actor index, A deceleration (always positive)
; Returns: -
; Modifies: A, zpSrcLo
BrakeActorY: sta zpSrcLo
lda actSY,x
beq AAY_Done2
bmi BAct_YNeg
BAct_YPos: sec
sbc zpSrcLo
bpl AAY_Done
BAct_YZero: lda #$00
beq AAY_Done
BAct_YNeg: clc
adc zpSrcLo
bpl BAct_YZero
bmi AAY_Done
; Process animation delay
;
; Parameters: X actor index, A animation speed-1 (in frames)
; Returns: C=1 delay exceeded, animationdelay reset
; Modifies: A, zpSrcLo
AnimationDelay: sta zpSrcLo
lda actFd,x
cmp zpSrcLo
bcs AD_Over
inc actFd,x
rts
; Perform one-shot animation with delay
;
; Parameters: Y end frame, A animation speed-1 (in frames)
; Returns: C=1 end reached
; Modifies: A, zpSrcLo-Hi
OneShotAnimation:
sta zpSrcLo
sty zpSrcHi
lda actFd,x
cmp zpSrcLo
bcs OSA_NextFrame
inc actFd,x
rts
OSA_NextFrame: lda actF1,x
cmp zpSrcHi
bcs OSA_Over
inc actF1,x
ResetAnimDelay:
AD_Over: lda #$00
sta actFd,x
OSA_Over: rts
; Transform actor & reset animation
;
; Parameters: X actor index, A new type
; Returns: A=0
; Modifies: A
TransformActor: sta actT,x
jsr ResetAnimDelay ;Returns with A=0
sta actF1,x
rts
; Reset actor speeds, also set actor as grounded
;
; Parameters: X actor index
; Returns: A=0, C=0 if actor is complex
; Modifies: A
ResetSpeed: lda #MB_GROUNDED
sta actMB,x
lda #$00
sta actSX,x
sta actSY,x
rts
; Disable movement interpolation for the current frame.
;
; Parameters: X actor index
; Returns: A=$ff
; Modifies: A
NoInterpolation:lda #$ff
sta actPrevYH,x
rts
; Add actor from leveldata
;
; Parameters: X leveldata index, must also be in zpDestLo
; newActType new actor type
; Returns: -
; Modifies: A,X,Y,zpSrcLo-Hi,zpDestHi,actLo-Hi
AddLevelActor: lda lvlActT,x
pha
bmi ALA_IsItem
ALA_IsNPC: jsr GetFreeNPC
bcs ALA_Fail
pla
sta actT,y
lda lvlActWpn,x
sta loadTempReg
and #$7f
sta actWpn,y
lda loadTempReg
and #$80
jmp InitLevelActor ;NPC facing direction
ALA_IsItem: jsr GetFreeNonNPC
bcs ALA_Fail
lda #ACT_ITEM
sta actT,y
lda #$00
jsr InitLevelActor
jsr GetBlockInfo ;Check for ground / locker shelf, start item falling if not grounded
and #BI_GROUND|BI_DROP
bne ALA_ItemGrounded
sta actMB,x
ALA_ItemGrounded:
pla
and #$7f
sta actF1,x
ldy zpDestLo
lda lvlActWpn,y
sta actHp,x
rts
ALA_Fail: pla
rts
; Remove all actors except player
;
; Parameters: X actor index
; Returns: -
; Modifies: A,Y,zpSrcLo
RemoveLevelActors:
ldx #MAX_ACT-1
RLA_Loop: lda actT,x
beq RLA_Next
jsr RemoveLevelActor
RLA_Next: dex
bne RLA_Loop
rts
; Remove actor and return to leveldata if applicable
;
; Parameters: X actor index
; Returns: -
; Modifies: A,Y,zpSrcLo
RemoveLevelActor:
lda actLvlDataOrg,x
cmp #ORG_NONPERSISTENT
beq RemoveActor
pha
jsr GetLevelActorIndex
pla
sta lvlActOrg,y ;Store levelnumber / persistence mode
lda actXH,x ;Store block coordinates
sta lvlActX,y
lda actYH,x
sta lvlActY,y
lda zoneNum
sta lvlActZ,y
lda actT,x ;Store actor type differently if
cmp #ACT_ITEM ;item or NPC
bne RA_StoreNPC
RA_StoreItem: lda actF1,x
ora #$80
sta lvlActT,y
lda actHp,x
bcs RA_StoreCommon ;C=1 here
RA_StoreNPC: sta lvlActT,y
lda actD,x
ora actWpn,x
RA_StoreCommon: sta lvlActWpn,y
; Remove actor and zero HP
;
; Parameters: X actor index
; Returns: A=0
; Modifies: A
RemoveActor: lda #$00
sta actT,x
sta actHp,x
ACD_Skip: rts
; Add (lethal) damage to self
;
; Parameters: A damage amount, X actor index
; Returns: -
; Modifies: A
AddSelfDamage: ldy #DF_LETHAL ;Lethal damage, but no impulse
pha
tya
sta actDmgFlags,x
txa
tay
pla
bpl AddDamage
; Add (bullet) damage to target actor
;
; Parameters: X source actor index, Y target actor index, A damage amount
; Returns: -
; Modifies: A,zpSrcLo-Hi,zpDestLo-Hi,xLo-yHi,loadTempReg
AddBulletDamage:lda actHp,x
AddDamage: sta xLo
stx saveActIndex
lda actDmgFlags,x
sta yLo
stx xHi
jsr GetActorYOffset
sta yHi
tya
tax ;Target actor to X
stx actIndex
jsr GetActorData
ldy #AD_DAMAGEMOD
lda (actLo),y
tay
lda xLo
jsr ModifyDamage ;Apply damage mod
sta xLo
lda actHp,x ;Subtract hitpoints
sec
sbc xLo
bcs AD_NoZeroHp
lda #$00
AD_NoZeroHp: sta actHp,x
lda #DF_HITFLASH
sta actDmgFlags,x
ldy #AD_TAKEDAMAGE+1
jsr ActorCall ;Call damage response for effects, destruction etc.
ldx saveActIndex ;Restore original actorindex
stx actIndex
ABD_ZeroDamage: rts
; Check if two actors have collided.
; Only compares against the first bounding box of actor X, so it would be typically
; an item actor or other that only has one. Should not be called when actor X's
; current sprite doesn't have bounds
;
; Parameters: X,Y actor indices. Actor X must be also in actIndex.
; Returns: C=0 if collided
; Modifies: A,Y
CheckActorCollision:
sty CAC_ActCmp+1
lda actBounds,x ;Actor X not on screen
bmi CAC_NoCollision
tax
lda actBounds,y
bmi CAC_NoCollision ;Actor Y not on screen
tay
bpl CAC_Loop
CAC_Next: iny
CAC_Loop: lda boundsAct,y ;Different actor or endmark? All actor's bounds checked then
CAC_ActCmp: cmp #$00
bne CAC_NoCollision
lda boundsL,y
cmp boundsR,x
bcs CAC_Next
lda boundsL,x
cmp boundsR,y
bcs CAC_Next
lda boundsU,y
cmp boundsD,x
bcs CAC_Next
lda boundsU,x
cmp boundsD,y
bcs CAC_Next
ldx actIndex ;Has collision
rts
SB_Success:
CBC_NoCollision:
CAC_NoCollision:ldx actIndex
CBC_NoCollision2:
sec
rts
; Check bullet's collision against all eligible actors, return first collision
; Should not be called when bullet's current sprite doesn't have bounds
;
; Parameters: X actor number, must also be in actIndex
; Returns: C=0 collision, actor index in Y, C=1 no collision
; Modifies: A,Y,zpBitsLo-Hi
CheckBulletCollision:
lda actBounds,x
bmi CBC_NoCollision2 ;Skip if bullet is outside screen
sta CBC_BulletBoundsIndex+1
tay
CBC_InitDone: ldy #$00
CBC_Loop: ldx boundsAct,y
CBC_Back: cpx #MAX_COMPLEXACT ;Endmark or non-complex actors reached?
bcs CBC_NoCollision
stx zpBitsLo
lda actFlags,x
eor currFlags
and #AF_GROUPFLAGS
beq CBC_NextAct ;Prevent friendly fire (same group)
lda actHp,x ;Eligible to be damaged?
beq CBC_NextAct
CBC_BulletBoundsIndex:
ldx #$00
CBC_BoundsLoop: lda boundsL,y
cmp boundsR,x
bcs CBC_NextBounds
lda boundsL,x
cmp boundsR,y
bcs CBC_NextBounds
lda boundsU,y
cmp boundsD,x
bcs CBC_NextBounds
lda boundsU,x
cmp boundsD,y
bcc CBC_HasCollision
CBC_NextBounds: iny
lda boundsAct,y ;More bounds of same actor?
cmp zpBitsLo
beq CBC_BoundsLoop
tax