‪Black Ops 3 Source Code Explorer  0.1
‪An script explorer for Black Ops 3 by ZeRoY
math_shared.gsc
Go to the documentation of this file.
1 #using scripts\shared\util_shared;
2 
3 #insert scripts\shared\shared.gsh;
4 
5 #namespace math;
6 
7 function ‪cointoss()
8 {
9  return RandomInt( 100 ) >= 50;
10 }
11 
23 function ‪clamp( val, val_min, val_max )
24 {
25  ‪DEFAULT( val_max, val );
26 
27  if (val < val_min)
28  {
29  val = val_min;
30  }
31  else if (val > val_max)
32  {
33  val = val_max;
34  }
35 
36  return val;
37 }
38 
52 function ‪linear_map(num, min_a, max_a, min_b, max_b)
53 {
54  return ‪clamp(( (num - min_a) / (max_a - min_a) * (max_b - min_b) + min_b ), min_b, max_b);
55 }
56 
69 function ‪lag(desired, curr, k, dt)
70 {
71  r = 0.0;
72 
73  if (((k * dt) >= 1.0) || (k <= 0.0))
74  {
75  r = desired;
76  }
77  else
78  {
79  err = desired - curr;
80  r = curr + k * err * dt;
81  }
82 
83  return r;
84 }
85 
86 function ‪find_box_center( mins, maxs )
87 {
88  center = ( 0, 0, 0 );
89  center = maxs - mins;
90  center = ( center[0]/2, center[1]/2, center[2]/2 ) + mins;
91  return center;
92 }
93 
94 function ‪expand_mins( mins, point )
95 {
96  if ( mins[0] > point[0] )
97  {
98  mins = ( point[0], mins[1], mins[2] );
99  }
100 
101  if ( mins[1] > point[1] )
102  {
103  mins = ( mins[0], point[1], mins[2] );
104  }
105 
106  if ( mins[2] > point[2] )
107  {
108  mins = ( mins[0], mins[1], point[2] );
109  }
110 
111  return mins;
112 }
113 
114 function ‪expand_maxs( maxs, point )
115 {
116  if ( maxs[0] < point[0] )
117  {
118  maxs = ( point[0], maxs[1], maxs[2] );
119  }
120 
121  if ( maxs[1] < point[1] )
122  {
123  maxs = ( maxs[0], point[1], maxs[2] );
124  }
125 
126  if ( maxs[2] < point[2] )
127  {
128  maxs = ( maxs[0], maxs[1], point[2] );
129  }
130 
131  return maxs;
132 }
133 
134 // ----------------------------------------------------------------------------------------------------
135 // -- Vectors -----------------------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------------------------------
137 
147 function ‪vector_compare(vec1, vec2)
148 {
149  return (abs(vec1[0] - vec2[0]) < .001) && (abs(vec1[1] - vec2[1]) < .001) && (abs(vec1[2] - vec2[2]) < .001);
150 }
151 
152 function ‪random_vector(max_length)
153 {
154  return (RandomFloatRange(-1 * max_length, max_length), RandomFloatRange(-1 * max_length, max_length), RandomFloatRange(-1 * max_length, max_length));
155 }
156 
157 function ‪angle_dif(oldangle, newangle)
158 {
159  outvalue = ( oldangle - newangle ) % 360;
160 
161  if ( outvalue < 0 )
162  {
163  outvalue+=360;
164  }
165 
166  if ( outvalue > 180 )
167  {
168  outvalue=(outvalue-360)*-1;
169  }
170 
171  return outvalue;
172 }
173 
174 function ‪sign( x )
175 {
176  return ( x >= 0 ? 1 : -1 );
177 }
178 
179 function ‪randomSign()
180 {
181  return ( RandomIntRange( -1, 1 ) >= 0 ? 1 : -1 );
182 }
183 
198 function ‪get_dot_direction( v_point, b_ignore_z, b_normalize, str_direction, b_use_eye )
199 {
200  assert( isdefined( v_point ), "v_point is a required parameter for get_dot" );
201 
202  if ( !isdefined( b_ignore_z ) )
203  {
204  b_ignore_z = false;
205  }
206 
207  if ( !isdefined( b_normalize ) )
208  {
209  b_normalize = true;
210  }
211 
212  if ( !isdefined( str_direction ) )
213  {
214  str_direction = "forward";
215  }
216 
217  if ( !isdefined( b_use_eye ) )
218  {
219  b_use_eye = false;
220 
221  if ( IsPlayer( self ) )
222  {
223  b_use_eye = true;
224  }
225  }
226 
227  v_angles = self.angles;
228  v_origin = self.origin;
229 
230  if ( b_use_eye )
231  {
232  v_origin = self ‪util::get_eye();
233  }
234 
235  if ( IsPlayer( self ) )
236  {
237  v_angles = self GetPlayerAngles();
238  if ( level.wiiu )
239  {
240  v_angles = self GetGunAngles();
241  }
242  }
243 
244  if ( b_ignore_z )
245  {
246  v_angles = ( v_angles[ 0 ], v_angles[ 1 ], 0 );
247  v_point = ( v_point[ 0 ], v_point[ 1 ], 0 );
248  v_origin = ( v_origin[ 0 ], v_origin[ 1 ], 0 );
249  }
250 
251  switch ( str_direction )
252  {
253  case "forward":
254  v_direction = AnglesToForward( v_angles );
255  break;
256 
257  case "backward":
258  v_direction = AnglesToForward( v_angles ) * ( -1 );
259  break;
260 
261  case "right":
262  v_direction = AnglesToRight( v_angles );
263  break;
264 
265  case "left":
266  v_direction = AnglesToRight( v_angles ) * ( -1 );
267  break;
268 
269  case "up":
270  v_direction = AnglesToUp( v_angles );
271  break;
272 
273  case "down":
274  v_direction = AnglesToUp( v_angles ) * ( -1 );
275  break;
276 
277  default:
278  AssertMsg( str_direction + " is not a valid str_direction for get_dot!" );
279  v_direction = AnglesToForward( v_angles ); // have to initialize variable for default case
280  break;
281  }
282 
283  v_to_point = v_point - v_origin;
284 
285  if ( b_normalize )
286  {
287  v_to_point = VectorNormalize( v_to_point );
288  }
289 
290  n_dot = VectorDot( v_direction, v_to_point );
291 
292  return n_dot;
293 }
294 
307 function ‪get_dot_right( v_point, b_ignore_z, b_normalize )
308 {
309  // get_dot will assert if missing, but scripter should know it's coming from get_dot_right
310  assert( isdefined( v_point ), "v_point is a required parameter for get_dot_right" );
311 
312  n_dot = ‪get_dot_direction( v_point, b_ignore_z, b_normalize, "right" );
313 
314  return n_dot;
315 }
316 
329 function ‪get_dot_up( v_point, b_ignore_z, b_normalize )
330 {
331  // get_dot will assert if missing, but scripter should know it's coming from get_dot_up
332  assert( isdefined( v_point ), "v_point is a required parameter for get_dot_up" );
333 
334  n_dot = ‪get_dot_direction( v_point, b_ignore_z, b_normalize, "up" );
335 
336  return n_dot;
337 }
338 
351 function ‪get_dot_forward( v_point, b_ignore_z, b_normalize )
352 {
353  // get_dot will assert if missing, but scripter should know it's coming from get_dot_forward
354  assert( isdefined( v_point ), "v_point is a required parameter for get_dot_forward" );
355 
356  n_dot = ‪get_dot_direction( v_point, b_ignore_z, b_normalize, "forward" );
357 
358  return n_dot;
359 }
360 
374 function ‪get_dot_from_eye( v_point, b_ignore_z, b_normalize, str_direction )
375 {
376  assert( isdefined( v_point ), "v_point is a required parameter for get_dot_forward" );
377  Assert( ( IsPlayer( self ) || IsAI( self ) ), "get_dot_from_eye was used on a " + self.classname + ". Valid ents are players and AI, since they have tag_eye." );
378 
379  n_dot = ‪get_dot_direction( v_point, b_ignore_z, b_normalize, str_direction, true );
380 
381  return n_dot;
382 }
383 
384 // ----------------------------------------------------------------------------------------------------
385 // -- Arrays ------------------------------------------------------------------------------------------
386 // ----------------------------------------------------------------------------------------------------
387 
398 {
399  assert( IsArray( ‪array ) );
400  assert( ‪array.size > 0 );
401 
402  total = 0;
403 
404  for ( i = 0; i < ‪array.size; i++ )
405  {
406  total += ‪array[i];
407  }
408 
409  return ( total / ‪array.size );
410 }
411 
423 {
424  assert( IsArray( ‪array ) );
425  assert( ‪array.size > 0 );
426 
427  tmp = [];
428  for ( i = 0; i < ‪array.size; i++ )
429  {
430  tmp[i] = ( ‪array[i] - mean ) * ( ‪array[i] - mean );
431  }
432 
433  total = 0;
434  for ( i = 0; i < tmp.size; i++ )
435  {
436  total = total + tmp[i];
437  }
438 
439  return Sqrt( total / ‪array.size );
440 }
441 
454 function ‪random_normal_distribution( mean, std_deviation, lower_bound, upper_bound )
455 {
456  //pixbeginevent( "random_normal_distribution" );
457 
458  // implements the Box-Muller transform for Gaussian random numbers (http://en.wikipedia.org/wiki/Box-Muller_transform)
459  x1 = 0;
460  x2 = 0;
461  w = 1;
462  y1 = 0;
463 
464  while ( w >= 1 )
465  {
466  x1 = 2 * RandomFloatRange( 0, 1 ) - 1;
467  x2 = 2 * RandomFloatRange( 0, 1 ) - 1;
468  w = x1 * x1 + x2 * x2;
469  }
470 
471  w = Sqrt( ( -2.0 * Log( w ) ) / w );
472  y1 = x1 * w;
473 
474  number = mean + y1 * std_deviation;
475 
476  if ( isdefined( lower_bound ) && number < lower_bound )
477  {
478  number = lower_bound;
479  }
480 
481  if ( isdefined( upper_bound ) && number > upper_bound )
482  {
483  number = upper_bound;
484  }
485 
486  //pixendevent();
487 
488  return( number );
489 }
490 
491 function ‪closest_point_on_line( point, lineStart, lineEnd )
492 {
493  lineMagSqrd = lengthsquared(lineEnd - lineStart);
494 
495  t = ( ( ( point[0] - lineStart[0] ) * ( lineEnd[0] - lineStart[0] ) ) +
496  ( ( point[1] - lineStart[1] ) * ( lineEnd[1] - lineStart[1] ) ) +
497  ( ( point[2] - lineStart[2] ) * ( lineEnd[2] - lineStart[2] ) ) ) /
498  ( lineMagSqrd );
499 
500  if( t < 0.0 )
501  {
502  return lineStart;
503  }
504  else if( t > 1.0 )
505  {
506  return lineEnd;
507  }
508 
509  start_x = lineStart[0] + t * ( lineEnd[0] - lineStart[0] );
510  start_y = lineStart[1] + t * ( lineEnd[1] - lineStart[1] );
511  start_z = lineStart[2] + t * ( lineEnd[2] - lineStart[2] );
512 
513  return (start_x,start_y,start_z);
514 }
515 
516 function ‪get_2d_yaw( start, ‪end )
517 {
518  vector = (‪end[0] - start[0], ‪end[1] - start[1], 0);
519 
520  return ‪vec_to_angles( vector );
521 }
522 
523 function ‪vec_to_angles( vector )
524 {
525  yaw = 0;
526 
527  vecX = vector[0];
528  vecY = vector[1];
529 
530  if ( vecX == 0 && vecY == 0 )
531  return 0;
532 
533  if ( vecY < 0.001 && vecY > -0.001 )
534  vecY = 0.001;
535 
536  yaw = atan( vecX / vecY );
537 
538  if ( vecY < 0 )
539  yaw += 180;
540 
541  return ( 90 - yaw );
542 }
543 
544 function ‪pow( base, exp )
545 {
546  if( exp == 0 )
547  {
548  return 1;
549  }
550 
551  ‪result = base;
552  for( i = 0; i < ( exp - 1 ); i++ )
553  {
554  ‪result *= base;
555  }
556 
557  return ‪result;
558 }
‪expand_maxs
‪function expand_maxs(maxs, point)
Definition: math_shared.gsc:114
‪randomSign
‪function randomSign()
Definition: math_shared.gsc:179
‪find_box_center
‪function find_box_center(mins, maxs)
Definition: math_shared.gsc:86
‪get_dot_from_eye
‪function get_dot_from_eye(v_point, b_ignore_z, b_normalize, str_direction)
Definition: math_shared.gsc:374
‪get_dot_up
‪function get_dot_up(v_point, b_ignore_z, b_normalize)
Definition: math_shared.gsc:329
‪get_dot_right
‪function get_dot_right(v_point, b_ignore_z, b_normalize)
Definition: math_shared.gsc:307
‪pow
‪function pow(base, exp)
Definition: math_shared.gsc:544
‪closest_point_on_line
‪function closest_point_on_line(point, lineStart, lineEnd)
Definition: math_shared.gsc:491
‪get_2d_yaw
‪function get_2d_yaw(start, end)
Definition: math_shared.gsc:516
‪lag
‪function lag(desired, curr, k, dt)
Definition: math_shared.gsc:69
‪DEFAULT
‪#define DEFAULT(__var, __default)
Definition: shared.gsh:270
‪cointoss
‪function cointoss()
Definition: math_shared.gsc:7
‪random_normal_distribution
‪function random_normal_distribution(mean, std_deviation, lower_bound, upper_bound)
Definition: math_shared.gsc:454
‪array_std_deviation
‪function array_std_deviation(array, mean)
Definition: math_shared.gsc:422
‪end
‪function end(final)
Definition: _killcam.gsc:511
‪clamp
‪function clamp(val, val_min, val_max)
Definition: math_shared.gsc:23
‪angle_dif
‪function angle_dif(oldangle, newangle)
Definition: math_shared.gsc:157
‪expand_mins
‪function expand_mins(mins, point)
Definition: math_shared.gsc:94
‪array
‪function filter array
Definition: array_shared.csc:16
‪linear_map
‪function linear_map(num, min_a, max_a, min_b, max_b)
Definition: math_shared.gsc:52
‪array_average
‪function array_average(array)
Definition: math_shared.gsc:397
‪random_vector
‪function random_vector(max_length)
Definition: math_shared.gsc:152
‪vec_to_angles
‪function vec_to_angles(vector)
Definition: math_shared.gsc:523
‪get_dot_forward
‪function get_dot_forward(v_point, b_ignore_z, b_normalize)
Definition: math_shared.gsc:351
‪result
‪function result(death, attacker, mod, weapon)
Definition: _zm_aat_blast_furnace.gsc:46
‪get_dot_direction
‪function get_dot_direction(v_point, b_ignore_z, b_normalize, str_direction, b_use_eye)
Definition: math_shared.gsc:198
‪sign
‪function sign(x)
Definition: math_shared.gsc:174
‪vector_compare
‪function vector_compare(vec1, vec2)
Definition: math_shared.gsc:147
‪get_eye
‪function get_eye()
Definition: util_shared.csc:948