Files
galaxy-game/game/internal/controller/science.go
T
Ilia Denisov dc621cc715
Tests · Go / test (push) Successful in 1m58s
Tests · Integration / integration (pull_request) Successful in 1m47s
Tests · Go / test (pull_request) Successful in 2m1s
fix(game): small reconciliation fixes (science, generation, dismantle, report)
A bundle of small rules-vs-engine corrections:

- Science proportions: accept a sum that equals 1 only up to float
  rounding (was an exact != 1 comparison); the rules example is reworded
  so it is unambiguous that proportions are fractions summing to 1.
- Generation: super-big planets get a resource strictly above 0 (minimum
  0.001, was a hard 0.1); the rules table is fixed for big planets (1-10,
  not 0.1-10) and the false "0.1-20 / average 1.5" resource claim removed.
- Dismantle over a neutral planet now unloads the colonists and settles
  it (the planet becomes the race's); over a foreign planet they are
  still lost. The rules clause is clarified for own / neutral / foreign.
- Report: ship-production entries are written at the compacted report
  index (was the planet's map index, which could write past the grown
  slice and panic); the incoming-group "remaining distance" is measured
  from the group's current hyperspace position, not its origin planet
  (matching OtherGroup).
- validator: the cargo-value error now carries the cargo value, not the
  shields value.

Tests added for each behavioural fix; rules.txt updated in the same patch.
2026-05-31 09:29:07 +02:00

105 lines
3.0 KiB
Go

package controller
import (
"fmt"
"math"
"slices"
"galaxy/util"
e "galaxy/error"
"galaxy/game/internal/model/game"
"github.com/google/uuid"
)
func (c *Cache) ScienceCreate(ri int, name string, drive, weapons, shileds, cargo float64) error {
c.validateRaceIndex(ri)
n, ok := util.ValidateTypeName(name)
if !ok {
return e.NewEntityTypeNameValidationError("%q", n)
}
if sc := slices.IndexFunc(c.g.Race[ri].Sciences, func(s game.Science) bool { return s.Name == n }); sc >= 0 {
return e.NewEntityDuplicateIdentifierError("science %q", c.g.Race[ri].Sciences[sc].Name)
}
if drive < 0 {
return e.NewDriveValueError(drive)
}
if weapons < 0 {
return e.NewWeaponsValueError(weapons)
}
if shileds < 0 {
return e.NewShieldsValueError(shileds)
}
if cargo < 0 {
return e.NewCargoValueError(cargo)
}
sum := drive + weapons + shileds + cargo
// The proportions must add up to one; a small tolerance keeps inputs like
// 0.1+0.2+0.3+0.4 (which is 1 only up to float rounding) from being rejected.
if math.Abs(sum-1) > 1e-9 {
return e.NewScienceSumValuesError("D=%f W=%f S=%f C=%f sum=%f", drive, weapons, shileds, cargo, sum)
}
c.g.Race[ri].Sciences = append(c.g.Race[ri].Sciences, game.Science{
ID: uuid.New(),
Name: n,
Drive: game.Float(drive),
Weapons: game.Float(weapons),
Shields: game.Float(shileds),
Cargo: game.Float(cargo),
})
return nil
}
func (c *Cache) ScienceRemove(ri int, name string) error {
c.validateRaceIndex(ri)
sc := slices.IndexFunc(c.g.Race[ri].Sciences, func(s game.Science) bool { return s.Name == name })
if sc < 0 {
return e.NewEntityNotExistsError("science %q", name)
}
if pl := slices.IndexFunc(c.g.Map.Planet, func(p game.Planet) bool {
return p.Production.Type == game.ResearchScience &&
p.Production.SubjectID != nil &&
*p.Production.SubjectID == c.g.Race[ri].Sciences[sc].ID
}); pl >= 0 {
return e.NewDeleteSciencePlanetProductionError(c.g.Map.Planet[pl].Name)
}
c.g.Race[ri].Sciences = append(c.g.Race[ri].Sciences[:sc], c.g.Race[ri].Sciences[sc+1:]...)
return nil
}
func ResearchTech(r *game.Race, freeProduction float64, drive, weapons, shields, cargo float64) {
increment := freeProduction / 5000.
if drive > 0 {
r.Tech = r.Tech.Set(game.TechDrive, r.Tech.Value(game.TechDrive)+increment*drive)
}
if weapons > 0 {
r.Tech = r.Tech.Set(game.TechWeapons, r.Tech.Value(game.TechWeapons)+increment*weapons)
}
if shields > 0 {
r.Tech = r.Tech.Set(game.TechShields, r.Tech.Value(game.TechShields)+increment*shields)
}
if cargo > 0 {
r.Tech = r.Tech.Set(game.TechCargo, r.Tech.Value(game.TechCargo)+increment*cargo)
}
}
// Internal func
func (c *Cache) raceScience(ri int) []game.Science {
c.validateRaceIndex(ri)
return c.g.Race[ri].Sciences
}
func (c *Cache) mustScience(ri int, id uuid.UUID) *game.Science {
c.validateRaceIndex(ri)
r := &c.g.Race[ri]
i := slices.IndexFunc(r.Sciences, func(s game.Science) bool { return s.ID == id })
if i < 0 {
panic(fmt.Sprintf("science not found for race=%q id=%v", r.Name, id))
}
return &c.g.Race[ri].Sciences[i]
}