-
Notifications
You must be signed in to change notification settings - Fork 3
/
terraform.go
410 lines (352 loc) · 13.4 KB
/
terraform.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package cs
import "math"
// The Terraformer interface handles terraforming planets
type Terraformer interface {
PermaformOneStep(planet *Planet, player *Player, habType HabType) TerraformResult
TerraformOneStep(planet *Planet, player *Player, terraformer *Player, reverse bool) TerraformResult
GetBestTerraform(planet *Planet, player *Player, terraformer *Player) *HabType
getTerraformAmount(hab Hab, baseHab Hab, player, terraformer *Player) Hab
getMinTerraformAmount(hab Hab, baseHab Hab, player *Player, terraformer *Player) Hab
}
type TerraformResult struct {
Type HabType
Direction int
}
func (t TerraformResult) Terraformed() bool {
return t.Direction != 0
}
type terraform struct {
}
func NewTerraformer() Terraformer {
return &terraform{}
}
// getTerraformAbility returns the terraform ability of a player taking into account total terraform and hab terraform
func (t *terraform) getTerraformAbility(player *Player) Hab {
bestTotalTerraform := player.Spec.Terraform[TerraformHabTypeAll]
totalTerraformAbility := 0
if bestTotalTerraform != nil {
totalTerraformAbility = bestTotalTerraform.Ability
}
terraformAbility := Hab{totalTerraformAbility, totalTerraformAbility, totalTerraformAbility}
for _, habType := range HabTypes {
// get the two ways we can terraform
bestHabTerraform := player.Spec.Terraform[FromHabType(habType)]
// find out which terraform tech has the greater terraform ability
ability := totalTerraformAbility
if bestHabTerraform != nil {
ability = MaxInt(ability, bestHabTerraform.Ability)
terraformAbility.Set(habType, ability)
}
if ability == 0 {
continue
}
}
return terraformAbility
}
// getTerraformAmount returns the total amount we can terraform this planet
func (t *terraform) getTerraformAmount(hab Hab, baseHab Hab, player, terraformer *Player) Hab {
terraformAmount := Hab{}
if player == nil {
// can't terraform, return an empty Hab
return terraformAmount
}
// default the terraformer to the player
if terraformer == nil {
terraformer = player
}
terraformAbility := t.getTerraformAbility(terraformer)
enemy := terraformer.IsEnemy(player.Num)
habCenter := player.Race.HabCenter()
for _, habType := range HabTypes {
if player.Race.IsImmune(habType) {
continue
}
ability := terraformAbility.Get(habType)
if ability == 0 {
continue
}
// The distance from the starting hab of this planet
fromIdealBase := habCenter.Get(habType) - baseHab.Get(habType)
// the distance from the current hab of this planet
fromIdeal := habCenter.Get(habType) - hab.Get(habType)
// if we have any left to terraform
if fromIdeal > 0 {
// i.e. our ideal is 50 and the planet hab is 47
if enemy {
alreadyTerraformed := AbsInt(fromIdealBase - fromIdeal)
terraformAmount.Set(habType, -(ability - alreadyTerraformed))
} else {
// we can either terrform up to our full ability, or however much
// we have left to terraform on this
alreadyTerraformed := fromIdealBase - fromIdeal
terraformAmount.Set(habType, MinInt(ability-alreadyTerraformed, fromIdeal))
}
} else if fromIdeal < 0 {
if enemy {
alreadyTerraformed := AbsInt(fromIdealBase - fromIdeal)
terraformAmount.Set(habType, ability-alreadyTerraformed)
} else {
// i.e. our ideal is 50 and the planet hab is 53
alreadyTerraformed := fromIdeal - fromIdealBase
terraformAmount.Set(habType, MaxInt(-(ability-alreadyTerraformed), fromIdeal))
}
} else if enemy {
// the terrformer is enemies with the player, terraform away from ideal
alreadyTerraformed := AbsInt(fromIdealBase - fromIdeal)
terraformAbility.Set(habType, ability-alreadyTerraformed)
}
}
return terraformAmount
}
// getMinTerraformAmount gets the minimum amount we need to terraform this planet to make it habitable (if we can terraform it at all)
func (t *terraform) getMinTerraformAmount(hab Hab, baseHab Hab, player *Player, terraformer *Player) Hab {
terraformAmount := Hab{}
if player == nil {
// can't terraform, return an empty Hab
return terraformAmount
}
// default the terraformer to the player
if terraformer == nil {
terraformer = player
}
// get how much this player can terraform each hab
terraformAbility := t.getTerraformAbility(terraformer)
habCenter := player.Race.HabCenter()
for _, habType := range HabTypes {
if player.Race.IsImmune(habType) {
continue
}
ability := terraformAbility.Get(habType)
if ability == 0 {
continue
}
// the distance from the current hab of this planet to our minimum hab threshold
// If this is positive, it means we need to terraform a certain percent to get it in range
fromHabitableDistance := 0
planetHabValue := hab.Get(habType)
playerHabIdeal := habCenter.Get(habType)
if planetHabValue > playerHabIdeal {
// this planet is higher that we want, check the upper bound distance
// if the player's high temp is 85, and this planet is 83, we are already in range
// and don't need to min-terraform. If the planet is 87, we need to drop it 2 to be in range.
fromHabitableDistance = planetHabValue - player.Race.HabHigh.Get(habType)
} else {
// this planet is lower than we want, check the lower bound distance
// if the player's low temp is 15, and this planet is 17, we are already in range
// and don't need to min-terraform. If the planet is 13, we need to increase it 2 to be in range.
fromHabitableDistance = player.Race.HabLow.Get(habType) - planetHabValue
}
// if we are already in range, set this to 0 because we don't want to terraform anymore
if fromHabitableDistance < 0 {
fromHabitableDistance = 0
}
// if we have any left to terraform
if fromHabitableDistance > 0 {
// the distance from the current hab of this planet
fromIdeal := playerHabIdeal - planetHabValue
fromIdealDistance := AbsInt(fromIdeal)
// The distance from the starting hab of this planet
fromIdealBaseDistance := AbsInt(playerHabIdeal - baseHab.Get(habType))
// we can either terrform up to our full ability, or however much
// we have left to terraform on this
alreadyTerraformed := fromIdealBaseDistance - fromIdealDistance
terraformAmountPossible := MinInt(ability-alreadyTerraformed, fromIdealDistance)
// if we are in range for this hab type, we won't terraform at
// if we are in range for this hab type, we won't terraform at all, otherwise return the max possible terraforming
// left.
terraformAmount.Set(habType, MinInt(fromHabitableDistance, terraformAmountPossible))
}
}
return terraformAmount
}
// Get the best hab to terraform (the one with the most distance away from ideal that we can still terraform)
func (t *terraform) GetBestTerraform(planet *Planet, player *Player, terraformer *Player) *HabType {
if player == nil || planet == nil {
return nil
}
// if we can terraform any, return true
var bestHabType *HabType
var direction int
isRed := false
redness := math.MaxInt
greenness := math.MinInt
greatest := math.MinInt
// default the terraformer to the player
if terraformer == nil {
terraformer = player
}
// get how much this player can terraform each hab
terraformAbility := t.getTerraformAbility(terraformer)
habCenter := player.Race.HabCenter()
for _, habType := range HabTypes {
if player.Race.IsImmune(habType) {
continue
}
ability := terraformAbility.Get(habType)
if ability == 0 {
continue
}
playerHabIdeal := habCenter.Get(habType)
// figure out what our hab is without any instaforming
// instaforming doesn't count as "terraforming" in that the planet doesn't change, it's just more habitable
// for the CA populace
habWithoutInstaforming := planet.BaseHab.Add(planet.TerraformedAmount)
// the distance from the current hab of this planet
fromIdeal := playerHabIdeal - habWithoutInstaforming.Get(habType)
fromIdealDist := AbsInt(fromIdeal)
if fromIdeal > 0 {
// for example, the planet has Grav 49, but our player wants Grav 50
if ability <= planet.TerraformedAmount.Get(habType) {
continue
}
if player.Race.HabLow.Get(habType) > planet.Hab.Get(habType) {
isRed = true
// For red planets we want the smallest redness
// * Terraforming only improves reds with less than 15%
// * If all habs are over 15% then smallest is closest to being improved in the future
if player.Race.HabLow.Get(habType)-planet.Hab.Get(habType) < redness {
redness = player.Race.HabLow.Get(habType) - planet.Hab.Get(habType)
newBest := habType
bestHabType = &newBest
}
continue
}
direction = 1
} else if fromIdeal < 0 {
if ability <= -planet.TerraformedAmount.Get(habType) {
continue
}
if player.Race.HabHigh.Get(habType) < planet.Hab.Get(habType) {
isRed = true
if planet.Hab.Get(habType)-player.Race.HabHigh.Get(habType) < redness {
redness = planet.Hab.Get(habType) - player.Race.HabHigh.Get(habType)
newBest := habType
bestHabType = &newBest
}
continue
}
direction = -1
} else {
// terraforming complete for this habType
continue
}
if !isRed {
// Test every possible improvement and select the highest habitability
// for cases where the highest hab is equivalent, pick the largest distance
newHab := planet.Hab
newHab.Set(habType, planet.Hab.Get(habType)+direction)
habitability := player.Race.GetPlanetHabitability(newHab)
if habitability > greenness {
greenness = habitability
greatest = fromIdealDist
newBest := habType
bestHabType = &newBest
} else if habitability == greenness && fromIdealDist > greatest {
greenness = habitability
greatest = fromIdealDist
newBest := habType
bestHabType = &newBest
}
}
}
return bestHabType
}
// getBestUnterraform returns the worst habitable parameter to terraform on the planet, or nil if no terraforming can be done.
func (t *terraform) getBestUnterraform(planet *Planet, player, terraformer *Player) *HabType {
if player == nil || planet == nil {
return nil
}
// if we can terraform any, return true
farthestAmount := math.MinInt
var farthest *HabType
// default the terraformer to the player
if terraformer == nil {
terraformer = player
}
// get how much this player can terraform each hab
terraformAbility := t.getTerraformAbility(terraformer)
habCenter := player.Race.HabCenter()
for _, habType := range HabTypes {
if player.Race.IsImmune(habType) {
continue
}
fromIdeal := habCenter.Get(habType) - planet.Hab.Get(habType)
terraformedAlready := AbsInt(planet.TerraformedAmount.Get(habType))
// if we can terraform this at all
if terraformedAlready < terraformAbility.Get(habType) {
// pick the farthest from ideal
if AbsInt(fromIdeal) > farthestAmount {
farthestAmount = AbsInt(fromIdeal)
newFarthest := habType
farthest = &newFarthest
}
}
}
return farthest
}
// TerraformOneStep terraforms the planet one step in whatever the best option is
// If reverse is true, this will terraform in the opposite direction making the planet less habitable
func (t *terraform) TerraformOneStep(planet *Planet, player *Player, terraformer *Player, reverse bool) TerraformResult {
var bestHab *HabType
if !reverse || terraformer == nil {
bestHab = t.GetBestTerraform(planet, player, terraformer)
} else {
bestHab = t.getBestUnterraform(planet, player, terraformer)
}
if bestHab != nil {
habType := *bestHab
fromIdeal := player.Race.Spec.HabCenter.Get(habType) - planet.Hab.Get(habType)
if fromIdeal > 0 {
// for example, the planet has Grav 49, but our player wants Grav 50
direction := 1
if reverse {
direction = -1
}
planet.Hab.Set(habType, planet.Hab.Get(habType)+direction)
planet.TerraformedAmount.Set(habType, planet.TerraformedAmount.Get(habType)+direction)
return TerraformResult{Type: habType, Direction: direction}
} else if fromIdeal < 0 {
// for example, the planet has Grav 51, but our player wants Grav 50
direction := -1
if reverse {
direction = 1
}
planet.Hab.Set(habType, planet.Hab.Get(habType)+direction)
planet.TerraformedAmount.Set(habType, planet.TerraformedAmount.Get(habType)+direction)
return TerraformResult{Type: habType, Direction: direction}
} else if fromIdeal == 0 && reverse {
// this is a perfect planet, just make it hotter, higher rad, etc
direction := 1
planet.Hab.Set(habType, planet.Hab.Get(habType)+direction)
planet.TerraformedAmount.Set(habType, planet.TerraformedAmount.Get(habType)+direction)
return TerraformResult{Type: habType, Direction: direction}
}
}
return TerraformResult{}
}
// Permanently terraform this planet one step for a random habtype
// This adjusts the BaseHab as well as the hab
func (t *terraform) PermaformOneStep(planet *Planet, player *Player, habType HabType) TerraformResult {
direction := 0
habCenter := player.Race.HabCenter()
playerHabIdeal := habCenter.Get(habType)
baseHab := planet.BaseHab.Get(habType)
fromIdealBaseHab := playerHabIdeal - baseHab
if fromIdealBaseHab > 0 {
// for example, the planet has Grav 49, but our player wants Grav 50
planet.BaseHab.Set(habType, baseHab+1)
direction = 1
} else if fromIdealBaseHab < 0 {
// for example, the planet has Grav 51, but our player wants Grav 50
planet.BaseHab.Set(habType, baseHab-1)
direction = -1
}
// if this means our terraformed hab is beter as well, improve it
fromIdealHab := playerHabIdeal - planet.Hab.Get(habType)
if fromIdealHab > 0 {
planet.Hab.Set(habType, planet.Hab.Get(habType)+1)
} else if fromIdealHab < 0 {
planet.Hab.Set(habType, planet.Hab.Get(habType)-1)
}
return TerraformResult{Type: habType, Direction: direction}
}