cl_input.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. Copyright (C) 1997-2001 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. // cl.input.c -- builds an intended movement command to send to the server
  16. #include "client.h"
  17. cvar_t *cl_nodelta;
  18. extern unsigned sys_frame_time;
  19. unsigned frame_msec;
  20. unsigned old_sys_frame_time;
  21. /*
  22. ===============================================================================
  23. KEY BUTTONS
  24. Continuous button event tracking is complicated by the fact that two different
  25. input sources (say, mouse button 1 and the control key) can both press the
  26. same button, but the button should only be released when both of the
  27. pressing key have been released.
  28. When a key event issues a button command (+forward, +attack, etc), it appends
  29. its key number as a parameter to the command so it can be matched up with
  30. the release.
  31. state bit 0 is the current state of the key
  32. state bit 1 is edge triggered on the up to down transition
  33. state bit 2 is edge triggered on the down to up transition
  34. Key_Event (int key, qboolean down, unsigned time);
  35. +mlook src time
  36. ===============================================================================
  37. */
  38. kbutton_t in_klook;
  39. kbutton_t in_left, in_right, in_forward, in_back;
  40. kbutton_t in_lookup, in_lookdown, in_moveleft, in_moveright;
  41. kbutton_t in_strafe, in_speed, in_use, in_attack;
  42. kbutton_t in_up, in_down;
  43. int in_impulse;
  44. void KeyDown (kbutton_t *b)
  45. {
  46. int k;
  47. char *c;
  48. c = Cmd_Argv(1);
  49. if (c[0])
  50. k = atoi(c);
  51. else
  52. k = -1; // typed manually at the console for continuous down
  53. if (k == b->down[0] || k == b->down[1])
  54. return; // repeating key
  55. if (!b->down[0])
  56. b->down[0] = k;
  57. else if (!b->down[1])
  58. b->down[1] = k;
  59. else
  60. {
  61. Com_Printf ("Three keys down for a button!\n");
  62. return;
  63. }
  64. if (b->state & 1)
  65. return; // still down
  66. // save timestamp
  67. c = Cmd_Argv(2);
  68. b->downtime = atoi(c);
  69. if (!b->downtime)
  70. b->downtime = sys_frame_time - 100;
  71. b->state |= 1 + 2; // down + impulse down
  72. }
  73. void KeyUp (kbutton_t *b)
  74. {
  75. int k;
  76. char *c;
  77. unsigned uptime;
  78. c = Cmd_Argv(1);
  79. if (c[0])
  80. k = atoi(c);
  81. else
  82. { // typed manually at the console, assume for unsticking, so clear all
  83. b->down[0] = b->down[1] = 0;
  84. b->state = 4; // impulse up
  85. return;
  86. }
  87. if (b->down[0] == k)
  88. b->down[0] = 0;
  89. else if (b->down[1] == k)
  90. b->down[1] = 0;
  91. else
  92. return; // key up without coresponding down (menu pass through)
  93. if (b->down[0] || b->down[1])
  94. return; // some other key is still holding it down
  95. if (!(b->state & 1))
  96. return; // still up (this should not happen)
  97. // save timestamp
  98. c = Cmd_Argv(2);
  99. uptime = atoi(c);
  100. if (uptime)
  101. b->msec += uptime - b->downtime;
  102. else
  103. b->msec += 10;
  104. b->state &= ~1; // now up
  105. b->state |= 4; // impulse up
  106. }
  107. void IN_KLookDown (void) {KeyDown(&in_klook);}
  108. void IN_KLookUp (void) {KeyUp(&in_klook);}
  109. void IN_UpDown(void) {KeyDown(&in_up);}
  110. void IN_UpUp(void) {KeyUp(&in_up);}
  111. void IN_DownDown(void) {KeyDown(&in_down);}
  112. void IN_DownUp(void) {KeyUp(&in_down);}
  113. void IN_LeftDown(void) {KeyDown(&in_left);}
  114. void IN_LeftUp(void) {KeyUp(&in_left);}
  115. void IN_RightDown(void) {KeyDown(&in_right);}
  116. void IN_RightUp(void) {KeyUp(&in_right);}
  117. void IN_ForwardDown(void) {KeyDown(&in_forward);}
  118. void IN_ForwardUp(void) {KeyUp(&in_forward);}
  119. void IN_BackDown(void) {KeyDown(&in_back);}
  120. void IN_BackUp(void) {KeyUp(&in_back);}
  121. void IN_LookupDown(void) {KeyDown(&in_lookup);}
  122. void IN_LookupUp(void) {KeyUp(&in_lookup);}
  123. void IN_LookdownDown(void) {KeyDown(&in_lookdown);}
  124. void IN_LookdownUp(void) {KeyUp(&in_lookdown);}
  125. void IN_MoveleftDown(void) {KeyDown(&in_moveleft);}
  126. void IN_MoveleftUp(void) {KeyUp(&in_moveleft);}
  127. void IN_MoverightDown(void) {KeyDown(&in_moveright);}
  128. void IN_MoverightUp(void) {KeyUp(&in_moveright);}
  129. void IN_SpeedDown(void) {KeyDown(&in_speed);}
  130. void IN_SpeedUp(void) {KeyUp(&in_speed);}
  131. void IN_StrafeDown(void) {KeyDown(&in_strafe);}
  132. void IN_StrafeUp(void) {KeyUp(&in_strafe);}
  133. void IN_AttackDown(void) {KeyDown(&in_attack);}
  134. void IN_AttackUp(void) {KeyUp(&in_attack);}
  135. void IN_UseDown (void) {KeyDown(&in_use);}
  136. void IN_UseUp (void) {KeyUp(&in_use);}
  137. void IN_Impulse (void) {in_impulse=atoi(Cmd_Argv(1));}
  138. /*
  139. ===============
  140. CL_KeyState
  141. Returns the fraction of the frame that the key was down
  142. ===============
  143. */
  144. float CL_KeyState (kbutton_t *key)
  145. {
  146. float val;
  147. int msec;
  148. key->state &= 1; // clear impulses
  149. msec = key->msec;
  150. key->msec = 0;
  151. if (key->state)
  152. { // still down
  153. msec += sys_frame_time - key->downtime;
  154. key->downtime = sys_frame_time;
  155. }
  156. #if 0
  157. if (msec)
  158. {
  159. Com_Printf ("%i ", msec);
  160. }
  161. #endif
  162. val = (float)msec / frame_msec;
  163. if (val < 0)
  164. val = 0;
  165. if (val > 1)
  166. val = 1;
  167. return val;
  168. }
  169. //==========================================================================
  170. cvar_t *cl_upspeed;
  171. cvar_t *cl_forwardspeed;
  172. cvar_t *cl_sidespeed;
  173. cvar_t *cl_yawspeed;
  174. cvar_t *cl_pitchspeed;
  175. cvar_t *cl_run;
  176. cvar_t *cl_anglespeedkey;
  177. /*
  178. ================
  179. CL_AdjustAngles
  180. Moves the local angle positions
  181. ================
  182. */
  183. void CL_AdjustAngles (void)
  184. {
  185. float speed;
  186. float up, down;
  187. if (in_speed.state & 1)
  188. speed = cls.frametime * cl_anglespeedkey->value;
  189. else
  190. speed = cls.frametime;
  191. if (!(in_strafe.state & 1))
  192. {
  193. cl.viewangles[YAW] -= speed*cl_yawspeed->value*CL_KeyState (&in_right);
  194. cl.viewangles[YAW] += speed*cl_yawspeed->value*CL_KeyState (&in_left);
  195. }
  196. if (in_klook.state & 1)
  197. {
  198. cl.viewangles[PITCH] -= speed*cl_pitchspeed->value * CL_KeyState (&in_forward);
  199. cl.viewangles[PITCH] += speed*cl_pitchspeed->value * CL_KeyState (&in_back);
  200. }
  201. up = CL_KeyState (&in_lookup);
  202. down = CL_KeyState(&in_lookdown);
  203. cl.viewangles[PITCH] -= speed*cl_pitchspeed->value * up;
  204. cl.viewangles[PITCH] += speed*cl_pitchspeed->value * down;
  205. }
  206. /*
  207. ================
  208. CL_BaseMove
  209. Send the intended movement message to the server
  210. ================
  211. */
  212. void CL_BaseMove (usercmd_t *cmd)
  213. {
  214. CL_AdjustAngles ();
  215. memset (cmd, 0, sizeof(*cmd));
  216. VectorCopy (cl.viewangles, cmd->angles);
  217. if (in_strafe.state & 1)
  218. {
  219. cmd->sidemove += cl_sidespeed->value * CL_KeyState (&in_right);
  220. cmd->sidemove -= cl_sidespeed->value * CL_KeyState (&in_left);
  221. }
  222. cmd->sidemove += cl_sidespeed->value * CL_KeyState (&in_moveright);
  223. cmd->sidemove -= cl_sidespeed->value * CL_KeyState (&in_moveleft);
  224. cmd->upmove += cl_upspeed->value * CL_KeyState (&in_up);
  225. cmd->upmove -= cl_upspeed->value * CL_KeyState (&in_down);
  226. if (! (in_klook.state & 1) )
  227. {
  228. cmd->forwardmove += cl_forwardspeed->value * CL_KeyState (&in_forward);
  229. cmd->forwardmove -= cl_forwardspeed->value * CL_KeyState (&in_back);
  230. }
  231. //
  232. // adjust for speed key / running
  233. //
  234. if ( (in_speed.state & 1) ^ (int)(cl_run->value) )
  235. {
  236. cmd->forwardmove *= 2;
  237. cmd->sidemove *= 2;
  238. cmd->upmove *= 2;
  239. }
  240. }
  241. void CL_ClampPitch (void)
  242. {
  243. float pitch;
  244. pitch = SHORT2ANGLE(cl.frame.playerstate.pmove.delta_angles[PITCH]);
  245. if (pitch > 180)
  246. pitch -= 360;
  247. if (cl.viewangles[PITCH] + pitch > 89)
  248. cl.viewangles[PITCH] = 89 - pitch;
  249. if (cl.viewangles[PITCH] + pitch < -89)
  250. cl.viewangles[PITCH] = -89 - pitch;
  251. }
  252. /*
  253. ==============
  254. CL_FinishMove
  255. ==============
  256. */
  257. void CL_FinishMove (usercmd_t *cmd)
  258. {
  259. int ms;
  260. int i;
  261. //
  262. // figure button bits
  263. //
  264. if ( in_attack.state & 3 )
  265. cmd->buttons |= BUTTON_ATTACK;
  266. in_attack.state &= ~2;
  267. if (in_use.state & 3)
  268. cmd->buttons |= BUTTON_USE;
  269. in_use.state &= ~2;
  270. if (anykeydown && cls.key_dest == key_game)
  271. cmd->buttons |= BUTTON_ANY;
  272. // send milliseconds of time to apply the move
  273. ms = cls.frametime * 1000;
  274. if (ms > 250)
  275. ms = 100; // time was unreasonable
  276. cmd->msec = ms;
  277. CL_ClampPitch ();
  278. for (i=0 ; i<3 ; i++)
  279. cmd->angles[i] = ANGLE2SHORT(cl.viewangles[i]);
  280. cmd->impulse = in_impulse;
  281. in_impulse = 0;
  282. // send the ambient light level at the player's current position
  283. cmd->lightlevel = (byte)cl_lightlevel->value;
  284. }
  285. /*
  286. =================
  287. CL_CreateCmd
  288. =================
  289. */
  290. usercmd_t CL_CreateCmd (void)
  291. {
  292. usercmd_t cmd;
  293. frame_msec = sys_frame_time - old_sys_frame_time;
  294. if (frame_msec < 1)
  295. frame_msec = 1;
  296. if (frame_msec > 200)
  297. frame_msec = 200;
  298. // get basic movement from keyboard
  299. CL_BaseMove (&cmd);
  300. // allow mice or other external controllers to add to the move
  301. IN_Move (&cmd);
  302. CL_FinishMove (&cmd);
  303. old_sys_frame_time = sys_frame_time;
  304. //cmd.impulse = cls.framecount;
  305. return cmd;
  306. }
  307. void IN_CenterView (void)
  308. {
  309. cl.viewangles[PITCH] = -SHORT2ANGLE(cl.frame.playerstate.pmove.delta_angles[PITCH]);
  310. }
  311. /*
  312. ============
  313. CL_InitInput
  314. ============
  315. */
  316. void CL_InitInput (void)
  317. {
  318. Cmd_AddCommand ("centerview",IN_CenterView);
  319. Cmd_AddCommand ("+moveup",IN_UpDown);
  320. Cmd_AddCommand ("-moveup",IN_UpUp);
  321. Cmd_AddCommand ("+movedown",IN_DownDown);
  322. Cmd_AddCommand ("-movedown",IN_DownUp);
  323. Cmd_AddCommand ("+left",IN_LeftDown);
  324. Cmd_AddCommand ("-left",IN_LeftUp);
  325. Cmd_AddCommand ("+right",IN_RightDown);
  326. Cmd_AddCommand ("-right",IN_RightUp);
  327. Cmd_AddCommand ("+forward",IN_ForwardDown);
  328. Cmd_AddCommand ("-forward",IN_ForwardUp);
  329. Cmd_AddCommand ("+back",IN_BackDown);
  330. Cmd_AddCommand ("-back",IN_BackUp);
  331. Cmd_AddCommand ("+lookup", IN_LookupDown);
  332. Cmd_AddCommand ("-lookup", IN_LookupUp);
  333. Cmd_AddCommand ("+lookdown", IN_LookdownDown);
  334. Cmd_AddCommand ("-lookdown", IN_LookdownUp);
  335. Cmd_AddCommand ("+strafe", IN_StrafeDown);
  336. Cmd_AddCommand ("-strafe", IN_StrafeUp);
  337. Cmd_AddCommand ("+moveleft", IN_MoveleftDown);
  338. Cmd_AddCommand ("-moveleft", IN_MoveleftUp);
  339. Cmd_AddCommand ("+moveright", IN_MoverightDown);
  340. Cmd_AddCommand ("-moveright", IN_MoverightUp);
  341. Cmd_AddCommand ("+speed", IN_SpeedDown);
  342. Cmd_AddCommand ("-speed", IN_SpeedUp);
  343. Cmd_AddCommand ("+attack", IN_AttackDown);
  344. Cmd_AddCommand ("-attack", IN_AttackUp);
  345. Cmd_AddCommand ("+use", IN_UseDown);
  346. Cmd_AddCommand ("-use", IN_UseUp);
  347. Cmd_AddCommand ("impulse", IN_Impulse);
  348. Cmd_AddCommand ("+klook", IN_KLookDown);
  349. Cmd_AddCommand ("-klook", IN_KLookUp);
  350. cl_nodelta = Cvar_Get ("cl_nodelta", "0", 0);
  351. }
  352. /*
  353. =================
  354. CL_SendCmd
  355. =================
  356. */
  357. void CL_SendCmd (void)
  358. {
  359. sizebuf_t buf;
  360. byte data[128];
  361. int i;
  362. usercmd_t *cmd, *oldcmd;
  363. usercmd_t nullcmd;
  364. int checksumIndex;
  365. // build a command even if not connected
  366. // save this command off for prediction
  367. i = cls.netchan.outgoing_sequence & (CMD_BACKUP-1);
  368. cmd = &cl.cmds[i];
  369. cl.cmd_time[i] = cls.realtime; // for netgraph ping calculation
  370. *cmd = CL_CreateCmd ();
  371. cl.cmd = *cmd;
  372. if (cls.state == ca_disconnected || cls.state == ca_connecting)
  373. return;
  374. if ( cls.state == ca_connected)
  375. {
  376. if (cls.netchan.message.cursize || curtime - cls.netchan.last_sent > 1000 )
  377. Netchan_Transmit (&cls.netchan, 0, buf.data);
  378. return;
  379. }
  380. // send a userinfo update if needed
  381. if (userinfo_modified)
  382. {
  383. CL_FixUpGender();
  384. userinfo_modified = false;
  385. MSG_WriteByte (&cls.netchan.message, clc_userinfo);
  386. MSG_WriteString (&cls.netchan.message, Cvar_Userinfo() );
  387. }
  388. SZ_Init (&buf, data, sizeof(data));
  389. if (cmd->buttons && cl.cinematictime > 0 && !cl.attractloop
  390. && cls.realtime - cl.cinematictime > 1000)
  391. { // skip the rest of the cinematic
  392. SCR_FinishCinematic ();
  393. }
  394. // begin a client move command
  395. MSG_WriteByte (&buf, clc_move);
  396. // save the position for a checksum byte
  397. checksumIndex = buf.cursize;
  398. MSG_WriteByte (&buf, 0);
  399. // let the server know what the last frame we
  400. // got was, so the next message can be delta compressed
  401. if (cl_nodelta->value || !cl.frame.valid || cls.demowaiting)
  402. MSG_WriteLong (&buf, -1); // no compression
  403. else
  404. MSG_WriteLong (&buf, cl.frame.serverframe);
  405. // send this and the previous cmds in the message, so
  406. // if the last packet was dropped, it can be recovered
  407. i = (cls.netchan.outgoing_sequence-2) & (CMD_BACKUP-1);
  408. cmd = &cl.cmds[i];
  409. memset (&nullcmd, 0, sizeof(nullcmd));
  410. MSG_WriteDeltaUsercmd (&buf, &nullcmd, cmd);
  411. oldcmd = cmd;
  412. i = (cls.netchan.outgoing_sequence-1) & (CMD_BACKUP-1);
  413. cmd = &cl.cmds[i];
  414. MSG_WriteDeltaUsercmd (&buf, oldcmd, cmd);
  415. oldcmd = cmd;
  416. i = (cls.netchan.outgoing_sequence) & (CMD_BACKUP-1);
  417. cmd = &cl.cmds[i];
  418. MSG_WriteDeltaUsercmd (&buf, oldcmd, cmd);
  419. // calculate a checksum over the move commands
  420. buf.data[checksumIndex] = COM_BlockSequenceCRCByte(
  421. buf.data + checksumIndex + 1, buf.cursize - checksumIndex - 1,
  422. cls.netchan.outgoing_sequence);
  423. //
  424. // deliver the message
  425. //
  426. Netchan_Transmit (&cls.netchan, buf.cursize, buf.data);
  427. }