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

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

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


# 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 }
}
