feat: move func to calc package
This commit is contained in:
@@ -1 +0,0 @@
|
|||||||
{"sessionId":"eb7ac833-18c4-4e5b-a2c0-a53f3599c55b","pid":31048,"procStart":"Wed May 6 22:37:00 2026","acquiredAt":1778147404017}
|
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
package controller
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"galaxy/util"
|
"galaxy/calc"
|
||||||
|
|
||||||
e "galaxy/error"
|
e "galaxy/error"
|
||||||
|
|
||||||
"galaxy/game/internal/model/game"
|
"galaxy/game/internal/model/game"
|
||||||
@@ -25,7 +24,7 @@ func (c *Cache) FleetSend(ri, fi int, planetNumber uint) error {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return e.NewEntityNotExistsError("destination planet #%d", planetNumber)
|
return e.NewEntityNotExistsError("destination planet #%d", planetNumber)
|
||||||
}
|
}
|
||||||
rangeToDestination := util.ShortDistance(c.g.Map.Width, c.g.Map.Height, p1.X.F(), p1.Y.F(), p2.X.F(), p2.Y.F())
|
rangeToDestination := calc.ShortDistance(c.g.Map.Width, c.g.Map.Height, p1.X.F(), p1.Y.F(), p2.X.F(), p2.Y.F())
|
||||||
if rangeToDestination > c.g.Race[ri].FlightDistance() {
|
if rangeToDestination > c.g.Race[ri].FlightDistance() {
|
||||||
return e.NewSendUnreachableDestinationError("range=%.03f", rangeToDestination)
|
return e.NewSendUnreachableDestinationError("range=%.03f", rangeToDestination)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,8 +9,6 @@ import (
|
|||||||
"galaxy/calc"
|
"galaxy/calc"
|
||||||
mr "galaxy/model/report"
|
mr "galaxy/model/report"
|
||||||
|
|
||||||
"galaxy/util"
|
|
||||||
|
|
||||||
"galaxy/game/internal/model/game"
|
"galaxy/game/internal/model/game"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@@ -94,7 +92,7 @@ func (c *Cache) InitReport(t uint) *mr.Report {
|
|||||||
}
|
}
|
||||||
for pi := range c.g.Map.Planet {
|
for pi := range c.g.Map.Planet {
|
||||||
p2 := &c.g.Map.Planet[pi]
|
p2 := &c.g.Map.Planet[pi]
|
||||||
distance := util.ShortDistance(c.g.Map.Width, c.g.Map.Height, sg.StateInSpace.X.F(), sg.StateInSpace.Y.F(), p2.X.F(), p2.Y.F())
|
distance := calc.ShortDistance(c.g.Map.Width, c.g.Map.Height, sg.StateInSpace.X.F(), sg.StateInSpace.Y.F(), p2.X.F(), p2.Y.F())
|
||||||
report.InSpaceGroupRangeCache[sgi][p2.Number] = distance
|
report.InSpaceGroupRangeCache[sgi][p2.Number] = distance
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -396,7 +394,7 @@ func (c *Cache) ReportIncomingGroup(ri int, rep *mr.Report) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
distance := util.ShortDistance(c.g.Map.Width, c.g.Map.Height, p1.X.F(), p1.Y.F(), p2.X.F(), p2.Y.F())
|
distance := calc.ShortDistance(c.g.Map.Width, c.g.Map.Height, p1.X.F(), p1.Y.F(), p2.X.F(), p2.Y.F())
|
||||||
var speed, mass float64
|
var speed, mass float64
|
||||||
if sg.FleetID != nil {
|
if sg.FleetID != nil {
|
||||||
speed, mass = c.FleetSpeedAndMass(c.MustFleetIndex(*sg.FleetID))
|
speed, mass = c.FleetSpeedAndMass(c.MustFleetIndex(*sg.FleetID))
|
||||||
@@ -597,7 +595,7 @@ func (c *Cache) ReportLocalFleet(ri int, rep *mr.Report) {
|
|||||||
if inSpace, ok := fleetState.InSpace(); ok {
|
if inSpace, ok := fleetState.InSpace(); ok {
|
||||||
rep.LocalFleet[i].Origin = &inSpace.Origin
|
rep.LocalFleet[i].Origin = &inSpace.Origin
|
||||||
p2 := c.MustPlanet(rep.LocalFleet[i].Destination)
|
p2 := c.MustPlanet(rep.LocalFleet[i].Destination)
|
||||||
rangeToDestination := mr.F(util.ShortDistance(c.g.Map.Width, c.g.Map.Height, inSpace.X.F(), inSpace.Y.F(), p2.X.F(), p2.Y.F()))
|
rangeToDestination := mr.F(calc.ShortDistance(c.g.Map.Width, c.g.Map.Height, inSpace.X.F(), inSpace.Y.F(), p2.X.F(), p2.Y.F()))
|
||||||
rep.LocalFleet[i].Range = &rangeToDestination
|
rep.LocalFleet[i].Range = &rangeToDestination
|
||||||
}
|
}
|
||||||
i++
|
i++
|
||||||
@@ -726,7 +724,7 @@ func (c *Cache) otherGroup(v *mr.OtherGroup, sg *game.ShipGroup, st *game.ShipTy
|
|||||||
if sg.State() == game.StateInSpace {
|
if sg.State() == game.StateInSpace {
|
||||||
v.Origin = &sg.StateInSpace.Origin
|
v.Origin = &sg.StateInSpace.Origin
|
||||||
p2 := c.MustPlanet(v.Destination)
|
p2 := c.MustPlanet(v.Destination)
|
||||||
rangeToDestination := mr.F(util.ShortDistance(c.g.Map.Width, c.g.Map.Height, sg.StateInSpace.X.F(), sg.StateInSpace.Y.F(), p2.X.F(), p2.Y.F()))
|
rangeToDestination := mr.F(calc.ShortDistance(c.g.Map.Width, c.g.Map.Height, sg.StateInSpace.X.F(), sg.StateInSpace.Y.F(), p2.X.F(), p2.Y.F()))
|
||||||
v.Range = &rangeToDestination
|
v.Range = &rangeToDestination
|
||||||
}
|
}
|
||||||
v.Speed = mr.F(sg.Speed(st))
|
v.Speed = mr.F(sg.Speed(st))
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"math/rand/v2"
|
"math/rand/v2"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
||||||
"galaxy/util"
|
"galaxy/calc"
|
||||||
|
|
||||||
e "galaxy/error"
|
e "galaxy/error"
|
||||||
|
|
||||||
@@ -28,7 +28,7 @@ func (c *Cache) PlanetRouteSet(ri int, rt game.RouteType, origin, destination ui
|
|||||||
if !ok {
|
if !ok {
|
||||||
return e.NewEntityNotExistsError("destination planet #%d", destination)
|
return e.NewEntityNotExistsError("destination planet #%d", destination)
|
||||||
}
|
}
|
||||||
rangeToDestination := util.ShortDistance(c.g.Map.Width, c.g.Map.Height, p1.X.F(), p1.Y.F(), p2.X.F(), p2.Y.F())
|
rangeToDestination := calc.ShortDistance(c.g.Map.Width, c.g.Map.Height, p1.X.F(), p1.Y.F(), p2.X.F(), p2.Y.F())
|
||||||
if rangeToDestination > c.g.Race[ri].FlightDistance() {
|
if rangeToDestination > c.g.Race[ri].FlightDistance() {
|
||||||
return e.NewSendUnreachableDestinationError("range=%.03f max=%.03f", rangeToDestination, c.g.Race[ri].FlightDistance())
|
return e.NewSendUnreachableDestinationError("range=%.03f max=%.03f", rangeToDestination, c.g.Race[ri].FlightDistance())
|
||||||
}
|
}
|
||||||
@@ -194,7 +194,7 @@ func (c *Cache) RemoveUnreachableRoutes() {
|
|||||||
ri := c.RaceIndex(*p1.Owner)
|
ri := c.RaceIndex(*p1.Owner)
|
||||||
for rt, destination := range p1.Route {
|
for rt, destination := range p1.Route {
|
||||||
p2 := c.MustPlanet(destination)
|
p2 := c.MustPlanet(destination)
|
||||||
rangeToDestination := util.ShortDistance(c.g.Map.Width, c.g.Map.Height, p1.X.F(), p1.Y.F(), p2.X.F(), p2.Y.F())
|
rangeToDestination := calc.ShortDistance(c.g.Map.Width, c.g.Map.Height, p1.X.F(), p1.Y.F(), p2.X.F(), p2.Y.F())
|
||||||
if rangeToDestination > c.g.Race[ri].FlightDistance() {
|
if rangeToDestination > c.g.Race[ri].FlightDistance() {
|
||||||
delete(p1.Route, rt)
|
delete(p1.Route, rt)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
package controller
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"galaxy/util"
|
"galaxy/calc"
|
||||||
|
|
||||||
e "galaxy/error"
|
e "galaxy/error"
|
||||||
|
|
||||||
"galaxy/game/internal/model/game"
|
"galaxy/game/internal/model/game"
|
||||||
@@ -47,7 +46,7 @@ func (c *Cache) shipGroupSend(ri int, groupID uuid.UUID, planetNumber uint) erro
|
|||||||
if !ok {
|
if !ok {
|
||||||
return e.NewEntityNotExistsError("destination planet #%d", planetNumber)
|
return e.NewEntityNotExistsError("destination planet #%d", planetNumber)
|
||||||
}
|
}
|
||||||
rangeToDestination := util.ShortDistance(c.g.Map.Width, c.g.Map.Height, p1.X.F(), p1.Y.F(), p2.X.F(), p2.Y.F())
|
rangeToDestination := calc.ShortDistance(c.g.Map.Width, c.g.Map.Height, p1.X.F(), p1.Y.F(), p2.X.F(), p2.Y.F())
|
||||||
if rangeToDestination > c.g.Race[ri].FlightDistance() {
|
if rangeToDestination > c.g.Race[ri].FlightDistance() {
|
||||||
return e.NewSendUnreachableDestinationError("range=%.03f", rangeToDestination)
|
return e.NewSendUnreachableDestinationError("range=%.03f", rangeToDestination)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
|
||||||
"galaxy/util"
|
"galaxy/calc"
|
||||||
|
|
||||||
"galaxy/game/internal/generator/plotter"
|
"galaxy/game/internal/generator/plotter"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -59,7 +58,7 @@ func (m Map) NewCoordinate(deadZoneRaduis float64) (Coordinate, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m Map) ShortDistance(from, to Coordinate) float64 {
|
func (m Map) ShortDistance(from, to Coordinate) float64 {
|
||||||
return util.ShortDistance(m.Width, m.Height, from.X, from.Y, to.X, to.Y)
|
return calc.ShortDistance(m.Width, m.Height, from.X, from.Y, to.X, to.Y)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RandI returns a random float64 value between min and max
|
// RandI returns a random float64 value between min and max
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package calc
|
||||||
|
|
||||||
|
import "math"
|
||||||
|
|
||||||
|
// shortest distance between points on torus map
|
||||||
|
func ShortDistance(w, h uint32, x1, y1, x2, y2 float64) float64 {
|
||||||
|
return math.Hypot(Deltas(w, h, x1, y1, x2, y2))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Deltas(w, h uint32, x1, y1, x2, y2 float64) (float64, float64) {
|
||||||
|
dx := math.Abs(x2 - x1)
|
||||||
|
dy := math.Abs(y2 - y1)
|
||||||
|
if dx > float64(w/2) {
|
||||||
|
dx = float64(h) - dx
|
||||||
|
}
|
||||||
|
if dy > float64(h/2) {
|
||||||
|
dy = float64(h) - dy
|
||||||
|
}
|
||||||
|
return dx, dy
|
||||||
|
}
|
||||||
+5
-18
@@ -1,6 +1,9 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import "math"
|
import (
|
||||||
|
"galaxy/calc"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
// WrapF maps value into the half-open interval [0, float64(size)).
|
// WrapF maps value into the half-open interval [0, float64(size)).
|
||||||
// It supports negative input values and is used for torus coordinates
|
// It supports negative input values and is used for torus coordinates
|
||||||
@@ -28,12 +31,8 @@ func WrapF(value float64, size int) float64 {
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func ShortDistance(w, h uint32, x1, y1, x2, y2 float64) float64 {
|
|
||||||
return math.Hypot(deltas(w, h, x1, y1, x2, y2))
|
|
||||||
}
|
|
||||||
|
|
||||||
func NextTravelCoord(w, h uint32, x1, y1, x2, y2, delta float64) (float64, float64, bool) {
|
func NextTravelCoord(w, h uint32, x1, y1, x2, y2, delta float64) (float64, float64, bool) {
|
||||||
deltaX, deltaY := deltas(w, h, x1, y1, x2, y2)
|
deltaX, deltaY := calc.Deltas(w, h, x1, y1, x2, y2)
|
||||||
distance := math.Hypot(deltaX, deltaY)
|
distance := math.Hypot(deltaX, deltaY)
|
||||||
if distance <= delta {
|
if distance <= delta {
|
||||||
return x2, y2, true
|
return x2, y2, true
|
||||||
@@ -93,15 +92,3 @@ func NextTravelCoord(w, h uint32, x1, y1, x2, y2, delta float64) (float64, float
|
|||||||
|
|
||||||
return tx, ty, false
|
return tx, ty, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func deltas(w, h uint32, x1, y1, x2, y2 float64) (float64, float64) {
|
|
||||||
dx := math.Abs(x2 - x1)
|
|
||||||
dy := math.Abs(y2 - y1)
|
|
||||||
if dx > float64(w/2) {
|
|
||||||
dx = float64(h) - dx
|
|
||||||
}
|
|
||||||
if dy > float64(h/2) {
|
|
||||||
dy = float64(h) - dy
|
|
||||||
}
|
|
||||||
return dx, dy
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"galaxy/calc"
|
||||||
"galaxy/util"
|
"galaxy/util"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@@ -22,7 +23,7 @@ func TestShortDistance(t *testing.T) {
|
|||||||
{10, 10, 8, 7, 1, 7, 3.},
|
{10, 10, 8, 7, 1, 7, 3.},
|
||||||
} {
|
} {
|
||||||
t.Run(fmt.Sprint(i), func(t *testing.T) {
|
t.Run(fmt.Sprint(i), func(t *testing.T) {
|
||||||
d := util.ShortDistance(tc.w, tc.h, tc.x1, tc.y1, tc.x2, tc.y2)
|
d := calc.ShortDistance(tc.w, tc.h, tc.x1, tc.y1, tc.x2, tc.y2)
|
||||||
assert.Equal(t, tc.d, util.Fixed3(d))
|
assert.Equal(t, tc.d, util.Fixed3(d))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user