forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.reserve.js
270 lines (266 loc) · 11.3 KB
/
task.reserve.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
// This task will react on exploit, reserve and remotemine flags, sending a reserving creep to the flags position.
let mod = {};
module.exports = mod;
mod.name = 'reserve';
mod.spawnRoomMaxRange = 6;
mod.creep = {
reserver: {
fixedBody: {
[CLAIM]: 2,
[MOVE]: 2,
},
multiBody: [CLAIM, MOVE],
maxMulti: 7,
name: "reserver",
behaviour: "claimer"
},
};
// hook into events
mod.register = () => {};
// for each flag
mod.handleFlagFound = flag => {
// if it is a reserve, exploit or remote mine flag
if( flag.compareTo(FLAG_COLOR.claim.reserve) ||
flag.compareTo(FLAG_COLOR.invade.exploit) ||
flag.compareTo(FLAG_COLOR.claim.mining)){
// check if a new creep has to be spawned
Task.reserve.checkForRequiredCreeps(flag);
}
};
// check if a new creep has to be spawned
mod.checkForRequiredCreeps = function(flag) {
let spawnParams;
if (flag.compareTo(FLAG_COLOR.claim.mining)) {
spawnParams = Task.mining.strategies.reserve.spawnParams(flag);
} else if( flag.compareTo(FLAG_COLOR.invade.exploit) ) {
spawnParams = mod.strategies.defaultStrategy.spawnParams(flag);
spawnParams.queue = 'Low'; // privateer reserve is always low queue
} else {
spawnParams = mod.strategies.defaultStrategy.spawnParams(flag);
}
// get task memory
let memory = Task.reserve.memory(flag);
// if low & creep in low queue => move to medium queue
if( spawnParams.queue !== 'Low' && memory.queued.length == 1 ) {
let spawnRoom = Game.rooms[memory.queued[0].room];
let elevate = (entry, index) => {
if( entry.targetName == memory.queued[0].targetName ){
let spawnData = spawnRoom.spawnQueueLow.splice(index, 1);
spawnRoom.spawnQueueMedium.push(spawnData);
return true;
}
return false;
};
spawnRoom.spawnQueueLow.find(elevate);
}
// count creeps assigned to task
let count = memory.queued.length + memory.spawning.length + memory.running.length;
// if creep count below requirement spawn a new creep creep
if( count < spawnParams.count ) {
Task.reserve.creep.reserver.queue = spawnParams.queue;
Task.spawn(
Task.reserve.creep.reserver, // creepDefinition
{ // destiny
task: mod.name, // taskName
targetName: flag.name, // targetName
},
{ // spawn room selection params
targetRoom: flag.pos.roomName,
minEnergyCapacity: 1300,
maxRange: this.spawnRoomMaxRange,
},
creepSetup => { // callback onQueued
let memory = Task.reserve.memory(Game.flags[creepSetup.destiny.targetName]);
memory.queued.push({
room: creepSetup.queueRoom,
name: creepSetup.name,
targetName: flag.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 != mod.name )
return;
// get flag which caused queueing of that creep
let flag = Game.flags[params.destiny.targetName];
if (flag) {
// get task memory
let memory = Task.reserve.memory(flag);
// clean/validate task memory queued creeps
if( memory.valid != Game.time ) Task.reserve.validateMemoryQueued(memory);
// save spawning creep to task memory
memory.spawning.push(params);
}
};
// 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 != mod.name)
return;
// get flag which caused request of that creep
let flag = Game.flags[creep.data.destiny.targetName];
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.reserve.memory(flag);
// clean/validate task memory spawning creeps
if( memory.valid != Game.time ) Task.reserve.validateMemorySpawning(memory);
// save running creep to task memory
memory.running.push(creep.name);
}
};
// when a creep died (or will die soon)
mod.handleCreepDied = name => {
// get creep memory
// console.log('task.reserve.handleCreepDied(' + name + ")" );
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 != mod.name)
return;
// get flag which caused request of that creep
let flag = Game.flags[mem.destiny.targetName];
if (flag) {
// get task memory
let memory = Task.reserve.memory(flag);
// clean/validate task memory running creeps
if( memory.valid != Game.time ) Task.reserve.validateMemoryRunning(memory);
}
};
mod.nextAction = creep => {
// override behaviours nextAction function
// this could be a global approach to manipulate creep behaviour
if (creep.data.destiny && creep.data.destiny.room !== creep.room.name) {
// go to target room
return Creep.action.travelling.assignRoom(creep, creep.data.destiny.room);
}
//Reserve if possible, if not (should be never) then recycle
let priority = [
Creep.action.reserving,
Creep.action.recycling
];
// console.log("bingo")
for(var iAction = 0; iAction < priority.length; iAction++) {
var action = priority[iAction];
if(action.isValidAction(creep) &&
action.isAddableAction(creep) &&
action.assign(creep)) {
break;
}
}
if( DEBUG && TRACE ) trace('Task', {creepName:creep.name, nextAction:creep.action.name, [mod.name]: 'nextAction', Task:mod.name});
};
// get task memory
mod.memory = (flag) => {
if( !flag.memory.tasks )
flag.memory.tasks = {};
if( !flag.memory.tasks.reserve ) {
flag.memory.tasks.reserve = {
valid: Game.time,
queued: [],
spawning: [],
running: []
};
}
let memory = flag.memory.tasks.reserve;
if( !memory.valid || memory.valid < ( Game.time - MEMORY_RESYNC_INTERVAL ) )
Task.reserve.validateMemory(memory);
return memory;
};
mod.validateMemoryQueued = memory => {
// clean/validate task memory queued creeps
let queued = [];
let validateQueued = entry => {
let room = Game.rooms[entry.room];
if( (room.spawnQueueMedium.some( c => c.name == entry.name)) || (room.spawnQueueLow.some( c => c.name == entry.name)) ){
queued.push(entry);
}
};
memory.queued.forEach(validateQueued);
memory.queued = queued;
};
mod.validateMemorySpawning = memory => {
// clean/validate task memory spawning creeps
let spawning = [];
let validateSpawning = entry => {
let spawn = Game.spawns[entry.spawn];
if( spawn && ((spawn.spawning && spawn.spawning.name == entry.name) || (spawn.newSpawn && spawn.newSpawn.name == entry.name))) {
spawning.push(entry);
}
};
memory.spawning.forEach(validateSpawning);
memory.spawning = spawning;
};
mod.validateMemoryRunning = memory => {
// clean/validate task memory running creeps
let running = [];
let validateRunning = entry => {
// invalidate dead or old creeps for predicted spawning
let creep = Game.creeps[entry];
if( !creep || !creep.data ) return;
// TODO: better distance calculation
let prediction;
if( creep.data.predictedRenewal ) prediction = creep.data.predictedRenewal;
else if( creep.data.spawningTime ) prediction = (creep.data.spawningTime + (routeRange(creep.data.homeRoom, flag.pos.roomName)*50));
else prediction = (routeRange(creep.data.homeRoom, flag.pos.roomName) + 1) * 50;
if( creep.ticksToLive > prediction ) {
running.push(entry);
}
};
memory.running.forEach(validateRunning);
memory.running = running;
};
mod.validateMemory = memory => {
Task.reserve.validateMemoryQueued(memory);
Task.reserve.validateMemorySpawning(memory);
Task.reserve.validateMemoryRunning(memory);
memory.valid = Game.time;
};
mod.strategies = {
defaultStrategy: {
name: `default-${mod.name}`,
spawnParams: function(flag) { //:{count:number, priority:string}
const params = {count: 0, queue: 'Low'}; // default to no spawn
const hasFlag = !!flag;
const hasController = hasFlag && (Room.isControllerRoom(flag.pos.roomName) || (flag.room && flag.room.controller));
if (!hasFlag || !hasController) {
if( DEBUG && TRACE ) trace('Task', {hasFlag, hasController, checkForRequiredCreeps:'skipping room, missing flag or controller',
[mod.name]:'checkForRequiredCreeps', Task:mod.name});
return params;
}
if (flag.room) {
flag.memory.lastVisible = Game.time;
flag.memory.ticksToEnd = flag.room.controller.reservation && flag.room.controller.reservation.ticksToEnd;
const validReservation = flag.room.controller.reservation && (flag.room.controller.reservation.ticksToEnd > 1000
|| flag.room.controller.reservation.username !== ME);
const isOwned = !!(flag.room.controller.owner);
if (isOwned || validReservation) {
if( DEBUG && TRACE ) trace('Task', {validReservation, isOwned, checkForRequiredCreeps:'skipping room, reserved or owned',
[mod.name]:'checkForRequiredCreeps', Task:mod.name});
return params;
}
const urgent = !flag.room.controller.reservation || flag.room.controller.reservation.ticksToEnd < 250;
params.count = 1;
if (urgent) params.queue = 'Medium';
if( DEBUG && TRACE ) {
const type = urgent ? 'urgent' : ' ';
trace('Task', {validReservation, isOwned, urgent, checkForRequiredCreeps:`sending${type}reserver`,
[mod.name]:'checkForRequiredCreeps', Task:mod.name});
}
} else if (_.isUndefined(flag.memory.lastVisible) ||
Game.time - flag.memory.lastVisible > ((flag.memory.ticksToEnd - 250) || 250)) {
params.count = 1;
params.queue = 'Medium';
if( DEBUG && TRACE ) trace('Task', {lastVisible: flag.memory.lastVisible,
tickToEnd: flag.memory.ticksToEnd, checkForRequiredCreeps:'sending urgent reserver, no visibility',
[mod.name]:'checkForRequiredCreeps', Task:mod.name});
}
return params;
},
},
};