namespace = rg_skill
#fleet_event = {
#    id = rg_skill.1
#    hide_window = yes
#    is_triggered_only = yes
#
#    trigger = {
#        NOT = { has_global_flag = rg_grayskill_cooldown }
#        any_owned_fleet = {
#					exists = leader
#					leader = { has_leader_flag = gray_leader_admiral }
#					is_in_combat = yes
#				}
#    }
#
#    immediate = {
#		if = {
#			limit = {
#				any_owned_fleet = {
#					exists = leader
#					leader = { has_leader_flag = gray_leader_admiral }
#					is_in_combat = yes
#				}
#			}
#			random_owned_fleet = {
#				if = {
#					limit = { leader = { has_leader_flag = gray_leader_admiral }}
#					save_event_target_as = gray_warship_location
#				}
#			}
#			create_fleet = {
#				name = "NAME_RG_carrier"
#				settings = { 
#					can_disband = yes
#					can_upgrade = no 
#					can_change_composition = yes 
#					can_change_leader = no 
#					uses_naval_capacity = no
#					spawn_debris = no
#				}				
#				effect = {
#					set_owner = event_target:gray_owner
#					while = {
#						count = 4
#						create_ship = {
#							name = random
#							design = "RG_carrier_Interdictor"
#							graphical_culture = "pirate_01"
#							prefix = no
#							upgradable = no
#						}
#					}
#					set_location = {
#						target = event_target:gray_warship_location
#						distance = 55
#						angle = 270
#					}
#					set_fleet_flag = RG_carrier_fleet
#					save_global_event_target_as = RG_carrier_fleet
#				}
#			}
#			
#			set_timed_global_flag = { 
#				flag = rg_grayskill_cooldown days = 120
#			}
#		}	
#	}	
#}
#fleet_event = {
#    id = rg_skill.2
#    hide_window = yes
#    is_triggered_only = yes
#
#    trigger = {
#        any_owned_fleet = {
#					has_fleet_flag = RG_carrier_fleet
#					is_in_combat = no
#				}
#    }
#
#    immediate = {
#		if = {
#			limit = {
#				any_owned_fleet = {
#					has_fleet_flag = RG_carrier_fleet
#					is_in_combat = no
#				}
#			}	
#			event_target:RG_carrier_fleet = { delete_ship = this }	
#		}
#	}
#}
#自动星系建设
ship_event = {
	id = rg_skill.3
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		OR = {
			AND = {
				is_ship_size = RG_armed_science_ship
				OR = {
					has_component = RG_AUTOCONST_HANGER
				}
			}
		}
		solar_system = {
			OR = {
				#星系无主
				NOT = {
					exists = space_owner
				}
				#星系为我方但有空间站未建造
				AND = {
					space_owner = {
						is_same_empire = root.controller
					}
					any_system_planet = {
						RG_planet_can_auto_build = yes
					}
				}
			}
			#邻国不能是军孤
			NOT = {
				any_neighbor_system = {
					has_owner = yes
					owner = {
						OR = {
							is_fallen_empire_xenophobe = yes
							has_ai_personality = fallen_empire_xenophobe
							has_ai_personality = awakened_fallen_empire_xenophobe
						}
					}
				}
			}
		}
	}
	immediate = {
		if = {
			limit = {
				owner = {
					NOT = {
						is_same_value = origin_rg_gray_country
					}
				}
				solar_system = {
					NOT = {
						#星系内不能有敌舰
						any_fleet_in_system = {
							exists = controller
							controller = {
								is_hostile = root.controller
							}
						}
					}
				}
			}
			#创建自动工程船暂存国家
			RG_const_auto_build_effect_event = yes
		}
		else_if = {
			limit = {
				owner = {
					is_same_value = origin_rg_gray_country
					NOT = {
						has_global_flag = l_cluster_opened
					}
				}
				solar_system = {
					has_star_flag = lcluster
					NOT = {
						#星系内不能有敌舰		
						any_fleet_in_system = {
							exists = controller
							controller = {
								is_hostile = root.controller
							}
						}
					}
				}
			}
			#创建自动工程船暂存国家
			RG_const_auto_build_effect_event = yes
		}
		else_if = {
			limit = {
				owner = {
					is_same_value = origin_rg_gray_country					# 是小灰起源
					has_global_flag = l_cluster_opened					# L星门已开启
				}
				solar_system = {
					# has_star_flag = lcluster
					NOT = {
						#星系内不能有敌舰		
						any_fleet_in_system = {
							exists = controller
							controller = {
								is_hostile = root.controller
							}
						}
					}
				}
			}
			#创建自动工程船暂存国家
			RG_const_auto_build_effect_event = yes
		}
		#循环检查
		ship_event = {
			id = rg_skill.3
			days = 10
		}
	}
}

# Gray Breeds(?
fleet_event = {
	id = rg_skill.4
	hide_window = yes
	mean_time_to_happen = {
		years = 1
	}
	trigger = {
		any_controlled_ship = {
			has_ship_flag = rg_mothership
		}
		num_ships < 20
		is_ship_size = RG_graygoo_mothership
	}
	immediate = {
		random_list = {
			14 = {
				create_ship = {
					name = random
					design = "RG_aircruiser_Interdictor"
					upgradable = yes
				}
			}
			12 = {
				create_ship = {
					name = random
					design = "RG_carrier_Interdictor"
					upgradable = yes
				}
			}
			12 = {
				create_ship = {
					name = random
					design = "RG_battleship_Interdictor"
					upgradable = yes
				}
			}
			12 = {
				create_ship = {
					name = random
					design = "RG_titan_interdictor"
					upgradable = yes
				}
			}
			12 = {
				create_ship = {
					name = random
					design = "RG_torpedo_interdictor"
					upgradable = yes
				}
			}
			12 = {
				create_ship = {
					name = random
					design = "RG_AA_interdictor"
					upgradable = yes
				}
			}
			12 = {
				create_ship = {
					name = random
					design = "RG_Arsenal_Interdictor"
					upgradable = yes
				}
			}
			12 = {
				create_ship = {
					name = random
					design = "RG_torpedocruiser_interdictor"
					upgradable = yes
				}
			}
		}
	}
}

#Tempest Breeds(?
fleet_event = {
	id = rg_skill.5
	hide_window = yes
	mean_time_to_happen = {
		months = 5
	}
	trigger = {
		any_controlled_ship = {
			OR = {
				OR = {
					is_ship_size = RG_graygoo_mothership
					is_ship_size = graygoo_mothership
				}
				has_ship_flag = rg_nanite_mothership
			}
			owner = {
				is_country_type = gray_goo
			}
		}
		num_ships < 40
	}
	immediate = {
		random_list = {
			14 = {
				create_ship = {
					name = random
					design = "RG_aircruiser_Interdictor"
					upgradable = yes
				}
			}
			12 = {
				create_ship = {
					name = random
					design = "RG_carrier_Interdictor"
					upgradable = yes
				}
			}
			12 = {
				create_ship = {
					name = random
					design = "RG_battleship_Interdictor"
					upgradable = yes
				}
			}
			12 = {
				create_ship = {
					name = random
					design = "RG_titan_interdictor"
					upgradable = yes
				}
			}
			12 = {
				create_ship = {
					name = random
					design = "RG_torpedo_interdictor"
					upgradable = yes
				}
			}
			12 = {
				create_ship = {
					name = random
					design = "RG_AA_interdictor"
					upgradable = yes
				}
			}
			12 = {
				create_ship = {
					name = random
					design = "RG_Arsenal_Interdictor"
					upgradable = yes
				}
			}
			12 = {
				create_ship = {
					name = random
					design = "RG_icebreaker_interdictor"
					upgradable = yes
				}
			}
		}
	}
}

# Tempest incoming traits
# Recursive check every X days
# This = planet
# From = country attacking
planet_event = {
	id = rg_skill.6
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		save_event_target_as = tempest_incoming_battlefield_planet
		if = {
			# planet
			limit = {
				has_ground_combat = no
			}
			remove_planet_flag = checking_tempest_incoming
		}
		else = {
			#planet
			any_ground_combat_attacker = {
				#army
				exists = leader
				leader = {
					#leader
					has_trait = RG_leader_trait_general_gray_skill
					owner = {
						#country
						save_event_target_as = attacking_general_country
					}
				}
			}
			# Do the same check again in X days
			country_event = {
				id = rg_skill.10
			}			# NOTE: Update custom loc for effect when changing days
		}
	}
}

# tempest incoming traits
# Inital event started by on_ground_combat_started
# to kick of the recursive check in X days
# This = planet
# From = country attacking
planet_event = {
	id = rg_skill.7
	hide_window = yes
	is_triggered_only = yes
	# Prevent duplicate events
	trigger = {
		not = {
			has_planet_flag = checking_tempest_incoming
		}
	}
	immediate = {
		set_planet_flag = checking_tempest_incoming		# Prevent duplicate events
		planet_event = {
			id = rg_skill.6
			days = 1
		}
	}
}

# On army killed in combat.
# This = owner
# From = army
# FromFrom = opponent
# FromFromFrom = planet
country_event = {
	id = rg_skill.8
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		FROMFROMFROM = {
			# Planet where ground combat is taking place.
			# Check if dead army is a defensive army.
			if = {
				limit = {
					any_ground_combat_defender = {
						is_same_value = ROOT.FROM
					}
				}
				# Check if attacking army has general with Crusader trait.
				random_ground_combat_attacker = {
					limit = {
						exists = leader
						leader = {
							OR = {
								has_trait = RG_leader_trait_general_gray_skill
							}
						}
					}
					leader = {
						save_event_target_as = attacking_general
						owner = {
							save_event_target_as = attacking_general_country
						}
					}
				}
			}
			if = {
				# planet where ground combat is taking place.
				limit = {
					exists = event_target:attacking_general
				}
				if = {
					limit = {
						event_target:attacking_general_country = {
							# attacking general country
							NOT = {
								check_variable = {
									which = rg_general_skill_combat_killed_variable
									value = 100
								}
							}
						}
					}
					event_target:attacking_general_country = {
						change_variable = {
							which = rg_general_skill_combat_killed_variable
							value = 1
						}
						change_variable = {
							which = rg_general_skill_combat_daypassed_variable
							value = 1
						}
					}
				}
				# Handle both sides of the ground combat
				RG_leader_trait_tempest_incoming_random_kill_effect = {
					SIDE_A = attacker
					SIDE_B = defender
				}
				RG_leader_trait_tempest_incoming_random_kill_effect = {
					SIDE_A = defender
					SIDE_B = attacker
				}
			}
		}
	}
}

country_event = {
	id = rg_skill.9
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		any_owned_army = {
			army_type = gray_army
			if = {
				limit = {
					exists = planet
				}
				planet = {
					has_ground_combat = no
				}
			}
		}
	}
	immediate = {
		set_variable = {
			# country
			which = rg_general_skill_combat_daypassed_variable
			value = 1
		}
		set_variable = {
			# country
			which = rg_general_skill_combat_killed_variable
			value = 0
		}
	}
}

country_event = {
	id = rg_skill.10
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		any_owned_army = {
			army_type = gray_army
			exists = leader
			leader = {
				OR = {
					has_trait = RG_leader_trait_general_gray_skill
				}
			}
			if = {
				limit = {
					exists = planet
				}
				planet = {
					has_ground_combat = yes
				}
			}
		}
	}
	immediate = {
		event_target:attacking_general_country = {
			change_variable = {
				which = rg_general_skill_combat_daypassed_variable
				value = 1
			}
		}
		country_event = {
			id = rg_skill.10
			days = 1
		}
	}
}

# 重组仪
country_event = {
	id = rg_skill.11
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		FromFromFrom = {
			is_in_combat = yes
			has_component = RG_revive_component
		}
	}
	immediate = {
		FromFromFrom = {
			save_event_target_as = event_ship
		}
		FromFromFrom.fleet = {
			create_ship = {
				name = event_target:event_ship
				design = event_target:event_ship
			}
		}
	}
}
