-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.harvester.js
79 lines (59 loc) · 2.62 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
/**
* Harvester is the simplest crip role that tries to get some
* energy directly from the source. It's small and versatile -
* can supply energy to many diffent targets. It will get energy from
* a random energy source. Not very efficient.
*
*/
var roleHarvester = {
/** @param {Creep} creep **/
run: function (creep) {
if (creep.changeRoomsIfNeeded())
return;
if (creep.store.getFreeCapacity() > 0) {
//still some capacity left, try to fill
if (creep.pickDroppedEnergyIfAny()) {
return;
}
let sources = creep.room.find(FIND_SOURCES);
//if there is no dropped then just try to harvest normal sources
if (creep.memory.assignment === undefined || creep.memory.assignment == 0) {
//pick random source
var r = Math.round(Math.random() * (sources.length - 1));
creep.memory.assignment = sources[r].id;
}
var my_source = Game.getObjectById(creep.memory.assignment);
if (creep.harvest(my_source) == ERR_NOT_IN_RANGE) {
creep.moveTo(my_source, { visualizePathStyle: { stroke: '#FF0000' } });
return;
}
}
else {
var targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_EXTENSION ||
structure.structureType == STRUCTURE_STORAGE ||
structure.structureType == STRUCTURE_TOWER ||
structure.structureType == STRUCTURE_SPAWN)
&&
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0
}
});
targets = targets.sort((a, b) =>
(a.store.getCapacity() - b.store.getCapacity()));
if (targets.length > 0) {
let best_targets = _.filter(targets, (t) => t.store.getCapacity() == targets[0].store.getCapacity());
my_target = creep.pos.findClosestByPath(best_targets);;
if (creep.transfer(my_target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(my_target, { visualizePathStyle: { stroke: '#95C8D8' } });
}
} else {
//stores dont need energy -> upgrade controllers
if (creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
creep.moveTo(creep.room.controller);
}
}
}
}
};
module.exports = roleHarvester;