#########################################
# TITLE SPECIFIC EVENTS					#
#########################################
namespace = title_event

# 0001-0010: Title name changes, by Emil Tisander
# 0011-0020: Asturias becomes Leon & Castille, by Ewan Cowhig Croft

#########################################

# West Francia becomes France when no longer controlled by a Karling
title_event.0001 = {
	type = character_event
	title = title_event_francia_name_change.title
	desc = title_event_francia_name_change.desc
	theme = crown
	right_portrait = root
	
	trigger = {
		scope:title = {
			this = title:k_france
		}
		NOR = {
			dynasty = { this = dynasty:25061 } # Not held by a Karling
			any_liege_or_above = {
				dynasty = { this = dynasty:25061 } # And does not have a liege that is a Karling
			}
			has_global_variable = west_francia_renamed
		}
	}
	
	immediate = {
		title:k_france = {
			save_scope_as = francia_title
		}
		set_global_variable = {
			name = west_francia_renamed
			value = yes
		}
	}
	
	option = {
		name = title_event_francia_name_change.a
		custom_tooltip = {
			text = title_event_francia_name_change.a_tt_1
			
			title:k_france = {
				reset_title_name = yes # I.e. set_title_name = k_france
			}
		}
	}
}

# East Francia becomes Germany when no longer controlled by a Karling
title_event.0002 = {
	type = character_event
	title = title_event_francia_name_change.title
	desc = title_event_francia_name_change.desc
	theme = crown
	right_portrait = root
	
	trigger = {
		scope:title = {
			this = title:k_east_francia
		}
		NOR = {
			dynasty = { this = dynasty:25061 }  # Not held by a Karling
			any_liege_or_above = {
				dynasty = { this = dynasty:25061 } # And does not have a liege that is a Karling
			}
			has_global_variable = east_francia_renamed
		}
	}
	
	immediate = {
		title:k_east_francia = {
			save_scope_as = francia_title
		}
		set_global_variable = {
			name = east_francia_renamed
			value = yes
		}
	}
	
	option = {
		name = title_event_francia_name_change.a
		custom_tooltip = {
			text = title_event_francia_name_change.a_tt_2
			
			title:k_east_francia = {
				set_title_name = k_germany
			}
		}
	}
}

##################################################
# Splitting the Crown
# by Ewan Cowhig Croft
# 0011 - 0020
##################################################

#	Hidden management event to determine if we need to axe Asturias &, if we do, what notification events to send out afterwards.
title_event.0011 = {
	hidden = yes

	trigger = {
		scope:title = title:k_castille
		title:k_asturias = {
			# Leon must be de jure part of Asturias, or else there's no reason to ever stop calling it Asturias.
			any_in_de_jure_hierarchy = { this = title:d_leon }
		}
	}

	immediate = {
		# Set up some scopes for easy reference.
		title:k_castille = {
			save_scope_as = castille
			holder = { save_scope_as = castille_char }
		}
		title:k_asturias = {
			save_scope_as = asturias
			if = {
				limit = { exists = holder }
				holder = { save_scope_as = leon_char }
			}
		}
		title:k_leon = { save_scope_as = leon }
		## Plus one for backgrounds; we take this one to get the hilly backdrops of northern Iberia.
		province:1815 = { save_scope_as = background_terrain_scope }
		# Then fire off relevant notifications to scope:leon_char & scope:castille_char.
		## If Asturias doesn't exist when Leon is created, we just send Castille one.
		if = {
			limit = {
				NOT = { exists = title:k_asturias.holder }
			}
			scope:castille_char = { trigger_event = title_event.0012 }
		}
		## & if Leon & Castille are now held by the same char, they get another.
		else_if = {
			limit = { scope:leon_char = scope:castille_char }
			scope:castille_char = { trigger_event = title_event.0013 }
		}
		## Otherwise, we send out the standard two-person configuration.
		else = {
			scope:leon_char = { trigger_event = title_event.0014 }
			scope:castille_char = { trigger_event = title_event.0015 }
		}
		# Finally, execute the effects.
		asturias_becomes_leon_and_castille_effect = yes
	}
}

#	There is no Asturian King, but Castille now exists.
title_event.0012 = {
	type = character_event
	title = title_event.0012.t
	desc = title_event.0012.desc
	theme = realm
	left_portrait = {
		character = scope:castille_char
		animation = personality_honorable
	}
	override_background = { reference = terrain_scope }

	immediate = {
		show_as_tooltip = { asturias_becomes_leon_and_castille_effect = yes }
	}

	# As it should be.
	option = {
		name = title_event.0012.a

		# No stress needed for a single-option notification event.
		ai_chance = {
			# No fine tweaking needed for the AI chance on a notification event.
			base = 100
		}
	}
}

#	The Asturian King is also King of Castille.
title_event.0013 = {
	type = character_event
	title = title_event.0013.t
	desc = title_event.0013.desc
	theme = realm
	left_portrait = {
		character = scope:castille_char
		animation = personality_honorable
	}
	override_background = { reference = terrain_scope }

	immediate = {
		show_as_tooltip = { asturias_becomes_leon_and_castille_effect = yes }
	}

	# Two crowns are better than one!
	option = {
		name = title_event.0013.a

		# No stress needed for a single-option notification event.
		ai_chance = {
			# No fine tweaking needed for the AI chance on a notification event.
			base = 100
		}
	}
}

#	Leon POV: Asturias becomes Leon.
title_event.0014 = {
	type = character_event
	title = title_event.0014.t
	desc = title_event.0014.desc
	theme = realm
	left_portrait = {
		character = scope:leon_char
		animation = anger
	}
	right_portrait = {
		character = scope:castille_char
		animation = personality_greedy
	}
	override_background = { reference = terrain_scope }

	immediate = {
		show_as_tooltip = { asturias_becomes_leon_and_castille_effect = yes }
	}

	# Splitters!
	option = {
		name = title_event.0014.a

		# No stress needed for the default option in a notification event.
		ai_chance = {
			# No fine tweaking needed for the AI chance on a notification event.
			base = 100
		}
	}
	
	# We'll get those lands back some day...
	option = {
		name = title_event.0014.b
		# We only want to allow the mechanical effect options as RP-outlets for players.
		trigger = { is_ai = no }

		progress_towards_rival_effect = {
			REASON = rival_lands_stolen
			CHARACTER = scope:castille_char
			OPINION = default_rival_opinion
		}

		stress_impact = { forgiving  = minor_stress_impact_gain }
		ai_chance = {
			# No fine tweaking needed for the AI chance on a notification event.
			base = 100
		}
	}
	
	# Huzzah! A whole _kingdom_ of comrades!
	option = {
		name = title_event.0014.c
		# We only want to allow the mechanical effect options as RP-outlets for players.
		trigger = { is_ai = no }

		progress_towards_friend_effect = {
			REASON = friend_castille_leon
			CHARACTER = scope:castille_char
			OPINION = default_friend_opinion
		}

		stress_impact = { vengeful = minor_stress_impact_gain }
		ai_chance = {
			# No fine tweaking needed for the AI chance on a notification event.
			base = 100
		}
	}
}

#	Castille POV: Asturias becomes Leon.
title_event.0015 = {
	type = character_event
	title = title_event.0015.t
	desc = title_event.0015.desc
	theme = realm
	left_portrait = {
		character = scope:castille_char
		animation = happiness
	}
	right_portrait = {
		character = scope:leon_char
		animation = anger
	}
	override_background = { reference = terrain_scope }

	immediate = {
		show_as_tooltip = { asturias_becomes_leon_and_castille_effect = yes }
	}

	# Power is back where it should always have been.
	option = {
		name = title_event.0015.a

		# No stress needed for the default option in a notification event.
		ai_chance = {
			# No fine tweaking needed for the AI chance on a notification event.
			base = 100
		}
	}
	
	# Today, Castille. Tomorrow, _Leon_.
	option = {
		name = title_event.0015.b
		# We only want to allow the mechanical effect options as RP-outlets for players.
		trigger = { is_ai = no }
		scope:leon_char = {
			progress_towards_rival_effect = {
				REASON = rival_leon_formed
				CHARACTER = scope:castille_char
				OPINION = default_rival_opinion
			}
		}
		stress_impact = { forgiving  = minor_stress_impact_gain }
		ai_chance = {
			# No fine tweaking needed for the AI chance on a notification event.
			base = 100
		}
	}
	
	# We may be divided in rule, but we're united by blood!
	option = {
		name = title_event.0015.c
		# We only want to allow the mechanical effect options as RP-outlets for players.
		trigger = { is_ai = no }
		scope:leon_char = {
			progress_towards_friend_effect = {
				REASON = friend_castille_leon
				CHARACTER = scope:castille_char
				OPINION = default_friend_opinion
			}
		}
		stress_impact = { vengeful = minor_stress_impact_gain }
		ai_chance = {
			# No fine tweaking needed for the AI chance on a notification event.
			base = 100
		}
	}
}

# Ascended Throne memory maintenance
title_event.9900 = {
	type = character_event
	hidden = yes
	cooldown = { days = 1 }

	trigger = {
		is_landless_adventurer = no
		any_in_list = { variable = new_titles }
	}

	immediate = {
		if = {
			limit = {
				is_target_in_variable_list = {
					name = new_titles
					target = root.primary_title
				}
			}
			primary_title = { save_scope_as = new_primary_title }
		}
		else = {
			ordered_in_list = {
				variable = new_titles
				order_by = tier
				save_scope_as = new_primary_title
			}
		}
		if = {
			limit = { exists = scope:new_primary_title.previous_holder }
			scope:new_primary_title.previous_holder = { save_scope_as = flavor }
		}
		else = { save_scope_as = flavor }
		create_character_memory = {
			type = ascended_throne_memory

			participants = {
				flavor_character = scope:flavor
			}
		}
		set_variable = {
			name = title_count
			value = -1
		}
		every_in_list = {
			variable = new_titles
			save_temporary_scope_as = title_temp
			scope:new_memory = {
				add_to_variable_list = {
					name = new_titles
					target = scope:title_temp
				}
			}
			root = {
				change_variable = {
					name = title_count
					add = 1
				}
			}
		}
		scope:new_memory = {
			set_variable = {
				name = title_count
				value = root.var:title_count
			}
			set_variable = {
				name = landed_title
				value = scope:new_primary_title
			}
			set_variable = {
				name = reason
				value = scope:real_transfer_type
			}
			#Only children should get childhood memories
			if = {
				limit = {
					root = { is_adult = no }
				}
				set_variable = childhood_memory
			}
			# Bonus scopes
			if = {
				limit = { exists = root.var:ascended_throne_extra }
				set_variable = {
					name = ascended_throne_extra
					value = root.var:ascended_throne_extra
				}
				root = { remove_variable = ascended_throne_extra }
			}
		}
		clear_variable_list = new_titles
		remove_variable = title_count
	}
}

#Lost title memory maintenance
title_event.9901 = {
	type = character_event
	hidden = yes
	cooldown = { days = 1 }

	immediate = {
		ordered_in_list = {
			variable = lost_titles
			order_by = tier
			save_scope_as = highest_old_title
		}
		create_character_memory = {
			type = lost_title_memory
			
			participants = {
				new_holder = scope:new_holder
			}
		}
		set_variable = {
			name = title_count
			value = -1
		}
		every_in_list = {
			variable = lost_titles
			save_temporary_scope_as = title_temp
			scope:new_memory = {
				add_to_variable_list = {
					name = lost_titles
					target = scope:title_temp
				}
			}
			root = {
				change_variable = {
					name = title_count
					add = 1
				}
			}
		}
		scope:new_memory = {
			set_variable = {
				name = title_count
				value = root.var:title_count
			}
			set_variable = {
				name = landed_title
				value = scope:highest_old_title
			}
			set_variable = {
				name = reason
				value = scope:real_transfer_type
			}
			#Only children should get childhood memories
			if = {
				limit = {
					root = { is_adult = no }
				}
				set_variable = childhood_memory
			}
			# Bonus scopes
			if = {
				limit = { exists = root.var:ascended_throne_extra }
				set_variable = {
					name = ascended_throne_extra
					value = root.var:ascended_throne_extra
				}
				root = { remove_variable = ascended_throne_extra }
			}
		}
		clear_variable_list = new_titles
		remove_variable = title_count
	}
}

# Make sure newbie adventurers aren't locked out of their camp.
title_event.9911 = {
	type = character_event
	hidden = yes

	trigger = {
		government_has_flag = government_is_landless_adventurer
		NOR = {
			is_commanding_army = yes
			exists = involved_activity
			is_travelling = yes
			is_imprisoned = yes
		}
	}

	immediate = { set_location_to_default = yes }
}
