-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
160 lines (129 loc) · 3.52 KB
/
main.go
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
package main
import (
"fmt"
"log"
"math/rand"
"os"
"time"
"github.com/real-time-footfall-analysis/rtfa-simulation/geometry"
"github.com/real-time-footfall-analysis/rtfa-simulation/utils"
"golang.org/x/exp/shiny/driver"
"golang.org/x/exp/shiny/screen"
)
func main() {
args := os.Args
if len(args) < 2 {
log.Fatalln("Scenario file required")
}
w := LoadScenario(args[1])
w.MakeChannes()
w.BulkSend = false
w.SendUpdates = false
w.maxSenders = 300
log.Println("loaded scenario")
/*fmt.Println("pressed size:", w.GetWidth(), w.GetHeight())
for y := 0; y < w.GetHeight(); y++ {
for x := 0; x < w.GetWidth(); x++ {
if w.GetTile(x, y).Walkable() {
fmt.Print(" ")
} else {
fmt.Print("#")
}
}
fmt.Println()
}
fmt.Println()*/
driver.Main(func(s screen.Screen) {
r := SetupRender(s, w.GetImage(), &w.Regions, &w)
defer r.Release()
go simulate(&w, &r)
for r.Step() {
}
fmt.Println("EOL")
})
fmt.Println("Bottom")
}
func simulate(world *State, r *RenderState) {
//time.Sleep(5 * time.Second)
<-world.playPauseChan
log.Println("Simulation starting")
steps := 0
world.groups = make([]*Group, world.scenario.TotalGroups)
for i, _ := range world.groups {
world.groups[i] = &Group{
Individuals: make([]*Individual, 0),
}
}
// Set up parallel processing channels
channels := make([]chan map[*Individual]utils.OptionalFloat64, 0)
for i := 0; i < len(world.groups); i++ {
channel := make(chan map[*Individual]utils.OptionalFloat64)
channels = append(channels, channel)
}
var avg float64 = -1
for world.time.Before(world.scenario.End) {
t := time.Now()
world.CalcDensity()
// add more people until someone doesn't fit
for i := world.peopleAdded; i < world.scenario.TotalPeople; i++ {
indiv := world.AddRandom()
if indiv == nil {
break
}
groupIndex := rand.Intn(world.scenario.TotalGroups)
world.groups[groupIndex].Individuals = append(world.groups[groupIndex].Individuals, indiv)
}
//fmt.Println(steps, "Tick at", t)
// world.MoveAll()
// Get the desired positions for each of the individuals in each group in parallel
for i, group := range world.groups {
go group.Next(channels[i], world)
}
// Process each group as they come
for _, channel := range channels {
// Process them as they come in
result := <-channel
processMovementsForGroup(world, result)
}
r.SendEvent(UpdateEvent{World: world})
world.peopleAddedChan <- world.peopleAdded
world.peopleCurrentChan <- world.peopleCurrent
world.simulationTimeChan <- world.time
world.currentSendersChan <- world.currentSenders
world.totalSendsChan <- GetTotalUpdates()
//fmt.Println("people: ", people)
steps++
world.TickTime()
geometry.FlipTick()
// calculate simulation speed analytics
dt := time.Since(t).Nanoseconds()
if avg < 0 {
avg = float64(dt)
} else {
avg = 0.9*avg + 0.1*float64(dt)
}
if steps%500 == 0 {
fmt.Println("average tick time: ", avg/1000000000)
fmt.Println("sim time: ", world.time)
SendBulk()
}
select {
case <-world.playPauseChan:
<-world.playPauseChan
default:
}
}
fmt.Println("Ticker stopped")
}
func processMovementsForGroup(world *State, movements map[*Individual]utils.OptionalFloat64) {
for individual, direction := range movements {
theta, ok := direction.Value()
if !ok {
// If we aren't meant to move... don't
world.MoveIndividual(individual, 0, 0)
continue
}
world.MoveIndividual(individual, theta, individual.StepSize)
UpdateRegions(world, individual, world.time, world.BulkSend)
}
}