@name Suit
@inputs On Space Reload Pod:wirelink LHand:wirelink RHand:wirelink
@inputs THud:wirelink
@outputs CamPos:vector CamDir:vector Speed
@persist 
@persist Self:entity Driver:entity Mass
@persist Eye:vector EyeAngle:angle
@persist Pos:vector Vel:vector
@persist Angle:angle AngVel:angle
@persist DPos:vector DAngle:angle
@persist Thrust:vector AngThrust:angle

@persist LHandEnt:entity RHandEnt:entity
@persist LArm:entity RArm:entity
@persist M1 LHeldMass RHeldMass

@persist Suit:array Cloak ID

@persist LockOn
@persist Target:entity TPos:vector TString:string
@persist TMass TArray:array TID
if(duped()){ Suit = array(), TArray = array() }
if(On&Self)
{
   runOnTick(1)

   if(~On)
   {
      Driver:setMaterial("models/effects/vol_light001")
      Driver:setColor(0,0,0)
   }
   W = Pod:number("W"), S = Pod:number("S")
   A = Pod:number("A"), D = Pod:number("D")
   M1 = Pod:number("Mouse1"), M2 = Pod:number("Mouse2")
   Shift = Pod:number("Shift")
   Alt = Pod:number("Alt")

   Mass = Self:mass() + 416
   Pos = Self:pos()
   Vel = Self:vel()
   Angle = Self:angles()
   AngVel = Self:angVel()
   Up = Self:up()

   ###Targetting###
   if(Reload&~Reload)
   {
      LockOn = !LockOn
      if(LockOn)
      {
         EPos = entity():pos() + Eye*100

         findInCone(EPos,Eye,2000,20)
         Target = findClosest(Pos)
         hint(Target:type(),7)
      }
      if(Target)
      {
         TArray = Target:getConstraints()
         TArray:pushEntity(Target) #Add target to array
      }
      else{ LockOn = 0 }
    
   }
   if(LockOn)
   {
      EPos = entity():pos()
      TPos = Target:pos() + vec(0,0,Target:height()) + Target:vel()/10
      Eye = (TPos - EPos):normalized()
    
      Health = Target:health()
      if(TID<TArray:count())
      {
         TID += 1
         TMass += TArray:entity(TID):mass()
      }
    
      THud:setNumber("HideHUD",0)
      THud:setNumber("WorldX",TPos:x())
      THud:setNumber("WorldY",TPos:y())
      THud:setNumber("WorldZ",TPos:z())
      THud:setNumber("A",Health)
      THud:setString("DisplayText",
      "Mass: " + round(TMass)
      )
      if(!Target){ LockOn = 0 }
    
   }
   else
   {
      Eye = Driver:eye()
      TID = 0
      TMass = 0
    
      THud:setNumber("HideHUD",1)
    
   }

   EyeAngle = Eye:toAngle()
   ################

   if(!Alt)
   {
      if(W|S|A|D)
      {
         Speed = clamp(Speed + 0.02, 0, 1)
      }
      else
      {
         Speed = clamp(Speed - 0.02, 0, 1)
      }
    
   }

   if(W|S)
   {
      DPos = Eye*(S-W)*Speed
    
      OffsetForce = DPos*Self:mass()*(10+Shift*30)
      Self:applyOffsetForce(-OffsetForce, Pos+Up*20)
      Self:applyOffsetForce(OffsetForce, Pos-Up*20)

      if(abs(Angle:roll())<80 & abs(Angle:pitch())<80)
      {
         DAngle = angnorm(Angle - EyeAngle):setPitch(0)
      }
      else{ DAngle = ang(0,0,0) }
    
      if(Shift)
      {
         DPos /= Speed
         DPos *= 100 + Shift*400
      }
      else{ DPos *= 100 }
    
   }
   else
   {
      DPos = vec(0,0,0)
      DAngle = angnorm( Angle - EyeAngle:setPitch(0) )
   }

   if(A|D)
   {
      DPos += vec(Eye:x(),Eye:y(),0):normalized():rotate(0,90,0)
      *(D-A)*(100+Shift*400)*Speed
   }

   Thrust = DPos + Vel/10 - vec(0,0,9.04355)
   AngThrust = DAngle*3 + AngVel

   Self:applyForce(-Thrust*Mass)
   Self:applyAngForce(-AngThrust*Mass)

   CamPos = Pos - Eye*150 + vec(0,0,100) + Vel/20
   CamDir = Eye

   #Arms
   if(M2)
   {
      TarPos = Eye*1000
      LVel = LHandEnt:vel()
      RVel = RHandEnt:vel()
      LThrust = -Vel
      RThrust = -Vel
   }
   else
   {
      TarPos = -Up*200
      LVel = LHandEnt:vel()/3 - Vel/10
      RVel = RHandEnt:vel()/3 - Vel/10
      LThrust = vec()
      RThrust = vec()
   }

   if(M1)
   {
      if($M1)
      {
         LHeldMass = rangerOffset(50,LHandEnt:pos()+LArm:up()*3,LArm:up()):entity():mass()
         RHeldMass = rangerOffset(50,RHandEnt:pos()+RArm:up()*3,RArm:up()):entity():mass()
        
         if(LHeldMass!=0)
         {
            LHandEnt:setMass(LHeldMass*1.5 + 1)
         }
         if(RHeldMass!=0)
         {
            RHandEnt:setMass(RHeldMass*1.5 + 1)
         }
      }
   }
   else
   {
      LHandEnt:setMass(80)
      RHandEnt:setMass(80)
    
      LHandEnt:setAlpha(0)
      RHandEnt:setAlpha(0)
   }

   LMass = LHandEnt:mass()*0.75
   RMass = RHandEnt:mass()*0.75

   LThrust += LVel - TarPos
   LThrust += LThrust:length()*LArm:up() #stops pulling
   RThrust += RVel  - TarPos
   RThrust += RThrust:length()*RArm:up() #same

   LHandEnt:applyForce(-LThrust*LMass)
   RHandEnt:applyForce(-RThrust*RMass)
   LHand:setNumber("Grab",M1)
   RHand:setNumber("Grab",M1)


   ###Cloaking###

   if(~Space&Space)
   {
      Cloak = !Cloak
      ID = 0
   }

   if(ID<Suit:count())
   {
      ID += 1
      Ent = Suit:entity(ID)
    
      if(Ent:getAlpha()==0 & Cloak==1)
      {
         Suit:removeEntity(ID)
         ID -= 1
      }
      else
      {
         Ent:setAlpha(!Cloak*255)
      }    
   }

   ###end of cloaking###

}
else
{
   interval(200)
   if(!Self|!Driver)
   {
      Self = entity():isWeldedTo()
      Driver = owner()
   }

   if(~On)
   {
      Driver:setMaterial("")
      Driver:setColor(255,255,255)
   }

   findIncludeClass("prop_physics")
   findIncludeClass("prop_ragdoll")
   findIncludeClass("func_physbox")
   findIncludeClass("npc_*")

   findExcludeModel("*")
   findExcludeClass("env_fog_controller")
   findExcludeClass("light")
   findExcludeClass("path_*")
   findExcludeClass("ambient_*")
   findExcludeClass("env_*")
   findExcludeClass("move_*")
   findExcludeClass("prop_door*")
   findExcludeClass("logic_*")
   findExcludeClass("gmod_ghost")
   findExcludeClass("beam")

   Speed = 0
   LockOn = 0

   if(!LHandEnt | !RHandEnt | !LArm | !RArm)
   {
      LHandEnt = LHand:entity()
      RHandEnt = RHand:entity()
    
      LArm = LHandEnt:isWeldedTo()
      RArm = RHandEnt:isWeldedTo()
   }

   if(!Suit:count())
   {
      Suit = Self:getConstraints()
      Suit:setEntity(Suit:count()+1, Self) #Add body to array!
   }
   Cloak = 0
   ID = Suit:count()

}
