sv_user.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703
  1. /*
  2. Copyright (C) 1996-1997 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. // sv_user.c -- server code for moving users
  16. #include "qwsvdef.h"
  17. edict_t *sv_player;
  18. usercmd_t cmd;
  19. cvar_t cl_rollspeed = {"cl_rollspeed", "200"};
  20. cvar_t cl_rollangle = {"cl_rollangle", "2.0"};
  21. cvar_t sv_spectalk = {"sv_spectalk", "1"};
  22. cvar_t sv_mapcheck = {"sv_mapcheck", "1"};
  23. extern vec3_t player_mins;
  24. extern int fp_messages, fp_persecond, fp_secondsdead;
  25. extern char fp_msg[];
  26. extern cvar_t pausable;
  27. /*
  28. ============================================================
  29. USER STRINGCMD EXECUTION
  30. host_client and sv_player will be valid.
  31. ============================================================
  32. */
  33. /*
  34. ================
  35. SV_New_f
  36. Sends the first message from the server to a connected client.
  37. This will be sent on the initial connection and upon each server load.
  38. ================
  39. */
  40. void SV_New_f (void)
  41. {
  42. char *gamedir;
  43. int playernum;
  44. if (host_client->state == cs_spawned)
  45. return;
  46. host_client->state = cs_connected;
  47. host_client->connection_started = realtime;
  48. // send the info about the new client to all connected clients
  49. // SV_FullClientUpdate (host_client, &sv.reliable_datagram);
  50. // host_client->sendinfo = true;
  51. gamedir = Info_ValueForKey (svs.info, "*gamedir");
  52. if (!gamedir[0])
  53. gamedir = "qw";
  54. //NOTE: This doesn't go through ClientReliableWrite since it's before the user
  55. //spawns. These functions are written to not overflow
  56. if (host_client->num_backbuf) {
  57. Con_Printf("WARNING %s: [SV_New] Back buffered (%d0, clearing", host_client->name, host_client->netchan.message.cursize);
  58. host_client->num_backbuf = 0;
  59. SZ_Clear(&host_client->netchan.message);
  60. }
  61. // send the serverdata
  62. MSG_WriteByte (&host_client->netchan.message, svc_serverdata);
  63. MSG_WriteLong (&host_client->netchan.message, PROTOCOL_VERSION);
  64. MSG_WriteLong (&host_client->netchan.message, svs.spawncount);
  65. MSG_WriteString (&host_client->netchan.message, gamedir);
  66. playernum = NUM_FOR_EDICT(host_client->edict)-1;
  67. if (host_client->spectator)
  68. playernum |= 128;
  69. MSG_WriteByte (&host_client->netchan.message, playernum);
  70. // send full levelname
  71. MSG_WriteString (&host_client->netchan.message, PR_GetString(sv.edicts->v.message));
  72. // send the movevars
  73. MSG_WriteFloat(&host_client->netchan.message, movevars.gravity);
  74. MSG_WriteFloat(&host_client->netchan.message, movevars.stopspeed);
  75. MSG_WriteFloat(&host_client->netchan.message, movevars.maxspeed);
  76. MSG_WriteFloat(&host_client->netchan.message, movevars.spectatormaxspeed);
  77. MSG_WriteFloat(&host_client->netchan.message, movevars.accelerate);
  78. MSG_WriteFloat(&host_client->netchan.message, movevars.airaccelerate);
  79. MSG_WriteFloat(&host_client->netchan.message, movevars.wateraccelerate);
  80. MSG_WriteFloat(&host_client->netchan.message, movevars.friction);
  81. MSG_WriteFloat(&host_client->netchan.message, movevars.waterfriction);
  82. MSG_WriteFloat(&host_client->netchan.message, movevars.entgravity);
  83. // send music
  84. MSG_WriteByte (&host_client->netchan.message, svc_cdtrack);
  85. MSG_WriteByte (&host_client->netchan.message, sv.edicts->v.sounds);
  86. // send server info string
  87. MSG_WriteByte (&host_client->netchan.message, svc_stufftext);
  88. MSG_WriteString (&host_client->netchan.message, va("fullserverinfo \"%s\"\n", svs.info) );
  89. }
  90. /*
  91. ==================
  92. SV_Soundlist_f
  93. ==================
  94. */
  95. void SV_Soundlist_f (void)
  96. {
  97. char **s;
  98. int n;
  99. if (host_client->state != cs_connected)
  100. {
  101. Con_Printf ("soundlist not valid -- allready spawned\n");
  102. return;
  103. }
  104. // handle the case of a level changing while a client was connecting
  105. if ( atoi(Cmd_Argv(1)) != svs.spawncount )
  106. {
  107. Con_Printf ("SV_Soundlist_f from different level\n");
  108. SV_New_f ();
  109. return;
  110. }
  111. n = atoi(Cmd_Argv(2));
  112. //NOTE: This doesn't go through ClientReliableWrite since it's before the user
  113. //spawns. These functions are written to not overflow
  114. if (host_client->num_backbuf) {
  115. Con_Printf("WARNING %s: [SV_Soundlist] Back buffered (%d0, clearing", host_client->name, host_client->netchan.message.cursize);
  116. host_client->num_backbuf = 0;
  117. SZ_Clear(&host_client->netchan.message);
  118. }
  119. MSG_WriteByte (&host_client->netchan.message, svc_soundlist);
  120. MSG_WriteByte (&host_client->netchan.message, n);
  121. for (s = sv.sound_precache+1 + n ;
  122. *s && host_client->netchan.message.cursize < (MAX_MSGLEN/2);
  123. s++, n++)
  124. MSG_WriteString (&host_client->netchan.message, *s);
  125. MSG_WriteByte (&host_client->netchan.message, 0);
  126. // next msg
  127. if (*s)
  128. MSG_WriteByte (&host_client->netchan.message, n);
  129. else
  130. MSG_WriteByte (&host_client->netchan.message, 0);
  131. }
  132. /*
  133. ==================
  134. SV_Modellist_f
  135. ==================
  136. */
  137. void SV_Modellist_f (void)
  138. {
  139. char **s;
  140. int n;
  141. if (host_client->state != cs_connected)
  142. {
  143. Con_Printf ("modellist not valid -- allready spawned\n");
  144. return;
  145. }
  146. // handle the case of a level changing while a client was connecting
  147. if ( atoi(Cmd_Argv(1)) != svs.spawncount )
  148. {
  149. Con_Printf ("SV_Modellist_f from different level\n");
  150. SV_New_f ();
  151. return;
  152. }
  153. n = atoi(Cmd_Argv(2));
  154. //NOTE: This doesn't go through ClientReliableWrite since it's before the user
  155. //spawns. These functions are written to not overflow
  156. if (host_client->num_backbuf) {
  157. Con_Printf("WARNING %s: [SV_Modellist] Back buffered (%d0, clearing", host_client->name, host_client->netchan.message.cursize);
  158. host_client->num_backbuf = 0;
  159. SZ_Clear(&host_client->netchan.message);
  160. }
  161. MSG_WriteByte (&host_client->netchan.message, svc_modellist);
  162. MSG_WriteByte (&host_client->netchan.message, n);
  163. for (s = sv.model_precache+1+n ;
  164. *s && host_client->netchan.message.cursize < (MAX_MSGLEN/2);
  165. s++, n++)
  166. MSG_WriteString (&host_client->netchan.message, *s);
  167. MSG_WriteByte (&host_client->netchan.message, 0);
  168. // next msg
  169. if (*s)
  170. MSG_WriteByte (&host_client->netchan.message, n);
  171. else
  172. MSG_WriteByte (&host_client->netchan.message, 0);
  173. }
  174. /*
  175. ==================
  176. SV_PreSpawn_f
  177. ==================
  178. */
  179. void SV_PreSpawn_f (void)
  180. {
  181. unsigned buf;
  182. unsigned check;
  183. if (host_client->state != cs_connected)
  184. {
  185. Con_Printf ("prespawn not valid -- allready spawned\n");
  186. return;
  187. }
  188. // handle the case of a level changing while a client was connecting
  189. if ( atoi(Cmd_Argv(1)) != svs.spawncount )
  190. {
  191. Con_Printf ("SV_PreSpawn_f from different level\n");
  192. SV_New_f ();
  193. return;
  194. }
  195. buf = atoi(Cmd_Argv(2));
  196. if (buf >= sv.num_signon_buffers)
  197. buf = 0;
  198. if (!buf) {
  199. // should be three numbers following containing checksums
  200. check = atoi(Cmd_Argv(3));
  201. // Con_DPrintf("Client check = %d\n", check);
  202. if (sv_mapcheck.value && check != sv.worldmodel->checksum &&
  203. check != sv.worldmodel->checksum2) {
  204. SV_ClientPrintf (host_client, PRINT_HIGH,
  205. "Map model file does not match (%s), %i != %i/%i.\n"
  206. "You may need a new version of the map, or the proper install files.\n",
  207. sv.modelname, check, sv.worldmodel->checksum, sv.worldmodel->checksum2);
  208. SV_DropClient (host_client);
  209. return;
  210. }
  211. host_client->checksum = check;
  212. }
  213. //NOTE: This doesn't go through ClientReliableWrite since it's before the user
  214. //spawns. These functions are written to not overflow
  215. if (host_client->num_backbuf) {
  216. Con_Printf("WARNING %s: [SV_PreSpawn] Back buffered (%d0, clearing", host_client->name, host_client->netchan.message.cursize);
  217. host_client->num_backbuf = 0;
  218. SZ_Clear(&host_client->netchan.message);
  219. }
  220. SZ_Write (&host_client->netchan.message,
  221. sv.signon_buffers[buf],
  222. sv.signon_buffer_size[buf]);
  223. buf++;
  224. if (buf == sv.num_signon_buffers)
  225. { // all done prespawning
  226. MSG_WriteByte (&host_client->netchan.message, svc_stufftext);
  227. MSG_WriteString (&host_client->netchan.message, va("cmd spawn %i 0\n",svs.spawncount) );
  228. }
  229. else
  230. { // need to prespawn more
  231. MSG_WriteByte (&host_client->netchan.message, svc_stufftext);
  232. MSG_WriteString (&host_client->netchan.message,
  233. va("cmd prespawn %i %i\n", svs.spawncount, buf) );
  234. }
  235. }
  236. /*
  237. ==================
  238. SV_Spawn_f
  239. ==================
  240. */
  241. void SV_Spawn_f (void)
  242. {
  243. int i;
  244. client_t *client;
  245. edict_t *ent;
  246. eval_t *val;
  247. int n;
  248. if (host_client->state != cs_connected)
  249. {
  250. Con_Printf ("Spawn not valid -- allready spawned\n");
  251. return;
  252. }
  253. // handle the case of a level changing while a client was connecting
  254. if ( atoi(Cmd_Argv(1)) != svs.spawncount )
  255. {
  256. Con_Printf ("SV_Spawn_f from different level\n");
  257. SV_New_f ();
  258. return;
  259. }
  260. n = atoi(Cmd_Argv(2));
  261. // make sure n is valid
  262. if ( n < 0 || n > MAX_CLIENTS )
  263. {
  264. Con_Printf ("SV_Spawn_f invalid client start\n");
  265. SV_New_f ();
  266. return;
  267. }
  268. // send all current names, colors, and frag counts
  269. // FIXME: is this a good thing?
  270. SZ_Clear (&host_client->netchan.message);
  271. // send current status of all other players
  272. // normally this could overflow, but no need to check due to backbuf
  273. for (i=n, client = svs.clients + n ; i<MAX_CLIENTS ; i++, client++)
  274. SV_FullClientUpdateToClient (client, host_client);
  275. // send all current light styles
  276. for (i=0 ; i<MAX_LIGHTSTYLES ; i++)
  277. {
  278. ClientReliableWrite_Begin (host_client, svc_lightstyle,
  279. 3 + (sv.lightstyles[i] ? strlen(sv.lightstyles[i]) : 1));
  280. ClientReliableWrite_Byte (host_client, (char)i);
  281. ClientReliableWrite_String (host_client, sv.lightstyles[i]);
  282. }
  283. // set up the edict
  284. ent = host_client->edict;
  285. memset (&ent->v, 0, progs->entityfields * 4);
  286. ent->v.colormap = NUM_FOR_EDICT(ent);
  287. ent->v.team = 0; // FIXME
  288. ent->v.netname = PR_SetString(host_client->name);
  289. host_client->entgravity = 1.0;
  290. val = GetEdictFieldValue(ent, "gravity");
  291. if (val)
  292. val->_float = 1.0;
  293. host_client->maxspeed = sv_maxspeed.value;
  294. val = GetEdictFieldValue(ent, "maxspeed");
  295. if (val)
  296. val->_float = sv_maxspeed.value;
  297. //
  298. // force stats to be updated
  299. //
  300. memset (host_client->stats, 0, sizeof(host_client->stats));
  301. ClientReliableWrite_Begin (host_client, svc_updatestatlong, 6);
  302. ClientReliableWrite_Byte (host_client, STAT_TOTALSECRETS);
  303. ClientReliableWrite_Long (host_client, pr_global_struct->total_secrets);
  304. ClientReliableWrite_Begin (host_client, svc_updatestatlong, 6);
  305. ClientReliableWrite_Byte (host_client, STAT_TOTALMONSTERS);
  306. ClientReliableWrite_Long (host_client, pr_global_struct->total_monsters);
  307. ClientReliableWrite_Begin (host_client, svc_updatestatlong, 6);
  308. ClientReliableWrite_Byte (host_client, STAT_SECRETS);
  309. ClientReliableWrite_Long (host_client, pr_global_struct->found_secrets);
  310. ClientReliableWrite_Begin (host_client, svc_updatestatlong, 6);
  311. ClientReliableWrite_Byte (host_client, STAT_MONSTERS);
  312. ClientReliableWrite_Long (host_client, pr_global_struct->killed_monsters);
  313. // get the client to check and download skins
  314. // when that is completed, a begin command will be issued
  315. ClientReliableWrite_Begin (host_client, svc_stufftext, 8);
  316. ClientReliableWrite_String (host_client, "skins\n" );
  317. }
  318. /*
  319. ==================
  320. SV_SpawnSpectator
  321. ==================
  322. */
  323. void SV_SpawnSpectator (void)
  324. {
  325. int i;
  326. edict_t *e;
  327. VectorCopy (vec3_origin, sv_player->v.origin);
  328. VectorCopy (vec3_origin, sv_player->v.view_ofs);
  329. sv_player->v.view_ofs[2] = 22;
  330. // search for an info_playerstart to spawn the spectator at
  331. for (i=MAX_CLIENTS-1 ; i<sv.num_edicts ; i++)
  332. {
  333. e = EDICT_NUM(i);
  334. if (!strcmp(PR_GetString(e->v.classname), "info_player_start"))
  335. {
  336. VectorCopy (e->v.origin, sv_player->v.origin);
  337. return;
  338. }
  339. }
  340. }
  341. /*
  342. ==================
  343. SV_Begin_f
  344. ==================
  345. */
  346. void SV_Begin_f (void)
  347. {
  348. unsigned pmodel = 0, emodel = 0;
  349. int i;
  350. if (host_client->state == cs_spawned)
  351. return; // don't begin again
  352. host_client->state = cs_spawned;
  353. // handle the case of a level changing while a client was connecting
  354. if ( atoi(Cmd_Argv(1)) != svs.spawncount )
  355. {
  356. Con_Printf ("SV_Begin_f from different level\n");
  357. SV_New_f ();
  358. return;
  359. }
  360. if (host_client->spectator)
  361. {
  362. SV_SpawnSpectator ();
  363. if (SpectatorConnect) {
  364. // copy spawn parms out of the client_t
  365. for (i=0 ; i< NUM_SPAWN_PARMS ; i++)
  366. (&pr_global_struct->parm1)[i] = host_client->spawn_parms[i];
  367. // call the spawn function
  368. pr_global_struct->time = sv.time;
  369. pr_global_struct->self = EDICT_TO_PROG(sv_player);
  370. PR_ExecuteProgram (SpectatorConnect);
  371. }
  372. }
  373. else
  374. {
  375. // copy spawn parms out of the client_t
  376. for (i=0 ; i< NUM_SPAWN_PARMS ; i++)
  377. (&pr_global_struct->parm1)[i] = host_client->spawn_parms[i];
  378. // call the spawn function
  379. pr_global_struct->time = sv.time;
  380. pr_global_struct->self = EDICT_TO_PROG(sv_player);
  381. PR_ExecuteProgram (pr_global_struct->ClientConnect);
  382. // actually spawn the player
  383. pr_global_struct->time = sv.time;
  384. pr_global_struct->self = EDICT_TO_PROG(sv_player);
  385. PR_ExecuteProgram (pr_global_struct->PutClientInServer);
  386. }
  387. // clear the net statistics, because connecting gives a bogus picture
  388. host_client->netchan.frame_latency = 0;
  389. host_client->netchan.frame_rate = 0;
  390. host_client->netchan.drop_count = 0;
  391. host_client->netchan.good_count = 0;
  392. //check he's not cheating
  393. pmodel = atoi(Info_ValueForKey (host_client->userinfo, "pmodel"));
  394. emodel = atoi(Info_ValueForKey (host_client->userinfo, "emodel"));
  395. if (pmodel != sv.model_player_checksum ||
  396. emodel != sv.eyes_player_checksum)
  397. SV_BroadcastPrintf (PRINT_HIGH, "%s WARNING: non standard player/eyes model detected\n", host_client->name);
  398. // if we are paused, tell the client
  399. if (sv.paused) {
  400. ClientReliableWrite_Begin (host_client, svc_setpause, 2);
  401. ClientReliableWrite_Byte (host_client, sv.paused);
  402. SV_ClientPrintf(host_client, PRINT_HIGH, "Server is paused.\n");
  403. }
  404. #if 0
  405. //
  406. // send a fixangle over the reliable channel to make sure it gets there
  407. // Never send a roll angle, because savegames can catch the server
  408. // in a state where it is expecting the client to correct the angle
  409. // and it won't happen if the game was just loaded, so you wind up
  410. // with a permanent head tilt
  411. ent = EDICT_NUM( 1 + (host_client - svs.clients) );
  412. MSG_WriteByte (&host_client->netchan.message, svc_setangle);
  413. for (i=0 ; i < 2 ; i++)
  414. MSG_WriteAngle (&host_client->netchan.message, ent->v.angles[i] );
  415. MSG_WriteAngle (&host_client->netchan.message, 0 );
  416. #endif
  417. }
  418. //=============================================================================
  419. /*
  420. ==================
  421. SV_NextDownload_f
  422. ==================
  423. */
  424. void SV_NextDownload_f (void)
  425. {
  426. byte buffer[1024];
  427. int r;
  428. int percent;
  429. int size;
  430. if (!host_client->download)
  431. return;
  432. r = host_client->downloadsize - host_client->downloadcount;
  433. if (r > 768)
  434. r = 768;
  435. r = fread (buffer, 1, r, host_client->download);
  436. ClientReliableWrite_Begin (host_client, svc_download, 6+r);
  437. ClientReliableWrite_Short (host_client, r);
  438. host_client->downloadcount += r;
  439. size = host_client->downloadsize;
  440. if (!size)
  441. size = 1;
  442. percent = host_client->downloadcount*100/size;
  443. ClientReliableWrite_Byte (host_client, percent);
  444. ClientReliableWrite_SZ (host_client, buffer, r);
  445. if (host_client->downloadcount != host_client->downloadsize)
  446. return;
  447. fclose (host_client->download);
  448. host_client->download = NULL;
  449. }
  450. void OutofBandPrintf(netadr_t where, char *fmt, ...)
  451. {
  452. va_list argptr;
  453. char send[1024];
  454. send[0] = 0xff;
  455. send[1] = 0xff;
  456. send[2] = 0xff;
  457. send[3] = 0xff;
  458. send[4] = A2C_PRINT;
  459. va_start (argptr, fmt);
  460. vsprintf (send+5, fmt, argptr);
  461. va_end (argptr);
  462. NET_SendPacket (strlen(send)+1, send, where);
  463. }
  464. /*
  465. ==================
  466. SV_NextUpload
  467. ==================
  468. */
  469. void SV_NextUpload (void)
  470. {
  471. byte buffer[1024];
  472. int r;
  473. int percent;
  474. int size;
  475. client_t *client;
  476. if (!*host_client->uploadfn) {
  477. SV_ClientPrintf(host_client, PRINT_HIGH, "Upload denied\n");
  478. ClientReliableWrite_Begin (host_client, svc_stufftext, 8);
  479. ClientReliableWrite_String (host_client, "stopul");
  480. // suck out rest of packet
  481. size = MSG_ReadShort (); MSG_ReadByte ();
  482. msg_readcount += size;
  483. return;
  484. }
  485. size = MSG_ReadShort ();
  486. percent = MSG_ReadByte ();
  487. if (!host_client->upload)
  488. {
  489. host_client->upload = fopen(host_client->uploadfn, "wb");
  490. if (!host_client->upload) {
  491. Sys_Printf("Can't create %s\n", host_client->uploadfn);
  492. ClientReliableWrite_Begin (host_client, svc_stufftext, 8);
  493. ClientReliableWrite_String (host_client, "stopul");
  494. *host_client->uploadfn = 0;
  495. return;
  496. }
  497. Sys_Printf("Receiving %s from %d...\n", host_client->uploadfn, host_client->userid);
  498. if (host_client->remote_snap)
  499. OutofBandPrintf(host_client->snap_from, "Server receiving %s from %d...\n", host_client->uploadfn, host_client->userid);
  500. }
  501. fwrite (net_message.data + msg_readcount, 1, size, host_client->upload);
  502. msg_readcount += size;
  503. Con_DPrintf ("UPLOAD: %d received\n", size);
  504. if (percent != 100) {
  505. ClientReliableWrite_Begin (host_client, svc_stufftext, 8);
  506. ClientReliableWrite_String (host_client, "nextul\n");
  507. } else {
  508. fclose (host_client->upload);
  509. host_client->upload = NULL;
  510. Sys_Printf("%s upload completed.\n", host_client->uploadfn);
  511. if (host_client->remote_snap) {
  512. char *p;
  513. if ((p = strchr(host_client->uploadfn, '/')) != NULL)
  514. p++;
  515. else
  516. p = host_client->uploadfn;
  517. OutofBandPrintf(host_client->snap_from, "%s upload completed.\nTo download, enter:\ndownload %s\n",
  518. host_client->uploadfn, p);
  519. }
  520. }
  521. }
  522. /*
  523. ==================
  524. SV_BeginDownload_f
  525. ==================
  526. */
  527. void SV_BeginDownload_f(void)
  528. {
  529. char *name;
  530. extern cvar_t allow_download;
  531. extern cvar_t allow_download_skins;
  532. extern cvar_t allow_download_models;
  533. extern cvar_t allow_download_sounds;
  534. extern cvar_t allow_download_maps;
  535. extern int file_from_pak; // ZOID did file come from pak?
  536. name = Cmd_Argv(1);
  537. // hacked by zoid to allow more conrol over download
  538. // first off, no .. or global allow check
  539. if (strstr (name, "..") || !allow_download.value
  540. // leading dot is no good
  541. || *name == '.'
  542. // leading slash bad as well, must be in subdir
  543. || *name == '/'
  544. // next up, skin check
  545. || (strncmp(name, "skins/", 6) == 0 && !allow_download_skins.value)
  546. // now models
  547. || (strncmp(name, "progs/", 6) == 0 && !allow_download_models.value)
  548. // now sounds
  549. || (strncmp(name, "sound/", 6) == 0 && !allow_download_sounds.value)
  550. // now maps (note special case for maps, must not be in pak)
  551. || (strncmp(name, "maps/", 6) == 0 && !allow_download_maps.value)
  552. // MUST be in a subdirectory
  553. || !strstr (name, "/") )
  554. { // don't allow anything with .. path
  555. ClientReliableWrite_Begin (host_client, svc_download, 4);
  556. ClientReliableWrite_Short (host_client, -1);
  557. ClientReliableWrite_Byte (host_client, 0);
  558. return;
  559. }
  560. if (host_client->download) {
  561. fclose (host_client->download);
  562. host_client->download = NULL;
  563. }
  564. // lowercase name (needed for casesen file systems)
  565. {
  566. char *p;
  567. for (p = name; *p; p++)
  568. *p = (char)tolower(*p);
  569. }
  570. host_client->downloadsize = COM_FOpenFile (name, &host_client->download);
  571. host_client->downloadcount = 0;
  572. if (!host_client->download
  573. // special check for maps, if it came from a pak file, don't allow
  574. // download ZOID
  575. || (strncmp(name, "maps/", 5) == 0 && file_from_pak))
  576. {
  577. if (host_client->download) {
  578. fclose(host_client->download);
  579. host_client->download = NULL;
  580. }
  581. Sys_Printf ("Couldn't download %s to %s\n", name, host_client->name);
  582. ClientReliableWrite_Begin (host_client, svc_download, 4);
  583. ClientReliableWrite_Short (host_client, -1);
  584. ClientReliableWrite_Byte (host_client, 0);
  585. return;
  586. }
  587. SV_NextDownload_f ();
  588. Sys_Printf ("Downloading %s to %s\n", name, host_client->name);
  589. }
  590. //=============================================================================
  591. /*
  592. ==================
  593. SV_Say
  594. ==================
  595. */
  596. void SV_Say (qboolean team)
  597. {
  598. client_t *client;
  599. int j, tmp;
  600. char *p;
  601. char text[2048];
  602. char t1[32], *t2;
  603. if (Cmd_Argc () < 2)
  604. return;
  605. if (team)
  606. {
  607. strncpy (t1, Info_ValueForKey (host_client->userinfo, "team"), 31);
  608. t1[31] = 0;
  609. }
  610. if (host_client->spectator && (!sv_spectalk.value || team))
  611. sprintf (text, "[SPEC] %s: ", host_client->name);
  612. else if (team)
  613. sprintf (text, "(%s): ", host_client->name);
  614. else {
  615. sprintf (text, "%s: ", host_client->name);
  616. }
  617. if (fp_messages) {
  618. if (!sv.paused && realtime<host_client->lockedtill) {
  619. SV_ClientPrintf(host_client, PRINT_CHAT,
  620. "You can't talk for %d more seconds\n",
  621. (int) (host_client->lockedtill - realtime));
  622. return;
  623. }
  624. tmp = host_client->whensaidhead - fp_messages + 1;
  625. if (tmp < 0)
  626. tmp = 10+tmp;
  627. if (!sv.paused &&
  628. host_client->whensaid[tmp] && (realtime-host_client->whensaid[tmp] < fp_persecond)) {
  629. host_client->lockedtill = realtime + fp_secondsdead;
  630. if (fp_msg[0])
  631. SV_ClientPrintf(host_client, PRINT_CHAT,
  632. "FloodProt: %s\n", fp_msg);
  633. else
  634. SV_ClientPrintf(host_client, PRINT_CHAT,
  635. "FloodProt: You can't talk for %d seconds.\n", fp_secondsdead);
  636. return;
  637. }
  638. host_client->whensaidhead++;
  639. if (host_client->whensaidhead > 9)
  640. host_client->whensaidhead = 0;
  641. host_client->whensaid[host_client->whensaidhead] = realtime;
  642. }
  643. p = Cmd_Args();
  644. if (*p == '"')
  645. {
  646. p++;
  647. p[Q_strlen(p)-1] = 0;
  648. }
  649. Q_strcat(text, p);
  650. Q_strcat(text, "\n");
  651. Sys_Printf ("%s", text);
  652. for (j = 0, client = svs.clients; j < MAX_CLIENTS; j++, client++)
  653. {
  654. if (client->state != cs_spawned)
  655. continue;
  656. if (host_client->spectator && !sv_spectalk.value)
  657. if (!client->spectator)
  658. continue;
  659. if (team)
  660. {
  661. // the spectator team
  662. if (host_client->spectator) {
  663. if (!client->spectator)
  664. continue;
  665. } else {
  666. t2 = Info_ValueForKey (client->userinfo, "team");
  667. if (strcmp(t1, t2) || client->spectator)
  668. continue; // on different teams
  669. }
  670. }
  671. SV_ClientPrintf(client, PRINT_CHAT, "%s", text);
  672. }
  673. }
  674. /*
  675. ==================
  676. SV_Say_f
  677. ==================
  678. */
  679. void SV_Say_f(void)
  680. {
  681. SV_Say (false);
  682. }
  683. /*
  684. ==================
  685. SV_Say_Team_f
  686. ==================
  687. */
  688. void SV_Say_Team_f(void)
  689. {
  690. SV_Say (true);
  691. }
  692. //============================================================================
  693. /*
  694. =================
  695. SV_Pings_f
  696. The client is showing the scoreboard, so send new ping times for all
  697. clients
  698. =================
  699. */
  700. void SV_Pings_f (void)
  701. {
  702. client_t *client;
  703. int j;
  704. for (j = 0, client = svs.clients; j < MAX_CLIENTS; j++, client++)
  705. {
  706. if (client->state != cs_spawned)
  707. continue;
  708. ClientReliableWrite_Begin (host_client, svc_updateping, 4);
  709. ClientReliableWrite_Byte (host_client, j);
  710. ClientReliableWrite_Short (host_client, SV_CalcPing(client));
  711. ClientReliableWrite_Begin (host_client, svc_updatepl, 4);
  712. ClientReliableWrite_Byte (host_client, j);
  713. ClientReliableWrite_Byte (host_client, client->lossage);
  714. }
  715. }
  716. /*
  717. ==================
  718. SV_Kill_f
  719. ==================
  720. */
  721. void SV_Kill_f (void)
  722. {
  723. if (sv_player->v.health <= 0)
  724. {
  725. SV_ClientPrintf (host_client, PRINT_HIGH, "Can't suicide -- allready dead!\n");
  726. return;
  727. }
  728. pr_global_struct->time = sv.time;
  729. pr_global_struct->self = EDICT_TO_PROG(sv_player);
  730. PR_ExecuteProgram (pr_global_struct->ClientKill);
  731. }
  732. /*
  733. ==================
  734. SV_TogglePause
  735. ==================
  736. */
  737. void SV_TogglePause (const char *msg)
  738. {
  739. int i;
  740. client_t *cl;
  741. sv.paused ^= 1;
  742. if (msg)
  743. SV_BroadcastPrintf (PRINT_HIGH, "%s", msg);
  744. // send notification to all clients
  745. for (i=0, cl = svs.clients ; i<MAX_CLIENTS ; i++, cl++)
  746. {
  747. if (!cl->state)
  748. continue;
  749. ClientReliableWrite_Begin (cl, svc_setpause, 2);
  750. ClientReliableWrite_Byte (cl, sv.paused);
  751. }
  752. }
  753. /*
  754. ==================
  755. SV_Pause_f
  756. ==================
  757. */
  758. void SV_Pause_f (void)
  759. {
  760. int i;
  761. client_t *cl;
  762. char st[sizeof(host_client->name) + 32];
  763. if (!pausable.value) {
  764. SV_ClientPrintf (host_client, PRINT_HIGH, "Pause not allowed.\n");
  765. return;
  766. }
  767. if (host_client->spectator) {
  768. SV_ClientPrintf (host_client, PRINT_HIGH, "Spectators can not pause.\n");
  769. return;
  770. }
  771. if (sv.paused)
  772. sprintf (st, "%s paused the game\n", host_client->name);
  773. else
  774. sprintf (st, "%s unpaused the game\n", host_client->name);
  775. SV_TogglePause(st);
  776. }
  777. /*
  778. =================
  779. SV_Drop_f
  780. The client is going to disconnect, so remove the connection immediately
  781. =================
  782. */
  783. void SV_Drop_f (void)
  784. {
  785. SV_EndRedirect ();
  786. if (!host_client->spectator)
  787. SV_BroadcastPrintf (PRINT_HIGH, "%s dropped\n", host_client->name);
  788. SV_DropClient (host_client);
  789. }
  790. /*
  791. =================
  792. SV_PTrack_f
  793. Change the bandwidth estimate for a client
  794. =================
  795. */
  796. void SV_PTrack_f (void)
  797. {
  798. int i;
  799. edict_t *ent, *tent;
  800. if (!host_client->spectator)
  801. return;
  802. if (Cmd_Argc() != 2)
  803. {
  804. // turn off tracking
  805. host_client->spec_track = 0;
  806. ent = EDICT_NUM(host_client - svs.clients + 1);
  807. tent = EDICT_NUM(0);
  808. ent->v.goalentity = EDICT_TO_PROG(tent);
  809. return;
  810. }
  811. i = atoi(Cmd_Argv(1));
  812. if (i < 0 || i >= MAX_CLIENTS || svs.clients[i].state != cs_spawned ||
  813. svs.clients[i].spectator) {
  814. SV_ClientPrintf (host_client, PRINT_HIGH, "Invalid client to track\n");
  815. host_client->spec_track = 0;
  816. ent = EDICT_NUM(host_client - svs.clients + 1);
  817. tent = EDICT_NUM(0);
  818. ent->v.goalentity = EDICT_TO_PROG(tent);
  819. return;
  820. }
  821. host_client->spec_track = i + 1; // now tracking
  822. ent = EDICT_NUM(host_client - svs.clients + 1);
  823. tent = EDICT_NUM(i + 1);
  824. ent->v.goalentity = EDICT_TO_PROG(tent);
  825. }
  826. /*
  827. =================
  828. SV_Rate_f
  829. Change the bandwidth estimate for a client
  830. =================
  831. */
  832. void SV_Rate_f (void)
  833. {
  834. int rate;
  835. if (Cmd_Argc() != 2)
  836. {
  837. SV_ClientPrintf (host_client, PRINT_HIGH, "Current rate is %i\n",
  838. (int)(1.0/host_client->netchan.rate + 0.5));
  839. return;
  840. }
  841. rate = atoi(Cmd_Argv(1));
  842. if (rate < 500)
  843. rate = 500;
  844. if (rate > 10000)
  845. rate = 10000;
  846. SV_ClientPrintf (host_client, PRINT_HIGH, "Net rate set to %i\n", rate);
  847. host_client->netchan.rate = 1.0/rate;
  848. }
  849. /*
  850. =================
  851. SV_Msg_f
  852. Change the message level for a client
  853. =================
  854. */
  855. void SV_Msg_f (void)
  856. {
  857. if (Cmd_Argc() != 2)
  858. {
  859. SV_ClientPrintf (host_client, PRINT_HIGH, "Current msg level is %i\n",
  860. host_client->messagelevel);
  861. return;
  862. }
  863. host_client->messagelevel = atoi(Cmd_Argv(1));
  864. SV_ClientPrintf (host_client, PRINT_HIGH, "Msg level set to %i\n", host_client->messagelevel);
  865. }
  866. /*
  867. ==================
  868. SV_SetInfo_f
  869. Allow clients to change userinfo
  870. ==================
  871. */
  872. void SV_SetInfo_f (void)
  873. {
  874. int i;
  875. char oldval[MAX_INFO_STRING];
  876. if (Cmd_Argc() == 1)
  877. {
  878. Con_Printf ("User info settings:\n");
  879. Info_Print (host_client->userinfo);
  880. return;
  881. }
  882. if (Cmd_Argc() != 3)
  883. {
  884. Con_Printf ("usage: setinfo [ <key> <value> ]\n");
  885. return;
  886. }
  887. if (Cmd_Argv(1)[0] == '*')
  888. return; // don't set priveledged values
  889. strcpy(oldval, Info_ValueForKey(host_client->userinfo, Cmd_Argv(1)));
  890. Info_SetValueForKey (host_client->userinfo, Cmd_Argv(1), Cmd_Argv(2), MAX_INFO_STRING);
  891. // name is extracted below in ExtractFromUserInfo
  892. // strncpy (host_client->name, Info_ValueForKey (host_client->userinfo, "name")
  893. // , sizeof(host_client->name)-1);
  894. // SV_FullClientUpdate (host_client, &sv.reliable_datagram);
  895. // host_client->sendinfo = true;
  896. if (!strcmp(Info_ValueForKey(host_client->userinfo, Cmd_Argv(1)), oldval))
  897. return; // key hasn't changed
  898. // process any changed values
  899. SV_ExtractFromUserinfo (host_client);
  900. i = host_client - svs.clients;
  901. MSG_WriteByte (&sv.reliable_datagram, svc_setinfo);
  902. MSG_WriteByte (&sv.reliable_datagram, i);
  903. MSG_WriteString (&sv.reliable_datagram, Cmd_Argv(1));
  904. MSG_WriteString (&sv.reliable_datagram, Info_ValueForKey(host_client->userinfo, Cmd_Argv(1)));
  905. }
  906. /*
  907. ==================
  908. SV_ShowServerinfo_f
  909. Dumps the serverinfo info string
  910. ==================
  911. */
  912. void SV_ShowServerinfo_f (void)
  913. {
  914. Info_Print (svs.info);
  915. }
  916. void SV_NoSnap_f(void)
  917. {
  918. if (*host_client->uploadfn) {
  919. *host_client->uploadfn = 0;
  920. SV_BroadcastPrintf (PRINT_HIGH, "%s refused remote screenshot\n", host_client->name);
  921. }
  922. }
  923. typedef struct
  924. {
  925. char *name;
  926. void (*func) (void);
  927. } ucmd_t;
  928. ucmd_t ucmds[] =
  929. {
  930. {"new", SV_New_f},
  931. {"modellist", SV_Modellist_f},
  932. {"soundlist", SV_Soundlist_f},
  933. {"prespawn", SV_PreSpawn_f},
  934. {"spawn", SV_Spawn_f},
  935. {"begin", SV_Begin_f},
  936. {"drop", SV_Drop_f},
  937. {"pings", SV_Pings_f},
  938. // issued by hand at client consoles
  939. {"rate", SV_Rate_f},
  940. {"kill", SV_Kill_f},
  941. {"pause", SV_Pause_f},
  942. {"msg", SV_Msg_f},
  943. {"say", SV_Say_f},
  944. {"say_team", SV_Say_Team_f},
  945. {"setinfo", SV_SetInfo_f},
  946. {"serverinfo", SV_ShowServerinfo_f},
  947. {"download", SV_BeginDownload_f},
  948. {"nextdl", SV_NextDownload_f},
  949. {"ptrack", SV_PTrack_f}, //ZOID - used with autocam
  950. {"snap", SV_NoSnap_f},
  951. {NULL, NULL}
  952. };
  953. /*
  954. ==================
  955. SV_ExecuteUserCommand
  956. ==================
  957. */
  958. void SV_ExecuteUserCommand (char *s)
  959. {
  960. ucmd_t *u;
  961. Cmd_TokenizeString (s);
  962. sv_player = host_client->edict;
  963. SV_BeginRedirect (RD_CLIENT);
  964. for (u=ucmds ; u->name ; u++)
  965. if (!strcmp (Cmd_Argv(0), u->name) )
  966. {
  967. u->func ();
  968. break;
  969. }
  970. if (!u->name)
  971. Con_Printf ("Bad user command: %s\n", Cmd_Argv(0));
  972. SV_EndRedirect ();
  973. }
  974. /*
  975. ===========================================================================
  976. USER CMD EXECUTION
  977. ===========================================================================
  978. */
  979. /*
  980. ===============
  981. V_CalcRoll
  982. Used by view and sv_user
  983. ===============
  984. */
  985. float V_CalcRoll (vec3_t angles, vec3_t velocity)
  986. {
  987. vec3_t forward, right, up;
  988. float sign;
  989. float side;
  990. float value;
  991. AngleVectors (angles, forward, right, up);
  992. side = DotProduct (velocity, right);
  993. sign = side < 0 ? -1 : 1;
  994. side = fabs(side);
  995. value = cl_rollangle.value;
  996. if (side < cl_rollspeed.value)
  997. side = side * value / cl_rollspeed.value;
  998. else
  999. side = value;
  1000. return side*sign;
  1001. }
  1002. //============================================================================
  1003. vec3_t pmove_mins, pmove_maxs;
  1004. /*
  1005. ====================
  1006. AddLinksToPmove
  1007. ====================
  1008. */
  1009. void AddLinksToPmove ( areanode_t *node )
  1010. {
  1011. link_t *l, *next;
  1012. edict_t *check;
  1013. int pl;
  1014. int i;
  1015. physent_t *pe;
  1016. pl = EDICT_TO_PROG(sv_player);
  1017. // touch linked edicts
  1018. for (l = node->solid_edicts.next ; l != &node->solid_edicts ; l = next)
  1019. {
  1020. next = l->next;
  1021. check = EDICT_FROM_AREA(l);
  1022. if (check->v.owner == pl)
  1023. continue; // player's own missile
  1024. if (check->v.solid == SOLID_BSP
  1025. || check->v.solid == SOLID_BBOX
  1026. || check->v.solid == SOLID_SLIDEBOX)
  1027. {
  1028. if (check == sv_player)
  1029. continue;
  1030. for (i=0 ; i<3 ; i++)
  1031. if (check->v.absmin[i] > pmove_maxs[i]
  1032. || check->v.absmax[i] < pmove_mins[i])
  1033. break;
  1034. if (i != 3)
  1035. continue;
  1036. if (pmove.numphysent == MAX_PHYSENTS)
  1037. return;
  1038. pe = &pmove.physents[pmove.numphysent];
  1039. pmove.numphysent++;
  1040. VectorCopy (check->v.origin, pe->origin);
  1041. pe->info = NUM_FOR_EDICT(check);
  1042. if (check->v.solid == SOLID_BSP)
  1043. pe->model = sv.models[(int)(check->v.modelindex)];
  1044. else
  1045. {
  1046. pe->model = NULL;
  1047. VectorCopy (check->v.mins, pe->mins);
  1048. VectorCopy (check->v.maxs, pe->maxs);
  1049. }
  1050. }
  1051. }
  1052. // recurse down both sides
  1053. if (node->axis == -1)
  1054. return;
  1055. if ( pmove_maxs[node->axis] > node->dist )
  1056. AddLinksToPmove ( node->children[0] );
  1057. if ( pmove_mins[node->axis] < node->dist )
  1058. AddLinksToPmove ( node->children[1] );
  1059. }
  1060. /*
  1061. ================
  1062. AddAllEntsToPmove
  1063. For debugging
  1064. ================
  1065. */
  1066. void AddAllEntsToPmove (void)
  1067. {
  1068. int e;
  1069. edict_t *check;
  1070. int i;
  1071. physent_t *pe;
  1072. int pl;
  1073. pl = EDICT_TO_PROG(sv_player);
  1074. check = NEXT_EDICT(sv.edicts);
  1075. for (e=1 ; e<sv.num_edicts ; e++, check = NEXT_EDICT(check))
  1076. {
  1077. if (check->free)
  1078. continue;
  1079. if (check->v.owner == pl)
  1080. continue;
  1081. if (check->v.solid == SOLID_BSP
  1082. || check->v.solid == SOLID_BBOX
  1083. || check->v.solid == SOLID_SLIDEBOX)
  1084. {
  1085. if (check == sv_player)
  1086. continue;
  1087. for (i=0 ; i<3 ; i++)
  1088. if (check->v.absmin[i] > pmove_maxs[i]
  1089. || check->v.absmax[i] < pmove_mins[i])
  1090. break;
  1091. if (i != 3)
  1092. continue;
  1093. pe = &pmove.physents[pmove.numphysent];
  1094. VectorCopy (check->v.origin, pe->origin);
  1095. pmove.physents[pmove.numphysent].info = e;
  1096. if (check->v.solid == SOLID_BSP)
  1097. pe->model = sv.models[(int)(check->v.modelindex)];
  1098. else
  1099. {
  1100. pe->model = NULL;
  1101. VectorCopy (check->v.mins, pe->mins);
  1102. VectorCopy (check->v.maxs, pe->maxs);
  1103. }
  1104. if (++pmove.numphysent == MAX_PHYSENTS)
  1105. break;
  1106. }
  1107. }
  1108. }
  1109. /*
  1110. ===========
  1111. SV_PreRunCmd
  1112. ===========
  1113. Done before running a player command. Clears the touch array
  1114. */
  1115. byte playertouch[(MAX_EDICTS+7)/8];
  1116. void SV_PreRunCmd(void)
  1117. {
  1118. memset(playertouch, 0, sizeof(playertouch));
  1119. }
  1120. /*
  1121. ===========
  1122. SV_RunCmd
  1123. ===========
  1124. */
  1125. void SV_RunCmd (usercmd_t *ucmd)
  1126. {
  1127. edict_t *ent;
  1128. int i, n;
  1129. int oldmsec;
  1130. cmd = *ucmd;
  1131. // chop up very long commands
  1132. if (cmd.msec > 50)
  1133. {
  1134. oldmsec = ucmd->msec;
  1135. cmd.msec = oldmsec/2;
  1136. SV_RunCmd (&cmd);
  1137. cmd.msec = oldmsec/2;
  1138. cmd.impulse = 0;
  1139. SV_RunCmd (&cmd);
  1140. return;
  1141. }
  1142. if (!sv_player->v.fixangle)
  1143. VectorCopy (ucmd->angles, sv_player->v.v_angle);
  1144. sv_player->v.button0 = ucmd->buttons & 1;
  1145. sv_player->v.button2 = (ucmd->buttons & 2)>>1;
  1146. if (ucmd->impulse)
  1147. sv_player->v.impulse = ucmd->impulse;
  1148. //
  1149. // angles
  1150. // show 1/3 the pitch angle and all the roll angle
  1151. if (sv_player->v.health > 0)
  1152. {
  1153. if (!sv_player->v.fixangle)
  1154. {
  1155. sv_player->v.angles[PITCH] = -sv_player->v.v_angle[PITCH]/3;
  1156. sv_player->v.angles[YAW] = sv_player->v.v_angle[YAW];
  1157. }
  1158. sv_player->v.angles[ROLL] =
  1159. V_CalcRoll (sv_player->v.angles, sv_player->v.velocity)*4;
  1160. }
  1161. host_frametime = ucmd->msec * 0.001;
  1162. if (host_frametime > 0.1)
  1163. host_frametime = 0.1;
  1164. if (!host_client->spectator)
  1165. {
  1166. pr_global_struct->frametime = host_frametime;
  1167. pr_global_struct->time = sv.time;
  1168. pr_global_struct->self = EDICT_TO_PROG(sv_player);
  1169. PR_ExecuteProgram (pr_global_struct->PlayerPreThink);
  1170. SV_RunThink (sv_player);
  1171. }
  1172. for (i=0 ; i<3 ; i++)
  1173. pmove.origin[i] = sv_player->v.origin[i] + (sv_player->v.mins[i] - player_mins[i]);
  1174. VectorCopy (sv_player->v.velocity, pmove.velocity);
  1175. VectorCopy (sv_player->v.v_angle, pmove.angles);
  1176. pmove.spectator = host_client->spectator;
  1177. pmove.waterjumptime = sv_player->v.teleport_time;
  1178. pmove.numphysent = 1;
  1179. pmove.physents[0].model = sv.worldmodel;
  1180. pmove.cmd = *ucmd;
  1181. pmove.dead = sv_player->v.health <= 0;
  1182. pmove.oldbuttons = host_client->oldbuttons;
  1183. movevars.entgravity = host_client->entgravity;
  1184. movevars.maxspeed = host_client->maxspeed;
  1185. for (i=0 ; i<3 ; i++)
  1186. {
  1187. pmove_mins[i] = pmove.origin[i] - 256;
  1188. pmove_maxs[i] = pmove.origin[i] + 256;
  1189. }
  1190. #if 1
  1191. AddLinksToPmove ( sv_areanodes );
  1192. #else
  1193. AddAllEntsToPmove ();
  1194. #endif
  1195. #if 0
  1196. {
  1197. int before, after;
  1198. before = PM_TestPlayerPosition (pmove.origin);
  1199. PlayerMove ();
  1200. after = PM_TestPlayerPosition (pmove.origin);
  1201. if (sv_player->v.health > 0 && before && !after )
  1202. Con_Printf ("player %s got stuck in playermove!!!!\n", host_client->name);
  1203. }
  1204. #else
  1205. PlayerMove ();
  1206. #endif
  1207. host_client->oldbuttons = pmove.oldbuttons;
  1208. sv_player->v.teleport_time = pmove.waterjumptime;
  1209. sv_player->v.waterlevel = waterlevel;
  1210. sv_player->v.watertype = watertype;
  1211. if (onground != -1)
  1212. {
  1213. sv_player->v.flags = (int)sv_player->v.flags | FL_ONGROUND;
  1214. sv_player->v.groundentity = EDICT_TO_PROG(EDICT_NUM(pmove.physents[onground].info));
  1215. }
  1216. else
  1217. sv_player->v.flags = (int)sv_player->v.flags & ~FL_ONGROUND;
  1218. for (i=0 ; i<3 ; i++)
  1219. sv_player->v.origin[i] = pmove.origin[i] - (sv_player->v.mins[i] - player_mins[i]);
  1220. #if 0
  1221. // truncate velocity the same way the net protocol will
  1222. for (i=0 ; i<3 ; i++)
  1223. sv_player->v.velocity[i] = (int)pmove.velocity[i];
  1224. #else
  1225. VectorCopy (pmove.velocity, sv_player->v.velocity);
  1226. #endif
  1227. VectorCopy (pmove.angles, sv_player->v.v_angle);
  1228. if (!host_client->spectator)
  1229. {
  1230. // link into place and touch triggers
  1231. SV_LinkEdict (sv_player, true);
  1232. // touch other objects
  1233. for (i=0 ; i<pmove.numtouch ; i++)
  1234. {
  1235. n = pmove.physents[pmove.touchindex[i]].info;
  1236. ent = EDICT_NUM(n);
  1237. if (!ent->v.touch || (playertouch[n/8]&(1<<(n%8))))
  1238. continue;
  1239. pr_global_struct->self = EDICT_TO_PROG(ent);
  1240. pr_global_struct->other = EDICT_TO_PROG(sv_player);
  1241. PR_ExecuteProgram (ent->v.touch);
  1242. playertouch[n/8] |= 1 << (n%8);
  1243. }
  1244. }
  1245. }
  1246. /*
  1247. ===========
  1248. SV_PostRunCmd
  1249. ===========
  1250. Done after running a player command.
  1251. */
  1252. void SV_PostRunCmd(void)
  1253. {
  1254. // run post-think
  1255. if (!host_client->spectator) {
  1256. pr_global_struct->time = sv.time;
  1257. pr_global_struct->self = EDICT_TO_PROG(sv_player);
  1258. PR_ExecuteProgram (pr_global_struct->PlayerPostThink);
  1259. SV_RunNewmis ();
  1260. } else if (SpectatorThink) {
  1261. pr_global_struct->time = sv.time;
  1262. pr_global_struct->self = EDICT_TO_PROG(sv_player);
  1263. PR_ExecuteProgram (SpectatorThink);
  1264. }
  1265. }
  1266. /*
  1267. ===================
  1268. SV_ExecuteClientMessage
  1269. The current net_message is parsed for the given client
  1270. ===================
  1271. */
  1272. void SV_ExecuteClientMessage (client_t *cl)
  1273. {
  1274. int c;
  1275. char *s;
  1276. usercmd_t oldest, oldcmd, newcmd;
  1277. client_frame_t *frame;
  1278. vec3_t o;
  1279. qboolean move_issued = false; //only allow one move command
  1280. int checksumIndex;
  1281. byte checksum, calculatedChecksum;
  1282. int seq_hash;
  1283. // calc ping time
  1284. frame = &cl->frames[cl->netchan.incoming_acknowledged & UPDATE_MASK];
  1285. frame->ping_time = realtime - frame->senttime;
  1286. // make sure the reply sequence number matches the incoming
  1287. // sequence number
  1288. if (cl->netchan.incoming_sequence >= cl->netchan.outgoing_sequence)
  1289. cl->netchan.outgoing_sequence = cl->netchan.incoming_sequence;
  1290. else
  1291. cl->send_message = false; // don't reply, sequences have slipped
  1292. // save time for ping calculations
  1293. cl->frames[cl->netchan.outgoing_sequence & UPDATE_MASK].senttime = realtime;
  1294. cl->frames[cl->netchan.outgoing_sequence & UPDATE_MASK].ping_time = -1;
  1295. host_client = cl;
  1296. sv_player = host_client->edict;
  1297. // seq_hash = (cl->netchan.incoming_sequence & 0xffff) ; // ^ QW_CHECK_HASH;
  1298. seq_hash = cl->netchan.incoming_sequence;
  1299. // mark time so clients will know how much to predict
  1300. // other players
  1301. cl->localtime = sv.time;
  1302. cl->delta_sequence = -1; // no delta unless requested
  1303. while (1)
  1304. {
  1305. if (msg_badread)
  1306. {
  1307. Con_Printf ("SV_ReadClientMessage: badread\n");
  1308. SV_DropClient (cl);
  1309. return;
  1310. }
  1311. c = MSG_ReadByte ();
  1312. if (c == -1)
  1313. break;
  1314. switch (c)
  1315. {
  1316. default:
  1317. Con_Printf ("SV_ReadClientMessage: unknown command char\n");
  1318. SV_DropClient (cl);
  1319. return;
  1320. case clc_nop:
  1321. break;
  1322. case clc_delta:
  1323. cl->delta_sequence = MSG_ReadByte ();
  1324. break;
  1325. case clc_move:
  1326. if (move_issued)
  1327. return; // someone is trying to cheat...
  1328. move_issued = true;
  1329. checksumIndex = MSG_GetReadCount();
  1330. checksum = (byte)MSG_ReadByte ();
  1331. // read loss percentage
  1332. cl->lossage = MSG_ReadByte();
  1333. MSG_ReadDeltaUsercmd (&nullcmd, &oldest);
  1334. MSG_ReadDeltaUsercmd (&oldest, &oldcmd);
  1335. MSG_ReadDeltaUsercmd (&oldcmd, &newcmd);
  1336. if ( cl->state != cs_spawned )
  1337. break;
  1338. // if the checksum fails, ignore the rest of the packet
  1339. calculatedChecksum = COM_BlockSequenceCRCByte(
  1340. net_message.data + checksumIndex + 1,
  1341. MSG_GetReadCount() - checksumIndex - 1,
  1342. seq_hash);
  1343. if (calculatedChecksum != checksum)
  1344. {
  1345. Con_DPrintf ("Failed command checksum for %s(%d) (%d != %d)\n",
  1346. cl->name, cl->netchan.incoming_sequence, checksum, calculatedChecksum);
  1347. return;
  1348. }
  1349. if (!sv.paused) {
  1350. SV_PreRunCmd();
  1351. if (net_drop < 20)
  1352. {
  1353. while (net_drop > 2)
  1354. {
  1355. SV_RunCmd (&cl->lastcmd);
  1356. net_drop--;
  1357. }
  1358. if (net_drop > 1)
  1359. SV_RunCmd (&oldest);
  1360. if (net_drop > 0)
  1361. SV_RunCmd (&oldcmd);
  1362. }
  1363. SV_RunCmd (&newcmd);
  1364. SV_PostRunCmd();
  1365. }
  1366. cl->lastcmd = newcmd;
  1367. cl->lastcmd.buttons = 0; // avoid multiple fires on lag
  1368. break;
  1369. case clc_stringcmd:
  1370. s = MSG_ReadString ();
  1371. SV_ExecuteUserCommand (s);
  1372. break;
  1373. case clc_tmove:
  1374. o[0] = MSG_ReadCoord();
  1375. o[1] = MSG_ReadCoord();
  1376. o[2] = MSG_ReadCoord();
  1377. // only allowed by spectators
  1378. if (host_client->spectator) {
  1379. VectorCopy(o, sv_player->v.origin);
  1380. SV_LinkEdict(sv_player, false);
  1381. }
  1382. break;
  1383. case clc_upload:
  1384. SV_NextUpload();
  1385. break;
  1386. }
  1387. }
  1388. }
  1389. /*
  1390. ==============
  1391. SV_UserInit
  1392. ==============
  1393. */
  1394. void SV_UserInit (void)
  1395. {
  1396. Cvar_RegisterVariable (&cl_rollspeed);
  1397. Cvar_RegisterVariable (&cl_rollangle);
  1398. Cvar_RegisterVariable (&sv_spectalk);
  1399. Cvar_RegisterVariable (&sv_mapcheck);
  1400. }