﻿#To ransom yourself 
## DTR: Slaves can't ransom themselves
## DTR: lowborn can't ransom themselves
ransom_me_interaction = {
	interface_priority = 50
	common_interaction = yes
	use_diplomatic_range = no
	category = interaction_category_prison
	icon = icon_gold

	greeting = positive
	notification_text = PAY_RANSOM_PROPOSAL_PERSONAL

	desc = ransom_me_interaction_desc

	redirect = { # So that users can ransom themselves by clicking on their portrait
		if = {
			limit = {
				scope:actor = scope:recipient
				exists = scope:actor.imprisoner
			}
			scope:actor.imprisoner = { save_scope_as = recipient }
		}
	}
	
	is_shown = {
		scope:actor = {
			is_imprisoned_by = scope:recipient
			# DTR
			has_trait_slave = no
			NOT = { has_trait = mamluk }
		}
	}

	is_valid_showing_failures_only = {
		# [PoW]
		PoW_can_be_ransomed = {
			PRISONER = scope:actor
			JAILOR = scope:recipient
			PAYER = scope:actor
		}
		# [/PoW]
		scope:recipient = { is_busy_in_events_localised = yes } #Imprisoner
		scope:actor = { #Prisoner 
			is_busy_in_events_localised = yes 
			# DTR
			has_trait_slave = no
			NOT = { has_trait = mamluk }
		} 
		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 = {
		if = {
			limit = { scope:actor = { is_imprisoned_by = scope:recipient } }
			scope:actor = {
				save_scope_as = prisoner
			}
			scope:recipient = {
				save_scope_as = imprisoner
			}
			scope:actor = {
				save_scope_as = payer
				
				# If the payer doesn't have enough funds to pay the full amount and we're only requesting their current funds, save how much they've agreed to pay.
				if = {
					limit = {
						OR = {
							exists = scope:current_gold
							exists = scope:extortionate_current_gold
						}
					}
					save_scope_value_as = {
						name = ransom_saved_gold_value
						value = scope:payer.current_gold_value
					}
				}

				# Must come after we save the payment value, or the payer won't know how much they need to pay.
				trigger_event = char_interaction.0150
			}
			scope:imprisoner = {
				send_interface_message = {
					type = event_generic_neutral
					title = recipient_released_from_prison
					right_icon = scope:prisoner
					left_icon = scope:imprisoner
					ransom_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 = { always = scope:hook }
				scope:actor = {
					use_hook = scope:recipient
				}
			}
		}
	}

	on_decline = {
		# If a player declines this ransom offer, wait 10 years before sending another one (they presumably have reasons for keeping you in prison).
		if = {
			limit = { scope:recipient = { is_ai = no } }
			scope:actor = {
				add_character_flag = {
					flag = character_ransom_refused_by_player
					years = 10
				}
			}
		}		

		# Trigger notification event
		scope:actor = {
			trigger_event = char_interaction.0151
		}
	}
	
	send_option = {
		is_shown = {
			exists = scope:recipient
			scope:recipient = {
				exists = dynasty
				dynasty = {
					has_dynasty_perk = fp1_pillage_legacy_3
				}
			}
		}
		is_valid = {
			scope:actor = {
				gold >= increased_ransom_cost
			}
		}
		flag = extortionate_gold
		localization = "RANSOM_EXTORTIONATE_GOLD_IMPRISONED_PERSPECTIVE_OPTION"
	}

	# If you don't have what they're worth, pay me what you have (extort version)!
	send_option = {
		is_shown = {
			exists = scope:recipient
			scope:recipient = {
				exists = dynasty
				dynasty = {
					has_dynasty_perk = fp1_pillage_legacy_3
				}
			}
			scope:actor = {
				gold < increased_ransom_cost
				gold > 0
			}
		}
		flag = extortionate_current_gold
		localization = "RANSOM_EXTORTIONATE_CURRENT_GOLD_IMPRISONED_PERSPECTIVE_OPTION"
	}
	
	send_option = {
		is_shown = {
			exists = scope:recipient
			scope:recipient = {
				OR = {
					is_lowborn = yes
					NOT = {
						dynasty = {
							has_dynasty_perk = fp1_pillage_legacy_3
						}
					}
				}
			}
		}
		is_valid = {
			scope:actor = {
				gold >= ransom_cost
			}
		}
		flag = gold
		localization = "RANSOM_GOLD_OPTION"
	}

	# If you don't have what they're worth, pay me what you have!
	send_option = {
		is_shown = {
			exists = scope:recipient
			scope:recipient = {
				OR = {
					is_lowborn = yes
					NOT = {
						dynasty = {
							has_dynasty_perk = fp1_pillage_legacy_3
						}
					}
				}
			}
			scope:actor = {
				gold < ransom_cost
				gold > 0
			}
		}
		flag = current_gold
		localization = "RANSOM_CURRENT_GOLD_OPTION"
	}
	
	send_option = {
		is_valid = {
			scope:recipient = {
				can_add_hook = { #To match the hook added through ransom_interaction_effect
					target = scope:actor
					type = favor_hook
				}
			}
		}
		flag = favor
		localization = "RANSOM_FAVOR_OPTION"
	}

	#Use hook
	send_option = {
		is_valid = {
			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
	
	localization_values = {
		EXTORTIONATE_RANSOM_COST = scope:actor.increased_ransom_cost_value
		RANSOM_COST = scope:actor.ransom_cost_value
		CURRENT_GOLD = scope:actor.gold
	}
	
	ai_accept = {
		base = 0
		modifier = {
			add = 50
			OR = {
				scope:gold = yes
				scope:extortionate_gold = yes
			}
			desc = "GOLD_POSITIVE_REASON"
		}
		
		modifier = { # Accept more than half the ransom (unless greedy)
			add = 25
			scope:current_gold = yes
			AND = {
				scope:actor = { gold >= half_ransom_cost_value }
				NOT = { scope:recipient.ai_greed >= medium_positive_ai_value }
			}
			desc = "GOLD_POSITIVE_REASON"
		}
		
		modifier = { # Accept more than half the ransom (unless greedy)
			add = 25
			scope:extortionate_current_gold = yes
			AND = {
				scope:actor = { gold >= half_increased_ransom_cost_value }
				NOT = { scope:recipient.ai_greed >= medium_positive_ai_value }
			}
			desc = "GOLD_POSITIVE_REASON"
		}
		
		modifier = {
			add = 50
			scope:favor = yes
			scope:recipient.top_liege = scope:actor.top_liege
			desc = "FAVOR_POSITIVE_REASON"
		}
		
		modifier = {
			add = -50
			scope:favor = yes
			NOR = {
				scope:gold = yes
				scope:extortionate_gold = yes
			}
			OR = {
				NOT = { scope:recipient.top_liege = scope:actor.top_liege }
				scope:recipient.ai_greed >= medium_positive_ai_value
			}
			desc = "NO_USE_FOR_A_FAVOR_REASON"
		}
		
		modifier = { # For clarification
			add = 0
			OR = {
				scope:current_gold = yes
				scope:extortionate_current_gold = yes
			}
			OR = {
				scope:actor = { gold < half_ransom_cost_value }
				scope:recipient.ai_greed >= medium_positive_ai_value
			}
			desc = "WANTS_MORE_GOLD"
		}
		
		#opinion_modifier = { # A lot more likely if Recipient likes Actor
		#	who = scope:recipient
		#	opinion_target = scope:actor
		#	multiplier = 1.0
		#	desc = AI_OPINION_REASON
		#}
		
		modifier = { # Say no to rivals
			add = -55
			scope:recipient = {
				has_relation_rival = scope:actor
			}
			desc = "ACTOR_RIVAL_TO_ME_REASON"
		}
		modifier = { # Say no to nemesis
			add = -300
			scope:recipient = {
				has_relation_nemesis = scope:actor
			}
			desc = "ACTOR_NEMESIS_TO_ME_REASON"
		}
		
		
		modifier = { # At war
			add = -300
			scope:recipient = {
				is_at_war_with = scope:actor
			}
			desc = "IS_AT_WAR_REASON"
		}

		modifier = {
			scope:hook = yes
			add = 100
			desc = SCHEME_WEAK_HOOK_USED
		}
		
		modifier = { # An Intimidated recipient is significantly more likely to accept an offer from the liege
			add = intimidated_reason_value
			trigger = {
				scope:recipient = {
					target_is_liege_or_above = scope:actor
					has_dread_level_towards = {
						target = scope:actor
						level = 1
					}
				}
			}
			desc = INTIMIDATED_REASON
		}

		modifier = { # A Cowed recipient is significantly more likely to accept an offer from the liege
			add = cowed_reason_value
			trigger = {
				scope:recipient = {
					target_is_liege_or_above = scope:actor
					has_dread_level_towards = {
						target = scope:actor
						level = 2
					}
				}
			}
			desc = COWED_REASON
		}
	}

	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_frequency = 24
	
	ai_targets = {
		ai_recipients = self
	}

	ai_potential = {
		always = yes
	}

	ai_will_do = {
		base = 0 

		modifier = {
			add = 100
			OR = {
				scope:gold = yes
				scope:extortionate_gold = yes
				AND = {
					scope:actor = {
						gold >= 25
					}
					OR = {
						time_in_prison = { years > 1 }
						has_trait = impatient #LET ME OUT! I want out! NOW!
					}
					OR = {
						scope:current_gold = yes
						scope:extortionate_current_gold = yes
					}
				}
			}
		}

		modifier = {
			add = 100
			scope:favor = yes
			scope:recipient = {
				OR = {
					is_vassal_of = scope:actor
					AND = {
						exists = liege
						liege = scope:actor
					}
				}
			}
		}
		
		modifier = {
			add = -100
			scope:favor = yes
			scope:recipient = {
				NOR = {
					is_vassal_of = scope:actor
					AND = {
						exists = liege
						liege = scope:actor
					}
				}
			}
		}

		# Struggle Agenda
		modifier = {
			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_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 give away prisoners for free
			factor = 0
			NOR = {
				scope:favor = yes
				scope:gold = yes
				scope:extortionate_gold = yes
				scope:current_gold = yes
				AND = {
					scope:actor = {
						gold >= 25
					}
					scope:current_gold = yes
				}
			}
		}

		modifier = { # Don't annoy players
			factor = 0
			scope:recipient = { is_ai = no }
			OR = {
				scope:recipient = { is_at_war = yes }
				scope:actor = { has_character_flag = character_ransom_refused_by_player }
			}
		}
	}
}
# DTR: Slaves cannot be simply released
release_from_prison_interaction = {
	interface_priority = 30
	common_interaction = yes
	category = interaction_category_prison
	special_interaction = release_from_prison_interaction
	icon = prison

	desc = release_from_prison_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
				}
				desc = RELEASE_PRISONER_OFFER
			}
			desc = RELEASE_PRISONER_OFFER_CONDITIONAL
		}
	}
	
	is_shown = {
		scope:recipient = {
			is_imprisoned_by = scope:actor
		}
	}

	is_valid_showing_failures_only = {
		scope:recipient = { # DTR
			trigger_if = {
				limit = {
					OR = {
						has_trait_slave = yes
						has_trait = mamluk
					}
				}
				is_courtier_of = scope:actor
			}
		}
		scope:recipient = { is_busy_in_events_localised = yes } #Prisoner
		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 = {
		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 = yes	}
					}
					banish = yes
					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_landed = 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_landed = 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
				}
				
				# Take Vows
				if = {
					limit = {
						scope:become_executioner = yes
					}
					release_as_executioner_effect = yes
				}
				
				# 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
					}
					# Notification to the imprisoner
					scope:actor = {
						send_interface_toast = {
							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
					}
				}
				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_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 = {
										release_from_prison = scope:recipient
										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 }
			# [PoW]
			scope:recipient = { NOT = { is_in_prison_type = prisoner_of_war }}
			# [/PoW]
		}
		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_landed = 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 = {
			# [PoW]
			scope:recipient = { NOT = { is_in_prison_type = prisoner_of_war }}
			# [/PoW]
			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 = {
			# [PoW]
			scope:recipient = { NOT = { is_in_prison_type = prisoner_of_war }}
			# [/PoW]
			trigger_if = {
				limit = {
					scope:actor = { is_ai = yes }
				}
				scope:recipient = {
					OR = {
						AND = {
							is_landed = no
							is_ruler = no
							gold > scope:actor.gold
							gold > scope:actor.medium_gold_value
							scope:actor.ai_greed > low_negative_ai_value
						}
						AND = {
							is_landed = no
							is_ruler = no
							gold > 0
							scope:actor = {
								has_banish_reason = scope:recipient
							}
						}
						AND = {
							is_landed = 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_landed = 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	
				}
			}
			#scope:recipient = {
			#	trigger_if = {
			#		limit = { exists = liege }
			#		liege = scope:actor
			#	}
			#	is_pool_character = no
			#}
		}
	}
	
	send_option = {
		flag = gain_hook
		localization = "RELEASE_GAIN_HOOK"
		# [PoW]
		is_shown = {	
			scope:recipient = { NOT = { is_in_prison_type = prisoner_of_war }}
		}
		# [/PoW]
		is_valid = {
			trigger_if = {
				limit = {
					scope:actor = { is_ai = yes }
				}
				scope:recipient = {
					OR = {
						is_vassal_of = scope:actor
						AND = {
							exists = liege
							liege = scope:actor
						}
					}
				}
			}
			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 = {
			# [PoW]
			scope:recipient = { NOT = { is_in_prison_type = prisoner_of_war } }
			# [/PoW]
			scope:actor.faith = {
				has_doctrine_parameter = take_vows_active
			}
		}
		is_valid = {
			trigger_if = {
				limit = {
					scope:actor = { is_ai = yes }
				}
				scope:recipient = {
					is_close_or_extended_family_of = scope:actor
					NOT = {
						is_heir_of = scope:actor
					}
				}
				scope:actor = {
					opinion = {
						target = scope:recipient
						value <= medium_negative_opinion
					}
				}
			}
			scope:recipient = {
				OR = {
					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 = {
			# [PoW]
			scope:recipient = { NOT = { is_in_prison_type = prisoner_of_war }}
			# [/PoW]
			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_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 = -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 = { # 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_landed = 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_landed = 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_landed = 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_landed = 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
				}
			}
		}

		evaluate_action_increasing_house_unity = {
			VALUE = 100
		}
	}
}
