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, this is fed to Protocol_EnumForName
25 cvar_t sv_protocolname = {0, "sv_protocolname", "DARKPLACES6"};
26 cvar_t sv_ratelimitlocalplayer = {0, "sv_ratelimitlocalplayer", "0"};
27 cvar_t sv_maxrate = {CVAR_SAVE | CVAR_NOTIFY, "sv_maxrate", "10000"};
29 static cvar_t sv_cullentities_pvs = {0, "sv_cullentities_pvs", "1"}; // fast but loose
30 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
31 static cvar_t sv_cullentities_stats = {0, "sv_cullentities_stats", "0"};
32 static cvar_t sv_entpatch = {0, "sv_entpatch", "1"};
34 extern cvar_t sys_ticrate;
36 cvar_t sv_gameplayfix_grenadebouncedownslopes = {0, "sv_gameplayfix_grenadebouncedownslopes", "1"};
37 cvar_t sv_gameplayfix_noairborncorpse = {0, "sv_gameplayfix_noairborncorpse", "1"};
38 cvar_t sv_gameplayfix_stepdown = {0, "sv_gameplayfix_stepdown", "1"};
39 cvar_t sv_gameplayfix_stepwhilejumping = {0, "sv_gameplayfix_stepwhilejumping", "1"};
40 cvar_t sv_gameplayfix_swiminbmodels = {0, "sv_gameplayfix_swiminbmodels", "1"};
41 cvar_t sv_gameplayfix_setmodelrealbox = {0, "sv_gameplayfix_setmodelrealbox", "1"};
42 cvar_t sv_gameplayfix_blowupfallenzombies = {0, "sv_gameplayfix_blowupfallenzombies", "1"};
43 cvar_t sv_gameplayfix_findradiusdistancetobox = {0, "sv_gameplayfix_findradiusdistancetobox", "1"};
45 cvar_t sv_progs = {0, "sv_progs", "progs.dat" };
50 mempool_t *sv_mempool = NULL;
52 //============================================================================
54 extern void SV_Phys_Init (void);
55 extern void SV_World_Init (void);
56 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_gameplayfix_setmodelrealbox);
87 Cvar_RegisterVariable (&sv_gameplayfix_blowupfallenzombies);
88 Cvar_RegisterVariable (&sv_gameplayfix_findradiusdistancetobox);
89 Cvar_RegisterVariable (&sv_protocolname);
90 Cvar_RegisterVariable (&sv_ratelimitlocalplayer);
91 Cvar_RegisterVariable (&sv_maxrate);
92 Cvar_RegisterVariable (&sv_progs);
97 sv_mempool = Mem_AllocPool("server", 0, NULL);
100 static void SV_SaveEntFile_f(void)
102 char basename[MAX_QPATH];
103 if (!sv.active || !sv.worldmodel)
105 Con_Print("Not running a server\n");
108 FS_StripExtension(sv.worldmodel->name, basename, sizeof(basename));
109 FS_WriteFile(va("%s.ent", basename), sv.worldmodel->brush.entities, strlen(sv.worldmodel->brush.entities));
114 =============================================================================
118 =============================================================================
125 Make sure the event gets sent to all clients
128 void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
132 if (sv.datagram.cursize > MAX_PACKETFRAGMENT-18)
134 MSG_WriteByte (&sv.datagram, svc_particle);
135 MSG_WriteCoord (&sv.datagram, org[0], sv.protocol);
136 MSG_WriteCoord (&sv.datagram, org[1], sv.protocol);
137 MSG_WriteCoord (&sv.datagram, org[2], sv.protocol);
138 for (i=0 ; i<3 ; i++)
145 MSG_WriteChar (&sv.datagram, v);
147 MSG_WriteByte (&sv.datagram, count);
148 MSG_WriteByte (&sv.datagram, color);
155 Make sure the event gets sent to all clients
158 void SV_StartEffect (vec3_t org, int modelindex, int startframe, int framecount, int framerate)
160 if (modelindex >= 256 || startframe >= 256)
162 if (sv.datagram.cursize > MAX_PACKETFRAGMENT-19)
164 MSG_WriteByte (&sv.datagram, svc_effect2);
165 MSG_WriteCoord (&sv.datagram, org[0], sv.protocol);
166 MSG_WriteCoord (&sv.datagram, org[1], sv.protocol);
167 MSG_WriteCoord (&sv.datagram, org[2], sv.protocol);
168 MSG_WriteShort (&sv.datagram, modelindex);
169 MSG_WriteShort (&sv.datagram, startframe);
170 MSG_WriteByte (&sv.datagram, framecount);
171 MSG_WriteByte (&sv.datagram, framerate);
175 if (sv.datagram.cursize > MAX_PACKETFRAGMENT-17)
177 MSG_WriteByte (&sv.datagram, svc_effect);
178 MSG_WriteCoord (&sv.datagram, org[0], sv.protocol);
179 MSG_WriteCoord (&sv.datagram, org[1], sv.protocol);
180 MSG_WriteCoord (&sv.datagram, org[2], sv.protocol);
181 MSG_WriteByte (&sv.datagram, modelindex);
182 MSG_WriteByte (&sv.datagram, startframe);
183 MSG_WriteByte (&sv.datagram, framecount);
184 MSG_WriteByte (&sv.datagram, framerate);
192 Each entity can have eight independant sound sources, like voice,
195 Channel 0 is an auto-allocate channel, the others override anything
196 already running on that entity/channel pair.
198 An attenuation of 0 will play full volume everywhere in the level.
199 Larger attenuations will drop off. (max 4 attenuation)
203 void SV_StartSound (edict_t *entity, int channel, const char *sample, int volume, float attenuation)
205 int sound_num, field_mask, i, ent;
207 if (volume < 0 || volume > 255)
208 Host_Error ("SV_StartSound: volume = %i", volume);
210 if (attenuation < 0 || attenuation > 4)
211 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
213 if (channel < 0 || channel > 7)
214 Host_Error ("SV_StartSound: channel = %i", channel);
216 if (sv.datagram.cursize > MAX_PACKETFRAGMENT-21)
219 // find precache number for sound
220 sound_num = SV_SoundIndex(sample, 1);
224 ent = NUM_FOR_EDICT(entity);
227 if (volume != DEFAULT_SOUND_PACKET_VOLUME)
228 field_mask |= SND_VOLUME;
229 if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
230 field_mask |= SND_ATTENUATION;
232 field_mask |= SND_LARGEENTITY;
233 if (sound_num >= 256 || channel >= 8)
234 field_mask |= SND_LARGESOUND;
236 // directed messages go only to the entity they are targeted on
237 MSG_WriteByte (&sv.datagram, svc_sound);
238 MSG_WriteByte (&sv.datagram, field_mask);
239 if (field_mask & SND_VOLUME)
240 MSG_WriteByte (&sv.datagram, volume);
241 if (field_mask & SND_ATTENUATION)
242 MSG_WriteByte (&sv.datagram, attenuation*64);
243 if (field_mask & SND_LARGEENTITY)
245 MSG_WriteShort (&sv.datagram, ent);
246 MSG_WriteByte (&sv.datagram, channel);
249 MSG_WriteShort (&sv.datagram, (ent<<3) | channel);
250 if (field_mask & SND_LARGESOUND)
251 MSG_WriteShort (&sv.datagram, sound_num);
253 MSG_WriteByte (&sv.datagram, sound_num);
254 for (i = 0;i < 3;i++)
255 MSG_WriteCoord (&sv.datagram, entity->v->origin[i]+0.5*(entity->v->mins[i]+entity->v->maxs[i]), sv.protocol);
259 ==============================================================================
263 ==============================================================================
270 Sends the first message from the server to a connected client.
271 This will be sent on the initial connection and upon each server load.
274 void SV_SendServerinfo (client_t *client)
279 // edicts get reallocated on level changes, so we need to update it here
280 client->edict = EDICT_NUM((client - svs.clients) + 1);
282 // if client is a botclient coming from a level change, we need to set up
283 // client info that normally requires networking
284 if (!client->netconnection)
287 ED_ClearEdict(client->edict);
289 // copy spawn parms out of the client_t
290 for (i=0 ; i< NUM_SPAWN_PARMS ; i++)
291 (&pr_global_struct->parm1)[i] = host_client->spawn_parms[i];
293 // call the spawn function
294 pr_global_struct->time = sv.time;
295 pr_global_struct->self = EDICT_TO_PROG(client->edict);
296 PR_ExecuteProgram (pr_global_struct->ClientConnect, "QC function ClientConnect is missing");
297 PR_ExecuteProgram (pr_global_struct->PutClientInServer, "QC function PutClientInServer is missing");
298 host_client->spawned = true;
302 // LordHavoc: clear entityframe tracking
303 client->latestframenum = 0;
305 if (client->entitydatabase)
306 EntityFrame_FreeDatabase(client->entitydatabase);
307 if (client->entitydatabase4)
308 EntityFrame4_FreeDatabase(client->entitydatabase4);
309 if (client->entitydatabase5)
310 EntityFrame5_FreeDatabase(client->entitydatabase5);
312 if (sv.protocol != PROTOCOL_QUAKE && sv.protocol != PROTOCOL_QUAKEDP && sv.protocol != PROTOCOL_NEHAHRAMOVIE)
314 if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3)
315 client->entitydatabase = EntityFrame_AllocDatabase(sv_mempool);
316 else if (sv.protocol == PROTOCOL_DARKPLACES4)
317 client->entitydatabase4 = EntityFrame4_AllocDatabase(sv_mempool);
319 client->entitydatabase5 = EntityFrame5_AllocDatabase(sv_mempool);
322 SZ_Clear (&client->message);
323 MSG_WriteByte (&client->message, svc_print);
324 dpsnprintf (message, sizeof (message), "\002\nServer: %s build %s (progs %i crc)", gamename, buildstring, pr_crc);
325 MSG_WriteString (&client->message,message);
327 MSG_WriteByte (&client->message, svc_serverinfo);
328 MSG_WriteLong (&client->message, Protocol_NumberForEnum(sv.protocol));
329 MSG_WriteByte (&client->message, svs.maxclients);
331 if (!coop.integer && deathmatch.integer)
332 MSG_WriteByte (&client->message, GAME_DEATHMATCH);
334 MSG_WriteByte (&client->message, GAME_COOP);
336 MSG_WriteString (&client->message,PR_GetString(sv.edicts->v->message));
338 for (i = 1;i < MAX_MODELS && sv.model_precache[i][0];i++)
339 MSG_WriteString (&client->message, sv.model_precache[i]);
340 MSG_WriteByte (&client->message, 0);
342 for (i = 1;i < MAX_SOUNDS && sv.sound_precache[i][0];i++)
343 MSG_WriteString (&client->message, sv.sound_precache[i]);
344 MSG_WriteByte (&client->message, 0);
347 MSG_WriteByte (&client->message, svc_cdtrack);
348 MSG_WriteByte (&client->message, sv.edicts->v->sounds);
349 MSG_WriteByte (&client->message, sv.edicts->v->sounds);
352 MSG_WriteByte (&client->message, svc_setview);
353 MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
355 MSG_WriteByte (&client->message, svc_signonnum);
356 MSG_WriteByte (&client->message, 1);
358 client->sendsignon = true;
359 client->spawned = false; // need prespawn, spawn, etc
366 Initializes a client_t for a new net connection. This will only be called
367 once for a player each game, not once for each level change.
370 void SV_ConnectClient (int clientnum, netconn_t *netconnection)
374 float spawn_parms[NUM_SPAWN_PARMS];
376 client = svs.clients + clientnum;
378 // set up the client_t
380 memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
381 memset (client, 0, sizeof(*client));
382 client->active = true;
383 client->netconnection = netconnection;
385 Con_DPrintf("Client %s connected\n", client->netconnection ? client->netconnection->address : "botclient");
387 strcpy(client->name, "unconnected");
388 strcpy(client->old_name, "unconnected");
389 client->spawned = false;
390 client->edict = EDICT_NUM(clientnum+1);
391 client->message.data = client->msgbuf;
392 client->message.maxsize = sizeof(client->msgbuf);
393 client->message.allowoverflow = true; // we can catch it
394 // updated by receiving "rate" command from client
395 client->rate = NET_MINRATE;
396 // no limits for local player
397 if (client->netconnection && LHNETADDRESS_GetAddressType(&client->netconnection->peeraddress) == LHNETADDRESSTYPE_LOOP)
398 client->rate = 1000000000;
399 client->connecttime = realtime;
402 memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
405 // call the progs to get default spawn parms for the new client
406 PR_ExecuteProgram (pr_global_struct->SetNewParms, "QC function SetNewParms is missing");
407 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
408 client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
411 // don't call SendServerinfo for a fresh botclient because its fields have
412 // not been set up by the qc yet
413 if (client->netconnection)
414 SV_SendServerinfo (client);
416 client->spawned = true;
421 ===============================================================================
425 ===============================================================================
434 void SV_ClearDatagram (void)
436 SZ_Clear (&sv.datagram);
440 =============================================================================
442 The PVS must include a small area around the client to allow head bobbing
443 or other small motion on the client side. Otherwise, a bob might cause an
444 entity that should be visible to not show up, especially when the bob
447 =============================================================================
450 int sv_writeentitiestoclient_pvsbytes;
451 qbyte sv_writeentitiestoclient_pvs[MAX_MAP_LEAFS/8];
453 static int numsendentities;
454 static entity_state_t sendentities[MAX_EDICTS];
455 static entity_state_t *sendentitiesindex[MAX_EDICTS];
457 void SV_PrepareEntitiesForSending(void)
464 // send all entities that touch the pvs
466 sendentitiesindex[0] = NULL;
467 for (e = 1, ent = NEXT_EDICT(sv.edicts);e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
469 sendentitiesindex[e] = NULL;
476 VectorCopy(ent->v->origin, cs.origin);
477 VectorCopy(ent->v->angles, cs.angles);
479 cs.effects = (unsigned)ent->v->effects;
480 cs.colormap = (unsigned)ent->v->colormap;
481 cs.skin = (unsigned)ent->v->skin;
482 cs.frame = (unsigned)ent->v->frame;
483 cs.viewmodelforclient = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)->edict;
484 cs.exteriormodelforclient = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)->edict;
485 cs.nodrawtoclient = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)->edict;
486 cs.drawonlytoclient = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)->edict;
487 cs.tagentity = GETEDICTFIELDVALUE(ent, eval_tag_entity)->edict;
488 cs.tagindex = (qbyte)GETEDICTFIELDVALUE(ent, eval_tag_index)->_float;
489 i = (int)(GETEDICTFIELDVALUE(ent, eval_glow_size)->_float * 0.25f);
490 cs.glowsize = (qbyte)bound(0, i, 255);
491 if (GETEDICTFIELDVALUE(ent, eval_glow_trail)->_float)
492 cs.flags |= RENDER_GLOWTRAIL;
494 // don't need to init cs.colormod because the defaultstate did that for us
495 //cs.colormod[0] = cs.colormod[1] = cs.colormod[2] = 32;
496 val = GETEDICTFIELDVALUE(ent, eval_colormod);
497 if (val->vector[0] || val->vector[1] || val->vector[2])
499 i = val->vector[0] * 32.0f;cs.colormod[0] = bound(0, i, 255);
500 i = val->vector[1] * 32.0f;cs.colormod[1] = bound(0, i, 255);
501 i = val->vector[2] * 32.0f;cs.colormod[2] = bound(0, i, 255);
505 i = (int)ent->v->modelindex;
506 if (i >= 1 && i < MAX_MODELS && *PR_GetString(ent->v->model))
510 f = (GETEDICTFIELDVALUE(ent, eval_alpha)->_float * 255.0f);
514 cs.alpha = (qbyte)bound(0, i, 255);
517 f = (GETEDICTFIELDVALUE(ent, eval_renderamt)->_float);
521 cs.alpha = (qbyte)bound(0, i, 255);
525 f = (GETEDICTFIELDVALUE(ent, eval_scale)->_float * 16.0f);
529 cs.scale = (qbyte)bound(0, i, 255);
533 f = (GETEDICTFIELDVALUE(ent, eval_glow_color)->_float);
535 cs.glowcolor = (int)f;
537 if (GETEDICTFIELDVALUE(ent, eval_fullbright)->_float)
538 cs.effects |= EF_FULLBRIGHT;
540 if (ent->v->movetype == MOVETYPE_STEP)
541 cs.flags |= RENDER_STEP;
542 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)
543 cs.flags |= RENDER_LOWPRECISION;
544 if (ent->v->colormap >= 1024)
545 cs.flags |= RENDER_COLORMAPPED;
546 if (cs.viewmodelforclient)
547 cs.flags |= RENDER_VIEWMODEL; // show relative to the view
549 f = GETEDICTFIELDVALUE(ent, eval_color)->vector[0]*256;
550 cs.light[0] = (unsigned short)bound(0, f, 65535);
551 f = GETEDICTFIELDVALUE(ent, eval_color)->vector[1]*256;
552 cs.light[1] = (unsigned short)bound(0, f, 65535);
553 f = GETEDICTFIELDVALUE(ent, eval_color)->vector[2]*256;
554 cs.light[2] = (unsigned short)bound(0, f, 65535);
555 f = GETEDICTFIELDVALUE(ent, eval_light_lev)->_float;
556 cs.light[3] = (unsigned short)bound(0, f, 65535);
557 cs.lightstyle = (qbyte)GETEDICTFIELDVALUE(ent, eval_style)->_float;
558 cs.lightpflags = (qbyte)GETEDICTFIELDVALUE(ent, eval_pflags)->_float;
560 if (gamemode == GAME_TENEBRAE)
562 // tenebrae's EF_FULLDYNAMIC conflicts with Q2's EF_NODRAW
566 cs.lightpflags |= PFLAGS_FULLDYNAMIC;
568 // tenebrae's EF_GREEN conflicts with DP's EF_ADDITIVE
576 cs.lightpflags |= PFLAGS_FULLDYNAMIC;
580 cs.specialvisibilityradius = 0;
581 if (cs.lightpflags & PFLAGS_FULLDYNAMIC)
582 cs.specialvisibilityradius = max(cs.specialvisibilityradius, cs.light[3]);
584 cs.specialvisibilityradius = max(cs.specialvisibilityradius, cs.glowsize * 4);
585 if (cs.flags & RENDER_GLOWTRAIL)
586 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 100);
587 if (cs.effects & (EF_BRIGHTFIELD | EF_MUZZLEFLASH | EF_BRIGHTLIGHT | EF_DIMLIGHT | EF_RED | EF_BLUE | EF_FLAME | EF_STARDUST))
589 if (cs.effects & EF_BRIGHTFIELD)
590 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 80);
591 if (cs.effects & EF_MUZZLEFLASH)
592 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 100);
593 if (cs.effects & EF_BRIGHTLIGHT)
594 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 400);
595 if (cs.effects & EF_DIMLIGHT)
596 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 200);
597 if (cs.effects & EF_RED)
598 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 200);
599 if (cs.effects & EF_BLUE)
600 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 200);
601 if (cs.effects & EF_FLAME)
602 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 250);
603 if (cs.effects & EF_STARDUST)
604 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 100);
607 if (numsendentities >= MAX_EDICTS)
609 // we can omit invisible entities with no effects that are not clients
610 // LordHavoc: this could kill tags attached to an invisible entity, I
611 // just hope we never have to support that case
612 if (cs.number > svs.maxclients && ((cs.effects & EF_NODRAW) || (!cs.modelindex && !cs.specialvisibilityradius)))
614 sendentitiesindex[e] = sendentities + numsendentities;
615 sendentities[numsendentities++] = cs;
619 static int sententitiesmark = 0;
620 static int sententities[MAX_EDICTS];
621 static int sententitiesconsideration[MAX_EDICTS];
622 static int sv_writeentitiestoclient_culled_pvs;
623 static int sv_writeentitiestoclient_culled_trace;
624 static int sv_writeentitiestoclient_visibleentities;
625 static int sv_writeentitiestoclient_totalentities;
626 //static entity_frame_t sv_writeentitiestoclient_entityframe;
627 static int sv_writeentitiestoclient_clentnum;
628 static vec3_t sv_writeentitiestoclient_testeye;
629 static client_t *sv_writeentitiestoclient_client;
631 void SV_MarkWriteEntityStateToClient(entity_state_t *s)
634 vec3_t entmins, entmaxs, lightmins, lightmaxs, testorigin;
637 if (sententitiesconsideration[s->number] == sententitiesmark)
639 sententitiesconsideration[s->number] = sententitiesmark;
640 // viewmodels don't have visibility checking
641 if (s->viewmodelforclient)
643 if (s->viewmodelforclient != sv_writeentitiestoclient_clentnum)
646 // never reject player
647 else if (s->number != sv_writeentitiestoclient_clentnum)
649 // check various rejection conditions
650 if (s->nodrawtoclient == sv_writeentitiestoclient_clentnum)
652 if (s->drawonlytoclient && s->drawonlytoclient != sv_writeentitiestoclient_clentnum)
654 if (s->effects & EF_NODRAW)
656 // LordHavoc: only send entities with a model or important effects
657 if (!s->modelindex && s->specialvisibilityradius == 0)
661 // tag attached entities simply check their parent
662 if (!sendentitiesindex[s->tagentity])
664 SV_MarkWriteEntityStateToClient(sendentitiesindex[s->tagentity]);
665 if (sententities[s->tagentity] != sententitiesmark)
668 // skip invalid modelindexes to avoid crashes
669 else if (s->modelindex >= MAX_MODELS)
671 // always send world submodels, they don't generate much traffic
672 // except in PROTOCOL_QUAKE where they hog bandwidth like crazy
673 else if (!(s->effects & EF_NODEPTHTEST) && (!(isbmodel = (model = sv.models[s->modelindex]) != NULL && model->name[0] == '*') || (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_QUAKEDP || sv.protocol == PROTOCOL_NEHAHRAMOVIE)))
675 Mod_CheckLoaded(model);
676 // entity has survived every check so far, check if visible
677 // enlarged box to account for prediction (not that there is
678 // any currently, but still helps the 'run into a room and
679 // watch items pop up' problem)
680 entmins[0] = s->origin[0] - 32.0f;
681 entmins[1] = s->origin[1] - 32.0f;
682 entmins[2] = s->origin[2] - 32.0f;
683 entmaxs[0] = s->origin[0] + 32.0f;
684 entmaxs[1] = s->origin[1] + 32.0f;
685 entmaxs[2] = s->origin[2] + 32.0f;
686 // using the model's bounding box to ensure things are visible regardless of their physics box
689 if (s->angles[0] || s->angles[2]) // pitch and roll
691 VectorAdd(entmins, model->rotatedmins, entmins);
692 VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
694 else if (s->angles[1])
696 VectorAdd(entmins, model->yawmins, entmins);
697 VectorAdd(entmaxs, model->yawmaxs, entmaxs);
701 VectorAdd(entmins, model->normalmins, entmins);
702 VectorAdd(entmaxs, model->normalmaxs, entmaxs);
705 lightmins[0] = min(entmins[0], s->origin[0] - s->specialvisibilityradius);
706 lightmins[1] = min(entmins[1], s->origin[1] - s->specialvisibilityradius);
707 lightmins[2] = min(entmins[2], s->origin[2] - s->specialvisibilityradius);
708 lightmaxs[0] = max(entmaxs[0], s->origin[0] + s->specialvisibilityradius);
709 lightmaxs[1] = max(entmaxs[1], s->origin[1] + s->specialvisibilityradius);
710 lightmaxs[2] = max(entmaxs[2], s->origin[2] + s->specialvisibilityradius);
711 sv_writeentitiestoclient_totalentities++;
712 // if not touching a visible leaf
713 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))
715 sv_writeentitiestoclient_culled_pvs++;
718 // or not seen by random tracelines
719 if (sv_cullentities_trace.integer && !isbmodel)
721 // LordHavoc: test center first
722 testorigin[0] = (entmins[0] + entmaxs[0]) * 0.5f;
723 testorigin[1] = (entmins[1] + entmaxs[1]) * 0.5f;
724 testorigin[2] = (entmins[2] + entmaxs[2]) * 0.5f;
725 sv.worldmodel->TraceBox(sv.worldmodel, 0, &trace, sv_writeentitiestoclient_testeye, sv_writeentitiestoclient_testeye, testorigin, testorigin, SUPERCONTENTS_SOLID);
726 if (trace.fraction == 1 || BoxesOverlap(trace.endpos, trace.endpos, entmins, entmaxs))
727 sv_writeentitiestoclient_client->visibletime[s->number] = realtime + 1;
730 // LordHavoc: test random offsets, to maximize chance of detection
731 testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
732 testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
733 testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
734 sv.worldmodel->TraceBox(sv.worldmodel, 0, &trace, sv_writeentitiestoclient_testeye, sv_writeentitiestoclient_testeye, testorigin, testorigin, SUPERCONTENTS_SOLID);
735 if (trace.fraction == 1 || BoxesOverlap(trace.endpos, trace.endpos, entmins, entmaxs))
736 sv_writeentitiestoclient_client->visibletime[s->number] = realtime + 1;
739 if (s->specialvisibilityradius)
741 // LordHavoc: test random offsets, to maximize chance of detection
742 testorigin[0] = lhrandom(lightmins[0], lightmaxs[0]);
743 testorigin[1] = lhrandom(lightmins[1], lightmaxs[1]);
744 testorigin[2] = lhrandom(lightmins[2], lightmaxs[2]);
745 sv.worldmodel->TraceBox(sv.worldmodel, 0, &trace, sv_writeentitiestoclient_testeye, sv_writeentitiestoclient_testeye, testorigin, testorigin, SUPERCONTENTS_SOLID);
746 if (trace.fraction == 1 || BoxesOverlap(trace.endpos, trace.endpos, entmins, entmaxs))
747 sv_writeentitiestoclient_client->visibletime[s->number] = realtime + 1;
751 if (realtime > sv_writeentitiestoclient_client->visibletime[s->number])
753 sv_writeentitiestoclient_culled_trace++;
757 sv_writeentitiestoclient_visibleentities++;
760 // this just marks it for sending
761 // FIXME: it would be more efficient to send here, but the entity
762 // compressor isn't that flexible
763 sententities[s->number] = sententitiesmark;
766 entity_state_t sendstates[MAX_EDICTS];
768 void SV_WriteEntitiesToClient(client_t *client, edict_t *clent, sizebuf_t *msg, int *stats)
770 int i, numsendstates;
773 // if there isn't enough space to accomplish anything, skip it
774 if (msg->cursize + 25 > msg->maxsize)
777 sv_writeentitiestoclient_client = client;
779 sv_writeentitiestoclient_culled_pvs = 0;
780 sv_writeentitiestoclient_culled_trace = 0;
781 sv_writeentitiestoclient_visibleentities = 0;
782 sv_writeentitiestoclient_totalentities = 0;
784 Mod_CheckLoaded(sv.worldmodel);
786 // find the client's PVS
787 // the real place being tested from
788 VectorAdd(clent->v->origin, clent->v->view_ofs, sv_writeentitiestoclient_testeye);
789 sv_writeentitiestoclient_pvsbytes = 0;
790 if (sv.worldmodel && sv.worldmodel->brush.FatPVS)
791 sv_writeentitiestoclient_pvsbytes = sv.worldmodel->brush.FatPVS(sv.worldmodel, sv_writeentitiestoclient_testeye, 8, sv_writeentitiestoclient_pvs, sizeof(sv_writeentitiestoclient_pvs));
793 sv_writeentitiestoclient_clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
797 for (i = 0;i < numsendentities;i++)
798 SV_MarkWriteEntityStateToClient(sendentities + i);
801 for (i = 0;i < numsendentities;i++)
803 if (sententities[sendentities[i].number] == sententitiesmark)
805 s = &sendstates[numsendstates++];
806 *s = sendentities[i];
807 if (s->exteriormodelforclient && s->exteriormodelforclient == sv_writeentitiestoclient_clentnum)
808 s->flags |= RENDER_EXTERIORMODEL;
812 if (sv_cullentities_stats.integer)
813 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);
815 if (client->entitydatabase5)
816 EntityFrame5_WriteFrame(msg, client->entitydatabase5, numsendstates, sendstates, client - svs.clients + 1, stats, client->movesequence);
817 else if (client->entitydatabase4)
818 EntityFrame4_WriteFrame(msg, client->entitydatabase4, numsendstates, sendstates);
819 else if (client->entitydatabase)
820 EntityFrame_WriteFrame(msg, client->entitydatabase, numsendstates, sendstates, client - svs.clients + 1);
822 EntityFrameQuake_WriteFrame(msg, numsendstates, sendstates);
831 void SV_CleanupEnts (void)
836 ent = NEXT_EDICT(sv.edicts);
837 for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
838 ent->v->effects = (int)ent->v->effects & ~EF_MUZZLEFLASH;
843 SV_WriteClientdataToMessage
847 void SV_WriteClientdataToMessage (client_t *client, edict_t *ent, sizebuf_t *msg, int *stats)
856 int weaponmodelindex;
859 // send a damage message
861 if (ent->v->dmg_take || ent->v->dmg_save)
863 other = PROG_TO_EDICT(ent->v->dmg_inflictor);
864 MSG_WriteByte (msg, svc_damage);
865 MSG_WriteByte (msg, ent->v->dmg_save);
866 MSG_WriteByte (msg, ent->v->dmg_take);
867 for (i=0 ; i<3 ; i++)
868 MSG_WriteCoord (msg, other->v->origin[i] + 0.5*(other->v->mins[i] + other->v->maxs[i]), sv.protocol);
870 ent->v->dmg_take = 0;
871 ent->v->dmg_save = 0;
875 // send the current viewpos offset from the view entity
877 SV_SetIdealPitch (); // how much to look up / down ideally
879 // a fixangle might get lost in a dropped packet. Oh well.
880 if ( ent->v->fixangle )
882 MSG_WriteByte (msg, svc_setangle);
883 for (i=0 ; i < 3 ; i++)
884 MSG_WriteAngle (msg, ent->v->angles[i], sv.protocol);
885 ent->v->fixangle = 0;
888 // stuff the sigil bits into the high bits of items for sbar, or else
890 val = GETEDICTFIELDVALUE(ent, eval_items2);
892 items = (int)ent->v->items | ((int)val->_float << 23);
894 items = (int)ent->v->items | ((int)pr_global_struct->serverflags << 28);
896 VectorClear(punchvector);
897 if ((val = GETEDICTFIELDVALUE(ent, eval_punchvector)))
898 VectorCopy(val->vector, punchvector);
900 weaponmodelindex = SV_ModelIndex(PR_GetString(ent->v->weaponmodel), 1);
903 if ((val = GETEDICTFIELDVALUE(ent, eval_viewzoom)))
904 viewzoom = val->_float * 255.0f;
910 if ((int)ent->v->flags & FL_ONGROUND)
912 if (ent->v->waterlevel >= 2)
914 if (ent->v->idealpitch)
915 bits |= SU_IDEALPITCH;
917 for (i=0 ; i<3 ; i++)
919 if (ent->v->punchangle[i])
920 bits |= (SU_PUNCH1<<i);
921 if (sv.protocol != PROTOCOL_QUAKE && sv.protocol != PROTOCOL_QUAKEDP && sv.protocol != PROTOCOL_NEHAHRAMOVIE)
923 bits |= (SU_PUNCHVEC1<<i);
924 if (ent->v->velocity[i])
925 bits |= (SU_VELOCITY1<<i);
928 memset(stats, 0, sizeof(int[MAX_CL_STATS]));
929 stats[STAT_VIEWHEIGHT] = ent->v->view_ofs[2];
930 stats[STAT_ITEMS] = items;
931 stats[STAT_WEAPONFRAME] = ent->v->weaponframe;
932 stats[STAT_ARMOR] = ent->v->armorvalue;
933 stats[STAT_WEAPON] = weaponmodelindex;
934 stats[STAT_HEALTH] = ent->v->health;
935 stats[STAT_AMMO] = ent->v->currentammo;
936 stats[STAT_SHELLS] = ent->v->ammo_shells;
937 stats[STAT_NAILS] = ent->v->ammo_nails;
938 stats[STAT_ROCKETS] = ent->v->ammo_rockets;
939 stats[STAT_CELLS] = ent->v->ammo_cells;
940 stats[STAT_ACTIVEWEAPON] = ent->v->weapon;
941 stats[STAT_VIEWZOOM] = viewzoom;
942 // the QC bumps these itself by sending svc_'s, so we have to keep them
943 // zero or they'll be corrected by the engine
944 //stats[STAT_TOTALSECRETS] = pr_global_struct->total_secrets;
945 //stats[STAT_TOTALMONSTERS] = pr_global_struct->total_monsters;
946 //stats[STAT_SECRETS] = pr_global_struct->found_secrets;
947 //stats[STAT_MONSTERS] = pr_global_struct->killed_monsters;
949 if (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_QUAKEDP || sv.protocol == PROTOCOL_NEHAHRAMOVIE || sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5)
951 if (stats[STAT_VIEWHEIGHT] != DEFAULT_VIEWHEIGHT) bits |= SU_VIEWHEIGHT;
953 if (stats[STAT_WEAPONFRAME]) bits |= SU_WEAPONFRAME;
954 if (stats[STAT_ARMOR]) bits |= SU_ARMOR;
956 // FIXME: which protocols support this? does PROTOCOL_DARKPLACES3 support viewzoom?
957 if (sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5)
964 if (bits >= 16777216)
968 MSG_WriteByte (msg, svc_clientdata);
969 MSG_WriteShort (msg, bits);
970 if (bits & SU_EXTEND1)
971 MSG_WriteByte(msg, bits >> 16);
972 if (bits & SU_EXTEND2)
973 MSG_WriteByte(msg, bits >> 24);
975 if (bits & SU_VIEWHEIGHT)
976 MSG_WriteChar (msg, stats[STAT_VIEWHEIGHT]);
978 if (bits & SU_IDEALPITCH)
979 MSG_WriteChar (msg, ent->v->idealpitch);
981 for (i=0 ; i<3 ; i++)
983 if (bits & (SU_PUNCH1<<i))
985 if (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_QUAKEDP || sv.protocol == PROTOCOL_NEHAHRAMOVIE)
986 MSG_WriteChar(msg, ent->v->punchangle[i]);
988 MSG_WriteAngle16i(msg, ent->v->punchangle[i]);
990 if (bits & (SU_PUNCHVEC1<<i))
992 if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4)
993 MSG_WriteCoord16i(msg, punchvector[i]);
995 MSG_WriteCoord32f(msg, punchvector[i]);
997 if (bits & (SU_VELOCITY1<<i))
999 if (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4)
1000 MSG_WriteChar(msg, ent->v->velocity[i] * (1.0f / 16.0f));
1002 MSG_WriteCoord32f(msg, ent->v->velocity[i]);
1006 if (bits & SU_ITEMS)
1007 MSG_WriteLong (msg, stats[STAT_ITEMS]);
1009 if (sv.protocol == PROTOCOL_DARKPLACES5)
1011 if (bits & SU_WEAPONFRAME)
1012 MSG_WriteShort (msg, stats[STAT_WEAPONFRAME]);
1013 if (bits & SU_ARMOR)
1014 MSG_WriteShort (msg, stats[STAT_ARMOR]);
1015 if (bits & SU_WEAPON)
1016 MSG_WriteShort (msg, stats[STAT_WEAPON]);
1017 MSG_WriteShort (msg, stats[STAT_HEALTH]);
1018 MSG_WriteShort (msg, stats[STAT_AMMO]);
1019 MSG_WriteShort (msg, stats[STAT_SHELLS]);
1020 MSG_WriteShort (msg, stats[STAT_NAILS]);
1021 MSG_WriteShort (msg, stats[STAT_ROCKETS]);
1022 MSG_WriteShort (msg, stats[STAT_CELLS]);
1023 MSG_WriteShort (msg, stats[STAT_ACTIVEWEAPON]);
1024 if (bits & SU_VIEWZOOM)
1025 MSG_WriteShort (msg, min(stats[STAT_VIEWZOOM], 65535));
1027 else if (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_QUAKEDP || sv.protocol == PROTOCOL_NEHAHRAMOVIE || sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4)
1029 if (bits & SU_WEAPONFRAME)
1030 MSG_WriteByte (msg, stats[STAT_WEAPONFRAME]);
1031 if (bits & SU_ARMOR)
1032 MSG_WriteByte (msg, stats[STAT_ARMOR]);
1033 if (bits & SU_WEAPON)
1034 MSG_WriteByte (msg, stats[STAT_WEAPON]);
1035 MSG_WriteShort (msg, stats[STAT_HEALTH]);
1036 MSG_WriteByte (msg, stats[STAT_AMMO]);
1037 MSG_WriteByte (msg, stats[STAT_SHELLS]);
1038 MSG_WriteByte (msg, stats[STAT_NAILS]);
1039 MSG_WriteByte (msg, stats[STAT_ROCKETS]);
1040 MSG_WriteByte (msg, stats[STAT_CELLS]);
1041 if (gamemode == GAME_HIPNOTIC || gamemode == GAME_ROGUE || gamemode == GAME_NEXUIZ)
1043 for (i = 0;i < 32;i++)
1044 if (stats[STAT_WEAPON] & (1<<i))
1046 MSG_WriteByte (msg, i);
1049 MSG_WriteByte (msg, stats[STAT_WEAPON]);
1050 if (bits & SU_VIEWZOOM)
1052 if (sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4)
1053 MSG_WriteByte (msg, min(stats[STAT_VIEWZOOM], 255));
1055 MSG_WriteShort (msg, min(stats[STAT_VIEWZOOM], 65535));
1061 =======================
1062 SV_SendClientDatagram
1063 =======================
1065 static qbyte sv_sendclientdatagram_buf[NET_MAXMESSAGE]; // FIXME?
1066 qboolean SV_SendClientDatagram (client_t *client)
1068 int rate, maxrate, maxsize, maxsize2;
1070 int stats[MAX_CL_STATS];
1072 if (LHNETADDRESS_GetAddressType(&host_client->netconnection->peeraddress) == LHNETADDRESSTYPE_LOOP && !sv_ratelimitlocalplayer.integer)
1074 // for good singleplayer, send huge packets
1075 maxsize = sizeof(sv_sendclientdatagram_buf);
1076 maxsize2 = sizeof(sv_sendclientdatagram_buf);
1078 else if (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_QUAKEDP || sv.protocol == PROTOCOL_NEHAHRAMOVIE || sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4)
1080 // no rate limiting support on older protocols because dp protocols
1081 // 1-4 kick the client off if they overflow, and quake protocol shows
1082 // less than the full entity set if rate limited
1088 // PROTOCOL_DARKPLACES5 and later support packet size limiting of updates
1089 maxrate = bound(NET_MINRATE, sv_maxrate.integer, NET_MAXRATE);
1090 if (sv_maxrate.integer != maxrate)
1091 Cvar_SetValueQuick(&sv_maxrate, maxrate);
1093 rate = bound(NET_MINRATE, client->rate, maxrate);
1094 rate = (int)(client->rate * sys_ticrate.value);
1095 maxsize = bound(100, rate, 1400);
1099 msg.data = sv_sendclientdatagram_buf;
1100 msg.maxsize = maxsize;
1103 MSG_WriteByte (&msg, svc_time);
1104 MSG_WriteFloat (&msg, sv.time);
1106 // add the client specific data to the datagram
1107 SV_WriteClientdataToMessage (client, client->edict, &msg, stats);
1108 SV_WriteEntitiesToClient (client, client->edict, &msg, stats);
1110 // expand packet size to allow effects to go over the rate limit
1111 // (dropping them is FAR too ugly)
1112 msg.maxsize = maxsize2;
1114 // copy the server datagram if there is space
1115 // FIXME: put in delayed queue of effects to send
1116 if (sv.datagram.cursize > 0 && msg.cursize + sv.datagram.cursize <= msg.maxsize)
1117 SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
1119 // send the datagram
1120 if (NetConn_SendUnreliableMessage (client->netconnection, &msg) == -1)
1122 SV_DropClient (true);// if the message couldn't send, kick off
1130 =======================
1131 SV_UpdateToReliableMessages
1132 =======================
1134 void SV_UpdateToReliableMessages (void)
1143 // check for changes to be sent over the reliable streams
1144 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1146 // update the host_client fields we care about according to the entity fields
1147 host_client->edict = EDICT_NUM(i+1);
1150 name = PR_GetString(host_client->edict->v->netname);
1153 // always point the string back at host_client->name to keep it safe
1154 strlcpy (host_client->name, name, sizeof (host_client->name));
1155 host_client->edict->v->netname = PR_SetEngineString(host_client->name);
1156 if (strcmp(host_client->old_name, host_client->name))
1158 if (host_client->spawned)
1159 SV_BroadcastPrintf("%s changed name to %s\n", host_client->old_name, host_client->name);
1160 strcpy(host_client->old_name, host_client->name);
1161 // send notification to all clients
1162 MSG_WriteByte (&sv.reliable_datagram, svc_updatename);
1163 MSG_WriteByte (&sv.reliable_datagram, i);
1164 MSG_WriteString (&sv.reliable_datagram, host_client->name);
1167 // DP_SV_CLIENTCOLORS
1168 // this is always found (since it's added by the progs loader)
1169 if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_clientcolors)))
1170 host_client->colors = (int)val->_float;
1171 if (host_client->old_colors != host_client->colors)
1173 host_client->old_colors = host_client->colors;
1174 // send notification to all clients
1175 MSG_WriteByte (&sv.reliable_datagram, svc_updatecolors);
1176 MSG_WriteByte (&sv.reliable_datagram, i);
1177 MSG_WriteByte (&sv.reliable_datagram, host_client->colors);
1180 // NEXUIZ_PLAYERMODEL
1181 if( eval_playermodel ) {
1182 model = PR_GetString(GETEDICTFIELDVALUE(host_client->edict, eval_playermodel)->string);
1185 // always point the string back at host_client->name to keep it safe
1186 strlcpy (host_client->playermodel, model, sizeof (host_client->playermodel));
1187 GETEDICTFIELDVALUE(host_client->edict, eval_playermodel)->string = PR_SetEngineString(host_client->playermodel);
1190 // NEXUIZ_PLAYERSKIN
1191 if( eval_playerskin ) {
1192 skin = PR_GetString(GETEDICTFIELDVALUE(host_client->edict, eval_playerskin)->string);
1195 // always point the string back at host_client->name to keep it safe
1196 strlcpy (host_client->playerskin, skin, sizeof (host_client->playerskin));
1197 GETEDICTFIELDVALUE(host_client->edict, eval_playerskin)->string = PR_SetEngineString(host_client->playerskin);
1201 host_client->frags = (int)host_client->edict->v->frags;
1202 if (host_client->old_frags != host_client->frags)
1204 host_client->old_frags = host_client->frags;
1205 // send notification to all clients
1206 MSG_WriteByte (&sv.reliable_datagram, svc_updatefrags);
1207 MSG_WriteByte (&sv.reliable_datagram, i);
1208 MSG_WriteShort (&sv.reliable_datagram, host_client->frags);
1212 for (j = 0, client = svs.clients;j < svs.maxclients;j++, client++)
1213 if (client->netconnection)
1214 SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
1216 SZ_Clear (&sv.reliable_datagram);
1221 =======================
1224 Send a nop message without trashing or sending the accumulated client
1226 =======================
1228 void SV_SendNop (client_t *client)
1234 msg.maxsize = sizeof(buf);
1237 MSG_WriteChar (&msg, svc_nop);
1239 if (NetConn_SendUnreliableMessage (client->netconnection, &msg) == -1)
1240 SV_DropClient (true); // if the message couldn't send, kick off
1241 client->last_message = realtime;
1245 =======================
1246 SV_SendClientMessages
1247 =======================
1249 void SV_SendClientMessages (void)
1251 int i, prepared = false;
1253 // update frags, names, etc
1254 SV_UpdateToReliableMessages();
1256 // build individual updates
1257 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1259 if (!host_client->active)
1261 if (!host_client->netconnection)
1263 SZ_Clear(&host_client->message);
1267 if (host_client->message.overflowed)
1269 SV_DropClient (true); // if the message couldn't send, kick off
1273 if (host_client->spawned)
1278 // only prepare entities once per frame
1279 SV_PrepareEntitiesForSending();
1281 if (!SV_SendClientDatagram (host_client))
1286 // the player isn't totally in the game yet
1287 // send small keepalive messages if too much time has passed
1288 // send a full message when the next signon stage has been requested
1289 // some other message data (name changes, etc) may accumulate
1290 // between signon stages
1291 if (!host_client->sendsignon)
1293 if (realtime - host_client->last_message > 5)
1294 SV_SendNop (host_client);
1295 continue; // don't send out non-signon messages
1299 if (host_client->message.cursize || host_client->dropasap)
1301 if (!NetConn_CanSendMessage (host_client->netconnection))
1304 if (host_client->dropasap)
1305 SV_DropClient (false); // went to another level
1308 if (NetConn_SendReliableMessage (host_client->netconnection, &host_client->message) == -1)
1309 SV_DropClient (true); // if the message couldn't send, kick off
1310 SZ_Clear (&host_client->message);
1311 host_client->last_message = realtime;
1312 host_client->sendsignon = false;
1317 // clear muzzle flashes
1323 ==============================================================================
1327 ==============================================================================
1336 int SV_ModelIndex(const char *s, int precachemode)
1338 int i, limit = ((sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_QUAKEDP || sv.protocol == PROTOCOL_NEHAHRAMOVIE) ? 256 : MAX_MODELS);
1339 char filename[MAX_QPATH];
1343 //if (precachemode == 2)
1345 strlcpy(filename, s, sizeof(filename));
1346 for (i = 2;i < limit;i++)
1348 if (!sv.model_precache[i][0])
1352 if (sv.state != ss_loading && (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_QUAKEDP || sv.protocol == PROTOCOL_NEHAHRAMOVIE || sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5))
1354 Con_Printf("SV_ModelIndex(\"%s\"): precache_model can only be done in spawn functions\n", filename);
1357 if (precachemode == 1)
1358 Con_Printf("SV_ModelIndex(\"%s\"): not precached (fix your code), precaching anyway\n", filename);
1359 strlcpy(sv.model_precache[i], filename, sizeof(sv.model_precache[i]));
1360 sv.models[i] = Mod_ForName (sv.model_precache[i], true, false, false);
1361 if (sv.state != ss_loading)
1363 MSG_WriteByte(&sv.reliable_datagram, svc_precache);
1364 MSG_WriteShort(&sv.reliable_datagram, i);
1365 MSG_WriteString(&sv.reliable_datagram, filename);
1369 Con_Printf("SV_ModelIndex(\"%s\"): not precached\n", filename);
1372 if (!strcmp(sv.model_precache[i], filename))
1375 Con_Printf("SV_ModelIndex(\"%s\"): i (%i) == MAX_MODELS (%i)\n", filename, i, MAX_MODELS);
1385 int SV_SoundIndex(const char *s, int precachemode)
1387 int i, limit = ((sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_QUAKEDP || sv.protocol == PROTOCOL_NEHAHRAMOVIE) ? 256 : MAX_SOUNDS);
1388 char filename[MAX_QPATH];
1392 //if (precachemode == 2)
1394 strlcpy(filename, s, sizeof(filename));
1395 for (i = 1;i < limit;i++)
1397 if (!sv.sound_precache[i][0])
1401 if (sv.state != ss_loading && (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_QUAKEDP || sv.protocol == PROTOCOL_NEHAHRAMOVIE || sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5))
1403 Con_Printf("SV_SoundIndex(\"%s\"): precache_sound can only be done in spawn functions\n", filename);
1406 if (precachemode == 1)
1407 Con_Printf("SV_SoundIndex(\"%s\"): not precached (fix your code), precaching anyway\n", filename);
1408 strlcpy(sv.sound_precache[i], filename, sizeof(sv.sound_precache[i]));
1409 if (sv.state != ss_loading)
1411 MSG_WriteByte(&sv.reliable_datagram, svc_precache);
1412 MSG_WriteShort(&sv.reliable_datagram, i + 32768);
1413 MSG_WriteString(&sv.reliable_datagram, filename);
1417 Con_Printf("SV_SoundIndex(\"%s\"): not precached\n", filename);
1420 if (!strcmp(sv.sound_precache[i], filename))
1423 Con_Printf("SV_SoundIndex(\"%s\"): i (%i) == MAX_SOUNDS (%i)\n", filename, i, MAX_SOUNDS);
1433 void SV_CreateBaseline (void)
1435 int i, entnum, large;
1438 // LordHavoc: clear *all* states (note just active ones)
1439 for (entnum = 0;entnum < sv.max_edicts;entnum++)
1441 // get the current server version
1442 svent = EDICT_NUM(entnum);
1444 // LordHavoc: always clear state values, whether the entity is in use or not
1445 svent->e->baseline = defaultstate;
1449 if (entnum > svs.maxclients && !svent->v->modelindex)
1452 // create entity baseline
1453 VectorCopy (svent->v->origin, svent->e->baseline.origin);
1454 VectorCopy (svent->v->angles, svent->e->baseline.angles);
1455 svent->e->baseline.frame = svent->v->frame;
1456 svent->e->baseline.skin = svent->v->skin;
1457 if (entnum > 0 && entnum <= svs.maxclients)
1459 svent->e->baseline.colormap = entnum;
1460 svent->e->baseline.modelindex = SV_ModelIndex("progs/player.mdl", 1);
1464 svent->e->baseline.colormap = 0;
1465 svent->e->baseline.modelindex = svent->v->modelindex;
1469 if (svent->e->baseline.modelindex & 0xFF00 || svent->e->baseline.frame & 0xFF00)
1472 // add to the message
1474 MSG_WriteByte (&sv.signon, svc_spawnbaseline2);
1476 MSG_WriteByte (&sv.signon, svc_spawnbaseline);
1477 MSG_WriteShort (&sv.signon, entnum);
1481 MSG_WriteShort (&sv.signon, svent->e->baseline.modelindex);
1482 MSG_WriteShort (&sv.signon, svent->e->baseline.frame);
1486 MSG_WriteByte (&sv.signon, svent->e->baseline.modelindex);
1487 MSG_WriteByte (&sv.signon, svent->e->baseline.frame);
1489 MSG_WriteByte (&sv.signon, svent->e->baseline.colormap);
1490 MSG_WriteByte (&sv.signon, svent->e->baseline.skin);
1491 for (i=0 ; i<3 ; i++)
1493 MSG_WriteCoord(&sv.signon, svent->e->baseline.origin[i], sv.protocol);
1494 MSG_WriteAngle(&sv.signon, svent->e->baseline.angles[i], sv.protocol);
1504 Tell all the clients that the server is changing levels
1507 void SV_SendReconnect (void)
1514 msg.maxsize = sizeof(data);
1516 MSG_WriteChar (&msg, svc_stufftext);
1517 MSG_WriteString (&msg, "reconnect\n");
1518 NetConn_SendToAll (&msg, 5);
1520 if (cls.state != ca_dedicated)
1521 Cmd_ExecuteString ("reconnect\n", src_command);
1529 Grabs the current state of each client for saving across the
1530 transition to another level
1533 void SV_SaveSpawnparms (void)
1537 svs.serverflags = pr_global_struct->serverflags;
1539 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1541 if (!host_client->active)
1544 // call the progs to get default spawn parms for the new client
1545 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1546 PR_ExecuteProgram (pr_global_struct->SetChangeParms, "QC function SetChangeParms is missing");
1547 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1548 host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1552 void SV_IncreaseEdicts(void)
1556 int oldmax_edicts = sv.max_edicts;
1557 void *oldedictsengineprivate = sv.edictsengineprivate;
1558 void *oldedictsfields = sv.edictsfields;
1559 void *oldmoved_edicts = sv.moved_edicts;
1561 if (sv.max_edicts >= MAX_EDICTS)
1564 // links don't survive the transition, so unlink everything
1565 for (i = 0, ent = sv.edicts;i < sv.max_edicts;i++, ent++)
1568 SV_UnlinkEdict(sv.edicts + i);
1569 memset(&ent->e->areagrid, 0, sizeof(ent->e->areagrid));
1573 sv.max_edicts = min(sv.max_edicts + 256, MAX_EDICTS);
1574 sv.edictsengineprivate = PR_Alloc(sv.max_edicts * sizeof(edict_engineprivate_t));
1575 sv.edictsfields = PR_Alloc(sv.max_edicts * pr_edict_size);
1576 sv.moved_edicts = PR_Alloc(sv.max_edicts * sizeof(edict_t *));
1578 memcpy(sv.edictsengineprivate, oldedictsengineprivate, oldmax_edicts * sizeof(edict_engineprivate_t));
1579 memcpy(sv.edictsfields, oldedictsfields, oldmax_edicts * pr_edict_size);
1581 for (i = 0, ent = sv.edicts;i < sv.max_edicts;i++, ent++)
1583 ent->e = sv.edictsengineprivate + i;
1584 ent->v = (void *)((qbyte *)sv.edictsfields + i * pr_edict_size);
1585 // link every entity except world
1587 SV_LinkEdict(ent, false);
1590 PR_Free(oldedictsengineprivate);
1591 PR_Free(oldedictsfields);
1592 PR_Free(oldmoved_edicts);
1599 This is called at the start of each level
1602 extern float scr_centertime_off;
1604 void SV_SpawnServer (const char *server)
1609 model_t *worldmodel;
1610 char modelname[sizeof(sv.modelname)];
1612 Con_DPrintf("SpawnServer: %s\n", server);
1614 if (cls.state != ca_dedicated)
1615 SCR_BeginLoadingPlaque();
1617 dpsnprintf (modelname, sizeof(modelname), "maps/%s.bsp", server);
1618 worldmodel = Mod_ForName(modelname, false, true, true);
1619 if (!worldmodel || !worldmodel->TraceBox)
1621 Con_Printf("Couldn't load map %s\n", modelname);
1625 // let's not have any servers with no name
1626 if (hostname.string[0] == 0)
1627 Cvar_Set ("hostname", "UNNAMED");
1628 scr_centertime_off = 0;
1630 svs.changelevel_issued = false; // now safe to issue another
1633 // tell all connected clients that we are going to a new level
1639 // make sure cvars have been checked before opening the ports
1640 NetConn_ServerFrame();
1641 NetConn_OpenServerPorts(true);
1645 // make cvars consistant
1648 Cvar_SetValue ("deathmatch", 0);
1649 // LordHavoc: it can be useful to have skills outside the range 0-3...
1650 //current_skill = bound(0, (int)(skill.value + 0.5), 3);
1651 //Cvar_SetValue ("skill", (float)current_skill);
1652 current_skill = (int)(skill.value + 0.5);
1655 // set up the new server
1657 Host_ClearMemory ();
1659 memset (&sv, 0, sizeof(sv));
1661 strlcpy (sv.name, server, sizeof (sv.name));
1663 sv.protocol = Protocol_EnumForName(sv_protocolname.string);
1664 if (sv.protocol == PROTOCOL_UNKNOWN)
1667 Protocol_Names(buffer, sizeof(buffer));
1668 Con_Printf("Unknown sv_protocolname \"%s\", valid values are:\n%s\n", sv_protocolname.string, buffer);
1669 sv.protocol = PROTOCOL_QUAKE;
1672 // load progs to get entity field count
1673 PR_LoadProgs ( sv_progs.string );
1675 // allocate server memory
1676 // start out with just enough room for clients and a reasonable estimate of entities
1677 sv.max_edicts = max(svs.maxclients + 1, 512);
1678 sv.max_edicts = min(sv.max_edicts, MAX_EDICTS);
1680 // edict_t structures (hidden from progs)
1681 sv.edicts = PR_Alloc(MAX_EDICTS * sizeof(edict_t));
1682 // engine private structures (hidden from progs)
1683 sv.edictsengineprivate = PR_Alloc(sv.max_edicts * sizeof(edict_engineprivate_t));
1684 // progs fields, often accessed by server
1685 sv.edictsfields = PR_Alloc(sv.max_edicts * pr_edict_size);
1686 // used by PushMove to move back pushed entities
1687 sv.moved_edicts = PR_Alloc(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 dpsnprintf(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_SetEngineString(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_SetEngineString(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 = host_frametime = 0.1;
1794 // create a baseline for more efficient communications
1795 if (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_QUAKEDP || sv.protocol == PROTOCOL_NEHAHRAMOVIE)
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);