-
Notifications
You must be signed in to change notification settings - Fork 62
/
role.builder.js
51 lines (48 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
var roleUpgrader = require('role.upgrader');
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) {
// find exit to target room
var exit = creep.room.findExitTo(creep.memory.target);
// move to exit
creep.moveTo(creep.pos.findClosestByRange(exit));
// return the function to not do anything else
return;
}
// if creep is trying to complete a constructionSite 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 complete a constructionSite
if (creep.memory.working == true) {
// find closest constructionSite
var constructionSite = creep.pos.findClosestByPath(FIND_CONSTRUCTION_SITES);
// if one is found
if (constructionSite != undefined) {
// try to build, if the constructionSite is not in range
if (creep.build(constructionSite) == ERR_NOT_IN_RANGE) {
// move towards the constructionSite
creep.moveTo(constructionSite);
}
}
// if no constructionSite is found
else {
// go upgrading the controller
roleUpgrader.run(creep);
}
}
// if creep is supposed to get energy
else {
creep.getEnergy(true, true);
}
}
};