@name E2 Pathfinding Test
@persist Path:array CurNode Pos:vector
@trigger PathClk
#include "lib/path/astar"
#include "e2shared/graph_gm_construct"

if (first()) {
    setGraph(Graph)
    path("73629","88249",25)
}

if (clk("PathClk")) {
    local Interval=1/PathClk["Path",array]:count()
    local Max=vec(0,255,0)
    local Min=vec(255,0,0)
    
    foreach(I,V:string=PathClk["Path",array]) {
        I++
        holoCreate(I)
        holoPos(I,Graph[V,table]["Pos",vector])
        holoModel(I,"models/hunter/misc/sphere1x1.mdl")
        holoScale(I,vec(0.3))
        holoDisableShading(I,1)
        holoShadow(I,0)
        holoMaterial(I,"debug/debugdrawflat")
        holoColor(I,Interval*I*(Max-Min)+Min)
    }
    
    Path=PathClk["Path",array]
    CurNode=1
    
    holoCreate(1)
    holoModel(1,"models/Humans/Group03/male_07.mdl")
    holoAnim(1,"ACT_WALK_CROUCH")
    Pos=Graph[Path[1,string],table]["Pos",vector]
    Ang=ang()
    
    holoPos(1,Pos)
    
    timer("run",10)
}

if (clk("run")) {
    local TargetPos=Graph[Path[CurNode+1,string],table]["Pos",vector]
    Ang=(TargetPos-Pos):toAngle()*ang(0,1,1)
    local RayForward=rangerOffset(5,Pos,(Ang+ang(45,0,0)):forward())
    Ang+=(TargetPos-RayForward:pos()):toAngle()*ang(1,0,0)
    Pos=Pos+Ang:forward()*6
    Pos=rangerOffset(5,Pos,vec(0,0,-1)):pos()
    
    #print(_HUD_PRINTCENTER,toString(Pos)+"\n"+toString(PrePos))
    if (Pos:distance(TargetPos)<12) {
        CurNode++
    }
    
    holoPos(1,Pos)
    holoAng(1,Ang*ang(0,1,1))
    
    timer("run",100)
}
