-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgeneric_effects.as
More file actions
5790 lines (4883 loc) · 162 KB
/
generic_effects.as
File metadata and controls
5790 lines (4883 loc) · 162 KB
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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import buildings;
from buildings import IBuildingHook;
import resources;
import util.formatting;
import systems;
import saving;
import influence;
from influence import InfluenceStore;
from statuses import IStatusHook, Status, StatusInstance;
from resources import integerSum, decimalSum;
import orbitals;
from orbitals import IOrbitalEffect;
import attributes;
import hook_globals;
import research;
import empire_effects;
import repeat_hooks;
import planet_types;
import ancient_buffs;
#section server
import object_creation;
from components.ObjectManager import getDefenseDesign;
from object_stats import ObjectStatType, getObjectStat;
#section all
//ModSupportBuildSpeed(<Percentage>)
// The consuming planet builds support ships <Percentage> faster.
class ModSupportBuildSpeed : GenericEffect, TriggerableGeneric {
Document doc("Increase the speed at which support ships are constructed.");
Argument factor(AT_Decimal, doc="Percentage increase to base build speed. eg. 0.2 for +20%.");
bool get_hasEffect() const override {
return true;
}
string formatEffect(Object& obj, array<const IResourceHook@>& hooks) const override {
return formatPctEffect(locale::IRON_EFFECT, decimalSum(hooks, 0), locale::MOD_SPEED);
}
#section server
void enable(Object& obj, any@ data) const override {
if(obj.hasConstruction)
obj.modSupportBuildSpeed(+int(arguments[0].decimal*100.0));
}
void disable(Object& obj, any@ data) const override {
if(obj.hasConstruction)
obj.modSupportBuildSpeed(-int(arguments[0].decimal*100.0));
}
#section all
};
//PlayParticles(<Particle Effect Name>, [Scale = 1.0])
// Plays the <Particle Effect> on the specified object, with optional <Scale>
class PlayParticles : GenericEffect, TriggerableGeneric {
Document doc("Play the specified particle effect around the object when the effect is enabled.");
Argument particle_effect(AT_Custom, doc="Name of the particle effect to play.");
Argument scale(AT_Decimal, "1.0", doc="Size of the particle effect relative to the object's size.");
#section server
void enable(Object& obj, any@ data) const override {
playParticleSystem(arguments[0].str, vec3d(), quaterniond(), arguments[1].decimal * obj.radius, obj);
}
#section all
};
class PersistentParticles : GenericEffect {
Document doc("Add a persistent particle effect to the object while this is active.");
Argument particle_effect(AT_Custom, doc="Name of the particle effect to play.");
Argument scale(AT_Decimal, "1.0", doc="Size of the particle effect relative to the object's size.");
Argument fleet_scale(AT_Boolean, "False", doc="Whether to scale the particle effect to the fleet's scale.");
#section server
void enable(Object& obj, any@ data) const override {
int64 effId = 0;
data.store(effId);
}
void tick(Object& obj, any@ data, double time) const override {
int64 effId = 0;
data.retrieve(effId);
if(effId == 0) {
effId = obj.id << 32 | 0x2 << 24 | randomi(0, 0xffffff);
data.store(effId);
double size = scale.decimal;
if(fleet_scale.boolean && obj.hasLeaderAI)
size *= obj.getFormationRadius() / obj.radius;
makePersistentParticles(ALL_PLAYERS, effId, obj, particle_effect.str, size);
}
}
void disable(Object& obj, any@ data) const override {
int64 effId = 0;
data.retrieve(effId);
if(effId != 0) {
removeGfxEffect(ALL_PLAYERS, effId);
effId = 0;
data.store(effId);
}
}
void load(any@ data, SaveFile& file) const override {
int64 effId = 0;
data.store(effId);
}
#section all
};
//GrantAbility(<Ability>)
// Grants the planet access to <Ability>.
class GrantAbility : GenericEffect, TriggerableGeneric {
Document doc("While this effect is active, the object has access to the specified ability.");
Argument ability(AT_Ability, doc="The ability type to grant.");
#section server
void enable(Object& obj, any@ data) const override {
int id = -1;
if(data !is null && data.retrieve(id) && id != -1) {
obj.enableAbility(id);
}
else {
if(!obj.hasAbilities) {
if(obj.isPlanet)
cast<Planet>(obj).activateAbilities();
else if(obj.isShip)
cast<Ship>(obj).activateAbilities();
else if(obj.isOrbital)
cast<Orbital>(obj).activateAbilities();
else
return;
}
if(data !is null) {
id = obj.addAbility(ability.integer);
data.store(id);
}
else {
obj.createAbility(ability.integer);
}
}
}
void disable(Object& obj, any@ data) const override {
int id = -1;
if(data.retrieve(id) && id != -1)
obj.disableAbility(id);
}
void save(any@ data, SaveFile& file) const override {
int id = -1;
data.retrieve(id);
file << id;
}
void load(any@ data, SaveFile& file) const override {
int id = -1;
file >> id;
data.store(id);
}
#section all
};
//AddPlanetResource(<Planet Resource>)
// Add a new resource to the planet of type <Planet Resource>.
class AddPlanetResource : GenericEffect, TriggerableGeneric {
Document doc("The object gains a new planetary resource while the effect is active.");
Argument resource(AT_PlanetResource, doc="Type of resource to give.");
#section server
void enable(Object& obj, any@ data) const override {
int64 id = obj.addResource(arguments[0].integer);
if(data !is null)
data.store(id);
}
void disable(Object& obj, any@ data) const override {
int64 id = 0;
data.retrieve(id);
obj.removeResource(id);
}
void save(any@ data, SaveFile& file) const override {
int64 id = 0;
data.retrieve(id);
file << id;
}
void load(any@ data, SaveFile& file) const override {
int64 id = 0;
file >> id;
data.store(id);
}
#section all
};
//AddStatus(<Status Effect>)
// Add a new status effect.
class AddStatus : GenericEffect {
Document doc("Give a new status effect to the object.");
Argument status(AT_Status, doc="Type of status effect to create.");
Argument duration(AT_Decimal, "-1", doc="How long the status effect should last. If set to -1, the status effect acts as long as this effect hook does.");
#section server
void enable(Object& obj, any@ data) const override {
if(obj.hasStatuses) {
int64 id = obj.addStatus(arguments[1].decimal, uint(arguments[0].integer));
if(data !is null)
data.store(id);
}
}
void disable(Object& obj, any@ data) const override {
if(obj.hasStatuses && data !is null) {
int64 id = 0;
data.retrieve(id);
obj.removeStatus(id);
}
}
void save(any@ data, SaveFile& file) const override {
int64 id = 0;
data.retrieve(id);
file << id;
}
void load(any@ data, SaveFile& file) const override {
int64 id = -1;
if(file >= SV_0013)
file >> id;
data.store(id);
}
#section all
};
class RemoveAllStatus : GenericEffect {
Document doc("Remove any and all statuses of a praticular type that are on this object at any point.");
Argument status(AT_Status, doc="Type of status effect to create.");
#section server
void enable(Object& obj, any@ data) const override {
check(obj);
}
void check(Object& obj) const {
if(!obj.hasStatuses)
return;
obj.removeStatusType(status.integer);
}
void tick(Object& obj, any@ data, double tick) const override {
check(obj);
}
#section all
};
class AddRegionStatus : GenericEffect, TriggerableGeneric {
Document doc("Add a status effect to everything in the region this object is in.");
Argument type(AT_Status, doc="Type of status effect to add.");
Argument empire_limited(AT_Boolean, "True", doc="Whether the status should be limited to the empire.");
#section server
void enable(Object& obj, any@ data) const {
Empire@ owner = obj.owner;
Region@ region = obj.region;
if(region !is null)
region.addRegionStatus(empire_limited.boolean ? owner : null, type.integer);
}
void disable(Object& obj, any@ data) const {
Empire@ owner = obj.owner;
Region@ region = obj.region;
if(region !is null)
region.removeRegionStatus(empire_limited.boolean ? owner : null, type.integer);
}
void ownerChange(Object& obj, any@ data, Empire@ prevOwner, Empire@ newOwner) const {
Region@ region = obj.region;
if(region !is null && empire_limited.boolean) {
region.removeRegionStatus(prevOwner, type.integer);
region.addRegionStatus(newOwner, type.integer);
}
}
void regionChange(Object& obj, any@ data, Region@ fromRegion, Region@ toRegion) const {
Empire@ owner = obj.owner;
if(fromRegion !is null)
fromRegion.removeRegionStatus(empire_limited.boolean ? owner : null, type.integer);
if(toRegion !is null)
toRegion.addRegionStatus(empire_limited.boolean ? owner : null, type.integer);
}
#section all
};
class AddRegionStatusEnemies : GenericEffect, TriggerableGeneric {
Document doc("Add a status effect to all enemy objects in the region this object is in.");
Argument type(AT_Status, doc="Type of status effect to add.");
#section server
void enable(Object& obj, any@ data) const {
int mask = 0;
data.store(mask);
}
void disable(Object& obj, any@ data) const {
int mask = 0;
data.retrieve(mask);
Region@ region = obj.region;
if(region !is null) {
for(uint i = 0, cnt = getEmpireCount(); i < cnt; ++i) {
Empire@ other = getEmpire(i);
if(other.major && mask & other.mask != 0)
region.removeRegionStatus(other, type.integer);
}
}
mask = 0;
data.store(mask);
}
void tick(Object& obj, any@ data) const {
int curMask = 0;
data.retrieve(curMask);
int newMask = obj.owner.hostileMask;
if(newMask != curMask) {
Region@ region = obj.region;
if(region !is null) {
for(uint i = 0, cnt = getEmpireCount(); i < cnt; ++i) {
Empire@ other = getEmpire(i);
if(!other.major)
continue;
if(curMask & other.mask != 0 && newMask & other.mask == 0)
region.removeRegionStatus(other, type.integer);
else if(curMask & other.mask == 0 && newMask & other.mask != 0)
region.addRegionStatus(other, type.integer);
}
}
curMask = newMask;
data.store(curMask);
}
}
void regionChange(Object& obj, any@ data, Region@ fromRegion, Region@ toRegion) const {
int curMask = 0;
data.retrieve(curMask);
if(fromRegion !is null) {
for(uint i = 0, cnt = getEmpireCount(); i < cnt; ++i) {
Empire@ other = getEmpire(i);
if(other.major && curMask & other.mask != 0)
fromRegion.removeRegionStatus(other, type.integer);
}
}
if(toRegion !is null) {
for(uint i = 0, cnt = getEmpireCount(); i < cnt; ++i) {
Empire@ other = getEmpire(i);
if(other.major && curMask & other.mask != 0)
toRegion.addRegionStatus(other, type.integer);
}
}
}
void save(any@ data, SaveFile& file) const override {
int mask = 0;
data.retrieve(mask);
file << mask;
}
void load(any@ data, SaveFile& file) const override {
int mask = 0;
file >> mask;
data.store(mask);
}
#section all
};
//FreeFTLSystem()
// Grants free ftl leaving the system.
class FreeFTLSystem : GenericEffect, TriggerableGeneric {
Document doc("Objects FTLing out of the system this effect is active in, that are owned by this effect object's owner, can FTL for free.");
#section server
void tick(Object& obj, any@ data, double time) const override {
Region@ region = obj.region;
Empire@ owner = obj.owner;
if(region !is null && owner !is null && owner.valid)
region.FreeFTLMask |= owner.mask;
}
void ownerChange(Object& obj, any@ data, Empire@ prevOwner, Empire@ newOwner) const override {
Region@ region = obj.region;
if(region !is null && prevOwner !is null && prevOwner.valid)
region.FreeFTLMask &= ~prevOwner.mask;
if(region !is null && newOwner !is null && newOwner.valid)
region.FreeFTLMask |= newOwner.mask;
}
void regionChange(Object& obj, any@ data, Region@ prevRegion, Region@ newRegion) const override {
Empire@ owner = obj.owner;
if(owner is null || !owner.valid)
return;
if(prevRegion !is null)
prevRegion.FreeFTLMask &= ~owner.mask;
if(newRegion !is null)
newRegion.FreeFTLMask |= owner.mask;
}
void disable(Object& obj, any@ data) const override {
Region@ region = obj.region;
Empire@ owner = obj.owner;
if(region !is null && owner !is null && owner.valid)
region.FreeFTLMask &= ~owner.mask;
}
#section all
};
//GiveNeighbourVision()
// Gives the empire vision over neighbouring systems.
class GiveNeighbourVision : GenericEffect, TriggerableGeneric {
Document doc("Gives full vision over systems neighbouring the one this effect is active in.");
#section server
void grant(Empire@ emp, Region@ reg) {
SystemDesc@ system = getSystem(reg);
for(uint i = 0, cnt = system.adjacent.length; i < cnt; ++i)
getSystem(system.adjacent[i]).object.grantVision(emp);
}
void revoke(Empire@ emp, Region@ reg) {
SystemDesc@ system = getSystem(reg);
for(uint i = 0, cnt = system.adjacent.length; i < cnt; ++i)
getSystem(system.adjacent[i]).object.revokeVision(emp);
}
void enable(Object& obj, any@ data) const override {
Region@ reg = obj.region;
if(reg !is null)
grant(obj.owner, reg);
}
void disable(Object& obj, any@ data) const override {
Region@ reg = obj.region;
if(reg !is null)
revoke(obj.owner, reg);
}
void ownerChange(Object& obj, any@ data, Empire@ prevOwner, Empire@ newOwner) const override {
Region@ reg = obj.region;
if(reg !is null) {
if(prevOwner !is null)
revoke(prevOwner, reg);
if(newOwner !is null)
grant(newOwner, reg);
}
}
void regionChange(Object& obj, any@ data, Region@ prevRegion, Region@ newRegion) const override {
Empire@ owner = obj.owner;
if(owner !is null) {
if(prevRegion !is null)
revoke(owner, prevRegion);
if(newRegion !is null)
grant(owner, newRegion);
}
}
#section all
};
//ModNeighbourLoyalty(<Amount>)
// Give <Amount> extra loyalty to all neighbouring planets.
class ModNeighbourLoyalty : GenericEffect, TriggerableGeneric {
Document doc("Modifies the loyalty of all owned planets in this effect's system or adjacent to it.");
Argument amount(AT_Integer, doc="How much to add or subtract from the loyalty value.");
#section server
void enable(Object& obj, any@ data) const override {
Region@ reg = obj.region;
Empire@ owner = obj.owner;
if(reg !is null)
reg.modNeighbourLoyalty(owner, +arguments[0].integer);
}
void disable(Object& obj, any@ data) const override {
Region@ reg = obj.region;
Empire@ owner = obj.owner;
if(reg !is null)
reg.modNeighbourLoyalty(owner, -arguments[0].integer);
}
void regionChange(Object& obj, any@ data, Region@ prevRegion, Region@ newRegion) const override {
Empire@ owner = obj.owner;
if(prevRegion !is null)
prevRegion.modNeighbourLoyalty(owner, -arguments[0].integer);
if(newRegion !is null)
newRegion.modNeighbourLoyalty(owner, +arguments[0].integer);
}
void ownerChange(Object& obj, any@ data, Empire@ prevOwner, Empire@ newOwner) const override {
Region@ reg = obj.region;
if(reg !is null) {
reg.modNeighbourLoyalty(prevOwner, -arguments[0].integer);
reg.modNeighbourLoyalty(newOwner, +arguments[0].integer);
}
}
#section all
};
//ModLocalLoyalty(<Amount>)
// Give <Amount> extra loyalty to all planets in the same region.
class ModLocalLoyalty : GenericEffect, TriggerableGeneric {
Document doc("Modifies the loyalty of all owned planets in this effect's system.");
Argument amount(AT_Integer, doc="How much to add or subtract from the loyalty value.");
#section server
void enable(Object& obj, any@ data) const override {
Region@ reg = obj.region;
Empire@ owner = obj.owner;
if(reg !is null)
reg.modLocalLoyalty(owner, +arguments[0].integer);
}
void disable(Object& obj, any@ data) const override {
Region@ reg = obj.region;
Empire@ owner = obj.owner;
if(reg !is null)
reg.modLocalLoyalty(owner, -arguments[0].integer);
}
void regionChange(Object& obj, any@ data, Region@ prevRegion, Region@ newRegion) const override {
Empire@ owner = obj.owner;
if(prevRegion !is null)
prevRegion.modLocalLoyalty(owner, -arguments[0].integer);
if(newRegion !is null)
newRegion.modLocalLoyalty(owner, +arguments[0].integer);
}
void ownerChange(Object& obj, any@ data, Empire@ prevOwner, Empire@ newOwner) const override {
Region@ reg = obj.region;
if(reg !is null) {
reg.modLocalLoyalty(prevOwner, -arguments[0].integer);
reg.modLocalLoyalty(newOwner, +arguments[0].integer);
}
}
#section all
};
//RecordBonusDPS(<Amount>)
// Record the object as having <Amount> bonus DPS;
class RecordBonusDPS : GenericEffect, TriggerableGeneric {
Document doc("Record the object as having bonus DPS. Changes the object's strength calculation.");
Argument amount(AT_Decimal, doc="Amount of 'bonus' DPS to add to the object.");
#section server
void enable(Object& obj, any@ data) const override {
if(obj.hasLeaderAI)
obj.modBonusDPS(+arguments[0].decimal);
}
void disable(Object& obj, any@ data) const override {
if(obj.hasLeaderAI)
obj.modBonusDPS(-arguments[0].decimal);
}
#section all
};
//AddEnergyIncome(<Amount>)
// Adds <Amount> Energy income per second.
class AddEnergyIncomeStarTemperature : GenericEffect, TriggerableGeneric {
Document doc("Increase the energy income per second, based on the temperature of the star.");
Argument min_amount(AT_Decimal, doc="Minimum amount of energy per second to add, for a low temperature star.");
Argument max_amount(AT_Decimal, doc="Maximum amount of energy per second to add, for a high temperature star.");
Argument sqrt_scale(AT_Boolean, "True", doc="Whether to scale with the square root, biasing towards lower temperatures.");
#section server
void enable(Object& obj, any@ data) const override {
double amount = 0;
data.store(amount);
}
void disable(Object& obj, any@ data) const override {
double amount = 0;
data.retrieve(amount);
obj.owner.modEnergyIncome(-amount);
}
void tick(Object& obj, any@ data, double time) const override {
double amount = 0;
data.retrieve(amount);
double newAmount = 0;
Region@ reg = obj.region;
if(reg !is null) {
double temp = reg.starTemperature;
double fact = 1.0;
if(temp > 0)
fact = clamp((temp - 2000)/26000, 0.0, 1.0);
if(sqrt_scale.boolean)
fact = sqrt(fact);
newAmount = min_amount.decimal + (max_amount.decimal - min_amount.decimal) * fact;
}
if(newAmount != amount) {
obj.owner.modEnergyIncome(newAmount-amount);
data.store(newAmount);
}
}
void ownerChange(Object& obj, any@ data, Empire@ prevOwner, Empire@ newOwner) const override {
double amount = 0;
data.retrieve(amount);
if(prevOwner !is null && prevOwner.valid)
prevOwner.modEnergyIncome(-amount);
if(newOwner !is null && newOwner.valid)
newOwner.modEnergyIncome(+amount);
}
void save(any@ data, SaveFile& file) const override {
double amount = 0.0;
data.retrieve(amount);
file << amount;
}
void load(any@ data, SaveFile& file) const override {
double amount = 0.0;
file >> amount;
data.store(amount);
}
#section all
};
//ProtectSystem()
// Protect the system from losing loyalty.
class ProtectSystem : GenericEffect {
Document doc("Protect owned planets in the system from losing loyalty in any way.");
Argument timer(AT_Decimal, "0", doc="Delay timer before the effect starts working after being enabled.");
bool get_hasEffect() const override {
return true;
}
string formatEffect(Object& obj, array<const IResourceHook@>& hooks) const override {
return locale::PSIONIC_REAGENTS_EFFECT;
}
#section server
void enable(Object& obj, any@ data) const override {
double timer = 0.0;
data.store(timer);
}
void disable(Object& obj, any@ data) const override {
if(obj.region !is null)
obj.region.ProtectedMask &= ~obj.owner.mask;
}
void tick(Object& obj, any@ data, double tick) const override {
double timer = 0.0;
data.retrieve(timer);
if(timer >= arguments[0].decimal) {
if(obj.region !is null)
obj.region.ProtectedMask |= obj.owner.mask;
}
else {
timer += tick;
data.store(timer);
}
}
void save(any@ data, SaveFile& file) const override {
double timer = 0.0;
data.retrieve(timer);
file << timer;
}
void load(any@ data, SaveFile& file) const override {
double timer = 0.0;
file >> timer;
data.store(timer);
}
void ownerChange(Object& obj, any@ data, Empire@ prevOwner, Empire@ newOwner) const override {
if(obj.region !is null) {
double timer = 0.0;
data.retrieve(timer);
if(timer >= arguments[0].decimal) {
obj.region.ProtectedMask &= ~prevOwner.mask;
obj.region.ProtectedMask |= newOwner.mask;
}
}
}
void regionChange(Object& obj, any@ data, Region@ fromRegion, Region@ toRegion) const override {
double timer = 0.0;
data.retrieve(timer);
if(timer >= arguments[0].decimal) {
if(fromRegion !is null)
fromRegion.ProtectedMask &= ~obj.owner.mask;
if(toRegion !is null)
toRegion.ProtectedMask |= obj.owner.mask;
}
}
#section all
};
//BlockSystemFTL(<Block Owner> = False, <Block Friendly> = False, <Timer> = 0)
// Prevent any FTL from being activat
class BlockSystemFTL : GenericEffect {
Document doc("Block FTL travel from hostile empires in the system this effect is active in.");
Argument block_owner(AT_Boolean, "False", doc="Whether to also block the owner of the effect from using FTL.");
Argument block_friendly(AT_Boolean, "False", doc="Whether to also block empires that are not at war with the effect's owner from using FTL.");
Argument timer(AT_Decimal, "0", doc="Delay timer before the effect starts working after being enabled.");
#section server
void enable(Object& obj, any@ data) const override {
double timer = 0.0;
data.store(timer);
}
void disable(Object& obj, any@ data) const override {
if(obj.region !is null)
obj.region.BlockFTLMask = 0;
}
void tick(Object& obj, any@ data, double tick) const override {
double timer = 0.0;
data.retrieve(timer);
if(timer >= arguments[2].decimal) {
if(obj.region !is null) {
uint mask = ~0;
if(!arguments[0].boolean && obj.owner !is null)
mask &= ~obj.owner.mask;
if(!arguments[1].boolean && obj.owner !is null)
mask &= obj.owner.hostileMask;
obj.region.BlockFTLMask |= mask;
}
}
else {
timer += tick;
data.store(timer);
}
}
void save(any@ data, SaveFile& file) const override {
double timer = 0.0;
data.retrieve(timer);
file << timer;
}
void load(any@ data, SaveFile& file) const override {
double timer = 0.0;
file >> timer;
data.store(timer);
}
void ownerChange(Object& obj, any@ data, Empire@ prevOwner, Empire@ newOwner) const override {
if(obj.region !is null)
obj.region.BlockFTLMask = 0;
}
void regionChange(Object& obj, any@ data, Region@ fromRegion, Region@ toRegion) const override {
if(fromRegion !is null)
fromRegion.BlockFTLMask = 0;
}
#section all
};
//ProtectOtherPlanets()
// Protect other planets in the system from losing loyalty.
class ProtectOtherPlanets : GenericEffect {
Document doc("Planets in the system other than the planet this effect is active on are protected from losing loyalty.");
#section server
void enable(Object& obj, any@ data) const override {
obj.setProtectionDisabled(true);
}
void disable(Object& obj, any@ data) const override {
if(obj.region !is null)
obj.region.ProtectedMask &= ~obj.owner.mask;
}
void tick(Object& obj, any@ data, double tick) const override {
if(obj.region !is null)
obj.region.ProtectedMask |= obj.owner.mask;
}
void ownerChange(Object& obj, any@ data, Empire@ prevOwner, Empire@ newOwner) const override {
if(obj.region !is null) {
obj.region.ProtectedMask &= ~prevOwner.mask;
obj.region.ProtectedMask |= newOwner.mask;
}
}
void regionChange(Object& obj, any@ data, Region@ fromRegion, Region@ toRegion) const override {
if(fromRegion !is null)
fromRegion.ProtectedMask &= ~obj.owner.mask;
if(toRegion !is null)
toRegion.ProtectedMask |= obj.owner.mask;
}
#section all
};
//ModResourceEfficiencyBonus(<Percentage>)
// Improve the resource efficiency bonus of native resources by <Percentage>.
class ModResourceEfficiencyBonus : GenericEffect, TriggerableGeneric {
Document doc("Modify the resource efficiency bonus for native resources. This bonus changes how much pressure they give wherever they are consumed.");
Argument amount(AT_Decimal, doc="How much to add to the efficiency bonus. eg. 0.2 adds 20% extra base pressure to the resource.");
bool get_hasEffect() const override {
return true;
}
const IResourceHook@ get_displayHook() const override {
return null;
}
const IResourceHook@ get_carriedHook() const override {
return this;
}
string formatEffect(Object& obj, array<const IResourceHook@>& hooks) const override {
return formatPctEffect(locale::EFFECT_EFFICIENCY, arguments[0].decimal);
}
#section server
void enable(Object& obj, any@ data) const override {
obj.modResourceEfficiencyBonus(+arguments[0].decimal);
}
void disable(Object& obj, any@ data) const override {
obj.modResourceEfficiencyBonus(-arguments[0].decimal);
}
#section all
};
//AddTurret(<Effector>, <Argument> = <Value>, ...)
// Add a turret to the object.
tidy final class AddTurret : GenericEffect {
Document doc("Add a turret to the object. Arguments are based on effector values and need to be named and set manually.");
Effector@ effector;
EffectorDef@ def;
dictionary values;
double range = 0.0;
bool parse(const string& name, array<string>& args) {
if(args.length == 0) {
error("AddTurret expects at least 1 argument.");
return false;
}
@def = getEffectorDef(args[0]);
if(def is null) {
error("AddTurret: could not find effector "+args[0]);
return false;
}
#section server
@effector = Effector(def);
#section all
for(uint i = 1, cnt = args.length; i < cnt; ++i) {
int pos = args[i].findFirst("=");
if(pos == -1) {
error("Invalid effector argument: "+escape(args[i]));
return false;
}
string name = args[i].substr(0, pos).trimmed();
double value = toDouble(args[i].substr(pos+1));
#section server
int index = def.getArgumentIndex(name);
if(index == -1) {
error("Could not find effector argument: "+escape(args[i]));
return false;
}
effector[index] = value;
#section client
if(name.equals_nocase("Range"))
range = value;
#section all
}
#section server
effector.turretAngle = random3d();
effector.relativePosition = random3d(0.5);
effector.evaluate();
#section all
return true;
}
#section server
void enable(Object& obj, any@ data) const override {
Turret@ tr = Turret(effector);
data.store(@tr);
}
void tick(Object& obj, any@ data, double time) const override {
Turret@ tr;
data.retrieve(@tr);
if(tr !is null) {
double eff = 1.0;
if(obj.isOrbital) {
Orbital@ orb = cast<Orbital>(obj);
eff = orb.efficiency;
}
tr.update(obj, time, eff);
if(tr.flags & TF_Firing != 0)
obj.engaged = true;
}
}
void disable(Object& obj, any@ data) const override {
Turret@ tr = null;
data.store(@tr);
}
void save(any@ data, SaveFile& file) const override {
Turret@ tr;
data.retrieve(@tr);
if(tr !is null) {
file << true;
tr.save(file);
}
else {
file << false;
}
}
void load(any@ data, SaveFile& file) const override {
Turret@ tr;
bool has = false;
file >> has;
if(has)
@tr = Turret(file);
data.store(@tr);
}
#section all
};
//GlobalTradeNode()
// The system the object is in can be traded to from any other GlobalTradeNode.
class GlobalTradeNode : GenericEffect, TriggerableGeneric {
Document doc("The system the effect is active in can be traded to from any other GlobalTradeNode by the effect owner.");
void enable(Object& obj, any@ data) const {
Empire@ owner = obj.owner;
Region@ region = obj.region;
if(region !is null && owner !is null && owner.valid)
region.GateMask |= obj.owner.mask;
}
void disable(Object& obj, any@ data) const {
Empire@ owner = obj.owner;
Region@ region = obj.region;
if(region !is null && owner !is null && owner.valid)
region.GateMask &= ~obj.owner.mask;
}
void tick(Object& obj, any@ data, double time) const {
Empire@ owner = obj.owner;
Region@ region = obj.region;
if(region !is null && owner !is null && owner.valid)
region.GateMask |= obj.owner.mask;
}
void ownerChange(Object& obj, any@ data, Empire@ prevOwner, Empire@ newOwner) const {
Region@ region = obj.region;
if(region !is null) {
if(prevOwner !is null && prevOwner.valid)
region.GateMask &= ~prevOwner.mask;
if(newOwner !is null && newOwner.valid)
region.GateMask |= newOwner.mask;
}
}
void regionChange(Object& obj, any@ data, Region@ fromRegion, Region@ toRegion) const {
Empire@ owner = obj.owner;
if(owner !is null && owner.valid) {
if(fromRegion !is null)
fromRegion.GateMask &= ~obj.owner.mask;