2020-06-22 12:09:39 +00:00
|
|
|
package generator
|
|
|
|
|
|
|
|
import (
|
2020-06-22 13:42:19 +00:00
|
|
|
"fmt"
|
2020-06-22 12:09:39 +00:00
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
"image/png"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/tribalwarshelp/shared/models"
|
|
|
|
)
|
|
|
|
|
2020-06-22 13:42:19 +00:00
|
|
|
const (
|
2020-06-22 15:24:58 +00:00
|
|
|
defaultBackgroundColor = "#000"
|
2020-06-22 13:42:19 +00:00
|
|
|
defaultMapSize = 1000
|
|
|
|
)
|
|
|
|
|
2020-06-22 12:09:39 +00:00
|
|
|
type Marker struct {
|
2020-06-22 15:24:58 +00:00
|
|
|
Villages []*models.Village `json:"villages" gqlgen:"villages" xml:"villages"`
|
|
|
|
Color string `json:"color" gqlgen:"color" xml:"color"`
|
2020-06-22 12:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
2020-06-22 13:42:19 +00:00
|
|
|
Markers []*Marker
|
|
|
|
Destination io.Writer
|
|
|
|
MapSize int
|
|
|
|
ContinentGrid bool
|
|
|
|
ContinentNumbers bool
|
|
|
|
BackgroundColor string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *Config) init() {
|
|
|
|
if cfg.BackgroundColor == "" {
|
|
|
|
cfg.BackgroundColor = defaultBackgroundColor
|
|
|
|
}
|
|
|
|
if cfg.MapSize <= 0 {
|
|
|
|
cfg.MapSize = defaultMapSize
|
|
|
|
}
|
2020-06-22 12:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Generate(cfg Config) error {
|
2020-06-22 13:42:19 +00:00
|
|
|
cfg.init()
|
2020-06-22 12:09:39 +00:00
|
|
|
upLeft := image.Point{0, 0}
|
|
|
|
lowRight := image.Point{cfg.MapSize, cfg.MapSize}
|
|
|
|
img := image.NewRGBA(image.Rectangle{upLeft, lowRight})
|
2020-06-22 13:42:19 +00:00
|
|
|
|
|
|
|
backgroundColor, err := parseHexColorFast(cfg.BackgroundColor)
|
2020-06-22 12:09:39 +00:00
|
|
|
if err != nil {
|
2020-06-22 13:42:19 +00:00
|
|
|
return errors.Wrap(err, "map-generator")
|
2020-06-22 12:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Background.
|
|
|
|
for y := 0; y < cfg.MapSize; y++ {
|
|
|
|
for x := 0; x < cfg.MapSize; x++ {
|
|
|
|
img.Set(x, y, backgroundColor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Markers
|
|
|
|
for _, marker := range cfg.Markers {
|
|
|
|
parsedColor, err := parseHexColorFast(marker.Color)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, village := range marker.Villages {
|
|
|
|
img.Set(village.X, village.Y, parsedColor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Continents
|
2020-06-22 13:42:19 +00:00
|
|
|
if cfg.ContinentGrid {
|
|
|
|
for y := cfg.MapSize / 10; y < cfg.MapSize; y += cfg.MapSize / 10 {
|
|
|
|
for x := 0; x < cfg.MapSize; x++ {
|
2020-06-22 15:24:58 +00:00
|
|
|
img.Set(x, y, color.White)
|
2020-06-22 13:42:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for x := cfg.MapSize / 10; x < cfg.MapSize; x += cfg.MapSize / 10 {
|
|
|
|
for y := 0; y < cfg.MapSize; y++ {
|
2020-06-22 15:24:58 +00:00
|
|
|
img.Set(x, y, color.White)
|
2020-06-22 13:42:19 +00:00
|
|
|
}
|
2020-06-22 12:09:39 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-22 13:42:19 +00:00
|
|
|
|
|
|
|
if cfg.ContinentNumbers {
|
|
|
|
continent := 0
|
|
|
|
for y := cfg.MapSize / 10; y <= cfg.MapSize; y += cfg.MapSize / 10 {
|
|
|
|
for x := cfg.MapSize / 10; x <= cfg.MapSize; x += cfg.MapSize / 10 {
|
|
|
|
continentStr := fmt.Sprintf("%d", continent)
|
|
|
|
if continent < 10 {
|
|
|
|
continentStr = fmt.Sprintf("0%d", continent)
|
|
|
|
}
|
|
|
|
drawText(img, x-16, y-3, continentStr)
|
|
|
|
continent++
|
|
|
|
}
|
2020-06-22 12:09:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := png.Encode(cfg.Destination, img); err != nil {
|
|
|
|
return errors.Wrap(err, "map-generator")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|