85 lines
2.3 KiB
Go
85 lines
2.3 KiB
Go
package controller
|
|
|
|
import (
|
|
e "github.com/iliadenisov/galaxy/internal/error"
|
|
"github.com/iliadenisov/galaxy/internal/model/game"
|
|
"github.com/iliadenisov/galaxy/internal/util"
|
|
)
|
|
|
|
func (c *Controller) SetRoute(raceName, loadType string, origin, destination uint) error {
|
|
ri, err := c.Cache.raceIndex(raceName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rt, ok := game.RouteTypeSet[loadType]
|
|
if !ok {
|
|
return e.NewCargoTypeInvalidError(loadType)
|
|
}
|
|
return c.Cache.SetRoute(ri, rt, origin, destination)
|
|
}
|
|
|
|
func (c *Cache) SetRoute(ri int, rt game.RouteType, origin, destination uint) error {
|
|
c.validateRaceIndex(ri)
|
|
p1, ok := c.Planet(origin)
|
|
if !ok {
|
|
return e.NewEntityNotExistsError("origin planet #%d", origin)
|
|
}
|
|
if p1.Owner != c.g.Race[ri].ID {
|
|
return e.NewEntityNotOwnedError("planet #%d", origin)
|
|
}
|
|
p2, ok := c.Planet(destination)
|
|
if !ok {
|
|
return e.NewEntityNotExistsError("destination planet #%d", destination)
|
|
}
|
|
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)
|
|
}
|
|
|
|
c.SetPlanetRoute(rt, origin, destination)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *Controller) RemoveRoute(raceName, loadType string, origin uint) error {
|
|
ri, err := c.Cache.raceIndex(raceName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
rt, ok := game.RouteTypeSet[loadType]
|
|
if !ok {
|
|
return e.NewCargoTypeInvalidError(loadType)
|
|
}
|
|
return c.Cache.RemoveRoute(ri, rt, origin)
|
|
}
|
|
|
|
func (c *Cache) RemoveRoute(ri int, rt game.RouteType, origin uint) error {
|
|
c.validateRaceIndex(ri)
|
|
p1, ok := c.Planet(origin)
|
|
if !ok {
|
|
return e.NewEntityNotExistsError("origin planet #%d", origin)
|
|
}
|
|
if p1.Owner != c.g.Race[ri].ID {
|
|
return e.NewEntityNotOwnedError("planet #%d", origin)
|
|
}
|
|
|
|
c.RemovePlanetRoute(rt, origin)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *Cache) SetPlanetRoute(rt game.RouteType, origin, destination uint) {
|
|
pi := c.MustPlanetIndex(origin)
|
|
if c.g.Map.Planet[pi].Route == nil {
|
|
c.g.Map.Planet[pi].Route = make(map[game.RouteType]uint)
|
|
}
|
|
c.g.Map.Planet[pi].Route[rt] = destination
|
|
}
|
|
|
|
func (c *Cache) RemovePlanetRoute(rt game.RouteType, origin uint) {
|
|
pi := c.MustPlanetIndex(origin)
|
|
if c.g.Map.Planet[pi].Route != nil {
|
|
delete(c.g.Map.Planet[pi].Route, rt)
|
|
}
|
|
}
|