@name UltraMech V2-5 - By Naografix
@inputs 
@outputs [Activate Hauteur Stab OnGround IncMouv Yaw Roll]:number Diff

###---MECH
@persist [Ent]:entity [Activate Hauteur Stab OnGround IncMouv Yaw Roll]:number

###---FOOT
@persist Foot:array Radius

###---SEAT
@persist [Seat Driver]:entity [WhiteList]:array [ItsOK KillCount CptWhite]:number

@persist HoloIndex [FootLeft FootRight]:entity

@trigger 
@model models/hunter/plates/plate1x1.mdl


if(first()){
    
    Ent = entity()
    
    function void createHolo(ModelHolo:string,PosHolo:vector,AngHolo:angle,ParentHolo:entity,ScaleHolo:vector,HoloAlpha:number){
           
        holoCreate(HoloIndex)
        holoModel(HoloIndex,ModelHolo)
        holoPos(HoloIndex,entity():toWorld(PosHolo))
        holoAng(HoloIndex,entity():toWorld(AngHolo))
        holoScale(HoloIndex,ScaleHolo)
        holoParent(HoloIndex,ParentHolo)
        holoAlpha(HoloIndex,HoloAlpha)
           
        HoloIndex++
    }
    
##FOOT LEFT
    createHolo("models/props_combine/combine_booth_short01a.mdl",vec(),ang(-90,180,0),Ent,vec(0.25),255)
    FootLeft = holoEntity(0)
    createHolo("models/props_combine/combine_generator01.mdl",vec(30,-5,5),ang(0,90,100),FootLeft,vec(0.22),255)
    createHolo("models/props_combine/combine_generator01.mdl",vec(30,11,5),ang(0,90,100),FootLeft,vec(0.22),255)
    createHolo("models/props_combine/combine_generator01.mdl",vec(-10,3,5),ang(0,90,80),FootLeft,vec(0.28),255)
    
    createHolo("",vec(0,0,9),ang(),FootLeft,vec(1,2,1),255)
    holoColor(HoloIndex-1,vec(100))
    holoMaterial(HoloIndex-1,"phoenix_storms/metalset_1-2")
    
    
##FOOT RIGHT
    createHolo("models/props_combine/combine_booth_short01a.mdl",vec(),ang(-90,180,0),Ent,vec(0.25),255)
    FootRight = holoEntity(5)
    createHolo("models/props_combine/combine_generator01.mdl",vec(30,-5,5),ang(0,90,100),FootRight,vec(0.22),255)
    createHolo("models/props_combine/combine_generator01.mdl",vec(30,11,5),ang(0,90,100),FootRight,vec(0.22),255)
    createHolo("models/props_combine/combine_generator01.mdl",vec(-10,3,5),ang(0,90,80),FootRight,vec(0.28),255)
    
    createHolo("",vec(0,0,9),ang(),FootRight,vec(1,2,1),255)
    holoColor(HoloIndex-1,vec(100))
    holoMaterial(HoloIndex-1,"phoenix_storms/metalset_1-2")
    
    
    
##LEG LEFT

    createHolo("",vec(0,60,-40),ang(),Ent,vec(1),255)
    createHolo("models/props_combine/combinethumper002.mdl",vec(0,55,-40),ang(0,90,0),holoEntity(10),vec(0.5,0.4,0.35),255)

    createHolo("models/Combine_Helicopter/helicopter_bomb01.mdl",vec(17,60,40),ang(0,90,0),holoEntity(11),vec(0.4),255)
    holoColor(HoloIndex-1,vec(180))
    holoMaterial(HoloIndex-1,"phoenix_storms/metalfloor_2-3")


     createHolo("models/props_combine/combinethumper002.mdl",vec(10,60,140),ang(180,180,0),holoEntity(12),vec(0.6,0.4,0.4),255)
    
    
    
    
##LEG RIGHT

    createHolo("",vec(0,-60,-40),ang(),Ent,vec(1),255)
    createHolo("models/props_combine/combinethumper002.mdl",vec(0,-65,-40),ang(0,90,0),holoEntity(14),vec(0.5,0.4,0.35),255)

    createHolo("models/Combine_Helicopter/helicopter_bomb01.mdl",vec(17,-60,40),ang(0,90,0),holoEntity(15),vec(0.4),255)
    holoColor(HoloIndex-1,vec(180))
    holoMaterial(HoloIndex-1,"phoenix_storms/metalfloor_2-3")


     createHolo("models/props_combine/combinethumper002.mdl",vec(10,-60,140),ang(180,180,0),holoEntity(16),vec(0.6,0.4,0.4),255)

    
######---------------------------------------------------------------------------------------------------------##--ENTITY SEAT
    function entity getSeat(){
           
        findIncludePlayerProps(owner())
        findByClass("prop_vehicle_prisoner_pod")
        findByModel("models/nova/jeep_seat.mdl")
           
        local Chrs = findToArray()
           
        for(I = 0,Chrs:count()){
            if(Chrs[I,entity]:isVehicle() && Chrs[I,entity]:getAlpha() > 200){
                return Chrs[I,entity]
            }  
        }
           
        return noentity()
    }
    
    printColor(vec(255,0,0),"Seat : NOT FINDED")
    printColor(vec(),"-----------------------")
    timer("SEAT",50)
######---------------------------------------------------------------------------------------------------------##--END ENTITY SEAT    

######---------------------------------------------------------------------------------------------------------##--STAB
    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)
    }
######---------------------------------------------------------------------------------------------------------##--END STAB


    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,80 - cosine(Dist, L1, L2),0))
        holoAng(I1,entity():toWorld(Quat:toAngle()))
        holoAng(I2, holoEntity(I1):toWorld(-ang(cosine(L2, L1, Dist) + 190, 0, 0)))
    }


######---------------------------------------------------------------------------------------------------------##--OTHER INIT  
    
    Stab = 20
    
    Ent:setMass(500)
    Radius = 40
    
    rangerFilter(Ent)
    rangerIgnoreWorld(0)
    rangerPersist(1)
    
    ##OTHER
    WhiteList:pushString("STEAM_0:0:52197292")
    
    
    Foot:pushString("mvm/giant_heavy/giant_heavy_step01.wav")
    Foot:pushString("mvm/giant_heavy/giant_heavy_step02.wav")
    Foot:pushString("mvm/giant_heavy/giant_heavy_step03.wav")
    Foot:pushString("mvm/giant_heavy/giant_heavy_step04.wav")
    Foot:pushString("mvm/giant_soldier/giant_soldier_step01.wav")
    Foot:pushString("mvm/giant_soldier/giant_soldier_step02.wav")
    Foot:pushString("mvm/giant_soldier/giant_soldier_step03.wav")
    Foot:pushString("mvm/giant_soldier/giant_soldier_step04.wav")
######---------------------------------------------------------------------------------------------------------##--END OTHER INIT          
                
    
}

######---------------------------------------------------------------------------------------------------------##--SEARCH SEAT
if(!Seat){
    if(clk("SEAT")){
        Seat = getSeat()
        Ent:setAlpha(0)
            
        if(Seat != noentity()){
            printColor(vec(0,255,0),"Seat : FINDED")
            printColor(vec(),"-----------------------")
        }
    
        timer("SEAT",50)
    }
}
if(changed(Seat) & Seat){timer("FIX",200) timer("PLAYER",200)}
######---------------------------------------------------------------------------------------------------------##--END SEARCH SEAT

######---------------------------------------------------------------------------------------------------------##--FIX SEAT
if(clk("FIX")){
    Seat:propFreeze(1)
    Seat:setPos(Ent:toWorld(vec(0,0,10)))
    Seat:setAng(Ent:toWorld(ang(0,-90,0)))
    timer("FIX",1)
}
######---------------------------------------------------------------------------------------------------------##--END FIX SEAT

######---------------------------------------------------------------------------------------------------------##--CHECK PLAYER
if(clk("PLAYER") && !ItsOK){

    Driver = Seat:driver()
      
    if(Driver == noentity()){
        ItsOK = 0
        CptWhite = 0
    } 
    
    if(Driver != noentity()){
        
        while (CptWhite <= WhiteList:count() && !ItsOK){
            if((Driver:steamID() == WhiteList[CptWhite,string]) || Driver == owner()){
                Activate = 1
                ItsOK = 1
            }
            CptWhite++
        }
    
        if(CptWhite == WhiteList:count()+1 && !ItsOK){    
            Seat:killPod()
            Seat:soundPlay(20,0,"weapons/enforcer_shoot.wav")
            KillCount++
        }
    }
    timer("PLAYER",200)
}
######---------------------------------------------------------------------------------------------------------##--END CHECK PLAYER


if(Activate){
    
    if(ItsOK){
        
        runOnTick(1)

######---------------------------------------------------------------------------------------------------------##--POD    
        
        if(Driver == noentity()){
            ItsOK = 0
            Activate = 0
            timer("PLAYER",200)
        }
        
        W = Driver:keyForward()
        A = Driver:keyLeft()
        S = Driver:keyBack()
        D = Driver:keyRight()
        R = Driver:keyReload()
        Shift = Driver:keySprint()
        Alt = Driver:keyWalk()
        Space = Driver:keyJump()
        Mouse1 = Driver:keyAttack1()
        Mouse2 = Driver:keyAttack2()
        Driver = Seat:driver()

######---------------------------------------------------------------------------------------------------------##--END POD   
        
        Roll = clamp(Roll,-3,3)
        
        if((W | S) & OnGround){
            if(W){IncMouv += 1.2 * (Shift+1) * ((S-W)*2)}
            if(S){IncMouv += 1.2 * (Shift+1) * ((W-S)*2)}
        }
        else{Roll = 0}
         
        if(A){Yaw = 2}
        if(D){Yaw = -2}
        if(!A & !D){Yaw = 0}
    
        if(OnGround){Radius = 120}
        else{
            Jump = 0
            Radius = 0
        }
                
        if(!Alt & !Space){Hauteur = 220}         
        
        
        RR = rangerOffset(Ent:toWorld(vec(0,-60,0)),Ent:toWorld(vec(cos(IncMouv-180)*Radius,-60,-Hauteur+max(sin(IncMouv-180)*Radius*0.5*(W-S),0))))
        RL = rangerOffset(Ent:toWorld(vec(0,60,0)),Ent:toWorld(vec(cos(IncMouv)*Radius,60,-Hauteur+max(sin(IncMouv)*Radius*0.5*(W-S),0))))

        holoPos(holoIndex(FootLeft),RL:pos())
        holoPos(holoIndex(FootRight),RR:pos())
        
        if((changed(RR:hit())&RR:hit())|(changed(RL:hit())&RL:hit())){
            soundPlay(1,0.4,Foot[random(1,6),string])
            soundPitch(1,145)
        }
        
     
######---------------------------------------------------------------------------------------------------------##--MOUV   

        Ranger = rangerOffset(Ent:pos(),Ent:pos()-vec(0,0,Hauteur+20))
        
        if(Ranger:hit()){
            
            OnGround = 1
            Diff = (Hauteur+sin(IncMouv*2)*(Stab*(Shift+1))-Ranger:distance())
    
            Ent:applyForce((vec(0,0,Diff)-Ent:vel()+(Ent:forward()*(W-S)*550/(Alt+1)/(Space+1)*(Shift+1)+Ent:up()*0))*Ent:mass())
    
        }
        else{OnGround = 0}
    
        Ent:turnTo(Ent:angles():setPitch(0):setRoll(Roll):setYaw(Ent:angles():yaw()+Yaw))
    }
    


     ik(120,120,holoEntity(10):pos(),FootLeft:pos(),10,12)
    
     ik(120,120,holoEntity(14):pos(),FootRight:pos(),14,16)
    
    
    
    
######---------------------------------------------------------------------------------------------------------##--END MOUV  
}

if(!Activate){
    Hauteur = 40
    ItsOK = 0
    Radius = 1
}
