shithub: orca

Download patch

ref: 62810ec4341d5b80a1a34a798dd17f22537c9778
parent: cddd709dc133a4b9c604cdd267c521c27e0bdfa1
author: cancel <cancel@cancel.fm>
date: Tue Dec 18 23:37:05 EST 2018

Clean up, remove unnecessary stuns

--- a/sim.c
+++ b/sim.c
@@ -201,6 +201,18 @@
                    &extra_params->cursor, index, out_vals, out_count);
 }
 
+static void oper_poke_and_stun(Glyph* restrict gbuffer, Mark* restrict mbuffer,
+                               Usz height, Usz width, Usz x, Usz y, Isz delta_y,
+                               Isz delta_x, Glyph g) {
+  Isz y0 = (Isz)y + delta_y;
+  Isz x0 = (Isz)x + delta_x;
+  if (y0 < 0 || x0 < 0 || (Usz)y0 >= height || (Usz)x0 >= width)
+    return;
+  Usz offs = (Usz)y0 * width + (Usz)x0;
+  gbuffer[offs] = g;
+  mbuffer[offs] |= Mark_flag_sleep;
+}
+
 ORCA_FORCE_STATIC_INLINE
 Usz usz_clamp(Usz val, Usz min, Usz max) {
   if (val < min)
@@ -286,6 +298,9 @@
 #define STUN(_delta_y, _delta_x)                                               \
   mbuffer_poke_relative_flags_or(mbuffer, height, width, y, x, _delta_y,       \
                                  _delta_x, Mark_flag_sleep)
+#define POKE_STUNNED(_delta_y, _delta_x, _glyph)                               \
+  oper_poke_and_stun(gbuffer, mbuffer, height, width, y, x, _delta_y,          \
+                     _delta_x, _glyph)
 #define LOCK(_delta_y, _delta_x)                                               \
   mbuffer_poke_relative_flags_or(mbuffer, height, width, y, x, _delta_y,       \
                                  _delta_x, Mark_flag_lock)
@@ -555,7 +570,6 @@
   Glyph g0 = PEEK(0, 1);
   Glyph g1 = PEEK(0, 2);
   POKE(1, 0, g0 == g1 ? '*' : '.');
-  STUN(1, 0);
 END_PHASE
 
 BEGIN_DUAL_PHASE_0(generator)
@@ -567,8 +581,7 @@
 END_PHASE
 BEGIN_DUAL_PHASE_1(generator)
   STOP_IF_NOT_BANGED;
-  POKE(1, 0, PEEK(0, 1));
-  STUN(0, 1);
+  POKE_STUNNED(1, 0, PEEK(0, 1));
 END_PHASE
 
 BEGIN_DUAL_PHASE_0(halt)
@@ -722,7 +735,6 @@
     coords[1] = 1;
   }
   POKE(1, 0, PEEK(coords[0], coords[1]));
-  STUN(1, 0);
 END_PHASE
 
 BEGIN_DUAL_PHASE_0(push)
@@ -866,7 +878,6 @@
     ival[0] = 1;
   }
   POKE(1, 0, PEEK(0, ival[0]));
-  STUN(1, 0);
 END_PHASE
 
 static Isz const uturn_data[] = {
@@ -902,7 +913,6 @@
     switch (g) {
     case MOVEMENT_CASES:
       POKE(dy, dx, (Glyph)uturn_data[i + 2]);
-      STUN(dy, dx);
     }
   }
 END_PHASE
@@ -950,7 +960,6 @@
   if (result == '.')
     return;
   POKE(1, 0, result);
-  STUN(1, 0);
 END_PHASE
 
 BEGIN_DUAL_PHASE_0(teleport)
@@ -976,8 +985,7 @@
     coords[0] = 1;
     coords[1] = 0;
   }
-  POKE(coords[0], coords[1], PEEK(0, 1));
-  STUN(coords[0], coords[1]);
+  POKE_STUNNED(coords[0], coords[1], PEEK(0, 1));
 END_PHASE
 
 //////// Run simulation