Skip to content

Commit

Permalink
More new models, new E2/SF functions
Browse files Browse the repository at this point in the history
Added more models courtesy of ghosteh (electric turret motor, hydraulic turret motor, gyroscope, and computer)

Adjusted min/max arc limits to not pass 0, so there is less potential confusion and the turret is always able to reach "home"

New E2/SF functions including:
E:acfGetTurret*
- Angle: Current degrees from center
- Gyro: Linked gyro
- Motor: Linked motor
- Mass: Loaded mass (kg)
- MassCenter: Local mass center
- SlewRate: Current slew rate (degrees/s)
- MaxSlewRate: Maximum slew rate (degrees/s)
- SlewAccel: Slew acceleration (degrees/s^2)
- Stabilized: 0-1 depending on stabilization
- Data: Table containing most of the above data for ease of use

(E2)
E:acfCenterTurret: Returns the turret to center
E:acfAimTurret: Takes a number, angle, or vector
- Number: Local degrees from center
- Angle: Global angle to attempt to aim at
- Vector: Position to attempt to aim at

(SF)
E:acfCenterTurret: Same as E2
E:acfSetTurretDegree: Attempts to rotate to this degree from center
E:acfSetTurretAngle: Attempts to rotate to this global angle
E:acfSetTurretTargetPosition: Attempts to rotate to aim at this position
  • Loading branch information
LiddulBOFH committed Feb 2, 2024
1 parent 12227d5 commit 7bf1a49
Show file tree
Hide file tree
Showing 36 changed files with 495 additions and 23 deletions.
11 changes: 7 additions & 4 deletions lua/acf/entities/turrets/turret_menu_cl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ do -- Turret ring
Panel:SetBGColor(Color(150,150,150))
end

local MinDeg = ArcSettings:AddSlider("Minimum Degrees", -180, 180, 1)
local MinDeg = ArcSettings:AddSlider("Minimum Degrees", -180, 0, 1)

local MaxDeg = ArcSettings:AddSlider("Maximum Degrees", -180, 180, 1)
local MaxDeg = ArcSettings:AddSlider("Maximum Degrees", 0, 180, 1)

local ArcDraw = vgui.Create("Panel",ArcSettings)
ArcDraw:SetSize(64,64)
Expand Down Expand Up @@ -99,7 +99,7 @@ do -- Turret ring

MinDeg:SetClientData("MinDeg","OnValueChanged")
MinDeg:DefineSetter(function(Panel, _, _, Value)
local N = math.Clamp(math.Round(Value,1),-180,MaxDeg:GetValue())
local N = math.Clamp(math.Round(Value,1),-180,0)

Panel:SetValue(N)

Expand All @@ -110,7 +110,7 @@ do -- Turret ring

MaxDeg:SetClientData("MaxDeg", "OnValueChanged")
MaxDeg:DefineSetter(function(Panel, _, _, Value)
local N = math.Clamp(math.Round(Value,1),MinDeg:GetValue(),180)
local N = math.Clamp(math.Round(Value,1),0,180)

Panel:SetValue(N)

Expand All @@ -119,6 +119,9 @@ do -- Turret ring
MaxDeg:SetValue(180)
MaxDeg:SetEnabled(false)

ACF.SetClientData("MinDeg",-180)
ACF.SetClientData("MaxDeg",180)

ArcToggle.OnChange = function(_, Value)
MinDeg:SetEnabled(Value)
MaxDeg:SetEnabled(Value)
Expand Down
16 changes: 9 additions & 7 deletions lua/acf/entities/turrets/turrets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,10 @@ do -- Turret drives

SlewFuncs = {
GetStab = function(Turret)
if (not (Turret.Stabilized and Turret.Active)) or (Turret.Manual == true) then return 0 end
local AngDiff = Turret.Rotator:WorldToLocalAngles(Turret.LastRotatorAngle)

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

GetTargetBearing = function(Turret,StabAmt)
Expand Down Expand Up @@ -225,9 +226,10 @@ do -- Turret drives

SlewFuncs = {
GetStab = function(Turret)
if (not (Turret.Stabilized and Turret.Active)) or (Turret.Manual == true) then return 0 end
local AngDiff = Turret.Rotator:WorldToLocalAngles(Turret.LastRotatorAngle)

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

GetTargetBearing = function(Turret,StabAmt)
Expand Down Expand Up @@ -284,7 +286,7 @@ do -- Turret motors
Turrets.RegisterItem("Motor-ELC","2-Motor",{
Name = "Electric Motor",
Description = "A snappy responsive electric motor, can handle most uses cases but quickly falters under higher weights",
Model = "models/engines/emotor-standalone-sml.mdl",
Model = "models/acf/core/t_drive_e.mdl",
Sound = "acf_base/fx/turret_electric.wav",

Mass = 60, -- Base mass, will be further modified by settings
Expand Down Expand Up @@ -315,7 +317,7 @@ do -- Turret motors
Turrets.RegisterItem("Motor-HYD","2-Motor",{
Name = "Hydraulic Motor",
Description = "A strong but sluggish hydraulic motor, it'll turn the world over but takes a little bit to get to that point.",
Model = "models/xqm/hydcontrolbox.mdl",
Model = "models/acf/core/t_drive_h.mdl",
Sound = "acf_base/fx/turret_hydraulic.wav",

Mass = 80, -- Base mass, will be further modified by settings
Expand Down Expand Up @@ -375,7 +377,7 @@ do -- Turret gyroscopes
Turrets.RegisterItem("2-Gyro","3-Gyro",{
Name = "Dual Axis Turret Gyro",
Description = "A component that will stabilize one vertical and horizontal turret drive.\nMust be parented to or share the parent with the horizontal turret drive.\nEach turret drive must have a motor linked.",
Model = "models/kobilica/relay.mdl",
Model = "models/acf/core/t_gyro.mdl",

Mass = 150,
IsDual = true,
Expand Down Expand Up @@ -413,7 +415,7 @@ do -- Turret computers
Turrets.RegisterItem("DIR-BalComp","4-Computer",{
Name = "Direct Ballistics Computer",
Description = "A component that is capable of calculating the angle required to shoot a weapon to hit a spot within view.\nHas a delay between uses.",
Model = "",
Model = "models/acf/core/t_computer.mdl",
Mass = 100,
Expand All @@ -426,7 +428,7 @@ do -- Turret computers
Turrets.RegisterItem("IND-BalComp","4-Computer",{
Name = "Indirect Ballistics Computer",
Description = "A component that is capable of calculating the angle required to shoot a weapon to hit a spot out of view.\nHas a delay between uses.",
Model = "",
Model = "models/acf/core/t_computer.mdl",
Mass = 150,
Expand Down
23 changes: 18 additions & 5 deletions lua/entities/acf_turret/cl_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ do -- Turret drive drawing
if CSM:GetColor() ~= self:GetColor() then CSM:Remove() return end
if CSM.Matrix ~= self.Matrix then CSM:Remove() return end

if CSM:GetParent() ~= self then CSM:Remove() return end

CSM:SetAngles(Rotator:GetAngles())
end

Expand Down Expand Up @@ -128,12 +130,13 @@ do -- Overlay

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
if IsValid(self.Rotator) then render.DrawLine(Pos,Pos + self.Rotator:GetForward() * X,color_white,true) end

local LocPos = self:WorldToLocal(Trace.HitPos)
local AimAng = 0
local CurAng = 0
local LocDir = Vector(LocPos.x,LocPos.y,0):GetNormalized()
local HasArc = not ((self.MinDeg == -180) and (self.MaxDeg == 180))

if self.Type == "Turret-V" then
LocDir = Vector(LocPos.x,0,LocPos.z):GetNormalized()
Expand All @@ -152,7 +155,9 @@ do -- Overlay
render.DrawWireframeSphere(self.Rotator:LocalToWorld(self.LocalCoM),1.5,4,3,red)
render.OverrideDepthEnable(false,false)

if not ((self.MinDeg == -180) and (self.MaxDeg == 180)) then
local MinArcPos = {}
local MaxArcPos = {}
if HasArc then
local MinDir = Vector(X,0,0)
local MaxDir = Vector(X,0,0)

Expand All @@ -164,8 +169,11 @@ do -- Overlay
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)
render.DrawLine(Pos - UX * 2,self:LocalToWorld(self:OBBCenter() + MinDir) - UX * 2,red,true)
render.DrawLine(Pos - UX * 2,self:LocalToWorld(self:OBBCenter() + MaxDir) - UX * 2,green,true)

MinArcPos = (self:LocalToWorld(self:OBBCenter() + MinDir) - UX * 2):ToScreen()
MaxArcPos = (self:LocalToWorld(self:OBBCenter() + MaxDir) - UX * 2):ToScreen()
end

local HomePos = (Pos + UX + self:GetForward() * X):ToScreen()
Expand All @@ -176,11 +184,16 @@ 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: " .. CurAng,"DermaDefault",CurPos.x,CurPos.y,green,TEXT_ALIGN_CENTER,TEXT_ALIGN_CENTER,1,color_black)
draw.SimpleTextOutlined("Current: " .. CurAng,"DermaDefault",CurPos.x,CurPos.y,color_white,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)

if HasArc then
draw.SimpleTextOutlined("Min: " .. self.MinDeg,"DermaDefault",MinArcPos.x,MinArcPos.y,red,TEXT_ALIGN_CENTER,TEXT_ALIGN_CENTER,1,color_black)
draw.SimpleTextOutlined("Max: " .. self.MaxDeg,"DermaDefault",MaxArcPos.x,MaxArcPos.y,green,TEXT_ALIGN_CENTER,TEXT_ALIGN_CENTER,1,color_black)
end
cam.End2D()
end
end
8 changes: 5 additions & 3 deletions lua/entities/acf_turret/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,8 @@ do -- Spawn and Update funcs
if not IsValid(k) then continue end
ParentLink(Entity,k,true)

if Class == "acf_turret_motor" then k:ValidatePlacement() end

local PO = k:GetPhysicsObject()
if not IsValid(PO) then continue end

Expand Down Expand Up @@ -762,15 +764,15 @@ do -- Metamethods
self.UseVector = false

if isnumber(Direction) then
self.DesiredDeg = Direction
self.DesiredDeg = math.NormalizeAngle(Direction)
return
end

self.Manual = false

if isangle(Direction) then
Direction:Normalize()
self.DesiredAng = Direction
self.DesiredAngle = Direction

return
end
Expand Down Expand Up @@ -819,7 +821,7 @@ do -- Metamethods

local StabAmt = math.Clamp(self.SlewFuncs.GetStab(self), -SlewMax, SlewMax)

local TargetBearing = self.SlewFuncs.GetTargetBearing(self,StabAmt)
local TargetBearing = math.Round(self.SlewFuncs.GetTargetBearing(self,StabAmt),8)

local sign = TargetBearing < 0 and -1 or 1
local Dist = math.abs(TargetBearing)
Expand Down
1 change: 1 addition & 0 deletions lua/entities/acf_turret_gyro/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ ENT.PrintName = "ACF Turret Gyro"
ENT.WireDebugName = "ACF Turret Gyro"
ENT.PluralName = "ACF Turret Gyros"
ENT.IsACFEntity = true
ENT.IsACFGyro = true

cleanup.Register("_acf_turret_gyro")
1 change: 1 addition & 0 deletions lua/entities/acf_turret_motor/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ ENT.PrintName = "ACF Turret Motor"
ENT.WireDebugName = "ACF Turret Motor"
ENT.PluralName = "ACF Turret Motors"
ENT.IsACFEntity = true
ENT.IsACFMotor = true

cleanup.Register("_acf_turret_motor")
Loading

0 comments on commit 7bf1a49

Please sign in to comment.