-
Notifications
You must be signed in to change notification settings - Fork 1
/
species.go
254 lines (224 loc) · 7.09 KB
/
species.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package bio
import (
"container/heap"
"fmt"
"math"
"github.com/Flokey82/genworldvoronoi/geo"
"github.com/Flokey82/genworldvoronoi/various"
)
func (b *Bio) genNRandomSpecies(n int) {
// TODO: Expand species from their origin until they encounter a competing
// species or they can't exist in the climate/environment.
// TODO: Ensure that we favor subtypes that are found in a particular biome.
// Like trees in forests, or grass in grasslands.
// TODO: Select prey and predators and add species if species are missing
// that are needed for the food chain / balance the ecosystem.
// TODO: Proper fitness function per species or per ecosphere.
sf := func(r int) float64 {
return 1.0
}
// TODO: Use directly competing species as seeds to maximize
// distance between species that compete for the same resources.
distSeedFunc := func() []int {
var res []int
for _, s := range b.Species {
res = append(res, s.Origin)
}
return res
}
// Place n species on the map.
for i := 0; i < n; i++ {
b.PlaceSpecies(sf, distSeedFunc)
}
// DEBUG: Print all species.
for _, s := range b.Species {
fmt.Println(s)
}
}
// PlaceSpecies places another species on the map at the region with the highest fitness score.
func (b *Bio) PlaceSpecies(sf func(int) float64, distSeedFunc func() []int) *Species {
// Score all regions, pick highest score.
var newspecies int
lastMax := math.Inf(-1)
for i, val := range b.CalcFitnessScore(sf, distSeedFunc) {
if val > lastMax {
newspecies = i
lastMax = val
}
}
tf := b.getTolerancesForRegionFunc()
return b.placeSpeciesAt(newspecies, tf)
}
// PlaceSpeciesAt places a species at the given region.
// TODO: Allow specifying the species type/subtype?
func (b *Bio) PlaceSpeciesAt(r int) *Species {
tf := b.getTolerancesForRegionFunc()
return b.placeSpeciesAt(r, tf)
}
func (b *Bio) placeSpeciesAt(r int, tf func(int) SpeciesTolerances) *Species {
// TODO: Pick species type based on biome through a weighted random array.
b.rand.Seed(b.Seed + int64(r))
s := b.newSpecies(r, SpeciesKingdoms[b.rand.Intn(len(SpeciesKingdoms))], tf)
b.Species = append(b.Species, s)
return s
}
func (b *Bio) expandSpecies() []int {
// For now, let's just do this the dumb way.
// TODO: Species with different competition hashes should be able to coexist in
// the same region?
// We might need to create a full index of all regions for each unique
// competition hash.... or, which is more wasteful, per species.
var seedPoints []int
originToSpecFit := make(map[int]func(int) float64)
for _, s := range b.Species {
seedPoints = append(seedPoints, s.Origin)
originToSpecFit[s.Origin] = b.getToleranceScoreFunc(s.SpeciesTolerances)
}
var queue geo.AscPriorityQueue
heap.Init(&queue)
outReg := make([]int, 0, 8)
// Get maxFlux and maxElev for normalizing.
_, maxFlux := minMax(b.Flux)
_, maxElev := minMax(b.Elevation)
// TODO: Move this to a generic function.
terrainWeight := func(o, u, v int) float64 {
// Don't cross from water to land and vice versa.
if (b.Elevation[u] > 0) != (b.Elevation[v] > 0) {
return -1
}
// Calculate horizontal distance.
ulat := b.LatLon[u][0]
ulon := b.LatLon[u][1]
vlat := b.LatLon[v][0]
vlon := b.LatLon[v][1]
horiz := various.Haversine(ulat, ulon, vlat, vlon) / (2 * math.Pi)
// Calculate vertical distance.
vert := (b.Elevation[v] - b.Elevation[u]) / maxElev
if vert > 0 {
vert /= 10
}
diff := 1 + 0.25*math.Pow(vert/horiz, 2)
// NOTE: Flux should only apply to animals since plants and fungi
// don't need to worry about drowning.
diff += 100 * math.Sqrt(b.Flux[u]/maxFlux)
if b.Elevation[u] <= 0 {
diff = 100
}
return horiz * diff
}
weight := func(o, u, v int) float64 {
// Call species specific fitness function.
sFit := originToSpecFit[o](v)
if sFit < 0 {
return -1
}
// Call terrain specific fitness function.
tFit := terrainWeight(o, u, v)
if tFit < 0 {
return -1
}
return tFit * sFit
}
// 'terr' will hold a mapping of region to species.
// The territory ID is the region number of the species origin.
terr := initRegionSlice(b.SphereMesh.NumRegions)
for i := 0; i < len(seedPoints); i++ {
terr[seedPoints[i]] = seedPoints[i]
for _, v := range b.SphereMesh.R_circulate_r(outReg, seedPoints[i]) {
newdist := weight(seedPoints[i], seedPoints[i], v)
if newdist < 0 {
continue
}
heap.Push(&queue, &geo.QueueEntry{
Score: newdist,
Origin: seedPoints[i],
Destination: v,
})
}
}
// Extend territories until the queue is empty.
for queue.Len() > 0 {
u := heap.Pop(&queue).(*geo.QueueEntry)
if terr[u.Destination] >= 0 {
continue
}
terr[u.Destination] = u.Origin
for _, v := range b.SphereMesh.R_circulate_r(outReg, u.Destination) {
if terr[v] >= 0 {
continue
}
newdist := weight(u.Origin, u.Destination, v)
if newdist < 0 {
continue
}
heap.Push(&queue, &geo.QueueEntry{
Score: u.Score + newdist,
Origin: u.Origin,
Destination: v,
})
}
}
return terr
}
func (b *Bio) newSpecies(r int, t SpeciesKingdom, tf func(int) SpeciesTolerances) *Species {
// TODO: Get culture and language from the region and use it to generate the name.
s := &Species{
Origin: r,
SpeciesProperties: SpeciesProperties{
Kingdom: t,
Size: SpeciesSizes[b.rand.Intn(len(SpeciesSizes))],
},
SpeciesTolerances: tf(r),
}
// Pick subtype and mode of locomotion.
if s.Ecosphere.IsWater() {
subTypes := speciesKingdomToFamiliesWater[s.Kingdom]
s.Family = subTypes[b.rand.Intn(len(subTypes))]
s.Locomotion = s.Family.Locomotion()
// There is further a remote chance that we have another way of locomotion.
if b.rand.Float64() < 0.01 {
s.Locomotion |= LocomotionTypesWater[b.rand.Intn(len(LocomotionTypesWater))]
}
} else {
subTypes := speciesKingdomToFamiliesLand[s.Kingdom]
s.Family = subTypes[b.rand.Intn(len(subTypes))]
s.Locomotion = s.Family.Locomotion()
// There is further a remote chance that we have another way of locomotion.
if b.rand.Float64() < 0.02 {
s.Locomotion |= LocomotionTypesLand[b.rand.Intn(len(LocomotionTypesLand))]
}
}
// Pick a name.
s.Name = b.genSpeciesNameByFamily(s.Family)
// Pick a random type of prey.
digestiveSystems := s.Kingdom.DigestiveSystems()
s.Digestion = digestiveSystems[b.rand.Intn(len(digestiveSystems))]
return s
}
func (b *Bio) getSpeciesScores(s *Species) []float64 {
scores := make([]float64, b.SphereMesh.NumRegions)
tsf := b.getToleranceScoreFunc(s.SpeciesTolerances)
chunkProcessor := func(start, end int) {
for i := start; i < end; i++ {
scores[i] = tsf(i)
}
}
useGoRoutines := true
if useGoRoutines {
// Use goroutines to process the chunks.
various.KickOffChunkWorkers(b.SphereMesh.NumRegions, chunkProcessor)
} else {
// Use the main thread to process the chunks.
chunkProcessor(0, b.SphereMesh.NumRegions)
}
return scores
}
type Species struct {
Name string
Origin int // The region where the species originated, acts as a seed.
SpeciesProperties
SpeciesTolerances
}
func (s *Species) String() string {
return fmt.Sprintf("%s (%s) %s\n", s.Name, s.SpeciesProperties.String(), s.SpeciesTolerances.String())
}