-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatSounds.as
1628 lines (1315 loc) · 51.1 KB
/
ChatSounds.as
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
#include "Stats"
#include "MicSounds"
#include "brap"
// . doesnt work in spectator for loaded sounds
// config
const string g_SoundFile = "scripts/plugins/cfg/ChatSounds.txt"; // permanently precached sounds
const string g_extraSoundFile = "scripts/plugins/cfg/ChatSounds_extra.txt"; // sounds that players can choose to precache
const string soundStatsFile = "scripts/plugins/store/cs_stats.txt"; // .csstats
const string soundListsFile = "scripts/plugins/store/cs_lists.txt"; // personal sound lists
const string g_spk_folder = "scripts/plugins/temp"; // path to sound files converted to NetworkMessage format
const string micsound_file = "scripts/plugins/store/_tocs.txt";
const string g_soundlist_link = "";
const uint g_BaseDelay = 6666;
const array<string> g_sprites = {'sprites/flower.spr', 'sprites/nyanpasu2.spr', 'sprites/flowergag.spr'};
const uint MAX_PERSONAL_SOUNDS = 8;
const float CSMIC_SOUND_TIMEOUT = 1.0f; // wait this many seconds before giving up on waiting for a sound to convert
const float CSMIC_VOLUME = 22; // global volume setting for sounds played over mic, 15 = 100% volume of normal sounds
const float CSRELIABLE_DELAY = 10.0f; // seconds to wait after player join to enable reliable packets (fixes overflows/desyncs)
/////////
// For .csmic to work, run the steam_voice program with the following arguments:
// "chatsounds" <path_to_micsound_file> <path_to_chatsound_txt> <path_to_Sven_Coop_folder>
class ChatSound {
string trigger;
string fpath;
bool alwaysPrecache;
// for sounds that players need to select
bool isPrecached = false;
bool isStreaming = false; // prevent parallel loading
ChatSound() {}
ChatSound(string trigger, string fpath, bool alwaysPrecache) {
const uint triggerLen = trigger.Length();
// if (trigger == "smithlol")
// g_PlayerFuncs.SayTextAll(null, "Found smith! length for word "+triggerLen+"\n");
for (uint i = 0; i < triggerLen; i++)
{
string triggerSequence = trigger.SubString(0, i+1);
dictionary@ wordList;
if( !g_SoundBeginTrie.exists(triggerSequence) )
{
@wordList = dictionary();
g_SoundBeginTrie[triggerSequence] = @wordList;
}
else
{
@wordList = cast<dictionary@>( g_SoundBeginTrie[triggerSequence] );
}
// wordList.insertLast(trigger);
wordList[trigger] = true;
for (uint j = 1; j < triggerLen-i+1; j++)
{
string containSequence = trigger.SubString(i, j);
// if (trigger == "smithlol")
// g_PlayerFuncs.SayTextAll(null, "Found smith! for word "+containSequence+" "+(i)+":"+j+"\n");
if( !g_SoundContainTrie.exists(containSequence) )
{
@wordList = dictionary();
g_SoundContainTrie[containSequence] = @wordList;
}
else
{
@wordList = cast<dictionary@>( g_SoundContainTrie[containSequence] );
}
// bool alreadyExists = false;
// for(uint k = 0; k < wordList.length(); k++)
// {
// if (wordList[k] == trigger)
// {
// alreadyExists = true;
// break;
// }
// }
// if (!alreadyExists)
// wordList.insertLast(trigger);
if (!wordList.exists(trigger))
wordList[trigger] = true;
}
}
this.trigger = trigger;
this.fpath = fpath;
this.alwaysPrecache = alwaysPrecache;
if (this.alwaysPrecache) {
isPrecached = true;
}
}
}
enum MicModes {
MICMODE_OFF, // don't ever use mic audio to play chatsounds
MICMODE_LOCAL, // use mic audio to play unloaded sounds + attenuate volume
MICMODE_GLOBAL, // use mic audio to play unloaded sounds + max volume everywhere in the map
MICMODE_SUPER_GLOBAL // play all sounds at max volume everywhere in the map
}
class PlayerState {
array<string> soundList;
array<string> muteList;
int micMode = MICMODE_LOCAL;
int volume = 100;
int pitch = 100;
Vector brapColor = Vector(200, 255, 200);
bool reliablePackets = false; // helps with lossy connections
float lastLaggyCmd = 0; // for cooldowns on commands that could lag the serber to death if spammed
float joinTime; // for delaying reliable packets
string lastSound;
SOUND_CHANNEL lastSoundChan;
string lastEmittedSound = ""; // last chatsound played by the user
float lastEmittedTime; // time of last chatsound played by user
}
uint g_Delay = g_BaseDelay;
bool precached = false;
dictionary g_SoundList;
dictionary g_SoundBeginTrie;
dictionary g_SoundContainTrie;
dictionary g_playerStates;
array<uint> g_ChatTimes(33);
array<string> @g_SoundListKeys;
array<string> g_normalSoundKeys;
array<string> g_extraSoundKeys;
size_t filesize;
string g_last_precache_map; // avoid precaching new sounds on restarted maps, or else fastdl will break
array<string> g_last_map_players; // players that were present during the previous level change
bool g_pause_mic_audio = false;
uint g_micsound_id = 0; // used with .cstop.
string g_previous_map = "";
bool g_stats_enabled = false;
CClientCommand g_ListSounds("listsounds", "List all chat sounds", @listsoundscmd);
CClientCommand g_ListSounds2("listsounds2", "List extra chat sounds", @listsounds2cmd);
CClientCommand g_CSPreview("cs", "Chat sound preview", @cspreviewcmd);
CClientCommand g_CSLoad("csload", "Chat sound loader", @csloadcmd);
CClientCommand g_CSUnload("csunload", "Chat sound loader", @csunloadcmd);
CClientCommand g_CSList("cslist", "Show your personal sounds", @listpersonalcmd);
CClientCommand g_CSMic("csmic", "Toggle microphone sound mode", @csmiccmd);
CClientCommand g_CSPitch("cspitch", "Sets the pitch at which your ChatSounds play (25-255)", @cspitch);
CClientCommand g_CSStats("csstats", "Sound usage stats", @cs_stats);
CClientCommand g_CSMute("csmute", "Mute sounds from player", @csmute);
CClientCommand g_CSVol("csvol", "Change sound volume for all players", @csvol);
CClientCommand g_writecsstats("writecsstats", "Write sound usage stats", @writecsstats_cmd, ConCommandFlag::AdminOnly);
CClientCommand g_cspause("cspause", "Pause chatsound mic audio to fix lag", @cspause, ConCommandFlag::AdminOnly);
CClientCommand g_csreliable("csreliable", "Reliable packets for unloaded sounds", @csreliable);
CClientCommand g_cstop("cstop", "Stop playing mic sounds", @cstop);
CConCommand _extMute( "csmute_ext", "Mute from other plugin", @extMute ); // for muting from another plugin
void PluginInit() {
g_Module.ScriptInfo.SetAuthor("incognico + w00tguy");
g_Module.ScriptInfo.SetContactInfo("https://discord.gg/qfZxWAd");
g_Hooks.RegisterHook(Hooks::Player::ClientSay, @ClientSay);
g_Hooks.RegisterHook(Hooks::Player::ClientDisconnect, @ClientDisconnect);
g_Hooks.RegisterHook(Hooks::Player::ClientPutInServer, @ClientPutInServer);
g_Hooks.RegisterHook( Hooks::Game::MapChange, @MapChange );
ReadSounds();
if (g_stats_enabled)
loadUsageStats();
loadPersonalSoundLists();
update_mic_sounds_config_all();
g_Scheduler.SetInterval("brap_think", 0.05f, -1);
}
void PluginExit() {
if (g_stats_enabled)
writeUsageStats();
writePersonalSoundLists();
brap_unload();
}
void stop_crying()
{
for( int i = 1; i <= g_Engine.maxClients; ++i )
{
CBasePlayer@ target = g_PlayerFuncs.FindPlayerByIndex( i );
if (target is null or !target.IsConnected())
continue;
PlayerState@ tstate = getPlayerState(target);
if (g_Engine.time - tstate.lastEmittedTime < 14.f && tstate.lastEmittedSound.SubString(0, 3) == "cry")
{
stop_mic_sound(target);
g_SoundSystem.StopSound(target.edict(), tstate.lastSoundChan, tstate.lastSound);
// g_PlayerFuncs.SayTextAll(null, "Stopped player from crying!\n");
}
}
}
void kiss_effect(EHandle h_plr, int kissLeft) {
CBasePlayer@ plr = cast<CBasePlayer@>(h_plr.GetEntity());
if (plr is null or !plr.IsConnected()) {
return;
}
te_playersprites(plr, "sprites/heart.spr", 2);
if (kissLeft > 0) {
g_Scheduler.SetTimeout("kiss_effect", 0.2f, h_plr, kissLeft-1);
}
}
void te_killplayerattachments(CBasePlayer@ plr, NetworkMessageDest msgType=MSG_BROADCAST, edict_t@ dest=null) {
NetworkMessage m(msgType, NetworkMessages::SVC_TEMPENTITY, dest);
m.WriteByte(TE_KILLPLAYERATTACHMENTS);
m.WriteByte(plr.entindex());
m.End();
}
void MapInit() {
g_ChatTimes.resize(33);
g_any_stats_changed = false;
g_Delay = g_BaseDelay;
if (g_Engine.mapname != g_last_precache_map) {
if (SoundsChanged())
ReadSounds();
updatePrecachedSounds();
g_last_precache_map = g_Engine.mapname;
}
for (uint i = 0; i < g_SoundListKeys.length(); ++i) {
ChatSound@ chatsound = cast<ChatSound@>(g_SoundList[g_SoundListKeys[i]]);
if (!chatsound.isPrecached) {
continue;
}
g_Game.PrecacheGeneric("sound/" + chatsound.fpath);
g_SoundSystem.PrecacheSound(chatsound.fpath);
}
for (uint i = 0; i < g_sprites.length(); ++i) {
g_Game.PrecacheModel(g_sprites[i]);
}
g_Game.PrecacheModel("sprites/heart.spr");
precached = true;
brap_precache();
}
void updatePrecachedSounds() {
for (uint i = 0; i < g_extraSoundKeys.length(); ++i) {
ChatSound@ chatsound = cast<ChatSound@>(g_SoundList[g_extraSoundKeys[i]]);
chatsound.isPrecached = false;
}
for (uint k = 0; k < g_last_map_players.size(); k++) {
string steamId = g_last_map_players[k];
PlayerState@ state = getPlayerState(steamId);
for (uint i = 0; i < state.soundList.size(); i++) {
if (!g_SoundList.exists(state.soundList[i])) {
continue;
}
ChatSound@ chatsound = cast<ChatSound@>(g_SoundList[state.soundList[i]]);
chatsound.isPrecached = true;
}
}
}
PlayerState@ getPlayerState(CBasePlayer@ plr) {
string steamId = g_EngineFuncs.GetPlayerAuthId( plr.edict() );
if (steamId == 'STEAM_ID_LAN') {
steamId = plr.pev.netname;
}
return getPlayerState(steamId);
}
PlayerState@ getPlayerState(string steamId) {
if ( !g_playerStates.exists(steamId) )
{
PlayerState state;
g_playerStates[steamId] = state;
}
return cast<PlayerState@>( g_playerStates[steamId] );
}
void loadPersonalSoundLists() {
DateTime start = DateTime();
File@ file = g_FileSystem.OpenFile(soundListsFile, OpenFile::READ);
if(file !is null && file.IsOpen())
{
while(!file.EOFReached())
{
string line;
file.ReadLine(line);
line.Trim();
if (line.Length() == 0)
continue;
array<string> parts = line.Split("\\");
int micMode = atoi(parts[1]);
if (micMode < 0) micMode = 0;
if (micMode > 3) micMode = 3;
array<string> sounds = (parts[2].Length() > 0) ? parts[2].Split(" ") : array<string>();
string steamid = "STEAM_0:" + parts[0];
PlayerState@ state = getPlayerState(steamid);
state.soundList = sounds;
state.micMode = micMode;
}
file.Close();
} else {
println("chat sound lists file not found: " + soundListsFile + "\n");
}
const float diff = TimeDifference(DateTime(), start).GetTimeDifference();
println("Finished chatsound list load in " + diff + " seconds");
}
void writePersonalSoundLists() {
File@ f = g_FileSystem.OpenFile( soundListsFile, OpenFile::WRITE);
DateTime start = DateTime();
if( f.IsOpen() )
{
array<string> steamIds = g_playerStates.getKeys();
int numWritten = 0;
for (uint i = 0; i < steamIds.size(); i++) {
string steamId = steamIds[i];
string fileSteamId = steamId;
fileSteamId = fileSteamId.SubString(8); // strip STEAM_0:
PlayerState@ state = cast<PlayerState@>(g_playerStates[steamId]);
if (state.soundList.size() == 0 and state.micMode == MICMODE_LOCAL) {
continue;
}
string line = fileSteamId + "\\" + state.micMode + "\\";
for (uint k = 0; k < state.soundList.size(); k++) {
line += (k > 0 ? " " : "") + state.soundList[k];
}
f.Write(line + "\n");
numWritten++;
}
f.Close();
println("Wrote " + numWritten + " personal chatsound lists");
}
else
println("Failed to open chat sound lists file: " + soundListsFile + "\n");
const float diff = TimeDifference(DateTime(), start).GetTimeDifference();
println("Wrote chatsound lists in " + diff + " seconds");
}
HookReturnCode MapChange() {
if (g_stats_enabled)
writeUsageStats();
writePersonalSoundLists();
g_last_map_players.resize(0);
for (int i = 1; i <= g_Engine.maxClients; i++) {
CBasePlayer@ plr = g_PlayerFuncs.FindPlayerByIndex(i);
if (plr is null or !plr.IsConnected()) {
continue;
}
const string steamId = g_EngineFuncs.GetPlayerAuthId(plr.edict());
g_last_map_players.insertLast(steamId);
}
return HOOK_CONTINUE;
}
void ReadSounds() {
g_SoundList.deleteAll();
g_SoundBeginTrie.deleteAll();
g_SoundContainTrie.deleteAll();
g_normalSoundKeys.resize(0);
g_extraSoundKeys.resize(0);
bool parsingExtraSounds = false;
File@ file = g_FileSystem.OpenFile(g_SoundFile, OpenFile::READ);
filesize = file.GetSize();
if (file !is null && file.IsOpen()) {
while(!file.EOFReached()) {
string sLine;
file.ReadLine(sLine);
sLine.Trim();
if (sLine.IsEmpty() or sLine[0] == '/')
continue;
if (sLine.Find("[extra_sounds]") == 0) {
parsingExtraSounds = true;
continue;
}
const array<string> parsed = sLine.Split(" ");
if (parsed.length() < 2)
continue;
ChatSound sound = ChatSound(parsed[0], parsed[1], !parsingExtraSounds);
if (parsingExtraSounds) {
g_extraSoundKeys.insertLast(parsed[0]);
} else {
g_normalSoundKeys.insertLast(parsed[0]);
}
g_SoundList[parsed[0]] = sound;
}
file.Close();
@g_SoundListKeys = g_SoundList.getKeys();
g_SoundListKeys.sortAsc();
g_normalSoundKeys.sortAsc();
g_extraSoundKeys.sortAsc();
}
}
const bool SoundsChanged() {
File@ file = g_FileSystem.OpenFile(g_SoundFile, OpenFile::READ);
const bool changed = (file.GetSize() != filesize) ? true : false;
file.Close();
return changed;
}
void listsounds(CBasePlayer@ plr, const CCommand@ args) {
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, "\nAVAILABLE SOUND TRIGGERS\n");
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, "------------------------\n");
string sMessage = "";
for (uint i = 1; i < g_normalSoundKeys.length()+1; ++i) {
sMessage += g_normalSoundKeys[i-1] + " | ";
if (i % 5 == 0) {
sMessage.Resize(sMessage.Length() -2);
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, sMessage);
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, "\n");
sMessage = "";
}
}
if (sMessage.Length() > 2) {
sMessage.Resize(sMessage.Length() -2);
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, sMessage + "\n");
}
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTCONSOLE, "\nType '.listsounds2' to see extra sounds that can be selectively loaded.\n\n");
}
// sounds that need to be selected before using
void listsounds2(CBasePlayer@ plr, const CCommand@ args) {
array<string> lines;
PlayerState@ state = getPlayerState(plr);
float delta = g_Engine.time - state.lastLaggyCmd;
if (delta < 5 and delta >= 0) {
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTTALK, "Wait a few seconds before using that command.\n");
return;
}
state.lastLaggyCmd = g_Engine.time;
string sMessage = "";
if (args.ArgC() > 1)
{
const string trigger = args.Arg(1);
if (g_SoundBeginTrie.exists(trigger))
{
lines.insertLast("\nSOUND TRIGGERS STARTING WITH '"+trigger+"'\n");
lines.insertLast("------------------------\n");
array<string> triggers = cast<dictionary@>( g_SoundBeginTrie[trigger] ).getKeys();
// triggers.sortAsc();
uint triggersAdded = 0;
uint msgLen = 0;
for (uint i = 0; i < triggers.length(); i++)
{
const string str = triggers[i];
const uint strLen = str.Length();
if (msgLen + strLen > 30)
{
lines.insertLast(sMessage);
lines.insertLast("\n");
sMessage = "";
msgLen = 0;
}
if (msgLen > 0)
sMessage += " | ";
sMessage += str;
msgLen += strLen;
// g_PlayerFuncs.SayTextAll(null, "Found begin trigger: "+triggers[i]+" for keyword: "+trigger+" "+sMessage+"\n");
}
lines.insertLast(sMessage);
lines.insertLast("\n");
sMessage = "";
}
else
{
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTTALK, "\nCouldn't find any sound that begins with that sequence.\n");
}
if (g_SoundContainTrie.exists(trigger))
{
lines.insertLast("\nSOUND TRIGGERS CONTAINING '"+trigger+"'\n");
lines.insertLast("------------------------\n");
array<string> triggers = cast<dictionary@>( g_SoundContainTrie[trigger] ).getKeys();
uint triggersAdded = 0;
uint msgLen = 0;
for (uint i = 0; i < triggers.length(); i++)
{
const string str = triggers[i];
const uint strLen = str.Length();
if (msgLen + strLen > 30)
{
lines.insertLast(sMessage);
lines.insertLast("\n");
sMessage = "";
msgLen = 0;
}
if (msgLen > 0)
sMessage += " | ";
sMessage += str;
msgLen += strLen;
// g_PlayerFuncs.SayTextAll(null, "Found contain trigger: "+triggers[i]+" for keyword: "+trigger+" "+sMessage+"\n");
}
lines.insertLast(sMessage);
lines.insertLast("\n");
sMessage = "";
}
else
{
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTTALK, "Couldn't find any sound that contains with that sequence.\n");
}
lines.insertLast("\n");
delay_print(EHandle(plr), lines, 24);
return;
}
// lines.insertLast("Amount of sounds: "+g_extraSoundKeys.length());
lines.insertLast("\nEXTRA SOUND TRIGGERS\n");
lines.insertLast("------------------------\n");
if (g_soundlist_link.Length() > 0) {
lines.insertLast("View the full sound list in your browser (" + (g_extraSoundKeys.size() + g_normalSoundKeys.size()) + " sounds):\n");
lines.insertLast(g_soundlist_link + "\n\n");
} else {
for (uint i = 1; i < g_extraSoundKeys.length()+1; ++i) {
sMessage += g_extraSoundKeys[i-1] + " | ";
if (i % 5 == 0) {
sMessage.Resize(sMessage.Length() -2);
lines.insertLast(sMessage);
lines.insertLast("\n");
sMessage = "";
}
}
if (sMessage.Length() > 2) {
sMessage.Resize(sMessage.Length() -2);
lines.insertLast(sMessage + "\n");
}
}
lines.insertLast(".listsounds2 sounds are streamed with '.csmic' and can be loaded as normal sounds using '.csload'.\n");
lines.insertLast("Type '.listsounds2 <search text>' to find a sound by name.\n");
lines.insertLast("Type '.listsounds' to see the list of sounds that are always loaded.\n\n");
delay_print(EHandle(plr), lines, 24);
}
void cspreviewcmd(const CCommand@ pArgs) {
CBasePlayer@ pPlayer = g_ConCommandSystem.GetCurrentPlayer();
cspreview(pPlayer, pArgs);
}
void csloadcmd(const CCommand@ pArgs) {
CBasePlayer@ pPlayer = g_ConCommandSystem.GetCurrentPlayer();
csload(pPlayer, pArgs);
}
void csunloadcmd(const CCommand@ pArgs) {
CBasePlayer@ pPlayer = g_ConCommandSystem.GetCurrentPlayer();
csunload(pPlayer, pArgs);
}
void listsoundscmd(const CCommand@ pArgs) {
CBasePlayer@ pPlayer = g_ConCommandSystem.GetCurrentPlayer();
listsounds(pPlayer, pArgs);
}
void listsounds2cmd(const CCommand@ pArgs) {
CBasePlayer@ pPlayer = g_ConCommandSystem.GetCurrentPlayer();
listsounds2(pPlayer, pArgs);
}
void listpersonalcmd(const CCommand@ pArgs) {
CBasePlayer@ pPlayer = g_ConCommandSystem.GetCurrentPlayer();
listpersonal(pPlayer, pArgs);
}
dictionary g_loadedSounds; // defined globally so anon function can see it
void listpersonal(CBasePlayer@ plr, const CCommand@ args) {
PlayerState@ stateCaller = getPlayerState(plr);
float delta = g_Engine.time - stateCaller.lastLaggyCmd;
if (delta < 3 and delta >= 0) {
g_PlayerFuncs.ClientPrint(plr, HUD_PRINTTALK, "Wait a few seconds before using that command.\n");
return;
}
stateCaller.lastLaggyCmd = g_Engine.time;
showPersonalSounds(plr);
g_loadedSounds.deleteAll();
for (int i = 1; i <= g_Engine.maxClients; i++) {
CBasePlayer@ p = g_PlayerFuncs.FindPlayerByIndex(i);
if (p is null or !p.IsConnected()) {
continue;
}
PlayerState@ state = getPlayerState(p);
for (uint k = 0; k < state.soundList.size(); k++) {
if (!g_SoundList.exists(state.soundList[k])) {
continue;
}
if (g_loadedSounds.exists(state.soundList[k])) {
array<string>@ loaders = cast<array<string>@>(g_loadedSounds[state.soundList[k]]);
loaders.insertLast(p.pev.netname);
} else {
array<string> loaders;
loaders.insertLast(p.pev.netname);
g_loadedSounds[state.soundList[k]] = loaders;
}
}
}
array<string> loadedSoundKeys = g_loadedSounds.getKeys();
if (loadedSoundKeys.size() > 0) {
loadedSoundKeys.sort(function(a,b) {
return cast<array<string>@>(g_loadedSounds[a]).size() > cast<array<string>@>(g_loadedSounds[b]).size();
});
}
array<string> printLines;
printLines.insertLast("\nBelow are sounds favorited by active players.\n");
printLines.insertLast("\n Sound Users");
printLines.insertLast("\n--------------------------------------------\n");
int position = 1;
for (uint i = 0; i < loadedSoundKeys.size(); i++) {
string posString = position;
if (position < 100) {
posString = " " + posString;
}
if (position < 10) {
posString = " " + posString;
}
position++;
string line = posString + ") " + loadedSoundKeys[i];
int padding = 20 - loadedSoundKeys[i].Length();
for (int k = 0; k < padding; k++)
line += " ";
array<string>@ userNames = cast<array<string>@>(g_loadedSounds[loadedSoundKeys[i]]);
userNames.sortAsc();
string userStr;
for (uint k = 0; k < userNames.size(); k++) {
string userName = userNames[k];
if (userName.Length() > 10) {
userName = userName.SubString(0, 9) + "-";
}
userStr += (k > 0 ? ", " : "") + userNames[k];
}
line += userStr + "\n";
printLines.insertLast(line);
}
delay_print(EHandle(plr), printLines, 12);
}
void cspitch(const CCommand@ pArgs) {
CBasePlayer@ pPlayer = g_ConCommandSystem.GetCurrentPlayer();
const string steamId = g_EngineFuncs.GetPlayerAuthId(pPlayer.edict());
if (pArgs.ArgC() < 2)
return;
setpitch(steamId, pArgs[1], pPlayer);
}
void extMute(const CCommand@ args) {
println("[ChatSounds] extmute " + args[1] + " " + args[2] + " " + args[3]);
CBasePlayer@ muter = g_PlayerFuncs.FindPlayerByIndex(atoi(args[1]));
string targetid = args[2].ToLowercase();
bool shouldMute = atoi(args[3]) != 0;
PlayerState@ state = getPlayerState(muter);
if (shouldMute) {
if (state.muteList.find(targetid) == -1) {
state.muteList.insertLast(targetid);
g_EngineFuncs.ServerCommand("stop_mic_sound " + muter.entindex() + " 0\n");
g_EngineFuncs.ServerExecute();
}
} else {
if (state.muteList.find(targetid) != -1) {
state.muteList.removeAt(state.muteList.find(targetid));
}
}
}
void csmute(const CCommand@ pArgs) {
CBasePlayer@ pPlayer = g_ConCommandSystem.GetCurrentPlayer();
const string steamId = g_EngineFuncs.GetPlayerAuthId(pPlayer.edict());
setmute(steamId, pArgs[1], pPlayer);
}
void csvol(const CCommand@ pArgs) {
CBasePlayer@ pPlayer = g_ConCommandSystem.GetCurrentPlayer();
const string steamId = g_EngineFuncs.GetPlayerAuthId(pPlayer.edict());
setvol(steamId, pArgs[1], pPlayer);
}
void cspause(const CCommand@ pArgs) {
CBasePlayer@ pPlayer = g_ConCommandSystem.GetCurrentPlayer();
g_pause_mic_audio = !g_pause_mic_audio;
if (g_pause_mic_audio) {
g_PlayerFuncs.ClientPrint(pPlayer, HUD_PRINTTALK, "[ChatSounds] Mic audio paused.\n");
} else {
g_PlayerFuncs.ClientPrint(pPlayer, HUD_PRINTTALK, "[ChatSounds] Mic audio resumed.\n");
}
}
void csreliable(const CCommand@ pArgs) {
CBasePlayer@ pPlayer = g_ConCommandSystem.GetCurrentPlayer();
PlayerState@ state = getPlayerState(pPlayer);
state.reliablePackets = !state.reliablePackets;
if (pArgs.ArgC() > 1) {
state.reliablePackets = atoi(pArgs[1]) != 0;
}
update_mic_sounds_config(pPlayer);
g_PlayerFuncs.SayText(pPlayer, "[ChatSounds] Reliable packets " + (state.reliablePackets ? "enabled" : "disabled") + ".\n");
}
void cstop(const CCommand@ pArgs) {
CBasePlayer@ pPlayer = g_ConCommandSystem.GetCurrentPlayer();
g_EngineFuncs.ServerCommand("stop_mic_sound " + pPlayer.entindex() + " 0\n");
g_EngineFuncs.ServerExecute();
}
void csmiccmd(const CCommand@ pArgs) {
CBasePlayer@ pPlayer = g_ConCommandSystem.GetCurrentPlayer();
csmic(pPlayer, pArgs);
}
bool isNumeric(string arg) {
if (arg.Length() == 0) {
return false;
}
if (!isdigit(arg[0]) and arg[0] != "-") {
return false;
}
for (uint i = 1; i < arg.Length(); i++) {
if (!isdigit(arg[i])) {
return false;
}
}
return true;
}
void print(string text) { g_Game.AlertMessage( at_console, text); }
void println(string text) { print(text + "\n"); }
void player_say(CBaseEntity@ plr, string msg) {
NetworkMessage m(MSG_ALL, NetworkMessages::NetworkMessageType(74), null);
m.WriteByte(plr.entindex());
m.WriteByte(2); // tell the client to color the player name according to team
m.WriteString("" + plr.pev.netname + ": " + msg + "\n");
m.End();
// fake the server log line and print
g_Game.AlertMessage(at_logged, "\"%1<%2><%3><player>\" say \"%4\"\n", plr.pev.netname, string(g_EngineFuncs.GetPlayerUserId(plr.edict())), g_EngineFuncs.GetPlayerAuthId(plr.edict()), msg);
g_EngineFuncs.ServerPrint("" + plr.pev.netname + ": " + msg + "\n");
}
/* void player_say_delayed(EHandle h_plr, string msg) {
CBasePlayer@ plr = cast<CBasePlayer@>(h_plr.GetEntity());
if (plr !is null and plr.IsConnected()) {
player_say(plr, msg);
}
} */
void play_chat_sound(CBasePlayer@ speaker, SOUND_CHANNEL channel, ChatSound@ snd, float volume, float attenuation, int pitch) {
PlayerState@ speakerState = getPlayerState(speaker);
speakerState.lastEmittedSound = snd.trigger;
speakerState.lastEmittedTime = g_Engine.time;
string speakerId = g_EngineFuncs.GetPlayerAuthId(speaker.edict());
speakerId = speakerId.ToLowercase();
array<EHandle> micListeners;
for (int i = 1; i <= g_Engine.maxClients; i++) {
CBasePlayer@ plr = g_PlayerFuncs.FindPlayerByIndex(i);
if (plr is null or !plr.IsConnected()) {
continue;
}
PlayerState@ state = getPlayerState(plr);
if (state.muteList.find(speakerId) != -1) {
continue; // player muted the speaker
}
int vol = state.volume;
float localVol = float(vol / 100.0f)*volume;
if (localVol > 0) {
if (snd.isPrecached && state.micMode != MICMODE_SUPER_GLOBAL) {
g_SoundSystem.PlaySound(speaker.edict(), channel, snd.fpath, localVol, attenuation, 0, pitch, plr.entindex());
state.lastSound = snd.fpath;
state.lastSoundChan = channel;
} else if (state.micMode > 0) {
micListeners.insertLast(EHandle(plr));
}
}
}
if (micListeners.size() > 0) {
play_mic_sound(EHandle(speaker), micListeners, snd, pitch);
}
}
void te_playersprites(CBasePlayer@ target,
string sprite="sprites/bubble.spr", uint8 count=16,
NetworkMessageDest msgType=MSG_BROADCAST, edict_t@ dest=null)
{
NetworkMessage m(msgType, NetworkMessages::SVC_TEMPENTITY, dest);
m.WriteByte(TE_PLAYERSPRITES);
m.WriteShort(target.entindex());
m.WriteShort(g_EngineFuncs.ModelIndex(sprite));
m.WriteByte(count);
m.WriteByte(0); // "size variation" - has no effect
m.End();
}
void delayGagIcon(EHandle h_plr) {
CBasePlayer@ plr = cast<CBasePlayer@>(h_plr.GetEntity());
if (plr is null or !plr.IsConnected())
return;
plr.ShowOverheadSprite(g_sprites[2], 51.0f, 1.5f);
}
HookReturnCode ClientSay(SayParameters@ pParams) {
const CCommand@ pArguments = pParams.GetArguments();
CBasePlayer@ pPlayer = pParams.GetPlayer();
PlayerState@ state = getPlayerState(pPlayer);
if (pArguments.ArgC() > 0) {
string soundArgUpper = pArguments.Arg(0);
string soundArg = pArguments.Arg(0).ToLowercase();
string pitchArg = pArguments.ArgC() > 1 ? pArguments.Arg(1) : "";
if (int(pitchArg.Find("%")) != -1) {
pitchArg = pitchArg.Replace("%", "%%");
}
if (soundArg == ".") {
player_say(pPlayer, pParams.GetCommand());
pParams.ShouldHide = true;
stop_mic_sound(pPlayer);
g_SoundSystem.StopSound(pPlayer.edict(), state.lastSoundChan, state.lastSound);
return HOOK_CONTINUE;
}
if (g_SoundList.exists(soundArg)) {
ChatSound@ chatsound = cast<ChatSound@>(g_SoundList[soundArg]);
const string steamId = g_EngineFuncs.GetPlayerAuthId(pPlayer.edict());
const int idx = pPlayer.entindex();
int pitch = state.pitch;
const bool pitchOverride = isNumeric(pitchArg) && pArguments.ArgC() == 2;
const bool allowrelay = ( (!pitchOverride && pArguments.ArgC() >= 2) || (soundArg == 'yes!' || soundArg == 'no!') && pArguments.ArgC() == 1 ); // can't take care of pitch override for yes!/no! here
if (pitchOverride) {
pitch = clampPitch(atoi(pitchArg));
}
if (!chatsound.isPrecached and state.micMode == MICMODE_OFF) {
string msg = "[ChatSounds] '" + soundArg + "' is unloaded. ";
array<string>@ personalSoundList = getPersonalSoundList(pPlayer);
if (personalSoundList.find(soundArg) != -1) {
msg += "Wait for a different map before using it, or enable .csmic";
} else {
msg += "Request it using the .csload command, or enable .csmic";
}
g_PlayerFuncs.ClientPrint(pPlayer, HUD_PRINTNOTIFY, msg + "\n");
// uhoh this code duplicated below
if (allowrelay)
return HOOK_CONTINUE;
else if (pitchOverride)
player_say(pPlayer, soundArgUpper); // hide the pitch modifier
else
player_say(pPlayer, pParams.GetCommand());
pParams.ShouldHide = true;
return HOOK_CONTINUE;
}
const uint t = uint(g_EngineFuncs.Time()*1000);
const uint d = t - g_ChatTimes[idx];
if (d < g_Delay) {
const float w = float(g_Delay - d) / 1000.0f;
g_PlayerFuncs.ClientPrint(pPlayer, HUD_PRINTCENTER, "Wait " + format_float(w) + " seconds\n");
if (allowrelay)
return HOOK_CONTINUE;
else if (pitchOverride)
player_say(pPlayer, soundArgUpper); // hide the pitch modifier
else
player_say(pPlayer, pParams.GetCommand());
te_killplayerattachments(pPlayer);
g_Scheduler.SetTimeout("delayGagIcon", 0.1f, EHandle(pPlayer));
pParams.ShouldHide = true;
}
else {
logSoundStat(pPlayer, soundArg);
g_ChatTimes[idx] = t;
if (soundArg == 'medic' || soundArg == 'meedic') {