-
Notifications
You must be signed in to change notification settings - Fork 3
/
flappy.asm
1846 lines (1494 loc) · 48.6 KB
/
flappy.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
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
; Copyright (c) 2017-2021, Stephen Illingworth
;
; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions are met:
;
; 1. Redistributions of source code must retain the above copyright notice, this
; list of conditions and the following disclaimer.
; 2. Redistributions in binary form must reproduce the above copyright notice,
; this list of conditions and the following disclaimer in the documentation
; and/or other materials provided with the distribution.
;
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
; DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
; ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
processor 6502
include vcs.h
include macro.h
include vcs_extra.h
include vcs_positioning.h
include vcs_sfx.h
include dasm_extra.h
PLUSROM = FALSE
PAL60 = FALSE
MDL_TYPE_CHECKING = TRUE
MDL_RANGE_CHECKING = TRUE
SHOW_READY_SCREEN = TRUE
RANDOM_TRUNK = FALSE
HARD_DIFFICULTY = FALSE
; ----------------------------------
; PlusROM Hotspots
IF PLUSROM
WriteToBuffer = $1ff0
WriteSendBuffer = $1ff1
ReceiveBuffer = $1ff2
ReceiveBufferSize = $1ff3
HIGHSCORE_ID = 34
ENDIF
; ----------------------------------
; * DATA - COLOURS
; colours
IF PAL60
BIRD_COLOR = $06
FOLIAGE_BACKGROUND = $32
FOLIAGE_COLOR = $30
FOREST_BACKGROUND = $32
FOREST_COLOR = $20
SWAMP_BACKGROUND = $72
SWAMP_COLOR = $70
SCORING_BACKGROUND = $00
SCORE_COLOR = $0E
HISCORE_COLOR = $26
OKAY_COLOR = $2E
ELSE
BIRD_COLOR = $06
FOLIAGE_BACKGROUND = $D2
FOLIAGE_COLOR = $D0
FOREST_BACKGROUND = $D2
FOREST_COLOR = $E0
SWAMP_BACKGROUND = $B2
SWAMP_COLOR = $B0
SCORING_BACKGROUND = $00
SCORE_COLOR = $0E
HISCORE_COLOR = $F6
OKAY_COLOR = $2E
ENDIF
; ----------------------------------
; * DATA - CONSTANTS
; values for CTRLPF
CTRLPF_FOLIAGE = %00100100
CTRLPF_PLAYAREA = %00100000
CTRLPF_SWAMP = %00100100
; death rates
DEATH_COLLISION_SPEED = $02 ; speed at which player sprite moves foward during death collision
DEATH_DROWNING_LEN = $0D ; should be the same BIRD_LOW + 1
; NUSIZ0 and NUSIZ1 values - first four bits set obstacle width
; - last four bits set the player sprite size and duplication number
; - the two nibbles will be OR'd to create the NUSIZ value
;
; size/width of obstacles and player sprites changes frequently throughout the game.
;
; summary of changes
; ==================
; o during game initialisation
; o during score routine
; o during overscan (depending on the play state)
; o during game play area, to simulate a solitary branch (NUSIZ1) only
;
OBSTACLE_WIDTH = %00100000 ; quad width
BRANCH_WIDTH = %00110000 ; octuple width
BALL_WIDTH = 001100000 ; octuple width
SCORE_DIGITS_SIZE = %00000101 ; single instance, double width
; bird sprite "coords"
BIRD_VPOS_INIT = BIRD_HIGH / 4 * 3
BIRD_HPOS_PLAY_POS = $0C
IF HARD_DIFFICULTY == TRUE
WING_WIDTH = %00000101 ; single instance, double width
HEAD_OFFSET_FROM_WINGS = $07
HEAD_WIDTH = %00000000 ; single instance, single width
BIRD_HPOS_INIT = $00
ELSE
WING_WIDTH = %00000000 ; single instance, single width
HEAD_OFFSET_FROM_WINGS = $02
HEAD_WIDTH = %00000000 ; single instance, single width
BIRD_HPOS_INIT = $05
ENDIF
; ok sign width
OK_SIGN_WIDTH = %00000101 ; single instance, double width
QMARK_SIGN_WIDTH = %00000000 ; single instance, single width
; play state -- death states are all negative
PLAY_STATE_PLAY = $00
PLAY_STATE_APPROACH = $01
PLAY_STATE_READY = $02
PLAY_STATE_COLLISION = $FF
PLAY_STATE_DROWN = $FE
; visible scan line usage
; =======================
; note the extra $04 scanlines in the score area are:
; * one at the start of the subroutine
; * two position resets
; * and another one because DIGIT_LINES breaks on -1 not 0 (BMI instead of BEQ)
;
; note that we don't loop the swamp WSYNCs because there are so few and we'll
; be doing something useful and different on each line
; care should be taken to update this value, if we increase the number of WSYNCs
SCANLINES_FOLIAGE = $20
SCANLINES_PLAYAREA = DISPLAY_SCANLINES - SCANLINES_FOLIAGE - SCANLINES_SWAMP - SCANLINES_SCOREAREA
SCANLINES_SWAMP = $04
SCANLINES_SCOREAREA = DIGIT_LINES + $04
SCANLINES_PER_FOLIAGE = SCANLINES_FOLIAGE / 8
; point at which to change the sprite color - to enable effective swamp colouring
; the value should be odd because it is checked for on the odd scanlines
SPLASH_LINE = $09
; screen boundaries for bird sprite
BIRD_HIGH = SCANLINES_PLAYAREA
BIRD_LOW = SCANLINES_SWAMP + SCANLINES_SCOREAREA
; DASM directives to output number of scanlines used
DASM_MESSAGE "Scanline Layout"
DASM_MESSAGE "---------------"
DASM_MESSAGE "FOLIAGE = ", SCANLINES_FOLIAGE
DASM_MESSAGE "PLAYAREA = ", SCANLINES_PLAYAREA
DASM_MESSAGE "SWAMP = ", SCANLINES_SWAMP
DASM_MESSAGE "SCORE AREA = ", SCANLINES_SCOREAREA
DASM_MESSAGE "TOTAL = ", SCANLINES_FOLIAGE + SCANLINES_PLAYAREA + SCANLINES_SWAMP + SCANLINES_SCOREAREA, "(", DISPLAY_SCANLINES, ")"
; ----------------------------------
; * DATA - RAM
SEG.U RAM
ORG $80 ; start of 2600 RAM
; VCS_EXTRA.H SCOPE
; - variables beginning with __ are required by routines in vcs_*.h
__MULTI_COUNT_STATE ds 1
__STATE_INPT4 ds 1
__STATE_SWCHB ds 1
; VCS_AUDIO.H SCOPE
__SFX_NEW_EVENT ds 1
__SFX_QUEUE_EVENT ds 1
__SFX_SUB_FRAMES ds 1
; LOCAL SCOPE
; - can be resused between subroutines
; - don't access these locations accept through aliases defined
; in the subroutine that uses them
_localA ds 1
_localB ds 1
_localC ds 1
_localD ds 1
_localE ds 1
_localF ds 1
_localG ds 1
; GLOBAL SCOPE
; - data that persists for a long time, say, frame to frame
PLAY_STATE ds 1 ; state of play - zero is normal, negative is death
SELECTED_HEAD ds 1 ; index into HEADS_TABLE
; selected flight pattern - see FLIGHT_PATTERN macros
FLIGHT_PATTERN ds 2
; which bird/detail sprite to use in the display kernel
ADDRESS_SPRITE_0 ds 2
ADDRESS_SPRITE_1 ds 2
BIRD_VPOS ds 1 ; between BIRD_HIGH and BIRD_LOW
BIRD_HPOS ds 1 ; current horizontal position of bird (in pixels)
BIRD_HEAD_OFFSET ds 1 ; number of pixels the head is offset from BIRD_HPOS
; PATTERN_INDEX's meaning changes depending on PLAY_STATE
;
; if PLAY_STATE == PLAY_STATE_PLAY || PLAY_STATE_APPROACH || PLAY_STATE_COLLISION
; then
; PATTERN_INDEX indexes FLIGHT_PATTERN
;
; if PLAY_STATE == PLAY_STATE_DROWN
; then
; PATTERN_INDEX counts number of frames to game reset
;
PATTERN_INDEX ds 1
; seed values for /next/ foliage, obstacle and branch
FOLIAGE_SEED ds 1
OBSTACLE_SEED ds 1
BRANCH_SEED ds 1
; obstacle defintions
OB_0 ds 2
OB_1 ds 2
OB_0_BRANCH ds 1
OB_1_BRANCH ds 1
OB_0_HPOS ds 1
OB_1_HPOS ds 1
OB_0_SPEED ds 1
OB_1_SPEED ds 1
; current state of background trees (playfield data)
FOREST_MID_0 ds 1
FOREST_MID_1 ds 1
FOREST_MID_2 ds 1
; colour of player/missile 0 below the splash line
; o BIRD_COLOR in most instances
; o SWAMP_COLOR during PLAY_STATE_DROWN
SPLASH_COLOR ds 1
; player score
SCORE ds 1
HISCORE ds 1
; DASM directive - echo number of bytes left in RAM
DASM_MESSAGE "",($100 - *) , "bytes of RAM left"
; ----------------------------------
; * DATA - GFX
SEG
ORG $F000 ; start of cart ROM
DATA_SEGMENT
; graphics data
; =============
; number of lines in each sprite - same for everything, except digits
SPRITE_LINES = 7
; number of line each digit
DIGIT_LINES = 4
EMPTY HEX 00 00 00 00 00 00 00 00
; ready state text
TEXT_OK HEX 00 EA AA AA AC EA 00 00
TEXT_QMARK HEX 00 18 00 1E 42 7E 00 00
; bird sprite - heads
WINGS
WINGS_UP HEX 00 00 00 00 30 70 60 40
WINGS_FLAT HEX 00 00 00 00 30 60 00 00
WINGS_DOWN HEX 00 40 60 70 30 00 00 00
; bird sprite - wings
PAGE_CHECK
HEADS
HEAD_GIRL_A HEX 00 00 00 00 20 1C 18 20
HEAD_BOY_A HEX 00 00 00 00 20 1C 18 00
HEAD_GIRL_B HEX 00 00 00 00 20 1C 58 20
HEAD_BOY_B HEX 00 00 00 00 20 1C 18 04
PAGE_CHECK_END "HEADS"
NUM_HEADS = 4
HEADS_TABLE .byte <HEAD_GIRL_A, <HEAD_BOY_A, <HEAD_GIRL_B, <HEAD_BOY_B
; splash sprite
_SPLASH HEX 18 18
SPLASH HEX 00 00 24 42 24 00 18 00
; digits - used for scoring
PAGE_CHECK
DIGITS
DIGIT_0 HEX 3C 24 24 24 3C
DIGIT_1 HEX 08 08 08 18 08
DIGIT_2 HEX 3C 20 18 04 3C
DIGIT_3 HEX 3C 04 08 04 3C
DIGIT_4 HEX 08 3C 28 20 20
DIGIT_5 HEX 38 04 3C 20 3C
DIGIT_6 HEX 3C 24 3C 20 20
DIGIT_7 HEX 04 04 0C 04 3C
DIGIT_8 HEX 3C 24 3C 24 3C
DIGIT_9 HEX 04 04 3C 24 3C
PAGE_CHECK_END "DIGITS"
DIGIT_TABLE .byte <DIGIT_0, <DIGIT_1, <DIGIT_2, <DIGIT_3, <DIGIT_4, <DIGIT_5, <DIGIT_6, <DIGIT_7, <DIGIT_8, <DIGIT_9
; foliage - playfield data
; (see "foliage" subroutine for full and laboured explanation)
FOLIAGE .byte %01100000, %10011010, %00111010, %10010000, %00110101, %11010001, %01010000, %01010110, %00110011, %10101000, %10100110, %00010110, %10010110, %10011010, %00111010, %00101011, %01101101, %01100110, %01011001, %11001101, %00101010, %01101100, %00100010, %10011010, %00111010, %00110011, %01101101, %01100110, %01011001, %10110101
MAX_FOLIAGE_SEED = 7
; background forest - initial values
FOREST_MID_0_INIT .byte %00100000
FOREST_MID_1_INIT .byte %00011000
FOREST_MID_2_INIT .byte %00001000
FOREST_STATIC_0 .byte %10000000
FOREST_STATIC_1 .byte %00100000
FOREST_STATIC_2 .byte %10010000
; ----------------------------------
; * DATA - OBSTACLES
; obstacles tables
; ===============
;
; * obstacle enable precalc table based on play ara of 153 lines
; - may need tuning if SCANLINES_PLAYAREA changes
; * works as a barrel shifter
; - 153 lines before AND after window
; - there's room for optimisation
; * the window (the zeroes in the table) is 32 bytes(lines)
; make sure we start on a page boundary - we don't want to cross a page boundary
ORG $F100
SET_OBSTACLE_TABLE
. HEX 02 02 02 02
. HEX 02 02 02 02 02 02 02 02 02 02
. HEX 02 02 02 02 02 02 02 02 02 02
. HEX 02 02 02 02 02 02 02 02 02 02
. HEX 02 02 02 02 02 02 02 02 02 02
. HEX 02 02 02 02 02 02 02 02 02 02
. HEX 02 02 02 02 02 02 02 02 02 02
. HEX 02 02 02 02 02 02 02 02 02 02
. HEX 02 02 02 02 02 02 02 02 02 02
. HEX 02 02 02 02 02 02 02 02 02 02
. HEX 02 02 02 02 02 02 02 02 02 02
. HEX 02 02 02 02 02 02 02 02 02 02
; 32 scanlines (pixels) of 00 (this is the size of the gap)
. HEX 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
. HEX 02 02 02 02
. HEX 02 02 02 02 02 02 02 02 02 02
. HEX 02 02 02 02 02 02 02 02 02 02
. HEX 02 02 02 02 02 02 02 02 02 02
. HEX 02 02 02 02 02 02 02 02 02 02
. HEX 02 02 02 02 02 02 02 02 02 02
. HEX 02 02 02 02 02 02 02 02 02 02
. HEX 02 02 02 02 02 02 02 02 02 02
. HEX 02 02 02 02 02 02 02 02 02 02
. HEX 02 02 02 02 02 02 02 02 02 02
. HEX 02 02 02 02 02 02 02 02 02 02
. HEX 02 02 02 02 02 02 02 02 02 02
; list of obstacles to use
OBSTACLES HEX 05 15 25 35 45 55 65
OBSTACLES_LEN = 7
; list of branches
; note that we test for branch on the even scanlines of the play area so these values should be even
BRANCHES HEX 18 24 30 38 44 58 70
BRANCHES_LEN = 7
; ----------------------------------
; * DATA - FLIGHT PATTERNS
; flight patterns
; ===============
; first byte is the length of the flight pattern proper (not including last byte or the first
; byte in the array).
;
; last byte is the index to initialse the pattern index to (usually start of the glide period)
;
; intervening bytes are the flight pattern proper
; note: negative pattern values less than -BIRD_LOW (ie. -12) may result in undefined behaviour
; as this may cause the bird to descend lower than the screen (ie. a negative value)
;
; see FLIGHT_PATTERN macros
FLIGHT_PATTERNS
EASY_FLIGHT_PATTERN .byte 20, 4, 4, 4, 4, 4, 0, 0, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, 6
DEF_FINE_POS_TABLE
; ----------------------------------
; * DATA - SFX
PAGE_CHECK
SFX_TABLE HEX FF 00 00 00 00 00
SFX_FLAP HEX FF 01 F8 03 0F 79
SFX_COLLISION HEX 00 06 87 31 01 49
. HEX 00 06 06 30 00 07
. HEX FF 06 06 31 00 04
SFX_SPLASH HEX 00 04 08 04 00 09
. HEX 00 04 08 03 00 09
. HEX 00 05 08 05 00 09
. HEX 00 06 08 06 00 06
. HEX 00 06 08 08 00 03
. HEX 00 06 08 09 02 01
. HEX FF 02 06 09 03 01
PAGE_CHECK_END "SFX_TABLE"
; ----------------------------------
; * MACROS - FLIGHT PATTERN
MAC APPLY_FLIGHT_PATTERN
; < S = PATTERN_INDEX
; ! AYCZVN
LDY PATTERN_INDEX
LDA (FLIGHT_PATTERN),Y
CLC
ADC BIRD_VPOS
ENDM
MAC RESET_FLIGHT_PATTERN
; > {death spiral} [boolean
; ! AYCZVN
; + PATTERN_INDEX
; initialise PATTERN_INDEX for selected FLIGHT_PATTERN
; (see FLIGHT_PATTERN definitions for memory layout explanation)
; first byte in (FLIGHT_PATTERN) is the pattern's length
LDY #$0
LDA (FLIGHT_PATTERN),Y
; last byte of (FLIGHT_PATTERN) is the pattern's index initialisation value
TAY
INY
LDA (FLIGHT_PATTERN),Y
IF {1} == TRUE
CMP PATTERN_INDEX
; if current PATTERN_INDEX is less than the initialisation value, then don't initialise the index
BCS .no_store_index
ENDIF
STA PATTERN_INDEX
.no_store_index
ENDM
MAC UPDATE_FLIGHT_PATTERN
; > {cycle pattern} [B}
; > Y = PATTERN_INDEX
; ! AYCZVN
; + PATTERN_INDEX
INY
TYA
LDX #$0 ; X = 0 -> length byte of FLIGHT_PATTERN
CMP (FLIGHT_PATTERN,X)
BCC .store_index
IF {1} == TRUE
LDA #$01
ELSE
DEY
TYA
ENDIF
.store_index
STA PATTERN_INDEX
ENDM
; ----------------------------------
; * MACROS - OTHER
MAC POSITION_BIRD_SPRITE
FINE_POS RESP0, BIRD_HPOS
LDA BIRD_HPOS
CLC
ADC BIRD_HEAD_OFFSET
FINE_POS_A RESP1
ENDM
MAC NEW_OBSTACLE
; > {obstacle number} [0,1]
; ! AXCZVN
; + OB_0, OB_1
IF {1} != 0 && {1} != 1
DASM_MACRO_ERROR "'NEW_OBSTACLE': {1} must be 0 or 1"
ENDIF
LDX OBSTACLE_SEED
LDA #<SET_OBSTACLE_TABLE
CLC
ADC OBSTACLES,X
IF {1} == 0
STA OB_0
ENDIF
IF {1} == 1
STA OB_1
LDX BRANCH_SEED
LDA BRANCHES,X
STA OB_1_BRANCH
ENDIF
ENDM
MAC DROWNING_PLAY_STATE
; ! ACZVN
; + BIRD_VPOS, PATTERN_INDEX, PLAY_STATE
; put game into drowning state
; o make sure bird is at the lowest position
; o use pattern index to measure length of drowning animation
; o point ADDRESS_SPRITE_0 to SPLASH
; o alter BIRD_HEAD_OFFSET and BIRD_HPOS
; o SPLASH_COLOR = SWAMP_COLOR
LDA #BIRD_LOW
STA BIRD_VPOS
; --
LDA #DEATH_DROWNING_LEN
STA PATTERN_INDEX
; --
LDA #<SPLASH
STA ADDRESS_SPRITE_0
; --
LDA #PLAY_STATE_DROWN
STA PLAY_STATE
IF PLUSROM
JSR SendPlusROMScore
ENDIF
; --
DEC BIRD_HEAD_OFFSET
DEC BIRD_HEAD_OFFSET
INC BIRD_HPOS
INC BIRD_HPOS
; --
LDA #SWAMP_COLOR
STA SPLASH_COLOR
; obstacle flicker phase correction done in the overscan, otherwise
; there will be a frame where the wrong obstacle definitions are used
SFX_LOAD SFX_SPLASH
ENDM
MAC FOLIAGE_ANIMATION
; > {animate forest background} [B]
; ! YCZVN
; + FOREST_MID_0, FOREST_MID_1, FOREST_MID_2, FOLIAGE_SEED
; cycle playfield data used to illustrate foliage, and by
; association, the playfield used for the water/swamp
LDY FOLIAGE_SEED
INY
CPY #MAX_FOLIAGE_SEED
BCC .foliage_updated
LDY #0
; rotate forest whenever foliage chaos cycle resets
IF {1} == TRUE
.rotate_forest
CLC
ROR FOREST_MID_0
LDA FOREST_MID_0
AND #%00001000
BNE .jump_tree
JMP .cont_forest
.jump_tree
LDA #%11110000
AND FOREST_MID_0
STA FOREST_MID_0
SEC
.cont_forest
ROR FOREST_MID_2
ROL FOREST_MID_1
BCS .carry_tree
JMP .forest_done
.carry_tree
LDA #%10000000
ORA FOREST_MID_0
STA FOREST_MID_0
.forest_done
ENDIF
.foliage_updated
STY FOLIAGE_SEED
ENDM
; ----------------------------------
; SETUP
setup SUBROUTINE setup
CLEAN_START
JMP game_state_init
; ----------------------------------
; TITLE SCREEN
; TODO: finish title screen
title_screen SUBROUTINE title_screen
.vsync
VSYNC_KERNEL_BASIC
.vblank
VBLANK_KERNEL_SETUP
NEW_TRIGGER_CHECK 0
BPL .end_title_screen
LDX #DISPLAY_SCANLINES
VBLANK_KERNEL_END
.visible_loop
STA WSYNC
DEX
BNE .visible_loop
.overscan
OVERSCAN_KERNEL_BASIC
JMP .vsync
.end_title_screen
VBLANK_KERNEL_END
OVERSCAN_KERNEL_BASIC
; ----------------------------------
; GAME - INITIALISATION
game_state_init SUBROUTINE game_state_init
; initialise most significant byte of indirect addresses
LDA #>DATA_SEGMENT
STA ADDRESS_SPRITE_0+1
STA ADDRESS_SPRITE_1+1
LDA #>FLIGHT_PATTERNS
STA FLIGHT_PATTERN+1
LDA #>SET_OBSTACLE_TABLE
STA OB_0+1
STA OB_1+1
; initialise head
LDY #$0
STY SELECTED_HEAD
LDA HEADS_TABLE,Y
STA ADDRESS_SPRITE_1
; initialise obstacles
LDA #$2
STA OBSTACLE_SEED
STA BRANCH_SEED
NEW_OBSTACLE 0
INC OBSTACLE_SEED
NEW_OBSTACLE 1
; initalise background trees
LDA FOREST_MID_0_INIT
STA FOREST_MID_0
LDA FOREST_MID_1_INIT
STA FOREST_MID_1
LDA FOREST_MID_2_INIT
STA FOREST_MID_2
; initialise NUSIZ values
LDA #(OBSTACLE_WIDTH | WING_WIDTH)
STA NUSIZ0
LDA #(OBSTACLE_WIDTH | HEAD_WIDTH)
STA NUSIZ1
JSR game_restart
JMP game_overscan
; ----------------------------------
; GAME - RESTART
; registers that are reset in game_restart subroutine are altered during gameplay
; and need to be reset on game restart
game_restart SUBROUTINE game_restart
; clear any active collisions
STA CXCLR
NEW_TRIGGER_CHECK 0
MULTI_COUNT_SETUP
; save hiscore
LDA SCORE
SED
CMP HISCORE
BCC .done_hiscore
STA HISCORE
.done_hiscore
CLD
; position elements
; make sure movement registers are zero
STA HMCLR
; position both obstacles inside the activision-border
FINE_POS_LEFT RESM0, OB_0_HPOS, 0, 4
FINE_POS_LEFT RESM1, OB_1_HPOS, 0, 8
; obstacle 0 starts moving immediately - obstacle 1 will begin moving later
LDA #1
STA OB_0_SPEED
LDA #0
STA OB_1_SPEED
; reset OB_0_BRANCH to zero - it should always be zero except when it is being
; used to swap the contents of OB_1_BRANCH during PLAY_STATE_DROWN.
; we're forcing the resetting of it's value here becaus PLAY_STATE_DROWN may have
; occurred out of phase and left the real value of OB_1_BRANCH in OB_0_BRANCH.
; the consequence of this is that OB_1_BRANCH may lose its real value for the
; next play ; but it doesn't really matter
STA OB_0_BRANCH
; put game into ready state
; o reset score
; o initialise vertical and horizontal position
; o use flat wings sprite at start
; o reset BIRD_HEAD_OFFSET
; o SPLASH_COLOR = BIRD_COLOUR
LDA #PLAY_STATE_READY
STA PLAY_STATE
; --
LDA #$0
STA SCORE
; --
LDA #BIRD_VPOS_INIT
STA BIRD_VPOS
LDA #BIRD_HPOS_INIT
STA BIRD_HPOS
; --
LDA #<WINGS_FLAT
STA ADDRESS_SPRITE_0
; --
LDA #HEAD_OFFSET_FROM_WINGS
STA BIRD_HEAD_OFFSET
; --
LDA #BIRD_COLOR
STA SPLASH_COLOR
SFX_ENGINE_INIT
RTS
; ----------------------------------
; GAME - VSYNC / VBLANK
game_vsync SUBROUTINE game_vsync
VSYNC_KERNEL_BASIC
game_vblank SUBROUTINE game_vblank
VBLANK_KERNEL_SETUP
LDX PLAY_STATE
BEQ .far_jmp_play
CPX #PLAY_STATE_APPROACH
BEQ .far_jmp_approach
CPX #PLAY_STATE_COLLISION
BEQ .far_jmp_collision
CPX #PLAY_STATE_DROWN
BEQ .far_jmp_drown
JMP game_vblank_ready
.far_jmp_collision
JMP game_vblank_death_collision
.far_jmp_drown
JMP game_vblank_death_drown
.far_jmp_approach
JMP game_vblank_approach
.far_jmp_play
JMP game_vblank_main_triage
; ----------------------------------
; GAME - VBLANK - READY STATE
game_vblank_ready SUBROUTINE game_vblank_ready
IF SHOW_READY_SCREEN == TRUE
; check for user input
NEW_TRIGGER_CHECK 0
BMI .ready_state_triage
ENDIF
; put into approach state
; o set correct flight pattern
; o change player sprites to point to wings/head data
; o put bird sprite at correct vertical positions
; o jump to vblank approach
LDA #PLAY_STATE_APPROACH
STA PLAY_STATE
; TODO: use flight pattern depending on switch
LDA #<EASY_FLIGHT_PATTERN
STA FLIGHT_PATTERN
RESET_FLIGHT_PATTERN FALSE
LDA #<WINGS_FLAT
STA ADDRESS_SPRITE_0
LDY SELECTED_HEAD
LDA HEADS_TABLE,Y
STA ADDRESS_SPRITE_1
LDA #BIRD_VPOS_INIT
STA BIRD_VPOS
JMP game_vblank_approach
; update every three frames
; - same sequence as main play state
.ready_state_triage
MULTI_COUNT_THREE_CMP 1
BEQ .prepare_display
BPL .update_foliage
JMP .prepare_display
.update_foliage
FOLIAGE_ANIMATION TRUE
.prepare_display
MULTI_COUNT_TWO_CMP
BEQ .display_ready_logo
.display_empty
LDA #<EMPTY
STA ADDRESS_SPRITE_0
LDA #<EMPTY
STA ADDRESS_SPRITE_1
JMP game_vblank_end
.display_ready_logo
LDA #90
STA BIRD_VPOS
LDA #OKAY_COLOR
STA COLUP0
STA COLUP1
LDA #<TEXT_OK
STA ADDRESS_SPRITE_0
LDA #<TEXT_QMARK
STA ADDRESS_SPRITE_1
FINE_POS RESP0, #72
FINE_POS RESP1, #88
JMP game_vblank_end
; ----------------------------------
; GAME - VBLANK - DEATH - COLLISION
game_vblank_death_collision SUBROUTINE game_vblank_death_collision
; update display elements every three frames
; note that we do all updating on the same frame, unlike during PLAY_STATE_PLAY
; where we update different elements (sprite, foliage) on different frames
MULTI_COUNT_THREE_CMP 0
BEQ .update_bird
BPL .update_foliage
JMP .prepare_display
.update_foliage
FOLIAGE_ANIMATION FALSE
JMP .prepare_display
.update_bird
; alter wing position - simulates furious flapping
LDA ADDRESS_SPRITE_0
CMP #<WINGS_UP
BEQ .use_wings_flat
CMP #<WINGS_FLAT
BEQ .use_wings_down
.use_wings_up
LDA #<WINGS_UP
STA ADDRESS_SPRITE_0
JMP .wings_updated
.use_wings_flat
LDA #<WINGS_FLAT
STA ADDRESS_SPRITE_0
JMP .wings_updated
.use_wings_down
LDA #<WINGS_DOWN
STA ADDRESS_SPRITE_0
.wings_updated
; apply flight pattern and check for drowning state
APPLY_FLIGHT_PATTERN
CMP #BIRD_LOW
BCC .enter_drowning_state
; limit bird height
CMP #BIRD_HIGH
BCC .update_pattern_idx
LDA #BIRD_HIGH
.update_pattern_idx
STA BIRD_VPOS
UPDATE_FLIGHT_PATTERN FALSE
; propel bird forward - after drowning check
LDA BIRD_HPOS
CLC
ADC #DEATH_COLLISION_SPEED
STA BIRD_HPOS
JMP .prepare_display
.enter_drowning_state
DROWNING_PLAY_STATE
.prepare_display
POSITION_BIRD_SPRITE
JMP game_vblank_end
; ----------------------------------
; GAME - VBLANK - DEATH - DROWN
game_vblank_death_drown SUBROUTINE game_vblank_death_drown
; slow down death animation
MULTI_COUNT_THREE_CMP 0
BEQ .update_bird
BPL .update_foliage
JMP .prepare_display
.update_foliage
FOLIAGE_ANIMATION FALSE
JMP .prepare_display
.update_bird
; end drowning after PATTERN_INDEX (initialised to DEATH_DROWNING_LEN)
DEC PATTERN_INDEX
BEQ .drowning_end
; decrease bird sprite position
DEC BIRD_VPOS
DEC ADDRESS_SPRITE_0
JMP .prepare_display
.drowning_end
JSR game_restart
JMP game_vblank_ready
.prepare_display
POSITION_BIRD_SPRITE
; flicker obstacle 0 and 1 positions and display both using only obstacle 1
; o hiding obstacle 0 in the activision border
; o we do this because we don't want the setting of COLUP0 to SPLASH_COLOR to be visible
FINE_POS_LEFT RESM0, NULL, 0, 4
MULTI_COUNT_TWO_CMP
BEQ .show_obstacle_1
FINE_POS RESM1, OB_0_HPOS
JMP .flipped_obstacles
.show_obstacle_1
FINE_POS RESM1, OB_1_HPOS
.flipped_obstacles
SWAP OB_0, OB_1