-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunctions.game.js
942 lines (847 loc) · 34.9 KB
/
functions.game.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
global.getMasterSpawn = function (roomName) {
return Game.rooms[roomName].memory.masterSpawn;
};
global.terminalTransfer = function (transferResource, transferAmount, targetRoomName, transferFlag) {
// transfer resources to remote room from whatever room(s) is cheapest
var roomCandidates = new Array();
var tempArray = new Array();
var resourceTotal = 0;
if (arguments.length == 0) {
return "terminalTransfer (transferResource, transferAmount, targetRoomName, transferFlag) --> terminalTransfer(\"Z\", 10000, \"W16S47\", false)";
}
if (transferAmount < 100) {
return "Minimal amount for terminal transfers are 100 units.";
}
for (var r in myRooms) {
if (Game.rooms[r].terminal != undefined && Game.rooms[r].storage != undefined && Game.rooms[r].storage.owner.username == playerUsername) {
//Fill candidate array with rooms
var roomResourceTotal = 0;
var roomArray = [];
// Add resource in storage
if (Game.rooms[r].storage != undefined && Game.rooms[r].storage.store[transferResource] != undefined) {
roomResourceTotal += Game.rooms[r].storage.store[transferResource];
}
// Add resource in containers
tempArray = Game.rooms[r].memory.roomArrayContainers;
var container;
for (var s in tempArray) {
container = Game.getObjectById(tempArray[s]);
if (container != undefined) {
if (container.store[transferResource] != undefined) {
roomResourceTotal += container.store[transferResource];
}
}
}
if (transferResource == RESOURCE_ENERGY) {
// Add resource in links
tempArray = Game.rooms[r].memory.roomArrayLinks;
for (var s in tempArray) {
container = Game.getObjectById(tempArray[s]);
if (container != undefined) {
roomResourceTotal += Game.getObjectById(tempArray[s]).energy;
}
}
}
if (roomResourceTotal > 0 && Game.rooms[r].name != targetRoomName) {
roomArray["name"] = Game.rooms[r].name;
roomArray["volume"] = roomResourceTotal;
if (roomResourceTotal > transferAmount) {
roomArray["totalCost"] = Game.market.calcTransactionCost(transferAmount, Game.rooms[r].name, targetRoomName);
}
else {
roomArray["totalCost"] = Game.market.calcTransactionCost(roomResourceTotal, Game.rooms[r].name, targetRoomName);
}
roomArray["cost"] = Game.market.calcTransactionCost(100, roomArray.name, targetRoomName);
if (transferFlag == false) {
console.log(roomArray.name + ": " + roomResourceTotal + " of " + transferResource + " (energy factor: " + roomArray.cost + ")");
}
roomCandidates.push(roomArray);
resourceTotal += roomResourceTotal;
}
}
}
var totalVolume = 0;
var totalCost = 0;
if (roomCandidates.length == 0) {
return "No rooms with " + transferResource + " found.";
}
else if (resourceTotal < transferAmount) {
return "Not enough " + transferResource + " found.";
}
else {
// There are rooms holding enough of the transfer resource
var candidatesByCost = _.sortBy(roomCandidates,"cost");
for (var c in candidatesByCost) {
if (Game.rooms[candidatesByCost[c].name].memory.terminalTransfer == undefined) {
if (candidatesByCost[c].volume > transferAmount) {
if (transferFlag == false) {
console.log("Terminal Transfer Preview for room " + candidatesByCost[c].name + " // " + targetRoomName + ":" + transferAmount + ":" + transferResource + ":TerminalTransfer // Total Energy Cost: " + candidatesByCost[c].totalCost);
}
else if (transferFlag == true) {
Game.rooms[candidatesByCost[c].name].memory.terminalTransfer = targetRoomName + ":" + transferAmount + ":" + transferResource + ":TerminalTransfer";
//console.log(transferAmount + " " + transferResource + " scheduled from room " + candidatesByCost[c].name + " to room " + targetRoomName + " for " + candidatesByCost[c].totalCost + " energy.");
}
totalVolume += transferAmount;
totalCost += candidatesByCost[c].totalCost;
break;
}
else {
if (transferFlag == false) {
console.log("Terminal Transfer Preview for room " + candidatesByCost[c].name + " // " + targetRoomName + ":" + candidatesByCost[c].volume + ":" + transferResource + ":TerminalTransfer // Total Energy Cost: " + candidatesByCost[c].totalCost);
}
else if (transferFlag == true) {
Game.rooms[candidatesByCost[c].name].memory.terminalTransfer = targetRoomName + ":" + candidatesByCost[c].volume + ":" + transferResource + ":TerminalTransfer";
//console.log(candidatesByCost[c].volume + " " + transferResource + " scheduled from room " + candidatesByCost[c].name + " to room " + targetRoomName + " for " + candidatesByCost[c].totalCost + " energy.");
}
totalVolume += candidatesByCost[c].volume;
totalCost += candidatesByCost[c].totalCost;
transferAmount -= candidatesByCost[c].volume;
}
}
}
if (transferFlag == "cost") {
return totalCost;
}
return "OK";
}
};
global.terminalTransferX = function (transferResource, transferAmount, sourceRoomName, targetRoomName, transferFlag) {
// transfer resources to from source to target
var roomCandidates = new Array();
var sourceRoom = Game.rooms[sourceRoomName];
if (arguments.length == 0) {
return "terminalTransferX (transferResource, transferAmount, sourceRoomName, targetRoomName, transferFlag) --> terminalTransfer(\"Z\", 10000, \"W18S49\", \"W16S47\", false)";
}
if (transferAmount < 100) {
return "Minimal amount for terminal transfers are 100 units.";
}
if (sourceRoom.memory.terminalTransfer != undefined) {
return "There is already an ongoing terminal transfer in room " + sourceRoomName + ".";
}
var totalCost = 0;
if (sourceRoom.storage == undefined || sourceRoom.terminal == undefined || (sourceRoom.storage.store[transferResource] + sourceRoom.terminal.store[transferResource]) < transferAmount) {
return "Error scheduling terminal transfer job.";
}
else {
if (transferFlag == false) {
console.log("Terminal Transfer Preview for room " + sourceRoom.name + " // " + targetRoomName + ":" + transferAmount + ":" + transferResource + ":TerminalTransfer // Total Energy Cost: " + Game.market.calcTransactionCost(transferAmount, sourceRoomName, targetRoomName));
}
else if (transferFlag == true) {
sourceRoom.memory.terminalTransfer = targetRoomName + ":" + transferAmount + ":" + transferResource + ":TerminalTransfer";
//console.log(transferAmount + " " + transferResource + " scheduled from room " + sourceRoomName + " to room " + targetRoomName + " for " + Game.market.calcTransactionCost(transferAmount, sourceRoomName, targetRoomName) + " energy.");
}
else {
return "Transfer Flag missing.";
}
if (transferFlag == "cost") {
return totalCost;
}
return "OK";
}
};
global.listStorages = function (displayResource) {
var returnstring = "<table><tr><th>Resource </th>";
var resourceTable = [];
var total = [];
//Prepare header row
for (var r in myRooms) {
if (Game.rooms[r].storage != undefined && Game.rooms[r].storage.owner.username == playerUsername) {
returnstring = returnstring.concat("<th>" + Game.rooms[r].name + " </th>");
for (var res in myRooms[r].storage.store) {
if (resourceTable.indexOf(res) == -1) {
resourceTable.push(res);
}
}
}
}
returnstring = returnstring.concat("</tr>");
resourceTable = _.sortBy(resourceTable, function (res) {return res.length;});
for (res in resourceTable) {
if (arguments.length == 0 || displayResource == resourceTable[res]) {
returnstring = returnstring.concat("<tr></tr><td>" + resourceTable[res] + " </td>");
let c = -1;
for (var r in myRooms) {
if (Game.rooms[r].storage != undefined && Game.rooms[r].storage.owner.username == playerUsername) {
c++;
var amount;
var color;
if (Game.rooms[r].storage.store[resourceTable[res]] == undefined) {
amount = 0;
}
else {
amount = Game.rooms[r].storage.store[resourceTable[res]];
}
if (amount < Game.rooms[r].memory.resourceLimits[resourceTable[res]].maxStorage) {
color = "#ff3333";
}
else if (amount > Game.rooms[r].memory.resourceLimits[resourceTable[res]].maxStorage) {
color ="#00ff00"
}
else {
color = "#aaffff";
}
returnstring = returnstring.concat("<td><font color='" + color + "'>" + prettyInt(amount) + " </font></td>");
if (total[c] == undefined) {
total[c] = amount;
}
else {
total[c] += amount;
}
}
}
returnstring = returnstring.concat("</tr>");
}
}
returnstring = returnstring.concat("<tr></tr><td>Total </td>");
for (let c in total) {
returnstring = returnstring.concat("<td>" + prettyInt(total[c]) + " </td>");
}
returnstring = returnstring.concat("</tr></table>");
return returnstring;
};
global.prettyInt = function (int) {
var string = int.toString();
var numbers = string.length;
var rest = numbers % 3;
var returnString = "";
if (rest > 0) {
returnString = string.substr(0, rest);
if (numbers > 3) {
returnString += "'";
}
}
numbers -= rest;
while (numbers > 0) {
returnString += string.substr(rest,3);
if (numbers > 3) {
returnString += "'";
}
rest += 3;
numbers -= 3;
}
return returnString;
};
global.listLimits = function (limitType, displayResource) {
if (arguments.length == 0) {
return "listLimits (limitTyoe, [displayResource]) - Known limit types: \"market\", \"storage\", \"production\", \"terminal\"";
}
var returnstring = "<table><tr><th>Resource </th>";
var resourceTable = [];
if (limitType == "market") {
limitType = "minMarket"
}
else if (limitType == "production") {
limitType = "minProduction"
}
else if (limitType == "terminal") {
limitType = "minTerminal"
}
else if (limitType == "storage") {
limitType = "maxStorage"
}
else {return "Invalid limit type."}
//Prepare header row
for (var r in myRooms) {
if (Game.rooms[r].storage != undefined && Game.rooms[r].storage.owner.username == playerUsername) {
returnstring = returnstring.concat("<th>" + Game.rooms[r].name + " </th>");
for (var res in myRooms[r].memory.resourceLimits) {
if (resourceTable.indexOf(res) == -1) {
resourceTable.push(res);
}
}
}
}
returnstring = returnstring.concat("</tr>");
resourceTable = _.sortBy(resourceTable, function (res) {return res.length;});
for (res in resourceTable) {
if (arguments.length == 1 || displayResource == resourceTable[res]) {
var tempstring ="<tr><td>" + resourceTable[res] + " </td>";
var tempsum = 0;
for (var r in myRooms) {
if (Game.rooms[r].storage != undefined && Game.rooms[r].storage.owner.username == playerUsername) {
tempstring = tempstring.concat("<td>" + prettyInt(Game.rooms[r].memory.resourceLimits[resourceTable[res]][limitType]) + " </td>");
tempsum += Game.rooms[r].memory.resourceLimits[resourceTable[res]].maxStorage;
}
}
if (tempsum > 0) {
returnstring = returnstring.concat(tempstring + "</tr>");
}
}
}
returnstring = returnstring.concat("</table>");
return returnstring;
};
global.setLimit = function(roomName, type, resource, limit) {
if (arguments.length == 0) {
return "setLimit (roomName, limitType, resource, limit) - Known limit types: \"market\", \"storage\", \"production\", \"terminal\"";
}
var roomNames = [];
var resources = [];
if (roomName == "*") {
for (var t in myRooms) {
roomNames.push(myRooms[t].name);
}
}
else {
roomNames.push(roomName);
}
if (resource == "*") {
for (var t in RESOURCES_ALL) {
resources.push(RESOURCES_ALL[t]);
}
}
else {
resources.push(resource);
}
for (var i in roomNames) {
for (let m in resources) {
switch (type) {
case "market":
Game.rooms[roomNames[i]].memory.resourceLimits[resources[m]].minMarket = limit;
break;
case "terminal":
Game.rooms[roomNames[i]].memory.resourceLimits[resources[m]].minTerminal = limit;
break;
case "storage":
Game.rooms[roomNames[i]].memory.resourceLimits[resources[m]].maxStorage = limit;
break;
case "production":
Game.rooms[roomNames[i]].memory.resourceLimits[resources[m]].minProduction = limit;
break;
case "*":
Game.rooms[roomNames[i]].memory.resourceLimits[resources[m]].minMarket = limit;
Game.rooms[roomNames[i]].memory.resourceLimits[resources[m]].minTerminal = limit;
Game.rooms[roomNames[i]].memory.resourceLimits[resources[m]].maxStorage = limit;
Game.rooms[roomNames[i]].memory.resourceLimits[resources[m]].minProduction = limit;
default:
return "Unknown type";
}
console.log("Room " + Game.rooms[roomNames[i]].name + " has set the " + type + " limit for " + resources[m] + " to " + limit + ".");
}
}
return "OK";
};
global.checkTerminalLimits = function (room, resource) {
// Check if terminal has exactly what it needs. If everything is as it should be true is returned.
// If material is missing or too much is in terminal, an array will be returned with the following format:
// delta.type = Type of resource / delta.amount = volume (positive number means surplus material)
//Check terminal limits
var uplift = 0;
var delta = {};
delta["type"] = resource;
if (room.memory.resourceLimits == undefined || room.terminal == undefined || room.storage == undefined) {
delta["amount"] = 0;
return delta;
}
var roomLimits = room.memory.resourceLimits;
if (roomLimits[resource] != undefined && room.terminal.store[resource] != undefined) {
delta.amount = room.terminal.store[resource] - roomLimits[resource].minTerminal;
}
else if (room.terminal.store[resource] == undefined) {
delta.amount = roomLimits[resource].minTerminal;
}
else {
delta.amount = 0
}
//Check market selling orders to add minerals to terminal
if (Object.keys(Game.market.orders).length > 0) {
//Look through orders to determine whether additional material is needed in terminal
var relevantOrders = _.filter(Game.market.orders,function (order) {
if (order.resourceType == resource && order.roomName == room.name && order.type == ORDER_SELL) {return true}
else {return false}
});
if (relevantOrders.length > 0) {
for (let o in relevantOrders) {
if (relevantOrders[o].remainingAmount > TERMINALMARKETSTORE) {
uplift += TERMINALMARKETSTORE;
}
else {
uplift += relevantOrders[o].remainingAmount;
}
}
delta.amount -= uplift;
}
}
//Check single buying orders to add energy to terminal
if (Memory.buyOrder != undefined && Memory.buyRoom == room.name && resource == RESOURCE_ENERGY) {
let info = Memory.buyOrder.split(":");
let order = Game.market.getOrderById(info[1]);
if (order != null) {
if (info[0] > 500) {
info[0] = 500;
}
if (info[0] > order.amount) {
info[0] = order.amount;
}
let plusEnergy = Game.market.calcTransactionCost(info[0], Memory.buyRoom, order.roomName);
delta.amount -= plusEnergy;
}
}
return delta;
};
global.checkStorageLimits = function(room, resource) {
// Check if storage has exactly what it needs. Return delta to maxStorage
// If everything is as it should be 0 is returned.
// If material is missing a negative amount will be returned
// If there is surplus a positive amount will be returned
var terminalDelta = 0;
if (room.storage == undefined) {
return 0;
}
terminalDelta = checkTerminalLimits(room, resource);
if (room.storage.store[resource] != undefined) {
return (terminalDelta.amount + room.storage.store[resource] - room.memory.resourceLimits[resource].maxStorage)
}
else {
return (terminalDelta.amount - room.memory.resourceLimits[resource].maxStorage);
}
};
global.whatIsLackingFor = function(room, amount, resource) {
//Return object [resource, amount] with lowest-tier resource missing in room for target resource
var returnArray = {};
returnArray.resource = resource;
var components = [];
var targetResourceDescription = mineralDescriptions[resource];
if (targetResourceDescription == undefined) {
console.log(resource + " not found in mineralDescriptions!");
}
returnArray["amount"] = amount;
if (targetResourceDescription.tier == 0) {
//Tier 0 resource
return returnArray;
}
else {
// Begin drill-down
var OKcount = 0;
do {
components[0] = targetResourceDescription.component1;
components[1] = targetResourceDescription.component2;
for (let c in components) {
if (room.storage.store[components[c]] == undefined || room.storage.store[components[c]] < returnArray.amount) {
// not enough of this component
targetResourceDescription = mineralDescriptions[components[c]];
returnArray.resource = components[c];
if (room.storage.store[components[c]] != undefined) {
returnArray.amount = amount - room.storage.store[components[c]];
}
else {
returnArray.amount = amount;
}
}
else {
OKcount++;
}
}
}
while (OKcount < 2 && targetResourceDescription.tier > 0);
return returnArray;
}
};
global.buy = function (orderID, amount) {
if (arguments.length == 0) {
return "buy (orderID, amount)";
}
var order = Game.market.getOrderById(orderID);
if (order == null) {
return "Invalid order ID!"
}
if (order.remainingAmount < amount) {
return "Order does not contain enough material!"
}
if (Game.market.credits < order.price * amount) {
return "Not enough credits!"
}
if (Memory.buyOrder != undefined) {
return "Active buy order found: " + Game.memory.buyOrder;
}
Memory.buyOrder = amount + ":" + order.id;
return "Buy queue created!";
};
global.sell = function (orderID, amount, roomName) {
if (arguments.length == 0) {
return "sell (orderID, amount, roomName)";
}
let order = Game.market.getOrderById(orderID);
if (order == null) {
return "Invalid order ID!"
}
if (Game.rooms[roomName].memory.terminalTransfer == undefined) {
Game.rooms[roomName].memory.terminalTransfer = order.id + ":" + amount + ":" + order.resourceType + ":MarketOrder";
return "Selling transfer scheduled.";
}
else {
return "Ongoing terminal transfer found. Try later.";
}
};
global.sellBulk = function (amount, resource) {
// Sell as much as possible as fast as possible, no matter the energy costs or price
if (arguments.length == 0) {
return "sellBulk (amount, resource)";
}
let amountBuffer = amount;
let orders = Game.market.getAllOrders({type: ORDER_BUY, resourceType: resource});
if (orders.length > 0) {
orders = _.sortBy(orders, "price");
orders = _.sortBy(orders, "amount");
orders.reverse();
let orderIndex = 0;
for (let r in myRooms) {
if (orders[orderIndex] != undefined && myRooms[r].terminal != undefined && myRooms[r].memory.terminalTransfer == undefined) {
let sellAmount;
if (orders[orderIndex].amount > amount) {
sellAmount = amount;
}
else {
sellAmount = orders[orderIndex].amount;
}
if (sellAmount <= 0) {
break;
}
else {
sell(orders[orderIndex].id, sellAmount, r);
}
amount-= sellAmount;
orderIndex++;
}
}
}
if (amount < 0) {
amount = 0;
}
return (amountBuffer - amount) + " units queued for sale.";
};
global.sellHigh = function (amount, resource) {
// Sell as much as possible as expensive as possible, no matter the energy costs
if (arguments.length == 0) {
return "sellHigh (amount, resource)";
}
let amountBuffer = amount;
let orders = Game.market.getAllOrders({type: ORDER_BUY, resourceType: resource});
if (orders.length > 0) {
orders = _.sortBy(orders, "amount");
orders = _.sortBy(orders, "price");
orders.reverse();
let orderIndex = 0;
for (let r in myRooms) {
if (myRooms[r].terminal != undefined && myRooms[r].memory.terminalTransfer == undefined) {
let sellAmount;
if (orders[orderIndex].amount > amount) {
sellAmount = amount;
}
else {
sellAmount = orders[orderIndex].amount;
}
if (sellAmount <= 0) {
break;
}
else {
sell(orders[orderIndex].id, sellAmount, r);
}
amount-= sellAmount;
orderIndex++;
}
}
}
if (amount < 0) {
amount = 0;
}
return (amountBuffer - amount) + " units queued for sale.";
};
global.sellOrder = function (amount, resource, roomName, price) {
if (arguments.length == 0) {
return "sellOrder (amount, resource, roomName, price)";
}
if (Game.rooms[roomName].storage != undefined && Game.rooms[roomName].storage.store[resource] >= amount) {
if (Game.market.createOrder(ORDER_SELL, resource, price, amount, roomName) == OK) {
return "Sell order created!";
}
}
else {
return "Room " + roomName + " is not able to sell this resource.";
}
};
global.buyOrder = function (amount, resource, roomName, price) {
if (arguments.length == 0) {
return "buyOrder (amount, resource, roomName, price)";
}
if (Game.market.createOrder(ORDER_BUY, resource, price, amount, roomName) == OK) {
return "Buy order created!";
}
};
global.produce = function (roomName, amount, resource) {
if (arguments.length == 0) {
return "produce (roomName, amount, resource)";
}
Game.rooms[roomName].memory.labTarget = amount + ":" + resource;
return "OK";
};
global.addBoostLab = function (roomName, labID) {
if (arguments.length == 0) {
return "addBoostLab (roomName, labID)";
}
var lab = Game.getObjectById(labID);
if (lab != null) {
var boostLabList;
var room = Game.rooms[roomName];
if (room.memory.boostLabs == undefined) {
boostLabList = [];
}
else {
boostLabList = room.memory.boostLabs;
}
boostLabList.push(labID);
room.memory.boostLabs = boostLabList;
return "Lab added as boost lab.";
}
};
global.listBoostLabs = function () {
var returnstring = "<table><tr><th>Room </th><th>Entry </th><th>Lab ID (position) </th></tr>";
for (let r in myRooms) {
returnstring = returnstring.concat("<tr><td>" + myRooms[r].name + " </td>");
for (let l in myRooms[r].memory.boostLabs) {
let lab = Game.getObjectById(myRooms[r].memory.boostLabs[l]);
returnstring = returnstring.concat("<td> " + l + " </td><td>" + lab.id + " (x=" + lab.pos.x + "/y=" + lab.pos.y + ")" + " </td>");
}
returnstring = returnstring.concat("</tr>");
}
returnstring = returnstring.concat("</table>");
return returnstring;
};
global.delBoostLab = function (roomName, entryNr) {
if (arguments.length == 0) {
return "delBoostLab (roomName, entryNr)";
}
Game.rooms[roomName].memory.boostLabs.splice(entryNr, 1);
return "Boost Lab removed.";
};
global.addBoost = function (roomName, role, mineralType, volume) {
if (arguments.length == 0) {
return "addBoost (roomName, role, mineralType, volume)";
}
var boostList;
var room = Game.rooms[roomName];
if (room.memory.boostList == undefined) {
boostList = [];
}
else {
boostList = room.memory.boostList;
}
var boostEntry = {};
boostEntry.role = role;
boostEntry.mineralType = mineralType;
boostEntry.volume = volume;
boostList.push(boostEntry);
room.memory.boostList = boostList;
return "Boost entry added.";
};
global.delBoost = function (roomName, entryNr) {
if (arguments.length == 0) {
return "delBoost (roomName, entryNr)";
}
Game.rooms[roomName].memory.boostList.splice(entryNr, 1);
return "Boost entry removed.";
};
global.listBoost = function (roomName) {
if (arguments.length == 0) {
return "listBoost (roomName)";
}
var roomList = [];
if (roomName == "*") {
for (let u in myRooms) {
roomList.push(u);
}
}
else {
roomList.push(roomName)
}
var returnstring = "";
for (roomName in roomList) {
var roles = [];
var boostMinerals = [];
var volumes = [];
for (let e in myRooms[roomList[roomName]].memory.boostList) {
if (roles.indexOf(Game.rooms[roomList[roomName]].memory.boostList[e].role) == -1) {
roles.push(Game.rooms[roomList[roomName]].memory.boostList[e].role);
boostMinerals.push(Game.rooms[roomList[roomName]].memory.boostList[e].mineralType);
volumes.push(Game.rooms[roomList[roomName]].memory.boostList[e].volume);
}
}
if (boostMinerals.length > 0) {
returnstring = returnstring.concat(roomList[roomName] + ":<br><table><tr><th>Entry </th><th>Role </th><th>Boost </th><th>Volume </th></tr>");
for (let r in roles) {
returnstring = returnstring.concat("<tr><td>" + r + ": </td>");
returnstring = returnstring.concat("<td>" + roles[r] + " </td><td>" + boostMinerals[r] + " </td><td>" + volumes[r] + " </td>");
returnstring = returnstring.concat("</tr>");
}
returnstring = returnstring.concat("</table><br>");
}
}
return returnstring;
};
global.activeTerminals = function () {
let entries = 0;
let returnString ="";
for (r in myRooms) {
if (myRooms[r].memory.terminalTransfer != undefined) {
entries ++;
var info = myRooms[r].memory.terminalTransfer.split(":");
let targetRoom = info[0];
let amount = parseInt(info[1]);
let resource = info[2];
let comment = info[3];
if (comment == "MarketOrder") {
returnString = returnString.concat(myRooms[r].name + ": " + "Sending " + amount + " " + resource + " to room " + targetRoom + "<br>");
}
else {
returnString = returnString.concat(myRooms[r].name + ": " + "Sending " + amount + " " + resource + " to room " + targetRoom + "<br>");
}
}
}
if (entries == 0) {
returnString = "No active terminals.";
}
return returnString;
};
global.activeLabs = function () {
let entries = 0;
let returnString ="";
for (r in myRooms) {
if (myRooms[r].memory.labOrder != undefined) {
entries ++;
var info = myRooms[r].memory.labOrder.split(":");
let amount = parseInt(info[0]);
let resource1 = info[1];
let resource2 = info[2];
returnString = returnString.concat(myRooms[r].name + ": " + "Reaction -> [ " + amount + " " + resource1 + " + " + resource2 + " ]<br>");
}
}
if (entries == 0) {
returnString = "No active labs.";
}
return returnString;
};
moveReusePath = function(express) {
if (express == true && Game.cpu.bucket > 99) {
return 5;
}
let minSteps = 10, maxSteps = 60;
let range = maxSteps - minSteps;
return minSteps + Math.floor((1 - (Game.cpu.bucket / 10000)) * range);
};
isHostile = function (creep) {
if (allies.indexOf(creep.owner.username) == -1 && creep.owner.username != playerUsername) {
//Not own and not allied creep
return true;
}
else {
return false;
}
};
String.prototype.hashCode = function(){
let hash = 0;
if (this.length == 0) {
return hash;
}
for (let i = 0; i < this.length; i++) {
let char = this.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
};
global.deleteFlowPath = function () {
delete Memory.flowPath;
return "OK";
};
global.roomCallback = function (roomName) {
let room = Game.rooms[roomName];
if (!room) {
return;
}
let costs = new PathFinder.CostMatrix();
//Set costs for structures
let structures = room.find(FIND_STRUCTURES);
for (let s in structures) {
if (structures[s].structureType == STRUCTURE_ROAD) {
costs.set(structures[s].pos.x, structures[s].pos.y, 1);
}
else if (structures[s].structureType == STRUCTURE_CONTROLLER || structures[s].structureType == STRUCTURE_EXTRACTOR) {
//Find squares around controllers (claimers) and extractors (miners)
let top = structures[s].pos.y - 1;
let bottom = structures[s].pos.y + 1;
let right = structures[s].pos.x - 1;
let left = structures[s].pos.x + 1;
let areaInfo = room.lookForAtArea(LOOK_TERRAIN, top, left, bottom, right, true);
for (let a in areaInfo) {
//Check if square is walkable. If it is, set costs to 0xcc. If it isn't, leave the costs be
if (areaInfo[a].structure != "wall") {
costs.set(areaInfo[a].x, areaInfo[a].y, 0xcc)
}
}
}
else if (structures[s].structureType != STRUCTURE_CONTAINER && structures[s].structureType != STRUCTURE_RAMPART) {
costs.set(structures[s].pos.x, structures[s].pos.y, 0xff);
}
}
//Find flags for stationaryRemoteHarvesters, stationaryHarvesters
let flags = room.find(FIND_FLAGS, {filter: (f) => f.memory.function == "haulEnergy" || f.memory.function == "narrowSource" || f.memory.function == "remoteController"});
for (let f in flags) {
let top = flags[f].pos.y - 1;
let bottom = flags[f].pos.y + 1;
let right = flags[f].pos.x - 1;
let left = flags[f].pos.x + 1;
costs.set(flags[f].pos.x, flags[f].pos.y, 0xdd);
let areaInfo = room.lookForAtArea(LOOK_TERRAIN, top, left, bottom, right, true);
for (let a in areaInfo) {
//Check if square is walkable. If it is, set costs to 0xcc. If it isn't, leave the costs be
if (areaInfo[a].structure != "wall") {
costs.set(areaInfo[a].x, areaInfo[a].y, 0xcc);
}
}
}
room.costMatrix = room.costMatrix || costs;
return room.costMatrix;
};
global.listCreeps = function (displayRole) {
var returnstring = "<table><tr><th>Role </th>";
var roleTable = [];
var total = [];
//Prepare header row
for (var r in myRooms) {
returnstring = returnstring.concat("<th>" + Game.rooms[r].name + " </th>");
let roomCreeps = _.filter(Game.creeps, function (c) { return c.memory.homeroom == myRooms[r].name; });
for (let c in roomCreeps) {
if (roleTable.indexOf(roomCreeps[c].memory.role) == -1) {
roleTable.push(roomCreeps[c].memory.role);
}
}
}
returnstring = returnstring.concat("</tr>");
roleTable.sort();
for (let role in roleTable) {
if (arguments.length == 0 || displayRole == roleTable[role]) {
returnstring = returnstring.concat("<tr></tr><td>" + roleTable[role] + " </td>");
let c = -1;
for (var r in myRooms) {
c++;
let amount;
let roleCreeps = _.filter(Game.creeps, function (c) { return (c.memory.role == roleTable[role] && c.memory.homeroom == myRooms[r].name);});
amount = roleCreeps.length;
returnstring = returnstring.concat("<td>" + prettyInt(amount) + " </td>");
if (total[c] == undefined) {
total[c] = amount;
}
else {
total[c] += amount;
}
}
returnstring = returnstring.concat("</tr>");
}
}
returnstring = returnstring.concat("<tr></tr><td>Total </td>");
for (let c in total) {
returnstring = returnstring.concat("<td>" + prettyInt(total[c]) + " </td>");
}
returnstring = returnstring.concat("</tr></table>");
return returnstring;
};