-
Notifications
You must be signed in to change notification settings - Fork 0
/
spawn.js
49 lines (43 loc) · 1.54 KB
/
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
module.exports = {
/* spawnCreep spawns, in room r, a creep named n with body b, role t, and
* optional memory m. The type will be placed in the creep's memory's role
* field. It will overwrite anything in m. */
spawnCreep: function (r, n, b, t, m) {
var rid = r.id; /* Room ID */
/* Make sure the caches are set up */
if (Game.rcache === undefined) {
Game.rcache = {};
}
if (Game.rcache[rid] === undefined) {
Game.rcache[rid] = {};
}
if (Game.rcache[rid].spawnInUse === undefined) {
Game.rcache[rid].spawnInUse = {};
}
/* Make sure we have a list of spawns */
findSpawnsInRoom(r);
/* Try to create the creep */
for (var i = 0; i < Game.rcache[rid].spawns.length; ++i) {
/* Ignore spawns in the process of spawning */
if (Game.rcache[rid].spawns[i].spawning) {
continue;
}
if (Game.rcache[rid].spawnInUse[Game.rcache[rid].spawns[i].id] === true) {
continue;
}
/* Creep's memory */
if (m === undefined) {
m = {};
}
m.role = t;
/* Try to make it */
var ret = (Game.rcache[rid].spawns[i].createCreep(b, n, m));
if (_.isString(ret)) {
Game.rcache[rid].spawnInUse[Game.rcache[rid].spawns[i].id] = true;
return ret;
}
return ret;
}
return undefined;
}
};