Skip to content

Commit b3760b7

Browse files
authored
trainhorn: refactor (#322)
1 parent 5f0ae36 commit b3760b7

File tree

3 files changed

+38
-36
lines changed

3 files changed

+38
-36
lines changed

[gameplay]/trainhorn/client.lua

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,48 @@
1-
local spam = {}
1+
local spam = 0
22

3-
function initBind(thePlayer)
4-
if (thePlayer == localPlayer and getVehicleType(source) == "Train") then
5-
bindKey("H", "down", soundHorn)
3+
function initBind(theVehicle)
4+
if (getVehicleType(theVehicle) == "Train") then
5+
bindKey("horn", "down", soundHorn)
66
end
77
end
8-
addEventHandler("onClientVehicleEnter", getRootElement(), initBind)
8+
addEventHandler("onClientPlayerVehicleEnter", localPlayer, initBind)
99

1010
function soundHorn()
1111
local vehicle = getPedOccupiedVehicle(localPlayer)
12-
13-
if getVehicleType(vehicle) ~= "Train" then return end
14-
1512
-- Make it so they can't play multiple train horns at the same time (causes lag and distortion)
16-
if spam[localPlayer] and getTickCount() - spam[localPlayer] < 5000 then
17-
return
18-
end
19-
20-
if (vehicle and getVehicleController(vehicle) == localPlayer) then
21-
spam[localPlayer] = getTickCount()
22-
x, y, z = getElementPosition(vehicle)
23-
triggerServerEvent("onSyncHorn", getRootElement(), localPlayer, vehicle)
24-
sound = playSound3D("horn.aac", x, y, z, false)
25-
setSoundVolume(sound, 1.0)
13+
if (vehicle and getVehicleType(vehicle) == "Train" and getVehicleController(vehicle) == localPlayer and getTickCount() - spam >= 5000) then
14+
spam = getTickCount()
15+
triggerServerEvent("onSyncHorn", resourceRoot)
16+
local x, y, z = getElementPosition(vehicle)
17+
local sound = playSound3D("horn.aac", x, y, z, false)
2618
attachElements(sound, vehicle)
2719
setSoundMaxDistance(sound, 250)
2820
end
2921
end
3022

3123
function syncedHorn(train, x, y, z)
32-
if (isElement(train) and getVehicleType(train) == "Train") then
33-
sound = playSound3D("horn.aac", x, y, z, false)
34-
setSoundVolume(sound, 1.0)
24+
if (isElement(train) and getVehicleType(train) == "Train" and source ~= localPlayer) then
25+
local sound = playSound3D("horn.aac", x, y, z, false)
3526
attachElements(sound, train)
3627
setSoundMaxDistance(sound, 250)
3728
end
3829
end
3930
addEvent("onPlaySyncedHorn", true)
40-
addEventHandler("onPlaySyncedHorn", localPlayer, syncedHorn)
31+
addEventHandler("onPlaySyncedHorn", root, syncedHorn)
4132

42-
function cleanUp(thePlayer)
43-
if (thePlayer == localPlayer and getVehicleType(source) == "Train") then
44-
unbindKey("H", "down", soundHorn)
33+
function cleanUp(theVehicle)
34+
if (getVehicleType(theVehicle) == "Train") then
35+
unbindKey("horn", "down", soundHorn)
4536
end
4637
end
47-
addEventHandler("onClientVehicleExit", getRootElement(), cleanUp)
38+
addEventHandler("onClientPlayerVehicleExit", localPlayer, cleanUp)
4839

49-
-- Dying in train, so not triggering onClientVehicleExit
40+
-- Dying in train, so not triggering onClientPlayerVehicleExit
5041
function integrityCheck()
5142
local vehicle = getPedOccupiedVehicle(localPlayer)
5243

5344
if (vehicle and getVehicleType(vehicle) == "Train") then
54-
unbindKey("H", "down", soundHorn)
45+
unbindKey("horn", "down", soundHorn)
5546
end
5647
end
5748
addEventHandler("onClientPlayerWasted", localPlayer, integrityCheck)

[gameplay]/trainhorn/meta.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<meta>
2-
<info author='MTA contributors (github.com/multitheftauto/mtasa-resources)' version='1.0' name='Train horn' description='Train horn' type='script' />
2+
<info author="MTA contributors (github.com/multitheftauto/mtasa-resources)" version="1.0" name="Train horn" description="Train horn" type="script" />
3+
<min_mta_version server="1.3.0-9.04570" />
4+
35
<script src="client.lua" type="client" />
46
<script src="server.lua" type="server" />
57

6-
<file src="horn.aac" download="true" />
8+
<file src="horn.aac" />
79
</meta>

[gameplay]/trainhorn/server.lua

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
function syncHorn(trainDriver, train)
1+
local spam = {}
22

3-
local x, y, z = getElementPosition(trainDriver)
4-
local nearbyPlayers = getElementsWithinRange(x, y, z, 250, "player")
3+
function syncHorn()
4+
local vehicle = getPedOccupiedVehicle(client)
5+
if vehicle and getVehicleType(vehicle) == "Train" and getVehicleController(vehicle) == client then
6+
if spam[client] and getTickCount() - spam[client] < 5000 then return end
7+
local x, y, z = getElementPosition(client)
8+
local nearbyPlayers = getElementsWithinRange(x, y, z, 250, "player")
9+
spam[client] = getTickCount()
510

6-
for _, p in ipairs(nearbyPlayers) do
7-
triggerClientEvent(p, "onPlaySyncedHorn", p, train, x, y, z)
11+
triggerClientEvent(nearbyPlayers, "onPlaySyncedHorn", client, vehicle, x, y, z)
812
end
913
end
1014
addEvent("onSyncHorn", true)
11-
addEventHandler("onSyncHorn", resourceRoot, syncHorn)
15+
addEventHandler("onSyncHorn", resourceRoot, syncHorn)
16+
17+
function quitHandler()
18+
spam[source] = nil
19+
end
20+
addEventHandler("onPlayerQuit", root, quitHandler)

0 commit comments

Comments
 (0)