Skip to content

Commit

Permalink
New models, broke up functions and other fixes
Browse files Browse the repository at this point in the history
Added new turret drive models courtesy of ghosteh (horizontal ring and vertical trunnion)
These new models are untextured, using debugwhite, as they were made with being tucked out of view in mind
Also the start of a new more organized file structure for models specifically, found started as >acf/
Currently only the new turret models are found in this

Broke up the turret slewing functions to further differentiate horizontal/vertical drives, now the vertical drive actually pitches instead of yaws so you are no longer required to rotate it on spawn for it to be correct
Updated hover info to reflect that (looking at the entity to reveal directions)
Damage fixes for motors/gyro
Mass rebalance for turret drives

Turret drives now also hide for the player, and instead redraw as the matching clientside model that better represents the current angle, very noticeable with the new trunnion model
  • Loading branch information
LiddulBOFH committed Jan 30, 2024
1 parent dbc7f74 commit 039bb2e
Show file tree
Hide file tree
Showing 18 changed files with 310 additions and 182 deletions.
108 changes: 82 additions & 26 deletions lua/acf/entities/turrets/turrets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ local InchToMm = ACF.InchToMm
-- Bunched all of the definitions together due to some loading issue

do -- Turret drives
local function ClampAngle(A,Amin,Amax)
local p,y,r

if A.p < Amin.p then p = Amin.p elseif A.p > Amax.p then p = Amax.p else p = A.p end
if A.y < Amin.y then y = Amin.y elseif A.y > Amax.y then y = Amax.y else y = A.y end
if A.r < Amin.r then r = Amin.r elseif A.r > Amax.r then r = Amax.r else r = A.r end

return Angle(p,y,r)
end

Turrets.Register("1-Turret",{
Name = "Turrets",
Description = "The turret drives themselves.\nThese have a fallback handcrank that is used automatically if no motor is available.",
Expand All @@ -25,7 +35,7 @@ do -- Turret drives
Text = "Maximum number of ACF turrets a player can create."
},
GetMass = function(Data, Size)
return math.Round(math.max(Data.Mass * (Size / Data.Size.Base),5), 1)
return math.Round(math.max(Data.Mass * (Size / Data.Size.Base),5) ^ 1.5, 1)
end,
GetTeethCount = function(Data, Size)
local SizePerc = (Size - Data.Size.Min) / (Data.Size.Max - Data.Size.Min)
Expand Down Expand Up @@ -126,74 +136,120 @@ do -- Turret drives
Turrets.RegisterItem("Turret-H","1-Turret",{
Name = "Horizontal Turret",
Description = "The large stable base of a turret.",
Model = "models/props_phx/construct/metal_plate_curve360.mdl",
Model = "models/acf/core/t_ring.mdl",
ModelSmall = "models/holograms/hq_cylinder.mdl", -- To be used for diameters < 12u, for RWS or other small turrets
Mass = 200, -- At default size, this is the mass of the turret ring. Will scale up/down with diameter difference
Mass = 25, -- At default size, this is the mass of the turret ring. Will scale up/down with diameter difference

Size = {
Base = 60, -- The default size for the menu
Min = 2, -- To accomodate itty bitty RWS turrets
Max = 512, -- To accomodate ship turrets
Ratio = 0.05 -- Height modifier for total size
Ratio = 0.1 -- Height modifier for total size
},

Teeth = { -- Used to give a final teeth count with size
Min = 12,
Max = 3072
},

MassLimit = {
Min = 50, -- Max amount of mass this component can support at minimum size
Max = 80000 -- Max amount of mass th is component can support at maximum size
},

Armor = {
Min = 5,
Min = 15,
Max = 175
},

SetupInputs = function(_,List)
local Count = #List

List[Count + 1] = "Bearing (Local degrees from home angle)"
end
end,

SlewFuncs = {
GetStab = function(Turret)
local AngDiff = Turret.Rotator:WorldToLocalAngles(Turret.LastRotatorAngle)

return (Turret.Stabilized and Turret.Active) and (AngDiff.yaw * Turret.StabilizeAmount) or 0
end,

GetTargetBearing = function(Turret,StabAmt)
local Rotator = Turret.Rotator

if Turret.HasArc then
if Turret.Manual then
return Rotator:WorldToLocalAngles(Turret:LocalToWorldAngles(Angle(0, math.Clamp(-Turret.DesiredDeg,Turret.MinDeg,Turret.MaxDeg), 0))).yaw
else
local LocalDesiredAngle = ClampAngle(Turret:WorldToLocalAngles(Turret.DesiredAngle) - Angle(0,StabAmt,0),Angle(0,-Turret.MaxDeg,0),Angle(0,-Turret.MinDeg,0))

return Rotator:WorldToLocalAngles(Turret:LocalToWorldAngles(LocalDesiredAngle)).yaw
end
else
return Turret.Manual and (Rotator:WorldToLocalAngles(Turret:LocalToWorldAngles(Angle(0, -Turret.DesiredDeg, 0))).yaw) or (Rotator:WorldToLocalAngles(Turret.DesiredAngle).yaw - StabAmt)
end
end,

SetRotatorAngle = function(Turret)
Turret.Rotator:SetAngles(Turret:LocalToWorldAngles(Angle(0, Turret.CurrentAngle, 0)))
end
}
})
end

do -- Vertical turret component
Turrets.RegisterItem("Turret-V","1-Turret",{
Name = "Vertical Turret",
Description = "The smaller part of a turret, usually has the weapon directly attached to it.\nCan be naturally stabilized up to 25% if there is no motor attached, but the mass must be balanced.",
Model = "models/holograms/hq_cylinder.mdl",
Mass = 100, -- At default size, this is the mass of the turret ring. Will scale up/down with diameter difference
Model = "models/acf/core/t_trun.mdl",
Mass = 25, -- At default size, this is the mass of the turret ring. Will scale up/down with diameter difference

Size = {
Base = 12, -- The default size for the menu
Min = 1, -- To accomodate itty bitty RWS turrets
Max = 36, -- To accomodate ship turrets
Ratio = 1.5 -- Height modifier for total size
Min = 4, -- To accomodate itty bitty RWS turrets
Max = 48, -- To accomodate ship turrets
Ratio = 1 -- Height modifier for total size
},

Teeth = { -- Used to give a final teeth count with size
Min = 8,
Max = 288
},

MassLimit = {
Min = 20, -- Max amount of mass this component can support at minimum size
Max = 40000 -- Max amount of mass th is component can support at maximum size
Max = 384
},

Armor = {
Min = 5,
Max = 175
Max = 305
},

SetupInputs = function(_,List)
local Count = #List

List[Count + 1] = "Elevation (Local degrees from home angle)"
end
end,

SlewFuncs = {
GetStab = function(Turret)
local AngDiff = Turret.Rotator:WorldToLocalAngles(Turret.LastRotatorAngle)

return (Turret.Stabilized and Turret.Active) and (AngDiff.pitch * Turret.StabilizeAmount) or 0
end,

GetTargetBearing = function(Turret,StabAmt)
local Rotator = Turret.Rotator

if Turret.HasArc then
if Turret.Manual then
return Rotator:WorldToLocalAngles(Turret:LocalToWorldAngles(Angle(math.Clamp(-Turret.DesiredDeg,Turret.MinDeg,Turret.MaxDeg), 0, 0))).pitch
else
local LocalDesiredAngle = ClampAngle(Turret:WorldToLocalAngles(Turret.DesiredAngle) - Angle(StabAmt,0,0),Angle(-Turret.MaxDeg,0,0),Angle(-Turret.MinDeg,0,0))

return Rotator:WorldToLocalAngles(Turret:LocalToWorldAngles(LocalDesiredAngle)).pitch
end
else
return Turret.Manual and (Rotator:WorldToLocalAngles(Turret:LocalToWorldAngles(Angle(-Turret.DesiredDeg, 0, 0))).pitch) or (Rotator:WorldToLocalAngles(Turret.DesiredAngle).pitch - StabAmt)
end
end,

SetRotatorAngle = function(Turret)
Turret.Rotator:SetAngles(Turret:LocalToWorldAngles(Angle(Turret.CurrentAngle, 0, 0)))
end
}
})
end
end
Expand Down Expand Up @@ -245,7 +301,7 @@ do -- Turret motors
Base = 12,

Min = 8,
Max = 24,
Max = 32,
},

Torque = {
Expand Down Expand Up @@ -276,7 +332,7 @@ do -- Turret motors
Base = 12,

Min = 8,
Max = 24,
Max = 32,
},

Torque = {
Expand Down
91 changes: 87 additions & 4 deletions lua/entities/acf_turret/cl_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ do -- NET SURFER
Entity.MinDeg = Data.MinDeg
Entity.MaxDeg = Data.MaxDeg
Entity.CoMDist = Data.CoMDist
Entity.Type = Data.Type

Entity.HasData = true
Entity.Age = Clock.CurTime + 5
Expand All @@ -42,6 +43,66 @@ do -- NET SURFER
end
end

do -- Turret drive drawing
local DrawDist = 1024 ^ 2
local HideInfo = ACF.HideInfoBubble

local function CSModel(Ent)
if not IsValid(Ent) then return end

if IsValid(Ent.CSModel) then
if Ent.CSModel:GetModel() ~= Ent:GetModel() then Ent.CSModel:Remove() return end

return Ent.CSModel
end

if not Ent.Matrix then return end

local CSModel = ClientsideModel(Ent:GetModel())
CSModel:SetParent(Ent)
CSModel:SetPos(Ent:GetPos())
CSModel:SetAngles(Ent:GetAngles())
CSModel:SetMaterial(Ent:GetMaterial())
CSModel:SetColor(Ent:GetColor())

CSModel.Material = Ent:GetMaterial()
CSModel.Matrix = Ent.Matrix
CSModel:EnableMatrix("RenderMultiply", CSModel.Matrix)

Ent.CSModel = CSModel

return Ent.CSModel
end

function ENT:Draw()

-- Partial from base_wire_entity, need the tooltip but without the model drawing since we're drawing our own
local looked_at = self:BeingLookedAtByLocalPlayer()

if looked_at then
self:DrawEntityOutline()
if not HideInfo() then self:AddWorldTip() end
end

local Rotator = self:GetNWEntity("ACF.Rotator")

if (not IsValid(Rotator)) or ((EyePos()):DistToSqr(self:GetPos()) > DrawDist) then self:DrawModel() return end

local CSM = CSModel(self)
if not IsValid(CSM) then self:DrawModel() return end

if CSM.Material ~= self:GetMaterial() then CSM:Remove() return end
if CSM:GetColor() ~= self:GetColor() then CSM:Remove() return end
if CSM.Matrix ~= self.Matrix then CSM:Remove() return end

CSM:SetAngles(Rotator:GetAngles())
end

function ENT:OnRemove()
if IsValid(self.CSModel) then self.CSModel:Remove() end
end
end

do -- Overlay
local red = Color(255,0,0)
local green = Color(0,255,0)
Expand All @@ -61,13 +122,28 @@ do -- Overlay
local X = self:OBBMaxs().x
local Pos = self:LocalToWorld(self:OBBCenter())

if self.Type == "Turret-V" then
UX = self:GetRight() * 0.5
end

render.DrawLine(Pos + UX,Pos + (self:GetForward() * X) + UX,orange,true)

if IsValid(self.Rotator) then render.DrawLine(Pos,Pos + self.Rotator:GetForward() * X,green,true) end

local LocPos = self:WorldToLocal(Trace.HitPos)
local AimAng = 0
local CurAng = 0
local LocDir = Vector(LocPos.x,LocPos.y,0):GetNormalized()

if self.Type == "Turret-V" then
LocDir = Vector(LocPos.x,0,LocPos.z):GetNormalized()
AimAng = -math.Round(self:WorldToLocalAngles(self:LocalToWorldAngles(LocDir:Angle())).pitch,2)
CurAng = -math.Round(self:WorldToLocalAngles(self.Rotator:GetAngles()).pitch,2)
else
AimAng = -math.Round(self:WorldToLocalAngles(self:LocalToWorldAngles(LocDir:Angle())).yaw,2)
CurAng = -math.Round(self:WorldToLocalAngles(self.Rotator:GetAngles()).yaw,2)
end

render.DrawLine(Pos - UX,self:LocalToWorld(self:OBBCenter() + LocDir * X * 2) - UX,magenta,true)

render.DrawLine(self:LocalToWorld(self:OBBCenter()),self.Rotator:LocalToWorld(self.LocalCoM),red,true)
Expand All @@ -78,9 +154,16 @@ do -- Overlay

if not ((self.MinDeg == -180) and (self.MaxDeg == 180)) then
local MinDir = Vector(X,0,0)
MinDir:Rotate(Angle(0,-self.MinDeg,0))
local MaxDir = Vector(X,0,0)
MaxDir:Rotate(Angle(0,-self.MaxDeg,0))

if self.Type == "Turret-V" then
MinDir:Rotate(Angle(-self.MinDeg,0,0))
MaxDir:Rotate(Angle(-self.MaxDeg,0,0))
else
MinDir:Rotate(Angle(0,-self.MinDeg,0))
MaxDir:Rotate(Angle(0,-self.MaxDeg,0))
end

render.DrawLine(Pos - UX,self:LocalToWorld(self:OBBCenter() + MinDir) - UX,red,true)
render.DrawLine(Pos - UX,self:LocalToWorld(self:OBBCenter() + MaxDir) - UX,green,true)
end
Expand All @@ -93,8 +176,8 @@ do -- Overlay

cam.Start2D()
draw.SimpleTextOutlined("Zero","DermaDefault",HomePos.x,HomePos.y,orange,TEXT_ALIGN_CENTER,TEXT_ALIGN_CENTER,1,color_black)
draw.SimpleTextOutlined("Current: " .. -math.Round(self:WorldToLocalAngles(self.Rotator:GetAngles()).yaw,2),"DermaDefault",CurPos.x,CurPos.y,green,TEXT_ALIGN_CENTER,TEXT_ALIGN_CENTER,1,color_black)
draw.SimpleTextOutlined("Aim: " .. -math.Round(self:WorldToLocalAngles(self:LocalToWorldAngles(LocDir:Angle())).yaw,2),"DermaDefault",AimPos.x,AimPos.y,magenta,TEXT_ALIGN_CENTER,TEXT_ALIGN_CENTER,1,color_black)
draw.SimpleTextOutlined("Current: " .. CurAng,"DermaDefault",CurPos.x,CurPos.y,green,TEXT_ALIGN_CENTER,TEXT_ALIGN_CENTER,1,color_black)
draw.SimpleTextOutlined("Aim: " .. AimAng,"DermaDefault",AimPos.x,AimPos.y,magenta,TEXT_ALIGN_CENTER,TEXT_ALIGN_CENTER,1,color_black)

draw.SimpleTextOutlined("Mass: " .. self.Mass .. "kg","DermaDefault",CoMPos.x,CoMPos.y,color_white,TEXT_ALIGN_CENTER,TEXT_ALIGN_CENTER,1,color_black)
draw.SimpleTextOutlined("Lateral Distance: " .. self.CoMDist .. "u","DermaDefault",CoMPos.x,CoMPos.y + 16,color_white,TEXT_ALIGN_CENTER,TEXT_ALIGN_CENTER,1,color_black)
Expand Down
Loading

0 comments on commit 039bb2e

Please sign in to comment.