unit: Added a roll to initiative.
authorLukas Krickl <lukas@krickl.dev>
Sun, 1 Jun 2025 06:40:15 +0000 (08:40 +0200)
committerLukas Krickl <lukas@krickl.dev>
Sun, 1 Jun 2025 06:40:15 +0000 (08:40 +0200)
RULES.md [new file with mode: 0644]
src/effect.s
src/rand.s
src/unit.s
src/wram.s

diff --git a/RULES.md b/RULES.md
new file mode 100644 (file)
index 0000000..1f85f19
--- /dev/null
+++ b/RULES.md
@@ -0,0 +1,20 @@
+# Gameplay rules
+
+This is the rulebook for all the rpg elements
+
+## Dice
+
+The game rolls 16-sided (d16) dice to decide what happens.
+The outcome of all mechanics is decided by one or more dice rolls. 
+
+## Turns
+
+Each unit can move until all of its moves are used up.
+When all moves are used control is handed over to the next unit.
+
+## Initiative 
+
+When a unit ends its turn the unit with the next highest initiative is picked.
+A unit may fail to take initiative if a d16 roll fails (initiative < d16). In such a case another unit may be chosen.
+
+If all units fail to take initiative or have used all their moves all moves are reset and the process is restarted.
index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..1e7d96db53e94bcf7e41228a3f60db2ef637fd73 100644 (file)
@@ -0,0 +1,22 @@
+  ; updates a unit's status effects
+  ; inputs:
+  ;   de: unit
+effects_unit_update:
+  ret
+
+  ; adds a new effect to a unit
+  ; if no effect slot is available 
+  ; the new effect is dropped
+  ; inputs:
+  ;   de: unit
+  ;    a: effect type
+  ;    b: effect dat
+effect_add:
+  ret
+
+  ; updates a single status effect
+  ; inputs:
+  ;   de: unit
+  ;   hl: effect
+effect_update:
+  ret
index 6cbd3d95e2bfb79e59cc11fbe4fbac058c1d6476..1fb95f1b9355852da9e8e414159509a178dc27e0 100644 (file)
@@ -4,10 +4,12 @@ rand_init:
 
   ; restore seed from 
   ; sram 
-  ld a, [sram_srand] 
+  ld a, [sram_srand]
+  or a, 1
   ld [srand], a
 
   ld a, [sram_srand+1]
+  or a, 1
   ld [srand+1], a
 
   call mbc1_ram_disable
@@ -77,9 +79,16 @@ rand:
   ; returns a number between
   ; 0 and 15
   ; returns:
-  ;   a: number of roll
+  ;  [d16]: last roll
+  ;      a: d16 roll
+  ; preserves registers other than af
 roll_d16:
+  push hl
+
   call rand
   and a, 0x0F
+  ld [d16], a
+
+  pop hl
   ret
 
index f9fab6e4ba3b4f561c71247d41f479f25d315ba2..e909e2cc8476b61d7214277dcb7a0b72897240ed 100644 (file)
@@ -451,8 +451,10 @@ unit_next_request:
 
   ; finds a unit with a higher initiative value 
   ; than the current one that has moves > 0 set
-  ; if all unts have have 0 moves, set moves to 
+  ; if all unts have have 0 moves, set moves to
   ; initial value and try again
+  ; when a unit has higher initiative it can still fail
+  ; to take the turn if it rolls a d16 and the roll is < initiative
   ; skips any ACT_T_NULL types
   ; inputs:
   ;   gameplay_unit_current: unit that requested a change
@@ -542,6 +544,16 @@ unit_next_no_current:
         ; otherwise store new init value and hl
         pop hl
         push hl
+
+        ; this unit is better
+        ; but it can fail an initiative roll
+        push af
+        call roll_d16
+        ld d, a ; d = roll value 
+        pop af ; a = init value
+        cp a, d
+        jr nc, @skip REL ; d < a
+
         ld [unit_next_best_init], a
 
         ld a, l
@@ -689,7 +701,7 @@ unit_demo_2:
 unit_demo_3:
   st_def 0x00, unit_demo_1_init, st_unit_idle
   act_def ACT_T_DEMO_1, 0, 4, 4, 0 
-  act_stat_def 1, 2, 3, 0, 0, 0, 0, 5 
+  act_stat_def 1, 2, 3, 0, 0, 0, 4, 5 
   act_inventory_empty
   act_equipment_empty
   act_effects_empty
index 3940f9bf4b852ef137475f3686299e8a4a8e6f7f..5078049fdd7ebd29dc057ef72e4f02c963268d82 100644 (file)
@@ -57,6 +57,9 @@ state:
   ; seed must never be 0
 srand: .adv 2
 
+  ; last d16 result
+d16: .adv 1
+
 cursor:
 cursor_y: .adv 1
 cursor_x: .adv 1