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_DARKPLACES6 for example, minus the PROTOCOL_ prefix
26 cvar_t sv_protocolname = {0, "sv_protocolname", "DARKPLACES6"};
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"};
42 cvar_t sv_gameplayfix_setmodelrealbox = {0, "sv_gameplayfix_setmodelrealbox", "1"};
44 cvar_t sv_progs = {0, "sv_progs", "progs.dat" };
49 mempool_t *sv_edicts_mempool = NULL;
51 //============================================================================
53 extern void SV_Phys_Init (void);
54 extern void SV_World_Init (void);
55 static void SV_SaveEntFile_f(void);
64 Cmd_AddCommand("sv_saveentfile", SV_SaveEntFile_f);
65 Cvar_RegisterVariable (&sv_maxvelocity);
66 Cvar_RegisterVariable (&sv_gravity);
67 Cvar_RegisterVariable (&sv_friction);
68 Cvar_RegisterVariable (&sv_edgefriction);
69 Cvar_RegisterVariable (&sv_stopspeed);
70 Cvar_RegisterVariable (&sv_maxspeed);
71 Cvar_RegisterVariable (&sv_accelerate);
72 Cvar_RegisterVariable (&sv_idealpitchscale);
73 Cvar_RegisterVariable (&sv_aim);
74 Cvar_RegisterVariable (&sv_nostep);
75 Cvar_RegisterVariable (&sv_deltacompress);
76 Cvar_RegisterVariable (&sv_cullentities_pvs);
77 Cvar_RegisterVariable (&sv_cullentities_trace);
78 Cvar_RegisterVariable (&sv_cullentities_stats);
79 Cvar_RegisterVariable (&sv_entpatch);
80 Cvar_RegisterVariable (&sv_gameplayfix_grenadebouncedownslopes);
81 Cvar_RegisterVariable (&sv_gameplayfix_noairborncorpse);
82 Cvar_RegisterVariable (&sv_gameplayfix_stepdown);
83 Cvar_RegisterVariable (&sv_gameplayfix_stepwhilejumping);
84 Cvar_RegisterVariable (&sv_gameplayfix_swiminbmodels);
85 Cvar_RegisterVariable (&sv_gameplayfix_setmodelrealbox);
86 Cvar_RegisterVariable (&sv_protocolname);
87 Cvar_RegisterVariable (&sv_ratelimitlocalplayer);
88 Cvar_RegisterVariable (&sv_maxrate);
89 Cvar_RegisterVariable (&sv_progs);
94 sv_edicts_mempool = Mem_AllocPool("server edicts", 0, NULL);
97 static void SV_SaveEntFile_f(void)
99 char basename[MAX_QPATH];
100 if (!sv.active || !sv.worldmodel)
102 Con_Print("Not running a server\n");
105 FS_StripExtension(sv.worldmodel->name, basename, sizeof(basename));
106 FS_WriteFile(va("%s.ent", basename), sv.worldmodel->brush.entities, strlen(sv.worldmodel->brush.entities));
111 =============================================================================
115 =============================================================================
122 Make sure the event gets sent to all clients
125 void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
129 if (sv.datagram.cursize > MAX_PACKETFRAGMENT-18)
131 MSG_WriteByte (&sv.datagram, svc_particle);
132 MSG_WriteCoord (&sv.datagram, org[0], sv.protocol);
133 MSG_WriteCoord (&sv.datagram, org[1], sv.protocol);
134 MSG_WriteCoord (&sv.datagram, org[2], sv.protocol);
135 for (i=0 ; i<3 ; i++)
142 MSG_WriteChar (&sv.datagram, v);
144 MSG_WriteByte (&sv.datagram, count);
145 MSG_WriteByte (&sv.datagram, color);
152 Make sure the event gets sent to all clients
155 void SV_StartEffect (vec3_t org, int modelindex, int startframe, int framecount, int framerate)
157 if (modelindex >= 256 || startframe >= 256)
159 if (sv.datagram.cursize > MAX_PACKETFRAGMENT-19)
161 MSG_WriteByte (&sv.datagram, svc_effect2);
162 MSG_WriteCoord (&sv.datagram, org[0], sv.protocol);
163 MSG_WriteCoord (&sv.datagram, org[1], sv.protocol);
164 MSG_WriteCoord (&sv.datagram, org[2], sv.protocol);
165 MSG_WriteShort (&sv.datagram, modelindex);
166 MSG_WriteShort (&sv.datagram, startframe);
167 MSG_WriteByte (&sv.datagram, framecount);
168 MSG_WriteByte (&sv.datagram, framerate);
172 if (sv.datagram.cursize > MAX_PACKETFRAGMENT-17)
174 MSG_WriteByte (&sv.datagram, svc_effect);
175 MSG_WriteCoord (&sv.datagram, org[0], sv.protocol);
176 MSG_WriteCoord (&sv.datagram, org[1], sv.protocol);
177 MSG_WriteCoord (&sv.datagram, org[2], sv.protocol);
178 MSG_WriteByte (&sv.datagram, modelindex);
179 MSG_WriteByte (&sv.datagram, startframe);
180 MSG_WriteByte (&sv.datagram, framecount);
181 MSG_WriteByte (&sv.datagram, framerate);
189 Each entity can have eight independant sound sources, like voice,
192 Channel 0 is an auto-allocate channel, the others override anything
193 already running on that entity/channel pair.
195 An attenuation of 0 will play full volume everywhere in the level.
196 Larger attenuations will drop off. (max 4 attenuation)
200 void SV_StartSound (edict_t *entity, int channel, char *sample, int volume, float attenuation)
202 int sound_num, field_mask, i, ent;
204 if (volume < 0 || volume > 255)
205 Host_Error ("SV_StartSound: volume = %i", volume);
207 if (attenuation < 0 || attenuation > 4)
208 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
210 if (channel < 0 || channel > 7)
211 Host_Error ("SV_StartSound: channel = %i", channel);
213 if (sv.datagram.cursize > MAX_PACKETFRAGMENT-21)
216 // find precache number for sound
217 sound_num = SV_SoundIndex(sample, 1);
221 ent = NUM_FOR_EDICT(entity);
224 if (volume != DEFAULT_SOUND_PACKET_VOLUME)
225 field_mask |= SND_VOLUME;
226 if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
227 field_mask |= SND_ATTENUATION;
229 field_mask |= SND_LARGEENTITY;
230 if (sound_num >= 256 || channel >= 8)
231 field_mask |= SND_LARGESOUND;
233 // directed messages go only to the entity they are targeted on
234 MSG_WriteByte (&sv.datagram, svc_sound);
235 MSG_WriteByte (&sv.datagram, field_mask);
236 if (field_mask & SND_VOLUME)
237 MSG_WriteByte (&sv.datagram, volume);
238 if (field_mask & SND_ATTENUATION)
239 MSG_WriteByte (&sv.datagram, attenuation*64);
240 if (field_mask & SND_LARGEENTITY)
242 MSG_WriteShort (&sv.datagram, ent);
243 MSG_WriteByte (&sv.datagram, channel);
246 MSG_WriteShort (&sv.datagram, (ent<<3) | channel);
247 if (field_mask & SND_LARGESOUND)
248 MSG_WriteShort (&sv.datagram, sound_num);
250 MSG_WriteByte (&sv.datagram, sound_num);
251 for (i = 0;i < 3;i++)
252 MSG_WriteCoord (&sv.datagram, entity->v->origin[i]+0.5*(entity->v->mins[i]+entity->v->maxs[i]), sv.protocol);
256 ==============================================================================
260 ==============================================================================
267 Sends the first message from the server to a connected client.
268 This will be sent on the initial connection and upon each server load.
271 void SV_SendServerinfo (client_t *client)
276 // edicts get reallocated on level changes, so we need to update it here
277 client->edict = EDICT_NUM((client - svs.clients) + 1);
279 // if client is a botclient coming from a level change, we need to set up
280 // client info that normally requires networking
281 if (!client->netconnection)
284 ED_ClearEdict(client->edict);
286 // copy spawn parms out of the client_t
287 for (i=0 ; i< NUM_SPAWN_PARMS ; i++)
288 (&pr_global_struct->parm1)[i] = host_client->spawn_parms[i];
290 // call the spawn function
291 pr_global_struct->time = sv.time;
292 pr_global_struct->self = EDICT_TO_PROG(client->edict);
293 PR_ExecuteProgram (pr_global_struct->ClientConnect, "QC function ClientConnect is missing");
294 PR_ExecuteProgram (pr_global_struct->PutClientInServer, "QC function PutClientInServer is missing");
295 host_client->spawned = true;
299 // LordHavoc: clear entityframe tracking
300 client->latestframenum = 0;
302 if (client->entitydatabase)
303 EntityFrame_FreeDatabase(client->entitydatabase);
304 if (client->entitydatabase4)
305 EntityFrame4_FreeDatabase(client->entitydatabase4);
306 if (client->entitydatabase5)
307 EntityFrame5_FreeDatabase(client->entitydatabase5);
309 if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3)
310 client->entitydatabase = EntityFrame_AllocDatabase(sv_clients_mempool);
311 if (sv.protocol == PROTOCOL_DARKPLACES4)
312 client->entitydatabase4 = EntityFrame4_AllocDatabase(sv_clients_mempool);
313 if (sv.protocol == PROTOCOL_DARKPLACES5 || sv.protocol == PROTOCOL_DARKPLACES6)
314 client->entitydatabase5 = EntityFrame5_AllocDatabase(sv_clients_mempool);
316 MSG_WriteByte (&client->message, svc_print);
317 snprintf (message, sizeof (message), "\002\nServer: %s build %s (progs %i crc)", gamename, buildstring, pr_crc);
318 MSG_WriteString (&client->message,message);
320 MSG_WriteByte (&client->message, svc_serverinfo);
321 MSG_WriteLong (&client->message, sv.protocol);
322 MSG_WriteByte (&client->message, svs.maxclients);
324 if (!coop.integer && deathmatch.integer)
325 MSG_WriteByte (&client->message, GAME_DEATHMATCH);
327 MSG_WriteByte (&client->message, GAME_COOP);
329 MSG_WriteString (&client->message,PR_GetString(sv.edicts->v->message));
331 for (i = 1;i < MAX_MODELS && sv.model_precache[i][0];i++)
332 MSG_WriteString (&client->message, sv.model_precache[i]);
333 MSG_WriteByte (&client->message, 0);
335 for (i = 1;i < MAX_SOUNDS && sv.sound_precache[i][0];i++)
336 MSG_WriteString (&client->message, sv.sound_precache[i]);
337 MSG_WriteByte (&client->message, 0);
340 MSG_WriteByte (&client->message, svc_cdtrack);
341 MSG_WriteByte (&client->message, sv.edicts->v->sounds);
342 MSG_WriteByte (&client->message, sv.edicts->v->sounds);
345 MSG_WriteByte (&client->message, svc_setview);
346 MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
348 MSG_WriteByte (&client->message, svc_signonnum);
349 MSG_WriteByte (&client->message, 1);
351 client->sendsignon = true;
352 client->spawned = false; // need prespawn, spawn, etc
359 Initializes a client_t for a new net connection. This will only be called
360 once for a player each game, not once for each level change.
363 void SV_ConnectClient (int clientnum, netconn_t *netconnection)
367 float spawn_parms[NUM_SPAWN_PARMS];
369 client = svs.clients + clientnum;
371 // set up the client_t
373 memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
374 memset (client, 0, sizeof(*client));
375 client->active = true;
376 client->netconnection = netconnection;
378 Con_DPrintf("Client %s connected\n", client->netconnection ? client->netconnection->address : "botclient");
380 strcpy(client->name, "unconnected");
381 strcpy(client->old_name, "unconnected");
382 client->spawned = false;
383 client->edict = EDICT_NUM(clientnum+1);
384 client->message.data = client->msgbuf;
385 client->message.maxsize = sizeof(client->msgbuf);
386 client->message.allowoverflow = true; // we can catch it
387 // updated by receiving "rate" command from client
388 client->rate = NET_MINRATE;
389 // no limits for local player
390 if (client->netconnection && LHNETADDRESS_GetAddressType(&client->netconnection->peeraddress) == LHNETADDRESSTYPE_LOOP)
391 client->rate = 1000000000;
392 client->connecttime = realtime;
395 memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
398 // call the progs to get default spawn parms for the new client
399 PR_ExecuteProgram (pr_global_struct->SetNewParms, "QC function SetNewParms is missing");
400 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
401 client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
404 // don't call SendServerinfo for a fresh botclient because its fields have
405 // not been set up by the qc yet
406 if (client->netconnection)
407 SV_SendServerinfo (client);
409 client->spawned = true;
414 ===============================================================================
418 ===============================================================================
427 void SV_ClearDatagram (void)
429 SZ_Clear (&sv.datagram);
433 =============================================================================
435 The PVS must include a small area around the client to allow head bobbing
436 or other small motion on the client side. Otherwise, a bob might cause an
437 entity that should be visible to not show up, especially when the bob
440 =============================================================================
443 int sv_writeentitiestoclient_pvsbytes;
444 qbyte sv_writeentitiestoclient_pvs[MAX_MAP_LEAFS/8];
446 static int numsendentities;
447 static entity_state_t sendentities[MAX_EDICTS];
448 static entity_state_t *sendentitiesindex[MAX_EDICTS];
450 void SV_PrepareEntitiesForSending(void)
456 // send all entities that touch the pvs
458 sendentitiesindex[0] = NULL;
459 for (e = 1, ent = NEXT_EDICT(sv.edicts);e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
461 sendentitiesindex[e] = NULL;
468 VectorCopy(ent->v->origin, cs.origin);
469 VectorCopy(ent->v->angles, cs.angles);
471 cs.effects = (int)ent->v->effects;
472 cs.colormap = (qbyte)ent->v->colormap;
473 cs.skin = (qbyte)ent->v->skin;
474 cs.frame = (qbyte)ent->v->frame;
475 cs.viewmodelforclient = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)->edict;
476 cs.exteriormodelforclient = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)->edict;
477 cs.nodrawtoclient = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)->edict;
478 cs.drawonlytoclient = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)->edict;
479 cs.tagentity = GETEDICTFIELDVALUE(ent, eval_tag_entity)->edict;
480 cs.tagindex = (qbyte)GETEDICTFIELDVALUE(ent, eval_tag_index)->_float;
481 i = (int)(GETEDICTFIELDVALUE(ent, eval_glow_size)->_float * 0.25f);
482 cs.glowsize = (qbyte)bound(0, i, 255);
483 if (GETEDICTFIELDVALUE(ent, eval_glow_trail)->_float)
484 cs.flags |= RENDER_GLOWTRAIL;
487 i = (int)ent->v->modelindex;
488 if (i >= 1 && i < MAX_MODELS && *PR_GetString(ent->v->model))
492 f = (GETEDICTFIELDVALUE(ent, eval_alpha)->_float * 255.0f);
496 cs.alpha = (qbyte)bound(0, i, 255);
499 f = (GETEDICTFIELDVALUE(ent, eval_renderamt)->_float);
503 cs.alpha = (qbyte)bound(0, i, 255);
507 f = (GETEDICTFIELDVALUE(ent, eval_scale)->_float * 16.0f);
511 cs.scale = (qbyte)bound(0, i, 255);
515 f = (GETEDICTFIELDVALUE(ent, eval_glow_color)->_float);
517 cs.glowcolor = (int)f;
519 if (GETEDICTFIELDVALUE(ent, eval_fullbright)->_float)
520 cs.effects |= EF_FULLBRIGHT;
522 if (ent->v->movetype == MOVETYPE_STEP)
523 cs.flags |= RENDER_STEP;
524 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)
525 cs.flags |= RENDER_LOWPRECISION;
526 if (ent->v->colormap >= 1024)
527 cs.flags |= RENDER_COLORMAPPED;
528 if (cs.viewmodelforclient)
529 cs.flags |= RENDER_VIEWMODEL; // show relative to the view
531 f = GETEDICTFIELDVALUE(ent, eval_color)->vector[0]*256;
532 cs.light[0] = (unsigned short)bound(0, f, 65535);
533 f = GETEDICTFIELDVALUE(ent, eval_color)->vector[1]*256;
534 cs.light[1] = (unsigned short)bound(0, f, 65535);
535 f = GETEDICTFIELDVALUE(ent, eval_color)->vector[2]*256;
536 cs.light[2] = (unsigned short)bound(0, f, 65535);
537 f = GETEDICTFIELDVALUE(ent, eval_light_lev)->_float;
538 cs.light[3] = (unsigned short)bound(0, f, 65535);
539 cs.lightstyle = (qbyte)GETEDICTFIELDVALUE(ent, eval_style)->_float;
540 cs.lightpflags = (qbyte)GETEDICTFIELDVALUE(ent, eval_pflags)->_float;
542 if (gamemode == GAME_TENEBRAE)
544 // tenebrae's EF_FULLDYNAMIC conflicts with Q2's EF_NODRAW
548 cs.lightpflags |= PFLAGS_FULLDYNAMIC;
550 // tenebrae's EF_GREEN conflicts with DP's EF_ADDITIVE
558 cs.lightpflags |= PFLAGS_FULLDYNAMIC;
562 cs.specialvisibilityradius = 0;
563 if (cs.lightpflags & PFLAGS_FULLDYNAMIC)
564 cs.specialvisibilityradius = max(cs.specialvisibilityradius, cs.light[3]);
566 cs.specialvisibilityradius = max(cs.specialvisibilityradius, cs.glowsize * 4);
567 if (cs.flags & RENDER_GLOWTRAIL)
568 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 100);
569 if (cs.effects & (EF_BRIGHTFIELD | EF_MUZZLEFLASH | EF_BRIGHTLIGHT | EF_DIMLIGHT | EF_RED | EF_BLUE | EF_FLAME | EF_STARDUST))
571 if (cs.effects & EF_BRIGHTFIELD)
572 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 80);
573 if (cs.effects & EF_MUZZLEFLASH)
574 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 100);
575 if (cs.effects & EF_BRIGHTLIGHT)
576 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 400);
577 if (cs.effects & EF_DIMLIGHT)
578 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 200);
579 if (cs.effects & EF_RED)
580 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 200);
581 if (cs.effects & EF_BLUE)
582 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 200);
583 if (cs.effects & EF_FLAME)
584 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 250);
585 if (cs.effects & EF_STARDUST)
586 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 100);
589 if (numsendentities >= MAX_EDICTS)
591 // we can omit invisible entities with no effects that are not clients
592 // LordHavoc: this could kill tags attached to an invisible entity, I
593 // just hope we never have to support that case
594 if (cs.number > svs.maxclients && ((cs.effects & EF_NODRAW) || (!cs.modelindex && !cs.specialvisibilityradius)))
596 sendentitiesindex[e] = sendentities + numsendentities;
597 sendentities[numsendentities++] = cs;
601 static int sententitiesmark = 0;
602 static int sententities[MAX_EDICTS];
603 static int sententitiesconsideration[MAX_EDICTS];
604 static int sv_writeentitiestoclient_culled_pvs;
605 static int sv_writeentitiestoclient_culled_trace;
606 static int sv_writeentitiestoclient_visibleentities;
607 static int sv_writeentitiestoclient_totalentities;
608 //static entity_frame_t sv_writeentitiestoclient_entityframe;
609 static int sv_writeentitiestoclient_clentnum;
610 static vec3_t sv_writeentitiestoclient_testeye;
611 static client_t *sv_writeentitiestoclient_client;
613 void SV_MarkWriteEntityStateToClient(entity_state_t *s)
616 vec3_t entmins, entmaxs, lightmins, lightmaxs, testorigin;
619 if (sententitiesconsideration[s->number] == sententitiesmark)
621 sententitiesconsideration[s->number] = sententitiesmark;
622 // viewmodels don't have visibility checking
623 if (s->viewmodelforclient)
625 if (s->viewmodelforclient != sv_writeentitiestoclient_clentnum)
628 // never reject player
629 else if (s->number != sv_writeentitiestoclient_clentnum)
631 // check various rejection conditions
632 if (s->nodrawtoclient == sv_writeentitiestoclient_clentnum)
634 if (s->drawonlytoclient && s->drawonlytoclient != sv_writeentitiestoclient_clentnum)
636 if (s->effects & EF_NODRAW)
638 // LordHavoc: only send entities with a model or important effects
639 if (!s->modelindex && s->specialvisibilityradius == 0)
643 // tag attached entities simply check their parent
644 if (!sendentitiesindex[s->tagentity])
646 SV_MarkWriteEntityStateToClient(sendentitiesindex[s->tagentity]);
647 if (sententities[s->tagentity] != sententitiesmark)
650 // skip invalid modelindexes to avoid crashes
651 else if (s->modelindex >= MAX_MODELS)
653 // always send world submodels, they don't generate much traffic
654 // except in PROTOCOL_QUAKE where they hog bandwidth like crazy
655 else if (!(s->effects & EF_NODEPTHTEST) && (!(isbmodel = (model = sv.models[s->modelindex]) != NULL && model->name[0] == '*') || sv.protocol == PROTOCOL_QUAKE))
657 Mod_CheckLoaded(model);
658 // entity has survived every check so far, check if visible
659 // enlarged box to account for prediction (not that there is
660 // any currently, but still helps the 'run into a room and
661 // watch items pop up' problem)
662 entmins[0] = s->origin[0] - 32.0f;
663 entmins[1] = s->origin[1] - 32.0f;
664 entmins[2] = s->origin[2] - 32.0f;
665 entmaxs[0] = s->origin[0] + 32.0f;
666 entmaxs[1] = s->origin[1] + 32.0f;
667 entmaxs[2] = s->origin[2] + 32.0f;
668 // using the model's bounding box to ensure things are visible regardless of their physics box
671 if (s->angles[0] || s->angles[2]) // pitch and roll
673 VectorAdd(entmins, model->rotatedmins, entmins);
674 VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
676 else if (s->angles[1])
678 VectorAdd(entmins, model->yawmins, entmins);
679 VectorAdd(entmaxs, model->yawmaxs, entmaxs);
683 VectorAdd(entmins, model->normalmins, entmins);
684 VectorAdd(entmaxs, model->normalmaxs, entmaxs);
687 lightmins[0] = min(entmins[0], s->origin[0] - s->specialvisibilityradius);
688 lightmins[1] = min(entmins[1], s->origin[1] - s->specialvisibilityradius);
689 lightmins[2] = min(entmins[2], s->origin[2] - s->specialvisibilityradius);
690 lightmaxs[0] = max(entmaxs[0], s->origin[0] + s->specialvisibilityradius);
691 lightmaxs[1] = max(entmaxs[1], s->origin[1] + s->specialvisibilityradius);
692 lightmaxs[2] = max(entmaxs[2], s->origin[2] + s->specialvisibilityradius);
693 sv_writeentitiestoclient_totalentities++;
694 // if not touching a visible leaf
695 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))
697 sv_writeentitiestoclient_culled_pvs++;
700 // or not seen by random tracelines
701 if (sv_cullentities_trace.integer && !isbmodel)
703 // LordHavoc: test center first
704 testorigin[0] = (entmins[0] + entmaxs[0]) * 0.5f;
705 testorigin[1] = (entmins[1] + entmaxs[1]) * 0.5f;
706 testorigin[2] = (entmins[2] + entmaxs[2]) * 0.5f;
707 sv.worldmodel->TraceBox(sv.worldmodel, 0, &trace, sv_writeentitiestoclient_testeye, sv_writeentitiestoclient_testeye, testorigin, testorigin, SUPERCONTENTS_SOLID);
708 if (trace.fraction == 1 || BoxesOverlap(trace.endpos, trace.endpos, entmins, entmaxs))
709 sv_writeentitiestoclient_client->visibletime[s->number] = realtime + 1;
712 // LordHavoc: test random offsets, to maximize chance of detection
713 testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
714 testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
715 testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
716 sv.worldmodel->TraceBox(sv.worldmodel, 0, &trace, sv_writeentitiestoclient_testeye, sv_writeentitiestoclient_testeye, testorigin, testorigin, SUPERCONTENTS_SOLID);
717 if (trace.fraction == 1 || BoxesOverlap(trace.endpos, trace.endpos, entmins, entmaxs))
718 sv_writeentitiestoclient_client->visibletime[s->number] = realtime + 1;
721 if (s->specialvisibilityradius)
723 // LordHavoc: test random offsets, to maximize chance of detection
724 testorigin[0] = lhrandom(lightmins[0], lightmaxs[0]);
725 testorigin[1] = lhrandom(lightmins[1], lightmaxs[1]);
726 testorigin[2] = lhrandom(lightmins[2], lightmaxs[2]);
727 sv.worldmodel->TraceBox(sv.worldmodel, 0, &trace, sv_writeentitiestoclient_testeye, sv_writeentitiestoclient_testeye, testorigin, testorigin, SUPERCONTENTS_SOLID);
728 if (trace.fraction == 1 || BoxesOverlap(trace.endpos, trace.endpos, entmins, entmaxs))
729 sv_writeentitiestoclient_client->visibletime[s->number] = realtime + 1;
733 if (realtime > sv_writeentitiestoclient_client->visibletime[s->number])
735 sv_writeentitiestoclient_culled_trace++;
739 sv_writeentitiestoclient_visibleentities++;
742 // this just marks it for sending
743 // FIXME: it would be more efficient to send here, but the entity
744 // compressor isn't that flexible
745 sententities[s->number] = sententitiesmark;
748 entity_state_t sendstates[MAX_EDICTS];
750 void SV_WriteEntitiesToClient(client_t *client, edict_t *clent, sizebuf_t *msg, int *stats)
752 int i, numsendstates;
755 // if there isn't enough space to accomplish anything, skip it
756 if (msg->cursize + 25 > msg->maxsize)
759 sv_writeentitiestoclient_client = client;
761 sv_writeentitiestoclient_culled_pvs = 0;
762 sv_writeentitiestoclient_culled_trace = 0;
763 sv_writeentitiestoclient_visibleentities = 0;
764 sv_writeentitiestoclient_totalentities = 0;
766 Mod_CheckLoaded(sv.worldmodel);
768 // find the client's PVS
769 // the real place being tested from
770 VectorAdd(clent->v->origin, clent->v->view_ofs, sv_writeentitiestoclient_testeye);
771 sv_writeentitiestoclient_pvsbytes = 0;
772 if (sv.worldmodel && sv.worldmodel->brush.FatPVS)
773 sv_writeentitiestoclient_pvsbytes = sv.worldmodel->brush.FatPVS(sv.worldmodel, sv_writeentitiestoclient_testeye, 8, sv_writeentitiestoclient_pvs, sizeof(sv_writeentitiestoclient_pvs));
775 sv_writeentitiestoclient_clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
779 for (i = 0;i < numsendentities;i++)
780 SV_MarkWriteEntityStateToClient(sendentities + i);
783 for (i = 0;i < numsendentities;i++)
785 if (sententities[sendentities[i].number] == sententitiesmark)
787 s = &sendstates[numsendstates++];
788 *s = sendentities[i];
789 if (s->exteriormodelforclient && s->exteriormodelforclient == sv_writeentitiestoclient_clentnum)
790 s->flags |= RENDER_EXTERIORMODEL;
794 if (sv_cullentities_stats.integer)
795 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);
797 if (client->entitydatabase5)
798 EntityFrame5_WriteFrame(msg, client->entitydatabase5, numsendstates, sendstates, client - svs.clients + 1, stats);
799 else if (client->entitydatabase4)
800 EntityFrame4_WriteFrame(msg, client->entitydatabase4, numsendstates, sendstates);
801 else if (client->entitydatabase)
802 EntityFrame_WriteFrame(msg, client->entitydatabase, numsendstates, sendstates, client - svs.clients + 1);
804 EntityFrameQuake_WriteFrame(msg, numsendstates, sendstates);
813 void SV_CleanupEnts (void)
818 ent = NEXT_EDICT(sv.edicts);
819 for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
820 ent->v->effects = (int)ent->v->effects & ~EF_MUZZLEFLASH;
825 SV_WriteClientdataToMessage
829 void SV_WriteClientdataToMessage (client_t *client, edict_t *ent, sizebuf_t *msg, int *stats)
838 int weaponmodelindex;
841 // send a damage message
843 if (ent->v->dmg_take || ent->v->dmg_save)
845 other = PROG_TO_EDICT(ent->v->dmg_inflictor);
846 MSG_WriteByte (msg, svc_damage);
847 MSG_WriteByte (msg, ent->v->dmg_save);
848 MSG_WriteByte (msg, ent->v->dmg_take);
849 for (i=0 ; i<3 ; i++)
850 MSG_WriteCoord (msg, other->v->origin[i] + 0.5*(other->v->mins[i] + other->v->maxs[i]), sv.protocol);
852 ent->v->dmg_take = 0;
853 ent->v->dmg_save = 0;
857 // send the current viewpos offset from the view entity
859 SV_SetIdealPitch (); // how much to look up / down ideally
861 // a fixangle might get lost in a dropped packet. Oh well.
862 if ( ent->v->fixangle )
864 MSG_WriteByte (msg, svc_setangle);
865 for (i=0 ; i < 3 ; i++)
866 MSG_WriteAngle (msg, ent->v->angles[i], sv.protocol);
867 ent->v->fixangle = 0;
870 // stuff the sigil bits into the high bits of items for sbar, or else
872 val = GETEDICTFIELDVALUE(ent, eval_items2);
874 items = (int)ent->v->items | ((int)val->_float << 23);
876 items = (int)ent->v->items | ((int)pr_global_struct->serverflags << 28);
878 VectorClear(punchvector);
879 if ((val = GETEDICTFIELDVALUE(ent, eval_punchvector)))
880 VectorCopy(val->vector, punchvector);
882 weaponmodelindex = SV_ModelIndex(PR_GetString(ent->v->weaponmodel), 1);
885 if ((val = GETEDICTFIELDVALUE(ent, eval_viewzoom)))
886 viewzoom = val->_float * 255.0f;
892 if ((int)ent->v->flags & FL_ONGROUND)
894 if (ent->v->waterlevel >= 2)
896 if (ent->v->idealpitch)
897 bits |= SU_IDEALPITCH;
899 for (i=0 ; i<3 ; i++)
901 if (ent->v->punchangle[i])
902 bits |= (SU_PUNCH1<<i);
903 if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5 || sv.protocol == PROTOCOL_DARKPLACES6)
905 bits |= (SU_PUNCHVEC1<<i);
906 if (ent->v->velocity[i])
907 bits |= (SU_VELOCITY1<<i);
910 memset(stats, 0, sizeof(int[MAX_CL_STATS]));
911 stats[STAT_VIEWHEIGHT] = ent->v->view_ofs[2];
912 stats[STAT_ITEMS] = items;
913 stats[STAT_WEAPONFRAME] = ent->v->weaponframe;
914 stats[STAT_ARMOR] = ent->v->armorvalue;
915 stats[STAT_WEAPON] = weaponmodelindex;
916 stats[STAT_HEALTH] = ent->v->health;
917 stats[STAT_AMMO] = ent->v->currentammo;
918 stats[STAT_SHELLS] = ent->v->ammo_shells;
919 stats[STAT_NAILS] = ent->v->ammo_nails;
920 stats[STAT_ROCKETS] = ent->v->ammo_rockets;
921 stats[STAT_CELLS] = ent->v->ammo_cells;
922 stats[STAT_ACTIVEWEAPON] = ent->v->weapon;
923 stats[STAT_VIEWZOOM] = viewzoom;
924 // the QC bumps these itself by sending svc_'s, so we have to keep them
925 // zero or they'll be corrected by the engine
926 //stats[STAT_TOTALSECRETS] = pr_global_struct->total_secrets;
927 //stats[STAT_TOTALMONSTERS] = pr_global_struct->total_monsters;
928 //stats[STAT_SECRETS] = pr_global_struct->found_secrets;
929 //stats[STAT_MONSTERS] = pr_global_struct->killed_monsters;
931 if (sv.protocol != PROTOCOL_DARKPLACES6)
933 if (stats[STAT_VIEWHEIGHT] != DEFAULT_VIEWHEIGHT) bits |= SU_VIEWHEIGHT;
935 if (stats[STAT_WEAPONFRAME]) bits |= SU_WEAPONFRAME;
936 if (stats[STAT_ARMOR]) bits |= SU_ARMOR;
938 // FIXME: which protocols support this? does PROTOCOL_DARKPLACES3 support viewzoom?
939 if (sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5)
946 if (bits >= 16777216)
950 MSG_WriteByte (msg, svc_clientdata);
951 MSG_WriteShort (msg, bits);
952 if (bits & SU_EXTEND1)
953 MSG_WriteByte(msg, bits >> 16);
954 if (bits & SU_EXTEND2)
955 MSG_WriteByte(msg, bits >> 24);
957 if (bits & SU_VIEWHEIGHT)
958 MSG_WriteChar (msg, stats[STAT_VIEWHEIGHT]);
960 if (bits & SU_IDEALPITCH)
961 MSG_WriteChar (msg, ent->v->idealpitch);
963 for (i=0 ; i<3 ; i++)
965 if (bits & (SU_PUNCH1<<i))
967 if (sv.protocol == PROTOCOL_QUAKE)
968 MSG_WriteChar(msg, ent->v->punchangle[i]);
969 else if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5 || sv.protocol == PROTOCOL_DARKPLACES6)
970 MSG_WriteAngle16i(msg, ent->v->punchangle[i]);
972 if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5 || sv.protocol == PROTOCOL_DARKPLACES6)
974 if (bits & (SU_PUNCHVEC1<<i))
976 if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4)
977 MSG_WriteCoord16i(msg, punchvector[i]);
978 else if (sv.protocol == PROTOCOL_DARKPLACES5 || sv.protocol == PROTOCOL_DARKPLACES6)
979 MSG_WriteCoord32f(msg, punchvector[i]);
982 if (bits & (SU_VELOCITY1<<i))
984 if (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4)
985 MSG_WriteChar(msg, ent->v->velocity[i] * (1.0f / 16.0f));
986 else if (sv.protocol == PROTOCOL_DARKPLACES5 || sv.protocol == PROTOCOL_DARKPLACES6)
987 MSG_WriteCoord32f(msg, ent->v->velocity[i]);
992 MSG_WriteLong (msg, stats[STAT_ITEMS]);
994 if (sv.protocol == PROTOCOL_DARKPLACES5)
996 if (bits & SU_WEAPONFRAME)
997 MSG_WriteShort (msg, stats[STAT_WEAPONFRAME]);
999 MSG_WriteShort (msg, stats[STAT_ARMOR]);
1000 if (bits & SU_WEAPON)
1001 MSG_WriteShort (msg, stats[STAT_WEAPON]);
1002 MSG_WriteShort (msg, stats[STAT_HEALTH]);
1003 MSG_WriteShort (msg, stats[STAT_AMMO]);
1004 MSG_WriteShort (msg, stats[STAT_SHELLS]);
1005 MSG_WriteShort (msg, stats[STAT_NAILS]);
1006 MSG_WriteShort (msg, stats[STAT_ROCKETS]);
1007 MSG_WriteShort (msg, stats[STAT_CELLS]);
1008 MSG_WriteShort (msg, stats[STAT_ACTIVEWEAPON]);
1009 if (bits & SU_VIEWZOOM)
1010 MSG_WriteShort (msg, min(stats[STAT_VIEWZOOM], 65535));
1012 else if (sv.protocol != PROTOCOL_DARKPLACES6)
1014 if (bits & SU_WEAPONFRAME)
1015 MSG_WriteByte (msg, stats[STAT_WEAPONFRAME]);
1016 if (bits & SU_ARMOR)
1017 MSG_WriteByte (msg, stats[STAT_ARMOR]);
1018 if (bits & SU_WEAPON)
1019 MSG_WriteByte (msg, stats[STAT_WEAPON]);
1020 MSG_WriteShort (msg, stats[STAT_HEALTH]);
1021 MSG_WriteByte (msg, stats[STAT_AMMO]);
1022 MSG_WriteByte (msg, stats[STAT_SHELLS]);
1023 MSG_WriteByte (msg, stats[STAT_NAILS]);
1024 MSG_WriteByte (msg, stats[STAT_ROCKETS]);
1025 MSG_WriteByte (msg, stats[STAT_CELLS]);
1026 if (gamemode == GAME_HIPNOTIC || gamemode == GAME_ROGUE || gamemode == GAME_NEXUIZ)
1028 for (i = 0;i < 32;i++)
1029 if (stats[STAT_WEAPON] & (1<<i))
1031 MSG_WriteByte (msg, i);
1034 MSG_WriteByte (msg, stats[STAT_WEAPON]);
1035 if (bits & SU_VIEWZOOM)
1037 if (sv.protocol == PROTOCOL_DARKPLACES4)
1038 MSG_WriteByte (msg, min(stats[STAT_VIEWZOOM], 255));
1039 else if (sv.protocol == PROTOCOL_DARKPLACES5 || sv.protocol == PROTOCOL_DARKPLACES6)
1040 MSG_WriteShort (msg, min(stats[STAT_VIEWZOOM], 65535));
1046 =======================
1047 SV_SendClientDatagram
1048 =======================
1050 static qbyte sv_sendclientdatagram_buf[NET_MAXMESSAGE]; // FIXME?
1051 qboolean SV_SendClientDatagram (client_t *client)
1053 int rate, maxrate, maxsize, maxsize2;
1055 int stats[MAX_CL_STATS];
1057 if (LHNETADDRESS_GetAddressType(&host_client->netconnection->peeraddress) == LHNETADDRESSTYPE_LOOP && !sv_ratelimitlocalplayer.integer)
1059 // for good singleplayer, send huge packets
1060 maxsize = sizeof(sv_sendclientdatagram_buf);
1061 maxsize2 = sizeof(sv_sendclientdatagram_buf);
1063 else if (sv.protocol == PROTOCOL_DARKPLACES5 || sv.protocol == PROTOCOL_DARKPLACES6)
1065 // PROTOCOL_DARKPLACES5 supports packet size limiting of updates
1066 maxrate = bound(NET_MINRATE, sv_maxrate.integer, NET_MAXRATE);
1067 if (sv_maxrate.integer != maxrate)
1068 Cvar_SetValueQuick(&sv_maxrate, maxrate);
1070 rate = bound(NET_MINRATE, client->rate, maxrate);
1071 rate = (int)(client->rate * sys_ticrate.value);
1072 maxsize = bound(100, rate, 1400);
1077 // no rate limiting support on older protocols because dp protocols
1078 // 1-4 kick the client off if they overflow, and quake protocol shows
1079 // less than the full entity set if rate limited
1084 msg.data = sv_sendclientdatagram_buf;
1085 msg.maxsize = maxsize;
1088 MSG_WriteByte (&msg, svc_time);
1089 MSG_WriteFloat (&msg, sv.time);
1091 // add the client specific data to the datagram
1092 SV_WriteClientdataToMessage (client, client->edict, &msg, stats);
1093 SV_WriteEntitiesToClient (client, client->edict, &msg, stats);
1095 // expand packet size to allow effects to go over the rate limit
1096 // (dropping them is FAR too ugly)
1097 msg.maxsize = maxsize2;
1099 // copy the server datagram if there is space
1100 // FIXME: put in delayed queue of effects to send
1101 if (sv.datagram.cursize > 0 && msg.cursize + sv.datagram.cursize <= msg.maxsize)
1102 SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
1104 // send the datagram
1105 if (NetConn_SendUnreliableMessage (client->netconnection, &msg) == -1)
1107 SV_DropClient (true);// if the message couldn't send, kick off
1115 =======================
1116 SV_UpdateToReliableMessages
1117 =======================
1119 void SV_UpdateToReliableMessages (void)
1128 // check for changes to be sent over the reliable streams
1129 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1131 // update the host_client fields we care about according to the entity fields
1132 host_client->edict = EDICT_NUM(i+1);
1135 name = PR_GetString(host_client->edict->v->netname);
1138 // always point the string back at host_client->name to keep it safe
1139 strlcpy (host_client->name, name, sizeof (host_client->name));
1140 host_client->edict->v->netname = PR_SetString(host_client->name);
1141 if (strcmp(host_client->old_name, host_client->name))
1143 if (host_client->spawned)
1144 SV_BroadcastPrintf("%s changed name to %s\n", host_client->old_name, host_client->name);
1145 strcpy(host_client->old_name, host_client->name);
1146 // send notification to all clients
1147 MSG_WriteByte (&sv.reliable_datagram, svc_updatename);
1148 MSG_WriteByte (&sv.reliable_datagram, i);
1149 MSG_WriteString (&sv.reliable_datagram, host_client->name);
1152 // DP_SV_CLIENTCOLORS
1153 // this is always found (since it's added by the progs loader)
1154 if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_clientcolors)))
1155 host_client->colors = (int)val->_float;
1156 if (host_client->old_colors != host_client->colors)
1158 host_client->old_colors = host_client->colors;
1159 // send notification to all clients
1160 MSG_WriteByte (&sv.reliable_datagram, svc_updatecolors);
1161 MSG_WriteByte (&sv.reliable_datagram, i);
1162 MSG_WriteByte (&sv.reliable_datagram, host_client->colors);
1165 // NEXUIZ_PLAYERMODEL
1166 if( eval_playermodel ) {
1167 model = PR_GetString(GETEDICTFIELDVALUE(host_client->edict, eval_playermodel)->string);
1170 // always point the string back at host_client->name to keep it safe
1171 strlcpy (host_client->playermodel, model, sizeof (host_client->playermodel));
1172 GETEDICTFIELDVALUE(host_client->edict, eval_playermodel)->string = PR_SetString(host_client->playermodel);
1175 // NEXUIZ_PLAYERSKIN
1176 if( eval_playerskin ) {
1177 skin = PR_GetString(GETEDICTFIELDVALUE(host_client->edict, eval_playerskin)->string);
1180 // always point the string back at host_client->name to keep it safe
1181 strlcpy (host_client->playerskin, skin, sizeof (host_client->playerskin));
1182 GETEDICTFIELDVALUE(host_client->edict, eval_playerskin)->string = PR_SetString(host_client->playerskin);
1186 host_client->frags = (int)host_client->edict->v->frags;
1187 if (host_client->old_frags != host_client->frags)
1189 host_client->old_frags = host_client->frags;
1190 // send notification to all clients
1191 MSG_WriteByte (&sv.reliable_datagram, svc_updatefrags);
1192 MSG_WriteByte (&sv.reliable_datagram, i);
1193 MSG_WriteShort (&sv.reliable_datagram, host_client->frags);
1197 for (j = 0, client = svs.clients;j < svs.maxclients;j++, client++)
1198 if (client->netconnection)
1199 SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
1201 SZ_Clear (&sv.reliable_datagram);
1206 =======================
1209 Send a nop message without trashing or sending the accumulated client
1211 =======================
1213 void SV_SendNop (client_t *client)
1219 msg.maxsize = sizeof(buf);
1222 MSG_WriteChar (&msg, svc_nop);
1224 if (NetConn_SendUnreliableMessage (client->netconnection, &msg) == -1)
1225 SV_DropClient (true); // if the message couldn't send, kick off
1226 client->last_message = realtime;
1230 =======================
1231 SV_SendClientMessages
1232 =======================
1234 void SV_SendClientMessages (void)
1236 int i, prepared = false;
1238 // update frags, names, etc
1239 SV_UpdateToReliableMessages();
1241 // build individual updates
1242 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1244 if (!host_client->active)
1246 if (!host_client->netconnection)
1248 SZ_Clear(&host_client->message);
1252 if (host_client->deadsocket || host_client->message.overflowed)
1254 SV_DropClient (true); // if the message couldn't send, kick off
1258 if (host_client->spawned)
1263 // only prepare entities once per frame
1264 SV_PrepareEntitiesForSending();
1266 if (!SV_SendClientDatagram (host_client))
1271 // the player isn't totally in the game yet
1272 // send small keepalive messages if too much time has passed
1273 // send a full message when the next signon stage has been requested
1274 // some other message data (name changes, etc) may accumulate
1275 // between signon stages
1276 if (!host_client->sendsignon)
1278 if (realtime - host_client->last_message > 5)
1279 SV_SendNop (host_client);
1280 continue; // don't send out non-signon messages
1284 if (host_client->message.cursize || host_client->dropasap)
1286 if (!NetConn_CanSendMessage (host_client->netconnection))
1289 if (host_client->dropasap)
1290 SV_DropClient (false); // went to another level
1293 if (NetConn_SendReliableMessage (host_client->netconnection, &host_client->message) == -1)
1294 SV_DropClient (true); // if the message couldn't send, kick off
1295 SZ_Clear (&host_client->message);
1296 host_client->last_message = realtime;
1297 host_client->sendsignon = false;
1302 // clear muzzle flashes
1308 ==============================================================================
1312 ==============================================================================
1321 int SV_ModelIndex(char *s, int precachemode)
1323 int i, limit = (sv.protocol == PROTOCOL_QUAKE ? 256 : MAX_MODELS);
1324 char filename[MAX_QPATH];
1328 //if (precachemode == 2)
1330 strlcpy(filename, s, sizeof(filename));
1331 for (i = 2;i < limit;i++)
1333 if (!sv.model_precache[i][0])
1337 if (sv.state != ss_loading && (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5))
1339 Con_Printf("SV_ModelIndex(\"%s\"): precache_model can only be done in spawn functions\n", filename);
1342 if (precachemode == 1)
1343 Con_Printf("SV_ModelIndex(\"%s\"): not precached (fix your code), precaching anyway\n", filename);
1344 strlcpy(sv.model_precache[i], filename, sizeof(sv.model_precache[i]));
1345 sv.models[i] = Mod_ForName (sv.model_precache[i], true, false, false);
1346 if (sv.protocol == PROTOCOL_DARKPLACES6)
1348 MSG_WriteByte(&sv.reliable_datagram, svc_precache);
1349 MSG_WriteShort(&sv.reliable_datagram, i);
1350 MSG_WriteString(&sv.reliable_datagram, filename);
1354 Con_Printf("SV_ModelIndex(\"%s\"): not precached\n", filename);
1357 if (!strcmp(sv.model_precache[i], filename))
1360 Con_Printf("SV_ModelIndex(\"%s\"): i (%i) == MAX_MODELS (%i)\n", filename, i, MAX_MODELS);
1370 int SV_SoundIndex(char *s, int precachemode)
1372 int i, limit = (sv.protocol == PROTOCOL_QUAKE ? 256 : MAX_SOUNDS);
1373 char filename[MAX_QPATH];
1377 //if (precachemode == 2)
1379 strlcpy(filename, s, sizeof(filename));
1380 for (i = 1;i < limit;i++)
1382 if (!sv.sound_precache[i][0])
1386 if (sv.state != ss_loading && (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5))
1388 Con_Printf("SV_SoundIndex(\"%s\"): precache_sound can only be done in spawn functions\n", filename);
1391 if (precachemode == 1)
1392 Con_Printf("SV_SoundIndex(\"%s\"): not precached (fix your code), precaching anyway\n", filename);
1393 strlcpy(sv.sound_precache[i], filename, sizeof(sv.sound_precache[i]));
1394 if (sv.protocol == PROTOCOL_DARKPLACES6)
1396 MSG_WriteByte(&sv.reliable_datagram, svc_precache);
1397 MSG_WriteShort(&sv.reliable_datagram, i + 32768);
1398 MSG_WriteString(&sv.reliable_datagram, filename);
1402 Con_Printf("SV_SoundIndex(\"%s\"): not precached\n", filename);
1405 if (!strcmp(sv.sound_precache[i], filename))
1408 Con_Printf("SV_SoundIndex(\"%s\"): i (%i) == MAX_SOUNDS (%i)\n", filename, i, MAX_SOUNDS);
1418 void SV_CreateBaseline (void)
1420 int i, entnum, large;
1423 // LordHavoc: clear *all* states (note just active ones)
1424 for (entnum = 0;entnum < sv.max_edicts;entnum++)
1426 // get the current server version
1427 svent = EDICT_NUM(entnum);
1429 // LordHavoc: always clear state values, whether the entity is in use or not
1430 svent->e->baseline = defaultstate;
1434 if (entnum > svs.maxclients && !svent->v->modelindex)
1437 // create entity baseline
1438 VectorCopy (svent->v->origin, svent->e->baseline.origin);
1439 VectorCopy (svent->v->angles, svent->e->baseline.angles);
1440 svent->e->baseline.frame = svent->v->frame;
1441 svent->e->baseline.skin = svent->v->skin;
1442 if (entnum > 0 && entnum <= svs.maxclients)
1444 svent->e->baseline.colormap = entnum;
1445 svent->e->baseline.modelindex = SV_ModelIndex("progs/player.mdl", 1);
1449 svent->e->baseline.colormap = 0;
1450 svent->e->baseline.modelindex = svent->v->modelindex;
1454 if (svent->e->baseline.modelindex & 0xFF00 || svent->e->baseline.frame & 0xFF00)
1457 // add to the message
1459 MSG_WriteByte (&sv.signon, svc_spawnbaseline2);
1461 MSG_WriteByte (&sv.signon, svc_spawnbaseline);
1462 MSG_WriteShort (&sv.signon, entnum);
1466 MSG_WriteShort (&sv.signon, svent->e->baseline.modelindex);
1467 MSG_WriteShort (&sv.signon, svent->e->baseline.frame);
1471 MSG_WriteByte (&sv.signon, svent->e->baseline.modelindex);
1472 MSG_WriteByte (&sv.signon, svent->e->baseline.frame);
1474 MSG_WriteByte (&sv.signon, svent->e->baseline.colormap);
1475 MSG_WriteByte (&sv.signon, svent->e->baseline.skin);
1476 for (i=0 ; i<3 ; i++)
1478 MSG_WriteCoord(&sv.signon, svent->e->baseline.origin[i], sv.protocol);
1479 MSG_WriteAngle(&sv.signon, svent->e->baseline.angles[i], sv.protocol);
1489 Tell all the clients that the server is changing levels
1492 void SV_SendReconnect (void)
1499 msg.maxsize = sizeof(data);
1501 MSG_WriteChar (&msg, svc_stufftext);
1502 MSG_WriteString (&msg, "reconnect\n");
1503 NetConn_SendToAll (&msg, 5);
1505 if (cls.state != ca_dedicated)
1506 Cmd_ExecuteString ("reconnect\n", src_command);
1514 Grabs the current state of each client for saving across the
1515 transition to another level
1518 void SV_SaveSpawnparms (void)
1522 svs.serverflags = pr_global_struct->serverflags;
1524 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1526 if (!host_client->active)
1529 // call the progs to get default spawn parms for the new client
1530 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1531 PR_ExecuteProgram (pr_global_struct->SetChangeParms, "QC function SetChangeParms is missing");
1532 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1533 host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1537 void SV_IncreaseEdicts(void)
1541 int oldmax_edicts = sv.max_edicts;
1542 void *oldedictsengineprivate = sv.edictsengineprivate;
1543 void *oldedictsfields = sv.edictsfields;
1544 void *oldmoved_edicts = sv.moved_edicts;
1546 if (sv.max_edicts >= MAX_EDICTS)
1549 // links don't survive the transition, so unlink everything
1550 for (i = 0, ent = sv.edicts;i < sv.max_edicts;i++, ent++)
1553 SV_UnlinkEdict(sv.edicts + i);
1554 memset(&ent->e->areagrid, 0, sizeof(ent->e->areagrid));
1558 sv.max_edicts = min(sv.max_edicts + 256, MAX_EDICTS);
1559 sv.edictsengineprivate = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_engineprivate_t));
1560 sv.edictsfields = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1561 sv.moved_edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_t *));
1563 memcpy(sv.edictsengineprivate, oldedictsengineprivate, oldmax_edicts * sizeof(edict_engineprivate_t));
1564 memcpy(sv.edictsfields, oldedictsfields, oldmax_edicts * pr_edict_size);
1566 for (i = 0, ent = sv.edicts;i < sv.max_edicts;i++, ent++)
1568 ent->e = sv.edictsengineprivate + i;
1569 ent->v = (void *)((qbyte *)sv.edictsfields + i * pr_edict_size);
1570 // link every entity except world
1572 SV_LinkEdict(ent, false);
1575 Mem_Free(oldedictsengineprivate);
1576 Mem_Free(oldedictsfields);
1577 Mem_Free(oldmoved_edicts);
1584 This is called at the start of each level
1587 extern float scr_centertime_off;
1589 void SV_SpawnServer (const char *server)
1594 model_t *worldmodel;
1595 char modelname[sizeof(sv.modelname)];
1597 Con_DPrintf("SpawnServer: %s\n", server);
1599 snprintf (modelname, sizeof(modelname), "maps/%s.bsp", server);
1600 worldmodel = Mod_ForName(modelname, false, true, true);
1601 if (!worldmodel || !worldmodel->TraceBox)
1603 Con_Printf("Couldn't load map %s\n", modelname);
1607 // let's not have any servers with no name
1608 if (hostname.string[0] == 0)
1609 Cvar_Set ("hostname", "UNNAMED");
1610 scr_centertime_off = 0;
1612 svs.changelevel_issued = false; // now safe to issue another
1615 // tell all connected clients that we are going to a new level
1621 // make sure cvars have been checked before opening the ports
1622 NetConn_ServerFrame();
1623 NetConn_OpenServerPorts(true);
1627 // make cvars consistant
1630 Cvar_SetValue ("deathmatch", 0);
1631 current_skill = bound(0, (int)(skill.value + 0.5), 3);
1633 Cvar_SetValue ("skill", (float)current_skill);
1636 // set up the new server
1638 Host_ClearMemory ();
1640 memset (&sv, 0, sizeof(sv));
1642 strlcpy (sv.name, server, sizeof (sv.name));
1644 sv.netquakecompatible = false;
1645 if (!strcasecmp(sv_protocolname.string, "QUAKE"))
1647 sv.protocol = PROTOCOL_QUAKE;
1648 sv.netquakecompatible = true;
1650 else if (!strcasecmp(sv_protocolname.string, "QUAKEDP"))
1651 sv.protocol = PROTOCOL_QUAKE;
1652 else if (!strcasecmp(sv_protocolname.string, "DARKPLACES1"))
1653 sv.protocol = PROTOCOL_DARKPLACES1;
1654 else if (!strcasecmp(sv_protocolname.string, "DARKPLACES2"))
1655 sv.protocol = PROTOCOL_DARKPLACES2;
1656 else if (!strcasecmp(sv_protocolname.string, "DARKPLACES3"))
1657 sv.protocol = PROTOCOL_DARKPLACES3;
1658 else if (!strcasecmp(sv_protocolname.string, "DARKPLACES4"))
1659 sv.protocol = PROTOCOL_DARKPLACES4;
1660 else if (!strcasecmp(sv_protocolname.string, "DARKPLACES5"))
1661 sv.protocol = PROTOCOL_DARKPLACES5;
1662 else if (!strcasecmp(sv_protocolname.string, "DARKPLACES6"))
1663 sv.protocol = PROTOCOL_DARKPLACES6;
1666 sv.protocol = PROTOCOL_DARKPLACES6;
1667 Con_Printf("Unknown sv_protocolname \"%s\", valid values are QUAKE, QUAKEDP, DARKPLACES1, DARKPLACES2, DARKPLACES3, DARKPLACES4, DARKPLACES5, DARKPLACES6, falling back to DARKPLACES6 protocol\n", sv_protocolname.string);
1670 // load progs to get entity field count
1671 PR_LoadProgs ( sv_progs.string );
1673 // allocate server memory
1674 // start out with just enough room for clients and a reasonable estimate of entities
1675 sv.max_edicts = max(svs.maxclients + 1, 512);
1676 sv.max_edicts = min(sv.max_edicts, MAX_EDICTS);
1678 // clear the edict memory pool
1679 Mem_EmptyPool(sv_edicts_mempool);
1680 // edict_t structures (hidden from progs)
1681 sv.edicts = Mem_Alloc(sv_edicts_mempool, MAX_EDICTS * sizeof(edict_t));
1682 // engine private structures (hidden from progs)
1683 sv.edictsengineprivate = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_engineprivate_t));
1684 // progs fields, often accessed by server
1685 sv.edictsfields = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1686 // used by PushMove to move back pushed entities
1687 sv.moved_edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_t *));
1688 for (i = 0;i < sv.max_edicts;i++)
1690 ent = sv.edicts + i;
1691 ent->e = sv.edictsengineprivate + i;
1692 ent->v = (void *)((qbyte *)sv.edictsfields + i * pr_edict_size);
1695 // fix up client->edict pointers for returning clients right away...
1696 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1697 host_client->edict = EDICT_NUM(i + 1);
1699 sv.datagram.maxsize = sizeof(sv.datagram_buf);
1700 sv.datagram.cursize = 0;
1701 sv.datagram.data = sv.datagram_buf;
1703 sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1704 sv.reliable_datagram.cursize = 0;
1705 sv.reliable_datagram.data = sv.reliable_datagram_buf;
1707 sv.signon.maxsize = sizeof(sv.signon_buf);
1708 sv.signon.cursize = 0;
1709 sv.signon.data = sv.signon_buf;
1711 // leave slots at start for clients only
1712 sv.num_edicts = svs.maxclients+1;
1714 sv.state = ss_loading;
1720 worldmodel->used = true;
1722 strlcpy (sv.name, server, sizeof (sv.name));
1723 strcpy(sv.modelname, modelname);
1724 sv.worldmodel = worldmodel;
1725 sv.models[1] = sv.worldmodel;
1728 // clear world interaction links
1732 strlcpy(sv.sound_precache[0], "", sizeof(sv.sound_precache[0]));
1734 strlcpy(sv.model_precache[0], "", sizeof(sv.model_precache[0]));
1735 strlcpy(sv.model_precache[1], sv.modelname, sizeof(sv.model_precache[1]));
1736 for (i = 1;i < sv.worldmodel->brush.numsubmodels;i++)
1738 snprintf(sv.model_precache[i+1], sizeof(sv.model_precache[i+1]), "*%i", i);
1739 sv.models[i+1] = Mod_ForName (sv.model_precache[i+1], false, false, false);
1743 // load the rest of the entities
1746 memset (ent->v, 0, progs->entityfields * 4);
1747 ent->e->free = false;
1748 ent->v->model = PR_SetString(sv.modelname);
1749 ent->v->modelindex = 1; // world model
1750 ent->v->solid = SOLID_BSP;
1751 ent->v->movetype = MOVETYPE_PUSH;
1754 pr_global_struct->coop = coop.integer;
1756 pr_global_struct->deathmatch = deathmatch.integer;
1758 pr_global_struct->mapname = PR_SetString(sv.name);
1760 // serverflags are for cross level information (sigils)
1761 pr_global_struct->serverflags = svs.serverflags;
1763 // load replacement entity file if found
1765 if (sv_entpatch.integer)
1766 entities = FS_LoadFile(va("maps/%s.ent", sv.name), tempmempool, true);
1769 Con_Printf("Loaded maps/%s.ent\n", sv.name);
1770 ED_LoadFromFile (entities);
1774 ED_LoadFromFile (sv.worldmodel->brush.entities);
1777 // LordHavoc: clear world angles (to fix e3m3.bsp)
1778 VectorClear(sv.edicts->v->angles);
1782 // all setup is completed, any further precache statements are errors
1783 sv.state = ss_active;
1785 // run two frames to allow everything to settle
1786 for (i = 0;i < 2;i++)
1788 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1794 // create a baseline for more efficient communications
1795 if (sv.protocol == PROTOCOL_QUAKE)
1796 SV_CreateBaseline ();
1798 // send serverinfo to all connected clients
1799 // (note this also handles botclients coming back from a level change)
1800 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1801 if (host_client->active)
1802 SV_SendServerinfo(host_client);
1804 Con_DPrint("Server spawned.\n");
1805 NetConn_Heartbeat (2);