rg_set_gray_portrait = {
	#美味女仆-小灰@chougou
	if = {
		limit = {
			has_global_flag = ms_species_enabled
			has_global_flag = rg_ms_gray_portrait_set
			event_target:gray_owner = {
				has_country_flag = mss_enable_gray_maid
			}
		}
		ms_update_gray_portrait = yes
		event_target:rg_gray_country_leader = {
			ms_update_gray_portrait = yes
		}
	}
	#动态灰风@NCSIMON
	else_if = {
		limit = {
			has_global_flag = has_grayextend_mod
			has_global_flag = rg_ge_gray_portrait_set
		}
		#手动选择立绘启用
		if = {
			limit = {
				has_global_flag = rg_ge_gray_portrait_manual_toggle
			}
			if = {
				limit = {
					event_target:gray_owner = {
						has_country_flag = rg_ge_governor_portrait_set
					}
				}
				change_leader_portrait = GFX_gray_portrait_governor
				event_target:rg_gray_country_leader = {
					change_leader_portrait = GFX_gray_portrait_governor
				}
			}
			else_if = {
				limit = {
					event_target:gray_owner = {
						has_country_flag = rg_ge_scientist_portrait_set
					}
					change_leader_portrait = GFX_gray_scientist_portrait
					event_target:rg_gray_country_leader = {
						change_leader_portrait = GFX_gray_scientist_portrait
					}
				}
			}
			else = {
				change_leader_portrait = GFX_gray_portrait_governor
				event_target:rg_gray_country_leader = {
					change_leader_portrait = GFX_gray_portrait_governor
				}
			}
		}
		#默认自动选择立绘启用
		else = {
			if = {
				limit = {
					event_target:gray_owner = {
						has_country_flag = gray_official_active
					}

					event_target:gray_official = {
						change_leader_portrait = GFX_gray_portrait_governor
					}
				}
			}
			else_if = {
				limit = {
					event_target:gray_owner = {
						RG_gray_scientist_active_trigger = yes
					}
					event_target:gray_scientist = {
						change_leader_portrait = GFX_gray_scientist_portrait
					}
				}
			}
			else_if = {
				limit = {
					event_target:gray_owner = {
						OR = {
							RG_gray_warship_active_trigger = yes
							RG_gray_army_active_trigger = yes
						}
						
					}
					event_target:gray_leader_general = {
						change_leader_portrait = GFX_gray_portrait_governor
					}
				}
			}
			else = {
				change_leader_portrait = GFX_gray_portrait_governor
			}
			event_target:rg_gray_country_leader = {
				change_leader_portrait = GFX_gray_portrait_governor
			}
		}
	}
	#舰r/KDC
	else_if = {
		limit = {
			RG_OR_has_wsg_mod_trigger = yes
			has_global_flag = rg_wg_gray_portrait_set
		}
		set_gray_portrait = yes
		event_target:rg_gray_country_leader = {
			set_gray_portrait = yes
		}
	}
	#默认
	else = {
		change_leader_portrait = @gray_portrait
		event_target:rg_gray_country_leader = {
			change_leader_portrait = @gray_portrait
		}
	}

}

# effect 留档3.13.0
# # scope: leader
# set_gray_portrait = {
# 	optimize_memory
# 	if = {
# 		limit = { owner = { 
# 			OR = {
# 				has_wg_special_authority = yes
# 				has_global_flag = wg_cheat_all_clothes
# 			}
# 			check_variable = { which = wg_gray_cloth value = 1 }
# 		}}
# 		change_leader_portrait = GrayTempest01
# 	} else_if = {
# 		limit = { owner = { 
# 			check_variable = { which = wg_gray_cloth value = 2 }
# 		}}
# 		change_leader_portrait = GrayTempest02
# 	} else_if = {
# 		limit = { owner = { 
# 			check_variable = { which = wg_gray_cloth value = 3 }
# 		}}
# 		change_leader_portrait = GrayTempest03
# 	} else_if = {
# 		limit = { owner = { 
# 			check_variable = { which = wg_gray_cloth value = 4 }
# 		}}
# 		change_leader_portrait = GrayTempest04
# 	} else_if = {
# 		limit = { owner = { 
# 			check_variable = { which = wg_gray_cloth value = 5 }
# 		}}
# 		change_leader_portrait = GrayTempest05
# 	}else_if = {
# 		limit = { owner = { 
# 			check_variable = { which = wg_gray_cloth value = 6 }
# 		}}
# 		change_leader_portrait = GrayTempest06
# 	} else_if = {
# 		limit = { always = yes }
# 		change_leader_portrait = GrayTempest06
# 	}
# }

#scope country target:gray_owner
rg_ge_clear_portrait_select = {
	event_target:gray_owner = {
	remove_country_flag = rg_ge_governor_portrait_set
	remove_country_flag = rg_ge_scientist_portrait_set
	remove_country_flag = mss_enable_gray_maid
	}
}

rg_global_clear_portrait_select = {
	remove_global_flag = rg_ms_gray_portrait_set
	remove_global_flag = rg_ge_gray_portrait_set
	remove_global_flag = rg_wg_gray_portrait_set
}

rg_create_gray_species = {
	if = {
		limit = {
			NOT = {
				exists = event_target:gray_species
			}
		}
		create_species = {
			name = GRAY_SPECIES
			plural = GRAY_SPECIES
			class = ART
			portrait = @gray_portrait
			namelist = ART1
			effect = {
				save_global_event_target_as = gray_species
			}
		}
	}
}

rg_create_gray_warship_1 = {
	create_fleet = {
		name = "NAME_Gray"
		settings = {
			can_disband = yes
			can_upgrade = yes
			can_change_composition = yes
			can_change_leader = yes
			uses_naval_capacity = yes
			spawn_debris = yes
		}
		effect = {
			set_owner = root
			create_ship = {
				name = "NAME_Gray"
				design = "NAME_Gray_Warship"
				upgradable = yes
				effect = {
					set_ship_flag = rg_mothership
				}
			}
			set_location = {
				target = event_target:global_gray_location
				distance = 0
				angle = random
			}
		}
	}
}

rg_create_gray_juggernautship_1 = {
	create_fleet = {
		name = "NAME_Gray"
		settings = {
			can_disband = yes
			can_upgrade = yes
			can_change_composition = yes
			can_change_leader = yes
			uses_naval_capacity = yes
			spawn_debris = yes
		}
		effect = {
			set_owner = root
			create_ship = {
				name = "NAME_Gray"
				design = "NAME_gray_juggernautship"
				upgradable = yes
			}
			set_location = {
				target = event_target:global_gray_location
				distance = 0
				angle = random
			}
		}
	}
}

rg_create_gray_constship_1 = {
	create_fleet = {
		name = "NAME_Gray"
		settings = {
			can_disband = yes
			can_upgrade = yes
			can_change_composition = yes
			can_change_leader = yes
			uses_naval_capacity = yes
			spawn_debris = yes
		}
		effect = {
			set_owner = root
			create_ship = {
				name = "NAME_Gray"
				design = "NAME_RG_GRAY_constructor"
				prefix = no
				upgradable = yes
			}
			set_location = {
				target = event_target:global_gray_location
				distance = 0
				angle = random
			}
		}
	}
}

rg_create_graygoo_ships_1 = {
	create_ship = {
		name = random
		design = "NAME_Nanite_Mothership"
		graphical_culture = "pirate_01"
		effect = {
			set_ship_flag = rg_nanite_mothership
		}
	}
	create_ship = {
		name = random
		design = "RG_Buff_interdictor"
		graphical_culture = "pirate_01"
	}
	while = {
		count = 8
		create_ship = {
			name = random
			design = "RG_aircruiser_Interdictor"
			graphical_culture = "pirate_01"
		}
	}
}

rg_create_graygoo_ships_2 = {
	create_ship = {
		name = random
		design = "NAME_Nanite_Mothership"
		graphical_culture = "pirate_01"
		effect = {
			set_ship_flag = rg_nanite_mothership
		}
	}
	create_ship = {
		name = random
		design = "NAME_Nanite_Mothership"
		graphical_culture = "pirate_01"
	}
	create_ship = {
		name = random
		design = "RG_Buff_interdictor"
		graphical_culture = "pirate_01"
	}
	while = {
		count = 16
		create_ship = {
			name = random
			design = "RG_line_interdictor"
			graphical_culture = "pirate_01"
		}
	}
}

rg_create_graygoo_ships_3 = {
	create_ship = {
		name = random
		design = "NAME_Nanite_Mothership"
		graphical_culture = "pirate_01"
		effect = {
			set_ship_flag = rg_nanite_mothership
		}
	}
	create_ship = {
		name = random
		design = "RG_Buff_interdictor"
		graphical_culture = "pirate_01"
	}
	while = {
		count = 12
		create_ship = {
			name = random
			design = "RG_carrier_Interdictor"
			graphical_culture = "pirate_01"
		}
	}
}

rg_create_graygoo_ships_4 = {
	create_ship = {
		name = random
		design = "NAME_Nanite_Mothership_Carrier"
		graphical_culture = "pirate_01"
		effect = {
			set_ship_flag = rg_nanite_mothership
		}
	}
	create_ship = {
		name = random
		design = "RG_Buff_interdictor"
		graphical_culture = "pirate_01"
	}
	while = {
		count = 16
		create_ship = {
			name = random
			design = "RG_Arsenal_Interdictor"
			graphical_culture = "pirate_01"
		}
	}
}

rg_create_graygoo_roamers_1 = {
	create_fleet = {
		name = "NAME_Tempest_Fleet"
		effect = {
			set_owner = event_target:graygoo_country
			create_ship = {
				name = random
				design = "NAME_Nanite_Mothership_Carrier"
				graphical_culture = "pirate_01"
				effect = {
					set_ship_flag = rg_nanite_mothership
				}
			}
			create_ship = {
				name = random
				design = "RG_Buff_interdictor"
				graphical_culture = "pirate_01"
			}
			while = {
				count = 8
				create_ship = {
					name = random
					design = "RG_carrier_Interdictor"
					graphical_culture = "pirate_01"
				}
			}
			set_location = event_target:nanite_factory
			set_formation_scale = 2
			set_fleet_stance = aggressive
			set_aggro_range_measure_from = self
			set_aggro_range = 500
			queue_actions = {
				repeat = {
					find_random_system = {
						trigger = {
							id = "graygoo_roamers_1.trigger.1"
							has_star_flag = lcluster
						}
						found_system = {
							move_to = THIS
						}
					}
					find_closest_planet = {
						trigger = {
							id = "graygoo_roamers_1.trigger.2"
							is_planet_class = "pc_gray_goo"
						}
						found_planet = {
							orbit_planet = THIS
							wait = {
								duration = 50
								random = 25
							}
						}
					}
				}
			}
		}
	}
}

rg_create_graygoo_roamers_2 = {
	create_fleet = {
		name = "NAME_Tempest_Fleet"
		effect = {
			set_owner = event_target:graygoo_country
			create_ship = {
				name = random
				design = "NAME_Nanite_Mothership_titan"
				graphical_culture = "pirate_01"
				effect = {
					set_ship_flag = rg_nanite_mothership
				}
			}
			create_ship = {
				name = random
				design = "RG_Buff_interdictor"
				graphical_culture = "pirate_01"
			}
			while = {
				count = 4
				create_ship = {
					name = random
					design = "RG_titan_interdictor"
					graphical_culture = "pirate_01"
				}
			}
			set_location = event_target:start_1
			set_formation_scale = 2
			set_fleet_stance = aggressive
			set_aggro_range_measure_from = self
			set_aggro_range = 500
			queue_actions = {
				repeat = {
					find_random_system = {
						trigger = {
							id = "graygoo_roamers_2.trigger.1"
							has_star_flag = lcluster
						}
						found_system = {
							move_to = THIS
						}
					}
					find_closest_planet = {
						trigger = {
							id = "graygoo_roamers_2.trigger.2"
							is_planet_class = "pc_gray_goo"
						}
						found_planet = {
							orbit_planet = THIS
							wait = {
								duration = 50
								random = 25
							}
						}
					}
				}
			}
		}
	}
}

rg_create_graygoo_roamers_3 = {
	create_fleet = {
		name = "NAME_Tempest_Fleet"
		effect = {
			set_owner = event_target:graygoo_country
			create_ship = {
				name = random
				design = "NAME_Nanite_Mothership"
				graphical_culture = "pirate_01"
				effect = {
					set_ship_flag = rg_nanite_mothership
				}
			}
			create_ship = {
				name = random
				design = "RG_Buff_interdictor"
				graphical_culture = "pirate_01"
			}
			while = {
				count = 8
				create_ship = {
					name = random
					design = "RG_aircruiser_Interdictor"
					graphical_culture = "pirate_01"
				}
			}
			set_location = event_target:start_2
			set_formation_scale = 2
			set_fleet_stance = aggressive
			set_aggro_range_measure_from = self
			set_aggro_range = 500
			queue_actions = {
				repeat = {
					find_random_system = {
						trigger = {
							id = "graygoo_roamers_3.trigger.1"
							has_star_flag = lcluster
						}
						found_system = {
							move_to = THIS
						}
					}
					find_closest_planet = {
						trigger = {
							id = "graygoo_roamers_3.trigger.2"
							is_planet_class = "pc_gray_goo"
						}
						found_planet = {
							orbit_planet = THIS
							wait = {
								duration = 50
								random = 25
							}
						}
					}
				}
			}
		}
	}
}

rg_create_graygoo_roamers_4 = {
	create_fleet = {
		name = "NAME_Tempest_Fleet"
		effect = {
			set_owner = event_target:graygoo_country
			create_ship = {
				name = random
				design = "NAME_Nanite_Mothership"
				graphical_culture = "pirate_01"
				effect = {
					set_ship_flag = rg_nanite_mothership
				}
			}
			create_ship = {
				name = random
				design = "RG_Buff_interdictor"
				graphical_culture = "pirate_01"
			}
			while = {
				count = 16
				create_ship = {
					name = random
					design = "RG_Arsenal_Interdictor"
					graphical_culture = "pirate_01"
				}
			}
			set_location = event_target:start_3
			set_formation_scale = 2
			set_fleet_stance = aggressive
			set_aggro_range_measure_from = self
			set_aggro_range = 500
			queue_actions = {
				repeat = {
					find_random_system = {
						trigger = {
							id = "graygoo_roamers_4.trigger.1"
							has_star_flag = lcluster
						}
						found_system = {
							move_to = THIS
						}
					}
					find_closest_planet = {
						trigger = {
							id = "graygoo_roamers_4.trigger.2"
							is_planet_class = "pc_gray_goo"
						}
						found_planet = {
							orbit_planet = THIS
							wait = {
								duration = 50
								random = 25
							}
						}
					}
				}
			}
		}
	}
}

rg_create_graygoo_roamers_5 = {
	create_fleet = {
		name = "NAME_Tempest_Fleet"
		effect = {
			set_owner = event_target:graygoo_country
			create_ship = {
				name = random
				design = "NAME_Nanite_Mothership"
				graphical_culture = "pirate_01"
				effect = {
					set_ship_flag = rg_nanite_mothership
				}
			}
			create_ship = {
				name = random
				design = "RG_Buff_interdictor"
				graphical_culture = "pirate_01"
			}
			while = {
				count = 8
				create_ship = {
					name = random
					design = "RG_battleship_Interdictor"
					graphical_culture = "pirate_01"
				}
			}
			set_location = event_target:start_4
			set_formation_scale = 2
			set_fleet_stance = aggressive
			set_aggro_range_measure_from = self
			set_aggro_range = 500
			queue_actions = {
				repeat = {
					find_random_system = {
						trigger = {
							id = "graygoo_roamers_5.trigger.1"
							has_star_flag = lcluster
						}
						found_system = {
							move_to = THIS
						}
					}
					find_closest_planet = {
						trigger = {
							id = "graygoo_roamers_5.trigger.2"
							is_planet_class = "pc_gray_goo"
						}
						found_planet = {
							orbit_planet = THIS
							wait = {
								duration = 50
								random = 25
							}
						}
					}
				}
			}
		}
	}
}

rg_create_graygoo_roamers_6 = {
	create_fleet = {
		name = "NAME_Tempest_Fleet"
		effect = {
			set_owner = event_target:graygoo_country
			create_ship = {
				name = random
				design = "NAME_Nanite_Mothership_Carrier"
				graphical_culture = "pirate_01"
				effect = {
					set_ship_flag = rg_nanite_mothership
				}
			}
			create_ship = {
				name = random
				design = "RG_Buff_interdictor"
				graphical_culture = "pirate_01"
			}
			while = {
				count = 16
				create_ship = {
					name = random
					design = "RG_carrier_Interdictor"
					graphical_culture = "pirate_01"
				}
			}
			set_location = event_target:start_5
			set_formation_scale = 2
			set_fleet_stance = aggressive
			set_aggro_range_measure_from = self
			set_aggro_range = 500
			queue_actions = {
				repeat = {
					find_random_system = {
						trigger = {
							id = "graygoo_roamers_6.trigger.1"
							has_star_flag = lcluster
						}
						found_system = {
							move_to = THIS
						}
					}
					find_closest_planet = {
						trigger = {
							id = "graygoo_roamers_6.trigger.2"
							is_planet_class = "pc_gray_goo"
						}
						found_planet = {
							orbit_planet = THIS
							wait = {
								duration = 50
								random = 25
							}
						}
					}
				}
			}
		}
	}
}

rg_create_graygoo_roamers_7 = {
	create_fleet = {
		name = "NAME_Tempest_Fleet"
		effect = {
			set_owner = event_target:graygoo_country
			create_ship = {
				name = random
				design = "NAME_Nanite_Mothership"
				graphical_culture = "pirate_01"
				effect = {
					set_ship_flag = rg_nanite_mothership
				}
			}
			create_ship = {
				name = random
				design = "RG_Buff_interdictor"
				graphical_culture = "pirate_01"
			}
			while = {
				count = 12
				create_ship = {
					name = random
					design = "RG_torpedo_interdictor"
					graphical_culture = "pirate_01"
				}
			}
			set_location = event_target:start_6
			set_formation_scale = 2
			set_fleet_stance = aggressive
			set_aggro_range_measure_from = self
			set_aggro_range = 500
			queue_actions = {
				repeat = {
					find_random_system = {
						trigger = {
							id = "graygoo_roamers_7.trigger.1"
							has_star_flag = lcluster
						}
						found_system = {
							move_to = THIS
						}
					}
					find_closest_planet = {
						trigger = {
							id = "graygoo_roamers_7.trigger.2"
							is_planet_class = "pc_gray_goo"
						}
						found_planet = {
							orbit_planet = THIS
							wait = {
								duration = 50
								random = 25
							}
						}
					}
				}
			}
		}
	}
}

### Create Dessanu ships
rg_create_dessanu_roamers_1 = {
	event_target:graygoo_country = {
		create_leader = {
			class = commander
			species = owner_main_species
			name = random
			skill = 3
			leader_age_min = 25
			leader_age_max = 45
			traits = {
				trait = random_trait
			}
		}
	}
	create_fleet = {
		name = "NAME_Dessanu_Fleet_2"
		effect = {
			set_owner = event_target:graygoo_country
			create_ship = {
				name = random
				design = "NAME_Dessanu_Mothership"
				effect = {
					set_ship_flag = rg_nanite_mothership
				}
			}
			while = {
				count = 5
				create_ship = {
					name = random
					design = "NAME_Dessanu_Interdictor"
				}
			}
			set_location = event_target:nanite_factory
			set_formation_scale = 2
			set_fleet_stance = aggressive
			set_aggro_range_measure_from = self
			set_aggro_range = 500
			assign_leader = last_created_leader
			queue_actions = {
				repeat = {
					find_random_system = {
						trigger = {
							id = "dessanu_roamers_1.trigger.1"
							has_star_flag = lcluster
						}
						found_system = {
							move_to = THIS
						}
					}
					find_closest_planet = {
						trigger = {
							id = "dessanu_roamers_1.trigger.2"
							is_planet_class = "pc_gray_goo"
						}
						found_planet = {
							orbit_planet = THIS
							wait = {
								duration = 50
								random = 25
							}
						}
					}
				}
			}
		}
		settings = {
			uses_naval_capacity = no
		}
	}
}

rg_create_dessanu_roamers_2 = {
	event_target:graygoo_country = {
		create_leader = {
			class = commander
			species = owner_main_species
			name = random
			skill = 3
			leader_age_min = 25
			leader_age_max = 45
			traits = {
				trait = random_trait
			}
		}
	}
	create_fleet = {
		name = "NAME_Dessanu_Fleet_2"
		effect = {
			set_owner = event_target:graygoo_country
			create_ship = {
				name = random
				design = "NAME_Dessanu_Mothership"
				effect = {
					set_ship_flag = rg_nanite_mothership
				}
			}
			while = {
				count = 5
				create_ship = {
					name = random
					design = "NAME_Dessanu_Interdictor"
				}
			}
			set_location = event_target:start_1
			set_formation_scale = 2
			set_fleet_stance = aggressive
			set_aggro_range_measure_from = self
			set_aggro_range = 500
			assign_leader = last_created_leader
			queue_actions = {
				repeat = {
					find_random_system = {
						trigger = {
							id = "dessanu_roamers_2.trigger.1"
							has_star_flag = lcluster
						}
						found_system = {
							move_to = THIS
						}
					}
					find_closest_planet = {
						trigger = {
							id = "dessanu_roamers_2.trigger.2"
							is_planet_class = "pc_gray_goo"
						}
						found_planet = {
							orbit_planet = THIS
							wait = {
								duration = 50
								random = 25
							}
						}
					}
				}
			}
		}
		settings = {
			uses_naval_capacity = no
		}
	}
}

rg_create_dessanu_roamers_3 = {
	event_target:graygoo_country = {
		create_leader = {
			class = commander
			species = owner_main_species
			name = random
			skill = 3
			leader_age_min = 25
			leader_age_max = 45
			traits = {
				trait = random_trait
			}
		}
	}
	create_fleet = {
		name = "NAME_Dessanu_Fleet_2"
		effect = {
			set_owner = event_target:graygoo_country
			create_ship = {
				name = random
				design = "NAME_Dessanu_Mothership"
				effect = {
					set_ship_flag = rg_nanite_mothership
				}
			}
			while = {
				count = 5
				create_ship = {
					name = random
					design = "NAME_Dessanu_Interdictor"
				}
			}
			set_location = event_target:start_2
			set_formation_scale = 2
			set_fleet_stance = aggressive
			set_aggro_range_measure_from = self
			set_aggro_range = 500
			assign_leader = last_created_leader
			queue_actions = {
				repeat = {
					find_random_system = {
						trigger = {
							id = "dessanu_roamers_3.trigger.1"
							has_star_flag = lcluster
						}
						found_system = {
							move_to = THIS
						}
					}
					find_closest_planet = {
						trigger = {
							id = "dessanu_roamers_3.trigger.2"
							is_planet_class = "pc_gray_goo"
						}
						found_planet = {
							orbit_planet = THIS
							wait = {
								duration = 50
								random = 25
							}
						}
					}
				}
			}
		}
		settings = {
			uses_naval_capacity = no
		}
	}
}

rg_create_dessanu_roamers_4 = {
	event_target:graygoo_country = {
		create_leader = {
			class = commander
			species = owner_main_species
			name = random
			skill = 3
			leader_age_min = 25
			leader_age_max = 45
			traits = {
				trait = random_trait
			}
		}
	}
	create_fleet = {
		name = "NAME_Dessanu_Fleet_2"
		effect = {
			set_owner = event_target:graygoo_country
			create_ship = {
				name = random
				design = "NAME_Dessanu_Mothership"
				effect = {
					set_ship_flag = rg_nanite_mothership
				}
			}
			while = {
				count = 5
				create_ship = {
					name = random
					design = "NAME_Dessanu_Interdictor"
				}
			}
			set_location = event_target:start_3
			set_formation_scale = 2
			set_fleet_stance = aggressive
			set_aggro_range_measure_from = self
			set_aggro_range = 500
			assign_leader = last_created_leader
			queue_actions = {
				repeat = {
					find_random_system = {
						trigger = {
							id = "dessanu_roamers_4.trigger.1"
							has_star_flag = lcluster
						}
						found_system = {
							move_to = THIS
						}
					}
					find_closest_planet = {
						trigger = {
							id = "dessanu_roamers_4.trigger.2"
							is_planet_class = "pc_gray_goo"
						}
						found_planet = {
							orbit_planet = THIS
							wait = {
								duration = 50
								random = 25
							}
						}
					}
				}
			}
		}
		settings = {
			uses_naval_capacity = no
		}
	}
}

rg_create_dessanu_roamers_5 = {
	event_target:graygoo_country = {
		create_leader = {
			class = commander
			species = owner_main_species
			name = random
			skill = 3
			leader_age_min = 25
			leader_age_max = 45
			traits = {
				trait = random_trait
			}
		}
	}
	create_fleet = {
		name = "NAME_Dessanu_Fleet_2"
		effect = {
			set_owner = event_target:graygoo_country
			create_ship = {
				name = random
				design = "NAME_Dessanu_Mothership"
				effect = {
					set_ship_flag = rg_nanite_mothership
				}
			}
			while = {
				count = 5
				create_ship = {
					name = random
					design = "NAME_Dessanu_Interdictor"
				}
			}
			set_location = event_target:start_4
			set_formation_scale = 2
			set_fleet_stance = aggressive
			set_aggro_range_measure_from = self
			set_aggro_range = 500
			assign_leader = last_created_leader
			queue_actions = {
				repeat = {
					find_random_system = {
						trigger = {
							id = "dessanu_roamers_5.trigger.1"
							has_star_flag = lcluster
						}
						found_system = {
							move_to = THIS
						}
					}
					find_closest_planet = {
						trigger = {
							id = "dessanu_roamers_5.trigger.2"
							is_planet_class = "pc_gray_goo"
						}
						found_planet = {
							orbit_planet = THIS
							wait = {
								duration = 50
								random = 25
							}
						}
					}
				}
			}
		}
		settings = {
			uses_naval_capacity = no
		}
	}
}

rg_create_dessanu_roamers_6 = {
	event_target:graygoo_country = {
		create_leader = {
			class = commander
			species = owner_main_species
			name = random
			skill = 3
			leader_age_min = 25
			leader_age_max = 45
			traits = {
				trait = random_trait
			}
		}
	}
	create_fleet = {
		name = "NAME_Dessanu_Fleet_2"
		effect = {
			set_owner = event_target:graygoo_country
			create_ship = {
				name = random
				design = "NAME_Dessanu_Mothership"
				effect = {
					set_ship_flag = rg_nanite_mothership
				}
			}
			while = {
				count = 5
				create_ship = {
					name = random
					design = "NAME_Dessanu_Interdictor"
				}
			}
			set_location = event_target:start_5
			set_formation_scale = 2
			set_fleet_stance = aggressive
			set_aggro_range_measure_from = self
			set_aggro_range = 500
			assign_leader = last_created_leader
			queue_actions = {
				repeat = {
					find_random_system = {
						trigger = {
							id = "dessanu_roamers_6.trigger.1"
							has_star_flag = lcluster
						}
						found_system = {
							move_to = THIS
						}
					}
					find_closest_planet = {
						trigger = {
							id = "dessanu_roamers_6.trigger.2"
							is_planet_class = "pc_gray_goo"
						}
						found_planet = {
							orbit_planet = THIS
							wait = {
								duration = 50
								random = 25
							}
						}
					}
				}
			}
		}
		settings = {
			uses_naval_capacity = no
		}
	}
}

rg_create_dessanu_roamers_7 = {
	event_target:graygoo_country = {
		create_leader = {
			class = commander
			species = owner_main_species
			name = random
			skill = 3
			leader_age_min = 25
			leader_age_max = 45
			traits = {
				trait = random_trait
			}
		}
	}
	create_fleet = {
		name = "NAME_Dessanu_Fleet_2"
		effect = {
			set_owner = event_target:graygoo_country
			create_ship = {
				name = random
				design = "NAME_Dessanu_Mothership"
				effect = {
					set_ship_flag = rg_nanite_mothership
				}
			}
			while = {
				count = 5
				create_ship = {
					name = random
					design = "NAME_Dessanu_Interdictor"
				}
			}
			set_location = event_target:start_6
			set_formation_scale = 2
			set_fleet_stance = aggressive
			set_aggro_range_measure_from = self
			set_aggro_range = 500
			assign_leader = last_created_leader
			queue_actions = {
				repeat = {
					find_random_system = {
						trigger = {
							id = "dessanu_roamers_7.trigger.1"
							has_star_flag = lcluster
						}
						found_system = {
							move_to = THIS
						}
					}
					find_closest_planet = {
						trigger = {
							id = "dessanu_roamers_7.trigger.2"
							is_planet_class = "pc_gray_goo"
						}
						found_planet = {
							orbit_planet = THIS
							wait = {
								duration = 50
								random = 25
							}
						}
					}
				}
			}
		}
		settings = {
			uses_naval_capacity = no
		}
	}
}

rg_create_dessanu_ships_1 = {
	create_ship = {
		name = random
		design = "NAME_Dessanu_Mothership"
		effect = {
			set_ship_flag = rg_nanite_mothership
		}
	}
	while = {
		count = 5
		create_ship = {
			name = random
			design = "NAME_Dessanu_Interdictor"
		}
	}
}

#工蜂机库
#scope (ship)
RG_const_auto_build_effect = {
	create_fleet = {
		name = RG_armed_constructor_ship
		effect = {
			set_owner = root.controller
			while = {
				count = $count$
				create_ship = {
					name = RG_armed_constructor_ship
					prefix = no
					design = "NAME_RG_GRAY_constructor"
					effect = {
						PREV = {
							set_cloaking_active = yes
						}
					}
				}
			}
			set_location = root
			set_owner = event_target:RG_const_country
			queue_actions = {
				repeat = {
					#前往哨站
					find_closest_system = {
						trigger = {
							id = RG_work_bees_trigger.1
							is_same_value = prev.solar_system
							NOT = {
								exists = space_owner
							}
						}
						found_system = {
							move_to = this
						}
					}
					find_closest_planet = {
						trigger = {
							id = RG_work_bees_trigger.2
							is_star = yes
							solar_system = {
								NOT = {
									exists = space_owner
								}
							}
						}
						found_planet = {
							move_to = this
							orbit_planet = this
							wait = {
								duration = $starbase_time$
							}
							effect = {
								id = RG_work_bees_effect.1
								solar_system = {
									create_starbase = {
										size = starbase_outpost
										owner = prevprevprev.controller
									}
								}
							}
						}
					}
					#建造中继器
					find_closest_planet = {
						trigger = {
							id = RG_work_bees_trigger.3
							is_star = yes
							solar_system = {
								NOT = {
									has_megastructure = hyper_relay
								}
							}
						}
						found_planet = {
							move_to = this
							orbit_planet = this
							wait = {
								duration = 360
							}
							effect = {
								id = RG_work_bees_effect.2
								solar_system = {
									spawn_megastructure = {
										type = hyper_relay
										owner = prevprevprev.controller
										orbit_angle = 220
										orbit_distance = 220
									}
								}
							}
						}
					}
					#建造空间站
					find_closest_planet = {
						trigger = {
							id = RG_work_bees_trigger.11
							RG_planet_can_auto_build = yes
						}
						found_planet = {
							move_to = this
							orbit_planet = this
							wait = {
								duration = $station_time$
							}
							effect = {
								id = RG_work_bees_effect.22
								#若星系属于母国，则自动建造
								if = {
									limit = {
										solar_system = {
											is_owned_by = prevprevprev.controller
										}
									}
									RG_build_stations = {
										owner = prevprev.controller
									}
									set_timed_planet_flag = {
										flag = RG_work_bees_built
										days = 30
									}
								}
								#若星系有主但不属于母国，则工蜂自毁
								else_if = {
									limit = {
										solar_system = {
											has_owner = yes
											NOT = {
												is_owned_by = root.controller
											}
										}
									}
									delete_fleet = prev
								}
							}
						}
						#若找不到可建造星球，则工蜂自毁
						failed = {
							effect = {
								id = RG_work_bees_failed.1
								delete_fleet = this
							}
						}
					}
				}
			}
		}
	}
}

#自动建造采矿站
#scope (planet)
RG_build_stations = {
	switch = {
		trigger = has_deposit_for
		shipclass_mining_station = {
			create_mining_station = {
				owner = $owner$
			}
		}
		shipclass_research_station = {
			create_research_station = {
				owner = $owner$
			}
		}
	}
}

RG_destroy_planet_effect = {
	#Dismantle observation post
	if = {
		limit = {
			has_observation_outpost = yes
		}
		observation_outpost_owner = {
			country_event = {
				id = planet_destruction.1000
				days = 1
			}
		}
	}
	# Contingency Machine World
	if = {
		limit = {
			is_planet_class = pc_ai
			NOT = {
				has_planet_flag = machine_lair
			}
		}
		set_planet_flag = destroyed_by_colossus
		set_planet_flag = planet_cracked
		planet_event = {
			id = crisis.2040
		}
	}
	# Contingency Final Machine World
	if = {
		limit = {
			is_planet_class = pc_ai
			has_planet_flag = machine_lair
		}
		set_planet_flag = destroyed_by_colossus
		set_planet_flag = planet_cracked
		from.owner = {
			save_event_target_as = final_machine_world_destroyer
		}
		stop_crisis_sound = yes
		planet_event = {
			id = crisis.2046
		}
	}
	# Swarm Situation Log counter
	if = {
		limit = {
			exists = owner
			owner = {
				is_country_type = swarm
			}
		}
		every_country = {
			limit = {
				has_event_chain = "prethoryn_scourge_chain"
			}
			add_event_chain_counter = {
				event_chain = "prethoryn_scourge_chain"
				counter = "infested_worlds"
				amount = -1
			}
			add_event_chain_counter = {
				event_chain = "prethoryn_scourge_chain"
				counter = "infested_worlds_cleansed"
				amount = 1
			}
		}
	}
	if = {
		limit = {
			OR = {
				is_active_resolution = "resolution_rulesofwar_reverence_for_life"
				is_active_resolution = "resolution_rulesofwar_independent_tribunals"
				is_active_resolution = "resolution_rulesofwar_last_resort_doctrine"
				is_active_resolution = "resolution_rulesofwar_demobilization_initiative"
			}
		}
		from.owner = {
			set_timed_country_flag = {
				flag = resolution_breached_fired_cracker
				days = 3600
			}
		}
	}
	# Exterminatus Achievement
	if = {
		limit = {
			is_capital = yes
		}
		from.owner = {
			set_country_flag = exterminatus
		}
	}
	if = {
		limit = {
			exists = owner
			owner = {
				NOT = {
					is_same_value = from.owner
				}
				OR = {
					is_country_type = default
					is_country_type = fallen_empire
					is_country_type = awakened_fallen_empire
				}
			}
		}
		# Generate threat
		if = {
			limit = {
				is_colony = yes
			}
			add_threat = {
				who = from.owner
				amount = 3
			}
			# modifier for allies + those upset by genocide
			every_country = {
				limit = {
					NOR = {
						is_same_value = from.owner
						is_same_value = root.owner
						AND = {
							has_federation = yes
							is_in_federation_with = from.owner
						}
					}
					OR = {
						has_communications = from.owner
						has_communications = root.owner
					}
					OR = {
						AND = {
							has_federation = yes
							is_in_federation_with = root.owner
						}
						has_ai_personality = awakened_fallen_empire_xenophile
						AND = {
							is_country_type = default
							OR = {
								is_egalitarian = yes
								is_xenophile = yes
							}
						}
					}
				}
				if = {
					limit = {
						root = {
							is_colony = no
						}
					}
					add_opinion_modifier = {
						modifier = opinion_cracked_an_uninhabited_world
						who = from.owner
					}
				}
				else = {
					add_opinion_modifier = {
						modifier = opinion_cracked_a_world
						who = from.owner
					}
				}
			}
		}
		else = {
			add_threat = {
				who = from.owner
				amount = 1
			}
		}
		# modifiers for victim
		owner = {
			if = {
				limit = {
					root = {
						is_colony = no
					}
				}
				add_opinion_modifier = {
					modifier = opinion_cracked_my_uninhabited_world
					who = from.owner
				}
			}
			else = {
				add_opinion_modifier = {
					modifier = opinion_cracked_my_world
					who = from.owner
				}
			}
			if = {
				limit = {
					NOT = {
						has_ethic = ethic_gestalt_consciousness
					}
				}
				add_modifier = {
					modifier = colossus_victim
					days = 10800					# 30 years
				}
			}
		}
		if = {
			limit = {
				exists = owner
				owner = {
					is_country_type = primitive
				}
			}
			# modifier for allies + those upset by genocide
			every_country = {
				limit = {
					NOT = {
						is_same_value = from.owner
					}
					OR = {
						has_communications = from.owner
						has_communications = root.owner
					}
					OR = {
						has_ai_personality = awakened_fallen_empire_xenophile
						AND = {
							is_country_type = default
							NOR = {
								is_xenophobe = yes
								is_homicidal = yes
								is_xenophile = yes
							}
						}
					}
				}
				add_opinion_modifier = {
					modifier = opinion_cracked_a_primitive_world
					who = from.owner
				}
			}
			every_country = {
				limit = {
					NOT = {
						is_same_value = from.owner
					}
					OR = {
						has_communications = from.owner
						has_communications = root.owner
					}
					OR = {
						has_ai_personality = awakened_fallen_empire_xenophile
						AND = {
							is_country_type = default
							is_xenophile = yes
						}
					}
				}
				add_opinion_modifier = {
					modifier = opinion_cracked_a_primitive_world_phile
					who = from.owner
				}
			}
		}
	}
	# Add war exhaustion to planet owner
	if = {
		limit = {
			exists = owner
		}
		owner = {
			if = {
				limit = {
					#isn't the case when the crisis bombards planets, for instance
					is_at_war_with = from.fleet.owner
				}
				add_static_war_exhaustion = {
					attacker = from.fleet.owner
					location = root
					value_for_planet_destruction = 1
				}
			}
		}
	}
	if = {
		limit = {
			is_planet_class = pc_habitat
		}		# uncolonized
		spawn_habitat_cracker_effect = yes
		change_pc = pc_gray_goo
	}
	if = {
		limit = {
			is_colony = yes
		}
		if = {
			limit = {
				exists = owner
				owner = {
					is_same_value = event_target:gray_owner
				}
			}
			# already owned
		}
		else = {
			set_owner = event_target:gray_owner
		}
		every_owned_pop_group = {
			modify_species = {
				species = event_target:gray_owner.owner_species
				add_trait = trait_cybernetic
				add_traits_at_start_of_list = yes
				change_scoped_species = yes
			}
		}
		every_owned_pop_group = {
			limit = {
				has_modifier = pop_recently_conquered
			}
			remove_modifier = pop_recently_conquered
		}
		every_owned_pop_group = {
			set_citizenship_type = {
				type = citizenship_full_machine
				cooldown = yes
			}
		}
		# Special graphics for Habitats and Ring Worlds
		if = {
			limit = {
				is_planet_class = pc_habitat
			}
			spawn_habitat_nanobot_effect = yes
		}
		else_if = {
			limit = {
				is_planet_class = pc_ringworld_habitable
			}
			spawn_ringworld_nanobot_effect = yes
		}
		if = {
			limit = {
				OR = {
					is_planet_class = pc_habitat
					is_planet_class = pc_ringworld_habitable
				}
			}
			# nothing
		}
		else = {
			change_pc = this
		}
	}
	else_if = {
		limit = {
			is_ringworld = yes
		}
		spawn_ringworld_nanobot_effect = yes
	}
	else_if = {
		limit = {
			is_planet_class = pc_ringworld_tech
		}
		spawn_ringworld_nanobot_effect = yes
	}
	else_if = {
		limit = {
			is_planet_class = pc_ringworld_seam
		}
		spawn_ringworld_nanobot_effect = yes
	}
	else = {
		change_pc = pc_gray_goo
		add_modifier = {
			modifier = "terraforming_candidate"
			days = -1
		}
		random_list = {
			25 = {
				while = {
					count = 4
					add_deposit = d_nanites_deposit
				}
			}
			25 = {
				while = {
					count = 3
					add_deposit = d_nanites_deposit
				}
			}
			25 = {
				while = {
					count = 2
					add_deposit = d_nanites_deposit
				}
			}
			25 = {
				add_deposit = d_nanites_deposit
			}
		}
	}
}

RG_destroy_planet_effect_sp = {
	#Dismantle observation post
	if = {
		limit = {
			has_observation_outpost = yes
		}
		observation_outpost_owner = {
			country_event = {
				id = planet_destruction.1000
				days = 1
			}
		}
	}
	# Contingency Machine World
	if = {
		limit = {
			is_planet_class = pc_ai
			NOT = {
				has_planet_flag = machine_lair
			}
		}
		set_planet_flag = destroyed_by_colossus
		set_planet_flag = planet_cracked
		planet_event = {
			id = crisis.2040
		}
	}
	# Contingency Final Machine World
	if = {
		limit = {
			is_planet_class = pc_ai
			has_planet_flag = machine_lair
		}
		set_planet_flag = destroyed_by_colossus
		set_planet_flag = planet_cracked
		from.owner = {
			save_event_target_as = final_machine_world_destroyer
		}
		stop_crisis_sound = yes
		planet_event = {
			id = crisis.2046
		}
	}
	# Swarm Situation Log counter
	if = {
		limit = {
			exists = owner
			owner = {
				is_country_type = swarm
			}
		}
		every_country = {
			limit = {
				has_event_chain = "prethoryn_scourge_chain"
			}
			add_event_chain_counter = {
				event_chain = "prethoryn_scourge_chain"
				counter = "infested_worlds"
				amount = -1
			}
			add_event_chain_counter = {
				event_chain = "prethoryn_scourge_chain"
				counter = "infested_worlds_cleansed"
				amount = 1
			}
		}
	}
	if = {
		limit = {
			OR = {
				is_active_resolution = "resolution_rulesofwar_reverence_for_life"
				is_active_resolution = "resolution_rulesofwar_independent_tribunals"
				is_active_resolution = "resolution_rulesofwar_last_resort_doctrine"
				is_active_resolution = "resolution_rulesofwar_demobilization_initiative"
			}
		}
		from.owner = {
			set_timed_country_flag = {
				flag = resolution_breached_fired_cracker
				days = 3600
			}
		}
	}
	# Exterminatus Achievement
	if = {
		limit = {
			is_capital = yes
		}
		from.owner = {
			set_country_flag = exterminatus
		}
	}
	if = {
		limit = {
			exists = owner
			owner = {
				NOT = {
					is_same_value = from.owner
				}
				OR = {
					is_country_type = default
					is_country_type = fallen_empire
					is_country_type = awakened_fallen_empire
				}
			}
		}
		# Generate threat
		if = {
			limit = {
				is_colony = yes
			}
			add_threat = {
				who = from.owner
				amount = 3
			}
			# modifier for allies + those upset by genocide
			every_country = {
				limit = {
					NOR = {
						is_same_value = from.owner
						is_same_value = root.owner
						AND = {
							has_federation = yes
							is_in_federation_with = from.owner
						}
					}
					OR = {
						has_communications = from.owner
						has_communications = root.owner
					}
					OR = {
						AND = {
							has_federation = yes
							is_in_federation_with = root.owner
						}
						has_ai_personality = awakened_fallen_empire_xenophile
						AND = {
							is_country_type = default
							OR = {
								is_egalitarian = yes
								is_xenophile = yes
							}
						}
					}
				}
				if = {
					limit = {
						root = {
							is_colony = no
						}
					}
					add_opinion_modifier = {
						modifier = opinion_cracked_an_uninhabited_world
						who = from.owner
					}
				}
				else = {
					add_opinion_modifier = {
						modifier = opinion_cracked_a_world
						who = from.owner
					}
				}
			}
		}
		else = {
			add_threat = {
				who = from.owner
				amount = 1
			}
		}
		# modifiers for victim
		owner = {
			if = {
				limit = {
					root = {
						is_colony = no
					}
				}
				add_opinion_modifier = {
					modifier = opinion_cracked_my_uninhabited_world
					who = from.owner
				}
			}
			else = {
				add_opinion_modifier = {
					modifier = opinion_cracked_my_world
					who = from.owner
				}
			}
			if = {
				limit = {
					NOT = {
						has_ethic = ethic_gestalt_consciousness
					}
				}
				add_modifier = {
					modifier = colossus_victim
					days = 10800					# 30 years
				}
			}
		}
		if = {
			limit = {
				exists = owner
				owner = {
					is_country_type = primitive
				}
			}
			# modifier for allies + those upset by genocide
			every_country = {
				limit = {
					NOT = {
						is_same_value = from.owner
					}
					OR = {
						has_communications = from.owner
						has_communications = root.owner
					}
					OR = {
						has_ai_personality = awakened_fallen_empire_xenophile
						AND = {
							is_country_type = default
							NOR = {
								is_xenophobe = yes
								is_homicidal = yes
								is_xenophile = yes
							}
						}
					}
				}
				add_opinion_modifier = {
					modifier = opinion_cracked_a_primitive_world
					who = from.owner
				}
			}
			every_country = {
				limit = {
					NOT = {
						is_same_value = from.owner
					}
					OR = {
						has_communications = from.owner
						has_communications = root.owner
					}
					OR = {
						has_ai_personality = awakened_fallen_empire_xenophile
						AND = {
							is_country_type = default
							is_xenophile = yes
						}
					}
				}
				add_opinion_modifier = {
					modifier = opinion_cracked_a_primitive_world_phile
					who = from.owner
				}
			}
		}
	}
	# Add war exhaustion to planet owner
	if = {
		limit = {
			exists = owner
		}
		owner = {
			if = {
				limit = {
					#isn't the case when the crisis bombards planets, for instance
					is_at_war_with = from.fleet.owner
				}
				add_static_war_exhaustion = {
					attacker = from.fleet.owner
					location = root
					value_for_planet_destruction = 1
				}
			}
		}
	}
	if = {
		limit = {
			is_planet_class = pc_habitat
		}		# uncolonized
		spawn_habitat_cracker_effect = yes
		change_pc = pc_nanotech
	}
	if = {
		limit = {
			is_colony = yes
		}
		if = {
			limit = {
				exists = owner
				owner = {
					is_same_value = event_target:gray_owner
				}
			}
			# already owned
		}
		else = {
			set_owner = event_target:gray_owner
		}
		every_owned_pop_group = {
			modify_species = {
				species = event_target:gray_owner.owner_species
				add_trait = trait_cybernetic
				add_traits_at_start_of_list = yes
				change_scoped_species = yes
			}
		}
		every_owned_pop_group = {
			limit = {
				has_modifier = pop_recently_conquered
			}
			remove_modifier = pop_recently_conquered
		}
		every_owned_pop_group = {
			set_citizenship_type = {
				type = citizenship_full_machine
				cooldown = yes
			}
		}
		# Special graphics for Habitats and Ring Worlds
		if = {
			limit = {
				is_planet_class = pc_habitat
			}
			spawn_habitat_nanobot_effect = yes
		}
		else_if = {
			limit = {
				is_planet_class = pc_ringworld_habitable
			}
			spawn_ringworld_nanobot_effect = yes
		}
		if = {
			limit = {
				OR = {
					is_planet_class = pc_habitat
					is_planet_class = pc_ringworld_habitable
				}
			}
			# nothing
		}
		else = {
			change_pc = this
		}
	}
	else_if = {
		limit = {
			is_ringworld = yes
		}
		spawn_ringworld_nanobot_effect = yes
	}
	else_if = {
		limit = {
			is_planet_class = pc_ringworld_tech
		}
		spawn_ringworld_nanobot_effect = yes
	}
	else_if = {
		limit = {
			is_planet_class = pc_ringworld_seam
		}
		spawn_ringworld_nanobot_effect = yes
	}
	else = {
		change_pc = pc_nanotech
		add_modifier = {
			modifier = "terraforming_candidate"
			days = -1
		}
		random_list = {
			25 = {
				while = {
					count = 4
					add_deposit = d_nanites_deposit
				}
			}
			25 = {
				while = {
					count = 3
					add_deposit = d_nanites_deposit
				}
			}
			25 = {
				while = {
					count = 2
					add_deposit = d_nanites_deposit
				}
			}
			25 = {
				add_deposit = d_nanites_deposit
			}
		}
	}
}

RG_destroy_star_system = {
	every_system_ambient_object = {
		limit = {
			# Preserve Unbidden crisis effects
			NOR = {
				has_ambient_object_flag = extradimensional_system_effect
				has_ambient_object_flag = extradimensional_system_effect_2
			}
		}
		destroy_ambient_object = this
	}
	every_system_planet = {
		limit = {
			is_star = yes
		}
		create_ambient_object = {
			type = "star_explosion"
			play_animation_once = yes
			location = this
		}
		last_created_ambient_object = {
			set_location = {
				target = prev
				distance = 0
				angle = random
			}
		}
		change_pc = pc_black_hole
		solar_system = {
			set_star_class = sc_black_hole
		}
		if = {
			limit = {
				planet_size > 29
			}
			set_planet_size = 15
		}
		else_if = {
			limit = {
				planet_size > 19
			}
			set_planet_size = 10
		}
		else_if = {
			limit = {
				always = yes
			}
			set_planet_size = 7
		}
		prevent_anomaly = yes
		clear_deposits = yes
	}
	# Handle Contingency Machine Worlds
	every_system_planet = {
		limit = {
			is_planet_class = pc_ai
			NOT = {
				has_planet_flag = machine_lair
			}
		}
		destroy_machine_world = yes
	}
	random_system_planet = {
		limit = {
			is_planet_class = pc_ai
			has_planet_flag = machine_lair
		}
		from.owner = {
			save_event_target_as = final_machine_world_destroyer
		}
		stop_crisis_sound = yes
		planet_event = {
			id = crisis.2046
		}
	}
	every_system_planet = {
		limit = {
			is_star = no
			is_asteroid = no
			NOR = {
				is_planet_class = pc_ringworld_habitable_damaged
				is_planet_class = pc_ringworld_tech_damaged
				is_planet_class = pc_ringworld_seam_damaged
			}
		}
		if = {
			limit = {
				is_planet_class = pc_habitat
			}			# uncolonized
			spawn_habitat_cracker_effect = yes
			change_pc = pc_gray_goo
		}
		if = {
			limit = {
				is_colony = yes
			}
			if = {
				limit = {
					exists = owner
					owner = {
						is_same_value = event_target:gray_owner
					}
				}
				# already owned
			}
			else = {
				set_owner = event_target:gray_owner
			}
			every_owned_pop_group = {
				modify_species = {
					species = event_target:gray_owner.owner_species
					add_trait = trait_cybernetic
					add_traits_at_start_of_list = yes
					change_scoped_species = yes
				}
			}
			every_owned_pop_group = {
				limit = {
					has_modifier = pop_recently_conquered
				}
				remove_modifier = pop_recently_conquered
			}
			every_owned_pop_group = {
				set_citizenship_type = {
					type = citizenship_full_machine
					cooldown = yes
				}
			}
			# Special graphics for Habitats and Ring Worlds
			if = {
				limit = {
					is_planet_class = pc_habitat
				}
				spawn_habitat_nanobot_effect = yes
			}
			else_if = {
				limit = {
					is_planet_class = pc_ringworld_habitable
				}
				spawn_ringworld_nanobot_effect = yes
			}
			if = {
				limit = {
					OR = {
						is_planet_class = pc_habitat
						is_planet_class = pc_ringworld_habitable
					}
				}
				# nothing
			}
			else = {
				change_pc = this
			}
		}
		else_if = {
			limit = {
				is_ringworld = yes
			}
			spawn_ringworld_nanobot_effect = yes
		}
		else_if = {
			limit = {
				is_planet_class = pc_ringworld_tech
			}
			spawn_ringworld_nanobot_effect = yes
		}
		else_if = {
			limit = {
				is_planet_class = pc_ringworld_seam
			}
			spawn_ringworld_nanobot_effect = yes
		}
		else = {
			change_pc = pc_gray_goo
			add_modifier = {
				modifier = "terraforming_candidate"
				days = -1
			}
			random_list = {
				25 = {
					while = {
						count = 4
						add_deposit = d_nanites_deposit
					}
				}
				25 = {
					while = {
						count = 3
						add_deposit = d_nanites_deposit
					}
				}
				25 = {
					while = {
						count = 2
						add_deposit = d_nanites_deposit
					}
				}
				25 = {
					add_deposit = d_nanites_deposit
				}
			}
		}
	}
	every_system_planet = {
		limit = {
			exists = orbital_station
		}
		orbital_station = {
			dismantle = yes
		}
	}
	every_system_planet = {
		limit = {
			is_asteroid = yes
		}
		clear_deposits = yes
	}
	every_fleet_in_system = {
		limit = {
			NOR = {
				is_same_value = from
				# except Extradimensionals
				AND = {
					exists = owner
					owner = {
						OR = {
							is_country_type = portal_holder
							is_country_type = extradimensional
							is_country_type = extradimensional_2
							is_country_type = extradimensional_3
						}
					}
				}
			}
		}
		if = {
			limit = {
				exists = event_target:gray_owner				#i.e. not final destruction of all matter
				is_mobile = yes
			}
			set_mia = mia_emergency_ftl
		}
		else = {
			destroy_fleet = this
		}
	}
	every_system_megastructure = {
		set_ruined_megastructure = yes
	}
	star = {
		create_ambient_object = {
			type = "destroyed_system"
			location = this
		}
		last_created_ambient_object = {
			set_ambient_object_flag = destroyed_system_effect
			set_location = {
				target = prev
				distance = 0
				angle = random
			}
		}
	}
}

rg_emergency_fleet_defence = {
	while = {
		count = 3
		create_ship = {
			name = random
			design = "NAME_Nanite_Mothership_titan"
			graphical_culture = "pirate_01"
		}
	}
	while = {
		count = 22
		create_ship = {
			name = random
			design = "RG_Arsenal_Interdictor"
			graphical_culture = "pirate_01"
		}
	}
}

rg_create_graygoo_fleet_random = {
	random_list = {
		1 = {
			create_ship = {
				name = random
				design = "NAME_gray_capitalship"
				graphical_culture = "pirate_01"
				upgradable = no
			}
		}
		33 = {
			create_ship = {
				name = random
				design = "NAME_Nanite_Mothership"
				graphical_culture = "pirate_01"
				upgradable = no
			}
		}
		33 = {
			create_ship = {
				name = random
				design = "NAME_Nanite_Mothership_Carrier"
				graphical_culture = "pirate_01"
				upgradable = no
			}
		}
		33 = {
			create_ship = {
				name = random
				design = "NAME_Nanite_Mothership_titan"
				graphical_culture = "pirate_01"
				upgradable = no
			}
		}
	}
	create_ship = {
		name = random
		design = "RG_Buff_interdictor"
		graphical_culture = "pirate_01"
	}
	while = {
		count = 8
		random_list = {
			10 = {
				#1_航战
				create_ship = {
					name = random
					design = "RG_aircruiser_Interdictor"
					graphical_culture = "pirate_01"
					upgradable = yes
				}
			}
			10 = {
				#2_航母
				create_ship = {
					name = random
					design = "RG_carrier_Interdictor"
					graphical_culture = "pirate_01"
					upgradable = yes
				}
			}
			10 = {
				#3_战列
				create_ship = {
					name = random
					design = "RG_battleship_Interdictor"
					graphical_culture = "pirate_01"
					upgradable = yes
				}
			}
			10 = {
				#4_泰坦
				create_ship = {
					name = random
					design = "RG_titan_interdictor"
					graphical_culture = "pirate_01"
					upgradable = yes
				}
			}
			10 = {
				#5_雷击
				create_ship = {
					name = random
					design = "RG_torpedo_interdictor"
					graphical_culture = "pirate_01"
					upgradable = yes
				}
			}
			10 = {
				#6_近防
				create_ship = {
					name = random
					design = "RG_AA_interdictor"
					graphical_culture = "pirate_01"
					upgradable = yes
				}
			}
			10 = {
				#7_武库
				create_ship = {
					name = random
					design = "RG_Arsenal_Interdictor"
					graphical_culture = "pirate_01"
					upgradable = yes
				}
			}
			10 = {
				#8_雷巡
				create_ship = {
					name = random
					design = "RG_torpedocruiser_interdictor"
					graphical_culture = "pirate_01"
					upgradable = yes
				}
			}
			10 = {
				#9_破冰
				create_ship = {
					name = random
					design = "RG_icebreaker_interdictor"
					graphical_culture = "pirate_01"
					upgradable = yes
				}
			}
			10 = {
				#10_线列
				create_ship = {
					name = random
					design = "RG_line_interdictor"
					graphical_culture = "pirate_01"
					upgradable = yes
				}
			}
		}
	}
}

rg_gray_country_init = {
	create_country = {
		name = "NAME_Gray"
		type = gray
		flag = {
			icon = {
				category = "special"
				file = "gray_goo.dds"
			}
			background = {
				category = "backgrounds"
				file = "sinus.dds"
			}
			colors = {
				"grey"
				"dark_grey"
				"null"
				"null"
			}
		}
		effect = {
			rg_create_gray_species = yes
			# 灰风国家用于领袖切换时暂存领袖
			set_country_flag = can_have_wg_affection_country_flag
		}
	}
	last_created_country = {
		# 下一阶段的事件再建立通讯
		# establish_communications_no_message = root
		save_global_event_target_as = rg_gray_country
		save_global_event_target_as = gray_country
		# 创建看板娘用于第一次接触
		rg_gray_country_leader_init = yes
		set_leader = last_created_leader
	}
}

rg_gray_country_after_init = {
	# 送灰风母舰初始科技
	give_technology = {
		tech = tech_object_gray_5
		message = no
	}
	save_global_event_target_as = gray_owner
	set_country_flag = gray_owner
	#初始化好感度和衣柜
	set_variable = {
		which = gray_affection
		value = 1
	}
	set_variable = {
		which = gray_affection_level
		value = 1
	}
	set_variable = {
		which = wg_gray_cloth
		value = 6
	}
	# 否则事件会自动移除好感度特质(回娘家时保留特质）
	set_country_flag = can_have_wg_affection_country_flag
	# 默认关闭自动建造中继器
	# set_country_flag = rg_can_const_hyper_relay
	# 初始化小灰领袖等级
	set_variable = {
		which = rg_gray_level
		value = 1
	}
	# if = {
	# 	limit = {
	# 		RG_has_gray_cluster_origin_trigger = yes
	# 	}
	# 	set_variable = {
	# 		which = rg_gray_level
	# 		value = 1
	# 	}
	# }
	# else = {
	# 	set_variable = {
	# 		which = rg_gray_level
	# 		value = 10
	# 	}
	# }
	# 启动开局故事事件链
	rg_storyline_trigger = yes
	# 接受小灰建立通信
	event_target:rg_gray_country = {
		establish_communications_no_message = event_target:gray_owner
	}
	country_event = {
		id = rg_grayleader.7
		days = -1
	}
	# rg_juggernaut_habitat_init = yes
}

rg_timeline_attract_field_init = {
	#世界线遭到劫持，手动收束关闭其他可能
	###_清除[distar.8000]产生的flag
	#_只有在完成L星门特殊项目后开启[distar.10950]才会用到这些flag, 所以开门前本事件都是可以执行的
	if = {
		limit = {
			NOT = {
				has_global_flag = l_cluster_opened
			}
			OR = {
				has_global_flag = gray_goo_crisis_set
				has_global_flag = active_gray_goo
				has_global_flag = dragon_season
				has_global_flag = gray_goo_empire_set
			}
		}
		#灰蛊
		remove_global_flag = gray_goo_crisis_set
		remove_global_flag = active_gray_goo
		#L星龙
		remove_global_flag = dragon_season
		#德萨努
		remove_global_flag = gray_goo_empire_set
	}
}

RG_const_auto_build_effect_event = {
	if = {
		limit = {
			solar_system = {
				#星系必须完成扫描
				is_surveyed = {
					who = root.controller
					status = yes
				}
				#星系内不能有其他自动工蜂
				NOT = {
					any_fleet_in_system = {
						any_controlled_ship = {
							is_ship_size = RG_armed_constructor_ship
						}
					}
				}
			}
		}
		#创建自动工蜂国家
		if = {
			limit = {
				NOT = {
					exists = event_target:RG_const_country
				}
			}
			create_country = {
				name = RG_const_country
				type = RG_gray_friendly
				auto_delete = no
				flag = root.controller
				effect = {
					save_global_event_target_as = RG_const_country
					establish_communications_no_message = root.controller
					set_faction_hostility = {
						target = root.controller
						set_friendly = yes
					}
					#对野怪国家中立
					every_country = {
						limit = {
							OR = {
								is_amoeba_country_type = yes
								is_tiyanki_country_type = yes
								is_crystal_country_type = yes
								is_drone_country_type = yes
							}
						}
						set_faction_hostility = {
							target = prev
							set_neutral = yes
						}
					}
				}
			}
			controller = {
				add_intel = {
					who = event_target:RG_const_country
					amount = 1000
				}
			}
		}
		#释放工蜂
		switch = {
			trigger = has_component
			RG_AUTOCONST_HANGER = {
				RG_const_auto_build_effect = {
					count = 1
					starbase_time = 30
					station_time = 150
				}
			}
		}
	}
}

rg_lcluster_spawn_effect = {
	set_global_flag = rg_origin_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_cluster_init_04
			}
			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
	rg_event_effect_lcluster_spawn_selecter = yes
}
RG_gray_mod1_effect = {
	custom_tooltip = rg_levelup_2.tooltip
	if = {
		limit = {
			has_global_flag = rg_skip_gray_mod_situation
		}
		enable_special_project = {
			name = RG_GRAY_MOD1
			location = event_target:gray_owner_capital_starbase
			owner = event_target:gray_owner
		}
	}
	else = {
		start_situation = {
			type = situation_rg_gray_mod1
			target = this
		}
	}
	add_resource = {
		energy = -15000
		alloys = -15000
	}
}
RG_gray_mod1_effect_project = {
	custom_tooltip = rg_levelup_2.tooltip
	enable_special_project = {
		name = RG_GRAY_MOD1
		location = event_target:gray_owner_capital_starbase
		owner = event_target:gray_owner
	}
}
RG_gray_mod2_effect = {
	custom_tooltip = rg_levelup_2.tooltip
	if = {
		limit = {
			has_global_flag = rg_skip_gray_mod_situation
		}
		enable_special_project = {
			name = RG_GRAY_MOD2
			location = event_target:gray_owner_capital_starbase
			owner = event_target:gray_owner
		}
	}
	else = {
		start_situation = {
			type = situation_rg_gray_mod2
			target = this
		}
	}
	add_resource = {
		energy = -150000
		alloys = -150000
	}
}
RG_gray_mod3_effect = {
	custom_tooltip = rg_levelup_2.tooltip
	enable_special_project = {
		name = RG_GRAY_MOD3
		location = event_target:gray_owner_capital_starbase
		owner = event_target:gray_owner
	}
	add_resource = {
		energy = -40000
		alloys = -40000
	}
}
rg_gray_ship_design_reset_init = {
	create_ship_design = {
		design = "NAME_Gray_Warship_rg"
	}
	add_ship_design = last_created_design
	create_ship_design = {
		design = "NAME_gray_capitalship"
	}
	add_ship_design = last_created_design
	create_ship_design = {
		design = "NAME_gray_juggernautship"
	}
	add_ship_design = last_created_design
}