﻿namespace = gdp_merchant

####
## Systemic/player onboarding events
# gdp_merchant.0001-0003 - Become Merchant - Voluntary
# gdp_merchant.0010 - Associate asks to be adopted
# gdp_merchant.0015 - No Trader chosen on Trade Route created
# gdp_merchant.0060-0061 - Stressed trader asks to be recalled
# gdp_merchant.0020 - Associates struggle for empty/ineffectual Partner position
# gdp_merchant.0025 - Artisan Purpose Recruitment Opportunity
# gdp_merchant.0030 - Creditor Purpose Recruitment Opportunity
# gdp_merchant.0035 - Caravaneer Purpose Recruitment Opportunity
# gdp_merchant.0101-0103 - Purchase Land - Merchant
# gdp_merchant.0110-0111 - Merge Company interaction outcome letters
# gdp_merchant.1001-1002 - Generating starting courtiers for player and AI characters
# gdp_merchant.1003 - Select a starting company focus.
# gdp_merchant.1004 - Generating starting courtiers for player patricians
# gdp_merchant.1200-1201 - Associate requests
## Monopoly and Charter events
# gdp_merchant.2000 - Bare Market
# gdp_merchant.2010 - Monopoly: The Craftsman
# gdp_merchant.2015 - The Price of X
# gdp_merchant.2020 - A Seat at the Table
# gdp_merchant.2025 - The Lesser Merchant
# gdp_merchant.3000-3100 - Court position boons
# gdp_merchant.4000 - 4100 - Silk Road Merchant events
### Usury Events
# gdp_merchant.5000-5040 - Disguised Usury handling and events
# gdp_merchant.8001-8100 - Merchant company capstones
# gdp_merchant.9005 - Merchant Eviction Event

# Become Merchant - Voluntary
gdp_merchant.0001 = {
	type = character_event
	window = fullscreen_event
	queue_icon = "gfx/interface/icons/splash_icons/splash_merchant.dds"
	title = gdp_merchant.0001.t
	desc = {
		desc = gdp_merchant.0001.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:laamp_heir
					scope:old_primary_title ?= { holder ?= scope:laamp_inheritor }
				}
				desc = gdp_merchant.0001.heir
			}
			triggered_desc = {
				trigger = {
					exists = scope:laamp_liege
					scope:old_primary_title ?= { holder ?= scope:laamp_inheritor }
				}
				desc = gdp_merchant.0001.liege
			}
			triggered_desc = {
				trigger = { scope:old_primary_title ?= { holder ?= scope:laamp_inheritor } }
				desc = gdp_merchant.0001.new
			}
		}
	}
	theme = realm
	left_portrait = {
		character = root
		animation = steward
	}
	lower_right_portrait = {
		character = scope:laamp_inheritor
		trigger = { exists = scope:laamp_inheritor }
	}
	override_background = { reference = gdp_fullscreen_merchant_positive }
	cooldown = { days = 5 }

	trigger = {
		OR = {
			exists = scope:laamp_inheritor
			government_has_flag = government_is_landless_adventurer
		}
	}

	immediate = {
		if = {
			limit = { exists = scope:laamp_inheritor }
			scope:laamp_inheritor = { save_scope_as = become_merchant_flavor_char }
		}
		else = { save_scope_as = become_merchant_flavor_char }
		# Create Merchant
		create_merchant_title_effect = {
			REASON = flag:voluntary
			FLAVOR_CHAR = scope:become_merchant_flavor_char
		}
		scope:laamp_inheritor ?= {
			# Tooltip for player
			custom_tooltip = become_landless_adventurer_decision_title_scope_tt
			# Create a loving family so inheritance isn't so borked
			if = {
				limit = { has_character_flag = new_laamp_inheritor }
				trigger_event = game_rule.1001
				remove_character_flag = new_laamp_inheritor
			}
		}
		save_scope_as = merchant
		scope:old_liege ?= { trigger_event = gdp_merchant.0002 }
		adventurer_prestige_reset_effect = yes
		trigger_event = gdp_merchant.1001
		stress_impact = {
			craven = minor_stress_impact_gain
			content = minor_stress_impact_gain
			lazy = minor_stress_impact_gain
		}
	}

	option = {
		name = gdp_merchant.0001.a
		add_character_modifier = {
			modifier = gdp_voluntary_merchant_modifier
			years = 5
		}
	}
}

# Liege event
gdp_merchant.0002 = {
	type = letter_event
	opening = gdp_merchant.0002.opening
	desc = gdp_merchant.0002.desc
	sender = scope:merchant

	trigger = { is_ai = no }

	immediate = {
		scope:merchant = { create_landless_adventurer_title_tooltip_effect = yes }
	}

	option = {
		name = gdp_merchant.0002.a
	}
}

# Associate asks to be adopted
gdp_merchant.0010 = {
	type = character_event
	title = gdp_merchant.0010.t
	desc = {
		desc = gdp_merchant.0010.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:associate = { has_character_flag = generated_starting_character }
				}
				desc = gdp_merchant.0010.desc.starting
			}
			desc = gdp_merchant.0010.desc.starting_fallback
		}
		desc = gdp_merchant.0010.desc.cont
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:heir }
				desc = gdp_merchant.0010.desc.children
			}
			desc = gdp_merchant.0010.desc.no_children
		}
		desc = gdp_merchant.0010.desc.outro
	}
	theme = merchant
	left_portrait = {
		character = root
		animation = interested
	}
	right_portrait = {
		character = scope:associate
		animation = nervous
	}

	cooldown = { years = 100 }

	trigger = {
		# Mechanical Player Guidance event
		is_ai = no
		# Only for merchants
		government_has_flag = government_is_merchant
		# Relevant conditions
		is_adult = yes
		NOT = {
			any_child = { is_adult = yes } # includes having no children at all
		}
		any_courtier = {
			NOR = {
				is_spouse_of = root
				is_close_family_of = root
				has_trait = adopted_no_inheritance
				house ?= root.house
			}
		}
		# Some time should have passed
		years_from_game_start >= 2
	}

	weight_multiplier = {
		base = 1
		modifier = { # Increase weight as time goes on, per ruler
			# If we're in China/Korea, there is Confucian content we want to expedite instead
			location = {
				NOR = {
					geographical_region = world_asia_china
					geographical_region = world_asia_korea
				}
			}
			add = {
				value = years_as_ruler
				multiply = 10
				min = 0 # avoid -1
			}
		}
	}

	immediate = {
		random_courtier = {
			limit = {
				can_be_child_of = root
				NOR = {
					is_spouse_of = root
					is_close_family_of = root
					has_trait = adopted_no_inheritance
					house ?= root.house
				}
			}
			alternative_limit = {
				age <= root.age
				NOR = {
					is_spouse_of = root
					is_close_family_of = root
					has_trait = adopted_no_inheritance
					house ?= root.house
				}
			}
			alternative_limit = {
				NOR = {
					is_spouse_of = root
					is_close_family_of = root
					has_trait = adopted_no_inheritance
					house ?= root.house
				}
			}
			weight = {
				base = 1
				modifier = {
					factor = 2
					has_character_flag = generated_starting_character
				}
			}
			save_scope_as = associate
		}
		# Saving any children for loc
		if = {
			limit = {
				any_child = { count >= 1 }
			}
			primary_heir = { save_scope_as = heir }
		}
	}

	option = { # MY DAUGHTER/SON
		name = gdp_merchant.0010.a
		custom_tooltip = merchant_adoption_event_tt
		merchant_adopt_effect = {
			CHILD = scope:associate
			ADOPTER = root
		}
		create_character_memory = {
			type = merchant_family_adopted
			participants = { adoptee = scope:associate }
		}
		scope:new_memory = {
			set_variable = {
				name = adopted_house
				value = root.house
			}
		}
		scope:associate = {
			create_character_memory = {
				type = was_merchant_family_adopted
				participants = { adopter = root }
			}
			scope:new_memory = {
				set_variable = {
					name = adopted_house
					value = root.house
				}
			}
		}
		reverse_add_opinion = {
			target = scope:associate
			modifier = grateful_opinion
			opinion = 30
		}
		house = {
			add_unity_value = {
				value = medium_unity_loss
				character = root
				desc = merchant_unity_adoption_loss
			}
		}
		stress_impact = {
			callous = minor_stress_impact_gain
			arrogant = medium_stress_impact_gain
			compassionate = medium_stress_impact_loss
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_sociability = 1
				ai_compassion = 1
				ai_zeal = -1
			}
			modifier = {
				has_trait = compassionate
				add = 20
			}
			modifier = {
				OR = {
					has_trait = callous
					has_trait = arrogant
				}
				factor = 0
			}
		}
	}

	option = { # Maybe you should do more.
		name = gdp_merchant.0010.b
		scope:associate = {
			add_character_modifier = {
				modifier = gdp_driven_by_motivation_modifier
				years = 10
			}
		}
		reverse_add_opinion = {
			modifier = irritated_opinion
			target = scope:associate
			opinion = -10
		}
		house = {
			add_unity_value = {
				value = medium_unity_gain
				character = root
				desc = merchant_unity_adoption_loss
			}
		}
		stress_impact = {
			ambitious = medium_stress_impact_loss
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 1
				ai_energy = 1
			}
			modifier = {
				has_trait = ambitious
				add = 50
			}
		}
	}

	option = { # I'm good
		name = gdp_merchant.0010.c
		reverse_add_opinion = {
			modifier = insulted_opinion
			target = scope:associate
			opinion = -5
		}
		stress_impact = {
			content = minor_stress_impact_loss
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_energy = -1
			}
		}
	}
}

scripted_effect gdp_merchant_event_0015_appoint_effect = {
	$CHOSEN$ = { save_temporary_scope_as = new_trader }
	trade_route_trader_opinion_effect = { TRADER = scope:new_trader }
	scope:new_trader = {
		show_as_tooltip = {
			if = {
				limit = {
					NOT = { has_trait = lifestyle_merchant }
				}
				add_trait = lifestyle_merchant # actually added by appointment on_action
			}
			add_trait_xp = { # always rewarded by this event
				trait = lifestyle_merchant
				track = caravaneer
				value = lifestyle_merchant_xp_gain_medium_value
			}
		}
		hidden_effect = {
			if = {
				limit = { has_trait = lifestyle_merchant } # added by appointment on_action, but prevent errors
				add_trait_xp = {
					trait = lifestyle_merchant
					track = caravaneer
					value = lifestyle_merchant_xp_gain_medium_value
				}
			}
		}
	}
	hidden_effect = {
		while = {
			count = 3
			trigger_event = { on_action = spawn_trade_opportunity_contract_on_action }
		}
	}
}

# No Trader chosen on Trade Route created
gdp_merchant.0015 = {
	type = character_event
	title = gdp_merchant.0015.t
	desc = {
		desc = gdp_merchant.0015.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:courtier_1 = { sum_of_all_skills_value >= sum_of_all_skills_threshold_good }
				}
				desc = gdp_merchant.0015.desc_good_intellect
			}
			triggered_desc = {
				trigger = {
					scope:courtier_1 = { sum_of_all_skills_value >= sum_of_all_skills_threshold_excellent }
				}
				desc = gdp_merchant.0015.desc_excellent_intellect
			}
			desc = gdp_merchant.0015.desc_poor_intellect
		}
	}
	theme = merchant
	left_portrait = {
		character = root
		animation = chancellor
	}
	right_portrait = {
		character = scope:courtier_1
		animation = flirtation
	}
	lower_left_portrait = scope:courtier_2
	lower_center_portrait = scope:courtier_3

	trigger = {
		# Only players get this event - AI chars automatically assign traders
		is_ai = no
		is_adult = yes
		government_allows = profit_trade_routes
		scope:trade_route = {
			is_commission_route = no
			has_trader = no # stop this firing if you assigned a trader yourself
		}
		any_courtier = {
			is_available_quick = {
				adult = yes
				incapable = no
				imprisoned = no
			}
			is_trader = no
			trigger_if = {
				limit = {
					root = { government_has_flag = government_is_republic }
				}
				is_councillor = no
			}
		}
	}

	immediate = {
		ordered_courtier = {
			limit = {
				is_available_quick = {
					adult = yes
					incapable = no
					imprisoned = no
				}
				is_trader = no
				trigger_if = {
					limit = {
						root = { government_has_flag = government_is_republic }
					}
					is_councillor = no
				}
				NOR = {
					scope:courtier_1 ?= this
					scope:courtier_2 ?= this
					scope:courtier_3 ?= this
				}
			}
			max = 3
			check_range_bounds = no
			assign_quirk_effect = yes
			if = {
				limit = { exists = scope:courtier_2 }
				save_scope_as = courtier_3
			}
			else_if = {
				limit = { exists = scope:courtier_1 }
				save_scope_as = courtier_2
			}
			else = { save_scope_as = courtier_1 }
		}
	}

	option = { # I choose you!!
		name = gdp_merchant.0015.a
		scope:trade_route = {
			assign_random_trader_find_highest_task_effect = { CHAR = scope:courtier_1 }
		}
		gdp_merchant_event_0015_appoint_effect = { CHOSEN = scope:courtier_1 }
		# No stress for mechanical events
	}

	option = { # You'll do I guess
		name = gdp_merchant.0015.b
		trigger = { exists = scope:courtier_2 }
		scope:trade_route = {
			assign_random_trader_find_highest_task_effect = { CHAR = scope:courtier_2 }
		}
		gdp_merchant_event_0015_appoint_effect = { CHOSEN = scope:courtier_2 }
		# No stress for mechanical events
	}

	option = { # Or you
		name = gdp_merchant.0015.c
		trigger = { exists = scope:courtier_3 }
		scope:trade_route = {
			assign_random_trader_find_highest_task_effect = { CHAR = scope:courtier_3 }
		}
		gdp_merchant_event_0015_appoint_effect = { CHOSEN = scope:courtier_3 }
		# No stress for mechanical events
	}

	option = { # Nah let me do my thing
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = {
							scope:courtier_1 = {
								OR = {
									sum_of_all_skills_value >= sum_of_all_skills_threshold_good
									sum_of_all_skills_value >= sum_of_all_skills_threshold_excellent
								}
							}
						}
						desc = gdp_merchant.0015.c
					}
					desc = gdp_merchant.0015.c.fallback
				}
			}
		}
		custom_tooltip = gdp_merchant.0015.c.tt
		# No stress for mechanical events
	}
}

 scripted_trigger gdp_merchant_event_0020_better_partners_trigger = {
	is_available_quick = {
		adult = yes
		imprisoned = no
		incapable = no
	}
	can_be_employed_as = partner_court_position
	"aptitude(partner_court_position)" >= 2
	NOR = {
		scope:candidate_1 ?= this
		scope:candidate_2 ?= this
		scope:candidate_3 ?= this
		has_any_moderate_bad_relationship_with_character_trigger = { CHARACTER = root }
	}
	opinion = {
		target = root
		value >= -25
	}
	# We keep disloyal and loyalty-hooked characters out, for tone
	NOR = {
		has_trait = disloyal
		root = { has_strong_hook = prev }
	}
}

scripted_effect gdp_merchant_0020_apply_appointment_effect = {
	reverse_add_opinion = {
		target = $CHOSEN$
		modifier = grateful_opinion
		opinion = 50
	}
	every_in_list = {
		list = candidates
		limit = {
			NOT = { this = $CHOSEN$ }
		}
		custom = gdp_merchant.0020.a.custom
		add_opinion = {
			target = $EMPLOYER$
			modifier = annoyed_opinion
			opinion = -10
		}
	}
	if = {
		limit = { NOT = { has_hook = $CHOSEN$ } }
		add_hook = {
			target = $CHOSEN$
			type = loyalty_hook
		}
	}
	# House Unity impact
	house ?= {
		if = {
			limit = { $CHOSEN$.house ?= this }
			add_unity_value = {
				value = medium_unity_gain
				character = $EMPLOYER$
				desc = merchant_unity_partner_gain
			}
		}
		else = {
			add_unity_value = {
				value = medium_unity_loss
				character = $EMPLOYER$
				desc = merchant_unity_partner_loss
			}
		}
	}
}

# Associates struggle for empty/ineffectual Partner position
gdp_merchant.0020 = {
	type = character_event
	title = gdp_merchant.0020.t
	desc = {
		desc = gdp_merchant.0020.desc
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:old_holder }
				desc = gdp_merchant.0020.desc.partner_exists
			}
			desc = gdp_merchant.0020.desc.fallback
		}
		desc = gdp_merchant.0020.desc.body
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:candidate_3 }
				desc = gdp_merchant.0020.desc.outro_three
			}
			triggered_desc = {
				trigger = { exists = scope:candidate_2 }
				desc = gdp_merchant.0020.desc.outro_two
			}
			desc = gdp_merchant.0020.desc.outro_one
		}
	}
	theme = merchant
	override_background = { reference = study }
	left_portrait = {
		character = root
		animation = thinking
	}
	right_portrait = {
		trigger = { exists = scope:old_holder }
		character = scope:old_holder
		animation = stressed_teacher
	}
	lower_left_portrait = scope:candidate_1
	lower_center_portrait = scope:candidate_2
	lower_right_portrait = scope:candidate_3

	cooldown = { years = 10 }

	trigger = {
		# Only for merchants
		government_has_flag = government_is_merchant
		# Either you don't have a Partner, or your Partner sucks.
		OR = {
			can_employ_court_position_type = partner_court_position
			AND = {
				employs_court_position = partner_court_position
				any_court_position_holder = {
					type = partner_court_position
					"aptitude(partner_court_position)" <= 1
				}
			}
		}
		# Standard checks.
		is_adult = yes
		is_available_allow_travelling = yes
		# Make sure we're on dry land.
		is_location_valid_for_travel_event_on_land = yes
		# Squabbles shouldn't happen in concerted households
		NOT = {
			house ?= { has_house_unity_stage = ennobled_cs }
		}
		# You have at least one characters who'd be better.
		any_courtier = {
			gdp_merchant_event_0020_better_partners_trigger = yes
		}
	}

	weight_multiplier = {
		modifier = { # Increase weight early on
			# If we're in China/Korea, there is Confucian content we want to expedite instead
			location = {
				NOR = {
					geographical_region = world_asia_china
					geographical_region = world_asia_korea
				}
			}
			add = {
				value = 100
				subtract = {
					value = years_as_ruler
					multiply = 10
				}
				min = 0 # avoid -1
			}
		}
		modifier = { # Increase weight if partner honks
			employs_court_position = partner_court_position
			any_court_position_holder = {
				type = partner_court_position
			}
			add = {
				value = 5
				subtract = "court_position:partner_court_position.aptitude(partner_court_position)"
				multiply = 10
			}
		}
	}

	immediate = {
		# Is the spot vacant, or merely incompetent?
		court_position_old_holder_effect = { POS = partner EMPLOYER = root }
		# Available options
		ordered_courtier = {
			limit = { gdp_merchant_event_0020_better_partners_trigger = yes }
			order_by = {
				value = "aptitude(partner_court_position)"
				# Stewardship only breaks ties, so we add a little of it after scaling aptitude
				multiply = 10
				add = {
					add = stewardship
					divide = 10
					ceiling = yes
				}
				# then ai_energy
				add = {
					value = ai_energy
					multiply = 0.1
				}
			}
			check_range_bounds = no
			max = 3
			if = {
				limit = { exists = scope:candidate_2 }
				save_scope_as = candidate_3
			}
			else_if = {
				limit = { exists = scope:candidate_1 }
				save_scope_as = candidate_2
			}
			else = { save_scope_as = candidate_1 }
			add_to_list = candidates
		}
	}

	# Choose scope:candidate_1
	option = {
		name = gdp_merchant.0020.a
		court_position_grant_effect = {
			POS = partner
			CANDIDATE = scope:candidate_1
			EMPLOYER = root
		}
		gdp_merchant_0020_apply_appointment_effect = {
			CHOSEN = scope:candidate_1
			EMPLOYER = root
		}
		ai_chance = {
			base = 1
			modifier = {
				add = {
					value = "scope:candidate_1.aptitude(partner_court_position)"
					multiply = 10
				}
			}
			opinion_modifier = { opinion_target = scope:candidate_1 multiplier = 0.25 }
			opinion_modifier = {
				trigger = { exists = scope:old_holder }
				opinion_target = scope:old_holder
				multiplier = -0.25
			}
		}
	}

	# Choose scope:candidate_2
	option = {
		name = gdp_merchant.0020.b
		trigger = { exists = scope:candidate_2 }
		court_position_grant_effect = {
			POS = partner
			CANDIDATE = scope:candidate_2
			EMPLOYER = root
		}
		gdp_merchant_0020_apply_appointment_effect = {
			CHOSEN = scope:candidate_2
			EMPLOYER = root
		}
		ai_chance = {
			base = 1
			modifier = {
				add = {
					value = "scope:candidate_2.aptitude(partner_court_position)"
					multiply = 10
				}
			}
			opinion_modifier = { opinion_target = scope:candidate_2 multiplier = 0.25 }
			opinion_modifier = {
				trigger = { exists = scope:old_holder }
				opinion_target = scope:old_holder
				multiplier = -0.25
			}
		}
	}

	# Choose scope:candidate_3
	option = {
		name = gdp_merchant.0020.c
		trigger = { exists = scope:candidate_3 }
		court_position_grant_effect = {
			POS = partner
			CANDIDATE = scope:candidate_3
			EMPLOYER = root
		}
		gdp_merchant_0020_apply_appointment_effect = {
			CHOSEN = scope:candidate_3
			EMPLOYER = root
		}
		ai_chance = {
			base = 1
			modifier = {
				add = {
					value = "scope:candidate_3.aptitude(partner_court_position)"
					multiply = 10
				}
			}
			opinion_modifier = { opinion_target = scope:candidate_3 multiplier = 0.25 }
			opinion_modifier = {
				trigger = { exists = scope:old_holder }
				opinion_target = scope:old_holder
				multiplier = -0.25
			}
		}
	}

	# Post remains vacant/partner keeps post
	option = {
		name = gdp_merchant.0020.d
		# Consolation prestige.
		add_prestige = minor_prestige_gain
		scope:old_holder ?= {
			add_opinion = {
				target = root
				modifier = grateful_opinion
				opinion = 30
			}
		}
		every_in_list = {
			list = candidates
			custom = gdp_merchant.0020.d.custom
			add_opinion = {
				target = root
				modifier = insulted_opinion
				opinion = -10
			}
		}
		# Lose stress if we're playing to our reputation + it's appropriate.
		if = {
			limit = { exists = scope:old_holder }
			stress_and_fulfillment_impact = { loyal = medium_stress_loss }
		}
		ai_chance = {
			base = 1
			ai_value_modifier = { ai_energy = -0.25 }
			opinion_modifier = {
				trigger = { exists = scope:old_holder }
				opinion_target = scope:old_holder
				multiplier = 0.25
			}
		}
	}
}

scripted_effect gdp_merchant_traveler_impressed_opinion_offset_effect = {
	# We want them to actually be impressed by us
	hidden_effect = {
		scope:traveler = {
			if = {
				limit = {
					opinion = {
						target = root
						value < 0
					}
				}
				add_opinion = {
					target = root
					modifier = impressed_opinion
					opinion = {
						value = "opinion(root)"
						multiply = -1
					}
				}
			}
		}
	}
}

scripted_effect gdp_merchant_hire_traveler_effect = {
	add_courtier = scope:traveler
	reverse_add_opinion = {
		target = scope:traveler
		modifier = recruited_me_opinion
		opinion = 50
	}
	scope:traveler = { remove_character_flag = created }
}

# Artisan Purpose Recruitment Opportunity
gdp_merchant.0025 = {
	type = character_event
	title = gdp_merchant.0025.t
	desc = gdp_merchant.0025.desc

	theme = merchant
	override_background = gdp_tannery

	left_portrait = {
		character = root
		animation = pondering
		camera = camera_event_left_to_the_left
	}

	right_portrait = {
		character = scope:traveler
		animation = hunting_carcass_start
	}

	trigger = {
		# Only for merchant house heads
		government_has_flag = government_is_merchant
		is_house_head = yes
		# Artisan track level 2
		has_trait_xp = {
			trait = lifestyle_merchant
			track = artisan
			value >= 65
		}
		primary_title ?= {
			NOT = { has_variable = had_event_gdp_merchant_0025 }
		}
	}

	immediate = {
		if = {
			limit = {
				any_pool_character = {
					province = root.location
					is_physically_able_adult = yes
					has_trait = lifestyle_hunter
				}
			}
			random_pool_character = {
				province = root.location
				limit = {
					is_physically_able_adult = yes
					has_trait = lifestyle_hunter
				}
				save_scope_as = traveler
			}
		}
		else = {		
			create_character = {
				template = trader_stewardship_template
			 	location = root.location
			    culture = root.location.culture
			    faith = root.location.faith
				learning = { 5 8 }
				prowess = { 5 10 }
			    save_scope_as = traveler
			    after_creation = {
			    	add_trait = lifestyle_hunter
					add_trait_xp = {
						trait = lifestyle_hunter
						track = hunter
						value = 50
					}
					add_character_flag = created
			    }
			}
		}
		gdp_merchant_traveler_impressed_opinion_offset_effect = yes
		primary_title ?= { set_variable = had_event_gdp_merchant_0025 }
	}

	option = { # Glad to have you!
		name = gdp_merchant.0025.a
		gdp_merchant_hire_traveler_effect = yes
		house ?= {
			add_unity_value = {
				value = medium_unity_loss
				character = root
				desc = merchant_unity_artisan_recruit_loss
			}
		}
		stress_and_fulfillment_impact = {
			gregarious = minor_stress_impact_loss
			paranoid = medium_stress_impact_gain
		}

		ai_chance = {
			base = 100
			modifier = {
				add = 25
				has_trait = gregarious
			}
			modifier = {
				factor = 0
				OR = {
					has_trait = shy
					has_trait = paranoid
				}
			}
		}
	}

	option = { # No... actually, we're good.
		name = gdp_merchant.0025.b
		primary_title = {
			add_trade_good_category_xp = {
				target = trade_good_category:trade_category_clothes
				value = trade_expertise_xp_monumental_value
			}
		}
		house ?= {
			add_unity_value = {
				value = medium_unity_gain
				character = root
				desc = merchant_unity_artisan_recruit_decline_gain
			}
		}
		stress_and_fulfillment_impact = {
			diligent = minor_stress_impact_loss
		}

		ai_chance = {
			base = 100
			modifier = {
				add = 25
				has_trait = diligent
			}
		}
	}

	after = {
		scope:traveler = { silent_disappearance_ai_if_created_effect = yes }
	}
}

# Creditor Purpose Recruitment Opportunity
gdp_merchant.0030 = {
	type = character_event
	title = gdp_merchant.0030.t
	desc = gdp_merchant.0030.desc

	theme = merchant
	override_background = gdp_smithy_workshop

	left_portrait = {
		character = root
		animation = thinking
		camera = camera_event_left_to_the_left
	}

	right_portrait = {
		character = scope:traveler
		animation = hero_flex
		camera = camera_event_right_forward
	}

	trigger = {
		government_has_flag = government_is_merchant
		is_house_head = yes
		has_trait_xp = {
			trait = lifestyle_merchant
			track = creditor
			value >= 65
		}
		primary_title ?= {
			NOT = { has_variable = had_event_gdp_merchant_0030 }
		}
	}

	immediate = {
		if = {
			limit = {
				any_pool_character = {
					province = root.location
					is_physically_able_adult = yes
					is_potential_knight = yes
					OR = {
						has_trait = education_martial
						has_trait = education_intrigue
					}
				}
			}
			random_pool_character = {
				province = root.location
				limit = {
					is_physically_able_adult = yes
					is_potential_knight = yes
					OR = {
						has_trait = education_martial
						has_trait = education_intrigue
					}
				}
				save_scope_as = traveler
			}
		}
		else = {
			create_character = {
				template = trader_martial_template
				location = root.location
				culture = root.location.culture
				faith = root.location.faith
				prowess = { 8 12 }
				save_scope_as = traveler
				after_creation = {
					add_character_flag = created
				}
			}
		}
		gdp_merchant_traveler_impressed_opinion_offset_effect = yes
		primary_title ?= { set_variable = had_event_gdp_merchant_0030 }
	}

	option = { # Glad to have you!
		name = gdp_merchant.0030.a
		gdp_merchant_hire_traveler_effect = yes
		scope:traveler = { set_knight_status = force }
		house ?= {
			add_unity_value = {
				value = medium_unity_loss
				character = root
				desc = merchant_unity_creditor_recruit_loss
			}
		}
		stress_and_fulfillment_impact = {
			gregarious = minor_stress_impact_loss
			paranoid = medium_stress_impact_gain
		}

		ai_chance = {
			base = 100
			modifier = {
				add = 25
				has_trait = gregarious
			}
			modifier = {
				factor = 0
				OR = {
					has_trait = shy
					has_trait = paranoid
				}
			}
		}
	}

	option = { # No... actually, we're good.
		name = gdp_merchant.0030.b
		primary_title = {
			add_trade_good_category_xp = {
				target = trade_good_category:trade_category_precious_metals
				value = trade_expertise_xp_monumental_value
			}
		}
		house ?= {
			add_unity_value = {
				value = medium_unity_gain
				character = root
				desc = merchant_unity_creditor_recruit_decline_gain
			}
		}
		stress_and_fulfillment_impact = {
			diligent = minor_stress_impact_loss
		}

		ai_chance = {
			base = 100
			modifier = {
				add = 25
				has_trait = diligent
			}
		}
	}

	after = {
		scope:traveler = { silent_disappearance_ai_if_created_effect = yes }
	}
}

# Caravaneer Purpose Recruitment Opportunity
gdp_merchant.0035 = {
	type = character_event
	title = gdp_merchant.0035.t
	desc = gdp_merchant.0035.desc

	theme = merchant
	override_background = gdp_artisan_street

	right_portrait = {
		character = scope:traveler
		animation = holding_scales_horse
		camera = camera_event_horse_left_forward
	}

	trigger = {
		government_has_flag = government_is_merchant
		is_house_head = yes
		has_trait_xp = {
			trait = lifestyle_merchant
			track = caravaneer
			value >= 65
		}
		primary_title ?= {
			NOT = { has_variable = had_event_gdp_merchant_0035 }
		}
	}

	immediate = {
		# We want them to be traveling
		random_empire = {
			limit = {
				any_de_jure_county = {
					culture != root.culture
					culture = { has_cultural_parameter = unlock_voluntary_mercantilism }
					"squared_distance(root.location)" < squared_distance_large
				}
			}
			random_de_jure_county = {
				limit = {
					culture != root.culture
					culture = { has_cultural_parameter = unlock_voluntary_mercantilism }
					"squared_distance(root.location)" < squared_distance_large
				}
				save_scope_as = foreign_merchant_home
			}
		}
		if = {
			limit = { NOT = { exists = scope:foreign_merchant_home } }
			root.location = { save_scope_as = foreign_merchant_home }
		}
		create_character = {
			template = trader_diplomacy_template
			location = root.location
			culture = scope:foreign_merchant_home.culture
			faith = scope:foreign_merchant_home.faith
			learning = { 5 8 }
			prowess = { 5 10 }
			save_scope_as = traveler
			after_creation = {
				add_character_flag = created
				set_location = root.location
			}
		}
		gdp_merchant_traveler_impressed_opinion_offset_effect = yes
		primary_title ?= { set_variable = had_event_gdp_merchant_0035 }
	}

	option = { # Glad to have you!
		name = gdp_merchant.0035.a
		gdp_merchant_hire_traveler_effect = yes
		house ?= {
			add_unity_value = {
				value = medium_unity_loss
				character = root
				desc = merchant_unity_caravaneer_recruit_loss
			}
		}
		stress_and_fulfillment_impact = {
			gregarious = minor_stress_impact_loss
			paranoid = medium_stress_impact_gain
		}

		ai_chance = {
			base = 100
			modifier = {
				add = 25
				has_trait = gregarious
			}
			modifier = {
				factor = 0
				OR = {
					has_trait = shy
					has_trait = paranoid
				}
			}
		}
	}

	option = { # No... actually, we're good.
		name = gdp_merchant.0035.b
		primary_title = {
			add_trade_good_category_xp = {
				target = trade_good_category:trade_category_luxury_consumption
				value = trade_expertise_xp_monumental_value
			}
		}
		house ?= {
			add_unity_value = {
				value = medium_unity_gain
				character = root
				desc = merchant_unity_caravaneer_recruit_decline_gain
			}
		}
		stress_and_fulfillment_impact = {
			diligent = minor_stress_impact_loss
		}

		ai_chance = {
			base = 100
			modifier = {
				add = 25
				has_trait = diligent
			}
		}
	}

	after = {
		scope:traveler = { silent_disappearance_ai_if_created_effect = yes }
	}
}

# A stressed trader asks the route owner to call the envoys home.
# scope:associate = the trader who wrote.
gdp_merchant.0060 = {
	type = letter_event
	opening = gdp_merchant.0060.opening
	desc = gdp_merchant.0060.desc
	sender = scope:associate

	trigger = {
		is_ai = no
		exists = scope:associate
		scope:associate = { is_alive = yes }
	}

	option = {
		name = gdp_merchant.0060.a
		trigger = {
			government_has_flag = government_has_merchant_company
			NOT = { has_character_flag = gdp_trade_envoys_recalled }
			NOT = { has_variable = gdp_trade_envoys_return_promise }
		}
		custom_tooltip = gdp_merchant.0060.a.tt
		set_variable = {
			name = gdp_trade_envoys_return_promise
			value = scope:associate
			months = 25
		}
		trigger_event = {
			id = gdp_merchant.0061
			years = 2
		}
	}

	option = {
		name = gdp_merchant.0060.b
		custom_tooltip = gdp_merchant.0060.b.tt
	}
}

# Hidden. Fires two years after the promise. Does nothing if Return Trade Envoys already cleared it.
gdp_merchant.0061 = {
	hidden = yes

	trigger = {
		has_variable = gdp_trade_envoys_return_promise
	}

	immediate = {
		if = {
			limit = {
				var:gdp_trade_envoys_return_promise ?= {
					is_alive = yes
					is_trader = yes
				}
			}
			var:gdp_trade_envoys_return_promise = { save_scope_as = associate }
		}
		if = {
			limit = { exists = scope:associate }
			send_interface_message = {
				type = event_generic_bad
				title = gdp_trade_envoys_return_promise_broken_title
				desc = gdp_trade_envoys_return_promise_broken_desc
				left_icon = scope:associate
				scope:associate = {
					add_opinion = {
						modifier = gdp_trader_broken_return_promise_opinion
						target = root
					}
				}
			}
		}
		remove_variable = gdp_trade_envoys_return_promise
	}
}

###
# gdp_merchant.0101-0103 - Purchase Land - Merchant
gdp_merchant.0101 = {
	type = character_event
	title = gdp_merchant.0101.t
	desc = {
		desc = gdp_merchant.0101.desc_republic
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:new_liege }
				desc = gdp_merchant.0101.liege
			}
			desc = gdp_merchant.0101.independent
		}
		desc = gdp_merchant.0101.republic
	}
	theme = republic
	left_portrait = {
		character = root
		animation = chancellor
	}
	right_portrait = {
		character = scope:recipient
		animation = steward
	}
	lower_left_portrait = scope:portrait_1
	lower_center_portrait = scope:portrait_2
	lower_right_portrait = scope:portrait_3
	override_background = {
		trigger = { scope:republic = yes }
		reference = market
	}

	immediate = {
		save_scope_as = merchant
		purchase_land_interaction_effect = {
			TITLE_RECEIVER = scope:actor
			TITLE_GIVER = scope:recipient
		}
		if = {
			limit = {
				exists = scope:new_liege
				scope:recipient != scope:new_liege
			}
			scope:new_liege = { save_scope_as = portrait_1 }
		}
		scope:recipient = { trigger_event = gdp_merchant.0102 }
	}

	option = {
		name = gdp_merchant.0101.a
		if = { # Vassal obligations
			limit = { scope:vassal_obligation = yes }
			vassal_contract_set_obligation_level = { type = feudal_government_taxes level = 4 }
			vassal_contract_set_obligation_level = { type = feudal_government_levies level = 4 }
		}
		# DISPLAY
		every_in_list = {
			list = new_arrivals
			custom_tooltip = new_republic_population_effect_tt
		}
	}
}

gdp_merchant.0102 = {
	type = letter_event
	opening = ep3_laamps.0101.opening
	desc = {
		desc = gdp_merchant.0102.desc 
		first_valid = {
			triggered_desc = {
				trigger = { scope:new_liege ?= scope:recipient }
				desc = gdp_merchant.0102.liege
			}
			triggered_desc = {
				trigger = { exists = scope:new_liege }
				desc = gdp_merchant.0102.top_liege
			}
			desc = gdp_merchant.0102.independent
		}
	}
	sender = scope:actor

	immediate = {
		show_as_tooltip = {
			purchase_land_interaction_effect = {
				TITLE_RECEIVER = scope:actor
				TITLE_GIVER = scope:recipient
			}
		}
		if = {
			limit = {
				top_liege != this
				scope:actor.liege != root
			}
			hidden_effect = {
				liege = {
					send_interface_message = {
						type = event_landless_adventurer_neutral
						title = ep3_laamps.0101.liege_message.title
						left_icon = scope:actor
						right_icon = scope:recipient
						show_as_tooltip = {
							purchase_land_interaction_effect = {
								TITLE_RECEIVER = scope:actor
								TITLE_GIVER = scope:recipient
							}
						}
					}
				}
			}
		}
	}

	option = {
		name = ep3_laamps.0101.a
		trigger = { scope:new_liege ?= scope:recipient }
	}

	option = {
		name = ep3_laamps.0101.b
		trigger = {
			exists = scope:new_liege
			NOT = { scope:new_liege ?= scope:recipient }
		}
	}

	option = {
		name = ep3_laamps.0101.c
		trigger = {
			NOT = { exists = scope:new_liege }
		}
	}
}

gdp_merchant.0103 = {
	type = letter_event
	opening = ep3_laamps.0102.opening
	desc = gdp_merchant.0103.desc
	sender = scope:actor

	option = { name = ep3_laamps.0102.a }
}

# Merge Company — acceptance event
gdp_merchant.0110 = {
	type = character_event
	title = gdp_merchant.0110.t
	desc = {
		desc = gdp_merchant.0110.desc
		first_valid = {
			triggered_desc = {
				trigger = { scope:personality = flag:ai_honor }
				desc = gdp_merchant.0110.desc.personality.ai_honor
			}
			triggered_desc = {
				trigger = { scope:personality = flag:ai_zeal }
				desc = gdp_merchant.0110.desc.personality.ai_zeal
			}
			triggered_desc = {
				trigger = { scope:personality = flag:ai_greed }
				desc = gdp_merchant.0110.desc.personality.ai_greed
			}
			triggered_desc = {
				trigger = { scope:personality = flag:ai_compassion }
				desc = gdp_merchant.0110.desc.personality.ai_compassion
			}
			triggered_desc = {
				trigger = { scope:personality = flag:ai_boldness }
				desc = gdp_merchant.0110.desc.personality.ai_boldness
			}
			triggered_desc = {
				trigger = { scope:personality = flag:ai_rationality }
				desc = gdp_merchant.0110.desc.personality.ai_rationality
			}
			triggered_desc = {
				trigger = { scope:personality = flag:ai_vengefulness }
				desc = gdp_merchant.0110.desc.personality.ai_vengefulness
			}
			triggered_desc = {
				trigger = { scope:personality = flag:ai_energy }
				desc = gdp_merchant.0110.desc.personality.ai_energy
			}
			triggered_desc = {
				trigger = { scope:personality = flag:ai_sociability }
				desc = gdp_merchant.0110.desc.personality.ai_sociability
			}
			triggered_desc = {
				trigger = { scope:personality = flag:ai_personality_fallback }
				desc = gdp_merchant.0110.desc.personality.ai_personality_fallback
			}
			desc = gdp_merchant.0110.desc.personality.ai_personality_fallback
		}
		desc = gdp_merchant.0110.desc.closing
	}
	left_portrait = {
		character = scope:actor
		animation = happiness
	}
	right_portrait = {
		character = scope:recipient
		animation = war_over_win
	}
	theme = merchant

	immediate = {
		scope:recipient = {
			select_merchant_personality_type_effect = yes
		}
		gdp_merge_company_effect = {
			MERGER = scope:actor
			SUBSUMED = scope:recipient
		}
	}

	option = {
		name = gdp_merchant.0110.a
	}
}

# Merge Company — decline letter
gdp_merchant.0111 = {
	type = letter_event
	opening = gdp_merchant.0111.opening
	desc = gdp_merchant.0111.desc
	sender = scope:recipient

	trigger = {
		is_ai = no
	}

	option = {
		name = gdp_merchant.0111.a
		progress_towards_rival_effect = {
			REASON = rival_proposed_merge_company
			CHARACTER = scope:actor
			OPINION = default_rival_opinion
		}
	}
}

# Patron construction promise unfulfilled
gdp_merchant.0200 = {
	type = character_event
	hidden = yes

	trigger = {
		government_has_flag = government_is_merchant
		exists = var:merchant_patron_fund_buildings_promise
		suzerain ?= var:merchant_patron_fund_buildings_promise
		var:merchant_patron_fund_buildings_year = current_year
	}

	immediate = {
		save_scope_as = merchant
		var:merchant_patron_fund_buildings_promise = { save_scope_as = patron }
		send_interface_toast = {
			type = event_toast_effect_bad
			title = gdp_merchant_patron_fund_buildings_promise_unfulfilled_toast
			right_icon = scope:patron
			scope:patron = {
				send_interface_toast = {
					type = event_toast_effect_bad
					title = gdp_merchant_patron_fund_buildings_promise_unfulfilled_toast
					right_icon = scope:merchant
					scope:merchant = { add_prestige = medium_prestige_loss }
					if = {
						limit = { is_ai = yes }
						add_opinion = {
							target = scope:merchant
							modifier = angry_opinion
							opinion = -20
						}
					}
				}
			}
		}
		remove_variable = merchant_patron_fund_buildings_promise
		remove_variable = merchant_patron_fund_buildings_year
	}
}

###
#	We generate some starting courtiers for you so that you don't get irredeemably screwed as a basic merchant.

scripted_effect generate_stewardship_associates = {
	while = {
		count = 2
		create_character = {
			template = pool_repopulate_stewardship
			dynasty = none
			employer = root
			culture = root.culture
			faith = root.faith
			diplomacy = {
				min_template_average_skill
				max_template_average_skill
			}
			martial = {
				min_template_average_skill
				max_template_average_skill
			}
			stewardship = {
				min_template_decent_skill
				max_template_decent_skill
			}
			intrigue = {
				min_template_average_skill
				max_template_average_skill
			}
			learning = {
				min_template_average_skill
				max_template_average_skill
			}
			prowess = {
				min_template_average_skill
				max_template_average_skill
			}
			after_creation = { add_to_list = spawned_chars_list }
		}
	}
	add_generated_associates_effect = yes
}

scripted_effect generate_diplomacy_associates = {
	while = {
		count = 2
		create_character = {
			template = pool_repopulate_diplomacy
			dynasty = none
			employer = root
			culture = root.culture
			faith = root.faith
			diplomacy = {
				min_template_decent_skill
				max_template_decent_skill
			}
			martial = {
				min_template_average_skill
				max_template_average_skill
			}
			stewardship = {
				min_template_average_skill
				max_template_average_skill
			}
			intrigue = {
				min_template_average_skill
				max_template_average_skill
			}
			learning = {
				min_template_average_skill
				max_template_average_skill
			}
			prowess = {
				min_template_average_skill
				max_template_average_skill
			}
			after_creation = { add_to_list = spawned_chars_list }
		}
	}
	add_generated_associates_effect = yes
}

scripted_effect generate_learning_associates = {
	while = {
		count = 2
		create_character = {
			template = pool_repopulate_learning
			dynasty = none
			employer = root
			culture = root.culture
			faith = root.faith
			diplomacy = {
				min_template_average_skill
				max_template_average_skill
			}
			martial = {
				min_template_average_skill
				max_template_average_skill
			}
			stewardship = {
				min_template_average_skill
				max_template_average_skill
			}
			intrigue = {
				min_template_average_skill
				max_template_average_skill
			}
			learning = {
				min_template_decent_skill
				max_template_decent_skill
			}
			prowess = {
				min_template_average_skill
				max_template_average_skill
			}
			after_creation = { add_to_list = spawned_chars_list }
		}
	}
	add_generated_associates_effect = yes
}

scripted_effect generate_intrigue_associates = {
	while = {
		count = 2
		create_character = {
			template = pool_repopulate_intrigue
			dynasty = none
			employer = root
			culture = root.culture
			faith = root.faith
			diplomacy = {
				min_template_average_skill
				max_template_average_skill
			}
			martial = {
				min_template_average_skill
				max_template_average_skill
			}
			stewardship = {
				min_template_average_skill
				max_template_average_skill
			}
			intrigue = {
				min_template_decent_skill
				max_template_decent_skill
			}
			learning = {
				min_template_average_skill
				max_template_average_skill
			}
			prowess = {
				min_template_average_skill
				max_template_average_skill
			}
			after_creation = { add_to_list = spawned_chars_list }
		}
	}
	add_generated_associates_effect = yes
}

scripted_effect generate_martial_associates = {
	while = {
		count = 2
		create_character = {
			template = pool_repopulate_martial
			dynasty = none
			employer = root
			culture = root.culture
			faith = root.faith
			diplomacy = {
				min_template_average_skill
				max_template_average_skill
			}
			martial = {
				min_template_decent_skill
				max_template_decent_skill
			}
			stewardship = {
				min_template_average_skill
				max_template_average_skill
			}
			intrigue = {
				min_template_average_skill
				max_template_average_skill
			}
			learning = {
				min_template_average_skill
				max_template_average_skill
			}
			prowess = {
				min_template_average_skill
				max_template_average_skill
			}
			after_creation = { add_to_list = spawned_chars_list }
		}
	}
	add_generated_associates_effect = yes
}

scripted_effect add_generated_associates_effect = {
	every_in_list = {
		list = spawned_chars_list
		limit = { liege != root }
		root = { add_courtier = prev }
		# Improve their opinion of us.
		add_opinion = {
			target = root
			modifier = respect_opinion
			opinion = 100
		}
		add_opinion = {
			target = root
			modifier = recruited_me_opinion
			opinion = 50
		}
		# Add a cheeky variable for future content
		add_character_flag = {
			flag = generated_starting_character
			years = 10
		}
	}
}

### For historical and AI characters
gdp_merchant.1001 = {
	type = character_event
	hidden = yes

	trigger = {
		government_has_flag = government_is_merchant
		# Must be on land or else we don't know where to take our locals from.
		exists = location.county
	}

	on_trigger_fail = {
		if = {
			limit = { government_has_flag = government_is_merchant }
			trigger_event = {
				id = gdp_merchant.1001
				months = { 1 3 }
			}
		}
	}

	cooldown = { years = 20 }

	immediate = {
		add_character_flag = got_starting_courtiers
		generate_stewardship_associates = yes
	}
}

### For player patricians, who need courtiers to assign as traders
gdp_merchant.1004 = {
	type = character_event
	hidden = yes

	trigger = {
		government_has_flag = government_is_republic
		NOT = { has_character_flag = got_starting_courtiers }
	}

	immediate = {
		add_character_flag = got_starting_courtiers
		generate_stewardship_associates = yes
	}
}

##	If you made a custom Merchant character you should get the chance to choose your own courtiers.
gdp_merchant.1002 = {
	type = character_event
	title = gdp_merchant.1002.t
	desc = gdp_merchant.1002.desc
	window = big_event_window
	theme = merchant
	override_background = { reference = study }

	left_portrait = {
		character = root
		animation = personality_rational
	}

	cooldown = { years = 20 }

	trigger = {
		government_has_flag = government_is_merchant
		# Must be on land or else we don't know where to take our locals from.
		exists = location.county
	}

	on_trigger_fail = {
		if = {
			limit = { government_has_flag = government_is_merchant }
			trigger_event = {
				id = gdp_merchant.1002
				months = { 1 3 }
			}
		}
	}

	immediate = {
		hidden_effect = {
			random_list = {
				pick = 2 
				unique = yes
				25 = { # Martial
					modifier = {
						OR = {
							martial >= excellent_skill_level
							number_of_commander_traits >= 1 
						}
						add = 25
					}
					save_scope_as = martial_char
				}
				25 = { # Diplomacy
					modifier = {
						OR = {
							diplomacy >= excellent_skill_level
							culture = { has_cultural_tradition = tradition_caravaneers }
						}
						add = 25
					}
					save_scope_as = diplo_char
				}
				25 = { # Learning
					modifier = {
						OR = {
							learning >= excellent_skill_level
							culture = { has_cultural_tradition = tradition_artisans }
						}
						add = 25
					}
					save_scope_as = learning_char
				}
				25 = { # Intrigue
					modifier = {
						intrigue >= excellent_skill_level
						add = 25
					}
					save_scope_as = intrigue_char
				}
			}
		}
	}

	option = { # Stewardship chars always relevant for Merchants
		name = gdp_merchant.1002.a
		skill = stewardship
		custom_tooltip = gdp_merchant.1002.a.tt
		hidden_effect = { generate_stewardship_associates = yes }
	}

	option = { # Martial
		name = gdp_merchant.1002.b
		trigger = { exists = scope:martial_char }
		skill = martial
		custom_tooltip = gdp_merchant.1002.b.tt
		hidden_effect = { generate_martial_associates = yes }
	}

	option = { # Diplomacy
		name = gdp_merchant.1002.c
		trigger = { exists = scope:diplo_char }
		skill = diplomacy
		custom_tooltip = gdp_merchant.1002.c.tt
		hidden_effect = { generate_diplomacy_associates = yes }
	}

	option = { # Learning
		name = gdp_merchant.1002.d
		trigger = { exists = scope:learning_char }
		skill = learning
		custom_tooltip = gdp_merchant.1002.d.tt
		hidden_effect = { generate_learning_associates = yes }
	}

	option = { # Intrigue
		name = gdp_merchant.1002.e
		trigger = { exists = scope:intrigue_char }
		skill = intrigue
		custom_tooltip = gdp_merchant.1002.e.tt
		hidden_effect = { generate_intrigue_associates = yes }
	}

	option = { # Opt out
		name = gdp_merchant.1002.f

		if = {
			limit = {
				any_courtier = { count <= 0 }
			}
			custom_tooltip = gdp_merchant.1002.f.tt.difficulty
		}
		else = { custom_tooltip = gdp_merchant.1002.f.tt.associates }
	}
}

##	Select a starting company focus.
gdp_merchant.1003 = {
	type = character_event
	title = gdp_merchant.1003.t
	desc = gdp_merchant.1003.desc
	window = big_event_window
	theme = merchant
	override_background = market_crowd

	left_portrait = {
		character = root
		animation = holding_scales
	}
	right_portrait = {
		character = scope:merchant
		hide_info = yes
		triggered_animation = {
			trigger = {
				location ?= { geographical_region = world_europe }
			}
			animation = merchant_gesturing
			camera = camera_ruler_designer_body_side
		}
		animation = holding_yoke
	}
	lower_left_portrait = {
		character = suzerain
		trigger = {
			root = {
				can_create_task_contract = { type_name = patron_hobnob }
			}
		}
	}

	cooldown = { years = 20 }

	trigger = {
		government_has_flag = government_is_merchant
		# Must be on land or else we don't know where to take our locals from.
		exists = location.county
	}

	on_trigger_fail = {
		if = {
			limit = { government_has_flag = government_is_merchant }
			trigger_event = {
				id = gdp_merchant.1003
				months = { 1 3 }
			}
		}
	}

	immediate = {
		if = {
			limit = { is_ai = no }
			create_character = {
				template = tgp_concubine_template
				location = root.location
				gender = male
				save_scope_as = merchant
			}
		}
		hidden_effect = {
			# Generate starting characters
			switch = {
				trigger = has_trait
				education_diplomacy = {
					generate_diplomacy_associates = yes
				}
				education_martial = {
					generate_martial_associates = yes
				}
				education_stewardship = {
					generate_stewardship_associates = yes
				}
				education_intrigue = {
					generate_intrigue_associates = yes
				}
				education_learning = {
					generate_learning_associates = yes
				}
			}
		}
		# The route search is expensive, so the AI only runs it when it picks option c
		if = {
			limit = { is_ai = no }
			every_top_trade_good_in_range = {
				selection_count = 1
				trade_good_need_category ?= {
					save_scope_as = top_trade_category
				}
			}
		}
	}

	option = { # Capacity
		name = gdp_merchant.1003.a
		add_character_modifier = {
			modifier = gdp_merchant_starting_capacity_modifier
			years = 10
		}
		ai_chance = {
			base = 0
		}
	}

	option = { # Mingle with Patron
		name = gdp_merchant.1003.b
		trigger = {
			can_create_task_contract = { type_name = patron_hobnob }
		}
		highlight_portrait = suzerain
		custom_tooltip = gdp_merchant.1003.b.tt
		hidden_effect = {
			trigger_event = { on_action = gdp_patron_hobnob }
		}
		ai_chance = {
			base = 0
		}
	}

	option = { # Profit
		name = gdp_merchant.1003.c
		trigger = {
			OR = {
				is_ai = yes
				exists = scope:top_trade_category
			}
		}
		if = {
			limit = { is_ai = yes }
			every_top_trade_good_in_range = {
				selection_count = 1
				trade_good_need_category ?= {
					save_scope_as = top_trade_category
				}
			}
		}
		if = {
			limit = { exists = scope:top_trade_category }
			primary_title = {
				add_trade_good_category_xp = {
					target = scope:top_trade_category
					value = trade_expertise_xp_massive_value
				}
			}
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_greed = medium_chance_impact_positive_ai_value
			}
			modifier = {
				has_trait = greedy
				add = 50
			}
			modifier = {
				has_trait = ambitious
				add = 25
			}
		}
	}
}

scripted_trigger gdp_merchant_1100_family_trigger = {
	OR = {
		house ?= root.house
		is_spouse_of = root
		is_close_family_of = root
	}
}

scripted_trigger gdp_merchant_1100_crowding_associate_trigger = {
	is_available_quick = {
		adult = yes
		imprisoned = no
		incapable = no
	}
	trigger_if = {
		limit = { exists = house }
		NOT = { house = root.house }
	}
	save_temporary_scope_as = crowding_associate
	NOR = {
		has_any_court_position = yes
		is_councillor_of = root
		is_knight_of = root
		has_variable = associate_request_type
		has_opinion_modifier = {
			target = root
			modifier = recruited_me_opinion
		}
		has_relation_friend = root
		has_relation_lover = root
		is_spouse_of = root
		is_concubine_of = root
		is_close_family_of = root
		any_spouse = {
			OR = {
				house ?= root.house
				is_close_or_extended_family_of = root
				is_great_grandparent_of = root
				is_great_grandchild_of = root
				any_sibling = { is_spouse_of = root }
			}
		}
		any_former_spouse = {
			even_if_dead = yes
			OR = {
				house ?= root.house
				is_close_or_extended_family_of = root
				is_great_grandparent_of = root
				is_great_grandchild_of = root
				any_sibling = { is_spouse_of = root }
			}
		}
		root = {
			any_spouse = {
				OR = {
					is_close_or_extended_family_of = scope:crowding_associate
					is_great_grandparent_of = scope:crowding_associate
					is_great_grandchild_of = scope:crowding_associate
				}
			}
			any_former_spouse = {
				even_if_dead = yes
				OR = {
					is_close_or_extended_family_of = scope:crowding_associate
					is_great_grandparent_of = scope:crowding_associate
					is_great_grandchild_of = scope:crowding_associate
				}
			}
		}
	}
}

scripted_effect gdp_merchant_1100_crowding_duel_effect = {
	$WINNER$ = {
		custom_tooltip = {
			text = gdp_merchant.1100.b.tt
			merchant_adopt_effect = {
				CHILD = $WINNER$
				ADOPTER = root
			}
			every_in_list = {
				list = associates
				limit = {
					NOT = { this = $WINNER$ }
				}
				add_opinion = {
					target = root
					modifier = upset_opinion
					opinion = -25
				}
			}
		}
	}
}

# Overcrowded
gdp_merchant.1100 = {
	type = character_event
	title = gdp_merchant.1100.t
	desc = gdp_merchant.1100.desc
	left_portrait = {
		character = root
		animation = personality_rational
	}
	right_portrait = {
		character = scope:plaintiff
		animation = interested
	}
	lower_left_portrait = scope:associate_2
	lower_center_portrait = scope:associate_1
	theme = trade
	override_background = { reference = study }
	cooldown = { years = 5 }

	trigger = {
		government_has_flag = government_is_merchant
		is_available_quick = {
			adult = yes
			activity = no
			travel = no
			imprisoned = no
			incapable = no
		}
		domicile ?= { is_domicile_type = merchant_company }
		any_courtier = {
			is_available_quick = {
				adult = yes
				imprisoned = no
				incapable = no
			}
			gdp_merchant_1100_family_trigger = yes
			save_temporary_scope_as = plaintiff_temp
		}
		any_courtier = {
			# Outsiders must outnumber adult family, and there must be at least 8 of them
			count >= {
				value = 1
				root = {
					every_courtier = {
						limit = {
							is_adult = yes
							gdp_merchant_1100_family_trigger = yes
						}
						add = 1
					}
				}
				min = 8
			}
			NOT = { this = scope:plaintiff_temp }
			gdp_merchant_1100_crowding_associate_trigger = yes
		}
	}

	immediate = {
		random_courtier = {
			limit = {
				is_available_quick = {
					adult = yes
					imprisoned = no
					incapable = no
				}
				gdp_merchant_1100_family_trigger = yes
			}
			weight = {
				base = 25
				modifier = {
					add = 200
					is_of_major_interest_trigger = { CHARACTER = root }
				}
			}
			save_scope_as = plaintiff
		}
		hidden_effect = {
			while = {
				count = 2
				random_courtier = {
					limit = {
						NOR = {
							scope:plaintiff ?= this
							scope:associate_1 ?= this
						}
						is_trader = no
						gdp_merchant_1100_crowding_associate_trigger = yes
					}
					alternative_limit = {
						NOR = {
							scope:plaintiff ?= this
							scope:associate_1 ?= this
						}
						gdp_merchant_1100_crowding_associate_trigger = yes
					}
					weight = {
						base = 50
						modifier = {
							add = {
								value = sum_of_all_skills_value
								multiply = -1
							}
						}
					}
					if = {
						limit = { exists = scope:associate_1 }
						save_scope_as = associate_2
					}
					else = { save_scope_as = associate_1 }
				}
			}
		}
	}

	option = {
		name = gdp_merchant.1100.a
		scope:associate_2 = {
			move_to_pool = yes
			add_opinion = {
				target = root
				modifier = insulted_opinion
				opinion = -25
			}
		}
		house = {
			add_unity_value = {
				value = medium_unity_gain
				character = root
				desc = merchant_overcrowding_fired_gain
			}
		}
		ai_chance = {
			base = 20
			modifier = {
				house.house_unity_value < 100
				add = 20
			}
			modifier = { add = scope:associate_1.sum_of_all_skills_value }
			opinion_modifier = {
				opinion_target = scope:associate_1
				multiplier = 0.2
			}
		}
	}

	option = {
		name = gdp_merchant.1100.b
		scope:associate_1 = {
			move_to_pool = yes
			add_opinion = {
				target = root
				modifier = insulted_opinion
				opinion = -25
			}
		}
		house = {
			add_unity_value = {
				value = medium_unity_gain
				character = root
				desc = merchant_overcrowding_fired_gain
			}
		}
		ai_chance = {
			base = 20
			modifier = {
				house.house_unity_value < 50
				add = 20
			}
			modifier = { add = scope:associate_2.sum_of_all_skills_value }
			opinion_modifier = {
				opinion_target = scope:associate_2
				multiplier = 0.2
			}
		}
	}

	option = {
		name = gdp_merchant.1100.c
		scope:associate_2 = {
			move_to_pool = yes
			add_opinion = {
				target = root
				modifier = insulted_opinion
				opinion = -25
			}
		}
		scope:associate_1 = {
			move_to_pool = yes
			add_opinion = {
				target = root
				modifier = insulted_opinion
				opinion = -25
			}
		}
		house = {
			add_unity_value = {
				value = massive_unity_gain
				character = root
				desc = merchant_overcrowding_yeet_gain
			}
		}
		stress_and_fulfillment_impact = {
			humble = minor_stress_impact_gain
		}
		ai_chance = {
			base = 20
			modifier = {
				house.house_unity_value < 50
				add = 20
			}
			opinion_modifier = {
				opinion_target = scope:associate_1
				multiplier = -0.2
			}
			opinion_modifier = {
				opinion_target = scope:associate_2
				multiplier = -0.2
			}
		}
	}

	option = {
		name = gdp_merchant.1100.d
		house = {
			add_unity_value = {
				value = massive_unity_loss
				character = root
				desc = merchant_overcrowding_ignored_loss
			}
		}
		stress_and_fulfillment_impact = {
			arrogant = minor_stress_impact_gain
		}
		ai_chance = {
			base = 20
			modifier = {
				house.house_unity_value > 150
				add = 20
			}
			opinion_modifier = {
				opinion_target = scope:associate_1
				multiplier = 0.2
			}
			opinion_modifier = {
				opinion_target = scope:associate_2
				multiplier = 0.2
			}
		}
	}
}

scripted_trigger gdp_merchant_1200_requester_trigger = {
	is_available_quick = {
		adult = yes
		imprisoned = no
		incapable = no
	}
	age < 61
	health > poor_health
	NOR = {
		has_character_flag = associate_request_cooldown
		has_variable = associate_request_type
		has_opinion_modifier = {
			target = root
			modifier = recruited_me_opinion
		}
	}
}

scripted_trigger gdp_merchant_1200_close_requester_trigger = {
	OR = {
		has_relation_friend = root
		house ?= root.house
		is_spouse_of = root
		is_close_family_of = root
		any_spouse = {
			OR = {
				house ?= root.house
				is_close_or_extended_family_of = root
			}
		}
		any_close_or_extended_family_member = { is_spouse_of = root }
	}
}

scripted_trigger gdp_merchant_1200_valid_requester_trigger = {
	gdp_merchant_1200_requester_trigger = yes
	trigger_if = {
		limit = { gdp_merchant_1200_close_requester_trigger = no }
		root = {
			NOT = { has_character_flag = gdp_merchant_1200_distant_request_cooldown }
		}
	}
}

# Associate Requests
gdp_merchant.1200 = {
	type = character_event
	title = gdp_merchant.1200.t
	desc = {
		desc = gdp_merchant.1200.desc
		first_valid = {
			triggered_desc = {
				trigger = { scope:associate_request_type = flag:luxury_contract }
				desc = gdp_merchant.1200.desc.luxury_contract
			}
			triggered_desc = {
				trigger = { scope:associate_request_type = flag:activity }
				desc = gdp_merchant.1200.desc.activity
			}
			desc = gdp_merchant.1200.desc.artifact
		}
	}

	left_portrait = {
		character = root
		animation = thinking
	}
	right_portrait = {
		character = scope:requester
		animation = interested
	}
	theme = trade
	override_background = { reference = study }
	cooldown = { years = 5 }

	trigger = {
		is_available_quick = {
			ai = no
			adult = yes
			activity = no
			travel = no
			imprisoned = no
			incapable = no
		}
		government_has_flag = government_is_merchant
		domicile ?= { is_domicile_type = merchant_company }
		num_taken_task_contracts < 1
		any_courtier = { gdp_merchant_1200_valid_requester_trigger = yes }
	}

	immediate = {
		random_courtier = {
			limit = { gdp_merchant_1200_valid_requester_trigger = yes }
			weight = {
				base = 25
				modifier = {
					add = 50
					is_of_major_interest_trigger = { CHARACTER = root }
				}
				modifier = {
					add = 25
					is_of_minor_interest_trigger = { CHARACTER = root }
				}
			}
			save_scope_as = requester
			add_character_flag = {
				flag = associate_request_cooldown
				years = 10
			}
			if = {
				limit = { gdp_merchant_1200_close_requester_trigger = no }
				root = {
					add_character_flag = {
						flag = gdp_merchant_1200_distant_request_cooldown
						years = 10
					}
				}
			}
		}
		# Type
		random_list = {
			10 = {
				trigger = {
					any_character_artifact = {
						is_equipped = no
						artifact_can_be_gift_to_trigger = { RECIPIENT = scope:requester }
					}
				}
				modifier = {
					add = -9
					OR = {
						has_character_flag = associate_request_recent_artifact
						any_in_list = {
							variable = associate_requests
							var:associate_request_type ?= flag:artifact
						}
					}
				}
				save_scope_value_as = {
					name = associate_request_type
					value = flag:artifact
				}
			}
			10 = {
				trigger = {
					NOT = { is_activity_type_on_cooldown = activity_feast }
				}
				modifier = {
					add = -9
					OR = {
						has_character_flag = associate_request_recent_activity
						any_in_list = {
							variable = associate_requests
							var:associate_request_type ?= flag:activity
						}
					}
				}
				save_scope_value_as = {
					name = associate_request_type
					value = flag:activity
				}
			}
			10 = {
				trigger = {
					OR = {
						any_available_task_contract = { has_task_contract_group = merchant_luxury_contract_group }
						# Fallback so the list is never empty
						is_activity_type_on_cooldown = activity_feast
					}
				}
				modifier = {
					add = -9
					OR = {
						has_character_flag = associate_request_recent_luxury_contract
						any_in_list = {
							variable = associate_requests
							var:associate_request_type ?= flag:luxury_contract
						}
					}
				}
				modifier = {
					any_available_task_contract = { has_task_contract_group = merchant_luxury_contract_group }
					factor = 2
				}
				save_scope_value_as = {
					name = associate_request_type
					value = flag:luxury_contract
				}
			}
		}
		switch = {
			trigger = scope:associate_request_type
			flag:artifact = {
				add_character_flag = {
					flag = associate_request_recent_artifact
					years = 10
				}
			}
			flag:activity = {
				add_character_flag = {
					flag = associate_request_recent_activity
					years = 10
				}
			}
			flag:luxury_contract = {
				add_character_flag = {
					flag = associate_request_recent_luxury_contract
					years = 10
				}
			}
		}
	}

	option = {
		name = gdp_merchant.1200.a
		switch = {
			trigger = scope:associate_request_type
			flag:artifact = {
				custom_description_no_bullet = {
					text = gdp_merchant_1200_a_tt_artifact
					object = scope:requester
				}
			}
			flag:activity = {
				custom_description_no_bullet = { text = gdp_merchant_1200_a_tt_activity }
			}
			flag:luxury_contract = {
				custom_description_no_bullet = { text = gdp_merchant_1200_a_tt_luxury_contract }
			}
		}
		show_as_tooltip = {
			reverse_add_opinion = {
				target = scope:requester
				modifier = pleased_opinion
				opinion = 20
			}
			house = {
				add_unity_value = {
					value = major_unity_gain
					character = root
					desc = merchant_associate_request_fulfill_gain
				}
			}
		}
		custom_tooltip = action_associate_request_help
		add_to_variable_list = {
			name = associate_requests
			target = scope:requester
			years = 5
		}
		scope:requester = {
			set_variable = {
				name = associate_request_type
				value = scope:associate_request_type
			}
			set_variable = {
				name = associate_request_merchant
				value = root
				years = 6
			}
			trigger_event = {
				id = gdp_merchant.1201
				years = 5
			}
		}
	}

	option = {
		name = gdp_merchant.1200.b
		reverse_add_opinion = {
			target = scope:requester
			modifier = disappointed_opinion
			opinion = -20
		}
		house = {
			add_unity_value = {
				value = major_unity_loss
				character = root
				desc = merchant_associate_request_reject_loss
			}
		}
	}
}

gdp_merchant.1201 = {
	type = character_event
	hidden = yes

	trigger = {
		is_ruler = no
		exists = var:associate_request_type
		var:associate_request_merchant ?= employer
	}

	immediate = {
		save_scope_as = associate
		employer = {
			save_scope_as = request_merchant
			send_interface_toast = {
				type = event_toast_effect_bad
				title = associate_request_failed_title
				right_icon = root
				switch = {
					trigger = var:associate_request_type
					flag:artifact = { custom_tooltip = associate_request_type_artifact_fail }
					flag:activity = { custom_tooltip = associate_request_type_activity_fail }
					flag:luxury_contract = { custom_tooltip = associate_request_type_luxury_contract_fail }
					flag:silk_road_market = { custom_tooltip = associate_request_type_silk_road_market_fail }
				}
				house ?= {
					add_unity_value = {
						value = major_unity_loss
						character = scope:request_merchant
						desc = merchant_associate_request_fail_loss
					}
				}
			}
		}
		if = {
			limit = { var:associate_request_type ?= flag:silk_road_market }
			remove_variable = associate_request_type
			remove_variable = associate_request_target_market
			remove_variable = associate_request_target_location
		}
		remove_variable = associate_request_merchant
	}
}

###############################
# MONOPOLY AND CHARTER EVENTS #
###############################

scripted_trigger gdp_merchant_starving_monopoly_trigger = {
	monopoly_type = exploitative
	is_trade_monopoly_for_trade_good_category = trade_category_$CATEGORY$
	monopoly_trade_market = {
		any_trade_good_with_need = { has_category = trade_good_category:trade_category_$CATEGORY$ }
		NOT = { any_trade_good_with_surplus = { has_category = trade_good_category:trade_category_$CATEGORY$ } }
	}
}

scripted_trigger gdp_merchant_starving_food_monopoly_trigger = {
	OR = {
		gdp_merchant_starving_monopoly_trigger = { CATEGORY = cereals }
		gdp_merchant_starving_monopoly_trigger = { CATEGORY = meat }
	}
}

# Bare Market - the people starve under an exploitative food monopoly
gdp_merchant.2000 = {
	type = character_event
	title = gdp_merchant.2000.t
	desc = {
		desc = gdp_merchant.2000.desc.intro
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:starving_county ?= {
						any_county_situation = {
							tgp_natural_disaster_situation_is_disaster_trigger = yes
							NOT = { situation_current_phase = recovery }
						}
					}
				}
				desc = gdp_merchant.2000.desc.disaster
			}
			triggered_desc = {
				trigger = { scope:local_ruler ?= { is_at_war = yes } }
				desc = gdp_merchant.2000.desc.war
			}
			triggered_desc = {
				trigger = { scope:starving_province ?= { any_province_epidemic = { always = yes } } }
				desc = gdp_merchant.2000.desc.plague
			}
			triggered_desc = {
				trigger = { scope:starving_county ?= { county_control < medium_county_control } }
				desc = gdp_merchant.2000.desc.unrest
			}
		}
		desc = gdp_merchant.2000.desc.stalls
	}
	theme = trade
	left_portrait = {
		character = scope:starving_citizen
		animation = beg
		camera = camera_event_left_away_3_4_small
	}
	right_portrait = {
		character = root
		animation = pondering
	}
	override_background = { reference = market }
	override_effect_2d = {
		trigger = {
			scope:starving_county ?= {
				any_county_situation = {
					situation_type = natural_disaster_flood
					NOT = { situation_current_phase = recovery }
				}
			}
		}
		reference = rain
	}
	override_effect_2d = {
		trigger = {
			scope:starving_county ?= {
				any_county_situation = {
					situation_type = natural_disaster_earthquake
					NOT = { situation_current_phase = recovery }
				}
			}
		}
		reference = smoke
	}
	override_effect_2d = {
		reference = flies
	}
	cooldown = { years = 10 }

	trigger = {
		is_house_head = yes
		is_available = yes
		government_has_flag = government_is_merchant
		any_trade_monopoly_for_character = {
			gdp_merchant_starving_food_monopoly_trigger = yes
		}
	}

	weight_multiplier = {
		base = 1
		modifier = { # war, disaster, or plague is already battering the market
			factor = 2
			any_trade_monopoly_for_character = {
				gdp_merchant_starving_food_monopoly_trigger = yes
				monopoly_trade_market = { upheaval_market_modifier_trigger = yes }
			}
		}
		modifier = { # the local ruler has lost his grip
			factor = 1.5
			any_trade_monopoly_for_character = {
				gdp_merchant_starving_food_monopoly_trigger = yes
				monopoly_trade_market = {
					trade_market_owner.county ?= {
						county_control < medium_county_control
					}
				}
			}
		}
	}

	immediate = {
		random_trade_monopoly_for_character = {
			limit = { gdp_merchant_starving_food_monopoly_trigger = yes }
			save_scope_as = starving_monopoly
			monopoly_trade_market = {
				save_scope_as = starving_market
				random_trade_good_with_need = {
					limit = {
						OR = {
							has_category = trade_good_category:trade_category_cereals
							has_category = trade_good_category:trade_category_meat
						}
					}
					save_scope_as = needed_good
				}
				trade_market_owner.title_province = {
					save_scope_as = starving_province
					county ?= {
						save_scope_as = starving_county
						holder ?= { save_scope_as = local_ruler }
					}
				}
			}
		}
		random_pool_character = {
			province = scope:starving_province
			limit = {
				is_available_quick = {
					adult = yes
					imprisoned = no
					incapable = no
					in_army = no
				}
			}
			alternative_limit = { always = yes }
			save_scope_as = starving_citizen
		}
		if = {
			limit = {
				exists = scope:starving_province
				NOT = { exists = scope:starving_citizen }
			}
			create_character = {
				template = generic_peasant_character
				location = scope:starving_province
				culture = scope:starving_province.culture
				faith = scope:starving_province.faith
				save_scope_as = starving_citizen
			}
		}
	}
	# Hoard what little is left
	option = { 
		name = gdp_merchant.2000.a
		add_character_modifier = {
			modifier = gdp_merchant_full_larder_modifier
			years = 5
		}
		if = {
			limit = {
				exists = scope:starving_county
				trigger_if = {
					limit = { exists = scope:local_ruler }
				}
			}
			scope:starving_county = { change_county_control = -15 }
		}
		stress_and_fulfillment_impact = {
			compassionate = medium_stress_impact_gain
			generous = minor_stress_impact_gain
			greedy = medium_stress_impact_loss
		}
		ai_chance = {
			base = 100
			modifier = {
				factor = 0
				OR = {
					has_trait = compassionate
					has_trait = generous
				}
			}
			modifier = {
				factor = 2
				has_trait = greedy
			}
		}
	}
	# The people will look for someone to blame
	option = { 
		name = gdp_merchant.2000.b
		add_character_modifier = {
			modifier = gdp_merchant_staying_vigilant
			years = 10
		}
		add_intrigue_lifestyle_xp = medium_lifestyle_xp
		stress_and_fulfillment_impact = {
			paranoid = minor_stress_impact_gain
			craven = minor_stress_impact_loss
		}
		ai_chance = {
			base = 100
			modifier = {
				factor = 2
				OR = {
					has_trait = paranoid
					has_trait = craven
				}
			}
		}
	}

	option = { # This is not my problem
		name = gdp_merchant.2000.c
		add_dread = medium_dread_gain
		stress_and_fulfillment_impact = {
			compassionate = medium_stress_impact_gain
			callous = minor_stress_impact_loss
			arbitrary = minor_stress_impact_loss
		}
		ai_chance = {
			base = 100
			modifier = {
				factor = 0
				has_trait = compassionate
			}
		}
	}
}

scripted_trigger gdp_merchant_craft_monopoly_trigger = {
	monopoly_type = normal
	is_complete_monopoly = no
	is_trade_monopoly_for_trade_good_category = trade_category_$CATEGORY$
}

scripted_trigger gdp_merchant_craft_category_monopoly_trigger = {
	OR = {
		gdp_merchant_craft_monopoly_trigger = { CATEGORY = base_metals }
		gdp_merchant_craft_monopoly_trigger = { CATEGORY = precious_metals }
		gdp_merchant_craft_monopoly_trigger = { CATEGORY = carpentry_supplies }
		gdp_merchant_craft_monopoly_trigger = { CATEGORY = clothes }
		gdp_merchant_craft_monopoly_trigger = { CATEGORY = luxury_items }
	}
}

# Monopoly: The Craftsman - a local expert in one good you hold monopoly on
gdp_merchant.2010 = {
	type = character_event
	title = {
		desc = gdp_merchant.2010.t
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:craft_monopoly ?= { gdp_merchant_craft_monopoly_trigger = { CATEGORY = base_metals } }
				}
				desc = gdp_merchant.2010.t.smith
			}
			triggered_desc = {
				trigger = {
					scope:craft_monopoly ?= { gdp_merchant_craft_monopoly_trigger = { CATEGORY = precious_metals } }
				}
				desc = gdp_merchant.2010.t.goldsmith
			}
			triggered_desc = {
				trigger = {
					scope:craft_monopoly ?= { gdp_merchant_craft_monopoly_trigger = { CATEGORY = carpentry_supplies } }
				}
				desc = gdp_merchant.2010.t.carpenter
			}
			triggered_desc = {
				trigger = {
					scope:craft_monopoly ?= { gdp_merchant_craft_monopoly_trigger = { CATEGORY = clothes } }
				}
				desc = gdp_merchant.2010.t.tanner
			}
			desc = gdp_merchant.2010.t.potter
		}
	}
	desc = {
		desc = gdp_merchant.2010.desc.intro
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:craft_monopoly ?= { gdp_merchant_craft_monopoly_trigger = { CATEGORY = base_metals } }
				}
				desc = gdp_merchant.2010.desc.weapon
			}
			triggered_desc = {
				trigger = {
					scope:craft_monopoly ?= { gdp_merchant_craft_monopoly_trigger = { CATEGORY = precious_metals } }
				}
				desc = gdp_merchant.2010.desc.chalice
			}
			triggered_desc = {
				trigger = {
					scope:craft_monopoly ?= { gdp_merchant_craft_monopoly_trigger = { CATEGORY = carpentry_supplies } }
				}
				desc = gdp_merchant.2010.desc.box
			}
			triggered_desc = {
				trigger = {
					scope:craft_monopoly ?= { gdp_merchant_craft_monopoly_trigger = { CATEGORY = clothes } }
				}
				desc = gdp_merchant.2010.desc.saddle
			}
			desc = gdp_merchant.2010.desc.vase
		}
		desc = gdp_merchant.2010.desc.craftsmanship
	}
	theme = trade
	left_portrait = {
		character = root
		triggered_animation = {
			trigger = {
				scope:craft_monopoly ?= { gdp_merchant_craft_monopoly_trigger = { CATEGORY = base_metals } }
			}
			animation = admiration
			camera = camera_event_center_pointing_right
		}
		triggered_animation = {
			trigger = {
				scope:craft_monopoly ?= { gdp_merchant_craft_monopoly_trigger = { CATEGORY = precious_metals } }
			}
			animation = toast_goblet
			camera = camera_event_left
		}
		triggered_animation = {
			trigger = {
				scope:craft_monopoly ?= { gdp_merchant_craft_monopoly_trigger = { CATEGORY = clothes } }
			}
			animation = admiration
			camera = camera_event_center_pointing_right
		}
		triggered_animation = {
			trigger = {
				scope:craft_monopoly ?= { gdp_merchant_craft_monopoly_trigger = { CATEGORY = carpentry_supplies } }
			}
			animation = admiration
			camera = camera_event_right_wedding
		}
		animation = happiness
		camera = camera_event_left
	}
	right_portrait = {
		character = scope:craftsman
		triggered_animation = {
			trigger = {
				scope:craft_monopoly ?= { gdp_merchant_craft_monopoly_trigger = { CATEGORY = base_metals } }
			}
			animation = inspect_weapon
		}
		triggered_animation = {
			trigger = {
				scope:craft_monopoly ?= { gdp_merchant_craft_monopoly_trigger = { CATEGORY = precious_metals } }
			}
			animation = crowning
			camera = camera_event_standing_with_horse_right
		}
		triggered_animation = {
			trigger = {
				scope:craft_monopoly ?= { gdp_merchant_craft_monopoly_trigger = { CATEGORY = carpentry_supplies } }
			}
			animation = merchant_box
			camera = camera_event_right_pointing_right
		}
		triggered_animation = {
			trigger = {
				scope:craft_monopoly ?= { gdp_merchant_craft_monopoly_trigger = { CATEGORY = clothes } }
			}
			animation = standing_horse
			camera = camera_event_center_pointing_very_left
		}
		animation = serving_tray
	}
	lower_center_portrait = {
		character = scope:associate
	}
	override_background = {
		trigger = {
			scope:craft_monopoly ?= {
				OR = {
					gdp_merchant_craft_monopoly_trigger = { CATEGORY = base_metals }
					gdp_merchant_craft_monopoly_trigger = { CATEGORY = precious_metals }
				}
			}
			location = {
				geographical_region = world_europe
			}
		}
		reference = gdp_smithy_workshop
	}
	override_background = {
		trigger = {
			scope:craft_monopoly ?= {
				OR = {
					gdp_merchant_craft_monopoly_trigger = { CATEGORY = base_metals }
					gdp_merchant_craft_monopoly_trigger = { CATEGORY = precious_metals }
				}
			}
		}
		reference = gdp_artisan_street_crowd
	}
	override_background = {
		trigger = {
			scope:craft_monopoly ?= { gdp_merchant_craft_monopoly_trigger = { CATEGORY = carpentry_supplies } }
		}
		reference = gdp_carpentry_workshop
	}
	override_background = {
		trigger = {
			scope:craft_monopoly ?= { gdp_merchant_craft_monopoly_trigger = { CATEGORY = clothes } }
		}
		reference = gdp_tannery
	}
	override_background = { reference = gdp_artisan_street_crowd }

	trigger = {
		is_house_head = yes
		is_available = yes
		government_has_flag = government_is_merchant
		primary_title ?= { NOT = { has_variable = had_event_gdp_merchant_2010 } }
		any_trade_monopoly_for_character = {
			gdp_merchant_craft_category_monopoly_trigger = yes
		}
		OR = {
			any_close_family_member = { is_available_ai_adult = yes }
			any_courtier = { is_available_ai_adult = yes }
		}
	}

	immediate = {
		primary_title ?= { set_variable = had_event_gdp_merchant_2010 }
		random_trade_monopoly_for_character = {
			limit = { gdp_merchant_craft_category_monopoly_trigger = yes }
			save_scope_as = craft_monopoly
			monopoly_trade_market = {
				save_scope_as = craft_market
				trade_market_owner.title_province = { save_scope_as = workshop_province }
			}
		}
		create_character = {
			template = gdp_master_craftsman_template
			location = scope:workshop_province
			culture = scope:workshop_province.culture
			faith = scope:workshop_province.faith
			trait = shy
			save_scope_as = craftsman
		}
		# Prioritize family
		random_close_family_member = {
			limit = {
				is_available_ai_adult = yes
				is_courtier_of = root
			}
			alternative_limit = { is_available_ai_adult = yes }
			save_scope_as = associate
		}
		if = {
			limit = { NOT = { exists = scope:associate } }
			random_courtier = {
				limit = { is_available_ai_adult = yes }
				save_scope_as = associate
			}
		}
	}

	# Such hands belong in my court
	option = {
		name = gdp_merchant.2010.a
		trigger = { short_term_gold >= major_gold_value }
		show_as_unavailable = { always = yes }
		pay_short_term_gold = {
			target = scope:craftsman
			gold = major_gold_value
		}
		add_courtier = scope:craftsman
		reverse_add_opinion = {
			target = scope:craftsman
			modifier = recruited_me_opinion
			opinion = 50
		}
		stress_and_fulfillment_impact = {
			ambitious = minor_stress_impact_loss
		}
		ai_chance = {
			base = 100
			modifier = {
				factor = 1.5
				has_trait = ambitious
			}
		}
	}

	# Perhaps you could teach my associate a thing or two
	option = {
		name = gdp_merchant.2010.b
		scope:associate = {
			add_character_modifier = {
				modifier = gdp_merchant_master_craft_tutelage_modifier
				years = 10
			}
			if = {
				limit = {
					NOT = { has_trait = lifestyle_merchant }
				}
				add_trait = lifestyle_merchant
			}
			else = {
				add_trait_xp = {
					trait = lifestyle_merchant
					track = artisan
					value = lifestyle_merchant_xp_gain_medium_value
				}
			}
		}
		reverse_add_opinion = {
			target = scope:associate
			modifier = grateful_opinion
			opinion = 15
		}
		stress_and_fulfillment_impact = {
			generous = minor_stress_impact_loss
		}
		ai_chance = {
			base = 100
			modifier = {
				factor = 1.5
				has_trait = generous
			}
		}
	}

	# I should take a moment to learn from the craftsman
	option = {
		name = gdp_merchant.2010.c
		add_trait_xp = {
			trait = lifestyle_merchant
			track = artisan
			value = lifestyle_merchant_xp_gain_medium_value
		}
		stress_and_fulfillment_impact = {
			diligent = minor_stress_impact_loss
			lazy = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			modifier = {
				factor = 2
				has_trait = diligent
			}
		}
	}

	after = {
		scope:craftsman = { silent_disappear_ai_if_not_hired = yes }
	}
}

scripted_trigger gdp_merchant_category_expertise_trigger = {
	has_trade_good_category_xp_track = yes
	save_temporary_scope_as = category_temp
	root.primary_title = {
		trade_good_category_xp = {
			target = scope:category_temp
			value >= 300
		}
	}
}

# The Price of X - your Patron asks your advice about a trade good
gdp_merchant.2015 = {
	type = character_event
	title = gdp_merchant.2015.t
	desc = gdp_merchant.2015.desc
	theme = merchant
	override_background = throne_room

	left_portrait = {
		character = scope:patron
		animation = pondering
		camera = camera_event_very_left
	}
	right_portrait = {
		character = root
		animation = obsequious_bow
		camera = camera_event_right_to_the_right
	}

	cooldown = { years = 10 }

	trigger = {
		is_house_head = yes
		is_available = yes
		government_has_flag = government_is_merchant
		character_has_trade_monopoly = no
		exists = suzerain
		short_term_gold >= {
			value = suzerain.major_gold_value
			multiply = {
				value = suzerain.highest_held_title_tier
				add = -1
			}
			multiply = 0.5
		}
		suzerain = {
			is_available_quick = { adult = yes }
			any_held_trade_market = {
				any_trade_route_in_market = {
					trade_route_owner = { this = root }
					any_traded_good = {
						any_trade_category = {
							gdp_merchant_category_expertise_trigger = yes
						}
					}
				}
			}
		}
	}

	immediate = {
		suzerain = {
			save_scope_as = patron
			get_quirk_character_effect = yes
			random_held_trade_market = {
				limit = {
					any_trade_route_in_market = {
						trade_route_owner = { this = root }
						any_traded_good = {
							any_trade_category = {
								gdp_merchant_category_expertise_trigger = yes
							}
						}
					}
				}
				save_scope_as = patron_market
			}
		}
		scope:patron_market = {
			random_trade_route_in_market = {
				limit = {
					trade_route_owner = { this = root }
					any_traded_good = {
						any_trade_category = {
							gdp_merchant_category_expertise_trigger = yes
						}
					}
				}
				random_traded_good = {
					limit = {
						any_trade_category = {
							gdp_merchant_category_expertise_trigger = yes
						}
					}
					save_scope_as = counsel_good
				}
			}
		}
		scope:counsel_good = {
			random_trade_category = {
				limit = {
					gdp_merchant_category_expertise_trigger = yes
				}
				save_scope_as = counsel_category
			}
		}
		save_scope_value_as = {
			name = monopoly_opinion_acceptance
			value = {
				value = 20
				multiply = 0.5
				add = 25
			}
		}
		save_scope_value_as = {
			name = monopoly_acceptance
			value = {
				value = 10
				multiply = 0.5
				add = 5
				add = {
					value = "root.primary_title.trade_good_category_xp(scope:counsel_category)"
					add = 300
					divide = trade_expertise_xp_five_levels_value
					floor = yes
					subtract = {
						value = "root.primary_title.trade_good_category_xp(scope:counsel_category)"
						divide = trade_expertise_xp_five_levels_value
						floor = yes
					}
					multiply = 2
				}
				if = {
					limit = { "root.primary_title.trade_good_category_level(scope:counsel_category)" < merchant_monopoly_trade_expertise_level_low_value }
					subtract = merchant_monopoly_trade_expertise_bonus_negative_value
				}
				if = {
					limit = {
						"root.primary_title.trade_good_category_xp(scope:counsel_category)" < {
							value = trade_expertise_xp_five_levels_value
							divide = 5
							multiply = merchant_monopoly_trade_expertise_level_low_value
							subtract = 300
						}
					}
					add = merchant_monopoly_trade_expertise_bonus_negative_value
				}
			}
		}
	}

	# I happen to be the expert on this very trade good
	option = {
		name = gdp_merchant.2015.a
		trigger = {
			custom_tooltip = {
				text = gdp_merchant.2015.a.tt
				primary_title = {
					trade_good_category_level = {
						target = scope:counsel_category
						value >= merchant_monopoly_trade_expertise_level_medium_value
					}
				}
			}
		}
		show_as_unavailable = { always = yes }
		reverse_add_opinion = {
			target = scope:patron
			modifier = impressed_opinion
			opinion = 20
		}
		custom_tooltip = gdp_merchant.2015.a.monopoly.tt
		set_variable = {
			name = showed_expertise_patron
			value = scope:patron
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 0.5
				ai_greed = 0.5
			}
		}
	}

	# Pretend to know what you are talking about 
	option = {
		name = gdp_merchant.2015.b
		duel = {
			skill = intrigue
			target = scope:patron
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				min = 5
				desc = gdp_merchant.2015.b.success
				hidden_effect = {
					primary_title = {
						add_trade_good_category_xp = {
							target = scope:counsel_category
							value = 300
						}
					}
				}
				send_interface_toast = {
					type = event_toast_effect_good
					title = gdp_merchant.2015.b.success
					right_icon = scope:patron
					show_as_tooltip = {
						primary_title = {
							add_trade_good_category_xp = {
								target = scope:counsel_category
								value = 300
							}
						}
					}
					custom_tooltip = gdp_merchant.2015.b.monopoly.tt
					set_variable = {
						name = showed_some_expertise_patron
						value = scope:patron
					}
					reverse_add_opinion = {
						target = scope:patron
						modifier = impressed_opinion
						opinion = 10
					}
				}
			}
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				min = 5
				desc = gdp_merchant.2015.b.failure
				send_interface_toast = {
					type = event_toast_effect_bad
					title = gdp_merchant.2015.b.failure
					right_icon = scope:patron
					reverse_add_opinion = {
						target = scope:patron
						modifier = disappointed_opinion
						opinion = -15
					}
				}
			}
		}
		stress_impact = {
			honest = minor_stress_impact_gain
		}
		ai_chance = {
			base = 50
			modifier = {
				factor = 2
				has_trait = deceitful
			}
			modifier = {
				factor = 0.25
				has_trait = honest
			}
			ai_value_modifier = {
				ai_boldness = 0.5
				ai_honor = -0.5
			}
		}
	}

	# The trade good is not mine to speak about
	option = {
		name = gdp_merchant.2015.c
		trigger = {
			NOT = {
				primary_title = {
					trade_good_category_level = {
						target = scope:counsel_category
						value >= merchant_monopoly_trade_expertise_level_medium_value
					}
				}
			}
		}
		add_prestige = minor_prestige_gain
		ai_chance = {
			base = 50
			modifier = {
				factor = 2
				has_trait = humble
			}
			ai_value_modifier = {
				ai_honor = 0.5
				ai_boldness = -0.5
			}
		}
	}
}

# A Seat at the Table - your Patron seats you among dignitaries
gdp_merchant.2020 = {
	type = character_event
	title = gdp_merchant.2020.t
	desc = gdp_merchant.2020.desc
	theme = merchant
	override_background = feast

	left_portrait = {
		character = scope:courtier_1
		animation = disapproval
	}
	right_portrait = {
		character = scope:patron
		animation = toast_goblet
		camera = camera_event_right_crowner_crowning
	}

	trigger = {
		is_house_head = yes
		is_available = yes
		government_has_flag = government_is_merchant
		primary_title ?= { NOT = { has_variable = had_event_gdp_merchant_2020 } }
		OR = {
			character_has_trade_monopoly = yes
			merchant_has_any_charter_trigger = yes
		}
		exists = suzerain
		suzerain = {
			is_available_quick = { adult = yes }
			any_courtier_or_guest = {
				this != root
				is_available_ai_adult = yes
				is_lowborn = no
				NOT = {
					government_has_flag = government_is_merchant
				}
			}
		}
	}

	immediate = {
		primary_title ?= { set_variable = had_event_gdp_merchant_2020 }
		suzerain = {
			save_scope_as = patron
			ordered_courtier_or_guest = {
				order_by = highest_held_title_tier
				limit = {
					this != root
					is_available_ai_adult = yes
					is_lowborn = no
					NOT = {
						government_has_flag = government_is_merchant
					}
				}
				max = 1
				check_range_bounds = no
				save_scope_as = courtier_1
			}
		}
	}

	# Splendid, we should do this again sometime
	option = {
		name = gdp_merchant.2020.a
		if = {
			limit = {
				scope:patron = { is_ai = yes }
			}
			custom_tooltip = gdp_merchant.2020.a.feast.tt
			hidden_effect = {
				scope:patron = {
					add_character_flag = { flag = feast_ai_override }
					ai_attempt_to_host_activity = activity_feast
				}
			}
		}
		reverse_add_opinion = {
			target = scope:patron
			modifier = grateful_opinion
			opinion = 10
		}
		stress_and_fulfillment_impact = {
			gregarious = medium_stress_impact_loss
			shy = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_sociability = 1.0
			}
		}
	}

	# My house has come far to earn a seat at this table
	option = {
		name = gdp_merchant.2020.b
		dynasty ?= { add_dynasty_prestige = miniscule_dynasty_prestige_gain }
		house ?= {
			add_unity_value = {
				value = minor_unity_gain
				character = root
				desc = gdp_merchant.2020.b.unity_gain.desc
			}
		}
		reverse_add_opinion = {
			target = scope:patron
			modifier = impressed_opinion
			opinion = 15
		}
		stress_and_fulfillment_impact = {
			ambitious = minor_stress_impact_loss
			humble = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			modifier = {
				factor = 2
				has_trait = ambitious
			}
			modifier = {
				factor = 0.25
				has_trait = humble
			}
		}
	}

	# Titles and toasts are fine, but what of business?
	option = {
		name = gdp_merchant.2020.c
		reverse_add_opinion = {
			target = scope:patron
			modifier = impressed_opinion
			opinion = 20
		}
		house ?= {
			add_unity_value = {
				value = minor_unity_loss
				character = root
				desc = gdp_merchant.2020.c.unity_loss.desc
			}
		}
		add_character_modifier = {
			modifier = gdp_full_attention_modifier
			years = 10
		}
		stress_and_fulfillment_impact = {
			greedy = minor_stress_impact_loss
			diligent = minor_stress_impact_loss
			gregarious = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_greed = 1.0
				ai_sociability = -0.5
			}
		}
	}
}

# The Lesser Merchant - a weaker Merchant holds the Charter you have earned
gdp_merchant.2025 = {
	type = character_event
	title = gdp_merchant.2025.t
	desc = gdp_merchant.2025.desc
	theme = merchant
	override_background = study

	left_portrait = {
		character = scope:patron
		animation = contemplating
	}
	right_portrait = {
		character = root
		animation = stressed_teacher
		camera = camera_event_right_to_the_right
	}
	lower_center_portrait = scope:charter_rival

	trigger = {
		is_house_head = yes
		is_available = yes
		government_has_flag = government_is_merchant
		primary_title ?= { NOT = { has_variable = had_event_gdp_merchant_2025 } }
		character_has_trade_monopoly = yes
		exists = suzerain
		NOT = {
			any_character_artifact = { var:charter_patron ?= root.suzerain }
		}
		trigger_if = {
			limit = { suzerain.highest_held_title_tier >= tier_empire }
			has_realm_law = company_size_huge
		}
		trigger_else_if = {
			limit = { suzerain.highest_held_title_tier >= tier_kingdom }
			OR = {
				has_realm_law = company_size_large
				has_realm_law = company_size_huge
			}
		}
		trigger_else = {
			OR = {
				has_realm_law = company_size_medium
				has_realm_law = company_size_large
				has_realm_law = company_size_huge
			}
		}
		suzerain = {
			is_available_quick = { adult = yes }
			highest_held_title_tier >= tier_duchy
			any_tributary = {
				this != root
				government_has_flag = government_is_merchant
				any_character_artifact = {
					var:charter_patron ?= root.suzerain
					NOT = { exists = var:forger }
				}
				sum_of_all_skills_value < root.sum_of_all_skills_value
			}
		}
	}

	immediate = {
		primary_title ?= { set_variable = had_event_gdp_merchant_2025 }
		suzerain = {
			save_scope_as = patron
			ordered_tributary = {
				limit = {
					this != root
					government_has_flag = government_is_merchant
					any_character_artifact = {
						var:charter_patron ?= root.suzerain
						NOT = { exists = var:forger }
					}
					sum_of_all_skills_value < root.sum_of_all_skills_value
				}
				order_by = sum_of_all_skills_value
				save_scope_as = charter_rival
			}
		}
		scope:charter_rival ?= {
			random_character_artifact = {
				limit = { var:charter_patron ?= scope:patron }
				save_scope_as = rival_charter
			}
		}
		save_scope_value_as = {
			name = charter_opinion_acceptance
			value = {
				value = 25
				multiply = 0.4
			}
		}
	}

	# Since I am so good, why not hand me that charter?
	option = {
		name = gdp_merchant.2025.a
		custom_tooltip = gdp_merchant.2025.a.charter.tt
		reverse_add_opinion = {
			target = scope:patron
			modifier = impressed_opinion
			opinion = 25
		}
		scope:patron = {
			add_opinion = {
				target = scope:charter_rival
				modifier = suspicion_opinion
				opinion = -20
			}
		}
		stress_and_fulfillment_impact = {
			ambitious = minor_stress_impact_loss
			craven = minor_stress_impact_gain
			shy = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 1.0
				ai_greed = 0.5
			}
		}
	}

	# Defer to the Patron's judgement on the charter
	option = {
		name = gdp_merchant.2025.b
		house ?= {
			add_unity_value = {
				value = major_unity_gain
				character = root
				desc = gdp_merchant.2025.b.unity_gain.desc
			}
		}
		add_prestige = minor_prestige_gain
		reverse_add_opinion = {
			target = scope:patron
			modifier = flattered_opinion
			opinion = 15
		}
		stress_and_fulfillment_impact = {
			humble = minor_stress_impact_loss
			ambitious = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_sociability = 0.5
				ai_boldness = -0.5
			}
		}
	}
}

# Court position events
scripted_trigger gdp_merchant_court_pos_booner_trigger = {
	is_available_healthy_ai_adult = yes
	aptitude = {
		court_position = $POS$_court_position
		value >= 3
	}
	trigger_if = {
		limit = { exists = employer }
		opinion = {
			target = employer
			value >= -50
		}
	}
	trigger_else = { always = yes }
}

gdp_merchant.3000 = { # Translator
	type = character_event
	title = gdp_merchant.3000.t
	desc = gdp_merchant.3000.desc
	left_portrait = {
		character = root
		animation = admiration
	}
	right_portrait = {
		character = scope:translator
		animation = writing
	}
	theme = trade
	override_background = { reference = study }
	cooldown = { years = 5 }

	trigger = {
		is_house_head = yes
		is_available_quick = {
			adult = yes
			activity = no
			travel = no
			imprisoned = no
			incapable = no
		}
		is_landless_merchant = yes
		any_court_position_holder = {
			type = translator_court_position
			gdp_merchant_court_pos_booner_trigger = { POS = translator }
			save_temporary_scope_as = translator_temp
		}
		any_culture_global = {
			culture_number_of_counties != 0
			NOT = { has_same_culture_language = root.culture }
			any_culture_duchy = {
				title_province ?= {
					squared_distance = {
						target = root.domicile.domicile_location
						value < squared_distance_large # more than adventurers
					}
					barony.trade_market = {
						any_trade_good_with_surplus = {
							any_trade_category = {
								has_trade_good_category_xp_track = yes
								save_temporary_scope_as = category_temp
								"root.primary_title.trade_good_category_level(scope:category_temp)" < 55
							}
						}
					}
				}
			}
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			add = {
				value = "court_position:translator_court_position.aptitude(translator_court_position)"
				subtract = 3
			}
		}
	}

	immediate = {
		random_court_position_holder = {
			type = translator_court_position
			limit = {
				gdp_merchant_court_pos_booner_trigger = { POS = translator }
			}
			save_scope_as = translator
		}
		every_culture_global = {
			limit = {
				culture_number_of_counties != 0
				NOT = { has_same_culture_language = root.culture }
			}
			every_culture_duchy = {
				limit = {
					title_province ?= {
						exists = barony.trade_market
						squared_distance = {
							target = root.domicile.domicile_location
							value < squared_distance_large # more than adventurers
						}
					}
				}
				title_province.barony.trade_market = {
					add_to_temporary_list = known_markets
				}
			}
		}
		# Save Market
		ordered_in_list = {
			list = known_markets
			limit = {
				any_trade_good_with_surplus = {
					any_trade_category = {
						has_trade_good_category_xp_track = yes
						save_temporary_scope_as = category_temp
						"root.primary_title.trade_good_category_level(scope:category_temp)" < 55
					}
				}
			}
			order_by = {
				value = 1
				every_trade_good_with_surplus = {
					limit = {
						any_trade_category = {
							has_trade_good_category_xp_track = yes
							save_temporary_scope_as = category_temp
							"root.primary_title.trade_good_category_level(scope:category_temp)" < 55
						}
					}
					add = 1
				}
			}
			position = 0
			check_range_bounds = no
			save_scope_as = market
		}
		ordered_culture_global = {
			limit = {
				culture_number_of_counties != 0
				NOT = { has_same_culture_language = root.culture }
				any_culture_duchy = {
					title_province ?= { barony.trade_market ?= scope:market }
				}
			}
			order_by = culture_number_of_counties
			save_scope_as = saved_culture
		}
		scope:market = {
			every_trade_good_with_surplus = {
				limit = {
					any_trade_category = {
						has_trade_good_category_xp_track = yes
						save_temporary_scope_as = category_temp
						"root.primary_title.trade_good_category_level(scope:category_temp)" < 55
						NOT = { is_in_list = categories }
					}
				}
				add_to_list = goods
			}
		}
		random_in_global_list = {
			variable = merchants_tally
			limit = {
				holder ?= {
					culture = scope:saved_culture
					save_temporary_scope_as = merchant_temp
				}
				scope:market = {
					any_character_in_market = { this = scope:merchant_temp }
				}
				gdp_merchant_diplo_range_trigger = {
					TARGET = scope:merchant_temp
					MERCHANT = root
				}
			}
			alternative_limit = {
				holder ?= {
					culture = scope:saved_culture
					save_temporary_scope_as = merchant_temp
				}
				gdp_merchant_diplo_range_trigger = {
					TARGET = scope:merchant_temp
					MERCHANT = root
				}
			}
			weight = {
				base = 1
				modifier = {
					holder ?= {
						is_available_quick = {
							adult = yes
							incapable = no
							imprisoned = no
						}
					}
					add = 100
				}
			}
			save_scope_as = culture_merchant
		}
		every_in_list = {
			list = goods
			random_trade_category = {
				limit = {
					has_trade_good_category_xp_track = yes
					save_temporary_scope_as = category_temp
					"root.primary_title.trade_good_category_level(scope:category_temp)" < 55
					NOT = { is_in_list = categories }
				}
				add_to_list = categories
			}
		}
	}

	option = {
		name = gdp_merchant.3000.a
		custom_tooltip = gdp_merchant.3000.a.tt
		every_in_list = {
			list = categories
			root.primary_title = {
				add_trade_good_category_xp = {
					target = prev
					value = {
						value = 200
						multiply = "scope:translator.aptitude(translator_court_position)"
						divide = list_size:categories
					}
				}
			}
		}
		ai_chance = {
			base = 75
		}
	}

	option = {
		name = gdp_merchant.3000.b
		trigger = {
			exists = scope:culture_merchant
			can_start_scheme = { type = learn_language target_character = scope:culture_merchant.holder }
		}
		start_scheme = {
			type = learn_language
			target_character = scope:culture_merchant.holder
		}
		custom_tooltip = {
			text = gdp_merchant.3000.b.tt
			random_scheme = {
				type = learn_language
				limit = { scheme_target_character = scope:culture_merchant.holder }
				add_scheme_modifier = { type = gdp_merchant_translator_buff_modifier }

			}
		}
		stress_and_fulfillment_impact = {
			lazy = medium_stress_impact_gain
		}
		ai_chance = {
			base = 25
			modifier = {
				factor = 0
				has_trait = lazy
			}
		}
	}

	option = {
		name = gdp_merchant.3000.d
		trigger = {
			NOT = {
				scope:translator = { knows_language_of_culture = scope:saved_culture }
			}
		}
		show_as_unavailable = { always = yes }
		scope:translator = { learn_language_of_culture = scope:saved_culture }
		stress_and_fulfillment_impact = {
			greedy = medium_stress_impact_gain
			impatient = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			modifier = {
				factor = 0
				has_trait = greedy
			}
		}
	}

	option = {
		name = gdp_merchant.3000.c
		add_short_term_gold = minor_gold_value
	}
}

scripted_trigger fixer_scheme_trigger = {
	hostile_scheme_trigger = yes
	scheme_owner = { is_ai = yes }
	save_temporary_scope_as = scheme
	scheme_progress >= scheme_progress_33
}

scripted_trigger fixer_secret_trigger = {
	OR = {
		is_shunned_for = secret_owner
		AND = {
			NOT = {
				is_blackmailable_secret_trigger = {
					PARTICIPANT = secret_owner
					BLACKMAILER = root
				}
			}
			secret_is_always_interesting_trigger = yes
		}
	}
	NOT = { is_known_by = root }
	# Spymaster should not reveal secrets targeting themselves unless it is a murder attempt
	trigger_if = {
		limit = {
			exists = secret_target
			NOT = { secret_type = secret_murder_attempt }
		}
		secret_target != $FIXER$
	}
	# If the spymaster is the father of a secret bastard, don't reveal that
	trigger_if = {
		limit = {
			OR = {
				secret_type = secret_disputed_heritage
				secret_type = secret_unmarried_illegitimate_child
			}
			exists = secret_target.real_father
		}
		secret_target.real_father != $FIXER$
	}
	NOT = {
		any_secret_participant = {
			OR = {
				this = $FIXER$
				this = root
			}
		}
	}
	save_temporary_scope_as = suitable_secret
	trigger_if = {
		limit = {
			secret_type = secret_disputed_heritage
			exists = secret_target
		}
		NOT = { secret_target = { has_trait = legitimized_bastard } }
	}
	OR = {
		secret_is_always_interesting_trigger = yes
		is_blackmailable_secret_trigger = { BLACKMAILER = root PARTICIPANT = scope:suitable_secret.secret_owner }
	}
}

gdp_merchant.3010 = { # Fixer
	type = character_event
	title = gdp_merchant.3010.t
	desc = {
		desc = gdp_merchant.3010.desc
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:secret }
				desc = gdp_merchant.3010.desc.secret
			}
			desc = gdp_merchant.3010.desc.scheme
		}
	}
	left_portrait = {
		character = root
		animation = admiration
	}
	right_portrait = {
		character = scope:fixer
		animation = trade_deal
	}
	lower_right_portrait = scope:secret_haver
	lower_left_portrait = scope:scheme_owner
	theme = trade
	override_background = { reference = study }
	cooldown = { years = 5 }

	trigger = {
		is_house_head = yes
		is_available_quick = {
			adult = yes
			activity = no
			travel = no
			imprisoned = no
			incapable = no
		}
		is_landless_merchant = yes
		any_court_position_holder = {
			type = fixer_court_position
			gdp_merchant_court_pos_booner_trigger = { POS = fixer }
			save_temporary_scope_as = fixer_temp
		}
		OR = {
			any_targeting_scheme = { fixer_scheme_trigger = yes }
			suzerain = {
				any_secret = {
					fixer_secret_trigger = { FIXER = scope:fixer_temp }
				}
			}
			any_trade_route = {
				any_trade_market_on_route = {
					any_character_in_market = {
						any_secret = {
							fixer_secret_trigger = { FIXER = scope:fixer_temp }
						}
					}
				}
			}
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			add = {
				value = "court_position:fixer_court_position.aptitude(fixer_court_position)"
				subtract = 3
			}
		}
	}

	immediate = {
		random_court_position_holder = {
			type = fixer_court_position
			limit = {
				gdp_merchant_court_pos_booner_trigger = { POS = fixer }
			}
			save_scope_as = fixer
		}
		hidden_effect = {
			random_list = {
				50 = {
					trigger = {
						any_targeting_scheme = { fixer_scheme_trigger = yes }
					}
					random_targeting_scheme = {
						limit = { fixer_scheme_trigger = yes }
						save_scope_as = scheme
						scheme_owner = { save_scope_as = scheme_owner }
					}
				}
				25 = {
					trigger = {
						suzerain = {
							any_secret = {
								fixer_secret_trigger = { FIXER = scope:fixer }
							}
						}
					}
					suzerain = {
						save_scope_as = secret_haver
						random_secret = {
							limit = {
								fixer_secret_trigger = { FIXER = scope:fixer }
							}
							save_scope_as = secret
						}
					}
				}
				25 = {
					trigger = {
						any_trade_route = {
							any_trade_market_on_route = {
								any_character_in_market = {
									any_secret = {
										fixer_secret_trigger = { FIXER = scope:fixer }
									}
								}
							}
						}
					}
					every_trade_route = {
						every_trade_market_on_route = {
							every_character_in_market = {
								limit = {
									any_secret = {
										fixer_secret_trigger = { FIXER = scope:fixer }
									}
								}
								add_to_list = secret_havers
							}
						}
					}
				}
			}
			if = {
				limit = {
					any_in_list = { list = secret_havers }
				}
				random_in_list = {
					list = secret_havers
					weight = {
						base = 1
						modifier = {
							is_landless_merchant = yes
							add = 5
						}
						modifier = {
							house ?= {
								any_house_relation = {
									any_relation_house = { this = root.house }
									OR = {
										has_house_relation_level = quarrel
										has_house_relation_level = rivalry
										has_house_relation_level = feud
									}
								}
							}
							add = 5
						}
					}
					save_scope_as = secret_haver
					random_secret = {
						limit = {
							fixer_secret_trigger = { FIXER = scope:fixer }
						}
						save_scope_as = secret
					}
				}
			}
		}
	}

	option = {
		name = gdp_merchant.3010.d
		trigger = {
			OR = {
				has_trait = journaller
				has_trait = lifestyle_poet
				has_trait = violet_poet
			}
		}
		if = {
			limit = { exists = scope:secret }
			scope:secret ?= { reveal_to = root }
		}
		else = {
			scope:scheme ?= { expose_scheme = yes }
		}
		add_character_modifier = {
			modifier = ep3_inspired_by_life
			years = 15
		}
		stress_and_fulfillment_impact = {
			journaller = medium_stress_impact_loss
			lifestyle_poet = medium_stress_impact_loss
			violet_poet = medium_stress_impact_loss
			deceitful = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
			modifier = {
				factor = 0
				has_trait = deceitful
			}
			modifier = {
				factor = 2
				OR = {
					has_trait = journaller
					has_trait = lifestyle_poet
					has_trait = violet_poet
				}
			}
		}
	}

	option = {
		name = gdp_merchant.3010.a
		if = {
			limit = { exists = scope:secret }
			scope:secret ?= { reveal_to = root }
		}
		else = {
			scope:scheme ?= { expose_scheme = yes }
		}
		random = {
			chance = 50
			scope:fixer = { add_intrigue_skill = 1 }
		}
		ai_chance = {
			base = 50
			modifier = {
				factor = 2
				has_trait = deceitful
			}
		}
	}
	
	option = {
		name = gdp_merchant.3010.b
		trigger = {
			can_add_hook = {
				target = scope:fixer
				type = favor_hook
			}
		}
		add_hook = {
			target = scope:fixer
			type = favor_hook
		}
		reverse_add_opinion = {
			target = scope:fixer
			modifier = upset_opinion
			opinion = -10
		}
		if = {
			limit = { exists = scope:secret }
			scope:secret ?= { reveal_to = root }
		}
		else = {
			scope:scheme ?= { expose_scheme = yes }
		}
		stress_and_fulfillment_impact = {
			paranoid = medium_stress_impact_loss
			trusting = medium_stress_impact_gain
		}
		ai_chance = {
			base = 25
			modifier = {
				factor = 0
				OR = {
					has_trait = compassionate
					has_trait = trusting
				}
			}
			modifier = {
				factor = 2
				OR = {
					has_trait = callous
					has_trait = paranoid
				}
			}
		}
	}
	
	option = {
		name = gdp_merchant.3010.c
		add_piety = minor_piety_gain
		reverse_add_opinion = {
			target = scope:fixer
			modifier = insulted_opinion
			opinion = -10
		}
		stress_and_fulfillment_impact = {
			zealous = medium_stress_impact_loss
			deceitful = medium_stress_impact_gain
			callous = medium_stress_impact_gain
		}
		ai_chance = {
			base = 25
			modifier = {
				factor = 0
				OR = {
					has_trait = deceitful
					has_trait = callous
				}
			}
			modifier = {
				factor = 2
				has_trait = zealous
			}
		}
	}
}

scripted_trigger gdp_merchant_3020_shared_loan_trigger = {
	story_type = story_cycle_loan
	story_owner = {
		is_ai = yes
		is_at_war = no
		is_available_quick = {
			adult = yes
			incapable = no
			imprisoned = no
		}
	}
	var:lender ?= $MERCHANT$
	exists = var:amount_owed
	# 2 years since loan
	exists = var:years_since
	var:years_since >= 2
	# not recently asked
	NOT = { exists = var:bailiff_cooldown }
}

scripted_trigger gdp_merchant_3020_pressure_debtor_trigger = {
	any_owned_story = {
		gdp_merchant_3020_pressure_loan_trigger = { MERCHANT = $MERCHANT$ }
	}
	NOR = {
		scope:demand_debtor_1 ?= this
		scope:demand_debtor_2 ?= this
		scope:pressure_debtor_1 ?= this
		scope:pressure_debtor_2 ?= this
	}
}

scripted_trigger gdp_merchant_3020_pressure_loan_trigger = {
	gdp_merchant_3020_shared_loan_trigger = { MERCHANT = $MERCHANT$ }
	# has some gold...
	story_owner.gold > story_owner.minor_gold_value
	# ...but cannot actually repay yet...
	var:amount_owed > story_owner.gold
	# ...and stop remainder becoming tiny by a thousand cuts
	var:amount_owed > story_owner.major_gold_value
}

scripted_effect gdp_merchant_3020_pressure_loan_effect = {
	$LOAN$ = { save_temporary_scope_as = loan_temp }
	scope:loan_temp = {
		set_variable = {
			name = bailiff_cooldown
			years = 5
		}
		story_owner = {
			add_opinion = {
				modifier = annoyed_opinion
				opinion = -10
				target = root
			}
			pay_short_term_gold = {
				target = root
				gold = minor_gold_value
			}
			custom_tooltip = gdp_merchant.3020.a.tt
		}
		root = { gdp_merchant_creditor_loan_acumen_effect = { GOLD = scope:loan_temp.story_owner.minor_gold_value } }
		change_variable = {
			name = amount_owed
			add = {
				value = story_owner.minor_gold_value
				multiply = -1
			}
		}
		save_scope_value_as = {
			name = outstanding
			value = var:amount_owed
		}
	}
	hidden_effect = {
		send_interface_toast = {
			title = ho_loan_reduced_title
			right_icon = scope:loan_temp.story_owner
			scope:loan_temp ?= { custom_tooltip = gdp_merchant_3020_pressure_tt }
		}
	}
}

scripted_trigger gdp_merchant_3020_demand_debtor_trigger = {
	any_owned_story = {
		gdp_merchant_3020_demand_loan_trigger = { MERCHANT = $MERCHANT$ }
	}
	NOR = {
		scope:demand_debtor_1 ?= this
		scope:demand_debtor_2 ?= this
		scope:pressure_debtor_1 ?= this
		scope:pressure_debtor_2 ?= this
	}
}

scripted_trigger gdp_merchant_3020_demand_loan_trigger = {
	gdp_merchant_3020_shared_loan_trigger = { MERCHANT = $MERCHANT$ }
	# can repay now right now this very second
	var:amount_owed < story_owner.gold
}

scripted_effect gdp_merchant_3020_save_effect = {
	if = {
		limit = { exists = scope:$TYPE$_debtor_2 }
		save_scope_as = $TYPE$_debtor_3
	}
	else_if = {
		limit = { exists = scope:$TYPE$_debtor_1 }
		save_scope_as = $TYPE$_debtor_2
	}
	else = { save_scope_as = $TYPE$_debtor_1 }
	if = {
		limit = { exists = scope:portrait_2 }
		save_scope_as = portrait_3
	}
	else_if = {
		limit = { exists = scope:portrait_1 }
		save_scope_as = portrait_2
	}
	else = { save_scope_as = portrait_1 }
	random_owned_story = {
		limit = {
			gdp_merchant_3020_$TYPE$_loan_trigger = { MERCHANT = root }
		}
		if = {
			limit = { exists = scope:$TYPE$_loan_2 }
			save_scope_as = $TYPE$_loan_3
		}
		else_if = {
			limit = { exists = scope:$TYPE$_loan_1 }
			save_scope_as = $TYPE$_loan_2
		}
		else = { save_scope_as = $TYPE$_loan_1 }
		add_to_list = loan_list
	}
}

scripted_effect gdp_merchant_3020_demand_loan_effect = {
	$LOAN$ = { save_temporary_scope_as = loan_temp }
	random_list = {
		1 = {
			modifier = {
				"scope:bailiff.aptitude(bailiff_court_position)" >= 3
				add = 10
			}
			modifier = {
				"scope:bailiff.aptitude(bailiff_court_position)" >= 4
				add = 25
			}
			opinion_modifier = {
				who = scope:loan_temp.story_owner
				opinion_target = root
				multiplier = 0.2
			}
			scope:loan_temp = { repay_and_end_loan_story_effect = yes } # use this effect to show relevant tooltips
			save_temporary_scope_as = loan_repaid
		}
		50 = {
			gdp_merchant_3020_pressure_loan_effect = { LOAN = scope:loan_temp }
		}
	}
	stress_and_fulfillment_impact = {
		patient = minor_stress_impact_gain
		generous = minor_stress_impact_gain
	}
}

gdp_merchant.3020 = { # Bailiff
	type = character_event
	title = gdp_merchant.3020.t
	desc = {
		desc = gdp_merchant.3020.desc
		triggered_desc = {
			trigger = { exists = scope:pressure_debtor_1 }
			desc = gdp_merchant.3020.pressure
		}
		triggered_desc = {
			trigger = { exists = scope:demand_debtor_1 }
			desc = gdp_merchant.3020.demand
		}
		triggered_desc = {
			trigger = { exists = scope:portrait_3 }
			desc = gdp_merchant.3020.third
		}
	}
	left_portrait = {
		character = root
		animation = admiration
	}
	right_portrait = {
		character = scope:bailiff
		animation = steward
	}
	lower_left_portrait = scope:portrait_1
	lower_center_portrait = scope:portrait_2
	lower_right_portrait = scope:portrait_3
	theme = trade
	override_background = { reference = study }
	cooldown = { years = 5 }

	trigger = {
		is_house_head = yes
		is_available_quick = {
			adult = yes
			activity = no
			travel = no
			imprisoned = no
			incapable = no
		}
		is_landless_merchant = yes
		any_court_position_holder = {
			type = bailiff_court_position
			gdp_merchant_court_pos_booner_trigger = { POS = bailiff }
		}
		OR = {
			any_in_list = {
				variable = owes_me_money
				any_owned_story = {
					gdp_merchant_3020_demand_loan_trigger = { MERCHANT = root }
				}
			}
			any_in_list = {
				variable = owes_me_money
				any_owned_story = {
					gdp_merchant_3020_pressure_loan_trigger = { MERCHANT = root }
				}
			}
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			add = {
				value = "court_position:bailiff_court_position.aptitude(bailiff_court_position)"
				subtract = 3
			}
		}
	}

	immediate = {
		random_court_position_holder = {
			type = bailiff_court_position
			limit = {
				gdp_merchant_court_pos_booner_trigger = { POS = bailiff }
			}
			save_scope_as = bailiff
		}
		# Pressure
		ordered_in_list = {
			variable = owes_me_money
			limit = {
				gdp_merchant_3020_pressure_debtor_trigger = { MERCHANT = root }
			}
			order_by = gold
			gdp_merchant_3020_save_effect = { TYPE = pressure }
		}
		# Demand
		ordered_in_list = {
			variable = owes_me_money
			limit = {
				gdp_merchant_3020_demand_debtor_trigger = { MERCHANT = root }
			}
			order_by = gold
			gdp_merchant_3020_save_effect = { TYPE = demand }
		}
		hidden_effect = {
			while = {
				count = 2
				random_list = {
					# Pressure
					10 = {
						trigger = {
							any_in_list = {
								variable = owes_me_money
								gdp_merchant_3020_pressure_debtor_trigger = { MERCHANT = root }
							}
						}
						ordered_in_list = {
							variable = owes_me_money
							limit = {
								gdp_merchant_3020_pressure_debtor_trigger = { MERCHANT = root }
							}
							order_by = gold
							gdp_merchant_3020_save_effect = { TYPE = pressure }
						}
					}
					# Demand
					10 = {
						trigger = {
							any_in_list = {
								variable = owes_me_money
								gdp_merchant_3020_demand_debtor_trigger = { MERCHANT = root }
							}
						}
						ordered_in_list = {
							variable = owes_me_money
							limit = {
								gdp_merchant_3020_demand_debtor_trigger = { MERCHANT = root }
							}
							order_by = gold
							gdp_merchant_3020_save_effect = { TYPE = demand }
						}
					}
				}
			}
		}
	}

	option = { # Pressure 1
		name = gdp_merchant.3020.a
		trigger = {
			scope:pressure_loan_1.story_owner ?= { gold > minor_gold_value }
		}
		gdp_merchant_3020_pressure_loan_effect = { LOAN = scope:pressure_loan_1 }
		stress_and_fulfillment_impact = {
			patient = minor_stress_impact_gain
			generous = minor_stress_impact_gain
		}
		ai_chance = {
			base = 50
			opinion_modifier = {
				opinion_target = scope:pressure_loan_1.story_owner
				multiplier = -0.25
			}
			modifier = {
				add = {
					value = scope:pressure_loan_1.var:years_since
					multiply = 5
				}
			}
			modifier = {
				has_trait = generous
				add = -25
			}
			modifier = {
				has_trait = patient
				add = -25
			}
		}
	}

	option = { # Demand 1
		name = gdp_merchant.3020.b
		trigger = { exists = scope:demand_loan_1 }
		gdp_merchant_3020_demand_loan_effect = { LOAN = scope:demand_loan_1 }
		ai_chance = {
			base = 50
			opinion_modifier = {
				opinion_target = scope:demand_loan_1.story_owner
				multiplier = -0.25
			}
			modifier = {
				add = {
					value = scope:demand_loan_1.var:years_since
					multiply = 5
				}
			}
			modifier = {
				has_trait = generous
				add = -25
			}
			modifier = {
				has_trait = patient
				add = -25
			}
		}
	}

	option = { # Pressure 2
		name = gdp_merchant.3020.c
		trigger = {
			scope:pressure_loan_2.story_owner ?= { gold > minor_gold_value }
		}
		gdp_merchant_3020_pressure_loan_effect = { LOAN = scope:pressure_loan_2 }
		stress_and_fulfillment_impact = {
			patient = minor_stress_impact_gain
			generous = minor_stress_impact_gain
		}
		ai_chance = {
			base = 50
			opinion_modifier = {
				opinion_target = scope:pressure_loan_2.story_owner
				multiplier = -0.25
			}
			modifier = {
				add = {
					value = scope:pressure_loan_2.var:years_since
					multiply = 5
				}
			}
			modifier = {
				has_trait = generous
				add = -25
			}
			modifier = {
				has_trait = patient
				add = -25
			}
		}
	}

	option = { # Demand 2
		name = gdp_merchant.3020.d
		trigger = { exists = scope:demand_loan_2 }
		gdp_merchant_3020_demand_loan_effect = { LOAN = scope:demand_loan_2 }
		ai_chance = {
			base = 50
			opinion_modifier = {
				opinion_target = scope:demand_loan_2.story_owner
				multiplier = -0.25
			}
			modifier = {
				add = {
					value = scope:demand_loan_2.var:years_since
					multiply = 5
				}
			}
			modifier = {
				has_trait = generous
				add = -25
			}
			modifier = {
				has_trait = patient
				add = -25
			}
		}
	}

	option = { # Pressure 3
		name = gdp_merchant.3020.e
		trigger = {
			scope:pressure_loan_3.story_owner ?= { gold > minor_gold_value }
		}
		gdp_merchant_3020_pressure_loan_effect = { LOAN = scope:pressure_loan_3 }
		stress_and_fulfillment_impact = {
			patient = minor_stress_impact_gain
			generous = minor_stress_impact_gain
		}
		ai_chance = {
			base = 50
			opinion_modifier = {
				opinion_target = scope:pressure_loan_3.story_owner
				multiplier = -0.25
			}
			modifier = {
				add = {
					value = scope:pressure_loan_3.var:years_since
					multiply = 5
				}
			}
			modifier = {
				has_trait = generous
				add = -25
			}
			modifier = {
				has_trait = patient
				add = -25
			}
		}
	}

	option = { # Demand 3
		name = gdp_merchant.3020.f
		trigger = { exists = scope:demand_loan_3 }
		gdp_merchant_3020_demand_loan_effect = { LOAN = scope:demand_loan_3 }
		ai_chance = {
			base = 50
			opinion_modifier = {
				opinion_target = scope:demand_loan_3.story_owner
				multiplier = -0.25
			}
			modifier = {
				add = {
					value = scope:demand_loan_3.var:years_since
					multiply = 5
				}
			}
			modifier = {
				has_trait = generous
				add = -25
			}
			modifier = {
				has_trait = patient
				add = -25
			}
		}
	}

	option = { # Shrug
		name = gdp_merchant.3020.g
		add_prestige = minor_prestige_gain
		stress_and_fulfillment_impact = {
			patient = minor_stress_impact_loss
			impatient = minor_stress_impact_gain
			lazy = minor_stress_impact_loss
		}
		ai_chance = {
			base = 50
			modifier = {
				factor = 0.5
				has_trait = impatient
			}
			modifier = {
				factor = 2
				has_trait = patient
			}
			modifier = {
				factor = 2
				has_trait = content
			}
			modifier = {
				factor = 2
				gold < minor_gold_value
			}
			modifier = {
				factor = 2
				prestige < minor_prestige_value
			}
		}
	}
}

gdp_merchant.3030 = { # Market Agent
	type = character_event
	hidden = yes
	cooldown = { years = 5 }

	trigger = {
		is_house_head = yes
		is_available_quick = {
			adult = yes
			activity = no
			travel = no
			imprisoned = no
			incapable = no
		}
		is_landless_merchant = yes
		any_court_position_holder = {
			type = market_agent_court_position
			gdp_merchant_court_pos_booner_trigger = { POS = market_agent }
		}
		any_trade_route = {
			has_trader = yes
			any_trade_market_on_route = {}
		}
		OR = {
			can_create_task_contract = { type_name = challenge_competitor }
			can_create_task_contract = { type_name = promote_goods }
			can_create_task_contract = { type_name = market_investment }
			can_create_task_contract = { type_name = exploit_upheaval }
			can_create_task_contract = { type_name = support_guilds }
			can_create_task_contract = { type_name = seek_debtors }
			can_create_task_contract = { type_name = sabotage_competition }
			can_create_task_contract = { type_name = build_category_connections }
			can_create_task_contract = { type_name = disrupt_charter }
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			add = {
				value = "court_position:market_agent_court_position.aptitude(market_agent_court_position)"
				subtract = 3
			}
		}
	}

	immediate = {
		random_court_position_holder = {
			type = market_agent_court_position
			limit = {
				gdp_merchant_court_pos_booner_trigger = { POS = market_agent }
			}
			save_scope_as = market_agent
		}
		set_variable = {
			name = market_agent_opportunity
			value = scope:market_agent
			days = 1
		}
		hidden_effect = {
			random_list = {
				10 = {
					trigger = {
						can_create_task_contract = { type_name = challenge_competitor }
					}
					trigger_event = { on_action = gdp_challenge_competitor }
				}
				10 = {
					trigger = {
						can_create_task_contract = { type_name = promote_goods }
					}
					trigger_event = { on_action = gdp_promote_goods }
				}
				10 = {
					trigger = {
						can_create_task_contract = { type_name = market_investment }
					}
					trigger_event = { on_action = gdp_market_investment }
				}
				10 = {
					trigger = {
						can_create_task_contract = { type_name = exploit_upheaval }
					}
					trigger_event = { on_action = gdp_exploit_upheaval }
				}
				10 = {
					trigger = {
						can_create_task_contract = { type_name = support_guilds }
					}
					trigger_event = { on_action = gdp_support_guilds }
				}
				10 = {
					trigger = {
						can_create_task_contract = { type_name = seek_debtors }
					}
					trigger_event = { on_action = gdp_seek_debtors }
				}
				10 = {
					trigger = {
						can_create_task_contract = { type_name = sabotage_competition }
					}
					trigger_event = { on_action = gdp_sabotage_competition }
				}
				10 = {
					trigger = {
						can_create_task_contract = { type_name = build_category_connections }
					}
					trigger_event = { on_action = gdp_build_category_connections }
				}
				10 = {
					trigger = {
						can_create_task_contract = { type_name = disrupt_charter }
					}
					trigger_event = { on_action = gdp_disrupt_charter }
				}
			}
		}
	}
}

gdp_merchant.3031 = {
	type = character_event
	title = gdp_merchant.3031.t
	desc = gdp_merchant.3031.desc
	left_portrait = {
		character = root
		animation = admiration
	}
	right_portrait = {
		character = scope:market_agent
		animation = holding_tally_stick
	}
	theme = trade
	override_background = { reference = study }
	cooldown = { years = 5 }

	trigger = {
		exists = scope:trade_opportunity
		exists = var:market_agent_opportunity
	}

	immediate = {
		var:market_agent_opportunity = { save_scope_as = market_agent }
	}

	option = {
		name = gdp_merchant.3031.a
		custom_tooltip = gdp_merchant.3031.a.tt
		ai_chance = {
			base = 10
		}
	}
	option = {
		name = gdp_merchant.3031.b
		if = {
			limit = {
				NOT = { has_trait = lifestyle_merchant }
			}
			add_trait = lifestyle_merchant
		}
		else = {
			if = {
				limit = { has_realm_law = company_purpose_creditor }
				add_trait_xp = {
					trait = lifestyle_merchant
					track = creditor
					value = 3
				}
			}
			else_if = {
				limit = { has_realm_law = company_purpose_artisan }
				add_trait_xp = {
					trait = lifestyle_merchant
					track = artisan
					value = 3
				}
			}
			else_if = {
				limit = { has_realm_law = company_purpose_caravaneer }
				add_trait_xp = {
					trait = lifestyle_merchant
					track = caravaneer
					value = 3
				}
			}
			else = {
				add_trait_xp = {
					trait = lifestyle_merchant
					track = creditor
					value = 1
				}
				add_trait_xp = {
					trait = lifestyle_merchant
					track = artisan
					value = 1
				}
				add_trait_xp = {
					trait = lifestyle_merchant
					track = caravaneer
					value = 1
				}
			}
		}
		reverse_add_opinion = {
			modifier = annoyed_opinion
			target = scope:market_agent
			opinion = -10
		}
		scope:trade_opportunity = { invalidate_contract = yes }
		ai_chance = {
			base = 0
		}
	}

	after = {
		remove_variable = market_agent_opportunity
	}
}

gdp_merchant.3040 = { # Antiquarian
	type = character_event
	title = gdp_merchant.3040.t
	desc = {
		desc = gdp_merchant.3040.desc
	}
	left_portrait = {
		character = root
		animation = admiration
	}
	right_portrait = {
		character = scope:antiquarian
		triggered_animation = {
			trigger = {
				scope:artifact = { artifact_slot_type = creature }
			}
			animation = thinking
		}
		triggered_animation = {
			trigger = {
				scope:artifact = { artifact_slot_type = curio }
			}
			animation = merchant_box
		}
		animation = page_flipping
	}
	artifact = {
		position = lower_center_portrait
		target = scope:artifact
		trigger = { exists = scope:artifact }
	}
	theme = trade
	override_background = { reference = study }
	cooldown = { years = 5 }

	trigger = {
		is_house_head = yes
		is_available_quick = {
			adult = yes
			activity = no
			travel = no
			imprisoned = no
			incapable = no
		}
		is_landless_merchant = yes
		any_court_position_holder = {
			type = antiquarian_court_position
			gdp_merchant_court_pos_booner_trigger = { POS = antiquarian }
		}
		any_equipped_character_artifact = {
			OR = {
				artifact_slot_type = creature
				artifact_slot_type = curio
				artifact_slot_type = library
			}
		}
	}

	weight_multiplier = {
		base = 1
		modifier = {
			add = {
				value = "court_position:antiquarian_court_position.aptitude(antiquarian_court_position)"
				subtract = 3
			}
		}
	}

	immediate = {
		random_court_position_holder = {
			type = antiquarian_court_position
			limit = {
				gdp_merchant_court_pos_booner_trigger = { POS = antiquarian }
			}
			save_scope_as = antiquarian
		}
		random_equipped_character_artifact = {
			limit = {
				OR = {
					artifact_slot_type = creature
					artifact_slot_type = curio
					artifact_slot_type = library
				}
				NOT = { exists = var:curator_cooldown }
				artifact_durability < artifact_max_durability
			}
			alternative_limit = {
				OR = {
					artifact_slot_type = creature
					artifact_slot_type = curio
					artifact_slot_type = library
				}
				NOT = { exists = var:curator_cooldown }
			}
			weight = {
				base = 1
				modifier = {
					add = {
						value = 100
						divide = artifact_max_durability
						multiply = artifact_durability
						subtract = 100
						multiply = -1
					}
				}
				modifier = {
					rarity = masterwork
					factor = 0.75
				}
				modifier = {
					rarity = famed
					factor = 0.5
				}
				modifier = {
					rarity = illustrious
					factor = 0.25
				}
			}
			set_variable = {
				name = curator_cooldown
				years = 5
			}
			save_scope_as = artifact
		}
	}

	option = {
		name = gdp_merchant.3040.b
		add_short_term_gold = {
			gold = {
				value = 0
				if = {
					limit = {
						scope:artifact = { rarity = illustrious }
					}
					add = 25
				}
				else_if = {
					limit = {
						scope:artifact = { rarity = famed }
					}
					add = 15
				}
				else_if = {
					limit = {
						scope:artifact = { rarity = masterwork }
					}
					add = 8
				}
				else = {
					add = 4
				}

				# Boost the value up to something meaningful (merchants)
				multiply = {
					value = 5
				}

				# multiply by durability percentage
				multiply = {
					add = scope:artifact.artifact_durability
					divide = scope:artifact.artifact_max_durability
					divide = 2
					add = 0.5
				}

				# More than just destroying
				if = {
					limit = { "scope:antiquarian.aptitude(antiquarian_court_position)" = 4 }
					multiply = 2
				}
				else_if = {
					limit = { "scope:antiquarian.aptitude(antiquarian_court_position)" = 3 }
					multiply = 1.75
				}
				else = { multiply = 1.5 }
			}
		}
		if = {
			limit = {
				scope:artifact = { artifact_type = creature }
			}
			scope:artifact = { save_scope_as = sold_creature }
			custom_tooltip = {
				text = sell_creature_artifact_effect
				destroy_owned_artifact = scope:artifact
			}
		}
		else = {
			destroy_owned_artifact = scope:artifact
		}
		stress_and_fulfillment_impact = {
			greedy = minor_stress_impact_loss
		}
		ai_chance = {
			base = 25
			modifier = {
				has_trait = greedy
				add = 25
			}
			modifier = {
				gold < minor_gold_value
				add = 25
			}
			modifier = {
				scope:artifact = { artifact_slot_type = library }
				NOT = {
					any_character_artifact = {
						is_equipped = no
						artifact_slot_type = library
					}
				}
				add = -25
			}
			modifier = {
				scope:artifact = { artifact_slot_type = curio }
				NOT = {
					any_character_artifact = {
						is_equipped = no
						artifact_slot_type = curio
					}
				}
				add = -25
			}
			modifier = {
				scope:artifact = { artifact_slot_type = creature }
				NOT = {
					any_character_artifact = {
						is_equipped = no
						artifact_slot_type = creature
					}
				}
				add = -25
			}
			modifier = {
				scope:artifact = { rarity = common }
				add = 25
			}
			modifier = {
				scope:artifact = { rarity = masterwork }
				add = 15
			}
			modifier = {
				scope:artifact = { rarity = famed }
				add = -15
			}
			modifier = {
				scope:artifact = { rarity = illustrious }
				add = -25
			}
		}
	}

	option = {
		name = gdp_merchant.3040.c
		trigger = {
			custom_tooltip = {
				text = gdp_merchant.3040.c.tt.creature
				NOT = {
					scope:artifact = { artifact_slot_type = creature }
				}
			}
			custom_tooltip = {
				text = gdp_merchant.3040.c.tt
				scope:artifact.artifact_durability < scope:artifact.artifact_max_durability
			}
		}
		show_as_unavailable = { always = yes }
		scope:artifact = { add_durability = 10 }
		scope:antiquarian = {
			stress_and_fulfillment_impact = {
				lazy = minor_stress_impact_gain
			}
		}
		stress_and_fulfillment_impact = {
			diligent = minor_stress_impact_loss
		}
		ai_chance = {
			base = 25
			modifier = {
				has_trait = diligent
				add = 25
			}
			modifier = {
				scope:artifact = { rarity = common }
				add = -25
			}
			modifier = {
				scope:artifact = { rarity = masterwork }
				add = -15
			}
			modifier = {
				scope:artifact = { rarity = famed }
				add = 15
			}
			modifier = {
				scope:artifact = { rarity = illustrious }
				add = 25
			}
		}
	}

	option = {
		name = gdp_merchant.3040.d
		add_prestige = minor_prestige_gain
		stress_and_fulfillment_impact = {
			content = minor_stress_impact_loss
			lazy = minor_stress_impact_loss
		}
		ai_chance = {
			base = 25
			modifier = {
				has_trait = content
				add = 25
			}
			modifier = {
				has_trait = lazy
				add = 25
			}
			modifier = {
				factor = 2
				prestige < minor_prestige_value
			}
		}
	}
}

################################
# POOL CHARACTER INTO MERCHANT #
################################
gdp_merchant.4001 = {
	hidden = yes
	trigger = {
		gdp_should_spawn_new_merchant_company_trigger = yes
		is_pool_character = yes # Can cease to be in the pool between it queuing and executing
		gdp_pool_character_valid_for_merchant_company_trigger = yes
		pool_character_is_pruneable_trigger = no # Avoid promoting uninteresting characters
		gdp_merchant_family_has_no_ruler_trigger = yes # No ruling dynasty / close or extended family
	}

	immediate = {
		create_merchant_title_effect = {
			REASON = flag:historical
			FLAVOR_CHAR = root
		}
	}
}

gdp_merchant.4002 = {
	hidden = yes
	trigger = {
		exists = global_var:gdp_minority_merchant_spawn_quota
		global_var:gdp_minority_merchant_spawn_quota > 0
		is_pool_character = yes
		gdp_minority_merchant_candidate_trigger = yes
		gdp_merchant_companies_below_cap_trigger = yes
		gdp_pool_character_valid_for_merchant_company_trigger = yes
		pool_character_is_pruneable_trigger = no
		gdp_merchant_family_has_no_ruler_trigger = yes
	}

	immediate = {
		change_global_variable = {
			name = gdp_minority_merchant_spawn_quota
			add = -1
		}
		gdp_minority_merchant_found_company_effect = yes
	}
}

gdp_merchant.4003 = {
	hidden = yes
	trigger = {
		gdp_minority_merchant_host_trigger = yes
		gdp_merchant_companies_below_cap_trigger = yes
	}

	immediate = {
		while = {
			count = 3
			if = {
				limit = { NOT = { exists = scope:minority_merchant_founder } }
				create_character = {
					template = pool_repopulate_local_flavor
					location = root.capital_province
					save_scope_as = minority_merchant
				}
				scope:minority_merchant = {
					if = {
						limit = { gdp_minority_merchant_candidate_trigger = yes }
						create_dynasty = {
							spread_to_descendants = yes
						}
					}
					if = {
						limit = {
							gdp_minority_merchant_candidate_trigger = yes
							gdp_pool_character_valid_for_merchant_company_trigger = yes
						}
						save_scope_as = minority_merchant_founder
						gdp_minority_merchant_found_company_effect = yes
					}
					else = {
						silent_disappearance_effect = yes
					}
				}
			}
		}
	}
}

# You are a Merchant in China who isn't Confucian
gdp_merchant.4000 = {
	type = character_event
	title = gdp_merchant.4000.t
	desc = {
		desc = gdp_merchant.4000.desc.intro
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:patron = { this = scope:market_character }
				}
				desc = gdp_merchant.4000.desc.patron
			}
			desc = gdp_merchant.4000.desc.patron_representative
		}
		desc = gdp_merchant.4000.desc.body
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:patron = { this = scope:market_character }
				}
				desc = gdp_merchant.4000.desc.body.patron
			}
			desc = gdp_merchant.4000.desc.body.patron_representative
		}
		desc = gdp_merchant.4000.desc.outro
	}

	theme = merchant

	left_portrait = {
		character = root
		triggered_animation = {
			trigger = {
				OR = {
					has_trait = arbitrary
					has_trait = arrogant
					has_trait = ambitious
				}
			}
			animation = anger
		}
		triggered_animation = {
			trigger = {
				OR = {
					has_trait = shy
					has_trait = craven
				}
			}
			animation = worry
		}
		animation = disbelief
		camera = camera_event_crowd
	}

	right_portrait = {
		character = scope:market_character
		animation = disapproval
	}

	trigger = {
		is_house_head = yes
		is_available_adult = yes
		government_has_flag = government_is_merchant
		primary_title ?= {
			NOT = { has_variable = had_event_gdp_merchant_4000 }
		}
		location = {
			OR = {			
		        geographical_region = world_asia_china
		        geographical_region = world_asia_korea
			}
		}
		NOT = {
			has_trait = confucian_education
		}
		suzerain = {
			government_has_flag = government_has_merit
			NOT = {
				has_any_good_relationship_with_character_trigger = { CHARACTER = root }
			}
			OR = {
				AND = {
					is_available_ai_adult = yes
					highest_held_title_tier < tier_kingdom
				}
				any_councillor = {
					is_available_ai_adult = yes
					this != root
				}
				any_courtier = { is_available_ai_adult = yes }
			}
		}
	}

	weight_multiplier = {
		modifier = { # Increase weight early on
			add = {
				value = 100
				subtract = {
					value = years_as_ruler
					multiply = 10
				}
				min = 0 # avoid -1
			}
		}
	}

	immediate = {
		suzerain = {
			save_scope_as = patron
			if = {
				limit = {
					is_available_ai_adult = yes
					highest_held_title_tier < tier_kingdom
				}
				save_scope_as = market_character
			}
			else_if = {
				limit = {
					any_councillor = {
						is_available_ai_adult = yes
						this != root
					}
				}
				random_councillor = {
					limit = {
						is_available_ai_adult = yes
						this != root
					}
					weight = {
						base = 1
						modifier = {
							factor = 2
							is_lowborn = no
						}
						modifier = {
							factor = 2
							has_trait = confucian_education
						}
					}
					save_scope_as = market_character
				}
			}
			else = {
				random_courtier = {
					limit = { is_available_ai_adult = yes }
					save_scope_as = market_character
				}
			}
		}
		# Put a cooldown on the primary title - the effects are pretty OP so let's not dole them out willy nilly
		primary_title ?= { set_variable = had_event_gdp_merchant_4000 }
	}

	option = { # I should learn about the Way
		name = gdp_merchant.4000.b
		custom_tooltip = gdp_merchant.4000.b.tt
		add_character_flag = gdp_merchant_confucian_university_discount
		if = {
			limit = {
				NOT = { knows_language_of_culture = scope:market_character.culture }
			}
			start_scheme = {
				type = learn_language
				target_character = scope:market_character
			}
		}
		else_if = {
			limit = { has_trait = confucian_education }
			add_trait_xp = {
				trait = confucian_education
				value = { 1 3 }
			}
		}
		else = {
			random = {
				chance = root.learning
				add_trait = confucian_education
			}
		}
		stress_and_fulfillment_impact = {
			base = medium_stress_impact_gain
			diligent = medium_stress_impact_loss
			patient = medium_stress_impact_loss
			impatient = medium_stress_impact_gain
			arrogant = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_sociability = 0.5
				ai_energy = 1
			}
			modifier = {
				OR = {
					has_trait = impatient
					has_trait = arrogant
				}
				factor = 0
			}
		}
	}

	option = { # I will focus all my efforts on the study of trade, thank you very much.
		name = gdp_merchant.4000.c
		add_character_modifier = {
			modifier = dedicated_trade_studies
			years = 10
		}
		reverse_add_opinion = {
			modifier = insulted_opinion
			target = scope:market_character
		}
		stress_and_fulfillment_impact = {
			stubborn = medium_stress_impact_loss
			callous = medium_stress_impact_loss
			shy = medium_stress_impact_gain
			craven = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_vengefulness = 1
			}
			modifier = {
				OR = {
					has_trait = shy
					has_trait = craven
				}
				factor = 0
			}
		}
	}
}

# You're a Merchant in China/Korea who hasn't taken the exam
gdp_merchant.4010 = {
	type = character_event
	title = gdp_merchant.4010.t
	desc = gdp_merchant.4010.desc

	theme = merchant
	override_background = tavern

	left_portrait = {
		character = root
		triggered_animation = {
			trigger = {
				OR = {
					has_trait = arbitrary
					has_trait = arrogant
					has_trait = ambitious
				}
			}
			animation = anger
		}
		triggered_animation = {
			trigger = {
				OR = {
					has_trait = shy
					has_trait = craven
				}
			}
			animation = worry
		}
		animation = disbelief
		camera = camera_event_crowd
	}

	right_portrait = {
		character = scope:student
		animation = dismissal
	}

	trigger = {
		is_house_head = yes
		is_available_adult = yes
		government_has_flag = government_is_merchant
		primary_title ?= {
			NOT = { has_variable = had_event_gdp_merchant_4010 }
		}
		location = {
			OR = {			
		        geographical_region = world_asia_china
		        geographical_region = world_asia_korea
			}
		}
		NOR = {
			has_character_flag = passed_provincial_exam
			has_character_flag = passed_metropolitan_exam
			has_character_flag = passed_palace_exam
		}
	}

	immediate = {
		if = {
			limit = {
				any_pool_character = {
					is_available_ai_adult = yes
					province = root.location
					OR = {
						has_character_flag = passed_provincial_exam
						has_character_flag = passed_metropolitan_exam
					}
				}
			}
			random_pool_character = {
				province = root.location
				limit = {
					is_available_ai_adult = yes
					OR = {
						has_character_flag = passed_provincial_exam
						has_character_flag = passed_metropolitan_exam
					}
				}
				save_scope_as = student
			}
		}
		else = {
			root.location.county.holder ?= { save_scope_as = host }
			create_character = {
				template = tgp_examiner_learning_template
				dynasty = none
				location = root.location
				faith = root.location.faith
				culture = root.location.culture
				gender = male
				save_scope_as = student
			    after_creation = {
			        add_character_flag = created
			        add_character_flag = passed_provincial_exam
			    }
			}
		}
		# Put a cooldown on the primary title - the effects are pretty OP so let's not dole them out willy nilly
		primary_title ?= { set_variable = had_event_gdp_merchant_4010 }
	}

	option = { # Maybe I should take that exam...
		name = gdp_merchant.4010.a
		trigger = { has_tgp_dlc_trigger = yes }
		custom_tooltip = preparatory.imperial_examination.advantage.tt
		imperial_examination_adjust_advantage_variable_effect = yes
		if = {
			limit = { has_trait = confucian_education }
			add_trait_xp = {
				trait = confucian_education
				value = { 1 3 }
			}
		}
		else = {
			random = {
				chance = root.learning
				add_trait = confucian_education
			}
		}
		stress_and_fulfillment_impact = {
			base = medium_stress_impact_gain
			diligent = medium_stress_impact_loss
			patient = medium_stress_impact_loss
			impatient = medium_stress_impact_gain
			arrogant = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_sociability = 0.5
				ai_energy = 1
			}
			modifier = {
				OR = {
					has_trait = impatient
					has_trait = arrogant
				}
				factor = 0
			}
		}
	}

	option = { # And whose tea are you drinking?
		name = gdp_merchant.4010.b
		trigger = {
			any_trade_route = {
				any_traded_good = { this = trade_good_type:trade_good_tea }
			}
		}
		add_prestige = medium_prestige_gain
		stress_and_fulfillment_impact = {
			stubborn = medium_stress_impact_loss
			callous = medium_stress_impact_loss
			shy = medium_stress_impact_gain
			craven = medium_stress_impact_gain
		}
		ai_chance = {
			base = 1000
		}
	}

	option = { # Ignore them.
		name = gdp_merchant.4010.c
		trigger = {
			NOT = {
				any_trade_route = {
					any_traded_good = { this = trade_good_type:trade_good_tea }
				}
			}
		}
		add_prestige = minor_prestige_gain
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_vengefulness = 1
			}
			modifier = {
				OR = {
					has_trait = shy
					has_trait = craven
				}
				factor = 0
			}
		}
	}

	after = {
		scope:student = { silent_disappearance_ai_if_created_effect = yes }
	}
}

# You join the Silk Road
gdp_merchant.4020 = {
	type = character_event
	title = gdp_merchant.4020.t
	desc = {
		desc = gdp_merchant.4020.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:silk_road_sub_region = {
						sub_region_current_phase = phase_exceptional_bounty
					}
				}
				desc = gdp_merchant.4020.desc.exceptional_bounty
			}
			triggered_desc = {
				trigger = {
					scope:silk_road_sub_region = {
						sub_region_current_phase = phase_hardship
					}
				}
				desc = gdp_merchant.4020.desc.hardship
			}
			desc = gdp_merchant.4020.desc.steady_trading
		}
	}

	theme = silk_road
	override_effect_2d = {
		trigger = {
			scope:silk_road_sub_region = {
				OR = {
					sub_region_current_phase = phase_exceptional_bounty
					sub_region_current_phase = phase_steady_trading
				}
			}
		}
		reference = legend_glow
	}
	override_sound = { reference = "event:/DLC/EP5/SFX/Stingers/ep5_mx_sting_merchant_joins_silk_road" }

	left_portrait = {
		character = root
		animation = steward
	}

	trigger = {
		is_ai = no
		current_date_is_start_date_trigger = no # Only for Merchants who reach the Silk Road during play
		primary_title ?= {
			NOT = { has_variable = had_event_gdp_merchant_4020 }
		}
		exists = scope:silk_road_sub_region
	}

	immediate = {
		primary_title ?= { set_variable = had_event_gdp_merchant_4020 }
	}

	option = {
		name = gdp_merchant.4020.a
	}
}

# You're a Merchant outside of the Silk Road hearing about it
gdp_merchant.4030 = {
	type = character_event
	title = gdp_merchant.4030.t
	desc = {
		desc = gdp_merchant.4030.desc.intro
		# Fetch an inticing trade good
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:rumoured_good ?= { this = trade_good_type:trade_good_silk }
				}
				desc = gdp_merchant.4030.desc.silk
			}
			triggered_desc = {
				trigger = {
					scope:rumoured_good ?= { this = trade_good_type:trade_good_spices }
				}
				desc = gdp_merchant.4030.desc.spice
			}
			triggered_desc = {
				trigger = {
					scope:rumoured_good ?= { this = trade_good_type:trade_good_jade }
				}
				desc = gdp_merchant.4030.desc.jade
			}
			triggered_desc = {
				trigger = {
					scope:rumoured_good ?= { this = trade_good_type:trade_good_ivory }
				}
				desc = gdp_merchant.4030.desc.ivory
			}
		}
		desc = gdp_merchant.4030.desc.outro
	}

	theme = merchant
	override_background = study

	left_portrait = {
		character = root
		animation = merchant_box
	}

	right_portrait = {
		character = scope:associate
		animation = storyteller
	}

	artifact = {
		trigger = { exists = scope:rumoured_curio }
		target = scope:rumoured_curio
		position = lower_right_portrait
	}

	trigger = {
		is_house_head = yes
		is_available_adult = yes
		government_has_flag = government_is_merchant
		years_from_game_start >= 2
		num_taken_task_contracts < 1
		primary_title ?= {
			NOT = { has_variable = had_event_gdp_merchant_4030 }
		}
		# We gotta have a messenger
		any_courtier = { gdp_merchant_1200_requester_trigger = yes }
		exists = situation:silk_road_situation
		# Not on the road already
		NOR = {
			situation:silk_road_situation ?= {
				any_participant_group = {
					participant_group_type = silk_road_merchant
					participant_group_has_character = root
				}
			}
			any_trade_route = {
				any_trade_market_on_route = {
					is_target_in_global_variable_list = {
						name = silk_road_markets
						target = this
					}
				}
			}
		}
		# There has to be a good worth rumouring about
		any_in_global_list = {
			variable = silk_road_markets
			is_rumourable_entrepot_market_trigger = { MERCHANT = root }
		}
	}

	weight_multiplier = {
		base = 1
		modifier = { 
			factor = 3
			any_trade_route = {
				any_traded_good = { is_interesting_entrepot_good_trigger = yes }
			}
		}
	}

	immediate = {
		if = {
			limit = {
				any_courtier = {
					has_court_position = partner_court_position
					gdp_merchant_1200_requester_trigger = yes
				}
			}
			random_courtier = {
				limit = {
					has_court_position = partner_court_position
					gdp_merchant_1200_requester_trigger = yes
				}
				save_scope_as = associate
			}
		}
		else = {
			ordered_courtier = {
				limit = { gdp_merchant_1200_requester_trigger = yes }
				order_by = "opinion(root)"
				save_scope_as = associate
			}
		}
		domicile.domicile_location = { save_scope_as = company_location }
		# Any market on the Silk Road can be the one tempting us away, not just the great entrepots.
		every_in_global_list = {
			variable = silk_road_markets
			limit = { is_rumourable_entrepot_market_trigger = { MERCHANT = root } }
			add_to_list = rumourable_markets
		}
		situation:silk_road_situation ?= {
			every_situation_county = {
				limit = {
					title_province.barony.trade_market ?= {
						is_rumourable_entrepot_market_trigger = { MERCHANT = root }
					}
				}
				title_province.barony.trade_market ?= { add_to_list = rumourable_markets }
			}
		}
		ordered_in_list = {
			list = rumourable_markets
			order_by = { # Ordered lists take the highest value first, so invert the distance
				value = "trade_market_owner.title_province.squared_distance(scope:company_location)"
				multiply = -1
			}
			position = 0
			check_range_bounds = no
			save_scope_as = rumoured_market
			trade_market_owner.title_province = { save_scope_as = rumoured_market_location }
			trade_market_owner.holder ?= { save_scope_as = rumoured_market_owner }
			random_trade_good_with_surplus = {
				limit = { is_interesting_entrepot_good_trigger = yes }
				save_scope_as = rumoured_good
			}
		}
		# The Associate bought the curio, so it is theirs until we ask for it
		scope:associate = {
			hidden_effect = {
				switch = {
					trigger = scope:rumoured_good
					trade_good_type:trade_good_silk = {
						create_artifact = {
							name = gdp_merchant.4030.artifact.silk
							description = gdp_merchant.4030.artifact.silk.desc
							type = miscellaneous
							visuals = fabric_roll
							modifier = artifact_monthly_prestige_2_modifier
							wealth = 50
							quality = 50
							max_durability = 30
							generate_history = no
							save_scope_as = rumoured_curio
						}
					}
					trade_good_type:trade_good_spices = {					
						create_artifact = {
							name = gdp_merchant.4030.artifact.spice
							description = gdp_merchant.4030.artifact.spice.desc
							type = miscellaneous
							visuals = pouch
							modifier = artifact_monthly_prestige_2_modifier
							wealth = 50
							quality = 50
							max_durability = 30
							generate_history = no
							save_scope_as = rumoured_curio
						}
					}
					trade_good_type:trade_good_jade = {					
						create_artifact = {
							name = gdp_merchant.4030.artifact.jade
							description = gdp_merchant.4030.artifact.jade.desc
							type = miscellaneous
							visuals = small_box
							modifier = artifact_monthly_prestige_2_modifier
							wealth = 50
							quality = 50
							max_durability = 30
							generate_history = no
							save_scope_as = rumoured_curio
						}
					}
					trade_good_type:trade_good_ivory = {					
						create_artifact = {
							name = gdp_merchant.4030.artifact.ivory
							description = gdp_merchant.4030.artifact.ivory.desc
							type = miscellaneous
							visuals = small_box
							modifier = artifact_monthly_prestige_2_modifier
							wealth = 50
							quality = 50
							max_durability = 30
							generate_history = no
							save_scope_as = rumoured_curio
						}
					}
				}
			}
		}
		primary_title ?= { set_variable = had_event_gdp_merchant_4030 }
	}

	option = {
		name = gdp_merchant.4030.a
		custom_tooltip = gdp_merchant_4030_a_tt
		show_as_tooltip = {
			reverse_add_opinion = {
				target = scope:associate
				modifier = pleased_opinion
				opinion = 20
			}
			house = {
				add_unity_value = {
					value = major_unity_gain
					character = root
					desc = merchant_associate_request_fulfill_gain
				}
			}
		}
		custom_tooltip = action_associate_request_help
		add_to_variable_list = {
			name = associate_requests
			target = scope:associate
			years = 5
		}
		scope:associate = {
			add_character_flag = {
				flag = associate_request_cooldown
				years = 10
			}
			set_variable = {
				name = associate_request_type
				value = flag:silk_road_market
			}
			set_variable = {
				name = associate_request_merchant
				value = root
				years = 6
			}
			set_variable = {
				name = associate_request_target_market
				value = scope:rumoured_market
				years = 5
			}
			set_variable = {
				name = associate_request_target_location
				value = scope:rumoured_market_location
				years = 5
			}
			trigger_event = {
				id = gdp_merchant.1201
				years = 5
			}
		}

		stress_and_fulfillment_impact = {
			ambitious = medium_stress_impact_loss
			greedy = minor_stress_impact_loss
			content = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 1
				ai_greed = 1
			}
			modifier = {
				add = 50
				has_trait = ambitious
			}
			modifier = {
				factor = 0.5
				has_trait = content
			}
			modifier = { # Even the closest market can be a world away
				factor = 0.25
				scope:rumoured_market_location = {
					squared_distance = {
						target = scope:company_location
						value > squared_distance_massive
					}
				}
			}
		}
	}

	option = { # Nice artifact, lemme have it.
		name = gdp_merchant.4030.b
		trigger = { exists = scope:rumoured_curio }
		scope:rumoured_curio = { set_owner = root }

		stress_and_fulfillment_impact = {
			greedy = minor_stress_impact_loss
			generous = minor_stress_impact_gain
		}
		ai_chance = {
			base = 150
			ai_value_modifier = {
				ai_greed = 1
				ai_boldness = -1
			}
		}
	}

	option = { # We have important business here actually
		name = gdp_merchant.4030.c
		add_character_modifier = {
			modifier = gdp_local_business_modifier
			years = 5
		}
		house ?= {
			add_unity_value = {
				value = minor_unity_loss
				character = root
				desc = merchant_unity_local_business
			}
		}

		stress_and_fulfillment_impact = {
			content = minor_stress_impact_loss
			craven = minor_stress_impact_loss
			ambitious = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = -1
				ai_energy = -1
			}
			modifier = {
				add = 50
				has_trait = content
			}
			modifier = {
				factor = 0.5
				has_trait = ambitious
			}
		}
	}
}

# gdp_merchant.5000 - Usurer's Penance

# Accuser's rite treats this loan as a religious offense.
scripted_trigger gdp_usury_penance_shunned_doctrine_trigger = {
	OR = {
		rite = { rite_has_parameter = usury_forbidden }
		AND = {
			rite = { rite_has_parameter = usury_outsiders_only }
			religion = root.religion
		}
	}
}

scripted_trigger gdp_usury_penance_discouraged_doctrine_trigger = {
	rite = { rite_has_parameter = usury_discouraged }
}

scripted_trigger gdp_usury_penance_accepted_doctrine_trigger = {
	gdp_usury_penance_shunned_doctrine_trigger = no
	gdp_usury_penance_discouraged_doctrine_trigger = no
}

scripted_effect gdp_usury_penance_cast_fallback_clergy_effect = {
	if = {
		limit = {
			NOT = { exists = scope:accuser }
		}
		random_pool_character = {
			province = scope:debtor.location
			limit = {
				is_adult = yes
				faith = scope:debtor.faith
				rite = scope:debtor.rite
				OR = {
					has_trait = cleric
					has_trait = devoted
					has_trait = education_learning_1
					has_trait = education_learning_2
					has_trait = education_learning_3
					has_trait = education_learning_4
				}
			}
			save_scope_as = accuser
		}
	}
	if = {
		limit = { NOT = { exists = scope:accuser } }
		scope:debtor = { save_scope_as = monk_employer }
		create_character = {
			location = scope:debtor.location
			template = pam_monk_courtier_template
			save_scope_as = accuser
			after_creation = {
				add_character_flag = created
			}
		}
	}
}

# Usurer's Penance
gdp_merchant.5000 = {
	type = character_event
	title = gdp_merchant.5000.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					var:gdp_usury_penance_accuser_type = flag:debtor
					scope:debtor = { has_trait = paranoid }
				}
				desc = gdp_merchant.5000.desc.debtor_paranoid
			}
			triggered_desc = {
				trigger = { var:gdp_usury_penance_accuser_type = flag:debtor }
				desc = gdp_merchant.5000.desc.debtor
			}
			triggered_desc = {
				trigger = { var:gdp_usury_penance_accuser_type = flag:steward }
				desc = gdp_merchant.5000.desc.steward
			}
			triggered_desc = {
				trigger = { var:gdp_usury_penance_accuser_type = flag:bishop }
				desc = gdp_merchant.5000.desc.bishop
			}
			desc = gdp_merchant.5000.desc.clergy
		}
		desc = gdp_merchant.5000.desc.break
		first_valid = {
			triggered_desc = {
				trigger = {
					var:gdp_usury_penance_accuser_type = flag:debtor
					scope:accuser = { gdp_usury_penance_shunned_doctrine_trigger = yes }
				}
				desc = gdp_merchant.5000.desc.shunned.debtor
			}
			triggered_desc = {
				trigger = {
					var:gdp_usury_penance_accuser_type = flag:debtor
					scope:accuser = { gdp_usury_penance_discouraged_doctrine_trigger = yes }
				}
				desc = gdp_merchant.5000.desc.discouraged.debtor
			}
			triggered_desc = {
				trigger = {
					var:gdp_usury_penance_accuser_type = flag:steward
					scope:accuser = { gdp_usury_penance_shunned_doctrine_trigger = yes }
				}
				desc = gdp_merchant.5000.desc.shunned.steward
			}
			triggered_desc = {
				trigger = {
					var:gdp_usury_penance_accuser_type = flag:steward
					scope:accuser = { gdp_usury_penance_discouraged_doctrine_trigger = yes }
				}
				desc = gdp_merchant.5000.desc.discouraged.steward
			}
			triggered_desc = {
				trigger = {
					scope:accuser = { gdp_usury_penance_shunned_doctrine_trigger = yes }
				}
				desc = gdp_merchant.5000.desc.shunned
			}
			triggered_desc = {
				trigger = {
					scope:accuser = { gdp_usury_penance_discouraged_doctrine_trigger = yes }
				}
				desc = gdp_merchant.5000.desc.discouraged
			}
			triggered_desc = {
				trigger = {
					var:gdp_usury_penance_accuser_type = flag:steward
				}
				desc = gdp_merchant.5000.desc.accepted.steward
			}
			desc = gdp_merchant.5000.desc.accepted
		}
	}
	theme = merchant
	override_background = corridor_night
	left_portrait = {
		character = root
		triggered_animation = {
			trigger = {
				OR = {
					has_trait = arrogant
					has_trait = stubborn
				}
			}
			animation = dismissal
		}
		animation = worry
	}
	right_portrait = {
		character = scope:accuser
		triggered_animation = {
			trigger = { scope:accuser = { has_trait = paranoid } }
			animation = paranoia
		}
		triggered_animation = {
			trigger = { scope:accuser = { has_trait = vengeful } }
			animation = personality_vengeful
		}
		triggered_animation = {
			trigger = { scope:accuser = { has_trait = wrathful } }
			animation = anger
		}
		triggered_animation = {
			trigger = { scope:accuser = { has_trait = cynical } }
			animation = personality_cynical
		}
		triggered_animation = {
			trigger = {
				root = {
					OR = {
						var:gdp_usury_penance_accuser_type = flag:bishop
						var:gdp_usury_penance_accuser_type = flag:clergy
					}
				}
			}
			animation = anger
		}
		animation = disapproval
	}
	lower_right_portrait = {
		character = scope:debtor
		trigger = { this != scope:accuser }
	}

	trigger = {
		is_alive = yes
		exists = var:gdp_usury_penance_debtor
		exists = var:gdp_usury_penance_loan
		var:gdp_usury_penance_debtor = {
			is_alive = yes
			is_ai = yes
		}
		var:gdp_usury_penance_loan = {
			exists = var:instrument
			var:instrument = flag:disguised
			var:lender = root
		}
	}

	immediate = {
		play_music_cue = "mx_cue_negative"
		var:gdp_usury_penance_debtor = { save_scope_as = debtor }
		var:gdp_usury_penance_loan = { save_scope_as = loan }
		scope:debtor = {
			# We're not letting clergy speak for the debtor if they have a different faith from us or if they accept usury.
			if = {
				limit = {
					OR = {
						faith != root.faith
						gdp_usury_penance_accepted_doctrine_trigger = yes
					}
				}
				# But they have to be available, actually. And not waaay above our station.
				if = {
					limit = {
						highest_held_title_tier < tier_empire
						is_available_ai_adult = yes
					}
					save_scope_as = accuser
					root = {
						set_variable = {
							name = gdp_usury_penance_accuser_type
							value = flag:debtor
						}
						set_variable = gdp_usury_penance_accuser_is_debtor
					}
				}
				# If they send a representative, we prefer the steward.
				else = {
					if = {
						limit = {
							cp:councillor_steward ?= {
								is_available_adult = yes
								this != root
								this != scope:debtor
							}
						}
						cp:councillor_steward ?= { save_scope_as = accuser }
					}
					else = {
						ordered_courtier = {
							limit = {
								is_available_healthy_ai_adult = yes
								this != root
							}
							order_by = stewardship
							save_scope_as = accuser
						}
					}
					root = {
						set_variable = {
							name = gdp_usury_penance_accuser_type
							value = flag:steward
						}
					}
				}
			}
			# Even if the debtor shares our faith, they might want to handle the matter themselves.
			else_if = {
				limit = {
					is_adult = yes
					is_available = yes
					OR = {
						has_trait = paranoid
						has_trait = vengeful
						has_trait = wrathful
						has_trait = cynical
					}
				}
				save_scope_as = accuser
				root = {
					set_variable = {
						name = gdp_usury_penance_accuser_type
						value = flag:debtor
					}
					set_variable = gdp_usury_penance_accuser_is_debtor
				}
			}
			# Find a suitable religious char
			else = {
				capital_county.clerical_region_title.holder ?= {
					if = {
						limit = {
							is_available_adult = yes
							rite = scope:debtor.rite
							this != root
							this != scope:debtor
						}
						save_scope_as = accuser
					}
				}
				if = {
					limit = {
						NOT = { exists = scope:accuser }
					}
					cp:councillor_court_chaplain ?= {
						if = {
							limit = {
								is_available_adult = yes
								rite = scope:debtor.rite
								this != root
								this != scope:debtor
							}
							save_scope_as = accuser
						}
					}
				}
				if = {
					limit = { NOT = { exists = scope:accuser } }
					gdp_usury_penance_cast_fallback_clergy_effect = yes
				}
				# Bishop for theocracies, priest otherwise. To reference in loc.
				root = {
					if = {
						limit = {
							scope:accuser = { government_has_flag = government_is_theocracy }
						}
						set_variable = {
							name = gdp_usury_penance_accuser_type
							value = flag:bishop
						}
					}
					else = {
						set_variable = {
							name = gdp_usury_penance_accuser_type
							value = flag:clergy
						}
					}
				}
			}
			# Steward and courtier picks can both miss. Cast clergy so the portrait, loc, and duel have a target.
			if = {
				limit = { NOT = { exists = scope:accuser } }
				gdp_usury_penance_cast_fallback_clergy_effect = yes
				root = {
					if = {
						limit = {
							scope:accuser = { government_has_flag = government_is_theocracy }
						}
						set_variable = {
							name = gdp_usury_penance_accuser_type
							value = flag:bishop
						}
					}
					else = {
						set_variable = {
							name = gdp_usury_penance_accuser_type
							value = flag:clergy
						}
					}
				}
			}
		}
	}

	# My patron's charter puts me above this.
	option = {
		name = {
			trigger = { scope:accuser = root.suzerain }
			text = gdp_merchant.5000.a.debtor
		}
		name = {
			trigger = { scope:accuser != root.suzerain }
			text = gdp_merchant.5000.a
		}
		trigger = {
			merchant_has_charter_from_patron_trigger = {
				EQUIPPED = no
				INCLUDE_FORGERIES = no
			}
		}
		reason = charter
		custom_tooltip = gdp_usury_penance_loan_stands_tt
		# Same patron reaction as abuse_charter_authority_decision.
		if = {
			limit = { exists = domicile.domicile_location }
			merchant_charter_authority_scope_effect = {
				PATRON = root.suzerain
				MERCHANT = root
			}
		}
		merchant_charter_abuse_counter_effect = {
			PATRON = root.suzerain
			MERCHANT = root
		}
		stress_and_fulfillment_impact = {
			arrogant = minor_stress_impact_loss
			humble = minor_stress_impact_gain
			greedy = minor_stress_impact_loss
		}
		ai_chance = {
			base = 30
			ai_value_modifier = {
				ai_greed = 0.5
				ai_boldness = 0.25
				ai_honor = -0.5
			}
		}
	}

	# Settle in goods, keep the debt.
	option = {
		name = gdp_merchant.5000.b
		custom_tooltip = gdp_usury_penance_loan_stands_tt
		add_character_modifier = {
			modifier = gdp_usury_penance_seized_goods_modifier
			years = 10
		}
		add_prestige = medium_prestige_loss
		stress_and_fulfillment_impact = {
			greedy = minor_stress_impact_loss
			generous = minor_stress_impact_gain
			cynical = minor_stress_impact_loss
			zealous = minor_stress_impact_gain
		}
		ai_chance = {
			base = 20
			ai_value_modifier = {
				ai_greed = 0.75
				ai_honor = 0.25
			}
		}
	}

	option = { # Check the fine print, actually...
		name = gdp_merchant.5000.c
		# Regardless of outcome, this will sting piety-wise
		if = {
			limit = {
				gdp_usury_forbids_loan_trigger = { BORROWER = scope:debtor }
			}
			add_piety = medium_piety_loss
		}
		else_if = {
			limit = {
				gdp_usury_condemns_loan_trigger = { BORROWER = scope:debtor }
			}
			add_piety = minor_piety_loss
		}
		duel = {
			skills = { diplomacy stewardship }
			target = scope:accuser
			45 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -35
				}
				modifier = {
					add = -15
					scope:accuser = {
						gdp_usury_condemns_loan_trigger = { BORROWER = scope:debtor }
					}
				}
				modifier = {
					add = -15
					faith = scope:debtor.faith
				}
				desc = gdp_merchant.5000.c.success
				custom_tooltip = gdp_usury_penance_loan_stands_tt
			}
			40 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -1.75
					min = -30
				}
				desc = gdp_merchant.5000.c.failure
				scope:loan = {
					save_temporary_scope_value_as = {
						name = principal_temp
						value = gdp_loan_disguised_principal_value
					}
					set_variable = {
						name = amount_owed
						value = scope:principal_temp
					}
					set_variable = {
						name = instrument
						value = flag:free
					}
				}
				custom_description = {
					text = gdp_usury_penance_forgive_interest_tt
					subject = root
					object = scope:debtor
					value = scope:principal_temp
				}
				if = {
					limit = {
						any_secret = {
							type = secret_disguised_usury
							secret_target = scope:debtor
						}
					}
					random_secret = {
						type = secret_disguised_usury
						limit = { secret_target = scope:debtor }
						remove_secret = yes
					}
				}
			}
			15 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -1.75
					min = -10
				}
				modifier = {
					add = 10
					scope:accuser = {
						gdp_usury_condemns_loan_trigger = { BORROWER = scope:debtor }
					}
				}
				modifier = {
					add = 10
					faith = scope:debtor.faith
				}
				desc = gdp_merchant.5000.c.critical
				if = {
					limit = {
						NOT = { has_trait = dealbreaker }
					}
					add_trait = dealbreaker
				}
				if = {
					limit = { faith = scope:debtor.faith }
					add_character_modifier = {
						modifier = gdp_usury_penance_same_faith_scandal_modifier
						years = 10
					}
				}
				hidden_effect = {
					if = {
						limit = {
							any_secret = {
								type = secret_disguised_usury
								secret_target = scope:debtor
							}
						}
						random_secret = {
							type = secret_disguised_usury
							limit = { secret_target = scope:debtor }
							remove_secret = yes
						}
					}
				}
				scope:loan = { forgive_loan_effect = yes }
			}
		}
		stress_and_fulfillment_impact = {
			deceitful = minor_stress_impact_loss
			honest = minor_stress_impact_gain
			arrogant = minor_stress_impact_loss
			craven = minor_stress_impact_gain
		}
		ai_chance = {
			base = 25
			ai_value_modifier = {
				ai_boldness = 0.5
				ai_rationality = 0.25
				ai_honor = -0.25
			}
			modifier = {
				factor = 0.5
				faith = scope:debtor.faith
			}
		}
	}

	# Forgive the interest, keep the principal. Hidden unless the rite accepts usury.
	option = {
		name = gdp_merchant.5000.d
		trigger = {
			scope:debtor = { gdp_usury_penance_accepted_doctrine_trigger = yes }
		}
		# The forgiving
		scope:loan = {
			save_temporary_scope_value_as = {
				name = principal_temp
				value = gdp_loan_disguised_principal_value
			}
			set_variable = {
				name = amount_owed
				value = scope:principal_temp
			}
			set_variable = {
				name = instrument
				value = flag:free
			}
		}
		custom_description = {
			text = gdp_usury_penance_forgive_interest_tt
			subject = root
			object = scope:debtor
			value = scope:principal_temp
		}
		hidden_effect = {
			if = {
				limit = {
					any_secret = {
						type = secret_disguised_usury
						secret_target = scope:debtor
					}
				}
				random_secret = {
					type = secret_disguised_usury
					limit = { secret_target = scope:debtor }
					remove_secret = yes
				}
			}
		}
		stress_and_fulfillment_impact = {
			honest = minor_stress_impact_loss
			deceitful = minor_stress_impact_gain
			generous = minor_stress_impact_loss
			greedy = minor_stress_impact_gain
		}
		ai_chance = {
			base = 40
			ai_value_modifier = {
				ai_honor = 0.5
				ai_rationality = 0.5
				ai_greed = -0.25
			}
		}
	}

	# Wipe the debt. Only when the rite shuns or discourages usury.
	option = {
		name = gdp_merchant.5000.e
		trigger = {
			scope:debtor = {
				OR = {
					gdp_usury_penance_shunned_doctrine_trigger = yes
					gdp_usury_penance_discouraged_doctrine_trigger = yes
				}
			}
		}
		scope:debtor = {
			add_opinion = {
				target = root
				modifier = gdp_interest_free_loan_opinion
				opinion = low_positive_opinion
			}
		}
		if = {
			limit = { faith = scope:debtor.faith }
			add_piety = minor_piety_gain
		}
		# Cancel loan
		hidden_effect = {
			if = {
				limit = {
					any_secret = {
						type = secret_disguised_usury
						secret_target = scope:debtor
					}
				}
				random_secret = {
					type = secret_disguised_usury
					limit = { secret_target = scope:debtor }
					remove_secret = yes
				}
			}
		}
		scope:loan = { forgive_loan_effect = yes }
		stress_and_fulfillment_impact = {
			generous = medium_stress_impact_loss
			greedy = medium_stress_impact_gain
			zealous = minor_stress_impact_loss
			cynical = minor_stress_impact_gain
			compassionate = minor_stress_impact_loss
			callous = minor_stress_impact_gain
		}
		ai_chance = {
			base = 15
			ai_value_modifier = {
				ai_compassion = 0.75
				ai_honor = 0.5
				ai_greed = -1
			}
		}
	}

	on_trigger_fail = {
		remove_variable = gdp_usury_penance_debtor
		remove_variable = gdp_usury_penance_loan
	}

	after = {
		remove_variable = gdp_usury_penance_debtor
		remove_variable = gdp_usury_penance_loan
		remove_variable = gdp_usury_penance_accuser_type
		remove_variable = gdp_usury_penance_accuser_is_debtor
		scope:accuser ?= {
			silent_disappearance_ai_if_created_effect = yes
		}
	}
}

# gdp_merchant.5010 - The debtor discovers the hidden interest.
gdp_merchant.5010 = {
	type = character_event
	title = gdp_merchant.5010.t
	desc = gdp_merchant.5010.desc
	theme = merchant
	override_background = { reference = study }
	left_portrait = {
		character = root
		animation = chancellor
	}
	lower_right_portrait = scope:lender

	trigger = {
		is_alive = yes
		exists = var:gdp_usury_debtor_discover_lender
		exists = var:gdp_usury_debtor_discover_loan
		var:gdp_usury_debtor_discover_lender = { is_alive = yes }
		var:gdp_usury_debtor_discover_loan = {
			exists = var:instrument
			var:instrument = flag:disguised
		}
		any_known_secret = { # Prevent firing if player exposed immediately via UI
			secret_type = secret_disguised_usury
			secret_owner = root.var:gdp_usury_debtor_discover_lender
			secret_target = root
		}
	}

	immediate = {
		var:gdp_usury_debtor_discover_lender = { save_scope_as = lender }
		var:gdp_usury_debtor_discover_loan = { save_scope_as = loan }
		save_scope_as = debtor
		hidden_effect = {
			scope:loan = { set_variable = gdp_usury_known }
		}
	}

	# Keep the disguised interest a secret.
	option = {
		name = gdp_merchant.5010.a
		custom_tooltip = gdp_usury_debtor_keep_secret_tt
		if = {
			limit = { scope:lender = { is_ai = no } }
			scope:lender = {
				send_interface_message = {
					type = event_secret_neutral
					title = gdp_usury_debtor_kept_secret_message
					desc = gdp_usury_debtor_kept_secret_message_desc
					left_icon = scope:debtor
				}
			}
		}
		stress_and_fulfillment_impact = {
			patient = minor_stress_impact_loss
			lazy = minor_stress_impact_loss
			impatient = minor_stress_impact_gain
		}
		ai_chance = {
			base = 30
			ai_value_modifier = {
				ai_boldness = -0.5
				ai_energy = -0.5
				ai_vengefulness = -0.25
			}
		}
	}

	# The creditor pays the hidden interest back.
	option = {
		name = { # Have paid back interest already
			text = gdp_merchant.5010.b
			trigger = { scope:loan.var:amount_owed < scope:loan.gdp_loan_less_interest_value }
		}
		name = { # Have not paid back interest
			text = gdp_merchant.5010.b.wipe
			trigger = { scope:loan.var:amount_owed >= scope:loan.gdp_loan_less_interest_value }
		}
		gdp_usury_debtor_discover_interest_tooltip_effect = yes
		if = {
			limit = { scope:lender = { is_ai = no } }
			custom_tooltip = gdp_usury_debtor_demand_repayment_tt
			custom_tooltip = gdp_usury_debtor_inform_creditor_tt
			scope:lender = {
				trigger_event = gdp_merchant.5020
			}
		}
		else = {
			scope:lender = {
				save_scope_value_as = {
					name = usury_skip_pay_toast
					value = yes
				}
				gdp_usury_pay_back_disguised_interest_effect = yes
			}
		}
		stress_and_fulfillment_impact = {
			greedy = minor_stress_impact_loss
			just = minor_stress_impact_loss
			arbitrary = minor_stress_impact_gain
			generous = minor_stress_impact_gain
		}
		ai_chance = {
			base = 40
			ai_value_modifier = {
				ai_greed = 1
				ai_vengefulness = 0.5
				ai_honor = 0.25
			}
		}
	}

	# Refuse the debt outright.
	option = {
		name = gdp_merchant.5010.c
		if = {
			limit = { scope:lender = { is_ai = no } }
			scope:loan = {
				var:lender ?= { save_scope_as = repudiated_lender }
				story_owner = { save_scope_as = repudiating_borrower }
			}
			custom_tooltip = repudiate_loan_tt
			custom_tooltip = gdp_repudiate_usurious_loan_credit_tt
			custom_tooltip = gdp_usury_debtor_inform_creditor_tt
			scope:lender = { trigger_event = gdp_merchant.5040 }
		}
		else = {
			scope:loan = { gdp_usury_repudiate_loan_effect = yes }
		}
		stress_and_fulfillment_impact = {
			greedy = minor_stress_impact_loss
			ambitious = minor_stress_impact_loss
			generous = minor_stress_impact_gain
			content = minor_stress_impact_gain
		}
		ai_chance = {
			base = 20
			ai_value_modifier = {
				ai_greed = 0.5
				ai_vengefulness = 0.5
				ai_honor = -0.75
			}
		}
	}

	after = {
		remove_variable = gdp_usury_debtor_discover_lender
		remove_variable = gdp_usury_debtor_discover_loan
	}
}

# gdp_merchant.5020 - The creditor answers a demand to repay the hidden interest.
gdp_merchant.5020 = {
	type = letter_event
	opening = gdp_merchant.5020.opening
	desc = gdp_merchant.5020.desc
	sender = scope:debtor

	trigger = {
		is_alive = yes
		exists = scope:debtor
		scope:debtor = { is_alive = yes }
		exists = scope:loan
		scope:loan = {
			exists = var:instrument
			var:instrument = flag:disguised
			var:lender = root
		}
	}

	option = {
		name = gdp_merchant.5020.a
		gdp_usury_pay_back_disguised_interest_effect = yes
		stress_and_fulfillment_impact = {
			generous = minor_stress_impact_loss
			just = minor_stress_impact_loss
			greedy = medium_stress_impact_gain
			arbitrary = minor_stress_impact_gain
		}
		ai_chance = {
			base = 50
			ai_value_modifier = {
				ai_honor = 0.5
				ai_compassion = 0.25
				ai_greed = -1
			}
		}
	}
}

# gdp_merchant.5040 - The debtor repudiates the loan.
gdp_merchant.5040 = {
	type = letter_event
	opening = gdp_merchant.5040.opening
	desc = gdp_merchant.5040.desc
	sender = scope:debtor

	trigger = {
		is_alive = yes
		exists = scope:debtor
		scope:debtor = { is_alive = yes }
		exists = scope:loan
		scope:loan = {
			exists = var:instrument
			var:instrument = flag:disguised
			var:lender = root
		}
	}

	option = {
		name = gdp_merchant.5040.a
		save_scope_value_as = {
			name = usury_skip_repudiate_toast
			value = yes
		}
		scope:loan = { gdp_usury_repudiate_loan_effect = yes }
	}
}

# Caravaneer capstone
gdp_merchant.8000 = {
	type = character_event
	title = gdp_merchant.8000.t
	desc = gdp_merchant.8000.desc
	theme = merchant
	left_portrait = {
		character = root
		animation = dromedary_holding
	}
	override_background = { reference = wilderness }

	immediate = {
		merchant_caravaneer_capstone_reward_effect = yes
	}

	option = {
		name = gdp_merchant.8000.a
		if = {
			limit = { exists = scope:nickname_choice }
			give_nickname = nick_the_caravaneer
		}
		ai_chance = { base = 100 }
	}

	option = {
		trigger = { exists = scope:nickname_choice }
		name = gdp_merchant.8000.b
		ai_chance = { base = 0 }
	}
}

# Artisan capstone
gdp_merchant.8001 = {
	type = character_event
	title = gdp_merchant.8001.t
	desc = gdp_merchant.8001.desc
	theme = merchant
	left_portrait = {
		character = root
		animation = merchant_box
	}

	immediate = {
		merchant_artisan_capstone_reward_effect = yes
	}

	option = {
		name = gdp_merchant.8001.a
		if = {
			limit = { exists = scope:nickname_choice }
			give_nickname = nick_the_artisan
		}
		ai_chance = { base = 100 }
	}

	option = {
		trigger = { exists = scope:nickname_choice }
		name = gdp_merchant.8001.b
		ai_chance = { base = 0 }
	}
}

# Creditor capstone
gdp_merchant.8002 = {
	type = character_event
	title = gdp_merchant.8002.t
	desc = gdp_merchant.8002.desc
	theme = merchant
	left_portrait = {
		character = root
		animation = steward
	}

	immediate = {
		merchant_creditor_capstone_reward_effect = yes
	}

	option = {
		name = gdp_merchant.8002.a
		if = {
			limit = { exists = scope:nickname_choice }
			give_nickname = nick_the_lender
		}
		ai_chance = { base = 100 }
	}

	option = {
		trigger = { exists = scope:nickname_choice }
		name = gdp_merchant.8002.b
		ai_chance = { base = 0 }
	}
}

# Caravaneer: Write Travelogue Start
gdp_merchant.8010 = {
	type = character_event
	title = gdp_merchant.8010.t
	desc = gdp_merchant.8010.desc
	theme = merchant
	override_background = { reference = study }
	left_portrait = {
		character = root
		animation = writing
	}

	trigger = {
		is_landless_merchant = yes
		exists = primary_title.var:merchant_company_caravaneer_capstone_unlocked
	}

	immediate = {
		random_in_list = {
			variable = visited_poi
			limit = {
				"squared_distance(root.domicile.domicile_location)" > squared_distance_large
				save_temporary_scope_as = poi_temp
				NOT = {
					root = {
						is_target_in_variable_list = {
							name = travelogue_poi
							target = scope:poi_temp
						}
					}
				}
			}
			weight = {
				base = 1
				modifier = { add = "squared_distance(root.domicile.domicile_location)" }
				modifier = { factor = province_size_level }
			}
			save_scope_as = poi
		}
		add_to_variable_list = {
			name = travelogue_poi
			target = scope:poi
		}
		hidden_effect = {
			random_list = {
				5 = {
					modifier = {
						add = 10
						scope:poi.county.holder.top_liege.highest_held_title_tier = tier_duchy
					}
					set_variable = {
						name = poi_title
						value = scope:poi.duchy
						years = 2
					}
				}
				5 = {
					modifier = {
						add = 10
						scope:poi.county.holder.top_liege.highest_held_title_tier = tier_kingdom
					}
					set_variable = {
						name = poi_title
						value = scope:poi.kingdom
						years = 2
					}
				}
				5 = {
					modifier = {
						add = 10
						scope:poi.county.holder.top_liege.highest_held_title_tier = tier_empire
					}
					set_variable = {
						name = poi_title
						value = scope:poi.empire
						years = 2
					}
				}
				20 = {
					trigger = { scope:poi.county.holder.top_liege.highest_held_title_tier = tier_hegemony }
					set_variable = {
						name = poi_title
						value = scope:poi.county.holder.top_liege.primary_title
					}
				}
				1 = {
					set_variable = {
						name = poi_title 
						value = scope:poi.barony
						years = 2
					}
				}
			}
		}
	}

	option = { 
		name = gdp_merchant.8010.a # Customs
		custom_tooltip = gdp_merchant.8010.a.tt
		set_variable = {
			name = travelogue_topic
			value = flag:travelogue_customs
		}
	}

	option = { 
		name = gdp_merchant.8010.b # Markets
		custom_tooltip = gdp_merchant.8010.a.tt
		set_variable = {
			name = travelogue_topic
			value = flag:travelogue_markets
		}
	}

	option = { 
		name = gdp_merchant.8010.c # Places
		custom_tooltip = gdp_merchant.8010.a.tt
		set_variable = {
			name = travelogue_topic
			value = flag:travelogue_places
		}
	}

	after = {
		add_character_modifier = {
			modifier = writing_travelogue
			years = 1
		}
		trigger_event = {
			id = gdp_merchant.8011
			years = 1
		}
		stress_and_fulfillment_impact = {
			base = major_stress_impact_gain
			lazy = minor_stress_impact_gain
			journaller = minor_stress_impact_loss #offsets penalty a bit
			lifestyle_scholar = minor_stress_impact_loss #offsets penalty a bit
		}
	}
}
	
# Caravaneer: Write Travelogue End
gdp_merchant.8011 = {
	type = character_event
	title = gdp_merchant.8011.t
	desc = {
		desc = gdp_merchant.8011.intro
		triggered_desc = {
			trigger = { scope:quality_of_artifact = flag:weak }
			desc = gdp_merchant.8011.weak
		}
		triggered_desc = {
			trigger = { scope:quality_of_artifact = flag:mid }
			desc = gdp_merchant.8011.mid
		}
		triggered_desc = {
			trigger = { scope:quality_of_artifact = flag:great }
			desc = gdp_merchant.8011.great
		}
	}
	theme = merchant
	override_background = { reference = study }
	
	left_portrait = {
		character = root
		animation = writing #We change the background in each event but not the animation, the background is what root is writing about
	}
	artifact = { # To display the artifact in the event-window
		trigger = { exists = scope:newly_created_artifact }
		target = scope:newly_created_artifact
		position = lower_right_portrait
	}

	trigger = {
		is_landless_merchant = yes
		exists = primary_title.var:merchant_company_caravaneer_capstone_unlocked
	}

	immediate = {
		save_scope_as = author
		#First we make the artifact, then we assign effects and descriptions.
		hidden_effect = {
			if = {
				limit = { artifacts_use_indian_books_trigger = yes }
				create_artifact = {
					name = TODO #This is replaced below
					type = journal
					creator = root
					description = TODO #This is replaced below
					visuals = indian_book
					modifier = artifact_placeholder_modifier
					save_scope_as = newly_created_artifact
					wealth = 50
					quality = 50
				}
			}
			else = {
				create_artifact = {
					name = TODO #This is replaced below
					type = journal
					creator = root
					description = TODO #This is replaced below
					visuals = book
					modifier = artifact_placeholder_modifier
					save_scope_as = newly_created_artifact
					wealth = 50
					quality = 50
				}
			}
		}
		#Common effect for adding modifiers!
		hidden_effect_new_object = {
			#Decide the skill level:
			random_list = {
				33 = {
					trigger = {
						OR = {
							NOT = { has_trait = lifestyle_traveler }
							has_trait_xp = {
								trait = lifestyle_traveler
								track = travel
								value < 60
							}
							NOT = { has_trait = lifestyle_merchant }
							has_trait_xp = {
								trait = lifestyle_merchant
								track = caravaneer
								value < 60
							}
						}
					}
					modifier = {
						has_trait = journaller
						add = -10
					}
					modifier = {
						has_trait = lifestyle_traveler
						add = -10
					}
					modifier = {
						has_trait = lifestyle_merchant
						add = -10
					}
					modifier = {
						has_trait_xp = {
							trait = lifestyle_traveler
							track = travel
							value > 20
						}
						add = -10
					}
					modifier = {
						has_trait_xp = {
							trait = lifestyle_merchant
							track = caravaneer
							value > 20
						}
						add = -10
					}
					modifier = {
						has_trait_xp = {
							trait = lifestyle_traveler
							track = travel
							value > 40
						}
						add = -10
					}
					modifier = {
						has_trait_xp = {
							trait = lifestyle_merchant
							track = caravaneer
							value > 40
						}
						add = -10
					}
					save_scope_value_as = {
						name = quality_of_artifact
						value = flag:weak
					}
				}
				33 = {
					save_scope_value_as = {
						name = quality_of_artifact
						value = flag:mid
					}
				}
				33 = {
					trigger = {
						OR = {
							has_trait_xp = {
								trait = lifestyle_traveler
								track = travel
								value > 40
							}
							has_trait_xp = {
								trait = lifestyle_merchant
								track = caravaneer
								value > 40
							}
						}
					}
					modifier = {
						has_trait = journaller
						add = 10
					}
					modifier = {
						has_trait_xp = {
							trait = lifestyle_traveler
							track = travel
							value > 60
						}
						add = 17
					}
					modifier = {
						has_trait_xp = {
							trait = lifestyle_merchant
							track = caravaneer
							value > 60
						}
						add = 17
					}
					modifier = {
						has_trait_xp = {
							trait = lifestyle_traveler
							track = travel
							value > 80
						}
						add = 20
					}
					modifier = {
						has_trait_xp = {
							trait = lifestyle_merchant
							track = caravaneer
							value > 80
						}
						add = 20
					}
					save_scope_value_as = {
						name = quality_of_artifact
						value = flag:great
					}
				}
			}
			scope:newly_created_artifact = {
				#And now we give modifiers:
				switch = {
					trigger = scope:quality_of_artifact
					flag:weak = {
						set_artifact_rarity_common = yes
						switch = {
							trigger = root.var:travelogue_topic
							flag:travelogue_customs = {
								random_list = {
									70 = {
										add_artifact_modifier = artifact_diplomacy_1_modifier
									}
									30 = {
										modifier = {
											add = root.diplomacy
										}
										add_artifact_modifier = artifact_diplomacy_2_modifier
									}
								}
							}
							flag:travelogue_markets = {
								random_list = {
									70 = {
										add_artifact_modifier = artifact_stewardship_1_modifier
									}
									30 = {
										modifier = {
											add = root.stewardship
										}
										add_artifact_modifier = artifact_stewardship_2_modifier
									}
								}
							}
							flag:travelogue_places = {
								random_list = {
									70 = {
										add_artifact_modifier = artifact_learning_1_modifier
									}
									30 = {
										modifier = {
											add = root.learning
										}
										add_artifact_modifier = artifact_learning_2_modifier
									}
								}
							}
						}
						add_artifact_modifier = artifact_monthly_prestige_2_modifier
						random_list = {
							80 = { }
							10 = { add_artifact_modifier = artifact_trait_track_travel_xp_gain_mult_1_modifier }
						}
						random_list = {
							80 = { }
							10 = { add_artifact_modifier = artifact_trait_track_caravaneer_xp_gain_mult_1_modifier }
						}
					}
					flag:mid = {
						#Influence & Confucian Education Gain
						set_artifact_rarity_masterwork = yes
						switch = {
							trigger = root.var:travelogue_topic
							flag:travelogue_customs = {
								random_list = {
									70 = {
										add_artifact_modifier = artifact_diplomacy_1_modifier
									}
									30 = {
										modifier = {
											add = root.diplomacy
										}
										add_artifact_modifier = artifact_diplomacy_2_modifier
									}
								}
							}
							flag:travelogue_markets = {
								random_list = {
									70 = {
										add_artifact_modifier = artifact_stewardship_1_modifier
									}
									30 = {
										modifier = {
											add = root.stewardship
										}
										add_artifact_modifier = artifact_stewardship_2_modifier
									}
								}
							}
							flag:travelogue_places = {
								random_list = {
									70 = {
										add_artifact_modifier = artifact_learning_1_modifier
									}
									30 = {
										modifier = {
											add = root.learning
										}
										add_artifact_modifier = artifact_learning_2_modifier
									}
								}
							}
						}
						add_artifact_modifier = artifact_monthly_prestige_4_modifier
						random_list = {
							40 = { add_artifact_modifier = artifact_trait_track_travel_xp_gain_mult_1_modifier }
							60 = {
								modifier = {
									add = root.learning
								}
								add_artifact_modifier = artifact_trait_track_travel_xp_gain_mult_2_modifier
							}
						}
						random_list = {
							40 = { add_artifact_modifier = artifact_trait_track_caravaneer_xp_gain_mult_1_modifier }
							60 = {
								modifier = {
									add = root.learning
								}
								add_artifact_modifier = artifact_trait_track_caravaneer_xp_gain_mult_2_modifier
							}
						}
					}
					flag:great = {
						#Influence & Confucian Education Gain
						set_artifact_rarity_famed = yes
						switch = {
							trigger = root.var:travelogue_topic
							flag:travelogue_customs = {
								random_list = {
									70 = {
										add_artifact_modifier = artifact_diplomacy_1_modifier
									}
									30 = {
										modifier = {
											add = root.diplomacy
										}
										add_artifact_modifier = artifact_diplomacy_2_modifier
									}
								}
							}
							flag:travelogue_markets = {
								random_list = {
									70 = {
										add_artifact_modifier = artifact_stewardship_1_modifier
									}
									30 = {
										modifier = {
											add = root.stewardship
										}
										add_artifact_modifier = artifact_stewardship_2_modifier
									}
								}
							}
							flag:travelogue_places = {
								random_list = {
									70 = {
										add_artifact_modifier = artifact_learning_1_modifier
									}
									30 = {
										modifier = {
											add = root.learning
										}
										add_artifact_modifier = artifact_learning_2_modifier
									}
								}
							}
						}
						add_artifact_modifier = artifact_monthly_prestige_6_modifier
						add_artifact_modifier = artifact_trait_track_travel_xp_gain_mult_2_modifier
						add_artifact_modifier = artifact_trait_track_caravaneer_xp_gain_mult_2_modifier
					}
				}
				set_artifact_description = travelogue_description_full
				random_list = {
					10 = {
						set_artifact_name = travelogue_name_1
					}
					10 = {
						set_artifact_name = travelogue_name_2
					}
					10 = {
						set_artifact_name = travelogue_name_3
					}
					10 = {
						set_artifact_name = travelogue_name_4
					}
					10 = {
						set_artifact_name = travelogue_name_5
					}
					10 = {
						set_artifact_name = travelogue_name_6
					}
					10 = {
						set_artifact_name = travelogue_name_7
					}
					1 = {
						set_artifact_name = travelogue_name_8
					}
					20 = {
						trigger = { root.var:travelogue_topic ?= flag:travelogue_customs }
						set_artifact_name = travelogue_name_9
					}
					10 = {
						trigger = { root.var:travelogue_topic ?= flag:travelogue_places }
						set_artifact_name = travelogue_name_10
					}
					1 = {
						set_artifact_name = travelogue_name_11
					}
					10 = {
						set_artifact_name = travelogue_name_12
					}
					10 = {
						set_artifact_name = travelogue_name_13
					}
					10 = {
						set_artifact_name = travelogue_name_14
					}
				}
			}
		}
		show_as_tooltip = {
			scope:newly_created_artifact = { set_owner = root }
			remove_character_modifier = writing_travelogue
		}
	}

	option = {
		name = gdp_merchant.8011.a
	}

	option = {
		name = gdp_merchant.8011.b
		scope:newly_created_artifact = {
			custom_tooltip = {
				text = reforge_artifact_library_item_tt
				reforge_artifact = { type = library_item }
			}
		}
	}

	after = {
		remove_variable = travelogue_topic
		remove_variable = poi_title
	}
}

scripted_trigger gdp_merchant_8020_merchant_trigger = {
	is_landless_merchant = yes
	NOT = { has_character_flag = gdp_merchant_8021_cooldown }
	is_available_quick = {
		adult = yes
		activity = no
		travel = no
		incapable = no
		imprisoned = no
	}
	domicile = { has_domicile_parameter = company_curiosity_visitors }
	any_equipped_character_artifact = {
		OR = {
			artifact_slot_type = library
			artifact_slot_type = creature
			artifact_slot_type = curio
		}
	}
}

scripted_trigger gdp_merchant_8020_visitor_trigger = {
	NOT = { has_character_flag = gdp_merchant_8021_cooldown }
	is_available_quick = {
		ai = yes
		adult = yes
		incapable = no
		imprisoned = no
		in_army = no
	}
	NOR = {
		liege ?= root
		employer ?= root
		court_owner ?= root
		has_relation_rival = root
	}
	save_temporary_scope_as = visitor_temp
	root = {
		NOT = {
			is_target_in_variable_list = {
				name = gdp_merchant_8020_cooldown
				target = scope:visitor_temp
			}
		}
	}
}

# Caravaneer: Merchant Visitor Selection
gdp_merchant.8020 = {
	hidden = yes

	trigger = { gdp_merchant_8020_merchant_trigger = yes }

	immediate = {
		random_list = {
			10 = {
				trigger = {
					any_pool_character = {
						province = root.domicile.domicile_location
						gdp_merchant_8020_visitor_trigger = yes
					}
				}
				random_pool_character = {
					province = root.domicile.domicile_location
					limit = { gdp_merchant_8020_visitor_trigger = yes }
					save_scope_as = visitor
				}
			}
			50 = {
				trigger = {
					suzerain = { gdp_merchant_8020_visitor_trigger = yes }
				}
				suzerain = { save_scope_as = visitor }
			}
			25 = {
				trigger = {
					any_in_global_list = {
						variable = merchants_tally
						holder ?= {
							gdp_merchant_8020_visitor_trigger = yes
							gdp_merchant_diplo_range_trigger = { TARGET = this MERCHANT = root }
						}
					}
				}
				random_in_global_list = {
					variable = merchants_tally
					limit = {
						holder ?= {
							gdp_merchant_8020_visitor_trigger = yes
							gdp_merchant_diplo_range_trigger = { TARGET = this MERCHANT = root }
						}
					}
					holder = { save_scope_as = visitor }
				}
			}
			10 = {
				trigger = {
					any_in_global_list = {
						variable = laamps_tally
						holder ?= {
							gdp_merchant_8020_visitor_trigger = yes
							ep3_laamp_diplo_range_trigger = { TARGET = root LAAMP = this }
						}
					}
				}
				random_in_global_list = {
					variable = laamps_tally
					limit = {
						holder ?= {
							gdp_merchant_8020_visitor_trigger = yes
							ep3_laamp_diplo_range_trigger = { TARGET = root LAAMP = this }
						}
					}
					holder = { save_scope_as = visitor }
				}
			}
			25 = {
				trigger = {
					domicile.domicile_location.empire = {
						any_in_de_jure_hierarchy = {
							holder ?= {
								gdp_merchant_8020_visitor_trigger = yes
								gdp_merchant_diplo_range_trigger = { TARGET = this MERCHANT = root }
							}
						}
					}
				}
				domicile.domicile_location.empire = {
					random_in_de_jure_hierarchy = {
						limit = {
							holder ?= {
								gdp_merchant_8020_visitor_trigger = yes
								gdp_merchant_diplo_range_trigger = { TARGET = this MERCHANT = root }
							}
						}
						weight = {
							base = 5
							modifier = {
								add = {
									value = holder.highest_held_title_tier
									multiply = -1
								}
							}
						}
						holder = { save_scope_as = visitor }
					}
				}
			}
			50 = {}
		}
		trigger_event = gdp_merchant.8021
	}
}

# Caravaneer: Merchant recieves Visitor
gdp_merchant.8021 = {
	type = character_event
	title = gdp_merchant.8021.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:visitor = suzerain }
				desc = gdp_merchant.8021.suzerain
			}
			triggered_desc = {
				trigger = {
					scope:visitor = { is_landless_merchant = yes }
				}
				desc = gdp_merchant.8021.merchant
			}
			triggered_desc = {
				trigger = {
					scope:visitor = { is_landless_adventurer = yes }
				}
				desc = gdp_merchant.8021.laamp
			}
			triggered_desc = {
				trigger = { is_ruler = yes }
				desc = gdp_merchant.8021.ruler
			}
			desc = gdp_merchant.8021.fallback
		}
		desc = gdp_merchant.8021.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:artifact = { artifact_slot_type = curio }
				}
				desc = gdp_merchant.8021.curio
			}
			triggered_desc = {
				trigger = {
					scope:artifact = { artifact_slot_type = library }
				}
				desc = gdp_merchant.8021.library
			}
			desc = gdp_merchant.8021.creature
		}
	}
	theme = merchant
	left_portrait = {
		animation = admiration
		character = root
	}
	right_portrait = {
		character = scope:visitor
		animation = deliberation
	}
	artifact = {
	    target = scope:artifact
	    position = lower_center_portrait
	}

	trigger = {
		exists = scope:visitor
		gdp_merchant_8020_merchant_trigger = yes
	}

	immediate = {
		add_character_flag = {
			flag = gdp_merchant_8021_cooldown
			years = 3
		}
		add_to_variable_list = {
			name = gdp_merchant_8020_cooldown
			target = scope:visitor
			years = 15
		}
		random_equipped_character_artifact = {
			limit = {
				OR = {
					artifact_slot_type = library
					artifact_slot_type = creature
					artifact_slot_type = curio
				}
			}
			save_scope_as = artifact
		}
		add_prestige = {
			value = {
				value = minor_prestige_value
				# Artifact
				if = {
					limit = { scope:artifact = { rarity = masterwork } }
					multiply = 1.5
				}
				if = {
					limit = { scope:artifact = { rarity = famed } }
					multiply = 2
				}
				if = {
					limit = { scope:artifact = { rarity = illustrious } }
					multiply = 2.5
				}
				# Tier
				if = {
					limit = {
						scope:visitor = { is_landed = yes }
						scope:visitor.highest_held_title_tier = tier_county
					}
					multiply = 1.5
				}
				if = {
					limit = {
						scope:visitor = { is_landed = yes }
						scope:visitor.highest_held_title_tier = tier_duchy
					}
					multiply = 2
				}
				if = {
					limit = {
						scope:visitor = { is_landed = yes }
						scope:visitor.highest_held_title_tier = tier_kingdom
					}
					multiply = 2.5
				}
				if = {
					limit = {
						scope:visitor = { is_landed = yes }
						scope:visitor.highest_held_title_tier >= tier_empire
					}
					multiply = 3
				}
			}
		}
	}

	option = {
		name = gdp_merchant.8021.a
	}

	option = {
		name = gdp_merchant.8021.b
		trigger = {
			scope:visitor = {
				is_ruler = yes
				short_term_gold >= major_gold_value
				trigger_if = {
					limit = {
						scope:artifact = { artifact_slot_type = creature }
					}
					is_landless_merchant = yes
				}
			}
		}
		show_as_unavailable = {
			scope:visitor = { is_ruler = yes }
		}
		scope:visitor = {
			pay_short_term_gold = {
				gold = {
					value = root.minor_gold_value
					if = {
						limit = { scope:artifact = { rarity = masterwork } }
						multiply = 2
					}
					if = {
						limit = { scope:artifact = { rarity = famed } }
						multiply = 3
					}
					if = {
						limit = { scope:artifact = { rarity = illustrious } }
						multiply = 4
					}
				}
				target = root
			}
		}
		scope:artifact = {
			set_owner = {
				target = scope:visitor
				history = {
					type = purchased
					actor = root
					recipient = scope:visitor
				}
			}
			hidden_effect = {
				if = {
					limit = {
						scope:visitor = { is_landless_merchant = no }
					}
					if = {
						limit = { artifact_slot_type = library }
						if = {
							limit = {
								scope:visitor = { has_royal_court = yes }
							}
							reforge_artifact = { type = book }
						}
						else = {
							reforge_artifact = { type = journal }
						}
					}
					else_if = {
						limit = { artifact_slot_type = curio }
						if = {
							limit = {
								scope:visitor = { has_royal_court = yes }
							}
							reforge_artifact = {
								type = pedestal
								visuals = small_box
							}
						}
						else = {
							reforge_artifact = { type = miscellaneous }
						}
					}
				}
			}
		}
	}
}

# Move Company
gdp_merchant.9000 = {
	type = character_event
	hidden = yes

	immediate = {
		location = { save_scope_as = origin }
		var:new_capital = { save_scope_as = relocation_destination }
		gdp_move_company_departure_effect = {
			MERCHANT = this
			OLD_MARKET = this.domicile.domicile_location.barony.trade_market
			NEW_MARKET = scope:relocation_destination.barony.trade_market
			OLD_PATRON = suzerain
			NEW_PATRON = var:new_patron
		}
		hidden_effect = {
			domicile = { move_domicile = scope:relocation_destination }
		}
		set_location = scope:origin
		remove_variable = new_patron
		remove_variable = new_capital
		if = {
			limit = { has_character_flag = being_evicted }
			remove_character_flag = being_evicted
			remove_variable = evicting_ruler
		}
	}
}

# Forced Eviction Notice
gdp_merchant.9005 = {
	type = character_event
	title = gdp_merchant.9005.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:courtier }
				desc = gdp_merchant.9005.desc_intro
			}
			desc = gdp_merchant.9005.desc_intro.fallback
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					is_known_dealbreaker_trigger = { KNOWER = scope:expeller }
				}
				desc = gdp_merchant.9005.desc_body.dealbreaker
			}
			triggered_desc = {
				trigger = {
					OR = {
						has_relation_rival = scope:expeller
						has_relation_nemesis = scope:expeller
					}
				}
				desc = gdp_merchant.9005.desc_body.enemies
			}
			desc = gdp_merchant.9005.desc_body.fallback
		}
	}
	theme = merchant
	override_background = study
	left_portrait = {
		character = root
		triggered_animation = {
			trigger = {
				OR = {
					has_trait = arrogant
					has_trait = stubborn
				}
			}
			animation = dismissal
		}
		animation = worry
	}
	right_portrait = {
		trigger = { exists = scope:courtier }
		character = scope:courtier
		animation = anger
	}
	lower_right_portrait = {
		character = scope:expeller
	}

	trigger = {
		is_ai = no
		exists = scope:expeller
	}

	immediate = {
		play_music_cue = "mx_cue_negative"
		if = {
			limit = {
				any_courtier = {
					has_court_position = partner_court_position
					is_available_ai_adult = yes
				}
			}
			random_courtier = {
				limit = {				
					has_court_position = partner_court_position
					is_available_ai_adult = yes
				}
				save_scope_as = courtier
			}
		}
		else_if = {
			limit = {
				any_courtier = { is_available_ai_adult = yes }
			}
			ordered_courtier = {
				limit = { is_available_ai_adult = yes }
				order_by = "opinion(root)"
				save_scope_as = courtier
			}
		}
	}

	option = {
		name = gdp_merchant.9005.a
		custom_tooltip = gdp_merchant.9005.a.tt
		show_as_tooltip = {
			add_opinion = {
				target = scope:expeller
				modifier = demanded_eviction_opinion
			}
		}
	}
}

# Client change messages
gdp_merchant.9011 = {
	type = character_event
	hidden = yes
	cooldown = { days = 1 }

	trigger = {
		domicile ?= { is_domicile_type = merchant_company }
		government_has_flag = government_is_merchant # Republics share the domicile type
	}

	immediate = {
		# scope:merchant, scope:old_suzerain and scope:new_suzerain are saved in gdp_merchant_patron_change_on_title_transfer_effect
		if = {
			limit = {
				exists = scope:old_suzerain
				exists = scope:new_suzerain
				scope:new_suzerain = { is_alive = yes }
				NOT = { scope:old_suzerain = scope:new_suzerain }
			}
			if = {
				limit = {
					exists = scope:old_suzerain
					scope:old_suzerain = { is_alive = yes }
				}
				scope:old_suzerain = {
					send_interface_toast = {
						type = event_toast_text_neutral
						title = msg_client_lost_title
						desc = msg_client_lost_desc
						left_icon = scope:merchant
						right_icon = scope:new_suzerain
						show_as_tooltip = {
							start_tributary_interaction_effect = {
								TRIBUTARY = scope:merchant
								SUZERAIN = scope:new_suzerain
							}
						}
					}
				}
			}
			gdp_notify_subject_overlord_changed_effect = yes
			if = {
				limit = {
					scope:merchant != scope:old_suzerain
					scope:old_suzerain = { is_alive = yes }
				}
				scope:new_suzerain = {
					send_interface_toast = {
						type = event_toast_text_neutral
						title = msg_client_gained_title
						desc = {
							first_valid = {
								triggered_desc = {
									trigger = {
										scope:merchant = { government_has_flag = government_is_merchant }
									}
									desc = msg_patron_changed_desc
								}
								desc = msg_suzerain_changed_desc
							}
						}
						left_icon = scope:merchant
						right_icon = scope:old_suzerain
					}
				}
			}
		}
		else_if = {
			limit = {
				exists = scope:new_suzerain
				scope:new_suzerain = { is_alive = yes }
				NOT = { scope:merchant ?= scope:new_suzerain }
			}
			gdp_notify_subject_overlord_gained_effect = yes
		}
		else_if = {
			limit = {
				exists = scope:old_suzerain
				NOT = { scope:merchant ?= scope:old_suzerain }
			}
			gdp_notify_subject_overlord_lost_effect = yes
		}
	}
}

# AI commissions a merchant
gdp_merchant.9020 = {
	type = character_event
	hidden = yes

	trigger = {
		# Allow trade to stabilize somewhat before commissions
		years_from_game_start >= 2
		# ...AI only...
		is_ai = yes
		# ...landed, duke, market owner...
		commission_trade_route_commissioner_trigger = yes
		# ...needs something...
		any_held_trade_market = {
			OR = {
				any_trade_good_with_unfulfilled_need = { save_temporary_scope_as = target_temp }
				any_trade_category_with_unfulfilled_need = {
					any_trade_good_in_category = { save_temporary_scope_as = target_temp }
				}
			}
			save_temporary_scope_as = trade_market_temp
		}
		# ...consider creating a distribution route first if possible
		should_create_distribution_route = no
		save_temporary_scope_as = commissioner_temp
		# We look up scopes after the chance roll so we do not search twice
		# If they cannot be found, we skip quietly
	}

	immediate = {
		save_scope_value_as = { # for debugging
			name = commission_chance
			value = commission_trade_route_yearly_chance
		}
		random = {
			chance = scope:commission_chance
			save_scope_as = commissioner
			random_held_trade_market = {
				limit = { market_has_any_need_trigger = yes }
				weight = {
					base = 1
					# Prioritize higher overall needs
					diversify_commission_route_market_need_modifier = yes
					modifier = {
						add = 1
						# Prioritize food
						market_has_food_need_trigger = yes
					}
					modifier = {
						factor = 2
						scope:commissioner.capital_province.barony.trade_market ?= this
					}
				}
				save_scope_as = trade_market
				# 1. Establish all categories with need or good need
				every_trade_category_with_unfulfilled_need = { add_to_list = needed_categories }
				every_trade_good_with_unfulfilled_need = {
					trade_good_need_category = { add_to_list = needed_categories }
				}
				# 2. Find a suitable good need from one of those categories
				random_in_list = {
					list = needed_categories
					weight = {
						base = 2
						# Weight by need
						diversify_commission_route_category_need_modifier = yes
						# Prefer to diversify if possible
						diversify_commission_route_category_routes_modifier = yes
						# Prioritize food
						modifier = { 
							OR = {
								this = trade_good_category:trade_category_cereals
								this = trade_good_category:trade_category_meat
							}
							factor = 2
						}
					}
					save_scope_as = category
					scope:category ?= {
						random_trade_good_in_category = {
							weight = {
								base = 2
								# Weight by need
								diversify_commission_route_good_need_modifier = yes
								# Prefer to diversify if possible
								diversify_commission_route_good_routes_modifier = yes
							}
							save_scope_as = target
						}
					}
				}
			}
		}
		if = {
			limit = { exists = scope:target }
			random_tributary = {
				limit = {
					government_has_flag = government_is_merchant
					commission_trade_route_merchant_trigger = {
						COMMISSIONER = scope:commissioner
						MERCHANT = this
					}
					commission_trade_route_good_trigger = {
						COMMISSIONER = scope:commissioner
						MARKET = scope:trade_market
						GOOD = scope:target
						MERCHANT = this
					}
				}
				weight = {
					base = 1
					# REALM
					modifier = {
						capital_province.county.holder.top_liege = scope:commissioner.top_liege
						add = 1
					}
					# COMPANY SIZE
					modifier = { # Prefer cool companies
						add = company_size_value
					}
					# CULTURE
					modifier = {
						culture = scope:commissioner.culture
						add = 1
					}
					modifier = {
						culture = { has_same_culture_heritage = scope:commissioner.culture }
						add = 1
					}
					modifier = {
						culture = { has_same_culture_language = scope:commissioner.culture }
						add = 1
					}
					# FAITH
					modifier = {
						rite = scope:commissioner.rite
						add = 1
					}
					modifier = {
						faith = scope:commissioner.faith
						add = 1
					}
					modifier = {
						religion = scope:commissioner.religion
						add = 1
					}
					modifier = {
						faith = {
							faith_hostility_level = {
								target = scope:commissioner.faith
								value >= faith_astray_level
							}
						}
						add = -1
					}
					modifier = {
						faith = {
							faith_hostility_level = {
								target = scope:commissioner.faith
								value >= faith_hostile_level
							}
						}
						add = -1
					}
					modifier = {
						faith = {
							faith_hostility_level = {
								target = scope:commissioner.faith
								value >= faith_evil_level
							}
						}
						add = -1
					}
					# EXPERTISE
					first_valid = {
						modifier = {
							factor = 4
							exists = scope:target
							primary_title = { save_temporary_scope_as = company_title_temp }
							scope:target = {
								any_trade_category = {
									save_temporary_scope_as = category_expertise_temp
									scope:company_title_temp = {
										trade_good_category_level = {
											target = scope:category_expertise_temp
											value >= merchant_monopoly_trade_expertise_level_high_value
										}
									}
								}
							}
						}
						modifier = {
							factor = 3
							exists = scope:target
							primary_title = { save_temporary_scope_as = company_title_temp }
							scope:target = {
								any_trade_category = {
									save_temporary_scope_as = category_expertise_temp
									scope:company_title_temp = {
										trade_good_category_level = {
											target = scope:category_expertise_temp
											value >= merchant_monopoly_trade_expertise_level_medium_value
										}
									}
								}
							}
						}
						modifier = {
							factor = 2
							exists = scope:target
							primary_title = { save_temporary_scope_as = company_title_temp }
							scope:target = {
								any_trade_category = {
									save_temporary_scope_as = category_expertise_temp
									scope:company_title_temp = {
										trade_good_category_level = {
											target = scope:category_expertise_temp
											value >= merchant_monopoly_trade_expertise_level_low_value
										}
									}
								}
							}
						}
					}
					# Favor players for visibility
					modifier = {
						is_ai = no
						factor = 3
					}
				}
				save_scope_as = merchant
			}
		}
		if = {
			limit = { exists = scope:merchant }
			scope:merchant = {
				trigger_event = gdp_merchant.9021
			}
		}
		# ERROR SUPPRESSION
		if = {
			limit = {
				always = no
				any_in_list = { variable = commission_routes }
			}
			add_to_variable_list = {
				name = commission_routes
				target = scope:commissioner
			}
		}
	}
}

# Merchant accepts commission
gdp_merchant.9021 = {
	type = letter_event
	opening = gdp_merchant.9021.opening
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {  
					scope:commissioner = {
						OR = {
							has_diarchy_type = regency
							has_diarchy_type = temporary_regency
						}	
					}
				}
				desc = gdp_merchant.9021.diarch.desc
			}
			desc = gdp_merchant.9021.no_diarch.desc
		}
		desc = gdp_merchant.9021.desc
	}
	sender = scope:letter_sender

	immediate = {
		## Making sure that child rulers are represented by their diarch
		if = {
			limit = {
				scope:commissioner = {
					OR = {
						has_diarchy_type = regency
						has_diarchy_type = temporary_regency
					}
				}
			}
			scope:commissioner.diarch ?= {
				save_scope_as = letter_sender
			}	
		}
		else = {
			scope:commissioner = {
				save_scope_as = letter_sender
			}
		}
	}

	option = {
		name = gdp_merchant.9021.a
		# The market can change while the letter waits for an answer
		trigger = {
			commission_trade_route_good_trigger = {
				COMMISSIONER = scope:commissioner
				MARKET = scope:trade_market
				GOOD = scope:target
				MERCHANT = scope:merchant
			}
		}
		show_as_unavailable = { always = yes }
		scope:commissioner = {
			# ...and again between choosing this option and it taking effect
			if = {
				limit = {
					commission_trade_route_good_trigger = {
						COMMISSIONER = scope:commissioner
						MARKET = scope:trade_market
						GOOD = scope:target
						MERCHANT = scope:merchant
					}
				}
				add_prestige = {
					subtract = gdp_commission_route_cost_value
				}
				scope:merchant = {
					add_prestige = {
						value = gdp_commission_route_cost_value
						divide = 2
					}
				}
				commission_merchant_effect = {
					MERCHANT = scope:merchant
					GOOD = scope:target
					DEST = scope:trade_market
					YEARS = 10
				}
			}
		}
		custom_tooltip = commission_merchant_cancel_warning_tt
		ai_chance = {
			base = 25
			opinion_modifier = {
				opinion_target = scope:commissioner
				multiplier = 0.25
			}
			ai_value_modifier = {
				ai_greed = 0.5
			}
		}
	}

	option = {
		name = gdp_merchant.9021.b
		# Turning down a commission that could not go ahead anyway is not held against the merchant
		if = {
			limit = {
				commission_trade_route_good_trigger = {
					COMMISSIONER = scope:commissioner
					MARKET = scope:trade_market
					GOOD = scope:target
					MERCHANT = scope:merchant
				}
			}
			custom_tooltip = {
				text = gdp_merchant.9021.b.cooldown_tt
				set_variable = {
					name = commission_trade_route_cooldown
					years = commission_trade_route_merchant_cooldown_years_value
				}
			}
			reverse_add_opinion = {
				modifier = disappointed_opinion
				target = scope:commissioner
				opinion = -15
			}
		}
		ai_chance = {
			base = 10
			opinion_modifier = {
				opinion_target = scope:commissioner
				multiplier = -0.25
			}
			ai_value_modifier = {
				ai_greed = -0.5
			}
		}
	}
	# Let's debug
	after = {
		if = {
			limit = {
				this = character:bookmark_wuhsha_al_dallala
			}
			if = {
				limit = {
					NOT = { exists = global_var:commissions_tally }
				}
				set_global_variable = {
					name = commissions_tally
					value = 1
				}
			}
			change_global_variable = {
				name = commissions_tally
				add = 1
			}
		}
	}
}

# You are a merchant who just became a patrician
gdp_merchant.9100 = {
	type = character_event
	title = gdp_merchant.9100.t
	desc = gdp_merchant.9100.desc
	theme = republic
	left_portrait = {
		character = root
		animation = admiration
	}
	lower_right_portrait = scope:new_patrician_liege

	immediate = {
		show_as_tooltip = {
			get_title = primary_title
			create_title_and_vassal_change = {
				type = conquest
				save_scope_as = change
			}
			change_liege = {
				liege = scope:new_patrician_liege
				change = scope:change
			}
			resolve_title_and_vassal_change = scope:change
			change_government = republic_government
		}
	}

	option = {
		name = gdp_merchant.9100.a
	}
}

gdp_merchant.9110 = {
	type = character_event
	title = gdp_merchant.9110.t
	desc = gdp_merchant.9110.desc
	theme = republic
	left_portrait = {
		character = root
		animation = stress
	}
	lower_right_portrait = scope:destroyer

	trigger = {
		NOT = { has_character_flag = deposed_despot }
	}

	immediate = {
		show_as_tooltip = {
			get_title = primary_title
			create_title_and_vassal_change = {
				type = conquest
				save_scope_as = change
			}
			becomes_independent = { change = scope:change }
			resolve_title_and_vassal_change = scope:change
			start_tributary = {
				contract_group = tributary_merchant
				suzerain = suzerain
			}
			change_government = merchant_government
		}
		custom_tooltip = merchant_patrician_transition_domicile_tt
	}

	option = {
		name = gdp_merchant.9110.a
	}
}
