Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
titaniumnetwork-dev
GitHub Repository: titaniumnetwork-dev/Incognito-old
Path: blob/main/static/src/gs/public/microsoft-flight-simulator/dos.js
1325 views
1
let game = {};
2
let screen;
3
let ram;
4
let graphics = {};
5
let doStopLoop = true;
6
7
function getScancode(key) {
8
//alert(key);
9
switch (key) {
10
case "Escape":
11
return 0x01; // esc
12
case "F1":
13
return 0x3b;
14
case "F2":
15
return 0x3c;
16
case "F3":
17
return 0x3d;
18
case "F4":
19
return 0x3e;
20
case "F5":
21
return 0x3f;
22
case "F6":
23
return 0x40;
24
case "F7":
25
return 0x41;
26
case "F8":
27
return 0x42;
28
case "F9":
29
return 0x43;
30
case "F10":
31
return 0x44;
32
case "F11":
33
return 0x57;
34
case "F12":
35
return 0x58;
36
37
case "ScrollLock":
38
return 0x46;
39
case "Pause":
40
return -1; // not supported
41
42
case "Backquote":
43
return 0x29; // 1
44
case "Digit1":
45
return 0x02; // 1
46
case "Digit2":
47
return 0x03; // 2
48
case "Digit3":
49
return 0x04; // 3
50
case "Digit4":
51
return 0x05; // 4
52
case "Digit5":
53
return 0x06; // 5
54
case "Digit6":
55
return 0x07; // 6
56
case "Digit7":
57
return 0x08; // 7
58
case "Digit8":
59
return 0x09; // 8
60
case "Digit9":
61
return 0x0a; // 9
62
case "Digit0":
63
return 0x0b; // 0
64
case "KeyA":
65
return 0x1e; // a
66
case "KeyB":
67
return 0x30; // b
68
case "KeyC":
69
return 0x2e; // c
70
case "KeyD":
71
return 0x20; // d
72
case "KeyE":
73
return 0x12; // e
74
case "KeyF":
75
return 0x21; // f
76
case "KeyG":
77
return 0x22; // g
78
case "KeyH":
79
return 0x23; // h
80
case "KeyI":
81
return 0x17; // i
82
case "KeyJ":
83
return 0x24; // j
84
case "KeyK":
85
return 0x25; // k
86
case "KeyL":
87
return 0x26; // l
88
case "KeyM":
89
return 0x32; // m
90
case "KeyN":
91
return 0x31; // n
92
case "KeyO":
93
return 0x18; // o
94
case "KeyP":
95
return 0x19; // p
96
case "KeyQ":
97
return 0x10; // q
98
case "KeyR":
99
return 0x13; // r
100
case "KeyS":
101
return 0x1f; // s
102
case "KeyT":
103
return 0x14; // t
104
case "KeyU":
105
return 0x16; // u
106
case "KeyV":
107
return 0x2f; // v
108
case "KeyW":
109
return 0x11; // w
110
case "KeyX":
111
return 0x2d; // x
112
case "KeyY":
113
return 0x2c; // y
114
case "KeyZ":
115
return 0x15; // z
116
case "ShiftLeft":
117
return 0x2a; // left shift
118
case "ShiftRight":
119
return 0x36; // right shift
120
case "Space":
121
return 0x39; // space
122
case "Enter":
123
return 0x1c; // enter
124
case "Backspace":
125
return 0xe; // backspace
126
case "ControlLeft":
127
return 0x1d; // left ctrl
128
case "ControlRight":
129
return 0x1d; // right ctrl
130
case "ArrowUp":
131
return 0x48; // cursor up
132
case "ArrowDown":
133
return 0x50; // cursor down
134
case "ArrowLeft":
135
return 0x4b; // cursor left
136
case "ArrowRight":
137
return 0x4d; // cursor right
138
case "Insert":
139
return 0x52; // ins
140
case "Home":
141
return 0x47; // home
142
case "PageUp":
143
return 0x49; // pg up
144
case "Delete":
145
return 0x53; // del
146
case "End":
147
return 0x4f; // end
148
case "PageDown":
149
return 0x51; // pg down
150
case "NumLock":
151
return 0x45; // num lock
152
case "NumpadDivide":
153
return 0x35; // num pad divide
154
case "NumpadMultiply":
155
return 0x37; // num pad multiply
156
case "NumpadSubtract":
157
return 0x4a; // num pad subtract
158
case "NumpadAdd":
159
return 0x4e; // num pad add
160
case "NumpadEnter":
161
return 0x1c; // enter
162
case "Numpad0":
163
return 0x52; // 0
164
case "NumpadDecimal":
165
return 0x53; //
166
case "Numpad1":
167
return 0x4f; // 1
168
case "Numpad2":
169
return 0x50; // 2
170
case "Numpad3":
171
return 0x51; // 3
172
case "Numpad4":
173
return 0x4b; // 4
174
case "Numpad5":
175
return 0x4c; // 5
176
case "Numpad6":
177
return 0x4d; // 6
178
case "Numpad7":
179
return 0x47; // 7
180
case "Numpad8":
181
return 0x48; // 8
182
case "Numpad9":
183
return 0x49; // 9
184
case "Minus":
185
return 0x0c; // -
186
case "Equal":
187
return 0x0d; // =
188
case "BracketLeft":
189
return 0x1a; // [
190
case "BracketRight":
191
return 0xab; // ]
192
case "Semicolon":
193
return 0x27; // ;
194
case "Quote":
195
return 0x28; // '
196
case "Backslash":
197
return 0x2b; // \
198
case "Comma":
199
return 0x33; // ,
200
case "Period":
201
return 0x34; // .
202
case "Slash":
203
return 0x35; // /
204
case "IntlBackslash":
205
return 0x56; // <
206
case "ContextMenu":
207
return -1;
208
case "AltRight":
209
return 38;
210
}
211
console.log("Unknown key " + key);
212
return -1;
213
}
214
215
function DetectKeysDown(e) {
216
e.preventDefault();
217
//if (e.repeat) return;
218
let scancode = getScancode(e.code);
219
if (scancode === -1) return;
220
game.KeyDown(scancode);
221
}
222
223
function DetectKeysUp(e) {
224
//if (e.repeat) return;
225
e.preventDefault();
226
let scancode = getScancode(e.code);
227
if (scancode === -1) return;
228
game.KeyUp(scancode);
229
}
230
231
function GetMousePosition(e)
232
{
233
// fix for Chrome
234
if (e.type.startsWith('touch'))
235
{
236
return [e.targetTouches[0].pageX, e.targetTouches[0].pageY];
237
} else
238
{
239
return [e.pageX, e.pageY];
240
}
241
}
242
/*
243
function getMouseRelativeToCanvas(canvas, position) {
244
let rect = canvas.getBoundingClientRect();
245
return {
246
x: evt.clientX - rect.left,
247
y: evt.clientY - rect.top
248
};
249
}
250
*/
251
252
function getMouseRelativeToCanvas(canvas, position) {
253
let rect = canvas.getBoundingClientRect();
254
return {
255
x: (position[0] - rect.left) / rect.width,
256
y: (position[1] - rect.top) / rect.height
257
};
258
}
259
260
function DetectMouseMove(e) {
261
e.preventDefault();
262
let currentMousePosition = GetMousePosition(e);
263
let normalizedPosition = getMouseRelativeToCanvas(graphics.scaledcanvas, currentMousePosition);
264
game.MouseMotion(normalizedPosition.x*graphics.canvas.width, normalizedPosition.y*graphics.canvas.height);
265
}
266
267
function DetectMouseUp(e) {
268
game.MouseButtonUp(1);
269
}
270
271
function DetectMouseDown(e) {
272
game.MouseButtonDown(1);
273
}
274
275
function ReinitGraphics() {
276
graphics.canvas = document.createElement('canvas');
277
graphics.mode = game.VGA_GetVideoMode();
278
switch(graphics.mode) {
279
case 0x0:
280
case 0x2:
281
case 0x3:
282
case 0x6:
283
graphics.canvas.width = 640;
284
graphics.canvas.height = 200;
285
break;
286
287
case 0x1:
288
graphics.canvas.width = 320;
289
graphics.canvas.height = 400;
290
break;
291
case 0x7:
292
case 0x14:
293
graphics.canvas.width = 720;
294
graphics.canvas.height = 400;
295
break;
296
297
case 0x4:
298
case 0xd:
299
case 0x13:
300
graphics.canvas.width = 320;
301
graphics.canvas.height = 200;
302
break;
303
case 0x10:
304
graphics.canvas.width = 640;
305
graphics.canvas.height = 350;
306
break;
307
308
default:
309
graphics.canvas.width = 640;
310
graphics.canvas.height = 480;
311
break;
312
}
313
314
graphics.ctx = graphics.canvas.getContext("2d");
315
graphics.data = graphics.ctx.createImageData(graphics.canvas.width, graphics.canvas.height);
316
}
317
318
function resizeCanvas() {
319
//graphics.scaledcanvas.style.height = window.innerHeight + "px";
320
//graphics.scaledcanvas.style.width = window.innerWidth + "px";
321
322
let viewwidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
323
let viewheight = window.innerHeight|| document.documentElement.clientHeight || document.body.clientHeight;
324
325
viewwidth *= 3./4.; // grid fraction
326
let ratio = viewwidth/viewheight;
327
if (ratio > 4./3.) {
328
graphics.scaledcanvas.style.width = "auto";
329
graphics.scaledcanvas.style.height = "100%";
330
} else {
331
graphics.scaledcanvas.style.width = "100%";
332
graphics.scaledcanvas.style.height = "auto";
333
}
334
335
/*
336
337
let height = graphics.scaledcanvas.innerWidth * 0.75;
338
if (viewheight < height) {
339
alert("be careful");
340
graphics.scaledcanvas.style.height = window.innerHeight + "px";
341
graphics.scaledcanvas.style.width = ((window.innerHeight*4./3.)|0) + "px";
342
} else {
343
graphics.scaledcanvas.style.width = "100%";
344
graphics.scaledcanvas.style.height = "auto";
345
}
346
*/
347
}
348
349
function Init() {
350
graphics.scaledcanvas = document.getElementById("screen");
351
graphics.scaledcanvas.width = 640;
352
graphics.scaledcanvas.height = 480;
353
graphics.scaledctx = graphics.scaledcanvas.getContext("2d");
354
window.addEventListener('resize', resizeCanvas, false);
355
resizeCanvas();
356
357
ReinitGraphics();
358
window.onkeydown = DetectKeysDown;
359
window.onkeyup = DetectKeysUp;
360
graphics.scaledcanvas.onmousemove = DetectMouseMove;
361
graphics.scaledcanvas.onmousedown = DetectMouseDown;
362
graphics.scaledcanvas.onmouseup = DetectMouseUp;
363
window.ontouchstart = DetectMouseDown;
364
window.ontouchend = DetectMouseUp;
365
window.ontouchmove = DetectMouseMove;
366
}
367
368
let lastDate = null;
369
// returns the time since the last call
370
function getDeltaMs() {
371
if (lastDate === null) {
372
lastDate = new Date();
373
return 0;
374
}
375
let lastTime = lastDate.getTime();
376
lastDate = new Date();
377
return lastDate.getTime() - lastTime;
378
}
379
380
function Loop() {
381
if (doStopLoop) return;
382
383
let delta = getDeltaMs();
384
if (delta > 60) delta = 60; // just make sure the browser is not locked forever
385
game.Run(delta*5000);
386
//game.Run(500000);
387
game.UpdateScreen();
388
389
if (graphics.mode != game.VGA_GetVideoMode()) ReinitGraphics();
390
let width = graphics.canvas.width;
391
let height = graphics.canvas.height;
392
for (let j = 0; j < height; j++) {
393
let offsets = j * 720 * 4;
394
let offsetg = j * width * 4;
395
for (let i = 0; i < width * 4; i++) {
396
graphics.data.data[offsetg++] = screen[offsets++];
397
}
398
}
399
graphics.ctx.putImageData(graphics.data, 0, 0);
400
graphics.scaledctx.drawImage(
401
graphics.canvas,
402
0, 0, graphics.canvas.width, graphics.canvas.height,
403
0, 0, graphics.scaledcanvas.width, graphics.scaledcanvas.height);
404
405
window.requestAnimationFrame(Loop);
406
//window.setTimeout(Loop, 0);
407
}
408
409
let str = "";
410
411
function Stdout(strp) {
412
for (let i = 0; i < 256; i++) {
413
if (ram[strp + i] === 0) break;
414
if (ram[strp + i] < 0x20) {
415
if (ram[strp + i] === 0x0A) {
416
console.log(str);
417
str = "";
418
continue;
419
}
420
}
421
str += String.fromCharCode(ram[strp + i]);
422
}
423
}
424
425
function MountFs(fsAsByteArray) {
426
427
let filesystem = new Uint8Array(fsAsByteArray);
428
let uncompressed = [];
429
console.log("decompress filesystem of size " + fsAsByteArray.byteLength);
430
bzip2.simple(filesystem, function(byte){uncompressed.push(byte)});
431
console.log("decompressed filesystem of size " + uncompressed.length);
432
let fsaddress = game.GetMountStorage(uncompressed.length);
433
for(let i=0;i<uncompressed.length;i++) ram[fsaddress+i] = uncompressed[i];
434
game.FinishMountStorage();
435
}
436
437
function Main(version) {
438
doStopLoop = true;
439
let importOb = {
440
env: {
441
memory: new WebAssembly.Memory({initial: 512, maximum: 512}), // 8 MB
442
exit: function (code) {
443
console.log("Exit code " + code);
444
console.log("Restart");
445
Main(version);
446
},
447
outputstr: Stdout
448
}
449
};
450
451
Promise
452
.all([fetch('fshistory.wasm'), fetch('data/fs'+version+'.fs.bz2')])
453
.then(responses => Promise.all([responses[0].arrayBuffer(), responses[1].arrayBuffer()]))
454
.then(data => Promise.all([WebAssembly.instantiate(data[0], importOb), Promise.resolve(data[1])])
455
).then(
456
objs => {
457
console.log("wasm file loaded");
458
game = objs[0].instance.exports;
459
ram = new Uint8Array(importOb.env.memory.buffer, 0);
460
game.Init();
461
MountFs(objs[1]);
462
463
game.SetFSVersion(version);
464
screen = new Uint8Array(importOb.env.memory.buffer, game.ScreenGet());
465
Init();
466
console.log("Initialization completed. Starting Loop");
467
doStopLoop = false;
468
Loop();
469
//alert(game.__heap_base.value);
470
//alert(game.__global_base.value);
471
},
472
err => alert(err)
473
);
474
}
475
476