sv_phys.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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. // sv_phys.c
  16. #include "qwsvdef.h"
  17. /*
  18. pushmove objects do not obey gravity, and do not interact with each other or trigger fields, but block normal movement and push normal objects when they move.
  19. onground is set for toss objects when they come to a complete rest. it is set for steping or walking objects
  20. doors, plats, etc are SOLID_BSP, and MOVETYPE_PUSH
  21. bonus items are SOLID_TRIGGER touch, and MOVETYPE_TOSS
  22. corpses are SOLID_NOT and MOVETYPE_TOSS
  23. crates are SOLID_BBOX and MOVETYPE_TOSS
  24. walking monsters are SOLID_SLIDEBOX and MOVETYPE_STEP
  25. flying/floating monsters are SOLID_SLIDEBOX and MOVETYPE_FLY
  26. solid_edge items only clip against bsp models.
  27. */
  28. cvar_t sv_maxvelocity = {"sv_maxvelocity","2000"};
  29. cvar_t sv_gravity = { "sv_gravity", "800"};
  30. cvar_t sv_stopspeed = { "sv_stopspeed", "100"};
  31. cvar_t sv_maxspeed = { "sv_maxspeed", "320"};
  32. cvar_t sv_spectatormaxspeed = { "sv_spectatormaxspeed", "500"};
  33. cvar_t sv_accelerate = { "sv_accelerate", "10"};
  34. cvar_t sv_airaccelerate = { "sv_airaccelerate", "0.7"};
  35. cvar_t sv_wateraccelerate = { "sv_wateraccelerate", "10"};
  36. cvar_t sv_friction = { "sv_friction", "4"};
  37. cvar_t sv_waterfriction = { "sv_waterfriction", "4"};
  38. #define MOVE_EPSILON 0.01
  39. void SV_Physics_Toss (edict_t *ent);
  40. /*
  41. ================
  42. SV_CheckAllEnts
  43. ================
  44. */
  45. void SV_CheckAllEnts (void)
  46. {
  47. int e;
  48. edict_t *check;
  49. // see if any solid entities are inside the final position
  50. check = NEXT_EDICT(sv.edicts);
  51. for (e=1 ; e<sv.num_edicts ; e++, check = NEXT_EDICT(check))
  52. {
  53. if (check->free)
  54. continue;
  55. if (check->v.movetype == MOVETYPE_PUSH
  56. || check->v.movetype == MOVETYPE_NONE
  57. || check->v.movetype == MOVETYPE_NOCLIP)
  58. continue;
  59. if (SV_TestEntityPosition (check))
  60. Con_Printf ("entity in invalid position\n");
  61. }
  62. }
  63. /*
  64. ================
  65. SV_CheckVelocity
  66. ================
  67. */
  68. void SV_CheckVelocity (edict_t *ent)
  69. {
  70. int i;
  71. //
  72. // bound velocity
  73. //
  74. for (i=0 ; i<3 ; i++)
  75. {
  76. if (IS_NAN(ent->v.velocity[i]))
  77. {
  78. Con_Printf ("Got a NaN velocity on %s\n", PR_GetString(ent->v.classname));
  79. ent->v.velocity[i] = 0;
  80. }
  81. if (IS_NAN(ent->v.origin[i]))
  82. {
  83. Con_Printf ("Got a NaN origin on %s\n", PR_GetString(ent->v.classname));
  84. ent->v.origin[i] = 0;
  85. }
  86. if (ent->v.velocity[i] > sv_maxvelocity.value)
  87. ent->v.velocity[i] = sv_maxvelocity.value;
  88. else if (ent->v.velocity[i] < -sv_maxvelocity.value)
  89. ent->v.velocity[i] = -sv_maxvelocity.value;
  90. }
  91. }
  92. /*
  93. =============
  94. SV_RunThink
  95. Runs thinking code if time. There is some play in the exact time the think
  96. function will be called, because it is called before any movement is done
  97. in a frame. Not used for pushmove objects, because they must be exact.
  98. Returns false if the entity removed itself.
  99. =============
  100. */
  101. qboolean SV_RunThink (edict_t *ent)
  102. {
  103. float thinktime;
  104. do
  105. {
  106. thinktime = ent->v.nextthink;
  107. if (thinktime <= 0)
  108. return true;
  109. if (thinktime > sv.time + host_frametime)
  110. return true;
  111. if (thinktime < sv.time)
  112. thinktime = sv.time; // don't let things stay in the past.
  113. // it is possible to start that way
  114. // by a trigger with a local time.
  115. ent->v.nextthink = 0;
  116. pr_global_struct->time = thinktime;
  117. pr_global_struct->self = EDICT_TO_PROG(ent);
  118. pr_global_struct->other = EDICT_TO_PROG(sv.edicts);
  119. PR_ExecuteProgram (ent->v.think);
  120. if (ent->free)
  121. return false;
  122. } while (1);
  123. return true;
  124. }
  125. /*
  126. ==================
  127. SV_Impact
  128. Two entities have touched, so run their touch functions
  129. ==================
  130. */
  131. void SV_Impact (edict_t *e1, edict_t *e2)
  132. {
  133. int old_self, old_other;
  134. old_self = pr_global_struct->self;
  135. old_other = pr_global_struct->other;
  136. pr_global_struct->time = sv.time;
  137. if (e1->v.touch && e1->v.solid != SOLID_NOT)
  138. {
  139. pr_global_struct->self = EDICT_TO_PROG(e1);
  140. pr_global_struct->other = EDICT_TO_PROG(e2);
  141. PR_ExecuteProgram (e1->v.touch);
  142. }
  143. if (e2->v.touch && e2->v.solid != SOLID_NOT)
  144. {
  145. pr_global_struct->self = EDICT_TO_PROG(e2);
  146. pr_global_struct->other = EDICT_TO_PROG(e1);
  147. PR_ExecuteProgram (e2->v.touch);
  148. }
  149. pr_global_struct->self = old_self;
  150. pr_global_struct->other = old_other;
  151. }
  152. /*
  153. ==================
  154. ClipVelocity
  155. Slide off of the impacting object
  156. returns the blocked flags (1 = floor, 2 = step / wall)
  157. ==================
  158. */
  159. #define STOP_EPSILON 0.1
  160. int ClipVelocity (vec3_t in, vec3_t normal, vec3_t out, float overbounce)
  161. {
  162. float backoff;
  163. float change;
  164. int i, blocked;
  165. blocked = 0;
  166. if (normal[2] > 0)
  167. blocked |= 1; // floor
  168. if (!normal[2])
  169. blocked |= 2; // step
  170. backoff = DotProduct (in, normal) * overbounce;
  171. for (i=0 ; i<3 ; i++)
  172. {
  173. change = normal[i]*backoff;
  174. out[i] = in[i] - change;
  175. if (out[i] > -STOP_EPSILON && out[i] < STOP_EPSILON)
  176. out[i] = 0;
  177. }
  178. return blocked;
  179. }
  180. /*
  181. ============
  182. SV_FlyMove
  183. The basic solid body movement clip that slides along multiple planes
  184. Returns the clipflags if the velocity was modified (hit something solid)
  185. 1 = floor
  186. 2 = wall / step
  187. 4 = dead stop
  188. If steptrace is not NULL, the trace of any vertical wall hit will be stored
  189. ============
  190. */
  191. #define MAX_CLIP_PLANES 5
  192. int SV_FlyMove (edict_t *ent, float time, trace_t *steptrace)
  193. {
  194. int bumpcount, numbumps;
  195. vec3_t dir;
  196. float d;
  197. int numplanes;
  198. vec3_t planes[MAX_CLIP_PLANES];
  199. vec3_t primal_velocity, original_velocity, new_velocity;
  200. int i, j;
  201. trace_t trace;
  202. vec3_t end;
  203. float time_left;
  204. int blocked;
  205. numbumps = 4;
  206. blocked = 0;
  207. VectorCopy (ent->v.velocity, original_velocity);
  208. VectorCopy (ent->v.velocity, primal_velocity);
  209. numplanes = 0;
  210. time_left = time;
  211. for (bumpcount=0 ; bumpcount<numbumps ; bumpcount++)
  212. {
  213. for (i=0 ; i<3 ; i++)
  214. end[i] = ent->v.origin[i] + time_left * ent->v.velocity[i];
  215. trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, end, false, ent);
  216. if (trace.allsolid)
  217. { // entity is trapped in another solid
  218. VectorCopy (vec3_origin, ent->v.velocity);
  219. return 3;
  220. }
  221. if (trace.fraction > 0)
  222. { // actually covered some distance
  223. VectorCopy (trace.endpos, ent->v.origin);
  224. VectorCopy (ent->v.velocity, original_velocity);
  225. numplanes = 0;
  226. }
  227. if (trace.fraction == 1)
  228. break; // moved the entire distance
  229. if (!trace.ent)
  230. SV_Error ("SV_FlyMove: !trace.ent");
  231. if (trace.plane.normal[2] > 0.7)
  232. {
  233. blocked |= 1; // floor
  234. if (trace.ent->v.solid == SOLID_BSP)
  235. {
  236. ent->v.flags = (int)ent->v.flags | FL_ONGROUND;
  237. ent->v.groundentity = EDICT_TO_PROG(trace.ent);
  238. }
  239. }
  240. if (!trace.plane.normal[2])
  241. {
  242. blocked |= 2; // step
  243. if (steptrace)
  244. *steptrace = trace; // save for player extrafriction
  245. }
  246. //
  247. // run the impact function
  248. //
  249. SV_Impact (ent, trace.ent);
  250. if (ent->free)
  251. break; // removed by the impact function
  252. time_left -= time_left * trace.fraction;
  253. // cliped to another plane
  254. if (numplanes >= MAX_CLIP_PLANES)
  255. { // this shouldn't really happen
  256. VectorCopy (vec3_origin, ent->v.velocity);
  257. return 3;
  258. }
  259. VectorCopy (trace.plane.normal, planes[numplanes]);
  260. numplanes++;
  261. //
  262. // modify original_velocity so it parallels all of the clip planes
  263. //
  264. for (i=0 ; i<numplanes ; i++)
  265. {
  266. ClipVelocity (original_velocity, planes[i], new_velocity, 1);
  267. for (j=0 ; j<numplanes ; j++)
  268. if (j != i)
  269. {
  270. if (DotProduct (new_velocity, planes[j]) < 0)
  271. break; // not ok
  272. }
  273. if (j == numplanes)
  274. break;
  275. }
  276. if (i != numplanes)
  277. { // go along this plane
  278. VectorCopy (new_velocity, ent->v.velocity);
  279. }
  280. else
  281. { // go along the crease
  282. if (numplanes != 2)
  283. {
  284. // Con_Printf ("clip velocity, numplanes == %i\n",numplanes);
  285. VectorCopy (vec3_origin, ent->v.velocity);
  286. return 7;
  287. }
  288. CrossProduct (planes[0], planes[1], dir);
  289. d = DotProduct (dir, ent->v.velocity);
  290. VectorScale (dir, d, ent->v.velocity);
  291. }
  292. //
  293. // if original velocity is against the original velocity, stop dead
  294. // to avoid tiny occilations in sloping corners
  295. //
  296. if (DotProduct (ent->v.velocity, primal_velocity) <= 0)
  297. {
  298. VectorCopy (vec3_origin, ent->v.velocity);
  299. return blocked;
  300. }
  301. }
  302. return blocked;
  303. }
  304. /*
  305. ============
  306. SV_AddGravity
  307. ============
  308. */
  309. void SV_AddGravity (edict_t *ent, float scale)
  310. {
  311. ent->v.velocity[2] -= scale * movevars.gravity * host_frametime;
  312. }
  313. /*
  314. ===============================================================================
  315. PUSHMOVE
  316. ===============================================================================
  317. */
  318. /*
  319. ============
  320. SV_PushEntity
  321. Does not change the entities velocity at all
  322. ============
  323. */
  324. trace_t SV_PushEntity (edict_t *ent, vec3_t push)
  325. {
  326. trace_t trace;
  327. vec3_t end;
  328. VectorAdd (ent->v.origin, push, end);
  329. if (ent->v.movetype == MOVETYPE_FLYMISSILE)
  330. trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, end, MOVE_MISSILE, ent);
  331. else if (ent->v.solid == SOLID_TRIGGER || ent->v.solid == SOLID_NOT)
  332. // only clip against bmodels
  333. trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, end, MOVE_NOMONSTERS, ent);
  334. else
  335. trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, end, MOVE_NORMAL, ent);
  336. VectorCopy (trace.endpos, ent->v.origin);
  337. SV_LinkEdict (ent, true);
  338. if (trace.ent)
  339. SV_Impact (ent, trace.ent);
  340. return trace;
  341. }
  342. /*
  343. ============
  344. SV_Push
  345. ============
  346. */
  347. qboolean SV_Push (edict_t *pusher, vec3_t move)
  348. {
  349. int i, e;
  350. edict_t *check, *block;
  351. vec3_t mins, maxs;
  352. vec3_t pushorig;
  353. int num_moved;
  354. edict_t *moved_edict[MAX_EDICTS];
  355. vec3_t moved_from[MAX_EDICTS];
  356. for (i=0 ; i<3 ; i++)
  357. {
  358. mins[i] = pusher->v.absmin[i] + move[i];
  359. maxs[i] = pusher->v.absmax[i] + move[i];
  360. }
  361. VectorCopy (pusher->v.origin, pushorig);
  362. // move the pusher to it's final position
  363. VectorAdd (pusher->v.origin, move, pusher->v.origin);
  364. SV_LinkEdict (pusher, false);
  365. // see if any solid entities are inside the final position
  366. num_moved = 0;
  367. check = NEXT_EDICT(sv.edicts);
  368. for (e=1 ; e<sv.num_edicts ; e++, check = NEXT_EDICT(check))
  369. {
  370. if (check->free)
  371. continue;
  372. if (check->v.movetype == MOVETYPE_PUSH
  373. || check->v.movetype == MOVETYPE_NONE
  374. || check->v.movetype == MOVETYPE_NOCLIP)
  375. continue;
  376. pusher->v.solid = SOLID_NOT;
  377. block = SV_TestEntityPosition (check);
  378. pusher->v.solid = SOLID_BSP;
  379. if (block)
  380. continue;
  381. // if the entity is standing on the pusher, it will definately be moved
  382. if ( ! ( ((int)check->v.flags & FL_ONGROUND)
  383. && PROG_TO_EDICT(check->v.groundentity) == pusher) )
  384. {
  385. if ( check->v.absmin[0] >= maxs[0]
  386. || check->v.absmin[1] >= maxs[1]
  387. || check->v.absmin[2] >= maxs[2]
  388. || check->v.absmax[0] <= mins[0]
  389. || check->v.absmax[1] <= mins[1]
  390. || check->v.absmax[2] <= mins[2] )
  391. continue;
  392. // see if the ent's bbox is inside the pusher's final position
  393. if (!SV_TestEntityPosition (check))
  394. continue;
  395. }
  396. VectorCopy (check->v.origin, moved_from[num_moved]);
  397. moved_edict[num_moved] = check;
  398. num_moved++;
  399. // try moving the contacted entity
  400. VectorAdd (check->v.origin, move, check->v.origin);
  401. block = SV_TestEntityPosition (check);
  402. if (!block)
  403. { // pushed ok
  404. SV_LinkEdict (check, false);
  405. continue;
  406. }
  407. // if it is ok to leave in the old position, do it
  408. VectorSubtract (check->v.origin, move, check->v.origin);
  409. block = SV_TestEntityPosition (check);
  410. if (!block)
  411. {
  412. num_moved--;
  413. continue;
  414. }
  415. // if it is still inside the pusher, block
  416. if (check->v.mins[0] == check->v.maxs[0])
  417. {
  418. SV_LinkEdict (check, false);
  419. continue;
  420. }
  421. if (check->v.solid == SOLID_NOT || check->v.solid == SOLID_TRIGGER)
  422. { // corpse
  423. check->v.mins[0] = check->v.mins[1] = 0;
  424. VectorCopy (check->v.mins, check->v.maxs);
  425. SV_LinkEdict (check, false);
  426. continue;
  427. }
  428. VectorCopy (pushorig, pusher->v.origin);
  429. SV_LinkEdict (pusher, false);
  430. // if the pusher has a "blocked" function, call it
  431. // otherwise, just stay in place until the obstacle is gone
  432. if (pusher->v.blocked)
  433. {
  434. pr_global_struct->self = EDICT_TO_PROG(pusher);
  435. pr_global_struct->other = EDICT_TO_PROG(check);
  436. PR_ExecuteProgram (pusher->v.blocked);
  437. }
  438. // move back any entities we already moved
  439. for (i=0 ; i<num_moved ; i++)
  440. {
  441. VectorCopy (moved_from[i], moved_edict[i]->v.origin);
  442. SV_LinkEdict (moved_edict[i], false);
  443. }
  444. return false;
  445. }
  446. return true;
  447. }
  448. /*
  449. ============
  450. SV_PushMove
  451. ============
  452. */
  453. void SV_PushMove (edict_t *pusher, float movetime)
  454. {
  455. int i;
  456. vec3_t move;
  457. if (!pusher->v.velocity[0] && !pusher->v.velocity[1] && !pusher->v.velocity[2])
  458. {
  459. pusher->v.ltime += movetime;
  460. return;
  461. }
  462. for (i=0 ; i<3 ; i++)
  463. move[i] = pusher->v.velocity[i] * movetime;
  464. if (SV_Push (pusher, move))
  465. pusher->v.ltime += movetime;
  466. }
  467. /*
  468. ================
  469. SV_Physics_Pusher
  470. ================
  471. */
  472. void SV_Physics_Pusher (edict_t *ent)
  473. {
  474. float thinktime;
  475. float oldltime;
  476. float movetime;
  477. vec3_t oldorg, move;
  478. float l;
  479. oldltime = ent->v.ltime;
  480. thinktime = ent->v.nextthink;
  481. if (thinktime < ent->v.ltime + host_frametime)
  482. {
  483. movetime = thinktime - ent->v.ltime;
  484. if (movetime < 0)
  485. movetime = 0;
  486. }
  487. else
  488. movetime = host_frametime;
  489. if (movetime)
  490. {
  491. SV_PushMove (ent, movetime); // advances ent->v.ltime if not blocked
  492. }
  493. if (thinktime > oldltime && thinktime <= ent->v.ltime)
  494. {
  495. VectorCopy (ent->v.origin, oldorg);
  496. ent->v.nextthink = 0;
  497. pr_global_struct->time = sv.time;
  498. pr_global_struct->self = EDICT_TO_PROG(ent);
  499. pr_global_struct->other = EDICT_TO_PROG(sv.edicts);
  500. PR_ExecuteProgram (ent->v.think);
  501. if (ent->free)
  502. return;
  503. VectorSubtract (ent->v.origin, oldorg, move);
  504. l = Length(move);
  505. if (l > 1.0/64)
  506. {
  507. // Con_Printf ("**** snap: %f\n", Length (l));
  508. VectorCopy (oldorg, ent->v.origin);
  509. SV_Push (ent, move);
  510. }
  511. }
  512. }
  513. /*
  514. =============
  515. SV_Physics_None
  516. Non moving objects can only think
  517. =============
  518. */
  519. void SV_Physics_None (edict_t *ent)
  520. {
  521. // regular thinking
  522. SV_RunThink (ent);
  523. }
  524. /*
  525. =============
  526. SV_Physics_Noclip
  527. A moving object that doesn't obey physics
  528. =============
  529. */
  530. void SV_Physics_Noclip (edict_t *ent)
  531. {
  532. // regular thinking
  533. if (!SV_RunThink (ent))
  534. return;
  535. VectorMA (ent->v.angles, host_frametime, ent->v.avelocity, ent->v.angles);
  536. VectorMA (ent->v.origin, host_frametime, ent->v.velocity, ent->v.origin);
  537. SV_LinkEdict (ent, false);
  538. }
  539. /*
  540. ==============================================================================
  541. TOSS / BOUNCE
  542. ==============================================================================
  543. */
  544. /*
  545. =============
  546. SV_CheckWaterTransition
  547. =============
  548. */
  549. void SV_CheckWaterTransition (edict_t *ent)
  550. {
  551. int cont;
  552. cont = SV_PointContents (ent->v.origin);
  553. if (!ent->v.watertype)
  554. { // just spawned here
  555. ent->v.watertype = cont;
  556. ent->v.waterlevel = 1;
  557. return;
  558. }
  559. if (cont <= CONTENTS_WATER)
  560. {
  561. if (ent->v.watertype == CONTENTS_EMPTY)
  562. { // just crossed into water
  563. SV_StartSound (ent, 0, "misc/h2ohit1.wav", 255, 1);
  564. }
  565. ent->v.watertype = cont;
  566. ent->v.waterlevel = 1;
  567. }
  568. else
  569. {
  570. if (ent->v.watertype != CONTENTS_EMPTY)
  571. { // just crossed into water
  572. SV_StartSound (ent, 0, "misc/h2ohit1.wav", 255, 1);
  573. }
  574. ent->v.watertype = CONTENTS_EMPTY;
  575. ent->v.waterlevel = cont;
  576. }
  577. }
  578. /*
  579. =============
  580. SV_Physics_Toss
  581. Toss, bounce, and fly movement. When onground, do nothing.
  582. =============
  583. */
  584. void SV_Physics_Toss (edict_t *ent)
  585. {
  586. trace_t trace;
  587. vec3_t move;
  588. float backoff;
  589. // regular thinking
  590. if (!SV_RunThink (ent))
  591. return;
  592. if (ent->v.velocity[2] > 0)
  593. ent->v.flags = (int)ent->v.flags & ~FL_ONGROUND;
  594. // if onground, return without moving
  595. if ( ((int)ent->v.flags & FL_ONGROUND) )
  596. return;
  597. SV_CheckVelocity (ent);
  598. // add gravity
  599. if (ent->v.movetype != MOVETYPE_FLY
  600. && ent->v.movetype != MOVETYPE_FLYMISSILE)
  601. SV_AddGravity (ent, 1.0);
  602. // move angles
  603. VectorMA (ent->v.angles, host_frametime, ent->v.avelocity, ent->v.angles);
  604. // move origin
  605. VectorScale (ent->v.velocity, host_frametime, move);
  606. trace = SV_PushEntity (ent, move);
  607. if (trace.fraction == 1)
  608. return;
  609. if (ent->free)
  610. return;
  611. if (ent->v.movetype == MOVETYPE_BOUNCE)
  612. backoff = 1.5;
  613. else
  614. backoff = 1;
  615. ClipVelocity (ent->v.velocity, trace.plane.normal, ent->v.velocity, backoff);
  616. // stop if on ground
  617. if (trace.plane.normal[2] > 0.7)
  618. {
  619. if (ent->v.velocity[2] < 60 || ent->v.movetype != MOVETYPE_BOUNCE )
  620. {
  621. ent->v.flags = (int)ent->v.flags | FL_ONGROUND;
  622. ent->v.groundentity = EDICT_TO_PROG(trace.ent);
  623. VectorCopy (vec3_origin, ent->v.velocity);
  624. VectorCopy (vec3_origin, ent->v.avelocity);
  625. }
  626. }
  627. // check for in water
  628. SV_CheckWaterTransition (ent);
  629. }
  630. /*
  631. ===============================================================================
  632. STEPPING MOVEMENT
  633. ===============================================================================
  634. */
  635. /*
  636. =============
  637. SV_Physics_Step
  638. Monsters freefall when they don't have a ground entity, otherwise
  639. all movement is done with discrete steps.
  640. This is also used for objects that have become still on the ground, but
  641. will fall if the floor is pulled out from under them.
  642. FIXME: is this true?
  643. =============
  644. */
  645. void SV_Physics_Step (edict_t *ent)
  646. {
  647. qboolean hitsound;
  648. // frefall if not onground
  649. if ( ! ((int)ent->v.flags & (FL_ONGROUND | FL_FLY | FL_SWIM) ) )
  650. {
  651. if (ent->v.velocity[2] < movevars.gravity*-0.1)
  652. hitsound = true;
  653. else
  654. hitsound = false;
  655. SV_AddGravity (ent, 1.0);
  656. SV_CheckVelocity (ent);
  657. SV_FlyMove (ent, host_frametime, NULL);
  658. SV_LinkEdict (ent, true);
  659. if ( (int)ent->v.flags & FL_ONGROUND ) // just hit ground
  660. {
  661. if (hitsound)
  662. SV_StartSound (ent, 0, "demon/dland2.wav", 255, 1);
  663. }
  664. }
  665. // regular thinking
  666. SV_RunThink (ent);
  667. SV_CheckWaterTransition (ent);
  668. }
  669. //============================================================================
  670. void SV_ProgStartFrame (void)
  671. {
  672. // let the progs know that a new frame has started
  673. pr_global_struct->self = EDICT_TO_PROG(sv.edicts);
  674. pr_global_struct->other = EDICT_TO_PROG(sv.edicts);
  675. pr_global_struct->time = sv.time;
  676. PR_ExecuteProgram (pr_global_struct->StartFrame);
  677. }
  678. /*
  679. ================
  680. SV_RunEntity
  681. ================
  682. */
  683. void SV_RunEntity (edict_t *ent)
  684. {
  685. if (ent->v.lastruntime == (float)realtime)
  686. return;
  687. ent->v.lastruntime = (float)realtime;
  688. switch ( (int)ent->v.movetype)
  689. {
  690. case MOVETYPE_PUSH:
  691. SV_Physics_Pusher (ent);
  692. break;
  693. case MOVETYPE_NONE:
  694. SV_Physics_None (ent);
  695. break;
  696. case MOVETYPE_NOCLIP:
  697. SV_Physics_Noclip (ent);
  698. break;
  699. case MOVETYPE_STEP:
  700. SV_Physics_Step (ent);
  701. break;
  702. case MOVETYPE_TOSS:
  703. case MOVETYPE_BOUNCE:
  704. case MOVETYPE_FLY:
  705. case MOVETYPE_FLYMISSILE:
  706. SV_Physics_Toss (ent);
  707. break;
  708. default:
  709. SV_Error ("SV_Physics: bad movetype %i", (int)ent->v.movetype);
  710. }
  711. }
  712. /*
  713. ================
  714. SV_RunNewmis
  715. ================
  716. */
  717. void SV_RunNewmis (void)
  718. {
  719. edict_t *ent;
  720. if (!pr_global_struct->newmis)
  721. return;
  722. ent = PROG_TO_EDICT(pr_global_struct->newmis);
  723. host_frametime = 0.05;
  724. pr_global_struct->newmis = 0;
  725. SV_RunEntity (ent);
  726. }
  727. /*
  728. ================
  729. SV_Physics
  730. ================
  731. */
  732. void SV_Physics (void)
  733. {
  734. int i;
  735. edict_t *ent;
  736. static double old_time;
  737. // don't bother running a frame if sys_ticrate seconds haven't passed
  738. host_frametime = realtime - old_time;
  739. if (host_frametime < sv_mintic.value)
  740. return;
  741. if (host_frametime > sv_maxtic.value)
  742. host_frametime = sv_maxtic.value;
  743. old_time = realtime;
  744. pr_global_struct->frametime = host_frametime;
  745. SV_ProgStartFrame ();
  746. //
  747. // treat each object in turn
  748. // even the world gets a chance to think
  749. //
  750. ent = sv.edicts;
  751. for (i=0 ; i<sv.num_edicts ; i++, ent = NEXT_EDICT(ent))
  752. {
  753. if (ent->free)
  754. continue;
  755. if (pr_global_struct->force_retouch)
  756. SV_LinkEdict (ent, true); // force retouch even for stationary
  757. if (i > 0 && i <= MAX_CLIENTS)
  758. continue; // clients are run directly from packets
  759. SV_RunEntity (ent);
  760. SV_RunNewmis ();
  761. }
  762. if (pr_global_struct->force_retouch)
  763. pr_global_struct->force_retouch--;
  764. }
  765. void SV_SetMoveVars(void)
  766. {
  767. movevars.gravity = sv_gravity.value;
  768. movevars.stopspeed = sv_stopspeed.value;
  769. movevars.maxspeed = sv_maxspeed.value;
  770. movevars.spectatormaxspeed = sv_spectatormaxspeed.value;
  771. movevars.accelerate = sv_accelerate.value;
  772. movevars.airaccelerate = sv_airaccelerate.value;
  773. movevars.wateraccelerate = sv_wateraccelerate.value;
  774. movevars.friction = sv_friction.value;
  775. movevars.waterfriction = sv_waterfriction.value;
  776. movevars.entgravity = 1.0;
  777. }