-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.builder.js
67 lines (63 loc) · 2.26 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
61
62
63
64
65
66
67
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.travelTo(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 if creep has a constructionSite in memory
if (creep.memory.constructionSite != undefined) {
constructionSite = Game.getObjectById(creep.memory.constructionSite.id)
// try to build, if the constructionSite is not in range
if (constructionSite) {
if (creep.build(constructionSite) == ERR_NOT_IN_RANGE) {
// move towards the constructionSite
creep.travelTo(constructionSite);
}
} else {
delete creep.memory.constructionSite;
}
if (constructionSite < 100) {
creep.memory.constructionSite == '';
}
}
// if creep does not have a constructionSite in memory
else {
// try to find one
var constructionSite = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES);
// if a constructionSite is found
if (constructionSite != undefined) {
creep.memory.constructionSite = constructionSite;
}
// if no constructionSite is found
else {
// go upgrade the controller
roleUpgrader.run(creep);
}
}
}
// if creep is supposed to get energy
else {
creep.getEnergy(true, true);
}
}
};