-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.experimental.js
238 lines (227 loc) · 8.15 KB
/
worker.experimental.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
/*
* This is where my proof-of-concept hacks go.
*/
var dedicatedUpgrader = function(creep) {
if(creep.carry.energy < 15) {
// let target = creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (structure) => (structure.structureType === STRUCTURE_LINK)});
let target = Game.getObjectById(creep.memory.link);
if(target.energy < 400) {
console.log(creep.name, 'forcing energy pull for', target);
target.pullEnergy();
}
let result = creep.withdraw(target, RESOURCE_ENERGY);
// console.log(creep.name, 'trying to get energy', target, result);
if(result == ERR_NOT_IN_RANGE) {
creep.loggedMove(target);
}
}
if(creep.carry.energy > 0) {
let target = creep.room.controller;
let result = creep.upgradeController(target);
// console.log(creep.name, 'trying to upgrade', target, result);
if(result == ERR_NOT_IN_RANGE && creep.carry.energy > 15) {
creep.loggedMove(target);
}
}
}
var dedicatedHarvester = function(creep) {
let target = Game.getObjectById(creep.memory.target);
let result = creep.harvest(target);
if(result == ERR_NOT_IN_RANGE || result == ERR_NOT_ENOUGH_RESOURCES) {
creep.loggedMove(target);
}
else if(result != OK) {
console.log(creep.name, creep.pos, 'got harvest error', result);
}
if(creep.carry.energy > 20) {
let link = Game.getObjectById(creep.memory.link);
if(link.energy > 750) {
link.pushEnergy();
}
result = creep.transfer(link, RESOURCE_ENERGY);
if(result == ERR_NOT_IN_RANGE) {
creep.loggedMove(link);
}
}
}
Creep.prototype.linkUpgraderInit = function() {
this.registerUpgrading();
let linkTarget = this.room.controller.pos.findClosestByRange(FIND_STRUCTURES, {filter: (structure) => (structure.structureType === STRUCTURE_LINK)});
this.memory.link = linkTarget.id;
}
Creep.prototype.linkHarvesterInit = function() {
console.log(this.name, 'initializing harvester');
let sources = this.room.memory.energySources;
for(let i = 0; i < sources.length; i++) {
if(sources[i].link && !(sources[i].dedicatedHarvester in Game.creeps)) {
console.log('dedicated harvester', this.name, 'selecting source', sources[i].id);
sources[i].miners.push(this.name);
sources[i].dedicatedHarvester = this.name;
this.memory.target = sources[i].id;
this.memory.link = sources[i].link;
return;
}
console.log(sources[i], 'has dedicated harvester', sources[i].dedicatedHarvester)
}
}
Room.prototype.getSourceLinks = function() {
var sourceLinks = [];
let sources = this.memory.energySources;
for(let i = 0; i < sources.length; i++) {
if(sources[i].link) {
sourceLinks.push(Game.getObjectById(sources[i].link));
}
}
return sourceLinks;
}
var energyTransport = function(creep) {
if(creep.carry.energy == 0 && !creep.memory.collecting) {
// console.log(creep.name, 'no energy, not collecting');
creep.memory.storing = false;
creep.memory.collecting = true;
// if(creep.room.memory.tasks.droppedEnergy.length > 0) {
// creep.memory.target = creep.room.memory.tasks.droppedEnergy[0].id;
// creep.memory.pickup = true;
// }
// else {
// creep.memory.pickup = false;
let sourceLinks = creep.room.getSourceLinks();
let targetLink = creep.pos.findClosestByPath(sourceLinks, {filter: (link) => link.energy >= 100});
if(!targetLink) {
// Something has gone wrong, like no energy to get
creep.memory.collecting = false;
return;
}
creep.memory.target = targetLink.id;
// }
}
else if(creep.carry.energy > 0 && !creep.memory.storing) {
// console.log(creep.name, 'energy, not storing');
creep.memory.storing = true;
creep.memory.collecting = false;
//Find a place to store
let closest = creep.pos.findClosestByPath(creep.room.memory.tasks.needEnergy, {filter: (structure) => structure.structureType != STRUCTURE_LINK});
if(closest) {
creep.memory.target = closest.id;
}
else {
if(creep.room.terminal && creep.room.terminal.store[RESOURCE_ENERGY] < 50000) {
creep.memory.target = creep.room.terminal.id;
}
else if(creep.room.storage) {
creep.memory.target = creep.room.storage.id;
}
else {
creep.memory.storing = false;
}
}
}
// else {
// console.log(creep.name, 'transport not changing state; storing:', creep.memory.storing, 'collecting:', creep.memory.collecting);
// }
let target = Game.getObjectById(creep.memory.target);
if(creep.memory.storing) {
let result;
// if(creep.memory.pickup) {
// result = creep.pickup(target);
// }
// else {
result = creep.transfer(target, RESOURCE_ENERGY);
// }
if(result == ERR_NOT_IN_RANGE) {
creep.loggedMove(target);
}
else if(result == OK || result == ERR_FULL) {
creep.memory.storing = false;
}
}
else if(creep.memory.collecting) {
let result = creep.withdraw(target, RESOURCE_ENERGY);
if(result == ERR_NOT_IN_RANGE) {
creep.loggedMove(target);
}
else if(result == OK || result == ERR_FULL || result == ERR_NOT_ENOUGH_RESOURCES) {
creep.memory.collecting = false;
}
}
}
//XXX This is a quick hack to pay a ransom.
var shiftMinerals = function(creep) {
if(!creep.carry[RESOURCE_HYDROGEN] && !creep.memory.collecting) {
console.log(creep.name, 'no minerals, not collecting');
creep.memory.storing = false;
creep.memory.collecting = true;
if(creep.room.storage) {
creep.memory.target = creep.room.storage.id;
}
else {
console.log(creep.name, 'is missing a storage unit');
creep.memory.collecting = false;
}
}
else if(creep.carry[RESOURCE_HYDROGEN] > 0 && !creep.memory.storing) {
console.log(creep.name, 'minerals, not storing');
creep.memory.storing = true;
creep.memory.collecting = false;
//Find a place to store
if(creep.room.terminal) {
creep.memory.target = creep.room.terminal.id;
}
else {
console.log(creep.name, 'has nowhere to shift minerals to.');
creep.memory.storing = false;
}
}
let target = Game.getObjectById(creep.memory.target);
if(creep.memory.storing) {
let result;
result = creep.transfer(target, RESOURCE_HYDROGEN);
if(result == ERR_NOT_IN_RANGE) {
creep.loggedMove(target);
}
else if(result == OK || result == ERR_FULL) {
creep.memory.storing = false;
}
}
else if(creep.memory.collecting) {
let result = creep.withdraw(target, RESOURCE_HYDROGEN);
if(result == ERR_NOT_IN_RANGE) {
creep.loggedMove(target);
}
else if(result == OK || result == ERR_FULL || result == ERR_NOT_ENOUGH_RESOURCES) {
creep.memory.collecting = false;
}
}
}
module.exports.runExperimental = function(creep) {
switch(creep.memory.role) {
case 'upgrader':
dedicatedUpgrader(creep);
break;
case 'transport':
energyTransport(creep);
break;
case 'harvester':
dedicatedHarvester(creep);
break;
case 'gofer':
shiftMinerals(creep);
break;
default:
break;
}
}
module.exports.initExperimental = function(creep) {
switch(creep.memory.role) {
case 'upgrader':
creep.linkUpgraderInit();
break;
case 'harvester':
creep.linkHarvesterInit();
break;
case 'transport':
break;
default:
break;
}
}