#@name mech rewrite 2
@inputs 
@outputs 
@persist MechCycle Test:table
@trigger none

if (first()) {
    function table mechCreateLeg( Ent:entity, Loc:vector, Rate, Vel, Step, Ground ) {
        local TempR = rangerOffset(250, Ent:toWorld(Loc), vec(0,0,-1))
        local TempV = TempR:position() + TempR:hitNormal() * Ground

        return table(
            "ent" = Ent, "loc" = Loc, "mode" = "check_step",
            "rate" = Rate, "vel" = Vel, "step" = Step, "ground" = Ground,
            "p0" = TempV, "p1" = TempV, "p2" = TempV, "hold" = TempV, "output" = TempV
        )    
    }

    function table:mechRunLeg() {
        switch (This["mode", string]) {
            case "check_step", 
                if (MechCycle == This["rate", number]) {
                    local TempR = rangerOffset(250, This["ent", entity]:toWorld(This["loc", vector]) + This["ent", entity]:vel():setZ(0) / This["vel", number], vec(0,0,-1))

                    if (TempR:hit()) {
                        This["hold", vector] = TempR:position() + TempR:hitNormal() * This["ground", number]
                        This["dist", number] = This["hold", vector]:distance(This["output", vector])

                        if (This["dist", number] > 25) {
                            This["p0", vector] = This["output", vector]
                            This["p1", vector] = mix(This["hold", vector], This["p0", vector], 0.33) + vec(0, 0, This["dist", number] / This["step", number])
                            This["hold", vector] = This["ent", entity]:toLocal(This["hold", vector]):setZ(0)
                            This["mode", string] = "exec_step"
                        }
                    }
                }
            break
    
            case "exec_step",
                local TempR = rangerOffset(250, This["ent", entity]:toWorld(This["hold", vector]), vec(0,0,-1))

                This["time", number] = min(This["time", number] + 0.25, 1)
                This["p2", vector] = TempR:position() + TempR:hitNormal() * This["ground", number] 
                This["output", vector] = mix(
                    mix(This["p2", vector], This["p1", vector], This["time", number]),
                    mix(This["p1", vector], This["p0", vector], This["time", number]),
                    This["time", number])

                if (This["time", number] >= 1) {
                    This["time", number] = 0
                    This["mode", string] = "check_step"    
                }
            break
            
            case "slide",
                local TempR = rangerOffset(250, This["ent", entity]:toWorld(This["loc", vector]), vec(0,0,-1))
                local TempV = TempR:position() + TempR:hitNormal() * This["ground", number]

                This["p0", vector] = This["p1", vector] = This["p2", vector] = This["hold", vector] = This["output", vector] = TempV
            break
        }
    }
    
    Test = mechCreateLeg( entity(), vec(), 0, 1.5, 3, 0 )

    holoCreate(1)

}

interval(90)

MechCycle = (MechCycle + 1) % 6
Test:mechRunLeg()

holoPos(1, Test["output", vector])
