﻿#
# on_title_destroyed
#

# root: Character - The holder before destruction
# scope:landed_title: Title - The title that is being destroyed
on_title_destroyed = {
	on_actions = { CTFA_on_title_destroyed }
}

CTFA_on_title_destroyed = {
	effect = {
		CTFA_handle_character_title_change = {
			CHANGED_TITLE = scope:landed_title
		}

		if = {
			limit = {
				scope:landed_title = { CTFA_is_vanity_title = yes }
			}

			remove_character_modifier = CTFA_held_vanity_title_modifier

			# The following is a workaround for AI King-level vassals destroying their VTs.
			# If we detect that an AI VT was destroyed not through CTFA_try_destroy_title
			# (i.e. neither by the player from CTFA UI nor by the mod from one of the mod's on_actions),
			# we assume that the AI has destroyed the title themselves and immediately trigger an event
			# to give it back to them, whether they like it or not.

			if = {
				limit = {
					is_ai = yes
					NOT = { has_character_flag = CTFA_vt_destruction_allowed }
				}

				# Find an available vanity title and bind it to this character's primary title.
				# (In this case it will be the very VT this AI has just foolishly tried to destroy. He-he-heee...)
				trigger_event = CTFA_events.1001
			}

			remove_character_flag = CTFA_vt_destruction_allowed
		}
	}
}

#
# on_title_gain
#

# root: Character - The new holder
# scope:title: Title - The title that changes hands
# scope:previous_holder: Character - Previous holder; might be dead
on_title_gain = {
	on_actions = { CTFA_on_title_gain }
}

CTFA_on_title_gain = {
	effect = {
		CTFA_handle_character_title_change = {
			CHANGED_TITLE = scope:title
		}
	}
}

#
# on_title_gain_inheritance
#

# root: Character - The new holder
# scope:title: Title - The title that changes hands
# scope:previous_holder: Character - Previous holder; should be dead
on_title_gain_inheritance = {
	on_actions = { CTFA_on_title_gain_inheritance }
}

CTFA_on_title_gain_inheritance = {
	effect = {
		CTFA_handle_character_title_inheritance = {
			INHERITED_TITLE = scope:title
		}
	}
}

#
# on_title_lost
#

# root: Character - The old holder
# scope:title: Title - The title that changes hands
# scope:new_holder: Character - New holder
on_title_lost = { # NOTE: This does NOT actually get called when title is destroyed.
	on_actions = { CTFA_on_title_lost }
}

CTFA_on_title_lost = {
	effect = {
		CTFA_handle_character_title_change = {
			CHANGED_TITLE = scope:title
		}
	}
}

#
# on_rank_up
#

# root: Character - Character ranking up
# scope:title: Title - Old primary title
on_rank_up = {
	on_actions = { CTFA_on_rank_up }
}

CTFA_on_rank_up = {
	effect = {
		if = {
			limit = {
				# Not interested in previously unlanded characters
				exists = scope:title
			}

			primary_title = {
				save_scope_as = CTFA_new_primary_title
			}

			CTFA_on_character_primary_title_changed = {
				NEW_PRIMARY_TITLE = scope:CTFA_new_primary_title
			}
		}
	}
}


#
# on_rank_down
#

# root: Character - Character ranking down
# scope:title: Title - Old primary title
on_rank_down = {
	on_actions = { CTFA_on_rank_down }
}

CTFA_on_rank_down = {
	effect = {
		if = {
			limit = {
				# Not interested in newly unlanded characters
				exists = primary_title
			}

			primary_title = {
				save_scope_as = CTFA_new_primary_title
			}

			CTFA_on_character_primary_title_changed = {
				NEW_PRIMARY_TITLE = scope:CTFA_new_primary_title
			}
		}
	}
}
