-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.SKRoomEnergyMiner.js
265 lines (234 loc) · 9.68 KB
/
role.SKRoomEnergyMiner.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
module.exports = {
// a function to run the logic for this role
run: function(creep) {
const source = Game.getObjectById(creep.memory.sourceId);
var sourceKeeperLair = undefined;
// get target room if not already in memory
if (creep.memory.target == undefined) {
creep.memory.target = source.room.name;
}
// move to target room if not in it
if (creep.memory.target != undefined && creep.room.name != creep.memory.target) {
// find exit to target room
var exit = creep.room.findExitTo(creep.memory.target);
// move to exit
creep.travelTo(creep.pos.findClosestByRange(exit));
// return the function to not do anything else
return;
}
// look for soureKeeperLair by source if there isn't one in memory
if (creep.memory.sourceKeeperLair == undefined) {
sourceKeeperLair = source.pos.findClosestByRange(FIND_HOSTILE_STRUCTURES, {
filter: s => s.structureType == STRUCTURE_KEEPER_LAIR
});
creep.memory.sourceKeeperLair = sourceKeeperLair;
}
var closestHostileCreep = creep.pos.findInRange(FIND_HOSTILE_CREEPS, 6);
// if there's a hostile creep within 6 spaces, pathfind away from hostile
if (closestHostileCreep.length > 0) {
let targets = _.map(creep.room.find(FIND_HOSTILE_CREEPS),
c => ({
pos: c.pos,
range: 5
}));
let ret = PathFinder.search(
creep.pos, targets, {
plainCost: 2,
swampCost: 10,
flee: true,
roomCallback: function(roomName) {
let room = Game.rooms[roomName];
if (!room) return;
let costs = new PathFinder.CostMatrix;
room.find(FIND_STRUCTURES).forEach(function(struct) {
if (struct.structureType === STRUCTURE_ROAD) {
// Favor roads over plain tiles
costs.set(struct.pos.x, struct.pos.y, 1);
} else if (struct.structureType !== STRUCTURE_CONTAINER &&
(struct.structureType !== STRUCTURE_RAMPART ||
!struct.my)) {
// Can't walk through non-walkable buildings
costs.set(struct.pos.x, struct.pos.y, 0xff);
}
});
// Avoid creeps in the room
room.find(FIND_CREEPS).forEach(function(creep) {
costs.set(creep.pos.x, creep.pos.y, 0xff);
});
return costs;
},
}
);
let pos = ret.path[0];
creep.move(creep.pos.getDirectionTo(pos));
return;
}
sourceKeeperLair = Game.getObjectById(creep.memory.sourceKeeperLair.id);
// check to see if a keeper is about to spawn and move away if necessary
if (sourceKeeperLair.ticksToSpawn < 10 || sourceKeeperLair.ticksToSpawn == undefined) {
let fleePoint = {
pos: sourceKeeperLair.pos,
range: 5
};
let ret = PathFinder.search(
creep.pos, fleePoint, {
plainCost: 2,
swampCost: 10,
flee: true,
roomCallback: function(roomName) {
let room = Game.rooms[roomName];
if (!room) return;
let costs = new PathFinder.CostMatrix;
room.find(FIND_STRUCTURES).forEach(function(struct) {
if (struct.structureType === STRUCTURE_ROAD) {
// Favor roads over plain tiles
costs.set(struct.pos.x, struct.pos.y, 1);
} else if (struct.structureType !== STRUCTURE_CONTAINER &&
(struct.structureType !== STRUCTURE_RAMPART ||
!struct.my)) {
// Can't walk through non-walkable buildings
costs.set(struct.pos.x, struct.pos.y, 0xff);
}
});
return costs;
},
}
);
let pos = ret.path[0];
creep.move(creep.pos.getDirectionTo(pos));
return;
}
if (source) {
// find container next to source
let container = source.pos.findInRange(FIND_STRUCTURES, 1, {
filter: s => s.structureType == STRUCTURE_CONTAINER
})[0];
// if there is a container by the source
if (container != undefined) {
// if the creep is not on top of the container, move towards it
if (!creep.pos.isEqualTo(container.pos)) {
let ret = PathFinder.search(
creep.pos, container.pos, {
// 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_HOSTILE_CREEPS).forEach(function(hostileCreep) {
if (hostileCreep) {
// pit 1 5x5 swamp around the creeps
costs.set(hostileCreep.pos.x, hostileCreep.pos.y, 0xff);
for (var offsetx = -4; offsetx < 4; offsetx++) {
for (var offsety = -4; offsety < 4; offsety++) {
costs.set(hostileCreep.pos.x + offsetx, hostileCreep.pos.y + offsety, 0xff);
}
}
}
});
return costs;
},
}
);
let pos = ret.path[0];
creep.move(creep.pos.getDirectionTo(pos));
}
// if the creep is on top of the container, harvest
else {
if (creep.memory.travellingToTarget) {
creep.memory.travellingToTarget = false;
}
// if the container does need to be repaired
if (container.hits < container.hitsMax) {
creep.harvest(source);
creep.repair(container);
}
// if the container doesn't need to be repaired
else {
creep.harvest(source)
}
}
}
// if there isn't a container, put a constructionSite by it and build it
else {
// look for a construciton site by the source
var constructionSiteBySource = source.pos.findInRange(FIND_CONSTRUCTION_SITES, 1);
// if there isn't a construction site by the source, create one
if (constructionSiteBySource == undefined || constructionSiteBySource == '' || constructionSiteBySource == null) {
var offsetx, offsety = 0;
buildSite:
for (var offsetx = -1; offsetx < 3; offsetx++) {
for (var offsety = -1; offsety < 3; offsety++) {
console.log(source.pos.x + offsetx, source.pos.y + offsety, creep.room.createConstructionSite(source.pos.x + offsetx, source.pos.y + offsety, STRUCTURE_CONTAINER))
if(creep.room.createConstructionSite(source.pos.x + offsetx, source.pos.y + offsety, STRUCTURE_CONTAINER) == 0){
creep.room.createConstructionSite(source.pos.x + offsetx, source.pos.y + offsety, STRUCTURE_CONTAINER);
break buildSite;
}
}
}
}
// if there is a construction site by the source, go to the source and start building
else {
// assign the construction site to creep's memory
if (creep.memory.targetContainerConstructionSite == undefined) {
creep.memory.targetContainerConstructionSite = constructionSiteBySource;
}
const totalCarry = _.sum(creep.carry);
// if the creep is by the source
if (creep.pos.inRangeTo(source, 1)) {
// if the creep is full of energy
if (_.sum(creep.carry) == creep.carryCapacity) {
const target = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES);
creep.build(target);
if (creep.memory.travellingToTarget) {
creep.memory.travellingToTarget = false;
}
}
// if the creep isn't full of energy
else {
creep.harvest(source);
}
}
// if the creep is not by the source, move towards it.
else {
let ret = PathFinder.search(
creep.pos, source.pos, {
// 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_HOSTILE_CREEPS).forEach(function(hostileCreep) {
if (hostileCreep) {
// pit 1 5x5 swamp around the creeps
costs.set(hostileCreep.pos.x, hostileCreep.pos.y, 0xff);
for (var offsetx = -3; offsetx < 3; offsetx++) {
for (var offsety = -3; offsety < 3; offsety++) {
costs.set(hostileCreep.pos.x + offsetx, hostileCreep.pos.y + offsety, 0xff);
}
}
}
});
return costs;
},
}
);
let pos = ret.path[0];
creep.move(creep.pos.getDirectionTo(pos));
}
}
}
}
}
};