Browse Source

Add basic (incorrect) 'E' implementation

master
cancel 6 years ago
parent
commit
5d5828d79e
  1. 2
      mark.h
  2. 77
      sim.c

2
mark.h

@ -7,6 +7,8 @@ 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_phase1 = 1 << 4,
} Mark_flags; } Mark_flags;
typedef struct { typedef struct {

77
sim.c

@ -1,6 +1,9 @@
#include "field.h" #include "field.h"
#include "mark.h"
#include "sim.h" #include "sim.h"
#define OPER_INLINE static inline
static Glyph const indexed_glyphs[] = { static Glyph const indexed_glyphs[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
@ -45,40 +48,80 @@ 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 act_a(Field* f, Usz y, Usz x) { static inline void oper_move_relative_or_explode(Glyph* field_buffer,
Glyph inp0 = field_peek_relative(f, y, x, 0, 1); Usz field_height,
Glyph inp1 = field_peek_relative(f, y, x, 0, 2); 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) {
field_buffer[y * field_width + x] = '*';
return;
}
Glyph* at_dest = field_buffer + (Usz)y0 * field_width + (Usz)x0;
if (*at_dest != '.') {
field_buffer[y * field_width + x] = '*';
return;
}
*at_dest = moved;
field_buffer[y * field_width + x] = '.';
}
static inline void oper_a_phase1(Field* field, Usz y, Usz x) {
Glyph inp0 = field_peek_relative(field, y, x, 0, 1);
Glyph inp1 = field_peek_relative(field, y, x, 0, 2);
if (inp0 != '.' && inp1 != '.') { if (inp0 != '.' && inp1 != '.') {
Glyph t = glyphs_sum(inp0, inp1); Glyph g = glyphs_sum(inp0, inp1);
field_poke_relative(f, y, x, 1, 0, t); field_poke_relative(field, y, x, 1, 0, g);
} }
} }
static inline void act_m(Field* f, Usz y, Usz x) { static inline void oper_E_phase0(Field* field, Usz y, Usz x) {
Glyph inp0 = field_peek_relative(f, y, x, 0, 1); oper_move_relative_or_explode(field->buffer, field->height, field->width, 'E',
Glyph inp1 = field_peek_relative(f, y, x, 0, 2); y, x, 0, 1);
}
static inline void oper_m_phase1(Field* field, Usz y, Usz x) {
Glyph inp0 = field_peek_relative(field, y, x, 0, 1);
Glyph inp1 = field_peek_relative(field, y, x, 0, 2);
if (inp0 != '.' && inp1 != '.') { if (inp0 != '.' && inp1 != '.') {
Glyph t = glyphs_mod(inp0, inp1); Glyph g = glyphs_mod(inp0, inp1);
field_poke_relative(f, y, x, 1, 0, t); field_poke_relative(field, y, x, 1, 0, g);
} }
} }
void orca_run(Field* f) { void orca_run(Field* field) {
Usz ny = f->height; Usz ny = field->height;
Usz nx = f->width; Usz nx = field->width;
Glyph* f_buffer = f->buffer; Glyph* field_buffer = field->buffer;
// Phase 0
for (Usz iy = 0; iy < ny; ++iy) {
Glyph* row = field_buffer + iy * nx;
for (Usz ix = 0; ix < nx; ++ix) {
Glyph c = row[ix];
switch (c) {
case 'E':
oper_E_phase0(field, iy, ix);
break;
}
}
}
// Phase 1
for (Usz iy = 0; iy < ny; ++iy) { for (Usz iy = 0; iy < ny; ++iy) {
Glyph* row = f_buffer + iy * nx; 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 = row[ix];
switch (c) { switch (c) {
case 'a': case 'a':
act_a(f, iy, ix); oper_a_phase1(field, iy, ix);
break; break;
case 'm': case 'm':
act_m(f, iy, ix); oper_m_phase1(field, iy, ix);
break; break;
} }
} }
} }
} }
#undef OPER_INLINE

Loading…
Cancel
Save