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
25 static cvar_t sv_cullentities_pvs = {0, "sv_cullentities_pvs", "0"}; // fast but loose
26 static cvar_t sv_cullentities_portal = {0, "sv_cullentities_portal", "0"}; // extremely accurate visibility checking, but too slow
27 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
28 static cvar_t sv_cullentities_stats = {0, "sv_cullentities_stats", "0"};
29 static cvar_t sv_entpatch = {0, "sv_entpatch", "1"};
34 static char localmodels[MAX_MODELS][5]; // inline model names for precache
36 mempool_t *sv_edicts_mempool = NULL;
38 //============================================================================
40 extern void SV_Phys_Init (void);
41 extern void SV_World_Init (void);
52 Cvar_RegisterVariable (&sv_maxvelocity);
53 Cvar_RegisterVariable (&sv_gravity);
54 Cvar_RegisterVariable (&sv_friction);
55 Cvar_RegisterVariable (&sv_edgefriction);
56 Cvar_RegisterVariable (&sv_stopspeed);
57 Cvar_RegisterVariable (&sv_maxspeed);
58 Cvar_RegisterVariable (&sv_accelerate);
59 Cvar_RegisterVariable (&sv_idealpitchscale);
60 Cvar_RegisterVariable (&sv_aim);
61 Cvar_RegisterVariable (&sv_nostep);
62 Cvar_RegisterVariable (&sv_deltacompress);
63 Cvar_RegisterVariable (&sv_cullentities_pvs);
64 Cvar_RegisterVariable (&sv_cullentities_portal);
65 Cvar_RegisterVariable (&sv_cullentities_trace);
66 Cvar_RegisterVariable (&sv_cullentities_stats);
67 Cvar_RegisterVariable (&sv_entpatch);
72 for (i = 0;i < MAX_MODELS;i++)
73 sprintf (localmodels[i], "*%i", i);
75 sv_edicts_mempool = Mem_AllocPool("edicts");
79 =============================================================================
83 =============================================================================
90 Make sure the event gets sent to all clients
93 void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
97 if (sv.datagram.cursize > MAX_DATAGRAM-16)
99 MSG_WriteByte (&sv.datagram, svc_particle);
100 MSG_WriteDPCoord (&sv.datagram, org[0]);
101 MSG_WriteDPCoord (&sv.datagram, org[1]);
102 MSG_WriteDPCoord (&sv.datagram, org[2]);
103 for (i=0 ; i<3 ; i++)
110 MSG_WriteChar (&sv.datagram, v);
112 MSG_WriteByte (&sv.datagram, count);
113 MSG_WriteByte (&sv.datagram, color);
120 Make sure the event gets sent to all clients
123 void SV_StartEffect (vec3_t org, int modelindex, int startframe, int framecount, int framerate)
125 if (sv.datagram.cursize > MAX_DATAGRAM-18)
127 if (modelindex >= 256 || startframe >= 256)
129 MSG_WriteByte (&sv.datagram, svc_effect2);
130 MSG_WriteDPCoord (&sv.datagram, org[0]);
131 MSG_WriteDPCoord (&sv.datagram, org[1]);
132 MSG_WriteDPCoord (&sv.datagram, org[2]);
133 MSG_WriteShort (&sv.datagram, modelindex);
134 MSG_WriteShort (&sv.datagram, startframe);
135 MSG_WriteByte (&sv.datagram, framecount);
136 MSG_WriteByte (&sv.datagram, framerate);
140 MSG_WriteByte (&sv.datagram, svc_effect);
141 MSG_WriteDPCoord (&sv.datagram, org[0]);
142 MSG_WriteDPCoord (&sv.datagram, org[1]);
143 MSG_WriteDPCoord (&sv.datagram, org[2]);
144 MSG_WriteByte (&sv.datagram, modelindex);
145 MSG_WriteByte (&sv.datagram, startframe);
146 MSG_WriteByte (&sv.datagram, framecount);
147 MSG_WriteByte (&sv.datagram, framerate);
155 Each entity can have eight independant sound sources, like voice,
158 Channel 0 is an auto-allocate channel, the others override anything
159 already running on that entity/channel pair.
161 An attenuation of 0 will play full volume everywhere in the level.
162 Larger attenuations will drop off. (max 4 attenuation)
166 void SV_StartSound (edict_t *entity, int channel, char *sample, int volume,
174 if (volume < 0 || volume > 255)
175 Host_Error ("SV_StartSound: volume = %i", volume);
177 if (attenuation < 0 || attenuation > 4)
178 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
180 if (channel < 0 || channel > 7)
181 Host_Error ("SV_StartSound: channel = %i", channel);
183 if (sv.datagram.cursize > MAX_DATAGRAM-16)
186 // find precache number for sound
187 for (sound_num=1 ; sound_num<MAX_SOUNDS
188 && sv.sound_precache[sound_num] ; sound_num++)
189 if (!strcmp(sample, sv.sound_precache[sound_num]))
192 if ( sound_num == MAX_SOUNDS || !sv.sound_precache[sound_num] )
194 Con_Printf ("SV_StartSound: %s not precached\n", sample);
198 ent = NUM_FOR_EDICT(entity);
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 field_mask |= SND_LARGEENTITY;
207 if (sound_num >= 256 || channel >= 8)
208 field_mask |= SND_LARGESOUND;
210 // directed messages go only to the entity they are targeted on
211 MSG_WriteByte (&sv.datagram, svc_sound);
212 MSG_WriteByte (&sv.datagram, field_mask);
213 if (field_mask & SND_VOLUME)
214 MSG_WriteByte (&sv.datagram, volume);
215 if (field_mask & SND_ATTENUATION)
216 MSG_WriteByte (&sv.datagram, attenuation*64);
217 if (field_mask & SND_LARGEENTITY)
219 MSG_WriteShort (&sv.datagram, ent);
220 MSG_WriteByte (&sv.datagram, channel);
223 MSG_WriteShort (&sv.datagram, (ent<<3) | channel);
224 if (field_mask & SND_LARGESOUND)
225 MSG_WriteShort (&sv.datagram, sound_num);
227 MSG_WriteByte (&sv.datagram, sound_num);
228 for (i = 0;i < 3;i++)
229 MSG_WriteDPCoord (&sv.datagram, entity->v->origin[i]+0.5*(entity->v->mins[i]+entity->v->maxs[i]));
233 ==============================================================================
237 ==============================================================================
244 Sends the first message from the server to a connected client.
245 This will be sent on the initial connection and upon each server load.
248 void SV_SendServerinfo (client_t *client)
253 // LordHavoc: clear entityframe tracking
254 client->entityframenumber = 0;
255 EntityFrame_ClearDatabase(&client->entitydatabase);
257 MSG_WriteByte (&client->message, svc_print);
258 sprintf (message, "\002\nServer: %s build %s (progs %i crc)", gamename, buildstring, pr_crc);
259 MSG_WriteString (&client->message,message);
261 MSG_WriteByte (&client->message, svc_serverinfo);
262 MSG_WriteLong (&client->message, DPPROTOCOL_VERSION3);
263 MSG_WriteByte (&client->message, svs.maxclients);
265 if (!coop.integer && deathmatch.integer)
266 MSG_WriteByte (&client->message, GAME_DEATHMATCH);
268 MSG_WriteByte (&client->message, GAME_COOP);
270 MSG_WriteString (&client->message,PR_GetString(sv.edicts->v->message));
272 for (s = sv.model_precache+1 ; *s ; s++)
273 MSG_WriteString (&client->message, *s);
274 MSG_WriteByte (&client->message, 0);
276 for (s = sv.sound_precache+1 ; *s ; s++)
277 MSG_WriteString (&client->message, *s);
278 MSG_WriteByte (&client->message, 0);
281 MSG_WriteByte (&client->message, svc_cdtrack);
282 MSG_WriteByte (&client->message, sv.edicts->v->sounds);
283 MSG_WriteByte (&client->message, sv.edicts->v->sounds);
286 MSG_WriteByte (&client->message, svc_setview);
287 MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
289 MSG_WriteByte (&client->message, svc_signonnum);
290 MSG_WriteByte (&client->message, 1);
292 client->sendsignon = true;
293 client->spawned = false; // need prespawn, spawn, etc
300 Initializes a client_t for a new net connection. This will only be called
301 once for a player each game, not once for each level change.
304 void SV_ConnectClient (int clientnum, netconn_t *netconnection)
308 float spawn_parms[NUM_SPAWN_PARMS];
310 client = svs.clients + clientnum;
312 // set up the client_t
314 memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
315 memset (client, 0, sizeof(*client));
316 client->netconnection = netconnection;
318 Con_DPrintf ("Client %s connected\n", client->netconnection->address);
320 strcpy (client->name, "unconnected");
321 client->active = true;
322 client->spawned = false;
323 client->edict = EDICT_NUM(clientnum+1);
324 client->message.data = client->msgbuf;
325 client->message.maxsize = sizeof(client->msgbuf);
326 client->message.allowoverflow = true; // we can catch it
329 memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
332 // call the progs to get default spawn parms for the new client
333 PR_ExecuteProgram (pr_global_struct->SetNewParms, "QC function SetNewParms is missing");
334 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
335 client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
336 // set up the edict a bit
337 ED_ClearEdict(client->edict);
338 client->edict->v->colormap = NUM_FOR_EDICT(client->edict);
339 client->edict->v->netname = PR_SetString(client->name);
342 SV_SendServerinfo (client);
347 ===============================================================================
351 ===============================================================================
360 void SV_ClearDatagram (void)
362 SZ_Clear (&sv.datagram);
366 =============================================================================
368 The PVS must include a small area around the client to allow head bobbing
369 or other small motion on the client side. Otherwise, a bob might cause an
370 entity that should be visible to not show up, especially when the bob
373 =============================================================================
377 qbyte fatpvs[MAX_MAP_LEAFS/8];
379 void SV_AddToFatPVS (vec3_t org, mnode_t *node)
388 // if this is a leaf, accumulate the pvs bits
389 if (node->contents < 0)
391 if (node->contents != CONTENTS_SOLID)
393 pvs = sv.worldmodel->LeafPVS(sv.worldmodel, (mleaf_t *)node);
394 for (i=0 ; i<fatbytes ; i++)
401 d = DotProduct (org, plane->normal) - plane->dist;
403 node = node->children[0];
405 node = node->children[1];
408 SV_AddToFatPVS (org, node->children[0]);
409 node = node->children[1];
418 Calculates a PVS that is the inclusive or of all leafs within 8 pixels of the
422 qbyte *SV_FatPVS (vec3_t org)
424 fatbytes = (sv.worldmodel->numleafs+31)>>3;
425 memset (fatpvs, 0, fatbytes);
426 SV_AddToFatPVS (org, sv.worldmodel->nodes);
430 //=============================================================================
433 int SV_BoxTouchingPVS (qbyte *pvs, vec3_t mins, vec3_t maxs, mnode_t *node)
437 if (node->contents < 0)
440 if (node->contents == CONTENTS_SOLID)
442 leafnum = (mleaf_t *)node - sv.worldmodel->leafs - 1;
443 return pvs[leafnum >> 3] & (1 << (leafnum & 7));
446 // node - recurse down the BSP tree
447 switch (BoxOnPlaneSide(mins, maxs, node->plane))
450 node = node->children[0];
453 node = node->children[1];
456 if (node->children[0]->contents != CONTENTS_SOLID)
457 if (SV_BoxTouchingPVS (pvs, mins, maxs, node->children[0]))
459 node = node->children[1];
469 SV_WriteEntitiesToClient
474 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
476 int e, clentnum, bits, alpha, glowcolor, glowsize, scale, effects, lightsize;
477 int culled_pvs, culled_portal, culled_trace, visibleentities, totalentities;
479 vec3_t origin, angles, entmins, entmaxs, testorigin, testeye;
480 float nextfullupdate, alphaf;
483 entity_state_t *baseline; // LordHavoc: delta or startup baseline
487 Mod_CheckLoaded(sv.worldmodel);
489 // find the client's PVS
490 VectorAdd (clent->v->origin, clent->v->view_ofs, testeye);
491 pvs = SV_FatPVS (testeye);
499 clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
500 // send all entities that touch the pvs
501 ent = NEXT_EDICT(sv.edicts);
502 for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
506 // prevent delta compression against this frame (unless actually sent, which will restore this later)
507 nextfullupdate = client->nextfullupdate[e];
508 client->nextfullupdate[e] = -1;
510 if (ent != clent) // LordHavoc: always send player
512 if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
514 if (val->edict != clentnum)
516 // don't show to anyone else
520 bits |= U_VIEWMODEL; // show relative to the view
524 // LordHavoc: never draw something told not to display to this client
525 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
527 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
534 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
535 glowsize = (int) val->_float >> 2;
536 if (glowsize > 255) glowsize = 255;
537 if (glowsize < 0) glowsize = 0;
539 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
540 if (val->_float != 0)
543 if (ent->v->modelindex >= 0 && ent->v->modelindex < MAX_MODELS && *PR_GetString(ent->v->model))
545 model = sv.models[(int)ent->v->modelindex];
546 Mod_CheckLoaded(model);
551 if (ent != clent) // LordHavoc: always send player
552 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
556 VectorCopy(ent->v->angles, angles);
557 VectorCopy(ent->v->origin, origin);
559 // ent has survived every check so far, check if it is visible
560 if (ent != clent && ((bits & U_VIEWMODEL) == 0))
562 // use the predicted origin
563 entmins[0] = origin[0] - 1.0f;
564 entmins[1] = origin[1] - 1.0f;
565 entmins[2] = origin[2] - 1.0f;
566 entmaxs[0] = origin[0] + 1.0f;
567 entmaxs[1] = origin[1] + 1.0f;
568 entmaxs[2] = origin[2] + 1.0f;
569 // using the model's bounding box to ensure things are visible regardless of their physics box
572 if (ent->v->angles[0] || ent->v->angles[2]) // pitch and roll
574 VectorAdd(entmins, model->rotatedmins, entmins);
575 VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
577 else if (ent->v->angles[1])
579 VectorAdd(entmins, model->yawmins, entmins);
580 VectorAdd(entmaxs, model->yawmaxs, entmaxs);
584 VectorAdd(entmins, model->normalmins, entmins);
585 VectorAdd(entmaxs, model->normalmaxs, entmaxs);
591 // if not touching a visible leaf
592 if (sv_cullentities_pvs.integer && !SV_BoxTouchingPVS(pvs, entmins, entmaxs, sv.worldmodel->nodes))
598 // or not visible through the portals
599 if (sv_cullentities_portal.integer && !Portal_CheckBox(sv.worldmodel, testeye, entmins, entmaxs))
605 // don't try to cull embedded brush models with this, they're sometimes huge (spanning several rooms)
606 if (sv_cullentities_trace.integer && (model == NULL || model->type != mod_brush || model->name[0] != '*'))
608 // LordHavoc: test random offsets, to maximize chance of detection
609 testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
610 testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
611 testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
613 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
615 if (trace.fraction == 1)
616 client->visibletime[e] = realtime + 1;
619 //test nearest point on bbox
620 testorigin[0] = bound(entmins[0], testeye[0], entmaxs[0]);
621 testorigin[1] = bound(entmins[1], testeye[1], entmaxs[1]);
622 testorigin[2] = bound(entmins[2], testeye[2], entmaxs[2]);
624 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
626 if (trace.fraction == 1)
627 client->visibletime[e] = realtime + 1;
628 else if (realtime > client->visibletime[e])
641 effects = ent->v->effects;
643 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
644 if (val->_float != 0)
645 alphaf = val->_float * 255.0f;
648 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
649 if (val->_float != 0)
650 alphaf = val->_float;
654 alpha = bound(0, alphaf, 255);
656 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
657 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
658 if (scale < 0) scale = 0;
659 if (scale > 255) scale = 255;
661 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
662 if (val->_float != 0)
663 glowcolor = (int) val->_float;
665 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
666 if (val->_float != 0)
667 effects |= EF_FULLBRIGHT;
671 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
675 // don't send if flagged for NODRAW and there are no effects
676 if (model->flags == 0 && ((effects & EF_NODRAW) || scale <= 0 || alpha <= 0))
679 else // no model and no effects
684 if (msg->maxsize - msg->cursize < 32) // LordHavoc: increased check from 16 to 32
686 Con_Printf ("packet overflow\n");
687 // mark the rest of the entities so they can't be delta compressed against this frame
688 for (;e < sv.num_edicts;e++)
690 client->nextfullupdate[e] = -1;
691 client->visibletime[e] = -1;
696 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
697 bits = bits | U_EXTERIORMODEL;
700 baseline = &ent->e->baseline;
702 if (((int)ent->v->effects & EF_DELTA) && sv_deltacompress.integer)
704 // every half second a full update is forced
705 if (realtime < client->nextfullupdate[e])
708 baseline = &ent->e->deltabaseline;
711 nextfullupdate = realtime + 0.5f;
714 nextfullupdate = realtime + 0.5f;
716 // restore nextfullupdate since this is being sent for real
717 client->nextfullupdate[e] = nextfullupdate;
720 bits |= U_LONGENTITY;
722 if (ent->v->movetype == MOVETYPE_STEP)
725 // LordHavoc: old stuff, but rewritten to have more exact tolerances
726 if (origin[0] != baseline->origin[0]) bits |= U_ORIGIN1;
727 if (origin[1] != baseline->origin[1]) bits |= U_ORIGIN2;
728 if (origin[2] != baseline->origin[2]) bits |= U_ORIGIN3;
729 if (((int)(angles[0]*(256.0/360.0)) & 255) != ((int)(baseline->angles[0]*(256.0/360.0)) & 255)) bits |= U_ANGLE1;
730 if (((int)(angles[1]*(256.0/360.0)) & 255) != ((int)(baseline->angles[1]*(256.0/360.0)) & 255)) bits |= U_ANGLE2;
731 if (((int)(angles[2]*(256.0/360.0)) & 255) != ((int)(baseline->angles[2]*(256.0/360.0)) & 255)) bits |= U_ANGLE3;
732 if (baseline->colormap != (qbyte) ent->v->colormap) bits |= U_COLORMAP;
733 if (baseline->skin != (qbyte) ent->v->skin) bits |= U_SKIN;
734 if ((baseline->frame & 0x00FF) != ((int) ent->v->frame & 0x00FF)) bits |= U_FRAME;
735 if ((baseline->effects & 0x00FF) != ((int) ent->v->effects & 0x00FF)) bits |= U_EFFECTS;
736 if ((baseline->modelindex & 0x00FF) != ((int) ent->v->modelindex & 0x00FF)) bits |= U_MODEL;
738 // LordHavoc: new stuff
739 if (baseline->alpha != alpha) bits |= U_ALPHA;
740 if (baseline->scale != scale) bits |= U_SCALE;
741 if (((int) baseline->effects & 0xFF00) != ((int) ent->v->effects & 0xFF00)) bits |= U_EFFECTS2;
742 if (baseline->glowsize != glowsize) bits |= U_GLOWSIZE;
743 if (baseline->glowcolor != glowcolor) bits |= U_GLOWCOLOR;
744 if (((int) baseline->frame & 0xFF00) != ((int) ent->v->frame & 0xFF00)) bits |= U_FRAME2;
745 if (((int) baseline->frame & 0xFF00) != ((int) ent->v->modelindex & 0xFF00)) bits |= U_MODEL2;
747 // update delta baseline
748 VectorCopy(ent->v->origin, ent->e->deltabaseline.origin);
749 VectorCopy(ent->v->angles, ent->e->deltabaseline.angles);
750 ent->e->deltabaseline.colormap = ent->v->colormap;
751 ent->e->deltabaseline.skin = ent->v->skin;
752 ent->e->deltabaseline.frame = ent->v->frame;
753 ent->e->deltabaseline.effects = ent->v->effects;
754 ent->e->deltabaseline.modelindex = ent->v->modelindex;
755 ent->e->deltabaseline.alpha = alpha;
756 ent->e->deltabaseline.scale = scale;
757 ent->e->deltabaseline.glowsize = glowsize;
758 ent->e->deltabaseline.glowcolor = glowcolor;
761 if (bits >= 16777216)
769 MSG_WriteByte (msg, bits);
770 if (bits & U_MOREBITS)
771 MSG_WriteByte (msg, bits>>8);
772 // LordHavoc: extend bytes have to be written here due to delta compression
773 if (bits & U_EXTEND1)
774 MSG_WriteByte (msg, bits>>16);
775 if (bits & U_EXTEND2)
776 MSG_WriteByte (msg, bits>>24);
778 // LordHavoc: old stuff
779 if (bits & U_LONGENTITY)
780 MSG_WriteShort (msg,e);
782 MSG_WriteByte (msg,e);
783 if (bits & U_MODEL) MSG_WriteByte (msg, ent->v->modelindex);
784 if (bits & U_FRAME) MSG_WriteByte (msg, ent->v->frame);
785 if (bits & U_COLORMAP) MSG_WriteByte (msg, ent->v->colormap);
786 if (bits & U_SKIN) MSG_WriteByte (msg, ent->v->skin);
787 if (bits & U_EFFECTS) MSG_WriteByte (msg, ent->v->effects);
788 if (bits & U_ORIGIN1) MSG_WriteDPCoord (msg, origin[0]);
789 if (bits & U_ANGLE1) MSG_WriteAngle(msg, angles[0]);
790 if (bits & U_ORIGIN2) MSG_WriteDPCoord (msg, origin[1]);
791 if (bits & U_ANGLE2) MSG_WriteAngle(msg, angles[1]);
792 if (bits & U_ORIGIN3) MSG_WriteDPCoord (msg, origin[2]);
793 if (bits & U_ANGLE3) MSG_WriteAngle(msg, angles[2]);
795 // LordHavoc: new stuff
796 if (bits & U_ALPHA) MSG_WriteByte(msg, alpha);
797 if (bits & U_SCALE) MSG_WriteByte(msg, scale);
798 if (bits & U_EFFECTS2) MSG_WriteByte(msg, (int)ent->v->effects >> 8);
799 if (bits & U_GLOWSIZE) MSG_WriteByte(msg, glowsize);
800 if (bits & U_GLOWCOLOR) MSG_WriteByte(msg, glowcolor);
801 if (bits & U_FRAME2) MSG_WriteByte(msg, (int)ent->v->frame >> 8);
802 if (bits & U_MODEL2) MSG_WriteByte(msg, (int)ent->v->modelindex >> 8);
805 if (sv_cullentities_stats.integer)
806 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);
809 static entity_frame_t sv_writeentitiestoclient_entityframe;
810 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
812 int e, clentnum, flags, alpha, glowcolor, glowsize, scale, effects, modelindex;
813 int culled_pvs, culled_portal, culled_trace, visibleentities, totalentities;
814 float alphaf, lightsize;
816 vec3_t origin, angles, entmins, entmaxs, lightmins, lightmaxs, testorigin, testeye;
823 Mod_CheckLoaded(sv.worldmodel);
825 // find the client's PVS
826 // the real place being tested from
827 VectorAdd (clent->v->origin, clent->v->view_ofs, testeye);
828 pvs = SV_FatPVS (testeye);
830 // the place being reported (to consider the fact the client still
831 // applies the view_ofs[2], so we have to only send the fractional part
832 // of view_ofs[2], undoing what the client will redo)
833 VectorCopy (testeye, testorigin);
834 e = (int) clent->v->view_ofs[2] & 255;
837 testorigin[2] -= (float) e;
838 EntityFrame_Clear(&sv_writeentitiestoclient_entityframe, testorigin);
846 clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
847 // send all entities that touch the pvs
848 ent = NEXT_EDICT(sv.edicts);
849 for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
855 if (ent != clent) // LordHavoc: always send player
857 if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
859 if (val->edict == clentnum)
860 flags |= RENDER_VIEWMODEL; // show relative to the view
863 // don't show to anyone else
869 // LordHavoc: never draw something told not to display to this client
870 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
872 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
878 effects = ent->v->effects;
879 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
880 glowsize = (int) val->_float >> 2;
881 glowsize = bound(0, glowsize, 255);
884 if (effects & (EF_BRIGHTFIELD | EF_MUZZLEFLASH | EF_BRIGHTLIGHT | EF_DIMLIGHT | EF_RED | EF_BLUE | EF_FLAME | EF_STARDUST))
886 if (effects & EF_BRIGHTFIELD)
887 lightsize = max(lightsize, 80);
888 if (effects & EF_MUZZLEFLASH)
889 lightsize = max(lightsize, 100);
890 if (effects & EF_BRIGHTLIGHT)
891 lightsize = max(lightsize, 400);
892 if (effects & EF_DIMLIGHT)
893 lightsize = max(lightsize, 200);
894 if (effects & EF_RED)
895 lightsize = max(lightsize, 200);
896 if (effects & EF_BLUE)
897 lightsize = max(lightsize, 200);
898 if (effects & EF_FLAME)
899 lightsize = max(lightsize, 250);
900 if (effects & EF_STARDUST)
901 lightsize = max(lightsize, 100);
904 lightsize = max(lightsize, glowsize << 2);
906 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
907 if (val->_float != 0)
909 flags |= RENDER_GLOWTRAIL;
910 lightsize = max(lightsize, 100);
914 if (ent->v->modelindex >= 0 && ent->v->modelindex < MAX_MODELS && *PR_GetString(ent->v->model))
916 modelindex = ent->v->modelindex;
917 model = sv.models[(int)ent->v->modelindex];
918 Mod_CheckLoaded(model);
923 if (ent != clent) // LordHavoc: always send player
924 if (lightsize == 0) // no effects
928 VectorCopy(ent->v->angles, angles);
929 VectorCopy(ent->v->origin, origin);
931 // ent has survived every check so far, check if it is visible
932 // always send embedded brush models, they don't generate much traffic
933 if (ent != clent && ((flags & RENDER_VIEWMODEL) == 0) && (model == NULL || model->type != mod_brush || model->name[0] != '*'))
935 // use the predicted origin
936 entmins[0] = origin[0] - 1.0f;
937 entmins[1] = origin[1] - 1.0f;
938 entmins[2] = origin[2] - 1.0f;
939 entmaxs[0] = origin[0] + 1.0f;
940 entmaxs[1] = origin[1] + 1.0f;
941 entmaxs[2] = origin[2] + 1.0f;
942 // using the model's bounding box to ensure things are visible regardless of their physics box
945 if (ent->v->angles[0] || ent->v->angles[2]) // pitch and roll
947 VectorAdd(entmins, model->rotatedmins, entmins);
948 VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
950 else if (ent->v->angles[1])
952 VectorAdd(entmins, model->yawmins, entmins);
953 VectorAdd(entmaxs, model->yawmaxs, entmaxs);
957 VectorAdd(entmins, model->normalmins, entmins);
958 VectorAdd(entmaxs, model->normalmaxs, entmaxs);
961 lightmins[0] = min(entmins[0], origin[0] - lightsize);
962 lightmins[1] = min(entmins[1], origin[1] - lightsize);
963 lightmins[2] = min(entmins[2], origin[2] - lightsize);
964 lightmaxs[0] = min(entmaxs[0], origin[0] + lightsize);
965 lightmaxs[1] = min(entmaxs[1], origin[1] + lightsize);
966 lightmaxs[2] = min(entmaxs[2], origin[2] + lightsize);
970 // if not touching a visible leaf
971 if (sv_cullentities_pvs.integer && !SV_BoxTouchingPVS(pvs, lightmins, lightmaxs, sv.worldmodel->nodes))
977 // or not visible through the portals
978 if (sv_cullentities_portal.integer && !Portal_CheckBox(sv.worldmodel, testeye, lightmins, lightmaxs))
984 if (sv_cullentities_trace.integer)
986 // LordHavoc: test center first
987 testorigin[0] = (entmins[0] + entmaxs[0]) * 0.5f;
988 testorigin[1] = (entmins[1] + entmaxs[1]) * 0.5f;
989 testorigin[2] = (entmins[2] + entmaxs[2]) * 0.5f;
990 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
991 if (trace.fraction == 1)
992 client->visibletime[e] = realtime + 1;
995 // LordHavoc: test random offsets, to maximize chance of detection
996 testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
997 testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
998 testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
999 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
1000 if (trace.fraction == 1)
1001 client->visibletime[e] = realtime + 1;
1006 // LordHavoc: test random offsets, to maximize chance of detection
1007 testorigin[0] = lhrandom(lightmins[0], lightmaxs[0]);
1008 testorigin[1] = lhrandom(lightmins[1], lightmaxs[1]);
1009 testorigin[2] = lhrandom(lightmins[2], lightmaxs[2]);
1010 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
1011 if (trace.fraction == 1)
1012 client->visibletime[e] = realtime + 1;
1015 if (realtime > client->visibletime[e])
1024 if (realtime > client->visibletime[e])
1039 effects = ent->v->effects;
1041 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
1042 if (val->_float != 0)
1043 alphaf = val->_float * 255.0;
1046 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
1047 if (val->_float != 0)
1048 alphaf = val->_float;
1052 alpha = bound(0, alphaf, 255);
1054 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
1055 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
1056 if (scale < 0) scale = 0;
1057 if (scale > 255) scale = 255;
1059 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
1060 if (val->_float != 0)
1061 glowcolor = (int) val->_float;
1063 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
1064 if (val->_float != 0)
1065 effects |= EF_FULLBRIGHT;
1069 if (lightsize == 0) // no effects
1073 // don't send if flagged for NODRAW and there are no effects
1074 if (model->flags == 0 && ((effects & EF_NODRAW) || scale <= 0 || alpha <= 0))
1077 else // no model and no effects
1082 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
1083 flags |= RENDER_EXTERIORMODEL;
1085 if (ent->v->movetype == MOVETYPE_STEP)
1086 flags |= RENDER_STEP;
1087 // don't send an entity if it's coordinates would wrap around
1088 if ((effects & EF_LOWPRECISION) && origin[0] >= -32768 && origin[1] >= -32768 && origin[2] >= -32768 && origin[0] <= 32767 && origin[1] <= 32767 && origin[2] <= 32767)
1089 flags |= RENDER_LOWPRECISION;
1091 s = EntityFrame_NewEntity(&sv_writeentitiestoclient_entityframe, e);
1092 // if we run out of space, abort
1095 VectorCopy(origin, s->origin);
1096 VectorCopy(angles, s->angles);
1097 if (ent->v->colormap >= 1024)
1098 flags |= RENDER_COLORMAPPED;
1099 s->colormap = ent->v->colormap;
1100 s->skin = ent->v->skin;
1101 s->frame = ent->v->frame;
1102 s->modelindex = modelindex;
1103 s->effects = effects;
1106 s->glowsize = glowsize;
1107 s->glowcolor = glowcolor;
1110 sv_writeentitiestoclient_entityframe.framenum = ++client->entityframenumber;
1111 EntityFrame_Write(&client->entitydatabase, &sv_writeentitiestoclient_entityframe, msg);
1113 if (sv_cullentities_stats.integer)
1114 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);
1124 void SV_CleanupEnts (void)
1129 ent = NEXT_EDICT(sv.edicts);
1130 for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
1131 ent->v->effects = (int)ent->v->effects & ~EF_MUZZLEFLASH;
1136 SV_WriteClientdataToMessage
1140 void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
1151 // send a damage message
1153 if (ent->v->dmg_take || ent->v->dmg_save)
1155 other = PROG_TO_EDICT(ent->v->dmg_inflictor);
1156 MSG_WriteByte (msg, svc_damage);
1157 MSG_WriteByte (msg, ent->v->dmg_save);
1158 MSG_WriteByte (msg, ent->v->dmg_take);
1159 for (i=0 ; i<3 ; i++)
1160 MSG_WriteDPCoord (msg, other->v->origin[i] + 0.5*(other->v->mins[i] + other->v->maxs[i]));
1162 ent->v->dmg_take = 0;
1163 ent->v->dmg_save = 0;
1167 // send the current viewpos offset from the view entity
1169 SV_SetIdealPitch (); // how much to look up / down ideally
1171 // a fixangle might get lost in a dropped packet. Oh well.
1172 if ( ent->v->fixangle )
1174 MSG_WriteByte (msg, svc_setangle);
1175 for (i=0 ; i < 3 ; i++)
1176 MSG_WriteAngle (msg, ent->v->angles[i] );
1177 ent->v->fixangle = 0;
1182 if (ent->v->view_ofs[2] != DEFAULT_VIEWHEIGHT)
1183 bits |= SU_VIEWHEIGHT;
1185 if (ent->v->idealpitch)
1186 bits |= SU_IDEALPITCH;
1188 // stuff the sigil bits into the high bits of items for sbar, or else
1190 val = GETEDICTFIELDVALUE(ent, eval_items2);
1193 items = (int)ent->v->items | ((int)val->_float << 23);
1195 items = (int)ent->v->items | ((int)pr_global_struct->serverflags << 28);
1199 if ( (int)ent->v->flags & FL_ONGROUND)
1200 bits |= SU_ONGROUND;
1202 if ( ent->v->waterlevel >= 2)
1206 VectorClear(punchvector);
1207 if ((val = GETEDICTFIELDVALUE(ent, eval_punchvector)))
1208 VectorCopy(val->vector, punchvector);
1211 if ((val = GETEDICTFIELDVALUE(ent, eval_viewzoom)))
1213 i = val->_float * 255.0f;
1217 i = bound(0, i, 255);
1221 if (viewzoom != 255)
1222 bits |= SU_VIEWZOOM;
1224 for (i=0 ; i<3 ; i++)
1226 if (ent->v->punchangle[i])
1227 bits |= (SU_PUNCH1<<i);
1228 if (punchvector[i]) // dpprotocol
1229 bits |= (SU_PUNCHVEC1<<i); // dpprotocol
1230 if (ent->v->velocity[i])
1231 bits |= (SU_VELOCITY1<<i);
1234 if (ent->v->weaponframe)
1235 bits |= SU_WEAPONFRAME;
1237 if (ent->v->armorvalue)
1244 if (bits >= 16777216)
1249 MSG_WriteByte (msg, svc_clientdata);
1250 MSG_WriteShort (msg, bits);
1251 if (bits & SU_EXTEND1)
1252 MSG_WriteByte(msg, bits >> 16);
1253 if (bits & SU_EXTEND2)
1254 MSG_WriteByte(msg, bits >> 24);
1256 if (bits & SU_VIEWHEIGHT)
1257 MSG_WriteChar (msg, ent->v->view_ofs[2]);
1259 if (bits & SU_IDEALPITCH)
1260 MSG_WriteChar (msg, ent->v->idealpitch);
1262 for (i=0 ; i<3 ; i++)
1264 if (bits & (SU_PUNCH1<<i))
1265 MSG_WritePreciseAngle(msg, ent->v->punchangle[i]); // dpprotocol
1266 if (bits & (SU_PUNCHVEC1<<i)) // dpprotocol
1267 MSG_WriteDPCoord(msg, punchvector[i]); // dpprotocol
1268 if (bits & (SU_VELOCITY1<<i))
1269 MSG_WriteChar (msg, ent->v->velocity[i]/16);
1272 // [always sent] if (bits & SU_ITEMS)
1273 MSG_WriteLong (msg, items);
1275 if (bits & SU_WEAPONFRAME)
1276 MSG_WriteByte (msg, ent->v->weaponframe);
1277 if (bits & SU_ARMOR)
1278 MSG_WriteByte (msg, ent->v->armorvalue);
1279 if (bits & SU_WEAPON)
1280 MSG_WriteByte (msg, SV_ModelIndex(PR_GetString(ent->v->weaponmodel)));
1282 MSG_WriteShort (msg, ent->v->health);
1283 MSG_WriteByte (msg, ent->v->currentammo);
1284 MSG_WriteByte (msg, ent->v->ammo_shells);
1285 MSG_WriteByte (msg, ent->v->ammo_nails);
1286 MSG_WriteByte (msg, ent->v->ammo_rockets);
1287 MSG_WriteByte (msg, ent->v->ammo_cells);
1289 if (gamemode == GAME_HIPNOTIC || gamemode == GAME_ROGUE)
1293 if ( ((int)ent->v->weapon) & (1<<i) )
1295 MSG_WriteByte (msg, i);
1302 MSG_WriteByte (msg, ent->v->weapon);
1305 if (bits & SU_VIEWZOOM)
1306 MSG_WriteByte (msg, viewzoom);
1310 =======================
1311 SV_SendClientDatagram
1312 =======================
1314 static qbyte sv_sendclientdatagram_buf[MAX_DATAGRAM]; // FIXME?
1315 qboolean SV_SendClientDatagram (client_t *client)
1319 msg.data = sv_sendclientdatagram_buf;
1320 msg.maxsize = sizeof(sv_sendclientdatagram_buf);
1323 MSG_WriteByte (&msg, svc_time);
1324 MSG_WriteFloat (&msg, sv.time);
1326 // add the client specific data to the datagram
1327 SV_WriteClientdataToMessage (client->edict, &msg);
1329 SV_WriteEntitiesToClient (client, client->edict, &msg);
1331 // copy the server datagram if there is space
1332 if (msg.cursize + sv.datagram.cursize < msg.maxsize)
1333 SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
1335 // send the datagram
1336 if (NetConn_SendUnreliableMessage (client->netconnection, &msg) == -1)
1338 SV_DropClient (true);// if the message couldn't send, kick off
1346 =======================
1347 SV_UpdateToReliableMessages
1348 =======================
1350 void SV_UpdateToReliableMessages (void)
1357 // check for changes to be sent over the reliable streams
1358 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1360 // update the host_client fields we care about according to the entity fields
1361 sv_player = host_client->edict;
1362 s = PR_GetString(sv_player->v->netname);
1363 if (s != host_client->name)
1367 // point the string back at host_client->name to keep it safe
1368 strncpy(host_client->name, s, sizeof(host_client->name) - 1);
1369 sv_player->v->netname = PR_SetString(host_client->name);
1371 if ((val = GETEDICTFIELDVALUE(sv_player, eval_clientcolors)) && host_client->colors != val->_float)
1372 host_client->colors = val->_float;
1373 host_client->frags = sv_player->v->frags;
1374 if (gamemode == GAME_NEHAHRA)
1375 if ((val = GETEDICTFIELDVALUE(sv_player, eval_pmodel)) && host_client->pmodel != val->_float)
1376 host_client->pmodel = val->_float;
1378 // if the fields changed, send messages about the changes
1379 if (strcmp(host_client->old_name, host_client->name))
1381 strcpy(host_client->old_name, host_client->name);
1382 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1384 if (!client->active || !client->spawned)
1386 MSG_WriteByte (&client->message, svc_updatename);
1387 MSG_WriteByte (&client->message, i);
1388 MSG_WriteString (&client->message, host_client->name);
1391 if (host_client->old_colors != host_client->colors)
1393 host_client->old_colors = host_client->colors;
1394 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1396 if (!client->active || !client->spawned)
1398 MSG_WriteByte (&client->message, svc_updatecolors);
1399 MSG_WriteByte (&client->message, i);
1400 MSG_WriteByte (&client->message, host_client->colors);
1403 if (host_client->old_frags != host_client->frags)
1405 host_client->old_frags = host_client->frags;
1406 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1408 if (!client->active || !client->spawned)
1410 MSG_WriteByte (&client->message, svc_updatefrags);
1411 MSG_WriteByte (&client->message, i);
1412 MSG_WriteShort (&client->message, host_client->frags);
1417 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1419 if (!client->active)
1421 SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
1424 SZ_Clear (&sv.reliable_datagram);
1429 =======================
1432 Send a nop message without trashing or sending the accumulated client
1434 =======================
1436 void SV_SendNop (client_t *client)
1442 msg.maxsize = sizeof(buf);
1445 MSG_WriteChar (&msg, svc_nop);
1447 if (NetConn_SendUnreliableMessage (client->netconnection, &msg) == -1)
1448 SV_DropClient (true); // if the message couldn't send, kick off
1449 client->last_message = realtime;
1453 =======================
1454 SV_SendClientMessages
1455 =======================
1457 void SV_SendClientMessages (void)
1461 // update frags, names, etc
1462 SV_UpdateToReliableMessages ();
1464 // build individual updates
1465 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1467 if (!host_client->active)
1470 if (host_client->deadsocket)
1472 SV_DropClient (true); // if the message couldn't send, kick off
1476 if (host_client->spawned)
1478 if (!SV_SendClientDatagram (host_client))
1483 // the player isn't totally in the game yet
1484 // send small keepalive messages if too much time has passed
1485 // send a full message when the next signon stage has been requested
1486 // some other message data (name changes, etc) may accumulate
1487 // between signon stages
1488 if (!host_client->sendsignon)
1490 if (realtime - host_client->last_message > 5)
1491 SV_SendNop (host_client);
1492 continue; // don't send out non-signon messages
1496 // check for an overflowed message. Should only happen
1497 // on a very fucked up connection that backs up a lot, then
1499 if (host_client->message.overflowed)
1501 SV_DropClient (true); // overflowed
1502 host_client->message.overflowed = false;
1506 if (host_client->message.cursize || host_client->dropasap)
1508 if (!NetConn_CanSendMessage (host_client->netconnection))
1511 if (host_client->dropasap)
1512 SV_DropClient (false); // went to another level
1515 if (NetConn_SendReliableMessage (host_client->netconnection, &host_client->message) == -1)
1516 SV_DropClient (true); // if the message couldn't send, kick off
1517 SZ_Clear (&host_client->message);
1518 host_client->last_message = realtime;
1519 host_client->sendsignon = false;
1525 // clear muzzle flashes
1531 ==============================================================================
1535 ==============================================================================
1544 int SV_ModelIndex (const char *name)
1548 if (!name || !name[0])
1551 for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
1552 if (!strcmp(sv.model_precache[i], name))
1554 if (i==MAX_MODELS || !sv.model_precache[i])
1555 Host_Error ("SV_ModelIndex: model %s not precached", name);
1559 #ifdef SV_QUAKEENTITIES
1566 void SV_CreateBaseline (void)
1568 int i, entnum, large;
1571 // LordHavoc: clear *all* states (note just active ones)
1572 for (entnum = 0;entnum < sv.max_edicts;entnum++)
1574 // get the current server version
1575 svent = EDICT_NUM(entnum);
1577 // LordHavoc: always clear state values, whether the entity is in use or not
1578 ClearStateToDefault(&svent->e->baseline);
1582 if (entnum > svs.maxclients && !svent->v->modelindex)
1585 // create entity baseline
1586 VectorCopy (svent->v->origin, svent->e->baseline.origin);
1587 VectorCopy (svent->v->angles, svent->e->baseline.angles);
1588 svent->e->baseline.frame = svent->v->frame;
1589 svent->e->baseline.skin = svent->v->skin;
1590 if (entnum > 0 && entnum <= svs.maxclients)
1592 svent->e->baseline.colormap = entnum;
1593 svent->e->baseline.modelindex = SV_ModelIndex("progs/player.mdl");
1597 svent->e->baseline.colormap = 0;
1598 svent->e->baseline.modelindex = svent->v->modelindex;
1602 if (svent->e->baseline.modelindex & 0xFF00 || svent->e->baseline.frame & 0xFF00)
1605 // add to the message
1607 MSG_WriteByte (&sv.signon, svc_spawnbaseline2);
1609 MSG_WriteByte (&sv.signon, svc_spawnbaseline);
1610 MSG_WriteShort (&sv.signon, entnum);
1614 MSG_WriteShort (&sv.signon, svent->e->baseline.modelindex);
1615 MSG_WriteShort (&sv.signon, svent->e->baseline.frame);
1619 MSG_WriteByte (&sv.signon, svent->e->baseline.modelindex);
1620 MSG_WriteByte (&sv.signon, svent->e->baseline.frame);
1622 MSG_WriteByte (&sv.signon, svent->e->baseline.colormap);
1623 MSG_WriteByte (&sv.signon, svent->e->baseline.skin);
1624 for (i=0 ; i<3 ; i++)
1626 MSG_WriteDPCoord(&sv.signon, svent->e->baseline.origin[i]);
1627 MSG_WriteAngle(&sv.signon, svent->e->baseline.angles[i]);
1638 Tell all the clients that the server is changing levels
1641 void SV_SendReconnect (void)
1648 msg.maxsize = sizeof(data);
1650 MSG_WriteChar (&msg, svc_stufftext);
1651 MSG_WriteString (&msg, "reconnect\n");
1652 NetConn_SendToAll (&msg, 5);
1654 if (cls.state != ca_dedicated)
1655 Cmd_ExecuteString ("reconnect\n", src_command);
1663 Grabs the current state of each client for saving across the
1664 transition to another level
1667 void SV_SaveSpawnparms (void)
1671 svs.serverflags = pr_global_struct->serverflags;
1673 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1675 if (!host_client->active)
1678 // call the progs to get default spawn parms for the new client
1679 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1680 PR_ExecuteProgram (pr_global_struct->SetChangeParms, "QC function SetChangeParms is missing");
1681 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1682 host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1686 void SV_IncreaseEdicts(void)
1690 int oldmax_edicts = sv.max_edicts;
1691 void *oldedictsengineprivate = sv.edictsengineprivate;
1692 void *oldedictsfields = sv.edictsfields;
1693 void *oldmoved_edicts = sv.moved_edicts;
1695 // links don't survive the transition, so unlink everything
1696 for (i = 0;i < sv.max_edicts;i++)
1697 SV_UnlinkEdict (sv.edicts + i);
1700 sv.max_edicts = min(sv.max_edicts + 32, MAX_EDICTS);
1701 sv.edictsengineprivate = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_engineprivate_t));
1702 sv.edictsfields = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1703 sv.moved_edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_t *));
1705 memcpy(sv.edictsengineprivate, oldedictsengineprivate, oldmax_edicts * sizeof(edict_engineprivate_t));
1706 memcpy(sv.edictsfields, oldedictsfields, oldmax_edicts * pr_edict_size);
1708 for (i = 0;i < sv.max_edicts;i++)
1710 ent = sv.edicts + i;
1711 ent->e = sv.edictsengineprivate + i;
1712 ent->v = (void *)((qbyte *)sv.edictsfields + i * pr_edict_size);
1713 // link every entity except world
1715 SV_LinkEdict(ent, false);
1718 Mem_Free(oldedictsengineprivate);
1719 Mem_Free(oldedictsfields);
1720 Mem_Free(oldmoved_edicts);
1727 This is called at the start of each level
1730 extern float scr_centertime_off;
1732 void SV_SpawnServer (const char *server)
1738 // let's not have any servers with no name
1739 if (hostname.string[0] == 0)
1740 Cvar_Set ("hostname", "UNNAMED");
1741 scr_centertime_off = 0;
1743 Con_DPrintf ("SpawnServer: %s\n",server);
1744 svs.changelevel_issued = false; // now safe to issue another
1747 // tell all connected clients that we are going to a new level
1750 SV_SendReconnect ();
1752 NetConn_OpenServerPorts(svs.maxclients > 1);
1755 // make cvars consistant
1758 Cvar_SetValue ("deathmatch", 0);
1759 current_skill = bound(0, (int)(skill.value + 0.5), 3);
1761 Cvar_SetValue ("skill", (float)current_skill);
1764 // set up the new server
1766 Host_ClearMemory ();
1768 memset (&sv, 0, sizeof(sv));
1770 strcpy (sv.name, server);
1772 // load progs to get entity field count
1775 // allocate server memory
1776 // start out with just enough room for clients and a reasonable estimate of entities
1777 sv.max_edicts = ((svs.maxclients + 128) + 31) & ~31;
1778 sv.max_edicts = min(sv.max_edicts, MAX_EDICTS);
1780 // clear the edict memory pool
1781 Mem_EmptyPool(sv_edicts_mempool);
1782 // edict_t structures (hidden from progs)
1783 sv.edicts = Mem_Alloc(sv_edicts_mempool, MAX_EDICTS * sizeof(edict_t));
1784 // engine private structures (hidden from progs)
1785 sv.edictsengineprivate = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_engineprivate_t));
1786 // progs fields, often accessed by server
1787 sv.edictsfields = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1788 // used by PushMove to move back pushed entities
1789 sv.moved_edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_t *));
1790 for (i = 0;i < sv.max_edicts;i++)
1792 ent = sv.edicts + i;
1793 ent->e = sv.edictsengineprivate + i;
1794 ent->v = (void *)((qbyte *)sv.edictsfields + i * pr_edict_size);
1797 sv.datagram.maxsize = sizeof(sv.datagram_buf);
1798 sv.datagram.cursize = 0;
1799 sv.datagram.data = sv.datagram_buf;
1801 sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1802 sv.reliable_datagram.cursize = 0;
1803 sv.reliable_datagram.data = sv.reliable_datagram_buf;
1805 sv.signon.maxsize = sizeof(sv.signon_buf);
1806 sv.signon.cursize = 0;
1807 sv.signon.data = sv.signon_buf;
1809 // leave slots at start for clients only
1810 sv.num_edicts = svs.maxclients+1;
1811 for (i=0 ; i<svs.maxclients ; i++)
1812 svs.clients[i].edict = EDICT_NUM(i+1);
1814 sv.state = ss_loading;
1821 strcpy (sv.name, server);
1822 sprintf (sv.modelname,"maps/%s.bsp", server);
1823 sv.worldmodel = Mod_ForName(sv.modelname, false, true, true);
1826 Con_Printf ("Couldn't spawn server %s\n", sv.modelname);
1830 sv.models[1] = sv.worldmodel;
1833 // clear world interaction links
1837 sv.sound_precache[0] = "";
1839 sv.model_precache[0] = "";
1840 sv.model_precache[1] = sv.modelname;
1841 for (i = 1;i < sv.worldmodel->numsubmodels;i++)
1843 sv.model_precache[i+1] = localmodels[i];
1844 sv.models[i+1] = Mod_ForName (localmodels[i], false, false, false);
1848 // load the rest of the entities
1851 memset (ent->v, 0, progs->entityfields * 4);
1852 ent->e->free = false;
1853 ent->v->model = PR_SetString(sv.modelname);
1854 ent->v->modelindex = 1; // world model
1855 ent->v->solid = SOLID_BSP;
1856 ent->v->movetype = MOVETYPE_PUSH;
1859 pr_global_struct->coop = coop.integer;
1861 pr_global_struct->deathmatch = deathmatch.integer;
1863 pr_global_struct->mapname = PR_SetString(sv.name);
1865 // serverflags are for cross level information (sigils)
1866 pr_global_struct->serverflags = svs.serverflags;
1868 // load replacement entity file if found
1870 if (sv_entpatch.integer)
1871 entities = FS_LoadFile(va("maps/%s.ent", sv.name), true);
1874 Con_Printf("Loaded maps/%s.ent\n", sv.name);
1875 ED_LoadFromFile (entities);
1879 ED_LoadFromFile (sv.worldmodel->entities);
1882 // LordHavoc: clear world angles (to fix e3m3.bsp)
1883 VectorClear(sv.edicts->v->angles);
1887 // all setup is completed, any further precache statements are errors
1888 sv.state = ss_active;
1890 // run two frames to allow everything to settle
1891 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1893 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1898 #ifdef QUAKEENTITIES
1899 // create a baseline for more efficient communications
1900 SV_CreateBaseline ();
1903 // send serverinfo to all connected clients
1904 for (i=0,host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1905 if (host_client->active)
1906 SV_SendServerinfo (host_client);
1908 Con_DPrintf ("Server spawned.\n");
1909 NetConn_Heartbeat (2);