Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2018
2,216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 13.98 KB | None | 0 0
  1. /*
  2.     Plug'N'Play Zombie System by Weponz (2018)
  3. */
  4. #define FILTERSCRIPT
  5. #include <a_samp>//Credits: SA-MP
  6. #include <rnpc>//Credits: Mauzen
  7.  
  8. #define HOLDING(%0) ((newkeys & (%0)) == (%0))
  9. #define PRESSED(%0) (((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))
  10.  
  11. //Configuration (YOU MAY EDIT THIS SECTION)
  12. #define MAX_ZOMBIES 25//Maximum amount of zombies to spawn. (Default: 25)
  13.  
  14. #define ZOMBIE_SKIN 162//Zombie skin id. (Default: 162)
  15. #define ZOMBIE_COLOUR 0xFF0000FF//Zombie colour. (Default: Red)
  16. #define ZOMBIE_DAMAGE 20//Zombie damage. (Default: 20)
  17. #define ZOMBIE_DETECT 25//Zombie detection range. (Default: 25)
  18.  
  19. //DO NOT EDIT BELOW THIS LINE!!!
  20. #define MAX_MOVEMENT 6
  21. #define MAX_ROAM 10
  22.  
  23. #define RUNNER_ZOMBIE 0
  24. #define SPRINTER_ZOMBIE 1
  25.  
  26. forward OnZombieCreate();
  27. forward OnZombieUpdate();
  28.  
  29. forward ResetDetectRange(playerid);
  30. forward ResetZombieKill(playerid);
  31.  
  32. forward RemoveZombieBody(npcid);
  33.  
  34. enum server_data
  35. {
  36.     server_zombies,
  37.     server_zombietimer
  38. };
  39. new ServerData[server_data];
  40.  
  41. enum zombie_data
  42. {
  43.     zombie_species,
  44.     zombie_victim,
  45.     zombie_roam,
  46.     zombie_movement
  47. };
  48. new ZombieData[MAX_ZOMBIES][zombie_data];
  49.  
  50. enum victim_data
  51. {
  52.     victim_detect,
  53.     victim_timer,
  54.     victim_status,
  55.     victim_kill
  56. };
  57. new VictimData[MAX_PLAYERS][victim_data];
  58.  
  59. stock GetZombieVictim(npcid)
  60. {
  61.     for(new i = 0; i < MAX_PLAYERS; i++)
  62.     {
  63.         if(IsPlayerConnected(i) && !IsPlayerNPC(i) && IsPlayerSpawned(i))
  64.         {
  65.             new Float:pos[3], Float:pos2[3];
  66.             GetPlayerPos(npcid, pos[0], pos[1], pos[2]);
  67.             GetPlayerPos(i, pos2[0], pos2[1], pos2[2]);
  68.             if(IsPlayerInRangeOfPoint(i, VictimData[i][victim_detect], pos[0], pos[1], pos[2]) && GetPlayerState(i) == PLAYER_STATE_ONFOOT)
  69.             {
  70.                 return GetClosestVictim(npcid);
  71.             }
  72.         }
  73.     }
  74.     return INVALID_PLAYER_ID;
  75. }
  76.  
  77. stock GetClosestVictim(npcid)
  78. {
  79.     new Float:dist = 1000.0, target = INVALID_PLAYER_ID, Float:pos[3], Float:pos2[3], Float:tmpdis;
  80.     GetPlayerPos(npcid, pos[0], pos[1], pos[2]);
  81.     for(new i = 0; i < MAX_PLAYERS; i++)
  82.     {
  83.         if(!IsPlayerConnected(i) || IsPlayerNPC(i) || i == npcid || !IsPlayerSpawned(i)) continue;
  84.         GetPlayerPos(i, pos2[0], pos2[1], pos2[2]);
  85.         tmpdis = floatsqroot(floatpower(floatabs(floatsub(pos2[0], pos[0])), 2) + floatpower(floatabs(floatsub(pos2[1], pos[1])), 2) + floatpower(floatabs(floatsub(pos2[2], pos[2])), 2));
  86.         if(tmpdis < dist && GetPlayerState(i) == PLAYER_STATE_ONFOOT)
  87.         {
  88.             dist = tmpdis;
  89.             target = i;
  90.         }
  91.     }
  92.     return target;
  93. }
  94.  
  95. stock GetRandomPlayer()
  96. {
  97.     new players[MAX_PLAYERS], online = 0;
  98.     for(new i = 0; i < MAX_PLAYERS; i++)
  99.     {
  100.         if(!IsPlayerNPC(i) && IsPlayerConnected(i))
  101.         {
  102.             players[online] = i;
  103.             online++;
  104.         }
  105.     }
  106.  
  107.     if(online > 0)
  108.     {
  109.         return players[random(online)];
  110.     }
  111.     return INVALID_PLAYER_ID;
  112. }
  113.  
  114. stock GetOnlinePlayers()
  115. {
  116.     new count = 0;
  117.     for(new i = 0; i < MAX_PLAYERS; i++)
  118.     {
  119.         if(IsPlayerConnected(i) && !IsPlayerNPC(i))
  120.         {
  121.             count++;
  122.         }
  123.     }
  124.     return count;
  125. }
  126.  
  127. stock bool:IsZombieStranded(npcid)
  128. {
  129.     new Float:pos[3];
  130.     GetPlayerPos(npcid, pos[0], pos[1], pos[2]);
  131.  
  132.     for(new i = 0; i < MAX_PLAYERS; i++)
  133.     {
  134.         if(!IsPlayerConnected(i) || IsPlayerNPC(i)) continue;
  135.         if(IsPlayerInRangeOfPoint(i, 300.0, pos[0], pos[1], pos[2]))
  136.         {
  137.             return false;
  138.         }
  139.     }
  140.     return true;
  141. }
  142.  
  143. stock IsHoldingFirearm(playerid)
  144. {
  145.     if(GetPlayerWeapon(playerid) == 30) { return 1; }
  146.     else if(GetPlayerWeapon(playerid) == 31) { return 1; }
  147.     else if(GetPlayerWeapon(playerid) == 32) { return 1; }
  148.     else if(GetPlayerWeapon(playerid) == 22) { return 1; }
  149.     else if(GetPlayerWeapon(playerid) == 23) { return 1; }
  150.     else if(GetPlayerWeapon(playerid) == 24) { return 1; }
  151.     else if(GetPlayerWeapon(playerid) == 33) { return 1; }
  152.     else if(GetPlayerWeapon(playerid) == 34) { return 1; }
  153.     else if(GetPlayerWeapon(playerid) == 25) { return 1; }
  154.     else if(GetPlayerWeapon(playerid) == 27) { return 1; }
  155.     else if(GetPlayerWeapon(playerid) == 26) { return 1; }
  156.     else if(GetPlayerWeapon(playerid) == 28) { return 1; }
  157.     else if(GetPlayerWeapon(playerid) == 29) { return 1; }
  158.     return 0;
  159. }
  160.  
  161. stock IsPlayerSpawned(playerid)
  162. {
  163.     new pState = GetPlayerState(playerid);
  164.     return 0 <= playerid <= MAX_PLAYERS && pState != PLAYER_STATE_NONE && pState != PLAYER_STATE_WASTED && pState != PLAYER_STATE_SPECTATING;
  165. }
  166.  
  167. public OnFilterScriptInit()
  168. {
  169.     for(new i = 0; i < MAX_ZOMBIES; i++)
  170.     {
  171.         ZombieData[i][zombie_species] = RUNNER_ZOMBIE;
  172.         ZombieData[i][zombie_victim] = INVALID_PLAYER_ID;
  173.         ZombieData[i][zombie_movement] = 0;
  174.     }
  175.  
  176.     ServerData[server_zombies] = 0;
  177.     ServerData[server_zombietimer] = SetTimer("OnZombieCreate", 80, true);//Just incase, as some servers require you to do this.
  178.    
  179.     SetTimer("OnZombieUpdate", 1000, true);
  180.     return 1;
  181. }
  182.  
  183. public OnPlayerSpawn(playerid)
  184. {
  185.     if(IsPlayerNPC(playerid))
  186.     {
  187.         new players = GetOnlinePlayers(), targetid = GetRandomPlayer(), position = random(10), Float:pos[3], health = random(25) + 15;
  188.         if(players > 0 && targetid != INVALID_PLAYER_ID)
  189.         {
  190.             GetPlayerPos(targetid, pos[0], pos[1], pos[2]);
  191.            
  192.             ClearAnimations(playerid);
  193.  
  194.             SetPlayerPos(playerid, pos[0], pos[1], pos[2] + 500.0);
  195.             SetPlayerInterior(playerid, 0);
  196.             SetPlayerVirtualWorld(playerid, 0);
  197.  
  198.             if(position == 0) { SetPlayerPos(playerid, pos[0] + 100.0, pos[1], pos[2] + 500.00); }
  199.             else if(position == 1) { SetPlayerPos(playerid, pos[0], pos[1] + 100.0, pos[2] + 500.00); }
  200.             else if(position == 2) { SetPlayerPos(playerid, pos[0] - 100.0, pos[1], pos[2] + 500.00); }
  201.             else if(position == 3) { SetPlayerPos(playerid, pos[0], pos[1] - 100.0, pos[2] + 500.00); }
  202.             else if(position == 4) { SetPlayerPos(playerid, pos[0] + 150.0, pos[1], pos[2] + 500.00); }
  203.             else if(position == 5) { SetPlayerPos(playerid, pos[0], pos[1] + 150.0, pos[2] + 500.00); }
  204.             else if(position == 6) { SetPlayerPos(playerid, pos[0] - 150.0, pos[1], pos[2] + 500.00); }
  205.             else { SetPlayerPos(playerid, pos[0], pos[1] - 150.0, pos[2] + 500.00); }
  206.         }
  207.         else
  208.         {
  209.             ClearAnimations(playerid);
  210.            
  211.             SetPlayerPos(playerid, 0.0, 0.0, 0.0 + 500.00);
  212.             SetPlayerInterior(playerid, 0);
  213.             SetPlayerVirtualWorld(playerid, 0);
  214.  
  215.             if(position == 0) { SetPlayerPos(playerid, 0.0 + 100.0, 0.0, 0.0 + 500.00); }
  216.             else if(position == 1) { SetPlayerPos(playerid, 0.0, 0.0 + 100.0, 0.0 + 500.00); }
  217.             else if(position == 2) { SetPlayerPos(playerid, 0.0 - 100.0, 0.0, 0.0 + 500.00); }
  218.             else if(position == 3) { SetPlayerPos(playerid, 0.0, 0.0 - 100.0, 0.0 + 500.00); }
  219.             else if(position == 4) { SetPlayerPos(playerid, 0.0 + 150.0, 0.0, 0.0 + 500.00); }
  220.             else if(position == 5) { SetPlayerPos(playerid, 0.0, 0.0 + 150.0, 0.0 + 500.00); }
  221.             else if(position == 6) { SetPlayerPos(playerid, 0.0 - 150.0, 0.0, 0.0 + 500.00); }
  222.         }
  223.  
  224.         SetRNPCHealth(playerid, health);
  225.         SetPlayerSkin(playerid, ZOMBIE_SKIN);
  226.         SetPlayerColor(playerid, ZOMBIE_COLOUR);
  227.  
  228.         RNPC_SetShootable(playerid, 1);
  229.         RNPC_ToggleVehicleCollisionCheck(playerid, 1);
  230.  
  231.         ZombieData[playerid][zombie_victim] = INVALID_PLAYER_ID;
  232.  
  233.         ZombieData[playerid][zombie_roam] = 0;
  234.         ZombieData[playerid][zombie_movement] = 0;
  235.     }
  236.     else if(!IsPlayerNPC(playerid))
  237.     {
  238.         VictimData[playerid][victim_status] = 0;
  239.         VictimData[playerid][victim_detect] = ZOMBIE_DETECT;
  240.     }
  241.     return 1;
  242. }
  243.  
  244. public OnPlayerDeath(playerid, killerid, reason)
  245. {
  246.     if(IsPlayerNPC(killerid))
  247.     {
  248.         new health = random(25) + 15;
  249.         SetRNPCHealth(killerid, health);
  250.         ApplyAnimation(killerid, "BOMBER", "BOM_Plant", 4.1, 0, 1, 1, 1, 0, 1);
  251.         SendDeathMessage(killerid, ZombieData[killerid][zombie_victim], reason);
  252.         ZombieData[killerid][zombie_victim] = INVALID_PLAYER_ID;
  253.     }
  254.     return 1;
  255. }
  256.  
  257. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  258. {
  259.     if(!IsPlayerNPC(playerid))
  260.     {
  261.         if(PRESSED(KEY_JUMP) && GetPlayerState(playerid) == PLAYER_STATE_ONFOOT)//Shift
  262.         {
  263.             if(VictimData[playerid][victim_status] == 0)
  264.             {
  265.                 VictimData[playerid][victim_detect] = (ZOMBIE_DETECT * 2);
  266.                 VictimData[playerid][victim_status] = 1;
  267.                 VictimData[playerid][victim_timer] = SetTimerEx("ResetDetectRange", 25000, false, "i", playerid);
  268.             }
  269.             return 1;
  270.         }
  271.  
  272.         if(HOLDING(KEY_SPRINT) && GetPlayerState(playerid) == PLAYER_STATE_ONFOOT)//Space
  273.         {
  274.             if(VictimData[playerid][victim_status] == 0)
  275.             {
  276.                 VictimData[playerid][victim_detect] = (ZOMBIE_DETECT * 2);
  277.                 VictimData[playerid][victim_status] = 1;
  278.                 VictimData[playerid][victim_timer] = SetTimerEx("ResetDetectRange", 25000, false, "i", playerid);
  279.             }
  280.             return 1;
  281.         }
  282.  
  283.         if(HOLDING(KEY_FIRE) && GetPlayerState(playerid) == PLAYER_STATE_ONFOOT)//LMB
  284.         {
  285.             if(IsHoldingFirearm(playerid) == 1)
  286.             {
  287.                 if(GetPlayerWeapon(playerid) != 23)
  288.                 {
  289.                     VictimData[playerid][victim_detect] = (ZOMBIE_DETECT * 4);
  290.                     if(VictimData[playerid][victim_status] == 1) { KillTimer(VictimData[playerid][victim_timer]); }
  291.                     VictimData[playerid][victim_status] = 1;
  292.                     VictimData[playerid][victim_timer] = SetTimerEx("ResetDetectRange", 25000, false, "i", playerid);
  293.                 }
  294.             }
  295.             return 1;
  296.         }
  297.     }
  298.     return 1;
  299. }
  300.  
  301. public OnRNPCDeath(npcid, killerid, reason)
  302. {
  303.     if(killerid != INVALID_PLAYER_ID)
  304.     {
  305.         if(VictimData[killerid][victim_kill] != npcid)
  306.         {
  307.             VictimData[killerid][victim_kill] = npcid;
  308.             SetTimerEx("ResetZombieKill", 4000, false, "i", killerid);
  309.             SendDeathMessage(killerid, npcid, reason);
  310.         }
  311.     }
  312.    
  313.     SetPlayerColor(npcid, 0xFFFFFF00);
  314.  
  315.     ApplyAnimation(npcid, "PED", "KO_shot_front", 4.1, 0, 0, 0, 1, 0);
  316.     SetTimerEx("RemoveZombieBody", 10000, false, "i", npcid);
  317.     return 1;
  318. }
  319.  
  320. public ResetDetectRange(playerid)
  321. {
  322.     VictimData[playerid][victim_status] = 0;
  323.     VictimData[playerid][victim_detect] = ZOMBIE_DETECT;
  324.     return 1;
  325. }
  326.  
  327. public ResetZombieKill(playerid)
  328. {
  329.     VictimData[playerid][victim_kill] = INVALID_PLAYER_ID;
  330.     return 1;
  331. }
  332.  
  333. public RemoveZombieBody(npcid)
  334. {
  335.     RespawnRNPC(npcid);
  336.     return 1;
  337. }
  338.  
  339. public OnZombieCreate()
  340. {
  341.     RNPC_SetUpdateRate(80);
  342.    
  343.     MapAndreas_Init(MAP_ANDREAS_MODE_NOBUFFER);
  344.  
  345.     new type = random(4), name[MAX_PLAYER_NAME], zombieid = ServerData[server_zombies];
  346.     if(type == SPRINTER_ZOMBIE)
  347.     {
  348.         ZombieData[zombieid][zombie_species] = SPRINTER_ZOMBIE;
  349.     }
  350.     else
  351.     {
  352.         ZombieData[zombieid][zombie_species] = RUNNER_ZOMBIE;
  353.     }
  354.  
  355.     ZombieData[zombieid][zombie_victim]= INVALID_PLAYER_ID;
  356.  
  357.     format(name, sizeof(name), "Zombie_%i", zombieid + 1);
  358.     ConnectRNPC(name);
  359.  
  360.     ServerData[server_zombies]++;
  361.  
  362.     if(ServerData[server_zombies] >= MAX_ZOMBIES)
  363.     {
  364.         KillTimer(ServerData[server_zombietimer]);
  365.         printf("Number of zombies created: %i", ServerData[server_zombies]);
  366.     }
  367.     return 1;
  368. }
  369.  
  370. public OnZombieUpdate()
  371. {
  372.     for(new i = 0; i < MAX_ZOMBIES; i++)
  373.     {
  374.         if(IsPlayerConnected(i) || IsPlayerNPC(i))
  375.         {
  376.             if(GetRNPCHealth(i) > 0)
  377.             {
  378.                 new victimid, Float:pos[3];
  379.                 victimid = GetZombieVictim(i);
  380.                 GetPlayerPos(i, pos[0], pos[1], pos[2]);
  381.  
  382.                 if(victimid != INVALID_PLAYER_ID)//Attack
  383.                 {
  384.                     if(IsPlayerInRangeOfPoint(victimid, VictimData[victimid][victim_detect], pos[0], pos[1], pos[2]))
  385.                     {
  386.                         if(IsPlayerInRangeOfPoint(victimid, 1.0, pos[0], pos[1], pos[2]))
  387.                         {
  388.                             ZombieData[i][zombie_victim] = victimid;
  389.  
  390.                             ZombieData[i][zombie_movement] = 0;
  391.                             ZombieData[i][zombie_roam] = 0;
  392.  
  393.                             RNPC_CreateBuild(i, PLAYER_RECORDING_TYPE_ONFOOT);
  394.                             RNPC_AddPause(100);
  395.                             RNPC_SetKeys(KEY_FIRE);
  396.                             RNPC_AddPause(100);
  397.                             RNPC_SetKeys(0);
  398.                             RNPC_FinishBuild();
  399.                             RNPC_StartBuildPlayback(i);
  400.  
  401.                             new Float:health;
  402.                             GetPlayerHealth(victimid, health);
  403.                             SetPlayerHealth(victimid, health - ZOMBIE_DAMAGE);
  404.  
  405.                             GameTextForPlayer(victimid, "~r~You are being bitten!", 3000, 5);
  406.                             continue;
  407.                         }
  408.                         else
  409.                         {
  410.                             ZombieData[i][zombie_movement] = 0;
  411.                             ZombieData[i][zombie_roam] = 0;
  412.  
  413.                             GetPlayerPos(victimid, pos[0], pos[1], pos[2]);
  414.  
  415.                             if(ZombieData[i][zombie_species] == SPRINTER_ZOMBIE)
  416.                             {
  417.                                 MoveRNPC(i, pos[0], pos[1], pos[2], RNPC_SPEED_SPRINT, 1);
  418.                             }
  419.                             else
  420.                             {
  421.                                 MoveRNPC(i, pos[0], pos[1], pos[2], RNPC_SPEED_RUN, 1);
  422.                             }
  423.                             continue;
  424.                         }
  425.                     }
  426.                 }
  427.                 else if(victimid == INVALID_PLAYER_ID)//Roam
  428.                 {
  429.                     if(ZombieData[i][zombie_roam] == 0)
  430.                     {
  431.                         ZombieData[i][zombie_roam]++;
  432.                         ZombieData[i][zombie_victim] = INVALID_PLAYER_ID;
  433.  
  434.                         RNPC_SetKeys(0);
  435.  
  436.                         new point = random(6);
  437.                         if(point == 0) { pos[0] = pos[0] + 100.0; }
  438.                         else if(point == 1) { pos[0] = pos[0] - 100.0; }
  439.                         else if(point == 2) { pos[1] = pos[1] + 100.0; }
  440.                         else if(point == 3) { pos[1] = pos[1] - 100.0; }
  441.  
  442.                         else if(point == 4) { pos[0] = pos[0] + 100.0; pos[1] = pos[1] + 100.0; }
  443.                         else if(point == 5) { pos[0] = pos[0] - 100.0; pos[1] = pos[1] - 100.0; }
  444.  
  445.                         MoveRNPC(i, pos[0], pos[1], pos[2], RNPC_SPEED_WALK, 1);
  446.                         continue;
  447.                     }
  448.                     else
  449.                     {
  450.                         ZombieData[i][zombie_roam]++;
  451.  
  452.                         if(ZombieData[i][zombie_roam] >= MAX_ROAM)
  453.                         {
  454.                             ZombieData[i][zombie_roam] = 0;
  455.                             ZombieData[i][zombie_victim] = INVALID_PLAYER_ID;
  456.  
  457.                             RNPC_SetKeys(0);
  458.  
  459.                             new point = random(6);
  460.                             if(point == 0) { pos[0] = pos[0] + 100.0; }
  461.                             else if(point == 1) { pos[0] = pos[0] - 100.0; }
  462.                             else if(point == 2) { pos[1] = pos[1] + 100.0; }
  463.                             else if(point == 3) { pos[1] = pos[1] - 100.0; }
  464.  
  465.                             else if(point == 4) { pos[0] = pos[0] + 100.0; pos[1] = pos[1] + 100.0; }
  466.                             else if(point == 5) { pos[0] = pos[0] - 100.0; pos[1] = pos[1] - 100.0; }
  467.  
  468.                             MoveRNPC(i, pos[0], pos[1], pos[2], RNPC_SPEED_WALK, 1);
  469.  
  470.                             ZombieData[i][zombie_movement]++;
  471.  
  472.                             if(ZombieData[i][zombie_movement] >= MAX_MOVEMENT && IsZombieStranded(i) == true)
  473.                             {
  474.                                 ZombieData[i][zombie_movement] = 0;
  475.                                 SetRNPCHealth(i, 0.0);
  476.                             }
  477.                             continue;
  478.                         }
  479.                     }
  480.                 }
  481.             }
  482.         }
  483.     }
  484.     return 1;
  485. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement