RyanUSNS

ServerSide

Dec 30th, 2016
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.29 KB | None | 0 0
  1. local spawnPoint, showSpawns, tempSpawns, spawned = {}, {}, {}, {}
  2. local limit, status, info
  3.  
  4. function addSpawn(plr, cmd, restriction)
  5.     if (not hasObjectPermissionTo(plr, "command.em", false) or getElementDimension(plr) ~= 336) then
  6.         return false
  7.     end
  8.     if (status) then
  9.         outputChatBox("Error: Someone is already using the spawn points!", plr, 255, 0, 0)
  10.         return false
  11.     end
  12.     if (not restriction) then
  13.         restriction = "all"
  14.     end
  15.     if (restriction and restriction ~= "all") then
  16.         local team = findTeam(restriction)
  17.         if (not team) then
  18.             outputChatBox("Error: Invalid team name.", plr, 255, 0, 0)
  19.             return false
  20.         end
  21.     end
  22.     local x, y, z = getElementPosition(plr)
  23.     local int = getElementInterior(plr)
  24.     spawnPoint[#spawnPoint + 1] = {x, y, z, int, restriction}
  25.     local text = "Added a marker. Currently has "..(#spawnPoint).." spawnPoint added."
  26.     if (showSpawns[plr]) then
  27.         showSpawn(plr, "showspawns", true)
  28.         setTimer(restartSpawn, 5000, 1, plr, "add")
  29.         text = "Added the marker and updating the spawnPoint. Within 5 seconds, spawnPoint will appear. Currently has "..(#spawnPoint).." spawnPoint added."
  30.         removeCommandHandler("addspawn", addSpawn)
  31.     end
  32.     outputChatBox(text, plr, 0, 255, 0)
  33. end
  34. addCommandHandler("addspawn", addSpawn)
  35.  
  36. function delSpawn(plr)
  37.     if (not hasObjectPermissionTo(plr, "command.em", false) or getElementDimension(plr) ~= 336) then
  38.         return false
  39.     end
  40.     if (status) then
  41.         outputChatBox("Error: Someone is already using the spawn points!", plr, 255, 0, 0)
  42.         return false
  43.     end
  44.     if (#spawnPoint <= 0) then
  45.         outputChatBox("Error: You do not have any marker added.", plr, 255, 0, 0)
  46.         return false
  47.     end
  48.     table.remove(spawnPoint, #spawnPoint)
  49.     local text = "Removed a marker. Currently has "..(#spawnPoint).." spawnPoint added."
  50.     if (showSpawns[plr]) then
  51.         showSpawn(plr, "showspawns", true)
  52.         setTimer(restartSpawn, 5000, 1, plr, "remove")
  53.         text = "Removed a marker and updating the spawnPoint. Within 5 seconds, spawnPoint will appear. Currently has "..(#spawnPoint).." spawnPoint added."
  54.         removeCommandHandler("delspawn", delSpawn)
  55.     end
  56.     outputChatBox(text, plr, 0, 255, 0)
  57. end
  58. addCommandHandler("delspawn", delSpawn)
  59.  
  60. function restartSpawn(plr, cmd)
  61.     if (not plr or not cmd) then
  62.         return false
  63.     end
  64.     if (cmd and cmd == "remove") then
  65.         addCommandHandler("delspawn", delSpawn)
  66.         showSpawn(plr, "showspawns", true)
  67.         return true
  68.     end
  69.     addCommandHandler("addspawn", addSpawn)
  70.     showSpawn(plr, "showspawns", true)
  71. end
  72.  
  73. function showSpawn(plr, cmd, auto)
  74.     if (not hasObjectPermissionTo(plr, "command.em", false) or getElementDimension(plr) ~= 336) then
  75.         return false
  76.     end
  77.     if (status) then
  78.         outputChatBox("Error: Someone is already using the spawn points!", plr, 255, 0, 0)
  79.         return false
  80.     end
  81.     if (#spawnPoint <= 0) then
  82.         outputChatBox("Error: You do not have any marker added.", plr, 255, 0, 0)
  83.         return false
  84.     end
  85.     if (showSpawns[plr]) then
  86.         for i, v in pairs(tempSpawns) do
  87.             if (isElement(i) and isElement(v)) then
  88.                 destroyElement(i)
  89.                 destroyElement(v)
  90.             end
  91.         end
  92.         tempSpawns = {}
  93.         showSpawns[plr] = false
  94.         if (auto) then
  95.             return false
  96.         end
  97.         outputChatBox("Disable the spawnPoint! Currently has "..(#spawnPoint).." spawnPoint added.", plr, 0, 255, 0)
  98.         return true
  99.     end
  100.     for i, v in pairs(spawnPoint) do
  101.         local x, y, z = v[1], v[2], v[3]
  102.         local r, g, b = 255, 255, 255
  103.         if (v[5] ~= "all") then
  104.             local team = findTeam(v[5])
  105.             r, g, b = getTeamColor(team)
  106.         end
  107.         local marker = createMarker(x, y, z-1, "cylinder", 4, r, g, b, 255, plr)
  108.         tempSpawns[marker] = createBlipAttachedTo(marker, 0, 2, r, g, b, 255, 0, 65535, plr)
  109.         setElementInterior(marker, v[4])
  110.         setElementInterior(tempSpawns[marker], v[4])
  111.         setElementDimension(marker, 336)
  112.         setElementDimension(tempSpawns[marker], 336)
  113.     end
  114.     showSpawns[plr] = true
  115.     if (auto) then
  116.         return false
  117.     end
  118.     outputChatBox("Enable the spawnPoint! Currently has "..(#spawnPoint).." spawnPoint added.", plr, 0, 255, 0)
  119. end
  120. addCommandHandler("showspawns", showSpawn)
  121.  
  122. function usageSpawns(plr, cmd, typeS, misc)
  123.     if (not hasObjectPermissionTo(plr, "command.em", false) or getElementDimension(plr) ~= 336) then
  124.         return false
  125.     end
  126.     if (not typeS or typeS ~= "stop" and not misc) then
  127.         outputChatBox("Syntax: '/emspawn start/stop/limit race(start)/dm(start)/number(limit)", plr, 255, 0, 0)
  128.         return false
  129.     end
  130.     if (typeS == "start" and not misc or typeS == "start" and misc ~= "dm" and misc ~= "race") then
  131.         outputChatBox("Syntax: '/emspawn start race/dm", plr, 255, 0, 0)
  132.         return false
  133.     end
  134.     if (typeS == "limit" and not tonumber(misc)) then
  135.         outputChatBox("Syntax: '/emspawn limit number", plr, 255, 0, 0)
  136.         return false
  137.     end
  138.     if (typeS == "limit") then
  139.         if (status) then
  140.             outputChatBox("Error: You cannot change the limit of respawn!", plr, 255, 0, 0)
  141.             return false
  142.         end
  143.         limit = tonumber(misc)
  144.         outputChatBox("Limited to "..tostring(limit).." respawns.", plr, 0, 255, 0)
  145.         return true
  146.     end
  147.     if (typeS == "start") then
  148.         if (status) then
  149.             outputChatBox("Error: Someone is already using the spawn points!", plr, 255, 0, 0)
  150.             return false
  151.         end
  152.         if (showSpawns[plr]) then
  153.             showSpawn(plr, "showspawns", true)
  154.             outputChatBox("Disable all spawnPoint!", plr, 0, 255, 0)
  155.         end
  156.         if (misc == "dm") then
  157.             removeEventHandler("onPlayerSpawn", root, plrSpawn)
  158.             addEventHandler("onPlayerSpawn", root, plrSpawn)
  159.             status = true
  160.             info = misc
  161.             outputChatBox("Started the death match event!", plr, 0, 255, 0)
  162.             return true
  163.         end
  164.         for i, v in pairs(spawnPoint) do
  165.             local x, y, z = v[1], v[2], v[3]
  166.             local r, g, b = 255, 255, 255
  167.             if (v[5] ~= "all") then
  168.                 local team = findTeam(v[5])
  169.                 r, g, b = getTeamColor(team)
  170.             end
  171.             local plrs = exports.CITutil:getPlayersInDimension(336)
  172.             for i, plr in pairs(plrs) do
  173.                 local marker = createMarker(x, y, z-1, "cylinder", 4, r, g, b, 0, plr)
  174.                 tempSpawns[marker] = {v[4], v[5]}
  175.                 setElementInterior(marker, v[4])
  176.                 setElementDimension(marker, 336)
  177.                 addEventHandler("onMarkerHit", marker, hitCheckPoint)
  178.                 if (hasObjectPermissionTo(plr, "command.em", false)) then
  179.                     spawned[plr] = 0
  180.                 end
  181.             end
  182.             removeEventHandler("onVehicleExplode", root, explodeVeh)
  183.             addEventHandler("onVehicleExplode", root, explodeVeh)
  184.         end
  185.         addCommandHandler("backpos", backPos)
  186.         status = true
  187.         info = misc
  188.         outputChatBox("Started the race event!", plr, 0, 255, 0)
  189.         return true
  190.     end
  191.     if (not status) then
  192.         outputChatBox("Error: Nobody is using the event!", plr, 255, 0, 0)
  193.         return false
  194.     end
  195.     spawned = {}
  196.     if (info == "dm") then
  197.         for i, v in pairs(tempSpawns) do
  198.             if (isElement(i)) then
  199.                 destroyElement(i)
  200.             end
  201.         end
  202.         tempSpawns = {}
  203.         spawnPoint = {}
  204.         removeCommandHandler("backpos", backPos)
  205.     end
  206.     info, status = false, false
  207.     outputChatBox("Cleaning the spawn points!", plr, 0, 255, 0)
  208. end
  209. addCommandHandler("emspawn", usageSpawns)
  210.  
  211. --DM section
  212. function plrSpawn()
  213.     if (not spawned[source] or spawned[source] > limit) then
  214.         return false
  215.     end
  216.     local x, y, z, int = getNearestSpawn(source, getElementPosition(source))
  217.     setTimer(respawnPlr, 5000, 1, source, x, y, z, int)
  218.     if (not limit or limit == 0) then
  219.         return false
  220.     end
  221. end
  222.  
  223. function respawnPlr(plr, x, y, z, int)
  224.     if (not plr or not x or not y or not z or not int) then
  225.         return false
  226.     end
  227.     setElementDimension(plr, 336)
  228.     setElementPosition(plr, x, y, z)
  229.     setElementInterior(plr, int)
  230.     if (spawned[plr] >= limit) then
  231.         executeCommandHandler("leaveevent", plr)
  232.         spawned[plr] = nil
  233.     end
  234. end
  235.  
  236. --Race Section
  237. function hitCheckPoint(hitElement, dim)
  238.     if (getElementType(hitElement) ~= "player" and getElementType(hitElement) ~= "vehicle" or not dim) then
  239.         return false
  240.     end
  241.     local plr
  242.     if (getElementType(hitElement) == "vehicle") then
  243.         plr = getVehicleController(hitElement)
  244.     end
  245.     if (getElementType(hitElement) == "player") then
  246.         plr = hitElement
  247.     end
  248.     if (tempSpawns[source][2] ~= "all") then
  249.         local team = findTeam(tempSpawns[source][2])
  250.         if (team ~= getPlayerTeam(plr)) then
  251.             return false
  252.         end
  253.     end
  254.     local x, y, z = getElementPosition(plr)
  255.     local rotx, roty, rotz = getElementRotation(plr)
  256.     spawned[plr] = {x, y, z, rotx, roty, rotz, tempSpawns[source][1]}
  257. end
  258.  
  259. function backPos(plr)
  260.     if (getElementDimension(plr) ~= 336) then
  261.         return false
  262.     end
  263.     if (not spawned[plr]) then
  264.         outputChatBox("You did not touch at spawn point!", plr, 255, 0, 0)
  265.         return false
  266.     end
  267.     local x, y, z, rotx, roty, rotz, int = spawned[plr][1], spawned[plr][2], spawned[plr][3], spawned[plr][4], spawned[plr][5], spawned[plr][6], spawned[plr][7]
  268.     local veh = getPedOccupiedVehicle(plr)
  269.     if (veh) then
  270.         setElementPosition(veh, x, y, z)
  271.         setElementRotation(veh, rotx, roty, rotz)
  272.         setElementDimension(veh, int)
  273.         fixVehicle(veh)
  274.         outputChatBox("Back to your last checkpoint!", plr, 0, 255, 0)
  275.         return true
  276.     end
  277.     setElementPosition(plr, x, y, z)
  278.     setElementRotation(plr, rotx, roty, rotz)
  279.     setElementDimension(plr, int)
  280.     outputChatBox("Back to your last checkpoint!", plr, 0, 255, 0)
  281. end
  282.  
  283. function plrLeaveEvent()
  284.     if (spawned[source]) then
  285.         spawned[source] = nil
  286.     end
  287. end
  288. addEventHandler("onPlayerQuit", root, plrLeaveEvent)
  289.  
  290. function explodeVeh()
  291.     if (getVehicleOccupant(source, 0) and getElementDimension(source) == 336) then
  292.         local plr = getVehicleController(source)
  293.         if (not spawned[plr]) then
  294.             return false
  295.         end
  296.         spawned[plr] = nil
  297.     end
  298. end
  299.  
  300. --Useful export
  301. function getNearestSpawn(plr, x, y, z)
  302.     local temp = {}
  303.     for i, v in pairs(spawnPoint) do
  304.         local x1, y1, z1 = v[1], v[2], v[3]
  305.         local dist = getDistanceBetweenPoints3D(x, y, z, x1, y1, z1)
  306.         if (v[5] ~= "all") then
  307.             local team = findTeam(v[5])
  308.             if (getPlayerTeam(plr) == team) then
  309.                 table.insert(temp, {i = i, dist = dist})
  310.             end
  311.         else
  312.             table.insert(temp, {i = i, dist = dist})
  313.         end
  314.     end
  315.     table.sort (
  316.         temp,
  317.         function( a, b )
  318.             return((a.dist or 0) < (b.dist or 0))
  319.         end
  320.     )
  321.     return unpack(spawnPoint[temp[1].i])
  322. end
  323.  
  324. function findTeam(name)
  325.     local name = name and name:gsub("#%x%x%x%x%x%x", ""):lower() or nil
  326.     if name then
  327.         for _, team in ipairs(getElementsByType("team")) do
  328.             local name_ = getTeamName(team):gsub("#%x%x%x%x%x%x", ""):lower()
  329.             if name_:find(name, 1, true) then
  330.                 return team
  331.             end
  332.         end
  333.     end
  334. end
Advertisement
Add Comment
Please, Sign In to add comment