- Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathGridPathModel.js
More file actions
Latest commit
66 lines (53 loc) · 1.79 KB
/
Copy pathGridPathModel.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
importAgentArrayfrom'https://agentscript.org/src/AgentArray.js'
importModelfrom'https://agentscript.org/src/Model.js'
// prettier-ignore
constWorldOptions={
minX: 0,maxX: 9,
minY: 0,maxY: 9,
minZ: 0,maxZ: 2,
}
exportdefaultclassGridPathModelextendsModel{
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()
constok=this.okNeighbors(this.walker.patch)
this.walker.choices=ok.length// used by view
letturtle=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){
returnp.neighbors4.filter(p=>p.ok)
}
floodFill(){
this.patches.ask(p=>(p.ok=false))
letpset=newAgentArray(this.patches.last())
letstep=0
while(pset.length>0){
pset.ask(p=>(p.ok=true))
pset.ask(p=>(p.step=step))
letpnext=pset
.map(p=>p.neighbors4)
.flat()
.uniq()
pset=pnext.filter(n=>!n.ok&&!n.occupied)
step++
}
}
}