mirror of
https://codeberg.org/scip/golsky.git
synced 2025-12-16 12:10:58 +01:00
next try to enhance performance: now using uint9 again and no pointers
This commit is contained in:
71
src/grid.go
71
src/grid.go
@@ -12,45 +12,50 @@ import (
|
||||
"github.com/tlinden/golsky/rle"
|
||||
)
|
||||
|
||||
type Cell struct {
|
||||
State uint8 // 1==life, 0==dead
|
||||
Neighbors [8]*Cell // all neighbors, max 8
|
||||
NeighborCount int // number of neighbors, might be less than 8 on edges
|
||||
}
|
||||
// func (cell *Cell) Count() uint8 {
|
||||
// var count uint8
|
||||
|
||||
func (cell *Cell) Count() uint8 {
|
||||
var count uint8
|
||||
// for idx := 0; idx < cell.NeighborCount; idx++ {
|
||||
// count += cell.Neighbors[idx].State
|
||||
// }
|
||||
|
||||
for idx := 0; idx < cell.NeighborCount; idx++ {
|
||||
count += cell.Neighbors[idx].State
|
||||
}
|
||||
// return count
|
||||
// }
|
||||
|
||||
return count
|
||||
type Neighbor struct {
|
||||
X, Y int
|
||||
}
|
||||
|
||||
type Grid struct {
|
||||
Data [][]*Cell
|
||||
Empty bool
|
||||
Config *Config
|
||||
Data [][]uint8
|
||||
NeighborCount [][]int
|
||||
Neighbors [][][]Neighbor
|
||||
Empty bool
|
||||
Config *Config
|
||||
}
|
||||
|
||||
// Create new empty grid and allocate Data according to provided dimensions
|
||||
func NewGrid(config *Config) *Grid {
|
||||
grid := &Grid{
|
||||
Data: make([][]*Cell, config.Height),
|
||||
Empty: config.Empty,
|
||||
Config: config,
|
||||
Data: make([][]uint8, config.Height),
|
||||
NeighborCount: make([][]int, config.Height),
|
||||
Neighbors: make([][][]Neighbor, config.Height),
|
||||
Empty: config.Empty,
|
||||
Config: config,
|
||||
}
|
||||
|
||||
// first setup the cells
|
||||
for y := 0; y < config.Height; y++ {
|
||||
grid.Data[y] = make([]*Cell, config.Width)
|
||||
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++ {
|
||||
grid.Data[y][x] = &Cell{}
|
||||
grid.Data[y][x] = 0
|
||||
}
|
||||
}
|
||||
|
||||
// in a second pass, collect pointers to the neighbors of each cell
|
||||
// in a second pass, collect positions to the neighbors of each cell
|
||||
for y := 0; y < config.Height; y++ {
|
||||
for x := 0; x < config.Width; x++ {
|
||||
grid.SetupNeighbors(x, y)
|
||||
@@ -63,6 +68,8 @@ func NewGrid(config *Config) *Grid {
|
||||
func (grid *Grid) SetupNeighbors(x, y int) {
|
||||
idx := 0
|
||||
|
||||
var neighbors []Neighbor
|
||||
|
||||
for nbgY := -1; nbgY < 2; nbgY++ {
|
||||
for nbgX := -1; nbgX < 2; nbgX++ {
|
||||
var col, row int
|
||||
@@ -89,16 +96,24 @@ func (grid *Grid) SetupNeighbors(x, y int) {
|
||||
continue
|
||||
}
|
||||
|
||||
grid.Data[y][x].Neighbors[idx] = grid.Data[row][col]
|
||||
grid.Data[y][x].NeighborCount++
|
||||
neighbors = append(neighbors, Neighbor{X: col, Y: row})
|
||||
grid.NeighborCount[y][x]++
|
||||
idx++
|
||||
}
|
||||
}
|
||||
|
||||
grid.Neighbors[y][x] = neighbors
|
||||
}
|
||||
|
||||
// count the living neighbors of a cell
|
||||
func (grid *Grid) CountNeighbors(x, y int) uint8 {
|
||||
return grid.Data[y][x].Count()
|
||||
var count uint8
|
||||
|
||||
for idx := 0; idx < grid.NeighborCount[y][x]; idx++ {
|
||||
count += grid.Data[grid.Neighbors[y][x][idx].Y][grid.Neighbors[y][x][idx].X]
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// Create a new 1:1 instance
|
||||
@@ -124,7 +139,7 @@ func (grid *Grid) Copy(other *Grid) {
|
||||
func (grid *Grid) Clear() {
|
||||
for y := range grid.Data {
|
||||
for x := range grid.Data[y] {
|
||||
grid.Data[y][x].State = 0
|
||||
grid.Data[y][x] = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -135,7 +150,7 @@ func (grid *Grid) FillRandom() {
|
||||
for y := range grid.Data {
|
||||
for x := range grid.Data[y] {
|
||||
if rand.Intn(grid.Config.Density) == 1 {
|
||||
grid.Data[y][x].State = 1
|
||||
grid.Data[y][x] = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -145,7 +160,7 @@ func (grid *Grid) FillRandom() {
|
||||
func (grid *Grid) Dump() {
|
||||
for y := 0; y < grid.Config.Height; y++ {
|
||||
for x := 0; x < grid.Config.Width; x++ {
|
||||
if grid.Data[y][x].State == 1 {
|
||||
if grid.Data[y][x] == 1 {
|
||||
fmt.Print("XX")
|
||||
} else {
|
||||
fmt.Print(" ")
|
||||
@@ -168,7 +183,7 @@ func (grid *Grid) LoadRLE(pattern *rle.RLE) {
|
||||
x = colIndex + startX
|
||||
y = rowIndex + startY
|
||||
|
||||
grid.Data[y][x].State = 1
|
||||
grid.Data[y][x] = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -279,7 +294,7 @@ func (grid *Grid) SaveState(filename, rule string) error {
|
||||
for y := range grid.Data {
|
||||
for _, cell := range grid.Data[y] {
|
||||
row := "."
|
||||
if cell.State == 1 {
|
||||
if cell == 1 {
|
||||
row = "o"
|
||||
}
|
||||
|
||||
|
||||
12
src/play.go
12
src/play.go
@@ -163,14 +163,14 @@ func (scene *ScenePlay) UpdateCells() {
|
||||
defer wg.Done()
|
||||
|
||||
for x := 0; x < scene.Config.Width; x++ {
|
||||
state := scene.Grids[scene.Index].Data[y][x].State // 0|1 == dead or alive
|
||||
state := scene.Grids[scene.Index].Data[y][x] // 0|1 == dead or alive
|
||||
neighbors := scene.Grids[scene.Index].CountNeighbors(x, y)
|
||||
|
||||
// actually apply the current rules
|
||||
nextstate := scene.RuleCheckFunc(state, neighbors)
|
||||
|
||||
// change state of current cell in next grid
|
||||
scene.Grids[next].Data[y][x].State = nextstate
|
||||
scene.Grids[next].Data[y][x] = nextstate
|
||||
|
||||
if scene.Config.ShowEvolution {
|
||||
// set history to current generation so we can infer the
|
||||
@@ -418,7 +418,7 @@ func (scene *ScenePlay) SaveRectRLE() {
|
||||
grid[y] = make([]uint8, width)
|
||||
|
||||
for x := 0; x < width; x++ {
|
||||
grid[y][x] = scene.Grids[scene.Index].Data[y+starty][x+startx].State
|
||||
grid[y][x] = scene.Grids[scene.Index].Data[y+starty][x+startx]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -471,7 +471,7 @@ func (scene *ScenePlay) ToggleCellOnCursorPos() {
|
||||
y := int(worldY) / scene.Config.Cellsize
|
||||
|
||||
if x > -1 && y > -1 && x < scene.Config.Width && y < scene.Config.Height {
|
||||
scene.Grids[scene.Index].Data[y][x].State ^= 1
|
||||
scene.Grids[scene.Index].Data[y][x] ^= 1
|
||||
scene.History.Age[y][x] = 1
|
||||
}
|
||||
}
|
||||
@@ -497,7 +497,7 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) {
|
||||
if scene.Config.ShowEvolution {
|
||||
scene.DrawEvolution(screen, x, y, op)
|
||||
} else {
|
||||
if scene.Grids[scene.Index].Data[y][x].State == 1 {
|
||||
if scene.Grids[scene.Index].Data[y][x] == 1 {
|
||||
scene.World.DrawImage(scene.Theme.Tile(ColLife), op)
|
||||
}
|
||||
}
|
||||
@@ -514,7 +514,7 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) {
|
||||
func (scene *ScenePlay) DrawEvolution(screen *ebiten.Image, x, y int, op *ebiten.DrawImageOptions) {
|
||||
age := scene.Generations - scene.History.Age[y][x]
|
||||
|
||||
switch scene.Grids[scene.Index].Data[y][x].State {
|
||||
switch scene.Grids[scene.Index].Data[y][x] {
|
||||
case Alive:
|
||||
if age > 50 && scene.Config.ShowEvolution {
|
||||
scene.World.DrawImage(scene.Theme.Tile(ColOld), op)
|
||||
|
||||
Reference in New Issue
Block a user