1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3 
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.
8 
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.
12 
13 See the GNU General Public License for more details.
14 
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.
18 
19 */
20 // sv_main.c -- server main program
21 
22 #include "quakedef.h"
23 
24 server_t		sv;
25 server_static_t	svs;
26 
27 char	localmodels[MAX_MODELS][5];			// inline model names for precache
28 
29 //============================================================================
30 
31 /*
32 ===============
33 SV_Init
34 ===============
35 */
SV_Init(void)36 void SV_Init (void)
37 {
38 	int		i;
39 	extern	cvar_t	sv_maxvelocity;
40 	extern	cvar_t	sv_gravity;
41 	extern	cvar_t	sv_nostep;
42 	extern	cvar_t	sv_friction;
43 	extern	cvar_t	sv_edgefriction;
44 	extern	cvar_t	sv_stopspeed;
45 	extern	cvar_t	sv_maxspeed;
46 	extern	cvar_t	sv_accelerate;
47 	extern	cvar_t	sv_idealpitchscale;
48 	extern	cvar_t	sv_aim;
49 
50 	Cvar_RegisterVariable (&sv_maxvelocity);
51 	Cvar_RegisterVariable (&sv_gravity);
52 	Cvar_RegisterVariable (&sv_friction);
53 	Cvar_RegisterVariable (&sv_edgefriction);
54 	Cvar_RegisterVariable (&sv_stopspeed);
55 	Cvar_RegisterVariable (&sv_maxspeed);
56 	Cvar_RegisterVariable (&sv_accelerate);
57 	Cvar_RegisterVariable (&sv_idealpitchscale);
58 	Cvar_RegisterVariable (&sv_aim);
59 	Cvar_RegisterVariable (&sv_nostep);
60 
61 	for (i=0 ; i<MAX_MODELS ; i++)
62 		sprintf (localmodels[i], "*%i", i);
63 }
64 
65 /*
66 =============================================================================
67 
68 EVENT MESSAGES
69 
70 =============================================================================
71 */
72 
73 /*
74 ==================
75 SV_StartParticle
76 
77 Make sure the event gets sent to all clients
78 ==================
79 */
SV_StartParticle(vec3_t org,vec3_t dir,int color,int count)80 void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
81 {
82 	int		i, v;
83 
84 	if (sv.datagram.cursize > MAX_DATAGRAM-16)
85 		return;
86 	MSG_WriteByte (&sv.datagram, svc_particle);
87 	MSG_WriteCoord (&sv.datagram, org[0]);
88 	MSG_WriteCoord (&sv.datagram, org[1]);
89 	MSG_WriteCoord (&sv.datagram, org[2]);
90 	for (i=0 ; i<3 ; i++)
91 	{
92 		v = dir[i]*16;
93 		if (v > 127)
94 			v = 127;
95 		else if (v < -128)
96 			v = -128;
97 		MSG_WriteChar (&sv.datagram, v);
98 	}
99 	MSG_WriteByte (&sv.datagram, count);
100 	MSG_WriteByte (&sv.datagram, color);
101 }
102 
103 /*
104 ==================
105 SV_StartSound
106 
107 Each entity can have eight independant sound sources, like voice,
108 weapon, feet, etc.
109 
110 Channel 0 is an auto-allocate channel, the others override anything
111 allready running on that entity/channel pair.
112 
113 An attenuation of 0 will play full volume everywhere in the level.
114 Larger attenuations will drop off.  (max 4 attenuation)
115 
116 ==================
117 */
SV_StartSound(edict_t * entity,int channel,char * sample,int volume,float attenuation)118 void SV_StartSound (edict_t *entity, int channel, char *sample, int volume,
119     float attenuation)
120 {
121     int         sound_num;
122     int field_mask;
123     int			i;
124 	int			ent;
125 
126 	if (volume < 0 || volume > 255)
127 		Sys_Error ("SV_StartSound: volume = %i", volume);
128 
129 	if (attenuation < 0 || attenuation > 4)
130 		Sys_Error ("SV_StartSound: attenuation = %f", attenuation);
131 
132 	if (channel < 0 || channel > 7)
133 		Sys_Error ("SV_StartSound: channel = %i", channel);
134 
135 	if (sv.datagram.cursize > MAX_DATAGRAM-16)
136 		return;
137 
138 // find precache number for sound
139     for (sound_num=1 ; sound_num<MAX_SOUNDS
140         && sv.sound_precache[sound_num] ; sound_num++)
141         if (!strcmp(sample, sv.sound_precache[sound_num]))
142             break;
143 
144     if ( sound_num == MAX_SOUNDS || !sv.sound_precache[sound_num] )
145     {
146         Con_Printf ("SV_StartSound: %s not precacheed\n", sample);
147         return;
148     }
149 
150 	ent = NUM_FOR_EDICT(entity);
151 
152 	channel = (ent<<3) | channel;
153 
154 	field_mask = 0;
155 	if (volume != DEFAULT_SOUND_PACKET_VOLUME)
156 		field_mask |= SND_VOLUME;
157 	if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
158 		field_mask |= SND_ATTENUATION;
159 
160 // directed messages go only to the entity the are targeted on
161 	MSG_WriteByte (&sv.datagram, svc_sound);
162 	MSG_WriteByte (&sv.datagram, field_mask);
163 	if (field_mask & SND_VOLUME)
164 		MSG_WriteByte (&sv.datagram, volume);
165 	if (field_mask & SND_ATTENUATION)
166 		MSG_WriteByte (&sv.datagram, attenuation*64);
167 	MSG_WriteShort (&sv.datagram, channel);
168 	MSG_WriteByte (&sv.datagram, sound_num);
169 	for (i=0 ; i<3 ; i++)
170 		MSG_WriteCoord (&sv.datagram, entity->v.origin[i]+0.5*(entity->v.mins[i]+entity->v.maxs[i]));
171 }
172 
173 /*
174 ==============================================================================
175 
176 CLIENT SPAWNING
177 
178 ==============================================================================
179 */
180 
181 /*
182 ================
183 SV_SendServerinfo
184 
185 Sends the first message from the server to a connected client.
186 This will be sent on the initial connection and upon each server load.
187 ================
188 */
SV_SendServerinfo(client_t * client)189 void SV_SendServerinfo (client_t *client)
190 {
191 	char			**s;
192 	char			message[2048];
193 
194 	MSG_WriteByte (&client->message, svc_print);
195 	sprintf (message, "%c\nVERSION %4.2f SERVER (%i CRC)", 2, VERSION, pr_crc);
196 	MSG_WriteString (&client->message,message);
197 
198 	MSG_WriteByte (&client->message, svc_serverinfo);
199 	MSG_WriteLong (&client->message, TENEBRAE_PROTOCOL_VERSION);
200 	MSG_WriteByte (&client->message, svs.maxclients);
201 
202 	if (!coop.value && deathmatch.value)
203 		MSG_WriteByte (&client->message, GAME_DEATHMATCH);
204 	else
205 		MSG_WriteByte (&client->message, GAME_COOP);
206 
207 	sprintf (message, pr_strings+sv.edicts->v.message);
208 
209 	MSG_WriteString (&client->message,message);
210 
211 	for (s = sv.model_precache+1 ; *s ; s++)
212 		MSG_WriteString (&client->message, *s);
213 	MSG_WriteByte (&client->message, 0);
214 
215 	for (s = sv.sound_precache+1 ; *s ; s++)
216 		MSG_WriteString (&client->message, *s);
217 	MSG_WriteByte (&client->message, 0);
218 
219 // send music
220 	MSG_WriteByte (&client->message, svc_cdtrack);
221 	MSG_WriteByte (&client->message, sv.edicts->v.sounds);
222 	MSG_WriteByte (&client->message, sv.edicts->v.sounds);
223 
224 // set view
225 	MSG_WriteByte (&client->message, svc_setview);
226 	MSG_WriteShort (&client->message, NUM_FOR_EDICT(client->edict));
227 
228 	MSG_WriteByte (&client->message, svc_signonnum);
229 	MSG_WriteByte (&client->message, 1);
230 
231 	client->sendsignon = true;
232 	client->spawned = false;		// need prespawn, spawn, etc
233 }
234 
235 /*
236 ================
237 SV_ConnectClient
238 
239 Initializes a client_t for a new net connection.  This will only be called
240 once for a player each game, not once for each level change.
241 ================
242 */
SV_ConnectClient(int clientnum)243 void SV_ConnectClient (int clientnum)
244 {
245 	edict_t			*ent;
246 	client_t		*client;
247 	int				edictnum;
248 	struct qsocket_s *netconnection;
249 	int				i;
250 	float			spawn_parms[NUM_SPAWN_PARMS];
251 
252 	client = svs.clients + clientnum;
253 
254 	Con_DPrintf ("Client %s connected\n", client->netconnection->address);
255 
256 	edictnum = clientnum+1;
257 
258 	ent = EDICT_NUM(edictnum);
259 
260 // set up the client_t
261 	netconnection = client->netconnection;
262 
263 	if (sv.loadgame)
264 		memcpy (spawn_parms, client->spawn_parms, sizeof(spawn_parms));
265 	memset (client, 0, sizeof(*client));
266 	client->netconnection = netconnection;
267 
268 	strcpy (client->name, "unconnected");
269 	client->active = true;
270 	client->spawned = false;
271 	client->edict = ent;
272 	client->message.data = client->msgbuf;
273 	client->message.maxsize = sizeof(client->msgbuf);
274 	client->message.allowoverflow = true;		// we can catch it
275 
276 #ifdef IDGODS
277 	client->privileged = IsID(&client->netconnection->addr);
278 #else
279 	client->privileged = false;
280 #endif
281 
282 	if (sv.loadgame)
283 		memcpy (client->spawn_parms, spawn_parms, sizeof(spawn_parms));
284 	else
285 	{
286 	// call the progs to get default spawn parms for the new client
287 		PR_ExecuteProgram (pr_global_struct->SetNewParms);
288 		for (i=0 ; i<NUM_SPAWN_PARMS ; i++)
289 			client->spawn_parms[i] = (&pr_global_struct->parm1)[i];
290 	}
291 
292 	SV_SendServerinfo (client);
293 }
294 
295 
296 /*
297 ===================
298 SV_CheckForNewClients
299 
300 ===================
301 */
SV_CheckForNewClients(void)302 void SV_CheckForNewClients (void)
303 {
304 	struct qsocket_s	*ret;
305 	int				i;
306 
307 //
308 // check for new connections
309 //
310 	while (1)
311 	{
312 		ret = NET_CheckNewConnections ();
313 		if (!ret)
314 			break;
315 
316 	//
317 	// init a new client structure
318 	//
319 		for (i=0 ; i<svs.maxclients ; i++)
320 			if (!svs.clients[i].active)
321 				break;
322 		if (i == svs.maxclients)
323 			Sys_Error ("Host_CheckForNewClients: no free clients");
324 
325 		svs.clients[i].netconnection = ret;
326 		SV_ConnectClient (i);
327 
328 		net_activeconnections++;
329 	}
330 }
331 
332 
333 
334 /*
335 ===============================================================================
336 
337 FRAME UPDATES
338 
339 ===============================================================================
340 */
341 
342 /*
343 ==================
344 SV_ClearDatagram
345 
346 ==================
347 */
SV_ClearDatagram(void)348 void SV_ClearDatagram (void)
349 {
350 	SZ_Clear (&sv.datagram);
351 }
352 
353 /*
354 =============================================================================
355 
356 The PVS must include a small area around the client to allow head bobbing
357 or other small motion on the client side.  Otherwise, a bob might cause an
358 entity that should be visible to not show up, especially when the bob
359 crosses a waterline.
360 
361 =============================================================================
362 */
363 
364 int		fatbytes;
365 byte	fatpvs[MAX_MAP_LEAFS/8];
366 
SV_AddToFatPVS(vec3_t org,mnode_t * node)367 void SV_AddToFatPVS (vec3_t org, mnode_t *node)
368 {
369 	int		i;
370 	byte	*pvs;
371 	mplane_t	*plane;
372 	float	d;
373 
374 	while (1)
375 	{
376 	// if this is a leaf, accumulate the pvs bits
377 		if (node->contents < 0)
378 		{
379 			if (node->contents != CONTENTS_SOLID)
380 			{
381 				pvs = Mod_LeafPVS ( (mleaf_t *)node, sv.worldmodel);
382 				for (i=0 ; i<fatbytes ; i++)
383 					fatpvs[i] |= pvs[i];
384 			}
385 			return;
386 		}
387 
388 		plane = node->plane;
389 		d = DotProduct (org, plane->normal) - plane->dist;
390 		if (d > 8)
391 			node = node->children[0];
392 		else if (d < -8)
393 			node = node->children[1];
394 		else
395 		{	// go down both
396 			SV_AddToFatPVS (org, node->children[0]);
397 			node = node->children[1];
398 		}
399 	}
400 }
401 
402 /*
403 =============
404 SV_FatPVS
405 
406 Calculates a PVS that is the inclusive or of all leafs within 8 pixels of the
407 given point.
408 =============
409 */
SV_FatPVS(vec3_t org)410 byte *SV_FatPVS (vec3_t org)
411 {
412 	fatbytes = (sv.worldmodel->numleafs+31)>>3;
413 	Q_memset (fatpvs, 0, fatbytes);
414 	SV_AddToFatPVS (org, sv.worldmodel->nodes);
415 	return fatpvs;
416 }
417 
418 //=============================================================================
419 
420 
421 /*
422 =============
423 SV_WriteEntitiesToClient
424 
425 =============
426 */
SV_WriteEntitiesToClient(edict_t * clent,sizebuf_t * msg)427 void SV_WriteEntitiesToClient (edict_t	*clent, sizebuf_t *msg)
428 {
429 	int		e, i;
430 	int		bits;
431 	byte	*pvs;
432 	vec3_t	org;
433 	float	miss;
434 	edict_t	*ent;
435 
436 // find the client's PVS
437 	VectorAdd (clent->v.origin, clent->v.view_ofs, org);
438 	pvs = SV_FatPVS (org);
439 
440 // send over all entities (excpet the client) that touch the pvs
441 	ent = NEXT_EDICT(sv.edicts);
442 	for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
443 	{
444 #ifdef QUAKE2
445 		// don't send if flagged for NODRAW and there are no lighting effects
446 		if (ent->v.effects == EF_NODRAW)
447 			continue;
448 #endif
449 
450 // ignore if not touching a PV leaf
451 		if (ent != clent)	// clent is ALLWAYS sent
452 		{
453 // ignore ents without visible models
454 			if (!ent->v.modelindex || !pr_strings[ent->v.model])
455 				continue;
456 
457 			for (i=0 ; i < ent->num_leafs ; i++)
458 				if (pvs[ent->leafnums[i] >> 3] & (1 << (ent->leafnums[i]&7) ))
459 					break;
460 
461 			if (i == ent->num_leafs)
462 				continue;		// not visible
463 		}
464 
465 		if (msg->maxsize - msg->cursize < 26) //Penta => more fields now
466 		{
467 			Con_Printf ("packet overflow\n");
468 			return;
469 		}
470 
471 // send an update
472 		bits = 0;
473 
474 		for (i=0 ; i<3 ; i++)
475 		{
476 			miss = ent->v.origin[i] - ent->baseline.origin[i];
477 			if ( miss < -0.1 || miss > 0.1 )
478 				bits |= U_ORIGIN1<<i;
479 		}
480 
481 		if ( ent->v.angles[0] != ent->baseline.angles[0] )
482 			bits |= U_ANGLE1;
483 
484 		if ( ent->v.angles[1] != ent->baseline.angles[1] )
485 			bits |= U_ANGLE2;
486 
487 		if ( ent->v.angles[2] != ent->baseline.angles[2] )
488 			bits |= U_ANGLE3;
489 
490 		if (ent->v.movetype == MOVETYPE_STEP)
491 			bits |= U_NOLERP;	// don't mess up the step animation
492 
493 		if (ent->baseline.colormap != ent->v.colormap)
494 			bits |= U_COLORMAP;
495 
496 		if (ent->baseline.skin != ent->v.skin)
497 			bits |= U_SKIN;
498 
499 		if (ent->baseline.frame != ent->v.frame)
500 			bits |= U_FRAME;
501 
502 		if (ent->baseline.effects != ent->v.effects)
503 			bits |= U_EFFECTS;
504 
505 		if (ent->baseline.modelindex != ent->v.modelindex) {
506 			bits |= U_MODEL;
507 		}
508 
509 		//PENTA: more baseline fields
510 		if (ent->baseline.alpha != HACKY_GETFIELD(ent, eval_alpha, float, 0)) {
511 			bits |= U_ALPHA;
512 		}
513 
514 		if (ent->baseline.style != HACKY_GETFIELD(ent, eval_style, float, 0)) {
515 			bits |= U_STYLE;
516 		}
517 
518 		if (ent->baseline.light_lev != HACKY_GETFIELD(ent, eval_light_lev, float, 300)) {
519 			bits |= U_LIGHTLEV;
520 		}
521 
522 		if ((ent->baseline.color[0] != HACKY_GETFIELD(ent, eval_color, vec3_t, vec3_origin)[0]) ||
523 			(ent->baseline.color[1] != HACKY_GETFIELD(ent, eval_color, vec3_t, vec3_origin)[1]) ||
524 			(ent->baseline.color[2] != HACKY_GETFIELD(ent, eval_color, vec3_t, vec3_origin)[2]))
525 		{
526 			bits |= U_COLOR;
527 		}
528 
529 		if (ent->baseline.pflags != HACKY_GETFIELD(ent, eval_pflags, float, 0)) {
530 			bits |= U_PFLAGS;
531 		}
532 
533 
534 		if (e >= 256)
535 			bits |= U_LONGENTITY;
536 
537 		if (bits >= 256)
538 			bits |= U_MOREBITS;
539 
540 		if (bits >= 65536)
541 			bits |= U_TENEBRAEBITS;
542 
543 	//
544 	// write the message
545 	//
546 		MSG_WriteByte (msg,bits | U_SIGNAL);
547 
548 		if (bits & U_MOREBITS)
549 			MSG_WriteByte (msg, bits>>8);
550 
551 		if (bits & U_TENEBRAEBITS)
552 			MSG_WriteShort (msg, bits>>16);
553 
554 		if (bits & U_LONGENTITY)
555 			MSG_WriteShort (msg,e);
556 		else
557 			MSG_WriteByte (msg,e);
558 
559 		if (bits & U_MODEL)
560 			MSG_WriteByte (msg,	ent->v.modelindex);
561 		if (bits & U_FRAME)
562 			MSG_WriteByte (msg, ent->v.frame);
563 		if (bits & U_COLORMAP)
564 			MSG_WriteByte (msg, ent->v.colormap);
565 		if (bits & U_SKIN)
566 			MSG_WriteByte (msg, ent->v.skin);
567 		if (bits & U_EFFECTS)
568 			MSG_WriteByte (msg, ent->v.effects);
569 		if (bits & U_ORIGIN1)
570 			MSG_WriteCoord (msg, ent->v.origin[0]);
571 		if (bits & U_ANGLE1)
572 			MSG_WriteAngle(msg, ent->v.angles[0]);
573 		if (bits & U_ORIGIN2)
574 			MSG_WriteCoord (msg, ent->v.origin[1]);
575 		if (bits & U_ANGLE2)
576 			MSG_WriteAngle(msg, ent->v.angles[1]);
577 		if (bits & U_ORIGIN3)
578 			MSG_WriteCoord (msg, ent->v.origin[2]);
579 		if (bits & U_ANGLE3)
580 			MSG_WriteAngle(msg, ent->v.angles[2]);
581 
582 		//PENTA: new baseline fields (Fixed!)
583 		if (bits & U_COLOR) {
584 			for (i=0 ; i<3 ; i++)
585 			{
586 				MSG_WriteByte(msg, (int)(HACKY_GETFIELD(ent, eval_color, vec3_t, vec3_origin)[i]*255));
587 			}
588 		}
589 
590 		if (bits & U_ALPHA)
591 			MSG_WriteByte (msg, (int)(HACKY_GETFIELD(ent, eval_alpha, float, 0)*255));
592 
593 		if (bits & U_LIGHTLEV)
594 			MSG_WriteShort (msg, HACKY_GETFIELD(ent, eval_light_lev, float, 300));
595 
596 		if (bits & U_STYLE)
597 			MSG_WriteByte (msg, HACKY_GETFIELD(ent, eval_style, float, 0));
598 
599 		if (bits & U_PFLAGS)
600 			MSG_WriteByte (msg, HACKY_GETFIELD(ent, eval_pflags, float, 0));
601 
602 	}
603 }
604 
605 /*
606 =============
607 SV_CleanupEnts
608 
609 =============
610 */
SV_CleanupEnts(void)611 void SV_CleanupEnts (void)
612 {
613 	int		e;
614 	edict_t	*ent;
615 
616 	ent = NEXT_EDICT(sv.edicts);
617 	for (e=1 ; e<sv.num_edicts ; e++, ent = NEXT_EDICT(ent))
618 	{
619 		ent->v.effects = (int)ent->v.effects & ~EF_MUZZLEFLASH;
620 	}
621 
622 }
623 
624 /*
625 ==================
626 SV_WriteClientdataToMessage
627 
628 ==================
629 */
SV_WriteClientdataToMessage(edict_t * ent,sizebuf_t * msg)630 void SV_WriteClientdataToMessage (edict_t *ent, sizebuf_t *msg)
631 {
632 	int		bits;
633 	int		i;
634 	edict_t	*other;
635 	int		items;
636 #ifndef QUAKE2
637 	eval_t	*val;
638 #endif
639 
640 //
641 // send a damage message
642 //
643 	if (ent->v.dmg_take || ent->v.dmg_save)
644 	{
645 		other = PROG_TO_EDICT(ent->v.dmg_inflictor);
646 		MSG_WriteByte (msg, svc_damage);
647 		MSG_WriteByte (msg, ent->v.dmg_save);
648 		MSG_WriteByte (msg, ent->v.dmg_take);
649 		for (i=0 ; i<3 ; i++)
650 			MSG_WriteCoord (msg, other->v.origin[i] + 0.5*(other->v.mins[i] + other->v.maxs[i]));
651 
652 		ent->v.dmg_take = 0;
653 		ent->v.dmg_save = 0;
654 	}
655 
656 //
657 // send the current viewpos offset from the view entity
658 //
659 	SV_SetIdealPitch ();		// how much to look up / down ideally
660 
661 // a fixangle might get lost in a dropped packet.  Oh well.
662 	if ( ent->v.fixangle )
663 	{
664 		MSG_WriteByte (msg, svc_setangle);
665 		for (i=0 ; i < 3 ; i++)
666 			MSG_WriteAngle (msg, ent->v.angles[i] );
667 		ent->v.fixangle = 0;
668 	}
669 
670 	bits = 0;
671 
672 	if (ent->v.view_ofs[2] != DEFAULT_VIEWHEIGHT)
673 		bits |= SU_VIEWHEIGHT;
674 
675 	if (ent->v.idealpitch)
676 		bits |= SU_IDEALPITCH;
677 
678 // stuff the sigil bits into the high bits of items for sbar, or else
679 // mix in items2
680 #ifdef QUAKE2
681 	items = (int)ent->v.items | ((int)ent->v.items2 << 23);
682 #else
683 	val = GetEdictFieldValue(ent, "items2");
684 
685 	if (val)
686 		items = (int)ent->v.items | ((int)val->_float << 23);
687 	else
688 		items = (int)ent->v.items | ((int)pr_global_struct->serverflags << 28);
689 #endif
690 
691 	bits |= SU_ITEMS;
692 
693 	if ( (int)ent->v.flags & FL_ONGROUND)
694 		bits |= SU_ONGROUND;
695 
696 	if ( ent->v.waterlevel >= 2)
697 		bits |= SU_INWATER;
698 
699 	for (i=0 ; i<3 ; i++)
700 	{
701 		if (ent->v.punchangle[i])
702 			bits |= (SU_PUNCH1<<i);
703 		if (ent->v.velocity[i])
704 			bits |= (SU_VELOCITY1<<i);
705 	}
706 
707 	if (ent->v.weaponframe)
708 		bits |= SU_WEAPONFRAME;
709 
710 	if (ent->v.armorvalue)
711 		bits |= SU_ARMOR;
712 
713 //	if (ent->v.weapon)
714 		bits |= SU_WEAPON;
715 
716 // send the data
717 
718 	MSG_WriteByte (msg, svc_clientdata);
719 	MSG_WriteShort (msg, bits);
720 
721 	if (bits & SU_VIEWHEIGHT)
722 		MSG_WriteChar (msg, ent->v.view_ofs[2]);
723 
724 	if (bits & SU_IDEALPITCH)
725 		MSG_WriteChar (msg, ent->v.idealpitch);
726 
727 	for (i=0 ; i<3 ; i++)
728 	{
729 		if (bits & (SU_PUNCH1<<i))
730 			MSG_WriteChar (msg, ent->v.punchangle[i]);
731 		if (bits & (SU_VELOCITY1<<i))
732 			MSG_WriteChar (msg, ent->v.velocity[i]/16);
733 	}
734 
735 // [always sent]	if (bits & SU_ITEMS)
736 	MSG_WriteLong (msg, items);
737 
738 	if (bits & SU_WEAPONFRAME)
739 		MSG_WriteByte (msg, ent->v.weaponframe);
740 	if (bits & SU_ARMOR)
741 		MSG_WriteByte (msg, ent->v.armorvalue);
742 	if (bits & SU_WEAPON)
743 		MSG_WriteByte (msg, SV_ModelIndex(pr_strings+ent->v.weaponmodel));
744 
745 	MSG_WriteShort (msg, ent->v.health);
746 	MSG_WriteByte (msg, ent->v.currentammo);
747 	MSG_WriteByte (msg, ent->v.ammo_shells);
748 	MSG_WriteByte (msg, ent->v.ammo_nails);
749 	MSG_WriteByte (msg, ent->v.ammo_rockets);
750 	MSG_WriteByte (msg, ent->v.ammo_cells);
751 
752 	if (standard_quake)
753 	{
754 		MSG_WriteByte (msg, ent->v.weapon);
755 	}
756 	else
757 	{
758 		for(i=0;i<32;i++)
759 		{
760 			if ( ((int)ent->v.weapon) & (1<<i) )
761 			{
762 				MSG_WriteByte (msg, i);
763 				break;
764 			}
765 		}
766 	}
767 }
768 
769 /*
770 =======================
771 SV_SendClientDatagram
772 =======================
773 */
SV_SendClientDatagram(client_t * client)774 qboolean SV_SendClientDatagram (client_t *client)
775 {
776 	byte		buf[MAX_DATAGRAM];
777 	sizebuf_t	msg;
778 
779 	msg.data = buf;
780 	msg.maxsize = sizeof(buf);
781 	msg.cursize = 0;
782 
783 	MSG_WriteByte (&msg, svc_time);
784 	MSG_WriteFloat (&msg, sv.time);
785 
786 // add the client specific data to the datagram
787 	SV_WriteClientdataToMessage (client->edict, &msg);
788 
789 	SV_WriteEntitiesToClient (client->edict, &msg);
790 
791 // copy the server datagram if there is space
792 	if (msg.cursize + sv.datagram.cursize < msg.maxsize)
793 		SZ_Write (&msg, sv.datagram.data, sv.datagram.cursize);
794 
795 // send the datagram
796 	if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
797 	{
798 		SV_DropClient (true);// if the message couldn't send, kick off
799 		return false;
800 	}
801 
802 	return true;
803 }
804 
805 /*
806 =======================
807 SV_UpdateToReliableMessages
808 =======================
809 */
SV_UpdateToReliableMessages(void)810 void SV_UpdateToReliableMessages (void)
811 {
812 	int			i, j;
813 	client_t *client;
814 
815 // check for changes to be sent over the reliable streams
816 	for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
817 	{
818 		if (host_client->old_frags != host_client->edict->v.frags)
819 		{
820 			for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
821 			{
822 				if (!client->active)
823 					continue;
824 				MSG_WriteByte (&client->message, svc_updatefrags);
825 				MSG_WriteByte (&client->message, i);
826 				MSG_WriteShort (&client->message, host_client->edict->v.frags);
827 			}
828 
829 			host_client->old_frags = host_client->edict->v.frags;
830 		}
831 	}
832 
833 	for (j=0, client = svs.clients ; j<svs.maxclients ; j++, client++)
834 	{
835 		if (!client->active)
836 			continue;
837 		SZ_Write (&client->message, sv.reliable_datagram.data, sv.reliable_datagram.cursize);
838 	}
839 
840 	SZ_Clear (&sv.reliable_datagram);
841 }
842 
843 
844 /*
845 =======================
846 SV_SendNop
847 
848 Send a nop message without trashing or sending the accumulated client
849 message buffer
850 =======================
851 */
SV_SendNop(client_t * client)852 void SV_SendNop (client_t *client)
853 {
854 	sizebuf_t	msg;
855 	byte		buf[4];
856 
857 	msg.data = buf;
858 	msg.maxsize = sizeof(buf);
859 	msg.cursize = 0;
860 
861 	MSG_WriteChar (&msg, svc_nop);
862 
863 	if (NET_SendUnreliableMessage (client->netconnection, &msg) == -1)
864 		SV_DropClient (true);	// if the message couldn't send, kick off
865 	client->last_message = realtime;
866 }
867 
868 /*
869 =======================
870 SV_SendClientMessages
871 =======================
872 */
SV_SendClientMessages(void)873 void SV_SendClientMessages (void)
874 {
875 	int			i;
876 
877 // update frags, names, etc
878 	SV_UpdateToReliableMessages ();
879 
880 // build individual updates
881 	for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
882 	{
883 		if (!host_client->active)
884 			continue;
885 
886 		if (host_client->spawned)
887 		{
888 			if (!SV_SendClientDatagram (host_client))
889 				continue;
890 		}
891 		else
892 		{
893 		// the player isn't totally in the game yet
894 		// send small keepalive messages if too much time has passed
895 		// send a full message when the next signon stage has been requested
896 		// some other message data (name changes, etc) may accumulate
897 		// between signon stages
898 			if (!host_client->sendsignon)
899 			{
900 				if (realtime - host_client->last_message > 5)
901 					SV_SendNop (host_client);
902 				continue;	// don't send out non-signon messages
903 			}
904 		}
905 
906 		// check for an overflowed message.  Should only happen
907 		// on a very fucked up connection that backs up a lot, then
908 		// changes level
909 		if (host_client->message.overflowed)
910 		{
911 			SV_DropClient (true);
912 			host_client->message.overflowed = false;
913 			continue;
914 		}
915 
916 		if (host_client->message.cursize || host_client->dropasap)
917 		{
918 			if (!NET_CanSendMessage (host_client->netconnection))
919 			{
920 //				I_Printf ("can't write\n");
921 				continue;
922 			}
923 
924 			if (host_client->dropasap)
925 				SV_DropClient (false);	// went to another level
926 			else
927 			{
928 				if (NET_SendMessage (host_client->netconnection
929 				, &host_client->message) == -1)
930 					SV_DropClient (true);	// if the message couldn't send, kick off
931 				SZ_Clear (&host_client->message);
932 				host_client->last_message = realtime;
933 				host_client->sendsignon = false;
934 			}
935 		}
936 	}
937 
938 
939 // clear muzzle flashes
940 	SV_CleanupEnts ();
941 }
942 
943 
944 /*
945 ==============================================================================
946 
947 SERVER SPAWNING
948 
949 ==============================================================================
950 */
951 
952 /*
953 ================
954 SV_ModelIndex
955 
956 ================
957 */
SV_ModelIndex(char * name)958 int SV_ModelIndex (char *name)
959 {
960 	int		i;
961 
962 	if (!name || !name[0])
963              return 0;
964 
965 	for (i=0 ; i<MAX_MODELS && sv.model_precache[i] ; i++)
966 		if (!strcmp(sv.model_precache[i], name))
967 			return i;
968 	if (i==MAX_MODELS || !sv.model_precache[i])
969 		Sys_Error ("SV_ModelIndex: model %s not precached", name);
970 	return i;
971 }
972 
973 /*
974 ================
975 SV_CreateBaseline
976 
977 ================
978 */
SV_CreateBaseline(void)979 void SV_CreateBaseline (void)
980 {
981 	int			i;
982 	edict_t			*svent;
983 	int				entnum;
984 
985 	for (entnum = 0; entnum < sv.num_edicts ; entnum++)
986 	{
987 	// get the current server version
988 		svent = EDICT_NUM(entnum);
989 		if (svent->free)
990 			continue;
991 		if (entnum > svs.maxclients && !svent->v.modelindex)
992 			continue;
993 
994 	//
995 	// create entity baseline
996 	//
997 
998 	//PENTA:
999 	//-=-WARNING-=-WARNING-=-WARNING-=-WARNING-=-WARNING-=-WARNING-=-WARNING-=-WARNING
1000 	//-=-WARNING-=-WARNING-=-WARNING-=-WARNING-=-WARNING-=-WARNING-=-WARNING-=-WARNING
1001 	//-=-WARNING-=-WARNING-=-WARNING-=-WARNING-=-WARNING-=-WARNING-=-WARNING-=-WARNING
1002 	// this code is duplicated elsewhere on the server side
1003 	// but on the client side the same parsing routine is used
1004 	// if you change anyting here don't forget to change the other too
1005 	// our you'll spend several hours debugging it.
1006 
1007 		VectorCopy (svent->v.origin, svent->baseline.origin);
1008 		VectorCopy (svent->v.angles, svent->baseline.angles);
1009 		svent->baseline.frame = svent->v.frame;
1010 		svent->baseline.skin = svent->v.skin;
1011 
1012 		//PENTA: new baseline fields
1013 		svent->baseline.alpha = HACKY_GETFIELD(svent, eval_alpha, float, 0);
1014 		svent->baseline.light_lev = HACKY_GETFIELD(svent, eval_light_lev, float, 300);
1015 		svent->baseline.style = HACKY_GETFIELD(svent, eval_style, float, 0);
1016 		svent->baseline.pflags = HACKY_GETFIELD(svent, eval_pflags, float, 0);
1017 
1018 		VectorCopy (HACKY_GETFIELD(svent, eval_color, vec3_t, vec3_origin), svent->baseline.color);
1019 
1020 		if (entnum > 0 && entnum <= svs.maxclients)
1021 		{
1022 			svent->baseline.colormap = entnum;
1023 			svent->baseline.modelindex = SV_ModelIndex("progs/player.mdl");
1024 		}
1025 		else
1026 		{
1027 			svent->baseline.colormap = 0;
1028 			svent->baseline.modelindex =
1029 				SV_ModelIndex(pr_strings + svent->v.model);
1030 		}
1031 
1032 	//
1033 	// add to the message
1034 	//
1035 		MSG_WriteByte (&sv.signon,svc_spawnbaseline);
1036 		MSG_WriteShort (&sv.signon,entnum);
1037 
1038 		MSG_WriteByte (&sv.signon, svent->baseline.modelindex);
1039 		MSG_WriteByte (&sv.signon, svent->baseline.frame);
1040 		MSG_WriteByte (&sv.signon, svent->baseline.colormap);
1041 		MSG_WriteByte (&sv.signon, svent->baseline.skin);
1042 
1043 		//PENTA: new baseline fields
1044 		MSG_WriteByte (&sv.signon, (int)(svent->baseline.alpha*255));
1045 		MSG_WriteByte (&sv.signon, svent->baseline.style);
1046 		MSG_WriteShort (&sv.signon, svent->baseline.light_lev);
1047 		MSG_WriteByte (&sv.signon,  svent->baseline.pflags);
1048 		for (i=0 ; i<3 ; i++)
1049 		{
1050 			MSG_WriteCoord(&sv.signon, svent->baseline.origin[i]);
1051 			MSG_WriteAngle(&sv.signon, svent->baseline.angles[i]);
1052 			MSG_WriteByte(&sv.signon, (int)(svent->baseline.color[i]*255));
1053 		}
1054 	}
1055 }
1056 
1057 
1058 /*
1059 ================
1060 SV_SendReconnect
1061 
1062 Tell all the clients that the server is changing levels
1063 ================
1064 */
SV_SendReconnect(void)1065 void SV_SendReconnect (void)
1066 {
1067 	char	data[128];
1068 	sizebuf_t	msg;
1069 
1070 	msg.data = data;
1071 	msg.cursize = 0;
1072 	msg.maxsize = sizeof(data);
1073 
1074 	MSG_WriteChar (&msg, svc_stufftext);
1075 	MSG_WriteString (&msg, "reconnect\n");
1076 	NET_SendToAll (&msg, 5);
1077 
1078 	if (cls.state != ca_dedicated)
1079 #ifdef QUAKE2
1080 		Cbuf_InsertText ("reconnect\n");
1081 #else
1082 		Cmd_ExecuteString ("reconnect\n", src_command);
1083 #endif
1084 }
1085 
1086 
1087 /*
1088 ================
1089 SV_SaveSpawnparms
1090 
1091 Grabs the current state of each client for saving across the
1092 transition to another level
1093 ================
1094 */
SV_SaveSpawnparms(void)1095 void SV_SaveSpawnparms (void)
1096 {
1097 	int		i, j;
1098 
1099 	svs.serverflags = pr_global_struct->serverflags;
1100 
1101 	for (i=0, host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1102 	{
1103 		if (!host_client->active)
1104 			continue;
1105 
1106 	// call the progs to get default spawn parms for the new client
1107 		pr_global_struct->self = EDICT_TO_PROG(host_client->edict);
1108 		PR_ExecuteProgram (pr_global_struct->SetChangeParms);
1109 		for (j=0 ; j<NUM_SPAWN_PARMS ; j++)
1110 			host_client->spawn_parms[j] = (&pr_global_struct->parm1)[j];
1111 	}
1112 }
1113 
1114 
1115 /*
1116 ================
1117 SV_SpawnServer
1118 
1119 This is called at the start of each level
1120 ================
1121 */
1122 extern float		scr_centertime_off;
1123 
1124 #ifdef QUAKE2
SV_SpawnServer(char * server,char * startspot)1125 void SV_SpawnServer (char *server, char *startspot)
1126 #else
1127 void SV_SpawnServer (char *server)
1128 #endif
1129 {
1130 	edict_t		*ent;
1131 	int			i;
1132 
1133 	// let's not have any servers with no name
1134 	if (hostname.string[0] == 0)
1135 		Cvar_Set ("hostname", "UNNAMED");
1136 	scr_centertime_off = 0;
1137 
1138 	Con_DPrintf ("SpawnServer: %s\n",server);
1139 	svs.changelevel_issued = false;		// now safe to issue another
1140 
1141 //
1142 // tell all connected clients that we are going to a new level
1143 //
1144 	if (sv.active)
1145 	{
1146 		SV_SendReconnect ();
1147 	}
1148 
1149 //
1150 // make cvars consistant
1151 //
1152 	if (coop.value)
1153 		Cvar_SetValue ("deathmatch", 0);
1154 	current_skill = (int)(skill.value + 0.5);
1155 	if (current_skill < 0)
1156 		current_skill = 0;
1157 	if (current_skill > 3)
1158 		current_skill = 3;
1159 
1160 	Cvar_SetValue ("skill", (float)current_skill);
1161 
1162 //
1163 // set up the new server
1164 //
1165 	Host_ClearMemory ();
1166 
1167 	memset (&sv, 0, sizeof(sv));
1168 
1169 	strcpy (sv.name, server);
1170 #ifdef QUAKE2
1171 	if (startspot)
1172 		strcpy(sv.startspot, startspot);
1173 #endif
1174 
1175 // load progs to get entity field count
1176 	PR_LoadProgs ();
1177 
1178 // allocate server memory
1179 	sv.max_edicts = MAX_EDICTS;
1180 
1181 	sv.edicts = Hunk_AllocName (sv.max_edicts*pr_edict_size, "edicts");
1182 
1183 	sv.datagram.maxsize = sizeof(sv.datagram_buf);
1184 	sv.datagram.cursize = 0;
1185 	sv.datagram.data = sv.datagram_buf;
1186 
1187 	sv.reliable_datagram.maxsize = sizeof(sv.reliable_datagram_buf);
1188 	sv.reliable_datagram.cursize = 0;
1189 	sv.reliable_datagram.data = sv.reliable_datagram_buf;
1190 
1191 	sv.signon.maxsize = sizeof(sv.signon_buf);
1192 	sv.signon.cursize = 0;
1193 	sv.signon.data = sv.signon_buf;
1194 
1195 // leave slots at start for clients only
1196 	sv.num_edicts = svs.maxclients+1;
1197 	for (i=0 ; i<svs.maxclients ; i++)
1198 	{
1199 		ent = EDICT_NUM(i+1);
1200 		svs.clients[i].edict = ent;
1201 	}
1202 
1203 	sv.state = ss_loading;
1204 	sv.paused = false;
1205 
1206 	sv.time = 1.0;
1207 
1208 	strcpy (sv.name, server);
1209 	sprintf (sv.modelname,"maps/%s.bsp", server);
1210 	sv.worldmodel = Mod_ForName (sv.modelname, false);
1211 	if (!sv.worldmodel)
1212 	{
1213 		Con_Printf ("Couldn't spawn server %s\n", sv.modelname);
1214 		sv.active = false;
1215 		return;
1216 	}
1217 	sv.models[1] = sv.worldmodel;
1218 
1219 //
1220 // clear world interaction links
1221 //
1222 	SV_ClearWorld ();
1223 
1224 	sv.sound_precache[0] = pr_strings;
1225 
1226 	sv.model_precache[0] = pr_strings;
1227 	sv.model_precache[1] = sv.modelname;
1228 	for (i=1 ; i<sv.worldmodel->numsubmodels ; i++)
1229 	{
1230 		sv.model_precache[1+i] = localmodels[i];
1231 		sv.models[i+1] = Mod_ForName (localmodels[i], false);
1232 	}
1233 
1234 //
1235 // load the rest of the entities
1236 //
1237 	ent = EDICT_NUM(0);
1238 	memset (&ent->v, 0, progs->entityfields * 4);
1239 	ent->free = false;
1240 	ent->v.model = sv.worldmodel->name - pr_strings;
1241 	ent->v.modelindex = 1;		// world model
1242 	ent->v.solid = SOLID_BSP;
1243 	ent->v.movetype = MOVETYPE_PUSH;
1244 
1245 	if (coop.value)
1246 		pr_global_struct->coop = coop.value;
1247 	else
1248 		pr_global_struct->deathmatch = deathmatch.value;
1249 
1250 	pr_global_struct->mapname = sv.name - pr_strings;
1251 #ifdef QUAKE2
1252 	pr_global_struct->startspot = sv.startspot - pr_strings;
1253 #endif
1254 
1255 // serverflags are for cross level information (sigils)
1256 	pr_global_struct->serverflags = svs.serverflags;
1257 
1258 	ED_LoadFromFile (sv.worldmodel->entities);
1259 
1260 	sv.active = true;
1261 
1262 // all setup is completed, any further precache statements are errors
1263 	sv.state = ss_active;
1264 
1265 // run two frames to allow everything to settle
1266 	host_frametime = 0.1;
1267 	SV_Physics ();
1268 	SV_Physics ();
1269 
1270 
1271 // create a baseline for more efficient communications
1272 	SV_CreateBaseline ();
1273 
1274 // send serverinfo to all connected clients
1275 	for (i=0,host_client = svs.clients ; i<svs.maxclients ; i++, host_client++)
1276 		if (host_client->active)
1277 			SV_SendServerinfo (host_client);
1278 
1279 	Con_DPrintf ("Server spawned.\n");
1280 }
1281 
1282