-
Notifications
You must be signed in to change notification settings - Fork 0
/
labCtrl.js
282 lines (256 loc) · 9.37 KB
/
labCtrl.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
/**********************************************
author:ChenyangDu
version:1.0
lab半自动
使用方法:
1、需要占用Memory.lab,不要和其他代码冲突
2、需要手动提供原矿或者其他中间产物
3、storage不要满,要不没地方放产物
代码部分如下
var labCtrl = require('labCtrl')
module.exports.loop = function () {
labCtrl.run('W43N27',RESOURCE_GHODIUM,2000)
//your code
}
***************************************************/
const STATE_FILL = 0
const STATE_REACTION = 1
const STATE_RECOVERY = 2
var room,needs,labs,creep
module.exports = {
run:function(roomName,need_type,need_amount){
room = Game.rooms[roomName];
const creepName = 'laber' + roomName;
creep = Game.creeps[creepName];
if(!initMemory(roomName))return;//这句是初始化用的,如果目标房间已经运行稳定了,就可以把这句删掉
if(!need_type || !need_amount){
console.log('ERROR: What do you want Room '+roomName+' do?')
return;
}
var state = Memory.lab[roomName].state;
labs = new Array();
var _id = 0;
Memory.lab[roomName]['labs'].forEach(labid => {
labs.push(Game.getObjectById(labid))
new RoomVisual(roomName).text(_id,labs[_id].pos,{color: 'white', font: 0.5})//如不需要绘制编号,这句也可以删了
_id++;
});
needs = new Array();
//needs.push([need_type,need_amount]);
pushMission([need_type,need_amount],roomName)
//console.log(needs)
if(needs.length >= 1){
var product = needs[needs.length-1][0],amount = Math.min(3000, needs[needs.length-1][1]);
var materials = findMaterial(product)
}
if(amount % 5)amount += 5-amount%5;
//console.log(materials)
if(materials == null && needs.length >= 1){
console.log('Room '+roomName+' need '+ amount + product)
}
if(materials == null){
if(creep)
creepKill(creep)
return
}
//change state
if(state == STATE_REACTION && (labs[0].mineralType == undefined || labs[1].mineralType == undefined)){
console.log('state change to STATE_RECOVERY')
state = STATE_RECOVERY
}
if(state == STATE_RECOVERY){
var allclear = true;
labs.forEach(lab => {
if(lab.mineralType != undefined)allclear = false;
});
if(allclear){
console.log('state change to STATE_FILL')
state = STATE_FILL
}
}
if(state == STATE_FILL){
if(labs[0].store[materials[0]] >= amount && labs[1].store[materials[1]] >= amount){
console.log('state change to STATE_REACTION')
state = STATE_REACTION
}
}
Memory.lab[roomName].state = state;
//console.log('state is ',state)
// run state
if(creep)creep.say('laber')
if(state == STATE_REACTION && Game.time % REACTION_TIME[product] == 0){
if(creep){
creepKill(creep)
}
for(var i = 2;i<labs.length;i++){
if(labs[0] && labs[1] && labs[i]){
if(labs[i].runReaction(labs[0],labs[1]) != OK){
//console.log('wa')
state = STATE_RECOVERY;
}
}
}
}
else if(state == STATE_FILL){
if(!creep){
autoSpawnCreep(creepName)
}else{
var withdrawTarget;
var type = materials[0]
if(labs[0].store[type] ==undefined || labs[0].store[type] < amount){
if(room.storage.store[type])withdrawTarget = room.storage;
else if(room.terminal.store[type])withdrawTarget = room.terminal;
if(labs[0].store[type])amount -= labs[0].store[type];
if(creep.store[type] >= amount)withdrawTarget = null;
//console.log(withdrawTarget,type,amount)
WAT(creep,withdrawTarget,labs[0],type,amount)
}else{
type = materials[1]
if(labs[1].store[type] ==undefined || labs[1].store[type] < amount){
if(room.storage.store[type])withdrawTarget = room.storage;
else if(room.terminal.store[type])withdrawTarget = room.terminal;
if(labs[1].store[type])amount -= labs[1].store[type];
WAT(creep,withdrawTarget,labs[1],type,amount)
}
}
}
}else if(state == STATE_RECOVERY){
if(!creep){
autoSpawnCreep(creepName)
}else{
var mission = false;
labs.forEach(lab => {
if(mission == false && lab.mineralType){
WAT(creep,lab,room.storage,lab.mineralType,3000)
mission = true;
}
});
}
}
}
};
function initMemory(roomName){
if(!Memory.lab){
Memory.lab = {}
}
if(!Memory.lab[roomName]){
Memory.lab[roomName] = {}
}
if(Memory.lab[roomName].state === undefined){
Memory.lab[roomName].state = STATE_REACTION
}
if(!Memory.lab[roomName].labs || Game.time % 3 == 0){
var labs = room.find(FIND_STRUCTURES,{filter:o=>(o.structureType == STRUCTURE_LAB)})
labs.forEach(lab => {
lab.value = 0;
labs.forEach(l => {
if(lab.pos.inRangeTo(l,2)){
lab.value ++;
}
});
});
labs.sort((a,b)=>(b.value - a.value));
for(var i = 0;i<labs.length;i++){
labs[i] = labs[i].id;
}
Memory.lab[roomName].labs = labs;
}
if(Memory.lab[roomName].labs.length >= 3){
return true;
}else{
console.log('ERROR: Room '+roomName+' must have more than 3 labs');
return false;
}
}
function findMaterial(product){
for(var i in REACTIONS){
for(var j in REACTIONS[i]){
if(REACTIONS[i][j] == product){
return [i,j]
}
}
}
return null
}
function getAvaliableSpawn(room){
for (var spawnname in Game.spawns){
var spawn = Game.spawns[spawnname]
if(spawn.room.name == room && spawn.spawning == null){
return spawn
}
}
return null;
}
function creepKill(creep){
const spawn = creep.pos.findClosestByPath(FIND_STRUCTURES,{filter:{structureType:STRUCTURE_SPAWN}})
if(!creep.pos.isNearTo(spawn))creep.moveTo(spawn,{ignoreCreep:false})
else spawn.recycleCreep(creep)
}
function WAT(creep,withdrawTarget,transferTarget,type,amount){
if(_.sum(creep.store) && creep.store[type] != _.sum(creep.store)){
//console.log(creep.store[type] , _.sum(creep.store),type)
creep.moveTo(creep.room.storage,{ignoreCreep:false})
if(creep.pos.isNearTo(creep.room.storage)){
for (var resourceType in creep.store){
if(resourceType != type){
creep.transfer(creep.room.storage,resourceType)
}
}
}
return;
}
amount = Math.min(amount,creep.store.getFreeCapacity(type));
//console.log('amount',amount)
if(_.sum(creep.store) == 0){
amount = Math.min(amount,withdrawTarget.store[type]);
creep.moveTo(withdrawTarget,{ignoreCreep:false})
if(creep.pos.isNearTo(withdrawTarget)){
creep.withdraw(withdrawTarget,type,amount)
}
}else{
//console.log(withdrawTarget,creep.store.getFreeCapacity(type),withdrawTarget.store[type])
if(withdrawTarget && creep.store[type] < amount && creep.store.getFreeCapacity(type) > 0 && withdrawTarget.store[type] > 0){
amount = Math.min(amount,creep.store.getFreeCapacity(type),withdrawTarget.store[type]);
creep.moveTo(withdrawTarget,{ignoreCreep:false})
if(creep.pos.isNearTo(withdrawTarget)){
creep.withdraw(withdrawTarget,type,amount)
}
}else{
creep.moveTo(transferTarget,{ignoreCreep:false})
if(creep.pos.isNearTo(transferTarget)){
creep.transfer(transferTarget,type)
}
}
}
}
function autoSpawnCreep(creepName){
var spawn = getAvaliableSpawn(room.name)
if(spawn){
spawn.spawnCreep([CARRY,CARRY,MOVE,CARRY,CARRY,MOVE,CARRY,CARRY,MOVE,CARRY,CARRY,MOVE,CARRY,CARRY,MOVE,
CARRY,CARRY,MOVE,CARRY,CARRY,MOVE,CARRY,CARRY,MOVE,CARRY,CARRY,MOVE,CARRY,CARRY,MOVE,],
creepName)
}
}
function getAllType(type){
var amount = 0;
amount += room.storage.store[type]
amount += room.terminal.store[type]
labs.forEach(lab => {
amount += lab.store[type];
});
if(creep)
amount += creep.store[type]
return amount;
}
function pushMission(mission,roomName){
mission[1] -= getAllType(mission[0])
if(mission[1] <= 0)return;
else {
needs.push(mission)
var materials = findMaterial(mission[0])
if(materials){
pushMission([materials[0],mission[1]],roomName)
pushMission([materials[1],mission[1]],roomName)
}
}
}