‪Black Ops 3 Source Code Explorer  0.1
‪An script explorer for Black Ops 3 by ZeRoY
array_shared.csc
Go to the documentation of this file.
1 #using scripts\codescripts\struct;
2 
3 #using scripts\shared\array_shared;
4 #using scripts\shared\flag_shared;
5 #using scripts\shared\flagsys_shared;
6 #using scripts\shared\util_shared;
7 
8 #insert scripts\shared\shared.gsh;
9 
10 #namespace array;
11 
15 function filter( &‪array, b_keep_keys, func_filter, arg1, arg2, arg3, arg4, arg5 )
16 {
17  ‪a_new = [];
18 
19  foreach ( key, val in ‪array )
20  {
21  if ( ‪util::single_func( self, func_filter, val, arg1, arg2, arg3, arg4, arg5 ) )
22  {
23  if ( IsString( key ) || IsWeapon( key ) )
24  {
25  // by default, string keys are kept
26  if ( isdefined( b_keep_keys ) && !b_keep_keys )
27  {
28  ‪a_new[ ‪a_new.size ] = val;
29  }
30  else
31  {
32  ‪a_new[ key ] = val;
33  }
34  }
35  else
36  {
37  // by default, int keys are not kept
38  if ( ‪IS_TRUE( b_keep_keys ) )
39  {
40  ‪a_new[ key ] = val;
41  }
42  else
43  {
44  ‪a_new[ ‪a_new.size ] = val;
45  }
46  }
47  }
48  }
49 
50  return ‪a_new;
51 }
52 
56 function ‪remove_undefined( ‪array, b_keep_keys )
57 {
58  if ( IsDefined( b_keep_keys ) )
59  {
60  ArrayRemoveValue( ‪array, undefined, b_keep_keys );
61  }
62  else
63  {
64  ArrayRemoveValue( ‪array, undefined );
65  }
66 
67  return ‪array;
68 }
69 
70 function get_touching( &‪array, b_keep_keys )
71 {
72  return filter( ‪array, b_keep_keys, &IsTouching );
73 }
74 
86 function ‪remove_index( ‪array, index, b_keep_keys )
87 {
88  ‪a_new = [];
89 
90  foreach ( key, val in ‪array )
91  {
92  if( key == index )
93  {
94  continue;
95  }
96  else
97  {
98  if ( ‪IS_TRUE( b_keep_keys ) )
99  {
100  ‪a_new[ key ] = val;
101  }
102  else
103  {
104  ‪a_new[ ‪a_new.size ] = val;
105  }
106  }
107  }
108 
109  return ‪a_new;
110 }
111 
121 function delete_all( &‪array, is_struct )
122 {
123  foreach ( ent in ‪array )
124  {
125  if ( isdefined( ent ) )
126  {
127  if ( ‪IS_TRUE( is_struct ) )
128  {
129  ent ‪struct::delete();
130  }
131  else if ( isdefined( ent.__vtable ) )
132  {
133  ‪DELETE( ent ); // class
134  }
135  else
136  {
137  ent Delete();
138  }
139  }
140  }
141 }
142 
153 function notify_all( &‪array, str_notify )
154 {
155  foreach ( elem in ‪array )
156  {
157  elem notify( str_notify );
158  }
159 }
160 
178 function thread_all( &entities, func, arg1, arg2, arg3, arg4, arg5, arg6 )
179 {
180  Assert( isdefined( entities ), "Undefined entity array passed to array::thread_all" );
181  Assert( isdefined( func ), "Undefined function passed to array::thread_all" );
182 
183  if ( IsArray( entities ) )
184  {
185  if ( isdefined( arg6 ) )
186  {
187  foreach ( ent in entities )
188  {
189  ent thread [[ func ]]( arg1, arg2, arg3, arg4, arg5, arg6 );
190  }
191  }
192  else if ( isdefined( arg5 ) )
193  {
194  foreach ( ent in entities )
195  {
196  ent thread [[ func ]]( arg1, arg2, arg3, arg4, arg5 );
197  }
198  }
199  else if ( isdefined( arg4 ) )
200  {
201  foreach ( ent in entities )
202  {
203  ent thread [[ func ]]( arg1, arg2, arg3, arg4 );
204  }
205  }
206  else if ( isdefined( arg3 ) )
207  {
208  foreach ( ent in entities )
209  {
210  ent thread [[ func ]]( arg1, arg2, arg3 );
211  }
212  }
213  else if ( isdefined( arg2 ) )
214  {
215  foreach ( ent in entities )
216  {
217  ent thread [[ func ]]( arg1, arg2 );
218  }
219  }
220  else if ( isdefined( arg1 ) )
221  {
222  foreach ( ent in entities )
223  {
224  ent thread [[ func ]]( arg1 );
225  }
226  }
227  else
228  {
229  foreach ( ent in entities )
230  {
231  ent thread [[ func ]]();
232  }
233  }
234  }
235  else
236  {
237  ‪util::single_thread( entities, func, arg1, arg2, arg3, arg4, arg5, arg6 );
238  }
239 }
240 
257 function thread_all_ents( &entities, func, arg1, arg2, arg3, arg4, arg5 )
258 {
259  Assert( isdefined( entities ), "Undefined entity array passed to util::array_ent_thread" );
260  Assert( isdefined( func ), "Undefined function passed to util::array_ent_thread" );
261 
262  if ( IsArray( entities ) )
263  {
264  if ( entities.size )
265  {
266  keys = GetArrayKeys( entities );
267  for ( i = 0; i < keys.size; i++ )
268  {
269  ‪util::single_thread( self, func, entities[keys[i]], arg1, arg2, arg3, arg4, arg5 );
270  }
271  }
272  }
273  else
274  {
275  ‪util::single_thread( self, func, entities, arg1, arg2, arg3, arg4, arg5 );
276  }
277 }
278 
296 function run_all( &entities, func, arg1, arg2, arg3, arg4, arg5, arg6 )
297 {
298  Assert( isdefined( entities ), "Undefined entity array passed to array::run_all" );
299  Assert( isdefined( func ), "Undefined function passed to array::run_all" );
300 
301  if ( IsArray( entities ) )
302  {
303  if ( isdefined( arg6 ) )
304  {
305  foreach ( ent in entities )
306  {
307  ent [[ func ]]( arg1, arg2, arg3, arg4, arg5, arg6 );
308  }
309  }
310  else if ( isdefined( arg5 ) )
311  {
312  foreach ( ent in entities )
313  {
314  ent [[ func ]]( arg1, arg2, arg3, arg4, arg5 );
315  }
316  }
317  else if ( isdefined( arg4 ) )
318  {
319  foreach ( ent in entities )
320  {
321  ent [[ func ]]( arg1, arg2, arg3, arg4 );
322  }
323  }
324  else if ( isdefined( arg3 ) )
325  {
326  foreach ( ent in entities )
327  {
328  ent [[ func ]]( arg1, arg2, arg3 );
329  }
330  }
331  else if ( isdefined( arg2 ) )
332  {
333  foreach ( ent in entities )
334  {
335  ent [[ func ]]( arg1, arg2 );
336  }
337  }
338  else if ( isdefined( arg1 ) )
339  {
340  foreach ( ent in entities )
341  {
342  ent [[ func ]]( arg1 );
343  }
344  }
345  else
346  {
347  foreach ( ent in entities )
348  {
349  ent [[ func ]]();
350  }
351  }
352  }
353  else
354  {
355  ‪util::single_func( entities, func, arg1, arg2, arg3, arg4, arg5, arg6 );
356  }
357 }
358 
370 function exclude( ‪array, array_exclude )// returns "array" minus all members of array_exclude
371 {
372  newarray = ‪array;
373 
374  if ( IsArray( array_exclude ) )
375  {
376  for ( i = 0; i < array_exclude.size; i++ )
377  {
378  ArrayRemoveValue( newarray, array_exclude[ i ] );
379  }
380  }
381  else
382  {
383  ArrayRemoveValue( newarray, array_exclude );
384  }
385 
386  return newarray;
387 }
388 
400 function ‪add( &‪array, item, allow_dupes = true )
401 {
402  if ( isdefined( item ) )
403  {
404  if ( allow_dupes || !IsInArray( ‪array, item ) )
405  {
406  ‪array[ ‪array.size ] = item;
407  }
408  }
409 
410  return ‪array;
411 }
412 
425 function add_sorted( &‪array, item, allow_dupes = true )
426 {
427  if ( isdefined( item ) )
428  {
429  if ( allow_dupes || !IsInArray( ‪array, item ) )
430  {
431  for ( i = 0; i <= ‪array.size; i++ )
432  {
433  if ( ( i == ‪array.size ) || ( item <= ‪array[i] ) )
434  {
435  ArrayInsert( ‪array, item, i );
436  break;
437  }
438  }
439  }
440  }
441 }
442 
454 function ‪wait_till( &‪array, msg, n_timeout )
455 {
456  ‪TIMEOUT( n_timeout );
457 
458  s_tracker = SpawnStruct();
459  s_tracker._wait_count = 0;
460 
461  foreach ( ent in ‪array )
462  {
463  if ( isdefined( ent ) )
464  {
465  ent thread ‪util::timeout( n_timeout, &‪util::_waitlogic, s_tracker, msg );
466  }
467  }
468 
469  if ( s_tracker._wait_count > 0 )
470  {
471  s_tracker waittill( "waitlogic_finished" );
472  }
473 }
474 
478 function flag_wait( &‪array, str_flag )
479 {
480  for ( i = 0; i < ‪array.size; i++ )
481  {
482  ent = ‪array[i];
483  if ( !ent ‪flag::get( str_flag ) )
484  {
485  ent waittill( str_flag );
486  i = -1;
487  }
488  }
489 }
490 
494 function flagsys_wait( &‪array, str_flag )
495 {
496  for ( i = 0; i < ‪array.size; i++ )
497  {
498  ent = ‪array[i];
499  if ( !ent ‪flagsys::get( str_flag ) )
500  {
501  ent waittill( str_flag );
502  i = -1;
503  }
504  }
505 }
506 
510 function flagsys_wait_any_flag( &‪array, ... )
511 {
512  for ( i = 0; i < ‪array.size; i++ )
513  {
514  ent = ‪array[i];
515 
516  if ( isdefined( ent ) )
517  {
518  b_flag_set = false;
519  foreach ( str_flag in vararg )
520  {
521  if ( ent ‪flagsys::get( str_flag ) )
522  {
523  b_flag_set = true;
524  break;
525  }
526  }
527 
528  if ( !b_flag_set )
529  {
530  ent ‪util::waittill_any_array( vararg );
531  i = -1;
532  }
533  }
534  }
535 }
536 
540 function flag_wait_clear( &‪array, str_flag )
541 {
542  for ( i = 0; i < ‪array.size; i++ )
543  {
544  ent = ‪array[i];
545  if ( ent ‪flag::get( str_flag ) )
546  {
547  ent waittill( str_flag );
548  i = -1;
549  }
550  }
551 }
552 
556 function flagsys_wait_clear( &‪array, str_flag )
557 {
558  for ( i = 0; i < ‪array.size; i++ )
559  {
560  ent = ‪array[i];
561  if ( ent ‪flagsys::get( str_flag ) )
562  {
563  ent waittill( str_flag );
564  i = -1;
565  }
566  }
567 }
568 
580 function wait_any( ‪array, msg, n_timeout )
581 {
582  ‪TIMEOUT( n_timeout );
583 
584  s_tracker = SpawnStruct();
585 
586  a_structs = [];
587  foreach ( ent in ‪array )
588  {
589  if ( isdefined( ent ) )
590  {
591  s = SpawnStruct();
592  s thread ‪util::timeout( n_timeout, &_waitlogic2, s_tracker, ent, msg );
593  ‪ARRAY_ADD( a_structs, s );
594  }
595  }
596 
597  s_tracker endon( "array_wait" );
598 
599  ‪wait_till( ‪array, "death" );
600 }
601 
602 function _waitlogic2( s_tracker, ent, msg )
603 {
604  s_tracker endon( "array_wait" );
605  ent endon( "death" );
606 
607  ent waittill( msg );
608  s_tracker notify( "array_wait" );
609 }
610 
611 function flag_wait_any( ‪array, str_flag )
612 {
613  self endon( "death" );
614 
615  foreach ( ent in ‪array )
616  {
617  if ( ent ‪flag::get( str_flag ) )
618  {
619  return ent;
620  }
621  }
622 
623  wait_any( ‪array, str_flag );
624 }
625 
635 function random( ‪array )
636 {
637  keys = GetArrayKeys( ‪array );
638  return ‪array[ keys[RandomInt( keys.size )] ];
639 }
640 
644 function randomize( ‪array )
645 {
646  for ( i = 0; i < ‪array.size; i++ )
647  {
648  j = RandomInt( ‪array.size );
649  temp = ‪array[ i ];
650  ‪array[ i ] = ‪array[ j ];
651  ‪array[ j ] = temp;
652  }
653 
654  return ‪array;
655 }
656 
667 function ‪reverse( ‪array )
668 {
669  a_array2 = [];
670  for ( i = ‪array.size - 1; i >= 0; i-- )
671  {
672  a_array2[ a_array2.size ] = ‪array[ i ];
673  }
674 
675  return a_array2;
676 }
677 
681 function remove_keys( ‪array )
682 {
683  ‪a_new = [];
684 
685  foreach ( _, val in ‪array )
686  {
687  if ( isdefined( val ) )
688  {
689  ‪a_new[ ‪a_new.size ] = val;
690  }
691  }
692 
693  return ‪a_new;
694 }
695 
699 function swap( &‪array, index1, index2 )
700 {
701  assert( index1 < ‪array.size, "index1 to swap out of range" );
702  assert( index2 < ‪array.size, "index2 to swap out of range" );
703 
704  temp = ‪array[index1];
705  ‪array[index1] = ‪array[index2];
706  ‪array[index2] = temp;
707 }
708 
709 function pop( &‪array, index, b_keep_keys = true )
710 {
711  if ( ‪array.size > 0 )
712  {
713  if ( !isdefined( index ) )
714  {
715  keys = GetArrayKeys( ‪array );
716  index = keys[ 0 ];
717  }
718 
719  if ( isdefined( ‪array[index] ) )
720  {
721  ret = ‪array[ index ];
722 
723  ArrayRemoveIndex( ‪array, index, b_keep_keys );
724 
725  return ret;
726  }
727  }
728 }
729 
730 function pop_front( &‪array, b_keep_keys = true )
731 {
732  keys = GetArrayKeys( ‪array );
733  index = keys[ keys.size - 1 ];
734  return pop( ‪array, index, b_keep_keys );
735 }
736 
737 function push( &‪array, val, index )
738 {
739  if ( !isdefined( index ) )
740  {
741  // use max free integer as index
742  index = 0;
743  foreach ( key in GetArrayKeys( ‪array ) )
744  {
745  if ( IsInt( key ) && ( key >= index ) )
746  {
747  index = key + 1;
748  }
749  }
750  }
751 
752  ArrayInsert( ‪array, val, index );
753 }
754 
755 function push_front( &‪array, val )
756 {
757  push( ‪array, val, 0 );
758 }
759 
772 function get_closest( org, &‪array, dist = undefined)
773 {
774  assert( 0, "Deprecated function. Use 'ArrayGetClosest' instead." );
775 }
776 
789 function get_farthest( org, &‪array, dist = undefined )
790 {
791  assert( 0, "Deprecated function. Use 'ArrayGetFarthest' instead." );
792 }
793 
794 function closerFunc( dist1, dist2 )
795 {
796  return dist1 >= dist2;
797 }
798 
799 function fartherFunc( dist1, dist2 )
800 {
801  return dist1 <= dist2;
802 }
803 
815 function get_all_farthest( org, &‪array, excluders, max )
816 {
817  sorted_array = get_closest( org, ‪array, excluders );
818 
819  if(isdefined( max ))
820  {
821  temp_array = [];
822  for( i=0; i < sorted_array.size; i++)
823  {
824  temp_array[temp_array.size] = sorted_array[sorted_array.size - i];
825  }
826  sorted_array = temp_array;
827  }
828 
829  sorted_array = ‪array::reverse( sorted_array );
830 
831  return( sorted_array );
832 }
833 
848 function get_all_closest( org, &‪array, excluders, max, maxdist )
849 {
850  // pass an array of entities to this function and it will return them in the order of closest
851  // to the origin you pass, you can also set max to limit how many ents get returned
852  if( !isdefined( max ) )
853  max = ‪array.size;
854  if( !isdefined( excluders ) )
855  excluders = [];
856 
857  maxdists2rd = undefined;
858  if( isdefined( maxdist ) )
859  maxdists2rd = maxdist * maxdist;
860 
861  // return the array, reordered from closest to farthest
862  dist = [];
863  index = [];
864  for( i = 0;i < ‪array.size;i ++ )
865  {
866  if(!isdefined(‪array[i]))
867  continue;
868 
869  excluded = false;
870  for( p = 0;p < excluders.size;p ++ )
871  {
872  if( ‪array[ i ] != excluders[ p ] )
873  continue;
874  excluded = true;
875  break;
876  }
877  if( excluded )
878  continue;
879 
880  length = distancesquared( org, ‪array[ i ].origin );
881 
882  if( isdefined( maxdists2rd ) && maxdists2rd < length )
883  continue;
884 
885  dist[ dist.size ] = length;
886 
887 
888  index[ index.size ] = i;
889  }
890 
891  for( ;; )
892  {
893  ‪change = false;
894  for( i = 0;i < dist.size - 1;i ++ )
895  {
896  if( dist[ i ] <= dist[ i + 1 ] )
897  continue;
898  ‪change = true;
899  temp = dist[ i ];
900  dist[ i ] = dist[ i + 1 ];
901  dist[ i + 1 ] = temp;
902  temp = index[ i ];
903  index[ i ] = index[ i + 1 ];
904  index[ i + 1 ] = temp;
905  }
906  if( !‪change )
907  break;
908  }
909 
910  newArray = [];
911  if( max > dist.size )
912  max = dist.size;
913  for( i = 0;i < max;i ++ )
914  newArray[ i ] = ‪array[ index[ i ] ];
915  return newArray;
916 }
917 
918 function alphabetize( &‪array )
919 {
920  return sort_by_value( ‪array, true );
921 }
922 
934 //Use ArraySort for distance based sorting of entities
935 function sort_by_value( &‪array, b_lowest_first = false )
936 {
937  return merge_sort( ‪array, &_sort_by_value_compare_func, b_lowest_first );
938 }
939 
940 function _sort_by_value_compare_func( val1, val2, b_lowest_first )
941 {
942  if ( b_lowest_first )
943  {
944  return val1 < val2;
945  }
946  else
947  {
948  return val1 > val2;
949  }
950 }
951 
963 //Use ArraySort for distance based sorting of entities
964 function sort_by_script_int( &‪a_ents, b_lowest_first = false )
965 {
966  return merge_sort( ‪a_ents, &_sort_by_script_int_compare_func, b_lowest_first );
967 }
968 
969 function _sort_by_script_int_compare_func( e1, e2, b_lowest_first )
970 {
971  if ( b_lowest_first )
972  {
973  return e1.script_int < e2.script_int;
974  }
975  else
976  {
977  return e1.script_int > e2.script_int;
978  }
979 }
980 
981 function merge_sort( &current_list, func_sort, param )
982 {
983  if ( current_list.size <= 1 )
984  {
985  return current_list;
986  }
987 
988  left = [];
989  right = [];
990 
991  middle = current_list.size / 2;
992 
993  for ( x = 0; x < middle; x++ )
994  {
995  ‪ARRAY_ADD( left, current_list[ x ] );
996  }
997 
998  for ( ; x < current_list.size; x++ )
999  {
1000  ‪ARRAY_ADD( right, current_list[ x ] );
1001  }
1002 
1003  left = merge_sort( left, func_sort, param );
1004  right = merge_sort( right, func_sort, param );
1005 
1006  ‪result = merge( left, right, func_sort, param );
1007 
1008  return ‪result;
1009 }
1010 
1011 function merge( left, right, func_sort, param )
1012 {
1013  ‪result = [];
1014 
1015  li = 0;
1016  ri = 0;
1017  while ( li < left.size && ri < right.size )
1018  {
1019  b_result = undefined;
1020 
1021  if ( isdefined( param ) )
1022  {
1023  b_result = [[ func_sort ]]( left[ li ], right[ ri ], param );
1024  }
1025  else
1026  {
1027  b_result = [[ func_sort ]]( left[ li ], right[ ri ] );
1028  }
1029 
1030  if ( b_result )
1031  {
1032  ‪result[ ‪result.size ] = left[ li ];
1033  li++;
1034  }
1035  else
1036  {
1037  ‪result[ ‪result.size ] = right[ ri ];
1038  ri++;
1039  }
1040  }
1041 
1042  while ( li < left.size )
1043  {
1044  ‪result[ ‪result.size ] = left[ li ];
1045  li++;
1046  }
1047 
1048  while ( ri < right.size )
1049  {
1050  ‪result[ ‪result.size ] = right[ ri ];
1051  ri++;
1052  }
1053 
1054  return ‪result;
1055 }
1056 
1072 function spread_all( &entities, func, arg1, arg2, arg3, arg4, arg5 )
1073 {
1074  Assert( isdefined( entities ), "Undefined entity array passed to array::spread_all_ents" );
1075  Assert( isdefined( func ), "Undefined function passed to array::spread_all_ents" );
1076 
1077  if ( IsArray( entities ) )
1078  {
1079  foreach ( ent in entities )
1080  {
1081  ‪util::single_thread( ent, func, arg1, arg2, arg3, arg4, arg5 );
1082  ‪WAIT_ABOUT( .1 );
1083  }
1084  }
1085  else
1086  {
1087  ‪util::single_thread( entities, func, arg1, arg2, arg3, arg4, arg5 );
1088  ‪WAIT_ABOUT( .1 );
1089  }
1090 }
1091 
‪timeout
‪function timeout(n_time, func, arg1, arg2, arg3, arg4, arg5, arg6)
Definition: util_shared.csc:762
‪waittill_any_array
‪function waittill_any_array(a_notifies)
Definition: util_shared.csc:392
‪a_new
‪return a_new
Definition: array_shared.csc:50
‪remove_index
‪function remove_index(array, index, b_keep_keys)
Definition: array_shared.csc:86
‪IS_TRUE
‪#define IS_TRUE(__a)
Definition: shared.gsh:251
‪get
‪function get(kvp_value, kvp_key="targetname")
Definition: struct.csc:13
‪reverse
‪function reverse()
Definition: traps_shared.gsc:1071
‪add
‪function add(entity, dyingplayer, team, timeout)
Definition: _deathicons.gsc:43
‪TIMEOUT
‪#define TIMEOUT(__t)
Definition: shared.gsh:218
‪ARRAY_ADD
‪#define ARRAY_ADD(__array, __item)
Definition: shared.gsh:304
‪DELETE
‪#define DELETE(__obj)
Definition: shared.gsh:444
‪remove_undefined
‪function remove_undefined(array, b_keep_keys)
Definition: array_shared.csc:56
‪wait_till
‪function wait_till(str_flag)
Definition: flag_shared.csc:189
‪array
‪function filter array
Definition: array_shared.csc:16
‪WAIT_ABOUT
‪#define WAIT_ABOUT(__time)
Definition: shared.gsh:436
‪a_ents
‪function _query_ents_by_substring_helper a_ents
Definition: util_shared.gsc:3334
‪_waitlogic
‪function _waitlogic(s_tracker, notifies)
Definition: util_shared.csc:182
‪single_thread
‪function single_thread(entity, func, arg1, arg2, arg3, arg4, arg5, arg6)
Definition: util_shared.csc:699
‪change
‪function change(team)
Definition: _teams.gsc:258
‪result
‪function result(death, attacker, mod, weapon)
Definition: _zm_aat_blast_furnace.gsc:46
‪single_func
‪function single_func(entity, func, arg1, arg2, arg3, arg4, arg5, arg6)
Definition: util_shared.csc:563
‪delete
‪function delete()
Definition: struct.csc:2