-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdungeon_generation.asm
1229 lines (1068 loc) · 42.2 KB
/
dungeon_generation.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
triggerDefPtr .byte $00, $00
areaGen_items .byte $08
areaGen_feats .byte $25
areaGen_npcs .byte $08
brushX .byte $00
brushY .byte $00
prevCoordX .byte $00
prevCoordY .byte $00
entryX .byte $00
entryY .byte $00
featOriginX .byte $00
featOriginY .byte $00
brushPtr .byte $00, $00
floorTile .byte $01
brushTile .byte $00
sweepIter .byte $00
sweepSteps .byte $00
genDir .byte $00
genDirModX .byte $00, $01, $00, $ff
genDirModY .byte $ff, $00, $01, $00
sweepBrushLn .byte $00
sweepBrush .byte %00000000
triggerTiles .byte $00, $00, $00, $00 ; Limit of four triggers for now
corrLn .byte $00
bitMasks .byte %10000000
.byte %01000000
.byte %00100000
.byte %00010000
.byte %00001000
.byte %00000100
.byte %00000010
.byte %00000001
leIndex .byte $00
generateDungeon:
;lda #$13 ; Set known seed for debug.
;sta seed
;ldx seed ; Print seed as decimal for debug.
;lda #<$0740
;sta print_target
;lda #>$0740
;sta print_target+1
;jsr print_decimal
ldx #$00 ; Clear loose ends from any previous run
lda #$ff
clearLooseEndsLoop sta looseEndDir, x
inx
cpx #$08
bne clearLooseEndsLoop
lda #$0d ; Solid rock tile
sta brushTile ; Fill entire buffer with rock
jsr fillLevel
lda #$01 ; Set brush tile to floor
sta brushTile
lda playerX ; Start carving caves at player loc
sta brushX
sta entryX
lda playerY
sta brushY
sta entryY
jsr randomGenDir ; Opposite direction of closest border, perhaps?
jsr addCaveRoom
genDungFeatsLoop
lda #$01 ; Set brush tile to floor
sta brushTile
lda #$0a ; Randomly decide if to continue with current loose end
sta num3 ; or switch to another
jsr rndInt
cmp #$01
beq useRandomLE
jmp randomFeatType
useRandomLE jsr getRandomActiveLE
randomFeatType lda #$07 ; Get a random int, 0-7
sta num3
jsr rndInt
cmp #$01
bcc loopAddRoomFeat
jsr addCaveCorridor
jmp featAdded
loopAddRoomFeat jsr addCaveRoom
featAdded
ldx leIndex ; Disable loose end
lda #$ff
sta looseEndDir, x
dec areaGen_feats
lda areaGen_feats
cmp #$00
bne genDungFeatsLoop
generationDone jsr decorateArea
jsr addExitTriggersToArea
jsr addItemsToArea
jsr addNpcsToArea
rts
;; +----------------------------------+
;; | CAVE CORRIDOR |
;; +----------------------------------+
caveCorrNoSolution rts
addCaveCorridor lda #$00
sta attempts
lda genDir
sta entryGenDir
addCaveCorrRetry inc attempts
lda attempts
cmp #$05
beq caveCorrNoSolution
ldx leIndex
lda looseEndX, x
sta brushX
lda looseEndY, x
sta brushY
lda #$07 ; Get a random corridor length, 0-7 + 3
sta num3
jsr rndInt
adc #$03
sta corrLn
sta modVal
jsr isTargetOutOfBounds
cmp #$01
beq addCaveCorrRetry
ldx #$00
stx iter
addCaveCorridorLoop cpx corrLn
beq addCaveCorridorDone
jsr brushCoordsToPtr
lda #$01
ldy #$00
sta ($22), y
jsr stepBrush
lda #$10 ; Get a random corridor length, 0-7 + 3
sta num3
jsr rndInt
cmp #$01
bne nextCorrLoopIter
jsr randomGenDirAheadOrTurn
jsr addLooseEnd
lda entryGenDir
sta genDir
nextCorrLoopIter inc iter
ldx iter
jmp addCaveCorridorLoop
addCaveCorridorDone jsr randomGenDirAheadOrTurn
jsr addLooseEnd
rts
;; +----------------------------------+
;; | CAVE ROOM |
;; +----------------------------------+
entryGenDir .byte $00
addCaveRoom lda genDir
sta entryGenDir
lda #$00 ; Begin pushing upwards
sta genDir
lda brushX ; Store current brush position as the origin of the cave room
sta featOriginX
lda brushY
sta featOriginY
addCaveRoomDirLoop lda featOriginX ; Return to origin of room for new dir
sta brushX
lda featOriginY
sta brushY
jsr generateSweepBrush ; Generate a random sweep brush
jsr genDirRight ; Center the brush around the origin
ldx #$00
ldy sweepBrushLn
dey
sty sweepSteps
centerSweepLoop jsr stepBrush
cpx sweepSteps
inx
dec sweepSteps
bcs centerSweepLoop
jsr genDirLeft
centered
jsr genSweepSteps
jmp checkSweepBounds
tryFewerSweepSteps dec sweepSteps
checkSweepBounds lda sweepSteps
cmp #$01
bmi endCaveSweep2
sta modVal
inc modVal
jsr isTargetOutOfBounds
cmp #$01
beq tryFewerSweepSteps
jsr genDirRight
lda #$02
sta modVal
jsr isTargetOutOfBounds
cmp #$01
bne prepCheckBrush
jsr genDir180
jsr stepBrush
jsr genDir180
prepCheckBrush jsr genDirLeft
jmp checkBrushBounds
tryShorterBrush dec sweepBrushLn
jmp checkBrushBoundsLoop
checkBrushBounds jsr genDirLeft
checkBrushBoundsLoop lda sweepBrushLn
cmp #$01
bmi endCaveSweep3
sta modVal
;inc modVal
jsr isTargetOutOfBounds
cmp #$01
beq tryShorterBrush
jsr genDirRight
prepSweepLoop ldx #$00 ; Set up iteration for sweeping
stx iter
sweepNStepsLoop jsr brushCoordsToPtr
jsr storeBrushToPrevCoord
jsr applySweepBrush ; Apply the brush
inc iter
ldx iter
cpx sweepSteps
beq endCaveSweep ; End the sweeping if all steps have been performed
jsr returnBrushToPrevCoord
jsr stepBrush ; ...and forward one step
jmp sweepNStepsLoop
endCaveSweep3 jsr genDirRight
jmp endCaveSweep2
endCaveSweep ;jsr addLooseEnd
endCaveSweep2 inc genDir ; Set up next sweep dir
lda genDir
cmp #$04
bne sweepNextDir
lda featOriginX ; Return to origin of room for new dir
sta brushX
lda featOriginY
sta brushY
lda entryGenDir ; Store loose end in dir of entry or turn
sta genDir
jsr randomGenDirAheadOrTurn
jsr addLooseEnd
jsr genDir180 ; Store loose end in opposite dir or turn
jsr randomGenDirAheadOrTurn
jsr addLooseEnd
endAddCaveRoom rts
sweepNextDir jmp addCaveRoomDirLoop
;; +----------------------------------+
;; | SWEEP BRUSH ROUTINES |
;; +----------------------------------+
applySweepBrush
ldx #$00
stx sweepIter
applySweepBrushLoop lda brushTile
ldy #$00
sta ($22), y
lda sweepBrush
and segMasks, x
cmp segMasks, x
bne noSweepDent
jsr stepBrush
jsr brushCoordsToPtr
lda brushTile
ldy #$00
sta ($22), y
jsr genDir180
jsr stepBrush
jsr brushCoordsToPtr
jsr genDir180
noSweepDent inc sweepIter
ldx sweepIter
cpx sweepBrushLn
beq sweepBrushApplied
jsr genDirLeft
jsr stepBrush
jsr brushCoordsToPtr
jsr genDirRight
jmp applySweepBrushLoop
sweepBrushApplied rts
generateSweepBrush ldx #$00
stx sweepBrush
stx sweepIter
jsr genSweepBLn
genSweepBrushLoop
lda #$01
sta num3
jsr rndInt
ldx sweepIter
cmp #$01
bne noSweepBrushIndent
lda sweepBrush
ora segMasks, x
sta sweepBrush
noSweepBrushIndent inx
cpx sweepBrushLn
beq endSweepBrushGen
stx sweepIter
jmp genSweepBrushLoop
endSweepBrushGen rts
genSweepSteps: lda #$03
sta num3
jsr rndInt
sta sweepSteps
inc sweepSteps
inc sweepSteps
ldx genDir
rts
genSweepBLn: lda #$03
sta num3
jsr rndInt
sta sweepBrushLn
inc sweepBrushLn
inc sweepBrushLn
ldx genDir
rts
;; +----------------------------------+
;; | BRUSH ROUTINES MANIPULATION |
;; +----------------------------------+
stepBrush ldy genDir
lda brushX
clc
adc genDirModX, y
sta brushX
lda brushY
clc
adc genDirModY, y
sta brushY
rts
genDir180 jsr genDirLeft
jsr genDirLeft
rts
genDirLeft dec genDir
lda genDir
cmp #$ff
bne genDirRts
lda #$03
sta genDir
genDirRts rts
genDirRight inc genDir
lda genDir
cmp #$04
bne genDirRts
lda #$00
sta genDir
rts
brushCoordsToPtr lda #<currentArea
sta $22
lda #>currentArea
sta $23
lda currentAreaWidth
sta inc22ModVal
lda brushY
sta rollIterations
jsr roll22Ptr
lda brushX
sta inc22ModVal
jsr inc22Ptr
lda $22
sta brushPtr
lda $23
sta brushPtr+1
rts
returnBrushToPrevCoord lda prevCoordX ; Return brush to previous location...
sta brushX
lda prevCoordY
sta brushY
rts
storeBrushToPrevCoord lda brushX ; Keep coords before modifying them
sta prevCoordX
lda brushY
sta prevCoordY
rts
randomGenDir: lda #$03
sta num3
jsr rndInt
sta genDir
rts
randomGenDirAheadOrTurn:
lda #$03
sta num3
jsr rndInt
cmp #$02
bcc noTurn
cmp #$02
beq rGenDirTurnLeft
jsr genDirRight
rts
rGenDirTurnLeft jsr genDirLeft
noTurn rts
;; +----------------------------------+
;; | LOOSE ENDS |
;; +----------------------------------+
looseEndX .byte $00, $00, $00, $00, $00, $00, $00, $00
looseEndY .byte $00, $00, $00, $00, $00, $00, $00, $00
looseEndDir .byte $ff, $ff, $ff, $ff, $ff, $ff, $ff, $ff
addLooseEnd:
ldx #$00
findFreeLE lda looseEndDir, x
cmp #$ff
beq foundLooseEnd
inx
cpx #$08
beq findRandomActiveLE
jmp findFreeLE
foundLooseEnd:
lda brushX
sta looseEndX, x
lda brushY
sta looseEndY, x
lda genDir
sta looseEndDir, x
stx leIndex
rts
findRandomActiveLE
jsr getRandomActiveLE
jmp foundLooseEnd
fraAttempt .byte $00
getRandomActiveLE
lda #$00
sta fraAttempt
getRandomActiveLERetry
lda fraAttempt
cmp #$0f
beq createLooseEndFromRandomFloorSpot
inc fraAttempt
lda #$07
sta num3
jsr rndInt
tax
lda looseEndDir, x
cmp #$ff
beq getRandomActiveLERetry
stx leIndex
rts
createLooseEndFromRandomFloorSpot
stx leIndex
lda #$00
sta fraAttempt
cLeRetry jsr getRandomFloorTile
cmp #$01
bne cLeStoreLE
cLeNextAttempt inc fraAttempt
lda fraAttempt
cmp #$30
bne cLeRetry
lda playerX
sta brushX
lda playerY
sta brushY
cLeStoreLE ldx leIndex
lda brushX
sta looseEndX, x
lda brushY
sta looseEndY, x
jsr randomGenDir
lda genDir
ldx leIndex
sta looseEndDir, x
jmp getRandomActiveLE
getRandomFloorTileGrnt
jsr getRandomFloorTile
cmp #$01
beq getRandomFloorTileGrnt
rts
getRandomFloorTile:
lda currentAreaWidth ; Random X Pos
sta num3
dec num3
jsr rndInt
sta brushX
lda currentAreaHeight ; Random Y Pos
sta num3
dec num3
jsr rndInt
sta brushY
ldx brushX ; Check bounds
ldy brushY
jsr isOutOfBounds
cmp #$01
beq getRDFTRetOOB
jsr brushCoordsToPtr
ldy #$00
lda ($22), y
cmp #$04
bcs getRDFTRetOOB
lda #$00
rts
getRDFTRetOOB lda #$01
rts
;; +----------------------------------+
;; | GENERAL AREA MANIPULATION |
;; +----------------------------------+
fillLevel:
ldx #$00
stx iter
lda #<currentArea
sta $22
lda #>currentArea
sta $23
lda currentAreaWidth
sta inc22ModVal
fillLevelRow ldy #$00
lda brushTile
fillLevelRowLoop cpy currentAreaWidth
beq levelRowFilled
sta ($22), y
iny
jmp fillLevelRowLoop
levelRowFilled inc iter
jsr inc22Ptr
ldx iter
cpx currentAreaHeight
bcs fillLevelComplete
jmp fillLevelRow
fillLevelComplete rts
;; +----------------------------------+
;; | NPC GENERATION |
;; +----------------------------------+
addNpcsToArea:
ldx #$00
stx npcTableSize
lda #<npcTable
sta $20
lda #>npcTable
sta $21
lda npcTableRowSize
sta inc20ModVal
txa
clc
calcTotalNpcRandWgt adc npcSet_weight, x
inx
cpx npcSet_size
bne calcTotalNpcRandWgt
sta totalRandWeight
dec totalRandWeight
addNpcLoop
lda totalRandWeight
sta num3
jsr rndInt
sta currentWeight
ldx #$00
lda #$00
clc
findNpcSetIndexLoop adc npcSet_weight, x
cmp currentWeight
bcs npcSetIndexFound
inx
jmp findNpcSetIndexLoop
npcSetIndexFound lda npcSet_type, x
tax
ldy var_npcModes
lda npc_attributes, x
sta ($20), y
ldy var_npcTileID
lda npc_tileID, x
sta ($20), y
ldy var_npcSpritePtr
lda npc_spritePtr, x
sta ($20), y
ldy var_npcNamePtrHi
lda npc_nameHi, x
sta ($20), y
ldy var_npcNamePtrLo
lda npc_nameLo, x
sta ($20), y
ldy var_npcCurrentHP
lda npc_maxHP, x
sta ($20), y
ldy var_npcMoveCost
lda npc_moveCost, x
sta ($20), y
ldy var_npcMode
lda #$01
sta ($20), y
jsr getRandomFloorTileGrnt ; Select a random location
ldy var_npcXPos
lda brushX
sta ($20), y
ldy var_npcYPos
lda brushY
sta ($20), y
inc npcTableSize
jsr inc20Ptr
dec areaGen_npcs
ldx areaGen_npcs
cpx #$00
bne addNpcLoop
rts
;; +----------------------------------+
;; | ITEM GENERATION |
;; +----------------------------------+
totalRandWeight .byte $00
currentWeight .byte $00
addItemsToArea:
ldx #$00
stx itemTableSize
lda #<itemTable
sta $20
lda #>itemTable
sta $21
lda itemTableRowSize
sta inc20ModVal
txa
clc
calcTotalRandWeight adc itemSet_weight, x
inx
cpx itemSet_size
bne calcTotalRandWeight
sta totalRandWeight
dec totalRandWeight
addItemLoop
lda totalRandWeight
sta num3
jsr rndInt
sta currentWeight
ldx #$00
lda #$00
clc
findItemSetIndexLoop adc itemSet_weight, x
cmp currentWeight
bcs itemSetIndexFound
inx
jmp findItemSetIndexLoop
itemSetIndexFound
lda itemSet_type, x ; Look up the selected type
sta tmpX
tax
ldy var_itemModes ; Copy from ItemDB to current items
lda itemAttributes, x
sta ($20), y
sta argItemModes
ldy var_itemTileID
lda itemTile, x
sta ($20), y
ldy var_itemTypeID
txa
sta ($20), y
ldy var_itemIdentifyToTypeID
lda itemNameHi, x
sta ($20), y
ldy var_itemValue
lda itemValue, x
sta ($20), y
lda argItemModes
and #%00011111 ; Switch off flag bits
cmp #$00
beq addItmSetRndGoldVal
cmp #$06
bcc addItmSetRndIdentified
addItmSetRndDurability
jsr rndNum
ldy var_itemValue
sta ($20), y
jmp addItemDoAdd
addItmSetRndGoldVal
lda itemSet_goldRnd
sta num3
jsr rndInt
clc
adc itemSet_goldMod
ldy var_itemValue
sta ($20), y
jmp addItemDoAdd
addItmSetRndIdentified
lda #$01
sta num3
jsr rndInt
cmp #$01
beq addItemDoAdd
ldy var_itemTypeID
lda ($20), y
ldy var_itemIdentifyToTypeID
sta ($20), y
ldy var_itemValue
lda #$00
sta ($20), y
ldy var_itemModes
findSubstituteLoop inx
lda itemAttributes, x
and #%01000000
cmp #%01000000
bne findSubstituteLoop
lda itemAttributes, x
sta ($20), y
ldy var_itemTileID
lda itemTile, x
sta ($20), y
ldy var_itemTypeID
txa
sta ($20), y
addItemDoAdd
jsr getRandomFloorTileGrnt ; Select a random location
ldy var_itemXPos
lda brushX
sta ($20), y
ldy var_itemYPos
lda brushY
sta ($20), y
inc itemTableSize
jsr inc20Ptr
dec areaGen_items
ldx areaGen_items
cpx #$00
beq endAddItem
jmp addItemLoop
endAddItem rts
;; +----------------------------------+
;; | EXIT TRIGGER GENERATION |
;; +----------------------------------+
genTriggerTableRowSize .byte $09
addExitTriggersToArea
lda triggerDefPtr ; Return pointer to where we were in level header
sta $20
lda triggerDefPtr+1
sta $21
ldy #$00 ; Read number of triggers to generate
sty iter
lda ($20), y
sta triggerTableSize
lda #$01 ; Safely skip one byte ahead
sta inc20ModVal
jsr inc20Ptr
lda #$00
cmp triggerTableSize
bne contGenTriggers
rts
contGenTriggers lda #<triggerTable
sta $22
lda #>triggerTable
sta $23
lda genTriggerTableRowSize
sta inc20ModVal
genTriggerLoop
ldy #$03 ; Read and set trigger type
lda ($20), y
ldy var_triggerType
sta ($22), y
ldy #$04 ; Read trigger tile
ldx iter
lda ($20), y
sta brushTile
ldy #$05 ; Read and set target area ptr
lda ($20), y
ldy var_triggerTgtAreaLo
sta ($22), y
ldy #$06
lda ($20), y
ldy var_triggerTgtAreaHi
sta ($22), y
ldy #$07 ; Read and set target coords or index
lda ($20), y
ldy var_triggerTgtXPos
sta ($22), y
ldy #$08
lda ($20), y
ldy var_triggerTgtYPos
sta ($22), y
ldy #$00 ; Read placement algorithm type and dispatch
lda ($20), y
cmp #$01
beq genTriggerEntryPlacemnt
lda $22 ; Store away pointer to triggerTablePos
sta tmpPtr1
lda $23
sta tmpPtr1+1
genTriggerRandomPlacmnt jsr getRandomFloorTileGrnt
jmp genTriggerAtBrushCoords
genTriggerEntryPlacemnt lda entryX ; Add stairs back up
sta brushX
lda entryY
sta brushY
genTriggerAtBrushCoords jsr brushCoordsToPtr
lda brushTile
ldy #$00
sta ($22), y
lda tmpPtr1 ; Read back trigger table ptr
sta $22
lda tmpPtr1+1
sta $23
ldy var_triggerXPos
lda brushX
sta ($22), y
ldy var_triggerYPos
lda brushY
sta ($22), y
lda trTargetIndex ; Set player coords to this trigger if
cmp iter ; we entered through it
bne genTriggerEndLoop
lda brushX
sta playerX
lda brushY
sta playerY
genTriggerEndLoop
lda triggerTableRowSize ; Will have been overwritten by brushCoordsToPtr
sta inc22ModVal
jsr inc20Ptr
jsr inc22Ptr
inc iter
lda iter
cmp triggerTableSize
bne genTriggerNextIter
rts
genTriggerNextIter jmp genTriggerLoop
;; +----------------------------------+
;; | AREA DECORATION |
;; +----------------------------------+
rowIter .byte $00
colIter .byte $00
decorateArea: lda #$05
sta $d020
lda #$00
sta brushX
sta brushY
sta rowIter
sta colIter
jsr brushCoordsToPtr
lda #$01
sta inc20ModVal
sta inc22ModVal
lda #<decorationBuffer
sta $20
lda #>decorationBuffer
sta $21
decorateColLoop ldx colIter
ldy rowIter
ldy #$00
lda ($22), y
jsr getTileDecoration
ldy #$00
sta ($20), y
inc colIter
jsr inc20Ptr
jsr inc22Ptr
lda colIter
cmp currentAreaWidth
beq decorateNextRow
jmp decorateColLoop
decorateNextRow inc rowIter
ldx #$00
stx colIter
lda rowIter
inc $d020
cmp currentAreaHeight
bne decorateColLoop
endDecorateArea lda #$08
sta $d020
lda #<decorationBuffer ; Copy decorated buffer back to level
sta $20
lda #>decorationBuffer
sta $21
lda #<currentArea
sta $22
lda #>currentArea
sta $23
lda currentAreaWidth
sta memcpy_rowSize
lda currentAreaHeight
sta memcpy_rows
jsr memcpy
lda #$00
sta $d020
rts
getTileDecoration cmp #$01
beq getRandomGroundTile
jmp findWallTile
rts
getRandomGroundTile lda #$02