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 //============================================================================
47 Cvar_RegisterVariable (&sv_maxvelocity);
48 Cvar_RegisterVariable (&sv_gravity);
49 Cvar_RegisterVariable (&sv_friction);
50 Cvar_RegisterVariable (&sv_edgefriction);
51 Cvar_RegisterVariable (&sv_stopspeed);
52 Cvar_RegisterVariable (&sv_maxspeed);
53 Cvar_RegisterVariable (&sv_accelerate);
54 Cvar_RegisterVariable (&sv_idealpitchscale);
55 Cvar_RegisterVariable (&sv_aim);
56 Cvar_RegisterVariable (&sv_nostep);
57 Cvar_RegisterVariable (&sv_predict);
58 Cvar_RegisterVariable (&sv_deltacompress);
59 Cvar_RegisterVariable (&sv_cullentities_pvs);
60 Cvar_RegisterVariable (&sv_cullentities_portal);
61 Cvar_RegisterVariable (&sv_cullentities_trace);
62 Cvar_RegisterVariable (&sv_cullentities_stats);
64 for (i = 0;i < MAX_MODELS;i++)
65 sprintf (localmodels[i], "*%i", i);
67 sv_edicts_mempool = Mem_AllocPool("edicts");
71 =============================================================================
75 =============================================================================
82 Make sure the event gets sent to all clients
85 void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
89 if (sv.datagram.cursize > MAX_DATAGRAM-16)
91 MSG_WriteByte (&sv.datagram, svc_particle);
92 MSG_WriteDPCoord (&sv.datagram, org[0]);
93 MSG_WriteDPCoord (&sv.datagram, org[1]);
94 MSG_WriteDPCoord (&sv.datagram, org[2]);
102 MSG_WriteChar (&sv.datagram, v);
104 MSG_WriteByte (&sv.datagram, count);
105 MSG_WriteByte (&sv.datagram, color);
112 Make sure the event gets sent to all clients
115 void SV_StartEffect (vec3_t org, int modelindex, int startframe, int framecount, int framerate)
117 if (sv.datagram.cursize > MAX_DATAGRAM-18)
119 if (modelindex >= 256 || startframe >= 256)
121 MSG_WriteByte (&sv.datagram, svc_effect2);
122 MSG_WriteDPCoord (&sv.datagram, org[0]);
123 MSG_WriteDPCoord (&sv.datagram, org[1]);
124 MSG_WriteDPCoord (&sv.datagram, org[2]);
125 MSG_WriteShort (&sv.datagram, modelindex);
126 MSG_WriteShort (&sv.datagram, startframe);
127 MSG_WriteByte (&sv.datagram, framecount);
128 MSG_WriteByte (&sv.datagram, framerate);
132 MSG_WriteByte (&sv.datagram, svc_effect);
133 MSG_WriteDPCoord (&sv.datagram, org[0]);
134 MSG_WriteDPCoord (&sv.datagram, org[1]);
135 MSG_WriteDPCoord (&sv.datagram, org[2]);
136 MSG_WriteByte (&sv.datagram, modelindex);
137 MSG_WriteByte (&sv.datagram, startframe);
138 MSG_WriteByte (&sv.datagram, framecount);
139 MSG_WriteByte (&sv.datagram, framerate);
147 Each entity can have eight independant sound sources, like voice,
150 Channel 0 is an auto-allocate channel, the others override anything
151 already running on that entity/channel pair.
153 An attenuation of 0 will play full volume everywhere in the level.
154 Larger attenuations will drop off. (max 4 attenuation)
158 void SV_StartSound (edict_t *entity, int channel, char *sample, int volume,
166 if (volume < 0 || volume > 255)
167 Host_Error ("SV_StartSound: volume = %i", volume);
169 if (attenuation < 0 || attenuation > 4)
170 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
172 if (channel < 0 || channel > 7)
173 Host_Error ("SV_StartSound: channel = %i", channel);
175 if (sv.datagram.cursize > MAX_DATAGRAM-16)
178 // find precache number for sound
179 for (sound_num=1 ; sound_num<MAX_SOUNDS
180 && sv.sound_precache[sound_num] ; sound_num++)
181 if (!strcmp(sample, sv.sound_precache[sound_num]))
184 if ( sound_num == MAX_SOUNDS || !sv.sound_precache[sound_num] )
186 Con_Printf ("SV_StartSound: %s not precached\n", sample);
190 ent = NUM_FOR_EDICT(entity);
192 channel = (ent<<3) | channel;
195 if (volume != DEFAULT_SOUND_PACKET_VOLUME)
196 field_mask |= SND_VOLUME;
197 if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
198 field_mask |= SND_ATTENUATION;
200 // directed messages go only to the entity they are targeted on
201 if (sound_num >= 256)
202 MSG_WriteByte (&sv.datagram, svc_sound2);
204 MSG_WriteByte (&sv.datagram, svc_sound);
205 MSG_WriteByte (&sv.datagram, field_mask);
206 if (field_mask & SND_VOLUME)
207 MSG_WriteByte (&sv.datagram, volume);
208 if (field_mask & SND_ATTENUATION)
209 MSG_WriteByte (&sv.datagram, attenuation*64);
210 MSG_WriteShort (&sv.datagram, channel);
211 if (sound_num >= 256)
212 MSG_WriteShort (&sv.datagram, sound_num);
214 MSG_WriteByte (&sv.datagram, sound_num);
215 for (i=0 ; i<3 ; i++)
216 MSG_WriteDPCoord (&sv.datagram, entity->v.origin[i]+0.5*(entity->v.mins[i]+entity->v.maxs[i]));
220 ==============================================================================
224 ==============================================================================
231 Sends the first message from the server to a connected client.
232 This will be sent on the initial connection and upon each server load.
235 void SV_SendServerinfo (client_t *client)
240 // LordHavoc: clear entityframe tracking
241 client->entityframenumber = 0;
242 EntityFrame_ClearDatabase(&client->entitydatabase);
244 MSG_WriteByte (&client->message, svc_print);
245 sprintf (message, "\002\nServer: %s build %s (progs %i crc)", gamename, buildstring, pr_crc);
246 MSG_WriteString (&client->message,message);
248 MSG_WriteByte (&client->message, svc_serverinfo);
249 MSG_WriteLong (&client->message, DPPROTOCOL_VERSION2);
250 MSG_WriteByte (&client->message, svs.maxclients);
252 if (!coop.integer && deathmatch.integer)
253 MSG_WriteByte (&client->message, GAME_DEATHMATCH);
255 MSG_WriteByte (&client->message, GAME_COOP);
257 sprintf (message, pr_strings+sv.edicts->v.message);
259 MSG_WriteString (&client->message,message);
261 for (s = sv.model_precache+1 ; *s ; s++)
262 MSG_WriteString (&client->message, *s);
263 MSG_WriteByte (&client->message, 0);
265 for (s = sv.sound_precache+1 ; *s ; s++)
266 MSG_WriteString (&client->message, *s);
267 MSG_WriteByte (&client->message, 0);
270 MSG_WriteByte (&client->message, svc_cdtrack);
271 MSG_WriteByte (&client->message, sv.edicts->v.sounds);
272 MSG_WriteByte (&client->message, sv.edicts->v.sounds);
275 MSG_WriteByte (&client->message, svc_setview);
276 MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
278 MSG_WriteByte (&client->message, svc_signonnum);
279 MSG_WriteByte (&client->message, 1);
281 client->sendsignon = true;
282 client->spawned = false; // need prespawn, spawn, etc
289 Initializes a client_t for a new net connection. This will only be called
290 once for a player each game, not once for each level change.
293 void SV_ConnectClient (int clientnum)
298 struct qsocket_s *netconnection;
300 float spawn_parms[NUM_SPAWN_PARMS];
302 client = svs.clients + clientnum;
304 Con_DPrintf ("Client %s connected\n", client->netconnection->address);
306 edictnum = clientnum+1;
308 ent = EDICT_NUM(edictnum);
310 // set up the client_t
311 netconnection = client->netconnection;
314 memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
315 memset (client, 0, sizeof(*client));
316 client->netconnection = netconnection;
318 strcpy (client->name, "unconnected");
319 client->active = true;
320 client->spawned = false;
322 client->message.data = client->msgbuf;
323 client->message.maxsize = sizeof(client->msgbuf);
324 client->message.allowoverflow = true; // we can catch it
327 memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
330 // call the progs to get default spawn parms for the new client
331 PR_ExecuteProgram (pr_global_struct->SetNewParms, "QC function SetNewParms is missing");
332 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
333 client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
336 // send serverinfo on first nop
337 client->waitingforconnect = true;
338 client->sendsignon = true;
339 client->spawned = false; // need prespawn, spawn, etc
345 SV_CheckForNewClients
349 void SV_CheckForNewClients (void)
351 struct qsocket_s *ret;
355 // check for new connections
359 ret = NET_CheckNewConnections ();
364 // init a new client structure
366 for (i=0 ; i<svs.maxclients ; i++)
367 if (!svs.clients[i].active)
369 if (i == svs.maxclients)
370 Sys_Error ("Host_CheckForNewClients: no free clients");
372 svs.clients[i].netconnection = ret;
373 SV_ConnectClient (i);
375 net_activeconnections++;
382 ===============================================================================
386 ===============================================================================
395 void SV_ClearDatagram (void)
397 SZ_Clear (&sv.datagram);
401 =============================================================================
403 The PVS must include a small area around the client to allow head bobbing
404 or other small motion on the client side. Otherwise, a bob might cause an
405 entity that should be visible to not show up, especially when the bob
408 =============================================================================
412 qbyte fatpvs[MAX_MAP_LEAFS/8];
414 void SV_AddToFatPVS (vec3_t org, mnode_t *node)
423 // if this is a leaf, accumulate the pvs bits
424 if (node->contents < 0)
426 if (node->contents != CONTENTS_SOLID)
428 pvs = Mod_LeafPVS ( (mleaf_t *)node, sv.worldmodel);
429 for (i=0 ; i<fatbytes ; i++)
436 d = DotProduct (org, plane->normal) - plane->dist;
438 node = node->children[0];
440 node = node->children[1];
443 SV_AddToFatPVS (org, node->children[0]);
444 node = node->children[1];
453 Calculates a PVS that is the inclusive or of all leafs within 8 pixels of the
457 qbyte *SV_FatPVS (vec3_t org)
459 fatbytes = (sv.worldmodel->numleafs+31)>>3;
460 memset (fatpvs, 0, fatbytes);
461 SV_AddToFatPVS (org, sv.worldmodel->nodes);
465 //=============================================================================
468 int SV_BoxTouchingPVS (qbyte *pvs, vec3_t mins, vec3_t maxs, mnode_t *node)
472 if (node->contents < 0)
475 if (node->contents == CONTENTS_SOLID)
477 leafnum = (mleaf_t *)node - sv.worldmodel->leafs - 1;
478 return pvs[leafnum >> 3] & (1 << (leafnum & 7));
481 // node - recurse down the BSP tree
482 switch (BOX_ON_PLANE_SIDE(mins, maxs, node->plane))
485 node = node->children[0];
488 node = node->children[1];
491 if (node->children[0]->contents != CONTENTS_SOLID)
492 if (SV_BoxTouchingPVS (pvs, mins, maxs, node->children[0]))
494 node = node->children[1];
504 SV_WriteEntitiesToClient
509 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
511 int e, clentnum, bits, alpha, glowcolor, glowsize, scale, effects;
512 int culled_pvs, culled_portal, culled_trace, visibleentities, totalentities;
514 vec3_t org, origin, angles, entmins, entmaxs;
515 float nextfullupdate;
518 entity_state_t *baseline; // LordHavoc: delta or startup baseline
522 double testorigin[3];
524 Mod_CheckLoaded(sv.worldmodel);
526 // find the client's PVS
527 VectorAdd (clent->v.origin, clent->v.view_ofs, org);
528 VectorCopy (org, testeye);
529 pvs = SV_FatPVS (org);
532 MSG_WriteByte(msg, svc_playerposition);
533 MSG_WriteFloat(msg, org[0]);
534 MSG_WriteFloat(msg, org[1]);
535 MSG_WriteFloat(msg, org[2]);
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, org, 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 memset (&trace, 0, sizeof(trace_t));
670 trace.allsolid = true;
671 VectorCopy(testorigin, trace.endpos);
673 VectorCopy(org, RecursiveHullCheckInfo.start);
674 VectorSubtract(testorigin, testeye, RecursiveHullCheckInfo.dist);
675 RecursiveHullCheckInfo.hull = sv.worldmodel->hulls;
676 RecursiveHullCheckInfo.trace = &trace;
677 SV_RecursiveHullCheck (sv.worldmodel->hulls->firstclipnode, 0, 1, testeye, testorigin);
679 if (trace.fraction == 1)
680 client->visibletime[e] = realtime + 1;
683 if (realtime > client->visibletime[e])
696 effects = ent->v.effects;
698 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
699 if (val->_float != 0)
700 alpha = (int) (val->_float * 255.0);
703 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
704 if (val->_float != 0)
705 alpha = (int) val->_float;
709 alpha = bound(0, alpha, 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 ((int)(origin[0]*8.0) != (int)(baseline->origin[0]*8.0)) bits |= U_ORIGIN1;
782 // if ((int)(origin[1]*8.0) != (int)(baseline->origin[1]*8.0)) bits |= U_ORIGIN2;
783 // if ((int)(origin[2]*8.0) != (int)(baseline->origin[2]*8.0)) bits |= U_ORIGIN3;
784 if (origin[0] != baseline->origin[0]) bits |= U_ORIGIN1;
785 if (origin[1] != baseline->origin[1]) bits |= U_ORIGIN2;
786 if (origin[2] != baseline->origin[2]) bits |= U_ORIGIN3;
787 if (((int)(angles[0]*(256.0/360.0)) & 255) != ((int)(baseline->angles[0]*(256.0/360.0)) & 255)) bits |= U_ANGLE1;
788 if (((int)(angles[1]*(256.0/360.0)) & 255) != ((int)(baseline->angles[1]*(256.0/360.0)) & 255)) bits |= U_ANGLE2;
789 if (((int)(angles[2]*(256.0/360.0)) & 255) != ((int)(baseline->angles[2]*(256.0/360.0)) & 255)) bits |= U_ANGLE3;
790 if (baseline->colormap != (qbyte) ent->v.colormap) bits |= U_COLORMAP;
791 if (baseline->skin != (qbyte) ent->v.skin) bits |= U_SKIN;
792 if ((baseline->frame & 0x00FF) != ((int) ent->v.frame & 0x00FF)) bits |= U_FRAME;
793 if ((baseline->effects & 0x00FF) != ((int) ent->v.effects & 0x00FF)) bits |= U_EFFECTS;
794 if ((baseline->modelindex & 0x00FF) != ((int) ent->v.modelindex & 0x00FF)) bits |= U_MODEL;
796 // LordHavoc: new stuff
797 if (baseline->alpha != alpha) bits |= U_ALPHA;
798 if (baseline->scale != scale) bits |= U_SCALE;
799 if (((int) baseline->effects & 0xFF00) != ((int) ent->v.effects & 0xFF00)) bits |= U_EFFECTS2;
800 if (baseline->glowsize != glowsize) bits |= U_GLOWSIZE;
801 if (baseline->glowcolor != glowcolor) bits |= U_GLOWCOLOR;
802 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.frame & 0xFF00)) bits |= U_FRAME2;
803 if (((int) baseline->frame & 0xFF00) != ((int) ent->v.modelindex & 0xFF00)) bits |= U_MODEL2;
805 // update delta baseline
806 VectorCopy(ent->v.origin, ent->deltabaseline.origin);
807 VectorCopy(ent->v.angles, ent->deltabaseline.angles);
808 ent->deltabaseline.colormap = ent->v.colormap;
809 ent->deltabaseline.skin = ent->v.skin;
810 ent->deltabaseline.frame = ent->v.frame;
811 ent->deltabaseline.effects = ent->v.effects;
812 ent->deltabaseline.modelindex = ent->v.modelindex;
813 ent->deltabaseline.alpha = alpha;
814 ent->deltabaseline.scale = scale;
815 ent->deltabaseline.glowsize = glowsize;
816 ent->deltabaseline.glowcolor = glowcolor;
819 if (bits >= 16777216)
827 MSG_WriteByte (msg, bits);
828 if (bits & U_MOREBITS)
829 MSG_WriteByte (msg, bits>>8);
830 // LordHavoc: extend bytes have to be written here due to delta compression
831 if (bits & U_EXTEND1)
832 MSG_WriteByte (msg, bits>>16);
833 if (bits & U_EXTEND2)
834 MSG_WriteByte (msg, bits>>24);
836 // LordHavoc: old stuff
837 if (bits & U_LONGENTITY)
838 MSG_WriteShort (msg,e);
840 MSG_WriteByte (msg,e);
841 if (bits & U_MODEL) MSG_WriteByte (msg, ent->v.modelindex);
842 if (bits & U_FRAME) MSG_WriteByte (msg, ent->v.frame);
843 if (bits & U_COLORMAP) MSG_WriteByte (msg, ent->v.colormap);
844 if (bits & U_SKIN) MSG_WriteByte (msg, ent->v.skin);
845 if (bits & U_EFFECTS) MSG_WriteByte (msg, ent->v.effects);
846 if (bits & U_ORIGIN1) MSG_WriteDPCoord (msg, origin[0]);
847 if (bits & U_ANGLE1) MSG_WriteAngle(msg, angles[0]);
848 if (bits & U_ORIGIN2) MSG_WriteDPCoord (msg, origin[1]);
849 if (bits & U_ANGLE2) MSG_WriteAngle(msg, angles[1]);
850 if (bits & U_ORIGIN3) MSG_WriteDPCoord (msg, origin[2]);
851 if (bits & U_ANGLE3) MSG_WriteAngle(msg, angles[2]);
853 // LordHavoc: new stuff
854 if (bits & U_ALPHA) MSG_WriteByte(msg, alpha);
855 if (bits & U_SCALE) MSG_WriteByte(msg, scale);
856 if (bits & U_EFFECTS2) MSG_WriteByte(msg, (int)ent->v.effects >> 8);
857 if (bits & U_GLOWSIZE) MSG_WriteByte(msg, glowsize);
858 if (bits & U_GLOWCOLOR) MSG_WriteByte(msg, glowcolor);
859 if (bits & U_FRAME2) MSG_WriteByte(msg, (int)ent->v.frame >> 8);
860 if (bits & U_MODEL2) MSG_WriteByte(msg, (int)ent->v.modelindex >> 8);
863 if (sv_cullentities_stats.integer)
864 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);
867 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
869 int e, clentnum, flags, alpha, glowcolor, glowsize, scale, effects;
870 int culled_pvs, culled_portal, culled_trace, visibleentities, totalentities;
872 vec3_t org, origin, angles, entmins, entmaxs;
878 double testorigin[3];
879 entity_frame_t entityframe;
882 if (client->sendsignon)
885 Mod_CheckLoaded(sv.worldmodel);
887 // find the client's PVS
888 VectorAdd (clent->v.origin, clent->v.view_ofs, testeye);
889 VectorCopy (testeye, org);
890 pvs = SV_FatPVS (org);
891 EntityFrame_Clear(&entityframe, org);
899 clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
900 // send all entities that touch the pvs
901 ent = NEXT_EDICT(sv.edicts);
902 for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
906 if (ent != clent) // LordHavoc: always send player
908 if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
910 if (val->edict == clentnum)
911 flags |= RENDER_VIEWMODEL; // show relative to the view
914 // don't show to anyone else
920 // LordHavoc: never draw something told not to display to this client
921 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
923 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
930 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
931 glowsize = (int) val->_float >> 2;
932 glowsize = bound(0, glowsize, 255);
934 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
935 if (val->_float != 0)
936 flags |= RENDER_GLOWTRAIL;
938 if (ent->v.modelindex >= 0 && ent->v.modelindex < MAX_MODELS && pr_strings[ent->v.model])
940 model = sv.models[(int)ent->v.modelindex];
941 Mod_CheckLoaded(model);
946 if (ent != clent) // LordHavoc: always send player
947 if (glowsize == 0 && (flags & RENDER_GLOWTRAIL) == 0) // no effects
951 VectorCopy(ent->v.angles, angles);
952 if (DotProduct(ent->v.velocity, ent->v.velocity) >= 1.0f && host_client->latency >= 0.01f)
954 VectorMA(ent->v.origin, host_client->latency, ent->v.velocity, origin);
955 // LordHavoc: trace predicted movement to avoid putting things in walls
956 trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, origin, MOVE_NORMAL, ent);
957 VectorCopy(trace.endpos, origin);
961 VectorCopy(ent->v.origin, origin);
964 // don't send an entity if it's coordinates would wrap around
965 if (origin[0] < -32768 || origin[1] < -32768 || origin[2] < -32768 || origin[0] > 32767 || origin[1] > 32767 || origin[2] > 32767)
968 // ent has survived every check so far, check if it is visible
969 // always send embedded brush models, they don't generate much traffic
970 if (ent != clent && ((flags & RENDER_VIEWMODEL) == 0) && (model == NULL || model->type != mod_brush || model->name[0] != '*'))
972 // use the predicted origin
973 entmins[0] = origin[0] - 1.0f;
974 entmins[1] = origin[1] - 1.0f;
975 entmins[2] = origin[2] - 1.0f;
976 entmaxs[0] = origin[0] + 1.0f;
977 entmaxs[1] = origin[1] + 1.0f;
978 entmaxs[2] = origin[2] + 1.0f;
979 // using the model's bounding box to ensure things are visible regardless of their physics box
982 if (ent->v.angles[0] || ent->v.angles[2]) // pitch and roll
984 VectorAdd(entmins, model->rotatedmins, entmins);
985 VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
987 else if (ent->v.angles[1])
989 VectorAdd(entmins, model->yawmins, entmins);
990 VectorAdd(entmaxs, model->yawmaxs, entmaxs);
994 VectorAdd(entmins, model->normalmins, entmins);
995 VectorAdd(entmaxs, model->normalmaxs, entmaxs);
1001 // if not touching a visible leaf
1002 if (sv_cullentities_pvs.integer && !SV_BoxTouchingPVS(pvs, entmins, entmaxs, sv.worldmodel->nodes))
1008 // or not visible through the portals
1009 if (sv_cullentities_portal.integer && !Portal_CheckBox(sv.worldmodel, org, entmins, entmaxs))
1015 if (sv_cullentities_trace.integer)
1017 // LordHavoc: test random offsets, to maximize chance of detection
1018 testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
1019 testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
1020 testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
1022 memset (&trace, 0, sizeof(trace_t));
1024 trace.allsolid = true;
1025 VectorCopy(testorigin, trace.endpos);
1027 VectorCopy(org, RecursiveHullCheckInfo.start);
1028 VectorSubtract(testorigin, testeye, RecursiveHullCheckInfo.dist);
1029 RecursiveHullCheckInfo.hull = sv.worldmodel->hulls;
1030 RecursiveHullCheckInfo.trace = &trace;
1031 SV_RecursiveHullCheck (sv.worldmodel->hulls->firstclipnode, 0, 1, testeye, testorigin);
1033 if (trace.fraction == 1)
1034 client->visibletime[e] = realtime + 1;
1037 if (realtime > client->visibletime[e])
1050 effects = ent->v.effects;
1052 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
1053 if (val->_float != 0)
1054 alpha = (int) (val->_float * 255.0);
1057 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
1058 if (val->_float != 0)
1059 alpha = (int) val->_float;
1063 alpha = bound(0, alpha, 255);
1065 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
1066 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
1067 if (scale < 0) scale = 0;
1068 if (scale > 255) scale = 255;
1070 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
1071 if (val->_float != 0)
1072 glowcolor = (int) val->_float;
1074 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
1075 if (val->_float != 0)
1076 effects |= EF_FULLBRIGHT;
1080 if (glowsize == 0 && (flags & RENDER_GLOWTRAIL) == 0) // no effects
1084 // don't send if flagged for NODRAW and there are no effects
1085 if (model->flags == 0 && ((effects & EF_NODRAW) || scale <= 0 || alpha <= 0))
1088 else // no model and no effects
1093 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
1094 flags |= RENDER_EXTERIORMODEL;
1096 if (ent->v.movetype == MOVETYPE_STEP)
1097 flags |= RENDER_STEP;
1099 s = EntityFrame_NewEntity(&entityframe, e);
1100 // if we run out of space, abort
1103 VectorCopy(origin, s->origin);
1104 VectorCopy(angles, s->angles);
1105 s->colormap = ent->v.colormap;
1106 s->skin = ent->v.skin;
1107 s->frame = ent->v.frame;
1108 s->modelindex = ent->v.modelindex;
1109 s->effects = effects;
1112 s->glowsize = glowsize;
1113 s->glowcolor = glowcolor;
1116 entityframe.framenum = ++client->entityframenumber;
1117 EntityFrame_Write(&client->entitydatabase, &entityframe, msg);
1119 if (sv_cullentities_stats.integer)
1120 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);
1130 void SV_CleanupEnts (void)
1135 ent = NEXT_EDICT(sv.edicts);
1136 for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
1137 ent->v.effects = (int)ent->v.effects & ~EF_MUZZLEFLASH;
1142 SV_WriteClientdataToMessage
1146 void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
1157 // send a damage message
1159 if (ent->v.dmg_take || ent->v.dmg_save)
1161 other = PROG_TO_EDICT(ent->v.dmg_inflictor);
1162 MSG_WriteByte (msg, svc_damage);
1163 MSG_WriteByte (msg, ent->v.dmg_save);
1164 MSG_WriteByte (msg, ent->v.dmg_take);
1165 for (i=0 ; i<3 ; i++)
1166 MSG_WriteDPCoord (msg, other->v.origin[i] + 0.5*(other->v.mins[i] + other->v.maxs[i]));
1168 ent->v.dmg_take = 0;
1169 ent->v.dmg_save = 0;
1173 // send the current viewpos offset from the view entity
1175 SV_SetIdealPitch (); // how much to look up / down ideally
1177 // a fixangle might get lost in a dropped packet. Oh well.
1178 if ( ent->v.fixangle )
1180 MSG_WriteByte (msg, svc_setangle);
1181 for (i=0 ; i < 3 ; i++)
1182 MSG_WriteAngle (msg, ent->v.angles[i] );
1183 ent->v.fixangle = 0;
1188 //if (ent->v.view_ofs[2] != DEFAULT_VIEWHEIGHT)
1189 bits |= SU_VIEWHEIGHT;
1191 if (ent->v.idealpitch)
1192 bits |= SU_IDEALPITCH;
1194 // stuff the sigil bits into the high bits of items for sbar, or else
1196 val = GETEDICTFIELDVALUE(ent, eval_items2);
1199 items = (int)ent->v.items | ((int)val->_float << 23);
1201 items = (int)ent->v.items | ((int)pr_global_struct->serverflags << 28);
1205 if ( (int)ent->v.flags & FL_ONGROUND)
1206 bits |= SU_ONGROUND;
1208 if ( ent->v.waterlevel >= 2)
1212 VectorClear(punchvector);
1213 if ((val = GETEDICTFIELDVALUE(ent, eval_punchvector)))
1214 VectorCopy(val->vector, punchvector);
1217 if ((val = GETEDICTFIELDVALUE(ent, eval_viewzoom)))
1219 i = val->_float * 255.0f;
1223 i = bound(0, i, 255);
1227 if (viewzoom != 255)
1228 bits |= SU_VIEWZOOM;
1230 for (i=0 ; i<3 ; i++)
1232 if (ent->v.punchangle[i])
1233 bits |= (SU_PUNCH1<<i);
1234 if (punchvector[i]) // dpprotocol
1235 bits |= (SU_PUNCHVEC1<<i); // dpprotocol
1236 if (ent->v.velocity[i])
1237 bits |= (SU_VELOCITY1<<i);
1240 if (ent->v.weaponframe)
1241 bits |= SU_WEAPONFRAME;
1243 if (ent->v.armorvalue)
1246 // if (ent->v.weapon)
1251 if (bits >= 16777216)
1256 MSG_WriteByte (msg, svc_clientdata);
1257 MSG_WriteShort (msg, bits);
1258 if (bits & SU_EXTEND1)
1259 MSG_WriteByte(msg, bits >> 16);
1260 if (bits & SU_EXTEND2)
1261 MSG_WriteByte(msg, bits >> 24);
1263 if (bits & SU_VIEWHEIGHT)
1264 //MSG_WriteChar (msg, ent->v.view_ofs[2]);
1265 MSG_WriteChar (msg, 0);
1267 if (bits & SU_IDEALPITCH)
1268 MSG_WriteChar (msg, ent->v.idealpitch);
1270 for (i=0 ; i<3 ; i++)
1272 if (bits & (SU_PUNCH1<<i))
1273 MSG_WritePreciseAngle(msg, ent->v.punchangle[i]); // dpprotocol
1274 if (bits & (SU_PUNCHVEC1<<i)) // dpprotocol
1275 MSG_WriteDPCoord(msg, punchvector[i]); // dpprotocol
1276 if (bits & (SU_VELOCITY1<<i))
1277 MSG_WriteChar (msg, ent->v.velocity[i]/16);
1280 // [always sent] if (bits & SU_ITEMS)
1281 MSG_WriteLong (msg, items);
1283 if (bits & SU_WEAPONFRAME)
1284 MSG_WriteByte (msg, ent->v.weaponframe);
1285 if (bits & SU_ARMOR)
1286 MSG_WriteByte (msg, ent->v.armorvalue);
1287 if (bits & SU_WEAPON)
1288 MSG_WriteByte (msg, SV_ModelIndex(pr_strings+ent->v.weaponmodel));
1290 MSG_WriteShort (msg, ent->v.health);
1291 MSG_WriteByte (msg, ent->v.currentammo);
1292 MSG_WriteByte (msg, ent->v.ammo_shells);
1293 MSG_WriteByte (msg, ent->v.ammo_nails);
1294 MSG_WriteByte (msg, ent->v.ammo_rockets);
1295 MSG_WriteByte (msg, ent->v.ammo_cells);
1297 if (gamemode == GAME_HIPNOTIC || gamemode == GAME_ROGUE)
1301 if ( ((int)ent->v.weapon) & (1<<i) )
1303 MSG_WriteByte (msg, i);
1310 MSG_WriteByte (msg, ent->v.weapon);
1313 if (bits & SU_VIEWZOOM)
1314 MSG_WriteByte (msg, viewzoom);
1318 =======================
1319 SV_SendClientDatagram
1320 =======================
1322 qboolean SV_SendClientDatagram (client_t *client)
1324 qbyte buf[MAX_DATAGRAM];
1328 msg.maxsize = sizeof(buf);
1331 MSG_WriteByte (&msg, svc_time);
1332 MSG_WriteFloat (&msg, sv.time);
1334 if (!client->waitingforconnect)
1336 // add the client specific data to the datagram
1337 SV_WriteClientdataToMessage (client->edict, &msg);
1339 SV_WriteEntitiesToClient (client, client->edict, &msg);
1341 // copy the server datagram if there is space
1342 if (msg.cursize + sv.datagram.cursize < msg.maxsize)
1343 SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
1346 // send the datagram
1347 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1349 SV_DropClient (true);// if the message couldn't send, kick off
1357 =======================
1358 SV_UpdateToReliableMessages
1359 =======================
1361 void SV_UpdateToReliableMessages (void)
1366 // check for changes to be sent over the reliable streams
1367 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1369 if (host_client->old_frags != host_client->edict->v.frags)
1371 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1373 if (!client->active || !client->spawned)
1375 MSG_WriteByte (&client->message, svc_updatefrags);
1376 MSG_WriteByte (&client->message, i);
1377 MSG_WriteShort (&client->message, host_client->edict->v.frags);
1380 host_client->old_frags = host_client->edict->v.frags;
1384 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1386 if (!client->active)
1388 SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
1391 SZ_Clear (&sv.reliable_datagram);
1396 =======================
1399 Send a nop message without trashing or sending the accumulated client
1401 =======================
1403 void SV_SendNop (client_t *client)
1409 msg.maxsize = sizeof(buf);
1412 MSG_WriteChar (&msg, svc_nop);
1414 if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
1415 SV_DropClient (true); // if the message couldn't send, kick off
1416 client->last_message = realtime;
1420 =======================
1421 SV_SendClientMessages
1422 =======================
1424 void SV_SendClientMessages (void)
1428 // update frags, names, etc
1429 SV_UpdateToReliableMessages ();
1431 // build individual updates
1432 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1434 if (!host_client->active)
1437 if (host_client->sendserverinfo)
1439 host_client->sendserverinfo = false;
1440 SV_SendServerinfo (host_client);
1443 if (host_client->spawned)
1445 if (!SV_SendClientDatagram (host_client))
1450 // the player isn't totally in the game yet
1451 // send small keepalive messages if too much time has passed
1452 // send a full message when the next signon stage has been requested
1453 // some other message data (name changes, etc) may accumulate
1454 // between signon stages
1455 if (!host_client->sendsignon)
1457 if (realtime - host_client->last_message > 5)
1458 SV_SendNop (host_client);
1459 continue; // don't send out non-signon messages
1463 // check for an overflowed message. Should only happen
1464 // on a very fucked up connection that backs up a lot, then
1466 if (host_client->message.overflowed)
1468 SV_DropClient (true);
1469 host_client->message.overflowed = false;
1473 if (host_client->message.cursize || host_client->dropasap)
1475 if (!NET_CanSendMessage (host_client->netconnection))
1477 // I_Printf ("can't write\n");
1481 if (host_client->dropasap)
1482 SV_DropClient (false); // went to another level
1485 if (NET_SendMessage (host_client->netconnection, &host_client->message) == -1)
1486 SV_DropClient (true); // if the message couldn't send, kick off
1487 SZ_Clear (&host_client->message);
1488 host_client->last_message = realtime;
1489 host_client->sendsignon = false;
1495 // clear muzzle flashes
1501 ==============================================================================
1505 ==============================================================================
1514 int SV_ModelIndex (char *name)
1518 if (!name || !name[0])
1521 for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
1522 if (!strcmp(sv.model_precache[i], name))
1524 if (i==MAX_MODELS || !sv.model_precache[i])
1525 Host_Error ("SV_ModelIndex: model %s not precached", name);
1529 #ifdef SV_QUAKEENTITIES
1536 void SV_CreateBaseline (void)
1538 int i, entnum, large;
1541 // LordHavoc: clear *all* states (note just active ones)
1542 for (entnum = 0; entnum < MAX_EDICTS ; entnum++)
1544 // get the current server version
1545 svent = EDICT_NUM(entnum);
1547 // LordHavoc: always clear state values, whether the entity is in use or not
1548 ClearStateToDefault(&svent->baseline);
1552 if (entnum > svs.maxclients && !svent->v.modelindex)
1555 // create entity baseline
1556 VectorCopy (svent->v.origin, svent->baseline.origin);
1557 VectorCopy (svent->v.angles, svent->baseline.angles);
1558 svent->baseline.frame = svent->v.frame;
1559 svent->baseline.skin = svent->v.skin;
1560 if (entnum > 0 && entnum <= svs.maxclients)
1562 svent->baseline.colormap = entnum;
1563 svent->baseline.modelindex = SV_ModelIndex("progs/player.mdl");
1567 svent->baseline.colormap = 0;
1568 svent->baseline.modelindex = svent->v.modelindex; //SV_ModelIndex(pr_strings + svent->v.model);
1572 if (svent->baseline.modelindex & 0xFF00 || svent->baseline.frame & 0xFF00)
1575 // add to the message
1577 MSG_WriteByte (&sv.signon, svc_spawnbaseline2);
1579 MSG_WriteByte (&sv.signon, svc_spawnbaseline);
1580 MSG_WriteShort (&sv.signon, entnum);
1584 MSG_WriteShort (&sv.signon, svent->baseline.modelindex);
1585 MSG_WriteShort (&sv.signon, svent->baseline.frame);
1589 MSG_WriteByte (&sv.signon, svent->baseline.modelindex);
1590 MSG_WriteByte (&sv.signon, svent->baseline.frame);
1592 MSG_WriteByte (&sv.signon, svent->baseline.colormap);
1593 MSG_WriteByte (&sv.signon, svent->baseline.skin);
1594 for (i=0 ; i<3 ; i++)
1596 MSG_WriteDPCoord(&sv.signon, svent->baseline.origin[i]);
1597 MSG_WriteAngle(&sv.signon, svent->baseline.angles[i]);
1608 Tell all the clients that the server is changing levels
1611 void SV_SendReconnect (void)
1618 msg.maxsize = sizeof(data);
1620 MSG_WriteChar (&msg, svc_stufftext);
1621 MSG_WriteString (&msg, "reconnect\n");
1622 NET_SendToAll (&msg, 5);
1624 if (cls.state != ca_dedicated)
1625 Cmd_ExecuteString ("reconnect\n", src_command);
1633 Grabs the current state of each client for saving across the
1634 transition to another level
1637 void SV_SaveSpawnparms (void)
1641 svs.serverflags = pr_global_struct->serverflags;
1643 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1645 if (!host_client->active)
1648 // call the progs to get default spawn parms for the new client
1649 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1650 PR_ExecuteProgram (pr_global_struct->SetChangeParms, "QC function SetChangeParms is missing");
1651 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1652 host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1660 This is called at the start of each level
1663 extern float scr_centertime_off;
1665 void SV_SpawnServer (char *server)
1670 // let's not have any servers with no name
1671 if (hostname.string[0] == 0)
1672 Cvar_Set ("hostname", "UNNAMED");
1673 scr_centertime_off = 0;
1675 Con_DPrintf ("SpawnServer: %s\n",server);
1676 svs.changelevel_issued = false; // now safe to issue another
1679 // tell all connected clients that we are going to a new level
1682 SV_SendReconnect ();
1685 // make cvars consistant
1688 Cvar_SetValue ("deathmatch", 0);
1689 current_skill = bound(0, (int)(skill.value + 0.5), 3);
1691 Cvar_SetValue ("skill", (float)current_skill);
1694 // set up the new server
1696 Host_ClearMemory ();
1698 memset (&sv, 0, sizeof(sv));
1700 strcpy (sv.name, server);
1702 // load progs to get entity field count
1705 // allocate server memory
1706 sv.max_edicts = MAX_EDICTS;
1708 // clear the edict memory pool
1709 Mem_EmptyPool(sv_edicts_mempool);
1710 sv.edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1712 sv.datagram.maxsize = sizeof(sv.datagram_buf);
1713 sv.datagram.cursize = 0;
1714 sv.datagram.data = sv.datagram_buf;
1716 sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1717 sv.reliable_datagram.cursize = 0;
1718 sv.reliable_datagram.data = sv.reliable_datagram_buf;
1720 sv.signon.maxsize = sizeof(sv.signon_buf);
1721 sv.signon.cursize = 0;
1722 sv.signon.data = sv.signon_buf;
1724 // leave slots at start for clients only
1725 sv.num_edicts = svs.maxclients+1;
1726 for (i=0 ; i<svs.maxclients ; i++)
1728 ent = EDICT_NUM(i+1);
1729 svs.clients[i].edict = ent;
1732 sv.state = ss_loading;
1739 strcpy (sv.name, server);
1740 sprintf (sv.modelname,"maps/%s.bsp", server);
1741 sv.worldmodel = Mod_ForName(sv.modelname, false, true, true);
1744 Con_Printf ("Couldn't spawn server %s\n", sv.modelname);
1748 sv.models[1] = sv.worldmodel;
1751 // clear world interaction links
1755 sv.sound_precache[0] = pr_strings;
1757 sv.model_precache[0] = pr_strings;
1758 sv.model_precache[1] = sv.modelname;
1759 for (i = 1;i < sv.worldmodel->numsubmodels;i++)
1761 sv.model_precache[i+1] = localmodels[i];
1762 sv.models[i+1] = Mod_ForName (localmodels[i], false, false, false);
1766 // load the rest of the entities
1769 memset (&ent->v, 0, progs->entityfields * 4);
1771 ent->v.model = sv.worldmodel->name - pr_strings;
1772 ent->v.modelindex = 1; // world model
1773 ent->v.solid = SOLID_BSP;
1774 ent->v.movetype = MOVETYPE_PUSH;
1777 pr_global_struct->coop = coop.integer;
1779 pr_global_struct->deathmatch = deathmatch.integer;
1781 pr_global_struct->mapname = sv.name - pr_strings;
1783 // serverflags are for cross level information (sigils)
1784 pr_global_struct->serverflags = svs.serverflags;
1786 ED_LoadFromFile (sv.worldmodel->entities);
1787 // LordHavoc: clear world angles (to fix e3m3.bsp)
1788 VectorClear(sv.edicts->v.angles);
1792 // all setup is completed, any further precache statements are errors
1793 sv.state = ss_active;
1795 // run two frames to allow everything to settle
1796 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1798 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1803 #ifdef QUAKEENTITIES
1804 // create a baseline for more efficient communications
1805 SV_CreateBaseline ();
1808 // send serverinfo to all connected clients
1809 for (i=0,host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1810 if (host_client->active)
1811 SV_SendServerinfo (host_client);
1813 Con_DPrintf ("Server spawned.\n");