-
Notifications
You must be signed in to change notification settings - Fork 8
/
foreach.inc
1888 lines (1602 loc) · 44.7 KB
/
foreach.inc
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
/*----------------------------------------------------------------------------
===========================
foreach efficient looping
===========================
Description:
Provides efficient looping through sparse data sets, such as connected
players. Significantly improved from the original version to be a generic
loop system, rather then purely a player loop system. When used for
players this has constant time O(n) for number of connected players (n),
unlike standard player loops which are O(MAX_PLAYERS), regardless of the
actual number of connected players. Even when n is MAX_PLAYERS this is
still faster.
Legal:
Version: MPL 1.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is the YSI foreach include.
The Initial Developer of the Original Code is Alex "Y_Less" Cole.
Portions created by the Initial Developer are Copyright (C) 2011
the Initial Developer. All Rights Reserved.
Contributors:
ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice, Kar
Thanks:
JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
ZeeX - Very productive conversations.
koolk - IsPlayerinAreaEx code.
TheAlpha - Danish translation.
breadfish - German translation.
Fireburn - Dutch translation.
yom - French translation.
50p - Polish translation.
Zamaroht - Spanish translation.
Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
for me to strive to better.
Pixels^ - Running XScripters where the idea was born.
Matite - Pestering me to release it and using it.
Very special thanks to:
Thiadmer - PAWN, whose limits continue to amaze me!
Kye/Kalcor - SA:MP.
SA:MP Team past, present and future - SA:MP.
Version:
19 (0.4.2)
Changelog:
14/05/15:
Cleaned up the entire include.
16/07/12:
Fixed a bug with "loadfs" blocking callbacks.
Slightly reduced the memory consumption of constant strings.
25/05/12:
Added a delay timer to remove players for "Kick" crashes.
05/01/12:
Fixed multi-dimensional iterators.
Fixed "FOREACH_NO_BOTS".
Made "Iterator:" support multi-dimensional arrays.
07/12/11:
Underlying implementation tweak for more consistent code.
Added Iter_Contains.
06/12/11:
Minor fixes.
31/10/11:
Changed the underlying loop code to be slightly faster.
Added support for Iter_SafeRemove, prompting refactoring.
17/09/11:
Fixed arrays under the new syntax.
28/04/11:
Moved iterator identifiers to end of variables.
Rewrote "foreach" to accept two syntaxes for "foreach (new i : Iter)".
16/08/10:
Removed all the "2" versions of the functions.
14/08/10:
Added Iter_Clear to reset an array.
06/08/10:
Added special array declaration format.
18/12/09:
Added Iter_Func2 functions for multi-dimensional iterators.
Renamed foreact et al as keywords in the documentation.
Changed licensing from GPL to MPL.
02/09/09:
Fixed (again) for 0.3.
Added free slot finding.
21/08/09:
Updated to include random functions.
Made entirely stand alone.
Ported to 0.3 (separate version).
Added automatic callback hook code.
Removed debug information from stand alone version.
06/01/08:
Added debug information.
09/10/07:
Moved to system.
16/09/07:
Added list sorting.
Made this part of Y SeRver Includes, not Y Sever Includes.
Made list sorting optional.
Fixed version number.
08/09/07:
First version.
Functions:
Public:
OnPlayerDisconnect - Called when a player leaves to remove them.
OnPlayerConnect - Called when a player connects to add them.
Core:
-
Stock:
Iter_AddInternal - Add a value to an iterator.
Iter_RemoveInternal - Remove a value from an iterator.
Iter_RandomInternal - Get a random item from an iterator.
Iter_FreeInternal - Gets the first free slot in the iterator.
Iter_InitInternal - Initialises a multi-dimensional iterator.
Iter_ContainsInternal - Checks if a value is in an iterator.
Static:
-
Inline:
Iter_Create - Create a new iterator value set.
Iter_Add - Wraps Iter_AddInternal.
Iter_SafeRemove - Wraps Iter_SafeRemoveInternal.
Iter_Remove - Wraps Iter_RemoveInternal.
Iter_Random - Wraps Iter_RandomInternal.
Iter_Count - Gets the number of items in an iterator.
Iter_Free - Wraps around Iter_FreeInternal.
Iter_Contains - Wraps around Iter_ContainsInternal.
API:
-
Callbacks:
-
Hooks:
Iter_OnPlayerConnect - Hook for the OnPlayerConnect callback.
Iter_OnPlayerDisconnect - Hook for the OnPlayerDisconnect callback.
Iter_OnGameModeInit - Only exists to make the code compile correctly...
Definitions:
-
Enums:
-
Macros:
-
Keywords:
foreach - Function to loop an iterator.
Tags:
Iterator - Declare an iterator.
Variables:
Global:
-
Static:
-
Commands:
-
Operators:
-
Iterators:
Player - List of all players connected.
Bot - List of all bots (npcs) connected.
NPC - Alias of Bot.
Character - All players and bots.
--------------------------------------------------------------------------------
*/
#define _FOREACH_LOCAL_VERSION 19
// Foreach is testing us.
#if defined _FOREACH_INC_TEST
#endinput
#endif
#define _FOREACH_INC_TEST
#if !defined _samp_included
#error "Please include a_samp or a_npc before foreach"
#endif
#if defined FOREACH_PLAYERSSTREAM_ITERATOR
#error FOREACH_PLAYERSSTREAM_ITERATOR is deprecated, use FOREACH_I_PlayerPlayersStream
#endif
#if defined FOREACH_VEHICLESSTREAM_ITERATOR
#error FOREACH_VEHICLESSTREAM_ITERATOR is deprecated, use FOREACH_I_PlayerVehiclesStream
#endif
#if defined FOREACH_ACTORSSTREAM_ITERATOR
#error FOREACH_ACTORSSTREAM_ITERATOR is deprecated, use FOREACH_I_PlayerActorsStream
#endif
#if defined PlayersStream@YSII_Cg
#error PlayersStream is deprecated, use PlayerPlayersStream
#endif
#if defined VehiclesStream@YSII_Cg
#error VehiclesStream is deprecated, use PlayerVehiclesStream
#endif
#if defined ActorsStream@YSII_Cg
#error ActorsStream is deprecated, use PlayerActorsStream
#endif
/*
--------------------------------------------------------------------------------
Base defines
--------------------------------------------------------------------------------
*/
#define ITER_NONE -1
#define _Y_ITER_ARRAY: _:_Y_ITER_C0:
#define _Y_ITER_ARRAY_SIZE(%1) _:_Y_ITER_C1:_Y_ITER_C2:sizeof %1@YSII_Ag-1
#define _Y_ITER_C0:%0[%1]@YSII_%4g%3) %0@YSII_%4g[%1]%3)
#define _Y_ITER_C1:_Y_ITER_C2:%0[%1]@YSII_Ag%3) %0@YSII_Ag[]%3)
#define _Y_ITER_C2:sizeof%0(%1)@YSII_Ag-1;_:(%2=_Y_ITER_ARRAY:%3(%4)@YSII_Ag[%5])!=_Y_ITER_ARRAY_SIZE(%6);) -1;_:(%2=%3@YSII_Ag(%4,%5))!=-1;)
#define _Y_ITER_C3:%0[%1]@YSII_Cg,%2[%3]@YSII_Ag[%4]={%5} _Y_ITER_C3:%0@YSII_Cg[%1],%0@YSII_Ag[%1][%4]
forward Iter_AddInternal(&count, array[], value, size);
forward Iter_RemoveInternal(&count, array[], value, size);
forward Iter_ClearInternal(&count, array[], size);
forward Iter_OPDCInternal(playerid);
static
bool:Iter_gIsFilterscript = false;
#if !defined FOREACH_I_Player
#define FOREACH_I_Player 1
#endif
#if !defined FOREACH_I_Bot
#define FOREACH_I_Bot 1
#endif
#if !defined FOREACH_I_Character
#define FOREACH_I_Character 1
#endif
#if !defined FOREACH_I_Vehicle
#define FOREACH_I_Vehicle 1
#endif
#if !defined FOREACH_I_Actor
#define FOREACH_I_Actor 1
#endif
#if !defined FOREACH_I_PlayerPlayersStream
#define FOREACH_I_PlayerPlayersStream 0
#else
#undef FOREACH_I_PlayerPlayersStream
#define FOREACH_I_PlayerPlayersStream 1
#endif
#if !defined FOREACH_I_PlayerVehiclesStream
#define FOREACH_I_PlayerVehiclesStream 0
#else
#undef FOREACH_I_PlayerVehiclesStream
#define FOREACH_I_PlayerVehiclesStream 1
#endif
#if !defined FOREACH_I_VehiclePlayersStream
#define FOREACH_I_VehiclePlayersStream 0
#else
#undef FOREACH_I_VehiclePlayersStream
#define FOREACH_I_VehiclePlayersStream 1
#endif
#if !defined FOREACH_I_PlayerActorsStream
#define FOREACH_I_PlayerActorsStream 0
#else
#undef FOREACH_I_PlayerActorsStream
#define FOREACH_I_PlayerActorsStream 1
#endif
#if !defined FOREACH_I_ActorPlayersStream
#define FOREACH_I_ActorPlayersStream 0
#else
#undef FOREACH_I_ActorPlayersStream
#define FOREACH_I_ActorPlayersStream 1
#endif
#if !defined FOREACH_I_PlayerInVehicle
#define FOREACH_I_PlayerInVehicle 0
#else
#undef FOREACH_I_PlayerInVehicle
#define FOREACH_I_PlayerInVehicle 1
#endif
#if defined FOREACH_MULTISCRIPT
#if FOREACH_I_Vehicle
forward Iter_AddVehicle(vehicleid);
forward Iter_RemoveVehicle(vehicleid);
#endif
#if FOREACH_I_Actor
forward Iter_AddActor(actorid);
forward Iter_RemoveActor(actorid);
#endif
#endif
/*
--------------------------------------------------------------------------------
Array:
IteratorArray
Notes:
Creates a new iterator array start/array pair.
--------------------------------------------------------------------------------
*/
#define IteratorArray:%1[%2]<%3> %1@YSII_Cg[%2],%1@YSII_Ag[%2][%3+1]
/*
--------------------------------------------------------------------------------
Array:
Iterator
Notes:
Creates a new iterator start/array pair.
--------------------------------------------------------------------------------
*/
#define Iterator:%1<%2> _Y_ITER_C3:%1@YSII_Cg,%1@YSII_Ag[(%2)+1]={(%2)*2,(%2)*2-1,...}
#define iterator%0<%1> new Iterator:%0<%1>
/*
--------------------------------------------------------------------------------
Function:
Iter_Init
Params:
iter - Name of the iterator array to initialise.
Return:
-
Notes:
Wrapper for Iter_InitInternal.
native Iter_Init(IteratorArray:Name[]<>);
--------------------------------------------------------------------------------
*/
#define Iter_Init(%1) \
Iter_InitInternal(%1@YSII_Ag,sizeof %1@YSII_Ag,sizeof %1@YSII_Ag[]-1)
/*
--------------------------------------------------------------------------------
Function:
Iter_Add
Params:
iter - Name of the iterator to add the data to.
value - Value to add to the iterator.
Return:
-
Notes:
Wrapper for Iter_AddInternal.
native Iter_Add(Iterator:Name<>, value);
--------------------------------------------------------------------------------
*/
#define Iter_Add(%1,%2) Iter_AddInternal(_Y_ITER_ARRAY:%1@YSII_Cg,_Y_ITER_ARRAY:%1@YSII_Ag,%2,_Y_ITER_ARRAY_SIZE(%1))
/*
--------------------------------------------------------------------------------
Function:
Iter_Free
Params:
iter - Name of the iterator to get the first free slot in.
Return:
-
Notes:
Wrapper for Iter_FreeInternal.
native Iter_Free(Iterator:Name<>);
--------------------------------------------------------------------------------
*/
#define Iter_Free(%1) Iter_FreeInternal(_Y_ITER_ARRAY:%1@YSII_Ag,_Y_ITER_ARRAY_SIZE(%1))
/*
--------------------------------------------------------------------------------
Function:
Iter_Remove
Params:
iter - Name of the iterator to remove data from.
value - Data to remove.
Return:
-
Notes:
Wrapper for Iter_RemoveInternal.
native Iter_Remove(Iterator:Name<>, value);
--------------------------------------------------------------------------------
*/
#define Iter_Remove(%1,%2) Iter_RemoveInternal(_Y_ITER_ARRAY:%1@YSII_Cg,_Y_ITER_ARRAY:%1@YSII_Ag,%2,_Y_ITER_ARRAY_SIZE(%1))
/*
--------------------------------------------------------------------------------
Function:
Iter_Contains
Params:
iter - Name of the iterator to check membership of.
value - Value to check.
Return:
-
Notes:
Checks if the given value is in the given iterator.
native Iter_Remove(Iterator:Name<>, value);
--------------------------------------------------------------------------------
*/
#define Iter_Contains(%1,%2) Iter_ContainsInternal(_Y_ITER_ARRAY:%1@YSII_Ag,%2,_Y_ITER_ARRAY_SIZE(%1))
/*
--------------------------------------------------------------------------------
Function:
Iter_SafeRemove
Params:
iter - Name of the iterator to remove data from.
value - Data to remove.
next - Container for the pointer to the next element.
Return:
-
Notes:
Wrapper for Iter_SafeRemoveInternal. Common use:
Iter_SafeRemove(iter, i, i);
native Iter_SafeRemove(Iterator:Name<>, value, &next);
--------------------------------------------------------------------------------
*/
#define Iter_SafeRemove(%1,%2,%3) Iter_SafeRemoveInternal(_Y_ITER_ARRAY:%1@YSII_Cg,_Y_ITER_ARRAY:%1@YSII_Ag,%2,%3,_Y_ITER_ARRAY_SIZE(%1))
/*
--------------------------------------------------------------------------------
Function:
Iter_Random
Params:
iter - Name of the iterator to get a random slot from.
Return:
-
Notes:
Wrapper for Iter_RandomInternal.
native Iter_Random(Iterator:Name<>);
--------------------------------------------------------------------------------
*/
#define Iter_Random(%1) Iter_RandomInternal(_Y_ITER_ARRAY:%1@YSII_Cg,_Y_ITER_ARRAY:%1@YSII_Ag,_Y_ITER_ARRAY_SIZE(%1))
/*
--------------------------------------------------------------------------------
Function:
Iter_Count
Params:
iter - Name of the iterator to get the number of items from.
Return:
-
Notes:
Returns the number of items in this iterator.
native Iter_Count(Iterator:Name<>);
--------------------------------------------------------------------------------
*/
#define Iter_Count(%1) (_Y_ITER_ARRAY:%1@YSII_Cg)
/*
--------------------------------------------------------------------------------
Function:
Iter_Clear
Params:
iter - Name of the iterator empty.
Return:
-
Notes:
Wrapper for Iter_ClearInternal.
native Iter_Clear(IteratorArray:Name[]<>);
--------------------------------------------------------------------------------
*/
#define Iter_Clear(%1) Iter_ClearInternal(_Y_ITER_ARRAY:%1@YSII_Cg,_Y_ITER_ARRAY:%1@YSII_Ag,_Y_ITER_ARRAY_SIZE(%1))
/*
--------------------------------------------------------------------------------
Function:
Iter_Index
Params:
iter - Name of the iterator empty.
value - Value of the iterator element.
Return:
-
Notes:
Wrapper for Iter_IndexInternal.
native Iter_Index(IteratorArray:Name[]<>, index);
--------------------------------------------------------------------------------
*/
#define Iter_Index(%1,%2) Iter_IndexInternal(_Y_ITER_ARRAY:%1@YSII_Cg,_Y_ITER_ARRAY:%1@YSII_Ag,_Y_ITER_ARRAY_SIZE(%1),_Y_ITER_ARRAY_SIZE(%1),%2)
/*
--------------------------------------------------------------------------------
Create the internal iterators.
--------------------------------------------------------------------------------
*/
#if FOREACH_I_Player
new Iterator:Player<MAX_PLAYERS>;
#endif
#if FOREACH_I_Bot
new Iterator:Bot<MAX_PLAYERS>;
#define NPC@YSII_Cg Bot@YSII_Cg
#define NPC@YSII_Ag Bot@YSII_Ag
#endif
#if FOREACH_I_Character
new Iterator:Character<MAX_PLAYERS>;
#endif
#if FOREACH_I_Vehicle
new Iterator:Vehicle<MAX_VEHICLES>;
#endif
#if FOREACH_I_Actor
new Iterator:Actor<MAX_ACTORS>;
#endif
#if FOREACH_I_PlayerPlayersStream
new Iterator:PlayerPlayersStream[MAX_PLAYERS]<MAX_PLAYERS>;
#endif
#if FOREACH_I_PlayerVehiclesStream
new Iterator:PlayerVehiclesStream[MAX_PLAYERS]<MAX_VEHICLES>;
#endif
#if FOREACH_I_VehiclePlayersStream
new Iterator:VehiclePlayersStream[MAX_VEHICLES]<MAX_PLAYERS>;
#endif
#if FOREACH_I_PlayerActorsStream
new Iterator:PlayerActorsStream[MAX_PLAYERS]<MAX_ACTORS>;
#endif
#if FOREACH_I_ActorPlayersStream
new Iterator:ActorPlayersStream[MAX_ACTORS]<MAX_PLAYERS>;
#endif
#if FOREACH_I_PlayerInVehicle
new Iterator:PlayerInVehicle[MAX_VEHICLES]<MAX_PLAYERS>;
static Iter_gPlayerVehicleId[MAX_PLAYERS] = {INVALID_VEHICLE_ID, ...};
#endif
/*
--------------------------------------------------------------------------------
Function:
foreach
Params:
data - Data to iterate through.
as - Variable to set value to.
Return:
-
Notes:
Not exactly the same as PHP foreach, just iterates through a list and
returns the value of the current slot but uses that slot as the next index
too. Variables must be in the form @YSII_<gname>S for the start index and
@YSII_<gname>A for the data array where <name> is what's entered in data.
--------------------------------------------------------------------------------
*/
#define foreach%1(%0) for(new Y_FOREACH_SECOND|||Y_FOREACH_THIRD|||%0|||)
// This allows us to use "new" multiple times - stripping off ONLY whole words.
#define new%0|||%9|||%1:%2||| %9|||%0|||%1|||%2|||
// This one is called if the new syntax is required, but the state of "new" is
// as-yet unknown. This attempts to call "%1" as a macro, if it starts with
// "new" as a whole word then it will (and will also helpfully strip off the
// "new" keyword for us).
#define Y_FOREACH_THIRD|||%0|||%1|||%2||| %1=Y_FOREACH_FIFTH|||Y_FOREACH_FOURTH|||%1:%2|||
// This is called if the "new" macro is called for a second time.
#define Y_FOREACH_FOURTH|||%0=Y_FOREACH_FIFTH|||%1|||%2||| new Y_FOREACH_SIXTH;%0|||Y_FOREACH_SEVENTH|||%2|||
// This is called when there are tags on the "new" declaration.
#define Y_FOREACH_SEVENTH|||%9Y_FOREACH_SIXTH;%0|||%1|||%2||| new %0:%1=%0:(_Y_ITER_ARRAY_SIZE(%2));_:(%1=%0:_Y_ITER_ARRAY:%2@YSII_Ag[_:%1])!=_Y_ITER_ARRAY_SIZE(%2);
// This is called when there aren't.
#define Y_FOREACH_SIXTH;%0|||Y_FOREACH_SEVENTH|||%2||| %0=_Y_ITER_ARRAY_SIZE(%2);_:(%0=_Y_ITER_ARRAY:%2@YSII_Ag[%0])!=_Y_ITER_ARRAY_SIZE(%2);
// This is called if "%1" didn't have "new" at the start.
#define Y_FOREACH_FIFTH|||Y_FOREACH_FOURTH|||%1:%2||| _Y_ITER_ARRAY_SIZE(%2);_:(%1=_Y_ITER_ARRAY:%2@YSII_Ag[%1])!=_Y_ITER_ARRAY_SIZE(%2);
// This is the old version, but DON'T add "new" because that already exists from
// the failed "new" macro call above.
#define Y_FOREACH_SECOND|||Y_FOREACH_THIRD|||%1,%2||| %2=_Y_ITER_ARRAY_SIZE(%1);_:(%2=_Y_ITER_ARRAY:%1@YSII_Ag[%2])!=_Y_ITER_ARRAY_SIZE(%1);
/*
--------------------------------------------------------------------------------
Function:
Iter_OnFilterScriptInit
Params:
-
Return:
-
Notes:
Fixes a bug where callbacks are not detected when "loadfs" is used after the
GM has already started. If this is a GM this is just never used called.
--------------------------------------------------------------------------------
*/
public OnFilterScriptInit()
{
Iter_gIsFilterscript = true;
Iter_ScriptInit();
#if defined Iter_OnFilterScriptInit
return Iter_OnFilterScriptInit();
#else
return 1;
#endif
}
#if defined _ALS_OnFilterScriptInit
#undef OnFilterScriptInit
#else
#define _ALS_OnFilterScriptInit
#endif
#define OnFilterScriptInit Iter_OnFilterScriptInit
#if defined Iter_OnFilterScriptInit
forward Iter_OnFilterScriptInit();
#endif
/*
--------------------------------------------------------------------------------
Function:
Iter_OnGameModeInit
Params:
-
Return:
-
Notes:
-
--------------------------------------------------------------------------------
*/
public OnGameModeInit()
{
if (!Iter_gIsFilterscript) {
Iter_ScriptInit();
}
#if defined Iter_OnGameModeInit
return Iter_OnGameModeInit();
#else
return 1;
#endif
}
#if defined _ALS_OnGameModeInit
#undef OnGameModeInit
#else
#define _ALS_OnGameModeInit
#endif
#define OnGameModeInit Iter_OnGameModeInit
#if defined Iter_OnGameModeInit
forward Iter_OnGameModeInit();
#endif
/*
--------------------------------------------------------------------------------
Function:
Iter_ScriptInit
Params:
-
Return:
-
Notes:
-
--------------------------------------------------------------------------------
*/
stock Iter_ScriptInit()
{
#if FOREACH_I_Player || FOREACH_I_Bot || FOREACH_I_Character
#if FOREACH_I_Player
Iter_Clear(Player);
#endif
#if FOREACH_I_Bot
Iter_Clear(Bot);
#endif
#if FOREACH_I_Character
Iter_Clear(Character);
#endif
for (new playerid = 0; playerid != MAX_PLAYERS; ++playerid) {
if (!IsPlayerConnected(playerid)) {
continue;
}
if (!IsPlayerNPC(playerid)) {
#if FOREACH_I_Player
Iter_Add(Player, playerid);
#endif
} else {
#if FOREACH_I_Bot
Iter_Add(Bot, playerid);
#endif
}
#if FOREACH_I_Character
Iter_Add(Character, playerid);
#endif
}
#endif
#if FOREACH_I_Vehicle
Iter_Clear(Vehicle);
for (new vehicleid = 1; vehicleid != MAX_VEHICLES; ++vehicleid) {
if (!GetVehicleModel(vehicleid)) {
continue;
}
Iter_Add(Vehicle, vehicleid);
}
#endif
#if FOREACH_I_Actor
Iter_Clear(Actor);
for (new actorid = 0; actorid != MAX_ACTORS; ++actorid) {
if (!IsValidActor(actorid)) {
continue;
}
Iter_Add(Actor, actorid);
}
#endif
#if FOREACH_I_PlayerPlayersStream
Iter_Init(PlayerPlayersStream);
for (new playerid = 0; playerid != MAX_PLAYERS; ++playerid) {
if (!IsPlayerConnected(playerid)) {
continue;
}
for (new targetid = 0; targetid != MAX_PLAYERS; ++targetid) {
if (!IsPlayerStreamedIn(playerid, targetid)) {
continue;
}
Iter_Add(PlayerPlayersStream[playerid], targetid);
}
}
#endif
#if FOREACH_I_PlayerVehiclesStream || FOREACH_I_VehiclePlayersStream
#if FOREACH_I_PlayerVehiclesStream
Iter_Init(PlayerVehiclesStream);
#endif
#if FOREACH_I_VehiclePlayersStream
Iter_Init(VehiclePlayersStream);
#endif
for (new playerid = 0; playerid != MAX_PLAYERS; ++playerid) {
if (!IsPlayerConnected(playerid)) {
continue;
}
for (new vehicleid = 1; vehicleid != MAX_VEHICLES; ++vehicleid) {
if (!IsVehicleStreamedIn(vehicleid, playerid)) {
continue;
}
#if FOREACH_I_PlayerVehiclesStream
Iter_Add(PlayerVehiclesStream[playerid], vehicleid);
#endif
#if FOREACH_I_VehiclePlayersStream
Iter_Add(VehiclePlayersStream[vehicleid], playerid);
#endif
}
}
#endif
#if FOREACH_I_PlayerActorsStream || FOREACH_I_ActorPlayersStream
#if FOREACH_I_PlayerActorsStream
Iter_Init(PlayerActorsStream);
#endif
#if FOREACH_I_ActorPlayersStream
Iter_Init(ActorPlayersStream);
#endif
for (new playerid = 0; playerid != MAX_PLAYERS; ++playerid) {
if (!IsPlayerConnected(playerid)) {
continue;
}
for (new actorid = 0; actorid != MAX_ACTORS; ++actorid) {
if (!IsActorStreamedIn(actorid, playerid)) {
continue;
}
#if FOREACH_I_PlayerActorsStream
Iter_Add(PlayerActorsStream[playerid], actorid);
#endif
#if FOREACH_I_ActorPlayersStream
Iter_Add(ActorPlayersStream[actorid], playerid);
#endif
}
}
#endif
#if FOREACH_I_PlayerInVehicle
Iter_Init(PlayerInVehicle);
for (new playerid = 0; playerid != MAX_PLAYERS; ++playerid) {
if (!IsPlayerConnected(playerid)) {
continue;
}
for (new vehicleid = 1; vehicleid != MAX_VEHICLES; ++vehicleid) {
if (!IsPlayerInVehicle(playerid, vehicleid)) {
continue;
}
Iter_Add(PlayerInVehicle[vehicleid], playerid);
}
}
#endif
}
/*
CHARACTERS
*/
#if FOREACH_I_Player || FOREACH_I_Bot || FOREACH_I_Character
/*
--------------------------------------------------------------------------------
Function:
Iter_OnPlayerConnect
Params:
playerid - Player who joined.
Return:
-
Notes:
Adds a player to the loop data. Now sorts the list too. Note that I found
the most bizzare bug ever (I *think* it may be a compiler but, but it
requires further investigation), basically it seems that multiple variables
were being treated as the same variable (namely @YSII_EgotS and
@YSII_CgharacterS were the same and @YSII_EgotC and @YSII_CgharacterC were the
same). Adding print statements which reference these variables seem to fix
the problem, and I have tried to make sure that the values will never actually
get printed.
--------------------------------------------------------------------------------
*/
public OnPlayerConnect(playerid)
{
if (!IsPlayerNPC(playerid)) {
#if FOREACH_I_Player
Iter_Add(Player, playerid);
#endif
} else {
#if FOREACH_I_Bot
Iter_Add(Bot, playerid);
#endif
}
#if FOREACH_I_Character
Iter_Add(Character, playerid);
#endif
#if defined Iter_OnPlayerConnect
return Iter_OnPlayerConnect(playerid);
#else
return 1;
#endif
}
#if defined _ALS_OnPlayerConnect
#undef OnPlayerConnect
#else
#define _ALS_OnPlayerConnect
#endif
#define OnPlayerConnect Iter_OnPlayerConnect
#if defined Iter_OnPlayerConnect
forward Iter_OnPlayerConnect(playerid);
#endif
/*
--------------------------------------------------------------------------------
Function:
Iter_OnPlayerDisconnect
Params:
playerid - Player who left.
Return:
-
Notes:
Removes a player from the loop data. No longer uses "hook" to ENSURE that
this is always last. Previously I think that the order of evaluation in
y_hooks meant that this got called before the user "OnPlayerDisconnect".
--------------------------------------------------------------------------------
*/
public OnPlayerDisconnect(playerid, reason)
{
#if defined Iter_OnPlayerDisconnect
Iter_OnPlayerDisconnect(playerid, reason);
#endif
SetTimerEx("Iter_OPDCInternal", 0, false, "i", playerid);
return 1;
}
#if defined _ALS_OnPlayerDisconnect
#undef OnPlayerDisconnect
#else
#define _ALS_OnPlayerDisconnect
#endif
#define OnPlayerDisconnect Iter_OnPlayerDisconnect
#if defined Iter_OnPlayerDisconnect
forward Iter_OnPlayerDisconnect(playerid, reason);
#endif
/*
--------------------------------------------------------------------------------
Function:
Iter_OPDCInternal
Params:
playerid - Player who left.
Return:
-
Notes:
Called AFTER "OnPlayerDisconnect" so that using "Kick" inside a "foreach"
loop does not crash the server due to an OOB error.
--------------------------------------------------------------------------------
*/
public Iter_OPDCInternal(playerid)
{
if (IsPlayerConnected(playerid)) {
return;
}
if (!IsPlayerNPC(playerid)) {
#if FOREACH_I_Player
Iter_Remove(Player, playerid);
#endif
} else {
#if FOREACH_I_Bot
Iter_Remove(Bot, playerid);
#endif
}
#if FOREACH_I_Character
Iter_Remove(Character, playerid);
#endif
#if FOREACH_I_PlayerInVehicle
if (Iter_gPlayerVehicleId[playerid] != INVALID_VEHICLE_ID) {
Iter_Remove(PlayerInVehicle[Iter_gPlayerVehicleId[playerid]], playerid);
Iter_gPlayerVehicleId[playerid] = INVALID_VEHICLE_ID;
}
#endif
#if FOREACH_I_PlayerPlayersStream
Iter_Clear(PlayerPlayersStream[playerid]);
#endif
#if FOREACH_I_PlayerVehiclesStream
Iter_Clear(PlayerVehiclesStream[playerid]);
#endif
#if FOREACH_I_PlayerActorsStream
Iter_Clear(PlayerActorsStream[playerid]);
#endif
}
#endif
/*
CHARACTERS ENDS
*/
/*