SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Reply
 
Thread Tools Display Modes
Old 12/09/2010, 10:22 AM   #1
miokie
High-roller
 
miokie's Avatar
 
Join Date: Jan 2007
Location: Bedfordshire, England
Posts: 1,148
Reputation: 13
Default Idle NPC's reacting to being aimed at and shot.

Before we start with this tutorial; We're going to need the following Include:
http://forum.sa-mp.com/showthread.php?t=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.
__________________

Last edited by miokie; 12/09/2010 at 07:40 PM.
miokie is offline   Reply With Quote
Old 12/09/2010, 10:29 AM   #2
Dime
Huge Clucker
 
Join Date: Jul 2010
Posts: 257
Reputation: 1
Default Re: Idle NPC's reacting to being aimed at and shot.

Nice tutorial.
Thanks
Dime is offline   Reply With Quote
Old 12/09/2010, 12:47 PM   #3
vyper
Huge Clucker
 
vyper's Avatar
 
Join Date: Jul 2009
Location: Hell. x_x
Posts: 276
Reputation: 68
Default Re: Idle NPC's reacting to being aimed at and shot.

i like it good work
vyper is offline   Reply With Quote
Old 13/09/2010, 10:44 AM   #4
playbox12
High-roller
 
playbox12's Avatar
 
Join Date: Feb 2010
Location: Netherlands
Posts: 1,318
Reputation: 216
Default Re: Idle NPC's reacting to being aimed at and shot.

Oh, nice job!
playbox12 is offline   Reply With Quote
Old 13/09/2010, 05:21 PM   #5
miokie
High-roller
 
miokie's Avatar
 
Join Date: Jan 2007
Location: Bedfordshire, England
Posts: 1,148
Reputation: 13
Default Re: Idle NPC's reacting to being aimed at and shot.

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
__________________
miokie is offline   Reply With Quote
Old 13/09/2010, 09:46 PM   #6
[MNC]Azz
Big Clucker
 
[MNC]Azz's Avatar
 
Join Date: Jul 2010
Location: Under a bridge
Posts: 159
Reputation: 1
Default Re: Idle NPC's reacting to being aimed at and shot.

nice tut dude
__________________
[MNC]Azz is offline   Reply With Quote
Old 07/10/2010, 01:10 PM   #7
jameskmonger
High-roller
 
jameskmonger's Avatar
 
Join Date: Nov 2009
Location: London, England
Posts: 1,113
Reputation: 76
Default Re: Idle NPC's reacting to being aimed at and shot.

Really nice job!
jameskmonger is offline   Reply With Quote
Old 11/10/2010, 08:46 AM   #8
HyperZ
High-roller
 
HyperZ's Avatar
 
Join Date: Aug 2009
Posts: 1,340
Reputation: 66
Default Re: Idle NPC's reacting to being aimed at and shot.

Great work.
__________________
HyperZ is offline   Reply With Quote
Old 10/05/2012, 07:29 PM   #9
AttachObject(Oo)
Little Clucker
 
Join Date: Sep 2011
Posts: 20
Reputation: 0
Default AW: Idle NPC's reacting to being aimed at and shot.

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
AttachObject(Oo) is offline   Reply With Quote
Old 26/12/2014, 06:03 AM   #10
ATGOggy
Gangsta
 
Join Date: Sep 2014
Location: Ls-rcr
Posts: 850
Reputation: 52
Default Re: Idle NPC's reacting to being aimed at and shot.

can we use GetPlayerTargetPlayer instead of that include? Because I can't download that include, the link is not available in my country.
__________________
******: "I've said this before. People think that MySQL is some simple plug-and-play magic bullet that you can use to just make your mode instantly better. They don't realise that the "L" in "SQL" means "language". So instead of programming PAWN, and using a bit of this mystery thing, you are now programming in two languages at once. If you spend months learning PAWN, you should spend the same time learning SQL, if not more - it is probably the more complex of the two languages, and not something that just gets bolted on to the side."


I give and want only fair reps. I hate this: "hey bro, I rep you, rep me plz

Click below if you wanna be amazed:
http://Paid2Refer.com/ref.php?refId=291262
ATGOggy 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
Detect if player has Shot someone else (And been Shot)? DarrenReeder Help Archive 9 04/04/2010 06:37 PM
Running a server idle? FrostBytez Server Support 2 03/01/2010 06:31 AM
NPC help with my idle timer BLAbla93 Help Archive 0 22/10/2009 06:56 AM
Detecting if a person is idle. Abernethy Help Archive 5 05/09/2009 12:51 PM


All times are GMT. The time now is 12:18 PM.


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