refactor: generator

This commit is contained in:
Ilia Denisov
2025-09-11 23:26:32 +03:00
parent 371453fac5
commit d3b00b5c8d
9 changed files with 241 additions and 201 deletions
+2 -2
View File
@@ -84,9 +84,9 @@ func (p bitmap) SetFreeN(number int) error {
return fmt.Errorf("set free pixel: no such number=%d, max=%d", number, n) return fmt.Errorf("set free pixel: no such number=%d, max=%d", number, n)
} }
func (p bitmap) Circle(x, y int, r float64, fill bool) { func (p bitmap) Circle(x, y int, r float32, fill bool) {
plotX := 0 plotX := 0
plotY := int(math.Ceil(r)) plotY := int(math.Ceil(float64(r)))
delta := 3 - 2*plotY delta := 3 - 2*plotY
lastY := plotY lastY := plotY
for plotX <= plotY { for plotX <= plotY {
+1 -1
View File
@@ -221,7 +221,7 @@ func TestClear(t *testing.T) {
func TestCircle(t *testing.T) { func TestCircle(t *testing.T) {
type testCase struct { type testCase struct {
x, y int x, y int
r float64 r float32
filled bool filled bool
} }
for i, tc := range []testCase{ for i, tc := range []testCase{
+40 -113
View File
@@ -4,146 +4,73 @@ import (
"fmt" "fmt"
"math" "math"
"math/rand" "math/rand"
"github.com/iliadenisov/galaxy/pkg/bitmap"
) )
type Map struct { func Generate(ms MapSetting) (Map, error) {
Width uint pl := func(c Coordinate, ps PlanetSetting) Planet {
Height uint
HomePlanets []PlanetarySystem
FreePlanets []Planet
plotter Plotter
}
type Coordinate struct {
X, Y float64
}
type Planet struct {
Position Coordinate
Size float64
Resources float64 // Сырьё
}
type PlanetarySystem struct {
HW Planet
DW []Planet
}
type Plotter struct {
factor float64
clearFn func()
circleFn func(x, y int, r float64)
freeCountFn func() int
freeNumberToCoordFn func(int) (int, int)
}
func Generate(param MapParameter) (result Map) {
pl := func(c Coordinate, param UninhabitedPlanetParameters) Planet {
return Planet{ return Planet{
Position: c, Position: c,
Size: param.MinSize + rand.Float64()*(param.MaxSize-param.MinSize), Size: ps.MinSize + rand.Float32()*(ps.MaxSize-ps.MinSize),
Resources: float64(param.MinResource) + rand.Float64()*(param.MaxResource-param.MinResource)} Resources: float32(ps.MinResource) + rand.Float32()*(ps.MaxResource-ps.MinResource)}
} }
// mapSize := uint(math.Ceil(math.Sqrt(float64(param.Players)))) * param.HW_MinDistance // mapSize := uint(math.Ceil(math.Sqrt(float64(param.Players)))) * param.HW_MinDistance
var mapSize uint = 200 var mapSize uint = 200
result = NewMap(mapSize, mapSize, param.Players) result, err := NewMap(mapSize, mapSize, ms.Players)
if err != nil {
return Map{}, fmt.Errorf("NewMap: %s", err)
}
totalPlanets := param.Players * 10 totalPlanets := ms.Players * 10
freePlanets := totalPlanets - param.Players*(param.DW_Count+1) freePlanets := totalPlanets - ms.Players*(ms.DWCount+1)
fmt.Println("map:", mapSize, "players:", param.Players, "planets:", totalPlanets, "uninhabited:", freePlanets) fmt.Println("map:", mapSize, "players:", ms.Players, "planets:", totalPlanets, "uninhabited:", freePlanets)
giantsNum := int(math.Ceil(float64(freePlanets) * param.GiantPlanets.Probability)) giantsNum := int(math.Ceil(float64(freePlanets) * float64(ms.GiantPlanets.Probability)))
fmt.Println("generating", giantsNum, "giant planets") fmt.Println("generating", giantsNum, "giant planets")
for i := 0; i < giantsNum; i++ { for range giantsNum {
coord := result.newPlanet(float64(param.GiantPlanets.MinDistanceHW)) coord, err := result.NewCoordinate(float32(ms.GiantPlanets.MinDistanceHW))
planet := pl(coord, param.GiantPlanets) if err != nil {
result.addPlanet(planet) return Map{}, err
// result.FreePlanets = append(result.FreePlanets, planet) }
planet := pl(coord, ms.GiantPlanets)
result.AddPlanet(planet)
} }
bigsNum := int(math.Ceil(float64(freePlanets) * param.BigPlanets.Probability)) bigsNum := int(math.Ceil(float64(freePlanets) * float64(ms.BigPlanets.Probability)))
fmt.Println("generating", bigsNum, "big planets") fmt.Println("generating", bigsNum, "big planets")
for i := 0; i < bigsNum; i++ { for range bigsNum {
coord := result.newPlanet(float64(param.BigPlanets.MinDistanceHW)) coord, err := result.NewCoordinate(float32(ms.BigPlanets.MinDistanceHW))
planet := pl(coord, param.BigPlanets) if err != nil {
result.addPlanet(planet) return Map{}, err
}
planet := pl(coord, ms.BigPlanets)
result.AddPlanet(planet)
} }
for player := 0; player < int(param.Players); player++ { for player := 0; player < int(ms.Players); player++ {
fmt.Println("generating HW #", player) fmt.Println("generating HW #", player)
coord := result.newPlanet(float64(param.HW_MinDistance)) coord, err := result.NewCoordinate(float32(ms.HWMinDistance))
planet := Planet{Position: coord, Size: float64(param.HW_Size), Resources: float64(param.HW_Resources)} 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.HomePlanets[player] = PlanetarySystem{HW: planet}
} }
result.plotter.clearFn() result.plotter.clearFn()
for _, hw := range result.HomePlanets { for i := range result.HomePlanets {
result.plotter.markNoGoZone(hw.HW.Position.X, hw.HW.Position.Y, 5) result.plotter.MarkDeadZone(result.HomePlanets[i].HW.Position.X, result.HomePlanets[i].HW.Position.Y, 5)
for _, dw := range hw.DW { for j := range result.HomePlanets[i].DW {
result.plotter.markNoGoZone(dw.Position.X, dw.Position.Y, 5) result.plotter.MarkDeadZone(result.HomePlanets[i].DW[j].Position.X, result.HomePlanets[i].DW[j].Position.Y, 5)
} }
} }
for _, planet := range result.FreePlanets { for i := range result.FreePlanets {
result.plotter.markNoGoZone(planet.Position.X, planet.Position.Y, 5) result.plotter.MarkDeadZone(result.FreePlanets[i].Position.X, result.FreePlanets[i].Position.Y, 5)
} }
return return *result, nil
}
func (m Map) newPlanet(deadZoneRaduis float64) Coordinate {
fsCount := m.plotter.freeCountFn()
if fsCount == 0 {
panic("no more space for planets")
}
next := rand.Intn(fsCount)
x, y := m.plotter.freeNumberToCoordFn(next)
fmt.Println("planet on plot: x =", x, "y =", y)
planetX := float64(x)*m.plotter.factor + rand.Float64()*m.plotter.factor
planetY := float64(y)*m.plotter.factor + rand.Float64()*m.plotter.factor
m.plotter.markDeadZone(int(x), int(y), deadZoneRaduis)
return Coordinate{X: planetX, Y: planetY}
}
func (m *Map) addPlanet(planet Planet) {
m.FreePlanets = append(m.FreePlanets, planet)
}
func (p Plotter) markNoGoZone(x, y float64, radius float64) { // TODO: test
p.markDeadZone(int(x/p.factor), int(y/p.factor), radius)
}
func (p Plotter) markDeadZone(x, y int, radius float64) {
p.circleFn(x, y, radius)
}
func NewMap(width, height, players uint) Map {
var factor float64 = 1.0
sectorsX := uint32(float64(width) / factor)
sectorsY := uint32(float64(height) / factor)
bm := bitmap.NewBitmap(sectorsX, sectorsY)
return Map{
Width: width,
Height: height,
HomePlanets: make([]PlanetarySystem, players),
plotter: Plotter{
factor: factor,
clearFn: bm.Clear,
circleFn: func(x, y int, r float64) { bm.Circle(x, y, r, true) },
freeCountFn: bm.FreeCount,
freeNumberToCoordFn: func(n int) (x int, y int) {
x, y, err := bm.GetFreeN(n)
if err != nil {
panic(err)
}
return
},
},
}
} }
+1 -1
View File
@@ -7,5 +7,5 @@ import (
) )
func Test_Generator(t *testing.T) { func Test_Generator(t *testing.T) {
generator.Generate(generator.DefaultMapParameters()) generator.Generate(generator.DefaultMapSetting())
} }
+53
View File
@@ -0,0 +1,53 @@
package generator
import (
"fmt"
)
type Map struct {
Width uint
Height uint
HomePlanets []PlanetarySystem
FreePlanets []Planet
plotter Plotter
}
type Coordinate struct {
X, Y float32
}
type Planet struct {
Position Coordinate
Size float32
Resources float32 // Сырьё
}
type PlanetarySystem struct {
HW Planet
DW []Planet
}
func NewMap(width, height, players uint) (*Map, error) {
p, err := NewPlotter(width, height, 1.0)
if err != nil {
return nil, fmt.Errorf("NewPlotter: %s", err)
}
return &Map{
Width: width,
Height: height,
HomePlanets: make([]PlanetarySystem, players),
plotter: p,
}, nil
}
func (m *Map) AddPlanet(planet Planet) {
m.FreePlanets = append(m.FreePlanets, planet)
}
func (m Map) NewCoordinate(deadZoneRaduis float32) (Coordinate, error) {
if x, y, err := m.plotter.RandomFreePoint(deadZoneRaduis); err != nil {
return Coordinate{}, fmt.Errorf("NewCoordinate: RandomFreePoint: %s", err)
} else {
return Coordinate{X: x, Y: y}, nil
}
}
-82
View File
@@ -1,82 +0,0 @@
package generator
type MapParameter struct {
Players uint
HW_Size uint
HW_Resources uint
HW_MinDistance uint
DW_Count uint
DW_Size uint
DW_Resources uint
DW_MinDistance uint
DW_MaxDistance uint
GiantPlanets UninhabitedPlanetParameters
BigPlanets UninhabitedPlanetParameters
NormalPlanets UninhabitedPlanetParameters
RichPlanets UninhabitedPlanetParameters
Asterioids UninhabitedPlanetParameters
}
type UninhabitedPlanetParameters struct {
MinDistanceHW uint
MinSize float64
MaxSize float64
MinResource float64
MaxResource float64
Probability float64
}
func DefaultMapParameters() MapParameter {
return MapParameter{
Players: 25,
HW_Size: 1000,
HW_Resources: 10,
HW_MinDistance: 30,
DW_Count: 2,
DW_Size: 500,
DW_Resources: 10,
DW_MinDistance: 5,
DW_MaxDistance: 15,
GiantPlanets: UninhabitedPlanetParameters{
MinDistanceHW: 20,
MinSize: 1500,
MaxSize: 2500,
MinResource: 0,
MaxResource: 3,
Probability: 0.06,
},
BigPlanets: UninhabitedPlanetParameters{
MinDistanceHW: 10,
MinSize: 1000,
MaxSize: 2000,
MinResource: 1,
MaxResource: 10,
Probability: 0.18,
},
NormalPlanets: UninhabitedPlanetParameters{
MinDistanceHW: 0,
MinSize: 0.001,
MaxSize: 1000,
MinResource: 0,
MaxResource: 10,
Probability: 0.5,
},
RichPlanets: UninhabitedPlanetParameters{
MinDistanceHW: 0,
MinSize: 0.001,
MaxSize: 500,
MinResource: 5,
MaxResource: 25,
Probability: 0.18,
},
Asterioids: UninhabitedPlanetParameters{
MinDistanceHW: 0,
MinSize: 0.001,
MaxSize: 10,
MinResource: 0,
MaxResource: 0,
Probability: 0.08,
},
}
}
+60
View File
@@ -0,0 +1,60 @@
package generator
import (
"errors"
"fmt"
"math/rand"
"github.com/iliadenisov/galaxy/pkg/bitmap"
)
type Plotter struct {
factor float32
clearFn func()
circleFn func(x, y int, r float32)
freeCountFn func() int
freeNumberToCoordFn func(int) (int, int)
}
func NewPlotter(width, height uint, factor float32) (Plotter, error) {
if factor > 1 || factor <= 0 {
return Plotter{}, fmt.Errorf("factor should be: 0 > F <= 1")
}
sectorsX := uint32(float32(width) / factor)
sectorsY := uint32(float32(height) / factor)
bm := bitmap.NewBitmap(sectorsX, sectorsY)
return Plotter{
factor: factor,
clearFn: bm.Clear,
circleFn: func(x, y int, r float32) { bm.Circle(x, y, r, true) },
freeCountFn: bm.FreeCount,
freeNumberToCoordFn: func(n int) (x int, y int) {
x, y, err := bm.GetFreeN(n)
if err != nil {
panic(err)
}
return
},
}, nil
}
func (p Plotter) RandomFreePoint(deadZoneRaduis float32) (float32, float32, error) {
fsCount := p.freeCountFn()
if fsCount == 0 {
return 0, 0, errors.New("no more space for planets")
}
next := rand.Intn(fsCount)
x, y := p.freeNumberToCoordFn(next)
p.PlotDeadZone(x, y, deadZoneRaduis)
planetX := float32(x)*p.factor + rand.Float32()*p.factor // TODO: correct shift?
planetY := float32(y)*p.factor + rand.Float32()*p.factor
return planetX, planetY, nil
}
func (p Plotter) MarkDeadZone(x, y float32, radius float32) {
p.PlotDeadZone(int(x/p.factor), int(y/p.factor), radius)
}
func (p Plotter) PlotDeadZone(x, y int, radius float32) {
p.circleFn(x, y, radius/p.factor)
}
+82
View File
@@ -0,0 +1,82 @@
package generator
type MapSetting struct {
Players uint
HWSize uint
HWResources uint
HWMinDistance uint
DWCount uint
DWSize uint
DWResources uint
DWMinDistance uint
DWMaxDistance uint
GiantPlanets PlanetSetting
BigPlanets PlanetSetting
NormalPlanets PlanetSetting
RichPlanets PlanetSetting
Asterioids PlanetSetting
}
type PlanetSetting struct {
MinDistanceHW uint
MinSize float32
MaxSize float32
MinResource float32
MaxResource float32
Probability float32
}
func DefaultMapSetting() MapSetting {
return MapSetting{
Players: 25,
HWSize: 1000,
HWResources: 10,
HWMinDistance: 30,
DWCount: 2,
DWSize: 500,
DWResources: 10,
DWMinDistance: 5,
DWMaxDistance: 15,
GiantPlanets: PlanetSetting{
MinDistanceHW: 20,
MinSize: 1500,
MaxSize: 2500,
MinResource: 0,
MaxResource: 3,
Probability: 0.06,
},
BigPlanets: PlanetSetting{
MinDistanceHW: 10,
MinSize: 1000,
MaxSize: 2000,
MinResource: 1,
MaxResource: 10,
Probability: 0.18,
},
NormalPlanets: PlanetSetting{
MinDistanceHW: 0,
MinSize: 0.001,
MaxSize: 1000,
MinResource: 0,
MaxResource: 10,
Probability: 0.5,
},
RichPlanets: PlanetSetting{
MinDistanceHW: 0,
MinSize: 0.001,
MaxSize: 500,
MinResource: 5,
MaxResource: 25,
Probability: 0.18,
},
Asterioids: PlanetSetting{
MinDistanceHW: 0,
MinSize: 0.001,
MaxSize: 10,
MinResource: 0,
MaxResource: 0,
Probability: 0.08,
},
}
}
+2 -2
View File
@@ -16,7 +16,7 @@ func New(storage storage.Storage) Server {
return Server{storage: storage} return Server{storage: storage}
} }
func (s Server) CreateGame(gameParam game.GameParameter, mapParam generator.MapParameter) (game.Game, error) { func (s Server) CreateGame(gameParam game.GameParameter, mapParam generator.MapSetting) (game.Game, error) {
_ = generator.Generate(mapParam) _, _ = generator.Generate(mapParam)
return game.Game{}, errors.New("not yet implemented") return game.Game{}, errors.New("not yet implemented")
} }