@name array2
@inputs 
@outputs 
@persist T:array M:array ID KPW KPB [VDI VDN] [VDL]:array Colors:array Time Inc
@trigger 

if (first()) {
    
    #include "test_wtf"    
    
    BD = "
        r n b q k b n r
        p p p p p p p p
        . . . . q . P .
        . . . . Q . . .
        . . . . . . . .
        . . . . . . p .
        P P P P . P P P
        R N B Q K B N R
    ":replaceRE("%s", "")

    BD = "
        . . . . . . . .
        . . . . . . . .
        . . . . q . . .
        . . . . Q . . .
        . . . . . . . .
        . . . . . . . .
        . . . . . . . .
        . . . . K . . .
    ":replaceRE("%s", "")
        
    ID = 29
    KPW = 61
    KPB = 5
    
    T = chess_create(BD)    
    M = T:chess_moves_at_index(ID, 0)
    #M = T:chess_moves_at_index(KPW, -4)
    
    print(M)
    
    #print(M)
    
    VDN = M:count()
    
    timer("validate", 1000)

    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*12.25, Y*12.25, 0)))
            holoColor(N, (X + Y) % 2 == 0 ? B : W)
            holoModel(N, "plane")
            
            if (T[N, number]) {
                holoCreate(N+1000, holoEntity(N):pos(), Sc, A90 )  
                holoColor(N+1000, T[N, number] > 0 ? W : B + vec(60))
                holoModel(N+1000, "models/sprops/misc/alphanum/alphanum_" + ValuePiece[T[N, number], string]:lower() + ".mdl")
            }
        }
    }
    
    Colors = array(
        1 = hsv2rgb(204, 0.76, 0.86),   #-- legal moves
        2 = hsv2rgb(5, 0.77, 0.753),    #-- legal captures
        3 = hsv2rgb(224,0.62,0.61),     #-- legal castle
        4 = hsv2rgb(204, 0.76, 0.86/3), #-- illegal moves
        5 = hsv2rgb(5, 0.77, 0.753/3),  #-- illegal captures
        6 = hsv2rgb(224,0.62,0.61/3)    #-- illegal castle
    )
    Time = curtime()
}


if (clk("validate")) {
    timer("validate", 15)
    
    while (perf(99) & VDI < VDN) {
        VDI++
        
        local From = ID
        local To = M[VDI, number]
        local P = T[To, number]
        
        local C = T:clone()

        C[To, number] = C[From, number]
        C[From, number] = 0

        
        #[
            1 = empty legal
            2 = attack legal
            3 = castle legal
            
            4 = empty illegal
            5 = attack illegal
            6 = castle illegal
            
        ]#
        
        local Legal = 1 + abs(sign(P))
        VDL[To, number] = Legal
        
        local Sign = -sign(KPW)
        for (N = 1, 6) {
            if (C:chess_moves_at_index(KPW, N*Sign):count()) { 
                VDL[To, number] = Legal + 3
            }
        }
        
        #holoColor(To, Colors[VDL[To, number], vector])
        
        if (VDI == VDN) {
            #printTable(VDL)
            print( curtime() - Time )
            #break    
        }
        
        #C:chess_print()
    }
}

