-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.controllerAttacker.js
89 lines (79 loc) · 3.14 KB
/
role.controllerAttacker.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
module.exports = {
// a function to run the logic for this role
run: function(creep) {
// if not in target room
if (creep.room.name != creep.memory.target) {
var hostileCreeps = creep.room.find(FIND_HOSTILE_CREEPS);
{
if(hostileCreeps.length > 0){
const exitDir = creep.room.findExitTo(creep.memory.target);
const exit = creep.pos.findClosestByRange(exitDir);
let ret = PathFinder.search(
creep.pos, exit, {
// We need to set the defaults costs higher so that we
// can set the road cost lower in `roomCallback`
plainCost: 1,
swampCost: 5,
roomCallback(roomName) {
let room = Game.rooms[roomName];
// In this example `room` will always exist, but since
// PathFinder supports searches which span multiple rooms
// you should be careful!
if (!room) return;
let costs = new PathFinder.CostMatrix;
room.find(FIND_STRUCTURES).forEach(function(struct) {
costs.set(struct.pos.x, struct.pos.y, 0xff);
});
room.find(FIND_HOSTILE_CREEPS).forEach(function(hostileCreep) {
if (hostileCreep) {
// pit 1 5x5 swamp around the creeps
costs.set(hostileCreep.pos.x, hostileCreep.pos.y, 10);
for (var offsetx = -5; offsetx < 5; offsetx++) {
for (var offsety = -5; offsety < 5; offsety++) {
costs.set(hostileCreep.pos.x + offsetx, hostileCreep.pos.y + offsety, 10);
}
}
}
});
return costs;
},
}
);
let pos = ret.path[0];
creep.move(creep.pos.getDirectionTo(pos));
}
else{
var exitToRoom = creep.room.findExitTo(creep.memory.target);
creep.travelTo(creep.pos.findClosestByRange(exitToRoom), {maxRooms: 1});
}
}
}
// if in target room
else {
creep.room.findExitTo(creep.memory.home);
if(creep.room.controller.level >= 1 && !creep.room.controller.my){
console.log(creep.claimController(creep.room.controller));
// try to attack controller
if (creep.attackController(creep.room.controller) == ERR_NOT_IN_RANGE) {
// move towards the controller
creep.travelTo(creep.room.controller);
}
}
else {
// try to claim controller
if (creep.claimController(creep.room.controller) == ERR_NOT_IN_RANGE) {
// move towards the controller
creep.travelTo(creep.room.controller);
}
// // if global control level is too low
// if (creep.claimController(creep.room.controller) == ERR_GCL_NOT_ENOUGH) {
// // try to reserve room
// if (creep.reserveController(creep.room.controller) == ERR_NOT_IN_RANGE) {
// // move towards the controller
// creep.travelTo(creep.room.controller);
// }
// }
}
}
}
};