summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-11-06 02:35:05 -0600
committersanine <sanine.not@pm.me>2023-11-06 02:35:05 -0600
commit2e2d9c400335b677297a68883b94bb98526b45ac (patch)
treea76af9947b41c79709c4f228026bd4068b274b29
parent41d720dbd78d91ac85239c8352cd479ec898f9e0 (diff)
implement cell flag setting
-rw-r--r--src/world/cell.js11
-rw-r--r--src/world/cell.test.js59
2 files changed, 60 insertions, 10 deletions
diff --git a/src/world/cell.js b/src/world/cell.js
index 5783d72..6e013c6 100644
--- a/src/world/cell.js
+++ b/src/world/cell.js
@@ -29,13 +29,20 @@ export function cells_apply(cells, proposals) {
(acc, prop) => {
const change = (prop.world_updates || []).reduce(
(acc_, update) => {
- acc_[update.y][update.x].type = update.to;
+ const cell = acc_[update.y][update.x];
+ if (update.to) { cell.type = update.to; }
+ if (update.flags) {
+ cell.flags = cell.flags || {}
+ for (let k of Object.keys(update.flags)) {
+ cell.flags[k] = update.flags[k];
+ }
+ }
return acc_
},
[...acc]
);
return change;
},
- [...cells]
+ [...cells].map(row => row.map(cell => ({ ...cell, flags: {}, })))
);
}
diff --git a/src/world/cell.test.js b/src/world/cell.test.js
index a3749e5..fb901eb 100644
--- a/src/world/cell.test.js
+++ b/src/world/cell.test.js
@@ -4,7 +4,11 @@ import { cells_update, cells_valid, cells_apply } from './cell.js';
test("growth update rule", () => {
- const cells = [[ { type: 'empty' }, { type: 'empty' }, { type: 'plant' } ]];
+ const cells = [[
+ { type: 'empty', flags: {} },
+ { type: 'empty', flags: {} },
+ { type: 'plant', flags: {} },
+ ]];
const update_rules = {
plant: () => {},
empty: (cells, x, y) => {
@@ -31,18 +35,26 @@ test("growth update rule", () => {
test("growth update rule applied", () => {
- const cells = [[ { type: 'empty' }, { type: 'empty' }, { type: 'plant' } ]];
- expect(cells_apply(cells, [{ world_updates:[{ x: 1, y: 0, from: 'empty', to: 'plant' }]}])).toEqual([
- [ { type: 'empty' }, { type: 'plant' }, { type: 'plant' } ],
- ]);
+ const cells = [[
+ { type: 'empty', flags: {} },
+ { type: 'empty', flags: {} },
+ { type: 'plant', flags: {} },
+ ]];
+ expect(cells_apply(cells, [{ world_updates:[{ x: 1, y: 0, from: 'empty', to: 'plant' }]}])).toEqual([[
+ { type: 'empty', flags: {} },
+ { type: 'plant', flags: {} },
+ { type: 'plant', flags: {} },
+ ]]);
expect(cells_apply(cells, [
{ world_updates: [{ x: 2, y: 0, from: 'plant', to: 'empty' } ]},
{ world_updates: [{ x: 1, y: 0, from: 'empty', to: 'plant' } ]},
{ world_updates: [{ x: 0, y: 0, from: 'empty', to: 'plant' } ]},
- ])).toEqual([
- [ { type: 'plant' }, { type: 'plant' }, { type: 'empty' } ],
- ]);
+ ])).toEqual([[
+ { type: 'plant', flags: {} },
+ { type: 'plant', flags: {} },
+ { type: 'empty', flags: {} },
+ ]]);
});
@@ -54,3 +66,34 @@ test("check proposals agains cells for validity", () => {
expect(cells_valid(cells, { world_updates: [{ x: 2, y: 0, from: 'empty', to: 'blah' }] })).toBe(false);
expect(cells_valid(cells, { world_updates: [{ x: 2, y: 1, from: 'empty', to: 'blah' }] })).toBe(false);
});
+
+
+test("proposals update cell flags appropriately", () => {
+ const cells = [
+ [
+ { type: 'empty', flags: { step: 1} },
+ { type: 'empty', flags: {} },
+ { type: 'plant', flags: { foo: 'bar' } },
+ ]
+ ];
+
+ // flags are reset each time step
+ expect(cells_apply(cells, [{ world_updates:[{ x: 1, y: 0, from: 'empty', to: 'plant' }]}])).toEqual([[
+ { type: 'empty', flags: {} },
+ { type: 'plant', flags: {} },
+ { type: 'plant', flags: {} },
+ ]]);
+
+ // flags are combined when updating
+ expect(cells_apply(cells, [
+ { world_updates: [{ x: 1, y: 0, flags: { foo: 'bar' } } ]},
+ { world_updates: [{ x: 1, y: 0, from: 'empty', to: 'plant', flags: { baz: 'baz' } } ]},
+ { world_updates: [{ x: 0, y: 0, from: 'empty', to: 'plant', flags: { foo: 'foo' } } ]},
+ { world_updates: [{ x: 0, y: 0, flags: { beep: 'boop' } } ]},
+ ])).toEqual([[
+ { type: 'plant', flags: { foo: 'foo', beep: 'boop' } },
+ { type: 'plant', flags: { foo: 'bar', baz: 'baz' } },
+ { type: 'plant', flags: {} },
+ ]]);
+
+});