mirror of
https://codeberg.org/scip/golsky.git
synced 2025-12-16 20:20:57 +01:00
normalized pattern file loading and saving, only one option for loading: -f
This commit is contained in:
2
TODO.md
2
TODO.md
@@ -3,3 +3,5 @@
|
|||||||
- changing options mid-game has no effect in most cases, even after a restart
|
- changing options mid-game has no effect in most cases, even after a restart
|
||||||
|
|
||||||
- initial zoom pos doesnt work for small grids
|
- initial zoom pos doesnt work for small grids
|
||||||
|
|
||||||
|
- fix generation count: starts even when delayedstart is on or on restart
|
||||||
|
|||||||
52
config.go
52
config.go
@@ -112,20 +112,33 @@ func (config *Config) ParseGeom(geom string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if we have been given an RLE file to load, then load it and
|
// check if we have been given an RLE or LIF file to load, then load
|
||||||
// adjust game settings accordingly
|
// it and adjust game settings accordingly
|
||||||
func (config *Config) ParseRLE(rlefile string) error {
|
func (config *Config) ParseRLE(rlefile string) error {
|
||||||
if rlefile == "" {
|
if rlefile == "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
rleobj, err := rle.GetRLE(rlefile)
|
var rleobj *rle.RLE
|
||||||
if err != nil {
|
|
||||||
return err
|
if strings.HasSuffix(rlefile, ".lif") {
|
||||||
|
lifobj, err := LoadLIF(rlefile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rleobj = lifobj
|
||||||
|
} else {
|
||||||
|
rleobject, err := rle.GetRLE(rlefile)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rleobj = rleobject
|
||||||
}
|
}
|
||||||
|
|
||||||
if rleobj == nil {
|
if rleobj == nil {
|
||||||
return errors.New("failed to load RLE file (uncatched module error)")
|
return errors.New("failed to load pattern file (uncatched module error)")
|
||||||
}
|
}
|
||||||
|
|
||||||
config.RLE = rleobj
|
config.RLE = rleobj
|
||||||
@@ -151,24 +164,6 @@ func (config *Config) ParseRLE(rlefile string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse a state file, if given, and adjust game settings accordingly
|
|
||||||
func (config *Config) ParseStatefile() error {
|
|
||||||
if config.Statefile == "" {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
grid, err := LoadState(config.Statefile)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to load game state: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
config.Width = grid.Width
|
|
||||||
config.Height = grid.Height
|
|
||||||
config.StateGrid = grid
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (config *Config) EnableCPUProfiling(filename string) error {
|
func (config *Config) EnableCPUProfiling(filename string) error {
|
||||||
if filename == "" {
|
if filename == "" {
|
||||||
return nil
|
return nil
|
||||||
@@ -203,8 +198,7 @@ func ParseCommandline() (*Config, error) {
|
|||||||
"game speed: the higher the slower (default: 10)")
|
"game speed: the higher the slower (default: 10)")
|
||||||
|
|
||||||
pflag.StringVarP(&rule, "rule", "r", "B3/S23", "game rule")
|
pflag.StringVarP(&rule, "rule", "r", "B3/S23", "game rule")
|
||||||
pflag.StringVarP(&rlefile, "rle-file", "f", "", "RLE pattern file")
|
pflag.StringVarP(&rlefile, "pattern-file", "f", "", "RLE or LIF pattern file")
|
||||||
pflag.StringVarP(&config.Statefile, "load-state-file", "l", "", "game state file")
|
|
||||||
|
|
||||||
pflag.BoolVarP(&config.ShowVersion, "version", "v", false, "show version")
|
pflag.BoolVarP(&config.ShowVersion, "version", "v", false, "show version")
|
||||||
pflag.BoolVarP(&config.Paused, "paused", "p", false, "do not start simulation (use space to start)")
|
pflag.BoolVarP(&config.Paused, "paused", "p", false, "do not start simulation (use space to start)")
|
||||||
@@ -232,11 +226,6 @@ func ParseCommandline() (*Config, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = config.ParseStatefile()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// load rule from commandline when no rule came from RLE file,
|
// load rule from commandline when no rule came from RLE file,
|
||||||
// default is B3/S23, aka conways game of life
|
// default is B3/S23, aka conways game of life
|
||||||
if config.Rule == nil {
|
if config.Rule == nil {
|
||||||
@@ -254,7 +243,6 @@ func (config *Config) TogglePaused() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (config *Config) ToggleDebugging() {
|
func (config *Config) ToggleDebugging() {
|
||||||
fmt.Println("DEBUG TOGGLED")
|
|
||||||
config.Debug = !config.Debug
|
config.Debug = !config.Debug
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
31
grid.go
31
grid.go
@@ -9,7 +9,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/alecthomas/repr"
|
|
||||||
"github.com/tlinden/golsky/rle"
|
"github.com/tlinden/golsky/rle"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -114,7 +113,7 @@ func (grid *Grid) LoadRLE(pattern *rle.RLE) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// load a lif file parameters like R and P are not supported yet
|
// load a lif file parameters like R and P are not supported yet
|
||||||
func LoadState(filename string) (*Grid, error) {
|
func LoadLIF(filename string) (*rle.RLE, error) {
|
||||||
fd, err := os.Open(filename)
|
fd, err := os.Open(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -126,12 +125,26 @@ func LoadState(filename string) (*Grid, error) {
|
|||||||
|
|
||||||
gothead := false
|
gothead := false
|
||||||
|
|
||||||
grid := &Grid{}
|
grid := &rle.RLE{}
|
||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
items := strings.Split(scanner.Text(), "")
|
line := scanner.Text()
|
||||||
|
items := strings.Split(line, "")
|
||||||
|
|
||||||
if len(items) > 0 && (items[0] == "#") {
|
if len(items) < 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.Contains(line, "# r") {
|
||||||
|
parts := strings.Split(line, " ")
|
||||||
|
if len(parts) == 2 {
|
||||||
|
grid.Rule = parts[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if items[0] == "#" {
|
||||||
if gothead {
|
if gothead {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -141,7 +154,7 @@ func LoadState(filename string) (*Grid, error) {
|
|||||||
|
|
||||||
gothead = true
|
gothead = true
|
||||||
|
|
||||||
row := make([]int64, len(items))
|
row := make([]int, len(items))
|
||||||
|
|
||||||
for idx, item := range items {
|
for idx, item := range items {
|
||||||
switch item {
|
switch item {
|
||||||
@@ -156,15 +169,14 @@ func LoadState(filename string) (*Grid, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
repr.Println(row)
|
grid.Pattern = append(grid.Pattern, row)
|
||||||
grid.Data = append(grid.Data, row)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// sanity check the grid
|
// sanity check the grid
|
||||||
explen := 0
|
explen := 0
|
||||||
rows := 0
|
rows := 0
|
||||||
first := true
|
first := true
|
||||||
for _, row := range grid.Data {
|
for _, row := range grid.Pattern {
|
||||||
length := len(row)
|
length := len(row)
|
||||||
|
|
||||||
if first {
|
if first {
|
||||||
@@ -184,7 +196,6 @@ func LoadState(filename string) (*Grid, error) {
|
|||||||
grid.Width = explen
|
grid.Width = explen
|
||||||
grid.Height = rows
|
grid.Height = rows
|
||||||
|
|
||||||
grid.Dump()
|
|
||||||
return grid, nil
|
return grid, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
28
play.go
28
play.go
@@ -156,7 +156,7 @@ func (scene *ScenePlay) UpdateCells() {
|
|||||||
|
|
||||||
func (scene *ScenePlay) Reset() {
|
func (scene *ScenePlay) Reset() {
|
||||||
scene.Config.Paused = true
|
scene.Config.Paused = true
|
||||||
scene.InitGrid(nil)
|
scene.InitGrid()
|
||||||
scene.Config.Paused = false
|
scene.Config.Paused = false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -385,7 +385,7 @@ func (scene *ScenePlay) SaveRectRLE() {
|
|||||||
func (scene *ScenePlay) Update() error {
|
func (scene *ScenePlay) Update() error {
|
||||||
if scene.Config.Restart {
|
if scene.Config.Restart {
|
||||||
scene.Config.Restart = false
|
scene.Config.Restart = false
|
||||||
scene.InitGrid(nil)
|
scene.InitGrid()
|
||||||
scene.InitCache()
|
scene.InitCache()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -565,19 +565,7 @@ func (scene *ScenePlay) InitCache() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// initialize grid[s], either using pre-computed from state or rle file, or random
|
// initialize grid[s], either using pre-computed from state or rle file, or random
|
||||||
func (scene *ScenePlay) InitGrid(grid *Grid) {
|
func (scene *ScenePlay) InitGrid() {
|
||||||
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)
|
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)
|
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)
|
history := NewGrid(scene.Config.Width, scene.Config.Height, scene.Config.Density, scene.Config.Empty)
|
||||||
@@ -637,12 +625,6 @@ func (scene *ScenePlay) InitTiles() {
|
|||||||
|
|
||||||
func (scene *ScenePlay) Init() {
|
func (scene *ScenePlay) Init() {
|
||||||
// setup the scene
|
// setup the scene
|
||||||
var grid *Grid
|
|
||||||
|
|
||||||
if scene.Config.StateGrid != nil {
|
|
||||||
grid = scene.Config.StateGrid
|
|
||||||
}
|
|
||||||
|
|
||||||
scene.Camera = Camera{
|
scene.Camera = Camera{
|
||||||
ViewPort: f64.Vec2{
|
ViewPort: f64.Vec2{
|
||||||
float64(scene.Config.ScreenWidth),
|
float64(scene.Config.ScreenWidth),
|
||||||
@@ -671,10 +653,10 @@ func (scene *ScenePlay) Init() {
|
|||||||
|
|
||||||
if scene.Config.DelayedStart && !scene.Config.Empty {
|
if scene.Config.DelayedStart && !scene.Config.Empty {
|
||||||
scene.Config.Empty = true
|
scene.Config.Empty = true
|
||||||
scene.InitGrid(grid)
|
scene.InitGrid()
|
||||||
scene.Config.Empty = false
|
scene.Config.Empty = false
|
||||||
} else {
|
} else {
|
||||||
scene.InitGrid(grid)
|
scene.InitGrid()
|
||||||
}
|
}
|
||||||
|
|
||||||
scene.InitPattern()
|
scene.InitPattern()
|
||||||
|
|||||||
Reference in New Issue
Block a user