-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutpost_repairer.js
71 lines (65 loc) · 2.74 KB
/
outpost_repairer.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
const body = [WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE];
module.exports = {
run: function (roomName, spawnRoomName) {
const creepName = 'outpost_repairer_' + roomName;
if(!Game.rooms[roomName]) return;
var roads = Game.rooms[roomName].find(FIND_STRUCTURES, {
filter: (structure) => {
return structure.structureType == STRUCTURE_ROAD && structure.hits <= (structure.hitsMax / 5);
}
});
var creep = Game.creeps[creepName];
if (!creep && roads[0]) {
autoSpawnCreep(creepName, spawnRoomName, body);
}
else if (creep){
if(!creep.pos.inRangeTo(new RoomPosition(25, 25, roomName),24)) creep.moveTo(new RoomPosition(25, 25, roomName));
else{
if(creep.memory.repairing && creep.store[RESOURCE_ENERGY] == 0) {
creep.memory.repairing = false;
}
if(!creep.memory.repairing && creep.store.getFreeCapacity() == 0) {
creep.memory.repairing = true;
}
if(creep.memory.repairing) {
var roads_to_repair = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: (structure) => {
return structure.structureType == STRUCTURE_ROAD && structure.hits < structure.hitsMax;
}
});
if(roads_to_repair) {
if(creep.repair(roads_to_repair) == ERR_NOT_IN_RANGE) {
creep.moveTo(roads_to_repair);
}
}
}
else {
var source = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: (structure) => {
return structure.structureType == STRUCTURE_CONTAINER &&
structure.store.energy > 0;
}
});
if(creep.withdraw(source, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(source);
}
}
}
}
}
}
function autoSpawnCreep(creepName, spawnRoomName, body) {
var spawn = getAvaliableSpawn(spawnRoomName);
if (spawn) {
spawn.spawnCreep(body, creepName, {memory: {role: 'outpost_repairer' }});
}
}
function getAvaliableSpawn(room) {
for (var spawnname in Game.spawns) {
var spawn = Game.spawns[spawnname];
if (spawn.room.name == room && spawn.spawning == null) {
return spawn;
}
}
return null;
}