-
Notifications
You must be signed in to change notification settings - Fork 3
/
role.distributor.js
223 lines (212 loc) · 9.99 KB
/
role.distributor.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
Creep.prototype.roleDistributor = function() {
var nuker = Game.getObjectById(this.room.memory.roomArray.nukers[0]);
if (this.room.memory.terminalTransfer != undefined) {
//ongoing terminal transfer
if (_.sum(this.carry) > 0) {
//Creep full
if (this.pos.getRangeTo(this.room.terminal) > 1) {
this.moveTo(this.room.terminal, {reusePath: moveReusePath()});
}
else {
// Dump everything into terminal
for (var res2 in this.carry) {
this.transfer(this.room.terminal, res2);
}
}
}
else {
//Creep empty
var transferAmount;
var targetRoom;
var transferResource;
var energyCost;
var packageVolume;
var info = this.room.memory.terminalTransfer; // Format: ROOM:AMOUNT:RESOURCE:COMMENT W21S38:100:Z:TestTransfer
info = info.split(":");
targetRoom = info[0];
transferAmount = parseInt(info[1]);
transferResource = info[2];
if (transferAmount > this.carryCapacity) {
packageVolume = this.carryCapacity;
}
else {
packageVolume = transferAmount;
}
if (info[3] == "MarketOrder") {
var order = Game.market.getOrderById(targetRoom);
if (order != null) {
energyCost = Game.market.calcTransactionCost(packageVolume, this.room.name, order.roomName);
}
else {
//Order invalid
delete this.room.memory.terminalTransfer;
}
}
else {
energyCost = Game.market.calcTransactionCost(packageVolume, this.room.name, targetRoom);
}
// Check resource status
if (this.room.terminal.store[transferResource] >= packageVolume) {
//Check for energy level
if ((transferResource != RESOURCE_ENERGY && this.room.terminal.store[RESOURCE_ENERGY] < energyCost + packageVolume)
|| transferResource == RESOURCE_ENERGY && this.room.terminal.store[RESOURCE_ENERGY] + transferAmount > energyCost) {
//Get energy
if (energyCost > this.carryCapacity) {
energyCost = this.carryCapacity;
}
if(this.withdraw(this.room.storage, RESOURCE_ENERGY, energyCost) == ERR_NOT_IN_RANGE) {
this.moveTo(this.room.storage, {reusePath: moveReusePath()});
}
}
else if (this.room.terminal.store[transferResource] < AUTOSELL_PACKETSIZE) {
// Get transfer resource
if(this.withdraw(this.room.storage, transferResource, packageVolume) == ERR_NOT_IN_RANGE) {
this.moveTo(this.room.storage, {reusePath: moveReusePath()});
}
}
}
else {
// Get transfer resource
if(this.withdraw(this.room.storage, transferResource, packageVolume) == ERR_NOT_IN_RANGE) {
this.moveTo(this.room.storage, {reusePath: moveReusePath()});
}
}
}
}
else if (this.checkTerminalLimits(RESOURCE_GHODIUM).amount == 0 && this.room.memory.terminalTransfer == undefined && nuker != undefined
&& nuker.ghodium < nuker.ghodiumCapacity && (this.room.storage.store[RESOURCE_GHODIUM] != undefined || this.carry[RESOURCE_GHODIUM] != undefined)) {
//No terminal transfer pending, nuker in need of Ghodium and storage has enough of it
if (this.storeAllBut(RESOURCE_GHODIUM) == true) {
if (_.sum(this.carry) < this.carryCapacity && this.room.storage.store[RESOURCE_GHODIUM] > 0) {
if (this.withdraw(this.room.storage, RESOURCE_GHODIUM) == ERR_NOT_IN_RANGE) {
this.moveTo(this.room.storage, {reusePath: moveReusePath()});
}
}
else {
if (this.transfer(nuker, RESOURCE_GHODIUM) == ERR_NOT_IN_RANGE) {
this.moveTo(nuker, {reusePath: moveReusePath()});
}
}
}
}
else if (this.room.storage != undefined && this.room.terminal != undefined) {
//Nothing special going on check for terminal levels
var terminalDelta;
if (this.room.memory.terminalDelta == undefined || Game.time % 10 == 0 || this.room.memory.terminalDelta != 0) {
terminalDelta = 0;
for (var res in this.room.terminal.store) {
delta = this.checkTerminalLimits(res);
terminalDelta += Math.abs(delta.amount);
}
for (var res in this.room.storage.store) {
delta = this.checkTerminalLimits(res);
terminalDelta += Math.abs(delta.amount);
}
}
else {
terminalDelta = this.room.memory.terminalDelta;
}
if (terminalDelta == 0) {
//Everything perfect!
if (this.storeAllBut(RESOURCE_ENERGY) == true) {
this.roleEnergyTransporter();
}
}
else {
if (_.sum(this.carry) > 0) {
//Creep full
var terminalResources = [];
for (var res in this.carry) {
delta = this.checkTerminalLimits(res);
if (delta.amount < 0 && this.carry[res] > 0) {
//Terminal needs material
var load = Math.abs(delta.amount);
if (load > this.carry[res]) {
load = this.carry[res];
}
if (this.transfer(this.room.terminal, res, load) == ERR_NOT_IN_RANGE) {
this.moveTo(this.room.terminal);
}
terminalResources.push(res);
break;
}
}
if (terminalResources.length == 0) {
// Material for storage left in creep
this.storeAllBut();
}
}
else {
// Creep empty
//Check storage for useful resources
terminalDelta = 0;
for (var res in this.room.terminal.store) {
var delta = this.checkTerminalLimits(res);
if (delta.amount > 0) {
//Terminal has surplus material
var load = Math.abs(delta.amount);
if (load > this.carryCapacity) {
load = this.carryCapacity;
}
if (this.withdraw(this.room.terminal, res, load) == ERR_NOT_IN_RANGE) {
this.moveTo(this.room.terminal);
}
terminalDelta++;
break;
}
}
if (terminalDelta == 0) {
//Check for surplus material in terminal
var breaker = false;
for (var res in this.room.storage.store) {
delta = this.checkTerminalLimits(res);
if (delta.amount < 0) {
//Terminal needs material from storage
var load = Math.abs(delta.amount);
if (load > this.carryCapacity) {
load = this.carryCapacity;
}
if (this.withdraw(this.room.storage, res, load) == ERR_NOT_IN_RANGE) {
this.moveTo(this.room.storage, {reusePath: moveReusePath()});
}
breaker = true;
break;
}
}
if (breaker == false && _.sum(this.carry) == 0) {
//Look for minerals in containers
let container;
if (this.memory.myMineralContainer == undefined) {
container = this.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => s.structureType == STRUCTURE_CONTAINER && s.store[RESOURCE_ENERGY] < _.sum(s.store)});
if (container != null) {
this.memory.myMineralContainer = container.id;
}
}
else {
container = Game.getObjectById(this.memory.myMineralContainer);
if (_.sum(container.store) == container.store[RESOURCE_ENERGY]) {
delete this.memory.myMineralContainer;
container = null;
}
}
var containerResource = undefined;
if (container != undefined && container != null && this.room.storage != undefined) {
//minerals waiting in containers
//analyzing storage of container
var store = container.store;
for (var s in store) {
if (s != RESOURCE_ENERGY) {
// mineral found in container
containerResource = s;
}
}
if (containerResource != undefined && this.withdraw(container, containerResource) == ERR_NOT_IN_RANGE) {
this.moveTo(container, {reusePath: moveReusePath()});
}
}
}
}
}
}
}
};