import os

scale = 12000
scaleVar = 0.001
step = 32
def genSubFile(x, y, i):
    if x != y-1 and x != y:
        k = open("C:/Users/chris/AppData/Roaming/.minecraft/saves/Parkour With Me/datapacks/Recorder/data/main/functions/recorder/getter/z_"+str(x)+"_"+str(y)+".mcfunction", "w+")
        k.write("execute if entity @s[scores={play_step="+str(x)+".."+str(x+(y-x)//2)+"}] run function main:recorder/getter/z_"+str(x)+"_"+str(x+(y-x)//2)+"\n")
        k.write("execute if entity @s[scores={play_step="+str(x+(y-x)//2+1)+".."+str(y)+"}] run function main:recorder/getter/z_"+str(x+(y-x)//2+1)+"_"+str(y)+"\n")
        k.close()
        
        genSubFile(x, x+(y-x)//2, i+1)
        genSubFile(x+(y-x)//2+1, y, i+1)
    else:
        k = open("C:/Users/chris/AppData/Roaming/.minecraft/saves/Parkour With Me/datapacks/Recorder/data/main/functions/recorder/getter/z_"+str(x)+"_"+str(y)+".mcfunction", "w+")
        k.write("execute if entity @s[scores={play_step="+str(x)+"}] run function main:recorder/getter/"+str(x)+"\n")
        k.write("execute if entity @s[scores={play_step="+str(y)+"}] run function main:recorder/getter/"+str(y)+"\n")
        k.close()
    

g = open("C:/Users/chris/AppData/Roaming/.minecraft/saves/Parkour With Me/datapacks/Recorder/data/main/functions/recorder/play.mcfunction", "w+")

for j in range(0,scale,step):
    f = open("C:/Users/chris/AppData/Roaming/.minecraft/saves/Parkour With Me/datapacks/Recorder/data/main/functions/recorder/getter/"+str(j)+".mcfunction", "w+")

    for i in range(j, j+step, 2):
        i = i+1
        f.write("execute as @s if entity @s[tag=!Loop,scores={play_step="+str(i)+"}] run bossbar set minecraft:record_time value "+str(i)+"\n")
        f.write("execute as @s if score @s play_step matches "+str(i)+" store result entity @s Pos[0] double "+str(scaleVar)+" run scoreboard players get @s record_"+str(i)+"_x\n")
        f.write("execute as @s if score @s play_step matches "+str(i)+" store result entity @s Pos[1] double "+str(scaleVar)+" run scoreboard players get @s record_"+str(i)+"_y\n")
        f.write("execute as @s if score @s play_step matches "+str(i)+" store result entity @s Pos[2] double "+str(scaleVar)+" run scoreboard players get @s record_"+str(i)+"_z\n")
        f.write("execute as @s if score @s play_step matches "+str(i)+" store result entity @s Rotation[0] float "+str(scaleVar)+" run scoreboard players get @s record_"+str(i)+"_u\n")
        f.write("execute as @s if score @s play_step matches "+str(i)+" store result entity @s Pose.Head[0] float "+str(scaleVar)+" run scoreboard players get @s record_"+str(i)+"_v\n")
        f.write("execute as @s if score @s play_step matches "+str(i)+" if score @s record_"+str(i)+"_w matches 1 run tag @s add Walking\n")
        if i == 0:
            f.write("execute as @s if score @s play_step matches "+str(i)+" if score @s record_"+str(i)+"_w matches 0 run tag @s remove Walking\n")
        elif i == 1:
            f.write("execute as @s if score @s play_step matches "+str(i)+" if score @s record_"+str(i-1)+"_w matches 0 if score @s record_"+str(i)+"_w matches 0 run tag @s remove Walking\n")
        else:
            f.write("execute as @s if score @s play_step matches "+str(i)+" if score @s record_"+str(i-2)+"_w matches 0 if score @s record_"+str(i-1)+"_w matches 0 if score @s record_"+str(i)+"_w matches 0 run tag @s remove Walking\n")
            
        f.write("execute as @s if score @s play_step matches "+str(i)+" if score @s record_"+str(i)+"_h matches 1 run tag @s add Hit\n")
        f.write("execute as @s if score @s play_step matches "+str(i)+" if score @s record_"+str(i)+"_h matches 0 run tag @s remove Hit\n\n")

    f.close()

    g.write("execute as @e[scores={play_step="+str(j)+".."+str(j+step-1)+"},tag=Replay] run function main:recorder/getter/"+str(j)+"\n")
    
#g.write("execute as @e[scores={play_step="+str(0)+".."+str(scale//2)+"},tag=Replay] run function main:recorder/getter/z_"+str(0)+"_"+str(scale//2)+"\n")
#g.write("execute as @e[scores={play_step="+str(scale//2+1)+".."+str(scale)+"},tag=Replay] run function main:recorder/getter/z_"+str(scale//2+1)+"_"+str(scale)+"\n")

g.write("scoreboard players add @e[tag=Replay] play_step 1\n")
g.write("execute as @e[tag=Replay,tag=Loop] if score @s play_step = @s play_length run scoreboard players set @s play_step 0\n")
g.write("execute unless entity @e[tag=Replay] run tag @e[tag=Loop] add Replay\n")
g.write("execute if entity @e[scores={play_step="+str(scale)+"},tag=playing] run function main:recorder/stop_play\n")
#g.write("""execute store result bossbar minecraft:record_time value run scoreboard players get @e[tag=Replay,limit=1,tag=Display,tag=!Loop] play_step\n""")
g.write("""bossbar set minecraft:record_time players @a""")

g.close()
            
