Useful Functions

Nvm. Don't response.
Reply

Quote:
Originally Posted by g_aSlice
Посмотреть сообщение
Yeah, well, if the congruence (2nd value) is negative; which, in this case, it isn't.
Ah, my mistake. I'll update it in a bit.
Reply

But cynic's function would only work if Hex lenght is 8
Reply

pawn Код:
enum
    V_Type
{
    ENGINE,
    LIGHTS,
    ALARM,
    DOORS,
    BONNET,
    BOOT,
    OBJECTIVE
};

stock
    GetVehicleParams(vehicleid, V_Type:type)
{
    if(vehicleid < 1) return INVALID_VEHICLE_ID;
    new
        GetStatus,
        None;
    switch(type)
    {
        case ENGINE:        GetVehicleParamsEx(vehicleid, GetStatus, None, None, None, None, None, None);
        case LIGHTS:        GetVehicleParamsEx(vehicleid, None, GetStatus, None, None, None, None, None);
        case ALARM:         GetVehicleParamsEx(vehicleid, None, None, GetStatus, None, None, None, None);
        case DOORS:         GetVehicleParamsEx(vehicleid, None, None, None, GetStatus, None, None, None);
        case BONNET:        GetVehicleParamsEx(vehicleid, None, None, None, None, GetStatus, None, None);
        case BOOT:          GetVehicleParamsEx(vehicleid, None, None, None, None, None, GetStatus, None);
        case OBJECTIVE:     GetVehicleParamsEx(vehicleid, None, None, None, None, None, None, GetStatus);
    }
    return GetStatus; // (-1 VEHICLE_PARAMS_UNSET)      (0 VEHICLE_PARAMS_OFF)      (1 VEHICLE_PARAMS_ON)
}
Verzion 2:
pawn Код:
stock
    GetVehicleParams(vehicleid, V_Type:type)
{
    if(vehicleid < 1) return INVALID_VEHICLE_ID;
    new
        engine,
        lights,
        alarm,
        doors,
        bonnet,
        boot,
        objective;
    GetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, boot, objective);
    switch(type)
    {
        case P_ENGINE:      return engine; 
        case P_LIGHTS:      return lights;
        case P_ALARM:       return alarm;
        case P_DOORS:       return doors;
        case P_BONNET:      return bonnet;
        case P_BOOT:        return boot;
        case P_OBJECTIVE:   return objective;
    }
    return VEHICLE_PARAMS_UNSET;
}
Example:
pawn Код:
if(GetVehicleParams(GetPlayerVehicleID(playerid), ENGINE) == VEHICLE_PARAMS_ON)
{
// etc...
Reply

I'm not sure is this posted before, but it's a useful one. Convert integer value from 1 to 12 to a month

Written by me of course

pawn Код:
stock NumberToMonth(number)
{
new month[24];
if (number < 1 || number > 12) month = "INVALID_MONTH";
if (number == 1) month = "January";
if (number == 2) month = "February";
if (number == 3) month = "March";
if (number == 4) month = "April";
if (number == 5) month = "May";
if (number == 6) month = "June";
if (number == 7) month = "July";
if (number == 8) month = "August";
if (number == 9) month = "September";
if (number == 10) month = "October";
if (number == 11) month = "November";
if (number == 12) month = "December";

return month;
}
Reply

pawn Код:
static stock NumberToMonth(number)
{
new month[24];
switch(number)
{
case 1:month = "January";
case 2: month = "February";
case 3: month = "March";
case 4: month = "April";
case 5: month = "May";
case 6: month = "June";
case 7: month = "July";
case 8: month = "August";
case 9: month = "September";
case 10: month = "October";
case 11: month = "November";
case 12: month = "December";
case default:month = "INVALID_MONTH";
}
return month;
}
Reply

I think it's just a a matter of opinion. All of them will work, so what's the problem?

EDIT:

Fail:

pawn Код:
case default:month = "INVALID_MONTH";
->>
pawn Код:
default:month = "INVALID_MONTH";
Reply

******,why if else better than switch?...
Jakku,oops
Reply

Function 1
pawn Код:
stock FactionColor(factiune)
{
    switch(factiune)
    {
        case 1..3: return COLOR_DBLUE;
        case 4: return COLOR_LIGHTRED;
        case 5: return COLOR_ORANGE;
        case 6: return COLOR_GREEN;
        case 7: return COLOR_LIGHTGREEN;
        case 8: return COLOR_RED;
        case 9: return COLOR_NEWS2;
        case 10: return COLOR_YELLOW;
        case 11: return COLOR_LICENSE;
        case 12: return COLOR_GANGSTA;
        case 13: return COLOR_BLOODS;
        case 14: return COLOR_SURENOS;
        case 15: return COLOR_NORTENOS;
        case 16: return COLOR_FMA;
    }
    return COLOR_WHITE;
}
vs function 2:
pawn Код:
stock FactionColor2(factiune)
{
    static const
        sc_culoare[17] =
        {
            COLOR_WHITE,
            COLOR_DBLUE,
            COLOR_DBLUE,
            COLOR_DBLUE,
            COLOR_LIGHTRED,
            COLOR_ORANGE,
            COLOR_GREEN,
            COLOR_LIGHTGREEN,
            COLOR_RED,
            COLOR_NEWS2,
            COLOR_YELLOW,
            COLOR_LICENSE,
            COLOR_GANGSTA,
            COLOR_BLOODS,
            COLOR_SURENOS,
            COLOR_NORTENOS,
            COLOR_FMA
        };
    if (0 <= factiune <= 16)
    {
        return sc_culoare[factiune];
    }
    return sc_culoare[0];
}
Timing for 100000 function calls with parameter 1:
Код:
[13:30:58] GetTickCount = 43912
[13:30:58] Time #1: 13
[13:31:01] GetTickCount = 46336
[13:31:01] Time #2: 19
Timing for 100000 function calls with parameter 16:
Код:
[13:41:31] GetTickCount = 13094
[13:41:31] Time #1: 16
[13:41:33] GetTickCount = 15054
[13:41:33] Time #2: 19
The execution time in case 1, will grow as long as the number of options(cases - which will have to be compared) will grow.
In the second implementation time remains the same, no matter how many options because the direct acces of the value in the array.

So, first is best for few options and second is for many options as perhaps > 50.
Reply

I think no-one is calling this "NumberToMonth"- function more than once, so it's useless to count how long it takes to execute it 100000 times. Using arrays is a bit faster, but whatever.
Reply

Quote:
Originally Posted by Jakku
Посмотреть сообщение
I think no-one is calling this "NumberToMonth"- function more than once, so it's useless to count how long it takes to execute it 100000 times. Using arrays is a bit faster, but whatever.
If you think like that when making a gamemode, you'll end up with tons of unoptimized functions resulting in higher and cpu usage.
Reply

******,but my function fast than function where if else =)
Reply

Quote:
Originally Posted by g_aSlice
Посмотреть сообщение
If you think like that when making a gamemode, you'll end up with tons of unoptimized functions resulting in higher and cpu usage.
Don't be a wise guy. That was just an example


Quote:
Originally Posted by Johnny_Xayc
Посмотреть сообщение
******,but my function fast than function where if else =)
Switch is faster than if.
Reply

I know when it's the right time to change using arrays. Even I know a little bit about optimizing
Reply

Need Function:
Код:
stock mktime(hour,minute,second,day,month,year)
{
	new timestamp2 = second + (minute * 60) + (hour * 3600), days_of_month[12], days_this_year = day;
	if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) days_of_month = {31,29,31,30,31,30,31,31,30,31,30,31};
	else days_of_month = {31,28,31,30,31,30,31,31,30,31,30,31};
	if(month > 1) for(new i=0; i<month-1;i++) days_this_year += days_of_month[i];
	timestamp2 += days_this_year * 86400;
	for(new j=1970;j<year;j++) {
		timestamp2 += 31536000;
		if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) timestamp2 += 86400;
	}
	return timestamp2;
}
Function
Код:
stock date(zeitpunkt, &hour, &minute, &second, &day, &month, &year) // by Steam
{
	new h = 0, m = 0, s = 0, da = 1, mo = 1, ye = 1970;
 	for(;;) { ye++; if(((zeitpunkt) - (mktime(h, m, s, da, mo, ye))) < 0) { ye--; break; } }
 	for(;;) { mo++; if(((zeitpunkt) - (mktime(h, m, s, da, mo, ye))) < 0) { mo--; break; } }
 	for(;;) { da++; if(((zeitpunkt) - (mktime(h, m, s, da, mo, ye))) < 0) { da--; break; } }
 	for(;;) { h++; if(((zeitpunkt) - (mktime(h, m, s, da, mo, ye))) < 0) { h--; break; } }
 	for(;;) { m++; if(((zeitpunkt) - (mktime(h, m, s, da, mo, ye))) < 0) { m--; break; } }
 	for(;;) { s++; if(((zeitpunkt) - (mktime(h, m, s, da, mo, ye))) < 0) { s--; break; } }
 	hour = h; minute = m; second = s; day = da; month = mo; year = ye; return 1;
}
Example:
Код:
new h, mins, s, d, m, y;
date(mktime(18, 32, 0, 16, 04, 2002), h, mins, s, d, m, y);
printf("%d.%d.%d %d:%d:%d", d, m, y, h, mins, s);
Reply

The function chechk, the vehicle have a lincense plate.

If not have, return false, else true.

pawn Код:
stock bool:IsLicensePlate( vehicleid )
{
    switch( GetVehicleModel(vehicleid) )
    {
        case 406, 417, 425, 430, 432, 435, 441, 444, 446, 447, 449, 450,
        452, 453, 454, 457, 460, 464, 465, 468, 469, 471, 472, 473, 476,
        481, 484, 485, 486, 487, 488, 493, 494, 497, 501, 502, 503, 509,
        510, 511, 512, 513, 514, 515, 519, 520, 522, 528, 530, 531, 532,
        537, 538, 539, 548, 553, 563, 564, 568, 569, 570, 571, 572, 573,
        577, 583, 584, 590, 591, 592, 593, 594, 595, 601, 606, 607, 608,
        610, 611: return false;
    }
    return true;
}
Reply

Kick for high ping using average
Credits to ****** for 'foreach'


#defines
pawn Код:
#define MAX_PING 1000
#define PING_CHECKS 5
variables (global)
pawn Код:
new avgcount[MAX_PLAYERS];
new pping[MAX_PLAYERS];
timer
pawn Код:
SetTimer("Ping_Timer", 5000, true);
function
pawn Код:
forward Ping_Timer();
public Ping_Timer()
{
    foreach(Player, i)
    {
        avgcount[i]++;
        new ping = pping[i];
        pping[i] = GetPlayerPing(i);
        pping[i] = ping + pping[i];
        if((avgcount[i]%PING_CHECKS) == 0)
        {
            pping[i] = (pping[i]/avgcount[i]);
            if(pping[i] > MAX_PING)
            {
                new name[24], string[128];
                GetPlayerName(i, name, sizeof(name));
                format(string, sizeof(string), "[PLUTO AC]: %s has been kicked for high ping (%d/%d)", name, pping[i], MAX_PING);
                SendClientMessageToAll(COLOR_REDONLY, string);
                Kick(i);
            }
            else pping[i] = 0;
        }
    }
}
Reply

This is a usefull function, what Im gonna give.
It already exists of course, but I found the PERFECT way to have an rp speed, and keep it short+easy
Here it is:

pawn Код:
stock GetVehicleSpeed(vehicleid)
{
    new Float:v_vX, Float:v_vY, Float:v_vZ, Float:retres;
    GetVehicleVelocity(vehicleid, v_vX, v_vY, v_vZ);
    retres = floatsqroot(floatabs(floatpower(v_vX + v_vY + v_vZ, 2)));
    return floatround(retres * 100, floatround_ceil);
}
It took me about 5 minutes. I'm not good in all the float stuff (the float functions) so I had to experiment xD.

- Kevin
Reply

Change weapon as passenger:
pawn Код:
#include <a_samp>
#define PRESSED(%0) (((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))

new bool: FirstTime[MAX_PLAYERS];

public OnPlayerDisconnect(playerid, reason)
{
    FirstTime[playerid] = false;
    return true;
}

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(PRESSED(128) && GetPlayerState(playerid) == PLAYER_STATE_PASSENGER)
    {
         if(FirstTime[playerid])
             {
                 SetPVarInt(playerid, "weaponActual", GetPlayerWeapon(playerid));  
                 FirstTime[playerid] = false;
             }
             NextSlot(playerid, GetPVarInt(playerid, "weaponActual"));            
    }
    return 1;
}

NextSlot(playerid, weaponinuse)
{
    static weapons[13][2], i, j;
    for (i = 2; i < 13; ++i)
    {
        GetPlayerWeaponData(playerid, i, weapons[i][0], weapons[i][1]);
       
        if(weapons[i][0] == weaponinuse) j=i;      
    }

    while(j < 7)
    {
        j++;
        if(weapons[j][0])
        {
            SetPlayerArmedWeapon(playerid, weapons[j][0]);
            SetPVarInt(playerid, "weaponActual", weapons[j][0]);
            return true;
        }
    }
    SetPlayerArmedWeapon(playerid, weapons[2][0]);
    SetPVarInt(playerid, "weaponActual", weapons[2][0]);
    return true;
}
Used PVars because the function GetPlayerWeapon or SetPlayerArmedWeapon don'ts works more than one time , when the player is a passenger, in the version 0.3c RC4-2.
Reply

Changing weapon preferences
Like the above poster, these functions will allow people to switch weapons but is not limited to the vehicle (in case you don't want certain weapons to be allowed to shoot from drive-by). Also, this gives a more simple, and clean approach. There are three functions included, even though one of them is used internally (I'm sure you could find better uses for it as well). Please note, this will take 0.3c RC4 or later to work correctly.

All descriptions about the functions can be found inside of the comments. If you have questions regarding them, ask.
pawn Код:
// You will need to change the IDs in the array to match with the weapons
// you do not want used inside of the script

/*
Function:
    AdjustWeapons
Params:
    playerid - The ID of the player
Usage:
    Checks for certain weapons, if that weapon is armed
    then it will replace it with a sub-machine gun if
    the player has one. If not, it will set to fist.
Returns:
    N/A
*/


stock AdjustWeapons( playerid )
{
    new
        ForbiddenWeapons[ ] = { 24, 27 }
    ;
   
    for( new i = 0; i < sizeof( ForbiddenWeapons ); i ++ )
    {
        if( GetPlayerWeapon( playerid ) == ForbiddenWeapons[ i ] )
        {
            new
                weapon,
                ammo
            ;
           
            GetPlayerWeaponData( playerid, 4, weapon, ammo );
            if( ammo > 0 )
                SetPlayerArmedWeapon( playerid, weapon );
            else   
                SetPlayerArmedWeapon( playerid, 0 );
        }
    }
}

/*
Function:
    AdjustWeaponsEx
Params:
    playerid - The ID of the player
    slot - The ID of the slot to replace the forbidden weapon with
    weapon = 0 - The ID of the weapon to replace with (if the slot specified is unoccupied)
Usage:
    Checks for certain weapons, if that weapon is armed
    it will replace it with a weapon in the specified
    slot. If there are no weapons in that slot, it will
    replace it with the specified weapon ID (default = 0)
    If that weapon is not found either, it will default
    back to fist.
Returns:
    N/A
*/


stock AdjustWeaponsEx( playerid, slot, weapon = 0 )
{
    new
        ForbiddenWeapons[ ] = { 24, 27 }
    ;
   
    for( new i = 0; i < sizeof( ForbiddenWeapons ); i ++ )
    {
        if( GetPlayerWeapon( playerid ) == ForbiddenWeapons[ i ] )
        {
            new
                weapon_s,
                ammo
            ;
           
            GetPlayerWeaponData( playerid, slot, weapon_s, ammo );
            if( ammo <= 0 )
            {
                GetPlayerWeaponData( playerid, GetWeaponSlot( weapon ), weapon_s, ammo );
                if( ammo <= 0 )
                    SetPlayerArmedWeapon( playerid, 0 );
                else   
                    SetPlayerArmedWeapon( playerid, weapon );
            }
            else   
                SetPlayerArmedWeapon( playerid, weapon_s );
        }
    }
}

/*
Function:
    GetWeaponSlot
Params:
    weaponid - The ID of the weapon
Usage:
    Gets the slot ID for the specified weapon
Returns:
    The returned slot ID. (Default = -1)
*/


stock GetWeaponSlot( weaponid )
{
    switch( weaponid )
    {
        case 0, 1: return 0;
        case 2..9: return 1;
        case 10..15: return 10;
        case 16..18, 39: return 8;
        case 22..24: return 2;
        case 25..27: return 3;
        case 28, 29, 32: return 4;
        case 30, 31: return 5;
        case 33, 34: return 6;
        case 35..38: return 7;
        case 40: return 12;
        case 41..43: return 9;
        case 44..46: return 11;
        default: return -1;
    }
    return -1;
}
As mentioned above, these functions are not limited to vehicles, so you can use them for other things as you please. A few examples are below:
pawn Код:
// Will check if the player has a weapon in slot 5 and put that weapon
// If not, it will switch the player to weapon ID 16
// If none of the above are possible (meaning the player has no weapons in slot 5 and no weapon ID 16), it will default to fist

// That is all considering the player entered the vehicle with a weapon that corresponds to the IDs in the array

public OnPlayerStateChange( playerid, newstate, oldstate )
{
    if( newstate == PLAYER_STATE_PASSENGER )
        AdjustWeaponsEx( playerid, 5, 16 );
   
    return 1;
}
I'm looking forward to releasing more things on the forums again. If you have any suggestions for anything, send me a PM.
Reply


Forum Jump:


Users browsing this thread: 41 Guest(s)