﻿namespace = gdp_merchant_unity_contract

####
# unity_irregularities
# gdp_merchant_unity_contract.1001-1100 - ?
###

scripted_effect gdp_merchant_unity_contract_1004_organise_travel_effect = {
	start_travel_plan = {
		destination = scope:destination
		on_arrival_event = gdp_merchant_unity_contract.1010
		travel_with_domicile = no
		return_trip = yes
		on_travel_planner_cancel_event = gdp_merchant_unity_contract.1005
		players_use_planner = no
		on_arrival_destinations = $EVENT_FIRE$
	}
}

#	Travel set-up event.
gdp_merchant_unity_contract.1004 = {
	type = character_event
	title = gdp_merchant_unity_contract.1004.t
	desc = gdp_merchant_unity_contract.1004.desc
	theme = trade
	left_portrait = {
		character = root
		animation = worry
	}
	lower_center_portrait = scope:trader
	lower_right_portrait = scope:local_ruler
	override_background = { reference = study }

	option = {
		name = gdp_merchant_unity_contract.1004.a
		custom_tooltip = gdp_merchant_unity_contract.1004.tt.destination_list
		custom_tooltip = gdp_merchant_unity_contract.1004.tt.explanation
		gdp_merchant_unity_contract_1004_organise_travel_effect = {
			EVENT_FIRE = all_but_last
		}
	}
}

#	Travel invalidation event.
gdp_merchant_unity_contract.1005 = {
	hidden = yes

	trigger = { is_alive = yes }

	immediate = {
		random_character_active_contract = {
			task_contract_type = laamp_base_2001
			save_scope_as = task_contract
		}
		send_interface_message = {
			title = contract_invalidated_title
			type = stewardship_contract_scheme_invalidated_message
			desc = contract_invalidated_desc
			right_icon = scope:task_contract.task_contract_employer
			scope:task_contract = { complete_task_contract = failure_standard }
		}
	}
}

scripted_trigger gdp_merchant_unity_contract_1010_would_rip_off_customers_trigger = {
	NOR = {
		# Honest chars never lie.
		has_trait = honest
		# Just characters won't lie unless they're actively deceitful.
		AND = {
			has_trait = just
			NOT = { has_trait = deceitful }
		}
	}
}

scripted_effect gdp_merchant_unity_contract_1010_find_partner_effect = {
	# Clean the partner if they exist (in case they die between events).
	clear_saved_scope = partner
	# Now nab 'em afresh.
	court_position:partner_court_position ?= {
		if = {
			limit = {
				is_available_allow_travelling = yes
				location = root.location
				# Not subject of investigation
				NOT = { this = scope:task_contract.task_contract_employer }
			}
			save_scope_as = partner
		}
	}
}

scripted_effect gdp_merchant_unity_contract_1010_person_scope_effect = {
	if = {
		limit = {
			NOT = { exists = scope:person_c }
		}
		save_scope_as = person_c
	}
	else_if = {
		limit = {
			NOT = { exists = scope:person_d }
		}
		save_scope_as = person_d
	}
}

scripted_trigger gdp_merchant_unity_contract_1010_person_scope_trigger = {
	is_available_quick = {
		ai = yes
		adult = yes
		imprisoned = no
		incapable = no
	}
	NOR = {
		scope:person_c ?= this
		scope:person_d ?= this
	}
}

#	Arrival at market event.
gdp_merchant_unity_contract.1010 = {
	type = character_event
	title = gdp_merchant_unity_contract.1010.t
	window = visit_settlement_window
	desc = {
		desc = gdp_merchant_unity_contract.1010.desc.intro
		triggered_desc = {
			trigger = { exists = scope:partner }
			desc = gdp_merchant_unity_contract.1010.desc.intro.has_partner
		}
		desc = gdp_merchant_unity_contract.1010.desc.midtro
		desc = gdp_merchant_unity_contract.1010.desc.outro
	}
	theme = trade
	left_portrait = {
		character = root
		animation = thinking
	}
	center_portrait = {
		character = scope:local_ruler
		animation = dismissal
	}
	right_portrait = {
		character = scope:trader
		animation = disapproval
	}
	lower_left_portrait = scope:person_c
	lower_center_portrait = scope:person_d
	override_background = { reference = alley_day }

	immediate = {
		scope:trader = {
			random_list = {
				# Trader is a bad'n
				100 = {
					trigger = { gdp_merchant_unity_contract_1010_would_rip_off_customers_trigger = yes }
					save_scope_value_as = {
						name = irregularity
						value = flag:missing
					}
				}
				# Trader is only human
				100 = {
					trigger = { gdp_merchant_unity_contract_1010_would_rip_off_customers_trigger = yes }
					save_scope_value_as = {
						name = irregularity
						value = flag:quality
					}
				}
				# Trader is a good'n
				100 = {
					save_scope_value_as = {
						name = irregularity
						value = flag:dues
					}
				}
			}
		}
		# Find merchants
		scope:market = {
			every_character_in_market = {
				limit = {
					government_has_flag = government_is_merchant
					NOT = { this = root }
					gdp_merchant_unity_contract_1010_person_scope_trigger = yes
				}
				add_to_temporary_list = potential_merchants_1
			}
			every_trade_route_in_market = {
				limit = {
					trade_route_owner = {
						government_has_flag = government_is_merchant
						NOR = {
							this = root
							is_in_list = potential_merchants_1
						}
						gdp_merchant_unity_contract_1010_person_scope_trigger = yes
					}
				}
				trade_route_owner = { add_to_temporary_list = potential_merchants_2 }
			}
			every_in_global_list = {
				variable = merchants_tally
				limit = {
					holder = {
						NOR = {
							this = root
							is_in_list = potential_merchants_1
							is_in_list = potential_merchants_2
						}
						gdp_merchant_diplo_range_trigger = {
							MERCHANT = this
							TARGET = root
						}
						gdp_merchant_diplo_range_trigger = {
							MERCHANT = this
							TARGET = scope:local_ruler
						}
						gdp_merchant_unity_contract_1010_person_scope_trigger = yes
					}
				}
				holder = { add_to_temporary_list = potential_merchants_3 }
			}
		}
		hidden_effect = {
			while = {
				limit = {
					NOT = { exists = scope:person_d }
				}
				random_list = {
					7 = {
						trigger = {
							list_size = { name = potential_merchants_1 value > 0 }
						}
						random_in_list = {
							list = potential_merchants_1
							limit = { gdp_merchant_unity_contract_1010_person_scope_trigger = yes }
							gdp_merchant_unity_contract_1010_person_scope_effect = yes
							remove_from_list = potential_merchants_1
						}
					}
					7 = {
						trigger = {
							list_size = { name = potential_merchants_1 value < 1 }
							list_size = { name = potential_merchants_2 value > 0 }
						}
						random_in_list = {
							list = potential_merchants_2
							limit = { gdp_merchant_unity_contract_1010_person_scope_trigger = yes }
							gdp_merchant_unity_contract_1010_person_scope_effect = yes
							remove_from_list = potential_merchants_2
						}
					}
					7 = {
						trigger = {
							list_size = { name = potential_merchants_1 value < 1 }
							list_size = { name = potential_merchants_2 value < 1 }
							list_size = { name = potential_merchants_3 value > 0 }
						}
						random_in_list = {
							list = potential_merchants_3
							limit = { gdp_merchant_unity_contract_1010_person_scope_trigger = yes }
							gdp_merchant_unity_contract_1010_person_scope_effect = yes
							remove_from_list = potential_merchants_3
						}
					}
					10 = {
						scope:local_ruler = {
							ordered_courtier = {
								limit = {
									OR = {
										location = scope:destination
										location = scope:local_ruler.capital_province
									}
									NOR = {
										is_close_or_extended_family_of = scope:local_ruler
										is_consort_of = scope:local_ruler
									}
									gdp_merchant_unity_contract_1010_person_scope_trigger = yes
								}
								order_by = {
									value = 0
									add = stewardship
									if = {
										limit = { is_councillor = yes }
										add = 10
									}
									if = {
										limit = { has_any_court_position = yes }
										add = 10
									}
								}
								gdp_merchant_unity_contract_1010_person_scope_effect = yes
							}
						}
					}
					10 = {
						create_character = {
							template = merchant_template
							location = scope:destination
							culture = scope:destination.culture
							faith = scope:destination.faith
							after_creation = {
								add_character_flag = created
								gdp_merchant_unity_contract_1010_person_scope_effect = yes
							}
						}
					}
				}
			}
		}
		### Append them to the task contract: if they die too early, we invalidate.
		#### Largely a concern for MP.
		scope:task_contract = {
			set_variable = {
				name = person_c
				value = scope:person_c
			}
			set_variable = {
				name = person_d
				value = scope:person_d
			}
		}
		# Note that we'll be taking our background cues from scope:person_b (local_ruler).
		scope:local_ruler = { save_scope_as = bg_override_char }
		# Try to grab our partner.
		gdp_merchant_unity_contract_1010_find_partner_effect = yes
		# Plus, stop us whilst we're here.
		hidden_effect = {
			current_travel_plan = { pause_travel_plan = yes }
		}
	}
	
	option = { # Meet scope:local_ruler (person_b).
		name = gdp_merchant_unity_contract.1010.a
		scope:local_ruler = {
			custom_tooltip = gdp_merchant_unity_contract.1010.a.tt
			save_scope_as = char_current
		}
		trigger_event = gdp_merchant_unity_contract.1011
	}
	
	option = { # Meet scope:person_c.
		name = gdp_merchant_unity_contract.1010.b
		trigger = { exists = scope:person_c }
		scope:person_c = {
			custom_tooltip = gdp_merchant_unity_contract.1010.a.tt
			save_scope_as = char_current
		}
		trigger_event = gdp_merchant_unity_contract.1011
	}
	
	option = { # Meet scope:person_d.
		name = gdp_merchant_unity_contract.1010.c
		trigger = { exists = scope:person_d }
		scope:person_d = {
			custom_tooltip = gdp_merchant_unity_contract.1010.a.tt
			save_scope_as = char_current
		}
		trigger_event = gdp_merchant_unity_contract.1011
	}
	
	option = { # Fast resolution.
		name = gdp_merchant_unity_contract.1010.d
		custom_tooltip = gdp_merchant_unity_contract.1012.tt.boilerplate.end_investigation
		trigger_event = gdp_merchant_unity_contract.1021
	}
}

scripted_effect gdp_merchant_unity_contract_1011_duel_success_effect = {
	custom_tooltip = gdp_merchant_unity_contract.1011.tt.success_increases_score
	scope:task_contract = {
		increment_variable_effect = {
			VAR = assurances_given
			VAL = 1
		}
	}
	scope:task_contract = {
		increment_variable_effect = {
			VAR = chars_checked
			VAL = 1
		}
	}
	save_scope_value_as = {
		name = result
		value = flag:success
	}
}

scripted_effect gdp_merchant_unity_contract_1011_duel_failure_effect = {
	custom_tooltip = gdp_merchant_unity_contract.1011.tt.failure_gives_no_score
	scope:task_contract = {
		increment_variable_effect = {
			VAR = chars_checked
			VAL = 1
		}
	}
	save_scope_value_as = {
		name = result
		value = flag:failure
	}
}

scripted_effect gdp_merchant_unity_contract_1011_duel_generic_effect = {
	save_scope_value_as = {
		name = method
		value = flag:$SKILL$
	}
	duel = {
		skill = $SKILL$
		target = scope:char_current
		# Victory!
		50 = {
			compare_modifier = {
				value = scope:duel_value
				multiplier = 3.5
				min = -49
			}
			min = 5
			desc = gdp_merchant_unity_contract.1011.$SKILL$.tt.success
			gdp_merchant_unity_contract_1011_duel_success_effect = yes
		}
		# Defeat!
		50 = {
			compare_modifier = {
				value = scope:duel_value
				multiplier = -3.5
				min = -49
			}
			min = 5
			desc = gdp_merchant_unity_contract.1011.$SKILL$.tt.failure
			gdp_merchant_unity_contract_1011_duel_failure_effect = yes
		}
	}
}

#	Meet with someone.
gdp_merchant_unity_contract.1011 = {
	type = character_event
	title = gdp_merchant_unity_contract.1011.t
	window = visit_settlement_window
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:business = flag:local_ruler }
				desc = gdp_merchant_unity_contract.1011.desc.business.local_ruler
			}
			triggered_desc = {
				trigger = { scope:business = flag:merchant }
				desc = gdp_merchant_unity_contract.1011.desc.business.merchant
			}
			triggered_desc = {
				trigger = { scope:business = flag:docks }
				desc = gdp_merchant_unity_contract.1011.desc.business.docks
			}
			triggered_desc = {
				trigger = { scope:business = flag:market }
				desc = gdp_merchant_unity_contract.1011.desc.business.market
			}
			triggered_desc = {
				trigger = { scope:business = flag:smithy }
				desc = gdp_merchant_unity_contract.1011.desc.business.smithy
			}
			triggered_desc = {
				trigger = { scope:business = flag:tannery }
				desc = gdp_merchant_unity_contract.1011.desc.business.tannery
			}
			triggered_desc = {
				trigger = { scope:business = flag:carpentry }
				desc = gdp_merchant_unity_contract.1011.desc.business.carpentry
			}
			triggered_desc = {
				trigger = { scope:business = flag:town_watch }
				desc = gdp_merchant_unity_contract.1011.desc.business.town_watch
			}
		}
		desc = boilerplate.paragraph
		first_valid = {
			triggered_desc = {
				trigger = { scope:personality = flag:local_ruler }
				desc = {
					desc = gdp_merchant_unity_contract.1011.desc.personality.local_ruler
					first_valid = {
						triggered_desc = {
							trigger = { scope:irregularity = flag:missing }
							desc = gdp_merchant_unity_contract.1011.desc.irregularity.missing
						}
						triggered_desc = {
							trigger = { scope:irregularity = flag:quality }
							desc = gdp_merchant_unity_contract.1011.desc.irregularity.quality
						}
						triggered_desc = {
							trigger = { scope:irregularity = flag:dues }
							desc = gdp_merchant_unity_contract.1011.desc.irregularity.dues
						}
					}
				}
			}
			triggered_desc = {
				trigger = { scope:personality = flag:merchant }
				desc = gdp_merchant_unity_contract.1011.desc.personality.merchant
			}
			triggered_desc = {
				trigger = { scope:personality = flag:courtier }
				desc = gdp_merchant_unity_contract.1011.desc.personality.courtier
			}
			triggered_desc = {
				trigger = {
					OR = {
						scope:personality = flag:market
						scope:personality = flag:docks
					}
				}
				desc = gdp_merchant_unity_contract.1011.desc.personality.market
			}
			triggered_desc = {
				trigger = { scope:personality = flag:town_watch }
				desc = gdp_merchant_unity_contract.1011.desc.personality.town_watch
			}
			triggered_desc = {
				trigger = { scope:personality = flag:ai_honor }
				desc = gdp_merchant_unity_contract.1011.desc.personality.ai_honor
			}
			triggered_desc = {
				trigger = { scope:personality = flag:ai_zeal }
				desc = gdp_merchant_unity_contract.1011.desc.personality.ai_zeal
			}
			triggered_desc = {
				trigger = { scope:personality = flag:ai_greed }
				desc = gdp_merchant_unity_contract.1011.desc.personality.ai_greed
			}
			triggered_desc = {
				trigger = { scope:personality = flag:ai_compassion }
				desc = gdp_merchant_unity_contract.1011.desc.personality.ai_compassion
			}
			triggered_desc = {
				trigger = { scope:personality = flag:ai_boldness }
				desc = gdp_merchant_unity_contract.1011.desc.personality.ai_boldness
			}
			triggered_desc = {
				trigger = { scope:personality = flag:ai_rationality }
				desc = gdp_merchant_unity_contract.1011.desc.personality.ai_rationality
			}
			triggered_desc = {
				trigger = { scope:personality = flag:ai_vengefulness }
				desc = gdp_merchant_unity_contract.1011.desc.personality.ai_vengefulness
			}
			triggered_desc = {
				trigger = { scope:personality = flag:ai_energy }
				desc = gdp_merchant_unity_contract.1011.desc.personality.ai_energy
			}
			triggered_desc = {
				trigger = { scope:personality = flag:ai_sociability }
				desc = gdp_merchant_unity_contract.1011.desc.personality.ai_sociability
			}
			triggered_desc = {
				trigger = { scope:personality = flag:ai_personality_fallback }
				desc = gdp_merchant_unity_contract.1011.desc.personality.ai_personality_fallback
			}
		}
		triggered_desc = {
			trigger = { scope:personality = flag:local_ruler }
			desc = gdp_merchant_unity_contract.1011.desc.personality.local_ruler.closing
		}
	}
	theme = trade
	left_portrait = {
		character = scope:partner
		animation = writing
	}
	center_portrait = {
		character = root
		animation = thinking
	}
	right_portrait = {
		character = scope:char_current
		triggered_animation = {
			trigger = { scope:personality = flag:local_ruler }
			animation = admiration
		}
		triggered_animation = {
			trigger = { scope:business = flag:docks }
			animation = page_flipping
		}
		triggered_animation = {
			trigger = { scope:personality = flag:ai_honor }
			animation = personality_honorable
		}
		triggered_animation = {
			trigger = { scope:personality = flag:ai_zeal }
			animation = personality_zealous
		}
		triggered_animation = {
			trigger = { scope:personality = flag:ai_greed }
			animation = personality_greedy
		}
		triggered_animation = {
			trigger = { scope:personality = flag:ai_compassion }
			animation = personality_compassionate
		}
		triggered_animation = {
			trigger = { scope:personality = flag:ai_boldness }
			animation = personality_bold
		}
		triggered_animation = {
			trigger = { scope:personality = flag:ai_rationality }
			animation = personality_rational
		}
		triggered_animation = {
			trigger = { scope:personality = flag:ai_vengefulness }
			animation = personality_vengeful
		}
		triggered_animation = {
			trigger = { scope:personality = flag:ai_energy }
			animation = personality_bold
		}
		triggered_animation = {
			trigger = { scope:personality = flag:ai_sociability }
			animation = personality_forgiving
		}
		triggered_animation = {
			trigger = { scope:personality = flag:ai_personality_fallback }
			animation = stress
		}
	}
	lower_left_portrait = scope:portrait_1
	lower_center_portrait = scope:portrait_2
	lower_right_portrait = scope:portrait_3
	# Backgrounds indented +1 for easier folding.
		override_background = {
			trigger = { scope:business = flag:local_ruler }
			reference = council_chamber
		}
		override_background = {
			trigger = { scope:business = flag:courtier }
			reference = council_chamber
		}
		override_background = {
			trigger = { scope:business = flag:merchant }
			reference = study
		}
		override_background = {
			trigger = { scope:business = flag:docks }
			reference = docks
		}
		override_background = {
			trigger = { scope:business = flag:market }
			reference = market_crowd
		}
		override_background = {
			trigger = { scope:business = flag:smithy }
			reference = gdp_smithy_workshop
		}
		override_background = {
			trigger = { scope:business = flag:tannery }
			reference = gdp_tannery
		}
		override_background = {
			trigger = { scope:business = flag:carpentry }
			reference = gdp_carpentry_workshop
		}
		override_background = {
			trigger = { scope:business = flag:town_watch }
			reference = armory
		}

	immediate = {
		# Try to grab our partner.
		gdp_merchant_unity_contract_1010_find_partner_effect = yes
		custom_tooltip = gdp_merchant_unity_contract.1011.tt.current_assurances
		custom_tooltip = gdp_merchant_unity_contract.1011.tt.chars_checked
		# Sort our loot.
		random_list = {
			# Gold.
			200 = {
				trigger = {
					scope:char_current = { is_clergy = no }
				}
				save_scope_value_as = {
					name = bribe
					value = flag:gold
				}
			}
			200 = {
				trigger = {
					scope:char_current = { government_has_flag = government_is_merchant }
				}
				save_scope_value_as = {
					name = bribe
					value = flag:competitiveness
				}
			}
			200 = {
				trigger = {
					scope:char_current = {
						OR = {
							is_clergy = yes
							government_has_flag = government_is_holy_order
						}
					}
				}
				save_scope_value_as = {
					name = bribe
					value = flag:piety
				}
			}
		}
		# And what our secondary skill test will be.
		random_list = {
			# Diplomacy.
			100 = {
				trigger = {
					NOT = { scope:secondary_duel = flag:diplomacy } # Variety
				}
				modifier = {
					add = 200
					scope:char_current = { highest_skill = diplomacy }
				}
				save_scope_value_as = {
					name = secondary_duel
					value = flag:diplomacy
				}
			}
			# Intrigue.
			100 = {
				trigger = {
					NOT = { scope:secondary_duel = flag:intrigue } # Variety
				}
				modifier = {
					add = 200
					scope:char_current = { highest_skill = intrigue }
				}
				save_scope_value_as = {
					name = secondary_duel
					value = flag:intrigue
				}
			}
		}
		# Personalise the event.
		## If we're looking at scope:local_ruler, then we pre-configure this stuff.
		if = {
			limit = { scope:char_current = scope:local_ruler }
			save_scope_value_as = {
				name = business
				value = flag:local_ruler
			}
			save_scope_value_as = {
				name = personality
				value = flag:local_ruler
			}
		}
		## If we're looking at scope:trader, then we pre-configure this stuff.
		else_if = {
			limit = { scope:char_current = scope:trader }
			save_scope_value_as = {
				name = business
				value = flag:merchant
			}
			save_scope_value_as = {
				name = personality
				value = flag:merchant
			}
		}
		## If we're looking at a courtier of the ruler, then we pre-configure this stuff.
		else_if = {
			limit = { scope:char_current.liege = scope:local_ruler }
			save_scope_value_as = {
				name = business
				value = flag:courtier
			}
			save_scope_value_as = {
				name = personality
				value = flag:courtier
			}
		}
		## Otherwise, we're looking at one of the others and need to roll for it.
		else = {
			scope:char_current = {
				# Select a business for them to run.
				random_list = {
					100 = {
						trigger = {
							scope:destination = { is_coastal = yes }
						}
						modifier = {
							add = -99
							scope:task_contract = { has_variable = had_docks_business }
						}
						save_scope_value_as = {
							name = business
							value = flag:docks
						}
						scope:task_contract = { set_variable = had_docks_business }
					}
					100 = {
						trigger = {
							NOT = { has_trait = generous }
						}
						modifier = {
							add = -99
							scope:task_contract = { has_variable = had_market_business }
						}
						save_scope_value_as = {
							name = business
							value = flag:market
						}
						scope:task_contract = { set_variable = had_market_business }
					}
					100 = {
						modifier = {
							add = -99
							scope:task_contract = { has_variable = had_smithy_business }
						}
						save_scope_value_as = {
							name = business
							value = flag:smithy
						}
						scope:task_contract = { set_variable = had_smithy_business }
					}
					100 = {
						modifier = {
							add = -99
							scope:task_contract = { has_variable = had_tannery_business }
						}
						save_scope_value_as = {
							name = business
							value = flag:tannery
						}
						scope:task_contract = { set_variable = had_tannery_business }
					}
					100 = {
						modifier = {
							add = -99
							scope:task_contract = { has_variable = had_carpentry_business }
						}
						save_scope_value_as = {
							name = business
							value = flag:carpentry
						}
						scope:task_contract = { set_variable = had_carpentry_business }
					}
					100 = {
						trigger = {
							can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = this }
						}
						modifier = {
							add = -99
							scope:task_contract = { has_variable = had_town_watch_business }
						}
						save_scope_value_as = {
							name = business
							value = flag:town_watch
						}
						scope:task_contract = { set_variable = had_town_watch_business }
						add_character_flag = wear_armor
					}
				}
				select_merchant_personality_type_effect = yes
			}
			## If we're looking at a merchant, then we post-configure this stuff.
			if = {
				limit = {
					scope:char_current = {
						is_ruler = yes
						government_has_flag = government_is_merchant
					}
				}
				save_scope_value_as = {
					name = business
					value = flag:merchant
				}
			}
		}
		## Organise our portraits.
		switch = {
			trigger = scope:char_current
			scope:person_b = {
				scope:person_c = { save_scope_as = portrait_1 }
				scope:person_d = { save_scope_as = portrait_2 }
			}
			scope:person_c = {
				scope:person_b = { save_scope_as = portrait_1 }
				scope:person_d = { save_scope_as = portrait_2 }
			}
			scope:person_d = {
				scope:person_b = { save_scope_as = portrait_1 }
				scope:person_c = { save_scope_as = portrait_2 }
			}
		}
		# Fix values
		save_scope_value_as = {
			name = reimburse_value
			value = {
				integer_range = {
					min = scope:char_current.minor_gold_value
					max = scope:char_current.medium_gold_value
				}
			}
		}
		save_scope_value_as = {
			name = bribe_value
			value = {
				integer_range = {
					min = minor_gold_value
					max = medium_gold_value
				}
			}
		}
		# Note that we've seen this character.
		scope:char_current = { add_to_list = investigated_persons_list }
	}

	# Huck partner at the problem.
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { exists = scope:partner }
						desc = gdp_merchant_unity_contract.1011.f.partner
					}
					triggered_desc = {
						trigger = { employs_court_position = partner_court_position }
						desc = gdp_merchant_unity_contract.1011.f.partner_away
					}
					triggered_desc = {
						trigger = { scope:partner ?= scope:trader }
						desc = gdp_merchant_unity_contract.1011.f.partner_diff
					}
					desc = gdp_merchant_unity_contract.1011.f.no_partner
				}
			}
		}
		trigger = {
			exists = scope:partner
			NOT = { scope:partner ?= scope:trader }
		}
		show_as_unavailable = { always = yes }
		reason = have_partner
		save_scope_value_as = {
			name = method
			value = flag:partner
		}
		scope:partner ?= {
			stress_and_fulfillment_impact = {
				base = medium_stress_gain
				greedy = minor_stress_impact_loss
				diligent = minor_stress_impact_loss
				generous = minor_stress_impact_gain
				lazy = medium_stress_impact_gain
			}
		}
		custom_tooltip = option_boilerplate.tt.partner_aptitude
		# Extra exists check because otherwise show_as_unavailable'll cause errors.
		if = {
			limit = { exists = scope:partner }
			random_list = {
				0 = {
					desc = option_boilerplate.tt.partner_aptitude.success
					gdp_merchant_unity_contract_1011_duel_success_effect = yes
					# Raw aptitude.
					modifier = {
						add = {
							scope:partner ?= { add = "aptitude(partner_court_position)" }
							multiply = 20
							min = 5
							max = 95
						}
						always = yes
					}
				}
				0 = {
					desc = option_boilerplate.tt.partner_aptitude.failure
					gdp_merchant_unity_contract_1011_duel_failure_effect = yes
					# Raw aptitude.
					modifier = {
						add = {
							scope:partner ?= { add = "aptitude(partner_court_position)" }
							multiply = -20
							min = 5
							max = 95
						}
						always = yes
					}
				}
			}
		}
		stress_and_fulfillment_impact = {
			lazy = minor_stress_impact_loss
			diligent = minor_stress_impact_gain
		}
		ai_chance = {
			base = 1
			modifier = {
				add = {
					scope:partner = { add = "aptitude(partner_court_position)" }
					multiply = 20
				}
				exists = scope:partner
			}
		}
	}

	# Convince with stewardship (jargon).
	option = {
		name = gdp_merchant_unity_contract.1011.a
		gdp_merchant_unity_contract_1011_duel_generic_effect = { SKILL = stewardship }
		ai_chance = {
			base = 1
			# AI will prioritise this if it lines up with their highest skill.
			modifier = {
				add = 100
				highest_skill = stewardship
			}
		}
	}

	# Secondary convince with diplo.
	option = {
		name = gdp_merchant_unity_contract.1011.b
		trigger = { scope:secondary_duel = flag:diplomacy }
		gdp_merchant_unity_contract_1011_duel_generic_effect = { SKILL = diplomacy }
		ai_chance = {
			base = 1
			# AI will prioritise this if it lines up with their highest skill.
			modifier = {
				add = 100
				highest_skill = diplomacy
			}
		}
	}

	# Secondary convince with intrigue.
	option = {
		name = gdp_merchant_unity_contract.1011.c
		trigger = { scope:secondary_duel = flag:intrigue }
		gdp_merchant_unity_contract_1011_duel_generic_effect = { SKILL = intrigue }
		ai_chance = {
			base = 1
			# AI will prioritise this if it lines up with their highest skill.
			modifier = {
				add = 100
				highest_skill = intrigue
			}
		}
	}

	# Reimburse them.
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = { scope:bribe = flag:gold }
						desc = gdp_merchant_unity_contract.1011.d.gold
					}
					triggered_desc = {
						trigger = { scope:bribe = flag:competitiveness }
						desc = gdp_merchant_unity_contract.1011.d.competitiveness
					}
				}
			}
		}
		trigger = {
			NOR = {
				scope:business = flag:local_ruler
				scope:business = flag:courtier
				scope:business = flag:town_watch
				scope:business = flag:docks
				scope:business = flag:market
			}
		}
		save_scope_value_as = {
			name = method
			value = flag:reimburse
		}
		switch = {
			trigger = scope:bribe
			flag:gold = {
				pay_treasury_or_gold = {
					target = scope:char_current
					value = scope:reimburse_value
				}
			}
			flag:competitiveness = {
				scope:market = {
					add_competitiveness_modifier_for_character = {
						modifier = gdp_unity_irregularities_reimburse_modifier
						character = scope:char_current
						years = 10
					}
				}
			}
			flag:piety = {
				add_piety = medium_piety_loss
			}
		}
		scope:char_current = { add_to_list = bribed_chars_list }
		gdp_merchant_unity_contract_1011_duel_success_effect = yes
		stress_and_fulfillment_impact = {
			arbitrary = minor_stress_impact_gain
			greedy = medium_stress_impact_gain
			generous = medium_stress_impact_loss
			just = medium_stress_impact_loss
		}
		ai_chance = {
			base = 1
			ai_value_modifier = { ai_greed = -2 }
		}
	}

	# Bribe them.
	option = {
		name = gdp_merchant_unity_contract.1011.e
		trigger = {
			OR = {
				scope:business = flag:local_ruler
				scope:business = flag:courtier
				scope:business = flag:town_watch
				scope:business = flag:docks
				scope:business = flag:market
			}
		}
		save_scope_value_as = {
			name = method
			value = flag:bribe
		}
		scope:char_current = { add_to_list = bribed_chars_list }
		random_list = {
			100 = {
				modifier = {
					add = {
						value = scope:char_current.ai_greed
						divide = 2
					}
				}
				opinion_modifier = {
					who = scope:char_current
					opinion_target = root
					multiplier = 1
				}
				min = 5
				pay_short_term_gold = {
					target = scope:char_current
					gold = scope:bribe_value
				}
				gdp_merchant_unity_contract_1011_duel_success_effect = yes
			}
			10 = {
				modifier = {
					add = {
						value = scope:char_current.ai_honor
						divide = 2
					}
				}
				min = 5
				gdp_merchant_unity_contract_1011_duel_failure_effect = yes
				reverse_add_opinion = {
					target = scope:char_current
					modifier = outraged_opinion
					opinion = -25
				}
			}
		}
		stress_and_fulfillment_impact = {
			arbitrary = minor_stress_impact_loss
			greedy = minor_stress_impact_gain
			just = medium_stress_impact_gain
		}
		ai_chance = {
			base = 1
			ai_value_modifier = { ai_honor = -2 }
		}
	}

	after = { trigger_event = gdp_merchant_unity_contract.1012 }
}

#	Meeting results.
gdp_merchant_unity_contract.1012 = {
	type = character_event
	title = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:result = flag:success }
				desc = gdp_merchant_unity_contract.1012.t.success
			}
			triggered_desc = {
				trigger = { scope:result = flag:failure }
				desc = gdp_merchant_unity_contract.1012.t.failure
			}
		}
	}
	window = visit_settlement_window
	desc = {
		# Method fluff.
		first_valid = {
			triggered_desc = {
				trigger = { scope:method = flag:stewardship }
				desc = gdp_merchant_unity_contract.1012.desc.method.stewardship
			}
			triggered_desc = {
				trigger = { scope:method = flag:diplomacy }
				desc = gdp_merchant_unity_contract.1012.desc.method.diplomacy
			}
			triggered_desc = {
				trigger = { scope:method = flag:intrigue }
				desc = gdp_merchant_unity_contract.1012.desc.method.intrigue
			}
			triggered_desc = {
				trigger = { scope:method = flag:learning }
				desc = gdp_merchant_unity_contract.1012.desc.method.learning
			}
			triggered_desc = {
				trigger = { scope:method = flag:partner }
				desc = gdp_merchant_unity_contract.1012.desc.method.partner
			}
			triggered_desc = {
				trigger = {
					scope:method = flag:bribe
					scope:bribe = flag:gold
				}
				desc = gdp_merchant_unity_contract.1012.desc.method.bribe.gold
			}
			triggered_desc = {
				trigger = { scope:method = flag:reimburse }
				desc = gdp_merchant_unity_contract.1012.desc.method.reimburse
			}
		}
		desc = boilerplate.paragraph
		# Result.
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:method = flag:bribe
					scope:result = flag:success
				}
				desc = gdp_merchant_unity_contract.1012.desc.result.bribe_success
			}
			triggered_desc = {
				trigger = {
					scope:method = flag:bribe
					scope:result = flag:success
				}
				desc = gdp_merchant_unity_contract.1012.desc.result.bribe_failure
			}
			triggered_desc = {
				trigger = { scope:method = flag:reimburse }
				desc = gdp_merchant_unity_contract.1012.desc.result.reimburse
			}
			triggered_desc = {
				trigger = { scope:result = flag:success }
				desc = gdp_merchant_unity_contract.1012.desc.result.success
			}
			triggered_desc = {
				trigger = { scope:result = flag:failure }
				desc = gdp_merchant_unity_contract.1012.desc.result.failure
			}
		}
		desc = boilerplate.paragraph
		# Result.
		first_valid = {
			triggered_desc = {
				trigger = { "list_size(investigated_persons_list)" >= 4 }
				desc = gdp_merchant_unity_contract.1012.desc.result.outro.all_done
			}
			triggered_desc = {
				trigger = { scope:result = flag:success }
				desc = gdp_merchant_unity_contract.1012.desc.result.outro.success
			}
			triggered_desc = {
				trigger = { scope:result = flag:failure }
				desc = gdp_merchant_unity_contract.1012.desc.result.outro.failure
			}
			triggered_desc = {
				trigger = { scope:result = flag:bribe }
				desc = gdp_merchant_unity_contract.1012.desc.result.outro.bribe
			}
			triggered_desc = {
				trigger = { scope:result = flag:reimburse }
				desc = gdp_merchant_unity_contract.1012.desc.result.outro.reimburse
			}
		}
	}
	theme = trade
	left_portrait = {
		character = scope:partner
		animation = debating
		trigger = { scope:method = flag:partner }
	}
	center_portrait = {
		character = root
		triggered_animation = {
			trigger = { scope:method = flag:reimburse }
			animation = steward
		}
		triggered_animation = {
			trigger = { scope:method = flag:bribe }
			animation = bribing
		}
		triggered_animation = {
			trigger = { scope:result = flag:success }
			animation = debating
		}
		triggered_animation = {
			trigger = { scope:result = flag:failure }
			animation = stress
		}
		camera = camera_event_scheme_center
	}
	right_portrait = {
		character = scope:char_current
		triggered_animation = {
			trigger = { scope:method = flag:reimburse }
			animation = admiration
		}
		triggered_animation = {
			trigger = {
				scope:method = flag:bribe
				scope:result = flag:success
			}
			animation = happiness
		}
		triggered_animation = {
			trigger = { scope:method = flag:bribe }
			animation = anger
		}
		triggered_animation = {
			trigger = { scope:result = flag:success }
			animation = worry
		}
		triggered_animation = {
			trigger = { scope:result = flag:failure }
			animation = dismissal
		}
		triggered_animation = {
			trigger = { scope:result = flag:ignored }
			animation = anger
		}
	}
	lower_left_portrait = scope:portrait_1
	lower_center_portrait = scope:portrait_2
	lower_right_portrait = scope:portrait_3
	# Backgrounds indented +1 for easier folding.
		override_background = {
			trigger = { scope:business = flag:local_ruler }
			reference = council_chamber
		}
		override_background = {
			trigger = { scope:business = flag:courtier }
			reference = council_chamber
		}
		override_background = {
			trigger = { scope:business = flag:merchant }
			reference = study
		}
		override_background = {
			trigger = { scope:business = flag:docks }
			reference = docks
		}
		override_background = {
			trigger = { scope:business = flag:market }
			reference = market_crowd
		}
		override_background = {
			trigger = { scope:business = flag:smithy }
			reference = gdp_smithy_workshop
		}
		override_background = {
			trigger = { scope:business = flag:tannery }
			reference = gdp_tannery
		}
		override_background = {
			trigger = { scope:business = flag:carpentry }
			reference = gdp_carpentry_workshop
		}
		override_background = {
			trigger = { scope:business = flag:town_watch }
			reference = armory
		}

	immediate = {
		custom_tooltip = gdp_merchant_unity_contract.1011.tt.current_assurances
		custom_tooltip = gdp_merchant_unity_contract.1011.tt.chars_checked
		# Try to grab our partner.
		gdp_merchant_unity_contract_1010_find_partner_effect = yes
	}
	
	option = { # Investigate scope:local_ruler (person_b) next.
		name = gdp_merchant_unity_contract.1012.a
		trigger = {
			NOT = {
				scope:local_ruler = { is_in_list = investigated_persons_list }
			}
		}
		custom_tooltip = gdp_merchant_unity_contract.1012.tt.boilerplate.continue_investigation
		scope:local_ruler = { save_scope_as = char_current }
		trigger_event = gdp_merchant_unity_contract.1011
		ai_chance = {
			# Select a new target we haven't investigated yet at random.
			base = 1000
		}
	}
	
	option = { # Investigate scope:person_c next.
		name = gdp_merchant_unity_contract.1012.b
		trigger = {
			NOT = {
				scope:person_c = { is_in_list = investigated_persons_list }
			}
		}
		custom_tooltip = gdp_merchant_unity_contract.1012.tt.boilerplate.continue_investigation
		scope:person_c = { save_scope_as = char_current }
		trigger_event = gdp_merchant_unity_contract.1011
		ai_chance = {
			# Select a new target we haven't investigated yet at random.
			base = 1000
		}
	}
	
	option = { # Investigate scope:person_d next.
		name = gdp_merchant_unity_contract.1012.c
		trigger = {
			NOT = {
				scope:person_d = { is_in_list = investigated_persons_list }
			}
		}
		custom_tooltip = gdp_merchant_unity_contract.1012.tt.boilerplate.continue_investigation
		scope:person_d = { save_scope_as = char_current }
		trigger_event = gdp_merchant_unity_contract.1011
		ai_chance = {
			# Select a new target we haven't investigated yet at random.
			base = 1000
		}
	}
	
	option = { # Make your declaration.
		name = gdp_merchant_unity_contract.1012.d
		custom_tooltip = gdp_merchant_unity_contract.1012.tt.boilerplate.end_investigation
		trigger_event = gdp_merchant_unity_contract.1021
	}
}

#	Settlement.
gdp_merchant_unity_contract.1021 = {
	type = character_event
	title = gdp_merchant_unity_contract.1021.t
	window = visit_settlement_window
	desc = {
		desc = gdp_merchant_unity_contract.1021.desc.intro
		first_valid = {
			triggered_desc = {
				trigger = { scope:task_contract.var:assurances_given >= 3 }
				desc = gdp_merchant_unity_contract.1021.desc.assurances_given.three
			}
			triggered_desc = {
				trigger = { scope:task_contract.var:assurances_given >= 2 }
				desc = gdp_merchant_unity_contract.1021.desc.assurances_given.two
			}
			desc = gdp_merchant_unity_contract.1021.desc.assurances_given.one_or_less
		}
		desc = gdp_merchant_unity_contract.1021.desc.body
		first_valid = {
			triggered_desc = {
				trigger = { scope:irregularity = flag:missing }
				desc = gdp_merchant_unity_contract.1021.desc.irregularity.missing
			}
			triggered_desc = {
				trigger = { scope:irregularity = flag:quality }
				desc = gdp_merchant_unity_contract.1021.desc.irregularity.quality
			}
			triggered_desc = {
				trigger = { scope:irregularity = flag:dues }
				desc = gdp_merchant_unity_contract.1021.desc.irregularity.dues
			}
		}
	}
	theme = trade
	left_portrait = {
		character = root
		animation = thinking
	}
	center_portrait = {
		character = scope:local_ruler
		animation = war_over_win
	}
	right_portrait = {
		character = scope:trader
		animation = worry
	}
	lower_left_portrait = scope:person_c
	lower_center_portrait = scope:person_d
	override_background = { reference = market }

	immediate = {
		custom_tooltip = gdp_merchant_unity_contract.1011.tt.current_assurances
		custom_tooltip = gdp_merchant_unity_contract.1011.tt.chars_checked
		# And what does that mean?
		## Max assurances — exceptional within reach.
		if = {
			limit = { scope:task_contract.var:assurances_given >= 4 }
			custom_tooltip = gdp_merchant_unity_contract.1021.tt.clue_total.exceptional_achievable
		}
		## 2-3 assurances — normal within reach.
		else_if = {
			limit = { scope:task_contract.var:assurances_given >= 2 }
			custom_tooltip = gdp_merchant_unity_contract.1021.tt.clue_total.normal_achievable
		}
		## 1 assurances — failure possible if they're forced.
		else = { custom_tooltip = gdp_merchant_unity_contract.1021.tt.clue_total.failure_possible }
	}

	# Attempt to smooth over.
	option = {
		name = gdp_merchant_unity_contract.1021.a
		custom_tooltip = gdp_merchant_unity_contract.1021.a.tt
		show_as_tooltip = {
			scope:task_contract = {
				if = {
					limit = { var:assurances_given >= 3 }
					scope:task_contract = { complete_task_contract = success_critical }
				}
				else_if = {
					limit = { var:assurances_given >= 2 }
					scope:task_contract = { complete_task_contract = success_standard }
				}
				else = {
					random_list = {
						100 = {
							show_chance = no
							desc = gdp_merchant_unity_contract.1021.a.tt.success_qualified
							complete_task_contract = success_qualified_insufficient_assurances
						}
						100 = {
							show_chance = no
							desc = gdp_merchant_unity_contract.1021.a.tt.failure_standard
							complete_task_contract = failure_standard
						}
					}
				}
			}
		}
		if = {
			limit = { scope:task_contract.var:assurances_given < 2 }
			# A calm or craven scope:local_ruler will never do this.
			if = {
				limit = {
					scope:local_ruler = {
						OR = {
							has_trait = calm
							has_trait = craven
						}
					}
				}
				save_scope_value_as = {
					name = total_result
					value = flag:success_qualified_insufficient_assurances
				}
			}
			# A wrathful scope:local_ruler will _only_ do this.
			else_if = {
				limit = {
					scope:local_ruler = { has_trait = wrathful }
				}
				save_scope_value_as = {
					name = total_result
					value = flag:failure_standard
				}
			}
			# Everyone else rolls the highest of their boldness or vengeance, min 20%.
			else = {
				random = {
					chance = {
						if = {
							limit = { scope:local_ruler.ai_vengefulness >= scope:local_ruler.ai_boldness }
							add = scope:local_ruler.ai_vengefulness
						}
						else = { add = scope:local_ruler.ai_boldness }
						min = 20
						# Plus weight this down a bit for every person we bribed.
						every_in_list = {
							list = bribed_chars_list
							add = -10
						}
					}
					save_scope_value_as = {
						name = total_result
						value = flag:failure_standard
					}
				}
				# If our roll produced no scope, then that means we've opted for peace.
				if = {
					limit = {
						NOT = { exists = scope:total_result }
					}
					save_scope_value_as = {
						name = total_result
						value = flag:success_qualified_insufficient_evidence
					}
					scope:task_contract = { complete_task_contract = success_qualified_insufficient_evidence }
				}
			}
		}
		trigger_event = gdp_merchant_unity_contract.1022
		stress_and_fulfillment_impact = {
			just = minor_stress_impact_loss
		}
		ai_chance = {
			base = 1
			modifier = {
				add = 200
				scope:task_contract.var:assurances_given >= 2
			}
		}
	}

	# Deny, deny, deny.
	option = {
		name = gdp_merchant_unity_contract.1021.c
		custom_tooltip = gdp_merchant_unity_contract.1021.c.tt
		show_as_tooltip = {
			scope:task_contract = { complete_task_contract = failure_denial }
		}
		trigger_event = gdp_merchant_unity_contract.1023
		stress_and_fulfillment_impact = {
			honest = minor_stress_impact_gain
			just = minor_stress_impact_gain
			craven = minor_stress_impact_gain
			arbitrary = minor_stress_impact_loss
		}
		ai_chance = {
			base = 1
			ai_value_modifier = {
				ai_boldness = 1
				ai_honor = -1
			}
		}
	}

	# Punish.
	option = {
		name = gdp_merchant_unity_contract.1021.d
		trigger = {
			NOT = { is_spouse_of = scope:trader }
		}
		show_as_unavailable = { always = yes }
		custom_tooltip = gdp_merchant_unity_contract.1021.d.tt
		show_as_tooltip = {
			scope:task_contract = { complete_task_contract = success_qualified_betrayed_trader }
		}
		if = {
			limit = { has_relation_lover = scope:trader }
			remove_relation_lover = scope:trader
			stress_and_fulfillment_impact = {
				base = minor_stress_impact_gain
				compassionate = minor_stress_impact_gain
			}
		}
		if = {
			limit = { has_relation_friend = scope:trader }
			remove_relation_friend = scope:trader
			stress_and_fulfillment_impact = {
				base = minor_stress_impact_gain
				compassionate = minor_stress_impact_gain
			}
		}
		trigger_event = gdp_merchant_unity_contract.1024
		stress_and_fulfillment_impact = {
			callous = minor_stress_impact_loss
			just = minor_stress_impact_gain
			compassionate = minor_stress_impact_gain
		}
		ai_chance = {
			base = 1
			ai_value_modifier = {
				ai_compassion = -1
				ai_honor = -1
			}
		}
	}
}

scripted_trigger gdp_merchant_unity_contract_1022_valid_admirer_trigger = {
	"opinion(root)" >= 25
	OR = {
		sum_of_all_skills_and_prowess_value >= 60
		average_of_all_skills_and_prowess >= 10
		highest_skill_value >= extremely_high_skill_rating
	}
}

#	Conclusion: tried to smooth over.
gdp_merchant_unity_contract.1022 = {
	type = character_event
	title = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:total_result = flag:success_critical }
				desc = gdp_merchant_unity_contract.1022.t.success_critical
			}
			triggered_desc = {
				trigger = { scope:total_result = flag:success_standard }
				desc = gdp_merchant_unity_contract.1022.t.success_standard
			}
			triggered_desc = {
				trigger = { scope:total_result = flag:success_qualified_insufficient_assurances }
				desc = gdp_merchant_unity_contract.1022.t.success_qualified_insufficient_assurances
			}
			triggered_desc = {
				trigger = { scope:total_result = flag:failure_standard }
				desc = gdp_merchant_unity_contract.1022.t.failure_standard
			}
		}
	}
	window = visit_settlement_window
	desc = {
		first_valid = {
			desc = {
				first_valid = {
					triggered_desc = {
						trigger = { scope:total_result = flag:success_critical }
						desc = gdp_merchant_unity_contract.1022.desc.success_critical
					}
					triggered_desc = {
						trigger = { scope:total_result = flag:success_standard }
						desc = gdp_merchant_unity_contract.1022.desc.success_standard
					}
					triggered_desc = {
						trigger = { scope:total_result = flag:success_qualified_insufficient_evidence }
						desc = gdp_merchant_unity_contract.1022.desc.success_qualified_insufficient_evidence
					}
					triggered_desc = {
						trigger = { scope:total_result = flag:failure_standard }
						desc = gdp_merchant_unity_contract.1022.desc.failure_standard
					}
				}
			}
		}
	}
	theme = trade
	left_portrait = {
		character = root
		triggered_animation = {
			trigger = { scope:total_result = flag:failure_standard }
			animation = stress
		}
		triggered_animation = {
			trigger = { scope:total_result = flag:success_qualified_insufficient_evidence }
			animation = worry
		}
		triggered_animation = {
			trigger = {
				OR = {
					scope:total_result = flag:success_critical
					scope:total_result = flag:success_standard
				}
			}
			animation = happiness
		}
	}
	center_portrait = {
		character = scope:local_ruler
		triggered_animation = {
			trigger = { scope:total_result = flag:failure_standard }
			animation = disgust
		}
		triggered_animation = {
			trigger = { scope:total_result = flag:success_qualified_insufficient_evidence }
			animation = war_over_win
		}
		triggered_animation = {
			trigger = {
				OR = {
					scope:total_result = flag:success_critical
					scope:total_result = flag:success_standard
				}
			}
			animation = war_over_tie
		}
	}
	right_portrait = {
		character = scope:trader
		triggered_animation = {
			trigger = { scope:total_result = flag:failure_standard }
			animation = disbelief
		}
		triggered_animation = {
			trigger = { scope:total_result = flag:success_qualified_insufficient_evidence }
			animation = war_over_tie
		}
		triggered_animation = {
			trigger = {
				OR = {
					scope:total_result = flag:success_critical
					scope:total_result = flag:success_standard
				}
			}
			animation = admiration
		}
	}
	lower_left_portrait = scope:person_c
	lower_center_portrait = scope:person_d
	override_background = { reference = market }

	immediate = {
		# Alrighty, let's process our results.
		## We got a crit success.
		if = {
			limit = { scope:task_contract.var:assurances_given >= 3 }
			save_scope_value_as = {
				name = total_result
				value = flag:success_critical
			}
			scope:task_contract = { complete_task_contract = success_critical }
		}
		## We got a standard success.
		else_if = {
			limit = { scope:task_contract.var:assurances_given >= 2 }
			save_scope_value_as = {
				name = total_result
				value = flag:success_standard
			}
			scope:task_contract = { complete_task_contract = success_standard }
		}
		## We got a qualified success.
		else_if = {
			limit = { scope:total_result = flag:success_qualified_insufficient_assurances }
			scope:task_contract = { complete_task_contract = success_qualified_insufficient_assurances }
		}
		## We done failed.
		else = {
			scope:task_contract = { complete_task_contract = failure_standard }
		}
	}

	# Critical success!
	option = {
		name = gdp_merchant_unity_contract.1022.a
		trigger = { scope:total_result = flag:success_critical }
	}

	# Success!
	option = {
		name = gdp_merchant_unity_contract.1022.b
		trigger = { scope:total_result = flag:success_standard }
	}

	# Partial success!
	option = {
		name = gdp_merchant_unity_contract.1022.c
		trigger = { scope:total_result = flag:success_qualified_insufficient_assurances }
	}

	# Failure!
	option = {
		name = gdp_merchant_unity_contract.1022.d
		trigger = { scope:total_result = flag:failure_standard }
	}

	after = {
		if = {
			limit = { scope:total_result = flag:success_critical }
			# See if we might have an admirer.
			## First, grab our candidates.
			scope:person_c = {
				if = {
					limit = { gdp_merchant_unity_contract_1022_valid_admirer_trigger = yes }
					add_to_list = potential_admirers_list
				}
			}
			scope:person_d = {
				if = {
					limit = { gdp_merchant_unity_contract_1022_valid_admirer_trigger = yes }
					add_to_list = potential_admirers_list
				}
			}
			## Then, nab the best fit.
			ordered_in_list = {
				list = potential_admirers_list
				order_by = {
					add = sum_of_all_skills_and_prowess_value
					add = average_of_all_skills_and_prowess
					add = highest_skill_value
				}
				save_scope_as = hopeful
				random = {
					chance = 50
					root = {
						trigger_event = {
							id = gdp_merchant_unity_contract.1031
							years = { 3 6 }
						}
					}
				}
			}
		}
		current_travel_plan = { resume_travel_plan = yes }
		return_home = yes
		scope:person_c = { silent_disappearance_ai_if_created_effect = yes }
		scope:person_d = { silent_disappearance_ai_if_created_effect = yes }
	}
}

#	Conclusion: denial.
gdp_merchant_unity_contract.1023 = {
	type = character_event
	title = gdp_merchant_unity_contract.1023.t
	window = visit_settlement_window
	desc = gdp_merchant_unity_contract.1023.desc
	theme = trade
	left_portrait = {
		character = root
		animation = debating
	}
	center_portrait = {
		character = scope:trader
		animation = dismissal
	}
	right_portrait = {
		character = scope:local_ruler
		animation = war_over_win
	}
	lower_left_portrait = scope:person_c
	lower_center_portrait = scope:person_d
	override_background = { reference = market }

	immediate = {
		scope:task_contract = { complete_task_contract = failure_denial }
	}

	option = {
		name = gdp_merchant_unity_contract.1023.a
	}

	after = {
		hidden_effect = {
			current_travel_plan = { resume_travel_plan = yes }
			return_home = yes
		}
		scope:person_c = { silent_disappearance_ai_if_created_effect = yes }
		scope:person_d = { silent_disappearance_ai_if_created_effect = yes }
	}
}

#	Conclusion: punish.
gdp_merchant_unity_contract.1024 = {
	type = character_event
	title = gdp_merchant_unity_contract.1024.t
	window = visit_settlement_window
	desc = gdp_merchant_unity_contract.1024.desc
	theme = trade
	left_portrait = {
		character = root
		animation = war_over_win
	}
	center_portrait = {
		character = scope:local_ruler
		animation = dismissal
	}
	right_portrait = {
		character = scope:trader
		animation = prisonhouse
		override_imprisonment_visuals = yes
	}
	lower_left_portrait = scope:person_c
	lower_center_portrait = scope:person_d
	override_background = { reference = market }

	immediate = {
		scope:task_contract = { complete_task_contract = success_qualified_betrayed_trader }
	}

	option = {
		name = gdp_merchant_unity_contract.1024.a
	}

	after = {
		hidden_effect = {
			current_travel_plan = { resume_travel_plan = yes }
			return_home = yes
		}
		scope:person_c = { silent_disappearance_ai_if_created_effect = yes }
		scope:person_d = { silent_disappearance_ai_if_created_effect = yes }
	}
}

#	Follow-up: impressed secondary char follows you.
gdp_merchant_unity_contract.1031 = {
	type = character_event
	title = gdp_merchant_unity_contract.1031.t
	desc = gdp_merchant_unity_contract.1031.desc
	theme = trade
	left_portrait = {
		character = root
		animation = thinking
	}
	right_portrait = {
		character = scope:hopeful
		animation = admiration
	}
	override_background = { reference = market }

	trigger = {
		is_location_valid_for_travel_event_on_land = yes
		government_has_flag = government_is_merchant
		scope:hopeful = {
			is_ruler = no
			is_available_quick = {
				ai = yes
				alive = yes
				imprisoned = no
				incapable = no
			}
			is_alive = yes
			is_ruler = no
			is_imprisoned = no
			NOR = {
				liege ?= { is_ai = no }
				any_spouse = { is_ai = no }
			}
		}
	}

	immediate = {
		scope:hopeful = {
			set_location = { location = root.location }
		}
	}

	# I accept.
	option = {
		name = gdp_merchant_unity_contract.1031.a
		add_courtier = scope:hopeful
		reverse_add_opinion = {
			target = scope:hopeful
			modifier = grateful_opinion
			opinion = 150
		}
		hidden_effect = {
			reverse_add_opinion = {
				target = scope:hopeful
				modifier = recruited_me_opinion
				opinion = 50
			}
		}
		ai_chance = {
			# Free courtier.
			base = 1000
		}
	}

	# You'd be a waste of salt.
	option = {
		name = gdp_merchant_unity_contract.1031.b
		add_prestige = minor_prestige_gain
		hidden_effect = {
			scope:hopeful = { silent_disappearance_effect = yes }
		}
		stress_and_fulfillment_impact = {
			callous = medium_stress_impact_loss
			sadistic = medium_stress_impact_loss
			compassionate = minor_stress_impact_gain
		}
		# No AI chance — we always want them to take the courtier.
	}
}

#	Handling expiry.
gdp_merchant_unity_contract.1041 = {
	type = character_event
	hidden = yes

	trigger = {
		any_character_task_contract = { this = scope:task_contract }
		NOT = {
			any_character_active_contract = { this = scope:task_contract }
		}
	}

	immediate = {
		scope:task_contract = { set_variable = expired }
	}
}

gdp_merchant_unity_contract.9999 = { # suppressor
	type = character_event
	hidden = yes
	orphan = yes

	trigger = {
		exists = var:expiry
	}

	immediate = {
		set_variable = expiry
	}	
}
