diff options
author | sanine <sanine.not@pm.me> | 2023-11-10 20:39:44 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-11-10 20:39:44 -0600 |
commit | e7eb6a78e165c2103323baa9bd6cbdaf7444c17d (patch) | |
tree | 2994b2cf3c37afec8982feecec19e074d1aeb51d | |
parent | 2160d6cf566df60a4a2b190965152cd8d5cb5fad (diff) |
add trigger action
-rw-r--r-- | src/simulation/actions.js | 20 | ||||
-rw-r--r-- | src/simulation/actions.test.js | 40 |
2 files changed, 58 insertions, 2 deletions
diff --git a/src/simulation/actions.js b/src/simulation/actions.js index 8a4e8f0..dd7c4e6 100644 --- a/src/simulation/actions.js +++ b/src/simulation/actions.js @@ -97,8 +97,26 @@ const place = { }; +const trigger = { + 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]; + return [ + { lattice_changes: [{ + x: agent.x + dx, y: agent.y + dy, + from: 'mutable', to: 'active', + flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] }, + }]} + ]; + } + }, +}; export const actions = [ - move_forward, move_backward, turn_left, turn_right, place, + move_forward, move_backward, turn_left, turn_right, + place, trigger, ]; diff --git a/src/simulation/actions.test.js b/src/simulation/actions.test.js index e94605d..1ebe9a9 100644 --- a/src/simulation/actions.test.js +++ b/src/simulation/actions.test.js @@ -6,7 +6,7 @@ import { actions } from './actions.js'; const [ move_forward, move_backward, turn_left, turn_right, - place, + place, trigger, ...rest ] = actions; @@ -159,3 +159,41 @@ test("place", () => { expect(place.propose(null, e, [0])).toEqual([]); expect(place.propose(null, w, [-1])).toEqual([]); }); + + +test("trigger", () => { + const n = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } }; + const s = { id: 0, x: 0, y: 0, flags: { orientation: 's' } }; + const e = { id: 0, x: 0, y: 0, flags: { orientation: 'e' } }; + const w = { id: 0, x: 0, y: 0, flags: { orientation: 'w' } }; + + expect(trigger.propose(null, n, [1])).toEqual([ + { lattice_changes: [{ + x: 0, y: -1, from: 'mutable', to: 'active', + flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] }, + }]}, + ]); + expect(trigger.propose(null, s, [1])).toEqual([ + { lattice_changes: [{ + x: 0, y: 1, from: 'mutable', to: 'active', + flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] }, + }]}, + ]); + expect(trigger.propose(null, e, [1])).toEqual([ + { lattice_changes: [{ + x: 1, y: 0, from: 'mutable', to: 'active', + flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] }, + }]}, + ]); + expect(trigger.propose(null, w, [1])).toEqual([ + { lattice_changes: [{ + x: -1, y: 0, from: 'mutable', to: 'active', + flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] }, + }]}, + ]); + + expect(trigger.propose(null, n, [0])).toEqual([]); + expect(trigger.propose(null, s, [-1])).toEqual([]); + expect(trigger.propose(null, e, [0])).toEqual([]); + expect(trigger.propose(null, w, [-1])).toEqual([]); + }); |