SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts > Includes

Reply
 
Thread Tools Display Modes
Old 23/01/2019, 03:00 PM   #1
Lokii
Huge Clucker
 
Join Date: Sep 2017
Posts: 319
Reputation: 81
Default Cops AI


COPS

current version (v1.0)

This include creates cop ai, they will kill anyone who is wanted, they can detect crimes like if player shoot, if player damage other player or a cop, if player killed someone, however for now they only follow wanted players otherwise they just stand, and they cant drive (will add later), no melee support yet.

How to include:

Code:
#define FILTERSCRIPT 
#include <a_samp>
#include <fcnpc>
#include <cops> //requires fcnpc 2.0.0
Crime types:

Code:
CRIME_KILL 0 //called only if you kill players, for cops use oncopdeath
CRIME_SHOOT 1 
CRIME_DAMAGE 2 //called only if you shoot players, for cops use oncoptakedamage
Functions:

Code:
native CreateCop(skinid, name[], Float:detection_area, Float:health, Float:x, Float:y, Float:z, Float:angle, weapon, Float:accuracy);
native IsCopDead(cop_id);
native IsCopInvulnerable(cop_id);
native IsCopMoving(cop_id);
native IsCopValid(cop_id);
native GetCopHealth(cop_id);
native GetCopTarget(cop_id);
native GetClosestPlayerToCop(cop_id);
native GetCopDetectionArea(cop_id);
native GetCopPos(cop_id, &Float:x, &Float:y, &Float:z);
native GetCopAngle(cop_id, &Float:angle);
native GetCopCreationPos(cop_id, &Float:x, &Float:y, &Float:z);
native GetCopCreationAngle(cop_id, &Float:angle);
native GetCopInterior(cop_id);
native GetCopVirtualWorld(cop_id);
native GetCopSkin(cop_id);
native GetCopCustomSkin(cop_id);
native GetCopWeapon(cop_id);
native SetCopWeapon(cop_id, weaponid);
native SetCopInvulnerable(cop_id, bool:invulnerable);
native SetCopSkin(cop_id, skinid);
native SetCopInterior(cop_id, interiorid);
native SetCopVirtualWorld(cop_id, worldid);
native SetCopHealth(cop_id, Float:health);
native RespawnCop(cop_id);
native DestroyCop(cop_id);
Callbacks:

Code:
OnCopDeath(cop_id, killerid, weaponid);
OnCopTakeDamage(cop_id, damagerid, weaponid, bodypart, Float:health_loss);
OnCopKillPlayer(cop_id, playerid);
OnCrimeHappenNearCop(playerid, crime);
Example:

PHP Code:
#define FILTERSCRIPT
#include <a_samp>
#include <fcnpc>
#include <cops>
#include <zcmd>

new cop;

CMD:cop(playerid)
{
    new 
Float:xFloat:yFloat:z;
    if(!
IsCopValid(cop)) //you could do without the check as well the include checks if cop allready exist
    
{
        
GetPlayerPos(playeridxyz);
        
cop CreateCop(284"Cop01"45.0100.0x+4yz, -1221.0);
    }
    return 
1;
}

CMD:dcop(playerid)
{
    if(
IsCopValid(cop)) //could do without the check as well
    
{
        
DestroyCop(cop);
    }
    return 
1;
}

public 
OnCrimeHappenNearCop(playeridcrime)
{
    switch(
crime)
    {
        case 
CRIME_KILL:
        {
            if(
GetPlayerWantedLevel(playerid) < 5)
            {
                
SetPlayerWantedLevel(playerid5);
            }
        }
        case 
CRIME_SHOOT:
        {
            if(
GetPlayerWantedLevel(playerid) < 2)
            {
                
SetPlayerWantedLevel(playerid2);
            }
        }
        case 
CRIME_DAMAGE:
        {
            if(
GetPlayerWantedLevel(playerid) < 3)
            {
                
SetPlayerWantedLevel(playerid3);
            }
        }
    }
    return 
1;
}

public 
OnCopKillPlayer(cop_idplayerid)
{
    if(
GetPlayerWantedLevel(playerid) > 1)
    {
        
SetPlayerWantedLevel(playerid0);
    }
    return 
1;

Video: Click here

Download: Click here

requires:

fcnpc 2.0.0


Bugs:

If you find any bugs please report them and i will fix them in the next update!


Credits:

SA-MP team - a_samp
ZIGGI - fcnpc
Lokii - cops
Pottus - colandreas & optimizing
Crayder - colandreas
Attached Files
File Type: zip Cops.zip (3.0 KB, 2 views)

Last edited by Lokii; 28/03/2019 at 08:49 PM.
Lokii is offline   Reply With Quote
Old 23/01/2019, 03:34 PM   #2
Pottus
High-roller
 
Pottus's Avatar
 
Join Date: Jun 2012
Posts: 4,855
Reputation: 1291
Default Re: Cops

Dont have time to look it over but IsValidCop has no array bounds checking
Pottus is offline   Reply With Quote
Old 23/01/2019, 03:51 PM   #3
Lokii
Huge Clucker
 
Join Date: Sep 2017
Posts: 319
Reputation: 81
Default Re: Cops

Quote:
Originally Posted by Pottus View Post
Dont have time to look it over but IsValidCop has no array bounds checking
Updated.
Lokii is offline   Reply With Quote
Old 23/01/2019, 06:27 PM   #4
Hazon
Big Clucker
 
Hazon's Avatar
 
Join Date: May 2018
Location: Who cares ?
Posts: 88
Reputation: 9
Default Re: Cops

Nice idea.
Hazon is offline   Reply With Quote
Old 23/01/2019, 06:45 PM   #5
Lokii
Huge Clucker
 
Join Date: Sep 2017
Posts: 319
Reputation: 81
Default Re: Cops

Quote:
Originally Posted by Hazon View Post
Nice idea.
Thanks
Lokii is offline   Reply With Quote
Old 23/01/2019, 07:27 PM   #6
Lucky13
Huge Clucker
 
Lucky13's Avatar
 
Join Date: Dec 2015
Location: Romania
Posts: 288
Reputation: 68
Default Re: Cops

Good job!
Lucky13 is offline   Reply With Quote
Old 23/01/2019, 07:28 PM   #7
Lokii
Huge Clucker
 
Join Date: Sep 2017
Posts: 319
Reputation: 81
Default Re: Cops

Quote:
Originally Posted by Lucky13 View Post
Good job!
Thx
Lokii is offline   Reply With Quote
Old 23/01/2019, 08:24 PM   #8
N0FeaR
High-roller
 
N0FeaR's Avatar
 
Join Date: Nov 2011
Location: Sweden
Posts: 2,290
Reputation: 161
Default Re: Cops

Good job i like this!
__________________
https://discord.gg/HyQQ8NV
N0FeaR is offline   Reply With Quote
Old 23/01/2019, 08:27 PM   #9
Lokii
Huge Clucker
 
Join Date: Sep 2017
Posts: 319
Reputation: 81
Default Re: Cops

Quote:
Originally Posted by N0FeaR View Post
Good job i like this!
Thank you
Lokii is offline   Reply With Quote
Old 24/01/2019, 06:05 PM   #10
Pottus
High-roller
 
Pottus's Avatar
 
Join Date: Jun 2012
Posts: 4,855
Reputation: 1291
Default Re: Cops

Okay here we go.

Should be static to maintain encapsulation. Shouldn't be able to be modified outside of include unless calling functions directly.
Code:
new e_cops[MAX_COPS][ee_cops];
IsCopValid() should return 1 if successful. There are other functions with this problem as well.
Code:
stock GetCopCreationPos(cop_id, &Float:x, &Float:y, &Float:z)
{
    if(IsCopValid(cop_id))
    {
        x = e_cops[cop_id][cop_pos_x];
        y = e_cops[cop_id][cop_pos_y];
        z = e_cops[cop_id][cop_pos_z];
    }
    return 0;
}
Why bother calling so many functions before checking if the NPC is actually valid? Check if the NPC is valid first then create.

Code:
CreateCop()[
......
	new id;
    for(new i = 0; i < MAX_COPS; i++)
    {
        if(IsCopValid(i)) continue;
        
        id = FCNPC_Create(name);
        
        if(FCNPC_IsValid(e_cops[i][copid]))
        {
	        e_cops[i][copid] = id;
	        FCNPC_Spawn(e_cops[i][copid], skinid, x, y, z);
	        FCNPC_SetHealth(e_cops[i][copid], health);
	        FCNPC_SetAngle(e_cops[i][copid], angle);
	        FCNPC_SetWeaponAccuracy(e_cops[i][copid], weapon, accuracy);
	        FCNPC_SetWeapon(e_cops[i][copid], weapon);
	        FCNPC_UseInfiniteAmmo(e_cops[i][copid], true);
	        e_cops[i][cop_pos_x] = x;
	        e_cops[i][cop_pos_y] = y;
	        e_cops[i][cop_pos_z] = z;
	        e_cops[i][cop_angle] = angle;
	        e_cops[i][cop_detection] = detection_area;
	        e_cops[i][cop_target] = 0xFFFF;
	        e_cops[i][cop_exist] = true;
			return i;
		}
    }
    return 0xFFFF;
Missing array bounds checking.
Code:
GetCopTarget(cop_id)
{
    return e_cops[cop_id][cop_target];
}
Why are you calling this function twice?
Code:
            if(GetPlayerDistanceFromPoint(i, x, y, z) >= last) continue;
            last = GetPlayerDistanceFromPoint(i, x, y, z);
Code:
new Float:tmp;
for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++)
{
    if(!IsPlayerConnected(i) || IsPlayerNPC(i)) continue;
	tmp = GetPlayerDistanceFromPoint(i, x, y, z);
	if(tmp >= last) continue;
	last = tmp;
    id = i;
}
Major fuck-up here.
Code:
public FCNPC_OnUpdate(npcid)
{
    new Float:x, Float:y, Float:z;
    for(new k = 0; k < MAX_COPS; k++)
    {
I don't even see npcid used at all, you are basically using this callback like a timer! That means you are doing as many times more work as the number of cops that are spawned!

Why bother doing anything until you check if the killerid is an NPC?
Code:
public OnPlayerDeath(playerid, killerid, reason)

Last edited by Pottus; 24/01/2019 at 06:39 PM.
Pottus 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
Near Cops STONEGOLD Scripting Help 2 17/02/2017 08:51 PM
[Ajuda] Cops Poderem pegar carros de outros Cops Vitor_Marfioso Português/Portuguese 16 12/01/2012 01:54 PM
[Ajuda] COPS pegarem carros de outros COPS Vitor_Marfioso Português/Portuguese 2 06/01/2012 08:22 PM
COPS Striker_Moe Everything and Nothing 2 24/12/2009 01:20 PM
cops on my GM cruising Help Archive 13 16/12/2009 02:46 PM


All times are GMT. The time now is 05:56 PM.


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