refactor: group launched/in_space state

This commit is contained in:
Ilia Denisov
2026-02-08 19:29:38 +02:00
parent 077c0999d2
commit b928bb2976
14 changed files with 43 additions and 62 deletions
+1 -1
View File
@@ -68,7 +68,7 @@ func TestCollectPlanetGroups(t *testing.T) {
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 10)) // 1 #0 assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 10)) // 1 #0
assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 3)) // 2 #1 assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 3)) // 2 #1
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 3)) // 3 #2 assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 3)) // 3 #2
c.ShipGroup(2).StateInSpace = &game.InSpace{Origin: 2, Range: 1.23} // 3 #2 -> In_Space c.ShipGroup(2).StateInSpace = &InSpace // 3 #2 -> In_Space
assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 1)) // 4 #3 assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 1)) // 4 #3
c.ShipGroup(3).Destination = R1_Planet_1_num // 4 #3 -> Planet_1 c.ShipGroup(3).Destination = R1_Planet_1_num // 4 #3 -> Planet_1
assert.NoError(t, c.CreateShips(Race_1_idx, Race_1_Gunship, R1_Planet_1_num, 15)) // 5 #4 assert.NoError(t, c.CreateShips(Race_1_idx, Race_1_Gunship, R1_Planet_1_num, 15)) // 5 #4
+1 -4
View File
@@ -50,10 +50,7 @@ func TestCollectBombingGroups(t *testing.T) {
// 3: idx = 2 / In_Space // 3: idx = 2 / In_Space
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 1)) assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 1))
c.ShipGroup(2).StateInSpace = &game.InSpace{ c.ShipGroup(2).StateInSpace = &InSpace
Origin: 2,
Range: 31.337,
}
// 4: idx = 3 / Has no Ammo // 4: idx = 3 / Has no Ammo
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 1)) assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 1))
+7
View File
@@ -99,6 +99,8 @@ var (
BadEntityName = "Bad(entitty)Name" BadEntityName = "Bad(entitty)Name"
UnknownRace = "UnknownRace" UnknownRace = "UnknownRace"
InSpace = game.InSpace{Origin: 2, X: floatRef(1.23), Y: floatRef(1.23)}
) )
// [ ] Delete this fake test // [ ] Delete this fake test
@@ -153,3 +155,8 @@ func newCache() (*controller.Cache, *controller.Controller) {
return c, ctl return c, ctl
} }
func floatRef(v float64) *game.Float {
f := game.Float(v)
return &f
}
+1 -1
View File
@@ -48,7 +48,7 @@ func TestSendFleet(t *testing.T) {
assert.Len(t, slices.Collect(c.ListFleets(Race_0_idx)), 3) assert.Len(t, slices.Collect(c.ListFleets(Race_0_idx)), 3)
// group #2 - in_space // group #2 - in_space
c.ShipGroup(1).StateInSpace = &game.InSpace{Origin: 2, Range: 1.23} c.ShipGroup(1).StateInSpace = &InSpace
assert.ErrorContains(t, assert.ErrorContains(t,
g.SendFleet(UnknownRace, fleetSending, 2), g.SendFleet(UnknownRace, fleetSending, 2),
+2 -5
View File
@@ -92,17 +92,14 @@ func TestJoinShipGroupToFleet(t *testing.T) {
// group not In_Orbit // group not In_Orbit
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 7)) assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 7))
gi = 3 gi = 3
c.ShipGroup(gi).StateInSpace = &game.InSpace{ c.ShipGroup(gi).StateInSpace = &InSpace
Origin: 2,
Range: 1,
}
assert.ErrorContains(t, assert.ErrorContains(t,
g.JoinShipGroupToFleet(Race_0.Name, fleetOne, c.ShipGroup(gi).Index, 0), g.JoinShipGroupToFleet(Race_0.Name, fleetOne, c.ShipGroup(gi).Index, 0),
e.GenericErrorText(e.ErrShipsBusy)) e.GenericErrorText(e.ErrShipsBusy))
c.ShipGroup(gi).StateInSpace = nil c.ShipGroup(gi).StateInSpace = nil
// existing fleet not on the same planet or in_orbit // existing fleet not on the same planet or in_orbit
c.ShipGroup(0).StateInSpace = &game.InSpace{Origin: 2, Range: 1} c.ShipGroup(0).StateInSpace = &InSpace
c.ShipGroup(1).StateInSpace = c.ShipGroup(0).StateInSpace c.ShipGroup(1).StateInSpace = c.ShipGroup(0).StateInSpace
assert.ErrorContains(t, assert.ErrorContains(t,
g.JoinShipGroupToFleet(Race_0.Name, fleetOne, c.ShipGroup(gi).Index, 0), g.JoinShipGroupToFleet(Race_0.Name, fleetOne, c.ShipGroup(gi).Index, 0),
+2 -8
View File
@@ -109,10 +109,7 @@ func TestListRoutedSendGroupIds(t *testing.T) {
// 3: idx = 2 / In_Space // 3: idx = 2 / In_Space
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7)) assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7))
c.ShipGroup(2).StateInSpace = &game.InSpace{ c.ShipGroup(2).StateInSpace = &InSpace
Origin: 2,
Range: 31.337,
}
// 4: idx = 3 / loaded with COL // 4: idx = 3 / loaded with COL
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 11)) assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 11))
@@ -275,10 +272,7 @@ func TestListRoutedUnloadShipGroupIds(t *testing.T) {
// 3: idx = 2 / In_Space // 3: idx = 2 / In_Space
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7)) assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7))
c.ShipGroup(2).StateInSpace = &game.InSpace{ c.ShipGroup(2).StateInSpace = &InSpace
Origin: 2,
Range: 31.337,
}
// 4: idx = 3 / loaded with COL // 4: idx = 3 / loaded with COL
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 11)) assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 11))
+3 -2
View File
@@ -42,8 +42,9 @@ func (c *Cache) moveShipGroup(i int, delta float64) {
var x, y float64 var x, y float64
x, y, arrived = x, y, arrived =
util.NextTravelCoord(c.g.Map.Width, c.g.Map.Height, originX, originY, destPlanet.X.F(), destPlanet.Y.F(), delta) util.NextTravelCoord(c.g.Map.Width, c.g.Map.Height, originX, originY, destPlanet.X.F(), destPlanet.Y.F(), delta)
sg.StateInSpace.X = game.F(x) fx, fy := game.F(x), game.F(y)
sg.StateInSpace.Y = game.F(y) sg.StateInSpace.X = &fx
sg.StateInSpace.Y = &fy
if arrived { if arrived {
sg.StateInSpace = nil sg.StateInSpace = nil
} }
+1 -4
View File
@@ -24,10 +24,7 @@ func TestListMoveableGroupIds(t *testing.T) {
// 4: idx = 3 / [v] In_Space // 4: idx = 3 / [v] In_Space
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7)) assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7))
c.ShipGroup(3).StateInSpace = &game.InSpace{ c.ShipGroup(3).StateInSpace = &InSpace
Origin: 2,
Range: 31.337,
}
// 5: idx = 4 / [x] In_Upgrage // 5: idx = 4 / [x] In_Upgrage
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7)) assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7))
+2 -2
View File
@@ -97,8 +97,8 @@ func (c *Cache) UnsendShips(sg *game.ShipGroup) *game.ShipGroup {
func LaunchShips(sg game.ShipGroup, destination uint, originX, originY float64) game.ShipGroup { func LaunchShips(sg game.ShipGroup, destination uint, originX, originY float64) game.ShipGroup {
sg.StateInSpace = &game.InSpace{ sg.StateInSpace = &game.InSpace{
Origin: sg.Destination, Origin: sg.Destination,
X: game.F(originX), X: nil,
Y: game.F(originY), Y: nil,
} }
sg.Destination = destination sg.Destination = destination
return sg return sg
+5 -5
View File
@@ -17,7 +17,7 @@ func TestSendGroup(t *testing.T) {
// group #2 - in_space // group #2 - in_space
assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 1)) assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 1))
// g.ShipGroups[1].StateInSpace = &game.InSpace{Origin: 2, Range: 1.23} // g.ShipGroups[1].StateInSpace = &game.InSpace{Origin: 2, Range: 1.23}
c.ShipGroup(1).StateInSpace = &game.InSpace{Origin: 2, Range: 1.23} c.ShipGroup(1).StateInSpace = &InSpace
// group #3 - in_orbit, unmovable // group #3 - in_orbit, unmovable
g.CreateShipType(Race_0.Name, "Fortress", 0, 50, 30, 100, 0) g.CreateShipType(Race_0.Name, "Fortress", 0, 50, 30, 100, 0)
assert.NoError(t, c.CreateShips(Race_0_idx, "Fortress", R0_Planet_0_num, 1)) assert.NoError(t, c.CreateShips(Race_0_idx, "Fortress", R0_Planet_0_num, 1))
@@ -54,8 +54,8 @@ func TestSendGroup(t *testing.T) {
assert.Equal(t, uint(3), c.ShipGroup(3).Number) assert.Equal(t, uint(3), c.ShipGroup(3).Number)
assert.Equal(t, game.StateLaunched, c.ShipGroup(3).State()) assert.Equal(t, game.StateLaunched, c.ShipGroup(3).State())
assert.NotNil(t, c.ShipGroup(3).StateInSpace) assert.NotNil(t, c.ShipGroup(3).StateInSpace)
assert.Equal(t, c.MustPlanet(R0_Planet_0_num).X, c.ShipGroup(3).StateInSpace.X) assert.Nil(t, c.ShipGroup(3).StateInSpace.X)
assert.Equal(t, c.MustPlanet(R0_Planet_0_num).Y, c.ShipGroup(3).StateInSpace.Y) assert.Nil(t, c.ShipGroup(3).StateInSpace.Y)
assert.NoError(t, g.SendGroup(Race_0.Name, 4, R0_Planet_0_num, 2)) // un-send 2 of 3 assert.NoError(t, g.SendGroup(Race_0.Name, 4, R0_Planet_0_num, 2)) // un-send 2 of 3
assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 4) assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 4)
@@ -64,8 +64,8 @@ func TestSendGroup(t *testing.T) {
assert.Equal(t, uint(1), c.MustShipGroup(Race_0_idx, 4).Number) assert.Equal(t, uint(1), c.MustShipGroup(Race_0_idx, 4).Number)
assert.Equal(t, game.StateLaunched, c.MustShipGroup(Race_0_idx, 4).State()) assert.Equal(t, game.StateLaunched, c.MustShipGroup(Race_0_idx, 4).State())
assert.NotNil(t, c.MustShipGroup(Race_0_idx, 4).StateInSpace) assert.NotNil(t, c.MustShipGroup(Race_0_idx, 4).StateInSpace)
assert.Equal(t, c.MustPlanet(R0_Planet_0_num).X, c.MustShipGroup(Race_0_idx, 4).StateInSpace.X) assert.Nil(t, c.MustShipGroup(Race_0_idx, 4).StateInSpace.X)
assert.Equal(t, c.MustPlanet(R0_Planet_0_num).Y, c.MustShipGroup(Race_0_idx, 4).StateInSpace.Y) assert.Nil(t, c.MustShipGroup(Race_0_idx, 4).StateInSpace.Y)
assert.NoError(t, g.SendGroup(Race_0.Name, 4, R0_Planet_0_num, 0)) // un-send the rest 1 assert.NoError(t, g.SendGroup(Race_0.Name, 4, R0_Planet_0_num, 0)) // un-send the rest 1
assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 3) assert.Len(t, slices.Collect(c.RaceShipGroups(Race_0_idx)), 3)
+5 -20
View File
@@ -102,10 +102,7 @@ func TestBreakGroup(t *testing.T) {
c, g := newCache() c, g := newCache()
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 13)) // group #1 (0) assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 13)) // group #1 (0)
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 7)) // group #2 (1) - In_Space assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 7)) // group #2 (1) - In_Space
c.ShipGroup(1).StateInSpace = &game.InSpace{ c.ShipGroup(1).StateInSpace = &InSpace
Origin: 1,
Range: 1,
}
fleet := "R0_Fleet" fleet := "R0_Fleet"
assert.NoError(t, g.JoinShipGroupToFleet(Race_0.Name, fleet, 1, 0)) assert.NoError(t, g.JoinShipGroupToFleet(Race_0.Name, fleet, 1, 0))
@@ -178,10 +175,7 @@ func TestTransferGroup(t *testing.T) {
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 17)) // group #2 (2) - In_Space assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 17)) // group #2 (2) - In_Space
assert.NoError(t, g.JoinShipGroupToFleet(Race_0.Name, "R0_Fleet", 2, 0)) assert.NoError(t, g.JoinShipGroupToFleet(Race_0.Name, "R0_Fleet", 2, 0))
assert.NotNil(t, c.ShipGroup(2).FleetID) assert.NotNil(t, c.ShipGroup(2).FleetID)
c.ShipGroup(2).StateInSpace = &game.InSpace{ c.ShipGroup(2).StateInSpace = &InSpace
Origin: 2,
Range: 31.337,
}
c.ShipGroup(2).CargoType = game.CargoMaterial.Ref() c.ShipGroup(2).CargoType = game.CargoMaterial.Ref()
c.ShipGroup(2).Load = 1.234 c.ShipGroup(2).Load = 1.234
@@ -271,10 +265,7 @@ func TestLoadCargo(t *testing.T) {
// 3: idx = 2 / In_Space // 3: idx = 2 / In_Space
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7)) assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7))
c.ShipGroup(2).StateInSpace = &game.InSpace{ c.ShipGroup(2).StateInSpace = &InSpace
Origin: 2,
Range: 31.337,
}
// 4: idx = 3 / loaded with COL // 4: idx = 3 / loaded with COL
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 11)) assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 11))
@@ -394,10 +385,7 @@ func TestUnloadCargo(t *testing.T) {
// 3: idx = 2 / In_Space // 3: idx = 2 / In_Space
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7)) assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7))
c.ShipGroup(2).StateInSpace = &game.InSpace{ c.ShipGroup(2).StateInSpace = &InSpace
Origin: 2,
Range: 31.337,
}
// 4: idx = 3 / loaded with COL // 4: idx = 3 / loaded with COL
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 11)) assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 11))
@@ -491,10 +479,7 @@ func TestDisassembleGroup(t *testing.T) {
// 2: idx = 1 / In_Space // 2: idx = 1 / In_Space
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7)) assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7))
c.ShipGroup(1).StateInSpace = &game.InSpace{ c.ShipGroup(1).StateInSpace = &InSpace
Origin: 2,
Range: 31.337,
}
// 3: idx = 2 / loaded with COL // 3: idx = 2 / loaded with COL
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 10)) assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 10))
@@ -119,7 +119,7 @@ func TestUpgradeGroup(t *testing.T) {
assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 10)) assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 10))
// group #2 - in_space // group #2 - in_space
assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 1)) assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 1))
c.ShipGroup(1).StateInSpace = &game.InSpace{Origin: 2, Range: 1.23} c.ShipGroup(1).StateInSpace = &InSpace
// group #3 - in_orbit, foreign planet // group #3 - in_orbit, foreign planet
assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 1)) assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 1))
c.ShipGroup(2).Destination = R1_Planet_1_num c.ShipGroup(2).Destination = R1_Planet_1_num
+8 -7
View File
@@ -46,12 +46,10 @@ func (sgs ShipGroupState) String() string {
} }
type InSpace struct { type InSpace struct {
// X, Y are nil for Launched state
Origin uint `json:"origin"` Origin uint `json:"origin"`
X Float `json:"x"` X *Float `json:"x,omitempty"`
Y Float `json:"y"` Y *Float `json:"y,omitempty"`
// zero is for Launched status
// TODO: calculate range dynamically -BUT- if affects ShipGroup.State()
Range Float `json:"range"`
} }
func (is InSpace) Equal(other InSpace) bool { func (is InSpace) Equal(other InSpace) bool {
@@ -59,7 +57,10 @@ func (is InSpace) Equal(other InSpace) bool {
} }
func (is InSpace) Launched() bool { func (is InSpace) Launched() bool {
return is.Range == 0 if (is.X == nil && is.Y != nil) || (is.X != nil && is.Y == nil) {
panic("group in space state invalid: one of coordinate is not set")
}
return is.X == nil && is.Y == is.X
} }
type InUpgrade struct { type InUpgrade struct {
@@ -132,7 +133,7 @@ func (sg ShipGroup) State() ShipGroupState {
case sg.StateInSpace == nil && sg.StateUpgrade == nil: case sg.StateInSpace == nil && sg.StateUpgrade == nil:
return StateInOrbit return StateInOrbit
case sg.StateInSpace != nil && sg.StateUpgrade == nil: case sg.StateInSpace != nil && sg.StateUpgrade == nil:
if sg.StateInSpace.Range > 0 { if !sg.StateInSpace.Launched() {
return StateInSpace return StateInSpace
} }
if sg.StateTransfer { if sg.StateTransfer {
+3 -1
View File
@@ -198,9 +198,11 @@ func TestShipGroupEqual(t *testing.T) {
assert.False(t, left.Equal(right)) assert.False(t, left.Equal(right))
right = *left right = *left
coord := game.Float(1)
left.StateInSpace = &game.InSpace{ left.StateInSpace = &game.InSpace{
Origin: 1, Origin: 1,
Range: 1, X: &coord,
Y: &coord,
} }
assert.False(t, left.Equal(right)) assert.False(t, left.Equal(right))