diff --git a/internal/controller/fleet.go b/internal/controller/fleet.go index 6bc1b13..6ac1ad4 100644 --- a/internal/controller/fleet.go +++ b/internal/controller/fleet.go @@ -17,7 +17,7 @@ type FleetState struct { State game.ShipGroupState Destination uint InSpace func() (game.InSpace, bool) - OnPlanet func() (uint, bool) + AtPlanet func() (uint, bool) } func (fs *FleetState) inSpace() bool { @@ -25,16 +25,16 @@ func (fs *FleetState) inSpace() bool { return ok } -func (fs FleetState) OnSamePlanet(other FleetState) bool { - op1, ok := fs.OnPlanet() +func (fs FleetState) AtSamePlanet(other FleetState) bool { + pn1, ok := fs.AtPlanet() if !ok { return false } - op2, ok := other.OnPlanet() + pn2, ok := other.AtPlanet() if !ok { return false } - return op1 == op2 + return pn1 == pn2 } func (c *Cache) FleetState(fleetID uuid.UUID) FleetState { @@ -43,16 +43,15 @@ func (c *Cache) FleetState(fleetID uuid.UUID) FleetState { fs := &FleetState{ State: fleetStateNil, InSpace: func() (game.InSpace, bool) { return game.InSpace{}, false }, - OnPlanet: func() (uint, bool) { return 0, false }, + AtPlanet: func() (uint, bool) { return 0, false }, } for sg := range c.FleetGroups(ri, fi) { if fs.State == fleetStateNil { fs.State = sg.State() fs.Destination = sg.Destination - if planet, ok := sg.OnPlanet(); ok { - fs.OnPlanet = func() (uint, bool) { return planet, ok } - } - if sg.StateInSpace != nil { + if pn, ok := sg.AtPlanet(); ok { + fs.AtPlanet = func() (uint, bool) { return pn, ok } + } else if sg.StateInSpace != nil { fs.InSpace = func() (game.InSpace, bool) { return *sg.StateInSpace, true } } continue @@ -63,8 +62,8 @@ func (c *Cache) FleetState(fleetID uuid.UUID) FleetState { if fs.Destination != sg.Destination { panic(fmt.Sprintf("FleetState: one or more ships in race's %q fleet %q has different destination", c.g.Race[ri].Name, c.g.Fleets[fi].Name)) } - if planet, ok := sg.OnPlanet(); ok { - if onPlanet, ok := fs.OnPlanet(); ok && onPlanet != planet { + if planet, ok := sg.AtPlanet(); ok { + if onPlanet, ok := fs.AtPlanet(); ok && onPlanet != planet { panic(fmt.Sprintf("FleetState: one or more ships in race's %q fleet %q are on different planets: %d <> %d", c.g.Race[ri].Name, c.g.Fleets[fi].Name, onPlanet, planet)) } } @@ -142,7 +141,7 @@ func (c *Cache) JoinShipGroupToFleet(ri int, fleetName string, groupIndex, quant } } else { fleetState := c.FleetState(c.g.Fleets[fi].ID) - if onPlanet, ok := fleetState.OnPlanet(); (ok && onPlanet != c.ShipGroup(sgi).Destination) || fleetState.State != game.StateInOrbit { + if onPlanet, ok := fleetState.AtPlanet(); (ok && onPlanet != c.ShipGroup(sgi).Destination) || fleetState.State != game.StateInOrbit { return e.NewShipsNotOnSamePlanetError("fleet: %s", fleetName) } } @@ -198,7 +197,7 @@ func (c *Cache) JoinFleets(ri int, fleetSourceName, fleetTargetName string) (err } stateSrc := c.FleetState(c.g.Fleets[fiSource].ID) stateDst := c.FleetState(c.g.Fleets[fiTarget].ID) - if !stateSrc.OnSamePlanet(stateDst) { + if !stateSrc.AtSamePlanet(stateDst) { return e.NewShipsNotOnSamePlanetError() } diff --git a/internal/controller/fleet_send.go b/internal/controller/fleet_send.go index dc0dc9d..3238024 100644 --- a/internal/controller/fleet_send.go +++ b/internal/controller/fleet_send.go @@ -22,7 +22,7 @@ func (c *Cache) SendFleet(ri, fi int, planetNumber uint) error { c.validateRaceIndex(ri) c.validateFleetIndex(fi) fleetState := c.FleetState(c.g.Fleets[fi].ID) - sourcePlanet, ok := fleetState.OnPlanet() + sourcePlanet, ok := fleetState.AtPlanet() if !ok || game.StateInOrbit != fleetState.State && game.StateLaunched != fleetState.State { return e.NewShipsBusyError() } diff --git a/internal/controller/ship_group_send.go b/internal/controller/ship_group_send.go index 0d5b783..3386579 100644 --- a/internal/controller/ship_group_send.go +++ b/internal/controller/ship_group_send.go @@ -27,7 +27,7 @@ func (c *Cache) SendGroup(ri int, groupIndex, planetNumber, quantity uint) error return e.NewSendShipHasNoDrivesError() } - sourcePlanet, ok := c.ShipGroup(sgi).OnPlanet() + sourcePlanet, ok := c.ShipGroup(sgi).AtPlanet() if !ok { return e.NewShipsBusyError() } diff --git a/internal/model/game/game.go b/internal/model/game/game.go index e01c29f..10ae54a 100644 --- a/internal/model/game/game.go +++ b/internal/model/game/game.go @@ -6,6 +6,7 @@ import ( "maps" "github.com/google/uuid" + "github.com/iliadenisov/galaxy/internal/number" ) type Float float64 @@ -19,7 +20,7 @@ func (f Float) Add(v float64) Float { } func (f Float) F() float64 { - return float64(f) + return number.Fixed12(float64(f)) } type TechSet map[Tech]Float diff --git a/internal/model/game/group.go b/internal/model/game/group.go index 770049e..ffa5f45 100644 --- a/internal/model/game/group.go +++ b/internal/model/game/group.go @@ -142,8 +142,7 @@ func (sg ShipGroup) State() ShipGroupState { } } -// FIXME: ambigous func, refactor -func (sg ShipGroup) OnPlanet() (uint, bool) { +func (sg ShipGroup) AtPlanet() (uint, bool) { switch sg.State() { case StateInOrbit: return sg.Destination, true @@ -171,7 +170,7 @@ func (sg ShipGroup) Equal(other ShipGroup) bool { sg.TechLevel(TechShields) == other.TechLevel(TechShields) && sg.TechLevel(TechCargo) == other.TechLevel(TechCargo) && sg.CargoType == other.CargoType && - sg.Load.F()/float64(sg.Number) == other.Load.F()/float64(other.Number) && + (sg.Load/F(float64(sg.Number))).F() == (other.Load/F(float64(other.Number))).F() && sg.State() == other.State() } diff --git a/internal/model/game/group_test.go b/internal/model/game/group_test.go index 5f789af..2f02c7c 100644 --- a/internal/model/game/group_test.go +++ b/internal/model/game/group_test.go @@ -244,6 +244,6 @@ func TestShipGroupEqual(t *testing.T) { // dirty hack to equalize loads left.Number = 5 - left.Load = game.F(right.Load.F() / float64(right.Number) * float64(left.Number)) + left.Load = game.F(float64(right.Load) / float64(right.Number) * float64(left.Number)) assert.True(t, left.Equal(right)) } diff --git a/internal/number/number.go b/internal/number/number.go index 51d5308..3a8bf6d 100644 --- a/internal/number/number.go +++ b/internal/number/number.go @@ -6,6 +6,10 @@ func Fixed3(num float64) float64 { return fixed(num, 3) } +func Fixed12(num float64) float64 { + return fixed(num, 12) +} + func fixed(num float64, precision int) float64 { output := math.Pow(10, float64(precision)) return float64(round(num*output)) / output