cmd: load cargo

This commit is contained in:
Ilia Denisov
2025-12-11 00:18:00 +03:00
parent 52bd71d1be
commit 4447d125ac
4 changed files with 225 additions and 4 deletions
+96 -3
View File
@@ -14,16 +14,27 @@ import (
type CargoType string
const (
// CargoNone CargoType = "-"
CargoColonist CargoType = "COL" // Колонисты
CargoMaterial CargoType = "MAT" // Сырьё
CargoCapital CargoType = "CAP" // Промышленность
)
var (
cargoTypeSet map[string]CargoType = map[string]CargoType{
CargoColonist.String(): CargoColonist,
CargoMaterial.String(): CargoMaterial,
CargoCapital.String(): CargoCapital,
}
)
func (ct CargoType) Ref() *CargoType {
return &ct
}
func (ct CargoType) String() string {
return string(ct)
}
type ShipGroup struct {
Index uint `json:"index"` // FIXME: use UUID for Group Index (ordered)
OwnerID uuid.UUID `json:"ownerId"` // Race link
@@ -134,6 +145,84 @@ 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 {
ri, err := g.raceIndex(raceName)
if err != nil {
return err
}
ct, ok := cargoTypeSet[cargoType]
if !ok {
return e.NewCargoTypeInvalidError(cargoType)
}
return g.loadCargoInternal(ri, groupIndex, ct, quantity)
}
func (g *Game) loadCargoInternal(ri int, groupIndex uint, ct CargoType, quantity float64) error {
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()
}
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 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 {
// 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.NewCargoLoadNoCargoBayError("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)
}
capacity := g.ShipGroups[sgi].CargoCapacity(&g.Race[ri].ShipTypes[sti])
freeShipGroupCargoLoad := capacity - g.ShipGroups[sgi].Load
if freeShipGroupCargoLoad == 0 {
return e.NewCargoLoadNoSpaceLeftError()
}
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 quantity > *availableOnPlanet || *availableOnPlanet == 0 {
return e.NewCargoLoadNotEnoughError("planet: #%d, %s=%.03f", g.Map.Planet[pl].Number, ct, *availableOnPlanet)
}
toBeLoaded := quantity
if quantity == 0 {
toBeLoaded = *availableOnPlanet
}
if toBeLoaded > freeShipGroupCargoLoad {
toBeLoaded = freeShipGroupCargoLoad
}
*availableOnPlanet = *availableOnPlanet - toBeLoaded
g.ShipGroups[sgi].Load += toBeLoaded
// fmt.Println("capacity:", capacity, "loaded:", g.ShipGroups[sgi].Load, "free:", capacity-g.ShipGroups[sgi].Load)
if g.ShipGroups[sgi].Load > 0 {
g.ShipGroups[sgi].CargoType = &ct
}
return nil
}
func (g *Game) GiveawayGroup(raceName, raceAcceptor string, groupIndex, quantity uint) error {
ri, err := g.raceIndex(raceName)
if err != nil {
@@ -148,7 +237,7 @@ func (g *Game) GiveawayGroup(raceName, raceAcceptor string, groupIndex, quantity
func (g *Game) giveawayGroupInternal(ri, riAccept int, groupIndex, quantity uint) (err error) {
if ri == riAccept {
return e.NewInputSameRaceError(g.Race[riAccept].Name)
return e.NewSameRaceError(g.Race[riAccept].Name)
}
sgi := -1
for i, sg := range g.listIndexShipGroups(ri) {
@@ -250,8 +339,12 @@ func (g *Game) breakGroupInternal(ri int, groupIndex, quantity uint) error {
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
}
newGroup.Number = quantity
g.ShipGroups[sgi].Number -= quantity
g.ShipGroups[sgi].Number -= newGroup.Number
newGroup.Index = maxIndex + 1
newGroup.FleetID = nil
g.ShipGroups = append(g.ShipGroups, newGroup)