refactor: group launched/in_space state
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -46,12 +46,10 @@ func (sgs ShipGroupState) String() string {
|
||||
}
|
||||
|
||||
type InSpace struct {
|
||||
Origin uint `json:"origin"`
|
||||
X Float `json:"x"`
|
||||
Y Float `json:"y"`
|
||||
// zero is for Launched status
|
||||
// TODO: calculate range dynamically -BUT- if affects ShipGroup.State()
|
||||
Range Float `json:"range"`
|
||||
// X, Y are nil for Launched state
|
||||
Origin uint `json:"origin"`
|
||||
X *Float `json:"x,omitempty"`
|
||||
Y *Float `json:"y,omitempty"`
|
||||
}
|
||||
|
||||
func (is InSpace) Equal(other InSpace) bool {
|
||||
@@ -59,7 +57,10 @@ func (is InSpace) Equal(other InSpace) 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 {
|
||||
@@ -132,7 +133,7 @@ func (sg ShipGroup) State() ShipGroupState {
|
||||
case sg.StateInSpace == nil && sg.StateUpgrade == nil:
|
||||
return StateInOrbit
|
||||
case sg.StateInSpace != nil && sg.StateUpgrade == nil:
|
||||
if sg.StateInSpace.Range > 0 {
|
||||
if !sg.StateInSpace.Launched() {
|
||||
return StateInSpace
|
||||
}
|
||||
if sg.StateTransfer {
|
||||
|
||||
@@ -198,9 +198,11 @@ func TestShipGroupEqual(t *testing.T) {
|
||||
assert.False(t, left.Equal(right))
|
||||
|
||||
right = *left
|
||||
coord := game.Float(1)
|
||||
left.StateInSpace = &game.InSpace{
|
||||
Origin: 1,
|
||||
Range: 1,
|
||||
X: &coord,
|
||||
Y: &coord,
|
||||
}
|
||||
assert.False(t, left.Equal(right))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user