From 4d1520b47ad06cb114b882e592d1b0947adbbd27 Mon Sep 17 00:00:00 2001 From: cancel Date: Mon, 26 Nov 2018 10:11:50 +0900 Subject: [PATCH] Add basic test of star/bang and E movement --- Makefile | 2 +- mark.h | 28 ++++++++++++++++++++++++++-- sim.c | 55 ++++++++++++++++++++++++++++++++++++++----------------- 3 files changed, 65 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 9bec092..d03e8ea 100644 --- 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 diff --git a/mark.h b/mark.h index e11172f..23ffcf2 100644 --- a/mark.h +++ b/mark.h @@ -7,8 +7,7 @@ typedef enum { 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; @@ -56,3 +55,28 @@ void markmap_poke_relative(Markmap_buffer map, Usz map_height, Usz map_width, 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]; +} diff --git a/sim.c b/sim.c index f94c3bf..b2de700 100644 --- a/sim.c +++ b/sim.c @@ -48,11 +48,10 @@ static inline Glyph glyphs_mod(Glyph a, Glyph b) { 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 @@ static inline void oper_move_relative_or_explode(Glyph* field_buffer, 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_a_phase1(Field* field, Usz y, Usz x) { } } -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_m_phase1(Field* field, Usz y, Usz x) { } } +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,27 +112,34 @@ void orca_run(Field* field, Markmap_buffer markmap) { 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; } } } // 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; } }