-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilder.js
314 lines (271 loc) · 8.6 KB
/
builder.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
var util = require('common');
var harvest = require('harvester'); // useful for energy finding routines
// Structure.prototype.needsWorkers = function() {
// var attendees = this.memory.workers;
// var maxAttendees = this.memory.maxWorkers;
//
// if (typeof attendees === 'undefined') {
// attendees = 0;
// }
//
// if (typeof maxAttendees === 'undefined') {
// maxAttendees = 1; // If not defined, be conservative to prevent log
// // jams
// }
// var count = 0;
// attendees.sort();
// for (var creep in attendees) {
// if (attendees[creep].hits > 0) {
// count++;
// } else {
// destroy(attendees[creep]);
// }
// }
// }
//
// Structure.prototype.needsRepair = function() {
// return this.hits < this.hitsMax * .8;
// };
//
// Structure.prototype.isDone = function() {
// return (this.hits == this.hitsMax);
// };
function builder(creep) {
// Take a look around the room for something to do
creep.say('⚒')
if (creep.getActiveBodyparts(WORK) == 0 || creep.getActiveBodyparts(CARRY) == 0) {
// poor guy is probably injured or something
// TODO, maybe find nearest friendly city?
creep.changeTask('leaveroom');
creep.leaveRoom(creep.memory.birthRoom);
return true;
}
if (creep.carry.energy == creep.carryCapacity) {
if (creep.taskState != "SPECIAL") {
creep.taskState = "SINK";
}
}
if (creep.carry.energy == 0) {
// Just got doing something else
if (creep.taskState != 'SINK') {
if (harvest.fillTank(creep)) {
creep.addTask('filltank');
return true;
} else {
return false;
}
}
// load done
// creep.log('builder popping')
return false;
}
//if (creep.memory.taskState == 'SOURCE') {
// return harvest.fillTank(creep);
// }
var target = Game.getObjectById(creep.memory.bTarget);
if (!util.def(creep.memory.bTarget) || !target) {
var orders = findSite(creep) || repairDuty(creep);
if (!util.def(orders) || orders == false) {
// dlog(creep.name + '/' + creep.room.name + ' says nothing to build or repair, converting to technician')
// creep.changeTask('technician');
return false;
} else {
creep.memory.bTarget = orders;
target = Game.getObjectById(orders);
}
}
if (target.progress >= 0) {
var res = creep.build(target);
switch (res) {
case OK:
creep.say(sayProgress(target) + '%');
return true;
break;
case ERR_NOT_IN_RANGE:
var derp = creep.moveTo(target, {
reusePath: 5,
visualizePathStyle: {
stroke: '1ffaa00'
}
});
if (derp == OK || derp == ERR_TIRED) {
return true;
} else {
return false
}
break;
case ERR_NOT_ENOUGH_RESOURCES:
return harvest.fillTank(creep);
break;
case ERR_RCL_NOT_ENOUGH:
creep.memory.taskList.pop();
creep.memory.bTarget = null;
return false;
break;
default:
dlog('Build command error: ' + util.getError(res));
return false;
}
} else if (needsRepair(target)) {
dlog(creep.name + 'im repairing now?')
if ((creep.pos.isNearTo(target)) && (creep.carry.energy > 0)) {
creep.say(sayProgress(target) + '%');
creep.repair(target)
return true;
} else if (creep.carry.energy == creep.carryCapacity) {
var res = creep.moveTo(target, {
reusePath: 5,
visualizePathStyle: {
stroke: '1ffaa00'
}
});
if (res == OK || res == ERR_TIRED) {
return true;
} else {
return false;
}
} else {
return harvest.fillTank(creep);
}
} else {
// console.log('clearing target ' + creep.name + ' target: '
// + target.structureType + ' ' + target.hits + '/'
// + target.hitsMax);
dlog('builder unsure what to do with target ' + target.id)
return false;
}
}
module.exports.builder = builder;
function needsRepair(target) {
// console.log('needs repair? ' + target.hits + '/' + target.hitsMax);
return target.hits < (target.hitsMax / 2);
}
function repairDuty(creep) {
return false;
var structures = creep.room.find(FIND_MY_STRUCTURES);
var options = [];
// TODO: can I sort structures in order of damage?
for (var i in structures) {
var s = structures[i];
var intendedPath = creep.checkPath(s);
// Check if path exists!! Otherwise, builders can block each other
if (s.hits === null) {
continue;
}
if (s.needsRepair()) {
var res = creep.moveTo(s, {
reusePath: 5,
visualizePathStyle: {
stroke: '1ffaa00'
}
});
creep.repair(s);
}
}
}
function findSite(creep) {
if (creep.getActiveBodyparts(WORK) == 0) {
creep.memory.taskList.pop();
return false;
}
if (!util.def(creep.memory.bTarget) ||
!util.def(Game.getObjectById(creep.memory.bTarget))) {
// If not listed then it's built by nearest after all these are done
var buildPriority = ['extension', 'container', 'storage', 'tower',
'spawn', 'link', 'rampart', 'road', 'constructedWall'
];
var newTarget = creep.room.find(FIND_MY_CONSTRUCTION_SITES);
if (!util.def(newTarget)) {
dlog('no build targets. untasking')
creep.memory.taskList.pop();
return false;
}
// Prioritize
for (var need in buildPriority) {
var priority = buildPriority[need];
for (var site in newTarget) {
if (newTarget[site].structureType == priority) {
creep.memory.bTarget = newTarget[site].id;
// dlog('assinging ' + creep.name + ' build target ' +
// priority);
return newTarget[site].id;
}
}
}
}
return false;
}
module.exports.findSite = findSite;
module.exports.upgradeRC = upgradeRC;
function upgradeRC(creep) {
var rc = creep.room.controller;
creep.say('🔧')
if (creep.getActiveBodyparts(WORK) == 0) {
creep.memory.taskList.pop();
return false;
}
if (creep.carry.energy == creep.carryCapacity) {
creep.memory.taskState = 'SINK';
}
if (creep.carry.energy == 0) {
// Just got doing something else
if (creep.taskState != 'SINK' && harvest.findCashMoney(creep)) {
creep.addTask('filltank');
return true;
}
// Job done
// creep.log('tech popping')
return false;
}
if (!util.def(rc)) {
// no controller in this room
return false;
}
if (rc.my) {
var res = creep.upgradeController(rc);
} else {
return false;
}
switch (res) {
case OK:
creep.say(sayProgress(rc) + "%");
return true;
break;
case ERR_NOT_IN_RANGE:
var path = creep.moveTo(rc, {
reusePath: 5,
visualizePathStyle: {
stroke: '1ffaa00'
}
});
if (path != OK && path != ERR_TIRED) {
return false;
dlog('Tech path error: ' + util.getError(path));
} else {
return true;
}
break;
case ERR_NOT_ENOUGH_RESOURCES:
creep.taskState = "SOURCE";
return false;
break;
case ERR_NO_BODYPART:
return false;
default:
dlog(creep.name + '- technician default error: ' + util.getError(res) + ' (' + res + ')');
dlog('owner is ' + rc.owner)
return false;
}
}
function sayProgress(target) {
if (util.def(target.progress)) {
return parseInt((target.progress / target.progressTotal) * 100);
} else if (target.hits !== null) {
return parseInt((target.hits / target.hitsMax) * 100);
} else {
dlog('say what?')
}
}
function dlog(msg) {
util.dlog('CONSTRUCTION', msg);
}