Skip to content
Open
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: 1 addition & 3 deletions lua/autorun/photon/cl_emv_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ local function DrawEMVLights()
if not should_render:GetBool() then return end

for k, v in pairs(EMVU:AllVehicles()) do
if IsValid(v) and v.IsEMV and v:IsEMV() then
if IsValid(v) then
if v.Photon_RenderEL then
v:Photon_RenderEL()
else
EMVU:MakeEMV(v, v:EMVName())
end
if v.Photon_RenderIllum then
v:Photon_RenderIllum()
Expand Down
8 changes: 7 additions & 1 deletion lua/autorun/photon/cl_emv_meta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ end)

local printedErrors = {}

hook.Add("Photon.SimpleNet.ValueChanged", "Photon.SetupEMV", function(name, old, new, ent)
if name == "VehicleIndex" then
EMVU:MakeEMV(ent, new)
end
end)

function EMVU:MakeEMV( emv, name )

if not emv or not emv:IsValid() or not emv:IsVehicle() then return false end
if not emv or not emv:IsValid() or not emv:IsVehicle() or not emv:IsEMV() then return false end

if name == "1" then return end

Expand Down
39 changes: 39 additions & 0 deletions lua/autorun/photon/emv/sh_meta.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--[[-- Emergency Lighting Vehicle Meta
@copyright Photon Team
@release development
@author Photon Team
@module VEHICLE
@alias ENT
--]]--

local ENT = FindMetaTable("Vehicle")

function ENT:EMVName()
return self:GetPhotonNet_VehicleIndex("")
end

function ENT:Photon_GetUnitNumber()
return self:GetPhotonNet_UnitNumber("")
end

function ENT:Photon_GetLiveryID()
return self:GetPhotonNet_LiveryID("")
end

function ENT:Photon_SelectionString()
return self:GetPhotonNet_SelectionString("")
end

function ENT:Photon_SelectionTable()
return string.Split(self:Photon_SelectionString(), ".")
end

function ENT:Photon_GetUtilStringTable()
return {
"",
self:EMVName(),
self:Photon_GetUnitNumber(),
self:Photon_GetLiveryID(),
self:Photon_SelectionString()
}
end
37 changes: 37 additions & 0 deletions lua/autorun/photon/emv/sv_meta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,40 @@ end
function ENT:ELS_HasAuxSiren()
return self:ELS_AuxSirenSet() ~= nil and self:ELS_AuxSirenSet() ~= 0
end

function ENT:Photon_SetLiveryId(val)
return self:SetPhotonNet_LiveryID(val)
end

function ENT:Photon_SetUnitNumber(val)
val = tostring(val):upper()
if #val > 3 then
val = string.sub(val, 1, 3)
end
if not val:match("%w") then
val = ""
end
if PHOTON_BANNED_UNIT_IDS[val:lower()] then
val = ""
end

return self:SetPhotonNet_UnitNumber(val)
end

function ENT:Photon_SetSelection(index, value)
if not istable(EMVU.Selections[self.Name][index]) then
return
end

local selectionTable = self:Photon_SelectionTable()
selectionTable[index] = value
self:SetPhotonNet_SelectionString(table.concat(selectionTable, "."))

local selectionData = EMVU.Selections[self.Name][index].Options[value]

if istable(selectionData.Bodygroups) then
for _, bgData in ipairs(selectionData.Bodygroups) do
self:SetBodygroup(bgData[1], bgData[2])
end
end
end
3 changes: 2 additions & 1 deletion lua/autorun/photon/sh_emv_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ include( "library/emv_auto.lua" )

Photon.include("emv/cl_meta.lua")
Photon.include("emv/sv_meta.lua")
Photon.include("emv/sh_meta.lua")

local emvVehicleTable = {}
local emvLastScan = 0
Expand All @@ -82,7 +83,7 @@ function EMVU:UpdateVehicles()
end

for _,ent in pairs( ents.GetAll() ) do
if IsValid( ent ) and ent.IsEMV and ent:IsEMV() then
if IsValid( ent ) and ent:IsVehicle() and ent.IsEMV and ent:IsEMV() then
emvVehicleTable[ #emvVehicleTable + 1 ] = ent
end
end
Expand Down
38 changes: 5 additions & 33 deletions lua/autorun/photon/sh_emv_meta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ local istable = istable

function ent:IsEMV()
if not IsValid( self ) then return false end
if not self:IsVehicle() then return false end
if not EMV_INDEX then return false end
local str = self:GetNW2String( "PhotonLE.EMV_INDEX" )
if string.StartWith( tostring(str), "ö" ) then return true end
if self:EMVName() ~= "" then return true end
return false
end

Expand All @@ -26,15 +26,6 @@ function ent:HasPhotonELS()
return true
end

function ent:EMVName()
if not IsValid( self ) then return "" end
if not EMV_INDEX then return "" end
if self:IsEMV() then
return string.Explode( "ö", self:GetNW2String( "PhotonLE.EMV_INDEX" ), false )[2]
end
return ""
end

function ent:Photon_GetSpeed()
if not IsValid( self ) then return 0 end
return self:GetVelocity():Length()
Expand All @@ -45,14 +36,6 @@ function ent:Photon_AdjustedSpeed()
return ( self:GetVelocity():Length() * ( 3600 / 63360 ) )
end

function ent:Photon_GetUnitNumber()
return string.Explode( "ö", self:GetNW2String( "PhotonLE.EMV_INDEX" ), false )[3] or ""
end

function ent:Photon_GetLiveryID()
return string.Explode( "ö", self:GetNW2String( "PhotonLE.EMV_INDEX" ), false )[4] or ""
end

function ent:Photon_GetAutoSkinIndex()
local materials = self:GetMaterials()
for i=1,#materials do
Expand All @@ -75,16 +58,9 @@ end
ent.LegacySetSkin = ent.LegacySetSkin or ent.SetSkin
function ent:SetSkin( index )
self:LegacySetSkin( index )
hook.Call( "Photon.EntityChangedSkin", GM, self, index )
end

function ent:Photon_SelectionString()
return string.Explode( "ö", self:GetNW2String( "PhotonLE.EMV_INDEX" ), false )[5]
end

function ent:Photon_SelectionTable()
local selectionString = string.Explode( "ö", self:GetNW2String( "PhotonLE.EMV_INDEX" ), false )[5]
return string.Explode( ".", selectionString, false )
if self:IsVehicle() and self:IsEMV() then
hook.Call( "Photon.EntityChangedSkin", GM, self, index )
end
end

function ent:Photon_SelectionOption( index )
Expand Down Expand Up @@ -184,10 +160,6 @@ function ent:Photon_ImportSelectionData( inputData )
return resultTable
end

function ent:Photon_GetUtilStringTable()
return string.Explode( "ö", self:GetNW2String( "PhotonLE.EMV_INDEX" ), false )
end

function ent:Photon_SelectionEnabled()
return istable( EMVU.Selections[ self:EMVName() ] )
end
Expand Down
115 changes: 103 additions & 12 deletions lua/autorun/photon/shared/sh_simplenet.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Easily optimise networking for setting local values on entities.
@author Photon Team
@module Photon.SNet
@alias NET
@todo Add Spawn-Sync for all Photon Variables.
--]]--

Photon = Photon or {}
Expand All @@ -16,6 +15,7 @@ local ENT = FindMetaTable("Entity")
if SERVER then
util.AddNetworkString("Photon_SimpleNet_Change")
util.AddNetworkString("Photon_SimpleNet_RequestSync")
util.AddNetworkString("Photon_SimpleNet_Resync")
end

NET.BOOL = 1
Expand Down Expand Up @@ -83,6 +83,31 @@ function NET:Map(name, netType, extra)
end

if SERVER then
--- Send the current value of a networked variable to a recipient.
-- @ent ent The entity the value belongs to.
-- @str name The name to send.
-- @param val Value to send.
-- @param[opt] to Player to send to. Broadcasts to everyone if omitted.
-- @internal
-- @state server
function NET:SendChange(ent, name, val, to)
local idx, netType, extra = unpack(self.RMap[name])
if not idx then
PhotonError(("Attempted to call SimpleNet:Change with an unregistered name: %s"):format(name))
return
end

net.Start("Photon_SimpleNet_Change")
net.WriteEntity(ent)
net.WriteUInt(idx, self.Bits)
self.WriteFunctions[netType](val, extra)
if to then
net.Send(to)
else
net.Broadcast()
end
end

--- Change a networked entity variable.
-- @ent ent The entity to change the value on.
-- @str name The name to change.
Expand All @@ -96,20 +121,59 @@ if SERVER then
local old = ent[varName]
if val ~= old then
ent[varName] = val
self:SendChange(ent, name, val)
end
end

local idx, netType, extra = unpack(self.RMap[name])
if not idx then
PhotonError(("Attempted to call SimpleNet:Change with an unregistered name: %s"):format(name))
return
--- Resend every currently-set networked variable on every vehicle to a
-- player, batched into a single net message.
-- Needed because values are only broadcast when they change, so a player
-- who joins after a value was set would otherwise never receive it.
-- @ply to Player to send the current state to.
-- @internal
-- @state server
function NET:Resync(to)
local groups = {}
for _, ent in ipairs(ents.GetAll()) do
if IsValid(ent) and ent:IsVehicle() then
local fields
for name in pairs(self.RMap) do
local val = ent[self.Normalise(name)]
if val ~= nil then
fields = fields or {}
local idx, netType, extra = unpack(self.RMap[name])
fields[#fields + 1] = {idx, netType, extra, val}
end
end
if fields then
groups[#groups + 1] = {ent, fields}
end
end
end

net.Start("Photon_SimpleNet_Change")
if #groups == 0 then return end

net.Start("Photon_SimpleNet_Resync")
net.WriteUInt(#groups, 16)
for _, group in ipairs(groups) do
local ent, fields = group[1], group[2]
net.WriteEntity(ent)
net.WriteUInt(idx, self.Bits)
self.WriteFunctions[netType](val, extra)
net.Broadcast()
net.WriteUInt(#fields, self.Bits)
for _, field in ipairs(fields) do
local idx, netType, extra, val = field[1], field[2], field[3], field[4]
net.WriteUInt(idx, self.Bits)
self.WriteFunctions[netType](val, extra)
end
end
net.Send(to)
end

hook.Add("PlayerInitialSpawn", "Photon.SimpleNet.Resync", function(ply)
timer.Simple(2, function()
if not IsValid(ply) then return end
NET:Resync(ply)
end)
end)
end

--- Get the latest cached version of a value on an entity.
Expand Down Expand Up @@ -145,11 +209,33 @@ if SERVER then
end

if CLIENT then
net.Receive("Photon_SimpleNet_Change", function(len, ply)
net.Receive("Photon_SimpleNet_Change", function()
local ent = net.ReadEntity()
local idx = net.ReadUInt(NET.Bits)
local name, netType, extra = unpack(NET.FMap[idx])
ent[NET.Normalise(name)] = NET.ReadFunctions[netType](extra)
local normalName = NET.Normalise(name)
local old = ent[normalName]
ent[normalName] = NET.ReadFunctions[netType](extra)
hook.Run("Photon.SimpleNet.ValueChanged", name, old, ent[normalName], ent)
end)

net.Receive("Photon_SimpleNet_Resync", function()
local entCount = net.ReadUInt(16)
for _ = 1, entCount do
local ent = net.ReadEntity()
local fieldCount = net.ReadUInt(NET.Bits)
for _ = 1, fieldCount do
local idx = net.ReadUInt(NET.Bits)
local name, netType, extra = unpack(NET.FMap[idx])
local val = NET.ReadFunctions[netType](extra)
if IsValid(ent) then
local normalName = NET.Normalise(name)
local old = ent[normalName]
ent[normalName] = val
hook.Run("Photon.SimpleNet.ValueChanged", name, old, val, ent)
end
end
end
end)

hook.Add("NotifyShouldTransmit", "EMVU.Net.NotifyShouldTransmit", function(ent, shouldTransmit)
Expand All @@ -161,7 +247,7 @@ if CLIENT then
end)
end

local UInt, Bool = NET.UINT, NET.BOOL
local UInt, Bool, Str = NET.UINT, NET.BOOL, NET.STR

NET:Map("CurrentSignal", UInt, 2)
NET:Map("Blinker", UInt, 2)
Expand All @@ -184,3 +270,8 @@ NET:Map("TrafficOption", UInt, 4)
NET:Map("IlluminationOn", Bool)
NET:Map("IlluminationOption", UInt, 4)
NET:Map("Preset", UInt, 10)

NET:Map("VehicleIndex", Str)
NET:Map("UnitNumber", Str)
NET:Map("LiveryID", Str)
NET:Map("SelectionString", Str)
Loading
Loading