@name array3
@inputs  
@outputs 
@persist Board:array 
@persist Runtime Test Colors:array
@trigger none

if (first()) {

        #[
            1 = empty legal
            2 = attack legal
            3 = castle legal
            
            4 = empty illegal
            5 = attack illegal
            6 = castle illegal
        ]#

    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
    )
            
    #include "test_wtf"    
    
    KingPosW = 61
    KingPosB = 5
    
    BD = "
        r n b q k b n r
        p p p p p p p p
        . . . . q . P .
        . . . . Q . . .
        . . . . . . . .
        . . . q . . p .
        P P P P . P P P
        R N B Q K B N R
    ":replaceRE("%s", "")

#[    BD = "
        . . . . . . . .
        . . . . . . . .
        . . . . . . Q .
        . . . . . . . .
        . . . . . . . .
        . . . n . . . .
        . . q . . . p .
        . . . . K . . .
    ":replaceRE("%s", "")]#
            
    Board = chess_create(BD)
    
    Board[45, number] = Board[44, number]
    Board[44, number] = 0
    
    print( Board:evaluate(44, 45) )
    
    #Board:chess_print()
    #print( Board:chess_moves_at_index(44, 0))
    
    Runtime = curtime()

#[    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 (Board[N, number]) {
                holoCreate(N+1000, holoEntity(N):pos(), Sc, A90 )  
                holoColor(N+1000, Board[N, number] > 0 ? W : B + vec(60))
                holoModel(N+1000, "models/sprops/misc/alphanum/alphanum_" + ValuePiece[Board[N, number], string]:lower() + ".mdl")
            }            
        }
    }
]#    
    function number getColor(Index) {
        return (floor((Index - 1) / 8) + mod(Index - 1, 8)) % 2 == 0
    }
        
    #timer("validate", 100)
}

if (clk("validate")) {
    timer("validate", 15)
    
    if (Board:evaluate(1)) {
        printTable(EvalList)
        stoptimer("validate")  
        timer("run", 0)  
        print(EvalLegalCount)
    }
}

if (clk("run")) {
    timer("run", 400)
    
    
    
    holoColor(Test, select(getColor(Test) + 1, vec(255), vec()))
    foreach (K, N:number = EvalList[Test, array]) { 
        holoColor(K, select(getColor(K) + 1, vec(255), vec()))
    }
    
    Test++
    #Test = 10
    holoColor(Test, vec(241, 196, 15))
    foreach (K, N:number = EvalList[Test, array]) { 
        holoColor(K, Colors[N, vector])
    }
    
    if (Test > 63) { Test = 0 }    
}
