forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.robbing.js
308 lines (306 loc) · 12.2 KB
/
task.robbing.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// This task will react on robbing flags (invade/rob or red/yellow), sending 2 creeps to rob that room
let mod = {};
module.exports = mod;
mod.name = 'robbing';
// hook into events
mod.register = () => {};
// for each flag
mod.handleFlagFound = flag => {
// if it is a robbing flag
if (flag.compareTo(FLAG_COLOR.invade.robbing)) {
// check if a new creep has to be spawned
Task.robbing.checkForRequiredCreeps(flag);
}
};
// check if a new creep has to be spawned
mod.checkForRequiredCreeps = (flag) => {
// get task memory
let memory = Task.robbing.memory(flag);
// count creeps assigned to task
const count = memory.queued.length + memory.spawning.length + memory.running.length;
const roomName = flag.pos.roomName;
// if creep count below requirement spawn a new creep creep
if( count < (memory.numRobbers || 2) ) {
const spawnRoom = mod.strategies.robber.spawnRoom({roomName});
if( !spawnRoom ) {
return;
}
// robbers set homeRoom if closer storage exists
const storageRoom = ROBBER_REHOME && (mod.strategies.robber.homeRoom(flag) || spawnRoom);
Task.spawn(
Task.robbing.creep.robbing, // creepDefinition
{ // destiny
task: mod.name, // taskName
targetName: flag.name, // targetName
homeRoom: storageRoom.name
},
{ // spawn room selection params
targetRoom: roomName,
explicit: spawnRoom.name,
},
creepSetup => { // callback onQueued
let memory = Task.robbing.memory(Game.flags[creepSetup.destiny.targetName]);
memory.queued.push({
room: creepSetup.queueRoom,
name: creepSetup.name
});
});
}
};
// when a creep starts spawning
mod.handleSpawningStarted = params => { // params: {spawn: spawn.name, name: creep.name, destiny: creep.destiny}
// ensure it is a creep which has been queued by this task (else return)
if ( !params.destiny || !params.destiny.task || params.destiny.task != 'robbing' )
return;
// get flag which caused queueing of that creep
// TODO: remove || creep.data.destiny.flagName (temporary backward compatibility)
let flag = Game.flags[params.destiny.targetName || params.destiny.flagName];
if (flag) {
// get task memory
let memory = Task.robbing.memory(flag);
// save spawning creep to task memory
memory.spawning.push(params);
// clean/validate task memory queued creeps
let queued = [];
let validateQueued = o => {
let room = Game.rooms[o.room];
if( (room.spawnQueueMedium.some( c => c.name == o.name)) || (room.spawnQueueLow.some( c => c.name == o.name)) ){
queued.push(o);
}
};
memory.queued.forEach(validateQueued);
memory.queued = queued;
}
};
// when a creep completed spawning
mod.handleSpawningCompleted = creep => {
// ensure it is a creep which has been requested by this task (else return)
if (!creep.data || !creep.data.destiny || !creep.data.destiny.task || creep.data.destiny.task != 'robbing')
return;
if( creep.data.destiny.homeRoom ) {
creep.data.homeRoom = creep.data.destiny.homeRoom;
}
// get flag which caused request of that creep
// TODO: remove || creep.data.destiny.flagName (temporary backward compatibility)
let flag = Game.flags[creep.data.destiny.targetName || creep.data.destiny.flagName];
if (flag) {
// calculate & set time required to spawn and send next substitute creep
// TODO: implement better distance calculation
creep.data.predictedRenewal = creep.data.spawningTime + (routeRange(creep.data.homeRoom, flag.pos.roomName)*50);
// get task memory
let memory = Task.robbing.memory(flag);
// save running creep to task memory
memory.running.push(creep.name);
// clean/validate task memory spawning creeps
let spawning = [];
let validateSpawning = o => {
let spawn = Game.spawns[o.spawn];
if( spawn && ((spawn.spawning && spawn.spawning.name == o.name) || (spawn.newSpawn && spawn.newSpawn.name == o.name))) {
spawning.push(o);
}
};
memory.spawning.forEach(validateSpawning);
memory.spawning = spawning;
}
};
// when a creep died (or will die soon)
mod.handleCreepDied = name => {
// get creep memory
let mem = Memory.population[name];
// ensure it is a creep which has been requested by this task (else return)
if (!mem || !mem.destiny || !mem.destiny.task || mem.destiny.task != 'robbing')
return;
// get flag which caused request of that creep
// TODO: remove || creep.data.destiny.flagName (temporary backward compatibility)
let flag = Game.flags[mem.destiny.targetName || mem.destiny.flagName];
if (flag) {
// get task memory
let memory = Task.robbing.memory(flag);
// clean/validate task memory running creeps
let running = [];
let validateRunning = o => {
let creep = Game.creeps[o];
// invalidate old creeps for predicted spawning
// TODO: better distance calculation
if( creep && creep.name != name && creep.data !== undefined && creep.data.spawningTime !== undefined && creep.ticksToLive > (creep.data.spawningTime + (routeRange(creep.data.homeRoom, flag.pos.roomName)*25) ) ) {
running.push(o);
}
};
memory.running.forEach(validateRunning);
memory.running = running;
}
};
// get task memory
mod.memory = (flag) => {
if( !flag.memory.tasks )
flag.memory.tasks = {};
if( !flag.memory.tasks.robbing ) {
flag.memory.tasks.robbing = {
queued: [],
spawning: [],
running: [],
numRobbers: 2
};
}
return flag.memory.tasks.robbing;
};
mod.nextAction = creep => {
let carrySum = creep.sum;
// at home
if( creep.pos.roomName == creep.data.homeRoom ){
// carrier filled
if( carrySum > 0 ){
if( DEBUG && TRACE ) trace('Task', {creepName:creep.name, pos:creep.pos, nextAction: 'storing?', robbing:'nextAction', Task:'robbing'});
let deposit = []; // deposit energy in...
// links?
if( creep.carry.energy == carrySum ) deposit = creep.room.structures.links.privateers;
// storage?
if( creep.room.storage ) deposit.push(creep.room.storage);
// containers?
if( creep.room.structures.container ) deposit = deposit.concat( creep.room.structures.container.privateers );
// Choose the closest
if( deposit.length > 0 ){
let target = creep.pos.findClosestByRange(deposit);
if( target.structureType == STRUCTURE_STORAGE && Creep.action.storing.assign(creep, target) ) return;
else if(Creep.action.charging.assign(creep, target) ) return;
}
//if( Creep.action.storing.assign(creep) ) return;
if( Creep.action.charging.assign(creep) ) return;
if( !creep.room.ally && Creep.action.storing.assign(creep) ) return;
if (Creep.action.dropping.assign(creep)) return;
Creep.behaviour.worker.nextAction(creep);
return;
}
// empty
// travelling
if( Task[creep.data.destiny.task].exploitNextRoom(creep) ) {
if( DEBUG && TRACE ) trace('Task', {creepName:creep.name, pos:creep.pos, nextAction: 'travelling', robbing:'nextAction', Task:'robbing'});
return;
} else {
// no new flag
// behave as worker
if( DEBUG && TRACE ) trace('Task', {creepName:creep.name, pos:creep.pos, nextAction: 'working', robbing:'nextAction', Task:'robbing'});
Creep.behaviour.worker.nextAction(creep);
return;
}
}
// not at home
else {
// at target room
if( creep.flag && creep.flag.pos.roomName === creep.pos.roomName ){
if( DEBUG && TRACE ) trace('Task', {creepName:creep.name, pos:creep.pos, nextAction: 'robbing', robbing:'nextAction', Task:'robbing'});
// get some energy
if( creep.sum < creep.carryCapacity*0.4 ) {
// harvesting or picking
var actions = [
Creep.action.picking,
Creep.action.robbing
];
for(var iAction = 0; iAction < actions.length; iAction++) {
var action = actions[iAction];
if(action.isValidAction(creep) &&
action.isAddableAction(creep) &&
action.assign(creep))
return;
}
// no targets in current room
if (creep.flag) {
creep.flag.cloaking = 50;
}
Task[creep.data.destiny.task].exploitNextRoom(creep);
return;
}
// carrier full
else {
mod.goHome(creep);
return;
}
}
// not at target room
else {
if( DEBUG && TRACE ) trace('Task', {creepName:creep.name, pos:creep.pos, nextAction: 'travelling2', robbing:'nextAction', Task:'robbing'});
Task[creep.data.destiny.task].exploitNextRoom(creep);
return;
}
}
// fallback
Creep.action.recycling.assign(creep);
};
mod.exploitNextRoom = creep => {
if( creep.sum < creep.carryCapacity*0.4 ) {
// calc by distance to home room
var flag;
if( creep.data.destiny ) flag = Game.flags[creep.data.destiny.flagName];
if( !flag ) flag = mod.getFlag(creep.data.homeRoom);
// new flag found
if( flag ) {
return mod.gotoTargetRoom(creep, flag);
}
}
// no new flag
// go home
return mod.goHome(creep);
};
mod.goHome = creep => {
Population.registerCreepFlag(creep, null);
Creep.action.travelling.assignRoom(creep, creep.data.homeRoom);
return false;
};
mod.getFlag = function(roomName) {
let validColor = flagEntry => (
(flagEntry.color == FLAG_COLOR.invade.robbing.color && flagEntry.secondaryColor == FLAG_COLOR.invade.robbing.secondaryColor)
);
return FlagDir.find(validColor, new RoomPosition(25, 25, roomName), false);
};
mod.storage = function(roomName, storageRoom) {
const memory = Task.robbing.memory(mod.getFlag(roomName));
if (storageRoom) {
const was = memory.storageRoom;
memory.storageRoom = storageRoom;
return `Task.${mod.name}: room ${roomName}, now sending haulers to ${storageRoom}, (was ${was})`;
} else if (!memory.storageRoom) {
return `Task.${mod.name}: room ${roomName}, no custom storage destination`;
} else if (storageRoom === false) {
const was = memory.storageRoom;
delete memory.storageRoom;
return `Task.${mod.name}: room ${roomName}, cleared custom storage room (was ${was})`;
} else {
return `Task.${mod.name}: room ${roomName}, sending haulers to ${memory.storageRoom}`;
}
};
mod.gotoTargetRoom = (creep, flag) => {
if( Creep.action.travelling.assignRoom(creep, flag.pos.roomName) ) {
Population.registerCreepFlag(creep, flag);
return true;
}
};
mod.creep = {
robbing: {
fixedBody: [WORK, CARRY, MOVE, MOVE],
multiBody: [CARRY, MOVE],
name: "robber",
behaviour: "privateer",
queue: 'Low'
},
};
mod.strategies = {
defaultStrategy: {
name: `default-${mod.name}`,
},
robber: {
name: `robber-${mod.name}`,
homeRoom: function(flag) {
// Explicity set by user?
const memory = Task.robbing.memory(flag);
if(memory.storageRoom) return Game.rooms[memory.storageRoom];
// Otherwise, score it
return Room.bestSpawnRoomFor(flag.pos.roomName);
},
spawnRoom: function({roomName, minWeight}) {
return Room.findSpawnRoom({
targetRoom: roomName,
minEnergyCapacity: minWeight || 250,
});
},
}
};