summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/simulation/lattice_rules.js13
-rw-r--r--src/simulation/lattice_rules.test.js108
-rw-r--r--src/world/lattice.js16
-rw-r--r--src/world/lattice.test.js61
-rw-r--r--src/world/world.js5
5 files changed, 114 insertions, 89 deletions
diff --git a/src/simulation/lattice_rules.js b/src/simulation/lattice_rules.js
index 0f46d0d..db027a3 100644
--- a/src/simulation/lattice_rules.js
+++ b/src/simulation/lattice_rules.js
@@ -30,7 +30,10 @@ export const lattice_rules = {
.filter(type => type === 'mutable' || type === 'active')
.length;
if (num_active_neighbors === 3) {
- return { world_updates: [{ x, y, from: 'empty', to: 'active' }] };
+ return { world_updates: [{
+ x, y, from: 'empty', to: 'active',
+ flags: { emit: [0, 0, 0, 0, 0, 0, 0, 1] },
+ }]};
}
},
@@ -39,10 +42,14 @@ export const lattice_rules = {
.map(([x, y, cell]) => cell.type)
.filter(type => type === 'mutable' || type === 'active')
.length;
+ const die = { world_updates: [{
+ x, y, from: 'active', to: 'empty',
+ flags: { emit: [0, 0, 0, 0, 0, 0, 0, -1] },
+ }]};
if (num_active_neighbors < 2) {
- return { world_updates: [{ x, y, from: 'active', to: 'empty' }] };
+ return die;
} else if (num_active_neighbors > 3) {
- return { world_updates: [{ x, y, from: 'active', to: 'empty' }] };
+ return die;
}
},
diff --git a/src/simulation/lattice_rules.test.js b/src/simulation/lattice_rules.test.js
index e541add..4b78116 100644
--- a/src/simulation/lattice_rules.test.js
+++ b/src/simulation/lattice_rules.test.js
@@ -13,85 +13,95 @@ function apply(f, n, x0) {
test("blinker", () => {
const L = { type: 'active', flags: {} };
- const D = { type: 'empty', flags: {} };
+ const _ = { type: 'empty', flags: {} };
+ const l = { type: 'active', flags: { emit: [0, 0, 0, 0, 0, 0, 0, 1] } };
+ const d = { type: 'empty', flags: { emit: [0, 0, 0, 0, 0, 0, 0, -1] } };
const lattice = [
- [ D, D, D, D, D ],
- [ D, D, D, D, D ],
- [ D, L, L, L, D ],
- [ D, D, D, D, D ],
- [ D, D, D, D, D ],
+ [ _, _, _, _, _ ],
+ [ _, _, _, _, _ ],
+ [ _, L, L, L, _ ],
+ [ _, _, _, _, _ ],
+ [ _, _, _, _, _ ],
];
const world = { lattice, lattice_rules, agents: [], senses: [], actions: [], validity: [] };
expect(world_update(world).lattice).toEqual([
- [ D, D, D, D, D ],
- [ D, D, L, D, D ],
- [ D, D, L, D, D ],
- [ D, D, L, D, D ],
- [ D, D, D, D, D ],
+ [ _, _, _, _, _ ],
+ [ _, _, l, _, _ ],
+ [ _, d, L, d, _ ],
+ [ _, _, l, _, _ ],
+ [ _, _, _, _, _ ],
+ ]);
+ expect(world_update(world_update(world)).lattice).toEqual([
+ [ _, _, _, _, _ ],
+ [ _, _, d, _, _ ],
+ [ _, l, L, l, _ ],
+ [ _, _, d, _, _ ],
+ [ _, _, _, _, _ ],
]);
- expect(world_update(world_update(world)).lattice).toEqual(lattice);
});
test("glider", () => {
const L = { type: 'active', flags: {} };
- const D = { type: 'empty', flags: {} };
+ const _ = { type: 'empty', flags: {} };
+ const l = { type: 'active', flags: { emit: [0, 0, 0, 0, 0, 0, 0, 1] } };
+ const d = { type: 'empty', flags: { emit: [0, 0, 0, 0, 0, 0, 0, -1] } };
const lattice = [
- [ D, D, D, D, D, D ],
- [ D, D, D, L, D, D ],
- [ D, L, D, L, D, D ],
- [ D, D, L, L, D, D ],
- [ D, D, D, D, D, D ],
- [ D, D, D, D, D, D ],
+ [ _, _, _, _, _, _ ],
+ [ _, _, _, L, _, _ ],
+ [ _, L, _, L, _, _ ],
+ [ _, _, L, L, _, _ ],
+ [ _, _, _, _, _, _ ],
+ [ _, _, _, _, _, _ ],
];
const world = { lattice, lattice_rules, agents: [], senses: [], actions: [], validity: [] };
//expect(world_update(world).lattice).toEqual([
expect(apply(world_update, 1, world).lattice).toEqual([
- [ D, D, D, D, D, D ],
- [ D, D, L, D, D, D ],
- [ D, D, D, L, L, D ],
- [ D, D, L, L, D, D ],
- [ D, D, D, D, D, D ],
- [ D, D, D, D, D, D ],
+ [ _, _, _, _, _, _ ],
+ [ _, _, l, d, _, _ ],
+ [ _, d, _, L, l, _ ],
+ [ _, _, L, L, _, _ ],
+ [ _, _, _, _, _, _ ],
+ [ _, _, _, _, _, _ ],
]);
expect(apply(world_update, 2, world).lattice).toEqual([
- [ D, D, D, D, D, D ],
- [ D, D, D, L, D, D ],
- [ D, D, D, D, L, D ],
- [ D, D, L, L, L, D ],
- [ D, D, D, D, D, D ],
- [ D, D, D, D, D, D ],
+ [ _, _, _, _, _, _ ],
+ [ _, _, d, l, _, _ ],
+ [ _, _, _, d, L, _ ],
+ [ _, _, L, L, l, _ ],
+ [ _, _, _, _, _, _ ],
+ [ _, _, _, _, _, _ ],
]);
expect(apply(world_update, 3, world).lattice).toEqual([
- [ D, D, D, D, D, D ],
- [ D, D, D, D, D, D ],
- [ D, D, L, D, L, D ],
- [ D, D, D, L, L, D ],
- [ D, D, D, L, D, D ],
- [ D, D, D, D, D, D ],
+ [ _, _, _, _, _, _ ],
+ [ _, _, _, d, _, _ ],
+ [ _, _, l, _, L, _ ],
+ [ _, _, d, L, L, _ ],
+ [ _, _, _, l, _, _ ],
+ [ _, _, _, _, _, _ ],
]);
expect(apply(world_update, 4, world).lattice).toEqual([
- [ D, D, D, D, D, D ],
- [ D, D, D, D, D, D ],
- [ D, D, D, D, L, D ],
- [ D, D, L, D, L, D ],
- [ D, D, D, L, L, D ],
- [ D, D, D, D, D, D ],
+ [ _, _, _, _, _, _ ],
+ [ _, _, _, _, _, _ ],
+ [ _, _, d, _, L, _ ],
+ [ _, _, l, d, L, _ ],
+ [ _, _, _, L, l, _ ],
+ [ _, _, _, _, _, _ ],
]);
});
test("beehive", () => {
const L = { type: 'active', flags: {} };
- const D = { type: 'empty', flags: {} };
+ const _ = { type: 'empty', flags: {} };
const lattice = [
- [ D, D, D, D, D, D ],
- [ D, D, L, L, D, D ],
- [ D, L, D, D, L, D ],
- [ D, D, L, L, D, D ],
- [ D, D, D, D, D, D ],
+ [ _, _, _, _, _, _ ],
+ [ _, _, L, L, _, _ ],
+ [ _, L, _, _, L, _ ],
+ [ _, _, L, L, _, _ ],
+ [ _, _, _, _, _, _ ],
];
const world = { lattice, lattice_rules, agents: [], senses: [], actions: [], validity: [] };
diff --git a/src/world/lattice.js b/src/world/lattice.js
index 243a47d..066a5ca 100644
--- a/src/world/lattice.js
+++ b/src/world/lattice.js
@@ -28,18 +28,19 @@ export function lattice_valid(lattice, proposal) {
// apply a set of proposals, returning the new lattice
export function lattice_apply(lattice, proposals) {
- return proposals.reduce(
+ const result = proposals.reduce(
(acc, prop) => {
const change = (prop.world_updates || []).reduce(
(acc_, update) => {
const cell = acc_[update.y][update.x];
if (update.to) { cell.type = update.to; }
if (update.flags) {
- cell.flags = cell.flags || {}
- // this is very side-effect-y but i couldn't think of a nicer compatible way of doing it 😔
- for (let k of Object.keys(update.flags)) {
- cell.flags[k] = update.flags[k];
- }
+ cell.flags = { ...(cell.flags || {}), ...update.flags };
+ //cell.flags = cell.flags || {}
+ //// this is very side-effect-y but i couldn't think of a nicer compatible way of doing it 😔
+ //for (let k of Object.keys(update.flags)) {
+ // cell.flags[k] = update.flags[k];
+ //}
}
return acc_
},
@@ -47,6 +48,7 @@ export function lattice_apply(lattice, proposals) {
);
return change;
},
- [...lattice].map(row => row.map(cell => ({ ...cell, flags: {}, })))
+ [...lattice]
);
+ return result;
}
diff --git a/src/world/lattice.test.js b/src/world/lattice.test.js
index c2fdb6b..d1bdd13 100644
--- a/src/world/lattice.test.js
+++ b/src/world/lattice.test.js
@@ -77,32 +77,35 @@ test("check proposals agains lattice for validity", () => {
});
-test("proposals update cell flags appropriately", () => {
- const lattice = [
- [
- { type: 'empty', flags: { step: 1} },
- { type: 'empty', flags: {} },
- { type: 'plant', flags: { foo: 'bar' } },
- ]
- ];
-
- // flags are reset each time step
- expect(lattice_apply(lattice, [{ 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(lattice_apply(lattice, [
- { 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: {} },
- ]]);
-
-});
+// this test is no longer relevant because resetting the cell flags is taken care of by world_update,
+// not lattice_apply
+//
+//test("proposals update cell flags appropriately", () => {
+// const lattice = [
+// [
+// { type: 'empty', flags: { step: 1} },
+// { type: 'empty', flags: {} },
+// { type: 'plant', flags: { foo: 'bar' } },
+// ]
+// ];
+//
+// // flags are reset each time step
+// expect(lattice_apply(lattice, [{ 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(lattice_apply(lattice, [
+// { 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: {} },
+// ]]);
+//
+//});
diff --git a/src/world/world.js b/src/world/world.js
index 4bab4d3..63d974a 100644
--- a/src/world/world.js
+++ b/src/world/world.js
@@ -16,7 +16,10 @@ import { proposal_merge } from './proposal.js';
export function world_update(world, postprocess=[]) {
const lattice_props = lattice_update(world.lattice, world.lattice_rules);
- const intermediate_lattice = lattice_apply(world.lattice, lattice_props);
+ const intermediate_lattice = lattice_apply(
+ world.lattice.map(row => row.map(cell => ({ ...cell, flags: {} }))),
+ lattice_props
+ );
const decisions = world.agents
.map(a => agent_decide(world, a, world.senses, world.actions))