-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.harvester-ld.js
59 lines (54 loc) · 2.15 KB
/
role.harvester-ld.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
var roleHarvester = {
/** @param {Creep} creep **/
run: function (creep) {
if (creep.store.getFreeCapacity() > 0) {
delete creep.memory.spawnId
let source
if (creep.memory.sourceId) {
source = Game.getObjectById(creep.memory.sourceId)
if (!source || source.energy === 0) {
delete creep.memory.sourceId
source = undefined
}
}
if (!source) {
source = creep.room.findClosestByPath(FIND_SOURCES, {range: 1});
}
if (source) {
creep.memory.sourceId = source.id
if (creep.harvest(source) == ERR_NOT_IN_RANGE) {
creep.moveTo(source, { visualizePathStyle: { stroke: '#ffaa00' } });
}
}
}
else {
delete creep.memory.sourceId
let target
if (creep.memory.spawnId) {
target = Game.getObjectById(creep.memory.spawnId)
if (!target || target.store.getFreeCapacity() === 0) {
delete creep.memory.spawnId
target = undefined
}
}
if (!target) {
target = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN || structure.structureType == STRUCTURE_TOWER) &&
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
}
})
}
if (target) {
console.log(`targets available for harvester creep ${creep}`)
if (creep.transfer(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(target, { visualizePathStyle: { stroke: '#ffffff' } });
return 'working'
}
} else {
console.log(`No targets available for harvester creep ${creep}`)
return 'idle'
}
}
}
};