-
Notifications
You must be signed in to change notification settings - Fork 1
/
prototype_room_mineral.js
209 lines (188 loc) · 5.65 KB
/
prototype_room_mineral.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
'use strict';
Room.prototype.getNextReaction = function() {
for (let mineralFirst in this.terminal.store) {
if (!REACTIONS[mineralFirst]) {
continue;
}
for (let mineralSecond in this.terminal.store) {
if (!REACTIONS[mineralFirst][mineralSecond]) {
continue;
}
let result = REACTIONS[mineralFirst][mineralSecond];
if (this.terminal.store[result] > config.mineral.minAmount) {
continue;
}
//this.log('Could build: ' + mineralFirst + ' ' + mineralSecond + ' ' + result);
return {
result: result,
first: mineralFirst,
second: mineralSecond
};
}
}
return false;
};
Room.prototype.reactions = function() {
if (!this.memory.reaction) {
let result = this.getNextReaction();
if (!result) {
return;
}
let labsAll = this.find(FIND_MY_STRUCTURES, {
filter: function(object) {
if (object.structureType != STRUCTURE_LAB) {
return false;
}
if (!object.mineralType) {
return true;
}
if (object.mineralType === result.result) {
return true;
}
return false;
}
});
let lab;
let labs = [];
let getNearLabs = function(object) {
if (object.id === lab.id) {
return false;
}
if (object.structureType != STRUCTURE_LAB) {
return false;
}
if (!object.mineralType) {
return true;
}
if (object.mineralType === result.first) {
return true;
}
if (object.mineralType === result.second) {
return true;
}
return false;
};
for (lab of labsAll) {
let labsNear = lab.pos.findInRange(FIND_MY_STRUCTURES, 2, {
filter: getNearLabs
});
if (labsNear.length >= 2) {
labs.push(lab.id);
// console.log(lab.mineralType, result.result);
for (let labNear of labsNear) {
if (!labNear.mineralType || labNear.mineralType === result.first) {
// console.log(labNear.mineralType, result.first);
labs.push(labNear.id);
break;
}
}
for (let labNear of labsNear) {
if (labNear.id === labs[1]) {
continue;
}
if (!labNear.mineralType || labNear.mineralType === result.second) {
// console.log(labNear.mineralType, result.second);
labs.push(labNear.id);
break;
}
}
break;
}
}
if (labs.length < 3) {
return false;
}
this.memory.reaction = {
result: result,
labs: labs
};
// this.log('Setting reaction: ' + JSON.stringify(this.memory.reaction));
}
if (this.terminal.store[this.memory.reaction.result.result] > config.mineral.minAmount) {
this.log('Done with reaction:' + this.memory.reaction.result.result);
delete this.memory.reaction;
}
};
Room.prototype.orderMinerals = function() {
let minerals = this.find(FIND_MINERALS);
let resource = minerals[0].mineralType;
if (Game.time % 20 === 0) {
let baseMinerals = [
RESOURCE_HYDROGEN,
RESOURCE_OXYGEN,
RESOURCE_UTRIUM,
RESOURCE_LEMERGIUM,
RESOURCE_KEANIUM,
RESOURCE_ZYNTHIUM,
RESOURCE_CATALYST,
RESOURCE_GHODIUM
];
let room = this;
let orderByDistance = function(object) {
return Game.map.getRoomLinearDistance(room.name, object);
};
for (let mineral of baseMinerals) {
if (!this.terminal.store[mineral]) {
let roomsOther = _.sortBy(Memory.myRooms, orderByDistance);
for (let roomOtherName of roomsOther) {
if (roomOtherName === this.name) {
continue;
}
let roomOther = Game.rooms[roomOtherName];
if (!roomOther || roomOther === null) {
continue;
}
let minerals = roomOther.find(FIND_MINERALS);
if (minerals.length === 0) {
continue;
}
let mineralType = minerals[0].mineralType;
if (!roomOther.terminal || roomOther.terminal[minerals[0].mineralType] < config.mineral.minAmount) {
continue;
}
if (mineralType === mineral) {
roomOther.memory.mineralOrder = roomOther.memory.mineralOrder || {};
if (roomOther.memory.mineralOrder[room.name]) {
break;
}
roomOther.memory.mineralOrder[room.name] = 1000;
// room.log('Ordering ' + mineralType + ' from ' + roomOther.name);
break;
}
}
}
}
}
};
Room.prototype.handleTerminal = function() {
let minerals = this.find(FIND_MINERALS);
if (minerals.length === 0) {
return false;
}
let resource = minerals[0].mineralType;
if (!this.terminal) {
return false;
}
this.orderMinerals();
this.reactions();
if (!this.memory.mineralOrder || Object.keys(this.memory.mineralOrder).length === 0) {
return false;
}
let roomOtherName = Object.keys(this.memory.mineralOrder)[0];
let roomOther = Game.rooms[roomOtherName];
let order = this.memory.mineralOrder[roomOtherName];
let linearDistanceBetweenRooms = Game.map.getRoomLinearDistance(this.name, roomOtherName);
let energy = Math.ceil(0.1 * order * linearDistanceBetweenRooms);
if (this.terminal.store.energy < energy) {
//this.log('Terminal not enough energy');
this.memory.terminalTooLessEnergy = true;
return false;
}
this.memory.terminalTooLessEnergy = false;
if (this.terminal.store[resource] < order) {
return false;
}
this.terminal.send(resource, order, roomOtherName);
delete this.memory.mineralOrder[roomOtherName];
return true;
};