diff options
author | sanine <sanine.not@pm.me> | 2023-11-11 13:58:56 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-11-11 13:58:56 -0600 |
commit | 78f2f5f4b1ccd58bbdef0d5e5d2dd026ac4ad668 (patch) | |
tree | 25e4fdad001cdcf7ca5bd1055591b84441a97d75 /src/simulation | |
parent | 7a3fc898def0283ed6655b5c45a5f413638272a7 (diff) |
add postprocessing rule to freeze agents
Diffstat (limited to 'src/simulation')
-rw-r--r-- | src/simulation/postprocess.js | 18 | ||||
-rw-r--r-- | src/simulation/postprocess.test.js | 37 |
2 files changed, 55 insertions, 0 deletions
diff --git a/src/simulation/postprocess.js b/src/simulation/postprocess.js new file mode 100644 index 0000000..d50839e --- /dev/null +++ b/src/simulation/postprocess.js @@ -0,0 +1,18 @@ +'use strict'; + +export const postprocess = [ + (world) => ({ + ...world, + agents: world.agents.map(a => { + const {x, y} = a; + if ( + world.lattice[y][x].type === 'mutable' || + world.lattice[y][x].type === 'active' + ) { + return { ...a, flags: {...a.flags, frozen: true } }; + } else { + return a; + } + }), + }), +]; diff --git a/src/simulation/postprocess.test.js b/src/simulation/postprocess.test.js new file mode 100644 index 0000000..dc77c6a --- /dev/null +++ b/src/simulation/postprocess.test.js @@ -0,0 +1,37 @@ +'use strict'; + +import { world_update } from '../world/world.js'; +import { postprocess } from './postprocess.js'; + +test("agents freeze when finishing on a mutable or active", () => { + const agent = { + id: 1, + net: { compute: () => [[1], null] }, + state: null, + x: 0, y: 0, + flags: {}, + }; + + const lattice = [[{ type: 'empty', flags: {} }]]; + + const world = { + lattice, + lattice_rules: { empty: ()=>{}, active: ()=>{}, mutable: ()=>{} }, + agents: [agent], + senses: [], + actions: [], + validity: [], + }; + + expect(world_update(world, postprocess).agents[0]).toEqual(agent); + world.lattice[0][0].type = 'mutable'; + expect(world_update(world, postprocess).agents[0]).toEqual({ + ...agent, + flags: { frozen: true }, + }); + world.lattice[0][0].type = 'active'; + expect(world_update(world, postprocess).agents[0]).toEqual({ + ...agent, + flags: { frozen: true }, + }); +}); |