SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 22/06/2012, 08:15 AM   #3281
Lorenc_
High-roller
 
Lorenc_'s Avatar
 
Join Date: Jan 2010
Location: Australia
Posts: 4,116
Reputation: 904
Default Re: Useful Functions

Revamped the version someone posted about ConvertPrice itself, it now doesn't use valstr and supports negative numbers.

pawn Code:
stock ConvertPrice( price, no_price_tag = 0 )
{
    new
        szString[ 32 ],
        iNumbers = 0,
        bool: isneg = price < 0 ? true : false
    ;
    if( isneg ) price = price * -1;
   
    iNumbers = floatround( floatlog( price ), floatround_floor ) + 1;
    format( szString, sizeof( szString ), "%d", price );
    while( iNumbers >= 4 ) {
        iNumbers -= 3;
        strins( szString, ",", iNumbers );
    }
    if( !no_price_tag ) strins( szString, "$", 0 );
    if( isneg ) strins( szString, "-", 0 );
    return szString;
}

Warning: Can crash if using a integer over the 32 bit integer limit..

I fixed some little glitch with the logarithm... Should work fine now!
__________________

192.169.82.202:7777 - An awesome CnR server consisting of doozy features! Come, we have players!


Lorenc_ is offline   Reply With Quote
Old 22/06/2012, 12:15 PM   #3282
RyDeR`
High-roller
 
RyDeR`'s Avatar
 
Join Date: Feb 2009
Location: Belgium
Posts: 3,133
Reputation: 640
Default Re: Useful Functions

I remember posting this function almost two years ago, just if you're interested:
pawn Code:
stock convertNumber(iValue, iDelim[2] = ".", szNum[15] = "", iSize = sizeof(szNum)) {
    format(szNum, iSize, "%d", iValue < 0 ? -iValue : iValue);
   
    for(new i = strlen(szNum) - 3; i > 0; i -= 3) {
        strins(szNum, iDelim, i, iSize);
    }
    if(iValue < 0) {
        strins(szNum, "-", 0, iSize);
    }
    return szNum;
}

pawn Code:
printf("%s", convertNumber(1246645455, .iDelim = "_")); // 1_246_645_455
printf("%s", convertNumber(-145455, .iDelim = ".")); // -145.455
printf("%s", convertNumber(-94411254, .iDelim = ",")); // -94,411,254
__________________

Sup?
RyDeR` is offline   Reply With Quote
Old 22/06/2012, 12:30 PM   #3283
Lorenc_
High-roller
 
Lorenc_'s Avatar
 
Join Date: Jan 2010
Location: Australia
Posts: 4,116
Reputation: 904
Default Re: Useful Functions

Yeah, I noticed I could of optimized a few things but I'll be sure to use yours as it might be faster (from the looks of it, mines larger in code).
__________________

192.169.82.202:7777 - An awesome CnR server consisting of doozy features! Come, we have players!


Lorenc_ is offline   Reply With Quote
Old 22/06/2012, 12:40 PM   #3284
RyDeR`
High-roller
 
RyDeR`'s Avatar
 
Join Date: Feb 2009
Location: Belgium
Posts: 3,133
Reputation: 640
Default Re: Useful Functions

Yeah, for example the logarithm part to calculate the length is pretty unnecessary - just use strlen as you have already converted the number into a string.
__________________

Sup?
RyDeR` is offline   Reply With Quote
Old 24/06/2012, 05:24 PM   #3285
Nero_3D
High-roller
 
Nero_3D's Avatar
 
Join Date: Jun 2007
Location: Germany
Posts: 3,339
Reputation: 344
Default AW: Useful Functions

I have been working on a wait function lately

One version with an global stack which and another one with SetTimerEx only

I just want that some people test it out since there were some problems with the second version

Pastebin

Usage

pawn Code:
// Usage:  wait(time)
wait(5000);
__________________


Nero_3D is online now   Reply With Quote
Old 24/06/2012, 07:06 PM   #3286
Y_Less
Beta Tester
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 18,052
Reputation: 2510
Default Re: Useful Functions

Any heap usage is going to seriously mess that up, and detecting its usage will be very hard given that you need to store the entire stack*. What gets returned if you, for example, use this in "OnPlayerCommandText"?

* Actually thinking about it it might not.

Edit 2: Just had a closer read through of your code, you can't do it like that. You seem to be (correct me if I'm wrong) passing the start address of the stack as an array pointer to SetTimerEx. Ignoring for the moment the fact that arrays don't work in SetTimerEx, even if they did when your "WakeEx" function gets called the stack will be reset and reused, overwriting all the things you were trying to save initially. Check y_inline which does something similar. You need to manually store the stack somewhere else or it will get changed by function calls between your wait and call, then in "WakeEx" put everything back on the stack (and probably the heap).

Or just use "sleep".

Edit 3: Just realised something else I didn't think of originally - how well does this work with "CallRemoteFunction"?
Y_Less is offline   Reply With Quote
Old 24/06/2012, 08:10 PM   #3287
Ricop522
High-roller
 
Ricop522's Avatar
 
Join Date: Feb 2010
Location: ES
Posts: 2,697
Reputation: 148
Default Re: Useful Functions

I wanna know if
pawn Code:
#define varGet(%0)              getproperty(0,%0)
#define varSet(%0,%1)           setproperty(0, %0, %1)


#define new_strcmp(%0,%1) \
                (varSet(%0, 1), varGet(%1) == varSet(%0, 0)) // by DraKiNs
is better than SA-MP strcmp?
__________________
"Any fool can write code that a
computer can understand. Good
programmers write code that
humans can understand."

Sistemas de Informação, IFES
Ricop522 is offline   Reply With Quote
Old 24/06/2012, 08:16 PM   #3288
[HLF]Southclaw
High-roller
 
[HLF]Southclaw's Avatar
 
Join Date: Apr 2009
Location: England
Posts: 4,465
Reputation: 993
Default Re: Useful Functions

Tempting to make a joke about DeaKiNs and his brilliant coding skills...

Regardless, I wouldn't think so, strcmp is a native so I would imagine it's better.
But I'm not really sure what the get/setproperty functions do anyway...
[HLF]Southclaw is offline   Reply With Quote
Old 24/06/2012, 08:17 PM   #3289
Slice
High-roller
 
Slice's Avatar
 
Join Date: Mar 2008
Location: Sweden
Posts: 1,858
Reputation: 1234
Default Re: Useful Functions

Quote:
Originally Posted by Ricop522 View Post
I wanna know if
pawn Code:
#define varGet(%0)              getproperty(0,%0)
#define varSet(%0,%1)           setproperty(0, %0, %1)


#define new_strcmp(%0,%1) \
                (varSet(%0, 1), varGet(%1) == varSet(%0, 0)) // by DraKiNs
is better than SA-MP strcmp?
__________________
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/06/2012, 08:40 PM   #3290
[HLF]Southclaw
High-roller
 
[HLF]Southclaw's Avatar
 
Join Date: Apr 2009
Location: England
Posts: 4,465
Reputation: 993
Default Re: Useful Functions

Okay that's a far more descriptive reply!

I just tested it and wow that's slightly slower than strcmp... Like 10 times slower...

[HLF]Southclaw 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 06:21 PM.


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