scitor 2 years ago
commit
c4e1b66330
2 changed files with 109 additions and 0 deletions
  1. 98
    0
      index.js
  2. 11
    0
      package.json

+ 98
- 0
index.js View File

@@ -0,0 +1,98 @@
1
+let kWorldSize = 255; // Talk to marcel before growing world larger than W127N127 :: E127S127
2
+
3
+module.exports = function (config) {
4
+    const common = config.common,
5
+        C = config.common.constants,
6
+        storage = common.storage,
7
+        engine = config.engine,
8
+        db = storage.db,
9
+        q = require('q');
10
+
11
+    var getGametime = function () {
12
+        return storage.env.get(storage.env.keys.GAMETIME).then(data => parseInt(data));
13
+    };
14
+
15
+    if (config.cronjobs) {
16
+        var gameTime;
17
+        config.cronjobs.genBusPortals = [5 * 60, function () {
18
+            return getGametime().then(currentGameTime => {
19
+                gameTime = currentGameTime;
20
+
21
+                return db.rooms.find({$and: [{bus: true}, {status: 'normal'}]});
22
+            }).then(rooms => q.all(rooms.map(room => {
23
+                var respawnTime = Math.round(Math.random() * C.POWER_BANK_RESPAWN_TIME / 2 + C.POWER_BANK_RESPAWN_TIME * 0.75);
24
+                if (!room.busPortalTime) {
25
+                    room.busPortalTime = gameTime + respawnTime;
26
+                    return db.rooms.update({_id: room._id}, {$set: room});
27
+                }
28
+                if (gameTime >= room.busPortalTime) {
29
+                    var thisRoomId = room._id,
30
+                        otherRoom, fromX, fromY, toX, toY;
31
+                    return db['rooms.terrain'].findOne({room: thisRoomId})
32
+                    .then((data) => {
33
+                        [fromX, fromY] = findFree(data);
34
+
35
+                        return db.rooms.count({$and: [{bus: true}, {status: 'normal'}]});
36
+                    }).then(count => {
37
+                        let roomCoords = parseRoomName(thisRoomId);
38
+                        let offset = Math.sin(roomCoords.xx + (roomCoords.yy * 30)).toString().substr(6) % count;
39
+
40
+                        return db.rooms.findEx({bus: true}, {skip: offset, limit: 1});
41
+                    }).then(otherRooms => {
42
+                        if (!otherRooms.length)
43
+                            throw new Error('No other rooms found ' + thisRoomId);
44
+
45
+                        otherRoom = otherRooms[0];
46
+                        return db['rooms.terrain'].findOne({room: otherRoom._id});
47
+                    }).then((data) => {
48
+                        [toX, toY] = findFree(data);
49
+
50
+                        let portalTime = Math.round(respawnTime * (2+Math.random())/3);
51
+
52
+                        return db["rooms.objects"].insert({
53
+                            room: thisRoomId,
54
+                            type: "portal",
55
+                            x: fromX,
56
+                            y: fromY,
57
+                            destination: {x: toX, y: toY, room: otherRoom._id},
58
+                            ticksToDecay: portalTime
59
+                        }).then(()=> db["rooms.objects"].insert({
60
+                            room: otherRoom._id,
61
+                            type: "portal",
62
+                            x: toX,
63
+                            y: toY,
64
+                            destination: {x: fromX, y: fromY, room: thisRoomId},
65
+                            ticksToDecay: portalTime
66
+                        })).then(() => db.rooms.update({_id: thisRoomId}, {$set: {active:true, busPortalTime: gameTime + respawnTime}}))
67
+                           .then(() => db.rooms.update({_id: otherRoom._id}, {$set: {active:true}}));
68
+                    });
69
+                }
70
+            })));
71
+        }];
72
+    }
73
+};
74
+
75
+function findFree(data) {
76
+    var x, y, isWall;
77
+    do {
78
+        x = Math.floor(Math.random() * 40 + 5);
79
+        y = Math.floor(Math.random() * 40 + 5);
80
+        isWall = parseInt(data.terrain.charAt(y * 50 + x)) & 1;
81
+    }
82
+    while (isWall);
83
+    return [x, y];
84
+}
85
+
86
+function parseRoomName(roomName) {
87
+    let room = /^([WE])([0-9]+)([NS])([0-9]+)$/.exec(roomName);
88
+    if (!room) {
89
+        throw new Error('Invalid room name ' + roomName);
90
+    }
91
+    let rx = (kWorldSize >> 1) + (room[1] === 'W' ? -Number(room[2]) : Number(room[2]) + 1);
92
+    let ry = (kWorldSize >> 1) + (room[3] === 'N' ? -Number(room[4]) : Number(room[4]) + 1);
93
+    if (!(rx >= 0 && rx <= kWorldSize && ry >= 0 && ry <= kWorldSize)) {
94
+        throw new Error('Invalid room name ' + roomName);
95
+    }
96
+    return {xx: rx, yy: ry};
97
+}
98
+

+ 11
- 0
package.json View File

@@ -0,0 +1,11 @@
1
+{
2
+  "name": "bus-portals",
3
+  "version": "1.0.0",
4
+  "description": "adds portals to bus rooms",
5
+  "main": "index.js",
6
+  "scripts": {
7
+    "test": "echo \"Error: no test specified\" && exit 1"
8
+  },
9
+  "author": "scitor <scitor@el337.de>",
10
+  "license": "ISC"
11
+}