package model import "math/rand" type TerrainColor struct { Color string `yaml:"color"` Weight int `yaml:"weight"` } type Terrain struct { Name string `yaml:"name"` Symbol string `yaml:"symbol"` ASCII string `yaml:"ascii"` Colors []TerrainColor `yaml:"colors"` } func (t Terrain) GetSymbol(unicode bool) string { if unicode { return t.Symbol } return t.ASCII } func (t Terrain) PickColor() string { if len(t.Colors) == 0 { return "0" } if len(t.Colors) == 1 { return t.Colors[0].Color } total := 0 for _, c := range t.Colors { total += c.Weight } r := rand.Intn(total) for _, c := range t.Colors { r -= c.Weight if r < 0 { return c.Color } } return t.Colors[0].Color }