diff options
author | sanine <sanine.not@pm.me> | 2023-11-09 16:46:33 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-11-09 16:46:33 -0600 |
commit | 55b714abf83e01aa0ff513ad6ba4978f4b4da6cd (patch) | |
tree | 71518c2a6546681724f095424b755c2ab1ec8ecd /src/world | |
parent | 819d8a51c5ba8b1bec06163dba9c3e0212f1120a (diff) |
add basic game of life lattice rules
Diffstat (limited to 'src/world')
-rw-r--r-- | src/world/lattice.test.js | 9 | ||||
-rw-r--r-- | src/world/world.js | 19 |
2 files changed, 24 insertions, 4 deletions
diff --git a/src/world/lattice.test.js b/src/world/lattice.test.js index 7c71d04..c2fdb6b 100644 --- a/src/world/lattice.test.js +++ b/src/world/lattice.test.js @@ -34,6 +34,15 @@ test("growth update rule", () => { }); +//test("agents cannot move into non-empty tiles", () => { +// const lattice = [[ {type: 'empty', flags: {}}, {type: 'filled', flags: {}} ]]; +// const bad_prop = [{ agent_updates: [{ agent_id: 14, x: 1, y: 0 }] }]; +// expect(lattice_valid(lattice, bad_prop)).toBe(false); +// const good_prop = [{ agent_updates: [{ agent_id: 14, x: 0, y: 0 }] }]; +// expect(lattice_valid(lattice, bad_prop)).toBe(true); +//}); + + test("growth update rule applied", () => { const lattice = [[ { type: 'empty', flags: {} }, diff --git a/src/world/world.js b/src/world/world.js index c824e91..87d0aed 100644 --- a/src/world/world.js +++ b/src/world/world.js @@ -1,19 +1,30 @@ +import { lattice_update, lattice_valid, lattice_apply } from './lattice.js'; +import { agent_decide, agent_apply } from './agent.js'; +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 agent_props = world.agents + const decisions = world.agents .map(a => agent_decide(world.lattice, agent, world.senses, world.actions)) + .reduce( + ([agents, props], [agent, prop]) => [[...agents, agent], [...props, prop]], + [[], []] + ); + const intermediate_agents = decisions[0]; + const agent_props = decisions[1] .flat() .reduce((acc, prop) => proposal_merge(acc, prop), []) .filter(prop => lattice_valid(intermediate_lattice, prop)) const lattice = lattice_apply(intermediate_lattice, agent_props); - const agents = world.agents.map(a => agent_apply(a, agent_props)); + const agents = intermediate_agents.map(a => agent_apply(a, agent_props)); - const world = {...world, lattice, agents}; + const new_world = {...world, lattice, agents}; return postprocess.reduce( (acc, f) => f(acc), - world + new_world ); } |