@name hexc11
@inputs 
@outputs
@persist [GM_Board GM_Ranks GM_Files GM_Values]:table GM_Index 
@persist [UI_HoloIndex] [UI_HexD UI_HexMX UI_HexMY] [UI_Ent UI_Own]:entity UI_HexOXY:vector UI_PModels:table UI_HexColors:array
@trigger none

if (first()) {

    #---------------------------------------------
    #-- UI Setup

    UI_HexColors = array(vec(20, 20, 20), vec(125, 125, 125), vec(255, 255, 255))
    
    UI_HexD   = 32                              #cell diameter
    UI_HexMX  = 0.5*UI_HexD*sqrt(3)             #cell mesh x value
    UI_HexMY  = 0.5*UI_HexD + 0.25*UI_HexD      #cell mesh y value
    UI_HexOXY = vec(-UI_HexMX*3, UI_HexMY*6, 0) #center board on e2

    #--
    UI_Ent = entity()
    UI_Own = owner()
    
    findClearWhiteList()
    findClearBlackList()
    
    findIncludePlayerProps(UI_Own)

    #--
    UI_PModels = 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"
    )

    function number newHoloCell( Index, Pos:vector ) {
        holoPos(Index, Pos)
        holoModel(Index, "hexagon")
        holoScaleUnits(Index, vec(UI_HexD - 1):setZ(1))
    }
    
    function number newHoloPiece( Index, Cell, Piece:string ) {
        holoPos(Index, holoEntity(Cell):toWorld(vec(0, 0, 0.5)))
        holoModel(Index, UI_PModels[Piece, string])
        holoScale(Index, vec(UI_HexD - 1)/24)
        
        local Team = inrange(toByte(Piece), 96, 122)
        holoColor(Index, Team ? UI_HexColors[1, vector] + vec(35,35,35) : UI_HexColors[3, vector])
        holoAng(Index, UI_Ent:toWorld(ang(0, Team*180, 0)))
        #holoDisableShading(Index, 0)
    }
    
    #---------------------------------------------
    #-- Game Setup

    GM_Ranks = table(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
    GM_Files = table("A", "B", "C", "D", "E", "F", "G", "H", "I", "K", "L")
                     #1   #2   #3   #4   #5   #6   #7   #8   #9   #10  #11

    GM_Values = table(
        "A" = 0,
        "B" = 6,
        "C" = 6 + 7,
        "D" = 6 + 7 + 8,
        "E" = 6 + 7 + 8 + 9,
        "F" = 6 + 7 + 8 + 9 + 10,
        "G" = 6 + 7 + 8 + 9 + 10 + 11,
        "H" = 6 + 7 + 8 + 9 + 10 + 11 + 10,
        "I" = 6 + 7 + 8 + 9 + 10 + 11 + 10 + 9,
        "K" = 6 + 7 + 8 + 9 + 10 + 11 + 10 + 9 + 8,
        "L" = 6 + 7 + 8 + 9 + 10 + 11 + 10 + 9 + 8 + 7
    )
    
    function number getN( RF:string ) {
        return GM_Values[RF[1], number] + RF:sub(2):toNumber()
    }
    
    function table:setPiece( RF:string, Piece:string ) {
        This[getN(RF), table]["piece", string] = Piece    
    }     
    
    function table:initPiece( Index, RF:string, Piece:string ) {
        local N = getN(RF)
        This[N, table]["piece", string] = Piece
        This[N, table]["pieceN", number] = Index
        
        newHoloPiece( Index, N, Piece )
    }

}


#---------------------------------------------
#-- Spawn hologram pool for squares and pieces

if (UI_HoloIndex < 127) {
    interval(15)

    while (holoCanCreate() & UI_HoloIndex < 127) {
        UI_HoloIndex++
        
        holoCreate(UI_HoloIndex)
        holoParent(UI_HoloIndex, entity())
        holoAng(UI_HoloIndex, entity():angles())
        #holoDisableShading(UI_HoloIndex, 1)
        holoMaterial(UI_HoloIndex, "phoenix_storms/fender_white")
    }
    
    if (UI_HoloIndex >= 127) {
        
        #---------------------------------------------
        #-- Setup the board
 
        local ColorStart = 0
        local ColorInner = 0
        
        for (I = 0, 5) {
            for (J = 0, 5 + I) {
                GM_Index++
                GM_Board[GM_Index, table] = table(
                    "rank" = GM_Ranks[J + 1, number], # integer 1-11
                    "file" = GM_Ranks[I + 1, number], # integer 1-11
                    "cell" = GM_Files[I + 1, string] + GM_Ranks[J + 1, number]
                )
                
                local Rank = GM_Board[GM_Index, table]["rank", number]
                local File = GM_Board[GM_Index, table]["file", number]
                
                newHoloCell( GM_Index, UI_Ent:toWorld(UI_HexOXY + vec(Rank*UI_HexMX - File*UI_HexMX*0.5, -File*UI_HexMY, 0)) )
                #holoPos(GM_Index, UI_Ent:toWorld(UI_HexOXY + vec(Rank*UI_HexMX - File*UI_HexMX*0.5, -File*UI_HexMY, 0)))

                if (File != GM_Board[GM_Index - 1, table]["file", number]) {
                    if (File < 7) {
                        ColorStart++
                        if (ColorStart > 2) { ColorStart = 0 }    
                    } 
                    else {
                        ColorStart--
                        if (ColorStart < 0) { ColorStart = 2 }
                    }
                    
                    holoColor(GM_Index, UI_HexColors[ColorStart + 1, vector])
                    ColorInner = ColorStart
                }
                else {
                    ColorInner++
                    if (ColorInner > 2) { ColorInner = 0 }
                    holoColor(GM_Index, UI_HexColors[ColorInner + 1, vector])
                }
            }
        }

        for (I = 6, 10) {
            for (J = 0, 15 - I) {
                GM_Index++
                GM_Board[GM_Index, table] = table(
                    "rank" = GM_Ranks[J + 1, number], # integer 1-11
                    "file" = GM_Ranks[I + 1, number], # integer 1-11
                    "cell" = GM_Files[I + 1, string] + GM_Ranks[J + 1, number]
                )

                local Rank = GM_Board[GM_Index, table]["rank", number]
                local File = GM_Board[GM_Index, table]["file", number]
 
                newHoloCell( GM_Index, UI_Ent:toWorld(UI_HexOXY + vec(Rank*UI_HexMX + (File - 12)*UI_HexMX*0.5, -File*UI_HexMY, 0)))
               
                if (File != GM_Board[GM_Index - 1, table]["file", number]) {
                    if (File < 7) {
                        ColorStart++
                        if (ColorStart > 2) { ColorStart = 0 }    
                    } 
                    else {
                        ColorStart--
                        if (ColorStart < 0) { ColorStart = 2 }
                    }
                    
                    holoColor(GM_Index, UI_HexColors[ColorStart + 1, vector])
                    ColorInner = ColorStart
                }
                else {
                    ColorInner++
                    if (ColorInner > 2) { ColorInner = 0 }
                    holoColor(GM_Index, UI_HexColors[ColorInner + 1, vector])
                }
            }
        }
        
        GM_Board:initPiece( 92, "B1", "P" )
        GM_Board:initPiece( 93, "C2", "P" )
        GM_Board:initPiece( 94, "D3", "P" )
        GM_Board:initPiece( 95, "E4", "P" )
        GM_Board:initPiece( 96, "F5", "P" )
        GM_Board:initPiece( 97, "G4", "P" )
        GM_Board:initPiece( 98, "H3", "P" )
        GM_Board:initPiece( 99, "I2", "P" )
        GM_Board:initPiece( 100, "K1", "P" )
        
        GM_Board:initPiece( 101, "C1", "R" )
        GM_Board:initPiece( 102, "I1", "R" )
        
        GM_Board:initPiece( 103, "D1", "N" )
        GM_Board:initPiece( 104, "H1", "N" )
        
        GM_Board:initPiece( 105, "E1", "Q" )
        GM_Board:initPiece( 106, "G1", "K" )
        
        GM_Board:initPiece( 107, "F1", "B" )
        GM_Board:initPiece( 108, "F2", "B" )
        GM_Board:initPiece( 109, "F3", "B" )
        
        
        
        
        
        
        
        GM_Board:initPiece( 110, "B7", "p" )
        GM_Board:initPiece( 111, "C7", "p" )
        GM_Board:initPiece( 112, "D7", "p" )
        GM_Board:initPiece( 113, "E7", "p" )
        GM_Board:initPiece( 114, "F7", "p" )
        GM_Board:initPiece( 115, "G7", "p" )
        GM_Board:initPiece( 116, "H7", "p" )
        GM_Board:initPiece( 117, "I7", "p" )
        GM_Board:initPiece( 118, "K7", "p" )
        
        GM_Board:initPiece( 119, "C8", "r" )
        GM_Board:initPiece( 120, "I8", "r" )
        
        GM_Board:initPiece( 121, "D9", "n" )
        GM_Board:initPiece( 122, "H9", "n" )
        
        GM_Board:initPiece( 123, "E10", "q" )
        GM_Board:initPiece( 124, "G10", "k" )
        
        GM_Board:initPiece( 125, "F11", "b" )
        GM_Board:initPiece( 126, "F10", "b" )
        GM_Board:initPiece( 127, "F9",  "b" )
    }
}

