@name Node Graph Test
@inputs 
@outputs Off:array Nodes:table CountN
@persist Nodes:table
@trigger 
#Generate a sample node table to work off of
if(first()){
    E=round(entity():pos())  #rounded to shorten the number lengths
    Off=array()
    Size=5
    Half=3
    Grid=100
    for(C=1,Size){ #This generates an offset grid from the chip's possition
        CS=toString(C)        
        for(D=1,Size){
            DS=toString(C)
            Off:pushVector(vec((C-Half)*Grid,(D-Half)*Grid,50))
        }
    }
    for(C=1,Off:count()){   #links it all up in a 90 degree grid pattern (I hope)
        CS=toString(C)
        Links=array()
        for(D=1,Off:count()){
            Distance=Off[C,vector]:distance(Off[D,vector])
            if(Distance<Grid*1.0){
                Links:pushVector(vec(D,Distance,C)) #Link format (NodeID,distance,SelfNode)
            }
        }
        Nodes[CS,string]=glonEncode(Links)
    }
    Count=Off:count()
    CountN=Nodes:count()
    C=1
    for(C=1,Off:count()){
        holoCreate(C,E+Off[C,vector])
        holoParent(C,entity())
        CS=toString(C)
        Nodes[CS,vector]=E+Off[C,vector]
    }

}
Self=E  #whatever it is you want to move to the target point
Tar=round(owner():pos()) #the target point
Search=0

if(Search){

    Open=array() Closed=array() Dist=array() 
    for(C=1,Nodes:count()){
        CS=toString(C)
        Dist:pushNumber(Self:distance(Nodes[CS,vector]))
    }
    Start=Dist:minIndex()
    G=Dist[Start,number] #The exact cost to reach this node from the starting node (this is starting node)    
    H=0      #Heuristic, cost to reach destination from here
    N=Start  #The node this refers to
    P=Start  #Parent node ID
    Open:pushVector4(vec4(G,H,N,P)) #mark the nearest node as Open
    #OpenF:pushNumber(Open[C,vector4]:x()+Open[C,vector4]:y())
    CurNode=Start 
    Goal=0
    while(Tar:distance(Self)>100){
        OpenF=array()
        for(C=1,Open:count()){
            OpenF:pushNumber(Open[C,vector4]:x()+Open[C,vector4]:y())
        }
        LowF=Open[OpenF:minIndex(),vector4]:z() #the node ID of the lowest F
        LowFI=OpenF:minIndex() #Low F Index
        if(LowF==Goal){
            break
        }else{
            Closed:pushVector4(Open[LowFI,vector4])
            Links=glonDecode(Nodes[toString(LowF),string])
            for(C=1,Links:count()){   #Load all links into Open list
                L=toString(Links[C,vector]:x()) #linked node
                G=Links[C,vector]:y() #Distance to node
                H=-1
                N=Links[C,vector]:z() #Self Node ID
                P=LowF                #Parent Node ID
                Open:pushVector4(vec4(G,H,N,P))
            }
            Open:remove(Open:minIndex())
            for(C=1,Open:count()){ #for each neightbor
                #if(Open[C,vector4]:
            }
            
        }#end if(LowF==Goal)
        
        
    }#end if target isn't near self
}#end search function


#     G 
#        the exact cost to reach this node from the starting node. 
#    H 
#        the estimated(heuristic) cost to reach the destination from here. 
#    F = G + H 
#        As the algorithm runs the F value of a node tells us how expensive we think it will be to reach our goal by way of that node. 
#


#   create the open list of nodes, initially containing only our starting node
#   create the closed list of nodes, initially empty
#   while (we have not reached our goal) {
#       consider the best node in the open list (the node with the lowest f value)
#       if (this node is the goal) {
#           then we're done
#       }
#       else {
#           move the current node to the closed list and consider all of its neighbors
#           for (each neighbor) {
#               if (this neighbor is in the closed list and our current g value is lower) {
#                   update the neighbor with the new, lower, g value 
#                   change the neighbor's parent to our current node
#               }
#               else if (this neighbor is in the open list and our current g value is lower) {
#                   update the neighbor with the new, lower, g value 
#                   change the neighbor's parent to our current node
#               }
#               else this neighbor is not in either the open or closed list {
#                   add the neighbor to the open list and set its g value
#               }
#           }
#       }
#   }
