﻿fixpack_friend_confider_event = {
	$CURRENT$ = {
		if = {
			limit = { has_relation_friend = $FRIEND$ }
			$FRIEND$ = {
				if = {
					limit = { has_relation_best_friend = $CURRENT$ }
					if = {
						limit = {
							NOT = {
								has_relation_flag = {
									target = $CURRENT$
									relation = best_friend
									flag = has_been_confided_in
								}
							}
						}
						add_relation_flag = {
							target = $CURRENT$
							relation = best_friend
							flag = has_been_confided_in
						}
					}
					else = {
						$CURRENT$ = {
							reverse_add_opinion = {
								target = $FRIEND$
								modifier = friendliness_opinion
								opinion = $OPINION_MODIFIER$
							}
						}
					}
				}
				else = {
					if = {
						limit = {
							NOT = {
								has_relation_flag = {
									target = $CURRENT$
									relation = friend
									flag = has_been_confided_in
								}
							}
						}
						add_relation_flag = {
							target = $CURRENT$
							relation = friend
							flag = has_been_confided_in
						}
					}
					else = {
						$CURRENT$ = {
							reverse_add_opinion = {
								target = $FRIEND$
								modifier = friendliness_opinion
								opinion = $OPINION_MODIFIER$
							}
						}
					}
				}
			}
		}
	}
}

fixpack_stress_threshold_2321 = {
	$CURRENT$ = {
		every_vassal = {
			limit = {
				is_alive = yes
				NOR = {
					has_trait = $TRAIT1$
					has_trait = $TRAIT2$
					has_trait = $TRAIT3$
				}
			}
			custom = $TEXT$
			add_opinion = {
				modifier = $OPINION_TEXT$
				opinion = $OPINION_MODIFIER$
				target = $CURRENT$
			}
		}

		#Unop putting it in an hidden effect since the previous custom = already display the text
		hidden_effect = {
			every_courtier = {
				limit = {
					is_alive = yes
					NOR = {
						has_trait = $TRAIT1$
						has_trait = $TRAIT2$
						has_trait = $TRAIT3$
					}
				}
				add_opinion = {
					modifier = $OPINION_TEXT$
					opinion = $OPINION_MODIFIER$
					target = $CURRENT$
				}
			}
		}
	}
}

unop_reveal_or_add_genetic_trait = {
	if = {
		limit = { NOT = { has_trait = $TRAIT$ } }
		if = {
			limit = { has_inactive_trait = $TRAIT$_genetic }
			make_trait_active = $TRAIT$_genetic
		}
		else_if = {
			add_trait = $TRAIT$_1
		}
	}
}

# Transfer the story to another character, used on inheritance
transfer_holy_order_loan_story_cycle_to_effect = {
	$STORY$ = {
		# Set story owner
		make_story_owner = $CHARACTER$

		#Modify the inner variable for tracking purpose
		set_variable = { name = current_loan_holder value = $CHARACTER$ }

		$STORY$.var:original_loan_holder = {
			save_scope_as = original_loan_holder
		}

		# Save story scope for reference
		save_scope_as = loan_story
	}

	$CHARACTER$ = {
		# Inform player heir that a loan was inherited
		if = {
			limit = { is_ai = no } #Unop: No need to fire an event for the AI
			trigger_event = {
				id = loan_story.0002
				days = { 3 5 }
			}
		}
	}
}

add_sparring_flag_effect = {
	random_list = {
		1 = {
			trigger = {
				NOR = {
					has_character_flag = sparring_honed_skills_modifier
					has_character_modifier = sparring_honed_skills_modifier
				}
			}

			add_character_flag = sparring_honed_skills_modifier

			modifier = {
				add = 1
				this = scope:sc_victor
			}
		}
		1 = {
			trigger = {
				NOR = {
					has_character_flag = sparring_full_of_confidence_modifier
					has_character_modifier = sparring_full_of_confidence_modifier
				}
			}

			add_character_flag = sparring_full_of_confidence_modifier

			modifier = {
				add = 1
				this = scope:sc_victor
			}
		}
		1 = {
			trigger = {
				NOR = {
					has_character_flag = sparring_chivalric_motivation_modifier
					has_character_modifier = sparring_chivalric_motivation_modifier
				}
			}

			add_character_flag = sparring_chivalric_motivation_modifier

			modifier = {
				add = 1
				this = scope:sc_victor
			}
		}
		1 = {
			trigger = {
				NOR = {
					has_character_flag = sparring_valiant_knight_modifier
					has_character_modifier = sparring_valiant_knight_modifier
				}
			}

			add_character_flag = sparring_valiant_knight_modifier

			modifier = {
				add = 1
				this = scope:sc_victor
			}
		}
		1 = {
			trigger = {
				NOR = {
					has_character_flag = sparring_noble_defender_modifier
					has_character_modifier = sparring_noble_defender_modifier
				}
			}

			add_character_flag = sparring_noble_defender_modifier

			modifier = {
				add = 1
				this = scope:sc_victor
			}
		}
		1 = {
			trigger = {
				NOR = {
					has_character_flag = sparring_delusions_of_superiority_modifier
					has_character_modifier = sparring_delusions_of_superiority_modifier
				}
			}

			add_character_flag = sparring_delusions_of_superiority_modifier

			modifier = {
				add = 1
				this = scope:sc_victor
			}

			modifier = {
				add = 1
				has_trait = arrogant
			}
		}
		1 = {
			trigger = {
				NOR = {
					has_character_flag = sparring_leader_of_knights_modifier
					has_character_modifier = sparring_leader_of_knights_modifier
				}
			}

			add_character_flag = sparring_leader_of_knights_modifier

			modifier = {
				add = 1
				this = scope:sc_victor
			}
		}
		1 = {
			trigger = {
				NOR = {
					has_character_flag = sparring_sprained_ankle_modifier
					has_character_modifier = sparring_sprained_ankle_modifier
				}
			}

			add_character_flag = sparring_sprained_ankle_modifier

			modifier = {
				add = 1
				this = scope:sc_victor
			}

			modifier = {
				add = 1
				NOT = {
					this = scope:sc_victor
				}
			}
		}
	}
}

add_sparring_modifier_from_flag_effect = {
	if = {
		limit = { has_character_flag = sparring_honed_skills_modifier }
		add_character_modifier = {
			modifier = sparring_honed_skills_modifier
			years = 10
		}
		remove_character_flag = sparring_honed_skills_modifier
	}
	if = {
		limit = { has_character_flag = sparring_full_of_confidence_modifier }
		add_character_modifier = {
			modifier = sparring_full_of_confidence_modifier
			years = 10
		}
		remove_character_flag = sparring_full_of_confidence_modifier
	}
	if = {
		limit = { has_character_flag = sparring_chivalric_motivation_modifier }
		add_character_modifier = {
			modifier = sparring_chivalric_motivation_modifier
			years = 10
		}
		remove_character_flag = sparring_chivalric_motivation_modifier
	}
	if = {
		limit = { has_character_flag = sparring_valiant_knight_modifier }
		add_character_modifier = {
			modifier = sparring_valiant_knight_modifier
			years = 10
		}
		remove_character_flag = sparring_valiant_knight_modifier
	}
	if = {
		limit = { has_character_flag = sparring_noble_defender_modifier }
		add_character_modifier = {
			modifier = sparring_noble_defender_modifier
			years = 10
		}
		remove_character_flag = sparring_noble_defender_modifier
	}
	if = {
		limit = { has_character_flag = sparring_delusions_of_superiority_modifier }
		add_character_modifier = {
			modifier = sparring_delusions_of_superiority_modifier
			years = 10
		}
		remove_character_flag = sparring_delusions_of_superiority_modifier
	}
	if = {
		limit = { has_character_flag = sparring_leader_of_knights_modifier }
		add_character_modifier = {
			modifier = sparring_leader_of_knights_modifier
			years = 10
		}
		remove_character_flag = sparring_leader_of_knights_modifier
	}
	if = {
		limit = { has_character_flag = sparring_sprained_ankle_modifier }
		add_character_modifier = {
			modifier = sparring_sprained_ankle_modifier
			years = 10
		}
		remove_character_flag = sparring_sprained_ankle_modifier
	}
}

unop_log_hunt_activity_effect = {
	debug_log = "==> HUNT <=="
	scope:activity = {
		if = {
			limit = { has_variable = hunt_invalidated }
			debug_log = "Invalidated: YES"
			unop_log_hunt_invalidation_flavor_effect = yes
		}
		else = {
			debug_log = "Invalidated: NO"
		}
		if = {
			limit = { has_activity_option = { category = special_type option = hunt_type_falconry } }			
			debug_log = "Type: falconry"
		}
		else_if = {
			limit = { has_activity_option = { category = special_type option = hunt_type_legendary } }
			debug_log = "Type: legendary"
		}
		else_if = {
			limit = { has_activity_option = { category = special_type option = hunt_type_standard } }
			debug_log = "Type: standard"
		}
		else = {
			debug_log = "Type: undefined"
		}
	}
	if = {
		limit = { exists = scope:activity.var:hunt_success }
		if = {
			limit = { scope:activity.var:hunt_success = flag:yes }
			debug_log = "Success: Yes"
		}
		else = {			
			debug_log = "Success: No"
		}
	}
	else = {
		debug_log = "Success: Unknown"
	}
	if = {
		limit = { exists = scope:activity.var:animal_spared }
		debug_log = "Spared: Yes"
	}
	else = {
		debug_log = "Spared: No"
	}
	if = {
		limit = { exists = scope:activity.var:legendary_proxy }
		debug_log = "LegendaryProxy: Yes"
	}
	else = {
		debug_log = "LegendaryProxy: No"
	}
	if = {
		limit = { exists = scope:activity.var:skull_damaged }
		debug_log = "Skull: Damaged"
	}
	else = {
		debug_log = "Skull: Mint"
	}
	if = {
		limit = { exists = scope:activity.var:pelt_damaged }
		debug_log = "Pelt: Damaged"
	}
	else = {
		debug_log = "Pelt: Mint"
	}
	#if = {
	#	limit = { exists = scope:activity.var:antlers_damaged }
	#	debug_log = "Antlers: Damaged"
	#}
	#else = {
	#	debug_log = "Antlers: Mint"
	#}
	# The animal female_quarry is the only one set as a var on the activity
	if = {
		limit = { exists = scope:activity.var:female_quarry }
		debug_log = "Animal: female_quarry"
		unop_log_hunted_animal_effect = yes
	}
	else = {
		unop_log_hunted_animal_effect = yes
	}
	if = {
		limit = { exists = scope:activity.var:captive_animal }
		debug_log = "Captured: Yes"
	}
	else = {
		debug_log = "Captured: No"
	}
	if = {
		limit = { exists = scope:activity.var:hunt_trophy_artifact }
		debug_log = "Hunt-Trophy: Yes"
	}
	else = {
		debug_log = "Hunt-Trophy: No"
	}
	#Unop: Replace this variable in hunt_1005 someday
	#if = {
	#	limit = { exists = scope:activity.var:hunt_1005 }
	#	debug_log = "hunt_1005: Yes"
	#}
	#else = {
	#	debug_log = "hunt_1005: No"
	#}	
	if = {
		limit = { exists = scope:activity.var:hunt_4013 }
		debug_log = "hunt_4013 : Yes"
	}
	else = { debug_log = "hunt_4013 : No" }
	if = {
		limit = { exists = scope:activity.var:hunt_4017 }
		debug_log = "hunt_4017 : Yes"
	}
	else = {
		debug_log = "hunt_4017 : No"
	}
	if = {
		limit = { exists = scope:activity.var:hunt_4019 }
		debug_log = "hunt_4019: Yes"
	}
	else = {
		debug_log = "hunt_4019: No"
	}
}

unop_log_hunt_invalidation_flavor_effect = {
	if = {
		limit = { scope:activity = { has_variable = hunt_invalidated } }
		switch = {
			trigger = scope:activity.var:hunt_invalidated
			flag:wounded = 					{ debug_log = "Flavor: wounded" }
			flag:wound = 					{ debug_log = "Flavor: wound" }
			flag:death = 					{ debug_log = "Flavor: death" }
			flag:assassination_attempt = 	{ debug_log = "Flavor: assassination_attempt" }
			flag:abduction_attempt = 		{ debug_log = "Flavor: abduction_attempt" }
			flag:abduction = 				{ debug_log = "Flavor: abduction" }
			flag:landless = 				{ debug_log = "Flavor: landless" }
			flag:fallback = 				{ debug_log = "Flavor: fallback" }
			fallback = 		{ debug_log = "Animal: You have forgot one in the switch!?" }
		}
	}
	else = {
		scope:activity = {
			if = { limit = { scope:host = { is_imprisoned = yes } } 	debug_log = "Flavor: Host Imprisoned" }
			if = { limit = { scope:host = { is_landed = no } } 			debug_log = "Flavor: Host Unlanded" }
			if = { limit = { scope:host = { is_incapable = yes } } 		debug_log = "Flavor: Host Incapable" }
			if = { limit = { has_attending_activity_guests = no } 		debug_log = "Flavor: No one shows up" }
			if = { limit = { scope:host = { is_alive = no } } 			debug_log = "Flavor: # Host died" }
		}
	}
}

unop_log_hunted_animal_effect = {
	if = {
		limit = { scope:activity = { has_variable = animal_type } }
		switch = {
			trigger = scope:activity.var:animal_type
			flag:cat = 		{ debug_log = "Animal: cat" }
			flag:dog = 		{ debug_log = "Animal: dog" }
			flag:fox = 		{ debug_log = "Animal: fox" }
			flag:hare = 	{ debug_log = "Animal: hare" }
			flag:wolf = 	{ debug_log = "Animal: wolf" }
			flag:stag = 	{ debug_log = "Animal: stag" }
			flag:hart = 	{ debug_log = "Animal: hart" }
			flag:horse = 	{ debug_log = "Animal: horse" }
			#flag:rabbit = 	{ debug_log = "Animal: rabbit" } #Unop: This animal is never set actually
			flag:boar = 	{ debug_log = "Animal: boar" }
			flag:lion = 	{ debug_log = "Animal: lion" }
			flag:tiger = 	{ debug_log = "Animal: tiger" }
			flag:bear = 	{ debug_log = "Animal: bear" }
			flag:aurochs = 	{ debug_log = "Animal: aurochs" }
			flag:unicorn = 	{ debug_log = "Animal: unicorn" }
			flag:dragon = 	{ debug_log = "Animal: dragon" }
			flag:lynx = 	{ debug_log = "Animal: lynx" }
			flag:antelope = { debug_log = "Animal: antelope" }
			flag:gazelle = 	{ debug_log = "Animal: gazelle" }
			flag:roe = 		{ debug_log = "Animal: roe" }
			flag:saiga = 	{ debug_log = "Animal: saiga" }
			flag:reindeer = { debug_log = "Animal: reindeer" }
			#flag:buck = 	{ debug_log = "Animal: buck" }
			flag:elk = 		{ debug_log = "Animal: elk" }
			#flag:zubr = 	{ debug_log = "Animal: zubr" }
			#flag:wisent = 	{ debug_log = "Animal: wisent" }
			flag:bison = 	{ debug_log = "Animal: bison" }
			flag:leopard = 	{ debug_log = "Animal: leopard" }
			flag:hyena = 	{ debug_log = "Animal: hyena" }
			#flag:gorilla = 	{ debug_log = "Animal: gorilla" }
			#flag:shape = 	{ debug_log = "Animal: shape" }
			flag:stork = 	{ debug_log = "Animal: stork" }
			flag:crane = 	{ debug_log = "Animal: crane" }
			flag:heron = 	{ debug_log = "Animal: heron" }
			flag:swan = 	{ debug_log = "Animal: swan" }
			flag:egret = 	{ debug_log = "Animal: egret" }
			flag:pelican = 	{ debug_log = "Animal: pelican" }
			flag:flamingo = { debug_log = "Animal: flamingo" }
			flag:bustard = 	{ debug_log = "Animal: bustard" }
			flag:pheasant = { debug_log = "Animal: pheasant" }
			flag:goose = 	{ debug_log = "Animal: goose" }
			flag:grouse = 	{ debug_log = "Animal: grouse" }
			flag:duck = 	{ debug_log = "Animal: duck" }
			flag:quail = 	{ debug_log = "Animal: quail" }
			#flag:eagle = 	{ debug_log = "Animal: eagle" }
			#flag:vulture = 	{ debug_log = "Animal: vulture" }
			#flag:buzzard = 	{ debug_log = "Animal: buzzard" }
			flag:falcon = 	{ debug_log = "Animal: falcon" }
			flag:hawk = 	{ debug_log = "Animal: hawk" }
			#flag:raptor = 	{ debug_log = "Animal: raptor" }

			# Found in some events but nowhere for hunt
			#flag:moose = 	{ debug_log = "Animal: moose" }
			#flag:wolverine = 	{ debug_log = "Animal: wolverine" }
			#flag:jackal = 	{ debug_log = "Animal: jackal" }
			#flag:lark = 	{ debug_log = "Animal: lark" }
			#flag:bull = 	{ debug_log = "Animal: bull" }
			#flag:peacock = 	{ debug_log = "Animal: peacock" }
			#flag:serpent = 	{ debug_log = "Animal: serpent" }
			#flag:crocodile = 	{ debug_log = "Animal: crocodile" }
			#flag:elephants = 	{ debug_log = "Animal: elephants" }
			#flag:rhinoceros = 	{ debug_log = "Animal: rhinoceros" }
			#flag:hippopotamus = 	{ debug_log = "Animal: hippopotamus" }

			

			flag:none = 	{ debug_log = "Animal: none" }
			fallback = 		{ debug_log = "Animal: You have forgot one in the switch!?" }
		}
	}
	else = {
		debug_log = "Animal: undefined"
	}
}

#Just a version that check if the char is not already involved in the activity
unop_recruit_to_entourage_court_and_activity_effect = {
	$NEW_COURT_OWNER$ = {
		if = {
			limit = {
				$CHAR_TO_ADD$ = { is_pool_character = yes }
			}
			hidden_effect = { 
				current_travel_plan ?= { add_companion = $CHAR_TO_ADD$ } 
			}
			show_as_tooltip = { add_courtier = $CHAR_TO_ADD$ } #This already happens with add_companion
			if = {
				limit = { 
					exists = involved_activity
					NOT = { $CHAR_TO_ADD$ = { involved_activity = scope:activity } }
				}
				$CHAR_TO_ADD$ = { add_to_activity = $NEW_COURT_OWNER$.involved_activity }
			}
		}
		else = {
			if = {
				limit = {
					$CHAR_TO_ADD$ = { 
						NOT = { is_courtier_of = $NEW_COURT_OWNER$ }
					}
				}
				add_courtier = $CHAR_TO_ADD$
			}
			hidden_effect = { 
				current_travel_plan ?= { add_companion = $CHAR_TO_ADD$ } 
			}
			if = {
				limit = { 
					exists = involved_activity
					NOT = { $CHAR_TO_ADD$ = { involved_activity = scope:activity } }
				}
				$CHAR_TO_ADD$ = { add_to_activity = $NEW_COURT_OWNER$.involved_activity }
			}
		}
	}
}

# Crease a matrilineal betrothal between SPOUSE_1 and SPOUSE_2 with HOST promising to host a grand wedding
# SPOUSE_1 = First spouse character
# SPOUSE_2 = Second spouse character
# HOST = Character promising to host the grand wedding
create_grand_wedding_matrilineal_betrothal = {

	# Unop: Little sanity checks (are one of them married ? is this a same sex wedding without the game rule ?)
	if = {
		limit = {
			#debug_only = yes
			always = yes #Unop: It seems debug_only is false even in debug ??
		}
		if = {
			limit = { $SPOUSE_1$ = { is_married = yes } }
			debug_log = "Unop: create_grand_wedding_matrilineal_betrothal => SPOUSE_1 is already married"
		}
		if = {
			limit = { $SPOUSE_2$ = { is_married = yes } }
			debug_log = "Unop: create_grand_wedding_matrilineal_betrothal => SPOUSE_2 is already married"
		}
		if = {
			limit = { 
				$SPOUSE_1$ = { sex_same_as = $SPOUSE_2$ }
				allowed_to_marry_same_sex_trigger = no
			}
			debug_log = "Unop: create_grand_wedding_matrilineal_betrothal => Same sex wedding is disallowed by game rule"
		}
	}

	$SPOUSE_1$ = {
		create_betrothal_matrilineal = $SPOUSE_2$
	}
	
	set_grand_wedding_betrothal_variables = {
		SPOUSE_1 = $SPOUSE_1$
		SPOUSE_2 = $SPOUSE_2$
		HOST = $HOST$
		PROMISEE = $PROMISEE$
	}
}

# Unop: Post-marry variant of clean_grand_wedding_betrothal_variables — clears the variable from
# spouses (via every_spouse) rather than from betrothed, which is empty once they're married.
unop_clean_grand_wedding_betrothal_variables = {
	save_temporary_scope_as = unop_gw_host
	var:promised_grand_wedding_marriage_countdown = {
		remove_variable = promised_grand_wedding_by
		every_spouse = {
			limit = {
				has_variable = promised_grand_wedding_by
				var:promised_grand_wedding_by = scope:unop_gw_host
			}
			remove_variable = promised_grand_wedding_by
		}
	}
	remove_variable = promised_grand_wedding_marriage_countdown
	remove_variable = promised_grand_wedding_to
}

unop_add_renown_effect = {
	if = {
		limit = { exists = dynasty }
		save_scope_as = unop_char
		dynasty = { add_dynasty_prestige = scope:unop_char.dynasty.$PRESTIGE_MODIFIER$ }
	}
	else = {
		debug_log = "Unop: unop_add_renown_effect called but char is lowborn or current scope is not a character"
	}
}

unop_travel_completion_effect = {
	
	traveler_lifestyle_rank_up_check_effect = yes

	# Allocates a tiny bit of Horse XP if you have the Hastiluder trait
	if = {
		limit = {
			has_trait = tourney_participant
		}
		add_trait_xp = {
			trait = tourney_participant
			track = horse
			value = tournament_hastiludes_xp_gain_miniscule_value
		}
	}

	# Send all the hired companions back to the pool
	root.current_travel_plan = {
		every_entourage_character = {
			limit = {
				has_character_flag = travel_option_added_character
			}
			select_and_move_to_pool_effect = yes
		}
	}
}

blah_effect = {
	duel = {
		skills = { learning $SKILL$ }
		value = medium_skill_rating
		# Victory!
		50 = {
			compare_modifier = {
				value = scope:duel_value
				multiplier = 3.5
				min = -49
			}
			min = 5
			desc = laamp_base_learning_contract_events.4025.success.tt
			send_interface_toast = {
				title = laamp_base_learning_contract_events.4025.success.t
				left_icon = root
				if = {
					limit = { $SKILL$ = diplomacy }
					add_piety = 5
				}
				if = {
					limit = { $SKILL$ = martial }
					add_prestige = 5
				}
				if = {
					limit = { $SKILL$ = stewardship }
					add_gold = 5
				}
				if = {
					limit = { $SKILL$ = intrigue }
					dynasty ?= {
						add_dynasty_prestige = 5
					}
				}
			}
		}
		# Defeat!
		50 = {
			compare_modifier = {
				value = scope:duel_value
				multiplier = -3.5
				min = -49
			}
			min = 5
			desc = laamp_base_learning_contract_events.4025.failure.tt
			send_interface_toast = {
				title = laamp_base_learning_contract_events.4025.failure.t
				left_icon = root
				add_trait = blind
			}
		}
	}
}