-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.longDistanceHarvester.js
89 lines (85 loc) · 3.29 KB
/
role.longDistanceHarvester.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
77
78
79
80
81
82
83
84
85
86
87
88
89
module.exports = {
// a function to run the logic for this role
/** @param {Creep} creep */
run: function(creep) {
// if target is defined and creep is not in target room
if (creep.memory.target != undefined && creep.room.name != creep.memory.target && creep.memory.working == false) {
// find exit to target room
var exit = creep.room.findExitTo(creep.memory.target);
// move to exit
creep.travelTo(creep.pos.findClosestByRange(exit));
// return the function to not do anything else
return;
}
// if creep is bringing energy to a structure but has no energy left
if (creep.memory.working == true && creep.carry.energy == 0) {
// switch state
creep.memory.working = false;
}
// if creep is harvesting energy but is full
else if (creep.memory.working == false && creep.carry.energy == creep.carryCapacity) {
// switch state
creep.memory.working = true;
}
// if creep is supposed to transfer energy to a structure
if (creep.memory.working == true) {
// if in home room
if (creep.room.name == creep.memory.home) {
// find closest spawn, extension or tower which is not full
var structure = creep.pos.findClosestByPath(FIND_MY_STRUCTURES, {
// the second argument for findClosestByPath is an object which takes
// a property called filter which can be a function
// we use the arrow operator to define it
filter: (s) => (s.structureType == STRUCTURE_SPAWN ||
s.structureType == STRUCTURE_EXTENSION ||
s.structureType == STRUCTURE_TOWER) &&
s.energy < s.energyCapacity
});
// if there isn't a spawn, extension or tower available
if (structure == undefined) {
// look for a storage or container structure
structure = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: s => s.structureType == STRUCTURE_CONTAINER ||
s.structureType == STRUCTURE_STORAGE &&
s.store[RESOURCE_ENERGY] > 0
});
}
// if we found one
if (structure != undefined) {
// try to transfer energy, if it is not in range
if (creep.transfer(structure, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
// move towards it
creep.travelTo(structure);
}
}
}
// if not in home room...
else {
// find exit to home room
var exit = creep.room.findExitTo(creep.memory.home);
// and move to exit
creep.travelTo(creep.pos.findClosestByRange(exit));
}
}
// if creep is supposed to harvest energy from source
else {
// if in target room
if (creep.room.name == creep.memory.target) {
// find source
var source = creep.room.find(FIND_SOURCES)[creep.memory.sourceIndex];
// try to harvest energy, if the source is not in range
if (creep.harvest(source) == ERR_NOT_IN_RANGE) {
// move towards the source
creep.travelTo(source);
}
}
// if not in target room
else {
// find exit to target room
var exit = creep.room.findExitTo(creep.memory.target);
// move to exit
creep.travelTo(creep.pos.findClosestByRange(exit));
}
}
}
};