#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
#
#    This function has been created by Skilendarz Group.
#             https://skilendarz.com
#
#      GIVE MAP MAKERS THE CREDIT THEY DESERVE!
#             COPYING IS PROHIBITED!
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

scoreboard players add start time 1

#Start
execute if score start time matches 2 run function f:lobby/start/begin

#Floor animation
execute if score start time matches 5 run function f:lobby/start/floor_convert

execute if score start time matches 6..15 as @e[tag=floor_as_left] at @s run tp @s ~-0.4 ~ ~
execute if score start time matches 6..15 as @e[tag=floor_as_right] at @s run tp @s ~0.4 ~ ~
execute if score start time matches 6..15 as @a at @s run playsound minecraft:block.piston.extend master @s ~ ~ ~ 1 2

execute if score start time matches 15 run effect give @a minecraft:invisibility 1 0 true
execute if score start time matches 15 run effect give @a minecraft:jump_boost 3 255 true

#Tp to cave
execute if score $game_type main matches 0..2 if score start time matches ..100 run tp @a[x=315,y=101,z=-354,dx=6,dy=1,dz=6] 272.0 114 -319.0 -90 -90
execute if score $game_type main matches 0..2 if score start time matches ..100 as @a unless entity @s[x=315,y=107,z=-354,dx=6,dy=3,dz=6] unless entity @s[x=266,y=103,z=-320,distance=..22] run tp @s 272.0 114 -319.0 -90 -90
execute if score $game_type main matches 3 if score start time matches ..100 run tp @a[x=315,y=101,z=-354,dx=6,dy=1,dz=6] 256.0 111.00 -323.0 -90 -90
execute if score $game_type main matches 3 if score start time matches ..100 as @a unless entity @s[x=315,y=107,z=-354,dx=6,dy=3,dz=6] unless entity @s[x=266,y=103,z=-320,distance=..22] run tp @s 256.0 111.00 -323.0 -90 -90

#Team
execute as @a[tag=!team-selected] at @s if block ~ ~-2 ~ #f:concrete run function f:lobby/start/team-select
execute as @a[team=!lobby] at @s if block ~ ~-2 ~ gray_concrete run playsound minecraft:block.note_block.pling master @s ~ ~ ~ 1 0.5
execute as @a[team=!lobby] at @s if block ~ ~-2 ~ gray_concrete run team join lobby @s
execute as @a at @s unless block ~ ~-2 ~ #f:concrete run tag @s remove team-selected
scoreboard players set @s team-count 0

#Music
playsound minecraft:sound.calm1 master @a[x=281,y=100,z=-311,distance=..2,tag=!music] 282 100 -311
tag @a[x=281,y=100,z=-311,distance=..2,tag=!music] add music

#Title
execute if score start time matches 15 run scoreboard players set start main 16
execute if score start time matches 15 run scoreboard players set start tick 0
execute if score start time matches 16.. run scoreboard players add start tick 1

execute if score start tick matches 20 run scoreboard players remove start main 1

execute if score start tick matches 20 if score start main matches 4..15 as @a at @s run playsound minecraft:block.note_block.bass master @s ~ ~ ~ 300
execute if score start tick matches 20 if score start main matches 3 as @a at @s run playsound minecraft:block.note_block.pling master @s ~ ~ ~ 300 0.2
execute if score start tick matches 20 if score start main matches 2 as @a at @s run playsound minecraft:block.note_block.pling master @s ~ ~ ~ 300 0.6
execute if score start tick matches 20 if score start main matches 1 as @a at @s run playsound minecraft:block.note_block.pling master @s ~ ~ ~ 300 0.8

execute if score start tick matches 20.. run scoreboard players set start tick 0

execute if score start main matches 4..15 run title @a title {"score":{"name":"start","objective":"main"},"color":"yellow","bold":true}
execute if score start main matches 1..3 run title @a title {"score":{"name":"start","objective":"main"},"color":"red","bold":true}
execute if score start main matches 4..15 run title @a subtitle ["",{"translate":"start.title.1","color":"green"}]
execute if score start main matches 1..3 run title @a subtitle ["",{"translate":"start.title.2","color":"yellow"}]

#Title team
execute if score $game_type main matches 0..2 if score start main matches 1..15 run title @a[team=lobby] actionbar ["",{"translate":"lobby.team.you","color":"gray"},{"translate":"lobby.team.spectator","color":"dark_gray","bold":true}]
execute if score $game_type main matches 3 if score start main matches 1..15 run title @a[team=lobby] actionbar ["",{"translate":"lobby.team.you","color":"gray"},{"translate":"lobby.team.seeker","color":"dark_purple","bold":true}]
title @a[team=f-blue] actionbar ["",{"translate":"lobby.team.you","color":"gray"},{"translate":"lobby.team.blue","color":"blue","bold":true}]
title @a[team=f-green] actionbar ["",{"translate":"lobby.team.you","color":"gray"},{"translate":"lobby.team.green","color":"dark_green","bold":true}]
title @a[team=f-red] actionbar ["",{"translate":"lobby.team.you","color":"gray"},{"translate":"lobby.team.red","color":"red","bold":true}]
title @a[team=f-yellow] actionbar ["",{"translate":"lobby.team.you","color":"gray"},{"translate":"lobby.team.yellow","color":"yellow","bold":true}]
title @a[team=f-hider] actionbar ["",{"translate":"lobby.team.you","color":"gray"},{"translate":"lobby.team.hider","color":"aqua","bold":true}]
title @a[team=random] actionbar ["",{"translate":"lobby.team.you","color":"gray"},{"translate":"lobby.team.random","color":"light_purple","bold":true}]

#Lock main timer
execute if score start time matches 17 run scoreboard players set start time 16
execute if score start main matches 0 run function f:lobby/start/final

#Cancel
execute if score start time matches 101 if score $game_type main matches 0..2 run tp @a 272.0 104 -319.0 -90 -90
execute if score start time matches 101 if score $game_type main matches 3 run tp @a 256.0 103.5 -323.0 -90 -90
execute if score start time matches 101 run effect give @a levitation 2 20 true
execute if score start time matches 101 run effect give @a minecraft:invisibility 2 0 true
execute if score start time matches 115 run tp @a 318 101 -351 180 90
execute if score start time matches 125 run effect clear @a minecraft:levitation
execute if score start time matches 121..130 as @e[tag=floor_as_left] at @s run tp @s ~0.4 ~ ~
execute if score start time matches 121..130 as @e[tag=floor_as_right] at @s run tp @s ~-0.4 ~ ~
execute if score start time matches 121..130 as @a at @s run playsound minecraft:block.piston.extend master @s ~ ~ ~ 1 1.5
execute if score start time matches 132 as @e[tag=floor] at @s run tp @s ~ ~-10 ~
execute if score start time matches 132 run kill @e[tag=floor]
execute if score start time matches 132 run clone 326 100 -382 331 100 -376 315 106 -354
execute if score start time matches 132 run fill 320 108 -347 318 108 -347 air
execute if score start time matches 140.. run scoreboard players set start time 0