-
Notifications
You must be signed in to change notification settings - Fork 0
/
pr.rooms.base.js
347 lines (293 loc) · 12.3 KB
/
pr.rooms.base.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
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
/*
* Module code goes here. Use 'module.exports' to export things:
* module.exports.thing = 'a thing';
*
* You can import it from another modules like this:
* var mod = require('pr.rooms.base');
* mod.thing == 'a thing'; // true
*/
var logger = require("screeps.logger");
logger = new logger("pr.rooms.base");
//logger.enabled = false;
let processClass = require("INeRT.process");
let threadClass = require("INeRT.thread");
let structureClass = require("pr.obj.structure");
let pileClass = require("pr.obj.pile");
let spawnClass = require("pr.obj.spawn");
let towerClass = require("pr.obj.tower");
let sourceClass = require("pr.obj.source");
let minerClass = require("pr.role.miner");
let builderClass = require("pr.role.builder");
let fillerClass = require("pr.role.filler");
let transporterClass = require("pr.role.transporter");
let upgraderClass = require("pr.role.upgrader");
let workerClass = require("pr.role.worker");
class baseRoom extends processClass {
init() {
this.taskManager = this.kernel.getProcess('taskManager');
this.intel = this.kernel.getProcess("intel");
this.flag = Game.flags[this.data.flagName];
this.flagX = this.flag.pos.x;
this.flagY = this.flag.pos.y;
}
initThreads() {
return [
this.createThread("run", "empire"),
this.createThread("taskCreate", "taskCreate"),
this.createThread("taskUpdate", "taskUpdate"),
];
}
run() {
logger.log(this.name, "baseRoom RUn", this.data.flagName, Game.flags[this.data.flagName]);
//kill ourselves if our flag is gone.
if (!Game.flags[this.data.flagName]) {
this.kernel.killProcess(this);
}
}
taskCreate() {
let room = Game.rooms[this.data.roomName];
if (room && room.controller && room.controller.my && !this.praiseTask) {
this.praiseTask = this.taskManager.createTask(this, global.Task.PRAISE, global.Task.TYPE_DOWORK, room.controller.pos, {"controllerId":room.controller.id});
this.feedUpgradeTask = this.taskManager.createTask(this, global.Task.FEEDUPGRADERS, global.Task.TYPE_DOWORK, room.controller.pos, {"controllerId":room.controller.id});
this.taskManager.setTask(this, this.praiseTask);
this.taskManager.setTask(this, this.feedUpgradeTask);
}
return threadClass.DONE;
}
taskUpdate() {
logger.log("-----------------------here???")
let roomIntel = this.intel.getRoomIntel(this.data.roomName);
let room = Game.rooms[this.data.roomName];
//---------------------- handle roads ----------------------
if(!roomIntel.structures.roads) {
logger.log("no roads in", this.data.roomName, "skipping")
return;
}
let roads = roomIntel.structures.roads;
//logger.log(this.data.roomName, "setting up roads:", JSON.stringify(roomIntel.structures.roads), JSON.stringify(roomIntel));
if (!this.roadRepairTasks) {
this.roadRepairTasks = {};
}
if (roads.length > 0) {
for(let r in roads) {
let roadId = roads[r].id;
let roadToRepair = Game.getObjectById(roadId);
if (!roadToRepair) {
//road decayed, skip it
//logger.log('decayed road!', roadId)
continue;
}
//logger.log('found road')
let repairThisRoadTask;
if (this.roadRepairTasks[roadToRepair.id]) {
repairThisRoadTask = this.roadRepairTasks[roadToRepair.id];
} else {
repairThisRoadTask = this.taskManager.createTask(this, global.Task.REPAIR, global.Task.TYPE_DOWORK, false, {"roadId":false});
this.roadRepairTasks[roadToRepair.id] = repairThisRoadTask;
}
repairThisRoadTask.pos = roadToRepair.pos;
//logger.log('----',JSON.stringify(repairThisRoadTask.data))
repairThisRoadTask.data.roadId = roadToRepair.id;
repairThisRoadTask.amount = (roadToRepair.hitsMax - roadToRepair.hits) / 100;
repairThisRoadTask.data.structureId = roadToRepair.id;
if (repairThisRoadTask.amount > 10 || repairThisRoadTask.amountAssigned > 0) {
this.taskManager.setTask(this, repairThisRoadTask);
}
}
}
}
setupContainers() {
let roomIntel = this.intel.getRoomIntel(this.data.roomName)
if(!roomIntel.structures)
return;
const conts = roomIntel.structures.containers;
for(let c in conts) {
const cont = conts[c];
let procName = `cont-${cont.id}`;
let data = {structureId:cont.id};
let proc = this.kernel.getProcess(procName);
if (!proc) {
proc = new structureClass(procName, data);
this.kernel.startProcess(proc, this);
}
}
}
setupPiles() {
let room = Game.rooms[this.data.roomName];
if (room) {
const piles = room.find(FIND_DROPPED_RESOURCES);
for(let p in piles) {
const pile = piles[p]
if (pile.amount > 100) {
let procName = `pile-${pile.id}`;
let data = {pileId:pile.id};
let proc = this.kernel.getProcess(procName);
if (!proc) {
proc = new pileClass(procName, data);
this.kernel.startProcess(proc, this);
}
}
}
}
}
setupSpawns() {
let roomIntel = this.intel.getRoomIntel(this.data.roomName);
let spawns = roomIntel.structures.spawns;
for(let s in spawns) {
let spawn = spawns[s];
let pos = spawn.pos;
//start source proc
let procName = `spawn-${spawn.id}`;
let data = {spawnId:spawn.id};
let proc = this.kernel.getProcess(procName);
if (!proc) {
proc = new spawnClass(procName, data);
this.kernel.startProcess(proc, this);
}
}
}
setupTowers() {
let roomIntel = this.intel.getRoomIntel(this.data.roomName);
let towers = roomIntel.structures.towers;
for(let t in towers) {
let tower = towers[t];
let pos = tower.pos;
//start source proc
let procName = `tower-${tower.id}`;
let data = {towerId:tower.id};
let proc = this.kernel.getProcess(procName);
if (!proc) {
proc = new towerClass(procName, data);
this.kernel.startProcess(proc, this);
}
}
}
setupSources() {
let intel = this.intel.getRoomIntel(this.data.roomName);
//logger.log("----",this.data.roomName, intel)
if (intel) {
this.miners = {};
let sources = intel.sources;
let index = 0;
for(let s in sources) {
let source = sources[s];
//start source proc
let procName = "source-";
if (source.id) {
procName += source.id;
} else {
procName += `${source.pos.x}-${source.pos.y}-${source.pos.roomName}`
}
let data = {sourceId:source.id, pos:source.pos};
let proc = this.kernel.getProcess(procName);
if (!proc) {
proc = new sourceClass(procName, data);
this.kernel.startProcess(proc, this);
}
}
}
}
setupMiners(creepClass = "miner") {
let intel = this.intel.getRoomIntel(this.data.roomName);
if (intel) {
this.miners = {};
let sources = intel.sources;
let index = 0;
for(let s in sources) {
let source = sources[s];
let pos = source.pos;
let rolePos = pos;
let procName = "miner-"+this.data.roomName + "-" + source.id;
let data = {roomName:this.data.roomName, pos:rolePos, sourceId:source.id, creepClass:creepClass};
let proc = this.kernel.getProcess(procName);
if (!proc) {
proc = new minerClass(procName, data);
this.kernel.startProcess(proc, this);
}
index++;
}
}
}
setupWorkers() {
let rolePos = new RoomPosition(this.flagX, this.flagY, this.data.roomName);
let procName = "workers-"+this.data.roomName;
let data = {roomName:this.data.roomName, pos:rolePos};
let proc = this.kernel.getProcess(procName);
if (!proc) {
proc = new workerClass(procName, data);
this.kernel.startProcess(proc, this);
}
proc.data = data;
}
setupBuilders() {
let rolePos = new RoomPosition(this.flagX, this.flagY+1, this.data.roomName);
//figure out how many we need;
let totalWork = this.taskManager.getTaskAmountByNameAndRange([global.Task.REPAIR, global.Task.BUILD], rolePos, 50);
let partsNeeded = totalWork / 50;
partsNeeded = Math.floor(Math.min(150, partsNeeded));
logger.log("builders", "total work:", totalWork, "parts needed", partsNeeded)
let requiredParts = {
WORK:(1 + partsNeeded)
};
let procName = "builders-"+this.data.roomName;
let data = {roomName:this.data.roomName, pos:rolePos, requiredParts:requiredParts};
let proc = this.kernel.getProcess(procName);
if (!proc) {
proc = new builderClass(procName, data);
this.kernel.startProcess(proc, this);
}
proc.data = data;
}
setupUpgraders() {
let room = Game.rooms[this.data.roomName];
let rolePos = new RoomPosition(this.flagX, this.flagY+2, this.data.roomName);
if (room) {
rolePos = room.controller.pos;
}
let procName = "upgraders-"+this.data.roomName;
let data = {roomName:this.data.roomName, pos:rolePos};
let proc = this.kernel.getProcess(procName);
if (!proc) {
proc = new upgraderClass(procName, data);
this.kernel.startProcess(proc, this);
}
proc.data = data;
}
setupTransporters() {
let rolePos = new RoomPosition(this.flagX, this.flagY+3, this.data.roomName);
//figure out how many we need;
let totalWork = this.taskManager.getTaskAmountByNameAndRange([global.Task.PICKUP, global.Task.PICKUPENERGYCONT], rolePos, 150);
let partsNeeded = totalWork / 50;
partsNeeded = Math.floor(Math.min(150, partsNeeded));
logger.log("transporters", "total work:", totalWork, "parts needed", partsNeeded)
let requiredParts = {
CARRY:(20 + partsNeeded)
};
let procName = "transporters-"+this.data.roomName;
let data = {roomName:this.data.roomName, pos:rolePos, requiredParts:requiredParts};
let proc = this.kernel.getProcess(procName);
if (!proc) {
proc = new transporterClass(procName, data);
this.kernel.startProcess(proc, this);
}
proc.data = data;
}
setupFillers() {
let rolePos = new RoomPosition(this.flagX, this.flagY+4, this.data.roomName);
let procName = "fillers-"+this.data.roomName;
let room = Game.rooms[this.data.roomName];
let partsNeeded = room.energyCapacityAvailable / 100
let requiredParts = {
CARRY:(1 + partsNeeded)
};
let data = {roomName:this.data.roomName, pos:rolePos, requiredParts:requiredParts};
let proc = this.kernel.getProcess(procName);
if (!proc) {
proc = new fillerClass(procName, data);
this.kernel.startProcess(proc, this);
}
proc.data = data;
}
}
profiler.registerClass(baseRoom, "rooms.base")
module.exports = baseRoom;