-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.repairer.js
73 lines (69 loc) · 1.98 KB
/
role.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
72
73
const body = [WORK, WORK, CARRY, CARRY, MOVE, MOVE];
const roleName = "repairer";
/**
* Spawn a creep as a repairer from the designated spawn.
*
* @param {StructureSpawn} spawn: where to spawn the new creep
* @param {Object} [opts] an Object with additional options for the spawning process
* @param {string} [opts] the name of the new creep's home room
*/
function spawn(spawn, opts) {
let args = {
memory: {
role: roleName,
home: spawn.room.name,
},
};
if (opts) {
if (opts instanceof Object) {
_.merge(args, opts);
} else if (typeof opts === "string" && Game.map.isRoomAvailable(opts)) {
args.memory.home = opts;
} else {
return ERR_INVALID_ARGS;
}
}
return spawn.spawnCreep(body, modules.util.genName(roleName, args.memory.home), args);
}
/**
* Instructions to be executed by the creep per tick.
*
* @param {Creep} creep
*/
function run(creep) {
if (creep.room.name !== creep.memory.home) {
return creep.moveTo(new RoomPosition(25, 25, creep.memory.home));
}
if (creep.carry.energy === 0) {
creep.memory.collecting = true;
} else if (creep.carry.energy === creep.carryCapacity) {
creep.memory.collecting = false;
}
if (creep.memory.collecting) {
creep.collectFromStockpile();
} else {
let target = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: (structure) => structure.hits < structure.hitsMax && structure.structureType !== STRUCTURE_WALL
});
if (target) {
if (creep.repair(target) === ERR_NOT_IN_RANGE) {
creep.moveTo(target);
}
} else {
modules.roles["builder"].run(creep);
}
}
}
/**
* Clear out the creep's storage from Memory
*
* @param {string} creepName
*/
function clear(creepName) {
delete Memory.creeps[creepName];
}
module.exports = {
spawn: spawn,
run: run,
clear: clear,
};