2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // sv_main.c -- server main program
24 // select which protocol to host, by name
25 // this is named the same as PROTOCOL_DARKPLACES5 for example, minus the PROTOCOL_ prefix
26 cvar_t sv_protocolname = {0, "sv_protocolname", "DARKPLACES5"};
27 cvar_t sv_ratelimitlocalplayer = {0, "sv_ratelimitlocalplayer", "0"};
28 cvar_t sv_maxrate = {CVAR_SAVE | CVAR_NOTIFY, "sv_maxrate", "10000"};
30 static cvar_t sv_cullentities_pvs = {0, "sv_cullentities_pvs", "1"}; // fast but loose
31 static cvar_t sv_cullentities_trace = {0, "sv_cullentities_trace", "0"}; // tends to get false negatives, uses a timeout to keep entities visible a short time after becoming hidden
32 static cvar_t sv_cullentities_stats = {0, "sv_cullentities_stats", "0"};
33 static cvar_t sv_entpatch = {0, "sv_entpatch", "1"};
35 extern cvar_t sys_ticrate;
37 cvar_t sv_gameplayfix_grenadebouncedownslopes = {0, "sv_gameplayfix_grenadebouncedownslopes", "1"};
38 cvar_t sv_gameplayfix_noairborncorpse = {0, "sv_gameplayfix_noairborncorpse", "1"};
39 cvar_t sv_gameplayfix_stepdown = {0, "sv_gameplayfix_stepdown", "1"};
40 cvar_t sv_gameplayfix_stepwhilejumping = {0, "sv_gameplayfix_stepwhilejumping", "1"};
41 cvar_t sv_gameplayfix_swiminbmodels = {0, "sv_gameplayfix_swiminbmodels", "1"};
46 static char localmodels[MAX_MODELS][5]; // inline model names for precache
48 mempool_t *sv_edicts_mempool = NULL;
50 //============================================================================
52 extern void SV_Phys_Init (void);
53 extern void SV_World_Init (void);
54 static void SV_SaveEntFile_f(void);
65 Cmd_AddCommand("sv_saveentfile", SV_SaveEntFile_f);
66 Cvar_RegisterVariable (&sv_maxvelocity);
67 Cvar_RegisterVariable (&sv_gravity);
68 Cvar_RegisterVariable (&sv_friction);
69 Cvar_RegisterVariable (&sv_edgefriction);
70 Cvar_RegisterVariable (&sv_stopspeed);
71 Cvar_RegisterVariable (&sv_maxspeed);
72 Cvar_RegisterVariable (&sv_accelerate);
73 Cvar_RegisterVariable (&sv_idealpitchscale);
74 Cvar_RegisterVariable (&sv_aim);
75 Cvar_RegisterVariable (&sv_nostep);
76 Cvar_RegisterVariable (&sv_deltacompress);
77 Cvar_RegisterVariable (&sv_cullentities_pvs);
78 Cvar_RegisterVariable (&sv_cullentities_trace);
79 Cvar_RegisterVariable (&sv_cullentities_stats);
80 Cvar_RegisterVariable (&sv_entpatch);
81 Cvar_RegisterVariable (&sv_gameplayfix_grenadebouncedownslopes);
82 Cvar_RegisterVariable (&sv_gameplayfix_noairborncorpse);
83 Cvar_RegisterVariable (&sv_gameplayfix_stepdown);
84 Cvar_RegisterVariable (&sv_gameplayfix_stepwhilejumping);
85 Cvar_RegisterVariable (&sv_gameplayfix_swiminbmodels);
86 Cvar_RegisterVariable (&sv_protocolname);
87 Cvar_RegisterVariable (&sv_ratelimitlocalplayer);
88 Cvar_RegisterVariable (&sv_maxrate);
93 for (i = 0;i < MAX_MODELS;i++)
94 sprintf (localmodels[i], "*%i", i);
96 sv_edicts_mempool = Mem_AllocPool("server edicts", 0, NULL);
99 static void SV_SaveEntFile_f(void)
101 char basename[MAX_QPATH];
102 if (!sv.active || !sv.worldmodel)
104 Con_Print("Not running a server\n");
107 FS_StripExtension(sv.worldmodel->name, basename, sizeof(basename));
108 FS_WriteFile(va("%s.ent", basename), sv.worldmodel->brush.entities, strlen(sv.worldmodel->brush.entities));
112 =============================================================================
116 =============================================================================
123 Make sure the event gets sent to all clients
126 void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
130 if (sv.datagram.cursize > MAX_PACKETFRAGMENT-18)
132 MSG_WriteByte (&sv.datagram, svc_particle);
133 MSG_WriteCoord (&sv.datagram, org[0], sv.protocol);
134 MSG_WriteCoord (&sv.datagram, org[1], sv.protocol);
135 MSG_WriteCoord (&sv.datagram, org[2], sv.protocol);
136 for (i=0 ; i<3 ; i++)
143 MSG_WriteChar (&sv.datagram, v);
145 MSG_WriteByte (&sv.datagram, count);
146 MSG_WriteByte (&sv.datagram, color);
153 Make sure the event gets sent to all clients
156 void SV_StartEffect (vec3_t org, int modelindex, int startframe, int framecount, int framerate)
158 if (modelindex >= 256 || startframe >= 256)
160 if (sv.datagram.cursize > MAX_PACKETFRAGMENT-19)
162 MSG_WriteByte (&sv.datagram, svc_effect2);
163 MSG_WriteCoord (&sv.datagram, org[0], sv.protocol);
164 MSG_WriteCoord (&sv.datagram, org[1], sv.protocol);
165 MSG_WriteCoord (&sv.datagram, org[2], sv.protocol);
166 MSG_WriteShort (&sv.datagram, modelindex);
167 MSG_WriteShort (&sv.datagram, startframe);
168 MSG_WriteByte (&sv.datagram, framecount);
169 MSG_WriteByte (&sv.datagram, framerate);
173 if (sv.datagram.cursize > MAX_PACKETFRAGMENT-17)
175 MSG_WriteByte (&sv.datagram, svc_effect);
176 MSG_WriteCoord (&sv.datagram, org[0], sv.protocol);
177 MSG_WriteCoord (&sv.datagram, org[1], sv.protocol);
178 MSG_WriteCoord (&sv.datagram, org[2], sv.protocol);
179 MSG_WriteByte (&sv.datagram, modelindex);
180 MSG_WriteByte (&sv.datagram, startframe);
181 MSG_WriteByte (&sv.datagram, framecount);
182 MSG_WriteByte (&sv.datagram, framerate);
190 Each entity can have eight independant sound sources, like voice,
193 Channel 0 is an auto-allocate channel, the others override anything
194 already running on that entity/channel pair.
196 An attenuation of 0 will play full volume everywhere in the level.
197 Larger attenuations will drop off. (max 4 attenuation)
201 void SV_StartSound (edict_t *entity, int channel, char *sample, int volume, float attenuation)
203 int sound_num, field_mask, i, ent;
205 if (volume < 0 || volume > 255)
206 Host_Error ("SV_StartSound: volume = %i", volume);
208 if (attenuation < 0 || attenuation > 4)
209 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
211 if (channel < 0 || channel > 7)
212 Host_Error ("SV_StartSound: channel = %i", channel);
214 if (sv.datagram.cursize > MAX_PACKETFRAGMENT-21)
217 // find precache number for sound
218 for (sound_num=1 ; sound_num<MAX_SOUNDS && sv.sound_precache[sound_num] ; sound_num++)
219 if (!strcmp(sample, sv.sound_precache[sound_num]))
222 if ( sound_num == MAX_SOUNDS || !sv.sound_precache[sound_num] )
224 Con_Printf("SV_StartSound: %s not precached\n", sample);
228 ent = NUM_FOR_EDICT(entity);
231 if (volume != DEFAULT_SOUND_PACKET_VOLUME)
232 field_mask |= SND_VOLUME;
233 if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
234 field_mask |= SND_ATTENUATION;
236 field_mask |= SND_LARGEENTITY;
237 if (sound_num >= 256 || channel >= 8)
238 field_mask |= SND_LARGESOUND;
240 // directed messages go only to the entity they are targeted on
241 MSG_WriteByte (&sv.datagram, svc_sound);
242 MSG_WriteByte (&sv.datagram, field_mask);
243 if (field_mask & SND_VOLUME)
244 MSG_WriteByte (&sv.datagram, volume);
245 if (field_mask & SND_ATTENUATION)
246 MSG_WriteByte (&sv.datagram, attenuation*64);
247 if (field_mask & SND_LARGEENTITY)
249 MSG_WriteShort (&sv.datagram, ent);
250 MSG_WriteByte (&sv.datagram, channel);
253 MSG_WriteShort (&sv.datagram, (ent<<3) | channel);
254 if (field_mask & SND_LARGESOUND)
255 MSG_WriteShort (&sv.datagram, sound_num);
257 MSG_WriteByte (&sv.datagram, sound_num);
258 for (i = 0;i < 3;i++)
259 MSG_WriteCoord (&sv.datagram, entity->v->origin[i]+0.5*(entity->v->mins[i]+entity->v->maxs[i]), sv.protocol);
263 ==============================================================================
267 ==============================================================================
274 Sends the first message from the server to a connected client.
275 This will be sent on the initial connection and upon each server load.
278 void SV_SendServerinfo (client_t *client)
283 // edicts get reallocated on level changes, so we need to update it here
284 client->edict = EDICT_NUM((client - svs.clients) + 1);
286 // if client is a botclient coming from a level change, we need to set up
287 // client info that normally requires networking
288 if (!client->netconnection)
293 ED_ClearEdict(client->edict);
295 // copy spawn parms out of the client_t
296 for (i=0 ; i< NUM_SPAWN_PARMS ; i++)
297 (&pr_global_struct->parm1)[i] = host_client->spawn_parms[i];
299 // call the spawn function
300 pr_global_struct->time = sv.time;
301 pr_global_struct->self = EDICT_TO_PROG(sv_player);
302 PR_ExecuteProgram (pr_global_struct->ClientConnect, "QC function ClientConnect is missing");
303 PR_ExecuteProgram (pr_global_struct->PutClientInServer, "QC function PutClientInServer is missing");
304 host_client->spawned = true;
308 // LordHavoc: clear entityframe tracking
310 if (client->entitydatabase)
311 EntityFrame_FreeDatabase(client->entitydatabase);
312 if (client->entitydatabase4)
313 EntityFrame4_FreeDatabase(client->entitydatabase4);
314 if (client->entitydatabase5)
315 EntityFrame5_FreeDatabase(client->entitydatabase5);
317 if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3)
318 client->entitydatabase = EntityFrame_AllocDatabase(sv_clients_mempool);
319 if (sv.protocol == PROTOCOL_DARKPLACES4)
320 client->entitydatabase4 = EntityFrame4_AllocDatabase(sv_clients_mempool);
321 if (sv.protocol == PROTOCOL_DARKPLACES5)
322 client->entitydatabase5 = EntityFrame5_AllocDatabase(sv_clients_mempool);
324 MSG_WriteByte (&client->message, svc_print);
325 snprintf (message, sizeof (message), "\002\nServer: %s build %s (progs %i crc)", gamename, buildstring, pr_crc);
326 MSG_WriteString (&client->message,message);
328 MSG_WriteByte (&client->message, svc_serverinfo);
329 MSG_WriteLong (&client->message, sv.protocol);
330 MSG_WriteByte (&client->message, svs.maxclients);
332 if (!coop.integer && deathmatch.integer)
333 MSG_WriteByte (&client->message, GAME_DEATHMATCH);
335 MSG_WriteByte (&client->message, GAME_COOP);
337 MSG_WriteString (&client->message,PR_GetString(sv.edicts->v->message));
339 for (s = sv.model_precache+1 ; *s ; s++)
340 MSG_WriteString (&client->message, *s);
341 MSG_WriteByte (&client->message, 0);
343 for (s = sv.sound_precache+1 ; *s ; s++)
344 MSG_WriteString (&client->message, *s);
345 MSG_WriteByte (&client->message, 0);
348 MSG_WriteByte (&client->message, svc_cdtrack);
349 MSG_WriteByte (&client->message, sv.edicts->v->sounds);
350 MSG_WriteByte (&client->message, sv.edicts->v->sounds);
353 MSG_WriteByte (&client->message, svc_setview);
354 MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
356 MSG_WriteByte (&client->message, svc_signonnum);
357 MSG_WriteByte (&client->message, 1);
359 client->sendsignon = true;
360 client->spawned = false; // need prespawn, spawn, etc
367 Initializes a client_t for a new net connection. This will only be called
368 once for a player each game, not once for each level change.
371 void SV_ConnectClient (int clientnum, netconn_t *netconnection)
375 float spawn_parms[NUM_SPAWN_PARMS];
377 client = svs.clients + clientnum;
379 // set up the client_t
381 memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
382 memset (client, 0, sizeof(*client));
383 client->active = true;
384 client->netconnection = netconnection;
386 Con_DPrintf("Client %s connected\n", client->netconnection ? client->netconnection->address : "botclient");
388 strcpy(client->name, "unconnected");
389 strcpy(client->old_name, "unconnected");
390 client->spawned = false;
391 client->edict = EDICT_NUM(clientnum+1);
392 client->message.data = client->msgbuf;
393 client->message.maxsize = sizeof(client->msgbuf);
394 client->message.allowoverflow = true; // we can catch it
395 // updated by receiving "rate" command from client
396 client->rate = NET_MINRATE;
397 // no limits for local player
398 if (client->netconnection && LHNETADDRESS_GetAddressType(&client->netconnection->peeraddress) == LHNETADDRESSTYPE_LOOP)
399 client->rate = 1000000000;
400 client->connecttime = realtime;
403 memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
406 // call the progs to get default spawn parms for the new client
407 PR_ExecuteProgram (pr_global_struct->SetNewParms, "QC function SetNewParms is missing");
408 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
409 client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
412 // don't call SendServerinfo for a fresh botclient because its fields have
413 // not been set up by the qc yet
414 if (client->netconnection)
415 SV_SendServerinfo (client);
420 ===============================================================================
424 ===============================================================================
433 void SV_ClearDatagram (void)
435 SZ_Clear (&sv.datagram);
439 =============================================================================
441 The PVS must include a small area around the client to allow head bobbing
442 or other small motion on the client side. Otherwise, a bob might cause an
443 entity that should be visible to not show up, especially when the bob
446 =============================================================================
449 int sv_writeentitiestoclient_pvsbytes;
450 qbyte sv_writeentitiestoclient_pvs[MAX_MAP_LEAFS/8];
452 static int numsendentities;
453 static entity_state_t sendentities[MAX_EDICTS];
454 static entity_state_t *sendentitiesindex[MAX_EDICTS];
456 void SV_PrepareEntitiesForSending(void)
462 // send all entities that touch the pvs
464 sendentitiesindex[0] = NULL;
465 for (e = 1, ent = NEXT_EDICT(sv.edicts);e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
467 sendentitiesindex[e] = NULL;
474 VectorCopy(ent->v->origin, cs.origin);
475 VectorCopy(ent->v->angles, cs.angles);
477 cs.effects = (int)ent->v->effects;
478 cs.colormap = (qbyte)ent->v->colormap;
479 cs.skin = (qbyte)ent->v->skin;
480 cs.frame = (qbyte)ent->v->frame;
481 cs.viewmodelforclient = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)->edict;
482 cs.exteriormodelforclient = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)->edict;
483 cs.nodrawtoclient = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)->edict;
484 cs.drawonlytoclient = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)->edict;
485 cs.tagentity = GETEDICTFIELDVALUE(ent, eval_tag_entity)->edict;
486 cs.tagindex = (qbyte)GETEDICTFIELDVALUE(ent, eval_tag_index)->_float;
487 i = (int)(GETEDICTFIELDVALUE(ent, eval_glow_size)->_float * 0.25f);
488 cs.glowsize = (qbyte)bound(0, i, 255);
489 if (GETEDICTFIELDVALUE(ent, eval_glow_trail)->_float)
490 cs.flags |= RENDER_GLOWTRAIL;
493 i = (int)ent->v->modelindex;
494 if (i >= 1 && i < MAX_MODELS && *PR_GetString(ent->v->model))
498 f = (GETEDICTFIELDVALUE(ent, eval_alpha)->_float * 255.0f);
502 cs.alpha = (qbyte)bound(0, i, 255);
505 f = (GETEDICTFIELDVALUE(ent, eval_renderamt)->_float);
509 cs.alpha = (qbyte)bound(0, i, 255);
513 f = (GETEDICTFIELDVALUE(ent, eval_scale)->_float * 16.0f);
517 cs.scale = (qbyte)bound(0, i, 255);
521 f = (GETEDICTFIELDVALUE(ent, eval_glow_color)->_float);
523 cs.glowcolor = (int)f;
525 if (GETEDICTFIELDVALUE(ent, eval_fullbright)->_float)
526 cs.effects |= EF_FULLBRIGHT;
528 if (ent->v->movetype == MOVETYPE_STEP)
529 cs.flags |= RENDER_STEP;
530 if ((cs.effects & EF_LOWPRECISION) && cs.origin[0] >= -32768 && cs.origin[1] >= -32768 && cs.origin[2] >= -32768 && cs.origin[0] <= 32767 && cs.origin[1] <= 32767 && cs.origin[2] <= 32767)
531 cs.flags |= RENDER_LOWPRECISION;
532 if (ent->v->colormap >= 1024)
533 cs.flags |= RENDER_COLORMAPPED;
534 if (cs.viewmodelforclient)
535 cs.flags |= RENDER_VIEWMODEL; // show relative to the view
537 f = GETEDICTFIELDVALUE(ent, eval_color)->vector[0]*256;
538 cs.light[0] = (unsigned short)bound(0, f, 65535);
539 f = GETEDICTFIELDVALUE(ent, eval_color)->vector[1]*256;
540 cs.light[1] = (unsigned short)bound(0, f, 65535);
541 f = GETEDICTFIELDVALUE(ent, eval_color)->vector[2]*256;
542 cs.light[2] = (unsigned short)bound(0, f, 65535);
543 f = GETEDICTFIELDVALUE(ent, eval_light_lev)->_float;
544 cs.light[3] = (unsigned short)bound(0, f, 65535);
545 cs.lightstyle = (qbyte)GETEDICTFIELDVALUE(ent, eval_style)->_float;
546 cs.lightpflags = (qbyte)GETEDICTFIELDVALUE(ent, eval_pflags)->_float;
548 if (gamemode == GAME_TENEBRAE)
550 // tenebrae's EF_FULLDYNAMIC conflicts with Q2's EF_NODRAW
554 cs.lightpflags |= PFLAGS_FULLDYNAMIC;
556 // tenebrae's EF_GREEN conflicts with DP's EF_ADDITIVE
564 cs.lightpflags |= PFLAGS_FULLDYNAMIC;
568 cs.specialvisibilityradius = 0;
569 if (cs.lightpflags & PFLAGS_FULLDYNAMIC)
570 cs.specialvisibilityradius = max(cs.specialvisibilityradius, cs.light[3]);
572 cs.specialvisibilityradius = max(cs.specialvisibilityradius, cs.glowsize * 4);
573 if (cs.flags & RENDER_GLOWTRAIL)
574 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 100);
575 if (cs.effects & (EF_BRIGHTFIELD | EF_MUZZLEFLASH | EF_BRIGHTLIGHT | EF_DIMLIGHT | EF_RED | EF_BLUE | EF_FLAME | EF_STARDUST))
577 if (cs.effects & EF_BRIGHTFIELD)
578 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 80);
579 if (cs.effects & EF_MUZZLEFLASH)
580 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 100);
581 if (cs.effects & EF_BRIGHTLIGHT)
582 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 400);
583 if (cs.effects & EF_DIMLIGHT)
584 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 200);
585 if (cs.effects & EF_RED)
586 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 200);
587 if (cs.effects & EF_BLUE)
588 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 200);
589 if (cs.effects & EF_FLAME)
590 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 250);
591 if (cs.effects & EF_STARDUST)
592 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 100);
595 if (numsendentities >= MAX_EDICTS)
597 // we can omit invisible entities with no effects that are not clients
598 // LordHavoc: this could kill tags attached to an invisible entity, I
599 // just hope we never have to support that case
600 if (cs.number > svs.maxclients && ((cs.effects & EF_NODRAW) || (!cs.modelindex && !cs.specialvisibilityradius)))
602 sendentitiesindex[e] = sendentities + numsendentities;
603 sendentities[numsendentities++] = cs;
607 static int sententitiesmark = 0;
608 static int sententities[MAX_EDICTS];
609 static int sententitiesconsideration[MAX_EDICTS];
610 static int sv_writeentitiestoclient_culled_pvs;
611 static int sv_writeentitiestoclient_culled_trace;
612 static int sv_writeentitiestoclient_visibleentities;
613 static int sv_writeentitiestoclient_totalentities;
614 //static entity_frame_t sv_writeentitiestoclient_entityframe;
615 static int sv_writeentitiestoclient_clentnum;
616 static vec3_t sv_writeentitiestoclient_testeye;
617 static client_t *sv_writeentitiestoclient_client;
619 void SV_MarkWriteEntityStateToClient(entity_state_t *s)
622 vec3_t entmins, entmaxs, lightmins, lightmaxs, testorigin;
625 if (sententitiesconsideration[s->number] == sententitiesmark)
627 sententitiesconsideration[s->number] = sententitiesmark;
628 // viewmodels don't have visibility checking
629 if (s->viewmodelforclient)
631 if (s->viewmodelforclient != sv_writeentitiestoclient_clentnum)
634 // never reject player
635 else if (s->number != sv_writeentitiestoclient_clentnum)
637 // check various rejection conditions
638 if (s->nodrawtoclient == sv_writeentitiestoclient_clentnum)
640 if (s->drawonlytoclient && s->drawonlytoclient != sv_writeentitiestoclient_clentnum)
642 if (s->effects & EF_NODRAW)
644 // LordHavoc: only send entities with a model or important effects
645 if (!s->modelindex && s->specialvisibilityradius == 0)
649 // tag attached entities simply check their parent
650 if (!sendentitiesindex[s->tagentity])
652 SV_MarkWriteEntityStateToClient(sendentitiesindex[s->tagentity]);
653 if (sententities[s->tagentity] != sententitiesmark)
656 // always send world submodels, they don't generate much traffic
657 // except in PROTOCOL_QUAKE where they hog bandwidth like crazy
658 else if ((!(isbmodel = (model = sv.models[s->modelindex]) != NULL && model->name[0] == '*') && !(s->effects & EF_NODEPTHTEST)) || sv.protocol == PROTOCOL_QUAKE)
660 Mod_CheckLoaded(model);
661 // entity has survived every check so far, check if visible
662 // enlarged box to account for prediction (not that there is
663 // any currently, but still helps the 'run into a room and
664 // watch items pop up' problem)
665 entmins[0] = s->origin[0] - 32.0f;
666 entmins[1] = s->origin[1] - 32.0f;
667 entmins[2] = s->origin[2] - 32.0f;
668 entmaxs[0] = s->origin[0] + 32.0f;
669 entmaxs[1] = s->origin[1] + 32.0f;
670 entmaxs[2] = s->origin[2] + 32.0f;
671 // using the model's bounding box to ensure things are visible regardless of their physics box
674 if (s->angles[0] || s->angles[2]) // pitch and roll
676 VectorAdd(entmins, model->rotatedmins, entmins);
677 VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
679 else if (s->angles[1])
681 VectorAdd(entmins, model->yawmins, entmins);
682 VectorAdd(entmaxs, model->yawmaxs, entmaxs);
686 VectorAdd(entmins, model->normalmins, entmins);
687 VectorAdd(entmaxs, model->normalmaxs, entmaxs);
690 lightmins[0] = min(entmins[0], s->origin[0] - s->specialvisibilityradius);
691 lightmins[1] = min(entmins[1], s->origin[1] - s->specialvisibilityradius);
692 lightmins[2] = min(entmins[2], s->origin[2] - s->specialvisibilityradius);
693 lightmaxs[0] = max(entmaxs[0], s->origin[0] + s->specialvisibilityradius);
694 lightmaxs[1] = max(entmaxs[1], s->origin[1] + s->specialvisibilityradius);
695 lightmaxs[2] = max(entmaxs[2], s->origin[2] + s->specialvisibilityradius);
696 sv_writeentitiestoclient_totalentities++;
697 // if not touching a visible leaf
698 if (sv_cullentities_pvs.integer && sv_writeentitiestoclient_pvsbytes && sv.worldmodel && sv.worldmodel->brush.BoxTouchingPVS && !sv.worldmodel->brush.BoxTouchingPVS(sv.worldmodel, sv_writeentitiestoclient_pvs, lightmins, lightmaxs))
700 sv_writeentitiestoclient_culled_pvs++;
703 // or not seen by random tracelines
704 if (sv_cullentities_trace.integer && !isbmodel)
706 // LordHavoc: test center first
707 testorigin[0] = (entmins[0] + entmaxs[0]) * 0.5f;
708 testorigin[1] = (entmins[1] + entmaxs[1]) * 0.5f;
709 testorigin[2] = (entmins[2] + entmaxs[2]) * 0.5f;
710 sv.worldmodel->TraceBox(sv.worldmodel, 0, &trace, sv_writeentitiestoclient_testeye, sv_writeentitiestoclient_testeye, testorigin, testorigin, SUPERCONTENTS_SOLID);
711 if (trace.fraction == 1 || BoxesOverlap(trace.endpos, trace.endpos, entmins, entmaxs))
712 sv_writeentitiestoclient_client->visibletime[s->number] = realtime + 1;
715 // LordHavoc: test random offsets, to maximize chance of detection
716 testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
717 testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
718 testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
719 sv.worldmodel->TraceBox(sv.worldmodel, 0, &trace, sv_writeentitiestoclient_testeye, sv_writeentitiestoclient_testeye, testorigin, testorigin, SUPERCONTENTS_SOLID);
720 if (trace.fraction == 1 || BoxesOverlap(trace.endpos, trace.endpos, entmins, entmaxs))
721 sv_writeentitiestoclient_client->visibletime[s->number] = realtime + 1;
724 if (s->specialvisibilityradius)
726 // LordHavoc: test random offsets, to maximize chance of detection
727 testorigin[0] = lhrandom(lightmins[0], lightmaxs[0]);
728 testorigin[1] = lhrandom(lightmins[1], lightmaxs[1]);
729 testorigin[2] = lhrandom(lightmins[2], lightmaxs[2]);
730 sv.worldmodel->TraceBox(sv.worldmodel, 0, &trace, sv_writeentitiestoclient_testeye, sv_writeentitiestoclient_testeye, testorigin, testorigin, SUPERCONTENTS_SOLID);
731 if (trace.fraction == 1 || BoxesOverlap(trace.endpos, trace.endpos, entmins, entmaxs))
732 sv_writeentitiestoclient_client->visibletime[s->number] = realtime + 1;
736 if (realtime > sv_writeentitiestoclient_client->visibletime[s->number])
738 sv_writeentitiestoclient_culled_trace++;
742 sv_writeentitiestoclient_visibleentities++;
745 // this just marks it for sending
746 // FIXME: it would be more efficient to send here, but the entity
747 // compressor isn't that flexible
748 sententities[s->number] = sententitiesmark;
751 entity_state_t sendstates[MAX_EDICTS];
753 void SV_WriteEntitiesToClient(client_t *client, edict_t *clent, sizebuf_t *msg)
755 int i, numsendstates;
758 // if there isn't enough space to accomplish anything, skip it
759 if (msg->cursize + 25 > msg->maxsize)
762 sv_writeentitiestoclient_client = client;
764 sv_writeentitiestoclient_culled_pvs = 0;
765 sv_writeentitiestoclient_culled_trace = 0;
766 sv_writeentitiestoclient_visibleentities = 0;
767 sv_writeentitiestoclient_totalentities = 0;
769 Mod_CheckLoaded(sv.worldmodel);
771 // find the client's PVS
772 // the real place being tested from
773 VectorAdd(clent->v->origin, clent->v->view_ofs, sv_writeentitiestoclient_testeye);
774 sv_writeentitiestoclient_pvsbytes = 0;
775 if (sv.worldmodel && sv.worldmodel->brush.FatPVS)
776 sv_writeentitiestoclient_pvsbytes = sv.worldmodel->brush.FatPVS(sv.worldmodel, sv_writeentitiestoclient_testeye, 8, sv_writeentitiestoclient_pvs, sizeof(sv_writeentitiestoclient_pvs));
778 sv_writeentitiestoclient_clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
782 for (i = 0;i < numsendentities;i++)
783 SV_MarkWriteEntityStateToClient(sendentities + i);
786 for (i = 0;i < numsendentities;i++)
788 if (sententities[sendentities[i].number] == sententitiesmark)
790 s = &sendstates[numsendstates++];
791 *s = sendentities[i];
792 if (s->exteriormodelforclient && s->exteriormodelforclient == sv_writeentitiestoclient_clentnum)
793 s->flags |= RENDER_EXTERIORMODEL;
797 if (sv_cullentities_stats.integer)
798 Con_Printf("client \"%s\" entities: %d total, %d visible, %d culled by: %d pvs %d trace\n", client->name, sv_writeentitiestoclient_totalentities, sv_writeentitiestoclient_visibleentities, sv_writeentitiestoclient_culled_pvs + sv_writeentitiestoclient_culled_trace, sv_writeentitiestoclient_culled_pvs, sv_writeentitiestoclient_culled_trace);
800 if (client->entitydatabase5)
801 EntityFrame5_WriteFrame(msg, client->entitydatabase5, numsendstates, sendstates, client - svs.clients + 1);
802 else if (client->entitydatabase4)
803 EntityFrame4_WriteFrame(msg, client->entitydatabase4, numsendstates, sendstates);
804 else if (client->entitydatabase)
805 EntityFrame_WriteFrame(msg, client->entitydatabase, numsendstates, sendstates, client - svs.clients + 1);
807 EntityFrameQuake_WriteFrame(msg, numsendstates, sendstates);
816 void SV_CleanupEnts (void)
821 ent = NEXT_EDICT(sv.edicts);
822 for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
823 ent->v->effects = (int)ent->v->effects & ~EF_MUZZLEFLASH;
828 SV_WriteClientdataToMessage
832 void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
843 // send a damage message
845 if (ent->v->dmg_take || ent->v->dmg_save)
847 other = PROG_TO_EDICT(ent->v->dmg_inflictor);
848 MSG_WriteByte (msg, svc_damage);
849 MSG_WriteByte (msg, ent->v->dmg_save);
850 MSG_WriteByte (msg, ent->v->dmg_take);
851 for (i=0 ; i<3 ; i++)
852 MSG_WriteCoord (msg, other->v->origin[i] + 0.5*(other->v->mins[i] + other->v->maxs[i]), sv.protocol);
854 ent->v->dmg_take = 0;
855 ent->v->dmg_save = 0;
859 // send the current viewpos offset from the view entity
861 SV_SetIdealPitch (); // how much to look up / down ideally
863 // a fixangle might get lost in a dropped packet. Oh well.
864 if ( ent->v->fixangle )
866 MSG_WriteByte (msg, svc_setangle);
867 for (i=0 ; i < 3 ; i++)
868 MSG_WriteAngle (msg, ent->v->angles[i], sv.protocol);
869 ent->v->fixangle = 0;
874 if (ent->v->view_ofs[2] != DEFAULT_VIEWHEIGHT)
875 bits |= SU_VIEWHEIGHT;
877 if (ent->v->idealpitch)
878 bits |= SU_IDEALPITCH;
880 // stuff the sigil bits into the high bits of items for sbar, or else
882 val = GETEDICTFIELDVALUE(ent, eval_items2);
885 items = (int)ent->v->items | ((int)val->_float << 23);
887 items = (int)ent->v->items | ((int)pr_global_struct->serverflags << 28);
891 if ( (int)ent->v->flags & FL_ONGROUND)
894 if ( ent->v->waterlevel >= 2)
897 // PROTOCOL_DARKPLACES
898 VectorClear(punchvector);
899 if ((val = GETEDICTFIELDVALUE(ent, eval_punchvector)))
900 VectorCopy(val->vector, punchvector);
903 if ((val = GETEDICTFIELDVALUE(ent, eval_viewzoom)))
905 i = val->_float * 255.0f;
909 i = bound(0, i, 65535);
913 // FIXME: which protocols support this? does PROTOCOL_DARKPLACES3 support viewzoom?
914 if (sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5)
918 for (i=0 ; i<3 ; i++)
920 if (ent->v->punchangle[i])
921 bits |= (SU_PUNCH1<<i);
922 if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5)
924 bits |= (SU_PUNCHVEC1<<i);
925 if (ent->v->velocity[i])
926 bits |= (SU_VELOCITY1<<i);
929 if (ent->v->weaponframe)
930 bits |= SU_WEAPONFRAME;
932 if (ent->v->armorvalue)
939 if (bits >= 16777216)
944 MSG_WriteByte (msg, svc_clientdata);
945 MSG_WriteShort (msg, bits);
946 if (bits & SU_EXTEND1)
947 MSG_WriteByte(msg, bits >> 16);
948 if (bits & SU_EXTEND2)
949 MSG_WriteByte(msg, bits >> 24);
951 if (bits & SU_VIEWHEIGHT)
952 MSG_WriteChar (msg, ent->v->view_ofs[2]);
954 if (bits & SU_IDEALPITCH)
955 MSG_WriteChar (msg, ent->v->idealpitch);
957 for (i=0 ; i<3 ; i++)
959 if (bits & (SU_PUNCH1<<i))
961 if (sv.protocol == PROTOCOL_QUAKE)
962 MSG_WriteChar(msg, ent->v->punchangle[i]);
963 else if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5)
964 MSG_WriteAngle16i(msg, ent->v->punchangle[i]);
966 if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5)
968 if (bits & (SU_PUNCHVEC1<<i))
970 if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4)
971 MSG_WriteCoord16i(msg, punchvector[i]);
972 else if (sv.protocol == PROTOCOL_DARKPLACES5)
973 MSG_WriteCoord32f(msg, punchvector[i]);
976 if (bits & (SU_VELOCITY1<<i))
978 if (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4)
979 MSG_WriteChar(msg, ent->v->velocity[i] * (1.0f / 16.0f));
980 else if (sv.protocol == PROTOCOL_DARKPLACES5)
981 MSG_WriteCoord32f(msg, ent->v->velocity[i]);
985 // [always sent] if (bits & SU_ITEMS)
986 MSG_WriteLong (msg, items);
988 if (sv.protocol == PROTOCOL_DARKPLACES5)
990 if (bits & SU_WEAPONFRAME)
991 MSG_WriteShort (msg, ent->v->weaponframe);
993 MSG_WriteShort (msg, ent->v->armorvalue);
994 if (bits & SU_WEAPON)
996 i = SV_ModelIndex(PR_GetString(ent->v->weaponmodel));
999 Con_DPrintf("weaponmodel \"%s\" not precached\n", PR_GetString(ent->v->weaponmodel));
1002 MSG_WriteShort (msg, i);
1005 MSG_WriteShort (msg, ent->v->health);
1006 MSG_WriteShort (msg, ent->v->currentammo);
1007 MSG_WriteShort (msg, ent->v->ammo_shells);
1008 MSG_WriteShort (msg, ent->v->ammo_nails);
1009 MSG_WriteShort (msg, ent->v->ammo_rockets);
1010 MSG_WriteShort (msg, ent->v->ammo_cells);
1012 MSG_WriteShort (msg, ent->v->weapon);
1014 if (bits & SU_VIEWZOOM)
1015 MSG_WriteShort (msg, viewzoom);
1019 if (bits & SU_WEAPONFRAME)
1020 MSG_WriteByte (msg, ent->v->weaponframe);
1021 if (bits & SU_ARMOR)
1022 MSG_WriteByte (msg, ent->v->armorvalue);
1023 if (bits & SU_WEAPON)
1025 i = SV_ModelIndex(PR_GetString(ent->v->weaponmodel));
1028 Con_DPrintf("weaponmodel \"%s\" not precached\n", PR_GetString(ent->v->weaponmodel));
1031 MSG_WriteByte (msg, i);
1034 MSG_WriteShort (msg, ent->v->health);
1035 MSG_WriteByte (msg, ent->v->currentammo);
1036 MSG_WriteByte (msg, ent->v->ammo_shells);
1037 MSG_WriteByte (msg, ent->v->ammo_nails);
1038 MSG_WriteByte (msg, ent->v->ammo_rockets);
1039 MSG_WriteByte (msg, ent->v->ammo_cells);
1041 if (gamemode == GAME_HIPNOTIC || gamemode == GAME_ROGUE || gamemode == GAME_NEXUIZ)
1045 if ( ((int)ent->v->weapon) & (1<<i) )
1047 MSG_WriteByte (msg, i);
1054 MSG_WriteByte (msg, ent->v->weapon);
1057 if (bits & SU_VIEWZOOM)
1059 if (sv.protocol == PROTOCOL_DARKPLACES4)
1061 viewzoom = min(viewzoom, 255);
1062 MSG_WriteByte (msg, viewzoom);
1064 else if (sv.protocol == PROTOCOL_DARKPLACES5)
1065 MSG_WriteShort (msg, viewzoom);
1071 =======================
1072 SV_SendClientDatagram
1073 =======================
1075 static qbyte sv_sendclientdatagram_buf[NET_MAXMESSAGE]; // FIXME?
1076 qboolean SV_SendClientDatagram (client_t *client)
1078 int rate, maxrate, maxsize, maxsize2;
1081 if (LHNETADDRESS_GetAddressType(&host_client->netconnection->peeraddress) == LHNETADDRESSTYPE_LOOP && !sv_ratelimitlocalplayer.integer)
1083 // for good singleplayer, send huge packets
1084 maxsize = sizeof(sv_sendclientdatagram_buf);
1085 maxsize2 = sizeof(sv_sendclientdatagram_buf);
1087 else if (sv.protocol == PROTOCOL_DARKPLACES5)
1089 // PROTOCOL_DARKPLACES5 supports packet size limiting of updates
1090 maxrate = bound(NET_MINRATE, sv_maxrate.integer, NET_MAXRATE);
1091 if (sv_maxrate.integer != maxrate)
1092 Cvar_SetValueQuick(&sv_maxrate, maxrate);
1094 rate = bound(NET_MINRATE, client->rate, maxrate);
1095 rate = (int)(client->rate * sys_ticrate.value);
1096 maxsize = bound(100, rate, 1400);
1101 // no rate limiting support on older protocols because dp protocols
1102 // 1-4 kick the client off if they overflow, and quake protocol shows
1103 // less than the full entity set if rate limited
1108 msg.data = sv_sendclientdatagram_buf;
1109 msg.maxsize = maxsize;
1112 MSG_WriteByte (&msg, svc_time);
1113 MSG_WriteFloat (&msg, sv.time);
1115 // add the client specific data to the datagram
1116 SV_WriteClientdataToMessage (client->edict, &msg);
1118 SV_WriteEntitiesToClient (client, client->edict, &msg);
1120 // expand packet size to allow effects to go over the rate limit
1121 // (dropping them is FAR too ugly)
1122 msg.maxsize = maxsize2;
1124 // copy the server datagram if there is space
1125 // FIXME: put in delayed queue of effects to send
1126 if (sv.datagram.cursize > 0 && msg.cursize + sv.datagram.cursize <= msg.maxsize)
1127 SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
1129 // send the datagram
1130 if (NetConn_SendUnreliableMessage (client->netconnection, &msg) == -1)
1132 SV_DropClient (true);// if the message couldn't send, kick off
1140 =======================
1141 SV_UpdateToReliableMessages
1142 =======================
1144 void SV_UpdateToReliableMessages (void)
1151 // check for changes to be sent over the reliable streams
1152 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1154 // update the host_client fields we care about according to the entity fields
1155 sv_player = EDICT_NUM(i+1);
1158 name = PR_GetString(sv_player->v->netname);
1161 // always point the string back at host_client->name to keep it safe
1162 strlcpy (host_client->name, name, sizeof (host_client->name));
1163 sv_player->v->netname = PR_SetString(host_client->name);
1164 if (strcmp(host_client->old_name, host_client->name))
1166 if (host_client->spawned)
1167 SV_BroadcastPrintf("%s changed name to %s\n", host_client->old_name, host_client->name);
1168 strcpy(host_client->old_name, host_client->name);
1169 // send notification to all clients
1170 MSG_WriteByte (&sv.reliable_datagram, svc_updatename);
1171 MSG_WriteByte (&sv.reliable_datagram, i);
1172 MSG_WriteString (&sv.reliable_datagram, host_client->name);
1175 // DP_SV_CLIENTCOLORS
1176 // this is always found (since it's added by the progs loader)
1177 if ((val = GETEDICTFIELDVALUE(sv_player, eval_clientcolors)))
1178 host_client->colors = (int)val->_float;
1179 if (host_client->old_colors != host_client->colors)
1181 host_client->old_colors = host_client->colors;
1182 // send notification to all clients
1183 MSG_WriteByte (&sv.reliable_datagram, svc_updatecolors);
1184 MSG_WriteByte (&sv.reliable_datagram, i);
1185 MSG_WriteByte (&sv.reliable_datagram, host_client->colors);
1189 host_client->frags = (int)sv_player->v->frags;
1190 if (host_client->old_frags != host_client->frags)
1192 host_client->old_frags = host_client->frags;
1193 // send notification to all clients
1194 MSG_WriteByte (&sv.reliable_datagram, svc_updatefrags);
1195 MSG_WriteByte (&sv.reliable_datagram, i);
1196 MSG_WriteShort (&sv.reliable_datagram, host_client->frags);
1200 for (j = 0, client = svs.clients;j < svs.maxclients;j++, client++)
1201 if (client->netconnection)
1202 SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
1204 SZ_Clear (&sv.reliable_datagram);
1209 =======================
1212 Send a nop message without trashing or sending the accumulated client
1214 =======================
1216 void SV_SendNop (client_t *client)
1222 msg.maxsize = sizeof(buf);
1225 MSG_WriteChar (&msg, svc_nop);
1227 if (NetConn_SendUnreliableMessage (client->netconnection, &msg) == -1)
1228 SV_DropClient (true); // if the message couldn't send, kick off
1229 client->last_message = realtime;
1233 =======================
1234 SV_SendClientMessages
1235 =======================
1237 void SV_SendClientMessages (void)
1239 int i, prepared = false;
1241 // update frags, names, etc
1242 SV_UpdateToReliableMessages();
1244 // build individual updates
1245 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1247 if (!host_client->active)
1249 if (!host_client->netconnection)
1251 SZ_Clear(&host_client->message);
1255 if (host_client->deadsocket || host_client->message.overflowed)
1257 SV_DropClient (true); // if the message couldn't send, kick off
1261 if (host_client->spawned)
1266 // only prepare entities once per frame
1267 SV_PrepareEntitiesForSending();
1269 if (!SV_SendClientDatagram (host_client))
1274 // the player isn't totally in the game yet
1275 // send small keepalive messages if too much time has passed
1276 // send a full message when the next signon stage has been requested
1277 // some other message data (name changes, etc) may accumulate
1278 // between signon stages
1279 if (!host_client->sendsignon)
1281 if (realtime - host_client->last_message > 5)
1282 SV_SendNop (host_client);
1283 continue; // don't send out non-signon messages
1287 if (host_client->message.cursize || host_client->dropasap)
1289 if (!NetConn_CanSendMessage (host_client->netconnection))
1292 if (host_client->dropasap)
1293 SV_DropClient (false); // went to another level
1296 if (NetConn_SendReliableMessage (host_client->netconnection, &host_client->message) == -1)
1297 SV_DropClient (true); // if the message couldn't send, kick off
1298 SZ_Clear (&host_client->message);
1299 host_client->last_message = realtime;
1300 host_client->sendsignon = false;
1305 // clear muzzle flashes
1311 ==============================================================================
1315 ==============================================================================
1324 int SV_ModelIndex (const char *name)
1328 if (!name || !name[0])
1331 for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
1332 if (!strcmp(sv.model_precache[i], name))
1334 if (i==MAX_MODELS || !sv.model_precache[i])
1336 Con_DPrintf ("SV_ModelIndex: model %s not precached", name);
1348 void SV_CreateBaseline (void)
1350 int i, entnum, large;
1353 // LordHavoc: clear *all* states (note just active ones)
1354 for (entnum = 0;entnum < sv.max_edicts;entnum++)
1356 // get the current server version
1357 svent = EDICT_NUM(entnum);
1359 // LordHavoc: always clear state values, whether the entity is in use or not
1360 svent->e->baseline = defaultstate;
1364 if (entnum > svs.maxclients && !svent->v->modelindex)
1367 // create entity baseline
1368 VectorCopy (svent->v->origin, svent->e->baseline.origin);
1369 VectorCopy (svent->v->angles, svent->e->baseline.angles);
1370 svent->e->baseline.frame = svent->v->frame;
1371 svent->e->baseline.skin = svent->v->skin;
1372 if (entnum > 0 && entnum <= svs.maxclients)
1374 svent->e->baseline.colormap = entnum;
1375 i = SV_ModelIndex("progs/player.mdl");
1378 svent->e->baseline.modelindex = i;
1382 svent->e->baseline.colormap = 0;
1383 svent->e->baseline.modelindex = svent->v->modelindex;
1387 if (svent->e->baseline.modelindex & 0xFF00 || svent->e->baseline.frame & 0xFF00)
1390 // add to the message
1392 MSG_WriteByte (&sv.signon, svc_spawnbaseline2);
1394 MSG_WriteByte (&sv.signon, svc_spawnbaseline);
1395 MSG_WriteShort (&sv.signon, entnum);
1399 MSG_WriteShort (&sv.signon, svent->e->baseline.modelindex);
1400 MSG_WriteShort (&sv.signon, svent->e->baseline.frame);
1404 MSG_WriteByte (&sv.signon, svent->e->baseline.modelindex);
1405 MSG_WriteByte (&sv.signon, svent->e->baseline.frame);
1407 MSG_WriteByte (&sv.signon, svent->e->baseline.colormap);
1408 MSG_WriteByte (&sv.signon, svent->e->baseline.skin);
1409 for (i=0 ; i<3 ; i++)
1411 MSG_WriteCoord(&sv.signon, svent->e->baseline.origin[i], sv.protocol);
1412 MSG_WriteAngle(&sv.signon, svent->e->baseline.angles[i], sv.protocol);
1422 Tell all the clients that the server is changing levels
1425 void SV_SendReconnect (void)
1432 msg.maxsize = sizeof(data);
1434 MSG_WriteChar (&msg, svc_stufftext);
1435 MSG_WriteString (&msg, "reconnect\n");
1436 NetConn_SendToAll (&msg, 5);
1438 if (cls.state != ca_dedicated)
1439 Cmd_ExecuteString ("reconnect\n", src_command);
1447 Grabs the current state of each client for saving across the
1448 transition to another level
1451 void SV_SaveSpawnparms (void)
1455 svs.serverflags = pr_global_struct->serverflags;
1457 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1459 if (!host_client->active)
1462 // call the progs to get default spawn parms for the new client
1463 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1464 PR_ExecuteProgram (pr_global_struct->SetChangeParms, "QC function SetChangeParms is missing");
1465 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1466 host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1470 void SV_IncreaseEdicts(void)
1474 int oldmax_edicts = sv.max_edicts;
1475 void *oldedictsengineprivate = sv.edictsengineprivate;
1476 void *oldedictsfields = sv.edictsfields;
1477 void *oldmoved_edicts = sv.moved_edicts;
1479 if (sv.max_edicts >= MAX_EDICTS)
1482 // links don't survive the transition, so unlink everything
1483 for (i = 0, ent = sv.edicts;i < sv.max_edicts;i++, ent++)
1486 SV_UnlinkEdict(sv.edicts + i);
1487 memset(&ent->e->areagrid, 0, sizeof(ent->e->areagrid));
1491 sv.max_edicts = min(sv.max_edicts + 256, MAX_EDICTS);
1492 sv.edictsengineprivate = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_engineprivate_t));
1493 sv.edictsfields = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1494 sv.moved_edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_t *));
1496 memcpy(sv.edictsengineprivate, oldedictsengineprivate, oldmax_edicts * sizeof(edict_engineprivate_t));
1497 memcpy(sv.edictsfields, oldedictsfields, oldmax_edicts * pr_edict_size);
1499 for (i = 0, ent = sv.edicts;i < sv.max_edicts;i++, ent++)
1501 ent->e = sv.edictsengineprivate + i;
1502 ent->v = (void *)((qbyte *)sv.edictsfields + i * pr_edict_size);
1503 // link every entity except world
1505 SV_LinkEdict(ent, false);
1508 Mem_Free(oldedictsengineprivate);
1509 Mem_Free(oldedictsfields);
1510 Mem_Free(oldmoved_edicts);
1517 This is called at the start of each level
1520 extern float scr_centertime_off;
1522 void SV_SpawnServer (const char *server)
1527 model_t *worldmodel;
1528 char modelname[sizeof(sv.modelname)];
1530 Con_DPrintf("SpawnServer: %s\n", server);
1532 snprintf (modelname, sizeof(modelname), "maps/%s.bsp", server);
1533 worldmodel = Mod_ForName(modelname, false, true, true);
1534 if (!worldmodel || !worldmodel->TraceBox)
1536 Con_Printf("Couldn't load map %s\n", modelname);
1540 // let's not have any servers with no name
1541 if (hostname.string[0] == 0)
1542 Cvar_Set ("hostname", "UNNAMED");
1543 scr_centertime_off = 0;
1545 svs.changelevel_issued = false; // now safe to issue another
1548 // tell all connected clients that we are going to a new level
1554 // make sure cvars have been checked before opening the ports
1555 NetConn_ServerFrame();
1556 NetConn_OpenServerPorts(true);
1560 // make cvars consistant
1563 Cvar_SetValue ("deathmatch", 0);
1564 current_skill = bound(0, (int)(skill.value + 0.5), 3);
1566 Cvar_SetValue ("skill", (float)current_skill);
1569 // set up the new server
1571 Host_ClearMemory ();
1573 memset (&sv, 0, sizeof(sv));
1575 strlcpy (sv.name, server, sizeof (sv.name));
1578 if (!strcasecmp(sv_protocolname.string, "QUAKE"))
1580 sv.protocol = PROTOCOL_QUAKE;
1581 sv.netquakecompatible = true;
1583 else if (!strcasecmp(sv_protocolname.string, "QUAKEDP"))
1585 sv.protocol = PROTOCOL_QUAKE;
1586 sv.netquakecompatible = false;
1588 else if (!strcasecmp(sv_protocolname.string, "DARKPLACES1"))
1590 sv.protocol = PROTOCOL_DARKPLACES1;
1591 sv.netquakecompatible = false;
1593 else if (!strcasecmp(sv_protocolname.string, "DARKPLACES2"))
1595 sv.protocol = PROTOCOL_DARKPLACES2;
1596 sv.netquakecompatible = false;
1598 else if (!strcasecmp(sv_protocolname.string, "DARKPLACES3"))
1600 sv.protocol = PROTOCOL_DARKPLACES3;
1601 sv.netquakecompatible = false;
1603 else if (!strcasecmp(sv_protocolname.string, "DARKPLACES4"))
1605 sv.protocol = PROTOCOL_DARKPLACES4;
1606 sv.netquakecompatible = false;
1608 else if (!strcasecmp(sv_protocolname.string, "DARKPLACES5"))
1610 sv.protocol = PROTOCOL_DARKPLACES5;
1611 sv.netquakecompatible = false;
1615 sv.protocol = PROTOCOL_DARKPLACES5;
1616 sv.netquakecompatible = false;
1617 Con_Printf("Unknown sv_protocolname \"%s\", valid values are QUAKE, QUAKEDP, DARKPLACES1, DARKPLACES2, DARKPLACES3, DARKPLACES4, DARKPLACES5, falling back to DARKPLACES5 protocol\n", sv_protocolname.string);
1620 // load progs to get entity field count
1623 // allocate server memory
1624 // start out with just enough room for clients and a reasonable estimate of entities
1625 sv.max_edicts = max(svs.maxclients + 1, 512);
1626 sv.max_edicts = min(sv.max_edicts, MAX_EDICTS);
1628 // clear the edict memory pool
1629 Mem_EmptyPool(sv_edicts_mempool);
1630 // edict_t structures (hidden from progs)
1631 sv.edicts = Mem_Alloc(sv_edicts_mempool, MAX_EDICTS * sizeof(edict_t));
1632 // engine private structures (hidden from progs)
1633 sv.edictsengineprivate = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_engineprivate_t));
1634 // progs fields, often accessed by server
1635 sv.edictsfields = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1636 // used by PushMove to move back pushed entities
1637 sv.moved_edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_t *));
1638 for (i = 0;i < sv.max_edicts;i++)
1640 ent = sv.edicts + i;
1641 ent->e = sv.edictsengineprivate + i;
1642 ent->v = (void *)((qbyte *)sv.edictsfields + i * pr_edict_size);
1645 sv.datagram.maxsize = sizeof(sv.datagram_buf);
1646 sv.datagram.cursize = 0;
1647 sv.datagram.data = sv.datagram_buf;
1649 sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1650 sv.reliable_datagram.cursize = 0;
1651 sv.reliable_datagram.data = sv.reliable_datagram_buf;
1653 sv.signon.maxsize = sizeof(sv.signon_buf);
1654 sv.signon.cursize = 0;
1655 sv.signon.data = sv.signon_buf;
1657 // leave slots at start for clients only
1658 sv.num_edicts = svs.maxclients+1;
1660 sv.state = ss_loading;
1666 worldmodel->used = true;
1668 strlcpy (sv.name, server, sizeof (sv.name));
1669 strcpy(sv.modelname, modelname);
1670 sv.worldmodel = worldmodel;
1671 sv.models[1] = sv.worldmodel;
1674 // clear world interaction links
1678 sv.sound_precache[0] = "";
1680 sv.model_precache[0] = "";
1681 sv.model_precache[1] = sv.modelname;
1682 for (i = 1;i < sv.worldmodel->brush.numsubmodels;i++)
1684 sv.model_precache[i+1] = localmodels[i];
1685 sv.models[i+1] = Mod_ForName (localmodels[i], false, false, false);
1689 // load the rest of the entities
1692 memset (ent->v, 0, progs->entityfields * 4);
1693 ent->e->free = false;
1694 ent->v->model = PR_SetString(sv.modelname);
1695 ent->v->modelindex = 1; // world model
1696 ent->v->solid = SOLID_BSP;
1697 ent->v->movetype = MOVETYPE_PUSH;
1700 pr_global_struct->coop = coop.integer;
1702 pr_global_struct->deathmatch = deathmatch.integer;
1704 pr_global_struct->mapname = PR_SetString(sv.name);
1706 // serverflags are for cross level information (sigils)
1707 pr_global_struct->serverflags = svs.serverflags;
1709 // load replacement entity file if found
1711 if (sv_entpatch.integer)
1712 entities = FS_LoadFile(va("maps/%s.ent", sv.name), tempmempool, true);
1715 Con_Printf("Loaded maps/%s.ent\n", sv.name);
1716 ED_LoadFromFile (entities);
1720 ED_LoadFromFile (sv.worldmodel->brush.entities);
1723 // LordHavoc: clear world angles (to fix e3m3.bsp)
1724 VectorClear(sv.edicts->v->angles);
1728 // all setup is completed, any further precache statements are errors
1729 sv.state = ss_active;
1731 // run two frames to allow everything to settle
1732 for (i = 0;i < 2;i++)
1734 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1740 // create a baseline for more efficient communications
1741 if (sv.protocol == PROTOCOL_QUAKE)
1742 SV_CreateBaseline ();
1744 // send serverinfo to all connected clients
1745 // (note this also handles botclients coming back from a level change)
1746 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1747 if (host_client->active)
1748 SV_SendServerinfo(host_client);
1750 Con_DPrint("Server spawned.\n");
1751 NetConn_Heartbeat (2);