@name hexc6
@inputs 
@outputs 
@persist 
@trigger 


Files = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "K", "L")
Ranks = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11")

local Tiles = table()
local Index = 0

for (I = 0, 5) {
    for (J = 0, 5 + I) {
        Tiles[Index, table] = table(
            "index" = Index, 
            "file"  = Files[I + 1, string], 
            "rank"  = Ranks[J + 1, string],
            "neighbors" = array()
        )
        Index++
    }
}


for (I = 6, 10) {
    for (J = 0, 15 - I) {
        Tiles[Index, table] = table(
            "index" = Index, 
            "file"  = Files[I + 1, string], 
            "rank"  = Ranks[J + 1, string],
            "neighbors" = array()
        )
        Index++
    }
}





for (Index = 0, 90) {
    local This = Tiles[Index, table]
    
    local Neighbors = This["neighbors", array]
    local Rank = This["rank", string]
    local File = This["file", string]

    if (Index != 90) {
        if (File == Tiles[Index + 1, table]["file", string]) {
            Neighbors[1, number] = Index + 1 
        }
    }
    
    if (Index != 0) {
        if (File == Tiles[Index - 1, table]["file", string]) {
            Neighbors[2, number] = Index - 1  
        }           
    }
    
    local ByteFile = toByte(File) 
    
    if (ByteFile < 70) { #abcde
        Neighbors[3, number] = Index + 7 + ByteFile
        Neighbors[5, number] = Index + 6 + ByteFile
        
        if (File != "A") {
            if (File == Tiles[Index + 1, table]["file", string]) {
                Neighbors[4, number] = Index - 5 + ByteFile
            } 
            
            if (Rank != "1") {
                Neighbors[6, number] = Index - 6 + ByteFile
            }  
        }
    }
    elseif (File == "F") { #f
        if (Rank != "11") {
            Neighbors[3, number] = Index + 11
            Neighbors[4, number] = Index - 10
        }
        if (Rank != "1") {
            Neighbors[5, number] = Index + 10
            Neighbors[6, number] = Index - 11
        }
    }
    else { #GHIKL
        Neighbors[4, number] = Index - 11 + ByteFile - 70
        Neighbors[6, number] = Index - 12 + ByteFile - 70
        
        if (File != "L") {
            if (File == Tiles[Index + 1, table]["file", string]) {
                Neighbors[3, number] = Index + 11 + 70 - ByteFile
            }
            if (Rank != "1") {
                Neighbors[5, number] = Index + 10 + 70 - ByteFile
            } 
        }
    }
}


local TileStart = 0
local TileInner = TileStart

local Colors = table(
 0 = 1,
 1 = 2,
 2 = 3
)


Tiles[0, table]["color", number] = 1

for (Index = 1, 90) {
    local This = Tiles[Index, table]

    local Rank = This["rank", string]
    local File = This["file", string]
    local ByteFile = toByte(File)
    
    if (File != Tiles[Index - 1, table]["file", string]) {
        if (ByteFile < 71) {
            TileStart++
            if (TileStart > 2) {
                TileSTart = 0
            }    
        }    
        else {
            TileStart--
            if (TileStart < 0) {
                TileStart = 2
            }
        }
        
        This["color", number] = Colors[TileStart, number]
        TileInner = TileStart
    }
    else {
        TileInner++
        if (TileInner > 2) {
            TileInner = 0
        }    
        This["color", number] = Colors[TileInner, number]
    }
}



local E = entity()
local TileDimZ = 12
local TileDimX = 12
local Dimension = 6

local XPos = 0
local ZPos = 0

for (I = 0, 90) {
    local This = Tiles[I, table]
    local File = This["file", string]
    local Rank = This["rank", string]
    
    local XF = toByte(File) - 64
    local XY = Rank:toNumber()


    holoCreate(I, vec(XF, XY, 0))
#[    local This = Tiles[I, table]

    local File = This["file", string]
    local ByteFile = toByte(File)
    
    if (I != 0 & File != This[I - 1, table]["file", string]) {
        XPos += TileDimX/2   
        
        if (ByteFile < toByte(Files[Dimension, string])) {
            ZPos += (TileDimZ/2 * ByteFile)
        }
        else {
            ZPos += (TileDimZ/2 * (Dimension*2 - 2 - ByteFile))
        }
    }
    This["position", vector] = vec(XPos, ZPos, 0)
    ZPos -= TileDimZ
    
    holoCreate(I, vec(XPos, 0, 0))]#
}

#fileWrite( "test.txt", vonEncode(Tiles) )
#[ 
            float XPos = 0.0f;
            float ZPos = 0.0f;
            float tileDimZ = 46.0f;
            float tileDimX = 78.0f;
            int _dimension = 6;
            for (int i = 0; i < tiles.Length; i++)
            {
                // On a new file
                if (i != 0 && tiles[i].file != tiles[i - 1].file)
                {
                    XPos += tileDimX / 2;    // something to this effect, shift enough over to interlock

                    switch (variant)
                    {
                        case GameVariant.Glinsky:
                        case GameVariant.McCooey:
                            if (tiles[i].file < (File)_dimension)
                            {
                                ZPos = +(tileDimZ / 2) * (int)tiles[i].file;
                            }
                            else
                            {
                                ZPos = +((tileDimZ / 2) * ((int)(_dimension * 2) - 2 - (int)tiles[i].file));
                            }
                            break;
                        case GameVariant.Shafran:
                            if (tiles[i].file < File.F)
                            {
                                ZPos = +(tileDimZ / 2) * (int)tiles[i].file;
                            }
                            else
                            {
                                ZPos = +((tileDimZ / 2) * ((int)File.I - (int)tiles[i].file));
                            }
                            break;
                    }
                }

                tiles[i].Position = new Vector3(XPos, 0.0f, ZPos);
                ZPos -= tileDimZ;
            }
        }]#
