@name Hatifnats Pathfinder
@inputs 
@outputs I Current:vector2 ParentIndex:array Path:array Pos:vector2
@persist [PlatformScale Aim Current BaseG BaseR Dis]:vector2 Holo
@persist [HoloPos HoloHistory F G H Open Child]:array
@persist [Object Closed OpenA Parent Valid]:table
@persist Once Once2 X Y GridScale Deleted I2 I3 Done A Low Finish HoloPath
@model models/hunter/plates/plate4x4.mdl
#      ^^^^^^^^^^^^Max is 6x6^^^^^^^^^^^^
interval(100)
if(first()) {
    setName("Pathfinder [0.8] [Pathfinder Version 0.8.3 Alpha]")
    GridScale = 11.85
    I=100
    
    #N NE E SE S SW W NW
    Child = array( vec2(1,0), vec2(1,-1), vec2(0,-1), vec2(-1,-1), vec2(-1,0), vec2(-1,1), vec2(0,1), vec2(1,1))
    
    PlatformScale3 = entity():boxSize()/4
    PlatformScale = vec2(PlatformScale3:x(),PlatformScale3:y())
    
    X = floor(PlatformScale:x()/GridScale)*4
    Y = floor(PlatformScale:y()/GridScale)*4
    
    holoCreate(1)
    holoScale(1,vec(GridScale/12,GridScale/12,0.2))
    holoColor(1,vec(0,0,255))
    holoParent(1,entity())
    
    holoCreate(2)
    holoScale(2,vec(GridScale/12,GridScale/12,0.2))
    holoColor(2,vec(0,255,0))
    holoParent(2,entity())
    
    holoCreate(3)
    holoScale(3,vec(GridScale/12,GridScale/12,0.2))
    holoColor(3,vec(255,0,0))
    holoParent(3,entity())
    
    holoCreate(4)
    holoModel(4,"models/Gibs/HGIBS.mdl")
    holoColor(4,vec(255,50,150))
    holoParent(4,entity())
    
    holoCreate(5)
    holoScale(5,vec(GridScale/12,GridScale/12,0.2))
    holoColor(5,vec(255,255,0))
    holoParent(5,entity())
    
    BaseG = vec2(X/2,Y/2)
    BaseR = vec2(-X/2+1,-Y/2+1)
    
    holoPos(2,entity():toWorld(vec(BaseG:x()*GridScale-GridScale/2,BaseG:y()*GridScale-GridScale/2,1)))
    holoPos(3,entity():toWorld(vec(BaseR:x()*GridScale-GridScale/2,BaseR:y()*GridScale-GridScale/2,1)))
    holoPos(4,entity():toWorld(vec(BaseR:x()*GridScale-GridScale/2,BaseR:y()*GridScale-GridScale/2,5)))

    Object[BaseG:toString(),number]=1
    Object[BaseR:toString(),number]=1
    Open[1,vector2]=BaseR
    OpenA[BaseR:toString(),number]=1
    Point=BaseR
    G[1,number]=0
    H[Open:count(),number]=((BaseG-BaseR-vec2(1,1)):x()+(BaseG-BaseR-vec2(1,1)):y())*10
    F[1,number]=H[Open:count(),number]+G[Open:count(),number]
    ParentIndex[1,vector2]=BaseR
    Current=BaseR
    Done=0
    A=1
    HoloPath=1
    #Path[I,vector2]=BaseG
    timer("FirstPart2",1000)
} elseif(clk("FirstPart2")) {
    for(I2= -X/2+1,0) {
        for(I3= -X/2+1,0) {
            Valid[vec2(I2,I3):toString(),number]=1
        }
    }
    timer("FirstPart3",1000)
} elseif(clk("FirstPart3")) {
    for(I2= 1,X/2) {
        for(I3= 1,Y/2) {
            Valid[vec2(I2,I3):toString(),number]=1
        }
    }
    Done=1
} elseif(Done) {
if(owner():aimEntity()==entity()) {
    Aim3 = clamp(floor(entity():toLocal(owner():aimPos())/GridScale)+vec(1,1,0),vec(BaseR:x(),BaseR:y(),0),vec(BaseG:x(),BaseG:x(),0))
    Aim = vec2(Aim3:x(),Aim3:y())
    holoPos(1,entity():toWorld(vec(Aim:x()*GridScale-GridScale/2,Aim:y()*GridScale-GridScale/2,2)))
    Holo=Object[Aim:toString(),number]:toString():sub(1,Object[Aim:toString(),number]:toString():length()):toNumber()
    if(!Object[Aim:toString(),number]) {
        if(owner():keyAttack1()&!Once) {
            Once=1
            I++
            holoCreate(I)
            holoPos(I,entity():toWorld(vec(Aim:x()*GridScale-GridScale/2,Aim:y()*GridScale-GridScale/2,2)))
            holoScale(I,vec(GridScale/12,GridScale/12,0.1))
            holoColor(I,vec(100,200,255))
            holoParent(I,entity())
            Object[Aim:toString(),number]=I
            HoloPos[I-100,vector2]=Aim
            HoloHistory[I-100,number]=I
            C=HoloHistory:count()
        } elseif(!owner():keyAttack1()) {Once=0}
    }
    #[if(owner():keyAttack2()&!Once2) {
        Once2=1
        Deleted = HoloHistory:removeNumber(Holo-100)
        HoloPos:removeVector2(Holo-100)
        #TObject=Object:clone()
        Object[Aim:toString(),string]="Deleted"
        Count=HoloHistory:count()
        
        for(I2=101,Count+100) {
            holoDelete(I2)
            if(I2>Deleted-100) {
                HoloHistory[I2-100,number] = HoloHistory[I2-100,number]-1
                #HoloPos[I,vector2] = HoloPos[I-1,vector2]
            }
        }
        for(I2=1,Count) {
            Aim=HoloPos[I2,vector2]
            if(Object[Aim:toString(),string]=="Deleted") {
                Object:remove(Aim:toString())
                Object[Aim:toString(),number] = Object[Aim:toString(),number]-1
            }
            if(I2==Count) {
                Object:remove(Aim:toString())
            }
            
        }
        
        for(I2=101,Count+100) {
            Aim=HoloPos[I2-100,vector2]
            Index=HoloHistory[I2-100,number]
            holoCreate(Index)
            holoPos(Index,entity():toWorld(vec(Aim:x()*GridScale-GridScale/2,Aim:y()*GridScale-GridScale/2,2)))
            holoScale(Index,vec(GridScale/12,GridScale/12,0.1))
            holoColor(Index,vec(100,200,255))
            holoParent(Index,entity())
        }
        TObject:clear()
        I=I-1
    } elseif(!owner():keyAttack2()) {Once2=0}]#

}

if(owner():keyReload() & Open:count() & Finish==0) {
    while(perf()) {
    if(Closed[BaseG:toString(),number]) {
        Finish=1
    }
    if(Open:count() & Finish==0)  {
        Low = F:minIndex()
        
        Current=Open[Low,vector2]
        holoPos(5,entity():toWorld(vec(Current:x()*GridScale-GridScale/2,Current:y()*GridScale-GridScale/2,2)))
        for(I2=1,8) {
            if(Valid[(Current+Child[I2,vector2]):toString(),number]) {
                if(Object[(Current+Child[I2,vector2]):toString(),number]<100) {
                    if(!OpenA[(Current+Child[I2,vector2]):toString(),number]) {
                        
                        
                        OpenA[(Current+Child[I2,vector2]):toString(),number]=Open:count()+1
                        Open[Open:count()+1,vector2]=Current+Child[I2,vector2]
                        
                        if(I2 % 2) {
                            G[Open:count(),number]=G[Low,number]+10
                        } else {
                            G[Open:count(),number]=G[Low,number]+14
                        }
                        H[Open:count(),number]=((BaseG-Open[Open:count(),vector2]-vec2(1,1)):x()+(BaseG-Open[Open:count(),vector2]-vec2(1,1)):y())*10
                        F[Open:count(),number]=H[Open:count(),number]+G[Open:count(),number]
                        Parent[(Current+Child[I2,vector2]):toString(),vector2]=Current
                        ParentIndex[Open:count(),vector2]=Current
                    }
                }
            }
        }
        if(!Closed[Current:toString(),number]) {
            Open:remove(Low)
            F:remove(Low)
            H:remove(Low)
            G:remove(Low)
            Closed[Current:toString(),number]=1
        }
    }elseif(!Open:count()) {Finish = -1}
}
} elseif(Finish == 1 & HoloPath!=Path:count()) {
    Pos = BaseG
    I3=1
    while(Pos!=BaseR) {
        Pos=Parent[Pos:toString(),vector2]
        Path[I3,vector2]=Pos
        I3++
    }
    if(HoloPath<Path:count()) {
        Pos = Path[HoloPath,vector2]
        holoCreate(HoloPath+300)
        holoPos(HoloPath+300,entity():toWorld(vec(Pos:x()*GridScale-GridScale/2,Pos:y()*GridScale-GridScale/2,2)))
        holoScale(HoloPath+300,vec(GridScale/12,GridScale/12,0.2))
        holoModel(HoloPath+300,"cylinder")
        holoColor(HoloPath+300,vec(255,0,0))
        holoParent(HoloPath+300,entity())
        HoloPath++
    }
}
}
