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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
'use strict';
const frozen = {
size: 1,
read: (world, agent) => {
if (agent.flags.frozen === true) {
return [ 1 ];
} else {
return [ 0 ];
}
},
};
// add two arrays together element-wise with a scaling factor
function array_scalesum(a, s, b) {
return a.map((x, i) => x + (s*b[i]));
}
// determine the square of the distance between two cells
function lattice_dist2(x0, y0, x1, y1) {
if (x0 === x1 && y0 === y1) { return 1; } // not proper distance but avoids divide-by-zero errors c:
return ((x0-x1)**2) + ((y0-y1)**2);
}
const hear = {
size: 8,
read: (world, agent) => {
const {x, y} = agent;
const lattice_sounds = world.lattice
.map((row, cy) => row.map((cell, cx) => [ 1/lattice_dist2(x, y, cx, cy), cell ]))
.flat()
.filter(([scale, cell]) => cell.flags.emit !== undefined)
.reduce(
(acc, [scale, cell]) => array_scalesum(acc, scale, cell.flags.emit),
[0, 0, 0, 0, 0, 0, 0, 0]
);
const agent_sounds = world.agents
.filter(a => a.flags.emit !== undefined)
.reduce(
(acc, a) => array_scalesum(acc, 1/lattice_dist2(x, y, a.x, a.y), a.flags.emit),
[0, 0, 0, 0, 0, 0, 0, 0]
);
return array_scalesum(lattice_sounds, 1, agent_sounds).map(ch => Math.tanh(ch));
},
};
const [VIS_WIDTH, VIS_HEIGHT] = [31, 31];
const [VIS_HWIDTH, VIS_HHEIGHT] = [Math.floor(VIS_WIDTH/2), Math.floor(VIS_HEIGHT/2)];
function identity_mod(n, p) {
const mod = (n % (2*p)) - p + 1;
return mod/p;
}
function world_pos_to_vision_pos(world, agent, x, y) {
const dx = x - agent.x;
const dy = y - agent.y;
const orientation = agent.flags.orientation || 'n';
switch (orientation) {
case 'n':
return [VIS_HWIDTH+dx, VIS_HEIGHT+dy-1];
case 's':
return [VIS_HWIDTH-dx, VIS_HEIGHT-dy-1];
case 'e':
return [VIS_HWIDTH+dy, VIS_HEIGHT-dx-1];
case 'w':
return [VIS_HWIDTH-dy, VIS_HEIGHT+dx-1];
}
}
function vision_pos_to_world_pos(world, agent, x, y) {
const dx = x-VIS_HWIDTH;
const dy = y-VIS_HEIGHT+1;
const orientation = agent.flags.orientation || 'n';
switch (orientation) {
case 'n':
return [agent.x + dx, agent.y + dy];
case 's':
return [agent.x - dx, agent.y - dy];
case 'e':
return [agent.x - dy, agent.y - dx];
case 'w':
return [agent.x + dy, agent.y + dx];
}
}
function world_pos_to_vision_idx(world, agent, x, y) {
const [vx, vy] = world_pos_to_vision_pos(world, agent, x, y);
return (VIS_WIDTH * vy) + vx;
}
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;
}
function see_cell(world, x, y) {
const team = 0;
const orientation = 0;
if (!world.lattice[y][x]) {
throw new Error(`${x}, ${y}`);
}
const type = {
active: -0.8,
mutable: -0.4,
empty: 0.0,
immutable: 0.4,
flag: 0.8,
}[world.lattice[y][x].type];
return [team, orientation, type];
}
function relative_orientation(viewer, agent) {
switch(viewer.flags.orientation) {
case 'n': return { n: -0.8, e: -0.4, s: 0.4, w: 0.8 }[agent.flags.orientation];
case 'e': return { e: -0.8, s: -0.4, w: 0.4, n: 0.8 }[agent.flags.orientation];
case 's': return { s: -0.8, w: -0.4, n: 0.4, e: 0.8 }[agent.flags.orientation];
case 'w': return { w: -0.8, n: -0.4, e: 0.4, s: 0.8 }[agent.flags.orientation];
}
}
function see_agent(viewer, agent) {
const team = {
0: -0.8,
1: -0.4,
2: 0.4,
3: 0.8,
}[agent.flags.team] + (identity_mod(agent.id, 11)/16);
const orientation = relative_orientation(viewer, agent) + (identity_mod(agent.id, 37)/16);
const type = (() => {
if (agent.flags.frozen && agent.flags.flag) {
return -0.4;
} else if (agent.flags.frozen) {
return -0.8;
} else if (agent.flags.flag) {
return 0.8;
} else {
return 0.0;
}
})() + (identity_mod(agent.id, 499)/16);
return [team, orientation, type];
}
const see = {
size: VIS_WIDTH * VIS_HEIGHT,
read: (world, agent) => {
const vision = [...Array(VIS_WIDTH*VIS_HEIGHT).keys()]
.map(idx => {
const [x, y] = vision_idx_to_world_pos(world, agent, idx);
return see_cell(world, x, y);
});
return world.agents.reduce(
(acc, a) => {
const idx = world_pos_to_vision_idx(world, agent, a.x, a.y);
if (idx < 0 || idx > VIS_WIDTH*VIS_HEIGHT) {
return acc;
} else {
return acc.toSpliced(idx, 1, see_agent(agent, a));
}
},
vision
);
},
};
export const senses = [
frozen, hear, see,
];
|