diff options
author | sanine <sanine.not@pm.me> | 2023-11-10 16:57:23 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-11-10 16:57:23 -0600 |
commit | a85f61cef6aef51bf6983f98020f3284aeb5020d (patch) | |
tree | f86af3b7f9b63914079d0ff8c55b8f17b47df5fd /src | |
parent | 3f8b3ea65df956c23b397913561cc8ebe5403c6e (diff) |
implement move_forward action
Diffstat (limited to 'src')
-rw-r--r-- | src/simulation/actions.js | 28 | ||||
-rw-r--r-- | src/simulation/actions.test.js | 32 | ||||
-rw-r--r-- | src/simulation/senses.js | 1 |
3 files changed, 60 insertions, 1 deletions
diff --git a/src/simulation/actions.js b/src/simulation/actions.js new file mode 100644 index 0000000..40545c5 --- /dev/null +++ b/src/simulation/actions.js @@ -0,0 +1,28 @@ +'use strict'; + +const threshold = 0.5; + + +const move_forward = { + size: 1, + propose: (agent, head) => { + if (head[0] > threshold) { + 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 [{ + agent_changes: [{ + agent_id: agent.id, + x: agent.x + dx, + y: agent.y + dy, + }], + }]; + } else { + return []; + } + }, +}; + + +export const actions = [ + move_forward, +]; diff --git a/src/simulation/actions.test.js b/src/simulation/actions.test.js new file mode 100644 index 0000000..a870352 --- /dev/null +++ b/src/simulation/actions.test.js @@ -0,0 +1,32 @@ +'use strict'; + +import { actions } from './actions.js'; + + +const [move_forward, ...rest] = actions; + + +test("move forward", () => { + 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(move_forward.propose(n, [1])).toEqual([ + { agent_changes: [{ agent_id: 0, x: 0, y: -1 }] }, + ]); + expect(move_forward.propose(s, [1])).toEqual([ + { agent_changes: [{ agent_id: 0, x: 0, y: 1 }] }, + ]); + expect(move_forward.propose(e, [1])).toEqual([ + { agent_changes: [{ agent_id: 0, x: 1, y: 0 }] }, + ]); + expect(move_forward.propose(w, [1])).toEqual([ + { agent_changes: [{ agent_id: 0, x: -1, y: 0 }] }, + ]); + + expect(move_forward.propose(n, [0])).toEqual([]); + expect(move_forward.propose(s, [-1])).toEqual([]); + expect(move_forward.propose(e, [0])).toEqual([]); + expect(move_forward.propose(w, [-1])).toEqual([]); +}); diff --git a/src/simulation/senses.js b/src/simulation/senses.js index bcfe9df..d2eb581 100644 --- a/src/simulation/senses.js +++ b/src/simulation/senses.js @@ -93,7 +93,6 @@ function vision_idx_to_world_pos(world, agent, idx) { const vx = idx % VIS_WIDTH; const vy = Math.floor(idx / VIS_WIDTH); const result = vision_pos_to_world_pos(world, agent, vx, vy); - console.log([agent.x, agent.y], vx, vy, result); return result; } |