-
Notifications
You must be signed in to change notification settings - Fork 0
/
gc.asm
2014 lines (1853 loc) · 52.8 KB
/
gc.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
!cpu w65c02
*=$0801
!byte $0C,$08,$0A,$00,$9E,' ','2','0','6','4',$00,$00,$00
*=$0810
; *******************************************************************
; Includes
; *******************************************************************
!src "x16.inc"
!src "text.inc"
!src "vera.inc"
!src "farbranch.inc"
; *******************************************************************
; Definitions of variables in Zero Page
; *******************************************************************
RANDNUM = $22 ; 2 bytes for RANDOM seed
IRQ_TRIG = RANDNUM+2 ; ZP address to show if IRQ triggered
LEVEL = IRQ_TRIG+1 ; ZP address to store current level
LIVES = LEVEL+1 ; ZP address to store number of lives
NUMGHOSTS = LIVES+1 ; Number of 'normal' ghosts
NUMPGHOSTS = NUMGHOSTS+1 ; Number of polter geists
NUMDGHOSTS = NUMPGHOSTS+1 ; Number of dimensional ghosts
NUMPORTALS = NUMDGHOSTS+1 ; Number of portals
POINTS = NUMPORTALS+1 ; 3 bytes
BTN_UP = POINTS+3
BTN_DN = BTN_UP+1
BTN_LT = BTN_DN+1
BTN_RT = BTN_LT+1
JIFFIES = BTN_RT+1
PLAYER_DELAY = JIFFIES+1
PLAYER_X = PLAYER_DELAY+1
PLAYER_Y = PLAYER_X+1
RANDSEED = PLAYER_Y+1 ; 2 bytes
PLAYER_SPEED = RANDSEED+2
JOY_DELAY = PLAYER_SPEED+1
LAST_DIR = JOY_DELAY+1
NUMWALLS = LAST_DIR+1
NUMSWALLS = NUMWALLS+1
GHOSTS_DELAY = NUMSWALLS+1
GHOSTS_SPEED = GHOSTS_DELAY+1
PGHOSTS_DELAY = GHOSTS_SPEED+1
PGHOSTS_SPEED = PGHOSTS_DELAY+1
DGHOSTS_DELAY = PGHOSTS_SPEED+1
DGHOSTS_SPEED = DGHOSTS_DELAY+1
PORTALS_SPEED = DGHOSTS_SPEED+1
DIR_DOWN = 1
DIR_LEFT = 2
DIR_RIGHT = 3
DIR_UP = 4
jmp main
; *****************************************************************************
; Increment a 16bit value
; *****************************************************************************
; INPUT: .num = the value to be incremented
; *****************************************************************************
!macro INC16 .num {
inc .num
bne .end
inc .num+1
.end:
}
; *****************************************************************************
; Decrement a 16bit value
; *****************************************************************************
; INPUT: .num = the value to be decremented
; USES: .A for testing for underflow
; *****************************************************************************
!macro DEC16 .num {
lda .num
bne .end
dec .num+1
.end:
dec .num
}
; *******************************************************************
; Find a random number between .min and .max
; *******************************************************************
; INPUT: .min & .max indicates the range within to find number
; USES: .A
; *******************************************************************
!macro RAND .min, .max {
- jsr randomize
cmp #(.max-.min)
bcs -
clc
adc #.min
}
; *******************************************************************
; Calculate new coordinates according to the direction stored in .A
; *******************************************************************
; INPUTS: .A = direction
; .xcord and .ycord must contain current coordinates
; OUTPUTS: .xcord and .ycord will contain the new coordinates
; *******************************************************************
!macro NEW_CORD .xcord, .ycord {
cmp #DIR_DOWN
bne .is_left
inc .ycord
bra .endm
.is_left:
cmp #DIR_LEFT
bne .is_right
dec .xcord
bra .endm
.is_right:
cmp #DIR_RIGHT
bne .is_up
inc .xcord
bra .endm
.is_up:
dec .ycord
.endm:
}
; *******************************************************************
; Write .num to screen using VERA, .num must be BCD encoded
; *******************************************************************
; INPUTS: .num = the number to write to screen
; USES: .A
; *******************************************************************
!macro WRITE_BCD_NUM .num {
lda .num
lsr ; Move high nibble to low
lsr
lsr
lsr
ora #$30 ; Convert to PETSCII
sta VERA_DATA0
lda .num
and #$0F ; Remove high nibble
ora #$30 ; Convert to PETSCII
inc VERA_ADDR_LOW ; Move to next char on screen
inc VERA_ADDR_LOW
sta VERA_DATA0
}
; *******************************************************************
; Add the total number of ghosts and write it to screen
; *******************************************************************
; USES: .X, .Y
; RETURNS: .Y = Total number of ghosts (BCD encoded).
; *******************************************************************
!macro SUM_GHOSTS {
sed ; Turn BCD mode on
lda NUMGHOSTS ; Add up all the ghosts
clc
adc NUMPORTALS
adc NUMPGHOSTS
adc NUMDGHOSTS
cld ; Turn BCD mode off
tay ; Store the total number of ghosts
lsr ; Move high nibble to low nibble
lsr
lsr
lsr
ora #$30 ; Convert to petscii code
tax ; Save result in .X
lda #$B0+0 ; Coordinates 35x0
sta VERA_ADDR_HIGH
lda #35*2
sta VERA_ADDR_LOW
stx VERA_DATA0 ; Write petscii digit
inc VERA_ADDR_LOW ; Coordinate 36x0
inc VERA_ADDR_LOW
tya ; Restore total number of ghosts
and #$0F ; Remove top nibble
ora #$30 ; Convert to petscii code
sta VERA_DATA0 ; Write petscii digit
}
; *******************************************************************
; Add points to the total number of points
; *******************************************************************
; INPUTS: .points = the amount of points to add to total
; USES: .A
; *******************************************************************
!macro ADD_POINTS .points {
sed
lda POINTS+2 ; Load low-byte of POINTS
clc ; Ensure Carry is clear
adc #.points ; Add .points
sta POINTS+2 ; Store low-byte of POINTS
bcc .end ; If carry is clear, we can end
lda POINTS+1 ; Load mid-byte of POINTS
adc #0 ; Add zero to actually add carry
sta POINTS+1 ; Store mid-byte of POINTS
bcc .end ; If carry is clear, we can end
lda POINTS ; Load high-byte of POINTS
adc #0 ; Add zero to actually add carry
sta POINTS ; Store high-byte of POINTS
.end:
cld
}
; *****************************************************************************
; Write the players current points to the screen
; *****************************************************************************
; USES: .A
; *****************************************************************************
!macro WRITE_POINTS {
; Move cursor to 8,1. 3rd option tells macro that we are using
; immediate values instead of variables
+VERA_GO_XY 8, $B0+1, 1
+WRITE_BCD_NUM POINTS ; Write high-byte of POINTS to screen
inc VERA_ADDR_LOW
inc VERA_ADDR_LOW
+WRITE_BCD_NUM POINTS+1 ; Write mid-byte of POINTS to screen
inc VERA_ADDR_LOW
inc VERA_ADDR_LOW
+WRITE_BCD_NUM POINTS+2 ; Write low-byte of POINTS to screen
}
; *****************************************************************************
; Compare value in .A with value in .dist. If .A is smaller, update .dist
; and write .curx & .cury values to .newx and .newy variables.
; *****************************************************************************
; INPUTS: .A & .dist = distance values to be compared
; .curx & .cury = X & Y values that may be copied
; .newx & .newy = variables to hold X & Y values
; OUTPUTS: .dist, .newx & .newy (updated if .A < .dist)
; *****************************************************************************
!macro UPD_DIST_XY .dist, .curx, .cury, .newx, .newy {
cmp .dist ; If returned distance is shorter than the
bcs .end ; previous saved, save this dist and coords.
sta .dist
lda .curx
sta .newx
lda .cury
sta .newy
.end:
}
; *****************************************************************************
; Read and combine keyboard, joy1 & joy2 inputs
; *****************************************************************************
; USES: .A, .X, .Y & TMPf
; OUTPUS: .A contains the combined .A from joystick_get for all 3 "joys"
; *****************************************************************************
!macro READ_JOY {
lda #0
jsr JOY_GET
sta TMPf
lda #1
jsr JOY_GET
and TMPf
sta TMPf
lda #2
jsr JOY_GET
and TMPf
}
; *******************************************************************
; Initialize ZP variables with correct values for start
; *******************************************************************
; USES: A
; *******************************************************************
init_vars:
lda #5 ; LIVES = 5
sta LIVES
sta RANDNUM
sta RANDNUM+1
sta PLAYER_DELAY
sta PLAYER_SPEED
lda #10
sta JOY_DELAY
lda #60
sta JIFFIES
ldy #End_game-Portal_delay
lda #0
- sta Portal_delay,y
dey
bne -
stz End_game
stz IRQ_TRIG ; IRQ_TRIG = 0
stz POINTS ; POINTS = 000000 (BCD)
stz POINTS+1
stz POINTS+2
rts
; *****************************************************************************
; 2 bytes of memory to store address of original IRQ handler function
; *****************************************************************************
Old_irq_handler:
!word $0000
; *******************************************************************
; Level definitions
; *******************************************************************
NUM_LEVELS = 20
Levels: ; Level structure
; Level 1
!word $873B ; Random seed
!byte 4 ; Number of static walls
!byte 85 ; Number of walls
!byte 2 ; Number of ghosts
!byte 40 ; Ghost speed (number of jiffies before ghost moves)
!byte 0 ; Number of poltergeists
!byte 0 ; PGhost speed (number of jiffies before pghosts moves)
!byte 0 ; Number of dimensional ghosts
!byte 0 ; DGhost speed (number of jiffies before dghosts moves)
!byte 0 ; Number of portals
!byte 0 ; Portal delay (seconds before portal releases a ghost)
; Level 2
!word $5711 ; Random seed
; SWALLS,WALLS,GHOSTS,SPEED,PGHOSTS,SPEED,DGHOSTS,SPEED,PORTALS,SPEED
!byte 4, 80, 2, 40, 0, 30, 0, 25, 0, 30
; Level 3
!word $28BC ; Random seed
; SWALLS,WALLS,GHOSTS,SPEED,PGHOSTS,SPEED,DGHOSTS,SPEED,PORTALS,SPEED
!byte 4, 80, 3, 40, 0, 30, 0, 25, 0, 30
; Level 4
!word $CC96 ; Random seed
; SWALLS,WALLS,GHOSTS,SPEED,PGHOSTS,SPEED,DGHOSTS,SPEED,PORTALS,SPEED
!byte 4, 80, 4, 40, 0, 30, 0, 25, 0, 30
; Level 5
!word $5BF0 ; Random seed
; SWALLS,WALLS,GHOSTS,SPEED,PGHOSTS,SPEED,DGHOSTS,SPEED,PORTALS,SPEED
!byte 4, 75, 4, 40, 0, 30, 0, 25, 0, 30
; Level 6
!word $FBA0 ; Random seed
; SWALLS,WALLS,GHOSTS,SPEED,PGHOSTS,SPEED,DGHOSTS,SPEED,PORTALS,SPEED
!byte 4, 70, 4, 40, 0, 30, 0, 25, 0, 30
; Level 7
!word $97CF ; Random seed
; SWALLS,WALLS,GHOSTS,SPEED,PGHOSTS,SPEED,DGHOSTS,SPEED,PORTALS,SPEED
!byte 15, 70, 0, 40, 1, 30, 0, 25, 0, 30
; Level 8
!word $B1AD ; Random seed
; SWALLS,WALLS,GHOSTS,SPEED,PGHOSTS,SPEED,DGHOSTS,SPEED,PORTALS,SPEED
!byte 15, 70, 1, 40, 1, 30, 0, 25, 0, 30
; Level 9
!word $C2BD ; Random seed
; SWALLS,WALLS,GHOSTS,SPEED,PGHOSTS,SPEED,DGHOSTS,SPEED,PORTALS,SPEED
!byte 15, 70, 2, 40, 1, 30, 0, 25, 0, 30
; Level 10
!word $E4D3 ; Random seed
; SWALLS,WALLS,GHOSTS,SPEED,PGHOSTS,SPEED,DGHOSTS,SPEED,PORTALS,SPEED
!byte 15, 70, 3, 40, 1, 30, 0, 25, 0, 30
; Level 11
!word $1383 ; Random seed
; SWALLS,WALLS,GHOSTS,SPEED,PGHOSTS,SPEED,DGHOSTS,SPEED,PORTALS,SPEED
!byte 15, 70, 1, 40, 2, 30, 0, 25, 0, 30
; Level 12
!word $C4E6 ; Random seed
; SWALLS,WALLS,GHOSTS,SPEED,PGHOSTS,SPEED,DGHOSTS,SPEED,PORTALS,SPEED
!byte 15, 70, 2, 40, 2, 30, 0, 25, 0, 30
; Level 13
!word $AEF0 ; Random seed
; SWALLS,WALLS,GHOSTS,SPEED,PGHOSTS,SPEED,DGHOSTS,SPEED,PORTALS,SPEED
!byte 15, 70, 3, 40, 2, 30, 0, 25, 0, 30
; Level 14
!word $C46F ; Random seed
; SWALLS,WALLS,GHOSTS,SPEED,PGHOSTS,SPEED,DGHOSTS,SPEED,PORTALS,SPEED
!byte 15, 70, 4, 40, 2, 30, 0, 25, 0, 30
; Level 15
!word $A1B5 ; Random seed
; SWALLS,WALLS,GHOSTS,SPEED,PGHOSTS,SPEED,DGHOSTS,SPEED,PORTALS,SPEED
!byte 15, 65, 4, 40, 2, 30, 0, 25, 0, 30
; Level 16
!word $17A9 ; Random seed
; SWALLS,WALLS,GHOSTS,SPEED,PGHOSTS,SPEED,DGHOSTS,SPEED,PORTALS,SPEED
!byte 15, 65, 4, 40, 3, 30, 0, 25, 0, 30
; Level 17
!word $3966 ; Random seed
; SWALLS,WALLS,GHOSTS,SPEED,PGHOSTS,SPEED,DGHOSTS,SPEED,PORTALS,SPEED
!byte 12, 65, 4, 40, 1, 30, 0, 25, 1, 30
; Level 18
!word $481F ; Random seed
; SWALLS,WALLS,GHOSTS,SPEED,PGHOSTS,SPEED,DGHOSTS,SPEED,PORTALS,SPEED
!byte 12, 65, 4, 40, 2, 30, 0, 25, 2, 30
; Level 19
!word $679E ; Random seed
; SWALLS,WALLS,GHOSTS,SPEED,PGHOSTS,SPEED,DGHOSTS,SPEED,PORTALS,SPEED
!byte 12, 65, 4, 40, 3, 30, 0, 25, 3, 30
; Level 20
!word $1C2A ; Random seed
; SWALLS,WALLS,GHOSTS,SPEED,PGHOSTS,SPEED,DGHOSTS,SPEED,PORTALS,SPEED
!byte 10, 60, 4, 40, 4, 30, 0, 25, 4, 30
; *****************************************************************************
; X and Y coordinates for ghosts
; *****************************************************************************
MAX_GHOSTS = 5
MAX_P_GHOSTS = 5
MAX_D_GHOSTS = 5
MAX_PORTALS = 5
Ghost_X !fill MAX_GHOSTS+1,0
Ghost_Y !fill MAX_GHOSTS+1,0
D_Ghost_X !fill MAX_D_GHOSTS+1,0
D_Ghost_Y !fill MAX_D_GHOSTS+1,0
P_Ghost_X !fill MAX_P_GHOSTS+1,0
P_Ghost_Y !fill MAX_P_GHOSTS+1,0
Portal_X !fill MAX_PORTALS+1,0
Portal_Y !fill MAX_PORTALS+1,0
Portal_delay !fill MAX_PORTALS+1,0
End_game !byte $00
; *******************************************************************
; Starting point of program
; *******************************************************************
main:
jsr init_vars
jsr splash_screen
+VERA_INIT
+SAVE_INT_VECTOR Old_irq_handler
+INSTALL_INT_HANDLER handle_irq
ldy #1
jsr load_level
; Wait for user to start game, use the time to
; to do random numbers
jsr wait_for_start
lda #3 ; Go to 40x30 mode
clc
jsr SCRMOD
jsr draw_border
jsr init_playfield
; This is the main loop, it will check the IRQ_TRIG variable
; each time it is set, it will call the functions to handle the game and
; reset the IRQ_TRIG variable. This means that the functions
; will be called 60 times a second
@game_loop:
wai
lda IRQ_TRIG ; Load IRQ_TRIG
beq @game_loop
; VSYNC IRQ has occurred, handle
+ jsr randomize
jsr do_clock ; Update the clock
jsr do_player ; Move player according to joystick input
jsr do_getjoy ; Read joystick input
jsr do_ghosts ; Move ghosts
lsr IRQ_TRIG ; Reset IRQ_TRIG
lda End_game
beq @game_loop
; Wait for user to press start/return button
jsr wait_for_start
; Remove custom interrupt handler
sei
+RESTORE_INT_VECTOR Old_irq_handler
cli
; Set screen to 80x60
lda #0
clc
jsr SCRMOD
; Empty keyboard buffer
- jsr GETIN
bne -
; Set blue background, white text and clear the screen
lda #(BLUE<<4)+WHITE
sta COLOR_PORT
lda #147
jsr CHROUT
rts
; *****************************************************************************
; Calculate a fields distance to the player. Only fields that a ghost can move
; to actually have their distance calculated.
; *****************************************************************************
; INPUTS: TMP4 & TMP5 = X & Y coordinates of field to do calculations for
; USES: TMP6
; OUTPUT: .A = Distance to player
; *****************************************************************************
calc_dist:
@field_x=TMP4
@field_y=TMP5
+VERA_GO_XY @field_x, @field_y
inc VERA_ADDR_LOW
lda VERA_DATA0 ; Read color of current field
cmp #PLAY_COL ; If it is PLAY_COL, it can be moved to
beq @handle_x
lda #$FF ; Else set Distance to $FF
rts
@handle_x:
lda @field_x ; Subtract current X from players X
sec
sbc PLAYER_X
bpl @handle_y ; If result positive handle Y coordinate
eor #$FF ; Else invert and inc to get positive result
inc
@handle_y:
sta TMP6 ; Save calculation
lda @field_y ; Subract current Y from players Y
sec
sbc PLAYER_Y
bpl @store_dist ; If result positive, store the distance
eor #$FF ; Else invert and inc to get positive result
inc
@store_dist:
clc ; Add saved calculation to current for
adc TMP6 ; complete distance
rts
; *****************************************************************************
; Move ghosts in the direction of the player if there are any open spaces
; *****************************************************************************
; INPUTS: TMP0 & TMP1 = Pointer to ghost X coordinate array
; TMP2 & TMP3 = Pointer to ghost Y coordinate array
; USES: .A, .X, .Y, TMP4-TMP9
; *****************************************************************************
move_ghost:
; Local variable names
@x_arr=TMP0
@y_arr=TMP2
@field_x=TMP4
@field_y=TMP5
@dist=TMP7
@new_x=TMP8
@new_y=TMP9
ldy #0 ; .Y used as index in X,Y arrays
@loop:
stz @new_x ; Reset distance and new coordinates
stz @new_y
lda #$FF
sta @dist
lda (@x_arr),y ; Load current x coord
+FBEQ @end ; If it is zero, we are done
sta @field_x ; Store current x coord in ZP variable
lda (@y_arr),y
sta @field_y ; Store current y coord in ZP variable
dec @field_x ; Top left field
dec @field_y ; X-1, Y-1
jsr calc_dist
+UPD_DIST_XY @dist, @field_x, @field_y, @new_x, @new_y
inc @field_x ; Top middle field
jsr calc_dist ; X, Y-1
+UPD_DIST_XY @dist, @field_x, @field_y, @new_x, @new_y
inc @field_x ; Top right field
jsr calc_dist ; X+1, Y-1
+UPD_DIST_XY @dist, @field_x, @field_y, @new_x, @new_y
inc @field_y ; Right middle field
jsr calc_dist ; X+1, Y
+UPD_DIST_XY @dist, @field_x, @field_y, @new_x, @new_y
inc @field_y ; Bottom right field
jsr calc_dist ; X+1, Y+1
+UPD_DIST_XY @dist, @field_x, @field_y, @new_x, @new_y
dec @field_x ; Bottom middle field
jsr calc_dist ; X, Y+1
+UPD_DIST_XY @dist, @field_x, @field_y, @new_x, @new_y
dec @field_x ; Bottom left field
jsr calc_dist ; X-1, Y+1
+UPD_DIST_XY @dist, @field_x, @field_y, @new_x, @new_y
dec @field_y ; Left middle field
jsr calc_dist ; X-1, Y
+UPD_DIST_XY @dist, @field_x, @field_y, @new_x, @new_y
lda @new_x ; If new_x is 0, no moveable fields were found
bne @ghost_move ; Check next
iny
jmp @loop ; Check next ghost
@ghost_move:
lda (@y_arr),y
sta VERA_ADDR_HIGH
lda (@x_arr),y
asl
sta VERA_ADDR_LOW
ldx VERA_DATA0 ; Save ghost character
lda #' ' ; Write an empty field...
sta VERA_DATA0
inc VERA_ADDR_LOW ; with PLAY_COL color
lda #PLAY_COL
sta VERA_DATA0
lda @new_y ; Save new ghost coordinates
sta (@y_arr),y ; In X and Y array and in VERA
sta VERA_ADDR_HIGH
lda @new_x
sta (@x_arr),y
asl
sta VERA_ADDR_LOW
lda VERA_DATA0 ; Save what we are overwriting
stx VERA_DATA0 ; Write the ghost character
tax
inc VERA_ADDR_LOW
lda #GHOST_COL
sta VERA_DATA0
cpx #PLAYER ; If we overwrote the PLAYER character, the
+FBEQ player_died ; Player has died. (jmp instead of jsr to
; avoid returning to this function)
iny
jmp @loop ; Check next ghost
@end
rts
; *****************************************************************************
; Handle moving of ghosts as well as converting portals to dimensional ghosts
; *****************************************************************************
; *****************************************************************************
do_ghosts:
@x_coord=TMP0
@y_coord=TMP2
lda NUMGHOSTS
beq @pghosts ; If there are no ghosts, check poltergeists
; Move normal ghosts
dec GHOSTS_DELAY ; Only move ghosts after the delay is 0
bne @pghosts
lda GHOSTS_SPEED ; Reset the delay for next move
sta GHOSTS_DELAY
lda #<Ghost_X ; Use zp memory as pointers to ghost X & Y
sta @x_coord
lda #>Ghost_X
sta @x_coord+1
lda #<Ghost_Y
sta @y_coord
lda #>Ghost_Y
sta @y_coord+1
jsr move_ghost
@pghosts:
lda NUMPGHOSTS
beq @dghosts ; If there are no poltergeists, check dimensional
; Move poltergeists
dec PGHOSTS_DELAY
bne @dghosts
lda PGHOSTS_SPEED
sta PGHOSTS_DELAY
lda #<P_Ghost_X
sta @x_coord
lda #>P_Ghost_X
sta @x_coord+1
lda #<P_Ghost_Y
sta @y_coord
lda #>P_Ghost_Y
sta @y_coord+1
jsr move_ghost
@dghosts:
lda NUMDGHOSTS
beq @portals ; If there are no dimensional ghosts, check portals
; move dimensional ghosts
dec DGHOSTS_DELAY
bne @portals
lda DGHOSTS_SPEED
sta DGHOSTS_DELAY
lda #<D_Ghost_X
sta @x_coord
lda #>D_Ghost_X
sta @x_coord+1
lda #<D_Ghost_Y
sta @y_coord
lda #>D_Ghost_Y
sta @y_coord+1
jsr move_ghost
@portals:
lda NUMPORTALS
beq @end ; If there are no portals, just continue
; Convert portal to dimensional ghost after delay
lda JIFFIES ; Only count down once a second
cmp #35
bne @end
ldx #0
- dec Portal_delay,x
bne +
bra convert_portal ; convert and return to caller
+ inx
cpx NUMPORTALS
bne -
@end:
rts
; *****************************************************************************
; Convert a portal to a dimensional ghost
; *****************************************************************************
; INPUTS: .X = index of portal that needs to be converted
; USES: .A & .Y
; *****************************************************************************
convert_portal:
ldy NUMDGHOSTS
lda Portal_X,x
sta TMP0
sta D_Ghost_X,y
asl
sta VERA_ADDR_LOW
lda Portal_Y,x
sta TMP1
sta D_Ghost_Y,y
clc
adc #$B0
sta VERA_ADDR_HIGH
lda #DGHOST
sta VERA_DATA0
inc NUMDGHOSTS
lda #<Portal_X
sta TMP2
lda #>Portal_X
sta TMP3
lda #<Portal_Y
sta TMP4
lda #>Portal_Y
sta TMP5
jmp rem_ghost_coords
; rts
; *****************************************************************************
; Handle that the player has died. Remove a life and restart current level
; *****************************************************************************
; *****************************************************************************
player_died:
dec LIVES
beq +
ldy LEVEL
jsr load_level
jmp init_playfield
+
jsr write_lives
+GOTO_XY 13, 13
ldx #<Game_over1
ldy #>Game_over1
jsr print_str
+GOTO_XY 13, 14
ldx #<Game_over2
ldy #>Game_over2
jsr print_str
+GOTO_XY 13, 15
ldx #<Game_over3
ldy #>Game_over3
jsr print_str
+GOTO_XY 13, 16
ldx #<Game_over4
ldy #>Game_over4
jsr print_str
+GOTO_XY 13, 17
ldx #<Game_over5
ldy #>Game_over5
jsr print_str
lda #$FF
sta End_game
rts
; *****************************************************************************
; Wait for the Start button (or return) to be pressed ensuring to handle the
; different ways the program can be started in the emulator and also ensuring
; that the randomize function is called while waiting.
; *****************************************************************************
; USES: .A, .X & .Y
; *****************************************************************************
wait_for_start:
lda #0 ; Read 1st joystick (or keyboard)
jsr JOY_GET
cmp #$00 ; If 0, program is started in emu from cmdline
beq @emu_run
@wait_for_settle: ; When run normally, the return key is read
jsr randomize ; several times after program is actually
lda #0 ; Started, so wait for the JOY_GET function
jsr JOY_GET ; to settle and return no key presses
cmp #$FF
bne @wait_for_settle
bra @norm_run
@emu_run: ; When run form cmdline, we just wait for the
jsr randomize ; result to be something other than 0
+READ_JOY
cmp #$00
beq @emu_run
@norm_run: ; When we reach this point, we can expect the
jsr randomize ; JOY_GET function to behave as documented and
+READ_JOY ; we can loop until user has pressed start
and #NES_STA ; or return
bne @norm_run
rts
; *******************************************************************
; Call the functions needed to initialize the entire playing field.
; *******************************************************************
; INPUTS: Global Zero Page variables must be set
; USES: .A, .X, Y
; *******************************************************************
init_playfield:
ldx #0 ; If there is 0 in RANDSEED
cpx RANDSEED ; We save RANDNUM in RANDSEED
bne + ; Otherwise we copy RANDSEED to RANDNUM
ldx RANDNUM ; This makes it possible to make levels
stx RANDSEED ; that can be loaded by setting RANDSEED
ldx RANDNUM+1
stx RANDSEED+1
+ ldx RANDSEED
stx RANDNUM
ldx RANDSEED+1
stx RANDNUM+1
jsr zero_ghost_coords
jsr clear_field
jsr place_swalls ; Place static walls
jsr place_walls ; Place walls
jsr place_ghosts ; Place ghosts according to ZP variables
jsr place_player
jsr write_level ; Write the current level to screen
jsr write_lives ; Write the current amount of lives
+SUM_GHOSTS
rts
; *****************************************************************************
; Ensure that all ghost coordinates are zeroed out.
; *****************************************************************************
; USES: .X
; *****************************************************************************
zero_ghost_coords:
ldx #Portal_delay-Ghost_X ; Size of all coordinate arrays
- dex
stz Ghost_X,x
bne -
rts
; *******************************************************************
; Write the current amount of lives
; *******************************************************************
; INPUTS: LIVES
; USES: .A
; *******************************************************************
write_lives:
+VERA_GO_XY 10,$B0+0,1
+WRITE_BCD_NUM LIVES
rts
; *******************************************************************
; Write the current level on screen
; *******************************************************************
; INPUTS: LEVEL
; USES: .A & TMP0
; *******************************************************************
write_level:
+VERA_GO_XY 22,$B0+0,1
ldy LEVEL
lda int_2_bcd,y
sta TMP0
+WRITE_BCD_NUM TMP0
rts
; *******************************************************************
; Load level information into global zeropage variables
; *******************************************************************
; INPUTS: .Y level to load
; USES: .A & .Y
; OUTPUTS: LEVEL, RANDSEED, NUMSWALLS, NUMWALLS,
; NUMGHOSTS, NUMPORTALS, NUMPGHOSTS & NUMDGHOSTS
; *******************************************************************
load_level:
@level_ptr=TMP0
sty LEVEL ; Store level
lda #>Levels ; TMP0 pointer to start of Levels
sta @level_ptr+1
lda #<Levels
sta @level_ptr
@find_level:
dey ; When Y reaches 0, we have found level
beq @do_load
lda @level_ptr
clc ; Add 12 to the level pointer to point to
adc #12 ; next level
sta @level_ptr
lda @level_ptr+1
adc #0
sta @level_ptr+1
jmp @find_level ; Go back and see if we found right level
@do_load:
lda (@level_ptr),y ; Random Seed
sta RANDSEED+0
iny
lda (@level_ptr),y
sta RANDSEED+1
iny
lda (@level_ptr),y ; Number of static walls
sta NUMSWALLS
iny
lda (@level_ptr),y ; Number of walls / 5
sta NUMWALLS
iny
lda (@level_ptr),y ; Number of ghosts
sta NUMGHOSTS
iny
lda (@level_ptr),y ; Ghost delay/speed
sta GHOSTS_DELAY
sta GHOSTS_SPEED
iny
lda (@level_ptr),y ; Number of poltergeists
sta NUMPGHOSTS
iny
lda (@level_ptr),y ; Poltergeist delay/speed
sta PGHOSTS_DELAY
sta PGHOSTS_SPEED
iny
lda (@level_ptr),y ; Number of dimensional ghosts
sta NUMDGHOSTS
iny
lda (@level_ptr),y ; Dimensional ghost delay/speed
sta DGHOSTS_DELAY
sta DGHOSTS_SPEED
iny
lda (@level_ptr),y ; Number of portals
sta NUMPORTALS
iny
lda (@level_ptr),y ; Portal delay
ldy NUMPORTALS ; If > 0, then store portal delay
beq @end
ldy #0 ; 1st portal uses portal delay, following
- sta Portal_delay,y ; portals wait in increments of 2 seconds
inc
inc
iny
cpy NUMPORTALS
bne -
@end:
rts
; *******************************************************************
; Get joystick input from 1st joystick and store in ZP varables
; *******************************************************************
; USES: .A, .Y & TMP0
; *******************************************************************
do_getjoy:
lda #0 ; Select first joystick
; jsr JOY_GET
+READ_JOY
sta TMP0 ; Save current joystick state
cmp #$FF ; If no key is pressed on joystick
bne @joy_start
sta LAST_DIR ; Make sure to reset LAST_DIR and
lda #10 ; JOY_DELAY variables
sta JOY_DELAY
jmp @end
@joy_start:
ldy #1 ; .Y is used to store into direction vars
lda TMP0 ; Restore current joystick state
and #JOY_DN ; Is Down-key preseed?
bne @check_left ; If not check Left-key
lda TMP0 ; Restore jystick state
cmp LAST_DIR
bne @mv_dn ; If it is equal to last time (key is held)
lda JOY_DELAY ; See if JOY_DELAY=0
beq @mv_dn ; If it does, we can move
dec JOY_DELAY ; Otherwise decrement JOY_DELAY =
jmp @end ; wait for 10 jiffies
@mv_dn: sty BTN_DN ; Store 1 in BTN_DN
sta LAST_DIR ; Save current direction
jmp @end