# scope must be fleet (construction ship)
auto_construct_hyper_relay_check = {
	$CHECK_SYSTEM$ = {
		exists = starbase
		OR = {
			is_inside_border = prev.owner
			AND = {
				exists = owner
				owner = {
					is_subject = yes
					overlord = { is_same_value = prevprevprev.owner }
				}
			}
		}
		OR = {
			NOT = {
				any_system_planet = {
					is_surveyed = {
						who = prevprev.owner
						status = no
					}
				}
			}
			AND = {
				exists = owner
				owner = {
					is_subject = yes
					overlord = { is_same_value = prevprevprev.owner }
				}
			}
		}
		NOR = {
			has_megastructure = hyper_relay
			has_megastructure = hyper_relay_ruined
			has_megastructure = hyper_relay_restored
		}
		count_neighbor_system = {
			count > 0
		}
		NOT = {
			any_ship_in_system = {
				exists = owner
				OR = {
					is_constructing = hyper_relay
					owner = {
						is_hostile = prevprevprev.owner
					}
				}
			}
		}
	}
	[[CHECK_PLANET]
		$CHECK_PLANET$ = {
			is_primary_star = yes
		}
	]
}

auto_construct_gateway_check = {
	$CHECK_SYSTEM$ = {
		exists = starbase
		OR = {
			is_inside_border = prev.owner
			AND = {
				exists = owner
				owner = {
					is_subject = yes
					overlord = { is_same_value = prevprevprev.owner }
				}
			}
		}
		OR = {
			NOT = {
				any_system_planet = {
					is_surveyed = {
						who = prevprev.owner
						status = no
					}
				}
			}
			AND = {
				exists = owner
				owner = {
					is_subject = yes
					overlord = { is_same_value = prevprevprev.owner }
				}
			}
		}
		NOR = {
			has_megastructure = gateway_0
			has_megastructure = gateway_ruined
			has_megastructure = gateway_restored
			has_megastructure = gateway_final
		}
		NOT = {
			any_ship_in_system = {
				exists = owner
				OR = {
					if = {
						limit = {
							prevprev.owner = {
								has_global_flag = giga_game_started
							}
						}
						is_constructing = gateway_final
					}
					else = {
						is_constructing = gateway_0
					}
					owner = {
						is_hostile = prevprevprev.owner
					}
				}
			}
		}
	}
	[[CHECK_PLANET]
		$CHECK_PLANET$ = {
			is_primary_star = yes
		}
	]
}

auto_construct_habitat_central_check = {
	$CHECK_SYSTEM$ = {
		exists = starbase
		is_inside_border = prev.owner
		NOR = {
			OR = {
				any_system_planet = {
					has_planet_flag = habitat
				}
				has_megastructure = habitat_central_complex_ruined
			}
			any_ship_in_system = {
				exists = owner
				OR = {
					is_constructing = habitat_central_complex
					owner = {
						is_hostile = prevprevprev.owner
					}
				}
			}
			has_star_flag = solarpunk_system_02
			OR = {
				has_star_flag = ring_world_built
				has_star_flag = ithomes_gate
			}
		}
		any_system_planet = {
			is_surveyed = {
				who = prevprev.owner
				status = yes
			}
			NOR = {
				has_planet_flag = megastructure
				has_planet_flag = has_megastructure
				has_planet_flag = has_orbital_debris
				has_anomaly = yes
				is_asteroid = yes
				is_moon = yes
				is_planet_class = pc_ringworld_habitable
				is_planet_class = pc_ringworld_habitable_damaged
				is_planet_class = pc_ringworld_tech
				is_planet_class = pc_ringworld_tech_damaged
				is_planet_class = pc_ringworld_seam
				is_planet_class = pc_ringworld_seam_damaged
				is_planet_class = pc_habitat
			}
		}
	}
	[[CHECK_PLANET]
		$CHECK_PLANET$ = {
			is_primary_star = yes
			is_surveyed = {
				who = prev.owner
				status = yes
			}
			NOR = {
				has_planet_flag = megastructure
				has_planet_flag = has_megastructure
				has_planet_flag = has_orbital_debris
				has_anomaly = yes
				is_asteroid = yes
				is_moon = yes
				is_planet_class = pc_ringworld_habitable
				is_planet_class = pc_ringworld_habitable_damaged
				is_planet_class = pc_ringworld_tech
				is_planet_class = pc_ringworld_tech_damaged
				is_planet_class = pc_ringworld_seam
				is_planet_class = pc_ringworld_seam_damaged
				is_planet_class = pc_habitat
			}
		}
	]
}

auto_construct_frameworld_outpost_check = {
	$CHECK_SYSTEM$ = {
		exists = starbase
		is_inside_border = prev.owner
		any_system_planet = {
			# is_surveyed = {
			# 	who = prevprev.owner
			# 	status = yes
			# }
			OR = {
				AND = {
					is_colonizable = yes
					NOT = {
						is_artificial = yes
					}
				}

				is_planet_class = pc_giga_planetary_computer
				is_planet_class = pc_ringworld_habitable
				is_planet_class = pc_shattered_ring_habitable
				is_planet_class = pc_habitable_gas_giant
				is_planet_class = pc_interstellar_ringworld_habitable
				is_planet_class = pc_giga_penrose_habitable
				is_planet_class = pc_birch
				is_planet_class = pc_squareworld_habitable
				is_planet_class = pc_alderson_slice_gaia
				is_planet_class = pc_alderson_slice_gaia_shattered
				is_planet_class = pc_flat_world
			}

			NOR = {
				has_anomaly = yes
				is_colony = yes
				is_asteroid = yes
				is_terraforming = yes
				is_artificial = yes
				is_star = yes
				has_frameworld_outpost = yes

				any_fleet_in_orbit = {
					OR = {
						is_constructing = frameworld_outpost_construction
						is_constructing = frameworld_outpost_construction_lithoid
						is_constructing = frameworld_outpost_construction_hive
						is_constructing = frameworld_outpost_construction_hive_lithoid
						is_constructing = frameworld_outpost_construction_machine
					}
				}
			}
		}
		NOT = {
			any_ship_in_system = {
				exists = owner
				owner = {
					is_hostile = prevprevprev.owner
				}
			}
		}
	}
	[[CHECK_PLANET]
		$CHECK_PLANET$ = {
			# is_surveyed = {
			# 	who = prevprev.owner
			# 	status = yes
			# }
			OR = {
				AND = {
					is_colonizable = yes
					NOT = {
						is_artificial = yes
					}
				}

				is_planet_class = pc_giga_planetary_computer
				is_planet_class = pc_ringworld_habitable
				is_planet_class = pc_shattered_ring_habitable
				is_planet_class = pc_habitable_gas_giant
				is_planet_class = pc_interstellar_ringworld_habitable
				is_planet_class = pc_giga_penrose_habitable
				is_planet_class = pc_birch
				is_planet_class = pc_squareworld_habitable
				is_planet_class = pc_alderson_slice_gaia
				is_planet_class = pc_alderson_slice_gaia_shattered
				is_planet_class = pc_flat_world
			}

			NOR = {
				has_anomaly = yes
				is_colony = yes
				is_asteroid = yes
				is_terraforming = yes
				is_artificial = yes
				is_star = yes
				has_frameworld_outpost = yes

				any_fleet_in_orbit = {
					OR = {
						is_constructing = frameworld_outpost_construction
						is_constructing = frameworld_outpost_construction_lithoid
						is_constructing = frameworld_outpost_construction_hive
						is_constructing = frameworld_outpost_construction_hive_lithoid
						is_constructing = frameworld_outpost_construction_machine
					}
				}
			}
		}
	]
}

auto_construct_frameworld_harvest_check = {
	$CHECK_SYSTEM$ = {
		exists = starbase
		is_inside_border = prev.owner
		any_system_planet = {
			# is_surveyed = {
			# 	who = prevprev.owner
			# 	status = yes
			# }
			is_asteroid = yes

			NOR = {
				has_anomaly = yes
				is_astral_scar = yes
				is_planet_habitable = yes
				has_planet_flag = megastructure
				has_planet_flag = has_megastructure

				is_planet_class = pc_giga_empty_space_asteroid

				any_fleet_in_orbit = {
					is_constructing = frameworld_harvest_0
				}
				has_deposit = d_artifacts_mining_1
				has_deposit = d_artifacts_mining_1
				has_deposit = d_artifacts_mining_1
				has_deposit = d_artifacts_planet_1
				has_deposit = d_artifacts_planet_2
				has_deposit = d_artifacts_planet_3
				has_deposit = d_artifacts_research_1
				has_deposit = d_artifacts_research_2
				has_deposit = d_artifacts_research_3
			}
		}
		NOT = {
			any_ship_in_system = {
				exists = owner
				owner = {
					is_hostile = prevprevprev.owner
				}
			}
		}
	}
	[[CHECK_PLANET]
		$CHECK_PLANET$ = {
			# is_surveyed = {
			# 	who = prevprev.owner
			# 	status = yes
			# }
			is_asteroid = yes

			NOR = {
				has_anomaly = yes
				is_astral_scar = yes
				is_planet_habitable = yes
				has_planet_flag = megastructure
				has_planet_flag = has_megastructure

				is_planet_class = pc_giga_empty_space_asteroid

				any_fleet_in_orbit = {
					is_constructing = frameworld_harvest_0
				}
				has_deposit = d_artifacts_mining_1
				has_deposit = d_artifacts_mining_1
				has_deposit = d_artifacts_mining_1
				has_deposit = d_artifacts_planet_1
				has_deposit = d_artifacts_planet_2
				has_deposit = d_artifacts_planet_3
				has_deposit = d_artifacts_research_1
				has_deposit = d_artifacts_research_2
				has_deposit = d_artifacts_research_3
			}
		}
	]
}

auto_construct_frameworld_salvage_habitat_check = {
	$CHECK_SYSTEM$ = {
		exists = starbase
		is_inside_border = prev.owner
		any_system_planet = {
			is_surveyed = {
				who = prevprev.owner
				status = yes
			}
			is_planet_class = pc_habitat

			NOR = {
				is_colony = yes
				has_anomaly = yes
				is_astral_scar = yes

				any_fleet_in_orbit = {
					is_constructing = frameworld_salvage_habitat_0
				}
			}
		}
		NOT = {
			any_ship_in_system = {
				exists = owner
				owner = {
					is_hostile = prevprevprev.owner
				}
			}
		}
	}
	[[CHECK_PLANET]
		$CHECK_PLANET$ = {
			is_surveyed = {
				who = prevprev.owner
				status = yes
			}
			is_planet_class = pc_habitat

			NOR = {
				is_colony = yes
				has_anomaly = yes
				is_astral_scar = yes

				any_fleet_in_orbit = {
					is_constructing = frameworld_salvage_habitat_0
				}
			}
		}
	]
}

auto_construct_orbital_ring_check = {
	$CHECK_SYSTEM$ = {
		exists = starbase
		is_inside_border = prev.owner
		NOT = {
			any_ship_in_system = {
				exists = owner
				owner = {
					is_hostile = prevprevprev.owner
				}
			}
		}

		any_system_planet = {
			is_surveyed = {
				who = prevprev.owner
				status = yes
			}
			is_colony = yes
			exists = owner
			exists = controller
			controller = {
				is_same_value = prevprevprev.owner
			}
			NOR = {
				has_anomaly = yes
				planet_is_hostile_or_crisis = yes
				AND = {
					exists = owner
					owner = {
						is_country_type = primitive
					}
				}
				has_planet_flag = megastructure
				AND = {
					has_planet_flag = has_megastructure
					# Special exception so you can build an orbital ring even if you have MSI's warship as a habitat.
					NOT = {
						has_planet_flag = has_payback_habitat
					}
				}
				has_planet_flag = ruined_orbital_ring_planet
				solar_system = {
					has_star_flag = ring_world_built
				}
				is_artificial = yes
				has_planet_flag = has_orbital_debris
				any_fleet_in_orbit = {
					is_constructing = orbital_ring
				}
			}
		}
	}
	[[CHECK_PLANET]
		$CHECK_PLANET$ = {
			is_surveyed = {
				who = prev.owner
				status = yes
			}
			is_colony = yes
			exists = owner
			exists = controller
			controller = {
				is_same_value = prevprev.owner
			}
			NOR = {
				has_anomaly = yes
				planet_is_hostile_or_crisis = yes
				AND = {
					exists = owner
					owner = {
						is_country_type = primitive
					}
				}
				has_planet_flag = megastructure
				AND = {
					has_planet_flag = has_megastructure
					# Special exception so you can build an orbital ring even if you have MSI's warship as a habitat.
					NOT = {
						has_planet_flag = has_payback_habitat
					}
				}
				has_planet_flag = ruined_orbital_ring_planet
				solar_system = {
					has_star_flag = ring_world_built
				}
				is_artificial = yes
				has_planet_flag = has_orbital_debris
				any_fleet_in_orbit = {
					is_constructing = orbital_ring
				}
			}
		}
	]
}

auto_construct_dyson_swarm_check = {
	$CHECK_SYSTEM$ = {
		prev.owner = {
			check_variable = {
				which = dyson_swarm_counter
				value < value:dyson_swarm_limit
			}
		}
		exists = starbase
		is_inside_border = prev.owner
		NOT = {
			any_ship_in_system = {
				exists = owner
				owner = {
					is_hostile = prevprevprev.owner
				}
			}
		}
		NOR = {
			is_star_class = sc_binary_1
			is_star_class = sc_binary_2
			is_star_class = sc_binary_3
			is_star_class = sc_binary_4
			is_star_class = sc_binary_5
			is_star_class = sc_binary_6
			is_star_class = sc_binary_7
			is_star_class = sc_binary_8
			is_star_class = sc_binary_9
			is_star_class = sc_binary_10
			is_star_class = sc_trinary_1
			is_star_class = sc_trinary_2
			is_star_class = sc_trinary_3
			is_star_class = sc_trinary_4

			is_star_class = sc_black_hole
			is_star_class = sc_neutron_star
			is_star_class = sc_pulsar
		}
		any_system_planet = {
			is_surveyed = {
				who = prevprev.owner
				status = yes
			}
			NOT = { has_anomaly = yes }
			is_star = yes
			NOR = {
				has_planet_flag = megastructure
				has_planet_flag = has_megastructure
			}
			NOT = {
				any_fleet_in_orbit = {
					is_constructing = dyson_swarm_1
				}
			}
			has_deposit_for = shipclass_mining_station
			NOR = {
				has_deposit = d_energy_1
				has_deposit = d_energy_2
				has_deposit = d_energy_3
				has_deposit = d_energy_4
				has_deposit = d_physics_1
				has_deposit = d_physics_2
				has_deposit = d_physics_3
				has_deposit = d_physics_4
			}
		}
	}
	[[CHECK_PLANET]
		$CHECK_PLANET$ = {
			is_surveyed = {
				who = prev.owner
				status = yes
			}
			NOT = { has_anomaly = yes }
			is_star = yes
			NOR = {
				has_planet_flag = megastructure
				has_planet_flag = has_megastructure
			}
			NOT = {
				any_fleet_in_orbit = {
					is_constructing = dyson_swarm_1
				}
			}
			has_deposit_for = shipclass_mining_station
			NOR = {
				has_deposit = d_energy_1
				has_deposit = d_energy_2
				has_deposit = d_energy_3
				has_deposit = d_energy_4
				has_deposit = d_physics_1
				has_deposit = d_physics_2
				has_deposit = d_physics_3
				has_deposit = d_physics_4

				is_star_class = sc_binary_1
				is_star_class = sc_binary_2
				is_star_class = sc_binary_3
				is_star_class = sc_binary_4
				is_star_class = sc_binary_5
				is_star_class = sc_binary_6
				is_star_class = sc_binary_7
				is_star_class = sc_binary_8
				is_star_class = sc_binary_9
				is_star_class = sc_binary_10
				is_star_class = sc_trinary_1
				is_star_class = sc_trinary_2
				is_star_class = sc_trinary_3
				is_star_class = sc_trinary_4

				is_star_class = sc_black_hole
				is_star_class = sc_neutron_star
				is_star_class = sc_pulsar
			}
		}
	]
}

auto_construct_arc_furnace_check = {
	$CHECK_SYSTEM$ = {
		prev.owner = {
			check_variable = {
				which = arc_furnace_counter
				value < value:arc_furnace_limit
			}
		}
		exists = starbase
		is_inside_border = prev.owner
		NOT = {
			any_ship_in_system = {
				exists = owner
				owner = {
					is_hostile = prevprevprev.owner
				}
			}
		}
		any_system_planet = {
			is_surveyed = {
				who = prevprev.owner
				status = yes
			}
			NOT = { has_anomaly = yes }
			is_planet_class = pc_molten
			NOR = {
				has_planet_flag = megastructure
				has_planet_flag = has_megastructure
			}
			NOT = {
				any_fleet_in_orbit = {
					is_constructing = orbital_arc_furnace_1
				}
			}
		}
		system_has_arc_furnace = no
		system_has_dyson_sphere = no
		if = {
			limit = {
				system_has_dyson_sphere = no
			}
			NOT = {
				has_star_flag = dyson_sphere_construction
			}
		}
		count_system_planet = {
			count > 8
			limit = {
				NOT = { has_deposit_for = shipclass_research_station }
				is_star = no
				is_astral_scar = no
				colonizable_planet = no
			}
		}
	}
	[[CHECK_PLANET]
		$CHECK_PLANET$ = {
			is_surveyed = {
				who = prev.owner
				status = yes
			}
			NOT = { has_anomaly = yes }
			is_planet_class = pc_molten
			NOR = {
				has_planet_flag = megastructure
				has_planet_flag = has_megastructure
			}
			NOT = {
				any_fleet_in_orbit = {
					is_constructing = orbital_arc_furnace_1
				}
			}
		}
	]
}

auto_construct_deep_space_citadel_check = {
	$CHECK_SYSTEM$ = {
		exists = starbase
		is_inside_border = prev.owner
		NOT = {
			any_system_planet = {
				is_surveyed = {
					who = prevprev.owner
					status = no
				}
			}
		}
		# count_ship_in_system = {
		# 	limit = {
		# 		is_space_citadel_ship_size = yes
		# 	}
		# 	count < prevprev.owner.modifier:deep_space_citadel_limit_add
		# }
		NOT = {
			any_ship_in_system = {
				exists = owner
				OR = {
					is_constructing = deep_space_citadel_0
					owner = {
						is_hostile = prevprevprev.owner
					}
				}
			}
		}
		count_ship_in_system = {
			limit = {
				is_space_citadel_ship_size = yes
			}
			count < 1
		}
	}
	[[CHECK_PLANET]
		$CHECK_PLANET$ = {
			is_primary_star = yes
		}
	]
}

auto_construct_planetary_computer_check = {
	$CHECK_SYSTEM$ = {
		prev.owner = {
			OR = {
				has_global_flag = planetary_computer_capped_u
				check_variable = {
					which = giga_current_planetary_computer
					value < value:giga_planetary_computer_limit
				}
			}
		}
		exists = starbase
		is_inside_border = prev.owner
		NOT = {
			any_ship_in_system = {
				exists = owner
				owner = {
					is_hostile = prevprevprev.owner
				}
			}
		}

		any_system_planet = {
			is_surveyed = {
				who = prevprev.owner
				status = yes
			}
			is_planet_habitable = yes
			OR = {
				NOT = {
					exists = owner
				}
				owner = {
					is_same_value = prevprev.space_owner
				}
			}

			NOR = {
				is_star = yes
				has_anomaly = yes
				is_asteroid = yes
				is_astral_scar = yes
				planet_is_hostile_or_crisis = yes
				is_colony = yes
				has_planet_flag = has_planet_pc_mega
				is_planet_class = pc_giga_planetary_computer
				habitable_structure = yes

				has_planet_flag = megastructure
				has_planet_flag = has_megastructure
				has_planet_flag = crisis_vital_planet

				any_fleet_in_orbit = {
					is_constructing = planetary_computer_0
				}
			}
		}
	}
	[[CHECK_PLANET]
		$CHECK_PLANET$ = {
			is_surveyed = {
				who = prev.owner
				status = yes
			}
			is_planet_habitable = yes
			OR = {
				NOT = {
					exists = owner
				}
				owner = {
					is_same_value = prevprev.space_owner
				}
			}

			NOR = {
				is_star = yes
				has_anomaly = yes
				is_asteroid = yes
				is_astral_scar = yes
				planet_is_hostile_or_crisis = yes
				is_colony = yes
				has_planet_flag = has_planet_pc_mega
				is_planet_class = pc_giga_planetary_computer
				habitable_structure = yes

				has_planet_flag = megastructure
				has_planet_flag = has_megastructure
				has_planet_flag = crisis_vital_planet

				any_fleet_in_orbit = {
					is_constructing = planetary_computer_0
				}
			}
		}
	]
}

auto_construct_terraform_site_check = {
	$CHECK_SYSTEM$ = {
		prev.owner = {
			OR = {
				OR = {
					has_global_flag = geothermal_capped_u
					check_variable = {
						which = giga_current_geothermal
						value < value:giga_geothermal_limit
					}
				}
				OR = {
					has_global_flag = terraform_toxic_capped_u
					check_variable = {
						which = giga_current_terraform_toxic
						value < value:giga_terraform_toxic_limit
					}
				}
				OR = {
					has_global_flag = terraform_barren_capped_u
					check_variable = {
						which = giga_current_terraform_barren
						value < value:giga_terraform_barren_limit
					}
				}
			}
		}
		exists = starbase
		is_inside_border = prev.owner
		NOT = {
			any_ship_in_system = {
				exists = owner
				owner = {
					is_hostile = prevprevprev.owner
				}
			}
		}

		any_system_planet = {
			is_surveyed = {
				who = prevprev.owner
				status = yes
			}

			OR = {
				AND = {
					is_planet_class = pc_molten
					prevprev.owner = {
						OR = {
							has_global_flag = geothermal_capped_u
							check_variable = {
								which = giga_current_geothermal
								value < value:giga_geothermal_limit
							}
						}
					}
				}
				AND = {
					is_planet_class = pc_toxic
					prevprev.owner = {
						OR = {
							OR = {
								has_global_flag = terraform_toxic_capped_u
								check_variable = {
									which = giga_current_terraform_toxic
									value < value:giga_terraform_toxic_limit
								}
							}
						}
					}
				}
				AND = {
					OR = {
						is_planet_class = pc_barren
						is_planet_class = pc_barren_cold
						is_planet_class = pc_frozen
					}
					prevprev.owner = {
						OR = {
							OR = {
								has_global_flag = terraform_barren_capped_u
								check_variable = {
									which = giga_current_terraform_barren
									value < value:giga_terraform_barren_limit
								}
							}
						}
					}
				}
			}

			NOR = {
				AND = {
					has_modifier = terraforming_candidate
					giga_is_barren_cn = yes
				}
				AND = {
					has_modifier = toxic_terraforming_candidate
					giga_is_toxic = yes
					from = { has_ascension_perk = ap_detox }
				}
				AND = {
					has_modifier = frozen_terraforming_candidate
					giga_is_frozen = yes
					from = { has_ascension_perk = ap_hydrocentric }
				}

				is_star = yes
				has_anomaly = yes
				is_asteroid = yes
				is_astral_scar = yes
				planet_is_hostile_or_crisis = yes
				is_planet_habitable = yes
				planet_has_no_megastructure = yes
				
				has_planet_flag = megastructure
				has_planet_flag = has_megastructure
				has_planet_flag = crisis_vital_planet
				has_planet_flag = legendary_leader_planet
				has_planet_flag = giga_terraforming_megastructure

				any_fleet_in_orbit = {
					is_constructing = terraform_site_0
				}
			}
		}
	}
	[[CHECK_PLANET]
		$CHECK_PLANET$ = {
			is_surveyed = {
				who = prev.owner
				status = yes
			}

			OR = {
				AND = {
					is_planet_class = pc_molten
					prevprev.owner = {
						OR = {
							has_global_flag = geothermal_capped_u
							check_variable = {
								which = giga_current_geothermal
								value < value:giga_geothermal_limit
							}
						}
					}
				}
				AND = {
					is_planet_class = pc_toxic
					prevprev.owner = {
						OR = {
							OR = {
								has_global_flag = terraform_toxic_capped_u
								check_variable = {
									which = giga_current_terraform_toxic
									value < value:giga_terraform_toxic_limit
								}
							}
						}
					}
				}
				AND = {
					OR = {
						is_planet_class = pc_barren
						is_planet_class = pc_barren_cold
						is_planet_class = pc_frozen
					}
					prevprev.owner = {
						OR = {
							OR = {
								has_global_flag = terraform_barren_capped_u
								check_variable = {
									which = giga_current_terraform_barren
									value < value:giga_terraform_barren_limit
								}
							}
						}
					}
				}
			}

			NOR = {
				AND = {
					has_modifier = terraforming_candidate
					giga_is_barren_cn = yes
				}
				AND = {
					has_modifier = toxic_terraforming_candidate
					giga_is_toxic = yes
					from = { has_ascension_perk = ap_detox }
				}
				AND = {
					has_modifier = frozen_terraforming_candidate
					giga_is_frozen = yes
					from = { has_ascension_perk = ap_hydrocentric }
				}

				is_star = yes
				has_anomaly = yes
				is_asteroid = yes
				is_astral_scar = yes
				planet_is_hostile_or_crisis = yes
				is_planet_habitable = yes
				planet_has_no_megastructure = yes
				
				has_planet_flag = megastructure
				has_planet_flag = has_megastructure
				has_planet_flag = crisis_vital_planet
				has_planet_flag = legendary_leader_planet
				has_planet_flag = giga_terraforming_megastructure

				any_fleet_in_orbit = {
					is_constructing = terraform_site_0
				}
			}
		}
	]
}

auto_construct_sra_orbital_ring_check = {
	$CHECK_SYSTEM$ = {
		exists = starbase
		is_inside_border = prev.owner
		NOT = {
			any_ship_in_system = {
				exists = owner
				OR = {
					is_constructing = SRA_orbital_ring_megastructure_0
					owner = {
						is_hostile = prevprevprev.owner
					}
				}
			}
		}
		any_system_planet = {
			is_colony = yes
			exists = owner
			exists = controller
			controller = {
				is_same_value = prevprevprev.owner
			}
			exists = orbital_defence
			orbital_defence = {
				exists = starbase
				starbase = {
					has_starbase_size >= orbital_ring_tier_1
				}
			}
			NOT = {
				any_fleet_in_orbit = {
					is_constructing = SRA_orbital_ring_megastructure_0
				}
			}
		}
	}
	[[CHECK_PLANET]
		$CHECK_PLANET$ = {
			is_colony = yes
			exists = owner
			exists = controller
			controller = {
				is_same_value = prevprev.owner
			}
			exists = orbital_defence
			orbital_defence = {
				exists = starbase
				starbase = {
					has_starbase_size >= orbital_ring_tier_1
				}
			}
			NOT = {
				any_fleet_in_orbit = {
					is_constructing = SRA_orbital_ring_megastructure_0
				}
			}
		}
	]
}

auto_construct_sra_barrier_guard_station_check = {
	$CHECK_SYSTEM$ = {
		exists = starbase
		is_inside_border = prev.owner
		NOR = {
			any_system_planet = {
				is_surveyed = {
					who = prevprev.owner
					status = no
				}
			}
			any_ship_in_system = {
				exists = owner
				OR = {
					is_constructing = SRA_Barrier_Guard_Station_megastructure_0
					owner = {
						is_hostile = prevprevprev.owner
					}
				}
			}
			any_fleet_in_system = {
				has_fleet_flag = auto_construct_sra_gz_platform_fleet_flag
			}
		}
	}
	[[CHECK_PLANET]
		$CHECK_PLANET$ = {
			is_primary_star = yes
		}
	]
}

auto_construct_sra_gz_platform_check = {
	$CHECK_SYSTEM$ = {
		exists = starbase
		is_inside_border = prev.owner
		OR = {
			NOT = {
				any_system_planet = {
					is_surveyed = {
						who = prevprev.owner
						status = no
					}
				}
			}
			# AND = {
			# 	exists = owner
			# 	owner = {
			# 		is_subject = yes
			# 		overlord = { is_same_value = prevprevprev.owner }
			# 	}
			# }
		}
		NOR = {
			any_ship_in_system = {
				exists = owner
				OR = {
					is_constructing = SRA_GZ_Platform_megastructure_0
					owner = {
						is_hostile = prevprevprev.owner
					}
				}
			}
			any_fleet_in_system = {
				has_fleet_flag = auto_construct_sra_gz_platform_fleet_flag
			}
		}
	}
	[[CHECK_PLANET]
		$CHECK_PLANET$ = {
			is_primary_star = yes
		}
	]
}

auto_construct_asteroid_manufactory_check = {
	$CHECK_SYSTEM$ = {
		exists = starbase
		is_inside_border = prev.owner
		
		NOT = {
			any_ship_in_system = {
				exists = owner
				owner = {
					is_hostile = prevprevprev.owner
				}
			}
		}

		prev.owner = {
			OR = {
				has_global_flag = asteroid_manufactory_capped_u
				check_variable = {
					which = giga_current_asteroid_manufactory
					value < value:giga_asteroid_manufactory_limit
				}
			}
		}

		any_system_planet = {
			giga_asteroid_industry_placement_player = yes
			is_asteroid = yes
			has_deposit = yes
			# is_surveyed = {
			# 	who = prevprev.owner
			# 	status = yes
			# } 
			NOR = {
				has_anomaly = yes
				has_planet_flag = megastructure
				has_planet_flag = has_megastructure
				any_fleet_in_orbit = {
					is_constructing = asteroid_manufactory_0
				}
			}
		}
	}

	[[CHECK_PLANET]
		$CHECK_PLANET$ = {
			giga_asteroid_industry_placement_player = yes
			# is_surveyed = {
			# 	who = prev.owner
			# 	status = yes
			# }
			NOR = {
				has_anomaly = yes
				has_planet_flag = megastructure
				has_planet_flag = has_megastructure
				any_fleet_in_orbit = {
					is_constructing = asteroid_manufactory_0
				}
			}
		}
	]
}