Guest User

Elevator-system

a guest
Sep 26th, 2012
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.99 KB | None | 0 0
  1. mysql = exports.mysql
  2.  
  3. addEvent("onPlayerInteriorChange", true)
  4.  
  5. -- Defines
  6. INTERIOR_X = 1
  7. INTERIOR_Y = 2
  8. INTERIOR_Z = 3
  9. INTERIOR_INT = 4
  10. INTERIOR_DIM = 5
  11. INTERIOR_ANGLE = 6
  12. INTERIOR_FEE = 7
  13.  
  14. INTERIOR_TYPE = 1
  15. INTERIOR_DISABLED = 2
  16. INTERIOR_LOCKED = 3
  17. INTERIOR_OWNER = 4
  18. INTERIOR_COST = 5
  19. INTERIOR_SUPPLIES = 6
  20.  
  21. -- Small hack
  22. function setElementDataEx(source, field, parameter, streamtoall, streamatall)
  23. exports['anticheat-system']:changeProtectedElementDataEx( source, field, parameter, streamtoall, streamatall)
  24. end
  25. -- End small hack
  26.  
  27.  
  28. function createElevator(thePlayer, commandName, interior, dimension, ix, iy, iz)
  29. if (exports.global:isPlayerSuperAdmin(thePlayer)) or (exports.donators:hasPlayerPerk(thePlayer,14) and exports.global:isPlayerFullAdmin(thePlayer)) then
  30. if not (interior) or not (dimension) or not (ix) or not (iy) or not (iz) then
  31. outputChatBox("SYNTAX: /" .. commandName .. " [Interior ID] [Dimension ID] [X] [Y] [Z]", thePlayer, 255, 194, 14)
  32. else
  33. local x, y, z = getElementPosition(thePlayer)
  34.  
  35. interior = tonumber(interior)
  36. dimension = tonumber(dimension)
  37. local interiorwithin = getElementInterior(thePlayer)
  38. local dimensionwithin = getElementDimension(thePlayer)
  39. ix = tonumber(ix)
  40. iy = tonumber(iy)
  41. iz = tonumber(iz)
  42. id = SmallestElevatorID()
  43. if id then
  44. 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) .. "'")
  45. if (query) then
  46. --reloadOneElevator(id, true)
  47. loadOneElevator(id)
  48. outputChatBox("Elevator created with ID #" .. id .. "!", thePlayer, 0, 255, 0)
  49. end
  50. else
  51. outputChatBox("There was an error while creating an elevator. Try again.", thePlayer, 255, 0, 0)
  52. end
  53. end
  54. end
  55. end
  56. addCommandHandler("addelevator", createElevator, false, false)
  57.  
  58. function findElevator(elevatorID)
  59. elevatorID = tonumber(elevatorID)
  60. if elevatorID > 0 then
  61. local possibleInteriors = getElementsByType("elevator")
  62. for _, elevator in ipairs(possibleInteriors) do
  63. local eleID = getElementData(elevator, "dbid")
  64. if eleID == elevatorID then
  65. local elevatorEntrance = getElementData(elevator, "entrance")
  66. local elevatorExit = getElementData(elevator, "exit")
  67. local elevatorStatus = getElementData(elevator, "status")
  68.  
  69. return elevatorID, elevatorEntrance, elevatorExit, elevatorStatus, elevator
  70. end
  71. end
  72. end
  73. return 0
  74. end
  75.  
  76. function findElevatorElement(elevatorID)
  77. elevatorID = tonumber(elevatorID)
  78. if elevatorID > 0 then
  79. local possibleInteriors = getElementsByType("elevator")
  80. for _, elevator in ipairs(possibleInteriors) do
  81. local eleID = getElementData(elevator, "dbid")
  82. if eleID == elevatorID then
  83. return elevator
  84. end
  85. end
  86. end
  87. return false
  88. end
  89.  
  90. function reloadOneElevator(elevatorID, skipcheck)
  91. local dbid, entrance, exit, status, elevatorElement = findElevator( elevatorID )
  92. if (dbid > 0 or skipcheck)then
  93. local realElevatorElement = findElevatorElement(dbid)
  94. if not realElevatorElement then
  95. outputDebugString("[reloadOneElevator] Can't find element")
  96. end
  97. triggerClientEvent("deleteInteriorElement", realElevatorElement, tonumber(dbid))
  98. destroyElement(realElevatorElement)
  99. loadOneElevator(tonumber(dbid), false)
  100. else
  101. --outputDebugString("You suckx2")
  102. outputDebugString("Tried to reload elevator without ID.")
  103. end
  104. end
  105.  
  106. function loadOneElevator(elevatorID, hasCoroutine)
  107. if (hasCoroutine==nil) then
  108. hasCoroutine = false
  109. end
  110.  
  111. local row = mysql:query_fetch_assoc("SELECT id, x, y, z, tpx, tpy, tpz, dimensionwithin, interiorwithin, dimension, interior, car, disabled FROM `elevators` WHERE id = " .. elevatorID )
  112. if row then
  113. --if (hasCoroutine) then
  114. -- coroutine.yield()
  115. --end
  116.  
  117. if row then
  118. for k, v in pairs( row ) do
  119. if v == null then
  120. row[k] = nil
  121. else
  122. row[k] = tonumber(v) or v
  123. end
  124. end
  125.  
  126. local elevatorElement = createElement("elevator", "ele"..tostring(row.id))
  127. setElementDataEx(elevatorElement, "dbid", row.id, true)
  128.  
  129. -- X Y Z Interior Dimension Angle Entree fee
  130. setElementDataEx(elevatorElement, "entrance", { row.x, row.y, row.z, row.interiorwithin, row.dimensionwithin, 0, 0 }, true )
  131. setElementDataEx(elevatorElement, "exit", { row.tpx, row.tpy, row.tpz, row.interior, row.dimension, 0, 0 }, true )
  132.  
  133. -- Type Is diabled?
  134. setElementDataEx(elevatorElement, "status", { row.car, row.disabled == 1 } , true )
  135. setElementDataEx(elevatorElement, "name", row.name, true )
  136. return true
  137. end
  138. end
  139. end
  140.  
  141. function loadAllElevators(res)
  142. local result = mysql:query("SELECT id FROM elevators")
  143. local counter = 0
  144.  
  145. if (result) then
  146. while true do
  147. local row = mysql:fetch_assoc(result)
  148. if not row then break end
  149. loadOneElevator(row.id)
  150. end
  151. mysql:free_result(result)
  152. end
  153. end
  154. addEventHandler("onResourceStart", getResourceRootElement(), loadAllElevators)
  155.  
  156. function isInteriorLocked(dimension)
  157. local result = mysql:query_fetch_assoc("SELECT type, locked FROM `interiors` WHERE id = " .. mysql:escape_string(dimension))
  158. local locked = false
  159. if result then
  160. if tonumber(result["rype"]) ~= 2 and tonumber(result["locked"]) == 1 then
  161. locked = true
  162. end
  163. end
  164. return locked
  165. end
  166.  
  167. function enterElevator(goingin)
  168. local pickup = source
  169. local player = client
  170. if getElementType(pickup) ~= "elevator" then
  171. return
  172. end
  173.  
  174. local elevatorStatus = getElementData(pickup, "status")
  175. if elevatorStatus[INTERIOR_TYPE] == 3 then
  176. outputChatBox("You try the door handle, but it seems to be locked.", player, 255, 0,0, true)
  177. return
  178. end
  179.  
  180. vehicle = getPedOccupiedVehicle( player )
  181. if ( ( vehicle and elevatorStatus[INTERIOR_TYPE] ~= 0 and getVehicleOccupant( vehicle ) == player ) or not vehicle ) then
  182. if not vehicle and elevatorStatus[INTERIOR_TYPE] == 2 then
  183. outputChatBox( "This entrance is for vehicles only.", player, 255, 0, 0 )
  184. return
  185. end
  186.  
  187. if elevatorStatus[INTERIOR_DISABLED] then
  188. outputChatBox( "This interior is currently disabled.", player, 255, 0, 0 )
  189. return
  190. end
  191.  
  192. local currentCP = nil
  193. local otherCP = nil
  194. if goingin then
  195. currentCP = getElementData(pickup, "entrance")
  196. otherCP = getElementData(pickup, "exit")
  197. else
  198. currentCP = getElementData(pickup, "exit")
  199. otherCP = getElementData(pickup, "entrance")
  200. end
  201.  
  202. local locked = false
  203. if currentCP[INTERIOR_DIM] == 0 and otherCP[INTERIOR_DIM] ~= 0 then -- entering a house
  204. locked = isInteriorLocked(otherCP[INTERIOR_DIM])
  205. elseif currentCP[INTERIOR_DIM] ~= 0 and otherCP[INTERIOR_DIM] == 0 then -- leaving a house
  206. locked = isInteriorLocked(currentCP[INTERIOR_DIM])
  207. elseif currentCP[INTERIOR_DIM] ~= 0 and otherCP[INTERIOR_DIM] ~= 0 and currentCP[INTERIOR_DIM] ~= otherCP[INTERIOR_DIM] then -- changing between two houses
  208. locked = isInteriorLocked(currentCP[INTERIOR_DIM]) or isInteriorLocked(otherCP[INTERIOR_DIM])
  209. else -- Moving in the same dimension
  210. locked = false
  211. end
  212.  
  213. if locked then
  214. outputChatBox("You try the door handle, but it seems to be locked.", player, 255, 0,0, true)
  215. return
  216. end
  217.  
  218. -- check for entrance fee
  219. local dbid, entrance, exit, interiorType, interiorElement = call( getResourceFromName( "interior-system" ), "findProperty", player, otherCP[INTERIOR_DIM] )
  220. if dbid > 0 then
  221. local interiorEntrance = getElementData(interiorElement, "entrance")
  222. local interiorStatus = getElementData(interiorElement, "status")
  223. if currentCP[INTERIOR_DIM] ~= otherCP[INTERIOR_DIM] and interiorElement then
  224. if getElementData( player, "adminduty" ) ~= 1 and not exports.global:hasItem( player, 5, otherCP[INTERIOR_DIM] ) then
  225. if interiorEntrance[INTERIOR_FEE] and interiorEntrance[INTERIOR_FEE] > 0 then
  226. if not exports.global:takeMoney( player, interiorEntrance[INTERIOR_FEE] ) then
  227. outputChatBox( "You don't have enough money with you to enter this interior.", player, 255, 0, 0 )
  228. return
  229. else
  230. local ownerid = interiorStatus[INTERIOR_OWNER]
  231. local query = mysql:query_free("UPDATE characters SET bankmoney = bankmoney + '" .. mysql:escae_string(interiorEntrance[INTERIOR_FEE]) .. "' WHERE id = '" .. mysql:escape_string(ownerid).."'" )
  232. if query then
  233. for k, v in pairs( getElementsByType( "player" ) ) do
  234. if isElement( v ) then
  235. if getElementData( v, "dbid" ) == ownerid then
  236. exports['anticheat-system']:changeProtectedElementDataEx( v, "businessprofit", getElementData( v, "businessprofit" ) + interiorEntrance[INTERIOR_FEE], false )
  237. break
  238. end
  239. end
  240. end
  241. else
  242. outputChatBox( "Error 9019 - Report on Forums.", player, 255, 0, 0 )
  243. end
  244. end
  245. end
  246. end
  247. end
  248. else
  249. dbid, entrance, exit, interiorType, interiorElement = call( getResourceFromName( "interior-system" ), "findProperty", player, currentCP[INTERIOR_DIM] )
  250. end
  251.  
  252. if vehicle then
  253. exports['anticheat-system']:changeProtectedElementDataEx(vehicle, "health", getElementHealth(vehicle), false)
  254. for i = 0, getVehicleMaxPassengers( vehicle ) do
  255. local p = getVehicleOccupant( vehicle )
  256. if p then
  257. triggerClientEvent( p, "CantFallOffBike", p )
  258. end
  259. end
  260. else
  261. end
  262.  
  263. if vehicle then
  264. setTimer(warpVehicleIntoInteriorfunction, 500, 1, vehicle, otherCP[INTERIOR_INT], otherCP[INTERIOR_DIM], 2, otherCP[INTERIOR_X],otherCP[INTERIOR_Y],otherCP[INTERIOR_Z],currentCP,otherCP)
  265. elseif isElement(player) then
  266. triggerClientEvent(player, "setPlayerInsideInterior", interiorElement or getRootElement(), otherCP, interiorElement or getRootElement())
  267. end
  268. end
  269. end
  270. addEvent("elevator:enter", true)
  271. addEventHandler("elevator:enter", getRootElement(), enterElevator)
  272.  
  273. function warpVehicleIntoInteriorfunction(vehicle, interior, dimension, offset, x,y,z,pickup,other)
  274. if isElement(vehicle) then
  275. local offset = getElementData(vehicle, "groundoffset") or 2
  276.  
  277. setElementPosition(vehicle, x, y, z - 1 + offset)
  278. setElementInterior(vehicle, interior)
  279. setElementDimension(vehicle, dimension)
  280. setElementVelocity(vehicle, 0, 0, 0)
  281. setVehicleTurnVelocity(vehicle, 0, 0, 0)
  282. local rx, ry, rz = getVehicleRotation(vehicle)
  283. setVehicleRotation(vehicle, 0, 0, rz)
  284. setTimer(setVehicleTurnVelocity, 50, 2, vehicle, 0, 0, 0)
  285. setElementHealth(vehicle, getElementData(vehicle, "health") or 1000)
  286. exports['anticheat-system']:changeProtectedElementDataEx(vehicle, "health")
  287. setElementFrozen(vehicle, true)
  288.  
  289. setTimer(setElementFrozen, 1000, 1, vehicle, false)
  290.  
  291. for i = 0, getVehicleMaxPassengers( vehicle ) do
  292. local player = getVehicleOccupant( vehicle, i )
  293. if player then
  294. setElementInterior(player, interior)
  295. setCameraInterior(player, interior)
  296. setElementDimension(player, dimension)
  297. setCameraTarget(player)
  298.  
  299. triggerEvent("onPlayerInteriorChange", player)
  300. exports['anticheat-system']:changeProtectedElementDataEx(player, "realinvehicle", 1, false)
  301. end
  302. end
  303. end
  304. end
  305.  
  306. function deleteElevator(thePlayer, commandName, id)
  307. if (exports.global:isPlayerSuperAdmin(thePlayer)) or (exports.donators:hasPlayerPerk(thePlayer,14) and exports.global:isPlayerFullAdmin(thePlayer)) then
  308. if not (tonumber(id)) then
  309. outputChatBox("SYNTAX: /" .. commandName .. " [ID]", thePlayer, 255, 194, 14)
  310. else
  311. id = tonumber(id)
  312.  
  313. local dbid, entrance, exit, status, elevatorElement = findElevator( id )
  314.  
  315. if elevatorElement then
  316. local query = mysql:query_free("DELETE FROM elevators WHERE id='" .. mysql:escape_string(dbid) .. "'")
  317. if query then
  318. reloadOneElevator(dbid)
  319. outputChatBox("Elevator #" .. id .. " Deleted!", thePlayer, 0, 255, 0)
  320. else
  321. outputChatBox("ELE0015 Error, please report to a scripter.", thePlayer, 255, 0, 0)
  322. end
  323. else
  324. outputChatBox("Elevator ID does not exist!", thePlayer, 255, 0, 0)
  325. end
  326. end
  327. end
  328. end
  329. addCommandHandler("delelevator", deleteElevator, false, false)
  330.  
  331. function getNearbyElevators(thePlayer, commandName)
  332. if (exports.global:isPlayerAdmin(thePlayer)) then
  333. local posX, posY, posZ = getElementPosition(thePlayer)
  334. local dimension = getElementDimension(thePlayer)
  335. outputChatBox("Nearby Elevators:", thePlayer, 255, 126, 0)
  336. local found = false
  337.  
  338. local possibleElevators = getElementsByType("elevator")
  339. for _, elevator in ipairs(possibleElevators) do
  340. local elevatorEntrance = getElementData(elevator, "entrance")
  341. local elevatorExit = getElementData(elevator, "exit")
  342.  
  343. for _, point in ipairs( { elevatorEntrance, elevatorExit } ) do
  344. if (point[INTERIOR_DIM] == dimension) then
  345. local distance = getDistanceBetweenPoints3D(posX, posY, posZ, point[INTERIOR_X], point[INTERIOR_Y], point[INTERIOR_Z])
  346. if (distance <= 11) then
  347. local dbid = getElementData(elevator, "dbid")
  348. if point == elevatorEntrance then
  349. outputChatBox(" ID " .. dbid ..", leading to dimension "..elevatorExit[INTERIOR_DIM], thePlayer, 255, 126, 0)
  350. else
  351. outputChatBox(" ID " .. dbid ..", leading to dimension "..elevatorEntrance[INTERIOR_DIM], thePlayer, 255, 126, 0)
  352. end
  353.  
  354. found = true
  355. end
  356. end
  357. end
  358. end
  359.  
  360. if not found then
  361. outputChatBox(" None.", thePlayer, 255, 126, 0)
  362. end
  363. end
  364. end
  365. addCommandHandler("nearbyelevators", getNearbyElevators, false, false)
  366.  
  367. function SmallestElevatorID( ) -- finds the smallest ID in the SQL instead of auto increment
  368. 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")
  369. if result then
  370. return tonumber(result["nextID"])
  371. end
  372. return false
  373. end
  374.  
  375. addEvent( "toggleCarTeleportMode", false )
  376. addEventHandler( "toggleCarTeleportMode", getRootElement(),
  377. function( player )
  378. local elevatorStatus = getElementData(source, "status")
  379. local mode = ( elevatorStatus[INTERIOR_TYPE] + 1 ) % 4
  380. local query = mysql:query_free("UPDATE elevators SET car = " .. mysql:escape_string(mode) .. " WHERE id = " .. mysql:escape_string(getElementData( source, "dbid" )) )
  381. if query then
  382. elevatorStatus[INTERIOR_TYPE] = mode
  383. exports['anticheat-system']:changeProtectedElementDataEx( source, "status", elevatorStatus, false )
  384. if mode == 0 then
  385. outputChatBox( "You changed the mode to 'players only'.", player, 0, 255, 0 )
  386. elseif mode == 1 then
  387. outputChatBox( "You changed the mode to 'players and vehicles'.", player, 0, 255, 0 )
  388. elseif mode == 2 then
  389. outputChatBox( "You changed the mode to 'vehicles only'.", player, 0, 255, 0 )
  390. else
  391. outputChatBox( "You changed the mode to 'no entrance'.", player, 0, 255, 0 )
  392. end
  393. else
  394. outputChatBox( "Error 9019 - Report on Forums.", player, 255, 0, 0 )
  395. end
  396. end
  397. )
  398.  
  399. function toggleElevator( thePlayer, commandName, id )
  400. if exports.global:isPlayerSuperAdmin( thePlayer ) then
  401. id = tonumber( id )
  402. if not id then
  403. outputChatBox( "SYNTAX: /" .. commandName .. " [ID]", thePlayer, 255, 194, 14 )
  404. else
  405. local dbid, entrance, exit, status, elevatorElement = findElevator( id )
  406.  
  407. if elevatorElement then
  408. if status[INTERIOR_DISABLED] then
  409. mysql:query_free("UPDATE elevators SET disabled = 0 WHERE id = " .. mysql:escape_string(dbid) )
  410. else
  411. mysql:query_free("UPDATE elevators SET disabled = 1 WHERE id = " .. mysql:escape_string(dbid) )
  412. end
  413. reloadOneElevator(dbid)
  414.  
  415. else
  416. outputChatBox( "Elevator not found.", thePlayer, 255, 194, 14 )
  417. end
  418. end
  419. end
  420. end
  421. addCommandHandler( "toggleelevator", toggleElevator )
Advertisement
Add Comment
Please, Sign In to add comment