From 80248b475b164965dad0c7dfc586186e6b2850b3 Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Tue, 5 Aug 2025 18:26:09 +0200 Subject: [PATCH] chore: added new math helper function to get an absolute distance btween 2 numbers --- src/main.s | 2 +- src/math.s | 27 +++++++++++++++++++++++++++ src/unit_cpu.s | 15 ++++++++++++++- 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 src/math.s diff --git a/src/main.s b/src/main.s index fda10b9..2972f72 100644 --- a/src/main.s +++ b/src/main.s @@ -61,7 +61,7 @@ main: #include "ui.s" #include "audio.s" #include "map.s" - +#include "math.s" #include "roompatterns.s" #include "mapgen.s" diff --git a/src/math.s b/src/math.s new file mode 100644 index 0000000..2d1d4b0 --- /dev/null +++ b/src/math.s @@ -0,0 +1,27 @@ + + ; calculates the distance between 2 + ; numbers: + ; inputs: + ; a: n1 + ; b: n2 + ; returns: + ; a: absolute distance between a and b + ; c: 1 if b > a + ; c: 0 otherwise +distance: + cp a, b + ; if b > a jp + jr c, @b_gt_a REL + + ; otherwise a simple sub will do + sub a, b + ld c, 0 + ret +@b_gt_a: + ; exchange a and b and sub + ld c, a + ld a, b + ld b, c + sub a, b + ld c, 1 + ret diff --git a/src/unit_cpu.s b/src/unit_cpu.s index fda1b99..10a5df8 100644 --- a/src/unit_cpu.s +++ b/src/unit_cpu.s @@ -1,6 +1,20 @@ + ; handles cpu inputs + ; inputs: + ; de: actor + ; returns: + ; bc: next state unit_handle_cpu_inputs: + call distance + call unit_cpu_random_move + ld bc, st_unit_delay_to_active + ret + + + ; moves actor into a random direction + ; inputs: + ; de: actor unit_cpu_random_move: ; pick where to go call roll_d16 @@ -21,6 +35,5 @@ unit_cpu_random_move: cp a, 3 call z, unit_try_move_down - ld bc, st_unit_delay_to_active ret -- 2.30.2