p_view.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  1. // Copyright (c) ZeniMax Media Inc.
  2. // Licensed under the GNU General Public License 2.0.
  3. #include "g_local.h"
  4. #include "m_player.h"
  5. #include "bots/bot_includes.h"
  6. static edict_t *current_player;
  7. static gclient_t *current_client;
  8. static vec3_t forward, right, up;
  9. float xyspeed;
  10. float bobmove;
  11. int bobcycle, bobcycle_run; // odd cycles are right foot going forward
  12. float bobfracsin; // sinf(bobfrac*M_PI)
  13. /*
  14. ===============
  15. SkipViewModifiers
  16. ===============
  17. */
  18. inline bool SkipViewModifiers() {
  19. if ( g_skipViewModifiers->integer && sv_cheats->integer ) {
  20. return true;
  21. }
  22. // don't do bobbing, etc on grapple
  23. if (current_client->ctf_grapple &&
  24. current_client->ctf_grapplestate > CTF_GRAPPLE_STATE_FLY) {
  25. return true;
  26. }
  27. // spectator mode
  28. if (current_client->resp.spectator || (G_TeamplayEnabled() && current_client->resp.ctf_team == CTF_NOTEAM)) {
  29. return true;
  30. }
  31. return false;
  32. }
  33. /*
  34. ===============
  35. SV_CalcRoll
  36. ===============
  37. */
  38. float SV_CalcRoll(const vec3_t &angles, const vec3_t &velocity)
  39. {
  40. if ( SkipViewModifiers() ) {
  41. return 0.0f;
  42. }
  43. float sign;
  44. float side;
  45. float value;
  46. side = velocity.dot(right);
  47. sign = side < 0 ? -1.f : 1.f;
  48. side = fabsf(side);
  49. value = sv_rollangle->value;
  50. if (side < sv_rollspeed->value)
  51. side = side * value / sv_rollspeed->value;
  52. else
  53. side = value;
  54. return side * sign;
  55. }
  56. /*
  57. ===============
  58. P_DamageFeedback
  59. Handles color blends and view kicks
  60. ===============
  61. */
  62. void P_DamageFeedback(edict_t *player)
  63. {
  64. gclient_t *client;
  65. float side;
  66. float realcount, count, kick;
  67. vec3_t v;
  68. int l;
  69. constexpr vec3_t armor_color = { 1.0, 1.0, 1.0 };
  70. constexpr vec3_t power_color = { 0.0, 1.0, 0.0 };
  71. constexpr vec3_t bcolor = { 1.0, 0.0, 0.0 };
  72. client = player->client;
  73. // flash the backgrounds behind the status numbers
  74. int16_t want_flashes = 0;
  75. if (client->damage_blood)
  76. want_flashes |= 1;
  77. if (client->damage_armor && !(player->flags & FL_GODMODE) && (client->invincible_time <= level.time))
  78. want_flashes |= 2;
  79. if (want_flashes)
  80. {
  81. client->flash_time = level.time + 100_ms;
  82. client->ps.stats[STAT_FLASHES] = want_flashes;
  83. }
  84. else if (client->flash_time < level.time)
  85. client->ps.stats[STAT_FLASHES] = 0;
  86. // total points of damage shot at the player this frame
  87. count = (float) (client->damage_blood + client->damage_armor + client->damage_parmor);
  88. if (count == 0)
  89. return; // didn't take any damage
  90. // start a pain animation if still in the player model
  91. if (client->anim_priority < ANIM_PAIN && player->s.modelindex == MODELINDEX_PLAYER)
  92. {
  93. static int i;
  94. client->anim_priority = ANIM_PAIN;
  95. if (client->ps.pmove.pm_flags & PMF_DUCKED)
  96. {
  97. player->s.frame = FRAME_crpain1 - 1;
  98. client->anim_end = FRAME_crpain4;
  99. }
  100. else
  101. {
  102. i = (i + 1) % 3;
  103. switch (i)
  104. {
  105. case 0:
  106. player->s.frame = FRAME_pain101 - 1;
  107. client->anim_end = FRAME_pain104;
  108. break;
  109. case 1:
  110. player->s.frame = FRAME_pain201 - 1;
  111. client->anim_end = FRAME_pain204;
  112. break;
  113. case 2:
  114. player->s.frame = FRAME_pain301 - 1;
  115. client->anim_end = FRAME_pain304;
  116. break;
  117. }
  118. }
  119. client->anim_time = 0_ms;
  120. }
  121. realcount = count;
  122. // if we took health damage, do a minimum clamp
  123. if (client->damage_blood)
  124. {
  125. if (count < 10)
  126. count = 10; // always make a visible effect
  127. }
  128. else
  129. {
  130. if (count > 2)
  131. count = 2; // don't go too deep
  132. }
  133. // play an appropriate pain sound
  134. if ((level.time > player->pain_debounce_time) && !(player->flags & FL_GODMODE) && (client->invincible_time <= level.time))
  135. {
  136. player->pain_debounce_time = level.time + 700_ms;
  137. constexpr const char *pain_sounds[] = {
  138. "*pain25_1.wav",
  139. "*pain25_2.wav",
  140. "*pain50_1.wav",
  141. "*pain50_2.wav",
  142. "*pain75_1.wav",
  143. "*pain75_2.wav",
  144. "*pain100_1.wav",
  145. "*pain100_2.wav",
  146. };
  147. if (player->health < 25)
  148. l = 0;
  149. else if (player->health < 50)
  150. l = 2;
  151. else if (player->health < 75)
  152. l = 4;
  153. else
  154. l = 6;
  155. if (brandom())
  156. l |= 1;
  157. gi.sound(player, CHAN_VOICE, gi.soundindex(pain_sounds[l]), 1, ATTN_NORM, 0);
  158. // Paril: pain noises alert monsters
  159. PlayerNoise(player, player->s.origin, PNOISE_SELF);
  160. }
  161. // the total alpha of the blend is always proportional to count
  162. if (client->damage_alpha < 0)
  163. client->damage_alpha = 0;
  164. // [Paril-KEX] tweak the values to rely less on this
  165. // and more on damage indicators
  166. if (client->damage_blood || (client->damage_alpha + count * 0.06f) < 0.15f)
  167. {
  168. client->damage_alpha += count * 0.06f;
  169. if (client->damage_alpha < 0.06f)
  170. client->damage_alpha = 0.06f;
  171. if (client->damage_alpha > 0.4f)
  172. client->damage_alpha = 0.4f; // don't go too saturated
  173. }
  174. // mix in colors
  175. v = {};
  176. if (client->damage_parmor)
  177. v += power_color * (client->damage_parmor / realcount);
  178. if (client->damage_blood)
  179. v += bcolor * max(15.0f, (client->damage_blood / realcount));
  180. if (client->damage_armor)
  181. v += armor_color * (client->damage_armor / realcount);
  182. client->damage_blend = v.normalized();
  183. //
  184. // calculate view angle kicks
  185. //
  186. kick = (float) abs(client->damage_knockback);
  187. if (kick && player->health > 0) // kick of 0 means no view adjust at all
  188. {
  189. kick = kick * 100 / player->health;
  190. if (kick < count * 0.5f)
  191. kick = count * 0.5f;
  192. if (kick > 50)
  193. kick = 50;
  194. v = client->damage_from - player->s.origin;
  195. v.normalize();
  196. side = v.dot(right);
  197. client->v_dmg_roll = kick * side * 0.3f;
  198. side = -v.dot(forward);
  199. client->v_dmg_pitch = kick * side * 0.3f;
  200. client->v_dmg_time = level.time + DAMAGE_TIME();
  201. }
  202. // [Paril-KEX] send view indicators
  203. if (client->num_damage_indicators)
  204. {
  205. gi.WriteByte(svc_damage);
  206. gi.WriteByte(client->num_damage_indicators);
  207. for (uint8_t i = 0; i < client->num_damage_indicators; i++)
  208. {
  209. auto &indicator = client->damage_indicators[i];
  210. // encode total damage into 5 bits
  211. uint8_t encoded = std::clamp((indicator.health + indicator.power + indicator.armor) / 3, 1, 0x1F);
  212. // encode types in the latter 3 bits
  213. if (indicator.health)
  214. encoded |= 0x20;
  215. if (indicator.armor)
  216. encoded |= 0x40;
  217. if (indicator.power)
  218. encoded |= 0x80;
  219. gi.WriteByte(encoded);
  220. gi.WriteDir((player->s.origin - indicator.from).normalized());
  221. }
  222. gi.unicast(player, false);
  223. }
  224. //
  225. // clear totals
  226. //
  227. client->damage_blood = 0;
  228. client->damage_armor = 0;
  229. client->damage_parmor = 0;
  230. client->damage_knockback = 0;
  231. client->num_damage_indicators = 0;
  232. }
  233. /*
  234. ===============
  235. SV_CalcViewOffset
  236. Auto pitching on slopes?
  237. fall from 128: 400 = 160000
  238. fall from 256: 580 = 336400
  239. fall from 384: 720 = 518400
  240. fall from 512: 800 = 640000
  241. fall from 640: 960 =
  242. damage = deltavelocity*deltavelocity * 0.0001
  243. ===============
  244. */
  245. void SV_CalcViewOffset(edict_t *ent)
  246. {
  247. float bob;
  248. float ratio;
  249. float delta;
  250. vec3_t v;
  251. //===================================
  252. // base angles
  253. vec3_t &angles = ent->client->ps.kick_angles;
  254. // if dead, fix the angle and don't add any kick
  255. if (ent->deadflag && !ent->client->resp.spectator)
  256. {
  257. angles = {};
  258. if (ent->flags & FL_SAM_RAIMI)
  259. {
  260. ent->client->ps.viewangles[ROLL] = 0;
  261. ent->client->ps.viewangles[PITCH] = 0;
  262. }
  263. else
  264. {
  265. ent->client->ps.viewangles[ROLL] = 40;
  266. ent->client->ps.viewangles[PITCH] = -15;
  267. }
  268. ent->client->ps.viewangles[YAW] = ent->client->killer_yaw;
  269. }
  270. else if (!ent->client->pers.bob_skip && !SkipViewModifiers())
  271. {
  272. // add angles based on weapon kick
  273. angles = P_CurrentKickAngles(ent);
  274. // add angles based on damage kick
  275. if (ent->client->v_dmg_time > level.time)
  276. {
  277. // [Paril-KEX] 100ms of slack is added to account for
  278. // visual difference in higher tickrates
  279. gtime_t diff = ent->client->v_dmg_time - level.time;
  280. // slack time remaining
  281. if (DAMAGE_TIME_SLACK())
  282. {
  283. if (diff > DAMAGE_TIME() - DAMAGE_TIME_SLACK())
  284. ratio = (DAMAGE_TIME() - diff).seconds() / DAMAGE_TIME_SLACK().seconds();
  285. else
  286. ratio = diff.seconds() / (DAMAGE_TIME() - DAMAGE_TIME_SLACK()).seconds();
  287. }
  288. else
  289. ratio = diff.seconds() / (DAMAGE_TIME() - DAMAGE_TIME_SLACK()).seconds();
  290. angles[PITCH] += ratio * ent->client->v_dmg_pitch;
  291. angles[ROLL] += ratio * ent->client->v_dmg_roll;
  292. }
  293. // add pitch based on fall kick
  294. if (ent->client->fall_time > level.time)
  295. {
  296. // [Paril-KEX] 100ms of slack is added to account for
  297. // visual difference in higher tickrates
  298. gtime_t diff = ent->client->fall_time - level.time;
  299. // slack time remaining
  300. if (DAMAGE_TIME_SLACK())
  301. {
  302. if (diff > FALL_TIME() - DAMAGE_TIME_SLACK())
  303. ratio = (FALL_TIME() - diff).seconds() / DAMAGE_TIME_SLACK().seconds();
  304. else
  305. ratio = diff.seconds() / (FALL_TIME() - DAMAGE_TIME_SLACK()).seconds();
  306. }
  307. else
  308. ratio = diff.seconds() / (FALL_TIME() - DAMAGE_TIME_SLACK()).seconds();
  309. angles[PITCH] += ratio * ent->client->fall_value;
  310. }
  311. // add angles based on velocity
  312. if (!ent->client->pers.bob_skip && !SkipViewModifiers())
  313. {
  314. delta = ent->velocity.dot(forward);
  315. angles[PITCH] += delta * run_pitch->value;
  316. delta = ent->velocity.dot(right);
  317. angles[ROLL] += delta * run_roll->value;
  318. // add angles based on bob
  319. delta = bobfracsin * bob_pitch->value * xyspeed;
  320. if ((ent->client->ps.pmove.pm_flags & PMF_DUCKED) && ent->groundentity)
  321. delta *= 6; // crouching
  322. delta = min(delta, 1.2f);
  323. angles[PITCH] += delta;
  324. delta = bobfracsin * bob_roll->value * xyspeed;
  325. if ((ent->client->ps.pmove.pm_flags & PMF_DUCKED) && ent->groundentity)
  326. delta *= 6; // crouching
  327. delta = min(delta, 1.2f);
  328. if (bobcycle & 1)
  329. delta = -delta;
  330. angles[ROLL] += delta;
  331. }
  332. // add earthquake angles
  333. if (ent->client->quake_time > level.time)
  334. {
  335. float factor = min(1.0f, (ent->client->quake_time.seconds() / level.time.seconds()) * 0.25f);
  336. angles.x += crandom_open() * factor;
  337. angles.z += crandom_open() * factor;
  338. angles.y += crandom_open() * factor;
  339. }
  340. }
  341. // [Paril-KEX] clamp angles
  342. for (int i = 0; i < 3; i++)
  343. angles[i] = clamp(angles[i], -31.f, 31.f);
  344. //===================================
  345. // base origin
  346. v = {};
  347. // add fall height
  348. if (!ent->client->pers.bob_skip && !SkipViewModifiers())
  349. {
  350. if (ent->client->fall_time > level.time)
  351. {
  352. // [Paril-KEX] 100ms of slack is added to account for
  353. // visual difference in higher tickrates
  354. gtime_t diff = ent->client->fall_time - level.time;
  355. // slack time remaining
  356. if (DAMAGE_TIME_SLACK())
  357. {
  358. if (diff > FALL_TIME() - DAMAGE_TIME_SLACK())
  359. ratio = (FALL_TIME() - diff).seconds() / DAMAGE_TIME_SLACK().seconds();
  360. else
  361. ratio = diff.seconds() / (FALL_TIME() - DAMAGE_TIME_SLACK()).seconds();
  362. }
  363. else
  364. ratio = diff.seconds() / (FALL_TIME() - DAMAGE_TIME_SLACK()).seconds();
  365. v[2] -= ratio * ent->client->fall_value * 0.4f;
  366. }
  367. // add bob height
  368. bob = bobfracsin * xyspeed * bob_up->value;
  369. if (bob > 6)
  370. bob = 6;
  371. // gi.DebugGraph (bob *2, 255);
  372. v[2] += bob;
  373. }
  374. // add kick offset
  375. if (!ent->client->pers.bob_skip && !SkipViewModifiers())
  376. v += P_CurrentKickOrigin(ent);
  377. // absolutely bound offsets
  378. // so the view can never be outside the player box
  379. if (v[0] < -14)
  380. v[0] = -14;
  381. else if (v[0] > 14)
  382. v[0] = 14;
  383. if (v[1] < -14)
  384. v[1] = -14;
  385. else if (v[1] > 14)
  386. v[1] = 14;
  387. if (v[2] < -22)
  388. v[2] = -22;
  389. else if (v[2] > 30)
  390. v[2] = 30;
  391. ent->client->ps.viewoffset = v;
  392. }
  393. /*
  394. ==============
  395. SV_CalcGunOffset
  396. ==============
  397. */
  398. void SV_CalcGunOffset(edict_t *ent)
  399. {
  400. int i;
  401. // ROGUE
  402. // ROGUE - heatbeam shouldn't bob so the beam looks right
  403. if (ent->client->pers.weapon &&
  404. !((ent->client->pers.weapon->id == IT_WEAPON_PLASMABEAM || ent->client->pers.weapon->id == IT_WEAPON_GRAPPLE) && ent->client->weaponstate == WEAPON_FIRING)
  405. && !SkipViewModifiers())
  406. {
  407. // ROGUE
  408. // gun angles from bobbing
  409. ent->client->ps.gunangles[ROLL] = xyspeed * bobfracsin * 0.005f;
  410. ent->client->ps.gunangles[YAW] = xyspeed * bobfracsin * 0.01f;
  411. if (bobcycle & 1)
  412. {
  413. ent->client->ps.gunangles[ROLL] = -ent->client->ps.gunangles[ROLL];
  414. ent->client->ps.gunangles[YAW] = -ent->client->ps.gunangles[YAW];
  415. }
  416. ent->client->ps.gunangles[PITCH] = xyspeed * bobfracsin * 0.005f;
  417. vec3_t viewangles_delta = ent->client->oldviewangles - ent->client->ps.viewangles;
  418. for (i = 0; i < 3; i++)
  419. ent->client->slow_view_angles[i] += viewangles_delta[i];
  420. // gun angles from delta movement
  421. for (i = 0; i < 3; i++)
  422. {
  423. float &d = ent->client->slow_view_angles[i];
  424. if (!d)
  425. continue;
  426. if (d > 180)
  427. d -= 360;
  428. if (d < -180)
  429. d += 360;
  430. if (d > 45)
  431. d = 45;
  432. if (d < -45)
  433. d = -45;
  434. // [Sam-KEX] Apply only half-delta. Makes the weapons look less detatched from the player.
  435. if (i == ROLL)
  436. ent->client->ps.gunangles[i] += (0.1f * d) * 0.5f;
  437. else
  438. ent->client->ps.gunangles[i] += (0.2f * d) * 0.5f;
  439. float reduction_factor = viewangles_delta[i] ? 0.05f : 0.15f;
  440. if (d > 0)
  441. d = max(0.f, d - gi.frame_time_ms * reduction_factor);
  442. else if (d < 0)
  443. d = min(0.f, d + gi.frame_time_ms * reduction_factor);
  444. }
  445. // [Paril-KEX] cl_rollhack
  446. ent->client->ps.gunangles[ROLL] = -ent->client->ps.gunangles[ROLL];
  447. }
  448. // ROGUE
  449. else
  450. {
  451. for (i = 0; i < 3; i++)
  452. ent->client->ps.gunangles[i] = 0;
  453. }
  454. // ROGUE
  455. // gun height
  456. ent->client->ps.gunoffset = {};
  457. // gun_x / gun_y / gun_z are development tools
  458. for (i = 0; i < 3; i++)
  459. {
  460. ent->client->ps.gunoffset[i] += forward[i] * (gun_y->value);
  461. ent->client->ps.gunoffset[i] += right[i] * gun_x->value;
  462. ent->client->ps.gunoffset[i] += up[i] * (-gun_z->value);
  463. }
  464. }
  465. /*
  466. =============
  467. SV_CalcBlend
  468. =============
  469. */
  470. void SV_CalcBlend(edict_t *ent)
  471. {
  472. gtime_t remaining;
  473. ent->client->ps.damage_blend = ent->client->ps.screen_blend = {};
  474. // add for powerups
  475. if (ent->client->quad_time > level.time)
  476. {
  477. remaining = ent->client->quad_time - level.time;
  478. if (remaining.milliseconds() == 3000) // beginning to fade
  479. gi.sound(ent, CHAN_ITEM, gi.soundindex("items/damage2.wav"), 1, ATTN_NORM, 0);
  480. if (G_PowerUpExpiringRelative(remaining))
  481. G_AddBlend(0, 0, 1, 0.08f, ent->client->ps.screen_blend);
  482. }
  483. // RAFAEL
  484. else if (ent->client->quadfire_time > level.time)
  485. {
  486. remaining = ent->client->quadfire_time - level.time;
  487. if (remaining.milliseconds() == 3000) // beginning to fade
  488. gi.sound(ent, CHAN_ITEM, gi.soundindex("items/quadfire2.wav"), 1, ATTN_NORM, 0);
  489. if (G_PowerUpExpiringRelative(remaining))
  490. G_AddBlend(1, 0.2f, 0.5f, 0.08f, ent->client->ps.screen_blend);
  491. }
  492. // RAFAEL
  493. // PMM - double damage
  494. else if (ent->client->double_time > level.time)
  495. {
  496. remaining = ent->client->double_time - level.time;
  497. if (remaining.milliseconds() == 3000) // beginning to fade
  498. gi.sound(ent, CHAN_ITEM, gi.soundindex("misc/ddamage2.wav"), 1, ATTN_NORM, 0);
  499. if (G_PowerUpExpiringRelative(remaining))
  500. G_AddBlend(0.9f, 0.7f, 0, 0.08f, ent->client->ps.screen_blend);
  501. }
  502. // PMM
  503. else if (ent->client->invincible_time > level.time)
  504. {
  505. remaining = ent->client->invincible_time - level.time;
  506. if (remaining.milliseconds() == 3000) // beginning to fade
  507. gi.sound(ent, CHAN_ITEM, gi.soundindex("items/protect2.wav"), 1, ATTN_NORM, 0);
  508. if (G_PowerUpExpiringRelative(remaining))
  509. G_AddBlend(1, 1, 0, 0.08f, ent->client->ps.screen_blend);
  510. }
  511. else if (ent->client->invisible_time > level.time)
  512. {
  513. remaining = ent->client->invisible_time - level.time;
  514. if (remaining.milliseconds() == 3000) // beginning to fade
  515. gi.sound(ent, CHAN_ITEM, gi.soundindex("items/protect2.wav"), 1, ATTN_NORM, 0);
  516. if (G_PowerUpExpiringRelative(remaining))
  517. G_AddBlend(0.8f, 0.8f, 0.8f, 0.08f, ent->client->ps.screen_blend);
  518. }
  519. else if (ent->client->enviro_time > level.time)
  520. {
  521. remaining = ent->client->enviro_time - level.time;
  522. if (remaining.milliseconds() == 3000) // beginning to fade
  523. gi.sound(ent, CHAN_ITEM, gi.soundindex("items/airout.wav"), 1, ATTN_NORM, 0);
  524. if (G_PowerUpExpiringRelative(remaining))
  525. G_AddBlend(0, 1, 0, 0.08f, ent->client->ps.screen_blend);
  526. }
  527. else if (ent->client->breather_time > level.time)
  528. {
  529. remaining = ent->client->breather_time - level.time;
  530. if (remaining.milliseconds() == 3000) // beginning to fade
  531. gi.sound(ent, CHAN_ITEM, gi.soundindex("items/airout.wav"), 1, ATTN_NORM, 0);
  532. if (G_PowerUpExpiringRelative(remaining))
  533. G_AddBlend(0.4f, 1, 0.4f, 0.04f, ent->client->ps.screen_blend);
  534. }
  535. // PGM
  536. if (ent->client->nuke_time > level.time)
  537. {
  538. float brightness = (ent->client->nuke_time - level.time).seconds() / 2.0f;
  539. G_AddBlend(1, 1, 1, brightness, ent->client->ps.screen_blend);
  540. }
  541. if (ent->client->ir_time > level.time)
  542. {
  543. remaining = ent->client->ir_time - level.time;
  544. if (G_PowerUpExpiringRelative(remaining))
  545. {
  546. ent->client->ps.rdflags |= RDF_IRGOGGLES;
  547. G_AddBlend(1, 0, 0, 0.2f, ent->client->ps.screen_blend);
  548. }
  549. else
  550. ent->client->ps.rdflags &= ~RDF_IRGOGGLES;
  551. }
  552. else
  553. {
  554. ent->client->ps.rdflags &= ~RDF_IRGOGGLES;
  555. }
  556. // PGM
  557. // add for damage
  558. if (ent->client->damage_alpha > 0)
  559. G_AddBlend(ent->client->damage_blend[0], ent->client->damage_blend[1], ent->client->damage_blend[2], ent->client->damage_alpha, ent->client->ps.damage_blend);
  560. // [Paril-KEX] drowning visual indicator
  561. if (ent->air_finished < level.time + 9_sec)
  562. {
  563. constexpr vec3_t drown_color = { 0.1f, 0.1f, 0.2f };
  564. constexpr float max_drown_alpha = 0.75f;
  565. float alpha = (ent->air_finished < level.time) ? 1 : (1.f - ((ent->air_finished - level.time).seconds() / 9.0f));
  566. G_AddBlend(drown_color[0], drown_color[1], drown_color[2], min(alpha, max_drown_alpha), ent->client->ps.damage_blend);
  567. }
  568. #if 0
  569. if (ent->client->bonus_alpha > 0)
  570. G_AddBlend(0.85f, 0.7f, 0.3f, ent->client->bonus_alpha, ent->client->ps.damage_blend);
  571. #endif
  572. // drop the damage value
  573. ent->client->damage_alpha -= gi.frame_time_s * 0.6f;
  574. if (ent->client->damage_alpha < 0)
  575. ent->client->damage_alpha = 0;
  576. // drop the bonus value
  577. ent->client->bonus_alpha -= gi.frame_time_s;
  578. if (ent->client->bonus_alpha < 0)
  579. ent->client->bonus_alpha = 0;
  580. }
  581. /*
  582. =============
  583. P_WorldEffects
  584. =============
  585. */
  586. void P_WorldEffects()
  587. {
  588. bool breather;
  589. bool envirosuit;
  590. water_level_t waterlevel, old_waterlevel;
  591. if (current_player->movetype == MOVETYPE_NOCLIP)
  592. {
  593. current_player->air_finished = level.time + 12_sec; // don't need air
  594. return;
  595. }
  596. waterlevel = current_player->waterlevel;
  597. old_waterlevel = current_client->old_waterlevel;
  598. current_client->old_waterlevel = waterlevel;
  599. breather = current_client->breather_time > level.time;
  600. envirosuit = current_client->enviro_time > level.time;
  601. //
  602. // if just entered a water volume, play a sound
  603. //
  604. if (!old_waterlevel && waterlevel)
  605. {
  606. PlayerNoise(current_player, current_player->s.origin, PNOISE_SELF);
  607. if (current_player->watertype & CONTENTS_LAVA)
  608. gi.sound(current_player, CHAN_BODY, gi.soundindex("player/lava_in.wav"), 1, ATTN_NORM, 0);
  609. else if (current_player->watertype & CONTENTS_SLIME)
  610. gi.sound(current_player, CHAN_BODY, gi.soundindex("player/watr_in.wav"), 1, ATTN_NORM, 0);
  611. else if (current_player->watertype & CONTENTS_WATER)
  612. gi.sound(current_player, CHAN_BODY, gi.soundindex("player/watr_in.wav"), 1, ATTN_NORM, 0);
  613. current_player->flags |= FL_INWATER;
  614. // clear damage_debounce, so the pain sound will play immediately
  615. current_player->damage_debounce_time = level.time - 1_sec;
  616. }
  617. //
  618. // if just completely exited a water volume, play a sound
  619. //
  620. if (old_waterlevel && !waterlevel)
  621. {
  622. PlayerNoise(current_player, current_player->s.origin, PNOISE_SELF);
  623. gi.sound(current_player, CHAN_BODY, gi.soundindex("player/watr_out.wav"), 1, ATTN_NORM, 0);
  624. current_player->flags &= ~FL_INWATER;
  625. }
  626. //
  627. // check for head just going under water
  628. //
  629. if (old_waterlevel != WATER_UNDER && waterlevel == WATER_UNDER)
  630. {
  631. gi.sound(current_player, CHAN_BODY, gi.soundindex("player/watr_un.wav"), 1, ATTN_NORM, 0);
  632. }
  633. //
  634. // check for head just coming out of water
  635. //
  636. if (current_player->health > 0 && old_waterlevel == WATER_UNDER && waterlevel != WATER_UNDER)
  637. {
  638. if (current_player->air_finished < level.time)
  639. { // gasp for air
  640. gi.sound(current_player, CHAN_VOICE, gi.soundindex("player/gasp1.wav"), 1, ATTN_NORM, 0);
  641. PlayerNoise(current_player, current_player->s.origin, PNOISE_SELF);
  642. }
  643. else if (current_player->air_finished < level.time + 11_sec)
  644. { // just break surface
  645. gi.sound(current_player, CHAN_VOICE, gi.soundindex("player/gasp2.wav"), 1, ATTN_NORM, 0);
  646. }
  647. }
  648. //
  649. // check for drowning
  650. //
  651. if (waterlevel == WATER_UNDER)
  652. {
  653. // breather or envirosuit give air
  654. if (breather || envirosuit)
  655. {
  656. current_player->air_finished = level.time + 10_sec;
  657. if (((current_client->breather_time - level.time).milliseconds() % 2500) == 0)
  658. {
  659. if (!current_client->breather_sound)
  660. gi.sound(current_player, CHAN_AUTO, gi.soundindex("player/u_breath1.wav"), 1, ATTN_NORM, 0);
  661. else
  662. gi.sound(current_player, CHAN_AUTO, gi.soundindex("player/u_breath2.wav"), 1, ATTN_NORM, 0);
  663. current_client->breather_sound ^= 1;
  664. PlayerNoise(current_player, current_player->s.origin, PNOISE_SELF);
  665. // FIXME: release a bubble?
  666. }
  667. }
  668. // if out of air, start drowning
  669. if (current_player->air_finished < level.time)
  670. { // drown!
  671. if (current_player->client->next_drown_time < level.time && current_player->health > 0)
  672. {
  673. current_player->client->next_drown_time = level.time + 1_sec;
  674. // take more damage the longer underwater
  675. current_player->dmg += 2;
  676. if (current_player->dmg > 15)
  677. current_player->dmg = 15;
  678. // play a gurp sound instead of a normal pain sound
  679. if (current_player->health <= current_player->dmg)
  680. gi.sound(current_player, CHAN_VOICE, gi.soundindex("*drown1.wav"), 1, ATTN_NORM, 0); // [Paril-KEX]
  681. else if (brandom())
  682. gi.sound(current_player, CHAN_VOICE, gi.soundindex("*gurp1.wav"), 1, ATTN_NORM, 0);
  683. else
  684. gi.sound(current_player, CHAN_VOICE, gi.soundindex("*gurp2.wav"), 1, ATTN_NORM, 0);
  685. current_player->pain_debounce_time = level.time;
  686. T_Damage(current_player, world, world, vec3_origin, current_player->s.origin, vec3_origin, current_player->dmg, 0, DAMAGE_NO_ARMOR, MOD_WATER);
  687. }
  688. }
  689. // Paril: almost-drowning sounds
  690. else if (current_player->air_finished <= level.time + 3_sec)
  691. {
  692. if (current_player->client->next_drown_time < level.time)
  693. {
  694. gi.sound(current_player, CHAN_VOICE, gi.soundindex(fmt::format("player/wade{}.wav", 1 + ((int32_t) level.time.seconds() % 3)).c_str()), 1, ATTN_NORM, 0);
  695. current_player->client->next_drown_time = level.time + 1_sec;
  696. }
  697. }
  698. }
  699. else
  700. {
  701. current_player->air_finished = level.time + 12_sec;
  702. current_player->dmg = 2;
  703. }
  704. //
  705. // check for sizzle damage
  706. //
  707. if (waterlevel && (current_player->watertype & (CONTENTS_LAVA | CONTENTS_SLIME)) && current_player->slime_debounce_time <= level.time)
  708. {
  709. if (current_player->watertype & CONTENTS_LAVA)
  710. {
  711. if (current_player->health > 0 && current_player->pain_debounce_time <= level.time && current_client->invincible_time < level.time)
  712. {
  713. if (brandom())
  714. gi.sound(current_player, CHAN_VOICE, gi.soundindex("player/burn1.wav"), 1, ATTN_NORM, 0);
  715. else
  716. gi.sound(current_player, CHAN_VOICE, gi.soundindex("player/burn2.wav"), 1, ATTN_NORM, 0);
  717. current_player->pain_debounce_time = level.time + 1_sec;
  718. }
  719. int dmg = (envirosuit ? 1 : 3) * waterlevel; // take 1/3 damage with envirosuit
  720. T_Damage(current_player, world, world, vec3_origin, current_player->s.origin, vec3_origin, dmg, 0, DAMAGE_NONE, MOD_LAVA);
  721. current_player->slime_debounce_time = level.time + 10_hz;
  722. }
  723. if (current_player->watertype & CONTENTS_SLIME)
  724. {
  725. if (!envirosuit)
  726. { // no damage from slime with envirosuit
  727. T_Damage(current_player, world, world, vec3_origin, current_player->s.origin, vec3_origin, 1 * waterlevel, 0, DAMAGE_NONE, MOD_SLIME);
  728. current_player->slime_debounce_time = level.time + 10_hz;
  729. }
  730. }
  731. }
  732. }
  733. /*
  734. ===============
  735. G_SetClientEffects
  736. ===============
  737. */
  738. void G_SetClientEffects(edict_t *ent)
  739. {
  740. int pa_type;
  741. ent->s.effects = EF_NONE;
  742. ent->s.renderfx &= RF_STAIR_STEP;
  743. ent->s.renderfx |= RF_IR_VISIBLE;
  744. ent->s.alpha = 1.0;
  745. if (ent->health <= 0 || level.intermissiontime)
  746. return;
  747. if (ent->flags & FL_FLASHLIGHT)
  748. ent->s.effects |= EF_FLASHLIGHT;
  749. //=========
  750. // PGM
  751. if (ent->flags & FL_DISGUISED)
  752. ent->s.renderfx |= RF_USE_DISGUISE;
  753. if (gamerules->integer)
  754. {
  755. if (DMGame.PlayerEffects)
  756. DMGame.PlayerEffects(ent);
  757. }
  758. // PGM
  759. //=========
  760. if (ent->powerarmor_time > level.time)
  761. {
  762. pa_type = PowerArmorType(ent);
  763. if (pa_type == IT_ITEM_POWER_SCREEN)
  764. {
  765. ent->s.effects |= EF_POWERSCREEN;
  766. }
  767. else if (pa_type == IT_ITEM_POWER_SHIELD)
  768. {
  769. ent->s.effects |= EF_COLOR_SHELL;
  770. ent->s.renderfx |= RF_SHELL_GREEN;
  771. }
  772. }
  773. // ZOID
  774. CTFEffects(ent);
  775. // ZOID
  776. if (ent->client->quad_time > level.time)
  777. {
  778. if (G_PowerUpExpiring(ent->client->quad_time))
  779. CTFSetPowerUpEffect(ent, EF_QUAD);
  780. }
  781. // RAFAEL
  782. if (ent->client->quadfire_time > level.time)
  783. {;
  784. if (G_PowerUpExpiring(ent->client->quadfire_time))
  785. CTFSetPowerUpEffect(ent, EF_DUALFIRE);
  786. }
  787. // RAFAEL
  788. //=======
  789. // ROGUE
  790. if (ent->client->double_time > level.time)
  791. {
  792. if (G_PowerUpExpiring(ent->client->double_time))
  793. CTFSetPowerUpEffect(ent, EF_DOUBLE);
  794. }
  795. if ((ent->client->owned_sphere) && (ent->client->owned_sphere->spawnflags == SPHERE_DEFENDER))
  796. {
  797. CTFSetPowerUpEffect(ent, EF_HALF_DAMAGE);
  798. }
  799. if (ent->client->tracker_pain_time > level.time)
  800. {
  801. ent->s.effects |= EF_TRACKERTRAIL;
  802. }
  803. if (ent->client->invisible_time > level.time)
  804. {
  805. if (ent->client->invisibility_fade_time <= level.time)
  806. ent->s.alpha = 0.1f;
  807. else
  808. {
  809. float x = (ent->client->invisibility_fade_time - level.time).seconds() / INVISIBILITY_TIME.seconds();
  810. ent->s.alpha = std::clamp(x, 0.1f, 1.0f);
  811. }
  812. }
  813. // ROGUE
  814. //=======
  815. if (ent->client->invincible_time > level.time)
  816. {
  817. if (G_PowerUpExpiring(ent->client->invincible_time))
  818. CTFSetPowerUpEffect(ent, EF_PENT);
  819. }
  820. // show cheaters!!!
  821. if (ent->flags & FL_GODMODE)
  822. {
  823. ent->s.effects |= EF_COLOR_SHELL;
  824. ent->s.renderfx |= (RF_SHELL_RED | RF_SHELL_GREEN | RF_SHELL_BLUE);
  825. }
  826. #if 0
  827. // disintegrator stuff
  828. if (ent->disintegrator_time)
  829. {
  830. if (ent->disintegrator_time > 100_sec)
  831. {
  832. gi.WriteByte(svc_temp_entity);
  833. gi.WriteByte(TE_BOSSTPORT);
  834. gi.WritePosition(ent->s.origin);
  835. gi.multicast(ent->s.origin, MULTICAST_PHS, false);
  836. Killed(ent, ent, ent, 999999, vec3_origin, MOD_NUKE);
  837. ent->disintegrator_time = 0_ms;
  838. ThrowClientHead(ent, 9999);
  839. ent->s.modelindex = 0;
  840. ent->solid = SOLID_NOT;
  841. }
  842. else
  843. {
  844. ent->disintegrator_time = max(0_ms, ent->disintegrator_time - 1500_ms);
  845. if (ent->disintegrator_time)
  846. ent->s.alpha = max(1 / 255.f, 1.f - (ent->disintegrator_time.seconds() / 100));
  847. else
  848. ent->s.alpha = 1;
  849. }
  850. }
  851. #endif
  852. }
  853. /*
  854. ===============
  855. G_SetClientEvent
  856. ===============
  857. */
  858. void G_SetClientEvent(edict_t *ent)
  859. {
  860. if (ent->s.event)
  861. return;
  862. if (ent->client->ps.pmove.pm_flags & PMF_ON_LADDER)
  863. {
  864. if (!deathmatch->integer &&
  865. current_client->last_ladder_sound < level.time &&
  866. (current_client->last_ladder_pos - ent->s.origin).length() > 48.f)
  867. {
  868. ent->s.event = EV_LADDER_STEP;
  869. current_client->last_ladder_pos = ent->s.origin;
  870. current_client->last_ladder_sound = level.time + LADDER_SOUND_TIME;
  871. }
  872. }
  873. else if (ent->groundentity && xyspeed > 225)
  874. {
  875. if ((int) (current_client->bobtime + bobmove) != bobcycle_run)
  876. ent->s.event = EV_FOOTSTEP;
  877. }
  878. }
  879. /*
  880. ===============
  881. G_SetClientSound
  882. ===============
  883. */
  884. void G_SetClientSound(edict_t *ent)
  885. {
  886. // help beep (no more than three times)
  887. if (ent->client->pers.helpchanged && ent->client->pers.helpchanged <= 3 && ent->client->pers.help_time < level.time)
  888. {
  889. if (ent->client->pers.helpchanged == 1) // [KEX] haleyjd: once only
  890. gi.sound(ent, CHAN_AUTO, gi.soundindex("misc/pc_up.wav"), 1, ATTN_STATIC, 0);
  891. ent->client->pers.helpchanged++;
  892. ent->client->pers.help_time = level.time + 5_sec;
  893. }
  894. // reset defaults
  895. ent->s.sound = 0;
  896. ent->s.loop_attenuation = 0;
  897. ent->s.loop_volume = 0;
  898. if (ent->waterlevel && (ent->watertype & (CONTENTS_LAVA | CONTENTS_SLIME)))
  899. {
  900. ent->s.sound = snd_fry;
  901. return;
  902. }
  903. if (ent->deadflag || ent->client->resp.spectator)
  904. return;
  905. if (ent->client->weapon_sound)
  906. ent->s.sound = ent->client->weapon_sound;
  907. else if (ent->client->pers.weapon)
  908. {
  909. if (ent->client->pers.weapon->id == IT_WEAPON_RAILGUN)
  910. ent->s.sound = gi.soundindex("weapons/rg_hum.wav");
  911. else if (ent->client->pers.weapon->id == IT_WEAPON_BFG)
  912. ent->s.sound = gi.soundindex("weapons/bfg_hum.wav");
  913. // RAFAEL
  914. else if (ent->client->pers.weapon->id == IT_WEAPON_PHALANX)
  915. ent->s.sound = gi.soundindex("weapons/phaloop.wav");
  916. // RAFAEL
  917. }
  918. // [Paril-KEX] if no other sound is playing, play appropriate grapple sounds
  919. if (!ent->s.sound && ent->client->ctf_grapple)
  920. {
  921. if (ent->client->ctf_grapplestate == CTF_GRAPPLE_STATE_PULL)
  922. ent->s.sound = gi.soundindex("weapons/grapple/grpull.wav");
  923. else if (ent->client->ctf_grapplestate == CTF_GRAPPLE_STATE_FLY)
  924. ent->s.sound = gi.soundindex("weapons/grapple/grfly.wav");
  925. else if (ent->client->ctf_grapplestate == CTF_GRAPPLE_STATE_HANG)
  926. ent->s.sound = gi.soundindex("weapons/grapple/grhang.wav");
  927. }
  928. // weapon sounds play at a higher attn
  929. ent->s.loop_attenuation = ATTN_NORM;
  930. }
  931. /*
  932. ===============
  933. G_SetClientFrame
  934. ===============
  935. */
  936. void G_SetClientFrame(edict_t *ent)
  937. {
  938. gclient_t *client;
  939. bool duck, run;
  940. if (ent->s.modelindex != MODELINDEX_PLAYER)
  941. return; // not in the player model
  942. client = ent->client;
  943. if (client->ps.pmove.pm_flags & PMF_DUCKED)
  944. duck = true;
  945. else
  946. duck = false;
  947. if (xyspeed)
  948. run = true;
  949. else
  950. run = false;
  951. // check for stand/duck and stop/go transitions
  952. if (duck != client->anim_duck && client->anim_priority < ANIM_DEATH)
  953. goto newanim;
  954. if (run != client->anim_run && client->anim_priority == ANIM_BASIC)
  955. goto newanim;
  956. if (!ent->groundentity && client->anim_priority <= ANIM_WAVE)
  957. goto newanim;
  958. if (client->anim_time > level.time)
  959. return;
  960. else if ((client->anim_priority & ANIM_REVERSED) && (ent->s.frame > client->anim_end))
  961. {
  962. if (client->anim_time <= level.time)
  963. {
  964. ent->s.frame--;
  965. client->anim_time = level.time + 10_hz;
  966. }
  967. return;
  968. }
  969. else if (!(client->anim_priority & ANIM_REVERSED) && (ent->s.frame < client->anim_end))
  970. {
  971. // continue an animation
  972. if (client->anim_time <= level.time)
  973. {
  974. ent->s.frame++;
  975. client->anim_time = level.time + 10_hz;
  976. }
  977. return;
  978. }
  979. if (client->anim_priority == ANIM_DEATH)
  980. return; // stay there
  981. if (client->anim_priority == ANIM_JUMP)
  982. {
  983. if (!ent->groundentity)
  984. return; // stay there
  985. ent->client->anim_priority = ANIM_WAVE;
  986. if (duck)
  987. {
  988. ent->s.frame = FRAME_jump6;
  989. ent->client->anim_end = FRAME_jump4;
  990. ent->client->anim_priority |= ANIM_REVERSED;
  991. }
  992. else
  993. {
  994. ent->s.frame = FRAME_jump3;
  995. ent->client->anim_end = FRAME_jump6;
  996. }
  997. ent->client->anim_time = level.time + 10_hz;
  998. return;
  999. }
  1000. newanim:
  1001. // return to either a running or standing frame
  1002. client->anim_priority = ANIM_BASIC;
  1003. client->anim_duck = duck;
  1004. client->anim_run = run;
  1005. client->anim_time = level.time + 10_hz;
  1006. if (!ent->groundentity)
  1007. {
  1008. // ZOID: if on grapple, don't go into jump frame, go into standing
  1009. // frame
  1010. if (client->ctf_grapple)
  1011. {
  1012. if (duck)
  1013. {
  1014. ent->s.frame = FRAME_crstnd01;
  1015. client->anim_end = FRAME_crstnd19;
  1016. }
  1017. else
  1018. {
  1019. ent->s.frame = FRAME_stand01;
  1020. client->anim_end = FRAME_stand40;
  1021. }
  1022. }
  1023. else
  1024. {
  1025. // ZOID
  1026. client->anim_priority = ANIM_JUMP;
  1027. if (duck)
  1028. {
  1029. if (ent->s.frame != FRAME_crwalk2)
  1030. ent->s.frame = FRAME_crwalk1;
  1031. client->anim_end = FRAME_crwalk2;
  1032. }
  1033. else
  1034. {
  1035. if (ent->s.frame != FRAME_jump2)
  1036. ent->s.frame = FRAME_jump1;
  1037. client->anim_end = FRAME_jump2;
  1038. }
  1039. }
  1040. }
  1041. else if (run)
  1042. { // running
  1043. if (duck)
  1044. {
  1045. ent->s.frame = FRAME_crwalk1;
  1046. client->anim_end = FRAME_crwalk6;
  1047. }
  1048. else
  1049. {
  1050. ent->s.frame = FRAME_run1;
  1051. client->anim_end = FRAME_run6;
  1052. }
  1053. }
  1054. else
  1055. { // standing
  1056. if (duck)
  1057. {
  1058. ent->s.frame = FRAME_crstnd01;
  1059. client->anim_end = FRAME_crstnd19;
  1060. }
  1061. else
  1062. {
  1063. ent->s.frame = FRAME_stand01;
  1064. client->anim_end = FRAME_stand40;
  1065. }
  1066. }
  1067. }
  1068. // [Paril-KEX]
  1069. static void P_RunMegaHealth(edict_t *ent)
  1070. {
  1071. if (!ent->client->pers.megahealth_time)
  1072. return;
  1073. else if (ent->health <= ent->max_health)
  1074. {
  1075. ent->client->pers.megahealth_time = 0_ms;
  1076. return;
  1077. }
  1078. ent->client->pers.megahealth_time -= FRAME_TIME_S;
  1079. if (ent->client->pers.megahealth_time <= 0_ms)
  1080. {
  1081. ent->health--;
  1082. if (ent->health > ent->max_health)
  1083. ent->client->pers.megahealth_time = 1000_ms;
  1084. else
  1085. ent->client->pers.megahealth_time = 0_ms;
  1086. }
  1087. }
  1088. // [Paril-KEX] push all players' origins back to match their lag compensation
  1089. void G_LagCompensate(edict_t *from_player, const vec3_t &start, const vec3_t &dir)
  1090. {
  1091. uint32_t current_frame = gi.ServerFrame();
  1092. // if you need this to fight monsters, you need help
  1093. if (!deathmatch->integer)
  1094. return;
  1095. else if (!g_lag_compensation->integer)
  1096. return;
  1097. // don't need this
  1098. else if (from_player->client->cmd.server_frame >= current_frame ||
  1099. (from_player->svflags & SVF_BOT))
  1100. return;
  1101. int32_t frame_delta = (current_frame - from_player->client->cmd.server_frame) + 1;
  1102. for (auto player : active_players())
  1103. {
  1104. // we aren't gonna hit ourselves
  1105. if (player == from_player)
  1106. continue;
  1107. // not enough data, spare them
  1108. if (player->client->num_lag_origins < frame_delta)
  1109. continue;
  1110. // if they're way outside of cone of vision, they won't be captured in this
  1111. if ((player->s.origin - start).normalized().dot(dir) < 0.75f)
  1112. continue;
  1113. int32_t lag_id = (player->client->next_lag_origin - 1) - (frame_delta - 1);
  1114. if (lag_id < 0)
  1115. lag_id = game.max_lag_origins + lag_id;
  1116. if (lag_id < 0 || lag_id >= player->client->num_lag_origins)
  1117. {
  1118. gi.Com_Print("lag compensation error\n");
  1119. G_UnLagCompensate();
  1120. return;
  1121. }
  1122. const vec3_t &lag_origin = (game.lag_origins + ((player->s.number - 1) * game.max_lag_origins))[lag_id];
  1123. // no way they'd be hit if they aren't in the PVS
  1124. if (!gi.inPVS(lag_origin, start, false))
  1125. continue;
  1126. // only back up once
  1127. if (!player->client->is_lag_compensated)
  1128. {
  1129. player->client->is_lag_compensated = true;
  1130. player->client->lag_restore_origin = player->s.origin;
  1131. }
  1132. player->s.origin = lag_origin;
  1133. gi.linkentity(player);
  1134. }
  1135. }
  1136. // [Paril-KEX] pop everybody's lag compensation values
  1137. void G_UnLagCompensate()
  1138. {
  1139. for (auto player : active_players())
  1140. {
  1141. if (player->client->is_lag_compensated)
  1142. {
  1143. player->client->is_lag_compensated = false;
  1144. player->s.origin = player->client->lag_restore_origin;
  1145. gi.linkentity(player);
  1146. }
  1147. }
  1148. }
  1149. // [Paril-KEX] save the current lag compensation value
  1150. static void G_SaveLagCompensation(edict_t *ent)
  1151. {
  1152. (game.lag_origins + ((ent->s.number - 1) * game.max_lag_origins))[ent->client->next_lag_origin] = ent->s.origin;
  1153. ent->client->next_lag_origin = (ent->client->next_lag_origin + 1) % game.max_lag_origins;
  1154. if (ent->client->num_lag_origins < game.max_lag_origins)
  1155. ent->client->num_lag_origins++;
  1156. }
  1157. /*
  1158. =================
  1159. ClientEndServerFrame
  1160. Called for each player at the end of the server frame
  1161. and right after spawning
  1162. =================
  1163. */
  1164. void ClientEndServerFrame(edict_t *ent)
  1165. {
  1166. // no player exists yet (load game)
  1167. if (!ent->client->pers.spawned)
  1168. return;
  1169. float bobtime, bobtime_run;
  1170. current_player = ent;
  1171. current_client = ent->client;
  1172. // check fog changes
  1173. P_ForceFogTransition(ent, false);
  1174. // check goals
  1175. G_PlayerNotifyGoal(ent);
  1176. // mega health
  1177. P_RunMegaHealth(ent);
  1178. //
  1179. // If the origin or velocity have changed since ClientThink(),
  1180. // update the pmove values. This will happen when the client
  1181. // is pushed by a bmodel or kicked by an explosion.
  1182. //
  1183. // If it wasn't updated here, the view position would lag a frame
  1184. // behind the body position when pushed -- "sinking into plats"
  1185. //
  1186. current_client->ps.pmove.origin = ent->s.origin;
  1187. current_client->ps.pmove.velocity = ent->velocity;
  1188. //
  1189. // If the end of unit layout is displayed, don't give
  1190. // the player any normal movement attributes
  1191. //
  1192. if (level.intermissiontime || ent->client->awaiting_respawn)
  1193. {
  1194. if (ent->client->awaiting_respawn || (level.intermission_eou || level.is_n64 || (deathmatch->integer && level.intermissiontime)))
  1195. {
  1196. current_client->ps.screen_blend[3] = current_client->ps.damage_blend[3] = 0;
  1197. current_client->ps.fov = 90;
  1198. current_client->ps.gunindex = 0;
  1199. }
  1200. G_SetStats(ent);
  1201. G_SetCoopStats(ent);
  1202. // if the scoreboard is up, update it if a client leaves
  1203. if (deathmatch->integer && ent->client->showscores && ent->client->menutime)
  1204. {
  1205. DeathmatchScoreboardMessage(ent, ent->enemy);
  1206. gi.unicast(ent, false);
  1207. ent->client->menutime = 0_ms;
  1208. }
  1209. return;
  1210. }
  1211. // ZOID
  1212. // regen tech
  1213. CTFApplyRegeneration(ent);
  1214. // ZOID
  1215. AngleVectors(ent->client->v_angle, forward, right, up);
  1216. // burn from lava, etc
  1217. P_WorldEffects();
  1218. //
  1219. // set model angles from view angles so other things in
  1220. // the world can tell which direction you are looking
  1221. //
  1222. if (ent->client->v_angle[PITCH] > 180)
  1223. ent->s.angles[PITCH] = (-360 + ent->client->v_angle[PITCH]) / 3;
  1224. else
  1225. ent->s.angles[PITCH] = ent->client->v_angle[PITCH] / 3;
  1226. ent->s.angles[YAW] = ent->client->v_angle[YAW];
  1227. ent->s.angles[ROLL] = 0;
  1228. // [Paril-KEX] cl_rollhack
  1229. ent->s.angles[ROLL] = -SV_CalcRoll(ent->s.angles, ent->velocity) * 4;
  1230. //
  1231. // calculate speed and cycle to be used for
  1232. // all cyclic walking effects
  1233. //
  1234. xyspeed = sqrt(ent->velocity[0] * ent->velocity[0] + ent->velocity[1] * ent->velocity[1]);
  1235. if (xyspeed < 5)
  1236. {
  1237. bobmove = 0;
  1238. current_client->bobtime = 0; // start at beginning of cycle again
  1239. }
  1240. else if (ent->groundentity)
  1241. { // so bobbing only cycles when on ground
  1242. if (xyspeed > 210)
  1243. bobmove = gi.frame_time_ms / 400.f;
  1244. else if (xyspeed > 100)
  1245. bobmove = gi.frame_time_ms / 800.f;
  1246. else
  1247. bobmove = gi.frame_time_ms / 1600.f;
  1248. }
  1249. bobtime = (current_client->bobtime += bobmove);
  1250. bobtime_run = bobtime;
  1251. if ((current_client->ps.pmove.pm_flags & PMF_DUCKED) && ent->groundentity)
  1252. bobtime *= 4;
  1253. bobcycle = (int) bobtime;
  1254. bobcycle_run = (int) bobtime_run;
  1255. bobfracsin = fabsf(sinf(bobtime * PIf));
  1256. // apply all the damage taken this frame
  1257. P_DamageFeedback(ent);
  1258. // determine the view offsets
  1259. SV_CalcViewOffset(ent);
  1260. // determine the gun offsets
  1261. SV_CalcGunOffset(ent);
  1262. // determine the full screen color blend
  1263. // must be after viewoffset, so eye contents can be
  1264. // accurately determined
  1265. SV_CalcBlend(ent);
  1266. // chase cam stuff
  1267. if (ent->client->resp.spectator)
  1268. G_SetSpectatorStats(ent);
  1269. else
  1270. G_SetStats(ent);
  1271. G_CheckChaseStats(ent);
  1272. G_SetCoopStats(ent);
  1273. G_SetClientEvent(ent);
  1274. G_SetClientEffects(ent);
  1275. G_SetClientSound(ent);
  1276. G_SetClientFrame(ent);
  1277. ent->client->oldvelocity = ent->velocity;
  1278. ent->client->oldviewangles = ent->client->ps.viewangles;
  1279. ent->client->oldgroundentity = ent->groundentity;
  1280. // ZOID
  1281. if (ent->client->menudirty && ent->client->menutime <= level.time)
  1282. {
  1283. if (ent->client->menu)
  1284. {
  1285. PMenu_Do_Update(ent);
  1286. gi.unicast(ent, true);
  1287. }
  1288. ent->client->menutime = level.time;
  1289. ent->client->menudirty = false;
  1290. }
  1291. // ZOID
  1292. // if the scoreboard is up, update it
  1293. if (ent->client->showscores && ent->client->menutime <= level.time)
  1294. {
  1295. // ZOID
  1296. if (ent->client->menu)
  1297. {
  1298. PMenu_Do_Update(ent);
  1299. ent->client->menudirty = false;
  1300. }
  1301. else
  1302. // ZOID
  1303. DeathmatchScoreboardMessage(ent, ent->enemy);
  1304. gi.unicast(ent, false);
  1305. ent->client->menutime = level.time + 3_sec;
  1306. }
  1307. if ( ( ent->svflags & SVF_BOT ) != 0 ) {
  1308. Bot_EndFrame( ent );
  1309. }
  1310. P_AssignClientSkinnum(ent);
  1311. if (deathmatch->integer)
  1312. G_SaveLagCompensation(ent);
  1313. Compass_Update(ent, false);
  1314. // [Paril-KEX] in coop, if player collision is enabled and
  1315. // we are currently in no-player-collision mode, check if
  1316. // it's safe.
  1317. if (coop->integer && G_ShouldPlayersCollide(false) && !(ent->clipmask & CONTENTS_PLAYER) && ent->takedamage)
  1318. {
  1319. bool clipped_player = false;
  1320. for (auto player : active_players())
  1321. {
  1322. if (player == ent)
  1323. continue;
  1324. trace_t clip = gi.clip(player, ent->s.origin, ent->mins, ent->maxs, ent->s.origin, CONTENTS_MONSTER | CONTENTS_PLAYER);
  1325. if (clip.startsolid || clip.allsolid)
  1326. {
  1327. clipped_player = true;
  1328. break;
  1329. }
  1330. }
  1331. // safe!
  1332. if (!clipped_player)
  1333. ent->clipmask |= CONTENTS_PLAYER;
  1334. }
  1335. }