SA-MP Forums Archive
Useful Functions - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Filterscripts (https://sampforum.blast.hk/forumdisplay.php?fid=17)
+--- Thread: Useful Functions (/showthread.php?tid=38965)



Re: Useful Functions - Luka P. - 21.02.2011

pawn Код:
stock IntToHex(num)  { new var[3]; format(var, sizeof(var), "%x", num); return var; }



Re: Useful Functions - BBAT - 21.02.2011

written in 30 sec

pawn Код:
stock position(playerid)
{
    new FLoat:lolA, Float:lolB, Float:lolC
    GetPlayerPos(playeridm lolA, lolB, lolC);
}

stock heal(playerid)
{
    SetPlayerHealth(playerid, 100);
}

stock kill(playerid)
{
    SetPlayerHealth(playerid, 0);
}

stock armored(playerid)
{
    SetPlayerArmor(playerid, 100);
}

stock unarmored(playerid)
{
    SetPlayerArmor(playerid, 0);
}

stock cj(playerid)
{
    SetPlayerSkin(playerid, 1);
}

stock name(playerid)
{
    new lolname[MAX_PLAYER_NAME];
    GetPlayerName(playerid, lolname, sizeof(lolname));
}



Re: Useful Functions - Calgon - 21.02.2011

Quote:
Originally Posted by BBAT
Посмотреть сообщение
written in 30 sec

pawn Код:
stock position(playerid)
{
    new FLoat:lolA, Float:lolB, Float:lolC
    GetPlayerPos(playeridm lolA, lolB, lolC);
}

stock heal(playerid)
{
    SetPlayerHealth(playerid, 100);
}

stock kill(playerid)
{
    SetPlayerHealth(playerid, 0);
}

stock armored(playerid)
{
    SetPlayerArmor(playerid, 100);
}

stock unarmored(playerid)
{
    SetPlayerArmor(playerid, 0);
}

stock cj(playerid)
{
    SetPlayerSkin(playerid, 1);
}

stock name(playerid)
{
    new lolname[MAX_PLAYER_NAME];
    GetPlayerName(playerid, lolname, sizeof(lolname));
}
Your name and position functions do the operation but return nothing, so if you execute name(playerid), nothing will be returned, as with the position function, you're not returning the position, you're just saving them in three different floats.


Re: Useful Functions - Amit_B - 21.02.2011

****** & RyDeR:
I didn't thought of using %x, you're right, it much better.

Edit:
I've checked that, and actually, using %x for 1 for example, will return "1" which is not able to be used on coloured chat messages.


Re: Useful Functions - BBAT - 22.02.2011

Quote:
Originally Posted by Calg00ne
Посмотреть сообщение
Your name and position functions do the operation but return nothing, so if you execute name(playerid), nothing will be returned, as with the position function, you're not returning the position, you're just saving them in three different floats.
oops i forgot that. I was in quick finger


Re: Useful Functions - $t3ve - 22.02.2011

This are just some functions/snippet i made, i didn't understood the difference between functions and snippets

[/CODE]
#define Slap(%0) new FloatX, FloatY, FloatZ, FloatH; GetPlayerHealth(%0, pH); SetPlayerHealth(%0, pH-5); GetPlayerPos(%0, pX, pY, pZ); SetPlayerPos(%0, pX, pY, pZ+5); PlayerPlaySound(%0, 1130, pX, pY, pZ+5)
#define Kill(%0) SetPlayerHealth(%0, 0.0)
#define Heal(%0) SetPlayerHealth(%0, 100.0)
#define function%0(%1) forward%0(%1); public%0(%1) // by RyDeRґ!
new __string[200];
#define SendClientMessagef(%0,%1,%2) format(__string, sizeof(__string), %2); SendClientMessage(%0, %1, __string)
#define SendClientMessageToAllf(%0,%1) format(__string, sizeof(__string), %1); SendClientMessageToAll(%0, __string)
#define RestartServer() SendRconCommand("gmx")
#define SetMapName(%0) format(__string, sizeof(__string), "mapname %s", %0); SendRconCommand(__string)
#define SetHostName(%0) format(__string, sizeof(__string), "hostname %s", %0); SendRconCommand(__string)
#define ReloadBans() SendRconCommand("reloadbans")
#define LoadFilterScript(%0) format(__string, sizeof(__string), "loadfs %s", %0); SendRconCommand(__string)
#define UnloadFilterScript(%0) format(__string, sizeof(__string), "unloadfs %s", %0); SendRconCommand(__string)
#define Crash(%0) SetPlayerSkin(%0, 666666)

stock Teleport(playerid, Float:X, Float:Y, Float:Z, interior)
{
if(IsPlayerInAnyVehicle(playerid))
{
if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
{
SetVehiclePos(GetPlayerVehicleID(playerid),X,Y,Z);
LinkVehicleToInterior(GetPlayerVehicleID(playerid) ,interior);
SetCameraBehindPlayer(playerid);
}
else
{
SendClientMessage(playerid,0xFFFF00AA, "ERROR: You need to be the driver!");
}
}
else
{
SetPlayerPos(playerid,X,Y,Z);
SetPlayerInterior(playerid,interior);
SetCameraBehindPlayer(playerid);
}
return 1;
}
[CODE]


Re: Useful Functions - $t3ve - 22.02.2011

This are just some functions/snippet i made, i didn't understood the difference between functions and snippets

Код:
#define Slap(%0) new Float:pX, Float:pY, Float:pZ, Float:pH; GetPlayerHealth(%0, pH); SetPlayerHealth(%0, pH-5); GetPlayerPos(%0, pX, pY, pZ); SetPlayerPos(%0, pX, pY, pZ+5); PlayerPlaySound(%0, 1130, pX, pY, pZ+5)
#define Kill(%0) SetPlayerHealth(%0, 0.0)
#define Heal(%0) SetPlayerHealth(%0, 100.0)
#define function%0(%1) forward%0(%1); public%0(%1) // by RyDeRґ!
new __string[200];
#define SendClientMessagef(%0,%1,%2) format(__string, sizeof(__string), %2); SendClientMessage(%0, %1, __string)
#define SendClientMessageToAllf(%0,%1) format(__string, sizeof(__string), %1); SendClientMessageToAll(%0, __string)
#define RestartServer() SendRconCommand("gmx")
#define SetMapName(%0) format(__string, sizeof(__string), "mapname %s", %0); SendRconCommand(__string)
#define SetHostName(%0) format(__string, sizeof(__string), "hostname %s", %0); SendRconCommand(__string)
#define ReloadBans() SendRconCommand("reloadbans")
#define LoadFilterScript(%0) format(__string, sizeof(__string), "loadfs %s", %0); SendRconCommand(__string)
#define UnloadFilterScript(%0) format(__string, sizeof(__string), "unloadfs %s", %0); SendRconCommand(__string)
#define Crash(%0) SetPlayerSkin(%0, 666666)

stock Teleport(playerid, Float:X, Float:Y, Float:Z, interior)
{
	if(IsPlayerInAnyVehicle(playerid))
 	{
 		if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER)
  		{
  			SetVehiclePos(GetPlayerVehicleID(playerid),X,Y,Z);
    		LinkVehicleToInterior(GetPlayerVehicleID(playerid),interior);
     		SetCameraBehindPlayer(playerid);
      	}
       	else
        {
       		SendClientMessage(playerid,0xFFFF00AA, "ERROR: You need to be the driver!");
		}
	}
 	else
  	{
  		SetPlayerPos(playerid,X,Y,Z);
   		SetPlayerInterior(playerid,interior);
    	SetCameraBehindPlayer(playerid);
   	}
	return 1;
}

// Oh and also: 
new __pName[MAX_PLAYER_NAME];
#define SetPlayerClanTag(%0,%1) GetPlayerName(%0, __pName, sizeof(__pName)); format(__string, sizeof(__string), "[%s]%s", %1, __pName); SetPlayerName(%0, __string)



Re: Useful Functions - Amit_B - 22.02.2011

Quote:
Originally Posted by ******
Посмотреть сообщение
Ahh - you didn't say you were doing that. The code for a colour is:

pawn Код:
format(str, sizeof (str), "{%06x}", colour);
Or, if it's in RGBA format:

pawn Код:
format(str, sizeof (str), "{%06x}", colour >>> 8);
Great, I didn't knew that "%0_x" is possible, thanks.


Re: Useful Functions - RyDeR` - 23.02.2011

Yet another version of quickSort implemented (from C++) to PAWN. I tested serveral times and it worked flawless (even a little bit faster than existing versions).

pawn Код:
stock quickSort(array[], left, right)
{
    new
        tempLeft = left,
        tempRight = right,
        pivot = array[(left + right) / 2],
        tempVar
    ;
    while(tempLeft <= tempRight)
    {
        while(array[tempLeft] < pivot) tempLeft++;
        while(array[tempRight] > pivot) tempRight--;
       
        if(tempLeft <= tempRight)
        {
            tempVar = array[tempLeft], array[tempLeft] = array[tempRight], array[tempRight] = tempVar;
            tempLeft++, tempRight--;
        }
    }
    if(left < tempRight) quickSort(array, left, tempRight);
    if(tempLeft < right) quickSort(array, tempLeft, right);
}
Example:
pawn Код:
new
    array[] = { -541, 54, 689, 12, 3, 0, 3, 55, 66, -541, 5468484, -564, 1554, 1656 }
;
quickSort(array, 0, sizeof(array) - 1);
   
for(new i; i != sizeof(array); ++i)
{
    printf("%d", array[i]);
}
Output:
Код:
-564
-541
-541
0
3
3
12
54
55
66
689
1554
1656
5468484



Re: Useful Functions - Sasino97 - 23.02.2011

Is there a function to return if a number is even or uneven?

Example:

stock IsNumberEven(number)
return true/false

etc...

Instead of

new EvenNumbers= {0,2,4,6,8,10,12,14,16,18,20,.... to the infinite?}


Re: Useful Functions - Toribio - 23.02.2011

A number is odd when (number & 1) returns 1 and it's even when it returns 0.

pawn Код:
#define iseven(%1) (((%1) & 1) == 0)
Then, of course !iseven(n) tells when a number is odd.


Re: Useful Functions - dice7 - 24.02.2011

pawn Код:
#define IsOdd(%0) %0 % 2
#define IsEven !IsOdd



Re: Useful Functions - Slice - 27.02.2011

That list will be full with disconnected IDs at the bottom having score 0. A good thing to add would probably be a size limit, which would allow one to loop through only what's neccessary.


Re: Useful Functions - RyDeR` - 27.02.2011

True, I changed the code a bit. Should work good now.


Re: Useful Functions - smeti - 27.02.2011

GetPlayerCameraFrontVector_Ex
Modified to samp verzion.
pawn Код:
stock                               // radius aka distance
    GetPlayerCameraFrontVector_Ex(Float:radius, Float:Phi, Float:Lambda, &Float:x, &Float:y, &Float:z) // Thanks ****** Decartes trigonometria
{
    x = radius * floatcos(-Phi, degrees) * floatsin(-Lambda, degrees);
    y = radius * floatcos(-Phi, degrees) * floatcos(-Lambda, degrees);
    z = radius * floatsin(-Phi, degrees);
}
[ame]http://www.youtube.com/watch?v=qVZKavjlvxA[/ame]


AW: Re: Useful Functions - Nero_3D - 27.02.2011

Quote:
Originally Posted by smeti
Посмотреть сообщение
GetPlayerCameraFrontVector_Ex
Modified to samp verzion.
pawn Код:
stock                               // radius aka distance
    GetPlayerCameraFrontVector_Ex(Float:radius, Float:Phi, Float:Lambda, &Float:x, &Float:y, &Float:z) // Thanks ****** Decartes trigonometria
{
    x = radius * floatcos(-Phi, degrees) * floatsin(-Lambda, degrees);
    y = radius * floatcos(-Phi, degrees) * floatcos(-Lambda, degrees);
    z = radius * floatsin(-Phi, degrees);
}
Unluckily I cant watch the video
Could you write an little explanation what this function does
Or what you need to insert in phi / lambda


Re: Useful Functions - smeti - 27.02.2011

GetPlayerFacingAngle(playerid, Lambda); (xy angle) 0-360
Phi = Horizont; (z angle) 0-360


Re: Useful Functions - RyDeR` - 28.02.2011

I edited my code a bit from this post (now, without checking if it's valid (-> code more faster)). Idea comes from JernejL. I just increased the performance of the code.

pawn Код:
stock filterColorTags(string[], bool: gxtTags, bool: sampColors)
{
    if(gxtTags)
    {
        new
            textPos = -1
        ;
        while((textPos = strfind(string, "~", false, (textPos + 1))) != -1)
        {  
            if(string[(textPos + 2)] == '~')
            {
                strdel(string, textPos, (textPos + 3));
            }
        }
    }
    if(sampColors)
    {
        new
            textPos = -1
        ;
        while((textPos = strfind(string, "{", false, (textPos + 1))) != -1)
        {
            if(string[(textPos + 7)] == '}')
            {
                strdel(string, textPos, (textPos + 8));
            }
        }
    }
}



Re: Useful Functions - Slice - 28.02.2011

You're assuming the string is 2 chars longer than any position found for the ~ character. That's not good!
A line ending with a ~ could cause problems with the script. Same goes for {, but that's even worse.


Re: Useful Functions - RyDeR` - 28.02.2011

No problems, works fine. (I just tried it)