refactor: game funcs moved to controller

This commit is contained in:
Ilia Denisov
2026-01-15 14:42:04 +02:00
parent fe8a8d4150
commit 16aba8435d
47 changed files with 1023 additions and 3093 deletions
+20 -35
View File
@@ -23,23 +23,6 @@ func (c *Cache) SendGroup(ri int, groupIndex, planetNumber, quantity uint) error
}
st := c.ShipGroupShipClass(sgi)
// 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)
// }
// 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)
// }
// st := g.Race[ri].ShipTypes[sti]
if st.DriveBlockMass() == 0 {
return e.NewSendShipHasNoDrivesError()
}
@@ -53,16 +36,14 @@ func (c *Cache) SendGroup(ri int, groupIndex, planetNumber, quantity uint) error
return e.NewBeakGroupNumberNotEnoughError("%d<%d", c.ShipGroup(sgi).Number, quantity)
}
p1 := c.MustPlanet(sourcePlanet)
// p1, ok := PlanetByNum(g, sourcePlanet)
// if !ok {
// return e.NewGameStateError("source planet #%d does not exists", sourcePlanet)
// }
p2 := c.MustPlanet(planetNumber)
// p2, ok := PlanetByNum(g, planetNumber)
// if !ok {
// return e.NewEntityNotExistsError("destination planet #%d", planetNumber)
// }
p1, ok := c.Planet(sourcePlanet)
if !ok {
return e.NewGameStateError("source planet #%d does not exists", sourcePlanet)
}
p2, ok := c.Planet(planetNumber)
if !ok {
return e.NewEntityNotExistsError("destination planet #%d", planetNumber)
}
rangeToDestination := util.ShortDistance(c.g.Map.Width, c.g.Map.Height, p1.X, p1.Y, p2.X, p2.Y)
if rangeToDestination > c.g.Race[ri].FlightDistance() {
return e.NewSendUnreachableDestinationError("range=%.03f", rangeToDestination)
@@ -76,35 +57,39 @@ func (c *Cache) SendGroup(ri int, groupIndex, planetNumber, quantity uint) error
sgi = nsgi
}
if sourcePlanet == planetNumber {
c.UnsendShips(*c.ShipGroup(sgi))
if p1.Number == p2.Number {
c.UnsendShips(c.ShipGroup(sgi))
c.JoinEqualGroups(ri)
return nil
}
c.LaunchShips(*c.ShipGroup(sgi), planetNumber)
c.LaunchShips(c.ShipGroup(sgi), planetNumber)
return nil
}
func (c *Cache) LaunchShips(sg game.ShipGroup, destination uint) *game.ShipGroup {
func (c *Cache) LaunchShips(sg *game.ShipGroup, destination uint) *game.ShipGroup {
for i := range c.ShipGroupsIndex() {
if c.ShipGroup(i).OwnerID == sg.OwnerID && c.ShipGroup(i).Index == sg.Index {
state := c.g.ShipGroups[i].State()
state := c.ShipGroup(i).State()
if state != game.StateInOrbit && state != game.StateLaunched {
panic("state invalid")
}
c.g.ShipGroups[i] = LaunchShips(sg, destination)
c.g.ShipGroups[i] = LaunchShips(*sg, destination)
return &c.g.ShipGroups[i]
}
}
panic("ship group not found")
}
func (c *Cache) UnsendShips(sg game.ShipGroup) *game.ShipGroup {
func (c *Cache) UnsendShips(sg *game.ShipGroup) *game.ShipGroup {
for i := range c.ShipGroupsIndex() {
if c.ShipGroup(i).OwnerID == sg.OwnerID && c.ShipGroup(i).Index == sg.Index {
c.g.ShipGroups[i] = UnsendShips(sg)
state := c.ShipGroup(i).State()
if state != game.StateLaunched {
panic("state invalid")
}
c.g.ShipGroups[i] = UnsendShips(*sg)
return &c.g.ShipGroups[i]
}
}