# © Spark Universe, 2023
# authors: destruc7i0n
# description: main loop of the tutorial, run as the player
# called by: ../loop

tag @s add instance_player

# store the player's id in the scoreboard for the duration of the loop
scoreboard players operation .tut_player_id player_id = @s player_id
scoreboard players set .target_count tut 0

# find the current instance, assign it to the current_instance tag
# tag the instance marker for the player
execute as @e[tag=tutorial_instance] if score @s player_id = .tut_player_id player_id run tag @s add current_instance
# tag pet in pet-related stages
execute as @e[type=spark:tutorial_marker,tag=current_instance] if score @s tut matches 5.. as @e[family=pet,tag=tamed] if score @s pet_id = .tut_player_id player_id run tag @s add instance_pet

# tag the targets for the instance
execute as @e[tag=tutorial_target] if score @s tut = .tut_player_id player_id run tag @s add instance_target
execute as @e[tag=instance_target] run scoreboard players add .target_count tut 1

# init the timer/state
scoreboard players add @e[tag=current_instance] tut_timer 0

execute as @e[type=spark:tutorial_marker,tag=current_instance] at @s if score @s tut matches 1 run function spark/tutorial/p1/loop
execute as @e[type=spark:tutorial_marker,tag=current_instance] at @s if score @s tut matches 2 run function spark/tutorial/p2/loop
execute as @e[type=spark:tutorial_marker,tag=current_instance] at @s if score @s tut matches 3 run function spark/tutorial/p3/loop
execute as @e[type=spark:tutorial_marker,tag=current_instance] at @s if score @s tut matches 4 run function spark/tutorial/p4/loop
execute as @e[type=spark:tutorial_marker,tag=current_instance] at @s if score @s tut matches 5 run function spark/tutorial/p5/loop
execute as @e[type=spark:tutorial_marker,tag=current_instance] at @s if score @s tut matches 6 run function spark/tutorial/p6/loop
execute as @e[type=spark:tutorial_marker,tag=current_instance] at @s if score @s tut matches 7 run function spark/tutorial/p7/loop

tag @s remove instance_player
tag @e[tag=current_instance] remove current_instance
tag @e[tag=instance_pet] remove instance_pet
tag @e[tag=instance_target] remove instance_target

scoreboard players reset .tut_player_id player_id
scoreboard players reset .target_count tut
