map: Added actor table loader
authorLukas Krickl <lukas@krickl.dev>
Fri, 20 Jun 2025 17:26:06 +0000 (19:26 +0200)
committerLukas Krickl <lukas@krickl.dev>
Fri, 20 Jun 2025 17:26:06 +0000 (19:26 +0200)
maps/default_map.s
src/actortables.s [new file with mode: 0644]
src/defs.s
src/main.s
src/map.s
src/mem.s
src/unit.s
src/update.s
tools/tms2map.py

index a4a0a7f021a617c34d70d1d53e800329176572f2..2913765ecef644fca217ee1e6d9f037d01302076 100644 (file)
@@ -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 (file)
index 0000000..51c2261
--- /dev/null
@@ -0,0 +1,4 @@
+default_map_actor_table:
+.db 2 ; size
+dw unit_demo_2
+dw unit_demo_3
index 15a804afd63fec01e64f89c08c2b761410b9b988..815673991cc6ad6fc56cdf9e45da521b361c8e2d 100644 (file)
 .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
   ; 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 
index 8da98ad080fea81e9844153e1ea99a87b179fc1f..ae6ddb20b840e7a8b0d2a582095f8a96bb5cd75f 100644 (file)
@@ -67,6 +67,7 @@ main:
 #include "effect.s"
 #include "text.s"
 #include "stats.s"
+#include "actortables.s"
 
 ; fill bank
 .fill 0, 0x7FFF - $
index b016d42352317e58690a46fac5d968b124b79209..fdf1a8f1daa02f860273ce7892f0c2a25f2475c8 100644 (file)
--- 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
index 475fbaf67a5ab9f7f612e6bca87d8e2b0f0d821f..45d102b2a8eb0bd37c09a488a62d2d86e24f9e1b 100644 (file)
--- 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
   
index 4a47585081e3d8aada750017f9ec8dbc1762c3f8..b837c43163dd4ead5f1f78bd21ff8420e5b7c1c3 100644 (file)
@@ -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
index 5d726dbd7d66d4a0afaefc327f41c061906f5446..2c6f0d221c46c0ef3b8c41374f6c9bc1c1b4e3bc 100644 (file)
@@ -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
 
index 78bbaa26e388f601bbfdbc366c0258a2f3b35fca..5143c54f40220ac8c870abe6f2280e68331f0ce0 100755 (executable)
@@ -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':