diff options
Diffstat (limited to 'src/simulation')
-rw-r--r-- | src/simulation/validity.js | 14 | ||||
-rw-r--r-- | src/simulation/validity.test.js | 43 |
2 files changed, 57 insertions, 0 deletions
diff --git a/src/simulation/validity.js b/src/simulation/validity.js new file mode 100644 index 0000000..de4acd1 --- /dev/null +++ b/src/simulation/validity.js @@ -0,0 +1,14 @@ +export const validity = [ + // prevent agents from moving onto immutables + (world, proposal) => (proposal.agent_changes || []).reduce( + (acc, change) => { + const {x, y} = change; + if (x !== undefined && y !== undefined && world.lattice[y][x].type === 'immutable') { + return false; + } else { + return acc; + } + }, + true, + ), +]; diff --git a/src/simulation/validity.test.js b/src/simulation/validity.test.js new file mode 100644 index 0000000..ba9e684 --- /dev/null +++ b/src/simulation/validity.test.js @@ -0,0 +1,43 @@ +'use strict'; + +import { world_update } from '../world/world.js'; +import { validity } from './validity.js'; + +test("agents are not allowed to move into immutables", () => { + const actions = [{ + size: 1, propose: (world, agent, head) => { + return [{ + agent_changes: [{ + agent_id: agent.id, + x: agent.x + 1, y: agent.y, + }], + }]; + }, + }]; + + const agent = { + id: 1, + net: { compute: () => [[1], null] }, + state: null, + x: 0, y: 0, + flags: {}, + }; + + const lattice = [[{ type: 'empty', flags: {} }, { type: 'immutable', flags: {} }]]; + + const world = { + lattice, + lattice_rules: { empty: ()=>{}, immutable: ()=>{} }, + agents: [agent], + senses: [], + actions, + validity, + }; + + expect(world_update(world).agents[0]).toEqual(agent); + world.validity = []; + expect(world_update(world).agents[0]).toEqual({ + ...agent, + x: 1, y: 0, + }); +}); |