From: Lukas Krickl Date: Sun, 1 Jun 2025 06:40:15 +0000 (+0200) Subject: unit: Added a roll to initiative. X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=e1cee8bdcbb40672e0d161c887eefad47c42a1ed;p=gbrg%2F.git unit: Added a roll to initiative. --- diff --git a/RULES.md b/RULES.md new file mode 100644 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. diff --git a/src/effect.s b/src/effect.s index e69de29..1e7d96d 100644 --- a/src/effect.s +++ b/src/effect.s @@ -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 diff --git a/src/rand.s b/src/rand.s index 6cbd3d9..1fb95f1 100644 --- a/src/rand.s +++ b/src/rand.s @@ -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 diff --git a/src/unit.s b/src/unit.s index f9fab6e..e909e2c 100644 --- a/src/unit.s +++ b/src/unit.s @@ -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 diff --git a/src/wram.s b/src/wram.s index 3940f9b..5078049 100644 --- a/src/wram.s +++ b/src/wram.s @@ -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