2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // sv_main.c -- server main program
24 // select which protocol to host, by name
25 // this is named the same as PROTOCOL_DARKPLACES5 for example, minus the PROTOCOL_ prefix
26 cvar_t sv_protocolname = {0, "sv_protocolname", "DARKPLACES5"};
27 cvar_t sv_ratelimitlocalplayer = {0, "sv_ratelimitlocalplayer", "0"};
28 cvar_t sv_maxrate = {CVAR_SAVE | CVAR_NOTIFY, "sv_maxrate", "10000"};
30 static cvar_t sv_cullentities_pvs = {0, "sv_cullentities_pvs", "1"}; // fast but loose
31 static cvar_t sv_cullentities_trace = {0, "sv_cullentities_trace", "0"}; // tends to get false negatives, uses a timeout to keep entities visible a short time after becoming hidden
32 static cvar_t sv_cullentities_stats = {0, "sv_cullentities_stats", "0"};
33 static cvar_t sv_entpatch = {0, "sv_entpatch", "1"};
35 cvar_t sv_gameplayfix_grenadebouncedownslopes = {0, "sv_gameplayfix_grenadebouncedownslopes", "1"};
36 cvar_t sv_gameplayfix_noairborncorpse = {0, "sv_gameplayfix_noairborncorpse", "1"};
37 cvar_t sv_gameplayfix_stepdown = {0, "sv_gameplayfix_stepdown", "1"};
38 cvar_t sv_gameplayfix_stepwhilejumping = {0, "sv_gameplayfix_stepwhilejumping", "1"};
39 cvar_t sv_gameplayfix_swiminbmodels = {0, "sv_gameplayfix_swiminbmodels", "1"};
44 static char localmodels[MAX_MODELS][5]; // inline model names for precache
46 mempool_t *sv_edicts_mempool = NULL;
48 //============================================================================
50 extern void SV_Phys_Init (void);
51 extern void SV_World_Init (void);
52 static void SV_SaveEntFile_f(void);
63 Cmd_AddCommand("sv_saveentfile", SV_SaveEntFile_f);
64 Cvar_RegisterVariable (&sv_maxvelocity);
65 Cvar_RegisterVariable (&sv_gravity);
66 Cvar_RegisterVariable (&sv_friction);
67 Cvar_RegisterVariable (&sv_edgefriction);
68 Cvar_RegisterVariable (&sv_stopspeed);
69 Cvar_RegisterVariable (&sv_maxspeed);
70 Cvar_RegisterVariable (&sv_accelerate);
71 Cvar_RegisterVariable (&sv_idealpitchscale);
72 Cvar_RegisterVariable (&sv_aim);
73 Cvar_RegisterVariable (&sv_nostep);
74 Cvar_RegisterVariable (&sv_deltacompress);
75 Cvar_RegisterVariable (&sv_cullentities_pvs);
76 Cvar_RegisterVariable (&sv_cullentities_trace);
77 Cvar_RegisterVariable (&sv_cullentities_stats);
78 Cvar_RegisterVariable (&sv_entpatch);
79 Cvar_RegisterVariable (&sv_gameplayfix_grenadebouncedownslopes);
80 Cvar_RegisterVariable (&sv_gameplayfix_noairborncorpse);
81 Cvar_RegisterVariable (&sv_gameplayfix_stepdown);
82 Cvar_RegisterVariable (&sv_gameplayfix_stepwhilejumping);
83 Cvar_RegisterVariable (&sv_gameplayfix_swiminbmodels);
84 Cvar_RegisterVariable (&sv_protocolname);
85 Cvar_RegisterVariable (&sv_ratelimitlocalplayer);
86 Cvar_RegisterVariable (&sv_maxrate);
91 for (i = 0;i < MAX_MODELS;i++)
92 sprintf (localmodels[i], "*%i", i);
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));
110 =============================================================================
114 =============================================================================
121 Make sure the event gets sent to all clients
124 void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
128 if (sv.datagram.cursize > MAX_PACKETFRAGMENT-18)
130 MSG_WriteByte (&sv.datagram, svc_particle);
131 MSG_WriteCoord (&sv.datagram, org[0], sv.protocol);
132 MSG_WriteCoord (&sv.datagram, org[1], sv.protocol);
133 MSG_WriteCoord (&sv.datagram, org[2], sv.protocol);
134 for (i=0 ; i<3 ; i++)
141 MSG_WriteChar (&sv.datagram, v);
143 MSG_WriteByte (&sv.datagram, count);
144 MSG_WriteByte (&sv.datagram, color);
151 Make sure the event gets sent to all clients
154 void SV_StartEffect (vec3_t org, int modelindex, int startframe, int framecount, int framerate)
156 if (modelindex >= 256 || startframe >= 256)
158 if (sv.datagram.cursize > MAX_PACKETFRAGMENT-19)
160 MSG_WriteByte (&sv.datagram, svc_effect2);
161 MSG_WriteCoord (&sv.datagram, org[0], sv.protocol);
162 MSG_WriteCoord (&sv.datagram, org[1], sv.protocol);
163 MSG_WriteCoord (&sv.datagram, org[2], sv.protocol);
164 MSG_WriteShort (&sv.datagram, modelindex);
165 MSG_WriteShort (&sv.datagram, startframe);
166 MSG_WriteByte (&sv.datagram, framecount);
167 MSG_WriteByte (&sv.datagram, framerate);
171 if (sv.datagram.cursize > MAX_PACKETFRAGMENT-17)
173 MSG_WriteByte (&sv.datagram, svc_effect);
174 MSG_WriteCoord (&sv.datagram, org[0], sv.protocol);
175 MSG_WriteCoord (&sv.datagram, org[1], sv.protocol);
176 MSG_WriteCoord (&sv.datagram, org[2], sv.protocol);
177 MSG_WriteByte (&sv.datagram, modelindex);
178 MSG_WriteByte (&sv.datagram, startframe);
179 MSG_WriteByte (&sv.datagram, framecount);
180 MSG_WriteByte (&sv.datagram, framerate);
188 Each entity can have eight independant sound sources, like voice,
191 Channel 0 is an auto-allocate channel, the others override anything
192 already running on that entity/channel pair.
194 An attenuation of 0 will play full volume everywhere in the level.
195 Larger attenuations will drop off. (max 4 attenuation)
199 void SV_StartSound (edict_t *entity, int channel, char *sample, int volume, float attenuation)
201 int sound_num, field_mask, i, ent;
203 if (volume < 0 || volume > 255)
204 Host_Error ("SV_StartSound: volume = %i", volume);
206 if (attenuation < 0 || attenuation > 4)
207 Host_Error ("SV_StartSound: attenuation = %f", attenuation);
209 if (channel < 0 || channel > 7)
210 Host_Error ("SV_StartSound: channel = %i", channel);
212 if (sv.datagram.cursize > MAX_PACKETFRAGMENT-21)
215 // find precache number for sound
216 for (sound_num=1 ; sound_num<MAX_SOUNDS && sv.sound_precache[sound_num] ; sound_num++)
217 if (!strcmp(sample, sv.sound_precache[sound_num]))
220 if ( sound_num == MAX_SOUNDS || !sv.sound_precache[sound_num] )
222 Con_Printf("SV_StartSound: %s not precached\n", sample);
226 ent = NUM_FOR_EDICT(entity);
229 if (volume != DEFAULT_SOUND_PACKET_VOLUME)
230 field_mask |= SND_VOLUME;
231 if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
232 field_mask |= SND_ATTENUATION;
234 field_mask |= SND_LARGEENTITY;
235 if (sound_num >= 256 || channel >= 8)
236 field_mask |= SND_LARGESOUND;
238 // directed messages go only to the entity they are targeted on
239 MSG_WriteByte (&sv.datagram, svc_sound);
240 MSG_WriteByte (&sv.datagram, field_mask);
241 if (field_mask & SND_VOLUME)
242 MSG_WriteByte (&sv.datagram, volume);
243 if (field_mask & SND_ATTENUATION)
244 MSG_WriteByte (&sv.datagram, attenuation*64);
245 if (field_mask & SND_LARGEENTITY)
247 MSG_WriteShort (&sv.datagram, ent);
248 MSG_WriteByte (&sv.datagram, channel);
251 MSG_WriteShort (&sv.datagram, (ent<<3) | channel);
252 if (field_mask & SND_LARGESOUND)
253 MSG_WriteShort (&sv.datagram, sound_num);
255 MSG_WriteByte (&sv.datagram, sound_num);
256 for (i = 0;i < 3;i++)
257 MSG_WriteCoord (&sv.datagram, entity->v->origin[i]+0.5*(entity->v->mins[i]+entity->v->maxs[i]), sv.protocol);
261 ==============================================================================
265 ==============================================================================
272 Sends the first message from the server to a connected client.
273 This will be sent on the initial connection and upon each server load.
276 void SV_SendServerinfo (client_t *client)
281 // edicts get reallocated on level changes, so we need to update it here
282 client->edict = EDICT_NUM(client->number + 1);
285 // LordHavoc: clear entityframe tracking
287 if (client->entitydatabase)
288 EntityFrame_FreeDatabase(client->entitydatabase);
289 if (client->entitydatabase4)
290 EntityFrame4_FreeDatabase(client->entitydatabase4);
291 if (client->entitydatabase5)
292 EntityFrame5_FreeDatabase(client->entitydatabase5);
294 if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3)
295 client->entitydatabase = EntityFrame_AllocDatabase(sv_clients_mempool);
296 if (sv.protocol == PROTOCOL_DARKPLACES4)
297 client->entitydatabase4 = EntityFrame4_AllocDatabase(sv_clients_mempool);
298 if (sv.protocol == PROTOCOL_DARKPLACES5)
299 client->entitydatabase5 = EntityFrame5_AllocDatabase(sv_clients_mempool);
301 MSG_WriteByte (&client->message, svc_print);
302 snprintf (message, sizeof (message), "\002\nServer: %s build %s (progs %i crc)", gamename, buildstring, pr_crc);
303 MSG_WriteString (&client->message,message);
305 MSG_WriteByte (&client->message, svc_serverinfo);
306 MSG_WriteLong (&client->message, sv.protocol);
307 MSG_WriteByte (&client->message, svs.maxclients);
309 if (!coop.integer && deathmatch.integer)
310 MSG_WriteByte (&client->message, GAME_DEATHMATCH);
312 MSG_WriteByte (&client->message, GAME_COOP);
314 MSG_WriteString (&client->message,PR_GetString(sv.edicts->v->message));
316 for (s = sv.model_precache+1 ; *s ; s++)
317 MSG_WriteString (&client->message, *s);
318 MSG_WriteByte (&client->message, 0);
320 for (s = sv.sound_precache+1 ; *s ; s++)
321 MSG_WriteString (&client->message, *s);
322 MSG_WriteByte (&client->message, 0);
325 MSG_WriteByte (&client->message, svc_cdtrack);
326 MSG_WriteByte (&client->message, sv.edicts->v->sounds);
327 MSG_WriteByte (&client->message, sv.edicts->v->sounds);
330 MSG_WriteByte (&client->message, svc_setview);
331 MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
333 MSG_WriteByte (&client->message, svc_signonnum);
334 MSG_WriteByte (&client->message, 1);
336 client->sendsignon = true;
337 client->spawned = false; // need prespawn, spawn, etc
344 Initializes a client_t for a new net connection. This will only be called
345 once for a player each game, not once for each level change.
348 void SV_ConnectClient (int clientnum, netconn_t *netconnection)
352 float spawn_parms[NUM_SPAWN_PARMS];
354 client = svs.clients + clientnum;
356 // set up the client_t
358 memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
359 memset (client, 0, sizeof(*client));
360 client->active = true;
361 client->netconnection = netconnection;
363 Con_DPrintf("Client %s connected\n", client->netconnection->address);
365 strcpy(client->name, "unconnected");
366 strcpy(client->old_name, "unconnected");
367 client->number = clientnum;
368 client->spawned = false;
369 client->edict = EDICT_NUM(clientnum+1);
370 client->message.data = client->msgbuf;
371 client->message.maxsize = sizeof(client->msgbuf);
372 client->message.allowoverflow = true; // we can catch it
375 memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
378 // call the progs to get default spawn parms for the new client
379 PR_ExecuteProgram (pr_global_struct->SetNewParms, "QC function SetNewParms is missing");
380 for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
381 client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
384 SV_SendServerinfo (client);
389 ===============================================================================
393 ===============================================================================
402 void SV_ClearDatagram (void)
404 SZ_Clear (&sv.datagram);
408 =============================================================================
410 The PVS must include a small area around the client to allow head bobbing
411 or other small motion on the client side. Otherwise, a bob might cause an
412 entity that should be visible to not show up, especially when the bob
415 =============================================================================
418 int sv_writeentitiestoclient_pvsbytes;
419 qbyte sv_writeentitiestoclient_pvs[MAX_MAP_LEAFS/8];
421 static int numsendentities;
422 static entity_state_t sendentities[MAX_EDICTS];
423 static entity_state_t *sendentitiesindex[MAX_EDICTS];
425 void SV_PrepareEntitiesForSending(void)
431 // send all entities that touch the pvs
433 sendentitiesindex[0] = NULL;
434 for (e = 1, ent = NEXT_EDICT(sv.edicts);e < sv.num_edicts;e++, ent = NEXT_EDICT(ent))
436 sendentitiesindex[e] = NULL;
443 VectorCopy(ent->v->origin, cs.origin);
444 VectorCopy(ent->v->angles, cs.angles);
446 cs.effects = (int)ent->v->effects;
447 cs.colormap = (qbyte)ent->v->colormap;
448 cs.skin = (qbyte)ent->v->skin;
449 cs.frame = (qbyte)ent->v->frame;
450 cs.viewmodelforclient = GETEDICTFIELDVALUE(ent, eval_viewmodelforclient)->edict;
451 cs.exteriormodelforclient = GETEDICTFIELDVALUE(ent, eval_exteriormodeltoclient)->edict;
452 cs.nodrawtoclient = GETEDICTFIELDVALUE(ent, eval_nodrawtoclient)->edict;
453 cs.drawonlytoclient = GETEDICTFIELDVALUE(ent, eval_drawonlytoclient)->edict;
454 cs.tagentity = GETEDICTFIELDVALUE(ent, eval_tag_entity)->edict;
455 cs.tagindex = (qbyte)GETEDICTFIELDVALUE(ent, eval_tag_index)->_float;
456 i = (int)(GETEDICTFIELDVALUE(ent, eval_glow_size)->_float * 0.25f);
457 cs.glowsize = (qbyte)bound(0, i, 255);
458 if (GETEDICTFIELDVALUE(ent, eval_glow_trail)->_float)
459 cs.flags |= RENDER_GLOWTRAIL;
462 i = (int)ent->v->modelindex;
463 if (i >= 1 && i < MAX_MODELS && *PR_GetString(ent->v->model))
467 f = (GETEDICTFIELDVALUE(ent, eval_alpha)->_float * 255.0f);
471 cs.alpha = (qbyte)bound(0, i, 255);
474 f = (GETEDICTFIELDVALUE(ent, eval_renderamt)->_float);
478 cs.alpha = (qbyte)bound(0, i, 255);
482 f = (GETEDICTFIELDVALUE(ent, eval_scale)->_float * 16.0f);
486 cs.scale = (qbyte)bound(0, i, 255);
490 f = (GETEDICTFIELDVALUE(ent, eval_glow_color)->_float);
492 cs.glowcolor = (int)f;
494 if (GETEDICTFIELDVALUE(ent, eval_fullbright)->_float)
495 cs.effects |= EF_FULLBRIGHT;
497 if (ent->v->movetype == MOVETYPE_STEP)
498 cs.flags |= RENDER_STEP;
499 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)
500 cs.flags |= RENDER_LOWPRECISION;
501 if (ent->v->colormap >= 1024)
502 cs.flags |= RENDER_COLORMAPPED;
503 if (cs.viewmodelforclient)
504 cs.flags |= RENDER_VIEWMODEL; // show relative to the view
506 f = GETEDICTFIELDVALUE(ent, eval_color)->vector[0]*256;
507 cs.light[0] = (unsigned short)bound(0, f, 65535);
508 f = GETEDICTFIELDVALUE(ent, eval_color)->vector[1]*256;
509 cs.light[1] = (unsigned short)bound(0, f, 65535);
510 f = GETEDICTFIELDVALUE(ent, eval_color)->vector[2]*256;
511 cs.light[2] = (unsigned short)bound(0, f, 65535);
512 f = GETEDICTFIELDVALUE(ent, eval_light_lev)->_float;
513 cs.light[3] = (unsigned short)bound(0, f, 65535);
514 cs.lightstyle = (qbyte)GETEDICTFIELDVALUE(ent, eval_style)->_float;
515 cs.lightpflags = (qbyte)GETEDICTFIELDVALUE(ent, eval_pflags)->_float;
517 if (gamemode == GAME_TENEBRAE)
519 // tenebrae's EF_FULLDYNAMIC conflicts with Q2's EF_NODRAW
523 cs.lightpflags |= PFLAGS_FULLDYNAMIC;
525 // tenebrae's EF_GREEN conflicts with DP's EF_ADDITIVE
533 cs.lightpflags |= PFLAGS_FULLDYNAMIC;
537 cs.specialvisibilityradius = 0;
538 if (cs.lightpflags & PFLAGS_FULLDYNAMIC)
539 cs.specialvisibilityradius = max(cs.specialvisibilityradius, cs.light[3]);
541 cs.specialvisibilityradius = max(cs.specialvisibilityradius, cs.glowsize * 4);
542 if (cs.flags & RENDER_GLOWTRAIL)
543 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 100);
544 if (cs.effects & (EF_BRIGHTFIELD | EF_MUZZLEFLASH | EF_BRIGHTLIGHT | EF_DIMLIGHT | EF_RED | EF_BLUE | EF_FLAME | EF_STARDUST))
546 if (cs.effects & EF_BRIGHTFIELD)
547 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 80);
548 if (cs.effects & EF_MUZZLEFLASH)
549 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 100);
550 if (cs.effects & EF_BRIGHTLIGHT)
551 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 400);
552 if (cs.effects & EF_DIMLIGHT)
553 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 200);
554 if (cs.effects & EF_RED)
555 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 200);
556 if (cs.effects & EF_BLUE)
557 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 200);
558 if (cs.effects & EF_FLAME)
559 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 250);
560 if (cs.effects & EF_STARDUST)
561 cs.specialvisibilityradius = max(cs.specialvisibilityradius, 100);
564 if (numsendentities >= MAX_EDICTS)
566 // we can omit invisible entities with no effects that are not clients
567 // LordHavoc: this could kill tags attached to an invisible entity, I
568 // just hope we never have to support that case
569 if (cs.number > svs.maxclients && ((cs.effects & EF_NODRAW) || (!cs.modelindex && !cs.specialvisibilityradius)))
571 sendentitiesindex[e] = sendentities + numsendentities;
572 sendentities[numsendentities++] = cs;
576 static int sententitiesmark = 0;
577 static int sententities[MAX_EDICTS];
578 static int sententitiesconsideration[MAX_EDICTS];
579 static int sv_writeentitiestoclient_culled_pvs;
580 static int sv_writeentitiestoclient_culled_trace;
581 static int sv_writeentitiestoclient_visibleentities;
582 static int sv_writeentitiestoclient_totalentities;
583 //static entity_frame_t sv_writeentitiestoclient_entityframe;
584 static int sv_writeentitiestoclient_clentnum;
585 static vec3_t sv_writeentitiestoclient_testeye;
586 static client_t *sv_writeentitiestoclient_client;
588 void SV_MarkWriteEntityStateToClient(entity_state_t *s)
591 vec3_t entmins, entmaxs, lightmins, lightmaxs, testorigin;
594 if (sententitiesconsideration[s->number] == sententitiesmark)
596 sententitiesconsideration[s->number] = sententitiesmark;
597 // viewmodels don't have visibility checking
598 if (s->viewmodelforclient)
600 if (s->viewmodelforclient != sv_writeentitiestoclient_clentnum)
603 // never reject player
604 else if (s->number != sv_writeentitiestoclient_clentnum)
606 // check various rejection conditions
607 if (s->nodrawtoclient == sv_writeentitiestoclient_clentnum)
609 if (s->drawonlytoclient && s->drawonlytoclient != sv_writeentitiestoclient_clentnum)
611 if (s->effects & EF_NODRAW)
613 // LordHavoc: only send entities with a model or important effects
614 if (!s->modelindex && s->specialvisibilityradius == 0)
618 // tag attached entities simply check their parent
619 if (!sendentitiesindex[s->tagentity])
621 SV_MarkWriteEntityStateToClient(sendentitiesindex[s->tagentity]);
622 if (sententities[s->tagentity] != sententitiesmark)
625 // always send world submodels, they don't generate much traffic
626 // except in PROTOCOL_QUAKE where they hog bandwidth like crazy
627 else if ((!(isbmodel = (model = sv.models[s->modelindex]) != NULL && model->name[0] == '*') && !(s->effects & EF_NODEPTHTEST)) || sv.protocol == PROTOCOL_QUAKE)
629 Mod_CheckLoaded(model);
630 // entity has survived every check so far, check if visible
631 // enlarged box to account for prediction (not that there is
632 // any currently, but still helps the 'run into a room and
633 // watch items pop up' problem)
634 entmins[0] = s->origin[0] - 32.0f;
635 entmins[1] = s->origin[1] - 32.0f;
636 entmins[2] = s->origin[2] - 32.0f;
637 entmaxs[0] = s->origin[0] + 32.0f;
638 entmaxs[1] = s->origin[1] + 32.0f;
639 entmaxs[2] = s->origin[2] + 32.0f;
640 // using the model's bounding box to ensure things are visible regardless of their physics box
643 if (s->angles[0] || s->angles[2]) // pitch and roll
645 VectorAdd(entmins, model->rotatedmins, entmins);
646 VectorAdd(entmaxs, model->rotatedmaxs, entmaxs);
648 else if (s->angles[1])
650 VectorAdd(entmins, model->yawmins, entmins);
651 VectorAdd(entmaxs, model->yawmaxs, entmaxs);
655 VectorAdd(entmins, model->normalmins, entmins);
656 VectorAdd(entmaxs, model->normalmaxs, entmaxs);
659 lightmins[0] = min(entmins[0], s->origin[0] - s->specialvisibilityradius);
660 lightmins[1] = min(entmins[1], s->origin[1] - s->specialvisibilityradius);
661 lightmins[2] = min(entmins[2], s->origin[2] - s->specialvisibilityradius);
662 lightmaxs[0] = max(entmaxs[0], s->origin[0] + s->specialvisibilityradius);
663 lightmaxs[1] = max(entmaxs[1], s->origin[1] + s->specialvisibilityradius);
664 lightmaxs[2] = max(entmaxs[2], s->origin[2] + s->specialvisibilityradius);
665 sv_writeentitiestoclient_totalentities++;
666 // if not touching a visible leaf
667 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))
669 sv_writeentitiestoclient_culled_pvs++;
672 // or not seen by random tracelines
673 if (sv_cullentities_trace.integer && !isbmodel)
675 // LordHavoc: test center first
676 testorigin[0] = (entmins[0] + entmaxs[0]) * 0.5f;
677 testorigin[1] = (entmins[1] + entmaxs[1]) * 0.5f;
678 testorigin[2] = (entmins[2] + entmaxs[2]) * 0.5f;
679 sv.worldmodel->TraceBox(sv.worldmodel, 0, &trace, sv_writeentitiestoclient_testeye, sv_writeentitiestoclient_testeye, testorigin, testorigin, SUPERCONTENTS_SOLID);
680 if (trace.fraction == 1 || BoxesOverlap(trace.endpos, trace.endpos, entmins, entmaxs))
681 sv_writeentitiestoclient_client->visibletime[s->number] = realtime + 1;
684 // LordHavoc: test random offsets, to maximize chance of detection
685 testorigin[0] = lhrandom(entmins[0], entmaxs[0]);
686 testorigin[1] = lhrandom(entmins[1], entmaxs[1]);
687 testorigin[2] = lhrandom(entmins[2], entmaxs[2]);
688 sv.worldmodel->TraceBox(sv.worldmodel, 0, &trace, sv_writeentitiestoclient_testeye, sv_writeentitiestoclient_testeye, testorigin, testorigin, SUPERCONTENTS_SOLID);
689 if (trace.fraction == 1 || BoxesOverlap(trace.endpos, trace.endpos, entmins, entmaxs))
690 sv_writeentitiestoclient_client->visibletime[s->number] = realtime + 1;
693 if (s->specialvisibilityradius)
695 // LordHavoc: test random offsets, to maximize chance of detection
696 testorigin[0] = lhrandom(lightmins[0], lightmaxs[0]);
697 testorigin[1] = lhrandom(lightmins[1], lightmaxs[1]);
698 testorigin[2] = lhrandom(lightmins[2], lightmaxs[2]);
699 sv.worldmodel->TraceBox(sv.worldmodel, 0, &trace, sv_writeentitiestoclient_testeye, sv_writeentitiestoclient_testeye, testorigin, testorigin, SUPERCONTENTS_SOLID);
700 if (trace.fraction == 1 || BoxesOverlap(trace.endpos, trace.endpos, entmins, entmaxs))
701 sv_writeentitiestoclient_client->visibletime[s->number] = realtime + 1;
705 if (realtime > sv_writeentitiestoclient_client->visibletime[s->number])
707 sv_writeentitiestoclient_culled_trace++;
711 sv_writeentitiestoclient_visibleentities++;
714 // this just marks it for sending
715 // FIXME: it would be more efficient to send here, but the entity
716 // compressor isn't that flexible
717 sententities[s->number] = sententitiesmark;
720 entity_state_t sendstates[MAX_EDICTS];
722 void SV_WriteEntitiesToClient(client_t *client, edict_t *clent, sizebuf_t *msg)
724 int i, numsendstates;
727 // if there isn't enough space to accomplish anything, skip it
728 if (msg->cursize + 25 > msg->maxsize)
731 sv_writeentitiestoclient_client = client;
733 sv_writeentitiestoclient_culled_pvs = 0;
734 sv_writeentitiestoclient_culled_trace = 0;
735 sv_writeentitiestoclient_visibleentities = 0;
736 sv_writeentitiestoclient_totalentities = 0;
738 Mod_CheckLoaded(sv.worldmodel);
740 // find the client's PVS
741 // the real place being tested from
742 VectorAdd(clent->v->origin, clent->v->view_ofs, sv_writeentitiestoclient_testeye);
743 sv_writeentitiestoclient_pvsbytes = 0;
744 if (sv.worldmodel && sv.worldmodel->brush.FatPVS)
745 sv_writeentitiestoclient_pvsbytes = sv.worldmodel->brush.FatPVS(sv.worldmodel, sv_writeentitiestoclient_testeye, 8, sv_writeentitiestoclient_pvs, sizeof(sv_writeentitiestoclient_pvs));
747 sv_writeentitiestoclient_clentnum = EDICT_TO_PROG(clent); // LordHavoc: for comparison purposes
751 for (i = 0;i < numsendentities;i++)
752 SV_MarkWriteEntityStateToClient(sendentities + i);
755 for (i = 0;i < numsendentities;i++)
757 if (sententities[sendentities[i].number] == sententitiesmark)
759 s = &sendstates[numsendstates++];
760 *s = sendentities[i];
761 if (s->exteriormodelforclient && s->exteriormodelforclient == sv_writeentitiestoclient_clentnum)
762 s->flags |= RENDER_EXTERIORMODEL;
766 if (sv_cullentities_stats.integer)
767 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);
769 if (client->entitydatabase5)
770 EntityFrame5_WriteFrame(msg, client->entitydatabase5, numsendstates, sendstates, client - svs.clients + 1);
771 else if (client->entitydatabase4)
772 EntityFrame4_WriteFrame(msg, client->entitydatabase4, numsendstates, sendstates);
773 else if (client->entitydatabase)
774 EntityFrame_WriteFrame(msg, client->entitydatabase, numsendstates, sendstates, client - svs.clients + 1);
776 EntityFrameQuake_WriteFrame(msg, numsendstates, sendstates);
785 void SV_CleanupEnts (void)
790 ent = NEXT_EDICT(sv.edicts);
791 for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
792 ent->v->effects = (int)ent->v->effects & ~EF_MUZZLEFLASH;
797 SV_WriteClientdataToMessage
801 void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
812 // send a damage message
814 if (ent->v->dmg_take || ent->v->dmg_save)
816 other = PROG_TO_EDICT(ent->v->dmg_inflictor);
817 MSG_WriteByte (msg, svc_damage);
818 MSG_WriteByte (msg, ent->v->dmg_save);
819 MSG_WriteByte (msg, ent->v->dmg_take);
820 for (i=0 ; i<3 ; i++)
821 MSG_WriteCoord (msg, other->v->origin[i] + 0.5*(other->v->mins[i] + other->v->maxs[i]), sv.protocol);
823 ent->v->dmg_take = 0;
824 ent->v->dmg_save = 0;
828 // send the current viewpos offset from the view entity
830 SV_SetIdealPitch (); // how much to look up / down ideally
832 // a fixangle might get lost in a dropped packet. Oh well.
833 if ( ent->v->fixangle )
835 MSG_WriteByte (msg, svc_setangle);
836 for (i=0 ; i < 3 ; i++)
837 MSG_WriteAngle (msg, ent->v->angles[i], sv.protocol);
838 ent->v->fixangle = 0;
843 if (ent->v->view_ofs[2] != DEFAULT_VIEWHEIGHT)
844 bits |= SU_VIEWHEIGHT;
846 if (ent->v->idealpitch)
847 bits |= SU_IDEALPITCH;
849 // stuff the sigil bits into the high bits of items for sbar, or else
851 val = GETEDICTFIELDVALUE(ent, eval_items2);
854 items = (int)ent->v->items | ((int)val->_float << 23);
856 items = (int)ent->v->items | ((int)pr_global_struct->serverflags << 28);
860 if ( (int)ent->v->flags & FL_ONGROUND)
863 if ( ent->v->waterlevel >= 2)
866 // PROTOCOL_DARKPLACES
867 VectorClear(punchvector);
868 if ((val = GETEDICTFIELDVALUE(ent, eval_punchvector)))
869 VectorCopy(val->vector, punchvector);
872 if ((val = GETEDICTFIELDVALUE(ent, eval_viewzoom)))
874 i = val->_float * 255.0f;
878 i = bound(0, i, 65535);
882 // FIXME: which protocols support this? does PROTOCOL_DARKPLACES3 support viewzoom?
883 if (sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5)
887 for (i=0 ; i<3 ; i++)
889 if (ent->v->punchangle[i])
890 bits |= (SU_PUNCH1<<i);
891 if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5)
893 bits |= (SU_PUNCHVEC1<<i);
894 if (ent->v->velocity[i])
895 bits |= (SU_VELOCITY1<<i);
898 if (ent->v->weaponframe)
899 bits |= SU_WEAPONFRAME;
901 if (ent->v->armorvalue)
908 if (bits >= 16777216)
913 MSG_WriteByte (msg, svc_clientdata);
914 MSG_WriteShort (msg, bits);
915 if (bits & SU_EXTEND1)
916 MSG_WriteByte(msg, bits >> 16);
917 if (bits & SU_EXTEND2)
918 MSG_WriteByte(msg, bits >> 24);
920 if (bits & SU_VIEWHEIGHT)
921 MSG_WriteChar (msg, ent->v->view_ofs[2]);
923 if (bits & SU_IDEALPITCH)
924 MSG_WriteChar (msg, ent->v->idealpitch);
926 for (i=0 ; i<3 ; i++)
928 if (bits & (SU_PUNCH1<<i))
930 if (sv.protocol == PROTOCOL_QUAKE)
931 MSG_WriteChar(msg, ent->v->punchangle[i]);
932 else if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5)
933 MSG_WriteAngle16i(msg, ent->v->punchangle[i]);
935 if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4 || sv.protocol == PROTOCOL_DARKPLACES5)
937 if (bits & (SU_PUNCHVEC1<<i))
939 if (sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4)
940 MSG_WriteCoord16i(msg, punchvector[i]);
941 else if (sv.protocol == PROTOCOL_DARKPLACES5)
942 MSG_WriteCoord32f(msg, punchvector[i]);
945 if (bits & (SU_VELOCITY1<<i))
947 if (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3 || sv.protocol == PROTOCOL_DARKPLACES4)
948 MSG_WriteChar(msg, ent->v->velocity[i] * (1.0f / 16.0f));
949 else if (sv.protocol == PROTOCOL_DARKPLACES5)
950 MSG_WriteCoord32f(msg, ent->v->velocity[i]);
954 // [always sent] if (bits & SU_ITEMS)
955 MSG_WriteLong (msg, items);
957 if (sv.protocol == PROTOCOL_DARKPLACES5)
959 if (bits & SU_WEAPONFRAME)
960 MSG_WriteShort (msg, ent->v->weaponframe);
962 MSG_WriteShort (msg, ent->v->armorvalue);
963 if (bits & SU_WEAPON)
965 i = SV_ModelIndex(PR_GetString(ent->v->weaponmodel));
968 Con_DPrintf("weaponmodel \"%s\" not precached\n", PR_GetString(ent->v->weaponmodel));
971 MSG_WriteShort (msg, i);
974 MSG_WriteShort (msg, ent->v->health);
975 MSG_WriteShort (msg, ent->v->currentammo);
976 MSG_WriteShort (msg, ent->v->ammo_shells);
977 MSG_WriteShort (msg, ent->v->ammo_nails);
978 MSG_WriteShort (msg, ent->v->ammo_rockets);
979 MSG_WriteShort (msg, ent->v->ammo_cells);
981 MSG_WriteShort (msg, ent->v->weapon);
983 if (bits & SU_VIEWZOOM)
984 MSG_WriteShort (msg, viewzoom);
988 if (bits & SU_WEAPONFRAME)
989 MSG_WriteByte (msg, ent->v->weaponframe);
991 MSG_WriteByte (msg, ent->v->armorvalue);
992 if (bits & SU_WEAPON)
994 i = SV_ModelIndex(PR_GetString(ent->v->weaponmodel));
997 Con_DPrintf("weaponmodel \"%s\" not precached\n", PR_GetString(ent->v->weaponmodel));
1000 MSG_WriteByte (msg, i);
1003 MSG_WriteShort (msg, ent->v->health);
1004 MSG_WriteByte (msg, ent->v->currentammo);
1005 MSG_WriteByte (msg, ent->v->ammo_shells);
1006 MSG_WriteByte (msg, ent->v->ammo_nails);
1007 MSG_WriteByte (msg, ent->v->ammo_rockets);
1008 MSG_WriteByte (msg, ent->v->ammo_cells);
1010 if (gamemode == GAME_HIPNOTIC || gamemode == GAME_ROGUE || gamemode == GAME_NEXUIZ)
1014 if ( ((int)ent->v->weapon) & (1<<i) )
1016 MSG_WriteByte (msg, i);
1023 MSG_WriteByte (msg, ent->v->weapon);
1026 if (bits & SU_VIEWZOOM)
1028 if (sv.protocol == PROTOCOL_DARKPLACES4)
1030 viewzoom = min(viewzoom, 255);
1031 MSG_WriteByte (msg, viewzoom);
1033 else if (sv.protocol == PROTOCOL_DARKPLACES5)
1034 MSG_WriteShort (msg, viewzoom);
1040 =======================
1041 SV_SendClientDatagram
1042 =======================
1044 static qbyte sv_sendclientdatagram_buf[NET_MAXMESSAGE]; // FIXME?
1045 qboolean SV_SendClientDatagram (client_t *client)
1047 int rate, maxrate, maxsize, maxsize2;
1050 if (LHNETADDRESS_GetAddressType(&host_client->netconnection->peeraddress) == LHNETADDRESSTYPE_LOOP && !sv_ratelimitlocalplayer.integer)
1052 // for good singleplayer, send huge packets
1053 maxsize = sizeof(sv_sendclientdatagram_buf);
1054 maxsize2 = sizeof(sv_sendclientdatagram_buf);
1056 else if (sv.protocol == PROTOCOL_DARKPLACES5)
1058 // PROTOCOL_DARKPLACES5 supports packet size limiting of updates
1059 maxrate = bound(NET_MINRATE, sv_maxrate.integer, NET_MAXRATE);
1060 if (sv_maxrate.integer != maxrate)
1061 Cvar_SetValueQuick(&sv_maxrate, maxrate);
1063 rate = bound(NET_MINRATE, client->netconnection->rate, maxrate);
1064 rate = (int)(client->netconnection->rate * sys_ticrate.value);
1065 maxsize = bound(100, rate, 1400);
1070 // no rate limiting support on older protocols because dp protocols
1071 // 1-4 kick the client off if they overflow, and quake protocol shows
1072 // less than the full entity set if rate limited
1077 msg.data = sv_sendclientdatagram_buf;
1078 msg.maxsize = maxsize;
1081 MSG_WriteByte (&msg, svc_time);
1082 MSG_WriteFloat (&msg, sv.time);
1084 // add the client specific data to the datagram
1085 SV_WriteClientdataToMessage (client->edict, &msg);
1087 SV_WriteEntitiesToClient (client, client->edict, &msg);
1089 // expand packet size to allow effects to go over the rate limit
1090 // (dropping them is FAR too ugly)
1091 msg.maxsize = maxsize2;
1093 // copy the server datagram if there is space
1094 // FIXME: put in delayed queue of effects to send
1095 if (sv.datagram.cursize > 0 && msg.cursize + sv.datagram.cursize <= msg.maxsize)
1096 SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
1098 // send the datagram
1099 if (NetConn_SendUnreliableMessage (client->netconnection, &msg) == -1)
1101 SV_DropClient (true);// if the message couldn't send, kick off
1109 =======================
1110 SV_UpdateToReliableMessages
1111 =======================
1113 void SV_UpdateToReliableMessages (void)
1120 // check for changes to be sent over the reliable streams
1121 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1123 // update the host_client fields we care about according to the entity fields
1124 sv_player = EDICT_NUM(i+1);
1127 name = PR_GetString(sv_player->v->netname);
1130 // always point the string back at host_client->name to keep it safe
1131 strlcpy (host_client->name, name, sizeof (host_client->name));
1132 sv_player->v->netname = PR_SetString(host_client->name);
1133 if (strcmp(host_client->old_name, host_client->name))
1135 if (host_client->spawned)
1136 SV_BroadcastPrintf("%s changed name to %s\n", host_client->old_name, host_client->name);
1137 strcpy(host_client->old_name, host_client->name);
1138 // send notification to all clients
1139 MSG_WriteByte (&sv.reliable_datagram, svc_updatename);
1140 MSG_WriteByte (&sv.reliable_datagram, host_client->number);
1141 MSG_WriteString (&sv.reliable_datagram, host_client->name);
1144 // DP_SV_CLIENTCOLORS
1145 // this is always found (since it's added by the progs loader)
1146 if ((val = GETEDICTFIELDVALUE(sv_player, eval_clientcolors)))
1147 host_client->colors = (int)val->_float;
1148 if (host_client->old_colors != host_client->colors)
1150 host_client->old_colors = host_client->colors;
1151 // send notification to all clients
1152 MSG_WriteByte (&sv.reliable_datagram, svc_updatecolors);
1153 MSG_WriteByte (&sv.reliable_datagram, host_client->number);
1154 MSG_WriteByte (&sv.reliable_datagram, host_client->colors);
1158 host_client->frags = (int)sv_player->v->frags;
1159 if (host_client->old_frags != host_client->frags)
1161 host_client->old_frags = host_client->frags;
1162 // send notification to all clients
1163 MSG_WriteByte (&sv.reliable_datagram, svc_updatefrags);
1164 MSG_WriteByte (&sv.reliable_datagram, host_client->number);
1165 MSG_WriteShort (&sv.reliable_datagram, host_client->frags);
1169 for (j = 0, client = svs.clients;j < svs.maxclients;j++, client++)
1170 if (client->netconnection)
1171 SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
1173 SZ_Clear (&sv.reliable_datagram);
1178 =======================
1181 Send a nop message without trashing or sending the accumulated client
1183 =======================
1185 void SV_SendNop (client_t *client)
1191 msg.maxsize = sizeof(buf);
1194 MSG_WriteChar (&msg, svc_nop);
1196 if (NetConn_SendUnreliableMessage (client->netconnection, &msg) == -1)
1197 SV_DropClient (true); // if the message couldn't send, kick off
1198 client->last_message = realtime;
1202 =======================
1203 SV_SendClientMessages
1204 =======================
1206 void SV_SendClientMessages (void)
1208 int i, prepared = false;
1210 // update frags, names, etc
1211 SV_UpdateToReliableMessages();
1213 // build individual updates
1214 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1216 if (!host_client->active)
1218 if (!host_client->netconnection)
1220 SZ_Clear(&host_client->message);
1224 if (host_client->deadsocket || host_client->message.overflowed)
1226 SV_DropClient (true); // if the message couldn't send, kick off
1230 if (host_client->spawned)
1235 // only prepare entities once per frame
1236 SV_PrepareEntitiesForSending();
1238 if (!SV_SendClientDatagram (host_client))
1243 // the player isn't totally in the game yet
1244 // send small keepalive messages if too much time has passed
1245 // send a full message when the next signon stage has been requested
1246 // some other message data (name changes, etc) may accumulate
1247 // between signon stages
1248 if (!host_client->sendsignon)
1250 if (realtime - host_client->last_message > 5)
1251 SV_SendNop (host_client);
1252 continue; // don't send out non-signon messages
1256 if (host_client->message.cursize || host_client->dropasap)
1258 if (!NetConn_CanSendMessage (host_client->netconnection))
1261 if (host_client->dropasap)
1262 SV_DropClient (false); // went to another level
1265 if (NetConn_SendReliableMessage (host_client->netconnection, &host_client->message) == -1)
1266 SV_DropClient (true); // if the message couldn't send, kick off
1267 SZ_Clear (&host_client->message);
1268 host_client->last_message = realtime;
1269 host_client->sendsignon = false;
1274 // clear muzzle flashes
1280 ==============================================================================
1284 ==============================================================================
1293 int SV_ModelIndex (const char *name)
1297 if (!name || !name[0])
1300 for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
1301 if (!strcmp(sv.model_precache[i], name))
1303 if (i==MAX_MODELS || !sv.model_precache[i])
1305 Con_DPrintf ("SV_ModelIndex: model %s not precached", name);
1317 void SV_CreateBaseline (void)
1319 int i, entnum, large;
1322 // LordHavoc: clear *all* states (note just active ones)
1323 for (entnum = 0;entnum < sv.max_edicts;entnum++)
1325 // get the current server version
1326 svent = EDICT_NUM(entnum);
1328 // LordHavoc: always clear state values, whether the entity is in use or not
1329 svent->e->baseline = defaultstate;
1333 if (entnum > svs.maxclients && !svent->v->modelindex)
1336 // create entity baseline
1337 VectorCopy (svent->v->origin, svent->e->baseline.origin);
1338 VectorCopy (svent->v->angles, svent->e->baseline.angles);
1339 svent->e->baseline.frame = svent->v->frame;
1340 svent->e->baseline.skin = svent->v->skin;
1341 if (entnum > 0 && entnum <= svs.maxclients)
1343 svent->e->baseline.colormap = entnum;
1344 i = SV_ModelIndex("progs/player.mdl");
1347 svent->e->baseline.modelindex = i;
1351 svent->e->baseline.colormap = 0;
1352 svent->e->baseline.modelindex = svent->v->modelindex;
1356 if (svent->e->baseline.modelindex & 0xFF00 || svent->e->baseline.frame & 0xFF00)
1359 // add to the message
1361 MSG_WriteByte (&sv.signon, svc_spawnbaseline2);
1363 MSG_WriteByte (&sv.signon, svc_spawnbaseline);
1364 MSG_WriteShort (&sv.signon, entnum);
1368 MSG_WriteShort (&sv.signon, svent->e->baseline.modelindex);
1369 MSG_WriteShort (&sv.signon, svent->e->baseline.frame);
1373 MSG_WriteByte (&sv.signon, svent->e->baseline.modelindex);
1374 MSG_WriteByte (&sv.signon, svent->e->baseline.frame);
1376 MSG_WriteByte (&sv.signon, svent->e->baseline.colormap);
1377 MSG_WriteByte (&sv.signon, svent->e->baseline.skin);
1378 for (i=0 ; i<3 ; i++)
1380 MSG_WriteCoord(&sv.signon, svent->e->baseline.origin[i], sv.protocol);
1381 MSG_WriteAngle(&sv.signon, svent->e->baseline.angles[i], sv.protocol);
1391 Tell all the clients that the server is changing levels
1394 void SV_SendReconnect (void)
1401 msg.maxsize = sizeof(data);
1403 MSG_WriteChar (&msg, svc_stufftext);
1404 MSG_WriteString (&msg, "reconnect\n");
1405 NetConn_SendToAll (&msg, 5);
1407 if (cls.state != ca_dedicated)
1408 Cmd_ExecuteString ("reconnect\n", src_command);
1416 Grabs the current state of each client for saving across the
1417 transition to another level
1420 void SV_SaveSpawnparms (void)
1424 svs.serverflags = pr_global_struct->serverflags;
1426 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1428 if (!host_client->active)
1431 // call the progs to get default spawn parms for the new client
1432 pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1433 PR_ExecuteProgram (pr_global_struct->SetChangeParms, "QC function SetChangeParms is missing");
1434 for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1435 host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1439 void SV_IncreaseEdicts(void)
1443 int oldmax_edicts = sv.max_edicts;
1444 void *oldedictsengineprivate = sv.edictsengineprivate;
1445 void *oldedictsfields = sv.edictsfields;
1446 void *oldmoved_edicts = sv.moved_edicts;
1448 if (sv.max_edicts >= MAX_EDICTS)
1451 // links don't survive the transition, so unlink everything
1452 for (i = 0, ent = sv.edicts;i < sv.max_edicts;i++, ent++)
1455 SV_UnlinkEdict(sv.edicts + i);
1456 memset(&ent->e->areagrid, 0, sizeof(ent->e->areagrid));
1460 sv.max_edicts = min(sv.max_edicts + 256, MAX_EDICTS);
1461 sv.edictsengineprivate = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_engineprivate_t));
1462 sv.edictsfields = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1463 sv.moved_edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_t *));
1465 memcpy(sv.edictsengineprivate, oldedictsengineprivate, oldmax_edicts * sizeof(edict_engineprivate_t));
1466 memcpy(sv.edictsfields, oldedictsfields, oldmax_edicts * pr_edict_size);
1468 for (i = 0, ent = sv.edicts;i < sv.max_edicts;i++, ent++)
1470 ent->e = sv.edictsengineprivate + i;
1471 ent->v = (void *)((qbyte *)sv.edictsfields + i * pr_edict_size);
1472 // link every entity except world
1474 SV_LinkEdict(ent, false);
1477 Mem_Free(oldedictsengineprivate);
1478 Mem_Free(oldedictsfields);
1479 Mem_Free(oldmoved_edicts);
1486 This is called at the start of each level
1489 extern float scr_centertime_off;
1491 void SV_SpawnServer (const char *server)
1496 model_t *worldmodel;
1497 char modelname[sizeof(sv.modelname)];
1499 Con_DPrintf("SpawnServer: %s\n", server);
1501 snprintf (modelname, sizeof(modelname), "maps/%s.bsp", server);
1502 worldmodel = Mod_ForName(modelname, false, true, true);
1503 if (!worldmodel || !worldmodel->TraceBox)
1505 Con_Printf("Couldn't load map %s\n", modelname);
1509 // let's not have any servers with no name
1510 if (hostname.string[0] == 0)
1511 Cvar_Set ("hostname", "UNNAMED");
1512 scr_centertime_off = 0;
1514 svs.changelevel_issued = false; // now safe to issue another
1517 // tell all connected clients that we are going to a new level
1523 // make sure cvars have been checked before opening the ports
1524 NetConn_ServerFrame();
1525 NetConn_OpenServerPorts(true);
1529 // make cvars consistant
1532 Cvar_SetValue ("deathmatch", 0);
1533 current_skill = bound(0, (int)(skill.value + 0.5), 3);
1535 Cvar_SetValue ("skill", (float)current_skill);
1538 // set up the new server
1540 Host_ClearMemory ();
1542 memset (&sv, 0, sizeof(sv));
1544 strlcpy (sv.name, server, sizeof (sv.name));
1547 if (!strcasecmp(sv_protocolname.string, "QUAKE"))
1549 sv.protocol = PROTOCOL_QUAKE;
1550 sv.netquakecompatible = true;
1552 else if (!strcasecmp(sv_protocolname.string, "QUAKEDP"))
1554 sv.protocol = PROTOCOL_QUAKE;
1555 sv.netquakecompatible = false;
1557 else if (!strcasecmp(sv_protocolname.string, "DARKPLACES1"))
1559 sv.protocol = PROTOCOL_DARKPLACES1;
1560 sv.netquakecompatible = false;
1562 else if (!strcasecmp(sv_protocolname.string, "DARKPLACES2"))
1564 sv.protocol = PROTOCOL_DARKPLACES2;
1565 sv.netquakecompatible = false;
1567 else if (!strcasecmp(sv_protocolname.string, "DARKPLACES3"))
1569 sv.protocol = PROTOCOL_DARKPLACES3;
1570 sv.netquakecompatible = false;
1572 else if (!strcasecmp(sv_protocolname.string, "DARKPLACES4"))
1574 sv.protocol = PROTOCOL_DARKPLACES4;
1575 sv.netquakecompatible = false;
1577 else if (!strcasecmp(sv_protocolname.string, "DARKPLACES5"))
1579 sv.protocol = PROTOCOL_DARKPLACES5;
1580 sv.netquakecompatible = false;
1584 sv.protocol = PROTOCOL_DARKPLACES5;
1585 sv.netquakecompatible = false;
1586 Con_Printf("Unknown sv_protocolname \"%s\", valid values are QUAKE, QUAKEDP, DARKPLACES1, DARKPLACES2, DARKPLACES3, DARKPLACES4, DARKPLACES5, falling back to DARKPLACES5 protocol\n", sv_protocolname.string);
1589 // load progs to get entity field count
1592 // allocate server memory
1593 // start out with just enough room for clients and a reasonable estimate of entities
1594 sv.max_edicts = max(svs.maxclients + 1, 512);
1595 sv.max_edicts = min(sv.max_edicts, MAX_EDICTS);
1597 // clear the edict memory pool
1598 Mem_EmptyPool(sv_edicts_mempool);
1599 // edict_t structures (hidden from progs)
1600 sv.edicts = Mem_Alloc(sv_edicts_mempool, MAX_EDICTS * sizeof(edict_t));
1601 // engine private structures (hidden from progs)
1602 sv.edictsengineprivate = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_engineprivate_t));
1603 // progs fields, often accessed by server
1604 sv.edictsfields = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * pr_edict_size);
1605 // used by PushMove to move back pushed entities
1606 sv.moved_edicts = Mem_Alloc(sv_edicts_mempool, sv.max_edicts * sizeof(edict_t *));
1607 for (i = 0;i < sv.max_edicts;i++)
1609 ent = sv.edicts + i;
1610 ent->e = sv.edictsengineprivate + i;
1611 ent->v = (void *)((qbyte *)sv.edictsfields + i * pr_edict_size);
1614 sv.datagram.maxsize = sizeof(sv.datagram_buf);
1615 sv.datagram.cursize = 0;
1616 sv.datagram.data = sv.datagram_buf;
1618 sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1619 sv.reliable_datagram.cursize = 0;
1620 sv.reliable_datagram.data = sv.reliable_datagram_buf;
1622 sv.signon.maxsize = sizeof(sv.signon_buf);
1623 sv.signon.cursize = 0;
1624 sv.signon.data = sv.signon_buf;
1626 // leave slots at start for clients only
1627 sv.num_edicts = svs.maxclients+1;
1629 sv.state = ss_loading;
1635 worldmodel->used = true;
1637 strlcpy (sv.name, server, sizeof (sv.name));
1638 strcpy(sv.modelname, modelname);
1639 sv.worldmodel = worldmodel;
1640 sv.models[1] = sv.worldmodel;
1643 // clear world interaction links
1647 sv.sound_precache[0] = "";
1649 sv.model_precache[0] = "";
1650 sv.model_precache[1] = sv.modelname;
1651 for (i = 1;i < sv.worldmodel->brush.numsubmodels;i++)
1653 sv.model_precache[i+1] = localmodels[i];
1654 sv.models[i+1] = Mod_ForName (localmodels[i], false, false, false);
1658 // load the rest of the entities
1661 memset (ent->v, 0, progs->entityfields * 4);
1662 ent->e->free = false;
1663 ent->v->model = PR_SetString(sv.modelname);
1664 ent->v->modelindex = 1; // world model
1665 ent->v->solid = SOLID_BSP;
1666 ent->v->movetype = MOVETYPE_PUSH;
1669 pr_global_struct->coop = coop.integer;
1671 pr_global_struct->deathmatch = deathmatch.integer;
1673 pr_global_struct->mapname = PR_SetString(sv.name);
1675 // serverflags are for cross level information (sigils)
1676 pr_global_struct->serverflags = svs.serverflags;
1678 // load replacement entity file if found
1680 if (sv_entpatch.integer)
1681 entities = FS_LoadFile(va("maps/%s.ent", sv.name), tempmempool, true);
1684 Con_Printf("Loaded maps/%s.ent\n", sv.name);
1685 ED_LoadFromFile (entities);
1689 ED_LoadFromFile (sv.worldmodel->brush.entities);
1692 // LordHavoc: clear world angles (to fix e3m3.bsp)
1693 VectorClear(sv.edicts->v->angles);
1697 // all setup is completed, any further precache statements are errors
1698 sv.state = ss_active;
1700 // run two frames to allow everything to settle
1701 for (i = 0;i < 2;i++)
1703 sv.frametime = pr_global_struct->frametime = host_frametime = 0.1;
1709 // create a baseline for more efficient communications
1710 if (sv.protocol == PROTOCOL_QUAKE)
1711 SV_CreateBaseline ();
1713 // send serverinfo to all connected clients
1714 for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
1715 if (host_client->netconnection)
1716 SV_SendServerinfo(host_client);
1718 Con_DPrint("Server spawned.\n");
1719 NetConn_Heartbeat (2);