cmd: break group

This commit is contained in:
Ilia Denisov
2025-12-08 21:33:48 +03:00
parent 4b2b042068
commit 7002f3d299
4 changed files with 113 additions and 3 deletions
+45
View File
@@ -122,6 +122,51 @@ func (g *Game) JoinEqualGroups(raceName string) error {
return nil
}
func (g *Game) BreakGroup(raceName string, groupIndex, quantity uint) error {
ri, err := g.raceIndex(raceName)
if err != nil {
return err
}
return g.breakGroupInternal(ri, groupIndex, quantity)
}
func (g *Game) breakGroupInternal(ri int, groupIndex, quantity uint) error {
sgi := -1
var maxIndex uint
for i, sg := range g.listIndexShipGroups(ri) {
if sgi < 0 && sg.Index == groupIndex {
sgi = i
}
if sg.Index > maxIndex {
maxIndex = sg.Index
}
}
if sgi < 0 {
return e.NewEntityNotExistsError("group #%d", groupIndex)
}
if g.ShipGroups[sgi].State != "In_Orbit" || g.ShipGroups[sgi].Origin != nil || g.ShipGroups[sgi].Range != nil {
return e.NewShipsBusyError()
}
if g.ShipGroups[sgi].Number < quantity {
return e.NewBeakGroupNumberNotEnoughError("%d<%d", g.ShipGroups[sgi].Number, quantity)
}
if quantity == 0 || quantity == g.ShipGroups[sgi].Number {
g.ShipGroups[sgi].FleetID = nil
} else {
newGroup := g.ShipGroups[sgi]
newGroup.Number = quantity
g.ShipGroups[sgi].Number -= quantity
newGroup.Index = maxIndex + 1
newGroup.FleetID = nil
g.ShipGroups = append(g.ShipGroups, newGroup)
}
return nil
}
func (g *Game) joinEqualGroupsInternal(ri int) {
shipGroups := slices.Collect(maps.Values(maps.Collect(g.listIndexShipGroups(ri))))
origin := len(shipGroups)