stock ShuffleArray(array[], start = 0, end = sizeof(array)) {
new
rand_Value,
i = start,
arr_Index = start,
temp_Var
;
while(i < end) {
rand_Value = random(end - start) + start;
temp_Var = array[arr_Index];
array[arr_Index] = array[rand_Value];
array[rand_Value] = temp_Var;
arr_Index = rand_Value;
i++;
}
return 1;
}
public OnFilterScriptInit() {
new
my_Array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
tempStr[25],
shortStr[5]
;
print("Before shuffling, my_Array = ");
for(new i = 0; i< 10; i++) {
valstr(shortStr, my_Array[i], false);
strcat(tempStr, shortStr, sizeof(tempStr));
strcat(tempStr, " ", sizeof(tempStr));
}
print(tempStr);
tempStr[0] = '\0';
print("After shuffling from 0 -> 10 (start = 0 | end = 10)");
ShuffleArray(my_Array); //default arguments start = 0, end = sizeof(array)
for(new i = 0; i< 10; i++) {
valstr(shortStr, my_Array[i], false);
strcat(tempStr, shortStr, sizeof(tempStr));
strcat(tempStr, " ", sizeof(tempStr));
}
print(tempStr);
tempStr[0] = '\0';
print("Default array again : ");
my_Array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for(new i = 0; i< 10; i++) {
valstr(shortStr, my_Array[i], false);
strcat(tempStr, shortStr, sizeof(tempStr));
strcat(tempStr, " ", sizeof(tempStr));
}
print(tempStr);
tempStr[0] = '\0';
print("After shuffling from 5 -> 10 (start = 5 | end = 10)");
ShuffleArray(my_Array, 5, sizeof(my_Array)); //sizeof(my_Array) here is 10.
for(new i = 0; i< 10; i++) {
valstr(shortStr, my_Array[i], false);
strcat(tempStr, shortStr, sizeof(tempStr));
strcat(tempStr, " ", sizeof(tempStr));
}
print(tempStr);
return 1;
}
Before shuffling, my_Array = 1 2 3 4 5 6 7 8 9 10 After shuffling from 0 -> 10 (start = 0 | end = 10) 2 10 3 1 5 8 6 9 7 4 Default array again : 1 2 3 4 5 6 7 8 9 10 After shuffling from 5 -> 10 (start = 5 | end = 10) 1 2 3 4 5 7 6 10 8 9
new MyArray[MAX_MESSAGES][7][200] =
{
{"hey", "Hello, how are you."},
{"hi", "Hello , how are you."}
};
bool:IsSequence( main_string[ ] , sub_string[ ] , first_size = sizeof( main_string ) , second_size = sizeof( sub_string ) )// by Sreyas
{
if( !second_size ) return true;
if( !first_size ) return false;
if( main_string[ first_size - 1 ] == sub_string[ second_size - 1 ] )
return IsSequence( main_string , sub_string , first_size - 1 , second_size - 1 );
return IsSequence( main_string , sub_string , first_size - 1 , second_size );
}
"abc" - placed at 33
"tat" - placed at 24
// code
new array[50][64];
printf("abc = %i", getIndex(array, "abc"));
printf("tat = %i", getIndex(array, "tat"));
stock getIndex(array[][], const value[], size = sizeof array)
{
new index;
for (new i = 0, j = strlen(value); i < j; i++)
{
index += value[i] + index;
}
index %= size;
return index;
}
getIndex
This function locates indexes using simple hash algorithm instead of looping through an entire array. This is good way to locate indexes quickly(faster than looping and strcmp) but indexes can be located anywhere from 0 to size. Say if i want to put "abc" and "tat" in my array of size 50. PHP код:
PHP код:
|
Really nice and useful function but it is failing on multiple possibilities and on invalid checking (if the result is not in the array).
|
new idx = getIndex(array, "abc");
if (!array[idx][0])
{
// its empty
}
PHP код:
|
PHP код:
|
new arrays[][]= {"abc","fdg","******","******"};
main()
{
printf("Index = %d",getIndex(arrays,"samp"));//prints 1
printf("Index = %d",getIndex(arrays,"******"));//prints 1
printf("Index = %d",getIndex(arrays,"fdg"));//prints 3
}
main()
{
new arrays1[][]= {"abc","******","loop","fdg"};
new arrays2[][]= {"abc","fdg","******","******"};
printf("Index = %d",getIndex(arrays1,"fdg"));//prints 3
printf("Index = %d",getIndex(arrays2,"fdg"));//prints 3
}
getIndex
This function locates indexes using simple hash algorithm instead of looping through an entire array. This is good way to locate indexes quickly(faster than looping and strcmp) but indexes can be located anywhere from 0 to size. Say if i want to put "abc" and "tat" in my array of size 50. PHP код:
PHP код:
|
stock getIndex(array[][], const string[], size = sizeof array)
{
new hash;
new i;
new c;
while ((c = string[i++]) != EOS)
{
hash = c + (hash << 6) + (hash << 16) - hash;
}
hash %= size;
if (array[hash][0] && strcmp(array[hash], string))
{
return -1;
}
return hash;
}
if( RandomPercentage(25) ) SCM(playerid, -1, "You won!");
stock RandomPercentage(percentage)
{
if(percentage >= random(101) ) return true;
else return false;
}
stock SelectRandomPlayer()
{
new k = GetPlayerPoolSize(),i,j,T[k];
for(i=0; i<k; i++){
if(IsPlayerConnected(i)){
T[j] = i;
j++;
}
}
return T[random(j)];
}
Select Random Player
PHP Code:
|
new k = GetPlayerPoolSize(),T[k];
new randomplayer = random(GetPlayerPoolSize);
while(!IsPlayerConnected(randomplayer))
{
randomplayer = random(GetPlayerPoolSize);
}
Iter_Random(Player);
I just do this
PHP Code:
|
enum EnumAdminCommands {command[45],information[80]};
new const AdminCommands[][EnumAdminCommands] = //{"command","information"}
{
{"/Admincmd","List of commands for admin"},
{"/Example","This command does not exist!"},
{"/Example2","..."}
};
CMD:admincmd(playerid) //using include izcmd
{
if(!IsPlayerAdmin(playerid)) //change
return 0;
new ss[50],s[500];
for(new i; i < sizeof(AdminCommands); i++)
{
format(ss, 50, "%s\n", AdminCommands[i][command]);
strcat(s, ss);
}
ShowPlayerDialog(playerid, 888, DIALOG_STYLE_LIST, "Admin Commands", s, "more", "close");
return 1;
}
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[ ])
{
if(dialogid == 888)
{
if(!response)
return 0;
new s[80];
for(new i; i < sizeof(AdminCommands); i++)
if(listitem == i) format(s, 80, "%s", AdminCommands[i][information]);
return ShowPlayerDialog(playerid, 889, DIALOG_STYLE_MSGBOX, "Admin Commands", s,"close","");
}
return 0;
}