@quake2ts/server 0.0.739 → 0.0.741

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/dist/dedicated.js DELETED
@@ -1,1104 +0,0 @@
1
- import { createGame, MulticastType, Solid } from '@quake2ts/game';
2
- import { createClient, ClientState } from './client.js';
3
- import { ClientMessageParser } from './protocol.js';
4
- import { BinaryWriter, ServerCommand, BinaryStream, traceBox, UPDATE_BACKUP, MAX_CONFIGSTRINGS, MAX_EDICTS, CollisionEntityIndex, inPVS, inPHS, crc8, ConfigStringIndex } from '@quake2ts/shared';
5
- import { parseBsp, PakArchive } from '@quake2ts/engine';
6
- import fs from 'node:fs/promises';
7
- import * as path from 'node:path';
8
- import { createPlayerInventory, createPlayerWeaponStates } from '@quake2ts/game';
9
- import { ServerState } from './server.js';
10
- import { writeDeltaEntity, writeRemoveEntity } from '@quake2ts/shared';
11
- import { writePlayerState } from './protocol/player.js';
12
- import { writeServerCommand } from './protocol/write.js';
13
- import { lerpAngle } from '@quake2ts/shared';
14
- import { WebSocketTransport } from './transports/websocket.js';
15
- function lerp(a, b, t) {
16
- return a + (b - a) * t;
17
- }
18
- const DEFAULT_MAX_CLIENTS = 16;
19
- const FRAME_RATE = 10; // 10Hz dedicated server loop (Q2 standard)
20
- const FRAME_TIME_MS = 1000 / FRAME_RATE;
21
- export class DedicatedServer {
22
- constructor(optionsOrPort = {}) {
23
- this.game = null;
24
- this.frameTimeout = null;
25
- this.entityIndex = null;
26
- // History buffer: Map<EntityIndex, HistoryArray>
27
- this.history = new Map();
28
- this.backup = new Map();
29
- const options = typeof optionsOrPort === 'number' ? { port: optionsOrPort } : optionsOrPort;
30
- this.options = {
31
- port: 27910,
32
- maxPlayers: DEFAULT_MAX_CLIENTS,
33
- deathmatch: true,
34
- ...options
35
- };
36
- this.transport = this.options.transport || new WebSocketTransport();
37
- this.svs = {
38
- initialized: false,
39
- realTime: 0,
40
- mapCmd: '',
41
- spawnCount: 0,
42
- clients: new Array(this.options.maxPlayers).fill(null),
43
- lastHeartbeat: 0,
44
- challenges: []
45
- };
46
- this.sv = {
47
- state: ServerState.Dead,
48
- attractLoop: false,
49
- loadGame: false,
50
- startTime: 0, // Initialize startTime
51
- time: 0,
52
- frame: 0,
53
- name: '',
54
- collisionModel: null,
55
- configStrings: new Array(MAX_CONFIGSTRINGS).fill(''),
56
- baselines: new Array(MAX_EDICTS).fill(null),
57
- multicastBuf: new Uint8Array(0)
58
- };
59
- this.entityIndex = new CollisionEntityIndex();
60
- }
61
- setTransport(transport) {
62
- if (this.svs.initialized) {
63
- throw new Error('Cannot set transport after server started');
64
- }
65
- this.transport = transport;
66
- }
67
- async startServer(mapName) {
68
- const map = mapName || this.options.mapName;
69
- if (!map) {
70
- throw new Error('No map specified');
71
- }
72
- await this.start(map);
73
- }
74
- stopServer() {
75
- this.stop();
76
- }
77
- kickPlayer(clientId) {
78
- if (clientId < 0 || clientId >= this.svs.clients.length)
79
- return;
80
- const client = this.svs.clients[clientId];
81
- if (client && client.state >= ClientState.Connected) {
82
- console.log(`Kicking client ${clientId}`);
83
- // Send disconnect message if possible
84
- if (client.netchan) {
85
- const writer = new BinaryWriter();
86
- writer.writeByte(ServerCommand.print);
87
- writer.writeByte(2);
88
- writer.writeString('Kicked by server.\n');
89
- try {
90
- const packet = client.netchan.transmit(writer.getData());
91
- client.net.send(packet);
92
- }
93
- catch (e) { }
94
- }
95
- this.dropClient(client);
96
- }
97
- }
98
- async changeMap(mapName) {
99
- console.log(`Changing map to ${mapName}`);
100
- // Notify clients?
101
- this.multicast({ x: 0, y: 0, z: 0 }, MulticastType.All, ServerCommand.print, 2, `Changing map to ${mapName}...\n`);
102
- // Stop current game loop
103
- if (this.frameTimeout)
104
- clearTimeout(this.frameTimeout);
105
- // Reset Server State
106
- this.sv.state = ServerState.Loading;
107
- this.sv.collisionModel = null;
108
- this.sv.time = 0;
109
- this.sv.frame = 0;
110
- this.sv.configStrings.fill('');
111
- this.sv.baselines.fill(null);
112
- this.history.clear();
113
- this.entityIndex = new CollisionEntityIndex();
114
- // Load new Map
115
- await this.loadMap(mapName);
116
- // Re-init game
117
- this.initGame();
118
- // Send new serverdata to all connected clients and respawn them
119
- for (const client of this.svs.clients) {
120
- if (client && client.state >= ClientState.Connected) {
121
- // Reset client game state
122
- client.edict = null; // Will be respawned
123
- client.state = ClientState.Connected; // Move back to connected state to trigger spawn
124
- // Send new serverdata
125
- this.sendServerData(client);
126
- // Force them to reload/precache
127
- client.netchan.writeReliableByte(ServerCommand.stufftext);
128
- client.netchan.writeReliableString(`map ${mapName}\n`);
129
- // Trigger spawn
130
- this.handleBegin(client);
131
- }
132
- }
133
- // Resume loop
134
- this.runFrame();
135
- }
136
- getConnectedClients() {
137
- const list = [];
138
- for (const client of this.svs.clients) {
139
- if (client && client.state >= ClientState.Connected) {
140
- list.push({
141
- id: client.index,
142
- name: 'Player', // TODO: Parse userinfo for name
143
- ping: client.ping,
144
- address: 'unknown'
145
- });
146
- }
147
- }
148
- return list;
149
- }
150
- async start(mapName) {
151
- console.log(`Starting Dedicated Server on port ${this.options.port}...`);
152
- this.sv.name = mapName;
153
- this.svs.initialized = true;
154
- this.svs.spawnCount++;
155
- // 1. Initialize Network
156
- this.transport.onConnection((driver, info) => {
157
- console.log('New connection', info ? `from ${info.socket?.remoteAddress}` : '');
158
- this.handleConnection(driver, info);
159
- });
160
- this.transport.onError((err) => {
161
- if (this.onServerError)
162
- this.onServerError(err);
163
- });
164
- await this.transport.listen(this.options.port);
165
- // 2. Load Map
166
- await this.loadMap(mapName);
167
- // 3. Initialize Game
168
- this.initGame();
169
- // 4. Start Loop
170
- this.runFrame();
171
- console.log('Server started.');
172
- }
173
- async loadMap(mapName) {
174
- try {
175
- console.log(`Loading map ${mapName}...`);
176
- this.sv.state = ServerState.Loading;
177
- this.sv.name = mapName;
178
- let arrayBuffer;
179
- // Check if map exists on disk
180
- try {
181
- await fs.access(mapName);
182
- const mapData = await fs.readFile(mapName);
183
- arrayBuffer = mapData.buffer.slice(mapData.byteOffset, mapData.byteOffset + mapData.byteLength);
184
- }
185
- catch (e) {
186
- console.log(`Map file ${mapName} not found on disk, checking pak.pak...`);
187
- // Check pak.pak
188
- const possiblePakPaths = [
189
- path.resolve(process.cwd(), 'pak.pak'),
190
- path.resolve(process.cwd(), '../pak.pak'),
191
- path.resolve(process.cwd(), '../../pak.pak'),
192
- path.resolve('baseq2/pak.pak')
193
- ];
194
- let pakPath = null;
195
- for (const p of possiblePakPaths) {
196
- try {
197
- await fs.access(p);
198
- pakPath = p;
199
- break;
200
- }
201
- catch { }
202
- }
203
- if (!pakPath) {
204
- throw new Error(`Map ${mapName} not found and pak.pak not found.`);
205
- }
206
- const pakBuffer = await fs.readFile(pakPath);
207
- const pakArrayBuffer = pakBuffer.buffer.slice(pakBuffer.byteOffset, pakBuffer.byteOffset + pakBuffer.byteLength);
208
- const pak = PakArchive.fromArrayBuffer('pak.pak', pakArrayBuffer);
209
- const entry = pak.getEntry(mapName);
210
- if (!entry) {
211
- throw new Error(`Map ${mapName} not found in pak.pak`);
212
- }
213
- const data = pak.readFile(mapName);
214
- arrayBuffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
215
- }
216
- const bspMap = parseBsp(arrayBuffer);
217
- // Convert BspMap to CollisionModel manually
218
- const planes = bspMap.planes.map(p => {
219
- const normal = { x: p.normal[0], y: p.normal[1], z: p.normal[2] };
220
- let signbits = 0;
221
- if (normal.x < 0)
222
- signbits |= 1;
223
- if (normal.y < 0)
224
- signbits |= 2;
225
- if (normal.z < 0)
226
- signbits |= 4;
227
- return {
228
- normal,
229
- dist: p.dist,
230
- type: p.type,
231
- signbits
232
- };
233
- });
234
- const nodes = bspMap.nodes.map(n => ({
235
- plane: planes[n.planeIndex],
236
- children: n.children
237
- }));
238
- const leafBrushes = [];
239
- const leaves = bspMap.leafs.map((l, i) => {
240
- const brushes = bspMap.leafLists.leafBrushes[i];
241
- const firstLeafBrush = leafBrushes.length;
242
- leafBrushes.push(...brushes);
243
- return {
244
- contents: l.contents,
245
- cluster: l.cluster,
246
- area: l.area,
247
- firstLeafBrush,
248
- numLeafBrushes: brushes.length
249
- };
250
- });
251
- const brushes = bspMap.brushes.map(b => {
252
- const sides = [];
253
- for (let i = 0; i < b.numSides; i++) {
254
- const sideIndex = b.firstSide + i;
255
- const bspSide = bspMap.brushSides[sideIndex];
256
- const plane = planes[bspSide.planeIndex];
257
- const texInfo = bspMap.texInfo[bspSide.texInfo];
258
- const surfaceFlags = texInfo ? texInfo.flags : 0;
259
- sides.push({
260
- plane,
261
- surfaceFlags
262
- });
263
- }
264
- return {
265
- contents: b.contents,
266
- sides,
267
- checkcount: 0
268
- };
269
- });
270
- const bmodels = bspMap.models.map(m => ({
271
- mins: { x: m.mins[0], y: m.mins[1], z: m.mins[2] },
272
- maxs: { x: m.maxs[0], y: m.maxs[1], z: m.maxs[2] },
273
- origin: { x: m.origin[0], y: m.origin[1], z: m.origin[2] },
274
- headnode: m.headNode
275
- }));
276
- let visibility;
277
- if (bspMap.visibility) {
278
- visibility = {
279
- numClusters: bspMap.visibility.numClusters,
280
- clusters: bspMap.visibility.clusters
281
- };
282
- }
283
- this.sv.collisionModel = {
284
- planes,
285
- nodes,
286
- leaves,
287
- brushes,
288
- leafBrushes,
289
- bmodels,
290
- visibility
291
- };
292
- console.log(`Map loaded successfully.`);
293
- }
294
- catch (e) {
295
- console.warn('Failed to load map:', e);
296
- if (this.onServerError)
297
- this.onServerError(e);
298
- }
299
- }
300
- initGame() {
301
- this.sv.startTime = Date.now();
302
- const imports = {
303
- trace: (start, mins, maxs, end, passent, contentmask) => {
304
- if (this.entityIndex) {
305
- const result = this.entityIndex.trace({
306
- start,
307
- end,
308
- mins: mins || undefined,
309
- maxs: maxs || undefined,
310
- model: this.sv.collisionModel,
311
- passId: passent ? passent.index : undefined,
312
- contentMask: contentmask
313
- });
314
- let hitEntity = null;
315
- if (result.entityId !== null && result.entityId !== undefined && this.game) {
316
- hitEntity = this.game.entities.getByIndex(result.entityId) ?? null;
317
- }
318
- return {
319
- allsolid: result.allsolid,
320
- startsolid: result.startsolid,
321
- fraction: result.fraction,
322
- endpos: result.endpos,
323
- plane: result.plane || null,
324
- surfaceFlags: result.surfaceFlags || 0,
325
- contents: result.contents || 0,
326
- ent: hitEntity
327
- };
328
- }
329
- const worldResult = this.sv.collisionModel ? traceBox({
330
- start,
331
- end,
332
- mins: mins || undefined,
333
- maxs: maxs || undefined,
334
- model: this.sv.collisionModel,
335
- contentMask: contentmask
336
- }) : {
337
- fraction: 1.0,
338
- endpos: { ...end },
339
- allsolid: false,
340
- startsolid: false,
341
- plane: null,
342
- surfaceFlags: 0,
343
- contents: 0
344
- };
345
- return {
346
- allsolid: worldResult.allsolid,
347
- startsolid: worldResult.startsolid,
348
- fraction: worldResult.fraction,
349
- endpos: worldResult.endpos,
350
- plane: worldResult.plane || null,
351
- surfaceFlags: worldResult.surfaceFlags || 0,
352
- contents: worldResult.contents || 0,
353
- ent: null
354
- };
355
- },
356
- pointcontents: (p) => 0,
357
- linkentity: (ent) => {
358
- if (!this.entityIndex)
359
- return;
360
- this.entityIndex.link({
361
- id: ent.index,
362
- origin: ent.origin,
363
- mins: ent.mins,
364
- maxs: ent.maxs,
365
- contents: ent.solid === 0 ? 0 : 1,
366
- surfaceFlags: 0
367
- });
368
- },
369
- areaEdicts: (mins, maxs) => {
370
- if (!this.entityIndex)
371
- return [];
372
- return this.entityIndex.gatherTriggerTouches({ x: 0, y: 0, z: 0 }, mins, maxs, 0xFFFFFFFF);
373
- },
374
- multicast: (origin, type, event, ...args) => this.multicast(origin, type, event, ...args),
375
- unicast: (ent, reliable, event, ...args) => this.unicast(ent, reliable, event, ...args),
376
- configstring: (index, value) => this.SV_SetConfigString(index, value),
377
- serverCommand: (cmd) => { console.log(`Server command: ${cmd}`); },
378
- setLagCompensation: (active, client, lagMs) => this.setLagCompensation(active, client, lagMs)
379
- };
380
- this.game = createGame(imports, this, {
381
- gravity: { x: 0, y: 0, z: -800 },
382
- deathmatch: this.options.deathmatch !== false
383
- });
384
- this.game.init(0);
385
- this.game.spawnWorld();
386
- this.populateBaselines();
387
- this.sv.state = ServerState.Game;
388
- }
389
- populateBaselines() {
390
- if (!this.game)
391
- return;
392
- this.game.entities.forEachEntity((ent) => {
393
- if (ent.index >= MAX_EDICTS)
394
- return;
395
- if (ent.modelindex > 0 || ent.solid !== Solid.Not) {
396
- this.sv.baselines[ent.index] = this.entityToState(ent);
397
- }
398
- });
399
- }
400
- entityToState(ent) {
401
- return {
402
- number: ent.index,
403
- origin: { ...ent.origin },
404
- angles: { ...ent.angles },
405
- modelIndex: ent.modelindex,
406
- frame: ent.frame,
407
- skinNum: ent.skin,
408
- effects: ent.effects,
409
- renderfx: ent.renderfx,
410
- solid: ent.solid,
411
- sound: ent.sounds,
412
- event: 0
413
- };
414
- }
415
- stop() {
416
- if (this.frameTimeout)
417
- clearTimeout(this.frameTimeout);
418
- this.transport.close();
419
- this.game?.shutdown();
420
- this.sv.state = ServerState.Dead;
421
- }
422
- handleConnection(driver, info) {
423
- let clientIndex = -1;
424
- for (let i = 0; i < this.options.maxPlayers; i++) {
425
- if (this.svs.clients[i] === null || this.svs.clients[i].state === ClientState.Free) {
426
- clientIndex = i;
427
- break;
428
- }
429
- }
430
- if (clientIndex === -1) {
431
- console.log('Server full, rejecting connection');
432
- driver.disconnect();
433
- return;
434
- }
435
- const client = createClient(clientIndex, driver);
436
- client.lastMessage = this.sv.frame;
437
- client.lastCommandTime = Date.now();
438
- this.svs.clients[clientIndex] = client;
439
- console.log(`Client ${clientIndex} attached to slot from ${info?.socket?.remoteAddress || 'unknown'}`);
440
- driver.onMessage((data) => this.onClientMessage(client, data));
441
- driver.onClose(() => this.onClientDisconnect(client));
442
- }
443
- onClientMessage(client, data) {
444
- const buffer = data.byteOffset === 0 && data.byteLength === data.buffer.byteLength
445
- ? data.buffer
446
- : data.slice().buffer;
447
- if (buffer instanceof ArrayBuffer) {
448
- client.messageQueue.push(new Uint8Array(buffer));
449
- }
450
- else {
451
- // SharedArrayBuffer fallback or other weirdness
452
- client.messageQueue.push(new Uint8Array(buffer));
453
- }
454
- }
455
- onClientDisconnect(client) {
456
- console.log(`Client ${client.index} disconnected`);
457
- if (client.edict && this.game) {
458
- this.game.clientDisconnect(client.edict);
459
- }
460
- if (this.onClientDisconnected) {
461
- this.onClientDisconnected(client.index);
462
- }
463
- client.state = ClientState.Free;
464
- this.svs.clients[client.index] = null;
465
- if (this.entityIndex && client.edict) {
466
- this.entityIndex.unlink(client.edict.index);
467
- }
468
- }
469
- dropClient(client) {
470
- if (client.net) {
471
- client.net.disconnect();
472
- }
473
- }
474
- handleMove(client, cmd, checksum, lastFrame) {
475
- if (lastFrame > 0 && lastFrame <= client.lastFrame && lastFrame > client.lastFrame - UPDATE_BACKUP) {
476
- const frameIdx = lastFrame % UPDATE_BACKUP;
477
- const frame = client.frames[frameIdx];
478
- if (frame.packetCRC !== checksum) {
479
- console.warn(`Client ${client.index} checksum mismatch for frame ${lastFrame}: expected ${frame.packetCRC}, got ${checksum}`);
480
- }
481
- }
482
- client.lastCmd = cmd;
483
- client.lastMessage = this.sv.frame;
484
- client.commandCount++;
485
- }
486
- handleUserInfo(client, info) {
487
- client.userInfo = info;
488
- }
489
- handleStringCmd(client, cmd) {
490
- console.log(`Client ${client.index} stringcmd: ${cmd}`);
491
- if (cmd === 'getchallenge') {
492
- this.handleGetChallenge(client);
493
- }
494
- else if (cmd.startsWith('connect ')) {
495
- const userInfo = cmd.substring(8);
496
- this.handleConnect(client, userInfo);
497
- }
498
- else if (cmd === 'begin') {
499
- this.handleBegin(client);
500
- }
501
- else if (cmd === 'status') {
502
- this.handleStatus(client);
503
- }
504
- }
505
- handleStatus(client) {
506
- let activeClients = 0;
507
- for (const c of this.svs.clients) {
508
- if (c && c.state >= ClientState.Connected) {
509
- activeClients++;
510
- }
511
- }
512
- let status = `map: ${this.sv.name}\n`;
513
- status += `players: ${activeClients} active (${this.options.maxPlayers} max)\n\n`;
514
- status += `num score ping name lastmsg address qport rate\n`;
515
- status += `--- ----- ---- --------------- ------- --------------------- ----- -----\n`;
516
- for (const c of this.svs.clients) {
517
- if (c && c.state >= ClientState.Connected) {
518
- const score = 0;
519
- const ping = 0;
520
- const lastMsg = this.sv.frame - c.lastMessage;
521
- const address = 'unknown';
522
- status += `${c.index.toString().padStart(3)} ${score.toString().padStart(5)} ${ping.toString().padStart(4)} ${c.userInfo.substring(0, 15).padEnd(15)} ${lastMsg.toString().padStart(7)} ${address.padEnd(21)} ${c.netchan.qport.toString().padStart(5)} 0\n`;
523
- }
524
- }
525
- const writer = new BinaryWriter();
526
- writer.writeByte(ServerCommand.print);
527
- writer.writeByte(2);
528
- writer.writeString(status);
529
- const packet = client.netchan.transmit(writer.getData());
530
- client.net.send(packet);
531
- }
532
- handleGetChallenge(client) {
533
- const challenge = Math.floor(Math.random() * 1000000) + 1;
534
- client.challenge = challenge;
535
- const writer = new BinaryWriter();
536
- writer.writeByte(ServerCommand.stufftext);
537
- writer.writeString(`challenge ${challenge}\n`);
538
- const packet = client.netchan.transmit(writer.getData());
539
- client.net.send(packet);
540
- }
541
- handleConnect(client, userInfo) {
542
- if (!this.game)
543
- return;
544
- const result = this.game.clientConnect(client.edict || null, userInfo);
545
- if (result === true) {
546
- client.state = ClientState.Connected;
547
- client.userInfo = userInfo;
548
- console.log(`Client ${client.index} connected: ${userInfo}`);
549
- if (this.onClientConnected) {
550
- // Extract name from userinfo if possible, default to Player
551
- this.onClientConnected(client.index, 'Player');
552
- }
553
- try {
554
- this.sendServerData(client);
555
- client.netchan.writeReliableByte(ServerCommand.stufftext);
556
- client.netchan.writeReliableString("precache\n");
557
- const packet = client.netchan.transmit();
558
- client.net.send(packet);
559
- }
560
- catch (e) {
561
- console.warn(`Client ${client.index} reliable buffer overflow or connection error`);
562
- this.dropClient(client);
563
- }
564
- }
565
- else {
566
- console.log(`Client ${client.index} rejected: ${result}`);
567
- const writer = new BinaryWriter();
568
- writer.writeByte(ServerCommand.print);
569
- writer.writeByte(2);
570
- writer.writeString(`Connection rejected: ${result}\n`);
571
- const packet = client.netchan.transmit(writer.getData());
572
- client.net.send(packet);
573
- }
574
- }
575
- handleBegin(client) {
576
- if (client.state === ClientState.Connected) {
577
- this.spawnClient(client);
578
- }
579
- }
580
- spawnClient(client) {
581
- if (!this.game)
582
- return;
583
- const ent = this.game.clientBegin({
584
- inventory: createPlayerInventory(),
585
- weaponStates: createPlayerWeaponStates(),
586
- buttons: 0,
587
- pm_type: 0,
588
- pm_time: 0,
589
- pm_flags: 0,
590
- gun_frame: 0,
591
- rdflags: 0,
592
- fov: 90,
593
- pers: {
594
- connected: true,
595
- inventory: [],
596
- health: 100,
597
- max_health: 100,
598
- savedFlags: 0,
599
- selected_item: 0
600
- }
601
- });
602
- client.edict = ent;
603
- client.state = ClientState.Active;
604
- console.log(`Client ${client.index} entered game`);
605
- }
606
- sendServerData(client) {
607
- client.netchan.writeReliableByte(ServerCommand.serverdata);
608
- client.netchan.writeReliableLong(34);
609
- client.netchan.writeReliableLong(this.sv.frame);
610
- client.netchan.writeReliableByte(0);
611
- client.netchan.writeReliableString("baseq2");
612
- client.netchan.writeReliableShort(client.index);
613
- client.netchan.writeReliableString(this.sv.name || "maps/test.bsp");
614
- for (let i = 0; i < MAX_CONFIGSTRINGS; i++) {
615
- if (this.sv.configStrings[i]) {
616
- client.netchan.writeReliableByte(ServerCommand.configstring);
617
- client.netchan.writeReliableShort(i);
618
- client.netchan.writeReliableString(this.sv.configStrings[i]);
619
- }
620
- }
621
- const baselineWriter = new BinaryWriter();
622
- for (let i = 0; i < MAX_EDICTS; i++) {
623
- if (this.sv.baselines[i]) {
624
- baselineWriter.reset();
625
- baselineWriter.writeByte(ServerCommand.spawnbaseline);
626
- writeDeltaEntity({}, this.sv.baselines[i], baselineWriter, true, true);
627
- const data = baselineWriter.getData();
628
- for (let j = 0; j < data.length; j++) {
629
- client.netchan.writeReliableByte(data[j]);
630
- }
631
- }
632
- }
633
- }
634
- SV_SetConfigString(index, value) {
635
- if (index < 0 || index >= MAX_CONFIGSTRINGS)
636
- return;
637
- this.sv.configStrings[index] = value;
638
- for (const client of this.svs.clients) {
639
- if (client && client.state >= ClientState.Connected) {
640
- if (client.netchan) {
641
- try {
642
- client.netchan.writeReliableByte(ServerCommand.configstring);
643
- client.netchan.writeReliableShort(index);
644
- client.netchan.writeReliableString(value);
645
- const packet = client.netchan.transmit();
646
- client.net.send(packet);
647
- }
648
- catch (e) {
649
- console.warn(`Client ${client.index} reliable buffer overflow`);
650
- this.dropClient(client);
651
- }
652
- }
653
- }
654
- }
655
- }
656
- SV_WriteConfigString(writer, index, value) {
657
- writer.writeByte(ServerCommand.configstring);
658
- writer.writeShort(index);
659
- writer.writeString(value);
660
- }
661
- SV_ReadPackets() {
662
- for (const client of this.svs.clients) {
663
- if (!client || client.state === ClientState.Free)
664
- continue;
665
- while (client.messageQueue.length > 0) {
666
- const rawData = client.messageQueue.shift();
667
- if (!rawData)
668
- continue;
669
- if (rawData.byteLength >= 10) {
670
- const view = new DataView(rawData.buffer, rawData.byteOffset, rawData.byteLength);
671
- const incomingQPort = view.getUint16(8, true);
672
- if (client.netchan.qport !== incomingQPort) {
673
- client.netchan.qport = incomingQPort;
674
- }
675
- }
676
- const data = client.netchan.process(rawData);
677
- if (!data) {
678
- continue;
679
- }
680
- if (data.length === 0) {
681
- continue;
682
- }
683
- let buffer;
684
- if (data.buffer instanceof ArrayBuffer) {
685
- buffer = data.buffer;
686
- }
687
- else {
688
- buffer = new Uint8Array(data).buffer;
689
- }
690
- const reader = new BinaryStream(buffer);
691
- const parser = new ClientMessageParser(reader, {
692
- onMove: (checksum, lastFrame, cmd) => this.handleMove(client, cmd, checksum, lastFrame),
693
- onUserInfo: (info) => this.handleUserInfo(client, info),
694
- onStringCmd: (cmd) => this.handleStringCmd(client, cmd),
695
- onNop: () => { },
696
- onBad: () => {
697
- console.warn(`Bad command from client ${client.index}`);
698
- }
699
- });
700
- try {
701
- parser.parseMessage();
702
- }
703
- catch (e) {
704
- console.error(`Error parsing message from client ${client.index}:`, e);
705
- }
706
- }
707
- }
708
- }
709
- runFrame() {
710
- if (!this.game)
711
- return;
712
- const startTime = Date.now();
713
- this.sv.frame++;
714
- this.sv.time += 100; // 100ms per frame
715
- // 1. Read network packets
716
- this.SV_ReadPackets();
717
- // 2. Run client commands
718
- for (const client of this.svs.clients) {
719
- if (!client || client.state === ClientState.Free)
720
- continue;
721
- if (client.edict && client.edict.client) {
722
- client.edict.client.ping = client.ping;
723
- }
724
- if (client.state >= ClientState.Connected) {
725
- const timeoutFrames = 300;
726
- if (this.sv.frame - client.lastMessage > timeoutFrames) {
727
- console.log(`Client ${client.index} timed out`);
728
- this.dropClient(client);
729
- continue;
730
- }
731
- }
732
- if (client && client.state === ClientState.Active && client.edict) {
733
- const now = Date.now();
734
- if (now - client.lastCommandTime >= 1000) {
735
- client.lastCommandTime = now;
736
- client.commandCount = 0;
737
- }
738
- if (client.commandCount > 200) {
739
- console.warn(`Client ${client.index} kicked for command flooding (count: ${client.commandCount})`);
740
- this.dropClient(client);
741
- continue;
742
- }
743
- this.game.clientThink(client.edict, client.lastCmd);
744
- }
745
- }
746
- // 3. Run simulation
747
- const snapshot = this.game.frame({
748
- frame: this.sv.frame,
749
- deltaMs: FRAME_TIME_MS,
750
- nowMs: Date.now()
751
- });
752
- // 3.1 Record History for Lag Compensation
753
- this.recordHistory();
754
- // 4. Send Updates
755
- if (snapshot && snapshot.state) {
756
- this.SV_SendClientMessages(snapshot.state);
757
- }
758
- const endTime = Date.now();
759
- const elapsed = endTime - startTime;
760
- const sleepTime = Math.max(0, FRAME_TIME_MS - elapsed);
761
- if (this.sv.state === ServerState.Game) {
762
- this.frameTimeout = setTimeout(() => this.runFrame(), sleepTime);
763
- }
764
- }
765
- SV_SendClientMessages(snapshot) {
766
- for (const client of this.svs.clients) {
767
- if (client && client.state === ClientState.Active) {
768
- this.SV_SendClientFrame(client, snapshot);
769
- }
770
- }
771
- }
772
- SV_SendClientFrame(client, snapshot) {
773
- const MTU = 1400;
774
- const writer = new BinaryWriter(MTU);
775
- writer.writeByte(ServerCommand.frame);
776
- writer.writeLong(this.sv.frame);
777
- let deltaFrame = 0;
778
- if (client.lastFrame && client.lastFrame < this.sv.frame && client.lastFrame >= this.sv.frame - UPDATE_BACKUP) {
779
- deltaFrame = client.lastFrame;
780
- }
781
- writer.writeLong(deltaFrame);
782
- writer.writeByte(0);
783
- writer.writeByte(0);
784
- writer.writeByte(ServerCommand.playerinfo);
785
- const ps = {
786
- pm_type: snapshot.pmType,
787
- origin: snapshot.origin,
788
- velocity: snapshot.velocity,
789
- pm_time: snapshot.pm_time,
790
- pm_flags: snapshot.pmFlags,
791
- gravity: Math.abs(snapshot.gravity.z),
792
- delta_angles: snapshot.deltaAngles,
793
- viewoffset: { x: 0, y: 0, z: 22 },
794
- viewangles: snapshot.viewangles,
795
- kick_angles: snapshot.kick_angles,
796
- gun_index: snapshot.gunindex,
797
- gun_frame: snapshot.gun_frame,
798
- gun_offset: snapshot.gunoffset,
799
- gun_angles: snapshot.gunangles,
800
- blend: snapshot.blend,
801
- fov: snapshot.fov,
802
- rdflags: snapshot.rdflags,
803
- stats: snapshot.stats,
804
- watertype: snapshot.watertype // Populate watertype
805
- };
806
- writePlayerState(writer, ps);
807
- writer.writeByte(ServerCommand.packetentities);
808
- const entities = snapshot.packetEntities || [];
809
- const currentEntityIds = [];
810
- const frameIdx = this.sv.frame % UPDATE_BACKUP;
811
- const currentFrame = client.frames[frameIdx];
812
- currentFrame.entities = entities;
813
- let oldEntities = [];
814
- if (deltaFrame > 0) {
815
- const oldFrameIdx = deltaFrame % UPDATE_BACKUP;
816
- oldEntities = client.frames[oldFrameIdx].entities;
817
- }
818
- for (const entityState of currentFrame.entities) {
819
- if (writer.getOffset() > MTU - 200) {
820
- console.warn('Packet MTU limit reached, dropping remaining entities');
821
- break;
822
- }
823
- currentEntityIds.push(entityState.number);
824
- const oldState = oldEntities.find(e => e.number === entityState.number);
825
- if (oldState) {
826
- writeDeltaEntity(oldState, entityState, writer, false, false);
827
- }
828
- else {
829
- writeDeltaEntity({}, entityState, writer, false, true);
830
- }
831
- }
832
- for (const oldId of client.lastPacketEntities) {
833
- if (writer.getOffset() > MTU - 10) {
834
- console.warn('Packet MTU limit reached, dropping remaining removals');
835
- break;
836
- }
837
- if (!currentEntityIds.includes(oldId)) {
838
- writeRemoveEntity(oldId, writer);
839
- }
840
- }
841
- writer.writeShort(0);
842
- const frameData = writer.getData();
843
- currentFrame.packetCRC = crc8(frameData);
844
- const packet = client.netchan.transmit(frameData);
845
- client.net.send(packet);
846
- client.lastFrame = this.sv.frame;
847
- client.lastPacketEntities = currentEntityIds;
848
- }
849
- // GameEngine Implementation
850
- trace(start, end) {
851
- return { fraction: 1.0 };
852
- }
853
- modelIndex(name) {
854
- console.log(`modelIndex(${name}) called`);
855
- // Find existing
856
- const start = ConfigStringIndex.Models;
857
- const end = ConfigStringIndex.Sounds;
858
- for (let i = start + 1; i < end; i++) {
859
- if (this.sv.configStrings[i] === name) {
860
- return i - start;
861
- }
862
- }
863
- // Find empty slot
864
- for (let i = start + 1; i < end; i++) {
865
- if (!this.sv.configStrings[i]) {
866
- this.SV_SetConfigString(i, name);
867
- return i - start;
868
- }
869
- }
870
- console.warn(`MAX_MODELS overflow for ${name}`);
871
- return 0;
872
- }
873
- soundIndex(name) {
874
- const start = ConfigStringIndex.Sounds;
875
- const end = ConfigStringIndex.Images;
876
- for (let i = start + 1; i < end; i++) {
877
- if (this.sv.configStrings[i] === name) {
878
- return i - start;
879
- }
880
- }
881
- for (let i = start + 1; i < end; i++) {
882
- if (!this.sv.configStrings[i]) {
883
- this.SV_SetConfigString(i, name);
884
- return i - start;
885
- }
886
- }
887
- console.warn(`MAX_SOUNDS overflow for ${name}`);
888
- return 0;
889
- }
890
- imageIndex(name) {
891
- const start = ConfigStringIndex.Images;
892
- const end = ConfigStringIndex.Lights;
893
- for (let i = start + 1; i < end; i++) {
894
- if (this.sv.configStrings[i] === name) {
895
- return i - start;
896
- }
897
- }
898
- for (let i = start + 1; i < end; i++) {
899
- if (!this.sv.configStrings[i]) {
900
- this.SV_SetConfigString(i, name);
901
- return i - start;
902
- }
903
- }
904
- console.warn(`MAX_IMAGES overflow for ${name}`);
905
- return 0;
906
- }
907
- multicast(origin, type, event, ...args) {
908
- const writer = new BinaryWriter();
909
- writeServerCommand(writer, event, ...args);
910
- const data = writer.getData();
911
- const reliable = false;
912
- for (const client of this.svs.clients) {
913
- if (!client || client.state < ClientState.Active || !client.edict) {
914
- continue;
915
- }
916
- let send = false;
917
- switch (type) {
918
- case MulticastType.All:
919
- send = true;
920
- break;
921
- case MulticastType.Pvs:
922
- if (this.sv.collisionModel) {
923
- send = inPVS(origin, client.edict.origin, this.sv.collisionModel);
924
- }
925
- else {
926
- send = true;
927
- }
928
- break;
929
- case MulticastType.Phs:
930
- if (this.sv.collisionModel) {
931
- send = inPHS(origin, client.edict.origin, this.sv.collisionModel);
932
- }
933
- else {
934
- send = true;
935
- }
936
- break;
937
- }
938
- if (send) {
939
- if (reliable) {
940
- try {
941
- for (let i = 0; i < data.length; i++) {
942
- client.netchan.writeReliableByte(data[i]);
943
- }
944
- }
945
- catch (e) {
946
- }
947
- }
948
- else {
949
- const packet = client.netchan.transmit(data);
950
- client.net.send(packet);
951
- }
952
- }
953
- }
954
- }
955
- unicast(ent, reliable, event, ...args) {
956
- const client = this.svs.clients.find(c => c?.edict === ent);
957
- if (client && client.state >= ClientState.Connected) {
958
- const writer = new BinaryWriter();
959
- writeServerCommand(writer, event, ...args);
960
- const data = writer.getData();
961
- if (reliable) {
962
- try {
963
- for (let i = 0; i < data.length; i++) {
964
- client.netchan.writeReliableByte(data[i]);
965
- }
966
- const packet = client.netchan.transmit();
967
- client.net.send(packet);
968
- }
969
- catch (e) {
970
- console.warn(`Client ${client.index} reliable buffer overflow in unicast`);
971
- this.dropClient(client);
972
- }
973
- }
974
- else {
975
- const packet = client.netchan.transmit(data);
976
- client.net.send(packet);
977
- }
978
- }
979
- }
980
- configstring(index, value) {
981
- this.SV_SetConfigString(index, value);
982
- }
983
- recordHistory() {
984
- if (!this.game)
985
- return;
986
- const now = Date.now();
987
- const HISTORY_MAX_MS = 1000;
988
- this.game.entities.forEachEntity((ent) => {
989
- if (ent.solid !== Solid.Not || ent.takedamage) {
990
- let hist = this.history.get(ent.index);
991
- if (!hist) {
992
- hist = [];
993
- this.history.set(ent.index, hist);
994
- }
995
- hist.push({
996
- time: now,
997
- origin: { ...ent.origin },
998
- mins: { ...ent.mins },
999
- maxs: { ...ent.maxs },
1000
- angles: { ...ent.angles }
1001
- });
1002
- while (hist.length > 0 && hist[0].time < now - HISTORY_MAX_MS) {
1003
- hist.shift();
1004
- }
1005
- }
1006
- });
1007
- }
1008
- setLagCompensation(active, client, lagMs) {
1009
- if (!this.game || !this.entityIndex)
1010
- return;
1011
- if (active) {
1012
- if (!client || lagMs === undefined)
1013
- return;
1014
- const now = Date.now();
1015
- const targetTime = now - lagMs;
1016
- this.game.entities.forEachEntity((ent) => {
1017
- if (ent === client)
1018
- return;
1019
- if (ent.solid === Solid.Not && !ent.takedamage)
1020
- return;
1021
- const hist = this.history.get(ent.index);
1022
- if (!hist || hist.length === 0)
1023
- return;
1024
- let i = hist.length - 1;
1025
- while (i >= 0 && hist[i].time > targetTime) {
1026
- i--;
1027
- }
1028
- if (i < 0) {
1029
- i = 0;
1030
- }
1031
- const s1 = hist[i];
1032
- const s2 = (i + 1 < hist.length) ? hist[i + 1] : s1;
1033
- let frac = 0;
1034
- if (s1.time !== s2.time) {
1035
- frac = (targetTime - s1.time) / (s2.time - s1.time);
1036
- }
1037
- if (frac < 0)
1038
- frac = 0;
1039
- if (frac > 1)
1040
- frac = 1;
1041
- const origin = {
1042
- x: s1.origin.x + (s2.origin.x - s1.origin.x) * frac,
1043
- y: s1.origin.y + (s2.origin.y - s1.origin.y) * frac,
1044
- z: s1.origin.z + (s2.origin.z - s1.origin.z) * frac
1045
- };
1046
- const angles = {
1047
- x: lerpAngle(s1.angles.x, s2.angles.x, frac),
1048
- y: lerpAngle(s1.angles.y, s2.angles.y, frac),
1049
- z: lerpAngle(s1.angles.z, s2.angles.z, frac)
1050
- };
1051
- this.backup.set(ent.index, {
1052
- origin: { ...ent.origin },
1053
- mins: { ...ent.mins },
1054
- maxs: { ...ent.maxs },
1055
- angles: { ...ent.angles },
1056
- link: true
1057
- });
1058
- ent.origin = origin;
1059
- ent.angles = angles;
1060
- ent.mins = {
1061
- x: s1.mins.x + (s2.mins.x - s1.mins.x) * frac,
1062
- y: s1.mins.y + (s2.mins.y - s1.mins.y) * frac,
1063
- z: s1.mins.z + (s2.mins.z - s1.mins.z) * frac
1064
- };
1065
- ent.maxs = {
1066
- x: s1.maxs.x + (s2.maxs.x - s1.maxs.x) * frac,
1067
- y: s1.maxs.y + (s2.maxs.y - s1.maxs.y) * frac,
1068
- z: s1.maxs.z + (s2.maxs.z - s1.maxs.z) * frac
1069
- };
1070
- this.entityIndex.link({
1071
- id: ent.index,
1072
- origin: ent.origin,
1073
- mins: ent.mins,
1074
- maxs: ent.maxs,
1075
- contents: ent.solid === 0 ? 0 : 1,
1076
- surfaceFlags: 0
1077
- });
1078
- });
1079
- }
1080
- else {
1081
- this.backup.forEach((state, id) => {
1082
- const ent = this.game?.entities.getByIndex(id);
1083
- if (ent) {
1084
- ent.origin = state.origin;
1085
- ent.mins = state.mins;
1086
- ent.maxs = state.maxs;
1087
- ent.angles = state.angles;
1088
- this.entityIndex.link({
1089
- id: ent.index,
1090
- origin: ent.origin,
1091
- mins: ent.mins,
1092
- maxs: ent.maxs,
1093
- contents: ent.solid === 0 ? 0 : 1,
1094
- surfaceFlags: 0
1095
- });
1096
- }
1097
- });
1098
- this.backup.clear();
1099
- }
1100
- }
1101
- }
1102
- export function createServer(options = {}) {
1103
- return new DedicatedServer(options);
1104
- }