pmove.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  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. #include "qcommon.h"
  16. #define STEPSIZE 18
  17. // all of the locals will be zeroed before each
  18. // pmove, just to make damn sure we don't have
  19. // any differences when running on client or server
  20. typedef struct
  21. {
  22. vec3_t origin; // full float precision
  23. vec3_t velocity; // full float precision
  24. vec3_t forward, right, up;
  25. float frametime;
  26. csurface_t *groundsurface;
  27. cplane_t groundplane;
  28. int groundcontents;
  29. vec3_t previous_origin;
  30. qboolean ladder;
  31. } pml_t;
  32. pmove_t *pm;
  33. pml_t pml;
  34. // movement parameters
  35. float pm_stopspeed = 100;
  36. float pm_maxspeed = 300;
  37. float pm_duckspeed = 100;
  38. float pm_accelerate = 10;
  39. float pm_airaccelerate = 0;
  40. float pm_wateraccelerate = 10;
  41. float pm_friction = 6;
  42. float pm_waterfriction = 1;
  43. float pm_waterspeed = 400;
  44. /*
  45. walking up a step should kill some velocity
  46. */
  47. /*
  48. ==================
  49. PM_ClipVelocity
  50. Slide off of the impacting object
  51. returns the blocked flags (1 = floor, 2 = step / wall)
  52. ==================
  53. */
  54. #define STOP_EPSILON 0.1
  55. void PM_ClipVelocity (vec3_t in, vec3_t normal, vec3_t out, float overbounce)
  56. {
  57. float backoff;
  58. float change;
  59. int i;
  60. backoff = DotProduct (in, normal) * overbounce;
  61. for (i=0 ; i<3 ; i++)
  62. {
  63. change = normal[i]*backoff;
  64. out[i] = in[i] - change;
  65. if (out[i] > -STOP_EPSILON && out[i] < STOP_EPSILON)
  66. out[i] = 0;
  67. }
  68. }
  69. /*
  70. ==================
  71. PM_StepSlideMove
  72. Each intersection will try to step over the obstruction instead of
  73. sliding along it.
  74. Returns a new origin, velocity, and contact entity
  75. Does not modify any world state?
  76. ==================
  77. */
  78. #define MIN_STEP_NORMAL 0.7 // can't step up onto very steep slopes
  79. #define MAX_CLIP_PLANES 5
  80. void PM_StepSlideMove_ (void)
  81. {
  82. int bumpcount, numbumps;
  83. vec3_t dir;
  84. float d;
  85. int numplanes;
  86. vec3_t planes[MAX_CLIP_PLANES];
  87. vec3_t primal_velocity;
  88. int i, j;
  89. trace_t trace;
  90. vec3_t end;
  91. float time_left;
  92. numbumps = 4;
  93. VectorCopy (pml.velocity, primal_velocity);
  94. numplanes = 0;
  95. time_left = pml.frametime;
  96. for (bumpcount=0 ; bumpcount<numbumps ; bumpcount++)
  97. {
  98. for (i=0 ; i<3 ; i++)
  99. end[i] = pml.origin[i] + time_left * pml.velocity[i];
  100. trace = pm->trace (pml.origin, pm->mins, pm->maxs, end);
  101. if (trace.allsolid)
  102. { // entity is trapped in another solid
  103. pml.velocity[2] = 0; // don't build up falling damage
  104. return;
  105. }
  106. if (trace.fraction > 0)
  107. { // actually covered some distance
  108. VectorCopy (trace.endpos, pml.origin);
  109. numplanes = 0;
  110. }
  111. if (trace.fraction == 1)
  112. break; // moved the entire distance
  113. // save entity for contact
  114. if (pm->numtouch < MAXTOUCH && trace.ent)
  115. {
  116. pm->touchents[pm->numtouch] = trace.ent;
  117. pm->numtouch++;
  118. }
  119. time_left -= time_left * trace.fraction;
  120. // slide along this plane
  121. if (numplanes >= MAX_CLIP_PLANES)
  122. { // this shouldn't really happen
  123. VectorCopy (vec3_origin, pml.velocity);
  124. break;
  125. }
  126. VectorCopy (trace.plane.normal, planes[numplanes]);
  127. numplanes++;
  128. #if 0
  129. float rub;
  130. //
  131. // modify velocity so it parallels all of the clip planes
  132. //
  133. if (numplanes == 1)
  134. { // go along this plane
  135. VectorCopy (pml.velocity, dir);
  136. VectorNormalize (dir);
  137. rub = 1.0 + 0.5 * DotProduct (dir, planes[0]);
  138. // slide along the plane
  139. PM_ClipVelocity (pml.velocity, planes[0], pml.velocity, 1.01);
  140. // rub some extra speed off on xy axis
  141. // not on Z, or you can scrub down walls
  142. pml.velocity[0] *= rub;
  143. pml.velocity[1] *= rub;
  144. pml.velocity[2] *= rub;
  145. }
  146. else if (numplanes == 2)
  147. { // go along the crease
  148. VectorCopy (pml.velocity, dir);
  149. VectorNormalize (dir);
  150. rub = 1.0 + 0.5 * DotProduct (dir, planes[0]);
  151. // slide along the plane
  152. CrossProduct (planes[0], planes[1], dir);
  153. d = DotProduct (dir, pml.velocity);
  154. VectorScale (dir, d, pml.velocity);
  155. // rub some extra speed off
  156. VectorScale (pml.velocity, rub, pml.velocity);
  157. }
  158. else
  159. {
  160. // Con_Printf ("clip velocity, numplanes == %i\n",numplanes);
  161. VectorCopy (vec3_origin, pml.velocity);
  162. break;
  163. }
  164. #else
  165. //
  166. // modify original_velocity so it parallels all of the clip planes
  167. //
  168. for (i=0 ; i<numplanes ; i++)
  169. {
  170. PM_ClipVelocity (pml.velocity, planes[i], pml.velocity, 1.01);
  171. for (j=0 ; j<numplanes ; j++)
  172. if (j != i)
  173. {
  174. if (DotProduct (pml.velocity, planes[j]) < 0)
  175. break; // not ok
  176. }
  177. if (j == numplanes)
  178. break;
  179. }
  180. if (i != numplanes)
  181. { // go along this plane
  182. }
  183. else
  184. { // go along the crease
  185. if (numplanes != 2)
  186. {
  187. // Con_Printf ("clip velocity, numplanes == %i\n",numplanes);
  188. VectorCopy (vec3_origin, pml.velocity);
  189. break;
  190. }
  191. CrossProduct (planes[0], planes[1], dir);
  192. d = DotProduct (dir, pml.velocity);
  193. VectorScale (dir, d, pml.velocity);
  194. }
  195. #endif
  196. //
  197. // if velocity is against the original velocity, stop dead
  198. // to avoid tiny occilations in sloping corners
  199. //
  200. if (DotProduct (pml.velocity, primal_velocity) <= 0)
  201. {
  202. VectorCopy (vec3_origin, pml.velocity);
  203. break;
  204. }
  205. }
  206. if (pm->s.pm_time)
  207. {
  208. VectorCopy (primal_velocity, pml.velocity);
  209. }
  210. }
  211. /*
  212. ==================
  213. PM_StepSlideMove
  214. ==================
  215. */
  216. void PM_StepSlideMove (void)
  217. {
  218. vec3_t start_o, start_v;
  219. vec3_t down_o, down_v;
  220. trace_t trace;
  221. float down_dist, up_dist;
  222. // vec3_t delta;
  223. vec3_t up, down;
  224. VectorCopy (pml.origin, start_o);
  225. VectorCopy (pml.velocity, start_v);
  226. PM_StepSlideMove_ ();
  227. VectorCopy (pml.origin, down_o);
  228. VectorCopy (pml.velocity, down_v);
  229. VectorCopy (start_o, up);
  230. up[2] += STEPSIZE;
  231. trace = pm->trace (up, pm->mins, pm->maxs, up);
  232. if (trace.allsolid)
  233. return; // can't step up
  234. // try sliding above
  235. VectorCopy (up, pml.origin);
  236. VectorCopy (start_v, pml.velocity);
  237. PM_StepSlideMove_ ();
  238. // push down the final amount
  239. VectorCopy (pml.origin, down);
  240. down[2] -= STEPSIZE;
  241. trace = pm->trace (pml.origin, pm->mins, pm->maxs, down);
  242. if (!trace.allsolid)
  243. {
  244. VectorCopy (trace.endpos, pml.origin);
  245. }
  246. #if 0
  247. VectorSubtract (pml.origin, up, delta);
  248. up_dist = DotProduct (delta, start_v);
  249. VectorSubtract (down_o, start_o, delta);
  250. down_dist = DotProduct (delta, start_v);
  251. #else
  252. VectorCopy(pml.origin, up);
  253. // decide which one went farther
  254. down_dist = (down_o[0] - start_o[0])*(down_o[0] - start_o[0])
  255. + (down_o[1] - start_o[1])*(down_o[1] - start_o[1]);
  256. up_dist = (up[0] - start_o[0])*(up[0] - start_o[0])
  257. + (up[1] - start_o[1])*(up[1] - start_o[1]);
  258. #endif
  259. if (down_dist > up_dist || trace.plane.normal[2] < MIN_STEP_NORMAL)
  260. {
  261. VectorCopy (down_o, pml.origin);
  262. VectorCopy (down_v, pml.velocity);
  263. return;
  264. }
  265. //!! Special case
  266. // if we were walking along a plane, then we need to copy the Z over
  267. pml.velocity[2] = down_v[2];
  268. }
  269. /*
  270. ==================
  271. PM_Friction
  272. Handles both ground friction and water friction
  273. ==================
  274. */
  275. void PM_Friction (void)
  276. {
  277. float *vel;
  278. float speed, newspeed, control;
  279. float friction;
  280. float drop;
  281. vel = pml.velocity;
  282. speed = sqrt(vel[0]*vel[0] +vel[1]*vel[1] + vel[2]*vel[2]);
  283. if (speed < 1)
  284. {
  285. vel[0] = 0;
  286. vel[1] = 0;
  287. return;
  288. }
  289. drop = 0;
  290. // apply ground friction
  291. if ((pm->groundentity && pml.groundsurface && !(pml.groundsurface->flags & SURF_SLICK) ) || (pml.ladder) )
  292. {
  293. friction = pm_friction;
  294. control = speed < pm_stopspeed ? pm_stopspeed : speed;
  295. drop += control*friction*pml.frametime;
  296. }
  297. // apply water friction
  298. if (pm->waterlevel && !pml.ladder)
  299. drop += speed*pm_waterfriction*pm->waterlevel*pml.frametime;
  300. // scale the velocity
  301. newspeed = speed - drop;
  302. if (newspeed < 0)
  303. {
  304. newspeed = 0;
  305. }
  306. newspeed /= speed;
  307. vel[0] = vel[0] * newspeed;
  308. vel[1] = vel[1] * newspeed;
  309. vel[2] = vel[2] * newspeed;
  310. }
  311. /*
  312. ==============
  313. PM_Accelerate
  314. Handles user intended acceleration
  315. ==============
  316. */
  317. void PM_Accelerate (vec3_t wishdir, float wishspeed, float accel)
  318. {
  319. int i;
  320. float addspeed, accelspeed, currentspeed;
  321. currentspeed = DotProduct (pml.velocity, wishdir);
  322. addspeed = wishspeed - currentspeed;
  323. if (addspeed <= 0)
  324. return;
  325. accelspeed = accel*pml.frametime*wishspeed;
  326. if (accelspeed > addspeed)
  327. accelspeed = addspeed;
  328. for (i=0 ; i<3 ; i++)
  329. pml.velocity[i] += accelspeed*wishdir[i];
  330. }
  331. void PM_AirAccelerate (vec3_t wishdir, float wishspeed, float accel)
  332. {
  333. int i;
  334. float addspeed, accelspeed, currentspeed, wishspd = wishspeed;
  335. if (wishspd > 30)
  336. wishspd = 30;
  337. currentspeed = DotProduct (pml.velocity, wishdir);
  338. addspeed = wishspd - currentspeed;
  339. if (addspeed <= 0)
  340. return;
  341. accelspeed = accel * wishspeed * pml.frametime;
  342. if (accelspeed > addspeed)
  343. accelspeed = addspeed;
  344. for (i=0 ; i<3 ; i++)
  345. pml.velocity[i] += accelspeed*wishdir[i];
  346. }
  347. /*
  348. =============
  349. PM_AddCurrents
  350. =============
  351. */
  352. void PM_AddCurrents (vec3_t wishvel)
  353. {
  354. vec3_t v;
  355. float s;
  356. //
  357. // account for ladders
  358. //
  359. if (pml.ladder && fabs(pml.velocity[2]) <= 200)
  360. {
  361. if ((pm->viewangles[PITCH] <= -15) && (pm->cmd.forwardmove > 0))
  362. wishvel[2] = 200;
  363. else if ((pm->viewangles[PITCH] >= 15) && (pm->cmd.forwardmove > 0))
  364. wishvel[2] = -200;
  365. else if (pm->cmd.upmove > 0)
  366. wishvel[2] = 200;
  367. else if (pm->cmd.upmove < 0)
  368. wishvel[2] = -200;
  369. else
  370. wishvel[2] = 0;
  371. // limit horizontal speed when on a ladder
  372. if (wishvel[0] < -25)
  373. wishvel[0] = -25;
  374. else if (wishvel[0] > 25)
  375. wishvel[0] = 25;
  376. if (wishvel[1] < -25)
  377. wishvel[1] = -25;
  378. else if (wishvel[1] > 25)
  379. wishvel[1] = 25;
  380. }
  381. //
  382. // add water currents
  383. //
  384. if (pm->watertype & MASK_CURRENT)
  385. {
  386. VectorClear (v);
  387. if (pm->watertype & CONTENTS_CURRENT_0)
  388. v[0] += 1;
  389. if (pm->watertype & CONTENTS_CURRENT_90)
  390. v[1] += 1;
  391. if (pm->watertype & CONTENTS_CURRENT_180)
  392. v[0] -= 1;
  393. if (pm->watertype & CONTENTS_CURRENT_270)
  394. v[1] -= 1;
  395. if (pm->watertype & CONTENTS_CURRENT_UP)
  396. v[2] += 1;
  397. if (pm->watertype & CONTENTS_CURRENT_DOWN)
  398. v[2] -= 1;
  399. s = pm_waterspeed;
  400. if ((pm->waterlevel == 1) && (pm->groundentity))
  401. s /= 2;
  402. VectorMA (wishvel, s, v, wishvel);
  403. }
  404. //
  405. // add conveyor belt velocities
  406. //
  407. if (pm->groundentity)
  408. {
  409. VectorClear (v);
  410. if (pml.groundcontents & CONTENTS_CURRENT_0)
  411. v[0] += 1;
  412. if (pml.groundcontents & CONTENTS_CURRENT_90)
  413. v[1] += 1;
  414. if (pml.groundcontents & CONTENTS_CURRENT_180)
  415. v[0] -= 1;
  416. if (pml.groundcontents & CONTENTS_CURRENT_270)
  417. v[1] -= 1;
  418. if (pml.groundcontents & CONTENTS_CURRENT_UP)
  419. v[2] += 1;
  420. if (pml.groundcontents & CONTENTS_CURRENT_DOWN)
  421. v[2] -= 1;
  422. VectorMA (wishvel, 100 /* pm->groundentity->speed */, v, wishvel);
  423. }
  424. }
  425. /*
  426. ===================
  427. PM_WaterMove
  428. ===================
  429. */
  430. void PM_WaterMove (void)
  431. {
  432. int i;
  433. vec3_t wishvel;
  434. float wishspeed;
  435. vec3_t wishdir;
  436. //
  437. // user intentions
  438. //
  439. for (i=0 ; i<3 ; i++)
  440. wishvel[i] = pml.forward[i]*pm->cmd.forwardmove + pml.right[i]*pm->cmd.sidemove;
  441. if (!pm->cmd.forwardmove && !pm->cmd.sidemove && !pm->cmd.upmove)
  442. wishvel[2] -= 60; // drift towards bottom
  443. else
  444. wishvel[2] += pm->cmd.upmove;
  445. PM_AddCurrents (wishvel);
  446. VectorCopy (wishvel, wishdir);
  447. wishspeed = VectorNormalize(wishdir);
  448. if (wishspeed > pm_maxspeed)
  449. {
  450. VectorScale (wishvel, pm_maxspeed/wishspeed, wishvel);
  451. wishspeed = pm_maxspeed;
  452. }
  453. wishspeed *= 0.5;
  454. PM_Accelerate (wishdir, wishspeed, pm_wateraccelerate);
  455. PM_StepSlideMove ();
  456. }
  457. /*
  458. ===================
  459. PM_AirMove
  460. ===================
  461. */
  462. void PM_AirMove (void)
  463. {
  464. int i;
  465. vec3_t wishvel;
  466. float fmove, smove;
  467. vec3_t wishdir;
  468. float wishspeed;
  469. float maxspeed;
  470. fmove = pm->cmd.forwardmove;
  471. smove = pm->cmd.sidemove;
  472. //!!!!! pitch should be 1/3 so this isn't needed??!
  473. #if 0
  474. pml.forward[2] = 0;
  475. pml.right[2] = 0;
  476. VectorNormalize (pml.forward);
  477. VectorNormalize (pml.right);
  478. #endif
  479. for (i=0 ; i<2 ; i++)
  480. wishvel[i] = pml.forward[i]*fmove + pml.right[i]*smove;
  481. wishvel[2] = 0;
  482. PM_AddCurrents (wishvel);
  483. VectorCopy (wishvel, wishdir);
  484. wishspeed = VectorNormalize(wishdir);
  485. //
  486. // clamp to server defined max speed
  487. //
  488. maxspeed = (pm->s.pm_flags & PMF_DUCKED) ? pm_duckspeed : pm_maxspeed;
  489. if (wishspeed > maxspeed)
  490. {
  491. VectorScale (wishvel, maxspeed/wishspeed, wishvel);
  492. wishspeed = maxspeed;
  493. }
  494. if ( pml.ladder )
  495. {
  496. PM_Accelerate (wishdir, wishspeed, pm_accelerate);
  497. if (!wishvel[2])
  498. {
  499. if (pml.velocity[2] > 0)
  500. {
  501. pml.velocity[2] -= pm->s.gravity * pml.frametime;
  502. if (pml.velocity[2] < 0)
  503. pml.velocity[2] = 0;
  504. }
  505. else
  506. {
  507. pml.velocity[2] += pm->s.gravity * pml.frametime;
  508. if (pml.velocity[2] > 0)
  509. pml.velocity[2] = 0;
  510. }
  511. }
  512. PM_StepSlideMove ();
  513. }
  514. else if ( pm->groundentity )
  515. { // walking on ground
  516. pml.velocity[2] = 0; //!!! this is before the accel
  517. PM_Accelerate (wishdir, wishspeed, pm_accelerate);
  518. // PGM -- fix for negative trigger_gravity fields
  519. // pml.velocity[2] = 0;
  520. if(pm->s.gravity > 0)
  521. pml.velocity[2] = 0;
  522. else
  523. pml.velocity[2] -= pm->s.gravity * pml.frametime;
  524. // PGM
  525. if (!pml.velocity[0] && !pml.velocity[1])
  526. return;
  527. PM_StepSlideMove ();
  528. }
  529. else
  530. { // not on ground, so little effect on velocity
  531. if (pm_airaccelerate)
  532. PM_AirAccelerate (wishdir, wishspeed, pm_accelerate);
  533. else
  534. PM_Accelerate (wishdir, wishspeed, 1);
  535. // add gravity
  536. pml.velocity[2] -= pm->s.gravity * pml.frametime;
  537. PM_StepSlideMove ();
  538. }
  539. }
  540. /*
  541. =============
  542. PM_CatagorizePosition
  543. =============
  544. */
  545. void PM_CatagorizePosition (void)
  546. {
  547. vec3_t point;
  548. int cont;
  549. trace_t trace;
  550. int sample1;
  551. int sample2;
  552. // if the player hull point one unit down is solid, the player
  553. // is on ground
  554. // see if standing on something solid
  555. point[0] = pml.origin[0];
  556. point[1] = pml.origin[1];
  557. point[2] = pml.origin[2] - 0.25;
  558. if (pml.velocity[2] > 180) //!!ZOID changed from 100 to 180 (ramp accel)
  559. {
  560. pm->s.pm_flags &= ~PMF_ON_GROUND;
  561. pm->groundentity = NULL;
  562. }
  563. else
  564. {
  565. trace = pm->trace (pml.origin, pm->mins, pm->maxs, point);
  566. pml.groundplane = trace.plane;
  567. pml.groundsurface = trace.surface;
  568. pml.groundcontents = trace.contents;
  569. if (!trace.ent || (trace.plane.normal[2] < 0.7 && !trace.startsolid) )
  570. {
  571. pm->groundentity = NULL;
  572. pm->s.pm_flags &= ~PMF_ON_GROUND;
  573. }
  574. else
  575. {
  576. pm->groundentity = trace.ent;
  577. // hitting solid ground will end a waterjump
  578. if (pm->s.pm_flags & PMF_TIME_WATERJUMP)
  579. {
  580. pm->s.pm_flags &= ~(PMF_TIME_WATERJUMP | PMF_TIME_LAND | PMF_TIME_TELEPORT);
  581. pm->s.pm_time = 0;
  582. }
  583. if (! (pm->s.pm_flags & PMF_ON_GROUND) )
  584. { // just hit the ground
  585. pm->s.pm_flags |= PMF_ON_GROUND;
  586. // don't do landing time if we were just going down a slope
  587. if (pml.velocity[2] < -200)
  588. {
  589. pm->s.pm_flags |= PMF_TIME_LAND;
  590. // don't allow another jump for a little while
  591. if (pml.velocity[2] < -400)
  592. pm->s.pm_time = 25;
  593. else
  594. pm->s.pm_time = 18;
  595. }
  596. }
  597. }
  598. #if 0
  599. if (trace.fraction < 1.0 && trace.ent && pml.velocity[2] < 0)
  600. pml.velocity[2] = 0;
  601. #endif
  602. if (pm->numtouch < MAXTOUCH && trace.ent)
  603. {
  604. pm->touchents[pm->numtouch] = trace.ent;
  605. pm->numtouch++;
  606. }
  607. }
  608. //
  609. // get waterlevel, accounting for ducking
  610. //
  611. pm->waterlevel = 0;
  612. pm->watertype = 0;
  613. sample2 = pm->viewheight - pm->mins[2];
  614. sample1 = sample2 / 2;
  615. point[2] = pml.origin[2] + pm->mins[2] + 1;
  616. cont = pm->pointcontents (point);
  617. if (cont & MASK_WATER)
  618. {
  619. pm->watertype = cont;
  620. pm->waterlevel = 1;
  621. point[2] = pml.origin[2] + pm->mins[2] + sample1;
  622. cont = pm->pointcontents (point);
  623. if (cont & MASK_WATER)
  624. {
  625. pm->waterlevel = 2;
  626. point[2] = pml.origin[2] + pm->mins[2] + sample2;
  627. cont = pm->pointcontents (point);
  628. if (cont & MASK_WATER)
  629. pm->waterlevel = 3;
  630. }
  631. }
  632. }
  633. /*
  634. =============
  635. PM_CheckJump
  636. =============
  637. */
  638. void PM_CheckJump (void)
  639. {
  640. if (pm->s.pm_flags & PMF_TIME_LAND)
  641. { // hasn't been long enough since landing to jump again
  642. return;
  643. }
  644. if (pm->cmd.upmove < 10)
  645. { // not holding jump
  646. pm->s.pm_flags &= ~PMF_JUMP_HELD;
  647. return;
  648. }
  649. // must wait for jump to be released
  650. if (pm->s.pm_flags & PMF_JUMP_HELD)
  651. return;
  652. if (pm->s.pm_type == PM_DEAD)
  653. return;
  654. if (pm->waterlevel >= 2)
  655. { // swimming, not jumping
  656. pm->groundentity = NULL;
  657. if (pml.velocity[2] <= -300)
  658. return;
  659. if (pm->watertype == CONTENTS_WATER)
  660. pml.velocity[2] = 100;
  661. else if (pm->watertype == CONTENTS_SLIME)
  662. pml.velocity[2] = 80;
  663. else
  664. pml.velocity[2] = 50;
  665. return;
  666. }
  667. if (pm->groundentity == NULL)
  668. return; // in air, so no effect
  669. pm->s.pm_flags |= PMF_JUMP_HELD;
  670. pm->groundentity = NULL;
  671. pml.velocity[2] += 270;
  672. if (pml.velocity[2] < 270)
  673. pml.velocity[2] = 270;
  674. }
  675. /*
  676. =============
  677. PM_CheckSpecialMovement
  678. =============
  679. */
  680. void PM_CheckSpecialMovement (void)
  681. {
  682. vec3_t spot;
  683. int cont;
  684. vec3_t flatforward;
  685. trace_t trace;
  686. if (pm->s.pm_time)
  687. return;
  688. pml.ladder = false;
  689. // check for ladder
  690. flatforward[0] = pml.forward[0];
  691. flatforward[1] = pml.forward[1];
  692. flatforward[2] = 0;
  693. VectorNormalize (flatforward);
  694. VectorMA (pml.origin, 1, flatforward, spot);
  695. trace = pm->trace (pml.origin, pm->mins, pm->maxs, spot);
  696. if ((trace.fraction < 1) && (trace.contents & CONTENTS_LADDER))
  697. pml.ladder = true;
  698. // check for water jump
  699. if (pm->waterlevel != 2)
  700. return;
  701. VectorMA (pml.origin, 30, flatforward, spot);
  702. spot[2] += 4;
  703. cont = pm->pointcontents (spot);
  704. if (!(cont & CONTENTS_SOLID))
  705. return;
  706. spot[2] += 16;
  707. cont = pm->pointcontents (spot);
  708. if (cont)
  709. return;
  710. // jump out of water
  711. VectorScale (flatforward, 50, pml.velocity);
  712. pml.velocity[2] = 350;
  713. pm->s.pm_flags |= PMF_TIME_WATERJUMP;
  714. pm->s.pm_time = 255;
  715. }
  716. /*
  717. ===============
  718. PM_FlyMove
  719. ===============
  720. */
  721. void PM_FlyMove (qboolean doclip)
  722. {
  723. float speed, drop, friction, control, newspeed;
  724. float currentspeed, addspeed, accelspeed;
  725. int i;
  726. vec3_t wishvel;
  727. float fmove, smove;
  728. vec3_t wishdir;
  729. float wishspeed;
  730. vec3_t end;
  731. trace_t trace;
  732. pm->viewheight = 22;
  733. // friction
  734. speed = VectorLength (pml.velocity);
  735. if (speed < 1)
  736. {
  737. VectorCopy (vec3_origin, pml.velocity);
  738. }
  739. else
  740. {
  741. drop = 0;
  742. friction = pm_friction*1.5; // extra friction
  743. control = speed < pm_stopspeed ? pm_stopspeed : speed;
  744. drop += control*friction*pml.frametime;
  745. // scale the velocity
  746. newspeed = speed - drop;
  747. if (newspeed < 0)
  748. newspeed = 0;
  749. newspeed /= speed;
  750. VectorScale (pml.velocity, newspeed, pml.velocity);
  751. }
  752. // accelerate
  753. fmove = pm->cmd.forwardmove;
  754. smove = pm->cmd.sidemove;
  755. VectorNormalize (pml.forward);
  756. VectorNormalize (pml.right);
  757. for (i=0 ; i<3 ; i++)
  758. wishvel[i] = pml.forward[i]*fmove + pml.right[i]*smove;
  759. wishvel[2] += pm->cmd.upmove;
  760. VectorCopy (wishvel, wishdir);
  761. wishspeed = VectorNormalize(wishdir);
  762. //
  763. // clamp to server defined max speed
  764. //
  765. if (wishspeed > pm_maxspeed)
  766. {
  767. VectorScale (wishvel, pm_maxspeed/wishspeed, wishvel);
  768. wishspeed = pm_maxspeed;
  769. }
  770. currentspeed = DotProduct(pml.velocity, wishdir);
  771. addspeed = wishspeed - currentspeed;
  772. if (addspeed <= 0)
  773. return;
  774. accelspeed = pm_accelerate*pml.frametime*wishspeed;
  775. if (accelspeed > addspeed)
  776. accelspeed = addspeed;
  777. for (i=0 ; i<3 ; i++)
  778. pml.velocity[i] += accelspeed*wishdir[i];
  779. if (doclip) {
  780. for (i=0 ; i<3 ; i++)
  781. end[i] = pml.origin[i] + pml.frametime * pml.velocity[i];
  782. trace = pm->trace (pml.origin, pm->mins, pm->maxs, end);
  783. VectorCopy (trace.endpos, pml.origin);
  784. } else {
  785. // move
  786. VectorMA (pml.origin, pml.frametime, pml.velocity, pml.origin);
  787. }
  788. }
  789. /*
  790. ==============
  791. PM_CheckDuck
  792. Sets mins, maxs, and pm->viewheight
  793. ==============
  794. */
  795. void PM_CheckDuck (void)
  796. {
  797. trace_t trace;
  798. pm->mins[0] = -16;
  799. pm->mins[1] = -16;
  800. pm->maxs[0] = 16;
  801. pm->maxs[1] = 16;
  802. if (pm->s.pm_type == PM_GIB)
  803. {
  804. pm->mins[2] = 0;
  805. pm->maxs[2] = 16;
  806. pm->viewheight = 8;
  807. return;
  808. }
  809. pm->mins[2] = -24;
  810. if (pm->s.pm_type == PM_DEAD)
  811. {
  812. pm->s.pm_flags |= PMF_DUCKED;
  813. }
  814. else if (pm->cmd.upmove < 0 && (pm->s.pm_flags & PMF_ON_GROUND) )
  815. { // duck
  816. pm->s.pm_flags |= PMF_DUCKED;
  817. }
  818. else
  819. { // stand up if possible
  820. if (pm->s.pm_flags & PMF_DUCKED)
  821. {
  822. // try to stand up
  823. pm->maxs[2] = 32;
  824. trace = pm->trace (pml.origin, pm->mins, pm->maxs, pml.origin);
  825. if (!trace.allsolid)
  826. pm->s.pm_flags &= ~PMF_DUCKED;
  827. }
  828. }
  829. if (pm->s.pm_flags & PMF_DUCKED)
  830. {
  831. pm->maxs[2] = 4;
  832. pm->viewheight = -2;
  833. }
  834. else
  835. {
  836. pm->maxs[2] = 32;
  837. pm->viewheight = 22;
  838. }
  839. }
  840. /*
  841. ==============
  842. PM_DeadMove
  843. ==============
  844. */
  845. void PM_DeadMove (void)
  846. {
  847. float forward;
  848. if (!pm->groundentity)
  849. return;
  850. // extra friction
  851. forward = VectorLength (pml.velocity);
  852. forward -= 20;
  853. if (forward <= 0)
  854. {
  855. VectorClear (pml.velocity);
  856. }
  857. else
  858. {
  859. VectorNormalize (pml.velocity);
  860. VectorScale (pml.velocity, forward, pml.velocity);
  861. }
  862. }
  863. qboolean PM_GoodPosition (void)
  864. {
  865. trace_t trace;
  866. vec3_t origin, end;
  867. int i;
  868. if (pm->s.pm_type == PM_SPECTATOR)
  869. return true;
  870. for (i=0 ; i<3 ; i++)
  871. origin[i] = end[i] = pm->s.origin[i]*0.125;
  872. trace = pm->trace (origin, pm->mins, pm->maxs, end);
  873. return !trace.allsolid;
  874. }
  875. /*
  876. ================
  877. PM_SnapPosition
  878. On exit, the origin will have a value that is pre-quantized to the 0.125
  879. precision of the network channel and in a valid position.
  880. ================
  881. */
  882. void PM_SnapPosition (void)
  883. {
  884. int sign[3];
  885. int i, j, bits;
  886. short base[3];
  887. // try all single bits first
  888. static int jitterbits[8] = {0,4,1,2,3,5,6,7};
  889. // snap velocity to eigths
  890. for (i=0 ; i<3 ; i++)
  891. pm->s.velocity[i] = (int)(pml.velocity[i]*8);
  892. for (i=0 ; i<3 ; i++)
  893. {
  894. if (pml.origin[i] >= 0)
  895. sign[i] = 1;
  896. else
  897. sign[i] = -1;
  898. pm->s.origin[i] = (int)(pml.origin[i]*8);
  899. if (pm->s.origin[i]*0.125 == pml.origin[i])
  900. sign[i] = 0;
  901. }
  902. VectorCopy (pm->s.origin, base);
  903. // try all combinations
  904. for (j=0 ; j<8 ; j++)
  905. {
  906. bits = jitterbits[j];
  907. VectorCopy (base, pm->s.origin);
  908. for (i=0 ; i<3 ; i++)
  909. if (bits & (1<<i) )
  910. pm->s.origin[i] += sign[i];
  911. if (PM_GoodPosition ())
  912. return;
  913. }
  914. // go back to the last position
  915. VectorCopy (pml.previous_origin, pm->s.origin);
  916. // Com_DPrintf ("using previous_origin\n");
  917. }
  918. #if 0
  919. //NO LONGER USED
  920. /*
  921. ================
  922. PM_InitialSnapPosition
  923. ================
  924. */
  925. void PM_InitialSnapPosition (void)
  926. {
  927. int x, y, z;
  928. short base[3];
  929. VectorCopy (pm->s.origin, base);
  930. for (z=1 ; z>=-1 ; z--)
  931. {
  932. pm->s.origin[2] = base[2] + z;
  933. for (y=1 ; y>=-1 ; y--)
  934. {
  935. pm->s.origin[1] = base[1] + y;
  936. for (x=1 ; x>=-1 ; x--)
  937. {
  938. pm->s.origin[0] = base[0] + x;
  939. if (PM_GoodPosition ())
  940. {
  941. pml.origin[0] = pm->s.origin[0]*0.125;
  942. pml.origin[1] = pm->s.origin[1]*0.125;
  943. pml.origin[2] = pm->s.origin[2]*0.125;
  944. VectorCopy (pm->s.origin, pml.previous_origin);
  945. return;
  946. }
  947. }
  948. }
  949. }
  950. Com_DPrintf ("Bad InitialSnapPosition\n");
  951. }
  952. #else
  953. /*
  954. ================
  955. PM_InitialSnapPosition
  956. ================
  957. */
  958. void PM_InitialSnapPosition(void)
  959. {
  960. int x, y, z;
  961. short base[3];
  962. static int offset[3] = { 0, -1, 1 };
  963. VectorCopy (pm->s.origin, base);
  964. for ( z = 0; z < 3; z++ ) {
  965. pm->s.origin[2] = base[2] + offset[ z ];
  966. for ( y = 0; y < 3; y++ ) {
  967. pm->s.origin[1] = base[1] + offset[ y ];
  968. for ( x = 0; x < 3; x++ ) {
  969. pm->s.origin[0] = base[0] + offset[ x ];
  970. if (PM_GoodPosition ()) {
  971. pml.origin[0] = pm->s.origin[0]*0.125;
  972. pml.origin[1] = pm->s.origin[1]*0.125;
  973. pml.origin[2] = pm->s.origin[2]*0.125;
  974. VectorCopy (pm->s.origin, pml.previous_origin);
  975. return;
  976. }
  977. }
  978. }
  979. }
  980. Com_DPrintf ("Bad InitialSnapPosition\n");
  981. }
  982. #endif
  983. /*
  984. ================
  985. PM_ClampAngles
  986. ================
  987. */
  988. void PM_ClampAngles (void)
  989. {
  990. short temp;
  991. int i;
  992. if (pm->s.pm_flags & PMF_TIME_TELEPORT)
  993. {
  994. pm->viewangles[YAW] = SHORT2ANGLE(pm->cmd.angles[YAW] + pm->s.delta_angles[YAW]);
  995. pm->viewangles[PITCH] = 0;
  996. pm->viewangles[ROLL] = 0;
  997. }
  998. else
  999. {
  1000. // circularly clamp the angles with deltas
  1001. for (i=0 ; i<3 ; i++)
  1002. {
  1003. temp = pm->cmd.angles[i] + pm->s.delta_angles[i];
  1004. pm->viewangles[i] = SHORT2ANGLE(temp);
  1005. }
  1006. // don't let the player look up or down more than 90 degrees
  1007. if (pm->viewangles[PITCH] > 89 && pm->viewangles[PITCH] < 180)
  1008. pm->viewangles[PITCH] = 89;
  1009. else if (pm->viewangles[PITCH] < 271 && pm->viewangles[PITCH] >= 180)
  1010. pm->viewangles[PITCH] = 271;
  1011. }
  1012. AngleVectors (pm->viewangles, pml.forward, pml.right, pml.up);
  1013. }
  1014. /*
  1015. ================
  1016. Pmove
  1017. Can be called by either the server or the client
  1018. ================
  1019. */
  1020. void Pmove (pmove_t *pmove)
  1021. {
  1022. pm = pmove;
  1023. // clear results
  1024. pm->numtouch = 0;
  1025. VectorClear (pm->viewangles);
  1026. pm->viewheight = 0;
  1027. pm->groundentity = 0;
  1028. pm->watertype = 0;
  1029. pm->waterlevel = 0;
  1030. // clear all pmove local vars
  1031. memset (&pml, 0, sizeof(pml));
  1032. // convert origin and velocity to float values
  1033. pml.origin[0] = pm->s.origin[0]*0.125;
  1034. pml.origin[1] = pm->s.origin[1]*0.125;
  1035. pml.origin[2] = pm->s.origin[2]*0.125;
  1036. pml.velocity[0] = pm->s.velocity[0]*0.125;
  1037. pml.velocity[1] = pm->s.velocity[1]*0.125;
  1038. pml.velocity[2] = pm->s.velocity[2]*0.125;
  1039. // save old org in case we get stuck
  1040. VectorCopy (pm->s.origin, pml.previous_origin);
  1041. pml.frametime = pm->cmd.msec * 0.001;
  1042. PM_ClampAngles ();
  1043. if (pm->s.pm_type == PM_SPECTATOR)
  1044. {
  1045. PM_FlyMove (false);
  1046. PM_SnapPosition ();
  1047. return;
  1048. }
  1049. if (pm->s.pm_type >= PM_DEAD)
  1050. {
  1051. pm->cmd.forwardmove = 0;
  1052. pm->cmd.sidemove = 0;
  1053. pm->cmd.upmove = 0;
  1054. }
  1055. if (pm->s.pm_type == PM_FREEZE)
  1056. return; // no movement at all
  1057. // set mins, maxs, and viewheight
  1058. PM_CheckDuck ();
  1059. if (pm->snapinitial)
  1060. PM_InitialSnapPosition ();
  1061. // set groundentity, watertype, and waterlevel
  1062. PM_CatagorizePosition ();
  1063. if (pm->s.pm_type == PM_DEAD)
  1064. PM_DeadMove ();
  1065. PM_CheckSpecialMovement ();
  1066. // drop timing counter
  1067. if (pm->s.pm_time)
  1068. {
  1069. int msec;
  1070. msec = pm->cmd.msec >> 3;
  1071. if (!msec)
  1072. msec = 1;
  1073. if ( msec >= pm->s.pm_time)
  1074. {
  1075. pm->s.pm_flags &= ~(PMF_TIME_WATERJUMP | PMF_TIME_LAND | PMF_TIME_TELEPORT);
  1076. pm->s.pm_time = 0;
  1077. }
  1078. else
  1079. pm->s.pm_time -= msec;
  1080. }
  1081. if (pm->s.pm_flags & PMF_TIME_TELEPORT)
  1082. { // teleport pause stays exactly in place
  1083. }
  1084. else if (pm->s.pm_flags & PMF_TIME_WATERJUMP)
  1085. { // waterjump has no control, but falls
  1086. pml.velocity[2] -= pm->s.gravity * pml.frametime;
  1087. if (pml.velocity[2] < 0)
  1088. { // cancel as soon as we are falling down again
  1089. pm->s.pm_flags &= ~(PMF_TIME_WATERJUMP | PMF_TIME_LAND | PMF_TIME_TELEPORT);
  1090. pm->s.pm_time = 0;
  1091. }
  1092. PM_StepSlideMove ();
  1093. }
  1094. else
  1095. {
  1096. PM_CheckJump ();
  1097. PM_Friction ();
  1098. if (pm->waterlevel >= 2)
  1099. PM_WaterMove ();
  1100. else {
  1101. vec3_t angles;
  1102. VectorCopy(pm->viewangles, angles);
  1103. if (angles[PITCH] > 180)
  1104. angles[PITCH] = angles[PITCH] - 360;
  1105. angles[PITCH] /= 3;
  1106. AngleVectors (angles, pml.forward, pml.right, pml.up);
  1107. PM_AirMove ();
  1108. }
  1109. }
  1110. // set groundentity, watertype, and waterlevel for final spot
  1111. PM_CatagorizePosition ();
  1112. PM_SnapPosition ();
  1113. }