Skip to content

Update c_main.lua #620

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
207 changes: 94 additions & 113 deletions [gamemodes]/[race]/[addons]/race_nos/c_main.lua
Original file line number Diff line number Diff line change
@@ -1,130 +1,111 @@
g_tScreenSize = { guiGetScreenSize( ) };

local nos = 0;

local isEditingPosition = false;

g_bShowGauge = false;
local DEBUG = true
local rootElement = getRootElement()
local resName = getResourceName(getThisResource())

local c_DefaultPopupTimeout = 5000 --ms
local c_FadeDelta = .03 --alpha per frame
local c_MaxAlpha = .9

local function fadeIn(wnd)
local function raiseAlpha()
local newAlpha = guiGetAlpha(wnd) + c_FadeDelta
if newAlpha <= c_MaxAlpha then
guiSetAlpha(wnd, newAlpha)
else
removeEventHandler("onClientRender", rootElement, raiseAlpha)
end
end
addEventHandler("onClientRender", rootElement, raiseAlpha)
end

g_tGaugePosition = { g_tScreenSize[1] * 0.02, g_tScreenSize[2] * 0.6 };
local function fadeOut(wnd)
local function lowerAlpha()
local newAlpha = guiGetAlpha(wnd) - c_FadeDelta
if newAlpha >= 0 then
guiSetAlpha(wnd, newAlpha)
else
removeEventHandler("onClientRender", rootElement, lowerAlpha)
destroyElement(wnd)
end
end
addEventHandler("onClientRender", rootElement, lowerAlpha)
end

addEventHandler( "onClientResourceStart", resourceRoot,
function( )
label = guiCreateLabel( 0, 0, 200, 40, "Click anywhere on the screen to\nchange gauge position", false );
guiSetFont( label, "default-bold-small" );
guiSetVisible( label, false );
function outputGuiPopup(text, timeout)
local screenX, screenY = guiGetScreenSize()
local width = 500
local height = 20
local wndPopup = guiCreateWindow((screenX - width) / 2, screenY - height, width, height, '', false)

guiSetAlpha(wndPopup, 0)
guiSetFont(wndPopup, "clear-normal")
guiSetText(wndPopup, text)

guiWindowSetMovable(wndPopup, false)
guiWindowSetSizable(wndPopup, false)

fadeIn(wndPopup)
setTimer(fadeOut, timeout or c_DefaultPopupTimeout, 1, wndPopup)
end

loadGaugePositionFromFile( );

bindKey( "vehicle_fire", "both", toggleNOS );
bindKey( "vehicle_secondary_fire", "both", toggleNOS );
bindKey( "#", "up", letMePositionGauge );
end
)
---- FPS

FPSMax = 1
FPSAvg = 1
FPSCalc = 0
FPSTime = getTickCount() + 1000
AVGTbl = {}
val = 1

addEventHandler( "onClientElementDataChange", root,
function( key )
if getElementType( source ) == "vehicle" and key == "NOS" then
local veh = getPedOccupiedVehicle( localPlayer );
if veh == source then
nos = getElementData( source, key );
end
function CalcFps( )
if (getTickCount() < FPSTime) then
FPSCalc = FPSCalc + 1
else
if (FPSCalc > FPSMax) then
FPSMax = FPSCalc
end
end
)


function toggleNOS( key, state )
local veh = getPedOccupiedVehicle( localPlayer );
if veh and not isEditingPosition then
if state == "up" then
removeVehicleUpgrade( veh, 1010 );
setPedControlState( "vehicle_fire", false );
setElementData( veh, "NOS", nos );
else
clicktick = getTickCount( );
vehnos = getElementData( veh, "NOS" );
if vehnos then nos = vehnos end
if nos > 0 then
addVehicleUpgrade( veh, 1010 );
setPedControlState( "vehicle_fire", true );
setElementData( veh, "NOS", nos );
end
if val == 101 then val = 1 end
AVGTbl[val] = FPSCalc
FPSAvg = 0
for k,v in pairs(AVGTbl) do
FPSAvg = FPSAvg + v
end
FPSAvg = math.floor(FPSAvg / #AVGTbl)
FPSCalc = 0
FPSTime = getTickCount() + 1000
val = val + 1
end
end


addEvent( "onClientScreenFadedIn", true )
addEventHandler( "onClientScreenFadedIn", root,
function ( )
g_bShowGauge = true;
nos = 0;
end
)
function GetCurrentFps()
return FPSCalc
end

addEvent( "onClientScreenFadedOut", true )
addEventHandler( "onClientScreenFadedOut", root,
function ( )
g_bShowGauge = false;
end
)


local previous_hourcheck = -1;
local previous_color = 0;
addEventHandler( "onClientRender", root,
function( )
if g_bShowGauge then
local veh = getPedOccupiedVehicle( localPlayer );
if veh then
local am_i_driver = getVehicleOccupant( veh, 0 );
if am_i_driver then
local color;
local hour, minute = getTime( );
if previous_hourcheck ~= hour then
color = ( (hour > 19 )or ( hour >= 0 and hour < 6 ) ) and tocolor( 0, 255, 0 ) or tocolor( 255, 255, 255 );
previous_color = color;
else
color = previous_color;
end
local nosangle = (nos / 100) * 225 + 45;
dxDrawImage( g_tGaugePosition[ 1 ], g_tGaugePosition[ 2 ], 100, 100, "gauge/nos_gauge.png", 0, 0, 0, color );
dxDrawImage( g_tGaugePosition[ 1 ] + 45, g_tGaugePosition[ 2 ] + 41, 10, 40, "gauge/nos_arrow.png", nosangle, 0, -11, tocolor( 255, 0, 0 ) );

local nitro = getVehicleUpgradeOnSlot( veh, 8 );
if ( getPedControlState( "vehicle_fire" ) and type( nitro ) == "number" and nitro ~= 0 ) then
if nos > 0 then
nos = nos - .2;
if getTickCount() - clicktick > 20000 then
local nitroID = getVehicleUpgradeOnSlot( veh, 8 );
if nitroID then
removeVehicleUpgrade( veh, nitroID );
addVehicleUpgrade( veh, nitroID );
setPedControlState( "vehicle_fire", false );
setPedControlState( "vehicle_fire", true );
clicktick = getTickCount();
end
end
else
removeVehicleUpgrade( veh, 1010 );
setPedControlState( "vehicle_fire", false );
setElementData( veh, "NOS", 0 );
end
end
end
end
end
end
)

function GetAverageFps()
return FPSAvg
end


addEvent( "refillNOS", true )
addEventHandler( "refillNOS", root,
function( newvalue )
nos = newvalue;
end
)
--
-- DEBUG
--
function alert(message, channel)
if not DEBUG then return end

message = resName..": "..tostring(message)

if channel == "console" then
outputConsole(message)
return
end

if channel == "chat" then
outputChatBox(message)
return
end

outputDebugString(message)
end