quake2ts 0.0.288 → 0.0.290
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/packages/client/dist/browser/index.global.js +13 -13
- package/packages/client/dist/browser/index.global.js.map +1 -1
- package/packages/client/dist/cjs/index.cjs +554 -2
- package/packages/client/dist/cjs/index.cjs.map +1 -1
- package/packages/client/dist/esm/index.js +554 -2
- package/packages/client/dist/esm/index.js.map +1 -1
- package/packages/client/dist/tsconfig.tsbuildinfo +1 -1
- package/packages/engine/dist/browser/index.global.js +16 -16
- package/packages/engine/dist/browser/index.global.js.map +1 -1
- package/packages/engine/dist/cjs/index.cjs +360 -35
- package/packages/engine/dist/cjs/index.cjs.map +1 -1
- package/packages/engine/dist/esm/index.js +360 -35
- package/packages/engine/dist/esm/index.js.map +1 -1
- package/packages/engine/dist/tsconfig.tsbuildinfo +1 -1
- package/packages/engine/dist/types/audio/constants.d.ts +1 -23
- package/packages/engine/dist/types/audio/constants.d.ts.map +1 -1
- package/packages/game/dist/browser/index.global.js +3 -3
- package/packages/game/dist/browser/index.global.js.map +1 -1
- package/packages/game/dist/cjs/index.cjs +556 -19
- package/packages/game/dist/cjs/index.cjs.map +1 -1
- package/packages/game/dist/esm/index.js +556 -19
- package/packages/game/dist/esm/index.js.map +1 -1
- package/packages/game/dist/tsconfig.tsbuildinfo +1 -1
- package/packages/game/dist/types/entities/monsters/actor.d.ts.map +1 -1
- package/packages/game/dist/types/entities/monsters/arachnid.d.ts.map +1 -1
- package/packages/game/dist/types/entities/system.d.ts +1 -0
- package/packages/game/dist/types/entities/system.d.ts.map +1 -1
- package/packages/game/dist/types/index.d.ts +2 -0
- package/packages/game/dist/types/index.d.ts.map +1 -1
- package/packages/server/dist/index.cjs +54 -11
- package/packages/server/dist/index.d.cts +6 -1
- package/packages/server/dist/index.d.ts +6 -1
- package/packages/server/dist/index.js +56 -13
- package/packages/shared/dist/browser/index.global.js +1 -1
- package/packages/shared/dist/browser/index.global.js.map +1 -1
- package/packages/shared/dist/cjs/index.cjs +562 -0
- package/packages/shared/dist/cjs/index.cjs.map +1 -1
- package/packages/shared/dist/esm/index.js +549 -0
- package/packages/shared/dist/esm/index.js.map +1 -1
- package/packages/shared/dist/tsconfig.tsbuildinfo +1 -1
- package/packages/shared/dist/types/audio/constants.d.ts +24 -0
- package/packages/shared/dist/types/audio/constants.d.ts.map +1 -0
- package/packages/shared/dist/types/index.d.ts +1 -0
- package/packages/shared/dist/types/index.d.ts.map +1 -1
- package/packages/shared/dist/types/net/index.d.ts +1 -0
- package/packages/shared/dist/types/net/index.d.ts.map +1 -1
- package/packages/shared/dist/types/protocol/crc.d.ts +5 -0
- package/packages/shared/dist/types/protocol/crc.d.ts.map +1 -0
- package/packages/shared/dist/types/protocol/index.d.ts +1 -0
- package/packages/shared/dist/types/protocol/index.d.ts.map +1 -1
- package/packages/tools/dist/tsconfig.tsbuildinfo +1 -1
|
@@ -2329,6 +2329,333 @@ var BinaryStream = class {
|
|
|
2329
2329
|
out.z = norm[2];
|
|
2330
2330
|
}
|
|
2331
2331
|
};
|
|
2332
|
+
var BinaryWriter = class {
|
|
2333
|
+
constructor(sizeOrBuffer = 1400) {
|
|
2334
|
+
if (typeof sizeOrBuffer === "number") {
|
|
2335
|
+
this.buffer = new Uint8Array(sizeOrBuffer);
|
|
2336
|
+
this.fixed = false;
|
|
2337
|
+
} else {
|
|
2338
|
+
this.buffer = sizeOrBuffer;
|
|
2339
|
+
this.fixed = true;
|
|
2340
|
+
}
|
|
2341
|
+
this.view = new DataView(this.buffer.buffer, this.buffer.byteOffset, this.buffer.byteLength);
|
|
2342
|
+
this.offset = 0;
|
|
2343
|
+
}
|
|
2344
|
+
ensureSpace(bytes) {
|
|
2345
|
+
if (this.offset + bytes > this.buffer.byteLength) {
|
|
2346
|
+
if (this.fixed) {
|
|
2347
|
+
throw new Error(`Buffer overflow: capacity ${this.buffer.byteLength}, needed ${this.offset + bytes}`);
|
|
2348
|
+
}
|
|
2349
|
+
const newSize = Math.max(this.buffer.byteLength * 2, this.offset + bytes);
|
|
2350
|
+
const newBuffer = new Uint8Array(newSize);
|
|
2351
|
+
newBuffer.set(this.buffer);
|
|
2352
|
+
this.buffer = newBuffer;
|
|
2353
|
+
this.view = new DataView(this.buffer.buffer, this.buffer.byteOffset, this.buffer.byteLength);
|
|
2354
|
+
}
|
|
2355
|
+
}
|
|
2356
|
+
writeByte(value) {
|
|
2357
|
+
this.ensureSpace(1);
|
|
2358
|
+
this.view.setUint8(this.offset, value);
|
|
2359
|
+
this.offset += 1;
|
|
2360
|
+
}
|
|
2361
|
+
writeChar(value) {
|
|
2362
|
+
this.ensureSpace(1);
|
|
2363
|
+
this.view.setInt8(this.offset, value);
|
|
2364
|
+
this.offset += 1;
|
|
2365
|
+
}
|
|
2366
|
+
writeShort(value) {
|
|
2367
|
+
this.ensureSpace(2);
|
|
2368
|
+
this.view.setInt16(this.offset, value, true);
|
|
2369
|
+
this.offset += 2;
|
|
2370
|
+
}
|
|
2371
|
+
writeLong(value) {
|
|
2372
|
+
this.ensureSpace(4);
|
|
2373
|
+
this.view.setInt32(this.offset, value, true);
|
|
2374
|
+
this.offset += 4;
|
|
2375
|
+
}
|
|
2376
|
+
writeFloat(value) {
|
|
2377
|
+
this.ensureSpace(4);
|
|
2378
|
+
this.view.setFloat32(this.offset, value, true);
|
|
2379
|
+
this.offset += 4;
|
|
2380
|
+
}
|
|
2381
|
+
writeString(value) {
|
|
2382
|
+
const len2 = value.length;
|
|
2383
|
+
this.ensureSpace(len2 + 1);
|
|
2384
|
+
for (let i = 0; i < len2; i++) {
|
|
2385
|
+
this.view.setUint8(this.offset + i, value.charCodeAt(i));
|
|
2386
|
+
}
|
|
2387
|
+
this.view.setUint8(this.offset + len2, 0);
|
|
2388
|
+
this.offset += len2 + 1;
|
|
2389
|
+
}
|
|
2390
|
+
writeCoord(value) {
|
|
2391
|
+
this.writeShort(Math.trunc(value * 8));
|
|
2392
|
+
}
|
|
2393
|
+
writeAngle(value) {
|
|
2394
|
+
this.writeByte(Math.trunc(value * 256 / 360) & 255);
|
|
2395
|
+
}
|
|
2396
|
+
writeAngle16(value) {
|
|
2397
|
+
this.writeShort(Math.trunc(value * 65536 / 360) & 65535);
|
|
2398
|
+
}
|
|
2399
|
+
writePos(pos) {
|
|
2400
|
+
this.writeCoord(pos.x);
|
|
2401
|
+
this.writeCoord(pos.y);
|
|
2402
|
+
this.writeCoord(pos.z);
|
|
2403
|
+
}
|
|
2404
|
+
writeDir(dir) {
|
|
2405
|
+
let maxDot = -1;
|
|
2406
|
+
let bestIndex = 0;
|
|
2407
|
+
if (dir.x === 0 && dir.y === 0 && dir.z === 0) {
|
|
2408
|
+
this.writeByte(0);
|
|
2409
|
+
return;
|
|
2410
|
+
}
|
|
2411
|
+
for (let i = 0; i < ANORMS.length; i++) {
|
|
2412
|
+
const norm = ANORMS[i];
|
|
2413
|
+
const dot2 = dir.x * norm[0] + dir.y * norm[1] + dir.z * norm[2];
|
|
2414
|
+
if (dot2 > maxDot) {
|
|
2415
|
+
maxDot = dot2;
|
|
2416
|
+
bestIndex = i;
|
|
2417
|
+
}
|
|
2418
|
+
}
|
|
2419
|
+
this.writeByte(bestIndex);
|
|
2420
|
+
}
|
|
2421
|
+
getData() {
|
|
2422
|
+
return this.buffer.slice(0, this.offset);
|
|
2423
|
+
}
|
|
2424
|
+
getBuffer() {
|
|
2425
|
+
return this.buffer;
|
|
2426
|
+
}
|
|
2427
|
+
getOffset() {
|
|
2428
|
+
return this.offset;
|
|
2429
|
+
}
|
|
2430
|
+
reset() {
|
|
2431
|
+
this.offset = 0;
|
|
2432
|
+
}
|
|
2433
|
+
};
|
|
2434
|
+
var _NetChan = class _NetChan2 {
|
|
2435
|
+
constructor() {
|
|
2436
|
+
this.qport = 0;
|
|
2437
|
+
this.incomingSequence = 0;
|
|
2438
|
+
this.outgoingSequence = 0;
|
|
2439
|
+
this.incomingAcknowledged = 0;
|
|
2440
|
+
this.incomingReliableAcknowledged = false;
|
|
2441
|
+
this.incomingReliableSequence = 0;
|
|
2442
|
+
this.outgoingReliableSequence = 0;
|
|
2443
|
+
this.reliableLength = 0;
|
|
2444
|
+
this.lastReceived = 0;
|
|
2445
|
+
this.lastSent = 0;
|
|
2446
|
+
this.remoteAddress = null;
|
|
2447
|
+
this.reliableMessage = new BinaryWriter(_NetChan2.MAX_MSGLEN);
|
|
2448
|
+
const now = Date.now();
|
|
2449
|
+
this.lastReceived = now;
|
|
2450
|
+
this.lastSent = now;
|
|
2451
|
+
this.qport = Math.floor(Math.random() * 65536);
|
|
2452
|
+
}
|
|
2453
|
+
/**
|
|
2454
|
+
* Setup the netchan with specific settings
|
|
2455
|
+
*/
|
|
2456
|
+
setup(qport, address = null) {
|
|
2457
|
+
this.qport = qport;
|
|
2458
|
+
this.remoteAddress = address;
|
|
2459
|
+
this.reset();
|
|
2460
|
+
}
|
|
2461
|
+
/**
|
|
2462
|
+
* Reset the netchan state
|
|
2463
|
+
*/
|
|
2464
|
+
reset() {
|
|
2465
|
+
this.incomingSequence = 0;
|
|
2466
|
+
this.outgoingSequence = 0;
|
|
2467
|
+
this.incomingAcknowledged = 0;
|
|
2468
|
+
this.incomingReliableAcknowledged = false;
|
|
2469
|
+
this.incomingReliableSequence = 0;
|
|
2470
|
+
this.outgoingReliableSequence = 0;
|
|
2471
|
+
this.reliableLength = 0;
|
|
2472
|
+
this.reliableMessage.reset();
|
|
2473
|
+
this.lastReceived = Date.now();
|
|
2474
|
+
this.lastSent = Date.now();
|
|
2475
|
+
}
|
|
2476
|
+
/**
|
|
2477
|
+
* Transmits a packet containing reliable and unreliable data
|
|
2478
|
+
*/
|
|
2479
|
+
transmit(unreliableData) {
|
|
2480
|
+
this.outgoingSequence++;
|
|
2481
|
+
this.lastSent = Date.now();
|
|
2482
|
+
const headerSize = _NetChan2.PACKET_HEADER;
|
|
2483
|
+
const reliableSize = this.reliableLength > 0 ? this.reliableLength + 2 : 0;
|
|
2484
|
+
let unreliableSize = unreliableData ? unreliableData.length : 0;
|
|
2485
|
+
if (headerSize + reliableSize + unreliableSize > _NetChan2.MAX_MSGLEN) {
|
|
2486
|
+
unreliableSize = _NetChan2.MAX_MSGLEN - headerSize - reliableSize;
|
|
2487
|
+
if (unreliableSize < 0) unreliableSize = 0;
|
|
2488
|
+
}
|
|
2489
|
+
const buffer = new ArrayBuffer(headerSize + reliableSize + unreliableSize);
|
|
2490
|
+
const view = new DataView(buffer);
|
|
2491
|
+
const result = new Uint8Array(buffer);
|
|
2492
|
+
let sequence = this.outgoingSequence;
|
|
2493
|
+
if (this.reliableLength > 0) {
|
|
2494
|
+
sequence |= 2147483648;
|
|
2495
|
+
if ((this.outgoingReliableSequence & 1) !== 0) {
|
|
2496
|
+
sequence |= 1073741824;
|
|
2497
|
+
}
|
|
2498
|
+
}
|
|
2499
|
+
view.setUint32(0, sequence, true);
|
|
2500
|
+
let ack = this.incomingSequence;
|
|
2501
|
+
if ((this.incomingReliableSequence & 1) !== 0) {
|
|
2502
|
+
ack |= 2147483648;
|
|
2503
|
+
}
|
|
2504
|
+
view.setUint32(4, ack, true);
|
|
2505
|
+
view.setUint16(8, this.qport, true);
|
|
2506
|
+
let offset = headerSize;
|
|
2507
|
+
if (this.reliableLength > 0) {
|
|
2508
|
+
view.setUint16(offset, this.reliableLength, true);
|
|
2509
|
+
offset += 2;
|
|
2510
|
+
const reliableBuffer = this.reliableMessage.getBuffer();
|
|
2511
|
+
const reliableBytes = reliableBuffer.subarray(0, this.reliableLength);
|
|
2512
|
+
result.set(reliableBytes, offset);
|
|
2513
|
+
offset += this.reliableLength;
|
|
2514
|
+
}
|
|
2515
|
+
if (unreliableData && unreliableSize > 0) {
|
|
2516
|
+
const chunk = unreliableData.slice(0, unreliableSize);
|
|
2517
|
+
result.set(chunk, offset);
|
|
2518
|
+
}
|
|
2519
|
+
return result;
|
|
2520
|
+
}
|
|
2521
|
+
/**
|
|
2522
|
+
* Processes a received packet
|
|
2523
|
+
* Returns the payload data (reliable + unreliable) to be processed, or null if discarded
|
|
2524
|
+
*/
|
|
2525
|
+
process(packet) {
|
|
2526
|
+
if (packet.length < _NetChan2.PACKET_HEADER) {
|
|
2527
|
+
return null;
|
|
2528
|
+
}
|
|
2529
|
+
this.lastReceived = Date.now();
|
|
2530
|
+
const view = new DataView(packet.buffer, packet.byteOffset, packet.byteLength);
|
|
2531
|
+
const sequence = view.getUint32(0, true);
|
|
2532
|
+
const ack = view.getUint32(4, true);
|
|
2533
|
+
const qport = view.getUint16(8, true);
|
|
2534
|
+
if (this.qport !== qport) {
|
|
2535
|
+
return null;
|
|
2536
|
+
}
|
|
2537
|
+
const seqNumberClean = sequence & ~(2147483648 | 1073741824);
|
|
2538
|
+
if ((seqNumberClean - this.incomingSequence | 0) <= 0) {
|
|
2539
|
+
return null;
|
|
2540
|
+
}
|
|
2541
|
+
this.incomingSequence = seqNumberClean;
|
|
2542
|
+
const ackNumber = ack & ~2147483648;
|
|
2543
|
+
const ackReliable = (ack & 2147483648) !== 0;
|
|
2544
|
+
if (ackNumber > this.incomingAcknowledged) {
|
|
2545
|
+
this.incomingAcknowledged = ackNumber;
|
|
2546
|
+
}
|
|
2547
|
+
if (this.reliableLength > 0) {
|
|
2548
|
+
const receivedAckBit = ackReliable ? 1 : 0;
|
|
2549
|
+
const currentReliableBit = this.outgoingReliableSequence & 1;
|
|
2550
|
+
if (receivedAckBit !== currentReliableBit) {
|
|
2551
|
+
this.reliableLength = 0;
|
|
2552
|
+
this.reliableMessage.reset();
|
|
2553
|
+
this.outgoingReliableSequence ^= 1;
|
|
2554
|
+
}
|
|
2555
|
+
}
|
|
2556
|
+
const hasReliableData = (sequence & 2147483648) !== 0;
|
|
2557
|
+
const reliableSeqBit = (sequence & 1073741824) !== 0 ? 1 : 0;
|
|
2558
|
+
let payloadOffset = _NetChan2.PACKET_HEADER;
|
|
2559
|
+
let reliableData = null;
|
|
2560
|
+
if (hasReliableData) {
|
|
2561
|
+
if (payloadOffset + 2 > packet.byteLength) return null;
|
|
2562
|
+
const reliableLen = view.getUint16(payloadOffset, true);
|
|
2563
|
+
payloadOffset += 2;
|
|
2564
|
+
const expectedBit = this.incomingReliableSequence & 1;
|
|
2565
|
+
if (reliableSeqBit === expectedBit) {
|
|
2566
|
+
this.incomingReliableSequence++;
|
|
2567
|
+
if (payloadOffset + reliableLen > packet.byteLength) return null;
|
|
2568
|
+
reliableData = packet.slice(payloadOffset, payloadOffset + reliableLen);
|
|
2569
|
+
}
|
|
2570
|
+
payloadOffset += reliableLen;
|
|
2571
|
+
}
|
|
2572
|
+
const unreliableData = packet.slice(payloadOffset);
|
|
2573
|
+
if (reliableData && reliableData.length > 0) {
|
|
2574
|
+
const totalLen = reliableData.length + unreliableData.length;
|
|
2575
|
+
const result = new Uint8Array(totalLen);
|
|
2576
|
+
result.set(reliableData, 0);
|
|
2577
|
+
result.set(unreliableData, reliableData.length);
|
|
2578
|
+
return result;
|
|
2579
|
+
}
|
|
2580
|
+
if (unreliableData) {
|
|
2581
|
+
return unreliableData;
|
|
2582
|
+
}
|
|
2583
|
+
return new Uint8Array(0);
|
|
2584
|
+
}
|
|
2585
|
+
/**
|
|
2586
|
+
* Checks if reliable message buffer is empty and ready for new data
|
|
2587
|
+
*/
|
|
2588
|
+
canSendReliable() {
|
|
2589
|
+
return this.reliableLength === 0;
|
|
2590
|
+
}
|
|
2591
|
+
/**
|
|
2592
|
+
* Writes a byte to the reliable message buffer
|
|
2593
|
+
*/
|
|
2594
|
+
writeReliableByte(value) {
|
|
2595
|
+
if (this.reliableLength + 1 > _NetChan2.MAX_MSGLEN - _NetChan2.HEADER_OVERHEAD) {
|
|
2596
|
+
throw new Error("NetChan reliable buffer overflow");
|
|
2597
|
+
}
|
|
2598
|
+
this.reliableMessage.writeByte(value);
|
|
2599
|
+
this.reliableLength++;
|
|
2600
|
+
}
|
|
2601
|
+
/**
|
|
2602
|
+
* Writes a short to the reliable message buffer
|
|
2603
|
+
*/
|
|
2604
|
+
writeReliableShort(value) {
|
|
2605
|
+
if (this.reliableLength + 2 > _NetChan2.MAX_MSGLEN - _NetChan2.HEADER_OVERHEAD) {
|
|
2606
|
+
throw new Error("NetChan reliable buffer overflow");
|
|
2607
|
+
}
|
|
2608
|
+
this.reliableMessage.writeShort(value);
|
|
2609
|
+
this.reliableLength += 2;
|
|
2610
|
+
}
|
|
2611
|
+
/**
|
|
2612
|
+
* Writes a long to the reliable message buffer
|
|
2613
|
+
*/
|
|
2614
|
+
writeReliableLong(value) {
|
|
2615
|
+
if (this.reliableLength + 4 > _NetChan2.MAX_MSGLEN - _NetChan2.HEADER_OVERHEAD) {
|
|
2616
|
+
throw new Error("NetChan reliable buffer overflow");
|
|
2617
|
+
}
|
|
2618
|
+
this.reliableMessage.writeLong(value);
|
|
2619
|
+
this.reliableLength += 4;
|
|
2620
|
+
}
|
|
2621
|
+
/**
|
|
2622
|
+
* Writes a string to the reliable message buffer
|
|
2623
|
+
*/
|
|
2624
|
+
writeReliableString(value) {
|
|
2625
|
+
const len2 = value.length + 1;
|
|
2626
|
+
if (this.reliableLength + len2 > _NetChan2.MAX_MSGLEN - _NetChan2.HEADER_OVERHEAD) {
|
|
2627
|
+
throw new Error("NetChan reliable buffer overflow");
|
|
2628
|
+
}
|
|
2629
|
+
this.reliableMessage.writeString(value);
|
|
2630
|
+
this.reliableLength += len2;
|
|
2631
|
+
}
|
|
2632
|
+
/**
|
|
2633
|
+
* Returns the current reliable data buffer
|
|
2634
|
+
*/
|
|
2635
|
+
getReliableData() {
|
|
2636
|
+
if (this.reliableLength === 0) {
|
|
2637
|
+
return new Uint8Array(0);
|
|
2638
|
+
}
|
|
2639
|
+
const buffer = this.reliableMessage.getBuffer();
|
|
2640
|
+
return buffer.subarray(0, this.reliableLength);
|
|
2641
|
+
}
|
|
2642
|
+
/**
|
|
2643
|
+
* Checks if we need to send a keepalive packet
|
|
2644
|
+
*/
|
|
2645
|
+
needsKeepalive(currentTime) {
|
|
2646
|
+
return currentTime - this.lastSent > 1e3;
|
|
2647
|
+
}
|
|
2648
|
+
/**
|
|
2649
|
+
* Checks if the connection has timed out
|
|
2650
|
+
*/
|
|
2651
|
+
isTimedOut(currentTime, timeoutMs = 3e4) {
|
|
2652
|
+
return currentTime - this.lastReceived > timeoutMs;
|
|
2653
|
+
}
|
|
2654
|
+
};
|
|
2655
|
+
_NetChan.MAX_MSGLEN = 1400;
|
|
2656
|
+
_NetChan.FRAGMENT_SIZE = 1024;
|
|
2657
|
+
_NetChan.PACKET_HEADER = 10;
|
|
2658
|
+
_NetChan.HEADER_OVERHEAD = _NetChan.PACKET_HEADER + 2;
|
|
2332
2659
|
var AmmoType = /* @__PURE__ */ ((AmmoType22) => {
|
|
2333
2660
|
AmmoType22[AmmoType22["Bullets"] = 0] = "Bullets";
|
|
2334
2661
|
AmmoType22[AmmoType22["Shells"] = 1] = "Shells";
|
|
@@ -8859,7 +9186,7 @@ var BinaryStream2 = class {
|
|
|
8859
9186
|
out.z = norm[2];
|
|
8860
9187
|
}
|
|
8861
9188
|
};
|
|
8862
|
-
var
|
|
9189
|
+
var BinaryWriter2 = class {
|
|
8863
9190
|
constructor(sizeOrBuffer = 1400) {
|
|
8864
9191
|
if (typeof sizeOrBuffer === "number") {
|
|
8865
9192
|
this.buffer = new Uint8Array(sizeOrBuffer);
|
|
@@ -9051,6 +9378,231 @@ var NetworkMessageBuilder = class {
|
|
|
9051
9378
|
this.writeByte(best);
|
|
9052
9379
|
}
|
|
9053
9380
|
};
|
|
9381
|
+
var _NetChan3 = class _NetChan4 {
|
|
9382
|
+
constructor() {
|
|
9383
|
+
this.qport = 0;
|
|
9384
|
+
this.incomingSequence = 0;
|
|
9385
|
+
this.outgoingSequence = 0;
|
|
9386
|
+
this.incomingAcknowledged = 0;
|
|
9387
|
+
this.incomingReliableAcknowledged = false;
|
|
9388
|
+
this.incomingReliableSequence = 0;
|
|
9389
|
+
this.outgoingReliableSequence = 0;
|
|
9390
|
+
this.reliableLength = 0;
|
|
9391
|
+
this.lastReceived = 0;
|
|
9392
|
+
this.lastSent = 0;
|
|
9393
|
+
this.remoteAddress = null;
|
|
9394
|
+
this.reliableMessage = new BinaryWriter2(_NetChan4.MAX_MSGLEN);
|
|
9395
|
+
const now = Date.now();
|
|
9396
|
+
this.lastReceived = now;
|
|
9397
|
+
this.lastSent = now;
|
|
9398
|
+
this.qport = Math.floor(Math.random() * 65536);
|
|
9399
|
+
}
|
|
9400
|
+
/**
|
|
9401
|
+
* Setup the netchan with specific settings
|
|
9402
|
+
*/
|
|
9403
|
+
setup(qport, address = null) {
|
|
9404
|
+
this.qport = qport;
|
|
9405
|
+
this.remoteAddress = address;
|
|
9406
|
+
this.reset();
|
|
9407
|
+
}
|
|
9408
|
+
/**
|
|
9409
|
+
* Reset the netchan state
|
|
9410
|
+
*/
|
|
9411
|
+
reset() {
|
|
9412
|
+
this.incomingSequence = 0;
|
|
9413
|
+
this.outgoingSequence = 0;
|
|
9414
|
+
this.incomingAcknowledged = 0;
|
|
9415
|
+
this.incomingReliableAcknowledged = false;
|
|
9416
|
+
this.incomingReliableSequence = 0;
|
|
9417
|
+
this.outgoingReliableSequence = 0;
|
|
9418
|
+
this.reliableLength = 0;
|
|
9419
|
+
this.reliableMessage.reset();
|
|
9420
|
+
this.lastReceived = Date.now();
|
|
9421
|
+
this.lastSent = Date.now();
|
|
9422
|
+
}
|
|
9423
|
+
/**
|
|
9424
|
+
* Transmits a packet containing reliable and unreliable data
|
|
9425
|
+
*/
|
|
9426
|
+
transmit(unreliableData) {
|
|
9427
|
+
this.outgoingSequence++;
|
|
9428
|
+
this.lastSent = Date.now();
|
|
9429
|
+
const headerSize = _NetChan4.PACKET_HEADER;
|
|
9430
|
+
const reliableSize = this.reliableLength > 0 ? this.reliableLength + 2 : 0;
|
|
9431
|
+
let unreliableSize = unreliableData ? unreliableData.length : 0;
|
|
9432
|
+
if (headerSize + reliableSize + unreliableSize > _NetChan4.MAX_MSGLEN) {
|
|
9433
|
+
unreliableSize = _NetChan4.MAX_MSGLEN - headerSize - reliableSize;
|
|
9434
|
+
if (unreliableSize < 0) unreliableSize = 0;
|
|
9435
|
+
}
|
|
9436
|
+
const buffer = new ArrayBuffer(headerSize + reliableSize + unreliableSize);
|
|
9437
|
+
const view = new DataView(buffer);
|
|
9438
|
+
const result = new Uint8Array(buffer);
|
|
9439
|
+
let sequence = this.outgoingSequence;
|
|
9440
|
+
if (this.reliableLength > 0) {
|
|
9441
|
+
sequence |= 2147483648;
|
|
9442
|
+
if ((this.outgoingReliableSequence & 1) !== 0) {
|
|
9443
|
+
sequence |= 1073741824;
|
|
9444
|
+
}
|
|
9445
|
+
}
|
|
9446
|
+
view.setUint32(0, sequence, true);
|
|
9447
|
+
let ack = this.incomingSequence;
|
|
9448
|
+
if ((this.incomingReliableSequence & 1) !== 0) {
|
|
9449
|
+
ack |= 2147483648;
|
|
9450
|
+
}
|
|
9451
|
+
view.setUint32(4, ack, true);
|
|
9452
|
+
view.setUint16(8, this.qport, true);
|
|
9453
|
+
let offset = headerSize;
|
|
9454
|
+
if (this.reliableLength > 0) {
|
|
9455
|
+
view.setUint16(offset, this.reliableLength, true);
|
|
9456
|
+
offset += 2;
|
|
9457
|
+
const reliableBuffer = this.reliableMessage.getBuffer();
|
|
9458
|
+
const reliableBytes = reliableBuffer.subarray(0, this.reliableLength);
|
|
9459
|
+
result.set(reliableBytes, offset);
|
|
9460
|
+
offset += this.reliableLength;
|
|
9461
|
+
}
|
|
9462
|
+
if (unreliableData && unreliableSize > 0) {
|
|
9463
|
+
const chunk = unreliableData.slice(0, unreliableSize);
|
|
9464
|
+
result.set(chunk, offset);
|
|
9465
|
+
}
|
|
9466
|
+
return result;
|
|
9467
|
+
}
|
|
9468
|
+
/**
|
|
9469
|
+
* Processes a received packet
|
|
9470
|
+
* Returns the payload data (reliable + unreliable) to be processed, or null if discarded
|
|
9471
|
+
*/
|
|
9472
|
+
process(packet) {
|
|
9473
|
+
if (packet.length < _NetChan4.PACKET_HEADER) {
|
|
9474
|
+
return null;
|
|
9475
|
+
}
|
|
9476
|
+
this.lastReceived = Date.now();
|
|
9477
|
+
const view = new DataView(packet.buffer, packet.byteOffset, packet.byteLength);
|
|
9478
|
+
const sequence = view.getUint32(0, true);
|
|
9479
|
+
const ack = view.getUint32(4, true);
|
|
9480
|
+
const qport = view.getUint16(8, true);
|
|
9481
|
+
if (this.qport !== qport) {
|
|
9482
|
+
return null;
|
|
9483
|
+
}
|
|
9484
|
+
const seqNumberClean = sequence & ~(2147483648 | 1073741824);
|
|
9485
|
+
if ((seqNumberClean - this.incomingSequence | 0) <= 0) {
|
|
9486
|
+
return null;
|
|
9487
|
+
}
|
|
9488
|
+
this.incomingSequence = seqNumberClean;
|
|
9489
|
+
const ackNumber = ack & ~2147483648;
|
|
9490
|
+
const ackReliable = (ack & 2147483648) !== 0;
|
|
9491
|
+
if (ackNumber > this.incomingAcknowledged) {
|
|
9492
|
+
this.incomingAcknowledged = ackNumber;
|
|
9493
|
+
}
|
|
9494
|
+
if (this.reliableLength > 0) {
|
|
9495
|
+
const receivedAckBit = ackReliable ? 1 : 0;
|
|
9496
|
+
const currentReliableBit = this.outgoingReliableSequence & 1;
|
|
9497
|
+
if (receivedAckBit !== currentReliableBit) {
|
|
9498
|
+
this.reliableLength = 0;
|
|
9499
|
+
this.reliableMessage.reset();
|
|
9500
|
+
this.outgoingReliableSequence ^= 1;
|
|
9501
|
+
}
|
|
9502
|
+
}
|
|
9503
|
+
const hasReliableData = (sequence & 2147483648) !== 0;
|
|
9504
|
+
const reliableSeqBit = (sequence & 1073741824) !== 0 ? 1 : 0;
|
|
9505
|
+
let payloadOffset = _NetChan4.PACKET_HEADER;
|
|
9506
|
+
let reliableData = null;
|
|
9507
|
+
if (hasReliableData) {
|
|
9508
|
+
if (payloadOffset + 2 > packet.byteLength) return null;
|
|
9509
|
+
const reliableLen = view.getUint16(payloadOffset, true);
|
|
9510
|
+
payloadOffset += 2;
|
|
9511
|
+
const expectedBit = this.incomingReliableSequence & 1;
|
|
9512
|
+
if (reliableSeqBit === expectedBit) {
|
|
9513
|
+
this.incomingReliableSequence++;
|
|
9514
|
+
if (payloadOffset + reliableLen > packet.byteLength) return null;
|
|
9515
|
+
reliableData = packet.slice(payloadOffset, payloadOffset + reliableLen);
|
|
9516
|
+
}
|
|
9517
|
+
payloadOffset += reliableLen;
|
|
9518
|
+
}
|
|
9519
|
+
const unreliableData = packet.slice(payloadOffset);
|
|
9520
|
+
if (reliableData && reliableData.length > 0) {
|
|
9521
|
+
const totalLen = reliableData.length + unreliableData.length;
|
|
9522
|
+
const result = new Uint8Array(totalLen);
|
|
9523
|
+
result.set(reliableData, 0);
|
|
9524
|
+
result.set(unreliableData, reliableData.length);
|
|
9525
|
+
return result;
|
|
9526
|
+
}
|
|
9527
|
+
if (unreliableData) {
|
|
9528
|
+
return unreliableData;
|
|
9529
|
+
}
|
|
9530
|
+
return new Uint8Array(0);
|
|
9531
|
+
}
|
|
9532
|
+
/**
|
|
9533
|
+
* Checks if reliable message buffer is empty and ready for new data
|
|
9534
|
+
*/
|
|
9535
|
+
canSendReliable() {
|
|
9536
|
+
return this.reliableLength === 0;
|
|
9537
|
+
}
|
|
9538
|
+
/**
|
|
9539
|
+
* Writes a byte to the reliable message buffer
|
|
9540
|
+
*/
|
|
9541
|
+
writeReliableByte(value) {
|
|
9542
|
+
if (this.reliableLength + 1 > _NetChan4.MAX_MSGLEN - _NetChan4.HEADER_OVERHEAD) {
|
|
9543
|
+
throw new Error("NetChan reliable buffer overflow");
|
|
9544
|
+
}
|
|
9545
|
+
this.reliableMessage.writeByte(value);
|
|
9546
|
+
this.reliableLength++;
|
|
9547
|
+
}
|
|
9548
|
+
/**
|
|
9549
|
+
* Writes a short to the reliable message buffer
|
|
9550
|
+
*/
|
|
9551
|
+
writeReliableShort(value) {
|
|
9552
|
+
if (this.reliableLength + 2 > _NetChan4.MAX_MSGLEN - _NetChan4.HEADER_OVERHEAD) {
|
|
9553
|
+
throw new Error("NetChan reliable buffer overflow");
|
|
9554
|
+
}
|
|
9555
|
+
this.reliableMessage.writeShort(value);
|
|
9556
|
+
this.reliableLength += 2;
|
|
9557
|
+
}
|
|
9558
|
+
/**
|
|
9559
|
+
* Writes a long to the reliable message buffer
|
|
9560
|
+
*/
|
|
9561
|
+
writeReliableLong(value) {
|
|
9562
|
+
if (this.reliableLength + 4 > _NetChan4.MAX_MSGLEN - _NetChan4.HEADER_OVERHEAD) {
|
|
9563
|
+
throw new Error("NetChan reliable buffer overflow");
|
|
9564
|
+
}
|
|
9565
|
+
this.reliableMessage.writeLong(value);
|
|
9566
|
+
this.reliableLength += 4;
|
|
9567
|
+
}
|
|
9568
|
+
/**
|
|
9569
|
+
* Writes a string to the reliable message buffer
|
|
9570
|
+
*/
|
|
9571
|
+
writeReliableString(value) {
|
|
9572
|
+
const len2 = value.length + 1;
|
|
9573
|
+
if (this.reliableLength + len2 > _NetChan4.MAX_MSGLEN - _NetChan4.HEADER_OVERHEAD) {
|
|
9574
|
+
throw new Error("NetChan reliable buffer overflow");
|
|
9575
|
+
}
|
|
9576
|
+
this.reliableMessage.writeString(value);
|
|
9577
|
+
this.reliableLength += len2;
|
|
9578
|
+
}
|
|
9579
|
+
/**
|
|
9580
|
+
* Returns the current reliable data buffer
|
|
9581
|
+
*/
|
|
9582
|
+
getReliableData() {
|
|
9583
|
+
if (this.reliableLength === 0) {
|
|
9584
|
+
return new Uint8Array(0);
|
|
9585
|
+
}
|
|
9586
|
+
const buffer = this.reliableMessage.getBuffer();
|
|
9587
|
+
return buffer.subarray(0, this.reliableLength);
|
|
9588
|
+
}
|
|
9589
|
+
/**
|
|
9590
|
+
* Checks if we need to send a keepalive packet
|
|
9591
|
+
*/
|
|
9592
|
+
needsKeepalive(currentTime) {
|
|
9593
|
+
return currentTime - this.lastSent > 1e3;
|
|
9594
|
+
}
|
|
9595
|
+
/**
|
|
9596
|
+
* Checks if the connection has timed out
|
|
9597
|
+
*/
|
|
9598
|
+
isTimedOut(currentTime, timeoutMs = 3e4) {
|
|
9599
|
+
return currentTime - this.lastReceived > timeoutMs;
|
|
9600
|
+
}
|
|
9601
|
+
};
|
|
9602
|
+
_NetChan3.MAX_MSGLEN = 1400;
|
|
9603
|
+
_NetChan3.FRAGMENT_SIZE = 1024;
|
|
9604
|
+
_NetChan3.PACKET_HEADER = 10;
|
|
9605
|
+
_NetChan3.HEADER_OVERHEAD = _NetChan3.PACKET_HEADER + 2;
|
|
9054
9606
|
var AmmoType2 = /* @__PURE__ */ ((AmmoType22) => {
|
|
9055
9607
|
AmmoType22[AmmoType22["Bullets"] = 0] = "Bullets";
|
|
9056
9608
|
AmmoType22[AmmoType22["Shells"] = 1] = "Shells";
|
|
@@ -11118,7 +11670,7 @@ var MultiplayerConnection = class {
|
|
|
11118
11670
|
if (this.commandHistory.length > CMD_BACKUP) {
|
|
11119
11671
|
this.commandHistory.shift();
|
|
11120
11672
|
}
|
|
11121
|
-
const writer = new
|
|
11673
|
+
const writer = new BinaryWriter2();
|
|
11122
11674
|
writer.writeLong(0);
|
|
11123
11675
|
writer.writeLong(0);
|
|
11124
11676
|
writer.writeByte(ClientCommand.move);
|