forked from screeps/backend-local
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
358 lines (318 loc) · 11.2 KB
/
utils.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
348
349
350
351
352
353
354
355
356
357
358
const _ = require('lodash');
const common = require('@screeps/common');
const config = common.configManager.config;
const C = config.common.constants;
const db = common.storage.db;
const env = common.storage.env;
const q = require('q');
const png = require('pngjs').PNG;
const fs = require('fs');
const path = require('path');
exports.roomNameFromXY = function(x,y) {
if(x < 0) {
x = 'W'+(-x-1);
}
else {
x = 'E'+(x);
}
if(y < 0) {
y = 'N'+(-y-1);
}
else {
y = 'S'+(y);
}
return ""+x+y;
}
exports.roomNameToXY = function(name) {
var [match,hor,x,ver,y] = name.match(/^(\w)(\d+)(\w)(\d+)$/);
if(hor == 'W') {
x = -x-1;
}
else {
x = +x;
//x--;
}
if(ver == 'N') {
y = -y-1;
}
else {
y = +y;
//y--;
}
return [x,y];
}
exports.translateModulesFromDb = function(modules) {
modules = modules || {};
for(var key in modules) {
var newKey = key.replace(/\$DOT\$/g, '.');
newKey = newKey.replace(/\$SLASH\$/g, '/');
newKey = newKey.replace(/\$BACKSLASH\$/g, '\\');
if(newKey != key) {
modules[newKey] = modules[key];
delete modules[key];
}
}
return modules;
};
exports.translateModulesToDb = function(modules) {
modules = modules || {};
for(var key in modules) {
var newKey = key.replace(/\./g,'$DOT$');
newKey = newKey.replace(/\//g,'$SLASH$');
newKey = newKey.replace(/\\/g,'$BACKSLASH$');
if(newKey[0] == '$') {
delete modules[key];
continue;
}
if(newKey != key) {
modules[newKey] = modules[key];
delete modules[key];
}
}
if(!modules.main) {
modules.main = '';
}
return modules;
};
exports.getUserWorldStatus = function(user) {
return db['rooms.objects'].count({user: ""+user._id})
.then((objectsCnt) => {
if(!objectsCnt) {
return {status: 'empty'};
}
return db['rooms.objects'].find({$and: [
{user: ""+user._id},
{type: {$in: ['spawn','controller']}}
]}).then((objects) => {
var spawns = false;
if(objects) {
objects.forEach((i) => {
if (i.type == 'spawn') {
if(!_.any(objects, {type: 'controller', room: i.room, user: i.user})) {
return;
}
spawns = true;
}
})
}
return {status: spawns ? 'normal' : 'lost'};
})
})
};
exports.respawnUser = async function(userId) {
return db['users'].findOne({username: C.SYSTEM_USERNAME})
.then(async systemUser => {
if(!systemUser) {
return q.reject('no system user');
}
const gameTime = await common.getGametime();
await db['rooms.objects'].removeWhere({$and: [{user: ""+userId}, {type: {$in: ['creep','powerCreep','constructionSite']}}]});
await db['users.power_creeps'].removeWhere({user: ""+userId});
const objects = await db['rooms.objects'].find({user: "" + userId, type: {$in: _.keys(C.CONSTRUCTION_COST)}});
if(objects.length) {
await db['rooms.objects'].insert(objects.map(i => ({
type: 'ruin',
user: ""+systemUser._id,
room: i.room,
x: i.x,
y: i.y,
structure: {
id: i._id,
type: i.type,
hits: 0,
hitsMax: i.hitsMax,
user: ""+systemUser._id
},
store: i.store || {},
destroyTime: gameTime,
decayTime: gameTime + 500000
})));
}
await db['rooms.objects'].update({user: "" + userId, type: 'ruin'}, {$set: {user: ""+systemUser._id}});
await db['rooms.objects'].removeWhere({user: "" + userId, type: {$ne: 'controller'}});
await db['rooms.flags'].removeWhere({user: ""+userId});
const controllers = db['rooms.objects'].find({$and: [{user: ""+userId}, {type: 'controller'}]});
for(let i in controllers) {
await db.rooms.update({_id: controllers[i].room}, {$set: {status: 'normal'}});
}
await db['rooms.objects'].update({$and: [{user: "" + userId}, {type: 'controller'}]}, {
$set: {
level: 0,
hits: 0,
hitsMax: 0,
progress: 0,
progressTotal: 0,
user: null,
downgradeTime: null,
safeMode: null,
safeModeAvailable: 0,
safeModeCooldown: null
}
});
})
.then(() => db['users'].update({_id: "" + userId}, {$set: {rooms: []}}));
};
exports.withHelp = function(array) {
var fn = array[1];
fn._help = array[0];
return fn;
};
exports.generateCliHelp = function(prefix, container) {
return `Available methods:\r\n`+Object.keys(container).filter(i => typeof container[i] == 'function').map(i => ' - ' + prefix + (container[i]._help || i)).join('\r\n');
};
exports.writePng = function(colors, width, height, filename) {
var image = new png({width, height});
for(var y=0; y<height; y++) {
for(var x=0; x<width; x++) {
var idx = (width*y + x) << 2;
image.data[idx] = colors[y][x][0];
image.data[idx+1] = colors[y][x][1];
image.data[idx+2] = colors[y][x][2];
image.data[idx+3] = colors[y][x][3] === undefined ? 255 : colors[y][x][3];
}
}
var defer = q.defer();
image.pack().pipe(fs.createWriteStream(filename)).on('finish', () => defer.resolve());
return defer.promise;
};
exports.createTerrainColorsMap = function(terrain, zoomIn) {
var colors = {},
width = 50, height = 50;
for(var y=0; y<height; y++) {
if(zoomIn) {
colors[y * 3] = {};
colors[y * 3 + 1] = {};
colors[y * 3 + 2] = {};
}
else {
colors[y] = {};
}
for(var x=0; x<width; x++) {
var color;
if(common.checkTerrain(terrain,x,y,C.TERRAIN_MASK_WALL)) {
color = [0,0,0];
}
else if(common.checkTerrain(terrain,x,y,C.TERRAIN_MASK_SWAMP)) {
color = [35,37,19];
}
else if(x == 0 || y == 0 || x == 49 || y == 49) {
color = [50,50,50];
}
else {
color = [43,43,43];
}
if(zoomIn) {
for (var dx = 0; dx < 3; dx++) {
for (var dy = 0; dy < 3; dy++) {
colors[y * 3 + dy][x * 3 + dx] = color;
}
}
}
else {
colors[y][x] = color;
}
}
}
return colors;
};
exports.writeTerrainToPng = function(terrain, filename, zoomIn) {
var colors = exports.createTerrainColorsMap(terrain, zoomIn);
return exports.writePng(colors, 50*(zoomIn?3:1), 50*(zoomIn?3:1), filename);
};
exports.loadBot = function(name) {
var dir = config.common.bots[name];
if(!dir) {
throw new Error(`Bot AI with the name "${name}" doesn't exist`);
}
var stat = fs.statSync(dir);
if (!stat.isDirectory()) {
throw new Error(`"${dir}" is not a directory`);
}
fs.statSync(path.resolve(dir, 'main.js'));
var files = fs.readdirSync(dir), modules = {};
files.forEach(file => {
var m = file.match(/^(.*)\.js$/);
if(!m) {
return;
}
modules[m[1]] = fs.readFileSync(path.resolve(dir, file), {encoding: 'utf8'});
});
return exports.translateModulesToDb(modules);
};
exports.reloadBotUsers = function(name) {
return db.users.find({bot: name})
.then(users => {
if(!users.length) {
return 'No bot players found';
}
var modules = exports.loadBot(name);
var timestamp = Date.now();
return db['users.code'].insert(users.map(i => ({
user: i._id,
branch: 't'+timestamp,
timestamp,
activeWorld: true,
activeSim: true,
modules
})))
.then(() => db['users.code'].removeWhere({$and: [
{user: {$in: users.map(i => i._id)}},
{branch: {$ne: 't'+timestamp}}
]}))
.then(() => 'Reloaded scripts for users: '+users.map(i => i.username).join(', '));
})
};
exports.isBus = function isBus(coord) {
return coord < 0 && (coord+1) % 10 == 0 || coord > 0 && (coord) % 10 == 0 || coord == 0;
};
exports.isCenter = function isCenter(x,y) {
return (x < 0 && Math.abs(x+1)%10 >= 4 && Math.abs(x+1)%10 <= 6 || x >= 0 && Math.abs(x)%10 >= 4 && Math.abs(x)%10 <= 6) &&
(y < 0 && Math.abs(y+1)%10 >= 4 && Math.abs(y+1)%10 <= 6 || y >= 0 && Math.abs(y)%10 >= 4 && Math.abs(y)%10 <= 6);
};
exports.isVeryCenter = function isVeryCenter(x,y) {
return (x < 0 && Math.abs(x+1)%10 == 5 || x >= 0 && Math.abs(x)%10 == 5) &&
(y < 0 && Math.abs(y+1)%10 == 5 || y >= 0 && Math.abs(y)%10 == 5);
};
exports.activateRoom = function activateRoom(room) {
return env.sadd(env.keys.ACTIVE_ROOMS, room);
};
exports.getActiveRooms = function getActiveRooms() {
return env.smembers(env.keys.ACTIVE_ROOMS);
};
exports.findFreePos = function findFreePos(roomName, distance, rect, exclude) {
if(!rect) {
rect = {x1: 4, x2: 45, y1: 4, y2: 45};
}
return q.all([
db['rooms.objects'].find({room: roomName}),
db['rooms.terrain'].findOne({room: roomName})])
.then(([objects, terrain]) => {
if (!terrain) {
return q.reject();
}
var x, y, spot, hasObjects, counter = 0;
do {
x = rect.x1 + Math.floor(Math.random() * (rect.x2 - rect.x1));
y = rect.y1 + Math.floor(Math.random() * (rect.y2 - rect.y1));
if(exclude && exclude.x == x && exclude.y == y) {
continue;
}
spot = true;
for (var dx = -distance; dx <= distance; dx++) {
for (var dy = -distance; dy <= distance; dy++) {
if (common.checkTerrain(terrain.terrain, x + dx, y + dy, C.TERRAIN_MASK_WALL)) {
spot = false;
}
}
}
hasObjects = _.any(objects, i => Math.abs(i.x - x) <= distance && Math.abs(i.y - y) <= distance &&
C.OBSTACLE_OBJECT_TYPES.concat(['rampart','portal']).indexOf(object.type) != -1);
counter++;
}
while ((!spot || hasObjects) && counter < 500);
if (!spot || hasObjects) {
return q.reject();
}
return {x, y};
});
}