# Matter Decompressor Site 
matter_decompressor_0 = {
	entity = "construction_platform_entity"
	construction_entity = "construction_platform_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	entity_offset = { x = -1.1 y = -2.8 }
	rotate_to_center = no
	scale_offset = yes
	build_time = 1800
	resources = {
		category = megastructures
		cost = {
			alloys = 5000
			unity = 5000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 5000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 5000
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 5000
			mult = @halved_alloy_to_food_cost_ratio
		}

		upkeep = {
			energy = 5
		}
	}

	custom_tooltip_requirements = "MEGASTRUCTURE_TOOLTIP_REQUIREMENTS_MATTER_DECOMPRESSOR"

	prerequisites = { "tech_matter_decompressor" }

	potential = {
		has_gw_matter_decompressor = yes
	}

	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_habitable_planets"
			NOT = {
				any_system_planet = {
					colonizable_planet = yes
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_black_hole"
			OR = {
				is_star_class = sc_black_hole
				is_star_class = sc_microquasar_1
				is_star_class = sc_microquasar_2
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_existing_matter_decompressor"
			from = {
				OR = {
					has_relic = r_contingency_core
					NOT = { has_country_flag = built_matter_decompressor_site }
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_existing_matter_decompressor_two"
			from = { NOT = { has_country_flag = built_matter_decompressor_site_contingency } }
		}
		custom_tooltip = {
			fail_text = "requires_no_existing_megastructure"
			has_no_non_gate_megastructure = yes
			NOT = {
				any_system_planet = {
					is_planet_class = pc_cosmogenesis_world
				}
			}
		}
	}

	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 }
			}
			is_star_class = sc_black_hole
		}
	}

	# 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 }
				}
			}
		}
	}

	on_build_start = {}
	on_build_cancel = {}
	on_build_complete = {
		from = {
			if = {
				limit = {
					has_relic = r_contingency_core
					has_country_flag = built_matter_decompressor_site
				}
				set_country_flag = built_matter_decompressor_site_contingency
			}
			set_country_flag = built_matter_decompressor_site
		}
		fromfrom.planet = {
			if = {
				limit = { has_orbital_station = yes }
				orbital_station = {
					dismantle = yes
				}
			}
		}
	}
}

# Matter Decompressor
matter_decompressor_1 = {
	entity = "matter_decompressor_stage_1_entity"
	construction_entity = "matter_decompressor_stage_1_construction_entity"
	portrait = "GFX_megastructure_construction_background"
	entity_offset = { x = -1.1 y = -2.8 }
	rotate_to_center = yes
	scale_offset = yes
	place_entity_on_planet_plane = no
	build_time = 3600
	resources = {
		category = megastructures
		cost = {
			alloys = 12500
			unity = 5000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 12500
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 12500
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 12500
			mult = @halved_alloy_to_food_cost_ratio
		}

		produces = {
			minerals = 500
		}

		upkeep = {
			energy = 25
		}
	}

	upgrade_from = {
		matter_decompressor_0
	}

	prerequisites = { "tech_matter_decompressor" }

	on_build_complete = {
		from = { country_event = { id = mega.9010 } }
	}
}

# Matter Decompressor
matter_decompressor_2 = {
	entity = "matter_decompressor_stage_2_entity"
	construction_entity = "matter_decompressor_stage_2_construction_entity"
	portrait = "GFX_megastructure_construction_background"
	entity_offset = { x = -1.1 y = -2.8 }
	rotate_to_center = yes
	scale_offset = yes
	place_entity_on_planet_plane = no
	build_time = 3600
	resources = {
		category = megastructures
		cost = {
			unity = 5000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 12500
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 12500
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 12500
			mult = @halved_alloy_to_food_cost_ratio
		}

		produces = {
			minerals = 1000
		}

		upkeep = {
			energy = 50
		}
	}
	upgrade_from = {
		matter_decompressor_1
	}

	prerequisites = { "tech_matter_decompressor" }

	on_build_complete = {
		from = { country_event = { id = mega.9011 } }
	}
}

# Matter Decompressor
matter_decompressor_3 = {
	entity = "matter_decompressor_stage_3_entity"
	construction_entity = "matter_decompressor_stage_3_construction_entity"
	portrait = "GFX_megastructure_construction_background"
	entity_offset = { x = -1.1 y = -2.8 }
	rotate_to_center = yes
	scale_offset = yes
	place_entity_on_planet_plane = no
	build_time = 3600
	resources = {
		category = megastructures
		cost = {
			unity = 5000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 12500
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 12500
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 12500
			mult = @halved_alloy_to_food_cost_ratio
		}

		produces = {
			minerals = 1500
		}

		upkeep = {
			energy = 75
		}
	}

	upgrade_from = {
		matter_decompressor_2
	}

	prerequisites = { "tech_matter_decompressor" }

	on_build_complete = {
		from = { country_event = { id = mega.9012 } }
	}
}

# Matter Decompressor
matter_decompressor_4 = {
	entity = "matter_decompressor_stage_4_entity"
	construction_entity = "matter_decompressor_stage_4_construction_entity"
	portrait = "GFX_megastructure_matter_decompressor_background"
	entity_offset = { x = -1.1 y = -2.8 }
	rotate_to_center = yes
	scale_offset = yes
	place_entity_on_planet_plane = no
	build_time = 3600
	resources = {
		category = megastructures
		cost = {
			unity = 5000
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 12500
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 12500
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 12500
			mult = @halved_alloy_to_food_cost_ratio
		}

		produces = {
			minerals = 2000
		}

		upkeep = {
			energy = 100
		}
	}

	upgrade_from = {
		matter_decompressor_3
	}

	prerequisites = { "tech_matter_decompressor" }
	show_prereqs = yes

	on_build_complete = {
		set_star_flag = matter_decompressor_built
		from = {
			set_country_flag = has_built_or_repaired_megastructure
			country_event = { id = mega.9013 }
		}
	}
}

# Permanently Ruined Matter Decompressor
matter_decompressor_permanently_ruined = {
	entity = "matter_decompressor_destroyed_01_entity"
	construction_entity = "matter_decompressor_stage_4_construction_entity"
	portrait = "GFX_megastructure_construction_background"
	entity_offset = { x = -1.1 y = -2.8 }
	rotate_to_center = yes
	scale_offset = yes
	place_entity_on_planet_plane = no
	show_galactic_map_icon = no
	show_in_outliner = no

	potential = {
		always = no
	}
}

# Ruined Matter Decompressor
matter_decompressor_ruined = {
	entity = "matter_decompressor_destroyed_01_entity"
	construction_entity = "matter_decompressor_stage_4_construction_entity"
	portrait = "GFX_megastructure_construction_background"
	entity_offset = { x = -1.1 y = -2.8 }
	rotate_to_center = yes
	scale_offset = yes
	place_entity_on_planet_plane = no

	potential = {
		always = no
	}
}

# Restored Matter Decompressor
matter_decompressor_restored = {
	entity = "matter_decompressor_stage_4_entity"
	construction_entity = "matter_decompressor_stage_4_construction_entity"
	portrait = "GFX_megastructure_matter_decompressor_background"
	entity_offset = { x = -1.1 y = -2.8 }
	rotate_to_center = yes
	scale_offset = yes
	place_entity_on_planet_plane = no
	build_time = 4800
	resources = {
		category = 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
		}

		produces = {
			minerals = 2000
		}

		upkeep = {
			energy = 100
		}
	}

	upgrade_from = {
		matter_decompressor_ruined
	}

	possible = {
		from = { has_technology = tech_mega_engineering }
	}

	on_build_start = {
		fromfrom = {
			set_graphical_culture = root.from
		}
	}

	on_build_complete = {
		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
				}
			}
			destroy_ambient_object = this
		}
		set_star_flag = matter_decompressor_built
		from = {
			set_country_flag = has_built_or_repaired_megastructure
			country_event = { id = mega.9101 }
		}
	}
}