forked from screepers/screeps-arena-typescript-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exported-game-constants.js
1284 lines (1149 loc) · 30.8 KB
/
exported-game-constants.js
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
export const ATTACK = "attack";
export const ATTACK_POWER = 30;
export const BODYPART_COST = { move: 50, work: 100, attack: 80, carry: 50, heal: 250, ranged_attack: 150, tough: 10 };
export const BODYPART_HITS = 100;
export const BOTTOM = 5;
export const BOTTOM_LEFT = 6;
export const BOTTOM_RIGHT = 4;
export const BUILD_POWER = 5;
export const CARRY = "carry";
export const CARRY_CAPACITY = 50;
export const CONSTRUCTION_COST = {
StructureTower: 5000,
StructureExtension: 200,
StructureRoad: 10,
StructureContainer: 100,
StructureWall: 100,
StructureRampart: 200,
StructureSpawn: 3000
};
export const CONSTRUCTION_COST_ROAD_SWAMP_RATIO = 5;
export const CONSTRUCTION_COST_ROAD_WALL_RATIO = 150;
export const CONTAINER_CAPACITY = 2000;
export const CONTAINER_HITS = 300;
export const CREEP_SPAWN_TIME = 3;
export class ConstructionSite extends GameObject {
get progress() {
if (!this.exists) {
return;
}
return SystemStore.roomObjectsData[this.id].progress;
}
get progressTotal() {
if (!this.exists) {
return;
}
return SystemStore.roomObjectsData[this.id].progressTotal;
}
get structurePrototypeName() {
if (!this.exists) {
return;
}
return SystemStore.roomObjectsData[this.id].structurePrototypeName;
}
get my() {
if (!this.exists) {
return;
}
if (SystemStore.roomObjectsData[this.id].user) {
return SystemStore.roomObjectsData[this.id].user === SystemStore.playerName;
}
return undefined;
}
toJSON() {
return Object.assign(super.toJSON(), {
progress: this.progress,
progressTotal: this.progressTotal,
my: this.my,
structure: this.structure
});
}
remove() {
if (!this.my) {
return C.ERR_NOT_OWNER;
}
Intents.set(this.id, "remove", {});
return C.OK;
}
}
export class CostMatrix {
constructor() {
this._bits = new Uint8Array(SystemStore.arenaSize * SystemStore.arenaSize);
}
set(xx, yy, val) {
xx = xx | 0;
yy = yy | 0;
this._bits[xx * SystemStore.arenaSize + yy] = Math.min(Math.max(0, val), 255);
}
get(xx, yy) {
xx = xx | 0;
yy = yy | 0;
return this._bits[xx * SystemStore.arenaSize + yy];
}
clone() {
var newMatrix = new CostMatrix();
newMatrix._bits = new Uint8Array(this._bits);
return newMatrix;
}
serialize() {
return Array.prototype.slice.apply(new Uint32Array(this._bits.buffer));
}
static deserialize(data) {
let instance = Object.create(CostMatrix.prototype);
instance._bits = new Uint8Array(new Uint32Array(data).buffer);
return instance;
}
}
export class Creep extends GameObject {
get hits() {
if (!this.exists) {
return;
}
return SystemStore.roomObjectsData[this.id].hits;
}
get hitsMax() {
if (!this.exists) {
return;
}
return SystemStore.roomObjectsData[this.id].hitsMax;
}
get my() {
if (!this.exists) {
return;
}
return SystemStore.roomObjectsData[this.id].user === SystemStore.playerName;
}
get fatigue() {
if (!this.exists) {
return;
}
return SystemStore.roomObjectsData[this.id].fatigue;
}
get body() {
if (!this.exists) {
return;
}
return SystemStore.roomObjectsData[this.id].body;
}
get store() {
return new Store(SystemStore.roomObjectsData[this.id]);
}
toJSON() {
return Object.assign(super.toJSON(), {
hits: this.hits,
hitsMax: this.hitsMax,
my: this.my,
fatigue: this.fatigue,
body: this.body
});
}
move(direction) {
if (!this.exists) {
return;
}
if (!this.my) {
return C.ERR_NOT_OWNER;
}
Intents.set(this.id, "move", { direction });
return C.OK;
}
moveTo(x, y, opts) {
if (typeof x === "object") {
if (!x) {
return;
}
opts = y;
y = x.y;
x = x.x;
}
const path = this.findPathTo({ x, y }, opts);
if (path.length > 0) {
let direction = getDirection(path[0].x - this.x, path[0].y - this.y);
return this.move(direction);
}
}
rangedAttack(target) {
if (!this.exists) {
return;
}
if (!this.my) {
return C.ERR_NOT_OWNER;
}
if (!target || !target.exists || !(target instanceof GameObject)) {
return C.ERR_INVALID_TARGET;
}
if (!this.body.some(p => p.type === C.RANGED_ATTACK && p.hits > 0)) {
return C.ERR_NO_BODYPART;
}
if (getDistance(this, target) > 3) {
return C.ERR_NOT_IN_RANGE;
}
Intents.set(this.id, "rangedAttack", { id: target.id });
return C.OK;
}
rangedMassAttack() {
if (!this.exists) {
return;
}
if (!this.my) {
return C.ERR_NOT_OWNER;
}
if (!this.body.some(p => p.type === C.RANGED_ATTACK && p.hits > 0)) {
return C.ERR_NO_BODYPART;
}
Intents.set(this.id, "rangedMassAttack", {});
return C.OK;
}
attack(target) {
if (!this.exists) {
return;
}
if (!this.my) {
return C.ERR_NOT_OWNER;
}
if (!target || !target.exists || !(target instanceof GameObject)) {
return C.ERR_INVALID_TARGET;
}
if (!this.body.some(p => p.type === C.ATTACK && p.hits > 0)) {
return C.ERR_NO_BODYPART;
}
if (getDistance(this, target) > 1) {
return C.ERR_NOT_IN_RANGE;
}
Intents.set(this.id, "attack", { id: target.id });
return C.OK;
}
heal(target) {
if (!this.exists) {
return;
}
if (!this.my) {
return C.ERR_NOT_OWNER;
}
if (!target || !target.exists || !(target instanceof GameObject)) {
return C.ERR_INVALID_TARGET;
}
if (!this.body.some(p => p.type === C.HEAL && p.hits > 0)) {
return C.ERR_NO_BODYPART;
}
if (getDistance(this, target) > 1) {
return C.ERR_NOT_IN_RANGE;
}
Intents.set(this.id, "heal", { id: target.id });
return C.OK;
}
rangedHeal(target) {
if (!this.exists) {
return;
}
if (!this.my) {
return C.ERR_NOT_OWNER;
}
if (!target || !target.exists || !(target instanceof GameObject)) {
return C.ERR_INVALID_TARGET;
}
if (!this.body.some(p => p.type === C.HEAL && p.hits > 0)) {
return C.ERR_NO_BODYPART;
}
if (getDistance(this, target) > 3) {
return C.ERR_NOT_IN_RANGE;
}
Intents.set(this.id, "rangedHeal", { id: target.id });
return C.OK;
}
pull(target) {
if (!this.exists) {
return;
}
if (!this.my) {
return C.ERR_NOT_OWNER;
}
if (!target || !target.exists || !(target instanceof Creep)) {
return C.ERR_INVALID_TARGET;
}
Intents.set(this.id, "pull", { id: target.id });
return C.OK;
}
withdraw(target, resourceType, amount) {
if (!this.exists) {
return;
}
if (!this.my) {
return C.ERR_NOT_OWNER;
}
if (this.spawning) {
return C.ERR_BUSY;
}
if (amount < 0) {
return C.ERR_INVALID_ARGS;
}
if (!C.RESOURCES_ALL.includes(resourceType)) {
return C.ERR_INVALID_ARGS;
}
if (!target || !target.id || !SystemStore.roomObjectsData[target.id].store || !(target instanceof Structure)) {
return C.ERR_INVALID_TARGET;
}
if (
!capacityForResource(SystemStore.roomObjectsData[target.id], resourceType) &&
!SystemStore.roomObjectsData[target.id].store[resourceType]
) {
return C.ERR_INVALID_TARGET;
}
if (this.getRangeTo(target) > 1) {
return C.ERR_NOT_IN_RANGE;
}
const emptySpace =
SystemStore.roomObjectsData[this.id].storeCapacity - sumObjectValues(SystemStore.roomObjectsData[this.id].store);
if (emptySpace <= 0) {
return C.ERR_FULL;
}
if (!amount) {
amount = Math.min(emptySpace, SystemStore.roomObjectsData[target.id].store[resourceType]);
}
if (amount > emptySpace) {
return C.ERR_FULL;
}
if (!amount || (SystemStore.roomObjectsData[target.id].store[resourceType] || 0) < amount) {
return C.ERR_NOT_ENOUGH_RESOURCES;
}
Intents.set(this.id, "withdraw", { id: target.id, amount, resourceType });
return C.OK;
}
transfer(target, resourceType, amount) {
if (!this.exists) {
return;
}
if (!this.my) {
return C.ERR_NOT_OWNER;
}
if (this.spawning) {
return C.ERR_BUSY;
}
if (amount < 0) {
return C.ERR_INVALID_ARGS;
}
if (!C.RESOURCES_ALL.includes(resourceType)) {
return C.ERR_INVALID_ARGS;
}
if (
!target ||
!target.id ||
!SystemStore.roomObjectsData[target.id].store ||
(target instanceof Creep && target.spawning) ||
(!(target instanceof Structure) && !(target instanceof Creep))
) {
return C.ERR_INVALID_TARGET;
}
if (!capacityForResource(SystemStore.roomObjectsData[target.id], resourceType)) {
return C.ERR_INVALID_TARGET;
}
if (this.getRangeTo(target) > 1) {
return C.ERR_NOT_IN_RANGE;
}
if (!SystemStore.roomObjectsData[this.id].store || !SystemStore.roomObjectsData[this.id].store[resourceType]) {
return C.ERR_NOT_ENOUGH_RESOURCES;
}
const storedAmount = SystemStore.roomObjectsData[target.id].storeCapacityResource
? SystemStore.roomObjectsData[target.id].store[resourceType] || 0
: sumObjectValues(SystemStore.roomObjectsData[target.id].store);
const targetCapacity = capacityForResource(SystemStore.roomObjectsData[target.id], resourceType);
if (!SystemStore.roomObjectsData[target.id].store || storedAmount >= targetCapacity) {
return C.ERR_FULL;
}
if (!amount) {
amount = Math.min(SystemStore.roomObjectsData[this.id].store[resourceType], targetCapacity - storedAmount);
}
if (SystemStore.roomObjectsData[this.id].store[resourceType] < amount) {
return C.ERR_NOT_ENOUGH_RESOURCES;
}
if (amount + storedAmount > targetCapacity) {
return C.ERR_FULL;
}
Intents.set(this.id, "transfer", { id: target.id, amount, resourceType });
return C.OK;
}
harvest(target) {
if (!this.exists) {
return;
}
if (!this.my) {
return C.ERR_NOT_OWNER;
}
if (this.spawning) {
return C.ERR_BUSY;
}
if (!this.body.some(p => p.type === C.WORK && p.hits > 0)) {
return C.ERR_NO_BODYPART;
}
if (!target || !target.id || !(target instanceof Source)) {
return C.ERR_INVALID_TARGET;
}
if (!target.energy) {
return C.ERR_NOT_ENOUGH_RESOURCES;
}
if (this.getRangeTo(target) > 1) {
return C.ERR_NOT_IN_RANGE;
}
Intents.set(this.id, "harvest", { id: target.id });
return C.OK;
}
drop(resourceType, amount) {
if (!this.exists) {
return;
}
if (!this.my) {
return C.ERR_NOT_OWNER;
}
if (this.spawning) {
return C.ERR_BUSY;
}
if (!C.RESOURCES_ALL.includes(resourceType)) {
return C.ERR_INVALID_ARGS;
}
if (!SystemStore.roomObjectsData[this.id].store || !SystemStore.roomObjectsData[this.id].store[resourceType]) {
return C.ERR_NOT_ENOUGH_RESOURCES;
}
if (!amount) {
amount = SystemStore.roomObjectsData[this.id].store[resourceType];
}
if (SystemStore.roomObjectsData[this.id].store[resourceType] < amount) {
return C.ERR_NOT_ENOUGH_RESOURCES;
}
Intents.set(this.id, "drop", { amount, resourceType });
return C.OK;
}
pickup(target) {
if (!this.exists) {
return;
}
if (!this.my) {
return C.ERR_NOT_OWNER;
}
if (this.spawning) {
return C.ERR_BUSY;
}
if (!target || !target.id || !(target instanceof Resource)) {
return C.ERR_INVALID_TARGET;
}
if (
sumObjectValues(SystemStore.roomObjectsData[this.id].store) >= SystemStore.roomObjectsData[this.id].storeCapacity
) {
return C.ERR_FULL;
}
if (this.getRangeTo(target) > 1) {
return C.ERR_NOT_IN_RANGE;
}
Intents.set(this.id, "pickup", { id: target.id });
return C.OK;
}
build(target) {
if (!this.exists) {
return;
}
if (!this.my) {
return C.ERR_NOT_OWNER;
}
if (this.spawning) {
return C.ERR_BUSY;
}
if (!this.body.some(p => p.type === C.WORK && p.hits > 0)) {
return C.ERR_NO_BODYPART;
}
if (!this.store.energy) {
return C.ERR_NOT_ENOUGH_RESOURCES;
}
if (!target || !target.id || !(target instanceof ConstructionSite)) {
return C.ERR_INVALID_TARGET;
}
if (this.getRangeTo(target) > 3) {
return C.ERR_NOT_IN_RANGE;
}
const objectsInTile = [],
creepsInTile = [];
let rampart;
Object.values(SystemStore.roomObjectsData).forEach(obj => {
if (obj.x == target.x && obj.y == target.y) {
if (obj.type == "rampart") {
rampart = obj;
}
if (C.OBSTACLE_OBJECT_TYPES.includes(obj.type)) {
if (obj.type == "creep") {
creepsInTile.push(obj);
} else {
objectsInTile.push(obj);
}
}
}
});
if (rampart && rampart.user !== SystemStore.roomObjectsData[this.id].user) {
return C.ERR_INVALID_TARGET;
}
if (C.OBSTACLE_OBJECT_TYPES.includes(C.STRUCTURE_PROTOTYPES[target.structurePrototypeName])) {
if (objectsInTile.length > 0) {
return C.ERR_INVALID_TARGET;
}
if (creepsInTile.length > 0) {
return C.ERR_INVALID_TARGET;
}
}
Intents.set(this.id, "build", { id: target.id, x: target.x, y: target.y });
return C.OK;
}
}
export const DISMANTLE_COST = 0.005;
export const DISMANTLE_POWER = 50;
export const ERR_BUSY = -4;
export const ERR_FULL = -8;
export const ERR_INVALID_ARGS = -10;
export const ERR_INVALID_TARGET = -7;
export const ERR_NAME_EXISTS = -3;
export const ERR_NOT_ENOUGH_ENERGY = -6;
export const ERR_NOT_ENOUGH_EXTENSIONS = -6;
export const ERR_NOT_ENOUGH_RESOURCES = -6;
export const ERR_NOT_FOUND = -5;
export const ERR_NOT_IN_RANGE = -9;
export const ERR_NOT_OWNER = -1;
export const ERR_NO_BODYPART = -12;
export const ERR_NO_PATH = -2;
export const ERR_TIRED = -11;
export const EXTENSION_ENERGY_CAPACITY = 100;
export const EXTENSION_HITS = 100;
export class GameObject {
constructor(id) {
if (id) {
Object.defineProperty(this, "id", {
configurable: false,
enumerable: true,
value: id
});
}
}
get exists() {
return !!this.id && !!SystemStore.roomObjectsData[this.id];
}
get x() {
if (!this.exists) {
return;
}
return SystemStore.roomObjectsData[this.id].x;
}
get y() {
if (!this.exists) {
return;
}
return SystemStore.roomObjectsData[this.id].y;
}
findPathTo(pos, opts) {
return utils.findPath(this, pos, opts);
}
findInRange(positions, range) {
return utils.findInRange(this, positions, range);
}
findClosestByRange(positions) {
return utils.findClosestByRange(this, positions);
}
findClosestByPath(positions, opts) {
return utils.findClosestByPath(this, positions, opts);
}
getRangeTo(pos) {
return utils.getRange(this, pos);
}
toJSON() {
return {
id: this.id,
x: this.x,
y: this.y
};
}
}
export const HARVEST_POWER = 2;
export const HEAL = "heal";
export const HEAL_POWER = 12;
export const LEFT = 7;
export const MAX_CONSTRUCTION_SITES = 10;
export const MAX_CREEP_SIZE = 50;
export const MOVE = "move";
export const OBSTACLE_OBJECT_TYPES = ["creep", "tower", "constructedWall", "spawn", "extension", "link"];
export const OK = 0;
export class OwnedStructure extends Structure {
get my() {
if (!this.exists) {
return;
}
if (SystemStore.roomObjectsData[this.id].user) {
return SystemStore.roomObjectsData[this.id].user === SystemStore.playerName;
}
return undefined;
}
toJSON() {
return Object.assign(super.toJSON(), {
my: this.my
});
}
}
export const RAMPART_HITS = 10000;
export const RAMPART_HITS_MAX = 10000;
export const RANGED_ATTACK = "ranged_attack";
export const RANGED_ATTACK_DISTANCE_RATE = { 0: 1, 1: 1, 2: 0.4, 3: 0.1 };
export const RANGED_ATTACK_POWER = 10;
export const RANGED_HEAL_POWER = 4;
export const REPAIR_COST = 0.01;
export const REPAIR_POWER = 100;
export const RESOURCES_ALL = ["energy"];
export const RESOURCE_DECAY = 1000;
export const RESOURCE_ENERGY = "energy";
export const RIGHT = 3;
export const ROAD_HITS = 500;
export const ROAD_WEAROUT = 1;
export class Resource extends GameObject {
get amount() {
if (!this.exists) {
return;
}
return SystemStore.roomObjectsData[this.id][SystemStore.roomObjectsData[this.id].resourceType];
}
get resourceType() {
if (!this.exists) {
return;
}
return SystemStore.roomObjectsData[this.id].resourceType;
}
toJSON() {
return Object.assign(super.toJSON(), {
amount: this.amount,
resourceType: this.resourceType
});
}
}
export class GameObject {
constructor(id) {
if (id) {
Object.defineProperty(this, "id", {
configurable: false,
enumerable: true,
value: id
});
}
}
get exists() {
return !!this.id && !!SystemStore.roomObjectsData[this.id];
}
get x() {
if (!this.exists) {
return;
}
return SystemStore.roomObjectsData[this.id].x;
}
get y() {
if (!this.exists) {
return;
}
return SystemStore.roomObjectsData[this.id].y;
}
findPathTo(pos, opts) {
return utils.findPath(this, pos, opts);
}
findInRange(positions, range) {
return utils.findInRange(this, positions, range);
}
findClosestByRange(positions) {
return utils.findClosestByRange(this, positions);
}
findClosestByPath(positions, opts) {
return utils.findClosestByPath(this, positions, opts);
}
getRangeTo(pos) {
return utils.getRange(this, pos);
}
toJSON() {
return {
id: this.id,
x: this.x,
y: this.y
};
}
}
export const SOURCE_ENERGY_REGEN = 10;
export const SPAWN_ENERGY_CAPACITY = 1000;
export const SPAWN_HITS = 3000;
export const STRUCTURE_PROTOTYPES = {
StructureTower: "tower",
StructureSpawn: "spawn",
StructureRoad: "road",
StructureRampart: "rampart",
StructureExtension: "extension",
StructureWall: "constructedWall",
StructureContainer: "container"
};
export class Source extends GameObject {
get energy() {
if (!this.exists) {
return;
}
return SystemStore.roomObjectsData[this.id].energy;
}
get energyCapacity() {
if (!this.exists) {
return;
}
return SystemStore.roomObjectsData[this.id].energyCapacity;
}
toJSON() {
return Object.assign(super.toJSON(), {
energy: this.energy,
energyCapacity: this.energyCapacity
});
}
}
export class Structure extends GameObject {
get hits() {
if (!this.exists) {
return;
}
return SystemStore.roomObjectsData[this.id].hits;
}
get hitsMax() {
if (!this.exists) {
return;
}
return SystemStore.roomObjectsData[this.id].hitsMax;
}
toJSON() {
return Object.assign(super.toJSON(), {
hits: this.hits,
hitsMax: this.hitsMax
});
}
}
export class StructureContainer extends OwnedStructure {
get store() {
return new Store(SystemStore.roomObjectsData[this.id]);
}
}
export class StructureExtension extends OwnedStructure {
get store() {
return new Store(SystemStore.roomObjectsData[this.id]);
}
}
export class StructureRampart extends OwnedStructure {}
export class StructureRoad extends Structure {}
export class StructureSpawn extends OwnedStructure {
get store() {
return new Store(SystemStore.roomObjectsData[this.id]);
}
spawnCreep(body) {
if (!this.my) {
return returnError(C.ERR_NOT_OWNER);
}
if (SystemStore.roomObjectsData[this.id].spawning) {
return returnError(C.ERR_BUSY);
}
if (!body || !Array.isArray(body) || body.length === 0 || body.length > C.MAX_CREEP_SIZE) {
return returnError(C.ERR_INVALID_ARGS);
}
for (let i = 0; i < body.length; i++) {
if (!C.BODYPART_COST[body[i]]) {
return returnError(C.ERR_INVALID_ARGS);
}
}
const energyAvailable = Object.values(SystemStore.roomObjectsData)
.filter(i => (i.user === SystemStore.roomObjectsData[this.id].user && i.type == "spawn") || i.type == "extension")
.reduce((sum, i) => sum + (i.store.energy || 0), 0);
if (energyAvailable < body.reduce((sum, i) => sum + C.BODYPART_COST[i], 0)) {
return returnError(C.ERR_NOT_ENOUGH_ENERGY);
}
let object = new Creep();
Intents.set(this.id, "spawnCreep", { body, createRequest: getCreateRequest(object) });
if (SystemStore.codeCreatedAt > breakingChanges.returnCompositeObject) {
return { object };
} else {
return object;
}
}
}
export class StructureTower extends OwnedStructure {
get store() {
return new Store(SystemStore.roomObjectsData[this.id]);
}
get cooldown() {
return SystemStore.roomObjectsData[this.id].cooldown || 0;
}
attack(target) {
if (!this.exists) {
return;
}
if (!this.my) {
return C.ERR_NOT_OWNER;
}
if (this.cooldown > 0) {
return C.ERR_TIRED;
}
if (!target || !target.exists || !(target instanceof GameObject)) {
return C.ERR_INVALID_TARGET;
}
if (SystemStore.roomObjectsData[this.id].store.energy < C.TOWER_ENERGY_COST) {
return C.ERR_NOT_ENOUGH_ENERGY;
}
Intents.set(this.id, "attack", { id: target.id });
return C.OK;
}
heal(target) {
if (!this.exists) {
return;
}
if (!this.my) {
return C.ERR_NOT_OWNER;
}
if (this.cooldown > 0) {
return C.ERR_TIRED;
}
if (!target || !target.exists || !(target instanceof GameObject)) {
return C.ERR_INVALID_TARGET;
}
if (SystemStore.roomObjectsData[this.id].store.energy < C.TOWER_ENERGY_COST) {
return C.ERR_NOT_ENOUGH_ENERGY;
}
Intents.set(this.id, "heal", { id: target.id });
return C.OK;
}
}
export class StructureWall extends Structure {}
export const TERRAIN_SWAMP = 2;
export const TERRAIN_WALL = 1;
export const TOP = 1;
export const TOP_LEFT = 8;
export const TOP_RIGHT = 2;
export const TOUGH = "tough";
export const TOWER_CAPACITY = 50;
export const TOWER_COOLDOWN = 10;
export const TOWER_ENERGY_COST = 10;
export const TOWER_FALLOFF = 0.75;
export const TOWER_FALLOFF_RANGE = 20;
export const TOWER_HITS = 3000;
export const TOWER_OPTIMAL_RANGE = 5;
export const TOWER_POWER_ATTACK = 600;
export const TOWER_POWER_HEAL = 400;
export const TOWER_POWER_REPAIR = 800;
export const TOWER_RANGE = 50;
export const WALL_HITS = 10000;
export const WALL_HITS_MAX = 10000;
export const WORK = "work";
export const arenaInfo = { name: "Spawn and Swamp", level: 1, season: "alpha" };
export const constants = {
ATTACK: "attack",
ATTACK_POWER: 30,
BODYPART_COST: { move: 50, work: 100, attack: 80, carry: 50, heal: 250, ranged_attack: 150, tough: 10 },
BODYPART_HITS: 100,
BOTTOM: 5,
BOTTOM_LEFT: 6,
BOTTOM_RIGHT: 4,
BUILD_POWER: 5,
CARRY: "carry",
CARRY_CAPACITY: 50,
CONSTRUCTION_COST: {
StructureTower: 5000,
StructureExtension: 200,
StructureRoad: 10,
StructureContainer: 100,
StructureWall: 100,
StructureRampart: 200,
StructureSpawn: 3000
},
CONSTRUCTION_COST_ROAD_SWAMP_RATIO: 5,
CONSTRUCTION_COST_ROAD_WALL_RATIO: 150,
CONTAINER_CAPACITY: 2000,
CONTAINER_HITS: 300,
CREEP_SPAWN_TIME: 3,
DISMANTLE_COST: 0.005,
DISMANTLE_POWER: 50,
ERR_BUSY: -4,
ERR_FULL: -8,
ERR_INVALID_ARGS: -10,
ERR_INVALID_TARGET: -7,
ERR_NAME_EXISTS: -3,
ERR_NOT_ENOUGH_ENERGY: -6,
ERR_NOT_ENOUGH_EXTENSIONS: -6,
ERR_NOT_ENOUGH_RESOURCES: -6,
ERR_NOT_FOUND: -5,
ERR_NOT_IN_RANGE: -9,
ERR_NOT_OWNER: -1,
ERR_NO_BODYPART: -12,
ERR_NO_PATH: -2,
ERR_TIRED: -11,
EXTENSION_ENERGY_CAPACITY: 100,
EXTENSION_HITS: 100,
HARVEST_POWER: 2,
HEAL: "heal",
HEAL_POWER: 12,
LEFT: 7,
MAX_CONSTRUCTION_SITES: 10,
MAX_CREEP_SIZE: 50,
MOVE: "move",
OBSTACLE_OBJECT_TYPES: ["creep", "tower", "constructedWall", "spawn", "extension", "link"],