Skip to content

Update s_main.lua #623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
243 changes: 223 additions & 20 deletions [gamemodes]/[race]/[addons]/race_nos/s_main.lua
Original file line number Diff line number Diff line change
@@ -1,35 +1,238 @@
function table.size( tab )
local size = 0;
for i,v in pairs( tab ) do
size = size + 1;

g_Root = getRootElement()
g_ResRoot = getResourceRootElement()

--
-- SETTINGS
--

g_SettingKeys = {
["nos_duration"] = {
clientSetting = "NosDuration",
dataType = tonumber,
defaultValue = 20
},
["nos_recharge_delay"] = {
clientSetting = "NosRechargeDelay",
dataType = tonumber,
defaultValue = 40
},
["nos_recharge_duration"] = {
clientSetting = "NosRechargeDuration",
dataType = tonumber,
defaultValue = 0
},
["nos_state_on_vehicle_spawn"] = {
clientSetting = "KeepNosOnPlayerWasted",
dataType = toboolean,
defaultValue = false
},
["nos_state_on_vehicle_change"] = {
clientSetting = "KeepNosOnVehicleChange",
dataType = toboolean,
defaultValue = false
}
}

g_Settings = {}

function refreshClientSettings(player)
player = player or g_Root
triggerClientEvent(player, "onClientUpdateNosSettings", player, g_Settings)
end

function cacheResourceSettings()
g_Settings = {}
for key,setting in pairs(g_SettingKeys) do
local clientSetting = setting.clientSetting or key
local value = get(key) or setting.defaultValue
g_Settings[clientSetting] = (setting.dataType or tostring)(value) or setting.defaultValue
end
end

addEvent("onRequestNosSettings", true)
addEventHandler("onRequestNosSettings", g_Root,
function()
cacheResourceSettings()
refreshClientSettings(source)
end
)


-- Called from the admin panel when a setting is changed there
addEvent ("onSettingChange")
addEventHandler("onSettingChange", g_ResRoot,
function(name, oldvalue, value, player)
cacheResourceSettings()
refreshClientSettings()
end
)


--
-- ADMIN COMMANDS
--

-- admin command to set NOS duration
function consoleSetNosDuration(playerSource, commandName, durationInSeconds)
if not durationInSeconds then
outputChatBox("Syntax: /nosduration <duration in seconds>", playerSource)
end
local duration = tonumber(durationInSeconds)
if duration <= 15 then
outputChatBox("A value greater that 15 seconds must be supplied.", playerSource)
return
end
set("*nos_duration", duration)
cacheResourceSettings()
refreshClientSettings()
outputChatBox("The duration of a NOS charge has been set to " .. duration .. " seconds.")
end
addCommandHandler("nosduration", consoleSetNosDuration, true)


-- admin command to set NOS recharge period
function consoleSetNosRechargePeriod(playerSource, commandName, durationInSeconds)
if not durationInSeconds then
outputChatBox("Syntax: /nosrecharge <period in seconds>", playerSource)
end
local duration = tonumber(durationInSeconds)
set("*nos_recharge_delay", duration)
cacheResourceSettings()
refreshClientSettings()
outputChatBox("The NOS recharge period has been set to " .. duration .. " seconds.")
end
addCommandHandler("nosrechargedelay", consoleSetNosRechargePeriod, true)


-- admin command to set NOS recharge period
function consoleSetNosRechargeDuration(playerSource, commandName, durationInSeconds)
if not durationInSeconds then
outputChatBox("Syntax: /nosrechargeduration <duration in seconds>", playerSource)
end
local duration = tonumber(durationInSeconds)
set("*nos_recharge_duration", duration)
cacheResourceSettings()
refreshClientSettings()
outputChatBox("The duration of a NOS recharge has been set to " .. duration .. " seconds.")
end
addCommandHandler("nosrecharge", consoleSetNosRechargePeriod, true)


-- admin command to set NOS state on vehicle spawn
function consoleSetNosStateOnVehicleSpawn(playerSource, commandName, state)
if state ~= "on" and state ~= "off" then
outputChatBox("Syntax: /nosonrespawn [on|off]", playerSource)
return
end
return size;
state = state == "on"
set("*nos_state_on_vehicle_spawn", state)
cacheResourceSettings()
refreshClientSettings()
end
addCommandHandler("nosonrespawn", consoleSetNosStateOnVehicleSpawn)


addEvent( "onGamemodeMapStart" )
addEventHandler( "onGamemodeMapStart", root,
function( resMap )
--g_MapRes = resMap;
--outputChatBox( "vehicles: " .. #getElementsByType( "vehicle", getResourceRootElement( resMap ) ) );
-- admin command to set NOS state on vehicle spawn
function consoleSetNosStateOnVehicleChange(playerSource, commandName, state)
if state ~= "on" and state ~= "off" then
outputChatBox("Syntax: /nosonchange [on|off]", playerSource)
return
end
state = state == "on"
set("*nos_state_on_vehicle_change", state)
cacheResourceSettings()
refreshClientSettings()
end
addCommandHandler("nosonchange", consoleSetNosStateOnVehicleSpawn)






--
-- RACE NOS
--

addEventHandler("onNotifyPlayerReady", g_Root,
function()
-- does the spawnpoint already have a nitro upgrade installed?
local vehicle = getPedOccupiedVehicle(source)

if not vehicle then return end
local nitro = getVehicleUpgradeOnSlot(vehicle, 8)
if type(nitro) == "number" and nitro ~= 0 then
playSoundFrontEnd(source, 46)
triggerClientEvent(source, "onClientPickupNos", source, 100)
end
end
)


addEvent( "onPlayerPickUpRacePickup" )
addEventHandler( "onPlayerPickUpRacePickup", root,
function( _, type, veh )
addEvent("onPlayerPickUpRacePickup")
addEventHandler("onPlayerPickUpRacePickup", g_Root,
function(_, type, vehicleId)
if type == "nitro" then
setVehicleNOS( getPedOccupiedVehicle( source ), 100 );
triggerClientEvent(source, "onClientPickupNos", source, 100)
elseif type == "vehiclechange" then
triggerClientEvent(source, "onClientVehicleChange", source, vehicleId)
end
end
)


function setVehicleNOS( vehicle, nos )
if getElementData( vehicle, "NOS" ) == 100 then
triggerClientEvent( getVehicleOccupant( vehicle ), "refillNOS", vehicle, 100 );
else
setElementData( vehicle, "NOS", 100 );
addEvent("onPlayerReachCheckpoint")
addEventHandler("onPlayerReachCheckpoint", g_Root,
function(checkpointNum, time)
triggerClientEvent(source, "onClientCheckpointReached", source, checkpointNum)
end
end
)


addEvent("onMapStarting")
addEventHandler("onMapStarting", g_Root,
function(mapInfo, mapOptions, gameOptions)
cacheResourceSettings()
-- allow map to override settings
for key,setting in pairs(g_SettingKeys) do
local clientSetting = setting.clientSetting or key
local value = get(string.format("#%s.%s", mapInfo.resname, key))
if value then
g_Settings[clientSetting] = (setting.dataType or tostring)(value) or setting.defaultValue
end
end
refreshClientSettings()
end
)

--
-- OTHER NOS
--

addEvent("onVehicleEnter")
addEventHandler("onVehicleEnter", g_Root,
function(player, seat, jacked)
if seat == 0 then
local vehicle = source
local savedAmount = getElementData(vehicle, "nos", false) or -1
if savedAmount == -1 then
-- may have upgrades
local nitro = getVehicleUpgradeOnSlot(vehicle, 8)
if type(nitro) == "number" and nitro ~= 0 then
savedAmount = 100
end
end

if savedAmount > 0 then
playSoundFrontEnd(source, 46)
triggerClientEvent(player, "onClientPickupNos", player, savedAmount)
end

end
end
)