-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathcamera.js
425 lines (337 loc) · 12.2 KB
/
camera.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
"use strict";
const { EventEmitter } = require("events");
const Jimp = require("jimp");
class Camera extends EventEmitter {
/**
* These represent the possible buttons that can be sent to the server.
*/
static Buttons = {
NONE: 0,
FORWARD: 2,
BACKWARD: 4,
LEFT: 8,
RIGHT: 16,
JUMP: 32,
DUCK: 64,
SPRINT: 128,
USE: 256,
FIRE_PRIMARY: 1024,
FIRE_SECONDARY: 2048,
RELOAD: 8192,
FIRE_THIRD: 134217728,
}
/**
* These represent the possible control flags that can be sent to the server.
* For example, Static CCTV cameras will not support movement.
*/
static ControlFlags = {
NONE: 0,
MOVEMENT: 1,
MOUSE: 2,
SPRINT_AND_DUCK: 4,
FIRE: 8,
RELOAD: 16,
CROSSHAIR: 32,
}
/**
* @param rustplus An existing RustPlus instance
* @param identifier Camera Identifier, such as OILRIG1 (or custom name)
*
* Events emitted by the Camera class instance
* - subscribing: When we are subscribing to the Camera.
* - subscribed: When we are subscribed to the Camera.
* - unsubscribing: When we are unsubscribing from the Camera.
* - unsubscribed: When we are unsubscribed from the Camera.
* - render: When a camera frame has been rendered. A png image buffer will be provided.
*/
constructor(rustplus, identifier) {
super();
this.rustplus = rustplus;
this.identifier = identifier;
this.isSubscribed = false;
this.cameraRays = [];
this.cameraSubscribeInfo = null;
this.subscribeInterval = null;
// listen to camera message broadcasts
this.rustplus.on('message', async (message) => {
await this._onMessage(message);
});
// unsubscribe when rustplus is disconnected (to prevent hanging due to intervals still running)
this.rustplus.on('disconnected', async () => {
if(this.isSubscribed){
await this.unsubscribe();
}
});
}
async _onMessage(message) {
// do nothing if not subscribed
if(!this.isSubscribed){
return;
}
if(message.broadcast && message.broadcast.cameraRays){
await this._onCameraRays(message.broadcast.cameraRays);
}
}
async _onCameraRays(cameraRays) {
// do nothing if not subscribed
if(!this.isSubscribed){
return;
}
// add new camera rays to cache
this.cameraRays.push(cameraRays);
// wait until we have enough camera rays to render an image
if(this.cameraRays.length > 10){
// remove first oldest rayData
this.cameraRays.shift();
// render to png
const frame = await this._renderCameraFrame(this.cameraRays, this.cameraSubscribeInfo.width, this.cameraSubscribeInfo.height);
// fire callback
await this._onRender(frame);
}
}
async _onRender(image) {
// do nothing if not subscribed
if(!this.isSubscribed){
return;
}
this.emit('render', image);
}
/**
* Render a camera frame to a PNG image buffer
* @param frames the frame data to render. This will be an array of camera rays from the server.
* @param width the width of the frame
* @param height the height of the frame
*/
async _renderCameraFrame(frames, width, height) {
// First we populate the samplePositionBuffer with the positions of each sample
const samplePositionBuffer = new Int16Array(width * height * 2);
for (let w = 0, _ = 0; _ < height; _++)
for (let g = 0; g < width; g++) {
samplePositionBuffer[w] = g;
samplePositionBuffer[++w] = _;
w++;
}
for (let B = new IndexGenerator(1337), R = width * height - 1; R >= 1; R--) {
let C = 2 * R,
I = 2 * B.nextInt(R + 1),
P = samplePositionBuffer[C],
k = samplePositionBuffer[C + 1],
A = samplePositionBuffer[I],
F = samplePositionBuffer[I + 1];
samplePositionBuffer[I] = P;
samplePositionBuffer[I + 1] = k;
samplePositionBuffer[C] = A;
samplePositionBuffer[C + 1] = F;
}
// Create the output buffer
const output = new Array(width * height);
// Loop through each frame
for (let frame of frames) {
// Reset some look back and pointer variables
let sampleOffset = 2 * frame.sampleOffset;
let dataPointer = 0;
let rayLookback = new Array(64);
for (let r = 0; r < 64; r++) rayLookback[r] = [0, 0, 0];
const rayData = frame.rayData;
// Loop through the ray data
while (true) {
if (dataPointer >= rayData.length - 1)
break;
// Get the first byte and set some variables
let t, r, i, n = rayData[dataPointer++];
// Ray Decoding Logic
if (255 === n) {
let l = rayData[dataPointer++],
o = rayData[dataPointer++],
s = rayData[dataPointer++],
u = (3 * (((t = (l << 2) | (o >> 6)) / 128) | 0) + 5 * (((r = 63 & o) / 16) | 0) + 7 * (i = s)) & 63,
f = rayLookback[u];
f[0] = t;
f[1] = r;
f[2] = i;
} else {
let c = 192 & n;
if (0 === c) {
let h = 63 & n, y = rayLookback[h];
t = y[0];
r = y[1];
i = y[2];
} else if (64 === c) {
let p = 63 & n,
v = rayLookback[p],
b = v[0],
w = v[1],
_ = v[2],
g = rayData[dataPointer++];
t = b + ((g >> 3) - 15);
r = w + ((7 & g) - 3);
i = _;
} else if (128 === c) {
let R = 63 & n,
C = rayLookback[R],
I = C[0],
P = C[1],
k = C[2];
t = I + (rayData[dataPointer++] - 127);
r = P;
i = k;
} else {
let A = rayData[dataPointer++],
F = rayData[dataPointer++],
D = (3 * (((t = (A << 2) | (F >> 6)) / 128) | 0) + 5 * (((r = 63 & F) / 16) | 0) + 7 * (i = 63 & n)) & 63,
E = rayLookback[D];
E[0] = t;
E[1] = r;
E[2] = i;
}
}
sampleOffset %= 2 * width * height;
const index = samplePositionBuffer[sampleOffset++] + samplePositionBuffer[sampleOffset++] * width;
output[index] = [t / 1023, r / 63, i];
}
}
const colours = [
[0.5, 0.5, 0.5], [0.8, 0.7, 0.7], [0.3, 0.7, 1], [0.6, 0.6, 0.6],
[0.7, 0.7, 0.7], [0.8, 0.6, 0.4], [1, 0.4, 0.4], [1, 0.1, 0.1],
];
const image = new Jimp(width, height);
for (let i = 0; i < output.length; i++) {
let ray = output[i];
if (!ray) {
continue;
}
let distance = ray[0]
let alignment = ray[1]
let material = ray[2]
let target_colour;
if (distance === 1 && alignment === 0 && material === 0) {
target_colour = [208, 230, 252];
} else {
let colour = colours[material];
target_colour = [(alignment * colour[0] * 255), (alignment * colour[1] * 255), (alignment * colour[2] * 255)]
}
let x = i % width;
let y = height - 1 - Math.floor(i / width);
image.setPixelColor(Jimp.rgbaToInt(target_colour[0], target_colour[1], target_colour[2], 255), x, y);
}
// return png buffer
return image.getBufferAsync(Jimp.MIME_PNG);
}
async _subscribe() {
// subscribe to camera
const response = await this.rustplus.sendRequestAsync({
cameraSubscribe: {
cameraId: this.identifier,
},
});
// update camera subscribe info
this.cameraSubscribeInfo = response.cameraSubscribeInfo;
this.isSubscribed = true;
}
async subscribe() {
this.emit('subscribing');
// subscribe to camera
await this._subscribe();
this.emit('subscribed');
// automatically resubscribe to the camera every 10 seconds
this.subscribeInterval = setInterval(async () => {
if(this.isSubscribed){
await this._subscribe();
}
}, 10_000);
}
async unsubscribe() {
this.emit('unsubscribing');
this.isSubscribed = false;
// stop automatically resubscribing
clearInterval(this.subscribeInterval);
// release memory
this.cameraRays = [];
this.cameraSubscribeInfo = null;
this.subscribeInterval = null;
// unsubscribe from camera on server (if connected)
if(this.rustplus.isConnected()){
try {
await this.rustplus.sendRequestAsync({
cameraUnsubscribe: {
},
});
} catch (error) {
// ignore errors unsubscribing from camera
}
}
this.emit('unsubscribed');
}
/**
* Sends camera movement to the server (mouse movement)
* @param buttons The buttons that are currently pressed
* @param x The x delta of the mouse movement
* @param y The y delta of the mouse movement
*/
async move(buttons, x, y) {
return await this.rustplus.sendRequestAsync({
cameraInput: {
buttons: buttons,
mouseDelta: {
x: x,
y: y,
}
},
});
}
/**
* Zooms a PTZ camera in by 1 level.
* PTZ cameras have 4 zoom levels.
* If the PTZ camera is already at max zoom (level 4), it zooms out as far as it can (level 1).
*/
async zoom() {
// press left mouse button to zoom in
await this.move(Camera.Buttons.FIRE_PRIMARY, 0, 0);
// release all mouse buttons
await this.move(Camera.Buttons.NONE, 0, 0);
}
/**
* Shoots a PTZ controllable Auto Turret.
*/
async shoot() {
// press left mouse button to shoot
await this.move(Camera.Buttons.FIRE_PRIMARY, 0, 0);
// release all mouse buttons
await this.move(Camera.Buttons.NONE, 0, 0);
}
/**
* Reloads a PTZ controllable Auto Turret
*/
async reload() {
// press reload button to reload turret
await this.move(Camera.Buttons.RELOAD, 0, 0);
// release all mouse buttons
await this.move(Camera.Buttons.NONE, 0, 0);
}
/**
* Check if camera is an auto turret
* @returns {boolean}
*/
isAutoTurret() {
const crosshairControlFlag = Camera.ControlFlags.CROSSHAIR;
return (this.cameraSubscribeInfo?.controlFlags & crosshairControlFlag) === crosshairControlFlag;
}
}
class IndexGenerator {
constructor(e) {
this.state = 0 | e;
this.nextState();
}
nextInt(e) {
let t = ((this.nextState() * (0 | e)) / 4294967295) | 0;
if (t < 0) t = e + t - 1;
return 0 | t;
}
nextState() {
let e = this.state, t = e;
e = ((e = ((e = (e ^ ((e << 13) | 0)) | 0) ^ ((e >>> 17) | 0)) | 0) ^ ((e << 5) | 0)) | 0;
this.state = e;
return t >= 0 ? t : 4294967295 + t - 1;
}
}
module.exports = Camera;