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 = {
		is_ship_size = RG_armed_science_ship
		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 = {
				solar_system = {
					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 = 30
		}
	}
}

# 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 = {
			if = {
				limit = {
					#planet
					any_ground_combat_attacker = {
						#army
						exists = leader
						leader = {
							#leader
							has_trait = RG_leader_trait_admiral_nanorebuild_gray
						}

					}
	
				}
				from = {
					#country
					save_event_target_as = attacking_general_country
				}
			}
			from = {
				# 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_admiral_nanorebuild_gray
							}
						}
					}
					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_admiral_nanorebuild_gray
				}
			}
			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
			}
		}
	}
}

# 纳米重塑
# This = owner of ship 1 (combatant)
# From = owner of ship 2 (destroyed)
# FromFrom = ship 1
# FromFromFrom = ship 2
country_event = {
	id = rg_skill.12
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		FromFromFrom = {
			is_in_combat = yes
			NOT = {
				is_same_value = event_target:gray_warship_ship
			}
			exists = fleet.leader
		}
		FromFromFrom.fleet.leader = {
			has_trait = RG_leader_trait_admiral_nanorebuild_gray
		}
	}
	immediate = {
		if = {
			limit = {
				From = {
					check_variable = {
						which = rg_nanorebuild_counter
						value >= 1
					}
				}
			}
			FromFromFrom = {
				save_event_target_as = event_ship
			}
			FromFromFrom.fleet = {
				create_ship = {
					name = event_target:event_ship
					design = event_target:event_ship
				}
			}
			From = {
				change_variable = {
					which = rg_nanorebuild_counter
					value = -1
				}
			}
		}
	}
}

# country_event = {
# 	id = rg_skill.13
# 	hide_window = yes
# 	is_triggered_only = yes
# 	trigger = {
# 		FromFromFrom = {
# 			is_in_combat = yes
# 		}
# 		FromFrom.solar_system = {
# 			any_fleet_in_system = {
# 				leader = {
# 					has_trait = RG_leader_trait_admiral_nanorebuild_gray
# 				}
# 			}
# 		}
# 		this = {
# 			NOT = {
# 				is_same_value = event_target:gray_owner
# 				is_in_federation_with = event_target:gray_owner
# 			}
# 		}
# 	}
# 	immediate = {
# 		event_target:gray_owner = {
# 			change_variable = {
# 				which = rg_nanorebuild_counter
# 				value = 1
# 			}
# 		}
# 	}
# }
# On destroying enemy ship.
country_event = {
	id = rg_skill.13
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		FromFrom = {
			is_in_combat = yes
			exists = fleet.leader
		}
		FromFrom.fleet.leader = {
			has_trait = RG_leader_trait_admiral_nanorebuild_gray
		}
	}
	immediate = {
		FROMFROM = {
			# Winning ship.
			if = {
				limit = {
					exists = fleet.leader
				}
				fleet.leader = {
					if = {
						limit = {
							OR = {
								has_trait = RG_leader_trait_admiral_nanorebuild_gray
							}
						}
						ROOT = {
							change_variable = {
								which = rg_nanorebuild_counter
								value = 1
							}
						}
					}
				}
			}
		}
	}
}

# This = owner of fleet 1 (winner)
# From = owner of fleet 2 (loser)
# FromFrom = fleet 1
# FromFromFrom = fleet 2
country_event = {
	id = rg_skill.14
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		exists = fromfrom.leader
		OR = {
			fromfrom.leader = {
				has_trait = RG_leader_trait_admiral_nanorebuild_gray
			}
		}
		NOT = {
			This = {
				check_variable = {
					which = rg_nanorebuild_counter
					value = 0
				}
			}
		}
	}
	immediate = {
		This = {
			set_variable = {
				which = rg_nanorebuild_counter
				value = 0
			}
		}
	}
}

# This = owner of fleet 1 (loser)
# From = owner of fleet 2 (winner)
# FromFrom = fleet 1
# FromFromFrom = fleet 2
country_event = {
	id = rg_skill.15
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		exists = fromfromfrom.leader
		OR = {
			fromfromfrom.leader = {
				has_trait = RG_leader_trait_admiral_nanorebuild_gray
			}
		}
		NOT = {
			This = {
				check_variable = {
					which = rg_nanorebuild_counter
					value = 0
				}
			}
		}
	}
	immediate = {
		This = {
			set_variable = {
				which = rg_nanorebuild_counter
				value = 0
			}
		}
	}
}
