#Four legged mech with twin turrets and swiveling upper section
#based on a similar design from Supreme Commander 2
@name MechAA
@inputs [Controls Camera]:wirelink [Core FR FL Torso]:entity
@outputs Fire
@persist [Campos Camvec Cpos Corepos Torpos Larmpos Rarmpos]:vector Zoom Posture
@persist CoreP CoreY CoreR TorP TorY TorR LarmP LarmY LarmR RarmP RarmY RarmR Inc Parts:array
@persist [FLfootgo FLToepos FRfootgo FRToepos]:vector
runOnTick(1)

if (first() | dupefinished()) {
    Zoom = 200
    Posture = 75
    Parts = entity():getConstraints()
    FLfootgo = FL:pos()
    FRfootgo = FR:pos()

    rangerFilter(Parts)
    rangerHitEntities(1)
    rangerHitWater(1)
    rangerPersist(1)
}

#start controls
W = Controls:number("W")
A = Controls:number("A")
S = Controls:number("S")
D = Controls:number("D")
M1 = Controls:number("Mouse1")
M2 = Controls:number("Mouse2")
Space = Controls:number("Space")
Shift = Controls:number("Shift")#
Mwheelup = Controls:number("NextWeapon")
Mwheeldown = Controls:number("PrevWeapon")
Active = Controls:number("Active")
Seat = Controls:entity("Entity")
#end controls
Camera:setNumber("Activated",Active)
Camera:setVector("Position",Campos)
Camera:setVector("Direction",Camvec)

if(M1){Fire = 1}else{Fire = 0}

if(Mwheelup){Zoom = Zoom + 10}
if(Mwheeldown){Zoom = Zoom - 10}
Cang = (Cpos - Core:pos()):normalized()
if((Core:pos() - Cpos):length() > 300){Cammul = 100}else{Cammul = 10}
if((Core:pos() - Cpos):length() > 10){Cpos = Cpos - (Cang * Cammul)}
Campos = Torso:pos() + vec(0,0,70)-Seat:driver():eye()*Zoom
Camvec = Seat:driver():eye()

if(Space & Posture < 100){Posture = Posture + 1}
if(Space & Posture > 100){Posture = Posture - 1}
if(!Space & !Shift & Posture < 50){Posture = Posture + 1}
if(!Space & !Shift & Posture > 50){Posture = Posture - 1}

#hippos
CoreP = Core:angles():pitch()
CoreY = Core:angVel():yaw()/100
CoreR = Core:angles():roll()
CorePitch = (($CoreP*150)+(CoreP*20))*Core:mass()*-1
CoreYaw = (($CoreY*150)+(CoreY*20))*Core:mass()*-1
CoreRoll =(($CoreR*150)+(CoreR*20))*Core:mass()*-1
Core:applyAngForce(ang(CorePitch,CoreYaw,CoreRoll))

Corepos = Core:pos() - (((FL:pos() + FR:pos())/2)+vec(0,0,Posture))
Corevec = (($Corepos*150)+(Corepos*20))*Core:mass()/3*-1
Core:applyForce(Corevec)

#torsopos
Torpos = Torso:pos() - (Core:pos()+vec(0,0,45))
Torvec = (($Torpos*150)+(Torpos*20))*Torso:mass()/10*-1
Torso:applyForce(Torvec)

if (Active) {
    TorY = Torso:bearing(Seat:driver():eye()*500000)
} else {TorY = Torso:bearing(Core:forward()*500000)}

TorP = Torso:angles():pitch()
TorR = Torso:angles():roll()
TorPitch = (($TorP*150)+(TorP*20))*Torso:mass()/3*-1
TorYaw = (($TorY*150)+(TorY*20))*Torso:mass()*-1
TorRoll =(($TorR*150)+(TorR*20))*Torso:mass()*-1
Torso:applyAngForce(ang(TorPitch,TorYaw,TorRoll))

#Target = Seat:driver():eye()*500000
Tranger = rangerOffset(10000,Campos,Seat:driver():eye())
Target = Tranger:position()

#walk
FLToepos = FL:pos() - (FLfootgo + vec(0,0,FL:pos():distance(FLfootgo)/2))
FLToevec = (($FLToepos*150)+(FLToepos*20))*(FL:mass()/10*-1)
FL:applyForce(FLToevec)

FRToepos = FR:pos() - (FRfootgo + vec(0,0,FR:pos():distance(FRfootgo)/2))
FRToevec = (($FRToepos*150)+(FRToepos*20))*(FR:mass()/10*-1)
FR:applyForce(FRToevec)

#control
if (W | S | A | D) {
    if(Inc < 100){Inc = Inc + 2 + (Shift*2)}
    if(Inc >= 100){Inc = 0}
    
    if(Inc <= 50)               {Foot = 1}
    if(Inc <= 100 & Inc > 50)   {Foot = 2}
    
    Vel = 70
} else {
    Foot = 0
    Vel = 0
}

if (!W & !S & !A & !D) {
    Foot = 0
    Vel = 0
}

if (A) { Dire = Torso:right()}

if (D) {Dire = Torso:right()*-1}

if (!A & !D) {Dire = Seat:driver():eye()}

if (changed(Foot)) {
    Core:soundPlay("footstepL",0.3,"npc/dog/dog_footstep" + randint(1,4) + ".wav")
}

if (Foot == 1) {
    Foottar = rangerOffset(5000,Torso:toWorld(vec(0,55,0)) + Dire*Vel,vec(0,0,-1))
    FLfootgo = Foottar:position() + vec(0,0,12)   
}

if (Foot == 2) {
    Foottar = rangerOffset(5000,Torso:toWorld(vec(0,-55,0)) + Dire*Vel,vec(0,0,-1))
    FRfootgo = Foottar:position() + vec(0,0,12)        
}
