shithub: orca

Download patch

ref: 4d1520b47ad06cb114b882e592d1b0947adbbd27
parent: 096768fc13226996e6c1d7b769ea2a0fe999be26
author: cancel <cancel@cancel.fm>
date: Mon Nov 26 05:11:50 EST 2018

Add basic test of star/bang and E movement

--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-basic_flags := -std=c99 -pipe -Wall -Wpedantic -Wextra -Werror=implicit-function-declaration -Werror=incompatible-pointer-types -Wconversion -D_XOPEN_SOURCE_EXTENDED=1
+basic_flags := -std=c99 -pipe -Wall -Wpedantic -Wextra -Wconversion -Werror=implicit-function-declaration -Werror=incompatible-pointer-types -Werror=int-conversion -D_XOPEN_SOURCE_EXTENDED=1
 debug_flags := -DDEBUG -ggdb
 sanitize_flags := -fsanitize=address -fsanitize=undefined
 # note: -fsanitize=leak not available on at least Mac 10.12
--- a/mark.h
+++ b/mark.h
@@ -7,8 +7,7 @@
   Mark_flag_input = 1 << 1,
   Mark_flag_lock = 1 << 2,
   Mark_flag_output = 1 << 3,
-  Mark_flag_sleep_phase0 = 1 << 4,
-  Mark_flag_sleep_phase1 = 1 << 5,
+  Mark_flag_sleep = 1 << 4,
 } Mark_flags;
 
 typedef U8* Markmap_buffer;
@@ -55,4 +54,29 @@
   if (y0 >= (Isz)map_height || x0 >= (Isz)map_width || y0 < 0 || x0 < 0)
     return;
   map[(Usz)y0 * map_width + (Usz)x0] = (U8)flags;
+}
+
+ORCA_FORCE_INLINE
+void markmap_poke_relative_flags_or(Markmap_buffer map, Usz map_height,
+                                    Usz map_width, Usz y, Usz x, Isz offs_y,
+                                    Isz offs_x, Mark_flags flags) {
+  Isz y0 = (Isz)y + offs_y;
+  Isz x0 = (Isz)x + offs_x;
+  if (y0 >= (Isz)map_height || x0 >= (Isz)map_width || y0 < 0 || x0 < 0)
+    return;
+  map[(Usz)y0 * map_width + (Usz)x0] |= (U8)flags;
+}
+
+ORCA_FORCE_INLINE
+void markmap_poke_flags_or(Markmap_buffer map, Usz map_height, Usz map_width,
+                           Usz y, Usz x, Mark_flags flags) {
+  (void)map_height;
+  map[y * map_width + x] |= (U8)flags;
+}
+
+ORCA_FORCE_INLINE
+Mark_flags markmap_peek(Markmap_buffer map, Usz map_height, Usz map_width,
+                        Usz y, Usz x) {
+  (void)map_height;
+  return map[y * map_width + x];
 }
--- a/sim.c
+++ b/sim.c
@@ -48,11 +48,10 @@
   return indexed_glyphs[ib == 0 ? 0 : (ia % ib)];
 }
 
-static inline void oper_move_relative_or_explode(Glyph* field_buffer,
-                                                 Usz field_height,
-                                                 Usz field_width, Glyph moved,
-                                                 Usz y, Usz x, Isz delta_y,
-                                                 Isz delta_x) {
+static inline void
+oper_move_relative_or_explode(Field_buffer field_buffer, Markmap_buffer markmap,
+                              Usz field_height, Usz field_width, Glyph moved,
+                              Usz y, Usz x, Isz delta_y, Isz delta_x) {
   Isz y0 = (Isz)y + delta_y;
   Isz x0 = (Isz)x + delta_x;
   if (y0 >= (Isz)field_height || x0 >= (Isz)field_width || y0 < 0 || x0 < 0) {
@@ -62,13 +61,19 @@
   Glyph* at_dest = field_buffer + (Usz)y0 * field_width + (Usz)x0;
   if (*at_dest != '.') {
     field_buffer[y * field_width + x] = '*';
+    markmap_poke_flags_or(markmap, field_height, field_width, y, x,
+                          Mark_flag_sleep);
     return;
   }
   *at_dest = moved;
+  markmap_poke_flags_or(markmap, field_height, field_width, (Usz)y0, (Usz)x0,
+                        Mark_flag_sleep);
   field_buffer[y * field_width + x] = '.';
 }
 
-static inline void oper_a_phase1(Field* field, Usz y, Usz x) {
+static inline void oper_phase2_a(Field* field, Markmap_buffer markmap, Usz y,
+                                 Usz x) {
+  (void)markmap;
   Glyph inp0 = field_peek_relative(field, y, x, 0, 1);
   Glyph inp1 = field_peek_relative(field, y, x, 0, 2);
   if (inp0 != '.' && inp1 != '.') {
@@ -77,12 +82,15 @@
   }
 }
 
-static inline void oper_E_phase0(Field* field, Usz y, Usz x) {
-  oper_move_relative_or_explode(field->buffer, field->height, field->width, 'E',
-                                y, x, 0, 1);
+static inline void oper_phase1_E(Field* field, Markmap_buffer markmap, Usz y,
+                                 Usz x) {
+  oper_move_relative_or_explode(field->buffer, markmap, field->height,
+                                field->width, 'E', y, x, 0, 1);
 }
 
-static inline void oper_m_phase1(Field* field, Usz y, Usz x) {
+static inline void oper_phase2_m(Field* field, Markmap_buffer markmap, Usz y,
+                                 Usz x) {
+  (void)markmap;
   Glyph inp0 = field_peek_relative(field, y, x, 0, 1);
   Glyph inp1 = field_peek_relative(field, y, x, 0, 2);
   if (inp0 != '.' && inp1 != '.') {
@@ -91,6 +99,12 @@
   }
 }
 
+static inline void oper_phase1_star(Field* field, Markmap_buffer markmap, Usz y,
+                                    Usz x) {
+  (void)markmap;
+  field_poke(field, y, x, '.');
+}
+
 void orca_run(Field* field, Markmap_buffer markmap) {
   Usz ny = field->height;
   Usz nx = field->width;
@@ -98,12 +112,17 @@
   Glyph* field_buffer = field->buffer;
   // Phase 0
   for (Usz iy = 0; iy < ny; ++iy) {
-    Glyph* row = field_buffer + iy * nx;
+    Glyph* glyph_row = field_buffer + iy * nx;
     for (Usz ix = 0; ix < nx; ++ix) {
-      Glyph c = row[ix];
+      Glyph c = glyph_row[ix];
+      if (markmap_peek(markmap, ny, nx, iy, ix) & Mark_flag_sleep)
+        continue;
       switch (c) {
+      case '*':
+        oper_phase1_star(field, markmap, iy, ix);
+        break;
       case 'E':
-        oper_E_phase0(field, iy, ix);
+        oper_phase1_E(field, markmap, iy, ix);
         break;
       }
     }
@@ -110,15 +129,17 @@
   }
   // Phase 1
   for (Usz iy = 0; iy < ny; ++iy) {
-    Glyph* row = field_buffer + iy * nx;
+    Glyph* glyph_row = field_buffer + iy * nx;
     for (Usz ix = 0; ix < nx; ++ix) {
-      Glyph c = row[ix];
+      if (markmap_peek(markmap, ny, nx, iy, ix) & Mark_flag_sleep)
+        continue;
+      Glyph c = glyph_row[ix];
       switch (c) {
       case 'a':
-        oper_a_phase1(field, iy, ix);
+        oper_phase2_a(field, markmap, iy, ix);
         break;
       case 'm':
-        oper_m_phase1(field, iy, ix);
+        oper_phase2_m(field, markmap, iy, ix);
         break;
       }
     }