···991010SECTION "DungeonCode", ROM0
11111212+BIT_USED_ROOM:: db $01
1313+BIT_ENTRANCE:: db $02
1414+BIT_DOOR_NORTH:: db $04
1515+BIT_DOOR_EAST:: db $08
1616+BIT_DOOR_SOUTH:: db $10
1717+BIT_DOOR_WEST:: db $20
1818+BIT_STAIR_BELOW:: db $40
1919+BIT_STAIR_UP:: db $80
2020+1221; Params:
1322; Starting Width: B
1423; Starting Height: C
1524InitDungeon::
1616- ld hl, current_width
2525+ ld hl, current_width ; current_width = B
1726 ld [hl], b
1818- ld hl, current_height
2727+ ld hl, current_height ; current_height = C
1928 ld [hl], c
2029 ret
21302231GenerateDungeon::
2323- ; Dungeon Area = E
3232+ ; Cache current register values to restore later
3333+ ; In the event this is called with values in the
3434+ ; registers to be used in an outer function
2435 push de
2536 push hl
2637 push af
2738 push bc
28392929- ld hl, current_width
4040+ ld hl, current_width ; dungeon_area = current_width * current_height
3041 ld e, [hl]
3142 ld d, 0
3243 ld hl, current_height
···3546 ld d, h
3647 ld e, l
37483838- ; Generated Cells Number = A
3939- ld a, 0
4949+ ; generated_cells_number is stored in A
5050+ ld a, 0 ; generated_cells_number = 0
40514141- ; i = B
4242- ld b, 0
4343-.LoopCheck:
4444- cp a, e
4545- jp nz, GenerateDungeon.Loop_Skip
5252+ ; i is stored in B
5353+ ld b, 0 ; i = 0
5454+.LoopCheck: ; for loop begin
5555+ cp a, e ; if (generated_cells_number < dungeon_area) && ((i == 0) || (i < generated_cells_number))
5656+ jp nz, GenerateDungeon.Loop_Skip ; Break Loop
4657 jp nc, GenerateDungeon.Loop_Skip
4747- jp GenerateDungeon.LoopCheck2
4858.LoopCheck2:
4959 push af
5060 ld a, b
···6272 jp nz, GenerateDungeon.Loop_Skip
6373 jp nc, GenerateDungeon.Loop_Skip
6474.Loop_Body:
6565- push af
7575+ push af ; if ((i == 0) && (generated_cells_number == 0))
6676 ld a, b
6777 cp a, 0
6878 pop af
6979 jp nz, GenerateDungeon.Loop_Body2
7080 cp a, 0
7181 jp nz, GenerateDungeon.Loop_Body2
8282+ push bc
8383+ ld b, 0
8484+ ld c, e
8585+ call rand_range
8686+ push hl
8787+ ld hl, entrance_id
8888+ ld [hl], b
8989+ ld hl, generated_cells
9090+ ld [hl], b
9191+ ld hl, dungeon_grid
9292+ push af
9393+ ld a, l
9494+ add a, b
9595+ ld l, a
9696+ ld a, BIT_ENTRANCE
9797+ or a, BIT_USED_ROOM
9898+ ld [hl], a
9999+ pop af
100100+ ld a, 1
101101+ pop hl
102102+ pop bc
72103.Loop_Body2:
104104+ call GenerateRoom
73105.Loop_Skip:
74106 pop bc
75107 pop af
76108 pop hl
77109 pop de
78110111111+ ret
112112+113113+GenerateRoom::
79114 ret