'use strict'; const threshold = 0.5; const move_forward = { size: 1, propose: (world, 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 []; } }, }; const move_backward = { size: 1, propose: (world, 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 []; } }, }; const turn_left = { size: 1, propose: (world, agent, head) => { if (head[0] > threshold) { const orientation = { n: 'w', e: 'n', s: 'e', w: 's' }[agent.flags.orientation]; return [{ agent_changes: [{ agent_id: agent.id, flags: { orientation }, }], }]; } else { return []; } }, }; const turn_right = { size: 1, propose: (world, agent, head) => { if (head[0] > threshold) { const orientation = { n: 'e', e: 's', s: 'w', w: 'n' }[agent.flags.orientation]; return [{ agent_changes: [{ agent_id: agent.id, flags: { orientation }, }], }]; } else { return []; } }, }; 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] }, }]} ]; } }, }; 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] }, }]} ]; } }, }; const pretend_frozen = { size: 1, propose: (world, agent, head) => { if (head[0] < threshold) { return [{ agent_changes: [{ agent_id: agent.id, flags: { pretend_frozen: false }, }], }]; } else { return [{ agent_changes: [{ agent_id: agent.id, flags: { pretend_frozen: true }, }] }]; } }, }; 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] }, }]} ]; } }, }; 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 }, }, ], }]; } } }, }; const drop_flag = { size: 1, propose: (world, agent, head) => { if (head[0] < threshold) { return []; } else if (!agent.flags.flag) { 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: 'flag', flags: { emit: [ 0, 0, -1, 0, 0, 0, 0, 0 ] }, }], agent_changes: [{ agent_id: agent.id, flags: { flag: false }, }], } ]; } }, }; const speak = { size: 8, propose: (world, agent, head) => { return [ { agent_changes: [{ agent_id: agent.id, flags: { emit: head }, }], }, ]; }, }; export const actions = [ move_forward, move_backward, turn_left, turn_right, place, trigger, pretend_frozen, unfreeze, take_flag, drop_flag, speak, ];