@name hcgui3
@inputs 
@outputs 
@persist Game:table UI:table
@trigger none


if (first()) {
    
    #include "games/chess/hex/lib/lib"

    local Fen = "6/P5p/RP4pr/N1P3p1n/Q2P2p2q/BBB1P1p1bbb/K2P2p2k/N1P3p1n/RP4pr/P5p/6"
         
    function table decode_fen( FEN:string ) {
    
        local Explode = FEN:explode("/")
        local String = ""
        
        for (File = 1, 11) {
            local Ranks = Explode[File, string]
            for (Rank = 1, Ranks:length()) {
                local Empty = Ranks[Rank]:toNumber()
                if (Empty) {
                    String = String + ".":repeat(Empty)
                }
                else {
                    String = String + Ranks[Rank]
                }
            }
        } 
        
        return table(
            "game_string" = String
        )
    
    }
    
    function table game_from_fen( FEN:string ) {
        
        local Return = table()
        
        local Decode = decode_fen( FEN )
        local String = Decode["game_string", string]
    
        for (Index = 1, 91) {
            Return[Index, table] = table(
                "game_piece" = String[Index]
            )    
        }
        
        return Return
    
    }
    
    Game = game_from_fen(Fen)

    local Settings = table(
        "cell_size" = 32,
        "cell_col_l" = vec(255, 255, 255),
        "cell_col_m" = vec(125, 125, 125),
        "cell_col_d" = vec( 25,  25,  25)
    )

    UI = table(
        "cell_meshx"  = Settings["cell_size", number]*0.5*sqrt(3),
        "cell_meshy"  = Settings["cell_size", number]*0.75,
        "cell_center" = vec(Settings["cell_size", number]*sqrt(3)*-1.5, Settings["cell_size", number]*4.5, 0),
        "cell_scale"  = vec(Settings["cell_size", number]):setZ(sqrt(Settings["cell_size", number])),
        "cell_index"  = 0,
        
        "game_owner"  = owner(),
        "game_player" = owner(),
        "game_entity" = entity()
    )
    
    UI = UI:add(Settings)

}

if (UI["cell_index", number] < 91) {
    interval(60)

    while (perf() & holoCanCreate() & UI["cell_index", number] < 91) {
        UI["cell_index", number] = UI["cell_index", number] + 1

        local Index = UI["cell_index", number]
        local Piece = Game[Index, table]["game_piece", string]
        
        Game[Index, table]["neighbors", array] = getNeighbors(Index)


    }

} else {

}



