‪Black Ops 3 Source Code Explorer  0.1
‪An script explorer for Black Ops 3 by ZeRoY
ai_interface.gsc
Go to the documentation of this file.
1 #define _ATTRIBUTE_CALLBACK "callback"
2 #define _ATTRIBUTE_DEFAULT_VALUE "default_value"
3 #define _ATTRIBUTE_MAX_VALUE "max_value"
4 #define _ATTRIBUTE_MIN_VALUE "min_value"
5 #define _ATTRIBUTE_TYPE "type"
6 #define _ATTRIBUTE_VALUES "values"
7 #define _INTERFACE_MATCH_TYPE "_interface_match"
8 #define _INTERFACE_NUMERIC_TYPE "_interface_numeric"
9 #define _INTERFACE_VECTOR_TYPE "_interface_vector"
10 
11 function autoexec ‪main()
12 {
13  /#
14  level.__ai_debugInterface = GetDvarInt( "ai_debugInterface" );
15  #/
16 }
17 
18 function private ‪_CheckValue( archetype, attributeName, value )
19 {
20  /#
21  attribute = level.__ai_interface[ archetype ][ attributeName ];
22 
23  switch ( attribute[ ‪_ATTRIBUTE_TYPE ] )
24  {
26  possibleValues = attribute[ ‪_ATTRIBUTE_VALUES ];
27 
28  assert( !IsArray( possibleValues ) || IsInArray( possibleValues, value ),
29  "AI: \"" + value + "\" is not one of the allowed values for attribute \"" +
30  attributeName + "\"." );
31  break;
32 
34  maxValue = attribute[ ‪_ATTRIBUTE_MAX_VALUE ];
35  minValue = attribute[ ‪_ATTRIBUTE_MIN_VALUE ];
36 
37  assert( IsInt( value ) || IsFloat( value ), "AI: Attribute \"" + attributeName +
38  "\" requires a numeric value and \"" + value + "\" does not qualify." );
39  assert( ( !IsDefined( maxValue ) && !IsDefined( minValue ) ) ||
40  ( value <= maxValue && value >= minValue ),
41  "AI: \"" + value + "\" is outside the allowed range of (" + minValue + "," +
42  maxValue + ")." );
43  break;
44 
46  if ( IsDefined( value ) )
47  assert( IsVec( value ), "AI: Attribute \"" + attributeName +
48  "\" requires a vector value and \"" + value + "\" does not qualify." );
49  break;
50 
51  default:
52  assert( "AI: Unknown attribute type of \"" + attribute[ ‪_ATTRIBUTE_TYPE ] +
53  "\" for attribute \"" + attributeName + "\"." );
54  break;
55  }
56  #/
57 }
58 
59 function private ‪_CheckPrerequisites( entity, attribute )
60 {
61  /#
62  assert( IsEntity( entity ), "AI: Must pass an entity to access an attribute." );
63  assert( IsActor( entity ) || IsVehicle( entity ), "AI: Must pass an actor or vehicle to access an attribute." );
64  assert( IsString( attribute ), "AI: Must pass in a valid attribute name." );
65 
66  // In depth debugging, only necessary to debug if an AI or the AI Interface system has been setup properly.
67  if ( IsDefined( level.__ai_debugInterface ) && level.__ai_debugInterface > 0 )
68  {
69  assert( IsArray( entity.__interface ),
70  "AI: Entity(" + entity.archetype + ") must create an interface before accessing an " +
71  "attribute, see \"ai::CreateInterfaceForEntity\"." );
72  assert( IsArray( level.__ai_interface ),
73  "AI: No attributes have been registered with the AI interface system yet." );
74  assert( IsArray( level.__ai_interface[ entity.archetype ] ),
75  "AI: No attributes for archetype \"" + entity.archetype + "\" have been registered." );
76  assert( IsArray( level.__ai_interface[ entity.archetype ][ attribute ] ),
77  "AI: Attribute \"" + attribute + "\" has not been registered for archetype \"" +
78  entity.archetype + "\" yet." );
79  assert( IsString( level.__ai_interface[ entity.archetype ][ attribute ][ ‪_ATTRIBUTE_TYPE ] ),
80  "AI: Attribute type is undefined for \"" + attribute + "\"." );
81  }
82  #/
83 }
84 
85 function private ‪_CheckRegistrationPrerequisites( archetype, attribute, callbackFunction )
86 {
87  /#
88  assert( IsString( archetype ), "AI: \"archetype\" value is mandatory for registration." );
89  assert( IsString( attribute ), "AI: \"attribute\" value is mandatory for registration." );
90  assert( !IsDefined( callbackFunction ) || IsFunctionPtr( callbackFunction ),
91  "AI: \"callbackFunction\" is optional but must be a function pointer if specified." );
92  #/
93 }
94 
95 function private ‪_InitializeLevelInterface( archetype )
96 {
97  if ( !IsDefined( level.__ai_interface ) )
98  {
99  level.__ai_interface = [];
100  }
101 
102  if ( !IsDefined( level.__ai_interface[ archetype ] ) )
103  {
104  level.__ai_interface[ archetype ] = [];
105  }
106 }
107 
108 #namespace ai;
109 
110 function ‪CreateInterfaceForEntity( entity )
111 {
112  if ( !IsDefined( entity.__interface ) )
113  {
114  entity.__interface = [];
115  }
116 }
117 
118 function ‪GetAiAttribute( entity, attribute )
119 {
120  /#
121  ‪ai_interface::_CheckPrerequisites( entity, attribute );
122  #/
123 
124  if ( !IsDefined( entity.__interface[ attribute ] ) )
125  {
126  return level.__ai_interface[ entity.archetype ][ attribute ][ ‪_ATTRIBUTE_DEFAULT_VALUE ];
127  }
128 
129  return entity.__interface[ attribute ];
130 }
131 
132 function ‪HasAiAttribute( entity, attribute )
133 {
134  return
135  IsDefined( entity ) &&
136  IsDefined( attribute ) &&
137  IsDefined( entity.archetype ) &&
138  IsDefined( level.__ai_interface ) &&
139  IsDefined( level.__ai_interface[ entity.archetype ] ) &&
140  IsDefined( level.__ai_interface[ entity.archetype ][ attribute ] );
141 }
142 
143 function ‪RegisterMatchedInterface( archetype, attribute, defaultValue, possibleValues, callbackFunction )
144 {
145  /#
146  ‪ai_interface::_CheckRegistrationPrerequisites( archetype, attribute, callbackFunction );
147  assert( !IsDefined( possibleValues ) || IsArray( possibleValues ),
148  "AI: \"possibleValues\" is optional but must be an array if specified." );
149  #/
150 
152 
153  /#
154  assert( !IsDefined( level.__ai_interface[ archetype ][ attribute ] ),
155  "AI: \"" + attribute + "\" is already registered for archetype \"" + archetype + "\"" );
156  #/
157 
158  level.__ai_interface[ archetype ][ attribute ] = [];
159  level.__ai_interface[ archetype ][ attribute ][ ‪_ATTRIBUTE_CALLBACK ] = callbackFunction;
160  level.__ai_interface[ archetype ][ attribute ][ ‪_ATTRIBUTE_DEFAULT_VALUE ] = defaultValue;
161  level.__ai_interface[ archetype ][ attribute ][ ‪_ATTRIBUTE_TYPE ] = ‪_INTERFACE_MATCH_TYPE;
162  level.__ai_interface[ archetype ][ attribute ][ ‪_ATTRIBUTE_VALUES ] = possibleValues;
163 
164  /#
165  ‪ai_interface::_CheckValue( archetype, attribute, defaultValue );
166  #/
167 }
168 
169 function ‪RegisterNumericInterface( archetype, attribute, defaultValue, minimum, maximum, callbackFunction )
170 {
171  /#
172  ‪ai_interface::_CheckRegistrationPrerequisites( archetype, attribute, callbackFunction );
173  assert( !IsDefined( minimum ) || IsInt( minimum ) || IsFloat( minimum ),
174  "AI: \"minimum\" is optional but must be a numeric if specified." );
175  assert( !IsDefined( maximum ) || IsInt( maximum ) || IsFloat( maximum ),
176  "AI: \"maximum\" is optional but must be a numeric if specified." );
177  assert( ( !IsDefined( minimum ) && !IsDefined( maximum ) ) ||
178  ( IsDefined( minimum ) && IsDefined( maximum ) ),
179  "AI: Either both a minimum and maximum must be defined or both undefined, not mixed.");
180  assert( ( !IsDefined( minimum ) && !IsDefined( maximum ) ) ||
181  ( minimum <= maximum ),
182  "AI: Attribute \"" + attribute + "\" cannot specify a minimum greater than " +
183  "the attribute's maximum");
184  #/
185 
187 
188  /#
189  assert( !IsDefined( level.__ai_interface[ archetype ][ attribute ] ),
190  "AI: \"" + attribute + "\" is already registered for archetype \"" + archetype + "\"" );
191  #/
192 
193  level.__ai_interface[ archetype ][ attribute ] = [];
194  level.__ai_interface[ archetype ][ attribute ][ ‪_ATTRIBUTE_CALLBACK ] = callbackFunction;
195  level.__ai_interface[ archetype ][ attribute ][ ‪_ATTRIBUTE_DEFAULT_VALUE ] = defaultValue;
196  level.__ai_interface[ archetype ][ attribute ][ ‪_ATTRIBUTE_MAX_VALUE ] = maximum;
197  level.__ai_interface[ archetype ][ attribute ][ ‪_ATTRIBUTE_MIN_VALUE ] = minimum;
198  level.__ai_interface[ archetype ][ attribute ][ ‪_ATTRIBUTE_TYPE ] = ‪_INTERFACE_NUMERIC_TYPE;
199 
200  /#
201  ‪ai_interface::_CheckValue( archetype, attribute, defaultValue );
202  #/
203 }
204 
205 function ‪RegisterVectorInterface( archetype, attribute, defaultValue, callbackFunction )
206 {
207  /#
208  ‪ai_interface::_CheckRegistrationPrerequisites( archetype, attribute, callbackFunction );
209  #/
210 
212 
213  /#
214  assert( !IsDefined( level.__ai_interface[ archetype ][ attribute ] ),
215  "AI: \"" + attribute + "\" is already registered for archetype \"" + archetype + "\"" );
216  #/
217 
218  level.__ai_interface[ archetype ][ attribute ] = [];
219  level.__ai_interface[ archetype ][ attribute ][ ‪_ATTRIBUTE_CALLBACK ] = callbackFunction;
220  level.__ai_interface[ archetype ][ attribute ][ ‪_ATTRIBUTE_DEFAULT_VALUE ] = defaultValue;
221  level.__ai_interface[ archetype ][ attribute ][ ‪_ATTRIBUTE_TYPE ] = ‪_INTERFACE_VECTOR_TYPE;
222 
223  /#
224  ‪ai_interface::_CheckValue( archetype, attribute, defaultValue );
225  #/
226 }
227 
228 function ‪SetAiAttribute( entity, attribute, value )
229 {
230  /#
231  ‪ai_interface::_CheckPrerequisites( entity, attribute );
232  ‪ai_interface::_CheckValue( entity.archetype, attribute, value );
233  #/
234 
235  oldValue = entity.__interface[ attribute ];
236 
237  if ( !IsDefined( oldValue ) )
238  {
239  oldValue = level.__ai_interface[ entity.archetype ][ attribute ][ ‪_ATTRIBUTE_DEFAULT_VALUE ];
240  }
241 
242  entity.__interface[ attribute ] = value;
243 
244  ‪callback = level.__ai_interface[ entity.archetype ][ attribute ][ ‪_ATTRIBUTE_CALLBACK ];
245 
246  if ( IsFunctionPtr( ‪callback ) )
247  {
248  [[‪callback]]( entity, attribute, oldValue, value );
249  }
250 }
‪callback
‪function callback(event, localclientnum, params)
Definition: callbacks_shared.csc:13
‪_INTERFACE_VECTOR_TYPE
‪#define _INTERFACE_VECTOR_TYPE
Definition: ai_interface.gsc:9
‪_ATTRIBUTE_DEFAULT_VALUE
‪#define _ATTRIBUTE_DEFAULT_VALUE
Definition: ai_interface.gsc:2
‪RegisterMatchedInterface
‪function RegisterMatchedInterface(archetype, attribute, defaultValue, possibleValues, callbackFunction)
Definition: ai_interface.gsc:143
‪SetAiAttribute
‪function SetAiAttribute(entity, attribute, value)
Definition: ai_interface.gsc:228
‪HasAiAttribute
‪function HasAiAttribute(entity, attribute)
Definition: ai_interface.gsc:132
‪_InitializeLevelInterface
‪function private _InitializeLevelInterface(archetype)
Definition: ai_interface.gsc:95
‪_ATTRIBUTE_MIN_VALUE
‪#define _ATTRIBUTE_MIN_VALUE
Definition: ai_interface.gsc:4
‪_INTERFACE_NUMERIC_TYPE
‪#define _INTERFACE_NUMERIC_TYPE
Definition: ai_interface.gsc:8
‪_CheckPrerequisites
‪function private _CheckPrerequisites(entity, attribute)
Definition: ai_interface.gsc:59
‪_ATTRIBUTE_TYPE
‪#define _ATTRIBUTE_TYPE
Definition: ai_interface.gsc:5
‪_CheckRegistrationPrerequisites
‪function private _CheckRegistrationPrerequisites(archetype, attribute, callbackFunction)
Definition: ai_interface.gsc:85
‪_ATTRIBUTE_VALUES
‪#define _ATTRIBUTE_VALUES
Definition: ai_interface.gsc:6
‪_ATTRIBUTE_CALLBACK
‪#define _ATTRIBUTE_CALLBACK
Definition: ai_interface.gsc:1
‪_ATTRIBUTE_MAX_VALUE
‪#define _ATTRIBUTE_MAX_VALUE
Definition: ai_interface.gsc:3
‪RegisterVectorInterface
‪function RegisterVectorInterface(archetype, attribute, defaultValue, callbackFunction)
Definition: ai_interface.gsc:205
‪GetAiAttribute
‪function GetAiAttribute(entity, attribute)
Definition: ai_interface.gsc:118
‪RegisterNumericInterface
‪function RegisterNumericInterface(archetype, attribute, defaultValue, minimum, maximum, callbackFunction)
Definition: ai_interface.gsc:169
‪main
‪function autoexec main()
Definition: ai_interface.gsc:11
‪_INTERFACE_MATCH_TYPE
‪#define _INTERFACE_MATCH_TYPE
Definition: ai_interface.gsc:7
‪CreateInterfaceForEntity
‪function CreateInterfaceForEntity(entity)
Definition: ai_interface.gsc:110
‪_CheckValue
‪function private _CheckValue(archetype, attributeName, value)
Definition: ai_interface.gsc:18