-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathsv_send.c
1734 lines (1542 loc) · 62.5 KB
/
sv_send.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 1996-1997 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "quakedef.h"
#include "sv_demo.h"
extern cvar_t sv_airaccel_qw_stretchfactor;
extern cvar_t sv_qcstats;
extern cvar_t sv_warsowbunny_airforwardaccel;
extern cvar_t sv_warsowbunny_accel;
extern cvar_t sv_warsowbunny_topspeed;
extern cvar_t sv_warsowbunny_turnaccel;
extern cvar_t sv_warsowbunny_backtosideratio;
extern cvar_t sv_onlycsqcnetworking;
extern cvar_t sv_cullentities_trace_entityocclusion;
extern cvar_t sv_cullentities_trace_samples_players;
extern cvar_t sv_cullentities_trace_eyejitter;
extern cvar_t sv_cullentities_trace_expand;
extern cvar_t sv_cullentities_trace_delay_players;
extern cvar_t sv_cullentities_trace_spectators;
/*
=============================================================================
EVENT MESSAGES
=============================================================================
*/
/*
=================
SV_ClientPrint
Sends text across to be displayed
FIXME: make this just a stuffed echo?
=================
*/
void SV_ClientPrint(const char *msg)
{
if (host_client->netconnection)
{
MSG_WriteByte(&host_client->netconnection->message, svc_print);
MSG_WriteString(&host_client->netconnection->message, msg);
}
}
/*
=================
SV_ClientPrintf
Sends text across to be displayed
FIXME: make this just a stuffed echo?
=================
*/
void SV_ClientPrintf(const char *fmt, ...)
{
va_list argptr;
char msg[MAX_INPUTLINE];
va_start(argptr,fmt);
dpvsnprintf(msg,sizeof(msg),fmt,argptr);
va_end(argptr);
SV_ClientPrint(msg);
}
/*
=================
SV_BroadcastPrint
Sends text to all active clients
=================
*/
void SV_BroadcastPrint(const char *msg)
{
int i;
client_t *client;
for (i = 0, client = svs.clients;i < svs.maxclients;i++, client++)
{
if (client->active && client->netconnection)
{
MSG_WriteByte(&client->netconnection->message, svc_print);
MSG_WriteString(&client->netconnection->message, msg);
}
}
if (sv_echobprint.integer && !host_isclient.integer)
Con_Print(msg);
}
/*
=================
SV_BroadcastPrintf
Sends text to all active clients
=================
*/
void SV_BroadcastPrintf(const char *fmt, ...)
{
va_list argptr;
char msg[MAX_INPUTLINE];
va_start(argptr,fmt);
dpvsnprintf(msg,sizeof(msg),fmt,argptr);
va_end(argptr);
SV_BroadcastPrint(msg);
}
/*
=================
SV_ClientCommands
Send text over to the client to be executed
=================
*/
void SV_ClientCommands(const char *fmt, ...)
{
va_list argptr;
char string[MAX_INPUTLINE];
if (!host_client->netconnection)
return;
va_start(argptr,fmt);
dpvsnprintf(string, sizeof(string), fmt, argptr);
va_end(argptr);
MSG_WriteByte(&host_client->netconnection->message, svc_stufftext);
MSG_WriteString(&host_client->netconnection->message, string);
}
/*
==================
SV_StartParticle
Make sure the event gets sent to all clients
==================
*/
void SV_StartParticle (vec3_t org, vec3_t dir, int color, int count)
{
int i;
if (sv.datagram.cursize > MAX_PACKETFRAGMENT-18)
return;
MSG_WriteByte (&sv.datagram, svc_particle);
MSG_WriteCoord (&sv.datagram, org[0], sv.protocol);
MSG_WriteCoord (&sv.datagram, org[1], sv.protocol);
MSG_WriteCoord (&sv.datagram, org[2], sv.protocol);
for (i=0 ; i<3 ; i++)
MSG_WriteChar (&sv.datagram, (int)bound(-128, dir[i]*16, 127));
MSG_WriteByte (&sv.datagram, count);
MSG_WriteByte (&sv.datagram, color);
SV_FlushBroadcastMessages();
}
/*
==================
SV_StartEffect
Make sure the event gets sent to all clients
==================
*/
void SV_StartEffect (vec3_t org, int modelindex, int startframe, int framecount, int framerate)
{
if (modelindex >= 256 || startframe >= 256)
{
if (sv.datagram.cursize > MAX_PACKETFRAGMENT-19)
return;
MSG_WriteByte (&sv.datagram, svc_effect2);
MSG_WriteCoord (&sv.datagram, org[0], sv.protocol);
MSG_WriteCoord (&sv.datagram, org[1], sv.protocol);
MSG_WriteCoord (&sv.datagram, org[2], sv.protocol);
MSG_WriteShort (&sv.datagram, modelindex);
MSG_WriteShort (&sv.datagram, startframe);
MSG_WriteByte (&sv.datagram, framecount);
MSG_WriteByte (&sv.datagram, framerate);
}
else
{
if (sv.datagram.cursize > MAX_PACKETFRAGMENT-17)
return;
MSG_WriteByte (&sv.datagram, svc_effect);
MSG_WriteCoord (&sv.datagram, org[0], sv.protocol);
MSG_WriteCoord (&sv.datagram, org[1], sv.protocol);
MSG_WriteCoord (&sv.datagram, org[2], sv.protocol);
MSG_WriteByte (&sv.datagram, modelindex);
MSG_WriteByte (&sv.datagram, startframe);
MSG_WriteByte (&sv.datagram, framecount);
MSG_WriteByte (&sv.datagram, framerate);
}
SV_FlushBroadcastMessages();
}
/*
==================
SV_StartSound
Each entity can have eight independant sound sources, like voice,
weapon, feet, etc.
Channel 0 is an auto-allocate channel, the others override anything
already running on that entity/channel pair.
An attenuation of 0 will play full volume everywhere in the level.
Larger attenuations will drop off. (max 4 attenuation)
==================
*/
void SV_StartSound (prvm_edict_t *entity, int channel, const char *sample, int nvolume, float attenuation, qbool reliable, float speed)
{
prvm_prog_t *prog = SVVM_prog;
sizebuf_t *dest;
int sound_num, field_mask, i, ent, speed4000;
dest = (reliable ? &sv.reliable_datagram : &sv.datagram);
if (nvolume < 0 || nvolume > 255)
{
Con_Printf ("SV_StartSound: volume = %i\n", nvolume);
return;
}
if (attenuation < 0 || attenuation > 4)
{
Con_Printf ("SV_StartSound: attenuation = %f\n", attenuation);
return;
}
if (!IS_CHAN(channel))
{
Con_Printf ("SV_StartSound: channel = %i\n", channel);
return;
}
channel = CHAN_ENGINE2NET(channel);
if (sv.datagram.cursize > MAX_PACKETFRAGMENT-21)
return;
// find precache number for sound
sound_num = SV_SoundIndex(sample, 1);
if (!sound_num)
return;
ent = PRVM_NUM_FOR_EDICT(entity);
speed4000 = (int)floor(speed * 4000.0f + 0.5f);
field_mask = 0;
if (nvolume != DEFAULT_SOUND_PACKET_VOLUME)
field_mask |= SND_VOLUME;
if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
field_mask |= SND_ATTENUATION;
if (speed4000 && speed4000 != 4000)
field_mask |= SND_SPEEDUSHORT4000;
if (ent >= 8192 || channel < 0 || channel > 7)
field_mask |= SND_LARGEENTITY;
if (sound_num >= 256)
field_mask |= SND_LARGESOUND;
// directed messages go only to the entity they are targeted on
MSG_WriteByte (dest, svc_sound);
MSG_WriteByte (dest, field_mask);
if (field_mask & SND_VOLUME)
MSG_WriteByte (dest, nvolume);
if (field_mask & SND_ATTENUATION)
MSG_WriteByte (dest, (int)(attenuation*64));
if (field_mask & SND_SPEEDUSHORT4000)
MSG_WriteShort (dest, speed4000);
if (field_mask & SND_LARGEENTITY)
{
MSG_WriteShort (dest, ent);
MSG_WriteChar (dest, channel);
}
else
MSG_WriteShort (dest, (ent<<3) | channel);
if ((field_mask & SND_LARGESOUND) || sv.protocol == PROTOCOL_NEHAHRABJP2 || sv.protocol == PROTOCOL_NEHAHRABJP3)
MSG_WriteShort (dest, sound_num);
else
MSG_WriteByte (dest, sound_num);
for (i = 0;i < 3;i++)
MSG_WriteCoord (dest, PRVM_serveredictvector(entity, origin)[i]+0.5*(PRVM_serveredictvector(entity, mins)[i]+PRVM_serveredictvector(entity, maxs)[i]), sv.protocol);
// TODO do we have to do anything here when dest is &sv.reliable_datagram?
if(!reliable)
SV_FlushBroadcastMessages();
}
/*
==================
SV_StartPointSound
Nearly the same logic as SV_StartSound, except an origin
instead of an entity is provided and channel is omitted.
The entity sent to the client is 0 (world) and the channel
is 0 (CHAN_AUTO). SND_LARGEENTITY will never occur in this
function, therefore the check for it is omitted.
==================
*/
void SV_StartPointSound (vec3_t origin, const char *sample, int nvolume, float attenuation, float speed)
{
int sound_num, field_mask, i, speed4000;
if (nvolume < 0 || nvolume > 255)
{
Con_Printf ("SV_StartPointSound: volume = %i\n", nvolume);
return;
}
if (attenuation < 0 || attenuation > 4)
{
Con_Printf ("SV_StartPointSound: attenuation = %f\n", attenuation);
return;
}
if (sv.datagram.cursize > MAX_PACKETFRAGMENT-21)
return;
// find precache number for sound
sound_num = SV_SoundIndex(sample, 1);
if (!sound_num)
return;
speed4000 = (int)(speed * 40.0f);
field_mask = 0;
if (nvolume != DEFAULT_SOUND_PACKET_VOLUME)
field_mask |= SND_VOLUME;
if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
field_mask |= SND_ATTENUATION;
if (sound_num >= 256)
field_mask |= SND_LARGESOUND;
if (speed4000 && speed4000 != 4000)
field_mask |= SND_SPEEDUSHORT4000;
// directed messages go only to the entity they are targeted on
MSG_WriteByte (&sv.datagram, svc_sound);
MSG_WriteByte (&sv.datagram, field_mask);
if (field_mask & SND_VOLUME)
MSG_WriteByte (&sv.datagram, nvolume);
if (field_mask & SND_ATTENUATION)
MSG_WriteByte (&sv.datagram, (int)(attenuation*64));
if (field_mask & SND_SPEEDUSHORT4000)
MSG_WriteShort (&sv.datagram, speed4000);
// Always write entnum 0 for the world entity
MSG_WriteShort (&sv.datagram, (0<<3) | 0);
if (field_mask & SND_LARGESOUND)
MSG_WriteShort (&sv.datagram, sound_num);
else
MSG_WriteByte (&sv.datagram, sound_num);
for (i = 0;i < 3;i++)
MSG_WriteCoord (&sv.datagram, origin[i], sv.protocol);
SV_FlushBroadcastMessages();
}
/*
===============================================================================
FRAME UPDATES
===============================================================================
*/
/*
=============================================================================
The PVS must include a small area around the client to allow head bobbing
or other small motion on the client side. Otherwise, a bob might cause an
entity that should be visible to not show up, especially when the bob
crosses a waterline.
=============================================================================
*/
static qbool SV_PrepareEntityForSending (prvm_edict_t *ent, entity_state_t *cs, int enumber)
{
prvm_prog_t *prog = SVVM_prog;
int i;
unsigned int sendflags;
unsigned int version;
unsigned int modelindex, effects, flags, glowsize, lightstyle, lightpflags, light[4], specialvisibilityradius;
unsigned int customizeentityforclient;
unsigned int sendentity;
float f;
prvm_vec_t *v;
vec3_t cullmins, cullmaxs;
model_t *model;
// fast path for games that do not use legacy entity networking
// note: still networks clients even if they are legacy
sendentity = PRVM_serveredictfunction(ent, SendEntity);
if (sv_onlycsqcnetworking.integer && !sendentity && enumber > svs.maxclients)
return false;
// this 2 billion unit check is actually to detect NAN origins
// (we really don't want to send those)
if (!(VectorLength2(PRVM_serveredictvector(ent, origin)) < 2000000000.0*2000000000.0))
return false;
// EF_NODRAW prevents sending for any reason except for your own
// client, so we must keep all clients in this superset
effects = (unsigned)PRVM_serveredictfloat(ent, effects);
// we can omit invisible entities with no effects that are not clients
// LadyHavoc: this could kill tags attached to an invisible entity, I
// just hope we never have to support that case
i = (int)PRVM_serveredictfloat(ent, modelindex);
modelindex = (i >= 1 && i < MAX_MODELS && PRVM_serveredictstring(ent, model) && *PRVM_GetString(prog, PRVM_serveredictstring(ent, model)) && sv.models[i]) ? i : 0;
flags = 0;
i = (int)(PRVM_serveredictfloat(ent, glow_size) * 0.25f);
glowsize = (unsigned char)bound(0, i, 255);
if (PRVM_serveredictfloat(ent, glow_trail))
flags |= RENDER_GLOWTRAIL;
if (PRVM_serveredictedict(ent, viewmodelforclient))
flags |= RENDER_VIEWMODEL;
v = PRVM_serveredictvector(ent, color);
f = v[0]*256;
light[0] = (unsigned short)bound(0, f, 65535);
f = v[1]*256;
light[1] = (unsigned short)bound(0, f, 65535);
f = v[2]*256;
light[2] = (unsigned short)bound(0, f, 65535);
f = PRVM_serveredictfloat(ent, light_lev);
light[3] = (unsigned short)bound(0, f, 65535);
lightstyle = (unsigned char)PRVM_serveredictfloat(ent, style);
lightpflags = (unsigned char)PRVM_serveredictfloat(ent, pflags);
if (gamemode == GAME_TENEBRAE)
{
// tenebrae's EF_FULLDYNAMIC conflicts with Q2's EF_NODRAW
if (effects & 16)
{
effects &= ~16;
lightpflags |= PFLAGS_FULLDYNAMIC;
}
// tenebrae's EF_GREEN conflicts with DP's EF_ADDITIVE
if (effects & 32)
{
effects &= ~32;
light[0] = (int)(0.2*256);
light[1] = (int)(1.0*256);
light[2] = (int)(0.2*256);
light[3] = 200;
lightpflags |= PFLAGS_FULLDYNAMIC;
}
}
specialvisibilityradius = 0;
if (lightpflags & PFLAGS_FULLDYNAMIC)
specialvisibilityradius = max(specialvisibilityradius, light[3]);
if (glowsize)
specialvisibilityradius = max(specialvisibilityradius, glowsize * 4);
if (flags & RENDER_GLOWTRAIL)
specialvisibilityradius = max(specialvisibilityradius, 100);
if (effects & (EF_BRIGHTFIELD | EF_MUZZLEFLASH | EF_BRIGHTLIGHT | EF_DIMLIGHT | EF_RED | EF_BLUE | EF_FLAME | EF_STARDUST))
{
if (effects & EF_BRIGHTFIELD)
specialvisibilityradius = max(specialvisibilityradius, 80);
if (effects & EF_MUZZLEFLASH)
specialvisibilityradius = max(specialvisibilityradius, 100);
if (effects & EF_BRIGHTLIGHT)
specialvisibilityradius = max(specialvisibilityradius, 400);
if (effects & EF_DIMLIGHT)
specialvisibilityradius = max(specialvisibilityradius, 200);
if (effects & EF_RED)
specialvisibilityradius = max(specialvisibilityradius, 200);
if (effects & EF_BLUE)
specialvisibilityradius = max(specialvisibilityradius, 200);
if (effects & EF_FLAME)
specialvisibilityradius = max(specialvisibilityradius, 250);
if (effects & EF_STARDUST)
specialvisibilityradius = max(specialvisibilityradius, 100);
}
// early culling checks
// (final culling is done by SV_MarkWriteEntityStateToClient)
customizeentityforclient = PRVM_serveredictfunction(ent, customizeentityforclient);
if (!customizeentityforclient && enumber > svs.maxclients && (!modelindex && !specialvisibilityradius))
return false;
*cs = defaultstate;
cs->active = ACTIVE_NETWORK;
cs->number = enumber;
VectorCopy(PRVM_serveredictvector(ent, origin), cs->origin);
VectorCopy(PRVM_serveredictvector(ent, angles), cs->angles);
cs->flags = flags;
cs->effects = effects;
cs->colormap = (unsigned)PRVM_serveredictfloat(ent, colormap);
cs->modelindex = modelindex;
cs->skin = (unsigned)PRVM_serveredictfloat(ent, skin);
cs->frame = (unsigned)PRVM_serveredictfloat(ent, frame);
cs->viewmodelforclient = PRVM_serveredictedict(ent, viewmodelforclient);
cs->exteriormodelforclient = PRVM_serveredictedict(ent, exteriormodeltoclient);
cs->nodrawtoclient = PRVM_serveredictedict(ent, nodrawtoclient);
cs->drawonlytoclient = PRVM_serveredictedict(ent, drawonlytoclient);
cs->customizeentityforclient = customizeentityforclient;
cs->tagentity = PRVM_serveredictedict(ent, tag_entity);
cs->tagindex = (unsigned char)PRVM_serveredictfloat(ent, tag_index);
cs->glowsize = glowsize;
cs->traileffectnum = PRVM_serveredictfloat(ent, traileffectnum);
// don't need to init cs->colormod because the defaultstate did that for us
//cs->colormod[0] = cs->colormod[1] = cs->colormod[2] = 32;
v = PRVM_serveredictvector(ent, colormod);
if (VectorLength2(v))
{
i = (int)(v[0] * 32.0f);cs->colormod[0] = bound(0, i, 255);
i = (int)(v[1] * 32.0f);cs->colormod[1] = bound(0, i, 255);
i = (int)(v[2] * 32.0f);cs->colormod[2] = bound(0, i, 255);
}
// don't need to init cs->glowmod because the defaultstate did that for us
//cs->glowmod[0] = cs->glowmod[1] = cs->glowmod[2] = 32;
v = PRVM_serveredictvector(ent, glowmod);
if (VectorLength2(v))
{
i = (int)(v[0] * 32.0f);cs->glowmod[0] = bound(0, i, 255);
i = (int)(v[1] * 32.0f);cs->glowmod[1] = bound(0, i, 255);
i = (int)(v[2] * 32.0f);cs->glowmod[2] = bound(0, i, 255);
}
cs->modelindex = modelindex;
cs->alpha = 255;
f = (PRVM_serveredictfloat(ent, alpha) * 255.0f);
if (f)
{
i = (int)f;
cs->alpha = (unsigned char)bound(0, i, 255);
}
// halflife
f = (PRVM_serveredictfloat(ent, renderamt));
if (f)
{
i = (int)f;
cs->alpha = (unsigned char)bound(0, i, 255);
}
cs->scale = 16;
f = (PRVM_serveredictfloat(ent, scale) * 16.0f);
if (f)
{
i = (int)f;
cs->scale = (unsigned char)bound(0, i, 255);
}
cs->glowcolor = 254;
f = PRVM_serveredictfloat(ent, glow_color);
if (f)
cs->glowcolor = (int)f;
if (PRVM_serveredictfloat(ent, fullbright))
cs->effects |= EF_FULLBRIGHT;
f = PRVM_serveredictfloat(ent, modelflags);
if (f)
cs->effects |= ((unsigned int)f & 0xff) << 24;
if (PRVM_serveredictfloat(ent, movetype) == MOVETYPE_STEP || ((int)PRVM_serveredictfloat(ent, flags) & FL_MONSTER))
cs->flags |= RENDER_STEP;
if (cs->number != sv.writeentitiestoclient_cliententitynumber && (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)
cs->flags |= RENDER_LOWPRECISION;
if (PRVM_serveredictfloat(ent, colormap) >= 1024)
cs->flags |= RENDER_COLORMAPPED;
if (cs->viewmodelforclient)
cs->flags |= RENDER_VIEWMODEL; // show relative to the view
if (PRVM_serveredictfloat(ent, sendcomplexanimation))
{
cs->flags |= RENDER_COMPLEXANIMATION;
if (PRVM_serveredictfloat(ent, skeletonindex) >= 1)
cs->skeletonobject = ent->priv.server->skeleton;
cs->framegroupblend[0].frame = PRVM_serveredictfloat(ent, frame);
cs->framegroupblend[1].frame = PRVM_serveredictfloat(ent, frame2);
cs->framegroupblend[2].frame = PRVM_serveredictfloat(ent, frame3);
cs->framegroupblend[3].frame = PRVM_serveredictfloat(ent, frame4);
cs->framegroupblend[0].start = PRVM_serveredictfloat(ent, frame1time);
cs->framegroupblend[1].start = PRVM_serveredictfloat(ent, frame2time);
cs->framegroupblend[2].start = PRVM_serveredictfloat(ent, frame3time);
cs->framegroupblend[3].start = PRVM_serveredictfloat(ent, frame4time);
cs->framegroupblend[1].lerp = PRVM_serveredictfloat(ent, lerpfrac);
cs->framegroupblend[2].lerp = PRVM_serveredictfloat(ent, lerpfrac3);
cs->framegroupblend[3].lerp = PRVM_serveredictfloat(ent, lerpfrac4);
cs->framegroupblend[0].lerp = 1.0f - cs->framegroupblend[1].lerp - cs->framegroupblend[2].lerp - cs->framegroupblend[3].lerp;
cs->frame = 0; // don't need the legacy frame
}
cs->light[0] = light[0];
cs->light[1] = light[1];
cs->light[2] = light[2];
cs->light[3] = light[3];
cs->lightstyle = lightstyle;
cs->lightpflags = lightpflags;
cs->specialvisibilityradius = specialvisibilityradius;
// calculate the visible box of this entity (don't use the physics box
// as that is often smaller than a model, and would not count
// specialvisibilityradius)
if ((model = SV_GetModelByIndex(modelindex)) && (model->type != mod_null))
{
float scale = cs->scale * (1.0f / 16.0f);
if (cs->angles[0] || cs->angles[2]) // pitch and roll
{
VectorMA(cs->origin, scale, model->rotatedmins, cullmins);
VectorMA(cs->origin, scale, model->rotatedmaxs, cullmaxs);
}
else if (cs->angles[1] || ((effects | model->effects) & EF_ROTATE))
{
VectorMA(cs->origin, scale, model->yawmins, cullmins);
VectorMA(cs->origin, scale, model->yawmaxs, cullmaxs);
}
else
{
VectorMA(cs->origin, scale, model->normalmins, cullmins);
VectorMA(cs->origin, scale, model->normalmaxs, cullmaxs);
}
}
else
{
// if there is no model (or it could not be loaded), use the physics box
VectorAdd(cs->origin, PRVM_serveredictvector(ent, mins), cullmins);
VectorAdd(cs->origin, PRVM_serveredictvector(ent, maxs), cullmaxs);
}
if (specialvisibilityradius)
{
cullmins[0] = min(cullmins[0], cs->origin[0] - specialvisibilityradius);
cullmins[1] = min(cullmins[1], cs->origin[1] - specialvisibilityradius);
cullmins[2] = min(cullmins[2], cs->origin[2] - specialvisibilityradius);
cullmaxs[0] = max(cullmaxs[0], cs->origin[0] + specialvisibilityradius);
cullmaxs[1] = max(cullmaxs[1], cs->origin[1] + specialvisibilityradius);
cullmaxs[2] = max(cullmaxs[2], cs->origin[2] + specialvisibilityradius);
}
// calculate center of bbox for network prioritization purposes
VectorMAM(0.5f, cullmins, 0.5f, cullmaxs, cs->netcenter);
// if culling box has moved, update pvs cluster links
if (!VectorCompare(cullmins, ent->priv.server->cullmins) || !VectorCompare(cullmaxs, ent->priv.server->cullmaxs))
{
VectorCopy(cullmins, ent->priv.server->cullmins);
VectorCopy(cullmaxs, ent->priv.server->cullmaxs);
// a value of -1 for pvs_numclusters indicates that the links are not
// cached, and should be re-tested each time, this is the case if the
// culling box touches too many pvs clusters to store, or if the world
// model does not support FindBoxClusters
ent->priv.server->pvs_numclusters = -1;
if (sv.worldmodel && sv.worldmodel->brush.FindBoxClusters)
{
i = sv.worldmodel->brush.FindBoxClusters(sv.worldmodel, cullmins, cullmaxs, MAX_ENTITYCLUSTERS, ent->priv.server->pvs_clusterlist);
if (i <= MAX_ENTITYCLUSTERS)
ent->priv.server->pvs_numclusters = i;
}
}
// we need to do some csqc entity upkeep here
// get self.SendFlags and clear them
// (to let the QC know that they've been read)
if (sendentity)
{
sendflags = (unsigned int)PRVM_serveredictfloat(ent, SendFlags);
PRVM_serveredictfloat(ent, SendFlags) = 0;
// legacy self.Version system
if ((version = (unsigned int)PRVM_serveredictfloat(ent, Version)))
{
if (sv.csqcentityversion[enumber] != version)
sendflags = 0xFFFFFF;
sv.csqcentityversion[enumber] = version;
}
// move sendflags into the per-client sendflags
if (sendflags)
for (i = 0;i < svs.maxclients;i++)
svs.clients[i].csqcentitysendflags[enumber] |= sendflags;
// mark it as inactive for non-csqc networking
cs->active = ACTIVE_SHARED;
}
return true;
}
static void SV_PrepareEntitiesForSending(void)
{
prvm_prog_t *prog = SVVM_prog;
int e;
prvm_edict_t *ent;
// send all entities that touch the pvs
sv.numsendentities = 0;
sv.sendentitiesindex[0] = NULL;
memset(sv.sendentitiesindex, 0, prog->num_edicts * sizeof(*sv.sendentitiesindex));
for (e = 1, ent = PRVM_NEXT_EDICT(prog->edicts);e < prog->num_edicts;e++, ent = PRVM_NEXT_EDICT(ent))
{
if (!ent->free && SV_PrepareEntityForSending(ent, sv.sendentities + sv.numsendentities, e))
{
sv.sendentitiesindex[e] = sv.sendentities + sv.numsendentities;
sv.numsendentities++;
}
}
}
#define MAX_LINEOFSIGHTTRACES 64
qbool SV_CanSeeBox(int numtraces, vec_t eyejitter, vec_t enlarge, vec_t entboxexpand, vec3_t eye, vec3_t entboxmins, vec3_t entboxmaxs)
{
prvm_prog_t *prog = SVVM_prog;
float pitchsign;
float alpha;
float starttransformed[3], endtransformed[3];
float boxminstransformed[3], boxmaxstransformed[3];
float localboxcenter[3], localboxextents[3], localboxmins[3], localboxmaxs[3];
int blocked = 0;
int traceindex;
int originalnumtouchedicts;
int numtouchedicts = 0;
int touchindex;
matrix4x4_t matrix, imatrix;
model_t *model;
prvm_edict_t *touch;
static prvm_edict_t *touchedicts[MAX_EDICTS];
vec3_t eyemins, eyemaxs, start;
vec3_t boxmins, boxmaxs;
vec3_t clipboxmins, clipboxmaxs;
vec3_t endpoints[MAX_LINEOFSIGHTTRACES];
numtraces = min(numtraces, MAX_LINEOFSIGHTTRACES);
// jitter the eye location within this box
eyemins[0] = eye[0] - eyejitter;
eyemaxs[0] = eye[0] + eyejitter;
eyemins[1] = eye[1] - eyejitter;
eyemaxs[1] = eye[1] + eyejitter;
eyemins[2] = eye[2] - eyejitter;
eyemaxs[2] = eye[2] + eyejitter;
// expand the box a little
boxmins[0] = (enlarge+1) * entboxmins[0] - enlarge * entboxmaxs[0] - entboxexpand;
boxmaxs[0] = (enlarge+1) * entboxmaxs[0] - enlarge * entboxmins[0] + entboxexpand;
boxmins[1] = (enlarge+1) * entboxmins[1] - enlarge * entboxmaxs[1] - entboxexpand;
boxmaxs[1] = (enlarge+1) * entboxmaxs[1] - enlarge * entboxmins[1] + entboxexpand;
boxmins[2] = (enlarge+1) * entboxmins[2] - enlarge * entboxmaxs[2] - entboxexpand;
boxmaxs[2] = (enlarge+1) * entboxmaxs[2] - enlarge * entboxmins[2] + entboxexpand;
VectorMAM(0.5f, boxmins, 0.5f, boxmaxs, endpoints[0]);
for (traceindex = 1;traceindex < numtraces;traceindex++)
VectorSet(endpoints[traceindex], lhrandom(boxmins[0], boxmaxs[0]), lhrandom(boxmins[1], boxmaxs[1]), lhrandom(boxmins[2], boxmaxs[2]));
// calculate sweep box for the entire swarm of traces
VectorCopy(eyemins, clipboxmins);
VectorCopy(eyemaxs, clipboxmaxs);
for (traceindex = 0;traceindex < numtraces;traceindex++)
{
clipboxmins[0] = min(clipboxmins[0], endpoints[traceindex][0]);
clipboxmins[1] = min(clipboxmins[1], endpoints[traceindex][1]);
clipboxmins[2] = min(clipboxmins[2], endpoints[traceindex][2]);
clipboxmaxs[0] = max(clipboxmaxs[0], endpoints[traceindex][0]);
clipboxmaxs[1] = max(clipboxmaxs[1], endpoints[traceindex][1]);
clipboxmaxs[2] = max(clipboxmaxs[2], endpoints[traceindex][2]);
}
// get the list of entities in the sweep box
if (sv_cullentities_trace_entityocclusion.integer)
numtouchedicts = SV_EntitiesInBox(clipboxmins, clipboxmaxs, MAX_EDICTS, touchedicts);
if (numtouchedicts > MAX_EDICTS)
{
// this never happens
Con_Printf("SV_EntitiesInBox returned %i edicts, max was %i\n", numtouchedicts, MAX_EDICTS);
numtouchedicts = MAX_EDICTS;
}
// iterate the entities found in the sweep box and filter them
originalnumtouchedicts = numtouchedicts;
numtouchedicts = 0;
for (touchindex = 0;touchindex < originalnumtouchedicts;touchindex++)
{
touch = touchedicts[touchindex];
if (PRVM_serveredictfloat(touch, solid) != SOLID_BSP)
continue;
model = SV_GetModelFromEdict(touch);
if (!model || !model->brush.TraceLineOfSight)
continue;
// skip obviously transparent entities
alpha = PRVM_serveredictfloat(touch, alpha);
if (alpha && alpha < 1)
continue;
if ((int)PRVM_serveredictfloat(touch, effects) & EF_ADDITIVE)
continue;
touchedicts[numtouchedicts++] = touch;
}
// now that we have a filtered list of "interesting" entities, fire each
// ray against all of them, this gives us an early-out case when something
// is visible (which it often is)
for (traceindex = 0;traceindex < numtraces;traceindex++)
{
VectorSet(start, lhrandom(eyemins[0], eyemaxs[0]), lhrandom(eyemins[1], eyemaxs[1]), lhrandom(eyemins[2], eyemaxs[2]));
// check world occlusion
if (sv.worldmodel && sv.worldmodel->brush.TraceLineOfSight)
if (!sv.worldmodel->brush.TraceLineOfSight(sv.worldmodel, start, endpoints[traceindex], boxmins, boxmaxs))
continue;
for (touchindex = 0;touchindex < numtouchedicts;touchindex++)
{
touch = touchedicts[touchindex];
model = SV_GetModelFromEdict(touch);
if(model && model->brush.TraceLineOfSight)
{
// get the entity matrix
pitchsign = SV_GetPitchSign(prog, touch);
Matrix4x4_CreateFromQuakeEntity(&matrix, PRVM_serveredictvector(touch, origin)[0], PRVM_serveredictvector(touch, origin)[1], PRVM_serveredictvector(touch, origin)[2], pitchsign * PRVM_serveredictvector(touch, angles)[0], PRVM_serveredictvector(touch, angles)[1], PRVM_serveredictvector(touch, angles)[2], 1);
Matrix4x4_Invert_Simple(&imatrix, &matrix);
// see if the ray hits this entity
Matrix4x4_Transform(&imatrix, start, starttransformed);
Matrix4x4_Transform(&imatrix, endpoints[traceindex], endtransformed);
Matrix4x4_Transform(&imatrix, boxmins, boxminstransformed);
Matrix4x4_Transform(&imatrix, boxmaxs, boxmaxstransformed);
// transform the AABB to local space
VectorMAM(0.5f, boxminstransformed, 0.5f, boxmaxstransformed, localboxcenter);
localboxextents[0] = fabs(boxmaxstransformed[0] - localboxcenter[0]);
localboxextents[1] = fabs(boxmaxstransformed[1] - localboxcenter[1]);
localboxextents[2] = fabs(boxmaxstransformed[2] - localboxcenter[2]);
localboxmins[0] = localboxcenter[0] - localboxextents[0];
localboxmins[1] = localboxcenter[1] - localboxextents[1];
localboxmins[2] = localboxcenter[2] - localboxextents[2];
localboxmaxs[0] = localboxcenter[0] + localboxextents[0];
localboxmaxs[1] = localboxcenter[1] + localboxextents[1];
localboxmaxs[2] = localboxcenter[2] + localboxextents[2];
if (!model->brush.TraceLineOfSight(model, starttransformed, endtransformed, localboxmins, localboxmaxs))
{
blocked++;
break;
}
}
}
// check if the ray was blocked
if (touchindex < numtouchedicts)
continue;
// return if the ray was not blocked
return true;
}
// no rays survived
return false;
}
void SV_MarkWriteEntityStateToClient(entity_state_t *s, client_t *client)
{
prvm_prog_t *prog = SVVM_prog;
int isbmodel;
model_t *model;
prvm_edict_t *ed;
if (sv.sententitiesconsideration[s->number] == sv.sententitiesmark)
return;
sv.sententitiesconsideration[s->number] = sv.sententitiesmark;
sv.writeentitiestoclient_stats_totalentities++;
if (s->customizeentityforclient)
{
PRVM_serverglobalfloat(time) = sv.time;
PRVM_serverglobaledict(self) = s->number;
PRVM_serverglobaledict(other) = sv.writeentitiestoclient_cliententitynumber;
prog->ExecuteProgram(prog, s->customizeentityforclient, "customizeentityforclient: NULL function");
if(!PRVM_G_FLOAT(OFS_RETURN) || !SV_PrepareEntityForSending(PRVM_EDICT_NUM(s->number), s, s->number))
return;
}
// never reject player
if (s->number != sv.writeentitiestoclient_cliententitynumber)
{
// check various rejection conditions
if (s->nodrawtoclient == sv.writeentitiestoclient_cliententitynumber)
return;
if (s->drawonlytoclient && s->drawonlytoclient != sv.writeentitiestoclient_cliententitynumber)
return;
if (s->effects & EF_NODRAW)
return;
// LadyHavoc: only send entities with a model or important effects
if (!s->modelindex && s->specialvisibilityradius == 0)
return;
isbmodel = (model = SV_GetModelByIndex(s->modelindex)) != NULL && model->name[0] == '*';
// viewmodels don't have visibility checking
if (s->viewmodelforclient)
{
if (s->viewmodelforclient != sv.writeentitiestoclient_cliententitynumber)
return;
}
else if (s->tagentity)
{
// tag attached entities simply check their parent
if (!sv.sendentitiesindex[s->tagentity])
return;
SV_MarkWriteEntityStateToClient(sv.sendentitiesindex[s->tagentity], client);
if (sv.sententities[s->tagentity] != sv.sententitiesmark)
return;
}
// always send world submodels in newer protocols because they don't
// generate much traffic (in old protocols they hog bandwidth)
// but only if sv_cullentities_nevercullbmodels is off
else if (!(s->effects & EF_NODEPTHTEST) && (!isbmodel || !sv_cullentities_nevercullbmodels.integer || sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_QUAKEDP || sv.protocol == PROTOCOL_NEHAHRAMOVIE))
{
// entity has survived every check so far, check if visible
ed = PRVM_EDICT_NUM(s->number);
// if not touching a visible leaf
if (sv_cullentities_pvs.integer && !r_novis.integer && !r_trippy.integer && sv.writeentitiestoclient_pvs)
{
if (ed->priv.server->pvs_numclusters < 0)
{
// entity too big for clusters list
if (sv.worldmodel && sv.worldmodel->brush.BoxTouchingPVS && !sv.worldmodel->brush.BoxTouchingPVS(sv.worldmodel, sv.writeentitiestoclient_pvs, ed->priv.server->cullmins, ed->priv.server->cullmaxs))
{
sv.writeentitiestoclient_stats_culled_pvs++;
return;
}
}
else
{
int i;
// check cached clusters list
for (i = 0;i < ed->priv.server->pvs_numclusters;i++)
if (CHECKPVSBIT(sv.writeentitiestoclient_pvs, ed->priv.server->pvs_clusterlist[i]))
break;
if (i == ed->priv.server->pvs_numclusters)
{
sv.writeentitiestoclient_stats_culled_pvs++;
return;
}
}
}
// or not seen by random tracelines
if (sv_cullentities_trace.integer && !isbmodel && sv.worldmodel && sv.worldmodel->brush.TraceLineOfSight && !r_trippy.integer && (client->frags != -666 || sv_cullentities_trace_spectators.integer))
{
int samples =
s->number <= svs.maxclients
? sv_cullentities_trace_samples_players.integer
:
s->specialvisibilityradius
? sv_cullentities_trace_samples_extra.integer
: sv_cullentities_trace_samples.integer;
if(samples > 0)
{
int eyeindex;
for (eyeindex = 0;eyeindex < sv.writeentitiestoclient_numeyes;eyeindex++)
if(SV_CanSeeBox(samples, sv_cullentities_trace_eyejitter.value, sv_cullentities_trace_enlarge.value, sv_cullentities_trace_expand.value, sv.writeentitiestoclient_eyes[eyeindex], ed->priv.server->cullmins, ed->priv.server->cullmaxs))
break;
if(eyeindex < sv.writeentitiestoclient_numeyes)
svs.clients[sv.writeentitiestoclient_clientnumber].visibletime[s->number] =
host.realtime + (
s->number <= svs.maxclients
? sv_cullentities_trace_delay_players.value
: sv_cullentities_trace_delay.value
);
else if ((float)host.realtime > svs.clients[sv.writeentitiestoclient_clientnumber].visibletime[s->number])
{
sv.writeentitiestoclient_stats_culled_trace++;
return;
}
}
}
}
}
// this just marks it for sending
// FIXME: it would be more efficient to send here, but the entity
// compressor isn't that flexible
sv.writeentitiestoclient_stats_visibleentities++;
sv.sententities[s->number] = sv.sententitiesmark;
}
#if MAX_LEVELNETWORKEYES > 0
#define MAX_EYE_RECURSION 1 // increase if recursion gets supported by portals
void SV_AddCameraEyes(void)
{
prvm_prog_t *prog = SVVM_prog;
int e, i, j, k;
prvm_edict_t *ed;
int cameras[MAX_LEVELNETWORKEYES];
vec3_t camera_origins[MAX_LEVELNETWORKEYES];
int eye_levels[MAX_CLIENTNETWORKEYES] = {0};
int n_cameras = 0;