-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.builder.js
60 lines (57 loc) · 1.94 KB
/
role.builder.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
var roleBuilder = {
/** @param {Creep} creep **/
run: function(creep) {
if(!creep.memory.harvesting && creep.carry.energy == 0)
{
creep.memory.harvesting = true;
creep.say("Collecting");
}
if(creep.memory.harvesting && creep.carry.energy == creep.carryCapacity)
{
creep.memory.harvesting = false;
creep.say("Building");
}
if(creep.memory.harvesting)
{
var source = creep.pos.findClosestByRange(FIND_MY_STRUCTURES, {
filter: (structure) => {
return (
structure.structureType == STRUCTURE_EXTENSION ||
structure.structureType == STRUCTURE_CONTAINER ) &&
structure.energy > 0;
}
});
if(source){
if(creep.withdraw(source, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(source);
}
} else {
var sources = creep.room.find(FIND_SOURCES);
if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(sources[0]);
}
}
}
if (!creep.memory.harvesting)
{
var sites = creep.room.find(FIND_CONSTRUCTION_SITES);
var repairs = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => structure.hits < structure.hitsMax });
repairs.sort( (a,b) => a.hits - b.hits) ;
if (sites.length > 0) {
if(creep.build(sites[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE)
{
creep.moveTo(sites[0]);
}
}
else if(repairs.length > 0)
{
if(creep.repair(repairs[0]) == ERR_NOT_IN_RANGE)
{
creep.moveTo(repairs[0])
}
}
}
}
}
module.exports = roleBuilder;