-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.fixer.js
126 lines (110 loc) · 3.63 KB
/
role.fixer.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const Utils = require("utils");
const repairTargets = [
STRUCTURE_STORAGE,
STRUCTURE_CONTAINER,
STRUCTURE_ROAD,
STRUCTURE_TOWER,
STRUCTURE_RAMPART
];
const roleFixer = {
run: creep => {
// Behavior assignment conditions
if (creep.memory.fixing && creep.store[RESOURCE_ENERGY] == 0) {
creep.memory.fixing = false;
creep.say("🔄 harvest");
}
if (!creep.memory.fixing && creep.store.getFreeCapacity() == 0) {
creep.memory.fixing = true;
creep.say("🚧 Fixing");
}
// Fixing
if (creep.memory.fixing) {
const targets = creep.room.find(FIND_STRUCTURES, {
filter: structure =>
repairTargets.includes(structure.structureType) &&
structure.hits <
structure.hitsMax * (structure.structureType === STRUCTURE_RAMPART ? 0.001 : 0.6)
});
if (!targets.length) return;
if (creep.repair(targets[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0], {
visualizePathStyle: { stroke: "#ffffff" }
});
}
return;
}
// Harvesting
Utils.withdrawFromStorageOrHarvest(creep);
},
emergency: creep => {
const transferToFirstTarget = targets => {
if (creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0], {
visualizePathStyle: { stroke: "#ffffff" }
});
}
};
const findStructures = targets => {
return creep.room.find(FIND_STRUCTURES, {
filter: structure =>
targets.includes(structure.structureType) &&
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0
});
};
if (creep.store[RESOURCE_ENERGY] == 0) {
const matchingStructures = creep.room.find(FIND_STRUCTURES, {
filter: structure =>
[STRUCTURE_CONTAINER, STRUCTURE_STORAGE].includes(structure.structureType) &&
structure.store.getCapacity() > 0
});
if (!matchingStructures && matchingStructures.length == 0) return;
const container = matchingStructures[0];
if (creep.withdraw(container, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(container, {
visualizePathStyle: { stroke: "#ffaa00" }
});
}
} else {
const targets = findStructures([STRUCTURE_EXTENSION, STRUCTURE_SPAWN]);
if (targets.length > 0) {
transferToFirstTarget(targets);
}
}
},
transferToStorage: creep => {
const transferToFirstTarget = targets => {
if (creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0], {
visualizePathStyle: { stroke: "#ffffff" }
});
}
};
const findStructures = targets => {
return creep.room.find(FIND_STRUCTURES, {
filter: structure =>
targets.includes(structure.structureType) &&
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0
});
};
if (creep.store[RESOURCE_ENERGY] == 0) {
const matchingStructures = creep.room.find(FIND_STRUCTURES, {
filter: structure =>
[STRUCTURE_CONTAINER].includes(structure.structureType) &&
structure.store.getCapacity() > 0
});
if (!matchingStructures && matchingStructures.length == 0) return;
const container = matchingStructures[0];
if (creep.withdraw(container, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(container, {
visualizePathStyle: { stroke: "#ffaa00" }
});
}
} else {
const targets = findStructures([STRUCTURE_STORAGE]);
if (targets.length > 0) {
transferToFirstTarget(targets);
}
}
}
};
module.exports = roleFixer;