Simplify indifferent access to sets

This commit is contained in:
2018-03-18 18:19:05 +00:00
parent 738abfc4a8
commit 5e91c31139

View File

@@ -18,6 +18,8 @@ type MapSet struct {
RightCount int RightCount int
CenterCount int CenterCount int
Palette []string
// TODO: is there any more structure than this? Should I preserve empty lines? // TODO: is there any more structure than this? Should I preserve empty lines?
SurfacePalette []string SurfacePalette []string
LeftPalette []string LeftPalette []string
@@ -25,6 +27,10 @@ type MapSet struct {
CenterPalette []string CenterPalette []string
} }
func (m *MapSet) Count() int {
return m.SurfaceCount + m.LeftCount + m.RightCount + m.CenterCount
}
func LoadSets(dir string) (map[string]*MapSet, error) { func LoadSets(dir string) (map[string]*MapSet, error) {
fis, err := ioutil.ReadDir(dir) fis, err := ioutil.ReadDir(dir)
if err != nil { if err != nil {
@@ -73,17 +79,24 @@ func LoadSet(filename string) (*MapSet, error) {
return nil, err return nil, err
} }
if err := consumePalette(s, out.SurfaceCount, &out.SurfacePalette); err != nil { fmt.Println(filename)
return nil, fmt.Errorf("Failed to read surface palette: %v", err) out.Palette = make([]string, out.Count())
} offset := 0
if err := consumePalette(s, out.LeftCount, &out.LeftPalette); err != nil {
return nil, fmt.Errorf("Failed to read left palette: %v", err) out.SurfacePalette = out.Palette[offset : offset+out.SurfaceCount : offset+out.SurfaceCount]
} offset += out.SurfaceCount
if err := consumePalette(s, out.RightCount, &out.RightPalette); err != nil {
return nil, fmt.Errorf("Failed to read right palette: %v", err) out.LeftPalette = out.Palette[offset : offset+out.LeftCount : offset+out.LeftCount]
} offset += out.LeftCount
if err := consumePalette(s, out.CenterCount, &out.CenterPalette); err != nil {
return nil, fmt.Errorf("Failed to read center palette: %v", err) out.RightPalette = out.Palette[offset : offset+out.RightCount : offset+out.RightCount]
offset += out.RightCount
out.CenterPalette = out.Palette[offset : offset+out.CenterCount : offset+out.CenterCount]
offset += out.CenterCount
if err := consumePalette(s, out.Palette); err != nil {
return nil, fmt.Errorf("Failed to read palette: %v", err)
} }
return out, nil return out, nil
@@ -128,18 +141,15 @@ func consumeDefs(scanner *asciiscan.Scanner, in *MapSet) error {
return nil return nil
} }
func consumePalette(scanner *asciiscan.Scanner, n int, in *[]string) error { func consumePalette(scanner *asciiscan.Scanner, into []string) error {
out := make([]string, 0, n) for i := 0; i < len(into); i++ {
for i := 0; i < n; i++ {
str, err := scanner.ConsumeString() str, err := scanner.ConsumeString()
if err != nil { if err != nil {
return err // EOF is bad: the number of entries is explicit return err // EOF is bad: the number of entries is explicit
} }
out = append(out, str) into[i] = str
} }
*in = out
return nil return nil
} }