chore: added new math helper function to get an absolute distance btween 2 numbers
authorLukas Krickl <lukas@krickl.dev>
Tue, 5 Aug 2025 16:26:09 +0000 (18:26 +0200)
committerLukas Krickl <lukas@krickl.dev>
Tue, 5 Aug 2025 16:26:09 +0000 (18:26 +0200)
src/main.s
src/math.s [new file with mode: 0644]
src/unit_cpu.s

index fda10b9af0bac287c134ce62d4aec6569b477419..2972f722dd933eba4136ddecbf4881b6ba435bf3 100644 (file)
@@ -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 (file)
index 0000000..2d1d4b0
--- /dev/null
@@ -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
index fda1b99c1941a367e00d0c367a18ad282d28dc77..10a5df869d9e5a1332cd6e7b5782b79da6993230 100644 (file)
@@ -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