﻿#On actions about titles

# A title is destroyed
# root = the holder before destruction
# scope:landed_title = the title that is being destroyed
on_title_destroyed = {
	events = {
	}
}

# A title is transferred to a new character
# root = the new holder
# scope:title = the title that changes hands
# scope:previous_holder = previous holder. Might be dead
on_title_gain = {
	effect = {
		###Imperial mod###
		if = { #Patrician gains duke tier title, destroy patrician title 
			limit = {
				has_government = patrician_government
				scope:title = {
					tier >= tier_duchy
					NOT = { 
						is_landless_patrician_title_trigger = yes
					}
				}
			}
			every_held_title = { #destroy all landless patrician titles
				limit = { 
					is_landless_patrician_title_trigger = yes
				}
				holder = { destroy_title = prev }
			}
		}
		if = { #Convert to military command if possible
			limit = { can_get_military_command_trigger = yes }
			change_government = military_command
			# every_held_title = {
				# limit = { 
					# NAND = { #not county capital barony
						# tier = tier_barony
						# holder = { highest_held_title_tier > 1 }
						# title_province = { is_county_capital = yes }
					# }
				# }
				# add_title_law = military_appointment_succession_law
			# }
		}
		
		#if go over domain limit try and give title to the military candidate first
		military_faction_title_grant_check_effect = { GRANTEE = root }
		###
		
		
		if = {
			limit = {
				scope:title = { is_holy_order = yes }
				NOT = { has_trait = order_member }
				has_government = holy_order_government
			}
			add_trait = order_member
		}

		#Did I receive a title from my sibling?
		if = {
			limit = {
				exists = scope:previous_holder
				scope:previous_holder = {
					is_alive = yes
					exists = var:sibling_waiting_for_land
					any_sibling = {
						this = root
						this = scope:previous_holder.var:sibling_waiting_for_land
					}
				}
			}
			add_character_flag = {
				flag = got_title_from_sibling
				years = 4
			}
		}
	}
	events = {
		roman_restoration.0005 #New Roman Emperor gets Augustus trait. (old holder, if still alive, loses it)
		roman_restoration.0199 #Restoring the Pentarchy.
		religious_decision.0312 #New holder of a county with a runestone
		coa.1 # check if title Coat of Arms should change.
	}
}

# A title is inherited by a character
# root = the new holder
# scope:title = the title that changes hands
# scope:previous_holder = previous holder. Should be dead
on_title_gain_inheritance = {
	events = {
	}
	effect = {
		#if go over domain limit try and give title to the military candidate first
		military_faction_title_grant_check_effect = { GRANTEE = root }
		###
		if = {
			limit = {
				scope:title = title:e_mongol_empire
				has_character_flag = gain_mongol_succession_dread
			}
			add_dread = 50
		}
	}
}

# A title is usurped by a character
# root = the new holder
# scope:title = the title that changes hands
# scope:previous_holder = previous holder. Shouldn't be dead
on_title_gain_usurpation = {
	effect = {
		#if go over domain limit try and give title to the military candidate first
		military_faction_title_grant_check_effect = { GRANTEE = root }
		###
	}
	events = {
	}
}

# A title is lost by a character
# root = the old holder
# scope:title = the title that changes hands
# scope:new_holder = new holder.
on_title_lost = {
	events = {
	}
}

# A claim is gained by a character
# root = the claimant
# scope:title = the title that is claimed
on_explicit_claim_gain = {
	events = {
		coa.1 # check if title Coat of Arms should change.
	}
}

# A claim is lost by a character
# root = the claimant
# scope:title = the title that was claimed
on_explicit_claim_lost = {
	events = {
		coa.1 # check if title Coat of Arms should change.
	}
}

# A title change makes the character rank up in the landed tiers (eg Duke -> King)
# root = character ranking up
# scope:title = old primary title
on_rank_up = { # Will not fire during history execution or for dying characters
	events = {
	}
	effect = {
		add_achievement_flag = { FLAG = achievement_moving_up_in_the_world_flag }
		update_embassies_effect = yes
		every_ally = {
			update_embassies_effect = yes
		}
	}
}

# A title change makes the character rank down in the landed tiers (eg King -> Duke)
# root = character ranking down
# scope:title = old primary title
on_rank_down = { # Will not fire during history execution or for dying characters
	events = {
	}
	effect = {
		add_character_flag = {
			flag = make_suicide_available
			years = 5
		}
		update_embassies_effect = yes
		every_ally = {
			update_embassies_effect = yes
		}
	}
}
