int64 => uint8

This commit is contained in:
2024-06-05 19:26:25 +02:00
parent 162d141b34
commit 422c5d5b7f
5 changed files with 41 additions and 44 deletions

View File

@@ -120,7 +120,7 @@ func removeWhitespace(input string) string {
} }
// Store a grid to an RLE file // Store a grid to an RLE file
func StoreGridToRLE(grid [][]int64, filename, rule string, width, height int) error { func StoreGridToRLE(grid [][]uint8, filename, rule string, width, height int) error {
fd, err := os.Create(filename) fd, err := os.Create(filename)
if err != nil { if err != nil {
return err return err

View File

@@ -13,7 +13,7 @@ import (
) )
type Grid struct { type Grid struct {
Data [][]int64 Data [][]uint8
Width, Height, Density int Width, Height, Density int
Empty bool Empty bool
} }
@@ -24,12 +24,12 @@ func NewGrid(width, height, density int, empty bool) *Grid {
Height: height, Height: height,
Width: width, Width: width,
Density: density, Density: density,
Data: make([][]int64, height), Data: make([][]uint8, height),
Empty: empty, Empty: empty,
} }
for y := 0; y < height; y++ { for y := 0; y < height; y++ {
grid.Data[y] = make([]int64, width) grid.Data[y] = make([]uint8, width)
} }
return grid return grid
@@ -233,12 +233,12 @@ func (grid *Grid) SaveState(filename, rule string) error {
} }
// generate filenames for dumps // generate filenames for dumps
func GetFilename(generations int64) string { func GetFilename(generations uint64) string {
now := time.Now() now := time.Now()
return fmt.Sprintf("dump-%s-%d.lif", now.Format("20060102150405"), generations) return fmt.Sprintf("dump-%s-%d.lif", now.Format("20060102150405"), generations)
} }
func GetFilenameRLE(generations int64) string { func GetFilenameRLE(generations uint64) string {
now := time.Now() now := time.Now()
return fmt.Sprintf("rect-%s-%d.rle", now.Format("20060102150405"), generations) return fmt.Sprintf("rect-%s-%d.rle", now.Format("20060102150405"), generations)
} }

View File

@@ -30,10 +30,10 @@ type ScenePlay struct {
Clear bool Clear bool
Grids []*Grid // 2 grids: one current, one next Grids []*Grid // 2 grids: one current, one next
History *Grid // holds state of past dead cells for evolution traces History [][]uint64 // holds state of past dead cells for evolution traces
Index int // points to current grid Index int // points to current grid
Generations int64 // Stats Generations uint64 // Stats
Black, White, Grey, Old color.RGBA Black, White, Grey, Old color.RGBA
AgeColor1, AgeColor2, AgeColor3, AgeColor4 color.RGBA AgeColor1, AgeColor2, AgeColor3, AgeColor4 color.RGBA
TicksElapsed int // tick counter for game speed TicksElapsed int // tick counter for game speed
@@ -85,8 +85,8 @@ func (scene *ScenePlay) SetNext(next SceneName) {
scene.Next = next scene.Next = next
} }
func (scene *ScenePlay) CheckRule(state int64, neighbors int64) int64 { func (scene *ScenePlay) CheckRule(state uint8, neighbors uint8) uint8 {
var nextstate int64 var nextstate uint8
// The standard Scene of Life is symbolized in rule-string notation // 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 // as B3/S23 (23/3 here). A cell is born if it has exactly three
@@ -133,8 +133,9 @@ func (scene *ScenePlay) UpdateCells() {
// set history to current generation so we can infer the // set history to current generation so we can infer the
// age of the cell's state during rendering and use it to // age of the cell's state during rendering and use it to
// deduce the color to use if evolution tracing is enabled // deduce the color to use if evolution tracing is enabled
// FIXME: unbranch somehow
if state != nextstate { if state != nextstate {
scene.History.Data[y][x] = scene.Generations scene.History[y][x] = scene.Generations
} }
} }
} }
@@ -363,10 +364,10 @@ func (scene *ScenePlay) SaveRectRLE() {
height = scene.Mark.Y - scene.Point.Y height = scene.Mark.Y - scene.Point.Y
} }
grid := make([][]int64, height) grid := make([][]uint8, height)
for y := 0; y < height; y++ { for y := 0; y < height; y++ {
grid[y] = make([]int64, 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][x+startx]
@@ -414,15 +415,15 @@ func (scene *ScenePlay) Update() error {
} }
// set a cell to alive or dead // set a cell to alive or dead
func (scene *ScenePlay) ToggleCellOnCursorPos(alive int64) { func (scene *ScenePlay) ToggleCellOnCursorPos(state uint8) {
// use cursor pos relative to the world // use cursor pos relative to the world
worldX, worldY := scene.Camera.ScreenToWorld(ebiten.CursorPosition()) worldX, worldY := scene.Camera.ScreenToWorld(ebiten.CursorPosition())
x := int(worldX) / scene.Config.Cellsize x := int(worldX) / scene.Config.Cellsize
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] = alive scene.Grids[scene.Index].Data[y][x] = state
scene.History.Data[y][x] = 1 scene.History[y][x] = 1
} }
} }
@@ -436,7 +437,7 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) {
op.GeoM.Translate(0, 0) op.GeoM.Translate(0, 0)
scene.World.DrawImage(scene.Cache, op) scene.World.DrawImage(scene.Cache, op)
var age int64 var age uint64
for y := 0; y < scene.Config.Height; y++ { for y := 0; y < scene.Config.Height; y++ {
for x := 0; x < scene.Config.Width; x++ { for x := 0; x < scene.Config.Width; x++ {
@@ -446,7 +447,7 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) {
float64(y*scene.Config.Cellsize), float64(y*scene.Config.Cellsize),
) )
age = scene.Generations - scene.History.Data[y][x] age = scene.Generations - scene.History[y][x]
switch scene.Grids[scene.Index].Data[y][x] { switch scene.Grids[scene.Index].Data[y][x] {
case Alive: case Alive:
@@ -458,7 +459,7 @@ func (scene *ScenePlay) Draw(screen *ebiten.Image) {
} }
case Dead: case Dead:
// only draw dead cells in case evolution trace is enabled // 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 { switch {
case age < 10: case age < 10:
scene.World.DrawImage(scene.Tiles.Age1, op) scene.World.DrawImage(scene.Tiles.Age1, op)
@@ -539,7 +540,6 @@ func (scene *ScenePlay) DrawDebug(screen *ebiten.Image) {
// load a pre-computed pattern from RLE file // load a pre-computed pattern from RLE file
func (scene *ScenePlay) InitPattern() { func (scene *ScenePlay) InitPattern() {
scene.Grids[0].LoadRLE(scene.Config.RLE) scene.Grids[0].LoadRLE(scene.Config.RLE)
scene.History.LoadRLE(scene.Config.RLE)
} }
// pre-render offscreen cache image // pre-render offscreen cache image
@@ -580,7 +580,10 @@ func (scene *ScenePlay) InitGrid() {
gridb, gridb,
} }
scene.History = history scene.History = make([][]uint64, scene.Config.Height)
for y := 0; y < scene.Config.Height; y++ {
scene.History[y] = make([]uint64, scene.Config.Width)
}
} }
// prepare tile images // prepare tile images
@@ -675,8 +678,8 @@ func (scene *ScenePlay) Init() {
} }
// count the living neighbors of a cell // count the living neighbors of a cell
func (scene *ScenePlay) CountNeighbors(x, y int) int64 { func (scene *ScenePlay) CountNeighbors(x, y int) uint8 {
var sum int64 var sum uint8
for nbgX := -1; nbgX < 2; nbgX++ { for nbgX := -1; nbgX < 2; nbgX++ {
for nbgY := -1; nbgY < 2; nbgY++ { for nbgY := -1; nbgY < 2; nbgY++ {

View File

@@ -9,22 +9,22 @@ import (
// a GOL rule // a GOL rule
type Rule struct { type Rule struct {
Definition string Definition string
Birth []int64 Birth []uint8
Death []int64 Death []uint8
} }
// parse one part of a GOL rule into rule slice // parse one part of a GOL rule into rule slice
func NumbersToList(numbers string) []int64 { func NumbersToList(numbers string) []uint8 {
list := []int64{} list := []uint8{}
items := strings.Split(numbers, "") items := strings.Split(numbers, "")
for _, item := range items { for _, item := range items {
num, err := strconv.ParseInt(item, 10, 64) num, err := strconv.ParseInt(item, 10, 8)
if err != nil { if err != nil {
log.Fatalf("failed to parse game rule part <%s>: %s", numbers, err) log.Fatalf("failed to parse game rule part <%s>: %s", numbers, err)
} }
list = append(list, num) list = append(list, uint8(num))
} }
return list return list

View File

@@ -238,20 +238,14 @@ func (game *Game) Draw(screen *ebiten.Image) {
} }
func main() { func main() {
//x := 1 // state := 1
//y := 0 // nextstate := 0
col := 1 >> 0xff v := 0
fmt.Printf("col: %d\n", col) for i := 0; i < 600; i++ {
v = ((v + 1) & 255)
x := 1 fmt.Println(i, i&255)
y := 2 }
c := 4
xm := x & (c - 1)
ym := y & (c - 1)
fmt.Println(xm & ym)
} }
func _main() { func _main() {