Fix for the problem that SDL applications exited
[AROS-Contrib.git] / Games / Quake / cl_input.c
blobb407ea27dbae01d408e40fb3086d92419bae9b4f
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
20 // cl.input.c -- builds an intended movement command to send to the server
22 // Quake is a trademark of Id Software, Inc., (c) 1996 Id Software, Inc. All
23 // rights reserved.
25 #include "quakedef.h"
28 ===============================================================================
30 KEY BUTTONS
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.
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.
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
45 ===============================================================================
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;
55 int in_impulse;
58 void KeyDown (kbutton_t *b)
60 int k;
61 char *c;
63 c = Cmd_Argv(1);
64 if (c[0])
65 k = atoi(c);
66 else
67 k = -1; // typed manually at the console for continuous down
69 if (k == b->down[0] || k == b->down[1])
70 return; // repeating key
72 if (!b->down[0])
73 b->down[0] = k;
74 else if (!b->down[1])
75 b->down[1] = k;
76 else
78 Con_Printf ("Three keys down for a button!\n");
79 return;
82 if (b->state & 1)
83 return; // still down
84 b->state |= 1 + 2; // down + impulse down
87 void KeyUp (kbutton_t *b)
89 int k;
90 char *c;
92 c = Cmd_Argv(1);
93 if (c[0])
94 k = atoi(c);
95 else
96 { // typed manually at the console, assume for unsticking, so clear all
97 b->down[0] = b->down[1] = 0;
98 b->state = 4; // impulse up
99 return;
102 if (b->down[0] == k)
103 b->down[0] = 0;
104 else if (b->down[1] == k)
105 b->down[1] = 0;
106 else
107 return; // key up without coresponding down (menu pass through)
108 if (b->down[0] || b->down[1])
109 return; // some other key is still holding it down
111 if (!(b->state & 1))
112 return; // still up (this should not happen)
113 b->state &= ~1; // now up
114 b->state |= 4; // impulse up
117 void IN_KLookDown (void) {KeyDown(&in_klook);}
118 void IN_KLookUp (void) {KeyUp(&in_klook);}
119 void IN_MLookDown (void) {KeyDown(&in_mlook);}
120 void IN_MLookUp (void) {
121 KeyUp(&in_mlook);
122 if ( !(in_mlook.state&1) && lookspring.value)
123 V_StartPitchDrift();
125 void IN_UpDown(void) {KeyDown(&in_up);}
126 void IN_UpUp(void) {KeyUp(&in_up);}
127 void IN_DownDown(void) {KeyDown(&in_down);}
128 void IN_DownUp(void) {KeyUp(&in_down);}
129 void IN_LeftDown(void) {KeyDown(&in_left);}
130 void IN_LeftUp(void) {KeyUp(&in_left);}
131 void IN_RightDown(void) {KeyDown(&in_right);}
132 void IN_RightUp(void) {KeyUp(&in_right);}
133 void IN_ForwardDown(void) {KeyDown(&in_forward);}
134 void IN_ForwardUp(void) {KeyUp(&in_forward);}
135 void IN_BackDown(void) {KeyDown(&in_back);}
136 void IN_BackUp(void) {KeyUp(&in_back);}
137 void IN_LookupDown(void) {KeyDown(&in_lookup);}
138 void IN_LookupUp(void) {KeyUp(&in_lookup);}
139 void IN_LookdownDown(void) {KeyDown(&in_lookdown);}
140 void IN_LookdownUp(void) {KeyUp(&in_lookdown);}
141 void IN_MoveleftDown(void) {KeyDown(&in_moveleft);}
142 void IN_MoveleftUp(void) {KeyUp(&in_moveleft);}
143 void IN_MoverightDown(void) {KeyDown(&in_moveright);}
144 void IN_MoverightUp(void) {KeyUp(&in_moveright);}
146 void IN_SpeedDown(void) {KeyDown(&in_speed);}
147 void IN_SpeedUp(void) {KeyUp(&in_speed);}
148 void IN_StrafeDown(void) {KeyDown(&in_strafe);}
149 void IN_StrafeUp(void) {KeyUp(&in_strafe);}
151 void IN_AttackDown(void) {KeyDown(&in_attack);}
152 void IN_AttackUp(void) {KeyUp(&in_attack);}
154 void IN_UseDown (void) {KeyDown(&in_use);}
155 void IN_UseUp (void) {KeyUp(&in_use);}
156 void IN_JumpDown (void) {KeyDown(&in_jump);}
157 void IN_JumpUp (void) {KeyUp(&in_jump);}
159 void IN_Impulse (void) {in_impulse=Q_atoi(Cmd_Argv(1));}
162 ===============
163 CL_KeyState
165 Returns 0.25 if a key was pressed and released during the frame,
166 0.5 if it was pressed and held
167 0 if held then released, and
168 1.0 if held for the entire time
169 ===============
171 float CL_KeyState (kbutton_t *key)
173 float val;
174 qboolean impulsedown, impulseup, down;
176 impulsedown = key->state & 2;
177 impulseup = key->state & 4;
178 down = key->state & 1;
179 val = 0;
181 if (impulsedown && !impulseup)
183 if (down)
184 val = 0.5; // pressed and held this frame
185 else
186 val = 0; // I_Error ();
188 if (impulseup && !impulsedown)
190 if (down)
191 val = 0; // I_Error ();
192 else
193 val = 0; // released this frame
195 if (!impulsedown && !impulseup)
197 if (down)
198 val = 1.0; // held the entire frame
199 else
200 val = 0; // up the entire frame
202 if (impulsedown && impulseup)
204 if (down)
205 val = 0.75; // released and re-pressed this frame
206 else
207 val = 0.25; // pressed and released this frame
210 key->state &= 1; // clear impulses
212 return val;
218 //==========================================================================
220 cvar_t cl_upspeed = {"cl_upspeed","200"};
221 cvar_t cl_forwardspeed = {"cl_forwardspeed","200", true};
222 cvar_t cl_backspeed = {"cl_backspeed","200", true};
223 cvar_t cl_sidespeed = {"cl_sidespeed","350"};
225 cvar_t cl_movespeedkey = {"cl_movespeedkey","2.0"};
227 cvar_t cl_yawspeed = {"cl_yawspeed","140"};
228 cvar_t cl_pitchspeed = {"cl_pitchspeed","150"};
230 cvar_t cl_anglespeedkey = {"cl_anglespeedkey","1.5"};
234 ================
235 CL_AdjustAngles
237 Moves the local angle positions
238 ================
240 void CL_AdjustAngles (void)
242 float speed;
243 float up, down;
245 if (in_speed.state & 1)
246 speed = host_frametime * cl_anglespeedkey.value;
247 else
248 speed = host_frametime;
250 if (!(in_strafe.state & 1))
252 cl.viewangles[YAW] -= speed*cl_yawspeed.value*CL_KeyState (&in_right);
253 cl.viewangles[YAW] += speed*cl_yawspeed.value*CL_KeyState (&in_left);
254 cl.viewangles[YAW] = anglemod(cl.viewangles[YAW]);
256 if (in_klook.state & 1)
258 V_StopPitchDrift ();
259 cl.viewangles[PITCH] -= speed*cl_pitchspeed.value * CL_KeyState (&in_forward);
260 cl.viewangles[PITCH] += speed*cl_pitchspeed.value * CL_KeyState (&in_back);
263 up = CL_KeyState (&in_lookup);
264 down = CL_KeyState(&in_lookdown);
266 cl.viewangles[PITCH] -= speed*cl_pitchspeed.value * up;
267 cl.viewangles[PITCH] += speed*cl_pitchspeed.value * down;
269 if (up || down)
270 V_StopPitchDrift ();
272 if (cl.viewangles[PITCH] > 80)
273 cl.viewangles[PITCH] = 80;
274 if (cl.viewangles[PITCH] < -70)
275 cl.viewangles[PITCH] = -70;
277 if (cl.viewangles[ROLL] > 50)
278 cl.viewangles[ROLL] = 50;
279 if (cl.viewangles[ROLL] < -50)
280 cl.viewangles[ROLL] = -50;
285 ================
286 CL_BaseMove
288 Send the intended movement message to the server
289 ================
291 void CL_BaseMove (usercmd_t *cmd)
293 if (cls.signon != SIGNONS)
294 return;
296 CL_AdjustAngles ();
298 Q_memset (cmd, 0, sizeof(*cmd));
300 if (in_strafe.state & 1)
302 cmd->sidemove += cl_sidespeed.value * CL_KeyState (&in_right);
303 cmd->sidemove -= cl_sidespeed.value * CL_KeyState (&in_left);
306 cmd->sidemove += cl_sidespeed.value * CL_KeyState (&in_moveright);
307 cmd->sidemove -= cl_sidespeed.value * CL_KeyState (&in_moveleft);
309 cmd->upmove += cl_upspeed.value * CL_KeyState (&in_up);
310 cmd->upmove -= cl_upspeed.value * CL_KeyState (&in_down);
312 if (! (in_klook.state & 1) )
314 cmd->forwardmove += cl_forwardspeed.value * CL_KeyState (&in_forward);
315 cmd->forwardmove -= cl_backspeed.value * CL_KeyState (&in_back);
319 // adjust for speed key
321 if (in_speed.state & 1)
323 cmd->forwardmove *= cl_movespeedkey.value;
324 cmd->sidemove *= cl_movespeedkey.value;
325 cmd->upmove *= cl_movespeedkey.value;
328 #ifdef QUAKE2
329 cmd->lightlevel = cl.light_level;
330 #endif
336 ==============
337 CL_SendMove
338 ==============
340 void CL_SendMove (usercmd_t *cmd)
342 int i;
343 int bits;
344 sizebuf_t buf;
345 byte data[128];
347 buf.maxsize = 128;
348 buf.cursize = 0;
349 buf.data = data;
351 cl.cmd = *cmd;
354 // send the movement message
356 MSG_WriteByte (&buf, clc_move);
358 MSG_WriteFloat (&buf, cl.mtime[0]); // so server can get ping times
360 for (i=0 ; i<3 ; i++)
361 MSG_WriteAngle (&buf, cl.viewangles[i]);
363 MSG_WriteShort (&buf, cmd->forwardmove);
364 MSG_WriteShort (&buf, cmd->sidemove);
365 MSG_WriteShort (&buf, cmd->upmove);
368 // send button bits
370 bits = 0;
372 if ( in_attack.state & 3 )
373 bits |= 1;
374 in_attack.state &= ~2;
376 if (in_jump.state & 3)
377 bits |= 2;
378 in_jump.state &= ~2;
380 MSG_WriteByte (&buf, bits);
382 MSG_WriteByte (&buf, in_impulse);
383 in_impulse = 0;
385 #ifdef QUAKE2
387 // light level
389 MSG_WriteByte (&buf, cmd->lightlevel);
390 #endif
393 // deliver the message
395 if (cls.demoplayback)
396 return;
399 // allways dump the first two message, because it may contain leftover inputs
400 // from the last level
402 if (++cl.movemessages <= 2)
403 return;
405 if (NET_SendUnreliableMessage (cls.netcon, &buf) == -1)
407 Con_Printf ("CL_SendMove: lost server connection\n");
408 CL_Disconnect ();
413 ============
414 CL_InitInput
415 ============
417 void CL_InitInput (void)
419 Cmd_AddCommand ("+moveup",IN_UpDown);
420 Cmd_AddCommand ("-moveup",IN_UpUp);
421 Cmd_AddCommand ("+movedown",IN_DownDown);
422 Cmd_AddCommand ("-movedown",IN_DownUp);
423 Cmd_AddCommand ("+left",IN_LeftDown);
424 Cmd_AddCommand ("-left",IN_LeftUp);
425 Cmd_AddCommand ("+right",IN_RightDown);
426 Cmd_AddCommand ("-right",IN_RightUp);
427 Cmd_AddCommand ("+forward",IN_ForwardDown);
428 Cmd_AddCommand ("-forward",IN_ForwardUp);
429 Cmd_AddCommand ("+back",IN_BackDown);
430 Cmd_AddCommand ("-back",IN_BackUp);
431 Cmd_AddCommand ("+lookup", IN_LookupDown);
432 Cmd_AddCommand ("-lookup", IN_LookupUp);
433 Cmd_AddCommand ("+lookdown", IN_LookdownDown);
434 Cmd_AddCommand ("-lookdown", IN_LookdownUp);
435 Cmd_AddCommand ("+strafe", IN_StrafeDown);
436 Cmd_AddCommand ("-strafe", IN_StrafeUp);
437 Cmd_AddCommand ("+moveleft", IN_MoveleftDown);
438 Cmd_AddCommand ("-moveleft", IN_MoveleftUp);
439 Cmd_AddCommand ("+moveright", IN_MoverightDown);
440 Cmd_AddCommand ("-moveright", IN_MoverightUp);
441 Cmd_AddCommand ("+speed", IN_SpeedDown);
442 Cmd_AddCommand ("-speed", IN_SpeedUp);
443 Cmd_AddCommand ("+attack", IN_AttackDown);
444 Cmd_AddCommand ("-attack", IN_AttackUp);
445 Cmd_AddCommand ("+use", IN_UseDown);
446 Cmd_AddCommand ("-use", IN_UseUp);
447 Cmd_AddCommand ("+jump", IN_JumpDown);
448 Cmd_AddCommand ("-jump", IN_JumpUp);
449 Cmd_AddCommand ("impulse", IN_Impulse);
450 Cmd_AddCommand ("+klook", IN_KLookDown);
451 Cmd_AddCommand ("-klook", IN_KLookUp);
452 Cmd_AddCommand ("+mlook", IN_MLookDown);
453 Cmd_AddCommand ("-mlook", IN_MLookUp);