package controller import ( "fmt" "github.com/iliadenisov/galaxy/internal/model/game" ) func (c *Cache) Planet(planetNumber uint) (*game.Planet, bool) { if c.planetByPlanetNumber == nil { c.planetByPlanetNumber = make(map[uint]*game.Planet) for p := range c.g.Map.Planet { c.planetByPlanetNumber[c.g.Map.Planet[p].Number] = &c.g.Map.Planet[p] } } if v, ok := c.planetByPlanetNumber[planetNumber]; ok { return v, true } else { return nil, false } } func (c *Cache) MustPlanet(planetNumber uint) *game.Planet { if v, ok := c.Planet(planetNumber); ok { return v } else { panic(fmt.Sprintf("Planet: not found by number=%d", planetNumber)) } } // Свободный производственный потенциал (L) // промышленность * 0.75 + население * 0.25 // за вычетом затрат, расходуемых в течение хода на модернизацию кораблей func (c *Cache) PlanetProductionCapacity(planetNumber uint) float64 { p := c.MustPlanet(planetNumber) // p, err := g.PlanetByNumber(planetNumber) // if err != nil { // panic(err) // } var busyResources float64 for sg := range c.g.ShipsInUpgrade(p.Number) { busyResources += sg.StateUpgrade.Cost() } return game.PlanetProduction(p.Industry, p.Population) - busyResources } // Internal fincs func (c *Cache) putPopulation(pn uint, v float64) { c.MustPlanet(pn).Population = v } func (c *Cache) putColonists(pn uint, v float64) { c.MustPlanet(pn).Colonists = v } func (c *Cache) putMaterial(pn uint, v float64) { c.MustPlanet(pn).Material = v } func UnloadColonists(p game.Planet, v float64) game.Planet { p.Population += v * 8 if p.Population > p.Size { p.Colonists += (p.Population - p.Size) / 8 p.Population = p.Size } return p }