-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrole.powerRefiner.js
54 lines (52 loc) · 1.96 KB
/
role.powerRefiner.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
const spawnHelper = require("helper.spawning");
module.exports = {
name: "powerRefiner",
parts: spawnHelper.makeParts(20, CARRY, 10, MOVE),
run: function(creep) {
if(creep.memory.delivering) {
this.deliver(creep);
} else {
this.pickup(creep);
}
},
deliver: function(creep) {
let spawner = this.spawner(creep);
if(creep.store[RESOURCE_POWER]) {
if(creep.transfer(spawner, RESOURCE_POWER) == ERR_NOT_IN_RANGE) {
creep.goTo(spawner);
}
} else if(creep.store.energy) {
let result = creep.transfer(spawner, RESOURCE_ENERGY);
if(result == OK) {
creep.memory.delivering = false;
} else if(result == ERR_NOT_IN_RANGE) {
creep.goTo(spawner);
}
} else {
creep.memory.delivering = false;
}
},
pickup: function(creep) {
let spawner = this.spawner(creep);
let neededPower = (spawner.powerCapacity - spawner.power) - (creep.store[RESOURCE_POWER] || 0);
neededPower = Math.min(neededPower, creep.store.getCapacity() - _.sum(creep.store), creep.room.storage.store.power || 0);
if(!creep.store[RESOURCE_POWER] && neededPower > 0) {
let result = creep.withdraw(creep.room.storage, RESOURCE_POWER, neededPower);
if(result == ERR_NOT_IN_RANGE) {
creep.goTo(creep.room.storage);
}
} else {
let result = creep.withdraw(creep.room.storage, RESOURCE_ENERGY);
if(result == OK || result == ERR_FULL) {
creep.memory.delivering = true;
} else if(result == ERR_NOT_IN_RANGE) {
creep.goTo(creep.room.storage);
}
}
},
spawner: function(creep) {
return creep.room.powerSpawn();
}
};
const profiler = require("screeps-profiler");
profiler.registerObject(module.exports, 'powerRefiner');