-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrole.miner.js
223 lines (211 loc) · 8.14 KB
/
role.miner.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
/* Specialist Miner Drone */
module.exports.role = 'miner';
/* SType */
module.exports.sType = 'specialist';
/* Spawn Roster */
module.exports.roster = {
1: 2,
2: 2,
3: 2,
4: 2,
5: 2,
6: 2,
7: 2,
8: 2,
};
/* Costs */
module.exports.cost = {
1 : 350,
2 : 550,
3 : 550,
4 : 550,
5 : 550,
6 : 550,
7 : 550,
8 : 550,
};
/* Body Parts at each RCL */
module.exports.body = {
1 : [ WORK,WORK,WORK,MOVE ],
2 : [ WORK,WORK,WORK,WORK,WORK,MOVE ],
3 : [ WORK,WORK,WORK,WORK,WORK,MOVE ],
4 : [ WORK,WORK,WORK,WORK,WORK,MOVE ],
5 : [ WORK,WORK,WORK,WORK,WORK,MOVE ],
6 : [ WORK,WORK,WORK,WORK,WORK,MOVE ],
7 : [ WORK,WORK,WORK,WORK,WORK,MOVE ],
8 : [ WORK,WORK,WORK,WORK,WORK,MOVE ],
};
/**
* Individual check for a room to check if this creep type should be enabled or not
*/
module.exports.enabled = function (room, debug = false) {
var _room = Game.rooms[room];
if (_room.controller && _room.controller.level > 1 && _room.memory.minersNeeded && _room.memory.minersNeeded > 0) {
// Now count the creeps
var list = _.filter(Game.creeps, (creep) => creep.memory.role === this.role && creep.memory.roomName === room && !creep.memory.dying);
if (list.length < _room.memory.minersNeeded) {
return true;
}
}
return false;
}
// Set a time for this creep to 'expire' at
module.exports.expiry = 75;
/* Run method */
module.exports.run = function (creep, debug = false) {
if (creep.isTired()) { return; }
// Okay, health check
if (!creep.memory.dying && creep.ticksToLive <= 150) {
if (debug) { console.log('Creep[' + creep.name + '] Miner Dying Making sure we spawn a new one'); }
// set dying to true and set the sourceId to null in room memory
creep.memory.dying = true;
var sourceId = creep.memory.assignedSource;
creep.room.memory.assignedSources[sourceId] = null;
}
// Alright if it's dying, output the timer
if (creep.memory.dying) {
if (debug) { console.log('Creep[' + creep.name + '] Miner Dying, ticking down'); }
creep.say(creep.ticksToLive);
// If it's less than 10 creep.ticksToLive, drop what we have
if (creep.ticksToLive < 10) {
if (debug) { console.log('Creep[' + creep.name + '] Miner about to die'); }
creep.say('!!' + creep.ticksToLive + '!!');
}
}
// Only do this if we don't have an assigned Source
if (!creep.memory.assignedSource) {
this.setup();
if (debug) { console.log('Creep[' + creep.name + '] Miner without assigned Source, assigning'); }
// Okay lets get the room memory for assigned sources
var sourceId = false;
var sources = creep.room.find(FIND_SOURCES);
var assigned = creep.room.memory.assignedSources;
// Can't loop through sources to just to an i = loop to get them
for (var i=0;i<=sources.length-1;i++) {
var source = sources[i];
if (assigned[source.id] === null) {
sourceId = source.id;
creep.room.memory.assignedSources[sourceId] = creep.id;
creep.memory.assignedSource = sourceId;
// Make sure we break out so we don't break the next source too
break;
}
}
// Do we have a sourceId?
if (sourceId === false) {
if (debug) { console.log('Creep[' + creep.name + '] Miner cannot find source!!'); }
// Game.notify(Game.time + ' Miner Creep unable to assign a source');
}
}
// Are we full?
if (creep.energy === creep.carryCapacity) {
if (debug) { console.log('Creep[' + creep.name + '] Miner full, dropping!'); }
creep.memory.dropping = true;
} else {
creep.memory.dropping = false;
}
// Are we dropping?
if (creep.memory.dropping) {
// This may need to change, depends if the drop costs fatigue or if dropping goes into a container
console.log(creep.drop(RESOURCE_ENERGY));
creep.memory.dropping = false;
creep.say('V');
// DANGER we just drop resources here... This could leave a pile of resources if our transfer dudes aren't keeping up
// // This is just here incase it's needed fast (saves me writing it in a panic)
// var container = creep.pos.findClosestByRange(FIND_STRUCTURES, {
// filter: (i) => {
// return (i.structureType == STRUCTURE_CONTAINER) && (_.sum(i.store) < i.storeCapacity)
// }
// });
// if (container) {
// try {
// for (var resource in creep.carry) {
// creep.transfer(container, resource);
// }
// } catch (ERR_NOT_IN_RANGE) {
// if (debug) { console.log('Creep[' + creep.name + '] Miner cannot transfer resources'); }
// }
// } else {
// creep.say('No Drop');
// if (debug) { console.log('Creep[' + creep.name + '] Miner unable to transfer resources is full!'); }
// }
}
if (!creep.memory.dropping) {
// Alright if we're not dropping, we're harvesting lets try harvesting our assigned source
var source = Game.getObjectById(creep.memory.assignedSource);
if (source) {
// Okay we have a source, lets trying harvesting it!
if (creep.harvest(source) === ERR_NOT_IN_RANGE) {
if (debug) { console.log('Creep[' + creep.name + '] Miner not in range, moving into range'); }
// We're not at the thing! Lets go there!
creep.travelTo(source);
// Moving make a say
creep.say('>>')
} else {
// Mining say we're mining
if (!creep.memory.dying) {
creep.say('d(^-^)b');
}
}
} else {
if (debug) { console.log('Creep[' + creep.name + '] Miner cannot find source!!'); }
creep.say('WTF?');
Game.notify(Game.time + ' Miner Creep unable to assign a source');
}
}
}
/**
* Run this script to setup rooms ready for assigned miners
*/
module.exports.setup = function (debug = false) {
// Loop through the game rooms we have
for (var name in Game.rooms) {
if (debug) { console.log('Setting up room ' + name); }
var theRoom = Game.rooms[name];
// Clear Assigned Sources
delete theRoom.memory.assignedSources;
// Get all the sources available
var sources = theRoom.find(FIND_SOURCES);
// Make sure we set the minersNeeded of the room
theRoom.memory.minersNeeded = sources.length;
// Make an array / object thing
var array = {};
// Loop through sources
for (var i=0; i<=sources.length-1; i++) {
if (debug) { console.log(sources[i].id); }
// Set it to null
array[sources[i].id] = null;
}
// Loop through the sources again
for (var i=0; i<=sources.length-1; i++) {
// Get the source so we can use it's id
var source = sources[i];
// Make found false by default
var found = false;
var creepId = null;
var sourceId = source.id;
// Loop through the miners
for (var creepName in Game.creeps) {
// Define the creep
var creep = Game.creeps[creepName];
if (!creep.memory.role === 'miner' || creep.memory.dying) {
continue;
}
// If this creep has the assigned Source, we found it
if (creep.memory.assignedSource === sourceId) {
found = true;
creepId = creep.id;
break;
}
}
// we found it
if (found) {
if (debug) { console.log(sourceId + ' set to ' + creepId); }
array[sourceId] = creepId;
}
}
// Set the room's assigned Sources to be the object
theRoom.memory.assignedSources = array;
}
return '++Miner Setup Complete++';
}