diff options
author | sanine <sanine.not@pm.me> | 2023-11-10 21:10:46 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-11-10 21:10:46 -0600 |
commit | 8252de1deb1a677a71108f6c5f8705d7c04a63fe (patch) | |
tree | e533d310788a98aa212ccd747b842454445e16a9 /src/simulation | |
parent | 3b148775e5cf69882fe2503e144777b9ffd166db (diff) |
implement unfreeze action
Diffstat (limited to 'src/simulation')
-rw-r--r-- | src/simulation/actions.js | 24 | ||||
-rw-r--r-- | src/simulation/actions.test.js | 31 |
2 files changed, 53 insertions, 2 deletions
diff --git a/src/simulation/actions.js b/src/simulation/actions.js index e389518..62a8395 100644 --- a/src/simulation/actions.js +++ b/src/simulation/actions.js @@ -137,7 +137,29 @@ const pretend_frozen = { }; +const unfreeze = { + size: 1, + propose: (world, agent, head) => { + if (head[0] < threshold) { return []; } + else { + const dx = { n: 0, e: 1, s: 0, w: -1 }[agent.flags.orientation]; + const dy = { n: -1, e: 0, s: 1, w: 0 }[agent.flags.orientation]; + const target = world.agents.filter( + a => a.x === agent.x+dx && a.y === agent.y+dy + )[0]; + if (target === undefined) { return []; } + return [ + { agent_changes: [{ + agent_id: target.id, + flags: { frozen: false, emit: [0, 1, 0, 0, 0, 0, 0, 0] }, + }]} + ]; + } + }, +}; + + export const actions = [ move_forward, move_backward, turn_left, turn_right, - place, trigger, pretend_frozen, + place, trigger, pretend_frozen, unfreeze, ]; diff --git a/src/simulation/actions.test.js b/src/simulation/actions.test.js index 43ad23c..c5f35be 100644 --- a/src/simulation/actions.test.js +++ b/src/simulation/actions.test.js @@ -7,7 +7,7 @@ const [ move_forward, move_backward, turn_left, turn_right, place, trigger, - pretend_frozen, + pretend_frozen, unfreeze, ...rest ] = actions; @@ -217,3 +217,32 @@ test("pretend frozen", () => { }] }]); }); + + +test("unfreeze", () => { + const agent1 = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } }; + const agent2 = { id: 1, x: 0, y: -1 }; + const agent3 = { id: 10, x: -1, y: 0 }; + const agents = [ agent1, agent2, agent3 ]; + + expect(unfreeze.propose({agents}, agent1, [1])).toEqual([{ + agent_changes: [{ + agent_id: 1, + flags: { frozen: false, emit: [ 0, 1, 0, 0, 0, 0, 0, 0 ] }, + }] + }]); + + agent1.flags.orientation = 'w'; + + expect(unfreeze.propose({agents}, agent1, [1])).toEqual([{ + agent_changes: [{ + agent_id: 10, + flags: { frozen: false, emit: [ 0, 1, 0, 0, 0, 0, 0, 0 ] }, + }] + }]); + + agent1.flags.orientation = 's'; + expect(unfreeze.propose({agents}, agent1, [1])).toEqual([]); + + expect(unfreeze.propose({agents}, agent1, [0])).toEqual([]); +}); |