summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/simulation/lattice_rules.test.js6
-rw-r--r--src/world/world.js16
2 files changed, 18 insertions, 4 deletions
diff --git a/src/simulation/lattice_rules.test.js b/src/simulation/lattice_rules.test.js
index 9a6a344..e541add 100644
--- a/src/simulation/lattice_rules.test.js
+++ b/src/simulation/lattice_rules.test.js
@@ -22,7 +22,7 @@ test("blinker", () => {
[ D, D, D, D, D ],
];
- const world = { lattice, lattice_rules, agents: [], senses: [], actions: [] };
+ 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 ],
@@ -46,7 +46,7 @@ test("glider", () => {
[ D, D, D, D, D, D ],
];
- const world = { lattice, lattice_rules, agents: [], senses: [], actions: [] };
+ 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 ],
@@ -94,7 +94,7 @@ test("beehive", () => {
[ D, D, D, D, D, D ],
];
- const world = { lattice, lattice_rules, agents: [], senses: [], actions: [] };
+ const world = { lattice, lattice_rules, agents: [], senses: [], actions: [], validity: [] };
//expect(world_update(world).lattice).toEqual([
expect(apply(world_update, 1, world).lattice).toEqual(lattice);
});
diff --git a/src/world/world.js b/src/world/world.js
index e1d984d..e1ba0cf 100644
--- a/src/world/world.js
+++ b/src/world/world.js
@@ -3,6 +3,17 @@ import { agent_decide, agent_apply } from './agent.js';
import { proposal_merge } from './proposal.js';
+// world structure:
+// {
+// lattice
+// lattice_rules: object
+// agents: agent[]
+// senses: sense[]
+// actions: action[]
+// validity: (function(proposal) => bool)[]
+// }
+
+
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);
@@ -19,7 +30,10 @@ export function world_update(world, postprocess=[]) {
.reduce((acc, prop) => proposal_merge(acc, prop), [])
.filter(prop => lattice_valid(intermediate_lattice, prop))
- const lattice = lattice_apply(intermediate_lattice, agent_props);
+ const lattice = lattice_apply(intermediate_lattice, world.validity.reduce(
+ (acc, rule) => acc.filter(rule),
+ agent_props
+ ));
const agents = intermediate_agents.map(a => agent_apply(a, agent_props));
const new_world = {...world, lattice, agents};