#Dyson Sphere Site
dyson_sphere_0 = {
	entity = "construction_platform_entity"
	construction_entity = "construction_platform_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	entity_offset = { x = -7 y = -7 }
	build_time = 1800
	resources = {
		category = megastructures_oeo_megastructures
		cost = { 
			####Unity replaced with influence for 1st stage####
			influence = 500
		}
		cost = {
			trigger = { country_uses_bio_ships = no }
			alloys = 1000
		}
		cost = {
			trigger = { country_uses_bio_ships = yes }
			alloys = 1000
			mult = 0.5
		}
		cost = {
			trigger = { country_uses_bio_ships = yes }
			food = 1000
			mult = @halved_alloy_to_food_cost_ratio
		}
		upkeep = { trade = @oeo_sphere_1_trade_upkeep }
	}
	dismantle_cost = {
		category = megastructures
		cost = { energy = 1000 }
	}
	dismantle_time = 360
	dismantle_potential = {	always = yes }
	dismantle_possible = {
		custom_tooltip = {
			fail_text = "oeo_dismantle_cooldown" 
			NOT = { has_megastructure_flag = oeo_dismantle_cooldown }
		}
	}
	on_dismantle_start = {
		fromfrom.planet = {
			set_planet_flag = oeo_not_cancelled
			planet_event = { id = oeo_individual_dismantle.1 days = 361 }
		}
	}
	on_dismantle_cancel = {
		fromfrom = { set_timed_megastructure_flag = { flag = oeo_dismantle_cooldown days = 360 } }
		fromfrom.planet = { remove_planet_flag = oeo_not_cancelled }
	}
	on_dismantle_complete = {	
		from = {
			add_resource = {
				alloys = 1000
				mult = modifier:megastructure_dismantle_refund_mult
			}
		}
	}	
	custom_tooltip_requirements = "oeo_MEGASTRUCTURE_TOOLTIP_REQUIREMENTS_MEGASTRUCTURE_LIMIT"
	prerequisites = { "tech_dyson_sphere" }
	potential = { has_machine_age_dlc = no }
	possible = {
		hidden_trigger = {
			exists = starbase
		}
		custom_tooltip = {
			fail_text = "requires_inside_border"
			is_inside_border = from
		}
		custom_tooltip = {
			fail_text = "requires_surveyed_system"
			NOT = {
				any_system_planet = {
					is_surveyed = {
						who = prev.from
						status = no
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_arc_furnace"
			system_has_arc_furnace = no
		}
		custom_tooltip = {
			fail_text = "requires_no_arc_furnace_construction"
			if = {
				limit = {
					system_has_arc_furnace = no
				}
				NOT = {
					solar_system = {
						has_star_flag = arc_furnace_construction
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_colonies"
			NOT = {
				any_system_planet = {
					OR = {
						sapient_pop_amount > 0
						is_colony = yes
					}
					#edited to prevent ringworld dyson spheres
					OR = {
						is_artificial = no
						is_planet_class = pc_ringworld_habitable
						is_planet_class = pc_oeo_planetary_ringworld
					}
					exists = owner
					owner = {
						is_primitive = no
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "blocked_by_pre_ftl_policy"
			if = {
				limit = {
					any_system_planet = {
						exists = owner
						owner = {
							is_primitive = yes
						}
					}
				}
				from = {
					has_policy_flag = interference_aggressive
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_black_hole_neutron_star_pulsar"
			NOT = {
				is_star_class = sc_black_hole
				is_star_class = sc_neutron_star
				is_star_class = sc_pulsar
			}
		}
	}
	placement_rules = {
		planet_possible = {
			custom_tooltip = {
				fail_text = "must_build_around_star"
				is_star = yes
			}
			custom_tooltip = {
				fail_text = "requires_no_anomaly"
				NOT = { has_anomaly = yes }
			}
			custom_tooltip = {
				fail_text = "requires_no_existing_megastructure"
				#can_build_megastructure_on_planet = yes
				NOR = {
					has_planet_flag = has_megastructure
					has_planet_flag = megastructure
				}
			}
		}
	}
	# root = system
	# from = country
	ai_weight = {
		factor = 15
		modifier = {
			factor = 0.1
			starbase = { NOT = { has_starbase_size >= starbase_starfortress } }
		}
		modifier = {
			factor = 0.1
			any_neighbor_system = {
				exists = owner
				NOT = {
					owner = { is_same_value = from }
				}
			}
		}
		modifier = {
			factor = 0
			from = {
				count_owned_megastructure = {
					limit = { is_oeo_sphere_category = yes }
					count < 0
				}
			}
		}
	}
	on_build_start = { set_star_flag = dyson_sphere_construction }
	on_build_cancel = { remove_star_flag = dyson_sphere_construction }
	on_build_complete = {
		remove_star_flag = dyson_sphere_construction
		set_star_flag = dyson_sphere_built
		remove_system_terraforming_candidates = yes
		from = {
			if = {
				limit = {
					has_relic = r_contingency_core
					has_country_flag = built_dyson_sphere
				}
				set_country_flag = built_dyson_sphere_contingency_swarm
			}
			set_country_flag = built_dyson_sphere_swarm
		}
		fromfrom.planet = {
			set_planet_flag = has_megastructure
			set_planet_flag = oeo_sphere
			if = {
				limit = { has_orbital_station = yes }
				orbital_station = {
					dismantle = yes
				}
			}
		}
	}
}

#Dyson Sphere Frame
dyson_sphere_1 = {
	entity = "dyson_sphere_phase_01_entity"
	construction_entity = "dyson_sphere_frame_entity"
	portrait = "GFX_megastructure_construction_background"
	build_time = 3600
	resources = {
		category = megastructures_oeo_megastructures
		cost = { unity = 5000 }
		cost = {
			trigger = { country_uses_bio_ships = no }
			alloys = 2000
		}
		cost = {
			trigger = { country_uses_bio_ships = yes }
			alloys = 2000
			mult = 0.5
		}
		cost = {
			trigger = { country_uses_bio_ships = yes }
			food = 10000
			mult = @halved_alloy_to_food_cost_ratio
		}
		upkeep = { trade = @oeo_sphere_2_trade_upkeep }
	}
	dismantle_cost = {
		category = megastructures
		cost = { energy = 2000 }
	}
	dismantle_time = 360
	dismantle_potential = {	always = yes }
	dismantle_possible = {
		custom_tooltip = {
			fail_text = "oeo_dismantle_cooldown" 
			NOT = { has_megastructure_flag = oeo_dismantle_cooldown }
		}
	}
	on_dismantle_start = {
		fromfrom.planet = {
			set_planet_flag = oeo_not_cancelled
			planet_event = { id = oeo_individual_dismantle.1 days = 361 }
		}
	}
	on_dismantle_cancel = {
		fromfrom = { set_timed_megastructure_flag = { flag = oeo_dismantle_cooldown days = 360 } }
		fromfrom.planet = { remove_planet_flag = oeo_not_cancelled }
	}
	on_dismantle_complete = {	
		from = {
			add_resource = {
				alloys = 2000
				mult = modifier:megastructure_dismantle_refund_mult
			}
		}
	}	
	possible = {
		custom_tooltip = {
			fail_text = "requires_no_arc_furnace"
			system_has_arc_furnace = no
		}
		custom_tooltip = {
			fail_text = "requires_no_arc_furnace_construction"
			if = {
				limit = { system_has_arc_furnace = no }
				NOT = {
					solar_system = { has_star_flag = arc_furnace_construction }
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_colonies"
			NOT = {
				any_system_planet = {
					OR = {
						sapient_pop_amount > 0
						is_colony = yes
					}
					is_artificial = no
					exists = owner
					owner = {
						is_primitive = no
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "blocked_by_pre_ftl_policy"
			if = {
				limit = {
					any_system_planet = {
						exists = owner
						owner = {
							is_primitive = yes
						}
					}
				}
				from = { has_policy_flag = interference_aggressive }
			}
		}
	}
	upgrade_from = { dyson_sphere_0 }
	prerequisites = { "tech_dyson_sphere" }
	on_build_start = { set_star_flag = dyson_sphere_construction }
	on_build_cancel = { remove_star_flag = dyson_sphere_construction }
	on_build_complete = {
		remove_star_flag = dyson_sphere_construction
		remove_system_terraforming_candidates = yes
	}
}
#Partial Dyson Sphere
dyson_sphere_2 = {
	entity = "dyson_sphere_phase_02_entity"
	construction_entity = "dyson_sphere_part_1_entity"
	portrait = "GFX_megastructure_construction_background"
	build_time = 3600
	use_planet_resource = yes
	possible = {
		custom_tooltip = {
			fail_text = "requires_no_arc_furnace"
			system_has_arc_furnace = no
		}
		custom_tooltip = {
			fail_text = "requires_no_arc_furnace_construction"
			if = {
				limit = { system_has_arc_furnace = no
				}
				NOT = {
					solar_system = { has_star_flag = arc_furnace_construction }
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_colonies"
			NOT = {
				any_system_planet = {
					OR = {
						sapient_pop_amount > 0
						is_colony = yes
					}
					is_artificial = no
					exists = owner
					owner = {
						is_primitive = no
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "blocked_by_pre_ftl_policy"
			if = {
				limit = {
					any_system_planet = {
						exists = owner
						owner = {
							is_primitive = yes
						}
					}
				}
				from = { has_policy_flag = interference_aggressive }
			}
		}
	}
	ai_weight = {
		factor = 15
		modifier = {
			factor = 0.1
			starbase = { NOT = { has_starbase_size >= starbase_starfortress } }
		}
		modifier = {
			factor = 0.1
			any_neighbor_system = {
				exists = owner
				NOT = {
					owner = { is_same_value = from }
				}
			}
		}
		modifier = {
			factor = 0
			from = {
				count_owned_megastructure = {
					limit = { 
						is_oeo_sphere_category = yes
					}
					count < 0
				}
			}
		}
	}
	resources = {
		category = megastructures_oeo_megastructures
		cost = { unity = 5000 }
		cost = {
			trigger = { country_uses_bio_ships = no }
			alloys = 10000
		}
		cost = {
			trigger = { country_uses_bio_ships = yes }
			alloys = 10000
			mult = 0.5
		}
		cost = {
			trigger = { country_uses_bio_ships = yes }
			food = 10000
			mult = @halved_alloy_to_food_cost_ratio
		}
		upkeep = { trade = @oeo_sphere_3_trade_upkeep }
		produces = { energy = 1000 }
	}
	dismantle_cost = {
		category = megastructures
		cost = { energy = 10000 }
	}
	dismantle_time = 360
	dismantle_potential = {	always = yes }
	dismantle_possible = {
		custom_tooltip = {
			fail_text = "oeo_dismantle_cooldown" 
			NOT = { has_megastructure_flag = oeo_dismantle_cooldown }
		}
	}
	on_dismantle_start = {
		fromfrom.planet = {
			set_planet_flag = oeo_not_cancelled
			planet_event = { id = oeo_individual_dismantle.1 days = 361 }
		}
	}
	on_dismantle_cancel = {
		fromfrom = { set_timed_megastructure_flag = { flag = oeo_dismantle_cooldown days = 360 } }
		fromfrom.planet = { remove_planet_flag = oeo_not_cancelled }
	}
	on_dismantle_complete = {	
		from = {
			add_resource = {
				alloys = 10000
				mult = modifier:megastructure_dismantle_refund_mult
			}
		}
	}	
	upgrade_from = {
		dyson_swarm_3
		dyson_sphere_1
	}
	prerequisites = { "tech_dyson_sphere" }
	on_build_start = { set_star_flag = dyson_sphere_construction }
	on_build_cancel = { remove_star_flag = dyson_sphere_construction }
	on_build_complete = {
		remove_star_flag = dyson_sphere_construction
		remove_system_terraforming_candidates = yes
	}
}

#Partial Dyson Sphere
dyson_sphere_3 = {
	entity = "dyson_sphere_phase_03_entity"
	construction_entity = "dyson_sphere_part_2_entity"
	portrait = "GFX_megastructure_construction_background"
	build_time = 3600
	use_planet_resource = yes
	resources = {
		category = megastructures_oeo_megastructures
		cost = { unity = 5000 }
		cost = {
			trigger = { country_uses_bio_ships = no }
			alloys = 10000
		}
		cost = {
			trigger = { country_uses_bio_ships = yes }
			alloys = 10000
			mult = 0.5
		}
		cost = {
			trigger = { country_uses_bio_ships = yes }
			food = 10000
			mult = @halved_alloy_to_food_cost_ratio
		}
		upkeep = { trade = @oeo_sphere_4_trade_upkeep }
		produces = { energy = 2000 }
	}
	dismantle_cost = {
		category = megastructures
		cost = { energy = 10000 }
	}
	dismantle_time = 360
	dismantle_potential = {	always = yes }
	dismantle_possible = {
		custom_tooltip = {
			fail_text = "oeo_dismantle_cooldown" 
			NOT = { has_megastructure_flag = oeo_dismantle_cooldown }
		}
	}
	on_dismantle_start = {
		fromfrom.planet = {
			set_planet_flag = oeo_not_cancelled
			planet_event = { id = oeo_individual_dismantle.1 days = 361 }
		}
	}
	on_dismantle_cancel = {
		fromfrom = { set_timed_megastructure_flag = { flag = oeo_dismantle_cooldown days = 360 } }
		fromfrom.planet = { remove_planet_flag = oeo_not_cancelled }
	}
	on_dismantle_complete = {	
		from = {
			add_resource = {
				alloys = 10000
				mult = modifier:megastructure_dismantle_refund_mult
			}
		}
	}	
	possible = {
		custom_tooltip = {
			fail_text = "requires_no_arc_furnace"
			system_has_arc_furnace = no
		}
		custom_tooltip = {
			fail_text = "requires_no_arc_furnace_construction"
			if = {
				limit = { system_has_arc_furnace = no }
				NOT = {
					solar_system = { has_star_flag = arc_furnace_construction }
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_colonies"
			NOT = {
				any_system_planet = {
					OR = {
						sapient_pop_amount > 0
						is_colony = yes
					}
					is_artificial = no
					exists = owner
					owner = {
						is_primitive = no
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "blocked_by_pre_ftl_policy"
			if = {
				limit = {
					any_system_planet = {
						exists = owner
						owner = { is_primitive = yes }
					}
				}
				from = { has_policy_flag = interference_aggressive }
			}
		}
	}
	upgrade_from = { dyson_sphere_2 }
	prerequisites = { "tech_dyson_sphere" }
	on_build_start = { set_star_flag = dyson_sphere_construction }
	on_build_cancel = { remove_star_flag = dyson_sphere_construction }
	on_build_complete = {
		remove_star_flag = dyson_sphere_construction
		remove_system_terraforming_candidates = yes
	}
}
#Partial Dyson Sphere
dyson_sphere_4 = {
	entity = "dyson_sphere_phase_04_entity"
	construction_entity = "dyson_sphere_part_3_entity"
	portrait = "GFX_megastructure_construction_background"
	build_time = 3600
	use_planet_resource = yes
	resources = {
		category = megastructures_oeo_megastructures
		cost = { unity = 5000 }
		cost = {
			trigger = { country_uses_bio_ships = no }
			alloys = 10000
		}
		cost = {
			trigger = { country_uses_bio_ships = yes }
			alloys = 10000
			mult = 0.5
		}
		cost = {
			trigger = { country_uses_bio_ships = yes }
			food = 10000
			mult = @halved_alloy_to_food_cost_ratio
		}
		upkeep = { trade = @oeo_sphere_5_trade_upkeep }
		produces = { energy = 3000 }
	}
	dismantle_cost = {
		category = megastructures
		cost = { energy = 10000 }
	}
	dismantle_time = 360
	dismantle_potential = {	always = yes }
	dismantle_possible = {
		custom_tooltip = {
			fail_text = "oeo_dismantle_cooldown" 
			NOT = { has_megastructure_flag = oeo_dismantle_cooldown }
		}
	}
	on_dismantle_start = {
		fromfrom.planet = {
			set_planet_flag = oeo_not_cancelled
			planet_event = { id = oeo_individual_dismantle.1 days = 361 }
		}
	}
	on_dismantle_cancel = {
		fromfrom = { set_timed_megastructure_flag = { flag = oeo_dismantle_cooldown days = 360 } }
		fromfrom.planet = { remove_planet_flag = oeo_not_cancelled }
	}
	on_dismantle_complete = {	
		from = {
			add_resource = {
				alloys = 10000
				mult = modifier:megastructure_dismantle_refund_mult
			}
		}
	}	
	upgrade_from = { dyson_sphere_3 }
	prerequisites = { "tech_dyson_sphere" }
	possible = {
		custom_tooltip = {
			fail_text = "requires_no_arc_furnace"
			system_has_arc_furnace = no
		}
		custom_tooltip = {
			fail_text = "requires_no_arc_furnace_construction"
			if = {
				limit = { system_has_arc_furnace = no }
				NOT = {
					solar_system = { has_star_flag = arc_furnace_construction }
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_colonies"
			NOT = {
				any_system_planet = {
					OR = {
						sapient_pop_amount > 0
						is_colony = yes
					}
					is_artificial = no
					exists = owner
					owner = {
						is_primitive = no
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "blocked_by_pre_ftl_policy"
			if = {
				limit = {
					any_system_planet = {
						exists = owner
						owner = { is_primitive = yes }
					}
				}
				from = { has_policy_flag = interference_aggressive }
			}
		}
	}
	on_build_start = { set_star_flag = dyson_sphere_construction }
	on_build_cancel = { remove_star_flag = dyson_sphere_construction }
	on_build_complete = {
		remove_star_flag = dyson_sphere_construction
		remove_system_terraforming_candidates = yes
	}
}
#Dyson Sphere
dyson_sphere_5 = {
	entity = "dyson_sphere_phase_05_entity"
	construction_entity = "dyson_sphere_part_4_entity"
	portrait = "GFX_megastructure_dyson_sphere_background"
	build_time = 3600
	use_planet_resource = yes
	resources = {
		category = megastructures_oeo_megastructures
		cost = { unity = 5000 }
		cost = {
			trigger = { country_uses_bio_ships = no }
			alloys = 10000
		}
		cost = {
			trigger = { country_uses_bio_ships = yes }
			alloys = 10000
			mult = 0.5
		}
		cost = {
			trigger = { country_uses_bio_ships = yes }
			food = 10000
			mult = @halved_alloy_to_food_cost_ratio
		}
		upkeep = { trade = @oeo_sphere_6_trade_upkeep }
		produces = { energy = 4000 }
	}
	dismantle_cost = {
		category = megastructures
		cost = { energy = 10000 }
	}
	dismantle_time = 360
	dismantle_potential = {	always = yes }
	dismantle_possible = {
		custom_tooltip = {
			fail_text = "oeo_dismantle_cooldown" 
			NOT = { has_megastructure_flag = oeo_dismantle_cooldown }
		}
	}
	on_dismantle_start = {
		fromfrom.planet = {
			set_planet_flag = oeo_not_cancelled
			planet_event = { id = oeo_individual_dismantle.1 days = 361 }
		}
	}
	on_dismantle_cancel = {
		fromfrom = { set_timed_megastructure_flag = { flag = oeo_dismantle_cooldown days = 360 } }
		fromfrom.planet = { remove_planet_flag = oeo_not_cancelled }
	}
	on_dismantle_complete = {	
		from = {
			add_resource = {
				alloys = 10000
				mult = modifier:megastructure_dismantle_refund_mult
			}
		}
	}	
	upgrade_from = { dyson_sphere_4 }
	prerequisites = { "tech_dyson_sphere" }
	show_prereqs = yes
	outliner_trigger = {
		exists = owner
		owner = {
			OR = {
				has_country_flag = can_build_disco_sphere
				NOT = { has_country_flag = oeo_sphere_outliner }
			}
		}
	}
	possible = {
		custom_tooltip = {
			fail_text = "requires_no_arc_furnace"
			system_has_arc_furnace = no
		}
		custom_tooltip = {
			fail_text = "requires_no_arc_furnace_construction"
			if = {
				limit = { system_has_arc_furnace = no }
				NOT = {
					solar_system = { has_star_flag = arc_furnace_construction }
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_colonies"
			NOT = {
				any_system_planet = {
					OR = {
						sapient_pop_amount > 0
						is_colony = yes
					}
					is_artificial = no
					exists = owner
					owner = {
						is_primitive = no
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "blocked_by_pre_ftl_policy"
			if = {
				limit = {
					any_system_planet = {
						exists = owner
						owner = { is_primitive = yes }
					}
				}
				from = { has_policy_flag = interference_aggressive }
			}
		}
	}
	on_build_start = {
		set_star_flag = dyson_sphere_construction
		solar_system = {
			if = {
				limit = {
					any_system_megastructure = { is_megastructure_type = dyson_sphere_5 }
				}
				set_star_flag = oeo_second_dyson_swarm
			}
		}
	}
	on_build_cancel = { remove_star_flag = dyson_sphere_construction }
	on_build_complete = {
		remove_star_flag = dyson_sphere_construction
		from = { set_country_flag = has_built_or_repaired_megastructure }
		every_system_planet = {
			limit = {
				OR = {
					is_planet_class = pc_molten
					is_planet_class = pc_toxic
					habitable_planet = yes
				}
			}
			if = {
				limit = {
					OR = {
						#has_colony_progress > 0 # TODO: [AUG-51261] Replace with pop count check
						is_colony = yes
					}
				}
				destroy_colony = yes
			}
			change_pc = pc_frozen
		}
		every_system_planet = {
			limit = { is_planet_class = pc_barren }
			change_pc = pc_barren_cold
		}
		remove_system_terraforming_candidates = yes
	}
}

# Restored Dyson Sphere
dyson_sphere_restored = {
	entity = "dyson_sphere_phase_05_entity"
	construction_entity = "dyson_sphere_part_4_entity"
	portrait = "GFX_megastructure_dyson_sphere_background"
	build_time = 7200
	resources = {
		category = megastructures_oeo_megastructures
		cost = { unity = 7500 }
		cost = {
			trigger = { country_uses_bio_ships = no }
			alloys = 20000
		}
		cost = {
			trigger = { country_uses_bio_ships = yes }
			alloys = 20000
			mult = 0.5
		}
		cost = {
			trigger = { country_uses_bio_ships = yes }
			food = 20000
			mult = @halved_alloy_to_food_cost_ratio
		}
		upkeep = { trade = @oeo_sphere_6_trade_upkeep }
		produces = { energy = 4000 }
	}
	dismantle_cost = {
		category = megastructures
		cost = { energy = 10000 }
	}
	dismantle_time = 360
	dismantle_potential = {	always = yes }
	dismantle_possible = {
		custom_tooltip = {
			fail_text = "oeo_dismantle_cooldown" 
			NOT = { has_megastructure_flag = oeo_dismantle_cooldown }
		}
	}
	on_dismantle_start = {
		fromfrom.planet = {
			set_planet_flag = oeo_not_cancelled
			planet_event = { id = oeo_individual_dismantle.1 days = 361 }
		}
	}
	on_dismantle_cancel = {
		fromfrom = { set_timed_megastructure_flag = { flag = oeo_dismantle_cooldown days = 360 } }
		fromfrom.planet = { remove_planet_flag = oeo_not_cancelled }
	}
	on_dismantle_complete = {	
		from = {
			add_resource = {
				alloys = 10000
				mult = modifier:megastructure_dismantle_refund_mult
			}
		}
	}	
	upgrade_from = { dyson_sphere_ruined }
	outliner_trigger = {
		exists = owner
		owner = {
			OR = {
				has_country_flag = can_build_disco_sphere
				NOT = { has_country_flag = oeo_sphere_outliner }
			}
		}
	}
	possible = {
		from = { has_technology = tech_mega_engineering }
		custom_tooltip = {
			fail_text = "requires_no_arc_furnace"
			system_has_arc_furnace = no
		}
		custom_tooltip = {
			fail_text = "requires_no_arc_furnace_construction"
			if = {
				limit = { system_has_arc_furnace = no }
				NOT = {
					solar_system = { has_star_flag = arc_furnace_construction }
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_colonies"
			NOT = {
				any_system_planet = {
					OR = {
						sapient_pop_amount > 0
						is_colony = yes
					}
					is_artificial = no
					exists = owner
					owner = {
						is_primitive = no
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "blocked_by_pre_ftl_policy"
			if = {
				limit = {
					any_system_planet = {
						exists = owner
						owner = { is_primitive = yes }
					}
				}
				from = { has_policy_flag = interference_aggressive }
			}
		}
	}
	on_build_start = {
		set_star_flag = dyson_sphere_construction
		fromfrom = { set_graphical_culture = root.from }
	}
	on_build_cancel = { remove_star_flag = dyson_sphere_construction }
	on_build_complete = {
		remove_star_flag = dyson_sphere_construction
		every_system_planet = {
			limit = {
				OR = {
					is_planet_class = pc_molten
					is_planet_class = pc_toxic
					habitable_planet = yes
				}
			}
			if = {
				limit = {
					OR = {
						#has_colony_progress > 0 # TODO: [AUG-51261] Replace with pop count check
						is_colony = yes
					}
				}
				destroy_colony = yes
			}
			change_pc = pc_frozen
		}
		every_system_planet = {
			limit = { is_planet_class = pc_barren }
			change_pc = pc_barren_cold
		}
		remove_system_terraforming_candidates = yes
		every_system_ambient_object = {
			limit = {
				OR = {
					is_ambient_object_type = large_debris_object
					is_ambient_object_type = medium_debris_01_object
					is_ambient_object_type = medium_debris_02_object
					is_ambient_object_type = small_debris_object
				}
			}
			destroy_ambient_object = this
		}
		from = {
			set_country_flag = has_built_or_repaired_megastructure
			country_event = { id = utopia.2011 }
		}
		fromfrom.planet = {
			set_planet_flag = has-megastructure
			set_planet_flag = has_orbital
			planet_event = { id = oeo_orbital_dismantle.1 }
		}
	}
}

#Disco Sphere
dyson_sphere_disco = {
	entity = "dyson_sphere_phase_05_entity"
	construction_entity = "dyson_sphere_part_4_entity"
	portrait = "GFX_megastructure_dyson_sphere_background"
	outliner_trigger = {
		exists = owner
		owner = {
			NOT = { has_country_flag = oeo_sphere_outliner }
		}
	}
	build_time = 360
	resources = {
		category = megastructures_oeo_megastructures
		cost = {
			rare_crystals = 2500
			unity = 3000
		}
		produces = { unity = 750 }
		upkeep = { trade = @oeo_sphere_disco_trade_upkeep }
		
	}
	dismantle_cost = {
		category = megastructures
		cost = { energy = 10000 }
	}
	dismantle_time = 360
	dismantle_potential = {	always = yes }
	dismantle_possible = {
		custom_tooltip = {
			fail_text = "oeo_dismantle_cooldown" 
			NOT = { has_megastructure_flag = oeo_dismantle_cooldown }
		}
	}
	on_dismantle_start = {
		fromfrom.planet = {
			set_planet_flag = oeo_not_cancelled
			planet_event = { id = oeo_individual_dismantle.1 days = 361 }
		}
	}
	on_dismantle_cancel = {
		fromfrom = { set_timed_megastructure_flag = { flag = oeo_dismantle_cooldown days = 360 } }
		fromfrom.planet = { remove_planet_flag = oeo_not_cancelled }
	}
	on_dismantle_complete = {	
		from = {
			add_resource = {
				alloys = 10000
				mult = modifier:megastructure_dismantle_refund_mult
			}
		}
	}	
	upgrade_from = { dyson_sphere_5 }
	potential = {
		has_nemesis = yes
		has_country_flag = can_build_disco_sphere
	}
	possible = {
		from = {
			has_nemesis = yes
			has_country_flag = can_build_disco_sphere
		}
	}
	country_modifier = { planet_amenities_mult = 0.3 }
	on_build_start = { set_star_flag = dyson_sphere_construction }
	on_build_cancel = { remove_star_flag = dyson_sphere_construction }
	on_build_complete = {
		remove_star_flag = dyson_sphere_construction
		set_star_flag = mega_art_installation_built
		fromfrom = {
			create_ambient_object = {
				location = this
				type = kaleidoscope_happy
				entity_scale_to_size = yes
				scale = 0.50
			}
			create_ambient_object = {
				location = this
				type = kaleidoscope_neutral
				entity_scale_to_size = yes
				scale = 0.30
			}
			last_created_ambient_object = {
				set_location = {
					target = prev
					angle = 45
				}
			}
			create_ambient_object = {
				location = this
				type = kaleidoscope_angry
				entity_scale_to_size = yes
				scale = 0.20
			}
			last_created_ambient_object = {
				set_location = {
					target = prev
					angle = 90
				}
			}
		}
		from = {
			remove_country_flag = can_build_disco_sphere
			remove_country_flag = built_dyson_sphere
			remove_country_flag = built_dyson_sphere_contingency
			country_event = { id = situation.2049 }
		}
	}
}

dyson_sphere_disco_restored = {
	entity = "dyson_sphere_phase_05_entity"
	construction_entity = "dyson_sphere_part_4_entity"
	portrait = "GFX_megastructure_dyson_sphere_background"
	outliner_trigger = {
		exists = owner
		owner = {
			NOT = { has_country_flag = oeo_sphere_outliner }
		}
	}
	build_time = 360
	resources = {
		category = megastructures_oeo_megastructures
		cost = {
			rare_crystals = 2500
			unity = 3000
		}
		produces = { unity = 500 }
		upkeep = { trade = @oeo_sphere_disco_trade_upkeep }
	}
	dismantle_cost = {
		category = megastructures
		cost = { energy = 10000 }
	}
	dismantle_time = 360
	dismantle_potential = {	always = yes }
	dismantle_possible = {
		custom_tooltip = {
			fail_text = "oeo_dismantle_cooldown" 
			NOT = { has_megastructure_flag = oeo_dismantle_cooldown }
		}
	}
	on_dismantle_start = {
		fromfrom.planet = {
			set_planet_flag = oeo_not_cancelled
			planet_event = { id = oeo_individual_dismantle.1 days = 361 }
		}
	}
	on_dismantle_cancel = {
		fromfrom = { set_timed_megastructure_flag = { flag = oeo_dismantle_cooldown days = 360 } }
		fromfrom.planet = { remove_planet_flag = oeo_not_cancelled }
	}
	on_dismantle_complete = {	
		from = {
			add_resource = {
				alloys = 10000
				mult = modifier:megastructure_dismantle_refund_mult
			}
		}
	}	
	upgrade_from = { dyson_sphere_restored }
	potential = {
		has_nemesis = yes
		has_country_flag = can_build_disco_sphere
	}
	possible = {
		from = {
			has_nemesis = yes
			has_country_flag = can_build_disco_sphere
		}
		custom_tooltip = {
			fail_text = "requires_no_arc_furnace"
			system_has_arc_furnace = no
		}
		custom_tooltip = {
			fail_text = "requires_no_arc_furnace_construction"
			if = {
				limit = {
					system_has_arc_furnace = no
				}
				NOT = {
					solar_system = {
						has_star_flag = arc_furnace_construction
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_colonies"
			NOT = {
				any_system_planet = {
					OR = {
						sapient_pop_amount > 0
						is_colony = yes
					}
					is_artificial = no
					exists = owner
					owner = {
						is_primitive = no
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "blocked_by_pre_ftl_policy"
			if = {
				limit = {
					any_system_planet = {
						exists = owner
						owner = {
							is_primitive = yes
						}
					}
				}
				from = {
					has_policy_flag = interference_aggressive
				}
			}
		}
	}
	country_modifier = { planet_amenities_mult = 0.3 }
	on_build_start = { set_star_flag = dyson_sphere_construction }
	on_build_cancel = { remove_star_flag = dyson_sphere_construction }
	on_build_complete = {
		remove_star_flag = dyson_sphere_construction
		set_star_flag = mega_art_installation_built
		fromfrom = {
			create_ambient_object = {
				location = this
				type = kaleidoscope_happy
				entity_scale_to_size = yes
				scale = 0.50
			}
			create_ambient_object = {
				location = this
				type = kaleidoscope_neutral
				entity_scale_to_size = yes
				scale = 0.30
			}
			last_created_ambient_object = {
				set_location = {
					target = prev
					angle = 45
				}
			}
			create_ambient_object = {
				location = this
				type = kaleidoscope_angry
				entity_scale_to_size = yes
				scale = 0.20
			}
			last_created_ambient_object = {
				set_location = {
					target = prev
					angle = 90
				}
			}
		}
		from = {
			remove_country_flag = can_build_disco_sphere
			remove_country_flag = built_dyson_sphere
			remove_country_flag = built_dyson_sphere_contingency
			country_event = { id = situation.2049 }
		}
	}
}
