execute if score world ongoing_run matches 1 unless score world Run_Active matches 1 run bossbar add daytime [{"bold":true,"color":"dark_red","text":"<<< "},{"color":"#FFBF0F","italic":false,"obfuscated":false,"text":" W"},{"color":"#FFD106","italic":false,"obfuscated":false,"text":"A"},{"color":"#FFDE06","italic":false,"obfuscated":false,"text":"V"},{"color":"#FFE016","italic":false,"obfuscated":false,"text":"E "},{"score": {"name": "world", "objective": "WaveCounter"},"color": "#ffe017"},{"text": " >>>","color":"dark_red"}]
execute if score world ongoing_run matches 1 unless score world Run_Active matches 1 run bossbar set daytime players @a
execute if score world ongoing_run matches 1 unless score world Run_Active matches 1 run bossbar set daytime visible true
execute if score world ongoing_run matches 1 unless score world Run_Active matches 1 run scoreboard players set world WavePickPercentage 5

execute if score world ongoing_run matches 1 unless score world Run_Active matches 1 run summon area_effect_cloud 0 0.5 0 {Particle:{type:"dust_pillar",block_state:"minecraft:redstone_block"},Radius:0f,RadiusPerTick:1f,Duration:40}
execute if score world ongoing_run matches 1 unless score world Run_Active matches 1 run particle explosion_emitter 0 1 0 1 25 1 0 50
execute if score world ongoing_run matches 1 unless score world Run_Active matches 1 run playsound minecraft:entity.generic.explode master @a 0 1 0 10 2
execute if score world ongoing_run matches 1 unless score world Run_Active matches 1 run playsound entity.warden.roar master @a 0 5 0 1 0
execute if score world ongoing_run matches 1 unless score world Run_Active matches 1 run playsound entity.fox.screech master @a 0 5 0 1 0
execute if score world ongoing_run matches 1 unless score world Run_Active matches 1 run playsound minecraft:entity.ender_dragon.growl master @a 0 5 0 1 0
execute if score world ongoing_run matches 1 unless score world Run_Active matches 1 run playsound minecraft:block.respawn_anchor.deplete master @a ~ ~ ~ 10000 0.3 1
execute if score world ongoing_run matches 1 unless score world Run_Active matches 1 run effect give @a nausea 3 1 true
execute if score world ongoing_run matches 1 unless score world Run_Active matches 1 run effect give @a darkness 3 1 true
execute if score world ongoing_run matches 1 unless score world Run_Active matches 1 run function kiwi2:run_dimension/start_wave_titles
execute if score world ongoing_run matches 1 unless score world Run_Active matches 1 run playsound minecraft:entity.arrow.hit_player master @a ~ ~ ~ 10000 2 1
execute if score world ongoing_run matches 1 unless score world Run_Active matches 1 run tellraw @a [{"color":"gold","obfuscated":true,"text":"xx"},{"bold":false,"color":"yellow","obfuscated":false,"text":" A chest has generated in the center! "},{"color":"gold","obfuscated":true,"text":"xx"}]
execute if score world ongoing_run matches 1 unless score world Run_Active matches 1 run effect clear @a
execute if score world ongoing_run matches 1 unless score world Run_Active matches 1 run setblock 0 0 0 chest{LootTable:"kiwi2:blocks/starter_chest"}
execute if score world ongoing_run matches 1 unless score world Run_Active matches 1 run scoreboard players set world WaveCounter 0
execute if score world ongoing_run matches 1 unless score world Run_Active matches 1 run scoreboard players set world RespawnCount 0
execute if score world ongoing_run matches 1 unless score world Run_Active matches 1 run scoreboard players set world BlackMarketWave 0
execute if score world ongoing_run matches 1 run scoreboard players set world Run_Active 1

# Increment WaveTimer and set it to the bossbar
execute if score world Run_Active matches 1 unless score world Boss_Death_Check matches 1 unless score world InIntermission matches 1 run scoreboard players add world NextWaveTimer 1
execute if score world Run_Active matches 1 run execute store result bossbar daytime value run scoreboard players get world NextWaveTimer

# For the first waves after intermission, after 200 ticks spawn the wave and reset the timer
execute if score world WaveCounter matches 0 if score world NextWaveTimer matches 200 run function kiwi2:run_dimension/first_wave_reset
execute if score world WaveCounter matches 21 if score world NextWaveTimer matches 200 unless score world FirstWaveOfIntermissionCheck matches 1 run function kiwi2:run_dimension/first_intermission_wave_reset
execute if score world WaveCounter matches 41 if score world NextWaveTimer matches 200 unless score world FirstWaveOfIntermissionCheck matches 1 run function kiwi2:run_dimension/first_intermission_wave_reset
execute if score world WaveCounter matches 61 if score world NextWaveTimer matches 200 unless score world FirstWaveOfIntermissionCheck matches 1 run function kiwi2:run_dimension/first_intermission_wave_reset
execute if score world WaveCounter matches 81 if score world NextWaveTimer matches 200 unless score world FirstWaveOfIntermissionCheck matches 1 run function kiwi2:run_dimension/first_intermission_wave_reset

# Normal wave timer, spawn a wave every 2000 ticks (100 seconds)
execute if score world WaveCounter matches 1.. if score world NextWaveTimer matches 2000 run scoreboard players add world WaveCounter 1
execute if score world WaveCounter matches 1.. if score world NextWaveTimer matches 2000 run scoreboard players add world RespawnCount 1
execute if score world WaveCounter matches 1.. if score world NextWaveTimer matches 2000 run title @a times 0 40 20
execute if score world WaveCounter matches 1.. if score world NextWaveTimer matches 2000 run team join WaveMobs @e[tag=wave_mob]
execute if score world WaveCounter matches 1.. if score world NextWaveTimer matches 2000 if score world BlackMarketSpawned matches 1 run function kiwi2:waves/black_market_waves/black_market_kill
execute if score world WaveCounter matches 1.. if score world NextWaveTimer matches 2000 if predicate kiwi2:chance_random_1 run scoreboard players set world BlackMarketWave 1
execute if score world WaveCounter matches 1.. if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. run title @a title [{"bold":true,"color":"red","text":"WAVE "},{"bold":true,"color":"red","score":{"name":"world","objective":"WaveCounter"}},""]
execute if score world WaveCounter matches 1.. if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. run bossbar set minecraft:daytime name [{"bold":true,"color":"dark_red","text":"<<< "},{"color":"#FFBF0F","italic":false,"obfuscated":false,"text":" W"},{"color":"#FFD106","italic":false,"obfuscated":false,"text":"A"},{"color":"#FFDE06","italic":false,"obfuscated":false,"text":"V"},{"color":"#FFE016","italic":false,"obfuscated":false,"text":"E "},{"score": {"name": "world", "objective": "WaveCounter"},"color": "#ffe017"},{"text": " >>>","color":"dark_red"}]
execute if score world WaveCounter matches 1.. if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. run playsound entity.wither.spawn master @a 0 5 0 5 0
execute if score world WaveCounter matches 1.. if score world NextWaveTimer matches 200 run tellraw @a {"clickEvent":{"action":"run_command","value":"/scoreboard players set world NextWaveTimer 1900"},"color":"white","text":"[ Skip to the next wave ]"}

execute if score world WaveCounter matches 1.. if score world OneSecondTimer matches 10 if entity @e[type=witch,tag=point_shop] run function kiwi2:point_shop/point_shop_respawn

# Waves 1 - 5 (100% W1S1)
execute if score world WaveCounter matches 1..5 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. run function kiwi2:waves/wave_1/set_1/w1s1_wave_roll
# Waves 6 - 14 (90-10, 80-20, 70-30, 60-40, 50-50, 40-60, 30-70, 20-80, 10-90) [W1S1, W2S1]
execute if score world WaveCounter matches 6..14 if score world NextWaveTimer matches 2000 run scoreboard players add world WavePickPercentage 10
execute if score world WaveCounter matches 6..14 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. store result score world WavePick run random value 1..100
execute if score world WaveCounter matches 6..14 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. if score world WavePick >= world WavePickPercentage run function kiwi2:waves/wave_1/set_1/w1s1_wave_roll
execute if score world WaveCounter matches 6..14 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. if score world WavePick < world WavePickPercentage run function kiwi2:waves/wave_2/set_1/w2s1_wave_roll
# Waves 15 - 19 (100% W2S1)
execute if score world WaveCounter matches 15..19 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. run function kiwi2:waves/wave_2/set_1/w2s1_wave_roll

# Waves 17 - 20 (100% W3S1)
execute if score world WaveCounter matches 22..25 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. run function kiwi2:waves/wave_3/set_1/w3s1_wave_roll
# Waves 21 - 25 (80-20, 65-35, 50-50, 35-65, 20-80) [W3S1, W1S2]
execute if score world WaveCounter matches 26..34 if score world NextWaveTimer matches 2000 run scoreboard players add world WavePickPercentage 10
execute if score world WaveCounter matches 26..34 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. store result score world WavePick run random value 1..100
execute if score world WaveCounter matches 26..34 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. if score world WavePick >= world WavePickPercentage run function kiwi2:waves/wave_3/set_1/w3s1_wave_roll
execute if score world WaveCounter matches 26..34 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. if score world WavePick < world WavePickPercentage run function kiwi2:waves/wave_1/set_2/w1s2_wave_roll
# Waves 26 - 29 (100% W1S2)
execute if score world WaveCounter matches 35..39 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. run function kiwi2:waves/wave_1/set_2/w1s2_wave_roll

# Waves 32 - 35 (100% W2S2)
execute if score world WaveCounter matches 42..45 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. run function kiwi2:waves/wave_2/set_2/w2s2_wave_roll
# Waves 36 - 40 (80-20, 65-35, 50-50, 35-65, 20-80) [W2S2, W3S2]
execute if score world WaveCounter matches 46..54 if score world NextWaveTimer matches 2000 run scoreboard players add world WavePickPercentage 10
execute if score world WaveCounter matches 46..54 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. store result score world WavePick run random value 1..100
execute if score world WaveCounter matches 46..54 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. if score world WavePick >= world WavePickPercentage run function kiwi2:waves/wave_2/set_2/w2s2_wave_roll
execute if score world WaveCounter matches 46..54 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. if score world WavePick < world WavePickPercentage run function kiwi2:waves/wave_3/set_2/w3s2_wave_roll
# Waves 41 - 44 (100% W3S2)
execute if score world WaveCounter matches 55..59 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. run function kiwi2:waves/wave_3/set_2/w3s2_wave_roll

# Waves 47 - 50 (100% W1S3)
execute if score world WaveCounter matches 62..65 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. run function kiwi2:waves/wave_1/set_3/w1s3_wave_roll
# Waves 51 - 55 (80-20, 65-35, 50-50, 35-65, 20-80) [W1S3, W2S3]
execute if score world WaveCounter matches 66..74 if score world NextWaveTimer matches 2000 run scoreboard players add world WavePickPercentage 10
execute if score world WaveCounter matches 66..74 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. store result score world WavePick run random value 1..100
execute if score world WaveCounter matches 66..74 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. if score world WavePick >= world WavePickPercentage run function kiwi2:waves/wave_1/set_3/w1s3_wave_roll
execute if score world WaveCounter matches 66..74 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. if score world WavePick < world WavePickPercentage run function kiwi2:waves/wave_2/set_3/w2s3_wave_roll
# Waves 56 - 59 (100% W2S3)
execute if score world WaveCounter matches 75..79 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. run function kiwi2:waves/wave_3/set_2/w3s2_wave_roll

# Waves 62 - 65 (100% W3S3)
execute if score world WaveCounter matches 82..85 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. run function kiwi2:waves/wave_3/set_3/w3s3_wave_roll
# Waves 66 - 70 (80-20, 65-35, 50-50, 35-65, 20-80) [W3S3, W1S4]
execute if score world WaveCounter matches 86..94 if score world NextWaveTimer matches 2000 run scoreboard players add world WavePickPercentage 10
execute if score world WaveCounter matches 86..94 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. store result score world WavePick run random value 1..100
execute if score world WaveCounter matches 86..94 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. if score world WavePick >= world WavePickPercentage run function kiwi2:waves/wave_3/set_3/w3s3_wave_roll
execute if score world WaveCounter matches 86..94 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. if score world WavePick < world WavePickPercentage run function kiwi2:waves/wave_1/set_4/w1s4_wave_roll
# Waves 71 - 74 (100% W1S4)
execute if score world WaveCounter matches 95..99 if score world NextWaveTimer matches 2000 unless score world BlackMarketWave matches 1.. run function kiwi2:waves/wave_1/set_4/w1s4_wave_roll

execute if score world WaveCounter matches 1.. if score world NextWaveTimer matches 2000 if score world BlackMarketWave matches 1.. run function kiwi2:waves/black_market_waves/black_market_spawn

# Boss Rolls
execute if score world WaveCounter matches 20 if score world NextWaveTimer matches 2000 run function kiwi2:waves/boss_waves/all_bosses/boss_wave_roll
execute if score world WaveCounter matches 40 if score world NextWaveTimer matches 2000 run function kiwi2:waves/boss_waves/all_bosses/boss_wave_roll
execute if score world WaveCounter matches 60 if score world NextWaveTimer matches 2000 run function kiwi2:waves/boss_waves/all_bosses/boss_wave_roll
execute if score world WaveCounter matches 80 if score world NextWaveTimer matches 2000 run function kiwi2:waves/boss_waves/all_bosses/boss_wave_roll
execute if score world WaveCounter matches 100 if score world NextWaveTimer matches 2000 run function kiwi2:waves/boss_waves/all_bosses/boss_wave_roll

# Boss Ambient
execute if score world WaveCounter matches 19 run function kiwi2:waves/boss_waves/all_bosses/boss_ambient
execute if score world WaveCounter matches 39 run function kiwi2:waves/boss_waves/all_bosses/boss_ambient
execute if score world WaveCounter matches 59 run function kiwi2:waves/boss_waves/all_bosses/boss_ambient
execute if score world WaveCounter matches 79 run function kiwi2:waves/boss_waves/all_bosses/boss_ambient
execute if score world WaveCounter matches 99 run function kiwi2:waves/boss_waves/all_bosses/boss_ambient

# Boss Warn
execute if score world WaveCounter matches 19 if score world NextWaveTimer matches 2000 run function kiwi2:waves/boss_waves/all_bosses/boss_warn
execute if score world WaveCounter matches 39 if score world NextWaveTimer matches 2000 run function kiwi2:waves/boss_waves/all_bosses/boss_warn
execute if score world WaveCounter matches 59 if score world NextWaveTimer matches 2000 run function kiwi2:waves/boss_waves/all_bosses/boss_warn
execute if score world WaveCounter matches 79 if score world NextWaveTimer matches 2000 run function kiwi2:waves/boss_waves/all_bosses/boss_warn
execute if score world WaveCounter matches 99 if score world NextWaveTimer matches 2000 run function kiwi2:waves/boss_waves/all_bosses/boss_warn

# Point Shop
execute if score world NextWaveTimer matches 2000 run scoreboard players add world PointShopWaveCount 1
execute if score world PointShopWaveCount matches 3 if score world PointShopSpawned matches 0 unless score world BlackMarketWave matches 1.. run title @a actionbar {"text": ">> POINT SHOP IS NOW AVAILABLE! <<","bold": true, "color": "gold"}
execute if score world PointShopWaveCount matches 3 if score world PointShopSpawned matches 0 unless score world BlackMarketWave matches 1.. run playsound minecraft:entity.arrow.hit_player master @a ~ ~ ~ 10000 0.7
execute if score world PointShopWaveCount matches 3 if score world PointShopSpawned matches 0 unless score world BlackMarketWave matches 1.. run function kiwi2:point_shop/point_shop_spawn
execute if score world PointShopWaveCount matches 3 if score world PointShopSpawned matches 0 unless score world BlackMarketWave matches 1.. run function kiwi2:point_shop/point_shop_roll
execute if score world PointShopWaveCount matches 3 if score world PointShopSpawned matches 0 run scoreboard players set world PointShopSpawned 1
execute if score world PointShopWaveCount matches 4 run kill @e[type=villager,tag=point_shop]
execute if score world PointShopWaveCount matches 4 run kill @e[type=witch,tag=point_shop]
execute if score world PointShopWaveCount matches 4 unless score world BlackMarketWave matches 1.. run fill -1 1 0 1 1 0 air
execute if score world PointShopWaveCount matches 4 unless score world BlackMarketWave matches 1.. run fill 0 1 -1 0 1 1 air
execute if score world PointShopWaveCount matches 4 run scoreboard players set world PointShopSpawned 0
execute if score world PointShopWaveCount matches 4 run scoreboard players set world PointShopWaveCount 1

# Reward Chest
execute if score world NextWaveTimer matches 2000 run setblock 0 1 0 air
execute if score world NextWaveTimer matches 2000 store result score world RewardChestRoll run random value 1..100
execute if score world NextWaveTimer matches 2000 if score world RewardChestRoll <= world RewardChestChance run function kiwi2:reward_chest/reward_chest_spawn

# Shrines
execute if score world NextWaveTimer matches 2000 run kill @e[tag=enchantment_shrine]
execute if score world NextWaveTimer matches 2000 if predicate kiwi2:chance_random_30 run function kiwi:enchantment_shrines/shrine_roll_spawn

# World Upgrades (End of Wave)
execute if score world NextWaveTimer matches 2000 run function kiwi2:upgrades/world_upgrades/world_upgrades_main

# Respawn Mechanic (3 Waves)
execute if score world RespawnCount matches 3.. if score world NextWaveTimer matches 2000.. run function kiwi2:run_dimension/revive_all_players
execute if score world RespawnCount matches 3.. if score world NextWaveTimer matches 2000.. run scoreboard players set world RespawnCount 0

# Add all wave mobs to the same team
execute if score world WaveCounter matches 0.. if score world NextWaveTimer matches 2000.. run team join WaveMobs @e[tag=wave_mob]

# Reset Wave timer (needs to be at the end)
execute if score world WaveCounter matches 1.. if score world NextWaveTimer matches 2000.. run scoreboard players set world NextWaveTimer 0