-
Notifications
You must be signed in to change notification settings - Fork 0
/
role2.scout.js
57 lines (51 loc) · 1.61 KB
/
role2.scout.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
const util = require('util.creep');
const ROLE = 'scout';
const BODY = [TOUGH, MOVE, TOUGH, MOVE];
const BODY_COST = util.bodyCost(BODY);
module.exports = {
ROLE,
exists: function(room) {
var found = false;
_.forEach(Memory.creeps, c => {
if(c.role == ROLE && c.target == room) {
found = true;
return false;
}
});
return found;
},
spawn: function(targetRoom) {
const name = `${ROLE}-${targetRoom}-${Game.time}`;
// find an idle spawner as near the room as possible..
var minPath = Number.POSITIVE_INFINITY;
var spawn;
_.forEach(Game.rooms, r => {
if(!r.controller || !r.controller.my) return;
if(r.energyAvailable < BODY_COST) return;
let spawns = r.find(FIND_MY_SPAWNS, {filter: s => !s.spawning});
if(!spawns.length) return;
let path = Game.map.findRoute(r, targetRoom);
if(path == ERR_NO_PATH) return;
if(path.length >= minPath) return;
minPath = path.length;
// TODO(baptr): Pick the closer spawn to the exit?
spawn = spawns[0];
});
if(!spawn) return ERR_NO_PATH;
console.log(`Spawning ${name} from ${spawn}`);
return spawn.spawnCreep([TOUGH, MOVE, TOUGH, MOVE], name, {
memory: {
target: targetRoom,
role: ROLE
}
});
},
run: function(creep) {
const target = creep.memory.target;
if(!target) {
console.log(`${creep.name} has nothing to do, good day!`);
creep.suicide();
}
creep.moveTo(new RoomPosition(25, 25, target), {visualizePathStyle: {}, reusePath: 100});
}
};