g_phys.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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. // g_phys.c
  16. #include "g_local.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. /*
  29. ============
  30. SV_TestEntityPosition
  31. ============
  32. */
  33. edict_t *SV_TestEntityPosition (edict_t *ent)
  34. {
  35. trace_t trace;
  36. int mask;
  37. if (ent->clipmask)
  38. mask = ent->clipmask;
  39. else
  40. mask = MASK_SOLID;
  41. trace = gi.trace (ent->s.origin, ent->mins, ent->maxs, ent->s.origin, ent, mask);
  42. if (trace.startsolid)
  43. return g_edicts;
  44. return NULL;
  45. }
  46. /*
  47. ================
  48. SV_CheckVelocity
  49. ================
  50. */
  51. void SV_CheckVelocity (edict_t *ent)
  52. {
  53. int i;
  54. //
  55. // bound velocity
  56. //
  57. for (i=0 ; i<3 ; i++)
  58. {
  59. if (ent->velocity[i] > sv_maxvelocity->value)
  60. ent->velocity[i] = sv_maxvelocity->value;
  61. else if (ent->velocity[i] < -sv_maxvelocity->value)
  62. ent->velocity[i] = -sv_maxvelocity->value;
  63. }
  64. }
  65. /*
  66. =============
  67. SV_RunThink
  68. Runs thinking code for this frame if necessary
  69. =============
  70. */
  71. qboolean SV_RunThink (edict_t *ent)
  72. {
  73. float thinktime;
  74. thinktime = ent->nextthink;
  75. if (thinktime <= 0)
  76. return true;
  77. if (thinktime > level.time+0.001)
  78. return true;
  79. ent->nextthink = 0;
  80. if (!ent->think)
  81. gi.error ("NULL ent->think");
  82. ent->think (ent);
  83. return false;
  84. }
  85. /*
  86. ==================
  87. SV_Impact
  88. Two entities have touched, so run their touch functions
  89. ==================
  90. */
  91. void SV_Impact (edict_t *e1, trace_t *trace)
  92. {
  93. edict_t *e2;
  94. // cplane_t backplane;
  95. e2 = trace->ent;
  96. if (e1->touch && e1->solid != SOLID_NOT)
  97. e1->touch (e1, e2, &trace->plane, trace->surface);
  98. if (e2->touch && e2->solid != SOLID_NOT)
  99. e2->touch (e2, e1, NULL, NULL);
  100. }
  101. /*
  102. ==================
  103. ClipVelocity
  104. Slide off of the impacting object
  105. returns the blocked flags (1 = floor, 2 = step / wall)
  106. ==================
  107. */
  108. #define STOP_EPSILON 0.1
  109. int ClipVelocity (vec3_t in, vec3_t normal, vec3_t out, float overbounce)
  110. {
  111. float backoff;
  112. float change;
  113. int i, blocked;
  114. blocked = 0;
  115. if (normal[2] > 0)
  116. blocked |= 1; // floor
  117. if (!normal[2])
  118. blocked |= 2; // step
  119. backoff = DotProduct (in, normal) * overbounce;
  120. for (i=0 ; i<3 ; i++)
  121. {
  122. change = normal[i]*backoff;
  123. out[i] = in[i] - change;
  124. if (out[i] > -STOP_EPSILON && out[i] < STOP_EPSILON)
  125. out[i] = 0;
  126. }
  127. return blocked;
  128. }
  129. /*
  130. ============
  131. SV_FlyMove
  132. The basic solid body movement clip that slides along multiple planes
  133. Returns the clipflags if the velocity was modified (hit something solid)
  134. 1 = floor
  135. 2 = wall / step
  136. 4 = dead stop
  137. ============
  138. */
  139. #define MAX_CLIP_PLANES 5
  140. int SV_FlyMove (edict_t *ent, float time, int mask)
  141. {
  142. edict_t *hit;
  143. int bumpcount, numbumps;
  144. vec3_t dir;
  145. float d;
  146. int numplanes;
  147. vec3_t planes[MAX_CLIP_PLANES];
  148. vec3_t primal_velocity, original_velocity, new_velocity;
  149. int i, j;
  150. trace_t trace;
  151. vec3_t end;
  152. float time_left;
  153. int blocked;
  154. numbumps = 4;
  155. blocked = 0;
  156. VectorCopy (ent->velocity, original_velocity);
  157. VectorCopy (ent->velocity, primal_velocity);
  158. numplanes = 0;
  159. time_left = time;
  160. ent->groundentity = NULL;
  161. for (bumpcount=0 ; bumpcount<numbumps ; bumpcount++)
  162. {
  163. for (i=0 ; i<3 ; i++)
  164. end[i] = ent->s.origin[i] + time_left * ent->velocity[i];
  165. trace = gi.trace (ent->s.origin, ent->mins, ent->maxs, end, ent, mask);
  166. if (trace.allsolid)
  167. { // entity is trapped in another solid
  168. VectorCopy (vec3_origin, ent->velocity);
  169. return 3;
  170. }
  171. if (trace.fraction > 0)
  172. { // actually covered some distance
  173. VectorCopy (trace.endpos, ent->s.origin);
  174. VectorCopy (ent->velocity, original_velocity);
  175. numplanes = 0;
  176. }
  177. if (trace.fraction == 1)
  178. break; // moved the entire distance
  179. hit = trace.ent;
  180. if (trace.plane.normal[2] > 0.7)
  181. {
  182. blocked |= 1; // floor
  183. if ( hit->solid == SOLID_BSP)
  184. {
  185. ent->groundentity = hit;
  186. ent->groundentity_linkcount = hit->linkcount;
  187. }
  188. }
  189. if (!trace.plane.normal[2])
  190. {
  191. blocked |= 2; // step
  192. }
  193. //
  194. // run the impact function
  195. //
  196. SV_Impact (ent, &trace);
  197. if (!ent->inuse)
  198. break; // removed by the impact function
  199. time_left -= time_left * trace.fraction;
  200. // cliped to another plane
  201. if (numplanes >= MAX_CLIP_PLANES)
  202. { // this shouldn't really happen
  203. VectorCopy (vec3_origin, ent->velocity);
  204. return 3;
  205. }
  206. VectorCopy (trace.plane.normal, planes[numplanes]);
  207. numplanes++;
  208. //
  209. // modify original_velocity so it parallels all of the clip planes
  210. //
  211. for (i=0 ; i<numplanes ; i++)
  212. {
  213. ClipVelocity (original_velocity, planes[i], new_velocity, 1);
  214. for (j=0 ; j<numplanes ; j++)
  215. if ((j != i) && !VectorCompare (planes[i], planes[j]))
  216. {
  217. if (DotProduct (new_velocity, planes[j]) < 0)
  218. break; // not ok
  219. }
  220. if (j == numplanes)
  221. break;
  222. }
  223. if (i != numplanes)
  224. { // go along this plane
  225. VectorCopy (new_velocity, ent->velocity);
  226. }
  227. else
  228. { // go along the crease
  229. if (numplanes != 2)
  230. {
  231. // gi.dprintf ("clip velocity, numplanes == %i\n",numplanes);
  232. VectorCopy (vec3_origin, ent->velocity);
  233. return 7;
  234. }
  235. CrossProduct (planes[0], planes[1], dir);
  236. d = DotProduct (dir, ent->velocity);
  237. VectorScale (dir, d, ent->velocity);
  238. }
  239. //
  240. // if original velocity is against the original velocity, stop dead
  241. // to avoid tiny occilations in sloping corners
  242. //
  243. if (DotProduct (ent->velocity, primal_velocity) <= 0)
  244. {
  245. VectorCopy (vec3_origin, ent->velocity);
  246. return blocked;
  247. }
  248. }
  249. return blocked;
  250. }
  251. /*
  252. ============
  253. SV_AddGravity
  254. ============
  255. */
  256. void SV_AddGravity (edict_t *ent)
  257. {
  258. ent->velocity[2] -= ent->gravity * sv_gravity->value * FRAMETIME;
  259. }
  260. /*
  261. ===============================================================================
  262. PUSHMOVE
  263. ===============================================================================
  264. */
  265. /*
  266. ============
  267. SV_PushEntity
  268. Does not change the entities velocity at all
  269. ============
  270. */
  271. trace_t SV_PushEntity (edict_t *ent, vec3_t push)
  272. {
  273. trace_t trace;
  274. vec3_t start;
  275. vec3_t end;
  276. int mask;
  277. VectorCopy (ent->s.origin, start);
  278. VectorAdd (start, push, end);
  279. retry:
  280. if (ent->clipmask)
  281. mask = ent->clipmask;
  282. else
  283. mask = MASK_SOLID;
  284. trace = gi.trace (start, ent->mins, ent->maxs, end, ent, mask);
  285. VectorCopy (trace.endpos, ent->s.origin);
  286. gi.linkentity (ent);
  287. if (trace.fraction != 1.0)
  288. {
  289. SV_Impact (ent, &trace);
  290. // if the pushed entity went away and the pusher is still there
  291. if (!trace.ent->inuse && ent->inuse)
  292. {
  293. // move the pusher back and try again
  294. VectorCopy (start, ent->s.origin);
  295. gi.linkentity (ent);
  296. goto retry;
  297. }
  298. }
  299. if (ent->inuse)
  300. G_TouchTriggers (ent);
  301. return trace;
  302. }
  303. typedef struct
  304. {
  305. edict_t *ent;
  306. vec3_t origin;
  307. vec3_t angles;
  308. float deltayaw;
  309. } pushed_t;
  310. pushed_t pushed[MAX_EDICTS], *pushed_p;
  311. edict_t *obstacle;
  312. /*
  313. ============
  314. SV_Push
  315. Objects need to be moved back on a failed push,
  316. otherwise riders would continue to slide.
  317. ============
  318. */
  319. qboolean SV_Push (edict_t *pusher, vec3_t move, vec3_t amove)
  320. {
  321. int i, e;
  322. edict_t *check, *block;
  323. vec3_t mins, maxs;
  324. pushed_t *p;
  325. vec3_t org, org2, move2, forward, right, up;
  326. // clamp the move to 1/8 units, so the position will
  327. // be accurate for client side prediction
  328. for (i=0 ; i<3 ; i++)
  329. {
  330. float temp;
  331. temp = move[i]*8.0;
  332. if (temp > 0.0)
  333. temp += 0.5;
  334. else
  335. temp -= 0.5;
  336. move[i] = 0.125 * (int)temp;
  337. }
  338. // find the bounding box
  339. for (i=0 ; i<3 ; i++)
  340. {
  341. mins[i] = pusher->absmin[i] + move[i];
  342. maxs[i] = pusher->absmax[i] + move[i];
  343. }
  344. // we need this for pushing things later
  345. VectorSubtract (vec3_origin, amove, org);
  346. AngleVectors (org, forward, right, up);
  347. // save the pusher's original position
  348. pushed_p->ent = pusher;
  349. VectorCopy (pusher->s.origin, pushed_p->origin);
  350. VectorCopy (pusher->s.angles, pushed_p->angles);
  351. if (pusher->client)
  352. pushed_p->deltayaw = pusher->client->ps.pmove.delta_angles[YAW];
  353. pushed_p++;
  354. // move the pusher to it's final position
  355. VectorAdd (pusher->s.origin, move, pusher->s.origin);
  356. VectorAdd (pusher->s.angles, amove, pusher->s.angles);
  357. gi.linkentity (pusher);
  358. // see if any solid entities are inside the final position
  359. check = g_edicts+1;
  360. for (e = 1; e < globals.num_edicts; e++, check++)
  361. {
  362. if (!check->inuse)
  363. continue;
  364. if (check->movetype == MOVETYPE_PUSH
  365. || check->movetype == MOVETYPE_STOP
  366. || check->movetype == MOVETYPE_NONE
  367. || check->movetype == MOVETYPE_NOCLIP)
  368. continue;
  369. if (!check->area.prev)
  370. continue; // not linked in anywhere
  371. // if the entity is standing on the pusher, it will definitely be moved
  372. if (check->groundentity != pusher)
  373. {
  374. // see if the ent needs to be tested
  375. if ( check->absmin[0] >= maxs[0]
  376. || check->absmin[1] >= maxs[1]
  377. || check->absmin[2] >= maxs[2]
  378. || check->absmax[0] <= mins[0]
  379. || check->absmax[1] <= mins[1]
  380. || check->absmax[2] <= mins[2] )
  381. continue;
  382. // see if the ent's bbox is inside the pusher's final position
  383. if (!SV_TestEntityPosition (check))
  384. continue;
  385. }
  386. if ((pusher->movetype == MOVETYPE_PUSH) || (check->groundentity == pusher))
  387. {
  388. // move this entity
  389. pushed_p->ent = check;
  390. VectorCopy (check->s.origin, pushed_p->origin);
  391. VectorCopy (check->s.angles, pushed_p->angles);
  392. pushed_p++;
  393. // try moving the contacted entity
  394. VectorAdd (check->s.origin, move, check->s.origin);
  395. if (check->client)
  396. { // FIXME: doesn't rotate monsters?
  397. check->client->ps.pmove.delta_angles[YAW] += amove[YAW];
  398. }
  399. // figure movement due to the pusher's amove
  400. VectorSubtract (check->s.origin, pusher->s.origin, org);
  401. org2[0] = DotProduct (org, forward);
  402. org2[1] = -DotProduct (org, right);
  403. org2[2] = DotProduct (org, up);
  404. VectorSubtract (org2, org, move2);
  405. VectorAdd (check->s.origin, move2, check->s.origin);
  406. // may have pushed them off an edge
  407. if (check->groundentity != pusher)
  408. check->groundentity = NULL;
  409. block = SV_TestEntityPosition (check);
  410. if (!block)
  411. { // pushed ok
  412. gi.linkentity (check);
  413. // impact?
  414. continue;
  415. }
  416. // if it is ok to leave in the old position, do it
  417. // this is only relevent for riding entities, not pushed
  418. // FIXME: this doesn't acount for rotation
  419. VectorSubtract (check->s.origin, move, check->s.origin);
  420. block = SV_TestEntityPosition (check);
  421. if (!block)
  422. {
  423. pushed_p--;
  424. continue;
  425. }
  426. }
  427. // save off the obstacle so we can call the block function
  428. obstacle = check;
  429. // move back any entities we already moved
  430. // go backwards, so if the same entity was pushed
  431. // twice, it goes back to the original position
  432. for (p=pushed_p-1 ; p>=pushed ; p--)
  433. {
  434. VectorCopy (p->origin, p->ent->s.origin);
  435. VectorCopy (p->angles, p->ent->s.angles);
  436. if (p->ent->client)
  437. {
  438. p->ent->client->ps.pmove.delta_angles[YAW] = p->deltayaw;
  439. }
  440. gi.linkentity (p->ent);
  441. }
  442. return false;
  443. }
  444. //FIXME: is there a better way to handle this?
  445. // see if anything we moved has touched a trigger
  446. for (p=pushed_p-1 ; p>=pushed ; p--)
  447. G_TouchTriggers (p->ent);
  448. return true;
  449. }
  450. /*
  451. ================
  452. SV_Physics_Pusher
  453. Bmodel objects don't interact with each other, but
  454. push all box objects
  455. ================
  456. */
  457. void SV_Physics_Pusher (edict_t *ent)
  458. {
  459. vec3_t move, amove;
  460. edict_t *part, *mv;
  461. // if not a team captain, so movement will be handled elsewhere
  462. if ( ent->flags & FL_TEAMSLAVE)
  463. return;
  464. // make sure all team slaves can move before commiting
  465. // any moves or calling any think functions
  466. // if the move is blocked, all moved objects will be backed out
  467. //retry:
  468. pushed_p = pushed;
  469. for (part = ent ; part ; part=part->teamchain)
  470. {
  471. if (part->velocity[0] || part->velocity[1] || part->velocity[2] ||
  472. part->avelocity[0] || part->avelocity[1] || part->avelocity[2]
  473. )
  474. { // object is moving
  475. VectorScale (part->velocity, FRAMETIME, move);
  476. VectorScale (part->avelocity, FRAMETIME, amove);
  477. if (!SV_Push (part, move, amove))
  478. break; // move was blocked
  479. }
  480. }
  481. if (pushed_p > &pushed[MAX_EDICTS])
  482. gi.error (ERR_FATAL, "pushed_p > &pushed[MAX_EDICTS], memory corrupted");
  483. if (part)
  484. {
  485. // the move failed, bump all nextthink times and back out moves
  486. for (mv = ent ; mv ; mv=mv->teamchain)
  487. {
  488. if (mv->nextthink > 0)
  489. mv->nextthink += FRAMETIME;
  490. }
  491. // if the pusher has a "blocked" function, call it
  492. // otherwise, just stay in place until the obstacle is gone
  493. if (part->blocked)
  494. part->blocked (part, obstacle);
  495. #if 0
  496. // if the pushed entity went away and the pusher is still there
  497. if (!obstacle->inuse && part->inuse)
  498. goto retry;
  499. #endif
  500. }
  501. else
  502. {
  503. // the move succeeded, so call all think functions
  504. for (part = ent ; part ; part=part->teamchain)
  505. {
  506. SV_RunThink (part);
  507. }
  508. }
  509. }
  510. //==================================================================
  511. /*
  512. =============
  513. SV_Physics_None
  514. Non moving objects can only think
  515. =============
  516. */
  517. void SV_Physics_None (edict_t *ent)
  518. {
  519. // regular thinking
  520. SV_RunThink (ent);
  521. }
  522. /*
  523. =============
  524. SV_Physics_Noclip
  525. A moving object that doesn't obey physics
  526. =============
  527. */
  528. void SV_Physics_Noclip (edict_t *ent)
  529. {
  530. // regular thinking
  531. if (!SV_RunThink (ent))
  532. return;
  533. VectorMA (ent->s.angles, FRAMETIME, ent->avelocity, ent->s.angles);
  534. VectorMA (ent->s.origin, FRAMETIME, ent->velocity, ent->s.origin);
  535. gi.linkentity (ent);
  536. }
  537. /*
  538. ==============================================================================
  539. TOSS / BOUNCE
  540. ==============================================================================
  541. */
  542. /*
  543. =============
  544. SV_Physics_Toss
  545. Toss, bounce, and fly movement. When onground, do nothing.
  546. =============
  547. */
  548. void SV_Physics_Toss (edict_t *ent)
  549. {
  550. trace_t trace;
  551. vec3_t move;
  552. float backoff;
  553. edict_t *slave;
  554. qboolean wasinwater;
  555. qboolean isinwater;
  556. vec3_t old_origin;
  557. // regular thinking
  558. SV_RunThink (ent);
  559. // if not a team captain, so movement will be handled elsewhere
  560. if ( ent->flags & FL_TEAMSLAVE)
  561. return;
  562. if (ent->velocity[2] > 0)
  563. ent->groundentity = NULL;
  564. // check for the groundentity going away
  565. if (ent->groundentity)
  566. if (!ent->groundentity->inuse)
  567. ent->groundentity = NULL;
  568. // if onground, return without moving
  569. if ( ent->groundentity )
  570. return;
  571. VectorCopy (ent->s.origin, old_origin);
  572. SV_CheckVelocity (ent);
  573. // add gravity
  574. if (ent->movetype != MOVETYPE_FLY
  575. && ent->movetype != MOVETYPE_FLYMISSILE)
  576. SV_AddGravity (ent);
  577. // move angles
  578. VectorMA (ent->s.angles, FRAMETIME, ent->avelocity, ent->s.angles);
  579. // move origin
  580. VectorScale (ent->velocity, FRAMETIME, move);
  581. trace = SV_PushEntity (ent, move);
  582. if (!ent->inuse)
  583. return;
  584. if (trace.fraction < 1)
  585. {
  586. if (ent->movetype == MOVETYPE_BOUNCE)
  587. backoff = 1.5;
  588. else
  589. backoff = 1;
  590. ClipVelocity (ent->velocity, trace.plane.normal, ent->velocity, backoff);
  591. // stop if on ground
  592. if (trace.plane.normal[2] > 0.7)
  593. {
  594. if (ent->velocity[2] < 60 || ent->movetype != MOVETYPE_BOUNCE )
  595. {
  596. ent->groundentity = trace.ent;
  597. ent->groundentity_linkcount = trace.ent->linkcount;
  598. VectorCopy (vec3_origin, ent->velocity);
  599. VectorCopy (vec3_origin, ent->avelocity);
  600. }
  601. }
  602. // if (ent->touch)
  603. // ent->touch (ent, trace.ent, &trace.plane, trace.surface);
  604. }
  605. // check for water transition
  606. wasinwater = (ent->watertype & MASK_WATER);
  607. ent->watertype = gi.pointcontents (ent->s.origin);
  608. isinwater = ent->watertype & MASK_WATER;
  609. if (isinwater)
  610. ent->waterlevel = 1;
  611. else
  612. ent->waterlevel = 0;
  613. if (!wasinwater && isinwater)
  614. gi.positioned_sound (old_origin, g_edicts, CHAN_AUTO, gi.soundindex("misc/h2ohit1.wav"), 1, 1, 0);
  615. else if (wasinwater && !isinwater)
  616. gi.positioned_sound (ent->s.origin, g_edicts, CHAN_AUTO, gi.soundindex("misc/h2ohit1.wav"), 1, 1, 0);
  617. // move teamslaves
  618. for (slave = ent->teamchain; slave; slave = slave->teamchain)
  619. {
  620. VectorCopy (ent->s.origin, slave->s.origin);
  621. gi.linkentity (slave);
  622. }
  623. }
  624. /*
  625. ===============================================================================
  626. STEPPING MOVEMENT
  627. ===============================================================================
  628. */
  629. /*
  630. =============
  631. SV_Physics_Step
  632. Monsters freefall when they don't have a ground entity, otherwise
  633. all movement is done with discrete steps.
  634. This is also used for objects that have become still on the ground, but
  635. will fall if the floor is pulled out from under them.
  636. FIXME: is this true?
  637. =============
  638. */
  639. //FIXME: hacked in for E3 demo
  640. #define sv_stopspeed 100
  641. #define sv_friction 6
  642. #define sv_waterfriction 1
  643. void SV_AddRotationalFriction (edict_t *ent)
  644. {
  645. int n;
  646. float adjustment;
  647. VectorMA (ent->s.angles, FRAMETIME, ent->avelocity, ent->s.angles);
  648. adjustment = FRAMETIME * sv_stopspeed * sv_friction;
  649. for (n = 0; n < 3; n++)
  650. {
  651. if (ent->avelocity[n] > 0)
  652. {
  653. ent->avelocity[n] -= adjustment;
  654. if (ent->avelocity[n] < 0)
  655. ent->avelocity[n] = 0;
  656. }
  657. else
  658. {
  659. ent->avelocity[n] += adjustment;
  660. if (ent->avelocity[n] > 0)
  661. ent->avelocity[n] = 0;
  662. }
  663. }
  664. }
  665. void SV_Physics_Step (edict_t *ent)
  666. {
  667. qboolean wasonground;
  668. qboolean hitsound = false;
  669. float *vel;
  670. float speed, newspeed, control;
  671. float friction;
  672. edict_t *groundentity;
  673. int mask;
  674. // airborn monsters should always check for ground
  675. if (!ent->groundentity)
  676. M_CheckGround (ent);
  677. groundentity = ent->groundentity;
  678. SV_CheckVelocity (ent);
  679. if (groundentity)
  680. wasonground = true;
  681. else
  682. wasonground = false;
  683. if (ent->avelocity[0] || ent->avelocity[1] || ent->avelocity[2])
  684. SV_AddRotationalFriction (ent);
  685. // add gravity except:
  686. // flying monsters
  687. // swimming monsters who are in the water
  688. if (! wasonground)
  689. if (!(ent->flags & FL_FLY))
  690. if (!((ent->flags & FL_SWIM) && (ent->waterlevel > 2)))
  691. {
  692. if (ent->velocity[2] < sv_gravity->value*-0.1)
  693. hitsound = true;
  694. if (ent->waterlevel == 0)
  695. SV_AddGravity (ent);
  696. }
  697. // friction for flying monsters that have been given vertical velocity
  698. if ((ent->flags & FL_FLY) && (ent->velocity[2] != 0))
  699. {
  700. speed = fabs(ent->velocity[2]);
  701. control = speed < sv_stopspeed ? sv_stopspeed : speed;
  702. friction = sv_friction/3;
  703. newspeed = speed - (FRAMETIME * control * friction);
  704. if (newspeed < 0)
  705. newspeed = 0;
  706. newspeed /= speed;
  707. ent->velocity[2] *= newspeed;
  708. }
  709. // friction for flying monsters that have been given vertical velocity
  710. if ((ent->flags & FL_SWIM) && (ent->velocity[2] != 0))
  711. {
  712. speed = fabs(ent->velocity[2]);
  713. control = speed < sv_stopspeed ? sv_stopspeed : speed;
  714. newspeed = speed - (FRAMETIME * control * sv_waterfriction * ent->waterlevel);
  715. if (newspeed < 0)
  716. newspeed = 0;
  717. newspeed /= speed;
  718. ent->velocity[2] *= newspeed;
  719. }
  720. if (ent->velocity[2] || ent->velocity[1] || ent->velocity[0])
  721. {
  722. // apply friction
  723. // let dead monsters who aren't completely onground slide
  724. if ((wasonground) || (ent->flags & (FL_SWIM|FL_FLY)))
  725. if (!(ent->health <= 0.0 && !M_CheckBottom(ent)))
  726. {
  727. vel = ent->velocity;
  728. speed = sqrt(vel[0]*vel[0] +vel[1]*vel[1]);
  729. if (speed)
  730. {
  731. friction = sv_friction;
  732. control = speed < sv_stopspeed ? sv_stopspeed : speed;
  733. newspeed = speed - FRAMETIME*control*friction;
  734. if (newspeed < 0)
  735. newspeed = 0;
  736. newspeed /= speed;
  737. vel[0] *= newspeed;
  738. vel[1] *= newspeed;
  739. }
  740. }
  741. if (ent->svflags & SVF_MONSTER)
  742. mask = MASK_MONSTERSOLID;
  743. else
  744. mask = MASK_SOLID;
  745. SV_FlyMove (ent, FRAMETIME, mask);
  746. gi.linkentity (ent);
  747. G_TouchTriggers (ent);
  748. if (!ent->inuse)
  749. return;
  750. if (ent->groundentity)
  751. if (!wasonground)
  752. if (hitsound)
  753. gi.sound (ent, 0, gi.soundindex("world/land.wav"), 1, 1, 0);
  754. }
  755. // regular thinking
  756. SV_RunThink (ent);
  757. }
  758. //============================================================================
  759. /*
  760. ================
  761. G_RunEntity
  762. ================
  763. */
  764. void G_RunEntity (edict_t *ent)
  765. {
  766. if (ent->prethink)
  767. ent->prethink (ent);
  768. switch ( (int)ent->movetype)
  769. {
  770. case MOVETYPE_PUSH:
  771. case MOVETYPE_STOP:
  772. SV_Physics_Pusher (ent);
  773. break;
  774. case MOVETYPE_NONE:
  775. SV_Physics_None (ent);
  776. break;
  777. case MOVETYPE_NOCLIP:
  778. SV_Physics_Noclip (ent);
  779. break;
  780. case MOVETYPE_STEP:
  781. SV_Physics_Step (ent);
  782. break;
  783. case MOVETYPE_TOSS:
  784. case MOVETYPE_BOUNCE:
  785. case MOVETYPE_FLY:
  786. case MOVETYPE_FLYMISSILE:
  787. SV_Physics_Toss (ent);
  788. break;
  789. default:
  790. gi.error ("SV_Physics: bad movetype %i", (int)ent->movetype);
  791. }
  792. }