rg_create_fallen_empire_species_effect = {
	create_species = {
		name = 纳米构造体
		class = RG_MACHINE
		portrait = "rg_machine_maid"
		traits = {
			trait = trait_rg_machine_unit
			trait = trait_robot_domestic_protocols
			trait = rg_machine_maid_trait
			trait = trait_auto_mod_robotic
			trait = trait_robot_streamlined_protocols
			trait = trait_robot_luxurious
		}
		extra_trait_points = 3
		allow_negative_traits = no
		namelist = MACHINE3
		effect = {
			save_event_target_as = rg_fallen_maid_species
		}
	}
}

rg_create_fallen_empire_country_effect = {
	create_country = {
		name = 德萨努-纳迪沙协议
		type = fallen_empire
		flag = {
			icon = {
				category = "special"
				file = "gray_goo.dds"
			}
			background = {
				category = "backgrounds"
				file = "sinus.dds"
			}
			colors = {
				"grey"
				"dark_grey"
				"null"
				"null"
			}
		}
		ignore_initial_colony_error = yes
		authority = auth_machine_intelligence
		civics = {
			civic = civic_machine_remnants
			civic = civic_custodian_directives
		}
		species = last_created_species
		ethos = {
			ethic = ethic_gestalt_consciousness
		}
		origin = "origin_fallen_empire"
		effect = {
			set_built_species = last_created_species
			save_global_event_target_as = rg_scion_fe
			set_country_flag = rg_fallen_empire_machine
			add_resource = {
				minerals = 10000
				energy = 10000
				food = 1000
				influence = 500
			}
			if = {
				limit = { host_has_dlc = "Apocalypse" }
				give_technology = {
					tech = tech_colossus
					message = no
				}
			}
			# must initialize global designs here
			add_global_ship_design = "NAME_Gray_Omega"
			add_global_ship_design = "NAME_Gray_Alpha"
			add_global_ship_design = "NAME_Gray_Beta"
			add_global_ship_design = "NAME_Gray_Gamma"
			add_global_ship_design = "NAME_Gray_Theta"
			add_global_ship_design = "NAME_Gray_Tau"
			add_global_ship_design = "NAME_Gray_Sigma"
			add_global_ship_design = "NAME_Gray_FE_Starbase"
			set_graphical_culture = fallen_machine_empire_01

			add_modifier = {
				modifier = fe_ship_cost_modifier
				days = -1
			}

			if = {
				limit = {
					NOT = {
						exists = event_target:rg_gray_country_leader
					}
				}
				rg_gray_country_leader_init = yes
				set_leader = last_created_leader
			}
			else = {
				set_leader = last_created_leader
			}
			ruler = {
				add_skill = 9
			}
		}
	}
}

rg_create_fallen_empire_reinforcements = {
	create_fleet = {
		effect = {
			set_owner = prev.owner
			while = {
				count = 4
				rg_create_fallen_empire_large_ship = yes
			}
			while = {
				count = 8
				rg_create_fallen_empire_small_ship = yes
			}
			random_list = {
				25 = {
					set_location = {
						target = prev
						distance = 30
						angle = random
					}
				}
				25 = {
					set_location = {
						target = prev
						distance = 45
						angle = random
					}
				}
				25 = {
					set_location = {
						target = prev
						distance = 60
						angle = random
					}
				}
				25 = {
					set_location = {
						target = prev
						distance = 75
						angle = random
					}
				}
			}
		}
	}
}

rg_create_fallen_empire_fleet = {
	owner = {
		create_leader = {
			class = commander
			species = owner_main_species
			name = random
			skill = 10
			leader_age_min = 25
			leader_age_max = 45
		}
	}
	create_fleet = {
		effect = {
			set_owner = prev.owner
			while = {
				count = 8
				rg_create_fallen_empire_large_ship = yes
			}
			while = {
				count = 18
				rg_create_fallen_empire_small_ship = yes
			}
			random_list = {
				25 = {
					set_location = {
						target = prev
						distance = 30
						angle = random
					}
				}
				25 = {
					set_location = {
						target = prev
						distance = 45
						angle = random
					}
				}
				25 = {
					set_location = {
						target = prev
						distance = 60
						angle = random
					}
				}
				25 = {
					set_location = {
						target = prev
						distance = 75
						angle = random
					}
				}
			}
		}
	}
	last_created_fleet = { assign_leader = last_created_leader }
}

rg_create_fallen_empire_starting_navy = {
	rg_create_fallen_empire_fleet = yes
	rg_create_fallen_empire_fleet = yes
	random = {
		chance = 33
		rg_create_fallen_empire_fleet = yes
	}
	if = {
		limit = {
			host_has_dlc = "Apocalypse"
		}
		random = {
			chance = 10
			create_fleet = {
				effect = {
					set_owner = prev.owner
					rg_create_fallen_empire_colossus = yes
					set_location = {
						target = prev
						distance = 30
						angle = random
					}
				}
			}
		}
	}
	random = {
		chance = 25
		create_fleet = {
			effect = {
				set_owner = prev.owner
				rg_create_fallen_empire_titan = yes
				set_location = {
					target = prev
					distance = 20
					angle = random
				}
			}
		}
	}
	create_fleet = {
		effect = {
			set_owner = prev.owner
			rg_create_fallen_empire_construction_ship = yes
			set_location = {
				target = prev
				distance = 60
				angle = random
			}
		}
	}
	create_fleet = {
		effect = {
			set_owner = prev.owner
			rg_create_fallen_empire_construction_ship = yes
			set_location = {
				target = prev
				distance = 80
				angle = random
			}
		}
	}
	create_fleet = {
		effect = {
			set_owner = prev.owner
			rg_create_fallen_empire_construction_ship = yes
			set_location = {
				target = prev
				distance = 100
				angle = random
			}
		}
	}
}

rg_create_fallen_empire_platform = {
	if = {
		limit = {
			owner = { is_machine_empire = yes }
		}
		create_ship = {
			name = random
			design = "NAME_Gray_Sigma"
			graphical_culture = owner
		}
	}
}

rg_create_fallen_empire_colossus = {
	if = {
		limit = {
			owner = { is_machine_empire = yes }
		}
		create_ship = {
			name = random
			design = "NAME_Gray_Omega"
			graphical_culture = owner
		}
	}
}

rg_create_fallen_empire_titan = {
	if = {
		limit = {
			owner = { is_machine_empire = yes }
		}
		create_ship = {
			name = random
			design = "NAME_Gray_Alpha"
			graphical_culture = owner
		}
	}
}

rg_create_fallen_empire_large_ship = {
	if = {
		limit = {
			owner = { is_machine_empire = yes }
		}
		create_ship = {
			name = random
			design = "NAME_Gray_Beta"
			graphical_culture = owner
		}
	}
}

rg_create_fallen_empire_small_ship = {
	if = {
		limit = {
			owner = { is_machine_empire = yes }
		}
		create_ship = {
			name = random
			design = "NAME_Gray_Gamma"
			graphical_culture = owner
		}
	}
}

rg_create_fallen_empire_construction_ship = {
	if = {
		limit = {
			owner = { is_machine_empire = yes }
		}
		create_ship = {
			name = random
			design = "NAME_Gray_Theta"
			graphical_culture = owner
		}
	}
}

rg_create_fe_citadel = {
	create_starbase = {
		size = "starbase_citadel"

		module = "shipyard"
		module = "anchorage"
		module = "anchorage"
		module = "hangar_bay"
		module = "gun_battery"
		module = "gun_battery"

		building = "crew_quarters"
		building = "command_center"
		building = "target_uplink_computer"
		building = "dark_matter_listening_post"

		effect = {
			set_owner = event_target:rg_scion_fe
			while = {
				count = 4
				rg_create_fallen_empire_platform = yes
			}
		}
	}
}

#scope = scion
rg_create_scion_wormhole_link = {
	capital_scope = {
		solar_system = { save_event_target_as = home_system }
	}
	event_target:rg_scion_master = {
		random_system_within_border = { save_event_target_as = fe_target_system }
	}
	event_target:home_system = {
		event_target:fe_target_system = {
			prev = { # done this way to ensure we don't spawn "orphan wormholes (wormholes that aren't linked to anything)
				spawn_natural_wormhole = {
					bypass_type = wormhole
					random_pos = no
					orbit_angle = 360
				}
			}
			spawn_natural_wormhole = {
				bypass_type = wormhole
				random_pos = no
				orbit_angle = 270
			}
			link_wormholes = prev
		}
	}
	set_country_flag = encountered_first_wormhole
	add_seen_bypass_type = wormhole
}

rg_lcluster_spawn_effect_scion = {
	set_global_flag = rg_origin_scion_l_cluster
	set_spawn_system_batch = begin
	no_scope = {
		# makes system positions originate from galactic core
		spawn_system = {
			min_distance >= 550
			max_distance <= 560
			min_orientation_angle = 44
			max_orientation_angle = 46
			initializer = rg_cluster_init_01
			hyperlane = no
		}
		random_system = {
			limit = {
				has_star_flag = lcluster1
			}
			save_global_event_target_as = lcluster1
			spawn_system = {
				min_distance >= 29
				max_distance <= 31
				min_orientation_angle = 24
				max_orientation_angle = 66
				initializer = rg_cluster_init_02
			}
			spawn_system = {
				min_distance >= 29
				max_distance <= 31
				min_orientation_angle = 114
				max_orientation_angle = 156
				initializer = rg_cluster_init_03
			}
			spawn_system = {
				min_distance >= 30
				max_distance <= 60
				min_orientation_angle = 340
				max_orientation_angle = 20
				initializer = rg_cluster_init_01b
			}
		}
		random_system = {
			limit = {
				has_star_flag = lcluster2
			}
			save_global_event_target_as = lcluster2
			spawn_system = {
				min_distance >= 30
				max_distance <= 60
				min_orientation_angle = 0
				max_orientation_angle = 40
				initializer = rg_cluster_init_02b
			}
		}
		random_system = {
			limit = {
				has_star_flag = lcluster3
			}
			save_global_event_target_as = lcluster3
			spawn_system = {
				min_distance >= 20
				max_distance <= 30
				min_orientation_angle = 24
				max_orientation_angle = 66
				initializer = rg_fallen_machine
			}
			spawn_system = {
				min_distance >= 10
				max_distance <= 50
				min_orientation_angle = 250
				max_orientation_angle = 290
				initializer = rg_cluster_init_03b
			}
		}
		random_system = {
			limit = {
				has_star_flag = lcluster4
			}
			save_global_event_target_as = lcluster4
			spawn_system = {
				min_distance >= 5
				max_distance <= 10
				min_orientation_angle = 250
				max_orientation_angle = 290
				initializer = rg_cluster_init_05
			}
			spawn_system = {
				min_distance >= 30
				max_distance <= 60
				min_orientation_angle = 20
				max_orientation_angle = 160
				initializer = rg_cluster_init_04b
			}
		}
	}
	random_system = {
		limit = {
			has_star_flag = lcluster5
		}
		save_global_event_target_as = lcluster5
	}
	random_system = {
		limit = {
			has_star_flag = lcluster1b
		}
		save_global_event_target_as = lcluster1b
	}
	random_system = {
		limit = {
			has_star_flag = lcluster2b
		}
		save_global_event_target_as = lcluster2b
	}
	random_system = {
		limit = {
			has_star_flag = lcluster3b
		}
		save_global_event_target_as = lcluster3b
	}
	random_system = {
		limit = {
			has_star_flag = lcluster4b
		}
		save_global_event_target_as = lcluster4b
	}
	# clear all hyperlanes to and within the cluster
	every_system = {
		limit = {
			has_star_flag = lcluster
		}
		isolate_system = yes
	}
	# add hyperlanes
	event_target:lcluster5 = {
		add_hyperlane = {
			from = event_target:lcluster5
			to = event_target:lcluster4
		}
	}
	event_target:lcluster4 = {
		add_hyperlane = {
			from = event_target:lcluster4
			to = event_target:lcluster3
		}
		add_hyperlane = {
			from = event_target:lcluster4
			to = event_target:lcluster2
		}
		add_hyperlane = {
			from = event_target:lcluster4
			to = event_target:lcluster4b
		}
	}
	event_target:lcluster3 = {
		add_hyperlane = {
			from = event_target:lcluster3
			to = event_target:lcluster1
		}
		add_hyperlane = {
			from = event_target:lcluster3
			to = event_target:lcluster3b
		}
	}
	event_target:lcluster2 = {
		add_hyperlane = {
			from = event_target:lcluster2
			to = event_target:lcluster1
		}
		add_hyperlane = {
			from = event_target:lcluster2
			to = event_target:lcluster3
		}
		add_hyperlane = {
			from = event_target:lcluster2
			to = event_target:lcluster2b
		}
	}
	event_target:lcluster1 = {
		add_hyperlane = {
			from = event_target:lcluster1
			to = event_target:lcluster1b
		}
	}
	event_target:lcluster1b = {
		add_hyperlane = {
			from = event_target:lcluster1b
			to = event_target:lcluster2b
		}
	}
	set_spawn_system_batch = end
}