spawn_rg_habitat_effect = {
	spawn_planet = {
		class = "pc_rg_grayhabitat"
		location = event_target:target_planet
		orbit_location = yes
		orbit_angle_offset = 135
		orbit_distance_offset = $DISTANCE$
		size = 15
		has_ring = no

		init_effect = {
			set_name = {
				key = HABITAT_PLANET_NAME
				variable_string = "\\[FROM.from.solar_system.GetName]"
			}
			set_planet_entity = {
				entity = "grey_tempesttw_orbital_habitat_entity"
			}
			set_surveyed = {
				surveyed = yes
				surveyor = FROM
			}
			set_all_comms_surveyed = yes
			clear_blockers = yes

			set_planet_flag = megastructure
			set_planet_flag = habitat
			set_planet_flag = rg_gray_habitat
			set_planet_flag = habitat_level_1
			save_event_target_as = target_habitat
			add_deposit_if_missing = {
				DEPOSIT = d_system_resources
			}
			add_deposit_if_missing = {
				DEPOSIT = d_orbital_research
			}
			add_deposit_if_missing = {
				DEPOSIT = d_orbital_energy
			}
			add_deposit_if_missing = {
				DEPOSIT = d_orbital_mining
			}
			event_target:target_planet = {
				set_planet_flag = habitat@PREV
			}
		}
	}
}

spawn_rg_oribital_ring_effect = {
	From = {
		create_ship_design = {
			design = "RG_ORBITAL_RING_TIER_1_STARBASE"
		}
		add_ship_design = last_created_design
	}
	create_starbase = {
		size = RG_orbital_ring_tier_1
		owner = FROM
		#module = <starbase_module>
		#building = <starbase_building>
		effect = {
		}
	}
	From = {
		remove_ship_design = last_created_design
	}
}

rg_nano_ripper_update_orbital_effect = {
	# Find the orbital and habitat complex and save them as event targets
	if = {
		limit = {
			any_fleet_in_orbit = {
				OR = {
					is_ship_size = major_orbital_resource
					is_ship_size = minor_orbital_resource
				}
			}
		}

		random_fleet_in_orbit = {
			limit = {
				OR = {
					is_ship_size = major_orbital_resource
					is_ship_size = minor_orbital_resource
				}
			}
			save_event_target_as = target_orbital
		}

		# solar_system = {
		# 	random_system_planet = {
		# 		limit = {
		# 			has_planet_flag = habitat
		# 		}
		# 		save_event_target_as = target_habitat
		# 	}
		# }

		last_added_deposit = {
			event_target:target_orbital = {
				set_fleet_flag = mining_orbital
			}
			#switch = {
			#	trigger = is_deposit_type
			#	# Minerals
			#	d_minerals_1 = {
			#		event_target:target_orbital = {
			#			set_fleet_flag = mining_orbital
			#		}
			#	}
			#	Alloys
			#	d_alloys_1 = {
			#		event_target:target_orbital = {
			#			set_fleet_flag = mining_orbital
			#		}
			#		event_target:target_habitat = {
			#			add_deposit = d_hab_alloy_1
			#			last_added_deposit = {
			#				set_deposit_flag = planet@event_target:target_planet
			#			}
			#		}
			#	}
			#}
		}
	}
}

rg_dismantle_nano_ripper_effect = {
	# Find the orbital and habitat complex and save them as event targets
	if = {
		limit = {
			any_fleet_in_orbit = {
				OR = {
					is_ship_size = major_orbital_resource
					is_ship_size = minor_orbital_resource
				}
			}
		}

		random_fleet_in_orbit = {
			limit = {
				OR = {
					is_ship_size = major_orbital_resource
					is_ship_size = minor_orbital_resource
				}
			}
			save_event_target_as = target_orbital
		}

		# solar_system = {
		# 	random_system_planet = {
		# 		limit = {
		# 			has_planet_flag = habitat
		# 		}
		# 		save_event_target_as = target_habitat
		# 	}
		# }
	}

	switch = {
		trigger = has_modifier
		rg_nano_ripper_1_mod = {
			remove_deposit = d_nanite_ripper_deposit_1
			remove_modifier = rg_nano_ripper_1_mod
		}
		rg_nano_ripper_2_mod = {
			remove_deposit = d_nanite_ripper_deposit_1
			remove_deposit = d_nanite_ripper_deposit_2
			remove_modifier = rg_nano_ripper_2_mod
		}
		rg_nano_ripper_3_mod = {
			remove_deposit = d_nanite_ripper_deposit_1
			remove_deposit = d_nanite_ripper_deposit_2
			remove_deposit = d_nanite_ripper_deposit_3
			remove_modifier = rg_nano_ripper_3_mod
			# if = {
			# 	limit = {
			# 		exists = event_target:target_orbital
			# 	}
			# 	event_target:target_habitat = {
			# 		random_deposit = {
			# 			limit = {
			# 				is_deposit_type = d_hab_alloy_1
			# 				has_deposit_flag = planet@event_target:target_planet
			# 			}
			# 			remove_deposit = yes
			# 		}
			# 	}
			# }
		}
		rg_nano_ripper_4_mod = {
			remove_deposit = d_nanite_ripper_deposit_1
			remove_deposit = d_nanite_ripper_deposit_2
			remove_deposit = d_nanite_ripper_deposit_3
			remove_deposit = d_nanite_ripper_deposit_4
			remove_modifier = rg_nano_ripper_4_mod
			# if = {
			# 	limit = {
			# 		exists = event_target:target_orbital
			# 	}
			# 	event_target:target_habitat = {
			# 		while = {
			# 			count = 2
			# 			random_deposit = {
			# 				limit = {
			# 					is_deposit_type = d_hab_alloy_1
			# 					has_deposit_flag = planet@event_target:target_planet
			# 				}
			# 				remove_deposit = yes
			# 			}
			# 		}
			# 	}
			# }
		}
	}

	if = {
		limit = {
			has_orbital_mining_deposit = no
			exists = event_target:target_orbital
		}
		event_target:target_orbital = {
			remove_fleet_flag = mining_orbital
		}
	}
}