From f995438c0e72e1d08203609d74854d07dbecd661 Mon Sep 17 00:00:00 2001 From: sanine Date: Sat, 11 Nov 2023 15:10:55 -0600 Subject: mutables can be activated by living neighbors --- src/simulation/lattice_rules.js | 14 +++++++++++++- src/simulation/lattice_rules.test.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/simulation/lattice_rules.js b/src/simulation/lattice_rules.js index db027a3..80f3221 100644 --- a/src/simulation/lattice_rules.js +++ b/src/simulation/lattice_rules.js @@ -53,7 +53,19 @@ export const lattice_rules = { } }, - mutable: () => {}, + mutable: (lattice, x, y) => { + const num_active_neighbors = neighbors(lattice, x, y) + .map(([x, y, cell]) => cell.type) + .filter(type => type === 'active') + .length; + if (num_active_neighbors > 0) { + // become living cell + return { world_updates: [{ + x, y, from: 'empty', to: 'active', + flags: { emit: [0, 0, 0, 0, 0, 0, 0, 1] }, + }]}; + } + }, immutable: () => {}, flag: () => {}, diff --git a/src/simulation/lattice_rules.test.js b/src/simulation/lattice_rules.test.js index 4b78116..7648a68 100644 --- a/src/simulation/lattice_rules.test.js +++ b/src/simulation/lattice_rules.test.js @@ -108,3 +108,34 @@ test("beehive", () => { //expect(world_update(world).lattice).toEqual([ expect(apply(world_update, 1, world).lattice).toEqual(lattice); }); + + +test("mutables are activated by neighboring actives", () => { + const L = { type: 'active', 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 M = { type: 'mutable', flags: {} }; + + const lattice = [ + [ _, _, _, _, ], + [ _, L, M, _, ], + [ _, M, M, _, ], + [ _, _, _, _, ], + ]; + + const world = { lattice, lattice_rules, agents: [], senses: [], actions: [], validity: [] }; + + expect(apply(world_update, 1, world).lattice).toEqual([ + [ _, _, _, _, ], + [ _, L, l, _, ], + [ _, l, l, _, ], + [ _, _, _, _, ], + ]); + expect(apply(world_update, 2, world).lattice).toEqual([ + [ _, _, _, _, ], + [ _, L, L, _, ], + [ _, L, L, _, ], + [ _, _, _, _, ], + ]); +}); -- cgit v1.2.1