From f09a049f85478a2a0c63e97b895eebb7f9f282bd Mon Sep 17 00:00:00 2001 From: Robotboy655 Date: Thu, 6 Mar 2014 13:18:57 +0200 Subject: [PATCH] Fixed some issues with constraint tools * Fixed Muscle tool "Start on" option not doing anything * Fixed Elastic tool creating a rope even if no elastic constraint were created --- .../entities/gmod_winch_controller.lua | 96 +++++++++---------- garrysmod/lua/includes/modules/constraint.lua | 26 +++-- 2 files changed, 59 insertions(+), 63 deletions(-) diff --git a/garrysmod/gamemodes/sandbox/entities/entities/gmod_winch_controller.lua b/garrysmod/gamemodes/sandbox/entities/entities/gmod_winch_controller.lua index e65aae0aae..665e020e32 100644 --- a/garrysmod/gamemodes/sandbox/entities/entities/gmod_winch_controller.lua +++ b/garrysmod/gamemodes/sandbox/entities/entities/gmod_winch_controller.lua @@ -21,6 +21,7 @@ function ENT:Initialize() self.min_length = self.min_length or 1 self.type = self.type or TYPE_NORMAL self.ctime = self.ctime or 0 + self.isexpanded = false end @@ -30,9 +31,9 @@ end Desc: Called when a keyvalue is added to us -----------------------------------------------------------]] function ENT:KeyValue( key, value ) - if (key == "minlength") then self.min_length = tonumber(value) - elseif (key == "maxlength") then self.max_length = tonumber(value) - elseif (key == "type") then self.type = tonumber(value) + if ( key == "minlength" ) then self.min_length = tonumber( value ) + elseif ( key == "maxlength" ) then self.max_length = tonumber( value ) + elseif ( key == "type" ) then self.type = tonumber( value ) end end @@ -46,14 +47,15 @@ function ENT:Think() local TimeDiff = CurTime() - self.last_time self.last_time = CurTime() - if (!self.constraint) then return end - if (!self.direction) then return end - if (self.direction == DIR_NONE) then return end + if ( !self.constraint ) then return true end + if ( !self.direction ) then return true end + if ( self.direction == DIR_NONE ) then return true end + local old_length = self.current_length local current_length = self.current_length - if (self.type == TYPE_NORMAL) then + if ( self.type == TYPE_NORMAL ) then local speed = 0 local dist = 0 @@ -66,14 +68,14 @@ function ENT:Think() dist = -speed * TimeDiff end - if (dist == 0) then return true end + if ( dist == 0 ) then return true end current_length = current_length + dist if ( self.min_length && current_length < self.min_length ) then current_length = self.min_length - if ( self.toggle ) then self.direction = DIR_NONE; end + if ( self.toggle ) then self.direction = DIR_NONE end end @@ -83,7 +85,7 @@ function ENT:Think() current_length = self.max_length self.isexpanded = true - if ( self.toggle ) then self.direction = DIR_NONE end + if ( self.toggle ) then self.direction = DIR_NONE end else @@ -98,14 +100,16 @@ function ENT:Think() local amp = self.constraint.amplitude local per = self.constraint.period - if ( per == 0 ) then return end + if ( per == 0 ) then return true end - local spos = ( math.sin( (self.ctime * math.pi * per )) + 1 ) * (amp / 2) + local spos = ( math.sin( (self.ctime * math.pi * per )) + 1 ) * ( amp / 2 ) - if (spos > amp) then spos = amp end - if (spos < 0) then spos = 0 end + if ( spos > amp ) then spos = amp end + if ( spos < 0 ) then spos = 0 end - current_length = self.min_length + spos + if ( self.direction != DIR_NONE ) then + current_length = self.min_length + spos + end self.ctime = self.ctime + TimeDiff end @@ -122,17 +126,16 @@ end function ENT:GetPos( ent, phys, lpos ) - if (ent:EntIndex() == 0) then + if ( ent:EntIndex() == 0 ) then return lpos end - if (phys ~= nil && phys:IsValid()) then + if ( IsValid( phys ) ) then return phys:LocalToWorld( lpos ) else return ent:LocalToWorld( lpos ) end - end @@ -142,25 +145,25 @@ function ENT:SetConstraint( c ) self.direction = DIR_NONE self.toggle = c.toggle - local p1 = self:GetPos(c.Ent1, c.Phys1, c.LPos1) - local p2 = self:GetPos(c.Ent2, c.Phys2, c.LPos2) - local dist = (p1 - p2) + local p1 = self:GetPos( c.Ent1, c.Phys1, c.LPos1 ) + local p2 = self:GetPos( c.Ent2, c.Phys2, c.LPos2 ) + local dist = ( p1 - p2 ) self.current_length = dist:Length() - if (self.max_length) then - self.isexpanded = (self.current_length >= self.max_length) + if ( self.max_length ) then + self.isexpanded = ( self.current_length >= self.max_length ) end - if (self.type == TYPE_MUSCLE) then + if ( self.type == TYPE_MUSCLE ) then local amp = self.constraint.amplitude local per = self.constraint.period local spos = self.current_length - self.min_length - spos = spos / (amp*2) + spos = spos / ( amp * 2 ) spos = spos - 1 - spos = math.Clamp(spos, -1, 1) -- just in case! - spos = math.asin(spos) - spos = spos / (per * math.pi) + spos = math.Clamp( spos, -1, 1 ) -- just in case! + spos = math.asin( spos ) + spos = spos / ( per * math.pi ) self.ctime = spos end @@ -187,12 +190,11 @@ end ------------------------------------------------------------------------]] local function HydraulicToggle( pl, hyd ) - if ( !hyd || !hyd:IsValid() ) then return false end + if ( !IsValid( hyd ) ) then return false end -- I hate this, shouldn't we just be calling hyd:Toggle() - - if ( hyd:GetDirection() == 0 ) then + if ( hyd:GetDirection() == 0 ) then if ( hyd:IsExpanded() ) then hyd:SetDirection( -1 ) else @@ -205,30 +207,28 @@ local function HydraulicToggle( pl, hyd ) elseif ( hyd:GetDirection() == 1 ) then - hyd:SetDirection( - 1) + hyd:SetDirection( -1 ) end end - numpad.Register( "HydraulicToggle", HydraulicToggle ) - --[[---------------------------------------------------------------------- WinchOn - Called to switch the winch on ------------------------------------------------------------------------]] local function WinchOn( pl, winch, dir ) - if ( !winch || !winch:IsValid() ) then return false end - winch:SetDirection(dir) + if ( !IsValid( winch ) ) then return false end + winch:SetDirection( dir ) end numpad.Register( "WinchOn", WinchOn ) --[[---------------------------------------------------------------------- - WinchOn - Called to switch the winch off + WinchOff - Called to switch the winch off ------------------------------------------------------------------------]] local function WinchOff( pl, winch ) - if ( !winch || !winch:IsValid() ) then return false end - winch:SetDirection(0) + if ( !IsValid( winch ) ) then return false end + winch:SetDirection( 0 ) end numpad.Register( "WinchOff", WinchOff ) @@ -236,23 +236,21 @@ numpad.Register( "WinchOff", WinchOff ) WinchToggle - Called to toggle the winch ------------------------------------------------------------------------]] local function WinchToggle( pl, winch, dir ) - if ( !winch || !winch:IsValid() ) then return false end - if (winch:GetDirection() == dir) then winch:SetDirection(0) - else winch:SetDirection(dir) end + if ( !IsValid( winch ) ) then return false end + if ( winch:GetDirection() == dir ) then winch:SetDirection( 0 ) + else winch:SetDirection( dir ) end end numpad.Register( "WinchToggle", WinchToggle ) - local function MuscleToggle( pl, hyd ) - if ( !hyd || !hyd:IsValid() ) then return false end + if ( !IsValid( hyd ) ) then return false end - if (hyd:GetDirection() == 0) then - hyd:SetDirection(1) + if ( hyd:GetDirection() == 0 ) then + hyd:SetDirection( 1 ) else - hyd:SetDirection(0) + hyd:SetDirection( 0 ) end end - -numpad.Register( "MuscleToggle", MuscleToggle ) \ No newline at end of file +numpad.Register( "MuscleToggle", MuscleToggle ) diff --git a/garrysmod/lua/includes/modules/constraint.lua b/garrysmod/lua/includes/modules/constraint.lua index 485c3a0446..bb68593723 100644 --- a/garrysmod/lua/includes/modules/constraint.lua +++ b/garrysmod/lua/includes/modules/constraint.lua @@ -569,6 +569,7 @@ function Elastic( Ent1, Ent2, Bone1, Bone2, LPos1, LPos2, constant, damping, rda local WPos2 = Phys2:LocalToWorld( LPos2 ) local Constraint = nil + local rope = nil -- Make Constraint if ( Phys1 != Phys2 ) then @@ -615,18 +616,15 @@ function Elastic( Ent1, Ent2, Bone1, Bone2, LPos1, LPos2, constant, damping, rda Constraint:SetTable( ctable ) + -- Make Rope + local kv = { + Collide = 1, + Type = 0 + } + + rope = CreateKeyframeRope( WPos1, width, material, Constraint, Ent1, LPos1, Bone1, Ent2, LPos2, Bone2, kv ) end - -- Make Rope - local kv = - { - Collide = 1, - Type = 0 - } - - local rope = CreateKeyframeRope( WPos1, width, material, Constraint, Ent1, LPos1, Bone1, Ent2, LPos2, Bone2, kv ) - - return Constraint, rope end @@ -1408,7 +1406,7 @@ function Hydraulic( pl, Ent1, Ent2, Bone1, Bone2, LPos1, LPos2, Length1, Length2 Constraint:DeleteOnRemove( controller ) - numpad.OnDown( pl, key, "HydraulicToggle", controller ) + numpad.OnDown( pl, key, "HydraulicToggle", controller ) return Constraint,rope,controller,slider else @@ -1445,7 +1443,7 @@ function Muscle( pl, Ent1, Ent2, Bone1, Bone2, LPos1, LPos2, Length1, Length2, w local ctable = { Type = "Muscle", - pl = pl, + pl = pl, Ent1 = Ent1, Ent2 = Ent2, Bone1 = Bone1, @@ -1479,7 +1477,7 @@ function Muscle( pl, Ent1, Ent2, Bone1, Bone2, LPos1, LPos2, Length1, Length2, w controller:SetKeyValue("minlength", Length2) controller:SetKeyValue("maxlength", Length1) end - controller:SetKeyValue("type", 1) + controller:SetKeyValue( "type", 1 ) controller:GetTable():SetConstraint( Constraint ) controller:Spawn() @@ -1488,7 +1486,7 @@ function Muscle( pl, Ent1, Ent2, Bone1, Bone2, LPos1, LPos2, Length1, Length2, w Constraint:DeleteOnRemove( controller ) - numpad.OnDown( pl, key, "MuscleToggle", controller ) + numpad.OnDown( pl, key, "MuscleToggle", controller ) if ( starton ) then controller:SetDirection( 1 )