[Tutorial] Idle NPC's reacting to being aimed at and shot.
#1

Before we start with this tutorial; We're going to need the following Include:
https://sampforum.blast.hk/showthread.php?tid=105568 - (a_Angles)

Okay, Now that is sorted let's continue with the tutorial.

Things to define:
pawn Code:
#define PRESSED(%0) \
    (((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))
This is defining keys pressed as we are going to be using OnPlayerKeyStateChange.

We will use the following stock to gain the players ID from their name: (I did not write this)
pawn Code:
stock GetPlayerIDFromName(const playername[], partofname=0)
{
    new i;
    new playername1[64];
    for (i=0;i<MAX_PLAYERS;i++)
    {
        if (IsPlayerConnected(i))
        {
            GetPlayerName(i,playername1,sizeof(playername1));
            if (strcmp(playername1,playername,true)==0)
            {
                return i;
            }
        }
    }
    new correctsigns_userid=-1;
    new tmpuname[128];
    new hasmultiple=-1;
    if(partofname)
    {
        for (i=0;i<MAX_PLAYERS;i++)
        {
            if (IsPlayerConnected(i))
            {
                GetPlayerName(i,tmpuname,sizeof(tmpuname));

                if(!strfind(tmpuname,playername1[partofname],true, 0))
                {
                    hasmultiple++;
                    correctsigns_userid=i;
                }
                if (hasmultiple>0)
                {
                    return -2;
                }
            }
        }
    }
    return correctsigns_userid;
}
Next go down to OnPlayerKeyStateChange and create the two keys we're going to be using, The Shooting key and the aiming key:
pawn Code:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(PRESSED(KEY_FIRE)) //Checks if the player is using the fire gun key.
    {
    }
    if(PRESSED(KEY_HANDBRAKE)) //checks if the player is using the aim guy key.
    {
    }
    return 1;
}
Next we're going to define and get some infomation we need, Place this in OnPlayerKeyStateChange:
pawn Code:
new NPCID = GetPlayerIDFromName("NPC"); //Gets the NPC's ID and puts it into something easily usable.
//Note: Replace 'NPC' with your NPC's name.
new wid = GetPlayerWeapon(playerid); //Gets the players weapon which is going to be used to aim and shoot.
new Float:x,Float:y,Float:z; //Defines X,Y,Z
GetPlayerPos(NPCID,x,y,z); //Gets the NPC's Position.
Now we're going to see if we are close enough to the NPC for it to react using IsPlayerInRangeOfPoint:
pawn Code:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    new NPCID = GetPlayerIDFromName("NPC");
    new wid = GetPlayerWeapon(playerid);
    new Float:x,Float:y,Float:z;
    GetPlayerPos(NPCID,x,y,z);
    if(IsPlayerInRangeOfPoint(playerid,15,x,y,z)) //Checking if you're near the NPC
    {
        if(PRESSED(KEY_FIRE))
        {
        }
        if(PRESSED(KEY_HANDBRAKE))
        {
        }
    }
    return 1;
}
Time to check if your weapon is a gun:
pawn Code:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    new NPCID = GetPlayerIDFromName("NPC");
    new wid = GetPlayerWeapon(playerid);
    new Float:x,Float:y,Float:z;
    GetPlayerPos(NPCID,x,y,z);
    if(IsPlayerInRangeOfPoint(playerid,15,x,y,z))
    {
        if(PRESSED(KEY_FIRE))
        {
            if(wid > 10) //Is the weapon you're using a gun?
            {
            }
        }
        if(PRESSED(KEY_HANDBRAKE))
        {
            if(wid > 10) //Is the weapon you're using a gun?
            {
            }
        }
    }
    return 1;
}
Now it's time to check you are facing the NPC. (This is where the include comes in...)
pawn Code:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    new NPCID = GetPlayerIDFromName("NPC");
    new wid = GetPlayerWeapon(playerid);
    new Float:x,Float:y,Float:z;
    GetPlayerPos(NPCID,x,y,z);
    if(IsPlayerInRangeOfPoint(playerid,15,x,y,z))
    {
        if(PRESSED(KEY_FIRE))
        {
            if(wid > 10)
            {
                if(IsPlayerFacingPlayer(playerid,NPCID,10) == 1) //Self explanitory
                {
                }
                else if(IsPlayerFacingPlayer(playerid,NPCID,10) == 0)//Self explanitory
                {
                }
            }
        }
        if(PRESSED(KEY_HANDBRAKE))
        {
            if(wid > 10)
            {
                if(IsPlayerFacingPlayer(playerid,NPCID,10) == 1)//Self explanitory
                {
                }
                else if(IsPlayerFacingPlayer(playerid,NPCID,10) == 0)//Self explanitory
                {
                }
            }
        }
    }
    return 1;
}
Now we're going to add the reactions to the aiming and shooting:
pawn Code:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    new NPCID = GetPlayerIDFromName("NPC");
    new wid = GetPlayerWeapon(playerid);
    new Float:x,Float:y,Float:z;
    GetPlayerPos(NPCID,x,y,z);
    if(IsPlayerInRangeOfPoint(playerid,15,x,y,z))
    {
        if(PRESSED(KEY_FIRE))
        {
            if(wid > 10)
            {
                if(IsPlayerFacingPlayer(playerid,NPCID,10) == 1)
                {
                    ApplyAnimation(NPCID,"CRACK","crckdeth4",3.0,0,0,0,1,0);
                    SetPlayerChatBubble(NPCID,"Ouch!",0xFFFFFFAA,50,5000);
                }
                else if(IsPlayerFacingPlayer(playerid,NPCID,10) == 0)
                {
                    SetPlayerChatBubble(NPCID,"Don't Shoot near me!",0xFFFFFFAA,50,5000);
                }
            }
        }
        if(PRESSED(KEY_HANDBRAKE))
        {
            if(wid > 10)
            {
                if(IsPlayerFacingPlayer(playerid,NPCID,10) == 1)
                {
                    ApplyAnimation(NPCID,"ped","handsup",3.0,0,0,0,1,0);
                    SetPlayerChatBubble(NPCID,"Please Don't Shoot me!",0xFFFFFFAA,50,5000);
                }
                else if(IsPlayerFacingPlayer(playerid,NPCID,10) == 0)
                {
                    SetPlayerChatBubble(NPCID,"Watch where you are aiming that gun!",0xFFFFFFAA,50,5000);
                }
            }
        }
    }
    return 1;
}
Done!

______________________________________Notes and other things:______________________________________
Idle NPC Definition: One without a recorded walking/driving path.
Reply
#2

Nice tutorial.
Thanks
Reply
#3

i like it good work
Reply
#4

Oh, nice job!
Reply
#5

Quote:
Originally Posted by oттo
View Post
Nice tutorial.
Thanks
Quote:
Originally Posted by vyper
View Post
i like it good work
Quote:
Originally Posted by playbox12
View Post
Oh, nice job!
Thanks guys. :P
Reply
#6

nice tut dude
Reply
#7

Really nice job!
Reply
#8

Great work.
Reply
#9

Can u also make a tutorial, the other way around ?
An NPC can aim at a player and shot at a player with specificied weapon ?
/aim [NPCNAME/ID] [playerid] [WeaponID]
and
/shot [NPCNAME/ID] [playerid] [WeaponID].

That the SetPlayerFacingAngle for NPC resets so fast, that the NPC always is aiming at the player.

MfG
Me
Reply
#10

can we use GetPlayerTargetPlayer instead of that include? Because I can't download that include, the link is not available in my country.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)