-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathvscript_server.cpp
More file actions
2353 lines (1993 loc) · 82.5 KB
/
Copy pathvscript_server.cpp
File metadata and controls
2353 lines (1993 loc) · 82.5 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 "characterset.h"
#include "sceneentity.h" // for exposing scene precache function
#include "isaverestore.h"
#include "gamerules.h"
#include "netpropmanager.h"
#include "ai_speech.h"
#include "ai_network.h"
#include "ai_node.h"
#include "ai_link.h"
#include "ai_basenpc.h"
#include "inetchannelinfo.h"
#include "decals.h"
#include "player_voice_listener.h"
#include "ColorText_Shared.h"
#ifdef _WIN32
#include "vscript_server_nut.h"
#endif
#include "asw_gamerules.h"
extern ScriptClassDesc_t * GetScriptDesc( CBaseEntity * );
// #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( funcname, funcdesc )
#endif // VMPROFILE
static ConVar sv_mapspawn_nut_exec( "sv_mapspawn_nut_exec", "0", FCVAR_NONE, "If set to 1, server will execute scripts/vscripts/mapspawn.nut file" );
extern char *s_ElementNames[MAX_ARRAY_ELEMENTS];
constexpr int CLAMP_COLOR(int value)
{
return value < 0 ? 0 : value > 255 ? 255 : value;
}
//-----------------------------------------------------------------------------
// Iterate through keys in a table and assign KeyValues on entity for spawn
//-----------------------------------------------------------------------------
void ParseTable( CBaseEntity *pEntity, HSCRIPT hTable, const char *pszKey = "" )
{
if ( !pEntity || !hTable )
return;
ScriptVariant_t key, value;
int nIterator = g_pScriptVM->GetKeyValue( hTable, 0, &key, &value );
while ( nIterator != -1 )
{
const char *szKeyName = key;
if ( V_strcmp( pszKey, "" ) != 0 )
szKeyName = pszKey;
switch ( value.m_type )
{
case FIELD_FLOAT:
{
pEntity->KeyValue( szKeyName, value.m_float );
break;
}
case FIELD_VECTOR:
{
pEntity->KeyValue( szKeyName, *value.m_pVector );
break;
}
case FIELD_INTEGER:
case FIELD_BOOLEAN:
{
pEntity->KeyValue( szKeyName, value.m_int );
break;
}
case FIELD_CSTRING:
{
pEntity->KeyValue( szKeyName, value.m_pszString );
break;
}
case FIELD_HSCRIPT:
{
ParseTable( pEntity, value.m_hScript, key );
break;
}
default:
{
Warning( "Unsupported KeyValue type for key %s (type %s)\n", key, ScriptFieldTypeName( value.m_type ) );
}
}
nIterator = g_pScriptVM->GetKeyValue( hTable, nIterator, &key, &value );
}
}
//-----------------------------------------------------------------------------
// Sets value of a SendProp for Temp Entity from a table
//-----------------------------------------------------------------------------
void TE_SetSendProp( SendProp *pSendProp, CBaseTempEntity *pTempEntity, int iOffset, int iElement, HSCRIPT hTable )
{
const char *pszPropName = pSendProp->GetName();
if ( iElement > -1 )
pszPropName = s_ElementNames[iElement];
ScriptVariant_t value;
bool bKeyExists = g_pScriptVM->GetValue( hTable, pszPropName, &value );
uint8 *pEntityPropData = (uint8 *)pTempEntity + iOffset + pSendProp->GetOffset();
switch ( pSendProp->GetType() )
{
case DPT_Int:
{
int nBits = pSendProp->m_nBits;
if ( nBits == 21 )
{
CBaseEntity *pOtherEntity = ToEnt( value.m_hScript );
CBaseHandle &baseHandle = *(CBaseHandle *)pEntityPropData;
if ( !pOtherEntity )
baseHandle.Set( NULL );
else
baseHandle.Set( (IHandleEntity *)pOtherEntity );
}
else if ( nBits >= 17 )
{
*(int32 *)pEntityPropData = (int32)value.m_int;
}
else if ( nBits >= 9 )
{
if (!pSendProp->IsSigned())
*(uint16 *)pEntityPropData = (uint16)value.m_int;
else
*(int16 *)pEntityPropData = (int16)value.m_int;
}
else if ( nBits >= 2 )
{
if (!pSendProp->IsSigned())
*(uint8 *)pEntityPropData = (uint8)value.m_int;
else
*(int8 *)pEntityPropData = (int8)value.m_int;
}
else
{
*(bool *)pEntityPropData = value.m_bool ? true : false;
}
break;
}
case DPT_Float:
{
*(float *)(uint8 *)pEntityPropData = value.m_float;
break;
}
case DPT_Vector:
{
if ( !bKeyExists )
value = Vector( 0, 0, 0 );
Vector *pVec = (Vector *)(uint8 *)pEntityPropData;
pVec->x = value.m_pVector->x;
pVec->y = value.m_pVector->y;
pVec->z = value.m_pVector->z;
break;
}
}
}
//-----------------------------------------------------------------------------
// Iterate through SendTable setting SendProp data for Temp Entity from a table
//-----------------------------------------------------------------------------
void TE_ParseSendPropTable( SendTable *pSendTable, CBaseTempEntity *pTempEntity, int iOffset, HSCRIPT hTable )
{
for ( int nPropIdx = 0; nPropIdx < pSendTable->GetNumProps(); nPropIdx++ )
{
SendProp *pSendProp = pSendTable->GetProp( nPropIdx );
if ( pSendProp->IsExcludeProp() )
continue;
SendTable *pInternalSendTable = pSendProp->GetDataTable();
if ( pInternalSendTable )
TE_ParseSendPropTable( pInternalSendTable, pTempEntity, (iOffset + pSendProp->GetOffset()), hTable );
else
{
if ( pSendProp->GetType() == DPT_Array )
{
SendProp *pArrayProp = pSendProp->GetArrayProp();
ScriptVariant_t value;
bool bSuccess = g_pScriptVM->GetValue( hTable, pArrayProp->GetName(), &value );
if ( bSuccess && value.m_type == FIELD_HSCRIPT )
{
for ( int element = 0; element < pSendProp->GetNumElements(); element++ )
{
TE_SetSendProp( pArrayProp, pTempEntity, iOffset + ( element * pSendProp->GetElementStride() ), element, value.m_hScript );
}
}
}
else
TE_SetSendProp( pSendProp, pTempEntity, iOffset, -1, hTable );
}
}
}
//-----------------------------------------------------------------------------
// Store SendProp type in a table for Temp Entity
//-----------------------------------------------------------------------------
void TE_StoreSendPropValue( SendProp *pSendProp, int iElement, HSCRIPT hTable )
{
if ( !hTable )
return;
const char *pszPropType = "unknown";
const char *pszPropName = pSendProp->GetName();
if ( iElement > -1 )
pszPropName = s_ElementNames[iElement];
switch ( pSendProp->GetType() )
{
case DPT_Int:
{
int nBits = pSendProp->m_nBits;
if ( nBits == 21 )
pszPropType = "instance";
else
pszPropType = "integer";
break;
}
case DPT_Float:
{
pszPropType = "float";
break;
}
case DPT_Vector:
{
pszPropType = "Vector";
break;
}
}
g_pScriptVM->SetValue( hTable, pszPropName, pszPropType );
}
//-----------------------------------------------------------------------------
// Iterate through SendTable storing SendProp types in a table for Temp Entity
//-----------------------------------------------------------------------------
void TE_CollectNestedSendProps( SendTable *pSendTable, HSCRIPT hTable )
{
if ( !hTable )
return;
for ( int nPropIdx = 0; nPropIdx < pSendTable->GetNumProps(); nPropIdx++ )
{
SendProp *pSendProp = pSendTable->GetProp( nPropIdx );
if ( pSendProp->IsExcludeProp() )
continue;
const char *pszPropName = pSendProp->GetName();
SendTable *pInternalSendTable = pSendProp->GetDataTable();
if ( pInternalSendTable )
{
if ( V_strcmp( pSendProp->GetName(), "baseclass" ) == 0 )
pszPropName = pInternalSendTable->m_pNetTableName;
ScriptVariant_t hPropTable;
g_pScriptVM->CreateTable( hPropTable );
TE_CollectNestedSendProps( pInternalSendTable, hPropTable );
g_pScriptVM->SetValue( hTable, pszPropName, hPropTable );
g_pScriptVM->ReleaseValue( hPropTable );
}
else
{
if ( pSendProp->GetType() == DPT_Array )
{
SendProp *pArrayProp = pSendProp->GetArrayProp();
ScriptVariant_t hPropTable;
g_pScriptVM->CreateTable( hPropTable );
for ( int element = 0; element < pSendProp->GetNumElements(); element++ )
{
TE_StoreSendPropValue( pArrayProp, element, hPropTable );
}
g_pScriptVM->SetValue( hTable, pszPropName, hPropTable );
g_pScriptVM->ReleaseValue( hPropTable );
}
else
TE_StoreSendPropValue( pSendProp, -1, hTable );
}
}
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
class CScriptEntityIterator
{
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 ) );
}
HSCRIPT FindByName( HSCRIPT hStartEntity, const char *szName )
{
return ToHScript( gEntList.FindEntityByName( ToEnt( hStartEntity ), szName ) );
}
HSCRIPT FindInSphere( HSCRIPT hStartEntity, const Vector &vecCenter, float flRadius )
{
return ToHScript( gEntList.FindEntityInSphere( ToEnt( hStartEntity ), vecCenter, flRadius ) );
}
HSCRIPT FindByTarget( HSCRIPT hStartEntity, const char *szName )
{
return ToHScript( gEntList.FindEntityByTarget( ToEnt( hStartEntity ), szName ) );
}
HSCRIPT FindByModel( HSCRIPT hStartEntity, const char *szModelName )
{
return ToHScript( gEntList.FindEntityByModel( ToEnt( hStartEntity ), szModelName ) );
}
HSCRIPT FindByNameNearest( const char *szName, const Vector &vecSrc, float flRadius )
{
return ToHScript( gEntList.FindEntityByNameNearest( szName, vecSrc, flRadius ) );
}
HSCRIPT FindByNameWithin( HSCRIPT hStartEntity, const char *szName, const Vector &vecSrc, float flRadius )
{
return ToHScript( gEntList.FindEntityByNameWithin( ToEnt( hStartEntity ), szName, vecSrc, flRadius ) );
}
HSCRIPT FindByClassnameNearest( const char *szName, const Vector &vecSrc, float flRadius )
{
return ToHScript( gEntList.FindEntityByClassnameNearest( szName, vecSrc, flRadius ) );
}
HSCRIPT FindByClassnameWithin( HSCRIPT hStartEntity , const char *szName, const Vector &vecSrc, float flRadius )
{
return ToHScript( gEntList.FindEntityByClassnameWithin( ToEnt( hStartEntity ), szName, vecSrc, flRadius ) );
}
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" )
END_SCRIPTDESC();
// ----------------------------------------------------------------------------
// 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 )
{
if ( !g_pScriptVM ) return NULL;
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 )
{
if ( !g_pScriptVM ) return NULL;
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 )
{
if ( !g_pScriptVM ) return NULL;
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 = NULL )
{
m_pKeyValues = pKeyValues;
}
// destructor
CScriptKeyValues::~CScriptKeyValues( )
{
if (m_pKeyValues)
{
m_pKeyValues->deleteThis();
}
m_pKeyValues = NULL;
}
class CScriptResponseCriteria
{
public:
const char *GetValue( HSCRIPT hEntity, const char *pCriteria )
{
CBaseEntity *pBaseEntity = ToEnt(hEntity);
if ( !pBaseEntity )
return "";
AI_CriteriaSet criteria;
pBaseEntity->ModifyOrAppendCriteria( criteria );
int index = criteria.FindCriterionIndex( pCriteria );
if ( !criteria.IsValidIndex( index ) )
return "";
return criteria.GetValue( index );
}
void GetTable( HSCRIPT hEntity, HSCRIPT hTable )
{
CBaseEntity *pBaseEntity = ToEnt(hEntity);
if ( !pBaseEntity || !hTable )
return;
if ( !g_pScriptVM ) return;
AI_CriteriaSet criteria;
pBaseEntity->ModifyOrAppendCriteria( criteria );
for ( int nCriteriaIdx = 0; nCriteriaIdx < criteria.GetCount(); nCriteriaIdx++ )
{
if ( !criteria.IsValidIndex( nCriteriaIdx ) )
continue;
g_pScriptVM->SetValue( hTable, criteria.GetName( nCriteriaIdx ), criteria.GetValue( nCriteriaIdx ) );
}
}
bool HasCriterion( HSCRIPT hEntity, const char *pCriteria )
{
CBaseEntity *pBaseEntity = ToEnt(hEntity);
if ( !pBaseEntity )
return false;
AI_CriteriaSet criteria;
pBaseEntity->ModifyOrAppendCriteria( criteria );
int index = criteria.FindCriterionIndex( pCriteria );
return criteria.IsValidIndex( index );
}
} g_ScriptResponseCriteria;
BEGIN_SCRIPTDESC_ROOT_NAMED( CScriptResponseCriteria, "CScriptResponseCriteria", SCRIPT_SINGLETON "Used to get response criteria" )
DEFINE_SCRIPTFUNC( GetValue, "Arguments: ( entity, criteriaName ) - returns a string" )
DEFINE_SCRIPTFUNC( GetTable, "Arguments: ( entity ) - returns a table of all criteria" )
DEFINE_SCRIPTFUNC( HasCriterion, "Arguments: ( entity, criteriaName ) - returns true if the criterion exists" )
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;
if ( !g_pScriptVM ) 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 )
{
DevMsg ("Error: Entity is NULL in EntityOutputs.AddOutput\n" );
return;
}
CBaseEntityOutput *pOutput = pBaseEntity->FindNamedOutput( szOutputName );
if ( !pOutput )
{
DevMsg ("Error: Cannot find named output \"%s\" in EntityOutputs.AddOutput\n", szOutputName );
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 )
{
DevMsg ("Error: Entity is NULL in EntityOutputs.RemoveOutput\n" );
return;
}
CBaseEntityOutput *pOutput = pBaseEntity->FindNamedOutput( szOutputName );
if ( !pOutput )
{
DevMsg ("Error: Cannot find named output \"%s\" in EntityOutputs.RemoveOutput\n", szOutputName );
return;
}
if ( V_strcmp( szTarget, "" ) == 0 )
pOutput->DeleteAllElements();
else
{
CEventAction *pAction = pOutput->GetFirstAction();
pOutput->ScriptRemoveEventAction( pAction, szTarget, szTargetInput, szParameter );
}
}
ScriptVariant_t GetValue( HSCRIPT hEntity, const char *szOutputName )
{
CBaseEntity *pBaseEntity = ToEnt( hEntity );
if ( !pBaseEntity )
{
DevMsg( "Error: Entity is NULL in EntityOutputs.GetValue\n" );
return SCRIPT_VARIANT_NULL;
}
CBaseEntityOutput *pOutput = pBaseEntity->FindNamedOutput( szOutputName );
if ( !pOutput )
{
DevMsg( "Error: Cannot find named output \"%s\" in EntityOutputs.GetValue\n", szOutputName );
return SCRIPT_VARIANT_NULL;
}
switch ( pOutput->ValueFieldType() )
{
default:
// COutputVariant is only used by one output (logic_case's OnDefault) so to avoid complicating this function, only the specific types supported by other typedefs are handled.
return SCRIPT_VARIANT_NULL;
case FIELD_INTEGER:
return static_cast<COutputInt *>( pOutput )->Get();
case FIELD_FLOAT:
return static_cast<COutputFloat *>( pOutput )->Get();
case FIELD_STRING:
return ScriptVariant_t( STRING( static_cast<COutputString *>( pOutput )->Get() ), true );
case FIELD_EHANDLE:
return ToHScript( static_cast<COutputEHANDLE *>( pOutput )->Get() );
case FIELD_VECTOR:
case FIELD_POSITION_VECTOR:
{
Vector vec;
static_cast<COutputVector *>( pOutput )->Get( vec );
return ScriptVariant_t( vec, true );
}
case FIELD_COLOR32:
return int( *static_cast<COutputColor32 *>( pOutput )->Get().asInt() );
}
}
} g_ScriptEntityOutputs;
BEGIN_SCRIPTDESC_ROOT_NAMED( CScriptEntityOutputs, "CScriptEntityOutputs", SCRIPT_SINGLETON "Used to get 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" )
DEFINE_SCRIPTFUNC( GetValue, "Arguments: ( entity, outputName ) - returns the value of the output if it has one" )
END_SCRIPTDESC();
class CScriptInfoNodes
{
public:
int GetNumNodes()
{
return g_pBigAINet->NumNodes();
}
Vector GetNodeOrigin( int node_id )
{
CAI_Node *pNode = g_pBigAINet->GetNode( node_id );
if ( !pNode )
return Vector( 0, 0, 0 );
return pNode->GetOrigin();
}
Vector GetNodePosition( int node_id, int hull )
{
CAI_Node *pNode = g_pBigAINet->GetNode( node_id );
if ( !pNode || ( hull < HULL_HUMAN || hull >= NUM_HULLS ) )
return Vector( 0, 0, 0 );
return pNode->GetPosition( hull );
}
int GetNodeType( int node_id )
{
CAI_Node *pNode = g_pBigAINet->GetNode( node_id );
if ( !pNode )
return -1;
return pNode->GetType();
}
HSCRIPT GetNodeByID( int node_id )
{
CAI_Node *pNode = g_pBigAINet->GetNode( node_id );
if ( !pNode )
return NULL;
return ToHScript( pNode );
}
HSCRIPT GetNearestNodeToPoint( HSCRIPT hNPC, const Vector &vPosition )
{
CBaseEntity *pBaseEntity = ToEnt(hNPC);
CAI_BaseNPC *pNPC = NULL;
if ( pBaseEntity )
pNPC = dynamic_cast<CAI_BaseNPC*>(pBaseEntity);
int node = g_pBigAINet->NearestNodeToPoint( pNPC, vPosition, false );
if ( node == NO_NODE )
return NULL;
return ToHScript( g_pBigAINet->GetNode( node ) );
}
int GetAllNearestNodes( HSCRIPT hNPC, const Vector &vPosition, int iMaxNodes, HSCRIPT hTable )
{
return g_pBigAINet->ScriptNearestNodesInBox( hNPC, vPosition, iMaxNodes, hTable );
}
void GetAllNodes( HSCRIPT hTable )
{
int nNodes = g_pBigAINet->NumNodes();
CAI_Node **ppNodes = g_pBigAINet->AccessNodes();
for ( int i = 0; i < nNodes; i++ )
{
CAI_Node *pNode = ppNodes[i];
g_pScriptVM->SetValue( hTable, CFmtStr( "node%i", i ), ToHScript( pNode ) );
}
}
HSCRIPT CreateLink( int srcID, int destID )
{
CAI_Link *pLink = g_pBigAINet->CreateLink( srcID, destID );
if ( !pLink )
return NULL;
return ToHScript( pLink );
}
} g_ScriptInfoNodes;
BEGIN_SCRIPTDESC_ROOT_NAMED( CScriptInfoNodes, "CScriptInfoNodes", SCRIPT_SINGLETON "Used to get info_node data" )
DEFINE_SCRIPTFUNC( GetNumNodes, "Returns the amount of info_nodes in the network array" )
DEFINE_SCRIPTFUNC( GetNodeOrigin, "Arguments: ( id ) - returns the origin of the node" )
DEFINE_SCRIPTFUNC( GetNodePosition, "Arguments: ( id, hull ) - returns the hull specific origin of the node" )
DEFINE_SCRIPTFUNC( GetNodeType, "Arguments: ( id ) - returns the type of node" )
DEFINE_SCRIPTFUNC( GetNodeByID, "Arguments: ( id ) - returns the node by ID" )
DEFINE_SCRIPTFUNC( GetNearestNodeToPoint, "Arguments: ( npc, origin ) - returns the node nearest to origin with optional npc parameter" )
DEFINE_SCRIPTFUNC( GetAllNearestNodes, "Arguments: ( npc, origin, maxNodes, table ) - fills a passed in table of x nearest nodes to origin with optional npc parameter" )
DEFINE_SCRIPTFUNC( GetAllNodes, "Arguments: ( table ) - fills a passed in table of all nodes" )
DEFINE_SCRIPTFUNC( CreateLink, "Arguments: ( srcID, destID ) - creates a new link from srcID to destID and returns the link" )
END_SCRIPTDESC();
// When a scripter wants to change a netprop value, they can use the
// CNetPropManager class; it checks for errors and such on its own.
CNetPropManager g_NetProps;
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 )" )
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()
class CScriptTempEnts
{
public:
void Create( HSCRIPT hPlayer, const char *pName, float flDelay, HSCRIPT hTable )
{
if ( !hTable )
return;
CBaseEntity *pBaseEntity = ToEnt(hPlayer);
CBasePlayer *pPlayer = NULL;
CRecipientFilter filter;
if ( pBaseEntity )
pPlayer = dynamic_cast<CBasePlayer*>(pBaseEntity);
if ( pPlayer )
{
CSingleUserRecipientFilter user( pPlayer );
filter = user;
}
else
{
CBroadcastRecipientFilter broadcast;
filter = broadcast;
}
CBaseTempEntity *tempEnt = CBaseTempEntity::GetList();
while ( tempEnt )
{
if ( V_strcmp( tempEnt->GetName(), pName ) == 0 )
{
ServerClass *pServerClass = tempEnt->GetServerClass();
SendTable *pSendTable = pServerClass->m_pTable;
TE_ParseSendPropTable( pSendTable, tempEnt, 0, hTable );
tempEnt->Create( filter, flDelay );
break;
}
tempEnt = tempEnt->GetNext();
}
}
void GetPropTypes( const char *pName, HSCRIPT hTable )
{
if ( !hTable )
return;
CBaseTempEntity *tempEnt = CBaseTempEntity::GetList();
while ( tempEnt )
{
if ( V_strcmp( tempEnt->GetName(), pName ) == 0 )
{
ServerClass *pServerClass = tempEnt->GetServerClass();
SendTable *pSendTable = pServerClass->m_pTable;
TE_CollectNestedSendProps( pSendTable, hTable );
break;
}
tempEnt = tempEnt->GetNext();
}
}
void GetNames( HSCRIPT hTable )
{
if ( !hTable )
return;
int i = 0;
CBaseTempEntity *tempEnt = CBaseTempEntity::GetList();
while ( tempEnt )
{
g_pScriptVM->SetValue( hTable, CFmtStr( "name%i", i++ ), tempEnt->GetName() );
tempEnt = tempEnt->GetNext();
}
}
} g_ScriptTempEnts;
BEGIN_SCRIPTDESC_ROOT_NAMED( CScriptTempEnts, "CScriptTempEnts", SCRIPT_SINGLETON "Used to create temp entities on clients" )
DEFINE_SCRIPTFUNC( Create, "Arguments: ( player, tempEntName, flDelay, table ) - Queue a temp entity for transmission from a passed table of SendProp data" )
DEFINE_SCRIPTFUNC( GetPropTypes, "Arguments: ( tempEntName, table ) - Fills in a passed table with all SendProps and their types for the temp entity" )
DEFINE_SCRIPTFUNC( GetNames, "Arguments: ( table ) - Fills in a passed table with the names of all temp entities" )
END_SCRIPTDESC();
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
static float Time()
{
return gpGlobals->curtime;
}
static float FrameTime()
{
return gpGlobals->frametime;
}
static int Script_GetFrameCount()
{
return gpGlobals->framecount;
}
static void SendToConsole( const char *pszCommand )
{
CBasePlayer *pPlayer = UTIL_GetLocalPlayerOrListenServerHost();
if ( !pPlayer )
{
DevMsg ("Cannot execute \"%s\", no player\n", pszCommand );
return;
}
engine->ClientCommand( pPlayer->edict(), pszCommand );
}
static void SendToServerConsole( const char *pszCommand )
{
char szCommand[512];
Q_snprintf( szCommand, sizeof(szCommand), "%s\n", pszCommand );
engine->ServerCommand( szCommand );
}
static const char *GetMapName()
{
return STRING( gpGlobals->mapname );
}