Guest User

Untitled

a guest
Feb 6th, 2012
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.51 KB | None | 0 0
  1. mysql = exports.mysql
  2.  
  3. addEvent("onPlayerInteriorChange", true)
  4.  
  5. function createElevator(thePlayer, commandName, interior, dimension, ix, iy, iz)
  6.     if (exports.global:isPlayerSuperAdmin(thePlayer)) then
  7.         if not (interior) or not (dimension) or not (ix) or not (iy) or not (iz) then
  8.             outputChatBox("SYNTAX: /" .. commandName .. " [Interior ID] [Dimension ID] [X] [Y] [Z]", thePlayer, 255, 194, 14)
  9.         else
  10.             local x, y, z = getElementPosition(thePlayer)
  11.            
  12.             interior = tonumber(interior)
  13.             dimension = tonumber(dimension)
  14.             local interiorwithin = getElementInterior(thePlayer)
  15.             local dimensionwithin = getElementDimension(thePlayer)
  16.             ix = tonumber(ix)
  17.             iy = tonumber(iy)
  18.             iz = tonumber(iz)
  19.             id = SmallestElevatorID()
  20.             if id then
  21.                 local query = mysql:query_free("INSERT INTO elevators SET id='" .. mysql:escape_string(id) .. "', x='" .. mysql:escape_string(x) .. "', y='" .. mysql:escape_string(y) .. "', z='" .. mysql:escape_string(z) .. "', tpx='" .. mysql:escape_string(ix) .. "', tpy='" .. mysql:escape_string(iy) .. "', tpz='" .. mysql:escape_string(iz) .. "', dimensionwithin='" .. mysql:escape_string(dimensionwithin) .. "', interiorwithin='" .. mysql:escape_string(interiorwithin) .. "', dimension='" .. mysql:escape_string(dimension) .. "', interior='" .. mysql:escape_string(interior) .. "'")
  22.                 if (query) then
  23.                     local pickup = createPickup(x, y, z, 3, 1318)
  24.                     exports.pool:allocateElement(pickup)
  25.                     local intpickup = createPickup(ix, iy, iz, 3, 1318)
  26.                     exports.pool:allocateElement(intpickup)
  27.                    
  28.                     exports['anticheat-system']:changeProtectedElementDataEx(pickup, "dbid", id, false)
  29.                     exports['anticheat-system']:changeProtectedElementDataEx(pickup, "other", intpickup)
  30.                     exports['anticheat-system']:changeProtectedElementDataEx(pickup, "car", 0, false)
  31.                     setElementInterior(pickup, interiorwithin)
  32.                     setElementDimension(pickup, dimensionwithin)
  33.                    
  34.                     exports['anticheat-system']:changeProtectedElementDataEx(intpickup, "dbid", id, false)
  35.                     exports['anticheat-system']:changeProtectedElementDataEx(intpickup, "other", pickup)
  36.                     exports['anticheat-system']:changeProtectedElementDataEx(intpickup, "car", 0, false)
  37.                     setElementInterior(intpickup, interior)
  38.                     setElementDimension(intpickup, dimension)
  39.                    
  40.                     outputChatBox("Elevator created with ID #" .. id .. "!", thePlayer, 0, 255, 0)
  41.                 end
  42.             else
  43.                 outputChatBox("There was an error while creating an elevator. Try again.", thePlayer, 255, 0, 0)
  44.             end
  45.         end
  46.     end
  47. end
  48. addCommandHandler("addelevator", createElevator, false, false)
  49.  
  50. function loadAllElevators(res)
  51.     local result = mysql:query("SELECT id, x, y, z, tpx, tpy, tpz, dimensionwithin, interiorwithin, dimension, interior, car, disabled FROM elevators")
  52.     local counter = 0
  53.    
  54.     if (result) then
  55.         while true do
  56.             local row = mysql:fetch_assoc(result)
  57.             if not row then break end
  58.            
  59.             local id = tonumber(row["id"])
  60.             local x = tonumber(row["x"])
  61.             local y = tonumber(row["y"])
  62.             local z = tonumber(row["z"])
  63.            
  64.             local ix = tonumber(row["tpx"])
  65.             local iy = tonumber(row["tpy"])
  66.             local iz = tonumber(row["tpz"])
  67.            
  68.             local dimensionwithin = tonumber(row["dimensionwithin"])
  69.             local interiorwithin = tonumber(row["interiorwithin"])
  70.            
  71.             local dimension = tonumber(row["dimension"])
  72.             local interior = tonumber(row["interior"])
  73.             local car = tonumber(row["car"])
  74.             local disabled = tonumber(row["disabled"])
  75.            
  76.             local pickup = createPickup(x, y, z, 3, disabled == 1 and 1314 or 1318)
  77.             exports.pool:allocateElement(pickup)
  78.             local intpickup = createPickup(ix, iy, iz, 3, disabled == 1 and 1314 or 1318)
  79.             exports.pool:allocateElement(intpickup)
  80.            
  81.             exports['anticheat-system']:changeProtectedElementDataEx(pickup, "dbid", id, false)
  82.             exports['anticheat-system']:changeProtectedElementDataEx(pickup, "other", intpickup)
  83.             exports['anticheat-system']:changeProtectedElementDataEx(pickup, "car", car, false)
  84.             setElementInterior(pickup, interiorwithin)
  85.             setElementDimension(pickup, dimensionwithin)
  86.                
  87.             exports['anticheat-system']:changeProtectedElementDataEx(intpickup, "dbid", id, false)
  88.             exports['anticheat-system']:changeProtectedElementDataEx(intpickup, "other", pickup)
  89.             exports['anticheat-system']:changeProtectedElementDataEx(intpickup, "car", car, false)
  90.             setElementInterior(intpickup, interior)
  91.             setElementDimension(intpickup, dimension)
  92.             counter = counter + 1
  93.         end
  94.         mysql:free_result(result)
  95.     end
  96. end
  97. addEventHandler("onResourceStart", getResourceRootElement(), loadAllElevators)
  98.  
  99. function hitInteriorPickup( thePlayer )
  100.     local pickuptype = getElementData(source, "type")
  101.    
  102.     local pdimension = getElementDimension(thePlayer)
  103.     local idimension = getElementDimension(source)
  104.    
  105.     if pdimension == idimension then -- same dimension?
  106.         local dbid, thePickup, theExit = call( getResourceFromName( "interior-system" ), "findProperty", player, getElementDimension( getElementData( source, "other" ) ) )
  107.         if thePickup and getElementDimension( thePickup ) ~= getElementDimension( theExit ) then
  108.             local name = getElementData( thePickup, "name" )
  109.            
  110.             if name then
  111.                 local owner = getElementData( thePickup, "owner" )
  112.                 local cost = getElementData( thePickup, "cost" )
  113.                
  114.                 local ownerName = exports['cache']:getCharacterName( owner ) or "None"
  115.                 triggerClientEvent(thePlayer, "displayInteriorName", thePlayer, name, ownerName, getElementData( thePickup, "inttype" ), cost, getElementData( thePickup, "fee" ) )
  116.             end
  117.         end
  118.        
  119.         bindKeys( thePlayer, source )
  120.         setTimer( checkLeavePickup, 500, 1, thePlayer, source )
  121.     end
  122.     cancelEvent()
  123. end
  124. addEventHandler("onPickupHit", getResourceRootElement(), hitInteriorPickup)
  125.  
  126. function isInPickup( thePlayer, thePickup, distance )
  127.     local ax, ay, az = getElementPosition(thePlayer)
  128.     local bx, by, bz = getElementPosition(thePickup)
  129.    
  130.     return getDistanceBetweenPoints3D(ax, ay, az, bx, by, bz) < ( distance or 2 ) and getElementInterior(thePlayer) == getElementInterior(thePickup) and getElementDimension(thePlayer) == getElementDimension(thePickup)
  131. end
  132.  
  133. function checkLeavePickup( thePlayer, thePickup )
  134.     if isElement( thePlayer ) then
  135.         if isInPickup( thePlayer, thePickup ) then
  136.             setTimer(checkLeavePickup, 1000, 1, thePlayer, thePickup)
  137.         else
  138.             unbindKeys(thePlayer, thePickup)
  139.         end
  140.     end
  141. end
  142.  
  143. function func (player, f, down, player, pickup) enterElevator(player, pickup) end
  144.  
  145. function bindKeys(player, pickup)
  146.     if (isElement(player)) then
  147.         if not(isKeyBound(player, "enter", "down", func)) then
  148.             bindKey(player, "enter", "down", func, player, pickup)
  149.         end
  150.        
  151.         if not(isKeyBound(player, "f", "down", func)) then
  152.             bindKey(player, "f", "down", func, player, pickup)
  153.         end
  154.        
  155.         exports['anticheat-system']:changeProtectedElementDataEx( player, "interiormarker", true, false )
  156.     end
  157. end
  158.  
  159. function unbindKeys(player, pickup)
  160.     if (isElement(player)) then
  161.         if (isKeyBound(player, "enter", "down", func)) then
  162.             unbindKey(player, "enter", "down", func, player, pickup)
  163.         end
  164.        
  165.         if (isKeyBound(player, "f", "down", func)) then
  166.             unbindKey(player, "f", "down", func, player, pickup)
  167.         end
  168.        
  169.         exports['anticheat-system']:changeProtectedElementDataEx( player, "interiormarker" )
  170.         triggerClientEvent( player, "displayInteriorName", player )
  171.     end
  172. end
  173.  
  174.  
  175. function isInteriorLocked(dimension)
  176.     local result = mysql:query_fetch_assoc("SELECT type, locked FROM `interiors` WHERE id = " .. mysql:escape_string(dimension))
  177.     local locked = false
  178.     if result then
  179.         if tonumber(result["rype"]) ~= 2 and tonumber(result["locked"]) == 1 then
  180.             locked = true
  181.         end
  182.     end
  183.     return locked
  184. end
  185.  
  186. --[[
  187. Car Teleport Modes:
  188. 0: players only
  189. 1: players and vehicles
  190. 2: vehicles only
  191. 3: no entrance
  192. ]]--
  193. function enterElevator(player, pickup)
  194.     local cartp = getElementData( pickup, "car" )
  195.     if cartp == 3 then
  196.         outputChatBox("You try the door handle, but it seems to be locked.", player, 255, 0,0, true)
  197.         return
  198.     end
  199.    
  200.     vehicle = getPedOccupiedVehicle( player )
  201.     if isInPickup ( player, pickup ) and ( ( vehicle and cartp ~= 0 and getVehicleOccupant( vehicle ) == player ) or not vehicle ) then
  202.         if not vehicle and cartp == 2 then
  203.             outputChatBox( "This entrance is for vehicles only.", player, 255, 0, 0 )
  204.             return
  205.         end
  206.        
  207.         if getElementModel( pickup ) == 1314 then
  208.             outputChatBox( "This interior is currently disabled.", player, 255, 0, 0 )
  209.             return
  210.         end
  211.        
  212.         local other = getElementData( pickup, "other" )
  213.        
  214.         local x, y, z = getElementPosition( other )
  215.         local interior = getElementInterior( other )
  216.         local dimension = getElementDimension( other )
  217.        
  218.         -- find the pickup inside to see if the house is locked
  219.         local ldimension = getElementDimension( pickup )
  220.        
  221.         local locked = false
  222.         if ldimension == 0 and dimension ~= 0 then -- entering a house
  223.             locked = isInteriorLocked(dimension)
  224.         elseif ldimension ~= 0 and dimension == 0 then -- leaving a house
  225.             locked = isInteriorLocked(ldimension)
  226.         elseif ldimension ~= 0 and dimension ~= 0 and ldimension ~= dimension then -- changing between two houses
  227.             locked = isInteriorLocked(ldimension) or isInteriorLocked(dimension)
  228.         else -- outside
  229.             locked = false
  230.         end
  231.        
  232.         if locked then
  233.             outputChatBox("You try the door handle, but it seems to be locked.", player, 255, 0,0, true)
  234.             return
  235.         end
  236.        
  237.         -- check for entrance fee
  238.         local dbid, thePickup = call( getResourceFromName( "interior-system" ), "findProperty", player, dimension )
  239.         if dimension ~= ldimension and thePickup then
  240.             if getElementData( player, "adminduty" ) ~= 1 and not exports.global:hasItem( player, 5, getElementData( thePickup, "dbid" ) ) then
  241.                 local fee = getElementData( thePickup, "fee" )
  242.                 if fee and fee > 0 then
  243.                     if not exports.global:takeMoney( player, fee ) then
  244.                         outputChatBox( "You don't have enough money with you to enter this interior.", player, 255, 0, 0 )
  245.                         return
  246.                     else
  247.                         local ownerid = getElementData( thePickup, "owner" )
  248.                         local query = mysql:query_free("UPDATE characters SET bankmoney = bankmoney + " .. mysql:escape_string(fee) .. " WHERE id = " .. mysql:escape_string(ownerid) )
  249.                         if query then
  250.                             for k, v in pairs( getElementsByType( "player" ) ) do
  251.                                 if isElement( v ) then
  252.                                     if getElementData( v, "dbid" ) == ownerid then
  253.                                         exports['anticheat-system']:changeProtectedElementDataEx( v, "businessprofit", getElementData( v, "businessprofit" ) + fee, false )
  254.                                         break
  255.                                     end
  256.                                 end
  257.                             end
  258.                         else
  259.                             outputChatBox( "Error 9019 - Report on Forums.", player, 255, 0, 0 )
  260.                         end
  261.                     end
  262.                 end
  263.             end
  264.         end
  265.        
  266.         if vehicle then
  267.             exports['anticheat-system']:changeProtectedElementDataEx(vehicle, "health", getElementHealth(vehicle), false)
  268.             for i = 0, getVehicleMaxPassengers( vehicle ) do
  269.                 local p = getVehicleOccupant( vehicle )
  270.                 if p then
  271.                     -- fade camera to black
  272.                     fadeCamera ( p, false, 1,0,0,0 )
  273.                    
  274.                     triggerClientEvent( p, "CantFallOffBike", p )
  275.                 end
  276.             end
  277.         else
  278.             -- fade camera to black
  279.             fadeCamera ( player, false, 1,0,0,0 )
  280.         end
  281.        
  282.         -- teleport the player during the black fade
  283.         if vehicle then
  284.             setTimer(function()
  285.                 if isElement(vehicle) then
  286.                     local offset = getElementData(vehicle, "groundoffset") or 2
  287.                     setElementInterior(vehicle, interior)
  288.                     setElementDimension(vehicle, dimension)
  289.                     setElementPosition(vehicle, x, y, z - 1 + offset)
  290.                     setElementVelocity(vehicle, 0, 0, 0)
  291.                     setVehicleTurnVelocity(vehicle, 0, 0, 0)
  292.                     local rx, ry, rz = getVehicleRotation(vehicle)
  293.                     setVehicleRotation(vehicle, 0, 0, rz)
  294.                     setTimer(setVehicleTurnVelocity, 50, 2, vehicle, 0, 0, 0)
  295.                    
  296.                     setElementHealth(vehicle, getElementData(vehicle, "health") or 1000)
  297.                     exports['anticheat-system']:changeProtectedElementDataEx(vehicle, "health")
  298.                     setVehicleFrozen(vehicle, true)
  299.                    
  300.                     setTimer(setVehicleFrozen, 2000, 1, vehicle, false)
  301.                    
  302.                     for i = 0, getVehicleMaxPassengers( vehicle ) do
  303.                         local player = getVehicleOccupant( vehicle, i )
  304.                         if player then
  305.                             setElementInterior(player, interior)
  306.                             setCameraInterior(player, interior)
  307.                             setElementDimension(player, dimension)
  308.                             setCameraTarget(player)
  309.                            
  310.                             triggerEvent("onPlayerInteriorChange", player, pickup, other)
  311.                            
  312.                             -- fade camera in
  313.                             setTimer(fadeCamera, 1000, 1 , player , true, 2)
  314.                            
  315.                             triggerClientEvent(player, "usedElevator", player)
  316.                             setPedFrozen(player, true)
  317.                             setPedGravity(player, 0)
  318.                         end
  319.                     end
  320.                 end
  321.             end, 1000, 1)
  322.         elseif isElement(player) then
  323.             setPedGravity( player, 0 )
  324.             triggerClientEvent(player, "setPlayerInsideInterior", pickup, other)
  325.         end
  326.         playSoundFrontEnd(player, 40)
  327.     end
  328. end
  329.  
  330. function resetGravity()
  331.     setTimer(setPedFrozen, 1000, 1, source, false)
  332.     setTimer(setPedGravity, 1000, 1, source, 0.008)
  333. end
  334. addEvent("resetGravity", true)
  335. addEventHandler("resetGravity", getRootElement(), resetGravity)
  336.  
  337. function deleteElevator(thePlayer, commandName, id)
  338.     if (exports.global:isPlayerSuperAdmin(thePlayer)) then
  339.         if not (id) then
  340.             outputChatBox("SYNTAX: /" .. commandName .. " [ID]", thePlayer, 255, 194, 14)
  341.         else
  342.             id = tonumber(id)
  343.            
  344.             local counter = 0
  345.             for k, thePickup in ipairs(getElementsByType("pickup", getResourceRootElement())) do
  346.                 local pickupID = tonumber(getElementData(thePickup, "dbid"))
  347.                 if pickupID == id then
  348.                     destroyElement(thePickup)
  349.                     counter = counter + 1
  350.                 end
  351.             end
  352.            
  353.             if (counter>0) then -- ID Exists
  354.                 mysql:query_free("DELETE FROM elevators WHERE id='" .. mysql:escape_string(id) .. "'")
  355.  
  356.                 outputChatBox("Elevator #" .. id .. " Deleted!", thePlayer, 0, 255, 0)
  357.             else
  358.                 outputChatBox("Elevator ID does not exist!", thePlayer, 255, 0, 0)
  359.             end
  360.         end
  361.     end
  362. end
  363. addCommandHandler("delelevator", deleteElevator, false, false)
  364.  
  365. function TempDelete(thePlayer, commandName)
  366.     if (exports.global:isPlayerSuperAdmin(thePlayer)) then
  367.         for k, thePickup in ipairs(getElementsByType("pickup", getResourceRootElement())) do
  368.             if isInPickup(thePlayer, thePickup) then
  369.                 local dbid = getElementData(thePickup, "dbid")
  370.                 local query = mysql:query_free( "DELETE FROM elevators WHERE id='" .. mysql:escape_string(dbid) .. "'")
  371.                 if (query) then
  372.                     outputChatBox(" Elevator deleted", thePlayer)
  373.                 end
  374.             end
  375.         end
  376.     end
  377. end
  378. addCommandHandler("tempdelete", TempDelete, false, false)
  379.  
  380. function getNearbyElevators(thePlayer, commandName)
  381.     if (exports.global:isPlayerAdmin(thePlayer)) then
  382.         local posX, posY, posZ = getElementPosition(thePlayer)
  383.         outputChatBox("Nearby Elevators:", thePlayer, 255, 126, 0)
  384.         local count = 0
  385.        
  386.         for k, thePickup in ipairs(getElementsByType("pickup", getResourceRootElement())) do
  387.             if isInPickup(thePlayer, thePickup, 10) then
  388.                 local dbid = getElementData(thePickup, "dbid")
  389.                 outputChatBox("   Elevator with ID " .. dbid .. ".", thePlayer, 255, 126, 0)
  390.                 count = count + 1
  391.             end
  392.         end
  393.        
  394.         if count == 0 then
  395.             outputChatBox("   None.", thePlayer, 255, 126, 0)
  396.         end
  397.     end
  398. end
  399. addCommandHandler("nearbyelevators", getNearbyElevators, false, false)
  400.  
  401. function SmallestElevatorID( ) -- finds the smallest ID in the SQL instead of auto increment
  402.     local result = mysql:query_fetch_assoc("SELECT MIN(e1.id+1) AS nextID FROM elevators AS e1 LEFT JOIN elevators AS e2 ON e1.id +1 = e2.id WHERE e2.id IS NULL")
  403.     if result then
  404.         return tonumber(result["nextID"])
  405.     end
  406.     return false
  407. end
  408.  
  409. addEvent( "toggleCarTeleportMode", false )
  410. addEventHandler( "toggleCarTeleportMode", getRootElement(),
  411.     function( player )
  412.         local mode = ( getElementData( source, "car" ) + 1 ) % 4
  413.         local query = mysql:query_free("UPDATE elevators SET car = " .. mysql:escape_string(mode) .. " WHERE id = " .. mysql:escape_string(getElementData( source, "dbid" )) )
  414.         if query then
  415.             if mode == 0 then
  416.                 outputChatBox( "You changed the mode to 'players only'.", player, 0, 255, 0 )
  417.             elseif mode == 1 then
  418.                 outputChatBox( "You changed the mode to 'players and vehicles'.", player, 0, 255, 0 )
  419.             elseif mode == 2 then
  420.                 outputChatBox( "You changed the mode to 'vehicles only'.", player, 0, 255, 0 )
  421.             else
  422.                 outputChatBox( "You changed the mode to 'no entrance'.", player, 0, 255, 0 )
  423.             end
  424.            
  425.             exports['anticheat-system']:changeProtectedElementDataEx( source, "car", mode, false )
  426.             exports['anticheat-system']:changeProtectedElementDataEx( getElementData( source, "other" ), "car", mode )
  427.         else
  428.             outputChatBox( "Error 9019 - Report on Forums.", player, 255, 0, 0 )
  429.         end
  430.     end
  431. )
  432.  
  433. function toggleElevator( thePlayer, commandName, id )
  434.     if exports.global:isPlayerSuperAdmin( thePlayer ) then
  435.         id = tonumber( id )
  436.         if not id then
  437.             outputChatBox( "SYNTAX: /" .. commandName .. " [ID]", thePlayer, 255, 194, 14 )
  438.         else
  439.             local pickup = nil
  440.             for k, thePickup in ipairs(getElementsByType("pickup", getResourceRootElement())) do
  441.                 local dbid = getElementData(thePickup, "dbid")
  442.                 if dbid == id then
  443.                     pickup = thePickup
  444.                     break
  445.                 end
  446.             end
  447.            
  448.             if pickup then
  449.                 if getElementModel( pickup ) == 1314 then
  450.                     mysql:query_free("UPDATE elevators SET disabled = 0 WHERE id = " .. mysql:escape_string(id) )
  451.                    
  452.                     setPickupType( pickup, 3, 1318 )
  453.                     setPickupType( getElementData( pickup, "other" ), 3, 1318 )
  454.                    
  455.                     outputChatBox( "Elevator #" .. id .. " enabled.", thePlayer, 0, 255, 0 )
  456.                 else
  457.                     mysql:query_free("UPDATE elevators SET disabled = 1 WHERE id = " .. mysql:escape_string(id) )
  458.                    
  459.                     setPickupType( pickup, 3, 1314 )
  460.                     setPickupType( getElementData( pickup, "other" ), 3, 1314 )
  461.                    
  462.                     outputChatBox( "Elevator #" .. id .. " disabled.", thePlayer, 255, 0, 0 )
  463.                 end
  464.             end
  465.         end
  466.     end
  467. end
  468. addCommandHandler( "toggleelevator", toggleElevator )
  469.  
  470. function enableAllElevators( thePlayer )
  471.     if exports.global:isPlayerLeadAdmin( thePlayer ) then
  472.         mysql:query_free("UPDATE elevators SET disabled = 0 WHERE disabled = 1" )
  473.         for k, thePickup in ipairs(getElementsByType("pickup", getResourceRootElement())) do
  474.             if getElementModel( thePickup ) == 1314 then
  475.                 setPickupType( thePickup, 3, 1318 )
  476.             end
  477.         end
  478.     end
  479. end
  480. addCommandHandler( "enableallelevators", enableAllElevators )
Advertisement
Add Comment
Please, Sign In to add comment