sv_main.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  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_main.c -- server main program
  16. #include "quakedef.h"
  17. server_t sv;
  18. server_static_t svs;
  19. char localmodels[MAX_MODELS][5]; // inline model names for precache
  20. //============================================================================
  21. /*
  22. ===============
  23. SV_Init
  24. ===============
  25. */
  26. void SV_Init (void)
  27. {
  28. int i;
  29. extern cvar_t sv_maxvelocity;
  30. extern cvar_t sv_gravity;
  31. extern cvar_t sv_nostep;
  32. extern cvar_t sv_friction;
  33. extern cvar_t sv_edgefriction;
  34. extern cvar_t sv_stopspeed;
  35. extern cvar_t sv_maxspeed;
  36. extern cvar_t sv_accelerate;
  37. extern cvar_t sv_idealpitchscale;
  38. extern cvar_t sv_aim;
  39. Cvar_RegisterVariable (&sv_maxvelocity);
  40. Cvar_RegisterVariable (&sv_gravity);
  41. Cvar_RegisterVariable (&sv_friction);
  42. Cvar_RegisterVariable (&sv_edgefriction);
  43. Cvar_RegisterVariable (&sv_stopspeed);
  44. Cvar_RegisterVariable (&sv_maxspeed);
  45. Cvar_RegisterVariable (&sv_accelerate);
  46. Cvar_RegisterVariable (&sv_idealpitchscale);
  47. Cvar_RegisterVariable (&sv_aim);
  48. Cvar_RegisterVariable (&sv_nostep);
  49. for (i=0 ; i<MAX_MODELS ; i++)
  50. sprintf (localmodels[i], "*%i", i);
  51. }
  52. /*
  53. =============================================================================
  54. EVENT MESSAGES
  55. =============================================================================
  56. */
  57. /*
  58. ==================
  59. SV_StartParticle
  60. Make sure the event gets sent to all clients
  61. ==================
  62. */
  63. void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
  64. {
  65. int i, v;
  66. if (sv.datagram.cursize > MAX_DATAGRAM-16)
  67. return;
  68. MSG_WriteByte (&sv.datagram, svc_particle);
  69. MSG_WriteCoord (&sv.datagram, org[0]);
  70. MSG_WriteCoord (&sv.datagram, org[1]);
  71. MSG_WriteCoord (&sv.datagram, org[2]);
  72. for (i=0 ; i<3 ; i++)
  73. {
  74. v = dir[i]*16;
  75. if (v > 127)
  76. v = 127;
  77. else if (v < -128)
  78. v = -128;
  79. MSG_WriteChar (&sv.datagram, v);
  80. }
  81. MSG_WriteByte (&sv.datagram, count);
  82. MSG_WriteByte (&sv.datagram, color);
  83. }
  84. /*
  85. ==================
  86. SV_StartSound
  87. Each entity can have eight independant sound sources, like voice,
  88. weapon, feet, etc.
  89. Channel 0 is an auto-allocate channel, the others override anything
  90. allready running on that entity/channel pair.
  91. An attenuation of 0 will play full volume everywhere in the level.
  92. Larger attenuations will drop off. (max 4 attenuation)
  93. ==================
  94. */
  95. void SV_StartSound (edict_t *entity, int channel, char *sample, int volume,
  96. float attenuation)
  97. {
  98. int sound_num;
  99. int field_mask;
  100. int i;
  101. int ent;
  102. if (volume < 0 || volume > 255)
  103. Sys_Error ("SV_StartSound: volume = %i", volume);
  104. if (attenuation < 0 || attenuation > 4)
  105. Sys_Error ("SV_StartSound: attenuation = %f", attenuation);
  106. if (channel < 0 || channel > 7)
  107. Sys_Error ("SV_StartSound: channel = %i", channel);
  108. if (sv.datagram.cursize > MAX_DATAGRAM-16)
  109. return;
  110. // find precache number for sound
  111. for (sound_num=1 ; sound_num<MAX_SOUNDS
  112. && sv.sound_precache[sound_num] ; sound_num++)
  113. if (!strcmp(sample, sv.sound_precache[sound_num]))
  114. break;
  115. if ( sound_num == MAX_SOUNDS || !sv.sound_precache[sound_num] )
  116. {
  117. Con_Printf ("SV_StartSound: %s not precacheed\n", sample);
  118. return;
  119. }
  120. ent = NUM_FOR_EDICT(entity);
  121. channel = (ent<<3) | channel;
  122. field_mask = 0;
  123. if (volume != DEFAULT_SOUND_PACKET_VOLUME)
  124. field_mask |= SND_VOLUME;
  125. if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
  126. field_mask |= SND_ATTENUATION;
  127. // directed messages go only to the entity the are targeted on
  128. MSG_WriteByte (&sv.datagram, svc_sound);
  129. MSG_WriteByte (&sv.datagram, field_mask);
  130. if (field_mask & SND_VOLUME)
  131. MSG_WriteByte (&sv.datagram, volume);
  132. if (field_mask & SND_ATTENUATION)
  133. MSG_WriteByte (&sv.datagram, attenuation*64);
  134. MSG_WriteShort (&sv.datagram, channel);
  135. MSG_WriteByte (&sv.datagram, sound_num);
  136. for (i=0 ; i<3 ; i++)
  137. MSG_WriteCoord (&sv.datagram, entity->v.origin[i]+0.5*(entity->v.mins[i]+entity->v.maxs[i]));
  138. }
  139. /*
  140. ==============================================================================
  141. CLIENT SPAWNING
  142. ==============================================================================
  143. */
  144. /*
  145. ================
  146. SV_SendServerinfo
  147. Sends the first message from the server to a connected client.
  148. This will be sent on the initial connection and upon each server load.
  149. ================
  150. */
  151. void SV_SendServerinfo (client_t *client)
  152. {
  153. char **s;
  154. char message[2048];
  155. MSG_WriteByte (&client->message, svc_print);
  156. sprintf (message, "%c\nVERSION %4.2f SERVER (%i CRC)", 2, VERSION, pr_crc);
  157. MSG_WriteString (&client->message,message);
  158. MSG_WriteByte (&client->message, svc_serverinfo);
  159. MSG_WriteLong (&client->message, PROTOCOL_VERSION);
  160. MSG_WriteByte (&client->message, svs.maxclients);
  161. if (!coop.value && deathmatch.value)
  162. MSG_WriteByte (&client->message, GAME_DEATHMATCH);
  163. else
  164. MSG_WriteByte (&client->message, GAME_COOP);
  165. sprintf (message, pr_strings+sv.edicts->v.message);
  166. MSG_WriteString (&client->message,message);
  167. for (s = sv.model_precache+1 ; *s ; s++)
  168. MSG_WriteString (&client->message, *s);
  169. MSG_WriteByte (&client->message, 0);
  170. for (s = sv.sound_precache+1 ; *s ; s++)
  171. MSG_WriteString (&client->message, *s);
  172. MSG_WriteByte (&client->message, 0);
  173. // send music
  174. MSG_WriteByte (&client->message, svc_cdtrack);
  175. MSG_WriteByte (&client->message, sv.edicts->v.sounds);
  176. MSG_WriteByte (&client->message, sv.edicts->v.sounds);
  177. // set view
  178. MSG_WriteByte (&client->message, svc_setview);
  179. MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
  180. MSG_WriteByte (&client->message, svc_signonnum);
  181. MSG_WriteByte (&client->message, 1);
  182. client->sendsignon = true;
  183. client->spawned = false; // need prespawn, spawn, etc
  184. }
  185. /*
  186. ================
  187. SV_ConnectClient
  188. Initializes a client_t for a new net connection. This will only be called
  189. once for a player each game, not once for each level change.
  190. ================
  191. */
  192. void SV_ConnectClient (int clientnum)
  193. {
  194. edict_t *ent;
  195. client_t *client;
  196. int edictnum;
  197. struct qsocket_s *netconnection;
  198. int i;
  199. float spawn_parms[NUM_SPAWN_PARMS];
  200. client = svs.clients + clientnum;
  201. Con_DPrintf ("Client %s connected\n", client->netconnection->address);
  202. edictnum = clientnum+1;
  203. ent = EDICT_NUM(edictnum);
  204. // set up the client_t
  205. netconnection = client->netconnection;
  206. if (sv.loadgame)
  207. memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
  208. memset (client, 0, sizeof(*client));
  209. client->netconnection = netconnection;
  210. strcpy (client->name, "unconnected");
  211. client->active = true;
  212. client->spawned = false;
  213. client->edict = ent;
  214. client->message.data = client->msgbuf;
  215. client->message.maxsize = sizeof(client->msgbuf);
  216. client->message.allowoverflow = true; // we can catch it
  217. #ifdef IDGODS
  218. client->privileged = IsID(&client->netconnection->addr);
  219. #else
  220. client->privileged = false;
  221. #endif
  222. if (sv.loadgame)
  223. memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
  224. else
  225. {
  226. // call the progs to get default spawn parms for the new client
  227. PR_ExecuteProgram (pr_global_struct->SetNewParms);
  228. for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
  229. client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
  230. }
  231. SV_SendServerinfo (client);
  232. }
  233. /*
  234. ===================
  235. SV_CheckForNewClients
  236. ===================
  237. */
  238. void SV_CheckForNewClients (void)
  239. {
  240. struct qsocket_s *ret;
  241. int i;
  242. //
  243. // check for new connections
  244. //
  245. while (1)
  246. {
  247. ret = NET_CheckNewConnections ();
  248. if (!ret)
  249. break;
  250. //
  251. // init a new client structure
  252. //
  253. for (i=0 ; i<svs.maxclients ; i++)
  254. if (!svs.clients[i].active)
  255. break;
  256. if (i == svs.maxclients)
  257. Sys_Error ("Host_CheckForNewClients: no free clients");
  258. svs.clients[i].netconnection = ret;
  259. SV_ConnectClient (i);
  260. net_activeconnections++;
  261. }
  262. }
  263. /*
  264. ===============================================================================
  265. FRAME UPDATES
  266. ===============================================================================
  267. */
  268. /*
  269. ==================
  270. SV_ClearDatagram
  271. ==================
  272. */
  273. void SV_ClearDatagram (void)
  274. {
  275. SZ_Clear (&sv.datagram);
  276. }
  277. /*
  278. =============================================================================
  279. The PVS must include a small area around the client to allow head bobbing
  280. or other small motion on the client side. Otherwise, a bob might cause an
  281. entity that should be visible to not show up, especially when the bob
  282. crosses a waterline.
  283. =============================================================================
  284. */
  285. int fatbytes;
  286. byte fatpvs[MAX_MAP_LEAFS/8];
  287. void SV_AddToFatPVS (vec3_t org, mnode_t *node)
  288. {
  289. int i;
  290. byte *pvs;
  291. mplane_t *plane;
  292. float d;
  293. while (1)
  294. {
  295. // if this is a leaf, accumulate the pvs bits
  296. if (node->contents < 0)
  297. {
  298. if (node->contents != CONTENTS_SOLID)
  299. {
  300. pvs = Mod_LeafPVS ( (mleaf_t *)node, sv.worldmodel);
  301. for (i=0 ; i<fatbytes ; i++)
  302. fatpvs[i] |= pvs[i];
  303. }
  304. return;
  305. }
  306. plane = node->plane;
  307. d = DotProduct (org, plane->normal) - plane->dist;
  308. if (d > 8)
  309. node = node->children[0];
  310. else if (d < -8)
  311. node = node->children[1];
  312. else
  313. { // go down both
  314. SV_AddToFatPVS (org, node->children[0]);
  315. node = node->children[1];
  316. }
  317. }
  318. }
  319. /*
  320. =============
  321. SV_FatPVS
  322. Calculates a PVS that is the inclusive or of all leafs within 8 pixels of the
  323. given point.
  324. =============
  325. */
  326. byte *SV_FatPVS (vec3_t org)
  327. {
  328. fatbytes = (sv.worldmodel->numleafs+31)>>3;
  329. Q_memset (fatpvs, 0, fatbytes);
  330. SV_AddToFatPVS (org, sv.worldmodel->nodes);
  331. return fatpvs;
  332. }
  333. //=============================================================================
  334. /*
  335. =============
  336. SV_WriteEntitiesToClient
  337. =============
  338. */
  339. void SV_WriteEntitiesToClient (edict_t *clent, sizebuf_t *msg)
  340. {
  341. int e, i;
  342. int bits;
  343. byte *pvs;
  344. vec3_t org;
  345. float miss;
  346. edict_t *ent;
  347. // find the client's PVS
  348. VectorAdd (clent->v.origin, clent->v.view_ofs, org);
  349. pvs = SV_FatPVS (org);
  350. // send over all entities (excpet the client) that touch the pvs
  351. ent = NEXT_EDICT(sv.edicts);
  352. for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
  353. {
  354. #ifdef QUAKE2
  355. // don't send if flagged for NODRAW and there are no lighting effects
  356. if (ent->v.effects == EF_NODRAW)
  357. continue;
  358. #endif
  359. // ignore if not touching a PV leaf
  360. if (ent != clent) // clent is ALLWAYS sent
  361. {
  362. // ignore ents without visible models
  363. if (!ent->v.modelindex || !pr_strings[ent->v.model])
  364. continue;
  365. for (i=0 ; i < ent->num_leafs ; i++)
  366. if (pvs[ent->leafnums[i] >> 3] & (1 << (ent->leafnums[i]&7) ))
  367. break;
  368. if (i == ent->num_leafs)
  369. continue; // not visible
  370. }
  371. if (msg->maxsize - msg->cursize < 16)
  372. {
  373. Con_Printf ("packet overflow\n");
  374. return;
  375. }
  376. // send an update
  377. bits = 0;
  378. for (i=0 ; i<3 ; i++)
  379. {
  380. miss = ent->v.origin[i] - ent->baseline.origin[i];
  381. if ( miss < -0.1 || miss > 0.1 )
  382. bits |= U_ORIGIN1<<i;
  383. }
  384. if ( ent->v.angles[0] != ent->baseline.angles[0] )
  385. bits |= U_ANGLE1;
  386. if ( ent->v.angles[1] != ent->baseline.angles[1] )
  387. bits |= U_ANGLE2;
  388. if ( ent->v.angles[2] != ent->baseline.angles[2] )
  389. bits |= U_ANGLE3;
  390. if (ent->v.movetype == MOVETYPE_STEP)
  391. bits |= U_NOLERP; // don't mess up the step animation
  392. if (ent->baseline.colormap != ent->v.colormap)
  393. bits |= U_COLORMAP;
  394. if (ent->baseline.skin != ent->v.skin)
  395. bits |= U_SKIN;
  396. if (ent->baseline.frame != ent->v.frame)
  397. bits |= U_FRAME;
  398. if (ent->baseline.effects != ent->v.effects)
  399. bits |= U_EFFECTS;
  400. if (ent->baseline.modelindex != ent->v.modelindex)
  401. bits |= U_MODEL;
  402. if (e >= 256)
  403. bits |= U_LONGENTITY;
  404. if (bits >= 256)
  405. bits |= U_MOREBITS;
  406. //
  407. // write the message
  408. //
  409. MSG_WriteByte (msg,bits | U_SIGNAL);
  410. if (bits & U_MOREBITS)
  411. MSG_WriteByte (msg, bits>>8);
  412. if (bits & U_LONGENTITY)
  413. MSG_WriteShort (msg,e);
  414. else
  415. MSG_WriteByte (msg,e);
  416. if (bits & U_MODEL)
  417. MSG_WriteByte (msg, ent->v.modelindex);
  418. if (bits & U_FRAME)
  419. MSG_WriteByte (msg, ent->v.frame);
  420. if (bits & U_COLORMAP)
  421. MSG_WriteByte (msg, ent->v.colormap);
  422. if (bits & U_SKIN)
  423. MSG_WriteByte (msg, ent->v.skin);
  424. if (bits & U_EFFECTS)
  425. MSG_WriteByte (msg, ent->v.effects);
  426. if (bits & U_ORIGIN1)
  427. MSG_WriteCoord (msg, ent->v.origin[0]);
  428. if (bits & U_ANGLE1)
  429. MSG_WriteAngle(msg, ent->v.angles[0]);
  430. if (bits & U_ORIGIN2)
  431. MSG_WriteCoord (msg, ent->v.origin[1]);
  432. if (bits & U_ANGLE2)
  433. MSG_WriteAngle(msg, ent->v.angles[1]);
  434. if (bits & U_ORIGIN3)
  435. MSG_WriteCoord (msg, ent->v.origin[2]);
  436. if (bits & U_ANGLE3)
  437. MSG_WriteAngle(msg, ent->v.angles[2]);
  438. }
  439. }
  440. /*
  441. =============
  442. SV_CleanupEnts
  443. =============
  444. */
  445. void SV_CleanupEnts (void)
  446. {
  447. int e;
  448. edict_t *ent;
  449. ent = NEXT_EDICT(sv.edicts);
  450. for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
  451. {
  452. ent->v.effects = (int)ent->v.effects & ~EF_MUZZLEFLASH;
  453. }
  454. }
  455. /*
  456. ==================
  457. SV_WriteClientdataToMessage
  458. ==================
  459. */
  460. void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
  461. {
  462. int bits;
  463. int i;
  464. edict_t *other;
  465. int items;
  466. #ifndef QUAKE2
  467. eval_t *val;
  468. #endif
  469. //
  470. // send a damage message
  471. //
  472. if (ent->v.dmg_take || ent->v.dmg_save)
  473. {
  474. other = PROG_TO_EDICT(ent->v.dmg_inflictor);
  475. MSG_WriteByte (msg, svc_damage);
  476. MSG_WriteByte (msg, ent->v.dmg_save);
  477. MSG_WriteByte (msg, ent->v.dmg_take);
  478. for (i=0 ; i<3 ; i++)
  479. MSG_WriteCoord (msg, other->v.origin[i] + 0.5*(other->v.mins[i] + other->v.maxs[i]));
  480. ent->v.dmg_take = 0;
  481. ent->v.dmg_save = 0;
  482. }
  483. //
  484. // send the current viewpos offset from the view entity
  485. //
  486. SV_SetIdealPitch (); // how much to look up / down ideally
  487. // a fixangle might get lost in a dropped packet. Oh well.
  488. if ( ent->v.fixangle )
  489. {
  490. MSG_WriteByte (msg, svc_setangle);
  491. for (i=0 ; i < 3 ; i++)
  492. MSG_WriteAngle (msg, ent->v.angles[i] );
  493. ent->v.fixangle = 0;
  494. }
  495. bits = 0;
  496. if (ent->v.view_ofs[2] != DEFAULT_VIEWHEIGHT)
  497. bits |= SU_VIEWHEIGHT;
  498. if (ent->v.idealpitch)
  499. bits |= SU_IDEALPITCH;
  500. // stuff the sigil bits into the high bits of items for sbar, or else
  501. // mix in items2
  502. #ifdef QUAKE2
  503. items = (int)ent->v.items | ((int)ent->v.items2 << 23);
  504. #else
  505. val = GetEdictFieldValue(ent, "items2");
  506. if (val)
  507. items = (int)ent->v.items | ((int)val->_float << 23);
  508. else
  509. items = (int)ent->v.items | ((int)pr_global_struct->serverflags << 28);
  510. #endif
  511. bits |= SU_ITEMS;
  512. if ( (int)ent->v.flags & FL_ONGROUND)
  513. bits |= SU_ONGROUND;
  514. if ( ent->v.waterlevel >= 2)
  515. bits |= SU_INWATER;
  516. for (i=0 ; i<3 ; i++)
  517. {
  518. if (ent->v.punchangle[i])
  519. bits |= (SU_PUNCH1<<i);
  520. if (ent->v.velocity[i])
  521. bits |= (SU_VELOCITY1<<i);
  522. }
  523. if (ent->v.weaponframe)
  524. bits |= SU_WEAPONFRAME;
  525. if (ent->v.armorvalue)
  526. bits |= SU_ARMOR;
  527. // if (ent->v.weapon)
  528. bits |= SU_WEAPON;
  529. // send the data
  530. MSG_WriteByte (msg, svc_clientdata);
  531. MSG_WriteShort (msg, bits);
  532. if (bits & SU_VIEWHEIGHT)
  533. MSG_WriteChar (msg, ent->v.view_ofs[2]);
  534. if (bits & SU_IDEALPITCH)
  535. MSG_WriteChar (msg, ent->v.idealpitch);
  536. for (i=0 ; i<3 ; i++)
  537. {
  538. if (bits & (SU_PUNCH1<<i))
  539. MSG_WriteChar (msg, ent->v.punchangle[i]);
  540. if (bits & (SU_VELOCITY1<<i))
  541. MSG_WriteChar (msg, ent->v.velocity[i]/16);
  542. }
  543. // [always sent] if (bits & SU_ITEMS)
  544. MSG_WriteLong (msg, items);
  545. if (bits & SU_WEAPONFRAME)
  546. MSG_WriteByte (msg, ent->v.weaponframe);
  547. if (bits & SU_ARMOR)
  548. MSG_WriteByte (msg, ent->v.armorvalue);
  549. if (bits & SU_WEAPON)
  550. MSG_WriteByte (msg, SV_ModelIndex(pr_strings+ent->v.weaponmodel));
  551. MSG_WriteShort (msg, ent->v.health);
  552. MSG_WriteByte (msg, ent->v.currentammo);
  553. MSG_WriteByte (msg, ent->v.ammo_shells);
  554. MSG_WriteByte (msg, ent->v.ammo_nails);
  555. MSG_WriteByte (msg, ent->v.ammo_rockets);
  556. MSG_WriteByte (msg, ent->v.ammo_cells);
  557. if (standard_quake)
  558. {
  559. MSG_WriteByte (msg, ent->v.weapon);
  560. }
  561. else
  562. {
  563. for(i=0;i<32;i++)
  564. {
  565. if ( ((int)ent->v.weapon) & (1<<i) )
  566. {
  567. MSG_WriteByte (msg, i);
  568. break;
  569. }
  570. }
  571. }
  572. }
  573. /*
  574. =======================
  575. SV_SendClientDatagram
  576. =======================
  577. */
  578. qboolean SV_SendClientDatagram (client_t *client)
  579. {
  580. byte buf[MAX_DATAGRAM];
  581. sizebuf_t msg;
  582. msg.data = buf;
  583. msg.maxsize = sizeof(buf);
  584. msg.cursize = 0;
  585. MSG_WriteByte (&msg, svc_time);
  586. MSG_WriteFloat (&msg, sv.time);
  587. // add the client specific data to the datagram
  588. SV_WriteClientdataToMessage (client->edict, &msg);
  589. SV_WriteEntitiesToClient (client->edict, &msg);
  590. // copy the server datagram if there is space
  591. if (msg.cursize + sv.datagram.cursize < msg.maxsize)
  592. SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
  593. // send the datagram
  594. if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
  595. {
  596. SV_DropClient (true);// if the message couldn't send, kick off
  597. return false;
  598. }
  599. return true;
  600. }
  601. /*
  602. =======================
  603. SV_UpdateToReliableMessages
  604. =======================
  605. */
  606. void SV_UpdateToReliableMessages (void)
  607. {
  608. int i, j;
  609. client_t *client;
  610. // check for changes to be sent over the reliable streams
  611. for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
  612. {
  613. if (host_client->old_frags != host_client->edict->v.frags)
  614. {
  615. for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
  616. {
  617. if (!client->active)
  618. continue;
  619. MSG_WriteByte (&client->message, svc_updatefrags);
  620. MSG_WriteByte (&client->message, i);
  621. MSG_WriteShort (&client->message, host_client->edict->v.frags);
  622. }
  623. host_client->old_frags = host_client->edict->v.frags;
  624. }
  625. }
  626. for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
  627. {
  628. if (!client->active)
  629. continue;
  630. SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
  631. }
  632. SZ_Clear (&sv.reliable_datagram);
  633. }
  634. /*
  635. =======================
  636. SV_SendNop
  637. Send a nop message without trashing or sending the accumulated client
  638. message buffer
  639. =======================
  640. */
  641. void SV_SendNop (client_t *client)
  642. {
  643. sizebuf_t msg;
  644. byte buf[4];
  645. msg.data = buf;
  646. msg.maxsize = sizeof(buf);
  647. msg.cursize = 0;
  648. MSG_WriteChar (&msg, svc_nop);
  649. if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
  650. SV_DropClient (true); // if the message couldn't send, kick off
  651. client->last_message = realtime;
  652. }
  653. /*
  654. =======================
  655. SV_SendClientMessages
  656. =======================
  657. */
  658. void SV_SendClientMessages (void)
  659. {
  660. int i;
  661. // update frags, names, etc
  662. SV_UpdateToReliableMessages ();
  663. // build individual updates
  664. for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
  665. {
  666. if (!host_client->active)
  667. continue;
  668. if (host_client->spawned)
  669. {
  670. if (!SV_SendClientDatagram (host_client))
  671. continue;
  672. }
  673. else
  674. {
  675. // the player isn't totally in the game yet
  676. // send small keepalive messages if too much time has passed
  677. // send a full message when the next signon stage has been requested
  678. // some other message data (name changes, etc) may accumulate
  679. // between signon stages
  680. if (!host_client->sendsignon)
  681. {
  682. if (realtime - host_client->last_message > 5)
  683. SV_SendNop (host_client);
  684. continue; // don't send out non-signon messages
  685. }
  686. }
  687. // check for an overflowed message. Should only happen
  688. // on a very fucked up connection that backs up a lot, then
  689. // changes level
  690. if (host_client->message.overflowed)
  691. {
  692. SV_DropClient (true);
  693. host_client->message.overflowed = false;
  694. continue;
  695. }
  696. if (host_client->message.cursize || host_client->dropasap)
  697. {
  698. if (!NET_CanSendMessage (host_client->netconnection))
  699. {
  700. // I_Printf ("can't write\n");
  701. continue;
  702. }
  703. if (host_client->dropasap)
  704. SV_DropClient (false); // went to another level
  705. else
  706. {
  707. if (NET_SendMessage (host_client->netconnection
  708. , &host_client->message) == -1)
  709. SV_DropClient (true); // if the message couldn't send, kick off
  710. SZ_Clear (&host_client->message);
  711. host_client->last_message = realtime;
  712. host_client->sendsignon = false;
  713. }
  714. }
  715. }
  716. // clear muzzle flashes
  717. SV_CleanupEnts ();
  718. }
  719. /*
  720. ==============================================================================
  721. SERVER SPAWNING
  722. ==============================================================================
  723. */
  724. /*
  725. ================
  726. SV_ModelIndex
  727. ================
  728. */
  729. int SV_ModelIndex (char *name)
  730. {
  731. int i;
  732. if (!name || !name[0])
  733. return 0;
  734. for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
  735. if (!strcmp(sv.model_precache[i], name))
  736. return i;
  737. if (i==MAX_MODELS || !sv.model_precache[i])
  738. Sys_Error ("SV_ModelIndex: model %s not precached", name);
  739. return i;
  740. }
  741. /*
  742. ================
  743. SV_CreateBaseline
  744. ================
  745. */
  746. void SV_CreateBaseline (void)
  747. {
  748. int i;
  749. edict_t *svent;
  750. int entnum;
  751. for (entnum = 0; entnum < sv.num_edicts ; entnum++)
  752. {
  753. // get the current server version
  754. svent = EDICT_NUM(entnum);
  755. if (svent->free)
  756. continue;
  757. if (entnum > svs.maxclients && !svent->v.modelindex)
  758. continue;
  759. //
  760. // create entity baseline
  761. //
  762. VectorCopy (svent->v.origin, svent->baseline.origin);
  763. VectorCopy (svent->v.angles, svent->baseline.angles);
  764. svent->baseline.frame = svent->v.frame;
  765. svent->baseline.skin = svent->v.skin;
  766. if (entnum > 0 && entnum <= svs.maxclients)
  767. {
  768. svent->baseline.colormap = entnum;
  769. svent->baseline.modelindex = SV_ModelIndex("progs/player.mdl");
  770. }
  771. else
  772. {
  773. svent->baseline.colormap = 0;
  774. svent->baseline.modelindex =
  775. SV_ModelIndex(pr_strings + svent->v.model);
  776. }
  777. //
  778. // add to the message
  779. //
  780. MSG_WriteByte (&sv.signon,svc_spawnbaseline);
  781. MSG_WriteShort (&sv.signon,entnum);
  782. MSG_WriteByte (&sv.signon, svent->baseline.modelindex);
  783. MSG_WriteByte (&sv.signon, svent->baseline.frame);
  784. MSG_WriteByte (&sv.signon, svent->baseline.colormap);
  785. MSG_WriteByte (&sv.signon, svent->baseline.skin);
  786. for (i=0 ; i<3 ; i++)
  787. {
  788. MSG_WriteCoord(&sv.signon, svent->baseline.origin[i]);
  789. MSG_WriteAngle(&sv.signon, svent->baseline.angles[i]);
  790. }
  791. }
  792. }
  793. /*
  794. ================
  795. SV_SendReconnect
  796. Tell all the clients that the server is changing levels
  797. ================
  798. */
  799. void SV_SendReconnect (void)
  800. {
  801. char data[128];
  802. sizebuf_t msg;
  803. msg.data = data;
  804. msg.cursize = 0;
  805. msg.maxsize = sizeof(data);
  806. MSG_WriteChar (&msg, svc_stufftext);
  807. MSG_WriteString (&msg, "reconnect\n");
  808. NET_SendToAll (&msg, 5);
  809. if (cls.state != ca_dedicated)
  810. #ifdef QUAKE2
  811. Cbuf_InsertText ("reconnect\n");
  812. #else
  813. Cmd_ExecuteString ("reconnect\n", src_command);
  814. #endif
  815. }
  816. /*
  817. ================
  818. SV_SaveSpawnparms
  819. Grabs the current state of each client for saving across the
  820. transition to another level
  821. ================
  822. */
  823. void SV_SaveSpawnparms (void)
  824. {
  825. int i, j;
  826. svs.serverflags = pr_global_struct->serverflags;
  827. for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
  828. {
  829. if (!host_client->active)
  830. continue;
  831. // call the progs to get default spawn parms for the new client
  832. pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
  833. PR_ExecuteProgram (pr_global_struct->SetChangeParms);
  834. for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
  835. host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
  836. }
  837. }
  838. /*
  839. ================
  840. SV_SpawnServer
  841. This is called at the start of each level
  842. ================
  843. */
  844. extern float scr_centertime_off;
  845. #ifdef QUAKE2
  846. void SV_SpawnServer (char *server, char *startspot)
  847. #else
  848. void SV_SpawnServer (char *server)
  849. #endif
  850. {
  851. edict_t *ent;
  852. int i;
  853. // let's not have any servers with no name
  854. if (hostname.string[0] == 0)
  855. Cvar_Set ("hostname", "UNNAMED");
  856. scr_centertime_off = 0;
  857. Con_DPrintf ("SpawnServer: %s\n",server);
  858. svs.changelevel_issued = false; // now safe to issue another
  859. //
  860. // tell all connected clients that we are going to a new level
  861. //
  862. if (sv.active)
  863. {
  864. SV_SendReconnect ();
  865. }
  866. //
  867. // make cvars consistant
  868. //
  869. if (coop.value)
  870. Cvar_SetValue ("deathmatch", 0);
  871. current_skill = (int)(skill.value + 0.5);
  872. if (current_skill < 0)
  873. current_skill = 0;
  874. if (current_skill > 3)
  875. current_skill = 3;
  876. Cvar_SetValue ("skill", (float)current_skill);
  877. //
  878. // set up the new server
  879. //
  880. Host_ClearMemory ();
  881. memset (&sv, 0, sizeof(sv));
  882. strcpy (sv.name, server);
  883. #ifdef QUAKE2
  884. if (startspot)
  885. strcpy(sv.startspot, startspot);
  886. #endif
  887. // load progs to get entity field count
  888. PR_LoadProgs ();
  889. // allocate server memory
  890. sv.max_edicts = MAX_EDICTS;
  891. sv.edicts = Hunk_AllocName (sv.max_edicts*pr_edict_size, "edicts");
  892. sv.datagram.maxsize = sizeof(sv.datagram_buf);
  893. sv.datagram.cursize = 0;
  894. sv.datagram.data = sv.datagram_buf;
  895. sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
  896. sv.reliable_datagram.cursize = 0;
  897. sv.reliable_datagram.data = sv.reliable_datagram_buf;
  898. sv.signon.maxsize = sizeof(sv.signon_buf);
  899. sv.signon.cursize = 0;
  900. sv.signon.data = sv.signon_buf;
  901. // leave slots at start for clients only
  902. sv.num_edicts = svs.maxclients+1;
  903. for (i=0 ; i<svs.maxclients ; i++)
  904. {
  905. ent = EDICT_NUM(i+1);
  906. svs.clients[i].edict = ent;
  907. }
  908. sv.state = ss_loading;
  909. sv.paused = false;
  910. sv.time = 1.0;
  911. strcpy (sv.name, server);
  912. sprintf (sv.modelname,"maps/%s.bsp", server);
  913. sv.worldmodel = Mod_ForName (sv.modelname, false);
  914. if (!sv.worldmodel)
  915. {
  916. Con_Printf ("Couldn't spawn server %s\n", sv.modelname);
  917. sv.active = false;
  918. return;
  919. }
  920. sv.models[1] = sv.worldmodel;
  921. //
  922. // clear world interaction links
  923. //
  924. SV_ClearWorld ();
  925. sv.sound_precache[0] = pr_strings;
  926. sv.model_precache[0] = pr_strings;
  927. sv.model_precache[1] = sv.modelname;
  928. for (i=1 ; i<sv.worldmodel->numsubmodels ; i++)
  929. {
  930. sv.model_precache[1+i] = localmodels[i];
  931. sv.models[i+1] = Mod_ForName (localmodels[i], false);
  932. }
  933. //
  934. // load the rest of the entities
  935. //
  936. ent = EDICT_NUM(0);
  937. memset (&ent->v, 0, progs->entityfields * 4);
  938. ent->free = false;
  939. ent->v.model = sv.worldmodel->name - pr_strings;
  940. ent->v.modelindex = 1; // world model
  941. ent->v.solid = SOLID_BSP;
  942. ent->v.movetype = MOVETYPE_PUSH;
  943. if (coop.value)
  944. pr_global_struct->coop = coop.value;
  945. else
  946. pr_global_struct->deathmatch = deathmatch.value;
  947. pr_global_struct->mapname = sv.name - pr_strings;
  948. #ifdef QUAKE2
  949. pr_global_struct->startspot = sv.startspot - pr_strings;
  950. #endif
  951. // serverflags are for cross level information (sigils)
  952. pr_global_struct->serverflags = svs.serverflags;
  953. ED_LoadFromFile (sv.worldmodel->entities);
  954. sv.active = true;
  955. // all setup is completed, any further precache statements are errors
  956. sv.state = ss_active;
  957. // run two frames to allow everything to settle
  958. host_frametime = 0.1;
  959. SV_Physics ();
  960. SV_Physics ();
  961. // create a baseline for more efficient communications
  962. SV_CreateBaseline ();
  963. // send serverinfo to all connected clients
  964. for (i=0,host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
  965. if (host_client->active)
  966. SV_SendServerinfo (host_client);
  967. Con_DPrintf ("Server spawned.\n");
  968. }