-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathvscript_server.cpp
More file actions
4191 lines (3596 loc) · 150 KB
/
vscript_server.cpp
File metadata and controls
4191 lines (3596 loc) · 150 KB
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 � 2008, Valve Corporation, All rights reserved. ========
//
// Purpose:
//
//=============================================================================
#include "cbase.h"
#include "vscript_server.h"
#include "icommandline.h"
#include "tier1/utlbuffer.h"
#include "tier1/fmtstr.h"
#include "filesystem.h"
#include "eventqueue.h"
#include "GameEventListener.h"
#include "gameinterface.h"
#include "functorutils.h"
#include "mapentities.h"
#include "characterset.h"
#include "sceneentity.h" // for exposing scene precache function
#include "isaverestore.h"
#include "gamerules.h"
#include "particle_parse.h"
#include "usermessages.h"
#include "engine/IEngineSound.h"
#include "vscript_utils.h"
#include "netpropmanager.h"
#include "client.h"
#include "tier0/vcrmode.h"
#include "in_buttons.h"
#include "coordsize.h"
#include "team.h"
#ifdef TF_DLL
#include "tf/tf_gamerules.h"
#include "nav_mesh/tf_nav_mesh.h"
#include "nav_mesh/tf_nav_area.h"
#include "NextBot/NextBotLocomotionInterface.h"
#include "bot/tf_bot.h"
#endif
#if defined( _WIN32 ) || defined( POSIX )
#include "vscript_server_nut.h"
#endif
#if defined( PORTAL2_PUZZLEMAKER )
#include "matchmaking/imatchframework.h"
#include "portal2_research_data_tracker.h"
#endif // PORTAL2_PUZZLEMAKER
#ifdef DOTA_DLL
#include "dota_animation.h"
#endif
extern ScriptClassDesc_t * GetScriptDesc( CBaseEntity * );
extern CServerGameDLL g_ServerGameDLL;
// #define VMPROFILE 1
#ifdef VMPROFILE
#define VMPROF_START float debugStartTime = Plat_FloatTime();
#define VMPROF_SHOW( funcname, funcdesc ) DevMsg("***VSCRIPT PROFILE***: %s %s: %6.4f milliseconds\n", (##funcname), (##funcdesc), (Plat_FloatTime() - debugStartTime)*1000.0 );
#else // !VMPROFILE
#define VMPROF_START
#define VMPROF_SHOW
#endif // VMPROFILE
ConVar script_connect_debugger_on_mapspawn( "script_connect_debugger_on_mapspawn", "0" );
ConVar script_attach_debugger_at_startup( "script_attach_debugger_at_startup", "0" );
ConVar script_break_in_native_debugger_on_error( "script_break_in_native_debugger_on_error", "0" );
#define VSCRIPT_CONVAR_ALLOWLIST_NAME "cfg/vscript_convar_allowlist.txt"
/// Exposes convars to script
class CScriptConvarAccessor : public CAutoGameSystem
{
public:
ScriptVariant_t GetBool( const char *cvar );
ScriptVariant_t GetInt( const char *cvar );
ScriptVariant_t GetFloat( const char *cvar );
ScriptVariant_t GetStr( const char *cvar );
const char *GetClientConvarValue( const char *cvar, int entindex );
void SetValue( const char *cvar, ScriptVariant_t value );
void LevelInitPreEntity() OVERRIDE;
void LevelShutdownPostEntity() OVERRIDE;
bool IsConVarOnAllowList( const char *cvar );
CUtlSymbolTable m_AllowedConVars;
};
CScriptConvarAccessor g_ScriptConvars;
#define FCVAR_SCRIPT_NONO ( FCVAR_PROTECTED | FCVAR_SERVER_CANNOT_QUERY )
ScriptVariant_t CScriptConvarAccessor::GetBool( const char *cvar )
{
if ( !cvar || !*cvar )
return ScriptVariant_t();
ConVarRef cref( cvar );
if ( cref.IsValid() && !cref.IsFlagSet( FCVAR_SCRIPT_NONO ) )
{
return cref.GetBool();
}
else
{
return ScriptVariant_t(); // default ctor is NULL
}
}
ScriptVariant_t CScriptConvarAccessor::GetInt( const char *cvar )
{
if ( !cvar || !*cvar )
return ScriptVariant_t();
ConVarRef cref( cvar );
if ( cref.IsValid() && !cref.IsFlagSet( FCVAR_SCRIPT_NONO ) )
{
return cref.GetInt();
}
else
{
return ScriptVariant_t(); // default ctor is NULL
}
}
ScriptVariant_t CScriptConvarAccessor::GetFloat( const char *cvar )
{
if ( !cvar || !*cvar )
return ScriptVariant_t();
ConVarRef cref( cvar );
if ( cref.IsValid() && !cref.IsFlagSet( FCVAR_SCRIPT_NONO ) )
{
return cref.GetFloat();
}
else
{
return ScriptVariant_t(); // default ctor is NULL
}
}
ScriptVariant_t CScriptConvarAccessor::GetStr( const char *cvar )
{
if ( !cvar || !*cvar )
return ScriptVariant_t();
ConVarRef cref( cvar );
if ( cref.IsValid() )
{
if ( cref.IsFlagSet( FCVAR_SCRIPT_NONO ) )
{
// the funny.
return "hunter2";
}
return cref.GetString();
}
else
{
return ScriptVariant_t(); // default ctor is NULL
}
}
const char *CScriptConvarAccessor::GetClientConvarValue( const char *cvar, int entindex )
{
if ( !cvar || !*cvar )
return "";
return engine->GetClientConVarValue( entindex, cvar );
}
void CScriptConvarAccessor::SetValue( const char *cvar, ScriptVariant_t value )
{
if ( !cvar || !*cvar )
return;
if ( !IsConVarOnAllowList( cvar ) )
{
DevMsg( "Convar %s was not in " VSCRIPT_CONVAR_ALLOWLIST_NAME "\n", cvar );
return;
}
ConVarRef cref( cvar );
if ( cref.IsValid() && !cref.IsFlagSet( FCVAR_SCRIPT_NONO ) )
{
bool bSave = true;
switch( value.GetType() )
{
case FIELD_BOOLEAN:
cref.SetValue( (bool)value );
break;
case FIELD_INTEGER:
cref.SetValue( (int)value );
break;
case FIELD_FLOAT:
cref.SetValue( (float)value );
break;
case FIELD_CSTRING:
cref.SetValue( (const char *)value );
break;
default:
Warning( "%s.SetValue() unsupported value type %s\n", cvar, ScriptFieldTypeName( value.GetType() ) );
bSave = false;
break;
}
if ( bSave )
{
GameRules()->SaveConvar( cref );
}
}
}
void CScriptConvarAccessor::LevelInitPreEntity()
{
m_AllowedConVars.RemoveAll();
KeyValues *kv = new KeyValues( "vscript_convar_allowlist" );
bool bLoaded = kv->LoadFromFile( g_pFullFileSystem, VSCRIPT_CONVAR_ALLOWLIST_NAME, "MOD" );
if ( bLoaded )
{
for ( KeyValues *pCurItem = kv->GetFirstValue(); pCurItem; pCurItem = pCurItem->GetNextValue() )
{
const char *pName = pCurItem->GetName();
const char *pValue = pCurItem->GetString();
if ( !V_stricmp( pValue, "allowed" ) )
m_AllowedConVars.AddString( pName );
}
}
if ( !bLoaded )
Warning( "Error loading " VSCRIPT_CONVAR_ALLOWLIST_NAME "\n" );
kv->deleteThis();
}
void CScriptConvarAccessor::LevelShutdownPostEntity()
{
m_AllowedConVars.RemoveAll();
}
bool CScriptConvarAccessor::IsConVarOnAllowList( const char *cvar )
{
if ( !cvar || !*cvar )
return false;
return m_AllowedConVars.Find( cvar ) != UTL_INVAL_SYMBOL;
}
BEGIN_SCRIPTDESC_ROOT_NAMED( CScriptConvarAccessor, "Convars", SCRIPT_SINGLETON "Access to convar functions" )
DEFINE_SCRIPTFUNC( GetBool, "GetBool(name) : returns the convar as a bool. May return null if no such convar." )
DEFINE_SCRIPTFUNC( GetInt, "GetInt(name) : returns the convar as an int. May return null if no such convar." )
DEFINE_SCRIPTFUNC( GetFloat, "GetFloat(name) : returns the convar as a float. May return null if no such convar." )
DEFINE_SCRIPTFUNC( GetStr, "GetStr(name) : returns the convar as a string. May return null if no such convar." )
DEFINE_SCRIPTFUNC( GetClientConvarValue, "GetClientConvarValue(name) : returns the convar value for the entindex as a string." )
DEFINE_SCRIPTFUNC( SetValue, "SetValue(name, value) : sets the value of the convar. The convar must be in " VSCRIPT_CONVAR_ALLOWLIST_NAME " to be set. Supported types are bool, int, float, string." )
DEFINE_SCRIPTFUNC( IsConVarOnAllowList, "IsConVarOnAllowList(name) : checks if the convar is allowed to be used and is in " VSCRIPT_CONVAR_ALLOWLIST_NAME ". Please be nice with this and use it for *compatibility* if you need check support and NOT to force server owners to allow hostname to be set... or else this will simply lie and return true in future. ;-) You have been warned!" )
END_SCRIPTDESC()
//-----------------------------------------------------------------------------
class CScriptEntityOutputs
{
public:
int GetNumElements( HSCRIPT hEntity, const char *szOutputName )
{
CBaseEntity *pBaseEntity = ToEnt( hEntity );
if ( !pBaseEntity )
return -1;
CBaseEntityOutput *pOutput = pBaseEntity->FindNamedOutput( szOutputName );
if ( !pOutput )
return -1;
return pOutput->NumberOfElements();
}
void GetOutputTable( HSCRIPT hEntity, const char *szOutputName, HSCRIPT hOutputTable, int element )
{
CBaseEntity *pBaseEntity = ToEnt( hEntity );
if ( !pBaseEntity || !hOutputTable || element < 0 )
return;
CBaseEntityOutput *pOutput = pBaseEntity->FindNamedOutput( szOutputName );
if ( pOutput )
{
int iCount = 0;
CEventAction *pAction = pOutput->GetFirstAction();
while ( pAction )
{
if ( iCount == element )
{
g_pScriptVM->SetValue( hOutputTable, "target", STRING( pAction->m_iTarget ) );
g_pScriptVM->SetValue( hOutputTable, "input", STRING( pAction->m_iTargetInput ) );
g_pScriptVM->SetValue( hOutputTable, "parameter", STRING( pAction->m_iParameter ) );
g_pScriptVM->SetValue( hOutputTable, "delay", pAction->m_flDelay );
g_pScriptVM->SetValue( hOutputTable, "times_to_fire", pAction->m_nTimesToFire );
break;
}
else
{
iCount++;
pAction = pAction->m_pNext;
}
}
}
}
bool HasOutput( HSCRIPT hEntity, const char *szOutputName )
{
CBaseEntity *pBaseEntity = ToEnt( hEntity );
if ( !pBaseEntity )
return false;
CBaseEntityOutput *pOutput = pBaseEntity->FindNamedOutput( szOutputName );
if ( !pOutput )
return false;
return true;
}
bool HasAction( HSCRIPT hEntity, const char *szOutputName )
{
CBaseEntity *pBaseEntity = ToEnt( hEntity );
if ( !pBaseEntity )
return false;
CBaseEntityOutput *pOutput = pBaseEntity->FindNamedOutput( szOutputName );
if ( pOutput )
{
CEventAction *pAction = pOutput->GetFirstAction();
if ( pAction )
return true;
}
return false;
}
void AddOutput( HSCRIPT hEntity, const char *szOutputName, const char *szTarget, const char *szTargetInput, const char *szParameter, float flDelay, int iTimesToFire )
{
CBaseEntity *pBaseEntity = ToEnt( hEntity );
if ( !pBaseEntity )
return;
CBaseEntityOutput *pOutput = pBaseEntity->FindNamedOutput( szOutputName );
if ( !pOutput )
return;
CEventAction *pAction = new CEventAction( NULL );
pAction->m_iTarget = AllocPooledString( szTarget );
pAction->m_iTargetInput = AllocPooledString( szTargetInput );
pAction->m_iParameter = AllocPooledString( szParameter );
pAction->m_flDelay = flDelay;
pAction->m_nTimesToFire = iTimesToFire;
pOutput->AddEventAction( pAction );
}
void RemoveOutput( HSCRIPT hEntity, const char *szOutputName, const char *szTarget, const char *szTargetInput, const char *szParameter )
{
CBaseEntity *pBaseEntity = ToEnt( hEntity );
if ( !pBaseEntity )
return;
CBaseEntityOutput *pOutput = pBaseEntity->FindNamedOutput( szOutputName );
if ( !pOutput )
return;
if ( V_strcmp( szTarget, "" ) == 0 )
pOutput->DeleteAllElements();
else
{
CEventAction *pAction = pOutput->GetFirstAction();
pOutput->ScriptRemoveEventAction( pAction, szTarget, szTargetInput, szParameter );
}
}
} g_ScriptEntityOutputs;
BEGIN_SCRIPTDESC_ROOT_NAMED( CScriptEntityOutputs, "CScriptEntityOutputs", SCRIPT_SINGLETON "Used to access entity output data" )
DEFINE_SCRIPTFUNC( GetNumElements, "Arguments: ( entity, outputName ) - returns the number of array elements" )
DEFINE_SCRIPTFUNC( GetOutputTable, "Arguments: ( entity, outputName, table, arrayElement ) - returns a table of output information" )
DEFINE_SCRIPTFUNC( HasOutput, "Arguments: ( entity, outputName ) - returns true if the output exists" )
DEFINE_SCRIPTFUNC( HasAction, "Arguments: ( entity, outputName ) - returns true if an action exists for the output" )
DEFINE_SCRIPTFUNC( AddOutput, "Arguments: ( entity, outputName, targetName, inputName, parameter, delay, timesToFire ) - add a new output to the entity" )
DEFINE_SCRIPTFUNC( RemoveOutput, "Arguments: ( entity, outputName, targetName, inputName, parameter ) - remove an output from the entity" )
END_SCRIPTDESC();
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
class CScrollingScreenOverlay
{
public:
CScrollingScreenOverlay( float x = 0.01, float y = 0.0, float duration = FLT_MAX, int iFirstLine = 1, int nLines = 50, int r = 255, int g = 255, int b = 255 );
void SetXY( float x, float y );
void SetTextDuration( float duration );
void SetFirstLine( int iFirstLine );
void SetNumLines( int nLines );
void AddText( const char *pszText, int r, int g, int b );
void AddText( const char *pszText );
void Clear();
void Draw();
private:
struct TextLine_t
{
CUtlString m_text;
float m_time;
int m_r, m_g, m_b;
};
CUtlLinkedList< TextLine_t > m_Text;
float m_x, m_y;
float m_duration;
int m_iFirstLine;
int m_nLines;
int m_r, m_g, m_b;
};
CScrollingScreenOverlay::CScrollingScreenOverlay( float x, float y, float duration, int iFirstLine, int nLines, int r, int g, int b ) :
m_x( x ),
m_y( y ),
m_duration( duration ),
m_iFirstLine( iFirstLine ),
m_nLines( nLines ),
m_r( r ),
m_g( g ),
m_b( b )
{
}
void CScrollingScreenOverlay::SetXY( float x, float y )
{
m_x = x;
m_y = y;
}
void CScrollingScreenOverlay::SetTextDuration( float duration )
{
m_duration = duration;
}
void CScrollingScreenOverlay::SetFirstLine( int iFirstLine )
{
m_iFirstLine = iFirstLine;
}
void CScrollingScreenOverlay::SetNumLines( int nLines )
{
m_nLines = nLines;
}
void CScrollingScreenOverlay::AddText( const char *pszText, int r, int g, int b )
{
while ( m_Text.Count() && m_Text.Count() >= m_nLines )
{
m_Text.Remove( m_Text.Head() );
}
int iNew = m_Text.AddToTail();
m_Text[iNew].m_text = pszText;
m_Text[iNew].m_time = gpGlobals->curtime;
m_Text[iNew].m_r = r;
m_Text[iNew].m_g = g;
m_Text[iNew].m_b = b;
}
void CScrollingScreenOverlay::AddText( const char *pszText )
{
AddText( pszText, m_r, m_g, m_b );
}
void CScrollingScreenOverlay::Clear()
{
m_Text.RemoveAll();
}
void CScrollingScreenOverlay::Draw()
{
if ( developer.GetBool() )
{
int line = m_iFirstLine;
int i;
int alpha;
float age;
while ( ( i = m_Text.Head() ) != m_Text.InvalidIndex() )
{
age = gpGlobals->curtime - m_Text[i].m_time;
if ( age >= m_duration )
{
m_Text.Remove( m_Text.Head() );
}
else
{
break;
}
}
CFmtStrN<1024> msg;
float msgTime;
for ( int i = m_Text.Head(); i != m_Text.InvalidIndex(); i = m_Text.Next( i ) )
{
msgTime = m_Text[i].m_time;
age = gpGlobals->curtime - msgTime;
if ( age <= m_duration - 1.0f )
{
alpha = 255;
}
else
{
alpha = 255 * ( m_duration - age );
}
msg.sprintf( "(%0.2f): %s", msgTime, m_Text[i].m_text.operator const char *() );
NDebugOverlay::ScreenTextLine( m_x, m_y, line++, msg, m_Text[i].m_r, m_Text[i].m_g, m_Text[i].m_b, alpha, NDEBUG_PERSIST_TILL_NEXT_SERVER );
}
}
}
CScrollingScreenOverlay g_ScriptErrorScreenOverlay( 0.01, 0.0, 20.0f, 14, 30, 255, 0, 0 );
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
bool VScriptServerScriptErrorFunc( ScriptErrorLevel_t /*eLevel*/, const char *pszText )
{
if ( script_break_in_native_debugger_on_error.GetBool() )
{
DebuggerBreakIfDebugging();
script_break_in_native_debugger_on_error.SetValue( "0" );
}
if ( developer.GetBool() )
{
char szTemp[1024];
V_strncpy( szTemp, pszText, ARRAYSIZE(szTemp) );
char *pszCurrent = szTemp;
char *pszNewline = pszCurrent;
while ( *pszCurrent )
{
while ( *pszNewline )
{
if ( *pszNewline == '\n' )
{
*pszNewline++ = 0;
break;
}
pszNewline++;
}
g_ScriptErrorScreenOverlay.AddText( pszCurrent );
pszCurrent = pszNewline;
}
}
return true;
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
class CScriptEntityIterator : public IEntityFindFilter
{
public:
HSCRIPT First() { return Next(NULL); }
HSCRIPT Next( HSCRIPT hStartEntity )
{
return ToHScript( gEntList.NextEnt( ToEnt( hStartEntity ) ) );
}
HSCRIPT CreateByClassname( const char *className )
{
return ToHScript( CreateEntityByName( className ) );
}
HSCRIPT FindByClassname( HSCRIPT hStartEntity, const char *szName )
{
return ToHScript( gEntList.FindEntityByClassname( ToEnt( hStartEntity ), szName, this ) );
}
HSCRIPT FindByName( HSCRIPT hStartEntity, const char *szName )
{
return ToHScript( gEntList.FindEntityByName( ToEnt( hStartEntity ), szName, NULL, NULL, NULL, this ) );
}
HSCRIPT FindInSphere( HSCRIPT hStartEntity, const Vector &vecCenter, float flRadius )
{
return ToHScript( gEntList.FindEntityInSphere( ToEnt( hStartEntity ), vecCenter, flRadius, this ) );
}
HSCRIPT FindByTarget( HSCRIPT hStartEntity, const char *szName )
{
return ToHScript( gEntList.FindEntityByTarget( ToEnt( hStartEntity ), szName, this ) );
}
HSCRIPT FindByModel( HSCRIPT hStartEntity, const char *szModelName )
{
return ToHScript( gEntList.FindEntityByModel( ToEnt( hStartEntity ), szModelName, this ) );
}
HSCRIPT FindByNameNearest( const char *szName, const Vector &vecSrc, float flRadius )
{
return ToHScript( gEntList.FindEntityByNameNearest( szName, vecSrc, flRadius, NULL, NULL, NULL, this ) );
}
HSCRIPT FindByNameWithin( HSCRIPT hStartEntity, const char *szName, const Vector &vecSrc, float flRadius )
{
return ToHScript( gEntList.FindEntityByNameWithin( ToEnt( hStartEntity ), szName, vecSrc, flRadius, NULL, NULL, NULL, this ) );
}
HSCRIPT FindByClassnameNearest( const char *szName, const Vector &vecSrc, float flRadius )
{
return ToHScript( gEntList.FindEntityByClassnameNearest( szName, vecSrc, flRadius, this ) );
}
HSCRIPT FindByClassnameWithin( HSCRIPT hStartEntity , const char *szName, const Vector &vecSrc, float flRadius )
{
return ToHScript( gEntList.FindEntityByClassnameWithin( ToEnt( hStartEntity ), szName, vecSrc, flRadius, this ) );
}
void DispatchSpawn( HSCRIPT hEntity )
{
::DispatchSpawn( ToEnt( hEntity ), false );
}
bool ShouldFindEntity( CBaseEntity *pEntity )
{
if ( !pEntity )
return true;
if ( pEntity->IsPlayer() )
{
CBasePlayer *pPlayer = assert_cast< CBasePlayer * >( pEntity );
if ( pPlayer->IsHLTV() )
return false;
}
return true;
}
CBaseEntity *GetFilterResult( void )
{
return NULL;
}
private:
} g_ScriptEntityIterator;
BEGIN_SCRIPTDESC_ROOT_NAMED( CScriptEntityIterator, "CEntities", SCRIPT_SINGLETON "The global list of entities" )
DEFINE_SCRIPTFUNC( First, "Begin an iteration over the list of entities" )
DEFINE_SCRIPTFUNC( Next, "Continue an iteration over the list of entities, providing reference to a previously found entity" )
DEFINE_SCRIPTFUNC( CreateByClassname, "Creates an entity by classname" )
DEFINE_SCRIPTFUNC( FindByClassname, "Find entities by class name. Pass 'null' to start an iteration, or reference to a previously found entity to continue a search" )
DEFINE_SCRIPTFUNC( FindByName, "Find entities by name. Pass 'null' to start an iteration, or reference to a previously found entity to continue a search" )
DEFINE_SCRIPTFUNC( FindInSphere, "Find entities within a radius. Pass 'null' to start an iteration, or reference to a previously found entity to continue a search" )
DEFINE_SCRIPTFUNC( FindByTarget, "Find entities by targetname. Pass 'null' to start an iteration, or reference to a previously found entity to continue a search" )
DEFINE_SCRIPTFUNC( FindByModel, "Find entities by model name. Pass 'null' to start an iteration, or reference to a previously found entity to continue a search" )
DEFINE_SCRIPTFUNC( FindByNameNearest, "Find entities by name nearest to a point." )
DEFINE_SCRIPTFUNC( FindByNameWithin, "Find entities by name within a radius. Pass 'null' to start an iteration, or reference to a previously found entity to continue a search" )
DEFINE_SCRIPTFUNC( FindByClassnameNearest, "Find entities by class name nearest to a point." )
DEFINE_SCRIPTFUNC( FindByClassnameWithin, "Find entities by class name within a radius. Pass 'null' to start an iteration, or reference to a previously found entity to continue a search" )
DEFINE_SCRIPTFUNC( DispatchSpawn, "Dispatches spawn of an entity!" )
END_SCRIPTDESC();
CVScriptGameEventListener g_VScriptGameEventListener;
void CVScriptGameEventListener::Init()
{
m_RunGameEventCallbacksFunc = INVALID_HSCRIPT;
m_CollectGameEventCallbacksFunc = INVALID_HSCRIPT;
m_ScriptHookCallbacksFunc = INVALID_HSCRIPT;
}
void CVScriptGameEventListener::FireGameEvent( IGameEvent *event )
{
// Pass all keyvales as a table of parameters
HSCRIPT paramsTable = ScriptTableFromKeyValues( g_pScriptVM, event->GetDataKeys() );
RunGameEventCallbacks( event->GetName(), paramsTable );
}
void CVScriptGameEventListener::ListenForScriptHook( const char* szName )
{
m_ScriptHooks.AddString( szName );
}
void CVScriptGameEventListener::ClearAllScriptHooks()
{
m_ScriptHooks.RemoveAll();
}
bool CVScriptGameEventListener::HasScriptHook( const char *szName )
{
if ( !szName || !*szName )
return false;
return m_ScriptHooks.Find( szName ).IsValid();
}
bool CVScriptGameEventListener::FireScriptHook( const char *pszHookName, HSCRIPT params )
{
if ( !HasScriptHook( pszHookName ) )
return false;
RunScriptHookCallbacks( pszHookName, params );
return true;
}
// Calls a squirrel func (see vscript_server.nut) to call each
// registered script function associated with this game event.
void CVScriptGameEventListener::RunGameEventCallbacks( const char* szName, HSCRIPT params )
{
Assert( szName );
if ( !szName )
return;
if ( m_RunGameEventCallbacksFunc == INVALID_HSCRIPT )
m_RunGameEventCallbacksFunc = g_pScriptVM->LookupFunction( "__RunGameEventCallbacks" );
if ( m_RunGameEventCallbacksFunc )
{
g_pScriptVM->Call( m_RunGameEventCallbacksFunc, NULL, true, NULL, szName, params );
}
}
void CVScriptGameEventListener::RunScriptHookCallbacks( const char* szName, HSCRIPT params )
{
Assert( szName );
if ( !szName )
return;
if ( m_ScriptHookCallbacksFunc == INVALID_HSCRIPT )
m_ScriptHookCallbacksFunc = g_pScriptVM->LookupFunction( "__RunScriptHookCallbacks" );
if ( m_ScriptHookCallbacksFunc )
{
g_pScriptVM->Call( m_ScriptHookCallbacksFunc, NULL, true, NULL, szName, params );
}
}
void CVScriptGameEventListener::CollectGameEventCallbacksInScope( HSCRIPT scope )
{
if ( m_CollectGameEventCallbacksFunc == INVALID_HSCRIPT )
m_CollectGameEventCallbacksFunc = g_pScriptVM->LookupFunction( "__CollectGameEventCallbacks" );
if ( m_CollectGameEventCallbacksFunc )
{
g_pScriptVM->Call( m_CollectGameEventCallbacksFunc, NULL, true, NULL, scope );
}
}
void RegisterScriptGameEventListener( const char* pszEventName )
{
if ( !pszEventName || !*pszEventName )
{
Log_Warning( LOG_VScript, "No event name specified\n" );
return;
}
g_VScriptGameEventListener.ListenForGameEvent( pszEventName );
}
void RegisterScriptHookListener( const char* pszEventName )
{
if ( !pszEventName || !*pszEventName )
{
Log_Warning( LOG_VScript, "No event name specified\n" );
return;
}
g_VScriptGameEventListener.ListenForScriptHook( pszEventName );
}
void CollectGameEventCallbacksInScope( HSCRIPT scope )
{
g_VScriptGameEventListener.CollectGameEventCallbacksInScope( scope );
}
void ClearScriptGameEventListeners( void )
{
g_VScriptGameEventListener.StopListeningForAllEvents();
g_VScriptGameEventListener.ClearAllScriptHooks();
}
ConVar vscript_script_hooks( "vscript_script_hooks", "1" );
bool ScriptHooksEnabled()
{
return g_pScriptVM && vscript_script_hooks.GetBool();
}
bool ScriptHookEnabled( const char *pszName )
{
if ( !ScriptHooksEnabled() )
return false;
if ( !pszName || !*pszName )
{
Log_Warning( LOG_VScript, "No event name specified\n" );
return false;
}
return g_VScriptGameEventListener.HasScriptHook( pszName );
}
bool RunScriptHook( const char *pszHookName, HSCRIPT params )
{
if ( !pszHookName || !*pszHookName )
{
Log_Warning( LOG_VScript, "No event name specified\n" );
return false;
}
return g_VScriptGameEventListener.FireScriptHook( pszHookName, params );
}
CNetPropManager g_ScriptNetPropManager;
BEGIN_SCRIPTDESC_ROOT_NAMED( CNetPropManager, "CNetPropManager", SCRIPT_SINGLETON "Used to get/set entity network fields" )
DEFINE_SCRIPTFUNC( GetPropInt, "Arguments: ( entity, propertyName )" )
DEFINE_SCRIPTFUNC( GetPropFloat, "Arguments: ( entity, propertyName )" )
DEFINE_SCRIPTFUNC( GetPropVector, "Arguments: ( entity, propertyName )" )
DEFINE_SCRIPTFUNC( GetPropEntity, "Arguments: ( entity, propertyName ) - returns an entity" )
DEFINE_SCRIPTFUNC( GetPropString, "Arguments: ( entity, propertyName )" )
DEFINE_SCRIPTFUNC( SetPropInt, "Arguments: ( entity, propertyName, value )" )
DEFINE_SCRIPTFUNC( SetPropFloat, "Arguments: ( entity, propertyName, value )" )
DEFINE_SCRIPTFUNC( SetPropVector, "Arguments: ( entity, propertyName, value )" )
DEFINE_SCRIPTFUNC( SetPropEntity, "Arguments: ( entity, propertyName, value )" )
DEFINE_SCRIPTFUNC( SetPropString, "Arguments: ( entity, propertyName, value )" )
DEFINE_SCRIPTFUNC( GetPropIntArray, "Arguments: ( entity, propertyName, arrayElement )" )
DEFINE_SCRIPTFUNC( GetPropFloatArray, "Arguments: ( entity, propertyName, arrayElement )" )
DEFINE_SCRIPTFUNC( GetPropVectorArray, "Arguments: ( entity, propertyName, arrayElement )" )
DEFINE_SCRIPTFUNC( GetPropEntityArray, "Arguments: ( entity, propertyName, arrayElement ) - returns an entity" )
DEFINE_SCRIPTFUNC( GetPropStringArray, "Arguments: ( entity, propertyName, arrayElement )" )
DEFINE_SCRIPTFUNC( SetPropIntArray, "Arguments: ( entity, propertyName, value, arrayElement )" )
DEFINE_SCRIPTFUNC( SetPropFloatArray, "Arguments: ( entity, propertyName, value, arrayElement )" )
DEFINE_SCRIPTFUNC( SetPropVectorArray, "Arguments: ( entity, propertyName, value, arrayElement )" )
DEFINE_SCRIPTFUNC( SetPropEntityArray, "Arguments: ( entity, propertyName, value, arrayElement )" )
DEFINE_SCRIPTFUNC( SetPropStringArray, "Arguments: ( entity, propertyName, value, arrayElement )" )
DEFINE_SCRIPTFUNC( GetPropArraySize, "Arguments: ( entity, propertyName )" )
DEFINE_SCRIPTFUNC( HasProp, "Arguments: ( entity, propertyName )" )
DEFINE_SCRIPTFUNC( GetPropType, "Arguments: ( entity, propertyName ) - return the prop type as a string" )
DEFINE_SCRIPTFUNC( GetPropBool, "Arguments: ( entity, propertyName )" )
DEFINE_SCRIPTFUNC( GetPropBoolArray, "Arguments: ( entity, propertyName, arrayElement )" )
DEFINE_SCRIPTFUNC( SetPropBool, "Arguments: ( entity, propertyName, value )" )
DEFINE_SCRIPTFUNC( SetPropBoolArray, "Arguments: ( entity, propertyName, value, arrayElement )" )
DEFINE_SCRIPTFUNC( GetPropInfo, "Arguments: ( entity, propertyName, arrayElement, table ) - Fills in a passed table with property info for the provided entity" )
DEFINE_SCRIPTFUNC( GetTable, "Arguments: ( entity, iPropType, table ) - Fills in a passed table with all props of a specified type for the provided entity (set iPropType to 0 for SendTable or 1 for DataMap)" )
END_SCRIPTDESC()
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
#if 0 // From Desolation.
class CScriptPanorama
{
public:
void DispatchEvent( const char *pszEventName, const char *pszMessage )
{
CBroadcastRecipientFilter filter;
filter.MakeReliable();
CCSUsrMsg_PanoramaDispatchEvent msg;
msg.set_event( pszEventName );
msg.set_message( pszMessage );
SendUserMessage( filter, CS_UM_PanoramaDispatchEvent, msg );
}
private:
} g_ScriptPanorama;
BEGIN_SCRIPTDESC_ROOT_NAMED( CScriptPanorama, "CPanorama", SCRIPT_SINGLETON "Panorama VScript Interface" )
DEFINE_SCRIPTFUNC( DispatchEvent, "Trigger a panorama event to the vscript event handler" )
END_SCRIPTDESC();
#endif
// ----------------------------------------------------------------------------
// KeyValues access - CBaseEntity::ScriptGetKeyFromModel returns root KeyValues
// ----------------------------------------------------------------------------
BEGIN_SCRIPTDESC_ROOT( CScriptKeyValues, "Wrapper class over KeyValues instance" )
DEFINE_SCRIPT_CONSTRUCTOR()
DEFINE_SCRIPTFUNC_NAMED( ScriptFindKey, "FindKey", "Given a KeyValues object and a key name, find a KeyValues object associated with the key name" );
DEFINE_SCRIPTFUNC_NAMED( ScriptGetFirstSubKey, "GetFirstSubKey", "Given a KeyValues object, return the first sub key object" );
DEFINE_SCRIPTFUNC_NAMED( ScriptGetNextKey, "GetNextKey", "Given a KeyValues object, return the next key object in a sub key group" );
DEFINE_SCRIPTFUNC_NAMED( ScriptGetKeyValueInt, "GetKeyInt", "Given a KeyValues object and a key name, return associated integer value" );
DEFINE_SCRIPTFUNC_NAMED( ScriptGetKeyValueFloat, "GetKeyFloat", "Given a KeyValues object and a key name, return associated float value" );
DEFINE_SCRIPTFUNC_NAMED( ScriptGetKeyValueBool, "GetKeyBool", "Given a KeyValues object and a key name, return associated bool value" );
DEFINE_SCRIPTFUNC_NAMED( ScriptGetKeyValueString, "GetKeyString", "Given a KeyValues object and a key name, return associated string value" );
DEFINE_SCRIPTFUNC_NAMED( ScriptIsKeyValueEmpty, "IsKeyEmpty", "Given a KeyValues object and a key name, return true if key name has no value" );
DEFINE_SCRIPTFUNC_NAMED( ScriptReleaseKeyValues, "ReleaseKeyValues", "Given a root KeyValues object, release its contents" );
END_SCRIPTDESC();
HSCRIPT CScriptKeyValues::ScriptFindKey( const char *pszName )
{
KeyValues *pKeyValues = m_pKeyValues->FindKey(pszName);
if ( pKeyValues == NULL )
return NULL;
CScriptKeyValues *pScriptKey = new CScriptKeyValues( pKeyValues );
// UNDONE: who calls ReleaseInstance on this??
HSCRIPT hScriptInstance = g_pScriptVM->RegisterInstance( pScriptKey );
return hScriptInstance;
}
HSCRIPT CScriptKeyValues::ScriptGetFirstSubKey( void )
{
KeyValues *pKeyValues = m_pKeyValues->GetFirstSubKey();
if ( pKeyValues == NULL )
return NULL;
CScriptKeyValues *pScriptKey = new CScriptKeyValues( pKeyValues );
// UNDONE: who calls ReleaseInstance on this??
HSCRIPT hScriptInstance = g_pScriptVM->RegisterInstance( pScriptKey );
return hScriptInstance;
}
HSCRIPT CScriptKeyValues::ScriptGetNextKey( void )
{
KeyValues *pKeyValues = m_pKeyValues->GetNextKey();
if ( pKeyValues == NULL )
return NULL;
CScriptKeyValues *pScriptKey = new CScriptKeyValues( pKeyValues );
// UNDONE: who calls ReleaseInstance on this??
HSCRIPT hScriptInstance = g_pScriptVM->RegisterInstance( pScriptKey );
return hScriptInstance;
}
int CScriptKeyValues::ScriptGetKeyValueInt( const char *pszName )
{
int i = m_pKeyValues->GetInt( pszName );
return i;
}
float CScriptKeyValues::ScriptGetKeyValueFloat( const char *pszName )
{
float f = m_pKeyValues->GetFloat( pszName );
return f;
}
const char *CScriptKeyValues::ScriptGetKeyValueString( const char *pszName )
{
const char *psz = m_pKeyValues->GetString( pszName );
return psz;
}
bool CScriptKeyValues::ScriptIsKeyValueEmpty( const char *pszName )
{
bool b = m_pKeyValues->IsEmpty( pszName );
return b;
}
bool CScriptKeyValues::ScriptGetKeyValueBool( const char *pszName )
{
bool b = m_pKeyValues->GetBool( pszName );
return b;
}
void CScriptKeyValues::ScriptReleaseKeyValues( )
{
m_pKeyValues->deleteThis();
m_pKeyValues = NULL;
}
// constructors
CScriptKeyValues::CScriptKeyValues( KeyValues *pKeyValues )
{
m_pKeyValues = pKeyValues;
}
// destructor
CScriptKeyValues::~CScriptKeyValues( )
{
if (m_pKeyValues)
{
m_pKeyValues->deleteThis();
}
m_pKeyValues = NULL;
}