-
Notifications
You must be signed in to change notification settings - Fork 7
/
role.harvester.js
123 lines (113 loc) · 4.97 KB
/
role.harvester.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
var spawnHelper = require('helper.spawning');
var logistic = require('helper.logistic');
module.exports = {
name: "harvester",
carryConfigs: [
[CARRY, CARRY, MOVE, CARRY, CARRY, MOVE, CARRY, CARRY, MOVE, CARRY, CARRY, MOVE, CARRY, CARRY, MOVE],
[CARRY, CARRY, MOVE, CARRY, CARRY, MOVE, CARRY, CARRY, MOVE],
[CARRY, CARRY, MOVE, CARRY, CARRY, MOVE],
[CARRY, CARRY, MOVE]
],
miningConfigs: [
[WORK, WORK, MOVE, WORK, WORK, MOVE, WORK, CARRY, MOVE, CARRY, CARRY, MOVE, CARRY, CARRY, MOVE],
[WORK, WORK, MOVE, WORK, WORK, MOVE, CARRY, CARRY, MOVE, CARRY, CARRY, MOVE],
[WORK, WORK, MOVE, CARRY, CARRY, MOVE, CARRY, CARRY, MOVE],
[WORK, WORK, MOVE, CARRY, CARRY, MOVE],
[WORK, WORK, CARRY, MOVE],
[WORK, CARRY, MOVE]
],
run: function(creep) {
// check if our carry has something else than energy and drop it (e.g. due to overfilling protection)
let wrongCarryResource = _.find(Object.keys(creep.store), (r) => r != "energy");
if(wrongCarryResource) {
creep.drop(wrongCarryResource);
}
if(creep.memory.delivering && creep.store.energy == 0) {
creep.memory.delivering = false;
}
if(!creep.memory.delivering && creep.store.energy == creep.store.getCapacity()) {
creep.memory.delivering = true;
}
if(creep.store.getFreeCapacity() > 0 && creep.pos.isNearTo(creep.room.storage)) {
// Safety valve: protect storage from overflowing with anything but energy
if(creep.room.storage.my &&(creep.room.storage.store.getFreeCapacity() < 10000)) {
let excessResource = _.invert(creep.room.storage.store)[_.sortBy(creep.room.storage.store, (r) => -r)[0]];
console.log("Storage in room " + creep.room.name + " is overfilled! Removing excess " + excessResource);
creep.withdraw(creep.room.storage, excessResource);
return;
}
creep.withdraw(creep.room.storage, RESOURCE_ENERGY);
}
if(creep.memory.delivering) {
this.deliver(creep);
} else {
if(this.pickup(creep)) this.deliver(creep);
}
},
deliver: function(creep) {
let targets = this.findTargets(creep);
let target = targets.shift();
if(target) {
creep.memory.stopped = false;
let transferResult = creep.transfer(target, RESOURCE_ENERGY);
if(transferResult == ERR_NOT_IN_RANGE) {
creep.goTo(target);
} else if(transferResult == OK) {
target = targets.shift();
if(target && !creep.pos.isNearTo(target)) {
creep.goTo(target);
}
}
} else {
creep.memory.stopped = true;
if(creep.store.energy < creep.store.getCapacity()) {
creep.memory.delivering = false;
}
}
},
findTargets: function(creep) {
var targets = creep.room.find(FIND_MY_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN) &&
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
}
});
if(targets.length == 0) {
targets = creep.room.find(FIND_MY_STRUCTURES, {
filter: (structure) => {
return structure.structureType == STRUCTURE_TOWER &&
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
}
});
}
if(creep.room.storage && creep.room.storage.store.energy > 100000) {
if(targets.length == 0 && creep.room.terminal) {
var terminal = creep.room.terminal;
if(terminal.store.getFreeCapacity() > 0 && terminal.store[RESOURCE_ENERGY] < 100000) {
targets = [terminal];
}
}
if(targets.length == 0 && creep.room.ai().mode !== "unclaim") {
targets = creep.room.find(FIND_MY_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_NUKER || structure.structureType == STRUCTURE_POWER_SPAWN) &&
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
}
});
}
}
return _.sortBy(targets, (t) => creep.pos.getRangeTo(t));
},
pickup: function(creep) {
creep.memory.stopped = false;
var source = Game.getObjectById(creep.memory.source);
var result = logistic.obtainEnergy(creep, source, true);
if(result == logistic.obtainResults.withdrawn) {
creep.memory.delivering = true;
return true;
}
return false;
}
};
const profiler = require("screeps-profiler");
profiler.registerObject(module.exports, 'harvester');