cmd: merge ship types
This commit is contained in:
@@ -44,6 +44,14 @@ type Fleet struct {
|
||||
ShipGroups []ShipGroup `json:"group"`
|
||||
}
|
||||
|
||||
func (st ShipType) Equal(o ShipType) bool {
|
||||
return st.Drive == o.Drive &&
|
||||
st.Weapons == o.Weapons &&
|
||||
st.Armament == o.Armament &&
|
||||
st.Shields == o.Shields &&
|
||||
st.Cargo == o.Cargo
|
||||
}
|
||||
|
||||
func (st ShipType) EmptyMass() float64 {
|
||||
shipMass := st.Drive + st.Shields + st.Cargo + st.WeaponsMass()
|
||||
return shipMass
|
||||
@@ -197,6 +205,53 @@ func (g Game) createShipTypeInternal(ri int, name string, d, w, s, c float64, a
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g Game) MergeShipType(race, name, targetName string) error {
|
||||
ri, err := g.raceIndex(race)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return g.mergeShipTypeInternal(ri, name, targetName)
|
||||
}
|
||||
|
||||
func (g Game) mergeShipTypeInternal(ri int, name, targetName string) error {
|
||||
st := slices.IndexFunc(g.Race[ri].ShipTypes, func(st ShipType) bool { return st.Name == name })
|
||||
if st < 0 {
|
||||
return e.NewEntityNotExistsError("source ship type %w", name)
|
||||
}
|
||||
if name == targetName {
|
||||
return e.NewEntityTypeNameEqualityError("ship type %q", targetName)
|
||||
}
|
||||
tt := slices.IndexFunc(g.Race[ri].ShipTypes, func(st ShipType) bool { return st.Name == targetName })
|
||||
if tt < 0 {
|
||||
return e.NewEntityNotExistsError("target ship type %w", name)
|
||||
}
|
||||
if !g.Race[ri].ShipTypes[st].Equal(g.Race[ri].ShipTypes[tt]) {
|
||||
return e.NewMergeShipTypeNotEqualError()
|
||||
}
|
||||
|
||||
// switch planet productions to the new type
|
||||
for pl := range g.Map.Planet {
|
||||
if g.Map.Planet[pl].Owner == g.Race[ri].ID &&
|
||||
g.Map.Planet[pl].Production.Production == ProductionShip &&
|
||||
g.Map.Planet[pl].Production.SubjectID != nil &&
|
||||
*g.Map.Planet[pl].Production.SubjectID == g.Race[ri].ShipTypes[st].ID {
|
||||
g.Map.Planet[pl].Production.SubjectID = &g.Race[ri].ShipTypes[tt].ID
|
||||
}
|
||||
}
|
||||
|
||||
// switch ship groups to the new type
|
||||
for sg := range g.Race[ri].ShipGroups {
|
||||
if g.Race[ri].ShipGroups[sg].TypeID == g.Race[ri].ShipTypes[st].ID {
|
||||
g.Race[ri].ShipGroups[sg].TypeID = g.Race[ri].ShipTypes[tt].ID
|
||||
}
|
||||
}
|
||||
|
||||
// remove the source type
|
||||
g.Race[ri].ShipTypes = append(g.Race[ri].ShipTypes[:st], g.Race[ri].ShipTypes[st+1:]...)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkShipTypeValues(d, w, s, c float64, a int) error {
|
||||
if !checkShipTypeValueDWSC(d) {
|
||||
return e.NewDriveValueError(d)
|
||||
|
||||
Reference in New Issue
Block a user