]> icculus.org git repositories - divverent/darkplaces.git/blob - cl_input.c
hopeful fix for flymove !trace.ent error
[divverent/darkplaces.git] / cl_input.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // cl.input.c  -- builds an intended movement command to send to the server
21
22 // Quake is a trademark of Id Software, Inc., (c) 1996 Id Software, Inc. All
23 // rights reserved.
24
25 #include "quakedef.h"
26
27 /*
28 ===============================================================================
29
30 KEY BUTTONS
31
32 Continuous button event tracking is complicated by the fact that two different
33 input sources (say, mouse button 1 and the control key) can both press the
34 same button, but the button should only be released when both of the
35 pressing key have been released.
36
37 When a key event issues a button command (+forward, +attack, etc), it appends
38 its key number as a parameter to the command so it can be matched up with
39 the release.
40
41 state bit 0 is the current state of the key
42 state bit 1 is edge triggered on the up to down transition
43 state bit 2 is edge triggered on the down to up transition
44
45 ===============================================================================
46 */
47
48
49 kbutton_t       in_mlook, in_klook;
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, in_use, in_jump, in_attack;
53 kbutton_t       in_up, in_down;
54 // LordHavoc: added 6 new buttons
55 kbutton_t       in_button3, in_button4, in_button5, in_button6, in_button7, in_button8;
56
57 int                     in_impulse;
58
59
60 void KeyDown (kbutton_t *b)
61 {
62         int             k;
63         char    *c;
64
65         c = Cmd_Argv(1);
66         if (c[0])
67                 k = atoi(c);
68         else
69                 k = -1;         // typed manually at the console for continuous down
70
71         if (k == b->down[0] || k == b->down[1])
72                 return;         // repeating key
73
74         if (!b->down[0])
75                 b->down[0] = k;
76         else if (!b->down[1])
77                 b->down[1] = k;
78         else
79         {
80                 Con_Printf ("Three keys down for a button!\n");
81                 return;
82         }
83         
84         if (b->state & 1)
85                 return;         // still down
86         b->state |= 1 + 2;      // down + impulse down
87 }
88
89 void KeyUp (kbutton_t *b)
90 {
91         int             k;
92         char    *c;
93         
94         c = Cmd_Argv(1);
95         if (c[0])
96                 k = atoi(c);
97         else
98         { // typed manually at the console, assume for unsticking, so clear all
99                 b->down[0] = b->down[1] = 0;
100                 b->state = 4;   // impulse up
101                 return;
102         }
103
104         if (b->down[0] == k)
105                 b->down[0] = 0;
106         else if (b->down[1] == k)
107                 b->down[1] = 0;
108         else
109                 return;         // key up without coresponding down (menu pass through)
110         if (b->down[0] || b->down[1])
111                 return;         // some other key is still holding it down
112
113         if (!(b->state & 1))
114                 return;         // still up (this should not happen)
115         b->state &= ~1;         // now up
116         b->state |= 4;          // impulse up
117 }
118
119 void IN_KLookDown (void) {KeyDown(&in_klook);}
120 void IN_KLookUp (void) {KeyUp(&in_klook);}
121 void IN_MLookDown (void) {KeyDown(&in_mlook);}
122 void IN_MLookUp (void) {
123 KeyUp(&in_mlook);
124 if ( !(in_mlook.state&1) &&  lookspring.value)
125         V_StartPitchDrift();
126 }
127 void IN_UpDown(void) {KeyDown(&in_up);}
128 void IN_UpUp(void) {KeyUp(&in_up);}
129 void IN_DownDown(void) {KeyDown(&in_down);}
130 void IN_DownUp(void) {KeyUp(&in_down);}
131 void IN_LeftDown(void) {KeyDown(&in_left);}
132 void IN_LeftUp(void) {KeyUp(&in_left);}
133 void IN_RightDown(void) {KeyDown(&in_right);}
134 void IN_RightUp(void) {KeyUp(&in_right);}
135 void IN_ForwardDown(void) {KeyDown(&in_forward);}
136 void IN_ForwardUp(void) {KeyUp(&in_forward);}
137 void IN_BackDown(void) {KeyDown(&in_back);}
138 void IN_BackUp(void) {KeyUp(&in_back);}
139 void IN_LookupDown(void) {KeyDown(&in_lookup);}
140 void IN_LookupUp(void) {KeyUp(&in_lookup);}
141 void IN_LookdownDown(void) {KeyDown(&in_lookdown);}
142 void IN_LookdownUp(void) {KeyUp(&in_lookdown);}
143 void IN_MoveleftDown(void) {KeyDown(&in_moveleft);}
144 void IN_MoveleftUp(void) {KeyUp(&in_moveleft);}
145 void IN_MoverightDown(void) {KeyDown(&in_moveright);}
146 void IN_MoverightUp(void) {KeyUp(&in_moveright);}
147
148 void IN_SpeedDown(void) {KeyDown(&in_speed);}
149 void IN_SpeedUp(void) {KeyUp(&in_speed);}
150 void IN_StrafeDown(void) {KeyDown(&in_strafe);}
151 void IN_StrafeUp(void) {KeyUp(&in_strafe);}
152
153 void IN_AttackDown(void) {KeyDown(&in_attack);}
154 void IN_AttackUp(void) {KeyUp(&in_attack);}
155
156 // LordHavoc: added 6 new buttons
157 void IN_Button3Down(void) {KeyDown(&in_button3);} void IN_Button3Up(void) {KeyUp(&in_button3);}
158 void IN_Button4Down(void) {KeyDown(&in_button4);} void IN_Button4Up(void) {KeyUp(&in_button4);}
159 void IN_Button5Down(void) {KeyDown(&in_button5);} void IN_Button5Up(void) {KeyUp(&in_button5);}
160 void IN_Button6Down(void) {KeyDown(&in_button6);} void IN_Button6Up(void) {KeyUp(&in_button6);}
161 void IN_Button7Down(void) {KeyDown(&in_button7);} void IN_Button7Up(void) {KeyUp(&in_button7);}
162 void IN_Button8Down(void) {KeyDown(&in_button8);} void IN_Button8Up(void) {KeyUp(&in_button8);}
163
164 void IN_UseDown (void) {KeyDown(&in_use);}
165 void IN_UseUp (void) {KeyUp(&in_use);}
166 void IN_JumpDown (void) {KeyDown(&in_jump);}
167 void IN_JumpUp (void) {KeyUp(&in_jump);}
168
169 void IN_Impulse (void) {in_impulse=atoi(Cmd_Argv(1));}
170
171 /*
172 ===============
173 CL_KeyState
174
175 Returns 0.25 if a key was pressed and released during the frame,
176 0.5 if it was pressed and held
177 0 if held then released, and
178 1.0 if held for the entire time
179 ===============
180 */
181 float CL_KeyState (kbutton_t *key)
182 {
183         float           val;
184         qboolean        impulsedown, impulseup, down;
185         
186         impulsedown = key->state & 2;
187         impulseup = key->state & 4;
188         down = key->state & 1;
189         val = 0;
190         
191         if (impulsedown && !impulseup)
192         {
193                 if (down)
194                         val = 0.5;      // pressed and held this frame
195                 else
196                         val = 0;        //      I_Error ();
197         }
198         if (impulseup && !impulsedown)
199         {
200                 if (down)
201                         val = 0;        //      I_Error ();
202                 else
203                         val = 0;        // released this frame
204         }
205         if (!impulsedown && !impulseup)
206         {
207                 if (down)
208                         val = 1.0;      // held the entire frame
209                 else
210                         val = 0;        // up the entire frame
211         }
212         if (impulsedown && impulseup)
213         {
214                 if (down)
215                         val = 0.75;     // released and re-pressed this frame
216                 else
217                         val = 0.25;     // pressed and released this frame
218         }
219
220         key->state &= 1;                // clear impulses
221         
222         return val;
223 }
224
225
226
227
228 //==========================================================================
229
230 cvar_t cl_upspeed = {CVAR_SAVE, "cl_upspeed","400"};
231 cvar_t cl_forwardspeed = {CVAR_SAVE, "cl_forwardspeed","400"};
232 cvar_t cl_backspeed = {CVAR_SAVE, "cl_backspeed","400"};
233 cvar_t cl_sidespeed = {CVAR_SAVE, "cl_sidespeed","350"};
234
235 cvar_t cl_movespeedkey = {CVAR_SAVE, "cl_movespeedkey","2.0"};
236
237 cvar_t cl_yawspeed = {CVAR_SAVE, "cl_yawspeed","140"};
238 cvar_t cl_pitchspeed = {CVAR_SAVE, "cl_pitchspeed","150"};
239
240 cvar_t cl_anglespeedkey = {CVAR_SAVE, "cl_anglespeedkey","1.5"};
241
242
243 /*
244 ================
245 CL_AdjustAngles
246
247 Moves the local angle positions
248 ================
249 */
250 void CL_AdjustAngles (void)
251 {
252         float   speed;
253         float   up, down;
254
255         if (in_speed.state & 1)
256                 speed = cl.frametime * cl_anglespeedkey.value;
257         else
258                 speed = cl.frametime;
259
260         if (!(in_strafe.state & 1))
261         {
262                 cl.viewangles[YAW] -= speed*cl_yawspeed.value*CL_KeyState (&in_right);
263                 cl.viewangles[YAW] += speed*cl_yawspeed.value*CL_KeyState (&in_left);
264                 cl.viewangles[YAW] = ANGLEMOD(cl.viewangles[YAW]);
265         }
266         if (in_klook.state & 1)
267         {
268                 V_StopPitchDrift ();
269                 cl.viewangles[PITCH] -= speed*cl_pitchspeed.value * CL_KeyState (&in_forward);
270                 cl.viewangles[PITCH] += speed*cl_pitchspeed.value * CL_KeyState (&in_back);
271         }
272
273         up = CL_KeyState (&in_lookup);
274         down = CL_KeyState(&in_lookdown);
275
276         cl.viewangles[PITCH] -= speed*cl_pitchspeed.value * up;
277         cl.viewangles[PITCH] += speed*cl_pitchspeed.value * down;
278
279         if (up || down)
280                 V_StopPitchDrift ();
281
282         // LordHavoc: changed from 80 to 90 (straight up)
283         if (cl.viewangles[PITCH] > 90)
284                 cl.viewangles[PITCH] = 90;
285         // LordHavoc: changed from -70 to -90 (straight down)
286         if (cl.viewangles[PITCH] < -90)
287                 cl.viewangles[PITCH] = -90;
288
289         if (cl.viewangles[ROLL] > 50)
290                 cl.viewangles[ROLL] = 50;
291         if (cl.viewangles[ROLL] < -50)
292                 cl.viewangles[ROLL] = -50;
293                 
294 }
295
296 /*
297 ================
298 CL_BaseMove
299
300 Send the intended movement message to the server
301 ================
302 */
303 void CL_BaseMove (usercmd_t *cmd)
304 {       
305         if (cls.signon != SIGNONS)
306                 return;
307                         
308         CL_AdjustAngles ();
309         
310         memset (cmd, 0, sizeof(*cmd));
311         
312         if (in_strafe.state & 1)
313         {
314                 cmd->sidemove += cl_sidespeed.value * CL_KeyState (&in_right);
315                 cmd->sidemove -= cl_sidespeed.value * CL_KeyState (&in_left);
316         }
317
318         cmd->sidemove += cl_sidespeed.value * CL_KeyState (&in_moveright);
319         cmd->sidemove -= cl_sidespeed.value * CL_KeyState (&in_moveleft);
320
321         cmd->upmove += cl_upspeed.value * CL_KeyState (&in_up);
322         cmd->upmove -= cl_upspeed.value * CL_KeyState (&in_down);
323
324         if (! (in_klook.state & 1) )
325         {       
326                 cmd->forwardmove += cl_forwardspeed.value * CL_KeyState (&in_forward);
327                 cmd->forwardmove -= cl_backspeed.value * CL_KeyState (&in_back);
328         }       
329
330 //
331 // adjust for speed key
332 //
333         if (in_speed.state & 1)
334         {
335                 cmd->forwardmove *= cl_movespeedkey.value;
336                 cmd->sidemove *= cl_movespeedkey.value;
337                 cmd->upmove *= cl_movespeedkey.value;
338         }
339 }
340
341
342
343 /*
344 ==============
345 CL_SendMove
346 ==============
347 */
348 void CL_SendMove (usercmd_t *cmd)
349 {
350         int             i;
351         int             bits;
352         sizebuf_t       buf;
353         byte    data[128];
354         static double lastmovetime;
355         static float forwardmove, sidemove, upmove, total; // accumulation
356
357         forwardmove += cmd->forwardmove;
358         sidemove += cmd->sidemove;
359         upmove += cmd->upmove;
360         total++;
361         // LordHavoc: cap outgoing movement messages to sys_ticrate
362         if ((cl.maxclients > 1) && (realtime - lastmovetime < sys_ticrate.value))
363                 return;
364         lastmovetime = realtime;
365         // average what has happened during this time
366         total = 1.0f / total;
367         forwardmove *= total;
368         sidemove *= total;
369         upmove *= total;
370         total = 0;
371
372         buf.maxsize = 128;
373         buf.cursize = 0;
374         buf.data = data;
375
376         cl.cmd = *cmd;
377
378 //
379 // send the movement message
380 //
381     MSG_WriteByte (&buf, clc_move);
382
383         MSG_WriteFloat (&buf, cl.mtime[0]);     // so server can get ping times
384
385         if (dpprotocol == DPPROTOCOL_VERSION2)
386         {
387                 for (i = 0;i < 3;i++)
388                         MSG_WriteFloat (&buf, cl.viewangles[i]);
389         }
390         else if (dpprotocol == DPPROTOCOL_VERSION1)
391         {
392                 for (i=0 ; i<3 ; i++)
393                         MSG_WritePreciseAngle (&buf, cl.viewangles[i]);
394         }
395         else
396         {
397                 for (i=0 ; i<3 ; i++)
398                         MSG_WriteAngle (&buf, cl.viewangles[i]);
399         }
400
401     MSG_WriteShort (&buf, forwardmove);
402     MSG_WriteShort (&buf, sidemove);
403     MSG_WriteShort (&buf, upmove);
404
405         forwardmove = sidemove = upmove = 0;
406 //
407 // send button bits
408 //
409         bits = 0;
410
411         if ( in_attack.state & 3 )
412                 bits |= 1;
413         in_attack.state &= ~2;
414
415         if (in_jump.state & 3)
416                 bits |= 2;
417         in_jump.state &= ~2;
418         // LordHavoc: added 6 new buttons
419         if (in_button3.state & 3) bits |=   4;in_button3.state &= ~2;
420         if (in_button4.state & 3) bits |=   8;in_button4.state &= ~2;
421         if (in_button5.state & 3) bits |=  16;in_button5.state &= ~2;
422         if (in_button6.state & 3) bits |=  32;in_button6.state &= ~2;
423         if (in_button7.state & 3) bits |=  64;in_button7.state &= ~2;
424         if (in_button8.state & 3) bits |= 128;in_button8.state &= ~2;
425
426     MSG_WriteByte (&buf, bits);
427
428     MSG_WriteByte (&buf, in_impulse);
429         in_impulse = 0;
430
431         // LordHavoc: should we ack this on receipt instead?  would waste net bandwidth though
432         i = EntityFrame_MostRecentlyRecievedFrameNum(&cl.entitydatabase);
433         if (i > 0)
434         {
435                 MSG_WriteByte (&buf, clc_ackentities);
436                 MSG_WriteLong (&buf, i);
437         }
438
439 //
440 // deliver the message
441 //
442         if (cls.demoplayback)
443                 return;
444
445 //
446 // always dump the first two messages, because they may contain leftover inputs from the last level
447 //
448         if (++cl.movemessages <= 2)
449                 return;
450
451         if (NET_SendUnreliableMessage (cls.netcon, &buf) == -1)
452         {
453                 Con_Printf ("CL_SendMove: lost server connection\n");
454                 CL_Disconnect ();
455         }
456 }
457
458 /*
459 ============
460 CL_InitInput
461 ============
462 */
463 void CL_InitInput (void)
464 {
465         Cmd_AddCommand ("+moveup",IN_UpDown);
466         Cmd_AddCommand ("-moveup",IN_UpUp);
467         Cmd_AddCommand ("+movedown",IN_DownDown);
468         Cmd_AddCommand ("-movedown",IN_DownUp);
469         Cmd_AddCommand ("+left",IN_LeftDown);
470         Cmd_AddCommand ("-left",IN_LeftUp);
471         Cmd_AddCommand ("+right",IN_RightDown);
472         Cmd_AddCommand ("-right",IN_RightUp);
473         Cmd_AddCommand ("+forward",IN_ForwardDown);
474         Cmd_AddCommand ("-forward",IN_ForwardUp);
475         Cmd_AddCommand ("+back",IN_BackDown);
476         Cmd_AddCommand ("-back",IN_BackUp);
477         Cmd_AddCommand ("+lookup", IN_LookupDown);
478         Cmd_AddCommand ("-lookup", IN_LookupUp);
479         Cmd_AddCommand ("+lookdown", IN_LookdownDown);
480         Cmd_AddCommand ("-lookdown", IN_LookdownUp);
481         Cmd_AddCommand ("+strafe", IN_StrafeDown);
482         Cmd_AddCommand ("-strafe", IN_StrafeUp);
483         Cmd_AddCommand ("+moveleft", IN_MoveleftDown);
484         Cmd_AddCommand ("-moveleft", IN_MoveleftUp);
485         Cmd_AddCommand ("+moveright", IN_MoverightDown);
486         Cmd_AddCommand ("-moveright", IN_MoverightUp);
487         Cmd_AddCommand ("+speed", IN_SpeedDown);
488         Cmd_AddCommand ("-speed", IN_SpeedUp);
489         Cmd_AddCommand ("+attack", IN_AttackDown);
490         Cmd_AddCommand ("-attack", IN_AttackUp);
491         Cmd_AddCommand ("+use", IN_UseDown);
492         Cmd_AddCommand ("-use", IN_UseUp);
493         Cmd_AddCommand ("+jump", IN_JumpDown);
494         Cmd_AddCommand ("-jump", IN_JumpUp);
495         Cmd_AddCommand ("impulse", IN_Impulse);
496         Cmd_AddCommand ("+klook", IN_KLookDown);
497         Cmd_AddCommand ("-klook", IN_KLookUp);
498         Cmd_AddCommand ("+mlook", IN_MLookDown);
499         Cmd_AddCommand ("-mlook", IN_MLookUp);
500
501         // LordHavoc: added 6 new buttons
502         Cmd_AddCommand ("+button3", IN_Button3Down);
503         Cmd_AddCommand ("-button3", IN_Button3Up);
504         Cmd_AddCommand ("+button4", IN_Button4Down);
505         Cmd_AddCommand ("-button4", IN_Button4Up);
506         Cmd_AddCommand ("+button5", IN_Button5Down);
507         Cmd_AddCommand ("-button5", IN_Button5Up);
508         Cmd_AddCommand ("+button6", IN_Button6Down);
509         Cmd_AddCommand ("-button6", IN_Button6Up);
510         Cmd_AddCommand ("+button7", IN_Button7Down);
511         Cmd_AddCommand ("-button7", IN_Button7Up);
512         Cmd_AddCommand ("+button8", IN_Button8Down);
513         Cmd_AddCommand ("-button8", IN_Button8Up);
514 }