Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //------------------------------------------------------------------------------
- ////////////////////////////////////////////////////
- // Player BodyGuard Include //
- // Author: Slade //
- // Credits: OrMisicL (FCNPC plugin) //
- ////////////////////////////////////////////////////
- //------------------------------------------------------------------------------
- #if defined _BGuard_included
- #endinput
- #endif
- #define _BGuard_included
- #if !defined _samp_included
- #tryinclude <a_samp>
- #if !defined _samp_included
- #error could not locate a_samp.inc file, please check your server includes
- #endif
- #endif
- #define BGuard:: BGuard_
- //------------------------------------------------------------------------------
- enum BGuard::En {
- BGuard::NPC_ID,
- BGuard::NPC_Stay,
- BGuard::NPC_SFire
- }
- new BGuard::Data[MAX_PLAYERS][BGuard::En];
- //------------------------------------------------------------------------------
- /*
- native BGuard::ToggleFollow( playerid, bool:toggle );
- native BGuard::DestroyBodyGuard(playerid);
- native BGuard::CreateBodyGuard(playerid, skin, weap);
- native BGuard::AttackPlayer(playerid, victim);
- native BGuard::StopAttack(playerid);
- */
- //------------------------------------------------------------------------------
- #if defined OnPlayerLoseBodyGuard
- forward OnPlayerLoseBodyGuard(playerid, ltype);
- #endif
- //---------------------------------[ HOOKS ]------------------------------------
- public OnPlayerConnect(playerid) {
- BGuard::Data[playerid][BGuard::NPC_ID] = -1;
- #if defined BGuard_OnPlayerConnect
- BGuard_OnPlayerConnect(playerid);
- #endif
- return 1;
- }
- #if defined _ALS_OnPlayerConnect
- #undef OnPlayerConnect
- #else
- #define _ALS_OnPlayerConnect
- #endif
- #define OnPlayerConnect BGuard_OnPlayerConnect
- #if defined BGuard_OnPlayerConnect
- forward BGuard_OnPlayerConnect(playerid);
- #endif
- public OnPlayerDisconnect(playerid, reason) {
- if(BGuard::Data[playerid][BGuard::NPC_ID] != -1) {
- FCNPC_Destroy(BGuard::Data[playerid][BGuard::NPC_ID]);
- #if defined OnPlayerLoseBodyGuard
- OnPlayerLoseBodyGuard(playerid, 4);
- #endif
- BGuard::Data[playerid][BGuard::NPC_ID] = -1;
- }
- #if defined BGuard_OnPlayerDisconnect
- BGuard_OnPlayerDisconnect(playerid, reason);
- #endif
- return 1;
- }
- #if defined _ALS_OnPlayerDisconnect
- #undef OnPlayerDisconnect
- #else
- #define _ALS_OnPlayerDisconnect
- #endif
- #define OnPlayerDisconnect BGuard_OnPlayerDisconnect
- #if defined BGuard_OnPlayerDisconnect
- forward BGuard_OnPlayerDisconnect(playerid, reason);
- #endif
- public OnPlayerDeath(playerid, killerid, reason) {
- if(BGuard::Data[playerid][BGuard::NPC_ID] != -1) {
- FCNPC_Destroy(BGuard::Data[playerid][BGuard::NPC_ID]);
- #if defined OnPlayerLoseBodyGuard
- OnPlayerLoseBodyGuard(playerid, 4);
- #endif
- BGuard::Data[playerid][BGuard::NPC_ID] = -1;
- }
- #if defined BGuard_OnPlayerDeath
- BGuard_OnPlayerDeath(playerid, killerid, reason);
- #endif
- return 1;
- }
- #if defined _ALS_OnPlayerDeath
- #undef OnPlayerDeath
- #else
- #define _ALS_OnPlayerDeath
- #endif
- #define OnPlayerDeath BGuard_OnPlayerDeath
- #if defined BGuard_OnPlayerDeath
- forward BGuard_OnPlayerDeath(playerid, killerid, reason);
- #endif
- public OnPlayerStateChange(playerid, newstate, oldstate) {
- if(newstate == PLAYER_STATE_DRIVER)
- {
- if(BGuard::Data[playerid][BGuard::NPC_ID] != -1 && FCNPC_IsValid(BGuard::Data[playerid][BGuard::NPC_ID])) {
- if(FCNPC_GetVehicleID(BGuard::Data[playerid][BGuard::NPC_ID]) != GetPlayerVehicleID(playerid)) {
- if(BGuard::GetFreeSeat(GetPlayerVehicleID(playerid)) != -1) {
- FCNPC_PutInVehicle(BGuard::Data[playerid][BGuard::NPC_ID], GetPlayerVehicleID(playerid), BGuard::GetFreeSeat(GetPlayerVehicleID(playerid)));
- }
- }
- }
- }
- #if defined BGuard_OnPlayerStateChange
- BGuard_OnPlayerStateChange(playerid, newstate, oldstate);
- #endif
- return 1;
- }
- #if defined _ALS_OnPlayerStateChange
- #undef OnPlayerStateChange
- #else
- #define _ALS_OnPlayerStateChange
- #endif
- #define OnPlayerStateChange BGuard_OnPlayerStateChange
- #if defined BGuard_OnPlayerStateChange
- forward BGuard_OnPlayerStateChange(playerid, newstate, oldstate);
- #endif
- //------------------------------------------------------------------------------
- public FCNPC_OnDeath(npcid, killerid, weaponid) {
- for(new i; i < MAX_PLAYERS; i++) {
- if(!IsPlayerConnected(i)) continue;
- if(BGuard::Data[i][BGuard::NPC_ID] == npcid && FCNPC_IsValid(npcid)) {
- SetTimerEx("BGuard::DestroyNPC", 2000, false, "i", npcid);
- #if defined OnPlayerLoseBodyGuard
- OnPlayerLoseBodyGuard(i, 1);
- #endif
- BGuard::Data[i][BGuard::NPC_ID] = -1;
- }
- }
- }
- //------------------------------------------------------------------------------
- Float: BGuard::WeaponDamage( weaponid ) {
- new Float: damage;
- switch(weaponid) {
- case 25, 26, 27: damage = 30.0;
- case 33, 34: damage = 100.0;
- case 24: damage = 46.0;
- default: damage = 10.0;
- }
- return damage;
- }
- stock BGuard::DamagePlayer(playerid, Float: amount, issuerid, weaponid, place = 3)
- {
- new Float:armour;
- GetPlayerArmour(playerid, armour);
- if(armour < 1) {
- new Float:health;
- GetPlayerHealth( playerid, health);
- SetPlayerHealth( playerid, health-amount);
- OnPlayerTakeDamage(playerid, issuerid, Float: amount, weaponid, place);
- }
- else {
- if(armour-amount >= 0) SetPlayerArmour(playerid, armour-amount);
- else SetPlayerArmour(playerid, 0);
- }
- }
- stock BGuard::FollowPlayer( npcid, playerid ) {
- FCNPC_GoToPlayer(npcid, playerid, FCNPC_MOVE_TYPE_SPRINT, FCNPC_MOVE_SPEED_RUN, false, 0.0, true, 1.0, 1.0);
- }
- stock BGuard::GetFreeSeat(vehicleid) {
- new seat[25], id = -1;
- foreach(new i : Player) {
- if(!IsPlayerInVehicle(i, vehicleid)) continue;
- if(GetPlayerVehicleSeat(i) == 128) continue;
- seat[GetPlayerVehicleSeat(i)] = 5;
- break;
- }
- for(new i = 1; i < sizeof(seat); i++) {
- if(seat[i] != 5) {
- id = i;
- break;
- }
- }
- return id;
- }
- forward BGuard::DestroyNPC(npcid);
- public BGuard::DestroyNPC(npcid)
- {
- if(FCNPC_IsValid(npcid)) FCNPC_Destroy(npcid);
- return 1;
- }
- stock BGuard::ToggleFollow( playerid, bool:toggle ) {
- if(BGuard::Data[playerid][BGuard::NPC_ID] == -1) return false;
- if(toggle) BGuard::Data[playerid][BGuard::NPC_Stay] = 0;
- else BGuard::Data[playerid][BGuard::NPC_Stay] = 1;
- return true;
- }
- stock BGuard::DestroyBodyGuard(playerid)
- {
- if(BGuard::Data[playerid][BGuard::NPC_ID] == -1) return false;
- #if defined OnPlayerLoseBodyGuard
- OnPlayerLoseBodyGuard(playerid, 3);
- #endif
- FCNPC_Destroy(BGuard::Data[playerid][BGuard::NPC_ID]);
- BGuard::Data[playerid][BGuard::NPC_ID] = -1;
- return 1;
- }
- stock BGuard::CreateBodyGuard(playerid, skin, weap, bool: destroyable = true)
- {
- if(BGuard::Data[playerid][BGuard::NPC_ID] != -1) return false;
- new npcname[24], Float:x, Float:y, Float:z;
- GetPlayerPos(playerid,x,y,z);
- format(npcname, sizeof(npcname), "Guard_%d", playerid);
- new npcid = FCNPC_Create(npcname);
- FCNPC_Spawn(npcid, skin, x+1,y+1,z);
- FCNPC_SetWeapon(npcid, weap);
- FCNPC_SetAmmo(npcid, 10);
- FCNPC_ToggleReloading(npcid,true);
- FCNPC_ToggleInfiniteAmmo(npcid,true);
- BGuard::FollowPlayer(npcid, playerid);
- BGuard::Data[playerid][BGuard::NPC_Stay] = 0,
- BGuard::Data[playerid][BGuard::NPC_SFire] = 0;
- BGuard::Data[playerid][BGuard::NPC_ID] = npcid;
- SetTimerEx("BGuard_GuardCheck", 1000, false, "i", playerid);
- if(destroyable) FCNPC_SetInvulnerable(npcid, false);
- else FCNPC_SetInvulnerable(npcid, true);
- return npcid;
- }
- stock BGuard::StopAttack(playerid) {
- if(BGuard::Data[playerid][BGuard::NPC_ID] != -1 && FCNPC_IsValid(BGuard::Data[playerid][BGuard::NPC_ID])) {
- if(FCNPC_IsAiming(BGuard::Data[playerid][BGuard::NPC_ID])) {
- BGuard::Data[playerid][BGuard::NPC_SFire] = 1;
- FCNPC_StopAim(BGuard::Data[playerid][BGuard::NPC_ID]);
- if(BGuard::Data[playerid][BGuard::NPC_Stay] == 0) BGuard::FollowPlayer(BGuard::Data[playerid][BGuard::NPC_ID], playerid);
- return true;
- }
- }
- return false;
- }
- stock BGuard::AttackPlayer(playerid, victim) {
- if(BGuard::Data[playerid][BGuard::NPC_ID] != -1 && FCNPC_IsValid(BGuard::Data[playerid][BGuard::NPC_ID])) {
- FCNPC_AimAtPlayer(BGuard::Data[playerid][BGuard::NPC_ID], victim, false);
- SetTimerEx("BGuard_AimTimer", 100, false, "ii", playerid, victim);
- }
- return 1;
- }
- forward BGuard_AimTimer(playerid, target);
- public BGuard_AimTimer(playerid, target)
- {
- if(BGuard::Data[playerid][BGuard::NPC_SFire] == 1) return BGuard::Data[playerid][BGuard::NPC_SFire] = 0;
- if(BGuard::Data[playerid][BGuard::NPC_ID] != -1 && FCNPC_IsValid(BGuard::Data[playerid][BGuard::NPC_ID])) {
- new Float:x, Float:y, Float:z, Float: tempdist;
- GetPlayerPos(target, x,y,z);
- tempdist = GetPlayerDistanceFromPoint(playerid, x, y, z);
- if(tempdist < 20) { //Ako je igrac blizu
- new time, chance;
- switch(FCNPC_GetWeapon(BGuard::Data[playerid][BGuard::NPC_ID])) { //Odredjujemo timer vrijeme, te sansu da npc pogodi.
- case 25, 26, 27: time = 400, chance = 35;
- case 33, 34: time = 500, chance = 50;
- case 24: time = 700, chance = 40;
- default: time = 100, chance = 40;
- }
- FCNPC_AimAtPlayer(BGuard::Data[playerid][BGuard::NPC_ID], target, false);
- SetTimerEx("BGuard_AimTimer", time, false, "ii", playerid, target);
- if(random(100) < chance) { //Ako je NPC pogodio.
- BGuard::DamagePlayer(target, BGuard::WeaponDamage(FCNPC_GetWeapon(BGuard::Data[playerid][BGuard::NPC_ID])), BGuard::Data[playerid][BGuard::NPC_ID], FCNPC_GetWeapon(BGuard::Data[playerid][BGuard::NPC_ID]));
- FCNPC_TriggerWeaponShot(BGuard::Data[playerid][BGuard::NPC_ID], FCNPC_GetWeapon(BGuard::Data[playerid][BGuard::NPC_ID]), BULLET_HIT_TYPE_PLAYER, target, x,y,z);
- }
- else { //Ako je NPC promasio.
- FCNPC_TriggerWeaponShot(BGuard::Data[playerid][BGuard::NPC_ID], FCNPC_GetWeapon(BGuard::Data[playerid][BGuard::NPC_ID]), BULLET_HIT_TYPE_PLAYER, target, x+0.2,y+0.2,z);
- }
- }
- else
- {
- FCNPC_StopAim(BGuard::Data[playerid][BGuard::NPC_ID]);
- if(!IsPlayerInAnyVehicle(playerid)) {
- if(BGuard::Data[playerid][BGuard::NPC_Stay] == 0) BGuard::FollowPlayer(BGuard::Data[playerid][BGuard::NPC_ID], playerid);
- else FCNPC_Stop(BGuard::Data[playerid][BGuard::NPC_ID]);
- }
- else {
- if(FCNPC_GetVehicleID(BGuard::Data[playerid][BGuard::NPC_ID]) != GetPlayerVehicleID(playerid)) {
- if(BGuard::GetFreeSeat(GetPlayerVehicleID(playerid)) != -1 && BGuard::Data[playerid][BGuard::NPC_Stay] == 0) {
- FCNPC_PutInVehicle(BGuard::Data[playerid][BGuard::NPC_ID], GetPlayerVehicleID(playerid), BGuard::GetFreeSeat(GetPlayerVehicleID(playerid)));
- }
- }
- }
- }
- }
- return 1;
- }
- forward BGuard_GuardCheck(i);
- public BGuard_GuardCheck(i) {
- if(BGuard::Data[i][BGuard::NPC_ID] != -1 && FCNPC_IsValid(BGuard::Data[i][BGuard::NPC_ID])) {
- new Float:x, Float:y, Float:z, Float: tempdist;
- FCNPC_GetPosition(BGuard::Data[i][BGuard::NPC_ID], x, y, z);
- tempdist = GetPlayerDistanceFromPoint(i, x, y, z);
- new Float:PX, Float:PY, Float:PZ;
- GetPlayerPos(i, PX, PY, PZ);
- if(tempdist > 60.0) {
- #if defined OnPlayerLoseBodyGuard
- OnPlayerLoseBodyGuard(i, 2);
- #endif
- FCNPC_Destroy(BGuard::Data[i][BGuard::NPC_ID]);
- BGuard::Data[i][BGuard::NPC_ID] = -1;
- }
- else {
- SetTimerEx("BGuard_GuardCheck", 1000, false, "i", i);
- if(tempdist > 4.0) {
- if(!IsPlayerInAnyVehicle(i) && !FCNPC_IsAiming(BGuard::Data[i][BGuard::NPC_ID])) {
- if(BGuard::Data[i][BGuard::NPC_Stay] == 0) BGuard::FollowPlayer(BGuard::Data[i][BGuard::NPC_ID], i);
- }
- }
- else FCNPC_Stop(BGuard::Data[i][BGuard::NPC_ID]), FCNPC_SetAngleToPos(BGuard::Data[i][BGuard::NPC_ID], PX, PY);
- }
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment