﻿agot_start_secret_identity_effect = {
	save_scope_as = secret_child
	create_story = secret_identity_story

	#MW Execution Tooltip
	if = {
		limit = { exists = scope:king_executioner }
		show_as_tooltip = { death = { death_reason = death_execution killer = scope:king_executioner } }
	}

	#Set New Identity
	hidden_effect = {
		#Handle Claims
		if = {
			limit = { NOT = { has_character_flag = from_title_gain } }
			agot_get_secret_claims_generic_effect = yes
		}
		else = {
			agot_get_secret_claims_on_title_gain_effect = yes
		}

		if = {
			limit = { any_claim = { count > 0 } }
			every_claim = {
				prev = {
					remove_claim = prev
				}
			}
		}

		#Handle Relations
		agot_clear_all_relations = yes
		if = {
			limit = {
				has_trait = squire
				has_trait_xp = {
					trait = squire
					track = knight
					value < 100
				}
			}
			remove_trait = squire
		}

		# #AGOT HF ADDED (for canon child comp)
		# if = {
		# 	limit = { scope:secret_child.mother = { is_alive = yes } }
		# 	scope:secret_child.mother = {
		# 		add_to_variable_list = {
		# 			name = agot_hf_has_secret_child_list
		# 			target = scope:secret_child
		# 		}
		# 	}
		# }

		#Create Dupe Character
		create_character = {
			age = scope:secret_child.age
			gender = male
			gender_female_chance = {
				value = 0
				if = {
					limit = { scope:secret_child = { is_female = yes } }
					add = 100
				}
			}

			location = scope:secret_child.location

			random_traits = no

			faith = scope:secret_child.faith
			culture = scope:secret_child.culture
			dynasty = none


			save_scope_as = dupe_kid

			after_creation = {
				scope:secret_child = {
					set_variable = {
						name = secret_identity_character
						value = scope:dupe_kid
					}
				}
				copy_inheritable_appearance_from = scope:secret_child
				copy_traits = scope:secret_child
				change_first_name = { template_character = scope:secret_child }

				if = {
					limit = { exists = scope:secret_child.father }
					set_father = scope:secret_child.father
				}
				if = {
					limit = { exists = scope:secret_child.mother }
					set_mother = scope:secret_child.mother
				}
				if = {
					limit = { exists = scope:secret_child.house }
					set_house = scope:secret_child.house
				}
				if = {
					limit = { exists = scope:king_executioner }
					death = { death_reason = death_execution killer = scope:king_executioner }
				}
				else = {
					death = { death_reason = death_disappearance }
				}
			}
		}

		# AGOT HF MODIFIED
		if = {
			limit = { has_character_flag = agot_hf_special_character }
			#Create Fake Mother
			create_character = {
				gender = female
				age = { 75 80 }
				culture = scope:secret_child.culture
				faith = scope:secret_child.faith
				location = scope:secret_child.location
				dynasty = none
				save_scope_as = throwaway_mother
				after_creation = {
					scope:secret_child = {
						set_variable = {
							name = secret_identity_character_mother
							value = scope:throwaway_mother
						}
						#AGOT HF MODIFIED
						# if = {
						# 	limit = { is_female = yes }
						# 	change_first_name = { template_character = scope:throwaway_mother }
						# }
						if = {
							limit = { is_female = yes }
							random_list = {
								1 = {
									change_first_name = { template_character = scope:throwaway_mother }
								}
								1 = {
									trigger = {
										is_adult = no
										NOT = { has_character_flag = agot_hf_has_street_name }
										is_human = yes
										agot_can_be_ruler = yes
										agot_hf_not_canon_character = yes
									}
									agot_hf_street_name_effect = yes
								}
							}
						}
					}
					death = { death_reason = death_disappearance }
				}
			}

			#Create Fake Father
			create_character = {
				gender = male
				age = { 75 80 }
				culture = scope:secret_child.culture
				faith = scope:secret_child.faith
				location = scope:secret_child.location
				dynasty = none
				save_scope_as = throwaway_father
				after_creation = {
					scope:secret_child = {
						set_variable = {
							name = secret_identity_character_father
							value = scope:throwaway_father
						}
						#AGOT HF MODIFIED
						# if = {
						# 	limit = { is_male = yes }
						# 	change_first_name = { template_character = scope:throwaway_father }
						# }
						if = {
							limit = { is_male = yes }
							random_list = {
								1 = {
									change_first_name = { template_character = scope:throwaway_father }
								}
								1 = {
									trigger = {
										is_adult = no
										NOT = { has_character_flag = agot_hf_has_street_name }
										is_human = yes
										agot_can_be_ruler = yes
										agot_hf_not_canon_character = yes
									}
									agot_hf_street_name_effect = yes
								}
							}
						}
					}
					death = { death_reason = death_disappearance }
				}
			}
		}
		else = {
			#Create Fake Mother
			create_character = {
				gender = female
				age = { 33 50 }
				culture = scope:secret_child.culture
				faith = scope:secret_child.faith
				location = scope:secret_child.location
				dynasty = none
				save_scope_as = throwaway_mother
				after_creation = {
					scope:secret_child = {
						set_variable = {
							name = secret_identity_character_mother
							value = scope:throwaway_mother
						}
						#AGOT HF MODIFIED
						# if = {
						# 	limit = { is_female = yes }
						# 	change_first_name = { template_character = scope:throwaway_mother }
						# }
						if = {
							limit = { is_female = yes }
							random_list = {
								1 = {
									change_first_name = { template_character = scope:throwaway_mother }
								}
								1 = {
									trigger = {
										is_adult = no
										NOT = { has_character_flag = agot_hf_has_street_name }
										is_human = yes
										agot_can_be_ruler = yes
										agot_hf_not_canon_character = yes
									}
									agot_hf_street_name_effect = yes
								}
							}
						}
					}
					death = { death_reason = death_disappearance }
				}
			}

			#Create Fake Father
			create_character = {
				gender = male
				age = { 33 50 }
				culture = scope:secret_child.culture
				faith = scope:secret_child.faith
				location = scope:secret_child.location
				dynasty = none
				save_scope_as = throwaway_father
				after_creation = {
					scope:secret_child = {
						set_variable = {
							name = secret_identity_character_father
							value = scope:throwaway_father
						}
						#AGOT HF MODIFIED
						# if = {
						# 	limit = { is_male = yes }
						# 	change_first_name = { template_character = scope:throwaway_father }
						# }
						if = {
							limit = { is_male = yes }
							random_list = {
								1 = {
									change_first_name = { template_character = scope:throwaway_father }
								}
								1 = {
									trigger = {
										is_adult = no
										NOT = { has_character_flag = agot_hf_has_street_name }
										is_human = yes
										agot_can_be_ruler = yes
										agot_hf_not_canon_character = yes
									}
									agot_hf_street_name_effect = yes
								}
							}
						}
					}
					death = { death_reason = death_disappearance }
				}
			}
		}
		# create_character = {
		# 	gender = female
		# 	age = { 33 50 }
		# 	culture = scope:secret_child.culture
		# 	faith = scope:secret_child.faith
		# 	location = scope:secret_child.location
		# 	dynasty = none
		# 	save_scope_as = throwaway_mother
		# 	after_creation = {
		# 		scope:secret_child = {
		# 			set_variable = {
		# 				name = secret_identity_character_mother
		# 				value = scope:throwaway_mother
		# 			}
		# 			if = {
		# 				limit = { is_female = yes }
		# 				change_first_name = { template_character = scope:throwaway_mother }
		# 			}
		# 		}
		# 		death = { death_reason = death_disappearance }
		# 	}
		# }

		# #Create Fake Father
		# create_character = {
		# 	gender = male
		# 	age = { 33 50 }
		# 	culture = scope:secret_child.culture
		# 	faith = scope:secret_child.faith
		# 	location = scope:secret_child.location
		# 	dynasty = none
		# 	save_scope_as = throwaway_father
		# 	after_creation = {
		# 		scope:secret_child = {
		# 			set_variable = {
		# 				name = secret_identity_character_father
		# 				value = scope:throwaway_father
		# 			}
		# 			if = {
		# 				limit = { is_male = yes }
		# 				change_first_name = { template_character = scope:throwaway_father }
		# 			}
		# 		}
		# 		death = { death_reason = death_disappearance }
		# 	}
		# }


		#Give new identity to the secret character
		scope:secret_child = {
			set_mother = scope:throwaway_mother
			set_father = scope:throwaway_father
			set_to_lowborn = yes
			if = {
				limit = { exists = var:secret_identity_character.father }
				#AGOT HF MODIFIED
				# set_real_father = var:secret_identity_character.father
				if = {
					limit = { exists = scope:secret_child.var:agot_hf_the_real_real_father }
					set_real_father = var:agot_hf_the_real_real_father
				}
				else = { set_real_father = var:secret_identity_character.father }
			}
			if = {
				limit = { exists = var:secret_identity_character.mother }
				set_real_mother = var:secret_identity_character.mother
			}

			#Move secret child to new location
			# AGOT HF MODIFIED
			# trigger_event = {
			# 	id = agot_secret_events.2000
			# 	days = 7
			# }
			if = {
				limit = {
					NOT = { has_character_flag = agot_hf_special_character }
					exists = var:secret_claim_capital
				}
				trigger_event = {
					id = agot_secret_events.2000
					days = 7
				}
			}

			add_character_flag = has_secret_identity
			add_character_flag = generic_secret_identity
			add_secret = { type = secret_agot_identity }

			#Checking if they are the selected one
			if = {
				limit = {
					scope:secret_child = { has_character_flag = secret_identity_candidate }
				}
				scope:king_executioner = {
					add_to_variable_list = {
						name = random_secret_child_pool
						target = scope:secret_child
					}
				}
			}

			#AGOT HF ADDED
			if = {
				limit = {
					exists = scope:secret_child.var:agot_hf_the_real_real_father
					has_trait = disputed_heritage
				}
				remove_trait = disputed_heritage
			}
		}

		#Keep only one secret child after MW punishment
		if = {
			limit = {
				scope:secret_child = { has_character_flag = secret_identity_candidate }
			}
			scope:king_executioner = {
				trigger_event = {
					id = agot_secret_events.2001
					days = 1
				}
			}
			scope:secret_child = {
				trigger_event = {
					id = agot_secret_events.2002
					days = 3
				}
			}
		}
	}
}

agot_end_secret_identity_effect = {
	hidden_effect = {
		#Handle Children
		if = {
			limit = {
				OR = {
					AND = {
						is_lowborn = no
						is_female = yes
						matrilinear_marriage = yes
						any_child = {
							count > 0
							house = scope:secret_child.house
						}
					}
					AND = {
						is_lowborn = no
						is_male = yes
						matrilinear_marriage = no
						any_child = {
							count > 0
							house = scope:secret_child.house
						}
					}
					AND = {
						is_lowborn = yes
						any_child = {
							count > 0
							is_lowborn = yes
						}
					}
				}
			}
			if = {
				limit = { is_lowborn = yes }
				every_child = {
					limit = { is_lowborn = yes }
					set_house = scope:secret_child.var:secret_identity_character.house
				}
			}
			else = {
				every_child = {
					limit = { house = scope:secret_child.house }
					set_house = scope:secret_child.var:secret_identity_character.house
				}
			}
		}

		#Handle Character Identity
		if = {
			limit = { exists = var:secret_identity_character.father }
			set_father = var:secret_identity_character.father
		}
		if = {
			limit = { exists = var:secret_identity_character.mother }
			set_mother = var:secret_identity_character.mother
		}
		if = {
			limit = { exists = var:secret_identity_character.house }
			set_house = var:secret_identity_character.house
		}
		change_first_name = { template_character = var:secret_identity_character }

		#AGOT HF ADDED
		if = {
			limit = {
				exists = scope:secret_child.var:agot_hf_the_real_real_father
				NOT = { has_trait = disputed_heritage }
			}
			add_trait = disputed_heritage
		}

		#Handle Title CoA
		if = {
			limit = { is_playable_character = yes }
			trigger_event = agot_coa_events.0001
		}

		#Handle Claims
		if = {
			limit = { exists = var:secret_claim_capital }
			add_pressed_claim = var:secret_claim_capital
		}
		if = {
			limit = { exists = var:secret_claim_capital_duchy }
			add_pressed_claim = var:secret_claim_capital_duchy
		}
		if = {
			limit = { exists = var:secret_claim_capital_kingdom }
			add_pressed_claim = var:secret_claim_capital_kingdom
		}
		if = {
			limit = { exists = var:secret_claim_capital_empire }
			add_pressed_claim = var:secret_claim_capital_empire
		}
		if = {
			limit = { exists = var:secret_claim_capital_hegemony }
			add_pressed_claim = var:secret_claim_capital_hegemony
		}

		#Handle Dupe Character Identity
		var:secret_identity_character = {
			set_father = scope:secret_child.var:secret_identity_character_father
			set_mother = scope:secret_child.var:secret_identity_character_mother
			set_to_lowborn = yes
			if = {
				limit = { is_male = yes }
				change_first_name = { template_character = scope:secret_child.var:secret_identity_character_father }
			}
			else = { change_first_name = { template_character = scope:secret_child.var:secret_identity_character_mother } }
		}

		remove_character_flag = has_secret_identity
		remove_character_flag = generic_secret_identity
		agot_events_after_identity_reveal = yes

		if = {
			limit = {
				any_owned_story = {
					story_type = secret_identity_story
				}
			}
			random_owned_story = {
				limit = { story_type = secret_identity_story }
				end_story = yes
			}
		}

		if = {
			limit = { has_character_flag = secret_child_is_adult }
			remove_character_flag = secret_child_is_adult
		}
	}
}

agot_swap_secret_child_bodies = {
	#Handle Character Identity
	scope:secret_child = {
		father = { save_scope_as = dupe_father }
		mother = { save_scope_as = dupe_mother }

		if = {
			limit = { exists = scope:dupe_char.father }
			set_father = scope:dupe_char.father
		}
		if = {
			limit = { exists = scope:dupe_char.mother }
			set_mother = scope:dupe_char.mother
		}
		if = {
			limit = { exists = scope:dupe_char.house }
			set_house = scope:dupe_char.house
		}
		change_first_name = { template_character = scope:dupe_char }
	}

	#Handle Dupe Character Identity
	scope:dupe_char = {
		set_father = scope:dupe_father
		set_mother = scope:dupe_mother
		set_to_lowborn = yes
		if = {
			limit = { is_male = yes }
			change_first_name = { template_character = scope:dupe_father }
		}
		else = { change_first_name = { template_character = scope:dupe_mother } }
	}
}

agot_move_secret_child = {
	if = {
		limit = { exists = var:secret_claim_capital_hegemony }
		var:secret_claim_capital_hegemony.holder = { agot_get_worst_vassal = yes }
		secret_flee_effect = yes
	}
	else_if = {
		limit = { exists = var:secret_claim_capital_empire }
		var:secret_claim_capital_empire.holder = { agot_get_worst_vassal = yes }
		secret_flee_effect = yes
	}
	else_if = {
		limit = { exists = var:secret_claim_capital_kingdom }
		var:secret_claim_capital_kingdom.holder = { agot_get_worst_vassal = yes }
		secret_flee_effect = yes
	}
	else_if = {
		limit = { exists = var:secret_claim_capital_duchy }
		var:secret_claim_capital_duchy.holder = { agot_get_worst_vassal = yes }
		secret_flee_effect = yes
	}
	else_if = {
		limit = { exists = var:secret_claim_capital }
		secret_flee_further_effect = yes
	}
}

secret_flee_effect = {
	if = {
		limit = {
			NOT = { has_character_flag = flee_further }
			has_character_flag = flee_closer
		}
		scope:worst_vassal = { add_courtier = scope:secret_child }
	}
	else = {
		secret_flee_further_effect = yes
	}
}

secret_flee_further_effect = {
	if = {
		limit = {
			var:secret_claim_capital.holder.top_liege = {
				any_vassal_or_below = {
					primary_title.tier > tier_barony
				}
			}
		}
		var:secret_claim_capital.holder.top_liege = {
			random_vassal_or_below = {
				limit = { primary_title.tier > tier_barony }
				add_courtier = scope:secret_child
			}
		}
	}
	else = {
		random_ruler = {
			limit = { primary_title.tier > tier_barony }
			add_courtier = scope:secret_child
		}
	}
}

agot_get_worst_vassal = {
	if = {
		limit = {
			scope:secret_child = {
				NOT = { exists = liege }
			}
		}
		scope:secret_child = {
			add_character_flag = {
				flag = flee_further
				days = 90
			}
		}
	}
	else_if = {
		limit = {
			any_vassal = {
				primary_title.tier > tier_barony
				exists = location
				is_human = yes
				is_adult = yes
				opinion = {
					target = scope:secret_child.var:secret_claim_capital.holder
					value < 0
				}
				scope:secret_child = {
					NOT = { is_courtier_of = prev }
				}
			}
		}
		random_vassal = {
			limit = {
				primary_title.tier > tier_barony
				exists = location
				is_human = yes
				is_adult = yes
				opinion = {
					target = scope:secret_child.var:secret_claim_capital.holder
					value < 0
				}
				scope:secret_child = {
					NOT = { is_courtier_of = prev }
				}
			}
			save_scope_as = worst_vassal
			scope:secret_child = {
				add_character_flag = {
					flag = flee_closer
					days = 90
				}
			}
		}
	}
	else_if = {
		limit = {
			any_vassal = {
				primary_title.tier > tier_barony
				exists = location
				is_human = yes
				is_adult = yes
				scope:secret_child = {
					NOT = { is_courtier_of = prev }
				}
			}
		}
		random_vassal = {
			limit = {
				primary_title.tier > tier_barony
				exists = location
				is_human = yes
				is_adult = yes
				scope:secret_child = {
					NOT = { is_courtier_of = prev }
				}
			}
			save_scope_as = worst_vassal
			scope:secret_child = {
				add_character_flag = {
					flag = flee_closer
					days = 90
				}
			}
		}
	}
	else = {
		add_character_flag = {
			flag = flee_further
			days = 90
		}
	}
}

agot_get_secret_claims_generic_effect = {
	if = {
		limit = {
			scope:secret_child.house.house_head = {
				highest_held_title_tier = tier_hegemony
			}
		}
		set_variable = {
			name = secret_claim_capital
			value = scope:secret_child.house.house_head.capital_county
		}
		if = {
			limit = {
				scope:secret_child.house.house_head.capital_county.duchy.holder = scope:secret_child.house.house_head
			}
			set_variable = {
				name = secret_claim_capital_duchy
				value = scope:secret_child.house.house_head.capital_county.duchy
			}
		}
		if = {
			limit = {
				scope:secret_child.house.house_head.capital_county.kingdom.holder = scope:secret_child.house.house_head
			}
			set_variable = {
				name = secret_claim_capital_kingdom
				value = scope:secret_child.house.house_head.capital_county.kingdom
			}
		}
		if = {
			limit = {
				scope:secret_child.house.house_head.capital_county.empire.holder = scope:secret_child.house.house_head
			}
			set_variable = {
				name = secret_claim_capital_empire
				value = scope:secret_child.house.house_head.capital_county.empire
			}
		}
		if = {
			limit = {
				exists = scope:secret_child.house.house_head.capital_county.hegemony
				scope:secret_child.house.house_head.capital_county.hegemony.holder = scope:secret_child.house.house_head
			}
			set_variable = {
				name = secret_claim_capital_hegemony
				value = scope:secret_child.house.house_head.capital_county.hegemony
			}
		}
	}
	if = {
		limit = {
			scope:secret_child.house.house_head = {
				highest_held_title_tier = tier_empire
			}
		}
		set_variable = {
			name = secret_claim_capital
			value = scope:secret_child.house.house_head.capital_county
		}
		if = {
			limit = {
				scope:secret_child.house.house_head.capital_county.duchy.holder = scope:secret_child.house.house_head
			}
			set_variable = {
				name = secret_claim_capital_duchy
				value = scope:secret_child.house.house_head.capital_county.duchy
			}
		}
		if = {
			limit = {
				scope:secret_child.house.house_head.capital_county.kingdom.holder = scope:secret_child.house.house_head
			}
			set_variable = {
				name = secret_claim_capital_kingdom
				value = scope:secret_child.house.house_head.capital_county.kingdom
			}
		}
		if = {
			limit = {
				scope:secret_child.house.house_head.capital_county.empire.holder = scope:secret_child.house.house_head
			}
			set_variable = {
				name = secret_claim_capital_empire
				value = scope:secret_child.house.house_head.capital_county.empire
			}
		}
	}
	if = {
		limit = {
			scope:secret_child.house.house_head = {
				highest_held_title_tier = tier_kingdom
			}
		}
		set_variable = {
			name = secret_claim_capital
			value = scope:secret_child.house.house_head.capital_county
		}
		if = {
			limit = {
				scope:secret_child.house.house_head.capital_county.duchy.holder = scope:secret_child.house.house_head
			}
			set_variable = {
				name = secret_claim_capital_duchy
				value = scope:secret_child.house.house_head.capital_county.duchy
			}
		}
		if = {
			limit = {
				scope:secret_child.house.house_head.capital_county.kingdom.holder = scope:secret_child.house.house_head
			}
			set_variable = {
				name = secret_claim_capital_kingdom
				value = scope:secret_child.house.house_head.capital_county.kingdom
			}
		}
	}
	if = {
		limit = {
			scope:secret_child.house.house_head = {
				highest_held_title_tier = tier_duchy
			}
		}
		set_variable = {
			name = secret_claim_capital
			value = scope:secret_child.house.house_head.capital_county
		}
		if = {
			limit = {
				scope:secret_child.house.house_head.capital_county.duchy.holder = scope:secret_child.house.house_head
			}
			set_variable = {
				name = secret_claim_capital_duchy
				value = scope:secret_child.house.house_head.capital_county.duchy
			}
		}
	}
	if = {
		limit = {
			scope:secret_child.house.house_head = {
				highest_held_title_tier = tier_county
			}
		}
		set_variable = {
			name = secret_claim_capital
			value = scope:secret_child.house.house_head.capital_county
		}
	}
}

agot_get_secret_claims_on_title_gain_effect = {
	if = {
		limit = { scope:title.tier = tier_hegemony }
		grab_secret_claim_capital_effect = yes
		grab_secret_claim_capital_duchy_effect = yes
		grab_secret_claim_capital_kingdom_effect = yes
		grab_secret_claim_capital_empire_effect = yes
		grab_secret_claim_capital_hegemony_effect = yes
	}
	if = {
		limit = { scope:title.tier = tier_empire }
		grab_secret_claim_capital_effect = yes
		grab_secret_claim_capital_duchy_effect = yes
		grab_secret_claim_capital_kingdom_effect = yes
		grab_secret_claim_capital_empire_effect = yes
	}
	if = {
		limit = { scope:title.tier = tier_kingdom }
		grab_secret_claim_capital_effect = yes
		grab_secret_claim_capital_duchy_effect = yes
		grab_secret_claim_capital_kingdom_effect = yes
	}
	if = {
		limit = { scope:title.tier = tier_duchy }
		grab_secret_claim_capital_effect = yes
		grab_secret_claim_capital_duchy_effect = yes
	}
	if = {
		limit = { scope:title.tier = tier_county }
		grab_secret_claim_capital_effect = yes
	}
}

grab_secret_claim_capital_effect = {
	if = {
		limit = {
			scope:title = scope:title.holder.capital_county
		}
		set_variable = {
			name = secret_claim_capital
			value = scope:title.holder.capital_county
		}
	}
	else_if = {
		limit = {
			NOT = { has_variable = secret_claim_capital }
			scope:title.holder = {
				any_held_county = {
					any_past_holder = {
						this = scope:previous_holder
					}
				}
			}
		}
		scope:title.holder = {
			random_held_county = {
				limit = {
					any_past_holder = {
						this = scope:previous_holder
					}
				}
				scope:secret_child = {
					set_variable = {
						name = secret_claim_capital
						value = prev
					}
				}
			}
		}
	}
	#fallback
	else = {
		scope:title.holder = {
			random_held_county = {
				scope:secret_child = {
					set_variable = {
						name = secret_claim_capital
						value = prev
					}
				}
			}
		}
	}
}

grab_secret_claim_capital_duchy_effect = {
	if = {
		limit = {
			scope:title = scope:title.holder.capital_county.duchy
		}
		set_variable = {
			name = secret_claim_capital_duchy
			value = scope:title.holder.capital_county.duchy
		}
	}
	else_if = {
		limit = {
			NOT = { has_variable = secret_claim_capital_duchy }
			scope:title.holder = {
				any_held_title = {
					tier = tier_duchy
					any_past_holder = {
						this = scope:previous_holder
					}
				}
			}
		}
		scope:title.holder = {
			random_held_title = {
				limit = {
					tier = tier_duchy
					any_past_holder = {
						this = scope:previous_holder
					}
				}
				scope:secret_child = {
					set_variable = {
						name = secret_claim_capital_duchy
						value = prev
					}
				}
			}
		}
	}
	#fallback
	else = {
		scope:title.holder = {
			random_held_title = {
				limit = {
					tier = tier_duchy
				}
				scope:secret_child = {
					set_variable = {
						name = secret_claim_capital_duchy
						value = prev
					}
				}
			}
		}
	}
}

grab_secret_claim_capital_kingdom_effect = {
	if = {
		limit = {
			scope:title = scope:title.holder.capital_county.kingdom
		}
		set_variable = {
			name = secret_claim_capital_kingdom
			value = scope:title.holder.capital_county.kingdom
		}
	}
	else_if = {
		limit = {
			NOT = { has_variable = secret_claim_capital_kingdom }
			scope:title.holder = {
				any_held_title = {
					tier = tier_kingdom
					any_past_holder = {
						this = scope:previous_holder
					}
				}
			}
		}
		scope:title.holder = {
			random_held_title = {
				limit = {
					tier = tier_kingdom
					any_past_holder = {
						this = scope:previous_holder
					}
				}
				scope:secret_child = {
					set_variable = {
						name = secret_claim_capital_kingdom
						value = prev
					}
				}
			}
		}
	}
	#fallback
	else = {
		scope:title.holder = {
			random_held_title = {
				limit = {
					tier = tier_kingdom
				}
				scope:secret_child = {
					set_variable = {
						name = secret_claim_capital_kingdom
						value = prev
					}
				}
			}
		}
	}
}

grab_secret_claim_capital_empire_effect = {
	if = {
		limit = {
			scope:title = scope:title.holder.capital_county.empire
		}
		set_variable = {
			name = secret_claim_capital_empire
			value = scope:title.holder.capital_county.empire
		}
	}
	else_if = {
		limit = {
			NOT = { has_variable = secret_claim_capital_empire }
			scope:title.holder = {
				any_held_title = {
					tier = tier_empire
					any_past_holder = {
						this = scope:previous_holder
					}
				}
			}
		}
		scope:title.holder = {
			random_held_title = {
				limit = {
					tier = tier_empire
					any_past_holder = {
						this = scope:previous_holder
					}
				}
				scope:secret_child = {
					set_variable = {
						name = secret_claim_capital_empire
						value = prev
					}
				}
			}
		}
	}
	#fallback
	else = {
		scope:title.holder = {
			random_held_title = {
				limit = {
					tier = tier_empire
				}
				scope:secret_child = {
					set_variable = {
						name = secret_claim_capital_empire
						value = prev
					}
				}
			}
		}
	}
}

grab_secret_claim_capital_hegemony_effect = {
	if = {
		limit = {
			scope:title = scope:title.holder.capital_county.hegemony
		}
		set_variable = {
			name = secret_claim_capital_hegemony
			value = scope:title.holder.capital_county.hegemony
		}
	}
	else_if = {
		limit = {
			NOT = { has_variable = secret_claim_capital_hegemony }
			scope:title.holder = {
				any_held_title = {
					tier = tier_hegemony
					any_past_holder = {
						this = scope:previous_holder
					}
				}
			}
		}
		scope:title.holder = {
			random_held_title = {
				limit = {
					tier = tier_hegemony
					any_past_holder = {
						this = scope:previous_holder
					}
				}
				scope:secret_child = {
					set_variable = {
						name = secret_claim_capital_hegemony
						value = prev
					}
				}
			}
		}
	}
	#fallback
	else = {
		scope:title.holder = {
			random_held_title = {
				limit = {
					tier = tier_hegemony
				}
				scope:secret_child = {
					set_variable = {
						name = secret_claim_capital_hegemony
						value = prev
					}
				}
			}
		}
	}
}


agot_secret_child_start_war_effect = {
	if = {
		limit = { scope:secret_child = { is_landless_adventurer = yes } }
		if = {
			limit = { exists = scope:secret_child.var:secret_claim_capital_hegemony }
			scope:secret_child.var:secret_claim_capital_hegemony.holder = { save_scope_as = war_target }
			start_war = {
				casus_belli = claim_cb
				target = scope:secret_child.var:secret_claim_capital_hegemony.holder
				target_title = scope:secret_child.var:secret_claim_capital_hegemony
				target_title = scope:secret_child.var:secret_claim_capital_empire
				target_title = scope:secret_child.var:secret_claim_capital_kingdom
				target_title = scope:secret_child.var:secret_claim_capital_duchy
				target_title = scope:secret_child.var:secret_claim_capital
				claimant = scope:secret_child
			}
			hidden_effect = { agot_create_secret_character_army_effect = yes }
		}
		else_if = {
			limit = { exists = scope:secret_child.var:secret_claim_capital_empire }
			scope:secret_child.var:secret_claim_capital_empire.holder = { save_scope_as = war_target }
			start_war = {
				casus_belli = claim_cb
				target = scope:secret_child.var:secret_claim_capital_empire.holder
				target_title = scope:secret_child.var:secret_claim_capital_empire
				target_title = scope:secret_child.var:secret_claim_capital_kingdom
				target_title = scope:secret_child.var:secret_claim_capital_duchy
				target_title = scope:secret_child.var:secret_claim_capital
				claimant = scope:secret_child
			}
			hidden_effect = { agot_create_secret_character_army_effect = yes }
		}
		else_if = {
			limit = { exists = scope:secret_child.var:secret_claim_capital_kingdom }
			scope:secret_child.var:secret_claim_capital_kingdom.holder = { save_scope_as = war_target }
			start_war = {
				casus_belli = claim_cb
				target = scope:secret_child.var:secret_claim_capital_kingdom.holder
				target_title = scope:secret_child.var:secret_claim_capital_kingdom
				target_title = scope:secret_child.var:secret_claim_capital_duchy
				target_title = scope:secret_child.var:secret_claim_capital
				claimant = scope:secret_child
			}
			hidden_effect = { agot_create_secret_character_army_effect = yes }
		}
		else_if = {
			limit = { exists = scope:secret_child.var:secret_claim_capital_duchy }
			scope:secret_child.var:secret_claim_capital_duchy.holder = { save_scope_as = war_target }
			start_war = {
				casus_belli = claim_cb
				target = scope:secret_child.var:secret_claim_capital_duchy.holder
				target_title = scope:secret_child.var:secret_claim_capital_duchy
				target_title = scope:secret_child.var:secret_claim_capital
				claimant = scope:secret_child
			}
			hidden_effect = { agot_create_secret_character_army_effect = yes }
		}
		else_if = {
			limit = { exists = scope:secret_child.var:secret_claim_capital }
			scope:secret_child.var:secret_claim_capital.holder = { save_scope_as = war_target }
			start_war = {
				casus_belli = claim_cb
				target = scope:secret_child.var:secret_claim_capital.holder
				target_title = scope:secret_child.var:secret_claim_capital
				claimant = scope:secret_child
			}
			hidden_effect = { agot_create_secret_character_army_effect = yes }
		}
	}
	else = {
		if = {
			limit = { exists = scope:secret_child.var:secret_claim_capital_hegemony }
			scope:secret_child.var:secret_claim_capital_hegemony.holder = { save_scope_as = war_target }
			start_war = {
				casus_belli = claim_cb
				target = scope:secret_child.var:secret_claim_capital_hegemony.holder
				target_title = scope:secret_child.var:secret_claim_capital_hegemony
				target_title = scope:secret_child.var:secret_claim_capital_empire
				target_title = scope:secret_child.var:secret_claim_capital_kingdom
				target_title = scope:secret_child.var:secret_claim_capital_duchy
				target_title = scope:secret_child.var:secret_claim_capital
				claimant = scope:secret_child
			}
			hidden_effect = { agot_create_secret_character_army_effect = yes }
		}
		else_if = {
			limit = { exists = scope:secret_child.var:secret_claim_capital_empire }
			scope:secret_child.var:secret_claim_capital_empire.holder = { save_scope_as = war_target }
			start_war = {
				casus_belli = claim_cb
				target = scope:secret_child.var:secret_claim_capital_empire.holder
				target_title = scope:secret_child.var:secret_claim_capital_empire
				target_title = scope:secret_child.var:secret_claim_capital_kingdom
				target_title = scope:secret_child.var:secret_claim_capital_duchy
				target_title = scope:secret_child.var:secret_claim_capital
				claimant = scope:secret_child
			}
			hidden_effect = { agot_create_secret_character_army_effect = yes }
		}
		else_if = {
			limit = { exists = scope:secret_child.var:secret_claim_capital_kingdom }
			scope:secret_child.var:secret_claim_capital_kingdom.holder = { save_scope_as = war_target }
			start_war = {
				casus_belli = claim_cb
				target = scope:secret_child.var:secret_claim_capital_kingdom.holder
				target_title = scope:secret_child.var:secret_claim_capital_kingdom
				target_title = scope:secret_child.var:secret_claim_capital_duchy
				target_title = scope:secret_child.var:secret_claim_capital
				claimant = scope:secret_child
			}
			hidden_effect = { agot_create_secret_character_army_effect = yes }
		}
		else_if = {
			limit = { exists = scope:secret_child.var:secret_claim_capital_duchy }
			scope:secret_child.var:secret_claim_capital_duchy.holder = { save_scope_as = war_target }
			start_war = {
				casus_belli = claim_cb
				target = scope:secret_child.var:secret_claim_capital_duchy.holder
				target_title = scope:secret_child.var:secret_claim_capital_duchy
				target_title = scope:secret_child.var:secret_claim_capital
				claimant = scope:secret_child
			}
			hidden_effect = { agot_create_secret_character_army_effect = yes }
		}
		else_if = {
			limit = { exists = scope:secret_child.var:secret_claim_capital }
			scope:secret_child.var:secret_claim_capital.holder = { save_scope_as = war_target }
			start_war = {
				casus_belli = claim_cb
				target = scope:secret_child.var:secret_claim_capital.holder
				target_title = scope:secret_child.var:secret_claim_capital
				claimant = scope:secret_child
			}
			hidden_effect = { agot_create_secret_character_army_effect = yes }
		}
	}
}

agot_events_after_identity_reveal = {
	if = {
		limit = { exists = scope:secret_child.var:secret_claim_capital.holder }
		scope:secret_child.var:secret_claim_capital.holder = { save_scope_as = the_holder }
		scope:the_holder = {
			if = {
				limit = { exists = scope:the_holder.liege }
				scope:the_holder.liege = {
					trigger_event = {
						id = agot_secret_events.2009
						days = 1
					}
					if = {
						limit = {
							any_vassal_or_below = {
								primary_title.tier >= tier_county
							}
						}
						every_vassal_or_below = {
							limit = {
								primary_title.tier >= tier_county
							}
							trigger_event = {
								id = agot_secret_events.2009
								days = 1
							}
						}
					}
				}
			}
			else = {
				trigger_event = {
					id = agot_secret_events.2009
					days = 1
				}
				if = {
					limit = {
						any_vassal_or_below = {
							primary_title.tier >= tier_county
						}
					}
					every_vassal_or_below = {
						limit = {
							primary_title.tier >= tier_county
						}
						trigger_event = {
							id = agot_secret_events.2009
							days = 1
						}
					}
				}
			}
		}
	}

	scope:secret_child = {
		if = {
			limit = {
				exists = liege.liege
				liege.liege = {
					any_vassal_or_below = {
						primary_title.tier >= tier_county
					}
				}
			}
			liege.liege = {
				trigger_event = agot_secret_events.2009
				add_character_flag = {
					flag = reveal_notification_already_sent
					days = 3
				}
				every_vassal_or_below = {
					limit = { primary_title.tier >= tier_county }
					trigger_event = agot_secret_events.2009
					add_character_flag = {
						flag = reveal_notification_already_sent
						days = 3
					}
				}
			}
		}
		else_if = {
			limit = {
				exists = liege
				liege = {
					any_vassal_or_below = {
						primary_title.tier >= tier_county
					}
				}
			}
			liege = {
				trigger_event = agot_secret_events.2009
				add_character_flag = {
					flag = reveal_notification_already_sent
					days = 3
				}
				every_vassal_or_below = {
					limit = { primary_title.tier >= tier_county }
					trigger_event = agot_secret_events.2009
					add_character_flag = {
						flag = reveal_notification_already_sent
						days = 3
					}
				}
			}
		}
	}
}

agot_create_secret_character_army_effect = {
	#Enemy is Duke tier ruler
	if = {
		limit = { scope:war_target.primary_title.tier = tier_duchy }
		if = {
			limit = {
				scope:secret_child = {
					OR = {
						has_trait = education_diplomacy_1
						has_trait = education_martial_1
						has_trait = education_stewardship_1
						has_trait = education_intrigue_1
						has_trait = education_learning_1
					}
				}
			}
			spawn_army = {
				levies = 0
				men_at_arms = {
					type = armored_footmen
					stacks = { 1 3 }
				}
				men_at_arms = {
					type = light_footmen
					stacks = { 3 5 }
				}
				inheritable = no
				location = scope:secret_child.location
				name = secret_spawned_army
			}
		}
		else_if = {
			limit = {
				scope:secret_child = {
					OR = {
						has_trait = education_diplomacy_2
						has_trait = education_martial_2
						has_trait = education_stewardship_2
						has_trait = education_intrigue_2
						has_trait = education_learning_2
					}
				}
			}
			spawn_army = {
				levies = 0
				men_at_arms = {
					type = armored_footmen
					stacks = { 3 4 }
				}
				men_at_arms = {
					type = light_footmen
					stacks = { 5 8 }
				}
				men_at_arms = {
					type = bowmen
					stacks = 1
				}
				inheritable = no
				location = scope:secret_child.location
				name = secret_spawned_army
			}
		}
		else_if = {
			limit = {
				scope:secret_child = {
					OR = {
						has_trait = education_diplomacy_3
						has_trait = education_martial_3
						has_trait = education_stewardship_3
						has_trait = education_intrigue_3
						has_trait = education_learning_3
					}
				}
			}
			spawn_army = {
				levies = 0
				men_at_arms = {
					type = armored_footmen
					stacks = { 4 6 }
				}
				men_at_arms = {
					type = light_footmen
					stacks = { 8 16 }
				}
				men_at_arms = {
					type = bowmen
					stacks = 2
				}
				inheritable = no
				location = scope:secret_child.location
				name = secret_spawned_army
			}
		}
		else_if = {
			limit = {
				scope:secret_child = {
					OR = {
						has_trait = education_diplomacy_4
						has_trait = education_martial_4
						has_trait = education_stewardship_4
						has_trait = education_intrigue_4
						has_trait = education_learning_4
					}
				}
			}
			spawn_army = {
				levies = 0
				men_at_arms = {
					type = armored_footmen
					stacks = { 6 9 }
				}
				men_at_arms = {
					type = light_footmen
					stacks = { 16 20 }
				}
				men_at_arms = {
					type = bowmen
					stacks = 3
				}
				inheritable = no
				location = scope:secret_child.location
				name = secret_spawned_army
			}
		}
		else_if = {
			limit = {
				scope:secret_child = {
					OR = {
						has_trait = education_diplomacy_5
						has_trait = education_martial_5
						has_trait = education_stewardship_5
						has_trait = education_intrigue_5
						has_trait = education_learning_5
					}
				}
			}
			spawn_army = {
				levies = 0
				men_at_arms = {
					type = armored_footmen
					stacks = { 9 12 }
				}
				men_at_arms = {
					type = light_footmen
					stacks = { 20 26 }
				}
				men_at_arms = {
					type = bowmen
					stacks = 4
				}
				inheritable = no
				location = scope:secret_child.location
				name = secret_spawned_army
			}
		}
	}
	#Enemy is King or Emperor tier ruler
	else_if = {
		limit = { scope:war_target.primary_title.tier > tier_duchy }
		if = {
			limit = {
				scope:secret_child = {
					OR = {
						has_trait = education_diplomacy_1
						has_trait = education_martial_1
						has_trait = education_stewardship_1
						has_trait = education_intrigue_1
						has_trait = education_learning_1
					}
				}
			}
			spawn_army = {
				levies = 0
				men_at_arms = {
					type = armored_footmen
					stacks = { 6 12 }
				}
				men_at_arms = {
					type = light_footmen
					stacks = { 12 22 }
				}
				men_at_arms = {
					type = bowmen
					stacks = { 2 5 }
				}
				inheritable = no
				location = scope:secret_child.location
				name = secret_spawned_army
			}
		}
		else_if = {
			limit = {
				scope:secret_child = {
					OR = {
						has_trait = education_diplomacy_2
						has_trait = education_martial_2
						has_trait = education_stewardship_2
						has_trait = education_intrigue_2
						has_trait = education_learning_2
					}
				}
			}
			spawn_army = {
				levies = 0
				men_at_arms = {
					type = armored_footmen
					stacks = { 12 24 }
				}
				men_at_arms = {
					type = light_footmen
					stacks = { 22 48 }
				}
				men_at_arms = {
					type = bowmen
					stacks = { 5 8 }
				}
				inheritable = no
				location = scope:secret_child.location
				name = secret_spawned_army
			}
		}
		else_if = {
			limit = {
				scope:secret_child = {
					OR = {
						has_trait = education_diplomacy_3
						has_trait = education_martial_3
						has_trait = education_stewardship_3
						has_trait = education_intrigue_3
						has_trait = education_learning_3
					}
				}
			}
			spawn_army = {
				levies = 0
				men_at_arms = {
					type = armored_footmen
					stacks = { 24 36 }
				}
				men_at_arms = {
					type = light_footmen
					stacks = { 48 75 }
				}
				men_at_arms = {
					type = bowmen
					stacks = { 8 12 }
				}
				inheritable = no
				location = scope:secret_child.location
				name = secret_spawned_army
			}
		}
		else_if = {
			limit = {
				scope:secret_child = {
					OR = {
						has_trait = education_diplomacy_4
						has_trait = education_martial_4
						has_trait = education_stewardship_4
						has_trait = education_intrigue_4
						has_trait = education_learning_4
					}
				}
			}
			spawn_army = {
				levies = 0
				men_at_arms = {
					type = armored_footmen
					stacks = { 36 50 }
				}
				men_at_arms = {
					type = light_footmen
					stacks = { 75 100 }
				}
				men_at_arms = {
					type = bowmen
					stacks = { 12 18 }
				}
				inheritable = no
				location = scope:secret_child.location
				name = secret_spawned_army
			}
		}
		else_if = {
			limit = {
				scope:secret_child = {
					OR = {
						has_trait = education_diplomacy_5
						has_trait = education_martial_5
						has_trait = education_stewardship_5
						has_trait = education_intrigue_5
						has_trait = education_learning_5
					}
				}
			}
			spawn_army = {
				levies = 0
				men_at_arms = {
					type = armored_footmen
					stacks = { 50 70 }
				}
				men_at_arms = {
					type = light_footmen
					stacks = { 100 130 }
				}
				men_at_arms = {
					type = bowmen
					stacks = { 18 22 }
				}
				inheritable = no
				location = scope:secret_child.location
				name = secret_spawned_army
			}
		}
	}
}

agot_clear_all_relations = {
	if = {
		limit = {
			any_relation = {
				type = guardian
			}
		}
		every_relation = {
			type = guardian
			scope:secret_child = {
				remove_relation_guardian = prev
			}
		}
	}
	if = {
		limit = {
			any_relation = {
				type = agot_knight
			}
		}
		every_relation = {
			type = agot_knight
			scope:secret_child = {
				remove_relation_agot_knight = prev
			}
		}
	}
	if = {
		limit = {
			any_relation = {
				type = friend
			}
		}
		every_relation = {
			type = friend
			scope:secret_child = {
				remove_relation_friend = prev
			}
		}
	}
	if = {
		limit = {
			any_relation = {
				type = lover
			}
		}
		every_relation = {
			type = lover
			scope:secret_child = {
				remove_relation_lover = prev
			}
		}
	}
	if = {
		limit = {
			any_relation = {
				type = rival
			}
		}
		every_relation = {
			type = rival
			scope:secret_child = {
				remove_relation_rival = prev
			}
		}
	}
	if = {
		limit = {
			any_relation = {
				type = crush
			}
		}
		every_relation = {
			type = crush
			scope:secret_child = {
				remove_relation_crush = prev
			}
		}
	}
	if = {
		limit = {
			any_relation = {
				type = victim
			}
		}
		every_relation = {
			type = victim
			scope:secret_child = {
				remove_relation_victim = prev
			}
		}
	}
	if = {
		limit = {
			any_relation = {
				type = bully
			}
		}
		every_relation = {
			type = bully
			scope:secret_child = {
				remove_relation_bully = prev
			}
		}
	}
	if = {
		limit = { exists = betrothed }
		break_betrothal = betrothed
	}
	if = {
		limit = { is_married = yes }
		every_spouse = {
			divorce = prev
		}
	}
}

agot_secret_identity_adventure_effect = {
	save_scope_as = secret_child

	if = {
		limit = {
			NOT = { exists = liege }
			is_playable_character = no
			exists = var:secret_claim_capital.holder
		}
		var:secret_claim_capital.holder.top_liege = {
			random_vassal_or_below = {
				limit = { primary_title.tier > tier_barony }
				add_courtier = scope:secret_child
			}
		}
		trigger_event = {
			id = agot_secret_events.2004
			days = 1
		}
	}
	else_if = {
		limit = { is_playable_character = no }
		if = {
			limit = { exists = var:secret_claim_capital_hegemony.holder }
			if = {
				limit = { liege.liege = var:secret_claim_capital_hegemony.holder }
				trigger_event = {
					id = agot_secret_events.2004
					days = 1
				}
			}
			else = {
				var:secret_claim_capital_hegemony.holder = { agot_get_worst_vassal = yes }
				scope:worst_vassal = { add_courtier = scope:secret_child }
				trigger_event = {
					id = agot_secret_events.2004
					days = 1
				}
			}
		}
		else_if = {
			limit = { exists = var:secret_claim_capital_empire.holder }
			if = {
				limit = { liege.liege = var:secret_claim_capital_empire.holder }
				trigger_event = {
					id = agot_secret_events.2004
					days = 1
				}
			}
			else = {
				var:secret_claim_capital_empire.holder = { agot_get_worst_vassal = yes }
				scope:worst_vassal = { add_courtier = scope:secret_child }
				trigger_event = {
					id = agot_secret_events.2004
					days = 1
				}
			}
		}
		else_if = {
			limit = { exists = var:secret_claim_capital_kingdom.holder }
			if = {
				limit = { liege.liege = var:secret_claim_capital_kingdom.holder }
				trigger_event = {
					id = agot_secret_events.2004
					days = 1
				}
			}
			else = {
				var:secret_claim_capital_kingdom.holder = { agot_get_worst_vassal = yes }
				scope:worst_vassal = { add_courtier = scope:secret_child }
				trigger_event = {
					id = agot_secret_events.2004
					days = 1
				}
			}
		}
		else_if = {
			limit = { exists = var:secret_claim_capital_duchy.holder }
			if = {
				limit = { liege.liege = var:secret_claim_capital_duchy.holder }
				trigger_event = {
					id = agot_secret_events.2004
					days = 1
				}
			}
			else = {
				var:secret_claim_capital_duchy.holder = { agot_get_worst_vassal = yes }
				scope:worst_vassal = { add_courtier = scope:secret_child }
				trigger_event = {
					id = agot_secret_events.2004
					days = 1
				}
			}
		}
		else_if = {
			limit = { exists = var:secret_claim_capital.holder }
			if = {
				limit = { NOT = { liege = var:secret_claim_capital.holder } }
				trigger_event = {
					id = agot_secret_events.2004
					days = 1
				}
			}
			else = {
				var:secret_claim_capital.holder.top_liege = {
					random_vassal_or_below = {
						limit = { primary_title.tier > tier_barony }
						add_courtier = scope:secret_child
					}
				}
				trigger_event = {
					id = agot_secret_events.2004
					days = 1
				}
			}
		}
	}
	else = {
		# AGOT HF ADDED
		if = {
			limit = {
				has_character_flag = agot_hf_special_character
				NOT = { has_character_flag = agot_hf_special_character_blocker }
			}
			random = {
				chance = 75
				add_character_flag = {
					flag = agot_hf_special_character_blocker
					years = 5
				}
			}
		}

		if = {
			limit = {
				OR = {
					# Hegemony defender is allowed in every case
					AND = {
						exists = var:secret_claim_capital_hegemony.holder
						NOT = { this = var:secret_claim_capital_hegemony.holder }
					}
					# Empire defender is allowed within same top realm
					AND = {
						exists = var:secret_claim_capital_empire.holder
						NOT = { this = var:secret_claim_capital_empire.holder }
						NOT = { var:secret_claim_capital_empire.holder = { is_vassal_of = scope:secret_child } }
						exists = var:secret_claim_capital_empire.holder.top_liege
						var:secret_claim_capital_empire.holder.top_liege = scope:secret_child.top_liege
					}
					# Kingdom defender is allowed within same top realm
					AND = {
						exists = var:secret_claim_capital_kingdom.holder
						NOT = { this = var:secret_claim_capital_kingdom.holder }
						NOT = { var:secret_claim_capital_kingdom.holder = { is_vassal_of = scope:secret_child } }
						exists = var:secret_claim_capital_kingdom.holder.top_liege
						var:secret_claim_capital_kingdom.holder.top_liege = scope:secret_child.top_liege
					}
					# Duchy defender is allowed within same top realm
					AND = {
						exists = var:secret_claim_capital_duchy.holder
						NOT = { this = var:secret_claim_capital_duchy.holder }
						NOT = { var:secret_claim_capital_duchy.holder = { is_vassal_or_below_of = scope:secret_child } }
						exists = var:secret_claim_capital_duchy.holder.top_liege
						var:secret_claim_capital_duchy.holder.top_liege = scope:secret_child.top_liege
					}
					# Non-Hegemony Independet defender is allowed for laamps
					AND = {
						is_landless_adventurer = yes
						NOT = { exists = var:secret_claim_capital_hegemony.holder }
						OR = {
							exists = var:secret_claim_capital_empire.holder
							exists = var:secret_claim_capital_kingdom.holder
							exists = var:secret_claim_capital_duchy.holder
							exists = var:secret_claim_capital.holder
						}
						trigger_if = {
							limit = { exists = var:secret_claim_capital_empire.holder }
							NOT = { this = var:secret_claim_capital_empire.holder }
							var:secret_claim_capital_empire.holder = { agot_is_independent_ruler = yes }
						}
						trigger_else_if = {
							limit = { exists = var:secret_claim_capital_kingdom.holder }
							NOT = { this = var:secret_claim_capital_kingdom.holder }
							var:secret_claim_capital_kingdom.holder = { agot_is_independent_ruler = yes }
						}
						trigger_else_if = {
							limit = { exists = var:secret_claim_capital_duchy.holder }
							NOT = { this = var:secret_claim_capital_duchy.holder }
							var:secret_claim_capital_duchy.holder = { agot_is_independent_ruler = yes }
						}
						trigger_else = {
							NOT = { this = var:secret_claim_capital.holder }
							var:secret_claim_capital.holder = { agot_is_independent_ruler = yes }
						}
					}
					# Hegemony Sub-Realm defender is allowed for laamps
					AND = {
						is_landless_adventurer = yes
						NOT = { exists = var:secret_claim_capital_hegemony }
						OR = {
							exists = var:secret_claim_capital_empire.holder
							exists = var:secret_claim_capital_kingdom.holder
							exists = var:secret_claim_capital_duchy.holder
							exists = var:secret_claim_capital.holder
						}
						var:secret_claim_capital.holder.top_liege = { highest_held_title_tier = tier_hegemony }
					}
				}
				# AGOT HF ADDED
				NOT = { has_character_flag = agot_hf_special_character_blocker }
			}
			trigger_event = {
				id = agot_secret_events.2005
				days = 1
			}
		}
		else = {
			# AGOT HF MODIFIED
			# trigger_event = {
			# 	id = agot_secret_events.2006
			# 	days = 1
			# }
			if = {
				limit = { NOT = { has_character_flag = agot_hf_special_character } }
				trigger_event = {
					id = agot_secret_events.2006
					days = 1
				}
			}
			else = {
				agot_restart_adventure_effect = yes
			}
		}
	}
}

agot_restart_adventure_effect = {
	if = {
		limit = { is_playable_character = yes }
		add_character_flag = secret_adventure_ongoing
		set_variable = {
			name = secret_adventure_chance
			value = 50
		}
	}
	else = { agot_select_method_of_secret_adventure = yes }
}

agot_select_method_of_secret_adventure = {
	save_opinion_value_as = {
		name = holder_opinion
		target = scope:secret_child.var:secret_claim_capital.holder
	}

	random_list = {
		#Keep wandering
		1 = {
			ai_value_modifier = {
				ai_boldness = -0.5
				ai_greed = -0.5
			}

			add_character_flag = secret_adventure_ongoing
			set_variable = {
				name = secret_adventure_chance
				value = 50
			}
		}
		#Become adventurer & Reveal my identity
		1 = {
			ai_value_modifier = {
				ai_boldness = 1
			}
			modifier = {
				factor = 2
				scope:holder_opinion >= 25
			}

			agot_end_secret_identity_effect = yes
			create_landless_adventurer_title_effect = {
				REASON = flag:voluntary
				FLAVOR_CHAR = scope:secret_child
			}
		}
		#Become adventurer & Reveal my identity & Declare war
		1 = {
			trigger = { NOT = { scope:secret_child.var:secret_claim_capital.holder.house = scope:secret_child.var:secret_identity_character.house } }
			ai_value_modifier = {
				ai_boldness = 1
			}
			modifier = {
				factor = 2
				scope:holder_opinion < 25
			}

			agot_end_secret_identity_effect = yes
			create_landless_adventurer_title_effect = {
				REASON = flag:voluntary
				FLAVOR_CHAR = scope:secret_child
			}
			agot_secret_child_start_war_effect = yes
		}
		#Become adventurer & Stay rando
		1 = {
			ai_value_modifier = {
				ai_boldness = 0.5
				ai_rationality = 0.5
			}

			create_landless_adventurer_title_effect = {
				REASON = flag:voluntary
				FLAVOR_CHAR = scope:secret_child
			}
			add_character_flag = secret_adventure_ongoing
			set_variable = {
				name = secret_adventure_chance
				value = 50
			}
		}
		#Become adventurer & Stay rando forever
		1 = {
			ai_value_modifier = {
				ai_boldness = 0.5
				ai_rationality = 0.2
				ai_greed = -0.3
			}

			create_landless_adventurer_title_effect = {
				REASON = flag:voluntary
				FLAVOR_CHAR = scope:secret_child
			}
			remove_character_flag = has_secret_identity
			remove_character_flag = generic_secret_identity
		}
	}
}