g_ctf.cpp 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863
  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 <assert.h>
  6. enum match_t
  7. {
  8. MATCH_NONE,
  9. MATCH_SETUP,
  10. MATCH_PREGAME,
  11. MATCH_GAME,
  12. MATCH_POST
  13. };
  14. enum elect_t
  15. {
  16. ELECT_NONE,
  17. ELECT_MATCH,
  18. ELECT_ADMIN,
  19. ELECT_MAP
  20. };
  21. struct ctfgame_t
  22. {
  23. int team1, team2;
  24. int total1, total2; // these are only set when going into intermission except in teamplay
  25. gtime_t last_flag_capture;
  26. int last_capture_team;
  27. match_t match; // match state
  28. gtime_t matchtime; // time for match start/end (depends on state)
  29. int lasttime; // last time update, explicitly truncated to seconds
  30. bool countdown; // has audio countdown started?
  31. elect_t election; // election type
  32. edict_t *etarget; // for admin election, who's being elected
  33. char elevel[32]; // for map election, target level
  34. int evotes; // votes so far
  35. int needvotes; // votes needed
  36. gtime_t electtime; // remaining time until election times out
  37. char emsg[256]; // election name
  38. int warnactive; // true if stat string 30 is active
  39. ghost_t ghosts[MAX_CLIENTS]; // ghost codes
  40. };
  41. ctfgame_t ctfgame;
  42. cvar_t *ctf;
  43. cvar_t *teamplay;
  44. cvar_t *g_teamplay_force_join;
  45. // [Paril-KEX]
  46. bool G_TeamplayEnabled()
  47. {
  48. return ctf->integer || teamplay->integer;
  49. }
  50. // [Paril-KEX]
  51. void G_AdjustTeamScore(ctfteam_t team, int32_t offset)
  52. {
  53. if (team == CTF_TEAM1)
  54. ctfgame.total1 += offset;
  55. else if (team == CTF_TEAM2)
  56. ctfgame.total2 += offset;
  57. }
  58. cvar_t *competition;
  59. cvar_t *matchlock;
  60. cvar_t *electpercentage;
  61. cvar_t *matchtime;
  62. cvar_t *matchsetuptime;
  63. cvar_t *matchstarttime;
  64. cvar_t *admin_password;
  65. cvar_t *allow_admin;
  66. cvar_t *warp_list;
  67. cvar_t *warn_unbalanced;
  68. // Index for various CTF pics, this saves us from calling gi.imageindex
  69. // all the time and saves a few CPU cycles since we don't have to do
  70. // a bunch of string compares all the time.
  71. // These are set in CTFPrecache() called from worldspawn
  72. int imageindex_i_ctf1;
  73. int imageindex_i_ctf2;
  74. int imageindex_i_ctf1d;
  75. int imageindex_i_ctf2d;
  76. int imageindex_i_ctf1t;
  77. int imageindex_i_ctf2t;
  78. int imageindex_i_ctfj;
  79. int imageindex_sbfctf1;
  80. int imageindex_sbfctf2;
  81. int imageindex_ctfsb1;
  82. int imageindex_ctfsb2;
  83. int modelindex_flag1, modelindex_flag2; // [Paril-KEX]
  84. constexpr item_id_t tech_ids[] = { IT_TECH_RESISTANCE, IT_TECH_STRENGTH, IT_TECH_HASTE, IT_TECH_REGENERATION };
  85. /*--------------------------------------------------------------------------*/
  86. #ifndef KEX_Q2_GAME
  87. /*
  88. =================
  89. findradius
  90. Returns entities that have origins within a spherical area
  91. findradius (origin, radius)
  92. =================
  93. */
  94. static edict_t *loc_findradius(edict_t *from, const vec3_t &org, float rad)
  95. {
  96. vec3_t eorg;
  97. int j;
  98. if (!from)
  99. from = g_edicts;
  100. else
  101. from++;
  102. for (; from < &g_edicts[globals.num_edicts]; from++)
  103. {
  104. if (!from->inuse)
  105. continue;
  106. for (j = 0; j < 3; j++)
  107. eorg[j] = org[j] - (from->s.origin[j] + (from->mins[j] + from->maxs[j]) * 0.5f);
  108. if (eorg.length() > rad)
  109. continue;
  110. return from;
  111. }
  112. return nullptr;
  113. }
  114. #endif
  115. static void loc_buildboxpoints(vec3_t (&p)[8], const vec3_t &org, const vec3_t &mins, const vec3_t &maxs)
  116. {
  117. p[0] = org + mins;
  118. p[1] = p[0];
  119. p[1][0] -= mins[0];
  120. p[2] = p[0];
  121. p[2][1] -= mins[1];
  122. p[3] = p[0];
  123. p[3][0] -= mins[0];
  124. p[3][1] -= mins[1];
  125. p[4] = org + maxs;
  126. p[5] = p[4];
  127. p[5][0] -= maxs[0];
  128. p[6] = p[0];
  129. p[6][1] -= maxs[1];
  130. p[7] = p[0];
  131. p[7][0] -= maxs[0];
  132. p[7][1] -= maxs[1];
  133. }
  134. static bool loc_CanSee(edict_t *targ, edict_t *inflictor)
  135. {
  136. trace_t trace;
  137. vec3_t targpoints[8];
  138. int i;
  139. vec3_t viewpoint;
  140. // bmodels need special checking because their origin is 0,0,0
  141. if (targ->movetype == MOVETYPE_PUSH)
  142. return false; // bmodels not supported
  143. loc_buildboxpoints(targpoints, targ->s.origin, targ->mins, targ->maxs);
  144. viewpoint = inflictor->s.origin;
  145. viewpoint[2] += inflictor->viewheight;
  146. for (i = 0; i < 8; i++)
  147. {
  148. trace = gi.traceline(viewpoint, targpoints[i], inflictor, MASK_SOLID);
  149. if (trace.fraction == 1.0f)
  150. return true;
  151. }
  152. return false;
  153. }
  154. #
  155. /*--------------------------------------------------------------------------*/
  156. void CTFSpawn()
  157. {
  158. memset(&ctfgame, 0, sizeof(ctfgame));
  159. CTFSetupTechSpawn();
  160. if (competition->integer > 1)
  161. {
  162. ctfgame.match = MATCH_SETUP;
  163. ctfgame.matchtime = level.time + gtime_t::from_min(matchsetuptime->value);
  164. }
  165. }
  166. void CTFInit()
  167. {
  168. ctf = gi.cvar("ctf", "0", CVAR_SERVERINFO | CVAR_LATCH);
  169. competition = gi.cvar("competition", "0", CVAR_SERVERINFO);
  170. matchlock = gi.cvar("matchlock", "1", CVAR_SERVERINFO);
  171. electpercentage = gi.cvar("electpercentage", "66", CVAR_NOFLAGS);
  172. matchtime = gi.cvar("matchtime", "20", CVAR_SERVERINFO);
  173. matchsetuptime = gi.cvar("matchsetuptime", "10", CVAR_NOFLAGS);
  174. matchstarttime = gi.cvar("matchstarttime", "20", CVAR_NOFLAGS);
  175. admin_password = gi.cvar("admin_password", "", CVAR_NOFLAGS);
  176. allow_admin = gi.cvar("allow_admin", "1", CVAR_NOFLAGS);
  177. warp_list = gi.cvar("warp_list", "q2ctf1 q2ctf2 q2ctf3 q2ctf4 q2ctf5", CVAR_NOFLAGS);
  178. warn_unbalanced = gi.cvar("warn_unbalanced", "0", CVAR_NOFLAGS);
  179. }
  180. /*
  181. * Precache CTF items
  182. */
  183. void CTFPrecache()
  184. {
  185. imageindex_i_ctf1 = gi.imageindex("i_ctf1");
  186. imageindex_i_ctf2 = gi.imageindex("i_ctf2");
  187. imageindex_i_ctf1d = gi.imageindex("i_ctf1d");
  188. imageindex_i_ctf2d = gi.imageindex("i_ctf2d");
  189. imageindex_i_ctf1t = gi.imageindex("i_ctf1t");
  190. imageindex_i_ctf2t = gi.imageindex("i_ctf2t");
  191. imageindex_i_ctfj = gi.imageindex("i_ctfj");
  192. imageindex_sbfctf1 = gi.imageindex("sbfctf1");
  193. imageindex_sbfctf2 = gi.imageindex("sbfctf2");
  194. imageindex_ctfsb1 = gi.imageindex("tag4");
  195. imageindex_ctfsb2 = gi.imageindex("tag5");
  196. modelindex_flag1 = gi.modelindex("players/male/flag1.md2");
  197. modelindex_flag2 = gi.modelindex("players/male/flag2.md2");
  198. PrecacheItem(GetItemByIndex(IT_WEAPON_GRAPPLE));
  199. }
  200. /*--------------------------------------------------------------------------*/
  201. const char *CTFTeamName(int team)
  202. {
  203. switch (team)
  204. {
  205. case CTF_TEAM1:
  206. return "RED";
  207. case CTF_TEAM2:
  208. return "BLUE";
  209. case CTF_NOTEAM:
  210. return "SPECTATOR";
  211. }
  212. return "UNKNOWN"; // Hanzo pointed out this was spelled wrong as "UKNOWN"
  213. }
  214. const char *CTFOtherTeamName(int team)
  215. {
  216. switch (team)
  217. {
  218. case CTF_TEAM1:
  219. return "BLUE";
  220. case CTF_TEAM2:
  221. return "RED";
  222. }
  223. return "UNKNOWN"; // Hanzo pointed out this was spelled wrong as "UKNOWN"
  224. }
  225. int CTFOtherTeam(int team)
  226. {
  227. switch (team)
  228. {
  229. case CTF_TEAM1:
  230. return CTF_TEAM2;
  231. case CTF_TEAM2:
  232. return CTF_TEAM1;
  233. }
  234. return -1; // invalid value
  235. }
  236. /*--------------------------------------------------------------------------*/
  237. float PlayersRangeFromSpot(edict_t *spot);
  238. bool SpawnPointClear(edict_t *spot);
  239. void CTFAssignSkin(edict_t *ent, const char *s)
  240. {
  241. int playernum = ent - g_edicts - 1;
  242. std::string_view t(s);
  243. if (size_t i = t.find_first_of('/'); i != std::string_view::npos)
  244. t = t.substr(0, i + 1);
  245. else
  246. t = "male/";
  247. switch (ent->client->resp.ctf_team)
  248. {
  249. case CTF_TEAM1:
  250. t = G_Fmt("{}\\{}{}\\default", ent->client->pers.netname, t, CTF_TEAM1_SKIN);
  251. break;
  252. case CTF_TEAM2:
  253. t = G_Fmt("{}\\{}{}\\default", ent->client->pers.netname, t, CTF_TEAM2_SKIN);
  254. break;
  255. default:
  256. t = G_Fmt("{}\\{}\\default", ent->client->pers.netname, s);
  257. break;
  258. }
  259. gi.configstring(CS_PLAYERSKINS + playernum, t.data());
  260. // gi.LocClient_Print(ent, PRINT_HIGH, "$g_assigned_team", ent->client->pers.netname);
  261. }
  262. void CTFAssignTeam(gclient_t *who)
  263. {
  264. edict_t *player;
  265. uint32_t team1count = 0, team2count = 0;
  266. who->resp.ctf_state = 0;
  267. if (!g_teamplay_force_join->integer && !(g_edicts[1 + (who - game.clients)].svflags & SVF_BOT))
  268. {
  269. who->resp.ctf_team = CTF_NOTEAM;
  270. return;
  271. }
  272. for (uint32_t i = 1; i <= game.maxclients; i++)
  273. {
  274. player = &g_edicts[i];
  275. if (!player->inuse || player->client == who)
  276. continue;
  277. switch (player->client->resp.ctf_team)
  278. {
  279. case CTF_TEAM1:
  280. team1count++;
  281. break;
  282. case CTF_TEAM2:
  283. team2count++;
  284. break;
  285. default:
  286. break;
  287. }
  288. }
  289. if (team1count < team2count)
  290. who->resp.ctf_team = CTF_TEAM1;
  291. else if (team2count < team1count)
  292. who->resp.ctf_team = CTF_TEAM2;
  293. else if (brandom())
  294. who->resp.ctf_team = CTF_TEAM1;
  295. else
  296. who->resp.ctf_team = CTF_TEAM2;
  297. }
  298. /*
  299. ================
  300. SelectCTFSpawnPoint
  301. go to a ctf point, but NOT the two points closest
  302. to other players
  303. ================
  304. */
  305. edict_t *SelectCTFSpawnPoint(edict_t *ent, bool force_spawn)
  306. {
  307. if (ent->client->resp.ctf_state)
  308. {
  309. select_spawn_result_t result = SelectDeathmatchSpawnPoint(g_dm_spawn_farthest->integer, force_spawn, false);
  310. if (result.any_valid)
  311. return result.spot;
  312. }
  313. const char *cname;
  314. switch (ent->client->resp.ctf_team)
  315. {
  316. case CTF_TEAM1:
  317. cname = "info_player_team1";
  318. break;
  319. case CTF_TEAM2:
  320. cname = "info_player_team2";
  321. break;
  322. default:
  323. {
  324. select_spawn_result_t result = SelectDeathmatchSpawnPoint(g_dm_spawn_farthest->integer, force_spawn, true);
  325. if (result.any_valid)
  326. return result.spot;
  327. gi.Com_Error("can't find suitable spectator spawn point");
  328. return nullptr;
  329. }
  330. }
  331. static std::vector<edict_t *> spawn_points;
  332. edict_t *spot = nullptr;
  333. spawn_points.clear();
  334. while ((spot = G_FindByString<&edict_t::classname>(spot, cname)) != nullptr)
  335. spawn_points.push_back(spot);
  336. if (!spawn_points.size())
  337. {
  338. select_spawn_result_t result = SelectDeathmatchSpawnPoint(g_dm_spawn_farthest->integer, force_spawn, true);
  339. if (!result.any_valid)
  340. gi.Com_Error("can't find suitable CTF spawn point");
  341. return result.spot;
  342. }
  343. std::shuffle(spawn_points.begin(), spawn_points.end(), mt_rand);
  344. for (auto &point : spawn_points)
  345. if (SpawnPointClear(point))
  346. return point;
  347. if (force_spawn)
  348. return random_element(spawn_points);
  349. return nullptr;
  350. }
  351. /*------------------------------------------------------------------------*/
  352. /*
  353. CTFFragBonuses
  354. Calculate the bonuses for flag defense, flag carrier defense, etc.
  355. Note that bonuses are not cumaltive. You get one, they are in importance
  356. order.
  357. */
  358. void CTFFragBonuses(edict_t *targ, edict_t *inflictor, edict_t *attacker)
  359. {
  360. edict_t *ent;
  361. item_id_t flag_item, enemy_flag_item;
  362. int otherteam;
  363. edict_t *flag, *carrier = nullptr;
  364. const char *c;
  365. vec3_t v1, v2;
  366. if (targ->client && attacker->client)
  367. {
  368. if (attacker->client->resp.ghost)
  369. if (attacker != targ)
  370. attacker->client->resp.ghost->kills++;
  371. if (targ->client->resp.ghost)
  372. targ->client->resp.ghost->deaths++;
  373. }
  374. // no bonus for fragging yourself
  375. if (!targ->client || !attacker->client || targ == attacker)
  376. return;
  377. otherteam = CTFOtherTeam(targ->client->resp.ctf_team);
  378. if (otherteam < 0)
  379. return; // whoever died isn't on a team
  380. // same team, if the flag at base, check to he has the enemy flag
  381. if (targ->client->resp.ctf_team == CTF_TEAM1)
  382. {
  383. flag_item = IT_FLAG1;
  384. enemy_flag_item = IT_FLAG2;
  385. }
  386. else
  387. {
  388. flag_item = IT_FLAG2;
  389. enemy_flag_item = IT_FLAG1;
  390. }
  391. // did the attacker frag the flag carrier?
  392. if (targ->client->pers.inventory[enemy_flag_item])
  393. {
  394. attacker->client->resp.ctf_lastfraggedcarrier = level.time;
  395. attacker->client->resp.score += CTF_FRAG_CARRIER_BONUS;
  396. gi.LocClient_Print(attacker, PRINT_MEDIUM, "$g_bonus_enemy_carrier",
  397. CTF_FRAG_CARRIER_BONUS);
  398. // the target had the flag, clear the hurt carrier
  399. // field on the other team
  400. for (uint32_t i = 1; i <= game.maxclients; i++)
  401. {
  402. ent = g_edicts + i;
  403. if (ent->inuse && ent->client->resp.ctf_team == otherteam)
  404. ent->client->resp.ctf_lasthurtcarrier = 0_ms;
  405. }
  406. return;
  407. }
  408. if (targ->client->resp.ctf_lasthurtcarrier &&
  409. level.time - targ->client->resp.ctf_lasthurtcarrier < CTF_CARRIER_DANGER_PROTECT_TIMEOUT &&
  410. !attacker->client->pers.inventory[flag_item])
  411. {
  412. // attacker is on the same team as the flag carrier and
  413. // fragged a guy who hurt our flag carrier
  414. attacker->client->resp.score += CTF_CARRIER_DANGER_PROTECT_BONUS;
  415. gi.LocBroadcast_Print(PRINT_MEDIUM, "$g_bonus_flag_defense",
  416. attacker->client->pers.netname,
  417. CTFTeamName(attacker->client->resp.ctf_team));
  418. if (attacker->client->resp.ghost)
  419. attacker->client->resp.ghost->carrierdef++;
  420. return;
  421. }
  422. // flag and flag carrier area defense bonuses
  423. // we have to find the flag and carrier entities
  424. // find the flag
  425. switch (attacker->client->resp.ctf_team)
  426. {
  427. case CTF_TEAM1:
  428. c = "item_flag_team1";
  429. break;
  430. case CTF_TEAM2:
  431. c = "item_flag_team2";
  432. break;
  433. default:
  434. return;
  435. }
  436. flag = nullptr;
  437. while ((flag = G_FindByString<&edict_t::classname>(flag, c)) != nullptr)
  438. {
  439. if (!(flag->spawnflags & SPAWNFLAG_ITEM_DROPPED))
  440. break;
  441. }
  442. if (!flag)
  443. return; // can't find attacker's flag
  444. // find attacker's team's flag carrier
  445. for (uint32_t i = 1; i <= game.maxclients; i++)
  446. {
  447. carrier = g_edicts + i;
  448. if (carrier->inuse &&
  449. carrier->client->pers.inventory[flag_item])
  450. break;
  451. carrier = nullptr;
  452. }
  453. // ok we have the attackers flag and a pointer to the carrier
  454. // check to see if we are defending the base's flag
  455. v1 = targ->s.origin - flag->s.origin;
  456. v2 = attacker->s.origin - flag->s.origin;
  457. if ((v1.length() < CTF_TARGET_PROTECT_RADIUS ||
  458. v2.length() < CTF_TARGET_PROTECT_RADIUS ||
  459. loc_CanSee(flag, targ) || loc_CanSee(flag, attacker)) &&
  460. attacker->client->resp.ctf_team != targ->client->resp.ctf_team)
  461. {
  462. // we defended the base flag
  463. attacker->client->resp.score += CTF_FLAG_DEFENSE_BONUS;
  464. if (flag->solid == SOLID_NOT)
  465. gi.LocBroadcast_Print(PRINT_MEDIUM, "$g_bonus_defend_base",
  466. attacker->client->pers.netname,
  467. CTFTeamName(attacker->client->resp.ctf_team));
  468. else
  469. gi.LocBroadcast_Print(PRINT_MEDIUM, "$g_bonus_defend_flag",
  470. attacker->client->pers.netname,
  471. CTFTeamName(attacker->client->resp.ctf_team));
  472. if (attacker->client->resp.ghost)
  473. attacker->client->resp.ghost->basedef++;
  474. return;
  475. }
  476. if (carrier && carrier != attacker)
  477. {
  478. v1 = targ->s.origin - carrier->s.origin;
  479. v2 = attacker->s.origin - carrier->s.origin;
  480. if (v1.length() < CTF_ATTACKER_PROTECT_RADIUS ||
  481. v2.length() < CTF_ATTACKER_PROTECT_RADIUS ||
  482. loc_CanSee(carrier, targ) || loc_CanSee(carrier, attacker))
  483. {
  484. attacker->client->resp.score += CTF_CARRIER_PROTECT_BONUS;
  485. gi.LocBroadcast_Print(PRINT_MEDIUM, "$g_bonus_defend_carrier",
  486. attacker->client->pers.netname,
  487. CTFTeamName(attacker->client->resp.ctf_team));
  488. if (attacker->client->resp.ghost)
  489. attacker->client->resp.ghost->carrierdef++;
  490. return;
  491. }
  492. }
  493. }
  494. void CTFCheckHurtCarrier(edict_t *targ, edict_t *attacker)
  495. {
  496. item_id_t flag_item;
  497. if (!targ->client || !attacker->client)
  498. return;
  499. if (targ->client->resp.ctf_team == CTF_TEAM1)
  500. flag_item = IT_FLAG2;
  501. else
  502. flag_item = IT_FLAG1;
  503. if (targ->client->pers.inventory[flag_item] &&
  504. targ->client->resp.ctf_team != attacker->client->resp.ctf_team)
  505. attacker->client->resp.ctf_lasthurtcarrier = level.time;
  506. }
  507. /*------------------------------------------------------------------------*/
  508. void CTFResetFlag(int ctf_team)
  509. {
  510. const char *c;
  511. edict_t *ent;
  512. switch (ctf_team)
  513. {
  514. case CTF_TEAM1:
  515. c = "item_flag_team1";
  516. break;
  517. case CTF_TEAM2:
  518. c = "item_flag_team2";
  519. break;
  520. default:
  521. return;
  522. }
  523. ent = nullptr;
  524. while ((ent = G_FindByString<&edict_t::classname>(ent, c)) != nullptr)
  525. {
  526. if (ent->spawnflags.has(SPAWNFLAG_ITEM_DROPPED))
  527. G_FreeEdict(ent);
  528. else
  529. {
  530. ent->svflags &= ~SVF_NOCLIENT;
  531. ent->solid = SOLID_TRIGGER;
  532. gi.linkentity(ent);
  533. ent->s.event = EV_ITEM_RESPAWN;
  534. }
  535. }
  536. }
  537. void CTFResetFlags()
  538. {
  539. CTFResetFlag(CTF_TEAM1);
  540. CTFResetFlag(CTF_TEAM2);
  541. }
  542. bool CTFPickup_Flag(edict_t *ent, edict_t *other)
  543. {
  544. int ctf_team;
  545. edict_t *player;
  546. item_id_t flag_item, enemy_flag_item;
  547. // figure out what team this flag is
  548. if (ent->item->id == IT_FLAG1)
  549. ctf_team = CTF_TEAM1;
  550. else if (ent->item->id == IT_FLAG2)
  551. ctf_team = CTF_TEAM2;
  552. else
  553. {
  554. gi.LocClient_Print(ent, PRINT_HIGH, "Don't know what team the flag is on.\n");
  555. return false;
  556. }
  557. // same team, if the flag at base, check to he has the enemy flag
  558. if (ctf_team == CTF_TEAM1)
  559. {
  560. flag_item = IT_FLAG1;
  561. enemy_flag_item = IT_FLAG2;
  562. }
  563. else
  564. {
  565. flag_item = IT_FLAG2;
  566. enemy_flag_item = IT_FLAG1;
  567. }
  568. if (ctf_team == other->client->resp.ctf_team)
  569. {
  570. if (!(ent->spawnflags & SPAWNFLAG_ITEM_DROPPED))
  571. {
  572. // the flag is at home base. if the player has the enemy
  573. // flag, he's just won!
  574. if (other->client->pers.inventory[enemy_flag_item])
  575. {
  576. gi.LocBroadcast_Print(PRINT_HIGH, "$g_flag_captured",
  577. other->client->pers.netname, CTFOtherTeamName(ctf_team));
  578. other->client->pers.inventory[enemy_flag_item] = 0;
  579. ctfgame.last_flag_capture = level.time;
  580. ctfgame.last_capture_team = ctf_team;
  581. if (ctf_team == CTF_TEAM1)
  582. ctfgame.team1++;
  583. else
  584. ctfgame.team2++;
  585. gi.sound(ent, CHAN_RELIABLE | CHAN_NO_PHS_ADD | CHAN_AUX, gi.soundindex("ctf/flagcap.wav"), 1, ATTN_NONE, 0);
  586. // other gets another 10 frag bonus
  587. other->client->resp.score += CTF_CAPTURE_BONUS;
  588. if (other->client->resp.ghost)
  589. other->client->resp.ghost->caps++;
  590. // Ok, let's do the player loop, hand out the bonuses
  591. for (uint32_t i = 1; i <= game.maxclients; i++)
  592. {
  593. player = &g_edicts[i];
  594. if (!player->inuse)
  595. continue;
  596. if (player->client->resp.ctf_team != other->client->resp.ctf_team)
  597. player->client->resp.ctf_lasthurtcarrier = -5_sec;
  598. else if (player->client->resp.ctf_team == other->client->resp.ctf_team)
  599. {
  600. if (player != other)
  601. player->client->resp.score += CTF_TEAM_BONUS;
  602. // award extra points for capture assists
  603. if (player->client->resp.ctf_lastreturnedflag && player->client->resp.ctf_lastreturnedflag + CTF_RETURN_FLAG_ASSIST_TIMEOUT > level.time)
  604. {
  605. gi.LocBroadcast_Print(PRINT_HIGH, "$g_bonus_assist_return", player->client->pers.netname);
  606. player->client->resp.score += CTF_RETURN_FLAG_ASSIST_BONUS;
  607. }
  608. if (player->client->resp.ctf_lastfraggedcarrier && player->client->resp.ctf_lastfraggedcarrier + CTF_FRAG_CARRIER_ASSIST_TIMEOUT > level.time)
  609. {
  610. gi.LocBroadcast_Print(PRINT_HIGH, "$g_bonus_assist_frag_carrier", player->client->pers.netname);
  611. player->client->resp.score += CTF_FRAG_CARRIER_ASSIST_BONUS;
  612. }
  613. }
  614. }
  615. CTFResetFlags();
  616. return false;
  617. }
  618. return false; // its at home base already
  619. }
  620. // hey, its not home. return it by teleporting it back
  621. gi.LocBroadcast_Print(PRINT_HIGH, "$g_returned_flag",
  622. other->client->pers.netname, CTFTeamName(ctf_team));
  623. other->client->resp.score += CTF_RECOVERY_BONUS;
  624. other->client->resp.ctf_lastreturnedflag = level.time;
  625. gi.sound(ent, CHAN_RELIABLE | CHAN_NO_PHS_ADD | CHAN_AUX, gi.soundindex("ctf/flagret.wav"), 1, ATTN_NONE, 0);
  626. // CTFResetFlag will remove this entity! We must return false
  627. CTFResetFlag(ctf_team);
  628. return false;
  629. }
  630. // hey, its not our flag, pick it up
  631. gi.LocBroadcast_Print(PRINT_HIGH, "$g_got_flag",
  632. other->client->pers.netname, CTFTeamName(ctf_team));
  633. other->client->resp.score += CTF_FLAG_BONUS;
  634. other->client->pers.inventory[flag_item] = 1;
  635. other->client->resp.ctf_flagsince = level.time;
  636. // pick up the flag
  637. // if it's not a dropped flag, we just make is disappear
  638. // if it's dropped, it will be removed by the pickup caller
  639. if (!(ent->spawnflags & SPAWNFLAG_ITEM_DROPPED))
  640. {
  641. ent->flags |= FL_RESPAWN;
  642. ent->svflags |= SVF_NOCLIENT;
  643. ent->solid = SOLID_NOT;
  644. }
  645. return true;
  646. }
  647. TOUCH(CTFDropFlagTouch) (edict_t *ent, edict_t *other, const trace_t &tr, bool other_touching_self) -> void
  648. {
  649. // owner (who dropped us) can't touch for two secs
  650. if (other == ent->owner &&
  651. ent->nextthink - level.time > CTF_AUTO_FLAG_RETURN_TIMEOUT - 2_sec)
  652. return;
  653. Touch_Item(ent, other, tr, other_touching_self);
  654. }
  655. THINK(CTFDropFlagThink) (edict_t *ent) -> void
  656. {
  657. // auto return the flag
  658. // reset flag will remove ourselves
  659. if (ent->item->id == IT_FLAG1)
  660. {
  661. CTFResetFlag(CTF_TEAM1);
  662. gi.LocBroadcast_Print(PRINT_HIGH, "$g_flag_returned",
  663. CTFTeamName(CTF_TEAM1));
  664. }
  665. else if (ent->item->id == IT_FLAG2)
  666. {
  667. CTFResetFlag(CTF_TEAM2);
  668. gi.LocBroadcast_Print(PRINT_HIGH, "$g_flag_returned",
  669. CTFTeamName(CTF_TEAM2));
  670. }
  671. gi.sound(ent, CHAN_RELIABLE | CHAN_NO_PHS_ADD | CHAN_AUX, gi.soundindex("ctf/flagret.wav"), 1, ATTN_NONE, 0);
  672. }
  673. // Called from PlayerDie, to drop the flag from a dying player
  674. void CTFDeadDropFlag(edict_t *self)
  675. {
  676. edict_t *dropped = nullptr;
  677. if (self->client->pers.inventory[IT_FLAG1])
  678. {
  679. dropped = Drop_Item(self, GetItemByIndex(IT_FLAG1));
  680. self->client->pers.inventory[IT_FLAG1] = 0;
  681. gi.LocBroadcast_Print(PRINT_HIGH, "$g_lost_flag",
  682. self->client->pers.netname, CTFTeamName(CTF_TEAM1));
  683. }
  684. else if (self->client->pers.inventory[IT_FLAG2])
  685. {
  686. dropped = Drop_Item(self, GetItemByIndex(IT_FLAG2));
  687. self->client->pers.inventory[IT_FLAG2] = 0;
  688. gi.LocBroadcast_Print(PRINT_HIGH, "$g_lost_flag",
  689. self->client->pers.netname, CTFTeamName(CTF_TEAM2));
  690. }
  691. if (dropped)
  692. {
  693. dropped->think = CTFDropFlagThink;
  694. dropped->nextthink = level.time + CTF_AUTO_FLAG_RETURN_TIMEOUT;
  695. dropped->touch = CTFDropFlagTouch;
  696. }
  697. }
  698. void CTFDrop_Flag(edict_t *ent, gitem_t *item)
  699. {
  700. if (brandom())
  701. gi.LocClient_Print(ent, PRINT_HIGH, "$g_lusers_drop_flags");
  702. else
  703. gi.LocClient_Print(ent, PRINT_HIGH, "$g_winners_drop_flags");
  704. }
  705. THINK(CTFFlagThink) (edict_t *ent) -> void
  706. {
  707. if (ent->solid != SOLID_NOT)
  708. ent->s.frame = 173 + (((ent->s.frame - 173) + 1) % 16);
  709. ent->nextthink = level.time + 10_hz;
  710. }
  711. THINK(CTFFlagSetup) (edict_t *ent) -> void
  712. {
  713. trace_t tr;
  714. vec3_t dest;
  715. ent->mins = { -15, -15, -15 };
  716. ent->maxs = { 15, 15, 15 };
  717. if (ent->model)
  718. gi.setmodel(ent, ent->model);
  719. else
  720. gi.setmodel(ent, ent->item->world_model);
  721. ent->solid = SOLID_TRIGGER;
  722. ent->movetype = MOVETYPE_TOSS;
  723. ent->touch = Touch_Item;
  724. ent->s.frame = 173;
  725. dest = ent->s.origin + vec3_t { 0, 0, -128 };
  726. tr = gi.trace(ent->s.origin, ent->mins, ent->maxs, dest, ent, MASK_SOLID);
  727. if (tr.startsolid)
  728. {
  729. gi.Com_PrintFmt("CTFFlagSetup: {} startsolid at {}\n", ent->classname, ent->s.origin);
  730. G_FreeEdict(ent);
  731. return;
  732. }
  733. ent->s.origin = tr.endpos;
  734. gi.linkentity(ent);
  735. ent->nextthink = level.time + 10_hz;
  736. ent->think = CTFFlagThink;
  737. }
  738. void CTFEffects(edict_t *player)
  739. {
  740. player->s.effects &= ~(EF_FLAG1 | EF_FLAG2);
  741. if (player->health > 0)
  742. {
  743. if (player->client->pers.inventory[IT_FLAG1])
  744. {
  745. player->s.effects |= EF_FLAG1;
  746. }
  747. if (player->client->pers.inventory[IT_FLAG2])
  748. {
  749. player->s.effects |= EF_FLAG2;
  750. }
  751. }
  752. if (player->client->pers.inventory[IT_FLAG1])
  753. player->s.modelindex3 = modelindex_flag1;
  754. else if (player->client->pers.inventory[IT_FLAG2])
  755. player->s.modelindex3 = modelindex_flag2;
  756. else
  757. player->s.modelindex3 = 0;
  758. }
  759. // called when we enter the intermission
  760. void CTFCalcScores()
  761. {
  762. ctfgame.total1 = ctfgame.total2 = 0;
  763. for (uint32_t i = 0; i < game.maxclients; i++)
  764. {
  765. if (!g_edicts[i + 1].inuse)
  766. continue;
  767. if (game.clients[i].resp.ctf_team == CTF_TEAM1)
  768. ctfgame.total1 += game.clients[i].resp.score;
  769. else if (game.clients[i].resp.ctf_team == CTF_TEAM2)
  770. ctfgame.total2 += game.clients[i].resp.score;
  771. }
  772. }
  773. // [Paril-KEX] end game rankings
  774. void CTFCalcRankings(std::array<uint32_t, MAX_CLIENTS> &player_ranks)
  775. {
  776. // we're all winners.. or losers. whatever
  777. if (ctfgame.total1 == ctfgame.total2)
  778. {
  779. player_ranks.fill(1);
  780. return;
  781. }
  782. ctfteam_t winning_team = (ctfgame.total1 > ctfgame.total2) ? CTF_TEAM1 : CTF_TEAM2;
  783. for (auto player : active_players())
  784. if (player->client->pers.spawned && player->client->resp.ctf_team != CTF_NOTEAM)
  785. player_ranks[player->s.number - 1] = player->client->resp.ctf_team == winning_team ? 1 : 2;
  786. }
  787. void CheckEndTDMLevel()
  788. {
  789. if (ctfgame.total1 >= fraglimit->integer || ctfgame.total2 >= fraglimit->integer)
  790. {
  791. gi.LocBroadcast_Print(PRINT_HIGH, "$g_fraglimit_hit");
  792. EndDMLevel();
  793. }
  794. }
  795. void CTFID_f(edict_t *ent)
  796. {
  797. if (ent->client->resp.id_state)
  798. {
  799. gi.LocClient_Print(ent, PRINT_HIGH, "Disabling player identication display.\n");
  800. ent->client->resp.id_state = false;
  801. }
  802. else
  803. {
  804. gi.LocClient_Print(ent, PRINT_HIGH, "Activating player identication display.\n");
  805. ent->client->resp.id_state = true;
  806. }
  807. }
  808. static void CTFSetIDView(edict_t *ent)
  809. {
  810. vec3_t forward, dir;
  811. trace_t tr;
  812. edict_t *who, *best;
  813. float bd = 0, d;
  814. // only check every few frames
  815. if (level.time - ent->client->resp.lastidtime < 250_ms)
  816. return;
  817. ent->client->resp.lastidtime = level.time;
  818. ent->client->ps.stats[STAT_CTF_ID_VIEW] = 0;
  819. ent->client->ps.stats[STAT_CTF_ID_VIEW_COLOR] = 0;
  820. AngleVectors(ent->client->v_angle, forward, nullptr, nullptr);
  821. forward *= 1024;
  822. forward = ent->s.origin + forward;
  823. tr = gi.traceline(ent->s.origin, forward, ent, MASK_SOLID);
  824. if (tr.fraction < 1 && tr.ent && tr.ent->client)
  825. {
  826. ent->client->ps.stats[STAT_CTF_ID_VIEW] = (tr.ent - g_edicts);
  827. if (tr.ent->client->resp.ctf_team == CTF_TEAM1)
  828. ent->client->ps.stats[STAT_CTF_ID_VIEW_COLOR] = imageindex_sbfctf1;
  829. else if (tr.ent->client->resp.ctf_team == CTF_TEAM2)
  830. ent->client->ps.stats[STAT_CTF_ID_VIEW_COLOR] = imageindex_sbfctf2;
  831. return;
  832. }
  833. AngleVectors(ent->client->v_angle, forward, nullptr, nullptr);
  834. best = nullptr;
  835. for (uint32_t i = 1; i <= game.maxclients; i++)
  836. {
  837. who = g_edicts + i;
  838. if (!who->inuse || who->solid == SOLID_NOT)
  839. continue;
  840. dir = who->s.origin - ent->s.origin;
  841. dir.normalize();
  842. d = forward.dot(dir);
  843. // we have teammate indicators that are better for this
  844. if (ent->client->resp.ctf_team == who->client->resp.ctf_team)
  845. continue;
  846. if (d > bd && loc_CanSee(ent, who))
  847. {
  848. bd = d;
  849. best = who;
  850. }
  851. }
  852. if (bd > 0.90f)
  853. {
  854. ent->client->ps.stats[STAT_CTF_ID_VIEW] = (best - g_edicts);
  855. if (best->client->resp.ctf_team == CTF_TEAM1)
  856. ent->client->ps.stats[STAT_CTF_ID_VIEW_COLOR] = imageindex_sbfctf1;
  857. else if (best->client->resp.ctf_team == CTF_TEAM2)
  858. ent->client->ps.stats[STAT_CTF_ID_VIEW_COLOR] = imageindex_sbfctf2;
  859. }
  860. }
  861. void SetCTFStats(edict_t *ent)
  862. {
  863. uint32_t i;
  864. int p1, p2;
  865. edict_t *e;
  866. if (ctfgame.match > MATCH_NONE)
  867. ent->client->ps.stats[STAT_CTF_MATCH] = CONFIG_CTF_MATCH;
  868. else
  869. ent->client->ps.stats[STAT_CTF_MATCH] = 0;
  870. if (ctfgame.warnactive)
  871. ent->client->ps.stats[STAT_CTF_TEAMINFO] = CONFIG_CTF_TEAMINFO;
  872. else
  873. ent->client->ps.stats[STAT_CTF_TEAMINFO] = 0;
  874. // ghosting
  875. if (ent->client->resp.ghost)
  876. {
  877. ent->client->resp.ghost->score = ent->client->resp.score;
  878. Q_strlcpy(ent->client->resp.ghost->netname, ent->client->pers.netname, sizeof(ent->client->resp.ghost->netname));
  879. ent->client->resp.ghost->number = ent->s.number;
  880. }
  881. // logo headers for the frag display
  882. ent->client->ps.stats[STAT_CTF_TEAM1_HEADER] = imageindex_ctfsb1;
  883. ent->client->ps.stats[STAT_CTF_TEAM2_HEADER] = imageindex_ctfsb2;
  884. bool blink = (level.time.milliseconds() % 1000) < 500;
  885. // if during intermission, we must blink the team header of the winning team
  886. if (level.intermissiontime && blink)
  887. {
  888. // blink half second
  889. // note that ctfgame.total[12] is set when we go to intermission
  890. if (ctfgame.team1 > ctfgame.team2)
  891. ent->client->ps.stats[STAT_CTF_TEAM1_HEADER] = 0;
  892. else if (ctfgame.team2 > ctfgame.team1)
  893. ent->client->ps.stats[STAT_CTF_TEAM2_HEADER] = 0;
  894. else if (ctfgame.total1 > ctfgame.total2) // frag tie breaker
  895. ent->client->ps.stats[STAT_CTF_TEAM1_HEADER] = 0;
  896. else if (ctfgame.total2 > ctfgame.total1)
  897. ent->client->ps.stats[STAT_CTF_TEAM2_HEADER] = 0;
  898. else
  899. { // tie game!
  900. ent->client->ps.stats[STAT_CTF_TEAM1_HEADER] = 0;
  901. ent->client->ps.stats[STAT_CTF_TEAM2_HEADER] = 0;
  902. }
  903. }
  904. // tech icon
  905. i = 0;
  906. ent->client->ps.stats[STAT_CTF_TECH] = 0;
  907. for (; i < q_countof(tech_ids); i++)
  908. {
  909. if (ent->client->pers.inventory[tech_ids[i]])
  910. {
  911. ent->client->ps.stats[STAT_CTF_TECH] = gi.imageindex(GetItemByIndex(tech_ids[i])->icon);
  912. break;
  913. }
  914. }
  915. if (ctf->integer)
  916. {
  917. // figure out what icon to display for team logos
  918. // three states:
  919. // flag at base
  920. // flag taken
  921. // flag dropped
  922. p1 = imageindex_i_ctf1;
  923. e = G_FindByString<&edict_t::classname>(nullptr, "item_flag_team1");
  924. if (e != nullptr)
  925. {
  926. if (e->solid == SOLID_NOT)
  927. {
  928. // not at base
  929. // check if on player
  930. p1 = imageindex_i_ctf1d; // default to dropped
  931. for (i = 1; i <= game.maxclients; i++)
  932. if (g_edicts[i].inuse &&
  933. g_edicts[i].client->pers.inventory[IT_FLAG1])
  934. {
  935. // enemy has it
  936. p1 = imageindex_i_ctf1t;
  937. break;
  938. }
  939. // [Paril-KEX] make sure there is a dropped version on the map somewhere
  940. if (p1 == imageindex_i_ctf1d)
  941. {
  942. e = G_FindByString<&edict_t::classname>(e, "item_flag_team1");
  943. if (e == nullptr)
  944. {
  945. CTFResetFlag(CTF_TEAM1);
  946. gi.LocBroadcast_Print(PRINT_HIGH, "$g_flag_returned",
  947. CTFTeamName(CTF_TEAM1));
  948. gi.sound(ent, CHAN_RELIABLE | CHAN_NO_PHS_ADD | CHAN_AUX, gi.soundindex("ctf/flagret.wav"), 1, ATTN_NONE, 0);
  949. }
  950. }
  951. }
  952. else if (e->spawnflags.has(SPAWNFLAG_ITEM_DROPPED))
  953. p1 = imageindex_i_ctf1d; // must be dropped
  954. }
  955. p2 = imageindex_i_ctf2;
  956. e = G_FindByString<&edict_t::classname>(nullptr, "item_flag_team2");
  957. if (e != nullptr)
  958. {
  959. if (e->solid == SOLID_NOT)
  960. {
  961. // not at base
  962. // check if on player
  963. p2 = imageindex_i_ctf2d; // default to dropped
  964. for (i = 1; i <= game.maxclients; i++)
  965. if (g_edicts[i].inuse &&
  966. g_edicts[i].client->pers.inventory[IT_FLAG2])
  967. {
  968. // enemy has it
  969. p2 = imageindex_i_ctf2t;
  970. break;
  971. }
  972. // [Paril-KEX] make sure there is a dropped version on the map somewhere
  973. if (p2 == imageindex_i_ctf2d)
  974. {
  975. e = G_FindByString<&edict_t::classname>(e, "item_flag_team2");
  976. if (e == nullptr)
  977. {
  978. CTFResetFlag(CTF_TEAM2);
  979. gi.LocBroadcast_Print(PRINT_HIGH, "$g_flag_returned",
  980. CTFTeamName(CTF_TEAM2));
  981. gi.sound(ent, CHAN_RELIABLE | CHAN_NO_PHS_ADD | CHAN_AUX, gi.soundindex("ctf/flagret.wav"), 1, ATTN_NONE, 0);
  982. }
  983. }
  984. }
  985. else if (e->spawnflags.has(SPAWNFLAG_ITEM_DROPPED))
  986. p2 = imageindex_i_ctf2d; // must be dropped
  987. }
  988. ent->client->ps.stats[STAT_CTF_TEAM1_PIC] = p1;
  989. ent->client->ps.stats[STAT_CTF_TEAM2_PIC] = p2;
  990. if (ctfgame.last_flag_capture && level.time - ctfgame.last_flag_capture < 5_sec)
  991. {
  992. if (ctfgame.last_capture_team == CTF_TEAM1)
  993. if (blink)
  994. ent->client->ps.stats[STAT_CTF_TEAM1_PIC] = p1;
  995. else
  996. ent->client->ps.stats[STAT_CTF_TEAM1_PIC] = 0;
  997. else if (blink)
  998. ent->client->ps.stats[STAT_CTF_TEAM2_PIC] = p2;
  999. else
  1000. ent->client->ps.stats[STAT_CTF_TEAM2_PIC] = 0;
  1001. }
  1002. ent->client->ps.stats[STAT_CTF_TEAM1_CAPS] = ctfgame.team1;
  1003. ent->client->ps.stats[STAT_CTF_TEAM2_CAPS] = ctfgame.team2;
  1004. ent->client->ps.stats[STAT_CTF_FLAG_PIC] = 0;
  1005. if (ent->client->resp.ctf_team == CTF_TEAM1 &&
  1006. ent->client->pers.inventory[IT_FLAG2] &&
  1007. (blink))
  1008. ent->client->ps.stats[STAT_CTF_FLAG_PIC] = imageindex_i_ctf2;
  1009. else if (ent->client->resp.ctf_team == CTF_TEAM2 &&
  1010. ent->client->pers.inventory[IT_FLAG1] &&
  1011. (blink))
  1012. ent->client->ps.stats[STAT_CTF_FLAG_PIC] = imageindex_i_ctf1;
  1013. }
  1014. else
  1015. {
  1016. ent->client->ps.stats[STAT_CTF_TEAM1_PIC] = imageindex_i_ctf1;
  1017. ent->client->ps.stats[STAT_CTF_TEAM2_PIC] = imageindex_i_ctf2;
  1018. ent->client->ps.stats[STAT_CTF_TEAM1_CAPS] = ctfgame.total1;
  1019. ent->client->ps.stats[STAT_CTF_TEAM2_CAPS] = ctfgame.total2;
  1020. }
  1021. ent->client->ps.stats[STAT_CTF_JOINED_TEAM1_PIC] = 0;
  1022. ent->client->ps.stats[STAT_CTF_JOINED_TEAM2_PIC] = 0;
  1023. if (ent->client->resp.ctf_team == CTF_TEAM1)
  1024. ent->client->ps.stats[STAT_CTF_JOINED_TEAM1_PIC] = imageindex_i_ctfj;
  1025. else if (ent->client->resp.ctf_team == CTF_TEAM2)
  1026. ent->client->ps.stats[STAT_CTF_JOINED_TEAM2_PIC] = imageindex_i_ctfj;
  1027. if (ent->client->resp.id_state)
  1028. CTFSetIDView(ent);
  1029. else
  1030. {
  1031. ent->client->ps.stats[STAT_CTF_ID_VIEW] = 0;
  1032. ent->client->ps.stats[STAT_CTF_ID_VIEW_COLOR] = 0;
  1033. }
  1034. }
  1035. /*------------------------------------------------------------------------*/
  1036. /*QUAKED info_player_team1 (1 0 0) (-16 -16 -24) (16 16 32)
  1037. potential team1 spawning position for ctf games
  1038. */
  1039. void SP_info_player_team1(edict_t *self)
  1040. {
  1041. }
  1042. /*QUAKED info_player_team2 (0 0 1) (-16 -16 -24) (16 16 32)
  1043. potential team2 spawning position for ctf games
  1044. */
  1045. void SP_info_player_team2(edict_t *self)
  1046. {
  1047. }
  1048. /*------------------------------------------------------------------------*/
  1049. /* GRAPPLE */
  1050. /*------------------------------------------------------------------------*/
  1051. // ent is player
  1052. void CTFPlayerResetGrapple(edict_t *ent)
  1053. {
  1054. if (ent->client && ent->client->ctf_grapple)
  1055. CTFResetGrapple(ent->client->ctf_grapple);
  1056. }
  1057. // self is grapple, not player
  1058. void CTFResetGrapple(edict_t *self)
  1059. {
  1060. if (!self->owner->client->ctf_grapple)
  1061. return;
  1062. gi.sound(self->owner, CHAN_WEAPON, gi.soundindex("weapons/grapple/grreset.wav"), self->owner->client->silencer_shots ? 0.2f : 1.0f, ATTN_NORM, 0);
  1063. gclient_t *cl;
  1064. cl = self->owner->client;
  1065. cl->ctf_grapple = nullptr;
  1066. cl->ctf_grapplereleasetime = level.time + 1_sec;
  1067. cl->ctf_grapplestate = CTF_GRAPPLE_STATE_FLY; // we're firing, not on hook
  1068. self->owner->flags &= ~FL_NO_KNOCKBACK;
  1069. G_FreeEdict(self);
  1070. }
  1071. TOUCH(CTFGrappleTouch) (edict_t *self, edict_t *other, const trace_t &tr, bool other_touching_self) -> void
  1072. {
  1073. float volume = 1.0;
  1074. if (other == self->owner)
  1075. return;
  1076. if (self->owner->client->ctf_grapplestate != CTF_GRAPPLE_STATE_FLY)
  1077. return;
  1078. if (tr.surface && (tr.surface->flags & SURF_SKY))
  1079. {
  1080. CTFResetGrapple(self);
  1081. return;
  1082. }
  1083. self->velocity = {};
  1084. PlayerNoise(self->owner, self->s.origin, PNOISE_IMPACT);
  1085. if (other->takedamage)
  1086. {
  1087. if (self->dmg)
  1088. T_Damage(other, self, self->owner, self->velocity, self->s.origin, tr.plane.normal, self->dmg, 1, DAMAGE_NONE, MOD_GRAPPLE);
  1089. CTFResetGrapple(self);
  1090. return;
  1091. }
  1092. self->owner->client->ctf_grapplestate = CTF_GRAPPLE_STATE_PULL; // we're on hook
  1093. self->enemy = other;
  1094. self->solid = SOLID_NOT;
  1095. if (self->owner->client->silencer_shots)
  1096. volume = 0.2f;
  1097. gi.sound(self, CHAN_WEAPON, gi.soundindex("weapons/grapple/grhit.wav"), volume, ATTN_NORM, 0);
  1098. self->s.sound = gi.soundindex("weapons/grapple/grpull.wav");
  1099. gi.WriteByte(svc_temp_entity);
  1100. gi.WriteByte(TE_SPARKS);
  1101. gi.WritePosition(self->s.origin);
  1102. gi.WriteDir(tr.plane.normal);
  1103. gi.multicast(self->s.origin, MULTICAST_PVS, false);
  1104. }
  1105. // draw beam between grapple and self
  1106. void CTFGrappleDrawCable(edict_t *self)
  1107. {
  1108. if (self->owner->client->ctf_grapplestate == CTF_GRAPPLE_STATE_HANG)
  1109. return;
  1110. vec3_t start, dir;
  1111. P_ProjectSource(self->owner, self->owner->client->v_angle, { 7, 2, -9 }, start, dir);
  1112. gi.WriteByte(svc_temp_entity);
  1113. gi.WriteByte(TE_GRAPPLE_CABLE_2);
  1114. gi.WriteEntity(self->owner);
  1115. gi.WritePosition(start);
  1116. gi.WritePosition(self->s.origin);
  1117. gi.multicast(self->s.origin, MULTICAST_PVS, false);
  1118. }
  1119. void SV_AddGravity(edict_t *ent);
  1120. // pull the player toward the grapple
  1121. void CTFGrapplePull(edict_t *self)
  1122. {
  1123. vec3_t hookdir, v;
  1124. float vlen;
  1125. if (self->owner->client->pers.weapon && self->owner->client->pers.weapon->id == IT_WEAPON_GRAPPLE &&
  1126. !(self->owner->client->newweapon || ((self->owner->client->latched_buttons | self->owner->client->buttons) & BUTTON_HOLSTER)) &&
  1127. self->owner->client->weaponstate != WEAPON_FIRING &&
  1128. self->owner->client->weaponstate != WEAPON_ACTIVATING)
  1129. {
  1130. if (!self->owner->client->newweapon)
  1131. self->owner->client->newweapon = self->owner->client->pers.weapon;
  1132. CTFResetGrapple(self);
  1133. return;
  1134. }
  1135. if (self->enemy)
  1136. {
  1137. if (self->enemy->solid == SOLID_NOT)
  1138. {
  1139. CTFResetGrapple(self);
  1140. return;
  1141. }
  1142. if (self->enemy->solid == SOLID_BBOX)
  1143. {
  1144. v = self->enemy->size * 0.5f;
  1145. v += self->enemy->s.origin;
  1146. self->s.origin = v + self->enemy->mins;
  1147. gi.linkentity(self);
  1148. }
  1149. else
  1150. self->velocity = self->enemy->velocity;
  1151. if (self->enemy->deadflag)
  1152. { // he died
  1153. CTFResetGrapple(self);
  1154. return;
  1155. }
  1156. }
  1157. CTFGrappleDrawCable(self);
  1158. if (self->owner->client->ctf_grapplestate > CTF_GRAPPLE_STATE_FLY)
  1159. {
  1160. // pull player toward grapple
  1161. vec3_t forward, up;
  1162. AngleVectors(self->owner->client->v_angle, forward, nullptr, up);
  1163. v = self->owner->s.origin;
  1164. v[2] += self->owner->viewheight;
  1165. hookdir = self->s.origin - v;
  1166. vlen = hookdir.length();
  1167. if (self->owner->client->ctf_grapplestate == CTF_GRAPPLE_STATE_PULL &&
  1168. vlen < 64)
  1169. {
  1170. self->owner->client->ctf_grapplestate = CTF_GRAPPLE_STATE_HANG;
  1171. self->s.sound = gi.soundindex("weapons/grapple/grhang.wav");
  1172. }
  1173. hookdir.normalize();
  1174. hookdir = hookdir * g_grapple_pull_speed->value;
  1175. self->owner->velocity = hookdir;
  1176. self->owner->flags |= FL_NO_KNOCKBACK;
  1177. SV_AddGravity(self->owner);
  1178. }
  1179. }
  1180. DIE(grapple_die) (edict_t *self, edict_t *other, edict_t *inflictor, int damage, const vec3_t &point, const mod_t &mod) -> void
  1181. {
  1182. if (mod.id == MOD_CRUSH)
  1183. CTFResetGrapple(self);
  1184. }
  1185. bool CTFFireGrapple(edict_t *self, const vec3_t &start, const vec3_t &dir, int damage, int speed, effects_t effect)
  1186. {
  1187. edict_t *grapple;
  1188. trace_t tr;
  1189. vec3_t normalized = dir.normalized();
  1190. grapple = G_Spawn();
  1191. grapple->s.origin = start;
  1192. grapple->s.old_origin = start;
  1193. grapple->s.angles = vectoangles(normalized);
  1194. grapple->velocity = normalized * speed;
  1195. grapple->movetype = MOVETYPE_FLYMISSILE;
  1196. grapple->clipmask = MASK_PROJECTILE;
  1197. // [Paril-KEX]
  1198. if (self->client && !G_ShouldPlayersCollide(true))
  1199. grapple->clipmask &= ~CONTENTS_PLAYER;
  1200. grapple->solid = SOLID_BBOX;
  1201. grapple->s.effects |= effect;
  1202. grapple->s.modelindex = gi.modelindex("models/weapons/grapple/hook/tris.md2");
  1203. grapple->owner = self;
  1204. grapple->touch = CTFGrappleTouch;
  1205. grapple->dmg = damage;
  1206. grapple->flags |= FL_NO_KNOCKBACK | FL_NO_DAMAGE_EFFECTS;
  1207. grapple->takedamage = true;
  1208. grapple->die = grapple_die;
  1209. self->client->ctf_grapple = grapple;
  1210. self->client->ctf_grapplestate = CTF_GRAPPLE_STATE_FLY; // we're firing, not on hook
  1211. gi.linkentity(grapple);
  1212. tr = gi.traceline(self->s.origin, grapple->s.origin, grapple, grapple->clipmask);
  1213. if (tr.fraction < 1.0f)
  1214. {
  1215. grapple->s.origin = tr.endpos + (tr.plane.normal * 1.f);
  1216. grapple->touch(grapple, tr.ent, tr, false);
  1217. return false;
  1218. }
  1219. grapple->s.sound = gi.soundindex("weapons/grapple/grfly.wav");
  1220. return true;
  1221. }
  1222. void CTFGrappleFire(edict_t *ent, const vec3_t &g_offset, int damage, effects_t effect)
  1223. {
  1224. float volume = 1.0;
  1225. if (ent->client->ctf_grapplestate > CTF_GRAPPLE_STATE_FLY)
  1226. return; // it's already out
  1227. vec3_t start, dir;
  1228. P_ProjectSource(ent, ent->client->v_angle, vec3_t{ 24, 8, -8 + 2 } + g_offset, start, dir);
  1229. if (ent->client->silencer_shots)
  1230. volume = 0.2f;
  1231. if (CTFFireGrapple(ent, start, dir, damage, g_grapple_fly_speed->value, effect))
  1232. gi.sound(ent, CHAN_WEAPON, gi.soundindex("weapons/grapple/grfire.wav"), volume, ATTN_NORM, 0);
  1233. PlayerNoise(ent, start, PNOISE_WEAPON);
  1234. }
  1235. void CTFWeapon_Grapple_Fire(edict_t *ent)
  1236. {
  1237. CTFGrappleFire(ent, vec3_origin, g_grapple_damage->integer, EF_NONE);
  1238. }
  1239. void CTFWeapon_Grapple(edict_t *ent)
  1240. {
  1241. constexpr int pause_frames[] = { 10, 18, 27, 0 };
  1242. constexpr int fire_frames[] = { 6, 0 };
  1243. int prevstate;
  1244. // if the the attack button is still down, stay in the firing frame
  1245. if ((ent->client->buttons & (BUTTON_ATTACK | BUTTON_HOLSTER)) &&
  1246. ent->client->weaponstate == WEAPON_FIRING &&
  1247. ent->client->ctf_grapple)
  1248. ent->client->ps.gunframe = 6;
  1249. if (!(ent->client->buttons & (BUTTON_ATTACK | BUTTON_HOLSTER)) &&
  1250. ent->client->ctf_grapple)
  1251. {
  1252. CTFResetGrapple(ent->client->ctf_grapple);
  1253. if (ent->client->weaponstate == WEAPON_FIRING)
  1254. ent->client->weaponstate = WEAPON_READY;
  1255. }
  1256. if ((ent->client->newweapon || ((ent->client->latched_buttons | ent->client->buttons) & BUTTON_HOLSTER)) &&
  1257. ent->client->ctf_grapplestate > CTF_GRAPPLE_STATE_FLY &&
  1258. ent->client->weaponstate == WEAPON_FIRING)
  1259. {
  1260. // he wants to change weapons while grappled
  1261. if (!ent->client->newweapon)
  1262. ent->client->newweapon = ent->client->pers.weapon;
  1263. ent->client->weaponstate = WEAPON_DROPPING;
  1264. ent->client->ps.gunframe = 32;
  1265. }
  1266. prevstate = ent->client->weaponstate;
  1267. Weapon_Generic(ent, 5, 10, 31, 36, pause_frames, fire_frames,
  1268. CTFWeapon_Grapple_Fire);
  1269. // if the the attack button is still down, stay in the firing frame
  1270. if ((ent->client->buttons & (BUTTON_ATTACK | BUTTON_HOLSTER)) &&
  1271. ent->client->weaponstate == WEAPON_FIRING &&
  1272. ent->client->ctf_grapple)
  1273. ent->client->ps.gunframe = 6;
  1274. // if we just switched back to grapple, immediately go to fire frame
  1275. if (prevstate == WEAPON_ACTIVATING &&
  1276. ent->client->weaponstate == WEAPON_READY &&
  1277. ent->client->ctf_grapplestate > CTF_GRAPPLE_STATE_FLY)
  1278. {
  1279. if (!(ent->client->buttons & (BUTTON_ATTACK | BUTTON_HOLSTER)))
  1280. ent->client->ps.gunframe = 6;
  1281. else
  1282. ent->client->ps.gunframe = 5;
  1283. ent->client->weaponstate = WEAPON_FIRING;
  1284. }
  1285. }
  1286. void CTFDirtyTeamMenu()
  1287. {
  1288. for (auto player : active_players())
  1289. if (player->client->menu)
  1290. {
  1291. player->client->menudirty = true;
  1292. player->client->menutime = level.time;
  1293. }
  1294. }
  1295. void CTFTeam_f(edict_t *ent)
  1296. {
  1297. if (!G_TeamplayEnabled())
  1298. return;
  1299. const char *t;
  1300. ctfteam_t desired_team;
  1301. t = gi.args();
  1302. if (!*t)
  1303. {
  1304. gi.LocClient_Print(ent, PRINT_HIGH, "$g_you_are_on_team",
  1305. CTFTeamName(ent->client->resp.ctf_team));
  1306. return;
  1307. }
  1308. if (ctfgame.match > MATCH_SETUP)
  1309. {
  1310. gi.LocClient_Print(ent, PRINT_HIGH, "$g_cant_change_teams");
  1311. return;
  1312. }
  1313. // [Paril-KEX] with force-join, don't allow us to switch
  1314. // using this command.
  1315. if (g_teamplay_force_join->integer)
  1316. {
  1317. if (!(ent->svflags & SVF_BOT))
  1318. {
  1319. gi.LocClient_Print(ent, PRINT_HIGH, "$g_cant_change_teams");
  1320. return;
  1321. }
  1322. }
  1323. if (Q_strcasecmp(t, "red") == 0)
  1324. desired_team = CTF_TEAM1;
  1325. else if (Q_strcasecmp(t, "blue") == 0)
  1326. desired_team = CTF_TEAM2;
  1327. else
  1328. {
  1329. gi.LocClient_Print(ent, PRINT_HIGH, "$g_unknown_team", t);
  1330. return;
  1331. }
  1332. if (ent->client->resp.ctf_team == desired_team)
  1333. {
  1334. gi.LocClient_Print(ent, PRINT_HIGH, "$g_already_on_team",
  1335. CTFTeamName(ent->client->resp.ctf_team));
  1336. return;
  1337. }
  1338. ////
  1339. ent->svflags = SVF_NONE;
  1340. ent->flags &= ~FL_GODMODE;
  1341. ent->client->resp.ctf_team = desired_team;
  1342. ent->client->resp.ctf_state = 0;
  1343. char value[MAX_INFO_VALUE] = { 0 };
  1344. gi.Info_ValueForKey(ent->client->pers.userinfo, "skin", value, sizeof(value));
  1345. CTFAssignSkin(ent, value);
  1346. // if anybody has a menu open, update it immediately
  1347. CTFDirtyTeamMenu();
  1348. if (ent->solid == SOLID_NOT)
  1349. {
  1350. // spectator
  1351. PutClientInServer(ent);
  1352. G_PostRespawn(ent);
  1353. gi.LocBroadcast_Print(PRINT_HIGH, "$g_joined_team",
  1354. ent->client->pers.netname, CTFTeamName(desired_team));
  1355. return;
  1356. }
  1357. ent->health = 0;
  1358. player_die(ent, ent, ent, 100000, vec3_origin, { MOD_SUICIDE, true });
  1359. // don't even bother waiting for death frames
  1360. ent->deadflag = true;
  1361. respawn(ent);
  1362. ent->client->resp.score = 0;
  1363. gi.LocBroadcast_Print(PRINT_HIGH, "$g_changed_team",
  1364. ent->client->pers.netname, CTFTeamName(desired_team));
  1365. }
  1366. constexpr size_t MAX_CTF_STAT_LENGTH = 1024;
  1367. /*
  1368. ==================
  1369. CTFScoreboardMessage
  1370. ==================
  1371. */
  1372. void CTFScoreboardMessage(edict_t *ent, edict_t *killer)
  1373. {
  1374. uint32_t i, j, k, n;
  1375. uint32_t sorted[2][MAX_CLIENTS];
  1376. int32_t sortedscores[2][MAX_CLIENTS];
  1377. int score;
  1378. uint32_t total[2];
  1379. int totalscore[2];
  1380. uint32_t last[2];
  1381. gclient_t *cl;
  1382. edict_t *cl_ent;
  1383. int team;
  1384. // sort the clients by team and score
  1385. total[0] = total[1] = 0;
  1386. last[0] = last[1] = 0;
  1387. totalscore[0] = totalscore[1] = 0;
  1388. for (i = 0; i < game.maxclients; i++)
  1389. {
  1390. cl_ent = g_edicts + 1 + i;
  1391. if (!cl_ent->inuse)
  1392. continue;
  1393. if (game.clients[i].resp.ctf_team == CTF_TEAM1)
  1394. team = 0;
  1395. else if (game.clients[i].resp.ctf_team == CTF_TEAM2)
  1396. team = 1;
  1397. else
  1398. continue; // unknown team?
  1399. score = game.clients[i].resp.score;
  1400. for (j = 0; j < total[team]; j++)
  1401. {
  1402. if (score > sortedscores[team][j])
  1403. break;
  1404. }
  1405. for (k = total[team]; k > j; k--)
  1406. {
  1407. sorted[team][k] = sorted[team][k - 1];
  1408. sortedscores[team][k] = sortedscores[team][k - 1];
  1409. }
  1410. sorted[team][j] = i;
  1411. sortedscores[team][j] = score;
  1412. totalscore[team] += score;
  1413. total[team]++;
  1414. }
  1415. // print level name and exit rules
  1416. // add the clients in sorted order
  1417. static std::string string;
  1418. string.clear();
  1419. // [Paril-KEX] time & frags
  1420. if (teamplay->integer)
  1421. {
  1422. if (fraglimit->integer)
  1423. {
  1424. fmt::format_to(std::back_inserter(string), FMT_STRING("xv -20 yv -10 loc_string2 1 $g_score_frags \"{}\" "), fraglimit->integer);
  1425. }
  1426. }
  1427. else
  1428. {
  1429. if (capturelimit->integer)
  1430. {
  1431. fmt::format_to(std::back_inserter(string), FMT_STRING("xv -20 yv -10 loc_string2 1 $g_score_captures \"{}\" "), capturelimit->integer);
  1432. }
  1433. }
  1434. if (timelimit->value)
  1435. {
  1436. 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);
  1437. }
  1438. // team one
  1439. if (teamplay->integer)
  1440. {
  1441. fmt::format_to(std::back_inserter(string),
  1442. FMT_STRING("if 25 xv -32 yv 8 pic 25 endif "
  1443. "xv -123 yv 28 cstring \"{}\" "
  1444. "xv 41 yv 12 num 3 19 "
  1445. "if 26 xv 208 yv 8 pic 26 endif "
  1446. "xv 117 yv 28 cstring \"{}\" "
  1447. "xv 280 yv 12 num 3 21 "),
  1448. total[0],
  1449. total[1]);
  1450. }
  1451. else
  1452. {
  1453. fmt::format_to(std::back_inserter(string),
  1454. FMT_STRING("if 25 xv -32 yv 8 pic 25 endif "
  1455. "xv 0 yv 28 string \"{:4}/{:<3}\" "
  1456. "xv 58 yv 12 num 2 19 "
  1457. "if 26 xv 208 yv 8 pic 26 endif "
  1458. "xv 240 yv 28 string \"{:4}/{:<3}\" "
  1459. "xv 296 yv 12 num 2 21 "),
  1460. totalscore[0], total[0],
  1461. totalscore[1], total[1]);
  1462. }
  1463. for (i = 0; i < 16; i++)
  1464. {
  1465. if (i >= total[0] && i >= total[1])
  1466. break; // we're done
  1467. // left side
  1468. if (i < total[0])
  1469. {
  1470. cl = &game.clients[sorted[0][i]];
  1471. cl_ent = g_edicts + 1 + sorted[0][i];
  1472. std::string_view entry = G_Fmt("ctf -40 {} {} {} {} {} ",
  1473. 42 + i * 8,
  1474. sorted[0][i],
  1475. cl->resp.score,
  1476. cl->ping > 999 ? 999 : cl->ping,
  1477. cl_ent->client->pers.inventory[IT_FLAG2] ? "sbfctf2" : "\"\"");
  1478. if (string.size() + entry.size() < MAX_CTF_STAT_LENGTH)
  1479. {
  1480. string += entry;
  1481. last[0] = i;
  1482. }
  1483. }
  1484. // right side
  1485. if (i < total[1])
  1486. {
  1487. cl = &game.clients[sorted[1][i]];
  1488. cl_ent = g_edicts + 1 + sorted[1][i];
  1489. std::string_view entry = G_Fmt("ctf 200 {} {} {} {} {} ",
  1490. 42 + i * 8,
  1491. sorted[1][i],
  1492. cl->resp.score,
  1493. cl->ping > 999 ? 999 : cl->ping,
  1494. cl_ent->client->pers.inventory[IT_FLAG1] ? "sbfctf1" : "\"\"");
  1495. if (string.size() + entry.size() < MAX_CTF_STAT_LENGTH)
  1496. {
  1497. string += entry;
  1498. last[1] = i;
  1499. }
  1500. }
  1501. }
  1502. // put in spectators if we have enough room
  1503. if (last[0] > last[1])
  1504. j = last[0];
  1505. else
  1506. j = last[1];
  1507. j = (j + 2) * 8 + 42;
  1508. k = n = 0;
  1509. if (string.size() < MAX_CTF_STAT_LENGTH - 50)
  1510. {
  1511. for (i = 0; i < game.maxclients; i++)
  1512. {
  1513. cl_ent = g_edicts + 1 + i;
  1514. cl = &game.clients[i];
  1515. if (!cl_ent->inuse ||
  1516. cl_ent->solid != SOLID_NOT ||
  1517. cl_ent->client->resp.ctf_team != CTF_NOTEAM)
  1518. continue;
  1519. if (!k)
  1520. {
  1521. k = 1;
  1522. fmt::format_to(std::back_inserter(string), FMT_STRING("xv 0 yv {} loc_string2 0 \"$g_pc_spectators\" "), j);
  1523. j += 8;
  1524. }
  1525. std::string_view entry = G_Fmt("ctf {} {} {} {} {} \"\" ",
  1526. (n & 1) ? 200 : -40, // x
  1527. j, // y
  1528. i, // playernum
  1529. cl->resp.score,
  1530. cl->ping > 999 ? 999 : cl->ping);
  1531. if (string.size() + entry.size() < MAX_CTF_STAT_LENGTH)
  1532. string += entry;
  1533. if (n & 1)
  1534. j += 8;
  1535. n++;
  1536. }
  1537. }
  1538. if (total[0] - last[0] > 1) // couldn't fit everyone
  1539. fmt::format_to(std::back_inserter(string), FMT_STRING("xv -32 yv {} loc_string 1 $g_ctf_and_more {} "),
  1540. 42 + (last[0] + 1) * 8, total[0] - last[0] - 1);
  1541. if (total[1] - last[1] > 1) // couldn't fit everyone
  1542. fmt::format_to(std::back_inserter(string), FMT_STRING("xv 208 yv {} loc_string 1 $g_ctf_and_more {} "),
  1543. 42 + (last[1] + 1) * 8, total[1] - last[1] - 1);
  1544. if (level.intermissiontime)
  1545. 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()));
  1546. gi.WriteByte(svc_layout);
  1547. gi.WriteString(string.c_str());
  1548. }
  1549. /*------------------------------------------------------------------------*/
  1550. /* TECH */
  1551. /*------------------------------------------------------------------------*/
  1552. void CTFHasTech(edict_t *who)
  1553. {
  1554. if (level.time - who->client->ctf_lasttechmsg > 2_sec)
  1555. {
  1556. gi.LocCenter_Print(who, "$g_already_have_tech");
  1557. who->client->ctf_lasttechmsg = level.time;
  1558. }
  1559. }
  1560. gitem_t *CTFWhat_Tech(edict_t *ent)
  1561. {
  1562. int i;
  1563. i = 0;
  1564. for (; i < q_countof(tech_ids); i++)
  1565. {
  1566. if (ent->client->pers.inventory[tech_ids[i]])
  1567. {
  1568. return GetItemByIndex(tech_ids[i]);
  1569. }
  1570. }
  1571. return nullptr;
  1572. }
  1573. bool CTFPickup_Tech(edict_t *ent, edict_t *other)
  1574. {
  1575. int i;
  1576. i = 0;
  1577. for (; i < q_countof(tech_ids); i++)
  1578. {
  1579. if (other->client->pers.inventory[tech_ids[i]])
  1580. {
  1581. CTFHasTech(other);
  1582. return false; // has this one
  1583. }
  1584. }
  1585. // client only gets one tech
  1586. other->client->pers.inventory[ent->item->id]++;
  1587. other->client->ctf_regentime = level.time;
  1588. return true;
  1589. }
  1590. static void SpawnTech(gitem_t *item, edict_t *spot);
  1591. static edict_t *FindTechSpawn()
  1592. {
  1593. return SelectDeathmatchSpawnPoint(false, true, true).spot;
  1594. }
  1595. THINK(TechThink) (edict_t *tech) -> void
  1596. {
  1597. edict_t *spot;
  1598. if ((spot = FindTechSpawn()) != nullptr)
  1599. {
  1600. SpawnTech(tech->item, spot);
  1601. G_FreeEdict(tech);
  1602. }
  1603. else
  1604. {
  1605. tech->nextthink = level.time + CTF_TECH_TIMEOUT;
  1606. tech->think = TechThink;
  1607. }
  1608. }
  1609. void CTFDrop_Tech(edict_t *ent, gitem_t *item)
  1610. {
  1611. edict_t *tech;
  1612. tech = Drop_Item(ent, item);
  1613. tech->nextthink = level.time + CTF_TECH_TIMEOUT;
  1614. tech->think = TechThink;
  1615. ent->client->pers.inventory[item->id] = 0;
  1616. }
  1617. void CTFDeadDropTech(edict_t *ent)
  1618. {
  1619. edict_t *dropped;
  1620. int i;
  1621. i = 0;
  1622. for (; i < q_countof(tech_ids); i++)
  1623. {
  1624. if (ent->client->pers.inventory[tech_ids[i]])
  1625. {
  1626. dropped = Drop_Item(ent, GetItemByIndex(tech_ids[i]));
  1627. // hack the velocity to make it bounce random
  1628. dropped->velocity[0] = crandom_open() * 300;
  1629. dropped->velocity[1] = crandom_open() * 300;
  1630. dropped->nextthink = level.time + CTF_TECH_TIMEOUT;
  1631. dropped->think = TechThink;
  1632. dropped->owner = nullptr;
  1633. ent->client->pers.inventory[tech_ids[i]] = 0;
  1634. }
  1635. }
  1636. }
  1637. static void SpawnTech(gitem_t *item, edict_t *spot)
  1638. {
  1639. edict_t *ent;
  1640. vec3_t forward, right;
  1641. vec3_t angles;
  1642. ent = G_Spawn();
  1643. ent->classname = item->classname;
  1644. ent->item = item;
  1645. ent->spawnflags = SPAWNFLAG_ITEM_DROPPED;
  1646. ent->s.effects = item->world_model_flags;
  1647. ent->s.renderfx = RF_GLOW | RF_NO_LOD;
  1648. ent->mins = { -15, -15, -15 };
  1649. ent->maxs = { 15, 15, 15 };
  1650. gi.setmodel(ent, ent->item->world_model);
  1651. ent->solid = SOLID_TRIGGER;
  1652. ent->movetype = MOVETYPE_TOSS;
  1653. ent->touch = Touch_Item;
  1654. ent->owner = ent;
  1655. angles[0] = 0;
  1656. angles[1] = (float) irandom(360);
  1657. angles[2] = 0;
  1658. AngleVectors(angles, forward, right, nullptr);
  1659. ent->s.origin = spot->s.origin;
  1660. ent->s.origin[2] += 16;
  1661. ent->velocity = forward * 100;
  1662. ent->velocity[2] = 300;
  1663. ent->nextthink = level.time + CTF_TECH_TIMEOUT;
  1664. ent->think = TechThink;
  1665. gi.linkentity(ent);
  1666. }
  1667. THINK(SpawnTechs) (edict_t *ent) -> void
  1668. {
  1669. edict_t *spot;
  1670. int i;
  1671. i = 0;
  1672. for (; i < q_countof(tech_ids); i++)
  1673. {
  1674. if ((spot = FindTechSpawn()) != nullptr)
  1675. SpawnTech(GetItemByIndex(tech_ids[i]), spot);
  1676. }
  1677. if (ent)
  1678. G_FreeEdict(ent);
  1679. }
  1680. // frees the passed edict!
  1681. void CTFRespawnTech(edict_t *ent)
  1682. {
  1683. edict_t *spot;
  1684. if ((spot = FindTechSpawn()) != nullptr)
  1685. SpawnTech(ent->item, spot);
  1686. G_FreeEdict(ent);
  1687. }
  1688. void CTFSetupTechSpawn()
  1689. {
  1690. edict_t *ent;
  1691. bool techs_allowed;
  1692. // [Paril-KEX]
  1693. if (!strcmp(g_allow_techs->string, "auto"))
  1694. techs_allowed = !!ctf->integer;
  1695. else
  1696. techs_allowed = !!g_allow_techs->integer;
  1697. if (!techs_allowed)
  1698. return;
  1699. ent = G_Spawn();
  1700. ent->nextthink = level.time + 2_sec;
  1701. ent->think = SpawnTechs;
  1702. }
  1703. void CTFResetTech()
  1704. {
  1705. edict_t *ent;
  1706. uint32_t i;
  1707. for (ent = g_edicts + 1, i = 1; i < globals.num_edicts; i++, ent++)
  1708. {
  1709. if (ent->inuse)
  1710. if (ent->item && (ent->item->flags & IF_TECH))
  1711. G_FreeEdict(ent);
  1712. }
  1713. SpawnTechs(nullptr);
  1714. }
  1715. int CTFApplyResistance(edict_t *ent, int dmg)
  1716. {
  1717. float volume = 1.0;
  1718. if (ent->client && ent->client->silencer_shots)
  1719. volume = 0.2f;
  1720. if (dmg && ent->client && ent->client->pers.inventory[IT_TECH_RESISTANCE])
  1721. {
  1722. // make noise
  1723. gi.sound(ent, CHAN_AUX, gi.soundindex("ctf/tech1.wav"), volume, ATTN_NORM, 0);
  1724. return dmg / 2;
  1725. }
  1726. return dmg;
  1727. }
  1728. int CTFApplyStrength(edict_t *ent, int dmg)
  1729. {
  1730. if (dmg && ent->client && ent->client->pers.inventory[IT_TECH_STRENGTH])
  1731. {
  1732. return dmg * 2;
  1733. }
  1734. return dmg;
  1735. }
  1736. bool CTFApplyStrengthSound(edict_t *ent)
  1737. {
  1738. float volume = 1.0;
  1739. if (ent->client && ent->client->silencer_shots)
  1740. volume = 0.2f;
  1741. if (ent->client &&
  1742. ent->client->pers.inventory[IT_TECH_STRENGTH])
  1743. {
  1744. if (ent->client->ctf_techsndtime < level.time)
  1745. {
  1746. ent->client->ctf_techsndtime = level.time + 1_sec;
  1747. if (ent->client->quad_time > level.time)
  1748. gi.sound(ent, CHAN_AUX, gi.soundindex("ctf/tech2x.wav"), volume, ATTN_NORM, 0);
  1749. else
  1750. gi.sound(ent, CHAN_AUX, gi.soundindex("ctf/tech2.wav"), volume, ATTN_NORM, 0);
  1751. }
  1752. return true;
  1753. }
  1754. return false;
  1755. }
  1756. bool CTFApplyHaste(edict_t *ent)
  1757. {
  1758. if (ent->client &&
  1759. ent->client->pers.inventory[IT_TECH_HASTE])
  1760. return true;
  1761. return false;
  1762. }
  1763. void CTFApplyHasteSound(edict_t *ent)
  1764. {
  1765. float volume = 1.0;
  1766. if (ent->client && ent->client->silencer_shots)
  1767. volume = 0.2f;
  1768. if (ent->client &&
  1769. ent->client->pers.inventory[IT_TECH_HASTE] &&
  1770. ent->client->ctf_techsndtime < level.time)
  1771. {
  1772. ent->client->ctf_techsndtime = level.time + 1_sec;
  1773. gi.sound(ent, CHAN_AUX, gi.soundindex("ctf/tech3.wav"), volume, ATTN_NORM, 0);
  1774. }
  1775. }
  1776. void CTFApplyRegeneration(edict_t *ent)
  1777. {
  1778. bool noise = false;
  1779. gclient_t *client;
  1780. int index;
  1781. float volume = 1.0;
  1782. client = ent->client;
  1783. if (!client)
  1784. return;
  1785. if (ent->client->silencer_shots)
  1786. volume = 0.2f;
  1787. if (client->pers.inventory[IT_TECH_REGENERATION])
  1788. {
  1789. if (client->ctf_regentime < level.time)
  1790. {
  1791. client->ctf_regentime = level.time;
  1792. if (ent->health < 150)
  1793. {
  1794. ent->health += 5;
  1795. if (ent->health > 150)
  1796. ent->health = 150;
  1797. client->ctf_regentime += 500_ms;
  1798. noise = true;
  1799. }
  1800. index = ArmorIndex(ent);
  1801. if (index && client->pers.inventory[index] < 150)
  1802. {
  1803. client->pers.inventory[index] += 5;
  1804. if (client->pers.inventory[index] > 150)
  1805. client->pers.inventory[index] = 150;
  1806. client->ctf_regentime += 500_ms;
  1807. noise = true;
  1808. }
  1809. }
  1810. if (noise && ent->client->ctf_techsndtime < level.time)
  1811. {
  1812. ent->client->ctf_techsndtime = level.time + 1_sec;
  1813. gi.sound(ent, CHAN_AUX, gi.soundindex("ctf/tech4.wav"), volume, ATTN_NORM, 0);
  1814. }
  1815. }
  1816. }
  1817. bool CTFHasRegeneration(edict_t *ent)
  1818. {
  1819. if (ent->client &&
  1820. ent->client->pers.inventory[IT_TECH_REGENERATION])
  1821. return true;
  1822. return false;
  1823. }
  1824. void CTFSay_Team(edict_t *who, const char *msg_in)
  1825. {
  1826. edict_t *cl_ent;
  1827. char outmsg[256];
  1828. if (CheckFlood(who))
  1829. return;
  1830. Q_strlcpy(outmsg, msg_in, sizeof(outmsg));
  1831. char *msg = outmsg;
  1832. if (*msg == '\"')
  1833. {
  1834. msg[strlen(msg) - 1] = 0;
  1835. msg++;
  1836. }
  1837. for (uint32_t i = 0; i < game.maxclients; i++)
  1838. {
  1839. cl_ent = g_edicts + 1 + i;
  1840. if (!cl_ent->inuse)
  1841. continue;
  1842. if (cl_ent->client->resp.ctf_team == who->client->resp.ctf_team)
  1843. gi.LocClient_Print(cl_ent, PRINT_CHAT, "({}): {}\n",
  1844. who->client->pers.netname, msg);
  1845. }
  1846. }
  1847. /*-----------------------------------------------------------------------*/
  1848. /*QUAKED misc_ctf_banner (1 .5 0) (-4 -64 0) (4 64 248) TEAM2
  1849. The origin is the bottom of the banner.
  1850. The banner is 248 tall.
  1851. */
  1852. THINK(misc_ctf_banner_think) (edict_t *ent) -> void
  1853. {
  1854. ent->s.frame = (ent->s.frame + 1) % 16;
  1855. ent->nextthink = level.time + 10_hz;
  1856. }
  1857. constexpr spawnflags_t SPAWNFLAG_CTF_BANNER_BLUE = 1_spawnflag;
  1858. void SP_misc_ctf_banner(edict_t *ent)
  1859. {
  1860. ent->movetype = MOVETYPE_NONE;
  1861. ent->solid = SOLID_NOT;
  1862. ent->s.modelindex = gi.modelindex("models/ctf/banner/tris.md2");
  1863. if (ent->spawnflags.has(SPAWNFLAG_CTF_BANNER_BLUE)) // team2
  1864. ent->s.skinnum = 1;
  1865. ent->s.frame = irandom(16);
  1866. gi.linkentity(ent);
  1867. ent->think = misc_ctf_banner_think;
  1868. ent->nextthink = level.time + 10_hz;
  1869. }
  1870. /*QUAKED misc_ctf_small_banner (1 .5 0) (-4 -32 0) (4 32 124) TEAM2
  1871. The origin is the bottom of the banner.
  1872. The banner is 124 tall.
  1873. */
  1874. void SP_misc_ctf_small_banner(edict_t *ent)
  1875. {
  1876. ent->movetype = MOVETYPE_NONE;
  1877. ent->solid = SOLID_NOT;
  1878. ent->s.modelindex = gi.modelindex("models/ctf/banner/small.md2");
  1879. if (ent->spawnflags.has(SPAWNFLAG_CTF_BANNER_BLUE)) // team2
  1880. ent->s.skinnum = 1;
  1881. ent->s.frame = irandom(16);
  1882. gi.linkentity(ent);
  1883. ent->think = misc_ctf_banner_think;
  1884. ent->nextthink = level.time + 10_hz;
  1885. }
  1886. /*-----------------------------------------------------------------------*/
  1887. static void SetGameName(pmenu_t *p)
  1888. {
  1889. if (ctf->integer)
  1890. Q_strlcpy(p->text, "$g_pc_3wctf", sizeof(p->text));
  1891. else
  1892. Q_strlcpy(p->text, "$g_pc_teamplay", sizeof(p->text));
  1893. }
  1894. static void SetLevelName(pmenu_t *p)
  1895. {
  1896. static char levelname[33];
  1897. levelname[0] = '*';
  1898. if (g_edicts[0].message)
  1899. Q_strlcpy(levelname + 1, g_edicts[0].message, sizeof(levelname) - 1);
  1900. else
  1901. Q_strlcpy(levelname + 1, level.mapname, sizeof(levelname) - 1);
  1902. levelname[sizeof(levelname) - 1] = 0;
  1903. Q_strlcpy(p->text, levelname, sizeof(p->text));
  1904. }
  1905. /*-----------------------------------------------------------------------*/
  1906. /* ELECTIONS */
  1907. bool CTFBeginElection(edict_t *ent, elect_t type, const char *msg)
  1908. {
  1909. int count;
  1910. edict_t *e;
  1911. if (electpercentage->value == 0)
  1912. {
  1913. gi.LocClient_Print(ent, PRINT_HIGH, "Elections are disabled, only an admin can process this action.\n");
  1914. return false;
  1915. }
  1916. if (ctfgame.election != ELECT_NONE)
  1917. {
  1918. gi.LocClient_Print(ent, PRINT_HIGH, "Election already in progress.\n");
  1919. return false;
  1920. }
  1921. // clear votes
  1922. count = 0;
  1923. for (uint32_t i = 1; i <= game.maxclients; i++)
  1924. {
  1925. e = g_edicts + i;
  1926. e->client->resp.voted = false;
  1927. if (e->inuse)
  1928. count++;
  1929. }
  1930. if (count < 2)
  1931. {
  1932. gi.LocClient_Print(ent, PRINT_HIGH, "Not enough players for election.\n");
  1933. return false;
  1934. }
  1935. ctfgame.etarget = ent;
  1936. ctfgame.election = type;
  1937. ctfgame.evotes = 0;
  1938. ctfgame.needvotes = (int) ((count * electpercentage->value) / 100);
  1939. ctfgame.electtime = level.time + 20_sec; // twenty seconds for election
  1940. Q_strlcpy(ctfgame.emsg, msg, sizeof(ctfgame.emsg));
  1941. // tell everyone
  1942. gi.Broadcast_Print(PRINT_CHAT, ctfgame.emsg);
  1943. gi.LocBroadcast_Print(PRINT_HIGH, "Type YES or NO to vote on this request.\n");
  1944. gi.LocBroadcast_Print(PRINT_HIGH, "Votes: {} Needed: {} Time left: {}s\n", ctfgame.evotes, ctfgame.needvotes,
  1945. (ctfgame.electtime - level.time).seconds<int>());
  1946. return true;
  1947. }
  1948. void DoRespawn(edict_t *ent);
  1949. void CTFResetAllPlayers()
  1950. {
  1951. uint32_t i;
  1952. edict_t *ent;
  1953. for (i = 1; i <= game.maxclients; i++)
  1954. {
  1955. ent = g_edicts + i;
  1956. if (!ent->inuse)
  1957. continue;
  1958. if (ent->client->menu)
  1959. PMenu_Close(ent);
  1960. CTFPlayerResetGrapple(ent);
  1961. CTFDeadDropFlag(ent);
  1962. CTFDeadDropTech(ent);
  1963. ent->client->resp.ctf_team = CTF_NOTEAM;
  1964. ent->client->resp.ready = false;
  1965. ent->svflags = SVF_NONE;
  1966. ent->flags &= ~FL_GODMODE;
  1967. PutClientInServer(ent);
  1968. }
  1969. // reset the level
  1970. CTFResetTech();
  1971. CTFResetFlags();
  1972. for (ent = g_edicts + 1, i = 1; i < globals.num_edicts; i++, ent++)
  1973. {
  1974. if (ent->inuse && !ent->client)
  1975. {
  1976. if (ent->solid == SOLID_NOT && ent->think == DoRespawn &&
  1977. ent->nextthink >= level.time)
  1978. {
  1979. ent->nextthink = 0_ms;
  1980. DoRespawn(ent);
  1981. }
  1982. }
  1983. }
  1984. if (ctfgame.match == MATCH_SETUP)
  1985. ctfgame.matchtime = level.time + gtime_t::from_min(matchsetuptime->value);
  1986. }
  1987. void CTFAssignGhost(edict_t *ent)
  1988. {
  1989. int ghost, i;
  1990. for (ghost = 0; ghost < MAX_CLIENTS; ghost++)
  1991. if (!ctfgame.ghosts[ghost].code)
  1992. break;
  1993. if (ghost == MAX_CLIENTS)
  1994. return;
  1995. ctfgame.ghosts[ghost].team = ent->client->resp.ctf_team;
  1996. ctfgame.ghosts[ghost].score = 0;
  1997. for (;;)
  1998. {
  1999. ctfgame.ghosts[ghost].code = irandom(10000, 100000);
  2000. for (i = 0; i < MAX_CLIENTS; i++)
  2001. if (i != ghost && ctfgame.ghosts[i].code == ctfgame.ghosts[ghost].code)
  2002. break;
  2003. if (i == MAX_CLIENTS)
  2004. break;
  2005. }
  2006. ctfgame.ghosts[ghost].ent = ent;
  2007. Q_strlcpy(ctfgame.ghosts[ghost].netname, ent->client->pers.netname, sizeof(ctfgame.ghosts[ghost].netname));
  2008. ent->client->resp.ghost = ctfgame.ghosts + ghost;
  2009. gi.LocClient_Print(ent, PRINT_CHAT, "Your ghost code is **** {} ****\n", ctfgame.ghosts[ghost].code);
  2010. gi.LocClient_Print(ent, PRINT_HIGH, "If you lose connection, you can rejoin with your score intact by typing \"ghost {}\".\n",
  2011. ctfgame.ghosts[ghost].code);
  2012. }
  2013. // start a match
  2014. void CTFStartMatch()
  2015. {
  2016. edict_t *ent;
  2017. ctfgame.match = MATCH_GAME;
  2018. ctfgame.matchtime = level.time + gtime_t::from_min(matchtime->value);
  2019. ctfgame.countdown = false;
  2020. ctfgame.team1 = ctfgame.team2 = 0;
  2021. memset(ctfgame.ghosts, 0, sizeof(ctfgame.ghosts));
  2022. for (uint32_t i = 1; i <= game.maxclients; i++)
  2023. {
  2024. ent = g_edicts + i;
  2025. if (!ent->inuse)
  2026. continue;
  2027. ent->client->resp.score = 0;
  2028. ent->client->resp.ctf_state = 0;
  2029. ent->client->resp.ghost = nullptr;
  2030. gi.LocCenter_Print(ent, "******************\n\nMATCH HAS STARTED!\n\n******************");
  2031. if (ent->client->resp.ctf_team != CTF_NOTEAM)
  2032. {
  2033. // make up a ghost code
  2034. CTFAssignGhost(ent);
  2035. CTFPlayerResetGrapple(ent);
  2036. ent->svflags = SVF_NOCLIENT;
  2037. ent->flags &= ~FL_GODMODE;
  2038. ent->client->respawn_time = level.time + random_time(1_sec, 4_sec);
  2039. ent->client->ps.pmove.pm_type = PM_DEAD;
  2040. ent->client->anim_priority = ANIM_DEATH;
  2041. ent->s.frame = FRAME_death308 - 1;
  2042. ent->client->anim_end = FRAME_death308;
  2043. ent->deadflag = true;
  2044. ent->movetype = MOVETYPE_NOCLIP;
  2045. ent->client->ps.gunindex = 0;
  2046. ent->client->ps.gunskin = 0;
  2047. gi.linkentity(ent);
  2048. }
  2049. }
  2050. }
  2051. void CTFEndMatch()
  2052. {
  2053. ctfgame.match = MATCH_POST;
  2054. gi.LocBroadcast_Print(PRINT_CHAT, "MATCH COMPLETED!\n");
  2055. CTFCalcScores();
  2056. gi.LocBroadcast_Print(PRINT_HIGH, "RED TEAM: {} captures, {} points\n",
  2057. ctfgame.team1, ctfgame.total1);
  2058. gi.LocBroadcast_Print(PRINT_HIGH, "BLUE TEAM: {} captures, {} points\n",
  2059. ctfgame.team2, ctfgame.total2);
  2060. if (ctfgame.team1 > ctfgame.team2)
  2061. gi.LocBroadcast_Print(PRINT_CHAT, "$g_ctf_red_wins_caps",
  2062. ctfgame.team1 - ctfgame.team2);
  2063. else if (ctfgame.team2 > ctfgame.team1)
  2064. gi.LocBroadcast_Print(PRINT_CHAT, "$g_ctf_blue_wins_caps",
  2065. ctfgame.team2 - ctfgame.team1);
  2066. else if (ctfgame.total1 > ctfgame.total2) // frag tie breaker
  2067. gi.LocBroadcast_Print(PRINT_CHAT, "$g_ctf_red_wins_points",
  2068. ctfgame.total1 - ctfgame.total2);
  2069. else if (ctfgame.total2 > ctfgame.total1)
  2070. gi.LocBroadcast_Print(PRINT_CHAT, "$g_ctf_blue_wins_points",
  2071. ctfgame.total2 - ctfgame.total1);
  2072. else
  2073. gi.LocBroadcast_Print(PRINT_CHAT, "$g_ctf_tie_game");
  2074. EndDMLevel();
  2075. }
  2076. bool CTFNextMap()
  2077. {
  2078. if (ctfgame.match == MATCH_POST)
  2079. {
  2080. ctfgame.match = MATCH_SETUP;
  2081. CTFResetAllPlayers();
  2082. return true;
  2083. }
  2084. return false;
  2085. }
  2086. void CTFWinElection()
  2087. {
  2088. switch (ctfgame.election)
  2089. {
  2090. case ELECT_MATCH:
  2091. // reset into match mode
  2092. if (competition->integer < 3)
  2093. gi.cvar_set("competition", "2");
  2094. ctfgame.match = MATCH_SETUP;
  2095. CTFResetAllPlayers();
  2096. break;
  2097. case ELECT_ADMIN:
  2098. ctfgame.etarget->client->resp.admin = true;
  2099. gi.LocBroadcast_Print(PRINT_HIGH, "{} has become an admin.\n", ctfgame.etarget->client->pers.netname);
  2100. gi.LocClient_Print(ctfgame.etarget, PRINT_HIGH, "Type 'admin' to access the adminstration menu.\n");
  2101. break;
  2102. case ELECT_MAP:
  2103. gi.LocBroadcast_Print(PRINT_HIGH, "{} is warping to level {}.\n",
  2104. ctfgame.etarget->client->pers.netname, ctfgame.elevel);
  2105. Q_strlcpy(level.forcemap, ctfgame.elevel, sizeof(level.forcemap));
  2106. EndDMLevel();
  2107. break;
  2108. default:
  2109. break;
  2110. }
  2111. ctfgame.election = ELECT_NONE;
  2112. }
  2113. void CTFVoteYes(edict_t *ent)
  2114. {
  2115. if (ctfgame.election == ELECT_NONE)
  2116. {
  2117. gi.LocClient_Print(ent, PRINT_HIGH, "No election is in progress.\n");
  2118. return;
  2119. }
  2120. if (ent->client->resp.voted)
  2121. {
  2122. gi.LocClient_Print(ent, PRINT_HIGH, "You already voted.\n");
  2123. return;
  2124. }
  2125. if (ctfgame.etarget == ent)
  2126. {
  2127. gi.LocClient_Print(ent, PRINT_HIGH, "You can't vote for yourself.\n");
  2128. return;
  2129. }
  2130. ent->client->resp.voted = true;
  2131. ctfgame.evotes++;
  2132. if (ctfgame.evotes == ctfgame.needvotes)
  2133. {
  2134. // the election has been won
  2135. CTFWinElection();
  2136. return;
  2137. }
  2138. gi.LocBroadcast_Print(PRINT_HIGH, "{}\n", ctfgame.emsg);
  2139. gi.LocBroadcast_Print(PRINT_CHAT, "Votes: {} Needed: {} Time left: {}s\n", ctfgame.evotes, ctfgame.needvotes,
  2140. (ctfgame.electtime - level.time).seconds<int>());
  2141. }
  2142. void CTFVoteNo(edict_t *ent)
  2143. {
  2144. if (ctfgame.election == ELECT_NONE)
  2145. {
  2146. gi.LocClient_Print(ent, PRINT_HIGH, "No election is in progress.\n");
  2147. return;
  2148. }
  2149. if (ent->client->resp.voted)
  2150. {
  2151. gi.LocClient_Print(ent, PRINT_HIGH, "You already voted.\n");
  2152. return;
  2153. }
  2154. if (ctfgame.etarget == ent)
  2155. {
  2156. gi.LocClient_Print(ent, PRINT_HIGH, "You can't vote for yourself.\n");
  2157. return;
  2158. }
  2159. ent->client->resp.voted = true;
  2160. gi.LocBroadcast_Print(PRINT_HIGH, "{}\n", ctfgame.emsg);
  2161. gi.LocBroadcast_Print(PRINT_CHAT, "Votes: {} Needed: {} Time left: {}s\n", ctfgame.evotes, ctfgame.needvotes,
  2162. (ctfgame.electtime - level.time).seconds<int>());
  2163. }
  2164. void CTFReady(edict_t *ent)
  2165. {
  2166. uint32_t i, j;
  2167. edict_t *e;
  2168. uint32_t t1, t2;
  2169. if (ent->client->resp.ctf_team == CTF_NOTEAM)
  2170. {
  2171. gi.LocClient_Print(ent, PRINT_HIGH, "Pick a team first (hit <TAB> for menu)\n");
  2172. return;
  2173. }
  2174. if (ctfgame.match != MATCH_SETUP)
  2175. {
  2176. gi.LocClient_Print(ent, PRINT_HIGH, "A match is not being setup.\n");
  2177. return;
  2178. }
  2179. if (ent->client->resp.ready)
  2180. {
  2181. gi.LocClient_Print(ent, PRINT_HIGH, "You have already commited.\n");
  2182. return;
  2183. }
  2184. ent->client->resp.ready = true;
  2185. gi.LocBroadcast_Print(PRINT_HIGH, "{} is ready.\n", ent->client->pers.netname);
  2186. t1 = t2 = 0;
  2187. for (j = 0, i = 1; i <= game.maxclients; i++)
  2188. {
  2189. e = g_edicts + i;
  2190. if (!e->inuse)
  2191. continue;
  2192. if (e->client->resp.ctf_team != CTF_NOTEAM && !e->client->resp.ready)
  2193. j++;
  2194. if (e->client->resp.ctf_team == CTF_TEAM1)
  2195. t1++;
  2196. else if (e->client->resp.ctf_team == CTF_TEAM2)
  2197. t2++;
  2198. }
  2199. if (!j && t1 && t2)
  2200. {
  2201. // everyone has commited
  2202. gi.LocBroadcast_Print(PRINT_CHAT, "All players have committed. Match starting\n");
  2203. ctfgame.match = MATCH_PREGAME;
  2204. ctfgame.matchtime = level.time + gtime_t::from_sec(matchstarttime->value);
  2205. ctfgame.countdown = false;
  2206. gi.positioned_sound(world->s.origin, world, CHAN_AUTO | CHAN_RELIABLE, gi.soundindex("misc/talk1.wav"), 1, ATTN_NONE, 0);
  2207. }
  2208. }
  2209. void CTFNotReady(edict_t *ent)
  2210. {
  2211. if (ent->client->resp.ctf_team == CTF_NOTEAM)
  2212. {
  2213. gi.LocClient_Print(ent, PRINT_HIGH, "Pick a team first (hit <TAB> for menu)\n");
  2214. return;
  2215. }
  2216. if (ctfgame.match != MATCH_SETUP && ctfgame.match != MATCH_PREGAME)
  2217. {
  2218. gi.LocClient_Print(ent, PRINT_HIGH, "A match is not being setup.\n");
  2219. return;
  2220. }
  2221. if (!ent->client->resp.ready)
  2222. {
  2223. gi.LocClient_Print(ent, PRINT_HIGH, "You haven't commited.\n");
  2224. return;
  2225. }
  2226. ent->client->resp.ready = false;
  2227. gi.LocBroadcast_Print(PRINT_HIGH, "{} is no longer ready.\n", ent->client->pers.netname);
  2228. if (ctfgame.match == MATCH_PREGAME)
  2229. {
  2230. gi.LocBroadcast_Print(PRINT_CHAT, "Match halted.\n");
  2231. ctfgame.match = MATCH_SETUP;
  2232. ctfgame.matchtime = level.time + gtime_t::from_min(matchsetuptime->value);
  2233. }
  2234. }
  2235. void CTFGhost(edict_t *ent)
  2236. {
  2237. int i;
  2238. int n;
  2239. if (gi.argc() < 2)
  2240. {
  2241. gi.LocClient_Print(ent, PRINT_HIGH, "Usage: ghost <code>\n");
  2242. return;
  2243. }
  2244. if (ent->client->resp.ctf_team != CTF_NOTEAM)
  2245. {
  2246. gi.LocClient_Print(ent, PRINT_HIGH, "You are already in the game.\n");
  2247. return;
  2248. }
  2249. if (ctfgame.match != MATCH_GAME)
  2250. {
  2251. gi.LocClient_Print(ent, PRINT_HIGH, "No match is in progress.\n");
  2252. return;
  2253. }
  2254. n = atoi(gi.argv(1));
  2255. for (i = 0; i < MAX_CLIENTS; i++)
  2256. {
  2257. if (ctfgame.ghosts[i].code && ctfgame.ghosts[i].code == n)
  2258. {
  2259. gi.LocClient_Print(ent, PRINT_HIGH, "Ghost code accepted, your position has been reinstated.\n");
  2260. ctfgame.ghosts[i].ent->client->resp.ghost = nullptr;
  2261. ent->client->resp.ctf_team = ctfgame.ghosts[i].team;
  2262. ent->client->resp.ghost = ctfgame.ghosts + i;
  2263. ent->client->resp.score = ctfgame.ghosts[i].score;
  2264. ent->client->resp.ctf_state = 0;
  2265. ctfgame.ghosts[i].ent = ent;
  2266. ent->svflags = SVF_NONE;
  2267. ent->flags &= ~FL_GODMODE;
  2268. PutClientInServer(ent);
  2269. gi.LocBroadcast_Print(PRINT_HIGH, "{} has been reinstated to {} team.\n",
  2270. ent->client->pers.netname, CTFTeamName(ent->client->resp.ctf_team));
  2271. return;
  2272. }
  2273. }
  2274. gi.LocClient_Print(ent, PRINT_HIGH, "Invalid ghost code.\n");
  2275. }
  2276. bool CTFMatchSetup()
  2277. {
  2278. if (ctfgame.match == MATCH_SETUP || ctfgame.match == MATCH_PREGAME)
  2279. return true;
  2280. return false;
  2281. }
  2282. bool CTFMatchOn()
  2283. {
  2284. if (ctfgame.match == MATCH_GAME)
  2285. return true;
  2286. return false;
  2287. }
  2288. /*-----------------------------------------------------------------------*/
  2289. void CTFJoinTeam1(edict_t *ent, pmenuhnd_t *p);
  2290. void CTFJoinTeam2(edict_t *ent, pmenuhnd_t *p);
  2291. void CTFReturnToMain(edict_t *ent, pmenuhnd_t *p);
  2292. void CTFChaseCam(edict_t *ent, pmenuhnd_t *p);
  2293. static const int jmenu_level = 1;
  2294. static const int jmenu_match = 2;
  2295. static const int jmenu_red = 4;
  2296. static const int jmenu_blue = 7;
  2297. static const int jmenu_chase = 10;
  2298. static const int jmenu_reqmatch = 12;
  2299. const pmenu_t joinmenu[] = {
  2300. { "*$g_pc_3wctf", PMENU_ALIGN_CENTER, nullptr },
  2301. { "", PMENU_ALIGN_CENTER, nullptr },
  2302. { "", PMENU_ALIGN_CENTER, nullptr },
  2303. { "", PMENU_ALIGN_CENTER, nullptr },
  2304. { "$g_pc_join_red_team", PMENU_ALIGN_LEFT, CTFJoinTeam1 },
  2305. { "", PMENU_ALIGN_LEFT, nullptr },
  2306. { "", PMENU_ALIGN_LEFT, nullptr },
  2307. { "$g_pc_join_blue_team", PMENU_ALIGN_LEFT, CTFJoinTeam2 },
  2308. { "", PMENU_ALIGN_LEFT, nullptr },
  2309. { "", PMENU_ALIGN_LEFT, nullptr },
  2310. { "$g_pc_chase_camera", PMENU_ALIGN_LEFT, CTFChaseCam },
  2311. { "", PMENU_ALIGN_LEFT, nullptr },
  2312. { "", PMENU_ALIGN_LEFT, nullptr },
  2313. };
  2314. const pmenu_t nochasemenu[] = {
  2315. { "$g_pc_3wctf", PMENU_ALIGN_CENTER, nullptr },
  2316. { "", PMENU_ALIGN_CENTER, nullptr },
  2317. { "", PMENU_ALIGN_CENTER, nullptr },
  2318. { "$g_pc_no_chase", PMENU_ALIGN_LEFT, nullptr },
  2319. { "", PMENU_ALIGN_CENTER, nullptr },
  2320. { "$g_pc_return", PMENU_ALIGN_LEFT, CTFReturnToMain }
  2321. };
  2322. void CTFJoinTeam(edict_t *ent, ctfteam_t desired_team)
  2323. {
  2324. PMenu_Close(ent);
  2325. ent->svflags &= ~SVF_NOCLIENT;
  2326. ent->client->resp.ctf_team = desired_team;
  2327. ent->client->resp.ctf_state = 0;
  2328. char value[MAX_INFO_VALUE] = { 0 };
  2329. gi.Info_ValueForKey(ent->client->pers.userinfo, "skin", value, sizeof(value));
  2330. CTFAssignSkin(ent, value);
  2331. // assign a ghost if we are in match mode
  2332. if (ctfgame.match == MATCH_GAME)
  2333. {
  2334. if (ent->client->resp.ghost)
  2335. ent->client->resp.ghost->code = 0;
  2336. ent->client->resp.ghost = nullptr;
  2337. CTFAssignGhost(ent);
  2338. }
  2339. PutClientInServer(ent);
  2340. G_PostRespawn(ent);
  2341. gi.LocBroadcast_Print(PRINT_HIGH, "$g_joined_team",
  2342. ent->client->pers.netname, CTFTeamName(desired_team));
  2343. if (ctfgame.match == MATCH_SETUP)
  2344. {
  2345. gi.LocCenter_Print(ent, "Type \"ready\" in console to ready up.\n");
  2346. }
  2347. // if anybody has a menu open, update it immediately
  2348. CTFDirtyTeamMenu();
  2349. }
  2350. void CTFJoinTeam1(edict_t *ent, pmenuhnd_t *p)
  2351. {
  2352. CTFJoinTeam(ent, CTF_TEAM1);
  2353. }
  2354. void CTFJoinTeam2(edict_t *ent, pmenuhnd_t *p)
  2355. {
  2356. CTFJoinTeam(ent, CTF_TEAM2);
  2357. }
  2358. static void CTFNoChaseCamUpdate(edict_t *ent)
  2359. {
  2360. pmenu_t *entries = ent->client->menu->entries;
  2361. SetGameName(&entries[0]);
  2362. SetLevelName(&entries[jmenu_level]);
  2363. }
  2364. void CTFChaseCam(edict_t *ent, pmenuhnd_t *p)
  2365. {
  2366. edict_t *e;
  2367. CTFJoinTeam(ent, CTF_NOTEAM);
  2368. if (ent->client->chase_target)
  2369. {
  2370. ent->client->chase_target = nullptr;
  2371. ent->client->ps.pmove.pm_flags &= ~(PMF_NO_POSITIONAL_PREDICTION | PMF_NO_ANGULAR_PREDICTION);
  2372. PMenu_Close(ent);
  2373. return;
  2374. }
  2375. for (uint32_t i = 1; i <= game.maxclients; i++)
  2376. {
  2377. e = g_edicts + i;
  2378. if (e->inuse && e->solid != SOLID_NOT)
  2379. {
  2380. ent->client->chase_target = e;
  2381. PMenu_Close(ent);
  2382. ent->client->update_chase = true;
  2383. return;
  2384. }
  2385. }
  2386. PMenu_Close(ent);
  2387. PMenu_Open(ent, nochasemenu, -1, sizeof(nochasemenu) / sizeof(pmenu_t), nullptr, CTFNoChaseCamUpdate);
  2388. }
  2389. void CTFReturnToMain(edict_t *ent, pmenuhnd_t *p)
  2390. {
  2391. PMenu_Close(ent);
  2392. CTFOpenJoinMenu(ent);
  2393. }
  2394. void CTFRequestMatch(edict_t *ent, pmenuhnd_t *p)
  2395. {
  2396. PMenu_Close(ent);
  2397. CTFBeginElection(ent, ELECT_MATCH, G_Fmt("{} has requested to switch to competition mode.\n",
  2398. ent->client->pers.netname).data());
  2399. }
  2400. void DeathmatchScoreboard(edict_t *ent);
  2401. void CTFShowScores(edict_t *ent, pmenu_t *p)
  2402. {
  2403. PMenu_Close(ent);
  2404. ent->client->showscores = true;
  2405. ent->client->showinventory = false;
  2406. DeathmatchScoreboard(ent);
  2407. }
  2408. void CTFUpdateJoinMenu(edict_t *ent)
  2409. {
  2410. pmenu_t *entries = ent->client->menu->entries;
  2411. SetGameName(entries);
  2412. if (ctfgame.match >= MATCH_PREGAME && matchlock->integer)
  2413. {
  2414. Q_strlcpy(entries[jmenu_red].text, "MATCH IS LOCKED", sizeof(entries[jmenu_red].text));
  2415. entries[jmenu_red].SelectFunc = nullptr;
  2416. Q_strlcpy(entries[jmenu_blue].text, " (entry is not permitted)", sizeof(entries[jmenu_blue].text));
  2417. entries[jmenu_blue].SelectFunc = nullptr;
  2418. }
  2419. else
  2420. {
  2421. if (ctfgame.match >= MATCH_PREGAME)
  2422. {
  2423. Q_strlcpy(entries[jmenu_red].text, "Join Red MATCH Team", sizeof(entries[jmenu_red].text));
  2424. Q_strlcpy(entries[jmenu_blue].text, "Join Blue MATCH Team", sizeof(entries[jmenu_blue].text));
  2425. }
  2426. else
  2427. {
  2428. Q_strlcpy(entries[jmenu_red].text, "$g_pc_join_red_team", sizeof(entries[jmenu_red].text));
  2429. Q_strlcpy(entries[jmenu_blue].text, "$g_pc_join_blue_team", sizeof(entries[jmenu_blue].text));
  2430. }
  2431. entries[jmenu_red].SelectFunc = CTFJoinTeam1;
  2432. entries[jmenu_blue].SelectFunc = CTFJoinTeam2;
  2433. }
  2434. // KEX_FIXME: what's this for?
  2435. if (g_teamplay_force_join->string && *g_teamplay_force_join->string)
  2436. {
  2437. if (Q_strcasecmp(g_teamplay_force_join->string, "red") == 0)
  2438. {
  2439. entries[jmenu_blue].text[0] = '\0';
  2440. entries[jmenu_blue].SelectFunc = nullptr;
  2441. }
  2442. else if (Q_strcasecmp(g_teamplay_force_join->string, "blue") == 0)
  2443. {
  2444. entries[jmenu_red].text[0] = '\0';
  2445. entries[jmenu_red].SelectFunc = nullptr;
  2446. }
  2447. }
  2448. if (ent->client->chase_target)
  2449. Q_strlcpy(entries[jmenu_chase].text, "$g_pc_leave_chase_camera", sizeof(entries[jmenu_chase].text));
  2450. else
  2451. Q_strlcpy(entries[jmenu_chase].text, "$g_pc_chase_camera", sizeof(entries[jmenu_chase].text));
  2452. SetLevelName(entries + jmenu_level);
  2453. uint32_t num1 = 0, num2 = 0;
  2454. for (uint32_t i = 0; i < game.maxclients; i++)
  2455. {
  2456. if (!g_edicts[i + 1].inuse)
  2457. continue;
  2458. if (game.clients[i].resp.ctf_team == CTF_TEAM1)
  2459. num1++;
  2460. else if (game.clients[i].resp.ctf_team == CTF_TEAM2)
  2461. num2++;
  2462. }
  2463. switch (ctfgame.match)
  2464. {
  2465. case MATCH_NONE:
  2466. entries[jmenu_match].text[0] = '\0';
  2467. break;
  2468. case MATCH_SETUP:
  2469. Q_strlcpy(entries[jmenu_match].text, "*MATCH SETUP IN PROGRESS", sizeof(entries[jmenu_match].text));
  2470. break;
  2471. case MATCH_PREGAME:
  2472. Q_strlcpy(entries[jmenu_match].text, "*MATCH STARTING", sizeof(entries[jmenu_match].text));
  2473. break;
  2474. case MATCH_GAME:
  2475. Q_strlcpy(entries[jmenu_match].text, "*MATCH IN PROGRESS", sizeof(entries[jmenu_match].text));
  2476. break;
  2477. default:
  2478. break;
  2479. }
  2480. if (*entries[jmenu_red].text)
  2481. {
  2482. Q_strlcpy(entries[jmenu_red + 1].text, "$g_pc_playercount", sizeof(entries[jmenu_red + 1].text));
  2483. G_FmtTo(entries[jmenu_red + 1].text_arg1, "{}", num1);
  2484. }
  2485. else
  2486. {
  2487. entries[jmenu_red + 1].text[0] = '\0';
  2488. entries[jmenu_red + 1].text_arg1[0] = '\0';
  2489. }
  2490. if (*entries[jmenu_blue].text)
  2491. {
  2492. Q_strlcpy(entries[jmenu_blue + 1].text, "$g_pc_playercount", sizeof(entries[jmenu_blue + 1].text));
  2493. G_FmtTo(entries[jmenu_blue + 1].text_arg1, "{}", num2);
  2494. }
  2495. else
  2496. {
  2497. entries[jmenu_blue + 1].text[0] = '\0';
  2498. entries[jmenu_blue + 1].text_arg1[0] = '\0';
  2499. }
  2500. entries[jmenu_reqmatch].text[0] = '\0';
  2501. entries[jmenu_reqmatch].SelectFunc = nullptr;
  2502. if (competition->integer && ctfgame.match < MATCH_SETUP)
  2503. {
  2504. Q_strlcpy(entries[jmenu_reqmatch].text, "Request Match", sizeof(entries[jmenu_reqmatch].text));
  2505. entries[jmenu_reqmatch].SelectFunc = CTFRequestMatch;
  2506. }
  2507. }
  2508. void CTFOpenJoinMenu(edict_t *ent)
  2509. {
  2510. uint32_t num1 = 0, num2 = 0;
  2511. for (uint32_t i = 0; i < game.maxclients; i++)
  2512. {
  2513. if (!g_edicts[i + 1].inuse)
  2514. continue;
  2515. if (game.clients[i].resp.ctf_team == CTF_TEAM1)
  2516. num1++;
  2517. else if (game.clients[i].resp.ctf_team == CTF_TEAM2)
  2518. num2++;
  2519. }
  2520. int team;
  2521. if (num1 > num2)
  2522. team = CTF_TEAM1;
  2523. else if (num2 > num1)
  2524. team = CTF_TEAM2;
  2525. team = brandom() ? CTF_TEAM1 : CTF_TEAM2;
  2526. PMenu_Open(ent, joinmenu, team, sizeof(joinmenu) / sizeof(pmenu_t), nullptr, CTFUpdateJoinMenu);
  2527. }
  2528. bool CTFStartClient(edict_t *ent)
  2529. {
  2530. if (!G_TeamplayEnabled())
  2531. return false;
  2532. if (ent->client->resp.ctf_team != CTF_NOTEAM)
  2533. return false;
  2534. if ((!(ent->svflags & SVF_BOT) && !g_teamplay_force_join->integer) || ctfgame.match >= MATCH_SETUP)
  2535. {
  2536. // start as 'observer'
  2537. ent->movetype = MOVETYPE_NOCLIP;
  2538. ent->solid = SOLID_NOT;
  2539. ent->svflags |= SVF_NOCLIENT;
  2540. ent->client->resp.ctf_team = CTF_NOTEAM;
  2541. ent->client->resp.spectator = true;
  2542. ent->client->ps.gunindex = 0;
  2543. ent->client->ps.gunskin = 0;
  2544. gi.linkentity(ent);
  2545. CTFOpenJoinMenu(ent);
  2546. return true;
  2547. }
  2548. return false;
  2549. }
  2550. void CTFObserver(edict_t *ent)
  2551. {
  2552. if (!G_TeamplayEnabled() || g_teamplay_force_join->integer)
  2553. return;
  2554. // start as 'observer'
  2555. if (ent->movetype == MOVETYPE_NOCLIP)
  2556. CTFPlayerResetGrapple(ent);
  2557. CTFDeadDropFlag(ent);
  2558. CTFDeadDropTech(ent);
  2559. ent->deadflag = false;
  2560. ent->movetype = MOVETYPE_NOCLIP;
  2561. ent->solid = SOLID_NOT;
  2562. ent->svflags |= SVF_NOCLIENT;
  2563. ent->client->resp.ctf_team = CTF_NOTEAM;
  2564. ent->client->ps.gunindex = 0;
  2565. ent->client->ps.gunskin = 0;
  2566. ent->client->resp.score = 0;
  2567. PutClientInServer(ent);
  2568. }
  2569. bool CTFInMatch()
  2570. {
  2571. if (ctfgame.match > MATCH_NONE)
  2572. return true;
  2573. return false;
  2574. }
  2575. bool CTFCheckRules()
  2576. {
  2577. int t;
  2578. uint32_t i, j;
  2579. char text[64];
  2580. edict_t *ent;
  2581. if (ctfgame.election != ELECT_NONE && ctfgame.electtime <= level.time)
  2582. {
  2583. gi.LocBroadcast_Print(PRINT_CHAT, "Election timed out and has been cancelled.\n");
  2584. ctfgame.election = ELECT_NONE;
  2585. }
  2586. if (ctfgame.match != MATCH_NONE)
  2587. {
  2588. t = (ctfgame.matchtime - level.time).seconds<int>();
  2589. // no team warnings in match mode
  2590. ctfgame.warnactive = 0;
  2591. if (t <= 0)
  2592. { // time ended on something
  2593. switch (ctfgame.match)
  2594. {
  2595. case MATCH_SETUP:
  2596. // go back to normal mode
  2597. if (competition->integer < 3)
  2598. {
  2599. ctfgame.match = MATCH_NONE;
  2600. gi.cvar_set("competition", "1");
  2601. CTFResetAllPlayers();
  2602. }
  2603. else
  2604. {
  2605. // reset the time
  2606. ctfgame.matchtime = level.time + gtime_t::from_min(matchsetuptime->value);
  2607. }
  2608. return false;
  2609. case MATCH_PREGAME:
  2610. // match started!
  2611. CTFStartMatch();
  2612. gi.positioned_sound(world->s.origin, world, CHAN_AUTO | CHAN_RELIABLE, gi.soundindex("misc/tele_up.wav"), 1, ATTN_NONE, 0);
  2613. return false;
  2614. case MATCH_GAME:
  2615. // match ended!
  2616. CTFEndMatch();
  2617. gi.positioned_sound(world->s.origin, world, CHAN_AUTO | CHAN_RELIABLE, gi.soundindex("misc/bigtele.wav"), 1, ATTN_NONE, 0);
  2618. return false;
  2619. default:
  2620. break;
  2621. }
  2622. }
  2623. if (t == ctfgame.lasttime)
  2624. return false;
  2625. ctfgame.lasttime = t;
  2626. switch (ctfgame.match)
  2627. {
  2628. case MATCH_SETUP:
  2629. for (j = 0, i = 1; i <= game.maxclients; i++)
  2630. {
  2631. ent = g_edicts + i;
  2632. if (!ent->inuse)
  2633. continue;
  2634. if (ent->client->resp.ctf_team != CTF_NOTEAM &&
  2635. !ent->client->resp.ready)
  2636. j++;
  2637. }
  2638. if (competition->integer < 3)
  2639. G_FmtTo(text, "{:02}:{:02} SETUP: {} not ready", t / 60, t % 60, j);
  2640. else
  2641. G_FmtTo(text, "SETUP: {} not ready", j);
  2642. gi.configstring(CONFIG_CTF_MATCH, text);
  2643. break;
  2644. case MATCH_PREGAME:
  2645. G_FmtTo(text, "{:02}:{:02} UNTIL START", t / 60, t % 60);
  2646. gi.configstring(CONFIG_CTF_MATCH, text);
  2647. if (t <= 10 && !ctfgame.countdown)
  2648. {
  2649. ctfgame.countdown = true;
  2650. gi.positioned_sound(world->s.origin, world, CHAN_AUTO | CHAN_RELIABLE, gi.soundindex("world/10_0.wav"), 1, ATTN_NONE, 0);
  2651. }
  2652. break;
  2653. case MATCH_GAME:
  2654. G_FmtTo(text, "{:02}:{:02} MATCH", t / 60, t % 60);
  2655. gi.configstring(CONFIG_CTF_MATCH, text);
  2656. if (t <= 10 && !ctfgame.countdown)
  2657. {
  2658. ctfgame.countdown = true;
  2659. gi.positioned_sound(world->s.origin, world, CHAN_AUTO | CHAN_RELIABLE, gi.soundindex("world/10_0.wav"), 1, ATTN_NONE, 0);
  2660. }
  2661. break;
  2662. default:
  2663. break;
  2664. }
  2665. return false;
  2666. }
  2667. else
  2668. {
  2669. int team1 = 0, team2 = 0;
  2670. if (level.time == gtime_t::from_sec(ctfgame.lasttime))
  2671. return false;
  2672. ctfgame.lasttime = level.time.seconds<int>();
  2673. // this is only done in non-match (public) mode
  2674. if (warn_unbalanced->integer)
  2675. {
  2676. // count up the team totals
  2677. for (i = 1; i <= game.maxclients; i++)
  2678. {
  2679. ent = g_edicts + i;
  2680. if (!ent->inuse)
  2681. continue;
  2682. if (ent->client->resp.ctf_team == CTF_TEAM1)
  2683. team1++;
  2684. else if (ent->client->resp.ctf_team == CTF_TEAM2)
  2685. team2++;
  2686. }
  2687. if (team1 - team2 >= 2 && team2 >= 2)
  2688. {
  2689. if (ctfgame.warnactive != CTF_TEAM1)
  2690. {
  2691. ctfgame.warnactive = CTF_TEAM1;
  2692. gi.configstring(CONFIG_CTF_TEAMINFO, "WARNING: Red has too many players");
  2693. }
  2694. }
  2695. else if (team2 - team1 >= 2 && team1 >= 2)
  2696. {
  2697. if (ctfgame.warnactive != CTF_TEAM2)
  2698. {
  2699. ctfgame.warnactive = CTF_TEAM2;
  2700. gi.configstring(CONFIG_CTF_TEAMINFO, "WARNING: Blue has too many players");
  2701. }
  2702. }
  2703. else
  2704. ctfgame.warnactive = 0;
  2705. }
  2706. else
  2707. ctfgame.warnactive = 0;
  2708. }
  2709. if (capturelimit->integer &&
  2710. (ctfgame.team1 >= capturelimit->integer ||
  2711. ctfgame.team2 >= capturelimit->integer))
  2712. {
  2713. gi.LocBroadcast_Print(PRINT_HIGH, "$g_capturelimit_hit");
  2714. return true;
  2715. }
  2716. return false;
  2717. }
  2718. /*--------------------------------------------------------------------------
  2719. * just here to help old map conversions
  2720. *--------------------------------------------------------------------------*/
  2721. TOUCH(old_teleporter_touch) (edict_t *self, edict_t *other, const trace_t &tr, bool other_touching_self) -> void
  2722. {
  2723. edict_t *dest;
  2724. vec3_t forward;
  2725. if (!other->client)
  2726. return;
  2727. dest = G_PickTarget(self->target);
  2728. if (!dest)
  2729. {
  2730. gi.Com_Print("Couldn't find destination\n");
  2731. return;
  2732. }
  2733. // ZOID
  2734. CTFPlayerResetGrapple(other);
  2735. // ZOID
  2736. // unlink to make sure it can't possibly interfere with KillBox
  2737. gi.unlinkentity(other);
  2738. other->s.origin = dest->s.origin;
  2739. other->s.old_origin = dest->s.origin;
  2740. // other->s.origin[2] += 10;
  2741. // clear the velocity and hold them in place briefly
  2742. other->velocity = {};
  2743. other->client->ps.pmove.pm_time = 160; // hold time
  2744. other->client->ps.pmove.pm_flags |= PMF_TIME_TELEPORT;
  2745. // draw the teleport splash at source and on the player
  2746. self->enemy->s.event = EV_PLAYER_TELEPORT;
  2747. other->s.event = EV_PLAYER_TELEPORT;
  2748. // set angles
  2749. other->client->ps.pmove.delta_angles = dest->s.angles - other->client->resp.cmd_angles;
  2750. other->s.angles[PITCH] = 0;
  2751. other->s.angles[YAW] = dest->s.angles[YAW];
  2752. other->s.angles[ROLL] = 0;
  2753. other->client->ps.viewangles = dest->s.angles;
  2754. other->client->v_angle = dest->s.angles;
  2755. // give a little forward velocity
  2756. AngleVectors(other->client->v_angle, forward, nullptr, nullptr);
  2757. other->velocity = forward * 200;
  2758. gi.linkentity(other);
  2759. // kill anything at the destination
  2760. if (!KillBox(other, true))
  2761. {
  2762. }
  2763. // [Paril-KEX] move sphere, if we own it
  2764. if (other->client->owned_sphere)
  2765. {
  2766. edict_t *sphere = other->client->owned_sphere;
  2767. sphere->s.origin = other->s.origin;
  2768. sphere->s.origin[2] = other->absmax[2];
  2769. sphere->s.angles[YAW] = other->s.angles[YAW];
  2770. gi.linkentity(sphere);
  2771. }
  2772. }
  2773. /*QUAKED trigger_ctf_teleport (0.5 0.5 0.5) ?
  2774. Players touching this will be teleported
  2775. */
  2776. void SP_trigger_ctf_teleport(edict_t *ent)
  2777. {
  2778. edict_t *s;
  2779. int i;
  2780. if (!ent->target)
  2781. {
  2782. gi.Com_Print("teleporter without a target.\n");
  2783. G_FreeEdict(ent);
  2784. return;
  2785. }
  2786. ent->svflags |= SVF_NOCLIENT;
  2787. ent->solid = SOLID_TRIGGER;
  2788. ent->touch = old_teleporter_touch;
  2789. gi.setmodel(ent, ent->model);
  2790. gi.linkentity(ent);
  2791. // noise maker and splash effect dude
  2792. s = G_Spawn();
  2793. ent->enemy = s;
  2794. for (i = 0; i < 3; i++)
  2795. s->s.origin[i] = ent->mins[i] + (ent->maxs[i] - ent->mins[i]) / 2;
  2796. s->s.sound = gi.soundindex("world/hum1.wav");
  2797. gi.linkentity(s);
  2798. }
  2799. /*QUAKED info_ctf_teleport_destination (0.5 0.5 0.5) (-16 -16 -24) (16 16 32)
  2800. Point trigger_teleports at these.
  2801. */
  2802. void SP_info_ctf_teleport_destination(edict_t *ent)
  2803. {
  2804. ent->s.origin[2] += 16;
  2805. }
  2806. /*----------------------------------------------------------------------------------*/
  2807. /* ADMIN */
  2808. struct admin_settings_t
  2809. {
  2810. int matchlen;
  2811. int matchsetuplen;
  2812. int matchstartlen;
  2813. bool weaponsstay;
  2814. bool instantitems;
  2815. bool quaddrop;
  2816. bool instantweap;
  2817. bool matchlock;
  2818. };
  2819. void CTFAdmin_UpdateSettings(edict_t *ent, pmenuhnd_t *setmenu);
  2820. void CTFOpenAdminMenu(edict_t *ent);
  2821. void CTFAdmin_SettingsApply(edict_t *ent, pmenuhnd_t *p)
  2822. {
  2823. admin_settings_t *settings = (admin_settings_t *) p->arg;
  2824. if (settings->matchlen != matchtime->value)
  2825. {
  2826. gi.LocBroadcast_Print(PRINT_HIGH, "{} changed the match length to {} minutes.\n",
  2827. ent->client->pers.netname, settings->matchlen);
  2828. if (ctfgame.match == MATCH_GAME)
  2829. {
  2830. // in the middle of a match, change it on the fly
  2831. ctfgame.matchtime = (ctfgame.matchtime - gtime_t::from_min(matchtime->value)) + gtime_t::from_min(settings->matchlen);
  2832. }
  2833. ;
  2834. gi.cvar_set("matchtime", G_Fmt("{}", settings->matchlen).data());
  2835. }
  2836. if (settings->matchsetuplen != matchsetuptime->value)
  2837. {
  2838. gi.LocBroadcast_Print(PRINT_HIGH, "{} changed the match setup time to {} minutes.\n",
  2839. ent->client->pers.netname, settings->matchsetuplen);
  2840. if (ctfgame.match == MATCH_SETUP)
  2841. {
  2842. // in the middle of a match, change it on the fly
  2843. ctfgame.matchtime = (ctfgame.matchtime - gtime_t::from_min(matchsetuptime->value)) + gtime_t::from_min(settings->matchsetuplen);
  2844. }
  2845. ;
  2846. gi.cvar_set("matchsetuptime", G_Fmt("{}", settings->matchsetuplen).data());
  2847. }
  2848. if (settings->matchstartlen != matchstarttime->value)
  2849. {
  2850. gi.LocBroadcast_Print(PRINT_HIGH, "{} changed the match start time to {} seconds.\n",
  2851. ent->client->pers.netname, settings->matchstartlen);
  2852. if (ctfgame.match == MATCH_PREGAME)
  2853. {
  2854. // in the middle of a match, change it on the fly
  2855. ctfgame.matchtime = (ctfgame.matchtime - gtime_t::from_sec(matchstarttime->value)) + gtime_t::from_sec(settings->matchstartlen);
  2856. }
  2857. gi.cvar_set("matchstarttime", G_Fmt("{}", settings->matchstartlen).data());
  2858. }
  2859. if (settings->weaponsstay != !!g_dm_weapons_stay->integer)
  2860. {
  2861. gi.LocBroadcast_Print(PRINT_HIGH, "{} turned {} weapons stay.\n",
  2862. ent->client->pers.netname, settings->weaponsstay ? "on" : "off");
  2863. gi.cvar_set("g_dm_weapons_stay", settings->weaponsstay ? "1" : "0");
  2864. }
  2865. if (settings->instantitems != !!g_dm_instant_items->integer)
  2866. {
  2867. gi.LocBroadcast_Print(PRINT_HIGH, "{} turned {} instant items.\n",
  2868. ent->client->pers.netname, settings->instantitems ? "on" : "off");
  2869. gi.cvar_set("g_dm_instant_items", settings->instantitems ? "1" : "0");
  2870. }
  2871. if (settings->quaddrop != (bool) !g_dm_no_quad_drop->integer)
  2872. {
  2873. gi.LocBroadcast_Print(PRINT_HIGH, "{} turned {} quad drop.\n",
  2874. ent->client->pers.netname, settings->quaddrop ? "on" : "off");
  2875. gi.cvar_set("g_dm_no_quad_drop", !settings->quaddrop ? "1" : "0");
  2876. }
  2877. if (settings->instantweap != !!g_instant_weapon_switch->integer)
  2878. {
  2879. gi.LocBroadcast_Print(PRINT_HIGH, "{} turned {} instant weapons.\n",
  2880. ent->client->pers.netname, settings->instantweap ? "on" : "off");
  2881. gi.cvar_set("g_instant_weapon_switch", settings->instantweap ? "1" : "0");
  2882. }
  2883. if (settings->matchlock != !!matchlock->integer)
  2884. {
  2885. gi.LocBroadcast_Print(PRINT_HIGH, "{} turned {} match lock.\n",
  2886. ent->client->pers.netname, settings->matchlock ? "on" : "off");
  2887. gi.cvar_set("matchlock", settings->matchlock ? "1" : "0");
  2888. }
  2889. PMenu_Close(ent);
  2890. CTFOpenAdminMenu(ent);
  2891. }
  2892. void CTFAdmin_SettingsCancel(edict_t *ent, pmenuhnd_t *p)
  2893. {
  2894. PMenu_Close(ent);
  2895. CTFOpenAdminMenu(ent);
  2896. }
  2897. void CTFAdmin_ChangeMatchLen(edict_t *ent, pmenuhnd_t *p)
  2898. {
  2899. admin_settings_t *settings = (admin_settings_t *) p->arg;
  2900. settings->matchlen = (settings->matchlen % 60) + 5;
  2901. if (settings->matchlen < 5)
  2902. settings->matchlen = 5;
  2903. CTFAdmin_UpdateSettings(ent, p);
  2904. }
  2905. void CTFAdmin_ChangeMatchSetupLen(edict_t *ent, pmenuhnd_t *p)
  2906. {
  2907. admin_settings_t *settings = (admin_settings_t *) p->arg;
  2908. settings->matchsetuplen = (settings->matchsetuplen % 60) + 5;
  2909. if (settings->matchsetuplen < 5)
  2910. settings->matchsetuplen = 5;
  2911. CTFAdmin_UpdateSettings(ent, p);
  2912. }
  2913. void CTFAdmin_ChangeMatchStartLen(edict_t *ent, pmenuhnd_t *p)
  2914. {
  2915. admin_settings_t *settings = (admin_settings_t *) p->arg;
  2916. settings->matchstartlen = (settings->matchstartlen % 600) + 10;
  2917. if (settings->matchstartlen < 20)
  2918. settings->matchstartlen = 20;
  2919. CTFAdmin_UpdateSettings(ent, p);
  2920. }
  2921. void CTFAdmin_ChangeWeapStay(edict_t *ent, pmenuhnd_t *p)
  2922. {
  2923. admin_settings_t *settings = (admin_settings_t *) p->arg;
  2924. settings->weaponsstay = !settings->weaponsstay;
  2925. CTFAdmin_UpdateSettings(ent, p);
  2926. }
  2927. void CTFAdmin_ChangeInstantItems(edict_t *ent, pmenuhnd_t *p)
  2928. {
  2929. admin_settings_t *settings = (admin_settings_t *) p->arg;
  2930. settings->instantitems = !settings->instantitems;
  2931. CTFAdmin_UpdateSettings(ent, p);
  2932. }
  2933. void CTFAdmin_ChangeQuadDrop(edict_t *ent, pmenuhnd_t *p)
  2934. {
  2935. admin_settings_t *settings = (admin_settings_t *) p->arg;
  2936. settings->quaddrop = !settings->quaddrop;
  2937. CTFAdmin_UpdateSettings(ent, p);
  2938. }
  2939. void CTFAdmin_ChangeInstantWeap(edict_t *ent, pmenuhnd_t *p)
  2940. {
  2941. admin_settings_t *settings = (admin_settings_t *) p->arg;
  2942. settings->instantweap = !settings->instantweap;
  2943. CTFAdmin_UpdateSettings(ent, p);
  2944. }
  2945. void CTFAdmin_ChangeMatchLock(edict_t *ent, pmenuhnd_t *p)
  2946. {
  2947. admin_settings_t *settings = (admin_settings_t *) p->arg;
  2948. settings->matchlock = !settings->matchlock;
  2949. CTFAdmin_UpdateSettings(ent, p);
  2950. }
  2951. void CTFAdmin_UpdateSettings(edict_t *ent, pmenuhnd_t *setmenu)
  2952. {
  2953. int i = 2;
  2954. admin_settings_t *settings = (admin_settings_t *) setmenu->arg;
  2955. PMenu_UpdateEntry(setmenu->entries + i, G_Fmt("Match Len: {:2} mins", settings->matchlen).data(), PMENU_ALIGN_LEFT, CTFAdmin_ChangeMatchLen);
  2956. i++;
  2957. PMenu_UpdateEntry(setmenu->entries + i, G_Fmt("Match Setup Len: {:2} mins", settings->matchsetuplen).data(), PMENU_ALIGN_LEFT, CTFAdmin_ChangeMatchSetupLen);
  2958. i++;
  2959. PMenu_UpdateEntry(setmenu->entries + i, G_Fmt("Match Start Len: {:2} secs", settings->matchstartlen).data(), PMENU_ALIGN_LEFT, CTFAdmin_ChangeMatchStartLen);
  2960. i++;
  2961. PMenu_UpdateEntry(setmenu->entries + i, G_Fmt("Weapons Stay: {}", settings->weaponsstay ? "Yes" : "No").data(), PMENU_ALIGN_LEFT, CTFAdmin_ChangeWeapStay);
  2962. i++;
  2963. PMenu_UpdateEntry(setmenu->entries + i, G_Fmt("Instant Items: {}", settings->instantitems ? "Yes" : "No").data(), PMENU_ALIGN_LEFT, CTFAdmin_ChangeInstantItems);
  2964. i++;
  2965. PMenu_UpdateEntry(setmenu->entries + i, G_Fmt("Quad Drop: {}", settings->quaddrop ? "Yes" : "No").data(), PMENU_ALIGN_LEFT, CTFAdmin_ChangeQuadDrop);
  2966. i++;
  2967. PMenu_UpdateEntry(setmenu->entries + i, G_Fmt("Instant Weapons: {}", settings->instantweap ? "Yes" : "No").data(), PMENU_ALIGN_LEFT, CTFAdmin_ChangeInstantWeap);
  2968. i++;
  2969. PMenu_UpdateEntry(setmenu->entries + i, G_Fmt("Match Lock: {}", settings->matchlock ? "Yes" : "No").data(), PMENU_ALIGN_LEFT, CTFAdmin_ChangeMatchLock);
  2970. i++;
  2971. PMenu_Update(ent);
  2972. }
  2973. const pmenu_t def_setmenu[] = {
  2974. { "*Settings Menu", PMENU_ALIGN_CENTER, nullptr },
  2975. { "", PMENU_ALIGN_CENTER, nullptr },
  2976. { "", PMENU_ALIGN_LEFT, nullptr }, // int matchlen;
  2977. { "", PMENU_ALIGN_LEFT, nullptr }, // int matchsetuplen;
  2978. { "", PMENU_ALIGN_LEFT, nullptr }, // int matchstartlen;
  2979. { "", PMENU_ALIGN_LEFT, nullptr }, // bool weaponsstay;
  2980. { "", PMENU_ALIGN_LEFT, nullptr }, // bool instantitems;
  2981. { "", PMENU_ALIGN_LEFT, nullptr }, // bool quaddrop;
  2982. { "", PMENU_ALIGN_LEFT, nullptr }, // bool instantweap;
  2983. { "", PMENU_ALIGN_LEFT, nullptr }, // bool matchlock;
  2984. { "", PMENU_ALIGN_LEFT, nullptr },
  2985. { "Apply", PMENU_ALIGN_LEFT, CTFAdmin_SettingsApply },
  2986. { "Cancel", PMENU_ALIGN_LEFT, CTFAdmin_SettingsCancel }
  2987. };
  2988. void CTFAdmin_Settings(edict_t *ent, pmenuhnd_t *p)
  2989. {
  2990. admin_settings_t *settings;
  2991. pmenuhnd_t *menu;
  2992. PMenu_Close(ent);
  2993. settings = (admin_settings_t *) gi.TagMalloc(sizeof(*settings), TAG_LEVEL);
  2994. settings->matchlen = matchtime->integer;
  2995. settings->matchsetuplen = matchsetuptime->integer;
  2996. settings->matchstartlen = matchstarttime->integer;
  2997. settings->weaponsstay = g_dm_weapons_stay->integer;
  2998. settings->instantitems = g_dm_instant_items->integer;
  2999. settings->quaddrop = !g_dm_no_quad_drop->integer;
  3000. settings->instantweap = g_instant_weapon_switch->integer != 0;
  3001. settings->matchlock = matchlock->integer != 0;
  3002. menu = PMenu_Open(ent, def_setmenu, -1, sizeof(def_setmenu) / sizeof(pmenu_t), settings, nullptr);
  3003. CTFAdmin_UpdateSettings(ent, menu);
  3004. }
  3005. void CTFAdmin_MatchSet(edict_t *ent, pmenuhnd_t *p)
  3006. {
  3007. PMenu_Close(ent);
  3008. if (ctfgame.match == MATCH_SETUP)
  3009. {
  3010. gi.LocBroadcast_Print(PRINT_CHAT, "Match has been forced to start.\n");
  3011. ctfgame.match = MATCH_PREGAME;
  3012. ctfgame.matchtime = level.time + gtime_t::from_sec(matchstarttime->value);
  3013. gi.positioned_sound(world->s.origin, world, CHAN_AUTO | CHAN_RELIABLE, gi.soundindex("misc/talk1.wav"), 1, ATTN_NONE, 0);
  3014. ctfgame.countdown = false;
  3015. }
  3016. else if (ctfgame.match == MATCH_GAME)
  3017. {
  3018. gi.LocBroadcast_Print(PRINT_CHAT, "Match has been forced to terminate.\n");
  3019. ctfgame.match = MATCH_SETUP;
  3020. ctfgame.matchtime = level.time + gtime_t::from_min(matchsetuptime->value);
  3021. CTFResetAllPlayers();
  3022. }
  3023. }
  3024. void CTFAdmin_MatchMode(edict_t *ent, pmenuhnd_t *p)
  3025. {
  3026. PMenu_Close(ent);
  3027. if (ctfgame.match != MATCH_SETUP)
  3028. {
  3029. if (competition->integer < 3)
  3030. gi.cvar_set("competition", "2");
  3031. ctfgame.match = MATCH_SETUP;
  3032. CTFResetAllPlayers();
  3033. }
  3034. }
  3035. void CTFAdmin_Reset(edict_t *ent, pmenuhnd_t *p)
  3036. {
  3037. PMenu_Close(ent);
  3038. // go back to normal mode
  3039. gi.LocBroadcast_Print(PRINT_CHAT, "Match mode has been terminated, reseting to normal game.\n");
  3040. ctfgame.match = MATCH_NONE;
  3041. gi.cvar_set("competition", "1");
  3042. CTFResetAllPlayers();
  3043. }
  3044. void CTFAdmin_Cancel(edict_t *ent, pmenuhnd_t *p)
  3045. {
  3046. PMenu_Close(ent);
  3047. }
  3048. pmenu_t adminmenu[] = {
  3049. { "*Administration Menu", PMENU_ALIGN_CENTER, nullptr },
  3050. { "", PMENU_ALIGN_CENTER, nullptr }, // blank
  3051. { "Settings", PMENU_ALIGN_LEFT, CTFAdmin_Settings },
  3052. { "", PMENU_ALIGN_LEFT, nullptr },
  3053. { "", PMENU_ALIGN_LEFT, nullptr },
  3054. { "Cancel", PMENU_ALIGN_LEFT, CTFAdmin_Cancel },
  3055. { "", PMENU_ALIGN_CENTER, nullptr },
  3056. };
  3057. void CTFOpenAdminMenu(edict_t *ent)
  3058. {
  3059. adminmenu[3].text[0] = '\0';
  3060. adminmenu[3].SelectFunc = nullptr;
  3061. adminmenu[4].text[0] = '\0';
  3062. adminmenu[4].SelectFunc = nullptr;
  3063. if (ctfgame.match == MATCH_SETUP)
  3064. {
  3065. Q_strlcpy(adminmenu[3].text, "Force start match", sizeof(adminmenu[3].text));
  3066. adminmenu[3].SelectFunc = CTFAdmin_MatchSet;
  3067. Q_strlcpy(adminmenu[4].text, "Reset to pickup mode", sizeof(adminmenu[4].text));
  3068. adminmenu[4].SelectFunc = CTFAdmin_Reset;
  3069. }
  3070. else if (ctfgame.match == MATCH_GAME || ctfgame.match == MATCH_PREGAME)
  3071. {
  3072. Q_strlcpy(adminmenu[3].text, "Cancel match", sizeof(adminmenu[3].text));
  3073. adminmenu[3].SelectFunc = CTFAdmin_MatchSet;
  3074. }
  3075. else if (ctfgame.match == MATCH_NONE && competition->integer)
  3076. {
  3077. Q_strlcpy(adminmenu[3].text, "Switch to match mode", sizeof(adminmenu[3].text));
  3078. adminmenu[3].SelectFunc = CTFAdmin_MatchMode;
  3079. }
  3080. // if (ent->client->menu)
  3081. // PMenu_Close(ent->client->menu);
  3082. PMenu_Open(ent, adminmenu, -1, sizeof(adminmenu) / sizeof(pmenu_t), nullptr, nullptr);
  3083. }
  3084. void CTFAdmin(edict_t *ent)
  3085. {
  3086. if (!allow_admin->integer)
  3087. {
  3088. gi.LocClient_Print(ent, PRINT_HIGH, "Administration is disabled\n");
  3089. return;
  3090. }
  3091. if (gi.argc() > 1 && admin_password->string && *admin_password->string &&
  3092. !ent->client->resp.admin && strcmp(admin_password->string, gi.argv(1)) == 0)
  3093. {
  3094. ent->client->resp.admin = true;
  3095. gi.LocBroadcast_Print(PRINT_HIGH, "{} has become an admin.\n", ent->client->pers.netname);
  3096. gi.LocClient_Print(ent, PRINT_HIGH, "Type 'admin' to access the adminstration menu.\n");
  3097. }
  3098. if (!ent->client->resp.admin)
  3099. {
  3100. CTFBeginElection(ent, ELECT_ADMIN, G_Fmt("{} has requested admin rights.\n",
  3101. ent->client->pers.netname).data());
  3102. return;
  3103. }
  3104. if (ent->client->menu)
  3105. PMenu_Close(ent);
  3106. CTFOpenAdminMenu(ent);
  3107. }
  3108. /*----------------------------------------------------------------*/
  3109. void CTFStats(edict_t *ent)
  3110. {
  3111. if (!G_TeamplayEnabled())
  3112. return;
  3113. ghost_t *g;
  3114. static std::string text;
  3115. edict_t *e2;
  3116. text.clear();
  3117. if (ctfgame.match == MATCH_SETUP)
  3118. {
  3119. for (uint32_t i = 1; i <= game.maxclients; i++)
  3120. {
  3121. e2 = g_edicts + i;
  3122. if (!e2->inuse)
  3123. continue;
  3124. if (!e2->client->resp.ready && e2->client->resp.ctf_team != CTF_NOTEAM)
  3125. {
  3126. std::string_view str = G_Fmt("{} is not ready.\n", e2->client->pers.netname);
  3127. if (text.length() + str.length() < MAX_CTF_STAT_LENGTH - 50)
  3128. text += str;
  3129. }
  3130. }
  3131. }
  3132. uint32_t i;
  3133. for (i = 0, g = ctfgame.ghosts; i < MAX_CLIENTS; i++, g++)
  3134. if (g->ent)
  3135. break;
  3136. if (i == MAX_CLIENTS)
  3137. {
  3138. if (!text.length())
  3139. text = "No statistics available.\n";
  3140. gi.Client_Print(ent, PRINT_HIGH, text.c_str());
  3141. return;
  3142. }
  3143. text += " #|Name |Score|Kills|Death|BasDf|CarDf|Effcy|\n";
  3144. for (i = 0, g = ctfgame.ghosts; i < MAX_CLIENTS; i++, g++)
  3145. {
  3146. if (!*g->netname)
  3147. continue;
  3148. int32_t e;
  3149. if (g->deaths + g->kills == 0)
  3150. e = 50;
  3151. else
  3152. e = g->kills * 100 / (g->kills + g->deaths);
  3153. std::string_view str = G_Fmt("{:3}|{:<16.16}|{:5}|{:5}|{:5}|{:5}|{:5}|{:4}%|\n",
  3154. g->number,
  3155. g->netname,
  3156. g->score,
  3157. g->kills,
  3158. g->deaths,
  3159. g->basedef,
  3160. g->carrierdef,
  3161. e);
  3162. if (text.length() + str.length() > MAX_CTF_STAT_LENGTH - 50)
  3163. {
  3164. text += "And more...\n";
  3165. break;
  3166. }
  3167. text += str;
  3168. }
  3169. gi.Client_Print(ent, PRINT_HIGH, text.c_str());
  3170. }
  3171. void CTFPlayerList(edict_t *ent)
  3172. {
  3173. static std::string text;
  3174. edict_t *e2;
  3175. // number, name, connect time, ping, score, admin
  3176. text.clear();
  3177. for (uint32_t i = 1; i <= game.maxclients; i++)
  3178. {
  3179. e2 = g_edicts + i;
  3180. if (!e2->inuse)
  3181. continue;
  3182. std::string_view str = G_Fmt("{:3} {:<16.16} {:02}:{:02} {:4} {:3}{}{}\n",
  3183. i,
  3184. e2->client->pers.netname,
  3185. (level.time - e2->client->resp.entertime).milliseconds() / 60000,
  3186. ((level.time - e2->client->resp.entertime).milliseconds() % 60000) / 1000,
  3187. e2->client->ping,
  3188. e2->client->resp.score,
  3189. (ctfgame.match == MATCH_SETUP || ctfgame.match == MATCH_PREGAME) ? (e2->client->resp.ready ? " (ready)" : " (notready)") : "",
  3190. e2->client->resp.admin ? " (admin)" : "");
  3191. if (text.length() + str.length() > MAX_CTF_STAT_LENGTH - 50)
  3192. {
  3193. text += "And more...\n";
  3194. break;
  3195. }
  3196. text += str;
  3197. }
  3198. gi.Client_Print(ent, PRINT_HIGH, text.data());
  3199. }
  3200. void CTFWarp(edict_t *ent)
  3201. {
  3202. char *token;
  3203. if (gi.argc() < 2)
  3204. {
  3205. gi.LocClient_Print(ent, PRINT_HIGH, "Where do you want to warp to?\n");
  3206. gi.LocClient_Print(ent, PRINT_HIGH, "Available levels are: {}\n", warp_list->string);
  3207. return;
  3208. }
  3209. const char *mlist = warp_list->string;
  3210. while (*(token = COM_Parse(&mlist)))
  3211. {
  3212. if (Q_strcasecmp(token, gi.argv(1)) == 0)
  3213. break;
  3214. }
  3215. if (!*token)
  3216. {
  3217. gi.LocClient_Print(ent, PRINT_HIGH, "Unknown CTF level.\n");
  3218. gi.LocClient_Print(ent, PRINT_HIGH, "Available levels are: {}\n", warp_list->string);
  3219. return;
  3220. }
  3221. if (ent->client->resp.admin)
  3222. {
  3223. gi.LocBroadcast_Print(PRINT_HIGH, "{} is warping to level {}.\n",
  3224. ent->client->pers.netname, gi.argv(1));
  3225. Q_strlcpy(level.forcemap, gi.argv(1), sizeof(level.forcemap));
  3226. EndDMLevel();
  3227. return;
  3228. }
  3229. if (CTFBeginElection(ent, ELECT_MAP, G_Fmt("{} has requested warping to level {}.\n",
  3230. ent->client->pers.netname, gi.argv(1)).data()))
  3231. Q_strlcpy(ctfgame.elevel, gi.argv(1), sizeof(ctfgame.elevel));
  3232. }
  3233. void CTFBoot(edict_t *ent)
  3234. {
  3235. edict_t *targ;
  3236. if (!ent->client->resp.admin)
  3237. {
  3238. gi.LocClient_Print(ent, PRINT_HIGH, "You are not an admin.\n");
  3239. return;
  3240. }
  3241. if (gi.argc() < 2)
  3242. {
  3243. gi.LocClient_Print(ent, PRINT_HIGH, "Who do you want to kick?\n");
  3244. return;
  3245. }
  3246. if (*gi.argv(1) < '0' && *gi.argv(1) > '9')
  3247. {
  3248. gi.LocClient_Print(ent, PRINT_HIGH, "Specify the player number to kick.\n");
  3249. return;
  3250. }
  3251. uint32_t i = strtoul(gi.argv(1), nullptr, 10);
  3252. if (i < 1 || i > game.maxclients)
  3253. {
  3254. gi.LocClient_Print(ent, PRINT_HIGH, "Invalid player number.\n");
  3255. return;
  3256. }
  3257. targ = g_edicts + i;
  3258. if (!targ->inuse)
  3259. {
  3260. gi.LocClient_Print(ent, PRINT_HIGH, "That player number is not connected.\n");
  3261. return;
  3262. }
  3263. gi.AddCommandString(G_Fmt("kick {}\n", i - 1).data());
  3264. }
  3265. void CTFSetPowerUpEffect(edict_t *ent, effects_t def)
  3266. {
  3267. if (ent->client->resp.ctf_team == CTF_TEAM1 && def == EF_QUAD)
  3268. ent->s.effects |= EF_PENT; // red
  3269. else if (ent->client->resp.ctf_team == CTF_TEAM2 && def == EF_PENT)
  3270. ent->s.effects |= EF_QUAD; // blue
  3271. else
  3272. ent->s.effects |= def;
  3273. }