-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal_simulation.go
73 lines (61 loc) · 1.45 KB
/
terminal_simulation.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
package main
import (
"fmt"
"time"
)
func RunTerminalMultiLaneSimulation() {
config := DefaultGeneralLaneConfig()
simulation, err := initMultiLaneSimulation(config)
simulation.runningSimulation = true
if err != nil {
panic(err)
}
go RunGeneralSimulation(simulation)
for {
if !simulation.runningSimulation {
fmt.Println("MultiLane completed")
return
}
select {
case <-simulation.drawUpdateChan:
fmt.Println(simulation)
break
}
}
}
// RunTerminalSingleLaneSimulation simulates and prints the movement through a single lane
func RunTerminalSingleLaneSimulation(action bool) {
simulation := initSingleLaneSimulation(10)
simulation.runningSimulation = true
go RunSingleLaneSimulation(simulation)
if action {
for {
if !simulation.runningSimulation {
fmt.Println("SingleLaneSimulation completed")
return
}
select {
case <-simulation.drawUpdateChan:
fmt.Println(simulation)
break
}
}
return
}
RenderTerminalFPS(simulation)
}
// RenderTerminalFPS renders date to the screen after every fps. It may clutter up the screen and lag
func RenderTerminalFPS(simulation *SingleLaneSimulation) {
var _, renderStartTime, diff, sleep int64
_ = time.Now().UnixNano()
for {
renderStartTime = time.Now().UnixNano()
fmt.Println(simulation.String())
diff = time.Now().UnixNano() - renderStartTime
sleep = fpsn - diff
if sleep < 0 {
continue
}
time.Sleep(time.Duration(sleep) * time.Nanosecond)
}
}