-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.labs.js
173 lines (152 loc) · 5 KB
/
manager.labs.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
/*
* 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('manager.labs');
* mod.thing == 'a thing'; // true
*/
var logger = require("screeps.logger");
logger = new logger("manager.labs");
logger.enabled = false;
var obj = function() {
}
obj.prototype.init = function(roomManager) {
this.roomManager = roomManager;
this.storageDesiredLevels = this.makeStoreObject(10000, 1000, 1000);
this.terminalDesiredLevels = this.makeStoreObject(30000, 200000, 1000);
this.linkDesiredLevels = this.makeStoreObject(800, 0, 0);
var labs = _.map(this.roomManager.room.find(FIND_MY_STRUCTURES, {filter: (s) => s.structureType == STRUCTURE_LAB}), (l) => l.id);
this.labDesiredLevels = {};
this.labIds = [];
for (var l in labs) {
var lab = labs[l];
this.labDesiredLevels[lab] = this.makeStoreObject(2000, 0, 0);
this.labIds.push(lab);
}
this.labs = false;
}
obj.prototype.tickInit = function() {
if (!this.roomManager.room.terminal) {
return;
}
this.storage = this.roomManager.room.storage;
this.terminal = this.roomManager.room.terminal;
this.link = this.terminal.getLink();
this.labs = {};
for(var i in this.labIds) {
var id = this.labIds[i];
this.labs[id] = Game.getObjectById(id);
}
this.mapImbalances();
}
obj.prototype.tick = function() {
}
obj.prototype.tickEnd = function() {
}
obj.prototype.mapImbalances = function() {
this.balancesNeeded = [];
if (!this.storage || !this.terminal) {
return;
}
logger.log(this.roomManager.roomName, "mapping imbalance", this.link, this.terminal, JSON.stringify(this.terminal.store))
for(var r in RESOURCES_ALL) {
var type = RESOURCES_ALL[r];
var locations = [];
//add storage
locations.push(this.mapImbalance(this.storage.id, type, this.storage.store, this.storageDesiredLevels));
//add terminal
locations.push(this.mapImbalance(this.terminal.id, type, this.terminal.store, this.terminalDesiredLevels));
//add link
if (this.link) {
var linkStore = {
energy:this.link.energy
}
locations.push(this.mapImbalance(this.link.id, type, linkStore, this.linkDesiredLevels));
}
//add labs
for(var l in this.labs) {
var lab = this.labs[l];
var labStore = {
energy:lab.energy
}
labStore[lab.mineralType] = lab.mineralAmount;
//logger.log(JSON.stringify(labStore))
locations.push(this.mapImbalance(lab.id, type, labStore, this.labDesiredLevels[lab.id]));
}
for(var l in locations) {
var location = locations[l];
if (location.diff > 0) {
var locationToMoveTo = this.findPlaceForExtras(locations, location.type, location.id);
if (locationToMoveTo) {
logger.log(this.roomManager.roomName, "has extra", JSON.stringify(location), JSON.stringify(locationToMoveTo))
this.balancesNeeded.push({
from:location,
to:locationToMoveTo
});
}
}
}
}
this.balancesNeeded = _.sortBy(this.balancesNeeded, function(op) {
if (op.from.type == RESOURCE_ENERGY) {
return op.from.diff;
} else {
return op.from.diff;
}
})
}
obj.prototype.findPlaceForExtras = function(locations, type, id) {
for(var l in locations) {
var location = locations[l];
if (location.type == type && location.id != id && location.diff < 0 ) {
//logger.log(JSON.stringify(location));
return location;
}
}
if (id == this.storage.id) {
return false;
}
//no location was found, use storage
return {
id:this.storage.id,
type:type,
amt:0,
target:2000,
diff:-2000
}
}
obj.prototype.mapImbalance = function(id, type, store, targetStore) {
var amt = store[type];
var target = targetStore[type];
if (target === false) {
target = 0;
}
if (amt == undefined) {
amt = 0;
}
if (type == RESOURCE_ENERGY)
logger.log(id, type, amt, target, amt-target, JSON.stringify(store))
return {
id:id,
type:type,
amt:amt,
target:target,
diff:amt-target
}
}
obj.prototype.makeStoreObject = function(targetEnergy, targetBaseMinerals, targetAllMinerals) {
var o = {};
for(var r in RESOURCES_ALL) {
var type = RESOURCES_ALL[r];
if (type == RESOURCE_ENERGY) {
o[type] = targetEnergy;
} else if(type.length == 1) {
o[type] = targetBaseMinerals;
} else {
o[type] = targetAllMinerals;
}
}
return o;
}
module.exports = obj;