@name Mechy Fix
@inputs Pod:wirelink Fire 
@outputs Ran Door Props:array Refire GT:array Start
@persist Length1 Length2 FootL:entity FootR:entity E:entity N Radius Wobble X X2 Foot:array Active Height Yaw Shift Space Jump OnGround Alt
@persist Rot C1 C2 Mod G1:ranger G2:ranger GL:array 
rangerPersist(1)
if(first()|dupefinished()){
            Wobble=20
            E=entity():isWeldedTo()
            E:setMass(500)
            FootL=holoCreate(99,E:pos(),vec(1),E:angles())
            FootR=holoCreate(98,E:pos(),vec(1),E:angles())
            
            holoAng(98,E:toWorld(ang(180,90,90)))
            holoAng(99,E:toWorld(ang(180,90,90)))
            
            E:setAlpha(255)
            
            holoParent(99,E)
            holoParent(98,E)
                        
            Length1=55
            Length2=55
            function number cosine(A, B, C){
                return acos((A^2 + B^2 - C^2) / (2*A*B))
            }
            function ik(L1,L2,Hip:vector,End:vector,I1,I2){
                local Axis = entity():toLocalAxis(End - Hip)
                local Angle = Axis:toAngle():setRoll(-bearing(Hip, entity():angles(), End))
                local Dist = min(Axis:length(), L1 + L2)
                local Quat = quat(Angle) * qRotation(vec(0,90 + cosine(Dist, L1, L2),0))
                holoAng(I1,entity():toWorld(Quat:toAngle()))
                holoAng(I2, holoEntity(I1):toWorld(ang(cosine(L2, L1, Dist) + 180, 0, 0)))
            }
            #ik(L1,L2,Hip position,End Position,Hip entity index,Knee entity index)
            holoCreate(1,E:toWorld(vec(0,40,0)),vec(1))
            holoCreate(2,holoEntity(1):toWorld(vec(0,0,Length1-20)))
            
            holoModel(1,"hq_sphere")
            holoModel(2,"hq_sphere")
            holoScale(2,vec(1))
            
            holoParent(1,E)
            holoParent(2,1)
            
            holoCreate(3,E:toWorld(vec(0,-40,0)),vec(1))
            holoCreate(4,holoEntity(3):toWorld(vec(0,0,Length1-20)))
            
            holoModel(3,"hq_sphere")
            holoModel(4,"hq_sphere")
            holoScale(4,vec(1))
            holoColor(1,vec(),0)
            holoColor(3,vec(),0)
            
            holoParent(3,E)
            holoParent(4,3)
            
            holoCreate(5,holoEntity(1):pos()+holoEntity(1):up()*(Length1/2),vec(0.5,0.5,4),holoEntity(1):angles(),vec(255),"hq_cylinder")
            holoCreate(6,holoEntity(2):pos()+holoEntity(2):up()*(Length2/2),vec(0.5,0.5,4),holoEntity(2):angles(),vec(255),"hq_cylinder")

            holoParent(5,1)
            holoParent(6,2)
            
            holoCreate(7,holoEntity(3):pos()+holoEntity(3):up()*(Length1/2),vec(0.5,0.5,4),holoEntity(1):angles(),vec(255),"hq_cylinder")
            holoCreate(8,holoEntity(4):pos()+holoEntity(4):up()*(Length2/2),vec(0.5,0.5,4),holoEntity(2):angles(),vec(255),"hq_cylinder")     
    
            holoParent(7,3)
            holoParent(8,4)
            
            rangerFilter(E)
            Radius=40
            
            holoModel(98,"models/props_combine/breenconsole.mdl")
            holoModel(99,"models/props_combine/breenconsole.mdl")
            
            holoModel(2,"models/props_combine/combine_interface003.mdl")
            holoModel(4,"models/props_combine/combine_interface003.mdl")
            holoModel(5,"models/props_combine/combinetrain01a.mdl")
            holoModel(7,"models/props_combine/combinetrain01a.mdl")
            holoModel(6,"models/props_combine/combine_generator01.mdl")
            holoModel(8,"models/props_combine/combine_generator01.mdl")
            
            holoScale(98,vec(0.9,0.8,0.9))
            holoScale(99,vec(0.9,0.8,0.9))
            
            holoScale(2,vec(0.4,0.5,0.4))
            holoScale(4,vec(0.4,0.5,0.4))
            holoScale(5,vec(0.2,0.05,0.2))
            holoScale(7,vec(0.2,0.05,0.2))
            holoScale(6,vec(0.4,0.5,0.5))
            holoScale(8,vec(0.4,0.5,0.5))
            
            holoAng(5,holoEntity(5):toWorld(ang(180,0,0)))
            holoAng(7,holoEntity(7):toWorld(ang(180,0,0)))
            holoAng(6,E:toWorld(ang(0,90,0)))
            holoAng(8,E:toWorld(ang(0,90,0)))
            
    Size = 0.3

holoCreate(9)
holoModel(9,"models/gibs/manhack_gib02.mdl")
holoPos(9,entity():toWorld(vec(0,2,10)/Size))
holoAng(9,ang(0,-90,0))
holoScale(9,vec(2,2,2)/Size)
holoParent(9,E)

holoCreate(10)
holoModel(10,"models/gibs/manhack_gib03.mdl")
holoPos(10,entity():toWorld(vec(5,-1,8.5)/Size))
holoAng(10,ang(0,90,180))
holoScale(10,vec(2,2,2)/Size)
holoParent(10,9)

holoCreate(11)
holoModel(11,"models/gibs/scanner_gib01.mdl")
holoPos(11,entity():toWorld(vec(2,0,2)/Size))
holoAng(11,ang(0,90,0))
holoScale(11,vec(1,1,1)/Size)
holoParent(11,10)

holoCreate(12)
holoModel(12,"models/weapons/w_mach_m249para.mdl")
holoPos(12,entity():toWorld(vec(10,-6,-5)/Size))
holoAng(12,ang(0,90,0))
holoScale(12,vec(0.8,0.8,0.8)/Size)
holoParent(12,11)

holoCreate(13)
holoModel(13,"models/gibs/scanner_gib04.mdl")
holoPos(13,entity():toWorld(vec(10,5,2)/Size))
holoAng(13,ang(0,90,0))
holoScale(13,vec(1,1,1)/Size)
holoParent(13,12)

holoCreate(14)
holoModel(14,"models/gibs/scanner_gib02.mdl")
holoPos(14,entity():toWorld(vec(12,0,-1)/Size))
holoAng(14,ang(45,90,220))
holoScale(14,vec(1,1,1)/Size)
holoParent(14,13)
}

runOnTick(1)
        ik(Length1,Length2,holoEntity(1):pos(),FootL:pos(),1,2)
        ik(Length1,Length2,holoEntity(3):pos(),FootR:pos(),3,4)
  

Vel=E:velL():y()/8

    Seat = Pod["Entity",entity]
    Driver = Seat:driver()
    W = Driver:keyForward()
    A = Driver:keyLeft()
    S = Driver:keyBack()
    D = Driver:keyRight()
    Alt = Driver:keyWalk()
    Space = Driver:keyJump()
    Mouse1 = Driver:keyAttack1()
    Mouse2 = Driver:keyAttack2()
    Shift = Driver:keySprint()
    Active = Driver:isPlayer()

Fade=!Active

Mod=clamp(Mod,250,1000) 
if(Active==1){
    G1=rangerOffset(50000,holoEntity(65):pos(),holoEntity(65):forward())
    G2=rangerOffset(50000,holoEntity(66):pos(),holoEntity(66):forward())
    Door+=1
    holoVisible(53,Pod["Entity",entity]:driver(),0)
    if(Alt|Space){Height=50}
    if(!Alt&!Space){Height=100}
    if(OnGround){Radius=50}
    else{Radius=0}
    if(Space){Mod+=4}
    if($Space&!Space&OnGround){Jump=Mod
        soundPlay(-3,1,"mvm/giant_pyro/giant_pyro_step_04.wav")}
    if(OnGround==0){Jump=0
        Mod=0}
}
if(Active==0){Height=40
    holoAng(55,E:toWorld(ang(90,0,0)))
    holoAng(53,E:toWorld(ang(Pod["Entity",entity]:driver():eyeAngles():pitch(),0,0)))
    holoAng(65,E:toWorld(ang(Pod["Entity",entity]:driver():eyeAngles():pitch(),0,0)))
    holoAng(66,E:toWorld(ang(Pod["Entity",entity]:driver():eyeAngles():pitch(),0,0)))

    Door-=1
    holoVisible(53,players(),1)
    Radius=1
    }
X2=clamp(X2,-3,3)
if(A){Yaw=2}
if(D){Yaw=-2}
if(!A&!D){Yaw=0}
Door=clamp(Door,-215,-90)
holoAng(61,holoEntity(55):toWorld(ang(Door,0,0)))


if($Active&Active){soundPlay(-1,5,"ambient/machines/thumper_startup1.wav")
    timer("ac",4100)
    soundPitch(-1,70)
} 
if($Active&!Active){soundPlay(-1,8,"ambient/machines/thumper_shutdown1.wav")
    soundPitch(-1,70)
} 

if(clk("ac")){soundPlay(-6,3,"hl1/fvox/activated.wav")
    soundPitch(-6,75)
}

##for/back
rangerFilter(E)
RR = rangerOffset(E:toWorld(vec(0,-20,0)),E:toWorld(vec(cos(N-180)*Radius,-20,-105+max(sin(N-180)*Radius*1.5*(W-S),0))))

rangerFilter(E)
RL = rangerOffset(E:toWorld(vec(0,20,0)),E:toWorld(vec(cos(N)*Radius,20,-105+max(sin(N)*Radius*1.5*(W-S),0))))

holoPos(99,RL:pos()+vec(0,0,10))
holoPos(98,RR:pos()+vec(0,0,10))

if((W|S)&OnGround==1){
    if(X){X2-=0.18*(Shift+1)}
    if(!X){X2+=0.18*(Shift+1)}
    if(W){N+=2*(Shift+1)*((S-W)*2)}
    if(S){N+=2*(Shift+1)*((W-S)*2)}

     if((changed(RR:hit())&RR:hit())|(changed(RL:hit())&RL:hit())){
        Ran=int(random(1,9))
        soundStop(1)
        soundPlay(1,0.4,Foot[Ran,string])
        X=!X
        soundVolume(1,1)
    }  

}
else{X2=0}
 
rangerFilter(E) 
R = rangerOffset(E:pos(),E:pos()-vec(0,0,120))
if(R:hit()){OnGround=1
Diff = Height+sin(N*2)*(Wobble*(Shift+1))-R:distance()

E:applyForce(vec(0,0,Diff)*2*E:mass()-(E:vel())*E:mass()+E:forward()*(W-S)*200/(Alt+1)/(Space+1)*(Shift+1)*E:mass()+E:up()*Jump*E:mass())

}
else{OnGround=0}
function entity:turnTo(Tarang:angle)
{
    local V = This:toLocal(rotationVector(quat(Tarang)/quat(This))+This:pos())
    This:applyTorque((1000*V - 30*This:angVelVector())*This:inertia()/3)
}
E:turnTo(E:angles():setPitch(0):setRoll(X2):setYaw(E:angles():yaw()+Yaw))

Foot[1,string]="mvm/giant_heavy/giant_heavy_step01.wav"
Foot[2,string]="mvm/giant_heavy/giant_heavy_step02.wav"
Foot[3,string]="mvm/giant_heavy/giant_heavy_step03.wav"
Foot[4,string]="mvm/giant_heavy/giant_heavy_step04.wav"
Foot[5,string]="mvm/giant_soldier/giant_soldier_step01.wav"
Foot[6,string]="mvm/giant_soldier/giant_soldier_step02.wav"
Foot[7,string]="mvm/giant_soldier/giant_soldier_step03.wav"
Foot[8,string]="mvm/giant_soldier/giant_soldier_step04.wav"

Props[1,entity]=entity():isWeldedTo()
