Useful Snippets

Quote:
Originally Posted by ******
Посмотреть сообщение
"min" means "minimum", so gets the smaller of the two items.
I know that, but is it implemented in pawn?
Reply

This function will disable the player weapons in hand efficiently.
For avoid problems if the player weapons are forbiden, before of give a weapon for the player, toggle the player weapons, give the new weapons and forbid it again.
pawn Код:
new PlayerWeapons[MAX_PLAYERS][2][13]; //in the top

TogglePlayerWeapons(playerid, bool:toggle)
{
    if(!toggle)
    {
        for(new i = 0; i < 13; i++)
        {
            GetPlayerWeaponData(playerid, i, PlayerWeapons[playerid][0][i], PlayerWeapons[playerid][1][i]);
            if(PlayerWeapons[playerid][0][i]) GivePlayerWeapon(playerid, PlayerWeapons[playerid][0][i], - (PlayerWeapons[playerid][1][i] * 2) );
        }
    }
    else
    {  
        ResetPlayerWeapons(playerid);      
        for(new i = 0; i < 13; i++)
        {
            if(PlayerWeapons[playerid][0][i]) GivePlayerWeapon(playerid, PlayerWeapons[playerid][0][i], PlayerWeapons[playerid][1][i]);
        }
    }
    return 1;
}
Reply

Anti Fake-Kill - 0.3d
pawn Код:
new
    iLastDamage[MAX_PLAYERS];

public OnPlayerGiveDamage(playerid, damagedid, Float:amount, weaponid)
{
    if (damagedid != INVALID_PLAYER_ID)
    {
        iLastDamage[damagedid] = GetTickCount();
    }
}

public OnPlayerDeath(playerid, killerid, reason)
{
    if (killerid != INVALID_PLAYER_ID && (iLastDamage[playerid] + 2000) < GetTickCount())
    {
        static
            szKickMessage[128],
            szPlayerName[MAX_PLAYER_NAME];

        GetPlayerName(playerid, szPlayerName, MAX_PLAYER_NAME);
        format(szKickMessage, 128, "* %s has been kicked because of doing a fake kill.", szPlayerName);
        SendClientMessageToAll(-1, szKickMessage);
        return Kick(playerid);
    }
}
Reply

That will probably suffer a couple false-positives because the killer could be lead aiming each shot (using a deagle, for example), only hitting the player getting killed for him.
Reply

Quote:
Originally Posted by Slice
Посмотреть сообщение
That will probably suffer a couple false-positives because the killer could be lead aiming each shot (using a deagle, for example), only hitting the player getting killed for him.
Sorry, but I cannot figure out what are you talking about. Could you please explain it further...?
Reply

OnPlayerGiveDamage is called when a player shoots another player *on their screen*, right? If someone is running around you in circles and you shoot ahead of the player you will hit him, but only *on his screen* (i.e. you won't see blood come out, but he'll lose damage).
Not only that, but if it were a 1-shot kill, and the shooter would be lagging, then the player that were shot will get kicked because the shooter didn't report to OnPlayerGiveDamage before the player that was shot reported OnPlayerDeath.
Reply

I can now understand what you say, but not sure if it is 100% ok (needs further investigation).
If a player shoots somebody, the client sends a packet that he shot to a position. When this happens, SA-MP checks if there is a player on the position, and if so, fires up OnPlayerGiveDamage callback and inform the victim player about the shoot.
Reply

Dynamic Object Creator

pawn Code:
public OnPlayerCommandText(playerid, cmdtext[])
{
    new cmd[256], idx;
    cmd = strtok(cmdtext, idx);
   
    if(strcmp(cmd, "/CreateObject", true) == 0)
    {
        new tmp[256];
        tmp = strtok(cmdtext, idx);
        if(!strlen(tmp)) return SendClientMessage(playerid, 0xFFFFFFFF, "Usage: {FF0000}/CreateObject [objectid]");
        new Float:X,Float:Y,Float:Z,Float:A;
        GetPlayerPos(playerid, X,Y,Z);
        GetPlayerFacingAngle(playerid, A);
        CreateObject(strval(tmp), X,Y,Z,0,0,A);
        SendClientMessage(playerid,0xFFFFFFFF, "{33FF33}Object Created!");
        return 1;
    }
    return 0;
}
Reply

Anti-Fake Kill. Needs more to it I guess but works.

pawn Code:
public OnPlayerDeath( playerid, killerid, reason )
{
    if( IsPlayerConnected( killerid ) )
    {
        if( IsPlayerConnected( GetPVarInt( playerid, "LastKilled" ) ) && GetPVarInt( playerid, "LastKilled" ) == killerid )
        {
            if( ( GetPVarInt( playerid, "LastKilledTickcount" ) - GetTickCount( ) ) < 500 )
            {
                SetPVarInt( playerid, "LastKilledTick", GetPVarInt( playerid, "LastKilledTick" ) + 1 );
                if( GetPVarInt( playerid, "LastKilledTick" ) > 1 ) Kick( playerid );
            }
            SetPVarInt( playerid, "LastKilledTickcount", GetTickCount( ) );
        }
        SetPVarInt( playerid, "LastKilled", killerid );
    }
    return 1;
}
There is a flaw with minigun, so make it admin immune or something.
Reply

Nevermind.
Reply

Quote:
Originally Posted by Jack_SMalls
View Post
pawn Code:
CMD:hbadge(playerid, params[])
{
    if(PlayerInfo[playerid][pENUM] == 6)//Use whatever is in your script. Like gTeam or something.
    {
        if(IsPlayerConnected(playerid))
        {
            if(isnull(params)) {
                SendClientMessageEx(playerid, COLOR_WHITE, "SYNTAX: /hbadge [name]");
                SendClientMessageEx(playerid, COLOR_RED, "Available names: CHANGEME, CHANGEME, CHANGEME, CHANGEME, Off");
                return 1;
            }
            if(strcmp(params,"CHANGEME",true) == 0)
            {
                SetPlayerColor(playerid, COLOR_WHITE);
                SendClientMessageEx(playerid, COLOR_WHITE, "Badge on."); // Change color.
            }
            else if(strcmp(params,"CHANGEME",true) == 0)
            {
                SetPlayerColor(playerid, COLOR_WHITE);
                SendClientMessageEx(playerid, COLOR_WHITE, "Badge on."); // Change color.
            }
            else if(strcmp(params,"CHANGEME",true) == 0)
            {
                SetPlayerColor(playerid, COLOR_WHITE);
                SendClientMessageEx(playerid, COLOR_WHITE, "Badge on.");// Change color.
            }
            else if(strcmp(params,"CHANGEME",true) == 0)
            {
                SetPlayerColor(playerid, COLOR_WHITE);
                SendClientMessageEx(playerid, COLOR_WHITE, "Badge on.");// Change color.
            }
            else if(strcmp(params,"off",true) == 0)
            {
                SetPlayerColor(playerid, COLOR_WHITE; // Possible to change this color. Depends on script default.
                SendClientMessageEx(playerid, COLOR_WHITE, "You're badge has been removed");
            }
        }
    }
    return 1;
}
Reply

@Jack_SMalls DAFUQ?!
Reply

OnPlayerMovedCamera - The callback's 0.3e RC2 above Only


pawn Код:
/*
 *
 * OnPlayerMovedCamera - Somente na versгo 0.3e RC2 ou superior.
 *
 *  Crйditos: DarkScripter
 *  - Nгo retire os crйditos.
 *
 *
*/


new bool:Chamed_Int[MAX_PLAYERS char] = false;

forward _FinisOnPlayerMovedCamera(playerid);
forward OnPlayerMovedCamera(playerid);

public _FinisOnPlayerMovedCamera(playerid)
{
    Chamed_Int{playerid} = false;
    CallLocalFunction("OnPlayerMovedCamera", "d", playerid);
}

stock InterpolateCameraLookAt_(p, Float:x, Float:y, Float:z, Float:x2, Float:y2, Float:z2, time, style)
{
    InterpolateCameraLookAt(p, Float:x, Float:y, Float:z, Float:x2, Float:y2, Float:z2, time, style);
    return !Chamed_Int{p} ? (SetTimerEx("_FinisOnPlayerMovedCamera", time, false, "d", p), Chamed_Int{p} = true) : true;
}
stock InterpolateCameraPos_(p, Float:x, Float:y, Float:z, Float:x2, Float:y2, Float:z2, time, style)
{
    InterpolateCameraPos(p, Float:x, Float:y, Float:z, Float:x2, Float:y2, Float:z2, time, style);
    return !Chamed_Int{p} ? (SetTimerEx("_FinisOnPlayerMovedCamera", time, false, "d", p), Chamed_Int{p} = true) : true;
}

#define InterpolateCameraLookAt         InterpolateCameraLookAt_
#define InterpolateCameraPos            InterpolateCameraPos_
How to use(exemple)
pawn Код:
#include <a_samp>

#define TIMER           ( 60000 * 1 ) // time to move cam ( 1 minute  )


public OnPlayerCommandText(playerid, cmdtext[])
{
    if(!strcmp(cmdtext,"/cam", true))
    {
        TogglePlayerSpectating(playerid, true);
        InterpolateCameraLookAt(playerid, 2045.2523, 1689.3992,  10.3990 + 50, 1336.8778, -2271.4685, 13.1868 + 50, TIMER, CAMERA_MOVE);
        InterpolateCameraPos(playerid,    2045.2523, 1689.3992,  10.3990 + 50, 1336.8778, -2271.4685, 13.1868 + 50, TIMER, CAMERA_MOVE);
        return true;
    }
    if(!strcmp(cmdtext,"/b", true))
    {
        TogglePlayerSpectating(playerid, false);
        SetCameraBehindPlayer(playerid);
        return true;
    }
    return true;
}
public OnPlayerMovedCamera(playerid)
{
    SendClientMessage(playerid, -1,"Arrived.");
    return 1;
}
Reply

Quote:
Originally Posted by DarkScripter
Посмотреть сообщение
OnPlayerMovedCamera - The callback's 0.3e RC2 above Only


pawn Код:
/*
 *
 * OnPlayerMovedCamera - Somente na versгo 0.3e RC2 ou superior.
 *
 *  Crйditos: DarkScripter
 *  - Nгo retire os crйditos.
 *
 *
*/


new bool:Chamed_Int[MAX_PLAYERS char] = false;

forward _FinisOnPlayerMovedCamera(playerid);
forward OnPlayerMovedCamera(playerid);

public _FinisOnPlayerMovedCamera(playerid)
{
    Chamed_Int{playerid} = false;
    CallLocalFunction("OnPlayerMovedCamera", "d", playerid);
}

stock InterpolateCameraLookAt_(p, Float:x, Float:y, Float:z, Float:x2, Float:y2, Float:z2, time, style)
{
    InterpolateCameraLookAt(p, Float:x, Float:y, Float:z, Float:x2, Float:y2, Float:z2, time, style);
    return !Chamed_Int{p} ? (SetTimerEx("_FinisOnPlayerMovedCamera", time, false, "d", p), Chamed_Int{p} = true) : true;
}
stock InterpolateCameraPos_(p, Float:x, Float:y, Float:z, Float:x2, Float:y2, Float:z2, time, style)
{
    InterpolateCameraPos(p, Float:x, Float:y, Float:z, Float:x2, Float:y2, Float:z2, time, style);
    return !Chamed_Int{p} ? (SetTimerEx("_FinisOnPlayerMovedCamera", time, false, "d", p), Chamed_Int{p} = true) : true;
}

#define InterpolateCameraLookAt         InterpolateCameraLookAt_
#define InterpolateCameraPos            InterpolateCameraPos_
How to use(exemple)
pawn Код:
#include <a_samp>

#define TIMER           ( 60000 * 1 ) // time to move cam ( 1 minute  )


public OnPlayerCommandText(playerid, cmdtext[])
{
    if(!strcmp(cmdtext,"/cam", true))
    {
        TogglePlayerSpectating(playerid, true);
        InterpolateCameraLookAt(playerid, 2045.2523, 1689.3992,  10.3990 + 50, 1336.8778, -2271.4685, 13.1868 + 50, TIMER, CAMERA_MOVE);
        InterpolateCameraPos(playerid,    2045.2523, 1689.3992,  10.3990 + 50, 1336.8778, -2271.4685, 13.1868 + 50, TIMER, CAMERA_MOVE);
        return true;
    }
    if(!strcmp(cmdtext,"/b", true))
    {
        TogglePlayerSpectating(playerid, false);
        SetCameraBehindPlayer(playerid);
        return true;
    }
    return true;
}
public OnPlayerMovedCamera(playerid)
{
    SendClientMessage(playerid, -1,"Arrived.");
    return 1;
}
This probably won't be very accurate. You should try OnPlayerUpdate..
Reply

its not much but is my addition..

some marcos to check a vehicle type,
it does not include every model because in my gm all other models will not have a job/objective tied to it.

might be usefull for a RPGM or the likes.,

tested and compiled!


pawn Код:
#define IsASemi(%0)         ((%0) == 403 || (%0) == 514 || (%0) == 515 || (%0) == 443)
#define IsATrailor(%0)      ((%0) == 435 || (%0) == 450 || (%0) == 584 || (%0) == 591 || (%0) == 606 || (%0) == 607 || (%0) == 608 || (%0) == 610 || (%0) == 611)
#define IsACopCar(%0)       ((%0) == 523 || (%0) == 427 || (%0) == 490 || (%0) == 528 || (%0) == 596 || (%0) == 598 || (%0) == 597 || (%0) == 599 || (%0) == 601)
#define IsAMotorBike(%0)    ((%0) == 462 || (%0) == 448 || (%0) == 581 || (%0) == 522 || (%0) == 461 || (%0) == 521 || (%0) == 523 || (%0) == 463 || (%0) == 586 || (%0) == 468)
#define IsABike(%0)         ((%0) == 809 || (%0) == 481 || (%0) == 510)
#define IsAPizzaBike(%0)    ((%0) == 448)
#define IsAFireTruck(%0)    ((%0) == 407 || (%0) == 544)
#define IsATaxi(%0)         ((%0) == 438 || (%0) == 420)
#define IsABus(%0)          ((%0) == 431 || (%0) == 437)
#define IsAAmbulance(%0)    ((%0) == 416)
#define IsALimo(%0)         ((%0) == 409)
#define IsAPlane(%0)        ((%0) == 460 || (%0) == 476 || (%0) == 511 || (%0) == 512 || (%0) == 513 || (%0) == 519 || (%0) == 520 || (%0) == 553 || (%0) == 577 || (%0) == 592 || (%0) == 593)
#define IsAHeli(%0)         ((%0) == 542 || (%0) == 425 || (%0) == 417 || (%0) == 487 || (%0) == 488 || (%0) == 497 || (%0) == 563 || (%0) == 447 || (%0) == 469)
#define IsABoat(%0)         ((%0) == 472 || (%0) == 473 || (%0) == 493 || (%0) == 595 || (%0) == 484 || (%0) == 430 || (%0) == 453 || (%0) == 452 || (%0) == 446 || (%0) == 454)
#define IsATrashTruck(%0)   ((%0) == 408)
usage

pawn Код:
if(IsASemi(vehiclemodelid)){/*code here*/}
if there's a need for a version of this using stocks instead of marcos just ask as i have converted
all these from stocks.
Reply

Creating a "Strike" ability;
I'm aware this may not be the most efficient way to do things, but it works. For the purpose of this tutorial, I allow users to use the Strike ability against tanks only. Soo, let's get started.

Definitions
pawn Код:
#define     MAX_TANKS       10
#define     STRIKE_COOLDOWN     1 //I've defined this as one minute, however, you can change this as needed.

#define     HOLDING(%0) \
                ((newkeys & (%0)) == (%0))
pawn Код:
new
    StrikeCooldown[ MAX_PLAYERS ];
   
new
    TanksSpawned = 0,
    Tank[ MAX_TANKS ];
pawn Код:
//Tanks
    Tank[ 0 ] = AddStaticVehicle( 432, 235.2255,17.2768,2.4297,143.3092, 3, 3 );
    Tank[ 1 ] = AddStaticVehicle( 432, 212.7084,-21.6392,1.4297,94.7918, 3, 3 );
pawn Код:
public      OnPlayerDisconnect( playerid, reason )
{
    if( GetPVarInt( playerid, "ExpTank" ) == 1)
    {
        KillTimer( StrikeCooldown[ playerid ] );
    }
    return true;
}
pawn Код:
public      OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if( HOLDING( KEY_AIM ) )
    {
        if( GetPlayerWeapon( playerid ) == 43 )
        {
            new
                Float: TankLoc[ 3 ];

            for( new tk; tk < MAX_TANKS; tk ++ )
            {
                GetVehiclePos( Tank[ tk ], TankLoc[ 0 ], TankLoc[ 1 ], TankLoc[ 2 ] );
                if( IsPlayerAimingAt( playerid, TankLoc[ 0 ], TankLoc[ 1 ], TankLoc[ 2 ], 3.0 ) )
                {
                    if( GetPVarInt( playerid, "ExpTank" ) == 0 )
                    {
                        SetTimerEx( "ExplodeThatBitch", 5000, false, "ii", tk, playerid );
                        SendClientMessage( playerid, c_ORANGE, "Strike requested! Continue aiming at the Tank!" );
                    }
                    else return SendClientMessage( playerid, c_ORANGE, "You've recently called in an airstrike! Please wait 5 minutes." );
                }
            }
        }
    }
    return true;
}

forward ExplodeThatBitch( TankID, playerid );
public ExplodeThatBitch( TankID, playerid )
{
    new
        Float: TankLoc[ 3 ],
        String[ 128 ],
        p_Name[ MAX_PLAYER_NAME ];

    GetPlayerName( playerid, p_Name, sizeof( p_Name ) );

    for( new tk; tk < MAX_TANKS; tk ++ )
    {
        GetVehiclePos( Tank[ tk ], TankLoc[ 0 ], TankLoc[ 1 ], TankLoc[ 2 ] );
        if( IsPlayerAimingAt( playerid, TankLoc[ 0 ], TankLoc[ 1 ], TankLoc[ 2 ], 3.0 ) )
        {
            CreateExplosion( TankLoc[ 0 ], TankLoc[ 1 ], TankLoc[ 2 ], 0, 10.0 );
           
            format( String, sizeof( String ), "%s has called in a Strike!", p_Name );
            SendClientMessageToAll( c_ORANGE, String );
           
            SetPVarInt( playerid, "ExpTank", 1 );
            StrikeCooldown[ playerid ] = SetTimerEx( "ResetCooldown", 60000*STRIKE_COOLDOWN, false, "i", playerid );
            return true;
        }
        else
        {
            SendClientMessage( playerid, c_ORANGE, "Target not found!" );
            SetPVarInt( playerid, "ExpTank", 0 );
            return true;
        }
    }
    return true;
}

forward ResetCooldown( playerid );
public ResetCooldown( playerid )
{
    SetPVarInt( playerid, "ExpTank", 0 );
    SendClientMessage( playerid, c_ORANGE, "You may now use your Strike capabilities!" );
    return true;
}
pawn Код:
Float:DistanceCameraTargetToLocation(Float:CamX, Float:CamY, Float:CamZ,  Float:ObjX, Float:ObjY, Float:ObjZ,  Float:FrX, Float:FrY, Float:FrZ) {

    new Float:TGTDistance;
   
    TGTDistance = floatsqroot((CamX - ObjX) * (CamX - ObjX) + (CamY - ObjY) * (CamY - ObjY) + (CamZ - ObjZ) * (CamZ - ObjZ));

    new Float:tmpX, Float:tmpY, Float:tmpZ;

    tmpX = FrX * TGTDistance + CamX;
    tmpY = FrY * TGTDistance + CamY;
    tmpZ = FrZ * TGTDistance + CamZ;

    return floatsqroot((tmpX - ObjX) * (tmpX - ObjX) + (tmpY - ObjY) * (tmpY - ObjY) + (tmpZ - ObjZ) * (tmpZ - ObjZ));
}
stock IsPlayerAimingAt(playerid, Float:x, Float:y, Float:z, Float:radius)
{
    new Float:cx,Float:cy,Float:cz,Float:fx,Float:fy,Float:fz;
    GetPlayerCameraPos(playerid, cx, cy, cz);
    GetPlayerCameraFrontVector(playerid, fx, fy, fz);
    return (radius >= DistanceCameraTargetToLocation(cx, cy, cz, x, y, z, fx, fy, fz));
}

//These are NOT my functions, although I'm not positive where they came from..
I know I really haven't explained anything, but this is why it's in the snippets thread, rather than a tutorial.
Reply

This one gives you 50 Health and 50 Armour
pawn Код:
stock FiftyFifty(playerid)
{
    SetPlayerHealth(playerid,50)
    SetPlayerArmour(playerid,50.0);
    return 1;
}
Reply

Best "snippet" I've ever seen! :trolling
Reply

Quote:
Originally Posted by STMatt
Посмотреть сообщение
Best "snippet" I've ever seen! :trolling
Who you talking to?


If your talking to me: This is a topic where you post snippets, not insult people.
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
What are you on about? Saying something is the best you've ever seen is a complement, not an insult!
Sarcasm much?
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)