using only one dimensional grid by calculating y+STRIDE*x

This commit is contained in:
2024-07-16 19:07:51 +02:00
parent e8ed283233
commit 4074debdbf
3 changed files with 69 additions and 54 deletions

View File

@@ -12,46 +12,42 @@ import (
"github.com/tlinden/golsky/rle" "github.com/tlinden/golsky/rle"
) )
// func (cell *Cell) Count() uint8 { // equals grid height, is being used to access grid elements and must be global
// var count uint8 var STRIDE int
// for idx := 0; idx < cell.NeighborCount; idx++ {
// count += cell.Neighbors[idx].State
// }
// return count
// }
type Neighbor struct { type Neighbor struct {
X, Y int X, Y int
} }
type Grid struct { type Grid struct {
Data [][]uint8 Data []uint8
NeighborCount [][]int NeighborCount []int
Neighbors [][][]Neighbor Neighbors [][]Neighbor
Empty bool Empty bool
Config *Config Config *Config
} }
// Create new empty grid and allocate Data according to provided dimensions // Create new empty grid and allocate Data according to provided dimensions
func NewGrid(config *Config) *Grid { func NewGrid(config *Config) *Grid {
STRIDE = config.Height
if config.Width > config.Height {
STRIDE = config.Width
}
size := STRIDE * STRIDE
grid := &Grid{ grid := &Grid{
Data: make([][]uint8, config.Height), Data: make([]uint8, size),
NeighborCount: make([][]int, config.Height), NeighborCount: make([]int, size),
Neighbors: make([][][]Neighbor, config.Height), Neighbors: make([][]Neighbor, size),
Empty: config.Empty, Empty: config.Empty,
Config: config, Config: config,
} }
// first setup the cells // first setup the cells
for y := 0; y < config.Height; y++ { for y := 0; y < config.Height; y++ {
grid.Data[y] = make([]uint8, config.Width)
grid.Neighbors[y] = make([][]Neighbor, config.Width)
grid.NeighborCount[y] = make([]int, config.Width)
for x := 0; x < config.Width; x++ { for x := 0; x < config.Width; x++ {
grid.Data[y][x] = 0 grid.Data[y+STRIDE*x] = 0
} }
} }
@@ -97,20 +93,25 @@ func (grid *Grid) SetupNeighbors(x, y int) {
} }
neighbors = append(neighbors, Neighbor{X: col, Y: row}) neighbors = append(neighbors, Neighbor{X: col, Y: row})
grid.NeighborCount[y][x]++ grid.NeighborCount[y+STRIDE*x]++
idx++ idx++
} }
} }
grid.Neighbors[y][x] = neighbors grid.Neighbors[y+STRIDE*x] = neighbors
} }
// count the living neighbors of a cell // count the living neighbors of a cell
func (grid *Grid) CountNeighbors(x, y int) uint8 { func (grid *Grid) CountNeighbors(x, y int) uint8 {
var count uint8 var count uint8
for idx := 0; idx < grid.NeighborCount[y][x]; idx++ { pos := y + STRIDE*x
count += grid.Data[grid.Neighbors[y][x][idx].Y][grid.Neighbors[y][x][idx].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]
} }
return count return count
@@ -127,30 +128,30 @@ func (grid *Grid) Clone() *Grid {
} }
// copy data // copy data
func (grid *Grid) Copy(other *Grid) { // func (grid *Grid) Copy(other *Grid) {
for y := range grid.Data { // for y := range grid.Data {
for x := range grid.Data[y] { // for x := range grid.Data[y] {
other.Data[y][x] = grid.Data[y][x] // other.Data[y+STRIDE*x] = grid.Data[y+STRIDE*x]
} // }
} // }
} // }
// delete all contents // delete all contents
func (grid *Grid) Clear() { // func (grid *Grid) Clear() {
for y := range grid.Data { // for y := range grid.Data {
for x := range grid.Data[y] { // for x := range grid.Data[y] {
grid.Data[y][x] = 0 // grid.Data[y+STRIDE*x] = 0
} // }
} // }
} // }
// initialize with random life cells using the given density // initialize with random life cells using the given density
func (grid *Grid) FillRandom() { func (grid *Grid) FillRandom() {
if !grid.Empty { if !grid.Empty {
for y := range grid.Data { for y := 0; y < grid.Config.Height; y++ {
for x := range grid.Data[y] { for x := 0; x < grid.Config.Width; x++ {
if rand.Intn(grid.Config.Density) == 1 { if rand.Intn(grid.Config.Density) == 1 {
grid.Data[y][x] = 1 grid.Data[y+STRIDE*x] = 1
} }
} }
} }
@@ -160,7 +161,7 @@ func (grid *Grid) FillRandom() {
func (grid *Grid) Dump() { func (grid *Grid) Dump() {
for y := 0; y < grid.Config.Height; y++ { for y := 0; y < grid.Config.Height; y++ {
for x := 0; x < grid.Config.Width; x++ { for x := 0; x < grid.Config.Width; x++ {
if grid.Data[y][x] == 1 { if grid.Data[y+STRIDE*x] == 1 {
fmt.Print("XX") fmt.Print("XX")
} else { } else {
fmt.Print(" ") fmt.Print(" ")
@@ -183,7 +184,7 @@ func (grid *Grid) LoadRLE(pattern *rle.RLE) {
x = colIndex + startX x = colIndex + startX
y = rowIndex + startY y = rowIndex + startY
grid.Data[y][x] = 1 grid.Data[y+STRIDE*x] = 1
} }
} }
} }
@@ -291,10 +292,10 @@ func (grid *Grid) SaveState(filename, rule string) error {
fmt.Fprintf(file, "#Life 1.05\n#R %s\n#D golsky state file\n#P -1 -1\n", rule) fmt.Fprintf(file, "#Life 1.05\n#R %s\n#D golsky state file\n#P -1 -1\n", rule)
for y := range grid.Data { for y := 0; y < grid.Config.Height; y++ {
for _, cell := range grid.Data[y] { for x := 0; x < grid.Config.Width; x++ {
row := "." row := "."
if cell == 1 { if grid.Data[y+STRIDE*x] == 1 {
row = "o" row = "o"
} }

View File

@@ -156,21 +156,24 @@ func (scene *ScenePlay) UpdateCells() {
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(scene.Config.Height) wg.Add(scene.Config.Height)
width := scene.Config.Width
height := scene.Config.Height
// compute life status of cells // compute life status of cells
for y := 0; y < scene.Config.Height; y++ { for y := 0; y < height; y++ {
go func() { go func() {
defer wg.Done() defer wg.Done()
for x := 0; x < scene.Config.Width; x++ { for x := 0; x < width; x++ {
state := scene.Grids[scene.Index].Data[y][x] // 0|1 == dead or alive state := scene.Grids[scene.Index].Data[y+STRIDE*x] // 0|1 == dead or alive
neighbors := scene.Grids[scene.Index].CountNeighbors(x, y) neighbors := scene.Grids[scene.Index].CountNeighbors(x, y)
// actually apply the current rules // actually apply the current rules
nextstate := scene.RuleCheckFunc(state, neighbors) nextstate := scene.RuleCheckFunc(state, neighbors)
// change state of current cell in next grid // change state of current cell in next grid
scene.Grids[next].Data[y][x] = nextstate scene.Grids[next].Data[y+STRIDE*x] = nextstate
if scene.Config.ShowEvolution { if scene.Config.ShowEvolution {
// set history to current generation so we can infer the // set history to current generation so we can infer the
@@ -418,7 +421,7 @@ func (scene *ScenePlay) SaveRectRLE() {
grid[y] = make([]uint8, width) grid[y] = make([]uint8, width)
for x := 0; x < width; x++ { for x := 0; x < width; x++ {
grid[y][x] = scene.Grids[scene.Index].Data[y+starty][x+startx] grid[y][x] = scene.Grids[scene.Index].Data[(y+starty)+STRIDE*(x+startx)]
} }
} }
@@ -471,7 +474,7 @@ func (scene *ScenePlay) ToggleCellOnCursorPos() {
y := int(worldY) / scene.Config.Cellsize y := int(worldY) / scene.Config.Cellsize
if x > -1 && y > -1 && x < scene.Config.Width && y < scene.Config.Height { if x > -1 && y > -1 && x < scene.Config.Width && y < scene.Config.Height {
scene.Grids[scene.Index].Data[y][x] ^= 1 scene.Grids[scene.Index].Data[y+STRIDE*x] ^= 1
scene.History.Age[y][x] = 1 scene.History.Age[y][x] = 1
} }
} }
@@ -497,7 +500,7 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) {
if scene.Config.ShowEvolution { if scene.Config.ShowEvolution {
scene.DrawEvolution(screen, x, y, op) scene.DrawEvolution(screen, x, y, op)
} else { } else {
if scene.Grids[scene.Index].Data[y][x] == 1 { if scene.Grids[scene.Index].Data[y+STRIDE*x] == 1 {
scene.World.DrawImage(scene.Theme.Tile(ColLife), op) scene.World.DrawImage(scene.Theme.Tile(ColLife), op)
} }
} }
@@ -514,7 +517,7 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) {
func (scene *ScenePlay) DrawEvolution(screen *ebiten.Image, x, y int, op *ebiten.DrawImageOptions) { func (scene *ScenePlay) DrawEvolution(screen *ebiten.Image, x, y int, op *ebiten.DrawImageOptions) {
age := scene.Generations - scene.History.Age[y][x] age := scene.Generations - scene.History.Age[y][x]
switch scene.Grids[scene.Index].Data[y][x] { switch scene.Grids[scene.Index].Data[y+STRIDE*x] {
case Alive: case Alive:
if age > 50 && scene.Config.ShowEvolution { if age > 50 && scene.Config.ShowEvolution {
scene.World.DrawImage(scene.Theme.Tile(ColOld), op) scene.World.DrawImage(scene.Theme.Tile(ColOld), op)

View File

@@ -87,6 +87,17 @@ func Loop(grid []bool) {
} }
func main() { func main() {
grid := make([]int, 50*50)
for y := 0; y < 50; y++ {
for x := 0; x < 50; x++ {
grid[y+50*x] = 1
fmt.Printf("%d,%d => %d\n", x, y, x+50*y)
}
}
}
func xmain() {
// enable cpu profiling. Do NOT use q to stop the game but // enable cpu profiling. Do NOT use q to stop the game but
// close the window to get a profile // close the window to get a profile
fd, err := os.Create("cpu.profile") fd, err := os.Create("cpu.profile")