Debian Codegraph @

code/client/cl_input.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 // cl.input.c -- builds an intended movement command to send to the server
23
24 #include "client.h"
25
26 unsigned frame_msec;
27 int old_com_frameTime;
28
29 /*
30 ===============================================================================
31
32 KEY BUTTONS
33
34 Continuous button event tracking is complicated by the fact that two different
35 input sources (say, mouse button 1 and the control key) can both press the
36 same button, but the button should only be released when both of the
37 pressing key have been released.
38
39 When a key event issues a button command (+forward, +attack, etc), it appends
40 its key number as argv(1) so it can be matched up with the release.
41
42 argv(2) will be set to the time the event happened, which allows exact
43 control even at low framerates when the down and up events may both get qued
44 at the same time.
45
46 ===============================================================================
47 */
48
49
50 kbutton_t in_left, in_right, in_forward, in_back;
51 kbutton_t in_lookup, in_lookdown, in_moveleft, in_moveright;
52 kbutton_t in_strafe, in_speed;
53 kbutton_t in_up, in_down;
54
55 #ifdef USE_VOIP
56 kbutton_t in_voiprecord;
57 #endif
58
59 kbutton_t in_buttons[16];
60
61
62 qboolean in_mlooking;
63
64
65 void IN_MLookDown( void ) {
66 in_mlooking = qtrue;
67 }
68
69 void IN_MLookUp( void ) {
70 in_mlooking = qfalse;
71 if ( !cl_freelook->integer ) {
72 IN_CenterView ();
73 }
74 }
75
76 void IN_KeyDown( kbutton_t *b ) {
77 int k;
78 char *c;
79
80 c = Cmd_Argv(1);
81 if ( c[0] ) {
82 k = atoi(c);
83 } else {
84 k = -1; // typed manually at the console for continuous down
85 }
86
87 if ( k == b->down[0] || k == b->down[1] ) {
88 return; // repeating key
89 }
90
91 if ( !b->down[0] ) {
92 b->down[0] = k;
93 } else if ( !b->down[1] ) {
94 b->down[1] = k;
95 } else {
96 Com_Printf ("Three keys down for a button!\n");
97 return;
98 }
99
100 if ( b->active ) {
101 return; // still down
102 }
103
104 // save timestamp for partial frame summing
105 c = Cmd_Argv(2);
106 b->downtime = atoi(c);
107
108 b->active = qtrue;
109 b->wasPressed = qtrue;
110 }
111
112 void IN_KeyUp( kbutton_t *b ) {
113 int k;
114 char *c;
115 unsigned uptime;
116
117 c = Cmd_Argv(1);
118 if ( c[0] ) {
119 k = atoi(c);
120 } else {
121 // typed manually at the console, assume for unsticking, so clear all
122 b->down[0] = b->down[1] = 0;
123 b->active = qfalse;
124 return;
125 }
126
127 if ( b->down[0] == k ) {
128 b->down[0] = 0;
129 } else if ( b->down[1] == k ) {
130 b->down[1] = 0;
131 } else {
132 return; // key up without coresponding down (menu pass through)
133 }
134 if ( b->down[0] || b->down[1] ) {
135 return; // some other key is still holding it down
136 }
137
138 b->active = qfalse;
139
140 // save timestamp for partial frame summing
141 c = Cmd_Argv(2);
142 uptime = atoi(c);
143 if ( uptime ) {
144 b->msec += uptime - b->downtime;
145 } else {
146 b->msec += frame_msec / 2;
147 }
148
149 b->active = qfalse;
150 }
151
152
153
154 /*
155 ===============
156 CL_KeyState
157
158 Returns the fraction of the frame that the key was down
159 ===============
160 */
161 float CL_KeyState( kbutton_t *key ) {
162 float val;
163 int msec;
164
165 msec = key->msec;
166 key->msec = 0;
167
168 if ( key->active ) {
169 // still down
170 if ( !key->downtime ) {
171 msec = com_frameTime;
172 } else {
173 msec += com_frameTime - key->downtime;
174 }
175 key->downtime = com_frameTime;
176 }
177
178 #if 0
179 if (msec) {
180 Com_Printf ("%i ", msec);
181 }
182 #endif
183
184 val = (float)msec / frame_msec;
185 if ( val < 0 ) {
186 val = 0;
187 }
188 if ( val > 1 ) {
189 val = 1;
190 }
191
192 return val;
193 }
194
195
196
197 void IN_UpDown(void) {IN_KeyDown(&in_up);}
198 void IN_UpUp(void) {IN_KeyUp(&in_up);}
199 void IN_DownDown(void) {IN_KeyDown(&in_down);}
200 void IN_DownUp(void) {IN_KeyUp(&in_down);}
201 void IN_LeftDown(void) {IN_KeyDown(&in_left);}
202 void IN_LeftUp(void) {IN_KeyUp(&in_left);}
203 void IN_RightDown(void) {IN_KeyDown(&in_right);}
204 void IN_RightUp(void) {IN_KeyUp(&in_right);}
205 void IN_ForwardDown(void) {IN_KeyDown(&in_forward);}
206 void IN_ForwardUp(void) {IN_KeyUp(&in_forward);}
207 void IN_BackDown(void) {IN_KeyDown(&in_back);}
208 void IN_BackUp(void) {IN_KeyUp(&in_back);}
209 void IN_LookupDown(void) {IN_KeyDown(&in_lookup);}
210 void IN_LookupUp(void) {IN_KeyUp(&in_lookup);}
211 void IN_LookdownDown(void) {IN_KeyDown(&in_lookdown);}
212 void IN_LookdownUp(void) {IN_KeyUp(&in_lookdown);}
213 void IN_MoveleftDown(void) {IN_KeyDown(&in_moveleft);}
214 void IN_MoveleftUp(void) {IN_KeyUp(&in_moveleft);}
215 void IN_MoverightDown(void) {IN_KeyDown(&in_moveright);}
216 void IN_MoverightUp(void) {IN_KeyUp(&in_moveright);}
217
218 void IN_SpeedDown(void) {IN_KeyDown(&in_speed);}
219 void IN_SpeedUp(void) {IN_KeyUp(&in_speed);}
220 void IN_StrafeDown(void) {IN_KeyDown(&in_strafe);}
221 void IN_StrafeUp(void) {IN_KeyUp(&in_strafe);}
222
223 #ifdef USE_VOIP
224 void IN_VoipRecordDown(void)
225 {
226 IN_KeyDown(&in_voiprecord);
227 Cvar_Set("cl_voipSend", "1");
228 }
229
230 void IN_VoipRecordUp(void)
231 {
232 IN_KeyUp(&in_voiprecord);
233 Cvar_Set("cl_voipSend", "0");
234 }
235 #endif
236
237 void IN_Button0Down(void) {IN_KeyDown(&in_buttons[0]);}
238 void IN_Button0Up(void) {IN_KeyUp(&in_buttons[0]);}
239 void IN_Button1Down(void) {IN_KeyDown(&in_buttons[1]);}
240 void IN_Button1Up(void) {IN_KeyUp(&in_buttons[1]);}
241 void IN_Button2Down(void) {IN_KeyDown(&in_buttons[2]);}
242 void IN_Button2Up(void) {IN_KeyUp(&in_buttons[2]);}
243 void IN_Button3Down(void) {IN_KeyDown(&in_buttons[3]);}
244 void IN_Button3Up(void) {IN_KeyUp(&in_buttons[3]);}
245 void IN_Button4Down(void) {IN_KeyDown(&in_buttons[4]);}
246 void IN_Button4Up(void) {IN_KeyUp(&in_buttons[4]);}
247 void IN_Button5Down(void) {IN_KeyDown(&in_buttons[5]);}
248 void IN_Button5Up(void) {IN_KeyUp(&in_buttons[5]);}
249 void IN_Button6Down(void) {IN_KeyDown(&in_buttons[6]);}
250 void IN_Button6Up(void) {IN_KeyUp(&in_buttons[6]);}
251 void IN_Button7Down(void) {IN_KeyDown(&in_buttons[7]);}
252 void IN_Button7Up(void) {IN_KeyUp(&in_buttons[7]);}
253 void IN_Button8Down(void) {IN_KeyDown(&in_buttons[8]);}
254 void IN_Button8Up(void) {IN_KeyUp(&in_buttons[8]);}
255 void IN_Button9Down(void) {IN_KeyDown(&in_buttons[9]);}
256 void IN_Button9Up(void) {IN_KeyUp(&in_buttons[9]);}
257 void IN_Button10Down(void) {IN_KeyDown(&in_buttons[10]);}
258 void IN_Button10Up(void) {IN_KeyUp(&in_buttons[10]);}
259 void IN_Button11Down(void) {IN_KeyDown(&in_buttons[11]);}
260 void IN_Button11Up(void) {IN_KeyUp(&in_buttons[11]);}
261 void IN_Button12Down(void) {IN_KeyDown(&in_buttons[12]);}
262 void IN_Button12Up(void) {IN_KeyUp(&in_buttons[12]);}
263 void IN_Button13Down(void) {IN_KeyDown(&in_buttons[13]);}
264 void IN_Button13Up(void) {IN_KeyUp(&in_buttons[13]);}
265 void IN_Button14Down(void) {IN_KeyDown(&in_buttons[14]);}
266 void IN_Button14Up(void) {IN_KeyUp(&in_buttons[14]);}
267 void IN_Button15Down(void) {IN_KeyDown(&in_buttons[15]);}
268 void IN_Button15Up(void) {IN_KeyUp(&in_buttons[15]);}
269
270 void IN_CenterView (void) {
271 cl.viewangles[PITCH] = -SHORT2ANGLE(cl.snap.ps.delta_angles[PITCH]);
272 }
273
274
275 //==========================================================================
276
277 cvar_t *cl_yawspeed;
278 cvar_t *cl_pitchspeed;
279
280 cvar_t *cl_run;
281
282 cvar_t *cl_anglespeedkey;
283
284
285 /*
286 ================
287 CL_AdjustAngles
288
289 Moves the local angle positions
290 ================
291 */
292 void CL_AdjustAngles( void ) {
293 float speed;
294
295 if ( in_speed.active ) {
296 speed = 0.001 * cls.frametime * cl_anglespeedkey->value;
297 } else {
298 speed = 0.001 * cls.frametime;
299 }
300
301 if ( !in_strafe.active ) {
302 cl.viewangles[YAW] -= speed*cl_yawspeed->value*CL_KeyState (&in_right);
303 cl.viewangles[YAW] += speed*cl_yawspeed->value*CL_KeyState (&in_left);
304 }
305
306 cl.viewangles[PITCH] -= speed*cl_pitchspeed->value * CL_KeyState (&in_lookup);
307 cl.viewangles[PITCH] += speed*cl_pitchspeed->value * CL_KeyState (&in_lookdown);
308 }
309
310 /*
311 ================
312 CL_KeyMove
313
314 Sets the usercmd_t based on key states
315 ================
316 */
317 void CL_KeyMove( usercmd_t *cmd ) {
318 int movespeed;
319 int forward, side, up;
320
321 //
322 // adjust for speed key / running
323 // the walking flag is to keep animations consistent
324 // even during acceleration and develeration
325 //
326 if ( in_speed.active ^ cl_run->integer ) {
327 movespeed = 127;
328 cmd->buttons &= ~BUTTON_WALKING;
329 } else {
330 cmd->buttons |= BUTTON_WALKING;
331 movespeed = 64;
332 }
333
334 forward = 0;
335 side = 0;
336 up = 0;
337 if ( in_strafe.active ) {
338 side += movespeed * CL_KeyState (&in_right);
339 side -= movespeed * CL_KeyState (&in_left);
340 }
341
342 side += movespeed * CL_KeyState (&in_moveright);
343 side -= movespeed * CL_KeyState (&in_moveleft);
344
345
346 up += movespeed * CL_KeyState (&in_up);
347 up -= movespeed * CL_KeyState (&in_down);
348
349 forward += movespeed * CL_KeyState (&in_forward);
350 forward -= movespeed * CL_KeyState (&in_back);
351
352 cmd->forwardmove = ClampChar( forward );
353 cmd->rightmove = ClampChar( side );
354 cmd->upmove = ClampChar( up );
355 }
356
357 /*
358 =================
359 CL_MouseEvent
360 =================
361 */
362 void CL_MouseEvent( int dx, int dy, int time ) {
363 if ( Key_GetCatcher( ) & KEYCATCH_UI ) {
364 VM_Call( uivm, UI_MOUSE_EVENT, dx, dy );
365 } else if (Key_GetCatcher( ) & KEYCATCH_CGAME) {
366 VM_Call (cgvm, CG_MOUSE_EVENT, dx, dy);
367 } else {
368 cl.mouseDx[cl.mouseIndex] += dx;
369 cl.mouseDy[cl.mouseIndex] += dy;
370 }
371 }
372
373 /*
374 =================
375 CL_JoystickEvent
376
377 Joystick values stay set until changed
378 =================
379 */
380 void CL_JoystickEvent( int axis, int value, int time ) {
381 if ( axis < 0 || axis >= MAX_JOYSTICK_AXIS ) {
382 Com_Error( ERR_DROP, "CL_JoystickEvent: bad axis %i", axis );
383 }
384 cl.joystickAxis[axis] = value;
385 }
386
387 /*
388 =================
389 CL_JoystickMove
390 =================
391 */
392 void CL_JoystickMove( usercmd_t *cmd ) {
393 float anglespeed;
394
395 float yaw = j_yaw->value * cl.joystickAxis[j_yaw_axis->integer];
396 float right = j_side->value * cl.joystickAxis[j_side_axis->integer];
397 float forward = j_forward->value * cl.joystickAxis[j_forward_axis->integer];
398 float pitch = j_pitch->value * cl.joystickAxis[j_pitch_axis->integer];
399 float up = j_up->value * cl.joystickAxis[j_up_axis->integer];
400
401 if ( !(in_speed.active ^ cl_run->integer) ) {
402 cmd->buttons |= BUTTON_WALKING;
403 }
404
405 if ( in_speed.active ) {
406 anglespeed = 0.001 * cls.frametime * cl_anglespeedkey->value;
407 } else {
408 anglespeed = 0.001 * cls.frametime;
409 }
410
411 if ( !in_strafe.active ) {
412 cl.viewangles[YAW] += anglespeed * yaw;
413 cmd->rightmove = ClampChar( cmd->rightmove + (int)right );
414 } else {
415 cl.viewangles[YAW] += anglespeed * right;
416 cmd->rightmove = ClampChar( cmd->rightmove + (int)yaw );
417 }
418
419 if ( in_mlooking ) {
420 cl.viewangles[PITCH] += anglespeed * forward;
421 cmd->forwardmove = ClampChar( cmd->forwardmove + (int)pitch );
422 } else {
423 cl.viewangles[PITCH] += anglespeed * pitch;
424 cmd->forwardmove = ClampChar( cmd->forwardmove + (int)forward );
425 }
426
427 cmd->upmove = ClampChar( cmd->upmove + (int)up );
428 }
429
430 /*
431 =================
432 CL_MouseMove
433 =================
434 */
435
436 void CL_MouseMove(usercmd_t *cmd)
437 {
438 float mx, my;
439
440 // allow mouse smoothing
441 if (m_filter->integer)
442 {
443 mx = (cl.mouseDx[0] + cl.mouseDx[1]) * 0.5f;
444 my = (cl.mouseDy[0] + cl.mouseDy[1]) * 0.5f;
445 }
446 else
447 {
448 mx = cl.mouseDx[cl.mouseIndex];
449 my = cl.mouseDy[cl.mouseIndex];
450 }
451
452 cl.mouseIndex ^= 1;
453 cl.mouseDx[cl.mouseIndex] = 0;
454 cl.mouseDy[cl.mouseIndex] = 0;
455
456 if (mx == 0.0f && my == 0.0f)
457 return;
458
459 if (cl_mouseAccel->value != 0.0f)
460 {
461 if(cl_mouseAccelStyle->integer == 0)
462 {
463 float accelSensitivity;
464 float rate;
465
466 rate = sqrt(mx * mx + my * my) / (float) frame_msec;
467
468 accelSensitivity = cl_sensitivity->value + rate * cl_mouseAccel->value;
469 mx *= accelSensitivity;
470 my *= accelSensitivity;
471
472 if(cl_showMouseRate->integer)
473 Com_Printf("rate: %f, accelSensitivity: %f\n", rate, accelSensitivity);
474 }
475 else
476 {
477 float rate[2];
478 float power[2];
479
480 // sensitivity remains pretty much unchanged at low speeds
481 // cl_mouseAccel is a power value to how the acceleration is shaped
482 // cl_mouseAccelOffset is the rate for which the acceleration will have doubled the non accelerated amplification
483 // NOTE: decouple the config cvars for independent acceleration setup along X and Y?
484
485 rate[0] = fabs(mx) / (float) frame_msec;
486 rate[1] = fabs(my) / (float) frame_msec;
487 power[0] = powf(rate[0] / cl_mouseAccelOffset->value, cl_mouseAccel->value);
488 power[1] = powf(rate[1] / cl_mouseAccelOffset->value, cl_mouseAccel->value);
489
490 mx = cl_sensitivity->value * (mx + ((mx < 0) ? -power[0] : power[0]) * cl_mouseAccelOffset->value);
491 my = cl_sensitivity->value * (my + ((my < 0) ? -power[1] : power[1]) * cl_mouseAccelOffset->value);
492
493 if(cl_showMouseRate->integer)
494 Com_Printf("ratex: %f, ratey: %f, powx: %f, powy: %f\n", rate[0], rate[1], power[0], power[1]);
495 }
496 }
497 else
498 {
499 mx *= cl_sensitivity->value;
500 my *= cl_sensitivity->value;
501 }
502
503 // ingame FOV
504 mx *= cl.cgameSensitivity;
505 my *= cl.cgameSensitivity;
506
507 // add mouse X/Y movement to cmd
508 if(in_strafe.active)
509 cmd->rightmove = ClampChar(cmd->rightmove + m_side->value * mx);
510 else
511 cl.viewangles[YAW] -= m_yaw->value * mx;
512
513 if ((in_mlooking || cl_freelook->integer) && !in_strafe.active)
514 cl.viewangles[PITCH] += m_pitch->value * my;
515 else
516 cmd->forwardmove = ClampChar(cmd->forwardmove - m_forward->value * my);
517 }
518
519
520 /*
521 ==============
522 CL_CmdButtons
523 ==============
524 */
525 void CL_CmdButtons( usercmd_t *cmd ) {
526 int i;
527
528 //
529 // figure button bits
530 // send a button bit even if the key was pressed and released in
531 // less than a frame
532 //
533 for (i = 0 ; i < 15 ; i++) {
534 if ( in_buttons[i].active || in_buttons[i].wasPressed ) {
535 cmd->buttons |= 1 << i;
536 }
537 in_buttons[i].wasPressed = qfalse;
538 }
539
540 if ( Key_GetCatcher( ) ) {
541 cmd->buttons |= BUTTON_TALK;
542 }
543
544 // allow the game to know if any key at all is
545 // currently pressed, even if it isn't bound to anything
546 if ( anykeydown && Key_GetCatcher( ) == 0 ) {
547 cmd->buttons |= BUTTON_ANY;
548 }
549 }
550
551
552 /*
553 ==============
554 CL_FinishMove
555 ==============
556 */
557 void CL_FinishMove( usercmd_t *cmd ) {
558 int i;
559
560 // copy the state that the cgame is currently sending
561 cmd->weapon = cl.cgameUserCmdValue;
562
563 // send the current server time so the amount of movement
564 // can be determined without allowing cheating
565 cmd->serverTime = cl.serverTime;
566
567 for (i=0 ; i<3 ; i++) {
568 cmd->angles[i] = ANGLE2SHORT(cl.viewangles[i]);
569 }
570 }
571
572
573 /*
574 =================
575 CL_CreateCmd
576 =================
577 */
578 usercmd_t CL_CreateCmd( void ) {
579 usercmd_t cmd;
580 vec3_t oldAngles;
581
582 VectorCopy( cl.viewangles, oldAngles );
583
584 // keyboard angle adjustment
585 CL_AdjustAngles ();
586
587 Com_Memset( &cmd, 0, sizeof( cmd ) );
588
589 CL_CmdButtons( &cmd );
590
591 // get basic movement from keyboard
592 CL_KeyMove( &cmd );
593
594 // get basic movement from mouse
595 CL_MouseMove( &cmd );
596
597 // get basic movement from joystick
598 CL_JoystickMove( &cmd );
599
600 // check to make sure the angles haven't wrapped
601 if ( cl.viewangles[PITCH] - oldAngles[PITCH] > 90 ) {
602 cl.viewangles[PITCH] = oldAngles[PITCH] + 90;
603 } else if ( oldAngles[PITCH] - cl.viewangles[PITCH] > 90 ) {
604 cl.viewangles[PITCH] = oldAngles[PITCH] - 90;
605 }
606
607 // store out the final values
608 CL_FinishMove( &cmd );
609
610 // draw debug graphs of turning for mouse testing
611 if ( cl_debugMove->integer ) {
612 if ( cl_debugMove->integer == 1 ) {
613 SCR_DebugGraph( fabs(cl.viewangles[YAW] - oldAngles[YAW]) );
614 }
615 if ( cl_debugMove->integer == 2 ) {
616 SCR_DebugGraph( fabs(cl.viewangles[PITCH] - oldAngles[PITCH]) );
617 }
618 }
619
620 return cmd;
621 }
622
623
624 /*
625 =================
626 CL_CreateNewCommands
627
628 Create a new usercmd_t structure for this frame
629 =================
630 */
631 void CL_CreateNewCommands( void ) {
632 int cmdNum;
633
634 // no need to create usercmds until we have a gamestate
635 if ( clc.state < CA_PRIMED ) {
636 return;
637 }
638
639 frame_msec = com_frameTime - old_com_frameTime;
640
641 // if running over 1000fps, act as if each frame is 1ms
642 // prevents divisions by zero
643 if ( frame_msec < 1 ) {
644 frame_msec = 1;
645 }
646
647 // if running less than 5fps, truncate the extra time to prevent
648 // unexpected moves after a hitch
649 if ( frame_msec > 200 ) {
650 frame_msec = 200;
651 }
652 old_com_frameTime = com_frameTime;
653
654
655 // generate a command for this frame
656 cl.cmdNumber++;
657 cmdNum = cl.cmdNumber & CMD_MASK;
658 cl.cmds[cmdNum] = CL_CreateCmd ();
659 }
660
661 /*
662 =================
663 CL_ReadyToSendPacket
664
665 Returns qfalse if we are over the maxpackets limit
666 and should choke back the bandwidth a bit by not sending
667 a packet this frame. All the commands will still get
668 delivered in the next packet, but saving a header and
669 getting more delta compression will reduce total bandwidth.
670 =================
671 */
672 qboolean CL_ReadyToSendPacket( void ) {
673 int oldPacketNum;
674 int delta;
675
676 // don't send anything if playing back a demo
677 if ( clc.demoplaying || clc.state == CA_CINEMATIC ) {
678 return qfalse;
679 }
680
681 // If we are downloading, we send no less than 50ms between packets
682 if ( *clc.downloadTempName &&
683 cls.realtime - clc.lastPacketSentTime < 50 ) {
684 return qfalse;
685 }
686
687 // if we don't have a valid gamestate yet, only send
688 // one packet a second
689 if ( clc.state != CA_ACTIVE &&
690 clc.state != CA_PRIMED &&
691 !*clc.downloadTempName &&
692 cls.realtime - clc.lastPacketSentTime < 1000 ) {
693 return qfalse;
694 }
695
696 // send every frame for loopbacks
697 if ( clc.netchan.remoteAddress.type == NA_LOOPBACK ) {
698 return qtrue;
699 }
700
701 // send every frame for LAN
702 if ( cl_lanForcePackets->integer && Sys_IsLANAddress( clc.netchan.remoteAddress ) ) {
703 return qtrue;
704 }
705
706 // check for exceeding cl_maxpackets
707 if ( cl_maxpackets->integer < 15 ) {
708 Cvar_Set( "cl_maxpackets", "15" );
709 } else if ( cl_maxpackets->integer > 125 ) {
710 Cvar_Set( "cl_maxpackets", "125" );
711 }
712 oldPacketNum = (clc.netchan.outgoingSequence - 1) & PACKET_MASK;
713 delta = cls.realtime - cl.outPackets[ oldPacketNum ].p_realtime;
714 if ( delta < 1000 / cl_maxpackets->integer ) {
715 // the accumulated commands will go out in the next packet
716 return qfalse;
717 }
718
719 return qtrue;
720 }
721
722 /*
723 ===================
724 CL_WritePacket
725
726 Create and send the command packet to the server
727 Including both the reliable commands and the usercmds
728
729 During normal gameplay, a client packet will contain something like:
730
731 4 sequence number
732 2 qport
733 4 serverid
734 4 acknowledged sequence number
735 4 clc.serverCommandSequence
736 <optional reliable commands>
737 1 clc_move or clc_moveNoDelta
738 1 command count
739 <count * usercmds>
740
741 ===================
742 */
743 void CL_WritePacket( void ) {
744 msg_t buf;
745 byte data[MAX_MSGLEN];
746 int i, j;
747 usercmd_t *cmd, *oldcmd;
748 usercmd_t nullcmd;
749 int packetNum;
750 int oldPacketNum;
751 int count, key;
752
753 // don't send anything if playing back a demo
754 if ( clc.demoplaying || clc.state == CA_CINEMATIC ) {
755 return;
756 }
757
758 Com_Memset( &nullcmd, 0, sizeof(nullcmd) );
759 oldcmd = &nullcmd;
760
761 MSG_Init( &buf, data, sizeof(data) );
762
763 MSG_Bitstream( &buf );
764 // write the current serverId so the server
765 // can tell if this is from the current gameState
766 MSG_WriteLong( &buf, cl.serverId );
767
768 // write the last message we received, which can
769 // be used for delta compression, and is also used
770 // to tell if we dropped a gamestate
771 MSG_WriteLong( &buf, clc.serverMessageSequence );
772
773 // write the last reliable message we received
774 MSG_WriteLong( &buf, clc.serverCommandSequence );
775
776 // write any unacknowledged clientCommands
777 for ( i = clc.reliableAcknowledge + 1 ; i <= clc.reliableSequence ; i++ ) {
778 MSG_WriteByte( &buf, clc_clientCommand );
779 MSG_WriteLong( &buf, i );
780 MSG_WriteString( &buf, clc.reliableCommands[ i & (MAX_RELIABLE_COMMANDS-1) ] );
781 }
782
783 // we want to send all the usercmds that were generated in the last
784 // few packet, so even if a couple packets are dropped in a row,
785 // all the cmds will make it to the server
786 if ( cl_packetdup->integer < 0 ) {
787 Cvar_Set( "cl_packetdup", "0" );
788 } else if ( cl_packetdup->integer > 5 ) {
789 Cvar_Set( "cl_packetdup", "5" );
790 }
791 oldPacketNum = (clc.netchan.outgoingSequence - 1 - cl_packetdup->integer) & PACKET_MASK;
792 count = cl.cmdNumber - cl.outPackets[ oldPacketNum ].p_cmdNumber;
793 if ( count > MAX_PACKET_USERCMDS ) {
794 count = MAX_PACKET_USERCMDS;
795 Com_Printf("MAX_PACKET_USERCMDS\n");
796 }
797
798 #ifdef USE_VOIP
799 if (clc.voipOutgoingDataSize > 0)
800 {
801 if((clc.voipFlags & VOIP_SPATIAL) || Com_IsVoipTarget(clc.voipTargets, sizeof(clc.voipTargets), -1))
802 {
803 MSG_WriteByte (&buf, clc_voipOpus);
804 MSG_WriteByte (&buf, clc.voipOutgoingGeneration);
805 MSG_WriteLong (&buf, clc.voipOutgoingSequence);
806 MSG_WriteByte (&buf, clc.voipOutgoingDataFrames);
807 MSG_WriteData (&buf, clc.voipTargets, sizeof(clc.voipTargets));
808 MSG_WriteByte(&buf, clc.voipFlags);
809 MSG_WriteShort (&buf, clc.voipOutgoingDataSize);
810 MSG_WriteData (&buf, clc.voipOutgoingData, clc.voipOutgoingDataSize);
811
812 // If we're recording a demo, we have to fake a server packet with
813 // this VoIP data so it gets to disk; the server doesn't send it
814 // back to us, and we might as well eliminate concerns about dropped
815 // and misordered packets here.
816 if(clc.demorecording && !clc.demowaiting)
817 {
818 const int voipSize = clc.voipOutgoingDataSize;
819 msg_t fakemsg;
820 byte fakedata[MAX_MSGLEN];
821 MSG_Init (&fakemsg, fakedata, sizeof (fakedata));
822 MSG_Bitstream (&fakemsg);
823 MSG_WriteLong (&fakemsg, clc.reliableAcknowledge);
824 MSG_WriteByte (&fakemsg, svc_voipOpus);
825 MSG_WriteShort (&fakemsg, clc.clientNum);
826 MSG_WriteByte (&fakemsg, clc.voipOutgoingGeneration);
827 MSG_WriteLong (&fakemsg, clc.voipOutgoingSequence);
828 MSG_WriteByte (&fakemsg, clc.voipOutgoingDataFrames);
829 MSG_WriteShort (&fakemsg, clc.voipOutgoingDataSize );
830 MSG_WriteBits (&fakemsg, clc.voipFlags, VOIP_FLAGCNT);
831 MSG_WriteData (&fakemsg, clc.voipOutgoingData, voipSize);
832 MSG_WriteByte (&fakemsg, svc_EOF);
833 CL_WriteDemoMessage (&fakemsg, 0);
834 }
835
836 clc.voipOutgoingSequence += clc.voipOutgoingDataFrames;
837 clc.voipOutgoingDataSize = 0;
838 clc.voipOutgoingDataFrames = 0;
839 }
840 else
841 {
842 // We have data, but no targets. Silently discard all data
843 clc.voipOutgoingDataSize = 0;
844 clc.voipOutgoingDataFrames = 0;
845 }
846 }
847 #endif
848
849 if ( count >= 1 ) {
850 if ( cl_showSend->integer ) {
851 Com_Printf( "(%i)", count );
852 }
853
854 // begin a client move command
855 if ( cl_nodelta->integer || !cl.snap.valid || clc.demowaiting
856 || clc.serverMessageSequence != cl.snap.messageNum ) {
857 MSG_WriteByte (&buf, clc_moveNoDelta);
858 } else {
859 MSG_WriteByte (&buf, clc_move);
860 }
861
862 // write the command count
863 MSG_WriteByte( &buf, count );
864
865 // use the checksum feed in the key
866 key = clc.checksumFeed;
867 // also use the message acknowledge
868 key ^= clc.serverMessageSequence;
869 // also use the last acknowledged server command in the key
870 key ^= MSG_HashKey(clc.serverCommands[ clc.serverCommandSequence & (MAX_RELIABLE_COMMANDS-1) ], 32);
871
872 // write all the commands, including the predicted command
873 for ( i = 0 ; i < count ; i++ ) {
874 j = (cl.cmdNumber - count + i + 1) & CMD_MASK;
875 cmd = &cl.cmds[j];
876 MSG_WriteDeltaUsercmdKey (&buf, key, oldcmd, cmd);
877 oldcmd = cmd;
878 }
879 }
880
881 //
882 // deliver the message
883 //
884 packetNum = clc.netchan.outgoingSequence & PACKET_MASK;
885 cl.outPackets[ packetNum ].p_realtime = cls.realtime;
886 cl.outPackets[ packetNum ].p_serverTime = oldcmd->serverTime;
887 cl.outPackets[ packetNum ].p_cmdNumber = cl.cmdNumber;
888 clc.lastPacketSentTime = cls.realtime;
889
890 if ( cl_showSend->integer ) {
891 Com_Printf( "%i ", buf.cursize );
892 }
893
894 CL_Netchan_Transmit (&clc.netchan, &buf);
895 }
896
897 /*
898 =================
899 CL_SendCmd
900
901 Called every frame to builds and sends a command packet to the server.
902 =================
903 */
904 void CL_SendCmd( void ) {
905 // don't send any message if not connected
906 if ( clc.state < CA_CONNECTED ) {
907 return;
908 }
909
910 // don't send commands if paused
911 if ( com_sv_running->integer && sv_paused->integer && cl_paused->integer ) {
912 return;
913 }
914
915 // we create commands even if a demo is playing,
916 CL_CreateNewCommands();
917
918 // don't send a packet if the last packet was sent too recently
919 if ( !CL_ReadyToSendPacket() ) {
920 if ( cl_showSend->integer ) {
921 Com_Printf( ". " );
922 }
923 return;
924 }
925
926 CL_WritePacket();
927 }
928
929 /*
930 ============
931 CL_InitInput
932 ============
933 */
934 void CL_InitInput( void ) {
935 Cmd_AddCommand ("centerview",IN_CenterView);
936
937 Cmd_AddCommand ("+moveup",IN_UpDown);
938 Cmd_AddCommand ("-moveup",IN_UpUp);
939 Cmd_AddCommand ("+movedown",IN_DownDown);
940 Cmd_AddCommand ("-movedown",IN_DownUp);
941 Cmd_AddCommand ("+left",IN_LeftDown);
942 Cmd_AddCommand ("-left",IN_LeftUp);
943 Cmd_AddCommand ("+right",IN_RightDown);
944 Cmd_AddCommand ("-right",IN_RightUp);
945 Cmd_AddCommand ("+forward",IN_ForwardDown);
946 Cmd_AddCommand ("-forward",IN_ForwardUp);
947 Cmd_AddCommand ("+back",IN_BackDown);
948 Cmd_AddCommand ("-back",IN_BackUp);
949 Cmd_AddCommand ("+lookup", IN_LookupDown);
950 Cmd_AddCommand ("-lookup", IN_LookupUp);
951 Cmd_AddCommand ("+lookdown", IN_LookdownDown);
952 Cmd_AddCommand ("-lookdown", IN_LookdownUp);
953 Cmd_AddCommand ("+strafe", IN_StrafeDown);
954 Cmd_AddCommand ("-strafe", IN_StrafeUp);
955 Cmd_AddCommand ("+moveleft", IN_MoveleftDown);
956 Cmd_AddCommand ("-moveleft", IN_MoveleftUp);
957 Cmd_AddCommand ("+moveright", IN_MoverightDown);
958 Cmd_AddCommand ("-moveright", IN_MoverightUp);
959 Cmd_AddCommand ("+speed", IN_SpeedDown);
960 Cmd_AddCommand ("-speed", IN_SpeedUp);
961 Cmd_AddCommand ("+attack", IN_Button0Down);
962 Cmd_AddCommand ("-attack", IN_Button0Up);
963 Cmd_AddCommand ("+button0", IN_Button0Down);
964 Cmd_AddCommand ("-button0", IN_Button0Up);
965 Cmd_AddCommand ("+button1", IN_Button1Down);
966 Cmd_AddCommand ("-button1", IN_Button1Up);
967 Cmd_AddCommand ("+button2", IN_Button2Down);
968 Cmd_AddCommand ("-button2", IN_Button2Up);
969 Cmd_AddCommand ("+button3", IN_Button3Down);
970 Cmd_AddCommand ("-button3", IN_Button3Up);
971 Cmd_AddCommand ("+button4", IN_Button4Down);
972 Cmd_AddCommand ("-button4", IN_Button4Up);
973 Cmd_AddCommand ("+button5", IN_Button5Down);
974 Cmd_AddCommand ("-button5", IN_Button5Up);
975 Cmd_AddCommand ("+button6", IN_Button6Down);
976 Cmd_AddCommand ("-button6", IN_Button6Up);
977 Cmd_AddCommand ("+button7", IN_Button7Down);
978 Cmd_AddCommand ("-button7", IN_Button7Up);
979 Cmd_AddCommand ("+button8", IN_Button8Down);
980 Cmd_AddCommand ("-button8", IN_Button8Up);
981 Cmd_AddCommand ("+button9", IN_Button9Down);
982 Cmd_AddCommand ("-button9", IN_Button9Up);
983 Cmd_AddCommand ("+button10", IN_Button10Down);
984 Cmd_AddCommand ("-button10", IN_Button10Up);
985 Cmd_AddCommand ("+button11", IN_Button11Down);
986 Cmd_AddCommand ("-button11", IN_Button11Up);
987 Cmd_AddCommand ("+button12", IN_Button12Down);
988 Cmd_AddCommand ("-button12", IN_Button12Up);
989 Cmd_AddCommand ("+button13", IN_Button13Down);
990 Cmd_AddCommand ("-button13", IN_Button13Up);
991 Cmd_AddCommand ("+button14", IN_Button14Down);
992 Cmd_AddCommand ("-button14", IN_Button14Up);
993 Cmd_AddCommand ("+mlook", IN_MLookDown);
994 Cmd_AddCommand ("-mlook", IN_MLookUp);
995
996 #ifdef USE_VOIP
997 Cmd_AddCommand ("+voiprecord", IN_VoipRecordDown);
998 Cmd_AddCommand ("-voiprecord", IN_VoipRecordUp);
999 #endif
1000
1001 cl_nodelta = Cvar_Get ("cl_nodelta", "0", 0);
1002 cl_debugMove = Cvar_Get ("cl_debugMove", "0", 0);
1003 }
1004
1005 /*
1006 ============
1007 CL_ShutdownInput
1008 ============
1009 */
1010 void CL_ShutdownInput(void)
1011 {
1012 Cmd_RemoveCommand("centerview");
1013
1014 Cmd_RemoveCommand("+moveup");
1015 Cmd_RemoveCommand("-moveup");
1016 Cmd_RemoveCommand("+movedown");
1017 Cmd_RemoveCommand("-movedown");
1018 Cmd_RemoveCommand("+left");
1019 Cmd_RemoveCommand("-left");
1020 Cmd_RemoveCommand("+right");
1021 Cmd_RemoveCommand("-right");
1022 Cmd_RemoveCommand("+forward");
1023 Cmd_RemoveCommand("-forward");
1024 Cmd_RemoveCommand("+back");
1025 Cmd_RemoveCommand("-back");
1026 Cmd_RemoveCommand("+lookup");
1027 Cmd_RemoveCommand("-lookup");
1028 Cmd_RemoveCommand("+lookdown");
1029 Cmd_RemoveCommand("-lookdown");
1030 Cmd_RemoveCommand("+strafe");
1031 Cmd_RemoveCommand("-strafe");
1032 Cmd_RemoveCommand("+moveleft");
1033 Cmd_RemoveCommand("-moveleft");
1034 Cmd_RemoveCommand("+moveright");
1035 Cmd_RemoveCommand("-moveright");
1036 Cmd_RemoveCommand("+speed");
1037 Cmd_RemoveCommand("-speed");
1038 Cmd_RemoveCommand("+attack");
1039 Cmd_RemoveCommand("-attack");
1040 Cmd_RemoveCommand("+button0");
1041 Cmd_RemoveCommand("-button0");
1042 Cmd_RemoveCommand("+button1");
1043 Cmd_RemoveCommand("-button1");
1044 Cmd_RemoveCommand("+button2");
1045 Cmd_RemoveCommand("-button2");
1046 Cmd_RemoveCommand("+button3");
1047 Cmd_RemoveCommand("-button3");
1048 Cmd_RemoveCommand("+button4");
1049 Cmd_RemoveCommand("-button4");
1050 Cmd_RemoveCommand("+button5");
1051 Cmd_RemoveCommand("-button5");
1052 Cmd_RemoveCommand("+button6");
1053 Cmd_RemoveCommand("-button6");
1054 Cmd_RemoveCommand("+button7");
1055 Cmd_RemoveCommand("-button7");
1056 Cmd_RemoveCommand("+button8");
1057 Cmd_RemoveCommand("-button8");
1058 Cmd_RemoveCommand("+button9");
1059 Cmd_RemoveCommand("-button9");
1060 Cmd_RemoveCommand("+button10");
1061 Cmd_RemoveCommand("-button10");
1062 Cmd_RemoveCommand("+button11");
1063 Cmd_RemoveCommand("-button11");
1064 Cmd_RemoveCommand("+button12");
1065 Cmd_RemoveCommand("-button12");
1066 Cmd_RemoveCommand("+button13");
1067 Cmd_RemoveCommand("-button13");
1068 Cmd_RemoveCommand("+button14");
1069 Cmd_RemoveCommand("-button14");
1070 Cmd_RemoveCommand("+mlook");
1071 Cmd_RemoveCommand("-mlook");
1072
1073 #ifdef USE_VOIP
1074 Cmd_RemoveCommand("+voiprecord");
1075 Cmd_RemoveCommand("-voiprecord");
1076 #endif
1077 }