summaryrefslogtreecommitdiff
path: root/src/simulation/actions.js
blob: 8a4e8f00968c86b57a71b20d09226a8cb76ef183 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'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] },
        }]}
      ];
    }
  },
};




export const actions = [
  move_forward, move_backward, turn_left, turn_right, place,
];