#####################################################
#                                          			#
#  Events for leader traits in Galactic Paragons.   #
#  Written by: Xuanming "XM" Zhou          			#
#                                   	   			#
#####################################################

namespace = paragon_2

# On destroying enemy ship.
country_event = {
	id = paragon_2.1005
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		# Cache the destroyed ship so trait rewards can scale off its used naval capacity.
		FROMFROMFROM = { save_event_target_as = paragon_destroyed_ship }
		FROMFROM = { # Winning ship.
			if = {
				limit = { exists = fleet.leader }
				fleet.leader = {

					# Corsair / Corsair II
					if = {
						limit = {
							OR = {
								has_trait = leader_trait_corsair
								has_trait = leader_trait_corsair_2
							}
							ROOT.FROM = { # Owner of losing ship.
								is_default_or_fallen = yes
							}
						}
						if = {
							limit = { has_trait = leader_trait_corsair }
							ROOT = { # Owner of winning ship.
								add_resource = {
									energy = @trait_corsair_amt
									mult = value:trait_kill_naval_cap_scaling|MULT|value:hells_heart_effect|
								}
							}
						}
						else = {
							ROOT = { # Owner of winning ship.
								add_resource = {
									energy = @trait_corsair_2_amt
									mult = value:trait_kill_naval_cap_scaling|MULT|value:hells_heart_effect|
								}
							}
						}
					}

					# Crusader / Crusader II
					if = {
						limit = {
							has_trait_tier1or2 = {
								TRAIT = leader_trait_crusader
							}
							ROOT.FROM = { # Owner of losing ship.
								is_default_or_fallen = yes
							}
						}

						ROOT = {
							if = {
								limit = { PREV = { has_trait = leader_trait_crusader } }
								add_resource = {
									unity = @trait_crusader_fleet_amt
									mult = value:trait_kill_naval_cap_scaling|MULT|1|
								}
							}
							else = {
								add_resource = {
									unity = @trait_crusader_2_fleet_amt
									mult = value:trait_kill_naval_cap_scaling|MULT|1|
								}
							}
						}
					}

					# Shipbreaker / Shipbreaker II
					if = {
						limit = {
							OR = {
								has_trait = leader_trait_shipbreaker
								has_trait = leader_trait_shipbreaker_2
							}
							ROOT.FROM = { # Owner of losing fleet.
								is_default_or_fallen = yes
							}
						}
						if = {
							limit = { has_trait = leader_trait_shipbreaker }
							ROOT = {
								add_resource = {
									alloys = @trait_shipbreaker_amt
									mult = value:trait_kill_naval_cap_scaling|MULT|value:hells_heart_effect|
								}
							}
						}
						else = {
							ROOT = {
								add_resource = {
									alloys = @trait_shipbreaker_2_amt
									mult = value:trait_kill_naval_cap_scaling|MULT|value:hells_heart_effect|
								}
							}
						}
					}

					# Juryrigger / Juryrigger II
					if = {
						limit = {
							OR = {
								has_trait = leader_trait_juryrigger
								has_trait = leader_trait_juryrigger_2
							}
						}
						ROOT.FROMFROM = {
							if = {
								limit = { PREV = { has_trait = leader_trait_juryrigger } }
								repair_percentage = @trait_juryrigger_amt
							}
							else = {
								repair_percentage = @trait_juryrigger_2_amt
							}
						}
					}

					# Plunderer / Plunderer II
					if = {
						limit = {
							OR = {
								has_trait = leader_trait_plunderer
								has_trait = leader_trait_plunderer_2
							}
						}
						ROOT.FROMFROM = {
							if = {
								limit = { PREV = { has_trait = leader_trait_plunderer } }
								root = {
									random_list = {
										1 = {
											add_resource = {
												trade = @trait_plunderer_amt
												mult = value:trait_kill_naval_cap_scaling|MULT|1|
											}
										}
										1 = {
											add_resource = {
												consumer_goods = @trait_plunderer_amt
												mult = value:trait_kill_naval_cap_scaling|MULT|1|
											}
										}
									}
								}
							}
							else = {
								root = {
									random_list = {
										1 = {
											add_resource = {
												trade = @trait_plunderer_2_amt
												mult = value:trait_kill_naval_cap_scaling|MULT|1|
											}
										}
										1 = {
											add_resource = {
												consumer_goods = @trait_plunderer_2_amt
												mult = value:trait_kill_naval_cap_scaling|MULT|1|
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}

# On winning a ground battle (before controller is switched)
# This = country, leader attacker
# From = country, planet owner
# FromFrom = planet
country_event = {
	id = paragon_2.1015
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		FROMFROM = {
			every_ground_combat_attacker = {
				if = {
					limit = { exists = leader }
					leader = {
						save_event_target_as = attacking_general

						# Interrogator.
						if = {
							limit = {
								exists = from
								has_trait = leader_trait_interrogator
							}
							ROOT = {
								add_intel = {
									who = root.from
									amount = 15
								}
							}
						}
						if = {
							limit = {
								exists = from
								has_trait = leader_trait_interrogator_2
							}
							ROOT = {
								add_intel = {
									who = root.from
									amount = 30
								}
							}
						}
					}
				}
			}
		}
	}
}

# Yearly roll for negative trait.
country_event = {
	id = paragon_2.1020
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		every_owned_leader = {
			if = {
				#log = "[This.GetName], unmodified: [This.leader_pending_negative_traits_unmodified], modified: [scriptval__leader_pending_negative_traits_modified]"
				# If leader can gain negative traits.
				limit = {
					NOT = { has_leader_flag = immune_to_negative_traits }
					is_variable_set = leader_pending_negative_traits_unmodified
					check_variable_arithmetic = { which = value:leader_pending_negative_traits_modified value > 0 }
				}
				if = {
					limit = { is_immortal = yes }
					# Roll the fixed random-check first (cheap), if success: check if leader is eligible (expensive).
					#log = "Eligible: [This.GetName], [scriptval__leader_eligible_for_negative_trait_immortal]"
					random = {
						chance = @negative_trait_yearly_chance_when_eligible_immortal
						if = {
							limit = { check_variable_arithmetic = { which = value:leader_eligible_for_negative_trait_immortal value > 0 } }
							# Add a Negative trait sometime this year.
							leader_event = { id = paragon_2.1021 days = 1 random = 358 }
						}
					}
				}
				else = {
					# Not Immortal.
					# For Mortal leaders, the chance increases once they're eligible for a negative trait.
					#log = "Chance %: [This.GetName], [scriptval__leader_yearly_chance_for_negative_trait]"
					random = {
						chance = value:leader_yearly_chance_for_negative_trait
						# Add a Negative trait sometime this year.
						leader_event = { id = paragon_2.1021 days = 1 random = 358 }
					}
				}
			}
		}
	}
}

# Delayed adding of negative trait, from paragon_2.1020
leader_event = {
	id = paragon_2.1021
	is_triggered_only = yes
	hide_window = yes

	trigger = {
		exists = owner
	}

	immediate = {
		if = {
			# Double check that leader still has pending negative traits.
			limit = {
				NOT = { has_leader_flag = immune_to_negative_traits }
				check_variable_arithmetic = { which = value:leader_pending_negative_traits_modified value > 0 }
			}

			# Add a Negative trait and reduce Pending negative traits counter here, so it matches negative traits actually added.
			add_trait = {
				trait = random_negative
			}
			subtract_variable = { which = leader_pending_negative_traits_unmodified value = 1 }
		}
	}
}


# On army killed in combat.
# This = owner
# From = army
# FromFrom = opponent
# FromFromFrom = planet
country_event = {
	id = paragon_2.1040
	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 = leader_trait_crusader
								has_trait = leader_trait_crusader_2
							}
						}
					}
					leader = {
						save_event_target_as = attacking_general
					}
				}
			}
		}

		# If attacking army has a leader with Crusader trait, award Unity.
		if = {
			limit = { exists = event_target:attacking_general }
			FROMFROM = {
				if = {
					limit = {
						event_target:attacking_general = {
							has_trait = leader_trait_crusader
						}
					}
					add_resource = {
						unity = 40 # Tier 1
					}
				}
				else = {
					add_resource = {
						unity = 60 # Tier 2
					}
				}
			}
		}
	}
}

# Salvager trait
# Fired when a science ship succesfully analyzed or scavenged debris (bot not both)
# this = country
# from = debris
# fromfrom = controller of destroyed ship
country_event = {
	id = paragon_2.1110
	is_triggered_only = yes
	hide_window = yes

	# Check if the country has a leader with the salvager trait in the same system
	trigger = {
		any_owned_leader = {
			OR = {
				has_trait = leader_trait_salvager
				has_trait = leader_trait_salvager_2
				has_trait = leader_trait_salvager_3
				has_trait = leader_trait_master_salvager
			}
			exists = solar_system
			solar_system = {
				is_same_value = from.solar_system
			}
		}
	}

	immediate = {
		salvage_ships_from_debris = {
			CHANCE = value:country_salvage_ships_from_debris_chance
		}
	}
}

# Shroud Preacher trait, on_decade_pulse
event = {
	id = paragon_2.1140
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		every_playable_country = {
			every_owned_leader = {
				limit = {
					has_trait = leader_trait_shroud_preacher
					exists = planet
				}
				planet = {
					weighted_random_owned_pop_group = {
						limit = {
							is_organic_species = yes
							is_being_assimilated = no
							has_psionic_species_trait = no
							NOT = { has_trait = trait_cybernetic }
						}
						add_latent_psionic_trait = yes
					}
				}
			}
		}
	}
}

# Aptitude - Champions of the Empire Tradition
# on_monthly_pulse
# event = {
# 	id = paragon_2.1145
# 	hide_window = yes
# 	is_triggered_only = yes

# 	immediate = {
# 		every_playable_country = {
# 			limit = { has_tradition = tr_aptitude_champions_of_the_empire }

# 			# Reset counters
# 			set_variable = { which = champs_of_empire_mil_amount value = 0 }
# 			set_variable = { which = champs_of_empire_gov_amount value = 0 }
# 			set_variable = { which = champs_of_empire_sci_amount value = 0 }
# 			create_variable_if_not_exists = { VARIABLE = prev_champs_of_empire_mil_amount VALUE_IF_CREATED = 0 }
# 			create_variable_if_not_exists = { VARIABLE = prev_champs_of_empire_gov_amount VALUE_IF_CREATED = 0 }
# 			create_variable_if_not_exists = { VARIABLE = prev_champs_of_empire_sci_amount VALUE_IF_CREATED = 0 }

# 			# Count how many leaders and skill levels there are of each class.
# 			if = {
# 				every_owned_leader = {
# 					if = {
# 						limit = { leader_class = commander }
# 						PREV = { change_variable = { which = champs_of_empire_mil_amount value = value:prev_has_skill } }
# 					}
# 					else_if = {
# 						limit = { leader_class = official }
# 						PREV = { change_variable = { which = champs_of_empire_gov_amount value = value:prev_has_skill } }
# 					}
# 					else_if = {
# 						limit = { leader_class = scientist }
# 						PREV = { change_variable = { which = champs_of_empire_sci_amount value = value:prev_has_skill } }
# 					}
# 				}
# 			}

# 			# Update/remove modifier as needed
# 			update_modifier_amount_if_needed = {
# 				MODIFIER = champs_of_empire_mil_effect
# 				MULT_VAR = champs_of_empire_mil_amount
# 				PREV_MULT_VAR = prev_champs_of_empire_mil_amount
# 			}
# 			update_modifier_amount_if_needed = {
# 				MODIFIER = champs_of_empire_gov_effect
# 				MULT_VAR = champs_of_empire_gov_amount
# 				PREV_MULT_VAR = prev_champs_of_empire_gov_amount
# 			}
# 			update_modifier_amount_if_needed = {
# 				MODIFIER = champs_of_empire_sci_effect
# 				MULT_VAR = champs_of_empire_sci_amount
# 				PREV_MULT_VAR = prev_champs_of_empire_sci_amount
# 			}
# 		}
# 	}
# }

#############################
# SCIENTIST TRAIT ANOMALIES #
#############################

# On surveying a planet.
ship_event = {
	id = paragon_2.2000
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = controller
		check_modifier_value = {
			modifier = deposit_increase_on_survey_chance_mult
			value > 0
		}
	}

	immediate = {
		# We wait a day in order to check if any anomalies or dig sites have spawned.
		ship_event = {
			id = paragon_2.2001
			scopes = {
				from = from
			}
			days = 1
		}
	}
}


# Prospector traits, the Treasure Hoarder trait and the Galactic Dowsing edicts
ship_event = {
	id = paragon_2.2001
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = controller
	}

	immediate = {
		FROM = {
			survey_deposit_increase_effect = {
				SHIP = root
			}
		}
	}
}

# Midas Touch trait
ship_event = {
	id = paragon_2.2003
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = leader
		leader = {
			has_trait = leader_trait_midas_touch
		}
	}

	immediate = {
		owner = {
			every_owned_leader = {
				limit = {
					leader_class = scientist
				}
				add_experience = 300
			}
		}
	}
}

country_event = { # Memorial Valuts Civic Paragon Level Tick / FROM = Leader
	id = paragon_2.2045
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_memory_vault_empire = yes
		FROM = {
			has_base_skill = 8
			NOT = { has_leader_flag = memory_vault_contributed }
		}
	}

	immediate = {
		FROM = {
			set_leader_flag = memory_vault_contributed
		}
		change_variable = {
			which = paragon_memory_vault
			value = 1
		}
	}
}

# Change commander background class.
# scope: country, from: leader
country_event = {
	id = paragon_2.2065
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		FROM = {
			leader_class = commander
			has_miltary_background_job = no
		}
	}

	immediate = {
		FROM = {
			set_random_commander_background_effect = yes
		}
	}
}

# This = planet
# From = country attacking
carrier_event = { #General with Chainbreaker trait
	id = paragon_2.2085
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_ground_combat_attacker = {
			exists = leader
			leader = {
				has_trait = leader_trait_chainbreaker
			}
		}
	}

	immediate = {
		while = {
			count = value:slaves_on_planet_100
			random_list = {
				70 = {
					weighted_random_owned_pop_group = {
						limit = {
							is_enslaved = yes
						}
						save_event_target_as = rebel_army_pop
					}
					create_army = {
						name = random
						owner = FROM
						species = event_target:rebel_army_pop
						type = "rebel_army"
					}
				}
				30 = {}
			}
		}
	}
}

country_event = { # Gestalt Council Nodes: Update portraits at level 4 and 8.
	id = paragon_2.2096
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_paragon_dlc = yes
		has_ethic = ethic_gestalt_consciousness
		FROM = {
			is_ruler = no
			is_councilor = yes
		}
	}

	immediate = {
		FROM = {
			set_gestalt_node_portrait_effect = yes
		}
	}
}

first_contact_event = { #Special Tech giver
	id = paragon_2.2100
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_paragon_dlc = yes
		owner = { is_country_type = default }
		from = { is_country_type = default }
		OR = {
			owner = {
				is_homicidal = no
				is_gestalt = no
				NOT = { has_technology = tech_xeno_linguistics }
			}
			from = {
				is_homicidal = no
				is_gestalt = no
				NOT = { has_technology = tech_xeno_linguistics }
			}
		}
	}

	immediate = {
		owner = {
			if = {
				limit = {
					is_homicidal = no
					is_gestalt = no
					NOT = { has_technology = tech_xeno_linguistics }
				}
				add_tech_progress = {
					tech = tech_xeno_linguistics
					progress = 0.25
				}
			}
		}
		from = {
			if = {
				limit = {
					is_homicidal = no
					is_gestalt = no
					NOT = { has_technology = tech_xeno_linguistics }
				}
				add_tech_progress = {
					tech = tech_xeno_linguistics
					progress = 0.25
				}
			}
		}
	}
}

country_event = { #Xeno-Linguistic researched
	id = paragon_2.2105
	title = "paragon_2.2105.name"
	picture = GFX_evt_hand_shake
	is_triggered_only = yes

	desc = {
		text = paragon_2.2105.desc_01
		trigger = { is_xenophobe = no }
	}
	desc = { # Xenophobe
		text = paragon_2.2105.desc_02
		trigger = { is_xenophobe = yes }
	}

	trigger = {
		has_paragon_dlc = yes
		last_increased_tech = "tech_xeno_linguistics"
	}

	immediate = {
	}

	option = {
		name = "paragon_2.2105.a"
		custom_tooltip = paragon_2.2105.tt
		trigger = {
			NOR = {
				is_xenophobe = yes
				has_ethic = ethic_gestalt_consciousness
			}
		}
	}
	option = { # Xenophobe
		name = "paragon_2.2105.b"
		custom_tooltip = paragon_2.2105.tt
		trigger = { is_xenophobe = yes }
	}
	after = {
		add_monthly_resource_mult = {
			resource = influence
			value = @tier2influencecontact
			min = @tier2influencecontactmin
			max = @tier2influencecontactmax
		}
	}
}

country_event = { # Set "Pending Negative Traits" for Leaders when created.
	id = paragon_2.2200
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		NOT = { has_country_flag = spawning_renowned_leader }
		is_country_type = default
		FROM = { NOT = { leader_class = envoy } }
	}

	immediate = {
		FROM = {
			if = {
				limit = { has_paragon_dlc = yes }
				random_list = {
					1 = { set_variable = { which = leader_pending_negative_traits_unmodified value = 0 } }
					4 = { set_variable = { which = leader_pending_negative_traits_unmodified value = 1 } }
					20 = { set_variable = { which = leader_pending_negative_traits_unmodified value = 2 } }
					55 = { set_variable = { which = leader_pending_negative_traits_unmodified value = 3 } }
					20 = { set_variable = { which = leader_pending_negative_traits_unmodified value = 4 } }
				}
			}
			else = {
				# No Galactic Paragons DLC, fewer traits in total.
				random_list = {
					1 = { set_variable = { which = leader_pending_negative_traits_unmodified value = 0 } }
					2 = { set_variable = { which = leader_pending_negative_traits_unmodified value = 1 } }
					4 = { set_variable = { which = leader_pending_negative_traits_unmodified value = 2 } }
				}
			}
			# If leader already got a Negative trait, typically from "action.406", reduce pending negative traits by 1.
			if = {
				limit = {
					num_leader_traits = {
						value > 0
						negative = yes
					}
				}
				subtract_variable = { which = leader_pending_negative_traits_unmodified value = 1 }
			}
		}
	}
}

#this = country
country_event = { #UTILITY - give starting ruler a proper previous job
	id = paragon_2.2125
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_gestalt = no
	}

	immediate = {
		ruler = {
			switch = {
				trigger = leader_class
				commander = {
					set_random_commander_background_effect = yes
				}
				scientist = {
					set_random_scientist_background_effect = yes
				}
				official = {
					set_random_official_background_effect = yes
				}
			}
		}
	}
}

# On winning a ground battle.
# (Before controller is switched)
# This = country, leader attacker
# From = country, planet owner
# FromFrom = planet
country_event = {
	id = paragon_2.2130
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		FROMFROM = {
			any_ground_combat_attacker = {
				exists = leader
				leader = {
					has_trait = leader_trait_tzrynn_tithe
				}
			}
		}
	}

	immediate = {
		FROMFROM = {
			every_ground_combat_attacker = {
				if = {
					limit = {
						exists = leader
						leader = {
							has_trait = leader_trait_tzrynn_tithe
						}
					}
					leader = { save_event_target_as = attacking_general }
				}
			}
			every_owned_pop_group = {
				save_event_target_as = kidnapped_pop # Pick the pop
				ROOT = { # Pick a planet
					reroll_random = yes
					random_owned_planet = {
						save_event_target_as = target_planet
					}
				}
				resettle_pop_group = {
					POP_GROUP = this
					PLANET = event_target:target_planet
					PERCENTAGE = 0.3
				}
				event_target:resettled_pop_group = {
					set_citizenship_type = {
						country = ROOT
						type = citizenship_slavery
					}
				}
			}
		}
	}
}

# Galactic Administration researched
country_event = {
	id = paragon_2.2135
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		last_increased_tech = "tech_galactic_administration"
	}

	immediate = {
		set_government_cooldown = no
	}
}
