This repository has been archived on 2022-09-04. You can view files and clone it, but cannot push or open issues or pull requests.
map-generator/generator/generator.go

105 lines
2.1 KiB
Go
Raw Normal View History

2020-06-22 12:09:39 +00:00
package generator
import (
"fmt"
2020-06-22 12:09:39 +00:00
"image"
"image/color"
"image/png"
"io"
"github.com/pkg/errors"
"github.com/tribalwarshelp/shared/models"
)
const (
defaultBackgroundColor = "#69380e"
defaultMapSize = 1000
)
2020-06-22 12:09:39 +00:00
type Marker struct {
Villages []*models.Village
Color string
Name string
2020-06-22 12:09:39 +00:00
}
type Config struct {
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 {
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})
backgroundColor, err := parseHexColorFast(cfg.BackgroundColor)
2020-06-22 12:09:39 +00:00
if err != nil {
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
if cfg.ContinentGrid {
for y := cfg.MapSize / 10; y < cfg.MapSize; y += cfg.MapSize / 10 {
for x := 0; x < cfg.MapSize; x++ {
img.Set(x, y, color.Black)
}
}
for x := cfg.MapSize / 10; x < cfg.MapSize; x += cfg.MapSize / 10 {
for y := 0; y < cfg.MapSize; y++ {
img.Set(x, y, color.Black)
}
2020-06-22 12:09:39 +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
}