﻿#On actions about titles

# All on-actions in this file provide scope:transfer_type unless otherwise noted
# scope:transfer_type has the following permutations:
# flag:conquest
# flag:conquest_holy_war
# flag:conquest_claim
# flag:conquest_populist
# flag:inheritance
# flag:abdication
# flag:destroyed
# flag:created
# flag:usurped
# flag:granted
# flag:revoked
# flag:election
# flag:independency
# flag:returned
# flag:leased_out
# flag:lease_revoked
# flag:faction_demand
# flag:swear_fealty

# 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 = {
	on_actions = {
		historicinvasions_destroy_on_title_gain_same_tier
		vlaanderen_latin_nicene_rivalry_on_action
		historicinvasions_on_title_gain_assassins
	}
}

# set_destroy_on_gain_same_tier can - for whatever reason - only be set for dynamic titles
# set_delete_on_gain_same_tier does not exist
# so this effect has to be set within the landed title using 'delete_on_gain_same_tier = yes'; this does in fact not delete the title, only destroy it - there is no 'destroy_on_gain_same_tier = yes'
# so I've made my own that can be added to titles via script after certain triggers have been met instead of it being enabled from the start:
# only on_inheritance to have the script check less often
historicinvasions_destroy_on_title_gain_same_tier = {
	trigger = {
		# only check if they receive a kingdom (since I have only set the variable for kingdoms so far)
		scope:title.tier = tier_kingdom
		# receiver already holds one other kingdom-tier title
		any_held_title = {
			tier = tier_kingdom
			count > 1
		}
		# only destroy the new title if it has a special variable set
		# variable may already be set before the title is created
		# once set, a variable on a title will remain active / intact even if the title is destroyed
		scope:title = { has_variable = set_destroy_on_gain_same_tier_var }
	}
	effect = {
		debug_log = "destroyed invader kingdom on succession"
		destroy_title = scope:title
	}
}

vlaanderen_latin_nicene_rivalry_on_action = {
	trigger = {
		OR = {
			scope:title = title:e_vlaanderen
			scope:title = title:e_nikaea
		}
		exists = title:e_vlaanderen.holder
		exists = title:e_nikaea.holder
		NOT = { title:e_vlaanderen.holder = title:e_nikaea.holder }
	}
	effect = {
		### Latin Empire
		if = {
			limit = { scope:title = title:e_vlaanderen }
			set_relation_rival = {
				target = title:e_nikaea.holder
				reason = roman_pretender_rivalry_reason
			}
			if = {
				limit = { is_allied_to = title:e_nikaea.holder }
				break_alliance = title:e_nikaea.holder
			}
		}
		### Nicene Empire
		else = {
			set_relation_rival = {
				target = title:e_vlaanderen.holder
				reason = roman_pretender_rivalry_reason
			}
			if = {
				limit = { is_allied_to = title:e_vlaanderen.holder }
				break_alliance = title:e_vlaanderen.holder
			}
		}
	}
}

historicinvasions_on_title_gain_assassins = {
	trigger = {
		OR = {
			scope:title = title:k_sabbah
			scope:title = title:k_jabalid
		}
	}
	effect = {
		if = {
			limit = { NOT = { faith = faith:nizari } }
			debug_log = "destroyed invader assassin title because its heir was of a different faith"
			destroy_title = scope:title
		}
		else = {
			add_character_modifier = nizari_assassin_modifier
			scope:previous_holder ?= { remove_character_modifier = nizari_assassin_modifier }
		}
	}
}