-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.harvester.js
76 lines (64 loc) · 3.09 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
var roleHarvester = {
/** @param {Creep} creep **/
run: function(creep) {
//console.log(creep.name + " " + creep.room.name);
var targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (((structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN ) &&
structure.energy < structure.energyCapacity) )
}
});
if (targets.length == 0) {
var targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_STORAGE)
}
});
}
if(targets.length > 0) {
//console.log('Creep ' + creep.name + ' delivering:' + creep.memory.delivering + ' on board:' + creep.carry.energy);
if(creep.memory.delivering != true) {
if (creep.memory.source == undefined || creep.memory.source == "") {
source = creep.pos.findClosestByPath(FIND_SOURCES);
if (source != null)
creep.memory.source = source.id;
}
if (creep.memory.source != undefined && creep.memory.source != "") {
var successcode = creep.harvest(Game.getObjectById(creep.memory.source));
//console.log(successcode);
if(successcode == ERR_NOT_IN_RANGE) {
//console.log("4");
creep.moveTo(Game.getObjectById(creep.memory.source));
} else if (successcode == ERR_INVALID_TARGET) {
//console.log("5");
creep.memory.source = "";
}
}
if(creep.carry.energy == creep.carryCapacity) {
creep.memory.delivering = true;
creep.memory.source = "";
}
}
else {
if (creep.memory.destid == undefined || creep.memory.destid == "") {
dest = creep.pos.findClosestByRange(targets);
//console.log(targets);
//console.log(dest);
if (dest != null)
creep.memory.destid = dest.id;
}
if(creep.transfer(Game.getObjectById(creep.memory.destid), RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(Game.getObjectById(creep.memory.destid));
}
//console.log(Game.getObjectById(creep.memory.destid).energy + " / " + Game.getObjectById(creep.memory.destid).energyCapacity);
if (Game.getObjectById(creep.memory.destid).energy == Game.getObjectById(creep.memory.destid).energyCapacity)
creep.memory.destid = "";
if(creep.carry.energy == 0) {
creep.memory.delivering = false;
creep.memory.destid = "";
}
}
}
}
};
module.exports = roleHarvester;