Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //
- // Foreach V2 (fast loop ir arrays pawn)
- // Author: Bruno da Silva (thanks SlashPT)
- // www.ips-team.blogspot.com
- //
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- #define Iterator:%0<%1> ipsArray%0[%1] = {-1, ...}, ipsInit%0 = -1, ipsFinal%0 = 0, ipsItens%0 = 0
- #define IteratorArray:%0[%1]<%2> ipsArray%0[%1][%2], ipsInit%0[%1] = {-1, ...}, ipsFinal%0[%1], ipsItens%0[%1]; functionArray_Init(ipsArray%0, %1, %2)
- #define Iter_Add(%0,%1) functionChangeIter(ipsArray%0, %1, ipsInit%0, ipsFinal%0, ipsItens%0)
- #define Iter_Remove(%0,%1) functionDeleteIter(ipsArray%0, %1, ipsInit%0, ipsFinal%0, ipsItens%0)
- #define Iter_Count(%0) _:ipsItens%0
- #define Iter_Random(%0) functionRandomIterArray(ipsInit%0, ipsFinal%0, ipsArray%0)
- #define Iter_Get(%0, %1) ipsArray%0[%1]
- #define foreach(%0,%1) for(new %1 = ipsInit%0 ; %1 != -1; %1 = ipsArray%0[%1])
- #define foreachex(%0,%1) for(%1 = ipsInit%0 ; %1 != -1; %1 = ipsArray%0[%1])
- new Iterator:Player<MAX_PLAYERS>;
- functionArray_Init(array[][], max1, max2)
- {
- for (new i; max1 != i; ++i) for (new j = 0; max2 != j; ++j)
- {
- array[i][j] = -1;
- }
- return true;
- }
- functionRandomIterArray(iniciar, finalizar,array[])
- {
- if(iniciar == -1) return -1; // não deixar travar servidor
- new retornarValor = -1;
- inicializarBusca:
- retornarValor = array[random(finalizar)];
- if(retornarValor == -1) goto inicializarBusca;
- return retornarValor;
- }
- functionChangeIter(array[], valor, &inicio, &ultimoNumeroVar, &count)
- {
- if(valor == -1 || array[valor] != -1)
- {
- return false;
- }
- ++count;
- if(inicio == -1)
- {
- return inicio = valor, ultimoNumeroVar = valor;
- }
- if(valor < inicio)
- {
- return array[valor] = inicio, inicio = valor, true;
- }
- if(array[inicio] == -1)
- {
- return array[inicio] = valor, ultimoNumeroVar = valor;
- }
- return array[ultimoNumeroVar] = valor, ultimoNumeroVar = valor, true;
- }
- functionDeleteIter(array[], value, &inicio, &ultimoNumeroVar, &count)
- {
- if(inicio == -1 || value == -1)
- {
- return false;
- }
- --count;
- if(inicio == value)
- {
- return inicio = -1, array[value] = -1, true;
- }
- ultimoNumeroVar = 0;
- for( ; array[ultimoNumeroVar] != value; ultimoNumeroVar = array[ultimoNumeroVar])
- {
- if(ultimoNumeroVar == -1) return false;
- }
- return array[ultimoNumeroVar] = array[value], array[value] = -1, true;
- }
- public OnPlayerConnect(playerid)
- {
- Iter_Add(Player, playerid);
- return CallLocalFunction("CallOnPlayerConnect", "i", playerid);
- }
- public OnPlayerDisconnect(playerid, reason)
- {
- Iter_Remove(Player, playerid);
- return CallLocalFunction("CallOnPlayerDisconnect", "ii", playerid, reason);
- }
- #if defined HookOnPlayerConnect
- #undef OnPlayerConnect
- #else
- #define HookOnPlayerConnect
- #endif
- #define OnPlayerConnect CallOnPlayerConnect
- #if defined HookOnPlayerDisconnect
- #undef OnPlayerDisconnect
- #else
- #define HookOnPlayerDisconnect
- #endif
- #define OnPlayerDisconnect CallOnPlayerDisconnect
- forward OnPlayerConnect(playerid);
- forward OnPlayerDisconnect(playerid, reason);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement