p_client.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  1. /*
  2. Copyright (C) 1997-2001 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include "g_local.h"
  16. #include "m_player.h"
  17. void ClientUserinfoChanged (edict_t *ent, char *userinfo);
  18. void SP_misc_teleporter_dest (edict_t *ent);
  19. //
  20. // Gross, ugly, disgustuing hack section
  21. //
  22. // this function is an ugly as hell hack to fix some map flaws
  23. //
  24. // the coop spawn spots on some maps are SNAFU. There are coop spots
  25. // with the wrong targetname as well as spots with no name at all
  26. //
  27. // we use carnal knowledge of the maps to fix the coop spot targetnames to match
  28. // that of the nearest named single player spot
  29. static void SP_FixCoopSpots (edict_t *self)
  30. {
  31. edict_t *spot;
  32. vec3_t d;
  33. spot = NULL;
  34. while(1)
  35. {
  36. spot = G_Find(spot, FOFS(classname), "info_player_start");
  37. if (!spot)
  38. return;
  39. if (!spot->targetname)
  40. continue;
  41. VectorSubtract(self->s.origin, spot->s.origin, d);
  42. if (VectorLength(d) < 384)
  43. {
  44. if ((!self->targetname) || Q_stricmp(self->targetname, spot->targetname) != 0)
  45. {
  46. // gi.dprintf("FixCoopSpots changed %s at %s targetname from %s to %s\n", self->classname, vtos(self->s.origin), self->targetname, spot->targetname);
  47. self->targetname = spot->targetname;
  48. }
  49. return;
  50. }
  51. }
  52. }
  53. // now if that one wasn't ugly enough for you then try this one on for size
  54. // some maps don't have any coop spots at all, so we need to create them
  55. // where they should have been
  56. static void SP_CreateCoopSpots (edict_t *self)
  57. {
  58. edict_t *spot;
  59. if(Q_stricmp(level.mapname, "security") == 0)
  60. {
  61. spot = G_Spawn();
  62. spot->classname = "info_player_coop";
  63. spot->s.origin[0] = 188 - 64;
  64. spot->s.origin[1] = -164;
  65. spot->s.origin[2] = 80;
  66. spot->targetname = "jail3";
  67. spot->s.angles[1] = 90;
  68. spot = G_Spawn();
  69. spot->classname = "info_player_coop";
  70. spot->s.origin[0] = 188 + 64;
  71. spot->s.origin[1] = -164;
  72. spot->s.origin[2] = 80;
  73. spot->targetname = "jail3";
  74. spot->s.angles[1] = 90;
  75. spot = G_Spawn();
  76. spot->classname = "info_player_coop";
  77. spot->s.origin[0] = 188 + 128;
  78. spot->s.origin[1] = -164;
  79. spot->s.origin[2] = 80;
  80. spot->targetname = "jail3";
  81. spot->s.angles[1] = 90;
  82. return;
  83. }
  84. }
  85. /*QUAKED info_player_start (1 0 0) (-16 -16 -24) (16 16 32)
  86. The normal starting point for a level.
  87. */
  88. void SP_info_player_start(edict_t *self)
  89. {
  90. if (!coop->value)
  91. return;
  92. if(Q_stricmp(level.mapname, "security") == 0)
  93. {
  94. // invoke one of our gross, ugly, disgusting hacks
  95. self->think = SP_CreateCoopSpots;
  96. self->nextthink = level.time + FRAMETIME;
  97. }
  98. }
  99. /*QUAKED info_player_deathmatch (1 0 1) (-16 -16 -24) (16 16 32)
  100. potential spawning position for deathmatch games
  101. */
  102. void SP_info_player_deathmatch(edict_t *self)
  103. {
  104. if (!deathmatch->value)
  105. {
  106. G_FreeEdict (self);
  107. return;
  108. }
  109. SP_misc_teleporter_dest (self);
  110. }
  111. /*QUAKED info_player_coop (1 0 1) (-16 -16 -24) (16 16 32)
  112. potential spawning position for coop games
  113. */
  114. void SP_info_player_coop(edict_t *self)
  115. {
  116. if (!coop->value)
  117. {
  118. G_FreeEdict (self);
  119. return;
  120. }
  121. if((Q_stricmp(level.mapname, "jail2") == 0) ||
  122. (Q_stricmp(level.mapname, "jail4") == 0) ||
  123. (Q_stricmp(level.mapname, "mine1") == 0) ||
  124. (Q_stricmp(level.mapname, "mine2") == 0) ||
  125. (Q_stricmp(level.mapname, "mine3") == 0) ||
  126. (Q_stricmp(level.mapname, "mine4") == 0) ||
  127. (Q_stricmp(level.mapname, "lab") == 0) ||
  128. (Q_stricmp(level.mapname, "boss1") == 0) ||
  129. (Q_stricmp(level.mapname, "fact3") == 0) ||
  130. (Q_stricmp(level.mapname, "biggun") == 0) ||
  131. (Q_stricmp(level.mapname, "space") == 0) ||
  132. (Q_stricmp(level.mapname, "command") == 0) ||
  133. (Q_stricmp(level.mapname, "power2") == 0) ||
  134. (Q_stricmp(level.mapname, "strike") == 0))
  135. {
  136. // invoke one of our gross, ugly, disgusting hacks
  137. self->think = SP_FixCoopSpots;
  138. self->nextthink = level.time + FRAMETIME;
  139. }
  140. }
  141. /*QUAKED info_player_intermission (1 0 1) (-16 -16 -24) (16 16 32)
  142. The deathmatch intermission point will be at one of these
  143. Use 'angles' instead of 'angle', so you can set pitch or roll as well as yaw. 'pitch yaw roll'
  144. */
  145. void SP_info_player_intermission(void)
  146. {
  147. }
  148. //=======================================================================
  149. void player_pain (edict_t *self, edict_t *other, float kick, int damage)
  150. {
  151. // player pain is handled at the end of the frame in P_DamageFeedback
  152. }
  153. qboolean IsFemale (edict_t *ent)
  154. {
  155. char *info;
  156. if (!ent->client)
  157. return false;
  158. info = Info_ValueForKey (ent->client->pers.userinfo, "gender");
  159. if (info[0] == 'f' || info[0] == 'F')
  160. return true;
  161. return false;
  162. }
  163. qboolean IsNeutral (edict_t *ent)
  164. {
  165. char *info;
  166. if (!ent->client)
  167. return false;
  168. info = Info_ValueForKey (ent->client->pers.userinfo, "gender");
  169. if (info[0] != 'f' && info[0] != 'F' && info[0] != 'm' && info[0] != 'M')
  170. return true;
  171. return false;
  172. }
  173. void ClientObituary (edict_t *self, edict_t *inflictor, edict_t *attacker)
  174. {
  175. int mod;
  176. char *message;
  177. char *message2;
  178. qboolean ff;
  179. if (coop->value && attacker->client)
  180. meansOfDeath |= MOD_FRIENDLY_FIRE;
  181. if (deathmatch->value || coop->value)
  182. {
  183. ff = meansOfDeath & MOD_FRIENDLY_FIRE;
  184. mod = meansOfDeath & ~MOD_FRIENDLY_FIRE;
  185. message = NULL;
  186. message2 = "";
  187. switch (mod)
  188. {
  189. case MOD_SUICIDE:
  190. message = "suicides";
  191. break;
  192. case MOD_FALLING:
  193. message = "cratered";
  194. break;
  195. case MOD_CRUSH:
  196. message = "was squished";
  197. break;
  198. case MOD_WATER:
  199. message = "sank like a rock";
  200. break;
  201. case MOD_SLIME:
  202. message = "melted";
  203. break;
  204. case MOD_LAVA:
  205. message = "does a back flip into the lava";
  206. break;
  207. case MOD_EXPLOSIVE:
  208. case MOD_BARREL:
  209. message = "blew up";
  210. break;
  211. case MOD_EXIT:
  212. message = "found a way out";
  213. break;
  214. case MOD_TARGET_LASER:
  215. message = "saw the light";
  216. break;
  217. case MOD_TARGET_BLASTER:
  218. message = "got blasted";
  219. break;
  220. case MOD_BOMB:
  221. case MOD_SPLASH:
  222. case MOD_TRIGGER_HURT:
  223. message = "was in the wrong place";
  224. break;
  225. }
  226. if (attacker == self)
  227. {
  228. switch (mod)
  229. {
  230. case MOD_HELD_GRENADE:
  231. message = "tried to put the pin back in";
  232. break;
  233. case MOD_HG_SPLASH:
  234. case MOD_G_SPLASH:
  235. if (IsNeutral(self))
  236. message = "tripped on its own grenade";
  237. else if (IsFemale(self))
  238. message = "tripped on her own grenade";
  239. else
  240. message = "tripped on his own grenade";
  241. break;
  242. case MOD_R_SPLASH:
  243. if (IsNeutral(self))
  244. message = "blew itself up";
  245. else if (IsFemale(self))
  246. message = "blew herself up";
  247. else
  248. message = "blew himself up";
  249. break;
  250. case MOD_BFG_BLAST:
  251. message = "should have used a smaller gun";
  252. break;
  253. default:
  254. if (IsNeutral(self))
  255. message = "killed itself";
  256. else if (IsFemale(self))
  257. message = "killed herself";
  258. else
  259. message = "killed himself";
  260. break;
  261. }
  262. }
  263. if (message)
  264. {
  265. gi.bprintf (PRINT_MEDIUM, "%s %s.\n", self->client->pers.netname, message);
  266. if (deathmatch->value)
  267. self->client->resp.score--;
  268. self->enemy = NULL;
  269. return;
  270. }
  271. self->enemy = attacker;
  272. if (attacker && attacker->client)
  273. {
  274. switch (mod)
  275. {
  276. case MOD_BLASTER:
  277. message = "was blasted by";
  278. break;
  279. case MOD_SHOTGUN:
  280. message = "was gunned down by";
  281. break;
  282. case MOD_SSHOTGUN:
  283. message = "was blown away by";
  284. message2 = "'s super shotgun";
  285. break;
  286. case MOD_MACHINEGUN:
  287. message = "was machinegunned by";
  288. break;
  289. case MOD_CHAINGUN:
  290. message = "was cut in half by";
  291. message2 = "'s chaingun";
  292. break;
  293. case MOD_GRENADE:
  294. message = "was popped by";
  295. message2 = "'s grenade";
  296. break;
  297. case MOD_G_SPLASH:
  298. message = "was shredded by";
  299. message2 = "'s shrapnel";
  300. break;
  301. case MOD_ROCKET:
  302. message = "ate";
  303. message2 = "'s rocket";
  304. break;
  305. case MOD_R_SPLASH:
  306. message = "almost dodged";
  307. message2 = "'s rocket";
  308. break;
  309. case MOD_HYPERBLASTER:
  310. message = "was melted by";
  311. message2 = "'s hyperblaster";
  312. break;
  313. case MOD_RAILGUN:
  314. message = "was railed by";
  315. break;
  316. case MOD_BFG_LASER:
  317. message = "saw the pretty lights from";
  318. message2 = "'s BFG";
  319. break;
  320. case MOD_BFG_BLAST:
  321. message = "was disintegrated by";
  322. message2 = "'s BFG blast";
  323. break;
  324. case MOD_BFG_EFFECT:
  325. message = "couldn't hide from";
  326. message2 = "'s BFG";
  327. break;
  328. case MOD_HANDGRENADE:
  329. message = "caught";
  330. message2 = "'s handgrenade";
  331. break;
  332. case MOD_HG_SPLASH:
  333. message = "didn't see";
  334. message2 = "'s handgrenade";
  335. break;
  336. case MOD_HELD_GRENADE:
  337. message = "feels";
  338. message2 = "'s pain";
  339. break;
  340. case MOD_TELEFRAG:
  341. message = "tried to invade";
  342. message2 = "'s personal space";
  343. break;
  344. }
  345. if (message)
  346. {
  347. gi.bprintf (PRINT_MEDIUM,"%s %s %s%s\n", self->client->pers.netname, message, attacker->client->pers.netname, message2);
  348. if (deathmatch->value)
  349. {
  350. if (ff)
  351. attacker->client->resp.score--;
  352. else
  353. attacker->client->resp.score++;
  354. }
  355. return;
  356. }
  357. }
  358. }
  359. gi.bprintf (PRINT_MEDIUM,"%s died.\n", self->client->pers.netname);
  360. if (deathmatch->value)
  361. self->client->resp.score--;
  362. }
  363. void Touch_Item (edict_t *ent, edict_t *other, cplane_t *plane, csurface_t *surf);
  364. void TossClientWeapon (edict_t *self)
  365. {
  366. gitem_t *item;
  367. edict_t *drop;
  368. qboolean quad;
  369. float spread;
  370. if (!deathmatch->value)
  371. return;
  372. item = self->client->pers.weapon;
  373. if (! self->client->pers.inventory[self->client->ammo_index] )
  374. item = NULL;
  375. if (item && (strcmp (item->pickup_name, "Blaster") == 0))
  376. item = NULL;
  377. if (!((int)(dmflags->value) & DF_QUAD_DROP))
  378. quad = false;
  379. else
  380. quad = (self->client->quad_framenum > (level.framenum + 10));
  381. if (item && quad)
  382. spread = 22.5;
  383. else
  384. spread = 0.0;
  385. if (item)
  386. {
  387. self->client->v_angle[YAW] -= spread;
  388. drop = Drop_Item (self, item);
  389. self->client->v_angle[YAW] += spread;
  390. drop->spawnflags = DROPPED_PLAYER_ITEM;
  391. }
  392. if (quad)
  393. {
  394. self->client->v_angle[YAW] += spread;
  395. drop = Drop_Item (self, FindItemByClassname ("item_quad"));
  396. self->client->v_angle[YAW] -= spread;
  397. drop->spawnflags |= DROPPED_PLAYER_ITEM;
  398. drop->touch = Touch_Item;
  399. drop->nextthink = level.time + (self->client->quad_framenum - level.framenum) * FRAMETIME;
  400. drop->think = G_FreeEdict;
  401. }
  402. }
  403. /*
  404. ==================
  405. LookAtKiller
  406. ==================
  407. */
  408. void LookAtKiller (edict_t *self, edict_t *inflictor, edict_t *attacker)
  409. {
  410. vec3_t dir;
  411. if (attacker && attacker != world && attacker != self)
  412. {
  413. VectorSubtract (attacker->s.origin, self->s.origin, dir);
  414. }
  415. else if (inflictor && inflictor != world && inflictor != self)
  416. {
  417. VectorSubtract (inflictor->s.origin, self->s.origin, dir);
  418. }
  419. else
  420. {
  421. self->client->killer_yaw = self->s.angles[YAW];
  422. return;
  423. }
  424. if (dir[0])
  425. self->client->killer_yaw = 180/M_PI*atan2(dir[1], dir[0]);
  426. else {
  427. self->client->killer_yaw = 0;
  428. if (dir[1] > 0)
  429. self->client->killer_yaw = 90;
  430. else if (dir[1] < 0)
  431. self->client->killer_yaw = -90;
  432. }
  433. if (self->client->killer_yaw < 0)
  434. self->client->killer_yaw += 360;
  435. }
  436. /*
  437. ==================
  438. player_die
  439. ==================
  440. */
  441. void player_die (edict_t *self, edict_t *inflictor, edict_t *attacker, int damage, vec3_t point)
  442. {
  443. int n;
  444. VectorClear (self->avelocity);
  445. self->takedamage = DAMAGE_YES;
  446. self->movetype = MOVETYPE_TOSS;
  447. self->s.modelindex2 = 0; // remove linked weapon model
  448. self->s.angles[0] = 0;
  449. self->s.angles[2] = 0;
  450. self->s.sound = 0;
  451. self->client->weapon_sound = 0;
  452. self->maxs[2] = -8;
  453. // self->solid = SOLID_NOT;
  454. self->svflags |= SVF_DEADMONSTER;
  455. if (!self->deadflag)
  456. {
  457. self->client->respawn_time = level.time + 1.0;
  458. LookAtKiller (self, inflictor, attacker);
  459. self->client->ps.pmove.pm_type = PM_DEAD;
  460. ClientObituary (self, inflictor, attacker);
  461. TossClientWeapon (self);
  462. if (deathmatch->value)
  463. Cmd_Help_f (self); // show scores
  464. // clear inventory
  465. // this is kind of ugly, but it's how we want to handle keys in coop
  466. for (n = 0; n < game.num_items; n++)
  467. {
  468. if (coop->value && itemlist[n].flags & IT_KEY)
  469. self->client->resp.coop_respawn.inventory[n] = self->client->pers.inventory[n];
  470. self->client->pers.inventory[n] = 0;
  471. }
  472. }
  473. // remove powerups
  474. self->client->quad_framenum = 0;
  475. self->client->invincible_framenum = 0;
  476. self->client->breather_framenum = 0;
  477. self->client->enviro_framenum = 0;
  478. self->flags &= ~FL_POWER_ARMOR;
  479. if (self->health < -40)
  480. { // gib
  481. gi.sound (self, CHAN_BODY, gi.soundindex ("misc/udeath.wav"), 1, ATTN_NORM, 0);
  482. for (n= 0; n < 4; n++)
  483. ThrowGib (self, "models/objects/gibs/sm_meat/tris.md2", damage, GIB_ORGANIC);
  484. ThrowClientHead (self, damage);
  485. self->takedamage = DAMAGE_NO;
  486. }
  487. else
  488. { // normal death
  489. if (!self->deadflag)
  490. {
  491. static int i;
  492. i = (i+1)%3;
  493. // start a death animation
  494. self->client->anim_priority = ANIM_DEATH;
  495. if (self->client->ps.pmove.pm_flags & PMF_DUCKED)
  496. {
  497. self->s.frame = FRAME_crdeath1-1;
  498. self->client->anim_end = FRAME_crdeath5;
  499. }
  500. else switch (i)
  501. {
  502. case 0:
  503. self->s.frame = FRAME_death101-1;
  504. self->client->anim_end = FRAME_death106;
  505. break;
  506. case 1:
  507. self->s.frame = FRAME_death201-1;
  508. self->client->anim_end = FRAME_death206;
  509. break;
  510. case 2:
  511. self->s.frame = FRAME_death301-1;
  512. self->client->anim_end = FRAME_death308;
  513. break;
  514. }
  515. gi.sound (self, CHAN_VOICE, gi.soundindex(va("*death%i.wav", (rand()%4)+1)), 1, ATTN_NORM, 0);
  516. }
  517. }
  518. self->deadflag = DEAD_DEAD;
  519. gi.linkentity (self);
  520. }
  521. //=======================================================================
  522. /*
  523. ==============
  524. InitClientPersistant
  525. This is only called when the game first initializes in single player,
  526. but is called after each death and level change in deathmatch
  527. ==============
  528. */
  529. void InitClientPersistant (gclient_t *client)
  530. {
  531. gitem_t *item;
  532. memset (&client->pers, 0, sizeof(client->pers));
  533. item = FindItem("Blaster");
  534. client->pers.selected_item = ITEM_INDEX(item);
  535. client->pers.inventory[client->pers.selected_item] = 1;
  536. client->pers.weapon = item;
  537. client->pers.health = 100;
  538. client->pers.max_health = 100;
  539. client->pers.max_bullets = 200;
  540. client->pers.max_shells = 100;
  541. client->pers.max_rockets = 50;
  542. client->pers.max_grenades = 50;
  543. client->pers.max_cells = 200;
  544. client->pers.max_slugs = 50;
  545. client->pers.connected = true;
  546. }
  547. void InitClientResp (gclient_t *client)
  548. {
  549. memset (&client->resp, 0, sizeof(client->resp));
  550. client->resp.enterframe = level.framenum;
  551. client->resp.coop_respawn = client->pers;
  552. }
  553. /*
  554. ==================
  555. SaveClientData
  556. Some information that should be persistant, like health,
  557. is still stored in the edict structure, so it needs to
  558. be mirrored out to the client structure before all the
  559. edicts are wiped.
  560. ==================
  561. */
  562. void SaveClientData (void)
  563. {
  564. int i;
  565. edict_t *ent;
  566. for (i=0 ; i<game.maxclients ; i++)
  567. {
  568. ent = &g_edicts[1+i];
  569. if (!ent->inuse)
  570. continue;
  571. game.clients[i].pers.health = ent->health;
  572. game.clients[i].pers.max_health = ent->max_health;
  573. game.clients[i].pers.savedFlags = (ent->flags & (FL_GODMODE|FL_NOTARGET|FL_POWER_ARMOR));
  574. if (coop->value)
  575. game.clients[i].pers.score = ent->client->resp.score;
  576. }
  577. }
  578. void FetchClientEntData (edict_t *ent)
  579. {
  580. ent->health = ent->client->pers.health;
  581. ent->max_health = ent->client->pers.max_health;
  582. ent->flags |= ent->client->pers.savedFlags;
  583. if (coop->value)
  584. ent->client->resp.score = ent->client->pers.score;
  585. }
  586. /*
  587. =======================================================================
  588. SelectSpawnPoint
  589. =======================================================================
  590. */
  591. /*
  592. ================
  593. PlayersRangeFromSpot
  594. Returns the distance to the nearest player from the given spot
  595. ================
  596. */
  597. float PlayersRangeFromSpot (edict_t *spot)
  598. {
  599. edict_t *player;
  600. float bestplayerdistance;
  601. vec3_t v;
  602. int n;
  603. float playerdistance;
  604. bestplayerdistance = 9999999;
  605. for (n = 1; n <= maxclients->value; n++)
  606. {
  607. player = &g_edicts[n];
  608. if (!player->inuse)
  609. continue;
  610. if (player->health <= 0)
  611. continue;
  612. VectorSubtract (spot->s.origin, player->s.origin, v);
  613. playerdistance = VectorLength (v);
  614. if (playerdistance < bestplayerdistance)
  615. bestplayerdistance = playerdistance;
  616. }
  617. return bestplayerdistance;
  618. }
  619. /*
  620. ================
  621. SelectRandomDeathmatchSpawnPoint
  622. go to a random point, but NOT the two points closest
  623. to other players
  624. ================
  625. */
  626. edict_t *SelectRandomDeathmatchSpawnPoint (void)
  627. {
  628. edict_t *spot, *spot1, *spot2;
  629. int count = 0;
  630. int selection;
  631. float range, range1, range2;
  632. spot = NULL;
  633. range1 = range2 = 99999;
  634. spot1 = spot2 = NULL;
  635. while ((spot = G_Find (spot, FOFS(classname), "info_player_deathmatch")) != NULL)
  636. {
  637. count++;
  638. range = PlayersRangeFromSpot(spot);
  639. if (range < range1)
  640. {
  641. range1 = range;
  642. spot1 = spot;
  643. }
  644. else if (range < range2)
  645. {
  646. range2 = range;
  647. spot2 = spot;
  648. }
  649. }
  650. if (!count)
  651. return NULL;
  652. if (count <= 2)
  653. {
  654. spot1 = spot2 = NULL;
  655. }
  656. else
  657. count -= 2;
  658. selection = rand() % count;
  659. spot = NULL;
  660. do
  661. {
  662. spot = G_Find (spot, FOFS(classname), "info_player_deathmatch");
  663. if (spot == spot1 || spot == spot2)
  664. selection++;
  665. } while(selection--);
  666. return spot;
  667. }
  668. /*
  669. ================
  670. SelectFarthestDeathmatchSpawnPoint
  671. ================
  672. */
  673. edict_t *SelectFarthestDeathmatchSpawnPoint (void)
  674. {
  675. edict_t *bestspot;
  676. float bestdistance, bestplayerdistance;
  677. edict_t *spot;
  678. spot = NULL;
  679. bestspot = NULL;
  680. bestdistance = 0;
  681. while ((spot = G_Find (spot, FOFS(classname), "info_player_deathmatch")) != NULL)
  682. {
  683. bestplayerdistance = PlayersRangeFromSpot (spot);
  684. if (bestplayerdistance > bestdistance)
  685. {
  686. bestspot = spot;
  687. bestdistance = bestplayerdistance;
  688. }
  689. }
  690. if (bestspot)
  691. {
  692. return bestspot;
  693. }
  694. // if there is a player just spawned on each and every start spot
  695. // we have no choice to turn one into a telefrag meltdown
  696. spot = G_Find (NULL, FOFS(classname), "info_player_deathmatch");
  697. return spot;
  698. }
  699. edict_t *SelectDeathmatchSpawnPoint (void)
  700. {
  701. if ( (int)(dmflags->value) & DF_SPAWN_FARTHEST)
  702. return SelectFarthestDeathmatchSpawnPoint ();
  703. else
  704. return SelectRandomDeathmatchSpawnPoint ();
  705. }
  706. edict_t *SelectCoopSpawnPoint (edict_t *ent)
  707. {
  708. int index;
  709. edict_t *spot = NULL;
  710. char *target;
  711. index = ent->client - game.clients;
  712. // player 0 starts in normal player spawn point
  713. if (!index)
  714. return NULL;
  715. spot = NULL;
  716. // assume there are four coop spots at each spawnpoint
  717. while (1)
  718. {
  719. spot = G_Find (spot, FOFS(classname), "info_player_coop");
  720. if (!spot)
  721. return NULL; // we didn't have enough...
  722. target = spot->targetname;
  723. if (!target)
  724. target = "";
  725. if ( Q_stricmp(game.spawnpoint, target) == 0 )
  726. { // this is a coop spawn point for one of the clients here
  727. index--;
  728. if (!index)
  729. return spot; // this is it
  730. }
  731. }
  732. return spot;
  733. }
  734. /*
  735. ===========
  736. SelectSpawnPoint
  737. Chooses a player start, deathmatch start, coop start, etc
  738. ============
  739. */
  740. void SelectSpawnPoint (edict_t *ent, vec3_t origin, vec3_t angles)
  741. {
  742. edict_t *spot = NULL;
  743. if (deathmatch->value)
  744. spot = SelectDeathmatchSpawnPoint ();
  745. else if (coop->value)
  746. spot = SelectCoopSpawnPoint (ent);
  747. // find a single player start spot
  748. if (!spot)
  749. {
  750. while ((spot = G_Find (spot, FOFS(classname), "info_player_start")) != NULL)
  751. {
  752. if (!game.spawnpoint[0] && !spot->targetname)
  753. break;
  754. if (!game.spawnpoint[0] || !spot->targetname)
  755. continue;
  756. if (Q_stricmp(game.spawnpoint, spot->targetname) == 0)
  757. break;
  758. }
  759. if (!spot)
  760. {
  761. if (!game.spawnpoint[0])
  762. { // there wasn't a spawnpoint without a target, so use any
  763. spot = G_Find (spot, FOFS(classname), "info_player_start");
  764. }
  765. if (!spot)
  766. gi.error ("Couldn't find spawn point %s\n", game.spawnpoint);
  767. }
  768. }
  769. VectorCopy (spot->s.origin, origin);
  770. origin[2] += 9;
  771. VectorCopy (spot->s.angles, angles);
  772. }
  773. //======================================================================
  774. void InitBodyQue (void)
  775. {
  776. int i;
  777. edict_t *ent;
  778. level.body_que = 0;
  779. for (i=0; i<BODY_QUEUE_SIZE ; i++)
  780. {
  781. ent = G_Spawn();
  782. ent->classname = "bodyque";
  783. }
  784. }
  785. void body_die (edict_t *self, edict_t *inflictor, edict_t *attacker, int damage, vec3_t point)
  786. {
  787. int n;
  788. if (self->health < -40)
  789. {
  790. gi.sound (self, CHAN_BODY, gi.soundindex ("misc/udeath.wav"), 1, ATTN_NORM, 0);
  791. for (n= 0; n < 4; n++)
  792. ThrowGib (self, "models/objects/gibs/sm_meat/tris.md2", damage, GIB_ORGANIC);
  793. self->s.origin[2] -= 48;
  794. ThrowClientHead (self, damage);
  795. self->takedamage = DAMAGE_NO;
  796. }
  797. }
  798. void CopyToBodyQue (edict_t *ent)
  799. {
  800. edict_t *body;
  801. // grab a body que and cycle to the next one
  802. body = &g_edicts[(int)maxclients->value + level.body_que + 1];
  803. level.body_que = (level.body_que + 1) % BODY_QUEUE_SIZE;
  804. // FIXME: send an effect on the removed body
  805. gi.unlinkentity (ent);
  806. gi.unlinkentity (body);
  807. body->s = ent->s;
  808. body->s.number = body - g_edicts;
  809. body->svflags = ent->svflags;
  810. VectorCopy (ent->mins, body->mins);
  811. VectorCopy (ent->maxs, body->maxs);
  812. VectorCopy (ent->absmin, body->absmin);
  813. VectorCopy (ent->absmax, body->absmax);
  814. VectorCopy (ent->size, body->size);
  815. body->solid = ent->solid;
  816. body->clipmask = ent->clipmask;
  817. body->owner = ent->owner;
  818. body->movetype = ent->movetype;
  819. body->die = body_die;
  820. body->takedamage = DAMAGE_YES;
  821. gi.linkentity (body);
  822. }
  823. void respawn (edict_t *self)
  824. {
  825. if (deathmatch->value || coop->value)
  826. {
  827. // spectator's don't leave bodies
  828. if (self->movetype != MOVETYPE_NOCLIP)
  829. CopyToBodyQue (self);
  830. self->svflags &= ~SVF_NOCLIENT;
  831. PutClientInServer (self);
  832. // add a teleportation effect
  833. self->s.event = EV_PLAYER_TELEPORT;
  834. // hold in place briefly
  835. self->client->ps.pmove.pm_flags = PMF_TIME_TELEPORT;
  836. self->client->ps.pmove.pm_time = 14;
  837. self->client->respawn_time = level.time;
  838. return;
  839. }
  840. // restart the entire server
  841. gi.AddCommandString ("menu_loadgame\n");
  842. }
  843. /*
  844. * only called when pers.spectator changes
  845. * note that resp.spectator should be the opposite of pers.spectator here
  846. */
  847. void spectator_respawn (edict_t *ent)
  848. {
  849. int i, numspec;
  850. // if the user wants to become a spectator, make sure he doesn't
  851. // exceed max_spectators
  852. if (ent->client->pers.spectator) {
  853. char *value = Info_ValueForKey (ent->client->pers.userinfo, "spectator");
  854. if (*spectator_password->string &&
  855. strcmp(spectator_password->string, "none") &&
  856. strcmp(spectator_password->string, value)) {
  857. gi.cprintf(ent, PRINT_HIGH, "Spectator password incorrect.\n");
  858. ent->client->pers.spectator = false;
  859. gi.WriteByte (svc_stufftext);
  860. gi.WriteString ("spectator 0\n");
  861. gi.unicast(ent, true);
  862. return;
  863. }
  864. // count spectators
  865. for (i = 1, numspec = 0; i <= maxclients->value; i++)
  866. if (g_edicts[i].inuse && g_edicts[i].client->pers.spectator)
  867. numspec++;
  868. if (numspec >= maxspectators->value) {
  869. gi.cprintf(ent, PRINT_HIGH, "Server spectator limit is full.");
  870. ent->client->pers.spectator = false;
  871. // reset his spectator var
  872. gi.WriteByte (svc_stufftext);
  873. gi.WriteString ("spectator 0\n");
  874. gi.unicast(ent, true);
  875. return;
  876. }
  877. } else {
  878. // he was a spectator and wants to join the game
  879. // he must have the right password
  880. char *value = Info_ValueForKey (ent->client->pers.userinfo, "password");
  881. if (*password->string && strcmp(password->string, "none") &&
  882. strcmp(password->string, value)) {
  883. gi.cprintf(ent, PRINT_HIGH, "Password incorrect.\n");
  884. ent->client->pers.spectator = true;
  885. gi.WriteByte (svc_stufftext);
  886. gi.WriteString ("spectator 1\n");
  887. gi.unicast(ent, true);
  888. return;
  889. }
  890. }
  891. // clear score on respawn
  892. ent->client->pers.score = ent->client->resp.score = 0;
  893. ent->svflags &= ~SVF_NOCLIENT;
  894. PutClientInServer (ent);
  895. // add a teleportation effect
  896. if (!ent->client->pers.spectator) {
  897. // send effect
  898. gi.WriteByte (svc_muzzleflash);
  899. gi.WriteShort (ent-g_edicts);
  900. gi.WriteByte (MZ_LOGIN);
  901. gi.multicast (ent->s.origin, MULTICAST_PVS);
  902. // hold in place briefly
  903. ent->client->ps.pmove.pm_flags = PMF_TIME_TELEPORT;
  904. ent->client->ps.pmove.pm_time = 14;
  905. }
  906. ent->client->respawn_time = level.time;
  907. if (ent->client->pers.spectator)
  908. gi.bprintf (PRINT_HIGH, "%s has moved to the sidelines\n", ent->client->pers.netname);
  909. else
  910. gi.bprintf (PRINT_HIGH, "%s joined the game\n", ent->client->pers.netname);
  911. }
  912. //==============================================================
  913. /*
  914. ===========
  915. PutClientInServer
  916. Called when a player connects to a server or respawns in
  917. a deathmatch.
  918. ============
  919. */
  920. void PutClientInServer (edict_t *ent)
  921. {
  922. vec3_t mins = {-16, -16, -24};
  923. vec3_t maxs = {16, 16, 32};
  924. int index;
  925. vec3_t spawn_origin, spawn_angles;
  926. gclient_t *client;
  927. int i;
  928. client_persistant_t saved;
  929. client_respawn_t resp;
  930. // find a spawn point
  931. // do it before setting health back up, so farthest
  932. // ranging doesn't count this client
  933. SelectSpawnPoint (ent, spawn_origin, spawn_angles);
  934. index = ent-g_edicts-1;
  935. client = ent->client;
  936. // deathmatch wipes most client data every spawn
  937. if (deathmatch->value)
  938. {
  939. char userinfo[MAX_INFO_STRING];
  940. resp = client->resp;
  941. memcpy (userinfo, client->pers.userinfo, sizeof(userinfo));
  942. InitClientPersistant (client);
  943. ClientUserinfoChanged (ent, userinfo);
  944. }
  945. else if (coop->value)
  946. {
  947. // int n;
  948. char userinfo[MAX_INFO_STRING];
  949. resp = client->resp;
  950. memcpy (userinfo, client->pers.userinfo, sizeof(userinfo));
  951. // this is kind of ugly, but it's how we want to handle keys in coop
  952. // for (n = 0; n < game.num_items; n++)
  953. // {
  954. // if (itemlist[n].flags & IT_KEY)
  955. // resp.coop_respawn.inventory[n] = client->pers.inventory[n];
  956. // }
  957. resp.coop_respawn.game_helpchanged = client->pers.game_helpchanged;
  958. resp.coop_respawn.helpchanged = client->pers.helpchanged;
  959. client->pers = resp.coop_respawn;
  960. ClientUserinfoChanged (ent, userinfo);
  961. if (resp.score > client->pers.score)
  962. client->pers.score = resp.score;
  963. }
  964. else
  965. {
  966. memset (&resp, 0, sizeof(resp));
  967. }
  968. // clear everything but the persistant data
  969. saved = client->pers;
  970. memset (client, 0, sizeof(*client));
  971. client->pers = saved;
  972. if (client->pers.health <= 0)
  973. InitClientPersistant(client);
  974. client->resp = resp;
  975. // copy some data from the client to the entity
  976. FetchClientEntData (ent);
  977. // clear entity values
  978. ent->groundentity = NULL;
  979. ent->client = &game.clients[index];
  980. ent->takedamage = DAMAGE_AIM;
  981. ent->movetype = MOVETYPE_WALK;
  982. ent->viewheight = 22;
  983. ent->inuse = true;
  984. ent->classname = "player";
  985. ent->mass = 200;
  986. ent->solid = SOLID_BBOX;
  987. ent->deadflag = DEAD_NO;
  988. ent->air_finished = level.time + 12;
  989. ent->clipmask = MASK_PLAYERSOLID;
  990. ent->model = "players/male/tris.md2";
  991. ent->pain = player_pain;
  992. ent->die = player_die;
  993. ent->waterlevel = 0;
  994. ent->watertype = 0;
  995. ent->flags &= ~FL_NO_KNOCKBACK;
  996. ent->svflags &= ~SVF_DEADMONSTER;
  997. VectorCopy (mins, ent->mins);
  998. VectorCopy (maxs, ent->maxs);
  999. VectorClear (ent->velocity);
  1000. // clear playerstate values
  1001. memset (&ent->client->ps, 0, sizeof(client->ps));
  1002. client->ps.pmove.origin[0] = spawn_origin[0]*8;
  1003. client->ps.pmove.origin[1] = spawn_origin[1]*8;
  1004. client->ps.pmove.origin[2] = spawn_origin[2]*8;
  1005. if (deathmatch->value && ((int)dmflags->value & DF_FIXED_FOV))
  1006. {
  1007. client->ps.fov = 90;
  1008. }
  1009. else
  1010. {
  1011. client->ps.fov = atoi(Info_ValueForKey(client->pers.userinfo, "fov"));
  1012. if (client->ps.fov < 1)
  1013. client->ps.fov = 90;
  1014. else if (client->ps.fov > 160)
  1015. client->ps.fov = 160;
  1016. }
  1017. client->ps.gunindex = gi.modelindex(client->pers.weapon->view_model);
  1018. // clear entity state values
  1019. ent->s.effects = 0;
  1020. ent->s.modelindex = 255; // will use the skin specified model
  1021. ent->s.modelindex2 = 255; // custom gun model
  1022. // sknum is player num and weapon number
  1023. // weapon number will be added in changeweapon
  1024. ent->s.skinnum = ent - g_edicts - 1;
  1025. ent->s.frame = 0;
  1026. VectorCopy (spawn_origin, ent->s.origin);
  1027. ent->s.origin[2] += 1; // make sure off ground
  1028. VectorCopy (ent->s.origin, ent->s.old_origin);
  1029. // set the delta angle
  1030. for (i=0 ; i<3 ; i++)
  1031. client->ps.pmove.delta_angles[i] = ANGLE2SHORT(spawn_angles[i] - client->resp.cmd_angles[i]);
  1032. ent->s.angles[PITCH] = 0;
  1033. ent->s.angles[YAW] = spawn_angles[YAW];
  1034. ent->s.angles[ROLL] = 0;
  1035. VectorCopy (ent->s.angles, client->ps.viewangles);
  1036. VectorCopy (ent->s.angles, client->v_angle);
  1037. // spawn a spectator
  1038. if (client->pers.spectator) {
  1039. client->chase_target = NULL;
  1040. client->resp.spectator = true;
  1041. ent->movetype = MOVETYPE_NOCLIP;
  1042. ent->solid = SOLID_NOT;
  1043. ent->svflags |= SVF_NOCLIENT;
  1044. ent->client->ps.gunindex = 0;
  1045. gi.linkentity (ent);
  1046. return;
  1047. } else
  1048. client->resp.spectator = false;
  1049. if (!KillBox (ent))
  1050. { // could't spawn in?
  1051. }
  1052. gi.linkentity (ent);
  1053. // force the current weapon up
  1054. client->newweapon = client->pers.weapon;
  1055. ChangeWeapon (ent);
  1056. }
  1057. /*
  1058. =====================
  1059. ClientBeginDeathmatch
  1060. A client has just connected to the server in
  1061. deathmatch mode, so clear everything out before starting them.
  1062. =====================
  1063. */
  1064. void ClientBeginDeathmatch (edict_t *ent)
  1065. {
  1066. G_InitEdict (ent);
  1067. InitClientResp (ent->client);
  1068. // locate ent at a spawn point
  1069. PutClientInServer (ent);
  1070. // send effect
  1071. gi.WriteByte (svc_muzzleflash);
  1072. gi.WriteShort (ent-g_edicts);
  1073. gi.WriteByte (MZ_LOGIN);
  1074. gi.multicast (ent->s.origin, MULTICAST_PVS);
  1075. gi.bprintf (PRINT_HIGH, "%s entered the game\n", ent->client->pers.netname);
  1076. // make sure all view stuff is valid
  1077. ClientEndServerFrame (ent);
  1078. }
  1079. /*
  1080. ===========
  1081. ClientBegin
  1082. called when a client has finished connecting, and is ready
  1083. to be placed into the game. This will happen every level load.
  1084. ============
  1085. */
  1086. void ClientBegin (edict_t *ent)
  1087. {
  1088. int i;
  1089. ent->client = game.clients + (ent - g_edicts - 1);
  1090. if (deathmatch->value)
  1091. {
  1092. ClientBeginDeathmatch (ent);
  1093. return;
  1094. }
  1095. // if there is already a body waiting for us (a loadgame), just
  1096. // take it, otherwise spawn one from scratch
  1097. if (ent->inuse == true)
  1098. {
  1099. // the client has cleared the client side viewangles upon
  1100. // connecting to the server, which is different than the
  1101. // state when the game is saved, so we need to compensate
  1102. // with deltaangles
  1103. for (i=0 ; i<3 ; i++)
  1104. ent->client->ps.pmove.delta_angles[i] = ANGLE2SHORT(ent->client->ps.viewangles[i]);
  1105. }
  1106. else
  1107. {
  1108. // a spawn point will completely reinitialize the entity
  1109. // except for the persistant data that was initialized at
  1110. // ClientConnect() time
  1111. G_InitEdict (ent);
  1112. ent->classname = "player";
  1113. InitClientResp (ent->client);
  1114. PutClientInServer (ent);
  1115. }
  1116. if (level.intermissiontime)
  1117. {
  1118. MoveClientToIntermission (ent);
  1119. }
  1120. else
  1121. {
  1122. // send effect if in a multiplayer game
  1123. if (game.maxclients > 1)
  1124. {
  1125. gi.WriteByte (svc_muzzleflash);
  1126. gi.WriteShort (ent-g_edicts);
  1127. gi.WriteByte (MZ_LOGIN);
  1128. gi.multicast (ent->s.origin, MULTICAST_PVS);
  1129. gi.bprintf (PRINT_HIGH, "%s entered the game\n", ent->client->pers.netname);
  1130. }
  1131. }
  1132. // make sure all view stuff is valid
  1133. ClientEndServerFrame (ent);
  1134. }
  1135. /*
  1136. ===========
  1137. ClientUserInfoChanged
  1138. called whenever the player updates a userinfo variable.
  1139. The game can override any of the settings in place
  1140. (forcing skins or names, etc) before copying it off.
  1141. ============
  1142. */
  1143. void ClientUserinfoChanged (edict_t *ent, char *userinfo)
  1144. {
  1145. char *s;
  1146. int playernum;
  1147. // check for malformed or illegal info strings
  1148. if (!Info_Validate(userinfo))
  1149. {
  1150. strcpy (userinfo, "\\name\\badinfo\\skin\\male/grunt");
  1151. }
  1152. // set name
  1153. s = Info_ValueForKey (userinfo, "name");
  1154. strncpy (ent->client->pers.netname, s, sizeof(ent->client->pers.netname)-1);
  1155. // set spectator
  1156. s = Info_ValueForKey (userinfo, "spectator");
  1157. // spectators are only supported in deathmatch
  1158. if (deathmatch->value && *s && strcmp(s, "0"))
  1159. ent->client->pers.spectator = true;
  1160. else
  1161. ent->client->pers.spectator = false;
  1162. // set skin
  1163. s = Info_ValueForKey (userinfo, "skin");
  1164. playernum = ent-g_edicts-1;
  1165. // combine name and skin into a configstring
  1166. gi.configstring (CS_PLAYERSKINS+playernum, va("%s\\%s", ent->client->pers.netname, s) );
  1167. // fov
  1168. if (deathmatch->value && ((int)dmflags->value & DF_FIXED_FOV))
  1169. {
  1170. ent->client->ps.fov = 90;
  1171. }
  1172. else
  1173. {
  1174. ent->client->ps.fov = atoi(Info_ValueForKey(userinfo, "fov"));
  1175. if (ent->client->ps.fov < 1)
  1176. ent->client->ps.fov = 90;
  1177. else if (ent->client->ps.fov > 160)
  1178. ent->client->ps.fov = 160;
  1179. }
  1180. // handedness
  1181. s = Info_ValueForKey (userinfo, "hand");
  1182. if (strlen(s))
  1183. {
  1184. ent->client->pers.hand = atoi(s);
  1185. }
  1186. // save off the userinfo in case we want to check something later
  1187. strncpy (ent->client->pers.userinfo, userinfo, sizeof(ent->client->pers.userinfo)-1);
  1188. }
  1189. /*
  1190. ===========
  1191. ClientConnect
  1192. Called when a player begins connecting to the server.
  1193. The game can refuse entrance to a client by returning false.
  1194. If the client is allowed, the connection process will continue
  1195. and eventually get to ClientBegin()
  1196. Changing levels will NOT cause this to be called again, but
  1197. loadgames will.
  1198. ============
  1199. */
  1200. qboolean ClientConnect (edict_t *ent, char *userinfo)
  1201. {
  1202. char *value;
  1203. // check to see if they are on the banned IP list
  1204. value = Info_ValueForKey (userinfo, "ip");
  1205. if (SV_FilterPacket(value)) {
  1206. Info_SetValueForKey(userinfo, "rejmsg", "Banned.");
  1207. return false;
  1208. }
  1209. // check for a spectator
  1210. value = Info_ValueForKey (userinfo, "spectator");
  1211. if (deathmatch->value && *value && strcmp(value, "0")) {
  1212. int i, numspec;
  1213. if (*spectator_password->string &&
  1214. strcmp(spectator_password->string, "none") &&
  1215. strcmp(spectator_password->string, value)) {
  1216. Info_SetValueForKey(userinfo, "rejmsg", "Spectator password required or incorrect.");
  1217. return false;
  1218. }
  1219. // count spectators
  1220. for (i = numspec = 0; i < maxclients->value; i++)
  1221. if (g_edicts[i+1].inuse && g_edicts[i+1].client->pers.spectator)
  1222. numspec++;
  1223. if (numspec >= maxspectators->value) {
  1224. Info_SetValueForKey(userinfo, "rejmsg", "Server spectator limit is full.");
  1225. return false;
  1226. }
  1227. } else {
  1228. // check for a password
  1229. value = Info_ValueForKey (userinfo, "password");
  1230. if (*password->string && strcmp(password->string, "none") &&
  1231. strcmp(password->string, value)) {
  1232. Info_SetValueForKey(userinfo, "rejmsg", "Password required or incorrect.");
  1233. return false;
  1234. }
  1235. }
  1236. // they can connect
  1237. ent->client = game.clients + (ent - g_edicts - 1);
  1238. // if there is already a body waiting for us (a loadgame), just
  1239. // take it, otherwise spawn one from scratch
  1240. if (ent->inuse == false)
  1241. {
  1242. // clear the respawning variables
  1243. InitClientResp (ent->client);
  1244. if (!game.autosaved || !ent->client->pers.weapon)
  1245. InitClientPersistant (ent->client);
  1246. }
  1247. ClientUserinfoChanged (ent, userinfo);
  1248. if (game.maxclients > 1)
  1249. gi.dprintf ("%s connected\n", ent->client->pers.netname);
  1250. ent->client->pers.connected = true;
  1251. return true;
  1252. }
  1253. /*
  1254. ===========
  1255. ClientDisconnect
  1256. Called when a player drops from the server.
  1257. Will not be called between levels.
  1258. ============
  1259. */
  1260. void ClientDisconnect (edict_t *ent)
  1261. {
  1262. int playernum;
  1263. if (!ent->client)
  1264. return;
  1265. gi.bprintf (PRINT_HIGH, "%s disconnected\n", ent->client->pers.netname);
  1266. // send effect
  1267. gi.WriteByte (svc_muzzleflash);
  1268. gi.WriteShort (ent-g_edicts);
  1269. gi.WriteByte (MZ_LOGOUT);
  1270. gi.multicast (ent->s.origin, MULTICAST_PVS);
  1271. gi.unlinkentity (ent);
  1272. ent->s.modelindex = 0;
  1273. ent->solid = SOLID_NOT;
  1274. ent->inuse = false;
  1275. ent->classname = "disconnected";
  1276. ent->client->pers.connected = false;
  1277. playernum = ent-g_edicts-1;
  1278. gi.configstring (CS_PLAYERSKINS+playernum, "");
  1279. }
  1280. //==============================================================
  1281. edict_t *pm_passent;
  1282. // pmove doesn't need to know about passent and contentmask
  1283. trace_t PM_trace (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end)
  1284. {
  1285. if (pm_passent->health > 0)
  1286. return gi.trace (start, mins, maxs, end, pm_passent, MASK_PLAYERSOLID);
  1287. else
  1288. return gi.trace (start, mins, maxs, end, pm_passent, MASK_DEADSOLID);
  1289. }
  1290. unsigned CheckBlock (void *b, int c)
  1291. {
  1292. int v,i;
  1293. v = 0;
  1294. for (i=0 ; i<c ; i++)
  1295. v+= ((byte *)b)[i];
  1296. return v;
  1297. }
  1298. void PrintPmove (pmove_t *pm)
  1299. {
  1300. unsigned c1, c2;
  1301. c1 = CheckBlock (&pm->s, sizeof(pm->s));
  1302. c2 = CheckBlock (&pm->cmd, sizeof(pm->cmd));
  1303. Com_Printf ("sv %3i:%i %i\n", pm->cmd.impulse, c1, c2);
  1304. }
  1305. /*
  1306. ==============
  1307. ClientThink
  1308. This will be called once for each client frame, which will
  1309. usually be a couple times for each server frame.
  1310. ==============
  1311. */
  1312. void ClientThink (edict_t *ent, usercmd_t *ucmd)
  1313. {
  1314. gclient_t *client;
  1315. edict_t *other;
  1316. int i, j;
  1317. pmove_t pm;
  1318. level.current_entity = ent;
  1319. client = ent->client;
  1320. if (level.intermissiontime)
  1321. {
  1322. client->ps.pmove.pm_type = PM_FREEZE;
  1323. // can exit intermission after five seconds
  1324. if (level.time > level.intermissiontime + 5.0
  1325. && (ucmd->buttons & BUTTON_ANY) )
  1326. level.exitintermission = true;
  1327. return;
  1328. }
  1329. pm_passent = ent;
  1330. if (ent->client->chase_target) {
  1331. client->resp.cmd_angles[0] = SHORT2ANGLE(ucmd->angles[0]);
  1332. client->resp.cmd_angles[1] = SHORT2ANGLE(ucmd->angles[1]);
  1333. client->resp.cmd_angles[2] = SHORT2ANGLE(ucmd->angles[2]);
  1334. } else {
  1335. // set up for pmove
  1336. memset (&pm, 0, sizeof(pm));
  1337. if (ent->movetype == MOVETYPE_NOCLIP)
  1338. client->ps.pmove.pm_type = PM_SPECTATOR;
  1339. else if (ent->s.modelindex != 255)
  1340. client->ps.pmove.pm_type = PM_GIB;
  1341. else if (ent->deadflag)
  1342. client->ps.pmove.pm_type = PM_DEAD;
  1343. else
  1344. client->ps.pmove.pm_type = PM_NORMAL;
  1345. client->ps.pmove.gravity = sv_gravity->value;
  1346. pm.s = client->ps.pmove;
  1347. for (i=0 ; i<3 ; i++)
  1348. {
  1349. pm.s.origin[i] = ent->s.origin[i]*8;
  1350. pm.s.velocity[i] = ent->velocity[i]*8;
  1351. }
  1352. if (memcmp(&client->old_pmove, &pm.s, sizeof(pm.s)))
  1353. {
  1354. pm.snapinitial = true;
  1355. // gi.dprintf ("pmove changed!\n");
  1356. }
  1357. pm.cmd = *ucmd;
  1358. pm.trace = PM_trace; // adds default parms
  1359. pm.pointcontents = gi.pointcontents;
  1360. // perform a pmove
  1361. gi.Pmove (&pm);
  1362. // save results of pmove
  1363. client->ps.pmove = pm.s;
  1364. client->old_pmove = pm.s;
  1365. for (i=0 ; i<3 ; i++)
  1366. {
  1367. ent->s.origin[i] = pm.s.origin[i]*0.125;
  1368. ent->velocity[i] = pm.s.velocity[i]*0.125;
  1369. }
  1370. VectorCopy (pm.mins, ent->mins);
  1371. VectorCopy (pm.maxs, ent->maxs);
  1372. client->resp.cmd_angles[0] = SHORT2ANGLE(ucmd->angles[0]);
  1373. client->resp.cmd_angles[1] = SHORT2ANGLE(ucmd->angles[1]);
  1374. client->resp.cmd_angles[2] = SHORT2ANGLE(ucmd->angles[2]);
  1375. if (ent->groundentity && !pm.groundentity && (pm.cmd.upmove >= 10) && (pm.waterlevel == 0))
  1376. {
  1377. gi.sound(ent, CHAN_VOICE, gi.soundindex("*jump1.wav"), 1, ATTN_NORM, 0);
  1378. PlayerNoise(ent, ent->s.origin, PNOISE_SELF);
  1379. }
  1380. ent->viewheight = pm.viewheight;
  1381. ent->waterlevel = pm.waterlevel;
  1382. ent->watertype = pm.watertype;
  1383. ent->groundentity = pm.groundentity;
  1384. if (pm.groundentity)
  1385. ent->groundentity_linkcount = pm.groundentity->linkcount;
  1386. if (ent->deadflag)
  1387. {
  1388. client->ps.viewangles[ROLL] = 40;
  1389. client->ps.viewangles[PITCH] = -15;
  1390. client->ps.viewangles[YAW] = client->killer_yaw;
  1391. }
  1392. else
  1393. {
  1394. VectorCopy (pm.viewangles, client->v_angle);
  1395. VectorCopy (pm.viewangles, client->ps.viewangles);
  1396. }
  1397. gi.linkentity (ent);
  1398. if (ent->movetype != MOVETYPE_NOCLIP)
  1399. G_TouchTriggers (ent);
  1400. // touch other objects
  1401. for (i=0 ; i<pm.numtouch ; i++)
  1402. {
  1403. other = pm.touchents[i];
  1404. for (j=0 ; j<i ; j++)
  1405. if (pm.touchents[j] == other)
  1406. break;
  1407. if (j != i)
  1408. continue; // duplicated
  1409. if (!other->touch)
  1410. continue;
  1411. other->touch (other, ent, NULL, NULL);
  1412. }
  1413. }
  1414. client->oldbuttons = client->buttons;
  1415. client->buttons = ucmd->buttons;
  1416. client->latched_buttons |= client->buttons & ~client->oldbuttons;
  1417. // save light level the player is standing on for
  1418. // monster sighting AI
  1419. ent->light_level = ucmd->lightlevel;
  1420. // fire weapon from final position if needed
  1421. if (client->latched_buttons & BUTTON_ATTACK)
  1422. {
  1423. if (client->resp.spectator) {
  1424. client->latched_buttons = 0;
  1425. if (client->chase_target) {
  1426. client->chase_target = NULL;
  1427. client->ps.pmove.pm_flags &= ~PMF_NO_PREDICTION;
  1428. } else
  1429. GetChaseTarget(ent);
  1430. } else if (!client->weapon_thunk) {
  1431. client->weapon_thunk = true;
  1432. Think_Weapon (ent);
  1433. }
  1434. }
  1435. if (client->resp.spectator) {
  1436. if (ucmd->upmove >= 10) {
  1437. if (!(client->ps.pmove.pm_flags & PMF_JUMP_HELD)) {
  1438. client->ps.pmove.pm_flags |= PMF_JUMP_HELD;
  1439. if (client->chase_target)
  1440. ChaseNext(ent);
  1441. else
  1442. GetChaseTarget(ent);
  1443. }
  1444. } else
  1445. client->ps.pmove.pm_flags &= ~PMF_JUMP_HELD;
  1446. }
  1447. // update chase cam if being followed
  1448. for (i = 1; i <= maxclients->value; i++) {
  1449. other = g_edicts + i;
  1450. if (other->inuse && other->client->chase_target == ent)
  1451. UpdateChaseCam(other);
  1452. }
  1453. }
  1454. /*
  1455. ==============
  1456. ClientBeginServerFrame
  1457. This will be called once for each server frame, before running
  1458. any other entities in the world.
  1459. ==============
  1460. */
  1461. void ClientBeginServerFrame (edict_t *ent)
  1462. {
  1463. gclient_t *client;
  1464. int buttonMask;
  1465. if (level.intermissiontime)
  1466. return;
  1467. client = ent->client;
  1468. if (deathmatch->value &&
  1469. client->pers.spectator != client->resp.spectator &&
  1470. (level.time - client->respawn_time) >= 5) {
  1471. spectator_respawn(ent);
  1472. return;
  1473. }
  1474. // run weapon animations if it hasn't been done by a ucmd_t
  1475. if (!client->weapon_thunk && !client->resp.spectator)
  1476. Think_Weapon (ent);
  1477. else
  1478. client->weapon_thunk = false;
  1479. if (ent->deadflag)
  1480. {
  1481. // wait for any button just going down
  1482. if ( level.time > client->respawn_time)
  1483. {
  1484. // in deathmatch, only wait for attack button
  1485. if (deathmatch->value)
  1486. buttonMask = BUTTON_ATTACK;
  1487. else
  1488. buttonMask = -1;
  1489. if ( ( client->latched_buttons & buttonMask ) ||
  1490. (deathmatch->value && ((int)dmflags->value & DF_FORCE_RESPAWN) ) )
  1491. {
  1492. respawn(ent);
  1493. client->latched_buttons = 0;
  1494. }
  1495. }
  1496. return;
  1497. }
  1498. // add player trail so monsters can follow
  1499. if (!deathmatch->value)
  1500. if (!visible (ent, PlayerTrail_LastSpot() ) )
  1501. PlayerTrail_Add (ent->s.old_origin);
  1502. client->latched_buttons = 0;
  1503. }