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, float attenuation)
168 int sound_num, field_mask, i, ent;
170 if (volume < 0 || volume > 255)
171 Host_Error ("SV_StartSound: volume = %i", volume);
173 if (attenuation < 0 || attenuation > 4)
174 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
176 if (channel < 0 || channel > 7)
177 Host_Error ("SV_StartSound: channel = %i", channel);
179 if (sv.datagram.cursize > MAX_DATAGRAM-16)
182 // find precache number for sound
183 for (sound_num=1 ; sound_num<MAX_SOUNDS && sv.sound_precache[sound_num] ; sound_num++)
184 if (!strcmp(sample, sv.sound_precache[sound_num]))
187 if ( sound_num == MAX_SOUNDS || !sv.sound_precache[sound_num] )
189 Con_Printf ("SV_StartSound: %s not precached\n", sample);
193 ent = NUM_FOR_EDICT(entity);
196 if (volume != DEFAULT_SOUND_PACKET_VOLUME)
197 field_mask |= SND_VOLUME;
198 if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
199 field_mask |= SND_ATTENUATION;
201 field_mask |= SND_LARGEENTITY;
202 if (sound_num >= 256 || channel >= 8)
203 field_mask |= SND_LARGESOUND;
205 // directed messages go only to the entity they are targeted on
206 MSG_WriteByte (&sv.datagram, svc_sound);
207 MSG_WriteByte (&sv.datagram, field_mask);
208 if (field_mask & SND_VOLUME)
209 MSG_WriteByte (&sv.datagram, volume);
210 if (field_mask & SND_ATTENUATION)
211 MSG_WriteByte (&sv.datagram, attenuation*64);
212 if (field_mask & SND_LARGEENTITY)
214 MSG_WriteShort (&sv.datagram, ent);
215 MSG_WriteByte (&sv.datagram, channel);
218 MSG_WriteShort (&sv.datagram, (ent<<3) | channel);
219 if (field_mask & SND_LARGESOUND)
220 MSG_WriteShort (&sv.datagram, sound_num);
222 MSG_WriteByte (&sv.datagram, sound_num);
223 for (i = 0;i < 3;i++)
224 MSG_WriteDPCoord (&sv.datagram, entity->v->origin[i]+0.5*(entity->v->mins[i]+entity->v->maxs[i]));
228 ==============================================================================
232 ==============================================================================
239 Sends the first message from the server to a connected client.
240 This will be sent on the initial connection and upon each server load.
243 void SV_SendServerinfo (client_t *client)
248 // edicts get reallocated on level changes, so we need to update it here
249 client->edict = EDICT_NUM((client - svs.clients) + 1);
251 // LordHavoc: clear entityframe tracking
252 client->entityframenumber = 0;
253 EntityFrame_ClearDatabase(&client->entitydatabase);
255 MSG_WriteByte (&client->message, svc_print);
256 sprintf (message, "\002\nServer: %s build %s (progs %i crc)", gamename, buildstring, pr_crc);
257 MSG_WriteString (&client->message,message);
259 MSG_WriteByte (&client->message, svc_serverinfo);
260 MSG_WriteLong (&client->message, DPPROTOCOL_VERSION3);
261 MSG_WriteByte (&client->message, svs.maxclients);
263 if (!coop.integer && deathmatch.integer)
264 MSG_WriteByte (&client->message, GAME_DEATHMATCH);
266 MSG_WriteByte (&client->message, GAME_COOP);
268 MSG_WriteString (&client->message,PR_GetString(sv.edicts->v->message));
270 for (s = sv.model_precache+1 ; *s ; s++)
271 MSG_WriteString (&client->message, *s);
272 MSG_WriteByte (&client->message, 0);
274 for (s = sv.sound_precache+1 ; *s ; s++)
275 MSG_WriteString (&client->message, *s);
276 MSG_WriteByte (&client->message, 0);
279 MSG_WriteByte (&client->message, svc_cdtrack);
280 MSG_WriteByte (&client->message, sv.edicts->v->sounds);
281 MSG_WriteByte (&client->message, sv.edicts->v->sounds);
284 MSG_WriteByte (&client->message, svc_setview);
285 MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
287 MSG_WriteByte (&client->message, svc_signonnum);
288 MSG_WriteByte (&client->message, 1);
290 client->sendsignon = true;
291 client->spawned = false; // need prespawn, spawn, etc
298 Initializes a client_t for a new net connection. This will only be called
299 once for a player each game, not once for each level change.
302 void SV_ConnectClient (int clientnum, netconn_t *netconnection)
306 float spawn_parms[NUM_SPAWN_PARMS];
308 client = svs.clients + clientnum;
310 // set up the client_t
312 memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
313 memset (client, 0, sizeof(*client));
314 client->netconnection = netconnection;
316 Con_DPrintf("Client %s connected\n", client->netconnection->address);
318 strcpy(client->name, "unconnected");
319 strcpy(client->old_name, "unconnected");
320 client->active = true;
321 client->spawned = false;
322 client->edict = EDICT_NUM(clientnum+1);
323 client->message.data = client->msgbuf;
324 client->message.maxsize = sizeof(client->msgbuf);
325 client->message.allowoverflow = true; // we can catch it
328 memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
331 // call the progs to get default spawn parms for the new client
332 PR_ExecuteProgram (pr_global_struct->SetNewParms, "QC function SetNewParms is missing");
333 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
334 client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
337 SV_SendServerinfo (client);
342 ===============================================================================
346 ===============================================================================
355 void SV_ClearDatagram (void)
357 SZ_Clear (&sv.datagram);
361 =============================================================================
363 The PVS must include a small area around the client to allow head bobbing
364 or other small motion on the client side. Otherwise, a bob might cause an
365 entity that should be visible to not show up, especially when the bob
368 =============================================================================
372 qbyte fatpvs[MAX_MAP_LEAFS/8];
374 void SV_AddToFatPVS (vec3_t org, mnode_t *node)
383 // if this is a leaf, accumulate the pvs bits
384 if (node->contents < 0)
386 if (node->contents != CONTENTS_SOLID)
388 pvs = sv.worldmodel->brushq1.LeafPVS(sv.worldmodel, (mleaf_t *)node);
389 for (i=0 ; i<fatbytes ; i++)
396 d = DotProduct (org, plane->normal) - plane->dist;
398 node = node->children[0];
400 node = node->children[1];
403 SV_AddToFatPVS (org, node->children[0]);
404 node = node->children[1];
413 Calculates a PVS that is the inclusive or of all leafs within 8 pixels of the
417 qbyte *SV_FatPVS (vec3_t org)
419 fatbytes = (sv.worldmodel->brushq1.numleafs+31)>>3;
420 memset (fatpvs, 0, fatbytes);
421 SV_AddToFatPVS (org, sv.worldmodel->brushq1.nodes);
425 //=============================================================================
428 int SV_BoxTouchingPVS (qbyte *pvs, vec3_t mins, vec3_t maxs, mnode_t *node)
432 if (node->contents < 0)
435 if (node->contents == CONTENTS_SOLID)
437 leafnum = (mleaf_t *)node - sv.worldmodel->brushq1.leafs - 1;
438 return pvs[leafnum >> 3] & (1 << (leafnum & 7));
441 // node - recurse down the BSP tree
442 switch (BoxOnPlaneSide(mins, maxs, node->plane))
445 node = node->children[0];
448 node = node->children[1];
451 if (node->children[0]->contents != CONTENTS_SOLID)
452 if (SV_BoxTouchingPVS (pvs, mins, maxs, node->children[0]))
454 node = node->children[1];
464 SV_WriteEntitiesToClient
469 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
471 int e, clentnum, bits, alpha, glowcolor, glowsize, scale, effects, lightsize;
472 int culled_pvs, culled_portal, culled_trace, visibleentities, totalentities;
474 vec3_t origin, angles, entmins, entmaxs, testorigin, testeye;
475 float nextfullupdate, alphaf;
478 entity_state_t *baseline; // LordHavoc: delta or startup baseline
482 Mod_CheckLoaded(sv.worldmodel);
484 // find the client's PVS
485 VectorAdd (clent->v->origin, clent->v->view_ofs, testeye);
486 pvs = SV_FatPVS (testeye);
494 clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
495 // send all entities that touch the pvs
496 ent = NEXT_EDICT(sv.edicts);
497 for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
501 // prevent delta compression against this frame (unless actually sent, which will restore this later)
502 nextfullupdate = client->nextfullupdate[e];
503 client->nextfullupdate[e] = -1;
505 if (ent != clent) // LordHavoc: always send player
507 if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
509 if (val->edict != clentnum)
511 // don't show to anyone else
515 bits |= U_VIEWMODEL; // show relative to the view
519 // LordHavoc: never draw something told not to display to this client
520 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
522 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
529 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
530 glowsize = (int) val->_float >> 2;
531 if (glowsize > 255) glowsize = 255;
532 if (glowsize < 0) glowsize = 0;
534 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
535 if (val->_float != 0)
538 if (ent->v->modelindex >= 0 && ent->v->modelindex < MAX_MODELS && *PR_GetString(ent->v->model))
540 model = sv.models[(int)ent->v->modelindex];
541 Mod_CheckLoaded(model);
546 if (ent != clent) // LordHavoc: always send player
547 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
551 VectorCopy(ent->v->angles, angles);
552 VectorCopy(ent->v->origin, origin);
554 // ent has survived every check so far, check if it is visible
555 if (ent != clent && ((bits & U_VIEWMODEL) == 0))
557 // use the predicted origin
558 entmins[0] = origin[0] - 1.0f;
559 entmins[1] = origin[1] - 1.0f;
560 entmins[2] = origin[2] - 1.0f;
561 entmaxs[0] = origin[0] + 1.0f;
562 entmaxs[1] = origin[1] + 1.0f;
563 entmaxs[2] = origin[2] + 1.0f;
564 // using the model's bounding box to ensure things are visible regardless of their physics box
567 if (ent->v->angles[0] || ent->v->angles[2]) // pitch and roll
569 VectorAdd(entmins, model->rotatedmins, entmins);
570 VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
572 else if (ent->v->angles[1])
574 VectorAdd(entmins, model->yawmins, entmins);
575 VectorAdd(entmaxs, model->yawmaxs, entmaxs);
579 VectorAdd(entmins, model->normalmins, entmins);
580 VectorAdd(entmaxs, model->normalmaxs, entmaxs);
586 // if not touching a visible leaf
587 if (sv_cullentities_pvs.integer && !SV_BoxTouchingPVS(pvs, entmins, entmaxs, sv.worldmodel->brushq1.nodes))
593 // or not visible through the portals
594 if (sv_cullentities_portal.integer && !Portal_CheckBox(sv.worldmodel, testeye, entmins, entmaxs))
600 // don't try to cull embedded brush models with this, they're sometimes huge (spanning several rooms)
601 if (sv_cullentities_trace.integer && (model == NULL || model->type != mod_brush || model->name[0] != '*'))
603 // LordHavoc: test random offsets, to maximize chance of detection
604 testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
605 testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
606 testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
608 if (sv.worldmodel && sv.worldmodel->brush.TraceBox)
609 sv.worldmodel->brush.TraceBox(sv.worldmodel, vec3_origin, vec3_origin, &trace, testeye, vec3_origin, vec3_origin, testorigin);
611 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
613 if (trace.fraction == 1)
614 client->visibletime[e] = realtime + 1;
617 //test nearest point on bbox
618 testorigin[0] = bound(entmins[0], testeye[0], entmaxs[0]);
619 testorigin[1] = bound(entmins[1], testeye[1], entmaxs[1]);
620 testorigin[2] = bound(entmins[2], testeye[2], entmaxs[2]);
622 if (sv.worldmodel && sv.worldmodel->brush.TraceBox)
623 sv.worldmodel->brush.TraceBox(sv.worldmodel, vec3_origin, vec3_origin, &trace, testeye, vec3_origin, vec3_origin, testorigin);
625 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
627 if (trace.fraction == 1)
628 client->visibletime[e] = realtime + 1;
629 else if (realtime > client->visibletime[e])
642 effects = ent->v->effects;
644 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
645 if (val->_float != 0)
646 alphaf = val->_float * 255.0f;
649 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
650 if (val->_float != 0)
651 alphaf = val->_float;
655 alpha = bound(0, alphaf, 255);
657 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
658 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
659 if (scale < 0) scale = 0;
660 if (scale > 255) scale = 255;
662 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
663 if (val->_float != 0)
664 glowcolor = (int) val->_float;
666 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
667 if (val->_float != 0)
668 effects |= EF_FULLBRIGHT;
672 if (glowsize == 0 && (bits & U_GLOWTRAIL) == 0) // no effects
676 // don't send if flagged for NODRAW and there are no effects
677 if (model->flags == 0 && ((effects & EF_NODRAW) || scale <= 0 || alpha <= 0))
680 else // no model and no effects
685 if (msg->maxsize - msg->cursize < 32) // LordHavoc: increased check from 16 to 32
687 Con_Printf ("packet overflow\n");
688 // mark the rest of the entities so they can't be delta compressed against this frame
689 for (;e < sv.num_edicts;e++)
691 client->nextfullupdate[e] = -1;
692 client->visibletime[e] = -1;
697 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
698 bits = bits | U_EXTERIORMODEL;
701 baseline = &ent->e->baseline;
703 if (((int)ent->v->effects & EF_DELTA) && sv_deltacompress.integer)
705 // every half second a full update is forced
706 if (realtime < client->nextfullupdate[e])
709 baseline = &ent->e->deltabaseline;
712 nextfullupdate = realtime + 0.5f;
715 nextfullupdate = realtime + 0.5f;
717 // restore nextfullupdate since this is being sent for real
718 client->nextfullupdate[e] = nextfullupdate;
721 bits |= U_LONGENTITY;
723 if (ent->v->movetype == MOVETYPE_STEP)
726 // LordHavoc: old stuff, but rewritten to have more exact tolerances
727 if (origin[0] != baseline->origin[0]) bits |= U_ORIGIN1;
728 if (origin[1] != baseline->origin[1]) bits |= U_ORIGIN2;
729 if (origin[2] != baseline->origin[2]) bits |= U_ORIGIN3;
730 if (((int)(angles[0]*(256.0/360.0)) & 255) != ((int)(baseline->angles[0]*(256.0/360.0)) & 255)) bits |= U_ANGLE1;
731 if (((int)(angles[1]*(256.0/360.0)) & 255) != ((int)(baseline->angles[1]*(256.0/360.0)) & 255)) bits |= U_ANGLE2;
732 if (((int)(angles[2]*(256.0/360.0)) & 255) != ((int)(baseline->angles[2]*(256.0/360.0)) & 255)) bits |= U_ANGLE3;
733 if (baseline->colormap != (qbyte) ent->v->colormap) bits |= U_COLORMAP;
734 if (baseline->skin != (qbyte) ent->v->skin) bits |= U_SKIN;
735 if ((baseline->frame & 0x00FF) != ((int) ent->v->frame & 0x00FF)) bits |= U_FRAME;
736 if ((baseline->effects & 0x00FF) != ((int) ent->v->effects & 0x00FF)) bits |= U_EFFECTS;
737 if ((baseline->modelindex & 0x00FF) != ((int) ent->v->modelindex & 0x00FF)) bits |= U_MODEL;
739 // LordHavoc: new stuff
740 if (baseline->alpha != alpha) bits |= U_ALPHA;
741 if (baseline->scale != scale) bits |= U_SCALE;
742 if (((int) baseline->effects & 0xFF00) != ((int) ent->v->effects & 0xFF00)) bits |= U_EFFECTS2;
743 if (baseline->glowsize != glowsize) bits |= U_GLOWSIZE;
744 if (baseline->glowcolor != glowcolor) bits |= U_GLOWCOLOR;
745 if (((int) baseline->frame & 0xFF00) != ((int) ent->v->frame & 0xFF00)) bits |= U_FRAME2;
746 if (((int) baseline->frame & 0xFF00) != ((int) ent->v->modelindex & 0xFF00)) bits |= U_MODEL2;
748 // update delta baseline
749 VectorCopy(ent->v->origin, ent->e->deltabaseline.origin);
750 VectorCopy(ent->v->angles, ent->e->deltabaseline.angles);
751 ent->e->deltabaseline.colormap = ent->v->colormap;
752 ent->e->deltabaseline.skin = ent->v->skin;
753 ent->e->deltabaseline.frame = ent->v->frame;
754 ent->e->deltabaseline.effects = ent->v->effects;
755 ent->e->deltabaseline.modelindex = ent->v->modelindex;
756 ent->e->deltabaseline.alpha = alpha;
757 ent->e->deltabaseline.scale = scale;
758 ent->e->deltabaseline.glowsize = glowsize;
759 ent->e->deltabaseline.glowcolor = glowcolor;
762 if (bits >= 16777216)
770 MSG_WriteByte (msg, bits);
771 if (bits & U_MOREBITS)
772 MSG_WriteByte (msg, bits>>8);
773 // LordHavoc: extend bytes have to be written here due to delta compression
774 if (bits & U_EXTEND1)
775 MSG_WriteByte (msg, bits>>16);
776 if (bits & U_EXTEND2)
777 MSG_WriteByte (msg, bits>>24);
779 // LordHavoc: old stuff
780 if (bits & U_LONGENTITY)
781 MSG_WriteShort (msg,e);
783 MSG_WriteByte (msg,e);
784 if (bits & U_MODEL) MSG_WriteByte (msg, ent->v->modelindex);
785 if (bits & U_FRAME) MSG_WriteByte (msg, ent->v->frame);
786 if (bits & U_COLORMAP) MSG_WriteByte (msg, ent->v->colormap);
787 if (bits & U_SKIN) MSG_WriteByte (msg, ent->v->skin);
788 if (bits & U_EFFECTS) MSG_WriteByte (msg, ent->v->effects);
789 if (bits & U_ORIGIN1) MSG_WriteDPCoord (msg, origin[0]);
790 if (bits & U_ANGLE1) MSG_WriteAngle(msg, angles[0]);
791 if (bits & U_ORIGIN2) MSG_WriteDPCoord (msg, origin[1]);
792 if (bits & U_ANGLE2) MSG_WriteAngle(msg, angles[1]);
793 if (bits & U_ORIGIN3) MSG_WriteDPCoord (msg, origin[2]);
794 if (bits & U_ANGLE3) MSG_WriteAngle(msg, angles[2]);
796 // LordHavoc: new stuff
797 if (bits & U_ALPHA) MSG_WriteByte(msg, alpha);
798 if (bits & U_SCALE) MSG_WriteByte(msg, scale);
799 if (bits & U_EFFECTS2) MSG_WriteByte(msg, (int)ent->v->effects >> 8);
800 if (bits & U_GLOWSIZE) MSG_WriteByte(msg, glowsize);
801 if (bits & U_GLOWCOLOR) MSG_WriteByte(msg, glowcolor);
802 if (bits & U_FRAME2) MSG_WriteByte(msg, (int)ent->v->frame >> 8);
803 if (bits & U_MODEL2) MSG_WriteByte(msg, (int)ent->v->modelindex >> 8);
806 if (sv_cullentities_stats.integer)
807 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);
810 static entity_frame_t sv_writeentitiestoclient_entityframe;
811 void SV_WriteEntitiesToClient (client_t *client, edict_t *clent, sizebuf_t *msg)
813 int e, clentnum, flags, alpha, glowcolor, glowsize, scale, effects, modelindex;
814 int culled_pvs, culled_portal, culled_trace, visibleentities, totalentities;
815 float alphaf, lightsize;
817 vec3_t origin, angles, entmins, entmaxs, lightmins, lightmaxs, testorigin, testeye;
824 Mod_CheckLoaded(sv.worldmodel);
826 // find the client's PVS
827 // the real place being tested from
828 VectorAdd (clent->v->origin, clent->v->view_ofs, testeye);
829 pvs = SV_FatPVS (testeye);
831 // the place being reported (to consider the fact the client still
832 // applies the view_ofs[2], so we have to only send the fractional part
833 // of view_ofs[2], undoing what the client will redo)
834 VectorCopy (testeye, testorigin);
835 e = (int) clent->v->view_ofs[2] & 255;
838 testorigin[2] -= (float) e;
839 EntityFrame_Clear(&sv_writeentitiestoclient_entityframe, testorigin);
847 clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
848 // send all entities that touch the pvs
849 ent = NEXT_EDICT(sv.edicts);
850 for (e = 1;e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
856 if (ent != clent) // LordHavoc: always send player
858 if ((val = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)) && val->edict)
860 if (val->edict == clentnum)
861 flags |= RENDER_VIEWMODEL; // show relative to the view
864 // don't show to anyone else
870 // LordHavoc: never draw something told not to display to this client
871 if ((val = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)) && val->edict == clentnum)
873 if ((val = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)) && val->edict && val->edict != clentnum)
879 effects = ent->v->effects;
880 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_size)))
881 glowsize = (int) val->_float >> 2;
882 glowsize = bound(0, glowsize, 255);
885 if (effects & (EF_BRIGHTFIELD | EF_MUZZLEFLASH | EF_BRIGHTLIGHT | EF_DIMLIGHT | EF_RED | EF_BLUE | EF_FLAME | EF_STARDUST))
887 if (effects & EF_BRIGHTFIELD)
888 lightsize = max(lightsize, 80);
889 if (effects & EF_MUZZLEFLASH)
890 lightsize = max(lightsize, 100);
891 if (effects & EF_BRIGHTLIGHT)
892 lightsize = max(lightsize, 400);
893 if (effects & EF_DIMLIGHT)
894 lightsize = max(lightsize, 200);
895 if (effects & EF_RED)
896 lightsize = max(lightsize, 200);
897 if (effects & EF_BLUE)
898 lightsize = max(lightsize, 200);
899 if (effects & EF_FLAME)
900 lightsize = max(lightsize, 250);
901 if (effects & EF_STARDUST)
902 lightsize = max(lightsize, 100);
905 lightsize = max(lightsize, glowsize << 2);
907 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_trail)))
908 if (val->_float != 0)
910 flags |= RENDER_GLOWTRAIL;
911 lightsize = max(lightsize, 100);
915 if (ent->v->modelindex >= 0 && ent->v->modelindex < MAX_MODELS && *PR_GetString(ent->v->model))
917 modelindex = ent->v->modelindex;
918 model = sv.models[(int)ent->v->modelindex];
919 Mod_CheckLoaded(model);
924 if (ent != clent) // LordHavoc: always send player
925 if (lightsize == 0) // no effects
929 VectorCopy(ent->v->angles, angles);
930 VectorCopy(ent->v->origin, origin);
932 // ent has survived every check so far, check if it is visible
933 // always send embedded brush models, they don't generate much traffic
934 if (ent != clent && ((flags & RENDER_VIEWMODEL) == 0) && (model == NULL || model->type != mod_brush || model->name[0] != '*'))
936 // use the predicted origin
937 entmins[0] = origin[0] - 1.0f;
938 entmins[1] = origin[1] - 1.0f;
939 entmins[2] = origin[2] - 1.0f;
940 entmaxs[0] = origin[0] + 1.0f;
941 entmaxs[1] = origin[1] + 1.0f;
942 entmaxs[2] = origin[2] + 1.0f;
943 // using the model's bounding box to ensure things are visible regardless of their physics box
946 if (ent->v->angles[0] || ent->v->angles[2]) // pitch and roll
948 VectorAdd(entmins, model->rotatedmins, entmins);
949 VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
951 else if (ent->v->angles[1])
953 VectorAdd(entmins, model->yawmins, entmins);
954 VectorAdd(entmaxs, model->yawmaxs, entmaxs);
958 VectorAdd(entmins, model->normalmins, entmins);
959 VectorAdd(entmaxs, model->normalmaxs, entmaxs);
962 lightmins[0] = min(entmins[0], origin[0] - lightsize);
963 lightmins[1] = min(entmins[1], origin[1] - lightsize);
964 lightmins[2] = min(entmins[2], origin[2] - lightsize);
965 lightmaxs[0] = min(entmaxs[0], origin[0] + lightsize);
966 lightmaxs[1] = min(entmaxs[1], origin[1] + lightsize);
967 lightmaxs[2] = min(entmaxs[2], origin[2] + lightsize);
971 // if not touching a visible leaf
972 if (sv_cullentities_pvs.integer && !SV_BoxTouchingPVS(pvs, lightmins, lightmaxs, sv.worldmodel->brushq1.nodes))
978 // or not visible through the portals
979 if (sv_cullentities_portal.integer && !Portal_CheckBox(sv.worldmodel, testeye, lightmins, lightmaxs))
985 if (sv_cullentities_trace.integer)
987 // LordHavoc: test center first
988 testorigin[0] = (entmins[0] + entmaxs[0]) * 0.5f;
989 testorigin[1] = (entmins[1] + entmaxs[1]) * 0.5f;
990 testorigin[2] = (entmins[2] + entmaxs[2]) * 0.5f;
991 if (sv.worldmodel && sv.worldmodel->brush.TraceBox)
992 sv.worldmodel->brush.TraceBox(sv.worldmodel, vec3_origin, vec3_origin, &trace, NULL, testeye, vec3_origin, vec3_origin, testorigin);
994 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
995 if (trace.fraction == 1)
996 client->visibletime[e] = realtime + 1;
999 // LordHavoc: test random offsets, to maximize chance of detection
1000 testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
1001 testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
1002 testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
1003 if (sv.worldmodel && sv.worldmodel->brush.TraceBox)
1004 sv.worldmodel->brush.TraceBox(sv.worldmodel, vec3_origin, vec3_origin, &trace, NULL, testeye, vec3_origin, vec3_origin, testorigin);
1006 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
1007 if (trace.fraction == 1)
1008 client->visibletime[e] = realtime + 1;
1013 // LordHavoc: test random offsets, to maximize chance of detection
1014 testorigin[0] = lhrandom(lightmins[0], lightmaxs[0]);
1015 testorigin[1] = lhrandom(lightmins[1], lightmaxs[1]);
1016 testorigin[2] = lhrandom(lightmins[2], lightmaxs[2]);
1017 if (sv.worldmodel && sv.worldmodel->brush.TraceBox)
1018 sv.worldmodel->brush.TraceBox(sv.worldmodel, vec3_origin, vec3_origin, &trace, NULL, testeye, vec3_origin, vec3_origin, testorigin);
1020 Collision_ClipTrace(&trace, NULL, sv.worldmodel, vec3_origin, vec3_origin, vec3_origin, vec3_origin, testeye, vec3_origin, vec3_origin, testorigin);
1021 if (trace.fraction == 1)
1022 client->visibletime[e] = realtime + 1;
1025 if (realtime > client->visibletime[e])
1034 if (realtime > client->visibletime[e])
1049 effects = ent->v->effects;
1051 if ((val = GETEDICTFIELDVALUE(ent, eval_alpha)))
1052 if (val->_float != 0)
1053 alphaf = val->_float * 255.0;
1056 if ((val = GETEDICTFIELDVALUE(ent, eval_renderamt)))
1057 if (val->_float != 0)
1058 alphaf = val->_float;
1062 alpha = bound(0, alphaf, 255);
1064 if ((val = GETEDICTFIELDVALUE(ent, eval_scale)))
1065 if ((scale = (int) (val->_float * 16.0)) == 0) scale = 16;
1066 if (scale < 0) scale = 0;
1067 if (scale > 255) scale = 255;
1069 if ((val = GETEDICTFIELDVALUE(ent, eval_glow_color)))
1070 if (val->_float != 0)
1071 glowcolor = (int) val->_float;
1073 if ((val = GETEDICTFIELDVALUE(ent, eval_fullbright)))
1074 if (val->_float != 0)
1075 effects |= EF_FULLBRIGHT;
1079 if (lightsize == 0) // no effects
1083 // don't send if flagged for NODRAW and there are no effects
1084 if (model->flags == 0 && ((effects & EF_NODRAW) || scale <= 0 || alpha <= 0))
1087 else // no model and no effects
1092 if ((val = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)) && val->edict == clentnum)
1093 flags |= RENDER_EXTERIORMODEL;
1095 if (ent->v->movetype == MOVETYPE_STEP)
1096 flags |= RENDER_STEP;
1097 // don't send an entity if it's coordinates would wrap around
1098 if ((effects & EF_LOWPRECISION) && origin[0] >= -32768 && origin[1] >= -32768 && origin[2] >= -32768 && origin[0] <= 32767 && origin[1] <= 32767 && origin[2] <= 32767)
1099 flags |= RENDER_LOWPRECISION;
1101 s = EntityFrame_NewEntity(&sv_writeentitiestoclient_entityframe, e);
1102 // if we run out of space, abort
1105 VectorCopy(origin, s->origin);
1106 VectorCopy(angles, s->angles);
1107 if (ent->v->colormap >= 1024)
1108 flags |= RENDER_COLORMAPPED;
1109 s->colormap = ent->v->colormap;
1110 s->skin = ent->v->skin;
1111 s->frame = ent->v->frame;
1112 s->modelindex = modelindex;
1113 s->effects = effects;
1116 s->glowsize = glowsize;
1117 s->glowcolor = glowcolor;
1120 sv_writeentitiestoclient_entityframe.framenum = ++client->entityframenumber;
1121 EntityFrame_Write(&client->entitydatabase, &sv_writeentitiestoclient_entityframe, msg);
1123 if (sv_cullentities_stats.integer)
1124 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);
1134 void SV_CleanupEnts (void)
1139 ent = NEXT_EDICT(sv.edicts);
1140 for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
1141 ent->v->effects = (int)ent->v->effects & ~EF_MUZZLEFLASH;
1146 SV_WriteClientdataToMessage
1150 void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
1161 // send a damage message
1163 if (ent->v->dmg_take || ent->v->dmg_save)
1165 other = PROG_TO_EDICT(ent->v->dmg_inflictor);
1166 MSG_WriteByte (msg, svc_damage);
1167 MSG_WriteByte (msg, ent->v->dmg_save);
1168 MSG_WriteByte (msg, ent->v->dmg_take);
1169 for (i=0 ; i<3 ; i++)
1170 MSG_WriteDPCoord (msg, other->v->origin[i] + 0.5*(other->v->mins[i] + other->v->maxs[i]));
1172 ent->v->dmg_take = 0;
1173 ent->v->dmg_save = 0;
1177 // send the current viewpos offset from the view entity
1179 SV_SetIdealPitch (); // how much to look up / down ideally
1181 // a fixangle might get lost in a dropped packet. Oh well.
1182 if ( ent->v->fixangle )
1184 MSG_WriteByte (msg, svc_setangle);
1185 for (i=0 ; i < 3 ; i++)
1186 MSG_WriteAngle (msg, ent->v->angles[i] );
1187 ent->v->fixangle = 0;
1192 if (ent->v->view_ofs[2] != DEFAULT_VIEWHEIGHT)
1193 bits |= SU_VIEWHEIGHT;
1195 if (ent->v->idealpitch)
1196 bits |= SU_IDEALPITCH;
1198 // stuff the sigil bits into the high bits of items for sbar, or else
1200 val = GETEDICTFIELDVALUE(ent, eval_items2);
1203 items = (int)ent->v->items | ((int)val->_float << 23);
1205 items = (int)ent->v->items | ((int)pr_global_struct->serverflags << 28);
1209 if ( (int)ent->v->flags & FL_ONGROUND)
1210 bits |= SU_ONGROUND;
1212 if ( ent->v->waterlevel >= 2)
1216 VectorClear(punchvector);
1217 if ((val = GETEDICTFIELDVALUE(ent, eval_punchvector)))
1218 VectorCopy(val->vector, punchvector);
1221 if ((val = GETEDICTFIELDVALUE(ent, eval_viewzoom)))
1223 i = val->_float * 255.0f;
1227 i = bound(0, i, 255);
1231 if (viewzoom != 255)
1232 bits |= SU_VIEWZOOM;
1234 for (i=0 ; i<3 ; i++)
1236 if (ent->v->punchangle[i])
1237 bits |= (SU_PUNCH1<<i);
1238 if (punchvector[i]) // dpprotocol
1239 bits |= (SU_PUNCHVEC1<<i); // dpprotocol
1240 if (ent->v->velocity[i])
1241 bits |= (SU_VELOCITY1<<i);
1244 if (ent->v->weaponframe)
1245 bits |= SU_WEAPONFRAME;
1247 if (ent->v->armorvalue)
1254 if (bits >= 16777216)
1259 MSG_WriteByte (msg, svc_clientdata);
1260 MSG_WriteShort (msg, bits);
1261 if (bits & SU_EXTEND1)
1262 MSG_WriteByte(msg, bits >> 16);
1263 if (bits & SU_EXTEND2)
1264 MSG_WriteByte(msg, bits >> 24);
1266 if (bits & SU_VIEWHEIGHT)
1267 MSG_WriteChar (msg, ent->v->view_ofs[2]);
1269 if (bits & SU_IDEALPITCH)
1270 MSG_WriteChar (msg, ent->v->idealpitch);
1272 for (i=0 ; i<3 ; i++)
1274 if (bits & (SU_PUNCH1<<i))
1275 MSG_WritePreciseAngle(msg, ent->v->punchangle[i]); // dpprotocol
1276 if (bits & (SU_PUNCHVEC1<<i)) // dpprotocol
1277 MSG_WriteDPCoord(msg, punchvector[i]); // dpprotocol
1278 if (bits & (SU_VELOCITY1<<i))
1279 MSG_WriteChar (msg, ent->v->velocity[i]/16);
1282 // [always sent] if (bits & SU_ITEMS)
1283 MSG_WriteLong (msg, items);
1285 if (bits & SU_WEAPONFRAME)
1286 MSG_WriteByte (msg, ent->v->weaponframe);
1287 if (bits & SU_ARMOR)
1288 MSG_WriteByte (msg, ent->v->armorvalue);
1289 if (bits & SU_WEAPON)
1290 MSG_WriteByte (msg, SV_ModelIndex(PR_GetString(ent->v->weaponmodel)));
1292 MSG_WriteShort (msg, ent->v->health);
1293 MSG_WriteByte (msg, ent->v->currentammo);
1294 MSG_WriteByte (msg, ent->v->ammo_shells);
1295 MSG_WriteByte (msg, ent->v->ammo_nails);
1296 MSG_WriteByte (msg, ent->v->ammo_rockets);
1297 MSG_WriteByte (msg, ent->v->ammo_cells);
1299 if (gamemode == GAME_HIPNOTIC || gamemode == GAME_ROGUE)
1303 if ( ((int)ent->v->weapon) & (1<<i) )
1305 MSG_WriteByte (msg, i);
1312 MSG_WriteByte (msg, ent->v->weapon);
1315 if (bits & SU_VIEWZOOM)
1316 MSG_WriteByte (msg, viewzoom);
1320 =======================
1321 SV_SendClientDatagram
1322 =======================
1324 static qbyte sv_sendclientdatagram_buf[MAX_DATAGRAM]; // FIXME?
1325 qboolean SV_SendClientDatagram (client_t *client)
1329 msg.data = sv_sendclientdatagram_buf;
1330 msg.maxsize = sizeof(sv_sendclientdatagram_buf);
1333 MSG_WriteByte (&msg, svc_time);
1334 MSG_WriteFloat (&msg, sv.time);
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);
1345 // send the datagram
1346 if (NetConn_SendUnreliableMessage (client->netconnection, &msg) == -1)
1348 SV_DropClient (true);// if the message couldn't send, kick off
1356 =======================
1357 SV_UpdateToReliableMessages
1358 =======================
1360 void SV_UpdateToReliableMessages (void)
1367 // check for changes to be sent over the reliable streams
1368 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1370 // only update the client fields if they've spawned in
1371 if (host_client->spawned)
1373 // update the host_client fields we care about according to the entity fields
1374 sv_player = host_client->edict;
1375 s = PR_GetString(sv_player->v->netname);
1376 if (s != host_client->name)
1380 // point the string back at host_client->name to keep it safe
1381 strncpy(host_client->name, s, sizeof(host_client->name) - 1);
1382 sv_player->v->netname = PR_SetString(host_client->name);
1384 if ((val = GETEDICTFIELDVALUE(sv_player, eval_clientcolors)) && host_client->colors != val->_float)
1385 host_client->colors = val->_float;
1386 host_client->frags = sv_player->v->frags;
1387 if (gamemode == GAME_NEHAHRA)
1388 if ((val = GETEDICTFIELDVALUE(sv_player, eval_pmodel)) && host_client->pmodel != val->_float)
1389 host_client->pmodel = val->_float;
1391 // if the fields changed, send messages about the changes
1392 if (strcmp(host_client->old_name, host_client->name))
1394 strcpy(host_client->old_name, host_client->name);
1395 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1397 if (!client->active || !client->spawned)
1399 MSG_WriteByte (&client->message, svc_updatename);
1400 MSG_WriteByte (&client->message, i);
1401 MSG_WriteString (&client->message, host_client->name);
1404 if (host_client->old_colors != host_client->colors)
1406 host_client->old_colors = host_client->colors;
1407 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1409 if (!client->active || !client->spawned)
1411 MSG_WriteByte (&client->message, svc_updatecolors);
1412 MSG_WriteByte (&client->message, i);
1413 MSG_WriteByte (&client->message, host_client->colors);
1416 if (host_client->old_frags != host_client->frags)
1418 host_client->old_frags = host_client->frags;
1419 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1421 if (!client->active || !client->spawned)
1423 MSG_WriteByte (&client->message, svc_updatefrags);
1424 MSG_WriteByte (&client->message, i);
1425 MSG_WriteShort (&client->message, host_client->frags);
1431 for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
1433 if (!client->active)
1435 SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
1438 SZ_Clear (&sv.reliable_datagram);
1443 =======================
1446 Send a nop message without trashing or sending the accumulated client
1448 =======================
1450 void SV_SendNop (client_t *client)
1456 msg.maxsize = sizeof(buf);
1459 MSG_WriteChar (&msg, svc_nop);
1461 if (NetConn_SendUnreliableMessage (client->netconnection, &msg) == -1)
1462 SV_DropClient (true); // if the message couldn't send, kick off
1463 client->last_message = realtime;
1467 =======================
1468 SV_SendClientMessages
1469 =======================
1471 void SV_SendClientMessages (void)
1475 // update frags, names, etc
1476 SV_UpdateToReliableMessages ();
1478 // build individual updates
1479 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1481 if (!host_client->active)
1484 if (host_client->deadsocket)
1486 SV_DropClient (true); // if the message couldn't send, kick off
1490 if (host_client->spawned)
1492 if (!SV_SendClientDatagram (host_client))
1497 // the player isn't totally in the game yet
1498 // send small keepalive messages if too much time has passed
1499 // send a full message when the next signon stage has been requested
1500 // some other message data (name changes, etc) may accumulate
1501 // between signon stages
1502 if (!host_client->sendsignon)
1504 if (realtime - host_client->last_message > 5)
1505 SV_SendNop (host_client);
1506 continue; // don't send out non-signon messages
1510 // check for an overflowed message. Should only happen
1511 // on a very fucked up connection that backs up a lot, then
1513 if (host_client->message.overflowed)
1515 SV_DropClient (true); // overflowed
1516 host_client->message.overflowed = false;
1520 if (host_client->message.cursize || host_client->dropasap)
1522 if (!NetConn_CanSendMessage (host_client->netconnection))
1525 if (host_client->dropasap)
1526 SV_DropClient (false); // went to another level
1529 if (NetConn_SendReliableMessage (host_client->netconnection, &host_client->message) == -1)
1530 SV_DropClient (true); // if the message couldn't send, kick off
1531 SZ_Clear (&host_client->message);
1532 host_client->last_message = realtime;
1533 host_client->sendsignon = false;
1539 // clear muzzle flashes
1545 ==============================================================================
1549 ==============================================================================
1558 int SV_ModelIndex (const char *name)
1562 if (!name || !name[0])
1565 for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
1566 if (!strcmp(sv.model_precache[i], name))
1568 if (i==MAX_MODELS || !sv.model_precache[i])
1569 Host_Error ("SV_ModelIndex: model %s not precached", name);
1573 #ifdef SV_QUAKEENTITIES
1580 void SV_CreateBaseline (void)
1582 int i, entnum, large;
1585 // LordHavoc: clear *all* states (note just active ones)
1586 for (entnum = 0;entnum < sv.max_edicts;entnum++)
1588 // get the current server version
1589 svent = EDICT_NUM(entnum);
1591 // LordHavoc: always clear state values, whether the entity is in use or not
1592 ClearStateToDefault(&svent->e->baseline);
1596 if (entnum > svs.maxclients && !svent->v->modelindex)
1599 // create entity baseline
1600 VectorCopy (svent->v->origin, svent->e->baseline.origin);
1601 VectorCopy (svent->v->angles, svent->e->baseline.angles);
1602 svent->e->baseline.frame = svent->v->frame;
1603 svent->e->baseline.skin = svent->v->skin;
1604 if (entnum > 0 && entnum <= svs.maxclients)
1606 svent->e->baseline.colormap = entnum;
1607 svent->e->baseline.modelindex = SV_ModelIndex("progs/player.mdl");
1611 svent->e->baseline.colormap = 0;
1612 svent->e->baseline.modelindex = svent->v->modelindex;
1616 if (svent->e->baseline.modelindex & 0xFF00 || svent->e->baseline.frame & 0xFF00)
1619 // add to the message
1621 MSG_WriteByte (&sv.signon, svc_spawnbaseline2);
1623 MSG_WriteByte (&sv.signon, svc_spawnbaseline);
1624 MSG_WriteShort (&sv.signon, entnum);
1628 MSG_WriteShort (&sv.signon, svent->e->baseline.modelindex);
1629 MSG_WriteShort (&sv.signon, svent->e->baseline.frame);
1633 MSG_WriteByte (&sv.signon, svent->e->baseline.modelindex);
1634 MSG_WriteByte (&sv.signon, svent->e->baseline.frame);
1636 MSG_WriteByte (&sv.signon, svent->e->baseline.colormap);
1637 MSG_WriteByte (&sv.signon, svent->e->baseline.skin);
1638 for (i=0 ; i<3 ; i++)
1640 MSG_WriteDPCoord(&sv.signon, svent->e->baseline.origin[i]);
1641 MSG_WriteAngle(&sv.signon, svent->e->baseline.angles[i]);
1652 Tell all the clients that the server is changing levels
1655 void SV_SendReconnect (void)
1662 msg.maxsize = sizeof(data);
1664 MSG_WriteChar (&msg, svc_stufftext);
1665 MSG_WriteString (&msg, "reconnect\n");
1666 NetConn_SendToAll (&msg, 5);
1668 if (cls.state != ca_dedicated)
1669 Cmd_ExecuteString ("reconnect\n", src_command);
1677 Grabs the current state of each client for saving across the
1678 transition to another level
1681 void SV_SaveSpawnparms (void)
1685 svs.serverflags = pr_global_struct->serverflags;
1687 for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1689 if (!host_client->active)
1692 // call the progs to get default spawn parms for the new client
1693 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1694 PR_ExecuteProgram (pr_global_struct->SetChangeParms, "QC function SetChangeParms is missing");
1695 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1696 host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1700 void SV_IncreaseEdicts(void)
1704 int oldmax_edicts = sv.max_edicts;
1705 void *oldedictsengineprivate = sv.edictsengineprivate;
1706 void *oldedictsfields = sv.edictsfields;
1707 void *oldmoved_edicts = sv.moved_edicts;
1709 if (sv.max_edicts >= MAX_EDICTS)
1712 // links don't survive the transition, so unlink everything
1713 for (i = 0, ent = sv.edicts;i < sv.max_edicts;i++, ent++)
1716 SV_UnlinkEdict(sv.edicts + i);
1717 memset(&ent->e->areagrid, 0, sizeof(ent->e->areagrid));
1721 sv.max_edicts = min(sv.max_edicts + 32, MAX_EDICTS);
1722 sv.edictsengineprivate = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_engineprivate_t));
1723 sv.edictsfields = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1724 sv.moved_edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_t *));
1726 memcpy(sv.edictsengineprivate, oldedictsengineprivate, oldmax_edicts * sizeof(edict_engineprivate_t));
1727 memcpy(sv.edictsfields, oldedictsfields, oldmax_edicts * pr_edict_size);
1729 for (i = 0, ent = sv.edicts;i < sv.max_edicts;i++, ent++)
1731 ent->e = sv.edictsengineprivate + i;
1732 ent->v = (void *)((qbyte *)sv.edictsfields + i * pr_edict_size);
1733 // link every entity except world
1735 SV_LinkEdict(ent, false);
1738 Mem_Free(oldedictsengineprivate);
1739 Mem_Free(oldedictsfields);
1740 Mem_Free(oldmoved_edicts);
1747 This is called at the start of each level
1750 extern float scr_centertime_off;
1752 void SV_SpawnServer (const char *server)
1758 // let's not have any servers with no name
1759 if (hostname.string[0] == 0)
1760 Cvar_Set ("hostname", "UNNAMED");
1761 scr_centertime_off = 0;
1763 Con_DPrintf ("SpawnServer: %s\n",server);
1764 svs.changelevel_issued = false; // now safe to issue another
1767 // tell all connected clients that we are going to a new level
1770 SV_SendReconnect ();
1772 NetConn_OpenServerPorts(svs.maxclients > 1);
1775 // make cvars consistant
1778 Cvar_SetValue ("deathmatch", 0);
1779 current_skill = bound(0, (int)(skill.value + 0.5), 3);
1781 Cvar_SetValue ("skill", (float)current_skill);
1784 // set up the new server
1786 Host_ClearMemory ();
1788 memset (&sv, 0, sizeof(sv));
1790 strcpy (sv.name, server);
1792 // load progs to get entity field count
1795 // allocate server memory
1796 // start out with just enough room for clients and a reasonable estimate of entities
1797 sv.max_edicts = ((svs.maxclients + 128) + 31) & ~31;
1798 sv.max_edicts = min(sv.max_edicts, MAX_EDICTS);
1800 // clear the edict memory pool
1801 Mem_EmptyPool(sv_edicts_mempool);
1802 // edict_t structures (hidden from progs)
1803 sv.edicts = Mem_Alloc(sv_edicts_mempool, MAX_EDICTS * sizeof(edict_t));
1804 // engine private structures (hidden from progs)
1805 sv.edictsengineprivate = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_engineprivate_t));
1806 // progs fields, often accessed by server
1807 sv.edictsfields = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1808 // used by PushMove to move back pushed entities
1809 sv.moved_edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_t *));
1810 for (i = 0;i < sv.max_edicts;i++)
1812 ent = sv.edicts + i;
1813 ent->e = sv.edictsengineprivate + i;
1814 ent->v = (void *)((qbyte *)sv.edictsfields + i * pr_edict_size);
1817 sv.datagram.maxsize = sizeof(sv.datagram_buf);
1818 sv.datagram.cursize = 0;
1819 sv.datagram.data = sv.datagram_buf;
1821 sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1822 sv.reliable_datagram.cursize = 0;
1823 sv.reliable_datagram.data = sv.reliable_datagram_buf;
1825 sv.signon.maxsize = sizeof(sv.signon_buf);
1826 sv.signon.cursize = 0;
1827 sv.signon.data = sv.signon_buf;
1829 // leave slots at start for clients only
1830 sv.num_edicts = svs.maxclients+1;
1832 sv.state = ss_loading;
1839 strcpy (sv.name, server);
1840 sprintf (sv.modelname,"maps/%s.bsp", server);
1841 sv.worldmodel = Mod_ForName(sv.modelname, false, true, true);
1844 Con_Printf ("Couldn't spawn server %s\n", sv.modelname);
1848 sv.models[1] = sv.worldmodel;
1851 // clear world interaction links
1855 sv.sound_precache[0] = "";
1857 sv.model_precache[0] = "";
1858 sv.model_precache[1] = sv.modelname;
1859 for (i = 1;i < sv.worldmodel->brushq1.numsubmodels;i++)
1861 sv.model_precache[i+1] = localmodels[i];
1862 sv.models[i+1] = Mod_ForName (localmodels[i], false, false, false);
1866 // load the rest of the entities
1869 memset (ent->v, 0, progs->entityfields * 4);
1870 ent->e->free = false;
1871 ent->v->model = PR_SetString(sv.modelname);
1872 ent->v->modelindex = 1; // world model
1873 ent->v->solid = SOLID_BSP;
1874 ent->v->movetype = MOVETYPE_PUSH;
1877 pr_global_struct->coop = coop.integer;
1879 pr_global_struct->deathmatch = deathmatch.integer;
1881 pr_global_struct->mapname = PR_SetString(sv.name);
1883 // serverflags are for cross level information (sigils)
1884 pr_global_struct->serverflags = svs.serverflags;
1886 // load replacement entity file if found
1888 if (sv_entpatch.integer)
1889 entities = FS_LoadFile(va("maps/%s.ent", sv.name), true);
1892 Con_Printf("Loaded maps/%s.ent\n", sv.name);
1893 ED_LoadFromFile (entities);
1897 ED_LoadFromFile (sv.worldmodel->brush.entities);
1900 // LordHavoc: clear world angles (to fix e3m3.bsp)
1901 VectorClear(sv.edicts->v->angles);
1905 // all setup is completed, any further precache statements are errors
1906 sv.state = ss_active;
1908 // run two frames to allow everything to settle
1909 for (i = 0;i < 2;i++)
1911 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1917 #ifdef QUAKEENTITIES
1918 // create a baseline for more efficient communications
1919 SV_CreateBaseline ();
1922 // send serverinfo to all connected clients
1923 for (i=0,host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1924 if (host_client->active)
1925 SV_SendServerinfo (host_client);
1927 Con_DPrintf ("Server spawned.\n");
1928 NetConn_Heartbeat (2);