Guest User

Untitled

a guest
Apr 19th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.96 KB | None | 0 0
  1. g_Root = getRootElement()
  2. g_ResRoot = getResourceRootElement(getThisResource())
  3. g_PlayerData = {}
  4. g_VehicleData = {}
  5. local chatTime = {}
  6. local lastChatMessage = {}
  7.  
  8. g_ArmedVehicles = {
  9. [425] = true,
  10. [447] = true,
  11. [520] = true,
  12. [430] = true,
  13. [464] = true,
  14. [432] = true
  15. }
  16. g_Trailers = {
  17. [606] = true,
  18. [607] = true,
  19. [610] = true,
  20. [590] = true,
  21. [569] = true,
  22. [611] = true,
  23. [584] = true,
  24. [608] = true,
  25. [435] = true,
  26. [450] = true,
  27. [591] = true
  28. }
  29.  
  30. g_RPCFunctions = {
  31. addPedClothes = { option = 'clothes', descr = 'Modifying clothes' },
  32. addVehicleUpgrade = { option = 'upgrades', descr = 'Adding/removing upgrades' },
  33. fadeVehiclePassengersCamera = true,
  34. fixVehicle = { option = 'repair', descr = 'Repairing vehicles' },
  35. giveMeVehicles = { option = 'createvehicle', descr = 'Creating vehicles' },
  36. giveMeWeapon = { option = 'weapons.enabled', descr = 'Getting weapons' },
  37. givePedJetPack = { option = 'jetpack', descr = 'Getting a jetpack' },
  38. killPed = { option = 'kill', descr = 'Killing yourself' },
  39. removePedClothes = { option = 'clothes', descr = 'Modifying clothes' },
  40. removePedFromVehicle = true,
  41. removePedJetPack = { option = 'jetpack', descr = 'Removing a jetpack' },
  42. removeVehicleUpgrade = { option = 'upgrades', descr = 'Adding/removing upgrades' },
  43. setElementAlpha = { option = 'alpha', descr = 'Changing your alpha' },
  44. setElementPosition = true,
  45. setElementInterior = true,
  46. setMyGameSpeed = { option = 'gamespeed.enabled', descr = 'Setting game speed' },
  47. setMySkin = { option = 'setskin', descr = 'Setting skin' },
  48. setPedAnimation = { option = 'anim', descr = 'Setting an animation' },
  49. setPedFightingStyle = { option = 'setstyle', descr = 'Setting fighting style' },
  50. setPedGravity = { option = 'gravity.enabled', descr = 'Setting gravity' },
  51. setPedStat = { option = 'stats', descr = 'Changing stats' },
  52. setTime = { option = 'time.set', descr = 'Changing time' },
  53. setTimeFrozen = { option = 'time.freeze', descr = 'Freezing time' },
  54. setVehicleColor = true,
  55. setVehicleHeadLightColor = true,
  56. setVehicleOverrideLights = { option = 'lights', descr = 'Forcing lights' },
  57. setVehiclePaintjob = { option = 'paintjob', descr = 'Applying paintjobs' },
  58. setVehicleRotation = true,
  59. setWeather = { option = 'weather', descr = 'Setting weather' },
  60. spawnMe = true,
  61. warpMe = { option = 'warp', descr = 'Warping' }
  62. }
  63.  
  64. g_OptionDefaults = {
  65. alpha = true,
  66. anim = true,
  67. clothes = true,
  68. createvehicle = true,
  69. gamespeed = {
  70. enabled = true,
  71. min = 0.0,
  72. max = 3
  73. },
  74. gravity = {
  75. enabled = true,
  76. min = 0,
  77. max = 0.1
  78. },
  79. jetpack = true,
  80. kill = true,
  81. lights = true,
  82. paintjob = true,
  83. repair = true,
  84. setskin = true,
  85. setstyle = true,
  86. spawnmaponstart = true,
  87. spawnmapondeath = true,
  88. stats = true,
  89. time = {
  90. set = true,
  91. freeze = true
  92. },
  93. upgrades = true,
  94. warp = true,
  95. weapons = {
  96. enabled = true,
  97. vehiclesenabled = true,
  98. disallowed = {}
  99. },
  100. weather = true,
  101. welcometextonstart = true,
  102. vehicles = {
  103. maxidletime = 60000,
  104. idleexplode = true,
  105. maxperplayer = 2,
  106. disallowed = {}
  107. }
  108. }
  109.  
  110. function getOption(optionName)
  111. local option = get(optionName:gsub('%.', '/'))
  112. if option then
  113. if option == 'true' then
  114. option = true
  115. elseif option == 'false' then
  116. option = false
  117. end
  118. return option
  119. end
  120. option = g_OptionDefaults
  121. for i,part in ipairs(optionName:split('.')) do
  122. option = option[part]
  123. end
  124. return option
  125. end
  126.  
  127. addEventHandler('onResourceStart', g_ResRoot,
  128. function()
  129. table.each(getElementsByType('player'), joinHandler)
  130. end
  131. )
  132.  
  133. function joinHandler(player)
  134. if not player then
  135. player = source
  136. end
  137. local r, g, b = math.random(50, 255), math.random(50, 255), math.random(50, 255)
  138. setPlayerNametagColor(player, r, g, b)
  139. g_PlayerData[player] = { vehicles = {} }
  140. g_PlayerData[player].blip = createBlipAttachedTo(player, 0, 2, r, g, b)
  141. if g_FrozenTime then
  142. clientCall(player, 'setTimeFrozen', true, g_FrozenTime[1], g_FrozenTime[2], g_FrozenWeather)
  143. end
  144. if getOption('welcometextonstart') then
  145. outputChatBox('Welcome to Freeroam', player, 0, 255, 0)
  146. outputChatBox('Press F1 to show/hide controls', player, 0, 255, 0)
  147. end
  148. end
  149. addEventHandler('onPlayerJoin', g_Root, joinHandler)
  150.  
  151. addEvent('onLoadedAtClient', true)
  152. addEventHandler('onLoadedAtClient', g_ResRoot,
  153. function(player)
  154. if getOption('spawnmaponstart') and isPedDead(player) then
  155. clientCall(player, 'showWelcomeMap')
  156. end
  157. end,
  158. false
  159. )
  160.  
  161. addEventHandler('onPlayerWasted', g_Root,
  162. function()
  163. if not getOption('spawnmapondeath') then
  164. return
  165. end
  166. local player = source
  167. setTimer(
  168. function()
  169. if isPedDead(player) then
  170. clientCall(player, 'showMap')
  171. end
  172. end,
  173. 2000,
  174. 1
  175. )
  176. end
  177. )
  178.  
  179. addEvent('onClothesInit', true)
  180. addEventHandler('onClothesInit', g_Root,
  181. function()
  182. local result = {}
  183. local texture, model
  184. -- get all clothes
  185. result.allClothes = {}
  186. local typeGroup, index
  187. for type=0,17 do
  188. typeGroup = {'group', type = type, name = getClothesTypeName(type), children = {}}
  189. table.insert(result.allClothes, typeGroup)
  190. index = 0
  191. texture, model = getClothesByTypeIndex(type, index)
  192. while texture do
  193. table.insert(typeGroup.children, {id = index, texture = texture, model = model})
  194. index = index + 1
  195. texture, model = getClothesByTypeIndex(type, index)
  196. end
  197. end
  198. -- get current player clothes { type = {texture=texture, model=model} }
  199. result.playerClothes = {}
  200. for type=0,17 do
  201. texture, model = getPedClothes(source, type)
  202. if texture then
  203. result.playerClothes[type] = {texture = texture, model = model}
  204. end
  205. end
  206. triggerClientEvent(source, 'onClientClothesInit', source, result)
  207. end
  208. )
  209.  
  210. addEvent('onPlayerGravInit', true)
  211. addEventHandler('onPlayerGravInit', g_Root,
  212. function()
  213. triggerClientEvent('onClientPlayerGravInit', source, getPedGravity(source))
  214. end
  215. )
  216.  
  217. function setMySkin(skinid)
  218. if isPedDead(source) then
  219. local x, y, z = getElementPosition(source)
  220. if isPedTerminated(source) then
  221. x = 0
  222. y = 0
  223. z = 3
  224. end
  225. local r = getPedRotation(source)
  226. local interior = getElementInterior(source)
  227. spawnPlayer(source, x, y, z, r, skinid)
  228. setElementInterior(source, interior)
  229. setCameraInterior(source, interior)
  230. else
  231. setElementModel(source, skinid)
  232. setElementHealth(source, 100)
  233. end
  234. setCameraTarget(source, source)
  235. setCameraInterior(source, getElementInterior(source))
  236. end
  237.  
  238. function spawnMe(x, y, z)
  239. if not x then
  240. x, y, z = getElementPosition(source)
  241. end
  242. if isPedTerminated(source) then
  243. repeat until spawnPlayer(source, x, y, z, 0, math.random(9, 288))
  244. else
  245. spawnPlayer(source, x, y, z, 0, getPedSkin(source))
  246. end
  247. setCameraTarget(source, source)
  248. setCameraInterior(source, getElementInterior(source))
  249. end
  250.  
  251. function warpMe(targetPlayer)
  252. if isPedDead(source) then
  253. spawnMe()
  254. end
  255.  
  256. local vehicle = getPedOccupiedVehicle(targetPlayer)
  257. if not vehicle then
  258. -- target player is not in a vehicle - just warp next to him
  259. local x, y, z = getElementPosition(targetPlayer)
  260. clientCall(source, 'setPlayerPosition', x + 2, y, z)
  261. else
  262. -- target player is in a vehicle - warp into it if there's space left
  263. if getPedOccupiedVehicle(source) then
  264. --removePlayerFromVehicle(source)
  265. outputChatBox('Get out of your vehicle first.', source)
  266. return
  267. end
  268. local numseats = getVehicleMaxPassengers(vehicle)
  269. for i=0,numseats do
  270. if not getVehicleOccupant(vehicle, i) then
  271. if isPedDead(source) then
  272. local x, y, z = getElementPosition(vehicle)
  273. spawnMe(x + 4, y, z + 1)
  274. end
  275. warpPedIntoVehicle(source, vehicle, i)
  276. return
  277. end
  278. end
  279. outputChatBox('No free seats left in ' .. getPlayerName(targetPlayer) .. '\'s vehicle.', source, 255, 0, 0)
  280. end
  281. local interior = getElementInterior(targetPlayer)
  282. setElementInterior(source, interior)
  283. setCameraInterior(source, interior)
  284. end
  285.  
  286. function giveMeWeapon(weapon, amount)
  287. if weapon and weapon > 50 then
  288. return
  289. end
  290. if table.find(getOption('weapons.disallowed'), weapon) then
  291. errMsg((getWeaponNameFromID(weapon) or tostring(weapon)) .. 's are not allowed', source)
  292. else
  293. giveWeapon(source, weapon, amount, true)
  294. end
  295. end
  296.  
  297. function giveMeVehicles(vehicles)
  298. if type(vehicles) == 'number' then
  299. vehicles = { vehicles }
  300. end
  301.  
  302. local px, py, pz, prot
  303. local radius = 3
  304. local playerVehicle = getPedOccupiedVehicle(source)
  305. if playerVehicle and isElement(playerVehicle) then
  306. px, py, pz = getElementPosition(playerVehicle)
  307. prot, prot, prot = getVehicleRotation(playerVehicle)
  308. else
  309. px, py, pz = getElementPosition(source)
  310. prot = getPedRotation(source)
  311. end
  312. local offsetRot = math.rad(prot)
  313. local vx = px + radius * math.cos(offsetRot)
  314. local vy = py + radius * math.sin(offsetRot)
  315. local vz = pz + 2
  316. local vrot = prot
  317.  
  318. local vehicleList = g_PlayerData[source].vehicles
  319. local vehicle
  320. if ( not vehicles ) then return end
  321. for i,vehID in ipairs(vehicles) do
  322. if vehID < 400 or vehID > 611 then
  323. errMsg(vehID ..' is incorrect vehicle model', source)
  324. elseif not table.find(getOption('vehicles.disallowed'), vehID) then
  325. if #vehicleList >= getOption('vehicles.maxperplayer') then
  326. unloadVehicle(vehicleList[1])
  327. end
  328. vehicle = createVehicle(vehID, vx, vy, vz, 0, 0, vrot)
  329. if (not isElement(vehicle)) then return end
  330. setElementInterior(vehicle, getElementInterior(source))
  331. setElementDimension(vehicle, getElementDimension(source))
  332. table.insert(vehicleList, vehicle)
  333. g_VehicleData[vehicle] = { creator = source, timers = {} }
  334. if vehID == 464 then
  335. warpPedIntoVehicle(source, vehicle)
  336. elseif not g_Trailers[vehID] then
  337. if getOption('vehicles.idleexplode') then
  338. g_VehicleData[vehicle].timers.fire = setTimer(commitArsonOnVehicle, getOption('vehicles.maxidletime'), 1, vehicle)
  339. end
  340. g_VehicleData[vehicle].timers.destroy = setTimer(unloadVehicle, getOption('vehicles.maxidletime') + (getOption('vehicles.idleexplode') and 10000 or 0), 1, vehicle)
  341. end
  342. vx = vx + 4
  343. vz = vz + 4
  344. else
  345. errMsg(getVehicleNameFromModel(vehID):gsub('y$', 'ie') .. 's are not allowed', source)
  346. end
  347. end
  348. end
  349.  
  350. _setPlayerGravity = setPedGravity
  351. function setPedGravity(player, grav)
  352. if grav < getOption('gravity.min') then
  353. errMsg(('Minimum allowed gravity is %.5f'):format(getOption('gravity.min')), player)
  354. elseif grav > getOption('gravity.max') then
  355. errMsg(('Maximum allowed gravity is %.5f'):format(getOption('gravity.max')), player)
  356. else
  357. _setPlayerGravity(player, grav)
  358. end
  359. end
  360.  
  361. function setMyGameSpeed(speed)
  362. if speed < getOption('gamespeed.min') then
  363. errMsg(('Minimum allowed gamespeed is %.5f'):format(getOption('gamespeed.min')), source)
  364. elseif speed > getOption('gamespeed.max') then
  365. errMsg(('Maximum allowed gamespeed is %.5f'):format(getOption('gamespeed.max')), source)
  366. else
  367. clientCall(source, 'setGameSpeed', speed)
  368. end
  369. end
  370.  
  371. function setTimeFrozen(state)
  372. if state then
  373. g_FrozenTime = { getTime() }
  374. g_FrozenWeather = getWeather()
  375. clientCall(g_Root, 'setTimeFrozen', state, g_FrozenTime[1], g_FrozenTime[2], g_FrozenWeather)
  376. else
  377. if g_FrozenTime then
  378. setTime(unpack(g_FrozenTime))
  379. g_FrozenTime = nil
  380. setWeather(g_FrozenWeather)
  381. g_FrozenWeather = nil
  382. end
  383. clientCall(g_Root, 'setTimeFrozen', state)
  384. end
  385. end
  386.  
  387. function fadeVehiclePassengersCamera(toggle)
  388. local vehicle = getPedOccupiedVehicle(source)
  389. if not vehicle then
  390. return
  391. end
  392. local player
  393. for i=0,getVehicleMaxPassengers(vehicle) do
  394. player = getVehicleOccupant(vehicle, i)
  395. if player then
  396. fadeCamera(player, toggle)
  397. end
  398. end
  399. end
  400.  
  401. addEventHandler('onPlayerChat', g_Root,
  402. function(msg, type)
  403. if type == 0 then
  404. cancelEvent()
  405. if chatTime[source] and chatTime[source] + tonumber(get("*chat/mainChatDelay")) > getTickCount() then
  406. outputChatBox("Stop spamming main chat!", source, 255, 0, 0)
  407. return
  408. else
  409. chatTime[source] = getTickCount()
  410. end
  411. if get("*chat/blockRepeatMessages") == "true" and lastChatMessage[source] and lastChatMessage[source] == msg then
  412. outputChatBox("Stop repeating yourself!", source, 255, 0, 0)
  413. return
  414. else
  415. lastChatMessage[source] = msg
  416. end
  417. local r, g, b = getPlayerNametagColor(source)
  418. outputChatBox(getPlayerName(source) .. ': #FFFFFF' .. msg:gsub('#%x%x%x%x%x%x', ''), g_Root, r, g, b, true)
  419. outputServerLog( "CHAT: " .. getPlayerName(source) .. ": " .. msg )
  420. end
  421. end
  422. )
  423.  
  424. addEventHandler('onVehicleEnter', g_Root,
  425. function(player, seat)
  426. if not g_VehicleData[source] then
  427. return
  428. end
  429. if g_VehicleData[source].timers.fire then
  430. killTimer(g_VehicleData[source].timers.fire)
  431. g_VehicleData[source].timers.fire = nil
  432. end
  433. if g_VehicleData[source].timers.destroy then
  434. killTimer(g_VehicleData[source].timers.destroy)
  435. g_VehicleData[source].timers.destroy = nil
  436. end
  437. if not getOption('weapons.vehiclesenabled') and g_ArmedVehicles[getElementModel(source)] then
  438. toggleControl(player, 'vehicle_fire', false)
  439. toggleControl(player, 'vehicle_secondary_fire', false)
  440. end
  441. end
  442. )
  443.  
  444. addEventHandler('onVehicleExit', g_Root,
  445. function(player, seat)
  446. if not g_VehicleData[source] then
  447. return
  448. end
  449. if not g_VehicleData[source].timers.fire then
  450. for i=0,getVehicleMaxPassengers(source) or 1 do
  451. if getVehicleOccupant(source, i) then
  452. return
  453. end
  454. end
  455. if getOption('vehicles.idleexplode') then
  456. g_VehicleData[source].timers.fire = setTimer(commitArsonOnVehicle, getOption('vehicles.maxidletime'), 1, source)
  457. end
  458. g_VehicleData[source].timers.destroy = setTimer(unloadVehicle, getOption('vehicles.maxidletime') + (getOption('vehicles.idleexplode') and 10000 or 0), 1, source)
  459. end
  460. if g_ArmedVehicles[getElementModel(source)] then
  461. toggleControl(player, 'vehicle_fire', true)
  462. toggleControl(player, 'vehicle_secondary_fire', true)
  463. end
  464. end
  465. )
  466.  
  467. function commitArsonOnVehicle(vehicle)
  468. g_VehicleData[vehicle].timers.fire = nil
  469. setElementHealth(vehicle, 0)
  470. end
  471.  
  472. addEventHandler('onVehicleExplode', g_Root,
  473. function()
  474. if not g_VehicleData[source] then
  475. return
  476. end
  477. if g_VehicleData[source].timers.fire then
  478. killTimer(g_VehicleData[source].timers.fire)
  479. g_VehicleData[source].timers.fire = nil
  480. end
  481. if not g_VehicleData[source].timers.destroy then
  482. g_VehicleData[source].timers.destroy = setTimer(unloadVehicle, 5000, 1, source)
  483. end
  484. end
  485. )
  486.  
  487. function unloadVehicle(vehicle)
  488. if not g_VehicleData[vehicle] then
  489. return
  490. end
  491. for name,timer in pairs(g_VehicleData[vehicle].timers) do
  492. if isTimer(timer) then
  493. killTimer(timer)
  494. end
  495. g_VehicleData[vehicle].timers[name] = nil
  496. end
  497. local creator = g_VehicleData[vehicle].creator
  498. if g_PlayerData[creator] then
  499. table.removevalue(g_PlayerData[creator].vehicles, vehicle)
  500. end
  501. g_VehicleData[vehicle] = nil
  502. if isElement(vehicle) then
  503. destroyElement(vehicle)
  504. end
  505. end
  506.  
  507. function quitHandler(player)
  508. if type(player) ~= 'userdata' then
  509. player = source
  510. end
  511. if g_PlayerData[player].blip and isElement(g_PlayerData[player].blip) then
  512. destroyElement(g_PlayerData[player].blip)
  513. end
  514. table.each(g_PlayerData[player].vehicles, unloadVehicle)
  515. g_PlayerData[player] = nil
  516. chatTime[player] = nil
  517. lastChatMessage[player] = nil
  518. end
  519. addEventHandler('onPlayerQuit', g_Root, quitHandler)
  520.  
  521. addEventHandler('onResourceStop', g_ResRoot,
  522. function()
  523. for player,data in pairs(g_PlayerData) do
  524. quitHandler(player)
  525. end
  526. end
  527. )
  528.  
  529. addEvent('onServerCall', true)
  530. addEventHandler('onServerCall', g_Root,
  531. function(fnName, ...)
  532. local fnInfo = g_RPCFunctions[fnName]
  533. if fnInfo and ((type(fnInfo) == 'boolean' and fnInfo) or (type(fnInfo) == 'table' and getOption(fnInfo.option))) then
  534. local fn = _G
  535. for i,pathpart in ipairs(fnName:split('.')) do
  536. fn = fn[pathpart]
  537. end
  538. fn(...)
  539. elseif type(fnInfo) == 'table' then
  540. errMsg(fnInfo.descr .. ' is not allowed', source)
  541. end
  542. end
  543. )
  544.  
  545. function clientCall(player, fnName, ...)
  546. triggerClientEvent(player, 'onClientCall', g_ResRoot, fnName, ...)
  547. end
Add Comment
Please, Sign In to add comment