Skip to content

Commit

Permalink
Add VEHICLE.Default parameter
Browse files Browse the repository at this point in the history
Setting `VEHICLE.Default = true` will apply the vehicle profile to all vehicles of the same class and model as the base vehicle defined with `VEHICLE.Vehicle`.

It will also prevent Photon 2 from creating a new (duplicate) vehicle entry for the profile.

This can be used to seamlessly apply Photon 2 vehicle lighting to normal vehicles without any special coding.

(Note that this is experimental and certain things like the HUD and controller beeps are still present.)
  • Loading branch information
ischmal committed Feb 19, 2024
1 parent 5756094 commit ec12ef6
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 45 deletions.
14 changes: 12 additions & 2 deletions .annotations/interfaces/ann_element_mesh.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,21 @@
---@field [4] string Mesh sub-material.

---@class PhotonElementMesh : PhotonElement, PhotonElementMeshProperties
---@field New? fun(light: PhotonElementMesh, template: string): PhotonElementMesh
---@field MeshSubIndex? number Model's mesh sub-index (default = `1`).
---@field Mesh? IMesh
---@field LocalPosition? Vector
---@field LocalAngles? Angle
---@field Matrix? VMatrix
---@field protected FinalScale? Vector The scale of the mesh multiplied by the scale of its spawned parent. Set automatically.
---@field DrawColor PhotonElementColor
---@field BloomColor PhotonElementColor
---@field FinalScale? Vector The scale of the mesh multiplied by the scale of its spawned parent. Set automatically.
---@field States? table<string, PhotonElementMeshState>
---@field Initialize fun(self: PhotonElementMesh, id: number, component: PhotonLightingComponent)
---@field Initialize fun(self: PhotonElementMesh, id: number, component: PhotonLightingComponent): PhotonElementMesh
---@field OnFileLoad fun()
---@field NewTemplate fun(data: PhotonElementMesh): PhotonElementMesh
---@field SetLightScale fun(self: PhotonElementMesh, scale: number)
---@field Activate fun(self: PhotonElementMesh)
---@field DeactivateNow fun(self: PhotonElementMesh)
---@field DoPreRender fun(self: PhotonElementMesh): PhotonElementMesh | nil
---@field OnStateChange fun(self: PhotonElementMesh, state: PhotonElementMeshState)
7 changes: 1 addition & 6 deletions .annotations/photon-v2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ PhotonSequenceCollection = PhotonSequenceCollection
---@type PhotonElementingSegment
PhotonElementingSegment = PhotonElementingSegment

---@type PhotonSequenceFrame
PhotonSequenceFrame = PhotonSequenceFrame

---@type PhotonLightingComponent
PhotonLightingComponent = PhotonLightingComponent

Expand Down Expand Up @@ -54,9 +51,6 @@ PhotonElementMesh = PhotonElementMesh
---@type PhotonElementMeshState
PhotonElementMeshState = PhotonElementMeshState

---@type PhotonComponent
PhotonComponent = PhotonComponent

---@type PhotonElementSound
PhotonElementSound = PhotonElementSound

Expand Down Expand Up @@ -176,6 +170,7 @@ PhotonMaterial = PhotonMaterial
---@field Equipment PhotonVehicleSelectionCategory[]
---@field Siren table<number, table<string, string> | string> Defines the siren(s) the vehicle should use by default. You may use a complete siren-set or select tones individually from other existing sets and re-map them. If you need more customization, you should create a new siren with `Photon2.RegisterSiren(...)` and use the name of it instead.
---@field Schema table<string, table<table>>
---@field Default? boolean If true, the profile will be used by default on the base vehicle and any other non-Photon 2 vehicles of the same class and model.
-- ---@field Equipment PhotonVehicleEquipment[]

---@class PhotonLibrarySiren
Expand Down
36 changes: 34 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"Lua.diagnostics.disable": [
"undefined-global",
"lowercase-global",
"need-check-nil"
"undefined-global",
],
"Lua.workspace.preloadFileSize": 8192,
"Lua.runtime.nonstandardSymbol": [
Expand All @@ -14,6 +13,39 @@
"continue",
"||"
],
"Lua.diagnostics.globals": [
"SERVER",
"CLIENT",
"IN_BACK",
"IN_FORWARD",
"IN_JUMP",
"MOUSE_LEFT",
"MOUSE_RIGHT",
"color_white",
"isstring",
"SOLID_VPHYSICS",
"MOVETYPE_VPHYSICS",
"SOLID_NONE",
"isbool",
"MOVETYPE_NONE",
"STENCIL_ALWAYS",
"STENCIL_REPLACE",
"STENCIL_KEEP",
"STENCIL_EQUAL",
"FILL",
"RIGHT",
"BOTTOM",
"TOP",
"LEFT",
"FORCE_BOOL",
"DButton",
"KEY_UP",
"KEY_DOWN",
"KEY_TAB",
"KEY_RSHIFT",
"KEY_RCONTROL",
"KEY_RALT"
],
"editor.insertSpaces": false,
"editor.detectIndentation": false,
}
2 changes: 1 addition & 1 deletion lua/autorun/sh_cami.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---@diagnostic disable: undefined-doc-name, missing-return-value, undefined-field
---@diagnostic disable: undefined-doc-name, missing-return-value, undefined-field, missing-return
--[[
CAMI - Common Admin Mod Interface.
Copyright 2020 CAMI Contributors
Expand Down
25 changes: 14 additions & 11 deletions lua/entities/photon_controller/shared.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---@class PhotonController : Entity
---@field ComponentParent Entity
---@field Components table<string, PhotonLightingComponent>
---@field ComponentArray PhotonBaseEntity[]
---@field ComponentArray PhotonLightingComponent[]
---@field CurrentProfile PhotonVehicle
---@field GetLinkedToVehicle fun(): boolean
---@field GetVehicleBraking fun(): boolean
Expand Down Expand Up @@ -540,8 +540,9 @@ function ENT:GetCurrentEquipment()
return result or self.Equipment
end

---@param id string
function ENT:SetupUIComponent( id )
local data = self.Equipment.UIComponents[id] --[[@as PhotonVehicleEquipment]]
local data = self.Equipment.UIComponents[id]
-- printf("Setting up UI component [%s]", id)
if ( not data ) then
error(string.format("Unable to locate equipment UI component ID [%s]", id))
Expand All @@ -556,7 +557,7 @@ end

---@param id string
function ENT:SetupVirtualComponent( id )
local data = self.Equipment.VirtualComponents[id] --[[@as PhotonVehicleEquipment]]
local data = self.Equipment.VirtualComponents[id]
-- printf("Setting up virtual component [%s]", id)
if (not data) then
error(string.format("Unable to locate equipment virtual component ID [%s]", id))
Expand Down Expand Up @@ -600,7 +601,7 @@ function ENT:SetupProp( id )
self.Props[id] = ent
end

---@param id string
---@param id string | any
function ENT:SetupBodyGroup( id )
if CLIENT then return end
local data = self.Equipment.BodyGroups[id]
Expand Down Expand Up @@ -648,10 +649,9 @@ function ENT:SetupInteractionSound( id )
self:SetInteractionSound( data.Class, data.Profile )
end

---@param id string
function ENT:SetupComponent( id )
self.AttemptingComponentSetup = true
local data = self.Equipment.Components[id] --[[@as PhotonVehicleEquipment]]
local data = self.Equipment.Components[id]
if (not data) then
print(string.format("Unable to locate equipment component ID [%s]", id))
return
Expand Down Expand Up @@ -1077,11 +1077,14 @@ function ENT:GetActiveComponents()
for _, componentIndex in pairs( map[selectionIndex].Components ) do
result[self.CurrentProfile.Equipment.Components[componentIndex].Component] = true
end
for _, componentIndex in pairs( map[selectionIndex].VirtualComponents ) do
result[self.CurrentProfile.Equipment.Components[componentIndex].Component] = true
end
for _, componentIndex in pairs( map[selectionIndex].UIComponents ) do
result[self.CurrentProfile.Equipment.Components[componentIndex].Component] = true
-- not checking for client here causes strange errors when reloading core files
if ( CLIENT ) then
for _, componentIndex in pairs( map[selectionIndex].VirtualComponents ) do
result[self.CurrentProfile.Equipment.Components[componentIndex].Component] = true
end
for _, componentIndex in pairs( map[selectionIndex].UIComponents ) do
result[self.CurrentProfile.Equipment.Components[componentIndex].Component] = true
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lua/photon-v2/cl_net.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function Photon2.cl_Network.SetControllerSelection( controller, table )
end

function Photon2.cl_Network.OnSetInputController( len )
Photon2.ClientInput.SetTargetController( net.ReadEntity() )
Photon2.ClientInput.SetTargetController( net.ReadEntity() --[[@as PhotonController]] )
end
net.Receive( "Photon2:SetPlayerInputControllerTarget", Photon2.cl_Network.OnSetInputController )

Expand Down
3 changes: 2 additions & 1 deletion lua/photon-v2/library/components/photon_fedsig_legend.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,13 @@ COMPONENT.Segments = {
}
},
Warning = {
FrameDuration = 1/44,
Frames = {
[1] = "4 6 8 10 12 13 15 17 19 21 23 25",
[2] = "14 16 18 20 22 24 26 3 5 7 9 11 ",
},
Sequences = {
["P22"] = { 1, 0, 2, 0 }
["P22"] = { 1, 1, 0, 2, 2, 0 }
}
},
Inner = {
Expand Down
23 changes: 23 additions & 0 deletions lua/photon-v2/library/vehicles/default_sgm_forexp13.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
if (Photon2.ReloadVehicleFile()) then return end
local VEHICLE = Photon2.LibraryVehicle()

VEHICLE.Title = "2013 Ford Explorer"
VEHICLE.Vehicle = "13fpiu_sgm"

VEHICLE.Default = true

VEHICLE.Equipment = {
{
Category = "Standard Lighting",
Options = {
{
Option = "Default",
VirtualComponents = {
{
Component = "photon_standard_sgmfpiu13",
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion lua/photon-v2/meta/base_entity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function ENT:SetupStaticBones()
data.Position = data.Position or data[1] or Vector()
data.Angles = data.Angles or data[2] or Angle()
data.Scale = data.Scale or data[3] or Vector( 1, 1, 1 )
if ( isnumber(data.Scale) ) then data.Scale = Vector( data.Scale, data.Scale, data.Scale ) end
if ( isnumber(data.Scale) ) then data.Scale = Vector( data.Scale --[[@as number]], data.Scale --[[@as number]], data.Scale --[[@as number]] ) end
data.Follow = data.Follow or data[4] or false

if ( not data.Follow ) then
Expand Down
10 changes: 5 additions & 5 deletions lua/photon-v2/meta/light_mesh.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ Light.States = {
},
["OFF"] = {
Intensity = 0,
BloomColor = PhotonColor( 0, 0, 0 ),
DrawColor = PhotonColor( 0, 0, 0 ),
BloomColor = PhotonColor( 0, 0, 0 ) --[[@as PhotonBlendColor]],
DrawColor = PhotonColor( 0, 0, 0 ) --[[@as PhotonBlendColor]],
},
["R"] = {
BloomColor = PhotonColor( 255, 0, 0 ):Blend( red ):GetBlendColor(),
Expand Down Expand Up @@ -134,7 +134,7 @@ function Light:Initialize( id, component )
self.Matrix = Matrix()
self.DrawColor = PhotonElementColor()
self.BloomColor = PhotonElementColor()
if ( isnumber( self.Scale ) ) then self.Scale = Vector( self.Scale, self.Scale, self.Scale ) end
if ( isnumber( self.Scale ) ) then self.Scale = Vector( self.Scale --[[@as number]], self.Scale--[[@as number]], self.Scale--[[@as number]] ) end

-- Fix for bizarre scaling bug in 64-bit game
self.Scale = self.Scale + Vector( 0.0000000001, 0.0000000001, 0.0000000001 )
Expand All @@ -148,7 +148,7 @@ function Light:Initialize( id, component )
end

if ( isstring( self.BoneParent ) ) then
self.BoneParent = parentEntity:LookupBone( self.BoneParent )
self.BoneParent = parentEntity:LookupBone( self.BoneParent --[[@as string]] )
end

if ( not self.Mesh and CLIENT ) then
Expand Down Expand Up @@ -219,7 +219,7 @@ function Light:DoPreRender()

else
self.Parent:SetupBones()
local matrix = self.Parent:GetBoneMatrix( self.BoneParent )
local matrix = self.Parent:GetBoneMatrix( self.BoneParent --[[@as number]] )
self.Position, self.Angles = LocalToWorld( self.LocalPosition, self.LocalAngles, matrix:GetTranslation(), matrix:GetAngles() )
end

Expand Down
1 change: 1 addition & 0 deletions lua/photon-v2/meta/lighting_component.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ local print = Photon2.Debug.Print
---@field AcceptControllerPulse boolean (Internal)
---@field AcceptControllerTiming boolean (Internal)
---@field Synchronized boolean If true, the component will synchronize with the controller's frame schedule (can be overridden by segments or sequences).
---@field VirtualOutputs table
local Component = exmeta.New()

local Builder = Photon2.ComponentBuilder
Expand Down
17 changes: 10 additions & 7 deletions lua/photon-v2/meta/vehicle.lua
Original file line number Diff line number Diff line change
Expand Up @@ -309,18 +309,21 @@ function Vehicle.New( data )
map[vehicle.EntityClass] = map[vehicle.EntityClass] or {}
map[vehicle.EntityClass][vehicle.Model] = map[vehicle.EntityClass][vehicle.Model] or {}
map[vehicle.EntityClass][vehicle.Model][vehicleListId] = data.Name
if ( data.Default ) then map[vehicle.EntityClass][vehicle.Model]["*"] = data.Name end


-- Populate entry in Vehicles table
profiles.Vehicles[vehicleListId] = data.Name

if ( not data.Default ) then
-- Generate table for Vehicles list table
local vehicleTable = Vehicle.CopyVehicle( data.Vehicle )
vehicleTable.Category = data.Category or target.Category
vehicleTable.Name = title
vehicleTable.IconOverride = "entities/" .. data.Name .. ".png"
vehicleTable.IsPhoton2Generated = true
list.Set( "Vehicles", vehicleListId, vehicleTable )

local vehicleTable = Vehicle.CopyVehicle( data.Vehicle )
vehicleTable.Category = data.Category or target.Category
vehicleTable.Name = title
vehicleTable.IconOverride = "entities/" .. data.Name .. ".png"
vehicleTable.IsPhoton2Generated = true
list.Set( "Vehicles", vehicleListId, vehicleTable )
end
setmetatable( vehicle, { __index = PhotonVehicle } )

-- SGM Model Attachment compatibility
Expand Down
1 change: 0 additions & 1 deletion lua/photon-v2/meta/vehicle_equipment.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ local printf = Photon2.Debug.PrintF
---@field Props table
---@field BodyGroups table
---@field SubMaterials table
---@field Hud
local Equipment = exmeta.New()

function Equipment.New( name, option )
Expand Down
20 changes: 13 additions & 7 deletions lua/photon-v2/sv_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ include("sv_functions.lua")

include("sh_input.lua")

print("sv_init.lua")

local profiles = Photon2.Index.Profiles
local print = Photon2.Debug.Print
local printf = Photon2.Debug.PrintF
Expand All @@ -27,7 +25,7 @@ Photon2.OnEntityCreated = function( ent )
timer.Simple(duration, function()
if not IsValid( ent ) then return end
if (
( ent:IsVehicle() ) or
-- ( ent:IsVehicle() ) or
( Photon2.Index.Profiles.Map[ent:GetClass()] and Photon2.Index.Profiles.Map[ent:GetClass()][ent:GetModel()] )
) then
Photon2.OnPostVehicleCreated ( ent )
Expand All @@ -40,15 +38,23 @@ hook.Add("OnEntityCreated", "Photon2:OnEntityCreated", Photon2.OnEntityCreated)
---@param ent Entity
Photon2.OnPostVehicleCreated = function ( ent )
if (not IsValid( ent )) then return end
if (not ent.VehicleName) then
local universal = Photon2.Index.Profiles.Map[ent:GetClass()][ent:GetModel()]["*"]
local vehicleName = ent.VehicleName
if ( ( not vehicleName ) and ( not universal ) ) then
-- Photon2.Debug.Print( "A vehicle was just spawned (" .. tostring( ent ) .. ") but no .VehicleName was set." )
-- print("Vehicle parent: " .. tostring( ent:GetParent() ))
return
end
local profile = profiles.Vehicles[ent.VehicleName]
if (profiles.Vehicles[ent.VehicleName]) then
-- Photon2.Debug.PrintF( "Matching vehicle profile found. [%s] = %s", ent.VehicleName, profile )

local profile

if ( vehicleName ) then profile = profiles.Vehicles[vehicleName] end

if ( profile ) then
-- Photon2.Debug.PrintF( "Matching vehicle profile found. [%s] = %s", ent.VehicleName, profile )
Photon2.AddControllerToVehicle( ent, profile )
elseif ( universal ) then
Photon2.AddControllerToVehicle( ent, universal )
end
end

Expand Down

0 comments on commit ec12ef6

Please sign in to comment.