import os

l   = ['n','s','e','w','spawn','middle','armory','boss','item','player']
# unicode free uE00 - F8FF
# F800 -F8FF for custom textures
# Room range e002 - e032 -> 30 -> cada sala té el mateix valor base de posició i un desfasament de (30 + 1)* index_l = 7*7*i = (slots^2)*i

slots = 7

p = 'address'
unicode = 0xE002
if not os.path.exists(p):
    os.mkdir(p)
    print("Directory ", p , " Created ")

address = open("address.mcfunction", "w")
for line in range(slots):

    p = 'address/line{0}'.format(line)
    if not os.path.exists(p):
        os.mkdir(p)
        print("Directory ", p , " Created ")

    address.write("\nexecute if score line map matches {0} at @s run function game:map/get/address/line{0}".format(line))

    f_line = open("address/line{0}.mcfunction".format(line), "w")

    for column in range(slots):

        p = 'address/line{0}/column{1}'.format(line, column)
        if not os.path.exists(p):
            os.mkdir(p)
            print("Directory ", p , " Created ")

        f_line.write("\nexecute if score column map matches {1} at @s run function game:map/get/address/line{0}/column{1}".format(line, column))

        f_column = open("address/line{0}/column{1}.mcfunction".format(line, column), "w")
        # === Rooms ===
        for e in l[4:8]:
            f_column.write("\nexecute if entity @e[tag={0},distance=..0.1,limit=1] run data modify storage map rooms append value '{1}\\\\u{2:X}{1}'".format(e, '"', unicode + slots**2*l.index(e) ))
        # === Others ===
        for e in l[8:]:
            f_column.write("\nexecute positioned ~-22.5 ~ ~-22.5 if entity @e[type={0},tag=!mapping,dx=46,dy=32,dz=46,limit=1] run data modify storage map others append value '{1}\\\\u{2:X}{1}'".format(e, '"', unicode + slots**2*l.index(e) ))
        # === Doors ===
        for r in [0, 90, 180, 270]:
            f_column.write("\nexecute if score rotation map matches {0} run function game:map/get/address/line{1}/column{2}/rotation{0}".format(r, line, column))

            f_rotation = open("address/line{0}/column{1}/rotation{2}.mcfunction".format(line, column, r), "w")
            if r == 180:
                l_cardinal = l[:4]
            elif r == 0:
                l_cardinal = ['s','n','w','e']
            elif r == 270:
                l_cardinal = ['e','w','s','n']
            else:
                l_cardinal = ['w','e','n','s']

            for i,e in enumerate(l_cardinal):
                f_rotation.write("\nexecute if entity @e[tag={0},tag=gate,distance=..23,limit=1] run data modify storage map doors append value '{1}\\\\u{2:X}{1}'".format(e.upper(), '"', unicode + slots**2*i))
            #f_rotation.write("\nsay " + str(r))
            f_rotation.close()

        #f_column.write("\nsay {0}{1}".format(line, column))
        f_column.close()
        unicode += 1

    f_line.close()

address.close()
#[{{"translate":"space.-100"}},{{"translate":"space.-125"}}]'
