-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgol.go
457 lines (380 loc) · 8.48 KB
/
cgol.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package main
import (
"fmt"
rl "github.com/gen2brain/raylib-go/raylib"
)
const bx = 100
const by = 100
var tickrate int = 10
const pixelscale int = 5
var updatepixels bool = false
func clamp32(number, min, max int32) int32 {
if number < min {
return min
} else if number > max {
return max
} else {
return number
}
}
func getMousePixelPos() (int, int) {
return int(clamp32(rl.GetMouseX()/int32(pixelscale), 0, bx-1)), int(clamp32(rl.GetMouseY()/int32(pixelscale), 0, by-1))
}
func clamp(number, min, max int) int {
if number < min {
return min
} else if number > max {
return max
} else {
return number
}
}
type contruct struct { // used for creating things that can be placed as preset constructs, such as gliders, etc
ctype string // "Still Life", "Spaceship", etc
name string // "Glider", "Block", etc
rotatable bool // for rotation, duh
positions [][2]int // stores the cell positions relative to the mouse position, so {1, -1} would be up and to the right of the mouse
}
func (c contruct) build(board [bx][by]bool) ([bx][by]bool, bool) {
out := board
for _, pos := range c.positions {
mx, my := getMousePixelPos()
px := pos[0]
py := pos[1]
if my+py > len(board)-1 || mx-px > len(board[0])-1 {
return board, true
}
if my+py < 0 || mx-px < 0 {
return board, true
}
out[my+py][mx-px] = true
}
return out, false
}
var constructs = []contruct{
// Still Lifes
{
"Still Life",
"Block",
true,
[][2]int{
{0, 0},
{1, -1},
{0, -1},
{1, 0},
},
},
{
"Still Life",
"Beehive",
true,
[][2]int{
{1, -1},
{1, 1},
{0, -1},
{0, 1},
{-1, 0},
{2, 0},
},
},
/*
{
"Still Life",
"Crab",
true,
[][2]int{
{-1, 1},
{-1, -1},
{0, 1},
{0, -1},
{1, 0},
{-2, 0},
},
},
*/
// Oscillators
{
"Oscillator",
"Blinker",
true,
[][2]int{
{0, 0},
{-1, 0},
{1, 0},
},
},
// Spaceships
{
"Spaceship",
"Glider",
true,
[][2]int{
{0, 0},
{1, -1},
{-1, 0},
{-1, -1},
{0, 1},
},
},
// Expanders
{
"Expander",
"Blinker(4)",
true,
[][2]int{
{0, 0},
{1, 0},
{0, -1},
{0, 1},
},
},
{
"Expander",
"Beehive(4)",
true,
[][2]int{
{0, 0},
{1, -1},
{1, 1},
{0, -1},
{0, 1},
{-1, 0},
{2, 0},
},
},
// Decayers
{
"Decayer",
"Tuning Fork",
true,
[][2]int{
{0, -2},
{1, -1},
{-1, -1},
{1, 0},
{-1, 0},
{1, 1},
{-1, 1},
{1, 2},
{-1, 2},
},
},
}
/*
type CellSetting struct {
x int
y int
state bool
}
*/
// taken from https://github.com/ThePrimeagen/game-of-life-vwm/blob/master/cmd/server.go
var dirs = [][]int{
{-1, -1},
{0, -1},
{1, -1},
{1, 0},
{1, 1},
{0, 1},
{-1, 1},
{-1, 0},
}
// taken from https://github.com/ThePrimeagen/game-of-life-vwm/blob/master/cmd/server.go
func getNeighborCount(state [by][bx]bool, x, y int) int {
count := 0
for _, d := range dirs {
row := y + d[0]
col := x + d[1]
if row < 0 || row >= len(state) {
continue
}
if col < 0 || col >= len(state[0]) {
continue
}
if state[row][col] {
count++
}
}
return count
}
// taken from https://github.com/ThePrimeagen/game-of-life-vwm/blob/master/cmd/server.go
func stepBoard(state [by][bx]bool) [by][bx]bool {
var out [by][bx]bool
for y, row := range state {
for x := range row {
out[y][x] = state[y][x]
count := getNeighborCount(state, x, y)
if count < 2 || count > 3 {
out[y][x] = false
} else if count == 3 {
out[y][x] = true
}
}
}
return out
}
var holdingShift bool = false
var constructIndex int = 0
var constructorMode bool = false
var windowX int32 = 1000
var windowY int32 = 650
func main() {
var savestate [by][bx]bool
var cansave = false
cannotLoadMessageTick := rl.NewColor(uint8(120), uint8(120), uint8(120), uint8(0))
LoadedMessageTick := rl.NewColor(uint8(120), uint8(120), uint8(120), uint8(0))
savedMessageTick := rl.NewColor(uint8(120), uint8(120), uint8(120), uint8(0))
cBuildFailedMessageTick := rl.NewColor(uint8(120), uint8(120), uint8(120), uint8(0))
/*// Create new parser object
parser := argparse.NewParser("clilexer", "lexes and prints the given string")
// Create string flag
boardxy := parser.IntList("xy", "boardxy", &argparse.Options{Required: true, Help: "dimentions of"})
// Parse input
err := parser.Parse(os.Args)
if err != nil || len(boardxy) < 2 {
// In case of error print error and print usage
// This can also be done by passing -h or --help flags
fmt.Print(parser.Usage(err))
}*/
var board [by][bx]bool
rl.InitWindow(windowX, windowY, "Conway's Game of Life")
defer rl.CloseWindow()
rl.SetTargetFPS(60)
var tick int = 0
rl.InitAudioDevice()
music_main := rl.LoadMusicStream("./Conways Game of Life.mp3")
pause := true
//rl.PlayMusicStream(music_main)
for !rl.WindowShouldClose() {
constructIndex = clamp(constructIndex, 0, len(constructs)-1)
tickrate = clamp(tickrate, 0, 60)
tick++
rl.UpdateMusicStream(music_main)
if rl.IsKeyPressed(rl.KeyP) {
constructorMode = !constructorMode
}
if rl.IsKeyPressed(rl.KeyM) {
pause = !pause
if pause {
rl.PauseMusicStream(music_main)
} else {
rl.PlayMusicStream(music_main)
}
}
if rl.IsKeyPressed(rl.KeyRight) {
if holdingShift {
oldCtype := constructs[constructIndex].ctype
for constructs[constructIndex].ctype == oldCtype {
if constructIndex+1 > len(constructs)-1 {
break
}
constructIndex++
}
} else {
constructIndex = clamp(constructIndex+1, 0, len(constructs)-1)
}
} else if rl.IsKeyPressed(rl.KeyLeft) {
if holdingShift {
oldCtype := constructs[constructIndex].ctype
for constructs[constructIndex].ctype == oldCtype {
if constructIndex-1 < 0 {
break
}
constructIndex--
}
} else {
constructIndex = clamp(constructIndex-1, 0, len(constructs)-1)
}
}
if rl.IsKeyPressed(rl.KeySpace) && updatepixels {
updatepixels = false
} else if rl.IsKeyPressed(rl.KeySpace) && !updatepixels {
updatepixels = true
}
if rl.IsKeyDown(rl.KeyLeftShift) || rl.IsKeyDown(rl.KeyRightShift) {
holdingShift = true
} else {
holdingShift = false
}
if rl.IsKeyPressed(rl.KeyS) {
cansave = true
savestate = board
savedMessageTick.A = 150
}
if rl.IsKeyPressed(rl.KeyL) && cansave && savestate != board {
board = savestate
//cansave = false
LoadedMessageTick.A = 150
} else if rl.IsKeyPressed(rl.KeyL) && (!cansave || savestate == board) {
cannotLoadMessageTick.A = 150
}
if rl.IsKeyPressed(rl.KeyUp) {
tickrate++
} else if rl.IsKeyPressed(rl.KeyDown) {
tickrate--
}
if rl.IsMouseButtonPressed(rl.MouseButtonLeft) {
if constructorMode {
tempboard, failed := constructs[constructIndex].build(board)
if failed {
cBuildFailedMessageTick.A = 150
} else {
board = tempboard
}
} else {
mx, my := getMousePixelPos()
if !board[my][mx] {
board[my][mx] = true
}
}
}
if rl.IsMouseButtonPressed(rl.MouseButtonRight) {
mx, my := getMousePixelPos()
if board[my][mx] {
board[my][mx] = false
}
}
if updatepixels && tick > tickrate {
board = stepBoard(board)
tick = 0
}
rl.BeginDrawing()
rl.ClearBackground(rl.Black)
//rl.DrawText("Congrats! You created your first window!", 190, 200, 20, rl.White)
for y, b1 := range board {
for x, b2 := range b1 {
if !b2 {
continue
}
pixcol := rl.White
rl.DrawRectangle(int32(x*pixelscale), int32(y*pixelscale), int32(pixelscale), int32(pixelscale), pixcol)
}
}
if !updatepixels {
rl.DrawText("currently paused, press Space to unpause", 2, 5, 20, rl.Gray)
}
if constructorMode {
rl.DrawText(fmt.Sprintf("current construct is '%s'('%s')", constructs[constructIndex].name, constructs[constructIndex].ctype), 2, 50, 15, rl.Gray)
}
rl.DrawText(fmt.Sprint(tickrate), 2, 25, 20, rl.Gray)
rl.DrawText("current state saved", 2, windowY-30, 20, savedMessageTick)
rl.DrawText("loaded saved state", 2, windowY-60, 20, LoadedMessageTick)
rl.DrawText("unable to load save", 2, windowY-90, 20, cannotLoadMessageTick)
rl.DrawText("unable to build construct", 2, windowY-120, 20, cBuildFailedMessageTick)
if savedMessageTick.A > 0 {
savedMessageTick.A--
}
if cannotLoadMessageTick.A > 0 {
cannotLoadMessageTick.A--
}
if LoadedMessageTick.A > 0 {
LoadedMessageTick.A--
}
if cBuildFailedMessageTick.A > 0 {
cBuildFailedMessageTick.A--
}
rl.EndDrawing()
}
//rl.UnloadMusicStream(music_main)
//rl.CloseAudioDevice()
//rl.CloseWindow()
}