/
sv_main.cpp
8260 lines (7095 loc) · 209 KB
/
sv_main.cpp
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
/*
*
* 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
*
* In addition, as a special exception, the author gives permission to
* link the code of this program with the Half-Life Game Engine ("HL
* Engine") and Modified Game Libraries ("MODs") developed by Valve,
* L.L.C ("Valve"). You must obey the GNU General Public License in all
* respects for all of the code used other than the HL Engine and MODs
* from Valve. If you modify this file, you may extend this exception
* to your version of the file, but you are not obligated to do so. If
* you do not wish to do so, delete this exception statement from your
* version.
*
*/
#include "precompiled.h"
typedef struct full_packet_entities_s
{
int num_entities;
entity_state_t entities[MAX_PACKET_ENTITIES];
} full_packet_entities_t;
int sv_lastnum;
extra_baselines_t g_sv_instance_baselines;
delta_t *g_pplayerdelta;
delta_t *g_pentitydelta;
delta_t *g_pcustomentitydelta;
delta_t *g_pclientdelta;
delta_t *g_pweapondelta;
#ifdef REHLDS_OPT_PEDANTIC
delta_t *g_pusercmddelta;
#endif
int hashstrings_collisions;
char *pr_strings;
qboolean scr_skipupdate;
float scr_centertime_off;
float g_LastScreenUpdateTime;
globalvars_t gGlobalVariables;
server_static_t g_psvs;
server_t g_psv;
rehlds_server_t g_rehlds_sv;
decalname_t sv_decalnames[MAX_BASE_DECALS];
int sv_decalnamecount;
UserMsg *sv_gpNewUserMsgs;
UserMsg *sv_gpUserMsgs;
playermove_t g_svmove;
qboolean g_bOutOfDateRestart;
delta_info_t *g_sv_delta;
delta_t *g_peventdelta;
//int num_servers;
int gPacketSuppressed;
char localinfo[MAX_LOCALINFO];
char localmodels[MAX_MODELS][5];
ipfilter_t ipfilters[MAX_IPFILTERS];
int numipfilters;
userfilter_t userfilters[MAX_USERFILTERS];
int numuserfilters;
//cvar_t sv_language;
//cvar_t laddermode;
int sv_playermodel;
//int player_datacounts[32];
char outputbuf[MAX_ROUTEABLE_PACKET];
redirect_t sv_redirected;
netadr_t sv_redirectto;
GameType_e g_eGameType = GT_Unitialized;
qboolean allow_cheats;
char *gNullString = "";
int SV_UPDATE_BACKUP = SINGLEPLAYER_BACKUP;
int SV_UPDATE_MASK = (SINGLEPLAYER_BACKUP - 1);
// With `REHLDS_FIXES` enabled we can simply use `svc_startofusermessages` instead of the hard-coded constant
#ifdef REHLDS_FIXES
int giNextUserMsg = svc_startofusermessages;
#else // REHLDS_FIXES
int giNextUserMsg = 64;
#endif // REHLDS_FIXES
cvar_t sv_lan = { "sv_lan", "0", 0, 0.0f, NULL };
cvar_t sv_lan_rate = { "sv_lan_rate", "20000.0", 0, 0.0f, NULL };
cvar_t sv_aim = { "sv_aim", "1", FCVAR_SERVER | FCVAR_ARCHIVE , 0.0f, NULL };
cvar_t sv_skycolor_r = { "sv_skycolor_r", "0", 0, 0.0f, NULL };
cvar_t sv_skycolor_g = { "sv_skycolor_g", "0", 0, 0.0f, NULL };
cvar_t sv_skycolor_b = { "sv_skycolor_b", "0", 0, 0.0f, NULL };
cvar_t sv_skyvec_x = { "sv_skyvec_x", "0", 0, 0.0f, NULL };
cvar_t sv_skyvec_y = { "sv_skyvec_y", "0", 0, 0.0f, NULL };
cvar_t sv_skyvec_z = { "sv_skyvec_z", "0", 0, 0.0f, NULL };
cvar_t sv_spectatormaxspeed = { "sv_spectatormaxspeed", "500", 0, 0.0f, NULL };
cvar_t sv_airaccelerate = { "sv_airaccelerate", "10", FCVAR_SERVER, 0.0f, NULL };
cvar_t sv_wateraccelerate = { "sv_wateraccelerate", "10", FCVAR_SERVER, 0.0f, NULL };
cvar_t sv_waterfriction = { "sv_waterfriction", "1", FCVAR_SERVER, 0.0f, NULL };
cvar_t sv_zmax = { "sv_zmax", "4096", FCVAR_SPONLY, 0.0f, NULL };
cvar_t sv_wateramp = { "sv_wateramp", "0", 0, 0.0f, NULL };
cvar_t sv_skyname = { "sv_skyname", "desert", 0, 0.0f, NULL };
cvar_t mapcyclefile = { "mapcyclefile", "mapcycle.txt", 0, 0.0f, NULL };
cvar_t motdfile = { "motdfile", "motd.txt", 0, 0.0f, NULL };
cvar_t servercfgfile = { "servercfgfile", "server.cfg", 0, 0.0f, NULL };
cvar_t lservercfgfile = { "lservercfgfile", "listenserver.cfg", 0, 0.0f, NULL };
cvar_t logsdir = { "logsdir", "logs", 0, 0.0f, NULL };
cvar_t bannedcfgfile = { "bannedcfgfile", "banned.cfg", 0, 0.0f, NULL };
int g_userid = 1;
cvar_t rcon_password = { "rcon_password", "", 0, 0.0f, NULL };
cvar_t sv_enableoldqueries = { "sv_enableoldqueries", "0", 0, 0.0f, NULL };
cvar_t sv_instancedbaseline = { "sv_instancedbaseline", "1", 0, 0.0f, NULL };
cvar_t sv_contact = { "sv_contact", "", FCVAR_SERVER, 0.0f, NULL };
cvar_t sv_maxupdaterate = { "sv_maxupdaterate", "30.0", 0, 0.0f, NULL };
cvar_t sv_minupdaterate = { "sv_minupdaterate", "10.0", 0, 0.0f, NULL };
cvar_t sv_filterban = { "sv_filterban", "1", 0, 0.0f, NULL };
cvar_t sv_minrate = { "sv_minrate", "0", FCVAR_SERVER, 0.0f, NULL };
cvar_t sv_maxrate = { "sv_maxrate", "0", FCVAR_SERVER, 0.0f, NULL };
cvar_t sv_logrelay = { "sv_logrelay", "0", 0, 0.0f, NULL };
cvar_t violence_hblood = { "violence_hblood", "1", 0, 0.0f, NULL };
cvar_t violence_ablood = { "violence_ablood", "1", 0, 0.0f, NULL };
cvar_t violence_hgibs = { "violence_hgibs", "1", 0, 0.0f, NULL };
cvar_t violence_agibs = { "violence_agibs", "1", 0, 0.0f, NULL };
cvar_t sv_newunit = { "sv_newunit", "0", 0, 0.0f, NULL };
cvar_t sv_clienttrace = { "sv_clienttrace", "1", FCVAR_SERVER, 0.0f, NULL };
cvar_t sv_timeout = { "sv_timeout", "60", 0, 0.0f, NULL };
cvar_t sv_failuretime = { "sv_failuretime", "0.5", 0, 0.0f, NULL };
cvar_t sv_cheats = { "sv_cheats", "0", FCVAR_SERVER, 0.0f, NULL };
cvar_t sv_password = { "sv_password", "", FCVAR_SERVER | FCVAR_PROTECTED, 0.0f, NULL };
cvar_t sv_proxies = { "sv_proxies", "1", FCVAR_SERVER, 0.0f, NULL };
cvar_t sv_outofdatetime = { "sv_outofdatetime", "1800", 0, 0.0f, NULL };
cvar_t mapchangecfgfile = { "mapchangecfgfile", "", 0, 0.0f, NULL };
cvar_t sv_allow_download = { "sv_allowdownload", "1", 0, 0.0f, NULL };
cvar_t sv_send_logos = { "sv_send_logos", "1", 0, 0.0f, NULL };
cvar_t sv_send_resources = { "sv_send_resources", "1", 0, 0.0f, NULL };
cvar_t sv_log_singleplayer = { "sv_log_singleplayer", "0", 0, 0.0f, NULL };
cvar_t sv_logsecret = { "sv_logsecret", "0", 0, 0.0f, NULL };
cvar_t sv_log_onefile = { "sv_log_onefile", "0", 0, 0.0f, NULL };
cvar_t sv_logbans = { "sv_logbans", "0", 0, 0.0f, NULL };
cvar_t sv_allow_upload = { "sv_allowupload", "1", FCVAR_SERVER, 0.0f, NULL };
cvar_t sv_max_upload = { "sv_uploadmax", "0.5", FCVAR_SERVER, 0.0f, NULL };
cvar_t hpk_maxsize = { "hpk_maxsize", "4", FCVAR_ARCHIVE, 0.0f, NULL };
cvar_t sv_visiblemaxplayers = { "sv_visiblemaxplayers", "-1", 0, 0.0f, NULL };
cvar_t sv_downloadurl = { "sv_downloadurl", "", FCVAR_PROTECTED, 0.0f, NULL };
cvar_t sv_allow_dlfile = { "sv_allow_dlfile", "1", 0, 0.0f, NULL };
#ifdef REHLDS_FIXES
cvar_t sv_version = { "sv_version", "", FCVAR_SERVER, 0.0f, NULL };
#else
cvar_t sv_version = {"sv_version", "", 0, 0.0f, NULL};
#endif
cvar_t sv_rcon_minfailures = { "sv_rcon_minfailures", "5", 0, 0.0f, NULL };
cvar_t sv_rcon_maxfailures = { "sv_rcon_maxfailures", "10", 0, 0.0f, NULL };
cvar_t sv_rcon_minfailuretime = { "sv_rcon_minfailuretime", "30", 0, 0.0f, NULL };
cvar_t sv_rcon_banpenalty = { "sv_rcon_banpenalty", "0", 0, 0.0f, NULL };
cvar_t scr_downloading = { "scr_downloading", "0", 0, 0.0f, NULL };
#ifdef REHLDS_FIXES
cvar_t sv_echo_unknown_cmd = { "sv_echo_unknown_cmd", "0", 0, 0.0f, NULL };
cvar_t sv_auto_precache_sounds_in_models = { "sv_auto_precache_sounds_in_models", "0", 0, 0.0f, nullptr };
cvar_t sv_delayed_spray_upload = { "sv_delayed_spray_upload", "0", 0, 0.0f, nullptr };
cvar_t sv_rehlds_force_dlmax = { "sv_rehlds_force_dlmax", "0", 0, 0.0f, nullptr };
cvar_t listipcfgfile = { "listipcfgfile", "listip.cfg", 0, 0.0f, nullptr };
cvar_t syserror_logfile = { "syserror_logfile", "sys_error.log", 0, 0.0f, nullptr };
cvar_t sv_rehlds_hull_centering = { "sv_rehlds_hull_centering", "0", 0, 0.0f, nullptr };
cvar_t sv_rcon_condebug = { "sv_rcon_condebug", "1", 0, 1.0f, nullptr };
cvar_t sv_rehlds_userinfo_transmitted_fields = { "sv_rehlds_userinfo_transmitted_fields", "", 0, 0.0f, nullptr };
cvar_t sv_rehlds_attachedentities_playeranimationspeed_fix = {"sv_rehlds_attachedentities_playeranimationspeed_fix", "0", 0, 0.0f, nullptr};
cvar_t sv_rehlds_local_gametime = {"sv_rehlds_local_gametime", "0", 0, 0.0f, nullptr};
cvar_t sv_rehlds_send_mapcycle = { "sv_rehlds_send_mapcycle", "0", 0, 0.0f, nullptr };
cvar_t sv_rehlds_maxclients_from_single_ip = { "sv_rehlds_maxclients_from_single_ip", "5", 0, 5.0f, nullptr };
cvar_t sv_use_entity_file = { "sv_use_entity_file", "0", 0, 0.0f, nullptr };
cvar_t sv_usercmd_custom_random_seed = { "sv_usercmd_custom_random_seed", "0", 0, 0.0f, nullptr };
#endif
delta_t *SV_LookupDelta(char *name)
{
delta_info_t *p = g_sv_delta;
while (p)
{
if (!Q_stricmp(name, p->name))
{
return p->delta;
}
p = p->next;
}
Sys_Error("%s: Couldn't find delta for %s\n", __func__, name);
}
NOXREF void SV_DownloadingModules(void)
{
NOXREFCHECK;
char message[] = "Downloading Security Module from Speakeasy.net ...\n";
Con_Printf(message);
#ifndef SWDS
// TODO: Possibly some client-side code here
#endif // SWDS
}
void SV_GatherStatistics(void)
{
int players = 0;
if (g_psvs.next_cleartime < realtime)
{
SV_CountPlayers(&players);
g_psvs.next_cleartime = realtime + 3600.0;
Q_memset(&g_psvs.stats, 0, sizeof(g_psvs.stats));
g_psvs.stats.maxusers = players;
g_psvs.stats.minusers = players;
return;
}
if (g_psvs.next_sampletime > realtime)
return;
SV_CountPlayers(&players);
g_psvs.stats.num_samples++;
g_psvs.next_sampletime = realtime + 60.0;
g_psvs.stats.cumulative_occupancy += (players * 100.0 / g_psvs.maxclients);
if (g_psvs.stats.num_samples >= 1)
g_psvs.stats.occupancy = g_psvs.stats.cumulative_occupancy / g_psvs.stats.num_samples;
if (g_psvs.stats.minusers > players)
g_psvs.stats.minusers = players;
else if (g_psvs.stats.maxusers < players)
g_psvs.stats.maxusers = players;
if ((g_psvs.maxclients - 1) <= players)
g_psvs.stats.at_capacity++;
if (players <= 1)
g_psvs.stats.at_empty++;
if (g_psvs.stats.num_samples >= 1)
{
g_psvs.stats.capacity_percent = g_psvs.stats.at_capacity * 100.0 / g_psvs.stats.num_samples;
g_psvs.stats.empty_percent = g_psvs.stats.at_empty * 100.0 / g_psvs.stats.num_samples;
}
int c = 0;
float v = 0.0f;
client_t *cl = g_psvs.clients;
for (int i = g_psvs.maxclients; i > 0; i--, cl++)
{
if (cl->active && !cl->fakeclient)
{
c++;
v += cl->latency;
}
}
if (c)
v = v / c;
g_psvs.stats.cumulative_latency += v;
if (g_psvs.stats.num_samples >= 1)
g_psvs.stats.average_latency = g_psvs.stats.cumulative_latency / g_psvs.stats.num_samples;
if (g_psvs.stats.num_sessions >= 1)
g_psvs.stats.average_session_len = g_psvs.stats.cumulative_sessiontime / g_psvs.stats.num_sessions;
}
void SV_DeallocateDynamicData(void)
{
if (g_moved_edict)
Mem_Free(g_moved_edict);
if (g_moved_from)
Mem_Free(g_moved_from);
g_moved_edict = NULL;
g_moved_from = NULL;
}
void SV_ReallocateDynamicData(void)
{
if (!g_psv.max_edicts)
{
Con_DPrintf("%s: sv.max_edicts == 0\n", __func__);
return;
}
int nSize = g_psv.max_edicts;
if (g_moved_edict)
{
Con_Printf("Reallocate on moved_edict\n");
#ifdef REHLDS_FIXES
Mem_Free(g_moved_edict);
#endif
}
g_moved_edict = (edict_t **)Mem_ZeroMalloc(sizeof(edict_t *) * nSize);
if (g_moved_from)
{
Con_Printf("Reallocate on moved_from\n");
#ifdef REHLDS_FIXES
Mem_Free(g_moved_from);
#endif
}
g_moved_from = (vec3_t *)Mem_ZeroMalloc(sizeof(vec3_t) * nSize);
}
void SV_AllocClientFrames(void)
{
client_t *cl = g_psvs.clients;
for (int i = 0; i < g_psvs.maxclientslimit; i++, cl++)
{
if (cl->frames)
{
Con_DPrintf("Allocating over frame pointer?\n");
#ifdef REHLDS_FIXES
Mem_Free(cl->frames);
#endif
}
cl->frames = (client_frame_t *)Mem_ZeroMalloc(sizeof(client_frame_t) * SV_UPDATE_BACKUP);
}
}
qboolean SV_IsPlayerIndex(int index)
{
return (index >= 1 && index <= g_psvs.maxclients);
}
#ifdef _WIN32
qboolean __declspec(naked) SV_IsPlayerIndex_wrapped(int index)
{
// Original SV_IsPlayerIndex in swds.dll doesn't modify ecx nor edx.
// During the compilation of original swds.dll compiler was assuming that these registers wouldn't be modified during call to SV_IsPlayerIndex().
// This is not true for code produced by msvc2013 (which uses ecx even in Release config). That's why we need a wrapper here that preserves ecx and edx before call to reversed SV_IsPlayerIndex().
__asm
{
mov eax, dword ptr[esp + 4];
push ecx;
push edx;
push eax;
call SV_IsPlayerIndex;
add esp, 4;
pop edx;
pop ecx;
retn;
}
}
#else // _WIN32
qboolean SV_IsPlayerIndex_wrapped(int index)
{
return SV_IsPlayerIndex(index);
}
#endif // _WIN32
void SV_ClearPacketEntities(client_frame_t *frame)
{
if (frame)
{
if (frame->entities.entities)
Mem_Free(frame->entities.entities);
frame->entities.entities = NULL;
frame->entities.num_entities = 0;
}
}
void SV_AllocPacketEntities(client_frame_t *frame, int numents)
{
if (frame)
{
#ifdef REHLDS_OPT_PEDANTIC
frame->entities.num_entities = numents;
// only alloc for max possible numents
if (!frame->entities.entities)
frame->entities.entities = (entity_state_t *)Mem_ZeroMalloc(sizeof(entity_state_t) * MAX_PACKET_ENTITIES);
#else // REHLDS_OPT_PEDANTIC
if (frame->entities.entities)
SV_ClearPacketEntities(frame);
int allocatedslots = numents;
if (numents < 1)
allocatedslots = 1;
frame->entities.num_entities = numents;
frame->entities.entities = (entity_state_t *)Mem_ZeroMalloc(sizeof(entity_state_t) * allocatedslots);
#endif // REHLDS_OPT_PEDANTIC
}
}
void SV_ClearFrames(client_frame_t **frames)
{
client_frame_t *pframe = *frames;
if (pframe)
{
for (int i = 0; i < SV_UPDATE_BACKUP; i++, pframe++)
{
SV_ClearPacketEntities(pframe);
pframe->senttime = 0.0;
pframe->ping_time = -1.0f;
}
Mem_Free(*frames);
*frames = NULL;
}
}
void SV_Serverinfo_f(void)
{
if (Cmd_Argc() == 1)
{
Con_Printf("Server info settings:\n");
Info_Print(Info_Serverinfo());
return;
}
if (Cmd_Argc() != 3)
{
Con_Printf("usage: serverinfo [ <key> <value> ]\n");
return;
}
if (Cmd_Argv(1)[0] == '*')
{
Con_Printf("Star variables cannot be changed.\n");
return;
}
#ifdef REHLDS_FIXES
Info_SetValueForKey(Info_Serverinfo(), Cmd_Argv(1), Cmd_Argv(2), MAX_INFO_STRING); // Do it right
#else // REHLDS_FIXES
Info_SetValueForKey(Info_Serverinfo(), Cmd_Argv(1), Cmd_Argv(2), 512);
#endif // REHLDS_FIXES
cvar_t *var = Cvar_FindVar(Cmd_Argv(1));
if (var)
{
Z_Free(var->string);
var->string = CopyString((char *)Cmd_Argv(2));
var->value = Q_atof(var->string);
}
SV_BroadcastCommand("fullserverinfo \"%s\"\n", Info_Serverinfo());
}
void SV_Localinfo_f(void)
{
if (Cmd_Argc() == 1)
{
Con_Printf("Local info settings:\n");
Info_Print(localinfo);
return;
}
if (Cmd_Argc() != 3)
{
Con_Printf("usage: localinfo [ <key> <value> ]\n");
return;
}
if (Cmd_Argv(1)[0] == '*')
{
Con_Printf("Star variables cannot be changed.\n");
return;
}
Info_SetValueForKey(localinfo, Cmd_Argv(1), Cmd_Argv(2), MAX_INFO_STRING * 128);
}
void SV_User_f(void)
{
if (!g_psv.active)
{
Con_Printf("Can't 'user', not running a server\n");
return;
}
if (Cmd_Argc() != 2)
{
Con_Printf("Usage: user <username / userid>\n");
return;
}
int uid = Q_atoi(Cmd_Argv(1));
client_t *cl = g_psvs.clients;
for (int i = 0; i < g_psvs.maxclients; i++, cl++)
{
if (cl->active || cl->spawned || cl->connected)
{
if (!cl->fakeclient && cl->name[0])
{
if (cl->userid == uid || !Q_strcmp(cl->name, Cmd_Argv(1)))
{
Info_Print(cl->userinfo);
return;
}
}
}
}
Con_Printf("User not in server.\n");
}
void SV_Users_f(void)
{
if (!g_psv.active)
{
Con_Printf("Can't 'users', not running a server\n");
return;
}
Con_Printf("userid : uniqueid : name\n");
Con_Printf("------ : ---------: ----\n");
int c = 0;
client_t *cl = g_psvs.clients;
for (int i = 0; i < g_psvs.maxclients; i++, cl++)
{
if (cl->active || cl->spawned || cl->connected)
{
if (!cl->fakeclient && cl->name[0])
{
Con_Printf("%6i : %s : %s\n", cl->userid, SV_GetClientIDString(cl), cl->name);
c++;
}
}
}
Con_Printf("%i users\n", c);
}
void SV_CountPlayers(int *clients)
{
int count = 0;
client_s *cl = g_psvs.clients;
for (int i = 0; i < g_psvs.maxclients; i++, cl++)
{
if (cl->active | cl->spawned | cl->connected)
count++;
}
*clients = count;
}
void SV_CountProxies(int *proxies)
{
*proxies = 0;
client_s *cl = g_psvs.clients;
for (int i = 0; i < g_psvs.maxclients; i++, cl++)
{
if (cl->active || cl->spawned || cl->connected)
{
if (cl->proxy)
(*proxies)++;
}
}
}
void SV_FindModelNumbers(void)
{
sv_playermodel = -1;
for (int i = 0; i < MAX_MODELS; i++)
{
if (!g_psv.model_precache[i])
break;
// use case-sensitive names to increase performance
#ifdef REHLDS_FIXES
if (!Q_strcmp(g_psv.model_precache[i], "models/player.mdl"))
sv_playermodel = i;
#else
if (!Q_stricmp(g_psv.model_precache[i], "models/player.mdl"))
sv_playermodel = i;
#endif
}
}
void SV_StartParticle(const vec_t *org, const vec_t *dir, int color, int count)
{
if (g_psv.datagram.cursize + 16 <= g_psv.datagram.maxsize)
{
MSG_WriteByte(&g_psv.datagram, svc_particle);
MSG_WriteCoord(&g_psv.datagram, org[0]);
MSG_WriteCoord(&g_psv.datagram, org[1]);
MSG_WriteCoord(&g_psv.datagram, org[2]);
for (int i = 0; i < 3; i++)
{
MSG_WriteChar(&g_psv.datagram, Q_clamp((int)(dir[i] * 16.0f), -128, 127));
}
MSG_WriteByte(&g_psv.datagram, count);
MSG_WriteByte(&g_psv.datagram, color);
}
}
void SV_StartSound(int recipients, edict_t *entity, int channel, const char *sample, int volume, float attenuation, int fFlags, int pitch)
{
g_RehldsHookchains.m_SV_StartSound.callChain(SV_StartSound_internal, recipients, entity, channel, sample, volume, attenuation, fFlags, pitch);
}
void EXT_FUNC SV_StartSound_internal(int recipients, edict_t *entity, int channel, const char *sample, int volume, float attenuation, int fFlags, int pitch)
{
vec3_t origin;
for (int i = 0; i < 3; i++)
{
origin[i] = (entity->v.maxs[i] + entity->v.mins[i]) * 0.5f + entity->v.origin[i];
}
if (!SV_BuildSoundMsg(entity, channel, sample, volume, attenuation, fFlags, pitch, origin, &g_psv.multicast))
{
return;
}
int flags = 0;
if (recipients == 1)
{
flags = MSG_FL_ONE;
}
qboolean sendPAS = channel != CHAN_STATIC && !(fFlags & SND_FL_STOP);
if (sendPAS)
{
SV_Multicast(entity, origin, flags | MSG_FL_PAS, FALSE);
}
else
{
if (Host_IsSinglePlayerGame())
SV_Multicast(entity, origin, flags | MSG_FL_BROADCAST, FALSE);
else
SV_Multicast(entity, origin, flags | MSG_FL_BROADCAST, TRUE);
}
}
qboolean SV_BuildSoundMsg(edict_t *entity, int channel, const char *sample, int volume, float attenuation, int fFlags, int pitch, const float *origin, sizebuf_t *buffer)
{
int sound_num;
int field_mask;
if (volume < 0 || volume > 255)
{
Con_Printf("%s: volume = %i", __func__, volume);
volume = (volume < 0) ? 0 : 255;
}
if (attenuation < 0.0f || attenuation > 4.0f)
{
Con_Printf("%s: attenuation = %f", __func__, attenuation);
attenuation = (attenuation < 0.0f) ? 0.0f : 4.0f;
}
if (channel < 0 || channel > 7)
{
Con_Printf("%s: channel = %i", __func__, channel);
channel = (channel < 0) ? CHAN_AUTO : CHAN_NETWORKVOICE_BASE;
}
if (pitch < 0 || pitch > 255)
{
Con_Printf("%s: pitch = %i", __func__, pitch);
pitch = (pitch < 0) ? 0 : 255;
}
field_mask = fFlags;
if (*sample == '!')
{
field_mask |= SND_FL_SENTENCE;
sound_num = Q_atoi(sample + 1);
if (sound_num >= CVOXFILESENTENCEMAX)
{
Con_Printf("%s: invalid sentence number: %s", __func__, sample + 1);
return FALSE;
}
}
else if (*sample == '#')
{
field_mask |= SND_FL_SENTENCE;
sound_num = Q_atoi(sample + 1) + CVOXFILESENTENCEMAX;
}
else
{
sound_num = SV_LookupSoundIndex(sample);
if (!sound_num || !g_psv.sound_precache[sound_num])
{
Con_Printf("%s: %s not precached (%d)\n", __func__, sample, sound_num);
return FALSE;
}
}
int ent = NUM_FOR_EDICT(entity);
if (volume != DEFAULT_SOUND_PACKET_VOLUME)
field_mask |= SND_FL_VOLUME;
if (attenuation != DEFAULT_SOUND_PACKET_ATTENUATION)
field_mask |= SND_FL_ATTENUATION;
if (pitch != DEFAULT_SOUND_PACKET_PITCH)
field_mask |= SND_FL_PITCH;
if (sound_num > 255)
field_mask |= SND_FL_LARGE_INDEX;
MSG_WriteByte(buffer, svc_sound);
MSG_StartBitWriting(buffer);
MSG_WriteBits(field_mask, 9);
if (field_mask & SND_FL_VOLUME)
MSG_WriteBits(volume, 8);
if (field_mask & SND_FL_ATTENUATION)
MSG_WriteBits((uint32)(attenuation * 64.0f), 8);
MSG_WriteBits(channel, 3);
MSG_WriteBits(ent, MAX_EDICT_BITS);
MSG_WriteBits(sound_num, (field_mask & SND_FL_LARGE_INDEX) ? 16 : 8);
MSG_WriteBitVec3Coord(origin);
if (field_mask & SND_FL_PITCH)
MSG_WriteBits(pitch, 8);
MSG_EndBitWriting(buffer);
return TRUE;
}
int SV_HashString(const char *string, int iBounds)
{
unsigned int hash = 0;
const char *cc = string;
while (*cc)
{
hash = tolower(*cc) + 2 * hash;
++cc;
}
return hash % iBounds;
}
int EXT_FUNC SV_LookupSoundIndex(const char *sample)
{
int index;
if (!g_psv.sound_precache_hashedlookup_built)
{
if (g_psv.state == ss_loading)
{
for (index = 1; index < MAX_SOUNDS && g_psv.sound_precache[index]; index++) // TODO: why from 1?
{
if (!Q_stricmp(sample, g_psv.sound_precache[index]))
return index;
}
return 0;
}
SV_BuildHashedSoundLookupTable();
}
int starting_index = SV_HashString(sample, 1023);
index = starting_index;
while (g_psv.sound_precache_hashedlookup[index])
{
if (!Q_stricmp(sample, g_psv.sound_precache[g_psv.sound_precache_hashedlookup[index]]))
return g_psv.sound_precache_hashedlookup[index];
index++;
if (index >= MAX_SOUNDS_HASHLOOKUP_SIZE)
index = 0;
if (index == starting_index)
return 0;
}
return 0;
}
void SV_BuildHashedSoundLookupTable(void)
{
Q_memset(g_psv.sound_precache_hashedlookup, 0, sizeof(g_psv.sound_precache_hashedlookup));
for (int sound_num = 0; sound_num < MAX_SOUNDS; sound_num++)
{
if (!g_psv.sound_precache[sound_num])
break;
SV_AddSampleToHashedLookupTable(g_psv.sound_precache[sound_num], sound_num);
}
g_psv.sound_precache_hashedlookup_built = TRUE;
}
void SV_AddSampleToHashedLookupTable(const char *pszSample, int iSampleIndex)
{
int starting_index = SV_HashString(pszSample, MAX_SOUNDS_HASHLOOKUP_SIZE);
int index = starting_index;
while (g_psv.sound_precache_hashedlookup[index])
{
index++;
hashstrings_collisions++;
if (index >= MAX_SOUNDS_HASHLOOKUP_SIZE)
index = 0;
if (index == starting_index)
Sys_Error("%s: NO FREE SLOTS IN SOUND LOOKUP TABLE", __func__);
}
g_psv.sound_precache_hashedlookup[index] = iSampleIndex;
}
qboolean SV_ValidClientMulticast(client_t *client, int soundLeaf, int to)
{
if (Host_IsSinglePlayerGame() || client->proxy)
{
return TRUE;
}
int destination = to & ~(MSG_FL_ONE);
if (destination == MSG_FL_BROADCAST)
{
return TRUE;
}
unsigned char* mask;
if (destination == MSG_FL_PVS)
{
mask = CM_LeafPVS(soundLeaf);
}
else
{
if (destination == MSG_FL_PAS)
{
mask = CM_LeafPAS(soundLeaf);
}
else
{
Con_Printf("MULTICAST: Error %d!\n", to);
return FALSE;
}
}
if (!mask)
{
return TRUE;
}
int bitNumber = SV_PointLeafnum(client->edict->v.origin);
if (mask[(bitNumber - 1) >> 3] & (1 << ((bitNumber - 1) & 7)))
{
return TRUE;
}
return FALSE;
}
void SV_Multicast(edict_t *ent, vec_t *origin, int to, qboolean reliable)
{
client_t *save = host_client;
int leafnum = SV_PointLeafnum(origin);
if (ent && !(host_client && host_client->edict == ent))
{
for (int i = 0; i < g_psvs.maxclients; i++)
{
if (g_psvs.clients[i].edict == ent)
{
host_client = &g_psvs.clients[i];
break;
}
}
}
for (int i = 0; i < g_psvs.maxclients; i++)
{
client_t *client = &g_psvs.clients[i];
if (!client->active)
continue;
if ((to & MSG_FL_ONE) && client == host_client)
continue;
if (ent && ent->v.groupinfo != 0 && client->edict->v.groupinfo != 0)
{
if (g_groupop)
{
if (g_groupop == GROUP_OP_NAND && (ent->v.groupinfo & client->edict->v.groupinfo))
continue;
}
else
{
if (!(ent->v.groupinfo & client->edict->v.groupinfo))
continue;
}
}
if (SV_ValidClientMulticast(client, leafnum, to))
{
sizebuf_t *pBuffer = &client->netchan.message;
if (!reliable)
pBuffer = &client->datagram;
#ifdef REHLDS_FIXES
if (pBuffer->cursize + g_psv.multicast.cursize <= pBuffer->maxsize) // TODO: Should it be <= ? I think so.
#else // REHLDS_FIXES
if (pBuffer->cursize + g_psv.multicast.cursize < pBuffer->maxsize)
#endif // REHLDS_FIXES
SZ_Write(pBuffer, g_psv.multicast.data, g_psv.multicast.cursize);
}
else
{
gPacketSuppressed += g_psv.multicast.cursize;
}
}
SZ_Clear(&g_psv.multicast);
host_client = save;
}
void EXT_FUNC SV_WriteMovevarsToClient(sizebuf_t *message)
{
MSG_WriteByte(message, svc_newmovevars);
MSG_WriteFloat(message, movevars.gravity);
MSG_WriteFloat(message, movevars.stopspeed);
MSG_WriteFloat(message, movevars.maxspeed);
MSG_WriteFloat(message, movevars.spectatormaxspeed);
MSG_WriteFloat(message, movevars.accelerate);
MSG_WriteFloat(message, movevars.airaccelerate);
MSG_WriteFloat(message, movevars.wateraccelerate);
MSG_WriteFloat(message, movevars.friction);
MSG_WriteFloat(message, movevars.edgefriction);
MSG_WriteFloat(message, movevars.waterfriction);
MSG_WriteFloat(message, movevars.entgravity);
MSG_WriteFloat(message, movevars.bounce);
MSG_WriteFloat(message, movevars.stepsize);
MSG_WriteFloat(message, movevars.maxvelocity);
MSG_WriteFloat(message, movevars.zmax);
MSG_WriteFloat(message, movevars.waveHeight);
MSG_WriteByte(message, movevars.footsteps != 0);
MSG_WriteFloat(message, movevars.rollangle);
MSG_WriteFloat(message, movevars.rollspeed);
MSG_WriteFloat(message, movevars.skycolor_r);
MSG_WriteFloat(message, movevars.skycolor_g);
MSG_WriteFloat(message, movevars.skycolor_b);
MSG_WriteFloat(message, movevars.skyvec_x);
MSG_WriteFloat(message, movevars.skyvec_y);
MSG_WriteFloat(message, movevars.skyvec_z);
MSG_WriteString(message, movevars.skyName);
}
void EXT_FUNC SV_WriteDeltaDescriptionsToClient(sizebuf_t *msg)
{
int i, c;
delta_description_t nulldesc;
Q_memset(&nulldesc, 0, sizeof(nulldesc));
for (delta_info_t *p = g_sv_delta; p != NULL; p = p->next)
{
MSG_WriteByte(msg, svc_deltadescription);
MSG_WriteString(msg, p->name);
MSG_StartBitWriting(msg);
c = p->delta->fieldCount;
MSG_WriteBits(c, 16);
for (i = 0; i < c; i++)
{
DELTA_WriteDelta((byte *)&nulldesc, (byte *)&p->delta->pdd[i], TRUE, (delta_t *)&g_MetaDelta, NULL);
}
MSG_EndBitWriting(msg);
}