game generation process

This commit is contained in:
Ilia Denisov
2025-09-25 02:13:16 +03:00
parent 99035fd95d
commit 46066d890b
12 changed files with 206 additions and 101 deletions
+74 -31
View File
@@ -3,6 +3,7 @@ package game
import (
"fmt"
"math"
"math/rand/v2"
"github.com/google/uuid"
"github.com/iliadenisov/galaxy/pkg/generator"
@@ -15,9 +16,9 @@ type Repo interface {
}
func NewGame(r Repo, races []string) (uuid.UUID, error) {
id, err := uuid.NewRandom()
gameID, err := uuid.NewRandom()
if err != nil {
return uuid.Nil, fmt.Errorf("generate uuid: %s", err)
return uuid.Nil, fmt.Errorf("generate game uuid: %s", err)
}
m, err := generator.Generate(func(ms *generator.MapSetting) {
ms.Players = uint32(len(races))
@@ -29,7 +30,7 @@ func NewGame(r Repo, races []string) (uuid.UUID, error) {
return uuid.Nil, fmt.Errorf("generate map: wrong number of home planets: %d, expected: %d ", len(m.HomePlanets), len(races))
}
g := &game.Game{
ID: id,
ID: gameID,
Race: make([]game.Race, len(races)),
}
@@ -38,44 +39,71 @@ func NewGame(r Repo, races []string) (uuid.UUID, error) {
Height: m.Height,
Planet: make([]game.Planet, 0),
}
for hw := range races {
g.Race[hw] = game.Race{
Name: races[hw],
Votes: 1, // TODO: check with rules
VoteFor: races[hw],
var planetCount uint = 0
for i := range races {
raceID, err := uuid.NewRandom()
if err != nil {
return uuid.Nil, fmt.Errorf("generate race uuid: %s", err)
}
g.Race[i] = game.Race{
ID: raceID,
Name: races[i],
Ally: raceID,
Drive: 1,
Weapons: 1,
Shields: 1,
Cargo: 1,
}
gameMap.Planet = append(gameMap.Planet, game.Planet{
Owner: races[hw],
X: m.HomePlanets[hw].HW.Position.X,
Y: m.HomePlanets[hw].HW.Position.Y,
Size: m.HomePlanets[hw].HW.Size,
Resources: m.HomePlanets[hw].HW.Resources,
Production: game.ProductionCapital.AsType(""), // TODO: check default production
})
for dw := range m.HomePlanets[hw].DW {
gameMap.Planet = append(gameMap.Planet, game.Planet{
X: m.HomePlanets[hw].DW[dw].Position.X,
Y: m.HomePlanets[hw].DW[dw].Position.Y,
Size: m.HomePlanets[hw].DW[dw].Size,
Resources: m.HomePlanets[hw].DW[dw].Resources,
Production: game.ProductionNone.AsType(""),
})
gameMap.Planet = append(gameMap.Planet, newPlanet(
planetCount,
m.HomePlanets[i].HW.RandomName(),
raceID,
m.HomePlanets[i].HW.Position.X,
m.HomePlanets[i].HW.Position.Y,
m.HomePlanets[i].HW.Size,
m.HomePlanets[i].HW.Size, // HW's pop & ind = size
m.HomePlanets[i].HW.Size,
m.HomePlanets[i].HW.Resources,
game.ResearchDrive.AsType(""),
))
planetCount++
for dw := range m.HomePlanets[i].DW {
gameMap.Planet = append(gameMap.Planet, newPlanet(
planetCount,
m.HomePlanets[i].DW[dw].RandomName(),
raceID,
m.HomePlanets[i].DW[dw].Position.X,
m.HomePlanets[i].DW[dw].Position.Y,
m.HomePlanets[i].DW[dw].Size,
m.HomePlanets[i].DW[dw].Size, // DW's pop & ind = size
m.HomePlanets[i].DW[dw].Size,
m.HomePlanets[i].DW[dw].Resources,
game.ResearchDrive.AsType(""),
))
planetCount++
}
}
for i := range m.FreePlanets {
gameMap.Planet = append(gameMap.Planet, game.Planet{
X: m.FreePlanets[i].Position.X,
Y: m.FreePlanets[i].Position.Y,
Size: m.FreePlanets[i].Size,
Resources: m.FreePlanets[i].Resources,
Production: game.ProductionNone.AsType(""),
})
gameMap.Planet = append(gameMap.Planet, newPlanet(
planetCount,
m.FreePlanets[i].RandomName(),
uuid.Nil,
m.FreePlanets[i].Position.X,
m.FreePlanets[i].Position.Y,
m.FreePlanets[i].Size,
0,
0,
m.FreePlanets[i].Resources,
game.ProductionNone.AsType(""),
))
planetCount++
}
// TODO: check code below actually works
rand.Shuffle(len(gameMap.Planet), func(i, j int) {
gameMap.Planet[i].Number, gameMap.Planet[j].Number = gameMap.Planet[j].Number, gameMap.Planet[i].Number
})
g.Map = *gameMap
if err := r.Persist(*g); err != nil {
@@ -84,6 +112,21 @@ func NewGame(r Repo, races []string) (uuid.UUID, error) {
return g.ID, nil
}
func newPlanet(num uint, name string, owner uuid.UUID, x, y, size, pop, ind, res float64, prod game.ProductionType) game.Planet {
return game.Planet{
Name: name,
Number: num,
Owner: owner,
X: x,
Y: y,
Size: size,
Population: pop,
Industry: ind,
Resources: res,
Production: prod,
}
}
func (r Race) FlightDistance() float64 {
return r.Drive * 40
}