-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprototype.creep.js
262 lines (236 loc) · 7.31 KB
/
prototype.creep.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
/**
* Define existing Roles here.
*/
var roles = {
H: require('role.harvester'),
M: require('role.miner'),
U: require('role.upgrader'),
B: require('role.builder'),
LDB: require('role.builder'),
R: require('role.repairer'),
WR: require('role.wallRepairer'),
C: require('role.carrier'),
LDH: require('role.longDistanceHarvester'),
CL: require('role.claimer')
};
/**
* Execute role for each creep.
*/
Creep.prototype.runRole = function () {
if (this.memory.energyCarried != null) {
Game.rooms[this.memory.home.name].memory.energyIncome = Game.rooms[this.memory.home.name].memory.energyIncome + this.carry[RESOURCE_ENERGY] - this.memory.energyCarried;
this.memory.energyCarried = null;
}
roles[this.memory.role].run(this);
//this.say(this.carry[RESOURCE_ENERGY]);
};
/**
* Attacks enemy if in room.
*/
Creep.prototype.attack = function (enemies) {
console.log('Attacker found @ ' + this.room.name + ': ' + enemies);
this.moveTo(enemies, { maxRooms: 1 });
this.attack(enemies);
}
/**
* Put energy in container/storage/base.
* Creep will use Base if nothing is else is available.
* @param useContainer Can use Container.
* @param useStorage Can use Storage.
* @param useBase Can use Base.
*/
Creep.prototype.putEnergy = function (useContainer, useStorage, useBase) {
let transferReturnMessage = null
if (useBase) {
transferReturnMessage = transferEnergyToBase(this);
}
if (useContainer && transferReturnMessage == null) {
transferReturnMessage = transferEnergyToContainer(this);
}
if (useStorage && transferReturnMessage == null) {
transferReturnMessage = transferEnergyToStorage(this);
}
if (transferReturnMessage == null) {
transferReturnMessage = transferEnergyToBase(this);
}
}
/**
* Collect energy from loot/container/storage/source.
* @param useContainer Can use Container.
* @param useStorage Can use Storage.
* @param useSource Can use Source.
*/
Creep.prototype.getEnergy = function (useLoot, useContainer, useStorage, useSource) {
let structures = null;
// If no Storage exists, use Container instead.
structures = this.room.find(FIND_MY_STRUCTURES, { filter: s => (s.structureType == STRUCTURE_STORAGE) });
if (structures == "") {
useContainer = true;
}
let withdrawReturnMessage = null;
if (useLoot) {
findAndPickUpLoot(this);
}
if (useContainer && withdrawReturnMessage == null) {
withdrawReturnMessage = withdrawFromContainer(this);
}
if (useStorage && withdrawReturnMessage == null) {
withdrawReturnMessage = withdrawFromStorage(this);
}
// If the creep should look for sources.
if (useSource && withdrawReturnMessage == null) {
// find closest source
var source = this.pos.findClosestByPath(FIND_SOURCES_ACTIVE);
// try to harvest energy, if the source is not in range
if (this.harvest(source) == ERR_NOT_IN_RANGE) {
// move towards it
this.moveTo(source, { maxRooms: 1 });
}
}
};
function withdrawFromStorage(creep) {
let structure = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: s => (s.structureType == STRUCTURE_STORAGE)
&& s.store[RESOURCE_ENERGY] > creep.carryCapacity / 10
});
let withdrawReturnMessage = withdrawEnergyFromStructure(creep, structure);
return withdrawReturnMessage;
}
function withdrawFromContainer(creep) {
let structure = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: s => (s.structureType == STRUCTURE_CONTAINER)
&& s.store[RESOURCE_ENERGY] > creep.carryCapacity / 3
});
let withdrawReturnMessage = withdrawEnergyFromStructure(creep, structure);
return withdrawReturnMessage;
}
/**
* Withdraws Energy from given Structure.
* @param creep
* @param structure
* @returns
*/
function withdrawEnergyFromStructure(creep, structure) {
if (structure != null) {
let withdrawReturnMessage = creep.withdraw(structure, RESOURCE_ENERGY);
if (withdrawReturnMessage == OK) {
if (creep.memory.role == 'C') {
creep.memory.energyCarried = creep.carry[RESOURCE_ENERGY];
}
} else if (withdrawReturnMessage == ERR_NOT_IN_RANGE) {
// If the container is not in range, move towards it.
creep.moveTo(structure, { maxRooms: 1 });
} else {
creep.say("Error: " + withdrawReturnMessage);
}
return withdrawReturnMessage;
} else {
creep.say("?");
return null;
}
}
/**
*
* @param creep
*/
function findAndPickUpLoot(creep) {
let loot = creep.pos.findClosestByRange(FIND_DROPPED_RESOURCES, { filter: s => (s.energy > 100) });
if (creep.pickup(loot) != OK) {
creep.moveTo(loot, { maxRooms: 1 });
}
else {
creep.say("Loot! o.o");
}
}
/**
* Tries to transfer energy to storage.
* @param creep
* @param structure
* @returns transfer message.
*/
function transferEnergyToStorage(creep) {
let structure = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: (s) => (s.structureType == STRUCTURE_STORAGE
// && s.energy < s.energyCapacity
)
});
transferReturnMessage = transferEnergyToStructure(creep, structure, true);
return transferReturnMessage;
}
/**
* Tries to transfer energy to container.
* @param creep
* @param structure
* @returns transfer message.
*/
function transferEnergyToContainer(creep) {
let structure = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: (s) => (s.structureType == STRUCTURE_CONTAINER
&& _.sum(s.store) < s.storeCapacity)
});
let transferReturnMessage = transferEnergyToStructure(creep, structure, true);
return transferReturnMessage;
}
/**
* Tries to transfer energy to base.
* @param creep
* @param structure
* @returns transfer message.
*/
function transferEnergyToBase(creep) {
let structure;
if (creep.memory.role != 'H') {
structure = creep.pos.findClosestByPath(FIND_MY_STRUCTURES, {
// the second argument for findClosestByPath is an object which takes
// a property called filter which can be a function
// we use the arrow operator to define it
filter: (s) => ((s.structureType == STRUCTURE_SPAWN
|| s.structureType == STRUCTURE_TOWER
|| s.structureType == STRUCTURE_EXTENSION)
&& s.energy < s.energyCapacity)
});
}
else {
structure = creep.pos.findClosestByPath(FIND_MY_STRUCTURES, {
// the second argument for findClosestByPath is an object which takes
// a property called filter which can be a function
// we use the arrow operator to define it
filter: (s) => ((s.structureType == STRUCTURE_SPAWN
|| s.structureType == STRUCTURE_EXTENSION)
&& s.energy < s.energyCapacity)
});
}
let transferReturnMessage = transferEnergyToStructure(creep, structure, false);
return transferReturnMessage;
}
/**
* Tries to transfer energy to structure.
* @param creep
* @param structure
* @param useCounter Defines if energy counter should be usesd.
* @returns transfer message.
*/
function transferEnergyToStructure(creep, structure, useCounter) {
if (structure != null) {
// Try to transfer energy.
creepEnergyAmount = creep.energy;
let transferReturnMessage = creep.transfer(structure, RESOURCE_ENERGY);
if (transferReturnMessage == OK) {
return transferReturnMessage;
if (useCounter != true) {
//Memory.rooms[this.memory.home].EnergyIncome = Memory.rooms[this.memory.home].EnergyIncome + creepEnergyAmount;
}
}
else if (transferReturnMessage == ERR_NOT_IN_RANGE) {
// If it is not in range move towards it.
creep.moveTo(structure, { maxRooms: 1 });
}
else {
creep.say("Error :" + transferReturnMessage);
}
return transferReturnMessage;
} else {
creep.say("!");
return null;
}
}