SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts

Reply
 
Thread Tools Display Modes
Old 24/07/2012, 01:42 PM   #3431
The King's Bastard
Little Clucker
 
The King's Bastard's Avatar
 
Join Date: Jan 2012
Posts: 32
Reputation: 29
Default Re: Useful Functions

Quote:
Originally Posted by FireCat View Post
OnPlayerMoneyChange v2
Faster, and better scripted.
pawn Code:
new gOldMoney[MAX_PLAYERS];
public OnPlayerUpdate(playerid)
{
    static iM;
    iM = GetPlayerMoney(playerid);
    if(iM != gOldMoney[playerid])
    {
        CallLocalFunction("OnPlayerMoneyChange","ii",playerid,(iM-gOldMoney[playerid]));
        gOldMoney[playerid] = iM;
    }
    return 1;
}

public OnPlayerMoneyChange(playerid, iAmount)
{
    printf("Someone's money changed by $%i",iAmount);
    return 1;
}
Why would that be faster?
You just changed the local money variable to a static local variable.
Do you think it might be faster because memory doesn't have to be allocated every function call?

---

Why not just write global variables in camel case (GlobalVariable) and local variables in lower case (local_variable or localvariable)?
The King's Bastard is offline   Reply With Quote
Old 24/07/2012, 01:50 PM   #3432
FireCat
Banned
 
Join Date: Jul 2010
Posts: 2,471
Reputation: 596
Default Re: Useful Functions

Quote:
Originally Posted by The King's Bastard View Post
Why would that be faster?
You just changed the local money variable to a static local variable.
Do you think it might be faster because memory doesn't have to be allocated every function call?

---

Why not just write global variables in camel case (GlobalVariable) and local variables in lower case (local_variable or localvariable)?
First of all, gOldMoney is a global variable, so we put a g.
Second of all, I didn't just "change" to static, I allocated the variable iM, to GetPlayerMoney() so we don't have to call that function every time, its not like LOTS faster...
But if you use it for big codes, it can affect the performance of the server

Edit:
Testing Code
Quote:
[15:04:31] Static and allocating the iM variable took 288 ms
[15:04:32] Static and not allocating the iM variable took 377 ms
FireCat is offline   Reply With Quote
Old 24/07/2012, 01:57 PM   #3433
Slice
High-roller
 
Slice's Avatar
 
Join Date: Mar 2008
Location: Sweden
Posts: 1,849
Reputation: 1229
Default Re: Useful Functions

You forgot to test just using "new". Or maybe you did, and realized there's hardly any difference.
__________________
Do you like SA-MP and DayZ? Come play both: ulclan.com:1421

Do you use any scripts I've made?
Star them on GitHub or reply in their topics so I know what I should keep working on!
Slice is offline   Reply With Quote
Old 24/07/2012, 02:00 PM   #3434
FireCat
Banned
 
Join Date: Jul 2010
Posts: 2,471
Reputation: 596
Default Re: Useful Functions

Quote:
Originally Posted by Slice View Post
You forgot to test just using "new". Or maybe you did, and realized there's hardly any difference.
Not even worth it
FireCat is offline   Reply With Quote
Old 24/07/2012, 02:10 PM   #3435
Vince
Godfather
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 6,507
Reputation: 1694
Default Re: Useful Functions

So, what's the point of calling OnPlayerMoneyChange through CallLocalFunction? Naturally, it'd be faster if it was called directly.
__________________
I will rarely post a ready made solution in your thread, but rather I try to make you realize your own mistakes so you can fix them yourself.
The average intellect of the average SA-MP forums user.
Vince is offline   Reply With Quote
Old 24/07/2012, 02:12 PM   #3436
Y_Less
Beta Tester
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 17,554
Reputation: 2438
Default Re: Useful Functions

Yes, but if the user doesn't have that callback the code won't even compile. By not using CallLocalFunction you FORCE the user to include that callback even if they don't want it.
Y_Less is online now   Reply With Quote
Old 24/07/2012, 02:19 PM   #3437
Slice
High-roller
 
Slice's Avatar
 
Join Date: Mar 2008
Location: Sweden
Posts: 1,849
Reputation: 1229
Default Re: Useful Functions

Non-recursive quicksort - sorts an array of plain integers.

pawn Code:
stock QuickSort(numbers[], size = sizeof(numbers)) {
    #define PUSH(%1,%2)      (stack[top][0] = (%1), stack[top][1] = (%2), top++)
    #define POP(%1,%2)       (--top, (%1 = stack[top][0]), (%2 = stack[top][1]))
   
    static stack[256][2];
    new top = 0;
   
    if (size <= 1)
        return;
   
    new left, right, pivot, l_hold, r_hold;
   
    PUSH(0, size - 1);
   
    while (0 < top) {
        POP(left, right);
       
        pivot = numbers[left];
        l_hold = left;
        r_hold = right;

        // insertion sort for small fragments
        if (right - left <= 6) {
            insertion_sort(numbers, left, right);

            continue;
        }

        while (left < right) {
            while (numbers[right] >= pivot && left < right)
                right--;

            if (left != right) {
                numbers[left] = numbers[right];
                left++;
            }

            while (numbers[left] <= pivot && left < right)
                left++;

            if (left != right) {
                numbers[right] = numbers[left];
                right--;
            }
        }

        numbers[left] = pivot;
        pivot = left;
        left = l_hold;
        right = r_hold;
       
        if (left < pivot)  PUSH(left, pivot - 1);
        if (right > pivot) PUSH(pivot + 1, right);
    }
   
    #undef PUSH
    #undef POP
}

stock insertion_sort(array[], left, right) {
    for (new j = left + 1; j <= right; j++) {
        new key = array[j], i = j - 1;
       
        while (i >= left && array[i] > key) {
            array[i + 1] = array[i];
           
            i--;
        }
       
        array[i + 1] = key;
    }
}
__________________
Do you like SA-MP and DayZ? Come play both: ulclan.com:1421

Do you use any scripts I've made?
Star them on GitHub or reply in their topics so I know what I should keep working on!
Slice is offline   Reply With Quote
Old 24/07/2012, 02:40 PM   #3438
Y_Less
Beta Tester
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 17,554
Reputation: 2438
Default Re: Useful Functions

Now where have I seen that code before...

I'm slightly surprised you didn't keep the GPL notice from the GNU original. I've debated porting that function before but can't stand GPL!
Y_Less is online now   Reply With Quote
Old 24/07/2012, 02:40 PM   #3439
FireCat
Banned
 
Join Date: Jul 2010
Posts: 2,471
Reputation: 596
Default Re: Useful Functions

IsPlayerInArea and IsPlayerInArea3D

pawn Code:
public IsPlayerInArea(playerid,Float:fXMax,Float:fYMax,Float:fXMin,Float:fYMin) //By: Firecat
{
    new
        Float:fX,Float:fY,Float:fZ;
       
    GetPlayerPos(playerid,fX,fY,fZ);
    if(((fX >= fXMin) && (fX <= fXMax)) && ((fY >= fYMin) &&(fY <= fYMax)))
        return 1;
    return 0;
}


public IsPlayerInArea3D(playerid,Float:fXMax,Float:fYMax,Float:fZMax,Float:fXMin,Float:fYMin,Float:fZMin) //By: Firecat
{
    static
        Float:fX,Float:fY,Float:fZ;

    GetPlayerPos(playerid,fX,fY,fZ);
    if(((fX >= fXMin) && (fX <= fXMax)) && ((fY >= fYMin) && (fY <= fYMax)) && ((fZ >= fZMin) && (fZ <= fZMax)))
        return 1;
    return 0;
}
FireCat is offline   Reply With Quote
Old 24/07/2012, 03:50 PM   #3440
Slice
High-roller
 
Slice's Avatar
 
Join Date: Mar 2008
Location: Sweden
Posts: 1,849
Reputation: 1229
Default Re: Useful Functions

Quote:
Originally Posted by Y_Less View Post
Now where have I seen that code before...

I'm slightly surprised you didn't keep the GPL notice from the GNU original. I've debated porting that function before but can't stand GPL!
Sorry, I was unsure exactly where it originally came from as it was inside an old scribble file.
The original one is actually made by you (from YSI_Misc.own), which is adapted from some WKU page (dead link).
After that I looked through a number of different implementations.
__________________
Do you like SA-MP and DayZ? Come play both: ulclan.com:1421

Do you use any scripts I've made?
Star them on GitHub or reply in their topics so I know what I should keep working on!
Slice is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Functions? sciman001 Help Archive 7 19/03/2011 03:03 PM
How to get functions? Twain32 Help Archive 4 08/10/2010 07:37 PM
[Include] [INC] SA:MP New Functions 0.4a DKN ipsBruno Lançamentos/Releases 22 17/06/2010 08:38 AM
[Include] [INC] LSF - Lorenc's Simple Functions (w/ gang/clan functions) Lorenc_ Includes 11 03/05/2010 09:47 PM
What this functions do? harrold Help Archive 2 08/05/2009 11:30 PM


All times are GMT. The time now is 05:46 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.