-
Notifications
You must be signed in to change notification settings - Fork 0
/
AgentContainer.js
381 lines (340 loc) · 10.1 KB
/
AgentContainer.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import { random } from './util.js'
class Point {
constructor(x, y) {
this.x = x
this.y = y
}
}
class Rectangle {
constructor(arg1, y, w, h) {
if(arg1 instanceof Rectangle) {
this.x = arg1.x
this.y = arg1.y
this.w = arg1.w
this.h = arg1.h
}
this.x = arg1
this.y = y
this.w = w
this.h = h
}
contains(p) {
return p.x >= this.x &&
p.y >= this.y &&
p.x < this.x + this.w &&
p.y < this.y + this.h
}
copy() {
return new Rectangle(this)
}
}
class Circle {
constructor(x, y, r) {
this.x = x;
this.y = y;
this.r = r;
}
contains(point) {
return ((point.x - this.x) ** 2 + (point.y - this.y) ** 2) <= this.r ** 2;
}
intersects(range) {
const center = new Point(this.x, this.y)
const max_ranges = [
new Rectangle(range.x - this.r, range.y , range.w + this.r, range.h),
new Rectangle(range.x , range.y - this.x, range.w , range.h + this.r),
new Circle(range.x , range.y , this.r),
new Circle(range.x + range.w, range.y , this.r),
new Circle(range.x , range.y + range.h, this.r),
new Circle(range.x + range.w, range.y + range.h, this.r)
]
for(const r of max_ranges)
if(r.contains(center))
return true
return false
}
}
class QuadTree {
constructor(boundary, capacity = 1, isRoot = true) {
this.boundary = boundary
this.capacity = capacity
this.agents = []
if(isRoot)
this.isRoot = true
}
makeChild(xoffset, yoffset) {
return new QuadTree(new Rectangle(this.boundary.x + xoffset * this.boundary.w / 2, this.boundary.y + yoffset * this.boundary.h / 2, this.boundary.w / 2, this.boundary.h / 2), this.capacity)
}
makeParent(xoffset, yoffset, prop) {
this.isRoot = false
const parent = new QuadTree(new Rectangle(this.boundary.x - xoffset * this.boundary.w, this.boundary.y - yoffset * this.boundary.h, this.boundary.w * 2, this.boundary.h * 2), this.capacity, true)
parent[prop] = this
return parent
}
divide() {
if(!this.ne) this.ne = this.makeChild(1, 0)
if(!this.nw) this.nw = this.makeChild(0, 0)
if(!this.sw) this.sw = this.makeChild(0, 1)
if(!this.se) this.se = this.makeChild(1, 1)
this.divided = true
}
insert(p) {
if(!this.boundary.contains(p))
{
if(this.isRoot) {
let curr = this,
parent
const expand = (function() {
let {x, y} = curr.boundary
if(p.x < x && p.y < y)
return [1, 1, 'se']
else if(p.x >= x && p.y < y)
return [0, 1, 'sw']
else if(p.x < x && p.y >= y)
return [1, 0, 'ne']
else if(p.x >= x && p.y >= y)
return [0, 0, 'nw']
})()
do {
parent = curr.makeParent(...expand)
parent.divide()
curr = parent
} while(!parent.boundary.contains(p))
return parent
} else {
return
}
} else {
if(this.agents.length < this.capacity){
this.agents.push(p)
} else {
this.overflow(p)
}
return this
}
}
overflow(p) {
if(!this.divided)
this.divide()
this.ne.insert(p)
this.nw.insert(p)
this.sw.insert(p)
this.se.insert(p)
}
getInCircle(x, y, r) {
return this.get(new Circle(x, y, r))
}
get(range, list = []) {
if (!range.intersects(this.boundary)) {
return list;
}
for (let p of this.agents) {
if (range.contains(p)) {
list.push(p);
}
}
if (this.divided) {
this.ne.get(range, list);
this.nw.get(range, list);
this.sw.get(range, list);
this.se.get(range, list);
}
return list;
}
*entries() {
yield* this.agents.values()
if(!this.divided)
return
yield* this.ne.entries()
yield* this.nw.entries()
yield* this.sw.entries()
yield* this.se.entries()
}
[Symbol.iterator]() {
return this.entries()
}
}
class AgentContainer {
constructor() {
this.agents = {}
this.ungrouped = []
}
copy() {
const agents_copy = {}
for(const [k, v] of Object.entries(this.agents)) {
agents_copy[k] = []
for(const a of v) {
const agent = Object.assign(new (a.constructor)(), a)
agents_copy[k].push(agent)
agent._ssinternal.original = a
}
}
const ungrouped_copy = []
for(const a of this.ungrouped) {
const agent = Object.assign(new (a.constructor)(), a)
ungrouped_copy.push(agent)
agent._ssinternal.original = a
}
const r = new AgentContainer()
r.agents = agents_copy
r.ungrouped = ungrouped_copy
return r
}
push(agent) {
if(agent.constructor.getTexture)
{
const key = agent.constructor.getTexture()
if(!this.agents[key])
this.agents[key] = []
this.agents[key].push(agent)
} else {
this.ungrouped.push(agent)
}
}
*getPartitioned() {
for(const k in this.agents) {
yield [this.agents[k], k]
}
for(const a in this.ungrouped) {
yield [[a], a.getTexture()]
}
}
*[Symbol.iterator]() {
for(const k in this.agents) {
yield* this.agents[k].values()
}
yield* this.ungrouped.values()
}
}
class CellContainer {
constructor(arg1, h, cellTypes, sim) {
if(arg1 instanceof CellContainer) {
this.w = arg1.w
this.h = arg1.h
this.cells = []
for(let x = 0; x < this.w; x++) {
this.cells.push([])
for(let y = 0; y < this.h; y++) {
const curr = arg1.cells[x][y]
const old = Object.assign(new (arg1.cells[x][y].constructor)(), curr)
old.curr = curr
this.cells[x].push(old)
}
}
} else {
const w = arg1
this.w = w
this.h = h
this.cells = []
if(cellTypes instanceof Function) {
cellTypes = [cellTypes]
}
for(let x = 0; x < w; x++) {
this.cells.push([])
for(let y = 0; y < h; y++) {
const cellType = random(cellTypes)
const cell = new (cellType)(x, y, sim)
cell.init()
// if(randomizeEach)
// cell.random()
this.cells[x].push(cell)
}
}
}
}
copy() {
return new CellContainer(this)
}
get(x, y) {
const mod = (n, M) => ((n % M) + M) % M
return this.cells[mod(x, this.w)][mod(y, this.h)]
}
set(cell) {
return this.cells[cell.x][cell.y] = cell
}
*[Symbol.iterator]() {
for(let i = 0; i < this.w; i++) {
for(let j = 0; j < this.h; j++) {
yield this.cells[i][j]
}
}
}
}
const cellCoordToId = ({x, y}) => `${x},${y}`
class InfiniteCellContainer {
constructor(arg1, h, emptyCell, sim) {
if(arg1 instanceof InfiniteCellContainer) {
this.boundary = arg1.boundary
this.sim = arg1.sim
this.emptyCell = arg1.emptyCell
this.cells = {}
for(const [id, cell] of Object.entries(arg1.cells)) {
const old = Object.assign(new (cell.constructor)(), cell)
old.curr = cell
this.cells[id] = old
}
} else {
this.sim = sim
this.emptyCell = emptyCell
this.cells = {}
this.boundary = null
}
}
getExtent() {
return this.boundary
}
copy() {
return new InfiniteCellContainer(this)
}
get(x, y) {
return this.cells[cellCoordToId({x, y})] || this.spawnEmpty(x, y)
}
set(c) {
if(!this.boundary) {
this.boundary = new Rectangle(c.x, c.y, 1, 1)
}
const b = this.boundary
if(c.x < b.x)
b.x = c.x
else if(c.x > b.x + b.w)
b.h = c.x - b.x + 1
if(c.y < b.y)
b.y = c.y
else if(c.y > b.y + b.h)
b.w = c.y - b.y + 1
const id = cellCoordToId(c)
return this.cells[id] = c
}
spawnEmpty(x, y, check = true) {
const id = cellCoordToId({x, y})
if(this.cells[id])
return
const new_cell = new this.emptyCell(x, y, this.sim)
new_cell.init()
this.set(new_cell)
return new_cell
}
registerCellUpdate(cell) {
if(cell._ssinternal.generatedNeighs)
return
const {x, y} = cell
this.spawnEmpty(x+1, y)
this.spawnEmpty(x, y+1)
this.spawnEmpty(x-1, y)
this.spawnEmpty(x, y-1)
this.spawnEmpty(x+1, y+1)
this.spawnEmpty(x-1, y-1)
this.spawnEmpty(x+1, y-1)
this.spawnEmpty(x-1, y+1)
cell._ssinternal.generatedNeighs = true
}
update() {
const cp = Object.assign({}, this.cells);
for(const cell of Object.values(cp)) {
this.registerCellUpdate(cell)
}
}
*[Symbol.iterator]() {
yield* Object.values(this.cells).values()
}
}
export {AgentContainer, QuadTree, Rectangle, CellContainer, InfiniteCellContainer};