git-svn-id: http://bladebattles.com/kurok/SVN@11 20cd92bb-ff49-0410-b73e-96a06e42c3b9
[kurok.git] / cl_input.c
blob46eae4a48016892ba89b30d22542e20d8b6cf970
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"
26 #include <pspctrl.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;
59 void KeyDown (kbutton_t *b)
61 int k;
62 char *c;
64 c = Cmd_Argv(1);
65 if (c[0])
66 k = atoi(c);
67 else
68 k = -1; // typed manually at the console for continuous down
70 if (k == b->down[0] || k == b->down[1])
71 return; // repeating key
73 if (!b->down[0])
74 b->down[0] = k;
75 else if (!b->down[1])
76 b->down[1] = k;
77 else
79 Con_Printf ("Three keys down for a button!\n");
80 return;
83 if (b->state & 1)
84 return; // still down
85 b->state |= 1 + 2; // down + impulse down
88 void KeyUp (kbutton_t *b)
90 int k;
91 char *c;
93 c = Cmd_Argv(1);
94 if (c[0])
95 k = atoi(c);
96 else
97 { // typed manually at the console, assume for unsticking, so clear all
98 b->down[0] = b->down[1] = 0;
99 b->state = 4; // impulse up
100 return;
103 if (b->down[0] == k)
104 b->down[0] = 0;
105 else if (b->down[1] == k)
106 b->down[1] = 0;
107 else
108 return; // key up without coresponding down (menu pass through)
109 if (b->down[0] || b->down[1])
110 return; // some other key is still holding it down
112 if (!(b->state & 1))
113 return; // still up (this should not happen)
114 b->state &= ~1; // now up
115 b->state |= 4; // impulse up
118 void IN_KLookDown (void) {KeyDown(&in_klook);}
119 void IN_KLookUp (void) {KeyUp(&in_klook);}
120 void IN_MLookDown (void) {KeyDown(&in_mlook);}
121 void IN_MLookUp (void) {
122 KeyUp(&in_mlook);
123 if ( !(in_mlook.state&1) && lookspring.value && !lookcenter.value)
124 V_StartPitchDrift();
126 void IN_UpDown(void) {KeyDown(&in_up);}
127 void IN_UpUp(void) {KeyUp(&in_up);}
128 void IN_DownDown(void) {KeyDown(&in_down);}
129 void IN_DownUp(void) {KeyUp(&in_down);}
130 void IN_LeftDown(void) {KeyDown(&in_left);}
131 void IN_LeftUp(void) {KeyUp(&in_left);}
132 void IN_RightDown(void) {KeyDown(&in_right);}
133 void IN_RightUp(void) {KeyUp(&in_right);}
134 void IN_ForwardDown(void) {KeyDown(&in_forward);}
135 void IN_ForwardUp(void) {KeyUp(&in_forward);}
136 void IN_BackDown(void) {KeyDown(&in_back);}
137 void IN_BackUp(void) {KeyUp(&in_back);}
138 void IN_LookupDown(void) {KeyDown(&in_lookup);}
139 void IN_LookupUp(void) {KeyUp(&in_lookup);}
140 void IN_LookdownDown(void) {KeyDown(&in_lookdown);}
141 void IN_LookdownUp(void) {KeyUp(&in_lookdown);}
142 void IN_MoveleftDown(void) {KeyDown(&in_moveleft);}
143 void IN_MoveleftUp(void) {KeyUp(&in_moveleft);}
144 void IN_MoverightDown(void) {KeyDown(&in_moveright);}
145 void IN_MoverightUp(void) {KeyUp(&in_moveright);}
147 void IN_SpeedDown(void) {KeyDown(&in_speed);}
148 void IN_SpeedUp(void) {KeyUp(&in_speed);}
149 void IN_StrafeDown(void) {KeyDown(&in_strafe);}
150 void IN_StrafeUp(void) {KeyUp(&in_strafe);}
152 void IN_AttackDown(void) {KeyDown(&in_attack);}
153 void IN_AttackUp(void) {KeyUp(&in_attack);}
155 void IN_UseDown (void) {KeyDown(&in_use);}
156 void IN_UseUp (void) {KeyUp(&in_use);}
157 void IN_JumpDown (void) {KeyDown(&in_jump);}
158 void IN_JumpUp (void) {KeyUp(&in_jump);}
160 void IN_Impulse (void) {in_impulse=Q_atoi(Cmd_Argv(1));}
163 ===============
164 CL_KeyState
166 Returns 0.25 if a key was pressed and released during the frame,
167 0.5 if it was pressed and held
168 0 if held then released, and
169 1.0 if held for the entire time
170 ===============
172 float CL_KeyState (kbutton_t *key)
174 float val;
175 qboolean impulsedown, impulseup, down;
177 impulsedown = key->state & 2;
178 impulseup = key->state & 4;
179 down = key->state & 1;
180 val = 0;
182 if (impulsedown && !impulseup)
184 if (down)
185 val = 0.5; // pressed and held this frame
186 else
187 val = 0; // I_Error ();
189 if (impulseup && !impulsedown)
191 if (down)
192 val = 0; // I_Error ();
193 else
194 val = 0; // released this frame
196 if (!impulsedown && !impulseup)
198 if (down)
199 val = 1.0; // held the entire frame
200 else
201 val = 0; // up the entire frame
203 if (impulsedown && impulseup)
205 if (down)
206 val = 0.75; // released and re-pressed this frame
207 else
208 val = 0.25; // pressed and released this frame
211 key->state &= 1; // clear impulses
213 return val;
219 //==========================================================================
221 cvar_t cl_upspeed = {"cl_upspeed","200"};
222 cvar_t cl_forwardspeed = {"cl_forwardspeed","200", true};
223 cvar_t cl_backspeed = {"cl_backspeed","200", true};
224 cvar_t cl_sidespeed = {"cl_sidespeed","350"};
226 cvar_t cl_movespeedkey = {"cl_movespeedkey","2.0"};
228 cvar_t cl_yawspeed = {"cl_yawspeed","140"};
229 cvar_t cl_pitchspeed = {"cl_pitchspeed","150"};
231 cvar_t cl_anglespeedkey = {"cl_anglespeedkey","1.5"};
233 extern cvar_t scr_fov;
236 ================
237 CL_AdjustAngles
239 Moves the local angle positions
240 ================
242 void CL_AdjustAngles (void)
244 float speed;
245 float up, down;
247 if (in_speed.state & 1)
248 speed = host_frametime * cl_anglespeedkey.value;
249 else
250 speed = host_frametime;
252 if (kurok)
254 if(scr_fov.value <= 25)
255 speed = speed / 4;
256 else if(scr_fov.value <= 50)
257 speed = speed / 3;
258 else if(scr_fov.value <= 75)
259 speed = speed / 2;
260 else
261 speed = speed;
264 if (!(in_strafe.state & 1))
266 cl.viewangles[YAW] -= speed*cl_yawspeed.value*CL_KeyState (&in_right);
267 cl.viewangles[YAW] += speed*cl_yawspeed.value*CL_KeyState (&in_left);
268 cl.viewangles[YAW] = anglemod(cl.viewangles[YAW]);
270 if (in_klook.state & 1)
272 V_StopPitchDrift ();
273 cl.viewangles[PITCH] -= speed*cl_pitchspeed.value * CL_KeyState (&in_forward);
274 cl.viewangles[PITCH] += speed*cl_pitchspeed.value * CL_KeyState (&in_back);
277 up = CL_KeyState (&in_lookup);
278 down = CL_KeyState(&in_lookdown);
280 cl.viewangles[PITCH] -= speed*cl_pitchspeed.value * up;
281 cl.viewangles[PITCH] += speed*cl_pitchspeed.value * down;
283 if (up || down)
284 V_StopPitchDrift ();
286 if (cl.viewangles[PITCH] > 90)
287 cl.viewangles[PITCH] = 90;
288 if (cl.viewangles[PITCH] < -90)
289 cl.viewangles[PITCH] = -90;
291 if (cl.viewangles[ROLL] > 50)
292 cl.viewangles[ROLL] = 50;
293 if (cl.viewangles[ROLL] < -50)
294 cl.viewangles[ROLL] = -50;
299 ================
300 CL_BaseMove
302 Send the intended movement message to the server
303 ================
305 void CL_BaseMove (usercmd_t *cmd)
307 if (cls.signon != SIGNONS)
308 return;
310 CL_AdjustAngles ();
312 Q_memset (cmd, 0, sizeof(*cmd));
314 if (in_strafe.state & 1)
316 cmd->sidemove += cl_sidespeed.value * CL_KeyState (&in_right);
317 cmd->sidemove -= cl_sidespeed.value * CL_KeyState (&in_left);
320 cmd->sidemove += cl_sidespeed.value * CL_KeyState (&in_moveright);
321 cmd->sidemove -= cl_sidespeed.value * CL_KeyState (&in_moveleft);
323 cmd->upmove += cl_upspeed.value * CL_KeyState (&in_up);
324 cmd->upmove -= cl_upspeed.value * CL_KeyState (&in_down);
326 if (! (in_klook.state & 1) )
328 cmd->forwardmove += cl_forwardspeed.value * CL_KeyState (&in_forward);
329 cmd->forwardmove -= cl_backspeed.value * CL_KeyState (&in_back);
333 // adjust for speed key
335 if (in_speed.state & 1)
337 cmd->forwardmove *= cl_movespeedkey.value;
338 cmd->sidemove *= cl_movespeedkey.value;
339 cmd->upmove *= cl_movespeedkey.value;
342 #ifdef QUAKE2
343 cmd->lightlevel = cl.light_level;
344 #endif
348 ==============
349 CL_SendMove
350 ==============
352 void CL_SendMove (usercmd_t *cmd)
354 int i;
355 int bits;
356 sizebuf_t buf;
357 byte data[128];
359 buf.maxsize = 128;
360 buf.cursize = 0;
361 buf.data = data;
363 cl.cmd = *cmd;
366 // send the movement message
368 MSG_WriteByte (&buf, clc_move);
370 MSG_WriteFloat (&buf, cl.mtime[0]); // so server can get ping times
372 if (!cls.demoplayback) // JPG - precise aim for ProQuake!
374 for (i=0 ; i<3 ; i++)
375 MSG_WritePreciseAngle (&buf, cl.viewangles[i]);
377 else
379 for (i=0 ; i<3 ; i++)
380 MSG_WriteAngle (&buf, cl.viewangles[i]);
384 // for (i=0 ; i<3 ; i++)
385 // MSG_WriteAngle (&buf, cl.viewangles[i]);
387 // Read the pad state.
388 SceCtrlData pad;
389 sceCtrlPeekBufferPositive(&pad, 1);
391 MSG_WriteAngle (&buf, cl.viewangles[PITCH] + ((pad.Ly - 128)));
392 MSG_WriteAngle (&buf, cl.viewangles[YAW] + ((pad.Lx - 128)));
393 MSG_WriteAngle (&buf, cl.viewangles[ROLL]);
395 MSG_WriteShort (&buf, cmd->forwardmove);
396 MSG_WriteShort (&buf, cmd->sidemove);
397 MSG_WriteShort (&buf, cmd->upmove);
400 // send button bits
402 bits = 0;
404 if ( in_attack.state & 3 )
405 bits |= 1;
406 in_attack.state &= ~2;
408 if (in_jump.state & 3)
409 bits |= 2;
410 in_jump.state &= ~2;
412 MSG_WriteByte (&buf, bits);
414 MSG_WriteByte (&buf, in_impulse);
415 in_impulse = 0;
417 #ifdef QUAKE2
419 // light level
421 MSG_WriteByte (&buf, cmd->lightlevel);
422 #endif
425 // deliver the message
427 if (cls.demoplayback)
428 return;
431 // allways dump the first two message, because it may contain leftover inputs
432 // from the last level
434 if (++cl.movemessages <= 2)
435 return;
437 if (NET_SendUnreliableMessage (cls.netcon, &buf) == -1)
439 Con_Printf ("CL_SendMove: lost server connection\n");
440 CL_Disconnect ();
445 ============
446 CL_InitInput
447 ============
449 void CL_InitInput (void)
451 Cmd_AddCommand ("+moveup",IN_UpDown);
452 Cmd_AddCommand ("-moveup",IN_UpUp);
453 Cmd_AddCommand ("+movedown",IN_DownDown);
454 Cmd_AddCommand ("-movedown",IN_DownUp);
455 Cmd_AddCommand ("+left",IN_LeftDown);
456 Cmd_AddCommand ("-left",IN_LeftUp);
457 Cmd_AddCommand ("+right",IN_RightDown);
458 Cmd_AddCommand ("-right",IN_RightUp);
459 Cmd_AddCommand ("+forward",IN_ForwardDown);
460 Cmd_AddCommand ("-forward",IN_ForwardUp);
461 Cmd_AddCommand ("+back",IN_BackDown);
462 Cmd_AddCommand ("-back",IN_BackUp);
463 Cmd_AddCommand ("+lookup", IN_LookupDown);
464 Cmd_AddCommand ("-lookup", IN_LookupUp);
465 Cmd_AddCommand ("+lookdown", IN_LookdownDown);
466 Cmd_AddCommand ("-lookdown", IN_LookdownUp);
467 Cmd_AddCommand ("+strafe", IN_StrafeDown);
468 Cmd_AddCommand ("-strafe", IN_StrafeUp);
469 Cmd_AddCommand ("+moveleft", IN_MoveleftDown);
470 Cmd_AddCommand ("-moveleft", IN_MoveleftUp);
471 Cmd_AddCommand ("+moveright", IN_MoverightDown);
472 Cmd_AddCommand ("-moveright", IN_MoverightUp);
473 Cmd_AddCommand ("+speed", IN_SpeedDown);
474 Cmd_AddCommand ("-speed", IN_SpeedUp);
475 Cmd_AddCommand ("+attack", IN_AttackDown);
476 Cmd_AddCommand ("-attack", IN_AttackUp);
477 Cmd_AddCommand ("+use", IN_UseDown);
478 Cmd_AddCommand ("-use", IN_UseUp);
479 Cmd_AddCommand ("+jump", IN_JumpDown);
480 Cmd_AddCommand ("-jump", IN_JumpUp);
481 Cmd_AddCommand ("impulse", IN_Impulse);
482 Cmd_AddCommand ("+klook", IN_KLookDown);
483 Cmd_AddCommand ("-klook", IN_KLookUp);
484 Cmd_AddCommand ("+mlook", IN_MLookDown);
485 Cmd_AddCommand ("-mlook", IN_MLookUp);