@name hcgui4
@inputs 
@outputs 
@persist Game:table UI_Models:table [UI_CellRadius] [UI_CellColorL UI_CellColorM UI_CellColorD]:vector Entity:entity SpawnIndex
@trigger none


if (first()) {

    #include "games/chess/hex/lib/lib"

    #----------------------------------
    #-- User Settings
    local Fen = "6/P5p/RP4pr/N1P3p1n/Q2P2p2q/BBB1P1p1bbb/K2P2p2k/N1P3p1n/RP4pr/P5p/6"

    Game = game_from_fen(Fen)

    UI_CellRadius = 32
    UI_CellColorL = vec(255, 255, 255)
    UI_CellColorM = vec(125, 125, 125)
    UI_CellColorD = vec( 45,  45,  45)

    UI_Models = table(
        "r" = "models/props_phx/games/chess/black_rook.mdl",
        "n" = "models/props_phx/games/chess/black_knight.mdl",
        "b" = "models/props_phx/games/chess/black_bishop.mdl",
        "q" = "models/props_phx/games/chess/black_queen.mdl",
        "k" = "models/props_phx/games/chess/black_king.mdl",
        "p" = "models/props_phx/games/chess/black_pawn.mdl",
        "R" = "models/props_phx/games/chess/white_rook.mdl",
        "N" = "models/props_phx/games/chess/white_knight.mdl",
        "B" = "models/props_phx/games/chess/white_bishop.mdl",
        "Q" = "models/props_phx/games/chess/white_queen.mdl",
        "K" = "models/props_phx/games/chess/white_king.mdl",
        "P" = "models/props_phx/games/chess/white_pawn.mdl"
    )
        
    Entity = entity()
    
    #----------------------------------
    #-- UI Setup
    function spawn_hexagon(Index) {
        
        local File = getFile(Index)
        local Rank = getRank(Index)
    
        local MeshX = UI_CellRadius*0.5*sqrt(3)
        local MeshY = UI_CellRadius*0.75
        local MeshZ = sqrt(UI_CellRadius)
    
        local Pos = vec(-MeshX*3, MeshY*6, MeshZ*0.5)
        local Col = select(getColor(Index, 1), UI_CellColorL, UI_CellColorM, UI_CellColorD) 
        
        switch (File < 7) {
            case 1, Pos = Pos + vec(Rank*MeshX - File*MeshX*0.5, -File*MeshY, 0), break
            case 0, Pos = Pos + vec(Rank*MeshX + (File - 12)*MeshX*0.5, -File*MeshY, 0), break
        }
        
        local Hologram = holoCreate(Index, Entity:toWorld(Pos), vec(UI_CellRadius -1):setZ(MeshZ)/12, Entity:angles(), Col, "models/holograms/hexagon.mdl")
        
        holoParent(Index, Entity)
        holoMaterial(Index, "phoenix_storms/fender_white")
        
        findIncludeEntity(Hologram)
    
    }
    
    function number spawn_piece(Index, Piece:string) {
        local Hash = hash(getNotation(Index))
        local IsBlack = isBlack(Piece)
        
        local Pos = holoEntity(Index):toWorld(vec(0, 0, sqrt(UI_CellRadius)/2))
        local Col = IsBlack ? mix(UI_CellColorD, UI_CellColorM, 0.75) : UI_CellColorL
        
        local Hologram = holoCreate(Hash, Pos, vec(UI_CellRadius - 1)/24, Entity:toWorld(ang(0, IsBlack*180, 0)), Col, UI_Models[Piece, string])
        holoParent(Hash, Entity)
        holoMaterial(Hash, "phoenix_storms/fender_white")
        
        findExcludeEntity(Hologram)
        
        return Hash
    }

}

if (SpawnIndex < 91) {
    interval(60)

    if (holoCanCreate()) {
        SpawnIndex++
        
        local Cell = Game[SpawnIndex, table]
        
        #-- Board table setup
        Cell["neighbors", array] = getNeighbors(SpawnIndex)
        
        #-- UI setup
        spawn_hexagon(SpawnIndex)
        if (Cell["piece_type", string] != ".") {
            Cell["piece_holo", number] = spawn_piece(SpawnIndex, Cell["piece_type", string])
        }
    }
    
}
else {
    printTable(Game)
}
