Skip to content

Commit

Permalink
Fixed: Reduce the network overhead for CREATE_CURVE_NODE
Browse files Browse the repository at this point in the history
Fixed: Automatically populate POA row value on registration
Added: Automatic super-elevation for curved tracks
Added: 100% grade as OP variable `FULL_SLOPEDG` tan(45)
  • Loading branch information
dvdvideo1234 committed Nov 14, 2024
1 parent dbceb13 commit b69a709
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
6 changes: 4 additions & 2 deletions lua/autorun/trackassembly_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ local netSendToServer = net and net.SendToServer
local netReceive = net and net.Receive
local netReadEntity = net and net.ReadEntity
local netReadVector = net and net.ReadVector
local netReadNormal = net and net.ReadNormal
local netReadAngle = net and net.ReadAngle
local netReadBool = net and net.ReadBool
local netReadUInt = net and net.ReadUInt
Expand Down Expand Up @@ -562,9 +563,10 @@ if(CLIENT) then

asmlib.SetAction("CREATE_CURVE_NODE",
function(nLen) local oPly, sLog = netReadEntity(), "*CREATE_CURVE_NODE"
local vNode, vNorm, vBase = netReadVector(), netReadVector(), netReadVector()
local vNode, vNorm, vBase = netReadVector(), netReadNormal(), netReadVector()
local vOrgw, aAngw, bRayw = netReadVector(), netReadAngle() , netReadBool()
local tC = asmlib.GetCacheCurve(oPly) -- Read the curve data location
local iNorm, tC = netReadUInt(16), asmlib.GetCacheCurve(oPly) -- Read the curve
if(iNorm > 0 and tC.Size and tC.Size >= 2) then tC.Norm[iNorm]:Set(netReadNormal()) end
tableInsert(tC.Node, vNode); tableInsert(tC.Norm, vNorm)
tableInsert(tC.Base, vBase); tableInsert(tC.Rays, {vOrgw, aAngw, bRayw})
tC.Size = (tC.Size + 1) -- Register the index after writing the data for drawing
Expand Down
31 changes: 16 additions & 15 deletions lua/trackassembly/trackasmlib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ function InitBase(sName, sPurp)
SetOpVar("CURVE_MARGIN", 15)
SetOpVar("COLOR_CLAMP", {0, 255})
SetOpVar("GOLDEN_RATIO",1.61803398875)
SetOpVar("FULL_SLOPEDG", 45)
SetOpVar("DATE_FORMAT","%y-%m-%d")
SetOpVar("TIME_FORMAT","%H:%M:%S")
SetOpVar("NAME_INIT",sName:lower())
Expand Down Expand Up @@ -2290,6 +2291,7 @@ end
]]
function LocatePOA(oRec, ivPoID)
if(not oRec) then LogInstance("Missing record"); return nil end
local sMo = oRec.Slot
local tOffs = oRec.Offs; if(not tOffs) then
LogInstance("Missing offsets for "..GetReport(oRec.Slot)); return nil end
local iPoID = tonumber(ivPoID); if(iPoID) then iPoID = mathFloor(iPoID)
Expand All @@ -2298,8 +2300,9 @@ function LocatePOA(oRec, ivPoID)
LogInstance("Missing ID "..GetReport(iPoID, oRec.Slot)); return nil end
if(oRec.Post) then oRec.Post = nil -- Transforming has started
for ID = 1, oRec.Size do
local tPOA, sM = tOffs[ID], oRec.Slot
local sP, sO, sA = tPOA.P:Raw(), tPOA.O:Raw(), tPOA.A:Raw()
local tPOA = tOffs[ID]
local oP, oO, oA = tPOA.P, tPOA.O, tPOA.A
local sP, sO, sA = oP:Raw(), oO:Raw(), oA:Raw()
if(sO) then tPOA.O:Decode(sO, sM, "Pos") end
if(sA) then tPOA.A:Decode(sA, sM, "Ang") end
if(sP) then tPOA.P:Decode(sP, sM, "Pos", tPOA.O:Get()) end
Expand All @@ -2309,33 +2312,31 @@ function LocatePOA(oRec, ivPoID)
end

function RegisterPOA(stData, ivID, sP, sO, sA)
local sNull = GetOpVar("MISS_NOSQL"); if(not stData) then
local sNu = GetOpVar("MISS_NOSQL"); if(not stData) then
LogInstance("Cache record invalid"); return nil end
local iID = tonumber(ivID); if(not IsHere(iID)) then
LogInstance("Offset ID mismatch "..GetReport(ivID)); return nil end
local sP = (sP or sNull); if(not isstring(sP)) then
local sP = (sP or sNu); if(not isstring(sP)) then
LogInstance("Point mismatch "..GetReport(sP)); return nil end
local sO = (sO or sNull); if(not isstring(sO)) then
local sO = (sO or sNu); if(not isstring(sO)) then
LogInstance("Origin mismatch "..GetReport(sO)); return nil end
local sA = (sA or sNull); if(not isstring(sA)) then
local sA = (sA or sNu); if(not isstring(sA)) then
LogInstance("Angle mismatch "..GetReport(sA)); return nil end
LogInstance("Store "..GetReport(sNull, iID, sP, sO, sA, stData.Slot))
LogInstance("Store "..GetReport(sNu, iID, sP, sO, sA, stData.Slot))
if(not stData.Offs) then if(iID ~= 1) then
LogInstance("Mismatch ID "..GetReport(iID, stData.Slot)); return nil end
stData.Offs = {}; stData.Post = true
stData.Offs = {}; stData.Post = true -- Mark post-process on spawn
end
local tOffs = stData.Offs; if(tOffs[iID]) then
LogInstance("Exists ID "..GetReport(iID)); return tOffs
else
else -- The offset ID does not exists so create one
if((iID > 1) and (not tOffs[iID - 1])) then
LogInstance("Scatter ID "..GetReport(iID)); return nil end
tOffs[iID] = {}; tOffs = tOffs[iID] -- Allocate a local offset index
tOffs.P = NewPOA(); tOffs.O = NewPOA(); tOffs.A = NewPOA()
end
if(not tOffs.O:Raw()) then tOffs.O:Set(); tOffs.O:Raw(sO) end
if(not tOffs.A:Raw()) then tOffs.A:Set(); tOffs.A:Raw(sA) end
if(not tOffs.P:Raw()) then tOffs.P:Set(); tOffs.P:Raw(sP) end
return tOffs -- On success return the populated POA offset
tOffs.P = NewPOA(); tOffs.P:Set(); tOffs.P:Raw(sP)
tOffs.O = NewPOA(); tOffs.O:Set(); tOffs.O:Raw(sO)
tOffs.A = NewPOA(); tOffs.A:Set(); tOffs.A:Raw(sA)
end; return tOffs -- On success return the populated POA offset
end

function Arrange(tSrc, vPrn, ...)
Expand Down
25 changes: 18 additions & 7 deletions lua/weapons/gmod_tool/stools/trackassembly.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ local netWriteBool = net and net.WriteBool
local netWriteAngle = net and net.WriteAngle
local netWriteEntity = net and net.WriteEntity
local netWriteVector = net and net.WriteVector
local netWriteNormal = net and net.WriteNormal
local vguiCreate = vgui and vgui.Create
local stringUpper = string and string.upper
local mathAbs = math and math.abs
Expand Down Expand Up @@ -914,11 +915,23 @@ function TOOL:GetCurveTransform(stTrace, bPnt)
end

function TOOL:CurveInsert(stTrace, bPnt, bMute)
local spnflat, iN, vN = self:GetSpawnFlat(), 0
local user, model = self:GetOwner(), self:GetModel()
local tData = self:GetCurveTransform(stTrace, bPnt); if(not tData) then
asmlib.LogInstance("Transform missing", gtLogs); return nil end
local tC = asmlib.GetCacheCurve(user); if(not tC) then
asmlib.LogInstance("Curve missing", gtLogs); return nil end
if(not spnflat and tC.Size and tC.Size >= 2) then
local nS, iC = asmlib.GetOpVar("FULL_SLOPEDG"), tC.Size
local tO, tR, tN = tC.Node, tC.Rays, tC.Norm
local vA, vB = tO[iC], tO[iC - 1]
local vD = Vector(tData.Org); vD:Sub(vA); vD:Normalize()
local vF = Vector(vA); vF:Sub(vB); vF:Normalize()
local aN = vF:AngleEx(tR[iC][2]:Up())
local nP = 0.5 * nS * vD:Dot(aN:Right())
aN:RotateAroundAxis(vF, nP)
vN = aN:Up(); tN[iC]:Set(vN); iN = iC
end
tC.Size = (tC.Size + 1) -- Increment stack size. Adding stuff
tableInsert(tC.Node, Vector(tData.Org))
tableInsert(tC.Norm, tData.Ang:Up())
Expand All @@ -929,11 +942,13 @@ function TOOL:CurveInsert(stTrace, bPnt, bMute)
netStart(gsLibName.."SendCreateCurveNode")
netWriteEntity(user)
netWriteVector(tC.Node[tC.Size])
netWriteVector(tC.Norm[tC.Size])
netWriteNormal(tC.Norm[tC.Size])
netWriteVector(tC.Base[tC.Size])
netWriteVector(tC.Rays[tC.Size][1])
netWriteAngle (tC.Rays[tC.Size][2])
netWriteBool (tC.Rays[tC.Size][3])
netWriteUInt (iN, 16)
if(iN > 0) then netWriteNormal(vN) end
netSend(user)
user:SetNWBool(gsToolPrefL.."engcurve", true)
end
Expand Down Expand Up @@ -1187,8 +1202,7 @@ function TOOL:LeftClick(stTrace)
}, function(oPly, oArg)
for iD = oArg.stard, tC.SSize do tS = tC.Snap[iD]
for iK = oArg.stark, tS.Size do local tV, ePiece = tS[iK], nil
oArg.spawn = asmlib.GetNormalSpawn(oPly, tV[1], tV[2], model, pointid,
nextx, nexty, nextz, nextpic, nextyaw, nextrol, oArg.spawn)
oArg.spawn = asmlib.GetNormalSpawn(oPly, tV[1], tV[2], model, pointid, 0, 0, 0, 0, 0, 0, oArg.spawn)
if(not oArg.spawn) then -- Make sure it persists to set it afterwards
asmlib.LogInstance(self:GetStatus(stTrace,"("..oArg.wname..") "..sItr..": Cannot obtain spawn data"),gtLogs); return false end
if(crvturnlm > 0 or crvleanlm > 0) then local nF, nU = asmlib.GetTurningFactor(oPly, tS, iK)
Expand Down Expand Up @@ -1629,8 +1643,6 @@ function TOOL:UpdateGhostCurve()
local stackcnt = self:GetStackCount()
local pointid, pnextid = self:GetPointID()
local tGho, iGho = asmlib.GetOpVar("ARRAY_GHOST"), 0
local nextx, nexty, nextz = self:GetPosOffsets()
local nextpic, nextyaw, nextrol = self:GetAngOffsets()
local bCrv = user:GetNWBool(gsToolPrefL.."engcurve", false)
if(bCrv) then
local workmode = self:GetWorkingMode()
Expand All @@ -1650,8 +1662,7 @@ function TOOL:UpdateGhostCurve()
for iD = 1, tCrv.SSize do local tS = tCrv.Snap[iD]
for iK = 1, tS.Size do iGho = (iGho + 1)
local tV, eGho = tS[iK], tGho[iGho]
local stSpawn = asmlib.GetNormalSpawn(user, tV[1], tV[2], model, pointid,
nextx, nexty, nextz, nextpic, nextyaw, nextrol)
local stSpawn = asmlib.GetNormalSpawn(user, tV[1], tV[2], model, pointid, 0, 0, 0, 0, 0, 0)
if(eGho and eGho:IsValid()) then eGho:SetNoDraw(true)
if(stackcnt > 0) then if(iGho > stackcnt) then eGho:SetNoDraw(true) else
if(stSpawn) then eGho:SetPos(stSpawn.SPos); eGho:SetAngles(stSpawn.SAng); eGho:SetNoDraw(false) end end
Expand Down

0 comments on commit b69a709

Please sign in to comment.