ZELDAX

infection.slua

Jan 15th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.43 KB | None | 0 0
  1. -----------------------------------------------------------------------------------------
  2. --  INFORMATION: Community Event Manager | Infection Event ( Zelda ) | Server side
  3. --  TOPIC:       https://cit2.net/index.php?topic=325432.msg4794240#msg4794240
  4. -----------------------------------------------------------------------------------------
  5.  
  6. local minPlayers = 3 --  Minimun players to participate
  7. local zombieHits = 2 -- Zombie hits to convert a survivor
  8. local timeEvent = 60 -- Seconds to start
  9. local lastPos, participants, survivors, zombies, dmg, start = {}, {}, {}, {}, nil
  10.  
  11. local zombieSpawn =  {
  12.     {395.75787353516, -2371.6535644531, 3.3488941192627},
  13.     {394.39294433594, -2371.9772949219, 3.3488941192627},
  14.     {395.90365600586, -2373.4309082031, 3.3488941192627},
  15.     {394.86157226563, -2373.7033691406, 3.3488941192627},
  16.     {405.0712890625, -2384.8032226563, 3.3488941192627},
  17.     {403.89306640625, -2384.9995117188, 3.3488941192627},
  18.     {404.85485839844, -2383.662109375, 3.3488941192627},
  19.     {403.74230957031, -2383.8522949219, 3.3488941192627},
  20.     {407.46069335938, -2381.8188476563, 3.3488941192627},
  21.     {407.12286376953, -2380.388671875, 3.3488941192627},
  22.     {406.87033081055, -2378.9853515625, 3.3488941192627},
  23.     {406.51379394531, -2377.5573730469, 3.3488941192627},
  24.     {400.5719909668, -2371.1164550781, 3.3600325584412},
  25.     {401.61032104492, -2370.9135742188, 3.3600325584412},
  26.     {400.85083007813, -2372.2338867188, 3.3600325584412},
  27.     {401.70031738281, -2371.9643554688, 3.3600325584412},
  28. }
  29.  
  30. local survivalsSpawn =  {
  31.     {429.25793457031, -2564.2958984375, 10.582830429077},
  32.     {428.640625, -2562.8400878906, 10.572721481323},
  33.     {430.07098388672, -2563.8388671875, 10.5006275177},
  34.     {429.4479675293, -2563.021484375, 10.516647338867},
  35.     {435.97274780273, -2559.837890625, 10.205181121826},
  36.     {436.78106689453, -2559.3420410156, 10.151559829712},
  37.     {436.71331787109, -2558.5192871094, 10.21697807312},
  38.     {435.56307983398, -2558.8542480469, 10.319427490234},
  39.     {433.6247253418, -2555.3972167969, 10.103618621826},
  40.     {434.46490478516, -2555.7663574219, 10.179895401001},
  41.     {432.54794311523, -2555.8879394531, 10.047999382019},
  42.     {433.64892578125, -2556.8305664063, 10.168029785156},
  43.     {428.171875, -2560.6547851563, 10.521595001221},
  44.     {428.96755981445, -2559.5014648438, 10.412731170654},
  45.     {430.02325439453, -2558.255859375, 10.279745101929},
  46.     {431.10305786133, -2557.2897949219, 10.156097412109},
  47. }
  48.  
  49. function makeEvent(player)
  50.     if (isGuestAccount(getPlayerAccount(player))) then
  51.         return
  52.     end
  53.     if (hasObjectPermissionTo(player, "command.givevehicle", false) or exports.CITadmin:getPlayerAdminLevel(player) >= 1) then
  54.         if startTime and isTimer(startTime) then
  55.             killTimer(startTime)
  56.         end
  57.         startTime = setTimer(startEvent, (timeEvent*1000), 1)
  58.         exports.CIThelp:dm("Infection event will start in "..math.floor((getTimerDetails(startTime)/1000)).." seconds, use /goinfection to participate", root, 255, 0, 200)
  59.     end
  60. end
  61. addCommandHandler("startinfection", makeEvent)
  62.  
  63. function joinEvent(player)
  64.     if (isGuestAccount(getPlayerAccount(player))) then
  65.         return
  66.     end
  67.     if (not startTime) or (not isTimer(startTime)) then
  68.         exports.CIThelp:dm("The infection event is not activated at this time", player, 255, 0, 0)
  69.         return
  70.     end
  71.     if (start) then
  72.         exports.CIThelp:dm("The infection event already started, wait for the next one", player, 255, 255, 0)
  73.         return
  74.     end
  75.     -- if (not exports.CITchecking:canPlayerDoAction(player, "InfectionEvent")) then return end
  76.     if (participants[player]) then
  77.         exports.CIThelp:dm("You are already participating, seconds remaining to start the event: "..math.floor((getTimerDetails(startTime)/1000)).."", player, 255, 255, 0)
  78.         return
  79.     end
  80.     participants[player] = true
  81.     exports.CIThelp:dm("Now you are participating in the infection event!", player, 0, 255, 0)
  82. end
  83. addCommandHandler("goinfection", joinEvent)
  84.  
  85. function onResourceStop()
  86.     for k, v in pairs(participants) do
  87.         if (isElement(k)) then
  88.             setElementData(k, "infection", false)
  89.             if (lastPos[k]) then
  90.                 local x, y, z, int, dim, model = lastPos[k][1], lastPos[k][2], lastPos[k][3], lastPos[k][4], lastPos[k][5], lastPos[k][6]
  91.                 setElementPosition(k, x, y, z)
  92.                 setElementInterior(k, int)
  93.                 setElementDimension(k, dim)
  94.                 exports.CITclothes:setModel(k, model)
  95.                 exports.CIThelp:dm("The script has been reset, they all appeared in their previous locations.", k, 255, 255, 0)
  96.             end
  97.         end
  98.     end
  99. end
  100. addEventHandler("onResourceStop", resourceRoot, onResourceStop)
  101.  
  102. function canPlayerEnter(player)
  103.     if (getElementHealth(player) < 1) then
  104.         exports.CIThelp:dm("Your participation to the infection event is canceled because you not have enough life", player, 255, 0, 0)
  105.         return
  106.     end
  107.     if (isPedDead(player)) then
  108.         exports.CIThelp:dm("Your participation to the infection event is canceled because you are dead", player, 255, 0, 0)
  109.         return
  110.     end
  111.     if (exports.CITprison:isPlayerInJail(player)) then
  112.         exports.CIThelp:dm("Your participation to the infection event is canceled because you are in prison", player, 255, 0, 0)
  113.         return
  114.     end
  115.     if (exports.CITpoliceArrest:isPlayerArrested(player)) then
  116.         exports.CIThelp:dm("Your participation to the infection event is canceled because you are arrested", player, 255, 0, 0)
  117.         return
  118.     end
  119.     if (exports.CITteams:isPlayerInTeams(player, "Staff")) then -- Not allowed because staff is ivencible
  120.         exports.CIThelp:dm("Remove your staff job frist", player, 255, 0, 0)
  121.         return
  122.     end
  123.     return true
  124. end
  125.  
  126. function eventCheck()
  127.     local n = 0
  128.     for k, v in pairs(participants) do
  129.         if (isElement(k) and canPlayerEnter(k)) then
  130.             n = n + 1
  131.         end
  132.     end
  133.     return n
  134. end
  135.  
  136. function getRandomZombie()
  137.     local temporal = {}
  138.     for k, v in pairs(participants) do
  139.         table.insert(temporal, {k})
  140.     end
  141.     return temporal[math.random(1, #temporal)][1]
  142. end
  143.  
  144. function startEvent()
  145.     if (eventCheck() < minPlayers) then
  146.         exports.CIThelp:dm("The infection event was canceled because there are not enough players to participate - Minimun: ("..minPlayers..").", root, 255, 0, 0)
  147.         participants = {}
  148.         return
  149.     end
  150.     start = true
  151.     local zombie = getRandomZombie()
  152.     for k, v in pairs(participants) do
  153.         if (isElement(k)) then
  154.             local x, y, z = getElementPosition(k)
  155.             local int, dim = getElementInterior(k), getElementDimension(k)
  156.             local model = getElementModel(k)
  157.             lastPos[k] = {x, y, z, int, dim, model}
  158.             if (k == zombie) then
  159.                 zombies[k] = true
  160.                 setElementData(k, "infection", "zombie")
  161.                 exports.CITclothes:setModel(k, 11)
  162.                 local random = math.random(#zombieSpawn)
  163.                 local x, y, z = zombieSpawn[random][1], zombieSpawn[random][2], zombieSpawn[random][3]
  164.                 setElementPosition(k, x, y, z)
  165.                 setElementDimension(k, 16000)
  166.                 setCameraTarget(k, k)
  167.                 exports.CITtrivia:sendMessage("Hunt the survivors!", k, 0, 255, 0)
  168.                 triggerClientEvent("CITinfection.startEvent", k)
  169.             else
  170.                 survivors[k] = true
  171.                 setElementData(k, "infection", "survivor")
  172.                 exports.CITclothes:setModel(k, 264)
  173.                 local random = math.random(#survivalsSpawn)
  174.                 local x, y, z = survivalsSpawn[random][1], survivalsSpawn[random][2], survivalsSpawn[random][3]
  175.                 setElementPosition(k, x, y, z)
  176.                 setElementDimension(k, 16000)
  177.                 setCameraTarget(k, k)
  178.                 exports.CITtrivia:sendMessage("Kill to zombie's!", k, 0, 255, 0)
  179.                 triggerClientEvent("CITinfection.startEvent", k)
  180.             end
  181.         end
  182.     end
  183. end
  184.  
  185. function getPlayerInEvent()
  186.     local surv, zomb = 0, 0
  187.     for k, v in pairs(survivors) do
  188.         if (isElement(k)) then
  189.             surv = surv + 1
  190.         end
  191.     end
  192.     for k, v in pairs(zombies) do
  193.         if (isElement(k)) then
  194.             zomb = zomb + 1
  195.         end
  196.     end
  197.     return surv, zomb
  198. end
  199.    
  200. function onPlayerWasted(ammo, theKiller, weapon, bodypart)
  201.     if (participants[source]) then
  202.         participants[source] = nil
  203.         triggerClientEvent("CITinfection.restoreModels", source)
  204.         if (getElementData(source, "infection") == "survivor") then
  205.             setElementData(source, "infection", false)
  206.             survivors[source] = nil
  207.         elseif (getElementData(source, "infection") == "zombie") then
  208.             zombies[source] = nil
  209.             setElementData(source, "infection", false)
  210.         end
  211.         local survs, zombs = getPlayerInEvent()
  212.         if (survs == 0) then
  213.             for k, v in pairs(participants) do
  214.                 if (isElement(k)) then
  215.                     exports.CITmoney:GPM(k, 15000, "Infection Event", true)
  216.                     setElementData(k, "infection", false)
  217.                     triggerClientEvent("CITinfection.passed", k)
  218.                     local x, y, z, int, dim, model = lastPos[k][1], lastPos[k][2], lastPos[k][3], lastPos[k][4], lastPos[k][5], lastPos[k][6]
  219.                     setElementPosition(k, x, y, z)
  220.                     setElementInterior(k, int)
  221.                     setElementDimension(k, dim)
  222.                     exports.CITclothes:setModel(k, model)
  223.                     triggerClientEvent("CITinfection.restoreModels", k)
  224.                 end
  225.             end
  226.             exports.CIThelp:dm("Survivors have won the infection event", root, 255, 0, 0)
  227.             lastPos, participants, survivors, zombies, start = {}, {}, {}, {}, nil
  228.         elseif zombs == 0 then
  229.             for k, v in pairs(participants) do
  230.                 if (isElement(k)) then
  231.                     exports.CITmoney:GPM(k, 15000, "Infection Event", true)
  232.                     setElementData(k, "infection", false)
  233.                     triggerClientEvent("CITinfection.passed", k)
  234.                     local x, y, z, int, dim, model = lastPos[k][1], lastPos[k][2], lastPos[k][3], lastPos[k][4], lastPos[k][5], lastPos[k][6]
  235.                     setElementPosition(k, x, y, z)
  236.                     setElementInterior(k, int)
  237.                     setElementDimension(k, dim)
  238.                     exports.CITclothes:setModel(k, model)
  239.                     triggerClientEvent("CITinfection.restoreModels", k)
  240.                 end
  241.             end
  242.             exports.CIThelp:dm("The zombies have won the event of infection", root, 255, 0, 0)
  243.             lastPos, participants, survivors, zombies, start = {}, {}, {}, {}, nil
  244.         end
  245.     end
  246. end
  247. addEventHandler("onPlayerWasted", root, onPlayerWasted)
  248.  
  249. function onPlayerQuit()
  250.     if (start) then
  251.         lastPos[source], participants[source], zombies[source], survivors[source] = nil, nil, nil, nil
  252.         local survs, zombs = getPlayerInEvent()
  253.         if (survs == 0) then
  254.             exports.CIThelp:dm("Survivors have won the infection event", root, 255, 0, 0)
  255.             for k, v in pairs(participants) do
  256.                 if isElement(k) then
  257.                     exports.CITmoney:GPM(k, 15000, "Infection Event", true)
  258.                     setElementData(k, "infection", false)
  259.                     triggerClientEvent("CITinfection.passed", k)
  260.                     local x, y, z, int, dim, model = lastPos[k][1], lastPos[k][2], lastPos[k][3], lastPos[k][4], lastPos[k][5], lastPos[k][6]
  261.                     setElementPosition(k, x, y, z)
  262.                     setElementInterior(k, int)
  263.                     setElementDimension(k, dim)
  264.                     exports.CITclothes:setModel(k, model)
  265.                     triggerClientEvent("CITinfection.restoreModels", k)
  266.                 end
  267.             end
  268.             lastPos, participants, survivors, zombies, start = {}, {}, {}, {}, nil
  269.         end
  270.     end
  271. end
  272. addEventHandler("onPlayerQuit", getRootElement(), onPlayerQuit)
  273.  
  274. function dmgReset(victim)
  275.     if (isElement(victim)) then
  276.         setElementData(victim, "hitz", 0)
  277.         dmg[victim] = nil
  278.     end
  279. end
  280.  
  281. function tryConvertToZombie(survivor)
  282.     if (not dmg[survivor]) then
  283.         dmg[survivor] = setTimer(dmgReset, 10000, 1, survivor)
  284.     end
  285.     setElementData(survivor, "hitz", (getElementData(survivor, "hitz") or 0) +1)
  286.     local data = getElementData(survivor,"hitz") or 0
  287.     exports.CIThelp:modTextBar(client, "CITinfection",  ""..data.."/"..zombieHits.." Bite by "..getPlayerName(client).."", 255, 0, 0, 255, 10000)
  288.     exports.CIThelp:modTextBar(survivor, "CITinfection", ""..data.."/"..zombieHits.." Bite by "..getPlayerName(client).."", 255 , 0, 0, 255, 10000)
  289.     if (getElementData(survivor, "hitz") < zombieHits) then
  290.         return
  291.     end
  292.     exports.CIThelp:modTextBar(client, "CITinfection", "Converted", 255, 255, 255)
  293.     exports.CIThelp:modTextBar(survivor, "CITinfection", "Converted", 255, 255, 255)
  294.     exports.CITclothes:setModel(survivor, 11)
  295.     survivors[survivor] = nil
  296.     zombies[survivor] = true
  297.     setElementData(survivor, "infection", "zombie")
  298.     triggerClientEvent("CITinfection.converted", survivor)
  299.     local survs, zombs = getPlayerInEvent()
  300.     if (survs == 0) then
  301.         participants[survivor] = nil
  302.         exports.CIThelp:dm("Survivors have won the infection event", root, 255, 0, 0)
  303.         for k, v in pairs(participants) do
  304.             if (isElement(k)) then
  305.                 setElementData(k, "infection", false)
  306.                 triggerClientEvent("CITinfection.passed", k)
  307.                 exports.CITmoney:GPM(k, 15000, "Infection Event", true)
  308.                 local x, y, z, int, dim, model = lastPos[k][1], lastPos[k][2], lastPos[k][3], lastPos[k][4], lastPos[k][5], lastPos[k][6]
  309.                 setElementPosition(k, x, y, z)
  310.                 setElementInterior(k, int)
  311.                 setElementDimension(k, dim)
  312.                 exports.CITclothes:setModel(k, model)
  313.                 triggerClientEvent("CITinfection.restoreModels", k)
  314.             end
  315.         end
  316.         lastPos, participants, survivors, zombies, start = {}, {}, {}, {}, nil
  317.     end
  318. end
  319. addEvent("CITinfection.tryConvertToZombie",true)
  320. addEventHandler("CITinfection.tryConvertToZombie", root, tryConvertToZombie)
Advertisement
Add Comment
Please, Sign In to add comment