Browse Source

Add basic test of star/bang and E movement

master
cancel 6 years ago
parent
commit
4d1520b47a
  1. 2
      Makefile
  2. 28
      mark.h
  3. 55
      sim.c

2
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 debug_flags := -DDEBUG -ggdb
sanitize_flags := -fsanitize=address -fsanitize=undefined sanitize_flags := -fsanitize=address -fsanitize=undefined
# note: -fsanitize=leak not available on at least Mac 10.12 # note: -fsanitize=leak not available on at least Mac 10.12

28
mark.h

@ -7,8 +7,7 @@ typedef enum {
Mark_flag_input = 1 << 1, Mark_flag_input = 1 << 1,
Mark_flag_lock = 1 << 2, Mark_flag_lock = 1 << 2,
Mark_flag_output = 1 << 3, Mark_flag_output = 1 << 3,
Mark_flag_sleep_phase0 = 1 << 4, Mark_flag_sleep = 1 << 4,
Mark_flag_sleep_phase1 = 1 << 5,
} Mark_flags; } Mark_flags;
typedef U8* Markmap_buffer; typedef U8* Markmap_buffer;
@ -56,3 +55,28 @@ void markmap_poke_relative(Markmap_buffer map, Usz map_height, Usz map_width,
return; return;
map[(Usz)y0 * map_width + (Usz)x0] = (U8)flags; 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];
}

55
sim.c

@ -48,11 +48,10 @@ static inline Glyph glyphs_mod(Glyph a, Glyph b) {
return indexed_glyphs[ib == 0 ? 0 : (ia % ib)]; return indexed_glyphs[ib == 0 ? 0 : (ia % ib)];
} }
static inline void oper_move_relative_or_explode(Glyph* field_buffer, static inline void
Usz field_height, oper_move_relative_or_explode(Field_buffer field_buffer, Markmap_buffer markmap,
Usz field_width, Glyph moved, Usz field_height, Usz field_width, Glyph moved,
Usz y, Usz x, Isz delta_y, Usz y, Usz x, Isz delta_y, Isz delta_x) {
Isz delta_x) {
Isz y0 = (Isz)y + delta_y; Isz y0 = (Isz)y + delta_y;
Isz x0 = (Isz)x + delta_x; Isz x0 = (Isz)x + delta_x;
if (y0 >= (Isz)field_height || x0 >= (Isz)field_width || y0 < 0 || x0 < 0) { 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; Glyph* at_dest = field_buffer + (Usz)y0 * field_width + (Usz)x0;
if (*at_dest != '.') { if (*at_dest != '.') {
field_buffer[y * field_width + x] = '*'; field_buffer[y * field_width + x] = '*';
markmap_poke_flags_or(markmap, field_height, field_width, y, x,
Mark_flag_sleep);
return; return;
} }
*at_dest = moved; *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] = '.'; 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 inp0 = field_peek_relative(field, y, x, 0, 1);
Glyph inp1 = field_peek_relative(field, y, x, 0, 2); Glyph inp1 = field_peek_relative(field, y, x, 0, 2);
if (inp0 != '.' && inp1 != '.') { 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) { static inline void oper_phase1_E(Field* field, Markmap_buffer markmap, Usz y,
oper_move_relative_or_explode(field->buffer, field->height, field->width, 'E', Usz x) {
y, x, 0, 1); 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 inp0 = field_peek_relative(field, y, x, 0, 1);
Glyph inp1 = field_peek_relative(field, y, x, 0, 2); Glyph inp1 = field_peek_relative(field, y, x, 0, 2);
if (inp0 != '.' && inp1 != '.') { 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) { void orca_run(Field* field, Markmap_buffer markmap) {
Usz ny = field->height; Usz ny = field->height;
Usz nx = field->width; Usz nx = field->width;
@ -98,27 +112,34 @@ void orca_run(Field* field, Markmap_buffer markmap) {
Glyph* field_buffer = field->buffer; Glyph* field_buffer = field->buffer;
// Phase 0 // Phase 0
for (Usz iy = 0; iy < ny; ++iy) { 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) { 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) { switch (c) {
case '*':
oper_phase1_star(field, markmap, iy, ix);
break;
case 'E': case 'E':
oper_E_phase0(field, iy, ix); oper_phase1_E(field, markmap, iy, ix);
break; break;
} }
} }
} }
// Phase 1 // Phase 1
for (Usz iy = 0; iy < ny; ++iy) { 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) { 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) { switch (c) {
case 'a': case 'a':
oper_a_phase1(field, iy, ix); oper_phase2_a(field, markmap, iy, ix);
break; break;
case 'm': case 'm':
oper_m_phase1(field, iy, ix); oper_phase2_m(field, markmap, iy, ix);
break; break;
} }
} }

Loading…
Cancel
Save