@buildtime = 720

RG_orbital_ring = {
	entity = ""
	construction_entity = "orbital_ring_construction_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = yes
	entity_offset = {
		x = 0
		y = 0
	}
	scales_with_planet = yes
	show_galactic_map_icon = no
	show_in_outliner = no
	build_time = 0
	resources = {
		category = megastructures
		cost = {
			alloys = 1000
			influence = 50
		}
	}
	construction_blocks_and_blocked_by = none
	potential = {
		always = no
		has_tradition = tr_rg_nanotech_2
		# OR = {
		# 	has_technology = tech_habitat_1
		# 	has_civic = civic_diadochi
		# 	has_civic = civic_great_khans_legacy
		# 	has_country_flag = democratic_khanate_flag
		# }
	}
	possible = {
		exists = starbase
		custom_tooltip = {
			fail_text = "requires_inside_border"
			is_inside_border = from
		}
	}
	placement_rules = {
		planet_possible = {
			custom_tooltip = {
				fail_text = "requires_surveyed_planet"
				is_surveyed = {
					# prevent leaking habitability information
					who = prev.from
					status = yes
				}
			}
			custom_tooltip = {
				fail_text = "requires_no_anomaly"
				NOT = {
					has_anomaly = yes
				}
			}
			custom_tooltip = {
				fail_text = "requires_colonized_planet_orbital_ring"
				# is_colony = yes
				exists = owner
				exists = controller
				controller = {
					is_same_value = prev.owner
				}
			}
			custom_tooltip = {
				fail_text = "requires_not_hostile_or_primitive"
				NOR = {
					planet_is_hostile_or_crisis = yes
					AND = {
						exists = owner
						owner = {
							is_country_type = primitive
						}
					}
				}
			}
			custom_tooltip = {
				fail_text = "requires_no_existing_megastructure"
				#can_build_megastructure_on_planet = yes
				NOR = {
					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
				}
			}
			custom_tooltip = {
				fail_text = "requires_no_orbital_debris"
				NOR = {
					has_planet_flag = has_orbital_debris
				}
			}
		}
	}
	# root = system
	# from = country
	ai_weight = {
		factor = 0
	}
	on_build_start = {
	}
	on_build_cancel = {
	}
	on_build_complete = {
		fromfrom.planet = {
			set_planet_flag = has_megastructure
			save_event_target_as = orbital_ring_planet
			if = {
				limit = {
					has_orbital_station = yes
				}
				orbital_station = {
					dismantle = yes
				}
			}
			spawn_rg_oribital_ring_effect = yes
		}
		remove_megastructure = fromfrom
		from = {
			country_event = {
				id = tutorial.2121
			}
		}
	}
}

RG_orbital_ring_ruined = {
	entity = "orbital_ring_01_destroyed_entity"
	construction_entity = "orbital_ring_construction_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = yes
	entity_offset = {
		x = 0
		y = 0
	}
	scales_with_planet = yes
	potential = {
		always = no
	}
	is_ruined_orbital_ring = yes
}

RG_orbital_ring_restored = {
	entity = ""
	construction_entity = "orbital_ring_construction_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = yes
	entity_offset = {
		x = 0
		y = 0
	}
	scales_with_planet = yes
	show_galactic_map_icon = no
	show_in_outliner = no
	build_time = 720
	resources = {
		category = megastructures
		cost = {
			alloys = 500
			influence = 50
		}
	}
	upgrade_from = {
		RG_orbital_ring_ruined
	}
	possible = {
		from = {
			has_tradition = tr_rg_nanotech_2
		}
		custom_tooltip = {
			fail_text = "requires_outpost"
			exists = starbase
		}
		custom_tooltip = {
			fail_text = "requires_inside_border"
			is_inside_border = from
		}
		custom_tooltip = {
			fail_text = "requires_no_anomaly"
			any_system_planet = {
				has_planet_flag = ruined_orbital_ring_planet
				NOT = {
					has_anomaly = yes
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_colonized_planet_orbital_ring"
			any_system_colony = {
				has_planet_flag = ruined_orbital_ring_planet
				is_colony = yes
				exists = owner
				exists = controller
				owner = {
					is_same_value = from
				}
				controller = {
					is_same_value = from
				}
			}
		}
	}
	on_build_start = {
	}
	on_build_cancel = {
	}
	on_build_complete = {
		random_system_planet = {
			limit = {
				has_planet_flag = ruined_orbital_ring_planet
			}
			set_planet_flag = has_megastructure
			save_event_target_as = orbital_ring_planet
			if = {
				limit = {
					has_orbital_station = yes
				}
				orbital_station = {
					dismantle = yes
				}
			}
			spawn_rg_oribital_ring_effect = yes
		}
		every_system_ambient_object = {
			limit = {
				OR = {
					is_ambient_object_type = medium_debris_01_object
					is_ambient_object_type = medium_debris_02_object
				}
				distance = {
					source = fromfrom
					max_distance <= 100
					same_solar_system = yes
				}
			}
			destroy_ambient_object = this
		}
		remove_megastructure = fromfrom
		from = {
			country_event = {
				id = tutorial.2121
			}
		}
	}
}
