Files
galaxy-game/pkg/generator/generator.go
T
2025-09-11 23:26:32 +03:00

77 lines
2.3 KiB
Go

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)}
}
// mapSize := uint(math.Ceil(math.Sqrt(float64(param.Players)))) * param.HW_MinDistance
var mapSize uint = 200
result, err := NewMap(mapSize, mapSize, ms.Players)
if err != nil {
return Map{}, fmt.Errorf("NewMap: %s", err)
}
totalPlanets := ms.Players * 10
freePlanets := totalPlanets - ms.Players*(ms.DWCount+1)
fmt.Println("map:", mapSize, "players:", ms.Players, "planets:", totalPlanets, "uninhabited:", freePlanets)
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)
}
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)
}
for player := 0; player < int(ms.Players); player++ {
fmt.Println("generating HW #", player)
coord, err := result.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}
}
result.plotter.clearFn()
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 result.FreePlanets {
result.plotter.MarkDeadZone(result.FreePlanets[i].Position.X, result.FreePlanets[i].Position.Y, 5)
}
return *result, nil
}