2025-11-13 21:30:44 +01:00
|
|
|
package cmd
|
2024-05-23 14:27:42 +02:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2024-05-24 17:53:38 +02:00
|
|
|
"math/rand"
|
2024-05-23 14:27:42 +02:00
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
2024-05-30 10:11:44 +02:00
|
|
|
|
2025-11-13 21:30:44 +01:00
|
|
|
"codeberg.org/scip/golsky/rle"
|
2024-05-23 14:27:42 +02:00
|
|
|
)
|
|
|
|
|
|
2024-07-16 19:07:51 +02:00
|
|
|
// equals grid height, is being used to access grid elements and must be global
|
|
|
|
|
var STRIDE int
|
2024-06-14 17:53:58 +02:00
|
|
|
|
2024-07-15 14:21:21 +02:00
|
|
|
type Neighbor struct {
|
|
|
|
|
X, Y int
|
2024-06-14 17:53:58 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-23 14:27:42 +02:00
|
|
|
type Grid struct {
|
2024-07-16 19:07:51 +02:00
|
|
|
Data []uint8
|
|
|
|
|
NeighborCount []int
|
|
|
|
|
Neighbors [][]Neighbor
|
2024-07-15 14:21:21 +02:00
|
|
|
Empty bool
|
|
|
|
|
Config *Config
|
2024-07-16 19:33:06 +02:00
|
|
|
Counter func(x, y int) uint8
|
2024-05-23 14:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-23 15:04:08 +02:00
|
|
|
// Create new empty grid and allocate Data according to provided dimensions
|
2024-06-14 17:53:58 +02:00
|
|
|
func NewGrid(config *Config) *Grid {
|
2024-07-16 19:07:51 +02:00
|
|
|
STRIDE = config.Height
|
|
|
|
|
if config.Width > config.Height {
|
|
|
|
|
STRIDE = config.Width
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size := STRIDE * STRIDE
|
|
|
|
|
|
2024-05-23 15:04:08 +02:00
|
|
|
grid := &Grid{
|
2024-07-16 19:07:51 +02:00
|
|
|
Data: make([]uint8, size),
|
|
|
|
|
NeighborCount: make([]int, size),
|
|
|
|
|
Neighbors: make([][]Neighbor, size),
|
2024-07-15 14:21:21 +02:00
|
|
|
Empty: config.Empty,
|
|
|
|
|
Config: config,
|
2024-05-23 15:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-14 17:53:58 +02:00
|
|
|
// first setup the cells
|
|
|
|
|
for y := 0; y < config.Height; y++ {
|
|
|
|
|
for x := 0; x < config.Width; x++ {
|
2024-07-16 19:07:51 +02:00
|
|
|
grid.Data[y+STRIDE*x] = 0
|
2024-06-14 17:53:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-15 14:21:21 +02:00
|
|
|
// in a second pass, collect positions to the neighbors of each cell
|
2024-06-14 17:53:58 +02:00
|
|
|
for y := 0; y < config.Height; y++ {
|
|
|
|
|
for x := 0; x < config.Width; x++ {
|
|
|
|
|
grid.SetupNeighbors(x, y)
|
|
|
|
|
}
|
2024-05-23 15:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-16 19:33:06 +02:00
|
|
|
if grid.Config.Wrap {
|
|
|
|
|
grid.Counter = grid.CountNeighborsWrap
|
|
|
|
|
} else {
|
|
|
|
|
grid.Counter = grid.CountNeighbors
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-23 15:04:08 +02:00
|
|
|
return grid
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-14 17:53:58 +02:00
|
|
|
func (grid *Grid) SetupNeighbors(x, y int) {
|
|
|
|
|
idx := 0
|
|
|
|
|
|
2024-07-15 14:21:21 +02:00
|
|
|
var neighbors []Neighbor
|
|
|
|
|
|
2024-06-14 17:53:58 +02:00
|
|
|
for nbgY := -1; nbgY < 2; nbgY++ {
|
|
|
|
|
for nbgX := -1; nbgX < 2; nbgX++ {
|
|
|
|
|
var col, row int
|
|
|
|
|
|
|
|
|
|
if grid.Config.Wrap {
|
|
|
|
|
// In wrap mode we look at all the 8 neighbors surrounding us.
|
|
|
|
|
// In case we are on an edge we'll look at the neighbor on the
|
|
|
|
|
// other side of the grid, thus wrapping lookahead around
|
|
|
|
|
// using the mod() function.
|
|
|
|
|
col = (x + nbgX + grid.Config.Width) % grid.Config.Width
|
|
|
|
|
row = (y + nbgY + grid.Config.Height) % grid.Config.Height
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// In traditional grid mode the edges are deadly
|
|
|
|
|
if x+nbgX < 0 || x+nbgX >= grid.Config.Width || y+nbgY < 0 || y+nbgY >= grid.Config.Height {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
col = x + nbgX
|
|
|
|
|
row = y + nbgY
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if col == x && row == y {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-15 14:21:21 +02:00
|
|
|
neighbors = append(neighbors, Neighbor{X: col, Y: row})
|
2024-07-16 19:07:51 +02:00
|
|
|
grid.NeighborCount[y+STRIDE*x]++
|
2024-06-14 17:53:58 +02:00
|
|
|
idx++
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-15 14:21:21 +02:00
|
|
|
|
2024-07-16 19:07:51 +02:00
|
|
|
grid.Neighbors[y+STRIDE*x] = neighbors
|
2024-06-14 17:53:58 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-16 19:33:06 +02:00
|
|
|
func (grid *Grid) CountNeighborsWrap(x, y int) uint8 {
|
|
|
|
|
var sum uint8
|
|
|
|
|
|
|
|
|
|
for nbgX := -1; nbgX < 2; nbgX++ {
|
|
|
|
|
for nbgY := -1; nbgY < 2; nbgY++ {
|
|
|
|
|
var col, row int
|
|
|
|
|
|
|
|
|
|
// In wrap mode we look at all the 8 neighbors surrounding us.
|
|
|
|
|
// In case we are on an edge we'll look at the neighbor on the
|
|
|
|
|
// other side of the grid, thus wrapping lookahead around
|
|
|
|
|
// using the mod() function.
|
|
|
|
|
col = (x + nbgX + grid.Config.Width) % grid.Config.Width
|
|
|
|
|
row = (y + nbgY + grid.Config.Height) % grid.Config.Height
|
|
|
|
|
|
|
|
|
|
sum += grid.Data[row+STRIDE*col]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// don't count ourselfes though
|
|
|
|
|
sum -= grid.Data[y+STRIDE*x]
|
|
|
|
|
|
|
|
|
|
return sum
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-15 12:06:17 +02:00
|
|
|
func (grid *Grid) CountNeighbors(x, y int) uint8 {
|
2024-07-16 19:33:06 +02:00
|
|
|
var sum uint8
|
|
|
|
|
|
|
|
|
|
width := grid.Config.Width
|
|
|
|
|
height := grid.Config.Height
|
|
|
|
|
|
|
|
|
|
for nbgX := -1; nbgX < 2; nbgX++ {
|
|
|
|
|
for nbgY := -1; nbgY < 2; nbgY++ {
|
|
|
|
|
var col, row int
|
|
|
|
|
|
|
|
|
|
xnbgX := x + nbgX
|
|
|
|
|
ynbgY := y + nbgY
|
|
|
|
|
|
|
|
|
|
// In traditional grid mode the edges are deadly
|
|
|
|
|
if xnbgX < 0 || xnbgX >= width || ynbgY < 0 || ynbgY >= height {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
col = xnbgX
|
|
|
|
|
row = ynbgY
|
|
|
|
|
|
|
|
|
|
sum += grid.Data[row+STRIDE*col]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// don't count ourselfes though
|
|
|
|
|
sum -= grid.Data[y+STRIDE*x]
|
|
|
|
|
|
|
|
|
|
return sum
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// count the living neighbors of a cell
|
|
|
|
|
func (grid *Grid) _CountNeighbors(x, y int) uint8 {
|
2024-07-15 14:21:21 +02:00
|
|
|
var count uint8
|
|
|
|
|
|
2024-07-16 19:07:51 +02:00
|
|
|
pos := y + STRIDE*x
|
|
|
|
|
neighbors := grid.Neighbors[pos]
|
|
|
|
|
neighborCount := grid.NeighborCount[pos]
|
|
|
|
|
|
|
|
|
|
for idx := 0; idx < neighborCount; idx++ {
|
|
|
|
|
neighbor := neighbors[idx]
|
|
|
|
|
count += grid.Data[neighbor.Y+STRIDE*neighbor.X]
|
2024-07-15 14:21:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return count
|
2024-06-14 17:53:58 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-30 10:23:31 +02:00
|
|
|
// Create a new 1:1 instance
|
2024-05-23 15:04:08 +02:00
|
|
|
func (grid *Grid) Clone() *Grid {
|
|
|
|
|
newgrid := &Grid{}
|
|
|
|
|
|
2024-06-14 17:53:58 +02:00
|
|
|
newgrid.Config = grid.Config
|
2024-05-23 15:04:08 +02:00
|
|
|
newgrid.Data = grid.Data
|
|
|
|
|
|
|
|
|
|
return newgrid
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-30 10:23:31 +02:00
|
|
|
// copy data
|
2024-07-16 19:07:51 +02:00
|
|
|
// func (grid *Grid) Copy(other *Grid) {
|
|
|
|
|
// for y := range grid.Data {
|
|
|
|
|
// for x := range grid.Data[y] {
|
|
|
|
|
// other.Data[y+STRIDE*x] = grid.Data[y+STRIDE*x]
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2024-05-30 10:23:31 +02:00
|
|
|
|
2024-05-30 10:11:44 +02:00
|
|
|
// delete all contents
|
2024-07-16 19:07:51 +02:00
|
|
|
// func (grid *Grid) Clear() {
|
|
|
|
|
// for y := range grid.Data {
|
|
|
|
|
// for x := range grid.Data[y] {
|
|
|
|
|
// grid.Data[y+STRIDE*x] = 0
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2024-05-24 17:53:38 +02:00
|
|
|
|
2024-05-30 10:11:44 +02:00
|
|
|
// initialize with random life cells using the given density
|
2024-05-30 10:23:31 +02:00
|
|
|
func (grid *Grid) FillRandom() {
|
2024-05-26 12:29:43 +02:00
|
|
|
if !grid.Empty {
|
2024-07-16 19:07:51 +02:00
|
|
|
for y := 0; y < grid.Config.Height; y++ {
|
|
|
|
|
for x := 0; x < grid.Config.Width; x++ {
|
2024-06-14 17:53:58 +02:00
|
|
|
if rand.Intn(grid.Config.Density) == 1 {
|
2024-07-16 19:07:51 +02:00
|
|
|
grid.Data[y+STRIDE*x] = 1
|
2024-05-24 17:53:38 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-01 20:22:28 +02:00
|
|
|
func (grid *Grid) Dump() {
|
2024-06-14 17:53:58 +02:00
|
|
|
for y := 0; y < grid.Config.Height; y++ {
|
|
|
|
|
for x := 0; x < grid.Config.Width; x++ {
|
2024-07-16 19:07:51 +02:00
|
|
|
if grid.Data[y+STRIDE*x] == 1 {
|
2024-06-01 20:22:28 +02:00
|
|
|
fmt.Print("XX")
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Print(" ")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fmt.Println()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-30 10:11:44 +02:00
|
|
|
// initialize using a given RLE pattern
|
|
|
|
|
func (grid *Grid) LoadRLE(pattern *rle.RLE) {
|
|
|
|
|
if pattern != nil {
|
2024-06-14 17:53:58 +02:00
|
|
|
startX := (grid.Config.Width / 2) - (pattern.Width / 2)
|
|
|
|
|
startY := (grid.Config.Height / 2) - (pattern.Height / 2)
|
2024-05-30 10:11:44 +02:00
|
|
|
var y, x int
|
2024-05-23 14:27:42 +02:00
|
|
|
|
2024-05-30 10:11:44 +02:00
|
|
|
for rowIndex, patternRow := range pattern.Pattern {
|
|
|
|
|
for colIndex := range patternRow {
|
|
|
|
|
if pattern.Pattern[rowIndex][colIndex] > 0 {
|
|
|
|
|
x = colIndex + startX
|
|
|
|
|
y = rowIndex + startY
|
|
|
|
|
|
2024-07-16 19:07:51 +02:00
|
|
|
grid.Data[y+STRIDE*x] = 1
|
2024-05-30 10:11:44 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-01 20:22:28 +02:00
|
|
|
|
|
|
|
|
//grid.Dump()
|
2024-05-30 10:11:44 +02:00
|
|
|
}
|
2024-05-26 20:26:13 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-04 13:49:06 +02:00
|
|
|
// load a lif file parameters like R and P are not supported yet
|
2024-06-04 14:09:40 +02:00
|
|
|
func LoadLIF(filename string) (*rle.RLE, error) {
|
2024-05-23 14:27:42 +02:00
|
|
|
fd, err := os.Open(filename)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(fd)
|
|
|
|
|
|
|
|
|
|
scanner.Split(bufio.ScanLines)
|
|
|
|
|
|
2024-06-04 13:49:06 +02:00
|
|
|
gothead := false
|
|
|
|
|
|
2024-06-04 14:09:40 +02:00
|
|
|
grid := &rle.RLE{}
|
2024-05-23 14:27:42 +02:00
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
2024-06-04 14:09:40 +02:00
|
|
|
line := scanner.Text()
|
|
|
|
|
items := strings.Split(line, "")
|
2024-05-23 14:27:42 +02:00
|
|
|
|
2024-06-04 14:09:40 +02:00
|
|
|
if len(items) < 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if strings.Contains(line, "# r") {
|
|
|
|
|
parts := strings.Split(line, " ")
|
|
|
|
|
if len(parts) == 2 {
|
|
|
|
|
grid.Rule = parts[1]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if items[0] == "#" {
|
2024-06-04 13:49:06 +02:00
|
|
|
if gothead {
|
|
|
|
|
break
|
2024-05-23 14:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-04 13:49:06 +02:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gothead = true
|
2024-05-23 14:27:42 +02:00
|
|
|
|
2024-06-04 14:09:40 +02:00
|
|
|
row := make([]int, len(items))
|
2024-06-04 13:49:06 +02:00
|
|
|
|
|
|
|
|
for idx, item := range items {
|
|
|
|
|
switch item {
|
|
|
|
|
case ".":
|
|
|
|
|
row[idx] = 0
|
|
|
|
|
case "o":
|
|
|
|
|
fallthrough
|
|
|
|
|
case "*":
|
|
|
|
|
row[idx] = 1
|
|
|
|
|
default:
|
|
|
|
|
return nil, errors.New("cells must be . or o")
|
|
|
|
|
}
|
2024-05-23 14:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
2024-06-04 14:09:40 +02:00
|
|
|
grid.Pattern = append(grid.Pattern, row)
|
2024-05-23 14:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sanity check the grid
|
|
|
|
|
explen := 0
|
|
|
|
|
rows := 0
|
2024-05-23 15:04:08 +02:00
|
|
|
first := true
|
2024-06-04 14:09:40 +02:00
|
|
|
for _, row := range grid.Pattern {
|
2024-05-23 14:27:42 +02:00
|
|
|
length := len(row)
|
|
|
|
|
|
|
|
|
|
if first {
|
|
|
|
|
explen = length
|
2024-05-23 15:04:08 +02:00
|
|
|
first = false
|
2024-05-23 14:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if explen != length {
|
2024-05-30 10:11:44 +02:00
|
|
|
return nil, fmt.Errorf(
|
|
|
|
|
fmt.Sprintf("all rows must be in the same length, got: %d, expected: %d",
|
|
|
|
|
length, explen))
|
2024-05-23 14:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rows++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
grid.Width = explen
|
|
|
|
|
grid.Height = rows
|
|
|
|
|
|
|
|
|
|
return grid, nil
|
|
|
|
|
}
|
2024-05-30 10:11:44 +02:00
|
|
|
|
2024-06-04 13:49:06 +02:00
|
|
|
// save the contents of the whole grid as a simple lif alike
|
|
|
|
|
// file. One line per row, 0 for dead and 1 for life cell.
|
|
|
|
|
// file format: https://conwaylife.com/wiki/Life_1.05
|
|
|
|
|
func (grid *Grid) SaveState(filename, rule string) error {
|
|
|
|
|
file, err := os.Create(filename)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to open state file: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
|
|
fmt.Fprintf(file, "#Life 1.05\n#R %s\n#D golsky state file\n#P -1 -1\n", rule)
|
|
|
|
|
|
2024-07-16 19:07:51 +02:00
|
|
|
for y := 0; y < grid.Config.Height; y++ {
|
|
|
|
|
for x := 0; x < grid.Config.Width; x++ {
|
2024-06-06 18:58:31 +02:00
|
|
|
row := "."
|
2024-07-16 19:07:51 +02:00
|
|
|
if grid.Data[y+STRIDE*x] == 1 {
|
2024-06-06 18:58:31 +02:00
|
|
|
row = "o"
|
2024-06-04 13:49:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err := file.WriteString(row)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed to write to state file: %w", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
file.WriteString("\n")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-30 10:11:44 +02:00
|
|
|
// generate filenames for dumps
|
|
|
|
|
func GetFilename(generations int64) string {
|
|
|
|
|
now := time.Now()
|
2024-06-04 13:49:06 +02:00
|
|
|
return fmt.Sprintf("dump-%s-%d.lif", now.Format("20060102150405"), generations)
|
2024-05-30 10:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetFilenameRLE(generations int64) string {
|
|
|
|
|
now := time.Now()
|
|
|
|
|
return fmt.Sprintf("rect-%s-%d.rle", now.Format("20060102150405"), generations)
|
|
|
|
|
}
|