-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathExitModel.js
82 lines (75 loc) · 2.43 KB
/
ExitModel.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
import World from 'https://code.agentscript.org/src/World.js'
import Model from 'https://code.agentscript.org/src/Model.js'
export default class ExitModel extends Model {
numExits = 10
population = 0.75 // percent of inside populated
// ======================
constructor(worldOptions = World.defaultOptions(35, 35, 1)) {
super(worldOptions)
}
setup() {
this.patchBreeds('exits inside wall')
this.turtles.setDefault('atEdge', turtle => turtle.die())
this.setupPatches()
this.setupTurtles()
}
setupPatches() {
const { maxX, maxY } = this.world
this.patches.ask(p => {
if (Math.abs(p.x) < 0.7 * maxX && Math.abs(p.y) < 0.7 * maxY) {
p.setBreed(this.inside)
}
})
this.inside.ask(p => {
p.neighbors4.ask(n => {
if (n.breed !== this.inside) n.setBreed(this.wall)
})
})
this.wall.nOf(this.numExits).ask((p, i) => {
p.setBreed(this.exits)
p.exitNumber = i
})
}
setupTurtles() {
const turtlePatches = this.inside.nOf(
this.population * this.inside.length
)
turtlePatches.ask(p => {
p.sprout(1, this.turtles, t => {
t.exit = this.exits.minOneOf(e => t.distance(e))
// t.sprite = t.exit.turtleSprite
})
})
}
step() {
let numMoves = 0
const emptyNeighbors = turtle =>
turtle.patch.neighbors.filter(
n => n.breed !== this.wall && n.turtlesHere.length === 0
)
this.turtles.ask(t => {
if (t.patch.breed === this.inside) {
const empty = emptyNeighbors(t)
if (empty.length > 0) {
const min = empty.minOneOf(n => n.distance(t.exit))
if (t.distance(t.exit) > min.distance(t.exit)) {
t.face(min)
t.setxy(min.x, min.y)
numMoves++
}
}
} else {
t.forward(1)
numMoves++
}
})
this.done = numMoves === 0
// if (this.done)
// console.log(
// 'model done at step',
// this.ticks,
// 'turtles left',
// this.turtles.length
// )
}
}