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