From: Lukas Krickl Date: Fri, 20 Jun 2025 17:26:06 +0000 (+0200) Subject: map: Added actor table loader X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=4cf47a90acd47e49574b247eebcb4c79ed614783;p=gbrg%2F.git map: Added actor table loader --- diff --git a/maps/default_map.s b/maps/default_map.s index a4a0a7f..2913765 100644 --- a/maps/default_map.s +++ b/maps/default_map.s @@ -2,7 +2,7 @@ default_map_header: .db 0, 0, 0, 0 ; flags dw default_map_bg dw st_map_null -dw map_actor_table_null +dw default_map_actor_table dw bank8000 dw bank8800 dw bank9000 diff --git a/src/actortables.s b/src/actortables.s new file mode 100644 index 0000000..51c2261 --- /dev/null +++ b/src/actortables.s @@ -0,0 +1,4 @@ +default_map_actor_table: +.db 2 ; size +dw unit_demo_2 +dw unit_demo_3 diff --git a/src/defs.s b/src/defs.s index 15a804a..8156739 100644 --- a/src/defs.s +++ b/src/defs.s @@ -108,7 +108,7 @@ .de ACT_T_CURSOR, 1 .de ACT_T_DEMO_1, 1 - ; actor struct + ; actor struct (unit's are instances of actors) ; actor structs are basically just states ; to define an actor the following macros must be used in order ; st_def @@ -232,11 +232,18 @@ ; maps to map property actor_table_ptr .de map_actor_table_ptr, 2 ; pointers to tile banks to be loaded - ; maps to map property bank0, bank1, bank2 + ; maps to map property tile_bank0, tile_bank1, tile_bank2 .de map_tile_bank0_ptr, 2 .de map_tile_bank1_ptr, 2 .de map_tile_bank2_ptr, 2 + + ; map actor table struct +.se 0 +.de map_actor_table_len, 1 + ; list of be pointers to actors +.de map_actor_table_act_ptrs, 0 + ; special text commands ; consumes the command diff --git a/src/main.s b/src/main.s index 8da98ad..ae6ddb2 100644 --- a/src/main.s +++ b/src/main.s @@ -67,6 +67,7 @@ main: #include "effect.s" #include "text.s" #include "stats.s" +#include "actortables.s" ; fill bank .fill 0, 0x7FFF - $ diff --git a/src/map.s b/src/map.s index b016d42..fdf1a8f 100644 --- a/src/map.s +++ b/src/map.s @@ -2,9 +2,24 @@ ; initial map setup map_init: ld hl, default_map_header - call tile_map_load - call cells_draw_all + ; loads a new map + ; inputs: + ; hl: map ptr +map_load: + push hl + call map_tiles_load + + pop hl + push hl + call map_state_load + pop hl + + push hl + call map_actors_load + pop hl + + call map_draw_all ret @@ -45,10 +60,10 @@ map_get_tile: ret - ; loads a map + ; loads map tiles ; inputs: ; hl: map header -tile_map_load: +map_tiles_load: ; load bg ptr ld de, map_bg_ptr add hl, de ; hl = bg_ptr @@ -106,10 +121,101 @@ tile_map_bg_load: jr nz, @loop REL ret + + ; loads a map's state + ; inputs: + ; hl: map ptr +map_state_load: + ld de, map_state_ptr + add hl, de ; hl = state ptr ptr + + ld a, [hl+] + ld e, a + ld a, [hl] + ld d, a + ; de = state ptr + ld hl, map_st + ld bc, st_size + call memcpy + + ret + + ; loads a map's actor table + ; starting at p0_actors+1 (0 is reserved for player) + ; inputs: + ; hl: map ptr +map_actors_load: + ; first clear actor table except player + push hl + ld hl, p0_units + act_size + ld bc, act_size * (UNITS_MAX - 1) + ld d, 0 + call memset + pop hl + + ld de, map_actor_table_ptr + add hl, de ; hl = actor table ptr + + ld a, [hl+] + ld e, a + ld a, [hl] + ld d, a + ; de = actor table + + ; load actor count + ld a, [de] + inc de ; de = first actor ptr + + ; load starting at slot 1 + ; hl = dst + ld hl, p0_units + act_size + @loop: + ; load next actor + push af + push hl + + ; load ptr + ld a, [de] + ld b, a + inc de + ld a, [de] + inc de + ; save de for now + ; de = next ptr + push de + + ; set src ptr + ld e, b + ld d, a + ; de = str ptr + ; hl = dst already + ld bc, act_size + call memcpy + + + ; copy + + pop de + pop hl + push de + ; move to next actor destination + ld de, act_size + add hl, de + pop de + + pop af + ; i-- + dec a + jr nz, @loop REL + + ; hand over control to a unit + ld de, p_empty_unit + call unit_next_request + ret ; draws all cells currently loaded to the screen ; only call during blank! -cells_draw_all: +map_draw_all: ; de = loop counter ld de, MAP_SIZE diff --git a/src/mem.s b/src/mem.s index 475fbaf..45d102b 100644 --- a/src/mem.s +++ b/src/mem.s @@ -15,7 +15,7 @@ mem_init: ; set up game mode - call map_load_demo_actors + call unit_load_default_player call game_init call mbc1_init diff --git a/src/unit.s b/src/unit.s index 4a47585..b837c43 100644 --- a/src/unit.s +++ b/src/unit.s @@ -791,6 +791,18 @@ unit_get_inventory: unit_get_equipment: ret + ; loads the default player unit + ; and hands over control +unit_load_default_player: + ld de, unit_player + ld hl, p0_units + ld bc, act_size + call memcpy + + ld de, p_empty_unit + call unit_next_request + ret + unit_player: st_def 0x00, unit_player_init, st_unit_idle act_def ACT_T_DEMO_1, 0, 2, 2, 0 diff --git a/src/update.s b/src/update.s index 5d726db..2c6f0d2 100644 --- a/src/update.s +++ b/src/update.s @@ -15,6 +15,10 @@ update_game: ; clear oam call shadow_oam_clear + ; update map state + ld de, map_st + call st_update + ld hl, p0_units call units_update diff --git a/tools/tms2map.py b/tools/tms2map.py index 78bbaa2..5143c54 100755 --- a/tools/tms2map.py +++ b/tools/tms2map.py @@ -80,7 +80,12 @@ def get_flags(tileset): get_flag(child) def get_map_props(root): - global state_ptr, actor_table_ptr, tile_bank0, tile_bank1, tile_bank2 + global state_ptr + global actor_table_ptr + global tile_bank0 + global tile_bank1 + global tile_bank2 + for child in root: if child.tag == "properties": for prop in child: @@ -89,7 +94,7 @@ def get_map_props(root): if name == 'state_ptr': state_ptr = value elif name == 'actor_table_ptr': - actor_table_ptr = value + actor_table_ptr = value elif name == 'tile_bank0': tile_bank0 = value elif name == 'tile_bank1':