-
Notifications
You must be signed in to change notification settings - Fork 2
/
spawner.js
113 lines (97 loc) · 4.04 KB
/
spawner.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
function findNameByRole(spawn, role) {
var index = 0;
var creeps = Game.creeps;
var roomName = spawn.room ? spawn.room.name : 'sim';
while (creeps[ spawn.room.name + 'X' + role + 'X' + index ]) index++;
return {
name: spawn.room.name + 'X' + role + 'X' + index,
index: index
};
}
function spawnByRole(spawn, creeps, role, parts) {
var opt = findNameByRole(spawn, role);
if (spawn.createCreep(parts, opt.name, { role: role, index: opt.index }) == opt.name) {
console.log("Spawning " + role + ": " + opt.name);
return true;
}
else {
return false;
}
}
function countInQueue(spawn, role) {
var inQueue = 0;
for (var i in spawn.memory.queue) {
var creepSpec = spawn.memory.queue[i];
if (creepSpec[0] == role) inQueue++;
}
return inQueue;
}
function checkCreepSupply(spawn, all, creeps, role, count, parts) {
if (spawn.spawning) return;
if (!spawn.memory.queue) spawn.memory.queue = [];
var inRoom = creeps[role] ? creeps[role].length : 0;
var inQueue = countInQueue(spawn, role);
while (inRoom + inQueue < count) {
spawn.memory.queue.push([ role, parts ]);
inQueue++;
}
}
var LIFETIME = 1500;
function spawnCreepEvery(spawn, creeps, role, ticks, stagger, parts) {
if ((Game.time - stagger) % ticks == 0 && countInQueue(spawn, role) == 0) {
if (!spawn.memory.queue) spawn.memory.queue = [];
spawn.memory.queue.push([ role, parts ]);
}
}
function spawnFromQueue(spawn, creeps) {
if (!spawn.memory.queue) return;
if (!spawn.memory.queue.length) return;
if (spawnByRole(spawn, creeps, spawn.memory.queue[0][0], spawn.memory.queue[0][1])) {
spawn.memory.queue.shift();
}
}
var TANKER_BODY = [ MOVE, CARRY, CARRY, MOVE ];
var BIG_TANKER_BODY = [ MOVE, CARRY, CARRY, MOVE, CARRY, CARRY, MOVE ];
var BIGGER_TANKER_BODY = [ MOVE, CARRY, CARRY, MOVE, CARRY, CARRY, MOVE, CARRY, CARRY, MOVE ];
var WORKER_BODY = [ MOVE, WORK, CARRY, MOVE ];
var BIG_WORKER_BODY = [ MOVE, WORK, CARRY, MOVE, WORK, CARRY, MOVE ];
var BIGGER_WORKER_BODY = [ MOVE, WORK, CARRY, MOVE, WORK, CARRY, MOVE, WORK, CARRY, MOVE ];
var GUARD_BODY = [ MOVE, TOUGH, TOUGH, MOVE, ATTACK, MOVE ];
module.exports = function (mon, spawn) {
var allCreeps = mon.findMyCreeps(spawn.room);
var roleCreeps = {};
for (var i in allCreeps) {
var thisCreep = allCreeps[i];
if (!roleCreeps[ thisCreep.memory.role ]) roleCreeps[ thisCreep.memory.role ] = [];
roleCreeps[ thisCreep.memory.role ].push(thisCreep);
}
var totalEnergy = spawn.energy;
var storages = spawn.room.find(FIND_MY_STRUCTURES, {
filter: function(s) {
return s.structureType == STRUCTURE_EXTENSION;
}
});
for (var i in storages) {
totalEnergy += storages[i].energy;
}
var worker = totalEnergy > 750 ? BIGGER_WORKER_BODY
: totalEnergy > 450 ? BIG_WORKER_BODY
: WORKER_BODY;
var tanker = totalEnergy > 500 ? BIGGER_TANKER_BODY
: totalEnergy > 350 ? BIG_TANKER_BODY
: TANKER_BODY;
spawnCreepEvery(spawn, roleCreeps, 'harvester', Math.round(LIFETIME / 6), 0, worker);
checkCreepSupply(spawn, allCreeps, roleCreeps, 'harvester', 1, worker);
// var targets = mon.findHostileCreeps(spawn.room);
// var min = targets.length < 1 ? 1 : targets.length;
// checkCreepSupply(spawn, allCreeps, roleCreeps, 'guard', min, GUARD_BODY);
spawnCreepEvery(spawn, roleCreeps, 'fixer', LIFETIME, 100, worker);
spawnCreepEvery(spawn, roleCreeps, 'keeper', Math.round(LIFETIME / 2), 300, worker);
spawnCreepEvery(spawn, roleCreeps, 'builder', LIFETIME / 2, 200, worker);
var storages = mon.findStorages(spawn.room)
if (storages.length) {
checkCreepSupply(spawn, allCreeps, roleCreeps, 'tanker', 1, tanker);
spawnCreepEvery(spawn, roleCreeps, 'tanker', LIFETIME / 2, 400, tanker);
}
spawnFromQueue(spawn, roleCreeps);
}