-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem.go
78 lines (62 loc) · 1.72 KB
/
system.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package sps
import (
"errors"
"github.com/google/uuid"
"math/rand"
)
type PlanetSystemInterface interface {
Generate() (system *PlanetarySystem, err error)
}
type PlanetarySystem struct {
ID uuid.UUID `json:"id"`
SystemStructure `json:"system_structure"`
Planets []Planet `json:"planets"`
}
type SystemStructure struct {
MinDensity int32 `json:"min_density"`
DensityFactor int32 `json:"density"`
DeviationFactor float64 `json:"deviation"`
Size float64 `json:"size"`
Seed *int64 `json:"seed"`
}
func NewPlanetarySystem(structure *SystemStructure) (PlanetSystemInterface, error) {
if err := structure.VerifyStructure(); err != nil {
return nil, err
}
return &PlanetarySystem{
ID: uuid.New(),
SystemStructure: *structure,
}, nil
}
func (s *PlanetarySystem) Generate() (system *PlanetarySystem, err error) {
r := rand.New(rand.NewSource(*s.SystemStructure.Seed))
chunkDensity := r.Int31n(
s.SystemStructure.MinDensity*s.SystemStructure.DensityFactor-s.SystemStructure.MinDensity+1) +
s.SystemStructure.MinDensity
for i := 0; i < int(chunkDensity); i++ {
var planet *Planet
if planet, err = NewPlanet().Generate(s, i); err != nil {
return nil, err
}
s.Planets = append(s.Planets, *planet)
}
return s, nil
}
func (s *SystemStructure) VerifyStructure() error {
if s.MinDensity < 0 {
return errors.New("invalid min density")
}
if s.DensityFactor < 0 {
return errors.New("invalid density factor")
}
if s.DeviationFactor < 0 {
return errors.New("invalid deviation factor")
}
if s.Size < 0 {
return errors.New("invalid size")
}
if s.Seed == nil {
return errors.New("seed is required to generate the system")
}
return nil
}