-
Notifications
You must be signed in to change notification settings - Fork 0
/
life.go
225 lines (187 loc) · 5.1 KB
/
life.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
/*Conway's Game Of Life
Uses maps/dicts for storage, so speed/memory use proportional to number of live cells.
in/out storage using png files.
save sequences of images for making movies.
*/
// example arguments: -i="./media/glider" -pipeMovie -wrap -size=200 -ticks=500
// TODO add terminal gocli display?
package main
import (
"os"
)
import "flag"
import "time"
import "log"
import "strconv"
import "path/filepath"
import "github.com/splace/fsflags"
type loc struct{ x, y int }
type surroundingLiveCellCounter uint8
var liveCells map[loc]surroundingLiveCellCounter
var deadCellsNextToLiveCell map[loc]surroundingLiveCellCounter
var limit int
var wrap bool
func main() {
var source fsflags.FileValue
flag.Var(&source, "i", "source for the starting cell pattern, encoded in PNG image.(default:<Stdin>)")
flag.Var(&source, "input", flag.Lookup("i").Usage)
var sink fsflags.CreateFileValue
flag.Var(&sink, "o", "file for encoding result cell pattern, PNG image.(default:Stdout)")
flag.Var(&sink, "output", flag.Lookup("o").Usage)
var cycles uint
flag.UintVar(&cycles, "ticks", 1, "Ticks/Cycles")
var logInterval time.Duration
flag.DurationVar(&logInterval, "interval", time.Second, "time between log status reports")
var wrap bool
flag.BoolVar(&wrap, "w", false, "sets arena to (s)ize.")
flag.BoolVar(&wrap, "wrap", false, flag.Lookup("w").Usage)
var pipeMovie bool
flag.BoolVar(&pipeMovie, "p", false,"send snapshot images to Stdout.")
flag.BoolVar(&pipeMovie, "pipeMovie",false, flag.Lookup("p").Usage)
var movie fsflags.NewOverwriteDirValue
flag.Var(&movie, "m", "directory for snapshot frames, PNG images.")
flag.Var(&movie, "movie", flag.Lookup("m").Usage)
var size uint
flag.UintVar(&size, "s", 32,"size of snapshots.")
flag.UintVar(&size, "size",32,flag.Lookup("s").Usage)
limit=int(size/2)
var ticksSnapshot uint
flag.UintVar(&ticksSnapshot, "f", 1,"ticks for each snapshot image.")
flag.UintVar(&ticksSnapshot, "frameTicks", 1,flag.Lookup("f").Usage)
var help bool
flag.BoolVar(&help, "h", false, "display help/usage.")
flag.BoolVar(&help, "help", false, flag.Lookup("h").Usage)
var logToo fsflags.CreateFileValue
flag.Var(&logToo, "log", "progress log destination.(default:Stderr)")
flag.Parse()
if help {
flag.PrintDefaults()
os.Exit(0)
}
if pipeMovie {
movie.File=os.Stdout
}
if logToo.File == nil {
logToo.File = os.Stderr
}
var c uint
doLog := time.NewTicker(logInterval)
go func() {
for _ = range doLog.C {
log.Printf("\t#%d\talive:%d", c, len(liveCells))
}
}()
if source.File == nil {
var err error
log.Printf("Loading:<<StdIn>>")
liveCells, err = DecodeCellsFromImages(os.Stdin)
if err != nil {
panic(err)
}
} else {
var err error
log.Printf("Loading:%q", &source)
liveCells, err = DecodeCellsFromImages(source)
if err != nil {
panic(err)
}
}
if movie.File!=nil {
log.Printf("Saving snapshot images to :%q", &movie)
}
log.Printf("\t#%d\talive:%d", 0, len(liveCells))
switch movie.File{
case nil:
for c = 0; c < cycles; c++ {
if anyChanged:=tick(); !anyChanged {
log.Print("Unchanging")
break
}
}
case os.Stdout:
for c = 0; c < cycles; c++ {
if anyChanged:=tick(); !anyChanged {
log.Print("Unchanging")
break
}
if c%ticksSnapshot ==0 {
EncodeCellsAsSizedImage(os.Stdout, liveCells,size)
}
}
default:
for c = 0; c < cycles; c++ {
if anyChanged:=tick(); !anyChanged {
log.Print("Unchanging")
break
}
if c%ticksSnapshot ==0 {
frameFile,err:=os.Create(filepath.Join(movie.File.Name(),strconv.FormatUint(uint64(c),10)+".png"))
if err!=nil{
log.Printf("\t#%d\tUnable to save frame:%s", c,err)
}else{
EncodeCellsAsSizedImage(frameFile, liveCells,size)
frameFile.Close()
}
}
}
}
log.Printf("\t#%d\talive:%d", c, len(liveCells))
doLog.Stop()
if sink.File == nil {
log.Printf("Saving:<<StdOut>>")
if wrap {
EncodeCellsAsSizedImage(os.Stdout, liveCells,size)
}else{
EncodeCellsAsImage(os.Stdout, liveCells)
}
} else {
log.Printf("Saving:%q", &sink)
if wrap {
EncodeCellsAsSizedImage(&sink, liveCells,size)
}else{
EncodeCellsAsImage(&sink, liveCells)
}
}
}
func tick() (activity bool) {
deadCellsNextToLiveCell = make(map[loc]surroundingLiveCellCounter)
var count surroundingLiveCellCounter
var l loc
for l = range liveCells {
liveCells[l] = atOffset(l, 1, 0) + atOffset(l, 1, 1) + atOffset(l, 0, 1) + atOffset(l, -1, 1) + atOffset(l, -1, 0) + atOffset(l, -1, -1) + atOffset(l, 0, -1) + atOffset(l, 1, -1)
}
for l, count = range liveCells {
if count > 3 || count < 2 {
delete(liveCells, l)
activity = true
}
}
for l, count = range deadCellsNextToLiveCell {
if count == 3 {
liveCells[l] = 0
activity = true
}
}
return
}
func atOffset(l loc, dx, dy int8) surroundingLiveCellCounter {
l.x += int(dx)
l.y += int(dy)
if wrap {
l.x+=limit
l.y+=limit
l.x %=limit
l.y %=limit
l.x-=limit
l.y-=limit
}
if _, in := liveCells[l]; in {
return 1
}
if c, in := deadCellsNextToLiveCell[l]; in {
deadCellsNextToLiveCell[l] = c + 1
} else {
deadCellsNextToLiveCell[l] = 1
}
return 0
}