circle radius
This commit is contained in:
@@ -2,10 +2,16 @@ package generator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"galaxy/util"
|
||||
"math"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
const (
|
||||
fullSectorWithFactor = int(360. / defaultFactor)
|
||||
deadZoneDWGrad = 15.
|
||||
)
|
||||
|
||||
func Generate(cfg ...func(*MapSetting)) (Map, error) {
|
||||
ms := DefaultMapSetting()
|
||||
for i := range cfg {
|
||||
@@ -43,12 +49,35 @@ func Generate(cfg ...func(*MapSetting)) (Map, error) {
|
||||
}
|
||||
hwPlanet := NewPlanet(PlanetClassHW, hwCoord, ms.HWSize, ms.HWResources)
|
||||
m.HomePlanets[player] = PlanetarySystem{HW: hwPlanet, DW: make([]Planet, ms.DWCount)}
|
||||
grads := make(map[uint16]bool, fullSectorWithFactor)
|
||||
for i := range fullSectorWithFactor {
|
||||
grads[uint16(i)] = true
|
||||
}
|
||||
for dw := 0; dw < int(ms.DWCount); dw++ {
|
||||
p := rand.Float64()*(float64(ms.DWMaxDistance)-float64(ms.DWMinDistance)) + float64(ms.DWMinDistance)
|
||||
phi := rand.Float64() * 360
|
||||
x := p * math.Cos(phi)
|
||||
y := p * math.Sin(phi)
|
||||
dwPlanet := NewPlanet(PlanetClassDW, Coordinate{hwCoord.X + x, hwCoord.Y + y}, ms.DWSize, ms.DWResources)
|
||||
free := make([]uint16, 0)
|
||||
for g := range grads {
|
||||
if v, ok := grads[g]; ok && v {
|
||||
free = append(free, g)
|
||||
}
|
||||
}
|
||||
randGrad := free[rand.Intn(len(free))]
|
||||
phi := float64(randGrad) * defaultFactor
|
||||
for i := range int(deadZoneDWGrad / defaultFactor) {
|
||||
lx := randGrad - uint16(i)
|
||||
if uint16(i) > randGrad {
|
||||
lx = uint16(fullSectorWithFactor) - (uint16(i) - randGrad)
|
||||
}
|
||||
grads[lx] = false
|
||||
rx := randGrad + uint16(i)
|
||||
if rx > uint16(fullSectorWithFactor) {
|
||||
rx = rx - uint16(fullSectorWithFactor)
|
||||
}
|
||||
grads[rx] = false
|
||||
}
|
||||
x := util.WrapF(hwCoord.X+p*math.Cos(phi), int(size))
|
||||
y := util.WrapF(hwCoord.Y+p*math.Sin(phi), int(size))
|
||||
dwPlanet := NewPlanet(PlanetClassDW, Coordinate{x, y}, ms.DWSize, ms.DWResources)
|
||||
m.HomePlanets[player].DW[dw] = dwPlanet
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,10 +24,12 @@ func TestGenerator(t *testing.T) {
|
||||
}
|
||||
assert.Equal(t, players, len(m.HomePlanets), "hw-s count")
|
||||
for hw := range m.HomePlanets {
|
||||
testPlanetPositionOnMap(t, generator.PlanetClassHW, m.Height, m.Width, m.HomePlanets[hw].HW.Position.X, m.HomePlanets[hw].HW.Position.Y)
|
||||
assert.Equal(t, s.HWSize, m.HomePlanets[hw].HW.Size, "hw #%d: size", hw)
|
||||
assert.Equal(t, s.HWResources, m.HomePlanets[hw].HW.Resources, "hw #%d: resources", hw)
|
||||
assert.Equal(t, int(s.DWCount), len(m.HomePlanets[hw].DW), "hw #%d: dw-s count", hw)
|
||||
for dw := range m.HomePlanets[hw].DW {
|
||||
testPlanetPositionOnMap(t, generator.PlanetClassDW, m.Height, m.Width, m.HomePlanets[hw].DW[dw].Position.X, m.HomePlanets[hw].DW[dw].Position.Y)
|
||||
assert.Equal(t, s.DWSize, m.HomePlanets[hw].DW[dw].Size, "hw #%d dw #%d: size", hw, dw)
|
||||
assert.Equal(t, s.DWResources, m.HomePlanets[hw].DW[dw].Resources, "hw #%d dw #%d: resources", hw, dw)
|
||||
d := m.ShortDistance(m.HomePlanets[hw].HW.Position, m.HomePlanets[hw].DW[dw].Position)
|
||||
@@ -43,7 +45,7 @@ func TestGenerator(t *testing.T) {
|
||||
freePlanetCount := make(map[generator.PlanetClass]int)
|
||||
for fp := range m.FreePlanets {
|
||||
ps := planetSettings(t, m.FreePlanets[fp].PlanetClass, s)
|
||||
testPlanetParameters(t, ps, m.FreePlanets[fp])
|
||||
testPlanetParameters(t, ps, m.Height, m.Width, m.FreePlanets[fp])
|
||||
if v, ok := freePlanetCount[m.FreePlanets[fp].PlanetClass]; !ok {
|
||||
freePlanetCount[m.FreePlanets[fp].PlanetClass] = 1
|
||||
} else {
|
||||
@@ -68,13 +70,21 @@ func TestGenerator(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func testPlanetParameters(t *testing.T, s generator.PlanetSetting, p generator.Planet) {
|
||||
func testPlanetParameters(t *testing.T, s generator.PlanetSetting, mapW, mapH uint32, p generator.Planet) {
|
||||
testPlanetPositionOnMap(t, p.PlanetClass, mapW, mapH, p.Position.X, p.Position.Y)
|
||||
assert.LessOrEqualf(t, s.MinResource, p.Resources, "planet class=%s min resources", p.PlanetClass)
|
||||
assert.GreaterOrEqualf(t, s.MaxResource, p.Resources, "planet class=%s max resources", p.PlanetClass)
|
||||
assert.LessOrEqualf(t, s.MinSize, p.Size, "planet class=%s min size", p.PlanetClass)
|
||||
assert.GreaterOrEqualf(t, s.MaxSize, p.Size, "planet class=%s max size", p.PlanetClass)
|
||||
}
|
||||
|
||||
func testPlanetPositionOnMap(t *testing.T, cls generator.PlanetClass, mapW, mapH uint32, x, y float64) {
|
||||
assert.GreaterOrEqual(t, x, 0.0, "planet class=%s x=%f < 0", cls, x)
|
||||
assert.Less(t, x, float64(mapW), "planet class=%s x=%f >= %d", cls, x, mapW)
|
||||
assert.GreaterOrEqual(t, y, 0.0, "planet class=%s y=%f < 0", cls, y)
|
||||
assert.Less(t, y, float64(mapH), "planet class=%s y=%f >= %d", cls, y, mapH)
|
||||
}
|
||||
|
||||
func planetSettings(t *testing.T, pc generator.PlanetClass, s generator.MapSetting) generator.PlanetSetting {
|
||||
switch pc {
|
||||
case generator.PlanetClassGiant:
|
||||
|
||||
Reference in New Issue
Block a user