SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 15/08/2016, 06:22 PM   #3901
Shinja
Gangsta
 
Shinja's Avatar
 
Join Date: Jan 2016
Posts: 964
Reputation: 101
Default Re: Useful Functions

PHP Code:
SecondToTime(seconds

    new 
string[40], minuteshoursnewseconds seconds
    while(
newseconds>=60){newseconds-=60,minutes++;} 
    while(
minutes>=60){minutes-=60,hours++;} 
    
format(stringsizeof(string), "%02dHour%s : %02dMinute%s : %02dSecond%s"hours, (hours 1)?("s"):(""), minutes, (minutes>1)?("s"):(""), newseconds, (newseconds>1)?("s"):("")); 
    
//debug printf("[converter] %d seconds are %02d hours and %02d minutes and %02d seconds", seconds, hours, minutes, newseconds); 
    
return string

Shinja is offline   Reply With Quote
Old 23/08/2016, 05:18 PM   #3902
Gammix
High-roller
 
Gammix's Avatar
 
Join Date: Jan 2015
Location: Canada
Posts: 1,772
Reputation: 854
Default Re: Useful Functions

ReturnTimelapse
Returns a string. String contains the time elapsed between the start and till timestamps.

pawn Code:
stock ReturnTimelapse(start, till)
{
    new seconds = till - start;
   
    const MINUTE = 60;
    const HOUR = 60 * MINUTE;
    const DAY = 24 * HOUR;
    const MONTH = 30 * DAY;

    new time[32];
    if (seconds == 1)
        format(time, sizeof (time), "A seconds ago");
    if (seconds < (1 * MINUTE))
        format(time, sizeof (time), "%i seconds ago", seconds);
    else if (seconds < (2 * MINUTE))
        format(time, sizeof (time), "A minute ago");
    else if (seconds < (45 * MINUTE))
        format(time, sizeof (time), "%i minutes ago", (seconds / MINUTE));
    else if (seconds < (90 * MINUTE))
        format(time, sizeof (time), "An hour ago");
    else if (seconds < (24 * HOUR))
        format(time, sizeof (time), "%i hours ago", (seconds / HOUR));
    else if (seconds < (48 * HOUR))
        format(time, sizeof (time), "Yesterday");
    else if (seconds < (30 * DAY))
        format(time, sizeof (time), "%i days ago", (seconds / DAY));
    else if (seconds < (12 * MONTH))
    {
        new months = floatround(seconds / DAY / 30);
        if (months <= 1)
            format(time, sizeof (time), "One month ago");
        else
            format(time, sizeof (time), "%i months ago", months);
    }
    else
    {
        new years = floatround(seconds / DAY / 365);
        if (years <= 1)
            format(time, sizeof (time), "One year ago");
        else
            format(time, sizeof (time), "%i years ago", years);
    }
    return time;
}

Example:
pawn Code:
print(ReturnTimelapse(gettime(), gettime() + 60)); // Result: "A minute ago"
pawn Code:
print(ReturnTimelapse(gettime(), gettime() + 15)); // Result: "15 seconds ago"
pawn Code:
print(ReturnTimelapse(gettime(), gettime() + 3600)); // Result: "An hour ago"
__________________

Follow me on Github: Follow me on Spotify: Donate on Paypal:

Gammix is offline   Reply With Quote
Old 08/09/2016, 12:23 AM   #3903
Dayrion
High-roller
 
Dayrion's Avatar
 
Join Date: Jan 2016
Location: France
Posts: 1,918
Reputation: 258
Default Re: Useful Functions

Create a circle with an object and get the center's coordinates.
PHP Code:
CreateCircle(objectidFloat:xFloat:yFloat:zFloat:size, &Float:valA2, &Float:valB2, &Float:valC2)
{
    new 
Float:valx2,
        
Float:valy2,
        
Float:valz2;

    for(new 
i360i++) // 3498 - 1214
    
{
        if(
== 180)
        {
            
valx2 x;
            
valy2 y;
            
valz2 z;
        }

        
x+size*floatcos(i,degrees);
          
y+size*floatsin(i,degrees);
      
          if(
objectid != 0CreateObject(objectidxyz0.00.00.0);
    }
    
GetCoordMid2Pts(valx2valy2valz2xyzvalA2valB2valC2);
}

stock GetCoordMid2Pts(Float:x1Float:y1Float:z1Float:x2Float:y2Float:z2, &Float:rX, &Float:rY, &Float:rZ)
{
    
rX = (x1+x2)/2;
    
rY = (y1+y2)/2
    
rZ = (z1+z2)/2;

__________________
actor_plus - v5.0.2
« I'm a goner, somebody catch my breath,
I'm a goner, somebody catch my breath »

Last edited by Dayrion; 09/09/2016 at 10:53 AM.
Dayrion is offline   Reply With Quote
Old 09/09/2016, 10:53 AM   #3904
Dayrion
High-roller
 
Dayrion's Avatar
 
Join Date: Jan 2016
Location: France
Posts: 1,918
Reputation: 258
Default Re: Useful Functions

PHP Code:
stock IsVehicleInRangeOfPoint(vehicleidFloat:rangeFloat:xFloat:yFloat:z)
{
    new 
Float:x1,
        
Float:y1,
        
Float:z1;

    
CreateCircle(0xyzrange*0.01x1y1z1);

    
GetVehiclePos(vehicleidxyz);

    new 
Float:dist GetDistanceBetween2Points(xyzx1y1z1);
  
    if(
dist range
        return 
true;
    else
        return 
false;
}

stock GetNearestVehicleByCoord(Float:xFloat:yFloat:zFloat:precision 2.0)
{
    new    
vehicle INVALID_VEHICLE_ID;

    for(new 
i=1i<MAX_VEHICLESi++)
    {
        new 
Float:vehDist GetVehicleDistanceFromPoint(ixyz);
        if(
vehDist == 0.0)
        {
            if(
GetVehiclePoolSize()) return vehicle;
            else continue;
        }
        if(
vehDist precision) continue;
        else 
precision vehDistvehicle i;
    }
    return 
vehicle;
}

stock Float:GetDistanceBetween2Points(Float:x1Float:y1Float:z1Float:x2Float:y2Float:z2)
{
    return 
VectorSize(x1-x2y1-y2z1-z2);
}

CMD:exemple(playerid)
{
    new 
Float:x
        
Float:y,
        
Float:z;
    
    
GetVehiclePos(GetPlayerVehicleID(playerid), xyz);

    if(
IsVehicleInRangeOfPoint(110xyz)) 
        return 
SCM(playeridGREEN"In range!");
    else
        return 
SCM(playeridRED"Out of range");

__________________
actor_plus - v5.0.2
« I'm a goner, somebody catch my breath,
I'm a goner, somebody catch my breath »

Last edited by Dayrion; 10/09/2016 at 12:00 PM.
Dayrion is offline   Reply With Quote
Old 09/09/2016, 04:42 PM   #3905
Nero_3D
High-roller
 
Nero_3D's Avatar
 
Join Date: Jun 2007
Location: Germany
Posts: 2,837
Reputation: 671
Default Re: Useful Functions

Quote:
Originally Posted by Dayrion View Post
PHP Code:
//CODE 
You should use natives because they are faster

IsVehicleInRangeOfPoint could be done with GetVehicleDistanceFromPoint
PHP Code:
#define IsVehicleInRangeOfPoint(%0,%1, %1 > GetVehicleDistanceFromPoint(%0, 
In GetNearestVehicleByCoord you use IsVehicleInRangeOfPoint three times, make use of an additional variable and call it only once
For GetDistanceBetween2Points use the native VectorSize
Nero_3D is online now   Reply With Quote
Old 10/09/2016, 10:25 AM   #3906
rootcause
Big Clucker
 
rootcause's Avatar
 
Join Date: Feb 2015
Location: /
Posts: 114
Reputation: 197
Default Re: Useful Functions

ReturnPhonetic
Returns NATO phonetic alphabet code word of a letter, d = delta for example. Might be useful for TDM servers.

Code:
static const
	NATOPhonetic[][9 char] = {
	//  https://en.wikipedia.org/wiki/NATO_phonetic_alphabet#Letters
	    !"Alfa", !"Bravo", !"Charlie", !"Delta", !"Echo",
		!"Foxtrot", !"Golf", !"Hotel", !"India", !"Juliett",
		!"Kilo", !"Lima", !"Mike", !"November", !"Oscar",
		!"Papa", !"Quebec", !"Romeo", !"Sierra", !"Tango",
		!"Uniform", !"Victor", !"Whiskey", !"X-ray", !"Yankee",
		!"Zulu"
	};

ReturnPhonetic(letter)
{
	new idx = toupper(letter) - 65, string[9];
	if(!(0 <= idx <= 25))
	{
		format(string, sizeof(string), "Invalid");
		return string;
	}

	strunpack(string, NATOPhonetic[idx], sizeof(string));
	return string;
}
Example:
Code:
new string[32];
format(string, sizeof(string), "We lost objective %s!", ReturnPhonetic('c'));
SendClientMessageToAll(-1, string);
// output: We lost objective Charlie!
__________________
All Releases | YouTube
rootcause is offline   Reply With Quote
Old 10/09/2016, 11:55 AM   #3907
Dayrion
High-roller
 
Dayrion's Avatar
 
Join Date: Jan 2016
Location: France
Posts: 1,918
Reputation: 258
Default Re: Useful Functions

Quote:
Originally Posted by Nero_3D View Post
You should use natives because they are faster

IsVehicleInRangeOfPoint could be done with GetVehicleDistanceFromPoint
PHP Code:
#define IsVehicleInRangeOfPoint(%0,%1, %1 > GetVehicleDistanceFromPoint(%0, 
In GetNearestVehicleByCoord you use IsVehicleInRangeOfPoint three times, make use of an additional variable and call it only once
For GetDistanceBetween2Points use the native VectorSize
I didn't thought about this. '-'
Oh yes, you are right. I forgot to check if I used too many times a function without affect it in a variable. : x
Yep! Someone said it like 5 mins ago. I took the mathematic function so.. I didn't check about a function which returning the norm of a vector. :$
Thanks for your reply!
- Sorry for English mistakes :$
__________________
actor_plus - v5.0.2
« I'm a goner, somebody catch my breath,
I'm a goner, somebody catch my breath »
Dayrion is offline   Reply With Quote
Old 02/10/2016, 11:12 AM   #3908
Dayrion
High-roller
 
Dayrion's Avatar
 
Join Date: Jan 2016
Location: France
Posts: 1,918
Reputation: 258
Default Re: Useful Functions

Back to school with this one ;p
PHP Code:
Float:EuclideanDivision(dividenddivisor, &rest, &Float:quotientprecision 5)
{
    new 
temp;
    
quotient dividend divisor;
    
rest dividend divisor;
    while(
rest != 0)
    {
        if(
temp++ > precision)
            break;
        
dividend *= 10;
        
quotient quotient + (dividend divisor) * 0.10;
        
rest dividend divisor;
    }
    return 
quotient;

__________________
actor_plus - v5.0.2
« I'm a goner, somebody catch my breath,
I'm a goner, somebody catch my breath »

Last edited by Dayrion; 17/10/2016 at 02:06 PM.
Dayrion is offline   Reply With Quote
Old 09/10/2016, 04:47 PM   #3909
OstGot
Gangsta
 
OstGot's Avatar
 
Join Date: Mar 2013
Location: Gomel, Belarus
Posts: 880
Reputation: 326
Default Re: Useful Functions

Fixed split function:
In contrast to the original function (which had an issue with out of bounds of the array "destination", if the string "source" had delimiters more than an array size of "destination"), this function requires to specify two-dimensional array size of "destination". So it have two new arguments: destsize (the size of the first dimension), destlen (and second).
Code:
split(const source[], destination[][], delim, destsize, destlen)
{
	for(new i, li, aNum, len, s = strlen(source); i <= s && aNum < destsize; i++)
	{
		if(source[i] == delim || i == s)
		{
			len = strmid(destination[aNum], source, li, i, destlen);
			destination[aNum][len] = EOS;
			li = i + 1;
			aNum++;
		}
	}
	return 1;
}
Example:
Code:
new pos[3][9];
new strtmp[] = "1958.3783, 1343.1572, 15.3746 ||| Other s,t,u,f,f";
split(strtmp, pos, ',', sizeof pos, sizeof pos[]);
SetPlayerPos(playerid, floatstr(pos[0]), floatstr(pos[1]), floatstr(pos[2]));
Simple strfindchar functon:
This is faster than using strfind function, but only in case to search for a single character in the string.
It's useful only if you use this function in events that are often called (cycle, OnPlayerUpdate?) or for find a large number of single characters
Code:
strfindchar(const string[], sub)
{
	for(new i; string[i] != '\0'; i++)
	{
		if(string[i] == sub) return i;
	}
	return -1;
}
Example:
Code:
new strtmp[] = "Question?";
if(strfindchar(strtmp, '?') != -1) //Pay attention to '' (not "")
{
	print("Yes");
}
else print("No");
__________________
aka Nexius
Don't want to always check for updates of my scripts?
Include it the last, after all others
Nexius's Update Checker
OstGot is offline   Reply With Quote
Old 14/10/2016, 05:44 PM   #3910
MerryDeer
Banned
 
Join Date: Jun 2016
Posts: 723
Reputation: 8
Default Re: Useful Functions

Who can make function laser point, how to get rotations rx,ry,rz for certain point?
MerryDeer 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
[Include] [INC] LSF - Lorenc's Simple Functions (w/ gang/clan functions) Lorenc_ Includes 14 21/04/2018 05:06 AM
Functions? sciman001 Help Archive 7 19/03/2011 04:03 PM
How to get functions? Twain32 Help Archive 4 08/10/2010 08:37 PM
[Include] [INC] SA:MP New Functions 0.4a DKN ipsBruno Lançamentos/Releases 22 17/06/2010 09:38 AM
What this functions do? harrold Help Archive 2 09/05/2009 12:30 AM


All times are GMT. The time now is 04:23 AM.


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