-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrole.harvester.js
84 lines (78 loc) · 3.03 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
/**
* @author Nicolas Durant
* @email [email protected]
* @create date 2019-11-29 14:18:41
* @modify date 2019-12-03 11:13:15
* @desc Harvester Role for a Creep. It will harvest Energy and put it into the closest empty energy store.
*/
module.exports = {
/**
* The function that is defining the creeps behavior.
*
* @param {any} creep - Creep Object
* @param {any} spawn - Spawn Object
*
* @memberOf Upgrader
*/
harvest: function (creep) {
// the creep is fully packed
if (creep.memory.idle && creep.store.getUsedCapacity() === creep.store.getCapacity()){
creep.say('Harvested👍')
creep.memory.idle = false;
}
// if the creep is empty or has not the idle memory yet
else if (!creep.memory.idle && creep.store.getUsedCapacity() === 0){
creep.say('Deposited👍')
creep.memory.idle = true;
}
// if the creep is idle, we sent it to the next source that is still harvestable (ACTIVE)
if (creep.memory.idle) {
const target = creep.pos.findClosestByPath(FIND_SOURCES_ACTIVE);
if (creep.harvest(target) === ERR_NOT_IN_RANGE){
if (creep.memory.status != 'to_work'){
creep.say('To work 🤮')
creep.memory.status = `to_work`
}
creep.moveTo(target, {reusePath: 5});
}
}
// else we sent it to the closest not filled energy store to unload its energy
else {
const towers = creep.pos.findClosestByPath(FIND_MY_STRUCTURES, {
filter: (s) => {
return s.structureType == STRUCTURE_TOWER && s.energy < s.energyCapacity
}
});
const structures = creep.pos.findClosestByPath(FIND_MY_STRUCTURES, {
filter: (s) => {
return (s.structureType == STRUCTURE_EXTENSION
|| s.structureType == STRUCTURE_SPAWN)
&& s.energy < s.energyCapacity
}
});
if (towers) {
if (creep.transfer(towers, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE){
if (creep.memory.status != 'to_tower'){
creep.say('To tower 🗼')
creep.memory.status = `to_tower`
}
creep.moveTo(towers, {reusePath: 5});
}
}
if (structures && !towers) {
if (creep.transfer(structures, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE){
if (creep.memory.status != 'to_store'){
creep.say('To store 🚛')
creep.memory.status = `to_store`
}
creep.moveTo(structures, {reusePath: 5});
}
}// there should always be somewhere to store
else{}
}
},
mine: function (creep) {
},
lorry: function (creep) {
}
};