cmd: unload cargo
This commit is contained in:
+138
-16
@@ -145,7 +145,7 @@ func (g *Game) BreakGroup(raceName string, groupIndex, quantity uint) error {
|
||||
return g.breakGroupInternal(ri, groupIndex, quantity)
|
||||
}
|
||||
|
||||
func (g *Game) LoadCargo(raceName string, groupIndex uint, cargoType string, quantity float64) error {
|
||||
func (g *Game) LoadCargo(raceName string, groupIndex uint, cargoType string, ships uint, quantity float64) error {
|
||||
ri, err := g.raceIndex(raceName)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -154,10 +154,102 @@ func (g *Game) LoadCargo(raceName string, groupIndex uint, cargoType string, qua
|
||||
if !ok {
|
||||
return e.NewCargoTypeInvalidError(cargoType)
|
||||
}
|
||||
return g.loadCargoInternal(ri, groupIndex, ct, quantity)
|
||||
return g.loadCargoInternal(ri, groupIndex, ct, ships, quantity)
|
||||
}
|
||||
|
||||
func (g *Game) loadCargoInternal(ri int, groupIndex uint, ct CargoType, quantity float64) error {
|
||||
func (g *Game) UnloadCargo(raceName string, groupIndex uint, ships uint, quantity float64) error {
|
||||
ri, err := g.raceIndex(raceName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return g.unloadCargoInternal(ri, groupIndex, ships, quantity)
|
||||
}
|
||||
|
||||
// Промышленность и Сырье могут быть выгружены на любой планете.
|
||||
// Колонисты могут быть высажены только на планеты, принадлежащие Вам или на необитаемые планеты.
|
||||
func (g *Game) unloadCargoInternal(ri int, groupIndex uint, ships uint, quantity float64) error {
|
||||
if ships == 0 && quantity > 0 {
|
||||
return e.NewCargoQuantityWithoutGroupBreakError()
|
||||
}
|
||||
sgi := -1
|
||||
for i, sg := range g.listIndexShipGroups(ri) {
|
||||
if sgi < 0 && sg.Index == groupIndex {
|
||||
sgi = i
|
||||
}
|
||||
}
|
||||
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()
|
||||
}
|
||||
var sti int
|
||||
if sti = slices.IndexFunc(g.Race[ri].ShipTypes, func(st ShipType) bool { return st.ID == g.ShipGroups[sgi].TypeID }); sti < 0 {
|
||||
// hard to test, need manual game data invalidation
|
||||
return e.NewGameStateError("not found: ShipType ID=%v", g.ShipGroups[sgi].TypeID)
|
||||
}
|
||||
if g.Race[ri].ShipTypes[sti].Cargo < 1 {
|
||||
return e.NewNoCargoBayError("ship_type %q", g.Race[ri].ShipTypes[sti].Name)
|
||||
}
|
||||
if g.ShipGroups[sgi].CargoType == nil || g.ShipGroups[sgi].Load == 0 {
|
||||
return e.NewCargoUnloadEmptyError()
|
||||
}
|
||||
ct := *g.ShipGroups[sgi].CargoType
|
||||
pl := slices.IndexFunc(g.Map.Planet, func(p Planet) bool { return p.Number == g.ShipGroups[sgi].Destination })
|
||||
if pl < 0 {
|
||||
return e.NewGameStateError("planet #%d", g.ShipGroups[sgi].Destination)
|
||||
}
|
||||
if ct == CargoColonist {
|
||||
if g.Map.Planet[pl].Owner != uuid.Nil && g.Map.Planet[pl].Owner != g.Race[ri].ID {
|
||||
return e.NewEntityNotOwnedError("planet #%d unload %v", g.Map.Planet[pl].Number, ct)
|
||||
}
|
||||
if g.Map.Planet[pl].Owner == uuid.Nil {
|
||||
g.Map.Planet[pl].Owner = g.Race[ri].ID
|
||||
}
|
||||
}
|
||||
var availableOnPlanet *float64
|
||||
switch ct {
|
||||
case CargoMaterial:
|
||||
availableOnPlanet = &g.Map.Planet[pl].Material
|
||||
case CargoCapital:
|
||||
availableOnPlanet = &g.Map.Planet[pl].Capital
|
||||
case CargoColonist:
|
||||
availableOnPlanet = &g.Map.Planet[pl].Colonists
|
||||
default:
|
||||
return e.NewGameStateError("CargoType not accepted: %v", ct)
|
||||
}
|
||||
if ships > 0 && ships < g.ShipGroups[sgi].Number {
|
||||
nsgi, err := g.breakGroupSafe(ri, groupIndex, ships)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sgi = nsgi
|
||||
}
|
||||
toBeUnloaded := quantity
|
||||
if quantity == 0 {
|
||||
toBeUnloaded = g.ShipGroups[sgi].Load
|
||||
}
|
||||
if toBeUnloaded > g.ShipGroups[sgi].Load {
|
||||
return e.NewCargoUnoadNotEnoughError("load: %.03f", g.ShipGroups[sgi].Load)
|
||||
}
|
||||
*availableOnPlanet += toBeUnloaded
|
||||
g.ShipGroups[sgi].Load -= toBeUnloaded
|
||||
if g.ShipGroups[sgi].Load == 0 {
|
||||
g.ShipGroups[sgi].CargoType = nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
TODO: multi-command processing: Game data should NOT be changed when error is returned
|
||||
*/
|
||||
// Корабль может нести только один тип груза одновременно.
|
||||
// Возможные типы груза - это колонисты, сырье и промышленность.
|
||||
// Груз может быть доставлен на борт корабля с Вашей или не занятой планеты, на которой он имеется.
|
||||
func (g *Game) loadCargoInternal(ri int, groupIndex uint, ct CargoType, ships uint, quantity float64) error {
|
||||
if ships == 0 && quantity > 0 {
|
||||
return e.NewCargoQuantityWithoutGroupBreakError()
|
||||
}
|
||||
sgi := -1
|
||||
for i, sg := range g.listIndexShipGroups(ri) {
|
||||
if sgi < 0 && sg.Index == groupIndex {
|
||||
@@ -174,8 +266,8 @@ func (g *Game) loadCargoInternal(ri int, groupIndex uint, ct CargoType, quantity
|
||||
if pl < 0 {
|
||||
return e.NewGameStateError("planet #%d", g.ShipGroups[sgi].Destination)
|
||||
}
|
||||
if g.Map.Planet[pl].Owner != g.Race[ri].ID {
|
||||
return e.NewEntityNotOwnedError("planet %#d", g.Map.Planet[pl].Number)
|
||||
if g.Map.Planet[pl].Owner != uuid.Nil && g.Map.Planet[pl].Owner != g.Race[ri].ID {
|
||||
return e.NewEntityNotOwnedError("planet #%d", g.Map.Planet[pl].Number)
|
||||
}
|
||||
var sti int
|
||||
if sti = slices.IndexFunc(g.Race[ri].ShipTypes, func(st ShipType) bool { return st.ID == g.ShipGroups[sgi].TypeID }); sti < 0 {
|
||||
@@ -183,11 +275,18 @@ func (g *Game) loadCargoInternal(ri int, groupIndex uint, ct CargoType, quantity
|
||||
return e.NewGameStateError("not found: ShipType ID=%v", g.ShipGroups[sgi].TypeID)
|
||||
}
|
||||
if g.Race[ri].ShipTypes[sti].Cargo < 1 {
|
||||
return e.NewCargoLoadNoCargoBayError("ship_type %q", g.Race[ri].ShipTypes[sti].Name)
|
||||
return e.NewNoCargoBayError("ship_type %q", g.Race[ri].ShipTypes[sti].Name)
|
||||
}
|
||||
if g.ShipGroups[sgi].CargoType != nil && *g.ShipGroups[sgi].CargoType != ct {
|
||||
return e.NewCargoLoadNotEqualError("cargo: %v", *g.ShipGroups[sgi].CargoType)
|
||||
}
|
||||
if ships > 0 && ships < g.ShipGroups[sgi].Number {
|
||||
nsgi, err := g.breakGroupSafe(ri, groupIndex, ships)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sgi = nsgi
|
||||
}
|
||||
capacity := g.ShipGroups[sgi].CargoCapacity(&g.Race[ri].ShipTypes[sti])
|
||||
freeShipGroupCargoLoad := capacity - g.ShipGroups[sgi].Load
|
||||
if freeShipGroupCargoLoad == 0 {
|
||||
@@ -338,21 +437,44 @@ func (g *Game) breakGroupInternal(ri int, groupIndex, quantity uint) error {
|
||||
if quantity == 0 || quantity == g.ShipGroups[sgi].Number {
|
||||
g.ShipGroups[sgi].FleetID = nil
|
||||
} else {
|
||||
newGroup := g.ShipGroups[sgi]
|
||||
if g.ShipGroups[sgi].CargoType != nil {
|
||||
newGroup.Load = g.ShipGroups[sgi].Load / float64(g.ShipGroups[sgi].Number) * float64(quantity)
|
||||
g.ShipGroups[sgi].Load -= newGroup.Load
|
||||
if _, err := g.breakGroupSafe(ri, groupIndex, quantity); err != nil {
|
||||
return err
|
||||
}
|
||||
newGroup.Number = quantity
|
||||
g.ShipGroups[sgi].Number -= newGroup.Number
|
||||
newGroup.Index = maxIndex + 1
|
||||
newGroup.FleetID = nil
|
||||
g.ShipGroups = append(g.ShipGroups, newGroup)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Game) breakGroupSafe(ri int, groupIndex uint, newGroupShips uint) (int, 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 -1, e.NewEntityNotExistsError("group #%d", groupIndex)
|
||||
}
|
||||
if g.ShipGroups[sgi].Number < newGroupShips {
|
||||
return -1, e.NewBreakGroupIllegalNumberError("group #%d ships: %d -> %d", g.ShipGroups[sgi].Index, g.ShipGroups[sgi].Number, newGroupShips)
|
||||
}
|
||||
newGroup := g.ShipGroups[sgi]
|
||||
if g.ShipGroups[sgi].CargoType != nil {
|
||||
newGroup.Load = g.ShipGroups[sgi].Load / float64(g.ShipGroups[sgi].Number) * float64(newGroupShips)
|
||||
g.ShipGroups[sgi].Load -= newGroup.Load
|
||||
}
|
||||
newGroup.Number = newGroupShips
|
||||
g.ShipGroups[sgi].Number -= newGroup.Number
|
||||
newGroup.Index = maxIndex + 1
|
||||
newGroup.FleetID = nil
|
||||
g.ShipGroups = append(g.ShipGroups, newGroup)
|
||||
return len(g.ShipGroups) - 1, nil
|
||||
}
|
||||
|
||||
func (g *Game) joinEqualGroupsInternal(ri int) {
|
||||
shipGroups := slices.Collect(maps.Values(maps.Collect(g.listIndexShipGroups(ri))))
|
||||
origin := len(shipGroups)
|
||||
@@ -385,7 +507,7 @@ func (g *Game) createShips(ri int, shipTypeName string, planetNumber int, quanti
|
||||
return e.NewEntityNotExistsError("planet #%d", planetNumber)
|
||||
}
|
||||
if g.Map.Planet[pl].Owner != g.Race[ri].ID {
|
||||
return e.NewEntityNotOwnedError("planet %#d", planetNumber)
|
||||
return e.NewEntityNotOwnedError("planet #%d", planetNumber)
|
||||
}
|
||||
|
||||
var maxIndex uint
|
||||
|
||||
Reference in New Issue
Block a user