Whitemambaa

The Perfect Wiggler

Dec 16th, 2014
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.77 KB | None | 0 0
  1. --By Quaternions
  2. --localize
  3. local abs=math.abs
  4. local ceil=math.ceil
  5. local min,max=math.min,math.max
  6. local sqrt=math.sqrt
  7. local tan=math.tan
  8.  
  9. local bit=bit
  10.  
  11. local insert,remove=table.insert,table.remove
  12.  
  13. local tick=SysTime
  14. local Angle=Angle
  15. local IsBtn=input.IsMouseDown
  16.  
  17. local Degree=math.pi/180
  18.  
  19. local IN_FORWARD=8
  20. local IN_BACK=16
  21. local IN_MOVELEFT=512
  22. local IN_MOVERIGHT=1024
  23. local IN_SPEED=131072
  24. local MOUSE_LEFT=107
  25.  
  26. local P=LocalPlayer()
  27. local Aspect=ScrH()/ScrW()/tan(P:GetFOV()*Degree/2)
  28.  
  29. --Settings
  30. local Calibrating=true
  31.  
  32. local MaxAngleSeconds=0.07
  33. local AveragingDuration=0.1
  34. local MinAveragingFrames=8
  35.  
  36. local DirectionBarWidth=30
  37. local DirectionBarHeight=300
  38.  
  39. local DirectionBarColour=Color(75,191,50,128)
  40.  
  41. --Ongoing collected data
  42. local Data={}
  43. local DataOverwrite=0
  44. local ConsecutiveValidFrames=0
  45. local _00,_01,_10,_11,_0,_1=0,0,0,0,0,0--Used to calculate linear least squares fit of equation constants
  46.  
  47. --Previous frame data
  48. local Wiggling=false
  49. local LastTime=tick()
  50. local LastAngle=P:EyeAngles().y
  51. local LastVelocity=P:GetAbsVelocity():Length2D()
  52. local CurrentAveragingFrames=0
  53.  
  54. --Previouse motion data
  55. local LastTime2=LastTime
  56. local TotalAngle
  57. local StrafeDirection=1
  58. local RealAngles
  59. local LastViewAngles
  60.  
  61. --Make GUI
  62. local RealDirection=vgui.Create("DPanel")
  63. RealDirection:SetSize(DirectionBarWidth,DirectionBarHeight)
  64. RealDirection:SetPos(ScrW()/2-DirectionBarWidth/2,0)
  65. RealDirection:SetBackgroundColor(DirectionBarColour)
  66. RealDirection:SetAlpha(0)
  67.  
  68. hook.Add("PreDrawHUD","UpdateWiggler",function()
  69.     local CurrentTime=tick()
  70.     local FramePeriod=CurrentTime-LastTime
  71.     local DesiredAveragingFrames=max(MinAveragingFrames,ceil(AveragingDuration/FramePeriod))
  72.     local Velocity=P:GetAbsVelocity()
  73.     local CurrentAngle=P:EyeAngles().y
  74.     local CurrentVelocity=Velocity:Length2D()
  75.     local CurrentYawspeed=((CurrentAngle-LastAngle+180)%360-180)/FramePeriod
  76.     local CurrentAcceleration=(CurrentVelocity-LastVelocity)/FramePeriod
  77.  
  78.     if DesiredAveragingFrames>CurrentAveragingFrames then
  79.         CurrentAveragingFrames=CurrentAveragingFrames+1
  80.         DataOverwrite=DataOverwrite+1
  81.         insert(Data,DataOverwrite,{v=LastVelocity,w=abs(CurrentYawspeed),a=CurrentAcceleration})
  82.     elseif DesiredAveragingFrames<CurrentAveragingFrames then
  83.         remove(Data,DataOverwrite%CurrentAveragingFrames+1)
  84.         CurrentAveragingFrames=CurrentAveragingFrames-1
  85.         if DataOverwrite>=CurrentAveragingFrames then
  86.             DataOverwrite=1
  87.         else
  88.             DataOverwrite=DataOverwrite+1
  89.         end
  90.         Data[DataOverwrite]={v=LastVelocity,w=abs(CurrentYawspeed),a=CurrentAcceleration}
  91.     else
  92.         DataOverwrite=DataOverwrite%CurrentAveragingFrames+1
  93.         Data[DataOverwrite]={v=LastVelocity,w=abs(CurrentYawspeed),a=CurrentAcceleration}
  94.     end
  95.  
  96.     if Calibrating then
  97.         local EyeAngle=Angle(0,CurrentAngle,0)
  98.         local ControlDirection=EyeAngle:Forward()*((P:KeyDown(IN_FORWARD)and 1 or 0)-(P:KeyDown(IN_BACK)and 1 or 0))+EyeAngle:Right()*((P:KeyDown(IN_MOVERIGHT)and 1 or 0)-(P:KeyDown(IN_MOVELEFT)and 1 or 0))
  99.         local ValidFrame=false
  100.         if CurrentYawspeed~=0 and CurrentAcceleration>0 and not P:OnGround() and CurrentVelocity>0 then
  101.             local cx,cy=ControlDirection.x,ControlDirection.y
  102.             local c2=cx*cx+cy*cy
  103.             if c2>0 then
  104.                 local vc=Velocity.x*cx+Velocity.y*cy
  105.                 if vc*vc/(c2*CurrentVelocity*CurrentVelocity)<0.0025 then --If the control direction is (approximately) perpendicular to the velocity
  106.                     ValidFrame=true
  107.                 end
  108.             end
  109.         end
  110.         if ValidFrame then
  111.             ConsecutiveValidFrames=ConsecutiveValidFrames+1
  112.             if ConsecutiveValidFrames>=CurrentAveragingFrames then
  113.                 local v,w,a=0,0,0
  114.                 for i=1,CurrentAveragingFrames do
  115.                     local d=Data[i]
  116.                     v,w,a=v+d.v,w+d.w,a+d.a
  117.                 end
  118.                 v,w,a=v/CurrentAveragingFrames,w/CurrentAveragingFrames,a/CurrentAveragingFrames
  119.                 local w2=w*w
  120.                 _0=_0-a*w
  121.                 _00=_00+w2
  122.                 _01=_01-v*w*w2
  123.                 _1=_1+a*v*w2
  124.                 _10=_10-v*w*w2
  125.                 _11=_11+v*v*w2*w2
  126.             end
  127.         elseif ConsecutiveValidFrames>0 then
  128.             ConsecutiveValidFrames=0
  129.         end
  130.     end
  131.  
  132.     if TotalAngle and RealAngles then
  133.         local RelativeAngle=(RealAngles.y-TotalAngle+180)%360-180
  134.         RealDirection:SetAlpha(255)
  135.         RealDirection:SetPos((ScrW()-DirectionBarWidth)*(0.5-Aspect*tan(RelativeAngle*Degree)/2),0)
  136.     else
  137.         RealDirection:SetAlpha(0)
  138.     end
  139.     LastTime,LastAngle,LastVelocity=CurrentTime,CurrentAngle,CurrentVelocity
  140. end)
  141.  
  142. hook.Add("CreateMove","Wiggle",function(cmd)
  143.     local CurrentTime=tick()
  144.     local FramePeriod=CurrentTime-LastTime2
  145.  
  146.     --Autohop
  147.     if not P:OnGround() then
  148.         cmd:SetButtons(bit.band(cmd:GetButtons(),bit.bnot(IN_JUMP)))
  149.     end
  150.  
  151.     if cmd:KeyDown(IN_SPEED) and (Wiggling or not P:OnGround()) then
  152.         if Wiggling then
  153.             local InputAngles=cmd:GetViewAngles()
  154.             RealAngles=RealAngles+InputAngles-LastViewAngles
  155.         else
  156.             RealAngles=cmd:GetViewAngles()
  157.             TotalAngle=RealAngles.y
  158.             LastViewAngles=RealAngles
  159.             Wiggling=true
  160.         end
  161.         local p=(_0*_11-_1*_01)/(_1*_00-_0*_10)
  162.         local v=P:GetAbsVelocity():Length2D()
  163.         if p==p and v>0 then
  164.             local w=p/(2*v)
  165.             local RelativeAngle=(RealAngles.y-TotalAngle+180)%360-180
  166.             StrafeDirection=RelativeAngle>w*MaxAngleSeconds and 1 or RelativeAngle<-w*MaxAngleSeconds and -1 or StrafeDirection
  167.             local AngleIncrement=StrafeDirection*abs(w*FramePeriod)
  168.             TotalAngle=TotalAngle+AngleIncrement
  169.             local NewAngle=Angle(RealAngles.p,TotalAngle,RealAngles.r)
  170.             cmd:SetViewAngles(NewAngle)
  171.             if StrafeDirection>0 then
  172.                 cmd:SetButtons(bit.bor(bit.band(cmd:GetButtons(),bit.bnot(IN_MOVELEFT)),IN_MOVERIGHT))
  173.             else
  174.                 cmd:SetButtons(bit.bor(bit.band(cmd:GetButtons(),bit.bnot(IN_MOVERIGHT)),IN_MOVELEFT))
  175.             end
  176.             cmd:SetSideMove((StrafeDirection>0 and 1 or StrafeDirection<0 and -1 or 0)*-10000)
  177.             LastViewAngles=NewAngle
  178.         end
  179.     elseif Wiggling then
  180.         Wiggling=false
  181.         RealAngles=nil
  182.         TotalAngle=nil
  183.     end
  184.     LastTime2=CurrentTime
  185. end)
Advertisement
Add Comment
Please, Sign In to add comment