-
Notifications
You must be signed in to change notification settings - Fork 7
/
role.factoryWorker.js
43 lines (37 loc) · 1.36 KB
/
role.factoryWorker.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
const spawnHelper = require("helper.spawning");
module.exports = {
name: "factoryWorker",
parts: spawnHelper.makeParts(10, CARRY, 5, MOVE),
run: function(creep) {
let factory = creep.room.ai().factory;
let storage = creep.room.storage;
if(creep.memory.importing) {
if(!this.carryTo(creep, factory.structure)) {
creep.memory.importing = false;
let exportResource = _.first(factory.exportableResources());
if(exportResource) {
creep.withdraw(factory.structure, exportResource);
}
}
} else {
if(!this.carryTo(creep, storage)) {
creep.memory.importing = true;
let importResource = _.filter(factory.importableResources(), (r) => storage.store[r] > 0)[0];
if(importResource) {
creep.withdraw(storage, importResource);
}
}
}
},
carryTo: function(creep, target) {
if(creep.pos.isNearTo(target)) {
if(_.sum(creep.store) == 0) return false;
creep.transfer(target, _.last(_.keys(creep.store)))
} else {
creep.goTo(target);
}
return true;
}
};
const profiler = require("screeps-profiler");
profiler.registerObject(module.exports, 'factoryWorker');