﻿######################
# Prisoner Interactions
######################
# Sell into slavery
sell_prisoner_interaction = {
	icon = icon_slave

	interface_priority = 50
	common_interaction = yes
	use_diplomatic_range = no
	category = interaction_category_prison

	desc = sell_prisoner_interaction_desc

	redirect = { # DTR-PoW: making liege the secondary_recipient
		scope:recipient = {
			if = {
				limit = {
					exists = liege
				}
				liege = { save_scope_as = secondary_recipient }
			}
		}
	}

	is_shown = {
		scope:recipient = {
			#exists = liege
			is_imprisoned_by = scope:actor
			#has_trait_slave = no
			#NOT = { has_trait = mamluk }
		}
		NOT = {
			scope:actor = scope:recipient
		}
	}

	is_valid_showing_failures_only = {
		scope:actor = {
			is_at_war = no # wait until wars are resolved
			NOT = { top_liege = scope:recipient.top_liege }
			NOT = { liege = scope:recipient }
			# Tribal pagans unreformed can sell any time they want...
			trigger_if = {
				limit = {
					faith = { has_doctrine_parameter = unreformed }
					government_has_flag = government_is_tribal
				}
				always = yes
			}
			# But others like feudal/clan and reformed faiths need...
			trigger_else = { 
				OR = {
					can_be_enslaved_by_faith = yes # 1.a. religious hatred
					AND = { 
						can_enslave_by_culture_trigger = yes # 1.b. culturally appropriate
						NOT = { scope:actor.faith = scope:recipient.faith }
						NOT = { scope:actor.culture = scope:recipient.culture }
					}
				}
				trigger_if = { # I. Slaves can be resold at any time
					limit = {
						scope:recipient = {
							OR = {
								has_trait_slave = yes
								has_trait = mamluk 
							}
						}
					}
					always = yes
				}
				trigger_else = { # II. Non-Slaves:
					trigger_if = { # A. Special case : High Intrigue
						limit = { # A.1. Appropriate traits
							OR = {
								trigger_if = {
									limit = {
										has_trait = torturer
									}
									OR = {
										has_trait = schemer
										faith = { # If religion is considered evil
											faith_hostility_level = {
												target = scope:recipient.faith
												value >= faith_evil_level
											}
										}
									}
								}
								has_trait_rank = { trait = lifestyle_slavemaster rank > 1 }
							}
							OR = { # A.2. Appropriate bad relation 
								has_relation_rival = scope:recipient
								has_relation_nemesis = scope:recipient
								scope:recipient = {
									OR = {
										any_close_family_member = {
											is_ruler = yes
											scope:actor = { 
												OR = {
													has_relation_rival = prev
													has_relation_nemesis = prev 
												}
											}
										}
										any_consort = {
											is_ruler = yes
											scope:actor = { 
												OR = {
													has_relation_rival = prev
													has_relation_nemesis = prev 
												}
											}
										}
									}
								}
								AND = {
									exists = scope:recipient.house
									exists = house
									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
									}
								}
							}
						}
						always = yes
					}
					trigger_else_if = { # B. General case
						limit = {
							scope:recipient = {
								trigger_if = { # B.2. Rulers only from tribals
									limit = { 
										is_ruler = yes 
										trigger_if = { # only relevant if we are close (diplomatic range is not usable anyways)
											limit = { exists = capital_county }
											scope:actor = {
												realm_to_title_distance_squared = {
													title = scope:recipient.capital_county
													value < enslave_distance_value
												}
											}
										}
									}
									NOT = { government_has_flag = government_is_tribal } # tribals are fair game for everyone
								}
								# B.2. Others (including rulers with non-tribal family rulers)
								trigger_else = {
									OR = {
										AND = {
											exists = primary_spouse
											primary_spouse = {
												this = {
													is_ruler = yes 
													NOT = { government_has_flag = government_is_tribal } # tribals are fair game for everyone
													trigger_if = { # only relevant if we are close (diplomatic range is not usable anyways)
														limit = { exists = capital_county }
														scope:actor = {
															realm_to_title_distance_squared = {
																title = prev.capital_county
																value < enslave_distance_value
															}
														}
													}
												}
											}
										}
										trigger_if = { # B.2.1. If they are lowborn, they can be easily sold
											limit = { is_lowborn = yes }
											OR = {
												any_close_family_member = {
													is_ruler = yes 
													NOT = { government_has_flag = government_is_tribal } # tribals are fair game for everyone
													trigger_if = { # only relevant if we are close (diplomatic range is not usable anyways)
														limit = { exists = capital_county }
														scope:actor = {
															realm_to_title_distance_squared = {
																title = prev.capital_county
																value < enslave_distance_value
															}
														}
													}
												}
												any_consort = {
													is_ruler = yes 
													NOT = { government_has_flag = government_is_tribal } # tribals are fair game for everyone
													trigger_if = { # only relevant if we are close (diplomatic range is not usable anyways)
														limit = { exists = capital_county }
														scope:actor = {
															realm_to_title_distance_squared = {
																title = prev.capital_county
																value < enslave_distance_value
															}
														}
													}
												}
											}
										}
										trigger_else = { # B.2.2. If they have a house, check that nobody is interested in them
											OR = {
												any_close_or_extended_family_member = {
													is_ruler = yes 
													NOT = { government_has_flag = government_is_tribal } # tribals are fair game for everyone
													trigger_if = { # only relevant if we are close (diplomatic range is not usable anyways)
														limit = { exists = capital_county }
														scope:actor = {
															realm_to_title_distance_squared = {
																title = prev.capital_county
																value < enslave_distance_value
															}
														}
													}
												}
												any_consort = {
													is_ruler = yes 
													NOT = { government_has_flag = government_is_tribal } # tribals are fair game for everyone
													trigger_if = { # only relevant if we are close (diplomatic range is not usable anyways)
														limit = { exists = capital_county }
														scope:actor = {
															realm_to_title_distance_squared = {
																title = prev.capital_county
																value < enslave_distance_value
															}
														}
													}
												}
											}
										}
									}
								}
								NOT = { has_character_flag = character_ransom_refused_by_player } # Ransom refused, fair game
								## Reciprocal Treaty conditions:
								# exists = scope:recipient.top_liege
								# top_liege = { # A. In close contact with each other
								# 	OR = {
								# 		any_neighboring_and_across_water_top_liege_realm_owner = {
								# 			this = scope:recipient.top_liege
								# 		}
								# 		AND = {
								# 			exists = capital_province
								# 			exists = scope:recipient.top_liege.capital_province
								# 			capital_province.county.duchy.kingdom.empire = scope:recipient.top_liege.capital_province.county.duchy.kingdom.empire
								# 		}
								# 		# AND = { # B. Involved in the same struggle 
								# 		# 	any_character_struggle = {
								# 		# 		involvement = involved
								# 		# 		is_struggle_type = iberian_struggle
								# 		# 	}
								# 		# 	scope:recipient.top_liege = {
								# 		# 		any_character_struggle = {
								# 		# 			involvement = involved
								# 		# 			is_struggle_type = iberian_struggle
								# 		# 		}
								# 		# 	}
								# 		# }
								# 	}
								# }
							}
						}
						# Ransom won't be accepted
						NOT = {
							is_character_interaction_potentially_accepted = {
								recipient = scope:recipient
								interaction = ransom_interaction
							}
						}
						# Unless potential ransomer is considering another offer
						trigger_if = {
							limit = {
								scope:recipient = { 
									is_ruler = no 
								}
							}
							is_character_interaction_shown = {
								recipient = scope:recipient.liege
								interaction = ransom_interaction
							}
						}
					}
					trigger_else = { # C. All others
						always = yes
					}
				}
			}
		}

		scope:recipient = { 
			is_busy_in_events_localised = yes
			NOT = { is_courtier_of = scope:actor }
			NOT = { is_pool_guest_of = scope:actor }
			NOT = { is_hostage_of = scope:actor }
			NOT = { is_hostage_from = scope:actor }
			has_disqualifying_disease_trigger = no
			custom_description = {
				text = "currently_being_tortured"
				NOT = { has_character_flag = is_being_tortured }
			}
			trigger_if = {
				limit = {
					scope:actor = {
						faith.religion = religion:islam_religion
					}
				}
				NOT = { has_trait = umm_walad }
			}
		}

		# [PoW]
		PoW_can_be_ransomed = { # DTR-PoW: liege is secondary_recipient
			PRISONER = scope:recipient
			JAILOR = scope:actor
			PAYER = scope:secondary_recipient
		}
		# [/PoW]
	}
	
	on_accept = {
		if = {
			limit = { # To prevent simultaneous release-shenanigans
				scope:recipient = { is_imprisoned_by = scope:actor } 
			} 
			if = { # For players
				limit = {
					scope:recipient = { is_ai = no }
				}
				scope:recipient = { trigger_event = slavery_events.0002 }
				show_as_tooltip = {
					sell_effect = { 
						ACTOR = scope:actor 
						RECIPIENT = scope:recipient
					}
				}
			}
			else = {
				# If it is already another's slave, seize them first
				if = {
					limit = {
						scope:recipient = { has_trait_slave = yes }
					}
					steal_slave_effect = yes
				}
				# Trying to show players dramatic events
				if = {
					limit = {
						scope:recipient = {
							any_consort = { is_ai = no }
						}
					}
					scope:recipient = { 
						random_consort = { ## Only one event, to avoid case of multiple spouses being players (duplicate events)
							limit = { is_ai = no }
							trigger_event = slavery_events.0012
						}
					}
					show_as_tooltip = {
						sell_effect = { 
							ACTOR = scope:actor 
							RECIPIENT = scope:recipient
						}
					}
				}
				else_if = {
					limit = {
						scope:recipient = {
							any_parent = { is_ai = no }
						}
					}
					scope:recipient = {
						random_parent = { ## Only one event, to avoid case of multiple spouses being players (duplicate events)
							limit = { is_ai = no }
							trigger_event = slavery_events.0012
						}
					}
					show_as_tooltip = {
						sell_effect = { 
							ACTOR = scope:actor 
							RECIPIENT = scope:recipient
						}
					}
				}
				else = {
					sell_effect = { 
						ACTOR = scope:actor 
						RECIPIENT = scope:recipient
					}
				}
			}
		}
	}

	auto_accept = yes

	# AI
	ai_targets = {
		ai_recipients = prisoners
	}

	ai_frequency = 6 # Increasing frequency is useful to avoid crowded markets

	ai_potential = {
		always = yes
	}

	ai_will_do = {
		base = 50 # Faith hostility is already high, so we tend to sell
		
		modifier = {
			add = -100 # if they potentially have the ability to pay, we are less likely to enslave
			scope:recipient = {
				OR = {
					is_landed = yes
					any_parent = {
						is_landed = yes
					}
				}
			}
		}

		# Gold
		modifier = {
			add = 100
			AND = {
				scope:actor = {
					gold < medium_gold_value # can do with a little more gold
				}
				scope:recipient = {
					time_in_prison = { months > 3 }
				}
			}
		}
		modifier = {
			add = 50
			scope:actor = {
				gold < 0 # can do with a little more gold
			}
		}
		modifier = {
			add = 50
			scope:recipient = {
				is_lowborn = yes
			}
		}

		# corrected by traits that make gain/lose stress
		modifier = {
			add = -15
			has_trait = just
		}
		modifier = {
			add = -50
			has_trait = compassionate
		}
		modifier = {
			add = -50
			has_trait = forgiving
		}
		modifier = {
			add = 50
			has_trait = zealous
		}
		modifier = {
			add = 75
			has_trait = sadistic
		}
		modifier = {
			add = 100
			has_trait = vengeful
		}
		modifier = {
			add = 50
			has_trait = greedy
		}
		modifier = {
			add = 25
			has_trait = schemer
		}
		modifier = {
			add = 50
			has_trait = torturer
		}
		# Slavers are more prone to enslave initially, then more likely to sell
		modifier = { # slaver 1
			add = 50
			has_trait_rank = { 
				trait = lifestyle_slavemaster 
				rank > 0 
			}
		}
		modifier = { # slaver 2
			add = 50
			has_trait_rank = { 
				trait = lifestyle_slavemaster 
				rank > 1
			}
		}
		modifier = { # slaver 3
			add = 50
			has_trait_rank = { 
				trait = lifestyle_slavemaster 
				rank > 2
			}
		}
		
		modifier = {
			add = ai_greed
			ai_greed > 0
		}

		modifier = { # religious reason
			add = 75
			ai_zeal > 0
			faith = {
				faith_hostility_level = {
					target = scope:recipient.faith
					value >= faith_evil_level
				}
			}
		}
		# Rivals
		modifier = {
			add = 100
			has_relation_rival = scope:recipient
		}
		modifier = {
			add = 50
			scope:recipient = {
				any_close_family_member = {
					has_relation_rival = scope:actor
				}
			}
		}
		# Nemesis
		modifier = {
			add = 300
			has_relation_nemesis = scope:recipient
		}
		modifier = {
			add = 150
			scope:recipient = {
				any_close_family_member = {
					has_relation_nemesis = scope:actor
				}
			}
		}
	}
}

# Enslave directly (stricter requirements)
make_slave_interaction = {
	icon = icon_enslave

	interface_priority = 50
	common_interaction = yes
	use_diplomatic_range = no
	category = interaction_category_prison

	desc = make_slave_interaction_desc

	redirect = { # DTR-PoW: making liege the secondary_recipient
		scope:recipient = {
			if = {
				limit = {
					exists = liege
				}
				liege = { save_scope_as = secondary_recipient }
			}
		}
	}

	is_shown = {
		scope:actor = {
			OR = {
				government_has_flag = government_is_tribal
				has_trait_rank = { trait = lifestyle_slavemaster rank > 0 }
				has_trait = torturer # special case
				AND = { # The court "slavemaster"
					can_employ_court_position_type = chief_eunuch_court_position
					any_court_position_holder = {
						type = chief_eunuch_court_position
					}
				}
			}
		}
		scope:recipient = {
			#exists = liege
			is_imprisoned_by = scope:actor
			has_trait_slave = no
			NOT = { has_trait = mamluk }
		}
		NOT = {
			scope:actor = scope:recipient
		}
	}

	is_valid_showing_failures_only = {
		scope:actor = {
			is_at_war = no # wait until wars are resolved
			NOT = { top_liege = scope:recipient.top_liege }
			NOT = { liege = scope:recipient }
			# Test this first for performance reasons:
			can_employ_court_position_type = slave_court_position
			any_court_position_holder = {
				type = slave_court_position
				count < 4
			}
			trigger_if = { # I. "Super-Special" case : Tribal Pagan + High Intrigue
				limit = { # 
					government_has_flag = government_is_tribal
					faith = { has_doctrine_parameter = unreformed }
					OR = {
						trigger_if = {
							limit = {
								has_trait = torturer
							}
							OR = {
								has_trait = schemer
								faith = { # If religion is considered evil
									faith_hostility_level = {
										target = scope:recipient.faith
										value >= faith_evil_level
									}
								}
							}
						}
						has_trait_rank = { trait = lifestyle_slavemaster rank > 0 }
					}
				}
				always = yes
			}
			trigger_else = { # II. All other cases
				# Tribal pagans unreformed can enslave any time they want...
				trigger_if = { 
					limit = {
						faith = {
							NOT = { has_doctrine_parameter = unreformed }
						}
						NOT = { government_has_flag = government_is_tribal }
					}
					OR = {
						can_be_enslaved_by_faith = yes # 1.a. religious hatred
						AND = { 
							NOT = { scope:actor.faith = scope:recipient.faith }
							NOT = { scope:actor.culture = scope:recipient.culture }
							can_enslave_by_culture_trigger = yes # 1.b. culturally appropriate
						}
					}
				}
				# Stricter requirements than for selling (it includes tribals)
				trigger_if = { # A. Special case : High Intrigue
					limit = { # A.1. Appropriate traits
						OR = {
							trigger_if = {
								limit = {
									has_trait = torturer
								}
								OR = {
									has_trait = schemer
									faith = { # If religion is considered evil
										faith_hostility_level = {
											target = scope:recipient.faith
											value >= faith_evil_level
										}
									}
								}
							}
							has_trait_rank = { trait = lifestyle_slavemaster rank > 1 }
						}
						OR = { # A.2. Appropriate bad relation 
							has_relation_rival = scope:recipient
							has_relation_nemesis = scope:recipient
							scope:recipient = {
								OR = {
									any_close_family_member = {
										is_ruler = yes
										scope:actor = { 
											OR = {
												has_relation_rival = prev
												has_relation_nemesis = prev 
											}
										}
									}
									any_consort = {
										is_ruler = yes
										scope:actor = { 
											OR = {
												has_relation_rival = prev
												has_relation_nemesis = prev 
											}
										}
									}
								}
							}
							AND = {
								exists = scope:recipient.house
								exists = house
								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
								}
							}
						}
						trigger_if = {
							limit = { # If it's a ruler, 
								scope:recipient = { is_ruler = yes }
								trigger_if = {
									limit = { # only relevant if we are close (diplomatic range is not usable)
										exists = scope:recipient.capital_county
									}
									realm_to_title_distance_squared = {
										title = scope:recipient.capital_county
										value < enslave_distance_value
									}
								}
							}
							trigger_if = { # Non-Tribals can enslave tribals
								limit = { 
									NOT = { government_has_flag = government_is_tribal }
								}
								scope:recipient = { government_has_flag = government_is_tribal }
							}
							trigger_else = { always = yes } # Tribals can enslave anyone
						}
					}
					always = yes
				}
				trigger_else_if = { # C. General case (non-rulers)
					limit = {
                        scope:recipient = {
                            trigger_if = { # B.2. Rulers only from tribals
                                limit = {
                                    is_ruler = yes
									trigger_if = {
										limit = { # only relevant if we are close (diplomatic range is not usable)
											exists = capital_county
										}
										scope:actor = {
											realm_to_title_distance_squared = {
												title = scope:recipient.capital_county
												value < enslave_distance_value
											}
										}
									}
                                }
								trigger_if = { # Non-Tribals can enslave tribals
									limit = { 
										scope:actor = {
											NOT = { government_has_flag = government_is_tribal }
										}
									}
									NOT = { government_has_flag = government_is_tribal }
								}
								trigger_else = { always = no } # Tribals can enslave anyone
                            }
                            # B.2. Others (including rulers with non-tribal family rulers)
                            trigger_else = {
                                OR = {
                                    AND = {
                                        exists = primary_spouse
                                        primary_spouse = {
                                            this = {
                                                is_ruler = yes 
                                                trigger_if = { # only relevant if we are close (diplomatic range is not usable anyways)
                                                    limit = { exists = capital_county }
                                                    scope:actor = {
                                                        realm_to_title_distance_squared = {
                                                            title = prev.capital_county
                                                            value < enslave_distance_value
                                                        }
                                                    }
                                                }
                                                trigger_if = { # Non-Tribals can enslave tribals
                                                    limit = { 
                                                        scope:actor = {
                                                            NOT = { government_has_flag = government_is_tribal }
                                                        }
                                                    }
                                                    NOT = { government_has_flag = government_is_tribal }
                                                }
                                                trigger_else = { always = no } # Tribals can enslave anyone
                                            }
                                        }
                                    }
                                    trigger_if = { # B.2.1. If they are lowborn, they can be easily sold
                                        limit = { is_lowborn = yes }
                                        any_close_family_member = {
											is_ruler = yes 
											trigger_if = { # only relevant if we are close (diplomatic range is not usable anyways)
												limit = { exists = capital_county }
												scope:actor = {
													realm_to_title_distance_squared = {
														title = prev.capital_county
														value < enslave_distance_value
													}
												}
											}
											trigger_if = { # Non-Tribals can enslave tribals
												limit = { 
													scope:actor = {
														NOT = { government_has_flag = government_is_tribal }
													}
												}
												NOT = { government_has_flag = government_is_tribal }
											}
											trigger_else = { always = no } # Tribals can enslave anyone
										}
										any_consort = {
											is_ruler = yes 
											trigger_if = { # only relevant if we are close (diplomatic range is not usable anyways)
												limit = { exists = capital_county }
												scope:actor = {
													realm_to_title_distance_squared = {
														title = prev.capital_county
														value < enslave_distance_value
													}
												}
											}
											trigger_if = { # Non-Tribals can enslave tribals
												limit = { 
													scope:actor = {
														NOT = { government_has_flag = government_is_tribal }
													}
												}
												NOT = { government_has_flag = government_is_tribal }
											}
											trigger_else = { always = no } # Tribals can enslave anyone
										}
                                    }
                                    trigger_else = { # B.2.2. If they have a house, check that nobody is interested in them
										OR = {
											any_close_or_extended_family_member = {
                                                is_ruler = yes 
                                                trigger_if = { # only relevant if we are close (diplomatic range is not usable anyways)
                                                    limit = { exists = capital_county }
                                                    scope:actor = {
                                                        realm_to_title_distance_squared = {
                                                            title = prev.capital_county
                                                            value < enslave_distance_value
                                                        }
                                                    }
                                                }
                                                trigger_if = { # Non-Tribals can enslave tribals
                                                    limit = { 
                                                        scope:actor = {
                                                            NOT = { government_has_flag = government_is_tribal }
                                                        }
                                                    }
                                                    NOT = { government_has_flag = government_is_tribal }
                                                }
                                                trigger_else = { always = no } # Tribals can enslave anyone
											}
											any_consort = {
												is_ruler = yes 
												trigger_if = { # only relevant if we are close (diplomatic range is not usable anyways)
													limit = { exists = capital_county }
													scope:actor = {
														realm_to_title_distance_squared = {
															title = prev.capital_county
															value < enslave_distance_value
														}
													}
												}
												trigger_if = { # Non-Tribals can enslave tribals
													limit = { 
														scope:actor = {
															NOT = { government_has_flag = government_is_tribal }
														}
													}
													NOT = { government_has_flag = government_is_tribal }
												}
												trigger_else = { always = no } # Tribals can enslave anyone
											}
										}
                                    }
                                }
                            }
                            NOT = { has_character_flag = character_ransom_refused_by_player } # Ransom refused, fair game
                        }
					}
					# Ransom won't be accepted
					NOT = {
						is_character_interaction_potentially_accepted = {
							recipient = scope:recipient
							interaction = ransom_interaction
						}
					}
					# Unless potential ransomer is considering another offer
					is_character_interaction_shown = {
						recipient = scope:recipient.liege
						interaction = ransom_interaction
					}
				}
				trigger_else = { # D. All others (including tribals)
					always = yes
				}
			}
		}

		scope:recipient = {
			is_adult = yes 
			is_busy_in_events_localised = yes
			NOT = { is_courtier_of = scope:actor }
			NOT = { is_pool_guest_of = scope:actor }
			NOT = { is_hostage_of = scope:actor }
			NOT = { is_hostage_from = scope:actor }
			# has_disqualifying_disease_trigger = no
			custom_description = {
				text = "currently_being_tortured"
				NOT = { has_character_flag = is_being_tortured }
			}
		}
		
		# [PoW]
		PoW_can_be_ransomed = { # DTR-PoW: liege is secondary_recipient
			PRISONER = scope:recipient
			JAILOR = scope:actor
			PAYER = scope:secondary_recipient
		}
		# [/PoW]
	}
	
	on_accept = {
		# For events
		save_scope_value_as = { 
			name = make_slave
			value = yes
		}
		# for loc. (message)
		if = {
			limit = {
				exists = scope:recipient.liege
			}
			scope:recipient.liege = {
				save_scope_as = recipient_liege
			}
		}
		else_if = {
			limit = {
				exists = scope:recipient.host
			}
			scope:recipient.host = {
				save_scope_as = recipient_liege
			}
		}
		else = {
			scope:actor = {
				save_scope_as = recipient_liege
			}
		}

		if = {
			limit = { # To prevent simultaneous release-shenanigans
				scope:recipient = { is_imprisoned_by = scope:actor } 
			} 
			if = { # For players
				limit = {
					scope:recipient = { is_ai = no }
				}
				scope:recipient = { trigger_event = slavery_events.0002 }
				show_as_tooltip = {
					make_slave_effect = {
						ACTOR = scope:actor 
						RECIPIENT = scope:recipient
						LIEGE = scope:recipient_liege
					}
				}
			}
			else = {
				# Trying to show players dramatic events
				if = {
					limit = {
						scope:recipient = {
							any_consort = { is_ai = no }
						}
					}
					scope:recipient = {
						random_consort = { ## Only one event, to avoid case of multiple spouses being players (duplicate events)
							limit = { is_ai = no }
							trigger_event = slavery_events.0012
						}
					}
					show_as_tooltip = {
						make_slave_effect = {
							ACTOR = scope:actor 
							RECIPIENT = scope:recipient
							LIEGE = scope:recipient_liege
						}
					}
				}
				else_if = {
					limit = {
						scope:recipient = {
							any_parent = { is_ai = no }
						}
					}
					scope:recipient = { 
						random_parent = { ## Only one event, to avoid case of multiple spouses being players (duplicate events)
							limit = { is_ai = no }
							trigger_event = slavery_events.0012
						}
					}
					show_as_tooltip = {
						make_slave_effect = {
							ACTOR = scope:actor 
							RECIPIENT = scope:recipient
							LIEGE = scope:recipient_liege
						}
					}
				}
				else = {
					make_slave_effect = {
						ACTOR = scope:actor 
						RECIPIENT = scope:recipient
						LIEGE = scope:recipient_liege
					}
				}
			}
		}
	}

	auto_accept = yes

	# AI
	ai_targets = {
		ai_recipients = prisoners
	}

	ai_frequency = 6 # Increasing frequency is useful to avoid crowded markets

	ai_potential = {
		always = yes
	}

	ai_will_do = {
		base = 50 # Faith hostility is already high, so we tend to enslave
		
		modifier = {
			add = -100 # if they potentially have the ability to pay, we are less likely to enslave
			scope:recipient = {
				OR = {
					is_landed = yes
					any_parent = {
						is_landed = yes
					}
				}
			}
		}

		# Gold
		modifier = { # can do with a little more gold
			add = -50
			AND = {
				scope:actor = {
					gold < medium_gold_value
				}
				scope:recipient = {
					time_in_prison = { months > 3 }
				}
			}
		}
		modifier = { # on negatives, prefer to sell
			add = -100
			scope:actor = {
				gold < 0 
			}
		}
		modifier = {
			add = 50
			scope:recipient = {
				is_lowborn = yes
			}
		}

		# Corrected by needs
		modifier = { # Muslims and concubinists are the main buyers...
			add = 150
			exists = scope:recipient
			scope:actor = {
				allowed_concubines = yes
				allowed_more_concubines = yes
				number_of_concubines < number_of_desired_concubines
				scope:recipient = { 
					can_become_concubine_of_character_valid_trigger = { CHARACTER = scope:actor }
					can_have_children_with = { CHARACTER = scope:actor } # Only if you can have kids since this is about fertility and genetic traits
					fertility > 0.2
					age < 36
				}
			}
		}

		ai_value_modifier = {
			ai_vengefulness = 1
			ai_zeal = 0.5
			ai_compassion = -0.5
		}

		# corrected by traits that make gain/lose stress
		modifier = {
			add = -15
			has_trait = just
		}
		modifier = {
			add = -50
			has_trait = compassionate
		}
		modifier = {
			add = -50
			has_trait = forgiving
		}
		modifier = {
			add = 50
			has_trait = zealous
		}
		modifier = {
			add = 75
			has_trait = sadistic
		}
		modifier = {
			add = 50
			has_trait = vengeful
		}
		modifier = {
			add = 25
			has_trait = lustful
			is_attracted_to_gender_of = scope:recipient
		}
		modifier = { # prefer to sell
			add = -50
			has_trait = greedy
		}
		modifier = { # slaver
			add = 60
			has_trait_rank = { 
				trait = lifestyle_slavemaster 
				rank > 0 
			}
		}
		modifier = { # slaver 2
			add = 40
			has_trait_rank = { 
				trait = lifestyle_slavemaster 
				rank > 1
			}
		}
		modifier = { # slaver
			add = 25
			has_trait_rank = { 
				trait = lifestyle_slavemaster 
				rank > 2
			}
		}
		modifier = { # natural slaver
			add = 75
			has_trait = torturer
		}
		modifier = {
			add = 50
			has_trait = schemer
		}
		
		modifier = { # religious reason
			add = 100
			ai_zeal > 0
			faith = {
				faith_hostility_level = {
					target = scope:recipient.faith
					value >= faith_evil_level
				}
			}
		}
		
		# Rivals
		modifier = {
			add = 100
			has_relation_rival = scope:recipient
		}
		modifier = {
			add = 50
			scope:recipient = {
				any_close_family_member = {
					has_relation_rival = scope:actor
				}
			}
		}
		# Nemesis
		modifier = {
			add = 300
			has_relation_nemesis = scope:recipient
		}
		modifier = {
			add = 150
			scope:recipient = {
				any_close_family_member = {
					has_relation_nemesis = scope:actor
				}
			}
		}
	}
}

######################
# Slave Interactions
######################

# Seize slave from prison
appoint_slave_interaction = {
	icon = icon_slave
	category = interaction_category_prison

	interface_priority = 50
	common_interaction = yes
	use_diplomatic_range = no

	desc = appoint_slave_interaction_desc

	redirect = { # DTR-PoW: making liege the secondary_recipient
		scope:recipient = {
			if = {
				limit = {
					exists = liege
				}
				liege = { save_scope_as = secondary_recipient }
			}
		}
	}

	is_shown = {
		scope:recipient = {
			#exists = liege
			OR = {
				has_trait_slave = yes
				has_trait = mamluk
			}
			OR = {
				is_imprisoned_by = scope:actor
				is_foreign_court_or_pool_guest_of = scope:actor # shouldn't be wandering around
			}
			NOT = { is_courtier_of = scope:actor }
		}
	}

	is_valid_showing_failures_only = {
		scope:recipient = {
			OR = {
				has_trait_slave = yes 
				has_trait = mamluk
			}
			is_busy_in_events_localised = yes 
			trigger_if = {
				limit = {
					scope:actor = {
						faith.religion = religion:islam_religion
					}
				}
				NOT = { has_trait = umm_walad }
			}
			custom_description = {
				text = "currently_being_tortured"
				NOT = { has_character_flag = is_being_tortured }
			}
			trigger_if = {
				limit = { is_close_family_of = scope:actor }
				always = yes
			}
			trigger_else = {
				is_adult = yes # minor relatives can be bought back
				# has_disqualifying_disease_trigger = no
				trigger_if = {
					limit = {
						exists = liege # should always exist, but we can't fully avoid wandering slaves...
					}
					liege = { 
						NOT = { has_strong_hook = scope:actor } 
					}
				}
				trigger_if = {
					limit = { has_trait = mamluk }
					scope:actor = {
						can_employ_court_position_type = mamluk_court_position # they must be able to appoint Mamluks
						OR = {
							any_court_position_holder = {
								type = mamluk_court_position
								count < 2
							}
							AND = {
								can_employ_court_position_type = slave_court_position
								any_court_position_holder = {
									type = slave_court_position
									count < 4
								}
							}
						}
					}
				}
				trigger_else_if = {
					limit = { has_trait = eunuch }
					scope:actor = {
						OR = {
							AND = {
								can_employ_court_position_type = chief_eunuch_court_position
								any_court_position_holder = {
									type = chief_eunuch_court_position
									count = 0
								}
							}
							AND = {
								can_employ_court_position_type = slave_court_position
								any_court_position_holder = {
									type = slave_court_position
									count < 4
								}
							}
						}
					}
				}
				trigger_else = {
					scope:actor = {
						can_employ_court_position_type = slave_court_position
						any_court_position_holder = {
							type = slave_court_position
							count < 4
						}
					}
					trigger_if = {
						limit = { # For enslavers by culture, the slave should be at least of different culture
							scope:actor = { 
								can_enslave_by_culture = yes
							}
						}
						NAND = { # not same faith + same culture
							faith = scope:actor.faith
							culture = scope:actor.culture
						}
					}
					trigger_else = { # We don't care about faith hostility here, since they are already chattel
						NOT = { faith = scope:actor.faith }
					}
				}
			}
		}

		# [PoW]
		PoW_can_be_ransomed = { # DTR-PoW: liege is secondary_recipient
			PRISONER = scope:recipient
			JAILOR = scope:actor
			PAYER = scope:secondary_recipient
		}
		# [/PoW]
	}

	auto_accept = yes

	# AI
	ai_targets = {
		ai_recipients = prisoners
		ai_recipients = courtiers
		ai_recipients = guests
	}

	ai_frequency = 12 # do not leave slaves wandering around too much

	ai_potential = {
		always = yes
	}

	ai_will_do = {
		base = 0
		
		modifier = {
			add = 500
			scope:recipient = {
				is_close_family_of = scope:actor
			}
		}
		# AI won't take slaves they have to manumit
		modifier = {
			add = 100
			faith = {
				faith_hostility_level = {
					target = scope:recipient.faith
					value >= religious_cb_enabled_hostility_level
				}
			}
		}
		modifier = {
			add = 100
			OR = {
				has_trait = greedy
				has_trait = vengeful
				has_trait = wrathful
			}
		}
		modifier = {
			add = 200
			short_term_gold < 200
		}

		modifier = {
			add = 200
			exists = scope:recipient.liege
			OR = {
				has_relation_nemesis = scope:recipient.liege
				has_relation_rival = scope:recipient.liege
				has_relation_potential_rival = scope:recipient.liege
			}
		}

		modifier = {
			add = -200
			exists = scope:recipient.liege
			scope:recipient = { # shouldn't be wandering around
				is_foreign_court_or_pool_guest = no
			}
			OR = {
				has_relation_best_friend = scope:recipient.liege
				has_relation_friend = scope:recipient.liege
				has_relation_potential_friend = scope:recipient.liege
				has_relation_soulmate = scope:recipient.liege
				has_relation_lover = scope:recipient.liege
				has_relation_potential_lover = scope:recipient.liege
			}
		}
	}
		
	on_accept = {

		steal_slave_effect = yes

		scope:actor = {
			if = {
				limit = {
					scope:slave = {
						is_close_family_of = scope:actor
					}
					OR = {
						# Let player decide if they want to free a relative or not, so long as they are adults
						is_ai = yes #
						scope:slave = { is_adult = no }
						# we don't care about eunuchs or mamluks here
						NOT = { can_employ_court_position_type = slave_court_position }
						any_court_position_holder = {
							type = slave_court_position
							count > 3
						}
					}
				}
				save_scope_as = owner
				scope:slave = {
					free_slave_effect = yes # needs scope:owner and scope:slave
				}
			}
			else = {
				save_scope_as = new_holder
				slave_appropriate_court_position = yes
				
				# Show us what we did
				hidden_effect = {
					send_interface_message = {
						type = event_prison_good
						title = msg_slave_seized
						custom_tooltip = msg_slave_seized_desc
						right_icon = scope:recipient
					}
				}
			}

		}

	}
}
