-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.externalbuilder.js
73 lines (70 loc) · 2.34 KB
/
role.externalbuilder.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
/*
* Module code goes here. Use 'module.exports' to export things:
* module.exports.thing = 'a thing';
*
* You can import it from another modules like this:
* var mod = require('role.externalbuilder');
* mod.thing == 'a thing'; // true
*/
module.exports = {
run: function(creep) {
if(creep.memory.building && creep.carry.energy == 0)
{
//If the creep is building and has no energy, remove the building flag
creep.memory.building = false;
}
if(!creep.memory.building && creep.carry.energy == creep.carryCapacity)
{
//If the creep is not building and has full energy, set the building flag
creep.memory.building = true;
}
if(creep.memory.building)
{
if (creep.memory.target_set) {
if (creep.memory.target == null) {
creep.memory.targetSet = false;
return;
}
var current_target = Game.getObjectById(creep.memory.target);
if (current_target && current_target.progress != undefined) {
//Only Construction Sites have the 'progress' modifier, this will return false if construction is complete
if (creep.build(current_target) == ERR_NOT_IN_RANGE) creep.moveTo(current_target);
} else {
creep.memory.target = null;
creep.memory.target_set = false;
}
} else {
var targets = Game.flags["ExternalBuilder"].room.find(FIND_CONSTRUCTION_SITES);
if (targets.length > 0) {
var target_object = Game.flags["ExternalBuilder"].pos.findClosestByPath(targets);
if (target_object == null) return;
creep.memory.target = target_object.id;
creep.memory.target_set = true;
}
}
}
else
{
//If the creep is not building:
//Create a variable array of the room's spawns and extensions that have energy
var targets = Game.rooms[creep.memory.homeroom].find(FIND_STRUCTURES,
{
filter: (structure) =>
{
return (structure.structureType == STRUCTURE_SPAWN ||
structure.structureType == STRUCTURE_EXTENSION) && structure.energy > 0;
}
});
if (targets.length > 0)
{
//If there are targets:
//If the creep is out of range of the first target, move it to the site
//If the creep is in range, it will automatically withdraw energy due to the if statement
if (creep.withdraw(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE)
{
creep.moveTo(targets[0]);
}
}
}
}
};