[Include] Simple AI Police Officer NPC using RNPC
#1

This is a script i developed for testing and tutorial purposes.
Basically its a ai script for police officers. You can create police npcs and assign them a set of waypoints, where they are walking continuosly. If a player near a police officer fires a weapon, the officer chases him, and fires at him if he is close enough. If the player dies, or runs too far away, the officer stops chasing him and continues his waypoint route.
The script contains quite a lot comments, especially in the RNPC relevant part, and is even easily extensible if you want the npcs to do more (like a third range level where the player is arrested, other conditions that start the chase, or whatever).

The script of course needs the RNPC plugin.
Then include the file, and create a police officer like this, all the rest is done automatically. Values like the attack or vision range can be modified through the defines at the beginning. See the include code for other provided functions, like removing the npcs.
pawn Код:
new rp = CreateRNPCPolice("Officerino", 280);
AddRPOLWaypoint(rp, 2029.42, 1354.34, 10.5);
AddRPOLWaypoint(rp, 2033.55, 1286.21, 10.5);
AddRPOLWaypoint(rp, 2079.03, 1286.00, 10.5);
AddRPOLWaypoint(rp, 2079.02, 1362.08, 10.5);
StartRPOLRoute(rp);
Dont just copy&paste that code to OnGameModeInit() or main(), this wont work due to RNPC limitations. Instead execute this code delayed, with a timer or a command.

So heres the link to the script

And heres a short video showing an RNPC officer in action:
[ame]http://www.youtube.com/watch?v=mM4xCOvavZQ[/ame]

Hope this helps some guys, either in understanding RNPC, AI scripts in general, or with the offered police services itself
Reply
#2

Nice example! i cant wait to do the same with vehicles in the future
Reply
#3

omg ill try it wjen i get home good job
Reply
#4

amazing!
Reply
#5

Can they detect wanted players?

And only shoot wanted players?

And can they support other weapons?
Reply
#6

Haha, this is cool.
Good job.
Reply
#7

Quote:
Originally Posted by lordturhan
Посмотреть сообщение
Can they detect wanted players?

And only shoot wanted players?

And can they support other weapons?
Wanted players can be scripted manualy.

Yes.

Yes.
Reply
#8

Quote:
Originally Posted by lordturhan
Посмотреть сообщение
Can they detect wanted players?

And only shoot wanted players?

And can they support other weapons?
Yep, as Archer already said. AIs can do whatever you like, this one is preconfigured to make it quite easy, just take a look at the script.
You can check the wanted level of players and their distance in the WatchTimer, same with shooting. Changing the weapon is a simple number, just keep an eye open for this:
pawn Код:
// Set weapon
RNPC_SetWeaponID(22);
Reply
#9

OMG!Amazing!!Can you kill the cop?
Reply
#10

Quote:
Originally Posted by Georgi166
Посмотреть сообщение
OMG!Amazing!!Can you kill the cop?
Killing the npcs requires an own hitdetection system, normally this isnt possible. There are things like OnPlayerShootPlayer, or something using GetPlayerTargetPlayer. I quite sure wont add hitdetection to RNPC, as I got my own system in sutd that wont be released, and i dont like rewriting something just for the release
Reply
#11

Good working Mauzen !!
I am creating a system of zombies, advise the use of RNPC !!

Kill NPC
pawn Code:
// top of gm
new tirosLevados[MAX_PLAYERS];

// onplayer update
static targetplayer,  Keys,ud,lr;
GetPlayerKeys(playerid,Keys,ud,lr);
targetplayer = GetPlayerTargetPlayer(playerid);

if(targetplayer != INVALID_PLAYER_ID && Keys == KEY_FIRE || Keys == 132)
{
    if(IsPlayerNPC(targetplayer)) {
        tirosLevados[targetplayer]++;
        SetPVarInt(playerid, "timesTick", GetTickCount());
        if(tirosLevados[targetplayer] > 20) { // 20 shoots +-
            SpawnPlayer(targetplayer);
            OnPlayerDeath(targetplayer, playerid, GetPlayerWeapon(playerid));
            tirosLevados[targetplayer] = 0;
          }
    }
}


Follow
pawn Code:
CHK_RowMoviment(n, moviment) {

    if(moviment > rowNPCMove[n]) {
        return false;
    }
   
    static
        rowSTR[3][32];

    format(rowSTR[0], 32, "rowNPC%dX", moviment);
    format(rowSTR[1], 32, "rowNPC%dY", moviment);
    format(rowSTR[2], 32, "rowNPC%dZ", moviment);

    return IsPlayerInRangeOfPoint(n, 1.5, GetPVarFloat(n, rowSTR[0]),GetPVarFloat(n, rowSTR[1]),GetPVarFloat(n, rowSTR[2]));
}

EXE_RowMoviment(n) {

    if(!rowNPCMove[n] || execROWMoviment[n] > (rowNPCMove[n] - 1))  {
        return false;
    }

    if(execROWMoviment[n]) {
        if(!CHK_RowMoviment(n, execROWMoviment[n]-1)) {
            return false;
        }
    }

   
    static
        rowSTR[4][32];


    if(execROWMoviment[n] > 1) {
        for(new i = execROWMoviment[n]-1; i > -1; i--) {
            format(rowSTR[0], 32, "rowNPC%dX", i);
            format(rowSTR[1], 32, "rowNPC%dY", i);
            format(rowSTR[2], 32, "rowNPC%dZ", i);
            format(rowSTR[3], 32, "rowNPC%dS", i);

            DeletePVar(n,rowSTR[0]);
            DeletePVar(n,rowSTR[1]);
            DeletePVar(n,rowSTR[2]);
            DeletePVar(n,rowSTR[3]);
        }
    }
   
    format(rowSTR[0], 32, "rowNPC%dX", execROWMoviment[n]);
    format(rowSTR[1], 32, "rowNPC%dY", execROWMoviment[n]);
    format(rowSTR[2], 32, "rowNPC%dZ", execROWMoviment[n]);
    format(rowSTR[3], 32, "rowNPC%dS", execROWMoviment[n]);
   
    MoveRNPC(n, GetPVarFloat(n, rowSTR[0]),GetPVarFloat(n, rowSTR[1]),GetPVarFloat(n, rowSTR[2]),GetPVarFloat(n, rowSTR[3]));

    return execROWMoviment[n]++;
}

ADD_RowMoviment(n, Float:x, Float:y, Float:z, Float:s) {

    static str[32];

    format(str, 32, "rowNPC%dX", rowNPCMove[n]);
    SetPVarFloat(n, str, x);
   
    format(str, 32, "rowNPC%dY", rowNPCMove[n]);
    SetPVarFloat(n, str, y);
   
    format(str, 32, "rowNPC%dZ", rowNPCMove[n]);
    SetPVarFloat(n, str, z);
   
    format(str, 32, "rowNPC%dS", rowNPCMove[n]);
    SetPVarFloat(n, str, s);

    return rowNPCMove[n]++;
}
Now use:
pawn Code:
ADD_RowMoviment(npcid, x-0.1, y-0.1, z,0.006);
// MoveRNPC(npcid, x-0.1, y-0.1, z,0.006);
EXE_RowMoviment(npcid);
pawn Code:
new execROWMoviment[MAX_PLAYERS];
new rowNPCMove[MAX_PLAYERS];
This follows exactly where the player walks. It can also be optimized ..

- So the NPC does not fly on the wall, or walk inside them or fly into holes
Reply
#12

@[FeK]DraKiNs

what about calling "OnPlayerShootPlayer" when a player get shoot another one? Should be easy using CallLocalFunction
Reply
#13

you need to make a health variable and have a list of weapon offsets to decrease health etc, it'd be much better if mauzen scripted a OnPlayerShootNPC public, because he is a math freak! (in a good way!)
Reply
#14

Quote:
Originally Posted by Lorenc_
View Post
you need to make a health variable and have a list of weapon offsets to decrease health etc, it'd be much better if mauzen scripted a OnPlayerShootNPC public, because he is a math freak! (in a good way!)
Like this
Reply
#15

nice dude...
Reply
#16

How do I add AI police in the script.
Reply
#17

Quote:
Originally Posted by qq757679895
View Post
How do I add AI police in the script.
CreateRNPCPolice(Name[], Skin);
Reply
#18

CreateRNPCPolice(Name[], Skin); added where
Reply
#19

Quote:
Originally Posted by qq757679895
View Post
CreateRNPCPolice(Name[], Skin); added where
Wherever you want to add the function. For example if you want to add a cop when the server starts up Just put on

pawn Code:
public OnGameModeInit()
pawn Code:
CreateRNPCPolice("Myname" 296);
Reply
#20

In the end how to build an AI police,I did not create a successful.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)