diff --git a/config.go b/config.go new file mode 100644 index 0000000..7bb08da --- /dev/null +++ b/config.go @@ -0,0 +1,98 @@ +package main + +import ( + "fmt" + "log" + "os" + + "github.com/spf13/pflag" + "github.com/tlinden/golsky/rle" +) + +type Config struct { + Width, Height, Cellsize, Density int // measurements + ScreenWidth, ScreenHeight int + TPG int // ticks per generation/game speed, 1==max + Debug, Empty, Invert, Paused bool // game modi + ShowEvolution, NoGrid, RunOneStep bool // flags + Rule *Rule // which rule to use, default: B3/S23 + RLE *rle.RLE // loaded GOL pattern from RLE file + Statefile string // load game state from it if non-nil + StateGrid *Grid // a grid from a statefile + Wrap bool // wether wraparound mode is in place or not +} + +func ParseCommandline() *Config { + config := Config{} + + showversion := false + var rule string + var rlefile string + + // commandline params, most configure directly config flags + pflag.IntVarP(&config.Width, "width", "W", 40, "grid width in cells") + pflag.IntVarP(&config.Height, "height", "H", 40, "grid height in cells") + pflag.IntVarP(&config.Cellsize, "cellsize", "c", 8, "cell size in pixels") + pflag.IntVarP(&config.Density, "density", "D", 10, "density of random cells") + pflag.IntVarP(&config.TPG, "ticks-per-generation", "t", 10, + "game speed: the higher the slower (default: 10)") + + pflag.StringVarP(&rule, "rule", "r", "B3/S23", "game rule") + pflag.StringVarP(&rlefile, "rle-file", "f", "", "RLE pattern file") + pflag.StringVarP(&config.Statefile, "load-state-file", "l", "", "game state file") + + pflag.BoolVarP(&showversion, "version", "v", false, "show version") + pflag.BoolVarP(&config.Paused, "paused", "p", false, "do not start simulation (use space to start)") + pflag.BoolVarP(&config.Debug, "debug", "d", false, "show debug info") + pflag.BoolVarP(&config.NoGrid, "nogrid", "n", false, "do not draw grid lines") + pflag.BoolVarP(&config.Empty, "empty", "e", false, "start with an empty screen") + pflag.BoolVarP(&config.Invert, "invert", "i", false, "invert colors (dead cell: black)") + pflag.BoolVarP(&config.ShowEvolution, "show-evolution", "s", false, "show evolution tracks") + pflag.BoolVarP(&config.Wrap, "wrap-around", "w", false, "wrap around grid mode") + + pflag.Parse() + + if showversion { + fmt.Printf("This is golsky version %s\n", VERSION) + os.Exit(0) + } + + // check if we have been given an RLE file to load + config.RLE = GetRLE(rlefile) + if config.RLE != nil { + if config.RLE.Width > config.Width || config.RLE.Height > config.Height { + config.Width = config.RLE.Width * 2 + config.Height = config.RLE.Height * 2 + fmt.Printf("rlew: %d, rleh: %d, w: %d, h: %d\n", + config.RLE.Width, config.RLE.Height, config.Width, config.Height) + } + + // RLE needs an empty grid + config.Empty = true + + // it may come with its own rule + if config.RLE.Rule != "" { + config.Rule = ParseGameRule(config.RLE.Rule) + } + } else if config.Statefile != "" { + grid, err := LoadState(config.Statefile) + if err != nil { + log.Fatalf("failed to load game state: %s", err) + } + + config.Width = grid.Width + config.Height = grid.Height + config.StateGrid = grid + } + + config.ScreenWidth = config.Cellsize * config.Width + config.ScreenHeight = config.Cellsize * config.Height + + // load rule from commandline when no rule came from RLE file, + // default is B3/S23, aka conways game of life + if config.Rule == nil { + config.Rule = ParseGameRule(rule) + } + + return &config +} diff --git a/game.go b/game.go index 7c89bbf..94ee80e 100644 --- a/game.go +++ b/game.go @@ -1,597 +1,61 @@ package main -import ( - "fmt" - "image" - "image/color" - "log" - "math/rand" - "os" - - "github.com/hajimehoshi/ebiten/v2" - "github.com/hajimehoshi/ebiten/v2/ebitenutil" - "github.com/hajimehoshi/ebiten/v2/inpututil" - "github.com/hajimehoshi/ebiten/v2/vector" - "github.com/tlinden/golsky/rle" - "golang.org/x/image/math/f64" -) - -type Images struct { - Black, White, Age1, Age2, Age3, Age4, Old *ebiten.Image -} +import "github.com/hajimehoshi/ebiten/v2" type Game struct { - Grids []*Grid // 2 grids: one current, one next - History *Grid // holds state of past dead cells for evolution tracks - Index int // points to current grid - Width, Height, Cellsize, Density int // measurements - ScreenWidth, ScreenHeight int - Generations int64 // Stats - Black, White, Grey, Old color.RGBA - AgeColor1, AgeColor2, AgeColor3, AgeColor4 color.RGBA - TPG int // ticks per generation/game speed, 1==max - TicksElapsed int // tick counter for game speed - Debug, Paused, Empty, Invert bool // game modi - ShowEvolution, NoGrid, RunOneStep bool // flags - Rule *Rule // which rule to use, default: B3/S23 - Tiles Images // pre-computed tiles for dead and alife cells - RLE *rle.RLE // loaded GOL pattern from RLE file - Camera Camera // for zoom+move - World *ebiten.Image // actual image we render to - WheelTurned bool // when user turns wheel multiple times, zoom faster - Dragging bool // middle mouse is pressed, move canvas - LastCursorPos []int // used to check if the user is dragging - Statefile string // load game state from it if non-nil - Markmode bool // enabled with 'c' - MarkTaken bool // true when mouse1 pressed - MarkDone bool // true when mouse1 released, copy cells between Mark+Point - Mark, Point image.Point // area to marks+save - Wrap bool // wether wraparound mode is in place or not + ScreenWidth, ScreenHeight, Cellsize int + Scenes map[SceneName]Scene + CurrentScene SceneName + Config *Config +} + +func NewGame(config *Config, startscene SceneName) *Game { + game := &Game{ + Config: config, + Scenes: map[SceneName]Scene{}, + ScreenWidth: config.ScreenWidth, + ScreenHeight: config.ScreenHeight, + } + + game.CurrentScene = startscene + game.Scenes[Play] = NewPlayScene(game, config) + + return game +} + +func (game *Game) GetCurrentScene() Scene { + return game.Scenes[game.CurrentScene] } func (game *Game) Layout(outsideWidth, outsideHeight int) (int, int) { return game.ScreenWidth, game.ScreenHeight } -func (game *Game) CheckRule(state int64, neighbors int64) int64 { - var nextstate int64 - - // The standard Game of Life is symbolized in rule-string notation - // as B3/S23 (23/3 here). A cell is born if it has exactly three - // neighbors, survives if it has two or three living neighbors, - // 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(game.Rule.Birth, neighbors) { - nextstate = Alive - } else if state == 1 && Contains(game.Rule.Death, neighbors) { - nextstate = Alive - } else { - nextstate = Dead - } - - return nextstate -} - -// Update all cells according to the current rule -func (game *Game) UpdateCells() { - // count ticks so we know when to actually run - game.TicksElapsed++ - - if game.TPG > game.TicksElapsed { - // need to sleep a little more - return - } - - // next grid index, we just xor 0|1 to 1|0 - next := game.Index ^ 1 - - // compute life status of cells - for y := 0; y < game.Height; y++ { - for x := 0; x < game.Width; x++ { - state := game.Grids[game.Index].Data[y][x] // 0|1 == dead or alive - neighbors := game.CountNeighbors(x, y) // alive neighbor count - - // actually apply the current rules - nextstate := game.CheckRule(state, neighbors) - - // change state of current cell in next grid - game.Grids[next].Data[y][x] = nextstate - - // set history to current generation so we can infer the - // age of the cell's state during rendering and use it to - // deduce the color to use if evolution tracking is enabled - if state != nextstate { - game.History.Data[y][x] = game.Generations - } - } - } - - // switch grid for rendering - game.Index ^= 1 - - // global stats counter - game.Generations++ - - if game.RunOneStep { - // setp-wise mode, halt the game - game.RunOneStep = false - } - - // reset speed counter - game.TicksElapsed = 0 -} - -func (game *Game) Reset() { - game.Paused = true - game.InitGrid(nil) - game.Paused = false -} - -// check user input -func (game *Game) CheckInput() { - if inpututil.IsKeyJustPressed(ebiten.KeyQ) { - os.Exit(0) - } - - if inpututil.IsKeyJustPressed(ebiten.KeyC) { - fmt.Println("mark mode on") - game.Markmode = true - } - - if game.Markmode { - return - } - - if inpututil.IsKeyJustPressed(ebiten.KeySpace) || inpututil.IsKeyJustPressed(ebiten.KeyEnter) { - game.Paused = !game.Paused - } - - if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { - game.ToggleCellOnCursorPos(Alive) - game.Paused = true // drawing while running makes no sense - } - - if ebiten.IsMouseButtonPressed(ebiten.MouseButtonRight) { - game.ToggleCellOnCursorPos(Dead) - game.Paused = true // drawing while running makes no sense - } - - if ebiten.IsKeyPressed(ebiten.KeyPageDown) { - if game.TPG < 120 { - game.TPG++ - } - } - - if ebiten.IsKeyPressed(ebiten.KeyPageUp) { - if game.TPG > 1 { - game.TPG-- - } - } - - if inpututil.IsKeyJustPressed(ebiten.KeyS) { - game.SaveState() - } - - if inpututil.IsKeyJustPressed(ebiten.KeyR) { - game.Reset() - } - - if game.Paused { - if inpututil.IsKeyJustPressed(ebiten.KeyN) { - game.RunOneStep = true - } - } -} - -// Check dragging input. move the canvas with the mouse while pressing -// the middle mouse button, zoom in and out using the wheel. -func (game *Game) CheckDraggingInput() { - if game.Markmode { - return - } - - // move canvas - if game.Dragging && !ebiten.IsMouseButtonPressed(ebiten.MouseButton1) { - // release - game.Dragging = false - } - - if !game.Dragging && ebiten.IsMouseButtonPressed(ebiten.MouseButton1) { - // start dragging - game.Dragging = true - game.LastCursorPos[0], game.LastCursorPos[1] = ebiten.CursorPosition() - } - - if game.Dragging { - x, y := ebiten.CursorPosition() - - if x != game.LastCursorPos[0] || y != game.LastCursorPos[1] { - // actually drag by mouse cursor pos diff to last cursor pos - game.Camera.Position[0] -= float64(x - game.LastCursorPos[0]) - game.Camera.Position[1] -= float64(y - game.LastCursorPos[1]) - } - - game.LastCursorPos[0], game.LastCursorPos[1] = ebiten.CursorPosition() - } - - // also support the arrow keys to move the canvas - if ebiten.IsKeyPressed(ebiten.KeyArrowLeft) { - game.Camera.Position[0] -= 1 - } - if ebiten.IsKeyPressed(ebiten.KeyArrowRight) { - game.Camera.Position[0] += 1 - } - if ebiten.IsKeyPressed(ebiten.KeyArrowUp) { - game.Camera.Position[1] -= 1 - } - if ebiten.IsKeyPressed(ebiten.KeyArrowDown) { - game.Camera.Position[1] += 1 - } - - // Zoom - _, dy := ebiten.Wheel() - step := 1 - - if game.WheelTurned { - // if keep scrolling the wheel, zoom faster - step = 50 - } else { - game.WheelTurned = false - } - - if dy < 0 { - if game.Camera.ZoomFactor > -2400 { - game.Camera.ZoomFactor -= step - } - } - - if dy > 0 { - if game.Camera.ZoomFactor < 2400 { - game.Camera.ZoomFactor += step - } - } - - if inpututil.IsKeyJustPressed(ebiten.KeyEscape) { - game.Camera.Reset() - } - -} - -func (game *Game) GetWorldCursorPos() image.Point { - worldX, worldY := game.Camera.ScreenToWorld(ebiten.CursorPosition()) - return image.Point{ - X: int(worldX) / game.Cellsize, - Y: int(worldY) / game.Cellsize, - } -} - -func (game *Game) CheckMarkInput() { - if !game.Markmode { - return - } - - if ebiten.IsMouseButtonPressed(ebiten.MouseButton0) { - if !game.MarkTaken { - game.Mark = game.GetWorldCursorPos() - game.MarkTaken = true - game.MarkDone = false - } - - game.Point = game.GetWorldCursorPos() - fmt.Printf("Mark: %v, Current: %v\n", game.Mark, game.Point) - } else if inpututil.IsMouseButtonJustReleased(ebiten.MouseButton0) { - game.Markmode = false - game.MarkTaken = false - game.MarkDone = true - } -} - -func (game *Game) SaveState() { - filename := GetFilename(game.Generations) - err := game.Grids[game.Index].SaveState(filename) - if err != nil { - log.Printf("failed to save game state to %s: %s", filename, err) - } - log.Printf("saved game state to %s at generation %d\n", filename, game.Generations) -} - func (game *Game) Update() error { - game.CheckInput() - game.CheckDraggingInput() - game.CheckMarkInput() + scene := game.GetCurrentScene() + scene.Update() - if !game.Paused || game.RunOneStep { - game.UpdateCells() + next := scene.GetNext() + + if next != game.CurrentScene { + // make sure we stay on the selected scene + scene.ResetNext() + + // finally switch + game.CurrentScene = next } return nil } -// set a cell to alive or dead -func (game *Game) ToggleCellOnCursorPos(alive int64) { - // use cursor pos relative to the world - worldX, worldY := game.Camera.ScreenToWorld(ebiten.CursorPosition()) - x := int(worldX) / game.Cellsize - y := int(worldY) / game.Cellsize - - //fmt.Printf("cell at %d,%d\n", x, y) - - if x > -1 && y > -1 { - game.Grids[game.Index].Data[y][x] = alive - game.History.Data[y][x] = 1 - } -} - -// draw the new grid state func (game *Game) Draw(screen *ebiten.Image) { - // we fill the whole screen with a background color, the cells - // themselfes will be 1px smaller as their nominal size, producing - // a nice grey grid with grid lines - op := &ebiten.DrawImageOptions{} + scene := game.GetCurrentScene() - if game.NoGrid { - game.World.Fill(game.White) + if scene.Clearscreen() { + ebiten.SetScreenClearedEveryFrame(true) } else { - game.World.Fill(game.Grey) + ebiten.SetScreenClearedEveryFrame(false) } - for y := 0; y < game.Height; y++ { - for x := 0; x < game.Width; x++ { - op.GeoM.Reset() - op.GeoM.Translate(float64(x*game.Cellsize), float64(y*game.Cellsize)) - - age := game.Generations - game.History.Data[y][x] - - switch game.Grids[game.Index].Data[y][x] { - case 1: - if age > 50 && game.ShowEvolution { - game.World.DrawImage(game.Tiles.Old, op) - } else { - game.World.DrawImage(game.Tiles.Black, op) - } - case 0: - if game.History.Data[y][x] > 1 && game.ShowEvolution { - switch { - case age < 10: - game.World.DrawImage(game.Tiles.Age1, op) - case age < 20: - game.World.DrawImage(game.Tiles.Age2, op) - case age < 30: - game.World.DrawImage(game.Tiles.Age3, op) - default: - game.World.DrawImage(game.Tiles.Age4, op) - } - } else { - game.World.DrawImage(game.Tiles.White, op) - } - } - } - } - - game.Camera.Render(game.World, screen) - - if game.Debug { - paused := "" - if game.Paused { - paused = "-- paused --" - } - - ebitenutil.DebugPrint( - screen, - fmt.Sprintf("FPS: %0.2f, TPG: %d, Mem: %0.2f MB, Generations: %d %s", - ebiten.ActualTPS(), game.TPG, GetMem(), game.Generations, paused), - ) - } -} - -// FIXME: move these into Grid -// load a pre-computed pattern from RLE file -func (game *Game) InitPattern() { - if game.RLE != nil { - startX := (game.Width / 2) - (game.RLE.Width / 2) - startY := (game.Height / 2) - (game.RLE.Height / 2) - var y, x int - - for rowIndex, patternRow := range game.RLE.Pattern { - for colIndex := range patternRow { - if game.RLE.Pattern[rowIndex][colIndex] > 0 { - x = colIndex + startX - y = rowIndex + startY - - game.History.Data[y][x] = 1 - game.Grids[0].Data[y][x] = 1 - } - } - } - } -} - -// initialize playing field/grid -func (game *Game) _InitGrid(grid *Grid) { - if grid != nil { - // use pre-loaded grid - game.Grids = []*Grid{ - grid, - NewGrid(grid.Width, grid.Height), - } - - game.History = NewGrid(grid.Width, grid.Height) - - return - } - - grida := NewGrid(game.Width, game.Height) - - grida.FillRandom(game) - - game.Grids = []*Grid{ - grida, - NewGrid(grida.Width, grida.Height), - } - - game.History = grida.Clone() -} - -func (game *Game) InitGrid(grid *Grid) { - if grid != nil { - // use pre-loaded grid - game.Grids = []*Grid{ - grid, - NewGrid(grid.Width, grid.Height), - } - - game.History = NewGrid(grid.Width, grid.Height) - - return - } - - grida := NewGrid(game.Width, game.Height) - gridb := NewGrid(game.Width, game.Height) - history := NewGrid(game.Width, game.Height) - - for y := 0; y < game.Height; y++ { - if !game.Empty { - for x := 0; x < game.Width; x++ { - if rand.Intn(game.Density) == 1 { - history.Data[y][x] = 1 - grida.Data[y][x] = 1 - } - } - } - } - - game.Grids = []*Grid{ - grida, - gridb, - } - - game.History = history -} - -// prepare tile images -func (game *Game) InitTiles() { - game.Grey = color.RGBA{128, 128, 128, 0xff} - game.Old = color.RGBA{255, 30, 30, 0xff} - - game.Black = color.RGBA{0, 0, 0, 0xff} - game.White = color.RGBA{200, 200, 200, 0xff} - game.AgeColor1 = color.RGBA{255, 195, 97, 0xff} // FIXME: use slice! - game.AgeColor2 = color.RGBA{255, 211, 140, 0xff} - game.AgeColor3 = color.RGBA{255, 227, 181, 0xff} - game.AgeColor4 = color.RGBA{255, 240, 224, 0xff} - - if game.Invert { - game.White = color.RGBA{0, 0, 0, 0xff} - game.Black = color.RGBA{200, 200, 200, 0xff} - - game.AgeColor1 = color.RGBA{82, 38, 0, 0xff} - game.AgeColor2 = color.RGBA{66, 35, 0, 0xff} - game.AgeColor3 = color.RGBA{43, 27, 0, 0xff} - game.AgeColor4 = color.RGBA{25, 17, 0, 0xff} - } - - game.Tiles.Black = ebiten.NewImage(game.Cellsize, game.Cellsize) - game.Tiles.White = ebiten.NewImage(game.Cellsize, game.Cellsize) - game.Tiles.Old = ebiten.NewImage(game.Cellsize, game.Cellsize) - game.Tiles.Age1 = ebiten.NewImage(game.Cellsize, game.Cellsize) - game.Tiles.Age2 = ebiten.NewImage(game.Cellsize, game.Cellsize) - game.Tiles.Age3 = ebiten.NewImage(game.Cellsize, game.Cellsize) - game.Tiles.Age4 = ebiten.NewImage(game.Cellsize, game.Cellsize) - - cellsize := game.ScreenWidth / game.Cellsize - - FillCell(game.Tiles.Black, cellsize, game.Black) - FillCell(game.Tiles.White, cellsize, game.White) - FillCell(game.Tiles.Old, cellsize, game.Old) - FillCell(game.Tiles.Age1, cellsize, game.AgeColor1) - FillCell(game.Tiles.Age2, cellsize, game.AgeColor2) - FillCell(game.Tiles.Age3, cellsize, game.AgeColor3) - FillCell(game.Tiles.Age4, cellsize, game.AgeColor4) -} - -func (game *Game) Init() { - // setup the game - var grid *Grid - - if game.Statefile != "" { - g, err := LoadState(game.Statefile) - if err != nil { - log.Fatalf("failed to load game state: %s", err) - } - - grid = g - - game.Width = grid.Width - game.Height = grid.Height - } - - game.ScreenWidth = game.Cellsize * game.Width - game.ScreenHeight = game.Cellsize * game.Height - - game.Camera = Camera{ - ViewPort: f64.Vec2{ - float64(game.ScreenWidth), - float64(game.ScreenHeight), - }, - } - - game.World = ebiten.NewImage(game.ScreenWidth, game.ScreenHeight) - - game.InitGrid(grid) - game.InitPattern() - game.InitTiles() - - game.Index = 0 - game.TicksElapsed = 0 - - game.LastCursorPos = make([]int, 2) -} - -// count the living neighbors of a cell -func (game *Game) CountNeighbors(x, y int) int64 { - var sum int64 - - for nbgX := -1; nbgX < 2; nbgX++ { - for nbgY := -1; nbgY < 2; nbgY++ { - fmt.Printf("nbgX: %d, nbgY: %d\n", nbgX, nbgY) - - var col, row int - if game.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 + game.Width) % game.Width - row = (y + nbgY + game.Height) % game.Height - - } else { - // In traditional grid mode the edges are deadly - if x+nbgX < 0 || x+nbgX >= game.Width || y+nbgY < 0 || y+nbgY >= game.Height { - continue - } - col = x + nbgX - row = y + nbgY - } - - sum += game.Grids[game.Index].Data[row][col] - } - } - - // don't count ourselfes though - sum -= game.Grids[game.Index].Data[y][x] - - return sum -} - -// fill a cell with the given color -func FillCell(tile *ebiten.Image, cellsize int, col color.RGBA) { - vector.DrawFilledRect( - tile, - float32(1), - float32(1), - float32(cellsize-1), - float32(cellsize-1), - col, false, - ) + scene.Draw(screen) } diff --git a/grid.go b/grid.go index 5694653..a20de67 100644 --- a/grid.go +++ b/grid.go @@ -12,16 +12,19 @@ import ( ) type Grid struct { - Data [][]int64 - Width, Height int + Data [][]int64 + Width, Height, Density int + Empty bool } // Create new empty grid and allocate Data according to provided dimensions -func NewGrid(width, height int) *Grid { +func NewGrid(width, height, density int, empty bool) *Grid { grid := &Grid{ - Height: height, - Width: width, - Data: make([][]int64, height), + Height: height, + Width: width, + Density: density, + Data: make([][]int64, height), + Empty: empty, } for y := 0; y < height; y++ { @@ -49,11 +52,11 @@ func (grid *Grid) Clear() { } } -func (grid *Grid) FillRandom(game *Game) { - if !game.Empty { +func (grid *Grid) FillRandom(game *ScenePlay) { + if !grid.Empty { for y := range grid.Data { for x := range grid.Data[y] { - if rand.Intn(game.Density) == 1 { + if rand.Intn(grid.Density) == 1 { grid.Data[y][x] = 1 } } diff --git a/main.go b/main.go index 6374272..46b4204 100644 --- a/main.go +++ b/main.go @@ -1,14 +1,12 @@ package main import ( - "fmt" "log" "os" "github.com/tlinden/golsky/rle" "github.com/hajimehoshi/ebiten/v2" - "github.com/spf13/pflag" ) const ( @@ -36,66 +34,9 @@ func GetRLE(filename string) *rle.RLE { } func main() { - game := &Game{} - showversion := false - var rule string - var rlefile string + config := ParseCommandline() - // commandline params, most configure directly game flags - pflag.IntVarP(&game.Width, "width", "W", 40, "grid width in cells") - pflag.IntVarP(&game.Height, "height", "H", 40, "grid height in cells") - pflag.IntVarP(&game.Cellsize, "cellsize", "c", 8, "cell size in pixels") - pflag.IntVarP(&game.Density, "density", "D", 10, "density of random cells") - pflag.IntVarP(&game.TPG, "ticks-per-generation", "t", 10, - "game speed: the higher the slower (default: 10)") - - pflag.StringVarP(&rule, "rule", "r", "B3/S23", "game rule") - pflag.StringVarP(&rlefile, "rle-file", "f", "", "RLE pattern file") - pflag.StringVarP(&game.Statefile, "load-state-file", "l", "", "game state file") - - pflag.BoolVarP(&showversion, "version", "v", false, "show version") - pflag.BoolVarP(&game.Paused, "paused", "p", false, "do not start simulation (use space to start)") - pflag.BoolVarP(&game.Debug, "debug", "d", false, "show debug info") - pflag.BoolVarP(&game.NoGrid, "nogrid", "n", false, "do not draw grid lines") - pflag.BoolVarP(&game.Empty, "empty", "e", false, "start with an empty screen") - pflag.BoolVarP(&game.Invert, "invert", "i", false, "invert colors (dead cell: black)") - pflag.BoolVarP(&game.ShowEvolution, "show-evolution", "s", false, "show evolution tracks") - pflag.BoolVarP(&game.Wrap, "wrap-around", "w", false, "wrap around grid mode") - - pflag.Parse() - - if showversion { - fmt.Printf("This is golsky version %s\n", VERSION) - os.Exit(0) - } - - // check if we have been given an RLE file to load - game.RLE = GetRLE(rlefile) - if game.RLE != nil { - if game.RLE.Width > game.Width || game.RLE.Height > game.Height { - game.Width = game.RLE.Width * 2 - game.Height = game.RLE.Height * 2 - fmt.Printf("rlew: %d, rleh: %d, w: %d, h: %d\n", - game.RLE.Width, game.RLE.Height, game.Width, game.Height) - } - - // RLE needs an empty grid - game.Empty = true - - // it may come with its own rule - if game.RLE.Rule != "" { - game.Rule = ParseGameRule(game.RLE.Rule) - } - } - - // load rule from commandline when no rule came from RLE file, - // default is B3/S23, aka conways game of life - if game.Rule == nil { - game.Rule = ParseGameRule(rule) - } - - // bootstrap the game - game.Init() + game := NewGame(config, Play) // setup environment ebiten.SetWindowSize(game.ScreenWidth, game.ScreenHeight) diff --git a/scene-play.go b/scene-play.go new file mode 100644 index 0000000..d133f73 --- /dev/null +++ b/scene-play.go @@ -0,0 +1,584 @@ +package main + +import ( + "fmt" + "image" + "image/color" + "log" + "math/rand" + "os" + + "github.com/hajimehoshi/ebiten/v2" + "github.com/hajimehoshi/ebiten/v2/ebitenutil" + "github.com/hajimehoshi/ebiten/v2/inpututil" + "github.com/hajimehoshi/ebiten/v2/vector" + "golang.org/x/image/math/f64" +) + +type Images struct { + Black, White, Age1, Age2, Age3, Age4, Old *ebiten.Image +} + +type ScenePlay struct { + Game *Game + Config *Config + Next SceneName + Whoami SceneName + + Grids []*Grid // 2 grids: one current, one next + History *Grid // holds state of past dead cells for evolution tracks + 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 + Tiles Images // pre-computed tiles for dead and alife cells + Camera Camera // for zoom+move + World *ebiten.Image // actual image we render to + WheelTurned bool // when user turns wheel multiple times, zoom faster + Dragging bool // middle mouse is pressed, move canvas + LastCursorPos []int // used to check if the user is dragging + Markmode bool // enabled with 'c' + MarkTaken bool // true when mouse1 pressed + MarkDone bool // true when mouse1 released, copy cells between Mark+Point + Mark, Point image.Point // area to marks+save + Paused, RunOneStep bool // mutable flags from config + TPG int +} + +func NewPlayScene(game *Game, config *Config) Scene { + scene := &ScenePlay{ + Whoami: Play, + Game: game, + Next: Play, + Config: config, + Paused: config.Paused, + TPG: config.TPG, + RunOneStep: config.RunOneStep, + } + + scene.Init() + + return scene +} + +func (scene *ScenePlay) GetNext() SceneName { + return scene.Next +} + +func (scene *ScenePlay) ResetNext() { + scene.Next = scene.Whoami +} + +func (scene *ScenePlay) SetNext(next SceneName) { + scene.Next = next +} + +func (scene *ScenePlay) Clearscreen() bool { + return true +} + +func (scene *ScenePlay) CheckRule(state int64, neighbors int64) int64 { + var nextstate int64 + + // 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 + // neighbors, survives if it has two or three living neighbors, + // 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) { + nextstate = Alive + } else if state == 1 && Contains(scene.Config.Rule.Death, neighbors) { + nextstate = Alive + } else { + nextstate = Dead + } + + return nextstate +} + +// Update all cells according to the current rule +func (scene *ScenePlay) UpdateCells() { + // count ticks so we know when to actually run + scene.TicksElapsed++ + + if scene.TPG > scene.TicksElapsed { + // need to sleep a little more + return + } + + // next grid index, we just xor 0|1 to 1|0 + next := scene.Index ^ 1 + + // compute life status of cells + for y := 0; y < scene.Config.Height; y++ { + for x := 0; x < scene.Config.Width; x++ { + state := scene.Grids[scene.Index].Data[y][x] // 0|1 == dead or alive + neighbors := scene.CountNeighbors(x, y) // alive neighbor count + + // actually apply the current rules + nextstate := scene.CheckRule(state, neighbors) + + // change state of current cell in next grid + scene.Grids[next].Data[y][x] = nextstate + + // set history to current generation so we can infer the + // age of the cell's state during rendering and use it to + // deduce the color to use if evolution tracking is enabled + if state != nextstate { + scene.History.Data[y][x] = scene.Generations + } + } + } + + // switch grid for rendering + scene.Index ^= 1 + + // global stats counter + scene.Generations++ + + if scene.Config.RunOneStep { + // setp-wise mode, halt the game + scene.Config.RunOneStep = false + } + + // reset speed counter + scene.TicksElapsed = 0 +} + +func (scene *ScenePlay) Reset() { + scene.Paused = true + scene.InitGrid(nil) + scene.Paused = false +} + +// check user input +func (scene *ScenePlay) CheckInput() { + if inpututil.IsKeyJustPressed(ebiten.KeyQ) { + os.Exit(0) + } + + if inpututil.IsKeyJustPressed(ebiten.KeyC) { + fmt.Println("mark mode on") + scene.Markmode = true + } + + if scene.Markmode { + return + } + + if inpututil.IsKeyJustPressed(ebiten.KeySpace) || inpututil.IsKeyJustPressed(ebiten.KeyEnter) { + scene.Paused = !scene.Paused + } + + if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { + scene.ToggleCellOnCursorPos(Alive) + scene.Paused = true // drawing while running makes no sense + } + + if ebiten.IsMouseButtonPressed(ebiten.MouseButtonRight) { + scene.ToggleCellOnCursorPos(Dead) + scene.Paused = true // drawing while running makes no sense + } + + if ebiten.IsKeyPressed(ebiten.KeyPageDown) { + if scene.Config.TPG < 120 { + scene.Config.TPG++ + } + } + + if ebiten.IsKeyPressed(ebiten.KeyPageUp) { + if scene.TPG > 1 { + scene.TPG-- + } + } + + if inpututil.IsKeyJustPressed(ebiten.KeyS) { + scene.SaveState() + } + + if inpututil.IsKeyJustPressed(ebiten.KeyR) { + scene.Reset() + } + + if scene.Paused { + if inpututil.IsKeyJustPressed(ebiten.KeyN) { + scene.Config.RunOneStep = true + } + } +} + +// Check dragging input. move the canvas with the mouse while pressing +// the middle mouse button, zoom in and out using the wheel. +func (scene *ScenePlay) CheckDraggingInput() { + if scene.Markmode { + return + } + + // move canvas + if scene.Dragging && !ebiten.IsMouseButtonPressed(ebiten.MouseButton1) { + // release + scene.Dragging = false + } + + if !scene.Dragging && ebiten.IsMouseButtonPressed(ebiten.MouseButton1) { + // start dragging + scene.Dragging = true + scene.LastCursorPos[0], scene.LastCursorPos[1] = ebiten.CursorPosition() + } + + if scene.Dragging { + x, y := ebiten.CursorPosition() + + if x != scene.LastCursorPos[0] || y != scene.LastCursorPos[1] { + // actually drag by mouse cursor pos diff to last cursor pos + scene.Camera.Position[0] -= float64(x - scene.LastCursorPos[0]) + scene.Camera.Position[1] -= float64(y - scene.LastCursorPos[1]) + } + + scene.LastCursorPos[0], scene.LastCursorPos[1] = ebiten.CursorPosition() + } + + // also support the arrow keys to move the canvas + if ebiten.IsKeyPressed(ebiten.KeyArrowLeft) { + scene.Camera.Position[0] -= 1 + } + if ebiten.IsKeyPressed(ebiten.KeyArrowRight) { + scene.Camera.Position[0] += 1 + } + if ebiten.IsKeyPressed(ebiten.KeyArrowUp) { + scene.Camera.Position[1] -= 1 + } + if ebiten.IsKeyPressed(ebiten.KeyArrowDown) { + scene.Camera.Position[1] += 1 + } + + // Zoom + _, dy := ebiten.Wheel() + step := 1 + + if scene.WheelTurned { + // if keep scrolling the wheel, zoom faster + step = 50 + } else { + scene.WheelTurned = false + } + + if dy < 0 { + if scene.Camera.ZoomFactor > -2400 { + scene.Camera.ZoomFactor -= step + } + } + + if dy > 0 { + if scene.Camera.ZoomFactor < 2400 { + scene.Camera.ZoomFactor += step + } + } + + if inpututil.IsKeyJustPressed(ebiten.KeyEscape) { + scene.Camera.Reset() + } + +} + +func (scene *ScenePlay) GetWorldCursorPos() image.Point { + worldX, worldY := scene.Camera.ScreenToWorld(ebiten.CursorPosition()) + return image.Point{ + X: int(worldX) / scene.Config.Cellsize, + Y: int(worldY) / scene.Config.Cellsize, + } +} + +func (scene *ScenePlay) CheckMarkInput() { + if !scene.Markmode { + return + } + + if ebiten.IsMouseButtonPressed(ebiten.MouseButton0) { + if !scene.MarkTaken { + scene.Mark = scene.GetWorldCursorPos() + scene.MarkTaken = true + scene.MarkDone = false + } + + scene.Point = scene.GetWorldCursorPos() + fmt.Printf("Mark: %v, Current: %v\n", scene.Mark, scene.Point) + } else if inpututil.IsMouseButtonJustReleased(ebiten.MouseButton0) { + scene.Markmode = false + scene.MarkTaken = false + scene.MarkDone = true + } +} + +func (scene *ScenePlay) SaveState() { + filename := GetFilename(scene.Generations) + err := scene.Grids[scene.Index].SaveState(filename) + if err != nil { + log.Printf("failed to save game state to %s: %s", filename, err) + } + log.Printf("saved game state to %s at generation %d\n", filename, scene.Generations) +} + +func (scene *ScenePlay) Update() error { + scene.CheckInput() + scene.CheckDraggingInput() + scene.CheckMarkInput() + + if !scene.Paused || scene.RunOneStep { + scene.UpdateCells() + } + + return nil +} + +// set a cell to alive or dead +func (scene *ScenePlay) ToggleCellOnCursorPos(alive int64) { + // use cursor pos relative to the world + worldX, worldY := scene.Camera.ScreenToWorld(ebiten.CursorPosition()) + x := int(worldX) / scene.Config.Cellsize + y := int(worldY) / scene.Config.Cellsize + + //fmt.Printf("cell at %d,%d\n", x, y) + + if x > -1 && y > -1 { + scene.Grids[scene.Index].Data[y][x] = alive + scene.History.Data[y][x] = 1 + } +} + +// draw the new grid state +func (scene *ScenePlay) Draw(screen *ebiten.Image) { + // we fill the whole screen with a background color, the cells + // themselfes will be 1px smaller as their nominal size, producing + // a nice grey grid with grid lines + op := &ebiten.DrawImageOptions{} + + if scene.Config.NoGrid { + scene.World.Fill(scene.White) + } else { + scene.World.Fill(scene.Grey) + } + + for y := 0; y < scene.Config.Height; y++ { + for x := 0; x < scene.Config.Width; x++ { + op.GeoM.Reset() + op.GeoM.Translate(float64(x*scene.Config.Cellsize), float64(y*scene.Config.Cellsize)) + + age := scene.Generations - scene.History.Data[y][x] + + switch scene.Grids[scene.Index].Data[y][x] { + case 1: + if age > 50 && scene.Config.ShowEvolution { + scene.World.DrawImage(scene.Tiles.Old, op) + } else { + scene.World.DrawImage(scene.Tiles.Black, op) + } + case 0: + if scene.History.Data[y][x] > 1 && scene.Config.ShowEvolution { + switch { + case age < 10: + scene.World.DrawImage(scene.Tiles.Age1, op) + case age < 20: + scene.World.DrawImage(scene.Tiles.Age2, op) + case age < 30: + scene.World.DrawImage(scene.Tiles.Age3, op) + default: + scene.World.DrawImage(scene.Tiles.Age4, op) + } + } else { + scene.World.DrawImage(scene.Tiles.White, op) + } + } + } + } + + scene.Camera.Render(scene.World, screen) + + if scene.Config.Debug { + paused := "" + if scene.Paused { + paused = "-- paused --" + } + + ebitenutil.DebugPrint( + screen, + fmt.Sprintf("FPS: %0.2f, TPG: %d, Mem: %0.2f MB, Generations: %d %s", + ebiten.ActualTPS(), scene.TPG, GetMem(), scene.Generations, paused), + ) + } +} + +// FIXME: move these into Grid +// load a pre-computed pattern from RLE file +func (scene *ScenePlay) InitPattern() { + if scene.Config.RLE != nil { + startX := (scene.Config.Width / 2) - (scene.Config.RLE.Width / 2) + startY := (scene.Config.Height / 2) - (scene.Config.RLE.Height / 2) + var y, x int + + for rowIndex, patternRow := range scene.Config.RLE.Pattern { + for colIndex := range patternRow { + if scene.Config.RLE.Pattern[rowIndex][colIndex] > 0 { + x = colIndex + startX + y = rowIndex + startY + + scene.History.Data[y][x] = 1 + scene.Grids[0].Data[y][x] = 1 + } + } + } + } +} + +func (scene *ScenePlay) InitGrid(grid *Grid) { + if grid != nil { + // use pre-loaded grid + scene.Grids = []*Grid{ + grid, + NewGrid(grid.Width, grid.Height, 0, false), + } + + scene.History = NewGrid(grid.Width, grid.Height, 0, false) + + return + } + + 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) + + for y := 0; y < scene.Config.Height; y++ { + if !scene.Config.Empty { + for x := 0; x < scene.Config.Width; x++ { + if rand.Intn(scene.Config.Density) == 1 { + history.Data[y][x] = 1 + grida.Data[y][x] = 1 + } + } + } + } + + scene.Grids = []*Grid{ + grida, + gridb, + } + + scene.History = history +} + +// prepare tile images +func (scene *ScenePlay) InitTiles() { + scene.Grey = color.RGBA{128, 128, 128, 0xff} + scene.Old = color.RGBA{255, 30, 30, 0xff} + + scene.Black = color.RGBA{0, 0, 0, 0xff} + scene.White = color.RGBA{200, 200, 200, 0xff} + scene.AgeColor1 = color.RGBA{255, 195, 97, 0xff} // FIXME: use slice! + scene.AgeColor2 = color.RGBA{255, 211, 140, 0xff} + scene.AgeColor3 = color.RGBA{255, 227, 181, 0xff} + scene.AgeColor4 = color.RGBA{255, 240, 224, 0xff} + + if scene.Config.Invert { + scene.White = color.RGBA{0, 0, 0, 0xff} + scene.Black = color.RGBA{200, 200, 200, 0xff} + + scene.AgeColor1 = color.RGBA{82, 38, 0, 0xff} + scene.AgeColor2 = color.RGBA{66, 35, 0, 0xff} + scene.AgeColor3 = color.RGBA{43, 27, 0, 0xff} + scene.AgeColor4 = color.RGBA{25, 17, 0, 0xff} + } + + scene.Tiles.Black = ebiten.NewImage(scene.Config.Cellsize, scene.Config.Cellsize) + scene.Tiles.White = ebiten.NewImage(scene.Config.Cellsize, scene.Config.Cellsize) + scene.Tiles.Old = ebiten.NewImage(scene.Config.Cellsize, scene.Config.Cellsize) + scene.Tiles.Age1 = ebiten.NewImage(scene.Config.Cellsize, scene.Config.Cellsize) + scene.Tiles.Age2 = ebiten.NewImage(scene.Config.Cellsize, scene.Config.Cellsize) + scene.Tiles.Age3 = ebiten.NewImage(scene.Config.Cellsize, scene.Config.Cellsize) + scene.Tiles.Age4 = ebiten.NewImage(scene.Config.Cellsize, scene.Config.Cellsize) + + cellsize := scene.Config.ScreenWidth / scene.Config.Cellsize + + FillCell(scene.Tiles.Black, cellsize, scene.Black) + FillCell(scene.Tiles.White, cellsize, scene.White) + FillCell(scene.Tiles.Old, cellsize, scene.Old) + FillCell(scene.Tiles.Age1, cellsize, scene.AgeColor1) + FillCell(scene.Tiles.Age2, cellsize, scene.AgeColor2) + FillCell(scene.Tiles.Age3, cellsize, scene.AgeColor3) + FillCell(scene.Tiles.Age4, cellsize, scene.AgeColor4) +} + +func (scene *ScenePlay) Init() { + // setup the scene + var grid *Grid + + if scene.Config.StateGrid != nil { + grid = scene.Config.StateGrid + + } + + scene.Camera = Camera{ + ViewPort: f64.Vec2{ + float64(scene.Config.ScreenWidth), + float64(scene.Config.ScreenHeight), + }, + } + + scene.World = ebiten.NewImage(scene.Config.ScreenWidth, scene.Config.ScreenHeight) + + scene.InitGrid(grid) + scene.InitPattern() + scene.InitTiles() + + scene.Index = 0 + scene.TicksElapsed = 0 + + scene.LastCursorPos = make([]int, 2) +} + +// count the living neighbors of a cell +func (scene *ScenePlay) CountNeighbors(x, y int) int64 { + var sum int64 + + for nbgX := -1; nbgX < 2; nbgX++ { + for nbgY := -1; nbgY < 2; nbgY++ { + var col, row int + if scene.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 + scene.Config.Width) % scene.Config.Width + row = (y + nbgY + scene.Config.Height) % scene.Config.Height + + } else { + // In traditional grid mode the edges are deadly + if x+nbgX < 0 || x+nbgX >= scene.Config.Width || y+nbgY < 0 || y+nbgY >= scene.Config.Height { + continue + } + col = x + nbgX + row = y + nbgY + } + + sum += scene.Grids[scene.Index].Data[row][col] + } + } + + // don't count ourselfes though + sum -= scene.Grids[scene.Index].Data[y][x] + + return sum +} + +// fill a cell with the given color +func FillCell(tile *ebiten.Image, cellsize int, col color.RGBA) { + vector.DrawFilledRect( + tile, + float32(1), + float32(1), + float32(cellsize-1), + float32(cellsize-1), + col, false, + ) +} diff --git a/scene.go b/scene.go new file mode 100644 index 0000000..c0fe759 --- /dev/null +++ b/scene.go @@ -0,0 +1,25 @@ +package main + +import "github.com/hajimehoshi/ebiten/v2" + +// Wrapper for different screens to be shown, as Welcome, Options, +// About, Menu Level and of course the actual game +// Scenes are responsible for screen clearing! That way a scene is able +// to render its content onto the running level, e.g. the options scene +// etc. + +type SceneName int + +type Scene interface { + SetNext(SceneName) + GetNext() SceneName + ResetNext() + Clearscreen() bool + Update() error + Draw(screen *ebiten.Image) +} + +const ( + Menu = iota // main top level menu + Play // actual playing happens here +)