# Game will first pick an initializer using usage_odds, but only use it if it passes the spawn_chance check
# Even if it fails the spawn_chance check it will still count towards the initializer's max_instances

###############
# ENCLAVES
###############

# NB: Mercenary Enclave is spawned by event, rather than a solar system initializer

# SALVAGER ENCLAVE
#Primary system
salvager_enclave_init_01 = {
	class = "rl_standard_stars"
	usage = misc_system_init
	usage_odds = {
		base = 0
		modifier = {
			has_overlord_dlc = yes
			add = @spawn_system_extreme
		}
		modifier = {
			factor = 0
			is_fe_cluster = yes
		}
		modifier = {
			factor = 0
			has_star_flag = empire_cluster
		}
	}
	max_instances = 1
	spawn_chance = 100 #Guaranteed, assuming usage_odds passes

	flags = {
		salvager_enclave_system
		enclave
	}

	asteroid_belt = {
		type = rocky_asteroid_belt
		radius = 85
	}

	asteroid_belt = {
		type = rocky_asteroid_belt
		radius = 195
	}

	planet = {
		count = 1
		class = star
		orbit_distance = 0
		orbit_angle = 1
		size = { min = 20 max = 30 }
		has_ring = no
		init_effect = {
			prevent_anomaly = yes
		}
	}

	change_orbit = 50

	planet = {
		count = 1
		class = pc_barren
		size = 16
		orbit_angle = 270
		init_effect = {
			prevent_anomaly = yes
		}
	}

	change_orbit = 40

	planet = {
		count = 1
		class = rl_unhabitable_planets
		orbit_angle = 80
		size = 20

		init_effect = {
			prevent_anomaly = yes
			set_planet_flag = salvager_enclave_planet
			clear_deposits = yes
			add_deposit = d_minerals_4
			if = {
				limit = {
					NOT = { exists = event_target:salvager_enclave_country }
				}
				save_global_event_target_as = salvager_enclave_planet
				create_species = {
					homeworld = this
					name = random
					class = SALVAGER
					namelist = MAM4
					portrait = random
					traits = {
						ideal_planet_class = pc_habitat
						trait = random_traits
					}
				}
				last_created_species = {
					save_event_target_as = salvager_enclave_species
				}
				create_salvager_enclave_country = yes
				solar_system = {
					save_global_event_target_as = salvager_enclave_system
				}
			}
		}
	}

	change_orbit = 35

	planet = {
		count = 2
		class = pc_asteroid
		orbit_angle = { min = 90 max = 270 }
		init_effect = {
			clear_deposits = yes
			add_deposit = d_alloys_1
			create_ambient_object = {
				type = small_debris_object
			}
		}
	}

	init_effect = {
		if = {
			limit = { is_inside_nebula = no }
			create_nebula = {
				radius = 25
				effect = {
					set_star_flag = salvager_nebula
					nebula_cloaking_effect = yes
				}
			}
		}
	}
}

#Prolog system
salvager_enclave_init_02 = {
	class = "rl_standard_stars"
	usage = misc_system_init
	usage_odds = {
		base = 0
		modifier = {
			has_overlord_dlc = yes
			add = @spawn_system_extreme
		}
		#Should ideally be near the primary system
		modifier = {
			has_overlord_dlc = yes
			any_neighbor_system = { has_star_flag = salvager_enclave_system }
			add = 50
		}
		modifier = {
			factor = 0
			is_fe_cluster = yes
		}
		modifier = {
			factor = 0
			has_star_flag = empire_cluster
		}
	}
	max_instances = 1
	scaled_spawn_chance = 16 # scales by galaxy size (1000 stars = 10x base)

	flags = {
		salvager_enclave_system
		enclave
	}

	asteroid_belt = {
		type = rocky_asteroid_belt
		radius = 125
	}

	planet = {
		count = 1
		class = star
		orbit_distance = 0
		orbit_angle = 1
		size = { min = 25 max = 35 }
		has_ring = no
		init_effect = {
			prevent_anomaly = yes
		}
	}

	change_orbit = 45

	planet = {
		count = 1
		class = rl_unhabitable_planets
		size = 14
		orbit_angle = { min = 110 max = 160}
	}

	change_orbit = 20

	planet = {
		count = 3
		class = pc_asteroid
		orbit_angle = { min = 90 max = 270 }
	}

	change_orbit = 15

	planet = {
		count = 1
		class = pc_barren
		orbit_angle = 80
		size = 20

		init_effect = {
			prevent_anomaly = yes
			set_planet_flag = salvager_enclave_planet
			clear_deposits = yes
			add_deposit = d_minerals_4
			if = {
				limit = {
					NOT = { exists = event_target:salvager_enclave_country }
				}
				save_global_event_target_as = salvager_enclave_planet
				create_species = {
					homeworld = this
					name = random
					class = SALVAGER
					namelist = MAM1
					portrait = random
					traits = {
						ideal_planet_class = pc_habitat
						trait = random_traits
					}
				}
				last_created_species = {
					save_event_target_as = salvager_enclave_species
				}
				create_salvager_enclave_country = yes
				solar_system = {
					save_global_event_target_as = salvager_enclave_system
				}
			}
		}
	}

	change_orbit = 90

	planet = {
		count = 1
		class = pc_gas_giant
		size = 25
		orbit_angle = { min = 110 max = 160 }
		has_ring = yes

		moon = {
			count = 2
			class = rl_unhabitable_planets
			orbit_distance = { min = 20 max = 28 }
			orbit_angle = 44
			size = { min = 6 max = 9 }
		}

		moon = {
			class = pc_nuked
			orbit_distance = 28
			orbit_angle = 30
			size = 14
			init_effect = {
				create_ambient_object = {
					type = enclave_salvager_starbase_ruined
					entity_offset = { min = 30 max = 50 }
					entity_offset_height = { min = -10 max = -30 }
					location = this
				}
			}
		}
	}

	init_effect = { }
}

# Salvager Enclave 3
salvager_enclave_init_03 = {
	class = "sc_black_hole"
	usage = misc_system_init
	usage_odds = {
		base = 0
		modifier = {
			has_overlord_dlc = yes
			add = @spawn_system_extreme
		}
		#Should ideally be near the primary system
		modifier = {
			has_overlord_dlc = yes
			any_neighbor_system = { has_star_flag = salvager_enclave_system }
			add = 50
		}
		modifier = {
			factor = 0
			is_fe_cluster = yes
		}
		modifier = {
			factor = 0
			has_star_flag = empire_cluster
		}
	}
	max_instances = 1
	scaled_spawn_chance = 12 # scales by galaxy size (1000 stars = 10x base)

	flags = {
		salvager_enclave_system
		enclave
	}

	planet = {
		class = star
		orbit_distance = 0
		orbit_angle = 1
		size = { min = 25 max = 35 }
		has_ring = no
		init_effect = {
			prevent_anomaly = yes

			create_ambient_object = {
				type = ruined_quantum_catapult_object
				location = prev
			    use_3d_location = yes
			    entity_offset_height = -12
			}

			create_ambient_object = {
				type = "large_debris_object"
				location = this
			}
			last_created_ambient_object = {
				set_location = {
					target = prev
					distance = 35
					angle = 20
				}
			}
			create_ambient_object = {
				type = "large_debris_object"
				location = this
			}
			last_created_ambient_object = {
				set_location = {
					target = prev
					distance = 35
					angle = 95
				}
			}
			create_ambient_object = {
				type = "large_debris_object"
				location = this
			}
			last_created_ambient_object = {
				set_location = {
					target = prev
					distance = 35
					angle = 205
				}
			}
		}
	}

	asteroid_belt = {
		type = rocky_asteroid_belt
		radius = 55
	}

	asteroid_belt = {
		type = rocky_asteroid_belt
		radius = 155
	}

	change_orbit = 65

	planet = {
		class = pc_broken
		size = 14

		init_effect = {
			prevent_anomaly = yes

			set_planet_flag = salvager_enclave_planet
			clear_deposits = yes
			if = {
				limit = {
					NOT = { exists = event_target:salvager_enclave_country }
				}
				save_global_event_target_as = salvager_enclave_planet
				create_species = {
					homeworld = this
					name = random
					class = SALVAGER
					namelist = MAM1
					portrait = random
					traits = {
						ideal_planet_class = pc_habitat
						trait = random_traits
					}
				}
				last_created_species = {
					save_event_target_as = salvager_enclave_species
				}
				create_salvager_enclave_country = yes
				solar_system = {
					save_global_event_target_as = salvager_enclave_system
				}
			}
		}
	}

	change_orbit = 35

	planet = {
		count = 3
		class = pc_asteroid
		orbit_angle = { min = 90 max = 270 }

		init_effect = {
			prevent_anomaly = yes
		}
	}

	change_orbit = 35

	planet = {
		class = pc_broken
		size = 14

		init_effect = {
			prevent_anomaly = yes

			create_ambient_object = {
				type = "large_debris_object"
				location = this
			}
			last_created_ambient_object = {
				set_location = {
					target = prev
					distance = 35
					angle = 20
				}
			}
			create_ambient_object = {
				type = "medium_debris_01_object"
				location = this
			}
			last_created_ambient_object = {
				set_location = {
					target = prev
					distance = 35
					angle = 120
				}
			}
		}
	}

	change_orbit = 15

	planet = {
		class = pc_broken
		size = 14

		init_effect = {
			prevent_anomaly = yes

			create_ambient_object = {
				type = enclave_salvager_starbase_ruined
				entity_offset = { min = 30 max = 50 }
				entity_offset_height = { min = -10 max = -30 }
				location = this
			}
		}
	}
}

# SHROUDWALKER ENCLAVE 1
shroudwalker_enclave_init_01 = {
	class = "rl_standard_stars"
	usage = misc_system_init
	usage_odds = {
		base = 0
		modifier = {
			potential_shroudwalker_enclave = yes
			add = @spawn_system_enclave
		}
		modifier = {
			factor = 0
			is_fe_cluster = yes
		}
		modifier = {
			factor = 0
			has_star_flag = empire_cluster
		}
	}
	max_instances = 1
	spawn_chance = 100 #Guaranteed, assuming usage_odds passes

	flags = {
		shroudwalker_enclave_system
		enclave
	}

	planet = {
		count = 1
		class = star
		orbit_distance = 0
		orbit_angle = 1
		size = { min = 20 max = 30 }
		has_ring = no

		init_effect = {
			prevent_anomaly = yes
			clear_deposits = yes
			create_ambient_object = {
				type = "nebula_L10_entity"
				location = this
			}
			last_created_ambient_object = {
				set_location = {
					target = prev
					distance = 0
					angle = random
				}
			}
		}
	}

	change_orbit = 45

	planet = {
		count = 1
		orbit_distance = 100
		class = pc_shrouded
		orbit_angle = { min = 90 max = 270 }
		size = 25
		has_ring = yes

		init_effect = {
			set_planet_flag = shroudwalker_enclave_planet
			save_event_target_as = shroudwalker_enclave_planet
			clear_deposits = yes
			prevent_anomaly = yes
			if = {
				limit = {
					NOT = {
						exists = event_target:shroudwalker_enclave_country
					}
				}
				create_species = {
					homeworld = this
					name = random
					class = SHROUDWALKER
					namelist = NECROID1
					portrait = random
					traits = {
						ideal_planet_class = pc_habitat
						trait = trait_psionic
						trait = random_traits
					}
				}
				last_created_species = {
					save_event_target_as = shroudwalker_enclave_species
				}
				create_shroudwalker_enclave_country = yes
				solar_system = {
					save_global_event_target_as = shroudwalker_enclave_system
				}
			}
			else = {
				event_target:shroudwalker_enclave_country = {
					create_shroudwalker_enclave_starbase = yes
				}
			}
			add_deposit = d_zro_deposit_5
		}
	}

	init_effect = {
		if = {
			limit = {
				exists = event_target:shroudwalker_enclave_country
			}
			spawn_natural_wormhole = {
				bypass_type = shroud_tunnel
				random_pos = no
				orbit_angle = 360
				orbit_distance = 15
			}
			set_star_flag = shroud_tunnel_nexus
			save_global_event_target_as = shroud_tunnel_nexus
		}
	}
}

#SHROUDWALKER ENCLAVE 2
shroudwalker_enclave_init_02 = {
	class = "rl_standard_stars"
	usage = misc_system_init
	usage_odds = {
		base = 0
		modifier = {
			potential_shroudwalker_enclave = yes
			add = @spawn_system_extreme
		}
		modifier = {
			factor = 0
			is_fe_cluster = yes
		}
		modifier = {
			factor = 0
			has_star_flag = empire_cluster
		}
	}
	max_instances = 1
	scaled_spawn_chance = 16	# scales by galaxy size (1000 stars = 10x base)

	flags = {
		shroudwalker_enclave_system
		enclave
	}

	asteroid_belt = {
		type = crystal_asteroid_belt
		radius = 125
	}

	planet = {
		count = 1
		class = star
		orbit_distance = 0
		orbit_angle = 1
		size = { min = 10 max = 20 }
		has_ring = no
	}

	change_orbit = 30

	planet = {
		count = 1
		orbit_distance = 50
		class = pc_shrouded
		orbit_angle = { min = 90 max = 270 }
		size = 30
		has_ring = no

		init_effect = {
			set_planet_flag = shroudwalker_enclave_planet
			save_event_target_as = shroudwalker_enclave_planet
			clear_deposits = yes
			prevent_anomaly = yes
			create_ambient_object = {
				type = "nebula_L8_entity"
				location = this
			}
			last_created_ambient_object = {
				set_location = {
					target = prev
					distance = 0
					angle = random
				}
			}
			if = {
				limit = {
					NOT = {
						exists = event_target:shroudwalker_enclave_country
					}
				}
				create_species = {
					homeworld = this
					name = random
					class = SHROUDWALKER
					namelist = LITHOID1
					portrait = random
					traits = {
						ideal_planet_class = pc_habitat
						trait = trait_psionic
						trait = random_traits
					}
				}
				last_created_species = {
					save_event_target_as = shroudwalker_enclave_species
				}
				create_shroudwalker_enclave_country = yes
				solar_system = {
					save_global_event_target_as = shroudwalker_enclave_system
					set_star_flag = shroudwalker_enclave_system
				}
			}
			else = {
				event_target:shroudwalker_enclave_country = {
					create_shroudwalker_enclave_starbase = yes
				}
			}
			add_deposit = d_zro_deposit_5
		}
	}

	init_effect = {
		if = {
			limit = {
				exists = event_target:shroudwalker_enclave_country
				NOT = {
					any_system = {
						OR = {
							has_star_flag = shroudwalker_enclave_system
							has_star_flag = shroud_tunnel_nexus
						}
					}
				}
			}
			spawn_natural_wormhole = {
				bypass_type = shroud_tunnel
				random_pos = no
				orbit_angle = 360
				orbit_distance = 15
			}
			set_star_flag = shroud_tunnel_nexus
			save_global_event_target_as = shroud_tunnel_nexus
		}
	}
}

#SHROUDWALKER ENCLAVE 3
shroudwalker_enclave_init_03 = {
	class = "sc_a"
	usage = misc_system_init
	usage_odds = {
		base = 0
		modifier = {
			potential_shroudwalker_enclave = yes
			add = @spawn_system_extreme
		}
		modifier = {
			factor = 0
			is_fe_cluster = yes
		}
		modifier = {
			factor = 0
			has_star_flag = empire_cluster
		}
	}
	max_instances = 1
	scaled_spawn_chance = 12	# scales by galaxy size (1000 stars = 10x base)

	flags = {
		shroudwalker_enclave_system
		enclave
	}

	asteroid_belt = {
		type = crystal_asteroid_belt
		radius = 30
	}

	asteroid_belt = {
		type = crystal_asteroid_belt
		radius = 80
	}

	asteroid_belt = {
		type = crystal_asteroid_belt
		radius = 155
	}

	planet = {
		count = 1
		class = star
		orbit_distance = 0
		orbit_angle = 1
		size = { min = 10 max = 20 }
		has_ring = no
	}

	change_orbit = 70

	planet = {
		count = 5
		class = pc_rare_crystal_asteroid
		size = 3
		orbit_distance = { min = 1 max = 4 }
		orbit_angle = { min = 10 max = 170 }
	}

	change_orbit = 20

	planet = {
		class = pc_shrouded
		orbit_angle = { min = 90 max = 270 }
		size = 30
		has_ring = no

		init_effect = {
			set_planet_flag = shroudwalker_enclave_planet
			save_event_target_as = shroudwalker_enclave_planet
			clear_deposits = yes
			prevent_anomaly = yes
			create_ambient_object = {
				type = "nebula_L8_entity"
				location = this
			}
			last_created_ambient_object = {
				set_location = {
					target = prev
					distance = 0
					angle = random
				}
			}
			if = {
				limit = {
					NOT = {
						exists = event_target:shroudwalker_enclave_country
					}
				}
				create_species = {
					homeworld = this
					name = random
					class = SHROUDWALKER
					namelist = LITHOID1
					portrait = random
					traits = {
						ideal_planet_class = pc_habitat
						trait = trait_psionic
						trait = random_traits
					}
				}
				last_created_species = {
					save_event_target_as = shroudwalker_enclave_species
				}
				create_shroudwalker_enclave_country = yes
				solar_system = {
					save_global_event_target_as = shroudwalker_enclave_system
					set_star_flag = shroudwalker_enclave_system
				}
			}
			else = {
				event_target:shroudwalker_enclave_country = {
					create_shroudwalker_enclave_starbase = yes
				}
			}
			add_deposit = d_zro_deposit_5
		}
	}

	init_effect = {
		if = {
			limit = {
				exists = event_target:shroudwalker_enclave_country
				NOT = {
					any_system = {
						OR = {
							has_star_flag = shroudwalker_enclave_system
							has_star_flag = shroud_tunnel_nexus
						}
					}
				}
			}
			spawn_natural_wormhole = {
				bypass_type = shroud_tunnel
				random_pos = no
				orbit_angle = 360
				orbit_distance = 15
			}
			set_star_flag = shroud_tunnel_nexus
			save_global_event_target_as = shroud_tunnel_nexus
		}

		event_target:shroudwalker_enclave_country = {
			create_fleet = {
				name = "NAME_Psionic_Avatar"
				settings = {
					can_upgrade = no
					can_change_composition = no
					can_change_leader = no
					uses_naval_capacity = no
					spawn_debris = no
					is_boss = yes
				}
				effect = {
					set_owner = PREV
					set_fleet_flag = avatar_fleet
					create_ship = {
						name = "NAME_Avatar"
						design = "NAME_Shroud_Avatar"
						prefix = no
						upgradable = no
					}
					set_location = last_created_ambient_object
				}
			}
			last_created_fleet = {
				clear_fleet_actions = this
				queue_actions = {
					repeat = { #repeating it creates a visual effect of swooping around the planet
						find_random_planet = {
							found_planet = {
								move_to = this
								orbit_planet = THIS
							}
						}
						wait = {
							duration = 15
						}
					}
				}
			}
		}
	}
}

#Broken Shroudwalker System
broken_shroudwalker_init_01 = {
	class = "sc_trinary_2"

	usage = misc_system_init
	usage_odds = {
		base = 0
		modifier = {
			potential_shroudwalker_enclave = yes
			add = @spawn_system_extreme
		}
		modifier = {
			factor = 0
			is_fe_cluster = yes
		}
		modifier = {
			factor = 0
			has_star_flag = empire_cluster
		}
	}
	max_instances = 1
	spawn_chance = 33

	flags = {
		broken_shroudwalker_enclave
	}

	planet = {
		name = "NAME_Mesembria"
		class = star
		orbit_distance = 0
		orbit_angle = 0
		size = 25
		has_ring = no

		init_effect = {
			clear_deposits = yes
			prevent_anomaly = yes

			create_ambient_object = {
				type = "nebula_L8_entity"
				location = this
			}
			last_created_ambient_object = {
				set_location = {
					target = prev
					distance = 0
					angle = random
				}
			}
		}

		change_orbit = 20

		planet = {
			count = 6
			class = pc_rare_crystal_asteroid
			orbit_distance = 5

			init_effect = {
				clear_deposits = yes
				prevent_anomaly = yes
			}
		}
	}

	asteroid_belt = {
		type = crystal_asteroid_belt
		radius = 20
	}

	asteroid_belt = {
		type = crystal_asteroid_belt
		radius = 30
	}

	asteroid_belt = {
		type = crystal_asteroid_belt
		radius = 40
	}

	asteroid_belt = {
		type = crystal_asteroid_belt
		radius = 50
	}

	asteroid_belt = {
		type = crystal_asteroid_belt
		radius = 65
	}

	planet = {
		name = "NAME_Auge"
		count = 1
		class = star
		orbit_distance = 120
		orbit_angle = 25
		size = 15
		has_ring = no

		planet = {
			name = "NAME_Anatole"
			orbit_angle = 205
			class = pc_shrouded
			orbit_distance = 25
			size = 8

			init_effect = {
				clear_deposits = yes
				prevent_anomaly = yes
				add_deposit = d_zro_deposit_2
			}
		}

		planet = {
			name = "NAME_Mousika"
			orbit_angle = 55
			class = pc_shrouded
			orbit_distance = 15
			size = 15

			init_effect = {
				clear_deposits = yes
				prevent_anomaly = yes
				add_deposit = d_zro_deposit_2

				create_ambient_object = {
					type = enclave_shroudwalker_starbase_ruined
					entity_offset_height = -15
					location = this
				}
			}
		}

		init_effect = {
			clear_deposits = yes
			prevent_anomaly = yes
		}
	}

	planet = {
		name = "NAME_Dysis"
		count = 1
		class = star
		orbit_distance = 0
		orbit_angle = 145
		size = 20
		has_ring = no

		planet = {
			name = "NAME_Hesperis"
			class = pc_shrouded
			orbit_angle = 135
			orbit_distance = 25
			size = 5

			init_effect = {
				clear_deposits = yes
				prevent_anomaly = yes
				add_deposit = d_zro_deposit_2
			}
		}

		planet = {
			name = "NAME_Arktos"
			class = pc_shrouded
			orbit_angle = 255
			orbit_distance = 15
			size = 12

			init_effect = {
				clear_deposits = yes
				prevent_anomaly = yes
				add_deposit = d_zro_deposit_2

				create_ambient_object = {
					type = enclave_shroudwalker_starbase_ruined
					entity_offset_height = -15
					location = this
				}
			}
		}

		init_effect = {
			clear_deposits = yes
			prevent_anomaly = yes
		}
	}
}

#################
# MEGASTRUCTURES
#################

# Ruined Quantum Catapult
quantum_catapult_init_01 = {
	class = sc_pulsar
	usage = misc_system_init

	flags = { ruined_catapult_system galactic_landmark_system }

	usage_odds = {
		base = @spawn_system_extreme
		modifier = {
			factor = 0
			has_overlord_dlc = no
		}
	}

	max_instances = 1
	scaled_spawn_chance = 4

	planet = {
		class = star
		orbit_distance = 0
		init_effect = {
			save_event_target_as = quantum_catapult_star
			clear_deposits = yes
		}
	}

	change_orbit = 45

	planet = {
		count = { min = 1 max = 6 }
		orbit_distance = 20
		orbit_angle = { min = 90 max = 270 }

		change_orbit = @base_moon_distance

		moon = {
			count = { min = 0 max = 1 }
			orbit_angle = { min = 90 max = 270 }
			orbit_distance = 5
		}
	}

	init_effect = {
		spawn_megastructure = {
			type = "quantum_catapult_ruined"
			planet = event_target:quantum_catapult_star

			init_effect = {
				create_ambient_object = {
					type = "large_debris_object"
					location = this
				}
				last_created_ambient_object = {
					set_location = {
						target = prev
						distance = 20
						angle = 20
					}
				}
				create_ambient_object = {
					type = "large_debris_object"
					location = this
				}
				last_created_ambient_object = {
					set_location = {
						target = prev
						distance = 20
						angle = 150
					}
				}
				create_ambient_object = {
					type = "medium_debris_01_object"
					location = this
				}
				last_created_ambient_object = {
					set_location = {
						target = prev
						distance = 35
						angle = 90
					}
				}
				create_ambient_object = {
					type = "medium_debris_02_object"
					location = this
				}
				last_created_ambient_object = {
					set_location = {
						target = prev
						distance = 3
						angle = 270
					}
				}
			}
		}
	}
}

# Ruined Orbital Ring
orbital_ring_init_01 = {
	class = "rl_binary_stars"
	asteroid_belt = {
		type = icy_asteroid_belt
		radius = 145
	}
	flags = { ruined_orbital_ring_system galactic_landmark_system }

	usage = misc_system_init
	usage_odds = {
		base = @spawn_system_extreme
		modifier = {
			factor = 0
			has_overlord_dlc = no
		}
		modifier = {
			factor = 0
			has_star_flag = empire_cluster
		}
		modifier = {
			factor = 0.2
			closest_system = {
				has_megastructure = ring_world_ruined
				distance = {
					source = prev
					max_distance <= 200
				}
			}
		}
	}
	max_instances = 1
	scaled_spawn_chance = 8	#by galaxy size

	planet = {
		count = 1
		class = star
		orbit_distance = 30
		orbit_angle = 0
		size = { min = 18 max = 30 }
		has_ring = no
	}

	planet = {
		count = 1
		class = star
		orbit_distance = 0
		size = { min = 15 max = 25 }
		has_ring = no
	}

	planet = {
		count = 1
		class = random_non_colonizable
		orbit_distance = 45
		orbit_angle = { min = 90 max = 270 }
		size = { min = 8 max = 12 }
		has_ring = no
	}

	planet = {
		count = { min = 2 max = 3 }
		class = pc_asteroid
		orbit_distance = 15
		orbit_angle = { min = 90 max = 270 }
	}

	planet = {
		count = 1
		class = random_colonizable
		orbit_distance = 20
		orbit_angle = { min = 90 max = 270 }
		size = { min = 8 max = 14 }
		has_ring = no
		flags = { ruined_orbital_ring_planet }
		init_effect = {
			prevent_anomaly = yes
			save_event_target_as = ruined_orbital_ring_planet
		}
	}

	planet = {
		count = { min = 1 max = 2 }
		class = pc_ice_asteroid
		orbit_distance = 20
		orbit_angle = { min = 90 max = 270 }
	}

	init_effect = {
		spawn_megastructure = {
			type = "orbital_ring_ruined"
			planet = event_target:ruined_orbital_ring_planet

			init_effect = {
				create_ambient_object = {
					type = "medium_debris_01_object"
					location = this
				}
				last_created_ambient_object = {
					set_location = {
						target = prev
						distance = 8
						angle = 90
					}
				}
				create_ambient_object = {
					type = "medium_debris_02_object"
					location = this
				}
				last_created_ambient_object = {
					set_location = {
						target = prev
						distance = 3
						angle = 270
					}
				}
			}
		}
	}
}

##########
# ORIGINS
##########

# Imperial Fiefdom Origin
overlord_system_init = { # Aix - Empire Capital
	class = "sc_pulsar"

	usage = misc_system_init
	usage_odds = {
		base = 0 # Spawned via imperial_origin_start_effect in 01_start_of_game_effects.txt
	}
	max_instances = 1

	flags = { overlord_empire_flag overlord_system_1_flag }

	planet = {
		class = star
		orbit_distance = 0
		orbit_angle = 1
		size = 30
		has_ring = no

		init_effect = {
			clear_deposits = yes
		}
	}

	planet = {
		count = { min = 1 max = 2 }
		class = pc_asteroid
		orbit_distance = 30
		orbit_angle = { min = 40 max = 100 }
	}

	planet = {
		count = { min = 1 max = 2 }
		orbit_distance = 30
		class = random_non_colonizable
		orbit_angle = { min = 90 max = 270 }

		change_orbit = 10

		moon = {
			count = { min = 0 max = 1 }
			class = random_non_colonizable
			orbit_angle = { min = 90 max = 270 }
			orbit_distance = 5
		}
	}

	planet = {
		count = { min = 1 max = 2 }
		orbit_distance = 30
		class = pc_gas_giant
		orbit_angle = { min = 90 max = 270 }

		change_orbit = 10

		moon = {
			count = { min = 0 max = 1 }
			class = random_non_colonizable
			orbit_angle = { min = 90 max = 270 }
			orbit_distance = 5
		}
	}

	planet = { # Colonizeable Planet
		name = "NAME_Aix"
		class = "pc_gaia"
		orbit_distance = 20
		orbit_angle = 150
		size = 30
		has_ring = no
		deposit_blockers = none
		modifiers = none

		init_effect = {
			save_global_event_target_as = origin_imperial_homeland
			create_species = {
				name = random
				class = random_non_machine
				portrait = random
				traits = random
				homeworld = this
			}
			last_created_species = {
				save_global_event_target_as = origin_imperial_species
			}
			create_country = {
				name = {
					key = "NAME_Imperial_Origin_Empire"
					variable_string = "[origin_imperial_species.GetAdj]"
				}
				authority = auth_imperial
				species = last_created_species
				civics = {
					civic = civic_byzantine_bureaucracy
					civic = civic_feudal_realm
				}
				ethos = {
					ethic = ethic_xenophile
					ethic = ethic_militarist
					ethic = ethic_materialist
				}
				origin = "origin_imperial_vassal_overlord"

				effect = {
					save_global_event_target_as = origin_imperial_homeland_country

					every_playable_country = {
						limit = {
							has_origin = origin_imperial_vassal
						}
						establish_communications_no_message = event_target:origin_imperial_homeland_country
					}

					# Fleets
					create_fleet = {
						effect = {
							set_owner = event_target:origin_imperial_homeland_country
							create_ship = {
								name = random
								random_existing_design = constructor
							}
							set_location = event_target:origin_imperial_homeland_country.capital_star
						}
					}

					create_fleet = {
						effect = {
							set_owner = event_target:origin_imperial_homeland_country
							create_ship = {
								name = random
								random_existing_design = science
							}
							set_location = event_target:origin_imperial_homeland_country.capital_star
							save_event_target_as = new_science_ship_target
						}
					}
					create_leader = {
						name = random
						species = event_target:origin_imperial_homeland_country.species
						class = scientist
						skill = 1
					}
					event_target:new_science_ship_target = {
						assign_leader = last_created_leader
					}

					while = {
						count = 4
						if = {
							limit = {
								event_target:origin_imperial_homeland_country = {
									country_uses_bio_ships = yes
								}
							}
							create_fleet = {
								effect = {
									set_owner = event_target:origin_imperial_homeland_country
									while = {
										count = 20
										create_ship = {
											name = random
											design = NAME_CHARGER_1
											growth_stage = 2
										}
									}
									set_location = event_target:origin_imperial_homeland_co
								}
							}
						}
						else = {
							create_fleet = {
								effect = {
									set_owner = event_target:origin_imperial_homeland_country
									while = {
										count = 10
										create_ship = {
											name = random
											design = NAME_Ravager
										}
									}
									set_location = event_target:origin_imperial_homeland_country.capital_star
								}
							}
						}
					}

					# Resources
					add_basic_resources_effect = { VALUE = 3000 }
					add_manufactured_resources_effect = { VALUE = 1500 }
					add_advanced_resources_effect = { VALUE = 1000 }

					# Tech
					give_physics_tech_tier_1_effect = yes
					give_physics_tech_tier_2_effect = yes
					give_engineering_tech_tier_1_effect = yes
					give_engineering_tech_tier_2_effect = yes
					give_society_tech_tier_1_effect = yes
					give_society_tech_tier_2_effect = yes
					give_technology = { tech = tech_hyper_drive_3 }
					give_technology = { tech = tech_gateway_activation }

					# Robots
					create_built_robot_species = yes
					last_created_species = { save_global_event_target_as = origin_imperial_species_robot }

					# Policies
					set_policy = {
						policy = diplomatic_stance
						option = diplo_stance_cooperative
						cooldown = yes
					}
					set_policy = {
						policy = war_philosophy
						option = no_wars
						cooldown = yes
					}
					set_policy = {
						policy = orbital_bombardment
						option = orbital_bombardment_selective
						cooldown = yes
					}
					set_policy = {
						policy = resettlement
						option = resettlement_allowed
						cooldown = yes
					}
					set_policy = {
						policy = first_contact_protocol
						option = first_contact_proactive
						cooldown = yes
					}
					set_policy = {
						policy = border_policy
						option = border_policy_open
						cooldown = yes
					}
					set_policy = {
						policy = economic_policy
						option = economic_policy_balanced
						cooldown = yes
					}
					set_policy = {
						policy = population_controls
						option = population_controls_not_allowed
						cooldown = yes
					}
				}
			}
			set_owner = event_target:origin_imperial_homeland_country
			set_controller = event_target:origin_imperial_homeland_country
			set_capital = yes

			while = {
				count = 45
				create_pop_group = {
					species = owner_main_species
				}
			}
			while = {
				count = 25
				create_pop_group = {
					species = event_target:origin_imperial_species_robot
				}
			}

			while = {
				count = 6
				add_district_and_planet_size_if_needed_effect = {
					district = district_city
				}
				add_district_and_planet_size_if_needed_effect = {
					district = district_mining
				}
				add_district_and_planet_size_if_needed_effect = {
					district = district_farming
				}
				add_district_and_planet_size_if_needed_effect = {
					district = district_generator
				}
			}
			remove_all_buildings = yes
			add_building = building_major_capital
			add_building = building_robot_assembly_plant
#			add_building = building_bureaucratic_2
#			add_building = building_research_lab_2
#			add_building = building_paradise_dome
#			add_building = building_commercial_zone
#			add_building = building_factory_1
#			add_building = building_foundry_2
#			add_building = building_energy_grid
#			add_building = building_holo_theatres

			validate_planet_buildings_and_districts = yes
		}
	}

	planet = {
		count = { min = 2 max = 4 }
		orbit_distance = 30
		class = random_non_colonizable
		orbit_angle = { min = 90 max = 270 }

		change_orbit = 10

		moon = {
			count = { min = 0 max = 1 }
			class = random_non_colonizable
			orbit_angle = { min = 90 max = 270 }
			orbit_distance = 5
		}
	}

	planet = {
		count = { min = 1 max = 2 }
		class = pc_asteroid
		orbit_distance = 25
		orbit_angle = { min = 40 max = 100 }
	}

	init_effect = {
		create_starbase = {
			size = "starbase_starhold"
			owner = event_target:origin_imperial_homeland_country

			module = "shipyard"
			module = "shipyard"
			module = "anchorage"
			module = "anchorage"

			building = "hydroponics_bay"
			building = "naval_logistics_office"
		}
		generate_home_system_resources = yes
		every_system_planet = {
			limit = {
				has_deposit_for = shipclass_mining_station
				has_mining_station = no
			}
			create_mining_station = {
				owner = event_target:origin_imperial_homeland_country
			}
		}
		every_system_planet = {
			limit = {
				has_deposit_for = shipclass_research_station
				has_research_station = no
			}
			create_research_station = {
				owner = event_target:origin_imperial_homeland_country
			}
		}
		spawn_megastructure = {
			type = "gateway_derelict"
			random_pos = yes
			graphical_culture = event_target:origin_imperial_homeland_country
			owner = event_target:origin_imperial_homeland_country
			orbit_angle = 360		# range of the randomization
		}
	}

	neighbor_system = {
		hyperlane_jumps = { min = 1 max = 3 }
		initializer = "overlord_system_2_init"
	}

	neighbor_system = {
		hyperlane_jumps = { min = 1 max = 3 }
		initializer = "overlord_system_3_init"
	}

	neighbor_system = {
		trigger = {
			NOT = {
				any_neighbor_system = {
					has_star_flag = empire_home_system
				}
			}
		}
		initializer = "overlord_system_4_init"
	}

	neighbor_system = {
		trigger = {
			NOR = {
				galaxy_size = tiny
				any_neighbor_system = {
					has_star_flag = empire_home_system
				}
			}
		}
		initializer = "overlord_system_5_init"
	}

	neighbor_system = {
		trigger = {
			NOR = {
				galaxy_size = tiny
				galaxy_size = small
				any_neighbor_system = {
					has_star_flag = empire_home_system
				}
			}
		}
		initializer = "overlord_system_6_init"
	}

	neighbor_system = {
		trigger = {
			NOR = {
				galaxy_size = tiny
				galaxy_size = small
				galaxy_size = medium
				any_neighbor_system = {
					has_star_flag = empire_home_system
				}
			}
		}
		initializer = "overlord_system_7_init"
	}
}

overlord_system_2_init = { # Empire second system
	class = "rl_standard_stars"

	flags = { overlord_empire_flag overlord_system_2_flag }

	asteroid_belt = {
		type = rocky_asteroid_belt
		radius = 40
	}
	asteroid_belt = {
		type = rocky_asteroid_belt
		radius = 175
	}

	planet = {
		count = 1
		class = star
		orbit_distance = 0
		orbit_angle = 1
		size = { min = 20 max = 30 }
		has_ring = no
	}

	change_orbit = 40

	# Innermost asteroid belt
	planet = {
		count = { min = 2 max = 3 }
		class = pc_asteroid
		orbit_distance = 0
		orbit_angle = { min = 40 max = 100 }
	}

	planet = { # Colonizeable Planet
		class = "pc_continental"
		orbit_distance = 20
		orbit_angle = 150
		size = 18
		has_ring = no
		deposit_blockers = none
		modifiers = none

		init_effect = {
			save_event_target_as = imperial_homeland_2
			event_target:origin_imperial_species = {
				create_species = {
					is_mod = yes
					name = this
					plural = this
					class = this
					portrait = this
					traits = this
					can_be_modified = this
					homeworld = this
					namelist = this
					traits = {
						ideal_planet_class = event_target:imperial_homeland_2
					}
				}
			}
			last_created_species = {
				save_event_target_as = local_imperial_species
			}

			set_owner = event_target:origin_imperial_homeland_country
			set_controller = event_target:origin_imperial_homeland_country

			while = {
				count = 25
				create_pop_group = {
					species = event_target:local_imperial_species
				}
			}

			while = {
				count = 15
				create_pop_group = {
					species = event_target:origin_imperial_species_robot
				}
			}

			while = {
				count = 3
				add_district_and_planet_size_if_needed_effect = {
					district = district_city
				}
				add_district_and_planet_size_if_needed_effect = {
					district = district_mining
				}
				add_district_and_planet_size_if_needed_effect = {
					district = district_farming
				}
				add_district_and_planet_size_if_needed_effect = {
					district = district_generator
				}
			}

			remove_all_buildings = yes
			add_building = building_major_capital
			add_building = building_robot_assembly_plant
#			add_building = building_bureaucratic_1
#			add_building = building_research_lab_1
#			add_building = building_paradise_dome
#			add_building = building_commercial_zone
#			add_building = building_energy_grid

			validate_planet_buildings_and_districts = yes
		}
	}

	planet = {
		count = { min = 1 max = 2 }
		orbit_distance = 30
		class = random_non_colonizable
		orbit_angle = { min = 90 max = 270 }

		change_orbit = 10

		moon = {
			count = { min = 0 max = 1 }
			class = random_non_colonizable
			orbit_angle = { min = 90 max = 270 }
			orbit_distance = 5
		}
	}

	planet = {
		count = 1
		orbit_distance = 30
		class = pc_gas_giant
		orbit_angle = { min = 90 max = 270 }

		change_orbit = 10

		moon = {
			count = { min = 0 max = 1 }
			class = random_non_colonizable
			orbit_angle = { min = 90 max = 270 }
			orbit_distance = 5
		}
	}

	planet = {
		count = { min = 1 max = 2 }
		orbit_distance = 30
		class = random_non_colonizable
		orbit_angle = { min = 90 max = 270 }

		change_orbit = 10

		moon = {
			count = { min = 0 max = 1 }
			class = random_non_colonizable
			orbit_angle = { min = 90 max = 270 }
			orbit_distance = 5
		}
	}

	# Outermost asteroid belt
	planet = {
		count = { min = 3 max = 5 }
		class = pc_asteroid
		orbit_distance = 25
		orbit_angle = { min = 40 max = 100 }
	}

	init_effect = {
		create_starbase = {
			size = "starbase_starhold"
			owner = event_target:origin_imperial_homeland_country

			module = "shipyard"
			module = "shipyard"
			module = "anchorage"
			module = "anchorage"

			building = "hydroponics_bay"
			building = "naval_logistics_office"
		}
		generate_home_system_resources = yes
		every_system_planet = {
			limit = {
				has_deposit_for = shipclass_mining_station
				has_mining_station = no
			}
			create_mining_station = {
				owner = event_target:origin_imperial_homeland_country
			}
		}
		every_system_planet = {
			limit = {
				has_deposit_for = shipclass_research_station
				has_research_station = no
			}
			create_research_station = {
				owner = event_target:origin_imperial_homeland_country
			}
		}
	}
}

overlord_system_3_init = { # Empire third system
	class = "rl_standard_stars"

	flags = { overlord_empire_flag overlord_system_3_flag }

	asteroid_belt = {
		type = rocky_asteroid_belt
		radius = 35
	}

	planet = {
		count = 1
		class = star
		orbit_distance = 0
		orbit_angle = 1
		size = { min = 20 max = 30 }
		has_ring = no
	}

	change_orbit = 30

	planet = {
		count = 2
		class = pc_asteroid
		orbit_distance = { min = 1 max = 2 }
		orbit_angle = { min = 40 max = 100 }
		size = 2
	}

	planet = { # Colonizeable Planet
		class = "pc_ocean"
		orbit_distance = 20
		orbit_angle = 150
		size = 15
		has_ring = no
		deposit_blockers = none
		modifiers = none

		init_effect = {
			save_event_target_as = imperial_homeland_3
			event_target:origin_imperial_species = {
				create_species = {
					is_mod = yes
					name = this
					plural = this
					class = this
					portrait = this
					traits = this
					can_be_modified = this
					homeworld = this
					namelist = this
					traits = {
						ideal_planet_class = event_target:imperial_homeland_3
					}
				}
			}
			last_created_species = {
				save_event_target_as = local_imperial_species
			}

			set_owner = event_target:origin_imperial_homeland_country
			set_controller = event_target:origin_imperial_homeland_country

			while = {
				count = 20
				create_pop_group = {
					species = event_target:local_imperial_species
				}
			}

			while = {
				count = 10
				create_pop_group = {
					species = event_target:origin_imperial_species_robot
				}
			}

			while = {
				count = 2
				add_district_and_planet_size_if_needed_effect = {
					district = district_city
				}
				add_district_and_planet_size_if_needed_effect = {
					district = district_mining
				}
				add_district_and_planet_size_if_needed_effect = {
					district = district_farming
				}
				add_district_and_planet_size_if_needed_effect = {
					district = district_generator
				}
			}

			remove_all_buildings = yes
			add_building = building_major_capital
			add_building = building_robot_assembly_plant
#			add_building = building_bureaucratic_1
#			add_building = building_research_lab_1
#			add_building = building_paradise_dome
#			add_building = building_commercial_zone

			validate_planet_buildings_and_districts = yes
		}
	}

	planet = {
		count = { min = 2 max = 3 }
		orbit_distance = 30
		class = random_non_colonizable
		orbit_angle = { min = 90 max = 270 }

		change_orbit = 10

		moon = {
			count = { min = 0 max = 1 }
			class = random_non_colonizable
			orbit_angle = { min = 90 max = 270 }
			orbit_distance = 5
		}
	}

	planet = {
		count = { min = 2 max = 3 }
		orbit_distance = 30
		class = random_non_colonizable
		orbit_angle = { min = 90 max = 270 }

		change_orbit = 10

		moon = {
			count = { min = 0 max = 1 }
			class = random_non_colonizable
			orbit_angle = { min = 90 max = 270 }
			orbit_distance = 5
		}
	}

	planet = {
		count = { min = 2 max = 3 }
		class = pc_asteroid
		orbit_distance = 25
		orbit_angle = { min = 40 max = 100 }
	}

	init_effect = {
		create_starbase = {
			size = "starbase_starhold"
			owner = event_target:origin_imperial_homeland_country

			module = "shipyard"
			module = "shipyard"
			module = "anchorage"
			module = "anchorage"

			building = "hydroponics_bay"
			building = "naval_logistics_office"
		}
		generate_home_system_resources = yes
		every_system_planet = {
			limit = {
				has_deposit_for = shipclass_mining_station
				has_mining_station = no
			}
			create_mining_station = {
				owner = event_target:origin_imperial_homeland_country
			}
		}
		every_system_planet = {
			limit = {
				has_deposit_for = shipclass_research_station
				has_research_station = no
			}
			create_research_station = {
				owner = event_target:origin_imperial_homeland_country
			}
		}
	}
}

overlord_system_4_init = { # First vassal
	class = "rl_standard_stars"

	flags = { overlord_empire_flag overlord_system_4_flag }

	planet = {
		count = 1
		class = star
		orbit_distance = 0
		orbit_angle = 1
		size = { min = 20 max = 30 }
		has_ring = no
	}

	planet = {
		count = 1
		orbit_distance = 30
		class = random_non_colonizable
		orbit_angle = { min = 90 max = 270 }

		change_orbit = 10

		moon = {
			count = { min = 0 max = 1 }
			class = random_non_colonizable
			orbit_angle = { min = 90 max = 270 }
			orbit_distance = 5
		}
	}

	planet = {
		count = { min = 1 max = 2 }
		class = pc_asteroid
		orbit_distance = 30
		orbit_angle = { min = 40 max = 100 }
	}

	planet = { # Colonizeable Planet
		class = "pc_arid"
		orbit_distance = { min = 20 max = 35 }
		orbit_angle = { min = 90 max = 270 }
		size = { min = 14 max = 18 }
		has_ring = no
		deposit_blockers = none
		modifiers = none

		init_effect = {
			save_event_target_as = imperial_homeland_4
			create_species = {
				name = random
				class = random_non_machine
				portrait = random
				traits = random
				homeworld = this
			}
			last_created_species = {
				save_event_target_as = origin_imperial_species_2
			}
			create_country = {
				name = {
					key = "NAME_Imperial_Origin_Empire_2"
					variable_string = "[origin_imperial_species_2.GetNamePlural]"
				}
				authority = auth_dictatorial
				species = last_created_species
				civics = {
					civic = civic_technocracy
					civic = civic_slaver_guilds
				}
				ethos = {
					ethic = ethic_fanatic_materialist
					ethic = ethic_authoritarian
				}
				origin = "origin_imperial_vassal"

				effect = {
					set_country_flag = origin_imperial_vassal_technocrat_country
					save_global_event_target_as = origin_imperial_homeland_country_2

					# Tech
					give_physics_tech_tier_1_effect = yes
					give_engineering_tech_tier_1_effect = yes
					give_society_tech_tier_1_effect = yes

					# Subjugation
					set_subject_of = {
						who = event_target:origin_imperial_homeland_country
					}

					# Communication
					every_playable_country = {
						limit = {
							has_origin = origin_imperial_vassal
						}
						establish_communications_no_message = event_target:origin_imperial_homeland_country_2
					}

					# Fleets
					while = {
						count = 2
						create_fleet = {
							effect = {
								set_owner = event_target:origin_imperial_homeland_country_2
								create_ship = {
									name = random
									random_existing_design = science
								}
								set_location = event_target:origin_imperial_homeland_country_2.capital_star
								save_event_target_as = new_science_ship_target
							}
						}
						create_leader = {
							name = random
							species = event_target:origin_imperial_homeland_country_2.species
							class = scientist
							skill = 1
						}
						event_target:new_science_ship_target = {
							assign_leader = last_created_leader
						}
					}

					# Resources
					add_basic_resources_effect = { VALUE = 1000 }
					add_manufactured_resources_effect = { VALUE = 500 }
					add_advanced_resources_effect = { VALUE = 25 }

					# Policies
					set_policy = {
						policy = diplomatic_stance
						option = diplo_stance_isolationist
						cooldown = yes
					}
					set_policy = {
						policy = war_philosophy
						option = no_wars
						cooldown = yes
					}
					set_policy = {
						policy = orbital_bombardment
						option = orbital_bombardment_indiscriminate
						cooldown = yes
					}
					set_policy = {
						policy = resettlement
						option = resettlement_allowed
						cooldown = yes
					}
					set_policy = {
						policy = first_contact_protocol
						option = first_contact_attack_allowed
						cooldown = yes
					}
					set_policy = {
						policy = border_policy
						option = border_policy_open
						cooldown = yes
					}
					set_policy = {
						policy = economic_policy
						option = economic_policy_balanced
						cooldown = yes
					}
					set_policy = {
						policy = population_controls
						option = population_controls_allowed
						cooldown = yes
					}
					set_policy = {
						policy = purge
						option = purge_displacement_only
						cooldown = yes
					}
				}
			}
			set_owner = event_target:origin_imperial_homeland_country_2
			set_controller = event_target:origin_imperial_homeland_country_2
			set_capital = yes

			while = {
				count = 25
				create_pop_group = {
					species = owner_main_species
				}
			}

			colony = {
				generate_start_buildings_and_districts = yes
			}
		}
	}

	planet = {
		count = { min = 1 max = 2 }
		orbit_distance = 30
		class = pc_gas_giant
		orbit_angle = { min = 90 max = 270 }

		change_orbit = 10

		moon = {
			count = { min = 0 max = 1 }
			class = random_non_colonizable
			orbit_angle = { min = 90 max = 270 }
			orbit_distance = 5
		}
	}

	planet = {
		count = { min = 2 max = 3 }
		orbit_distance = 30
		class = random_non_colonizable
		orbit_angle = { min = 90 max = 270 }

		change_orbit = 10

		moon = {
			count = { min = 0 max = 1 }
			class = random_non_colonizable
			orbit_angle = { min = 90 max = 270 }
			orbit_distance = 5
		}
	}

	planet = {
		count = { min = 1 max = 2 }
		class = pc_asteroid
		orbit_distance = 25
		orbit_angle = { min = 40 max = 100 }
	}

	planet = {
		orbit_distance = 30
		class = random_non_colonizable
		orbit_angle = { min = 90 max = 270 }

		moon = {
			count = { min = 1 max = 2 }
			class = random_non_colonizable
			orbit_angle = { min = 90 max = 270 }
			orbit_distance = 5
		}
	}

	init_effect = {
		create_starbase = {
			size = "starbase_starport"
			owner = event_target:origin_imperial_homeland_country_2

			module = "shipyard"
			module = "anchorage"

			building = "crew_quarters"
		}
		generate_home_system_resources = yes
		every_system_planet = {
			limit = {
				has_deposit_for = shipclass_mining_station
				has_mining_station = no
			}
			create_mining_station = {
				owner = event_target:origin_imperial_homeland_country_2
			}
		}
		every_system_planet = {
			limit = {
				has_deposit_for = shipclass_research_station
				has_research_station = no
			}
			create_research_station = {
				owner = event_target:origin_imperial_homeland_country_2
			}
		}
	}
}

overlord_system_5_init = { # Second vassal
	class = "rl_standard_stars"

	flags = { overlord_empire_flag overlord_system_5_flag }

	planet = {
		count = 1
		class = star
		orbit_distance = 0
		orbit_angle = 1
		size = { min = 20 max = 30 }
		has_ring = no
	}

	planet = {
		count = { min = 2 max = 4 }
		class = pc_asteroid
		orbit_distance = 15
		orbit_angle = { min = 40 max = 100 }
	}

	planet = {
		count = { min = 1 max = 2 }
		orbit_distance = 20
		class = random_non_colonizable
		orbit_angle = { min = 90 max = 270 }

		change_orbit = 10

		moon = {
			count = { min = 1 max = 2 }
			class = random_non_colonizable
			orbit_angle = { min = 90 max = 270 }
			orbit_distance = 5
		}
	}

	planet = { # Colonizeable Planet
		class = "pc_continental"
		orbit_distance = { min = 30 max = 45 }
		orbit_angle = { min = 90 max = 270 }
		size = { min = 14 max = 18 }
		has_ring = no
		deposit_blockers = none
		modifiers = none

		init_effect = {
			save_event_target_as = imperial_homeland_5
			create_species = {
				name = random
				class = random_non_machine
				portrait = random
				traits = random
				homeworld = this
			}
			last_created_species = {
				save_event_target_as = origin_imperial_species_3
			}
			create_country = {
				name = {
					key = "NAME_Imperial_Origin_Empire_3"
					variable_string = "[origin_imperial_species_3.GetNamePlural]"
				}
				authority = auth_dictatorial
				species = last_created_species
				civics = {
					civic = civic_warrior_culture
					civic = civic_distinguished_admiralty
				}
				ethos = {
					ethic = ethic_fanatic_militarist
					ethic = ethic_spiritualist
				}
				origin = "origin_imperial_vassal"

				effect = {
					save_global_event_target_as = origin_imperial_homeland_country_3
					set_country_flag = origin_imperial_vassal_prince_electorate_country

					# Subjugation
					set_subject_of = {
						who = event_target:origin_imperial_homeland_country
					}

					# Communication
					every_playable_country = {
						limit = {
							has_origin = origin_imperial_vassal
						}
						establish_communications_no_message = event_target:origin_imperial_homeland_country_3
					}

					# Fleets
					create_fleet = {
						effect = {
							set_owner = event_target:origin_imperial_homeland_country_3
							create_ship = {
								name = random
								random_existing_design = constructor
							}
							set_location = event_target:origin_imperial_homeland_country_3.capital_star
						}
					}

					create_fleet = {
						effect = {
							set_owner = event_target:origin_imperial_homeland_country_3
							create_ship = {
								name = random
								random_existing_design = science
							}
							set_location = event_target:origin_imperial_homeland_country_3.capital_star
							save_event_target_as = new_science_ship_target
						}
					}
					create_leader = {
						name = random
						species = event_target:origin_imperial_homeland_country_3.species
						class = scientist
						skill = 1
					}
					event_target:new_science_ship_target = {
						assign_leader = last_created_leader
					}

					if = {
						limit = {
							event_target:origin_imperial_homeland_country_3 = {
								country_uses_bio_ships = yes
							}
						}
						create_fleet = {
							effect = {
								set_owner = event_target:origin_imperial_homeland_country_3
								while = {
									count = 5
									create_ship = {
										name = random
										design = NAME_CHARGER_1
										growth_stage = 1
									}
									create_ship = {
										name = random
										design = NAME_MOTH_1
										growth_stage = 1
									}
								}
								set_location = event_target:origin_imperial_homeland_country_3.capital_star
							}
						}
					}
					else = {
						create_fleet = {
							effect = {
								set_owner = event_target:origin_imperial_homeland_country_3
								while = {
									count = 5
									create_ship = {
										name = random
										design = NAME_Dagger
									}
									create_ship = {
										name = random
										design = NAME_Ravager
									}
								}
								set_location = event_target:origin_imperial_homeland_country_3.capital_star
							}
						}
					}

					# Resources
					add_basic_resources_effect = { VALUE = 1000 }
					add_manufactured_resources_effect = { VALUE = 500 }
					add_advanced_resources_effect = { VALUE = 25 }

					# Policies
					set_policy = {
						policy = diplomatic_stance
						option = diplo_stance_cooperative
						cooldown = yes
					}
					set_policy = {
						policy = war_philosophy
						option = liberation_wars
						cooldown = yes
					}
					set_policy = {
						policy = orbital_bombardment
						option = orbital_bombardment_selective
						cooldown = yes
					}
					set_policy = {
						policy = first_contact_protocol
						option = first_contact_cautious
						cooldown = yes
					}
					set_policy = {
						policy = border_policy
						option = border_policy_open
						cooldown = yes
					}
					set_policy = {
						policy = economic_policy
						option = economic_policy_military
						cooldown = yes
					}
					set_policy = {
						policy = purge
						option = purge_not_allowed
						cooldown = yes
					}
				}
			}
			set_owner = event_target:origin_imperial_homeland_country_3
			set_controller = event_target:origin_imperial_homeland_country_3
			set_capital = yes

			while = {
				count = 25
				create_pop_group = {
					species = owner_main_species
				}
			}

			colony = {
				generate_start_buildings_and_districts = yes
			}
		}
	}

	planet = {
		class = pc_gas_giant
		orbit_distance = 20
		orbit_angle = { min = 90 max = 270 }

		moon = {
			count = { min = 0 max = 3 }
			class = random_non_colonizable
			orbit_angle = { min = 90 max = 270 }
			orbit_distance = 5
		}
	}

	planet = {
		count = { min = 2 max = 4 }
		class = pc_asteroid
		orbit_distance = 15
		orbit_angle = { min = 40 max = 100 }
	}

	init_effect = {
		create_starbase = {
			size = "starbase_starport"
			owner = event_target:origin_imperial_homeland_country_3

			module = "shipyard"
			module = "anchorage"

			building = "crew_quarters"
		}
		generate_home_system_resources = yes
		every_system_planet = {
			limit = {
				has_deposit_for = shipclass_mining_station
				has_mining_station = no
			}
			create_mining_station = {
				owner = event_target:origin_imperial_homeland_country_3
			}
		}
		every_system_planet = {
			limit = {
				has_deposit_for = shipclass_research_station
				has_research_station = no
			}
			create_research_station = {
				owner = event_target:origin_imperial_homeland_country_3
			}
		}
	}
}

overlord_system_6_init = { # Third vassal
	class = "rl_standard_stars"

	flags = { overlord_empire_flag overlord_system_6_flag }

	asteroid_belt = {
		type = rocky_asteroid_belt
		radius = 125
	}

	asteroid_belt = {
		type = rocky_asteroid_belt
		radius = 200
	}

	planet = {
		count = 1
		class = star
		orbit_distance = 0
		orbit_angle = 1
		size = { min = 20 max = 30 }
		has_ring = no
	}

	planet = {
		orbit_distance = 40
		class = random_non_colonizable
		orbit_angle = { min = 90 max = 270 }

		moon = {
			count = { min = 0 max = 1 }
			class = random_non_colonizable
			orbit_angle = { min = 90 max = 270 }
			orbit_distance = 5
		}
	}

	planet = { # Colonizeable Planet
		class = "pc_arctic"
		orbit_distance = 40
		orbit_angle = { min = 90 max = 270 }
		size = { min = 14 max = 18 }
		has_ring = no
		deposit_blockers = none
		modifiers = none

		init_effect = {
			save_event_target_as = imperial_homeland_6
			create_species = {
				name = random
				class = random_non_machine
				portrait = random
				traits = random
				homeworld = this
			}
			last_created_species = {
				save_event_target_as = origin_imperial_species_4
			}
			create_country = {
				name = {
					key = "NAME_Imperial_Origin_Empire_4"
					variable_string = "[origin_imperial_species_4.GetNamePlural]"
				}
				authority = auth_dictatorial
				species = last_created_species
				civics = {
					civic = civic_philosopher_king
					civic = civic_exalted_priesthood
				}
				ethos = {
					ethic = ethic_fanatic_spiritualist
					ethic = ethic_xenophobe
				}
				origin = "origin_imperial_vassal"

				effect = {
					save_global_event_target_as = origin_imperial_homeland_country_4
					set_country_flag = origin_imperial_vassal_palatinate_country

					# Subjugation
					set_subject_of = {
						who = event_target:origin_imperial_homeland_country
					}

					# Communication
					every_playable_country = {
						limit = {
							has_origin = origin_imperial_vassal
						}
						establish_communications_no_message = event_target:origin_imperial_homeland_country_4
					}

					# Fleets
					create_fleet = {
						effect = {
							set_owner = event_target:origin_imperial_homeland_country_4
							create_ship = {
								name = random
								random_existing_design = constructor
							}
							set_location = event_target:origin_imperial_homeland_country_4.capital_star
						}
					}

					create_fleet = {
						effect = {
							set_owner = event_target:origin_imperial_homeland_country_4
							create_ship = {
								name = random
								random_existing_design = science
							}
							set_location = event_target:origin_imperial_homeland_country_4.capital_star
							save_event_target_as = new_science_ship_target
						}
					}
					create_leader = {
						name = random
						species = event_target:origin_imperial_homeland_country_4.species
						class = scientist
						skill = 1
					}
					event_target:new_science_ship_target = {
						assign_leader = last_created_leader
					}

					# Resources
					add_basic_resources_effect = { VALUE = 1000 }
					add_manufactured_resources_effect = { VALUE = 500 }
					add_advanced_resources_effect = { VALUE = 25 }

					# Policies
					set_policy = {
						policy = diplomatic_stance
						option = diplo_stance_isolationist
						cooldown = yes
					}
					set_policy = {
						policy = war_philosophy
						option = no_wars
						cooldown = yes
					}
					set_policy = {
						policy = orbital_bombardment
						option = orbital_bombardment_selective
						cooldown = yes
					}
					set_policy = {
						policy = resettlement
						option = resettlement_not_allowed
						cooldown = yes
					}
					set_policy = {
						policy = first_contact_protocol
						option = first_contact_cautious
						cooldown = yes
					}
					set_policy = {
						policy = border_policy
						option = border_policy_closed
						cooldown = yes
					}
					set_policy = {
						policy = economic_policy
						option = economic_policy_civilian
						cooldown = yes
					}
					set_policy = {
						policy = robot_pop_policy
						option = robot_pops_outlawed
						cooldown = yes
					}
					set_policy = {
						policy = population_controls
						option = population_controls_not_allowed
						cooldown = yes
					}
					set_policy = {
						policy = slavery
						option = slavery_not_allowed
						cooldown = yes
					}
					set_policy = {
						policy = purge
						option = purge_allowed
						cooldown = yes
					}
				}
			}
			set_owner = event_target:origin_imperial_homeland_country_4
			set_controller = event_target:origin_imperial_homeland_country_4
			set_capital = yes

			while = {
				count = 25
				create_pop_group = {
					species = owner_main_species
				}
			}

			colony = {
				generate_start_buildings_and_districts = yes
			}
		}
	}

	change_orbit = 40

	# Innermost asteroid belt (r=125)

	planet = {
		count = { min = 1 max = 2 }
		class = pc_asteroid
		orbit_distance = 2
		orbit_angle = { min = 40 max = 100 }
	}

	planet = {
		count = 2
		class = pc_gas_giant
		orbit_distance = 25
		orbit_angle = { min = 90 max = 270 }

		moon = {
			count = { min = 0 max = 3 }
			class = random_non_colonizable
			orbit_angle = { min = 90 max = 270 }
			orbit_distance = 8
		}
	}

	change_orbit = 15

	# Outermost asteroid belt (r=200)

	planet = {
		count = { min = 2 max = 3 }
		class = pc_asteroid
		orbit_distance = { min = 1 max = 3 }
		orbit_angle = { min = 40 max = 100 }
	}

	init_effect = {
		create_starbase = {
			size = "starbase_starport"
			owner = event_target:origin_imperial_homeland_country_4

			module = "shipyard"
			module = "anchorage"

			building = "crew_quarters"
		}
		generate_home_system_resources = yes
		every_system_planet = {
			limit = {
				has_deposit_for = shipclass_mining_station
				has_mining_station = no
			}
			create_mining_station = {
				owner = event_target:origin_imperial_homeland_country_4
			}
		}
		every_system_planet = {
			limit = {
				has_deposit_for = shipclass_research_station
				has_research_station = no
			}
			create_research_station = {
				owner = event_target:origin_imperial_homeland_country_4
			}
		}
	}
}

overlord_system_7_init = { # Fourth vassal
	class = "rl_standard_stars"

	flags = { overlord_empire_flag overlord_system_7_flag }

	planet = {
		count = 1
		class = star
		orbit_distance = 0
		orbit_angle = 1
		size = { min = 20 max = 30 }
		has_ring = no
	}

	asteroid_belt = {
		type = icy_asteroid_belt
		radius = 150
	}

	planet = {
		count = { min = 0 max = 2 }
		orbit_distance = 35
		class = random_non_colonizable
		orbit_angle = { min = 90 max = 270 }

		moon = {
			count = { min = 0 max = 1 }
			class = random_non_colonizable
			orbit_angle = { min = 90 max = 270 }
			orbit_distance = 5
		}
	}

	change_orbit = 10

	planet = {
		count = { min = 3 max = 4 }
		class = pc_asteroid
		orbit_distance = 0
		orbit_angle = { min = 40 max = 90 }
	}

	planet = { # Colonizeable Planet
		class = "pc_ocean"
		orbit_distance = { min = 20 max = 35 }
		orbit_angle = { min = 90 max = 270 }
		size = { min = 14 max = 18 }
		has_ring = no
		deposit_blockers = none
		modifiers = none

		init_effect = {
			save_event_target_as = imperial_homeland_7
			create_species = {
				name = random
				class = random_non_machine
				portrait = random
				traits = random
				homeworld = this
			}
			last_created_species = {
				save_event_target_as = origin_imperial_species_5
			}
			if = {
				limit = {
					host_has_dlc = "Megacorp"
				}
				create_country = {
					name = {
						key = "NAME_Imperial_Origin_Empire_5"
						variable_string = "[origin_imperial_species_5.GetAdj]"
					}
					authority = auth_corporate
					species = last_created_species
					civics = {
						civic = civic_free_traders
						civic = civic_ruthless_competition
					}
					ethos = {
						ethic = ethic_fanatic_materialist
						ethic = ethic_xenophile
					}
					origin = "origin_imperial_vassal"

					effect = {
						save_global_event_target_as = origin_imperial_homeland_country_5
					}
				}
			}
			else = {
				create_country = {
					name = {
						key = "NAME_Imperial_Origin_Empire_5"
						variable_string = "[origin_imperial_species_5.GetAdj]"
					}
					authority = auth_oligarchic
					species = last_created_species
					civics = {
						civic = civic_meritocracy
						civic = civic_corporate_dominion
					}
					ethos = {
						ethic = ethic_fanatic_materialist
						ethic = ethic_xenophile
					}
					origin = "origin_imperial_vassal"

					effect = {
						save_global_event_target_as = origin_imperial_homeland_country_5
					}
				}
			}

			event_target:origin_imperial_homeland_country_5 = {
				set_country_flag = origin_imperial_vassal_league_country
				# Subjugation
				set_subject_of = {
					who = event_target:origin_imperial_homeland_country
				}

				# Communication
				every_playable_country = {
					limit = {
						has_origin = origin_imperial_vassal
					}
					establish_communications_no_message = event_target:origin_imperial_homeland_country_5
				}

				# Fleets
				create_fleet = {
					effect = {
						set_owner = event_target:origin_imperial_homeland_country_5
						create_ship = {
							name = random
							random_existing_design = science
						}
						set_location = event_target:origin_imperial_homeland_country_5.capital_star
						save_event_target_as = new_science_ship_target
					}
				}
				create_leader = {
					name = random
					species = event_target:origin_imperial_homeland_country_5.species
					class = scientist
					skill = 1
				}
				event_target:new_science_ship_target = {
					assign_leader = last_created_leader
				}

				if = {
					limit = {
						event_target:origin_imperial_homeland_country_3 = {
							country_uses_bio_ships = yes
						}
					}
					create_fleet = {
						effect = {
							set_owner = event_target:origin_imperial_homeland_country_5
							while = {
								count = 5
								create_ship = {
									name = random
									design = NAME_CHARGER_1
									growth_stage = 1
								}
							}
							set_location = event_target:origin_imperial_homeland_country_5.capital_star
						}
					}
				}
				else = {
					create_fleet = {
						effect = {
							set_owner = event_target:origin_imperial_homeland_country_5
							while = {
								count = 5
								create_ship = {
									name = random
									design = NAME_Dagger
								}
							}
							set_location = event_target:origin_imperial_homeland_country_5.capital_star
						}
					}
				}

				# Resources
				add_basic_resources_effect = { VALUE = 1000 }
				add_manufactured_resources_effect = { VALUE = 500 }
				add_advanced_resources_effect = { VALUE = 25 }

				# Policies
				set_policy = {
					policy = diplomatic_stance
					option = diplo_stance_mercantile
					cooldown = yes
				}
				set_policy = {
					policy = war_philosophy
					option = liberation_wars
					cooldown = yes
				}
				set_policy = {
					policy = orbital_bombardment
					option = orbital_bombardment_selective
					cooldown = yes
				}
				set_policy = {
					policy = resettlement
					option = resettlement_allowed
					cooldown = yes
				}
				set_policy = {
					policy = first_contact_protocol
					option = first_contact_proactive
					cooldown = yes
				}
				set_policy = {
					policy = border_policy
					option = border_policy_open
					cooldown = yes
				}
				set_policy = {
					policy = population_controls
					option = population_controls_not_allowed
					cooldown = yes
				}
			}

			set_owner = event_target:origin_imperial_homeland_country_5
			set_controller = event_target:origin_imperial_homeland_country_5
			set_capital = yes

			while = {
				count = 25
				create_pop_group = {
					species = owner_main_species
				}
			}

			colony = {
				generate_start_buildings_and_districts = yes
			}
		}
	}

	planet = {
		count = { min = 1 max = 2 }
		orbit_distance = 30
		class = pc_gas_giant
		orbit_angle = { min = 90 max = 270 }

		moon = {
			count = { min = 0 max = 3 }
			class = random_non_colonizable
			orbit_angle = { min = 90 max = 270 }
			orbit_distance = 5
		}
	}

	planet = {
		count = { min = 1 max = 2 }
		orbit_distance = 20
		class = random_non_colonizable
		orbit_angle = { min = 90 max = 270 }

		moon = {
			count = { min = 0 max = 1 }
			class = random_non_colonizable
			orbit_angle = { min = 90 max = 270 }
			orbit_distance = 5
		}
	}

	init_effect = {
		create_starbase = {
			size = "starbase_starport"
			owner = event_target:origin_imperial_homeland_country_5

			module = "shipyard"
			module = "anchorage"

			building = "crew_quarters"
		}
		generate_home_system_resources = yes
		every_system_planet = {
			limit = {
				has_deposit_for = shipclass_mining_station
				has_mining_station = no
			}
			create_mining_station = {
				owner = event_target:origin_imperial_homeland_country_5
			}
		}
		every_system_planet = {
			limit = {
				has_deposit_for = shipclass_research_station
				has_research_station = no
			}
			create_research_station = {
				owner = event_target:origin_imperial_homeland_country_5
			}
		}
	}
}

overlord_system_8_init = { # Overlord Colonies
	class = "rl_standard_stars"

	flags = { overlord_empire_flag overlord_colony_flag }

	planet = {
		count = 1
		class = star
		orbit_distance = 0
		orbit_angle = 1
		size = { min = 20 max = 30 }
		has_ring = no
	}

	asteroid_belt = {
		type = rocky_asteroid_belt
		radius = { min = 35 max = 55 }
	}

	asteroid_belt = {
		type = rocky_asteroid_belt
		radius = { min = 65 max = 85 }
	}

	change_orbit = 45

	# Innermost asteroid belt
	planet = {
		count = { min = 1 max = 2 }
		class = pc_asteroid
		orbit_distance = 0
		orbit_angle = { min = 120 max = 300 }
	}

	change_orbit = 45

	planet = {
		count = 1
		orbit_distance = 25
		class = pc_gas_giant
		orbit_angle = { min = 90 max = 270 }
		size = 25

		change_orbit = @base_moon_distance

		moon = {
			count = 1
			class = random_non_colonizable
			orbit_angle = { min = 90 max = 270 }
			orbit_distance = 4
		}

		moon = {
			count = 1
			home_planet = yes
			class = random_colonizable
			orbit_distance = 10
			orbit_angle = { min = 90 max = 270 }
			size = { min = 15 max = 20 }
			deposit_blockers = none
			modifiers = none

			init_effect = {
				prevent_anomaly = yes
			}

			init_effect = {
				save_event_target_as = imperial_colony
				event_target:origin_imperial_species = {
					create_species = {
						is_mod = yes
						name = this
						plural = this
						class = this
						portrait = this
						traits = this
						can_be_modified = this
						homeworld = this
						namelist = this
						traits = {
							ideal_planet_class = event_target:imperial_colony
						}
					}
				}
				last_created_species = {
					save_event_target_as = local_imperial_species
				}

				set_owner = event_target:origin_imperial_homeland_country
				set_controller = event_target:origin_imperial_homeland_country

				while = {
					count = 10
					create_pop_group = {
						species = event_target:local_imperial_species
					}
				}

				while = {
					count = 10
					create_pop_group = {
						species = event_target:origin_imperial_species_robot
					}
				}

				add_district_and_planet_size_if_needed_effect = {
					district = district_city
				}
				add_district_and_planet_size_if_needed_effect = {
					district = district_mining
				}
				add_district_and_planet_size_if_needed_effect = {
					district = district_farming
				}
				add_district_and_planet_size_if_needed_effect = {
					district = district_generator
				}

				remove_all_buildings = yes
				add_building = building_capital
				add_building = building_bureaucratic_1
				add_building = building_research_lab_1
#				add_building = building_commercial_zone

				validate_planet_buildings_and_districts = yes
			}
		}
	}

	change_orbit = 45

	planet = {
		count = { min = 1 max = 3 }
		orbit_distance = 25
		class = random_non_colonizable
		orbit_angle = { min = 90 max = 270 }
	}

	init_effect = {
		create_starbase = {
			size = "starbase_starhold"
			owner = event_target:origin_imperial_homeland_country

			module = "shipyard"
			module = "shipyard"
			module = "anchorage"
			module = "anchorage"

			building = "hydroponics_bay"
			building = "naval_logistics_office"
		}

		spawn_megastructure = {
			type = "gateway_derelict"
			random_pos = yes
			graphical_culture = event_target:origin_imperial_homeland_country
			owner = event_target:origin_imperial_homeland_country
			orbit_angle = 360		# range of the randomization
		}
	}
}
