Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 19 additions & 28 deletions [gameplay]/trainhorn/client.lua
Original file line number Diff line number Diff line change
@@ -1,57 +1,48 @@
local spam = {}
local spam = 0

function initBind(thePlayer)
if (thePlayer == localPlayer and getVehicleType(source) == "Train") then
bindKey("H", "down", soundHorn)
function initBind(theVehicle)
if (getVehicleType(theVehicle) == "Train") then
bindKey("horn", "down", soundHorn)
end
end
addEventHandler("onClientVehicleEnter", getRootElement(), initBind)
addEventHandler("onClientPlayerVehicleEnter", localPlayer, initBind)

function soundHorn()
local vehicle = getPedOccupiedVehicle(localPlayer)

if getVehicleType(vehicle) ~= "Train" then return end

-- Make it so they can't play multiple train horns at the same time (causes lag and distortion)
if spam[localPlayer] and getTickCount() - spam[localPlayer] < 5000 then
return
end

if (vehicle and getVehicleController(vehicle) == localPlayer) then
spam[localPlayer] = getTickCount()
x, y, z = getElementPosition(vehicle)
triggerServerEvent("onSyncHorn", getRootElement(), localPlayer, vehicle)
sound = playSound3D("horn.aac", x, y, z, false)
setSoundVolume(sound, 1.0)
if (vehicle and getVehicleType(vehicle) == "Train" and getVehicleController(vehicle) == localPlayer and getTickCount() - spam >= 5000) then
spam = getTickCount()
triggerServerEvent("onSyncHorn", resourceRoot)
local x, y, z = getElementPosition(vehicle)
local sound = playSound3D("horn.aac", x, y, z, false)
attachElements(sound, vehicle)
setSoundMaxDistance(sound, 250)
end
end

function syncedHorn(train, x, y, z)
if (isElement(train) and getVehicleType(train) == "Train") then
sound = playSound3D("horn.aac", x, y, z, false)
setSoundVolume(sound, 1.0)
if (isElement(train) and getVehicleType(train) == "Train" and source ~= localPlayer) then
local sound = playSound3D("horn.aac", x, y, z, false)
attachElements(sound, train)
setSoundMaxDistance(sound, 250)
end
end
addEvent("onPlaySyncedHorn", true)
addEventHandler("onPlaySyncedHorn", localPlayer, syncedHorn)
addEventHandler("onPlaySyncedHorn", root, syncedHorn)

function cleanUp(thePlayer)
if (thePlayer == localPlayer and getVehicleType(source) == "Train") then
unbindKey("H", "down", soundHorn)
function cleanUp(theVehicle)
if (getVehicleType(theVehicle) == "Train") then
unbindKey("horn", "down", soundHorn)
end
end
addEventHandler("onClientVehicleExit", getRootElement(), cleanUp)
addEventHandler("onClientPlayerVehicleExit", localPlayer, cleanUp)

-- Dying in train, so not triggering onClientVehicleExit
-- Dying in train, so not triggering onClientPlayerVehicleExit
function integrityCheck()
local vehicle = getPedOccupiedVehicle(localPlayer)

if (vehicle and getVehicleType(vehicle) == "Train") then
unbindKey("H", "down", soundHorn)
unbindKey("horn", "down", soundHorn)
end
end
addEventHandler("onClientPlayerWasted", localPlayer, integrityCheck)
6 changes: 4 additions & 2 deletions [gameplay]/trainhorn/meta.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<meta>
<info author='MTA contributors (github.com/multitheftauto/mtasa-resources)' version='1.0' name='Train horn' description='Train horn' type='script' />
<info author="MTA contributors (github.com/multitheftauto/mtasa-resources)" version="1.0" name="Train horn" description="Train horn" type="script" />
<min_mta_version server="1.3.0-9.04570" />

<script src="client.lua" type="client" />
<script src="server.lua" type="server" />

<file src="horn.aac" download="true" />
<file src="horn.aac" />
</meta>
21 changes: 15 additions & 6 deletions [gameplay]/trainhorn/server.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
function syncHorn(trainDriver, train)
local spam = {}

local x, y, z = getElementPosition(trainDriver)
local nearbyPlayers = getElementsWithinRange(x, y, z, 250, "player")
function syncHorn()
local vehicle = getPedOccupiedVehicle(client)
if vehicle and getVehicleType(vehicle) == "Train" and getVehicleController(vehicle) == client then
if spam[client] and getTickCount() - spam[client] < 5000 then return end
local x, y, z = getElementPosition(client)
local nearbyPlayers = getElementsWithinRange(x, y, z, 250, "player")
spam[client] = getTickCount()

for _, p in ipairs(nearbyPlayers) do
triggerClientEvent(p, "onPlaySyncedHorn", p, train, x, y, z)
triggerClientEvent(nearbyPlayers, "onPlaySyncedHorn", client, vehicle, x, y, z)
end
end
addEvent("onSyncHorn", true)
addEventHandler("onSyncHorn", resourceRoot, syncHorn)
addEventHandler("onSyncHorn", resourceRoot, syncHorn)

function quitHandler()
spam[source] = nil
end
addEventHandler("onPlayerQuit", root, quitHandler)