-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathprototype.spawn.js
200 lines (183 loc) · 8.59 KB
/
prototype.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
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
var listOfRoles = ['harvester', 'lorry', 'claimer', 'upgrader', 'repairer', 'builder', 'wallRepairer'];
// create a new function for StructureSpawn
StructureSpawn.prototype.spawnCreepsIfNecessary =
function () {
/** @type {Room} */
let room = this.room;
// find all creeps in room
/** @type {Array.<Creep>} */
let creepsInRoom = room.find(FIND_MY_CREEPS);
// count the number of creeps alive for each role in this room
// _.sum will count the number of properties in Game.creeps filtered by the
// arrow function, which checks for the creep being a specific role
/** @type {Object.<string, number>} */
let numberOfCreeps = {};
for (let role of listOfRoles) {
numberOfCreeps[role] = _.sum(creepsInRoom, (c) => c.memory.role == role);
}
let maxEnergy = room.energyCapacityAvailable;
let name = undefined;
// if no harvesters are left AND either no miners or no lorries are left
// create a backup creep
if (numberOfCreeps['harvester'] == 0 && numberOfCreeps['lorry'] == 0) {
// if there are still miners or enough energy in Storage left
if (numberOfCreeps['miner'] > 0 ||
(room.storage != undefined && room.storage.store[RESOURCE_ENERGY] >= 150 + 550)) {
// create a lorry
name = this.createLorry(150);
}
// if there is no miner and not enough energy in Storage left
else {
// create a harvester because it can work on its own
name = this.createCustomCreep(room.energyAvailable, 'harvester');
}
}
// if no backup creep is required
else {
// check if all sources have miners
let sources = room.find(FIND_SOURCES);
// iterate over all sources
for (let source of sources) {
// if the source has no miner
if (!_.some(creepsInRoom, c => c.memory.role == 'miner' && c.memory.sourceId == source.id)) {
// check whether or not the source has a container
/** @type {Array.StructureContainer} */
let containers = source.pos.findInRange(FIND_STRUCTURES, 1, {
filter: s => s.structureType == STRUCTURE_CONTAINER
});
// if there is a container next to the source
if (containers.length > 0) {
// spawn a miner
name = this.createMiner(source.id);
break;
}
}
}
}
// if none of the above caused a spawn command check for other roles
if (name == undefined) {
for (let role of listOfRoles) {
// check for claim order
if (role == 'claimer' && this.memory.claimRoom != undefined) {
// try to spawn a claimer
name = this.createClaimer(this.memory.claimRoom);
// if that worked
if (name != undefined && _.isString(name)) {
// delete the claim order
delete this.memory.claimRoom;
}
}
// if no claim order was found, check other roles
else if (this.memory.hasOwnProperty(this.memory.minCreeps) && this.memory.hasOwnProperty(this.memory.minCreeps[role])
&& numberOfCreeps[role] < this.memory.minCreeps[role]) {
if (role == 'lorry') {
name = this.createLorry(150);
}
else {
name = this.createCustomCreep(maxEnergy, role);
}
break;
}
}
}
// if none of the above caused a spawn command check for LongDistanceHarvesters
/** @type {Object.<string, number>} */
let numberOfLongDistanceHarvesters = {};
if (name == undefined) {
// count the number of long distance harvesters globally
for (let roomName in this.memory.minLongDistanceHarvesters) {
numberOfLongDistanceHarvesters[roomName] = _.sum(Game.creeps, (c) =>
c.memory.role == 'longDistanceHarvester' && c.memory.target == roomName)
if (numberOfLongDistanceHarvesters[roomName] < this.memory.minLongDistanceHarvesters[roomName]) {
name = this.createLongDistanceHarvester(maxEnergy, 2, room.name, roomName, 0);
}
}
}
// print name to console if spawning was a success
if (name != undefined && _.isString(name)) {
console.log(this.name + " spawned new creep: " + name + " (" + Game.creeps[name].memory.role + ")");
for (let role of listOfRoles) {
console.log(role + ": " + numberOfCreeps[role]);
}
for (let roomName in numberOfLongDistanceHarvesters) {
console.log("LongDistanceHarvester" + roomName + ": " + numberOfLongDistanceHarvesters[roomName]);
}
}
};
// create a new function for StructureSpawn
StructureSpawn.prototype.createCustomCreep =
function (energy, roleName) {
// create a balanced body as big as possible with the given energy
var numberOfParts = Math.floor(energy / 200);
// make sure the creep is not too big (more than 50 parts)
numberOfParts = Math.min(numberOfParts, Math.floor(50 / 3));
var body = [];
for (let i = 0; i < numberOfParts; i++) {
body.push(WORK);
}
for (let i = 0; i < numberOfParts; i++) {
body.push(CARRY);
}
for (let i = 0; i < numberOfParts; i++) {
body.push(MOVE);
}
// create creep with the created body and the given role
return this.spawnCreep(body, roleName + '_' + Game.time, { memory: { role: roleName, working: false }});
};
// create a new function for StructureSpawn
StructureSpawn.prototype.createLongDistanceHarvester =
function (energy, numberOfWorkParts, home, target, sourceIndex) {
// create a body with the specified number of WORK parts and one MOVE part per non-MOVE part
var body = [];
for (let i = 0; i < numberOfWorkParts; i++) {
body.push(WORK);
}
// 150 = 100 (cost of WORK) + 50 (cost of MOVE)
energy -= 150 * numberOfWorkParts;
var numberOfParts = Math.floor(energy / 100);
// make sure the creep is not too big (more than 50 parts)
numberOfParts = Math.min(numberOfParts, Math.floor((50 - numberOfWorkParts * 2) / 2));
for (let i = 0; i < numberOfParts; i++) {
body.push(CARRY);
}
for (let i = 0; i < numberOfParts + numberOfWorkParts; i++) {
body.push(MOVE);
}
// create creep with the created body
return this.spawnCreep(body, roleName + '_' + Game.time, { memory: {
role: 'longDistanceHarvester',
home: home,
target: target,
sourceIndex: sourceIndex,
working: false
}});
};
// create a new function for StructureSpawn
StructureSpawn.prototype.createClaimer =
function (target) {
return this.spawnCreep([CLAIM, MOVE], 'claimer_' + Game.time, {memory: { role: 'claimer', target: target }});
};
// create a new function for StructureSpawn
StructureSpawn.prototype.createMiner =
function (sourceId) {
//return this.spawnCreep([CLAIM, MOVE], 'claimer_' + Game.time, {memory: { role: 'claimer', target: target }});
return this.spawnCreep([WORK, WORK, WORK, WORK, WORK, MOVE], 'miner_' + Game.time,
{memory: { role: 'miner', sourceId: sourceId }});
};
// create a new function for StructureSpawn
StructureSpawn.prototype.createLorry =
function (energy) {
// create a body with twice as many CARRY as MOVE parts
var numberOfParts = Math.floor(energy / 150);
// make sure the creep is not too big (more than 50 parts)
numberOfParts = Math.min(numberOfParts, Math.floor(50 / 3));
var body = [];
for (let i = 0; i < numberOfParts * 2; i++) {
body.push(CARRY);
}
for (let i = 0; i < numberOfParts; i++) {
body.push(MOVE);
}
// create creep with the created body and the role 'lorry'
return this.spawnCreep(body, 'lorry_' + Game.time, {memory: { role: 'lorry', working: false }});
};