# Dyson Swarm First Stage
dyson_swarm_1 = {
	entity = "dyson_swarm_phase_1_entity"
	construction_entity = "dyson_swarm_phase_1_entity"
	portrait = "GFX_megastructure_dyson_swarm_background"
	entity_offset = { x = 0.1 y = 0.1 }
	rotate_to_center = no
	scale_offset = yes
	place_entity_on_planet_plane = yes
	#show_in_outliner = no
	use_planet_resource = yes
	build_time = 1080 # 3 years
	resources = {
		category = megastructures
		cost = {
			unity = 1000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 500
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 500
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 500
			mult = @halved_alloy_to_food_cost_ratio
		}

		upkeep = {
			alloys = 5
		}
	}

	construction_blocks_and_blocked_by = none
	custom_tooltip_requirements = "MEGASTRUCTURE_TOOLTIP_REQUIREMENTS_DYSON_SWARM"
	tooltip_show_star_resources = yes
	tooltip_best_systems_header = "MEGASTRUCTURE_DYSON_SWARM_BEST_SYSTEMS_TOOLTIP_HEADER"
	tooltip_system_filter = {
		system_has_dyson_swarm = no
		is_binary_star = no
		is_trinary_star = no
		NOR = {
			is_star_class = sc_black_hole
			is_star_class = sc_neutron_star
			is_star_class = sc_pulsar
		}
	}

	dismantle_cost = {
		category = megastructures
		cost = {
			energy = 500
		}
	}

	dismantle_time = 360


	dismantle_potential = {
		always = yes
	}

	dismantle_possible = {
		can_dismantle_megastructure = {
			TECH = tech_dyson_swarm
		}
	}

	on_dismantle_complete = {
		random_system_planet = {
			limit = {
				has_modifier = dyson_swarm_1_mod
			}
			remove_planet_flag = has_megastructure
			remove_modifier = dyson_swarm_1_mod
		}
		from = {
			add_resource = {
				alloys = 500
				mult = modifier:megastructure_dismantle_refund_mult
			}
			if = {
				limit = {
					check_variable = {
						which = dyson_swarm_counter
						value >= 1
					}
				}
				change_variable = {
					which = dyson_swarm_counter
					value = -1
				}
			}
		}
	}

	prerequisites = { "tech_dyson_swarm" }

	potential = {
		has_machine_age_dlc = yes
	}

	possible = {
		inline_script = megastructures/system_ownership_or_waystation_check
		inline_script = megastructures/system_surveyed_or_waystation_check
		custom_tooltip = {
			fail_text = "requires_no_binary_trinary"
			is_binary_star = no
			is_trinary_star = no
		}
		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
			}
		}
		custom_tooltip = {
			fail_text = "requires_less_than_x_dyson_swarms"
			from = {
				check_variable = {
					which = dyson_swarm_counter
					value <= value:dyson_swarm_limit
				}
			}
		}
	}

	placement_rules = {
		planet_possible = {
			custom_tooltip = {
				fail_text = "requires_no_protostar"
				NOT = {
					is_planet_class = pc_protostar
				}
			}
			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"
				NOR = {
					has_planet_flag = megastructure
					has_planet_flag = has_megastructure
				}
			}
			custom_tooltip = {
				fail_text = "requires_stable_star"
				NOT = {
					has_planet_flag = red_giant_unstable
				}
			}
			custom_tooltip = {
				fail_text = "requires_deposit_on_star"
				OR = {
					has_deposit_for = shipclass_mining_station
					has_deposit_for = shipclass_research_station
				}
			}
			if = {
				limit = {
					from = {
						is_ai = yes
					}
				}
				NOR = {
					has_deposit = d_energy_1
					has_deposit = d_energy_2
				}
			}
		}
	}

	custom_tooltip = dyson_swarm_1_mod_tooltip

	# root = system
	# from = country
	ai_weight = {
		factor = 5

		modifier = {
			factor = 0.1
			from = {
				is_nomadic = no
			}
			starbase = {
				NOT = {
					has_starbase_size >= starbase_starfortress
				}
			}
		}

		modifier = {
			factor = 0
			from = {
				is_nomadic = yes
			}
			NOT = {
				any_fleet_in_system = {
					OR = {
						is_ship_size = starbase_waystation_2
						is_ship_size = starbase_waystation_3
					}
					owner? = {
						is_same_value = from
					}
				}
			}
		}

		modifier = {
			factor = 0.1
			from = {
				is_nomadic = yes
			}
			owner? = {
				NOT = {
					benefits_from_waystation_network = {
						NOMAD = from
						SYSTEM_OWNER = this
					}
				}
			}
		}
		modifier = {
			factor = 0
			from = {
				check_variable = {
					which = dyson_swarm_counter
					value >= value:dyson_swarm_limit
				}
			}
		}
	}

	on_build_start = {
		from = {
			change_variable = {
				which = dyson_swarm_counter
				value = 1
			}
		}
	}
	on_build_cancel = {
		from = {
			if = {
				limit = {
					check_variable = {
						which = dyson_swarm_counter
						value >= 1
					}
				}
				change_variable = {
					which = dyson_swarm_counter
					value = -1
				}
			}
		}
	}
	on_build_complete = {
		set_star_flag = conduit_excluded
		fromfrom.planet = {
			add_modifier = {
				modifier = dyson_swarm_1_mod
				days = -1
			}
			set_planet_flag = has_megastructure
			if = {
				limit = {
					has_orbital_station = yes
				}
				orbital_station = {
					dismantle = yes
				}
			}
		}
	}
}

# Dyson Swarm Middle Stage
dyson_swarm_2 = {
	entity = "dyson_swarm_phase_2_entity"
	construction_entity = "dyson_swarm_phase_2_entity"
	portrait = "GFX_megastructure_dyson_swarm_background"
	entity_offset = { x = -0.1 y = -0.1 }
	rotate_to_center = no
	scale_offset = yes
	place_entity_on_planet_plane = yes
	#show_in_outliner = no
	use_planet_resource = yes
	build_time = 1080 # 3 years
	resources = {
		category = megastructures
		cost = {
			unity = 2500
		}
		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 = {
			alloys = 10
		}
	}

	construction_blocks_and_blocked_by = none

	dismantle_cost = {
		category = megastructures
		cost = {
			energy = 1000
		}
	}

	dismantle_time = 360

	dismantle_potential = {
		always = yes
	}

	dismantle_possible = {
		can_dismantle_megastructure = {
			TECH = tech_dyson_swarm
		}
	}

	on_dismantle_complete = {
		random_system_planet = {
			limit = {
				has_modifier = dyson_swarm_2_mod
			}
			remove_planet_flag = has_megastructure
			remove_modifier = dyson_swarm_2_mod
		}
		from = {
			add_resource = {
				alloys = 1000
				mult = modifier:megastructure_dismantle_refund_mult
			}
			if = {
				limit = {
					check_variable = {
						which = dyson_swarm_counter
						value >= 1
					}
				}
				change_variable = {
					which = dyson_swarm_counter
					value = -1
				}
			}
		}
	}

	upgrade_from = {
		dyson_swarm_1
	}

	prerequisites = { "tech_dyson_swarm" }

	potential = {
		always = yes
	}

	custom_tooltip = dyson_swarm_2_mod_tooltip

	on_build_complete = {
		fromfrom.planet = {
			remove_modifier = "dyson_swarm_1_mod"
			add_modifier = {
				modifier = dyson_swarm_2_mod
				days = -1
			}
		}
	}
}

# Dyson Swarm Final Stage
dyson_swarm_3 = {
	entity = "dyson_swarm_phase_3_entity"
	construction_entity = "dyson_swarm_phase_3_entity"
	portrait = "GFX_megastructure_dyson_swarm_background"
	entity_offset = { x = -0.1 y = -0.1 }
	rotate_to_center = no
	scale_offset = yes
	place_entity_on_planet_plane = yes
	use_planet_resource = yes
	outliner_trigger = {
		exists = owner
		system_has_arc_furnace = no
		NOT = {
			solar_system = {
				has_star_flag = arc_furnace_construction
			}
		}
		NOT = {
			solar_system = {
				any_system_planet = {
					is_colony = yes
					is_artificial = no
					owner? = {
						is_primitive = no
					}
				}
			}
		}
		if = {
			limit = {
				solar_system = {
					any_system_planet = {
						owner? = {
							is_primitive = yes
						}
					}
				}
			}
			owner = {
				has_policy_flag = interference_aggressive
			}
		}
		owner = {
			has_technology = tech_dyson_sphere
			OR = {
				NOR = {
					has_country_flag = built_dyson_sphere_swarm
					has_country_flag = built_dyson_sphere
				}
				AND = {
					has_relic = r_contingency_core
					NOR = {
						has_country_flag = built_dyson_sphere_contingency_swarm
						has_country_flag = built_dyson_sphere_contingency
					}
				}
			}
		}
	}

	construction_blocks_and_blocked_by = none

	dismantle_cost = {
		category = megastructures
		cost = {
			energy = 1500
		}
	}

	dismantle_time = 360

	dismantle_potential = {
		always = yes
	}

	dismantle_possible = {
		can_dismantle_megastructure = {
			TECH = tech_dyson_swarm
		}
	}

	on_dismantle_complete = {
		random_system_planet = {
			limit = {
				has_modifier = dyson_swarm_3_mod
			}
			remove_planet_flag = has_megastructure
			remove_modifier = dyson_swarm_3_mod
		}
		from = {
			add_resource = {
				alloys = 1500
				mult = modifier:megastructure_dismantle_refund_mult
			}
			if = {
				limit = {
					check_variable = {
						which = dyson_swarm_counter
						value >= 1
					}
				}
				change_variable = {
					which = dyson_swarm_counter
					value = -1
				}
			}
		}
		##Deal with Dyson Sphere flag shenanigans
		if = {
			limit = {
				solar_system = {
					has_star_flag = dyson_sphere_construction
				}
			}
			from = {
				if = {
					limit = {
						has_country_flag = built_dyson_sphere_contingency_swarm
					}
					remove_country_flag = built_dyson_sphere_contingency_swarm
				}
				if = {
					limit = {
						has_country_flag = built_dyson_sphere_swarm
					}
					remove_country_flag = built_dyson_sphere_swarm
				}
			}
		}
	}

	build_time = 1080 # 3 years
	resources = {
		category = megastructures
		cost = {
			unity = 5000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 1500
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 1500
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 1500
			mult = @halved_alloy_to_food_cost_ratio
		}

		upkeep = {
			alloys = 20
		}
	}
	upgrade_from = {
		dyson_swarm_2
	}

	prerequisites = { "tech_dyson_swarm" }

	potential = {
		always = yes
	}

	custom_tooltip = dyson_swarm_3_mod_tooltip

	on_build_complete = {
		fromfrom.planet = {
			remove_modifier = "dyson_swarm_2_mod"
			add_modifier = {
				modifier = dyson_swarm_3_mod
				days = -1
			}
		}
		if = {
			limit = {
				any_system_megastructure = {
					OR = {
						is_megastructure_type = orbital_arc_furnace_4
						is_megastructure_type = orbital_arc_furnace_restored
					}
				}
			}
			from = {
				set_country_flag = burning_brightly_achievement
			}
		}
	}
}
