-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreepPrimitives.js
More file actions
375 lines (304 loc) · 9.08 KB
/
creepPrimitives.js
File metadata and controls
375 lines (304 loc) · 9.08 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//var _ = require('lodash');
module.exports = {
findBestSource (creep, pullsFromStorage) {
var sources,
currentHighestSource,
bestSourceID;
//console.log(creep.name);
pullsFromStorage = pullsFromStorage || false;
if (pullsFromStorage) {
//console.log(creep.room.propertyIsEnumerable("storage"));
//console.log(creep.room.storage.store[RESOURCE_ENERGY] > 0);
//console.log(bestSourceID !== creep.room.storage.id);
if (creep.room.propertyIsEnumerable("storage") && creep.room.storage.store[RESOURCE_ENERGY] > 0 && bestSourceID !== creep.room.storage.id) {
return creep.room.storage.id;
}
}
sources = creep.room.find(FIND_SOURCES, { filter: (source) => { return source.energy > 0; } });
if (sources.length === 0) {
creep.say('no sources');
return creep.realMemory.destination;
} else {
currentHighestSource = sources[0];
}
for (i = 1; i <= sources.length - 1; i++) {
if (sources[i].energy > sources[i - 1].energy) {
currentHighestSource = sources[i];
}
}
bestSourceID = currentHighestSource.id;
//console.log(JSON.stringify(bestSourceID));
return bestSourceID;
},
findBestDropoff: function (creep) {
var dropoffs,
storage,
bestDropoffID;
//creep.say('HELP!');
dropoffs = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (
(structure.structureType === STRUCTURE_EXTENSION || structure.structureType === STRUCTURE_SPAWN) && structure.energy < structure.energyCapacity);
}
});
//dropoffs.sort(this.compareObjectProperties("energyCapacity", "ASC"));
storage = creep.room.find(FIND_STRUCTURES, { filter: (structure) => { return structure.structureType === STRUCTURE_STORAGE; } });
//console.log(JSON.stringify(storage));
//console.log(JSON.stringify(dropoffs));
if (dropoffs.length >= 2) {
//console.log(JSON.stringify(dropoffs));
//console.log(JSON.stringify(creep.pos));
//console.log(JSON.stringify(creep.pos.findClosestByPath(dropoffs)));
return creep.pos.findClosestByPath(dropoffs).id;
} else if (dropoffs.length === 0) {
dropoffs.push(storage[0]);
} //if (dropoffs.length > 1) {
return dropoffs[0].id;
},
findBestProject: function (creep) {
var projects,
currentMostCompleted,
bestProjectsID;
projects = _.sortByOrder(creep.room.find(FIND_CONSTRUCTION_SITES),
[
'progressTotal',
function (o) {
return o.progress / o.progressTotal;
}
],
['asc', 'desc']);
//console.log(JSON.stringify(projects));
if (projects.length) {
if (projects.length > 1) {
bestProjectsID = projects[0].id; //creep.pos.findClosestByPath(projects).id;
} else if (projects.length === 1) {
bestProjectsID = projects[0].id;
} else {
//test
}
}
return bestProjectsID;
},
moveCreep: function (creep, range) {
try {
range = range || 1;
var moveToHere,
needsToMove;
moveToHere = Game.getObjectById(creep.realMemory.destination);
needsToMove = !creep.pos.inRangeTo(moveToHere, range);
//console.log(range);
//creep.say(needsToMove);
if (needsToMove) {
creep.moveTo(moveToHere);
}
}
catch (e) {
creep.say('Trouble moving');
}
return needsToMove;
},
compareObjectProperties: function (property, order) {
order.toUpperCase();
switch ('ASC') {
case 'ASC':
//return a.hits - b.hits;
return function (a, b) {
//console.log(property);
//console.log(a[property] - b[property]);
return a[property] - b[property];
};
//break;
case 'DESC':
return b[property] - a[property];
//break;
default:
break;
}
},
spawnCreep: function (roomName, spawn, build, propertiesObj, neg6ErrMsg) {
var result = spawn.canCreateCreep(build, null);
if (result === 0 && spawn.spawning === null) {
result = spawn.createCreep(build, null, propertiesObj);
console.log(result + ' is one of us.');
//console.log(Game.creeps[result].id);
//Game.creeps[result].setProperties(propertiesObj);
} else if (result === -6) {
console.log('We are waiting for energy to build a better creep in room ' + roomName + '.');
} else if (result !== -4) {
console.log('There was an error spawning creep in ' + roomName + ': ' + result);
}
return result;
},
findCommonGround: function (firstRoomObject, secondRoomObject) {
try {
var commonRoomObjects = [];
if (true) {
throw new Error('test');
}
}
catch (err) {
console.log("There was a problem finding common ground: " + err.message);
}
return commonRoomObjects;
},
findConstructionByRoom: function () {
var allConstruction,
sortedConstruction,
constructionByRoom;
constructionByRoom = {};
try {
allConstruction = _.values(Game.constructionSites);
//console.log(JSON.stringify(allConstruction));
sortedConstruction = _.sortByOrder(
allConstruction,
[
function (o) {
return o.pos.roomName;
},
'progressTotal',
function (o) {
return o.progress / o.progressTotal;
}
],
['asc', 'asc', 'desc']);
for (i = 0; i < sortedConstruction.length; i++) {
if (!constructionByRoom.propertyIsEnumerable(sortedConstruction[i].pos.roomName)) {
constructionByRoom[sortedConstruction[i].pos.roomName] = [];
constructionByRoom[sortedConstruction[i].pos.roomName].push(sortedConstruction[i].id);
} else {
constructionByRoom[sortedConstruction[i].pos.roomName].push(sortedConstruction[i].id);
}
}
return constructionByRoom;
}
catch (err) {
console.log("There was a problem creating a list of construction sites by room");
}
finally {
//placeholder
}
},
changeRole: function (creep, properties) {
creep.setProperties(properties);
},
prototypeThis: function () {
Room.prototype.construction = function () {
return this.find(FIND_MY_CONSTRUCTION_SITES);
};
Room.prototype.census = function (print) {
print = print || false;
var census = {},
creepsInRoom = this.find(FIND_MY_CREEPS),
roomName = this.name;
_.forEach(creepsInRoom, function (n, key) {
var currentCreep = creepsInRoom[key],
currentRole = currentCreep.realMemory.role;
if (!census.propertyIsEnumerable(currentRole)) {
census[currentRole] = [];
census[currentRole].push(currentCreep.id);
} else {
census[currentRole].push(currentCreep.id);
}
});
if (print) {
_.forEach(census, function (n, key) {
console.log(roomName + ' ' + key + ' count: ' + n.length);
});
}
//console.log(JSON.stringify(census));
return census;
};
Object.defineProperty(RoomObject.prototype, 'realMemory', {
get: function () {
try {
if (_.isUndefined(Memory[this.id])) {
Memory[this.id] = {};
}
if (!_.isObject(Memory[this.id])) {
return undefined;
}
return Memory[this.id] = Memory[this.id] || {};
}
catch (err) {
console.log(err.message);
}
},
set: function (value) {
try {
if (_.isUndefined(Memory[this.id])) {
Memory[this.id] = {};
}
if (!_.isObject(Memory[this.id])) {
throw new Error('Could not set RoomObject memory');
}
Memory[this.id] = value;
}
catch (err) {
console.log(err.message);
}
}
});
RoomObject.prototype.setProperties = function (propertyObject) {
var objectMemory = Memory[this.id];
console.log(JSON.stringify(objectMemory));
for (var key in propertyObject) {
// skip loop if the property is from prototype
if (!propertyObject.hasOwnProperty(key)) continue;
if (_.isUndefined(Memory[this.id][key])) {
objectMemory[key] = {};
}
objectMemory[key] = propertyObject[key];
}
};
RoomObject.prototype.resetProperties = function (propertyObject) {
var objectMemory = Memory[this.id];
//console.log(JSON.stringify(objectMemory));
for (var key in propertyObject) {
// skip loop if the property is from prototype
if (!propertyObject.hasOwnProperty(key)) continue;
if (_.isUndefined(Memory[this.id][key])) {
objectMemory[key] = {};
}
objectMemory[key] = propertyObject[key];
}
};
massSuicide = function (room) {
var creepsToDie = Game.rooms[room].find(FIND_MY_CREEPS);
for (var int in creepsToDie) {
creepName = creepsToDie[int].name;
Game.creeps[creepName].suicide();
console.log(creepName + ' drank the Kool-Aid. Onward to the ascension');
}
};
},
performTask: function (){
},
CreepDestination: function (destinationID) {
this.destinationID = destinationID;
this.pos = Game.getObjectById(destinationID).pos;
},
//CreepHarvest.prototype = new module.exports.CreepDestination() {
// constructor(destinationID) {
// //this.destinationID = destinationID;
// //this.pos = Game.getObjectById(destiantionID).pos;
// this.action = 'harvest';
// }
//},
//creepDestination: function (destinationID) {
// return {
// destinationID: destinationID,
// pos: Game.getObjectById(destinationID).pos
// }
//},
//CreepHarvest: class {
// constructor(destinationID) {
// try {
// this.destination = module.exports.creepDestination(destinationID);
// this.action = 'harvest';
// }
// catch (err) {
// console.log('Problem creating task: ' + err.message)
// }
// }
//}
};