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, 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
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
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
+1 -4
View File
@@ -50,10 +50,7 @@ func TestCollectBombingGroups(t *testing.T) {
// 3: idx = 2 / In_Space
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 1))
c.ShipGroup(2).StateInSpace = &game.InSpace{
Origin: 2,
Range: 31.337,
}
c.ShipGroup(2).StateInSpace = &InSpace
// 4: idx = 3 / Has no Ammo
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"
UnknownRace = "UnknownRace"
InSpace = game.InSpace{Origin: 2, X: floatRef(1.23), Y: floatRef(1.23)}
)
// [ ] Delete this fake test
@@ -153,3 +155,8 @@ func newCache() (*controller.Cache, *controller.Controller) {
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)
// group #2 - in_space
c.ShipGroup(1).StateInSpace = &game.InSpace{Origin: 2, Range: 1.23}
c.ShipGroup(1).StateInSpace = &InSpace
assert.ErrorContains(t,
g.SendFleet(UnknownRace, fleetSending, 2),
+2 -5
View File
@@ -92,17 +92,14 @@ func TestJoinShipGroupToFleet(t *testing.T) {
// group not In_Orbit
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 7))
gi = 3
c.ShipGroup(gi).StateInSpace = &game.InSpace{
Origin: 2,
Range: 1,
}
c.ShipGroup(gi).StateInSpace = &InSpace
assert.ErrorContains(t,
g.JoinShipGroupToFleet(Race_0.Name, fleetOne, c.ShipGroup(gi).Index, 0),
e.GenericErrorText(e.ErrShipsBusy))
c.ShipGroup(gi).StateInSpace = nil
// 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
assert.ErrorContains(t,
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
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7))
c.ShipGroup(2).StateInSpace = &game.InSpace{
Origin: 2,
Range: 31.337,
}
c.ShipGroup(2).StateInSpace = &InSpace
// 4: idx = 3 / loaded with COL
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
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7))
c.ShipGroup(2).StateInSpace = &game.InSpace{
Origin: 2,
Range: 31.337,
}
c.ShipGroup(2).StateInSpace = &InSpace
// 4: idx = 3 / loaded with COL
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
x, y, arrived =
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)
sg.StateInSpace.Y = game.F(y)
fx, fy := game.F(x), game.F(y)
sg.StateInSpace.X = &fx
sg.StateInSpace.Y = &fy
if arrived {
sg.StateInSpace = nil
}
+1 -4
View File
@@ -24,10 +24,7 @@ func TestListMoveableGroupIds(t *testing.T) {
// 4: idx = 3 / [v] In_Space
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7))
c.ShipGroup(3).StateInSpace = &game.InSpace{
Origin: 2,
Range: 31.337,
}
c.ShipGroup(3).StateInSpace = &InSpace
// 5: idx = 4 / [x] In_Upgrage
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 {
sg.StateInSpace = &game.InSpace{
Origin: sg.Destination,
X: game.F(originX),
Y: game.F(originY),
X: nil,
Y: nil,
}
sg.Destination = destination
return sg
+5 -5
View File
@@ -17,7 +17,7 @@ func TestSendGroup(t *testing.T) {
// group #2 - in_space
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}
c.ShipGroup(1).StateInSpace = &game.InSpace{Origin: 2, Range: 1.23}
c.ShipGroup(1).StateInSpace = &InSpace
// group #3 - in_orbit, unmovable
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))
@@ -54,8 +54,8 @@ func TestSendGroup(t *testing.T) {
assert.Equal(t, uint(3), c.ShipGroup(3).Number)
assert.Equal(t, game.StateLaunched, c.ShipGroup(3).State())
assert.NotNil(t, c.ShipGroup(3).StateInSpace)
assert.Equal(t, c.MustPlanet(R0_Planet_0_num).X, 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.X)
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.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, game.StateLaunched, c.MustShipGroup(Race_0_idx, 4).State())
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.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.X)
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.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()
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
c.ShipGroup(1).StateInSpace = &game.InSpace{
Origin: 1,
Range: 1,
}
c.ShipGroup(1).StateInSpace = &InSpace
fleet := "R0_Fleet"
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, g.JoinShipGroupToFleet(Race_0.Name, "R0_Fleet", 2, 0))
assert.NotNil(t, c.ShipGroup(2).FleetID)
c.ShipGroup(2).StateInSpace = &game.InSpace{
Origin: 2,
Range: 31.337,
}
c.ShipGroup(2).StateInSpace = &InSpace
c.ShipGroup(2).CargoType = game.CargoMaterial.Ref()
c.ShipGroup(2).Load = 1.234
@@ -271,10 +265,7 @@ func TestLoadCargo(t *testing.T) {
// 3: idx = 2 / In_Space
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7))
c.ShipGroup(2).StateInSpace = &game.InSpace{
Origin: 2,
Range: 31.337,
}
c.ShipGroup(2).StateInSpace = &InSpace
// 4: idx = 3 / loaded with COL
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
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7))
c.ShipGroup(2).StateInSpace = &game.InSpace{
Origin: 2,
Range: 31.337,
}
c.ShipGroup(2).StateInSpace = &InSpace
// 4: idx = 3 / loaded with COL
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
assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7))
c.ShipGroup(1).StateInSpace = &game.InSpace{
Origin: 2,
Range: 31.337,
}
c.ShipGroup(1).StateInSpace = &InSpace
// 3: idx = 2 / loaded with COL
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))
// group #2 - in_space
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
assert.NoError(t, c.CreateShips(Race_0_idx, ShipType_Cruiser, R0_Planet_0_num, 1))
c.ShipGroup(2).Destination = R1_Planet_1_num