mirror of
https://codeberg.org/scip/golsky.git
synced 2025-12-16 12:10:58 +01:00
changed grid data type to bool, save mem and better perf
This commit is contained in:
12
rle/rle.go
12
rle/rle.go
@@ -120,7 +120,7 @@ func removeWhitespace(input string) string {
|
||||
}
|
||||
|
||||
// Store a grid to an RLE file
|
||||
func StoreGridToRLE(grid [][]int64, filename, rule string, width, height int) error {
|
||||
func StoreGridToRLE(grid [][]bool, filename, rule string, width, height int) error {
|
||||
fd, err := os.Create(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -131,12 +131,12 @@ func StoreGridToRLE(grid [][]int64, filename, rule string, width, height int) er
|
||||
for y := 0; y < height; y++ {
|
||||
line := ""
|
||||
for x := 0; x < width; x++ {
|
||||
switch grid[y][x] {
|
||||
case 0:
|
||||
line += "b"
|
||||
case 1:
|
||||
line += "o"
|
||||
char := "b"
|
||||
if grid[y][x] {
|
||||
char = "o"
|
||||
}
|
||||
|
||||
line += char
|
||||
}
|
||||
|
||||
// if first row is: 001011110, then line is now:
|
||||
|
||||
@@ -42,8 +42,8 @@ type Config struct {
|
||||
|
||||
const (
|
||||
VERSION = "v0.0.8"
|
||||
Alive = 1
|
||||
Dead = 0
|
||||
Alive = true
|
||||
Dead = false
|
||||
|
||||
DEFAULT_GRID_WIDTH = 600
|
||||
DEFAULT_GRID_HEIGHT = 400
|
||||
|
||||
23
src/grid.go
23
src/grid.go
@@ -13,7 +13,7 @@ import (
|
||||
)
|
||||
|
||||
type Grid struct {
|
||||
Data [][]int64
|
||||
Data [][]bool
|
||||
Width, Height, Density int
|
||||
Empty bool
|
||||
}
|
||||
@@ -24,12 +24,12 @@ func NewGrid(width, height, density int, empty bool) *Grid {
|
||||
Height: height,
|
||||
Width: width,
|
||||
Density: density,
|
||||
Data: make([][]int64, height),
|
||||
Data: make([][]bool, height),
|
||||
Empty: empty,
|
||||
}
|
||||
|
||||
for y := 0; y < height; y++ {
|
||||
grid.Data[y] = make([]int64, width)
|
||||
grid.Data[y] = make([]bool, width)
|
||||
}
|
||||
|
||||
return grid
|
||||
@@ -59,7 +59,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] = 0
|
||||
grid.Data[y][x] = false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -70,7 +70,7 @@ func (grid *Grid) FillRandom() {
|
||||
for y := range grid.Data {
|
||||
for x := range grid.Data[y] {
|
||||
if rand.Intn(grid.Density) == 1 {
|
||||
grid.Data[y][x] = 1
|
||||
grid.Data[y][x] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -80,7 +80,7 @@ func (grid *Grid) FillRandom() {
|
||||
func (grid *Grid) Dump() {
|
||||
for y := 0; y < grid.Height; y++ {
|
||||
for x := 0; x < grid.Width; x++ {
|
||||
if grid.Data[y][x] == 1 {
|
||||
if grid.Data[y][x] {
|
||||
fmt.Print("XX")
|
||||
} else {
|
||||
fmt.Print(" ")
|
||||
@@ -103,7 +103,7 @@ func (grid *Grid) LoadRLE(pattern *rle.RLE) {
|
||||
x = colIndex + startX
|
||||
y = rowIndex + startY
|
||||
|
||||
grid.Data[y][x] = 1
|
||||
grid.Data[y][x] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -213,12 +213,9 @@ func (grid *Grid) SaveState(filename, rule string) error {
|
||||
|
||||
for y := range grid.Data {
|
||||
for _, cell := range grid.Data[y] {
|
||||
row := ""
|
||||
switch cell {
|
||||
case 1:
|
||||
row += "o"
|
||||
case 0:
|
||||
row += "."
|
||||
row := "."
|
||||
if cell {
|
||||
row = "o"
|
||||
}
|
||||
|
||||
_, err := file.WriteString(row)
|
||||
|
||||
53
src/play.go
53
src/play.go
@@ -5,6 +5,7 @@ import (
|
||||
"image"
|
||||
"image/color"
|
||||
"log"
|
||||
"unsafe"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
||||
@@ -30,10 +31,10 @@ type ScenePlay struct {
|
||||
|
||||
Clear bool
|
||||
|
||||
Grids []*Grid // 2 grids: one current, one next
|
||||
History *Grid // holds state of past dead cells for evolution traces
|
||||
Index int // points to current grid
|
||||
Generations int64 // Stats
|
||||
Grids []*Grid // 2 grids: one current, one next
|
||||
History [][]int64 // holds state of past dead cells for evolution traces
|
||||
Index int // points to current grid
|
||||
Generations int64 // Stats
|
||||
Black, White, Grey, Old color.RGBA
|
||||
AgeColor1, AgeColor2, AgeColor3, AgeColor4 color.RGBA
|
||||
TicksElapsed int // tick counter for game speed
|
||||
@@ -85,8 +86,8 @@ func (scene *ScenePlay) SetNext(next SceneName) {
|
||||
scene.Next = next
|
||||
}
|
||||
|
||||
func (scene *ScenePlay) CheckRule(state int64, neighbors int64) int64 {
|
||||
var nextstate int64
|
||||
func (scene *ScenePlay) CheckRule(state bool, neighbors int) bool {
|
||||
var nextstate bool
|
||||
|
||||
// The standard Scene of Life is symbolized in rule-string notation
|
||||
// as B3/S23 (23/3 here). A cell is born if it has exactly three
|
||||
@@ -94,9 +95,9 @@ func (scene *ScenePlay) CheckRule(state int64, neighbors int64) int64 {
|
||||
// and dies otherwise. The first number, or list of numbers, is
|
||||
// what is required for a dead cell to be born.
|
||||
|
||||
if state == 0 && Contains(scene.Config.Rule.Birth, neighbors) {
|
||||
if !state && Contains(scene.Config.Rule.Birth, neighbors) {
|
||||
nextstate = Alive
|
||||
} else if state == 1 && Contains(scene.Config.Rule.Death, neighbors) {
|
||||
} else if state && Contains(scene.Config.Rule.Death, neighbors) {
|
||||
nextstate = Alive
|
||||
} else {
|
||||
nextstate = Dead
|
||||
@@ -135,7 +136,7 @@ func (scene *ScenePlay) UpdateCells() {
|
||||
// deduce the color to use if evolution tracing is enabled
|
||||
// 60FPS:
|
||||
if state != nextstate {
|
||||
scene.History.Data[y][x] = scene.Generations
|
||||
scene.History[y][x] = scene.Generations
|
||||
}
|
||||
|
||||
// 10FPS:
|
||||
@@ -367,10 +368,10 @@ func (scene *ScenePlay) SaveRectRLE() {
|
||||
height = scene.Mark.Y - scene.Point.Y
|
||||
}
|
||||
|
||||
grid := make([][]int64, height)
|
||||
grid := make([][]bool, height)
|
||||
|
||||
for y := 0; y < height; y++ {
|
||||
grid[y] = make([]int64, width)
|
||||
grid[y] = make([]bool, width)
|
||||
|
||||
for x := 0; x < width; x++ {
|
||||
grid[y][x] = scene.Grids[scene.Index].Data[y+starty][x+startx]
|
||||
@@ -418,7 +419,7 @@ func (scene *ScenePlay) Update() error {
|
||||
}
|
||||
|
||||
// set a cell to alive or dead
|
||||
func (scene *ScenePlay) ToggleCellOnCursorPos(alive int64) {
|
||||
func (scene *ScenePlay) ToggleCellOnCursorPos(alive bool) {
|
||||
// use cursor pos relative to the world
|
||||
worldX, worldY := scene.Camera.ScreenToWorld(ebiten.CursorPosition())
|
||||
x := int(worldX) / scene.Config.Cellsize
|
||||
@@ -426,7 +427,7 @@ func (scene *ScenePlay) ToggleCellOnCursorPos(alive int64) {
|
||||
|
||||
if x > -1 && y > -1 && x < scene.Config.Width && y < scene.Config.Height {
|
||||
scene.Grids[scene.Index].Data[y][x] = alive
|
||||
scene.History.Data[y][x] = 1
|
||||
scene.History[y][x] = 1
|
||||
}
|
||||
}
|
||||
|
||||
@@ -451,7 +452,7 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) {
|
||||
)
|
||||
|
||||
//age = scene.Generations - scene.History.Data[y][x]
|
||||
age = scene.History.Data[y][x]
|
||||
age = scene.History[y][x]
|
||||
|
||||
switch scene.Grids[scene.Index].Data[y][x] {
|
||||
case Alive:
|
||||
@@ -463,7 +464,7 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) {
|
||||
}
|
||||
case Dead:
|
||||
// only draw dead cells in case evolution trace is enabled
|
||||
if scene.History.Data[y][x] > 1 && scene.Config.ShowEvolution {
|
||||
if scene.History[y][x] > 1 && scene.Config.ShowEvolution {
|
||||
switch {
|
||||
case age < 10:
|
||||
scene.World.DrawImage(scene.Tiles.Age1, op)
|
||||
@@ -544,7 +545,6 @@ func (scene *ScenePlay) DrawDebug(screen *ebiten.Image) {
|
||||
// load a pre-computed pattern from RLE file
|
||||
func (scene *ScenePlay) InitPattern() {
|
||||
scene.Grids[0].LoadRLE(scene.Config.RLE)
|
||||
scene.History.LoadRLE(scene.Config.RLE)
|
||||
}
|
||||
|
||||
// pre-render offscreen cache image
|
||||
@@ -574,18 +574,19 @@ func (scene *ScenePlay) InitCache() {
|
||||
func (scene *ScenePlay) InitGrid() {
|
||||
grida := NewGrid(scene.Config.Width, scene.Config.Height, scene.Config.Density, scene.Config.Empty)
|
||||
gridb := NewGrid(scene.Config.Width, scene.Config.Height, scene.Config.Density, scene.Config.Empty)
|
||||
history := NewGrid(scene.Config.Width, scene.Config.Height, scene.Config.Density, scene.Config.Empty)
|
||||
|
||||
// startup is delayed until user has selected options
|
||||
grida.FillRandom()
|
||||
grida.Copy(history)
|
||||
|
||||
scene.Grids = []*Grid{
|
||||
grida,
|
||||
gridb,
|
||||
}
|
||||
|
||||
scene.History = history
|
||||
scene.History = make([][]int64, scene.Config.Height)
|
||||
for y := 0; y < scene.Config.Height; y++ {
|
||||
scene.History[y] = make([]int64, scene.Config.Width)
|
||||
}
|
||||
}
|
||||
|
||||
// prepare tile images
|
||||
@@ -679,9 +680,15 @@ func (scene *ScenePlay) Init() {
|
||||
scene.Camera.Setup()
|
||||
}
|
||||
|
||||
func bool2int(b bool) int {
|
||||
return int(*(*byte)(unsafe.Pointer(&b)))
|
||||
}
|
||||
|
||||
// count the living neighbors of a cell
|
||||
func (scene *ScenePlay) CountNeighbors(x, y int) int64 {
|
||||
var sum int64
|
||||
func (scene *ScenePlay) CountNeighbors(x, y int) int {
|
||||
var sum int
|
||||
|
||||
grid := scene.Grids[scene.Index].Data
|
||||
|
||||
for nbgX := -1; nbgX < 2; nbgX++ {
|
||||
for nbgY := -1; nbgY < 2; nbgY++ {
|
||||
@@ -703,12 +710,12 @@ func (scene *ScenePlay) CountNeighbors(x, y int) int64 {
|
||||
row = y + nbgY
|
||||
}
|
||||
|
||||
sum += scene.Grids[scene.Index].Data[row][col]
|
||||
sum += bool2int(grid[row][col])
|
||||
}
|
||||
}
|
||||
|
||||
// don't count ourselfes though
|
||||
sum -= scene.Grids[scene.Index].Data[y][x]
|
||||
sum -= bool2int(grid[y][x])
|
||||
|
||||
return sum
|
||||
}
|
||||
|
||||
10
src/rule.go
10
src/rule.go
@@ -9,17 +9,17 @@ import (
|
||||
// a GOL rule
|
||||
type Rule struct {
|
||||
Definition string
|
||||
Birth []int64
|
||||
Death []int64
|
||||
Birth []int
|
||||
Death []int
|
||||
}
|
||||
|
||||
// parse one part of a GOL rule into rule slice
|
||||
func NumbersToList(numbers string) []int64 {
|
||||
list := []int64{}
|
||||
func NumbersToList(numbers string) []int {
|
||||
list := []int{}
|
||||
|
||||
items := strings.Split(numbers, "")
|
||||
for _, item := range items {
|
||||
num, err := strconv.ParseInt(item, 10, 64)
|
||||
num, err := strconv.Atoi(item)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to parse game rule part <%s>: %s", numbers, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user