﻿#########################################
# 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 = { exists = root.var:ascended_throne_reason }
			root.var:ascended_throne_reason = { save_scope_as = real_transfer_type }
		}
		else = {
			scope:transfer_type = { save_scope_as = real_transfer_type }
		}
		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 }
			}
		}
		#Clean up LAAMPS who get the wrong governments in claimant wars
		if = {
			limit = {
				OR = {
					AND = {
						has_variable = city_construction_is_temple_citadel
						any_in_list = {
							variable = new_titles
							this = root.var:city_construction_is_temple_citadel
						}
					}
					AND = {
						has_variable = city_construction_is_castle
						any_in_list = {
							variable = new_titles
							this = root.var:city_construction_is_castle
						}
					}
				}
			}
			#Can the LAAMP become Mandala?
			if = {
				limit = { has_variable = city_construction_is_temple_citadel }
				if = {
					limit = {
						#... we're lenient
						faith = { has_doctrine = doctrine_polytheist }
					}
					#Sort out the House shenanigans
					if = {
						limit = { is_house_head = no }
						house = { 
							if = {
								limit = {
									house_head = { 
										is_landed = no
										is_courtier_of = root
									}
								}
								set_house_head = root
							}
						}
					}
					else = {
						house ?= {
							set_house_aspiration = { type = no_aspect }
						}
					}	
				}
				#Otherwise go vanilla
				else = {
					if = {
						limit = { ep3_is_clan_inclined_trigger = yes }
						change_government = clan_government
					}
					else = { change_government = feudal_government }
				}
			}
			#Otherwise we have city_construction_is_castle
			else = {
				#... Feudals shouldn't have temple capitals
				if = {
					limit = {
						has_variable = holding_barony
						capital_barony = { this != root.var:holding_barony }
						capital_province = { has_holding_type = church_holding }
					}
					capital_province = { set_holding_type = castle_holding }				
				}
				#Otherwise go vanilla
				else = {
					if = {
						limit = { ep3_is_clan_inclined_trigger = yes }
						change_government = clan_government
					}
					else = { change_government = feudal_government }
				}
			}
			remove_variable = city_construction_is_temple_citadel
			remove_variable = city_construction_is_castle
			remove_variable = holding_barony
		}
		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 }
}
