Skip to content
This repository has been archived by the owner on Mar 21, 2021. It is now read-only.

Commit

Permalink
Changed fire siren audio system
Browse files Browse the repository at this point in the history
DO NOT BLINDLY UPDATE // THIS IS A DEVELOPMENT BUILD

Changed the audio system for the fire siren to match that of the pass alarm, in that instead of only being able to play one sound on the client at one time, now as many fire sirens as you want can be heard at the same time, meaning siren radiuses can now overlap without any issues. You can even stagger start them, if you want them to sound cooler. Tested on localhost with no issues. If you test this resource in your server, please report back to us! We have not thoroughly tested this update and would appreciate your feedback.
  • Loading branch information
Christopher committed Dec 23, 2019
1 parent 3e91383 commit 12e7b66
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 115 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Inferno Collection Fire/EMS Pager + Fire Siren Version 4.52 Alpha
# Inferno Collection Fire/EMS Pager + Fire Siren Version 4.53 Alpha
#
# Copyright (c) 2019, Christopher M, Inferno Collection. All rights reserved.
#
Expand Down
2 changes: 1 addition & 1 deletion [inferno-collection]/inferno-fire-ems-pager/__resource.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Inferno Collection Fire/EMS Pager + Fire Siren Version 4.52 Alpha
-- Inferno Collection Fire/EMS Pager + Fire Siren Version 4.53 Alpha
--
-- Copyright (c) 2019, Christopher M, Inferno Collection. All rights reserved.
--
Expand Down
184 changes: 86 additions & 98 deletions [inferno-collection]/inferno-fire-ems-pager/client.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Inferno Collection Fire/EMS Pager + Fire Siren Version 4.52 Alpha
-- Inferno Collection Fire/EMS Pager + Fire Siren Version 4.53 Alpha
--
-- Copyright (c) 2019, Christopher M, Inferno Collection. All rights reserved.
--
Expand Down Expand Up @@ -67,8 +67,6 @@ Pager.Tones = Config.Tones

-- Local Fire Siren Variables
local FireSiren = {}
-- Is a fire siren currently being paged
FireSiren.Enabled = false
-- Stations that currently have a fire siren being played
FireSiren.EnabledStations = {}
-- Fire Station Variables
Expand Down Expand Up @@ -317,41 +315,44 @@ end)
-- Used to play a fire siren at a specific station/s
RegisterCommand("firesiren", function(Source, Args)
if Whitelist.Command.firesiren then
if not FireSiren.Enabled then
local ToBeSirened = {}
-- Loop though all the stations provided in the command
for _, ProvidedStation in ipairs(Args) do
-- Loop through all the valid stations
for _, ValidStation in ipairs(FireSiren.Stations) do
-- If a provided station matches a valid station
if ProvidedStation:lower() == ValidStation.Name then
local ToBeSirened = {}
-- Loop though all the stations provided in the command
for _, ProvidedStation in ipairs(Args) do
-- Loop through all the valid stations
for _, ValidStation in ipairs(FireSiren.Stations) do
-- If a provided station matches a valid station
if ProvidedStation:lower() == ValidStation.Name then
-- If station is not already playing a siren
if not FireSiren.EnabledStations[ProvidedStation:lower()] then
ValidStation.x, ValidStation.y, ValidStation.z = table.unpack(ValidStation.Loc)
table.insert(ToBeSirened, ValidStation)
-- Station is already playing a siren
else
NewNoti("~r~~h~One or more stations provided are already sounding!", true)
return
end
end
end
end

-- If the number of originally provided stations matches
-- the number of stations, and there where stations acutally
-- provided in the first place
if not #Args ~= #ToBeSirened and #Args ~= 0 then
-- Create a temporary variable to add more text to
local NotificationText = "~g~Sounding:~y~"
-- Loop though all stations
for _, Station in ipairs(ToBeSirened) do
-- Add station to temporary variable
NotificationText = NotificationText .. " " .. Station.Name:upper()
end
NewNoti(NotificationText, false)
-- Bounces stations off of server
TriggerServerEvent("Fire-EMS-Pager:SoundSirens", ToBeSirened)
-- If there is a mismatch, i.e. invalid/no stations/s provided
else
NewNoti("~r~~h~Invalid stations for sounding, please check your command arguments.", true)
-- If the number of originally provided stations matches
-- the number of stations, and there where stations acutally
-- provided in the first place
if not #Args ~= #ToBeSirened and #Args ~= 0 then
-- Create a temporary variable to add more text to
local NotificationText = "~g~Sounding:~y~"
-- Loop though all stations
for _, Station in ipairs(ToBeSirened) do
TriggerServerEvent("Fire-EMS-Pager:StoreSiren", Station)
-- Add station to temporary variable
NotificationText = NotificationText .. " " .. Station.Name:upper()
end
-- If sirens are already being sounded
NewNoti(NotificationText, false)
Citizen.Wait(2000)
TriggerServerEvent("Fire-EMS-Pager:SoundSirens", ToBeSirened)
-- If there is a mismatch, i.e. invalid/no stations/s provided
else
NewNoti("~r~~h~Sirens are already being sounded!", true)
NewNoti("~r~~h~Invalid stations for sounding, please check your command arguments.", true)
end
-- If player is not whitelisted
else
Expand Down Expand Up @@ -531,25 +532,16 @@ AddEventHandler("Fire-EMS-Pager:PlayTones", function(Tones, HasDetails, Details)

-- If player is tuned to this tone
if Tuned then
SendNUIMessage({
PayloadType = "PlayTone",
Payload = "vibrate"
})
TriggerEvent("Fire-EMS-Pager:Bounce:NUI", "PlayTone", "vibrate")
NewNoti("~h~~y~" .. Tone:upper() .. " call!", true)
-- If player is not tuned to it
else
SendNUIMessage({
PayloadType = "PlayTone",
Payload = Tone
})
TriggerEvent("Fire-EMS-Pager:Bounce:NUI", "PlayTone", Tone)
end
Citizen.Wait(Pager.WaitTime)
end

SendNUIMessage({
PayloadType = "PlayTone",
Payload = "end"
})
TriggerEvent("Fire-EMS-Pager:Bounce:NUI", "PlayTone", "end")

local Hours = GetClockHours()
local Minutes = GetClockMinutes()
Expand Down Expand Up @@ -623,25 +615,10 @@ end)

-- Play fire sirens
RegisterNetEvent("Fire-EMS-Pager:PlaySirens")
AddEventHandler("Fire-EMS-Pager:PlaySirens", function(Stations)
FireSiren.Enabled = true

-- Loop though all stations
for _, Station in ipairs(Stations) do
Station.Loc = vector3(Station.x, Station.y, Station.z)
-- Insert temporary array into enabled stations
table.insert(FireSiren.EnabledStations, Station)
AddEventHandler("Fire-EMS-Pager:PlaySirens", function()
for _, Station in pairs(FireSiren.EnabledStations) do
TriggerEvent("Fire-EMS-Pager:Bounce:NUI", "PlaySiren", {Station.Name, Station.ID})
end

Citizen.Wait(1000)

SendNUIMessage({
PayloadType = "PlaySiren"
})

Citizen.Wait(51000)

FireSiren.Enabled = false
end)

-- Plays cancelpage sound on the client
Expand All @@ -668,10 +645,7 @@ AddEventHandler("Fire-EMS-Pager:CancelPage", function(Tones, HasDetails, Details
NewNoti("~g~~h~Your pager activates!", true)
Citizen.Wait(1500)

SendNUIMessage({
PayloadType = "PlayTone",
Payload = "cancel"
})
TriggerEvent("Fire-EMS-Pager:Bounce:NUI", "PlayTone", "cancel")

if HasDetails then
local NewDetails = ""
Expand Down Expand Up @@ -714,6 +688,29 @@ AddEventHandler("Fire-EMS-Pager:CancelPage", function(Tones, HasDetails, Details
Pager.Paging = false
end)

RegisterNUICallback("RemoveSiren", function(Station)
-- If the client is the owner of this fire siren, remove fire siren
-- This check is done so only one event is sent to the server instead of 32+
if GetPlayerServerId(PlayerId()) == Station.ID then
TriggerServerEvent("Fire-EMS-Pager:RemoveSiren", Station.Name)
end

FireSiren.EnabledStations[Station.Name] = nil
end)

-- Bounce between server script and client script
RegisterNetEvent("Fire-EMS-Pager:Bounce:ServerValues")
AddEventHandler("Fire-EMS-Pager:Bounce:ServerValues", function(Sirens) FireSiren.EnabledStations = Sirens end)

-- Bounce between server & client script and client NUI
RegisterNetEvent("Fire-EMS-Pager:Bounce:NUI")
AddEventHandler("Fire-EMS-Pager:Bounce:NUI", function(Type, Load)
SendNUIMessage({
PayloadType = Type,
Payload = Load
})
end)

-- Draws notification on client's screen
function NewNoti(Text, Flash)
if not Config.DisableAllMessages then
Expand All @@ -726,45 +723,36 @@ function NewNoti(Text, Flash)
end
end

-- Resource master loop
-- Volume loop
-- Sets the volumes of all active fire sirens
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)

if FireSiren.Enabled then
-- Get player position
local pP = GetEntityCoords(GetPlayerPed(-1), false)
local StationDistances = {}

-- Loop though all valid stations
for _, Station in ipairs(FireSiren.Stations) do
-- Calculate distance between player and station
local Distance = GetDistanceBetweenCoords(pP.x, pP.y, pP.z, Station.Loc.x, Station.Loc.y, Station.Loc.z, true)
-- Insert distance into temporary array
table.insert(StationDistances, {Name = Station.Name, Distance = Distance + 0.01}) -- Stops divide by 0 errors
end
-- Sort array so the closest station to the player is first
table.sort(StationDistances, function(A, B) return A.Distance < B.Distance end)
-- Loop though all enabled stations
for _, Station in ipairs(FireSiren.EnabledStations) do
-- If the closest station to the player is an enabled station
if StationDistances[1].Name == Station.Name then
-- If the distance to the closest station is within the fire siren radius
if (StationDistances[1].Distance <= Station.Radius) then
-- Volume is equal to 1 (max volume) mius the distance to the nearest station from the player
-- divided the radius of the fire siren
-- New NUI message
SendNUIMessage({
PayloadType = "SetSirenVolume",
volume = 1 - (StationDistances[1].Distance / Station.Radius)
})
-- If the cloest station is out of the radius of the fire siren
else
SendNUIMessage({
PayloadType = "SetSirenVolume",
volume = 0
})
local FireSirenCount = 0
for _, _ in pairs(FireSiren.EnabledStations) do FireSirenCount = FireSirenCount + 1 end

if FireSirenCount >= 1 then
local PlayerPed = PlayerPedId()
local PlayerCoords = GetEntityCoords(PlayerPed, false)

for _, Station in pairs(FireSiren.EnabledStations) do
local Distance = Vdist(PlayerCoords.x, PlayerCoords.y, PlayerCoords.z, Station.x, Station.y, Station.z) + 0.01 -- Stops divide by 0 errors
if (Distance <= Station.Radius) then

local SirenVolume = 1 - (Distance / Station.Radius)
if IsPedInAnyVehicle(PlayerPedId(), false) then
local VC = GetVehicleClass(GetVehiclePedIsIn(PlayerPedId()), false)
-- If vehicle is not a motobike or a bicycle
if VC ~= 8 or VC ~= 13 then
-- Lower the alarm volume by 45%
SirenVolume = SirenVolume * 0.45
end
end

TriggerEvent("Fire-EMS-Pager:Bounce:NUI", "SetSirenVolume", {Station.Name, SirenVolume})
else
TriggerEvent("Fire-EMS-Pager:Bounce:NUI", "SetSirenVolume", {Station.Name, 0})
end
end
end
Expand Down
26 changes: 18 additions & 8 deletions [inferno-collection]/inferno-fire-ems-pager/html/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
Inferno Collection Fire/EMS Pager + Fire Siren Version 4.52 Alpha
Inferno Collection Fire/EMS Pager + Fire Siren Version 4.53 Alpha
Copyright (c) 2019, Christopher M, Inferno Collection. All rights reserved.
Expand All @@ -22,7 +22,7 @@
<body>
<script type="text/javascript">
$(function() {
var siren = null;
var FireSirens = [];

// Wait to receive message from client script
window.addEventListener("message", function(event) {
Expand All @@ -33,14 +33,24 @@
pager.play();

} else if (event.data.PayloadType == "PlaySiren") {
siren = new Audio("./sounds/firesiren.mp3");
// Set volume to 0 assuming it will be updated in the next few frames
siren.volume = 0.0;
siren.play();
if (!FireSirens[event.data.Payload[0]]) {
FireSirens[event.data.Payload[0]] = new Audio("./sounds/firesiren.mp3");
// Set volume to 0 assuming it will be updated in the next few frames
FireSirens[event.data.Payload[0]].volume = 0.0;
FireSirens[event.data.Payload[0]].play();
FireSirens[event.data.Payload[0]].addEventListener("ended",function(){
// Send call back to remove siren
$.post("http://inferno-fire-ems-pager/RemoveSiren", JSON.stringify({
Name: event.data.Payload[0],
ID: event.data.Payload[1]
}));
FireSirens[event.data.Payload[0]] = null;
});
}

} else if (event.data.PayloadType == "SetSirenVolume") {
if (siren != null) {
siren.volume = event.data.volume;
if (FireSirens[event.data.Payload[0]]) {
FireSirens[event.data.Payload[0]].volume = event.data.Payload[1];
}
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Inferno Collection Fire/EMS Pager + Fire Siren Version 4.52 Alpha
# Inferno Collection Fire/EMS Pager + Fire Siren Version 4.53 Alpha
#
# Copyright (c) 2019, Christopher M, Inferno Collection. All rights reserved.
#
Expand Down
36 changes: 30 additions & 6 deletions [inferno-collection]/inferno-fire-ems-pager/server.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Inferno Collection Fire/EMS Pager + Fire Siren Version 4.52 Alpha
-- Inferno Collection Fire/EMS Pager + Fire Siren Version 4.53 Alpha
--
-- Copyright (c) 2019, Christopher M, Inferno Collection. All rights reserved.
--
Expand All @@ -8,22 +8,47 @@
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. THE SOFTWARE MAY NOT BE SOLD.
--

-- Play tones on all clients
--
-- Nothing past this point needs to be edited, all the settings for the resource are found ABOVE this line.
-- Do not make changes below this line unless you know what you are doing!
--

-- Master Fire Siren storage variable
local FireSirens = {}

RegisterServerEvent("Fire-EMS-Pager:StoreSiren")
AddEventHandler("Fire-EMS-Pager:StoreSiren", function(Station)
if not FireSirens[Station.Name:lower()] then
FireSirens[Station.Name:lower()] = Station
FireSirens[Station.Name:lower()].ID = source

TriggerClientEvent("Fire-EMS-Pager:Bounce:ServerValues", -1, FireSirens)
end
end)

RegisterServerEvent("Fire-EMS-Pager:RemoveSiren")
AddEventHandler("Fire-EMS-Pager:RemoveSiren", function(StationName)
if FireSirens[StationName] then
FireSirens[StationName] = nil
end
end)

-- Plays tones on all clients
RegisterServerEvent("Fire-EMS-Pager:PageTones")
AddEventHandler("Fire-EMS-Pager:PageTones", function(Tones, HasDetails, Details)
TriggerClientEvent("Fire-EMS-Pager:PlayTones", -1, Tones, HasDetails, Details)
end)

-- Play cancel sound on all clients
-- Plays cancel sound on all clients
RegisterServerEvent("Fire-EMS-Pager:CancelPage")
AddEventHandler("Fire-EMS-Pager:CancelPage", function(Tones, HasDetails, Details)
TriggerClientEvent("Fire-EMS-Pager:CancelPage", -1, Tones, HasDetails, Details)
end)

-- Play fire siren on all clients
RegisterServerEvent("Fire-EMS-Pager:SoundSirens")
AddEventHandler("Fire-EMS-Pager:SoundSirens", function(Stations)
TriggerClientEvent("Fire-EMS-Pager:PlaySirens", -1, Stations)
AddEventHandler("Fire-EMS-Pager:SoundSirens", function()
TriggerClientEvent("Fire-EMS-Pager:PlaySirens", -1)
end)

-- Whitelist check on server join
Expand Down Expand Up @@ -105,7 +130,6 @@ AddEventHandler("Fire-EMS-Pager:WhitelistCheck", function(Whitelist)
end
-- If using neither json, Ace, or disabled
else
-- Print error message to server console
print("===================================================================")
print("==============================WARNING==============================")
print("''" .. tostring(Whitelist.Enabled) .. "'' is not a valid Whitelist option.")
Expand Down

0 comments on commit 12e7b66

Please sign in to comment.