@name [Lyeek] Mech
@inputs [LeftF RightF Body]:entity W A S D StrafeL StrafeR 
@outputs Diff
#vectors
@persist [Feft Right SollPos BodyPos]:vector MechEnts:array L R
#Settings
@persist FeetSPosDist RangerRange BodyH BodyVel FeetVel FetMovSped FTForward FTMaxH FTMinH 
@persist RangerFL RangerFR RangerBL RangerBR RangerHitR RangerHitL
#stuff
@persist DistR DistL Step:string 

interval(25)

if(first()|dupefinished()){
    MechEnts = entity():getConstraints()
    Step = "Links_Unten"
    rangerFilter(MechEnts)    
    rangerPersist(1)

}
    Diff = abs((LeftF:pos():z() - RightF:pos():z())/2)/1.5  #stops the Base from going up and down while moving // change the 1.5 to adjust it

    #[ --- Variables --- ]#
    #[ --- Settings --- ]#  
    W = owner():keyWalk()      
    BodyH           = 77 -Diff      #BodyHeight    
    RangerRange     = 5         #When it hits, it will set the next SollPos! #Depends on feetsize 
    FeetSPosDist    = 3         #Feet sollpos distance wenns weiterschalten soll 
    BodyVel         = 4         #Minus Velocity for the Body  
    FeetVel         = 4         #Minus Velocity for the Feets
    FeetMovSpeed    = 5         #Feet move speed
    FTFDist         = 22        #Feet to feet distance
    FTForward       = 60        #how far the feet shall go to walk 
    FTMaxH          = 50        #Feet max height #SollPos -"_Oben"
    FTMinH          = 77        #Feet Minimum height   #SollPos -"_unten"
                  
    Body    = entity():isWeldedTo()
    Left    = LeftF:massCenter()
    Right   = RightF:massCenter()   
    
    BodyPos = ( Left + Right ) / 2 +Body:up()*BodyH
    Body:applyForce((BodyPos - Body:massCenter())*Body:mass()*10)     #Moving the base above the feet
    LeftPos   =   ( Body:massCenter() + Body:right()*-FTFDist + Body:up()*-BodyH )   
    RightPos  =   ( Body:massCenter() + Body:right()*FTFDist  + Body:up()*-BodyH ) 

        #[ --- Minus Velocity --- ]#
Body:applyForce(    -Body:vel() * Body:mass()     / BodyVel )
LeftF:applyForce(   -LeftF:vel() * LeftF:mass()   / FeetVel )  
RightF:applyForce(  -RightF:vel() * RightF:mass() / FeetVel ) 

        #[ --- Rangers Feet Front --- ]#
RangerFL = rangerOffset( 100000000, Left +LeftF:forward()*15 , -LeftF:up()):distance()
RangerFR = rangerOffset( 100000000, Right +RightF:forward()*15, -RightF:up()):distance()
        #[ --- Rangers Feet Back --- ]#

RangerBL = rangerOffset( 100000000, Left +LeftF:forward()*-15, -LeftF:up()):distance()
RangerBR = rangerOffset( 100000000, Right +RightF:forward()*-15, -RightF:up()):distance()
      

if( RangerFL < RangerRange | RangerFR < RangerRange    | RangerBL < RangerRange | RangerBR < RangerRange){
    
    RangerHitL = (RangerFL < RangerRange | RangerBL < RangerRange )
    RangerHitR = (RangerFR < RangerRange | RangerBR < RangerRange )

    if(changed(RangerHitL)&RangerHitL){
        LeftF:soundPlay("footstepL", 0.3, "npc/dog/dog_footstep" + randint(1, 4) + ".wav")
        soundVolume(1,0.3)        
    }
    if(changed(RangerHitR)&RangerHitR){
        RightF:soundPlay("footstepL", 0.3, "npc/dog/dog_footstep" + randint(1, 4) + ".wav")
        soundVolume(1,0.3)
    }

        #[ --- Positioning / Moving --- ]#
        #[ --- Steps    FORWARD  --- ]# 
        if( W | S ){        
                           
            if( Step == "Links_Unten" ){
                SollPos =  Body:massCenter() + Body:right()*-FTFDist + Body:up()*-FTMinH + Body:forward()*-FTForward * (W-S)
                if(Left:distance(SollPos) < FeetSPosDist | RangerHitL ){
                    Step = "Rechts_Oben"
                    #exit()
                }
            }
            if( Step == "Rechts_Oben" ){
                SollPos =  Body:massCenter() + Body:right()*FTFDist + Body:up()*-FTMaxH  + Body:forward()*10 * (S-W)
                if(Right:distance( SollPos ) < FeetSPosDist + 3){
                    Step = "Rechts_Unten"
                    #exit()        
                }     
            }
          
            if( Step == "Rechts_Unten" ){
                SollPos =  Body:massCenter() + Body:right()*FTFDist + Body:up()*-FTMinH + Body:forward()*-FTForward * (W-S)
                if(Right:distance(SollPos) < FeetSPosDist | RangerHitR){
                    Step = "Links_Oben" 
                    #exit()       
                }  
            }
            if( Step == "Links_Oben" ){
                SollPos =  Body:massCenter() + Body:right()*-FTFDist + Body:up()*-FTMaxH  + Body:forward()*10 * (S-W) 
                if(Left:distance( SollPos ) < FeetSPosDist + 3){
                    Step = "Links_Unten"
                    #exit()
                }
            }  
                #[ --- Feet Move  --- ]#
      
    }    
       #[ --- Steps    left and Right Strafe  --- ]#
        if((A | D) & (!W | S)){
          
           if( Step == "Links_Unten" ){
                SollPos =  Body:massCenter() + Body:right()*-FTFDist + Body:up()*-FTMinH 
                if(Left:distance(SollPos) < FeetSPosDist | RangerHitL){
                    Step = "Rechts_Oben"
                    #exit()
                }
            }
                if( Step == "Rechts_Oben" ){
                SollPos =  Body:massCenter() + Body:right()*FTFDist   + Body:up()*-FTMaxH  
                if(Right:distance( SollPos ) < FeetSPosDist ){
                    Step = "Rechts_Unten"
                    #exit()        
                }     
            }
          
            if( Step == "Rechts_Unten" ){
                SollPos =  Body:massCenter() + Body:right()*FTFDist  + Body:up()*-FTMinH 
                if(Right:distance(SollPos) < FeetSPosDist | RangerHitR){
                    Step = "Links_Oben" 
                    #exit()       
                }  
            }
            if( Step == "Links_Oben" ){
                SollPos =  Body:massCenter() + Body:right()*-FTFDist + Body:up()*-FTMaxH    
                if(Left:distance( SollPos ) < FeetSPosDist ){
                    Step = "Links_Unten"
                    #exit()
                }
            }  
                     
    }
                if(W | A | S | D){
              
                if(Step:find( "Rechts_" ) ){

                    RightF:applyForce((  SollPos  - Right)*RightF:mass()*FeetMovSpeed)
                }
                if(Step:find( "Links_" ) ){
                    LeftF:applyForce((  SollPos  - Left)*LeftF:mass()*FeetMovSpeed)
                } 
                if(A|D){
                    Body:applyAngForce(ang(0,(A-D)*(W+1),0)*Body:mass()*25)                
                }      
            }        
}else{
        RangerHitR = 0
        RangerHitL = 0 
}
    
if(Step:find( "Links_" ) & (!RangerHitR | Right:distance(SollPos) < FeetSPosDist ) ){
    RightF:applyForce(   -Body:up()*RightF:mass()*200 )  
}               
if(Step:find( "Rechts_" )& ( !RangerHitL | Left:distance(SollPos) < FeetSPosDist ) ){ 
    LeftF:applyForce(   -Body:up()*LeftF:mass()*200 )
}
