@name hexc2
@inputs 
@outputs 
@persist Hex:table N E:entity Scale:vector HoloN STR RTS POS:table POSN
@trigger 

if (first()) {
function table board( Width ) {
    local Temp = table(
        "width"           = Width,
        "fieldSideLength" = Width/17,
        "height"          = 11*(Width/17)*sqrt(3),
        "fields" = table(),
        "colors" = array(vec(45), vec(125), vec(200))
    )
    
    return Temp
}

function table table:draw() {
    local A = This["fieldSideLength", number]
    local FieldsInRow = 6
    local RowLetters = "abcdefghijk"
    
    local OppositeColorOrder = 0
    local ColorNum = 0
    local RowsCounter = 1
    
    for (I = 0, 10) {
        This["fields", table][RowLetters[I + 1], table] = table()
        
        local X = I*3/2*A

        if (I >= 6) {
            FieldsInRow--
            OppositeColorOrder = 1
        }
        else {
            FieldsInRow++
        }
        
        switch (RowsCounter) {
            case 1, ColorNum = OppositeColorOrder ? 1 : 2, break
            case 2, ColorNum = OppositeColorOrder ? 2 : 1, break
            case 3, ColorNum = OppositeColorOrder ? 0 : 0, break
        }
        
        
        for (J = 1, FieldsInRow - 1) {
            local Color = This["colors", array][ColorNum + 1, vector]
            
            local Y = 0.5*This["height", number]+A*sqrt(3)/2*FieldsInRow - J*A*sqrt(3)
            
            POSN++
            POS[POSN, array] =    array( vec(X, Y, 0)    , Color )     
           # Field(rowsLetters[i], (i>5) ? j+i-5 : j, x, y, color);

            #local ID = I + 1 > 5 ? (J+I-5) : J
            
            #This["fields", table][RowLetters[I + 1], table][ID, vector] = vec(X, Y, 0)#array(RowLetters[I + 1], (I > 5) ? (J+I-5) : J, X, Y, Color)
            
            if (ColorNum <= 0) {
                ColorNum = 2
            }
            else {
                ColorNum--
            }
        }
        
        if (RowsCounter >= 3) {
            RowsCounter = 1
        }
        else {
            RowsCounter++
        }
        
    }
    return This
}

Hex = board(24):draw()


E = entity()
Scale = vec(sqrt(24) / sqrt(3)) / 12


STR = 1

}

interval(60)

while (holoCanCreate() & HoloN < 91) {
    HoloN++
    
    holoCreate(HoloN)
    holoModel(HoloN, "hexagon")
    holoScale(HoloN, Scale)
    holoParent(HoloN, E)
    
    holoPos(HoloN, E:toWorld(POS[HoloN, array][1, vector]))
    holoAng(HoloN, E:toWorld(ang(0,90,0)))
    holoColor(HoloN, POS[HoloN, array][2, vector])
}

#[if (HoloN >= 91) {
    
    if (STR < 11) {
        local String = "abcdefghijk"
        local Table = Hex["fields", table][String[STR], table]
        
        if (RTS < Table:count()) {
            RTS++
            
            N++
            holoPos(N, E:toWorld(Table[RTS, vector]))
            
            if (RTS >= Table:count()) {
                STR++
                RTS = 0
            }
        }
    }
}]#
#[    if (HoloN >= 91) {
        foreach (K, V:table = Hex["fields", table]) {
            for (I = 1, V:count()) {
                N++
                holoPos(N, E:toWorld(V[I, vector]))
                print(V[I, vector])
            }
        } 
    }]#

#[foreach (K, V:table = Hex["fields", table]) {
    for (I = 1, V:count()) {
        local T = V[I, array]
        
        N++
        holoCreate(N, E:toWorld(vec(T[3, number], T[4, number], 0)), Scale)
    }
}]#
