diff options
author | sanine <sanine.not@pm.me> | 2023-11-10 20:05:18 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-11-10 20:05:18 -0600 |
commit | 2160d6cf566df60a4a2b190965152cd8d5cb5fad (patch) | |
tree | 1c2adcf281f90d6d1b3e0dcb13865059f23742cd /src | |
parent | 984f07a24d3d377c13358fc5533c0eda383858f5 (diff) |
add place action
Diffstat (limited to 'src')
-rw-r--r-- | src/simulation/actions.js | 21 | ||||
-rw-r--r-- | src/simulation/actions.test.js | 45 |
2 files changed, 65 insertions, 1 deletions
diff --git a/src/simulation/actions.js b/src/simulation/actions.js index 0f1a767..8a4e8f0 100644 --- a/src/simulation/actions.js +++ b/src/simulation/actions.js @@ -78,8 +78,27 @@ const turn_right = { }; +const place = { + 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: 'empty', to: 'mutable', + flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] }, + }]} + ]; + } + }, +}; + + export const actions = [ - move_forward, move_backward, turn_left, turn_right, + move_forward, move_backward, turn_left, turn_right, place, ]; diff --git a/src/simulation/actions.test.js b/src/simulation/actions.test.js index 04111f1..e94605d 100644 --- a/src/simulation/actions.test.js +++ b/src/simulation/actions.test.js @@ -6,6 +6,7 @@ import { actions } from './actions.js'; const [ move_forward, move_backward, turn_left, turn_right, + place, ...rest ] = actions; @@ -114,3 +115,47 @@ test("turn_right", () => { }); +test("place", () => { + 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(place.propose(null, n, [1])).toEqual([ + { lattice_changes: [ + { + x: 0, y: -1, from: 'empty', to: 'mutable', + flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] } + } + ]} + ]); + expect(place.propose(null, s, [1])).toEqual([ + { lattice_changes: [ + { + x: 0, y: 1, from: 'empty', to: 'mutable', + flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] } + } + ]} + ]); + expect(place.propose(null, e, [1])).toEqual([ + { lattice_changes: [ + { + x: 1, y: 0, from: 'empty', to: 'mutable', + flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] } + } + ]} + ]); + expect(place.propose(null, w, [1])).toEqual([ + { lattice_changes: [ + { + x: -1, y: 0, from: 'empty', to: 'mutable', + flags: { emit: [1, 0, 0, 0, 0, 0, 0, 0] } + } + ]} + ]); + + expect(place.propose(null, n, [0])).toEqual([]); + expect(place.propose(null, s, [-1])).toEqual([]); + expect(place.propose(null, e, [0])).toEqual([]); + expect(place.propose(null, w, [-1])).toEqual([]); +}); |