-
Notifications
You must be signed in to change notification settings - Fork 0
/
pr.obj.spawn.js
114 lines (93 loc) · 4.07 KB
/
pr.obj.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
/*
* 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.obj.spawn');
* mod.thing == 'a thing'; // true
*/
var logger = require("screeps.logger");
logger = new logger("pr.obj.spawn");
let processClass = require("INeRT.process");
let threadClass = require("INeRT.thread");
class spawnProc extends processClass {
init() {
this.taskManager = this.kernel.getProcess('taskManager');
this.creepManager = this.kernel.getProcess('creepManager');
this.intel = this.kernel.getProcess('intel');
this.req = false;
this.spawnQueue = [];
let spawn = Game.getObjectById(this.data.spawnId);
this.spawnTask = this.taskManager.createTask(this, global.Task.FILLSPAWNS, global.Task.TYPE_DOWORK, spawn.pos, {"spawnId":spawn.id});
this.spawnContTask = this.taskManager.createTask(this, global.Task.FEEDSPAWNS, global.Task.TYPE_DOWORK, false, {"targetId":false});
//this.feedSpawnTask = this.taskManager.createTask(this, global.Task.FEEDSPAWN, global.Task.TYPE_DOWORK, false, {"targetId":false});
this.taskManager.setTask(this, this.spawnTask);
}
initThreads() {
return [
this.createThread("taskUpdate", "taskUpdate"),
this.createThread("doSpawning", "work")
];
}
taskUpdate() {
this.spawnQueue = [];
let spawn = Game.getObjectById(this.data.spawnId);
if (!spawn) {
return threadClass.DONE;
}
this.spawnTask.amount = spawn.energyCapacity - spawn.energy;
let cont = spawn.getContainer(1);
if (cont) {
this.spawnContTask.amount = cont.storeCapacity - _.sum(cont.store);
this.spawnContTask.pos = cont.pos;
this.spawnContTask.data.targetId = cont.id
//logger.log(cont, cont.storeCapacity, _.sum(cont.store), this.spawnContTask.amount);
this.taskManager.setTask(this, this.spawnContTask);
}
}
doSpawning() {
let spawn = Game.getObjectById(this.data.spawnId);
if (!spawn) {
return threadClass.DONE;
}
//logger.log("tryna spawn")
if (!spawn.spawning) {
this.req = this.spawnFromQueue(spawn);
} else {
global.utils.drawText(`spawning ${this.req.role} for ${this.req.proc}`, spawn.pos)
}
}
spawnFromQueue(spawn) {
let req = this.creepManager.getCreepToSpawn(spawn);
logger.log("tryina spawn", JSON.stringify(req));
if (req) {
let classObj = global.creepClasses[req.creepClass];
if (classObj) {
let roomIntel = this.intel.getRoomIntel(spawn.room.name);
let energyToUse = (Object.keys(Game.creeps).length < 10) ? spawn.room.energyAvailable : spawn.room.energyCapacityAvailable;
//let energyToUse = spawn.room.energyAvailable
let body = classObj.getBody(energyToUse);
let name = `${req.proc}-${req.role}-${req.creepClass}-${req.index}`;
let memory = this.creepManager.getMemoryFromReq(req);
logger.log(spawn, "spawning", name, "for", req.proc, JSON.stringify(body));
let ret = spawn.spawnCreep(body, name, {memory:memory})
let index = req.index;
while(ret == -3) {
index++;
name = `${req.proc}-${req.role}-${req.creepClass}-${index}`;
ret = spawn.spawnCreep(body, name, {memory:memory})
}
if (ret == 0) { //it worked, mark req filled
this.creepManager.markReqFilled(req);
}
logger.log('spawn result', ret);
} else {
//class not defined!
logger.log("CREEP CLASS NOT DEFINED!!", req.creepClass);
}
return req;
}
return false;
}
}
module.exports = spawnProc;