Browse Source

anomaly portals

scitor 1 year ago
parent
commit
e97cc1025e
Signed by: scitor (C4CB001A60CA5028)
2 changed files with 71 additions and 612 deletions
  1. 70
    608
      index.js
  2. 1
    4
      package.json

+ 70
- 608
index.js View File

@@ -1,4 +1,4 @@
1
-let kWorldSize = 255; // Talk to marcel before growing world larger than W127N127 :: E127S127
1
+let kWorldSize = 255;
2 2
 
3 3
 module.exports = function (config) {
4 4
     const common = config.common,
@@ -6,98 +6,7 @@ module.exports = function (config) {
6 6
         storage = common.storage,
7 7
         engine = config.engine,
8 8
         db = storage.db,
9
-        q = require('q'),
10
-        fs = require('fs'),
11
-        path = require('path'),
12
-        _ = require('lodash'),
13
-        zlib = require('zlib');
14
-
15
-    const utils = {
16
-        roomNameFromXY: function (x, y) {
17
-            if (x < 0) {
18
-                x = 'W' + (-x - 1)
19
-            } else {
20
-                x = 'E' + (x)
21
-            }
22
-            if (y < 0) {
23
-                y = 'N' + (-y - 1)
24
-            } else {
25
-                y = 'S' + (y)
26
-            }
27
-            return '' + x + y
28
-        },
29
-        roomNameToXY: function (name) {
30
-            var [match, hor, x, ver, y] = name.match(/^(\w)(\d+)(\w)(\d+)$/)
31
-            if (hor == 'W') {
32
-                x = -x - 1
33
-            } else {
34
-                x = +x
35
-                // x--;
36
-            }
37
-            if (ver == 'N') {
38
-                y = -y - 1
39
-            } else {
40
-                y = +y
41
-                // y--;
42
-            }
43
-            return [x, y]
44
-        },
45
-        writePng: function (colors, width, height) {
46
-            let image = new ImageData(width, height)
47
-            for (var y = 0; y < height; y++) {
48
-                for (var x = 0; x < width; x++) {
49
-                    var idx = (width * y + x) << 2
50
-
51
-                    image.data[idx] = colors[y][x][0]
52
-                    image.data[idx + 1] = colors[y][x][1]
53
-                    image.data[idx + 2] = colors[y][x][2]
54
-                    image.data[idx + 3] = colors[y][x][3] === undefined ? 255 : colors[y][x][3]
55
-                }
56
-            }
57
-            return image
58
-        },
59
-        createTerrainColorsMap: function (terrain, zoomIn) {
60
-            var colors = {},
61
-                width = 50, height = 50
62
-
63
-            for (var y = 0; y < height; y++) {
64
-                if (zoomIn) {
65
-                    colors[y * 3] = {}
66
-                    colors[y * 3 + 1] = {}
67
-                    colors[y * 3 + 2] = {}
68
-                } else {
69
-                    colors[y] = {}
70
-                }
71
-                for (var x = 0; x < width; x++) {
72
-                    var color
73
-                    if (common.checkTerrain(terrain, x, y, C.TERRAIN_MASK_WALL)) {
74
-                        color = [0, 0, 0]
75
-                    } else if (common.checkTerrain(terrain, x, y, C.TERRAIN_MASK_SWAMP)) {
76
-                        color = [35, 37, 19]
77
-                    } else if (x == 0 || y == 0 || x == 49 || y == 49) {
78
-                        color = [50, 50, 50]
79
-                    } else {
80
-                        color = [43, 43, 43]
81
-                    }
82
-                    if (zoomIn) {
83
-                        for (var dx = 0; dx < 3; dx++) {
84
-                            for (var dy = 0; dy < 3; dy++) {
85
-                                colors[y * 3 + dy][x * 3 + dx] = color
86
-                            }
87
-                        }
88
-                    } else {
89
-                        colors[y][x] = color
90
-                    }
91
-                }
92
-            }
93
-
94
-            return colors
95
-        },
96
-        writeTerrainToPng: function (terrain, zoomIn) {
97
-            var colors = this.createTerrainColorsMap(terrain, zoomIn)
98
-            return this.writePng(colors, 50 * (zoomIn ? 3 : 1), 50 * (zoomIn ? 3 : 1))
99
-        }
100
-    };
9
+        q = require('q');
101 10
 
102 11
     var getGametime = function () {
103 12
         return storage.env.get(storage.env.keys.GAMETIME).then(data => parseInt(data));
@@ -105,540 +14,97 @@ module.exports = function (config) {
105 14
 
106 15
     if (config.cronjobs) {
107 16
         var gameTime;
108
-        config.cronjobs.genAnnomalies = [5 * 60, function () {
17
+        config.cronjobs.genAnomalies = [5 * 60, function () {
109 18
             return getGametime().then(currentGameTime => {
110 19
                 gameTime = currentGameTime;
111 20
 
112
-                return db.rooms.find({$and: [{central: true}, {status: 'normal'}]});
21
+                return db.rooms.find({anomaly: true});
113 22
             }).then(rooms => q.all(rooms.map(room => {
114
-                var thisRoomId = room._id,
115
-                    fromX, fromY, toX, toY;
116
-                var otherRoomId = thisRoomId.replace('W', 'E');
117
-
118
-                var respawnTime = Math.round(Math.random() * C.POWER_BANK_RESPAWN_TIME / 2 + C.POWER_BANK_RESPAWN_TIME * 0.75);
119
-                if (!room.centralPortalTime || gameTime >= room.centralPortalTime) {
120
-                    return db['rooms.terrain'].findOne({room: thisRoomId})
121
-                    .then((data) => {
122
-                        [fromX, fromY] = findFree(data);
123
-
124
-                        return db.rooms.count({_id:otherRoomId});
125
-                    }).then((count) => {
126
-                        if (count > 0)
127
-                            return removeRoom(otherRoomId);
128
-                    }).then(() => generateRoom(otherRoomId, {
129
-                        exits: {left: [], right: [], top: [], bottom: []},
130
-                        keeperLairs: false,
131
-                        controller: false,
132
-                        mineral: 'X',
133
-                        sources: 8
134
-                    }))
135
-                    .then(() => db['rooms.objects'].findOne({type: 'mineral', room: otherRoomId}))
136
-                    .then(mineral=> db['rooms.objects'].insert({
137
-                        type: 'extractor',
138
-                        room: room._id,
139
-                        x: mineral.x,
140
-                        y: mineral.y
141
-                    }))
142
-                    .then(() => db['rooms.terrain'].findOne({room: otherRoomId}))
143
-                    .then((data) => {
144
-                        [toX, toY] = findFree(data);
145
-                        let portalTime = Math.round(respawnTime * (2+Math.random())/3);
146
-
147
-                        return db["rooms.objects"].insert({
148
-                            room: thisRoomId,
149
-                            type: "portal",
150
-                            x: fromX,
151
-                            y: fromY,
152
-                            destination: {x: toX, y: toY, room: otherRoomId},
153
-                            ticksToDecay: portalTime
154
-                        }).then(()=> db["rooms.objects"].insert({
155
-                            room: otherRoomId,
156
-                            type: "portal",
157
-                            x: toX,
158
-                            y: toY,
159
-                            destination: {x: fromX, y: fromY, room: thisRoomId},
160
-                            ticksToDecay: portalTime
161
-                        })).then(() => db.rooms.update({_id: thisRoomId}, {$set: {active:true, centralPortalTime: gameTime + respawnTime}}))
162
-                           .then(() => db.rooms.update({_id: otherRoomId}, {$set: {active:true}}));
163
-                    });
23
+                var respawnTime = Math.round(10 * Math.random() * C.POWER_BANK_RESPAWN_TIME / 2 + C.POWER_BANK_RESPAWN_TIME * 0.75);
24
+                if (!room.portalTime) {
25
+                    room.portalTime = gameTime + respawnTime;
26
+                    return db.rooms.update({_id: room._id}, {$set: room});
164 27
                 }
165
-            })));
166
-            /*.then(() => {
167
-                return db.rooms.find({$and: [{central: true}, {status: 'normal'}]});
168
-            }).then(rooms => q.all(rooms.map(room => {
28
+                if (gameTime >= room.portalTime) {
29
+                    var thisRoomId = room._id,
30
+                        otherRoom, fromX, fromY, toX, toY;
169 31
 
170
-                if (room.bus || room.central)
171
-                    return;
172
-
173
-                var thisRoomId = room._id,
174
-                    fromX, fromY, toX, toY;
175
-
176
-                var respawnTime = Math.round(Math.random() * C.POWER_BANK_RESPAWN_TIME / 2 + C.POWER_BANK_RESPAWN_TIME * 0.75);
177
-                if (!room.annomalyTime || gameTime >= room.annomalyTime) {
32
+                    let otherRoomName = isCenter(thisRoomId) ? findCenterRoom() : findRoom();
178 33
                     return db['rooms.terrain'].findOne({room: thisRoomId})
179 34
                         .then((data) => {
180 35
                             [fromX, fromY] = findFree(data);
181 36
 
182
-                            return db.rooms.count({_id:otherRoomId});
183
-                        }).then((count) => {
184
-                            if (count > 0)
185
-                                return utils.removeRoom(otherRoomId);
186
-                        }).then(() => utils.generateRoom(otherRoomId, {
187
-                            exits: {left: [], right: [], top: [], bottom: []},
188
-                            keeperLairs: false,
189
-                            controller: false,
190
-                            mineral: 'X',
191
-                            sources: 8
192
-                        }))
193
-                        .then(() => db['rooms.objects'].findOne({type: 'mineral', room: otherRoomId}))
194
-                        .then(mineral=> db['rooms.objects'].insert({
195
-                            type: 'extractor',
196
-                            room: room._id,
197
-                            x: mineral.x,
198
-                            y: mineral.y
199
-                        }))
200
-                        .then(() => db['rooms.terrain'].findOne({room: otherRoomId}))
201
-                        .then((data) => {
37
+                            return db.rooms.find({_id:otherRoomName});
38
+                        }).then(otherRooms => {
39
+                            if (!otherRooms.length)
40
+                                throw new Error('No other rooms found: ' + otherRoomName);
41
+
42
+                            otherRoom = otherRooms[0];
43
+                            return db['rooms.terrain'].findOne({room: otherRoom._id});
44
+                        }).then((data) => {
202 45
                             [toX, toY] = findFree(data);
203
-                            let portalTime = Math.round(respawnTime * (2+Math.random())/3);
204 46
 
205
-                            return db["rooms.objects"].insert({
47
+                            let portalTime = Math.round(respawnTime * (2 + Math.random()) / 3);
48
+
49
+                            return db["rooms.objects"].remove({
50
+                                type: "portal",
51
+                                room: thisRoomId,
52
+                                destinationRoom: otherRoom._id
53
+                            }).then(()=> db["rooms.objects"].remove({
54
+                                type: "portal",
55
+                                room: otherRoom._id,
56
+                                destinationRoom: thisRoomId
57
+                            })).then(()=> db["rooms.objects"].insert({
206 58
                                 room: thisRoomId,
207 59
                                 type: "portal",
208 60
                                 x: fromX,
209 61
                                 y: fromY,
210
-                                destination: {x: toX, y: toY, room: otherRoomId},
211
-                                ticksToDecay: portalTime
212
-                            }).then(()=> db["rooms.objects"].insert({
213
-                                room: otherRoomId,
62
+                                destination: {x: toX, y: toY, room: otherRoom._id},
63
+                                destinationRoom: otherRoom._id,
64
+                                decayTime: gameTime + portalTime
65
+                            })).then(()=> db["rooms.objects"].insert({
66
+                                room: otherRoom._id,
214 67
                                 type: "portal",
215 68
                                 x: toX,
216 69
                                 y: toY,
217 70
                                 destination: {x: fromX, y: fromY, room: thisRoomId},
218
-                                ticksToDecay: portalTime
219
-                            })).then(() => db.rooms.update({_id: thisRoomId}, {$set: {active:true, annomalyTime: gameTime + respawnTime}}))
220
-                                .then(() => db.rooms.update({_id: otherRoomId}, {$set: {active:true}}));
71
+                                destinationRoom: thisRoomId,
72
+                                decayTime: gameTime + portalTime
73
+                            })).then(() => db.rooms.update({_id: thisRoomId}, {$set: {portalTime: gameTime + respawnTime}}))
74
+                                /*.then(() => db.rooms.update({_id: otherRoom._id}, {$set: {active:true}}))*/;
221 75
                         });
222 76
                 }
223
-            })));*/
77
+            })));
224 78
         }];
225 79
     }
80
+};
226 81
 
227
-    function generateRoom(roomName, opts) {
228
-        opts = opts || {};
229
-
230
-        opts.exits = opts.exits || {};
231
-
232
-        if(!/^[WE]\d+[NS]\d+$/.test(roomName)) {
233
-            return q.reject("Invalid room name");
234
-        }
235
-
236
-        function _checkFlood(terrain) {
237
-
238
-            var startX, startY, x, y;
239
-
240
-            for(x=0; x<50; x++) {
241
-                for(y=0; y<50; y++) {
242
-                    if(!terrain[y][x].wall) {
243
-                        startX = x;
244
-                        startY = y;
245
-                        break;
246
-                    }
247
-                }
248
-                if(startX && startY) {
249
-                    break;
250
-                }
251
-            }
252
-
253
-            var visited = {};
254
-            for(y=0; y<50; y++) {
255
-                visited[y] = {};
256
-                for (x = 0; x < 50; x++) {
257
-                    visited[y][x] = false;
258
-                }
259
-            }
260
-
261
-            var list = [[startX, startY]];
262
-            do {
263
-                var i = list.pop();
264
-
265
-                x = i[0];
266
-                y = i[1];
267
-
268
-                visited[y][x] = true;
269
-                for(var dx=-1; dx<=1; dx++) {
270
-                    for(var dy=-1; dy<=1; dy++) {
271
-                        if(!dx && !dy) {
272
-                            continue;
273
-                        }
274
-                        if(x+dx >= 0 && x+dx <= 49 && y+dy >= 0 && y+dy <= 49 && !terrain[y+dy][x+dx].wall && !visited[y+dy][x+dx]) {
275
-                            visited[y+dy][x+dx] = true;
276
-                            list.push([x+dx,y+dy]);
277
-                        }
278
-                    }
279
-                }
280
-            }
281
-            while(list.length > 0);
282
-
283
-            for(y=0; y<50; y++) {
284
-                for (x = 0; x < 50; x++) {
285
-                    if(!terrain[y][x].wall && !visited[y][x]) {
286
-                        return false;
287
-                    }
288
-                }
289
-            }
290
-
291
-            return true;
292
-        }
293
-
294
-        function _smoothTerrain(terrain, factor, key) {
295
-            var newTerrain = {};
296
-
297
-            for(var y=0; y<50; y++) {
298
-                newTerrain[y] = {};
299
-                for(var x=0; x<50; x++) {
300
-                    newTerrain[y][x] = _.clone(terrain[y][x]);
301
-                    var cnt = 0;
302
-                    for(var dx=-1;dx<=1;dx++) {
303
-                        for(var dy=-1;dy<=1;dy++) {
304
-                            if(key == 'wall' && (x+dx < 0 || y+dy < 0 || x+dx > 49 || y+dy > 49) ||
305
-                                x+dx >= 0 && x+dx <= 49 && y+dy >= 0 && y+dy <= 49 && terrain[y+dy][x+dx][key]) {
306
-                                cnt++;
307
-                            }
308
-                        }
309
-                    }
310
-                    newTerrain[y][x][key] = cnt >= factor;
311
-                    if(key == 'wall') {
312
-                        if(x == 0 || x == 49 || y == 0 || y == 49) {
313
-                            newTerrain[y][x].wall = true;
314
-                        }
315
-                        if(terrain[y][x].forceOpen) {
316
-                            newTerrain[y][x].wall = false;
317
-                        }
318
-                    }
319
-                }
320
-            }
321
-
322
-            return newTerrain;
323
-        }
324
-
325
-        function _genTerrain(wallType, sources) {
326
-
327
-            var types = {
328
-                1: {fill: 0.4, smooth: 10, factor: 5},
329
-                2: {fill: 0.2, smooth: 20, factor: 4},
330
-                3: {fill: 0.2, smooth: 20, factor: 4},
331
-                4: {fill: 0.3, smooth: 18, factor: 4},
332
-                5: {fill: 0.3, smooth: 10, factor: 4},
333
-                6: {fill: 0.3, smooth: 10, factor: 4},
334
-                7: {fill: 0.3, smooth: 10, factor: 4},
335
-                8: {fill: 0.35, smooth: 15, factor: 4},
336
-                9: {fill: 0.3, smooth: 2, factor: 4},
337
-                10: {fill: 0.35, smooth: 2, factor: 4},
338
-                11: {fill: 0.35, smooth: 5, factor: 4},
339
-                12: {fill: 0.35, smooth: 5, factor: 4},
340
-                13: {fill: 0.25, smooth: 5, factor: 4},
341
-
342
-                14: {fill: 0.4, smooth: 3, factor: 5},
343
-                15: {fill: 0.5, smooth: 3, factor: 5},
344
-                16: {fill: 0.45, smooth: 4, factor: 5},
345
-                17: {fill: 0.45, smooth: 6, factor: 5},
346
-                18: {fill: 0.45, smooth: 10, factor: 5},
347
-                19: {fill: 0.5, smooth: 10, factor: 5},
348
-
349
-                20: {fill: 0.4, smooth: 3, factor: 5},
350
-                21: {fill: 0.5, smooth: 2, factor: 5},
351
-                22: {fill: 0.45, smooth: 4, factor: 5},
352
-                23: {fill: 0.45, smooth: 6, factor: 5},
353
-                24: {fill: 0.45, smooth: 10, factor: 5},
354
-                25: {fill: 0.5, smooth: 10, factor: 5},
355
-
356
-                26: {fill: 0.45, smooth: 10, factor: 5},
357
-                27: {fill: 0.45, smooth: 6, factor: 5},
358
-                28: {fill: 0.2, smooth: 20, factor: 4}
359
-            };
360
-
361
-            var terrain, tries = 0, x, y;
362
-
363
-            do {
364
-                terrain = {};
365
-                tries++;
366
-
367
-                if(tries > 100) {
368
-                    wallType = Math.floor(Math.random()*27)+1;
369
-                    tries = 0;
370
-                }
371
-
372
-                for (y = 0; y < 50; y++) {
373
-                    terrain[y] = {};
374
-                    for (x = 0; x < 50; x++) {
375
-                        terrain[y][x] = {};
376
-                    }
377
-                }
378
-                for (y = 0; y < 50; y++) {
379
-                    for (x = 0; x < 50; x++) {
380
-                        terrain[y][x].exit = false;
381
-                        terrain[y][x].wall = Math.random() < types[wallType].fill;
382
-                        terrain[y][x].swamp = false;
383
-                    }
384
-                }
385
-
386
-                for (let i = 0; i < types[wallType].smooth; i++) {
387
-                    terrain = _smoothTerrain(terrain, types[wallType].factor, 'wall');
388
-                }
389
-            }
390
-            while(!_checkFlood(terrain));
391
-
392
-            for(let i=0; i<sources; i++) {
393
-                tries = 0;
394
-
395
-                do {
396
-
397
-                    tries++;
398
-
399
-                    x = Math.floor(Math.random() * 44) + 3;
400
-                    y = Math.floor(Math.random() * 44) + 3;
401
-
402
-                    var passNearby = false;
403
-                    for(var dx=-1; dx<=1; dx++) {
404
-                        for(var dy=-1; dy<=1; dy++) {
405
-                            if(x+dx < 0 || y+dy < 0 || x+dx > 49 || y+dy > 49) {
406
-                                continue;
407
-                            }
408
-                            if(!terrain[y+dy][x+dx].wall) {
409
-                                passNearby = true;
410
-                                break;
411
-                            }
412
-                        }
413
-                    }
414
-
415
-                    if(tries > 1000) {
416
-                        return _genTerrain(Math.floor(Math.random() * 27) + 1, sources);
417
-                    }
418
-                }
419
-                while(!terrain[y][x].wall || !passNearby);
420
-
421
-                terrain[y][x].source = true;
422
-            }
423
-
424
-
425
-            return terrain;
426
-        }
427
-
428
-        return db.rooms.findOne({_id: roomName})
429
-            .then(data => data ? q.reject('This room already exists') : undefined)
430
-            .then(() => {
431
-
432
-                if(opts.terrainType === undefined) {
433
-                    opts.terrainType = Math.floor(Math.random() * 27) + 1;
434
-                }
435
-                if(opts.sources === undefined) {
436
-                    opts.sources = Math.random() > 0.5 ? 1 : 2;
437
-                }
438
-
439
-                var roomData = _genTerrain(opts.terrainType, opts.sources);
440
-
441
-                var objects = [], terrain = [], sourceKeepers = false;
442
-
443
-                for (var y in roomData) {
444
-                    y = parseInt(y);
445
-                    for (var x in roomData[y]) {
446
-                        x = parseInt(x);
447
-                        if (roomData[y][x].wall) {
448
-                            terrain.push({type: 'wall', x, y});
449
-                        }
450
-                        if (roomData[y][x].source) {
451
-                            objects.push({
452
-                                room: roomName,
453
-                                type: 'source',
454
-                                x,
455
-                                y,
456
-                                "energy": C.SOURCE_ENERGY_NEUTRAL_CAPACITY,
457
-                                "energyCapacity": C.SOURCE_ENERGY_NEUTRAL_CAPACITY,
458
-                                "ticksToRegeneration": C.ENERGY_REGEN_TIME
459
-                            });
460
-                        }
461
-                        if (roomData[y][x].controller) {
462
-                            objects.push({room: roomName, type: 'controller', x, y, level: 0});
463
-                        }
464
-                        if (roomData[y][x].keeperLair) {
465
-                            objects.push({room: roomName, type: 'keeperLair', x, y});
466
-                            sourceKeepers = true;
467
-                        }
468
-                        if (roomData[y][x].swamp) {
469
-                            var flag = false;
470
-                            for (var dx = -1; dx <= 1; dx++) {
471
-                                for (var dy = -1; dy <= 1; dy++) {
472
-                                    if (x + dx >= 0 && y + dy >= 0 && x + dx <= 49 && y + dy <= 49 && !roomData[y + dy][x + dx].wall) {
473
-                                        flag = true;
474
-                                        break;
475
-                                    }
476
-                                }
477
-                                if (flag) {
478
-                                    break;
479
-                                }
480
-                            }
481
-                            if (flag) {
482
-                                terrain.push({type: 'swamp', x, y});
483
-                            }
484
-                        }
485
-                    }
486
-                }
487
-
488
-                terrain = common.encodeTerrain(terrain);
489
-
490
-                if(opts.mineral === undefined) {
491
-                    var types = ['H','H','H','H','H','H',  'O','O','O','O','O','O',  'Z','Z','Z', 'K','K','K', 'U','U','U', 'L','L','L', 'X'];
492
-                    opts.mineral = types[Math.floor(Math.random()*types.length)];
493
-                }
494
-
495
-                if(opts.mineral) {
496
-                    var mx,my,isWall,hasSpot,hasObjects;
497
-                    do {
498
-                        mx = 4 + Math.floor(Math.random()*42);
499
-                        my = 4 + Math.floor(Math.random()*42);
500
-                        isWall = common.checkTerrain(terrain, mx, my, C.TERRAIN_MASK_WALL);
501
-                        hasSpot = false;
502
-                        for(var dx=-1;dx<=1;dx++) {
503
-                            for(var dy=-1;dy<=1;dy++) {
504
-                                if(!common.checkTerrain(terrain,mx+dx,my+dy, C.TERRAIN_MASK_WALL)) {
505
-                                    hasSpot = true;
506
-                                }
507
-                            }
508
-                        }
509
-                        hasObjects = _.any(objects, i => (i.type == 'source' || i.type == 'controller') && Math.abs(i.x - mx) < 5 && Math.abs(i.y - my) < 5);
510
-                    }
511
-                    while(!isWall || !hasSpot || hasObjects);
512
-
513
-                    var random = Math.random(), density;
514
-                    for(var d in C.MINERAL_DENSITY_PROBABILITY) {
515
-                        if(random <= C.MINERAL_DENSITY_PROBABILITY[d]) {
516
-                            density = +d;
517
-                            break;
518
-                        }
519
-                    }
520
-
521
-                    objects.push({
522
-                        type: 'mineral',
523
-                        mineralType: opts.mineral,
524
-                        density,
525
-                        mineralAmount: C.MINERAL_DENSITY[density],
526
-                        x: mx,
527
-                        y: my,
528
-                        room: roomName
529
-                    });
530
-                }
531
-
532
-                return q.all([
533
-                    db.rooms.insert({_id: roomName, status: 'normal', active: true}),
534
-                    db['rooms.terrain'].insert({room: roomName, terrain}),
535
-                    objects.length ? db['rooms.objects'].insert(objects) : q.when()
536
-                ]);
537
-            })
538
-            .then(() => updateRoomImageAssets(roomName))
539
-            .then(() => updateTerrainData());
540
-    }
541
-
542
-    function removeRoom(roomName) {
543
-        return db.rooms.findOne({_id: roomName})
544
-            .then(data => !data ? q.reject(`The room ${roomName} is not found`) :
545
-                q.all([
546
-                    db.rooms.removeWhere({_id: roomName}),
547
-                    db['rooms.objects'].removeWhere({room: roomName}),
548
-                    db['rooms.flags'].removeWhere({room: roomName}),
549
-                    db['rooms.terrain'].removeWhere({room: roomName}),
550
-                    db['rooms.intents'].removeWhere({room: roomName}),
551
-                    db['market.stats'].removeWhere({room: roomName}),
552
-                    env.del(env.keys.MAP_VIEW+roomName)
553
-                ]))
554
-            .then(() => updateTerrainData())
555
-            .then(() => {
556
-                fs.unlinkSync(path.resolve(process.env.ASSET_DIR, 'map', roomName+'.png'));
557
-            });
558
-    }
559
-
560
-    function updateRoomImageAssets(roomName) {
561
-
562
-        return db['rooms.terrain'].findOne({room: roomName})
563
-        .then(terrainItem => utils.writeTerrainToPng(terrainItem.terrain, path.resolve(process.env.ASSET_DIR, 'map', roomName + '.png'), true))
564
-        .then(() => {
565
-            var [x,y] = utils.roomNameToXY(roomName);
566
-            x = Math.floor(x / 4) * 4;
567
-            y = Math.floor(y / 4) * 4;
568
-            var firstRoomName = utils.roomNameFromXY(x,y);
569
-            var allRoomNames = [];
570
-            for(var xx=x; xx<=x+4; xx++) {
571
-                for(var yy=y; yy<=y+4; yy++) {
572
-                    allRoomNames.push(utils.roomNameFromXY(xx,yy));
573
-                }
574
-            }
575
-            return db['rooms.terrain'].find({room: {$in: allRoomNames}})
576
-            .then(data => {
577
-                var mergedColors = {};
578
-                for(var yy=0; yy<200; yy++) {
579
-                    mergedColors[yy] = {};
580
-                    for(var xx=0; xx<200; xx++) {
581
-                        mergedColors[yy][xx] = [0,0,0,0];
582
-                    }
583
-                }
584
-                for(var xx=0; xx<4; xx++) {
585
-                    for(var yy=0; yy<4; yy++) {
586
-
587
-                        var terrainItem = _.find(data, {room: utils.roomNameFromXY(xx+x,yy+y)});
588
-                        if(!terrainItem) {
589
-                            continue;
590
-                        }
591
-
592
-                        var colors = utils.createTerrainColorsMap(terrainItem.terrain, false);
593
-                        for(var cy in colors) {
594
-                            for(var cx in colors[cy]) {
595
-                                mergedColors[parseInt(cy)+yy*50][parseInt(cx)+xx*50] = colors[cy][cx];
596
-                            }
597
-                        }
598
-                    }
599
-                }
600
-
601
-                return utils.writePng(mergedColors, 200, 200,
602
-                    path.resolve(process.env.ASSET_DIR, 'map/zoom2', firstRoomName + '.png'));
603
-            });
604
-        });
605
-    }
82
+function findRoom() {
83
+    let roomW, roomN, max = 30;
84
+    do {
85
+        roomW = Math.floor(Math.random()*max);
86
+        roomN = Math.floor(Math.random()*max);
87
+    } while (roomW%10 === 0 || roomN%10 === 0 ||
88
+             roomW%10 >= 4 && roomW%10 <= 6 ||
89
+             roomN%10 >= 4 && roomN%10 <= 6);
606 90
 
607
-    function updateTerrainData() {
91
+    return 'W'+roomW+'N'+roomN;
92
+}
608 93
 
609
-        var walled = '';
610
-        for(var i=0; i<2500; i++) {
611
-            walled += '1';
612
-        }
94
+function findCenterRoom() {
95
+    let roomW = Math.round(Math.random()*2)*10+5;
96
+    let roomN = Math.round(Math.random()*2)*10+5;
613 97
 
614
-        return q.all([
615
-            db.rooms.find(),
616
-            db['rooms.terrain'].find()
617
-        ])
618
-        .then(result => {
619
-            var [rooms,terrain] = result;
98
+    return 'W'+roomW+'N'+roomN;
99
+}
620 100
 
621
-            rooms.forEach(room => {
622
-                if(room.status == 'out of borders') {
623
-                    _.find(terrain, {room: room._id}).terrain = walled;
624
-                }
625
-                var m = room._id.match(/(W|E)(\d+)(N|S)(\d+)/);
626
-                var roomH = m[1]+(+m[2]+1)+m[3]+m[4], roomV = m[1]+m[2]+m[3]+(+m[4]+1);
627
-                if(!_.any(terrain, {room: roomH})) {
628
-                    terrain.push({room: roomH, terrain: walled});
629
-                }
630
-                if(!_.any(terrain, {room: roomV})) {
631
-                    terrain.push({room: roomV, terrain: walled});
632
-                }
633
-            });
101
+function isCenter(room) {
102
+    var [match,x,y] = room.match(/^E(\d+)N(\d+)$/);
634 103
 
635
-            return q.ninvoke(zlib, 'deflate', JSON.stringify(terrain));
636
-        })
637
-        .then(compressed => env.set(env.keys.TERRAIN_DATA, compressed.toString('base64')));
638
-    }
639
-};
104
+    return (x-5)%10===0 && (y-5)%10===0;
105
+}
640 106
 
641
-function findFree(data) {
107
+function findFree (data) {
642 108
     var x, y, isWall;
643 109
     do {
644 110
         x = Math.floor(Math.random() * 40 + 5);
@@ -648,16 +114,12 @@ function findFree(data) {
648 114
     while (isWall);
649 115
     return [x, y];
650 116
 }
117
+/**
118
+generate center room:
119
+map.generateRoom("E5N15", {sources: 7, keeperLair:false, controller: false, exits:{top:[],bottom:[],left:[],right:[]}})
651 120
 
652
-function parseRoomName(roomName) {
653
-    let room = /^([WE])([0-9]+)([NS])([0-9]+)$/.exec(roomName);
654
-    if (!room) {
655
-        throw new Error('Invalid room name ' + roomName);
656
-    }
657
-    let rx = (kWorldSize >> 1) + (room[1] === 'W' ? -Number(room[2]) : Number(room[2]) + 1);
658
-    let ry = (kWorldSize >> 1) + (room[3] === 'N' ? -Number(room[4]) : Number(room[4]) + 1);
659
-    if (!(rx >= 0 && rx <= kWorldSize && ry >= 0 && ry <= kWorldSize)) {
660
-        throw new Error('Invalid room name ' + roomName);
661
-    }
662
-    return {xx: rx, yy: ry};
663
-}
121
+generate room:
122
+map.generateRoom("E4N14", {sources: 6, keeperLair:false, controller: false, exits:{top:[],bottom:[],left:[],right:[]}})
123
+
124
+install extractors:
125
+storage.db.rooms.find({anomaly:true}).then(rooms => Promise.all(rooms.map(room=>storage.db['rooms.objects'].findOne({type:'mineral',room:room._id}).then(mineral=>storage.db['rooms.objects'].insert({type:'extractor',room:room._id,x:mineral.x,y:mineral.y})))));

+ 1
- 4
package.json View File

@@ -7,8 +7,5 @@
7 7
     "test": "echo \"Error: no test specified\" && exit 1"
8 8
   },
9 9
   "author": "scitor <scitor@el337.de>",
10
-  "license": "ISC",
11
-  "dependencies": {
12
-    "zlib": "^1.0.5"
13
-  }
10
+  "license": "ISC"
14 11
 }