/* Liberty Unleashed Speedo Script -------------------------------------------------- Author: Juppi Version: 0.2.0 */ const UNIT_KPH = 0; const UNIT_MPH = 1; /* * Global Vars */ // Colours for leds LEDCOL_OFF <- Colour( 40, 70, 50 ); LEDCOL_YELLOW <- Colour( 255, 165, 10 ); LEDCOL_RED <- Colour( 255, 0, 0 ); LEDCOL_BLUE <- Colour( 70, 70, 220 ); // Settings g_iPlayerUnit <- 0; // Storage g_pLocalPlayer <- FindLocalPlayer(); g_bInVehicle <- false; g_pLocalVehicle <- null; // Hud window components g_HudWindow <- GUIWindow(); // The window to replace main GTA hud elements g_SpeedBar <- GUIProgressBar(); // Speedo bar g_HealthBar <- GUIProgressBar(); // Speedo bar // Info window components g_InfoWindow <- GUIWindow(); // Info window g_SpeedLabel <- GUILabel(); // Current speed g_SpeedUnitLabel <- GUILabel(); // Speedo unit g_GearLabel <- GUILabel(); // Current gear // Leds g_ServiceLED <- GUILabel(); // Service led g_ServiceLabel <- GUILabel(); // 'ServiceDRIVE' g_HandbrakeLED <- GUILabel(); // A led to show the handbrake status g_HandbrakeLabel <- GUILabel(); // 'HANDBRAKE' g_LightLED <- GUILabel(); // Light led g_LightLabel <- GUILabel(); // 'LIGHTS' // Service led status g_iLastLEDUpdate <- 0; g_ServiceLEDState <- 0; g_ServiceLEDToggle <- false; /* * LU Events */ function onScriptLoad() { print( "Loaded Liberty Unleashed Speedo Script" ); print( "--------------------------------------" ); print( "Version 0.2.0, written by Juppi" ); print( "Type /speedo to switch between units" ); print( "Press 'L' to toggle vehicle lights on/off" ); BindKey( 'L', BINDTYPE_DOWN, "ToggleVehicleLights" ); // Initialize the bar window and set style if ( g_HudWindow.Initialize() ) { g_HudWindow.Pos = VectorScreen( ScreenWidth - 250, 100 ); g_HudWindow.Size = VectorScreen( 140, 33 ); g_HudWindow.Colour = Colour( 0, 0, 20 ); g_HudWindow.Alpha = 180; g_HudWindow.Transparent = true; g_HudWindow.Moveable = false; g_HudWindow.Titlebar = false; g_HudWindow.Visible = false; // Init and add progress bars g_SpeedBar.Pos = VectorScreen( 5, 5 ); g_SpeedBar.Size = ScreenSize( 130, 16 ); g_SpeedBar.Thickness = 4; g_SpeedBar.MaxValue = 0.96; g_SpeedBar.StartColour = Colour( 0, 0, 255 ); g_SpeedBar.EndColour = Colour( 150, 150, 255 ); g_HealthBar.Pos = VectorScreen( 5, 31 ); g_HealthBar.Size = ScreenSize( 130, 16 ); g_HealthBar.Thickness = 4; g_HealthBar.MaxValue = 750.0; g_HealthBar.StartColour = Colour( 200, 0, 0 ); g_HealthBar.EndColour = Colour( 50, 200, 50 ); g_HudWindow.AddProgressBar( g_SpeedBar ); g_HudWindow.AddProgressBar( g_HealthBar ); } // Initialize the info window if ( g_InfoWindow.Initialize() ) { g_InfoWindow.Pos = VectorScreen( ScreenWidth - 250, ScreenHeight - 190 ); g_InfoWindow.Size = VectorScreen( 180, 100 ); g_InfoWindow.Colour = Colour( 0, 0, 20 ); g_InfoWindow.Alpha = 180; g_InfoWindow.Transparent = true; g_InfoWindow.Moveable = false; g_InfoWindow.Titlebar = false; g_InfoWindow.Visible = false; } // Gear display if ( g_GearLabel.Initialize() ) { g_GearLabel.Pos = VectorScreen( 20, 22 ); g_GearLabel.FontName = "System"; g_GearLabel.FontSize = 38; g_GearLabel.TextColour = Colour( 160, 0, 0 ); g_GearLabel.Text = "1"; g_InfoWindow.AddLabel( g_GearLabel ); } // Current speed if ( g_SpeedLabel.Initialize() ) { g_SpeedLabel.Pos = VectorScreen( 75, 15 ); g_SpeedLabel.FontName = "System"; g_SpeedLabel.FontSize = 20; g_SpeedLabel.TextColour = Colour( 170, 0, 0 ); g_SpeedLabel.Text = "0"; g_InfoWindow.AddLabel( g_SpeedLabel ); } // Speedo unit display if ( g_SpeedUnitLabel.Initialize() ) { g_SpeedUnitLabel.Pos = VectorScreen( 128, 28 ); g_SpeedUnitLabel.FontName = "Fixedsys"; g_SpeedUnitLabel.FontSize = 9; g_SpeedUnitLabel.TextColour = Colour( 255, 255, 255 ); g_SpeedUnitLabel.Text = "KPH"; g_InfoWindow.AddLabel( g_SpeedUnitLabel ); } // 'SERVICE' led if ( g_ServiceLED.Initialize() ) { g_ServiceLED.Pos = VectorScreen( 75, 55 ); g_ServiceLED.FontName = "Wingdings"; g_ServiceLED.FontSize = 10; g_ServiceLED.TextColour = LEDCOL_OFF; g_ServiceLED.Text = "l"; g_InfoWindow.AddLabel( g_ServiceLED ); } // 'SERVICE' label if ( g_ServiceLabel.Initialize() ) { g_ServiceLabel.Pos = VectorScreen( 95, 55 ); g_ServiceLabel.FontName = "Arial"; g_ServiceLabel.FontSize = 7; g_ServiceLabel.TextColour = Colour( 180, 180, 180 ); g_ServiceLabel.Text = "SERVICE"; g_InfoWindow.AddLabel( g_ServiceLabel ); } // 'HANDBRAKE' led if ( g_HandbrakeLED.Initialize() ) { g_HandbrakeLED.Pos = VectorScreen( 75, 70 ); g_HandbrakeLED.FontName = "Wingdings"; g_HandbrakeLED.FontSize = 10; g_HandbrakeLED.TextColour = LEDCOL_OFF; g_HandbrakeLED.Text = "l"; g_InfoWindow.AddLabel( g_HandbrakeLED ); } // 'HANDBRAKE' label if ( g_HandbrakeLabel.Initialize() ) { g_HandbrakeLabel.Pos = VectorScreen( 95, 70 ); g_HandbrakeLabel.FontName = "Arial"; g_HandbrakeLabel.FontSize = 7; g_HandbrakeLabel.TextColour = Colour( 180, 180, 180 ); g_HandbrakeLabel.Text = "HANDBRAKE"; g_InfoWindow.AddLabel( g_HandbrakeLabel ); } // 'LIGHTS' led if ( g_LightLED.Initialize() ) { g_LightLED.Pos = VectorScreen( 75, 85 ); g_LightLED.FontName = "Wingdings"; g_LightLED.FontSize = 10; g_LightLED.TextColour = LEDCOL_OFF; g_LightLED.Text = "l"; g_InfoWindow.AddLabel( g_LightLED ); } // 'LIGHTS' label if ( g_LightLabel.Initialize() ) { g_LightLabel.Pos = VectorScreen( 95, 85 ); g_LightLabel.FontName = "Arial"; g_LightLabel.FontSize = 7; g_LightLabel.TextColour = Colour( 180, 180, 180 ); g_LightLabel.Text = "LIGHTS"; g_InfoWindow.AddLabel( g_LightLabel ); } AddWindowLayer( g_HudWindow ); SendWindowToBack( g_HudWindow ); AddWindowLayer( g_InfoWindow ); SendWindowToBack( g_InfoWindow ); return 1; } function onClientEnteredVehicle( veh, door ) { // Disable GTA HUD stuff except for radar SetHUDItemEnabled( HUD_ARMOUR, false ); SetHUDItemEnabled( HUD_CLOCK, false ); SetHUDItemEnabled( HUD_HEALTH, false ); SetHUDItemEnabled( HUD_MONEY, false ); SetHUDItemEnabled( HUD_WANTED, false ); SetHUDItemEnabled( HUD_WEAPON, false ); // ...and show our own stuff g_HudWindow.Visible = true; if ( IsVehicleCar( veh.Model ) ) g_InfoWindow.Visible = true; // Only show the info window if we're driving a car // Store some info for our rendering hook g_bInVehicle = true; g_pLocalVehicle = veh; g_LightLED.TextColour = LEDCOL_OFF; return 1; } function onClientExitedVehicle( veh ) { // Re-enable GTA HUD stuff SetHUDItemEnabled( HUD_ARMOUR, true ); SetHUDItemEnabled( HUD_CLOCK, true ); SetHUDItemEnabled( HUD_HEALTH, true ); SetHUDItemEnabled( HUD_MONEY, true ); SetHUDItemEnabled( HUD_WANTED, true ); SetHUDItemEnabled( HUD_WEAPON, true ); // ...and hide our own stuff g_HudWindow.Visible = false; g_InfoWindow.Visible = false; // Reset data g_bInVehicle = false; g_pLocalVehicle = null; g_LightLED.TextColour = LEDCOL_OFF; return 1; } function onClientRender() { if ( g_bInVehicle ) { local pVehicle = ::g_pLocalVehicle; local fSpeed = GetSpeedFromVelocity( pVehicle.Velocity ); local fHealth = pVehicle.Health - 250; local fCarHealth = fHealth < 0 ? 0.0 : fHealth; local szSpeed = ( ::g_iPlayerUnit == UNIT_MPH ) ? ( 143 * fSpeed ).tointeger().tostring() : ( 230 * fSpeed ).tointeger().tostring(); ::g_SpeedBar.Value = fSpeed; ::g_SpeedLabel.Text = szSpeed; ::g_HealthBar.Value = fCarHealth; ::g_GearLabel.Text = pVehicle.Gear ? pVehicle.Gear.tostring() : "R"; // LED processing if ( fHealth < 200.0 ) { local iTimestamp = GetTickCount(); if ( iTimestamp - g_iLastLEDUpdate > 150 ) { if ( ::g_ServiceLEDToggle ) ::g_ServiceLED.TextColour = ::LEDCOL_OFF; else ::g_ServiceLED.TextColour = ::LEDCOL_RED; ::g_ServiceLEDToggle = !g_ServiceLEDToggle; ::g_ServiceLEDState = 1; ::g_iLastLEDUpdate = iTimestamp; } } else if ( fHealth < 350.0 ) { ::g_ServiceLED.TextColour = ::LEDCOL_RED; ::g_ServiceLEDState = 1; } else if ( fHealth < 500.0 ) { ::g_ServiceLED.TextColour = ::LEDCOL_YELLOW; ::g_ServiceLEDState = 1; } else if ( ::g_ServiceLEDState != 0 ) { ::g_ServiceLED.TextColour = ::LEDCOL_OFF; ::g_ServiceLEDState = 0; } } return 1; } function onClientKeyStateChange( oldkeys, newkeys ) { if ( g_bInVehicle ) { if ( ( ( newkeys & KEY_INCAR_HANDBRAKE ) == KEY_INCAR_HANDBRAKE ) && ( ( oldkeys & KEY_INCAR_HANDBRAKE ) != KEY_INCAR_HANDBRAKE ) ) { // Handbrake used. Light the led! g_HandbrakeLED.TextColour = LEDCOL_YELLOW; } else if ( ( ( newkeys & KEY_INCAR_HANDBRAKE ) != KEY_INCAR_HANDBRAKE ) && ( ( oldkeys & KEY_INCAR_HANDBRAKE ) == KEY_INCAR_HANDBRAKE ) ) { // Handbrake released. Kill the led! g_HandbrakeLED.TextColour = LEDCOL_OFF; } } return 1; } function onClientCommand( cmd, text ) { if ( cmd == "speedo" ) { local szUnit = text.tolower(); if ( szUnit == "kph" ) { g_iPlayerUnit = UNIT_KPH; g_SpeedUnitLabel.Text = "KPH"; } else if ( szUnit == "mph" ) { g_iPlayerUnit = UNIT_MPH; g_SpeedUnitLabel.Text = "MPH"; } } else if ( cmd == "do2" ) { try{ compilestring( text )(); }catch(e)Message(e); } else if ( cmd == "info" ) NewTimer( "Info", 250, 0 ); return 1; } /* * Misc Functions */ function IsVehicleCar( model ) { switch ( model ) { case VEH_PREDATOR: case VEH_TRAIN: case VEH_CHOPPER: case VEH_DODO: case VEH_RCBANDIT: case VEH_AIRTRAIN: case VEH_DEADDODO: case VEH_SPEEDER: case VEH_REEFER: return false; } return true; } function GetSpeedFromVelocity( v ) { return sqrt( v.x*v.x + v.y*v.y + v.z*v.z ); } function ToggleVehicleLights() { if ( g_bInVehicle ) { local bLights = g_pLocalVehicle.LightState == LIGHTSTATE_ON ? false : true; CallServerFunc( /*"speedo_server.nut"*/null, "ToggleVehicleLights", g_pLocalVehicle, bLights ); if ( bLights ) ::g_LightLED.TextColour = ::LEDCOL_BLUE; else ::g_LightLED.TextColour = ::LEDCOL_OFF; } }