Index: apps/plugins/rocklife.c =================================================================== --- apps/plugins/rocklife.c (revision 27759) +++ apps/plugins/rocklife.c (working copy) @@ -134,43 +134,175 @@ init_grid(pgrid); - char c; - int nc, x, y, xmid, ymid; + char c, ext_c, header[30], *ext_s; + char *marker; + int nc, x, y, cnt, xmid, ymid, w_off, h_off; bool comment; + cnt = 0; x=0; y=0; xmid = (GRID_W>>1) - 2; ymid = (GRID_H>>1) - 2; comment = false; + ext_s = rb->strrchr(file, '.'); /* Get the file extension */ + if (rb->strcmp(ext_s, ".cells") == 0) + ext_c = 'a'; + if (rb->strcmp(ext_s, ".l") == 0) + ext_c = 'b'; + switch(ext_c) { /* handle different file types */ + case 'a': + while (true) { + nc = rb->read(fd, &c, 1); + if (nc <= 0) + break; - while (true) { + switch(c) { + case '!': + comment = true; + break; + case '.': + if (!comment) + x++; + break; + case 'O': + if (!comment) { + if (is_valid_cell(xmid + x, ymid + y)) + set_cell(xmid + x, ymid + y, pgrid); + x++; + } + break; + case '\n': + y++; + x=0; + comment = false; + break; + default: + break; + } + } + + case 'b': /* These come in two varieties */ nc = rb->read(fd, &c, 1); - if (nc <= 0) - break; - - switch(c) { - case '!': - comment = true; - case '.': - if (!comment) - x++; - break; - case 'O': - if (!comment) { - if (is_valid_cell(xmid + x, ymid + y)) - set_cell(xmid + x, ymid + y, pgrid); - x++; + if (c == 'x') { /* test for RBE type of file */ + rb->splash(HZ, "Loaded RBE"); + int i; + i = 0; + while (c != '\n') { + nc = rb->read(fd, &c, 1); + if (nc <= 0) + break; + header[i] = c; + i++; } - break; - case '\n': - y++; - x=0; - comment = false; - break; - default: - break; + w_off = rb->atoi(rb->strtok_r(header, " ,=y\n", &marker)) + / 2; + h_off = rb->atoi(rb->strtok_r(NULL, " ,=y\n", &marker)) / 2; + + /* The header may also contain parameters for setting + * new rule sets. TODO: Implement engine with fillable + * parameters to allow more general automata and parse + * extra tokens here. */ + + while(true) { + nc = rb->read(fd, &c, 1); + if (nc <= 0) + break; + if (c == '!') + break; + else if (c >= 48 && c <= 57) /* c is a number */ + cnt = cnt * 10 + (int) c - 48; + else { + switch(c) { /* parse body chars */ + case 'b': + x++; + while (cnt > 1) { + cnt--; + x++; + } + cnt = 0; + break; + case 'o': + if (is_valid_cell(xmid - w_off + x, ymid + - h_off + y)) + set_cell(xmid - w_off + x, ymid + - h_off + y, pgrid); + x++; + while (cnt > 1) { + cnt--; + if (is_valid_cell(xmid - w_off + x, ymid + - h_off + y)) + set_cell(xmid - w_off + x, ymid + - h_off + y, pgrid); + x++; + } + cnt = 0; + break; + case '$': + y++; + x = 0; + break; + default: + break; + } + } + } } - } + + else if (c == '!') { /* LIF 1.05 type */ + char lastchar; + lastchar = ' '; + rb->splash(HZ, "Loaded LIF type"); + while (true) { + nc = rb->read(fd, &c, 1); + if (nc <= 0) + break; + if (c == '\n' && lastchar == '!') + comment = false; + lastchar = c; + if (c >= 48 && c <= 57) /* c is a number */ + cnt = cnt * 10 + (int) c - 48; + else { + switch(c) { + case '!': + comment = true; + break; + case '.': + if (!comment){ + x++; + while (cnt > 1) { + cnt--; + x++; + } + cnt = 0; + } + break; + case 'O': + if (!comment) { + if (is_valid_cell(xmid + x, ymid + y)) + set_cell(xmid + x, ymid + y, pgrid); + x++; + while (cnt > 1) { + cnt--; + if (is_valid_cell(xmid + x, ymid + y)) + set_cell(xmid + x, ymid + y, pgrid); + x++; + } + cnt = 0; + } + break; + case '\n': + if (!comment) { + y++; + x=0; + } + break; + default: + break; + } + } + } + } + } rb->close(fd); return true; } Index: apps/plugins/viewers.config =================================================================== --- apps/plugins/viewers.config (revision 27759) +++ apps/plugins/viewers.config (working copy) @@ -59,6 +59,7 @@ colours,apps/text_editor,11 ssg,games/superdom,- cells,games/rocklife,- +l,games/rocklife,- link,viewers/shortcuts_view,- *,viewers/shortcuts_append,- *,apps/md5sum,-