@name array
@inputs 
@outputs 

@persist Temp Wtf:array Test
@trigger none

if (first()) {

    #include "array_test1"
    
   # Wtf = fenToBoard( "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR/ w KQkq" )
    #Wtf = fenToBoard( "8/8/8/3Q4/8/5Q2/8/RNBQKBNR/ w KQkq" )
    Wtf = fenToBoard("4k3/1B6/3R4/5RB1/2Q5/5N2/3N4/4K3 w XXXX" )
    
    #Wtf = fenToBoard( "rnbkqbnr/pppppppp/8/8/8/8/PPPP1PPP/RNBQKBNR/ w KQkq" )
    
    #printTable(Wtf)

    GenListInc = 0
    GenListCount = 0
    GenList = table()
    GenLegal = table()
    GenIllegal = table()
    
    timer("test", 500)
    
    local W = vec(255)
    local B = vec()
    local Sc = vec(0.5,0.5,0.5)
    local A90 = ang(0, 0, 90)
    local E = entity()
    
    local N = 0
    for (Y = 1, 8) {
        for (X = 1, 8) {
            N++
            holoCreate(N, E:toWorld(vec(-X*13, Y*13, 0)))
            holoColor(N, (X + Y) % 2 == 0 ? B : W)
            holoModel(N, "plane")
            
            if (Wtf[N, string] != ".") {
                holoCreate(N+1000, holoEntity(N):pos(), Sc, A90 )  
                holoColor(N+1000, inrange(toByte(Wtf[N, string]), 60, 90) ? W : B)
                holoModel(N+1000, "models/sprops/misc/alphanum/alphanum_" + Wtf[N, string]:lower() + ".mdl")
            }
        }
    }
    
    function number getColor(Index) {
        return (floor((Index - 1) / 8) + mod(Index - 1, 8)) % 2 == 0
    }

}

if (clk("test")) {
    timer("test", 15)
    
    if (Wtf:genMoves("w")) {
        #printTable(Wtf)
        
        print("legal")
        printTable(GenLegal)
        print("\nillegal")
        printTable(GenIllegal)
        
        stoptimer("test")
        timer("run", 0)
    }
}

if (clk("run")) {
    timer("run", 200)

    holoColor(Test, select(getColor(Test) + 1, vec(255), vec()))
    foreach (K, N:number = GenLegal[Test, array]) { holoColor(N, select(getColor(N) + 1, vec(255), vec())) }
    
    Test = 10
    holoColor(Test, vec(255,255,0))
    foreach (K, N:number = GenLegal[Test, array]) { holoColor(N, vec(125, 125, 255)) }
    #print(GenLegal[Test, array])
    
    if (Test > 63) { Test = 0 }
}
