cells: cells now have a timer just like states
authorLukas Krickl <lukas@krickl.dev>
Wed, 23 Apr 2025 19:20:29 +0000 (21:20 +0200)
committerLukas Krickl <lukas@krickl.dev>
Wed, 23 Apr 2025 19:20:29 +0000 (21:20 +0200)
This allows easier scheduling e.g. for build timers

src/buildings.s
src/cells.s
src/defs.s
src/macros.inc
src/simulation.s

index 3b479993341e53fb24e92e473c49dd2dbf5e151e..bebaa13f738d92c42f62c56d0ea4404992268e1c 100644 (file)
@@ -23,10 +23,6 @@ build_road:
   ld bc, c_size
   call memcpy 
 
-  call cursor_get_tile
-  ld a, BUILDING_ROAD_NORTH_SOUTH
-  call bg_update_queue_push
-
   ldnull bc
   ret
 
index afffd51d19a21fd0944c8f46bc0601b48948787b..2946925b6fc4bcb763e5ca50e87f8fda90cbd2eb 100644 (file)
@@ -5,17 +5,28 @@
   ;   de: cell ptr
   ;   bc: screen ptr 
 cell_update:
+  push de
+  inc de 
+  inc de
   inc de ; de = flags
   ld a, [de] ; a = head offset
   and a, CF_HEAD
+  pop de
   ; if not head cell return
   ret z
 
   ; save cell ptr
-  dec de ; de is c_type
   push de
-
-  inc de
+  
+  ; check timer 
+  ld a, [de] ; a = timer
+  cp a, 0
+  jr z, @update REL ; if timer is 0 update
+    dec a ; otherwise timer--
+    ld [de], a
+    pop de
+    ret
+@update:
   inc de ; de = c_st_routine 
 
   ld a, [de]
@@ -39,7 +50,6 @@ cell_update:
   or a, c
   ret z 
   
-  inc de
   inc de
   ; de = c_st_routine 
 
@@ -61,8 +71,13 @@ cell_road_update:
   ; updates the tiles 
   ; by pushing tile updates 
 cell_road_init:
+  push bc
+  pop hl ; hl = ptr to tile 
+  ld a, BUILDING_ROAD_NORTH_SOUTH
+  call bg_update_queue_push
+  
   ld bc, cell_road_update
   ret
 
 cell_template_road:
-  c_def C_ROAD, CF_HEAD, cell_road_init
+  c_def 0, cell_road_init, CF_HEAD 
index 8f4d4587618e1658db0804986c1bf60166f39063..4011df40caf60701bb66cc8a958ae6e0534e8f4a 100644 (file)
   ; game state cell struct 
   ; this holds all the information 
   ; a cell on the map has 
+  ; cells are a reduced version of states
+  ; without a default next value and additional flags
 .se 0
-  ; cell type/cell gfx
-.de c_type, 1
-.de c_flags, 1
-  ; if c_st_rotuine 
-  ; is unsued (true for all non-head cells)
-  ; this space can be used by the head cell
-  ; to store data
+  ; cell delay timer only used for head cell
+  ; when it reaches 0 the next state is called
+.de c_time, 1
+  ; type/gfx or custom data for non-head cells
+.de c_type, 0
 .de c_st_data, 0 
   ; this is the same as st routine, 
   ; but tiles do not have a timer or 
   ;   de: cell ptr 
   ;   bc: screen ptr
 .de c_st_routine, 2 
+.de c_flags, 1
+  ; if c_st_rotuine 
+  ; is unsued (true for all non-head cells)
+  ; this space can be used by the head cell
+  ; to store data
 .de c_size, 0
 
   ; cell type enum
 #define CURSOR_MIN_Y 0 
 #define CURSOR_MAX_Y 0xB8
 
-  ; state struct 
+  ; state struct
+  ; states are intended to 
+  ; be used as part of a larger
+  ; struct 
+  ; e.g. player state
 .se 0
   ; time until next state
 .de st_time, 1
index a4f6367e03c7841659620f166dd3759401b2f439..b1fefaa7a5c2ce65f6e6a88a8de024a8b800da36 100644 (file)
 
   ; defines a new cell template
   ; inputs:
-  ;   $1: c_type
-  ;   $3: c_flags
+  ;   $1: c_time
   ;   $2: c_st_routine
+  ;   $3: c_flags
 #macro c_def
   .db $1
-  .db $2
-  dw $3
+  dw $2
+  .db $3
 #endmacro
 
   ; loads NULL into a 16 bit register
index dd2f8a0eaf3ca871ed3283453753321f289d1d0a..857a09aabfc5d0a505afdfad377ee1791ee442e8 100644 (file)
@@ -41,17 +41,16 @@ sim_update:
   ld a, SIM_UPDATE_MAX+1
   push af
 @loop:
-
-    ; advance to next cell
-    .rep i, c_size, 1, inc de
-    inc bc
-    
     push de
     push bc
     call cell_update
     pop bc
     pop de
     pop af
+
+    ; advance to next cell
+    .rep i, c_size, 1, inc de
+    inc bc
     
     ; loop counter--
     dec a