From 12d7116d7678309edd00df677685f848f0070638 Mon Sep 17 00:00:00 2001 From: AlfredoAnonym Date: Mon, 21 Apr 2025 14:42:46 +0200 Subject: [PATCH] Update c_main.lua --- .../[race]/[addons]/race_nos/c_main.lua | 207 ++++++++---------- 1 file changed, 94 insertions(+), 113 deletions(-) diff --git a/[gamemodes]/[race]/[addons]/race_nos/c_main.lua b/[gamemodes]/[race]/[addons]/race_nos/c_main.lua index 86864d8ca..f37e1c6ff 100644 --- a/[gamemodes]/[race]/[addons]/race_nos/c_main.lua +++ b/[gamemodes]/[race]/[addons]/race_nos/c_main.lua @@ -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