-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
273 lines (227 loc) · 6.18 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Initialization and general application loop
package main
import (
"encoding/csv"
"flag"
"fmt"
"image"
"image/png"
"log"
"math/rand"
"os"
"runtime/pprof"
"time"
"github.com/AllenDang/giu"
)
func init() {
rand.New(rand.NewSource(time.Now().UnixNano()))
image.RegisterFormat("png", "png", png.Decode, png.DecodeConfig)
}
var mapResize = 4 //TODO: calculate it instead
type Speed int
const (
Paused Speed = iota
Custom
Unlimited
)
type Sim struct {
mapGrid *mapGrid
humanGrid *HumanGrid
}
type Anaxim struct {
simulation *Sim
mapImage *image.RGBA
mapTexture *giu.Texture
mapWidth, mapHeight int
speed Speed
speedCustomTPS float32
previousTick time.Time
lastTick time.Time
lastRefresh time.Time
speedWidgets *SpeedWidgets
howeringOverCellAt image.Point
howeringOverCellCanvasPoint image.Point
inspectingCellAt image.Point
inspectingCell *humanCell
inspectingCanvasPoint image.Point
}
func (s *Sim) Prerun(generations int) {
for s.humanGrid.generation < generations {
s.humanGrid.Update()
gen := s.humanGrid.generation
if gen%(generations/50) == 0 {
fmt.Printf("Prerunning simulation... %d/%d (%d%%)\n",
gen, generations, int(100*(float32(gen)/float32(generations))))
}
}
}
func (s *Sim) Update() error {
return s.humanGrid.Update()
}
func NewAnaxim(s *Sim) *Anaxim {
mapImg :=
image.NewRGBA(image.Rect(0, 0, s.mapGrid.width, s.mapGrid.height))
UpdateGridImage(s, mapImg, image.Pt(-1, -1))
a := &Anaxim{
simulation: s,
mapImage: mapImg,
mapWidth: s.mapGrid.width,
mapHeight: s.mapGrid.height,
speed: Unlimited,
lastTick: time.Now(),
lastRefresh: time.Now(),
speedCustomTPS: 1,
howeringOverCellAt: image.Pt(0, 0),
howeringOverCellCanvasPoint: image.Pt(0, 0),
inspectingCellAt: image.Pt(-1, -1),
inspectingCell: nil,
}
return a
}
func (a *Anaxim) TimeSinceLastTick() time.Duration {
return time.Since(a.lastTick)
}
func (a *Anaxim) TimeBetweenLastTicks() time.Duration {
return a.lastTick.Sub(a.previousTick)
}
func (a *Anaxim) TimeSinceLastRefresh() time.Duration {
return time.Since(a.lastRefresh)
}
func (a *Anaxim) WriteRecord() {
gen := a.simulation.humanGrid.generation
UpdateGridImage(a.simulation, a.mapImage, a.inspectingCellAt)
pngFile, fileerr := os.Create(
fmt.Sprintf("%v/gen_%v.png", recordFolder, gen))
if fileerr != nil {
log.Fatalf("While creating png file: %v", fileerr)
}
defer pngFile.Close()
if fileerr = png.Encode(pngFile, a.mapImage); fileerr != nil {
log.Fatalf("While encoding png file: %v", fileerr)
}
data := []string{
pngFile.Name(),
fmt.Sprint(gen),
fmt.Sprint(a.simulation.humanGrid.globalPop),
fmt.Sprint(a.simulation.humanGrid.biggestPopCell.population),
fmt.Sprintf("(%d,%d)",
a.simulation.humanGrid.biggestPopCell.x,
a.simulation.humanGrid.biggestPopCell.y,
),
}
if csverr := csvWriter.Write(data); csverr != nil {
log.Fatal(csverr)
}
csvWriter.Flush()
if csverr := csvWriter.Error(); csverr != nil {
log.Fatal(csverr)
}
}
func (a *Anaxim) Update() {
err := a.simulation.Update()
if err != nil {
log.Fatalf("Simulation error: %v", err)
}
// Add csv record if needed
if recordInterval != -1 &&
(a.simulation.humanGrid.generation == 1 ||
a.simulation.humanGrid.generation%recordInterval == 0) {
a.WriteRecord()
}
//Refresh image and info only now and then
if a.TimeSinceLastRefresh() > time.Second/24 {
a.updateMapTexture()
}
a.previousTick = a.lastTick
a.lastTick = time.Now()
}
func (a *Anaxim) loop() {
a.createLayout()
}
func (a *Anaxim) updateMapTexture() {
UpdateGridImage(a.simulation, a.mapImage, a.inspectingCellAt)
giu.EnqueueNewTextureFromRgba(
a.mapImage,
func(tex *giu.Texture) {
a.mapTexture = tex
})
giu.Update()
a.lastRefresh = time.Now()
}
func (a *Anaxim) runSim() {
for {
switch a.speed {
case Paused:
return
case Unlimited:
a.Update()
case Custom:
a.Update()
time.Sleep(time.Duration(
(1 - a.speedCustomTPS) * float32(time.Second)))
}
}
}
var csvWriter *csv.Writer
var recordFolder string
var recordInterval int
func main() {
mapPath := flag.String("mappath", "./Maps/oldworld.png", "Path to the map PNG file")
prerunGenerations := flag.Int("prerun", 0, "Generations to simulate before launching, min 50")
flag.IntVar(&mapResize, "mapsize", 4, "How much to resize the map (needs to be between 1 and 8)")
cpuprof := flag.Bool("pprof", false, "Enable cpu profiling")
flag.IntVar(&recordInterval, "ri", -1,
"Record interval, how often to record the simulation data in a csv file and images")
flag.Parse()
if recordInterval > -1 {
name := time.Now().Format(time.RFC3339)
recordFolder = "record_" + name
err := os.Mkdir(recordFolder, 0755)
if err != nil {
log.Fatalf("When creating record folder: %v", err)
}
csvFile, err := os.Create(
recordFolder + "/" + name + ".csv")
if err != nil {
log.Fatalf("When creating csv file: %v", err)
}
csvWriter = csv.NewWriter(csvFile)
csvWriter.Write([]string{"Map", "Generation", "World pop", "Record pop", "Record at"})
defer csvFile.Close()
}
if *cpuprof {
f, err := os.Create("anaxim.prof")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if mapResize < 1 || mapResize > 8 {
log.Fatalf("Flag mapsize out of range [1,8]: %d", mapResize)
}
preloadedMap, err := NewMapGrid(*mapPath)
if err != nil {
log.Fatalf("Error creating MapGrid: %v", err)
}
mapWidth, mapHeight := preloadedMap.width, preloadedMap.height
s := &Sim{
mapGrid: preloadedMap,
humanGrid: NewHumanGrid(*preloadedMap, mapWidth, mapHeight,
(mapWidth*mapHeight)/5000),
}
if *prerunGenerations > 49 {
s.Prerun(*prerunGenerations)
}
anaxim := NewAnaxim(s)
wnd := giu.NewMasterWindow("Anaxim",
mapWidth*mapResize+leftColumnWidth+20,
mapHeight*mapResize+100,
giu.MasterWindowFlagsFloating)
anaxim.updateMapTexture()
anaxim.initUI()
go anaxim.runSim()
giu.Context.GetRenderer().
SetTextureMagFilter(giu.TextureFilterNearest)
wnd.Run(anaxim.loop)
}