Skip to content

Commit

Permalink
Fixed: Attempt to index a nil value self.Trace
Browse files Browse the repository at this point in the history
Added: Zero width results in running the old trace method
Updated: Closest trace measured according to true start origin
  • Loading branch information
dvdvideo1234 committed Nov 5, 2021
1 parent 4a6881f commit 0ba3078
Showing 1 changed file with 34 additions and 22 deletions.
56 changes: 34 additions & 22 deletions lua/stargate/shared/tracelines.lua
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,26 @@ function StarGate.Trace:New(start,dir,ignore,mask,cogrp,iworld,width)
self.Data.start:Set(start)
self.Data.endpos:Set(dir); data.endpos:Add(start)
self.Data.filter = ignore
self.Data.mask = tonumber(mask) or MASK_SOLID
self.Data.collisiongroup = tonumber(cogrp) or COLLISION_GROUP_NONE
self.Data.mask = (tonumber(mask) or MASK_SOLID)
self.Data.collisiongroup = (tonumber(cogrp) or COLLISION_GROUP_NONE)
self.Data.ignoreworld = tobool(iworld)
if(width) then local m = width / 2
self.Data.mins:SetUnpacked(-m, -m, -m)
self.Data.maxs:SetUnpacked( m, m, m)
self.Trace.Code = util.TraceHull
else -- No width. Fall back to zero width trace
self.Trace.Code = util.TraceLine

-- Setup trace width and routine
if(width) then -- Trace cube hull with side of width
local m = (tonumber(width) or 0) / 2
if(m > 0) then -- Width is a valid non-zero number
self.Data.mins:SetUnpacked(-m, -m, -m)
self.Data.maxs:SetUnpacked( m, m, m)
self.Code = util.TraceHull -- Use New trace
else -- Margin must be a valid non-zero number
self.Code = util.TraceLine -- Use the old trace
end -- Otherwise palls back to using the old trace method
else -- No width so work as before. Fall back to zero width trace
self.Code = util.TraceLine
end

-- Run the trace when setup is ready and code is picked
local trace = self.Trace.Code(self.Data)
local trace = self.Code(self.Data)

-- This is better and faster than using table.HasValue(ignore,e) (nested for loops)
local quick_ignore = {};
Expand Down Expand Up @@ -222,22 +229,22 @@ function StarGate.Trace:New(start,dir,ignore,mask,cogrp,iworld,width)
local hit;
local hit2;
-- We need to check to what side the start pos is the nearest and if the normal (to that side where we checking it) isn't zero
if (class == "shield_core_buble") then // go ahead with my method @Mad
if StarGate.IsRayBoxIntersect(start, trace.HitPos, e) then // check, if we intersecting bounding box - save cpu if we are not
if (class == "shield_core_buble") then -- Go ahead with my method @Mad
if StarGate.IsRayBoxIntersect(start, trace.HitPos, e) then -- Check, if we intersecting bounding box - save cpu if we are not
local a = not in_box;
local dir2 = dir;
if in_box then dir2 = -1*dir end // fix shoting if we are inside, and not shape - to get hitpos on right side (not opposite)
if (e.ShShap == 2) then a = in_box end // small fix for box shape, i fucked triangles directions
if in_box then dir2 = -1*dir end -- Fix shoting if we are inside, and not shape - to get hitpos on right side (not opposite)
if (e.ShShap == 2) then a = in_box end -- Small fix for box shape, i fucked triangles directions
hit2 = StarGate.RayPhysicsPluckerIntersect(trace, dir2, e, a);
end
elseif (class == "tokra_shield") then // go ahead with my method @Mad
-- if StarGate.IsRayBoxIntersect(start, trace.HitPos, e) then // check, if we intersecting bounding box - save cpu if we are not
elseif (class == "tokra_shield") then -- Go ahead with my method @Mad
-- if StarGate.IsRayBoxIntersect(start, trace.HitPos, e) then -- Check, if we intersecting bounding box - save cpu if we are not
-- local a = not in_box;
-- local dir2 = dir;
-- if in_box then dir2 = -1*dir end // fix shoting if we are inside, and not shape - to get hitpos on right side (not opposite)
-- if (e.ShShap == 2) then a = in_box end // small fix for box shape, i fucked triangles directions
-- if in_box then dir2 = -1*dir end -- Fix shoting if we are inside, and not shape - to get hitpos on right side (not opposite)
-- if (e.ShShap == 2) then a = in_box end -- Small fix for box shape, i fucked triangles directions
hit2 = StarGate.RayPhysicsPluckerIntersect(trace, dir, e, true);
-- this code not working, need something to do @ AlexALX
-- This code not working, need something to do @ AlexALX
-- end
else
if(norm.x ~= 0) then
Expand Down Expand Up @@ -301,11 +308,16 @@ function StarGate.Trace:New(start,dir,ignore,mask,cogrp,iworld,width)
end
end

-- START OF Lynix modification
local anc, mar = trace.start -- First entry minumum @dvdvideo1234
for i = 1, #trace_array do -- Faster loop for arrays @dvdvideo1234
--[[ @Lynix @dvdvideo1234
* First entry is considered the first minimum
* Use for-integer loop as it is way faster than pairs
* Store true trace data reference to a local and compare
* If margin is not defined will be considered as minimum
]]
local anc, mar = self.Data.start
for i = 1, #trace_array do
local v = trace_array[i]
local m = anc:DistToSqr(v.HitPos) -- Faster check @dvdvideo1234
local m = anc:DistToSqr(v.HitPos)
if(not mar or m < mar) then
mar, trace = m, v;
end
Expand Down

0 comments on commit 0ba3078

Please sign in to comment.