-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.claimer.js
76 lines (69 loc) · 2.64 KB
/
role.claimer.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
const roleUpgrader = require('role.upgrader');
const roleBootstrapper = require('role2.bootstrapper');
const BodyBuilder = require('util.bodybuilder');
const util = require('util.creep');
function sack(creep) {
spawn = creep.pos.findClosestByPath(FIND_MY_SPAWNS);
if(!spawn) {
pos = creep.pos.findClosestByPath(FIND_MY_CONSTRUCTION_SITES, {
filter: {structureType: STRUCTURE_SPAWN}
});
if(pos) creep.moveTo(pos);
return;
}
if(creep.pos.isNearTo(spawn)) {
spawn.recycleCreep(creep);
} else {
creep.moveTo(spawn);
}
}
const ROLE = 'claimer'
module.exports = {
ROLE,
needed: function(room) {
if(room.controller && room.controller.my) return false;
return !_.find(Memory.creeps, c => c.role == ROLE && c.targetRoom == room.name)
},
spawn: function(spawn, room) {
if(spawn.room.findExitTo(room) < 0) {
console.log(`Unable to spawn claimer for room ${room}, no exit`);
return false;
}
var builder = new BodyBuilder([CLAIM, MOVE], spawn.room.energyAvailable);
builder.extend([MOVE, MOVE, CARRY, WORK], limit=3);
// TODO(baptr): Add some tough if they go through hostile rooms.
var mem = {role: ROLE, targetRoom: room};
return spawn.spawnCreep(builder.sort(), `${ROLE}-${room}`, {memory: mem});
},
run: function(creep) {
// It can happen that the path to the controller ends up leaving the room.
// Without remembering the controller position, the creep might go right
// back in to spot it again, and get stuck on the border.
const targetRoom = creep.memory.targetRoom;
var room = Game.rooms[targetRoom];
if(room) {
var ctrl = room.controller;
if(ctrl.my) return sack(creep);
if(!creep.pos.isNearTo(ctrl)) {
return creep.moveTo(ctrl);
}
var ret = creep.claimController(ctrl);
switch(ret) {
case ERR_GCL_NOT_ENOUGH:
if(Game.time % 20 == 0) console.log(`Too soon to claim ${targetRoom}`);
break;
case OK:
console.log(`Welcome to ${targetRoom}!!`);
if(creep.getActiveBodyparts(WORK) > 0) {
creep.memory.role = roleBootstrapper.ROLE;
}
break;
default:
console.log(`Unrecognized claim(${targetRoom}) ret: ${ret}`);
return ret;
}
} else {
return creep.moveTo(new RoomPosition(25, 25, targetRoom));
}
}
};