test dw distances
This commit is contained in:
@@ -37,18 +37,18 @@ func Generate(cfg ...func(*MapSetting)) (Map, error) {
|
||||
|
||||
// 3. Place players' Home Worlds
|
||||
for player := 0; player < int(ms.Players); player++ {
|
||||
coord, err := m.NewCoordinate(float64(ms.HWMinDistance))
|
||||
hwCoord, err := m.NewCoordinate(float64(ms.HWMinDistance))
|
||||
if err != nil {
|
||||
return Map{}, fmt.Errorf("%s: hw new_coordinate: %s", ms, err)
|
||||
}
|
||||
planet := NewPlanet(coord, float64(ms.HWSize), float64(ms.HWResources))
|
||||
m.HomePlanets[player] = PlanetarySystem{HW: planet, DW: make([]Planet, ms.DWCount)}
|
||||
hwPlanet := NewPlanet(hwCoord, float64(ms.HWSize), float64(ms.HWResources))
|
||||
m.HomePlanets[player] = PlanetarySystem{HW: hwPlanet, DW: make([]Planet, ms.DWCount)}
|
||||
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(Coordinate{x, y}, float64(ms.DWSize), float64(ms.DWResources))
|
||||
dwPlanet := NewPlanet(Coordinate{hwCoord.X + x, hwCoord.Y + y}, float64(ms.DWSize), float64(ms.DWResources))
|
||||
m.HomePlanets[player].DW[dw] = dwPlanet
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,13 @@ func TestGenerator(t *testing.T) {
|
||||
assert.Equal(t, players, len(m.HomePlanets), "hw count")
|
||||
for i := range m.HomePlanets {
|
||||
assert.Equal(t, int(s.DWCount), len(m.HomePlanets[i].DW), "hw #%d: dw count", i)
|
||||
for dw := range m.HomePlanets[i].DW {
|
||||
d := m.ShortDistance(m.HomePlanets[i].HW.Position, m.HomePlanets[i].DW[dw].Position)
|
||||
assert.LessOrEqualf(t, float64(s.DWMinDistance), d, "HW[%.04f,%04f] - DW[%.04f,%04f]",
|
||||
m.HomePlanets[i].HW.Position.X, m.HomePlanets[i].HW.Position.Y, m.HomePlanets[i].DW[dw].Position.X, m.HomePlanets[i].DW[dw].Position.Y)
|
||||
assert.GreaterOrEqualf(t, float64(s.DWMaxDistance), d, "HW[%.04f,%04f] - DW[%.04f,%04f]",
|
||||
m.HomePlanets[i].HW.Position.X, m.HomePlanets[i].HW.Position.Y, m.HomePlanets[i].DW[dw].Position.X, m.HomePlanets[i].DW[dw].Position.Y)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package generator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"math/rand"
|
||||
|
||||
"github.com/iliadenisov/galaxy/pkg/generator/plotter"
|
||||
@@ -67,6 +68,18 @@ func (m Map) NewCoordinate(deadZoneRaduis float64) (Coordinate, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (m Map) ShortDistance(from, to Coordinate) float64 {
|
||||
dx := math.Abs(to.X - from.X)
|
||||
dy := math.Abs(to.Y - from.Y)
|
||||
if dx > float64(m.Width/2) {
|
||||
dx = float64(m.Width) - dx
|
||||
}
|
||||
if dy > float64(m.Height/2) {
|
||||
dy = float64(m.Height) - dy
|
||||
}
|
||||
return math.Sqrt(math.Pow(dx, 2) + math.Pow(dy, 2))
|
||||
}
|
||||
|
||||
func NewPlanet(c Coordinate, size, resources float64) Planet {
|
||||
return Planet{
|
||||
Position: c,
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package generator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/iliadenisov/galaxy/pkg/number"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestShortDistance(t *testing.T) {
|
||||
for i, tc := range []struct {
|
||||
w, h uint32
|
||||
x1, y1, x2, y2, d float64
|
||||
}{
|
||||
{10, 10, 0, 0, 5, 5, 7.071},
|
||||
{10, 10, 0, 0, 5.01, 5.01, 7.057},
|
||||
{10, 10, 2, 2, 8, 2, 4.},
|
||||
{10, 10, 8, 7, 1, 7, 3.},
|
||||
} {
|
||||
t.Run(fmt.Sprint(i), func(t *testing.T) {
|
||||
m := Map{Width: tc.w, Height: tc.h}
|
||||
d := m.ShortDistance(Coordinate{tc.x1, tc.y1}, Coordinate{tc.x2, tc.y2})
|
||||
assert.Equal(t, tc.d, number.Fixed3(d))
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user