@quake2ts/cgame 0.0.739

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.
Files changed (52) hide show
  1. package/dist/browser/index.global.js +10 -0
  2. package/dist/browser/index.global.js.map +1 -0
  3. package/dist/cjs/index.cjs +947 -0
  4. package/dist/cjs/index.cjs.map +1 -0
  5. package/dist/esm/index.js +940 -0
  6. package/dist/esm/index.js.map +1 -0
  7. package/dist/index.cjs +1353 -0
  8. package/dist/index.cjs.map +1 -0
  9. package/dist/index.d.cts +202 -0
  10. package/dist/index.d.ts +202 -0
  11. package/dist/index.js +1346 -0
  12. package/dist/index.js.map +1 -0
  13. package/dist/tsconfig.tsbuildinfo +1 -0
  14. package/dist/types/hud/blends.d.ts +4 -0
  15. package/dist/types/hud/blends.d.ts.map +1 -0
  16. package/dist/types/hud/crosshair.d.ts +7 -0
  17. package/dist/types/hud/crosshair.d.ts.map +1 -0
  18. package/dist/types/hud/damage.d.ts +5 -0
  19. package/dist/types/hud/damage.d.ts.map +1 -0
  20. package/dist/types/hud/diagnostics.d.ts +3 -0
  21. package/dist/types/hud/diagnostics.d.ts.map +1 -0
  22. package/dist/types/hud/icons.d.ts +12 -0
  23. package/dist/types/hud/icons.d.ts.map +1 -0
  24. package/dist/types/hud/layout.d.ts +30 -0
  25. package/dist/types/hud/layout.d.ts.map +1 -0
  26. package/dist/types/hud/messages.d.ts +15 -0
  27. package/dist/types/hud/messages.d.ts.map +1 -0
  28. package/dist/types/hud/numbers.d.ts +3 -0
  29. package/dist/types/hud/numbers.d.ts.map +1 -0
  30. package/dist/types/hud/pickup.d.ts +4 -0
  31. package/dist/types/hud/pickup.d.ts.map +1 -0
  32. package/dist/types/hud/statusbar.d.ts +16 -0
  33. package/dist/types/hud/statusbar.d.ts.map +1 -0
  34. package/dist/types/hud/subtitles.d.ts +7 -0
  35. package/dist/types/hud/subtitles.d.ts.map +1 -0
  36. package/dist/types/hud/types.d.ts +39 -0
  37. package/dist/types/hud/types.d.ts.map +1 -0
  38. package/dist/types/index.d.ts +21 -0
  39. package/dist/types/index.d.ts.map +1 -0
  40. package/dist/types/parse.d.ts +7 -0
  41. package/dist/types/parse.d.ts.map +1 -0
  42. package/dist/types/prediction/index.d.ts +53 -0
  43. package/dist/types/prediction/index.d.ts.map +1 -0
  44. package/dist/types/screen.d.ts +64 -0
  45. package/dist/types/screen.d.ts.map +1 -0
  46. package/dist/types/types.d.ts +109 -0
  47. package/dist/types/types.d.ts.map +1 -0
  48. package/dist/types/view/camera.d.ts +4 -0
  49. package/dist/types/view/camera.d.ts.map +1 -0
  50. package/dist/types/view/effects.d.ts +42 -0
  51. package/dist/types/view/effects.d.ts.map +1 -0
  52. package/package.json +56 -0
@@ -0,0 +1,940 @@
1
+ import { angleVectors, clampViewAngles, ZERO_VEC3, hasPmFlag, PmFlag, dotVec3, PmType, WaterLevel, subtractVec3, lengthVec3, scaleVec3, angleMod, applyPmove, PlayerStat, PowerupId, G_GetPowerupStat, WEAPON_WHEEL_ORDER, WEAPON_AMMO_MAP, G_GetAmmoStat, ConfigStringIndex, MAX_MODELS, MAX_SOUNDS, MAX_IMAGES } from '@quake2ts/shared';
2
+ import { vec3 } from 'gl-matrix';
3
+
4
+ // src/index.ts
5
+
6
+ // src/hud/crosshair.ts
7
+ var crosshairPic = null;
8
+ var crosshairColor = [1, 1, 1, 1];
9
+ var CROSSHAIR_NAMES = ["ch1", "ch2", "ch3"];
10
+ var crosshairPics = [null, null, null];
11
+ var Init_Crosshair = (cgi3) => {
12
+ for (let i = 0; i < CROSSHAIR_NAMES.length; i++) {
13
+ const name = CROSSHAIR_NAMES[i];
14
+ try {
15
+ crosshairPics[i] = cgi3.Draw_RegisterPic(`pics/${name}.pcx`);
16
+ } catch (e) {
17
+ if (i === 0) {
18
+ try {
19
+ crosshairPics[i] = cgi3.Draw_RegisterPic("pics/crosshair.pcx");
20
+ } catch (e2) {
21
+ cgi3.Com_Print("Failed to load crosshair image\n");
22
+ }
23
+ }
24
+ }
25
+ }
26
+ crosshairPic = crosshairPics[0];
27
+ };
28
+ var Draw_Crosshair = (cgi3, width, height) => {
29
+ if (crosshairPic) {
30
+ const size = cgi3.Draw_GetPicSize(crosshairPic);
31
+ const x = (width - size.width) / 2;
32
+ const y = (height - size.height) / 2;
33
+ cgi3.SCR_DrawColorPic(x, y, crosshairPic, { x: crosshairColor[0], y: crosshairColor[1], z: crosshairColor[2] }, crosshairColor[3]);
34
+ }
35
+ };
36
+
37
+ // src/hud/icons.ts
38
+ var iconPics = /* @__PURE__ */ new Map();
39
+ var ICON_NAMES = [
40
+ // Weapons
41
+ "w_blaster",
42
+ "w_shotgun",
43
+ "w_sshotgun",
44
+ "w_machinegun",
45
+ "w_chaingun",
46
+ "w_glauncher",
47
+ "w_rlauncher",
48
+ "w_hyperblaster",
49
+ "w_railgun",
50
+ "w_bfg",
51
+ "w_grapple",
52
+ // Ammo
53
+ "a_grenades",
54
+ "a_bullets",
55
+ "a_cells",
56
+ "a_rockets",
57
+ "a_shells",
58
+ "a_slugs",
59
+ // Powerups
60
+ "p_quad",
61
+ "p_invulnerability",
62
+ "p_silencer",
63
+ "p_rebreather",
64
+ "p_envirosuit",
65
+ "p_adrenaline",
66
+ "p_megahealth",
67
+ // Armor
68
+ "i_jacketarmor",
69
+ "i_combatarmor",
70
+ "i_bodyarmor",
71
+ "i_powerscreen",
72
+ "i_powershield",
73
+ // Keys
74
+ "k_datacd",
75
+ "k_powercube",
76
+ "k_pyramid",
77
+ "k_dataspin",
78
+ "k_security",
79
+ "k_bluekey",
80
+ "k_redkey"
81
+ ];
82
+ var Init_Icons = (cgi3) => {
83
+ for (const name of ICON_NAMES) {
84
+ try {
85
+ const pic = cgi3.Draw_RegisterPic(`pics/${name}.pcx`);
86
+ iconPics.set(name, pic);
87
+ } catch (e) {
88
+ cgi3.Com_Print(`Failed to load HUD image: pics/${name}.pcx
89
+ `);
90
+ }
91
+ }
92
+ };
93
+ var damagePics = /* @__PURE__ */ new Map();
94
+ var DAMAGE_INDICATOR_NAMES = [
95
+ "d_left",
96
+ "d_right",
97
+ "d_up",
98
+ "d_down"
99
+ ];
100
+ var Init_Damage = (cgi3) => {
101
+ for (const name of DAMAGE_INDICATOR_NAMES) {
102
+ try {
103
+ const pic = cgi3.Draw_RegisterPic(`pics/${name}.pcx`);
104
+ damagePics.set(name, pic);
105
+ } catch (e) {
106
+ cgi3.Com_Print(`Failed to load HUD image: pics/${name}.pcx
107
+ `);
108
+ }
109
+ }
110
+ };
111
+ var WHITE = { x: 1, y: 1, z: 1 };
112
+ var Draw_Damage = (cgi3, ps, width, height) => {
113
+ if ((!ps.damageAlpha || ps.damageAlpha <= 0) && (!ps.damageIndicators || ps.damageIndicators.length === 0)) {
114
+ return;
115
+ }
116
+ if (!ps.damageIndicators || ps.damageIndicators.length === 0) {
117
+ return;
118
+ }
119
+ const cx = width * 0.5;
120
+ const cy = height * 0.5;
121
+ const { forward, right } = angleVectors(ps.viewAngles);
122
+ const radius = Math.min(width, height) * 0.25;
123
+ for (const indicator of ps.damageIndicators) {
124
+ const localRight = dotVec3(indicator.direction, right);
125
+ const localForward = dotVec3(indicator.direction, forward);
126
+ const angle = Math.atan2(localForward, localRight) * (180 / Math.PI);
127
+ let picName = "";
128
+ let xOff = 0;
129
+ let yOff = 0;
130
+ if (angle >= 45 && angle < 135) {
131
+ picName = "d_up";
132
+ yOff = -radius;
133
+ } else if (angle >= -45 && angle < 45) {
134
+ picName = "d_right";
135
+ xOff = radius;
136
+ } else if (angle >= -135 && angle < -45) {
137
+ picName = "d_down";
138
+ yOff = radius;
139
+ } else {
140
+ picName = "d_left";
141
+ xOff = -radius;
142
+ }
143
+ const pic = damagePics.get(picName);
144
+ if (pic) {
145
+ const size = cgi3.Draw_GetPicSize(pic);
146
+ const x = cx + xOff - size.width * 0.5;
147
+ const y = cy + yOff - size.height * 0.5;
148
+ const alpha = Math.max(0, Math.min(1, indicator.strength));
149
+ cgi3.SCR_DrawColorPic(x, y, pic, WHITE, alpha);
150
+ }
151
+ }
152
+ };
153
+
154
+ // src/hud/messages.ts
155
+ var CENTER_PRINT_DURATION = 3e3;
156
+ var NOTIFY_DURATION = 5e3;
157
+ var MAX_NOTIFY_MESSAGES = 4;
158
+ var MessageSystem = class {
159
+ constructor() {
160
+ this.centerPrintMsg = null;
161
+ this.notifyMessages = [];
162
+ }
163
+ addCenterPrint(text, now) {
164
+ this.centerPrintMsg = {
165
+ text,
166
+ startTime: now,
167
+ duration: CENTER_PRINT_DURATION
168
+ };
169
+ }
170
+ addNotify(text, now) {
171
+ this.notifyMessages.push({
172
+ text,
173
+ startTime: now,
174
+ duration: NOTIFY_DURATION
175
+ });
176
+ if (this.notifyMessages.length > MAX_NOTIFY_MESSAGES) {
177
+ this.notifyMessages.shift();
178
+ }
179
+ }
180
+ // Additional methods for cgame API
181
+ setCenterPrint(text, now) {
182
+ this.centerPrintMsg = {
183
+ text,
184
+ startTime: now,
185
+ duration: CENTER_PRINT_DURATION
186
+ };
187
+ }
188
+ addNotification(text, is_chat, now) {
189
+ this.notifyMessages.push({
190
+ text,
191
+ startTime: now,
192
+ duration: NOTIFY_DURATION
193
+ });
194
+ if (this.notifyMessages.length > MAX_NOTIFY_MESSAGES) {
195
+ this.notifyMessages.shift();
196
+ }
197
+ }
198
+ clearNotifications() {
199
+ this.notifyMessages = [];
200
+ }
201
+ clearCenterPrint() {
202
+ this.centerPrintMsg = null;
203
+ }
204
+ drawCenterPrint(cgi3, now, layout) {
205
+ if (!this.centerPrintMsg) return;
206
+ if (now > this.centerPrintMsg.startTime + this.centerPrintMsg.duration) {
207
+ this.centerPrintMsg = null;
208
+ return;
209
+ }
210
+ const y = layout.CENTER_PRINT_Y;
211
+ cgi3.SCR_DrawCenterString(y, this.centerPrintMsg.text);
212
+ }
213
+ drawNotifications(cgi3, now) {
214
+ while (this.notifyMessages.length > 0 && now > this.notifyMessages[0].startTime + this.notifyMessages[0].duration) {
215
+ this.notifyMessages.shift();
216
+ }
217
+ let y = 10;
218
+ for (const msg of this.notifyMessages) {
219
+ cgi3.SCR_DrawFontString(10, y, msg.text);
220
+ y += cgi3.SCR_FontLineHeight();
221
+ }
222
+ }
223
+ };
224
+
225
+ // src/hud/subtitles.ts
226
+ var SUBTITLE_DURATION = 3e3;
227
+ var SubtitleSystem = class {
228
+ constructor() {
229
+ this.subtitle = null;
230
+ }
231
+ addSubtitle(text, now) {
232
+ this.subtitle = {
233
+ text,
234
+ startTime: now,
235
+ duration: SUBTITLE_DURATION
236
+ };
237
+ }
238
+ drawSubtitles(cgi3, now) {
239
+ if (!this.subtitle) {
240
+ return;
241
+ }
242
+ if (now > this.subtitle.startTime + this.subtitle.duration) {
243
+ this.subtitle = null;
244
+ return;
245
+ }
246
+ const y = 200;
247
+ cgi3.SCR_DrawCenterString(y, this.subtitle.text);
248
+ }
249
+ };
250
+
251
+ // src/hud/blends.ts
252
+ var Draw_Blends = (cgi3, ps, width, height) => {
253
+ if (!ps.blend) return;
254
+ const [r, g, b, a] = ps.blend;
255
+ };
256
+
257
+ // src/hud/pickup.ts
258
+ var Draw_Pickup = (cgi3, ps, width, height) => {
259
+ if (!ps.pickupIcon) return;
260
+ const icon = iconPics.get(ps.pickupIcon);
261
+ if (icon) {
262
+ const size = cgi3.Draw_GetPicSize(icon);
263
+ const x = width - size.width - 10;
264
+ const y = height - size.height - 10;
265
+ cgi3.SCR_DrawPic(x, y, icon);
266
+ }
267
+ };
268
+
269
+ // src/hud/numbers.ts
270
+ var Draw_Number = (cgi3, x, y, value, pics, width, color) => {
271
+ const s = Math.abs(value).toString();
272
+ for (let i = 0; i < s.length; i++) {
273
+ const digit = parseInt(s[i]);
274
+ const pic = pics[digit];
275
+ if (pic) {
276
+ if (color) {
277
+ cgi3.SCR_DrawColorPic(x + i * width, y, pic, { x: color[0], y: color[1], z: color[2] }, color[3]);
278
+ } else {
279
+ cgi3.SCR_DrawPic(x + i * width, y, pic);
280
+ }
281
+ }
282
+ }
283
+ };
284
+ var Draw_StatusBar = (cgi3, ps, hudNumberPics2, numberWidth2, timeMs, layout) => {
285
+ const health = ps.stats[PlayerStat.STAT_HEALTH] || 0;
286
+ const armor = ps.stats[PlayerStat.STAT_ARMOR] || 0;
287
+ const ammo = ps.stats[PlayerStat.STAT_AMMO] || 0;
288
+ const armorIconIdx = ps.stats[PlayerStat.STAT_ARMOR_ICON] || 0;
289
+ let healthColor = void 0;
290
+ if (health <= 25) {
291
+ {
292
+ healthColor = [1, 0, 0, 1];
293
+ }
294
+ }
295
+ if (hudNumberPics2.length > 0) {
296
+ Draw_Number(cgi3, layout.HEALTH_X, layout.HEALTH_Y, health, hudNumberPics2, numberWidth2, healthColor);
297
+ Draw_Number(cgi3, layout.ARMOR_X, layout.ARMOR_Y, armor, hudNumberPics2, numberWidth2);
298
+ Draw_Number(cgi3, layout.AMMO_X, layout.AMMO_Y, ammo, hudNumberPics2, numberWidth2);
299
+ }
300
+ if (armorIconIdx > 0) {
301
+ const iconName = cgi3.get_configstring(ConfigStringIndex.Images + armorIconIdx);
302
+ if (iconName) {
303
+ const icon = cgi3.Draw_RegisterPic(iconName);
304
+ if (icon) {
305
+ cgi3.SCR_DrawPic(layout.ARMOR_X - 24, layout.ARMOR_Y - 2, icon);
306
+ }
307
+ }
308
+ }
309
+ if (ps.pickupIcon) {
310
+ const icon = cgi3.Draw_RegisterPic(ps.pickupIcon);
311
+ if (icon) {
312
+ cgi3.SCR_DrawPic(layout.WEAPON_ICON_X, layout.WEAPON_ICON_Y, icon);
313
+ }
314
+ } else {
315
+ const selectedIconIdx = ps.stats[PlayerStat.STAT_SELECTED_ICON] || 0;
316
+ if (selectedIconIdx > 0) {
317
+ const iconName = cgi3.get_configstring(ConfigStringIndex.Images + selectedIconIdx);
318
+ if (iconName) {
319
+ const icon = cgi3.Draw_RegisterPic(iconName);
320
+ if (icon) {
321
+ cgi3.SCR_DrawPic(layout.WEAPON_ICON_X, layout.WEAPON_ICON_Y, icon);
322
+ }
323
+ }
324
+ }
325
+ }
326
+ };
327
+
328
+ // src/hud/layout.ts
329
+ var REFERENCE_WIDTH = 640;
330
+ var REFERENCE_HEIGHT = 480;
331
+ var getHudLayout = (width, height) => {
332
+ const scaleX = width / REFERENCE_WIDTH;
333
+ const scaleY = height / REFERENCE_HEIGHT;
334
+ const scale = Math.min(scaleX, scaleY);
335
+ return {
336
+ // Status bar numbers - Anchored Bottom-Left / Center / Right
337
+ HEALTH_X: 100 * scale,
338
+ HEALTH_Y: height - (REFERENCE_HEIGHT - 450) * scale,
339
+ ARMOR_X: 200 * scale,
340
+ ARMOR_Y: height - (REFERENCE_HEIGHT - 450) * scale,
341
+ AMMO_X: width - (REFERENCE_WIDTH - 540) * scale,
342
+ // Anchor right? 540 is near right (640)
343
+ AMMO_Y: height - (REFERENCE_HEIGHT - 450) * scale,
344
+ // Center print messages - Center
345
+ CENTER_PRINT_X: width / 2,
346
+ CENTER_PRINT_Y: 100 * scale,
347
+ // Top anchor
348
+ // Weapon and powerup icons
349
+ WEAPON_ICON_X: 10 * scale,
350
+ WEAPON_ICON_Y: height - (REFERENCE_HEIGHT - 450) * scale,
351
+ POWERUP_X: width - (REFERENCE_WIDTH - 610) * scale,
352
+ POWERUP_Y: height - (REFERENCE_HEIGHT - 450) * scale,
353
+ scale
354
+ };
355
+ };
356
+
357
+ // src/screen.ts
358
+ var cgi = null;
359
+ var hudNumberPics = [];
360
+ var numberWidth = 0;
361
+ var messageSystem = new MessageSystem();
362
+ var subtitleSystem = new SubtitleSystem();
363
+ function CG_InitScreen(imports) {
364
+ cgi = imports;
365
+ }
366
+ function CG_TouchPics() {
367
+ if (!cgi) return;
368
+ hudNumberPics.length = 0;
369
+ for (let i = 0; i < 10; i++) {
370
+ try {
371
+ const pic = cgi.Draw_RegisterPic(`pics/hud/num_${i}.pcx`);
372
+ hudNumberPics.push(pic);
373
+ if (i === 0) {
374
+ const size = cgi.Draw_GetPicSize(pic);
375
+ numberWidth = size.width;
376
+ }
377
+ } catch (e) {
378
+ cgi.Com_Print(`Warning: Failed to load HUD image: pics/hud/num_${i}.pcx
379
+ `);
380
+ }
381
+ }
382
+ Init_Crosshair(cgi);
383
+ Init_Icons(cgi);
384
+ Init_Damage(cgi);
385
+ }
386
+ function CG_DrawHUD(isplit, data, hud_vrect, hud_safe, scale, playernum, ps) {
387
+ if (!cgi) {
388
+ console.error("CG_DrawHUD: cgame imports not initialized");
389
+ return;
390
+ }
391
+ const timeMs = cgi.CL_ClientTime();
392
+ const layout = getHudLayout(hud_vrect.width, hud_vrect.height);
393
+ Draw_Blends(cgi, ps, hud_vrect.width, hud_vrect.height);
394
+ Draw_StatusBar(cgi, ps, hudNumberPics, numberWidth, timeMs, layout);
395
+ Draw_Pickup(cgi, ps, hud_vrect.width, hud_vrect.height);
396
+ Draw_Damage(cgi, ps, hud_vrect.width, hud_vrect.height);
397
+ if (ps.centerPrint) {
398
+ const lines = ps.centerPrint.split("\n");
399
+ let y = hud_vrect.height / 2 - lines.length * 10;
400
+ for (const line of lines) {
401
+ cgi.SCR_DrawCenterString(y, line);
402
+ y += 16;
403
+ }
404
+ }
405
+ messageSystem.drawCenterPrint(cgi, timeMs, layout);
406
+ messageSystem.drawNotifications(cgi, timeMs);
407
+ subtitleSystem.drawSubtitles(cgi, timeMs);
408
+ Draw_Crosshair(cgi, hud_vrect.width, hud_vrect.height);
409
+ }
410
+ function CG_GetMessageSystem() {
411
+ return messageSystem;
412
+ }
413
+ function CG_GetSubtitleSystem() {
414
+ return subtitleSystem;
415
+ }
416
+ function CG_ParseConfigString(cgi3, i, s) {
417
+ if (i >= ConfigStringIndex.Models && i < ConfigStringIndex.Models + MAX_MODELS) {
418
+ cgi3.RegisterModel(s);
419
+ } else if (i >= ConfigStringIndex.Sounds && i < ConfigStringIndex.Sounds + MAX_SOUNDS) {
420
+ cgi3.RegisterSound(s);
421
+ } else if (i >= ConfigStringIndex.Images && i < ConfigStringIndex.Images + MAX_IMAGES) {
422
+ cgi3.Draw_RegisterPic(s);
423
+ }
424
+ }
425
+ var toGlMatrixVec3 = (v) => {
426
+ return vec3.fromValues(v.x, v.y, v.z);
427
+ };
428
+ var updateCamera = (camera, viewSample) => {
429
+ camera.bobAngles = toGlMatrixVec3(viewSample.angles);
430
+ camera.bobOffset = toGlMatrixVec3(viewSample.offset);
431
+ };
432
+ var DEFAULT_SETTINGS = {
433
+ runPitch: 2e-3,
434
+ runRoll: 0.01,
435
+ bobUp: 5e-3,
436
+ bobPitch: 2e-3,
437
+ bobRoll: 2e-3,
438
+ maxBobHeight: 6,
439
+ maxBobAngle: 1.2
440
+ };
441
+ function clampViewOffset(offset) {
442
+ return {
443
+ x: Math.max(-14, Math.min(14, offset.x)),
444
+ y: Math.max(-14, Math.min(14, offset.y)),
445
+ z: Math.max(-22, Math.min(30, offset.z))
446
+ };
447
+ }
448
+ function computeBobMove(xyspeed, onGround, frameTimeMs) {
449
+ if (!onGround) return 0;
450
+ if (xyspeed > 210) return frameTimeMs / 400;
451
+ if (xyspeed > 100) return frameTimeMs / 800;
452
+ return frameTimeMs / 1600;
453
+ }
454
+ function computeBobValues(previousBobTime, xyspeed, pmFlags, onGround, frameTimeMs) {
455
+ if (xyspeed < 5) {
456
+ return { bobTime: 0, bobCycle: 0, bobCycleRun: 0, bobFracSin: 0 };
457
+ }
458
+ const bobMove = computeBobMove(xyspeed, onGround, frameTimeMs);
459
+ const bobTimeRun = previousBobTime + bobMove;
460
+ const crouched = hasPmFlag(pmFlags, PmFlag.Ducked) && onGround;
461
+ const bobTime = crouched ? bobTimeRun * 4 : bobTimeRun;
462
+ return {
463
+ bobTime: bobTimeRun,
464
+ bobCycle: Math.floor(bobTime),
465
+ bobCycleRun: Math.floor(bobTimeRun),
466
+ bobFracSin: Math.abs(Math.sin(bobTime * Math.PI))
467
+ };
468
+ }
469
+ var ViewEffects = class {
470
+ constructor(settings = {}) {
471
+ this.bobTime = 0;
472
+ this.bobCycle = 0;
473
+ this.bobCycleRun = 0;
474
+ this.bobFracSin = 0;
475
+ this.settings = { ...DEFAULT_SETTINGS, ...settings };
476
+ }
477
+ addKick(kick) {
478
+ if (kick.durationMs <= 0) return;
479
+ this.kick = { ...kick, remainingMs: kick.durationMs };
480
+ }
481
+ get last() {
482
+ return this.lastSample;
483
+ }
484
+ sample(state, frameTimeMs) {
485
+ const { forward, right } = angleVectors(
486
+ clampViewAngles({ pmFlags: state.pmFlags, cmdAngles: state.viewAngles, deltaAngles: state.deltaAngles ?? ZERO_VEC3 }).viewangles
487
+ );
488
+ const xyspeed = Math.sqrt(state.velocity.x * state.velocity.x + state.velocity.y * state.velocity.y);
489
+ const onGround = hasPmFlag(state.pmFlags, PmFlag.OnGround);
490
+ const bobValues = computeBobValues(this.bobTime, xyspeed, state.pmFlags, onGround, frameTimeMs);
491
+ this.bobTime = bobValues.bobTime;
492
+ this.bobCycle = bobValues.bobCycle;
493
+ this.bobCycleRun = bobValues.bobCycleRun;
494
+ this.bobFracSin = bobValues.bobFracSin;
495
+ let pitchTilt = dotVec3(state.velocity, forward) * this.settings.runPitch;
496
+ const side = dotVec3(state.velocity, right);
497
+ const sign = side < 0 ? -1 : 1;
498
+ const absSide = Math.abs(side);
499
+ let rollTilt = absSide * this.settings.runRoll;
500
+ if (rollTilt > 2) {
501
+ rollTilt = 2;
502
+ }
503
+ rollTilt *= sign;
504
+ let pitchDelta = this.bobFracSin * this.settings.bobPitch * xyspeed;
505
+ let rollDelta = this.bobFracSin * this.settings.bobRoll * xyspeed;
506
+ if (hasPmFlag(state.pmFlags, PmFlag.Ducked) && onGround) {
507
+ pitchDelta *= 6;
508
+ rollDelta *= 6;
509
+ }
510
+ pitchTilt += Math.min(pitchDelta, this.settings.maxBobAngle);
511
+ rollDelta = Math.min(rollDelta, this.settings.maxBobAngle);
512
+ if (this.bobCycle & 1) rollDelta = -rollDelta;
513
+ rollTilt += rollDelta;
514
+ const bobHeight = Math.min(this.bobFracSin * xyspeed * this.settings.bobUp, this.settings.maxBobHeight);
515
+ let kickPitch = 0;
516
+ let kickRoll = 0;
517
+ let kickX = 0;
518
+ let kickY = 0;
519
+ let kickZ = 0;
520
+ if (this.kick && this.kick.remainingMs > 0) {
521
+ const ratio = Math.max(0, Math.min(1, this.kick.remainingMs / this.kick.durationMs));
522
+ kickPitch += ratio * this.kick.pitch;
523
+ kickRoll += ratio * this.kick.roll;
524
+ if (this.kick.origin) {
525
+ kickX += ratio * this.kick.origin.x;
526
+ kickY += ratio * this.kick.origin.y;
527
+ kickZ += ratio * this.kick.origin.z;
528
+ }
529
+ this.kick.remainingMs = Math.max(0, this.kick.remainingMs - frameTimeMs);
530
+ if (this.kick.remainingMs === 0) this.kick = void 0;
531
+ }
532
+ const angles = { x: pitchTilt + kickPitch, y: 0, z: rollTilt + kickRoll };
533
+ const offset = { x: kickX, y: kickY, z: bobHeight + kickZ };
534
+ const sample = {
535
+ angles,
536
+ offset: clampViewOffset(offset),
537
+ bobCycle: this.bobCycle,
538
+ bobCycleRun: this.bobCycleRun,
539
+ bobFracSin: this.bobFracSin,
540
+ xyspeed
541
+ };
542
+ this.lastSample = sample;
543
+ return sample;
544
+ }
545
+ };
546
+ var DEFAULTS = {
547
+ pmFriction: 6,
548
+ pmStopSpeed: 100,
549
+ pmAccelerate: 10,
550
+ pmAirAccelerate: 1,
551
+ pmWaterAccelerate: 4,
552
+ pmWaterFriction: 1,
553
+ pmMaxSpeed: 300,
554
+ pmDuckSpeed: 100,
555
+ pmWaterSpeed: 400,
556
+ groundIsSlick: false,
557
+ errorTolerance: 0.1,
558
+ errorSnapThreshold: 10
559
+ };
560
+ var DEFAULT_GRAVITY = 800;
561
+ var ZERO_VEC32 = { x: 0, y: 0, z: 0 };
562
+ var CMD_BACKUP = 64;
563
+ function defaultPredictionState() {
564
+ return {
565
+ origin: ZERO_VEC32,
566
+ velocity: ZERO_VEC32,
567
+ viewAngles: ZERO_VEC32,
568
+ onGround: false,
569
+ // Physics fields
570
+ pmFlags: PmFlag.OnGround,
571
+ pmType: PmType.Normal,
572
+ waterLevel: WaterLevel.None,
573
+ watertype: 0,
574
+ gravity: DEFAULT_GRAVITY,
575
+ deltaAngles: ZERO_VEC32,
576
+ // Bounds
577
+ mins: { x: -16, y: -16, z: -24 },
578
+ maxs: { x: 16, y: 16, z: 32 },
579
+ // Visual/Game fields
580
+ damageAlpha: 0,
581
+ damageIndicators: [],
582
+ blend: [0, 0, 0, 0],
583
+ stats: [],
584
+ kick_angles: ZERO_VEC32,
585
+ kick_origin: ZERO_VEC32,
586
+ gunoffset: ZERO_VEC32,
587
+ gunangles: ZERO_VEC32,
588
+ gunindex: 0,
589
+ // New fields
590
+ pm_time: 0,
591
+ pm_type: PmType.Normal,
592
+ pm_flags: PmFlag.OnGround,
593
+ gun_frame: 0,
594
+ rdflags: 0,
595
+ fov: 90,
596
+ renderfx: 0,
597
+ // Optional fields
598
+ pickupIcon: void 0,
599
+ centerPrint: void 0,
600
+ notify: void 0,
601
+ client: void 0,
602
+ health: 0,
603
+ armor: 0,
604
+ ammo: 0
605
+ };
606
+ }
607
+ function normalizeState(state) {
608
+ if (!state) return defaultPredictionState();
609
+ return {
610
+ ...defaultPredictionState(),
611
+ ...state,
612
+ origin: { ...state.origin },
613
+ velocity: { ...state.velocity },
614
+ viewAngles: { ...state.viewAngles },
615
+ deltaAngles: state.deltaAngles ? { ...state.deltaAngles } : ZERO_VEC32,
616
+ blend: state.blend ? [...state.blend] : [0, 0, 0, 0],
617
+ damageIndicators: state.damageIndicators ? [...state.damageIndicators] : [],
618
+ stats: state.stats ? [...state.stats] : []
619
+ };
620
+ }
621
+ function lerp(a, b, t) {
622
+ return a + (b - a) * t;
623
+ }
624
+ function lerpAngle(a, b, t) {
625
+ let delta = angleMod(b - a);
626
+ if (delta > 180) {
627
+ delta -= 360;
628
+ }
629
+ return angleMod(a + delta * t);
630
+ }
631
+ function interpolatePredictionState(previous, latest, alpha) {
632
+ const clamped = Math.max(0, Math.min(alpha, 1));
633
+ return {
634
+ ...latest,
635
+ // Default to latest for discrete fields
636
+ origin: {
637
+ x: lerp(previous.origin.x, latest.origin.x, clamped),
638
+ y: lerp(previous.origin.y, latest.origin.y, clamped),
639
+ z: lerp(previous.origin.z, latest.origin.z, clamped)
640
+ },
641
+ velocity: {
642
+ x: lerp(previous.velocity.x, latest.velocity.x, clamped),
643
+ y: lerp(previous.velocity.y, latest.velocity.y, clamped),
644
+ z: lerp(previous.velocity.z, latest.velocity.z, clamped)
645
+ },
646
+ viewAngles: {
647
+ x: lerpAngle(previous.viewAngles.x, latest.viewAngles.x, clamped),
648
+ y: lerpAngle(previous.viewAngles.y, latest.viewAngles.y, clamped),
649
+ z: lerpAngle(previous.viewAngles.z, latest.viewAngles.z, clamped)
650
+ },
651
+ damageAlpha: lerp(previous.damageAlpha, latest.damageAlpha, clamped),
652
+ blend: [
653
+ lerp(previous.blend[0], latest.blend[0], clamped),
654
+ lerp(previous.blend[1], latest.blend[1], clamped),
655
+ lerp(previous.blend[2], latest.blend[2], clamped),
656
+ lerp(previous.blend[3], latest.blend[3], clamped)
657
+ ],
658
+ // Interpolate health/armor for smooth HUD? Usually step.
659
+ health: lerp(previous.health || 0, latest.health || 0, clamped),
660
+ armor: lerp(previous.armor || 0, latest.armor || 0, clamped),
661
+ ammo: lerp(previous.ammo || 0, latest.ammo || 0, clamped)
662
+ };
663
+ }
664
+ function simulateCommand(state, cmd, settings, trace, pointContents) {
665
+ const pmoveCmd = {
666
+ forwardmove: cmd.forwardmove,
667
+ sidemove: cmd.sidemove,
668
+ upmove: cmd.upmove,
669
+ buttons: cmd.buttons,
670
+ angles: cmd.angles
671
+ // Added missing property
672
+ };
673
+ const newState = applyPmove(state, pmoveCmd, trace, pointContents);
674
+ const { viewangles } = clampViewAngles({
675
+ pmFlags: state.pmFlags,
676
+ cmdAngles: cmd.angles,
677
+ deltaAngles: state.deltaAngles ?? ZERO_VEC32
678
+ });
679
+ return {
680
+ ...newState,
681
+ viewAngles: viewangles
682
+ };
683
+ }
684
+ var ClientPrediction = class {
685
+ constructor(physics, settings = {}) {
686
+ this.enabled = true;
687
+ this.baseFrame = {
688
+ frame: 0,
689
+ timeMs: 0,
690
+ state: defaultPredictionState()
691
+ };
692
+ this.commands = [];
693
+ this.predicted = defaultPredictionState();
694
+ this.predictionError = ZERO_VEC32;
695
+ this.settings = { ...DEFAULTS, ...settings };
696
+ this.physics = physics;
697
+ this.predicted = this.baseFrame.state ?? defaultPredictionState();
698
+ }
699
+ setPredictionEnabled(enabled) {
700
+ this.enabled = enabled;
701
+ }
702
+ setAuthoritative(frame) {
703
+ const normalized = normalizeState(frame.state);
704
+ if (frame.frame <= this.baseFrame.frame) {
705
+ return this.predicted;
706
+ }
707
+ if (this.enabled) {
708
+ let predictedAtFrame;
709
+ const relevantCommands = this.commands.filter((c) => c.sequence <= frame.frame && c.sequence > this.baseFrame.frame);
710
+ if (relevantCommands.length > 0 || this.baseFrame.frame === frame.frame) {
711
+ let tempState = normalizeState(this.baseFrame.state);
712
+ for (const cmd of relevantCommands) {
713
+ tempState = simulateCommand(tempState, cmd, this.settings, this.physics.trace, this.physics.pointContents);
714
+ }
715
+ predictedAtFrame = tempState;
716
+ }
717
+ if (predictedAtFrame) {
718
+ const error = subtractVec3(predictedAtFrame.origin, normalized.origin);
719
+ const errorLen = lengthVec3(error);
720
+ if (errorLen > this.settings.errorSnapThreshold) {
721
+ this.predictionError = ZERO_VEC32;
722
+ } else if (errorLen > this.settings.errorTolerance) {
723
+ this.predictionError = error;
724
+ } else {
725
+ this.predictionError = ZERO_VEC32;
726
+ }
727
+ } else {
728
+ this.predictionError = ZERO_VEC32;
729
+ }
730
+ } else {
731
+ this.predictionError = ZERO_VEC32;
732
+ }
733
+ this.baseFrame = { ...frame, state: normalized };
734
+ this.commands = this.commands.filter((cmd) => cmd.sequence > frame.frame);
735
+ return this.recompute();
736
+ }
737
+ getPredictionError() {
738
+ return this.predictionError;
739
+ }
740
+ // Decay error over time - usually called once per client frame
741
+ decayError(frametime) {
742
+ const len = lengthVec3(this.predictionError);
743
+ if (len > 0) {
744
+ const decay = len * 10 * frametime;
745
+ const scale = Math.max(0, len - decay) / len;
746
+ this.predictionError = scaleVec3(this.predictionError, scale);
747
+ }
748
+ }
749
+ enqueueCommand(cmd) {
750
+ this.commands.push(cmd);
751
+ if (this.commands.length > CMD_BACKUP) {
752
+ this.commands.shift();
753
+ }
754
+ return this.recompute();
755
+ }
756
+ getCommand(sequence) {
757
+ return this.commands.find((c) => c.sequence === sequence);
758
+ }
759
+ getPredictedState() {
760
+ return this.predicted;
761
+ }
762
+ recompute() {
763
+ let state = normalizeState(this.baseFrame.state);
764
+ if (this.enabled) {
765
+ for (const cmd of this.commands) {
766
+ state = simulateCommand(state, cmd, this.settings, this.physics.trace, this.physics.pointContents);
767
+ }
768
+ }
769
+ this.predicted = state;
770
+ return state;
771
+ }
772
+ };
773
+
774
+ // src/index.ts
775
+ var cgi2 = null;
776
+ var cg_predict = null;
777
+ function Init() {
778
+ if (!cgi2) {
779
+ console.error("CGame Init: cgame imports not set");
780
+ return;
781
+ }
782
+ cgi2.Com_Print("===== CGame Initialization =====\n");
783
+ CG_InitScreen(cgi2);
784
+ cg_predict = cgi2.Cvar_Get("cg_predict", "1", 0);
785
+ cgi2.Cvar_Get("cg_showmiss", "0", 0);
786
+ cgi2.Com_Print("CGame initialized\n");
787
+ }
788
+ function Shutdown() {
789
+ if (cgi2) {
790
+ cgi2.Com_Print("CGame shutdown\n");
791
+ }
792
+ cgi2 = null;
793
+ }
794
+ function DrawHUD(isplit, data, hud_vrect, hud_safe, scale, playernum, ps) {
795
+ CG_DrawHUD(isplit, data, hud_vrect, hud_safe, scale, playernum, ps);
796
+ }
797
+ function TouchPics() {
798
+ CG_TouchPics();
799
+ }
800
+ function GetLayoutFlags(ps) {
801
+ return 0;
802
+ }
803
+ function GetActiveWeaponWheelWeapon(ps) {
804
+ return ps.stats[PlayerStat.STAT_ACTIVE_WHEEL_WEAPON] ?? 0;
805
+ }
806
+ function GetOwnedWeaponWheelWeapons(ps) {
807
+ const owned = [];
808
+ const bits1 = ps.stats[PlayerStat.STAT_WEAPONS_OWNED_1] || 0;
809
+ const bits2 = ps.stats[PlayerStat.STAT_WEAPONS_OWNED_2] || 0;
810
+ const fullBits = bits1 | bits2 << 16;
811
+ for (let i = 0; i < WEAPON_WHEEL_ORDER.length; i++) {
812
+ if (fullBits & 1 << i) {
813
+ owned.push(i);
814
+ }
815
+ }
816
+ return owned;
817
+ }
818
+ function GetWeaponWheelAmmoCount(ps, weapon) {
819
+ if (weapon < 0 || weapon >= WEAPON_WHEEL_ORDER.length) {
820
+ return 0;
821
+ }
822
+ const weaponId = WEAPON_WHEEL_ORDER[weapon];
823
+ const ammoType = WEAPON_AMMO_MAP[weaponId];
824
+ if (ammoType === null) {
825
+ return -1;
826
+ }
827
+ return G_GetAmmoStat(ps.stats, ammoType);
828
+ }
829
+ function GetPowerupWheelCount(ps) {
830
+ let count = 0;
831
+ const powerups = Object.values(PowerupId);
832
+ for (const id of powerups) {
833
+ if (G_GetPowerupStat(ps.stats, id) > 0) {
834
+ count++;
835
+ }
836
+ }
837
+ return count;
838
+ }
839
+ function GetHitMarkerDamage(ps) {
840
+ return ps.stats[PlayerStat.STAT_HIT_MARKER] ?? 0;
841
+ }
842
+ function Pmove(pmove) {
843
+ const pm = pmove;
844
+ if (!pm || !pm.s || !pm.cmd || !cgi2) {
845
+ return;
846
+ }
847
+ if (cg_predict && cg_predict.value === 0) {
848
+ return;
849
+ }
850
+ const traceAdapter = (start, end, mins, maxs) => {
851
+ const zero = { x: 0, y: 0, z: 0 };
852
+ return cgi2.PM_Trace(
853
+ start,
854
+ end,
855
+ mins || zero,
856
+ maxs || zero
857
+ );
858
+ };
859
+ const pointContentsAdapter = (point) => {
860
+ const zero = { x: 0, y: 0, z: 0 };
861
+ const tr = cgi2.PM_Trace(point, point, zero, zero);
862
+ return tr.contents || 0;
863
+ };
864
+ const newState = applyPmove(pm.s, pm.cmd, traceAdapter, pointContentsAdapter);
865
+ pm.s.origin = newState.origin;
866
+ pm.s.velocity = newState.velocity;
867
+ pm.s.onGround = newState.onGround;
868
+ pm.s.waterLevel = newState.waterLevel;
869
+ }
870
+ function ParseConfigString(i, s) {
871
+ if (!cgi2) return;
872
+ CG_ParseConfigString(cgi2, i, s);
873
+ }
874
+ function ParseCenterPrint(str, isplit, instant) {
875
+ if (!cgi2) return;
876
+ const messageSystem2 = CG_GetMessageSystem();
877
+ messageSystem2.setCenterPrint(str, cgi2.CL_ClientTime());
878
+ }
879
+ function NotifyMessage(isplit, msg, is_chat) {
880
+ if (!cgi2) return;
881
+ const messageSystem2 = CG_GetMessageSystem();
882
+ messageSystem2.addNotification(msg, is_chat, cgi2.CL_ClientTime());
883
+ }
884
+ function ClearNotify(isplit) {
885
+ const messageSystem2 = CG_GetMessageSystem();
886
+ messageSystem2.clearNotifications();
887
+ }
888
+ function ClearCenterprint(isplit) {
889
+ const messageSystem2 = CG_GetMessageSystem();
890
+ messageSystem2.clearCenterPrint();
891
+ }
892
+ function ShowSubtitle(text, soundName) {
893
+ if (!cgi2) return;
894
+ const subtitleSystem2 = CG_GetSubtitleSystem();
895
+ subtitleSystem2.addSubtitle(text, cgi2.CL_ClientTime());
896
+ }
897
+ function GetMonsterFlashOffset(id) {
898
+ return { x: 0, y: 0, z: 0 };
899
+ }
900
+ function GetExtension(name) {
901
+ return null;
902
+ }
903
+ function GetCGameAPI(imports) {
904
+ cgi2 = imports;
905
+ return {
906
+ // Lifecycle
907
+ Init,
908
+ Shutdown,
909
+ // Rendering
910
+ DrawHUD,
911
+ TouchPics,
912
+ // Layout
913
+ LayoutFlags: GetLayoutFlags,
914
+ // Weapon wheel
915
+ GetActiveWeaponWheelWeapon,
916
+ GetOwnedWeaponWheelWeapons,
917
+ GetWeaponWheelAmmoCount,
918
+ GetPowerupWheelCount,
919
+ // Hit markers
920
+ GetHitMarkerDamage,
921
+ // Prediction
922
+ Pmove,
923
+ // Parsing
924
+ ParseConfigString,
925
+ ParseCenterPrint,
926
+ NotifyMessage,
927
+ ShowSubtitle,
928
+ // State management
929
+ ClearNotify,
930
+ ClearCenterprint,
931
+ // Effects
932
+ GetMonsterFlashOffset,
933
+ // Extension
934
+ GetExtension
935
+ };
936
+ }
937
+
938
+ export { ClientPrediction, GetCGameAPI, ViewEffects, defaultPredictionState, interpolatePredictionState, updateCamera };
939
+ //# sourceMappingURL=index.js.map
940
+ //# sourceMappingURL=index.js.map