-
Notifications
You must be signed in to change notification settings - Fork 40
/
spawner.js
159 lines (126 loc) · 2.6 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
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
module.exports =
{
initSpawnQue: function()
{
if(Memory.spawnQue == undefined)
Memory.spawnQue = [ ];
},
addToQue: function(creep, unshift)
{
this.initSpawnQue();
if(unshift != undefined && unshift === true)
Memory.spawnQue.unshift(creep);
else
Memory.spawnQue.push(creep);
},
spawnNextInQue: function()
{
this.initSpawnQue();
if(!Memory.spawnQue.length)
return;
var spawns = Game.getRoom('1-1').find(Game.MY_SPAWNS, {
filter: function(spawn)
{
return spawn.spawning === undefined || spawn.spawning === null;
}
});
if(!spawns.length)
return;
var role = Memory.spawnQue[0];
if(typeof role == "string")
{
role = { type: role, memory: { } };
}
var me = this;
var toSpawnAt = spawns.filter(function(spawn)
{
return me.canSpawn(spawn, role.type);
});
if(! toSpawnAt.length)
return;
toSpawnAt = toSpawnAt[0];
this.spawn(role.type, role.memory, toSpawnAt);
Memory.spawnQue.shift();
},
spawn: function(role, memory, spawnPoint)
{
if(!spawnPoint)
spawnPoint = Game.spawns.Spawn1;
var manager = require('roleManager');
if(!manager.roleExists(role))
{
return;
}
if(!this.canSpawn(spawnPoint, role))
{
return;
}
if(memory == undefined)
memory = { };
memory['role'] = role;
var nameCount = 0;
var name = null;
while(name == null)
{
nameCount++;
var tryName = role + nameCount;
if(Game.creeps[tryName] == undefined)
name = tryName;
}
console.log('Spawning ' + role);
spawnPoint.createCreep(manager.getRoleBodyParts(role), name, memory);
},
canSpawn: function(spawnPoint, role)
{
if(typeof spawnPoint == "string" && role == undefined)
{
role = spawnPoint;
spawnPoint = Game.spawns.Spawn1;
}
return spawnPoint.energy >= this.spawnCost(role)
&& (spawnPoint.spawning == null
|| spawnPoint.spawning == undefined);
},
spawnCost: function(role)
{
var manager = require('roleManager');
var parts = manager.getRoleBodyParts(role);
var total = 0;
for(var index in parts)
{
var part = parts[index];
switch(part)
{
case Game.MOVE:
total += 50
break;
case Game.WORK:
total += 20
break;
case Game.CARRY:
total += 50
break;
case Game.ATTACK:
total += 100
break;
case Game.RANGED_ATTACK:
total += 150
break;
case Game.HEAL:
total += 200
break;
case Game.TOUGH:
total += 5
break;
}
}
return total;
},
killAll: function(role)
{
for(var i in Game.creeps) {
if(role == undefined || Game.creeps[i].memory.role == role)
Game.creeps[i].suicide();
}
}
}