refactor: fleet/group AtPlanet

This commit is contained in:
Ilia Denisov
2026-02-05 15:49:51 +02:00
parent e90218368c
commit fef1be577d
7 changed files with 24 additions and 21 deletions
+13 -14
View File
@@ -17,7 +17,7 @@ type FleetState struct {
State game.ShipGroupState
Destination uint
InSpace func() (game.InSpace, bool)
OnPlanet func() (uint, bool)
AtPlanet func() (uint, bool)
}
func (fs *FleetState) inSpace() bool {
@@ -25,16 +25,16 @@ func (fs *FleetState) inSpace() bool {
return ok
}
func (fs FleetState) OnSamePlanet(other FleetState) bool {
op1, ok := fs.OnPlanet()
func (fs FleetState) AtSamePlanet(other FleetState) bool {
pn1, ok := fs.AtPlanet()
if !ok {
return false
}
op2, ok := other.OnPlanet()
pn2, ok := other.AtPlanet()
if !ok {
return false
}
return op1 == op2
return pn1 == pn2
}
func (c *Cache) FleetState(fleetID uuid.UUID) FleetState {
@@ -43,16 +43,15 @@ func (c *Cache) FleetState(fleetID uuid.UUID) FleetState {
fs := &FleetState{
State: fleetStateNil,
InSpace: func() (game.InSpace, bool) { return game.InSpace{}, false },
OnPlanet: func() (uint, bool) { return 0, false },
AtPlanet: func() (uint, bool) { return 0, false },
}
for sg := range c.FleetGroups(ri, fi) {
if fs.State == fleetStateNil {
fs.State = sg.State()
fs.Destination = sg.Destination
if planet, ok := sg.OnPlanet(); ok {
fs.OnPlanet = func() (uint, bool) { return planet, ok }
}
if sg.StateInSpace != nil {
if pn, ok := sg.AtPlanet(); ok {
fs.AtPlanet = func() (uint, bool) { return pn, ok }
} else if sg.StateInSpace != nil {
fs.InSpace = func() (game.InSpace, bool) { return *sg.StateInSpace, true }
}
continue
@@ -63,8 +62,8 @@ func (c *Cache) FleetState(fleetID uuid.UUID) FleetState {
if fs.Destination != sg.Destination {
panic(fmt.Sprintf("FleetState: one or more ships in race's %q fleet %q has different destination", c.g.Race[ri].Name, c.g.Fleets[fi].Name))
}
if planet, ok := sg.OnPlanet(); ok {
if onPlanet, ok := fs.OnPlanet(); ok && onPlanet != planet {
if planet, ok := sg.AtPlanet(); ok {
if onPlanet, ok := fs.AtPlanet(); ok && onPlanet != planet {
panic(fmt.Sprintf("FleetState: one or more ships in race's %q fleet %q are on different planets: %d <> %d", c.g.Race[ri].Name, c.g.Fleets[fi].Name, onPlanet, planet))
}
}
@@ -142,7 +141,7 @@ func (c *Cache) JoinShipGroupToFleet(ri int, fleetName string, groupIndex, quant
}
} else {
fleetState := c.FleetState(c.g.Fleets[fi].ID)
if onPlanet, ok := fleetState.OnPlanet(); (ok && onPlanet != c.ShipGroup(sgi).Destination) || fleetState.State != game.StateInOrbit {
if onPlanet, ok := fleetState.AtPlanet(); (ok && onPlanet != c.ShipGroup(sgi).Destination) || fleetState.State != game.StateInOrbit {
return e.NewShipsNotOnSamePlanetError("fleet: %s", fleetName)
}
}
@@ -198,7 +197,7 @@ func (c *Cache) JoinFleets(ri int, fleetSourceName, fleetTargetName string) (err
}
stateSrc := c.FleetState(c.g.Fleets[fiSource].ID)
stateDst := c.FleetState(c.g.Fleets[fiTarget].ID)
if !stateSrc.OnSamePlanet(stateDst) {
if !stateSrc.AtSamePlanet(stateDst) {
return e.NewShipsNotOnSamePlanetError()
}
+1 -1
View File
@@ -22,7 +22,7 @@ func (c *Cache) SendFleet(ri, fi int, planetNumber uint) error {
c.validateRaceIndex(ri)
c.validateFleetIndex(fi)
fleetState := c.FleetState(c.g.Fleets[fi].ID)
sourcePlanet, ok := fleetState.OnPlanet()
sourcePlanet, ok := fleetState.AtPlanet()
if !ok || game.StateInOrbit != fleetState.State && game.StateLaunched != fleetState.State {
return e.NewShipsBusyError()
}
+1 -1
View File
@@ -27,7 +27,7 @@ func (c *Cache) SendGroup(ri int, groupIndex, planetNumber, quantity uint) error
return e.NewSendShipHasNoDrivesError()
}
sourcePlanet, ok := c.ShipGroup(sgi).OnPlanet()
sourcePlanet, ok := c.ShipGroup(sgi).AtPlanet()
if !ok {
return e.NewShipsBusyError()
}