···11-# Created by https://www.toptal.com/developers/gitignore/api/c
22-# Edit at https://www.toptal.com/developers/gitignore?templates=c
33-44-bin/
55-obj/11+obj/
22+bin/
···11+SECTION "DungeonVariables", WRAM0
22+33+dungeon_grid:: ds 512
44+entrance_id:: db
55+current_room:: db
66+current_width:: db
77+current_height:: db
88+generated_cells:: ds 512
99+1010+SECTION "DungeonCode", ROM0
1111+1212+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+MAX_ROOMS:: dw 512
2121+NEIGHBORS:: db %00111100
2222+2323+; Params:
2424+; Starting Width: B
2525+; Starting Height: C
2626+InitDungeon::
2727+ ld hl, current_width ; current_width = B
2828+ ld [hl], b
2929+ ld hl, current_height ; current_height = C
3030+ ld [hl], c
3131+ ret
3232+3333+GenerateDungeon::
3434+ ; REG_B = i
3535+ ; REG_C = generated_cells_number
3636+ ; REG_D = dungeon_area
3737+ ld hl, current_width
3838+ ld e, [hl]
3939+ ld d, 0
4040+ ld hl, current_height
4141+ ld a, [hl]
4242+ call Mul8
4343+ ld d, l
4444+ ld c, 0
4545+ ld b, 0
4646+.LoopCheck
4747+ ld a, c
4848+ cp a, d
4949+ jp nc, .LoopEnd
5050+ jp z, .LoopEnd
5151+ ld a, b
5252+ cp a, 0
5353+ jp nz, .LoopCheck2
5454+ jp z, .LoopBody
5555+.LoopCheck2
5656+ cp a, c
5757+ jp nc, .LoopEnd
5858+ jp z, .LoopEnd
5959+.LoopBody
6060+ cp a, 0
6161+ jp nz, .LoopBody2
6262+ ld a, c
6363+ cp a, 0
6464+ jp nz, .LoopBody2
6565+ push bc
6666+ call rand
6767+ ld l, b
6868+ ld h, 0
6969+ ld c, d
7070+ inc c
7171+ call Mod8
7272+ ld hl, entrance_id
7373+ ld [hl], a
7474+ pop bc
7575+ ld hl, generated_cells+$0
7676+ ld [hl], a
7777+ ld hl, BIT_ENTRANCE
7878+ ld a, [hl]
7979+ ld hl, BIT_USED_ROOM
8080+ or a, [hl]
8181+ ld hl, dungeon_grid
8282+ push de
8383+ push hl
8484+ ld hl, entrance_id
8585+ ld e, [hl]
8686+ ld d, 0
8787+ pop hl
8888+ add hl, de
8989+ pop de
9090+ ld [hl], a
9191+ ld c, 1
9292+.LoopBody2
9393+ call GenerateRoom
9494+ ld hl, dungeon_grid
9595+ push de
9696+ push hl
9797+ ld hl, generated_cells
9898+ ld e, b
9999+ ld d, 0
100100+ add hl, de
101101+ ld e, [hl]
102102+ ld d, 0
103103+ pop hl
104104+ add hl, de
105105+ pop de
106106+ ld a, [hl]
107107+ push hl
108108+ ld hl, BIT_USED_ROOM
109109+ and a, [hl]
110110+ pop hl
111111+ jp nz, .LoopBody3
112112+ ld a, [hl]
113113+ push hl
114114+ ld hl, BIT_USED_ROOM
115115+ or a, [hl]
116116+ pop hl
117117+ ld [hl], a
118118+.LoopBody3
119119+ ld a, c
120120+ sub a, 1
121121+ ld e, a
122122+ ld a, b
123123+ cp a, e
124124+ jp nz, .LoopBody4
125125+ push bc
126126+ ld l, d
127127+ ld h, 0
128128+ ld c, 4
129129+ call Mod8
130130+ pop bc
131131+ push de
132132+ ld e, l
133133+ ld d, h
134134+ ld a, 3
135135+ call Mul8
136136+ pop de
137137+ ld a, c
138138+ cp a, l
139139+ jp z, .LoopBody4
140140+ jp nc, .LoopBody4
141141+ jp .LoopEnd
142142+.LoopBody4
143143+ inc b
144144+ jp .LoopCheck
145145+.LoopEnd
146146+ ret
147147+148148+GenerateRoom::
149149+ ; REG_B = cell_index_queue
150150+ ; REG_C = queue_size
151151+ ; REG_D = potential_doors
152152+ ; REG_E = door
153153+ ; REG_H = neighbor_room
154154+ ; REG_L = cell_index
155155+ ; generated_cells = cells_queue
156156+ push de
157157+ push bc
158158+ call rand
159159+ ld hl, NEIGHBORS
160160+ ld c, [hl]
161161+ inc c
162162+ ld b, l
163163+ ld h, 0
164164+ call Mod8
165165+ pop bc
166166+ ld d, a
167167+ ld hl, generated_cells
168168+ ld a, l
169169+ add a, b
170170+ ld l, a
171171+ push de
172172+ ld e, [hl]
173173+ ld l, e
174174+ pop de
175175+ ld e, 1
176176+.LoopCheck
177177+ ld a, e
178178+ push hl
179179+ ld hl, NEIGHBORS
180180+ cp a, [hl]
181181+ pop hl
182182+ jp nc, .LoopEnd
183183+ jp nz, .LoopEnd
184184+ ld a, e
185185+ push de
186186+ push hl
187187+ ld hl, NEIGHBORS
188188+ ld e, [hl]
189189+ and a, e
190190+ pop hl
191191+ pop de
192192+ cp a, e
193193+ jp nz, .LoopContinue
194194+ push de
195195+ ld e, l
196196+ ld hl, dungeon_grid
197197+ ld a, l
198198+ add a, e
199199+ ld l, a
200200+ ld a, [hl]
201201+ pop de
202202+ and a, e
203203+ jp nz, .LoopContinue
204204+ call GetNeighborRoomIndex
205205+ ld a, h
206206+ cpl
207207+ cp a, 0
208208+ jp nz, .LoopContinue
209209+ push hl
210210+ push de
211211+ ld e, h
212212+ ld hl, dungeon_grid
213213+ ld a, l
214214+ add a, e
215215+ ld l, a
216216+ pop de
217217+ ld a, [hl]
218218+ ld hl, BIT_USED_ROOM
219219+ and a, [hl]
220220+ pop hl
221221+ jp nz, .LoopContinue
222222+ push bc ; b = opposite_door, cache earlier values
223223+ call GetOppositeDirectionBit
224224+ ld a, e
225225+ and a, d
226226+ cp a, e
227227+ jp nz, .LoopBody2
228228+ push hl
229229+ push de
230230+ ld e, l
231231+ ld hl, dungeon_grid
232232+ ld a, l
233233+ add a, e
234234+ ld l, a
235235+ pop de
236236+ ld a, [hl]
237237+ or a, e
238238+ ld [hl], a
239239+ push de
240240+ ld e, h
241241+ ld hl, dungeon_grid
242242+ ld a, l
243243+ add a, e
244244+ ld l, a
245245+ pop de
246246+ ld a, [hl]
247247+ or a, b
248248+ ld [hl], a
249249+ pop hl
250250+.LoopBody2
251251+ push hl
252252+ push de
253253+ ld e, h
254254+ ld hl, dungeon_grid
255255+ ld a, l
256256+ add a, e
257257+ ld l, a
258258+ pop de
259259+ ld a, [hl]
260260+ pop hl
261261+ cp a, b
262262+ jp nz, .LoopContinue
263263+ pop bc
264264+ push hl
265265+ push de
266266+ ld e, h
267267+ ld hl, generated_cells
268268+ ld a, l
269269+ add a, c
270270+ ld l, a
271271+ ld [hl], e
272272+ pop de
273273+ pop hl
274274+ inc c
275275+.LoopContinue
276276+ rlc e
277277+ jp .LoopCheck
278278+.LoopEnd
279279+ pop de
280280+ ret
281281+282282+GetNeighborRoomIndex::
283283+ ; REG_L = current_room => C
284284+ ; REG_E = direction => B
285285+ push de
286286+ push bc
287287+ push hl
288288+ ld b, e
289289+ ld c, l
290290+ ld a, b
291291+ ld hl, BIT_DOOR_NORTH
292292+ cp a, [hl]
293293+ jp nz, .FuncBody
294294+ ld a, c
295295+ ld hl, current_width
296296+ sub a, [hl]
297297+ ld e, a
298298+ jp .FuncBody5
299299+.FuncBody
300300+ ld hl, BIT_DOOR_EAST
301301+ cp a, [hl]
302302+ jp nz, .FuncBody2
303303+ ld a, c
304304+ inc a
305305+ ld e, a
306306+ jp .FuncBody5
307307+.FuncBody2
308308+ ld hl, BIT_DOOR_SOUTH
309309+ cp a, [hl]
310310+ jp nz, .FuncBody3
311311+ ld a, c
312312+ ld hl, current_width
313313+ add a, [hl]
314314+ ld e, a
315315+ jp .FuncBody5
316316+.FuncBody3
317317+ ld hl, BIT_DOOR_WEST
318318+ cp a, [hl]
319319+ jp nz, .FuncBody4
320320+ ld a, c
321321+ dec a
322322+ ld e, a
323323+ jp .FuncBody5
324324+.FuncBody4
325325+ ld e, -1
326326+.FuncBody5
327327+ ld a, b
328328+ ld hl, BIT_DOOR_NORTH
329329+ cp a, [hl]
330330+ jp nz, .FuncBody6
331331+ ld a, e
332332+ cp a, 0
333333+ jp c, .FuncBody6
334334+ jp .FuncBody10
335335+.FuncBody6
336336+ ld a, b
337337+ ld hl, BIT_DOOR_SOUTH
338338+ cp a, [hl]
339339+ jp nz, .FuncBody7
340340+ push de
341341+ push bc
342342+ ld c, e
343343+ ld hl, current_width
344344+ ld e, [hl]
345345+ ld d, 0
346346+ ld hl, current_height
347347+ ld a, [hl]
348348+ call Mul8
349349+ ld d, l
350350+ ld a, e
351351+ pop bc
352352+ cp a, d
353353+ pop de
354354+ jp nc, .FuncBody7
355355+ jp z, .FuncBody7
356356+ jp .FuncBody10
357357+.FuncBody7
358358+ ld a, b
359359+ ld hl, BIT_DOOR_EAST
360360+ cp a, [hl]
361361+ jp nz, .FuncBody8
362362+ push bc
363363+ ld hl, current_width
364364+ ld c, [hl]
365365+ ld l, e
366366+ ld h, 0
367367+ call Mod8
368368+ pop bc
369369+ cp a, 0
370370+ jp c, .FuncBody8
371371+ jp z, .FuncBody8
372372+ jp .FuncBody10
373373+.FuncBody8
374374+ ld a, b
375375+ ld hl, BIT_DOOR_WEST
376376+ cp a, [hl]
377377+ jp nz, .FuncBody9
378378+ push bc
379379+ ld hl, current_width
380380+ ld c, [hl]
381381+ ld l, e
382382+ ld h, 0
383383+ call Mod8
384384+ ld b, a
385385+ ld hl, current_width
386386+ ld a, [hl]
387387+ dec a
388388+ ld c, a
389389+ ld a, b
390390+ cp a, c
391391+ pop bc
392392+ jp nc, .FuncBody9
393393+ jp z, .FuncBody9
394394+ jp .FuncBody10
395395+.FuncBody9
396396+ ld e, -1
397397+.FuncBody10
398398+ pop hl
399399+ ld h, e
400400+ pop bc
401401+ pop de
402402+ ret
403403+404404+GetOppositeDirectionBit::
405405+ push hl
406406+ ld a, e
407407+ push de
408408+ ld hl, BIT_DOOR_NORTH
409409+ ld e, [hl]
410410+ cp a, e
411411+ jp nz, .FuncBody
412412+ ld hl, BIT_DOOR_SOUTH
413413+ ld b, [hl]
414414+ jp .FuncEnd
415415+.FuncBody
416416+ ld hl, BIT_DOOR_WEST
417417+ ld e, [hl]
418418+ cp a, e
419419+ jp nz, .FuncBody2
420420+ ld hl, BIT_DOOR_EAST
421421+ ld b, [hl]
422422+ jp .FuncEnd
423423+.FuncBody2
424424+ ld hl, BIT_DOOR_SOUTH
425425+ ld e, [hl]
426426+ cp a, e
427427+ jp nz, .FuncBody3
428428+ ld hl, BIT_DOOR_NORTH
429429+ ld b, [hl]
430430+ jp .FuncEnd
431431+.FuncBody3
432432+ ld hl, BIT_DOOR_EAST
433433+ ld e, [hl]
434434+ cp a, e
435435+ jp nz, .FuncEnd
436436+ ld hl, BIT_DOOR_WEST
437437+ ld b, [hl]
438438+.FuncEnd
439439+ pop de
440440+ pop hl
441441+ ret
···11+INCLUDE "hardware.inc/hardware.inc"
22+ rev_Check_hardware_inc 4.0
33+44+SECTION "Header", ROM0[$100]
55+66+ ; This is your ROM's entry point
77+ ; You have 4 bytes of code to do... something
88+ di
99+ jp EntryPoint
1010+1111+ ; Make sure to allocate some space for the header, so no important
1212+ ; code gets put there and later overwritten by RGBFIX.
1313+ ; RGBFIX is designed to operate over a zero-filled header, so make
1414+ ; sure to put zeros regarless of the padding value. (This feature
1515+ ; was introduced in RGBDS 0.4.0, but the -MG etc flags were also
1616+ ; introduced in that version.)
1717+ ds $150 - @, 0
1818+1919+SECTION "Entry point", ROM0
2020+2121+EntryPoint:
2222+ ; Seed random number generator
2323+ ld b, 0
2424+ ld c, 10
2525+ call srand
2626+2727+ ; Initialize Dungeon
2828+ ; ld b, 6
2929+ ; ld c, 6
3030+ ; call InitDungeon
3131+ ; NOTE: Dungeon generation is not verified to work
3232+3333+ jp Done
3434+3535+Done:
3636+ jp Done
···11-/*
22-33- SIMPLE_SPRITE.C
44-55- Tile Source File.
66-77- Info:
88- Form : All tiles as one unit.
99- Format : Gameboy 4 color.
1010- Compression : None.
1111- Counter : None.
1212- Tile size : 8 x 8
1313- Tiles : 0 to 0
1414-1515- Palette colors : None.
1616- SGB Palette : None.
1717- CGB Palette : None.
1818-1919- Convert to metatiles : No.
2020-2121- This file was generated by GBTD v2.2
2222-2323-*/
2424-2525-/* Start of tile array. */
2626-const unsigned char simple_sprite[] =
2727-{
2828- 0x3C,0x3C,0x3C,0x66,0xFF,0xFF,0xFF,0xBD,
2929- 0xFF,0xBD,0xFF,0xFF,0x3C,0x66,0x3C,0x3C
3030-};
3131-3232-/* End of SIMPLE_SPRITE.C */
-35
gb/src/simple_sprite.h
···11-/*
22-33- SIMPLE_SPRITE.H
44-55- Include File.
66-77- Info:
88- Form : All tiles as one unit.
99- Format : Gameboy 4 color.
1010- Compression : None.
1111- Counter : None.
1212- Tile size : 8 x 8
1313- Tiles : 0 to 0
1414-1515- Palette colors : None.
1616- SGB Palette : None.
1717- CGB Palette : None.
1818-1919- Convert to metatiles : No.
2020-2121- This file was generated by GBTD v2.2
2222-2323-*/
2424-2525-#ifndef __simple_sprite_h_INCLUDE
2626-#define __simple_sprite_h_INCLUDE
2727-2828-/* Bank of tiles. */
2929-#define simple_spriteBank 0
3030-/* Start of tile array. */
3131-extern const unsigned char simple_sprite[];
3232-3333-#endif
3434-3535-/* End of SIMPLE_SPRITE.H */
+73
gb/src/utils.asm
···11+SECTION "MathVariables", WRAM0
22+33+randstate:: ds 4
44+55+SECTION "Math", ROM0
66+;; From: https://github.com/pinobatch/libbet/blob/master/src/rand.z80#L34-L54
77+; Generates a pseudorandom 16-bit integer in BC
88+; using the LCG formula from cc65 rand():
99+; x[i + 1] = x[i] * 0x01010101 + 0xB3B3B3B3
1010+; @return A=B=state bits 31-24 (which have the best entropy),
1111+; C=state bits 23-16, HL trashed
1212+rand::
1313+ ; Add 0xB3 then multiply by 0x01010101
1414+ push hl
1515+ push af
1616+ ld hl, randstate+0
1717+ ld a, [hl]
1818+ add a, $B3
1919+ ld [hl+], a
2020+ adc a, [hl]
2121+ ld [hl+], a
2222+ adc a, [hl]
2323+ ld [hl+], a
2424+ ld c, a
2525+ adc a, [hl]
2626+ ld [hl], a
2727+ ld b, a
2828+ pop af
2929+ pop hl
3030+ ret
3131+3232+; Sets the random seed to BC.
3333+; C expects startup code to behave as if srand(1) was called.
3434+; AHL trashed
3535+srand::
3636+ ld hl,randstate+3
3737+ xor a
3838+ ld [hl-],a
3939+ ld [hl-],a
4040+ ld a,b
4141+ ld [hl-],a
4242+ ld [hl],c
4343+ ret
4444+4545+; HL = DE * A
4646+Mul8::
4747+ ld hl, 0
4848+ ld b, 8
4949+.Loop:
5050+ rrca
5151+ jp nc, Mul8.Skip
5252+ add hl, de
5353+.Skip:
5454+ sla e
5555+ rl d
5656+ jp nz, Mul8.Loop
5757+ ret
5858+5959+; A = HL % C
6060+; HL = HL / C
6161+Mod8::
6262+ ld b, 16
6363+.Loop
6464+ xor a
6565+ add hl, hl
6666+ rla
6767+ cp c
6868+ jp c, Mod8.Exit
6969+ inc l
7070+ sub c
7171+ jr nz, Mod8.Loop
7272+.Exit
7373+ ret
-36
gb/src/utils.c
···11-#include "utils.h"
22-#include <stdbool.h>
33-#include <rand.h>
44-#include <stdint.h>
55-66-RollResult roll(unsigned int die_amount, unsigned int die_type, int bonus) {
77- unsigned int sum = 0;
88- bool crit_success = false;
99- bool crit_failure = false;
1010-1111- for (int i = 0; i < die_amount; i++) {
1212- unsigned int val = rand_range(1, die_type);
1313-1414- if (val == 20) {
1515- crit_success = true;
1616- } else if (val == 1) {
1717- crit_failure = true;
1818- }
1919- }
2020-2121- if (bonus > ((int)sum * -1)) {
2222- sum = 0;
2323- crit_success = false;
2424- crit_failure = false;
2525- }
2626-2727- RollResult r;
2828- r.result = sum + bonus;
2929- r.crit_success = crit_success;
3030- r.crit_failure = crit_failure;
3131- return r;
3232-}
3333-3434-unsigned int rand_range(unsigned int min, unsigned int max) {
3535- return rand() % (max - min) + min;
3636-}
-16
gb/src/utils.h
···11-#ifndef UTILS_H
22-#define UTILS_H
33-44-#include <stdbool.h>
55-#include <stdint.h>
66-77-typedef struct _result {
88- unsigned int result;
99- bool crit_success;
1010- bool crit_failure;
1111-} RollResult;
1212-1313-RollResult roll(unsigned int die_amount, unsigned int die_type, int bonus);
1414-unsigned int rand_range(unsigned int min, unsigned int max);
1515-1616-#endif