-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathraces_client.lua
4095 lines (3586 loc) · 162 KB
/
races_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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
Copyright (c) 2024, Neil J. Tan
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--]]
local STATE_IDLE <const> = 0 -- idle state
local STATE_EDITING <const> = 1 -- editing track state
local STATE_JOINING <const> = 2 -- joining race state
local STATE_RACING <const> = 3 -- racing state
local state = STATE_IDLE -- player state
local white <const> = {r = 255, g = 255, b = 255}
local red <const> = {r = 255, g = 0, b = 0}
local green <const> = {r = 0, g = 255, b = 0}
local blue <const> = {r = 0, g = 0, b = 255}
local yellow <const> = {r = 255, g = 255, b = 0}
local orange <const> = {r = 255, g = 127, b = 0}
local purple <const> = {r = 255, g = 0, b = 255}
local startFinishBlipColor <const> = 17 -- orange
local startBlipColor <const> = 2 -- green
local finishBlipColor <const> = 0 -- white
local numberedBlipColor <const> = 38 -- dark blue
local registerBlipColor <const> = 83 -- purple
local racerBlipColor <const> = 2 -- green
local currentWPBlipColor <const> = 5 -- yellow
local selectedBlipColor <const> = 1 -- red
local blipRouteColor <const> = 18 -- light blue
local checkeredFlagSprite <const> = 38 -- checkered flag
local numberedSprite <const> = 1 -- numbered circle
local registerSprite <const> = 58 -- circled star
local racerSprite <const> = 1 -- circle
local checkeredFlagCheckpoint <const> = GetGameBuildNumber() < 2189 and 9 or 10 -- cylinder checkered flag
local numberedCheckpoint <const> = GetGameBuildNumber() < 2189 and 42 or 45 -- cylinder with number
local plainCheckpoint <const> = GetGameBuildNumber() < 2189 and 45 or 48 -- cylinder
local arrow3Checkpoint <const> = GetGameBuildNumber() < 2189 and 7 or 8 -- cylinder with 3 arrows
local defaultBuyin <const> = 500 -- default race buy-in
local defaultLaps <const> = 1 -- default number of laps in a race
local defaultTimeout <const> = 120 -- default DNF timeout
local defaultAllowAI <const> = "no" -- default allow AI value
local defaultRecur <const> = "yes" -- default random vehicle race recur value
local defaultOrder <const> = "no" -- default random vehicle race order value
local defaultDelay <const> = 30 -- default race start delay
local defaultModel <const> = "adder" -- default spawned vehicle model
local defaultRadius <const> = 5.0 -- default waypoint radius
local minDelay <const> = 5 -- minimum race start delay
local minRadius <const> = 0.5 -- minimum waypoint radius
local maxRadius <const> = 10.0 -- maximum waypoint radius
local topSide <const> = 0.45 -- top position of HUD
local leftSide <const> = 0.02 -- left position of HUD
local rightSide <const> = leftSide + 0.09 -- right position of HUD
local maxNumVisible <const> = 3 -- maximum number of waypoints visible during a race
local numVisible = maxNumVisible -- number of waypoints visible during a race - may be less than maxNumVisible
local races = {} -- races[playerID] = {owner, access, trackName, buyin, laps, timeout, allowAI, rtype, restrict, vclass, className, svehicle, recur, order, vehicleList, blip, checkpoint} - race information
local raceIndex = -1 -- index of race player has joined
local waypoints = {} -- waypoints[] = {coord = {x, y, z, r}, checkpoint, blip, sprite, color, number, name} - race waypoints
local startIsFinish = false -- flag indicating if start and finish are same waypoint
local numLaps = -1 -- number of laps in current race
local DNFTimeout = -1 -- DNF timeout after first player finishes the race
local beginDNFTimeout = false -- flag indicating if DNF timeout should begin
local DNFTimeoutStart = -1 -- start time of DNF timeout
local raceType = nil -- type of race that player has joined
local startVehicle = nil -- vehicle model of starting vehicle used in random vehicle races
local startVehicleSpawned = false -- flag indicating if start vehicle has spawned
local recurring = true -- flag indicating if vehicles recur in random vehicle races
local ordered = false -- flag indicating if all racers switch to the same random vehicle after the same lap number in random vehicle races
local raceStart = -1 -- start time of race before delay
local raceDelay = -1 -- delay before official start of race
local started = false -- flag indicating if race started
local startCoord = nil -- coordinates of vehicle once race has started
local heading = nil -- heading of player at last waypoint
local destCoord = nil -- coordinates of destination waypoint
local numWaypointsPassed = -1 -- number of waypoints player has passed
local currentWaypoint = -1 -- current waypoint index - for multi-lap races, actual current waypoint index is currentWaypoint % #waypoints + 1
local currentLap = -1 -- current lap
local lapTimeStart = -1 -- start time of current lap
local bestLapTime = -1 -- best lap time
local bestLapVehicleName = nil -- name of vehicle in which player recorded best lap time
local originalVehicleHash = nil -- vehicle hash of original vehicle before switching to other vehicles in random vehicle races
local originalColorPri = -1 -- primary color of original vehicle
local originalColorSec = -1 -- secondary color of original vehicle
local originalColorPearl = -1 -- pearlescent color of original vehicle
local originalColorWheel = -1 -- wheel color of original vehicle
local respawnVehicleHash = nil -- hash of respawn vehicle being driven
local respawnColorPri = -1 -- primary color of respawn vehicle
local respawnColorSec = -1 -- secondary color of respawn vehicle
local respawnColorPearl = -1 -- pearlescent color of respawn vehicle
local respawnColorWheel = -1 -- wheel color of respawn vehicle
local respawnCtrlPressed = false -- flag indicating if respawn crontrol is pressed
local respawnStart = -1 -- start time when respawn control pressed
local currentVehicleName = nil -- name of current vehicle being driven
local raceVehicleList = {} -- list of vehicles used in random vehicle race
local highlightedIndex = 0 -- index of highlighted checkpoint
local selectedIndex0 = 0 -- index of first selected waypoint
local selectedIndex1 = 0 -- index of second selected waypoint
local trackAccess = nil -- indicaties if saved track is public("pub") or private("pvt")
local savedTrackName = nil -- name of saved track - nil if track not saved
local vehicleList = {} -- vehicle list used for custom class races and random vehicle races
local camTransStarted = false -- flag indicating if camera transition at start of race has started
local vehicleFrozen = false -- flag indicating if vehicle player is in is frozen before start of race
local countdown = -1 -- countdown before start
local position = -1 -- position in race out of numRacers players
local numRacers = -1 -- number of players in race - no DNF players included
local raceCheckpoint = nil -- race checkpoint of current waypoint in world
local racerBlipGT = {} -- racerBlipGT[netID] = {blip, gamerTag, name} - blips, gamer tags and names for all racers participating in race
local results = {} -- results[] = {source, playerName, aiName, finishTime, bestLapTime, vehicleName} - race results
local speedo = false -- flag indicating if speedometer is displayed
local preRaceSpeedo = speedo -- speedo value before race start
local unitom = "imperial" -- current unit of measurement
local panelsInitialized = false -- flag indicating if selectable fields of all panels have been populated
local panelShown = false -- flag indicating if main, track, ai, list or register panel is shown
local allVehiclesList = {} -- list of all vehicles from vehicles.json
local aiState = nil -- table containing race and AI driver info
math.randomseed(GetCloudTimeAsInt())
TriggerServerEvent("races:init")
local function notifyPlayer(msg)
TriggerEvent("chat:addMessage", {
color = {255, 0, 0},
multiline = true,
args = {"[races:client]", msg}
})
end
local function sendMessage(msg)
if true == panelShown then
SendNUIMessage({
action = "reply",
message = string.gsub(msg, "\n", "<br>")
})
end
notifyPlayer(msg)
end
local function copyTable(tbl)
if nil == tbl then
return nil
end
local t = {}
for key, value in pairs(tbl) do
t[key] = value
end
return t
end
local function deleteWaypointCheckpoints()
for i = 1, #waypoints do
DeleteCheckpoint(waypoints[i].checkpoint)
end
end
local function getCheckpointColor(blipColor)
if 0 == blipColor then
return white
elseif 1 == blipColor then
return red
elseif 2 == blipColor then
return green
elseif 38 == blipColor then
return blue
elseif 5 == blipColor then
return yellow
elseif 17 == blipColor then
return orange
elseif 83 == blipColor then
return purple
else
return yellow
end
end
local function makeCheckpoint(checkpointType, coord, nextCoord, color, alpha, num)
local zCoord = coord.z
if numberedCheckpoint == checkpointType or plainCheckpoint == checkpointType then
zCoord = zCoord - coord.r / 2.0
else
zCoord = zCoord + coord.r / 2.0
end
local checkpoint = CreateCheckpoint(checkpointType, coord.x, coord.y, zCoord, nextCoord.x, nextCoord.y, nextCoord.z, coord.r * 2.0, color.r, color.g, color.b, alpha, num)
SetCheckpointCylinderHeight(checkpoint, 10.0, 10.0, coord.r * 2.0)
return checkpoint
end
local function setStartToFinishCheckpoints()
for i = 1, #waypoints do
local color = getCheckpointColor(waypoints[i].color)
local checkpointType = -1 == waypoints[i].number and checkeredFlagCheckpoint or numberedCheckpoint
waypoints[i].checkpoint = makeCheckpoint(checkpointType, waypoints[i].coord, waypoints[i].coord, color, 127, i - 1)
end
end
local function setBlipProperties(index)
local waypoint = waypoints[index]
SetBlipSprite(waypoint.blip, waypoint.sprite)
SetBlipColour(waypoint.blip, waypoint.color)
ShowNumberOnBlip(waypoint.blip, waypoint.number)
BeginTextCommandSetBlipName("STRING")
AddTextComponentSubstringPlayerName(waypoint.name)
EndTextCommandSetBlipName(waypoint.blip)
end
local function setStartToFinishBlips()
-- waypoints[] = {coord = {x, y, z, r}, checkpoint, blip, sprite, color, number, name}
if true == startIsFinish then
waypoints[1].sprite = checkeredFlagSprite
waypoints[1].color = startFinishBlipColor
waypoints[1].number = -1
waypoints[1].name = "Start/Finish"
if #waypoints > 1 then
waypoints[#waypoints].sprite = numberedSprite
waypoints[#waypoints].color = numberedBlipColor
waypoints[#waypoints].number = #waypoints - 1
waypoints[#waypoints].name = "Waypoint"
end
else -- #waypoints should be > 1
waypoints[1].sprite = checkeredFlagSprite
waypoints[1].color = startBlipColor
waypoints[1].number = -1
waypoints[1].name = "Start"
waypoints[#waypoints].sprite = checkeredFlagSprite
waypoints[#waypoints].color = finishBlipColor
waypoints[#waypoints].number = -1
waypoints[#waypoints].name = "Finish"
end
for i = 2, #waypoints - 1 do
waypoints[i].sprite = numberedSprite
waypoints[i].color = numberedBlipColor
waypoints[i].number = i - 1
waypoints[i].name = "Waypoint"
end
for i = 1, #waypoints do
setBlipProperties(i)
end
end
local function loadWaypointBlips(waypointCoords)
for i = 1, #waypoints do
RemoveBlip(waypoints[i].blip)
end
waypoints = {}
for i = 1, #waypointCoords - 1 do
local blip = AddBlipForCoord(waypointCoords[i].x, waypointCoords[i].y, waypointCoords[i].z)
waypoints[i] = {coord = waypointCoords[i], checkpoint = nil, blip = blip, sprite = -1, color = -1, number = -1, name = nil}
end
startIsFinish =
waypointCoords[1].x == waypointCoords[#waypointCoords].x and
waypointCoords[1].y == waypointCoords[#waypointCoords].y and
waypointCoords[1].z == waypointCoords[#waypointCoords].z
if false == startIsFinish then
local blip = AddBlipForCoord(waypointCoords[#waypointCoords].x, waypointCoords[#waypointCoords].y, waypointCoords[#waypointCoords].z)
waypoints[#waypointCoords] = {coord = waypointCoords[#waypointCoords], checkpoint = nil, blip = blip, sprite = -1, color = -1, number = -1, name = nil}
end
setStartToFinishBlips()
SetBlipRoute(waypoints[1].blip, true)
SetBlipRouteColour(waypoints[1].blip, blipRouteColor)
end
local function drawMsg(x, y, msg, scale, justify)
SetTextFont(4)
SetTextScale(0, scale)
SetTextColour(255, 255, 0, 255)
SetTextOutline()
SetTextJustification(justify)
SetTextWrap(0.0, 1.0)
BeginTextCommandDisplayText("STRING")
AddTextComponentSubstringPlayerName(msg)
EndTextCommandDisplayText(x, y)
end
local function drawRect(x, y, w, h, r, g, b, a)
DrawRect(x + w / 2.0, y + h / 2.0, w, h, r, g, b, a)
end
local function waypointsToCoords()
local waypointCoords = {}
for i = 1, #waypoints do
waypointCoords[i] = waypoints[i].coord
end
if true == startIsFinish then
waypointCoords[#waypointCoords + 1] = waypointCoords[1]
end
return waypointCoords
end
local function waypointsToCoordsRev()
local waypointCoords = {}
if true == startIsFinish then
waypointCoords[1] = waypoints[1].coord
end
for i = #waypoints, 1, -1 do
waypointCoords[#waypointCoords + 1] = waypoints[i].coord
end
return waypointCoords
end
local function minutesSeconds(milliseconds)
local seconds = milliseconds / 1000.0
local minutes = math.floor(seconds / 60.0)
seconds = seconds - minutes * 60.0
return minutes, seconds
end
local function loadModel(model)
RequestModel(model)
while false == HasModelLoaded(model) do
Citizen.Wait(0)
end
end
local function putPedInVehicle(ped, model, priColor, secColor, pearlColor, wheelColor, coord, pedHeading)
local vehicle = CreateVehicle(model, coord.x, coord.y, coord.z, pedHeading, true, false)
SetModelAsNoLongerNeeded(model)
if priColor ~= -1 and secColor ~= -1 and pearlColor ~= -1 and wheelColor ~= -1 then
SetVehicleColours(vehicle, priColor, secColor)
SetVehicleExtraColours(vehicle, pearlColor, wheelColor)
end
SetVehicleDirtLevel(vehicle, 0.0)
SetVehicleEngineOn(vehicle, true, true, false)
SetVehRadioStation(vehicle, "OFF")
SetPedIntoVehicle(ped, vehicle, -1)
return vehicle
end
local function switchVehicle(ped, model, priColor, secColor, pearlColor, wheelColor)
local vehicle = GetVehiclePedIsIn(ped, false)
if 0 == vehicle then
loadModel(model)
return putPedInVehicle(ped, model, priColor, secColor, pearlColor, wheelColor, GetEntityCoords(ped), GetEntityHeading(ped))
end
local newVehicle = nil
if GetPedInVehicleSeat(vehicle, -1) == ped then
local passengers = {}
for seat = 0, GetVehicleModelNumberOfSeats(GetEntityModel(vehicle)) - 2 do
local passenger = GetPedInVehicleSeat(vehicle, seat)
if passenger ~= 0 then
passengers[#passengers + 1] = {ped = passenger, seat = seat}
end
end
local coord = GetEntityCoords(vehicle)
local speed = GetEntitySpeed(vehicle)
loadModel(model)
SetEntityAsMissionEntity(vehicle, true, true)
DeleteVehicle(vehicle)
newVehicle = putPedInVehicle(ped, model, priColor, secColor, pearlColor, wheelColor, coord, GetEntityHeading(ped))
SetVehicleForwardSpeed(newVehicle, speed)
for _, passenger in pairs(passengers) do
SetPedIntoVehicle(passenger.ped, newVehicle, passenger.seat)
end
end
return newVehicle
end
local function getClassName(vclass)
if -1 == vclass then
return "'Custom'(-1)"
elseif vclass >= 0 and vclass <= 22 then
return "'" .. GetLabelText("VEH_CLASS_" .. vclass) .. "'(" .. vclass .. ")"
else
return "'Unknown'(" .. vclass .. ")"
end
end
local function finishRace(time)
TriggerServerEvent("races:finish", raceIndex, PedToNet(PlayerPedId()), nil, numWaypointsPassed, time, bestLapTime, bestLapVehicleName, nil)
for i = 1, #waypoints do
SetBlipDisplay(waypoints[i].blip, 2)
end
local curr = true == startIsFinish and currentWaypoint % #waypoints + 1 or currentWaypoint
SetBlipColour(waypoints[curr].blip, waypoints[curr].color)
SetBlipRoute(waypoints[1].blip, true)
SetBlipRouteColour(waypoints[1].blip, blipRouteColor)
speedo = preRaceSpeedo
if "rand" == raceType and originalVehicleHash ~= nil then
local vehicle = switchVehicle(PlayerPedId(), originalVehicleHash, originalColorPri, originalColorSec, originalColorPearl, originalColorWheel)
if vehicle ~= nil then
SetEntityAsNoLongerNeeded(vehicle)
end
end
state = STATE_IDLE
end
local function setBlipCheckpointColor(waypoint, color)
SetBlipColour(waypoint.blip, color)
local cpColor = getCheckpointColor(color)
SetCheckpointRgba(waypoint.checkpoint, cpColor.r, cpColor.g, cpColor.b, 127)
SetCheckpointRgba2(waypoint.checkpoint, cpColor.r, cpColor.g, cpColor.b, 127)
end
local function editWaypoints(coord)
local selectedIndex = 0
local minDist = maxRadius
for index, waypoint in ipairs(waypoints) do
local dist = #(coord - vector3(waypoint.coord.x, waypoint.coord.y, waypoint.coord.z))
if dist < waypoint.coord.r and dist < minDist then
minDist = dist
selectedIndex = index
end
end
if 0 == selectedIndex then -- no existing waypoint selected
if 0 == selectedIndex0 then -- no previous selected waypoints exist, add new waypoint
local blip = AddBlipForCoord(coord.x, coord.y, coord.z)
waypoints[#waypoints + 1] = {coord = {x = coord.x, y = coord.y, z = coord.z, r = defaultRadius}, checkpoint = nil, blip = blip, sprite = -1, color = -1, number = -1, name = nil}
startIsFinish = 1 == #waypoints
setStartToFinishBlips()
deleteWaypointCheckpoints()
setStartToFinishCheckpoints()
else -- first selected waypoint exists
if 0 == selectedIndex1 then -- second selected waypoint does not exist, move first selected waypoint to new location
local selectedWaypoint0 = waypoints[selectedIndex0]
selectedWaypoint0.coord = {x = coord.x, y = coord.y, z = coord.z, r = selectedWaypoint0.coord.r}
SetBlipCoords(selectedWaypoint0.blip, coord.x, coord.y, coord.z)
DeleteCheckpoint(selectedWaypoint0.checkpoint)
local color = getCheckpointColor(selectedBlipColor)
local checkpointType = -1 == selectedWaypoint0.number and checkeredFlagCheckpoint or numberedCheckpoint
selectedWaypoint0.checkpoint = makeCheckpoint(checkpointType, selectedWaypoint0.coord, coord, color, 127, selectedIndex0 - 1)
else -- second selected waypoint exists, add waypoint between first and second selected waypoints
for i = #waypoints, selectedIndex1, -1 do
waypoints[i + 1] = waypoints[i]
end
local blip = AddBlipForCoord(coord.x, coord.y, coord.z)
waypoints[selectedIndex1] = {coord = {x = coord.x, y = coord.y, z = coord.z, r = defaultRadius}, checkpoint = nil, blip = blip, sprite = -1, color = -1, number = -1, name = nil}
setStartToFinishBlips()
deleteWaypointCheckpoints()
setStartToFinishCheckpoints()
selectedIndex0 = 0
selectedIndex1 = 0
end
end
trackAccess = nil
savedTrackName = nil
SetBlipRoute(waypoints[1].blip, true)
SetBlipRouteColour(waypoints[1].blip, blipRouteColor)
else -- existing waypoint selected
local selectedWaypoint = waypoints[selectedIndex]
if 0 == selectedIndex0 then -- no previous selected waypoint exists, show that waypoint is selected
setBlipCheckpointColor(selectedWaypoint, selectedBlipColor)
selectedIndex0 = selectedIndex
else -- first selected waypoint exists
if selectedIndex == selectedIndex0 then -- selected waypoint and first selected waypoint are the same, unselect
setBlipCheckpointColor(selectedWaypoint, selectedWaypoint.color)
if selectedIndex1 ~= 0 then
selectedIndex0 = selectedIndex1
selectedIndex1 = 0
else
selectedIndex0 = 0
end
elseif selectedIndex == selectedIndex1 then -- selected waypoint and second selected waypoint are the same, unselect
setBlipCheckpointColor(selectedWaypoint, selectedWaypoint.color)
selectedIndex1 = 0
else -- selected waypoint and first and second selected waypoints are different
if 0 == selectedIndex1 then -- second selected waypoint does not exist
local splitCombine = false
local checkpointType = checkeredFlagCheckpoint
local waypointNum = 0
if true == startIsFinish then
if #waypoints == selectedIndex and 1 == selectedIndex0 then -- split start/finish waypoint
splitCombine = true
startIsFinish = false
waypoints[1].sprite = checkeredFlagSprite
waypoints[1].color = startBlipColor
waypoints[1].number = -1
waypoints[1].name = "Start"
waypoints[#waypoints].sprite = checkeredFlagSprite
waypoints[#waypoints].color = finishBlipColor
waypoints[#waypoints].number = -1
waypoints[#waypoints].name = "Finish"
end
else
if 1 == selectedIndex and #waypoints == selectedIndex0 then -- combine start and finish waypoints
splitCombine = true
startIsFinish = true
waypoints[1].sprite = checkeredFlagSprite
waypoints[1].color = startFinishBlipColor
waypoints[1].number = -1
waypoints[1].name = "Start/Finish"
waypoints[#waypoints].sprite = numberedSprite
waypoints[#waypoints].color = numberedBlipColor
waypoints[#waypoints].number = #waypoints - 1
waypoints[#waypoints].name = "Waypoint"
checkpointType = numberedCheckpoint
waypointNum = #waypoints - 1
end
end
if true == splitCombine then
setBlipProperties(1)
setBlipProperties(#waypoints)
local color = getCheckpointColor(waypoints[1].color)
SetCheckpointRgba(waypoints[1].checkpoint, color.r, color.g, color.b, 127)
SetCheckpointRgba2(waypoints[1].checkpoint, color.r, color.g, color.b, 127)
DeleteCheckpoint(waypoints[#waypoints].checkpoint)
color = getCheckpointColor(waypoints[#waypoints].color)
waypoints[#waypoints].checkpoint = makeCheckpoint(checkpointType, waypoints[#waypoints].coord, waypoints[#waypoints].coord, color, 127, waypointNum)
selectedIndex0 = 0
trackAccess = nil
savedTrackName = nil
else
if selectedIndex0 + 1 == selectedIndex then
setBlipCheckpointColor(selectedWaypoint, selectedBlipColor)
selectedIndex1 = selectedIndex
elseif selectedIndex0 - 1 == selectedIndex then
setBlipCheckpointColor(selectedWaypoint, selectedBlipColor)
selectedIndex1 = selectedIndex0
selectedIndex0 = selectedIndex
else
setBlipCheckpointColor(selectedWaypoint, selectedBlipColor)
setBlipCheckpointColor(waypoints[selectedIndex0], waypoints[selectedIndex0].color)
selectedIndex0 = selectedIndex
end
end
else -- second selected waypoint exists
if selectedIndex1 + 1 == selectedIndex then
setBlipCheckpointColor(selectedWaypoint, selectedBlipColor)
setBlipCheckpointColor(waypoints[selectedIndex0], waypoints[selectedIndex0].color)
selectedIndex0 = selectedIndex1
selectedIndex1 = selectedIndex
elseif selectedIndex0 - 1 == selectedIndex then
setBlipCheckpointColor(selectedWaypoint, selectedBlipColor)
setBlipCheckpointColor(waypoints[selectedIndex1], waypoints[selectedIndex1].color)
selectedIndex1 = selectedIndex0
selectedIndex0 = selectedIndex
else
setBlipCheckpointColor(selectedWaypoint, selectedBlipColor)
setBlipCheckpointColor(waypoints[selectedIndex0], waypoints[selectedIndex0].color)
setBlipCheckpointColor(waypoints[selectedIndex1], waypoints[selectedIndex1].color)
selectedIndex0 = selectedIndex
selectedIndex1 = 0
end
end
end
end
end
end
local function removeAllRacerBlipGT()
for _, racer in pairs(racerBlipGT) do
RemoveBlip(racer.blip)
RemoveMpGamerTag(racer.gamerTag)
end
racerBlipGT = {}
end
local function respawnAI(driver)
local passengers = {}
for seat = 0, GetVehicleModelNumberOfSeats(GetEntityModel(driver.vehicle)) - 2 do
local ped = GetPedInVehicleSeat(driver.vehicle, seat)
if ped ~= 0 then
passengers[#passengers + 1] = {ped = ped, seat = seat}
end
end
local priColor, secColor = GetVehicleColours(driver.vehicle)
local pearlColor, wheelColor = GetVehicleExtraColours(driver.vehicle)
local vehicleHash = GetEntityModel(driver.vehicle)
loadModel(vehicleHash)
SetEntityAsMissionEntity(driver.vehicle, true, true)
DeleteVehicle(driver.vehicle)
local coord = driver.startCoord
if true == aiState.startIsFinish then
if driver.currentWaypoint > 0 then
coord = aiState.waypointCoords[driver.currentWaypoint]
end
else
if driver.currentWaypoint > 1 then
coord = aiState.waypointCoords[driver.currentWaypoint - 1]
end
end
driver.vehicle = putPedInVehicle(driver.ped, vehicleHash, priColor, secColor, pearlColor, wheelColor, coord, driver.heading)
for _, passenger in pairs(passengers) do
SetPedIntoVehicle(passenger.ped, driver.vehicle, passenger.seat)
end
driver.destSet = true
end
local function updateVehicleList()
table.sort(vehicleList)
local html = ""
for _, model in ipairs(vehicleList) do
html = html .. "<option value = \"" .. model .. "\">" .. model .. " (" .. GetLabelText(GetDisplayNameFromVehicleModel(model)) .. ")</option>"
end
SendNUIMessage({
action = "update",
list = "vehicleList",
vehicleList = html
})
end
local function updateStartVehicleList(vclass)
vclass = tonumber(vclass)
local startVehiclesHTML = "<option value = \"any\">Any</option>"
for _, model in ipairs(allVehiclesList) do
if -1 == vclass or GetVehicleClassFromName(model) == vclass then
startVehiclesHTML = startVehiclesHTML .. "<option value = \"" .. model .. "\">" .. model .. " (" .. GetLabelText(GetDisplayNameFromVehicleModel(model)) .. ")</option>"
end
end
SendNUIMessage({
action = "update",
list = "svehicles",
startVehicles = startVehiclesHTML
})
end
local function deleteDriver(name, pIndex)
local driver = aiState.drivers[name]
if nil == driver then
sendMessage("Cannot delete '" .. name .. "'. AI driver not found.\n")
elseif STATE_RACING == driver.state then
sendMessage("Cannot delete '" .. name .. "'. AI driver is in a race.\n")
elseif driver.state ~= STATE_JOINING then
sendMessage("Cannot delete '" .. name .. "'. AI driver is not joined to a race.\n")
else
if driver.ped ~= nil then
TriggerServerEvent("races:leave", pIndex, driver.netID, name)
DeletePed(driver.ped)
SetEntityAsMissionEntity(driver.vehicle, true, true)
DeleteVehicle(driver.vehicle)
end
aiState.numRacing = aiState.numRacing - 1
if aiState.numRacing ~= 0 then
aiState.drivers[name] = nil
else
aiState = nil
end
sendMessage("AI driver '" .. name .. "' deleted.\n")
return true
end
return false
end
local function edit()
if STATE_IDLE == state then
state = STATE_EDITING
SetWaypointOff()
setStartToFinishCheckpoints()
sendMessage("Editing started.\n")
elseif STATE_EDITING == state then
state = STATE_IDLE
highlightedIndex = 0
if selectedIndex0 ~= 0 then
SetBlipColour(waypoints[selectedIndex0].blip, waypoints[selectedIndex0].color)
selectedIndex0 = 0
end
if selectedIndex1 ~= 0 then
SetBlipColour(waypoints[selectedIndex1].blip, waypoints[selectedIndex1].color)
selectedIndex1 = 0
end
deleteWaypointCheckpoints()
sendMessage("Editing stopped.\n")
else
sendMessage("Cannot edit waypoints. Leave race first.\n")
end
end
local function clear()
if STATE_IDLE == state then
for i = 1, #waypoints do
RemoveBlip(waypoints[i].blip)
end
waypoints = {}
startIsFinish = false
trackAccess = nil
savedTrackName = nil
sendMessage("Waypoints cleared.\n")
elseif STATE_EDITING == state then
highlightedIndex = 0
selectedIndex0 = 0
selectedIndex1 = 0
for i = 1, #waypoints do
RemoveBlip(waypoints[i].blip)
DeleteCheckpoint(waypoints[i].checkpoint)
end
waypoints = {}
startIsFinish = false
trackAccess = nil
savedTrackName = nil
sendMessage("Waypoints cleared.\n")
else
sendMessage("Cannot clear waypoints. Leave race first.\n")
end
end
local function reverse()
if #waypoints < 2 then
sendMessage("Cannot reverse waypoints. Track needs to have at least 2 waypoints.\n")
elseif STATE_IDLE == state then
trackAccess = nil
savedTrackName = nil
loadWaypointBlips(waypointsToCoordsRev())
sendMessage("Waypoints reversed.\n")
elseif STATE_EDITING == state then
trackAccess = nil
savedTrackName = nil
highlightedIndex = 0
selectedIndex0 = 0
selectedIndex1 = 0
deleteWaypointCheckpoints()
loadWaypointBlips(waypointsToCoordsRev())
setStartToFinishCheckpoints()
sendMessage("Waypoints reversed.\n")
else
sendMessage("Cannot reverse waypoints. Leave race first.\n")
end
end
local function loadTrack(access, name)
if access ~= "pvt" and access ~= "pub" then
sendMessage("Cannot load track. Invalid access type.\n")
elseif nil == name then
sendMessage("Cannot load track. Name required.\n")
elseif STATE_JOINING == state or STATE_RACING == state then
sendMessage("Cannot load track '" .. name .. "'. Leave race first.\n")
else
TriggerServerEvent("races:load", access, name)
end
end
local function saveTrack(access, name)
if access ~= "pvt" and access ~= "pub" then
sendMessage("Cannot save track. Invalid access type.\n")
elseif nil == name then
sendMessage("Cannot save track. Name required.\n")
elseif #waypoints < 2 then
sendMessage("Cannot save track '" .. name .. "'. Track needs to have at least 2 waypoints.\n")
else
TriggerServerEvent("races:save", access, name, waypointsToCoords())
end
end
local function overwriteTrack(access, name)
if access ~= "pvt" and access ~= "pub" then
sendMessage("Cannot overwrite track. Invalid access type.\n")
elseif nil == name then
sendMessage("Cannot overwrite track. Name required.\n")
elseif #waypoints < 2 then
sendMessage("Cannot overwrite track '" .. name .. "'. Track needs to have at least 2 waypoints.\n")
else
TriggerServerEvent("races:overwrite", access, name, waypointsToCoords())
end
end
local function deleteTrack(access, name)
if access ~= "pvt" and access ~= "pub" then
sendMessage("Cannot delete track. Invalid access type.\n")
elseif nil == name then
sendMessage("Cannot delete track. Name required.\n")
else
TriggerServerEvent("races:delete", access, name)
end
end
local function listTracks(access)
if access ~= "pvt" and access ~= "pub" then
sendMessage("Cannot list tracks. Invalid access type.\n")
else
TriggerServerEvent("races:list", access)
end
end
local function bestLapTimes(access, name)
if access ~= "pvt" and access ~= "pub" then
sendMessage("Cannot list best lap times. Invalid access type.\n")
elseif nil == name then
sendMessage("Cannot list best lap times. Name required.\n")
else
TriggerServerEvent("races:blt", access, name)
end
end
local function spawnAIDriver(name, model, coord, aiHeading)
if nil == name then
sendMessage("Cannot spawn AI driver. Name required.\n")
return false
end
model = model or defaultModel
if false == IsModelInCdimage(model) or false == IsModelAVehicle(model) then
sendMessage("Cannot spawn '" .. name .. "'. Invalid vehicle.\n")
return false
end
local pIndex = GetPlayerServerId(PlayerId())
local race = races[pIndex]
if nil == race then
sendMessage("Cannot spawn '" .. name .. "'. Race has not been registered.\n")
return false
elseif "no" == race.allowAI then
sendMessage("Cannot spawn '" .. name .. "'. AI drivers not allowed.\n")
return false
end
if nil == aiState then
aiState = {
waypointCoords = {},
startIsFinish = false,
numLaps = race.laps,
DNFTimeout = race.timeout * 1000,
beginDNFTimeout = false,
DNFTimeoutStart = -1,
raceType = race.rtype,
startVehicle = race.svehicle,
recurring = "yes" == race.recur,
ordered = "yes" == race.order,
raceStart = -1,
raceDelay = -1,
numRacing = 0,
drivers = {}
}
end
if nil == aiState.drivers[name] then
aiState.drivers[name] = {
state = STATE_JOINING,
started = false,
startCoord = coord,
heading = aiHeading,
destCoord = nil,
destSet = false,
numWaypointsPassed = 0,
currentWaypoint = -1,
currentLap = 1,
lapTimeStart = -1,
bestLapTime = -1,
bestLapVehicleName = nil,
raceVehicleList = nil,
model = nil,
vehicle = nil,
colorPri = -1,
colorSec = -1,
colorPearl = -1,
colorWheel = -1,
ped = nil,
netID = nil,
enteringVehicle = false,
stuckCoord = coord,
stuckStart = -1
}
aiState.numRacing = aiState.numRacing + 1
end
if "rest" == race.rtype then
if model ~= race.restrict then
sendMessage("Cannot spawn '" .. name .. "'. AI driver must be in '" .. race.restrict .. "' vehicle.\n")
return false
end
elseif "class" == race.rtype then
if race.vclass ~= -1 then
if GetVehicleClassFromName(model) ~= race.vclass then