Advertisement
EnzoMetlc

[Include] AFNPC - Control System for NCP.

Jan 23rd, 2014
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 9.70 KB | None | 0 0
  1. #include <a_samp>
  2. #include <YSI\y_hooks>
  3.  
  4.  
  5. /* // Native functions:
  6.  
  7.  
  8.     // Done by RyDeR:
  9.     native Float:GetPointZPos(const Float: fX, const Float: fY, &Float: fZ = 0.0);
  10.     native RotateNPCToPos(npcid, const Float:PosX, const Float:PosY);
  11.    
  12.  
  13.     // Done by me:
  14.     native CreateNPC(npcid, name[]);
  15.     native DestroyNPC(npcid);
  16.    
  17.     native SpawnNPC(npcid);
  18.     native SetNPCPos(npcid, Float:PosX, Float:PosY, Float:PosZ);
  19.     native SetNPCPosFindZ(npcid);
  20.     native SetNPCPosFindZEx(npcid, Float:PosX, Float:PosY);
  21.     native SetNPCFacingAngle(npcid, Float:Angle);
  22.     native SetNPCHealth(npcid, Float:Health);
  23.     native SetNPCArmour(npcid, Float:Armour);
  24.  
  25.     native ApplyNPCAnimation(npcid, animlib[], animname[], Float:fDelta, loop, lockx, locky, freeze, time, forcesync = 0);
  26.  
  27.     native GetNPCPos(npcid, &Float:PosX, &Float:PosY, &Float:PosZ);
  28.     native GetNPCFacingAngle(npcid, &Float:Angle);
  29.     native Float:GetNPCHealth(npcid);
  30.     native Float:GetNPCArmour(npcid);
  31.  
  32.     native ForeachNPC(iterate); // Thank you pds2k12 for a better code. :)
  33.     native CountAllNPC();
  34.     native KickAllNPC();
  35.  
  36.  
  37. // Forward's:
  38.  
  39.     forward OnNPCCreated(npcid);
  40.     forward OnNPCSpawn(npcid);
  41.     forward OnPlayerDamgeToNPC(npcid, playerid, weaponid, Float:amount);
  42.     forward OnNPCDeath(npcid, killerid, weaponid);
  43.     forward OnNPCDisconnect(npcid);
  44. */
  45.  
  46.  
  47. #define MAX_NPC 5
  48.  
  49. /
  50. #define ForeachNPC(%0,%1) \
  51.     new %0; while(++%0 < %1) if(IsPlayerNPC(%0))  return true; else return false;
  52.  
  53.  
  54. new ToAddUpNPCCreated;
  55.  
  56.  
  57. enum DataNPC
  58. {
  59.     Float:HealthNPC,
  60.     Float:ArmourNPC,
  61.     bool:IsCreated,
  62.     bool:IsSpawned,
  63. };
  64. new InfoNPC[MAX_NPC][DataNPC];
  65.  
  66.  
  67.  
  68. stock Float:GetPointZPos(const Float: fX, const Float: fY, &Float: fZ = 0.0)
  69. {
  70.     if(!((-3000.0 < fX < 3000.0) && (-3000.0 < fY < 3000.0)))
  71.     {
  72.         return 0.0;
  73.     }
  74.     static File: s_hMap;
  75.     if(!s_hMap)
  76.     {
  77.         s_hMap = fopen("SAfull.hmap", io_read);
  78.         if(!s_hMap)
  79.         {
  80.             return 0.0;
  81.         }
  82.     }
  83.     new afZ[1] ;
  84.     fseek(s_hMap, ((6000 * (-floatround(fY, floatround_tozero) + 3000) + (floatround(fX, floatround_tozero) + 3000)) << 1));
  85.     fblockread(s_hMap, afZ);
  86.     return (fZ = ((afZ[0] >>> 16) * 0.01));
  87. }
  88.  
  89.  
  90. stock CreateNPC(npcid, name[])
  91. {
  92.     if(InfoNPC[npcid][IsCreated] == false)
  93.     {
  94.         if(strlen(name) > 2)
  95.         {
  96.             ToAddUpNPCCreated ++;
  97.             new string[15];
  98.             format(string, sizeof(string), "E_NPC[%i]", ToAddUpNPCCreated);
  99.             npcid = ConnectNPC(name, string);
  100.             InfoNPC[npcid][IsSpawned] = false;
  101.             InfoNPC[npcid][IsCreated] = true;
  102.             ApplyNPCAnimation(npcid, "SWEET", "LaFin_Sweet", 4.1, 1, 1, 1, 1, 0, 0);
  103.         }
  104.         else
  105.         {
  106.             print("  ");
  107.             print("***********************************************************************");
  108.             print("*                                                                     *");
  109.             print("*                                                                     *");
  110.             print("* [Warning-AFNPC] The Nick of the NPC must have 3 characters or more. *");
  111.             print("*                                                                     *");
  112.             print("*                                                                     *");
  113.             print("***********************************************************************");
  114.             print("  ");
  115.         }
  116.     }
  117. }
  118.  
  119.  
  120.  
  121.  
  122. stock GetNPCFacingAngle(npcid, &Float:Angle)
  123. {
  124.     if(IsPlayerNPC(npcid)) return GetPlayerFacingAngle(npcid, Angle);
  125.     return 1;
  126. }
  127.  
  128. stock Float:GetNPCHealth(npcid)
  129. {
  130.     if(IsPlayerNPC(npcid)) return (InfoNPC[npcid][HealthNPC]);
  131.     return 1.0;
  132. }
  133.  
  134.  
  135.  
  136. stock Float:GetNPCArmour(npcid)
  137. {
  138.     if(IsPlayerNPC(npcid)) return (InfoNPC[npcid][ArmourNPC]);
  139. }
  140.  
  141.  
  142. stock DestroyNPC(npcid)
  143. {
  144.     if(IsPlayerNPC(npcid))
  145.     {
  146.         ToAddUpNPCCreated --;
  147.         Kick(npcid);
  148.     }
  149. }
  150.  
  151.  
  152. stock SpawnNPC(npcid)
  153. {
  154.     if(IsPlayerNPC(npcid))
  155.     {
  156.         SpawnPlayer(npcid);
  157.         InfoNPC[npcid][IsSpawned] = true;
  158.     }
  159. }
  160.  
  161.  
  162.  
  163. stock SetNPCPos(npcid, Float:PosX, Float:PosY, Float:PosZ)
  164. {
  165.     if(IsPlayerNPC(npcid))
  166.     {
  167.         SetPlayerPos(npcid, PosX, PosY, PosZ);
  168.     }
  169. }
  170.  
  171.  
  172. stock SetNPCPosFindZ(npcid)
  173. {
  174.     if(IsPlayerNPC(npcid))
  175.     {
  176.         new Float:Pos[3];
  177.         GetNPCPos(npcid, Pos[0], Pos[1], Pos[2]);
  178.         GetPointZPos(Pos[0], Pos[1], Pos[2]);
  179.         SetNPCPos(npcid, Pos[0], Pos[1], Pos[2]);
  180.     }
  181. }
  182.  
  183. stock SetNPCPosFindZEx(npcid, Float:PosX, Float:PosY)
  184. {
  185.     if(IsPlayerNPC(npcid))
  186.     {
  187.         SetNPCPos(npcid, PosX, PosY, 0.0);
  188.         SetNPCPosFindZ(npcid);
  189.     }
  190. }
  191.  
  192.  
  193. /*
  194.  
  195. // This script serves, but it does not work correctly (it is not possible to do that the NPC runs softly like it should)
  196. stock MoveNPC(npcid, Float:PosX, Float:PosY, Float:PosZ, Float:speed)
  197. {
  198.     if(IsPlayerNPC(npcid))
  199.     {
  200.         new Float:Pos[3];
  201.         new Float:AngleNPC;
  202.  
  203.         GetPlayerPos(npcid, Pos[0], Pos[1], Pos[2]);
  204.         GetNPCFacingAngle(npcid, AngleNPC);
  205.  
  206.         RotateNPCToPos(npcid, PosX, PosY);
  207.         DestroyObject(InfoNPC[npcid][ObjectMoveNPC]);
  208.         InfoNPC[npcid][ObjectMoveNPC] = CreateObject(19300, Pos[0], Pos[1], Pos[2], 0.0, 0.0, AngleNPC);
  209.         MoveObject(InfoNPC[npcid][ObjectMoveNPC], PosX, PosY, PosZ, speed, 0.0, 0.0, 0.0);
  210.  
  211.         ApplyNPCAnimation(npcid, "PED", "RUN_civi", 4.1, 0, 1, 1, 0, -1);
  212.  
  213.         InfoNPC[npcid][TimerMoveNPC] = SetTimerEx("MoveNPC_Callback", 75, true, "d", npcid);
  214.  
  215.         InfoNPC[npcid][IsMoving] = true;
  216.     }
  217. }
  218.  
  219.  
  220. forward MoveNPC_Callback(npcid);
  221. public MoveNPC_Callback(npcid)
  222. {
  223.     new Float:Pos[3];
  224.  
  225.     //ApplyNPCAnimation(npcid, "PED", "RUN_civi", 4.1, 0, 1, 1, 0, -1);
  226.     GetObjectPos(InfoNPC[npcid][ObjectMoveNPC], Pos[0], Pos[1], Pos[2]);
  227.     GetPointZPos(Pos[0], Pos[1], Pos[2]);
  228.  
  229.     SetNPCPos(npcid, Pos[0], Pos[1], Pos[2]+0.5);
  230.     return 1;
  231. }
  232.  
  233.  
  234.  
  235. public OnObjectMoved(objectid)
  236. {
  237.     new i;
  238.     ForeachNPC(i)
  239.     {
  240.         if(InfoNPC[i][IsFollowing] == true)
  241.         {
  242.             StopObject(InfoNPC[i][ObjectMoveNPC]);
  243.             KillTimer(InfoNPC[i][TimerMoveNPC]);
  244.         }
  245.     }
  246.     return 1;
  247. }
  248. */
  249.  
  250.  
  251. stock SetNPCFacingAngle(npcid, Float:Angle)
  252. {
  253.     if(IsPlayerNPC(npcid))
  254.     {
  255.         SetPlayerFacingAngle(npcid, Angle);
  256.     }
  257. }
  258.  
  259.  
  260.  
  261.  
  262. stock RotateNPCToPos(npcid, const Float:PosX, const Float:PosY)
  263. {
  264.     if(IsPlayerNPC(npcid))
  265.     {
  266.         new Float:Pos[2];
  267.         new Float:AngleNPC;
  268.  
  269.         if(GetPlayerPos(npcid, Pos[0], Pos[1], AngleNPC))
  270.         {
  271.             AngleNPC = atan2((PosY-Pos[1]), (PosX-Pos[0])) + 270.0;
  272.             SetNPCFacingAngle(npcid, (AngleNPC));
  273.         }
  274.     }
  275. }
  276.  
  277.  
  278. stock SetNPCSkin(npcid, skinid)
  279. {
  280.     if(IsPlayerNPC(npcid))
  281.     {
  282.         SetPlayerSkin(npcid, skinid);
  283.     }
  284. }
  285.  
  286.  
  287. stock GetNPCPos(npcid, &Float:PosX, Float:PosY, Float:PosZ)
  288. {
  289.     if(IsPlayerNPC(npcid)) return GetPlayerPos(npcid, PosX, PosY, PosZ);
  290.     return 1;
  291. }
  292.  
  293.  
  294.  
  295. stock SetNPCHealth(npcid, Float:Health)
  296. {
  297.     if(IsPlayerNPC(npcid))
  298.     {
  299.         InfoNPC[npcid][HealthNPC] = Health;
  300.     }
  301. }
  302.  
  303. stock SetNPCArmour(npcid, Float:Armour)
  304. {
  305.     if(IsPlayerNPC(npcid))
  306.     {
  307.         InfoNPC[npcid][ArmourNPC] = Armour;
  308.     }
  309. }
  310.  
  311.  
  312.  
  313. stock ApplyNPCAnimation(npcid, animlib[], animname[], Float:fDelta, loop, lockx, locky, freeze, time, forcesync = 0)
  314. {
  315.     if(IsPlayerNPC(npcid))
  316.     {
  317.         ClearAnimations(npcid);
  318.         ApplyAnimation(npcid, animlib, animname, fDelta, loop, lockx, locky, freeze, time, forcesync);
  319.     }
  320. }
  321.  
  322.  
  323.  
  324. stock CountAllNPC() return ToAddUpNPCCreated;
  325.  
  326. stock KickToAllNpc()
  327. {
  328.     ToAddUpNPCCreated = -1;
  329.     new i;
  330.     ForeachNPC(i)
  331.     {
  332.         Kick(i);
  333.     }
  334. }
  335.  
  336.  
  337.  
  338. // Hoocked functions:
  339. hook: public OnPlayerWeaponShot(playerid, weaponid, hittype, hitid, Float:fX, Float:fY, Float:fZ)
  340. {
  341.     if(hittype == 1 && IsPlayerNPC(hitid))
  342.     {
  343.         if(InfoNPC[hitid][IsSpawned] == true)
  344.         {
  345.             new Float:Health = GetNPCHealth(hitid);
  346.             if(GetNPCHealth(hitid) >= 0.001)
  347.             {
  348.                 switch(weaponid)
  349.                 {
  350.                     case 22: SetNPCHealth(hitid, Health-8.25);
  351.                     case 23: SetNPCHealth(hitid, Health-13.2);
  352.                     case 24: SetNPCHealth(hitid, Health-46.2);
  353.                     case 25: SetNPCHealth(hitid, Health-49.5);
  354.                     case 26: SetNPCHealth(hitid, Health-49.5);
  355.                     case 27: SetNPCHealth(hitid, Health-39.6);
  356.                     case 28: SetNPCHealth(hitid, Health-6.6);
  357.                     case 29: SetNPCHealth(hitid, Health-8.25);
  358.                     case 30, 31: SetNPCHealth(hitid, Health-9.9);
  359.                     case 32: SetNPCHealth(hitid, Health-6.6);
  360.                     case 33: SetNPCHealth(hitid, Health-24.75);
  361.                     case 34: SetNPCHealth(hitid, Health-41.25);
  362.                     case 38: SetNPCHealth(hitid, Health-46.2);
  363.                 }
  364.                 switch(random(2))
  365.                 {
  366.                     case 0: ApplyNPCAnimation(hitid, "PED", "HIT_front", 4.1, 0, 1, 1, 0, 500);
  367.                     case 1: ApplyNPCAnimation(hitid, "PED", "IDLE_stance", 4.1, 0, 1, 1, 0, 500);
  368.                 }
  369.                 new Float:HealthRest = GetNPCHealth(hitid);
  370.                 CallLocalFunction("OnPlayerDamgeToNPC", "ddif", hitid, playerid, weaponid, Health-HealthRest);
  371.             }
  372.             else
  373.             {
  374.                 CallLocalFunction("OnNPCDeath", "ddi", hitid, playerid, weaponid);
  375.                 SetTimerEx("OnNPCSpawn", 4000, false, "d", hitid);
  376.                 ApplyNPCAnimation(hitid, "SWEET", "LaFin_Sweet", 4.1, 1, 1, 1, 1, 0, 0);
  377.             }
  378.         }
  379.     }
  380.     return 1;
  381. }
  382.  
  383.  
  384.  
  385. hook: public OnFilterScriptInit()
  386. {
  387.     ToAddUpNPCCreated = -1;
  388.     return 1;
  389. }
  390.  
  391. hook: public OnPlayerConnect(playerid)
  392. {
  393.     if(IsPlayerNPC(playerid))
  394.     {
  395.         CallLocalFunction("OnNPCCreated", "d", playerid);
  396.     }
  397.     return 1;
  398. }
  399.  
  400. hook: public OnPlayerDisconnect(playerid, reason)
  401. {
  402.     if(IsPlayerNPC(playerid))
  403.     {
  404.         CallLocalFunction("OnNPCDisconnect", "di", playerid, reason);
  405.     }
  406.     return 1;
  407. }
  408.  
  409. hook: public OnPlayerSpawn(playerid)
  410. {
  411.     if(IsPlayerNPC(playerid))
  412.     {
  413.         SetTimerEx("OnNPCSpawn", 500, false, "d", playerid); // Fixed.
  414.     }
  415.     return 1;
  416. }
  417.  
  418. hook: public OnNPCDeath(npcid, playerid, weaponid)
  419. {
  420.     InfoNPC[npcid][IsSpawned] = false;
  421.     return 1;
  422. }
  423. //*********
  424.  
  425.  
  426. forward OnNPCCreated(npcid);
  427. forward OnNPCSpawn(npcid);
  428. forward OnPlayerDamgeToNPC(npcid, playerid, weaponid, Float:amount);
  429. forward OnNPCDeath(npcid, killerid, weaponid);
  430. forward OnNPCDisconnect(npcid);
  431.  
  432. // End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement