-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.js
335 lines (297 loc) · 8.47 KB
/
engine.js
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
* A spacial hashing engine
* @class
*/
class Engine {
/**
* constructor - A constructor for the Engine
*
* @param {number} width The width of the environment
* @param {number} height The height of the environment
* @param {number} bucketSize The size of environment tiles
* @return {Engine} An engine object
*/
constructor(width, height, bucketSize) {
this.width = width;
this.height = height;
this.bucketSize = bucketSize;
this.agents = [];
this.buckets = {};
}
/**
* update - Updates all agents that exist in the engine
*
*/
update() {
//preClear(this.agents); //Update or move agents
for(let i in this.agents) {
this.agents[i].update();
}
this.boundaryRepulse(5, 5);
this.clearBuckets();
//popManage(this.agents); //Add or remove agents at this point
this.rehash(); //Rehash the agents
this.updateLocales();
//postClear(this.agents); //Agent state transitions
}
/**
* show - Draws all agents in the engine
*
* @param {p5} canvas The p5 instance on which to draw
* @return {type} description
*/
show(canvas) {
for(let agent of this.agents) {
agent.show(canvas);
}
}
/**
* clearBuckets - Resets the engine's spacial hash table
*
*/
clearBuckets() {
for(let key in this.buckets) {
//If the bucket was empty remove
if(this.buckets[key].length == 0) {
delete this.buckets[key];
} else { //Otherwise, just clear its contents
this.buckets[key] = [];
}
}
}
/**
* rehash - Rehashes all agents in the engine
*
*/
rehash() {
for(let agent of this.agents) {
this.readd(agent);
}
}
/**
* updateLocales - Sets each agent's locale to a table of agents within
* a circular region around the agent
*
*/
updateLocales() {
for(let agent of this.agents) {
var neighborhood = this.inround(agent.pos, agent.species.range);
var self = neighborhood.indexOf(agent);
if(self > -1) {
neighborhood.splice(self, 1);
}
agent.locale = {}; //Clear the locale
for(let near of neighborhood) {
if(agent.locale[near.species]) {
agent.locale[near.species].push(near);
} else {
agent.locale[near.species] = [near];
}
}
}
}
/**
* inround - Function to find all the agents in a circular region
*
* @param {p5.Vector} center The center of the circle
* @param {number} radius The radius of the circle
* @return {list} List of agents in the region specified
*/
inround(center, radius) {
var selected = this.roughrect(center, radius, radius);
return selected.filter(function(agent) {
return p5.Vector.sub(agent.pos, center).magSq() < radius*radius;
});
}
/**
* inrect - Function to find all the agents in a rectangular region
*
* @param {p5.Vector} pos The upper right corner of the rectangle
* @param {number} xrange Width of the rectangle
* @param {number} yrange Height of the rectangle
* @return {list} A list of agents
*/
inrect(pos, xrange, yrange) {
var selected = this.roughrect(pos, xrange, yrange);
return selected.filter(function(agent) {
return (agent.pos.x > pos.x - xrange) && (agent.pos.x < pos.x + xrange)
&& (agent.pos.y > pos.y - yrange) && (agent.pos.y < pos.y + yrange);
});
}
/**
* roughrect - Function that gets agents from all hashbuckets overlapping
* the rectangle
*
* @param {p5.Vector} pos The upper right corner of the rectangle
* @param {number} xrange Width of the rectangle
* @param {number} yrange Height of the rectangle
* @return {list} A list of agents
*/
roughrect(pos, xrange, yrange) {
//Create upper and lower rectangle corners
var offset = sim.createVector(xrange, yrange);
var [lx, ly] = this.hashPair(p5.Vector.sub(pos, offset));
var [ux, uy] = this.hashPair(p5.Vector.add(pos, offset));
//Add all from the buckets between upper bucket and lower bucket
var selected = [];
for(var x = lx ; x <= ux; x++) {
for(var y = ly; y <= uy; y++) {
var cur = this.hashBucket(x + ',' + y);
if(cur.length != 0) {
selected.push(...cur); //Spread operator!
}
}
}
return selected;
}
/**
* nearest - A function to get the agent nearest to the position specified
* and within the range specified
*
* @param {p5.Vector} pos The position to search from
* @param {number} range The farthest distance to consider
* @return {Agent} Agent closest to the position
*/
nearest(pos, range) {
var consider = this.inround(pos, range);
if(consider.length == 0) {
return null;
}
else if(consider.length == 1) {
return consider[0];
}
var mindist = range**2;
var closest = consider[0];
for(var agent of consider) {
var dist = p5.Vector.sub(agent.pos, pos).magSq();
if(dist < mindist) {
mindist = dist;
closest = agent;
}
}
return closest;
}
/**
* hashBucket - Function to get the hashbucket associated with a key
*
* @param {string} key The string that is the coordinates of the bucket
* @return {list} A list of agents; the bucket contents
*/
hashBucket(key) {
if(this.buckets[key]) {
return this.buckets[key];
} else {
return [];
}
}
/**
* hashPair - A function to compute spatial hashes
*
* @param {p5.Vector} pos A position to hash
* @return {string} The hash key for the position
*/
hashPair(pos) {
return [Math.trunc(pos.x/this.bucketSize), Math.trunc(pos.y/this.bucketSize)];
}
/**
* add - A function to add a brand new agent to the simulation
*
* @param {Agent} object The agent to add
*/
add(object) {
this.clamp(object); //Sanitize the object for use in environment
this.agents.push(object);
this.insert(object);
}
/**
* readd - A function to add the object again (without duplicating it) after
* the hash structure has been cleared for rehashing: do NOT call to add a
* new agent
*
* @param {type} object Agent to add
* @return {type} description
*/
readd(object) {
this.clamp(object); //Sanitize the object for use in environment
this.insert(object);
}
/**
* insert - Method to hash an object in the bounds of the environment
*
* @param {Agent} object An agent that needs to be hashed into the engine
*/
insert(object) {
var [x, y] = this.hashPair(object.pos);
var key = x + ',' + y;
if(this.buckets[key]) {
this.buckets[key].push(object);
} else {
this.buckets[key] = [object];
}
}
/**
* clamp - Method to clamp an agent inside the environment
*
* @param {Agent} object The agent to clamp
*/
clamp(object) {
if(object.pos.x < 0) {
object.pos.x = 0;
} else if(object.pos.x > this.width) {
object.pos.x = this.width;
}
if(object.pos.y < 0) {
object.pos.y = 0;
} else if(object.pos.y > this.height) {
object.pos.y = this.height;
}
}
/**
* boundaryRepulse - A function that adds margins with a constant force
* effect to keep entities inside/cause bouncing on boudaries
*
* @param {type} edge description
* @param {type} force description
* @return {type} description
*/
boundaryRepulse(edge, force) {
for(let agent of this.agents) {
if(agent.pos.x < edge) {
agent.vel.x += force;
} else if(agent.pos.x > this.width - edge) {
agent.vel.x -= force;
}
if(agent.pos.y < edge) {
agent.vel.y += 1;
} else if(agent.pos.y > this.height - edge) {
agent.vel.y -= force;
}
}
}
/**
* remove - Removes an agent, all agents of a species or all agents in a list
* from the engine depending on the argument
*
* @param {Agent|Species|Agent[]|Species[]} object An agent, species or list of agents or species
*/
remove(object) {
if(object instanceof Agent) {
var index = this.agents.indexOf(object);
if(index > -1) {
this.agents.splice(index, 1)
}
} else if(object instanceof Species) {
var list = [];
for(let agent of this.agents) {
if(agent.species != object) {
list.push(agent);
}
}
this.agents = list;
} else if(object instanceof Array) {
for(let piece of object) {
this.remove(piece);
}
}
}
}