g_phys.c 21 KB

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