sv_init.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. #include "qwsvdef.h"
  16. server_static_t svs; // persistant server info
  17. server_t sv; // local server
  18. char localmodels[MAX_MODELS][5]; // inline model names for precache
  19. char localinfo[MAX_LOCALINFO_STRING+1]; // local game info
  20. /*
  21. ================
  22. SV_ModelIndex
  23. ================
  24. */
  25. int SV_ModelIndex (char *name)
  26. {
  27. int i;
  28. if (!name || !name[0])
  29. return 0;
  30. for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
  31. if (!strcmp(sv.model_precache[i], name))
  32. return i;
  33. if (i==MAX_MODELS || !sv.model_precache[i])
  34. SV_Error ("SV_ModelIndex: model %s not precached", name);
  35. return i;
  36. }
  37. /*
  38. ================
  39. SV_FlushSignon
  40. Moves to the next signon buffer if needed
  41. ================
  42. */
  43. void SV_FlushSignon (void)
  44. {
  45. if (sv.signon.cursize < sv.signon.maxsize - 512)
  46. return;
  47. if (sv.num_signon_buffers == MAX_SIGNON_BUFFERS-1)
  48. SV_Error ("sv.num_signon_buffers == MAX_SIGNON_BUFFERS-1");
  49. sv.signon_buffer_size[sv.num_signon_buffers-1] = sv.signon.cursize;
  50. sv.signon.data = sv.signon_buffers[sv.num_signon_buffers];
  51. sv.num_signon_buffers++;
  52. sv.signon.cursize = 0;
  53. }
  54. /*
  55. ================
  56. SV_CreateBaseline
  57. Entity baselines are used to compress the update messages
  58. to the clients -- only the fields that differ from the
  59. baseline will be transmitted
  60. ================
  61. */
  62. void SV_CreateBaseline (void)
  63. {
  64. int i;
  65. edict_t *svent;
  66. int entnum;
  67. for (entnum = 0; entnum < sv.num_edicts ; entnum++)
  68. {
  69. svent = EDICT_NUM(entnum);
  70. if (svent->free)
  71. continue;
  72. // create baselines for all player slots,
  73. // and any other edict that has a visible model
  74. if (entnum > MAX_CLIENTS && !svent->v.modelindex)
  75. continue;
  76. //
  77. // create entity baseline
  78. //
  79. VectorCopy (svent->v.origin, svent->baseline.origin);
  80. VectorCopy (svent->v.angles, svent->baseline.angles);
  81. svent->baseline.frame = svent->v.frame;
  82. svent->baseline.skinnum = svent->v.skin;
  83. if (entnum > 0 && entnum <= MAX_CLIENTS)
  84. {
  85. svent->baseline.colormap = entnum;
  86. svent->baseline.modelindex = SV_ModelIndex("progs/player.mdl");
  87. }
  88. else
  89. {
  90. svent->baseline.colormap = 0;
  91. svent->baseline.modelindex =
  92. SV_ModelIndex(PR_GetString(svent->v.model));
  93. }
  94. //
  95. // flush the signon message out to a seperate buffer if
  96. // nearly full
  97. //
  98. SV_FlushSignon ();
  99. //
  100. // add to the message
  101. //
  102. MSG_WriteByte (&sv.signon,svc_spawnbaseline);
  103. MSG_WriteShort (&sv.signon,entnum);
  104. MSG_WriteByte (&sv.signon, svent->baseline.modelindex);
  105. MSG_WriteByte (&sv.signon, svent->baseline.frame);
  106. MSG_WriteByte (&sv.signon, svent->baseline.colormap);
  107. MSG_WriteByte (&sv.signon, svent->baseline.skinnum);
  108. for (i=0 ; i<3 ; i++)
  109. {
  110. MSG_WriteCoord(&sv.signon, svent->baseline.origin[i]);
  111. MSG_WriteAngle(&sv.signon, svent->baseline.angles[i]);
  112. }
  113. }
  114. }
  115. /*
  116. ================
  117. SV_SaveSpawnparms
  118. Grabs the current state of the progs serverinfo flags
  119. and each client for saving across the
  120. transition to another level
  121. ================
  122. */
  123. void SV_SaveSpawnparms (void)
  124. {
  125. int i, j;
  126. if (!sv.state)
  127. return; // no progs loaded yet
  128. // serverflags is the only game related thing maintained
  129. svs.serverflags = pr_global_struct->serverflags;
  130. for (i=0, host_client = svs.clients ; i<MAX_CLIENTS ; i++, host_client++)
  131. {
  132. if (host_client->state != cs_spawned)
  133. continue;
  134. // needs to reconnect
  135. host_client->state = cs_connected;
  136. // call the progs to get default spawn parms for the new client
  137. pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
  138. PR_ExecuteProgram (pr_global_struct->SetChangeParms);
  139. for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
  140. host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
  141. }
  142. }
  143. /*
  144. ================
  145. SV_CalcPHS
  146. Expands the PVS and calculates the PHS
  147. (Potentially Hearable Set)
  148. ================
  149. */
  150. void SV_CalcPHS (void)
  151. {
  152. int rowbytes, rowwords;
  153. int i, j, k, l, index, num;
  154. int bitbyte;
  155. unsigned *dest, *src;
  156. byte *scan;
  157. int count, vcount;
  158. Con_Printf ("Building PHS...\n");
  159. num = sv.worldmodel->numleafs;
  160. rowwords = (num+31)>>5;
  161. rowbytes = rowwords*4;
  162. sv.pvs = Hunk_Alloc (rowbytes*num);
  163. scan = sv.pvs;
  164. vcount = 0;
  165. for (i=0 ; i<num ; i++, scan+=rowbytes)
  166. {
  167. memcpy (scan, Mod_LeafPVS(sv.worldmodel->leafs+i, sv.worldmodel),
  168. rowbytes);
  169. if (i == 0)
  170. continue;
  171. for (j=0 ; j<num ; j++)
  172. {
  173. if ( scan[j>>3] & (1<<(j&7)) )
  174. {
  175. vcount++;
  176. }
  177. }
  178. }
  179. sv.phs = Hunk_Alloc (rowbytes*num);
  180. count = 0;
  181. scan = sv.pvs;
  182. dest = (unsigned *)sv.phs;
  183. for (i=0 ; i<num ; i++, dest += rowwords, scan += rowbytes)
  184. {
  185. memcpy (dest, scan, rowbytes);
  186. for (j=0 ; j<rowbytes ; j++)
  187. {
  188. bitbyte = scan[j];
  189. if (!bitbyte)
  190. continue;
  191. for (k=0 ; k<8 ; k++)
  192. {
  193. if (! (bitbyte & (1<<k)) )
  194. continue;
  195. // or this pvs row into the phs
  196. // +1 because pvs is 1 based
  197. index = ((j<<3)+k+1);
  198. if (index >= num)
  199. continue;
  200. src = (unsigned *)sv.pvs + index*rowwords;
  201. for (l=0 ; l<rowwords ; l++)
  202. dest[l] |= src[l];
  203. }
  204. }
  205. if (i == 0)
  206. continue;
  207. for (j=0 ; j<num ; j++)
  208. if ( ((byte *)dest)[j>>3] & (1<<(j&7)) )
  209. count++;
  210. }
  211. Con_Printf ("Average leafs visible / hearable / total: %i / %i / %i\n"
  212. , vcount/num, count/num, num);
  213. }
  214. unsigned SV_CheckModel(char *mdl)
  215. {
  216. byte stackbuf[1024]; // avoid dirtying the cache heap
  217. byte *buf;
  218. unsigned short crc;
  219. // int len;
  220. buf = (byte *)COM_LoadStackFile (mdl, stackbuf, sizeof(stackbuf));
  221. crc = CRC_Block(buf, com_filesize);
  222. // for (len = com_filesize; len; len--, buf++)
  223. // CRC_ProcessByte(&crc, *buf);
  224. return crc;
  225. }
  226. /*
  227. ================
  228. SV_SpawnServer
  229. Change the server to a new map, taking all connected
  230. clients along with it.
  231. This is only called from the SV_Map_f() function.
  232. ================
  233. */
  234. void SV_SpawnServer (char *server)
  235. {
  236. edict_t *ent;
  237. int i;
  238. Con_DPrintf ("SpawnServer: %s\n",server);
  239. SV_SaveSpawnparms ();
  240. svs.spawncount++; // any partially connected client will be
  241. // restarted
  242. sv.state = ss_dead;
  243. Mod_ClearAll ();
  244. Hunk_FreeToLowMark (host_hunklevel);
  245. // wipe the entire per-level structure
  246. memset (&sv, 0, sizeof(sv));
  247. sv.datagram.maxsize = sizeof(sv.datagram_buf);
  248. sv.datagram.data = sv.datagram_buf;
  249. sv.datagram.allowoverflow = true;
  250. sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
  251. sv.reliable_datagram.data = sv.reliable_datagram_buf;
  252. sv.multicast.maxsize = sizeof(sv.multicast_buf);
  253. sv.multicast.data = sv.multicast_buf;
  254. sv.master.maxsize = sizeof(sv.master_buf);
  255. sv.master.data = sv.master_buf;
  256. sv.signon.maxsize = sizeof(sv.signon_buffers[0]);
  257. sv.signon.data = sv.signon_buffers[0];
  258. sv.num_signon_buffers = 1;
  259. strcpy (sv.name, server);
  260. // load progs to get entity field count
  261. // which determines how big each edict is
  262. PR_LoadProgs ();
  263. // allocate edicts
  264. sv.edicts = Hunk_AllocName (MAX_EDICTS*pr_edict_size, "edicts");
  265. // leave slots at start for clients only
  266. sv.num_edicts = MAX_CLIENTS+1;
  267. for (i=0 ; i<MAX_CLIENTS ; i++)
  268. {
  269. ent = EDICT_NUM(i+1);
  270. svs.clients[i].edict = ent;
  271. //ZOID - make sure we update frags right
  272. svs.clients[i].old_frags = 0;
  273. }
  274. sv.time = 1.0;
  275. strcpy (sv.name, server);
  276. sprintf (sv.modelname,"maps/%s.bsp", server);
  277. sv.worldmodel = Mod_ForName (sv.modelname, true);
  278. SV_CalcPHS ();
  279. //
  280. // clear physics interaction links
  281. //
  282. SV_ClearWorld ();
  283. sv.sound_precache[0] = pr_strings;
  284. sv.model_precache[0] = pr_strings;
  285. sv.model_precache[1] = sv.modelname;
  286. sv.models[1] = sv.worldmodel;
  287. for (i=1 ; i<sv.worldmodel->numsubmodels ; i++)
  288. {
  289. sv.model_precache[1+i] = localmodels[i];
  290. sv.models[i+1] = Mod_ForName (localmodels[i], false);
  291. }
  292. //check player/eyes models for hacks
  293. sv.model_player_checksum = SV_CheckModel("progs/player.mdl");
  294. sv.eyes_player_checksum = SV_CheckModel("progs/eyes.mdl");
  295. //
  296. // spawn the rest of the entities on the map
  297. //
  298. // precache and static commands can be issued during
  299. // map initialization
  300. sv.state = ss_loading;
  301. ent = EDICT_NUM(0);
  302. ent->free = false;
  303. ent->v.model = PR_SetString(sv.worldmodel->name);
  304. ent->v.modelindex = 1; // world model
  305. ent->v.solid = SOLID_BSP;
  306. ent->v.movetype = MOVETYPE_PUSH;
  307. pr_global_struct->mapname = PR_SetString(sv.name);
  308. // serverflags are for cross level information (sigils)
  309. pr_global_struct->serverflags = svs.serverflags;
  310. // run the frame start qc function to let progs check cvars
  311. SV_ProgStartFrame ();
  312. // load and spawn all other entities
  313. ED_LoadFromFile (sv.worldmodel->entities);
  314. // look up some model indexes for specialized message compression
  315. SV_FindModelNumbers ();
  316. // all spawning is completed, any further precache statements
  317. // or prog writes to the signon message are errors
  318. sv.state = ss_active;
  319. // run two frames to allow everything to settle
  320. host_frametime = 0.1;
  321. SV_Physics ();
  322. SV_Physics ();
  323. // save movement vars
  324. SV_SetMoveVars();
  325. // create a baseline for more efficient communications
  326. SV_CreateBaseline ();
  327. sv.signon_buffer_size[sv.num_signon_buffers-1] = sv.signon.cursize;
  328. Info_SetValueForKey (svs.info, "map", sv.name, MAX_SERVERINFO_STRING);
  329. Con_DPrintf ("Server spawned.\n");
  330. }