﻿
# Events fired from the Sell Titles interaction

namespace = sell_titles


################################
# A Vassal wants a Hook, rather than titles
# by Petter Vilberg
################################
scripted_trigger sell_titles_0001_vassal_trigger = {
	is_available_ai_adult = yes
	NOT = {
		has_hook = root
	}
}

sell_titles.0001 = {
	type = letter_event
	opening = sell_titles.0001.opening
	desc = {
		first_valid = {
			triggered_desc = { # Special desc if the vassal dislikes you
				trigger = {
					scope:vassal = {
						is_powerful_vassal = yes
						OR = {
							has_relation_rival = root
							opinion = {
								target = root
								value <= -10
							}
						}
					}
				}
				desc = sell_titles.0001.desc.dislike
			}
			desc = sell_titles.0001.desc.normal
		}
	}
	sender = {
		character = scope:vassal
		animation = schadenfreude
	}
	
	trigger = {
		NOT = { has_character_flag = had_event_sell_titles_0001 }
		any_vassal = {
			sell_titles_0001_vassal_trigger = yes
		}
	}

	weight_multiplier = {
		base = 0.5
		modifier = { # At 1 if there's a Major Interest vassal out there
			add = 0.5
			any_vassal = {
				sell_titles_0001_vassal_trigger = yes
				is_of_major_interest_trigger = {
					CHARACTER = root
				}
			}
		}
	}

	immediate = {
		add_character_flag = {
			flag = had_event_sell_titles_0001
			years = 10
		}
		random_vassal = {
			limit = {
				sell_titles_0001_vassal_trigger = yes
				is_of_major_interest_trigger = {
					CHARACTER = root
				}
			}
			alternative_limit = {
				sell_titles_0001_vassal_trigger = yes
				is_of_minor_interest_trigger = {
					CHARACTER = root
				}
			}
			alternative_limit = {
				sell_titles_0001_vassal_trigger = yes
			}
			save_scope_as = vassal
			hidden_effect = { # Make sure they have enough to pay
				if = {
					limit = {
						is_powerful_vassal = yes
						short_term_gold < root.major_gold_value
					}
					add_gold = root.major_gold_value
				}
				else_if = {
					limit = {
						short_term_gold < root.medium_gold_value
					}
					add_gold = root.medium_gold_value
				}
			}
		}
	}

	option = { # Sounds great!
		name = sell_titles.0001.a
		scope:vassal = {
			if = {
				limit = {
					is_powerful_vassal = yes
				}
				pay_short_term_gold = {
					target = root
					gold = root.major_gold_value
				}
			}
			else = {
				pay_short_term_gold = {
					target = root
					gold = root.major_gold_value
				}
			}
			add_hook = {
				type = indebted_hook
				target = root
			}
		}
		stress_impact = {
			paranoid = major_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_greed = 1
			}
		}
	}

	option = { #Never!
		name = sell_titles.0001.b
		add_prestige = minor_prestige_gain
		stress_impact = {
			greedy = medium_stress_impact_gain
		}
		ai_chance = {
			base = 10
		}
	}
}


################################
# A friend gives Gold to help out
# by Petter Vilberg
################################
scripted_trigger sell_titles_0002_friend_trigger = {
	is_available_ai_adult = yes
	has_relation_friend = root
	NOT = { has_character_flag = sell_titles_0002_gave_friend_gold }
}

sell_titles.0002 = {
	type = letter_event
	opening = sell_titles.0002.opening
	desc = sell_titles.0002.desc
	sender = {
		character = scope:friend
		animation = happiness
	}
	
	trigger = {
		NOT = { has_character_flag = had_event_sell_titles_0002 }
		any_relation = {
			type = friend
			sell_titles_0002_friend_trigger = yes
		}
	}

	weight_multiplier = {
		base = 1
	}

	immediate = {
		add_character_flag = {
			flag = had_event_sell_titles_0002
			years = 5
		}
		random_relation = {
			type = friend
			limit = {
				sell_titles_0002_friend_trigger = yes
			}
			save_scope_as = friend
			add_character_flag = {
				flag = sell_titles_0002_gave_friend_gold
				years = 10
			}
			sell_titles_grant_sufficient_gold_effect = {
				AMOUNT = root.medium_gold_value
			}
		}
	}

	option = { # Why thank you, my friend
		name = sell_titles.0002.a
		scope:friend = {
			pay_short_term_gold = {
				target = root
				gold = root.medium_gold_value
			}
			add_opinion = {
				target = root
				modifier = friendliness_opinion
				opinion = 10
			}
		}
		hidden_effect = {
			add_opinion = {
				target = scope:friend
				modifier = grateful_opinion
				opinion = 30
			}
		}
		stress_impact = {
			arrogant = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
		}
	}

	option = { # I cannot accept this
		name = sell_titles.0002.b
		add_prestige = minor_prestige_value
		scope:friend = {
			add_opinion = {
				target = root
				modifier = respect_opinion
				opinion = 20
			}
		}
		stress_impact = {
			greedy = medium_stress_impact_gain
		}
		ai_chance = {
			base = 10
		}
	}
}


################################
# Vassal is tricked into giving both Gold and Hook
# by Petter Vilberg
################################
scripted_trigger sell_titles_0003_vassal_trigger = {
	is_available_ai_adult = yes
	NOT = { has_character_flag = sell_titles_0003_bought_titles }
	NOR = {
		has_trait = shrewd
		has_trait = intellect_good
		has_trait = arrogant
	}
}

sell_titles.0003 = {
	type = letter_event
	opening = sell_titles.0001.opening
	desc = sell_titles.0003.desc
	sender = scope:vassal
	
	trigger = {
		NOT = { has_character_flag = had_event_sell_titles_0003 }
		any_vassal = {
			sell_titles_0003_vassal_trigger = yes
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			add = 1
			any_vassal = {
				sell_titles_0003_vassal_trigger = yes
				has_trait = intellect_bad
			}
		}
		modifier = {
			add = 2
			has_relation_oaf = root
		}
	}

	immediate = {
		add_character_flag = {
			flag = had_event_sell_titles_0003
			years = 5
		}
		random_vassal = {
			limit = {
				sell_titles_0003_vassal_trigger = yes
			}
			weight = {
				base = 1
				modifier = {
					add = 1
					has_trait = intellect_bad_1
				}
				modifier = {
					add = 3
					has_trait = intellect_bad_2
				}
				modifier = {
					add = 5
					has_trait = intellect_bad_3
				}
				modifier = {
					add = 2
					has_trait = dull
				}
				compare_modifier = {
					value = ai_honor
					multiplier = 0.02
				}
				compare_modifier = {
					value = ai_rationality
					multiplier = -0.02
				}
			}
			save_scope_as = vassal
			add_character_flag = {
				flag = sell_titles_0003_bought_titles
				years = 15
			}
			sell_titles_grant_sufficient_gold_effect = {
				AMOUNT = root.medium_gold_value
			}
		}
		hidden_effect = {
			if = {
				limit = {
					any_relation = {
						type = oaf
						count < 1
					}
				}
				set_relation_oaf = scope:vassal
			}
		}
	}

	option = {
		name = sell_titles.0003.a
		flavor = sell_titles.0003.a.flavor
		scope:vassal = {
			pay_short_term_gold = {
				target = root
				gold = root.medium_gold_value
			}
			add_prestige = minor_prestige_value
		}
		if = {
			limit = {
				NOT = { has_hook = scope:vassal }
			}
			add_hook = {
				target = scope:vassal
				type = indebted_hook
			}
		}
	}
}


################################
# Powerful Vassal wants greater honors
# by Petter Vilberg
################################
scripted_trigger sell_titles_0004_vassal_trigger = {
	is_available_ai_adult = yes
	prestige_level < 5
	ai_boldness > 0
	NOR = {
		has_relation_friend = root
		has_relation_lover = root
		has_trait = humble
		has_trait = content
	}
	NOT = { has_character_modifier = sell_titles_respected_by_peers_modifier }
}

sell_titles.0004 = {
	type = letter_event
	opening = sell_titles.0004.opening
	desc = sell_titles.0004.desc
	sender = {
		character = scope:vassal
		animation = personality_bold
	}
	
	trigger = {
		NOT = { has_character_flag = had_event_sell_titles_0004 }
		any_powerful_vassal = {
			sell_titles_0004_vassal_trigger = yes
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			add = 0.5
			any_powerful_vassal = {
				sell_titles_0004_vassal_trigger = yes
				has_trait = ambitious
			}
		}
		modifier = {
			add = 0.25
			any_powerful_vassal = {
				sell_titles_0004_vassal_trigger = yes
				has_trait = arrogant
			}
		}
	}

	immediate = {
		add_character_flag = {
			flag = had_event_sell_titles_0004
			years = 10
		}
		random_powerful_vassal = {
			limit = {
				sell_titles_0004_vassal_trigger = yes
			}
			weight = {
				base = 1
				modifier = {
					add = 5
					has_trait = arrogant
				}
				modifier = {
					add = 10
					has_trait = ambitious
				}
			}
			save_scope_as = vassal

		}
		hidden_effect = {
			random = {
				chance = 5
				compare_modifier = {
					target = scope:vassal
					value = ai_honor
					multiplier = 0.1
				}
				save_scope_value_as = {
					name = nickname
					value = yes
				}
			}
		}
	}

	option = { # I suppose it would be right
		name = sell_titles.0004.a
		add_prestige = minor_prestige_loss
		scope:vassal = {
			add_opinion = {
				target = root
				modifier = grateful_opinion
				opinion = 20
			}
			if = {
				limit = {
					exists = scope:nickname
				}
				set_nickname_effect = {
					NICKNAME = nick_the_honorable
				}
			}
			add_prestige_level = 1
			add_character_modifier = {
				modifier = sell_titles_respected_by_peers_modifier
				years = 10
			}
		}
	}

	option = { # Do not aggrandize yourself!
		name = sell_titles.0004.b
		scope:vassal = {
			add_opinion = {
				target = root
				modifier = insult_opinion
				opinion = -30
			}
		}
	}
}


################################
# Powerful Vassal reduces their contract
# by Petter Vilberg
################################
scripted_trigger sell_titles_0005_vassal_trigger = {
	is_available_ai_adult = yes
	vassal_contract_obligation_level_can_be_decreased = feudal_government_taxes
}

sell_titles.0005 = {
	type = letter_event
	opening = sell_titles.0004.opening
	desc = sell_titles.0005.desc
	sender = scope:vassal
	
	trigger = {
		NOT = { has_character_flag = had_event_sell_titles_0005 }
		any_powerful_vassal = {
			sell_titles_0005_vassal_trigger = yes
		}
	}

	weight_multiplier = {
		base = 1
		modifier = { # Increase likelihood if you have a lot of maxed-out vassal contracts
			add = {
				value = 0
				every_vassal = {
					limit = {
						is_powerful_vassal = yes
						NOT = { vassal_contract_obligation_level_can_be_increased = feudal_government_taxes }
					}
					add = 0.25
				}
			}
			always = yes
		}
	}

	immediate = {
		add_character_flag = {
			flag = had_event_sell_titles_0005
			years = 10
		}
		random_powerful_vassal = {
			limit = {
				sell_titles_0005_vassal_trigger = yes
			}
			weight = {
				base = 100
				opinion_modifier = {
					opinion_target = root
					min = -90
				}
				compare_modifier = {
					value = ai_rationality
				}
			}
			save_scope_as = vassal
			sell_titles_grant_sufficient_gold_effect = {
				AMOUNT = root.medium_gold_value
			}
		}
	}

	option = { # Oh.
		name = sell_titles.0005.a
		scope:vassal = {
			vassal_contract_decrease_obligation_level = feudal_government_taxes
			pay_short_term_gold = {
				target = root
				gold = root.medium_gold_value
			}
		}
		hidden_effect = {
			if = {
				limit = {
					can_set_relation_potential_rival_trigger = {
						CHARACTER = scope:vassal
					}
				}
				set_relation_potential_rival = scope:vassal
			}
		}
	}

	option = { # Damn you!
		name = sell_titles.0005.b
		trigger = {
			OR = {
				has_relation_potential_rival = scope:vassal
				has_relation_rival = scope:vassal
			}
		}
		exclusive = yes
		scope:vassal = {
			vassal_contract_increase_obligation_level = feudal_government_taxes
			pay_short_term_gold = {
				target = root
				gold = root.medium_gold_value
			}
		}
		progress_towards_rival_effect = {
			REASON = rival_sold_titles
			CHARACTER = scope:vassal
			OPINION = 0
		}
	}
}


################################
# Powerful Vassal or Councillor chastises you
# by Petter Vilberg
################################
scripted_trigger sell_titles_0006_vassal_trigger = {
	is_available_ai_adult = yes
	ai_honor >= 20 # Needs to have *some* honor
	num_sinful_traits < 2
	OR = {
		is_vassal_of = root
		is_normal_councillor = yes
	}
	faith = { # Can't fire for faiths that love greed
		NOT = { trait_is_virtue = greedy }
	}
}

sell_titles.0006 = {
	type = letter_event
	opening = sell_titles.0004.opening
	desc = sell_titles.0006.desc
	sender = {
		character = scope:vassal
		animation = disapproval
	}
	
	trigger = {
		NOT = { has_character_flag = had_event_sell_titles_0006 }
		any_powerful_vassal = {
			sell_titles_0006_vassal_trigger = yes
		}
		any_councillor = {
			sell_titles_0006_vassal_trigger = yes
		}
	}

	weight_multiplier = {
		base = 1
	}

	immediate = {
		add_character_flag = {
			flag = had_event_sell_titles_0006
			years = 10
		}
		every_powerful_vassal = {
			limit = {
				sell_titles_0006_vassal_trigger = yes
			}
			add_to_list = potential_chastisers
		}
		every_councillor = {
			limit = {
				sell_titles_0006_vassal_trigger = yes
				NOT = { is_in_list = potential_chastisers }
			}
			add_to_list = potential_chastisers
		}
		random_in_list = {
			list = potential_chastisers
			weight = {
				base = 100
				compare_modifier = {
					value = ai_honor
				}
				modifier = {
					add = 50
					has_trait = honest
				}
			}
			save_scope_as = vassal
		}
		scope:vassal = {
			add_opinion = {
				target = root
				modifier = respect_opinion
				opinion = -30
			}
		}
	}

	option = { # How dare you shame me so?!
		name = sell_titles.0006.a
		add_prestige = medium_prestige_loss
		add_character_modifier = {
			modifier = sell_titles_shameful_behavior_modifier
			years = 5
		}
		progress_towards_rival_effect = {
			REASON = rival_shamed_sell_titles
			CHARACTER = scope:vassal
			OPINION = 0
		}
	}
}


################################
# Chains
# 1000-1999
################################


################################
# Merchant
# by Petter Vilberg
################################
# Setup
sell_titles.1001 = {
	hidden = yes

	trigger = {
		NOT = { has_character_flag = had_sell_titles_merchant_event }
		is_ai = no
		is_landed = yes

		exists = father
	}

	weight_multiplier = {
		base = 1
		modifier = {
			add = -0.3
			highest_held_title_tier = tier_kingdom
		}
		modifier = {
			add = -0.7
			highest_held_title_tier = tier_empire
		}
	}

	immediate = {
		add_character_flag = had_sell_titles_merchant_event

		create_character = {
			template = merchant_template
			location = root.capital_province
			save_scope_as = merchant
		}
		scope:merchant = {
			visit_court_of = root	
			add_gold = root.massive_gold_value
		}

		trigger_event = sell_titles.1002
	}
}

# Merchant requests dynasty membership
sell_titles.1002 = {
	type = character_event
	title = sell_titles.1002.t
	desc = sell_titles.1002.desc
	theme = stewardship
	left_portrait = {
		character = root
		animation = stunned
	}
	right_portrait = {
		character = scope:merchant
		animation = personality_bold
	}

	trigger = {
		exists = house
	}

	option = {
		name = {
			trigger = { religion = religion:islam_religion }
			text = sell_titles.1002.a.muslim
		}
		name = {
			text = sell_titles.1002.a
		}
		add_prestige = medium_prestige_loss
		dynasty = {
			add_dynasty_prestige = major_dynasty_prestige_loss
		}
		scope:merchant = {
			pay_short_term_gold = {
				target = root
				gold = root.massive_gold_value
			}
			set_house = root.house
			hidden_effect = {
				set_father = root.father
			}
		}
		hidden_effect = {
			add_courtier = scope:merchant
			trigger_event = {
				id = sell_titles.1003
				days = 365
			}
		}
	}
	
	option = { # My name is not for sale
		name = sell_titles.1002.b
		dynasty = {
			add_dynasty_prestige = minor_dynasty_prestige_gain
		}
	}
}

# Roll for follow-up
sell_titles.1003 = {
	hidden = yes
	
	trigger = {
		scope:merchant = {
			is_alive = yes
			is_imprisoned = no
			OR = {
				is_courtier_of = root
				is_vassal_of = root
			}
		}
	}
	
	immediate = {
		random_list = {
			80 = { } # Nothing happens
			30 = { # They're a great knight!
				trigger = {				
					scope:merchant = {
						can_be_combatant_based_on_gender_trigger = {
							ARMY_OWNER = root
						}
					}
				}
				trigger_event = sell_titles.1004
			}
			20 = { # Merchant is wise!
				trigger_event = sell_titles.1005
			}
			20 = { # Merchant is very clever
				trigger_event = sell_titles.1006
			}
			10 = { # Merchant disgraces the Dynasty
				trigger_event = sell_titles.1007
			}
		}
	}	
}

# Merchant turns out to be a great knight!
sell_titles.1004 = {
	type = character_event
	title = sell_titles.1004.t
	desc = sell_titles.1004.desc
	theme = stewardship
	left_portrait = {
		character = root
		animation = happiness
	}
	right_portrait = {
		character = scope:merchant
		animation = marshal_random_weapon
	}

	option = {
		name = sell_titles.1004.a
		scope:merchant = {
			add_prowess_skill = 7
			add_martial_skill = 5
		}
		if = {
			limit = {
				can_add_hook = {
					target = scope:merchant
					type = favor_hook
				}
			}
			add_hook = {
				target = scope:merchant
				type = favor_hook
			}
		}
	}
}

# Merchant turns out to be very wise!
sell_titles.1005 = {
	type = character_event
	title = sell_titles.1004.t
	desc = sell_titles.1005.desc
	theme = stewardship
	left_portrait = {
		character = root
		animation = happiness
	}
	right_portrait = {
		character = scope:merchant
		animation = personality_rational
	}

	option = {
		name = sell_titles.1005.a
		scope:merchant = {
			add_learning_skill = 5
		}
		add_character_modifier = {
			modifier = sell_titles_related_to_wisdom_modifier
			years = 10
		}
		if = {
			limit = {
				can_add_hook = {
					target = scope:merchant
					type = favor_hook
				}
			}
			add_hook = {
				target = scope:merchant
				type = favor_hook
			}
		}
	}
}

# Merchant turns out to be very clever!
sell_titles.1006 = {
	type = character_event
	title = sell_titles.1004.t
	desc = sell_titles.1006.desc
	theme = stewardship
	left_portrait = {
		character = root
		animation = happiness
	}
	right_portrait = {
		character = scope:merchant
		animation = personality_rational
	}

	option = {
		name = sell_titles.1006.a
		scope:merchant = {
			add_intrigue_skill = 5
		}
		add_character_modifier = {
			modifier = sell_titles_clever_servant_modifier
			years = 10
		}
		if = {
			limit = {
				can_add_hook = {
					target = scope:merchant
					type = favor_hook
				}
			}
			add_hook = {
				target = scope:merchant
				type = favor_hook
			}
		}
	}
}

# Merchant turns out to be trouble
sell_titles.1007 = {
	type = character_event
	title = sell_titles.1007.t
	desc = sell_titles.1007.desc
	theme = stewardship
	left_portrait = {
		character = root
		animation = worry
	}
	right_portrait = {
		character = scope:merchant
		animation = schadenfreude
	}

	immediate = {
		hidden_effect = {
			if = {
				limit = {
					any_relation = {
						type = oaf
						count < 1
					}
				}
				set_relation_oaf = scope:merchant
			}
		}
	}

	option = {
		name = sell_titles.1007.a
		dynasty = {
			add_dynasty_prestige = medium_dynasty_prestige_loss
		}
		if = {
			limit = {
				exists = dynasty.dynast
				NOR = {
					dynasty.dynast = root
					dynasty.dynast = scope:merchant
				}
			}
			dynasty.dynast = {
				add_opinion = {
					target = root
					modifier = respect_opinion
					opinion = -30
				}
			}
		}
		if = {
			limit = {
				exists = house.house_head
				NOR = {
					house.house_head = root
					house.house_head = scope:merchant
				}
			}
			house.house_head = {
				add_opinion = {
					target = root
					modifier = respect_opinion
					opinion = -30
				}
			}
		}
	}
}
