-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.spawnMaintainer.js
69 lines (58 loc) · 2.81 KB
/
role.spawnMaintainer.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
var roleSpawnMaintainer = {
/** @param {Creep} creep **/
run: function(creep) {
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) {
//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 == "") {
//console.log("1");
var source = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_STORAGE )
}
});
source = creep.pos.findClosestByRange(source);
if (source != null)
creep.memory.source = source.id;
}
//console.log("2");
if (creep.memory.source != undefined && creep.memory.source != "") {
var statuscode = creep.withdraw(Game.getObjectById(creep.memory.source), RESOURCE_ENERGY);
//console.log(statuscode);
if(statuscode == ERR_NOT_IN_RANGE)
creep.moveTo(Game.getObjectById(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 == "") {
//console.log(targets.length);
dest = creep.pos.findClosestByRange(targets);
if (dest == null)
dest = creep.pos.findClosestByRange(targets);
//console.log(dest);
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));
}
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 = roleSpawnMaintainer;