namespace = rg_event
country_event = {
	id = rg_event.19
	hide_window = yes
	is_triggered_only = yes
	fire_only_once = yes
	trigger = {
		is_ai = no
	}
	immediate = {
		set_global_flag = has_rg_mod
	}
}

country_event = {
	# 年检模拟龙年龄
	id = rg_event.20
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		has_country_flag = rg_fake_dragon_owner
	}
	immediate = {
		if = {
			limit = {
				NOT = {
					has_country_flag = rg_fake_dragon_reborn_country
				}
			}
			change_variable = {
				which = rg_fake_dragon_timedlife_counter
				value = 12
			}
		}
		else_if = {
			limit = {
				has_country_flag = rg_fake_dragon_reborn_country
				NOT = {
					has_country_flag = origin_gray_dragon_country
				}
			}
			change_variable = {
				which = rg_fake_dragon_timedlife_counter
				value = 1
			}
		}
		else_if = {
			limit = {
				has_country_flag = rg_fake_dragon_reborn_country
				has_country_flag = origin_gray_dragon_country
			}
			change_variable = {
				which = rg_fake_dragon_timedlife_counter
				value = 12
			}
		}
		#等级导入，把存储的领袖等级赋予到领袖身上
		random_owned_leader = {
			limit = {
				has_leader_flag = rg_fake_dragon
			}
			set_age = owner.rg_fake_dragon_timedlife_counter
			if = {
				limit = {
					has_global_flag = has_wsg_mod
				}
				change_leader_portrait = wg_dragon
			}
			else = {
				change_leader_portrait = species
			}
		}
		random_owned_fleet = {
			limit = {
				NOT = {
					has_fleet_flag = rg_fake_dragon_fleet
				}
				leader = {
					has_leader_flag = rg_fake_dragon
					NOT = {
						has_leader_flag = rg_fake_dragon_reborn
					}
				}
			}
			unassign_leader = this
		}
	}
}

# looping check for owning gateway, once every two months
country_event = {
	id = rg_event.21
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		NOR = {
			has_global_flag = l_cluster_opened
			has_special_project = RG_LCLUSTER_PROJECT
		}
	}
	immediate = {
		if = {
			limit = {
				any_owned_megastructure = {
					is_megastructure_type = lgate_base
				}
				has_technology = tech_lgate_activation
			}
			random_owned_megastructure = {
				limit = {
					is_megastructure_type = lgate_base
				}
				save_event_target_as = candidate_gateway
			}
			country_event = {
				id = distar.10905
			}
			# enable project
		}
		else = {
			country_event = {
				id = rg_event.21
				days = 120
			}
			# *THIS* looping check
		}
	}
}

# looping check for grayreforming
country_event = {
	id = rg_event.22
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		is_same_value = event_target:gray_owner
		has_country_flag = gray_reforming
	}
	immediate = {
		if = {
			limit = {
				NOT = {
					has_modifier = "gray_scattered"
				}
			}
			country_event = {
				id = graygoo.512
			}
		}
		else = {
			country_event = {
				id = rg_event.22
				years = 1
			}
			# *THIS* looping check
		}
	}
}

# 拆毁工厂小灰通讯
country_event = {
	id = rg_event.200
	title = "rg_event.200.name"
	trigger = {
		NOT = {
			any_playable_country = {
				has_country_flag = gray_owner
			}
		}
	}
	desc = {
		trigger = {
			owner = {
				is_synthetic_empire = no
			}
		}
		text = "rg_event.200.a.desc"
	}
	desc = {
		trigger = {
			owner = {
				is_synthetic_empire = yes
			}
		}
		text = "rg_event.200.b.desc"
	}
	picture = GFX_evt_ship_in_orbit_2
	show_sound = event_radio_chatter
	is_triggered_only = yes
	#immediate = {
	#	from = { save_event_target_as = gray_homeworld }
	#}
	option = {
		name = rg_event.200.a
		hidden_effect = {
			create_fleet = {
				effect = {
					set_owner = event_target:graygoo_country
					create_ship = {
						name = random
						design = "NAME_Gray_Warship"
						graphical_culture = "pirate_01"
					}
					set_location = {
						target = event_target:main_star
						distance = 55
						angle = 270
					}
					save_event_target_as = aggressive_gray
				}
			}
		}
	}
	option = {
		name = rg_event.200.b
		hidden_effect = {
			owner = {
				country_event = {
					id = rg_event.400
				}
			}
		}
	}
}

# L星龙暴毙小灰通讯
country_event = {
	id = rg_event.201
	title = "rg_event.201.name"
	trigger = {
		NOT = {
			any_playable_country = {
				has_country_flag = gray_owner
			}
		}
	}
	desc = {
		text = "rg_event.201.desc"
	}
	picture = GFX_evt_ship_in_orbit_2
	show_sound = event_radio_chatter
	is_triggered_only = yes
	#immediate = {
	#	from = { save_event_target_as = gray_homeworld }
	#}
	option = {
		name = rg_event.201.a
		hidden_effect = {
			create_fleet = {
				effect = {
					set_owner = event_target:graygoo_country
					create_ship = {
						name = random
						design = "NAME_Gray_Warship"
						graphical_culture = "pirate_01"
					}
					set_location = {
						target = event_target:main_star
						distance = 55
						angle = 270
					}
					save_event_target_as = aggressive_gray
				}
			}
		}
	}
	option = {
		name = rg_event.201.b
		hidden_effect = {
			owner = {
				country_event = {
					id = rg_event.402
				}
			}
		}
	}
}

# 灰色风暴线小灰通讯
country_event = {
	id = rg_event.400
	title = "TRANSMISSION"
	trigger = {
		NOT = {
			any_playable_country = {
				has_country_flag = gray_owner
			}
		}
	}
	desc = {
		trigger = {
			NOT = {
				has_authority = auth_machine_intelligence
			}
		}
		text = "rg_event.400.a.desc"
	}
	desc = {
		trigger = {
			has_authority = auth_machine_intelligence
		}
		text = "rg_event.400.b.desc"
	}
	diplomatic = yes
	picture_event_data = {
		portrait = root.species
		room = "ethic_spaceship_room"
	}
	is_triggered_only = yes
	option = {
		name = rg_event.400.a
		trigger = {
			NOR = {
				has_authority = auth_machine_intelligence
				has_authority = auth_hive_mind
			}
		}
		hidden_effect = {
			country_event = {
				id = graygoo.406
			}
		}
	}
	option = {
		name = rg_event.400.b
		trigger = {
			has_authority = auth_machine_intelligence
		}
		hidden_effect = {
			country_event = {
				id = graygoo.406
			}
		}
	}
	option = {
		name = rg_event.400.c
		trigger = {
			has_authority = auth_hive_mind
		}
		hidden_effect = {
			country_event = {
				id = graygoo.406
			}
		}
	}
	option = {
		name = rg_event.400.d
		hidden_effect = {
			country_event = {
				id = rg_event.401
			}
		}
	}
}

country_event = {
	id = rg_event.401
	title = "TRANSMISSION"
	desc = "rg_event.401.desc"
	diplomatic = yes
	picture_event_data = {
		portrait = root.species
		room = "ethic_spaceship_room"
	}
	is_triggered_only = yes
	option = {
		name = rg_event.401.a
		response_text = rg_event.401.a.response
	}
	option = {
		name = rg_event.401.b
		hidden_effect = {
			create_fleet = {
				effect = {
					set_owner = event_target:graygoo_country
					create_ship = {
						name = random
						design = "NAME_Gray_Warship"
						graphical_culture = "pirate_01"
					}
					set_location = {
						target = event_target:main_star
						distance = 55
						angle = 270
					}
					save_event_target_as = aggressive_gray
				}
			}
		}
	}
}

# L星龙线小灰通讯
country_event = {
	id = rg_event.402
	title = "TRANSMISSION"
	desc = {
		text = "rg_event.402.a.desc"
	}
	diplomatic = yes
	picture_event_data = {
		portrait = root.species
		room = "ethic_spaceship_room"
	}
	is_triggered_only = yes
	option = {
		name = rg_event.402.a
		hidden_effect = {
			country_event = {
				id = rg_event.403
			}
		}
	}
	option = {
		name = rg_event.402.b
		hidden_effect = {
			country_event = {
				id = rg_event.401
			}
		}
	}
}

# L星龙线小灰通讯回答
country_event = {
	id = rg_event.403
	title = "TRANSMISSION"
	desc = "rg_event.403.desc"
	diplomatic = yes
	picture_event_data = {
		portrait = root.species
		room = "ethic_spaceship_room"
	}
	is_triggered_only = yes
	option = {
		name = rg_event.403.a
		response_text = rg_event.403.a.response
		default_hide_option = yes
		hidden_effect = {
			country_event = {
				id = graygoo.499
			}
		}
	}
}

#快速防御
country_event = {
	id = rg_event.404
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		random_owned_ship = {
			limit = {
				is_ship_size = graygoo_factory
			}
			save_event_target_as = graygoo_factory
		}
		create_fleet = {
			name = "NAME_Tempest_Fleet"
			effect = {
				set_owner = root
				rg_emergency_fleet_defence = yes
				set_formation_scale = 2
				set_fleet_stance = aggressive
				set_aggro_range_measure_from = self
				set_aggro_range = 250
				set_location = {
					target = event_target:graygoo_factory
					distance = 10
					angle = random
				}
				fleet_event = {
					id = rg_event.406
					days = 30
					random = 5
				}
			}
		}
		if = {
			limit = {
				num_fleets < 60
			}
			create_fleet = {
				name = "NAME_Tempest_Fleet"
				effect = {
					set_owner = root
					rg_emergency_fleet_defence = yes
					set_formation_scale = 2
					set_fleet_stance = aggressive
					set_aggro_range_measure_from = self
					set_aggro_range = 250
					set_location = {
						target = event_target:graygoo_factory
						distance = 10
						angle = random
					}
					fleet_event = {
						id = rg_event.406
						days = 30
						random = 5
					}
				}
			}
		}
		if = {
			limit = {
				num_fleets < 30
			}
			create_fleet = {
				name = "NAME_Tempest_Fleet"
				effect = {
					set_owner = root
					rg_emergency_fleet_defence = yes
					set_formation_scale = 2
					set_fleet_stance = aggressive
					set_aggro_range_measure_from = self
					set_aggro_range = 250
					set_location = {
						target = event_target:graygoo_factory
						distance = 10
						angle = random
					}
					fleet_event = {
						id = rg_event.406
						days = 30
						random = 5
					}
				}
			}
		}
	}
}

#舰队增援-触发
country_event = {
	id = rg_event.405
	hide_window = yes
	trigger = {
		is_country_type = gray_goo
	}
	mean_time_to_happen = {
		days = 180
	}
	immediate = {
		every_owned_fleet = {
			limit = {
				any_ship = {
					is_ship_size = graygoo_mothership
				}
			}
			fleet_event = {
				id = rg_event.406
				days = 4
				random = 3
			}
		}
	}
}

#母舰增援-天灾
fleet_event = {
	id = rg_event.406
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		#_end game
		if = {
			limit = {
				end_game_years_passed >= -10
				num_ships < 20
			}
			random_list = {
				#_100%
				20 = {
					create_ship = {
						name = random
						design = "NAME_Nanite_Mothership"
						graphical_culture = "pirate_01"
						upgradable = no
					}
				}
				10 = {
					create_ship = {
						name = random
						design = "RG_aircruiser_Interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
				10 = {
					create_ship = {
						name = random
						design = "RG_carrier_Interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
				10 = {
					create_ship = {
						name = random
						design = "RG_battleship_Interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
				10 = {
					create_ship = {
						name = random
						design = "RG_titan_interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
				10 = {
					create_ship = {
						name = random
						design = "RG_torpedo_interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
				10 = {
					create_ship = {
						name = random
						design = "RG_AA_interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
				10 = {
					create_ship = {
						name = random
						design = "RG_Arsenal_Interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
				10 = {
					create_ship = {
						name = random
						design = "RG_torpedocruiser_interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
			}
		}
		#mid game
		else_if = {
			limit = {
				end_game_years_passed < -10
				mid_game_years_passed >= -10
				num_ships < 15
			}
			random_list = {
				#_85%
				10 = {
					create_ship = {
						name = random
						design = "RG_aircruiser_Interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
				10 = {
					create_ship = {
						name = random
						design = "RG_carrier_Interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
				10 = {
					create_ship = {
						name = random
						design = "RG_battleship_Interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
				10 = {
					create_ship = {
						name = random
						design = "RG_titan_interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
				10 = {
					create_ship = {
						name = random
						design = "RG_torpedo_interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
				10 = {
					create_ship = {
						name = random
						design = "RG_AA_interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
				10 = {
					create_ship = {
						name = random
						design = "RG_Arsenal_Interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
				10 = {
					create_ship = {
						name = random
						design = "RG_torpedocruiser_interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
				5 = {
					create_ship = {
						name = random
						design = "NAME_Nanite_Mothership"
						graphical_culture = "pirate_01"
						upgradable = no
					}
				}
			}
		}
		#before mid game
		else_if = {
			limit = {
				mid_game_years_passed < -10
				num_ships < 10
			}
			random_list = {
				#45%
				55 = {
				}
				5 = {
					create_ship = {
						name = random
						design = "NAME_Nanite_Mothership"
						graphical_culture = "pirate_01"
						upgradable = no
					}
				}
				5 = {
					create_ship = {
						name = random
						design = "RG_aircruiser_Interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
				5 = {
					create_ship = {
						name = random
						design = "RG_carrier_Interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
				5 = {
					create_ship = {
						name = random
						design = "RG_battleship_Interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
				5 = {
					create_ship = {
						name = random
						design = "RG_titan_interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
				5 = {
					create_ship = {
						name = random
						design = "RG_torpedo_interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
				5 = {
					create_ship = {
						name = random
						design = "RG_AA_interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
				5 = {
					create_ship = {
						name = random
						design = "RG_Arsenal_Interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
				5 = {
					create_ship = {
						name = random
						design = "RG_torpedocruiser_interdictor"
						graphical_culture = "pirate_01"
						upgradable = yes
					}
				}
			}
		}
	}
}

#中期增援
country_event = {
	id = rg_event.407
	hide_window = yes
	trigger = {
		is_country = event_target:global_event_country
		end_game_years_passed < -10		#后期
		mid_game_years_passed >= -10		#中期
		has_global_flag = active_gray_goo		#灰蛊天灾开始，且没有被清除
		any_system = {
			has_star_flag = graygoo_factory_system
			any_fleet_in_system = {
				is_ship_size = graygoo_factory
			}
		}
	}
	mean_time_to_happen = {
		months = 20
	}
	immediate = {
		#设定灰蛊国家target
		random_country = {
			limit = {
				is_country_type = gray_goo
			}
			save_event_target_as = graygoo_country
		}
		#外部威胁进入，立刻创建3支增援舰队
		if = {
			limit = {
				any_system = {
					has_star_flag = lcluster
					any_fleet_in_system = {
						owner = {
							NOT = {
								is_country = event_target:graygoo_country
							}
							#非灰蛊天灾
						}
					}
				}
			}
			random_country = {
				limit = {
					is_country = event_target:graygoo_country
				}
				country_event = {
					id = rg_event.405
					days = 10
					random = 5
				}
			}
		}
		else_if = {
			limit = {
				event_target:graygoo_country = {
					num_fleets < 30
				}
			}
			every_system = {
				limit = {
					NOR = {
						has_star_flag = lcluster_lgate
						has_star_flag = lgate_originating_system
					}
					any_system_megastructure = {
						is_megastructure_type = l_gate_base
					}
				}
				random_system_megastructure = {
					#每个L门产生一支灰蛊舰队
					limit = {
						is_megastructure_type = l_gate_base
					}
					create_ambient_object = {
						type = "horror_spawn_object"
						location = THIS
						use_3d_location = yes
						duration = 10
					}
					create_fleet = {
						#中期1支舰队
						name = "NAME_Tempest_Fleet"
						effect = {
							set_owner = event_target:graygoo_country
							rg_create_graygoo_fleet_random = yes
							set_formation_scale = 2
							set_fleet_stance = aggressive
							set_aggro_range_measure_from = self
							set_aggro_range = 250
							set_location = {
								target = prev
								distance = 0
								angle = 0
							}
							fleet_event = {
								id = rg_event.406
								days = 30
								random = 5
							}
							#_
						}
					}
				}
			}
		}
	}
}

#末期增援
country_event = {
	id = rg_event.408
	hide_window = yes
	trigger = {
		is_country = event_target:global_event_country
		end_game_years_passed >= -10		#后期增援
		has_global_flag = active_gray_goo		#灰蛊天灾开始，且没有被清除
		any_system = {
			has_star_flag = graygoo_factory_system
			any_fleet_in_system = {
				is_ship_size = graygoo_factory
			}
		}
	}
	mean_time_to_happen = {
		months = 20
	}
	immediate = {
		random_country = {
			#只有一个灰蛊
			limit = {
				is_country_type = gray_goo
			}
			save_event_target_as = graygoo_country
		}
		#外部威胁进入，立刻创建3支增援舰队
		if = {
			limit = {
				any_system = {
					has_star_flag = lcluster
					any_fleet_in_system = {
						owner = {
							NOT = {
								is_country = event_target:graygoo_country
							}
							#非灰蛊天灾
						}
					}
				}
			}
			random_country = {
				limit = {
					is_country = event_target:graygoo_country
				}
				country_event = {
					id = rg_event.405
					days = 10
					random = 5
				}
			}
		}
		if = {
			limit = {
				event_target:graygoo_country = {
					num_fleets < 60
				}
			}
			every_system = {
				limit = {
					NOR = {
						has_star_flag = lcluster_lgate
						has_star_flag = lgate_originating_system
					}
					any_system_megastructure = {
						is_megastructure_type = l_gate_base
					}
				}
				random_system_megastructure = {
					limit = {
						is_megastructure_type = l_gate_base
					}
					create_ambient_object = {
						type = "horror_spawn_object"
						location = THIS
						use_3d_location = yes
						duration = 10
					}
					while = {
						#后期2支舰队
						count = 2
						create_fleet = {
							name = "NAME_Tempest_Fleet"
							effect = {
								set_owner = event_target:graygoo_country
								rg_create_graygoo_fleet_random = yes
								set_formation_scale = 2
								set_fleet_stance = aggressive
								set_aggro_range_measure_from = self
								set_aggro_range = 250
								set_location = {
									target = prev
									distance = 0
									angle = 0
								}
								fleet_event = {
									id = rg_event.406
									days = 30
									random = 5
								}
								#_
							}
						}
					}
				}
			}
		}
	}
}
