-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrum.master.js
106 lines (93 loc) · 3.73 KB
/
scrum.master.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const FINDCONTAINERENERGYAT = 200;
const FINDDROPPEDENERGYAT = 100;
let scrumMaster = {
run: function (creep) {
if (creep.memory.building && creep.carry.energy == 0) {
creep.memory.building = false;
}
if (!creep.memory.building && creep.carry.energy == creep.carryCapacity) {
creep.memory.building = true;
}
if (!creep.memory.building) {
findEnergy(creep);
}
else {
restoreEnergy(creep);
}
}
}
function restoreEnergy(creep) {
const buildings = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: (structure) => {
return ((structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN) &&
structure.energy < structure.energyCapacity);
}
});
if (buildings) {
if (creep.transfer(buildings, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(buildings);
}
}
else {
// If the storage is full move the scrum masters back to the spawn
// so they are out of the way.
const getOutOfWay = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: (structure) => {
return ((structure.structureType == STRUCTURE_SPAWN));
}
});
creep.moveTo(getOutOfWay);
}
}
function findEnergy(creep) {
let storedEnergyContainers = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_STORAGE &&
structure.store[RESOURCE_ENERGY] >= FINDCONTAINERENERGYAT || structure.structureType == STRUCTURE_CONTAINER &&
structure.store[RESOURCE_ENERGY] >= FINDCONTAINERENERGYAT)
}
});
// If the creep is not a scrum master or upgrader they can't use the
// upgrader or spawn containers.
if (creep.memory.role == 'transporter')
storedEnergyContainers = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_CONTAINER &&
structure.store[RESOURCE_ENERGY] > FINDCONTAINERENERGYAT
&& structure.id != Game.spawns[creep.memory.spawnName].memory.upgradeContainerId
&& structure.id != Game.spawns[creep.memory.spawnName].memory.spawnContainer1Id
&& structure.id != Game.spawns[creep.memory.spawnName].memory.spawnContainer2Id
);
}
});
if (creep.memory.role == 'repair_squad') {
storedEnergyContainers = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return ((structure.structureType == STRUCTURE_STORAGE && structure.store[RESOURCE_ENERGY] >= 100) ||
(structure.structureType == STRUCTURE_CONTAINER && structure.store[RESOURCE_ENERGY] >= 100));
}
})
}
const closestEnergyContainer = creep.pos.findClosestByPath(storedEnergyContainers);
const droppedResources = creep.room.find(FIND_DROPPED_RESOURCES);
const lotsofenergy = droppedResources.filter(resource => resource.energy >= FINDDROPPEDENERGYAT);
const closestDroppedEnergy = creep.pos.findClosestByPath(lotsofenergy);
if (closestDroppedEnergy != null)
getDroppedEnergy(creep, closestDroppedEnergy);
else if (closestEnergyContainer != null) {
getStoredEnergy(creep, closestEnergyContainer);
}
}
function getDroppedEnergy(creep, target) {
creep.moveTo(target);
creep.pickup(target);
}
function getStoredEnergy(creep, target) {
if (creep.withdraw(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(target);
}
}
module.exports = {
scrumMaster: scrumMaster,
findEnergy: findEnergy
};