p_hud.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. // Copyright (c) ZeniMax Media Inc.
  2. // Licensed under the GNU General Public License 2.0.
  3. #include "g_local.h"
  4. #include "g_statusbar.h"
  5. /*
  6. ======================================================================
  7. INTERMISSION
  8. ======================================================================
  9. */
  10. void DeathmatchScoreboard(edict_t *ent);
  11. void MoveClientToIntermission(edict_t *ent)
  12. {
  13. // [Paril-KEX]
  14. if (ent->client->ps.pmove.pm_type != PM_FREEZE)
  15. ent->s.event = EV_OTHER_TELEPORT;
  16. if (deathmatch->integer)
  17. ent->client->showscores = true;
  18. ent->s.origin = level.intermission_origin;
  19. ent->client->ps.pmove.origin = level.intermission_origin;
  20. ent->client->ps.viewangles = level.intermission_angle;
  21. ent->client->ps.pmove.pm_type = PM_FREEZE;
  22. ent->client->ps.gunindex = 0;
  23. ent->client->ps.gunskin = 0;
  24. ent->client->ps.damage_blend[3] = ent->client->ps.screen_blend[3] = 0;
  25. ent->client->ps.rdflags = RDF_NONE;
  26. // clean up powerup info
  27. ent->client->quad_time = 0_ms;
  28. ent->client->invincible_time = 0_ms;
  29. ent->client->breather_time = 0_ms;
  30. ent->client->enviro_time = 0_ms;
  31. ent->client->invisible_time = 0_ms;
  32. ent->client->grenade_blew_up = false;
  33. ent->client->grenade_time = 0_ms;
  34. ent->client->showhelp = false;
  35. ent->client->showscores = false;
  36. globals.server_flags &= ~SERVER_FLAG_SLOW_TIME;
  37. // RAFAEL
  38. ent->client->quadfire_time = 0_ms;
  39. // RAFAEL
  40. // ROGUE
  41. ent->client->ir_time = 0_ms;
  42. ent->client->nuke_time = 0_ms;
  43. ent->client->double_time = 0_ms;
  44. ent->client->tracker_pain_time = 0_ms;
  45. // ROGUE
  46. ent->viewheight = 0;
  47. ent->s.modelindex = 0;
  48. ent->s.modelindex2 = 0;
  49. ent->s.modelindex3 = 0;
  50. ent->s.modelindex = 0;
  51. ent->s.effects = EF_NONE;
  52. ent->s.sound = 0;
  53. ent->solid = SOLID_NOT;
  54. ent->movetype = MOVETYPE_NOCLIP;
  55. gi.linkentity(ent);
  56. // add the layout
  57. if (deathmatch->integer)
  58. {
  59. DeathmatchScoreboard(ent);
  60. ent->client->showscores = true;
  61. }
  62. }
  63. // [Paril-KEX] update the level entry for end-of-unit screen
  64. void G_UpdateLevelEntry()
  65. {
  66. if (!level.entry)
  67. return;
  68. level.entry->found_secrets = level.found_secrets;
  69. level.entry->total_secrets = level.total_secrets;
  70. level.entry->killed_monsters = level.killed_monsters;
  71. level.entry->total_monsters = level.total_monsters;
  72. }
  73. inline void G_EndOfUnitEntry(std::stringstream &layout, const int &y, const level_entry_t &entry)
  74. {
  75. layout << G_Fmt("yv {} ", y);
  76. // we didn't visit this level, so print it as an unknown entry
  77. if (!*entry.pretty_name)
  78. {
  79. layout << "table_row 1 ??? ";
  80. return;
  81. }
  82. layout << G_Fmt("table_row 4 \"{}\" ", entry.pretty_name) <<
  83. G_Fmt("{}/{} ", entry.killed_monsters, entry.total_monsters) <<
  84. G_Fmt("{}/{} ", entry.found_secrets, entry.total_secrets);
  85. int32_t minutes = entry.time.milliseconds() / 60000;
  86. int32_t seconds = (entry.time.milliseconds() / 1000) % 60;
  87. int32_t milliseconds = entry.time.milliseconds() % 1000;
  88. layout << G_Fmt("{:02}:{:02}:{:03} ", minutes, seconds, milliseconds);
  89. }
  90. void G_EndOfUnitMessage()
  91. {
  92. // [Paril-KEX] update game level entry
  93. G_UpdateLevelEntry();
  94. std::stringstream layout;
  95. // sort entries
  96. std::sort(game.level_entries.begin(), game.level_entries.end(), [](const level_entry_t &a, const level_entry_t &b) {
  97. int32_t a_order = a.visit_order ? a.visit_order : (*a.pretty_name ? (MAX_LEVELS_PER_UNIT + 1) : (MAX_LEVELS_PER_UNIT + 2));
  98. int32_t b_order = b.visit_order ? b.visit_order : (*b.pretty_name ? (MAX_LEVELS_PER_UNIT + 1) : (MAX_LEVELS_PER_UNIT + 2));
  99. return a_order < b_order;
  100. });
  101. layout << "start_table 4 $m_eou_level $m_eou_kills $m_eou_secrets $m_eou_time ";
  102. int y = 16;
  103. level_entry_t totals {};
  104. int32_t num_rows = 0;
  105. for (auto &entry : game.level_entries)
  106. {
  107. if (!*entry.map_name)
  108. break;
  109. G_EndOfUnitEntry(layout, y, entry);
  110. y += 8;
  111. totals.found_secrets += entry.found_secrets;
  112. totals.killed_monsters += entry.killed_monsters;
  113. totals.time += entry.time;
  114. totals.total_monsters += entry.total_monsters;
  115. totals.total_secrets += entry.total_secrets;
  116. if (entry.visit_order)
  117. num_rows++;
  118. }
  119. y += 8;
  120. // make this a space so it prints totals
  121. if (num_rows > 1)
  122. {
  123. layout << "table_row 0 "; // empty row to separate totals
  124. totals.pretty_name[0] = ' ';
  125. G_EndOfUnitEntry(layout, y, totals);
  126. }
  127. layout << "xv 160 yt 0 draw_table ";
  128. layout << "ifgef " << (level.intermission_server_frame + (5_sec).frames()) << " yb -48 xv 0 loc_cstring2 0 \"$m_eou_press_button\" endif ";
  129. gi.WriteByte(svc_layout);
  130. gi.WriteString(layout.str().c_str());
  131. gi.multicast(vec3_origin, MULTICAST_ALL, true);
  132. for (auto player : active_players())
  133. player->client->showeou = true;
  134. }
  135. // data is binary now.
  136. // u8 num_teams
  137. // u8 num_players
  138. // [ repeat num_teams:
  139. // string team_name
  140. // ]
  141. // [ repeat num_players:
  142. // u8 client_index
  143. // s32 score
  144. // u8 ranking
  145. // (if num_teams > 0)
  146. // u8 team
  147. // ]
  148. void G_ReportMatchDetails(bool is_end)
  149. {
  150. static std::array<uint32_t, MAX_CLIENTS> player_ranks;
  151. player_ranks = {};
  152. // CTF/TDM is simple
  153. if (ctf->integer || teamplay->integer)
  154. {
  155. CTFCalcRankings(player_ranks);
  156. gi.WriteByte(2);
  157. gi.WriteString("RED TEAM"); // team 0
  158. gi.WriteString("BLUE TEAM"); // team 1
  159. }
  160. else
  161. {
  162. // sort players by score, then match everybody to
  163. // the current highest score downwards until we run out of players.
  164. static std::array<edict_t *, MAX_CLIENTS> sorted_players;
  165. size_t num_active_players = 0;
  166. for (auto player : active_players())
  167. sorted_players[num_active_players++] = player;
  168. std::sort(sorted_players.begin(), sorted_players.begin() + num_active_players, [](const edict_t *a, const edict_t *b) { return b->client->resp.score < a->client->resp.score; });
  169. int32_t current_score = INT_MIN;
  170. int32_t current_rank = 0;
  171. for (size_t i = 0; i < num_active_players; i++)
  172. {
  173. if (!current_rank || sorted_players[i]->client->resp.score != current_score)
  174. {
  175. current_rank++;
  176. current_score = sorted_players[i]->client->resp.score;
  177. }
  178. player_ranks[sorted_players[i]->s.number - 1] = current_rank;
  179. }
  180. gi.WriteByte(0);
  181. }
  182. uint8_t num_players = 0;
  183. for (auto player : active_players())
  184. {
  185. // leave spectators out of this data, they don't need to be seen.
  186. if (player->client->pers.spawned && !player->client->resp.spectator)
  187. {
  188. // just in case...
  189. if (G_TeamplayEnabled() && player->client->resp.ctf_team == CTF_NOTEAM)
  190. continue;
  191. num_players++;
  192. }
  193. }
  194. gi.WriteByte(num_players);
  195. for (auto player : active_players())
  196. {
  197. // leave spectators out of this data, they don't need to be seen.
  198. if (player->client->pers.spawned && !player->client->resp.spectator)
  199. {
  200. // just in case...
  201. if (G_TeamplayEnabled() && player->client->resp.ctf_team == CTF_NOTEAM)
  202. continue;
  203. gi.WriteByte(player->s.number - 1);
  204. gi.WriteLong(player->client->resp.score);
  205. gi.WriteByte(player_ranks[player->s.number - 1]);
  206. if (G_TeamplayEnabled())
  207. gi.WriteByte(player->client->resp.ctf_team == CTF_TEAM1 ? 0 : 1);
  208. }
  209. }
  210. gi.ReportMatchDetails_Multicast(is_end);
  211. }
  212. void BeginIntermission(edict_t *targ)
  213. {
  214. edict_t *ent, *client;
  215. if (level.intermissiontime)
  216. return; // already activated
  217. // ZOID
  218. if (ctf->integer)
  219. CTFCalcScores();
  220. // ZOID
  221. game.autosaved = false;
  222. level.intermissiontime = level.time;
  223. // respawn any dead clients
  224. for (uint32_t i = 0; i < game.maxclients; i++)
  225. {
  226. client = g_edicts + 1 + i;
  227. if (!client->inuse)
  228. continue;
  229. if (client->health <= 0)
  230. {
  231. // give us our max health back since it will reset
  232. // to pers.health; in instanced items we'd lose the items
  233. // we touched so we always want to respawn with our max.
  234. if (P_UseCoopInstancedItems())
  235. client->client->pers.health = client->client->pers.max_health = client->max_health;
  236. respawn(client);
  237. }
  238. }
  239. level.intermission_server_frame = gi.ServerFrame();
  240. level.changemap = targ->map;
  241. level.intermission_clear = targ->spawnflags.has(SPAWNFLAG_CHANGELEVEL_CLEAR_INVENTORY);
  242. level.intermission_eou = false;
  243. level.intermission_fade = targ->spawnflags.has(SPAWNFLAG_CHANGELEVEL_FADE_OUT);
  244. // destroy all player trails
  245. PlayerTrail_Destroy(nullptr);
  246. // [Paril-KEX] update game level entry
  247. G_UpdateLevelEntry();
  248. if (strstr(level.changemap, "*"))
  249. {
  250. if (coop->integer)
  251. {
  252. for (uint32_t i = 0; i < game.maxclients; i++)
  253. {
  254. client = g_edicts + 1 + i;
  255. if (!client->inuse)
  256. continue;
  257. // strip players of all keys between units
  258. for (uint32_t n = 0; n < IT_TOTAL; n++)
  259. if (itemlist[n].flags & IF_KEY)
  260. client->client->pers.inventory[n] = 0;
  261. }
  262. }
  263. if (level.achievement && level.achievement[0])
  264. {
  265. gi.WriteByte(svc_achievement);
  266. gi.WriteString(level.achievement);
  267. gi.multicast(vec3_origin, MULTICAST_ALL, true);
  268. }
  269. level.intermission_eou = true;
  270. // "no end of unit" maps handle intermission differently
  271. if (!targ->spawnflags.has(SPAWNFLAG_CHANGELEVEL_NO_END_OF_UNIT))
  272. G_EndOfUnitMessage();
  273. else if (targ->spawnflags.has(SPAWNFLAG_CHANGELEVEL_IMMEDIATE_LEAVE) && !deathmatch->integer)
  274. {
  275. // Need to call this now
  276. G_ReportMatchDetails(true);
  277. level.exitintermission = 1; // go immediately to the next level
  278. return;
  279. }
  280. }
  281. else
  282. {
  283. if (!deathmatch->integer)
  284. {
  285. level.exitintermission = 1; // go immediately to the next level
  286. return;
  287. }
  288. }
  289. // Call while intermission is running
  290. G_ReportMatchDetails(true);
  291. level.exitintermission = 0;
  292. if (!level.level_intermission_set)
  293. {
  294. // find an intermission spot
  295. ent = G_FindByString<&edict_t::classname>(nullptr, "info_player_intermission");
  296. if (!ent)
  297. { // the map creator forgot to put in an intermission point...
  298. ent = G_FindByString<&edict_t::classname>(nullptr, "info_player_start");
  299. if (!ent)
  300. ent = G_FindByString<&edict_t::classname>(nullptr, "info_player_deathmatch");
  301. }
  302. else
  303. { // choose one of four spots
  304. int32_t i = irandom(4);
  305. while (i--)
  306. {
  307. ent = G_FindByString<&edict_t::classname>(ent, "info_player_intermission");
  308. if (!ent) // wrap around the list
  309. ent = G_FindByString<&edict_t::classname>(ent, "info_player_intermission");
  310. }
  311. }
  312. level.intermission_origin = ent->s.origin;
  313. level.intermission_angle = ent->s.angles;
  314. }
  315. // move all clients to the intermission point
  316. for (uint32_t i = 0; i < game.maxclients; i++)
  317. {
  318. client = g_edicts + 1 + i;
  319. if (!client->inuse)
  320. continue;
  321. MoveClientToIntermission(client);
  322. }
  323. }
  324. constexpr size_t MAX_SCOREBOARD_SIZE = 1024;
  325. /*
  326. ==================
  327. DeathmatchScoreboardMessage
  328. ==================
  329. */
  330. void DeathmatchScoreboardMessage(edict_t *ent, edict_t *killer)
  331. {
  332. static std::string entry, string;
  333. size_t j;
  334. int sorted[MAX_CLIENTS];
  335. int sortedscores[MAX_CLIENTS];
  336. int score;
  337. int x, y;
  338. gclient_t *cl;
  339. edict_t *cl_ent;
  340. const char *tag;
  341. // ZOID
  342. if (G_TeamplayEnabled())
  343. {
  344. CTFScoreboardMessage(ent, killer);
  345. return;
  346. }
  347. // ZOID
  348. entry.clear();
  349. string.clear();
  350. // sort the clients by score
  351. uint32_t total = 0;
  352. for (uint32_t i = 0; i < game.maxclients; i++)
  353. {
  354. cl_ent = g_edicts + 1 + i;
  355. if (!cl_ent->inuse || game.clients[i].resp.spectator)
  356. continue;
  357. score = game.clients[i].resp.score;
  358. for (j = 0; j < total; j++)
  359. {
  360. if (score > sortedscores[j])
  361. break;
  362. }
  363. for (uint32_t k = total; k > j; k--)
  364. {
  365. sorted[k] = sorted[k - 1];
  366. sortedscores[k] = sortedscores[k - 1];
  367. }
  368. sorted[j] = i;
  369. sortedscores[j] = score;
  370. total++;
  371. }
  372. // add the clients in sorted order
  373. if (total > 16)
  374. total = 16;
  375. for (uint32_t i = 0; i < total; i++)
  376. {
  377. cl = &game.clients[sorted[i]];
  378. cl_ent = g_edicts + 1 + sorted[i];
  379. x = (i >= 8) ? 130 : -72;
  380. y = 0 + 32 * (i % 8);
  381. // add a dogtag
  382. // [Paril-KEX] use dynamic dogtags
  383. tag = nullptr;
  384. //===============
  385. // ROGUE
  386. // allow new DM games to override the tag picture
  387. if (gamerules->integer)
  388. {
  389. if (DMGame.DogTag)
  390. DMGame.DogTag(cl_ent, killer, &tag);
  391. }
  392. // ROGUE
  393. //===============
  394. if (tag)
  395. {
  396. fmt::format_to(std::back_inserter(entry), FMT_STRING("xv {} yv {} picn {} "), x + 32, y, tag);
  397. if (string.length() + entry.length() > MAX_SCOREBOARD_SIZE)
  398. break;
  399. string += entry;
  400. }
  401. else
  402. {
  403. fmt::format_to(std::back_inserter(entry), FMT_STRING("xv {} yv {} dogtag {} "), x + 32, y, sorted[i]);
  404. if (string.length() + entry.length() > MAX_SCOREBOARD_SIZE)
  405. break;
  406. string += entry;
  407. }
  408. entry.clear();
  409. fmt::format_to(std::back_inserter(entry),
  410. FMT_STRING("client {} {} {} {} {} {} "),
  411. x, y, sorted[i], cl->resp.score, cl->ping, (int32_t) (level.time - cl->resp.entertime).minutes());
  412. if (string.length() + entry.length() > MAX_SCOREBOARD_SIZE)
  413. break;
  414. string += entry;
  415. entry.clear();
  416. }
  417. // [Paril-KEX] time & frags
  418. if (fraglimit->integer)
  419. {
  420. fmt::format_to(std::back_inserter(string), FMT_STRING("xv -20 yv -10 loc_string2 1 $g_score_frags \"{}\" "), fraglimit->integer);
  421. }
  422. if (timelimit->value && !level.intermissiontime)
  423. {
  424. fmt::format_to(std::back_inserter(string), FMT_STRING("xv 340 yv -10 time_limit {} "), gi.ServerFrame() + ((gtime_t::from_min(timelimit->value) - level.time)).milliseconds() / gi.frame_time_ms);
  425. }
  426. if (level.intermissiontime)
  427. fmt::format_to(std::back_inserter(string), FMT_STRING("ifgef {} yb -48 xv 0 loc_cstring2 0 \"$m_eou_press_button\" endif "), (level.intermission_server_frame + (5_sec).frames()));
  428. gi.WriteByte(svc_layout);
  429. gi.WriteString(string.c_str());
  430. }
  431. /*
  432. ==================
  433. DeathmatchScoreboard
  434. Draw instead of help message.
  435. Note that it isn't that hard to overflow the 1400 byte message limit!
  436. ==================
  437. */
  438. void DeathmatchScoreboard(edict_t *ent)
  439. {
  440. DeathmatchScoreboardMessage(ent, ent->enemy);
  441. gi.unicast(ent, true);
  442. ent->client->menutime = level.time + 3_sec;
  443. }
  444. /*
  445. ==================
  446. Cmd_Score_f
  447. Display the scoreboard
  448. ==================
  449. */
  450. void Cmd_Score_f(edict_t *ent)
  451. {
  452. if (level.intermissiontime)
  453. return;
  454. ent->client->showinventory = false;
  455. ent->client->showhelp = false;
  456. globals.server_flags &= ~SERVER_FLAG_SLOW_TIME;
  457. // ZOID
  458. if (ent->client->menu)
  459. PMenu_Close(ent);
  460. // ZOID
  461. if (!deathmatch->integer && !coop->integer)
  462. return;
  463. if (ent->client->showscores)
  464. {
  465. ent->client->showscores = false;
  466. ent->client->update_chase = true;
  467. return;
  468. }
  469. ent->client->showscores = true;
  470. DeathmatchScoreboard(ent);
  471. }
  472. /*
  473. ==================
  474. HelpComputer
  475. Draw help computer.
  476. ==================
  477. */
  478. void HelpComputer(edict_t *ent)
  479. {
  480. const char *sk;
  481. if (skill->integer == 0)
  482. sk = "$m_easy";
  483. else if (skill->integer == 1)
  484. sk = "$m_medium";
  485. else if (skill->integer == 2)
  486. sk = "$m_hard";
  487. else
  488. sk = "$m_nightmare";
  489. // send the layout
  490. std::string helpString = "";
  491. helpString += G_Fmt(
  492. "xv 32 yv 8 picn help " // background
  493. "xv 0 yv 25 cstring2 \"{}\" ", // level name
  494. level.level_name);
  495. if (level.is_n64)
  496. {
  497. helpString += G_Fmt("xv 0 yv 54 loc_cstring 1 \"{{}}\" \"{}\" ", // help 1
  498. game.helpmessage1);
  499. }
  500. else
  501. {
  502. int y = 54;
  503. if (strlen(game.helpmessage1))
  504. {
  505. helpString += G_Fmt("xv 0 yv {} loc_cstring2 0 \"$g_pc_primary_objective\" " // title
  506. "xv 0 yv {} loc_cstring 0 \"{}\" ",
  507. y,
  508. y + 11,
  509. game.helpmessage1);
  510. y += 58;
  511. }
  512. if (strlen(game.helpmessage2))
  513. {
  514. helpString += G_Fmt("xv 0 yv {} loc_cstring2 0 \"$g_pc_secondary_objective\" " // title
  515. "xv 0 yv {} loc_cstring 0 \"{}\" ",
  516. y,
  517. y + 11,
  518. game.helpmessage2);
  519. }
  520. }
  521. helpString += G_Fmt("xv 55 yv 164 loc_string2 0 \"{}\" "
  522. "xv 265 yv 164 loc_rstring2 1 \"{{}}: {}/{}\" \"$g_pc_goals\" "
  523. "xv 55 yv 172 loc_string2 1 \"{{}}: {}/{}\" \"$g_pc_kills\" "
  524. "xv 265 yv 172 loc_rstring2 1 \"{{}}: {}/{}\" \"$g_pc_secrets\" ",
  525. sk,
  526. level.found_goals, level.total_goals,
  527. level.killed_monsters, level.total_monsters,
  528. level.found_secrets, level.total_secrets);
  529. gi.WriteByte(svc_layout);
  530. gi.WriteString(helpString.c_str());
  531. gi.unicast(ent, true);
  532. }
  533. /*
  534. ==================
  535. Cmd_Help_f
  536. Display the current help message
  537. ==================
  538. */
  539. void Cmd_Help_f(edict_t *ent)
  540. {
  541. // this is for backwards compatability
  542. if (deathmatch->integer)
  543. {
  544. Cmd_Score_f(ent);
  545. return;
  546. }
  547. if (level.intermissiontime)
  548. return;
  549. ent->client->showinventory = false;
  550. ent->client->showscores = false;
  551. if (ent->client->showhelp &&
  552. (ent->client->pers.game_help1changed == game.help1changed ||
  553. ent->client->pers.game_help2changed == game.help2changed))
  554. {
  555. ent->client->showhelp = false;
  556. globals.server_flags &= ~SERVER_FLAG_SLOW_TIME;
  557. return;
  558. }
  559. ent->client->showhelp = true;
  560. ent->client->pers.helpchanged = 0;
  561. globals.server_flags |= SERVER_FLAG_SLOW_TIME;
  562. HelpComputer(ent);
  563. }
  564. //=======================================================================
  565. // [Paril-KEX] for stats we want to always be set in coop
  566. // even if we're spectating
  567. void G_SetCoopStats(edict_t *ent)
  568. {
  569. if (coop->integer && g_coop_enable_lives->integer)
  570. ent->client->ps.stats[STAT_LIVES] = ent->client->pers.lives + 1;
  571. else
  572. ent->client->ps.stats[STAT_LIVES] = 0;
  573. // stat for text on what we're doing for respawn
  574. if (ent->client->coop_respawn_state)
  575. ent->client->ps.stats[STAT_COOP_RESPAWN] = CONFIG_COOP_RESPAWN_STRING + (ent->client->coop_respawn_state - COOP_RESPAWN_IN_COMBAT);
  576. else
  577. ent->client->ps.stats[STAT_COOP_RESPAWN] = 0;
  578. }
  579. struct powerup_info_t
  580. {
  581. item_id_t item;
  582. gtime_t gclient_t::*time_ptr = nullptr;
  583. int32_t gclient_t::*count_ptr = nullptr;
  584. } powerup_table[] = {
  585. { IT_ITEM_QUAD, &gclient_t::quad_time },
  586. { IT_ITEM_QUADFIRE, &gclient_t::quadfire_time },
  587. { IT_ITEM_DOUBLE, &gclient_t::double_time },
  588. { IT_ITEM_INVULNERABILITY, &gclient_t::invincible_time },
  589. { IT_ITEM_INVISIBILITY, &gclient_t::invisible_time },
  590. { IT_ITEM_ENVIROSUIT, &gclient_t::enviro_time },
  591. { IT_ITEM_REBREATHER, &gclient_t::breather_time },
  592. { IT_ITEM_IR_GOGGLES, &gclient_t::ir_time },
  593. { IT_ITEM_SILENCER, nullptr, &gclient_t::silencer_shots }
  594. };
  595. /*
  596. ===============
  597. G_SetStats
  598. ===============
  599. */
  600. void G_SetStats(edict_t *ent)
  601. {
  602. gitem_t *item;
  603. item_id_t index;
  604. int cells = 0;
  605. item_id_t power_armor_type;
  606. unsigned int invIndex;
  607. //
  608. // health
  609. //
  610. if (ent->s.renderfx & RF_USE_DISGUISE)
  611. ent->client->ps.stats[STAT_HEALTH_ICON] = level.disguise_icon;
  612. else
  613. ent->client->ps.stats[STAT_HEALTH_ICON] = level.pic_health;
  614. ent->client->ps.stats[STAT_HEALTH] = ent->health;
  615. //
  616. // weapons
  617. //
  618. uint32_t weaponbits = 0;
  619. for (invIndex = IT_WEAPON_GRAPPLE; invIndex <= IT_WEAPON_DISRUPTOR; invIndex++)
  620. {
  621. if (ent->client->pers.inventory[invIndex])
  622. {
  623. weaponbits |= 1 << GetItemByIndex((item_id_t) invIndex)->weapon_wheel_index;
  624. }
  625. }
  626. ent->client->ps.stats[STAT_WEAPONS_OWNED_1] = (weaponbits & 0xFFFF);
  627. ent->client->ps.stats[STAT_WEAPONS_OWNED_2] = (weaponbits >> 16);
  628. ent->client->ps.stats[STAT_ACTIVE_WHEEL_WEAPON] = (ent->client->newweapon ? ent->client->newweapon->weapon_wheel_index :
  629. ent->client->pers.weapon ? ent->client->pers.weapon->weapon_wheel_index :
  630. -1);
  631. ent->client->ps.stats[STAT_ACTIVE_WEAPON] = ent->client->pers.weapon ? ent->client->pers.weapon->weapon_wheel_index : -1;
  632. //
  633. // ammo
  634. //
  635. ent->client->ps.stats[STAT_AMMO_ICON] = 0;
  636. ent->client->ps.stats[STAT_AMMO] = 0;
  637. if (ent->client->pers.weapon && ent->client->pers.weapon->ammo)
  638. {
  639. item = GetItemByIndex(ent->client->pers.weapon->ammo);
  640. if (!G_CheckInfiniteAmmo(item))
  641. {
  642. ent->client->ps.stats[STAT_AMMO_ICON] = gi.imageindex(item->icon);
  643. ent->client->ps.stats[STAT_AMMO] = ent->client->pers.inventory[ent->client->pers.weapon->ammo];
  644. }
  645. }
  646. memset(&ent->client->ps.stats[STAT_AMMO_INFO_START], 0, sizeof(uint16_t) * NUM_AMMO_STATS);
  647. for (unsigned int ammoIndex = AMMO_BULLETS; ammoIndex < AMMO_MAX; ++ammoIndex)
  648. {
  649. gitem_t *ammo = GetItemByAmmo((ammo_t) ammoIndex);
  650. uint16_t val = G_CheckInfiniteAmmo(ammo) ? AMMO_VALUE_INFINITE : clamp(ent->client->pers.inventory[ammo->id], 0, AMMO_VALUE_INFINITE - 1);
  651. G_SetAmmoStat((uint16_t *) &ent->client->ps.stats[STAT_AMMO_INFO_START], ammo->ammo_wheel_index, val);
  652. }
  653. //
  654. // armor
  655. //
  656. power_armor_type = PowerArmorType(ent);
  657. if (power_armor_type)
  658. cells = ent->client->pers.inventory[IT_AMMO_CELLS];
  659. index = ArmorIndex(ent);
  660. if (power_armor_type && (!index || (level.time.milliseconds() % 3000) < 1500))
  661. { // flash between power armor and other armor icon
  662. ent->client->ps.stats[STAT_ARMOR_ICON] = power_armor_type == IT_ITEM_POWER_SHIELD ? gi.imageindex("i_powershield") : gi.imageindex("i_powerscreen");
  663. ent->client->ps.stats[STAT_ARMOR] = cells;
  664. }
  665. else if (index)
  666. {
  667. item = GetItemByIndex(index);
  668. ent->client->ps.stats[STAT_ARMOR_ICON] = gi.imageindex(item->icon);
  669. ent->client->ps.stats[STAT_ARMOR] = ent->client->pers.inventory[index];
  670. }
  671. else
  672. {
  673. ent->client->ps.stats[STAT_ARMOR_ICON] = 0;
  674. ent->client->ps.stats[STAT_ARMOR] = 0;
  675. }
  676. //
  677. // pickup message
  678. //
  679. if (level.time > ent->client->pickup_msg_time)
  680. {
  681. ent->client->ps.stats[STAT_PICKUP_ICON] = 0;
  682. ent->client->ps.stats[STAT_PICKUP_STRING] = 0;
  683. }
  684. // owned powerups
  685. memset(&ent->client->ps.stats[STAT_POWERUP_INFO_START], 0, sizeof(uint16_t) * NUM_POWERUP_STATS);
  686. for (unsigned int powerupIndex = POWERUP_SCREEN; powerupIndex < POWERUP_MAX; ++powerupIndex)
  687. {
  688. gitem_t *powerup = GetItemByPowerup((powerup_t) powerupIndex);
  689. uint16_t val;
  690. switch (powerup->id)
  691. {
  692. case IT_ITEM_POWER_SCREEN:
  693. case IT_ITEM_POWER_SHIELD:
  694. if (!ent->client->pers.inventory[powerup->id])
  695. val = 0;
  696. else if (ent->flags & FL_POWER_ARMOR)
  697. val = 2;
  698. else
  699. val = 1;
  700. break;
  701. case IT_ITEM_FLASHLIGHT:
  702. if (!ent->client->pers.inventory[powerup->id])
  703. val = 0;
  704. else if (ent->flags & FL_FLASHLIGHT)
  705. val = 2;
  706. else
  707. val = 1;
  708. break;
  709. default:
  710. val = clamp(ent->client->pers.inventory[powerup->id], 0, 3);
  711. break;
  712. }
  713. G_SetPowerupStat((uint16_t *) &ent->client->ps.stats[STAT_POWERUP_INFO_START], powerup->powerup_wheel_index, val);
  714. }
  715. ent->client->ps.stats[STAT_TIMER_ICON] = 0;
  716. ent->client->ps.stats[STAT_TIMER] = 0;
  717. //
  718. // timers
  719. //
  720. // PGM
  721. if (ent->client->owned_sphere)
  722. {
  723. if (ent->client->owned_sphere->spawnflags == SPHERE_DEFENDER) // defender
  724. ent->client->ps.stats[STAT_TIMER_ICON] = gi.imageindex("p_defender");
  725. else if (ent->client->owned_sphere->spawnflags == SPHERE_HUNTER) // hunter
  726. ent->client->ps.stats[STAT_TIMER_ICON] = gi.imageindex("p_hunter");
  727. else if (ent->client->owned_sphere->spawnflags == SPHERE_VENGEANCE) // vengeance
  728. ent->client->ps.stats[STAT_TIMER_ICON] = gi.imageindex("p_vengeance");
  729. else // error case
  730. ent->client->ps.stats[STAT_TIMER_ICON] = gi.imageindex("i_fixme");
  731. ent->client->ps.stats[STAT_TIMER] = ceil(ent->client->owned_sphere->wait - level.time.seconds());
  732. }
  733. else
  734. {
  735. powerup_info_t *best_powerup = nullptr;
  736. for (auto &powerup : powerup_table)
  737. {
  738. auto *powerup_time = powerup.time_ptr ? &(ent->client->*powerup.time_ptr) : nullptr;
  739. auto *powerup_count = powerup.count_ptr ? &(ent->client->*powerup.count_ptr) : nullptr;
  740. if (powerup_time && *powerup_time <= level.time)
  741. continue;
  742. else if (powerup_count && !*powerup_count)
  743. continue;
  744. if (!best_powerup)
  745. {
  746. best_powerup = &powerup;
  747. continue;
  748. }
  749. if (powerup_time && *powerup_time < ent->client->*best_powerup->time_ptr)
  750. {
  751. best_powerup = &powerup;
  752. continue;
  753. }
  754. else if (powerup_count && !best_powerup->time_ptr)
  755. {
  756. best_powerup = &powerup;
  757. continue;
  758. }
  759. }
  760. if (best_powerup)
  761. {
  762. int16_t value;
  763. if (best_powerup->count_ptr)
  764. value = (ent->client->*best_powerup->count_ptr);
  765. else
  766. value = ceil((ent->client->*best_powerup->time_ptr - level.time).seconds());
  767. ent->client->ps.stats[STAT_TIMER_ICON] = gi.imageindex(GetItemByIndex(best_powerup->item)->icon);
  768. ent->client->ps.stats[STAT_TIMER] = value;
  769. }
  770. }
  771. // PGM
  772. //
  773. // selected item
  774. //
  775. ent->client->ps.stats[STAT_SELECTED_ITEM] = ent->client->pers.selected_item;
  776. if (ent->client->pers.selected_item == IT_NULL)
  777. ent->client->ps.stats[STAT_SELECTED_ICON] = 0;
  778. else
  779. {
  780. ent->client->ps.stats[STAT_SELECTED_ICON] = gi.imageindex(itemlist[ent->client->pers.selected_item].icon);
  781. if (ent->client->pers.selected_item_time < level.time)
  782. ent->client->ps.stats[STAT_SELECTED_ITEM_NAME] = 0;
  783. }
  784. //
  785. // layouts
  786. //
  787. ent->client->ps.stats[STAT_LAYOUTS] = 0;
  788. if (deathmatch->integer)
  789. {
  790. if (ent->client->pers.health <= 0 || level.intermissiontime || ent->client->showscores)
  791. ent->client->ps.stats[STAT_LAYOUTS] |= LAYOUTS_LAYOUT;
  792. if (ent->client->showinventory && ent->client->pers.health > 0)
  793. ent->client->ps.stats[STAT_LAYOUTS] |= LAYOUTS_INVENTORY;
  794. }
  795. else
  796. {
  797. if (ent->client->showscores || ent->client->showhelp || ent->client->showeou)
  798. ent->client->ps.stats[STAT_LAYOUTS] |= LAYOUTS_LAYOUT;
  799. if (ent->client->showinventory && ent->client->pers.health > 0)
  800. ent->client->ps.stats[STAT_LAYOUTS] |= LAYOUTS_INVENTORY;
  801. if (ent->client->showhelp)
  802. ent->client->ps.stats[STAT_LAYOUTS] |= LAYOUTS_HELP;
  803. }
  804. if (level.intermissiontime || ent->client->awaiting_respawn)
  805. {
  806. if (ent->client->awaiting_respawn || (level.intermission_eou || level.is_n64 || (deathmatch->integer && level.intermissiontime)))
  807. ent->client->ps.stats[STAT_LAYOUTS] |= LAYOUTS_HIDE_HUD;
  808. // N64 always merges into one screen on level ends
  809. if (level.intermission_eou || level.is_n64 || (deathmatch->integer && level.intermissiontime))
  810. ent->client->ps.stats[STAT_LAYOUTS] |= LAYOUTS_INTERMISSION;
  811. }
  812. if (level.story_active)
  813. ent->client->ps.stats[STAT_LAYOUTS] |= LAYOUTS_HIDE_CROSSHAIR;
  814. else
  815. ent->client->ps.stats[STAT_LAYOUTS] &= ~LAYOUTS_HIDE_CROSSHAIR;
  816. // [Paril-KEX] key display
  817. if (!deathmatch->integer)
  818. {
  819. int32_t key_offset = 0;
  820. player_stat_t stat = STAT_KEY_A;
  821. ent->client->ps.stats[STAT_KEY_A] =
  822. ent->client->ps.stats[STAT_KEY_B] =
  823. ent->client->ps.stats[STAT_KEY_C] = 0;
  824. // there's probably a way to do this in one pass but
  825. // I'm lazy
  826. std::array<item_id_t, IT_TOTAL> keys_held;
  827. size_t num_keys_held = 0;
  828. for (auto &item : itemlist)
  829. {
  830. if (!(item.flags & IF_KEY))
  831. continue;
  832. else if (!ent->client->pers.inventory[item.id])
  833. continue;
  834. keys_held[num_keys_held++] = item.id;
  835. }
  836. if (num_keys_held > 3)
  837. key_offset = (int32_t) (level.time.seconds() / 5);
  838. for (int32_t i = 0; i < min(num_keys_held, (size_t) 3); i++, stat = (player_stat_t) (stat + 1))
  839. ent->client->ps.stats[stat] = gi.imageindex(GetItemByIndex(keys_held[(i + key_offset) % num_keys_held])->icon);
  840. }
  841. //
  842. // frags
  843. //
  844. ent->client->ps.stats[STAT_FRAGS] = ent->client->resp.score;
  845. //
  846. // help icon / current weapon if not shown
  847. //
  848. if (ent->client->pers.helpchanged >= 1 && ent->client->pers.helpchanged <= 2 && (level.time.milliseconds() % 1000) < 500) // haleyjd: time-limited
  849. ent->client->ps.stats[STAT_HELPICON] = gi.imageindex("i_help");
  850. else if ((ent->client->pers.hand == CENTER_HANDED) && ent->client->pers.weapon)
  851. ent->client->ps.stats[STAT_HELPICON] = gi.imageindex(ent->client->pers.weapon->icon);
  852. else
  853. ent->client->ps.stats[STAT_HELPICON] = 0;
  854. ent->client->ps.stats[STAT_SPECTATOR] = 0;
  855. // set & run the health bar stuff
  856. for (size_t i = 0; i < MAX_HEALTH_BARS; i++)
  857. {
  858. byte *health_byte = reinterpret_cast<byte *>(&ent->client->ps.stats[STAT_HEALTH_BARS]) + i;
  859. if (!level.health_bar_entities[i])
  860. *health_byte = 0;
  861. else if (level.health_bar_entities[i]->timestamp)
  862. {
  863. if (level.health_bar_entities[i]->timestamp < level.time)
  864. {
  865. level.health_bar_entities[i] = nullptr;
  866. *health_byte = 0;
  867. continue;
  868. }
  869. *health_byte = 0b10000000;
  870. }
  871. else
  872. {
  873. // enemy dead
  874. if (!level.health_bar_entities[i]->enemy->inuse || level.health_bar_entities[i]->enemy->health <= 0)
  875. {
  876. // hack for Makron
  877. if (level.health_bar_entities[i]->enemy->monsterinfo.aiflags & AI_DOUBLE_TROUBLE)
  878. {
  879. *health_byte = 0b10000000;
  880. continue;
  881. }
  882. if (level.health_bar_entities[i]->delay)
  883. {
  884. level.health_bar_entities[i]->timestamp = level.time + gtime_t::from_sec(level.health_bar_entities[i]->delay);
  885. *health_byte = 0b10000000;
  886. }
  887. else
  888. {
  889. level.health_bar_entities[i] = nullptr;
  890. *health_byte = 0;
  891. }
  892. continue;
  893. }
  894. else if (level.health_bar_entities[i]->spawnflags.has(SPAWNFLAG_HEALTHBAR_PVS_ONLY) && !gi.inPVS(ent->s.origin, level.health_bar_entities[i]->enemy->s.origin, true))
  895. {
  896. *health_byte = 0;
  897. continue;
  898. }
  899. float health_remaining = ((float) level.health_bar_entities[i]->enemy->health) / level.health_bar_entities[i]->enemy->max_health;
  900. *health_byte = ((byte) (health_remaining * 0b01111111)) | 0b10000000;
  901. }
  902. }
  903. // ZOID
  904. SetCTFStats(ent);
  905. // ZOID
  906. }
  907. /*
  908. ===============
  909. G_CheckChaseStats
  910. ===============
  911. */
  912. void G_CheckChaseStats(edict_t *ent)
  913. {
  914. gclient_t *cl;
  915. for (uint32_t i = 1; i <= game.maxclients; i++)
  916. {
  917. cl = g_edicts[i].client;
  918. if (!g_edicts[i].inuse || cl->chase_target != ent)
  919. continue;
  920. cl->ps.stats = ent->client->ps.stats;
  921. G_SetSpectatorStats(g_edicts + i);
  922. }
  923. }
  924. /*
  925. ===============
  926. G_SetSpectatorStats
  927. ===============
  928. */
  929. void G_SetSpectatorStats(edict_t *ent)
  930. {
  931. gclient_t *cl = ent->client;
  932. if (!cl->chase_target)
  933. G_SetStats(ent);
  934. cl->ps.stats[STAT_SPECTATOR] = 1;
  935. // layouts are independant in spectator
  936. cl->ps.stats[STAT_LAYOUTS] = 0;
  937. if (cl->pers.health <= 0 || level.intermissiontime || cl->showscores)
  938. cl->ps.stats[STAT_LAYOUTS] |= LAYOUTS_LAYOUT;
  939. if (cl->showinventory && cl->pers.health > 0)
  940. cl->ps.stats[STAT_LAYOUTS] |= LAYOUTS_INVENTORY;
  941. if (cl->chase_target && cl->chase_target->inuse)
  942. cl->ps.stats[STAT_CHASE] = CS_PLAYERSKINS +
  943. (cl->chase_target - g_edicts) - 1;
  944. else
  945. cl->ps.stats[STAT_CHASE] = 0;
  946. }