2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // sv_main.c -- server main program
24 static cvar_t sv_cullentities_pvs = {0, "sv_cullentities_pvs", "0"}; // fast but loose
25 static cvar_t sv_cullentities_portal = {0, "sv_cullentities_portal", "0"}; // extremely accurate visibility checking, but too slow
26 static cvar_t sv_cullentities_trace = {0, "sv_cullentities_trace", "1"}; // tends to get false negatives, uses a timeout to keep entities visible a short time after becoming hidden
27 static cvar_t sv_cullentities_stats = {0, "sv_cullentities_stats", "0"};
32 static char localmodels[MAX_MODELS][5]; // inline model names for precache
34 static mempool_t *sv_edicts_mempool = NULL;
36 //============================================================================
38 extern void SV_Phys_Init (void);
39 extern void SV_World_Init (void);
50 Cvar_RegisterVariable (&sv_maxvelocity);
51 Cvar_RegisterVariable (&sv_gravity);
52 Cvar_RegisterVariable (&sv_friction);
53 Cvar_RegisterVariable (&sv_edgefriction);
54 Cvar_RegisterVariable (&sv_stopspeed);
55 Cvar_RegisterVariable (&sv_maxspeed);
56 Cvar_RegisterVariable (&sv_accelerate);
57 Cvar_RegisterVariable (&sv_idealpitchscale);
58 Cvar_RegisterVariable (&sv_aim);
59 Cvar_RegisterVariable (&sv_nostep);
60 Cvar_RegisterVariable (&sv_predict);
61 Cvar_RegisterVariable (&sv_deltacompress);
62 Cvar_RegisterVariable (&sv_cullentities_pvs);
63 Cvar_RegisterVariable (&sv_cullentities_portal);
64 Cvar_RegisterVariable (&sv_cullentities_trace);
65 Cvar_RegisterVariable (&sv_cullentities_stats);
70 for (i = 0;i < MAX_MODELS;i++)
71 sprintf (localmodels[i], "*%i", i);
73 sv_edicts_mempool = Mem_AllocPool("edicts");
77 =============================================================================
81 =============================================================================
88 Make sure the event gets sent to all clients
91 void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
95 if (sv.datagram.cursize > MAX_DATAGRAM-16)
97 MSG_WriteByte (&sv.datagram, svc_particle);
98 MSG_WriteDPCoord (&sv.datagram, org[0]);
99 MSG_WriteDPCoord (&sv.datagram, org[1]);
100 MSG_WriteDPCoord (&sv.datagram, org[2]);
101 for (i=0 ; i<3 ; i++)
108 MSG_WriteChar (&sv.datagram, v);
110 MSG_WriteByte (&sv.datagram, count);
111 MSG_WriteByte (&sv.datagram, color);
118 Make sure the event gets sent to all clients
121 void SV_StartEffect (vec3_t org, int modelindex, int startframe, int framecount, int framerate)
123 if (sv.datagram.cursize > MAX_DATAGRAM-18)
125 if (modelindex >= 256 || startframe >= 256)
127 MSG_WriteByte (&sv.datagram, svc_effect2);
128 MSG_WriteDPCoord (&sv.datagram, org[0]);
129 MSG_WriteDPCoord (&sv.datagram, org[1]);
130 MSG_WriteDPCoord (&sv.datagram, org[2]);
131 MSG_WriteShort (&sv.datagram, modelindex);
132 MSG_WriteShort (&sv.datagram, startframe);
133 MSG_WriteByte (&sv.datagram, framecount);
134 MSG_WriteByte (&sv.datagram, framerate);
138 MSG_WriteByte (&sv.datagram, svc_effect);
139 MSG_WriteDPCoord (&sv.datagram, org[0]);
140 MSG_WriteDPCoord (&sv.datagram, org[1]);
141 MSG_WriteDPCoord (&sv.datagram, org[2]);
142 MSG_WriteByte (&sv.datagram, modelindex);
143 MSG_WriteByte (&sv.datagram, startframe);
144 MSG_WriteByte (&sv.datagram, framecount);
145 MSG_WriteByte (&sv.datagram, framerate);
153 Each entity can have eight independant sound sources, like voice,
156 Channel 0 is an auto-allocate channel, the others override anything
157 already running on that entity/channel pair.
159 An attenuation of 0 will play full volume everywhere in the level.
160 Larger attenuations will drop off. (max 4 attenuation)
164 void SV_StartSound (edict_t *entity, int channel, char *sample, int volume,
172 if (volume < 0 || volume > 255)
173 Host_Error ("SV_StartSound: volume = %i", volume);
175 if (attenuation < 0 || attenuation > 4)
176 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
178 if (channel < 0 || channel > 7)
179 Host_Error ("SV_StartSound: channel = %i", channel);
181 if (sv.datagram.cursize > MAX_DATAGRAM-16)
184 // find precache number for sound
185 for (sound_num=1 ; sound_num<MAX_SOUNDS
186 && sv.sound_precache[sound_num] ; sound_num++)
187 if (!strcmp(sample, sv.sound_precache[sound_num]))
190 if ( sound_num == MAX_SOUNDS || !sv.sound_precache[sound_num] )
192 Con_Printf ("SV_StartSound: %s not precached\n", sample);
196 ent = NUM_FOR_EDICT(entity);
198 channel = (ent<<3) | channel;
201 if (volume != DEFAULT_SOUND_PACKET_VOLUME)
202 field_mask |= SND_VOLUME;
203 if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
204 field_mask |= SND_ATTENUATION;
206 // directed messages go only to the entity they are targeted on
207 if (sound_num >= 256)
208 MSG_WriteByte (&sv.datagram, svc_sound2);
210 MSG_WriteByte (&sv.datagram, svc_sound);
211 MSG_WriteByte (&sv.datagram, field_mask);
212 if (field_mask & SND_VOLUME)
213 MSG_WriteByte (&sv.datagram, volume);
214 if (field_mask & SND_ATTENUATION)
215 MSG_WriteByte (&sv.datagram, attenuation*64);
216 MSG_WriteShort (&sv.datagram, channel);
217 if (sound_num >= 256)
218 MSG_WriteShort (&sv.datagram, sound_num);
220 MSG_WriteByte (&sv.datagram, sound_num);
221 for (i=0 ; i<3 ; i++)
222 MSG_WriteDPCoord (&sv.datagram, entity->v.origin[i]+0.5*(entity->v.mins[i]+entity->v.maxs[i]));
226 ==============================================================================
230 ==============================================================================
237 Sends the first message from the server to a connected client.
238 This will be sent on the initial connection and upon each server load.
241 void SV_SendServerinfo (client_t *client)
246 // LordHavoc: clear entityframe tracking
247 client->entityframenumber = 0;
248 EntityFrame_ClearDatabase(&client->entitydatabase);
250 MSG_WriteByte (&client->message, svc_print);
251 sprintf (message, "\002\nServer: %s build %s (progs %i crc)", gamename, buildstring, pr_crc);
252 MSG_WriteString (&client->message,message);
254 MSG_WriteByte (&client->message, svc_serverinfo);
255 MSG_WriteLong (&client->message, DPPROTOCOL_VERSION3);
256 MSG_WriteByte (&client->message, svs.maxclients);
258 if (!coop.integer && deathmatch.integer)
259 MSG_WriteByte (&client->message, GAME_DEATHMATCH);
261 MSG_WriteByte (&client->message, GAME_COOP);
263 sprintf (message, pr_strings+sv.edicts->v.message);
265 MSG_WriteString (&client->message,message);
267 for (s = sv.model_precache+1 ; *s ; s++)
268 MSG_WriteString (&client->message, *s);
269 MSG_WriteByte (&client->message, 0);
271 for (s = sv.sound_precache+1 ; *s ; s++)
272 MSG_WriteString (&client->message, *s);
273 MSG_WriteByte (&client->message, 0);
276 MSG_WriteByte (&client->message, svc_cdtrack);
277 MSG_WriteByte (&client->message, sv.edicts->v.sounds);
278 MSG_WriteByte (&client->message, sv.edicts->v.sounds);
281 MSG_WriteByte (&client->message, svc_setview);
282 MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
284 MSG_WriteByte (&client->message, svc_signonnum);
285 MSG_WriteByte (&client->message, 1);
287 client->sendsignon = true;
288 client->spawned = false; // need prespawn, spawn, etc
295 Initializes a client_t for a new net connection. This will only be called
296 once for a player each game, not once for each level change.
299 void SV_ConnectClient (int clientnum)
304 struct qsocket_s *netconnection;
306 float spawn_parms[NUM_SPAWN_PARMS];
308 client = svs.clients + clientnum;
310 Con_DPrintf ("Client %s connected\n", client->netconnection->address);
312 edictnum = clientnum+1;
314 ent = EDICT_NUM(edictnum);
316 // set up the client_t
317 netconnection = client->netconnection;
320 memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
321 memset (client, 0, sizeof(*client));
322 client->netconnection = netconnection;
324 strcpy (client->name, "unconnected");
325 client->active = true;
326 client->spawned = false;
328 client->message.data = client->msgbuf;
329 client->message.maxsize = sizeof(client->msgbuf);
330 client->message.allowoverflow = true; // we can catch it
333 memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
336 // call the progs to get default spawn parms for the new client
337 PR_ExecuteProgram (pr_global_struct->SetNewParms, "QC function SetNewParms is missing");
338 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
339 client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
343 SV_SendServerinfo (client);
345 // send serverinfo on first nop
346 client->waitingforconnect = true;
347 client->sendsignon = true;
348 client->spawned = false; // need prespawn, spawn, etc
355 SV_CheckForNewClients
359 void SV_CheckForNewClients (void)
361 struct qsocket_s *ret;
365 // check for new connections
369 ret = NET_CheckNewConnections ();
374 // init a new client structure
376 for (i=0 ; i<svs.maxclients ; i++)
377 if (!svs.clients[i].active)
379 if (i == svs.maxclients)
380 Sys_Error ("Host_CheckForNewClients: no free clients");
382 svs.clients[i].netconnection = ret;
383 SV_ConnectClient (i);
385 net_activeconnections++;
392 ===============================================================================
396 ===============================================================================
405 void SV_ClearDatagram (void)
407 SZ_Clear (&sv.datagram);
411 =============================================================================
413 The PVS must include a small area around the client to allow head bobbing
414 or other small motion on the client side. Otherwise, a bob might cause an
415 entity that should be visible to not show up, especially when the bob
418 =============================================================================
422 qbyte fatpvs[MAX_MAP_LEAFS/8];
424 void SV_AddToFatPVS (vec3_t org, mnode_t *node)
433 // if this is a leaf, accumulate the pvs bits
434 if (node->contents < 0)
436 if (node->contents != CONTENTS_SOLID)
438 pvs = Mod_LeafPVS ( (mleaf_t *)node, sv.worldmodel);
439 for (i=0 ; i<fatbytes ; i++)
446 d = DotProduct (org, plane->normal) - plane->dist;
448 node = node->children[0];
450 node = node->children[1];
453 SV_AddToFatPVS (org, node->children[0]);
454 node = node->children[1];
463 Calculates a PVS that is the inclusive or of all leafs within 8 pixels of the
467 qbyte *SV_FatPVS (vec3_t org)
469 fatbytes = (sv.worldmodel->numleafs+31)>>3;
470 memset (fatpvs, 0, fatbytes);
471 SV_AddToFatPVS (org, sv.worldmodel->nodes);
475 //=============================================================================
478 int SV_BoxTouchingPVS (qbyte *pvs, vec3_t mins, vec3_t maxs, mnode_t *node)
482 if (node->contents < 0)
485 if (node->contents == CONTENTS_SOLID)
487 leafnum = (mleaf_t *)node - sv.worldmodel->leafs - 1;
488 return pvs[leafnum >> 3] & (1 << (leafnum & 7));
491 // node - recurse down the BSP tree
492 switch (BOX_ON_PLANE_SIDE(mins, maxs, node->plane))
495 node = node->children[0];
498 node = node->children[1];
501 if (node->children[0]->contents != CONTENTS_SOLID)
502 if (SV_BoxTouchingPVS (pvs, mins, maxs, node->children[0]))
504 node = node->children[1];
514 SV_WriteEntitiesToClient
519 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
521 int e, clentnum, bits, alpha, glowcolor, glowsize, scale, effects;
522 int culled_pvs, culled_portal, culled_trace, visibleentities, totalentities;
524 vec3_t origin, angles, entmins, entmaxs, testorigin, testeye;
525 float nextfullupdate, alphaf;
528 entity_state_t *baseline; // LordHavoc: delta or startup baseline
532 Mod_CheckLoaded(sv.worldmodel);
534 // find the client's PVS
535 VectorAdd (clent->v.origin, clent->v.view_ofs, testeye);
536 pvs = SV_FatPVS (testeye);
544 clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
545 // send all entities that touch the pvs
546 ent = NEXT_EDICT(sv.edicts);
547 for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
551 // prevent delta compression against this frame (unless actually sent, which will restore this later)
552 nextfullupdate = client->nextfullupdate[e];
553 client->nextfullupdate[e] = -1;
555 if (ent != clent) // LordHavoc: always send player
557 if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
559 if (val->edict != clentnum)
561 // don't show to anyone else
565 bits |= U_VIEWMODEL; // show relative to the view
569 // LordHavoc: never draw something told not to display to this client
570 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
572 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
579 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
580 glowsize = (int) val->_float >> 2;
581 if (glowsize > 255) glowsize = 255;
582 if (glowsize < 0) glowsize = 0;
584 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
585 if (val->_float != 0)
588 if (ent->v.modelindex >= 0 && ent->v.modelindex < MAX_MODELS && pr_strings[ent->v.model])
590 model = sv.models[(int)ent->v.modelindex];
591 Mod_CheckLoaded(model);
596 if (ent != clent) // LordHavoc: always send player
597 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
601 VectorCopy(ent->v.angles, angles);
602 if (DotProduct(ent->v.velocity, ent->v.velocity) >= 1.0f)
604 VectorMA(ent->v.origin, host_client->latency, ent->v.velocity, origin);
605 // LordHavoc: trace predicted movement to avoid putting things in walls
606 trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, origin, MOVE_NORMAL, ent);
607 VectorCopy(trace.endpos, origin);
611 VectorCopy(ent->v.origin, origin);
614 // ent has survived every check so far, check if it is visible
615 if (ent != clent && ((bits & U_VIEWMODEL) == 0))
617 // use the predicted origin
618 entmins[0] = origin[0] - 1.0f;
619 entmins[1] = origin[1] - 1.0f;
620 entmins[2] = origin[2] - 1.0f;
621 entmaxs[0] = origin[0] + 1.0f;
622 entmaxs[1] = origin[1] + 1.0f;
623 entmaxs[2] = origin[2] + 1.0f;
624 // using the model's bounding box to ensure things are visible regardless of their physics box
627 if (ent->v.angles[0] || ent->v.angles[2]) // pitch and roll
629 VectorAdd(entmins, model->rotatedmins, entmins);
630 VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
632 else if (ent->v.angles[1])
634 VectorAdd(entmins, model->yawmins, entmins);
635 VectorAdd(entmaxs, model->yawmaxs, entmaxs);
639 VectorAdd(entmins, model->normalmins, entmins);
640 VectorAdd(entmaxs, model->normalmaxs, entmaxs);
646 // if not touching a visible leaf
647 if (sv_cullentities_pvs.integer && !SV_BoxTouchingPVS(pvs, entmins, entmaxs, sv.worldmodel->nodes))
653 // or not visible through the portals
654 if (sv_cullentities_portal.integer && !Portal_CheckBox(sv.worldmodel, testeye, entmins, entmaxs))
660 // don't try to cull embedded brush models with this, they're sometimes huge (spanning several rooms)
661 if (sv_cullentities_trace.integer && (model == NULL || model->type != mod_brush || model->name[0] != '*'))
663 // LordHavoc: test random offsets, to maximize chance of detection
664 testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
665 testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
666 testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
668 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
670 if (trace.fraction == 1)
671 client->visibletime[e] = realtime + 1;
674 //test nearest point on bbox
675 testorigin[0] = bound(entmins[0], testeye[0], entmaxs[0]);
676 testorigin[1] = bound(entmins[1], testeye[1], entmaxs[1]);
677 testorigin[2] = bound(entmins[2], testeye[2], entmaxs[2]);
679 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
681 if (trace.fraction == 1)
682 client->visibletime[e] = realtime + 1;
683 else if (realtime > client->visibletime[e])
696 effects = ent->v.effects;
698 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
699 if (val->_float != 0)
700 alphaf = val->_float * 255.0f;
703 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
704 if (val->_float != 0)
705 alphaf = val->_float;
709 alpha = bound(0, alphaf, 255);
711 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
712 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
713 if (scale < 0) scale = 0;
714 if (scale > 255) scale = 255;
716 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
717 if (val->_float != 0)
718 glowcolor = (int) val->_float;
720 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
721 if (val->_float != 0)
722 effects |= EF_FULLBRIGHT;
726 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
730 // don't send if flagged for NODRAW and there are no effects
731 if (model->flags == 0 && ((effects & EF_NODRAW) || scale <= 0 || alpha <= 0))
734 else // no model and no effects
739 if (msg->maxsize - msg->cursize < 32) // LordHavoc: increased check from 16 to 32
741 Con_Printf ("packet overflow\n");
742 // mark the rest of the entities so they can't be delta compressed against this frame
743 for (;e < sv.num_edicts;e++)
745 client->nextfullupdate[e] = -1;
746 client->visibletime[e] = -1;
751 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
752 bits = bits | U_EXTERIORMODEL;
755 baseline = &ent->baseline;
757 if (((int)ent->v.effects & EF_DELTA) && sv_deltacompress.integer)
759 // every half second a full update is forced
760 if (realtime < client->nextfullupdate[e])
763 baseline = &ent->deltabaseline;
766 nextfullupdate = realtime + 0.5f;
769 nextfullupdate = realtime + 0.5f;
771 // restore nextfullupdate since this is being sent for real
772 client->nextfullupdate[e] = nextfullupdate;
775 bits |= U_LONGENTITY;
777 if (ent->v.movetype == MOVETYPE_STEP)
780 // LordHavoc: old stuff, but rewritten to have more exact tolerances
781 if (origin[0] != baseline->origin[0]) bits |= U_ORIGIN1;
782 if (origin[1] != baseline->origin[1]) bits |= U_ORIGIN2;
783 if (origin[2] != baseline->origin[2]) bits |= U_ORIGIN3;
784 if (((int)(angles[0]*(256.0/360.0)) & 255) != ((int)(baseline->angles[0]*(256.0/360.0)) & 255)) bits |= U_ANGLE1;
785 if (((int)(angles[1]*(256.0/360.0)) & 255) != ((int)(baseline->angles[1]*(256.0/360.0)) & 255)) bits |= U_ANGLE2;
786 if (((int)(angles[2]*(256.0/360.0)) & 255) != ((int)(baseline->angles[2]*(256.0/360.0)) & 255)) bits |= U_ANGLE3;
787 if (baseline->colormap != (qbyte) ent->v.colormap) bits |= U_COLORMAP;
788 if (baseline->skin != (qbyte) ent->v.skin) bits |= U_SKIN;
789 if ((baseline->frame & 0x00FF) != ((int) ent->v.frame & 0x00FF)) bits |= U_FRAME;
790 if ((baseline->effects & 0x00FF) != ((int) ent->v.effects & 0x00FF)) bits |= U_EFFECTS;
791 if ((baseline->modelindex & 0x00FF) != ((int) ent->v.modelindex & 0x00FF)) bits |= U_MODEL;
793 // LordHavoc: new stuff
794 if (baseline->alpha != alpha) bits |= U_ALPHA;
795 if (baseline->scale != scale) bits |= U_SCALE;
796 if (((int) baseline->effects & 0xFF00) != ((int) ent->v.effects & 0xFF00)) bits |= U_EFFECTS2;
797 if (baseline->glowsize != glowsize) bits |= U_GLOWSIZE;
798 if (baseline->glowcolor != glowcolor) bits |= U_GLOWCOLOR;
799 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.frame & 0xFF00)) bits |= U_FRAME2;
800 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.modelindex & 0xFF00)) bits |= U_MODEL2;
802 // update delta baseline
803 VectorCopy(ent->v.origin, ent->deltabaseline.origin);
804 VectorCopy(ent->v.angles, ent->deltabaseline.angles);
805 ent->deltabaseline.colormap = ent->v.colormap;
806 ent->deltabaseline.skin = ent->v.skin;
807 ent->deltabaseline.frame = ent->v.frame;
808 ent->deltabaseline.effects = ent->v.effects;
809 ent->deltabaseline.modelindex = ent->v.modelindex;
810 ent->deltabaseline.alpha = alpha;
811 ent->deltabaseline.scale = scale;
812 ent->deltabaseline.glowsize = glowsize;
813 ent->deltabaseline.glowcolor = glowcolor;
816 if (bits >= 16777216)
824 MSG_WriteByte (msg, bits);
825 if (bits & U_MOREBITS)
826 MSG_WriteByte (msg, bits>>8);
827 // LordHavoc: extend bytes have to be written here due to delta compression
828 if (bits & U_EXTEND1)
829 MSG_WriteByte (msg, bits>>16);
830 if (bits & U_EXTEND2)
831 MSG_WriteByte (msg, bits>>24);
833 // LordHavoc: old stuff
834 if (bits & U_LONGENTITY)
835 MSG_WriteShort (msg,e);
837 MSG_WriteByte (msg,e);
838 if (bits & U_MODEL) MSG_WriteByte (msg, ent->v.modelindex);
839 if (bits & U_FRAME) MSG_WriteByte (msg, ent->v.frame);
840 if (bits & U_COLORMAP) MSG_WriteByte (msg, ent->v.colormap);
841 if (bits & U_SKIN) MSG_WriteByte (msg, ent->v.skin);
842 if (bits & U_EFFECTS) MSG_WriteByte (msg, ent->v.effects);
843 if (bits & U_ORIGIN1) MSG_WriteDPCoord (msg, origin[0]);
844 if (bits & U_ANGLE1) MSG_WriteAngle(msg, angles[0]);
845 if (bits & U_ORIGIN2) MSG_WriteDPCoord (msg, origin[1]);
846 if (bits & U_ANGLE2) MSG_WriteAngle(msg, angles[1]);
847 if (bits & U_ORIGIN3) MSG_WriteDPCoord (msg, origin[2]);
848 if (bits & U_ANGLE3) MSG_WriteAngle(msg, angles[2]);
850 // LordHavoc: new stuff
851 if (bits & U_ALPHA) MSG_WriteByte(msg, alpha);
852 if (bits & U_SCALE) MSG_WriteByte(msg, scale);
853 if (bits & U_EFFECTS2) MSG_WriteByte(msg, (int)ent->v.effects >> 8);
854 if (bits & U_GLOWSIZE) MSG_WriteByte(msg, glowsize);
855 if (bits & U_GLOWCOLOR) MSG_WriteByte(msg, glowcolor);
856 if (bits & U_FRAME2) MSG_WriteByte(msg, (int)ent->v.frame >> 8);
857 if (bits & U_MODEL2) MSG_WriteByte(msg, (int)ent->v.modelindex >> 8);
860 if (sv_cullentities_stats.integer)
861 Con_Printf("client \"%s\" entities: %d total, %d visible, %d culled by: %d pvs %d portal %d trace\n", client->name, totalentities, visibleentities, culled_pvs + culled_portal + culled_trace, culled_pvs, culled_portal, culled_trace);
864 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
866 int e, clentnum, flags, alpha, glowcolor, glowsize, scale, effects;
867 int culled_pvs, culled_portal, culled_trace, visibleentities, totalentities;
870 vec3_t origin, angles, entmins, entmaxs, testorigin, testeye;
875 entity_frame_t entityframe;
878 if (client->sendsignon)
881 Mod_CheckLoaded(sv.worldmodel);
883 // find the client's PVS
884 VectorAdd (clent->v.origin, clent->v.view_ofs, testeye);
885 pvs = SV_FatPVS (testeye);
886 EntityFrame_Clear(&entityframe, testeye);
894 clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
895 // send all entities that touch the pvs
896 ent = NEXT_EDICT(sv.edicts);
897 for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
901 if (ent != clent) // LordHavoc: always send player
903 if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
905 if (val->edict == clentnum)
906 flags |= RENDER_VIEWMODEL; // show relative to the view
909 // don't show to anyone else
915 // LordHavoc: never draw something told not to display to this client
916 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
918 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
925 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
926 glowsize = (int) val->_float >> 2;
927 glowsize = bound(0, glowsize, 255);
929 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
930 if (val->_float != 0)
931 flags |= RENDER_GLOWTRAIL;
933 if (ent->v.modelindex >= 0 && ent->v.modelindex < MAX_MODELS && pr_strings[ent->v.model])
935 model = sv.models[(int)ent->v.modelindex];
936 Mod_CheckLoaded(model);
941 if (ent != clent) // LordHavoc: always send player
942 if (glowsize == 0 && (flags & RENDER_GLOWTRAIL) == 0) // no effects
946 VectorCopy(ent->v.angles, angles);
947 if (DotProduct(ent->v.velocity, ent->v.velocity) >= 1.0f && host_client->latency >= 0.01f)
949 VectorMA(ent->v.origin, host_client->latency, ent->v.velocity, origin);
950 // LordHavoc: trace predicted movement to avoid putting things in walls
951 trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, origin, MOVE_NORMAL, ent);
952 VectorCopy(trace.endpos, origin);
956 VectorCopy(ent->v.origin, origin);
959 // ent has survived every check so far, check if it is visible
960 // always send embedded brush models, they don't generate much traffic
961 if (ent != clent && ((flags & RENDER_VIEWMODEL) == 0) && (model == NULL || model->type != mod_brush || model->name[0] != '*'))
963 // use the predicted origin
964 entmins[0] = origin[0] - 1.0f;
965 entmins[1] = origin[1] - 1.0f;
966 entmins[2] = origin[2] - 1.0f;
967 entmaxs[0] = origin[0] + 1.0f;
968 entmaxs[1] = origin[1] + 1.0f;
969 entmaxs[2] = origin[2] + 1.0f;
970 // using the model's bounding box to ensure things are visible regardless of their physics box
973 if (ent->v.angles[0] || ent->v.angles[2]) // pitch and roll
975 VectorAdd(entmins, model->rotatedmins, entmins);
976 VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
978 else if (ent->v.angles[1])
980 VectorAdd(entmins, model->yawmins, entmins);
981 VectorAdd(entmaxs, model->yawmaxs, entmaxs);
985 VectorAdd(entmins, model->normalmins, entmins);
986 VectorAdd(entmaxs, model->normalmaxs, entmaxs);
992 // if not touching a visible leaf
993 if (sv_cullentities_pvs.integer && !SV_BoxTouchingPVS(pvs, entmins, entmaxs, sv.worldmodel->nodes))
999 // or not visible through the portals
1000 if (sv_cullentities_portal.integer && !Portal_CheckBox(sv.worldmodel, testeye, entmins, entmaxs))
1006 if (sv_cullentities_trace.integer)
1008 // LordHavoc: test random offsets, to maximize chance of detection
1009 testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
1010 testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
1011 testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
1013 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
1015 if (trace.fraction == 1)
1016 client->visibletime[e] = realtime + 1;
1019 if (realtime > client->visibletime[e])
1032 effects = ent->v.effects;
1034 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
1035 if (val->_float != 0)
1036 alphaf = val->_float * 255.0;
1039 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
1040 if (val->_float != 0)
1041 alphaf = val->_float;
1045 alpha = bound(0, alphaf, 255);
1047 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
1048 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
1049 if (scale < 0) scale = 0;
1050 if (scale > 255) scale = 255;
1052 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
1053 if (val->_float != 0)
1054 glowcolor = (int) val->_float;
1056 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
1057 if (val->_float != 0)
1058 effects |= EF_FULLBRIGHT;
1062 if (glowsize == 0 && (flags & RENDER_GLOWTRAIL) == 0) // no effects
1066 // don't send if flagged for NODRAW and there are no effects
1067 if (model->flags == 0 && ((effects & EF_NODRAW) || scale <= 0 || alpha <= 0))
1070 else // no model and no effects
1075 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
1076 flags |= RENDER_EXTERIORMODEL;
1078 if (ent->v.movetype == MOVETYPE_STEP)
1079 flags |= RENDER_STEP;
1080 // don't send an entity if it's coordinates would wrap around
1081 if ((effects & EF_LOWPRECISION) && origin[0] >= -32768 && origin[1] >= -32768 && origin[2] >= -32768 && origin[0] <= 32767 && origin[1] <= 32767 && origin[2] <= 32767)
1082 flags |= RENDER_LOWPRECISION;
1084 s = EntityFrame_NewEntity(&entityframe, e);
1085 // if we run out of space, abort
1088 VectorCopy(origin, s->origin);
1089 VectorCopy(angles, s->angles);
1090 s->colormap = ent->v.colormap;
1091 s->skin = ent->v.skin;
1092 s->frame = ent->v.frame;
1093 s->modelindex = ent->v.modelindex;
1094 s->effects = effects;
1097 s->glowsize = glowsize;
1098 s->glowcolor = glowcolor;
1101 entityframe.framenum = ++client->entityframenumber;
1102 EntityFrame_Write(&client->entitydatabase, &entityframe, msg);
1104 if (sv_cullentities_stats.integer)
1105 Con_Printf("client \"%s\" entities: %d total, %d visible, %d culled by: %d pvs %d portal %d trace\n", client->name, totalentities, visibleentities, culled_pvs + culled_portal + culled_trace, culled_pvs, culled_portal, culled_trace);
1115 void SV_CleanupEnts (void)
1120 ent = NEXT_EDICT(sv.edicts);
1121 for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
1122 ent->v.effects = (int)ent->v.effects & ~EF_MUZZLEFLASH;
1127 SV_WriteClientdataToMessage
1131 void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
1142 // send a damage message
1144 if (ent->v.dmg_take || ent->v.dmg_save)
1146 other = PROG_TO_EDICT(ent->v.dmg_inflictor);
1147 MSG_WriteByte (msg, svc_damage);
1148 MSG_WriteByte (msg, ent->v.dmg_save);
1149 MSG_WriteByte (msg, ent->v.dmg_take);
1150 for (i=0 ; i<3 ; i++)
1151 MSG_WriteDPCoord (msg, other->v.origin[i] + 0.5*(other->v.mins[i] + other->v.maxs[i]));
1153 ent->v.dmg_take = 0;
1154 ent->v.dmg_save = 0;
1158 // send the current viewpos offset from the view entity
1160 SV_SetIdealPitch (); // how much to look up / down ideally
1162 // a fixangle might get lost in a dropped packet. Oh well.
1163 if ( ent->v.fixangle )
1165 MSG_WriteByte (msg, svc_setangle);
1166 for (i=0 ; i < 3 ; i++)
1167 MSG_WriteAngle (msg, ent->v.angles[i] );
1168 ent->v.fixangle = 0;
1173 bits |= SU_VIEWHEIGHT;
1175 if (ent->v.idealpitch)
1176 bits |= SU_IDEALPITCH;
1178 // stuff the sigil bits into the high bits of items for sbar, or else
1180 val = GETEDICTFIELDVALUE(ent, eval_items2);
1183 items = (int)ent->v.items | ((int)val->_float << 23);
1185 items = (int)ent->v.items | ((int)pr_global_struct->serverflags << 28);
1189 if ( (int)ent->v.flags & FL_ONGROUND)
1190 bits |= SU_ONGROUND;
1192 if ( ent->v.waterlevel >= 2)
1196 VectorClear(punchvector);
1197 if ((val = GETEDICTFIELDVALUE(ent, eval_punchvector)))
1198 VectorCopy(val->vector, punchvector);
1201 if ((val = GETEDICTFIELDVALUE(ent, eval_viewzoom)))
1203 i = val->_float * 255.0f;
1207 i = bound(0, i, 255);
1211 if (viewzoom != 255)
1212 bits |= SU_VIEWZOOM;
1214 for (i=0 ; i<3 ; i++)
1216 if (ent->v.punchangle[i])
1217 bits |= (SU_PUNCH1<<i);
1218 if (punchvector[i]) // dpprotocol
1219 bits |= (SU_PUNCHVEC1<<i); // dpprotocol
1220 if (ent->v.velocity[i])
1221 bits |= (SU_VELOCITY1<<i);
1224 if (ent->v.weaponframe)
1225 bits |= SU_WEAPONFRAME;
1227 if (ent->v.armorvalue)
1234 if (bits >= 16777216)
1239 MSG_WriteByte (msg, svc_clientdata);
1240 MSG_WriteShort (msg, bits);
1241 if (bits & SU_EXTEND1)
1242 MSG_WriteByte(msg, bits >> 16);
1243 if (bits & SU_EXTEND2)
1244 MSG_WriteByte(msg, bits >> 24);
1246 if (bits & SU_VIEWHEIGHT)
1247 //MSG_WriteChar (msg, ent->v.view_ofs[2]);
1248 MSG_WriteChar (msg, 0);
1250 if (bits & SU_IDEALPITCH)
1251 MSG_WriteChar (msg, ent->v.idealpitch);
1253 for (i=0 ; i<3 ; i++)
1255 if (bits & (SU_PUNCH1<<i))
1256 MSG_WritePreciseAngle(msg, ent->v.punchangle[i]); // dpprotocol
1257 if (bits & (SU_PUNCHVEC1<<i)) // dpprotocol
1258 MSG_WriteDPCoord(msg, punchvector[i]); // dpprotocol
1259 if (bits & (SU_VELOCITY1<<i))
1260 MSG_WriteChar (msg, ent->v.velocity[i]/16);
1263 // [always sent] if (bits & SU_ITEMS)
1264 MSG_WriteLong (msg, items);
1266 if (bits & SU_WEAPONFRAME)
1267 MSG_WriteByte (msg, ent->v.weaponframe);
1268 if (bits & SU_ARMOR)
1269 MSG_WriteByte (msg, ent->v.armorvalue);
1270 if (bits & SU_WEAPON)
1271 MSG_WriteByte (msg, SV_ModelIndex(pr_strings+ent->v.weaponmodel));
1273 MSG_WriteShort (msg, ent->v.health);
1274 MSG_WriteByte (msg, ent->v.currentammo);
1275 MSG_WriteByte (msg, ent->v.ammo_shells);
1276 MSG_WriteByte (msg, ent->v.ammo_nails);
1277 MSG_WriteByte (msg, ent->v.ammo_rockets);
1278 MSG_WriteByte (msg, ent->v.ammo_cells);
1280 if (gamemode == GAME_HIPNOTIC || gamemode == GAME_ROGUE)
1284 if ( ((int)ent->v.weapon) & (1<<i) )
1286 MSG_WriteByte (msg, i);
1293 MSG_WriteByte (msg, ent->v.weapon);
1296 if (bits & SU_VIEWZOOM)
1297 MSG_WriteByte (msg, viewzoom);
1301 =======================
1302 SV_SendClientDatagram
1303 =======================
1305 qboolean SV_SendClientDatagram (client_t *client)
1307 qbyte buf[MAX_DATAGRAM];
1311 msg.maxsize = sizeof(buf);
1314 MSG_WriteByte (&msg, svc_time);
1315 MSG_WriteFloat (&msg, sv.time);
1317 if (client->spawned)
1319 // add the client specific data to the datagram
1320 SV_WriteClientdataToMessage (client->edict, &msg);
1322 SV_WriteEntitiesToClient (client, client->edict, &msg);
1324 // copy the server datagram if there is space
1325 if (msg.cursize + sv.datagram.cursize < msg.maxsize)
1326 SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
1329 // send the datagram
1330 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1332 SV_DropClient (true);// if the message couldn't send, kick off
1340 =======================
1341 SV_UpdateToReliableMessages
1342 =======================
1344 void SV_UpdateToReliableMessages (void)
1349 // check for changes to be sent over the reliable streams
1350 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1352 if (host_client->old_frags != host_client->edict->v.frags)
1354 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1356 if (!client->active || !client->spawned)
1358 MSG_WriteByte (&client->message, svc_updatefrags);
1359 MSG_WriteByte (&client->message, i);
1360 MSG_WriteShort (&client->message, host_client->edict->v.frags);
1363 host_client->old_frags = host_client->edict->v.frags;
1367 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1369 if (!client->active)
1371 SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
1374 SZ_Clear (&sv.reliable_datagram);
1379 =======================
1382 Send a nop message without trashing or sending the accumulated client
1384 =======================
1386 void SV_SendNop (client_t *client)
1392 msg.maxsize = sizeof(buf);
1395 MSG_WriteChar (&msg, svc_nop);
1397 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1398 SV_DropClient (true); // if the message couldn't send, kick off
1399 client->last_message = realtime;
1403 =======================
1404 SV_SendClientMessages
1405 =======================
1407 void SV_SendClientMessages (void)
1411 // update frags, names, etc
1412 SV_UpdateToReliableMessages ();
1414 // build individual updates
1415 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1417 if (!host_client->active)
1420 #ifndef NOROUTINGFIX
1421 if (host_client->sendserverinfo)
1423 host_client->sendserverinfo = false;
1424 SV_SendServerinfo (host_client);
1428 if (host_client->spawned)
1430 if (!SV_SendClientDatagram (host_client))
1435 // the player isn't totally in the game yet
1436 // send small keepalive messages if too much time has passed
1437 // send a full message when the next signon stage has been requested
1438 // some other message data (name changes, etc) may accumulate
1439 // between signon stages
1440 if (!host_client->sendsignon)
1442 if (realtime - host_client->last_message > 5)
1443 SV_SendNop (host_client);
1444 continue; // don't send out non-signon messages
1448 // check for an overflowed message. Should only happen
1449 // on a very fucked up connection that backs up a lot, then
1451 if (host_client->message.overflowed)
1453 SV_DropClient (true);
1454 host_client->message.overflowed = false;
1458 if (host_client->message.cursize || host_client->dropasap)
1460 if (!NET_CanSendMessage (host_client->netconnection))
1463 if (host_client->dropasap)
1464 SV_DropClient (false); // went to another level
1467 if (NET_SendMessage (host_client->netconnection, &host_client->message) == -1)
1468 SV_DropClient (true); // if the message couldn't send, kick off
1469 SZ_Clear (&host_client->message);
1470 host_client->last_message = realtime;
1471 host_client->sendsignon = false;
1477 // clear muzzle flashes
1483 ==============================================================================
1487 ==============================================================================
1496 int SV_ModelIndex (char *name)
1500 if (!name || !name[0])
1503 for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
1504 if (!strcmp(sv.model_precache[i], name))
1506 if (i==MAX_MODELS || !sv.model_precache[i])
1507 Host_Error ("SV_ModelIndex: model %s not precached", name);
1511 #ifdef SV_QUAKEENTITIES
1518 void SV_CreateBaseline (void)
1520 int i, entnum, large;
1523 // LordHavoc: clear *all* states (note just active ones)
1524 for (entnum = 0; entnum < MAX_EDICTS ; entnum++)
1526 // get the current server version
1527 svent = EDICT_NUM(entnum);
1529 // LordHavoc: always clear state values, whether the entity is in use or not
1530 ClearStateToDefault(&svent->baseline);
1534 if (entnum > svs.maxclients && !svent->v.modelindex)
1537 // create entity baseline
1538 VectorCopy (svent->v.origin, svent->baseline.origin);
1539 VectorCopy (svent->v.angles, svent->baseline.angles);
1540 svent->baseline.frame = svent->v.frame;
1541 svent->baseline.skin = svent->v.skin;
1542 if (entnum > 0 && entnum <= svs.maxclients)
1544 svent->baseline.colormap = entnum;
1545 svent->baseline.modelindex = SV_ModelIndex("progs/player.mdl");
1549 svent->baseline.colormap = 0;
1550 svent->baseline.modelindex = svent->v.modelindex;
1554 if (svent->baseline.modelindex & 0xFF00 || svent->baseline.frame & 0xFF00)
1557 // add to the message
1559 MSG_WriteByte (&sv.signon, svc_spawnbaseline2);
1561 MSG_WriteByte (&sv.signon, svc_spawnbaseline);
1562 MSG_WriteShort (&sv.signon, entnum);
1566 MSG_WriteShort (&sv.signon, svent->baseline.modelindex);
1567 MSG_WriteShort (&sv.signon, svent->baseline.frame);
1571 MSG_WriteByte (&sv.signon, svent->baseline.modelindex);
1572 MSG_WriteByte (&sv.signon, svent->baseline.frame);
1574 MSG_WriteByte (&sv.signon, svent->baseline.colormap);
1575 MSG_WriteByte (&sv.signon, svent->baseline.skin);
1576 for (i=0 ; i<3 ; i++)
1578 MSG_WriteDPCoord(&sv.signon, svent->baseline.origin[i]);
1579 MSG_WriteAngle(&sv.signon, svent->baseline.angles[i]);
1590 Tell all the clients that the server is changing levels
1593 void SV_SendReconnect (void)
1600 msg.maxsize = sizeof(data);
1602 MSG_WriteChar (&msg, svc_stufftext);
1603 MSG_WriteString (&msg, "reconnect\n");
1604 NET_SendToAll (&msg, 5);
1606 if (cls.state != ca_dedicated)
1607 Cmd_ExecuteString ("reconnect\n", src_command);
1615 Grabs the current state of each client for saving across the
1616 transition to another level
1619 void SV_SaveSpawnparms (void)
1623 svs.serverflags = pr_global_struct->serverflags;
1625 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1627 if (!host_client->active)
1630 // call the progs to get default spawn parms for the new client
1631 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1632 PR_ExecuteProgram (pr_global_struct->SetChangeParms, "QC function SetChangeParms is missing");
1633 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1634 host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1642 This is called at the start of each level
1645 extern float scr_centertime_off;
1647 void SV_SpawnServer (char *server)
1652 // let's not have any servers with no name
1653 if (hostname.string[0] == 0)
1654 Cvar_Set ("hostname", "UNNAMED");
1655 scr_centertime_off = 0;
1657 Con_DPrintf ("SpawnServer: %s\n",server);
1658 svs.changelevel_issued = false; // now safe to issue another
1661 // tell all connected clients that we are going to a new level
1664 SV_SendReconnect ();
1667 // make cvars consistant
1670 Cvar_SetValue ("deathmatch", 0);
1671 current_skill = bound(0, (int)(skill.value + 0.5), 3);
1673 Cvar_SetValue ("skill", (float)current_skill);
1676 // set up the new server
1678 Host_ClearMemory ();
1680 memset (&sv, 0, sizeof(sv));
1682 strcpy (sv.name, server);
1684 // load progs to get entity field count
1687 // allocate server memory
1688 sv.max_edicts = MAX_EDICTS;
1690 // clear the edict memory pool
1691 Mem_EmptyPool(sv_edicts_mempool);
1692 sv.edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1694 sv.datagram.maxsize = sizeof(sv.datagram_buf);
1695 sv.datagram.cursize = 0;
1696 sv.datagram.data = sv.datagram_buf;
1698 sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1699 sv.reliable_datagram.cursize = 0;
1700 sv.reliable_datagram.data = sv.reliable_datagram_buf;
1702 sv.signon.maxsize = sizeof(sv.signon_buf);
1703 sv.signon.cursize = 0;
1704 sv.signon.data = sv.signon_buf;
1706 // leave slots at start for clients only
1707 sv.num_edicts = svs.maxclients+1;
1708 for (i=0 ; i<svs.maxclients ; i++)
1710 ent = EDICT_NUM(i+1);
1711 svs.clients[i].edict = ent;
1714 sv.state = ss_loading;
1721 strcpy (sv.name, server);
1722 sprintf (sv.modelname,"maps/%s.bsp", server);
1723 sv.worldmodel = Mod_ForName(sv.modelname, false, true, true);
1726 Con_Printf ("Couldn't spawn server %s\n", sv.modelname);
1730 sv.models[1] = sv.worldmodel;
1733 // clear world interaction links
1737 sv.sound_precache[0] = pr_strings;
1739 sv.model_precache[0] = pr_strings;
1740 sv.model_precache[1] = sv.modelname;
1741 for (i = 1;i < sv.worldmodel->numsubmodels;i++)
1743 sv.model_precache[i+1] = localmodels[i];
1744 sv.models[i+1] = Mod_ForName (localmodels[i], false, false, false);
1748 // load the rest of the entities
1751 memset (&ent->v, 0, progs->entityfields * 4);
1753 ent->v.model = sv.worldmodel->name - pr_strings;
1754 ent->v.modelindex = 1; // world model
1755 ent->v.solid = SOLID_BSP;
1756 ent->v.movetype = MOVETYPE_PUSH;
1759 pr_global_struct->coop = coop.integer;
1761 pr_global_struct->deathmatch = deathmatch.integer;
1763 pr_global_struct->mapname = sv.name - pr_strings;
1765 // serverflags are for cross level information (sigils)
1766 pr_global_struct->serverflags = svs.serverflags;
1768 ED_LoadFromFile (sv.worldmodel->entities);
1769 // LordHavoc: clear world angles (to fix e3m3.bsp)
1770 VectorClear(sv.edicts->v.angles);
1774 // all setup is completed, any further precache statements are errors
1775 sv.state = ss_active;
1777 // run two frames to allow everything to settle
1778 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1780 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1785 #ifdef QUAKEENTITIES
1786 // create a baseline for more efficient communications
1787 SV_CreateBaseline ();
1790 // send serverinfo to all connected clients
1791 for (i=0,host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1792 if (host_client->active)
1793 SV_SendServerinfo (host_client);
1795 Con_DPrintf ("Server spawned.\n");