From d1bbe5d3abb438dc41cccdade58a355beb528987 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Mon, 30 Dec 2024 15:16:29 +0100 Subject: [PATCH] map: Added basic structure and docs for mapgen This implementation is not yet functional, but the basic idea is taking shape :^) --- src/map.s | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/mem.s | 4 +++ src/wram.s | 10 +++++- 3 files changed, 102 insertions(+), 3 deletions(-) diff --git a/src/map.s b/src/map.s index 2196059..327f75f 100644 --- a/src/map.s +++ b/src/map.s @@ -470,19 +470,96 @@ room_get_flag_masked: ; sets the current room ; sets player position map_generate: + ; clear map0 + ld d, 0 + ld hl, map0 + ld bc, map0_end - map0 + call memset + + ; basic mapgen setup + xor a, a + ld [mapgen_depth], a + ld d, ROOMS_TOTAL ; d = loop counter ld hl, map0 ; hl = current room - ; TODO: this is placeholder mapgen + + ; move to map_gen_next from here + ; generate the next room + ; inputs: + ; hl: current room +map_gen_next: + ; depth ++ + ld a, [mapgen_depth] + inc a + ld [mapgen_depth], a + + ; select a room + ; and copy it to the current position + ; linking back NULL on all exits initially + push hl + + ; load base room ptr + ; TODO we are simply loading base_room_header every time + ld hl, base_room_table + ld a, [hl+] + ld e, a + ld a, [hl] + ld d, a + pop hl ; hl = room dst + ; set default exit ptr to 0000 + ; to indicate it is empty + xor a, a + ld b, a + ld c, a + + ; now we copy + push hl ; save original ptr for now + call map_gen_copy_base_room + pop hl + + ; select a direction to walk into and call again + ; and set up pointers + ; if direction is already taken try again + ; if direction is special return to previous room unless depth is 1 + ; if all directions are taken, return + ; otherwise set up next ptr and move there + ; update door tiles in picked direction + ; push map ptr to stack before calling again + + ; depth -- + ld a, [mapgen_depth] + dec a + ld [mapgen_depth], a ret ; copies a base room ; inputs: - ; hl: target ; de: base room + ; hl: target ; bc: default exit room ptr map_gen_copy_base_room: + push bc + + ; this routine copies a base room + ; and fixes the header accordingly + + ; copy header + ld bc, room_size + call memcpy + + ; copy tiles + ld bc, ROOM_TILES_SIZE + call memcpy + + ; copy flags + + ; copy actors + + ; set exits + pop bc ; restore default exit value + ret ; base room @@ -491,6 +568,16 @@ map_gen_copy_base_room: #include "./maps/base_room.s" #include "./maps/base_room2.s" + ; a table of all base room ptrs + ; length must be divisible by 2 +base_room_table: + dw base_room_header + dw base_room2_header +base_room_table_end: + + ; count of all pointers in base room table +#define BASE_ROOM_TABLE_LEN ((base_room_table_end - base_room_table) / 2) + ; exit table for empty exits room_empty_exits: dw base_room2_header diff --git a/src/mem.s b/src/mem.s index a825b54..09c90c1 100644 --- a/src/mem.s +++ b/src/mem.s @@ -30,6 +30,10 @@ mem_init: ; de: source ; hl: destination ; bc: length +; returns: +; hl: hl+length +; de: de+length +; bc: 0 memcpy: @loop: ld a, [de] diff --git a/src/wram.s b/src/wram.s index c8b7b1b..cb6e064 100644 --- a/src/wram.s +++ b/src/wram.s @@ -165,7 +165,9 @@ floor: .adv 1 ; version of save game .de save_game_version, 1 ; saving rng seed -.de save_game_seed, 1 +.de save_game_srand, 1 + ; saving floor number +.de save_game_floor, 1 ; saving all room headers ; it is important to ensure ; that the rooms are loaded back in their intended memory space @@ -264,3 +266,9 @@ anim_target_x: .adv 1 ct_poy: .adv 1 ct_pox: .adv 1 ct_mask: .adv 1 + + ; mapgen tmp values + + ; current depth + ; used for recursion in mapgen +mapgen_depth: .adv 1 -- 2.30.2