diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/simulation/actions.js | 45 | ||||
-rw-r--r-- | src/simulation/actions.test.js | 34 |
2 files changed, 78 insertions, 1 deletions
diff --git a/src/simulation/actions.js b/src/simulation/actions.js index 62a8395..6497917 100644 --- a/src/simulation/actions.js +++ b/src/simulation/actions.js @@ -159,7 +159,50 @@ const unfreeze = { }; +const take_flag = { + 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 || !target.flags.flag) { + return [ + { + lattice_changes: [{ + x: agent.x + dx, y: agent.y + dy, + from: 'flag', to: 'empty', + flags: { emit: [ 0, 0, 1, 0, 0, 0, 0, 0 ] }, + }], + agent_changes: [{ + agent_id: agent.id, + flags: { flag: true }, + }], + } + ]; + } else { + return [{ + agent_changes: [ + { + agent_id: target.id, + flags: { flag: false }, + }, + { + agent_id: agent.id, + flags: { flag: true }, + }, + ], + }]; + } + } + }, +}; + + export const actions = [ move_forward, move_backward, turn_left, turn_right, - place, trigger, pretend_frozen, unfreeze, + place, trigger, pretend_frozen, unfreeze, take_flag, ]; diff --git a/src/simulation/actions.test.js b/src/simulation/actions.test.js index c5f35be..e0b29dc 100644 --- a/src/simulation/actions.test.js +++ b/src/simulation/actions.test.js @@ -8,6 +8,7 @@ const [ turn_left, turn_right, place, trigger, pretend_frozen, unfreeze, + take_flag, ...rest ] = actions; @@ -246,3 +247,36 @@ test("unfreeze", () => { expect(unfreeze.propose({agents}, agent1, [0])).toEqual([]); }); + + +test("take flag", () => { + const agent = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } }; + const other1 = { id: 1, x: 0, y: -1, flags: { flag: true } }; + const other2 = { id: 2, x: 0, y: 1, flags: { flag: false } }; + + const world = { agents: [ agent, other1, other2 ] }; + + expect(take_flag.propose(world, agent, [1])).toEqual([ + { agent_changes: [ + { agent_id: 1, flags: { flag: false } }, + { agent_id: 0, flags: { flag: true } }, + ]}, + ]); + + agent.flags.orientation = 's'; + expect(take_flag.propose(world, agent, [1])).toEqual([ + { + lattice_changes: [ + { + x: 0, y: 1, from: 'flag', to: 'empty', + flags: { emit: [ 0, 0, 1, 0, 0, 0, 0, 0 ] }, + }, + ], + agent_changes: [ + { agent_id: 0, flags: { flag: true } }, + ] + }, + ]); + + expect(take_flag.propose(world, agent, [0])).toEqual([]); +}); |