Skip to content
Closed
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion lua/effects/glide_exhaust.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ function EFFECT:Init( data )
local scale = Clamp( data:GetScale(), 0.1, 5 )
local alpha = Clamp( data:GetColor(), 0, 255 )
local power = Clamp( data:GetMagnitude() / 1000, 0, 1 )
local count = math.min( math.floor( Glide.GetScreenSize( origin, 6 * scale ) / 2 ), 3 )
if count < 1 then return end

local emitter = ParticleEmitter( origin, false )
if not IsValid( emitter ) then return end

alpha = ( 1 - power ) * alpha
power = 0.5 + power * 1.5

for _ = 1, 3 do
for _ = 1, count do
local p = emitter:Add( EXHAUST_SMOKE_MAT .. RandomInt( 9 ), origin )
if p then
p:SetDieTime( 0.6 )
Expand Down
13 changes: 10 additions & 3 deletions lua/effects/glide_tire_roll.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ function EFFECT:Init( data )
local velocity = data:GetStart()
local matId = data:GetSurfaceProp()
local scale = Clamp( data:GetScale(), 0.1, 10 )
local fx = surfaceFX[matId]
local count = 5

if fx then
count = math.min( math.floor( Glide.GetScreenSize( origin, scale * math.max( fx.minSize * 1.1, fx.maxSize * 1.5 ) ) / 2 ), 5 )
if count < 1 then return end
end

local emitter = ParticleEmitter( origin, false )
if not IsValid( emitter ) then return end

if surfaceFX[matId] then
self:DoSurface( emitter, origin, velocity, scale, surfaceFX[matId] )
self:DoSurface( emitter, origin, velocity, scale, fx, count )
else
self:DoSmoke( emitter, origin, velocity, scale, data:GetEntity() )
end
Expand All @@ -34,11 +41,11 @@ local Config = Glide.Config
local debrisGravity = Vector( 0, 0, 0 )
local debrisVelocity = Vector( 0, 0, 0 )

function EFFECT:DoSurface( emitter, origin, velocity, scale, fx )
function EFFECT:DoSurface( emitter, origin, velocity, scale, fx, count )
local p
local lifetime = fx.lifetime * ( Config.reduceTireParticles and 0.4 or 1 )

for _ = 1, 5 do
for _ = 1, count do
p = emitter:Add( fx.mat, origin )

if p then
Expand Down
67 changes: 61 additions & 6 deletions lua/entities/base_glide/cl_lights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,53 @@ function ENT:CreateHeadlight( index, offset, angles, color, texture, fovScale )
light:Update()
end

local cvarMaxHeadlights = CreateClientConVar( "glide_max_projected_headlights", "8", true, false,
"How many of the nearest vehicles can cast projected headlights at once (0 = no limit)", 0, 128 )

local headlightVehicles = setmetatable( {}, { __mode = "k" } )

do
local candidates = {}
local nextBudgetUpdate = 0

local function SortByDistance( a, b )
return a.glideHeadlightRank < b.glideHeadlightRank
end

hook.Add( "Think", "Glide.HeadlightBudget", function()
local t = RealTime()
if t < nextBudgetUpdate then return end
nextBudgetUpdate = t + 0.25

local limit = cvarMaxHeadlights:GetInt()
local count = 0
local myVehicle = Glide.currentVehicle

for e in pairs( headlightVehicles ) do
if not IsValid( e ) or not e.rfMisc or not e.rfMisc.isActive or e:GetHeadlightState() < 1 then
headlightVehicles[e] = nil

elseif limit < 1 or e == myVehicle then
e.allowProjectedHeadlights = true

else
count = count + 1
candidates[count] = e
e.glideHeadlightRank = e.rfMisc.lastDistance * ( e.allowProjectedHeadlights == true and 0.8 or 1 )
end
end

if count > limit then
table.sort( candidates, SortByDistance )
end

for i = 1, count do
candidates[i].allowProjectedHeadlights = i <= limit
candidates[i] = nil
end
end )
end

function ENT:RemoveHeadlights()
if not self.activeHeadlights then return end

Expand Down Expand Up @@ -68,26 +115,30 @@ function ENT:UpdateLights( selfTbl )
local headlightState = selfTbl.GetHeadlightState( self )

if headlightState > 0 then
headlightVehicles[self] = true
local colorVec = selfTbl.GetHeadlightColor( self )
colorHeadlight.r = colorVec[1] * 255
colorHeadlight.g = colorVec[2] * 255
colorHeadlight.b = colorVec[3] * 255
end

local allowProjected = selfTbl.allowProjectedHeadlights ~= false

-- Handle projected lights
if selfTbl.headlightState ~= headlightState then
if selfTbl.headlightState ~= headlightState or selfTbl.hasProjectedHeadlights ~= allowProjected then
selfTbl.headlightState = headlightState
selfTbl.hasProjectedHeadlights = allowProjected
selfTbl.RemoveHeadlights( self )

if headlightState == 0 then return end

local enable

for index, v in ipairs( selfTbl.Headlights ) do
enable = true
enable = allowProjected

-- Check for optional bodygroup requirement
if v.ifBodygroupId then
if enable and v.ifBodygroupId then
enable = self:GetBodygroup( v.ifBodygroupId ) == ( v.ifSubModelId or 0 )
end

Expand All @@ -98,7 +149,7 @@ function ENT:UpdateLights( selfTbl )
end
end

if headlightState > 0 and selfTbl.shouldThinkNow then
if headlightState > 0 and selfTbl.shouldThinkNow and allowProjected then
local l, hasLight

for index, data in ipairs( selfTbl.Headlights ) do
Expand Down Expand Up @@ -136,8 +187,6 @@ function ENT:UpdateLights( selfTbl )
local pos, dir, ltype, enable

for _, l in ipairs( selfTbl.LightSprites ) do
pos = self:LocalToWorld( l.offset )
dir = self:LocalToWorld( l.dir ) - myPos
ltype = l.type
enable = lightState[ltype]

Expand Down Expand Up @@ -170,6 +219,12 @@ function ENT:UpdateLights( selfTbl )
enable = enable and self:GetBodygroup( l.ifBodygroupId ) == ( l.ifSubModelId or 0 )
end

if enable then
pos = self:LocalToWorld( l.offset )
dir = self:LocalToWorld( l.dir )
dir:Sub( myPos )
end

if enable and ltype == "headlight" then
DrawLightSprite( pos, dir, l.size or 30, l.color or colorHeadlight, l.spriteMaterial )

Expand Down
2 changes: 2 additions & 0 deletions lua/entities/base_glide/sv_lights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ local CurTime = CurTime

--- Update out model's bodygroups depending on which lights are on.
function ENT:UpdateLightBodygroups( selfTbl )
if not selfTbl.LightBodygroups[1] then return end

local headlightState = selfTbl.GetHeadlightState( self )
local allowLights = selfTbl.IsEngineOn( self ) or headlightState > 0

Expand Down
38 changes: 24 additions & 14 deletions lua/entities/base_glide/sv_water.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,36 @@ local GetDevMode = Glide.GetDevMode
local EntityPairs = Glide.EntityPairs

function ENT:WaterThink( selfTbl, dt )
-- Update buoyancy points
local underWaterPoints = 0
local waterState = selfTbl.GetWaterState( self )

for _, point in ipairs( selfTbl.buoyancyPoints ) do
point.isUnderWater = IsUnderWater( self:LocalToWorld( point.offset ) )
local phys = self:GetPhysicsObject()
local skipPoints = waterState == 0 and selfTbl.hasValidPhysics and IsValid( phys ) and phys:IsAsleep()

if point.isUnderWater then
underWaterPoints = underWaterPoints + 1
if not skipPoints then
-- Update buoyancy points
local underWaterPoints = 0

for _, point in ipairs( selfTbl.buoyancyPoints ) do
point.isUnderWater = IsUnderWater( self:LocalToWorld( point.offset ) )

if point.isUnderWater then
underWaterPoints = underWaterPoints + 1
end
end
end

local waterState = underWaterPoints < 1 and 0 or (
underWaterPoints > selfTbl.buoyancyPointsCount * 0.5 and 2 or 1
)
local newState = underWaterPoints < 1 and 0 or (
underWaterPoints > selfTbl.buoyancyPointsCount * 0.5 and 2 or 1
)

if self:WaterLevel() > 2 then
waterState = 3
end
if self:WaterLevel() > 2 then
newState = 3
end

selfTbl.SetWaterState( self, waterState )
if newState ~= waterState then
waterState = newState
selfTbl.SetWaterState( self, waterState )
end
end

-- If necessary, kick passengers when underwater
if selfTbl.FallWhileUnderWater and waterState > 2 then
Expand Down
14 changes: 12 additions & 2 deletions lua/entities/base_glide_car/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,11 @@ function ENT:OnPostThink( dt, selfTbl )

-- Update driver inputs
selfTbl.UpdateSteering( self, dt, selfTbl )
selfTbl.SetBrakeValue( self, Clamp( selfTbl.frontBrake + selfTbl.rearBrake + ( selfTbl.GetInputBool( self, 1, "handbrake" ) and 1 or 0 ), 0, 1 ) )
local brakeValue = Clamp( selfTbl.frontBrake + selfTbl.rearBrake + ( selfTbl.GetInputBool( self, 1, "handbrake" ) and 1 or 0 ), 0, 1 )

if selfTbl.GetBrakeValue( self ) ~= brakeValue then
selfTbl.SetBrakeValue( self, brakeValue )
end

local phys = self:GetPhysicsObject()

Expand Down Expand Up @@ -505,6 +509,10 @@ function ENT:UpdateSteering( dt, selfTbl )
steerCone = Clamp( steerCone, Abs( sideSlip ), 1 )
inputSteer = ExpDecay( selfTbl.inputSteer, inputSteer * steerCone, selfTbl.GetSteerConeChangeRate( self ), dt )

if Abs( inputSteer - selfTbl.inputSteer ) < 0.0001 and Abs( inputSteer ) < 0.0001 then
inputSteer = 0
end

selfTbl.inputSteer = inputSteer

-- Counter-steer when slipping, going fast and not using steer input
Expand All @@ -513,7 +521,9 @@ function ENT:UpdateSteering( dt, selfTbl )
counterSteer = Clamp( counterSteer, -1, 1 ) * selfTbl.GetCounterSteer( self )
inputSteer = Clamp( inputSteer + counterSteer, -1, 1 )

selfTbl.SetSteering( self, inputSteer )
if selfTbl.GetSteering( self ) ~= inputSteer then
selfTbl.SetSteering( self, inputSteer )
end
selfTbl.steerAngle[2] = -inputSteer * selfTbl.GetMaxSteerAngle( self )

-- Reduce front wheel sideways friction when trying to do a J-turn
Expand Down
27 changes: 27 additions & 0 deletions lua/entities/base_glide_car/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function ENT:SetupDataTables()

local order = 0
local uneditable = self.UneditableNWVars
local tuningKeys = {}

-- We add a bunch of floats here so, this utility function helps.
local function AddFloatVar( key, min, max, category )
Expand All @@ -57,6 +58,7 @@ function ENT:SetupDataTables()
} )

self:NetworkVar( "Float", key, editData )
tuningKeys[#tuningKeys + 1] = key
end

local function AddBoolVar( key, category )
Expand All @@ -66,6 +68,8 @@ function ENT:SetupDataTables()
KeyName = key,
Edit = { type = "Bool", order = order, category = category }
} )

tuningKeys[#tuningKeys + 1] = key
end

-- Steering parameters
Expand Down Expand Up @@ -104,6 +108,29 @@ function ENT:SetupDataTables()
AddFloatVar( "SideTractionMin", 100, 5000, "#glide.editvar.traction" )

if SERVER then
local cache = {}

local function Invalidate( _, name )
cache[name] = nil
end

for _, key in ipairs( tuningKeys ) do
local getter = self["Get" .. key]

self["Get" .. key] = function( ent )
local value = cache[key]

if value == nil then
value = getter( ent )
cache[key] = value
end

return value
end

self:NetworkVarNotify( key, Invalidate )
end

-- Callback used to change the wheel radius
self:NetworkVarNotify( "WheelRadius", self.OnWheelRadiusChange )

Expand Down
15 changes: 12 additions & 3 deletions lua/entities/base_glide_car/sv_engine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ do
rpm = Max( rpm, 0 )

self.flywheelVelocity = rpm * TAU / 60
self:SetEngineRPM( rpm )
if self:GetEngineRPM() ~= rpm then
self:SetEngineRPM( rpm )
end
end

function ENT:TransmissionToEngineRPM( gear, selfTbl )
Expand Down Expand Up @@ -442,8 +444,15 @@ function ENT:EngineThink( dt, selfTbl )
throttle = Approach( throttle, inputThrottle, dt * 4 )

self:EngineAccelerate( selfTbl.flywheelFriction + selfTbl.flywheelTorque * throttle, dt )
self:SetEngineThrottle( throttle )
self:SetIsRedlining( isRedlining and inputThrottle > 0 )

if self:GetEngineThrottle() ~= throttle then
self:SetEngineThrottle( throttle )
end

isRedlining = isRedlining and inputThrottle > 0
if self:GetIsRedlining() ~= isRedlining then
self:SetIsRedlining( isRedlining )
end
end
end

Expand Down
Loading
Loading