-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.spawn.js
69 lines (56 loc) · 1.87 KB
/
util.spawn.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
var format = require('util.format');
var withDefault = require('util.withDefault');
var loopMem = require('util.loopMem');
module.exports = {
spawn: function(body, build, role, version) {
var lastSpawn = Memory.lastSpawn;
var memory = {
build: build,
role: role,
version: version,
}
Memory.lastSpawn = Game.spawns['Spawn1'].createCreep(body, undefined, memory);
if (lastSpawn == Memory.lastSpawn) {
return null;
}
if (Memory.lastSpawn < 0) {
console.log("Couldn't spawn " + build + "/" + role + ": " +
format.error(Memory.lastSpawn) + " (" + Memory.lastSpawn + ")");
return null;
}
console.log('Spawned new ' + build + "/" + role + ': ' + Memory.lastSpawn);
return Memory.lastSpawn;
},
findSpawn: function() {
return Game.spawns['Spawn1'];
},
requestSpawn: function(priority, build, args) {
console.log("Spawn requested: ", priority, " - ", build.build , " (", args, ")")
var queues = withDefault.getArr(loopMem.get('util.spawn'), 'queues');
var queue = withDefault.getArr(queues, priority);
queue.push({
build: build,
args: args,
});
},
executeQueues: function() {
var queues = loopMem.get('util.spawn').queues;
var numSpawns = Object.keys(Game.spawns).length;
var numQueued = 0;
for (p in queues) {
var queue = queues[p];
for (request of queue) {
request.build.spawn.apply(request.build, request.args);
numQueued++;
if (numQueued >= numSpawns) {
break;
}
}
if (numQueued >= numSpawns) {
break;
}
}
},
PRIORITY_DEFAULT: 5,
PRIORITY_LAST: 10,
};