- Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathHelloPlusModel.js
More file actions
Latest commit
50 lines (45 loc) · 1.65 KB
/
Copy pathHelloPlusModel.js
File metadata and controls
50 lines (45 loc) · 1.65 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
importHelloModelfrom'https://agentscript.org/models/HelloModel.js'
import*asutilfrom'https://agentscript.org/src/utils.js'
// Here is a simple modification that allows setting the population dynamically.
// Note that speed & wiggle are already dynamic.
exportdefaultclassHelloPlusModelextendsHelloModel{
population=15// override HelloModel
minPopulation=5
maxPopulation=25
changeTick=25// set to null to avoid auto population changes
// ======================
// We can use Model's constructor, due to using Model's default World.
// If you pass in world options, Model will use them
constructor(){
super()// use default world options.
}
step(){
if(util.mod(this.ticks,this.changeTick)===0){
// true when ticks = 0
this.population=util.randomInt2(
this.minPopulation,
this.maxPopulation
)
console.log('new population',this.population)
}
this.checkPopulation()
super.step()
}
checkPopulation(){
// return if no change
constdelta=this.population-this.turtles.length
if(delta===0)return
// add/remove turtles as needed
if(delta<0){
util.repeat(-delta,()=>this.turtles.oneOf().die())
}else{
this.turtles.create(delta,t=>t.moveTo(this.patches.oneOf()))
}
// make sure all turtles have at least one link
this.turtles.ask(t=>{
if(t.links.length===0){
this.links.create(t,this.turtles.otherOneOf(t))
}
})
}
}