##STAGES 
execute if score .room_count room matches -1.. if score .stage arena matches 1 run function main:game/wave/stages/1/tick
execute if score .room_count room matches -1.. if score .stage arena matches 3 run function main:game/wave/stages/2/tick
execute if score .room_count room matches -1.. if score .stage arena matches 2 run function main:game/wave/stages/3/tick
execute if score .room_count room matches -1.. if score .stage arena matches 4 run function main:game/wave/stages/4/tick
# execute if score .stage arena matches 5 run function main:game/condition/win/start


##MISC
#chatbox
# execute as @a[tag=!information] if score .start dialog matches ..0 run function main:game/wave/chat

#time end screen
function main:game/wave/tick/time

#ai
function main:game/mobs/ai/tick


# ##REST
#next wave
execute if entity @a[tag=!rest,tag=!wave] run function main:game/wave/next_room

execute as @a[tag=!rest,tag=!wave] run function main:game/wave/reward/tick
# execute at @e[type=#main:room,tag=room_exit_active] positioned ~-1.5 ~-1.5 ~-1.5 as @a[tag=!rest,tag=!wave,dx=2,dy=2,dz=2] run function main:game/wave/next

execute at @e[type=#main:room,tag=room_exit_active] run particle minecraft:trial_spawner_detection_ominous ~ ~1 ~ 0.75 1 0.75 0 5

#if workshop
execute as @e[type=text_display,tag=workshop] run function main:game/wave/workshop/tick

#if shop
execute as @e[type=text_display,tag=shop] run function main:game/wave/shop/tick

#if healing fountain
execute as @e[type=text_display,tag=fountain] run function main:game/wave/fountain/tick

#if rest
execute if entity @a[tag=rest] run return run function main:game/wave/tick/rest

##CORE
#enemy count
execute store result score .enemies arena if entity @e[type=#main:enemies,tag=enemy,tag=!fake,tag=!minion]
# execute store result score .enemies_melee arena if entity @e[type=#main:enemies,tag=enemy,tag=melee,tag=!fake,tag=!minion]
# execute store result score .enemies_ranged arena if entity @e[type=#main:enemies,tag=enemy,tag=ranged,tag=!fake,tag=!minion]

#determine how much enemies should still spawn
function main:game/wave/math_1

#enemy locator if less than 3
# execute if score .enemies arena matches -1..3 at @e[type=#main:enemies,tag=enemy,tag=!boss] positioned ~ ~3 ~ run particle dust{color:[1,0,0],scale:0.5} ~ ~ ~ 0 5 0 0 50 force

#spawn stuff
execute if entity @a[tag=wave] if score .enemies arena matches ..20 run function main:game/wave/spawn/tick


##END ROOM
execute as @a[tag=!rest,tag=wave] if score .kills arena >= .goal arena run return run function main:game/wave/rest

# ##BOSS
# #spawn
# execute if score .room_count room matches 5.. unless entity @e[type=#main:enemies,tag=enemy,tag=boss] unless entity @a[tag=boss_fight] run function main:game/mobs/spawn/boss

#tick
execute if score .room_count room matches 9.. run function main:game/wave/boss_tick

##END STAGE
execute if score .room_count room matches 9.. unless entity @e[type=#main:enemies,tag=enemy] unless entity @e[type=marker,tag=room_boss_trigger] unless entity @e[type=#main:enemies,tag=enemy,tag=boss] unless entity @e[type=item,tag=boss_key] unless entity @a[tag=choice] unless entity @a[tag=reward_choice] unless entity @a[tag=shop_choice] run function main:game/stage_end/start

execute if entity @a[tag=choice] run function main:game/stage_end/tick

#condition (win/loss)
function main:game/condition/tick

execute if entity @a[tag=e_sky] run return fail
execute if entity @a[tag=e_entry] run return fail
#bossbar
execute if score .room_count room matches ..9 store result bossbar minecraft:goal value run scoreboard players get .kills arena
execute if score .room_count room matches 9.. store result bossbar minecraft:goal value as @e[type=#main:enemies,tag=enemy,tag=boss] run scoreboard players get @s health
execute if score .room_count room matches 9.. store result bossbar minecraft:goal max as @e[type=#main:enemies,tag=enemy,tag=boss] run scoreboard players get @s health_max