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