@name Walking Chip MkIV
@model models/hunter/plates/plate.mdl
@inputs Left:entity Right:entity W A S D R Active
@persist Foot_pos_array:array Heading Stage Offset Speed Foot:entity Sensativity Host:entity
@persist Angle:angle Foot_dest_rot Foot_final_rot Foot_rot:angle Speed_rot On Stride Foot_lift Foot_fall Delay
@persist Dest:vector Foot_final_pos:vector Foot_dest_pos:vector Foot_force:vector Last_pressed:string
if (duped()) {
    reset()}
if (first()) {
    #remove *On from lines 50 and 52 to stop the robot collapsing when not in use
    On=0
    Last_pressed="w"
    Host=entity():isConstrainedTo()
    Heading=Host:angles():yaw()
    Foot_pos_array=array()
    Foot=Right
    Stage=0
    print("You are using the Walking Chip Mk IV, written by NightReaper")
    #the 10 lines below are constants that you can alter to allow for different sized walking bases
    Delay=10 #how many 10s of millieseconds to let the foot settle after reaching its final destination before moving the other foot
    Foot_lift=14 #how far below the pelvis to lift the foot up to
    Foot_fall=7.5 #how fast the foot falls to its final position
    Stride=48 #how far forwards from the pelvis to pus the foot before letting it fall
    Speed=5 #how many units to move the foot every 10 milliseconds
    Sensativity=1 #how many degrees every 10 millieseconds to alter the heading by
    Offset=12 #how far to the side the feet are from the pelvis
    Left:setMass(250) #mass of the left foot (this should be left alone under most but not all circumstances)
    Right:setMass(250) #mass of the right foot (this should be left alone under most but not all circumstances)
    Host:setMass(750) #mass of the pelvis (this should be left alone under most but not all circumstances)
    interval(10)}
if (clk()) {
    if (Active!=On) {
        if (Active==1) {
            soundPlay(1,0,"npc/roller/remote_yes.wav")
            Heading=Host:angles():yaw()}
        else {
            Stage=0
            soundStop(1)
            soundStop(2)
            soundStop(3)
            soundPlay(1,0,"npc/roller/code2.wav")}
        On=Active}
    Heading+=(A-D)*Sensativity
    if (Heading>360) {Heading-=360}
    if (Heading<0) {Heading+=360}
    if (R==1) {Stage=1}
    Yaw=Host:angles():yaw()-Heading
    if (Yaw>180) {Yaw=0-(360-Yaw)}
    if (Yaw<-180) {Yaw=360+Yaw}
    Angle=Host:angles():setYaw(Yaw)
    Host:applyAngForce(($Angle*20+Angle*4)*Host:mass()*-1*On)
    Dest=(mix(Left:pos(),Right:pos(),0.5)+vec(0,0,50))-Host:pos()
    Host:applyForce((($Dest*20)+(Dest*4))*Host:mass()*On)
    if (W!=S & Stage==0) {Stage=1}
    if (Stage==1) {
        Foot_pos_array:insertVector(1,Foot:pos()+vec(0,0,Foot:radius()*3))
        Foot_pos_array:insertVector(2,Host:pos()+Host:up()*(0-Foot_lift)+Host:right()*Offset)
        Foot_pos_array:insertVector(3,Foot_pos_array[2,vector]+Host:forward()*((W-S)*Stride))
        Foot_final_rot=Heading
        Foot_dest_rot=Foot:angles():yaw()
        Foot_dest_pos=Foot:pos()
        soundPlay(1,0,"vehicles/tank_turret_start1.wav")
        soundVolume(1,0.375)
        Stage+=1}
    elseif (Stage==2 | Stage==4 | Stage==6) {
        Foot_final_pos=Foot_pos_array[Stage/2,vector]
        Stage+=1}
    elseif (Stage==3 | Stage==5 | Stage==7) {
        soundPlay(2,0,"vehicles/tank_turret_loop1.wav")
        soundVolume(2,0.375)
        if (Foot_dest_pos:distance(Foot_final_pos)>Speed) {
            Foot_dest_pos+=((Foot_final_pos-Foot_dest_pos):normalized()*Speed)}
        else {Stage+=1}
        Foot_force=Foot_dest_pos-Foot:pos()
        Foot:applyForce((($Foot_force*20)+(Foot_force*4))*Foot:mass())
        Foot_yaw=Foot:angles():yaw()-Foot_final_rot
        if (Foot_yaw>180) {Foot_yaw-=360}
        if (Foot_yaw<-180) {Foot_yaw+=360}
        Foot_rot=Foot:angles():setYaw(Foot_yaw)
        Foot:applyAngForce(($Foot_rot*10+Foot_rot*2)*Foot:mass()*-1)}
    elseif (Stage==8) {
        if (Foot:vel():length()<2.5) {Stage+=1}
        else {
            Foot:applyForce(Foot:vel()/Foot_fall*Foot:mass()*-1)
            Foot:applyAngForce(Foot:angVel()/2*Foot:mass()*-1)}}
    elseif (Stage==9) {
        soundStop(2)
        soundPlay(3,0,"vehicles/tank_turret_stop1.wav")
        soundVolume(3,0.375)
        soundPlay(4,0,"npc/dog/dog_footstep"+toString(floor(random(1,5)))+".wav")
        Offset=0-Offset
        if (Foot==Left) {Foot=Right}
        else {Foot=Left}
        Stage+=1}
    elseif (Stage<10+Delay & Stage!=0) {Stage+=1}
    elseif (Stage==10+Delay) {Stage=0}
    interval(10)}
