import os

from numpy import mat

l   = ['north','south','east','west','spawn','enemy','challenge','trapped','armory','shop','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
unicode = 0xE002

def make_dir(path):
    if not os.path.exists(path):
        os.mkdir(path)
        print("Directory ", path , " Created ")


# Dirs
make_dir('address')
for row in range(slots):
    make_dir(f'address/row{row}')
    for column in range(slots): 
        make_dir(f'address/row{row}/column{column}')
        make_dir(f'address/row{row}/column{column}/gate')

# Add root function (calls to row root function)
with open("address.mcfunction", "w") as file:                       
    for row in range(slots):
        file.write(f"execute if score row map matches {row} at @s run function game:map/get/address/row{row}\n") 

# Add root row function for each row (calls to each column root function)
for row in range(slots):
    with open(f"address/row{row}.mcfunction", "w") as file:          
        for column in range(slots):
            file.write(f"execute if score column map matches {column} at @s run function game:map/get/address/row{row}/column{column}\n")

for row in range(slots):
    for column in range(slots):
        with open(f"address/row{row}/column{column}.mcfunction", "w") as file: # Add root column function
            # file.write(f"say {row}-{column}")
            
            # === Rooms ===
            file.write(f"execute as @e[type=marker,tag=center,distance=..0.1,limit=1] run function game:map/get/address/row{row}/column{column}/room\n")
            
            with open(f"address/row{row}/column{column}/room.mcfunction", "w") as file2:
                for tag in l[4:11]:
                    uni = unicode + slots**2*l.index(tag)
                    file2.write(f"""execute if entity @s[tag={tag}] run data modify storage map rooms append value '"\\\\u{uni:X}"'\n""")

            # === Others ===
            for e in l[11:]:
                uni = unicode + slots**2*l.index(e)
                file.write(f"""execute positioned ~-22.5 ~ ~-22.5 if entity @e[type={e},tag=!map_ignore,dx=46,dy=32,dz=46,limit=1] run data modify storage map others append value '"\\\\u{uni:X}"'\n""")

            # === Doors ===
            file.write(f"execute as @e[type=marker,tag=gate,distance=..23.5,limit=4] run function game:map/get/address/row{row}/column{column}/gate/main\n")

            with open(f"address/row{row}/column{column}/gate/main.mcfunction", "w") as file2:
                for cardinal in l[:4]:
                    file2.write(f"execute if entity @s[tag={cardinal}] run function game:map/get/address/row{row}/column{column}/gate/{cardinal}\n")

            rots = {
                0: ['south','north','west','east'],
                90: ['west','east','north','south'],
                180: l[:4],
                270: ['east','west','south','north']
            }
            for cardinal in l[:4]:
                with open(f"address/row{row}/column{column}/gate/{cardinal}.mcfunction", "w") as file2:
                    # file2.write(f"say {cardinal}\n")
                    for rot, list in rots.items():
                        i = list.index(cardinal)
                        uni = unicode + slots**2*i
                        file2.write(f"""execute if score rotation map matches {rot} run data modify storage map doors append value '"\\\\u{uni:X}"'\n""")

            unicode += 1

#[{{"translate":"space.-100"}},{{"translate":"space.-125"}}]'
