@name Meching Mech
@inputs Pod:wirelink 
@outputs Mode 
@persist Length1 Length2 FootL:entity FootR:entity E:entity N Radius Wobble 
@persist Foot:array Active Height Yaw Shift Space Jump OnGround Alt 
@persist Rot C1 C2 Mod G1:ranger G2:ranger Reload Reloading This Y 
@persist R RGB Charge Firing BodyYaw Range:ranger
rangerPersist(1)
runOnTick(1)

Seat = Pod["Entity",entity]
Driver = Seat:driver()
Alt = Pod["Alt",number]
Space = Pod["Space",number]
Active = Pod["Active",number]
Shift = Pod["Shift",number]
W = Pod["W",number]
A = Pod["A",number]
S = Pod["S",number]    
D = Pod["D",number]
M1 = Pod["Mouse1",number]

if (first()|dupefinished()) {
    Wobble = 20
        
    E = entity():isWeldedTo()
    
    E:setAlpha(0)
        
    E:setMass(500)
        
    FootL = holoCreate(11,E:pos(),vec(1))
    FootR = holoCreate(12,E:pos(),vec(1))
            
    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 = E:toLocalAxis(End - Hip)
        local Angle = Axis:toAngle():setRoll(-bearing(Hip,E:angles(),End))
        local Dist = min(Axis:length(),L1 + L2)
        local Quat = quat(Angle) * qRotation(vec(0,90 + cosine(Dist,L1,L2),0))
        holoAng(I1,E:toWorld(Quat:toAngle()))
        holoAng(I2,holoEntity(I1):toWorld(ang(cosine(L2,L1,Dist) + 180,0,0)))
    }
    
    holoCreate(1,E:toWorld(vec(0,25,0)))
    holoCreate(2,holoEntity(1):toWorld(vec(0,0,Length1 - 20)))
            
    holoModel(1,"hq_sphere")
    holoModel(2,"hq_sphere")
            
    holoParent(1,E)
    holoParent(2,1)
            
    holoCreate(3,E:toWorld(vec(0,-25,0)))
    holoCreate(4,holoEntity(3):toWorld(vec(0,0,Length1 - 20)))
            
    holoModel(3,"hq_sphere")
    holoModel(4,"hq_sphere")
                
    holoParent(3,E)
    holoParent(4,3)
            
    holoCreate(5,holoEntity(1):pos()+holoEntity(1):up()*(Length1/3),vec(0.5,0.5,3.5),holoEntity(1):angles(),vec(255),"hq_cylinder")
    holoCreate(6,holoEntity(2):pos()+holoEntity(2):up()*(Length2/3),vec(0.5,0.5,3.5),holoEntity(2):angles(),vec(255),"hq_cylinder")

    holoParent(5,1)
    holoParent(6,2)
            
    holoCreate(7,holoEntity(3):pos()+holoEntity(3):up()*(Length1/3),vec(0.5,0.5,3.5),holoEntity(1):angles(),vec(255),"hq_cylinder")
    holoCreate(8,holoEntity(4):pos()+holoEntity(4):up()*(Length2/3),vec(0.5,0.5,3.5),holoEntity(2):angles(),vec(255),"hq_cylinder")
    
    holoParent(7,3)
    holoParent(8,4)
    
    for (Legs = 1,12) {holoMaterial(Legs,"models/props_combine/metal_combinebridge001")}
    
    rangerFilter(E)
    
    Radius = 40
    
    #Pivitor
    holoCreate(100)
    holoAng(100,E:toWorld(ang(0,0,0)))
    holoAlpha(100,0)
    holoParent(100,E)
    
    #Left Arm
    holoCreate(20)
    holoModel(20,"models/props_c17/oildrum001.mdl")
    holoMaterial(20,"models/props_combine/metal_combinebridge001")
    holoPos(20,E:toWorld(vec(-50,-35,20)))
    holoAng(20,E:toWorld(ang(0,90,90)))
    holoScale(20,vec(0.4,0.4,0.8))
    holoParent(20,100)
    
    holoCreate(21)
    holoModel(21,"models/Items/combine_rifle_cartridge01.mdl")
    holoPos(21,E:toWorld(vec(7,-35,22)))
    holoAng(21,E:toWorld(ang(45,180,0)))
    holoScale(21,vec(6,4,3))
    holoParent(21,100)
    
    holoCreate(22)
    holoModel(22,"models/Items/combine_rifle_ammo01.mdl")
    holoPos(22,E:toWorld(vec(-9,-30,20)))
    holoAng(22,E:toWorld(ang(90,0,0)))
    holoParent(22,100)
    
    holoCreate(23)
    holoModel(23,"models/props_combine/breenglobe.mdl")
    holoMaterial(23,"models/props_combine/metal_combinebridge001")
    holoPos(23,E:toWorld(vec(-55,-35,20)))
    holoAng(23,E:toWorld(ang(0,90,90)))
    holoParent(23,20)
    
    #Right Arm
    holoCreate(24)
    holoModel(24,"models/props_c17/oildrum001.mdl")
    holoMaterial(24,"models/props_combine/metal_combinebridge001")
    holoPos(24,E:toWorld(vec(-30,35,20)))
    holoAng(24,E:toWorld(ang(0,90,90)))
    holoScale(24,vec(0.4,0.4,0.4))
    holoParent(24,100)
    
    holoCreate(25)
    holoModel(25,"models/Items/combine_rifle_cartridge01.mdl")
    holoPos(25,E:toWorld(vec(7,35,22)))
    holoAng(25,E:toWorld(ang(45,180,0)))
    holoScale(25,vec(6,4,3))
    holoParent(25,100)
    
    holoCreate(26)
    holoModel(26,"models/Items/combine_rifle_ammo01.mdl")
    holoPos(26,E:toWorld(vec(-9,30,20)))
    holoAng(26,E:toWorld(ang(90,0,0)))
    holoParent(26,100)
    
    holoCreate(27)
    holoModel(27,"models/props_combine/combine_binocular01.mdl")
    holoPos(27,E:toWorld(vec(-40,35,20)))
    holoAng(27,E:toWorld(ang(90,90,90)))
    holoParent(27,24)
    
    holoCreate(28)
    holoModel(28,"hq_sphere")
    holoMaterial(28,"models/alyx/emptool_glow")
    holoPos(28,holoEntity(23):pos() + vec(-2,0,0))
    holoAlpha(28,0)
    holoParent(28,23)
    
    holoCreate(29)
    holoModel(29,"models/props_combine/combine_mortar01b.mdl")
    holoMaterial(29,"models/props_combine/metal_combinebridge001")
    holoPos(29,E:toWorld(vec(-30,0,40)))
    holoAng(29,E:toWorld(ang(90,90,90)))
    holoParent(29,100)
    
    holoCreate(30)
    holoModel(30,"models/props_combine/combine_booth_short01a.mdl")
    holoPos(30,E:toWorld(vec(20,0,10)))
    holoAng(30,E:toWorld(ang(0,0,0)))
    holoScale(30,vec(0.4,0.4,0.4))
    holoParent(30,100)
    
    holoCreate(31)
    holoModel(31,"models/props_combine/combine_barricade_med01b.mdl")
    holoPos(31,E:toWorld(vec(-20,0,50)))
    holoAng(31,E:toWorld(ang(-90,90,90)))
    holoScale(31,vec(0.4,0.4,0.4))
    holoParent(31,100)
    
    holoCreate(32)
    holoModel(32,"models/combine_helicopter/bomb_debris_2.mdl")
    holoPos(32,E:toWorld(vec(-3,25,40)))
    holoAng(32,E:toWorld(ang(0,0,90)))
    holoScale(32,vec(2.1,4,3))
    holoParent(32,100)
    
    holoCreate(33)
    holoModel(33,"models/combine_helicopter/bomb_debris_2.mdl")
    holoPos(33,E:toWorld(vec(-3,-25,40)))
    holoAng(33,E:toWorld(ang(0,180,90)))
    holoScale(33,vec(2.1,4,3))
    holoParent(33,100)
    
    holoCreate(34)
    holoModel(34,"models/combine_helicopter/bomb_debris_2.mdl")
    holoPos(34,E:toWorld(vec(-3,0,15)))
    holoAng(34,E:toWorld(ang(0,180,0)))
    holoScale(34,vec(2.1,4,3))
    holoParent(34,100)
    
    holoCreate(35)
    holoPos(35,holoEntity(11):pos() + vec(0,0,-12))
    holoParent(35,11)
    
    holoCreate(36)
    holoPos(36,holoEntity(12):pos() + vec(0,0,-12))
    holoParent(36,12)
    
    for (Feet = 35,36) {holoModel(Feet,"models/props_combine/combine_smallmonitor001.mdl")}
}

holoAng(11,E:angles() + ang(90,90,0))
holoAng(12,E:angles() + ang(90,90,0))

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

if (changed(M1) & M1) {
    holoEntity(20):soundPlay(4,1.5,"weapons/cow_mangler_over_charge.wav")
    timer("fire",1550)
    
    Rot = 1
}

if (clk("fire")) {
    holoEntity(20):soundPlay(4,2,"weapons/cow_mangler_main_shot.wav")
    soundPitch(4,70)
    
    Rot = 0
}

if (Rot == 1) {
    holoAng(23,holoEntity(23):angles() + ang(8,0,0))
    holoAlpha(28,255)
}
    
if (Rot == 0) {
    holoAng(23,holoEntity(100):toWorld(ang(0,90,90)))
    holoAlpha(28,0)
}

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())
    
    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 & OnGround) {soundPlay(-3,1,"")}
    if (OnGround == 0){
        Jump = 0
        Mod = 0
    }
   Yaw = Driver:eyeAngles():yaw()
    
    holoAng(100,ang(0,Yaw + 180,0))
}

if (holoEntity(100):angVel()) {
    soundPlay(100,0,"ambient/machines/hydraulic_1.wav")
}

if (Active == 0) {
    Height = 50
    Radius = 1
}

if (A) {Yaw = 2}
if (D) {Yaw = -2}
if (!A & !D) {Yaw = 0}

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(11,RL:pos()+vec(0,0,14))
holoPos(12,RR:pos()+vec(0,0,14))

if (changed(RL:hit()) & RL:hit() | changed(RR:hit()) & RR:hit()) {
    E:soundPlay("footstepL", 0.3,"npc/dog/dog_footstep" + randint(1,4) + ".wav")
}

if ((W|S) & OnGround == 1) {
    if (W) {N += 2 * (Shift+1) * ((W-S)*2)}
    if (S) {N += 2 * (Shift+1) * ((S-W)*2)}
}
 
rangerFilter(E) 
Rng = rangerOffset(E:pos(),E:pos()-vec(0,0,120))

if (Rng:hit()) {
    OnGround = 1
    Diff = Height+sin(N * 2) * (Wobble * (Shift+1))-Rng:distance()

    E:applyForce(vec(0,0,Diff)*2*E:mass()-(E:vel())*E:mass()+E:forward()*(S-W)*150/(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(0):setYaw(E:angles():yaw()+Yaw))
