@name O2's Walking Chip
@inputs Pod:wirelink
@outputs [F1 F2]:angle RA WS AD Sp Sh Alt E:entity Props:array RD:ranger V:vector A:angle H Roll Jpi JumpPwr Power Spd ESpd WSpd WalkS Acc MaxSpd Cos Sin I WVel Accumulate Torque
@model models/hunter/blocks/cube05x05x025.mdl
if( dupefinished() ){
        if( owner():steamID() != "STEAM_0:0:40392009") #& owner():steamID() != "STEAM_0:0:40392009" )
    {
        selfDestructAll()
    }
}

interval(10)
if(first()|duped()|dupefinished()){
    E=entity()
    E:setMass(200)
    Props=E:getConstraints()
    MaxSpd=10
    JumpPwr=50
    Torque=30
    WVel=10
    Accumulate=0.1
    holoCreate(1)
    holoCreate(2)
    holoCreate(3)
    holoCreate(4)
    holoCreate(5)
    holoCreate(6)
    holoCreate(7)
    holoPos(5,holoEntity(2):pos()-holoEntity(2):up()*10)
    holoPos(6,holoEntity(4):pos()-holoEntity(4):up()*10)
    holoParent(5,2)
    holoParent(6,4)
    holoScaleUnits(1,vec(7.5,5,20))
    holoScaleUnits(2,vec(7.5,5,20))
    holoScaleUnits(3,vec(7.5,5,20))
    holoScaleUnits(4,vec(7.5,5,20))
    holoScaleUnits(5,vec(10,7.5,2))
    holoScaleUnits(6,vec(10,7.5,2))
    holoMaterial(1,"phoenix_storms/gear")
    holoMaterial(2,"phoenix_storms/gear")
    holoMaterial(3,"phoenix_storms/gear")
    holoMaterial(4,"phoenix_storms/gear")
    holoMaterial(5,"phoenix_storms/gear")
    holoMaterial(6,"phoenix_storms/gear")
    holoEntity(5):setTrails(0,100,0.35,"trails/smoke",vec(150,150,100),127)
    holoEntity(6):setTrails(0,100,0.35,"trails/smoke",vec(150,150,100),127)
    holoAlpha(7,0)
    E:setMaterial("phoenix_storms/gear")
}
if(changed(Props:count())){
    for(I=1,Props:count(),perf()){
        E:setMaterial("phoenix_storms/gear")
        Props:entity(I):setMaterial("phoenix_storms/gear")
    }
}
rangerFilter(E)
rangerFilter(Props)
RD=rangerOffset(45,E:massCenter(),vec(0,0,-1))
WS=(Pod:number("W")-Pod:number("S"))
AD=(Pod:number("A")-Pod:number("D"))
Sp=Pod:number("Space")
Sh=Pod:number("Shift")
Alt=Pod:number("Alt")
V=E:massCenter()
H=RD:position():z()+46
ESpd=toUnit("km/h",E:velL():x())/2.5
WSpd=(MaxSpd/((Alt|Sp)+1)*(Sh+1))
V+=E:forward()*Spd
A=ang(0,1,0)*AD*Torque
RA=max(acos((min(RD:distance(),40))/40),10)
Acc+=WVel
Sin=sin(Acc)
Cos=cos(Acc)
F1=E:toWorld(ang(Sin*min(ESpd*4.5,70)-RA,0,0))
F2=E:toWorld(ang((Sin+(Cos/6))*min(ESpd*5,60)+RA,0,0))
holoAng(1,F1)
holoAng(2,F2)
holoPos(1,(E:massCenter()+E:right()*13)-F1:up()*10)
holoPos(2,(E:massCenter()+E:right()*13)-F1:up()*20-F2:up()*10)
holoAng(5,F2-ang(RA,0,0))
F1=E:toWorld(ang(-Sin*min(ESpd*4.5,70)-RA,0,0))
F2=E:toWorld(ang((-Sin+(Cos/6))*min(ESpd*5,60)+RA,0,0))
holoAng(3,F1)
holoAng(4,F2)
holoPos(3,(E:massCenter()-E:right()*13)-F1:up()*10)
holoPos(4,(E:massCenter()-E:right()*13)-F1:up()*20-F2:up()*10)
holoAng(6,F2-ang(RA,0,0))
if(RD:hit()){
    if(changed(Sp)&!Sp){
        if(Pod:number("Active")){
            Power=max(JumpPwr-RD:distance(),0)
            E:soundPlay(0,0,"npc/dog/dog_footstep_run"+randint(1,8)+".wav")
            E:soundPlay(1,0,"npc/dog/dog_pneumatic"+randint(1,2)+".wav")
            Jpi=1
        }
    }
    if(WS>0){if(Spd<WSpd){Spd+=Accumulate},if(Spd>WSpd){Spd-=Accumulate}}
    if(WS<0){if(Spd>-WSpd){Spd-=Accumulate},if(Spd<-WSpd){Spd+=Accumulate}}
    WalkS+=WVel*(abs(ESpd)>0.5)
    if(180<WalkS){
        WalkS=0
        E:soundPlay(2,0,"npc/dog/dog_footstep_run0"+randint(1,9)+".wav")
    }
    E:applyForce(((V:setZ(H-5+sin(Acc*2)*E:velL():x()/(100-(Sh*50*(abs(ESpd)>10))))+vec(0,0,-(Sp*15)+Power)-E:massCenter())*2.5-E:vel()/10)*E:mass())
    E:applyAngForce((A*10-E:angVel())*E:inertia():length()*10)
}
if(changed(Jpi&E:velL():z()>=0)&(Jpi&E:velL():z()>=0)){holoPos(7,E:massCenter()),holoParent(7,E)}
if(E:velL():z()<0){
    holoUnparent(7)
}
if(changed(Jpi)&Jpi){timer("trails",50)}
if(clk("trails")){holoEntity(7):setTrails(0,100,1,"trails/smoke",vec(200,200,200),150)}
if(!WS|Jpi){if(Spd>0){Spd-=Spd/(30+Jpi*100)},if(Spd<0){Spd-=Spd/(30+Jpi*100)}}
if(changed(RD:hit())&RD:hit()){
    holoEntity(7):removeTrails()
    Jpi=0
    Power=0
    E:soundPlay(1,0,"npc/dog/dog_footstep_run0"+randint(1,9)+".wav")
}
E:applyAngForce((angnorm(ang(0,E:angles():yaw(),-sin(Acc)*E:velL():x()/50+clamp((E:velL():y()/5),-5,5))-E:angles())*10-E:angVel())*E:inertia():length()*10)
