-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.asm
1667 lines (1399 loc) · 29.2 KB
/
game.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
; game related code
; inits game mode
; inputs:
; level_data_ptr -> pointing to compressed level
; attr_ptr -> pointing to attributes
; palette_ptr -> pointing to palette
; enables sprite updating
init_game:
lda #$00
sta gfx_flags
; clear all character flags
; unrolled loop
.mrep CHAR_FLAGS
sta character_flags + .ri.
.endrep
lda #ACTIONS_PER_TURN
sta base_actions
sta actions
lda #$FF
sta last_player_damage
sta last_player_armor
sta last_keys
sta last_coins
sta last_player_exp
sta last_magic
lda #START_MAGIC
sta pmagic_base
sta pmagic
; tile update mode
lda #%01000000
sta editor_flags
inc level
; re-enable input just in case player died
; during sword animation
lda nmi_flags
and #%11011111 ; enables input
sta nmi_flags
lda #GAME_MODE_GAME
sta game_mode
lda #MAX_HP ; hp
sta player_hp
lda #START_DAMAGE
sta player_damage
lda #START_WEAPON
sta weapon_type
lda #START_ARMOR ; base armor
sta player_armor_base
sta player_armor
lda #$00
sta map_flags ; reset all map flags
sta key_count ; no keys when map begins
sta player_timer ; no timer for player
sta iframes ; no iframes
sta move_timer ; reset move timer
sta coins
sta player_exp
sta spell_type
sta player_level
sta player_flags
lda #<update_game
sta update_sub
lda #>update_game
sta update_sub+1
lda #<update_game_crit
sta update_sub_crit
lda #>update_game_crit
sta update_sub_crit+1
; copy palette
lda #<level_palette
sta palette_ptr
lda #>level_palette
sta palette_ptr+1
jsr load_palette
jsr hide_objs
jsr init_projectile_slots ; clear projectiles
lda game_flags
ora #%01000000
sta game_flags ; enable sprite updating, disable tile updating
; enable sprite rendering in case it was off
lda mask_flags
ora #%00010000 ; sprites
sta mask_flags
jsr init_ai_tiles
jsr find_start
; check if start was found
cmp #$01
beq @no_error
; error state
lda #ERROR_NO_START_TILE
sta errno
@no_error:
lda player_x
sta player_x_bac
lda player_y
sta player_y_bac
; player sprite
lda #$32
sta sprite_data+1
; move other sprites offscreen
lda #$00
sta sprite_data_1
sta sprite_data_1+3
sta sprite_data_2
sta sprite_data_2+3
; move hp sprites to correct
; location and init them
lda #2 * 8 ; x positions
sta sprite_data_8+3
sta sprite_data_C+3 ; sword
lda #3 * 8
sta sprite_data_9+3
lda #4 * 8
sta sprite_data_A+3
lda #24 * 8 ; y position
sta sprite_data_8
sta sprite_data_9
sta sprite_data_A
lda #$02 ; palette
sta sprite_data_8+2
sta sprite_data_9+2
sta sprite_data_A+2
lda #$5 * 8
sta sprite_data_D+3 ; armor
lda #$8 * 8
sta sprite_data_B+3 ; key
lda #$5 * 8
sta sprite_data_E+3 ; coin
lda #$3F
sta sprite_data_E+1 ; coin sprite
; move other UI sprites onscreen
lda #26 * 8 ; y position
sta sprite_data_B ; key
sta sprite_data_C ; sword
sta sprite_data_D ; armor
lda #25 * 8
sta sprite_data_E ; coin
lda #$3C ; armor spirte
sta sprite_data_D+1
; jsr init_test_song
; reload game state if flag is set
lda load_flags
and #%00100000
beq @no_load
jsr load_save
@no_load:
rts
; this routine is called every frame
; it updates the game state
; critical game update
update_game_crit:
jsr render_tile_updates
jsr update_ui
; test if actions are to become 0
; this flag may be used by some routines that
; update during non-critical code, but want to waste a turn
; like a trap
lda map_flags
and #%00010000
beq @not_actions_zero
lda map_flags
and #%11101111 ; unset flag
sta map_flags
lda #$00
sta actions
@not_actions_zero:
jsr check_player_move
cmp #$01
bne @player_not_moved
; test collision
; jsr collision_check
; load collision result from previous frame
lda game_flags
and #%00000001
; if a = 1 collision occured
cmp #$01
beq @player_not_moved
; if player did move set remaining actions
; to 0. a move ends the turn
; lda #$00
; sta actions
lda #$01
jsr dec_actions
; skip tile update if flag is not set
lda game_flags
and #%00000010
beq @skip_tile_update
; test if the current tile
; is already marked if so, do not update the previous tile but rather unmark the current
lda player_x
sta get_tile_x
lda player_y
sta get_tile_y
jsr get_tile
and #%10000000
beq @tile_update_not_marked
jsr update_tile
jmp @skip_tile_update
@tile_update_not_marked:
; update current tile if player did move
; game mode is puzzle
; therefore a tile update will update the tile to become
; a passed over tile by setting bit 7 to 1
; for that however we use the previous location rather than the current one
; to update the tile behind the player
lda player_x
pha
lda player_y
pha
lda player_x_bac
sta player_x
lda player_y_bac
sta player_y
jsr update_tile
; restore position
pla
sta player_y
pla
sta player_x
@skip_tile_update:
@player_not_moved:
jsr update_player_animation
; store previous position
lda player_x
sta player_x_bac
lda player_y
sta player_y_bac
jmp update_crit_done
; forces a complete UI refresh
; by overwriting the last known values
force_update_ui:
ldx player_damage
dex
stx last_player_damage
ldx player_armor
dex
stx last_player_armor
ldx coins
dex
stx last_coins
ldx player_exp
dex
stx last_player_exp
ldx pmagic
dex
stx last_magic
lda map_flags ; skip update this frame
ora #%00001000
sta map_flags
rts
; updates UI for key, armor and damage count
; performs nametable update required to display numbers
update_ui:
lda map_flags ; skip update this frame if flag is set
and #%00001000
beq @no_skip:
lda map_flags
and #%11110111
sta map_flags
rts
@no_skip:
lda last_player_damage
cmp player_damage
beq @no_update_damage
; UI is always at same location so it is easy to update
lda player_damage
sta last_player_damage ; update completed store for next frame
jsr convert_hex
; update nametable
lda #03
sta get_tile_x
lda #26
sta get_tile_y
ldx nametable
jsr draw_hex_buffer
rts
@no_update_damage:
lda last_player_armor
cmp player_armor
beq @no_update_armor
; UI is always at same location so it is easy to update
lda player_armor
sta last_player_armor ; update completed store for next frame
jsr convert_hex
; update nametable
lda #06
sta get_tile_x
lda #26
sta get_tile_y
ldx nametable
jsr draw_hex_buffer
rts
@no_update_armor:
lda last_coins
cmp coins
beq @no_update_coins
; UI is always at same location so it is easy to update
lda coins
sta last_coins ; update completed store for next frame
jsr convert_hex
; update nametable
lda #06
sta get_tile_x
lda #25
sta get_tile_y
ldx nametable
jsr draw_hex_buffer
rts
@no_update_coins:
lda last_player_exp
cmp player_exp
beq @no_update_exp
; UI is always at same location so it is easy to update
lda player_exp
sta last_player_exp ; update completed store for next frame
jsr convert_hex
; update nametable
lda #06
sta get_tile_x
lda #24
sta get_tile_y
ldx nametable
jsr draw_hex_buffer
dec get_tile_x
ldx #$00
lda #$0E
ldx nametable
jsr set_tile
rts
@no_update_exp:
lda last_magic
cmp pmagic
beq @no_update_magic
lda pmagic
sta last_magic
; draw single tiles
lda #02
sta get_tile_x
lda #25
sta get_tile_y
ldy pmagic
lda magic_ui_1, y
ldx #$00
jsr set_tile
ldy pmagic
lda magic_ui_2, y
jsr set_tile_immediate
ldy pmagic
lda magic_ui_3, y
jsr set_tile_immediate
; draw current spell sprite
ldy spell_type
lda #08
sta get_tile_x
lda weapon_sprite, y
ldx nametable
jsr set_tile
rts
@no_update_magic:
rts
; non-critical game updates
update_game:
; test if level up
jsr level_up_check
jsr check_player_move
cmp #$01
bne @player_not_moved
; test trap flag, if trapped rng roll to become free
; if bad roll skip move by failing collision check
; but consume action
lda map_flags
and #%00100000
beq @not_trapped
jsr random
lda rand8
cmp #$7F
bcc @free ; free
; if not free skip move and consume action
; restore position and remove smooth movement
lda #$00
sta smooth_down
sta smooth_up
sta smooth_right
sta smooth_left
lda player_x_bac
sta player_x
lda player_y_bac
sta player_y
; set flag to set actions to 0 on crit update
lda map_flags
ora #%00010000
sta map_flags
jmp @player_not_moved
@free:
lda map_flags
and #%11011111
sta map_flags ; unset trap flag
@not_trapped:
jsr collision_check
sta temp
lda game_flags
and #%11111110
ora temp
sta game_flags ; store this frames collision result in game_flags
jsr setup_tile_updates
@player_not_moved:
; run custom update routine unless ptr is FF FF
lda map_sub_ptr
and map_sub_ptr+1 ; FF and FF = FF
cmp #$FF
beq @no_sub
lda map_sub_ptr
sta src_ptr
lda map_sub_ptr+1
sta src_ptr+1
jsr jsr_indirect
@no_sub:
; see if song finished by testing square wave 1
ldy #$00
lda (pulse_ptr_1), y
cmp #$FF
bne @no_audio_reset
; jsr init_test_song
@no_audio_reset:
; test for movement inputs, if non occured reset
; timer
lda last_inputs
and #%00001111
bne @no_move_timer_reset
sta move_timer
@no_move_timer_reset:
; update hp display
lda #$3A ; heart sprite
sta sprite_data_8+1
sta sprite_data_9+1
sta sprite_data_A+1
ldx player_hp
cpx #MAX_HP
beq @no_damage
ldy #$0A
@damage_display_loop:
lda obj_index_to_addr, y
sta sprite_ptr
tya
pha
ldy #$01
lda #$24 ; empty tile
sta (sprite_ptr), y
pla
tay
inx
dey
cpx #MAX_HP
bne @damage_display_loop
@no_damage:
; update other UI elements
ldy #$24 ; empty sprite
lda key_count
beq @no_keys
ldy #$59 ; key sprite
@no_keys:
sty sprite_data_B+1
ldy weapon_type
lda weapon_sprite, y
sta sprite_data_C+1
; ldy #$24 ; empty sprite
; lda player_armor_base
; beq @no_armor
; ldy #$3C
; @no_armor:
; sty sprite_data_D+1
; update damage animation
jsr update_damage_animation
jsr test_victory_condition
bne @done
; if animation timer is already going do not porceed
lda delay_timer
ora delay_timer+1
bne @done
; only finish if movment finished as well
lda smooth_up
ora smooth_down
ora smooth_left
ora smooth_right
; bne @done
; set up win condition pointers
sta delay_timer
lda #$00
sta delay_timer+1 ; second byte, we only need first byte
lda #<empty_sub
sta delay_update
lda #>empty_sub
sta delay_update+1
lda #<update_none
sta update_sub
lda #>update_none
sta update_sub+1
lda #<update_crit_none
sta update_sub_crit
lda #>update_crit_none
sta update_sub_crit+1
lda #<init_win_condition
sta delay_done
lda #>init_win_condition
sta delay_done+1
@done:
jmp update_done
; this sub rout01e is called when win condition
; animation finishes
init_win_condition:
lda #$00
sta $2001 ; no rendering
; advance seed
lda seed
jsr random_reg
sta seed
lda seed+1
jsr random_reg
sta seed+1
inc level
; inc magic every leve unless it is full already
lda pmagic_base
cmp pmagic
beq @no_inc_magic
inc pmagic
@no_inc_magic:
; store gamestate
jsr store_save
; if random flag is set load next map
lda #%10000000
and load_flags
bne @next_map
; set flag to reload values from sram
lda load_flags
ora #%00100000
sta load_flags
lda #$01
sta nametable
set_nmi_flag
ldx #GAME_MODE_MESSAGE
stx game_mode
jsr load_menu
jsr init_message
lda #$01 ; set flag to skip update
vblank_wait
rts
@next_map:
; lda #MAIN_MENU_RANDOM
; sta menu_select
; load next map
; make it load the next seed
lda seed
sta seed_bac
lda seed+1
sta seed_bac+1
; load from savegame
lda load_flags
ora #%00100000
sta load_flags
jsr reload_room
; jsr a_input_main_menu
rts
; loads the game over message as a menu
init_game_over:
; re-enable inputs
lda nmi_flags
and #%11011111
sta nmi_flags
lda #$00
sta $2001 ; no rendering
lda #$01
sta nametable
set_nmi_flag
vblank_wait
ldx #GAME_MODE_GAME_OVER
lda #GAME_MODE_MESSAGE
sta game_mode
jsr load_menu
jsr init_message
; write level to correct position
lda level
jsr convert_hex
; update nametable
lda #16
sta get_tile_x
lda #10
sta get_tile_y
jsr draw_hex_buffer
; hide all sprites
jsr hide_objs
lda #$01 ; set flag to skip update
vblank_wait
rts
; this sub routine updates the player's animation based on
; the movement offset
; inputs:
; smooth up, down, left, right
; side effects:
; modifies registers, flags
; modifies player sprite and attributes
update_player_animation:
lda delay_timer ; do not update during delay timer
bne @done
lda iframes
beq @no_iframes
; if iframes flash player
lda sprite_data+1
cmp #$24
bne @swap_invisible
lda #$32
sta sprite_data+1
rts
@swap_invisible:
lda #$24
sta sprite_data+1
rts
@no_iframes:
; see if player is invisible if so swap back to normal sprite
lda sprite_data+1
cmp #$24
bne @not_invisible
lda #$32
sta sprite_data+1
@not_invisible:
; update player animation
; every 128 frames blink
lda player_timer
bne @done
lda sprite_data+1
eor #%10000000
sta sprite_data+1
and #%10000000
bne @short_timer ; blink for 8 frames
; long timer wait 128 frames
jsr random
lda rand8
ora #%10000000
sta player_timer ; at least 128 frames, but maybe more
rts
@short_timer:
lda #08
sta player_timer
rts
@done:
dec player_timer
rts
; checks for player collision based on the currently occupied tile
; inputs:
; player_x, y and respective _bac
; side effects:
; if tile does collide, player position is restored
; to values in _bac
; overwrites src_ptr
; returns:
; a = 0 -> if collision did not occur
; a = 1 -> if collision occured
collision_check:
lda map_flags
and #%01000000 ; collision off flag
beq @collision_enabled
lda #$00
rts
@collision_enabled:
lda player_x
sta get_tile_x
lda player_y
sta get_tile_y
jsr get_tile
tax
; get routine for current tile
lda tile_sub_lo, x
sta src_ptr
lda tile_sub_hi, x
sta src_ptr+1
jsr jsr_indirect
cmp #$01
bne @no_collision_tile
beq @collision_tile
@no_collision_tile:
; after we have checked tile collision also verify sprite collision
jsr sprite_collision
cmp #$01
bne @no_collision
@collision_tile:
; if collision, restore previous location
; and remove smooth movement
ldx #$00
stx smooth_left
stx smooth_right
stx smooth_up
stx smooth_down
ldx player_x_bac
stx player_x
ldx player_y_bac
stx player_y
jsr convert_tile_location
rts
@no_collision:
rts
; this sub routine updates the tiles to clear counter
; it does this based on the negative flag
; inputs:
; N flag = 0 -> dec
; N flag = 1 -> inc
; side effects:
; tiles_to_clear is changed
; flags may be changed
; registers are preserved
update_tiles_to_clear:
pha
; eor sets the negative flag when bit 7 is set
; since that is exactly the bit we set we can use it to
; decide wheter to inc or dec
bmi @negative_flag
lda tiles_to_clear
clc
adc #$01
sta tiles_to_clear
lda tiles_to_clear+1
adc #$00
sta tiles_to_clear+1
jmp @done
@negative_flag:
lda tiles_to_clear
sec
sbc #$01
sta tiles_to_clear
lda tiles_to_clear+1
sbc #$00
sta tiles_to_clear+1
@done:
pla
rts
; this sub rotuine checks win condition
; returns:
; a == 0 if won
; a == 1 if not won
test_victory_condition:
; test victory condition
; if only one tile is left to clear the player must be on it
lda tiles_to_clear+1
cmp #$00
bne @done
lda tiles_to_clear
cmp #$01
bne @done
lda #$00
rts
@done:
lda #$01
rts
; this sub routine loads the win screen
; inputs:
; none
; side effects:
; inits a new game mode
init_message:
lda #$00 ; move player offscreen
sta sprite_data
sta sprite_data+3
sta player_x
sta player_y
sta player_x_bac
sta player_y_bac
lda #<update_message
sta update_sub
lda #>update_message
sta update_sub+1
lda #<update_crit_none
sta update_sub_crit
lda #>update_crit_none
sta update_sub_crit+1
rts
; update routine for the win screen
update_message:
jmp update_done
; this sub routine checks if player has moved
; compared to its previous position
; inputs:
; player_x, y,
; player_x_bac, y_bac
; returns:
; a = 0 -> no move
; a = 1 -> move
check_player_move:
; check if player moved
lda #$00 ; move flag
ldx player_x
cpx player_x_bac
beq @player_not_moved_x
lda #$01 ; did move
@player_not_moved_x:
ldx player_y
cpx player_y_bac
beq @player_not_moved_y
lda #$01 ; did move
@player_not_moved_y:
rts
; tests if player leveled up
; and runs relevant code
level_up_check:
lda player_exp
cmp #LVL_UP_EXP
bcc @not_level_up
; TODO add option to select reward
lda #$00
sta player_exp
inc player_level
; upgrade the smaller stat
lda player_damage
cmp player_armor_base
bcc @upgrade_damage
inc player_armor_base
inc player_armor
rts
@upgrade_damage:
inc player_damage
@not_level_up:
rts
; updates the sword sprite (sprite 1)
; based on the delay timer
sword_update:
; lda #$33 ; tile
; sta sprite_data_1+1
ldy weapon_x
lda tile_convert_table, y
sta sprite_data_1+3
ldy weapon_y
lda tile_convert_table, y
sta sprite_data_1
rts
; moves sword to 0/0
; enables player inputs
sword_done:
lda nmi_flags
and #%11011111 ; enables input
sta nmi_flags
lda #$00
sta weapon_x
sta weapon_y