forked from shryme/Screeps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.js
273 lines (218 loc) · 9.26 KB
/
factory.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
//List of module prices
var prices = {};
prices.move = 50;
prices.work = 20;
prices.carry = 50;
prices.attack = 80;
prices.ranged_attack = 150;
prices.heal = 200;
prices.tough = 20;
//List of position to take for lazy harvester and bus
var listAllBus = Game.spawns.Spawn1.room.memory.listAllBus;
var listAllHarvester = Game.spawns.Spawn1.room.memory.listAllHarvester;
function getListOfOpenPosHarvester(type, currentList) {
var newList = new Array();
var listToFill;
if (type === 'bus')
listToFill = listAllBus;
else
listToFill = listAllHarvester;
for (var i = 0; i < listToFill.length; i++) {
var x = listToFill[i].toGo.x;
var y = listToFill[i].toGo.y;
var isUsed = false;
for (var j = 0; j < currentList.length; j++) {
if (currentList[j].toGo.x === x && currentList[j].toGo.y === y) {
isUsed = true;
break;
}
}
if (!isUsed)
newList.push(listToFill[i]);
}
return newList;
}
//Calculate cost and detect if it's fine to spawn a new creep
function getCost(arr) {
var total = 0;
for (var i = 0; i < arr.length; i++) {
total += prices[arr[i]];
}
return total;
}
function canSpawnUnit(arr) {
var unit = getCost(arr);
var harvester = getCost([Game.WORK, Game.WORK, Game.WORK, Game.CARRY, Game.MOVE]);
var bus = getCost([Game.CARRY, Game.MOVE]);
var allBus = bus * (Game.spawns.Spawn1.memory.path.length - 2);
if (Game.spawns.Spawn1.energy > unit + harvester + allBus) {
return true;
}
return false
}
//Creation creep
function spawnWeakGuard() {
var modules = [Game.TOUGH, Game.MOVE, Game.ATTACK, Game.MOVE, Game.ATTACK, Game.RANGED_ATTACK];
if (canSpawnUnit(modules))
Game.spawns.Spawn1.createCreep(modules, undefined, {role: 'guard'});
}
function spawnWeakBuilder() {
var modules = [Game.CARRY, Game.CARRY, Game.WORK, Game.WORK, Game.MOVE];
if (canSpawnUnit(modules))
Game.spawns.Spawn1.createCreep(modules, undefined, {role: 'builder'});
}
function spawnWeakHealer(targetRole) {
var modules = [Game.HEAL, Game.HEAL, Game.HEAL, Game.MOVE, Game.MOVE];
if (canSpawnUnit(modules))
Game.spawns.Spawn1.createCreep(modules, undefined, {role: 'healer', targetRole: targetRole});
}
function spawnBigHealer() {
var modules = [Game.TOUGH, Game.TOUGH, Game.HEAL, Game.HEAL, Game.HEAL, Game.HEAL, Game.MOVE];
if (canSpawnUnit(modules))
Game.spawns.Spawn1.createCreep(modules, undefined, {role: 'healer'});
}
function spawnExtensionGuard(pieces) {
if (canSpawnUnit(pieces))
Game.spawns.Spawn1.createCreep(pieces, undefined, {role: 'guard'});
}
function spawnToughGuard() {
var modules = [Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.MOVE, Game.MOVE, Game.ATTACK, Game.ATTACK, Game.ATTACK];
if (canSpawnUnit(modules))
Game.spawns.Spawn1.createCreep(modules, undefined, {role: 'guard'});
}
function spawnMediumGuard() {
var modules = [Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.ATTACK, Game.ATTACK, Game.ATTACK, Game.MOVE, Game.ATTACK];
if (canSpawnUnit(modules))
Game.spawns.Spawn1.createCreep(modules, undefined, {role: 'guard'});
}
function spawnSourceHealer(list, isTemp) {
var modules = [Game.HEAL, Game.HEAL, Game.HEAL, Game.HEAL, Game.MOVE];
if (canSpawnUnit(modules))
Game.spawns.Spawn1.createCreep(modules, undefined, {role: 'source_healer'});
}
function findClosestSourceKeeperSpot(from) {
//Game.spawns.Spawn1.pos.findClosest(Game.HOSTILE_STRUCTURES).ticksToSpawn;
if (Game.spawns.Spawn1.memory.sourceKeeperPos === undefined) {
var lair = from.pos.findClosest(Game.HOSTILE_STRUCTURES);
var sourceKeeper = lair.pos.findClosest(Game.HOSTILE_CREEPS);
sourceKeeper.pos.x = sourceKeeper.pos.x + 1;
Game.spawns.Spawn1.memory.sourceKeeperPos = sourceKeeper.pos;
}
return Game.spawns.Spawn1.memory.sourceKeeperPos;
}
function spawnSourceDestroyer() {
var keeperPos = findClosestSourceKeeperSpot(Game.spawns.Spawn1);
keeperPos.x = 45;
keeperPos.y = 26;
var modules = [Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.TOUGH,Game.RANGED_ATTACK, Game.RANGED_ATTACK, Game.RANGED_ATTACK, Game.RANGED_ATTACK, Game.MOVE];
if (canSpawnUnit(modules))
Game.spawns.Spawn1.createCreep(modules, undefined, {role: 'source_destroyer', targetPos: keeperPos});
}
function spawnSourceHarvester() {
var modules = [Game.MOVE, Game.WORK, Game.WORK, Game.WORK, Game.CARRY];
if (canSpawnUnit(modules))
Game.spawns.Spawn1.createCreep(modules, undefined, {role: 'source_harvester'});
}
function spawnSourceCarrier() {
var modules = [Game.MOVE, Game.CARRY, Game.CARRY, Game.CARRY, Game.MOVE];
if (canSpawnUnit(modules))
Game.spawns.Spawn1.createCreep(modules, undefined, {role: 'source_carrier'});
}
module.exports = {
isDone: function(nbHarvester, nbBus, listBus, listHarvester) {
//Calculate the path to take to go to the nearest source
if (listAllBus === undefined) {
listAllBus = new Array();
listAllHarvester = new Array();
var source = Game.spawns.Spawn1.pos.findClosest(Game.SOURCES_ACTIVE, {maxOps: 1000, ignoreDestructibleStructures: true, ignoreCreeps: true});
Game.spawns.Spawn1.memory.path = Game.spawns.Spawn1.room.findPath(Game.spawns.Spawn1.pos, source.pos, {maxOps: 3000, ignoreDestructibleStructures: true, ignoreCreeps: true});
var path = Game.spawns.Spawn1.memory.path;
for (var i = 0; i < path.length - 1; i++) {
if (i === 0) {
listAllHarvester.push({toGo: path[path.length - 2], toDrop: path[path.length - 3]});
//TODO don't hardcode this part
//TODO need to detect where to put second harvester
listAllHarvester.push({toGo: {x: path[path.length - 2].x, y: path[path.length - 2].y - 1}, toDrop: path[path.length - 2]});
}
else {
var currentPath = {toGo: path[path.length - 2 - i], toDrop: path[path.length - 3 - i]}
listAllBus.push(currentPath);
}
}
Game.spawns.Spawn1.room.memory.listAllBus = listAllBus;
Game.spawns.Spawn1.room.memory.listAllHarvester = listAllHarvester;
}
//Gets the positions that are currently open and that needs to be filled
var listNewPos = getListOfOpenPosHarvester('bus', listBus);
var listNewHarvesterPos = getListOfOpenPosHarvester('harverster', listHarvester);
//First create the first harvester, then all the transporters, then the rest of the harvester
if (nbHarvester < 1 && listNewHarvesterPos.length > 0) {
Game.spawns.Spawn1.createCreep([Game.WORK, Game.WORK, Game.WORK, Game.CARRY, Game.MOVE], undefined,
{role: 'lazy_harvester', toGo: listNewHarvesterPos[0].toGo, toDrop: listNewHarvesterPos[0].toDrop}); //160
}
else if (listNewPos !== undefined && listNewPos.length > 0) {
Game.spawns.Spawn1.createCreep([Game.CARRY, Game.MOVE], undefined,
{role: 'bus', toGo: listNewPos[0].toGo, toDrop: listNewPos[0].toDrop}); //100
}
else if (listNewHarvesterPos.length > 0) {
Game.spawns.Spawn1.createCreep([Game.WORK, Game.WORK, Game.WORK, Game.CARRY, Game.MOVE], undefined,
{role: 'lazy_harvester', toGo: listNewHarvesterPos[0].toGo, toDrop: listNewHarvesterPos[0].toDrop}); //160
}
else {
return true
}
return false;
},
createRobotz: function(data) {
if (this.isDone(data.lazy_harvesterNumbers, data.busNumbers, data.listBus, data.listHarvester)) {
/*var targets = Game.spawns.Spawn1.pos.findInRange(Game.HOSTILE_CREEPS, 8);
if (targets.length > 0) {
}*/
/*if (data.guardNumbers >= 8 && Game.spawns.Spawn1) {
Game.spawns.Spawn1.createCreep([Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.RANGED_ATTACK, Game.RANGED_ATTACK, Game.RANGED_ATTACK, Game.MOVE, Game.RANGED_ATTACK], undefined, {role: 'guard'});
}*/
var extNumbers = Game.spawns.Spawn1.room.find(Game.MY_STRUCTURES, {filter: function(object) {return object.structureType === Game.STRUCTURE_EXTENSION && object.energy === object.energyCapacity} }).length;
if (data.guardNumbers < 2) {
spawnWeakGuard();
}
//TODO Upgrade the detection for when the spot is safe for harvesting before doing this
else if (data.sourceDestroyerNumbers < 1 || (data.destroyerRefresh && data.sourceDestroyerNumbers < 2)) {
// else if (data.sourceDestroyerNumbers < 1) {
spawnSourceDestroyer();
}
//TODO Upgrade the detection for when the spot is safe for harvesting before doing this
else if (data.sourceHealerNumbers < 2 || (data.destroyerRefresh && data.sourceHealerNumbers < 4)) {
// else if (data.sourceHealerNumbers < 2) {
spawnSourceHealer(data.listSourceHealer, data.destroyerRefresh);
}
else if (data.healerNumbers < 2 || data.needHealer === true) {
spawnWeakHealer();
}
else if (data.guardNumbers < 4) {
spawnWeakGuard();
}
else if (data.sourceHarvesterNumbers < 2) {
spawnSourceHarvester();
}
else if (data.sourceCarrierNumbers < 8) {
spawnSourceCarrier();
}
else if (extNumbers > 0) {
var pieces = [Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.TOUGH, Game.ATTACK, Game.ATTACK, Game.MOVE, Game.MOVE, Game.MOVE];
for (var i = 0; i < extNumbers; i++)
pieces.push(Game.ATTACK);
spawnExtensionGuard(pieces);
}
else if (data.healerNumbers < 4) {
spawnBigHealer();
}
else if (data.healerNumbers >= 2 && data.guardNumbers < 10 && Game.spawns.Spawn1) {
spawnToughGuard();
}
else if (data.guardNumbers >= 10 && Game.spawns.Spawn1) {
//spawnMediumGuard();
spawnToughGuard();
}
}
}
}