aboutsummaryrefslogtreecommitdiff
path: root/internal/model/terrain.go
blob: 7772620c2ef1746069033eebc4e0c2d5c9902373 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
}