cl_cam.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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 included (GNU.txt) 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. /* ZOID
  16. *
  17. * Player camera tracking in Spectator mode
  18. *
  19. * This takes over player controls for spectator automatic camera.
  20. * Player moves as a spectator, but the camera tracks and enemy player
  21. */
  22. #include "quakedef.h"
  23. #include "winquake.h"
  24. #define PM_SPECTATORMAXSPEED 500
  25. #define PM_STOPSPEED 100
  26. #define PM_MAXSPEED 320
  27. #define BUTTON_JUMP 2
  28. #define BUTTON_ATTACK 1
  29. #define MAX_ANGLE_TURN 10
  30. static vec3_t desired_position; // where the camera wants to be
  31. static qboolean locked = false;
  32. static int oldbuttons;
  33. // track high fragger
  34. cvar_t cl_hightrack = {"cl_hightrack", "0" };
  35. cvar_t cl_chasecam = {"cl_chasecam", "0"};
  36. //cvar_t cl_camera_maxpitch = {"cl_camera_maxpitch", "10" };
  37. //cvar_t cl_camera_maxyaw = {"cl_camera_maxyaw", "30" };
  38. qboolean cam_forceview;
  39. vec3_t cam_viewangles;
  40. double cam_lastviewtime;
  41. int spec_track = 0; // player# of who we are tracking
  42. int autocam = CAM_NONE;
  43. static void vectoangles(vec3_t vec, vec3_t ang)
  44. {
  45. float forward;
  46. float yaw, pitch;
  47. if (vec[1] == 0 && vec[0] == 0)
  48. {
  49. yaw = 0;
  50. if (vec[2] > 0)
  51. pitch = 90;
  52. else
  53. pitch = 270;
  54. }
  55. else
  56. {
  57. yaw = (int) (atan2(vec[1], vec[0]) * 180 / M_PI);
  58. if (yaw < 0)
  59. yaw += 360;
  60. forward = sqrt (vec[0]*vec[0] + vec[1]*vec[1]);
  61. pitch = (int) (atan2(vec[2], forward) * 180 / M_PI);
  62. if (pitch < 0)
  63. pitch += 360;
  64. }
  65. ang[0] = pitch;
  66. ang[1] = yaw;
  67. ang[2] = 0;
  68. }
  69. static float vlen(vec3_t v)
  70. {
  71. return sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
  72. }
  73. // returns true if weapon model should be drawn in camera mode
  74. qboolean Cam_DrawViewModel(void)
  75. {
  76. if (!cl.spectator)
  77. return true;
  78. if (autocam && locked && cl_chasecam.value)
  79. return true;
  80. return false;
  81. }
  82. // returns true if we should draw this player, we don't if we are chase camming
  83. qboolean Cam_DrawPlayer(int playernum)
  84. {
  85. if (cl.spectator && autocam && locked && cl_chasecam.value &&
  86. spec_track == playernum)
  87. return false;
  88. return true;
  89. }
  90. void Cam_Unlock(void)
  91. {
  92. if (autocam) {
  93. MSG_WriteByte (&cls.netchan.message, clc_stringcmd);
  94. MSG_WriteString (&cls.netchan.message, "ptrack");
  95. autocam = CAM_NONE;
  96. locked = false;
  97. Sbar_Changed();
  98. }
  99. }
  100. void Cam_Lock(int playernum)
  101. {
  102. char st[40];
  103. sprintf(st, "ptrack %i", playernum);
  104. MSG_WriteByte (&cls.netchan.message, clc_stringcmd);
  105. MSG_WriteString (&cls.netchan.message, st);
  106. spec_track = playernum;
  107. cam_forceview = true;
  108. locked = false;
  109. Sbar_Changed();
  110. }
  111. pmtrace_t Cam_DoTrace(vec3_t vec1, vec3_t vec2)
  112. {
  113. #if 0
  114. memset(&pmove, 0, sizeof(pmove));
  115. pmove.numphysent = 1;
  116. VectorCopy (vec3_origin, pmove.physents[0].origin);
  117. pmove.physents[0].model = cl.worldmodel;
  118. #endif
  119. VectorCopy (vec1, pmove.origin);
  120. return PM_PlayerMove(pmove.origin, vec2);
  121. }
  122. // Returns distance or 9999 if invalid for some reason
  123. static float Cam_TryFlyby(player_state_t *self, player_state_t *player, vec3_t vec, qboolean checkvis)
  124. {
  125. vec3_t v;
  126. pmtrace_t trace;
  127. float len;
  128. vectoangles(vec, v);
  129. // v[0] = -v[0];
  130. VectorCopy (v, pmove.angles);
  131. VectorNormalize(vec);
  132. VectorMA(player->origin, 800, vec, v);
  133. // v is endpos
  134. // fake a player move
  135. trace = Cam_DoTrace(player->origin, v);
  136. if (/*trace.inopen ||*/ trace.inwater)
  137. return 9999;
  138. VectorCopy(trace.endpos, vec);
  139. VectorSubtract(trace.endpos, player->origin, v);
  140. len = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
  141. if (len < 32 || len > 800)
  142. return 9999;
  143. if (checkvis) {
  144. VectorSubtract(trace.endpos, self->origin, v);
  145. len = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
  146. trace = Cam_DoTrace(self->origin, vec);
  147. if (trace.fraction != 1 || trace.inwater)
  148. return 9999;
  149. }
  150. return len;
  151. }
  152. // Is player visible?
  153. static qboolean Cam_IsVisible(player_state_t *player, vec3_t vec)
  154. {
  155. pmtrace_t trace;
  156. vec3_t v;
  157. float d;
  158. trace = Cam_DoTrace(player->origin, vec);
  159. if (trace.fraction != 1 || /*trace.inopen ||*/ trace.inwater)
  160. return false;
  161. // check distance, don't let the player get too far away or too close
  162. VectorSubtract(player->origin, vec, v);
  163. d = vlen(v);
  164. if (d < 16)
  165. return false;
  166. return true;
  167. }
  168. static qboolean InitFlyby(player_state_t *self, player_state_t *player, int checkvis)
  169. {
  170. float f, max;
  171. vec3_t vec, vec2;
  172. vec3_t forward, right, up;
  173. VectorCopy(player->viewangles, vec);
  174. vec[0] = 0;
  175. AngleVectors (vec, forward, right, up);
  176. // for (i = 0; i < 3; i++)
  177. // forward[i] *= 3;
  178. max = 1000;
  179. VectorAdd(forward, up, vec2);
  180. VectorAdd(vec2, right, vec2);
  181. if ((f = Cam_TryFlyby(self, player, vec2, checkvis)) < max) {
  182. max = f;
  183. VectorCopy(vec2, vec);
  184. }
  185. VectorAdd(forward, up, vec2);
  186. VectorSubtract(vec2, right, vec2);
  187. if ((f = Cam_TryFlyby(self, player, vec2, checkvis)) < max) {
  188. max = f;
  189. VectorCopy(vec2, vec);
  190. }
  191. VectorAdd(forward, right, vec2);
  192. if ((f = Cam_TryFlyby(self, player, vec2, checkvis)) < max) {
  193. max = f;
  194. VectorCopy(vec2, vec);
  195. }
  196. VectorSubtract(forward, right, vec2);
  197. if ((f = Cam_TryFlyby(self, player, vec2, checkvis)) < max) {
  198. max = f;
  199. VectorCopy(vec2, vec);
  200. }
  201. VectorAdd(forward, up, vec2);
  202. if ((f = Cam_TryFlyby(self, player, vec2, checkvis)) < max) {
  203. max = f;
  204. VectorCopy(vec2, vec);
  205. }
  206. VectorSubtract(forward, up, vec2);
  207. if ((f = Cam_TryFlyby(self, player, vec2, checkvis)) < max) {
  208. max = f;
  209. VectorCopy(vec2, vec);
  210. }
  211. VectorAdd(up, right, vec2);
  212. VectorSubtract(vec2, forward, vec2);
  213. if ((f = Cam_TryFlyby(self, player, vec2, checkvis)) < max) {
  214. max = f;
  215. VectorCopy(vec2, vec);
  216. }
  217. VectorSubtract(up, right, vec2);
  218. VectorSubtract(vec2, forward, vec2);
  219. if ((f = Cam_TryFlyby(self, player, vec2, checkvis)) < max) {
  220. max = f;
  221. VectorCopy(vec2, vec);
  222. }
  223. // invert
  224. VectorSubtract(vec3_origin, forward, vec2);
  225. if ((f = Cam_TryFlyby(self, player, vec2, checkvis)) < max) {
  226. max = f;
  227. VectorCopy(vec2, vec);
  228. }
  229. VectorCopy(forward, vec2);
  230. if ((f = Cam_TryFlyby(self, player, vec2, checkvis)) < max) {
  231. max = f;
  232. VectorCopy(vec2, vec);
  233. }
  234. // invert
  235. VectorSubtract(vec3_origin, right, vec2);
  236. if ((f = Cam_TryFlyby(self, player, vec2, checkvis)) < max) {
  237. max = f;
  238. VectorCopy(vec2, vec);
  239. }
  240. VectorCopy(right, vec2);
  241. if ((f = Cam_TryFlyby(self, player, vec2, checkvis)) < max) {
  242. max = f;
  243. VectorCopy(vec2, vec);
  244. }
  245. // ack, can't find him
  246. if (max >= 1000) {
  247. // Cam_Unlock();
  248. return false;
  249. }
  250. locked = true;
  251. VectorCopy(vec, desired_position);
  252. return true;
  253. }
  254. static void Cam_CheckHighTarget(void)
  255. {
  256. int i, j, max;
  257. player_info_t *s;
  258. j = -1;
  259. for (i = 0, max = -9999; i < MAX_CLIENTS; i++) {
  260. s = &cl.players[i];
  261. if (s->name[0] && !s->spectator && s->frags > max) {
  262. max = s->frags;
  263. j = i;
  264. }
  265. }
  266. if (j >= 0) {
  267. if (!locked || cl.players[j].frags > cl.players[spec_track].frags)
  268. Cam_Lock(j);
  269. } else
  270. Cam_Unlock();
  271. }
  272. // ZOID
  273. //
  274. // Take over the user controls and track a player.
  275. // We find a nice position to watch the player and move there
  276. void Cam_Track(usercmd_t *cmd)
  277. {
  278. player_state_t *player, *self;
  279. frame_t *frame;
  280. vec3_t vec;
  281. float len;
  282. if (!cl.spectator)
  283. return;
  284. if (cl_hightrack.value && !locked)
  285. Cam_CheckHighTarget();
  286. if (!autocam || cls.state != ca_active)
  287. return;
  288. if (locked && (!cl.players[spec_track].name[0] || cl.players[spec_track].spectator)) {
  289. locked = false;
  290. if (cl_hightrack.value)
  291. Cam_CheckHighTarget();
  292. else
  293. Cam_Unlock();
  294. return;
  295. }
  296. frame = &cl.frames[cls.netchan.incoming_sequence & UPDATE_MASK];
  297. player = frame->playerstate + spec_track;
  298. self = frame->playerstate + cl.playernum;
  299. if (!locked || !Cam_IsVisible(player, desired_position)) {
  300. if (!locked || realtime - cam_lastviewtime > 0.1) {
  301. if (!InitFlyby(self, player, true))
  302. InitFlyby(self, player, false);
  303. cam_lastviewtime = realtime;
  304. }
  305. } else
  306. cam_lastviewtime = realtime;
  307. // couldn't track for some reason
  308. if (!locked || !autocam)
  309. return;
  310. if (cl_chasecam.value) {
  311. cmd->forwardmove = cmd->sidemove = cmd->upmove = 0;
  312. VectorCopy(player->viewangles, cl.viewangles);
  313. VectorCopy(player->origin, desired_position);
  314. if (memcmp(&desired_position, &self->origin, sizeof(desired_position)) != 0) {
  315. MSG_WriteByte (&cls.netchan.message, clc_tmove);
  316. MSG_WriteCoord (&cls.netchan.message, desired_position[0]);
  317. MSG_WriteCoord (&cls.netchan.message, desired_position[1]);
  318. MSG_WriteCoord (&cls.netchan.message, desired_position[2]);
  319. // move there locally immediately
  320. VectorCopy(desired_position, self->origin);
  321. }
  322. self->weaponframe = player->weaponframe;
  323. } else {
  324. // Ok, move to our desired position and set our angles to view
  325. // the player
  326. VectorSubtract(desired_position, self->origin, vec);
  327. len = vlen(vec);
  328. cmd->forwardmove = cmd->sidemove = cmd->upmove = 0;
  329. if (len > 16) { // close enough?
  330. MSG_WriteByte (&cls.netchan.message, clc_tmove);
  331. MSG_WriteCoord (&cls.netchan.message, desired_position[0]);
  332. MSG_WriteCoord (&cls.netchan.message, desired_position[1]);
  333. MSG_WriteCoord (&cls.netchan.message, desired_position[2]);
  334. }
  335. // move there locally immediately
  336. VectorCopy(desired_position, self->origin);
  337. VectorSubtract(player->origin, desired_position, vec);
  338. vectoangles(vec, cl.viewangles);
  339. cl.viewangles[0] = -cl.viewangles[0];
  340. }
  341. }
  342. #if 0
  343. static float adjustang(float current, float ideal, float speed)
  344. {
  345. float move;
  346. current = anglemod(current);
  347. ideal = anglemod(ideal);
  348. if (current == ideal)
  349. return current;
  350. move = ideal - current;
  351. if (ideal > current)
  352. {
  353. if (move >= 180)
  354. move = move - 360;
  355. }
  356. else
  357. {
  358. if (move <= -180)
  359. move = move + 360;
  360. }
  361. if (move > 0)
  362. {
  363. if (move > speed)
  364. move = speed;
  365. }
  366. else
  367. {
  368. if (move < -speed)
  369. move = -speed;
  370. }
  371. //Con_Printf("c/i: %4.2f/%4.2f move: %4.2f\n", current, ideal, move);
  372. return anglemod (current + move);
  373. }
  374. #endif
  375. #if 0
  376. void Cam_SetView(void)
  377. {
  378. return;
  379. player_state_t *player, *self;
  380. frame_t *frame;
  381. vec3_t vec, vec2;
  382. if (cls.state != ca_active || !cl.spectator ||
  383. !autocam || !locked)
  384. return;
  385. frame = &cl.frames[cls.netchan.incoming_sequence & UPDATE_MASK];
  386. player = frame->playerstate + spec_track;
  387. self = frame->playerstate + cl.playernum;
  388. VectorSubtract(player->origin, cl.simorg, vec);
  389. if (cam_forceview) {
  390. cam_forceview = false;
  391. vectoangles(vec, cam_viewangles);
  392. cam_viewangles[0] = -cam_viewangles[0];
  393. } else {
  394. vectoangles(vec, vec2);
  395. vec2[PITCH] = -vec2[PITCH];
  396. cam_viewangles[PITCH] = adjustang(cam_viewangles[PITCH], vec2[PITCH], cl_camera_maxpitch.value);
  397. cam_viewangles[YAW] = adjustang(cam_viewangles[YAW], vec2[YAW], cl_camera_maxyaw.value);
  398. }
  399. VectorCopy(cam_viewangles, cl.viewangles);
  400. VectorCopy(cl.viewangles, cl.simangles);
  401. }
  402. #endif
  403. void Cam_FinishMove(usercmd_t *cmd)
  404. {
  405. int i;
  406. player_info_t *s;
  407. int end;
  408. if (cls.state != ca_active)
  409. return;
  410. if (!cl.spectator) // only in spectator mode
  411. return;
  412. #if 0
  413. if (autocam && locked) {
  414. frame = &cl.frames[cls.netchan.incoming_sequence & UPDATE_MASK];
  415. player = frame->playerstate + spec_track;
  416. self = frame->playerstate + cl.playernum;
  417. VectorSubtract(player->origin, self->origin, vec);
  418. if (cam_forceview) {
  419. cam_forceview = false;
  420. vectoangles(vec, cam_viewangles);
  421. cam_viewangles[0] = -cam_viewangles[0];
  422. } else {
  423. vectoangles(vec, vec2);
  424. vec2[PITCH] = -vec2[PITCH];
  425. cam_viewangles[PITCH] = adjustang(cam_viewangles[PITCH], vec2[PITCH], cl_camera_maxpitch.value);
  426. cam_viewangles[YAW] = adjustang(cam_viewangles[YAW], vec2[YAW], cl_camera_maxyaw.value);
  427. }
  428. VectorCopy(cam_viewangles, cl.viewangles);
  429. }
  430. #endif
  431. if (cmd->buttons & BUTTON_ATTACK) {
  432. if (!(oldbuttons & BUTTON_ATTACK)) {
  433. oldbuttons |= BUTTON_ATTACK;
  434. autocam++;
  435. if (autocam > CAM_TRACK) {
  436. Cam_Unlock();
  437. VectorCopy(cl.viewangles, cmd->angles);
  438. return;
  439. }
  440. } else
  441. return;
  442. } else {
  443. oldbuttons &= ~BUTTON_ATTACK;
  444. if (!autocam)
  445. return;
  446. }
  447. if (autocam && cl_hightrack.value) {
  448. Cam_CheckHighTarget();
  449. return;
  450. }
  451. if (locked) {
  452. if ((cmd->buttons & BUTTON_JUMP) && (oldbuttons & BUTTON_JUMP))
  453. return; // don't pogo stick
  454. if (!(cmd->buttons & BUTTON_JUMP)) {
  455. oldbuttons &= ~BUTTON_JUMP;
  456. return;
  457. }
  458. oldbuttons |= BUTTON_JUMP; // don't jump again until released
  459. }
  460. // Con_Printf("Selecting track target...\n");
  461. if (locked && autocam)
  462. end = (spec_track + 1) % MAX_CLIENTS;
  463. else
  464. end = spec_track;
  465. i = end;
  466. do {
  467. s = &cl.players[i];
  468. if (s->name[0] && !s->spectator) {
  469. Cam_Lock(i);
  470. return;
  471. }
  472. i = (i + 1) % MAX_CLIENTS;
  473. } while (i != end);
  474. // stay on same guy?
  475. i = spec_track;
  476. s = &cl.players[i];
  477. if (s->name[0] && !s->spectator) {
  478. Cam_Lock(i);
  479. return;
  480. }
  481. Con_Printf("No target found ...\n");
  482. autocam = locked = false;
  483. }
  484. void Cam_Reset(void)
  485. {
  486. autocam = CAM_NONE;
  487. spec_track = 0;
  488. }
  489. void CL_InitCam(void)
  490. {
  491. Cvar_RegisterVariable (&cl_hightrack);
  492. Cvar_RegisterVariable (&cl_chasecam);
  493. // Cvar_RegisterVariable (&cl_camera_maxpitch);
  494. // Cvar_RegisterVariable (&cl_camera_maxyaw);
  495. }