-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathGridPathModel.js
More file actions
66 lines (53 loc) · 1.79 KB
/
GridPathModel.js
File metadata and controls
66 lines (53 loc) · 1.79 KB
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
import AgentArray from 'https://agentscript.org/src/AgentArray.js'
import Model from 'https://agentscript.org/src/Model.js'
// prettier-ignore
const WorldOptions = {
minX: 0, maxX: 9,
minY: 0, maxY: 9,
minZ: 0, maxZ: 2,
}
export default class GridPathModel extends Model {
constructor(worldOptions = WorldOptions) {
super(worldOptions) // default world options if "undefined"
}
setup() {
this.patches.ask(p => (p.occupied = false))
this.walker = this.turtles.createOne()
this.walker.moveTo(this.patches.first())
this.walker.patch.occupied = true
}
step() {
if (this.done) return
this.floodFill()
const ok = this.okNeighbors(this.walker.patch)
this.walker.choices = ok.length // used by view
let turtle = this.walker.hatch()[0]
;[this.walker, turtle] = [turtle, this.walker] // swap walker & turtle
this.walker.moveTo(ok.oneOf())
this.walker.patch.occupied = true
this.links.createOne(this.walker, turtle)
this.done = this.patches.last().occupied
if (this.done) {
this.walker.choices = this.okNeighbors(this.walker.patch).length
console.log(`Model done at tick: ${this.ticks}`)
}
}
okNeighbors(p) {
return p.neighbors4.filter(p => p.ok)
}
floodFill() {
this.patches.ask(p => (p.ok = false))
let pset = new AgentArray(this.patches.last())
let step = 0
while (pset.length > 0) {
pset.ask(p => (p.ok = true))
pset.ask(p => (p.step = step))
let pnext = pset
.map(p => p.neighbors4)
.flat()
.uniq()
pset = pnext.filter(n => !n.ok && !n.occupied)
step++
}
}
}