mirror of
https://codeberg.org/scip/golsky.git
synced 2025-12-16 12:10:58 +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
|
||||
|
||||
- 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
|
||||
}
|
||||
|
||||
// check if we have been given an RLE file to load, then load it and
|
||||
// adjust game settings accordingly
|
||||
// check if we have been given an RLE or LIF file to load, then load
|
||||
// it and adjust game settings accordingly
|
||||
func (config *Config) ParseRLE(rlefile string) error {
|
||||
if rlefile == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
rleobj, err := rle.GetRLE(rlefile)
|
||||
if err != nil {
|
||||
return err
|
||||
var rleobj *rle.RLE
|
||||
|
||||
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 {
|
||||
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
|
||||
@@ -151,24 +164,6 @@ func (config *Config) ParseRLE(rlefile string) error {
|
||||
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 {
|
||||
if filename == "" {
|
||||
return nil
|
||||
@@ -203,8 +198,7 @@ func ParseCommandline() (*Config, error) {
|
||||
"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.StringVarP(&rlefile, "pattern-file", "f", "", "RLE or LIF pattern file")
|
||||
|
||||
pflag.BoolVarP(&config.ShowVersion, "version", "v", false, "show version")
|
||||
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
|
||||
}
|
||||
|
||||
err = config.ParseStatefile()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// load rule from commandline when no rule came from RLE file,
|
||||
// default is B3/S23, aka conways game of life
|
||||
if config.Rule == nil {
|
||||
@@ -254,7 +243,6 @@ func (config *Config) TogglePaused() {
|
||||
}
|
||||
|
||||
func (config *Config) ToggleDebugging() {
|
||||
fmt.Println("DEBUG TOGGLED")
|
||||
config.Debug = !config.Debug
|
||||
}
|
||||
|
||||
|
||||
31
grid.go
31
grid.go
@@ -9,7 +9,6 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/alecthomas/repr"
|
||||
"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
|
||||
func LoadState(filename string) (*Grid, error) {
|
||||
func LoadLIF(filename string) (*rle.RLE, error) {
|
||||
fd, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -126,12 +125,26 @@ func LoadState(filename string) (*Grid, error) {
|
||||
|
||||
gothead := false
|
||||
|
||||
grid := &Grid{}
|
||||
grid := &rle.RLE{}
|
||||
|
||||
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 {
|
||||
break
|
||||
}
|
||||
@@ -141,7 +154,7 @@ func LoadState(filename string) (*Grid, error) {
|
||||
|
||||
gothead = true
|
||||
|
||||
row := make([]int64, len(items))
|
||||
row := make([]int, len(items))
|
||||
|
||||
for idx, item := range items {
|
||||
switch item {
|
||||
@@ -156,15 +169,14 @@ func LoadState(filename string) (*Grid, error) {
|
||||
}
|
||||
}
|
||||
|
||||
repr.Println(row)
|
||||
grid.Data = append(grid.Data, row)
|
||||
grid.Pattern = append(grid.Pattern, row)
|
||||
}
|
||||
|
||||
// sanity check the grid
|
||||
explen := 0
|
||||
rows := 0
|
||||
first := true
|
||||
for _, row := range grid.Data {
|
||||
for _, row := range grid.Pattern {
|
||||
length := len(row)
|
||||
|
||||
if first {
|
||||
@@ -184,7 +196,6 @@ func LoadState(filename string) (*Grid, error) {
|
||||
grid.Width = explen
|
||||
grid.Height = rows
|
||||
|
||||
grid.Dump()
|
||||
return grid, nil
|
||||
}
|
||||
|
||||
|
||||
28
play.go
28
play.go
@@ -156,7 +156,7 @@ func (scene *ScenePlay) UpdateCells() {
|
||||
|
||||
func (scene *ScenePlay) Reset() {
|
||||
scene.Config.Paused = true
|
||||
scene.InitGrid(nil)
|
||||
scene.InitGrid()
|
||||
scene.Config.Paused = false
|
||||
}
|
||||
|
||||
@@ -385,7 +385,7 @@ func (scene *ScenePlay) SaveRectRLE() {
|
||||
func (scene *ScenePlay) Update() error {
|
||||
if scene.Config.Restart {
|
||||
scene.Config.Restart = false
|
||||
scene.InitGrid(nil)
|
||||
scene.InitGrid()
|
||||
scene.InitCache()
|
||||
return nil
|
||||
}
|
||||
@@ -565,19 +565,7 @@ func (scene *ScenePlay) InitCache() {
|
||||
}
|
||||
|
||||
// initialize grid[s], either using pre-computed from state or rle file, or random
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
@@ -637,12 +625,6 @@ func (scene *ScenePlay) InitTiles() {
|
||||
|
||||
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),
|
||||
@@ -671,10 +653,10 @@ func (scene *ScenePlay) Init() {
|
||||
|
||||
if scene.Config.DelayedStart && !scene.Config.Empty {
|
||||
scene.Config.Empty = true
|
||||
scene.InitGrid(grid)
|
||||
scene.InitGrid()
|
||||
scene.Config.Empty = false
|
||||
} else {
|
||||
scene.InitGrid(grid)
|
||||
scene.InitGrid()
|
||||
}
|
||||
|
||||
scene.InitPattern()
|
||||
|
||||
Reference in New Issue
Block a user