-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathcablecar.lua
More file actions
684 lines (592 loc) · 26.3 KB
/
cablecar.lua
File metadata and controls
684 lines (592 loc) · 26.3 KB
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
--[[
Skyway Tram (Cablecar) by GlitchDetector, Nov. 2018
Thanks to:
IllusiveTea for moral support and introduing me to the topic of cable cars
These trams try to sync between players, but is no guaranteed, they only sync whenever the hosts car reaches the bottom
You can enter the cars when they are docked, and you'll have to press E to attach yourself, if you're not attached the car kicks you out
Cars makes noise upon arrival and departure, and makes a running sound while moving
]]
-- Lerp, not to be confused with Liable Emerates Role Play
function Lerp(a, b, t)
return a + (b - a) * t
end
-- Mass lerper
function VecLerp(x1, y1, z1, x2, y2, z2, l, clamp)
if clamp then
if l < 0.0 then l = 0.0 end
if l > 1.0 then l = 1.0 end
end
local x = Lerp(x1, x2, l)
local y = Lerp(y1, y2, l)
local z = Lerp(z1, z2, l)
return vector3(x, y, z)
end
-- The script train tracks (basically every node that the "trains" move between in order)
local TRACKS = {
[0] = { -- Left skytram (from bottom)
vector3(-740.911, 5599.341, 47.25),
vector3(-739.557, 5599.346, 46.997),
vector3(-581.009, 5596.517, 77.379),
vector3(-575.717, 5596.388, 79.22),
vector3(-273.805, 5590.844, 240.795),
vector3(-268.707, 5590.744, 243.395),
vector3(6.896, 5585.668, 423.614),
vector3(11.774, 5585.591, 426.711),
vector3(236.82, 5581.445, 599.642),
vector3(241.365, 5581.369, 603.183),
vector3(412.855, 5578.216, 774.401),
vector3(417.541, 5578.124, 777.688),
vector3(444.93, 5577.589, 786.535),
vector3(446.288, 5577.59, 786.75),
},
[1] = { -- Right skytram (from bottom)
vector3(446.291, 5566.377, 786.75),
vector3(444.937, 5566.383, 786.551),
vector3(417.371, 5567.001, 777.708),
vector3(412.661, 5567.085, 774.439),
vector3(241.31, 5570.594, 603.137),
vector3(236.821, 5570.663, 599.561),
vector3(11.35, 5575.298, 426.629),
vector3(6.575, 5575.391, 423.57),
vector3(-268.965, 5580.996, 243.386),
vector3(-273.993, 5581.124, 240.808),
vector3(-575.898, 5587.286, 79.251),
vector3(-581.321, 5587.4, 77.348),
vector3(-739.646, 5590.614, 47.006),
vector3(-740.97, 5590.617, 47.306),
},
}
-- Cable car data, lots is left-over from experiments and old iterations
local CABLE_CARS = {
[0] = { -- Left track car
entity = nil, -- The car itself
-- Doors (I don't actually know if the left ones are on the left or not)
doorLL = nil,
doorLR = nil,
doorRL = nil,
doorRR = nil,
index = 0, -- The index, used to set the track
position = vector3(0,0,0), -- The current position of the car
direction = 1, -- What direction we're moving (up or down)
gradient = 1, -- Believed to be the gradient during research, but was actually just the current node we're moving from
run_timer = 0, -- Scale used for lerping
altitude = 0, -- Used for the scenic camera in SP, not used here
activation_timer = 0, -- Not used here
gradient_distance = 0.0, -- Distance between the current node we're moving from and the next node
offset_modifier = 0.0, -- Something believed to be an offset modifier
can_move = true, -- Determine if the car can move, not actually used here though
is_player_seated = false, -- Another value from the SP script, not actually used because fucking hell I'm tired
speed = 17.5, -- Movement speed modifier, determines the speed of the car on the track
maxSpeedDistance = 50, -- Distance from station at which the car will attain maximum speed
state = "IDLE", -- The current state of the car
showTramBlips = true, -- Show blip on the map
offset = vector3(-0.2, 0.0, 0.0),
},
[1] = { -- Right track car
entity = nil,
doorLL = nil,
doorLR = nil,
doorRL = nil,
doorRR = nil,
index = 1,
position = vector3(0,0,0),
direction = 1,
gradient = 1,
run_timer = 0,
altitude = 0,
activation_timer = 0,
gradient_distance = 0.0,
offset_modifier = 0.0,
can_move = true,
is_player_seated = false,
speed = 17.5,
maxSpeedDistance = 50,
state = "IDLE",
showTramBlips = true,
offset = vector3(-0.2, 0.0, 0.0),
},
}
Citizen.CreateThread(function()
-- Load the things we need
while not HasModelLoaded("p_cablecar_s") do
RequestModel("p_cablecar_s")
Wait(100)
end
while not HasModelLoaded("p_cablecar_s_door_l") do
RequestModel("p_cablecar_s_door_l")
Wait(100)
end
while not HasModelLoaded("p_cablecar_s_door_r") do
RequestModel("p_cablecar_s_door_r")
Wait(100)
end
while not HasAnimDictLoaded("p_cablecar_s") do
RequestAnimDict("p_cablecar_s")
Wait(100)
end
RequestScriptAudioBank("CABLE_CAR", false, -1)
RequestScriptAudioBank("CABLE_CAR_SOUNDS", false, -1)
LoadStream("CABLE_CAR", "CABLE_CAR_SOUNDS")
LoadStream("CABLE_CAR_SOUNDS", "CABLE_CAR")
-- Spawn all them entities and attach the doors to the cars
CABLE_CARS[0].entity = CreateObjectNoOffset("p_cablecar_s", -740.911, 5599.341, 47.25, 0, 1, 0)
CABLE_CARS[0].doorLL = CreateObjectNoOffset("p_cablecar_s_door_l", -740.911, 5599.341, 47.25, 0, 1, 0)
CABLE_CARS[0].doorLR = CreateObjectNoOffset("p_cablecar_s_door_r", -740.911, 5599.341, 47.25, 0, 1, 0)
AttachEntityToEntity(CABLE_CARS[0].doorLL, CABLE_CARS[0].entity, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0, 1, 0, 2, 1)
AttachEntityToEntity(CABLE_CARS[0].doorLR, CABLE_CARS[0].entity, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0, 1, 0, 2, 1)
CABLE_CARS[0].doorRL = CreateObjectNoOffset("p_cablecar_s_door_l", -740.911, 5599.341, 47.25, 0, 1, 0)
CABLE_CARS[0].doorRR = CreateObjectNoOffset("p_cablecar_s_door_r", -740.911, 5599.341, 47.25, 0, 1, 0)
AttachEntityToEntity(CABLE_CARS[0].doorRL, CABLE_CARS[0].entity, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 180.0, 0, 0, 1, 0, 2, 1)
AttachEntityToEntity(CABLE_CARS[0].doorRR, CABLE_CARS[0].entity, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 180.0, 0, 0, 1, 0, 2, 1)
-- Do the same to the right track car
CABLE_CARS[1].entity = CreateObjectNoOffset("p_cablecar_s", 446.291, 5566.377, 786.75, 0, 1, 0)
CABLE_CARS[1].doorLL = CreateObjectNoOffset("p_cablecar_s_door_l", -740.911, 5599.341, 47.25, 0, 1, 0)
CABLE_CARS[1].doorLR = CreateObjectNoOffset("p_cablecar_s_door_r", -740.911, 5599.341, 47.25, 0, 1, 0)
AttachEntityToEntity(CABLE_CARS[1].doorLL, CABLE_CARS[1].entity, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0, 1, 0, 2, 1)
AttachEntityToEntity(CABLE_CARS[1].doorLR, CABLE_CARS[1].entity, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0, 1, 0, 2, 1)
CABLE_CARS[1].doorRL = CreateObjectNoOffset("p_cablecar_s_door_l", -740.911, 5599.341, 47.25, 0, 1, 0)
CABLE_CARS[1].doorRR = CreateObjectNoOffset("p_cablecar_s_door_r", -740.911, 5599.341, 47.25, 0, 1, 0)
AttachEntityToEntity(CABLE_CARS[1].doorRL, CABLE_CARS[1].entity, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 180.0, 0, 0, 1, 0, 2, 1)
AttachEntityToEntity(CABLE_CARS[1].doorRR, CABLE_CARS[1].entity, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 180.0, 0, 0, 1, 0, 2, 1)
-- Align everything so it fits on the track
FreezeEntityPosition(CABLE_CARS[0].entity, true)
FreezeEntityPosition(CABLE_CARS[1].entity, true)
SetEntityRotation(CABLE_CARS[0].entity, 0.0, 0.0, 270.0, 0, 1)
SetEntityRotation(CABLE_CARS[1].entity, 0.0, 0.0, 90.0, 0, 1)
-- Initialize the state
CABLE_CARS[0].state = "MOVE_TO_IDLE_TOP"
CABLE_CARS[1].state = "MOVE_TO_IDLE_TOP"
-- KickPlayerOutOfMyCablecar(CABLE_CARS[0])
-- KickPlayerOutOfMyCablecar(CABLE_CARS[1])
if CABLE_CARS[0].showTramBlips then
local TramBlip1 = AddBlipForEntity(CABLE_CARS[0].entity)
SetBlipSprite (TramBlip1, 36)
SetBlipDisplay(TramBlip1, 4)
SetBlipScale (TramBlip1, 0.8)
SetBlipColour (TramBlip1, 2)
SetBlipAsShortRange(TramBlip1, true)
BeginTextCommandSetBlipName("STRING")
AddTextComponentString("Pala Springs Tramway")
EndTextCommandSetBlipName(TramBlip1)
end
if CABLE_CARS[1].showTramBlips then
local TramBlip2 = AddBlipForEntity(CABLE_CARS[1].entity)
SetBlipSprite (TramBlip2, 36)
SetBlipDisplay(TramBlip2, 4)
SetBlipScale (TramBlip2, 0.8)
SetBlipColour (TramBlip2, 2)
SetBlipAsShortRange(TramBlip2, true)
BeginTextCommandSetBlipName("STRING")
AddTextComponentString("Pala Springs Tramway")
EndTextCommandSetBlipName(TramBlip2)
end
-- Control movement forever
while true do
Wait(0)
for ccIndex, cablecar in next, CABLE_CARS do
UpdateCablecarMovement(cablecar)
end
end
end)
RegisterNetEvent("omni:cablecar:forceState")
AddEventHandler("omni:cablecar:forceState", function(index, state)
local cablecar = CABLE_CARS[index]
if state == "IDLE_BOTTOM" then
cablecar.state = "MOVE_TO_IDLE_BOTTOM"
cablecar.run_timer = 0.0
end
if state == "IDLE_TOP" then
cablecar.state = "MOVE_TO_IDLE_TOP"
cablecar.run_timer = 0.0
end
if state == "MOVE_DOWN" then
cablecar.state = "IDLE_TO_MOVE_DOWN"
cablecar.gradient = #TRACKS[index]
cablecar.gradient_distance = 0.0
cablecar.run_timer = 0.0
end
if state == "MOVE_UP" then
cablecar.state = "IDLE_TO_MOVE_UP"
cablecar.gradient = 1
cablecar.gradient_distance = 0.0
cablecar.run_timer = 0.0
end
end)
AddEventHandler("onResourceStop", function(name)
-- Delete all cable car things if the resource stops, just so we don't have cable cars galore sitting around
if name == GetCurrentResourceName() then
if CABLE_CARS[0].is_player_seated then
KickPlayerOutOfMyCablecar(CABLE_CARS[0])
end
if CABLE_CARS[1].is_player_seated then
KickPlayerOutOfMyCablecar(CABLE_CARS[1])
end
DeleteEntity(CABLE_CARS[0].entity)
DeleteEntity(CABLE_CARS[1].entity)
DeleteEntity(CABLE_CARS[0].doorLL)
DeleteEntity(CABLE_CARS[1].doorLL)
DeleteEntity(CABLE_CARS[0].doorLR)
DeleteEntity(CABLE_CARS[1].doorLR)
DeleteEntity(CABLE_CARS[0].doorRL)
DeleteEntity(CABLE_CARS[1].doorRL)
DeleteEntity(CABLE_CARS[0].doorRR)
DeleteEntity(CABLE_CARS[1].doorRR)
end
end)
function DrawCablecarText3D(text, x, y, z, s, font, a)
local onScreen, _x, _y = World3dToScreen2d(x, y, z)
local px, py, pz = table.unpack(GetGameplayCamCoords())
local dist = GetDistanceBetweenCoords(px, py, pz, x, y, z, 1)
if s == nil then
s = 1.0
end
if font == nil then
font = 4
end
if a == nil then
a = 255
end
local scale = ((1 / dist) * 2) * s
local fov = (1 / GetGameplayCamFov()) * 100
local scale = scale * fov
if onScreen then
if true then
SetDrawOrigin(x, y, z, 0)
end
SetTextScale(0.0 * scale, 1.1 * scale)
if true then
SetTextFont(font)
else
SetTextFont(font)
end
SetTextProportional(1)
-- SetTextScale(0.0, 0.55)
SetTextColour(255, 255, 255, a)
-- SetTextDropshadow(0, 0, 0, 0, 255)
SetTextEdge(2, 0, 0, 0, 150)
SetTextDropShadow()
SetTextOutline()
SetTextEntry("STRING")
SetTextCentre(1)
AddTextComponentString(text)
if true then
DrawText(0.0, 0.0)
ClearDrawOrigin()
else
DrawText(_x, _y)
end
end
end
-- Control the movements of a car
function UpdateCablecarMovement(cablecar)
if cablecar.state == "MOVE_UP" then
-- Assign a directional value used to determine the moving direction of the car
cablecar.direction = 1.0
-- Get the previous node and the next node (basically the two points of the track on each side of us)
local _prev, _next = TRACKS[cablecar.index][cablecar.gradient], TRACKS[cablecar.index][cablecar.gradient + 1]
-- Initialize the segment distance if there is none already
if cablecar.gradient_distance == 0.0 then
cablecar.gradient_distance = GetDistanceBetweenCoords(_prev, _next, true)
end
-- Calculate the speed we want to move between segments
local dist = cablecar.gradient_distance
local speed = ((1.0 / dist) * Timestep()) * cablecar.speed
local distanceFromOrigin = GetDistanceBetweenCoords(TRACKS[cablecar.index][#TRACKS[cablecar.index]], cablecar.position, true)
local distanceFromDestin = GetDistanceBetweenCoords(TRACKS[cablecar.index][1], cablecar.position, true)
if distanceFromOrigin <= cablecar.maxSpeedDistance then
speed = speed * math.abs(distanceFromOrigin + 1)/cablecar.maxSpeedDistance
elseif distanceFromDestin <= cablecar.maxSpeedDistance then
speed = speed * math.abs(distanceFromDestin + 1)/cablecar.maxSpeedDistance
end
-- Add the speed to the timer
cablecar.run_timer = cablecar.run_timer + speed
-- Check if we've reached the end of the segment
if cablecar.run_timer > 1.0 then
-- Add one to the current node index we're at
cablecar.gradient = cablecar.gradient + 1
-- Get the next set of nodes
_prev, _next = TRACKS[cablecar.index][cablecar.gradient], TRACKS[cablecar.index][cablecar.gradient + 1]
cablecar.gradient_distance = GetDistanceBetweenCoords(_prev, _next, true)
cablecar.run_timer = 0.0
-- Check if we've reached the top
if cablecar.gradient >= #TRACKS[cablecar.index] then
cablecar.state = "MOVE_TO_IDLE_TOP"
cablecar.gradient_distance = 0.0
return
end
-- Update the cars hook angle
UpdateCablecarGradient(cablecar)
else
-- Update the position of the car
cablecar.position = VecLerp(_prev.x, _prev.y, _prev.z, _next.x, _next.y, _next.z, cablecar.run_timer, true)
end
-- Add a bit of "hang" on the long segments since the cable sags slightly (ATTENTION TO DETAIL!! xd)
local zLerp = 0.0
if cablecar.gradient_distance > 30.0 then
zLerp = (-1.0 + math.abs(Lerp(1.0, -1.0, cablecar.run_timer))) * 0.25
end
-- Set the position of the car
SetEntityCoords(cablecar.entity, cablecar.position + cablecar.offset + vector3(0.0, 0.0, zLerp), 1, false, 0, 1)
GivePlayerOptionToJoinMyCablecar(cablecar, true)
elseif cablecar.state == "MOVE_DOWN" then
-- Assign a directional value used to determine the moving direction of the car
cablecar.direction = -1.0
-- Get the previous node and the next node (basically the two points of the track on each side of us)
local _prev, _next = TRACKS[cablecar.index][cablecar.gradient], TRACKS[cablecar.index][cablecar.gradient - 1]
-- Initialize the segment distance if there is none already
if cablecar.gradient_distance == 0.0 then
cablecar.gradient_distance = GetDistanceBetweenCoords(_prev, _next, true)
end
-- Calculate the speed we want to move between segments
local dist = cablecar.gradient_distance
local speed = ((1.0 / dist) * Timestep()) * cablecar.speed
local distanceFromOrigin = GetDistanceBetweenCoords(TRACKS[cablecar.index][#TRACKS[cablecar.index]], cablecar.position, true)
local distanceFromDestin = GetDistanceBetweenCoords(TRACKS[cablecar.index][1], cablecar.position, true)
if distanceFromOrigin <= cablecar.maxSpeedDistance then
speed = speed * math.abs(distanceFromOrigin + 1)/cablecar.maxSpeedDistance
elseif distanceFromDestin <= cablecar.maxSpeedDistance then
speed = speed * math.abs(distanceFromDestin + 1)/cablecar.maxSpeedDistance
end
-- Add the speed to the timer
cablecar.run_timer = cablecar.run_timer + speed
-- Check if we've reached the end of the segment
if cablecar.run_timer > 1.0 then
-- Take one from the current node index we're at, since we're going backwards
cablecar.gradient = cablecar.gradient - 1
-- Get the next set of nodes
_prev, _next = TRACKS[cablecar.index][cablecar.gradient], TRACKS[cablecar.index][cablecar.gradient - 1]
cablecar.gradient_distance = GetDistanceBetweenCoords(_prev, _next, true)
cablecar.run_timer = 0.0
-- Check if we've reached the bottom again
if cablecar.gradient <= 1 then
-- Set to raw idle to do nothing and ask the server to sync cars
cablecar.state = "IDLE"
cablecar.gradient_distance = 0.0
TriggerServerEvent("omni:cablecar:host:sync", cablecar.index, "IDLE_BOTTOM")
return
end
-- Update the cars hook angle
UpdateCablecarGradient(cablecar)
else
cablecar.position = VecLerp(_prev.x, _prev.y, _prev.z, _next.x, _next.y, _next.z, cablecar.run_timer, true)
end
-- Add a bit of "hang" on the long segments since the cable sags slightly (ATTENTION TO DETAIL!! xd)
local zLerp = 0.0
if cablecar.gradient_distance > 20.0 then
zLerp = (-1.0 + math.abs(Lerp(1.0, -1.0, cablecar.run_timer))) * 0.25
end
-- Set the position of the car
SetEntityCoords(cablecar.entity, cablecar.position + cablecar.offset + vector3(0.0, 0.0, zLerp), 1, false, 0, 1)
GivePlayerOptionToJoinMyCablecar(cablecar, true)
elseif cablecar.state == "IDLE_TO_MOVE_UP" then
cablecar.gradient = 1
cablecar.gradient_distance = 0.0
cablecar.run_timer = 0.0
if cablecar.is_player_seated then
-- add scenic camera
else
CheckIfPlayerShouldBeKickedOut(cablecar)
end
-- Close doors
SetCablecarDoors(cablecar, false)
cablecar.audio = GetSoundId()
PlaySoundFromEntity(cablecar.audio, "Running", cablecar.entity, "CABLE_CAR_SOUNDS", 0, 0)
cablecar.state = "MOVE_UP"
elseif cablecar.state == "IDLE_TO_MOVE_DOWN" then
cablecar.gradient = #TRACKS[cablecar.index]
cablecar.gradient_distance = 0.0
cablecar.run_timer = 0.0
if cablecar.is_player_seated then
-- add scenic camera
else
CheckIfPlayerShouldBeKickedOut(cablecar)
end
-- Close doors
SetCablecarDoors(cablecar, false)
cablecar.audio = GetSoundId()
PlaySoundFromEntity(cablecar.audio, "Running", cablecar.entity, "CABLE_CAR_SOUNDS", 0, 0)
cablecar.state = "MOVE_DOWN"
elseif cablecar.state == "MOVE_TO_IDLE_TOP" then
cablecar.position = TRACKS[cablecar.index][#TRACKS[cablecar.index]]
SetEntityCoords(cablecar.entity, cablecar.position + cablecar.offset + vector3(0.0, 0.0, 0.0), 1, false, 0, 1)
if cablecar.is_player_seated then
-- kick player out
-- KickPlayerOutOfMyCablecar(cablecar)
-- cablecar.is_player_seated = false
end
-- Open doors
SetCablecarDoors(cablecar, true)
ReleaseRunningSound(cablecar)
cablecar.state = "IDLE_TOP"
cablecar.run_timer = 0.0
elseif cablecar.state == "MOVE_TO_IDLE_BOTTOM" then
cablecar.position = TRACKS[cablecar.index][1]
SetEntityCoords(cablecar.entity, cablecar.position + cablecar.offset + vector3(0.0, 0.0, 0.0), 1, false, 0, 1)
if cablecar.is_player_seated then
-- kick player out
-- KickPlayerOutOfMyCablecar(cablecar)
-- cablecar.is_player_seated = false
end
-- Open doors
SetCablecarDoors(cablecar, true)
ReleaseRunningSound(cablecar)
cablecar.state = "IDLE_BOTTOM"
cablecar.run_timer = 0.0
elseif cablecar.state == "IDLE_TOP" then
-- Idle state for idling at the top station
-- Wait 10 seconds (if that's even how it works lmao)
cablecar.run_timer = cablecar.run_timer + (Timestep() / 20.0)
-- If the time is up we start moving down
if cablecar.run_timer > 1.0 then
cablecar.state = "IDLE_TO_MOVE_DOWN"
cablecar.run_timer = 0.0
end
GivePlayerOptionToJoinMyCablecar(cablecar)
elseif cablecar.state == "IDLE_BOTTOM" then
-- Idle state for idling at the bottom station
-- Wait 10 seconds (if that's even how it works lmao)
cablecar.run_timer = cablecar.run_timer + (Timestep() / 20.0)
-- If the time is up we start moving up
if cablecar.run_timer > 1.0 then
cablecar.state = "IDLE_TO_MOVE_UP"
cablecar.run_timer = 0.0
end
GivePlayerOptionToJoinMyCablecar(cablecar)
elseif cablecar.state == "IDLE" then
-- Just a default state, it does absolutely fuck all
-- Used to halt movement until host and server sync is done
end
end
function ReleaseRunningSound(cablecar)
if cablecar.audio ~= -1 and cablecar.audio ~= nil then
StopSound(cablecar.audio)
ReleaseSoundId(cablecar.audio)
cablecar.audio = -1
end
end
function CheckIfPlayerShouldBeKickedOut(cablecar)
local ply = PlayerPedId()
local pos = cablecar.position + vector3(0.0, 0.0, -5.3)
local plypos = GetEntityCoords(ply, true)
local dist = #(pos - plypos)
if dist < 3.0 then
KickPlayerOutOfMyCablecar(cablecar)
end
end
function KickPlayerOutOfMyCablecar(cablecar)
local ply = PlayerPedId()
cablecar.is_player_seated = false
DetachEntity(ply, 0, 0)
local _, rightvec, _ = GetEntityMatrix(cablecar.entity)
local right = vector3(rightvec.x * 3.5, rightvec.y * 3.5, rightvec.z * 3.5)
SetEntityCoords(ply, cablecar.position + right + vector3(0.0, 0.0, -5.3), xAxis, yAxis, zAxis, clearArea)
end
function GivePlayerOptionToJoinMyCablecar(cablecar, moving)
local ply = PlayerPedId()
local pos = cablecar.position + vector3(0.0, 0.0, -5.3)
if not cablecar.is_player_seated then
local plypos = GetEntityCoords(ply, true)
local dist = #(pos - plypos)
if dist < 3.0 then
DrawCablecarText3D("Press ~g~E ~w~to enter the cablecar", pos.x, pos.y, pos.z + 1.0)
if IsControlJustPressed(0, 38) then
cablecar.is_player_seated = true
AttachEntityToEntity(ply, cablecar.entity, 0, (plypos - cablecar.position), GetEntityRotation(ply, 0), 0, 0, 0, 1, 0, 0)
end
end
else
-- give player option to exit
if not moving then
DrawCablecarText3D("Press ~g~E ~w~to exit the cablecar", pos.x, pos.y, pos.z + 1.0)
if IsControlJustPressed(0, 38) then
cablecar.is_player_seated = false
DetachEntity(ply, 0, 0)
end
end
end
end
function SetCablecarDoors(cablecar, state)
local doorClosePos = 0.95
local doorOpenDist = 0.9
if state == true then
doorStart = doorClosePos
doorDirect = 1
PlaySoundFromEntity(-1, "Arrive_Station", cablecar.entity, "CABLE_CAR_SOUNDS", 0, 0)
PlaySoundFromEntity(-1, "DOOR_OPEN", cablecar.entity, "CABLE_CAR_SOUNDS", 0, 0)
else
doorStart = doorClosePos+doorOpenDist
doorDirect = -1
PlaySoundFromEntity(-1, "Leave_Station", cablecar.entity, "CABLE_CAR_SOUNDS", 0, 0)
PlaySoundFromEntity(-1, "DOOR_CLOSE", cablecar.entity, "CABLE_CAR_SOUNDS", 0, 0)
end
for i = 0,100,1
do
local doorPos = doorStart+doorDirect*doorOpenDist*(i/100)
DetachEntity(cablecar.doorLL, 0, 0)
DetachEntity(cablecar.doorLR, 0, 0)
DetachEntity(cablecar.doorRL, 0, 0)
DetachEntity(cablecar.doorRR, 0, 0)
AttachEntityToEntity(cablecar.doorLL, cablecar.entity, 0, 0.0, -doorPos, 0.0, 0.0, 0.0, 0.0, 0, 0, 1, 0, 2, 1)
AttachEntityToEntity(cablecar.doorLR, cablecar.entity, 0, 0.0, doorPos, 0.0, 0.0, 0.0, 0.0, 0, 0, 1, 0, 2, 1)
AttachEntityToEntity(cablecar.doorRL, cablecar.entity, 0, 0.0, doorPos, 0.0, 0.0, 0.0, 180.0, 0, 0, 1, 0, 2, 1)
AttachEntityToEntity(cablecar.doorRR, cablecar.entity, 0, 0.0, -doorPos, 0.0, 0.0, 0.0, 180.0, 0, 0, 1, 0, 2, 1)
Wait(10)
end
Wait(2000)
end
-- Check what direction the specific car is going
function WhatDirectionDoesMyCablecarGo(cablecar)
if cablecar.index == 0 then
if cablecar.direction >= 0 then
return 0
else
return 1
end
else
-- since they start on opposing ends, the right car is reversed and treats up as down and down as up
if cablecar.direction >= 0 then
return 1
else
return 0
end
end
end
-- Set the hook angle using magical anims, doesn't work properly backwards but eh whatever
function UpdateCablecarGradient(cablecar)
local text = "C" .. (cablecar.index + 1)
if WhatDirectionDoesMyCablecarGo(cablecar) == 0 then
local _data = {
[0] = "_up_9",
[1] = "_up_1",
[3] = "_up_3",
[5] = "_up_4",
[7] = "_up_5",
[9] = "_up_6",
[11] = "_up_8",
[12] = "_up_9",
}
if _data[cablecar.gradient - 1] then
text = text .. _data[cablecar.gradient - 1]
else
return 0
end
else
local _data = {
[0] = "_down_1",
[1] = "_down_2",
[3] = "_down_3",
[5] = "_down_4",
[7] = "_down_5",
[9] = "_down_6",
[11] = "_down_8",
[12] = "_down_9",
}
if _data[cablecar.gradient - 1] then
text = text .. _data[cablecar.gradient - 1]
else
return 0
end
end
PlayEntityAnim(cablecar.entity, text, "p_cablecar_s", 8.0, false, 1, 0, 0, 0)
return 1
end