﻿
ai_release_concubine_interaction = {
	hidden = yes
	
	ai_targets = {
		ai_recipients = courtiers
	}
	ai_target_quick_trigger = {
		adult = yes
	}
	ai_frequency = 1

	ai_potential = {
		is_ruler = yes
		is_landed = yes
		is_adult = yes
	}

	is_shown = {
		scope:recipient = { 
			is_courtier_of = scope:actor 
			is_concubine = yes
			NOT = { location = concubinist.location }
		}
	}

	is_valid_showing_failures_only = {
		scope:recipient = { 
			NOT = { is_concubine_of = scope:actor }
		}
	}

	auto_accept = yes

	ai_will_do = {
		base = 100
	}

	on_accept = {
		scope:recipient = {
			if = {
				limit = { 
					has_opinion_modifier = {
						target = scope:recipient.concubinist
						modifier = forced_me_concubine_marriage_opinion
					}
				}
				add_opinion = {
					target = scope:actor
					modifier = grateful_opinion
					opinion = 15
				}
			}
		}
		scope:recipient.concubinist = { 
			remove_concubine = scope:recipient 
			add_opinion = {
				target = scope:actor
				modifier = stole_concubine_opinion
			}
		}		
	}
}

release_concubine_interaction = {
	interface_priority = 31
	common_interaction = yes
	category = interaction_category_prison
	special_interaction = release_from_prison_interaction
	icon = prison

	desc = release_concubine_interaction_desc

	notification_text = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:demand_conversion = no
					scope:renounce_claims = no
					scope:banish = no
					scope:take_vows = no
					scope:recruit = no
					scope:disfigure = no
					scope:blind = no
					scope:castrate = no
				}
				desc = RELEASE_PRISONER_OFFER
			}
			desc = RELEASE_PRISONER_OFFER_CONDITIONAL
		}
	}

	is_shown = {
		scope:recipient = {
			NOT = { is_concubine_of = scope:actor }
			is_imprisoned_by = scope:actor
			is_concubine = yes
		}
	}

	is_valid_showing_failures_only = {
		scope:recipient = {
			custom_description = {
				text = "currently_being_tortured"
				NOT = { has_character_flag = is_being_tortured }
			}
		}
		cannot_release_former_regent_whilst_old_regent_holds_power_trigger = {
			PRISONER = scope:recipient
			REGENT = scope:recipient.var:imprisoned_by_diarch
		}
	}

	on_accept = {
		#MOD BEGIN
		scope:recipient = {
			if = {
				limit = { 
					has_opinion_modifier = {
						target = scope:recipient.concubinist
						modifier = forced_me_concubine_marriage_opinion
					}
				}
				add_opinion = {
					target = scope:actor
					modifier = grateful_opinion
					opinion = 15
				}
			}
		}
		scope:recipient.concubinist = { 
			remove_concubine = scope:recipient 
			if = {
				limit = { scope:recruit = no}
				remove_courtier_or_guest = scope:recipient
			}
			add_opinion = {
				target = scope:actor
				modifier = stole_concubine_opinion
			}
		}
		#MOD END
		if = {
			limit = { scope:recipient = { has_character_modifier = allowed_to_go_outside } }
			scope:recipient = { remove_character_modifier = allowed_to_go_outside }
		}
		if = {
			limit = { scope:recipient = { has_character_modifier = moldy_gruel_diet } }
			scope:recipient = { remove_character_modifier = moldy_gruel_diet }
		}
		if = {
			limit = { scope:recipient = { is_imprisoned_by = scope:actor } }

			scope:recipient = {
				# Demand Conversion
				if = {
					limit = { scope:demand_conversion = yes }
					demand_conversion_interaction_effect = yes
					add_opinion = {
						modifier = demanded_my_conversion_opinion
						target = scope:actor
					}

					# Struggle Catalyst
					if = {
						limit = {
							scope:actor = {
								any_character_struggle = {
									involvement = involved
									activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
										CATALYST = catalyst_demanding_important_conversion
										CHAR = scope:recipient
									}
								}
							}
						}
						scope:actor = {
							every_character_struggle = {
								involvement = involved
								limit = {
									activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
										CATALYST = catalyst_demanding_important_conversion
										CHAR = scope:recipient
									}
								}
								activate_struggle_catalyst = {
									catalyst = catalyst_demanding_important_conversion
									character = scope:actor
								}
							}
						}
					}
				}

				# Banish Landed Character
				if = {
					limit = {
						scope:banish = yes
						scope:recipient = { is_landed_or_landless_administrative = yes }
					}
					banish_effect = { BANISHER = scope:actor }
					if = {
						limit = {
							NOT = {
								scope:actor = {
									has_banish_reason = scope:recipient
								}
							}
						}
						scope:actor = { add_tyranny = banishment_tyranny_gain }
					}
					add_opinion = {
						modifier = banished_me
						target = scope:actor
					}
				}
				# Banish Unlanded Character
				else_if = {
					limit = {
						AND = {
							scope:banish = yes
							scope:recipient = {
								is_landless_adventurer = no
								is_pool_character = no # It is possible for them to end up in the pool between the interaction being sent, and it being accepted
							}
						}
					}
					if = {
						limit = {
							this.gold > 0
							NOT = {
								has_character_flag = realm_priest
							}
						}
						pay_short_term_gold = { target = scope:actor gold = this.gold }
					}
					if = {
						limit = {
							has_any_artifact = yes
						}
						every_character_artifact = {
							set_owner = {
								target = scope:actor
								history = {
									location = scope:recipient.location
									actor = scope:recipient
									recipient = scope:actor
									type = stolen
								}
							}
							scope:actor = {
								if = {
									limit = {
										NOT = { has_variable = stolen_artifact }
									}
									set_variable = {
										name = stolen_artifact
										value = 1
									}
								}
								else = {
									change_variable = {
										name = stolen_artifact
										add = 1
									}
								}
							}
						}
					}
					banish = yes
					add_opinion = {
						modifier = banished_me
						target = scope:actor
					}
				}
				# 'Banish' Pool Character
				else_if = {
					limit = {
						AND = {
							scope:banish = yes
							scope:recipient = {
								is_playable_character = no
								is_pool_character = yes # They'll just leave for the pool
							}
						}
					}
					if = {
						limit = {
							this.gold > 0
						}
						pay_short_term_gold = { target = scope:actor gold = this.gold }
					}
					add_opinion = {
						modifier = banished_me
						target = scope:actor
					}
				}

				# Recruit Character
				if = {
					limit = { scope:recruit = yes }
					if = {
						limit = {
							scope:actor = {
								culture = {
									has_cultural_parameter = can_recruit_prisoners_easily
								}
							}
						}
						add_opinion = {
							modifier = loyal_servant
							target = scope:actor
						}
						scope:actor = {
							if = {
								limit = {
									can_add_hook = {
										target = scope:recipient
										type = loyalty_hook
									}
								}
								add_hook = {
									type = loyalty_hook
									target = scope:recipient
								}
							}
						}
					}
					else = {
						add_opinion = {
							modifier = demanded_recruitment
							target = scope:actor
						}
					}
					scope:actor = {
						if = {
							limit = {
								AND = {
									scope:actor.faith = {
										has_doctrine = tenet_communal_possessions
									}
									scope:recipient = {
										has_trait = peasant_leader
									}
								}
							}
							scope:actor = {
								add_piety = medium_piety_gain
							}
						}
					}
					scope:actor = {
						add_courtier = scope:recipient
					}
				}

				# Renounce Claims
				if = {
					limit = { scope:renounce_claims = yes }
					add_opinion = {
						modifier = demanded_claim_renouncement
						target = scope:actor
					}
					scope:recipient = {
						every_claim = {
							explicit = yes
							limit = {
								save_temporary_scope_as = temp_claim
								OR = {
									holder = scope:actor
									AND = {
										exists = holder
										NOT = { prev = { target_is_liege_or_above = scope:actor } }
										holder = { target_is_liege_or_above = scope:actor }
									}
									scope:actor = {
										any_held_title = {
											is_de_jure_liege_or_above_target = scope:temp_claim
										}
									}
								}
							}
							scope:recipient = { remove_claim = prev }
						}
					}
				}

				# Gain Hook
				if = {
					limit = { scope:gain_hook = yes }
					add_opinion = {
						modifier = demanded_hook
						target = scope:actor
					}
					scope:actor = {
						add_hook = { #Hook effect must match gain_hook option condition
							target = scope:recipient
							type = favor_hook
						}
					}
				}

				# Take Vows
				if = {
					limit = { scope:take_vows = yes }
					send_child_to_clergy_effect = yes
					#Does Byzan go hay-wire with deposing vassals?
					if = {
						limit = {
							scope:actor = {
								is_ai = yes
								culture = { has_cultural_pillar = heritage_byzantine }
							}
						}
						debug_log = debug_ai_byzan_forced_vows_on_prison_release
						debug_log_scopes = yes
					}
					#Does the AI in general go haywire with deposing prisoners?
					else_if = {
						limit = {
							scope:actor = { is_ai = yes }
						}
						debug_log = debug_ai_forced_vows_on_prison_release
						debug_log_scopes = yes
					}
				}

				# Take Vows
				if = {
					limit = { scope:become_executioner = yes }
					release_as_executioner_effect = yes
				}

				# Disfigure
				if = {
					limit = { scope:disfigure = yes }

					scope:recipient = {
						add_character_flag = {
							flag = is_being_tortured
							months = 6 # Players in MP can keep the event window open...
						}
					}

					blind_castrate_and_disfigure_effect = yes #Stress & dread

					show_as_tooltip = {
						scope:recipient = {
							add_trait = disfigured
						}
					}

					torture_blind_castrate_disfigure_opinion_effect = { VERB = disfigured }

					scope:actor = {
						trigger_event = {
							id = prison.1030
							days = 1
						}
					}
				}

				# Blind
				if = {
					limit = { scope:blind = yes }
					scope:recipient = {
						add_character_flag = {
							flag = is_being_tortured
							months = 6 # Players in MP can keep the event window open...
						}
					}

					blind_castrate_and_disfigure_effect = yes #Stress & dread

					show_as_tooltip = {
						scope:recipient = {
							add_trait = blind
						}
					}

					# Merciful Blindings
					scope:actor = {
						if = {
							limit = {
								culture = { has_cultural_parameter = merciful_blinding }
								OR = {
									has_execute_reason = scope:recipient
									has_banish_reason = scope:recipient
								}
							}
							add_piety = medium_piety_gain
						}
					}

					torture_blind_castrate_disfigure_opinion_effect = { VERB = blinded }

					scope:actor = {
						trigger_event = {
							id = prison.1020
							days = 1
						}
					}

					# If we're a clan this interaction affects unity
					if = {
						limit = { scope:actor.culture = { has_cultural_parameter = merciful_blinding } } # If actor's culture has Merciful Blindings, they lose less unity
						add_clan_unity_interaction_effect = {
							CHARACTER = scope:actor
							TARGET = scope:recipient
							VALUE = minor_unity_loss
							DESC = clan_unity_blinding.desc
							REVERSE_NON_HOUSE_TARGET = no
						}
					}
					else = {
						add_clan_unity_interaction_effect = {
							CHARACTER = scope:actor
							TARGET = scope:recipient
							VALUE = medium_unity_loss
							DESC = clan_unity_blinding.desc
							REVERSE_NON_HOUSE_TARGET = no
						}
					}
				}

				# Castrate
				if = {
					limit = { scope:castrate = yes }
					scope:recipient = {
						add_character_flag = {
							flag = is_being_tortured
							months = 6 # Players in MP can keep the event window open...
						}
					}

					blind_castrate_and_disfigure_effect = yes #Stress & dread

					show_as_tooltip = {
						scope:recipient = {
							if = {
								limit = {
									age < 12
								}
								ep3_child_castration_effect = yes
							}
							else = {
								ep3_youth_castration_effect = yes
							}
						}
					}

					torture_blind_castrate_disfigure_opinion_effect = { VERB = castrated }

					scope:actor = {
						trigger_event = {
							id = prison.1025
							days = 1
						}
					}

					# If we're a clan this interaction affects unity
					add_clan_unity_interaction_effect = {
						CHARACTER = scope:actor
						TARGET = scope:recipient
						VALUE = major_unity_loss
						DESC = clan_unity_abelarding.desc
						REVERSE_NON_HOUSE_TARGET = no
					}
				}

				# No Demands Added
				if = {
					limit = {
						scope:demand_conversion = no
						scope:renounce_claims = no
						scope:banish = no
						scope:gain_hook = no
						scope:take_vows = no
						scope:become_executioner = no
						scope:recruit = no
						scope:disfigure = no
						scope:blind = no
						scope:castrate = no
					}
					# Notification to the imprisoner
					scope:actor = {
						send_interface_toast = {
							type = event_toast_effect_neutral
							title = recipient_released_from_prison
							left_icon = scope:recipient
							scope:recipient = {
								add_opinion = {
									modifier = released_from_prison
									target = scope:actor
								}
							}
						}
						scope:actor = {
							add_dread = minor_dread_loss
							stress_impact = {
								sadistic = medium_stress_impact_gain
								callous = minor_stress_impact_gain
							}
						}
						# Struggle impact
						if = {
							limit = {
								any_character_struggle = {
									involvement = involved
									OR = {
										has_struggle_phase_parameter = release_prisoner_diff_culture_gives_prestige
										has_struggle_phase_parameter = release_prisoner_diff_faith_gives_prestige
									}
								}
								is_diff_faith_or_culture_trigger = {
									CHAR = scope:recipient
									STATUS = involved
								}
							}
							add_prestige = medium_prestige_gain
						}

						# Struggle Catalyst
						if = {
							limit = {
								any_character_struggle = {
									involvement = involved
									activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
										CATALYST = catalyst_release_important
										CHAR = scope:recipient
									}
								}
							}
							every_character_struggle = {
								involvement = involved
								limit = {
									activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
										CATALYST = catalyst_release_important
										CHAR = scope:recipient
									}
								}
								activate_struggle_catalyst = {
									catalyst = catalyst_release_important
									character = scope:actor
								}
							}
						}
						if = {
							limit = {
								fp3_struggle_involves_one_supporter_and_one_detractor = {
									FIRST = scope:actor
									SECOND = scope:recipient
								}
								any_character_struggle = {
									involvement = involved
									activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
										CATALYST = catalyst_release_supporter_detractor
										CHAR = scope:recipient
									}
								}
							}
							every_character_struggle = {
								involvement = involved
								limit = {
									activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
										CATALYST = catalyst_release_supporter_detractor
										CHAR = scope:recipient
									}
								}
								activate_struggle_catalyst = {
									catalyst = catalyst_release_supporter_detractor
									character = scope:actor
								}
								log_debug_variable_for_persian_struggle_effect = { VAR = concession_catalyst_release_supporter_detractor }
							}
						}
					}

					# If we're a clan (and not making any demands) this interaction affects unity
					add_clan_unity_interaction_effect = {
						CHARACTER = scope:actor
						TARGET = scope:recipient
						VALUE = minor_unity_gain
						DESC = clan_unity_released_from_own_prison.desc
						REVERSE_NON_HOUSE_TARGET = no
					}

					# LEGITIMACY GAIN FROM RELEASING DUKES AND ABOVE
					if = {
						limit = {
							exists = scope:recipient.primary_title
							scope:recipient.primary_title.tier >= tier_duchy
						}
						# Minus 2, since we want Duchy to be 1, and it's normally 3
						save_scope_value_as = {
							name = title_tier_minus_2
							value = {
								value = scope:recipient.primary_title.tier
								subtract = 2
							}
						}
						scope:actor = {
							send_interface_toast = {
								type = msg_legitimacy_gain
								title = legitimacy_gain_toast
								left_icon = scope:actor
								right_icon = scope:recipient
								add_legitimacy = {
									value = {
										value = miniscule_legitimacy_gain
										multiply = scope:title_tier_minus_2
									}
								}
							}
						}
					}
				}
				else = {
					scope:actor = {
						trigger_event = char_interaction.0160
					}
				}

				# If a child was imprisoned due to yearly_4021 they'll come back to say hi
				if = {
					limit = {
						exists = var:marked_for_revenge_in_event_yearly_4021
					}
					add_opinion = {
						modifier = abandoned_me_opinion
						target = var:marked_for_revenge_in_event_yearly_4021
					}
					if = {
						limit = {
							var:marked_for_revenge_in_event_yearly_4021 = {
								is_alive = yes
								is_ai = no
							}
						}
						save_scope_as = child
						if = {
							limit = { #Make sure they're sadistic!
								NOT = { has_trait = sadistic }
								number_of_personality_traits < personality_trait_limit
							}
							add_trait = sadistic
						}
						var:marked_for_revenge_in_event_yearly_4021 = {
							add_character_flag = { #TO make sure that they don't get spammed about the release
								flag = block_for_prison_release_notification
								days = 10
							}
							trigger_event = yearly.4022
						}
						remove_variable = marked_for_revenge_in_event_yearly_4021
					}
				}

				if = {
					limit = {
						is_imprisoned = yes
						# Release is handled in follow-up events
						scope:disfigure = no
						scope:blind = no
						scope:castrate = no
					}
					release_from_prison = yes
				}
			}

			# FP3 - If prisoner is important for war
			scope:actor = { # FP3
				if = {
					limit = {
						exists = scope:recipient.house
						any_character_war = {
							using_cb = fp3_free_house_member_cb
							is_defender = scope:actor
							primary_attacker.house = scope:recipient.house
						}
					}
					random_character_war = {
						limit = {
							using_cb = fp3_free_house_member_cb
							is_defender = scope:actor
							primary_attacker.house = scope:recipient.house
						}
						primary_attacker = {
							hidden_effect = {
								send_interface_message = {
									type = event_war_good
									title = lesson_war_final_notes_victory_step_1
									left_icon = scope:recipient
									right_icon = scope:actor

									show_as_tooltip = {
										scope:recipient = { release_from_prison = yes }
										add_prestige = major_prestige_gain
										prev.primary_defender = { add_prestige = major_prestige_loss }
									}
								}
							}
							add_prestige = major_prestige_gain
							prev.primary_defender = { add_prestige = major_prestige_loss }
						}
					}
				}
			}
		}
	}

	on_decline = {
		scope:actor = {
			trigger_event = char_interaction.0161
		}
	}

	send_option = {
		flag = demand_conversion
		localization = "RELEASE_DEMAND_CONVERSION"
		is_shown = {
			NOT = { scope:recipient.faith = scope:actor.faith }
		}
		is_valid = {
			trigger_if = {
				limit = {
					scope:actor = { is_ai = yes }
				}
				scope:recipient = {
					OR = {
						is_close_or_extended_family_of = scope:actor
						is_vassal_of = scope:actor
					}
				}
			}

			# Cannot ask Landed Rulers to convert if they're not your vassal
			custom_description = {
				text = cannot_demand_unsubordinate_landed_ruler_conversion
				scope:recipient = {
					trigger_if = {
						limit = {
							is_playable_character = yes
						}
						is_vassal_or_below_of = scope:actor
					}
				}
			}

			# Cannot ask Religious Heads to convert
			custom_description = {
				text = cannot_demand_religious_head_conversion
				NOT = {
					AND = {
						exists = scope:recipient.faith.religious_head
						scope:recipient.faith.religious_head = scope:recipient
					}
				}
			}
			# Cannot ask Holy Order Master to convert
			custom_description = {
				text = cannot_demand_holy_order_master_conversion
				NAND = {
					exists = scope:recipient.faith
					scope:recipient.faith = {
						any_faith_holy_order = { leader = scope:recipient }
					}
				}
			}
		}
	}

	send_option = {
		flag = renounce_claims
		localization = RELEASE_RENOUNCE_CLAIMS
		is_shown = {
			custom_description = {
				text = "release_renounce_claims"
				subject = scope:recipient
				scope:recipient = {
					any_claim = {
						explicit = yes
						save_temporary_scope_as = temp_claim
						OR = {
							holder = scope:actor
							AND = {
								NOT = { prev = { target_is_liege_or_above = scope:actor } }
								trigger_if = {
									limit = {
										exists = holder
									}
									holder = { target_is_liege_or_above = scope:actor }
								}
							}
							scope:actor = {
								any_held_title = {
									is_de_jure_liege_or_above_target = scope:temp_claim
								}
							}
						}
					}
				}
			}
		}
		current_description = {
			desc = RELEASE_RENOUNCE_CLAIMS_DESC
		}
	}

	send_option = {
		flag = banish
		localization = "RELEASE_BANISH"
		is_shown = {
			trigger_if = {
				limit = {
					scope:actor = { is_ai = yes }
				}
				scope:recipient = {
					OR = {
						AND = {
							is_playable_character = no
							is_ruler = no
							gold > scope:actor.gold
							gold > scope:actor.medium_gold_value
							scope:actor.ai_greed > low_negative_ai_value
						}
						AND = {
							is_playable_character = no
							is_ruler = no
							gold > 0
							scope:actor = {
								has_banish_reason = scope:recipient
							}
						}
						AND = {
							is_playable_character = no
							is_ruler = no
							gold > 100
							scope:actor.ai_greed >= high_positive_ai_value
						}
						AND = {
							scope:actor = {
								has_banish_reason = scope:recipient
							}
							scope:actor = {
								opinion = {
									target = scope:recipient
									value <= medium_negative_opinion
								}
							}
						}
					}
				}
			}
			trigger_if = {
				limit = {
					scope:recipient = {
						is_playable_character = yes
					}
				}
				scope:recipient = {
					is_vassal_of = scope:actor
				}
			}
			trigger_else = {
				scope:recipient = {
					is_courtier_of = scope:actor
				}
			}
			custom_description = {
				text = "release_banish_invalid_take_vows"
				object = scope:recipient
				scope:take_vows = no
			}
			scope:recruit = no #Should be impossible to have both but just in case
			NOT = {
				scope:recipient = {
					is_spouse_of = scope:actor
				}
			}
		}
	}

	send_option = {
		flag = gain_hook
		localization = "RELEASE_GAIN_HOOK"
		is_shown = {
			trigger_if = {
				limit = {
					scope:actor = { is_ai = yes }
				}
				scope:recipient = {
					OR = {
						is_vassal_of = scope:actor
						AND = {
							exists = liege
							liege = scope:actor
						}
					}
				}
			}
		}
		is_valid = {
			scope:actor = {
				can_add_hook = {
					type = favor_hook #Matches the hook added in the on_accept
					target = scope:recipient
				}
			}
		}
	}

	send_option = {
		flag = take_vows
		localization = "RELEASE_TAKE_VOWS"
		is_shown = {
			scope:actor.faith = {
				has_doctrine_parameter = take_vows_active
			}
		}
		is_valid = {
			#Handle AI
			trigger_if = {
				limit = {
					scope:actor = { is_ai = yes }
				}
				#We're AI and Byzantium
				trigger_if = {
					limit = {
						scope:actor = {
							culture = { has_cultural_pillar = heritage_byzantine }
						}
					}
					scope:recipient = {
						is_ai = yes #Not gonna depose player characters, for now
						OR = {
							is_close_or_extended_family_of = scope:actor
							is_vassal_or_below_of = scope:actor #Weigh this for Byz AI in the modifiers below
							has_relation_rival = scope:actor
							is_courtier_of = scope:actor
						}
						NOT = {
							is_heir_of = scope:actor
						}
					}
					#Get rid of rivals despite potentially liking them
					trigger_if = {
						limit = {
							scope:recipient = { has_relation_rival = scope:actor }
						}
						always = yes
					}
					#Only get rid of family members if we dislike them
					trigger_else = {
						scope:actor = {
							opinion = {
								target = scope:recipient
								value <= medium_negative_opinion
							}
						}
					}
				}
				#We're not Byzantium but plain AI
				trigger_else = {
					scope:recipient = {
						is_ai = yes #Not gonna depose player characters, for now
						is_close_or_extended_family_of = scope:actor
						NOT = {
							is_heir_of = scope:actor
						}
					}
					scope:actor = {
						opinion = {
							target = scope:recipient
							value <= medium_negative_opinion
						}
					}
					custom_description = {
						text = "release_vows_invalid_not_your_court"
						object = scope:recipient
						subject = scope:actor
						OR = {
							scope:recipient = { is_courtier_of = scope:actor }
							scope:recruit = yes
						}
					}
				}
			}
			#Enhanced Take the Vows for Byzantium players -- who do we want to boot to a monastery far, far away?
			trigger_else_if = {
				limit = {
					scope:actor = {
						culture = { has_cultural_pillar = heritage_byzantine }
					}
				}
				scope:recipient = {
					is_ai = yes #Not gonna depose player characters, for now
					custom_description = {
						text = "release_byz_vows_invalid"
						object = scope:recipient
						subject = scope:actor
						OR = {
							is_close_or_extended_family_of = scope:actor
							is_vassal_or_below_of = scope:actor
							has_relation_rival = scope:actor
							is_courtier_of = scope:actor
						}
					}
				}
			}
			#Otherwise standard procedure Take the Vows requirements
			trigger_else = {
				scope:recipient = {
					is_ai = yes #Not gonna depose player characters, for now
				}
				scope:actor = { is_close_or_extended_family_of = scope:recipient } #Triggering I_AM_NOT instead of CHARACTER_IS_NOT makes the tooltip look less cursed
				custom_description = {
					text = "release_vows_invalid_not_your_court"
					object = scope:recipient
					subject = scope:actor
					OR = {
						scope:recipient = { is_courtier_of = scope:actor }
						scope:recruit = yes
					}
				}

			}
			custom_description = {
				text = "release_vows_invalid_not_your_faith"
				object = scope:recipient
				subject = scope:actor
				trigger_if = {
					limit = {
						NOT = { scope:recipient.faith = scope:actor.faith }
					}
					scope:demand_conversion = yes
				}
			}
			custom_description = {
				text = "release_executioner_invalid_take_vows_condition"
				object = scope:recipient
				scope:become_executioner = no
			}
			custom_description = {
				text = "release_banish_invalid_take_vows"
				object = scope:recipient
				scope:banish = no
			}
			scope:recipient = {
				age >= 10
				NOR = {
					has_trait = excommunicated
					has_trait = devoted
					has_trait = incapable
				}
				#Do they have the "wrong" marriage type?
				NOR = {
					custom_description = {
						text = is_married_matrilineally
						subject = scope:recipient
						any_spouse = {
							is_female = yes
							matrilinear_marriage = yes
						}
					}
					custom_description = {
						text = is_married_patrilineally
						subject = scope:recipient
						any_spouse = {
							is_male = yes
							patrilinear_marriage = yes
						}
					}
					custom_description = {
						text = matrilinear_betrothal
						subject = scope:recipient
						exists = betrothed
						betrothed = {
							is_female = yes
							matrilinear_betrothal = yes
						}
					}
					custom_description = {
						text = patrilinear_betrothal
						subject = scope:recipient
						exists = betrothed
						betrothed = {
							is_male = yes
							patrilinear_betrothal = yes
						}
					}
				}
			}
		}
	}
	send_option = {
		flag = become_executioner
		localization = "RELEASE_AS_EXECUTIONER"
		is_shown = {
			has_ep1_court_positions_dlc_trigger = yes
			scope:actor = {
				has_royal_court = yes
			}
			scope:recipient = {
				is_adult = yes
			}
		}
		is_valid = {
			scope:actor = {
				NOT = { employs_court_position = executioner_court_position }
			}
			scope:recipient = {
				trigger_if = {
					limit = { scope:recruit = yes }
					custom_description = {
						text = "release_executioner_invalid_recruit"
						object = scope:recipient
						scope:recruit = yes
					}
				}
				trigger_else = {
					is_courtier_of = scope:actor
				}

				custom_description = {
					text = "release_executioner_invalid_take_vows_condition"
					object = scope:recipient
					scope:take_vows = no
				}
				NOT = { is_spouse_of = scope:actor  }
			}
		}
	}

	send_option = {
		flag = recruit
		localization = "RELEASE_RECRUIT"
		is_shown = {
			trigger_if = {
				limit = {
					scope:actor = { is_ai = yes }
				}
				scope:recipient = {
					OR = {
						has_relation_best_friend = scope:actor
						has_relation_friend = scope:actor
						has_relation_lover = scope:actor
						has_relation_soulmate = scope:actor
						has_secret_relation_lover = scope:actor
						is_heir_of = scope:actor
					}
				}
			}
			scope:recipient = {
				is_ruler = no
				NOT = { is_courtier_of = scope:actor }
				OR = {
					is_adult = yes
					AND = {
						is_adult = no
						OR = {
							trigger_if = {
								limit = { exists = liege }
								NOT = { is_heir_of = liege }
							}
							NOT = { exists = liege }
						}
					}
				}
			}
		}
	}

	send_option = {
		flag = disfigure
		localization = "RELEASE_DISFIGURE"
		is_shown = {
			scope:actor = {
				government_allows = administrative
				culture = { has_cultural_tradition = tradition_ep3_palace_politics }
				# If they're our partner emperor, we should use the maiming interaction instead.
				liege_should_systematically_maim_co_ruler_trigger = no
			}
			scope:recipient = {
				is_adult = yes
			}
		}
		is_valid = {
			scope:recipient = {
				NOT = { has_trait = disfigured }
			}
			# Byzantines can't disfigure those not in their realm
			trigger_if = {
				limit = {
					scope:actor = {
						culture = { has_cultural_pillar = heritage_byzantine }
					}
				}
				scope:recipient = {
					custom_description = {
						text = "release_byz_disfigure_invalid"
						object = scope:recipient
						subject = scope:actor
						OR = {
							is_close_or_extended_family_of = scope:actor
							is_vassal_or_below_of = scope:actor
							has_relation_rival = scope:actor
							is_courtier_of = scope:actor
						}
					}
				}
			}
			custom_description = {
				text = "release_prisoner_disfigure_blind_castrate_condition"
				object = scope:recipient
				scope:blind = no
				scope:castrate = no
			}
		}
	}

	send_option = {
		flag = blind
		localization = "RELEASE_BLIND"
		is_shown = {
			scope:actor = {
				is_adult = yes
				culture = { has_cultural_parameter = can_blind_prisoners }
				# If they're our partner emperor, we should use the maiming interaction instead.
				liege_should_systematically_maim_co_ruler_trigger = no
			}
			scope:recipient = {
				is_adult = yes
			}
		}
		is_valid = {
			scope:recipient = {
				NOT = { has_trait = blind }
			}
			custom_description = {
				text = "release_prisoner_disfigure_blind_castrate_condition"
				object = scope:recipient
				scope:disfigure = no
				scope:castrate = no
			}
		}
	}

	send_option = {
		flag = castrate
		localization = "RELEASE_CASTRATE"
		is_shown = {
			scope:recipient = {
				is_imprisoned_by = scope:actor
				is_eunuch_trigger = no
				is_male = yes
			}
			scope:actor = {
				is_adult = yes
				culture = {
					has_cultural_parameter = can_castrate_prisoners
				}
				# If they're our partner emperor, we should use the maiming interaction instead.
				liege_should_systematically_maim_co_ruler_trigger = no
			}
		}
		is_valid = {
			scope:recipient = {
				is_eunuch_trigger = no
			}
			custom_description = {
				text = "release_prisoner_disfigure_blind_castrate_condition"
				object = scope:recipient
				scope:disfigure = no
				scope:blind = no
			}
		}
	}

	send_options_exclusive = no

	auto_accept = {
		scope:demand_conversion = no
		scope:renounce_claims = no
		scope:banish = no
		scope:gain_hook = no
		scope:take_vows = no
		scope:recruit = no
	}

	ai_accept = {
		base = 0
		modifier = {
			add = 100
			desc = "WANTS_FREEDOM_REASON"
		}
		modifier = {
			trigger = {
				scope:recipient = { ai_zeal <= 20 }
				scope:demand_conversion = yes
			}
			add = -20
			desc = "CONVERSION_NEGATIVE_REASON"
		}
		modifier = {
			trigger = {
				scope:recipient = { ai_zeal > 20 }
				scope:demand_conversion = yes
			}
			add = {
				value = ai_zeal
				if = {
					limit = {
						scope:recipient.faith = {
							faith_hostility_level = {
								target = scope:actor.faith
								value = faith_astray_level
							}
						}
					}
					multiply = -1
				}
				else = {
					multiply = -2
				}
			}
			desc = "CONVERSION_NEGATIVE_REASON"
		}
		modifier = {
			add = -25
			trigger = {
				scope:renounce_claims = yes
				ai_greed < 0
			}
			desc = "RENOUNCE_CLAIMS_REASON"
		}
		modifier = {
			add = -50
			trigger = {
				scope:renounce_claims = yes
				ai_greed >= 0
				ai_greed < 26
			}
			desc = "RENOUNCE_CLAIMS_REASON"
		}
		modifier = {
			add = -50
			trigger = {
				scope:become_executioner = yes
				# Sadists don't care about becoming an executioner
				NOR = {
					has_trait = sadistic
					has_trait = callous
				}
			}
			desc = "BECOME_EXECUTIONER_REASON"
		}
		modifier = {
			add = -75
			trigger = {
				scope:renounce_claims = yes
				ai_greed >= 26
				ai_greed < 51
			}
			desc = "RENOUNCE_CLAIMS_REASON"
		}
		modifier = {
			add = -95
			trigger = {
				scope:renounce_claims = yes
				ai_greed >= 51
			}
			desc = "RENOUNCE_CLAIMS_REASON"
		}
		modifier = {
			add = -50
			trigger = {
				scope:banish = yes
			}
			NOT = {
				scope:actor = {
					has_imprisonment_reason = scope:recipient
					culture = { has_cultural_parameter = vassals_more_likely_accept_punishments }
				}
			}
			desc = "BANISH_NEGATIVE_REASON"
		}
		modifier = { # Legalistic tradition
			add = legalistic_vassal_punishment_acceptance
			trigger = {
				scope:banish = yes
			}
			scope:actor = {
				has_imprisonment_reason = scope:recipient
				culture = { has_cultural_parameter = vassals_more_likely_accept_punishments }
			}
			desc = tradition_legalistic_name
		}
		modifier = {
			add = {
				value = -50
				if = {
					limit = {
						scope:recipient = {
							ai_vengefulness > 0
						}
					}
					subtract = ai_vengefulness
				}
			}
			trigger = {
				scope:gain_hook = yes
			}
			desc = "GAIN_HOOK_NEGATIVE_REASON"
		}
		modifier = {
			add = -30
			trigger = {
				scope:take_vows = yes
			}
			desc = "TAKE_VOWS_NEGATIVE_REASON"
		}
		modifier = {
			add = -30
			trigger = {
				scope:take_vows = yes
				has_trait = lustful
			}
			desc = "TAKE_VOWS_LUSTFUL_REASON"
		}
		modifier = {
			add = -30
			trigger = {
				scope:take_vows = yes
				has_trait = rakish
			}
			desc = "TAKE_VOWS_RAKISH_REASON"
		}
		modifier = {
			add = -30
			trigger = {
				scope:take_vows = yes
				has_trait = fornicator
			}
			desc = "TAKE_VOWS_FORNICATOR_REASON"
		}
		modifier = {
			add = -30
			trigger = {
				scope:take_vows = yes
				has_trait = seducer
			}
			desc = "TAKE_VOWS_SEDUCER_REASON"
		}
		modifier = {
			add = -30
			trigger = {
				scope:take_vows = yes
				has_trait = deviant
			}
			desc = "TAKE_VOWS_DEVIANT_REASON"
		}
		modifier = {
			add = {
				value = -15
				multiply = num_sinful_traits
			}
			trigger = {
				scope:take_vows = yes
				num_sinful_traits > 1
			}
			desc = "TAKE_VOWS_SINS_REASON"
		}
		modifier = {
			add = -30
			trigger = {
				scope:take_vows = yes
				has_trait = cynical
			}
			desc = "TAKE_VOWS_CYNICAL_REASON"
		}
		modifier = {
			add = -10
			trigger = {
				scope:recruit = yes
				NOT = {
					scope:actor = {
						culture = {
							has_cultural_parameter = can_recruit_prisoners_easily
						}
					}
				}
			}
			desc = "RECRUITMET_NEGATIVE_REASON"
		}
		# Struggle
		modifier = {
			trigger = {
				scope:demand_conversion = no
				scope:renounce_claims = no
				scope:banish = no
				scope:gain_hook = no
				scope:take_vows = no
				scope:recruit = no
			}
			scope:recipient = {
				any_character_struggle = {
					involvement = involved
				}
			}
			scope:actor = {
				any_character_struggle = {
					involvement = involved
				}
			}
			add = {
				value = 0
				if = {
					limit = {
						scope:recipient = {
							any_character_struggle = {
								phase_has_catalyst = catalyst_release_important
							}
							has_character_flag = agenda_towards_escalation
						}
					}
					add = -100
				}
				else_if = {
					limit = {
						scope:recipient = {
							any_character_struggle = {
								phase_has_catalyst = catalyst_release_important
							}
						}
					}
					add = 200
				}
			}
		}
	}

	# AI
	ai_targets = {
		ai_recipients = prisoners
	}
	ai_frequency = 1

	ai_potential = {
		has_prisoners = yes
	}

	ai_will_do = {
		base = 0

		modifier = {
			add = 20
			scope:demand_conversion = yes
		}
		modifier = {
			add = 100
			scope:demand_conversion = yes
			scope:recipient = {
				is_vassal_of = scope:actor
			}
		}
		modifier = {
			add = 30
			scope:renounce_claims = yes
		}
		modifier = {
			add = 50
			scope:banish = yes
		}
		modifier = {
			add = 30
			scope:take_vows = yes
		}
		modifier = {
			add = 10
			scope:recruit = yes
		}
		modifier = {
			add = 30
			scope:disfigure = yes
		}
		modifier = {
			add = 20
			scope:blind = yes
		}
		modifier = {
			add = 50
			scope:castrate = yes
		}
		modifier = { # Rivals can rot
			add = -40
			scope:actor = {
				NOT = {
					has_trait = forgiving
				}
			}
			scope:recipient = {
				OR = {
					has_relation_rival = scope:actor
					has_relation_nemesis = scope:actor
					scope:recipient = {
						is_spouse_of = scope:actor
						exposed_cheating_on_spouse_trigger = { SPOUSE = scope:actor }
					}
				}
			}
		}
		modifier = { # Rivals can rot FOREVER if vengeful
			add = -100
			scope:actor = {
				NOT = {
					has_trait = forgiving
				}
			}
			scope:actor.ai_vengefulness >= very_high_positive_ai_value
			scope:recipient = {
				OR = {
					has_relation_rival = scope:actor
					has_relation_nemesis = scope:actor
					scope:recipient = {
						is_spouse_of = scope:actor
						exposed_cheating_on_spouse_trigger = { SPOUSE = scope:actor }
					}
				}
			}
		}
		modifier = { # Compassionate characters don't want to keep children in their dungeon for too long
			add = 10
			scope:actor = {
				is_at_war = no
				ai_compassion >= low_positive_ai_value
			}
			scope:recipient = {
				is_adult = no
				time_in_prison = { years > 1 }
			}
		}
		modifier = { # Very compassionate characters tend to release prisoners
			add = 10
			scope:actor = {
				is_at_war = no
				ai_compassion >= very_high_positive_ai_value
			}
			scope:recipient = {
				time_in_prison = { years > 1 }
			}
		}
		modifier = { # Somewhat compassionate characters tend to keep people in their dungeon for a while
			add = 10
			scope:actor = {
				is_at_war = no
				ai_compassion >= medium_positive_ai_value
			}
			scope:recipient = {
				is_playable_character = no
				time_in_prison = { years > 3 }
			}
		}
		modifier = { # Only truly discompassionate characters will have their dungeon full of hapless victims after 5 years
			add = 10
			scope:actor = {
				is_at_war = no
				ai_compassion >= low_negative_ai_value
			}
			scope:recipient = {
				is_playable_character = no
				time_in_prison = { years > 5 }
			}
		}
		modifier = { # Most AI characters will let family out...
			add = 10
			scope:actor = {
				is_at_war = no
				OR = {
					ai_compassion >= very_high_positive_ai_value
					AND = {
						ai_compassion >= high_negative_ai_value
						opinion = {
							target = scope:recipient
							value >= low_negative_opinion
						}
					}
				}
			}
			scope:recipient = {
				time_in_prison = { years > 1 }
				is_playable_character = no
				is_close_family_of = scope:actor
			}
		}
		modifier = { # Almost all will let their own children out
			add = 40
			scope:actor = {
				is_at_war = no
				ai_compassion >= very_high_negative_ai_value
			}
			scope:recipient = {
				is_playable_character = no
				is_child_of = scope:actor
			}
		}
		# Struggle
		modifier = {
			trigger = {
				scope:demand_conversion = no
				scope:renounce_claims = no
				scope:banish = no
				scope:gain_hook = no
				scope:take_vows = no
				scope:recruit = no
			}
			scope:recipient = {
				any_character_struggle = {
					involvement = involved
				}
			}
			scope:actor = {
				any_character_struggle = {
					involvement = involved
				}
			}
			add = {
				value = 0
				if = {
					limit = {
						scope:actor = {
							any_character_struggle = {
								phase_has_catalyst = catalyst_release_important
							}
							has_character_flag = agenda_towards_escalation
						}
					}
					add = -100
				}
				else_if = {
					limit = {
						scope:actor = {
							any_character_struggle = {
								phase_has_catalyst = catalyst_release_important
							}
							NOT = {
								ai_greed >= 25
							}
							OR = {
								ai_compassion > 50
								AND  = {
									ai_compassion > 0
									scope:recipient = {
										time_in_prison = { years > 1 }
									}
								}
							}
						}
					}
					add = 200
				}
			}
		}
		modifier = { # Family Feud
			add = -50
			exists = scope:actor.house
			exists = scope:recipient.house
			scope:actor = {
				house.house_head = {
					any_owned_story = {
						story_type = story_cycle_house_feud
						has_variable = house_feud_house
						var:house_feud_house = scope:recipient.house
					}
				}
				has_opinion_modifier = {
					modifier = house_feud_opinion
					target = scope:recipient.house.house_head
				}
			}
		}
		#Byz AI can shove vassals off to remote monasteries - weigh that here
		modifier = {
			add = -10
			scope:take_vows = yes
			scope:actor = {
				culture = { has_cultural_pillar = heritage_byzantine }
			}
			scope:recipient = { is_vassal_or_below_of = scope:actor }
		}

		evaluate_action_increasing_house_unity = {
			VALUE = 100
		}
		#don't release prisoners that are tied with a prison break contract, we don't want it invalidating all the time
		modifier = {
			factor = 0
			scope:recipient = {
				has_character_flag = being_prisonbroken_by_laamp
			}
		}
	}
}

#For lieges to demand release of their courtiers
demand_release_interaction = {
	interface_priority = 50
	common_interaction = yes
	icon = declare_war_interaction
	category = interaction_category_prison
	can_send_despite_rejection = yes
	use_diplomatic_range = no
	force_notification = yes
	popup_on_receive = yes
	pause_on_receive = yes
	ai_maybe = yes

	redirect = {
		scope:recipient = {
			save_scope_as = secondary_recipient
			if = {
				limit = {
					exists = imprisoner
				}
				imprisoner = { save_scope_as = recipient }
			}
			if = {
				limit = { is_concubine = yes }
				concubinist = { save_scope_as = recipient }
			}
		}
	}

	desc = demand_release_interaction_desc

	greeting = negative
	notification_text = PAY_RANSOM_PROPOSAL
	pre_answer_maybe_key = ANSWER_MIGHT_SUCCEED
	pre_answer_no_key = ANSWER_CANT_SUCCEED
	pre_answer_yes_key = ANSWER_WILL_SUCCEED
	pre_answer_maybe_breakdown_key = ANSWER_SUM_CHANCE
	
	is_shown = {
		primary_title.tier >= tier_county
		exists = scope:recipient
		scope:secondary_recipient = {
			demand_release_filter_trigger = { 
				ACTOR = scope:actor
				RECIPIENT = scope:recipient
			}
		}
		NOT = {
			scope:actor = scope:secondary_recipient
		}
	}

	is_valid_showing_failures_only = {
		scope:recipient = { is_ruler = yes }
		scope:actor = {
			NOT = { 
				is_at_war_with = scope:recipient
				is_imprisoned_by = scope:recipient
			}
		}
		scope:recipient = {
			custom_description = {
				text = "currently_being_tortured"
				NOT = { has_character_flag = is_being_tortured }
			}
		}
		cannot_release_former_regent_whilst_old_regent_holds_power_trigger = {
			PRISONER = scope:recipient
			REGENT = scope:recipient.var:imprisoned_by_diarch
		}
	}
	
	on_accept = {
		# Set up event scopes and trigger the exchange.
		if = {
			limit = { 
				scope:secondary_recipient = {
					OR= {
						is_imprisoned_by = scope:recipient 
						is_concubine_of = scope:recipient
					}
				} 
			}
			scope:secondary_recipient = {
				save_scope_as = prisoner
			}
			scope:recipient = {
				save_scope_as = imprisoner
			}
			scope:actor = {
				save_scope_as = payer
				
				# Stress impacts
				if = {
					limit = {
						scope:prisoner = { # Stress impact for random courtiers
							NOR = {
								is_vassal_of = scope:actor
								is_consort_of = scope:actor
								is_close_family_of = scope:actor
								dynasty ?= {
									this ?= scope:actor.dynasty
								}
								any_spouse = {
									OR = {
										is_close_family_of = scope:actor
										dynasty ?= {
											this ?= scope:actor.dynasty
										}
									}
								}
								has_relation_lover = scope:actor
								has_relation_friend = scope:actor
							}
						}
					}
					stress_impact = {
						compassionate = minor_stress_impact_loss
						generous = medium_stress_impact_loss
					}
				}
				else_if = { # Stress impact for 'relevant' ransoms; in other words, everyone but random courtiers
					limit = {
						scope:prisoner = {
							OR = {
								is_vassal_of = scope:actor
								is_consort_of = scope:actor
								is_close_family_of = scope:actor
								dynasty ?= {
									this ?= scope:actor.dynasty
								}
								any_spouse = {
									OR = {
										is_close_family_of = scope:actor
										dynasty ?= {
											this ?= scope:actor.dynasty
										}
									}
								}
								has_relation_lover = scope:actor
								has_relation_friend = scope:actor
							}
						}
					}
					stress_impact = {
						compassionate = medium_stress_impact_loss
						generous = minor_stress_impact_loss
						just = minor_stress_impact_loss
					}
				}
				
				if = { # Lustful characters want their lovers back
					limit = {
						scope:prisoner = {
							has_relation_lover = scope:actor
						}
					}
					stress_impact = {
						lustful = major_stress_impact_loss
					}
				}
				
				if = { # Loyal characters want to free their friends and lovers
					limit = {
						scope:prisoner = {
							OR = {
								has_relation_friend = scope:actor
								has_relation_lover = scope:actor
							}
						}
					}
					stress_impact = {
						loyal = major_stress_impact_loss
					}
				}
				
				if = { # Family should be free!
					limit = {
						scope:prisoner = {
							is_close_family_of = scope:payer
						}
					}
					stress_impact = {
						family_first = miniscule_stress_impact_loss
					}
				}

				# Must come after we save the payment value, or the payer won't know how much they need to pay.
				trigger_event = char_interaction.0140
			}
			hidden_effect = {
				scope:prisoner = {
					send_interface_toast = {
						type = event_toast_effect_good
						title = ransomed_by_someone
						left_icon = scope:payer
						show_as_tooltip = {
							scope:imprisoner = {
								demand_release_interaction_effect = yes
							}
						}
					}
				}
				scope:prisoner = {
					if = {
						limit = {
							liege ?= {
								is_ai = no
								NOR = {
									this = scope:payer
									this = scope:imprisoner
								}
								OR = {
									is_close_or_extended_family_of = scope:prisoner
									is_consort_of = scope:prisoner
									has_relation_lover = scope:prisoner
									has_relation_friend = scope:prisoner
								}
							}
						}
						liege = {
							save_scope_as = prisoner_liege_scope
							send_interface_toast = {
								type = event_toast_effect_good
								title = ransomed_by_someone_third_party
								left_icon = scope:payer
								right_icon = scope:prisoner
								custom_tooltip = ransomed_by_someone_third_party_desc
							}
						}
					}
				}
			}
			scope:imprisoner = {
				send_interface_message = {
					type = event_generic_neutral
					title = recipient_released_from_prison
					right_icon = scope:prisoner
					left_icon = scope:imprisoner
					demand_release_interaction_effect = yes
				}

				# Struggle Catalyst
				if = {
					limit = {
						any_character_struggle = {
							involvement = involved
							activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
								CATALYST = catalyst_ransom_important
								CHAR = scope:prisoner
							}
						}
					}
					every_character_struggle = {
						involvement = involved
						limit = {
							activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
								CATALYST = catalyst_ransom_important
								CHAR = scope:prisoner
							}
						}
						activate_struggle_catalyst = {
							catalyst = catalyst_ransom_important
							character = scope:imprisoner
						}
					}
				}
			}
			if = {
				limit = { scope:hook = yes }
				scope:actor = {
					use_hook = scope:recipient
				}
			}
		}
		show_as_tooltip = {
			scope:actor = {
				add_opinion = {
					target = scope:recipient
					modifier = pleased_opinion
					opinion = 10
				}
			}
			scope:recipient = {
				add_opinion = {
					target = scope:actor
					modifier = threatened_opinion
				}
				if = {
					limit = { scope:secondary_recipient = { is_concubine_of = scope:recipient } }
					add_opinion = {
						target = scope:actor
						modifier = stole_concubine_opinion
					}
				}
			}
		}
		hidden_effect = {
			scope:actor = {
				add_opinion = {
					target = scope:recipient
					modifier = pleased_opinion
					opinion = 10
				}
			}
			scope:recipient = {
				add_opinion = {
					target = scope:actor
					modifier = threatened_opinion
				}
				if = {
					limit = { scope:secondary_recipient = { is_concubine_of = scope:recipient } }
					add_opinion = {
						target = scope:actor
						modifier = stole_concubine_opinion
					}
				}
			}
		}
	}

	on_decline = {
		# Trigger notification event
		scope:actor = {
			trigger_event = char_interaction.0141
		}
		show_as_tooltip = {
			scope:actor = {
				add_opinion = {
					target = scope:recipient
					modifier = angry_opinion
					opinion = -30
				}
				if = {
					limit = {
						scope:recipient = { 
							OR = { 
								is_independent_ruler = yes
								is_vassal_or_below_of = scope:actor
								liege = scope:actor.liege
							}
						}
					}
					start_war = {
						cb = liberation_cb
						target = scope:recipient
					}
				}
				else_if = {
					limit = { 
						NOR = { 
							top_liege = scope:recipient.top_liege 
							liege = scope:recipient.top_liege 
						} 
					}
					start_war = {
						cb = liberation_cb
						target = scope:recipient.top_liege
					}					
				}
				else = {
					start_war = {
						cb = liberation_cb
						target = scope:recipient.liege
					}					
				}
			}
			scope:secondary_recipient = {
				if = {
					limit = { is_concubine = yes }
					concubine_opinion_on_refusal_effect = {
						ACTOR = scope:actor
						RECIPIENT = scope:recipient
					}
				}
			}
		}
		hidden_effect = {
			scope:actor = {
				add_opinion = {
					target = scope:recipient
					modifier = angry_opinion
					opinion = -30
				}
				if = {
					limit = {
						scope:recipient = { is_independent_ruler = yes }
					}
					start_war = {
						cb = liberation_cb
						target = scope:recipient
					}
				}
				else = {
					start_war = {
						cb = liberation_cb
						target = scope:recipient.top_liege
					}					
				}
			}
			scope:secondary_recipient = {
				concubine_opinion_on_refusal_effect = {
					ACTOR = scope:actor
					RECIPIENT = scope:recipient
				}
			}
		}
	}

	#Use hook
	send_option = {
		is_valid = {
			exists = scope:recipient
			scope:actor = {
				has_usable_hook = scope:recipient
			}
		}
		flag = hook
		localization = SCHEME_HOOK
	}
	should_use_extra_icon = {
		scope:actor = { has_usable_hook = scope:recipient }
	}
	extra_icon = "gfx/interface/icons/character_interactions/hook_icon.dds"

	send_options_exclusive = no
	
	ai_accept = {
		base = 0

		opinion_modifier = { #Compare Opinion modifier.
			trigger = {
				scope:secondary_recipient = { is_imprisoned_by = scope:recipient }
				scope:recipient = {
					opinion = {
						target = scope:secondary_recipient
						value > 0
					}				
				}
			}
			who = scope:secondary_recipient
			opinion_target = scope:actor
			multiplier = 0.2
		}
		opinion_modifier = { #Compare Opinion modifier.
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 0.2
		}
		opinion_modifier = { #Compare Opinion modifier.
			trigger = {
				scope:secondary_recipient = { is_imprisoned_by = scope:recipient }
			}
			who = scope:recipient
			opinion_target = scope:secondary_recipient
			multiplier = 0.4
		}
		opinion_modifier = { #Compare Opinion modifier.
			trigger = {
				scope:secondary_recipient = { is_concubine_of = scope:recipient }
			}
			who = scope:recipient
			opinion_target = scope:secondary_recipient
			multiplier = -0.4
		}
		modifier = {
			add = 80
			scope:recipient = { is_independent_ruler = yes }
			scope:actor.current_military_strength > scope:recipient.current_military_strength
			desc = ARTIFACT_MILITARY_STRONGER_REASON
		}	
		modifier = {
			add = 80
			scope:recipient = { is_independent_ruler = no }
			scope:actor.current_military_strength > scope:recipient.top_liege.current_military_strength
			desc = ARTIFACT_MILITARY_STRONGER_REASON
		}			
		modifier = {
			desc = offer_vassalization_interaction_aibehavior_power_tt
			scope:recipient = { is_independent_ruler = yes }
	  	  	add = {
				value = 1
				subtract = {
					value = scope:recipient.current_military_strength
					divide = { value = scope:actor.current_military_strength min = 1 }
				}
				multiply = 20
				ceiling = yes
	  		}
		}
		modifier = {
			desc = offer_vassalization_interaction_aibehavior_power_tt
			scope:recipient = { is_independent_ruler = no }
	  	  	add = {
				value = 1
				subtract = {
					value = scope:recipient.top_liege.current_military_strength
					divide = { value = scope:actor.current_military_strength min = 1 }
				}
				multiply = 20
				ceiling = yes
	  		}
		}
		modifier = { # An Intimidated recipient is significantly more likely to accept an offer
			add = 30
			trigger = {
				scope:recipient = {
					has_dread_level_towards = {
						target = scope:actor
						level = 1
					}
				}
			}
			desc = INTIMIDATED_REASON
		}
		modifier = { # An Cowed recipient is significantly more likely to accept an offer
			add = 100
			trigger = {
				scope:recipient = {
					has_dread_level_towards = {
						target = scope:actor
						level = 2
					}
				}
			}
			desc = COWED_REASON
		}
		modifier = {
			add = {
				value = ai_boldness
				multiply = -1
				divide = 2
			}
			NOT = { ai_boldness = 0 }
			desc = ARTIFACT_BOLDNESS_REASON
		}
		modifier = {
			add = -50
			scope:actor = {
				has_relation_rival = scope:recipient
			}
			desc = ARTIFACT_RIVAL_REASON
		}
		modifier = {
			add = -80
			scope:actor = {
				has_relation_nemesis = scope:recipient
			}
			desc = ARTIFACT_NEMESIS_REASON
		}
		modifier = { # Say no to rivals
			add = -55
			scope:recipient = {
				has_relation_rival = scope:secondary_recipient
			}
			desc = "RIVAL_TO_ME_REASON"
		}
		modifier = { # Say no to rivals
			add = -300
			scope:recipient = {
				has_relation_nemesis = scope:secondary_recipient
			}
			desc = "NEMESIS_TO_ME_REASON"
		}
		modifier = {
			scope:hook = yes
			add = 100
			desc = SCHEME_WEAK_HOOK_USED
		}
	}

	auto_accept = {
		custom_description = {
			text = "spending_hook"
			subject = scope:actor
			object = scope:recipient
			scope:hook = yes
			scope:actor = { has_strong_hook = scope:recipient }
		}
	}	
	
	# AI
	ai_targets = {
		ai_recipients = family
	}
	ai_targets = {
		ai_recipients = spouses
	}
	ai_targets = {
		ai_recipients = scripted_relations
	}
	ai_targets = {
		ai_recipients = liege
	}
	ai_targets = {
		ai_recipients = neighboring_rulers
		ai_recipients = peer_vassals
		max = 5
	}
	ai_frequency = 3

	ai_potential = {
		gold >= 25
	}

	ai_will_do = {
		base = 0

		modifier = { # Only send if secondary_recipient has negative opinion of recipient
			factor = 0
			scope:secondary_recipient = {
				is_concubine = yes
				opinion = {
					target = scope:recipient
					value > 0
				}
			}
		}

		modifier = { # Only send if ransom tried first
			factor = 0
			scope:recipient = { is_ai = no }
			scope:secondary_recipient = { NOT = { has_character_flag = character_ransom_refused_by_player } }
		}

		modifier = { # Only send if stronger than recipient
			factor = 0
			scope:recipient = { is_independent_ruler = yes }
			scope:actor.max_military_strength <= scope:recipient.max_military_strength
		}

		modifier = { # Only send if stronger than recipient
			factor = 0
			scope:recipient = { is_independent_ruler = no }
			scope:actor.max_military_strength <= { value = scope:recipient.top_liege.max_military_strength add = scope:recipient.max_military_strength }
		}
		
		modifier = {
			add = -100
			has_relation_rival = scope:secondary_recipient
		}
		
		modifier = {
			add = -300
			has_relation_nemesis = scope:secondary_recipient
		}
		
		modifier = { # Only ransom people you care about... unless you're very compassionate
			factor = 0
			ai_compassion < high_positive_ai_value
			scope:secondary_recipient = {
				NOR = {
					is_heir_of = scope:actor
					is_child_of = scope:actor
					is_grandchild_of = scope:actor
					is_great_grandchild_of = scope:actor
					is_consort_of = scope:actor
					is_parent_of = scope:actor
					is_grandparent_of = scope:actor
					is_great_grandparent_of = scope:actor
					has_relation_friend = scope:actor
					has_relation_lover = scope:actor
					has_secret_relation_lover = scope:actor
					any_spouse = {
						OR = {
							is_heir_of = scope:actor
							is_child_of = scope:actor
							is_grandchild_of = scope:actor
							is_great_grandchild_of = scope:actor
							is_parent_of = scope:actor
							is_grandparent_of = scope:actor
							is_great_grandparent_of = scope:actor
							has_relation_friend = scope:actor
						}
					}
				}
			}
		}
		
		modifier = { # If you're very compassionate, well, then you ransom most people you can!
			factor = 0
			ai_compassion >= high_positive_ai_value
			scope:secondary_recipient = {
				NOR = {
					is_heir_of = scope:actor
					is_close_or_extended_family_of = scope:actor
					is_consort_of = scope:actor
					has_relation_friend = scope:actor
					has_relation_lover = scope:actor
					has_secret_relation_lover = scope:actor
					any_spouse = {
						OR = {
							is_heir_of = scope:actor
							is_close_or_extended_family_of = scope:actor
							has_relation_friend = scope:actor
						}
					}
				}
			}
		}
		
		modifier = { # If you're very greedy... then they're a little bit more restrictive!
			factor = 0
			ai_greed >= high_positive_ai_value
			scope:secondary_recipient = {
				NOR = { # Very Greedy rulers only ransom people in their own realm, or their heir
					is_courtier_of = scope:actor
					is_vassal_or_below_of = scope:actor
					is_heir_of = scope:actor
				}
				NOR = {
					is_heir_of = scope:actor
					is_close_or_extended_family_of = scope:actor
					is_consort_of = scope:actor
					has_relation_friend = scope:actor
					has_relation_lover = scope:actor
					has_secret_relation_lover = scope:actor
					any_spouse = {
						OR = {
							is_heir_of = scope:actor
							is_close_or_extended_family_of = scope:actor
							has_relation_friend = scope:actor
						}
					}
				}
			}
		}
		
		# Some exceptions!
		modifier = { # Lustful characters want to ransom lovers
			add = 100
			has_trait = lustful
			ai_greed <= 50
			scope:secondary_recipient = {
				has_relation_lover = scope:actor
			}
			OR = {
				AND = {
					scope:actor = {
						gold >= 25
					}
					scope:secondary_recipient = {
						time_in_prison = { years > 1 }
					}
				}
			}
		}
		
		modifier = { # Loyal characters want to ransom their relations
			add = 100
			has_trait = loyal
			scope:secondary_recipient = {
				OR = {
					has_relation_lover = scope:actor
					has_relation_friend = scope:actor
				}
			}
			OR = {
				AND = {
					scope:actor = {
						gold >= 25
					}
					scope:secondary_recipient = {
						time_in_prison = { years > 1 }
					}
				}
			}
		}
		
		modifier = { # Certain characters will try to ransom their liege!
			add = 100
			ai_greed <= very_high_negative_ai_value
			ai_compassion >= very_high_negative_ai_value
			NOT = { exists = joined_faction }
			is_at_war = no
			OR = {
				has_trait = loyal
				ai_honor >= high_positive_ai_value
			}
			scope:secondary_recipient = {
				is_liege_or_above_of = scope:actor
			}
		}

		# Struggle Agenda
		modifier = {
			scope:secondary_recipient = {
				any_character_struggle = {
					involvement = involved
				}
			}
			scope:actor = {
				any_character_struggle = {
					involvement = involved
				}
			}
			add = {
				value = 0
				if = {
					limit = {
						scope:actor = {
							any_character_struggle = {
								phase_has_catalyst = catalyst_ransom_important
							}
							has_character_flag = agenda_towards_escalation
						}
					}
					add = -100
				}
				else_if = {
					limit = {
						scope:actor = {
							any_character_struggle = {
								phase_has_catalyst = catalyst_ransom_important
							}
						}
					}
					add = 200
				}
			}
		}
		
		modifier = { # Don't annoy players
			factor = 0
			scope:recipient = { is_ai = no }
			scope:recipient = { is_at_war = yes }
		}
	}
}
