feat: move func to calc package

This commit is contained in:
Ilia Denisov
2026-05-10 14:55:14 +02:00
parent 92413575f3
commit 408097e3aa
11 changed files with 21214 additions and 38 deletions
-1
View File
@@ -1 +0,0 @@
{"sessionId":"eb7ac833-18c4-4e5b-a2c0-a53f3599c55b","pid":31048,"procStart":"Wed May 6 22:37:00 2026","acquiredAt":1778147404017}
+2 -3
View File
@@ -1,8 +1,7 @@
package controller
import (
"galaxy/util"
"galaxy/calc"
e "galaxy/error"
"galaxy/game/internal/model/game"
@@ -25,7 +24,7 @@ func (c *Cache) FleetSend(ri, fi int, planetNumber uint) error {
if !ok {
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() {
return e.NewSendUnreachableDestinationError("range=%.03f", rangeToDestination)
}
+4 -6
View File
@@ -9,8 +9,6 @@ import (
"galaxy/calc"
mr "galaxy/model/report"
"galaxy/util"
"galaxy/game/internal/model/game"
"github.com/google/uuid"
@@ -94,7 +92,7 @@ func (c *Cache) InitReport(t uint) *mr.Report {
}
for pi := range c.g.Map.Planet {
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
}
} else {
@@ -396,7 +394,7 @@ func (c *Cache) ReportIncomingGroup(ri int, rep *mr.Report) {
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
if sg.FleetID != nil {
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 {
rep.LocalFleet[i].Origin = &inSpace.Origin
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
}
i++
@@ -726,7 +724,7 @@ func (c *Cache) otherGroup(v *mr.OtherGroup, sg *game.ShipGroup, st *game.ShipTy
if sg.State() == game.StateInSpace {
v.Origin = &sg.StateInSpace.Origin
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.Speed = mr.F(sg.Speed(st))
+3 -3
View File
@@ -8,7 +8,7 @@ import (
"math/rand/v2"
"slices"
"galaxy/util"
"galaxy/calc"
e "galaxy/error"
@@ -28,7 +28,7 @@ func (c *Cache) PlanetRouteSet(ri int, rt game.RouteType, origin, destination ui
if !ok {
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() {
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)
for rt, destination := range p1.Route {
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() {
delete(p1.Route, rt)
}
+2 -3
View File
@@ -1,8 +1,7 @@
package controller
import (
"galaxy/util"
"galaxy/calc"
e "galaxy/error"
"galaxy/game/internal/model/game"
@@ -47,7 +46,7 @@ func (c *Cache) shipGroupSend(ri int, groupID uuid.UUID, planetNumber uint) erro
if !ok {
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() {
return e.NewSendUnreachableDestinationError("range=%.03f", rangeToDestination)
}
+2 -3
View File
@@ -4,8 +4,7 @@ import (
"fmt"
"math/rand"
"galaxy/util"
"galaxy/calc"
"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 {
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
+20
View File
@@ -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
View File
@@ -1,6 +1,9 @@
package util
import "math"
import (
"galaxy/calc"
"math"
)
// WrapF maps value into the half-open interval [0, float64(size)).
// It supports negative input values and is used for torus coordinates
@@ -28,12 +31,8 @@ func WrapF(value float64, size int) float64 {
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) {
deltaX, deltaY := deltas(w, h, x1, y1, x2, y2)
deltaX, deltaY := calc.Deltas(w, h, x1, y1, x2, y2)
distance := math.Hypot(deltaX, deltaY)
if distance <= delta {
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
}
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
}
+2 -1
View File
@@ -5,6 +5,7 @@ import (
"math"
"testing"
"galaxy/calc"
"galaxy/util"
"github.com/stretchr/testify/assert"
@@ -22,7 +23,7 @@ func TestShortDistance(t *testing.T) {
{10, 10, 8, 7, 1, 7, 3.},
} {
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))
})
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff