Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.61 KB | None | 0 0
  1. RegisterNetEvent('race:weGotPorn')
  2. RegisterNetEvent('race:itsGoTime')
  3. RegisterNetEvent('race:showGoMessage')
  4. RegisterNetEvent('race:setCheckpoint')
  5. RegisterNetEvent('race:updateStatus')
  6.  
  7. AddEventHandler("onClientMapStart", function()
  8. Citizen.Wait(0)
  9. initializeMap()
  10. end)
  11.  
  12.  
  13. local curCheckpoint, nextCheckpoint
  14. local goGoGo
  15.  
  16. local playerCar
  17. local weFinished
  18. local resultsShown
  19.  
  20. local checkpoints = {}
  21. local playerScores = {}
  22.  
  23.  
  24. function resetGameMode()
  25. curCheckpoint = nil
  26. nextCheckpoint = nil
  27.  
  28. -- SetMultiplayerHudTime('')
  29.  
  30. checkpointCount = 0
  31. playerScores = {}
  32.  
  33. -- if IsThisMachineTheServer() then
  34. -- load the initial map
  35. --initializeMap()
  36. -- end
  37.  
  38. goGoGo = false
  39. weFinished = false
  40. resultsShown = false
  41. end
  42.  
  43. function initializeMap()
  44. --echo("[RACE] initializeMap\n")
  45.  
  46. TriggerServerEvent('race:updateCheckpoints', checkpoints)
  47. end
  48.  
  49.  
  50.  
  51. function updatePositions()
  52. local players = {}
  53.  
  54. for id, data in pairs(playerScores) do
  55. data.playerId = id
  56.  
  57. table.insert(players, data)
  58. end
  59.  
  60. table.sort(players, function(a, b)
  61. if a.finishPosition or b.finishPosition then
  62. if not b.finishPosition then
  63. return true
  64. end
  65.  
  66. if not a.finishPosition then
  67. return false
  68. end
  69.  
  70. return a.finishPosition < b.finishPosition
  71. end
  72.  
  73. if a.cp == b.cp then
  74. local aPed = a.ped
  75. local bPed = b.ped
  76.  
  77. local aPos
  78. local bPos
  79.  
  80. if not DoesCharExist(aPed) or not DoesCharExist(bPed) then
  81. aPos = { 0, 0 }
  82. bPos = { 0, 0 }
  83. else
  84. aPos = a.ped.position
  85. bPos = b.ped.position
  86. end
  87.  
  88. local nextCp = checkpoints[a.cp + 1]
  89.  
  90. if not nextCp then
  91. return a.cp > b.cp
  92. end
  93.  
  94. local aDist = GetDistanceBetweenCoords2d(aPos[1], aPos[2], nextCp.pos[1], nextCp.pos[2])
  95. local bDist = GetDistanceBetweenCoords2d(bPos[1], bPos[2], nextCp.pos[1], nextCp.pos[2])
  96.  
  97. return aDist < bDist
  98. end
  99.  
  100. return a.cp > b.cp
  101. end)
  102.  
  103. if not playerScores[GetPlayerByServerId] then
  104. return
  105. end
  106.  
  107. local lastPosition = selfLastPosition
  108.  
  109. local i = 1
  110.  
  111. for _, v in ipairs(players) do
  112. playerScores[v.playerId].position = i
  113.  
  114. i = i + 1
  115. end
  116.  
  117. local selfPosition = playerScores[GetPlayerId().serverId].position
  118. selfLastPosition = selfPosition
  119.  
  120. if selfPosition ~= lastPosition then
  121. TriggerEvent('chatMessage', '', { 0, 0, 0 }, 'position changed to ' .. tostring(selfPosition) .. ' from ' .. tostring(lastPosition))
  122. end
  123.  
  124.  
  125. -- positions updated, we hope
  126. end
  127.  
  128. AddEventHandler('race:onPlayerFinished', function(player, data)
  129. local selfId = GetPlayerId().serverId
  130.  
  131. if not playerScores[player] then
  132. local ped = sPlayer.ped
  133.  
  134. playerScores[player] = {
  135. cp = #checkpoints,
  136. ped = ped,
  137. vehicle = ped.vehicle
  138. }
  139. end
  140.  
  141. playerScores[player].finishPosition = data.position
  142.  
  143. if selfId == player then
  144. exports.obituary:printObituary('New world record!')
  145.  
  146. TriggerEvent('chatMessage', '', { 0, 0, 0 }, 'you finished!')
  147.  
  148. weFinished = true
  149.  
  150. tearDownCheckpoint(curCheckpoint)
  151. tearDownCheckpoint(nextCheckpoint)
  152.  
  153. -- todo: spectate?
  154.  
  155. CreateThread(function()
  156. Wait(500)
  157.  
  158. if playerCar then
  159. FreezeCarPosition(playerCar, true)
  160. end
  161. end)
  162. end
  163.  
  164. local sPlayer = GetPlayerByServerId(player)
  165.  
  166. if sPlayer then
  167. exports.obituary:printObituary('<b>%s</b> finished in %s seconds', sPlayer.name, tostring(data.finishSeconds))
  168. end
  169. end)
  170.  
  171. AddEventHandler('onClientGameTypeStart', function()
  172. CreateThread(function()
  173. --[[while true do
  174. Wait(500)
  175.  
  176. local player = GetPlayerId()
  177.  
  178. TriggerServerEvent('race:updatePos', player.ped.position)
  179. end]]
  180. end)
  181.  
  182. CreateThread(function()
  183. while true do
  184. Wait(250)
  185.  
  186. updatePositions()
  187. end
  188. end)
  189. end)
  190.  
  191. function GetPlayerInteger(i)
  192. local serverId = GetPlayerServerId()
  193. local players = {}
  194.  
  195. for k, v in ipairs(players) do
  196. if v.serverId == serverId then
  197. return k
  198. end
  199. end
  200.  
  201. return 1
  202. end
  203.  
  204. function spawnVehicle(spawnPoint)
  205. local carModel
  206.  
  207. if not spawnPoint.carModel then
  208. carModel = 'taxi'
  209. else
  210. carModel = spawnPoint.carModel
  211. end
  212.  
  213. if not tonumber(carModel) then
  214. carModel = GetHashKey(carModel)
  215. end
  216.  
  217. -- is the model actually a model?
  218. if not IsModelInCdimage(carModel) then
  219. -- error("invalid spawn model")
  220. end
  221.  
  222. -- is is even a vehicle?
  223. if not IsThisModelAVehicle(carModel) then
  224. -- error("this model ain't a vehicle!")
  225. end
  226.  
  227. -- spawn a vehicle for our lovely player
  228. RequestModel(carModel)
  229. LoadAllObjectsNow()
  230.  
  231. playerCar = CreateCar(carModel, spawnPoint.x, spawnPoint.y, spawnPoint.z, 0, 1)
  232. SetCarHeading(playerCar, spawnPoint.heading)
  233. SetCarOnGroundProperly(playerCar)
  234.  
  235. WarpCharIntoCar(GetPlayerId().ped, playerCar)
  236.  
  237. if not goGoGo then
  238. FreezeCarPosition(playerCar, true)
  239. end
  240.  
  241. LockCarDoors(playerCar, 4)
  242.  
  243. -- and done, hopefully.
  244. end
  245.  
  246. AddEventHandler('race:itsGoTime', function()
  247. if playerCar then
  248. -- let go of the brakes
  249. FreezeCarPosition(playerCar, false)
  250. end
  251.  
  252. -- gogogo
  253. goGoGo = true
  254. end)
  255.  
  256. string.lpad = function(str, len, char)
  257. if char == nil then char = ' ' end
  258. return string.rep(char, len - #str) .. str
  259. end
  260.  
  261. AddEventHandler('race:results', function(time)
  262. if playerCar then
  263. FreezeCarPosition(playerCar, true)
  264. end
  265.  
  266. tearDownCheckpoint(curCheckpoint)
  267. tearDownCheckpoint(nextCheckpoint)
  268.  
  269. -- SetMultiplayerHudTime('')
  270.  
  271. updatePositions()
  272.  
  273. local players = {}
  274.  
  275. for id, data in pairs(playerScores) do
  276. table.insert(players, data)
  277. end
  278.  
  279. table.sort(players, function(a, b) return a.position < b.position end)
  280.  
  281. TriggerEvent('chatMessage', '', { 0, 0, 0 }, 'RESULTS')
  282.  
  283. for i, p in ipairs(players) do
  284. local name = '**INVALID**'
  285. local sp = GetPlayerByServerId(p.playerId)
  286.  
  287. if sp then
  288. name = sp.name
  289. end
  290.  
  291. TriggerEvent('chatMessage', '', { 0, 0, 0 }, tostring(i) .. '. ' .. name)
  292. end
  293. end)
  294.  
  295. AddEventHandler('race:hurryUp', function(time)
  296. CreateThread(function()
  297. --echo("resultsShown: " .. tostring(resultsShown) .. " , weF: " .. tostring(weFinished) .. "\n")
  298.  
  299. while not resultsShown and not weFinished do
  300. Wait(1000)
  301.  
  302. time = time - 1000
  303.  
  304. -- SetMultiplayerHudTime('00:' .. tostring(math.floor(time / 1000)):lpad(2, '0'))
  305. --echo(tostring(math.floor(time / 1000)):lpad(2, '0') .. ':' .. tostring(math.floor((time % 1000) / 100)):lpad(2, '0') .. "\n")
  306. end
  307. end)
  308. end)
  309.  
  310. AddEventHandler('race:showGoMessage', function(message)
  311. TriggerEvent('chatMessage', '', { 0, 0, 0 }, message)
  312. end)
  313.  
  314. AddEventHandler('onClientMapStart', function(res)
  315. resetGameMode()
  316.  
  317. requestedGo = true
  318.  
  319. TriggerServerEvent('race:requestGo')
  320. end)
  321.  
  322. AddEventHandler('onClientMapStop', function(res)
  323. DoScreenFadeOut(50)
  324. end)
  325.  
  326.  
  327.  
  328. AddEventHandler('race:weGotPorn', function()
  329. --echo("[RACE] race:weGotPorn\n")
  330.  
  331. if not requestedGo then
  332. return
  333. end
  334.  
  335. requestedGo = false
  336.  
  337. exports.spawnmanager:setAutoSpawn(false)
  338.  
  339. exports.spawnmanager:spawnPlayer(GetPlayerInteger(GetPlayerServerId(PlayerId())), function(spawnPoint)
  340. spawnVehicle(spawnPoint)
  341. end)
  342.  
  343. TriggerServerEvent('race:requestCheckpoint', '1234')
  344. end)
  345.  
  346. function setUpCheckpoint(cp, next)
  347. local nextPos, typeNum
  348.  
  349. if next then
  350. nextPos = next.pos
  351. typeNum = 2
  352. else
  353. nextPos = { 0.0, 0.0, 0.0 }
  354. typeNum = 3
  355. end
  356.  
  357. -- 2 = regular 'ground', 3 = finish 'ground', others are different 3dmarker types
  358. cp.handle = CreateCheckpoint(typeNum, cp.pos[1], cp.pos[2], cp.pos[3] + 2.5, nextPos[1], nextPos[2], nextPos[3], 1.0001, _r)
  359. cp.blip = AddBlipForCoord(cp.pos[1], cp.pos[2], cp.pos[3], _i)
  360.  
  361. if cp == nextCheckpoint then
  362. ChangeBlipScale(cp.blip, 0.8)
  363. end
  364.  
  365. ChangeBlipSprite(cp.blip, 3)
  366. end
  367.  
  368. function tearDownCheckpoint(cp)
  369. if not cp then
  370. return
  371. end
  372.  
  373. if cp.blip then
  374. RemoveBlip(cp.blip)
  375. cp.blip = nil
  376. end
  377.  
  378. if cp.handle then
  379. DeleteCheckpoint(cp.handle)
  380. cp.handle = nil
  381. end
  382. end
  383.  
  384. AddEventHandler('race:setCheckpoint', function(cur, next, later)
  385. if curCheckpoint then
  386. tearDownCheckpoint(curCheckpoint)
  387. end
  388.  
  389. if nextCheckpoint then
  390. tearDownCheckpoint(nextCheckpoint)
  391. end
  392.  
  393. curCheckpoint = cur
  394. nextCheckpoint = next
  395.  
  396. if cur then
  397. setUpCheckpoint(curCheckpoint, nextCheckpoint)
  398.  
  399. -- make a background thread waiting for the checkpoint to be reached
  400. CreateThread(function()
  401. local localCur = curCheckpoint
  402.  
  403. -- so we exit if the checkpoint target is changed
  404. while curCheckpoint == localCur do
  405. Wait(25)
  406.  
  407. if playerCar then
  408. local px, py, pz = GetCarCoordinates(playerCar)
  409. local distance = GetDistanceBetweenCoords2d(px, py, localCur.pos[1], localCur.pos[2])
  410.  
  411. if distance < 10 then
  412. -- pass the fact we reached the checkpoint to the server
  413. TriggerServerEvent('race:gotCP', '1234')
  414.  
  415. break
  416. end
  417. end
  418. end
  419. end)
  420. end
  421.  
  422. if next then
  423. setUpCheckpoint(nextCheckpoint, later)
  424. end
  425. end)
  426.  
  427. AddEventHandler('race:confirmCP', function()
  428. PlayAudioEvent('FRONTEND_GAME_PICKUP_CHECKPOINT')
  429. end)
  430.  
  431. AddEventHandler('race:updateStatus', function(player, curCP)
  432. if curCP == -1 then
  433. playerScores[player] = nil
  434. end
  435.  
  436. local sPlayer = GetPlayerServerId(player)
  437.  
  438. if not sPlayer then
  439. return
  440. end
  441.  
  442. local ped = sPlayer.ped
  443.  
  444. playerScores[player] = {
  445. cp = curCP,
  446. ped = ped,
  447. vehicle = ped.vehicle
  448. }
  449.  
  450. TriggerEvent('chatMessage', '', { 0, 0, 0 }, sPlayer.name .. ' now has cp ' .. curCP)
  451.  
  452. updatePositions()
  453. end)
  454.  
  455. AddEventHandler('onClientMapStop', function()
  456. if playerCar then
  457. MarkCarAsNoLongerNeeded(playerCar)
  458. playerCar = nil
  459. end
  460.  
  461. if curCheckpoint and curCheckpoint.handle then
  462. DeleteCheckpoint(curCheckpoint.handle)
  463. end
  464.  
  465. if nextCheckpoint and nextCheckpoint.handle then
  466. DeleteCheckpoint(nextCheckpoint.handle)
  467. end
  468. end)
  469.  
  470. AddEventHandler('getMapDirectives', function(add)
  471. -- call the remote callback
  472. add('checkpoint', function(state, data)
  473. table.insert(checkpoints, data)
  474.  
  475. state.add('pos', data.pos)
  476.  
  477. -- delete callback follows on the next line
  478. end, function(state, arg)
  479. for i, sp in ipairs(checkpoints) do
  480. if sp.pos[1] == state.pos[1] and sp.pos[2] == state.pos[2] and sp.pos[3] == state.pos[3] then
  481. table.remove(checkpoints, i)
  482. return
  483. end
  484. end
  485. end)
  486. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement