cl_main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /*
  2. Copyright (C) 1996-1997 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_main.c -- client main loop
  16. #include "quakedef.h"
  17. // we need to declare some mouse variables here, because the menu system
  18. // references them even when on a unix system.
  19. // these two are not intended to be set directly
  20. cvar_t cl_name = {"_cl_name", "player", true};
  21. cvar_t cl_color = {"_cl_color", "0", true};
  22. cvar_t cl_shownet = {"cl_shownet","0"}; // can be 0, 1, or 2
  23. cvar_t cl_nolerp = {"cl_nolerp","0"};
  24. cvar_t lookspring = {"lookspring","0", true};
  25. cvar_t lookstrafe = {"lookstrafe","0", true};
  26. cvar_t sensitivity = {"sensitivity","3", true};
  27. cvar_t m_pitch = {"m_pitch","0.022", true};
  28. cvar_t m_yaw = {"m_yaw","0.022", true};
  29. cvar_t m_forward = {"m_forward","1", true};
  30. cvar_t m_side = {"m_side","0.8", true};
  31. client_static_t cls;
  32. client_state_t cl;
  33. // FIXME: put these on hunk?
  34. efrag_t cl_efrags[MAX_EFRAGS];
  35. entity_t cl_entities[MAX_EDICTS];
  36. entity_t cl_static_entities[MAX_STATIC_ENTITIES];
  37. lightstyle_t cl_lightstyle[MAX_LIGHTSTYLES];
  38. dlight_t cl_dlights[MAX_DLIGHTS];
  39. int cl_numvisedicts;
  40. entity_t *cl_visedicts[MAX_VISEDICTS];
  41. /*
  42. =====================
  43. CL_ClearState
  44. =====================
  45. */
  46. void CL_ClearState (void)
  47. {
  48. int i;
  49. if (!sv.active)
  50. Host_ClearMemory ();
  51. // wipe the entire cl structure
  52. memset (&cl, 0, sizeof(cl));
  53. SZ_Clear (&cls.message);
  54. // clear other arrays
  55. memset (cl_efrags, 0, sizeof(cl_efrags));
  56. memset (cl_entities, 0, sizeof(cl_entities));
  57. memset (cl_dlights, 0, sizeof(cl_dlights));
  58. memset (cl_lightstyle, 0, sizeof(cl_lightstyle));
  59. memset (cl_temp_entities, 0, sizeof(cl_temp_entities));
  60. memset (cl_beams, 0, sizeof(cl_beams));
  61. //
  62. // allocate the efrags and chain together into a free list
  63. //
  64. cl.free_efrags = cl_efrags;
  65. for (i=0 ; i<MAX_EFRAGS-1 ; i++)
  66. cl.free_efrags[i].entnext = &cl.free_efrags[i+1];
  67. cl.free_efrags[i].entnext = NULL;
  68. }
  69. /*
  70. =====================
  71. CL_Disconnect
  72. Sends a disconnect message to the server
  73. This is also called on Host_Error, so it shouldn't cause any errors
  74. =====================
  75. */
  76. void CL_Disconnect (void)
  77. {
  78. // stop sounds (especially looping!)
  79. S_StopAllSounds (true);
  80. // bring the console down and fade the colors back to normal
  81. // SCR_BringDownConsole ();
  82. // if running a local server, shut it down
  83. if (cls.demoplayback)
  84. CL_StopPlayback ();
  85. else if (cls.state == ca_connected)
  86. {
  87. if (cls.demorecording)
  88. CL_Stop_f ();
  89. Con_DPrintf ("Sending clc_disconnect\n");
  90. SZ_Clear (&cls.message);
  91. MSG_WriteByte (&cls.message, clc_disconnect);
  92. NET_SendUnreliableMessage (cls.netcon, &cls.message);
  93. SZ_Clear (&cls.message);
  94. NET_Close (cls.netcon);
  95. cls.state = ca_disconnected;
  96. if (sv.active)
  97. Host_ShutdownServer(false);
  98. }
  99. cls.demoplayback = cls.timedemo = false;
  100. cls.signon = 0;
  101. }
  102. void CL_Disconnect_f (void)
  103. {
  104. CL_Disconnect ();
  105. if (sv.active)
  106. Host_ShutdownServer (false);
  107. }
  108. /*
  109. =====================
  110. CL_EstablishConnection
  111. Host should be either "local" or a net address to be passed on
  112. =====================
  113. */
  114. void CL_EstablishConnection (char *host)
  115. {
  116. if (cls.state == ca_dedicated)
  117. return;
  118. if (cls.demoplayback)
  119. return;
  120. CL_Disconnect ();
  121. cls.netcon = NET_Connect (host);
  122. if (!cls.netcon)
  123. Host_Error ("CL_Connect: connect failed\n");
  124. Con_DPrintf ("CL_EstablishConnection: connected to %s\n", host);
  125. cls.demonum = -1; // not in the demo loop now
  126. cls.state = ca_connected;
  127. cls.signon = 0; // need all the signon messages before playing
  128. }
  129. /*
  130. =====================
  131. CL_SignonReply
  132. An svc_signonnum has been received, perform a client side setup
  133. =====================
  134. */
  135. void CL_SignonReply (void)
  136. {
  137. char str[8192];
  138. Con_DPrintf ("CL_SignonReply: %i\n", cls.signon);
  139. switch (cls.signon)
  140. {
  141. case 1:
  142. MSG_WriteByte (&cls.message, clc_stringcmd);
  143. MSG_WriteString (&cls.message, "prespawn");
  144. break;
  145. case 2:
  146. MSG_WriteByte (&cls.message, clc_stringcmd);
  147. MSG_WriteString (&cls.message, va("name \"%s\"\n", cl_name.string));
  148. MSG_WriteByte (&cls.message, clc_stringcmd);
  149. MSG_WriteString (&cls.message, va("color %i %i\n", ((int)cl_color.value)>>4, ((int)cl_color.value)&15));
  150. MSG_WriteByte (&cls.message, clc_stringcmd);
  151. sprintf (str, "spawn %s", cls.spawnparms);
  152. MSG_WriteString (&cls.message, str);
  153. break;
  154. case 3:
  155. MSG_WriteByte (&cls.message, clc_stringcmd);
  156. MSG_WriteString (&cls.message, "begin");
  157. Cache_Report (); // print remaining memory
  158. break;
  159. case 4:
  160. SCR_EndLoadingPlaque (); // allow normal screen updates
  161. break;
  162. }
  163. }
  164. /*
  165. =====================
  166. CL_NextDemo
  167. Called to play the next demo in the demo loop
  168. =====================
  169. */
  170. void CL_NextDemo (void)
  171. {
  172. char str[1024];
  173. if (cls.demonum == -1)
  174. return; // don't play demos
  175. SCR_BeginLoadingPlaque ();
  176. if (!cls.demos[cls.demonum][0] || cls.demonum == MAX_DEMOS)
  177. {
  178. cls.demonum = 0;
  179. if (!cls.demos[cls.demonum][0])
  180. {
  181. Con_Printf ("No demos listed with startdemos\n");
  182. cls.demonum = -1;
  183. return;
  184. }
  185. }
  186. sprintf (str,"playdemo %s\n", cls.demos[cls.demonum]);
  187. Cbuf_InsertText (str);
  188. cls.demonum++;
  189. }
  190. /*
  191. ==============
  192. CL_PrintEntities_f
  193. ==============
  194. */
  195. void CL_PrintEntities_f (void)
  196. {
  197. entity_t *ent;
  198. int i;
  199. for (i=0,ent=cl_entities ; i<cl.num_entities ; i++,ent++)
  200. {
  201. Con_Printf ("%3i:",i);
  202. if (!ent->model)
  203. {
  204. Con_Printf ("EMPTY\n");
  205. continue;
  206. }
  207. Con_Printf ("%s:%2i (%5.1f,%5.1f,%5.1f) [%5.1f %5.1f %5.1f]\n"
  208. ,ent->model->name,ent->frame, ent->origin[0], ent->origin[1], ent->origin[2], ent->angles[0], ent->angles[1], ent->angles[2]);
  209. }
  210. }
  211. /*
  212. ===============
  213. SetPal
  214. Debugging tool, just flashes the screen
  215. ===============
  216. */
  217. void SetPal (int i)
  218. {
  219. #if 0
  220. static int old;
  221. byte pal[768];
  222. int c;
  223. if (i == old)
  224. return;
  225. old = i;
  226. if (i==0)
  227. VID_SetPalette (host_basepal);
  228. else if (i==1)
  229. {
  230. for (c=0 ; c<768 ; c+=3)
  231. {
  232. pal[c] = 0;
  233. pal[c+1] = 255;
  234. pal[c+2] = 0;
  235. }
  236. VID_SetPalette (pal);
  237. }
  238. else
  239. {
  240. for (c=0 ; c<768 ; c+=3)
  241. {
  242. pal[c] = 0;
  243. pal[c+1] = 0;
  244. pal[c+2] = 255;
  245. }
  246. VID_SetPalette (pal);
  247. }
  248. #endif
  249. }
  250. /*
  251. ===============
  252. CL_AllocDlight
  253. ===============
  254. */
  255. dlight_t *CL_AllocDlight (int key)
  256. {
  257. int i;
  258. dlight_t *dl;
  259. // first look for an exact key match
  260. if (key)
  261. {
  262. dl = cl_dlights;
  263. for (i=0 ; i<MAX_DLIGHTS ; i++, dl++)
  264. {
  265. if (dl->key == key)
  266. {
  267. memset (dl, 0, sizeof(*dl));
  268. dl->key = key;
  269. return dl;
  270. }
  271. }
  272. }
  273. // then look for anything else
  274. dl = cl_dlights;
  275. for (i=0 ; i<MAX_DLIGHTS ; i++, dl++)
  276. {
  277. if (dl->die < cl.time)
  278. {
  279. memset (dl, 0, sizeof(*dl));
  280. dl->key = key;
  281. return dl;
  282. }
  283. }
  284. dl = &cl_dlights[0];
  285. memset (dl, 0, sizeof(*dl));
  286. dl->key = key;
  287. return dl;
  288. }
  289. /*
  290. ===============
  291. CL_DecayLights
  292. ===============
  293. */
  294. void CL_DecayLights (void)
  295. {
  296. int i;
  297. dlight_t *dl;
  298. float time;
  299. time = cl.time - cl.oldtime;
  300. dl = cl_dlights;
  301. for (i=0 ; i<MAX_DLIGHTS ; i++, dl++)
  302. {
  303. if (dl->die < cl.time || !dl->radius)
  304. continue;
  305. dl->radius -= time*dl->decay;
  306. if (dl->radius < 0)
  307. dl->radius = 0;
  308. }
  309. }
  310. /*
  311. ===============
  312. CL_LerpPoint
  313. Determines the fraction between the last two messages that the objects
  314. should be put at.
  315. ===============
  316. */
  317. float CL_LerpPoint (void)
  318. {
  319. float f, frac;
  320. f = cl.mtime[0] - cl.mtime[1];
  321. if (!f || cl_nolerp.value || cls.timedemo || sv.active)
  322. {
  323. cl.time = cl.mtime[0];
  324. return 1;
  325. }
  326. if (f > 0.1)
  327. { // dropped packet, or start of demo
  328. cl.mtime[1] = cl.mtime[0] - 0.1;
  329. f = 0.1;
  330. }
  331. frac = (cl.time - cl.mtime[1]) / f;
  332. //Con_Printf ("frac: %f\n",frac);
  333. if (frac < 0)
  334. {
  335. if (frac < -0.01)
  336. {
  337. SetPal(1);
  338. cl.time = cl.mtime[1];
  339. // Con_Printf ("low frac\n");
  340. }
  341. frac = 0;
  342. }
  343. else if (frac > 1)
  344. {
  345. if (frac > 1.01)
  346. {
  347. SetPal(2);
  348. cl.time = cl.mtime[0];
  349. // Con_Printf ("high frac\n");
  350. }
  351. frac = 1;
  352. }
  353. else
  354. SetPal(0);
  355. return frac;
  356. }
  357. /*
  358. ===============
  359. CL_RelinkEntities
  360. ===============
  361. */
  362. void CL_RelinkEntities (void)
  363. {
  364. entity_t *ent;
  365. int i, j;
  366. float frac, f, d;
  367. vec3_t delta;
  368. float bobjrotate;
  369. vec3_t oldorg;
  370. dlight_t *dl;
  371. // determine partial update time
  372. frac = CL_LerpPoint ();
  373. cl_numvisedicts = 0;
  374. //
  375. // interpolate player info
  376. //
  377. for (i=0 ; i<3 ; i++)
  378. cl.velocity[i] = cl.mvelocity[1][i] +
  379. frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
  380. if (cls.demoplayback)
  381. {
  382. // interpolate the angles
  383. for (j=0 ; j<3 ; j++)
  384. {
  385. d = cl.mviewangles[0][j] - cl.mviewangles[1][j];
  386. if (d > 180)
  387. d -= 360;
  388. else if (d < -180)
  389. d += 360;
  390. cl.viewangles[j] = cl.mviewangles[1][j] + frac*d;
  391. }
  392. }
  393. bobjrotate = anglemod(100*cl.time);
  394. // start on the entity after the world
  395. for (i=1,ent=cl_entities+1 ; i<cl.num_entities ; i++,ent++)
  396. {
  397. if (!ent->model)
  398. { // empty slot
  399. if (ent->forcelink)
  400. R_RemoveEfrags (ent); // just became empty
  401. continue;
  402. }
  403. // if the object wasn't included in the last packet, remove it
  404. if (ent->msgtime != cl.mtime[0])
  405. {
  406. ent->model = NULL;
  407. continue;
  408. }
  409. VectorCopy (ent->origin, oldorg);
  410. if (ent->forcelink)
  411. { // the entity was not updated in the last message
  412. // so move to the final spot
  413. VectorCopy (ent->msg_origins[0], ent->origin);
  414. VectorCopy (ent->msg_angles[0], ent->angles);
  415. }
  416. else
  417. { // if the delta is large, assume a teleport and don't lerp
  418. f = frac;
  419. for (j=0 ; j<3 ; j++)
  420. {
  421. delta[j] = ent->msg_origins[0][j] - ent->msg_origins[1][j];
  422. if (delta[j] > 100 || delta[j] < -100)
  423. f = 1; // assume a teleportation, not a motion
  424. }
  425. // interpolate the origin and angles
  426. for (j=0 ; j<3 ; j++)
  427. {
  428. ent->origin[j] = ent->msg_origins[1][j] + f*delta[j];
  429. d = ent->msg_angles[0][j] - ent->msg_angles[1][j];
  430. if (d > 180)
  431. d -= 360;
  432. else if (d < -180)
  433. d += 360;
  434. ent->angles[j] = ent->msg_angles[1][j] + f*d;
  435. }
  436. }
  437. // rotate binary objects locally
  438. if (ent->model->flags & EF_ROTATE)
  439. ent->angles[1] = bobjrotate;
  440. if (ent->effects & EF_BRIGHTFIELD)
  441. R_EntityParticles (ent);
  442. #ifdef QUAKE2
  443. if (ent->effects & EF_DARKFIELD)
  444. R_DarkFieldParticles (ent);
  445. #endif
  446. if (ent->effects & EF_MUZZLEFLASH)
  447. {
  448. vec3_t fv, rv, uv;
  449. dl = CL_AllocDlight (i);
  450. VectorCopy (ent->origin, dl->origin);
  451. dl->origin[2] += 16;
  452. AngleVectors (ent->angles, fv, rv, uv);
  453. VectorMA (dl->origin, 18, fv, dl->origin);
  454. dl->radius = 200 + (rand()&31);
  455. dl->minlight = 32;
  456. dl->die = cl.time + 0.1;
  457. }
  458. if (ent->effects & EF_BRIGHTLIGHT)
  459. {
  460. dl = CL_AllocDlight (i);
  461. VectorCopy (ent->origin, dl->origin);
  462. dl->origin[2] += 16;
  463. dl->radius = 400 + (rand()&31);
  464. dl->die = cl.time + 0.001;
  465. }
  466. if (ent->effects & EF_DIMLIGHT)
  467. {
  468. dl = CL_AllocDlight (i);
  469. VectorCopy (ent->origin, dl->origin);
  470. dl->radius = 200 + (rand()&31);
  471. dl->die = cl.time + 0.001;
  472. }
  473. #ifdef QUAKE2
  474. if (ent->effects & EF_DARKLIGHT)
  475. {
  476. dl = CL_AllocDlight (i);
  477. VectorCopy (ent->origin, dl->origin);
  478. dl->radius = 200.0 + (rand()&31);
  479. dl->die = cl.time + 0.001;
  480. dl->dark = true;
  481. }
  482. if (ent->effects & EF_LIGHT)
  483. {
  484. dl = CL_AllocDlight (i);
  485. VectorCopy (ent->origin, dl->origin);
  486. dl->radius = 200;
  487. dl->die = cl.time + 0.001;
  488. }
  489. #endif
  490. if (ent->model->flags & EF_GIB)
  491. R_RocketTrail (oldorg, ent->origin, 2);
  492. else if (ent->model->flags & EF_ZOMGIB)
  493. R_RocketTrail (oldorg, ent->origin, 4);
  494. else if (ent->model->flags & EF_TRACER)
  495. R_RocketTrail (oldorg, ent->origin, 3);
  496. else if (ent->model->flags & EF_TRACER2)
  497. R_RocketTrail (oldorg, ent->origin, 5);
  498. else if (ent->model->flags & EF_ROCKET)
  499. {
  500. R_RocketTrail (oldorg, ent->origin, 0);
  501. dl = CL_AllocDlight (i);
  502. VectorCopy (ent->origin, dl->origin);
  503. dl->radius = 200;
  504. dl->die = cl.time + 0.01;
  505. }
  506. else if (ent->model->flags & EF_GRENADE)
  507. R_RocketTrail (oldorg, ent->origin, 1);
  508. else if (ent->model->flags & EF_TRACER3)
  509. R_RocketTrail (oldorg, ent->origin, 6);
  510. ent->forcelink = false;
  511. if (i == cl.viewentity && !chase_active.value)
  512. continue;
  513. #ifdef QUAKE2
  514. if ( ent->effects & EF_NODRAW )
  515. continue;
  516. #endif
  517. if (cl_numvisedicts < MAX_VISEDICTS)
  518. {
  519. cl_visedicts[cl_numvisedicts] = ent;
  520. cl_numvisedicts++;
  521. }
  522. }
  523. }
  524. /*
  525. ===============
  526. CL_ReadFromServer
  527. Read all incoming data from the server
  528. ===============
  529. */
  530. int CL_ReadFromServer (void)
  531. {
  532. int ret;
  533. cl.oldtime = cl.time;
  534. cl.time += host_frametime;
  535. do
  536. {
  537. ret = CL_GetMessage ();
  538. if (ret == -1)
  539. Host_Error ("CL_ReadFromServer: lost server connection");
  540. if (!ret)
  541. break;
  542. cl.last_received_message = realtime;
  543. CL_ParseServerMessage ();
  544. } while (ret && cls.state == ca_connected);
  545. if (cl_shownet.value)
  546. Con_Printf ("\n");
  547. CL_RelinkEntities ();
  548. CL_UpdateTEnts ();
  549. //
  550. // bring the links up to date
  551. //
  552. return 0;
  553. }
  554. /*
  555. =================
  556. CL_SendCmd
  557. =================
  558. */
  559. void CL_SendCmd (void)
  560. {
  561. usercmd_t cmd;
  562. if (cls.state != ca_connected)
  563. return;
  564. if (cls.signon == SIGNONS)
  565. {
  566. // get basic movement from keyboard
  567. CL_BaseMove (&cmd);
  568. // allow mice or other external controllers to add to the move
  569. IN_Move (&cmd);
  570. // send the unreliable message
  571. CL_SendMove (&cmd);
  572. }
  573. if (cls.demoplayback)
  574. {
  575. SZ_Clear (&cls.message);
  576. return;
  577. }
  578. // send the reliable message
  579. if (!cls.message.cursize)
  580. return; // no message at all
  581. if (!NET_CanSendMessage (cls.netcon))
  582. {
  583. Con_DPrintf ("CL_WriteToServer: can't send\n");
  584. return;
  585. }
  586. if (NET_SendMessage (cls.netcon, &cls.message) == -1)
  587. Host_Error ("CL_WriteToServer: lost server connection");
  588. SZ_Clear (&cls.message);
  589. }
  590. /*
  591. =================
  592. CL_Init
  593. =================
  594. */
  595. void CL_Init (void)
  596. {
  597. SZ_Alloc (&cls.message, 1024);
  598. CL_InitInput ();
  599. CL_InitTEnts ();
  600. //
  601. // register our commands
  602. //
  603. Cvar_RegisterVariable (&cl_name);
  604. Cvar_RegisterVariable (&cl_color);
  605. Cvar_RegisterVariable (&cl_upspeed);
  606. Cvar_RegisterVariable (&cl_forwardspeed);
  607. Cvar_RegisterVariable (&cl_backspeed);
  608. Cvar_RegisterVariable (&cl_sidespeed);
  609. Cvar_RegisterVariable (&cl_movespeedkey);
  610. Cvar_RegisterVariable (&cl_yawspeed);
  611. Cvar_RegisterVariable (&cl_pitchspeed);
  612. Cvar_RegisterVariable (&cl_anglespeedkey);
  613. Cvar_RegisterVariable (&cl_shownet);
  614. Cvar_RegisterVariable (&cl_nolerp);
  615. Cvar_RegisterVariable (&lookspring);
  616. Cvar_RegisterVariable (&lookstrafe);
  617. Cvar_RegisterVariable (&sensitivity);
  618. Cvar_RegisterVariable (&m_pitch);
  619. Cvar_RegisterVariable (&m_yaw);
  620. Cvar_RegisterVariable (&m_forward);
  621. Cvar_RegisterVariable (&m_side);
  622. // Cvar_RegisterVariable (&cl_autofire);
  623. Cmd_AddCommand ("entities", CL_PrintEntities_f);
  624. Cmd_AddCommand ("disconnect", CL_Disconnect_f);
  625. Cmd_AddCommand ("record", CL_Record_f);
  626. Cmd_AddCommand ("stop", CL_Stop_f);
  627. Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
  628. Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
  629. }