Useful Functions

ShuffleArray - Function shuffles the items in the given array.

Parameters:
array[] - The array to be shuffled.
start = 0 - The starting point of the array index to start shuffling. (optional)
end = sizeof(array) - The ending point of the array index to be shuffled. (optional)

pawn Код:
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;
}
Example:
pawn Код:
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;
}
Output:
Код:
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
I believe this function can be even more randomized to make it better, I'll work on it when I got some free time and mostly some interest.
Reply

How can i shuffle for example this:

PHP код:
new MyArray[MAX_MESSAGES][7][200] =
{
    {
"hey""Hello, how are you."},
    {
"hi""Hello , how are you."}
}; 
to make the outcome shuffle the messages, for example move first element in second cell (currently i access first element with MyArray[0][...]..., what i wanted to do is, shuffle the messages and change their pos)
Reply

IsSequence


Gist Link


This function checks given string is a sequence of another string.That means a string can be extracted from another string by removing the words in order.For example string "samp" is a sequence of string "san andreas multiplayer".This function uses standard recursion for traversing.

Parameters
  • main_string - string need to be checked in
  • sub_string - sub string which is sequence of main_string
  • first_size - size of main string (optional)
  • second_size - size of sub string (optional)
PHP код:
bool:IsSequencemain_string[ ] , sub_string[ ] , first_size sizeofmain_string ) , second_size sizeofsub_string ) )// by Sreyas
{
    if( !
second_size ) return true;
    if( !
first_size ) return false;
    if( 
main_stringfirst_size ]  == sub_stringsecond_size ] )
        return 
IsSequencemain_string sub_string first_size second_size );
    return 
IsSequencemain_string sub_string first_size second_size );

Reply

Wrong.
Reply

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 код:
"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")); 
Source
PHP код:
stock getIndex(array[][], const value[], size sizeof array)
{
    new 
index;
    for (new 
0strlen(value); ji++)
    {
        
index += value[i] + index;
    }
    
index %= size;
    return 
index;

Reply

Quote:
Originally Posted by Gammix
Посмотреть сообщение
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 код:
"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")); 
Source
PHP код:
stock getIndex(array[][], const value[], size sizeof array)
{
    new 
index;
    for (new 
0strlen(value); ji++)
    {
        
index += value[i] + index;
    }
    
index %= size;
    return 
index;

Really nice and useful function but it is failing on multiple possibilities and on invalid checking (if the result is not in the array).
Reply

Quote:
Originally Posted by SyS
Посмотреть сообщение
Really nice and useful function but it is failing on multiple possibilities and on invalid checking (if the result is not in the array).
PHP код:
new idx getIndex(array, "abc");
if (!array[
idx][0])
{
    
// its empty

Reply

Quote:
Originally Posted by Gammix
Посмотреть сообщение
PHP код:
new idx getIndex(array, "abc");
if (!array[
idx][0])
{
    
// its empty

The main issue I see is that your hashing is to simple resulting in collisions ("abc" has the same result as "bca")
Even more important, there is no fallback code if a collision happend

Hash algorithm you could use: bernstein (djb2), sdbm, adler32 or any other fast algorithm with halfway acceptable results

If you need a reference y_stringhash contains an implentation for bernstein, fnv1 and fnv1a

Hash Functions: http://www.cse.yorku.ca/~oz/hash.html
Simple Hash table: http://www.algolist.net/Data_structu...Simple_example
Reply

Quote:
Originally Posted by Gammix
Посмотреть сообщение
PHP код:
new idx getIndex(array, "abc");
if (!array[
idx][0])
{
    
// its empty

i think you don't understand what i said. I didn't meant searching in array having no elements. Your function returns some index if there is no match in array and if there is multiple possible matches.

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 

Edit : i saw it also fails with other arrays too.
PHP код:

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 

Reply

Quote:
Originally Posted by Gammix
Посмотреть сообщение
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 код:
"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")); 
Source
PHP код:
stock getIndex(array[][], const value[], size sizeof array)
{
    new 
index;
    for (new 
0strlen(value); ji++)
    {
        
index += value[i] + index;
    }
    
index %= size;
    return 
index;

pretty sure that you didn't run much tests on that code. It will not work as you expecting to.Might give correct result on small strings. Just view the assembly of that code and you will understand why.
Reply

My bad, i didn't do significant amount of tests. I'll post a new one with taking care of all the possibilities.
Reply

Here is the modified getIndex function, using sdbm algorithm.

PHP Code:
stock getIndex(array[][], const string[], size sizeof array)
{
    new 
hash;
    new 
i;
    new 
c;
    while ((
string[i++]) != EOS)
    {
        
hash + (hash << 6) + (hash << 16) - hash;
    }
    
hash %= size;
    if (array[
hash][0] && strcmp(array[hash], string))
    {
        return -
1;
    }
    return 
hash;

(since i am using this for my libraries now, so i don't mind posting them here!)
Reply

PHP Code:
if( RandomPercentage(25) ) SCM(playerid, -1"You won!"); 
PHP Code:
stock RandomPercentage(percentage)
{
    if(
percentage >= random(101) ) return true;
    else return 
false;

Reply

Select Random Player
PHP Code:
stock SelectRandomPlayer()
{
    new 
GetPlayerPoolSize(),i,j,T[k];
    for(
i=0i<ki++){
        if(
IsPlayerConnected(i)){
            
T[j] = i;
            
j++;
        }
    }
    return 
T[random(j)];

Reply

Quote:
Originally Posted by Eoussama
View Post
Select Random Player
PHP Code:
stock SelectRandomPlayer()
{
    new 
GetPlayerPoolSize(),i,j,T[k];
    for(
i=0i<ki++){
        if(
IsPlayerConnected(i)){
            
T[j] = i;
            
j++;
        }
    }
    return 
T[random(j)];

Your code won't work and is not efficient.Because:
PHP Code:
new GetPlayerPoolSize(),T[k]; 
the above code isn't right.The variable k has unknown value and not constant at the time of compilation and pawn doesn't support dynamic initialization .So providing size k to array T will throw error.

Now in your algorithm you are creating an array for nothing all we need is a random player id. What you now doing is creating an array of connected players and randomizing the index of that array and returning the value in that index.Which is clearly a bad method.

You can either use foreach and use it's own builtin Iter_Random function to get random players from the iterator Player or if you want to do without foreach either modify this function to supporting a cache like system.That means change the content of the array (static or global one) only when a change in number of connected player or their id occurred and return the random id.So that the same process of creating an array is not required in each overhead.
Reply

I just do this
PHP Code:
new randomplayer random(GetPlayerPoolSize);
while(!
IsPlayerConnected(randomplayer))
{
    
randomplayer random(GetPlayerPoolSize);

Reply

PHP код:
Iter_Random(Player); 
Reply

Quote:
Originally Posted by coool
View Post
I just do this
PHP Code:
new randomplayer random(GetPlayerPoolSize);
while(!
IsPlayerConnected(randomplayer))
{
    
randomplayer random(GetPlayerPoolSize);

That's a terrible method and incredibly inefficient. GetPlayerPoolSize gets the highest connected player ID, meaning if you have 1 player in your server, and they have player ID 100, it could take thousands of iterations before they're even selected. Not to mention your code is wrong, so that player would never be selected. The 'random' function returns a value between 0 and input minus 1, so the highest player ID is unachievable.
Reply

Quote:
Originally Posted by Threshold
View Post
...
Thanks, for telling it.
Reply

List of admin commands

Log in rcon
( /rcon login [password] )
  • Pictures



  • Script
PHP код:
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 
isizeof(AdminCommands); i++)
    {
        
format(ss50"%s\n"AdminCommands[i][command]);
        
strcat(sss);
    }
    
ShowPlayerDialog(playerid888DIALOG_STYLE_LIST"Admin Commands"s"more""close");
    return 
1;
}
public 
OnDialogResponse(playeriddialogidresponselistiteminputtext[ ])
{
    if(
dialogid == 888)
    {
        if(!
response)
            return 
0;
        
        new 
s[80];
        for(new 
isizeof(AdminCommands); i++)
        if(
listitem == iformat(s80"%s"AdminCommands[i][information]);
        
        return 
ShowPlayerDialog(playerid889DIALOG_STYLE_MSGBOX"Admin Commands"s,"close","");
    }
    return 
0;

I was going to throw it in the trash, but it's a job.
Sorry for my english and if I posted on the wrong topic.
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)