-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.harvester.js
67 lines (65 loc) · 2.84 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
var roleHarvester = {
/** @param {Creep} creep **/
run: function(creep) {
if(creep.carry.energy < creep.carryCapacity) {
var target;
if (creep.memory.destination != undefined) {
target = Game.getObjectById(creep.memory.destination);
}
if (target == undefined) {
var targets = creep.room.find(FIND_DROPPED_ENERGY);
var opti = {'id': undefined, 'qty': undefined};
for(var i = targets.length - 1; i>=0; i--) {
if(opti.id == undefined || opti.qty < targets[i].amount) {
opti.id = targets[i].id;
opti.qty = targets[i].energy;
}
}
if(opti.id != undefined) {
target = Game.getObjectById(opti.id);
creep.memory.destination = opti.id;
} else {
target = creep.pos.findClosestByPath(FIND_SOURCES_ACTIVE);
if (target != undefined) {
creep.memory.destination = target.id;
}
}
}
if (creep.pickup(target) == ERR_NOT_IN_RANGE || creep.harvest(target) == ERR_NOT_IN_RANGE) {
creep.moveTo(target);
} else {
creep.memory.destination = undefined;
}
}
else {
creep.memory.destination = undefined;
var target = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN) && structure.energy < structure.energyCapacity;
}
});
if(target != undefined) {
if(creep.transfer(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(target);
}
} else {
target = creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => s.structureType == STRUCTURE_TOWER && s.energy < s.energyCapacity});
if(target != undefined) {
if(creep.transfer(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(target);
}
} else {
target = creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: (s) => s.structureType == STRUCTURE_STORAGE});
if(target != undefined) {
if(creep.transfer(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(target);
}
} else {
creep.moveTo(27,33);
}
}
}
}
}
};
module.exports = roleHarvester;