g_phys.cpp 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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
  7. and push normal objects when they move.
  8. onground is set for toss objects when they come to a complete rest. it is set for steping or walking objects
  9. doors, plats, etc are SOLID_BSP, and MOVETYPE_PUSH
  10. bonus items are SOLID_TRIGGER touch, and MOVETYPE_TOSS
  11. corpses are SOLID_NOT and MOVETYPE_TOSS
  12. crates are SOLID_BBOX and MOVETYPE_TOSS
  13. walking monsters are SOLID_SLIDEBOX and MOVETYPE_STEP
  14. flying/floating monsters are SOLID_SLIDEBOX and MOVETYPE_FLY
  15. solid_edge items only clip against bsp models.
  16. */
  17. void SV_Physics_NewToss(edict_t *ent); // PGM
  18. // [Paril-KEX] fetch the clipmask for this entity; certain modifiers
  19. // affect the clipping behavior of objects.
  20. contents_t G_GetClipMask(edict_t *ent)
  21. {
  22. contents_t mask = ent->clipmask;
  23. // default masks
  24. if (!mask)
  25. {
  26. if (ent->svflags & SVF_MONSTER)
  27. mask = MASK_MONSTERSOLID;
  28. else if (ent->svflags & SVF_PROJECTILE)
  29. mask = MASK_PROJECTILE;
  30. else
  31. mask = MASK_SHOT & ~CONTENTS_DEADMONSTER;
  32. }
  33. // non-solid objects (items, etc) shouldn't try to clip
  34. // against players/monsters
  35. if (ent->solid == SOLID_NOT || ent->solid == SOLID_TRIGGER)
  36. mask &= ~(CONTENTS_MONSTER | CONTENTS_PLAYER);
  37. // monsters/players that are also dead shouldn't clip
  38. // against players/monsters
  39. if ((ent->svflags & (SVF_MONSTER | SVF_PLAYER)) && (ent->svflags & SVF_DEADMONSTER))
  40. mask &= ~(CONTENTS_MONSTER | CONTENTS_PLAYER);
  41. return mask;
  42. }
  43. /*
  44. ============
  45. SV_TestEntityPosition
  46. ============
  47. */
  48. edict_t *SV_TestEntityPosition(edict_t *ent)
  49. {
  50. trace_t trace;
  51. trace = gi.trace(ent->s.origin, ent->mins, ent->maxs, ent->s.origin, ent, G_GetClipMask(ent));
  52. if (trace.startsolid)
  53. return g_edicts;
  54. return nullptr;
  55. }
  56. /*
  57. ================
  58. SV_CheckVelocity
  59. ================
  60. */
  61. void SV_CheckVelocity(edict_t *ent)
  62. {
  63. //
  64. // bound velocity
  65. //
  66. float speed = ent->velocity.length();
  67. if (speed > sv_maxvelocity->value)
  68. ent->velocity = (ent->velocity / speed) * sv_maxvelocity->value;
  69. }
  70. /*
  71. =============
  72. SV_RunThink
  73. Runs thinking code for this frame if necessary
  74. =============
  75. */
  76. bool SV_RunThink(edict_t *ent)
  77. {
  78. gtime_t thinktime = ent->nextthink;
  79. if (thinktime <= 0_ms)
  80. return true;
  81. if (thinktime > level.time)
  82. return true;
  83. ent->nextthink = 0_ms;
  84. if (!ent->think)
  85. gi.Com_Error("nullptr ent->think");
  86. ent->think(ent);
  87. return false;
  88. }
  89. /*
  90. ==================
  91. G_Impact
  92. Two entities have touched, so run their touch functions
  93. ==================
  94. */
  95. void G_Impact(edict_t *e1, const trace_t &trace)
  96. {
  97. edict_t *e2 = trace.ent;
  98. if (e1->touch && (e1->solid != SOLID_NOT || (e1->flags & FL_ALWAYS_TOUCH)))
  99. e1->touch(e1, e2, trace, false);
  100. if (e2->touch && (e2->solid != SOLID_NOT || (e2->flags & FL_ALWAYS_TOUCH)))
  101. e2->touch(e2, e1, trace, true);
  102. }
  103. /*
  104. ============
  105. SV_FlyMove
  106. The basic solid body movement clip that slides along multiple planes
  107. ============
  108. */
  109. void SV_FlyMove(edict_t *ent, float time, contents_t mask)
  110. {
  111. ent->groundentity = nullptr;
  112. touch_list_t touch;
  113. PM_StepSlideMove_Generic(ent->s.origin, ent->velocity, time, ent->mins, ent->maxs, touch, false, [&](const vec3_t &start, const vec3_t &mins, const vec3_t &maxs, const vec3_t &end)
  114. {
  115. return gi.trace(start, mins, maxs, end, ent, mask);
  116. });
  117. for (size_t i = 0; i < touch.num; i++)
  118. {
  119. auto &trace = touch.traces[i];
  120. if (trace.plane.normal[2] > 0.7f)
  121. {
  122. ent->groundentity = trace.ent;
  123. ent->groundentity_linkcount = trace.ent->linkcount;
  124. }
  125. //
  126. // run the impact function
  127. //
  128. G_Impact(ent, trace);
  129. // impact func requested velocity kill
  130. if (ent->flags & FL_KILL_VELOCITY)
  131. {
  132. ent->flags &= ~FL_KILL_VELOCITY;
  133. ent->velocity = {};
  134. }
  135. }
  136. }
  137. /*
  138. ============
  139. SV_AddGravity
  140. ============
  141. */
  142. void SV_AddGravity(edict_t *ent)
  143. {
  144. ent->velocity += ent->gravityVector * (ent->gravity * level.gravity * gi.frame_time_s);
  145. }
  146. /*
  147. ===============================================================================
  148. PUSHMOVE
  149. ===============================================================================
  150. */
  151. /*
  152. ============
  153. SV_PushEntity
  154. Does not change the entities velocity at all
  155. ============
  156. */
  157. trace_t SV_PushEntity(edict_t *ent, const vec3_t &push)
  158. {
  159. vec3_t start = ent->s.origin;
  160. vec3_t end = start + push;
  161. trace_t trace = gi.trace(start, ent->mins, ent->maxs, end, ent, G_GetClipMask(ent));
  162. ent->s.origin = trace.endpos + (trace.plane.normal * .5f);
  163. gi.linkentity(ent);
  164. if (trace.fraction != 1.0f || trace.startsolid)
  165. {
  166. G_Impact(ent, trace);
  167. // if the pushed entity went away and the pusher is still there
  168. if (!trace.ent->inuse && ent->inuse)
  169. {
  170. // move the pusher back and try again
  171. ent->s.origin = start;
  172. gi.linkentity(ent);
  173. return SV_PushEntity(ent, push);
  174. }
  175. }
  176. // ================
  177. // PGM
  178. // FIXME - is this needed?
  179. ent->gravity = 1.0;
  180. // PGM
  181. // ================
  182. if (ent->inuse)
  183. G_TouchTriggers(ent);
  184. return trace;
  185. }
  186. struct pushed_t
  187. {
  188. edict_t *ent;
  189. vec3_t origin;
  190. vec3_t angles;
  191. bool rotated;
  192. float yaw;
  193. };
  194. pushed_t pushed[MAX_EDICTS], *pushed_p;
  195. edict_t *obstacle;
  196. /*
  197. ============
  198. SV_Push
  199. Objects need to be moved back on a failed push,
  200. otherwise riders would continue to slide.
  201. ============
  202. */
  203. bool SV_Push(edict_t *pusher, vec3_t &move, vec3_t &amove)
  204. {
  205. edict_t *check, *block = nullptr;
  206. vec3_t mins, maxs;
  207. pushed_t *p;
  208. vec3_t org, org2, move2, forward, right, up;
  209. // find the bounding box
  210. mins = pusher->absmin + move;
  211. maxs = pusher->absmax + move;
  212. // we need this for pushing things later
  213. org = -amove;
  214. AngleVectors(org, forward, right, up);
  215. // save the pusher's original position
  216. pushed_p->ent = pusher;
  217. pushed_p->origin = pusher->s.origin;
  218. pushed_p->angles = pusher->s.angles;
  219. pushed_p->rotated = false;
  220. pushed_p++;
  221. // move the pusher to it's final position
  222. pusher->s.origin += move;
  223. pusher->s.angles += amove;
  224. gi.linkentity(pusher);
  225. // see if any solid entities are inside the final position
  226. check = g_edicts + 1;
  227. for (uint32_t e = 1; e < globals.num_edicts; e++, check++)
  228. {
  229. if (!check->inuse)
  230. continue;
  231. if (check->movetype == MOVETYPE_PUSH || check->movetype == MOVETYPE_STOP || check->movetype == MOVETYPE_NONE ||
  232. check->movetype == MOVETYPE_NOCLIP)
  233. continue;
  234. if (!check->linked)
  235. continue; // not linked in anywhere
  236. // if the entity is standing on the pusher, it will definitely be moved
  237. if (check->groundentity != pusher)
  238. {
  239. // see if the ent needs to be tested
  240. if (check->absmin[0] >= maxs[0] || check->absmin[1] >= maxs[1] || check->absmin[2] >= maxs[2] ||
  241. check->absmax[0] <= mins[0] || check->absmax[1] <= mins[1] || check->absmax[2] <= mins[2])
  242. continue;
  243. // see if the ent's bbox is inside the pusher's final position
  244. if (!SV_TestEntityPosition(check))
  245. continue;
  246. }
  247. if ((pusher->movetype == MOVETYPE_PUSH) || (check->groundentity == pusher))
  248. {
  249. // move this entity
  250. pushed_p->ent = check;
  251. pushed_p->origin = check->s.origin;
  252. pushed_p->angles = check->s.angles;
  253. pushed_p->rotated = !!amove[YAW];
  254. if (pushed_p->rotated)
  255. pushed_p->yaw =
  256. pusher->client ? (float) pusher->client->ps.pmove.delta_angles[YAW] : pusher->s.angles[YAW];
  257. pushed_p++;
  258. vec3_t old_position = check->s.origin;
  259. // try moving the contacted entity
  260. check->s.origin += move;
  261. if (check->client)
  262. {
  263. // Paril: disabled because in vanilla delta_angles are never
  264. // lerped. delta_angles can probably be lerped as long as event
  265. // isn't EV_PLAYER_TELEPORT or a new RDF flag is set
  266. // check->client->ps.pmove.delta_angles[YAW] += amove[YAW];
  267. }
  268. else
  269. check->s.angles[YAW] += amove[YAW];
  270. // figure movement due to the pusher's amove
  271. org = check->s.origin - pusher->s.origin;
  272. org2[0] = org.dot(forward);
  273. org2[1] = -(org.dot(right));
  274. org2[2] = org.dot(up);
  275. move2 = org2 - org;
  276. check->s.origin += move2;
  277. // may have pushed them off an edge
  278. if (check->groundentity != pusher)
  279. check->groundentity = nullptr;
  280. block = SV_TestEntityPosition(check);
  281. // [Paril-KEX] this is a bit of a hack; allow dead player skulls
  282. // to be a blocker because otherwise elevators/doors get stuck
  283. if (block && check->client && !check->takedamage)
  284. {
  285. check->s.origin = old_position;
  286. block = nullptr;
  287. }
  288. if (!block)
  289. { // pushed ok
  290. gi.linkentity(check);
  291. // impact?
  292. continue;
  293. }
  294. // if it is ok to leave in the old position, do it.
  295. // this is only relevent for riding entities, not pushed
  296. check->s.origin = old_position;
  297. block = SV_TestEntityPosition(check);
  298. if (!block)
  299. {
  300. pushed_p--;
  301. continue;
  302. }
  303. }
  304. // save off the obstacle so we can call the block function
  305. obstacle = check;
  306. // move back any entities we already moved
  307. // go backwards, so if the same entity was pushed
  308. // twice, it goes back to the original position
  309. for (p = pushed_p - 1; p >= pushed; p--)
  310. {
  311. p->ent->s.origin = p->origin;
  312. p->ent->s.angles = p->angles;
  313. if (p->rotated)
  314. {
  315. //if (p->ent->client)
  316. // p->ent->client->ps.pmove.delta_angles[YAW] = p->yaw;
  317. //else
  318. p->ent->s.angles[YAW] = p->yaw;
  319. }
  320. gi.linkentity(p->ent);
  321. }
  322. return false;
  323. }
  324. // FIXME: is there a better way to handle this?
  325. // see if anything we moved has touched a trigger
  326. for (p = pushed_p - 1; p >= pushed; p--)
  327. G_TouchTriggers(p->ent);
  328. return true;
  329. }
  330. /*
  331. ================
  332. SV_Physics_Pusher
  333. Bmodel objects don't interact with each other, but
  334. push all box objects
  335. ================
  336. */
  337. void SV_Physics_Pusher(edict_t *ent)
  338. {
  339. vec3_t move, amove;
  340. edict_t *part, *mv;
  341. // if not a team captain, so movement will be handled elsewhere
  342. if (ent->flags & FL_TEAMSLAVE)
  343. return;
  344. // make sure all team slaves can move before commiting
  345. // any moves or calling any think functions
  346. // if the move is blocked, all moved objects will be backed out
  347. retry:
  348. pushed_p = pushed;
  349. for (part = ent; part; part = part->teamchain)
  350. {
  351. if (part->velocity[0] || part->velocity[1] || part->velocity[2] || part->avelocity[0] || part->avelocity[1] ||
  352. part->avelocity[2])
  353. { // object is moving
  354. move = part->velocity * gi.frame_time_s;
  355. amove = part->avelocity * gi.frame_time_s;
  356. if (!SV_Push(part, move, amove))
  357. break; // move was blocked
  358. }
  359. }
  360. if (pushed_p > &pushed[MAX_EDICTS])
  361. gi.Com_Error("pushed_p > &pushed[MAX_EDICTS], memory corrupted");
  362. if (part)
  363. {
  364. // if the pusher has a "blocked" function, call it
  365. // otherwise, just stay in place until the obstacle is gone
  366. if (part->moveinfo.blocked)
  367. part->moveinfo.blocked(part, obstacle);
  368. if (!obstacle->inuse)
  369. goto retry;
  370. }
  371. else
  372. {
  373. // the move succeeded, so call all think functions
  374. for (part = ent; part; part = part->teamchain)
  375. {
  376. // prevent entities that are on trains that have gone away from thinking!
  377. if (part->inuse)
  378. SV_RunThink(part);
  379. }
  380. }
  381. }
  382. //==================================================================
  383. /*
  384. =============
  385. SV_Physics_None
  386. Non moving objects can only think
  387. =============
  388. */
  389. void SV_Physics_None(edict_t *ent)
  390. {
  391. // regular thinking
  392. SV_RunThink(ent);
  393. }
  394. /*
  395. =============
  396. SV_Physics_Noclip
  397. A moving object that doesn't obey physics
  398. =============
  399. */
  400. void SV_Physics_Noclip(edict_t *ent)
  401. {
  402. // regular thinking
  403. if (!SV_RunThink(ent) || !ent->inuse)
  404. return;
  405. ent->s.angles += (ent->avelocity * gi.frame_time_s);
  406. ent->s.origin += (ent->velocity * gi.frame_time_s);
  407. gi.linkentity(ent);
  408. }
  409. /*
  410. ==============================================================================
  411. TOSS / BOUNCE
  412. ==============================================================================
  413. */
  414. /*
  415. =============
  416. SV_Physics_Toss
  417. Toss, bounce, and fly movement. When onground, do nothing.
  418. =============
  419. */
  420. void SV_Physics_Toss(edict_t *ent)
  421. {
  422. trace_t trace;
  423. vec3_t move;
  424. float backoff;
  425. edict_t *slave;
  426. bool wasinwater;
  427. bool isinwater;
  428. vec3_t old_origin;
  429. // regular thinking
  430. SV_RunThink(ent);
  431. if (!ent->inuse)
  432. return;
  433. // if not a team captain, so movement will be handled elsewhere
  434. if (ent->flags & FL_TEAMSLAVE)
  435. return;
  436. if (ent->velocity[2] > 0)
  437. ent->groundentity = nullptr;
  438. // check for the groundentity going away
  439. if (ent->groundentity)
  440. if (!ent->groundentity->inuse)
  441. ent->groundentity = nullptr;
  442. // if onground, return without moving
  443. if (ent->groundentity && ent->gravity > 0.0f) // PGM - gravity hack
  444. {
  445. if (ent->svflags & SVF_MONSTER)
  446. {
  447. M_CatagorizePosition(ent, ent->s.origin, ent->waterlevel, ent->watertype);
  448. M_WorldEffects(ent);
  449. }
  450. return;
  451. }
  452. old_origin = ent->s.origin;
  453. SV_CheckVelocity(ent);
  454. // add gravity
  455. if (ent->movetype != MOVETYPE_FLY &&
  456. ent->movetype != MOVETYPE_FLYMISSILE
  457. // RAFAEL
  458. // move type for rippergun projectile
  459. && ent->movetype != MOVETYPE_WALLBOUNCE
  460. // RAFAEL
  461. )
  462. SV_AddGravity(ent);
  463. // move angles
  464. ent->s.angles += (ent->avelocity * gi.frame_time_s);
  465. // move origin
  466. int num_tries = 5;
  467. float time_left = gi.frame_time_s;
  468. while (time_left)
  469. {
  470. if (num_tries == 0)
  471. break;
  472. num_tries--;
  473. move = ent->velocity * time_left;
  474. trace = SV_PushEntity(ent, move);
  475. if (!ent->inuse)
  476. return;
  477. if (trace.fraction == 1.f)
  478. break;
  479. // [Paril-KEX] don't build up velocity if we're stuck.
  480. // just assume that the object we hit is our ground.
  481. else if (trace.allsolid)
  482. {
  483. ent->groundentity = trace.ent;
  484. ent->groundentity_linkcount = trace.ent->linkcount;
  485. ent->velocity = {};
  486. ent->avelocity = {};
  487. break;
  488. }
  489. time_left -= time_left * trace.fraction;
  490. if (ent->movetype == MOVETYPE_TOSS)
  491. ent->velocity = SlideClipVelocity(ent->velocity, trace.plane.normal, 0.5f);
  492. else
  493. {
  494. // RAFAEL
  495. if (ent->movetype == MOVETYPE_WALLBOUNCE)
  496. backoff = 2.0f;
  497. // RAFAEL
  498. else
  499. backoff = 1.6f;
  500. ent->velocity = ClipVelocity(ent->velocity, trace.plane.normal, backoff);
  501. }
  502. // RAFAEL
  503. if (ent->movetype == MOVETYPE_WALLBOUNCE)
  504. ent->s.angles = vectoangles(ent->velocity);
  505. // RAFAEL
  506. // stop if on ground
  507. else
  508. {
  509. if (trace.plane.normal[2] > 0.7f)
  510. {
  511. if ((ent->movetype == MOVETYPE_TOSS && ent->velocity.length() < 60.f) ||
  512. (ent->movetype != MOVETYPE_TOSS && ent->velocity.scaled(trace.plane.normal).length() < 60.f))
  513. {
  514. if (!(ent->flags & FL_NO_STANDING) || trace.ent->solid == SOLID_BSP)
  515. {
  516. ent->groundentity = trace.ent;
  517. ent->groundentity_linkcount = trace.ent->linkcount;
  518. }
  519. ent->velocity = {};
  520. ent->avelocity = {};
  521. break;
  522. }
  523. // friction for tossing stuff (gibs, etc)
  524. if (ent->movetype == MOVETYPE_TOSS)
  525. {
  526. ent->velocity *= 0.75f;
  527. ent->avelocity *= 0.75f;
  528. }
  529. }
  530. }
  531. // only toss "slides" multiple times
  532. if (ent->movetype != MOVETYPE_TOSS)
  533. break;
  534. }
  535. // check for water transition
  536. wasinwater = (ent->watertype & MASK_WATER);
  537. ent->watertype = gi.pointcontents(ent->s.origin);
  538. isinwater = ent->watertype & MASK_WATER;
  539. if (isinwater)
  540. ent->waterlevel = WATER_FEET;
  541. else
  542. ent->waterlevel = WATER_NONE;
  543. if (ent->svflags & SVF_MONSTER)
  544. {
  545. M_CatagorizePosition(ent, ent->s.origin, ent->waterlevel, ent->watertype);
  546. M_WorldEffects(ent);
  547. }
  548. else
  549. {
  550. if (!wasinwater && isinwater)
  551. gi.positioned_sound(old_origin, g_edicts, CHAN_AUTO, gi.soundindex("misc/h2ohit1.wav"), 1, 1, 0);
  552. else if (wasinwater && !isinwater)
  553. gi.positioned_sound(ent->s.origin, g_edicts, CHAN_AUTO, gi.soundindex("misc/h2ohit1.wav"), 1, 1, 0);
  554. }
  555. // prevent softlocks from keys falling into slime/lava
  556. if (isinwater && ent->watertype & (CONTENTS_SLIME | CONTENTS_LAVA) && ent->item &&
  557. (ent->item->flags & IF_KEY) && ent->spawnflags.has(SPAWNFLAG_ITEM_DROPPED))
  558. ent->velocity = { crandom_open() * 300, crandom_open() * 300, 300.f + (crandom_open() * 300.f) };
  559. // move teamslaves
  560. for (slave = ent->teamchain; slave; slave = slave->teamchain)
  561. {
  562. slave->s.origin = ent->s.origin;
  563. gi.linkentity(slave);
  564. }
  565. }
  566. /*
  567. ===============================================================================
  568. STEPPING MOVEMENT
  569. ===============================================================================
  570. */
  571. /*
  572. =============
  573. SV_Physics_Step
  574. Monsters freefall when they don't have a ground entity, otherwise
  575. all movement is done with discrete steps.
  576. This is also used for objects that have become still on the ground, but
  577. will fall if the floor is pulled out from under them.
  578. =============
  579. */
  580. void SV_AddRotationalFriction(edict_t *ent)
  581. {
  582. int n;
  583. float adjustment;
  584. ent->s.angles += (ent->avelocity * gi.frame_time_s);
  585. adjustment = gi.frame_time_s * sv_stopspeed->value * sv_friction; // PGM now a cvar
  586. for (n = 0; n < 3; n++)
  587. {
  588. if (ent->avelocity[n] > 0)
  589. {
  590. ent->avelocity[n] -= adjustment;
  591. if (ent->avelocity[n] < 0)
  592. ent->avelocity[n] = 0;
  593. }
  594. else
  595. {
  596. ent->avelocity[n] += adjustment;
  597. if (ent->avelocity[n] > 0)
  598. ent->avelocity[n] = 0;
  599. }
  600. }
  601. }
  602. void SV_Physics_Step(edict_t *ent)
  603. {
  604. bool wasonground;
  605. bool hitsound = false;
  606. float *vel;
  607. float speed, newspeed, control;
  608. float friction;
  609. edict_t *groundentity;
  610. contents_t mask = G_GetClipMask(ent);
  611. // airborne monsters should always check for ground
  612. if (!ent->groundentity)
  613. M_CheckGround(ent, mask);
  614. groundentity = ent->groundentity;
  615. SV_CheckVelocity(ent);
  616. if (groundentity)
  617. wasonground = true;
  618. else
  619. wasonground = false;
  620. if (ent->avelocity[0] || ent->avelocity[1] || ent->avelocity[2])
  621. SV_AddRotationalFriction(ent);
  622. // FIXME: figure out how or why this is happening
  623. if (isnan(ent->velocity[0]) || isnan(ent->velocity[1]) || isnan(ent->velocity[2]))
  624. ent->velocity = {};
  625. // add gravity except:
  626. // flying monsters
  627. // swimming monsters who are in the water
  628. if (!wasonground)
  629. if (!(ent->flags & FL_FLY))
  630. if (!((ent->flags & FL_SWIM) && (ent->waterlevel > WATER_WAIST)))
  631. {
  632. if (ent->velocity[2] < level.gravity * -0.1f)
  633. hitsound = true;
  634. if (ent->waterlevel != WATER_UNDER)
  635. SV_AddGravity(ent);
  636. }
  637. // friction for flying monsters that have been given vertical velocity
  638. if ((ent->flags & FL_FLY) && (ent->velocity[2] != 0) && !(ent->monsterinfo.aiflags & AI_ALTERNATE_FLY))
  639. {
  640. speed = fabsf(ent->velocity[2]);
  641. control = speed < sv_stopspeed->value ? sv_stopspeed->value : speed;
  642. friction = sv_friction / 3;
  643. newspeed = speed - (gi.frame_time_s * control * friction);
  644. if (newspeed < 0)
  645. newspeed = 0;
  646. newspeed /= speed;
  647. ent->velocity[2] *= newspeed;
  648. }
  649. // friction for flying monsters that have been given vertical velocity
  650. if ((ent->flags & FL_SWIM) && (ent->velocity[2] != 0) && !(ent->monsterinfo.aiflags & AI_ALTERNATE_FLY))
  651. {
  652. speed = fabsf(ent->velocity[2]);
  653. control = speed < sv_stopspeed->value ? sv_stopspeed->value : speed;
  654. newspeed = speed - (gi.frame_time_s * control * sv_waterfriction * (float) ent->waterlevel);
  655. if (newspeed < 0)
  656. newspeed = 0;
  657. newspeed /= speed;
  658. ent->velocity[2] *= newspeed;
  659. }
  660. if (ent->velocity[2] || ent->velocity[1] || ent->velocity[0])
  661. {
  662. // apply friction
  663. if ((wasonground || (ent->flags & (FL_SWIM | FL_FLY))) && !(ent->monsterinfo.aiflags & AI_ALTERNATE_FLY))
  664. {
  665. vel = &ent->velocity.x;
  666. speed = sqrtf(vel[0] * vel[0] + vel[1] * vel[1]);
  667. if (speed)
  668. {
  669. friction = sv_friction;
  670. // Paril: lower friction for dead monsters
  671. if (ent->deadflag)
  672. friction *= 0.5f;
  673. control = speed < sv_stopspeed->value ? sv_stopspeed->value : speed;
  674. newspeed = speed - gi.frame_time_s * control * friction;
  675. if (newspeed < 0)
  676. newspeed = 0;
  677. newspeed /= speed;
  678. vel[0] *= newspeed;
  679. vel[1] *= newspeed;
  680. }
  681. }
  682. vec3_t old_origin = ent->s.origin;
  683. SV_FlyMove(ent, gi.frame_time_s, mask);
  684. G_TouchProjectiles(ent, old_origin);
  685. M_CheckGround(ent, mask);
  686. gi.linkentity(ent);
  687. // ========
  688. // PGM - reset this every time they move.
  689. // G_touchtriggers will set it back if appropriate
  690. ent->gravity = 1.0;
  691. // ========
  692. // [Paril-KEX] this is something N64 does to avoid doors opening
  693. // at the start of a level, which triggers some monsters to spawn.
  694. if (!level.is_n64 || level.time > FRAME_TIME_S)
  695. G_TouchTriggers(ent);
  696. if (!ent->inuse)
  697. return;
  698. if (ent->groundentity)
  699. if (!wasonground)
  700. if (hitsound)
  701. ent->s.event = EV_FOOTSTEP;
  702. }
  703. if (!ent->inuse) // PGM g_touchtrigger free problem
  704. return;
  705. if (ent->svflags & SVF_MONSTER)
  706. {
  707. M_CatagorizePosition(ent, ent->s.origin, ent->waterlevel, ent->watertype);
  708. M_WorldEffects(ent);
  709. // [Paril-KEX] last minute hack to fix Stalker upside down gravity
  710. if (wasonground != !!ent->groundentity)
  711. {
  712. if (ent->monsterinfo.physics_change)
  713. ent->monsterinfo.physics_change(ent);
  714. }
  715. }
  716. // regular thinking
  717. SV_RunThink(ent);
  718. }
  719. // [Paril-KEX]
  720. inline void G_RunBmodelAnimation(edict_t *ent)
  721. {
  722. auto &anim = ent->bmodel_anim;
  723. if (anim.currently_alternate != anim.alternate)
  724. {
  725. anim.currently_alternate = anim.alternate;
  726. anim.next_tick = 0_ms;
  727. }
  728. if (level.time < anim.next_tick)
  729. return;
  730. const auto &speed = anim.alternate ? anim.alt_speed : anim.speed;
  731. anim.next_tick = level.time + gtime_t::from_ms(speed);
  732. const auto &style = anim.alternate ? anim.alt_style : anim.style;
  733. const auto &start = anim.alternate ? anim.alt_start : anim.start;
  734. const auto &end = anim.alternate ? anim.alt_end : anim.end;
  735. switch (style)
  736. {
  737. case BMODEL_ANIM_FORWARDS:
  738. if (end >= start)
  739. ent->s.frame++;
  740. else
  741. ent->s.frame--;
  742. break;
  743. case BMODEL_ANIM_BACKWARDS:
  744. if (end >= start)
  745. ent->s.frame--;
  746. else
  747. ent->s.frame++;
  748. break;
  749. case BMODEL_ANIM_RANDOM:
  750. ent->s.frame = irandom(start, end + 1);
  751. break;
  752. }
  753. const auto &nowrap = anim.alternate ? anim.alt_nowrap : anim.nowrap;
  754. if (nowrap)
  755. {
  756. if (end >= start)
  757. ent->s.frame = clamp(ent->s.frame, start, end);
  758. else
  759. ent->s.frame = clamp(ent->s.frame, end, start);
  760. }
  761. else
  762. {
  763. if (ent->s.frame < start)
  764. ent->s.frame = end;
  765. else if (ent->s.frame > end)
  766. ent->s.frame = start;
  767. }
  768. }
  769. //============================================================================
  770. /*
  771. ================
  772. G_RunEntity
  773. ================
  774. */
  775. void G_RunEntity(edict_t *ent)
  776. {
  777. // PGM
  778. trace_t trace;
  779. vec3_t previous_origin;
  780. bool has_previous_origin = false;
  781. if (ent->movetype == MOVETYPE_STEP)
  782. {
  783. previous_origin = ent->s.origin;
  784. has_previous_origin = true;
  785. }
  786. // PGM
  787. if (ent->prethink)
  788. ent->prethink(ent);
  789. // bmodel animation stuff runs first, so custom entities
  790. // can override them
  791. if (ent->bmodel_anim.enabled)
  792. G_RunBmodelAnimation(ent);
  793. switch ((int) ent->movetype)
  794. {
  795. case MOVETYPE_PUSH:
  796. case MOVETYPE_STOP:
  797. SV_Physics_Pusher(ent);
  798. break;
  799. case MOVETYPE_NONE:
  800. SV_Physics_None(ent);
  801. break;
  802. case MOVETYPE_NOCLIP:
  803. SV_Physics_Noclip(ent);
  804. break;
  805. case MOVETYPE_STEP:
  806. SV_Physics_Step(ent);
  807. break;
  808. case MOVETYPE_TOSS:
  809. case MOVETYPE_BOUNCE:
  810. case MOVETYPE_FLY:
  811. case MOVETYPE_FLYMISSILE:
  812. // RAFAEL
  813. case MOVETYPE_WALLBOUNCE:
  814. // RAFAEL
  815. SV_Physics_Toss(ent);
  816. break;
  817. // ROGUE
  818. case MOVETYPE_NEWTOSS:
  819. SV_Physics_NewToss(ent);
  820. break;
  821. // ROGUE
  822. default:
  823. gi.Com_ErrorFmt("SV_Physics: bad movetype {}", (int32_t) ent->movetype);
  824. }
  825. // PGM
  826. if (has_previous_origin && ent->movetype == MOVETYPE_STEP)
  827. {
  828. // if we moved, check and fix origin if needed
  829. if (ent->s.origin != previous_origin)
  830. {
  831. trace = gi.trace(ent->s.origin, ent->mins, ent->maxs, previous_origin, ent, G_GetClipMask(ent));
  832. if (trace.allsolid || trace.startsolid)
  833. ent->s.origin = previous_origin;
  834. }
  835. }
  836. // PGM
  837. #if 0
  838. // disintegrator stuff; only for non-players
  839. if (ent->disintegrator_time)
  840. {
  841. if (ent->disintegrator_time > 100_sec)
  842. {
  843. gi.WriteByte(svc_temp_entity);
  844. gi.WriteByte(TE_BOSSTPORT);
  845. gi.WritePosition(ent->s.origin);
  846. gi.multicast(ent->s.origin, MULTICAST_PHS, false);
  847. Killed(ent, ent->disintegrator, ent->disintegrator, 999999, vec3_origin, MOD_NUKE);
  848. G_FreeEdict(ent);
  849. }
  850. ent->disintegrator_time = max(0_ms, ent->disintegrator_time - (15000_ms / gi.tick_rate));
  851. if (ent->disintegrator_time)
  852. ent->s.alpha = max(1 / 255.f, 1.f - (ent->disintegrator_time.seconds() / 100.f));
  853. else
  854. ent->s.alpha = 1;
  855. }
  856. #endif
  857. if (ent->postthink)
  858. ent->postthink(ent);
  859. }