refactor: plotter, generator

This commit is contained in:
Ilia Denisov
2025-09-12 21:32:50 +03:00
parent d3b00b5c8d
commit 05999687aa
11 changed files with 262 additions and 140 deletions
+42 -44
View File
@@ -3,74 +3,72 @@ package generator
import (
"fmt"
"math"
"math/rand"
)
func Generate(ms MapSetting) (Map, error) {
pl := func(c Coordinate, ps PlanetSetting) Planet {
return Planet{
Position: c,
Size: ps.MinSize + rand.Float32()*(ps.MaxSize-ps.MinSize),
Resources: float32(ps.MinResource) + rand.Float32()*(ps.MaxResource-ps.MinResource)}
func (m *Map) CreatePlanets(num int, deadZoneRadius float32, size, resources func() float32) error {
for range num {
coord, err := m.NewCoordinate(deadZoneRadius)
if err != nil {
return err
}
planet := NewPlanet(coord, size(), resources())
m.AddPlanet(planet)
}
// mapSize := uint(math.Ceil(math.Sqrt(float64(param.Players)))) * param.HW_MinDistance
var mapSize uint = 200
return nil
}
result, err := NewMap(mapSize, mapSize, ms.Players)
func Generate(cfg ...func(*MapSetting)) (Map, error) {
ms := DefaultMapSetting()
for i := range cfg {
cfg[i](&ms)
}
// TODO: pre-calculate sufficient map size
var mapSize uint32 = 200
m, err := NewMap(mapSize, mapSize, ms.Players)
if err != nil {
return Map{}, fmt.Errorf("NewMap: %s", err)
}
totalPlanets := ms.Players * 10
totalPlanets := ms.Players * 10 // TODO: why 10?
freePlanets := totalPlanets - ms.Players*(ms.DWCount+1)
fmt.Println("map:", mapSize, "players:", ms.Players, "planets:", totalPlanets, "uninhabited:", freePlanets)
// 1. Place Giant planets
giantsNum := int(math.Ceil(float64(freePlanets) * float64(ms.GiantPlanets.Probability)))
fmt.Println("generating", giantsNum, "giant planets")
for range giantsNum {
coord, err := result.NewCoordinate(float32(ms.GiantPlanets.MinDistanceHW))
if err != nil {
return Map{}, err
}
planet := pl(coord, ms.GiantPlanets)
result.AddPlanet(planet)
}
m.CreatePlanets(giantsNum, float32(ms.GiantPlanets.MinDistanceHW),
RandIFn(ms.GiantPlanets.MinSize, ms.GiantPlanets.MaxSize),
RandIFn(ms.GiantPlanets.MinResource, ms.GiantPlanets.MaxResource),
)
// 2. Place Big planets
bigsNum := int(math.Ceil(float64(freePlanets) * float64(ms.BigPlanets.Probability)))
fmt.Println("generating", bigsNum, "big planets")
for range bigsNum {
coord, err := result.NewCoordinate(float32(ms.BigPlanets.MinDistanceHW))
if err != nil {
return Map{}, err
}
planet := pl(coord, ms.BigPlanets)
result.AddPlanet(planet)
}
m.CreatePlanets(bigsNum, float32(ms.BigPlanets.MinDistanceHW),
RandIFn(ms.BigPlanets.MinSize, ms.BigPlanets.MaxSize),
RandIFn(ms.BigPlanets.MinResource, ms.BigPlanets.MaxResource),
)
// X. Place players' Home Worlds
for player := 0; player < int(ms.Players); player++ {
fmt.Println("generating HW #", player)
coord, err := result.NewCoordinate(float32(ms.HWMinDistance))
coord, err := m.NewCoordinate(float32(ms.HWMinDistance))
if err != nil {
return Map{}, err
}
planet := Planet{Position: coord, Size: float32(ms.HWSize), Resources: float32(ms.HWResources)}
result.HomePlanets[player] = PlanetarySystem{HW: planet}
planet := NewPlanet(coord, float32(ms.HWSize), float32(ms.HWResources))
m.HomePlanets[player] = PlanetarySystem{HW: planet}
}
result.plotter.clearFn()
m.plotter.Clear()
for i := range result.HomePlanets {
result.plotter.MarkDeadZone(result.HomePlanets[i].HW.Position.X, result.HomePlanets[i].HW.Position.Y, 5)
for j := range result.HomePlanets[i].DW {
result.plotter.MarkDeadZone(result.HomePlanets[i].DW[j].Position.X, result.HomePlanets[i].DW[j].Position.Y, 5)
for i := range m.HomePlanets {
m.plotter.MarkDeadZone(m.HomePlanets[i].HW.Position.X, m.HomePlanets[i].HW.Position.Y, 5)
for j := range m.HomePlanets[i].DW {
m.plotter.MarkDeadZone(m.HomePlanets[i].DW[j].Position.X, m.HomePlanets[i].DW[j].Position.Y, 5)
}
}
for i := range result.FreePlanets {
result.plotter.MarkDeadZone(result.FreePlanets[i].Position.X, result.FreePlanets[i].Position.Y, 5)
for i := range m.FreePlanets {
m.plotter.MarkDeadZone(m.FreePlanets[i].Position.X, m.FreePlanets[i].Position.Y, 5)
}
return *result, nil
return *m, nil
}