-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.attacker.js
211 lines (197 loc) · 7.61 KB
/
role.attacker.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
module.exports = {
// a function to run the logic for this role
/** @param {Creep} creep */
run: function(creep) {
var isAttackingCreeps = true,
isAttackingWalls = false,
isAttackingHostileStructures = true;
// if target is defined and creep is not in target room
if (creep.memory.target != undefined && 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, 5);
for (var offsetx = -5; offsetx < 5; offsetx++) {
for (var offsety = -5; offsety < 5; offsety++) {
costs.set(hostileCreep.pos.x + offsetx, hostileCreep.pos.y + offsety, 5);
}
}
}
});
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));
}
}
return;
}
var enemyCreeps = creep.room.find(FIND_HOSTILE_CREEPS);
if (enemyCreeps.length > 0 && creep.room.name != creep.memory.target) {
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));
return;
}
// if creep is in target room, find nearest hostile creep
else {
var target;
if(isAttackingCreeps){
// find nearest hostile creep
target = creep.pos.findClosestByRange(FIND_HOSTILE_CREEPS);
// if there is a hostile creep target
if (target != undefined) {
isAlly = false;
for (ally in Memory.allies) {
if (target.owner.username == Memory.allies[ally]) {
isAlly = true;
}
}
if (!isAlly) {
// ...FIRE!
// if hostile creep is in range
if (creep.attack(target) != ERR_NOT_IN_RANGE) {
// ATTACK!
creep.attack(target);
creep.say("ATTACK 🗡️");
// if hostile creep is not in range
} else if (creep.attack(target) == ERR_NOT_IN_RANGE) {
// move towards hostile creep
creep.travelTo(target);
}
}
}
else{
target = undefined
}
}
if(isAttackingWalls && target == undefined){
target = creep.pos.findClosestByPath(FIND_STRUCTURES, {
// the second argument for findClosestByPath is an object which takes
// a property called filter which can be a function
// we use the arrow operator to define it
filter: (s) => (s.structureType == STRUCTURE_WALL)
});
if(target != undefined){
if (creep.attack(target) != ERR_NOT_IN_RANGE) {
// ATTACK!
creep.attack(target);
creep.say("ATTACK 🗡️");
// if the hostile structure is NOT in range
}
else if (creep.attack(target) == ERR_NOT_IN_RANGE) {
// move towards hostile structure
creep.travelTo(target);
}
}
}
// find hostile structure if nothing has been found so far
if (isAttackingHostileStructures && target == undefined) {
if (target == undefined) {
// find a hostile structure
target = creep.pos.findClosestByPath(FIND_HOSTILE_STRUCTURES, {
// the second argument for findClosestByPath is an object which takes
// a property called filter which can be a function
// we use the arrow operator to define it
filter: (s) => (s.structureType == STRUCTURE_EXTENSION)
});
}
if (target == undefined) {
// find a hostile structure
target = creep.pos.findClosestByPath(FIND_HOSTILE_STRUCTURES, {
// the second argument for findClosestByPath is an object which takes
// a property called filter which can be a function
// we use the arrow operator to define it
filter: (s) => (s.structureType == STRUCTURE_RAMPART)
});
}
// if there is a hostile structure
if (target != undefined) {
isAlly = false;
for (ally in Memory.allies) {
if(target.owner != undefined)
{
if (target.owner.username == Memory.allies[ally]) {
isAlly = true;
}
}
}
if (!isAlly) {
target = Game.getObjectById('598781e85627db4542315ae7')
// if hostile structure is in range
if (creep.attack(target) != ERR_NOT_IN_RANGE) {
// ATTACK!
creep.attack(target);
creep.say("ATTACK 🗡️");
// if the hostile structure is NOT in range
} else if (creep.attack(target) == ERR_NOT_IN_RANGE) {
// move towards hostile structure
creep.travelTo(target);
}
}
}
}
//}
//}
}
}
};