forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.mining.js
318 lines (302 loc) · 12.9 KB
/
task.mining.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
309
310
311
312
313
314
315
316
317
318
let mod = {};
module.exports = mod;
mod.minControllerLevel = 4;
mod.name = 'mining';
mod.register = () => {
// when a new flag has been found (occurs every tick, for each flag)
Flag.found.on( flag => Task.mining.handleFlagFound(flag) );
// when a flag has been removed
Flag.FlagRemoved.on( flagName => Task.mining.handleFlagRemoved(flagName) );
// a creep starts spawning
Creep.spawningStarted.on( params => Task.mining.handleSpawningStarted(params) );
Creep.spawningCompleted.on( creep => Task.mining.handleSpawningCompleted(creep) );
};
mod.checkFlag = (flag) => {
if( flag.color == FLAG_COLOR.claim.mining.color && flag.secondaryColor == FLAG_COLOR.claim.mining.secondaryColor ) {
flag.memory.roomName = flag.pos.roomName;
flag.memory.task = mod.name;
return true;
}
return false;
};
mod.handleFlagRemoved = flagName => {
// check flag
let flagMem = Memory.flags[flagName];
if( flagMem && flagMem.task === mod.name && flagMem.roomName ){
// if there is still a mining flag in that room ignore.
let flags = FlagDir.filter(FLAG_COLOR.claim.mining, new RoomPosition(25,25,flagMem.roomName), true);
if( flags && flags.length > 0 )
return;
else {
// no more mining in that room.
// clear memory
Task.clearMemory(mod.name, flagMem.roomName);
}
}
};
mod.handleFlagFound = flag => {
// Analyze Flag
if( Task.mining.checkFlag(flag) ){
// check if a new creep has to be spawned
Task.mining.checkForRequiredCreeps(flag);
}
};
// remove creep from task memory of queued creeps
mod.handleSpawningStarted = params => {
if ( !params.destiny || !params.destiny.task || params.destiny.task != mod.name )
return;
let memory = Task.mining.memory(params.destiny.room);
if( memory.queued[params.destiny.type] ) memory.queued[params.destiny.type].pop();
else if( params.destiny.role ) {
// temporary migration
if( params.destiny.role == "hauler" ) params.destiny.type = 'remoteHauler';
else if( params.destiny.role == "miner" ) params.destiny.type = 'remoteMiner';
else if( params.destiny.role == "worker" ) params.destiny.type = 'remoteWorker';
memory.queued[params.destiny.type].pop();
}
};
mod.handleSpawningCompleted = creep => {
if ( !creep.data.destiny || !creep.data.destiny.task || creep.data.destiny.task != mod.name )
return;
if( creep.data.destiny.homeRoom ) {
creep.data.homeRoom = creep.data.destiny.homeRoom;
}
};
// check if a new creep has to be spawned
mod.checkForRequiredCreeps = (flag) => {
const roomName = flag.pos.roomName;
const room = Game.rooms[roomName];
// Use the roomName as key in Task.memory?
// Prevents accidentally processing same room multiple times if flags > 1
let memory = Task.mining.memory(roomName);
// get number of sources
let sourceCount;
// has visibility. get cached property.
if( room ) sourceCount = room.sources.length;
// no visibility, but been there before
else if( Memory.rooms[roomName] && Memory.rooms[roomName].sources ) sourceCount = Memory.rooms[roomName].sources.length
// never been there
else sourceCount = 1;
// TODO: don't iterate/filter all creeps (3 times) each tick. store numbers into memory (see guard tasks)
const creepsByType = _.chain(Game.creeps)
.filter(function(c) {return c.data && c.data.destiny && c.data.destiny.room===roomName;})
.groupBy('data.creepType').value();
let existingHaulers = creepsByType.remoteHauler || [];
let haulerCount = memory.queued.remoteHauler.length + existingHaulers.length;
let existingMiners = creepsByType.remoteMiner || [];
let minerCount = memory.queued.remoteMiner.length +
_.sum(existingMiners, function(c) {return (c.ticksToLive || CREEP_LIFE_TIME) > (c.data.predictedRenewal || 0)});
let workerCount = memory.queued.remoteWorker.length + (creepsByType.remoteWorker || []).length;
// TODO: calculate creeps by type needed per source / mineral
if( DEBUG && TRACE ) trace('Task', {Task:mod.name, flagName:flag.name, sourceCount, haulerCount, minerCount, workerCount, [mod.name]:'Flag.found'}, 'checking flag@', flag.pos);
if(minerCount < sourceCount) {
if( DEBUG && TRACE ) trace('Task', {Task:mod.name, room:flag.pos.roomName, minerCount,
minerTTLs: _.map(existingMiners, "ticksToLive"), [mod.name]:'minerCount'});
for(let i = minerCount; i < sourceCount; i++) {
Task.spawn(
Task.mining.creep.miner, // creepDefinition
{ // destiny
task: mod.name, // taskName
targetName: flag.name, // targetName
type: Task.mining.creep.miner.behaviour // custom
},
{ // spawn room selection params
targetRoom: flag.pos.roomName,
minEnergyCapacity: 800
},
creepSetup => { // onQueued callback
let memory = Task.mining.memory(creepSetup.destiny.room);
memory.queued[creepSetup.behaviour].push({
room: creepSetup.queueRoom,
name: creepSetup.name
});
}
);
}
}
// only spawn haulers for sources a miner has been spawned for
let runningMiners = _.filter(existingMiners, creep => creep.spawning === false);
let maxHaulers = Math.ceil(runningMiners.length * REMOTE_HAULER_MULTIPLIER);
if(haulerCount < maxHaulers) {
for(let i = haulerCount; i < maxHaulers; i++) {
const spawnRoom = mod.strategies.hauler.spawnRoom(roomName);
if( !spawnRoom ) break;
// haulers set homeRoom if closer storage exists
const storageRoom = REMOTE_HAULER_REHOME && mod.strategies.hauler.homeRoom(roomName) || spawnRoom;
const maxWeight = mod.strategies.hauler.maxWeight(
roomName, storageRoom, existingHaulers, memory); // TODO Task.strategies
if( !maxWeight || (i > 1 && maxWeight < REMOTE_HAULER_MIN_WEIGHT)) break;
const creepDefinition = _.create(Task.mining.creep.hauler);
creepDefinition.maxWeight = maxWeight;
Task.spawn(
creepDefinition,
{ // destiny
task: mod.name, // taskName
targetName: flag.name, // targetName
type: Task.mining.creep.hauler.behaviour, // custom
homeRoom: storageRoom.name
}, {
targetRoom: roomName,
explicit: spawnRoom.name,
},
creepSetup => { // onQueued callback
let memory = Task.mining.memory(creepSetup.destiny.room);
memory.queued[creepSetup.behaviour].push({
room: creepSetup.queueRoom,
name: creepSetup.name,
weight: Creep.bodyCosts(creepSetup.parts),
});
}
);
}
}
if( room && room.constructionSites.length > 0 && workerCount < REMOTE_WORKER_MULTIPLIER) {
for(let i = workerCount; i < REMOTE_WORKER_MULTIPLIER; i++) {
Task.spawn(
Task.mining.creep.worker, // creepDefinition
{ // destiny
task: mod.name, // taskName
targetName: flag.name, // targetName
type: Task.mining.creep.worker.behaviour // custom
},
{ // spawn room selection params
targetRoom: roomName,
minEnergyCapacity: 600
},
creepSetup => { // onQueued callback
let memory = Task.mining.memory(creepSetup.destiny.room);
memory.queued[creepSetup.behaviour].push({
room: creepSetup.queueRoom,
name: creepSetup.name
});
}
);
}
}
};
mod.memory = key => {
let memory = Task.memory(mod.name, key);
if( !memory.hasOwnProperty('queued') ){
memory.queued = {
remoteMiner:[],
remoteHauler:[],
remoteWorker:[]
};
}
// temporary migration
if( memory.queued.miner ){
memory.queued.remoteMiner = memory.queued.miner;
delete memory.queued.miner;
}
if( memory.queued.hauler ){
memory.queued.remoteHauler = memory.queued.hauler;
delete memory.queued.hauler;
}
if( memory.queued.worker ){
memory.queued.remoteWorker = memory.queued.worker;
delete memory.queued.worker;
}
return memory;
};
mod.creep = {
miner: {
fixedBody: [MOVE, MOVE, MOVE, WORK, WORK, WORK, WORK, WORK, WORK, CARRY],
multiBody: [],
behaviour: 'remoteMiner',
queue: 'Low'
},
hauler: {
fixedBody: [CARRY, CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, WORK],
multiBody: [CARRY, CARRY, MOVE],
behaviour: 'remoteHauler',
queue: 'Low'
},
worker: {
fixedBody: [MOVE, MOVE, MOVE, CARRY, CARRY, CARRY, WORK, WORK, WORK],
multiBody: [],
behaviour: 'remoteWorker',
queue: 'Low'
}
};
mod.carry = function(roomName, partChange) {
let memory = Task.mining.memory(roomName);
memory.carryParts = (memory.carryParts || 0) + (partChange || 0);
return `Task.${mod.name} set hauler carry parts for ${roomName} to ${memory.carryParts}`;
};
mod.storage = function(roomName, storageRoom) {
const room = Game.rooms[roomName];
let memory = Task.mining.memory(roomName);
if (storageRoom) {
memory.storageRoom = storageRoom;
return `Task.${mod.name} set ${roomName} storage destination for haulers to ${storageRoom}`;
} else if (!memory.storageRoom) {
return `Task.${mod.name} mining ${roomName} custom storage rooms not set`;
} else if (storageRoom === false) {
const was = memory.storageRoom;
delete memory.storageRoom;
return `Task.${mod.name} cleared ${roomName} custom storage room, was ${was}`;
} else {
return `Task.${mod.name} mining ${roomName} sending haulers to ${memory.storageRoom}`
}
};
function haulerWeightToCarry(weight) {
if( !weight || weight < 0) return 0;
const multiWeight = _.max([0, weight - 500]);
return 5 + 2 * _.floor(multiWeight / 150);
}
function haulerCarryToWeight(carry) {
if( !carry || carry < 0) return 0;
const multiCarry = _.max([0, carry - 5]);
return 500 + 150 * _.ceil(multiCarry * 0.5);
}
mod.strategies = {
defaultStrategy: {
name: `default-${mod.name}`,
},
hauler: {
name: `hauler-${mod.name}`,
homeRoom: function(flagRoomName) {
// Explicity set by user?
let memory = Task.mining.memory(flagRoomName);
if(memory.storageRoom) return Game.rooms[memory.storageRoom];
// Otherwise, score it
return Room.findSpawnRoom({
targetRoom: flagRoomName,
minRCL: 4,
callBack: function(r){return r.storage},
rangeRclRatio: Infinity,
rangeQueueRatio: Infinity,
});
},
spawnRoom: function(flagRoomName) {
return Room.findSpawnRoom({
targetRoom: flagRoomName,
minEnergyCapacity: 500
});
},
maxWeight: function(roomName, travelRoom, existingCreeps, memory) {
if( !memory ) memory = Task.mining.memory(roomName);
if( !existingCreeps ) existingCreeps = [];
const queuedCreeps = memory.queued.remoteHauler;
const room = Game.rooms[roomName];
const travel = routeRange(roomName, travelRoom.name);
let ept = 10;
if( room ) {
ept = 10 * room.sources.length;
} else if( travel > 3 ) {
ept = 20; // assume profitable
}
// carry = ept * travel * 2 * 50 / 50
const existingCarry = _.chain(existingCreeps)
.filter(function(c) {return (c.ticksToLive || CREEP_LIFE_TIME) > (50 * travel - 40 + c.data.spawningTime)})
.sum(function(c) {return haulerWeightToCarry(c.data.weight || 500)}).value();
const queuedCarry = _.sum(queuedCreeps, c=>haulerWeightToCarry(c.weight || 500));
const neededCarry = ept * travel * 2 + (memory.carryParts || 0) - existingCarry - queuedCarry;
const maxWeight = haulerCarryToWeight(neededCarry);
if( DEBUG && TRACE ) trace('Task', {Task:mod.name, room: roomName, travelRoom: travelRoom.name,
haulers: existingCreeps.length + queuedCreeps.length, ept, travel, existingCarry, queuedCarry,
neededCarry, maxWeight, [mod.name]:'maxWeight'});
return maxWeight;
}
},
};