# This is called in initializers for all standard empires, before the empire actually exists
generate_empire_home_planet = {
}

generate_home_system_resources = {
	optimize_memory
	every_system_planet = {
		limit = {
			colonizable_planet = no
			NOT = { has_planet_flag = ignore_startup_effect }
		}
		prevent_anomaly = yes
		clear_deposits = yes
	}

	system_star = {
		set_planet_flag = starting_mining_target
		set_planet_flag = starting_deposit
		set_deposit = d_energy_5
	}

	random_system_planet = {
		limit = {
			NOT = { has_planet_flag = starting_deposit }
			NOT = { has_planet_flag = ignore_startup_effect }
			OR = {
				is_planet_class = pc_a_star
				is_planet_class = pc_b_star
				is_planet_class = pc_f_star
				is_planet_class = pc_g_star
				is_planet_class = pc_k_star
				is_planet_class = pc_m_star
				is_planet_class = pc_molten
				is_planet_class = pc_gas_giant
				is_planet_class = pc_t_star
			}
		}

		set_planet_flag = starting_mining_target
		set_planet_flag = starting_deposit
		set_deposit = d_energy_5
	}

	while = {
		count = 2
		random_system_planet = {
			limit = {
				NOT = { has_planet_flag = starting_deposit }
				NOT = { has_planet_flag = ignore_startup_effect }
				OR = {
					is_asteroid = yes
					is_planet_class = pc_barren
					is_planet_class = pc_barren_cold
					is_planet_class = pc_toxic
					is_planet_class = pc_frozen
				}
			}

			set_planet_flag = starting_mining_target
			set_planet_flag = starting_deposit
			set_deposit = d_minerals_5
		}
	}

	random_system_planet = {
		limit = {
			NOT = { has_planet_flag = starting_deposit }
			NOT = { has_planet_flag = ignore_startup_effect }
			OR = {
				is_planet_class = pc_a_star
				is_planet_class = pc_b_star
				is_planet_class = pc_f_star
				is_planet_class = pc_g_star
				is_planet_class = pc_k_star
				is_planet_class = pc_m_star
				is_planet_class = pc_molten
				is_planet_class = pc_gas_giant
				is_planet_class = pc_t_star
			}
		}

		set_planet_flag = starting_deposit
		random_list = {
			50 = { set_deposit = d_energy_2 }
			50 = { set_deposit = d_energy_3 }
		}
	}

	random_system_planet = {
		limit = {
			NOT = { has_planet_flag = starting_deposit }
			NOT = { has_planet_flag = ignore_startup_effect }
			OR = {
				is_asteroid = yes
				is_planet_class = pc_barren
				is_planet_class = pc_barren_cold
				is_planet_class = pc_toxic
				is_planet_class = pc_frozen
			}
		}

		set_planet_flag = starting_deposit
		random_list = {
			50 = { set_deposit = d_minerals_2 }
			50 = { set_deposit = d_minerals_3 }
		}
	}

	random_system_planet = {
		limit = {
			NOT = { has_planet_flag = starting_deposit }
			NOT = { has_planet_flag = ignore_startup_effect }
			OR = {
				is_planet_class = pc_gas_giant
				is_planet_class = pc_toxic
				is_planet_class = pc_frozen
				is_planet_class = pc_barren_cold
				is_planet_class = pc_barren
				is_star = yes
			}
		}

		set_planet_flag = starting_research_target
		set_planet_flag = starting_deposit

		if = {
			limit = {
				is_planet_class = pc_toxic
			}
			set_deposit = d_society_3
		}
		else_if = {
			limit = {
				OR = {
					is_planet_class = pc_barren
					is_star = yes
				}
			}
			set_deposit = d_physics_3
		}
		else = {
			set_deposit = d_engineering_3
		}
	}

	random_system_planet = {
		limit = {
			NOT = { has_planet_flag = starting_deposit }
			NOT = { has_planet_flag = ignore_startup_effect }
			OR = {
				is_planet_class = pc_gas_giant
				is_planet_class = pc_toxic
				is_planet_class = pc_frozen
				is_planet_class = pc_barren_cold
				is_planet_class = pc_barren
				is_star = yes
			}
		}

		set_planet_flag = starting_deposit

		if = {
			limit = {
				is_planet_class = pc_toxic
			}
			set_deposit = d_society_2
		}
		else_if = {
			limit = {
				OR = {
					is_planet_class = pc_barren
					is_star = yes
				}
			}
			set_deposit = d_physics_2
		}
		else = {
			set_deposit = d_engineering_2
		}
	}
}

add_random_natural_blocker_and_give_tech = { # see also 'has_natural_blocker', 'wilderness_get_natural_blocker_name'
	if = {
		limit = {
			is_dry = yes
		}
		random_list = {
			10 = {
				modifier = {
					factor = 0.1
					owner = {
						has_technology = tech_deep_sinkhole
					}
				}
				add_blocker = {
					type = d_deep_sinkhole
					blocked_deposit = none
				}
				owner = {
					if = {
						limit = {
							NOT = {
								has_technology = tech_deep_sinkhole
							}
						}
						give_technology = {
							tech = tech_deep_sinkhole
							message = no
						}
					}
				}
			}
			10 = {
				modifier = {
					factor = 0.1
					owner = {
						has_technology = tech_quicksand_basin
					}
				}
				add_blocker = {
					type = d_quicksand_basin
					blocked_deposit = none
				}
				owner = {
					if = {
						limit = {
							NOT = {
								has_technology = tech_quicksand_basin
							}
						}
						give_technology = {
							tech = tech_quicksand_basin
							message = no
						}
					}
				}
			}
			10 = {
				modifier = {
					factor = 0.1
					owner = {
						has_technology = tech_volcano
					}
				}
				modifier = {
					factor = 0
					owner = {
						has_origin = origin_arc_welders # Starting planet is too small
					}
				}
				add_blocker = {
					type = d_active_volcano
					blocked_deposit = none
				}
				owner = {
					if = {
						limit = {
							NOT = {
								has_technology = tech_volcano
							}
						}
						give_technology = {
							tech = tech_volcano
							message = no
						}
					}
				}
			}
			10 = {
				modifier = {
					factor = 0.1
					owner = {
						has_technology = tech_mountain_range
					}
				}
				modifier = {
					factor = 0
					owner = {
						has_origin = origin_arc_welders # Starting planet is too small
					}
				}
				add_blocker = {
					type = d_mountain_range
					blocked_deposit = none
				}
				owner = {
					if = {
						limit = {
							NOT = {
								has_technology = tech_mountain_range
							}
						}
						give_technology = {
							tech = tech_mountain_range
							message = no
						}
					}
				}
			}
			10 = {
				modifier = {
					factor = 0.1
					owner = {
						has_technology = tech_dangerous_wildlife
					}
				}
				modifier = {
					factor = 0
					owner = {
						has_origin = origin_arc_welders # Starting planet is too small
					}
				}
				add_blocker = {
					type = d_dangerous_wildlife_blocker
					blocked_deposit = none
				}
				owner = {
					if = {
						limit = {
							NOT = {
								has_technology = tech_dangerous_wildlife
							}
						}
						give_technology = {
							tech = tech_dangerous_wildlife
							message = no
						}
					}
				}
			}
		}
	}
	else_if = {
		limit = {
			is_wet = yes
		}
		random_list = {
			10 = {
				modifier = {
					factor = 0.1
					owner = {
						has_technology = tech_dense_jungle
					}
				}
				add_blocker = {
					type = d_dense_jungle
					blocked_deposit = none
				}
				owner = {
					if = {
						limit = {
							NOT = {
								has_technology = tech_dense_jungle
							}
						}
						give_technology = {
							tech = tech_dense_jungle
							message = no
						}
					}
				}
			}
			10 = {
				modifier = {
					factor = 0.1
					owner = {
						has_technology = tech_toxic_kelp
					}
				}
				add_blocker = {
					type = d_toxic_kelp
					blocked_deposit = none
				}
				owner = {
					if = {
						limit = {
							NOT = {
								has_technology = tech_toxic_kelp
							}
						}
						give_technology = {
							tech = tech_toxic_kelp
							message = no
						}
					}
				}
			}
			10 = {
				modifier = {
					factor = 0.1
					owner = {
						has_technology = tech_volcano
					}
				}
				modifier = {
					factor = 0
					owner = {
						has_origin = origin_arc_welders # Starting planet is too small
					}
				}
				add_blocker = {
					type = d_active_volcano
					blocked_deposit = none
				}
				owner = {
					if = {
						limit = {
							NOT = {
								has_technology = tech_volcano
							}
						}
						give_technology = {
							tech = tech_volcano
							message = no
						}
					}
				}
			}
			10 = {
				modifier = {
					factor = 0.1
					owner = {
						has_technology = tech_mountain_range
					}
				}
				modifier = {
					factor = 0
					owner = {
						has_origin = origin_arc_welders # Starting planet is too small
					}
				}
				add_blocker = {
					type = d_mountain_range
					blocked_deposit = none
				}
				owner = {
					if = {
						limit = {
							NOT = {
								has_technology = tech_mountain_range
							}
						}
						give_technology = {
							tech = tech_mountain_range
							message = no
						}
					}
				}
			}
			10 = {
				modifier = {
					factor = 0.1
					owner = {
						has_technology = tech_dangerous_wildlife
					}
				}
				modifier = {
					factor = 0
					owner = {
						has_origin = origin_arc_welders # Starting planet is too small
					}
				}
				add_blocker = {
					type = d_dangerous_wildlife_blocker
					blocked_deposit = none
				}
				owner = {
					if = {
						limit = {
							NOT = {
								has_technology = tech_dangerous_wildlife
							}
						}
						give_technology = {
							tech = tech_dangerous_wildlife
							message = no
						}
					}
				}
			}
		}
	}
	else_if = {
		limit = {
			is_cold = yes
		}
		random_list = {
			10 = {
				modifier = {
					factor = 0.1
					owner = {
						has_technology = tech_noxious_swamp
					}
				}
				add_blocker = {
					type = d_noxious_swamp
					blocked_deposit = none
				}
				owner = {
					if = {
						limit = {
							NOT = {
								has_technology = tech_noxious_swamp
							}
						}
						give_technology = {
							tech = tech_noxious_swamp
							message = no
						}
					}
				}
			}
			10 = {
				modifier = {
					factor = 0.1
					owner = {
						has_technology = tech_massive_glacier
					}
				}
				add_blocker = {
					type = d_massive_glacier
					blocked_deposit = none
				}
				owner = {
					if = {
						limit = {
							NOT = {
								has_technology = tech_massive_glacier
							}
						}
						give_technology = {
							tech = tech_massive_glacier
							message = no
						}
					}
				}
			}
			10 = {
				modifier = {
					factor = 0.1
					owner = {
						has_technology = tech_volcano
					}
				}
				modifier = {
					factor = 0
					owner = {
						has_origin = origin_arc_welders # Starting planet is too small
					}
				}
				add_blocker = {
					type = d_active_volcano
					blocked_deposit = none
				}
				owner = {
					if = {
						limit = {
							NOT = {
								has_technology = tech_volcano
							}
						}
						give_technology = {
							tech = tech_volcano
							message = no
						}
					}
				}
			}
			10 = {
				modifier = {
					factor = 0.1
					owner = {
						has_technology = tech_mountain_range
					}
				}
				modifier = {
					factor = 0
					owner = {
						has_origin = origin_arc_welders # Starting planet is too small
					}
				}
				add_blocker = {
					type = d_mountain_range
					blocked_deposit = none
				}
				owner = {
					if = {
						limit = {
							NOT = {
								has_technology = tech_mountain_range
							}
						}
						give_technology = {
							tech = tech_mountain_range
							message = no
						}
					}
				}
			}
			10 = {
				modifier = {
					factor = 0.1
					owner = {
						has_technology = tech_dangerous_wildlife
					}
				}
				modifier = {
					factor = 0
					owner = {
						has_origin = origin_arc_welders # Starting planet is too small
					}
				}
				add_blocker = {
					type = d_dangerous_wildlife_blocker
					blocked_deposit = none
				}
				owner = {
					if = {
						limit = {
							NOT = {
								has_technology = tech_dangerous_wildlife
							}
						}
						give_technology = {
							tech = tech_dangerous_wildlife
							message = no
						}
					}
				}
			}
		}
	}
}

add_random_natural_blocker = {
	if = {
		limit = {
			is_dry = yes
		}
		random_list = {
			1 = {
				add_deposit = d_deep_sinkhole
			}
			1 = {
				add_deposit = d_quicksand_basin
			}
			1 = {
				add_deposit = d_active_volcano
			}
			1 = {
				add_deposit = d_mountain_range
			}
			1 = {
				add_deposit = d_dangerous_wildlife_blocker
			}
		}
	}
	else_if = {
		limit = {
			is_wet = yes
		}
		random_list = {
			1 = {
				add_deposit = d_dense_jungle
			}
			1 = {
				add_deposit = d_toxic_kelp
			}
			1 = {
				add_deposit = d_active_volcano
			}
			1 = {
				add_deposit = d_mountain_range
			}
			1 = {
				add_deposit = d_dangerous_wildlife_blocker
			}
		}
	}
	else_if = {
		limit = {
			is_cold = yes
		}
		random_list = {
			1 = {
				add_deposit = d_noxious_swamp
			}
			1 = {
				add_deposit = d_massive_glacier
			}
			1 = {
				add_deposit = d_active_volcano
			}
			1 = {
				add_deposit = d_mountain_range
			}
			1 = {
				add_deposit = d_dangerous_wildlife_blocker
			}
		}
	}
}

add_research_options_for_natural_blockers = {
	if = {
		limit = {
			has_deposit = d_mountain_range
		}
		owner = {
			add_research_option = tech_mountain_range
		}
	}
	if = {
		limit = {
			OR = {
				has_deposit = d_active_volcano
				has_deposit = d_raging_lavafalls
				has_deposit = d_rugged_landscape
				has_deposit = d_seismic_bombing_crater
			}
		}
		owner = {
			add_research_option = tech_volcano
		}
	}
	if = {
		limit = {
			has_deposit = d_dangerous_wildlife_blocker
		}
		owner = {
			add_research_option = tech_dangerous_wildlife
		}
	}
	if = {
		limit = {
			has_deposit = d_dense_jungle
		}
		owner = {
			add_research_option = tech_dense_jungle
		}
	}
	if = {
		limit = {
			has_deposit = d_toxic_kelp
		}
		owner = {
			add_research_option = tech_toxic_kelp
		}
	}
	if = {
		limit = {
			has_deposit = d_deep_sinkhole
		}
		owner = {
			add_research_option = tech_deep_sinkhole
		}
	}
	if = {
		limit = {
			has_deposit = d_quicksand_basin
		}
		owner = {
			add_research_option = tech_quicksand_basin
		}
	}
	if = {
		limit = {
			has_deposit = d_noxious_swamp
		}
		owner = {
			add_research_option = tech_noxious_swamp
		}
	}
	if = {
		limit = {
			has_deposit = d_massive_glacier
		}
		owner = {
			add_research_option = tech_massive_glacier
		}
	}
}

# This is called after game start
generate_start_deposits_and_blockers = {
	optimize_memory
	clear_deposits = yes

	# blockers
	if = {
		limit = {
			owner = {
				is_apocalyptic_empire = yes
			}
		}
		add_deposit = d_tomb_world_ruins
		if = {
			limit = {
				owner = {
					is_environmental_architects_empire = yes
				}
			}
			add_random_natural_blocker_and_give_tech = yes
		}
		else = {
			add_deposit = d_tomb_world_wasteland
		}
	}
	else_if = {
		limit = {
			owner = {
				has_civic = civic_machine_servitor
			}
		}

		add_deposit = d_unsupervised_settlement
		if = {
			limit = {
				owner = {
					is_environmental_architects_empire = yes
				}
			}
			add_random_natural_blocker_and_give_tech = yes
		}
		else = {
			add_deposit = d_strip_mine
		}
	}
	else_if = {
		limit = {
			owner = {
				is_environmental_architects_empire = yes
			}
		}

		while = {
			count = 2
			add_random_natural_blocker_and_give_tech = yes
		}
	}
	else_if = {
		limit = {
			owner = {
				has_civic = civic_machine_terminator
				NOT = { has_origin = origin_machine }
			}
		}

		add_deposit = d_exterminators_ruins
		add_deposit = d_former_battlefield
	}
	else_if = {
		limit = {
			owner = {
				has_civic = civic_machine_assimilator
				NOT = { has_origin = origin_machine }
			}
		}

		add_deposit = d_strip_mine
		add_deposit = d_assimilators_ruins
	}
	else_if = {
		limit = {
			owner = {
				is_machine_empire = yes
				NOT = { has_origin = origin_machine }
			}
		}

		add_deposit = d_strip_mine
		add_deposit = d_machine_empire_ruins
	}
	else_if = { # "Unplugged"
		limit = {
			owner = {
				has_origin = origin_unplugged
			}
		}

		add_deposit = d_decrepit_dwellings
		add_deposit = d_strip_mine
		add_deposit = d_former_battlefield
	}
	else_if = {
		limit = {
			owner = {
				NOR = {
					has_origin = origin_machine
					has_origin = origin_synthetic_fertility
					has_origin = origin_cosmic_dawn
					is_gestalt = yes
				}
			}
		}

		add_deposit = d_decrepit_dwellings
		add_deposit = d_failing_infrastructure
		add_deposit = d_failing_infrastructure
	}
	else_if = {
		limit = {
			owner = {
				is_hive_empire = yes
				NOT = { has_origin = origin_cosmic_dawn }
			}
		}

		add_deposit = d_collapsed_burrows
		add_deposit = d_failing_infrastructure
		add_deposit = d_failing_infrastructure
	}

	if = {
		limit = {
			has_carrier_flag = planet_unity
		}
		remove_deposit = d_failing_infrastructure
		add_deposit = d_forgotten_civilization
	}
	if = {
		limit = {
			owner = {
				is_lithoid_empire = yes
				NOR = {
					has_valid_civic = civic_hive_devouring_swarm
					has_origin = origin_lithoid
					has_origin = origin_broken_shackles
				}
			}
		}
		while = {
			count = 4
			add_deposit = d_hibernating_lithoids
		}
	}

	if = {
		limit = {
			owner = {
				is_lithoid_devouring_swarm = yes
			}
		}
		while = {
			count = 2
			add_deposit = d_hibernating_lithoids
		}
	}

	if = {
		limit = {
			owner = {
				is_apocalyptic_empire = yes
			}
		}

		# 3 + 3 energy
		add_deposit = d_hot_springs
		add_deposit = d_searing_desert
		add_deposit = d_underwater_vent

		# 3 + 4 mining
		add_deposit = d_veiny_cliffs
		add_deposit = d_ore_rich_caverns
		add_deposit = d_submerged_ore_veins

		# 3 + 2 farming
		add_deposit = d_lichen_fields
		add_deposit = d_fungal_caves
		add_deposit = d_fungal_forest
	}

	if = {
		limit = {
			owner = {
				has_origin = origin_life_seeded
			}
		}

		create_life_seeded_planet = yes
	}

	if = {
		limit = {
			is_planet_class = pc_continental
		}

		# 3 + 3 energy
		add_deposit = d_hot_springs
		add_deposit = d_rushing_waterfalls
		add_deposit = d_tempestous_mountain
		if = {
			limit = { owner = { is_machine_empire = yes } }
			add_deposit = d_rushing_waterfalls
		}
		if = {
			limit = { owner = { has_valid_civic = civic_machine_servitor } }
			add_deposit = d_hot_springs
		}

		# 3 + 3 mining
		add_deposit = d_veiny_cliffs
		add_deposit = d_prosperous_mesa
		add_deposit = d_rich_mountain

		# 4 + 3 farming (lithoids swap 3 for minerals)
		add_deposit = d_rugged_woods
		add_deposit = d_fertile_lands
		if = {
			limit = { owner = { is_machine_empire = no } }
			add_deposit = d_rugged_woods

			if = {
				limit = {
					owner = {
						is_lithoid_or_infernal_empire = yes
					}
				}
				add_deposit = d_rich_mountain
			}
			else = {
				add_deposit = d_black_soil
			}
		}
	}

	if = {
		limit = {
			is_planet_class = pc_tropical
		}

		# 3 + 3 energy
		add_deposit = d_hot_springs
		add_deposit = d_rushing_waterfalls
		add_deposit = d_tempestous_mountain
		if = {
			limit = { owner = { is_machine_empire = yes } }
			add_deposit = d_rushing_waterfalls
		}
		if = {
			limit = { owner = { has_valid_civic = civic_machine_servitor } }
			add_deposit = d_hot_springs
		}

		# 3 + 3 mining
		add_deposit = d_mineral_fields
		add_deposit = d_prosperous_mesa
		add_deposit = d_submerged_ore_veins

		# 4 + 3 farming (lithoids swap 3 for minerals)
		add_deposit = d_green_hills
		add_deposit = d_lush_jungle
		if = {
			limit = { owner = { is_machine_empire = no } }
			if = {
				limit = {
					owner = {
						is_lithoid_or_infernal_empire = yes
					}
				}
				add_deposit = d_submerged_ore_veins
			}
			else = {
				add_deposit = d_green_hills
				add_deposit = d_tropical_island
			}
		}
	}

	if = {
		limit = {
			is_planet_class = pc_ocean
		}

		# 3 + 3 energy
		add_deposit = d_hot_springs
		add_deposit = d_rushing_waterfalls
		add_deposit = d_underwater_vent
		if = {
			limit = { owner = { is_machine_empire = yes } }
			add_deposit = d_rushing_waterfalls
		}
		if = {
			limit = { owner = { has_valid_civic = civic_machine_servitor } }
			add_deposit = d_hot_springs
		}

		# 3 + 3 mining
		add_deposit = d_mineral_fields
		add_deposit = d_ore_rich_caverns
		add_deposit = d_submerged_ore_veins

		# 4 + 3 farming (lithoids swap 3 for minerals)
		add_deposit = d_green_hills
		add_deposit = d_lush_jungle
		if = {
			limit = { owner = { is_machine_empire = no } }
			add_deposit = d_green_hills

			if = {
				limit = {
					owner = {
						is_lithoid_or_infernal_empire = yes
					}
				}
				add_deposit = d_submerged_ore_veins
			}
			else = {
				add_deposit = d_teeming_reef
			}
		}
	}

	if = {
		limit = {
			is_planet_class = pc_arctic
		}

		# 3 + 3 energy
		add_deposit = d_hot_springs
		add_deposit = d_frozen_gas_lake
		add_deposit = d_geothermal_vent
		if = {
			limit = { owner = { is_machine_empire = yes } }
			add_deposit = d_frozen_gas_lake
		}
		if = {
			limit = { owner = { has_valid_civic = civic_machine_servitor } }
			add_deposit = d_hot_springs
		}

		# 3 + 3 mining
		add_deposit = d_veiny_cliffs
		add_deposit = d_ore_rich_caverns
		add_deposit = d_rich_mountain

		# 4 + 3 farming (lithoids swap 3 for minerals)
		add_deposit = d_lichen_fields
		add_deposit = d_fungal_caves
		if = {
			limit = { owner = { is_machine_empire = no } }
			add_deposit = d_lichen_fields

			if = {
				limit = {
					owner = {
						is_lithoid_or_infernal_empire = yes
					}
				}
				add_deposit = d_rich_mountain
			}
			else = {
				add_deposit = d_fungal_forest
			}
		}
	}

	if = {
		limit = {
			is_planet_class = pc_tundra
		}

		# 3 + 3 energy
		add_deposit = d_hot_springs
		add_deposit = d_frozen_gas_lake
		add_deposit = d_geothermal_vent
		if = {
			limit = { owner = { is_machine_empire = yes } }
			add_deposit = d_frozen_gas_lake
		}
		if = {
			limit = { owner = { has_valid_civic = civic_machine_servitor } }
			add_deposit = d_hot_springs
		}

		# 3 + 3 mining
		add_deposit = d_mineral_fields
		add_deposit = d_ore_rich_caverns
		add_deposit = d_rich_mountain

		# 4 + 3 farming (lithoids swap 3 for minerals)
		add_deposit = d_forgiving_tundra
		add_deposit = d_fungal_caves
		if = {
			limit = { owner = { is_machine_empire = no } }
			add_deposit = d_forgiving_tundra

			if = {
				limit = {
					owner = {
						is_lithoid_or_infernal_empire = yes
					}
				}
				add_deposit = d_rich_mountain
			}
			else = {
				add_deposit = d_fungal_forest
			}
		}
	}

	if = {
		limit = {
			is_planet_class = pc_alpine
		}

		# 3 + 3 energy
		add_deposit = d_hot_springs
		add_deposit = d_frozen_gas_lake
		add_deposit = d_tempestous_mountain
		if = {
			limit = { owner = { is_machine_empire = yes } }
			add_deposit = d_frozen_gas_lake
		}
		if = {
			limit = { owner = { has_valid_civic = civic_machine_servitor } }
			add_deposit = d_hot_springs
		}

		# 3 + 3 mining
		add_deposit = d_veiny_cliffs
		add_deposit = d_ore_rich_caverns
		add_deposit = d_rich_mountain

		# 4 + 3 farming (lithoids swap 3 for minerals)
		add_deposit = d_forgiving_tundra
		add_deposit = d_fungal_caves
		if = {
			limit = { owner = { is_machine_empire = no } }
			add_deposit = d_forgiving_tundra

			if = {
				limit = {
					owner = {
						is_lithoid_or_infernal_empire = yes
					}
				}
				add_deposit = d_rich_mountain
			}
			else = {
				add_deposit = d_fungal_forest
			}
		}
	}

	if = {
		limit = {
			is_planet_class = pc_desert
		}

		# 3 + 3 energy
		add_deposit = d_arid_highlands
		add_deposit = d_searing_desert
		add_deposit = d_geothermal_vent
		if = {
			limit = { owner = { is_machine_empire = yes } }
			add_deposit = d_searing_desert
		}
		if = {
			limit = { owner = { has_valid_civic = civic_machine_servitor } }
			add_deposit = d_arid_highlands
		}

		# 3 + 3 mining
		add_deposit = d_mineral_fields
		add_deposit = d_ore_rich_caverns
		add_deposit = d_rich_mountain

		# 4 + 3 farming (lithoids swap 3 for minerals)
		add_deposit = d_nutritious_mudland
		add_deposit = d_great_river
		if = {
			limit = { owner = { is_machine_empire = no } }
			add_deposit = d_nutritious_mudland

			if = {
				limit = {
					owner = {
						is_lithoid_or_infernal_empire = yes
					}
				}
				add_deposit = d_rich_mountain
			}
			else = {
				add_deposit = d_marvelous_oasis
			}
		}
		set_carrier_flag = tasty_desert
	}

	if = {
		limit = {
			is_planet_class = pc_arid
		}

		# 3 + 3 energy
		add_deposit = d_arid_highlands
		add_deposit = d_searing_desert
		add_deposit = d_geothermal_vent
		if = {
			limit = { owner = { is_machine_empire = yes } }
			add_deposit = d_searing_desert
		}
		if = {
			limit = { owner = { has_valid_civic = civic_machine_servitor } }
			add_deposit = d_arid_highlands
		}

		# 3 + 3 mining
		add_deposit = d_veiny_cliffs
		add_deposit = d_ore_rich_caverns
		add_deposit = d_rich_mountain

		# 4 + 3 farming (lithoids swap 3 for minerals)
		add_deposit = d_bountiful_plains
		add_deposit = d_great_river
		if = {
			limit = { owner = { is_machine_empire = no } }
			add_deposit = d_bountiful_plains

			if = {
				limit = {
					owner = {
						is_lithoid_or_infernal_empire = yes
					}
				}
				add_deposit = d_rich_mountain
			}
			else = {
				add_deposit = d_tropical_island
			}
		}
	}

	if = {
		limit = {
			is_planet_class = pc_savannah
		}

		# 3 + 3 energy
		add_deposit = d_arid_highlands
		add_deposit = d_searing_desert
		add_deposit = d_geothermal_vent
		if = {
			limit = { owner = { is_machine_empire = yes } }
			add_deposit = d_searing_desert
		}
		if = {
			limit = { owner = { has_valid_civic = civic_machine_servitor } }
			add_deposit = d_arid_highlands
		}

		# 3 + 3 mining
		add_deposit = d_mineral_fields
		add_deposit = d_prosperous_mesa
		add_deposit = d_submerged_ore_veins

		# 4 + 3 farming (lithoids swap 3 for minerals)
		add_deposit = d_bountiful_plains
		add_deposit = d_fertile_lands
		if = {
			limit = { owner = { is_machine_empire = no } }
			add_deposit = d_bountiful_plains

			if = {
				limit = {
					owner = {
						is_lithoid_or_infernal_empire = yes
					}
				}
				add_deposit = d_rich_mountain
			}
			else = {
				add_deposit = d_black_soil
			}
		}
	}

	if = {
		limit = {
			is_planet_class = pc_relic
			owner = {
				OR = {
					has_origin = origin_remnants
					has_origin = origin_slavers
				}
			}
		}

		set_planet_size = 22

		# 6 blockers
		add_deposit = d_ruined_arcology
		add_deposit = d_ruined_arcology
		add_deposit = d_ruined_arcology
		add_deposit = d_ruined_arcology
		add_deposit = d_ruined_arcology
		add_deposit = d_ruined_arcology

		# 3 + 3 energy
		add_deposit = d_arid_highlands
		add_deposit = d_searing_desert
		add_deposit = d_geothermal_vent
		if = {
			limit = { owner = { is_machine_empire = yes } }
			add_deposit = d_searing_desert
		}
		if = {
			limit = { owner = { has_valid_civic = civic_machine_servitor } }
			add_deposit = d_arid_highlands
		}

		# 3 + 3 mining
		add_deposit = d_mineral_fields
		add_deposit = d_prosperous_mesa
		add_deposit = d_submerged_ore_veins

		# 4 + 3 farming
		add_deposit = d_bountiful_plains
		add_deposit = d_fertile_lands
		if = {
			limit = { owner = { is_machine_empire = no } }
			add_deposit = d_bountiful_plains
			add_deposit = d_black_soil
		}
	}

	if = {
		limit = {
			is_planet_class = pc_volcanic
		}

		# Mining - 9 total
		# +2 each, RL so not all homeworlds are the same
		while = {
			count = 2
			random_list = {
				1 = { add_deposit = d_volcanic_rich_mountain }
				1 = { add_deposit = d_volcanic_mineral_fields }
				1 = { add_deposit = d_volcanic_mineral_layers }
				1 = { add_deposit = d_volcanic_weak_crust }
			}
		}
		# +1 each
		add_deposit = d_volcanic_ore_veins
		add_deposit = d_volcanic_mineral_hills
		# +3
		add_deposit = d_volcanic_lava_river

		# Polytechnic - 6 total
		# +2 each, RL so not all homeworlds are the same
		while = {
			count = 3
			random_list = {
				1 = { add_deposit = d_volcanic_stable_plateaus }
				1 = { add_deposit = d_volcanic_tranquil_flatlands }
				1 = { add_deposit = d_volcanic_intraplate }
				1 = { add_deposit = d_volcanic_basalt_plains }
			}
		}
	}

	# Ocean Paradise Origin

	if = {
		limit = {
			owner = {
				OR  = {
					has_origin = origin_ocean_paradise
					has_origin = origin_ocean_machines
				}
			}
		}
		create_ocean_paradise_planet = yes
	}

	# Wilderness Origin

	if = {
		limit = {
			owner = { is_wilderness_empire = yes }
		}
		create_wilderness_planet = yes
	}
}

# Special for Earth
generate_earth_deposits_and_blockers = {
	optimize_memory
	clear_deposits = yes
	add_deposit = d_mesopotamian_urban_corridor
	add_deposit = d_boswash_metropolitan_axis
	add_deposit = d_mauritanian_security_zone
	add_deposit = d_great_albertan_crater
	add_deposit = d_scandinavian_reclamation_sector
	add_deposit = d_saharan_irrigation_project
	add_deposit = d_pearl_river_agglomerate
	add_deposit = d_great_pacific_garbage_patch
	if = {
		limit = {
			owner = { is_machine_empire = yes }
		}
		add_deposit = d_rushing_waterfalls
	}

	if = {
		limit = {
			owner = {
				is_environmental_architects_empire = yes
			}
		}
		add_random_natural_blocker_and_give_tech = yes

		if = {
			limit = {
				owner = { has_valid_civic = civic_machine_servitor }
			}
			add_deposit = d_unsupervised_settlement
		}
	}
	else = {
		add_deposit = d_failing_infrastructure_earth
		if = {
			limit = {
				owner = { has_valid_civic = civic_machine_servitor }
			}
			add_deposit = d_hot_springs
			add_deposit = d_strip_mine
			add_deposit = d_unsupervised_settlement
		}
		else_if = {
			limit = {
				owner = {
					is_hive_empire = yes
				}
			}

			add_deposit = d_collapsed_burrows
		}
		else = {
			add_deposit = d_decrepit_dwellings
		}
	}

	if = {
		limit = {
			owner = {
				is_lithoid_empire = yes
				NOT = { has_valid_civic = civic_hive_devouring_swarm }
			}
		}
		while = {
			count = 4
			add_deposit = d_hibernating_lithoids
		}
	}

	if = {
		limit = {
			owner = {
				is_lithoid_devouring_swarm = yes
			}
		}
		while = {
			count = 2
			add_deposit = d_hibernating_lithoids
		}
	}

	if = {
		limit = {
			owner = {
				has_origin = origin_life_seeded
			}
		}

		set_planet_size = 30

		add_deposit = d_dust_caverns
		add_deposit = d_bubbling_swamp
		add_deposit = d_crystalline_caverns
	}

	if = {
		limit = {
			is_planet_class = pc_desert # Just in case players cause trouble
		}
		set_carrier_flag = tasty_desert
	}
}

generate_start_buildings_and_districts = {
	optimize_memory
	set_timed_carrier_flag = {
		flag = ignore_ai_building_limitations
		days = 1
	}
	inline_script = "game_start/rural_districts_initializer"

	# Non-Nomadic
	inline_script = "game_start/authority_regular"
	inline_script = "game_start/authority_hive"
	inline_script = "game_start/authority_machine"
	# Nomadic
	inline_script = "game_start/authority_regular_nomad"
	inline_script = "game_start/authority_hive_nomad"
	inline_script = "game_start/authority_machine_nomad"

	## End of Default
	## Start of Unique District Setup

	## Species
	inline_script = "game_start/species_lithoids"
	inline_script = "game_start/species_infernals"
	inline_script = "game_start/species_individual_machine"
	inline_script = "game_start/species_subspecies" ## Set subspecies flags

	# Shipsets
	inline_script = "game_start/shipsets"

	## Ethic
	inline_script = "game_start/ethic_spiritualist"
	inline_script = "game_start/ethic_materialist"
	inline_script = "game_start/ethic_pacifist"
	inline_script = "game_start/ethic_militarist"
	inline_script = "game_start/ethic_egalitarian"
	inline_script = "game_start/ethic_authoritarian"
	inline_script = "game_start/ethic_xenophile"
	inline_script = "game_start/ethic_xenophobe"

	# BioGenesis
	inline_script = "game_start/origin_wilderness"

	## Origin, default origin first, then listed in release order (newest first)
	# Default
	inline_script = "game_start/origin_default"
	# Forever Cruise
	inline_script = "game_start/origin_forever_cruise"
	# Sacred Path
	inline_script = "game_start/origin_sacred_path"
	# Heirs of the Khan
	inline_script = "game_start/origin_heirs_of_the_khan"
	# Shadows of the Shroud
	inline_script = "game_start/origin_shroud_forged"
	inline_script = "game_start/origin_mindwarden"
	# Grand Archive
	inline_script = "game_start/origin_primal_calling"
	# Machine Age
	inline_script = "game_start/origin_cybernetic_creed"
	inline_script = "game_start/origin_synthetic_fertility"
	inline_script = "game_start/origin_arc_welders"
	# First Contact
	inline_script = "game_start/origin_payback"
	inline_script = "game_start/origin_broken_shackles"
	# Toxoids
	inline_script = "game_start/origin_toxic_knights"
	# Overlord
	inline_script = "game_start/origin_progenitor_hive"
	inline_script = "game_start/origin_subterranean"
	# Aquatics
	inline_script = "game_start/origin_ocean_paradise"
	# Necroids
	inline_script = "game_start/origin_necrophage"
	# Federations
	inline_script = "game_start/origin_shattered_ring"
	inline_script = "game_start/origin_remnants"
	## for Void Dwellers see void_dweller_home_planet_setup
	# Humanoids
	inline_script = "game_start/origin_clone_army"
	# Synthetic Dawn
	inline_script = "game_start/origin_machine"
	# Utopia
	inline_script = "game_start/origin_mechanist"

	##Civics
	inline_script = "game_start/civics"

	# Tankbound civic (removes every farming districts so we want to do that at the end)
	inline_script = "game_start/civic_tankbound"

	# Volcanic Worlds use a different district_set and deposits
	# Keep this above game_start/rural_districts_placement
	if = {
		limit = { uses_district_set = volcanic_world }
		inline_script = "game_start/volcanic_world_setup"
	}
	# Apply rural districts depending on variable count
	inline_script = "game_start/rural_districts_placement"

	give_starting_resources_effect = yes
	validate_planet_buildings_and_districts = yes
}

add_building_if_not_present = {
	if = {
		limit = {
			NOT = {
				has_building = $BUILDING$
			}
		}
		add_building = $BUILDING$
	}
}

generate_start_pops = {
	optimize_memory
	# SECONDARY SPECIES STARTS: add Traits, set living standards, etc.
	if = {
		limit = {
			owner = {
				OR = {
					has_origin = origin_syncretic_evolution
					has_origin = origin_necrophage
					has_origin = origin_forever_cruise
					has_valid_civic = civic_machine_servitor
					has_valid_civic = civic_machine_assimilator
					has_valid_civic = civic_hive_bodysnatcher
				}
			}
		}

		if = { # Machines: Add 'Machine' Trait
			limit = {
				last_created_species = { is_species_class = MACHINE }
			}
			modify_species = {
				species = last_created_species
				add_trait = trait_machine_unit
				add_traits_at_start_of_list = yes
			}
			last_created_species = {
				save_event_target_as = last_created_species
			}
		}

		# Syncretic Evolution & Necrophage: set flag & citizenship type; (Necrophage) spawn prepatent Pops
		if = {
			limit = {
				owner = {
					OR = {
						has_origin = origin_syncretic_evolution
						has_origin = origin_necrophage
					}
				}
			}

			last_created_species = {
				set_species_flag = syncretic_species@root.owner
				if = {
					limit = {
						prev.owner = {
							has_origin = origin_syncretic_evolution
						}
					}
					set_citizenship_type = {
						country = root.owner
						type = citizenship_slavery
					}
				}
			}
			if = {
				limit = {
					owner = {
						has_origin = origin_necrophage
						OR = {
							has_valid_civic = civic_fanatic_purifiers
							has_valid_civic = civic_hive_devouring_swarm
							is_gestalt = yes
						}
					}
				}
				create_pop_group = {
					size = 1200
					species = last_created_species
				}
			}
			else = {
				create_pop_group = {
					size = 2400
					species = last_created_species
				}
			}
		}
		# Forever Cruise: set citizenship type
		# Both primary and secondary species are already generated by this point:
		#	owner_main_species - primary species
		#	last_created_species - secondary species
		if = {
			limit = {
				owner = { has_origin = origin_forever_cruise }
			}
			owner_main_species = {
				set_citizenship_type = {
					country = prev.owner
					type = citizenship_cruise_crew
				}
				set_species_flag = cruise_founding_crew
				save_global_event_target_as = cruise_crew_species@prev.owner
			}
		}
		generate_civic_secondary_pops = yes
		make_nascent_starting_pops = yes
	}

	# Mechanist: spawn robot Pops; add Job-specific Traits; spawn Robot Assembly Plant
	if = {
		limit = {
			owner = { has_origin = origin_mechanists }
		}
		owner = {
			create_built_robot_species = yes
		}
		last_created_species = {
			change_species_characteristics = {
				add_trait = trait_auto_mod_robotic
			}
		}
		create_pop_group = {
			size = 800
			species = last_created_species
		}
		while = {
			limit = { pop_amount < 2700 }
			create_pop_group = {
				species = owner_main_species
			}
		}
	}

	# MISC: spawn Pops
	if = {
		limit = {
			owner = {
				is_hive_empire = yes
				is_wilderness_empire = no
			}
		}
		while = {
			limit = { pop_amount < 3300 }
			create_pop_group = {
				species = owner_main_species
			}
		}
	}
	if = {
		limit = {
			owner = { has_valid_civic = civic_machine_servitor }
		}
		while = {
			limit = { pop_amount < 4800 }	# Includes the 2,000 Bio-Trophies that were already created.
			create_pop_group = {
				species = owner_main_species
			}
		}
	}
	if = {
		limit = { owner = { has_origin = origin_shroud_forged } }
		while = {
			limit = { pop_amount < 1900 }
			create_pop_group = {
				species = owner_main_species
				ethos = random
			}
		}
		owner_main_species = {
			drone_conversion_new_species_effect = yes
			set_species_flag = original_main_species # After conversion effect, so it's the only species with the flag
		}
		while = {
			limit = { pop_amount < 2900 }
			create_pop_group = {
				species = event_target:secondary_main_species
				category = complex_drone
			}
		}
	}
	if = {
		limit = { owner = { is_machine_empire = yes } }
		while = {
			limit = { pop_amount < 2800 }
			create_pop_group = {
				species = owner_main_species
			}
		}
	}
	if = {
		limit = {
			owner = {
				is_megacorp = yes
				has_reduced_pop_origin = no
			}
		}
		while = {
			limit = { pop_amount < 2800 }
			create_pop_group = {
				species = owner_main_species
			}
		}
	}
	if = {
		limit = {
			owner = { has_origin = origin_broken_shackles }
			pop_amount < 600
		}

		while = {
			limit = { pop_amount < 600 }
			create_pop_group = {
				species = owner_main_species
			}
		}
	}
	if = {
		limit = {
			owner = {
				is_regular_empire = yes
				has_reduced_pop_origin = no
			}
		}
		while = {
			limit = { pop_amount < 2800 }
			create_pop_group = {
				species = owner_main_species
			}
		}
	}
	if = {
		limit = {
			owner = { has_origin = origin_payback }
			pop_amount < 1800
		}
		while = {
			limit = { pop_amount < 1800 }
			create_pop_group = {
				species = owner_main_species
			}
		}
	}
	if = {
		limit = {
			owner = { has_origin = origin_fear_of_the_dark }
		}
		while = {
			limit = { pop_amount < 2000 }
			create_pop_group = {
				species = owner_main_species
			}
		}
	}
	if = {
		limit = {
			owner = { has_origin = origin_cosmic_dawn }
		}
		while = {
			limit = { pop_amount < 2200 }
			create_pop_group = {
				species = owner_main_species
			}
		}
	}
	if = {
		limit = {
			owner = { has_origin = origin_default }
		}
		create_pop_group = {
			size = 400
			species = owner_main_species
		}
	}
	if = {
		limit = {
			owner = {
				is_eager_explorer_empire = yes
			}
		}
		while = {
			count = 10
			random_owned_pop_group = {
				limit = {
					is_same_species = owner.species
				}
				kill_single_pop = yes
			}
		}
	}

	if = {
		limit = {
			owner? = {
				has_origin = origin_toxic_knights
			}
		}
		while = {
			count = 11
			random_owned_pop_group = { kill_single_pop = yes }
		}
	}

	# Nascent Stage pops being generated
	make_nascent_starting_pops = yes

	# Permanent Employment: add Zombie Trait
	if = {
		limit = {
			owner = {
				has_civic = civic_permanent_employment
			}
		}
		while = {
			count = 4
			random_owned_pop_group = { kill_single_pop = yes }
		}
		create_zombie_pop_group = {
			SIZE = 400
		}
	}

	# Civilians
	if = {
		limit = {
			owner =  {
				NOR = {
					has_origin = origin_broken_shackles
					is_wilderness_empire = yes
					is_tankbound_empire = yes
					has_valid_civic = civic_machine_servitor
				}
			}
		}
		while = {
			count = @civilians_counter
			if = {
				limit = {
					owner = { is_gestalt = no }
				}
				create_pop_group = {
					size = @civilians_gamestart
					species = owner_main_species
					category = civilian
				}
			}
			else = {
				create_pop_group = {
					size = @civilians_gamestart
					species = owner_main_species
					category = maintenance_drone
				}
			}
		}
	}

	# Wilderness
	if = {
		limit = {
			owner = { is_wilderness_empire = yes }
		}
		while = {
			limit = { pop_amount < 2000 }
			create_pop_group = {
				species = owner_main_species
			}
		}
	}

	#Synthetic Fertility
	if = {
		limit = {
			owner = { has_origin = origin_synthetic_fertility }
		}
		create_pop_group = {
			size = 900
			species = owner_main_species
		}
	}

	#Deep Sleep
	if = {
		limit = {
			owner = { is_deep_sleep_country = yes }
		}
		create_pop_group = {
			size = 3000
			species = owner_main_species
		}
	}
}

generate_civic_secondary_pops = {
	# Ensuring that this country flag has been set by this point
	if = {
		limit = {
			owner = {
				OR = {
					last_created_species = { is_lithoid = yes }
					last_created_species = { has_trait = trait_machine_unit }
				}
			}
		}
		owner = {
			set_country_flag = lithoid_subspecies
		}
	}
	else_if = {
		limit = {
			owner = { last_created_species = { is_infernal = yes } }
		}
		owner = {
			set_country_flag = infernal_subspecies
		}
	}
	else = {
		owner = {
			set_country_flag = non_lithoid_subspecies
		}
	}
	# Rogue Servitor & Driven Assimilator setting up pop special traits to make sure that their secondary species have all the cool traits. Spawning of the pops happens next.
	if = {
		limit = {
			owner = {
				OR = {
					has_valid_civic = civic_machine_assimilator
					has_valid_civic = civic_machine_servitor
				}
				OR = {
					has_origin = origin_void_machines
					has_origin = origin_post_apocalyptic_machines
					has_origin = origin_subterranean_machines
				}
			}
		}
		owner = {
			switch = {
				trigger = has_origin
				origin_void_machines = {
					last_created_species = {
						modify_species = {
							species = this
							add_trait = trait_void_dweller_1
							add_traits_at_start_of_list = yes
							ideal_planet_class = pc_habitat
						}
					}
				}
				origin_post_apocalyptic_machines = {
					last_created_species = {
						change_species_characteristics = {
							add_trait = trait_survivor
							add_traits_at_start_of_list = yes
						}
					}
				}
				origin_subterranean_machines = {
					last_created_species = {
						change_species_characteristics = {
							add_trait = trait_cave_dweller
							add_traits_at_start_of_list = yes
						}
					}
				}
			}
		}
	}
	else_if = {
		limit = {
			owner = {
				has_valid_civic = civic_hive_bodysnatcher
				OR = {
					has_origin = origin_void_dwellers
					has_origin = origin_post_apocalyptic
					has_origin = origin_subterranean
					has_origin = origin_wilderness
				}
			}
		}
		owner = {
			switch = {
				trigger = has_origin
				origin_void_dwellers = {
					last_created_species = {
						modify_species = {
							species = this
							add_trait = trait_void_dweller_1
							add_traits_at_start_of_list = yes
							ideal_planet_class = pc_habitat
						}
					}
				}
				origin_post_apocalyptic = {
					last_created_species = {
						change_species_characteristics = {
							add_trait = trait_survivor
							add_traits_at_start_of_list = yes
						}
					}
				}
				origin_subterranean = {
					last_created_species = {
						change_species_characteristics = {
							add_trait = trait_cave_dweller
							add_traits_at_start_of_list = yes
						}
					}
				}
				origin_wilderness = {
					last_created_species = {
						change_species_characteristics = {
							add_trait = trait_wilderness
							add_traits_at_start_of_list = yes
						}
					}
				}
			}
		}
	}
	# Rogue Servitor: set citizenship, living standard & preference; spawn Pops
	if = {
		limit = {
			owner = { has_valid_civic = civic_machine_servitor }
		}
		last_created_species = {
			if = {
				limit = {
					prev = {
						OR = {
							is_planet_class = pc_shattered_ring_habitable
							is_planet_class = pc_gaia
						}
					}
				}
				modify_species = {
					species = this
					ideal_planet_class = home_planet
				}
			}
			set_citizenship_type = {
				country = prev.owner
				type = citizenship_limited
			}
			set_living_standard = {
				country = prev.owner
				type = living_standard_organic_trophy
			}
		}
		while = {
			count = @civilians_counter
			create_pop_group = {
				size = @civilians_gamestart
				species = last_created_species
			}
		}
	}

	# Driven Assimilator and Bodysnatcher set citizenship, living standard & preference; spawn Pops
	if = {
		limit = {
			owner = {
				OR = {
					has_valid_civic = civic_machine_assimilator
					has_valid_civic = civic_hive_bodysnatcher
				}
			}
		}
		last_created_species = {
			if = {
				limit = {
					prev = {
						OR = {
							is_planet_class = pc_shattered_ring_habitable
							is_planet_class = pc_gaia
						}
					}
				}
				modify_species = {
					species = this
					ideal_planet_class = home_planet
				}
			}
			if = {
				limit = {
					prev.owner = { is_machine_empire = yes }
				}
				set_citizenship_type = {
					country = prev.owner
					type = citizenship_full_machine
				}
			}
			else = {
				set_citizenship_type = {
					country = prev.owner
					type = citizenship_full
				}
			}

			set_living_standard = {
				country = prev.owner
				type = living_standard_hive_mind
			}
		}
		while = {
			count = @civilians_counter
			create_pop_group = {
				size = @civilians_gamestart
				species = last_created_species
			}
		}
	}

	# Forever Cruise: set citizenship, living standard & preference; spawn Pops
	if = {
		limit = {
			owner = { has_origin = origin_forever_cruise }
		}
		if = {
			limit = {
				owner = { has_valid_civic = civic_machine_servitor }
			}
			# Pops were already spawned by the Rogue Servitor block above; only re-tag here.
			last_created_species = {
				set_citizenship_type = {
					country = prev.owner
					type = citizenship_organic_trophy
				}
				set_species_flag = cruise_founding_passenger
				save_global_event_target_as = cruise_passenger_species@prev.owner
			}
		}
		else = {
			last_created_species = {
				set_citizenship_type = {
					country = prev.owner
					type = citizenship_cruise_passenger
				}
				set_living_standard = {
					country = prev.owner
					type = living_standard_cruise_passenger
				}
				set_population_controls = {
					country = prev.owner
					type = population_control_no
					cooldown = no
				}
				set_species_flag = cruise_founding_passenger
				save_global_event_target_as = cruise_passenger_species@prev.owner
			}
			while = {
				count = @civilians_counter
				create_pop_group = {
					size = @civilians_gamestart
					species = last_created_species
				}
			}
		}
	}
}

generate_home_planet_final_pass = {
	optimize_memory
	# Adjust life-seeded colonies
	if = {
		limit = {
			owner = {
				has_origin = origin_life_seeded
			}
		}

		owner = {
			# Nuke other colonies gained from advanced empire, etc
			every_owned_planet = {
				limit = { is_capital = no }
				remove_all_buildings = yes
				destroy_colony = yes
			}
			if = {
				limit = {
					NOR = {
						has_valid_civic = civic_permanent_employment
						has_valid_civic = civic_machine_servitor
						has_valid_civic = civic_machine_assimilator
						has_valid_civic = civic_hive_bodysnatcher
						species = { has_trait = trait_nascent_stage }
					}
				}
				change_dominant_species = { species = last_created_species change_all = yes }
			}
		}
	}
	if = {
		limit = {
			owner = {
				OR = {
					has_origin = origin_shattered_ring
					has_origin = origin_machine
				}
			}
		}
		owner = {
			capital_scope = {
				system_star = {
					set_deposit = d_energy_10
				}
			}
		}
	}
	if = {
		limit = {
			owner = {
				has_origin = origin_tree_of_life
			}
		}
		owner = {
			capital_scope = {
				add_deposit = d_tree_of_life_home
			}
		}
	}
	if = {
		limit = {
			owner = {
				has_origin = origin_lithoid
			}
		}
		owner = {
			capital_scope = {
				add_deposit = d_lithoid_crater
				add_deposit = d_buried_lithoids
				add_deposit = d_buried_lithoids
				add_deposit = d_buried_lithoids
				add_deposit = d_buried_lithoids
			}
		}
	}
	if = {
		limit = {
			owner = {
				has_origin = origin_lost_colony
				is_gestalt = no
			}
		}
		owner = {
			capital_scope = {
				add_modifier = { modifier = colonial_spirit_mod }
			}
		}
	}
	else_if = {
		limit = {
			owner = {
				has_origin = origin_lost_colony
				is_gestalt = yes
			}
		}
		owner = {
			capital_scope = {
				add_modifier = { modifier = colonial_spirit_gestalt_mod }
			}
		}
	}
	if = {
		limit = {
			owner = {
				has_origin = origin_progenitor_hive
			}
		}
		owner = {
			capital_scope = {
				add_deposit = d_progenitor
			}
		}
	}
	if = {
		limit = {
			owner = {
				has_origin = origin_default
			}
		}
		owner = {
			if = {
				limit = {
					is_gestalt = no
				}
				capital_scope = {
					add_modifier = { modifier = prosp_uni_mod days = 7200 }
				}
			}
			else = {
				capital_scope = {
					add_modifier = { modifier = prosp_uni_mod_gestalt days = 7200 }
				}
			}
		}
	}
	if = {
		limit = {
			owner = {
				has_origin = origin_machine
			}
		}
		owner = {
			capital_scope = {
				add_deposit = d_machine_minerals
				solar_system = {
					every_system_planet = {
						limit = {
							colonizable_planet = no
							is_star = no
						}
						clear_deposits = yes
					}
					every_system_planet = {
						limit = {
							is_asteroid = yes
						}
						remove_planet = yes
					}
					every_system_planet = {
						limit = {
							colonizable_planet = no
							is_star = no
							is_asteroid = no
							NOT = { is_planet_class = pc_gas_giant }
						}
						change_pc = pc_broken
					}
					every_system_planet = {
						limit = {
							colonizable_planet = no
							is_star = no
							is_planet_class = pc_gas_giant
						}
						change_pc = pc_molten
						add_modifier = { modifier = chthonian_planet }
						every_moon = {
							limit = {
								colonizable_planet = no
							}
							remove_planet = yes
						}
						while = {
							limit = {
								planet_size >= 20
							}
							change_planet_size = -6
						}
					}
				}
			}
		}
	}
	if = {
		limit = {
			owner = {
				any_owned_pop_group = {
					has_trait = trait_exotic_metabolism
				}
			}
		}

		add_deposit = d_exotic_mountain
	}

}

### Origins

void_dweller_home_planet_setup = {
	optimize_memory
	planet = {
		set_carrier_flag = habitat_level_2
		set_timed_carrier_flag = {
			flag = ignore_ai_building_limitations
			days = 1
		}
	}
	owner = {
		save_event_target_as = void_dweller_owner
		give_technology = {
			tech = tech_habitat_1
			message = no
		}
		add_research_option = tech_habitat_2
	}
	solar_system = {
		random_system_planet = {
			limit = { has_planet_flag = habitat_1_planet }
			save_event_target_as = habitat_1_planet
			solar_system = {
				spawn_orbital_effect = {
					TYPE = major
					HABITAT_OWNER = event_target:void_dweller_owner
					TARGET_PLANET = event_target:habitat_1_planet
				}
			}
		}
		random_system_planet = {
			limit = { has_planet_flag = habitat_2_planet }
			save_event_target_as = habitat_2_planet
			solar_system = {
				spawn_orbital_effect = {
					TYPE = major
					HABITAT_OWNER = event_target:void_dweller_owner
					TARGET_PLANET = event_target:habitat_2_planet
				}
			}
		}
		random_system_planet = {
			limit = { has_planet_flag = habitat_3_planet }
			save_event_target_as = habitat_3_planet
			solar_system = {
				spawn_orbital_effect = {
					TYPE = major
					HABITAT_OWNER = event_target:void_dweller_owner
					TARGET_PLANET = event_target:habitat_3_planet
				}
			}
		}
		if = {
			limit = {
				event_target:void_dweller_owner = {
					is_lithoid_or_infernal_empire = yes
				}
			}
			random_system_planet = {
				limit = {
					has_deposit = no
					can_have_mineral_deposits = yes
				}
				add_deposit = d_minerals_4
				set_planet_flag = mining_planet
			}
		}
		every_system_planet = {
			limit = { has_planet_flag = mining_planet }
			create_mining_station = { owner = event_target:void_dweller_owner }
		}
		# Change starting pre-scripted ideal into inhabitable worlds
		# First pre-sripted is a research system
		closest_system = {
			limit = { has_star_flag = neighbor_t1_first_colony }
			set_star_flag = ideal_habitat_t1
			max_steps = 3
			convert_to_research_system_effect = yes
		}
		# Second pre-sripted is a resource system
		closest_system = {
			limit = { has_star_flag = neighbor_t2_second_colony }
			set_star_flag = ideal_habitat_t2
			max_steps = 6
			convert_to_resource_system_effect = yes
		}
	}
	event_target:habitat_1_planet = {
		set_planet_flag = habitat@PREV
	}
	owner = {
		if = {
			limit = {
				OR = {
					AND = {
						is_lithoid_empire = no
						is_mechanical_species = no
						is_infernal_empire = no
					}
					is_catalytic_empire = yes
					country_uses_bio_ships = yes
				}
			}
			root.solar_system.starbase = {
				remove_starbase_building = {
					slot = 1
				}
				set_starbase_building = {
					slot = 1
					building = hydroponics_bay
				}
			}
		}
		if = {
			limit = {
				has_valid_civic = civic_private_healthcare_corporate
			}
			give_technology = { message = no tech = tech_frontier_health }
			add_research_option = tech_subdermal_stimulation
		}
	}

	if = { # First we generate pops for empires without strange pop starts
		limit = {
			owner = {
				NOR = {
					is_eager_explorer_empire = yes
					has_valid_civic = civic_machine_servitor
					has_valid_civic = civic_machine_assimilator
					has_valid_civic = civic_hive_bodysnatcher
				}
			}
		}
		if = {
			limit = { owner = { is_tankbound_empire = yes } }
			create_pop_group = {
				size = 800
				species = owner_main_species
			}
		}
		else = {
			create_pop_group = {
				size = 2800
				species = owner_main_species
			}
		}
	}
	else_if = { # Then we generate pops for eager explorer empires
		limit = {
			owner = {
				is_eager_explorer_empire = yes
			}
		}
		if = { #if they have a secondary species spawn that now
			limit = {
				owner = {
					OR = {
						has_valid_civic = civic_machine_servitor
						has_valid_civic = civic_machine_assimilator
						has_valid_civic = civic_hive_bodysnatcher
					}
				}
			}
			generate_civic_secondary_pops = yes
		}
		while = {
			limit = { pop_amount < 1800 }
			create_pop_group = {
				species = owner_main_species
			}
		}
	}
	else = { # Now we should only have secondary species generating empires left in this chain so spawn the rest
		generate_civic_secondary_pops = yes
		while = {
			limit = { pop_amount < 2800 }
			create_pop_group = {
				species = owner_main_species
			}
		}
	}
	if = {
		limit = {
			owner = {
				is_hive_empire = yes
			}
		}
		create_pop_group = {
			size = 500
			species = owner_main_species
		}
	}

	# Civilians
	while = {
		count = @civilians_counter
		create_pop_group = {
			size = @civilians_gamestart
			species = owner_main_species
		}
	}

	if = {
		limit = {
			owner = {
				has_valid_civic = civic_permanent_employment
			}
		}
		while = {
			count = 4
			weighted_random_owned_pop_group = {
				limit = {
					is_pop_category = worker
					is_robotic_species = no
				}
				modify_species = {
					species = this
					add_trait = trait_zombie
					add_traits_at_start_of_list = yes
				}
				if = {
					limit = {
						has_trait = trait_syncretic_proles
					}
					modify_species = {
						species = this
						remove_trait = trait_syncretic_proles
					}
				}
			}
		}
	}

	planet = {
		set_planet_entity = {
			entity = "habitat_phase_03_entity"
			graphical_culture = owner
		}
	}

	inline_script = "game_start/rural_districts_initializer"

	add_building = building_hab_major_capital

	add_district_and_planet_size_if_needed_effect_amount = {
		amount = 2
		district = district_hab_housing
	}

	if = {
		limit = {
			owner = {
				OR = {
					is_regular_empire = yes
					has_valid_civic = civic_machine_servitor
					has_valid_civic = civic_machine_obsessional_directive
				}
			}
		}
		add_zone = {
			district = district_hab_housing
			zone = zone_industrial
			zone_slot = 2
		}
		add_building = {
			building = building_factory_1
			zone = zone_industrial
		}
	}
	else = {
		add_zone = {
			district = district_hab_housing
			zone = zone_foundry
			zone_slot = 2
		}
	}

	add_district_and_planet_size_if_needed_effect_amount = {
		amount = 1
		district = district_hab_science
	}

	add_zone = {
		district = district_hab_housing
		zone = zone_habitat_research_unity
		zone_slot = 1
	}

	# Shipsets
	inline_script = "game_start/shipsets"

	# Species
	inline_script = "game_start/species_lithoids"
	inline_script = "game_start/species_infernals"
	inline_script = "game_start/species_hive_void_dwellers"
	inline_script = "game_start/species_machine_void_dwellers"
	inline_script = "game_start/species_individual_machine_void_dwellers"
	inline_script = "game_start/species_subspecies" ## Set subspecies flags

	## Ethic
	inline_script = "game_start/ethic_spiritualist"
	inline_script = "game_start/ethic_materialist"
	inline_script = "game_start/ethic_pacifist"
	inline_script = "game_start/ethic_militarist"
	inline_script = "game_start/ethic_egalitarian"
	inline_script = "game_start/ethic_authoritarian"
	inline_script = "game_start/ethic_xenophile"
	inline_script = "game_start/ethic_xenophobe"

	##Civics
	inline_script = "game_start/civics"

	# Tankbound civic (removes every farming districts so we want to do that at the end)
	inline_script = "game_start/civic_tankbound"

	# Apply rural districts depending on variable count
	inline_script = "game_start/rural_districts_placement"

	set_planet_size = 6

	give_starting_resources_effect = yes
}

advanced_empire_base_init_effect = {
	# scope: country
	set_country_flag = is_advanced_start_AI

	give_technology = { tech = tech_galactic_ambitions		message = no }
	give_technology = { tech = tech_mining_1				message = no }
	give_technology = { tech = tech_combat_computers_1		message = no }
	give_technology = { tech = tech_planetary_unification	message = no }

	add_resource = { energy		= 1000 }
	add_resource = { minerals	= 1000 }
	add_resource = { alloys		= 500 }
	add_resource = { influence	= 200 }
	add_monthly_resource_mult = {
		resource = unity
		value = @tier2unityreward
		min = @tier2unitymin
		max = @tier2unitymax
	}
	if = {
		limit = {
			OR = {
				AND = {
					is_lithoid_empire = no
					is_machine_empire = no
				}
				is_catalytic_empire = yes
				has_country_flag = non_lithoid_subspecies
				has_origin = origin_broken_shackles
			}
		}
		add_resource = { food = 1000 }
	}
	else = {
		add_resource = { energy = 1000 }
	}
	if = {
		limit = {
			country_uses_consumer_goods_game_start = yes
		}
		add_resource = { consumer_goods = 500 }
	}
	else = {
		add_resource = { minerals = 500 }
	}
}

add_advanced_empire_capital_pops_effect = {
	# scope: colony
	if = {
		limit = {
			owner = {
				is_machine_empire = yes
			}
		}
		while = {
			count = 13
			create_pop_group = {
				species = owner_main_species
			}
		}
	}
	else_if = {
		limit = {
			owner = {
				is_hive_empire = yes
			}
		}
		while = {
			count = 16
			create_pop_group = {
				species = owner_main_species
			}
		}
	}
	else_if = {
		limit = {
			owner = {
				has_origin = origin_mechanists
			}
		}
		random_galaxy_species = {
			limit = {
				has_species_flag = mechanical_species@root.owner
			}
			save_event_target_as = mechanical_species
		}
		while = {
			count = 6
			create_pop_group = {
				species = event_target:mechanical_species
			}
		}
		while = {
			count = 12
			create_pop_group = {
				species = owner_main_species
			}
		}
	}
	else_if = {
		limit = {
			owner = {
				OR = {
					has_origin = origin_syncretic_evolution
					has_origin = origin_necrophage
				}
			}
		}
		random_galaxy_species = {
			limit = {
				has_species_flag = syncretic_species@root.owner
			}
			save_event_target_as = syncretic_species
		}
		while = {
			count = 8
			create_pop_group = {
				species = event_target:syncretic_species
			}
		}
		while = {
			count = 10
			create_pop_group = {
				species = owner_main_species
			}
		}
	}
	else = {
		while = {
			count = 18
			create_pop_group = {
				species = owner_main_species
			}
		}
	}
}

add_advanced_empire_colony_pops_effect = {
	# scope: colony
	if = {
		limit = {
			owner = {
				is_machine_empire = yes
			}
		}
		while = {
			count = 15
			create_pop_group = {
				species = owner_main_species
			}
		}
	}
	else_if = {
		limit = {
			owner = {
				is_hive_empire = yes
			}
		}
		while = {
			count = 18
			create_pop_group = {
				species = owner_main_species
			}
		}
	}
	else_if = {
		limit = {
			owner = {
				has_origin = origin_mechanists
			}
		}
		random_galaxy_species = {
			limit = {
				has_species_flag = mechanical_species@root.owner
			}
			save_event_target_as = mechanical_species
		}
		while = {
			count = 4
			create_pop_group = {
				species = event_target:mechanical_species
			}
		}
		while = {
			count = 7
			create_pop_group = {
				species = owner_main_species
			}
		}
	}
	else_if = {
		limit = {
			owner = {
				OR = {
					has_origin = origin_syncretic_evolution
					has_origin = origin_necrophage
				}
			}
		}
		random_galaxy_species = {
			limit = {
				has_species_flag = syncretic_species@root.owner
			}
			save_event_target_as = syncretic_species
		}
		while = {
			count = 4
			create_pop_group = {
				species = event_target:syncretic_species
			}
		}
		while = {
			count = 6
			create_pop_group = {
				species = owner_main_species
			}
		}
	}
	else = {
		while = {
			count = 10
			create_pop_group = {
				species = owner_main_species
			}
		}
	}
}

add_advanced_empire_capital_districts_effect = {
	# scope: colony (capital, non-shattered-ring)
	add_district_and_planet_size_if_needed_effect = { district = district_mining }
	add_district_and_planet_size_if_needed_effect = { district = district_generator }
	add_district_and_planet_size_if_needed_effect = { district = district_generator }

	if = {
		limit = {
			owner = {
				is_machine_empire = yes
			}
		}
		add_district_and_planet_size_if_needed_effect = { district = district_nexus }
		add_district_and_planet_size_if_needed_effect = { district = district_nexus }
	}
	else_if = {
		limit = {
			owner = {
				is_hive_empire = yes
			}
		}
		add_district_and_planet_size_if_needed_effect = { district = district_hive }
		add_district_and_planet_size_if_needed_effect = { district = district_hive }
	}
	else = {
		add_district_and_planet_size_if_needed_effect = { district = district_city }
		add_district_and_planet_size_if_needed_effect = { district = district_city }
	}

	if = {
		limit = {
			owner = {
				country_uses_food_game_start = no
			}
		}
		add_district_and_planet_size_if_needed_effect = { district = district_generator }
		add_district_and_planet_size_if_needed_effect = { district = district_generator }
	}
	else = {
		add_district_and_planet_size_if_needed_effect = { district = district_farming }
		add_district_and_planet_size_if_needed_effect = { district = district_farming }
	}

	if = {
		limit = {
			owner = {
				is_subterranean_empire = yes
			}
		}
		add_district_and_planet_size_if_needed_effect = { district = district_mining }
		add_district_and_planet_size_if_needed_effect = { district = district_mining }
	}
}

add_advanced_empire_shattered_ring_capital_districts_effect = {
	# scope: colony (capital, shattered ring origin)
	random_deposit = {
		limit = {
			is_deposit_type = d_decrepit_tunnels_2
		}
		clear_blocker = yes
	}
	random_deposit = {
		limit = {
			is_deposit_type = d_decrepit_tunnels_3
		}
		clear_blocker = yes
	}
	random_deposit = {
		limit = {
			is_deposit_type = d_segment_rubble_4
		}
		clear_blocker = yes
	}
	while = {
		count = 2
		add_district_if_possible_effect = {
			district = district_mining
		}
	}
	if = {
		limit = {
			owner = {
				is_gestalt = no
			}
		}
		while = {
			count = 2
			add_district_if_possible_effect = {
				district = district_city
			}
		}
		if = {
			limit = {
				owner = {
					country_uses_food_game_start = no
				}
			}
			add_district_if_possible_effect = {
				district = district_mining
			}
		}
		else = {
			add_district_if_possible_effect = {
				district = district_farming
			}
		}
	}
	else_if = {
		limit = {
			owner = {
				is_hive_empire = yes
			}
		}
		while = {
			count = 2
			add_district_if_possible_effect = {
				district = district_hive
			}
		}
		if = {
			limit = {
				owner = {
					country_uses_food_game_start = no
				}
			}
			add_district_if_possible_effect = {
				district = district_mining
			}
		}
		else = {
			add_district_if_possible_effect = {
				district = district_farming
			}
		}
	}
	else = {
		while = {
			count = 3
			add_district_if_possible_effect = {
				district = district_nexus
			}
		}
	}
}

add_advanced_empire_colony_districts_effect = {
	# scope: colony (non-capital)
	add_district_and_planet_size_if_needed_effect = { district = district_generator }
	add_district_and_planet_size_if_needed_effect = { district = district_mining }

	if = {
		limit = {
			owner = {
				is_machine_empire = yes
			}
		}
		add_district_and_planet_size_if_needed_effect = { district = district_nexus }
		add_district_and_planet_size_if_needed_effect = { district = district_generator }
		add_district_and_planet_size_if_needed_effect = { district = district_generator }
	}
	else_if = {
		limit = {
			owner = {
				is_hive_empire = yes
			}
		}
		add_district_and_planet_size_if_needed_effect = { district = district_hive }
	}
	else = {
		add_district_and_planet_size_if_needed_effect = { district = district_city }
	}
	if = {
		limit = {
			owner = {
				country_uses_food_game_start = yes
			}
		}
		add_district_and_planet_size_if_needed_effect = { district = district_farming }
	}
}

add_advanced_empire_colony_buildings_effect = {
	# scope: colony
	set_timed_carrier_flag = {
		flag = ignore_ai_building_limitations
		days = 1
	}
	if = {
		limit = {
			owner = {
				OR = {
					is_machine_empire = yes
					is_individual_machine = yes
				}
			}
		}
		add_building = building_machine_assembly_plant
	}
	if = {
		limit = {
			owner = {
				is_hive_empire = yes
			}
		}
		if = {
			limit = {
				owner = {
					NOT = {
						has_origin = origin_progenitor_hive
					}
				}
			}
			add_building = building_spawning_pool
		}
		else = {
			add_building = building_offspring_nest
		}
	}
	if = {
		limit = {
			owner = {
				has_origin = origin_mechanists
			}
		}
		add_building = building_robot_assembly_plant
	}
	if = {
		limit = {
			owner = {
				has_origin = origin_necrophage
				is_homicidal = no #purges them instead
				NOT = { has_ai_personality_behaviour = purger }
			}
		}
		add_building = building_necrophage_elevation_chamber
	}
	if = {
		limit = {
			owner = {
				has_origin = origin_clone_army
			}
		}
		add_building = building_clone_army_clone_vat
	}
}

create_advanced_empire_system_stations_effect = {
	# scope: galactic_object (system)
	# $OWNER$ = country
	every_system_planet = {
		limit = {
			has_deposit_for = shipclass_mining_station
			has_mining_station = no
			has_any_rare_strategic_resource = no
		}
		create_mining_station = {
			owner = $OWNER$
		}
	}
	every_system_planet = {
		limit = {
			has_deposit_for = shipclass_research_station
			has_research_station = no
			has_any_rare_strategic_resource = no
		}
		create_research_station = {
			owner = $OWNER$
		}
	}
}

add_advanced_empire_habitat_capital_districts_effect = {
	# scope: colony (capital habitat, void dwellers / void machines origin)
	add_district_and_planet_size_if_needed_effect = { district = district_hab_mining }
	add_district_and_planet_size_if_needed_effect = { district = district_hab_energy }
	add_district_and_planet_size_if_needed_effect = { district = district_hab_energy }
	add_district_and_planet_size_if_needed_effect = { district = district_hab_housing }
	add_district_and_planet_size_if_needed_effect = { district = district_hab_housing }
	add_district_and_planet_size_if_needed_effect = { district = district_hab_energy }
	add_district_and_planet_size_if_needed_effect = { district = district_hab_energy }

	if = {
		limit = {
			owner? = {
				country_uses_food_game_start = yes
			}
		}
		if = {
			limit = {
				has_zone = {
					zone = zone_habitat_hydroponics
				}
			}
			add_building = {
				zone = zone_habitat_hydroponics
				building = building_hydroponics_farm
			}
		}
		else = {
			add_zone = {
				district = district_hab_energy
				zone = zone_habitat_hydroponics
			}
		}
	}
}

add_advanced_empire_habitat_colony_districts_effect = {
	# scope: colony (non-capital habitat, void dwellers / void machines origin)
	add_district_and_planet_size_if_needed_effect = { district = district_hab_energy }
	add_district_and_planet_size_if_needed_effect = { district = district_hab_mining }
	add_district_and_planet_size_if_needed_effect = { district = district_hab_housing }
	add_district_and_planet_size_if_needed_effect = { district = district_hab_energy }

	if = {
		limit = {
			owner = {
				OR = {
					is_machine_empire = yes
					is_individual_machine = yes
				}
			}
		}
		add_district_and_planet_size_if_needed_effect = { district = district_hab_energy }
	}

	if = {
		limit = {
			owner? = {
				country_uses_food_game_start = yes
			}
		}
		if = {
			limit = {
				has_zone = {
					zone = zone_habitat_hydroponics
				}
			}
			add_building = {
				zone = zone_habitat_hydroponics
				building = building_hydroponics_farm
			}
		}
		else = {
			add_zone = {
				district = district_hab_energy
				zone = zone_habitat_hydroponics
			}
		}
	}
}

add_district_and_planet_size_if_needed_effect = {
	optimize_memory
	if = {
		limit = {
			free_district_slots >= 1
		}
		add_district = {
			district_type = $district$
			ignore_cap = yes
		}
	}
	else_if = {
		limit = {
			free_district_slots = 0
			planet_size < 25
		}
		change_planet_size = 1
		add_district = {
			district_type = $district$
			ignore_cap = yes
		}
	}
	#else do nothing
}

add_district_if_possible_effect = {
	optimize_memory
	if = {
		limit = {
			free_district_slots >= 1
		}
		add_district = {
			district_type = $district$
			ignore_cap = yes
		}
	}
	#else do nothing
}

give_starting_resources_effect = {
	optimize_memory
	if = {
		limit = {
			owner = {
				NOT = {
					has_country_flag = start_resources_granted
				}
			}
		}
		owner = {
			add_resource = { energy = 200 }
			add_resource = { minerals = 200 }
			add_resource = { alloys = 200 }
			add_resource = { trade = 200 }
			add_resource = { influence = 100 }
			add_resource = { unity = 50 }
		}
		if = {
			limit = {
				owner = {
					OR = {
						country_uses_bio_ships = yes
						has_valid_civic = civic_hive_cordyceptic_drones
						is_beastmasters_empire = yes
					}
				}
			}
			owner = {
				add_resource = { food = 200 }
				add_resource = { alloys = -50 }
			}
		}
		if = {
			limit = { is_infernal_empire = yes }
			owner = {
				add_resource = { food = -200 }
				add_resource = { minerals = 50 }
				add_resource = { alloys = 50 }
			}
			if = {
				limit = {
					owner = {
						has_country_flag = non_lithoid_subspecies
					}
				}
				owner = {
					add_resource = { food = 200 } #Adding 200 starting food back if theres a food consuming subspecies
				}
			}
		}
		else_if = {
			limit = {
				owner = {
					country_uses_food_game_start = yes
				}
			}
			owner = {
				add_resource = { food = 200 }
			}
		}
		if = {
			limit = {
				owner = {
					country_uses_consumer_goods_game_start = yes
				}
			}
			owner = {
				add_resource = { consumer_goods = 100 }
			}
		}
		if = {
			limit = {
				owner = {
					is_catalytic_empire = yes
				}
			}
			owner = {
				add_resource = { food = 200	}
			}
		}
		if = {
			limit = {
				owner = {
					is_nomadic = yes
				}
			}
			owner = {
				add_resource = {
					energy = 1000
					minerals = 40000
					alloys = 500
					trade = 2000
					volatile_motes = 100
					exotic_gases = 100
					rare_crystals = 100
				}
				if = {
					limit = {
						is_gestalt = no
					}
					add_resource = {
					consumer_goods = 1000
					}
				}
				if = {
					limit = {
						country_uses_food_game_start = yes
					}
					add_resource = {
						food = 2000
					}
				}
				else_if = {
					limit = {
						is_machine_empire = yes
					}
					add_resource = {
						energy = 2000
					}
				}
				else_if = {
					limit = {
						is_lithoid_empire = yes
					}
					add_resource = {
						minerals = 2000
					}
				}
				else_if = {
					limit = {
						is_infernal_empire = yes
					}
					add_resource = {
						minerals = 500
						alloys = 500
					}
				}
			}
		}
		owner = {
			set_country_flag = start_resources_granted
		}
	}
}

imperial_origin_start_effect = {
	# Main Backup
	if = { # Backup if the main system hasn't already been initialized
		limit = {
			NOT = { exists = event_target:origin_imperial_homeland_country }
		}
		# Find a random system, hopefully close to a player with the origin
		random_system = {
			limit = {
				has_owner = no
				has_special_star_flag_trigger = no
			}
			weights = {
				base = 1
				modifier = {
					factor = 100
					closest_system = {
						use_bypasses = no
						min_steps = 2
						max_steps = 4
						limit = {
							has_owner = yes
							owner = {
								has_origin = origin_imperial_vassal
							}
						}
					}
				}
			}
			# Start Spawning Systems
			set_spawn_system_batch = begin
			origin_spawn_system_effect = { INITIALIZER = overlord_system_init }
			# Main Empire
			event_target:origin_imperial_homeland.solar_system = {
				origin_spawn_system_effect = { INITIALIZER = overlord_system_2_init }
				origin_spawn_system_effect = { INITIALIZER = overlord_system_3_init }
			}
			# Vassals
			event_target:imperial_homeland_2.solar_system = { origin_spawn_system_effect = { INITIALIZER = overlord_system_4_init } }
			if = {
				limit = {
					NOT = {
						galaxy_size = tiny
					}
				}
				event_target:imperial_homeland_2.solar_system = { origin_spawn_system_effect = { INITIALIZER = overlord_system_5_init } }
			}
			if = {
				limit = {
					NOR = {
						galaxy_size = tiny
						galaxy_size = small
					}
				}
				event_target:imperial_homeland_3.solar_system = { origin_spawn_system_effect = { INITIALIZER = overlord_system_6_init } }
			}
			if = {
				limit = {
					NOR = {
						galaxy_size = tiny
						galaxy_size = small
						galaxy_size = medium
					}
				}
				event_target:imperial_homeland_3.solar_system = { origin_spawn_system_effect = { INITIALIZER = overlord_system_7_init } }
			}
			set_spawn_system_batch = end

			# Set up hyperlanes if they are missing
			imperial_origin_start_hyperlane_effect = {
				ORIGINAL_STAR_SYSTEM = event_target:imperial_homeland_2.solar_system
				SECOND_STAR_SYSTEM = event_target:origin_imperial_homeland.solar_system
			}
			imperial_origin_start_hyperlane_effect = {
				ORIGINAL_STAR_SYSTEM = event_target:imperial_homeland_3.solar_system
				SECOND_STAR_SYSTEM = event_target:origin_imperial_homeland.solar_system
			}
			imperial_origin_start_hyperlane_effect = {
				ORIGINAL_STAR_SYSTEM = event_target:imperial_homeland_4.solar_system
				SECOND_STAR_SYSTEM = event_target:imperial_homeland_2.solar_system
			}
			if = {
				limit = {
					exists = event_target:imperial_homeland_5
				}
				imperial_origin_start_hyperlane_effect = {
					ORIGINAL_STAR_SYSTEM = event_target:imperial_homeland_5.solar_system
					SECOND_STAR_SYSTEM = event_target:imperial_homeland_2.solar_system
				}
			}
			if = {
				limit = {
					exists = event_target:imperial_homeland_6
				}
				imperial_origin_start_hyperlane_effect = {
					ORIGINAL_STAR_SYSTEM = event_target:imperial_homeland_6.solar_system
					SECOND_STAR_SYSTEM = event_target:imperial_homeland_3.solar_system
				}
			}
			if = {
				limit = {
					exists = event_target:imperial_homeland_7
				}
				imperial_origin_start_hyperlane_effect = {
					ORIGINAL_STAR_SYSTEM = event_target:imperial_homeland_7.solar_system
					SECOND_STAR_SYSTEM = event_target:imperial_homeland_3.solar_system
				}
			}
		}
	}
	else_if = {
		limit = { # Backup if any of the other main empire systems are missing
			OR = {
				NOT = { any_system = { has_star_flag = overlord_system_2_flag } }
				NOT = { any_system = { has_star_flag = overlord_system_3_flag } }
			}
		}
		event_target:origin_imperial_homeland.solar_system = {
			origin_spawn_system_effect = { INITIALIZER = overlord_system_2_init }
			origin_spawn_system_effect = { INITIALIZER = overlord_system_3_init }
			imperial_origin_start_hyperlane_effect = {
				ORIGINAL_STAR_SYSTEM = event_target:imperial_homeland_2.solar_system
				SECOND_STAR_SYSTEM = event_target:origin_imperial_homeland.solar_system
			}
			imperial_origin_start_hyperlane_effect = {
				ORIGINAL_STAR_SYSTEM = event_target:imperial_homeland_3.solar_system
				SECOND_STAR_SYSTEM = event_target:origin_imperial_homeland.solar_system
			}
		}
	}

	# Vassal Backup
	if = { # Backup for vassal 1
		limit = {
			any_system = { has_star_flag = overlord_system_2_flag }
			NOT = { any_system = { has_star_flag = overlord_system_4_flag } }
		}
		imperial_origin_vassal_backup_effect = {
			SYSTEM_FLAG = overlord_system_2_flag
			INITIALIZER = overlord_system_4_init
			ORIGINAL_STAR_SYSTEM = event_target:imperial_homeland_4.solar_system
		}
	}
	if = { # Backup for vassal 2
		limit = {
			any_system = { has_star_flag = overlord_system_2_flag }
			NOR = {
				galaxy_size = tiny
				any_system = { has_star_flag = overlord_system_5_flag }
			}
		}
		imperial_origin_vassal_backup_effect = {
			SYSTEM_FLAG = overlord_system_2_flag
			INITIALIZER = overlord_system_5_init
			ORIGINAL_STAR_SYSTEM = event_target:imperial_homeland_5.solar_system
		}
	}
	if = { # Backup for vassal 3
		limit = {
			any_system = { has_star_flag = overlord_system_3_flag }
			NOR = {
				galaxy_size = tiny
				galaxy_size = small
				any_system = { has_star_flag = overlord_system_6_flag }
			}
		}
		imperial_origin_vassal_backup_effect = {
			SYSTEM_FLAG = overlord_system_3_flag
			INITIALIZER = overlord_system_6_init
			ORIGINAL_STAR_SYSTEM = event_target:imperial_homeland_6.solar_system
		}
	}
	if = { # Backup for vassal 4
		limit = {
			any_system = { has_star_flag = overlord_system_3_flag }
			NOR = {
				galaxy_size = tiny
				galaxy_size = small
				galaxy_size = medium
				any_system = { has_star_flag = overlord_system_7_flag }
			}
		}
		imperial_origin_vassal_backup_effect = {
			SYSTEM_FLAG = overlord_system_3_flag
			INITIALIZER = overlord_system_7_init
			ORIGINAL_STAR_SYSTEM = event_target:imperial_homeland_7.solar_system
		}
	}

	# Modifier to reduce influence
	event_target:origin_imperial_homeland_country = {
		add_modifier = {
			modifier = imperial_vassal_ai_modifier
			days = -1
		}
	}

	# Rival Country
	if = {
		limit = {
			OR = {
				AND = {
					is_multiplayer = yes
					NOT = {
						count_country = {
							count >= 2
							limit = {
								is_ai = no
								has_origin = origin_imperial_vassal
							}
						}
					}
				}
				is_multiplayer = no
			}
		}
		random_country = {
			limit = {
				is_ai = yes
				has_origin = origin_imperial_vassal
			}
			set_country_flag = origin_imperial_vassal_rival_country
		}
	}

	# Startup event for vassals
	every_playable_country = {
		limit = { has_origin = origin_imperial_vassal }
		country_event = { id = origin.5700 }
		if = {
			limit = {
				is_ai = no
			}
			country_event = {
				id = origin.5703
				days = 1
			}
		}
		else = {
			country_event = { id = origin.5710 }
		}
	}
}

imperial_origin_start_hyperlane_effect = {
	if = {
		limit = {
			NOT = {
				$ORIGINAL_STAR_SYSTEM$ = {
					has_hyperlane_to = $SECOND_STAR_SYSTEM$
				}
			}
		}
		add_hyperlane = {
			from = $ORIGINAL_STAR_SYSTEM$
			to = $SECOND_STAR_SYSTEM$
		}
	}
}

origin_spawn_system_effect = {
	spawn_system = {
		min_distance = 10
		max_distance = 30
		max_jumps = 0
		hyperlane = yes
		initializer = $INITIALIZER$
	}
}

imperial_origin_vassal_backup_effect = {
	random_system = {
		limit = {
			has_star_flag = $SYSTEM_FLAG$
		}
		save_event_target_as = this_system

		origin_spawn_system_effect = { INITIALIZER = $INITIALIZER$ }
		imperial_origin_start_hyperlane_effect = {
			ORIGINAL_STAR_SYSTEM = $ORIGINAL_STAR_SYSTEM$
			SECOND_STAR_SYSTEM = event_target:this_system
		}
	}
}

create_life_seeded_planet = {
	set_planet_size = 30

	# 5 + 3 energy
	add_deposit = d_searing_desert
	add_deposit = d_underwater_vent
	add_deposit = d_tempestous_mountain

	# 5 + 3 mining
	add_deposit = d_ore_rich_caverns
	add_deposit = d_prosperous_mesa
	add_deposit = d_submerged_ore_veins

	# 5 + 3 farming (lithoids swap 3 for minerals)
	add_deposit = d_lush_jungle
	add_deposit = d_teeming_reef

	if = {
		limit = {
			exists = owner
			owner = {
				is_lithoid_or_infernal_empire = yes
			}
		}
		add_deposit = d_rich_mountain
	}
	else_if = {
		limit = {
			exists = owner
			owner = {
				is_lithoid_empire = no
			}
		}
		add_deposit = d_black_soil
	}
	# In case the owner doesn't exist, as when creating Life-Seeded pre-ftls.
	else = {
		random_list = {
			50 = { add_deposit = d_rich_mountain }
			50 = { add_deposit = d_black_soil }
		}
	}

	# 1 random medium deposit
	random_list = {
		33 = { add_deposit = d_frozen_gas_lake }
		33 = { add_deposit = d_prosperous_mesa }
		33 = { add_deposit = d_fertile_lands }
	}

	add_deposit = d_dust_desert
	add_deposit = d_fuming_bog
	add_deposit = d_crystal_forest
}

create_ocean_paradise_planet = {
	if = {
		limit = {
			exists = owner
			owner = { is_gestalt = yes }
		}
		add_modifier = { modifier = ocean_paradise_hive }
	}
	else = {
		add_modifier = { modifier = ocean_paradise }
	}
	clear_deposits = yes
	clear_blockers = yes

	# Food
	add_deposit = d_teeming_reef
	add_deposit = d_teeming_reef
	add_deposit = d_teeming_reef
	add_deposit = d_teeming_reef
	add_deposit = d_teeming_reef
	add_deposit = d_teeming_reef
	add_deposit = d_teeming_reef
	add_deposit = d_teeming_reef

	# Minerals
	add_deposit = d_submerged_ore_veins
	add_deposit = d_submerged_ore_veins
	add_deposit = d_submerged_ore_veins

	# Energy
	add_deposit = d_underwater_vent
	add_deposit = d_underwater_vent
	add_deposit = d_underwater_vent

	# Bonus
	add_deposit = d_crystal_reef
	random_list = {
		50 = { add_deposit = d_submerged_ore_veins }
		50 = { add_deposit = d_underwater_vent }
	}
	# Planetscapers
	if = {
		limit = {
			exists = owner
			owner = {
				is_environmental_architects_empire = yes
			}
		}
		add_random_natural_blocker_and_give_tech = yes
	}
}

create_wilderness_planet = {
	clear_deposits = yes
	clear_blockers = yes

	# Features
	# Food
	add_deposit = d_lichen_fields
	add_deposit = d_great_river
	add_deposit = d_forgiving_tundra

	if = {
		limit = {
			exists = owner
			owner = {
				is_lithoid_or_infernal_empire = yes
			}
		}
		add_deposit = d_rich_mountain
	}
	else = {
		add_deposit = d_fungal_forest
	}

	# Minerals
	add_deposit = d_veiny_cliffs
	add_deposit = d_rich_mountain
	add_deposit = d_rich_mountain

	# Energy
	add_deposit = d_arid_highlands
	add_deposit = d_rushing_waterfalls
	add_deposit = d_geothermal_vent

	# Blockers
	add_deposit = d_scorched_plains
	add_deposit = d_scorched_plains

	if = {
		limit = {
			owner = { has_valid_civic = civic_machine_servitor }
		}
		add_deposit = d_hot_springs
		add_deposit = d_strip_mine
		add_deposit = d_unsupervised_settlement
	}
	else = {
		add_deposit = d_decrepit_dwellings
	}

	if = {
		limit = {
			owner = {
				is_lithoid_empire = yes
				NOT = { has_valid_civic = civic_hive_devouring_swarm }
			}
		}
		while = {
			count = 4
			add_deposit = d_hibernating_lithoids
		}
	}

	if = {
		limit = {
			owner = {
				is_lithoid_empire = yes
				has_valid_civic = civic_hive_devouring_swarm
			}
		}
		while = {
			count = 2
			add_deposit = d_hibernating_lithoids
		}
	}

	if = {
		limit = {
			is_planet_class = pc_desert # Just in case players cause trouble
		}
		set_carrier_flag = tasty_desert
	}
}

fear_of_the_dark_binary_planets = {
	save_event_target_as = fotd_country
	random_planet_within_border = {
		limit = {
			has_planet_flag = delete_me_planet
		}
		remove_planet = yes
	}
	random_planet_within_border = {
		limit = {
			has_planet_flag = haven_flag
		}
		save_global_event_target_as = fotd_seperatist_planet@prev
		set_planet_flag = fotd_seperatist_planet@prev
		create_fear_of_the_dark_country = yes
		colony = {
			setup_fear_of_the_dark_pre_ftl = yes
		}
		if = {
			limit = {
				has_planet_flag = planet_mars
			}
			set_mars_entity = yes
		}
	}
	random_planet_within_border = {
		limit = {
			has_planet_flag = divider_flag
		}
		add_deposit = d_alloys_5
	}
	random_planet_within_border = {
		limit = {
			has_planet_flag = divider_flag
		}
		create_mining_station = {
			owner = event_target:fotd_country
		}
	}
}

low_tech_start_add_research_options_effect = {
	if = {
		limit = {
			country_uses_bio_ships = yes
		}
		add_research_option = tech_maulers
	}
	else = {
		add_research_option = tech_corvettes
	}
	add_research_option = tech_basic_science_lab_1
	add_research_option = tech_planetary_government
}

fruitful_start_effect = {
	# Establish first contact with the Tyanki
	set_country_flag = tiyanki_encountered
	create_tiyanki_country = yes

	# Set up the event chain container for the special proects
	begin_event_chain = {
		event_chain = fruitful_partnership_chain
		target = this
	}

	# Set up event for getting the Tyanki technologies later
	country_event = { id = origin.7000 days = 1800 random = 1800 }

	random_owned_starbase = {
		set_starbase_building = {
			slot = 1
			building = feeder
		}
	}

	capital_scope.solar_system = {
		# Spawn critters to a nearby system
		random_neighbor_system = {
			set_star_flag = tiyanki_plantoid_system
			random_system_planet = {
				limit = { is_star = yes }
				event_target:tiyanki_country = {
					create_fleet = {
						name = "NAME_Tiyanki_Space_Whale"
						effect = {
							set_owner = event_target:tiyanki_country
							create_ship = {
								design = "NAME_Tiyanki_Cow"
							}
							create_ship = {
								design = "NAME_Tiyanki_Bull"
							}
							create_ship = {
								design = "NAME_Tiyanki_Calf"
							}
							create_ship = {
								design = "NAME_Tiyanki_Hatchling"
							}
							set_location = prevprev
							set_fleet_stance = passive
							set_aggro_range_measure_from = self
							set_aggro_range = 0
							set_fleet_flag = NAME_Tiyanki_Space_Whale
							queue_actions = {
								wait = { # Cooldown until they start moving
									duration = 320
									random = 80
								}
							}
						}
					}
				}
			}
		}

		# Imprint the space amoeba near your home so they will immediately go to your home system to eat,
		# instead of vanishing into a natural wormhole
		closest_system = {
			limit = {
				has_star_flag = tiyanki_plantoid_system
			}
			every_fleet_in_system = {
				limit = {
					owner = {
						is_country_type = tiyanki
					}
				}
				set_fleet_flag = fruitful_lured_critter
				auto_move_to_planet = {
					target = prev.star
					clear_auto_move_on_arrival = yes
				}
			}
		}
	}
}

lost_colony_generate_parent_effect = {
	event_target:lost_colony_child = {
		if = {
			limit = {
				is_nomadic = yes
			}
			random_list = {
				1 = {
					modify_species = {
						species = species
						ideal_planet_class = pc_desert
						change_scoped_species = no
						effect = { save_event_target_as = lost_colony_parent_species }
					}
					modifier = {
						mult = 0
						species = { has_trait = trait_aquatic }
					}
				}
				1 = {
					modify_species = {
						species = species
						ideal_planet_class = pc_arid
						change_scoped_species = no
						effect = { save_event_target_as = lost_colony_parent_species }
					}
					modifier = {
						mult = 0
						species = { has_trait = trait_aquatic }
					}
				}
				1 = {
					modify_species = {
						species = species
						ideal_planet_class = pc_savannah
						change_scoped_species = no
						effect = { save_event_target_as = lost_colony_parent_species }
					}
					modifier = {
						mult = 0
						species = { has_trait = trait_aquatic }
					}
				}
				1 = {
					modify_species = {
						species = species
						ideal_planet_class = pc_tropical
						change_scoped_species = no
						effect = { save_event_target_as = lost_colony_parent_species }
					}
				}
				1 = {
					modify_species = {
						species = species
						ideal_planet_class = pc_continental
						change_scoped_species = no
						effect = { save_event_target_as = lost_colony_parent_species }
					}
				}
				1 = {
					modify_species = {
						species = species
						ideal_planet_class = pc_ocean
						change_scoped_species = no
						effect = { save_event_target_as = lost_colony_parent_species }
					}
					modifier = {
						mult = 10
						species = { has_trait = trait_aquatic }
					}
				}
				1 = {
					modify_species = {
						species = species
						ideal_planet_class = pc_arctic
						change_scoped_species = no
						effect = { save_event_target_as = lost_colony_parent_species }
					}
				}
				1 = {
					modify_species = {
						species = species
						ideal_planet_class = pc_tundra
						change_scoped_species = no
						effect = { save_event_target_as = lost_colony_parent_species }
					}
				}
				1 = {
					modify_species = {
						species = species
						ideal_planet_class = pc_alpine
						change_scoped_species = no
						effect = { save_event_target_as = lost_colony_parent_species }
					}
				}
				0 = {
					modify_species = {
						species = species
						ideal_planet_class = pc_volcanic
						change_scoped_species = no
						effect = { save_event_target_as = lost_colony_parent_species }
					}
					modifier = {
						add = 50
						species = { has_trait = trait_infernal }
					}
				}
			}
		}
		else = {
			species = { save_event_target_as = lost_colony_parent_species }
		}
	}
	if = { # hive parent
		limit = {
			event_target:lost_colony_child = {
				is_hive_empire = yes
			}
		}
		if = { # Parent hive has Progenitor hive if you own the Overlord DLC
			limit = {
				has_overlord_dlc = yes
			}
			create_country = {
				species = event_target:lost_colony_parent_species
				name_list = event_target:lost_colony_child
				type = default
				origin = "origin_progenitor_hive"
				government_restrictions = {
					civics = {
						NOT = {
							value = civic_hive_bodysnatcher
						}
					}
				}
				authority = auth_hive_mind
				civics = {
					civic = civic_hive_divided_attention
					civic = random
				}
			}
		}
		else = {
			create_country = {
				species = event_target:lost_colony_parent_species
				name_list = event_target:lost_colony_child
				type = default
				origin = "origin_default"
				government_restrictions = {
					civics = {
						NOT = {
							value = civic_hive_bodysnatcher
						}
					}
				}
				authority = auth_hive_mind
				civics = {
					civic = civic_hive_divided_attention
					civic = random
				}
			}
		}
	}
	else_if = { # machine parent
		limit = {
			event_target:lost_colony_child = {
				is_machine_empire = yes
			}
		}
		create_country = {
			species = event_target:lost_colony_parent_species
			name_list = event_target:lost_colony_child
			type = default
			origin = "origin_default"
			government_restrictions = {
				civics = {
					NOR = {
						value = civic_machine_servitor
						value = civic_machine_assimilator
					}
				}
			}
			authority = auth_machine_intelligence
			civics = {
				civic = civic_machine_delegated_functions
				civic = random
			}
		}
	}
	else = { # default parent
		create_country = {
			species = event_target:lost_colony_parent_species
			name_list = event_target:lost_colony_child
			type = default
			origin = "origin_default"
			government_restrictions = {
				authority = {
					NOR = {
						value = auth_machine_intelligence
						value = auth_hive_mind
					}
				}
			}
		}
	}
	last_created_country = {
		save_event_target_as = lost_colony_parent
		add_resource = {
			minerals = 2400
			energy = 2400
			food = 2400
			alloys = 800
			consumer_goods = 1200
			influence = 600
		}
	}
}

lost_colony_generate_parent_human_effect = {
	# The parent always spawns on Earth (pc_continental). If the child is nomadic its
	# species carries arkship habitability (trait_pc_ark_preference), so derive a
	# continental-preference variant for the planet-bound parent. origin.100 cannot fix
	# this because it intentionally skips humans (is_human_species = no).
	if = {
		limit = { event_target:human_lost_colony = { is_nomadic = yes } }
		event_target:human_lost_colony = {
			modify_species = {
				species = species
				ideal_planet_class = pc_continental
				change_scoped_species = no
				effect = { save_event_target_as = human_parent_species }
			}
		}
	}
	else = {
		event_target:human_species = { save_event_target_as = human_parent_species }
	}
	if = { # hive parent
		limit = {
			event_target:human_lost_colony = {
				is_hive_empire = yes
			}
		}
		if = { # Parent hive has Progenitor hive if you own the Overlord DLC
			limit = {
				has_overlord_dlc = yes
			}
			random_list = {
				50 = {
					create_country = {
						species = event_target:human_parent_species
						name_list = HUMAN1
						type = default
						origin = "origin_progenitor_hive"
						authority = auth_hive_mind
						civics = {
							civic = civic_hive_divided_attention
							civic = random
						}
					}
				}
				50 = {
					create_country = {
						species = event_target:human_parent_species
						name_list = HUMAN2
						type = default
						origin = "origin_progenitor_hive"
						authority = auth_hive_mind
						civics = {
							civic = civic_hive_divided_attention
							civic = random
						}
					}
				}
			}
		}
		else = {
			random_list = {
				50 = {
					create_country = {
						species = event_target:human_parent_species
						name_list = HUMAN1
						type = default
						origin = "origin_default"
						authority = auth_hive_mind
						civics = {
							civic = civic_hive_divided_attention
							civic = random
						}
					}
				}
				50 = {
					create_country = {
						species = event_target:human_parent_species
						name_list = HUMAN2
						type = default
						origin = "origin_default"
						authority = auth_hive_mind
						civics = {
							civic = civic_hive_divided_attention
							civic = random
						}
					}
				}
			}
		}
	}
	else = { # default parent
		random_list = {
			50 = {
				create_country = {
					species = event_target:human_parent_species
					name_list = HUMAN1
					type = default
					origin = "origin_default"
					government_restrictions = {
						authority = {
							NOR = {
								value = auth_machine_intelligence
								value = auth_hive_mind
							}
						}
					}
				}
			}
			50 = {
				create_country = {
					species = event_target:human_parent_species
					name_list = HUMAN2
					type = default
					origin = "origin_default"
					government_restrictions = {
						authority = {
							NOR = {
								value = auth_machine_intelligence
								value = auth_hive_mind
							}
						}
					}
				}
			}
		}
	}
	last_created_country = {
		save_event_target_as = lost_colony_parent
		add_resource = {
			minerals = 2400
			energy = 2400
			food = 2400
			alloys = 800
			consumer_goods = 1200
			influence = 600
		}
	}
}

add_district_and_planet_size_if_needed_effect_amount = {
	optimize_memory
	while = {
		count = $amount$
		if = {
			limit = {
				free_district_slots >= 1
			}
			add_district = {
				district_type = $district$
				ignore_cap = yes
			}
		}
		else_if = {
			limit = {
				free_district_slots = 0
				planet_size < 25
			}
			change_planet_size = 1
			add_district = {
				district_type = $district$
				ignore_cap = yes
			}
		}
	}
}

unplugged_origin_start_effect = {
	if = {
		limit = {
			NOT = { exists = event_target:origin_unplugged_assimilator_empire }
		}

		create_species = {
			name = NAME_Unplugged_Assimilator_species
			class = MACHINE
			namelist = MACHINE2
			portrait = sd_art_robot
			traits = {
				ideal_planet_class = pc_machine
				trait = trait_machine_unit
				trait = random_traits
			}
			extra_trait_points = 2
			allow_negative_traits = yes
		}
		last_created_species = {
			save_event_target_as = unplugged_assimilator_species
		}

		create_country = {
			name = NAME_Unplugged_Assimilator
			type = faction
			auto_delete = no
			flag = {
				icon = {
					category = "legion"
					file = "flag_opus.dds"
				}
				background = {
					category = "backgrounds"
					file = "00_solid.dds"
				}
				colors = {
					"burgundy"
					"burgundy"
					"null"
					"null"
				}
			}
			origin = origin_machine
			species = event_target:unplugged_assimilator_species

			effect = {
				every_playable_country = {
					limit = {
						has_origin = origin_unplugged
					}
					establish_communications_no_message = prev
					set_country_flag = Story2 # already encountered complex space-dwelling life
					set_country_flag = Story5 # already encountered sapient lifeforms
				}
				set_country_flag = unplugged_assimilator_empire
				save_global_event_target_as = origin_unplugged_assimilator_empire
				set_faction_hostility = {
					set_hostile = yes
					set_neutral = no
					set_friendly = no
				}
				random_playable_country = {
					limit = {
						has_origin = origin_unplugged
					}
					save_event_target_as = random_unplugged_country
				}
				set_graphical_culture = event_target:random_unplugged_country
			}
		}
	}

	# Spawn the Machine capital
	if = {
		limit = {
			NOT = { exists = event_target:origin_unplugged_machine_legacy_core }
		}
		random_system = {
			limit = {
				has_owner = no
				has_special_star_flag_trigger = no
			}
			weights = {
				base = 1
				modifier = {
					factor = 1000000
					closest_system = {
						use_bypasses = no
						min_steps = 3
						max_steps = 4
						limit = {
							has_owner = yes
							owner = {
								has_origin = origin_unplugged
							}
						}
					}
				}
				modifier = {
					factor = 0.5
					closest_system = {
						use_bypasses = no
						min_steps = 1
						max_steps = 3
						limit = {
							has_owner = yes
							owner = {
								NOT = { has_origin = origin_unplugged }
							}
						}
					}
				}
				modifier = {
					factor = 2
					count_neighbor_system = {
						count > 2
					}
				}
			}
			set_spawn_system_batch = begin
			random_list = {
				1 = {
					spawn_system = {
						min_distance = 15
						max_distance = 15
						max_jumps = 0
						hyperlane = no
						# angle of 270 +- 60
						min_orientation_angle = 210
						max_orientation_angle = 330
						initializer = origin_unplugged_machine_legacy_core_system_init
					}
					set_star_flag = machine_legacy_connect_periphery_1
				}
				1 = {
					spawn_system = {
						min_distance = 15
						max_distance = 15
						max_jumps = 0
						hyperlane = no
						# angle of 30 +- 60
						min_orientation_angle = 330
						max_orientation_angle = 90
						initializer = origin_unplugged_machine_legacy_core_system_init
					}
					set_star_flag = machine_legacy_connect_periphery_2
				}
				1 = {
					spawn_system = {
						min_distance = 15
						max_distance = 15
						max_jumps = 0
						hyperlane = no
						# angle of 150 +- 60
						min_orientation_angle = 90
						max_orientation_angle = 210
						initializer = origin_unplugged_machine_legacy_core_system_init
					}
					set_star_flag = machine_legacy_connect_periphery_3
				}
			}

			event_target:origin_unplugged_machine_legacy_core.solar_system = {
				spawn_system = {
					min_distance = 6
					max_distance = 6
					max_jumps = 0
					hyperlane = no
					min_orientation_angle = 90
					max_orientation_angle = 90
					initializer = origin_unplugged_machine_legacy_periphery_system_init
				}
				last_created_system = {
					save_event_target_as = origin_unplugged_machine_legacy_periphery_system_1
				}
				spawn_system = {
					min_distance = 6
					max_distance = 6
					max_jumps = 0
					hyperlane = no
					min_orientation_angle = 210
					max_orientation_angle = 210
					initializer = origin_unplugged_machine_legacy_periphery_system_init
				}
				last_created_system = {
					save_event_target_as = origin_unplugged_machine_legacy_periphery_system_2
				}
				spawn_system = {
					min_distance = 6
					max_distance = 6
					max_jumps = 0
					hyperlane = no
					min_orientation_angle = 330
					max_orientation_angle = 330

					initializer = origin_unplugged_machine_legacy_periphery_system_init
				}
				last_created_system = {
					save_event_target_as = origin_unplugged_machine_legacy_periphery_system_3
				}
			}
			set_spawn_system_batch = end
			imperial_origin_start_hyperlane_effect = {
				ORIGINAL_STAR_SYSTEM = event_target:origin_unplugged_machine_legacy_core
				SECOND_STAR_SYSTEM = event_target:origin_unplugged_machine_legacy_periphery_system_1
			}
			imperial_origin_start_hyperlane_effect = {
				ORIGINAL_STAR_SYSTEM = event_target:origin_unplugged_machine_legacy_core
				SECOND_STAR_SYSTEM = event_target:origin_unplugged_machine_legacy_periphery_system_2
			}
			imperial_origin_start_hyperlane_effect = {
				ORIGINAL_STAR_SYSTEM = event_target:origin_unplugged_machine_legacy_core
				SECOND_STAR_SYSTEM = event_target:origin_unplugged_machine_legacy_periphery_system_3
			}
			imperial_origin_start_hyperlane_effect = {
				ORIGINAL_STAR_SYSTEM = event_target:origin_unplugged_machine_legacy_periphery_system_1
				SECOND_STAR_SYSTEM = event_target:origin_unplugged_machine_legacy_periphery_system_2
			}
			imperial_origin_start_hyperlane_effect = {
				ORIGINAL_STAR_SYSTEM = event_target:origin_unplugged_machine_legacy_periphery_system_2
				SECOND_STAR_SYSTEM = event_target:origin_unplugged_machine_legacy_periphery_system_3
			}
			imperial_origin_start_hyperlane_effect = {
				ORIGINAL_STAR_SYSTEM = event_target:origin_unplugged_machine_legacy_periphery_system_3
				SECOND_STAR_SYSTEM = event_target:origin_unplugged_machine_legacy_periphery_system_1
			}

			switch = {
				trigger = has_star_flag
				machine_legacy_connect_periphery_1 = {
					imperial_origin_start_hyperlane_effect = {
						ORIGINAL_STAR_SYSTEM = event_target:origin_unplugged_machine_legacy_periphery_system_1
						SECOND_STAR_SYSTEM = this
					}
				}
				machine_legacy_connect_periphery_2 = {
					imperial_origin_start_hyperlane_effect = {
						ORIGINAL_STAR_SYSTEM = event_target:origin_unplugged_machine_legacy_periphery_system_2
						SECOND_STAR_SYSTEM = this
					}
				}
				machine_legacy_connect_periphery_3 = {
					imperial_origin_start_hyperlane_effect = {
						ORIGINAL_STAR_SYSTEM = event_target:origin_unplugged_machine_legacy_periphery_system_3
						SECOND_STAR_SYSTEM = this
					}
				}
			}
		}

		# every unplugged player should have some machine legacy around them
		every_country = {
			limit = {
				has_origin = origin_unplugged
			}
			capital_star.solar_system = {
				every_neighbor_system = {
					limit = {
						has_owner = no
					}
					set_star_flag = unplugged_machine_legacy_flag
					set_star_flag = unplugged_machine_legacy_border_flag_3
				}
			}
		}

		unplugged_origin_expand_machine_legacy_border_effect = yes

		set_spawn_system_batch = begin

		unplugged_origin_place_special_system_effect = {
			SYSTEM_FLAG = unplugged_machine_legacy_border_flag_3
			INITIALIZER = unplugged_machine_legacy_tomb_1_system_init
		}

		unplugged_origin_place_special_system_effect = {
			SYSTEM_FLAG = unplugged_machine_legacy_border_flag_3
			INITIALIZER = unplugged_machine_legacy_tomb_2_system_init
		}

		unplugged_origin_place_special_system_effect = {
			SYSTEM_FLAG = unplugged_machine_legacy_border_flag_1
			INITIALIZER = unplugged_machine_legacy_tomb_3_system_init
		}

		unplugged_origin_place_special_system_effect = {
			SYSTEM_FLAG = unplugged_machine_legacy_border_flag_2
			INITIALIZER = unplugged_machine_legacy_tomb_4_system_init
		}

		set_spawn_system_batch = end

		unplugged_origin_mark_exisiting_tomb_worlds_effect = yes

		unplugged_origin_system_backup_effect = yes

		unplugged_origin_find_available_machine_legacy_world_effect = {
			LOCAL_EVENT_TARGET = maker_planet
		}
		event_target:maker_planet = {
			save_global_event_target_as = origin_unplugged_maker_planet
		}
		unplugged_origin_find_available_machine_legacy_world_effect = {
			LOCAL_EVENT_TARGET = taken_planet
		}
		event_target:taken_planet = {
			save_global_event_target_as = origin_unplugged_taken_planet
		}
		unplugged_origin_find_available_machine_legacy_world_effect = {
			LOCAL_EVENT_TARGET = creators_planet
		}
		event_target:creators_planet = {
			save_global_event_target_as = origin_unplugged_creators_planet
		}

		every_country = {
			limit = {
				has_origin = origin_unplugged
			}
			unplugged_origin_find_available_machine_legacy_world_effect = {
				LOCAL_EVENT_TARGET = unplugged_homeworld
			}
			event_target:unplugged_homeworld = {
				save_global_event_target_as = origin_unplugged_unplugged_planet_@prev
			}
		}

		unplugged_origin_populate_machine_legacy_effect = yes
	}
}

generate_starting_rural_districts = {
	add_district_and_planet_size_if_needed_effect_amount = {
		amount = 2
		district = district_generator
	}
	add_district_and_planet_size_if_needed_effect_amount = {
		amount = 2
		district = district_mining
	}
	add_district_and_planet_size_if_needed_effect_amount = {
		amount = 3
		district = district_farming
	}
}

# Nomads
nomad_country_initial_setup_effect = {
	capital_scope = {
		upgrade_colony_shelter = yes
	}
	# Arkship Random Event Director
	country_event = {
		id = arkship.1
		random = 20
	}
	# Nomadic empires can ignore closed borders
	set_faction_properties = {
		needs_border_access = no
	}
	save_event_target_as = nomad_country
	set_variable = { which = contract_completed value = 0 }
	if = {
		limit = {
			has_origin = origin_forever_cruise
		}
		set_variable = { which = cruise_contract_completed value = 0 }
	}
	set_resource_converter = nomadic_operational_reserves

	# Caravan Masters start with an initial Waystation & Tech
	if = {
		limit = {
			is_caravan_masters_empire = yes
		}
		capital_scope = {
			solar_system = {
				create_waystation_effect = {
					OWNER = event_target:nomad_country
					ANGLE = 120
				}
			}
		}
		random_owned_nonprimary_starbase = {
			limit = {
				is_waystation_starbase = yes
			}
			set_starbase_module = {
				slot = 1
				module = waystation_energy_module
			}
			set_starbase_module = {
				slot = 2
				module = waystation_mining_module
			}
		}
		add_research_option = tech_waystation_2
	}

	if = {
		limit = {
			is_deep_sleep_country = yes
		}
		capital_scope = {
			add_blocker = { type = d_deep_sleep_cryo_pods }
			add_blocker = { type = d_deep_sleep_cryo_pods }
			add_blocker = { type = d_deep_sleep_cryo_pods }
		}
	}

	if = {
		limit = {
			NOT = { has_origin = origin_forever_cruise }
		}
		# Issue the initial contracts from your own nomad country
		country_event = {
			id = nomads.3010
		}
	}

	if = {
		# Necrophage nomads keep their two guaranteed ideal planets as pre-FTL
		limit = {
			has_origin = origin_necrophage
			NOT = { has_country_flag = is_advanced_start_AI }
		}
		# First pre-scripted ideal: older, more developed pre-FTL
		closest_system = {
			limit = { has_star_flag = neighbor_t1_first_colony }
			max_steps = 3
			random_system_planet = {
				limit = {
					has_planet_flag = prescripted_ideal
				}
				random_list = {
					1 = { change_pc = pc_tropical }
					1 = { change_pc = pc_arid }
					1 = { change_pc = pc_continental }
					1 = { change_pc = pc_ocean }
					1 = { change_pc = pc_tundra }
					1 = { change_pc = pc_arctic }
					1 = { change_pc = pc_alpine }
					1 = { change_pc = pc_savannah }
					1 = { change_pc = pc_desert }
				}
				reroll_planet = yes
				generate_very_early_pre_ftls_on_planet = yes
				set_name = random
				change_colony_foundation_date = -461

				while = {
					count = 4
					create_army = {
						owner = last_created_country
						type = primitive_additional_army
						species = last_created_country
					}
				}
			}
		}
		# Second pre-scripted ideal: even older pre-FTL
		closest_system = {
			limit = { has_star_flag = neighbor_t2_second_colony }
			max_steps = 6
			random_system_planet = {
				limit = {
					has_planet_flag = prescripted_ideal
				}
				random_list = {
					1 = { change_pc = pc_tropical }
					1 = { change_pc = pc_arid }
					1 = { change_pc = pc_continental }
					1 = { change_pc = pc_ocean }
					1 = { change_pc = pc_tundra }
					1 = { change_pc = pc_arctic }
					1 = { change_pc = pc_alpine }
					1 = { change_pc = pc_savannah }
					1 = { change_pc = pc_desert }
				}
				reroll_planet = yes
				generate_very_early_pre_ftls_on_planet = yes
				set_name = random
				change_colony_foundation_date = -1275

				while = {
					count = 4
					create_army = {
						owner = last_created_country
						type = primitive_additional_army
						species = last_created_country
					}
				}
			}
		}
	}
	else = {
		# Change starting pre-scripted ideals into inhabitable worlds
		# First pre-sripted is a research system
		closest_system = {
			limit = { has_star_flag = neighbor_t1_first_colony }
			max_steps = 3
			convert_to_research_system_effect = yes
		}
		# Second pre-sripted is a resource system
		closest_system = {
			limit = { has_star_flag = neighbor_t2_second_colony }
			max_steps = 6
			convert_to_resource_system_effect = yes
		}
	}
	# Caravan Masters keeps their starting-system deposits so the civic's free Waystation has something to work with
	if = {
		limit = {
			is_caravan_masters_empire = no
		}
		capital_scope = {
			solar_system = {
				every_system_planet = {
					limit = {
						has_deposit = d_minerals_5
					}
					remove_deposit = d_minerals_5
				}
				every_system_planet = {
					limit = {
						has_deposit = d_energy_5
					}
					remove_deposit = d_energy_5
				}
				every_system_planet = {
					limit = {
						has_harvestable_resources_trigger = yes
					}
					set_variable_to_random_value = { which = duration min = @arkship_harvest_resources_duration max = @arkship_harvest_resources_duration_doubled rounded = yes }
					add_modifier = {
						modifier = harvested_resources_mining
						months = 1
						time_multiplier = duration
					}
				}
			}
		}
	}

	# Grant the Arkship Readiness checklist
	country_event = {
		id = nomads.50
	}
}

home_arkship_initial_setup = {
	initialize_arkship_starbase_effect = yes
	switch = {
		trigger = is_ship_size
		civilian_arkship_tier_1 = {
			set_graphical_culture = civilian_arkship
			owner = { set_country_flag = starting_civilian_arkship }
		}
		science_arkship_tier_1 = {
			set_graphical_culture = science_arkship
			owner = { set_country_flag = starting_science_arkship }
		}
		military_arkship_tier_1 = {
			set_graphical_culture = military_arkship
			owner = { set_country_flag = starting_military_arkship }
		}
	}
	owner = {
		create_leader = {
			class = commander
			name = random
			species = owner_main_species
			randomize_traits = no
			traits = {
				1 = random_common
			}
		}
	}
	fleet = {
		set_leader = last_created_leader
	}
	# Add blockers
	colony = {
		if = {
			limit = {
				owner = {
					has_civic = civic_machine_servitor
				}
			}
			add_deposit = d_unsupervised_settlement
		}
		else_if = {
			limit = {
				owner = {
					is_gestalt = no
				}
			}
			add_deposit = d_decrepit_dwellings
		}
		else_if = {
			limit = {
				owner = {
					is_hive_empire = yes
				}
			}
			add_deposit = d_collapsed_burrows
		}
	}
}

heirs_of_the_khan_setup_effect = {
	set_variable = {
		which = defeated_heirs_hunters #This one is to track how many fleets you have defeated
		value = 0
	}
	set_variable = {
		which = defeated_heirs_hunters_spawner #This one is to impact the power of the hostile fleet we spawn, these two variables might get out of sync
		value = 0
	}
	if = {
		limit = {
			NOT = {
				exists = event_target:marauder_heir
			}
		}
		if = {
			limit = {
				exists = event_target:marauder_country_1
			}
			event_target:marauder_country_1 = {
				save_global_event_target_as = marauder_heir
			}
		}
		else = {
			random_system = {
				limit = {
					is_in_cluster = resource_cluster_1 # Defined in code; see RESOURCE_CLUSTER_COUNT
					is_fe_cluster = no
					NOR = {
						has_star_flag = empire_cluster
						has_star_flag = guardians
						has_star_flag = hostile_system
						any_neighbor_system = {
							OR = {
								has_star_flag = empire_cluster
								has_star_flag = guardians
								has_star_flag = hostile_system
								is_fe_cluster = yes
							}
						}
					}
				}
				spawn_system = {
					initializer = marauder_1_1
				}
				last_created_country = {
					save_global_event_target_as = marauder_heir
					change_country_flag = {
						#For some reason they are not getting their flag when run through this effect.
						icon = {
							category = "pirate"
							file = "flag_pirate_10.dds"
						}
						background = {
							category = "backgrounds"
							file = "00_solid.dds"
						}
						colors = {
							"black"
							"black"
							"null"
							"null"
						}
					}
				}
			}
		}

		establish_communications_no_message = event_target:marauder_heir

		create_country = {
			name = NAME_Hunter_Marauders
			type = "marauder_raiders"
			flag = random
			auto_delete = no
			effect = {
				save_global_event_target_as = marauder_hunters
				set_country_flag = heirs_marauder_hunters
				establish_communications_no_message = prev
				set_faction_hostility = {
					target = prev
					set_hostile = yes
					set_neutral = no
					set_friendly = no
				}
				create_leader = {
					class = commander
					species = event_target:marauder_heir.species
					name = random
					skill = 3
					leader_age_min = 25
					leader_age_max = 45
					traits = {
						0 = leader_trait_mercenary_warrior
					}
				}
			}
		}
	}
	else = {
		establish_communications_no_message = event_target:marauder_heir
		event_target:marauder_hunters = {
			set_faction_hostility = {
				target = prev
				set_hostile = yes
				set_neutral = no
				set_friendly = no
			}
		}
	}
	country_event = {
		id = nomads.501
		days = 1
	}
}

convert_to_research_system_effect = {
	random_system_planet = {
		limit = {
			is_colony = no
			has_planet_flag = prescripted_ideal
		}
		remove_planet_flag = prescripted_ideal
		change_pc = pc_frozen
		set_deposit = d_engineering_5
	}
	while = {
		count = 3
		random_system_planet = {
			limit = {
				is_star = no
				is_colonizable = no
				has_deposit = no
				has_anomaly = no
			}
			random_list = {
				5 = { set_deposit = d_physics_2 }
				5 = { set_deposit = d_society_2 }
				5 = { set_deposit = d_engineering_2 }
				1 = { set_deposit = d_physics_3 }
				1 = { set_deposit = d_society_3 }
				1 = { set_deposit = d_engineering_3 }
			}
		}
	}
}

convert_to_resource_system_effect = {
	random_system_planet = {
		limit = {
			is_colony = no
			has_planet_flag = prescripted_ideal
		}
		remove_planet_flag = prescripted_ideal
		change_pc = pc_molten
		set_deposit = d_alloys_3
		add_deposit = d_minerals_5
	}
	while = {
		count = 3
		random_system_planet = {
			limit = {
				is_star = no
				has_deposit = no
				has_anomaly = no
			}
			random_list = {
				5 = { set_deposit = d_minerals_2 }
				5 = { set_deposit = d_energy_2 }
				1 = { set_deposit = d_minerals_3 }
				1 = { set_deposit = d_energy_3 }
			}
		}
	}
}
