-
Notifications
You must be signed in to change notification settings - Fork 3
/
shipdesign.go
860 lines (784 loc) · 32.3 KB
/
shipdesign.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
package cs
import (
"fmt"
"math"
"strings"
"slices"
)
// Fleets are made up of ships, and each ship has a design. Players start with designs created
// during universe generation, and they can add new designs in the UI.
// Deleting a design deletes all fleets associated with it.
type ShipDesign struct {
GameDBObject
Num int `json:"num,omitempty"`
PlayerNum int `json:"playerNum"`
OriginalPlayerNum int `json:"originalPlayerNum"`
Name string `json:"name"`
Dirty bool `json:"-"`
Version int `json:"version"`
Hull string `json:"hull"`
HullSetNumber int `json:"hullSetNumber"`
CannotDelete bool `json:"cannotDelete,omitempty"`
Slots []ShipDesignSlot `json:"slots"`
Purpose ShipDesignPurpose `json:"purpose,omitempty"`
Spec ShipDesignSpec `json:"spec"`
Delete bool // used by the AI to mark a design for deletion
}
type ShipDesignSlot struct {
HullComponent string `json:"hullComponent"`
HullSlotIndex int `json:"hullSlotIndex"`
Quantity int `json:"quantity"`
}
type ShipDesignSpec struct {
HullType TechHullType `json:"hullType,omitempty"`
Engine Engine `json:"engine,omitempty"`
NumEngines int `json:"numEngines,omitempty"`
Cost Cost `json:"cost,omitempty"`
TechLevel TechLevel `json:"techLevel,omitempty"`
Mass int `json:"mass,omitempty"`
Armor int `json:"armor,omitempty"`
FuelCapacity int `json:"fuelCapacity,omitempty"`
FuelGeneration int `json:"fuelGeneration,omitempty"`
CargoCapacity int `json:"cargoCapacity,omitempty"`
CloakUnits int `json:"cloakUnits,omitempty"`
ScanRange int `json:"scanRange,omitempty"`
ScanRangePen int `json:"scanRangePen,omitempty"`
InnateScanRangePenFactor float64 `json:"innateScanRangePenFactor,omitempty"`
RepairBonus float64 `json:"repairBonus,omitempty"`
TorpedoInaccuracyFactor float64 `json:"torpedoInaccuracyFactor,omitempty"`
TorpedoJamming float64 `json:"torpedoJamming,omitempty"`
BeamBonus float64 `json:"beamBonus,omitempty"`
BeamDefense float64 `json:"beamDefense,omitempty"`
Initiative int `json:"initiative,omitempty"`
Movement int `json:"movement,omitempty"`
ReduceMovement int `json:"reduceMovement,omitempty"`
PowerRating int `json:"powerRating,omitempty"`
Bomber bool `json:"bomber,omitempty"`
Bombs []Bomb `json:"bombs,omitempty"`
SmartBombs []Bomb `json:"smartBombs,omitempty"`
RetroBombs []Bomb `json:"retroBombs,omitempty"`
Scanner bool `json:"scanner,omitempty"`
ImmuneToOwnDetonation bool `json:"immuneToOwnDetonation,omitempty"`
MineLayingRateByMineType map[MineFieldType]int `json:"mineLayingRateByMineType,omitempty"`
Shields int `json:"shields,omitempty"`
Colonizer bool `json:"colonizer,omitempty"`
Starbase bool `json:"starbase,omitempty"`
CanLayMines bool `json:"canLayMines,omitempty"`
SpaceDock int `json:"spaceDock,omitempty"`
MiningRate int `json:"miningRate,omitempty"`
TerraformRate int `json:"terraformRate,omitempty"`
MineSweep int `json:"mineSweep,omitempty"`
CloakPercent int `json:"cloakPercent,omitempty"`
CloakPercentFullCargo int `json:"cloakPercentFullCargo,omitempty"`
ReduceCloaking float64 `json:"reduceCloaking,omitempty"`
CanStealFleetCargo bool `json:"canStealFleetCargo,omitempty"`
CanStealPlanetCargo bool `json:"canStealPlanetCargo,omitempty"`
OrbitalConstructionModule bool `json:"orbitalConstructionModule,omitempty"`
HasWeapons bool `json:"hasWeapons,omitempty"`
WeaponSlots []ShipDesignSlot `json:"weaponSlots,omitempty"`
Stargate string `json:"stargate,omitempty"`
SafeHullMass int `json:"safeHullMass,omitempty"`
SafeRange int `json:"safeRange,omitempty"`
MaxHullMass int `json:"maxHullMass,omitempty"`
MaxRange int `json:"maxRange,omitempty"`
MassDriver string `json:"massDriver,omitempty"`
SafePacketSpeed int `json:"safePacketSpeed,omitempty"`
BasePacketSpeed int `json:"basePacketSpeed,omitempty"`
AdditionalMassDrivers int `json:"additionalMassDrivers,omitempty"`
MaxPopulation int `json:"maxPopulation,omitempty"`
Radiating bool `json:"radiating,omitempty"`
NumInstances int `json:"numInstances,omitempty"`
NumBuilt int `json:"numBuilt,omitempty"`
EstimatedRange int `json:"estimatedRange,omitempty"`
EstimatedRangeFull int `json:"estimatedRangeFull,omitempty"`
}
type MineLayingRateByMineType struct {
}
type ShipDesignPurpose string
const (
ShipDesignPurposeNone ShipDesignPurpose = ""
ShipDesignPurposeScout ShipDesignPurpose = "Scout"
ShipDesignPurposeColonizer ShipDesignPurpose = "Colonizer"
ShipDesignPurposeBomber ShipDesignPurpose = "Bomber"
ShipDesignPurposeStructureBomber ShipDesignPurpose = "StructureBomber"
ShipDesignPurposeSmartBomber ShipDesignPurpose = "SmartBomber"
ShipDesignPurposeFighter ShipDesignPurpose = "Fighter"
ShipDesignPurposeFighterScout ShipDesignPurpose = "FighterScout"
ShipDesignPurposeCapitalShip ShipDesignPurpose = "CapitalShip"
ShipDesignPurposeFreighter ShipDesignPurpose = "Freighter"
ShipDesignPurposeColonistFreighter ShipDesignPurpose = "ColonistFreighter"
ShipDesignPurposeFuelFreighter ShipDesignPurpose = "FuelFreighter"
ShipDesignPurposeMultiPurposeFreighter ShipDesignPurpose = "MultiPurposeFreighter"
ShipDesignPurposeArmedFreighter ShipDesignPurpose = "ArmedFreighter"
ShipDesignPurposeMiner ShipDesignPurpose = "Miner"
ShipDesignPurposeTerraformer ShipDesignPurpose = "Terraformer"
ShipDesignPurposeDamageMineLayer ShipDesignPurpose = "DamageMineLayer"
ShipDesignPurposeSpeedMineLayer ShipDesignPurpose = "SpeedMineLayer"
ShipDesignPurposeStarbase ShipDesignPurpose = "Starbase"
ShipDesignPurposeFuelDepot ShipDesignPurpose = "FuelDepot"
ShipDesignPurposeStarbaseQuarter ShipDesignPurpose = "StarbaseQuarter"
ShipDesignPurposeStarbaseHalf ShipDesignPurpose = "StarbaseHalf"
ShipDesignPurposePacketThrower ShipDesignPurpose = "PacketThrower"
ShipDesignPurposeStargater ShipDesignPurpose = "Stargater"
ShipDesignPurposeFort ShipDesignPurpose = "Fort"
ShipDesignPurposeStarterColony ShipDesignPurpose = "StarterColony"
)
func NewShipDesign(player *Player, num int) *ShipDesign {
return &ShipDesign{PlayerNum: player.Num, Num: num, Dirty: true, Slots: []ShipDesignSlot{}}
}
func (sd *ShipDesign) WithName(name string) *ShipDesign {
sd.Name = name
return sd
}
func (sd *ShipDesign) WithHull(hull string) *ShipDesign {
sd.Hull = hull
return sd
}
func (sd *ShipDesign) WithSlots(slots []ShipDesignSlot) *ShipDesign {
sd.Slots = slots
return sd
}
func (sd *ShipDesign) WithPurpose(purpose ShipDesignPurpose) *ShipDesign {
sd.Purpose = purpose
return sd
}
func (sd *ShipDesign) WithHullSetNumber(num int) *ShipDesign {
sd.HullSetNumber = num
return sd
}
// Compute the spec for this ShipDesign. This function is mostly for universe generation and tests
func (sd *ShipDesign) WithSpec(rules *Rules, player *Player) *ShipDesign {
sd.Spec = ComputeShipDesignSpec(rules, player.TechLevels, player.Race.Spec, sd)
return sd
}
func (sd *ShipDesign) MarkDirty() {
sd.Dirty = true
}
// validate that this ship design is available to the player
func (sd *ShipDesign) Validate(rules *Rules, player *Player) error {
if strings.TrimSpace(sd.Name) == "" {
return fmt.Errorf("design has no name")
}
hull := rules.techs.GetHull(sd.Hull)
if hull == nil {
return fmt.Errorf("hull %s not found", sd.Hull)
}
for _, slot := range sd.Slots {
if slot.HullSlotIndex < 1 || slot.HullSlotIndex > len(hull.Slots) {
return fmt.Errorf("hull component index %d out of range", slot.HullSlotIndex)
}
hullSlot := hull.Slots[slot.HullSlotIndex-1]
if slot.Quantity < 0 || slot.Quantity > hullSlot.Capacity {
return fmt.Errorf("hull component quantity %d out of range", slot.Quantity)
}
if hullSlot.Required && hullSlot.Capacity != slot.Quantity {
return fmt.Errorf("hull component required but quantity %d != capacity %d", slot.Quantity, hullSlot.Capacity)
}
// if we have a hull component, check it
if slot.HullComponent != "" {
hc := rules.techs.GetHullComponent(slot.HullComponent)
if hc == nil {
return fmt.Errorf("hull component %s not found", slot.HullComponent)
}
if hullSlot.Type&hc.HullSlotType == 0 {
return fmt.Errorf("hull component %s won't work in slot %v", hc.Name, hullSlot.Type)
}
if len(hc.Requirements.HullsAllowed) > 0 && slices.IndexFunc(hc.Requirements.HullsAllowed, func(h string) bool { return hull.Name == h }) == -1 {
return fmt.Errorf("hull component %s is not mountable on the %s hull", hc.Name, sd.Hull)
}
if len(hc.Requirements.HullsDenied) > 0 && slices.IndexFunc(hc.Requirements.HullsDenied, func(h string) bool { return hull.Name == h }) != -1 {
return fmt.Errorf("hull component %s is not mountable on the %s hull", hc.Name, sd.Hull)
}
if !player.HasTech(&hc.Tech) {
return fmt.Errorf("hull component %s is not available to player", hc.Name)
}
}
}
for i, hullSlot := range hull.Slots {
if hullSlot.Required {
found := false
for _, slot := range sd.Slots {
if slot.HullSlotIndex-1 == i && slot.Quantity == hullSlot.Capacity {
found = true
break
}
}
if !found {
return fmt.Errorf("%d %s required", hullSlot.Capacity, hullSlot.Type.String())
}
}
}
return nil
}
// compare two ship design's slots and return true if they are equal
func (d *ShipDesign) SlotsEqual(other *ShipDesign) bool {
if len(d.Slots) != len(other.Slots) {
return false
}
for i, v := range d.Slots {
if v != other.Slots[i] {
return false
}
}
return true
}
func ComputeShipDesignSpec(rules *Rules, techLevels TechLevel, raceSpec RaceSpec, design *ShipDesign) ShipDesignSpec {
hull := rules.techs.GetHull(design.Hull)
spec := ShipDesignSpec{
Mass: hull.Mass,
Armor: hull.Armor,
FuelCapacity: hull.FuelCapacity,
FuelGeneration: hull.FuelGeneration,
Cost: hull.GetPlayerCost(techLevels, raceSpec.MiniaturizationSpec),
TechLevel: hull.Requirements.TechLevel,
CargoCapacity: hull.CargoCapacity,
CloakUnits: raceSpec.BuiltInCloakUnits,
Initiative: hull.Initiative,
TorpedoInaccuracyFactor: 1,
ImmuneToOwnDetonation: hull.ImmuneToOwnDetonation,
RepairBonus: hull.RepairBonus,
ScanRange: NoScanner,
ScanRangePen: NoScanner,
SpaceDock: hull.SpaceDock,
Starbase: hull.Starbase,
MaxPopulation: hull.MaxPopulation,
HullType: hull.Type,
InnateScanRangePenFactor: hull.InnateScanRangePenFactor,
}
torpedoJammingFactor := 1.0
numTachyonDetectors := 0
// rating calcs
beamPower := 0
torpedoPower := 0
bombsPower := 0
for _, slot := range design.Slots {
if slot.Quantity > 0 {
component := rules.techs.GetHullComponent(slot.HullComponent)
hullSlot := hull.Slots[slot.HullSlotIndex-1]
// record engine details
if hullSlot.Type == HullSlotTypeEngine {
engine := rules.techs.GetEngine(slot.HullComponent)
spec.Engine = engine.Engine
spec.NumEngines = slot.Quantity
}
if component.Category == TechCategoryBeamWeapon && component.Power > 0 && (component.Range+hull.RangeBonus) > 0 {
// mine sweep is power * (range)^2
gattlingMultiplier := 1
if component.Gattling {
// gattlings are 4x more mine-sweepery (all gatlings have range of 2)
// lol, 4x, get it?
gattlingMultiplier = component.Range * component.Range
}
spec.MineSweep += slot.Quantity * component.Power * ((component.Range + hull.RangeBonus) * component.Range) * gattlingMultiplier
}
spec.Cost = spec.Cost.Add(component.Tech.GetPlayerCost(techLevels, raceSpec.MiniaturizationSpec).MultiplyInt(slot.Quantity))
spec.TechLevel = spec.TechLevel.Max(component.Requirements.TechLevel)
spec.Mass += component.Mass * slot.Quantity
spec.Armor += component.Armor * slot.Quantity
spec.Shields += component.Shield * slot.Quantity
spec.CargoCapacity += component.CargoBonus * slot.Quantity
spec.FuelCapacity += component.FuelBonus * slot.Quantity
spec.FuelGeneration += component.FuelGeneration * slot.Quantity
spec.Colonizer = spec.Colonizer || component.ColonizationModule || component.OrbitalConstructionModule
spec.Initiative += component.InitiativeBonus
spec.Movement += component.MovementBonus * slot.Quantity
spec.ReduceMovement = MaxInt(spec.ReduceMovement, component.ReduceMovement) // these don't stack
spec.MiningRate += component.MiningRate * slot.Quantity
spec.TerraformRate += component.TerraformRate * slot.Quantity
spec.OrbitalConstructionModule = spec.OrbitalConstructionModule || component.OrbitalConstructionModule
spec.CanStealFleetCargo = spec.CanStealFleetCargo || component.CanStealFleetCargo
spec.CanStealPlanetCargo = spec.CanStealPlanetCargo || component.CanStealPlanetCargo
spec.Radiating = spec.Radiating || component.Radiating
// Add this mine type to the layers this design has
if component.MineLayingRate > 0 {
spec.CanLayMines = true
if spec.MineLayingRateByMineType == nil {
spec.MineLayingRateByMineType = make(map[MineFieldType]int)
}
if _, ok := spec.MineLayingRateByMineType[component.MineFieldType]; !ok {
spec.MineLayingRateByMineType[component.MineFieldType] = 0
}
spec.MineLayingRateByMineType[component.MineFieldType] += int(float64(component.MineLayingRate) * float64(slot.Quantity) * (1 + hull.MineLayingBonus))
}
// i.e. two .3f battle computers is (1 -.3) * (1 - .3) or (.7 * .7) or it decreases innaccuracy by 49%
// so a 75% accurate torpedo would be 100 - (100 - 75) * .49 = 100 - 12.25 or 88% accurate
// a 75% accurate torpedo with two 30% comps and one 50% comp would be
// 100 - (100 - 75) * .7 * .7 * .5 = 94% accurate
// if TorpedoInnaccuracyDecrease is 1 (default), it's just 75%
spec.TorpedoInaccuracyFactor *= float64(math.Pow((1 - float64(component.TorpedoBonus)), float64(slot.Quantity)))
torpedoJammingFactor *= float64(math.Pow((1 - float64(component.TorpedoJamming)), float64(slot.Quantity)))
// beam bonuses
spec.BeamBonus += component.BeamBonus * float64(slot.Quantity)
spec.BeamDefense += component.BeamDefense * float64(slot.Quantity)
// if this slot has a bomb, this design is a bomber
if component.HullSlotType == HullSlotTypeBomb || component.MinKillRate > 0 {
spec.Bomber = true
bomb := Bomb{
Quantity: slot.Quantity,
KillRate: component.KillRate,
MinKillRate: component.MinKillRate,
StructureDestroyRate: component.StructureDestroyRate,
UnterraformRate: component.UnterraformRate,
}
if component.UnterraformRate > 0 {
spec.RetroBombs = append(spec.RetroBombs, bomb)
} else if component.Smart {
spec.SmartBombs = append(spec.SmartBombs, bomb)
} else {
spec.Bombs = append(spec.Bombs, bomb)
}
// bombs add to rating
bombsPower += int((bomb.KillRate+bomb.StructureDestroyRate)*10) * slot.Quantity * 2
}
if component.Power > 0 {
spec.HasWeapons = true
spec.WeaponSlots = append(spec.WeaponSlots, slot)
switch component.Category {
case TechCategoryBeamWeapon:
// beams contribute to the rating based on range, but sappers
// are 1/3rd rated
rating := component.Power * slot.Quantity * (component.Range + 3) / 4
if component.DamageShieldsOnly {
rating /= 3
}
beamPower += rating
case TechCategoryTorpedo:
torpedoPower += component.Power * slot.Quantity * (component.Range - 2) / 2
}
}
// cloaking
if component.CloakUnits > 0 {
spec.CloakUnits += component.CloakUnits * slot.Quantity
}
if component.ReduceCloaking {
numTachyonDetectors++
}
// cargo and space doc that are built into the hull
// the space dock assumes that there is only one slot like that
// it won't add them up
if hullSlot.Type&HullSlotTypeSpaceDock > 0 {
spec.SpaceDock = hullSlot.Capacity
}
// mass drivers
if component.PacketSpeed > 0 {
// if we already have a massdriver at this speed, add an additional mass driver to up
// our speed
if spec.BasePacketSpeed == component.PacketSpeed {
spec.AdditionalMassDrivers++
}
spec.BasePacketSpeed = MaxInt(spec.BasePacketSpeed, component.PacketSpeed)
spec.MassDriver = component.Name
}
// stargate fields
if component.SafeHullMass != 0 {
spec.Stargate = component.Name
spec.SafeHullMass = component.SafeHullMass
}
if component.MaxHullMass != 0 {
spec.MaxHullMass = component.MaxHullMass
}
if component.SafeRange != 0 {
spec.SafeRange = component.SafeRange
}
if component.MaxRange != 0 {
spec.MaxRange = component.MaxRange
}
}
}
// ISB gives some special starbase bonuses
if hull.Starbase {
spec.CloakUnits += raceSpec.BuiltInCloakUnits
spec.Cost = spec.Cost.MultiplyFloat64(raceSpec.StarbaseCostFactor)
}
spec.Shields = int(float64(spec.Shields) * raceSpec.ShieldStrengthFactor)
spec.Armor = int(float64(spec.Armor) * raceSpec.ArmorStrengthFactor)
spec.TorpedoJamming = 1 - torpedoJammingFactor
// determine the safe speed for this design
spec.SafePacketSpeed = spec.BasePacketSpeed + spec.AdditionalMassDrivers
// figure out the cloak as a percentage after we specd our cloak units
spec.CloakPercent = getCloakPercentForCloakUnits(spec.CloakUnits)
spec.CloakPercentFullCargo = getCloakPercentForCloakUnits(int(math.Round(float64(spec.CloakUnits) * float64(spec.Mass) / float64(spec.Mass+spec.CargoCapacity))))
if numTachyonDetectors > 0 {
// 95% ^ (SQRT(#_of_detectors) = Reduction factor for other player's cloaking (Capped at 81% or 17TDs)
spec.ReduceCloaking = math.Pow((100.0-float64(rules.TachyonCloakReduction))/100, math.Sqrt(float64(numTachyonDetectors)))
}
if spec.NumEngines > 0 {
// Movement = IdealEngineSpeed - 2 - Mass / 70 / NumEngines + NumManeuveringJets + 2*NumOverThrusters
// we added any MovementBonus components above
// we round up the slightest bit, and we can't go below 2, or above 10
spec.Movement = Clamp((spec.Engine.IdealSpeed-2)-spec.Mass/70/spec.NumEngines+spec.Movement+raceSpec.MovementBonus, 2, 10)
} else {
spec.Movement = 0
}
beamPower = int(float64(beamPower) * (1 + spec.BeamBonus))
if beamPower > 0 {
// starbases don't move, but for the beam power calcs
// assume they have a movement of "2" which is the lowest possible
movement := Clamp(spec.Movement, 2, 10)
// a movement of 1 1/2 int the UI (i.e. 6) doesn't impact your beam
// power rating. Anything less reduces your beam power, anything higher increases it
beamPower += (beamPower * (movement - 6)) / 10
}
spec.PowerRating = beamPower + torpedoPower + bombsPower
spec.computeScanRanges(rules, raceSpec.ScannerSpec, techLevels, design, hull)
// compute the estimated range for this design
if spec.NumEngines > 0 {
fuelCostFor1kly := spec.Engine.getFuelCostForEngine(spec.Engine.IdealSpeed, spec.Mass, 1000, 1+raceSpec.FuelEfficiencyOffset)
fuelCostFor1klyFull := spec.Engine.getFuelCostForEngine(spec.Engine.IdealSpeed, spec.Mass+spec.CargoCapacity, 1000, 1+raceSpec.FuelEfficiencyOffset)
if fuelCostFor1kly == 0 {
spec.EstimatedRange = Infinite
spec.EstimatedRangeFull = Infinite
} else {
spec.EstimatedRange = int(float64(spec.FuelCapacity) / float64(fuelCostFor1kly) * 1000)
spec.EstimatedRangeFull = int(float64(spec.FuelCapacity) / float64(fuelCostFor1klyFull) * 1000)
}
}
return spec
}
// Compute the scan ranges for this ship design The formula is: (scanner1**4 + scanner2**4 + ...
// + scannerN**4)**(.25)
func (spec *ShipDesignSpec) computeScanRanges(rules *Rules, scannerSpec ScannerSpec, techLevels TechLevel, design *ShipDesign, hull *TechHull) {
spec.ScanRange = NoScanner
spec.ScanRangePen = NoScanner
// compute scanner as a built in JoaT scanner if it's built in
builtInScannerMultiplier := scannerSpec.BuiltInScannerMultiplier
if builtInScannerMultiplier > 0 && hull.BuiltInScanner {
spec.ScanRange = techLevels.Electronics * builtInScannerMultiplier
if !scannerSpec.NoAdvancedScanners {
spec.ScanRangePen = int(math.Pow(float64(spec.ScanRange)/2, 4))
}
spec.ScanRange = int(math.Pow(float64(spec.ScanRange), 4))
}
for _, slot := range design.Slots {
if slot.Quantity > 0 {
component := rules.techs.GetHullComponent(slot.HullComponent)
// bat scanners have 0 range
if component.ScanRange != NoScanner {
spec.ScanRange += int(math.Pow(float64(component.ScanRange), 4) * float64(slot.Quantity))
}
if component.ScanRangePen != NoScanner {
spec.ScanRangePen += int((math.Pow(float64(component.ScanRangePen), 4)) * float64(slot.Quantity))
}
}
}
// now quad root it
if spec.ScanRange != NoScanner {
spec.ScanRange = int(math.Pow(float64(spec.ScanRange), .25) + .5)
spec.ScanRange = int(float64(spec.ScanRange) * scannerSpec.ScanRangeFactor)
}
if spec.ScanRangePen != NoScanner {
spec.ScanRangePen = int(math.Pow(float64(spec.ScanRangePen), .25) + .5)
}
// if we have no pen scan but we have a regular scan, set the pen scan range to 0
if spec.ScanRangePen == NoScanner {
if spec.ScanRange != NoScanner {
spec.ScanRangePen = 0
} else {
spec.ScanRangePen = NoScanner
}
}
// true if we have any scanning capability
spec.Scanner = spec.ScanRange != NoScanner || spec.ScanRangePen != NoScanner
}
func DesignShip(techStore *TechStore, hull *TechHull, name string, player *Player, num int, hullSetNumber int, purpose ShipDesignPurpose, fleetPurpose FleetPurpose) *ShipDesign {
design := NewShipDesign(player, num).WithName(name).WithHull(hull.Name)
design.Purpose = purpose
// fuel depots are empty
if purpose == ShipDesignPurposeFuelDepot {
return design
}
engine := techStore.GetBestEngine(player, hull, fleetPurpose)
scanner := techStore.GetBestScanner(player)
fuelTank := techStore.GetBestFuelTank(player)
cargoPod := techStore.GetBestCargoPod(player)
beamWeapon := techStore.GetBestBeamWeapon(player)
torpedo := techStore.GetBestTorpedo(player)
bomb := techStore.GetBestBomb(player)
smartBomb := techStore.GetBestSmartBomb(player)
structureBomb := techStore.GetBestStructureBomb(player)
shield := techStore.GetBestShield(player)
armor := techStore.GetBestArmor(player)
colonizationModule := techStore.GetBestColonizationModule(player)
battleComputer := techStore.GetBestBattleComputer(player)
miningRobot := techStore.GetBestMiningRobot(player)
standardMineLayer := techStore.GetBestMineLayer(player, MineFieldTypeStandard)
heavyMineLayer := techStore.GetBestMineLayer(player, MineFieldTypeHeavy)
speedMineLayer := techStore.GetBestMineLayer(player, MineFieldTypeSpeedBump)
packetThrower := techStore.GetBestPacketThrower(player)
stargate := techStore.GetBestStargate(player)
numColonizationModules := 0
numScanners := 0
numBeamWeapons := 0
numTorpedos := 0
numArmors := 0
numShields := 0
numFuelTanks := 0
numCargoPods := 0
numPacketThrowers := 0
numStargates := 0
for i, hullSlot := range hull.Slots {
slot := ShipDesignSlot{HullSlotIndex: i + 1}
slot.Quantity = hullSlot.Capacity
// reduce quantity of armor and weapons when designing starbases for defense
if hullSlot.Type == HullSlotTypeArmor ||
hullSlot.Type == HullSlotTypeShield ||
hullSlot.Type == HullSlotTypeShieldArmor ||
hullSlot.Type == HullSlotTypeWeaponShield ||
hullSlot.Type == HullSlotTypeWeapon {
if purpose == ShipDesignPurposeStarbaseQuarter {
slot.Quantity = MaxInt(1, hullSlot.Capacity/4)
} else if purpose == ShipDesignPurposeStarbaseHalf {
slot.Quantity = MaxInt(1, hullSlot.Capacity/2)
}
}
switch hullSlot.Type {
case HullSlotTypeEngine:
slot.HullComponent = engine.Name
case HullSlotTypeScanner:
numScanners++
slot.HullComponent = scanner.Name
case HullSlotTypeWeapon:
if numTorpedos > numBeamWeapons {
slot.HullComponent = beamWeapon.Name
numBeamWeapons++
} else {
slot.HullComponent = torpedo.Name
numTorpedos++
}
case HullSlotTypeBomb:
// fill the bomb slot based on the type of bomber we want
// or leave it blank
switch purpose {
case ShipDesignPurposeSmartBomber:
if smartBomb != nil {
slot.HullComponent = smartBomb.Name
}
case ShipDesignPurposeStructureBomber:
if structureBomb != nil {
slot.HullComponent = structureBomb.Name
}
default:
if bomb != nil {
slot.HullComponent = bomb.Name
}
}
case HullSlotTypeShieldArmor:
// fuel freighters stay fast and loose
if purpose == ShipDesignPurposeFuelFreighter {
continue
}
// if we are choosing shield or armor, pick armor first, then shield
if numArmors > numShields {
slot.HullComponent = shield.Name
} else {
slot.HullComponent = armor.Name
}
case HullSlotTypeArmor:
// fuel freighters stay fast and loose
if purpose == ShipDesignPurposeFuelFreighter {
continue
}
slot.HullComponent = armor.Name
numArmors++
case HullSlotTypeShield:
// fuel freighters stay fast and loose
if purpose == ShipDesignPurposeFuelFreighter {
continue
}
slot.HullComponent = shield.Name
numShields++
case HullSlotTypeMining:
slot.HullComponent = miningRobot.Name
case HullSlotTypeMineLayer:
switch purpose {
case ShipDesignPurposeSpeedMineLayer:
slot.HullComponent = speedMineLayer.Name
default:
if heavyMineLayer != nil {
slot.HullComponent = heavyMineLayer.Name
} else {
slot.HullComponent = standardMineLayer.Name
}
}
case HullSlotTypeOrbital:
fallthrough
case HullSlotTypeOrbitalElectrical:
// if this starbase is designed for stargates or packet throwers, fill those
// first. By default add packet throwers, then stargates, then battle computers
switch purpose {
case ShipDesignPurposePacketThrower:
if packetThrower != nil {
slot.HullComponent = packetThrower.Name
numPacketThrowers++
}
case ShipDesignPurposeStargater:
if stargate != nil {
slot.HullComponent = packetThrower.Name
numStargates++
}
default:
// packet throwers for defense, then stargates
if numPacketThrowers == 0 && packetThrower != nil {
slot.HullComponent = packetThrower.Name
numPacketThrowers++
} else if numStargates == 0 && stargate != nil {
slot.HullComponent = stargate.Name
numStargates++
}
if slot.HullComponent == "" && hullSlot.Type == HullSlotTypeOrbitalElectrical {
slot.HullComponent = battleComputer.Name
}
}
case HullSlotTypeElectrical:
// TODO: add in jammers, stealth, etc
switch purpose {
case ShipDesignPurposeCapitalShip:
fallthrough
case ShipDesignPurposeFighter:
fallthrough
case ShipDesignPurposeFighterScout:
fallthrough
default:
slot.HullComponent = battleComputer.Name
}
case HullSlotTypeMechanical:
switch purpose {
case ShipDesignPurposeFuelFreighter:
slot.HullComponent = fuelTank.Name
numFuelTanks++
case ShipDesignPurposeFreighter:
fallthrough
case ShipDesignPurposeColonistFreighter:
// add cargo pods to freighters if we have a ramscoop
if engine.FreeSpeed > 1 && cargoPod != nil {
slot.HullComponent = cargoPod.Name
numCargoPods++
}
case ShipDesignPurposeColonizer:
if colonizationModule != nil && numColonizationModules == 0 {
numColonizationModules++
slot.HullComponent = colonizationModule.Name
slot.Quantity = 1 // we only need 1 colonization module
} else {
// balance fuel and cargo, fuel firsts
if numFuelTanks > numCargoPods {
slot.HullComponent = cargoPod.Name
numCargoPods++
} else {
slot.HullComponent = fuelTank.Name
numFuelTanks++
}
}
default:
slot.HullComponent = fuelTank.Name
numFuelTanks++
}
case HullSlotTypeScannerElectricalMechanical:
switch purpose {
case ShipDesignPurposeFuelFreighter:
slot.HullComponent = fuelTank.Name
numFuelTanks++
case ShipDesignPurposeFreighter:
fallthrough
case ShipDesignPurposeColonistFreighter:
// add cargo pods to freighters if we have a ramscoop
if engine.FreeSpeed > 1 && cargoPod != nil {
slot.HullComponent = cargoPod.Name
numCargoPods++
} else {
slot.HullComponent = fuelTank.Name
numFuelTanks++
}
case ShipDesignPurposeColonizer:
if colonizationModule != nil && numColonizationModules == 0 {
numColonizationModules++
slot.HullComponent = colonizationModule.Name
slot.Quantity = 1 // we only need 1 colonization module
} else {
// balance fuel and cargo, fuel firsts
if numFuelTanks > numCargoPods && cargoPod != nil {
slot.HullComponent = cargoPod.Name
numCargoPods++
} else {
slot.HullComponent = fuelTank.Name
numFuelTanks++
}
}
default:
if numScanners == 0 {
numScanners++
slot.HullComponent = scanner.Name
} else {
// can always use more
slot.HullComponent = fuelTank.Name
numFuelTanks++
}
}
case HullSlotTypeArmorScannerElectricalMechanical:
switch purpose {
case ShipDesignPurposeFuelFreighter:
slot.HullComponent = fuelTank.Name
numFuelTanks++
case ShipDesignPurposeColonizer:
if colonizationModule != nil && numColonizationModules == 0 {
numColonizationModules++
slot.HullComponent = colonizationModule.Name
slot.Quantity = 1 // we only need 1 colonization module
} else { // balance fuel and cargo, fuel firsts
if numFuelTanks > numCargoPods && cargoPod != nil {
slot.HullComponent = cargoPod.Name
numCargoPods++
} else {
slot.HullComponent = fuelTank.Name
numFuelTanks++
}
}
default:
if numScanners == 0 {
slot.HullComponent = scanner.Name
numScanners++
} else {
slot.HullComponent = fuelTank.Name
numFuelTanks++
}
}
case HullSlotTypeGeneral:
switch purpose {
case ShipDesignPurposeFuelFreighter:
slot.HullComponent = fuelTank.Name
numFuelTanks++
case ShipDesignPurposeColonizer:
// balance fuel and cargo, fuel firsts
if numFuelTanks > numCargoPods && cargoPod != nil {
slot.HullComponent = cargoPod.Name
numCargoPods++
} else {
slot.HullComponent = fuelTank.Name
numFuelTanks++
}
case ShipDesignPurposeFighter:
fallthrough
case ShipDesignPurposeFighterScout:
if numScanners == 0 {
slot.HullComponent = scanner.Name
numScanners++
} else {
slot.HullComponent = beamWeapon.Name
}
default:
if numScanners == 0 {
slot.HullComponent = scanner.Name
numScanners++
} else {
slot.HullComponent = fuelTank.Name
numFuelTanks++
}
}
}
// we filled it, add it
if slot.HullComponent != "" {
design.Slots = append(design.Slots, slot)
}
}
return design
}