Guest User

BGuard.inc

a guest
Aug 3rd, 2018
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.80 KB | None | 0 0
  1. //------------------------------------------------------------------------------
  2. ////////////////////////////////////////////////////
  3. // Player BodyGuard Include //
  4. // Author: Slade //
  5. // Credits: OrMisicL (FCNPC plugin) //
  6. ////////////////////////////////////////////////////
  7. //------------------------------------------------------------------------------
  8. #if defined _BGuard_included
  9. #endinput
  10. #endif
  11. #define _BGuard_included
  12.  
  13. #if !defined _samp_included
  14. #tryinclude <a_samp>
  15. #if !defined _samp_included
  16. #error could not locate a_samp.inc file, please check your server includes
  17. #endif
  18. #endif
  19. #define BGuard:: BGuard_
  20. //------------------------------------------------------------------------------
  21. enum BGuard::En {
  22. BGuard::NPC_ID,
  23. BGuard::NPC_Stay,
  24. BGuard::NPC_SFire
  25. }
  26. new BGuard::Data[MAX_PLAYERS][BGuard::En];
  27. //------------------------------------------------------------------------------
  28. /*
  29. native BGuard::ToggleFollow( playerid, bool:toggle );
  30. native BGuard::DestroyBodyGuard(playerid);
  31. native BGuard::CreateBodyGuard(playerid, skin, weap);
  32. native BGuard::AttackPlayer(playerid, victim);
  33. native BGuard::StopAttack(playerid);
  34. */
  35. //------------------------------------------------------------------------------
  36. #if defined OnPlayerLoseBodyGuard
  37. forward OnPlayerLoseBodyGuard(playerid, ltype);
  38. #endif
  39. //---------------------------------[ HOOKS ]------------------------------------
  40. public OnPlayerConnect(playerid) {
  41. BGuard::Data[playerid][BGuard::NPC_ID] = -1;
  42. #if defined BGuard_OnPlayerConnect
  43. BGuard_OnPlayerConnect(playerid);
  44. #endif
  45. return 1;
  46. }
  47.  
  48. #if defined _ALS_OnPlayerConnect
  49. #undef OnPlayerConnect
  50. #else
  51. #define _ALS_OnPlayerConnect
  52. #endif
  53. #define OnPlayerConnect BGuard_OnPlayerConnect
  54. #if defined BGuard_OnPlayerConnect
  55. forward BGuard_OnPlayerConnect(playerid);
  56. #endif
  57.  
  58. public OnPlayerDisconnect(playerid, reason) {
  59. if(BGuard::Data[playerid][BGuard::NPC_ID] != -1) {
  60. FCNPC_Destroy(BGuard::Data[playerid][BGuard::NPC_ID]);
  61. #if defined OnPlayerLoseBodyGuard
  62. OnPlayerLoseBodyGuard(playerid, 4);
  63. #endif
  64. BGuard::Data[playerid][BGuard::NPC_ID] = -1;
  65. }
  66. #if defined BGuard_OnPlayerDisconnect
  67. BGuard_OnPlayerDisconnect(playerid, reason);
  68. #endif
  69. return 1;
  70. }
  71.  
  72. #if defined _ALS_OnPlayerDisconnect
  73. #undef OnPlayerDisconnect
  74. #else
  75. #define _ALS_OnPlayerDisconnect
  76. #endif
  77. #define OnPlayerDisconnect BGuard_OnPlayerDisconnect
  78. #if defined BGuard_OnPlayerDisconnect
  79. forward BGuard_OnPlayerDisconnect(playerid, reason);
  80. #endif
  81.  
  82. public OnPlayerDeath(playerid, killerid, reason) {
  83. if(BGuard::Data[playerid][BGuard::NPC_ID] != -1) {
  84. FCNPC_Destroy(BGuard::Data[playerid][BGuard::NPC_ID]);
  85. #if defined OnPlayerLoseBodyGuard
  86. OnPlayerLoseBodyGuard(playerid, 4);
  87. #endif
  88. BGuard::Data[playerid][BGuard::NPC_ID] = -1;
  89. }
  90. #if defined BGuard_OnPlayerDeath
  91. BGuard_OnPlayerDeath(playerid, killerid, reason);
  92. #endif
  93. return 1;
  94. }
  95.  
  96. #if defined _ALS_OnPlayerDeath
  97. #undef OnPlayerDeath
  98. #else
  99. #define _ALS_OnPlayerDeath
  100. #endif
  101. #define OnPlayerDeath BGuard_OnPlayerDeath
  102. #if defined BGuard_OnPlayerDeath
  103. forward BGuard_OnPlayerDeath(playerid, killerid, reason);
  104. #endif
  105.  
  106. public OnPlayerStateChange(playerid, newstate, oldstate) {
  107. if(newstate == PLAYER_STATE_DRIVER)
  108. {
  109. if(BGuard::Data[playerid][BGuard::NPC_ID] != -1 && FCNPC_IsValid(BGuard::Data[playerid][BGuard::NPC_ID])) {
  110. if(FCNPC_GetVehicleID(BGuard::Data[playerid][BGuard::NPC_ID]) != GetPlayerVehicleID(playerid)) {
  111. if(BGuard::GetFreeSeat(GetPlayerVehicleID(playerid)) != -1) {
  112. FCNPC_PutInVehicle(BGuard::Data[playerid][BGuard::NPC_ID], GetPlayerVehicleID(playerid), BGuard::GetFreeSeat(GetPlayerVehicleID(playerid)));
  113. }
  114. }
  115. }
  116. }
  117. #if defined BGuard_OnPlayerStateChange
  118. BGuard_OnPlayerStateChange(playerid, newstate, oldstate);
  119. #endif
  120. return 1;
  121. }
  122.  
  123. #if defined _ALS_OnPlayerStateChange
  124. #undef OnPlayerStateChange
  125. #else
  126. #define _ALS_OnPlayerStateChange
  127. #endif
  128. #define OnPlayerStateChange BGuard_OnPlayerStateChange
  129. #if defined BGuard_OnPlayerStateChange
  130. forward BGuard_OnPlayerStateChange(playerid, newstate, oldstate);
  131. #endif
  132. //------------------------------------------------------------------------------
  133. public FCNPC_OnDeath(npcid, killerid, weaponid) {
  134. for(new i; i < MAX_PLAYERS; i++) {
  135. if(!IsPlayerConnected(i)) continue;
  136. if(BGuard::Data[i][BGuard::NPC_ID] == npcid && FCNPC_IsValid(npcid)) {
  137. SetTimerEx("BGuard::DestroyNPC", 2000, false, "i", npcid);
  138. #if defined OnPlayerLoseBodyGuard
  139. OnPlayerLoseBodyGuard(i, 1);
  140. #endif
  141. BGuard::Data[i][BGuard::NPC_ID] = -1;
  142. }
  143. }
  144. }
  145. //------------------------------------------------------------------------------
  146. Float: BGuard::WeaponDamage( weaponid ) {
  147. new Float: damage;
  148. switch(weaponid) {
  149. case 25, 26, 27: damage = 30.0;
  150. case 33, 34: damage = 100.0;
  151. case 24: damage = 46.0;
  152. default: damage = 10.0;
  153. }
  154. return damage;
  155. }
  156. stock BGuard::DamagePlayer(playerid, Float: amount, issuerid, weaponid, place = 3)
  157. {
  158. new Float:armour;
  159. GetPlayerArmour(playerid, armour);
  160. if(armour < 1) {
  161. new Float:health;
  162. GetPlayerHealth( playerid, health);
  163. SetPlayerHealth( playerid, health-amount);
  164. OnPlayerTakeDamage(playerid, issuerid, Float: amount, weaponid, place);
  165. }
  166. else {
  167. if(armour-amount >= 0) SetPlayerArmour(playerid, armour-amount);
  168. else SetPlayerArmour(playerid, 0);
  169. }
  170. }
  171. stock BGuard::FollowPlayer( npcid, playerid ) {
  172. FCNPC_GoToPlayer(npcid, playerid, FCNPC_MOVE_TYPE_SPRINT, FCNPC_MOVE_SPEED_RUN, false, 0.0, true, 1.0, 1.0);
  173. }
  174. stock BGuard::GetFreeSeat(vehicleid) {
  175. new seat[25], id = -1;
  176. foreach(new i : Player) {
  177. if(!IsPlayerInVehicle(i, vehicleid)) continue;
  178. if(GetPlayerVehicleSeat(i) == 128) continue;
  179. seat[GetPlayerVehicleSeat(i)] = 5;
  180. break;
  181. }
  182. for(new i = 1; i < sizeof(seat); i++) {
  183. if(seat[i] != 5) {
  184. id = i;
  185. break;
  186. }
  187. }
  188. return id;
  189. }
  190. forward BGuard::DestroyNPC(npcid);
  191. public BGuard::DestroyNPC(npcid)
  192. {
  193. if(FCNPC_IsValid(npcid)) FCNPC_Destroy(npcid);
  194. return 1;
  195. }
  196. stock BGuard::ToggleFollow( playerid, bool:toggle ) {
  197. if(BGuard::Data[playerid][BGuard::NPC_ID] == -1) return false;
  198. if(toggle) BGuard::Data[playerid][BGuard::NPC_Stay] = 0;
  199. else BGuard::Data[playerid][BGuard::NPC_Stay] = 1;
  200. return true;
  201. }
  202. stock BGuard::DestroyBodyGuard(playerid)
  203. {
  204. if(BGuard::Data[playerid][BGuard::NPC_ID] == -1) return false;
  205. #if defined OnPlayerLoseBodyGuard
  206. OnPlayerLoseBodyGuard(playerid, 3);
  207. #endif
  208. FCNPC_Destroy(BGuard::Data[playerid][BGuard::NPC_ID]);
  209. BGuard::Data[playerid][BGuard::NPC_ID] = -1;
  210. return 1;
  211. }
  212. stock BGuard::CreateBodyGuard(playerid, skin, weap, bool: destroyable = true)
  213. {
  214. if(BGuard::Data[playerid][BGuard::NPC_ID] != -1) return false;
  215. new npcname[24], Float:x, Float:y, Float:z;
  216. GetPlayerPos(playerid,x,y,z);
  217. format(npcname, sizeof(npcname), "Guard_%d", playerid);
  218. new npcid = FCNPC_Create(npcname);
  219. FCNPC_Spawn(npcid, skin, x+1,y+1,z);
  220. FCNPC_SetWeapon(npcid, weap);
  221. FCNPC_SetAmmo(npcid, 10);
  222. FCNPC_ToggleReloading(npcid,true);
  223. FCNPC_ToggleInfiniteAmmo(npcid,true);
  224. BGuard::FollowPlayer(npcid, playerid);
  225. BGuard::Data[playerid][BGuard::NPC_Stay] = 0,
  226. BGuard::Data[playerid][BGuard::NPC_SFire] = 0;
  227. BGuard::Data[playerid][BGuard::NPC_ID] = npcid;
  228. SetTimerEx("BGuard_GuardCheck", 1000, false, "i", playerid);
  229. if(destroyable) FCNPC_SetInvulnerable(npcid, false);
  230. else FCNPC_SetInvulnerable(npcid, true);
  231. return npcid;
  232. }
  233. stock BGuard::StopAttack(playerid) {
  234. if(BGuard::Data[playerid][BGuard::NPC_ID] != -1 && FCNPC_IsValid(BGuard::Data[playerid][BGuard::NPC_ID])) {
  235. if(FCNPC_IsAiming(BGuard::Data[playerid][BGuard::NPC_ID])) {
  236. BGuard::Data[playerid][BGuard::NPC_SFire] = 1;
  237. FCNPC_StopAim(BGuard::Data[playerid][BGuard::NPC_ID]);
  238. if(BGuard::Data[playerid][BGuard::NPC_Stay] == 0) BGuard::FollowPlayer(BGuard::Data[playerid][BGuard::NPC_ID], playerid);
  239. return true;
  240. }
  241. }
  242. return false;
  243. }
  244. stock BGuard::AttackPlayer(playerid, victim) {
  245. if(BGuard::Data[playerid][BGuard::NPC_ID] != -1 && FCNPC_IsValid(BGuard::Data[playerid][BGuard::NPC_ID])) {
  246. FCNPC_AimAtPlayer(BGuard::Data[playerid][BGuard::NPC_ID], victim, false);
  247. SetTimerEx("BGuard_AimTimer", 100, false, "ii", playerid, victim);
  248. }
  249. return 1;
  250. }
  251. forward BGuard_AimTimer(playerid, target);
  252. public BGuard_AimTimer(playerid, target)
  253. {
  254. if(BGuard::Data[playerid][BGuard::NPC_SFire] == 1) return BGuard::Data[playerid][BGuard::NPC_SFire] = 0;
  255. if(BGuard::Data[playerid][BGuard::NPC_ID] != -1 && FCNPC_IsValid(BGuard::Data[playerid][BGuard::NPC_ID])) {
  256. new Float:x, Float:y, Float:z, Float: tempdist;
  257. GetPlayerPos(target, x,y,z);
  258. tempdist = GetPlayerDistanceFromPoint(playerid, x, y, z);
  259. if(tempdist < 20) { //Ako je igrac blizu
  260. new time, chance;
  261. switch(FCNPC_GetWeapon(BGuard::Data[playerid][BGuard::NPC_ID])) { //Odredjujemo timer vrijeme, te sansu da npc pogodi.
  262. case 25, 26, 27: time = 400, chance = 35;
  263. case 33, 34: time = 500, chance = 50;
  264. case 24: time = 700, chance = 40;
  265. default: time = 100, chance = 40;
  266. }
  267. FCNPC_AimAtPlayer(BGuard::Data[playerid][BGuard::NPC_ID], target, false);
  268. SetTimerEx("BGuard_AimTimer", time, false, "ii", playerid, target);
  269. if(random(100) < chance) { //Ako je NPC pogodio.
  270. 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]));
  271. FCNPC_TriggerWeaponShot(BGuard::Data[playerid][BGuard::NPC_ID], FCNPC_GetWeapon(BGuard::Data[playerid][BGuard::NPC_ID]), BULLET_HIT_TYPE_PLAYER, target, x,y,z);
  272. }
  273. else { //Ako je NPC promasio.
  274. 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);
  275. }
  276. }
  277. else
  278. {
  279. FCNPC_StopAim(BGuard::Data[playerid][BGuard::NPC_ID]);
  280. if(!IsPlayerInAnyVehicle(playerid)) {
  281. if(BGuard::Data[playerid][BGuard::NPC_Stay] == 0) BGuard::FollowPlayer(BGuard::Data[playerid][BGuard::NPC_ID], playerid);
  282. else FCNPC_Stop(BGuard::Data[playerid][BGuard::NPC_ID]);
  283. }
  284. else {
  285. if(FCNPC_GetVehicleID(BGuard::Data[playerid][BGuard::NPC_ID]) != GetPlayerVehicleID(playerid)) {
  286. if(BGuard::GetFreeSeat(GetPlayerVehicleID(playerid)) != -1 && BGuard::Data[playerid][BGuard::NPC_Stay] == 0) {
  287. FCNPC_PutInVehicle(BGuard::Data[playerid][BGuard::NPC_ID], GetPlayerVehicleID(playerid), BGuard::GetFreeSeat(GetPlayerVehicleID(playerid)));
  288. }
  289. }
  290. }
  291. }
  292. }
  293. return 1;
  294. }
  295. forward BGuard_GuardCheck(i);
  296. public BGuard_GuardCheck(i) {
  297. if(BGuard::Data[i][BGuard::NPC_ID] != -1 && FCNPC_IsValid(BGuard::Data[i][BGuard::NPC_ID])) {
  298. new Float:x, Float:y, Float:z, Float: tempdist;
  299. FCNPC_GetPosition(BGuard::Data[i][BGuard::NPC_ID], x, y, z);
  300. tempdist = GetPlayerDistanceFromPoint(i, x, y, z);
  301. new Float:PX, Float:PY, Float:PZ;
  302. GetPlayerPos(i, PX, PY, PZ);
  303. if(tempdist > 60.0) {
  304. #if defined OnPlayerLoseBodyGuard
  305. OnPlayerLoseBodyGuard(i, 2);
  306. #endif
  307. FCNPC_Destroy(BGuard::Data[i][BGuard::NPC_ID]);
  308. BGuard::Data[i][BGuard::NPC_ID] = -1;
  309. }
  310. else {
  311. SetTimerEx("BGuard_GuardCheck", 1000, false, "i", i);
  312. if(tempdist > 4.0) {
  313. if(!IsPlayerInAnyVehicle(i) && !FCNPC_IsAiming(BGuard::Data[i][BGuard::NPC_ID])) {
  314. if(BGuard::Data[i][BGuard::NPC_Stay] == 0) BGuard::FollowPlayer(BGuard::Data[i][BGuard::NPC_ID], i);
  315. }
  316. }
  317. else FCNPC_Stop(BGuard::Data[i][BGuard::NPC_ID]), FCNPC_SetAngleToPos(BGuard::Data[i][BGuard::NPC_ID], PX, PY);
  318. }
  319. }
  320. return 1;
  321. }
Advertisement
Add Comment
Please, Sign In to add comment