﻿namespace = MTXXX_raiding

scripted_trigger MTXXX_held_prisoner = {
	is_alive = yes
	is_imprisoned = yes
	is_imprisoned_by = scope:barony.holder
}

scripted_trigger MTXXX_raiding_root_interested_in_concubine_trigger = {
	this ?= { is_alive = yes }
	or = {
		this = root
		any_close_or_extended_family_member = { this = root }
		is_great_grandparent_of = root
		is_great_grandchild_of = root
		dynasty ?= root.dynasty
	}
}

scripted_trigger MTXXX_raiding_former_consort_interested_in_concubine_trigger = {
	this ?= { is_alive = yes }
	or = {
		prev = { MTXXX_raiding_root_interested_in_concubine_trigger = yes }
		MTXXX_raiding_root_interested_in_concubine_trigger = yes
	}
	or = {
		has_opinion_modifier = { target = prev.concubinist modifier = forced_spouse_concubine_marriage_opinion }
		has_opinion_modifier = { target = prev.concubinist modifier = stole_concubine_opinion }
	}
}

MTXXX_raiding.0010 = {
	hidden = yes

	trigger = {
		root = {
			highest_held_title_tier > tier_county
		}
		root.culture = { has_cultural_tradition = tradition_raiders_of_the_lost }
		scope:barony.holder.capital_barony = scope:barony
	}

	immediate = {
		scope:raider = { save_scope_as = occupant }
		scope:barony.holder = { save_scope_as = holder}
		scope:barony = { save_scope_as = barony_prison }

		dynasty = {
			every_dynasty_member = {
				limit = { MTXXX_held_prisoner = yes }
				add_to_list = MTXXX_released_targets_list
			}
		}

		every_consort = {
			limit = {
				MTXXX_held_prisoner = yes
				not = { is_in_list = MTXXX_released_targets_list }
			}
			add_to_list = MTXXX_released_targets_list
		}

		every_close_or_extended_family_member = {
			limit = {
				MTXXX_held_prisoner = yes
				not = { is_in_list = MTXXX_released_targets_list }
			}
			add_to_list = MTXXX_released_targets_list
		}

		every_relation = {
			type = lover
			limit = {
				MTXXX_held_prisoner = yes
				not = { is_in_list = MTXXX_released_targets_list }
			}
			add_to_list = MTXXX_released_targets_list
		}
	
		every_relation = {
			type = soulmate
			limit = {
				MTXXX_held_prisoner = yes
				not = { is_in_list = MTXXX_released_targets_list }
			}
			add_to_list = MTXXX_released_targets_list
		}
	
		every_relation = {
			type = ward
			limit = {
				MTXXX_held_prisoner = yes
				not = { is_in_list = MTXXX_released_targets_list }
			}
			add_to_list = MTXXX_released_targets_list
		}
	
		if = {
			limit = { list_size = { name = MTXXX_released_targets_list value > 0 } }

			every_in_list = {
				list = MTXXX_released_targets_list

				release_from_prison = yes

				scope:raider = {
					add_hook = {
						type = loyalty_hook
						target = prev
					}
				}

				add_opinion = {
					target = scope:raider
					modifier = MTXXX_released_during_raid_opinion
				}

				create_character_memory = {
					type = MTXXX_memory_released
				
					participants = {
						raider = scope:raider
						holder = scope:holder
					}
				}
			}

			scope:raider = {
				create_character_memory = {
					type = MTXXX_memory_release
				
					participants = {
						holder = scope:holder
						raider = scope:raider
					}
				}
				trigger_event = MTXXX_raiding.0020
			}

			if = {
				limit = {
					exists = scope:holder
					scope:holder = { is_alive = yes }
				}
				scope:holder = {
					trigger_event = MTXXX_raiding.0025
				}
			}

			every_player = {
				limit = {
					exists = scope:holder
					scope:holder = { is_alive = yes }
					NOR = {
						this = scope:raider
						this = scope:holder
					}
					save_temporary_scope_as = notification_target
					any_in_list = {
						list = MTXXX_released_targets_list
						has_any_relation_trigger = { CHARACTER = scope:notification_target }
					}
				}
				trigger_event = MTXXX_raiding.0030
			}
		}

		every_in_list = {
			list = MTXXX_released_targets_list

			hidden_effect = {
				add_character_flag = {
					flag = block_imprisonment_event
					days = 7
				}
			}
		}

		if = {
			limit = {
				scope:holder ?= {
					is_alive = yes
					any_concubine = {
						count > 0
						has_opinion_modifier = {
							target = prev
							modifier = forced_me_concubine_marriage_opinion
						}
						or = {
							any_former_spouse = { MTXXX_raiding_former_consort_interested_in_concubine_trigger = yes }
							any_former_concubinist = { MTXXX_raiding_former_consort_interested_in_concubine_trigger = yes }
							MTXXX_raiding_root_interested_in_concubine_trigger = yes
						}
					}
				}
			}

			scope:holder = {
				every_concubine = {
					limit = {
						has_opinion_modifier = {
							target = prev
							modifier = forced_me_concubine_marriage_opinion
						}
						or = {
							any_former_spouse = { MTXXX_raiding_former_consort_interested_in_concubine_trigger = yes }
							any_former_concubinist = { MTXXX_raiding_former_consort_interested_in_concubine_trigger = yes }
							MTXXX_raiding_root_interested_in_concubine_trigger = yes
						}
					}

					add_to_list = mtxxx_concubines
				}
			}

			if = {
				limit = {
					list_size = { name = mtxxx_concubines value > 0 }
				}

				scope:raider = {
					create_character_memory = {
						type = MTXXX_memory_release_concubine
					
						participants = {
							holder = scope:holder
							raider = scope:raider
						}
					}
				}
			

				every_player = {
					limit = {
						exists = scope:holder
						scope:holder = { is_alive = yes }
						NOR = {
							this = scope:raider
							this = scope:holder
						}
						save_temporary_scope_as = notification_target
						any_in_list = {
							list = mtxxx_concubines
							has_any_relation_trigger = { CHARACTER = scope:notification_target }
						}
					}

					trigger_event = MTXXX_raiding.0035
				}

				every_in_list = {
					list = mtxxx_concubines
					save_scope_as = mtxxx_concubine
					root = {
						trigger_event = MTXXX_raiding.0040
					}
				}
			}
		}
	}
}

MTXXX_raiding.0020 = {
	hidden = yes

	immediate = {
		set_variable = {
			name = num_other_relevant_released_prisoners
			value = 0
		}

		ordered_in_list = {
			list = MTXXX_released_targets_list
			max = 99
			check_range_bounds = no

			order_by = {
				value = MTXXX_released_prisoners_importance_to_root_value
			}

			add_to_list = MTXXX_released_targets_root_notification_list
			root = {
				change_variable = {
					name = num_other_relevant_released_prisoners
					add = 1
				}
			}

			if = {
				limit = {
					NOT = { exists = scope:prisoner_1 }
				}
				save_scope_as = prisoner_1
			}
			else_if = {
				limit = {
					NOT = { exists = scope:prisoner_2 }
				}
				save_scope_as = prisoner_2
			}
		}

		change_variable = {
			name = num_other_relevant_released_prisoners
			add = -1
		}

		send_interface_message = {
			type = event_prison_good
			left_icon = scope:prisoner_1
			right_icon = root
			title = {
				first_valid = {
					triggered_desc = {
						trigger = {
							any_in_list = {
								list = MTXXX_released_targets_root_notification_list
								count = 1
							}
						}
						desc = MTXXX_raiding.0020.one_released.t
					}
					desc = MTXXX_raiding.0020.several_released.t
				}
			}
			desc = {
				first_valid = {
					triggered_desc = {
						trigger = {
							any_in_list = {
								list = MTXXX_released_targets_root_notification_list
								count = 1
							}
						}
						desc = MTXXX_raiding.0020.one_released
					}
					triggered_desc = {
						trigger = {
							any_in_list = {
								list = MTXXX_released_targets_root_notification_list
								count = 2
							}
						}
						desc = MTXXX_raiding.0020.two_released
					}
					desc = MTXXX_raiding.0020.several_released
				}
			}
			tooltip = event_message_effect

			show_as_tooltip = {
				every_in_list = {
					list = MTXXX_released_targets_root_notification_list

					release_from_prison = yes
				}
			}
		}

		remove_variable = num_other_relevant_released_prisoners
	}
}

MTXXX_raiding.0025 = {
	hidden = yes

	immediate = {
		send_interface_message = {
			type = event_prison_good
			left_icon = root
			right_icon = scope:holder
			title = MTXXX_raiding.0025.several_released.t
			desc = MTXXX_raiding.0025.end
			tooltip = event_message_effect
			every_in_list = {
				list = MTXXX_released_targets_list
				show_as_tooltip = {
					release_from_prison = yes
				}
			}
		}
	}
}

MTXXX_raiding.0030 = {
	hidden = yes

	immediate = {
		send_interface_message = {
			type = event_prison_good
			left_icon = root
			right_icon = scope:barony
			title = MTXXX_raiding.0030.relation_released.t
			desc = MTXXX_raiding.0030.end
			tooltip = event_message_effect
			every_in_list = {
				list = MTXXX_released_targets_list
				limit = {
					has_any_relation_trigger = { CHARACTER = scope:notification_target }
				}
				show_as_tooltip = {
					release_from_prison = yes
				}
			}
		}
	}
}

MTXXX_raiding.0035 = {
	hidden = yes

	immediate = {
		send_interface_message = {
			type = event_prison_good
			left_icon = root
			right_icon = scope:barony
			title = MTXXX_raiding.0035.relation_released.t
			desc = MTXXX_raiding.0035.end
			tooltip = event_message_effect
			every_in_list = {
				list = mtxxx_concubines
				limit = {
					has_any_relation_trigger = { CHARACTER = scope:notification_target }
				}
				show_as_tooltip = {
					release_from_prison = yes
				}
			}
		}
	}
}

MTXXX_raiding.0040 = {
	type = character_event
	title = MTXXX_raiding.0040.t
	desc = MTXXX_raiding.0040.desc
	theme = dynasty
	override_background = { reference = throne_room }

	left_portrait = {
		character = scope:mtxxx_concubine
		animation = thinking
	}

	right_portrait = {
		character = root
		triggered_animation = {
			trigger = {
				or = {
					scope:mtxxx_concubine = {
						or = {
							MTXXX_raiding_root_interested_in_concubine_trigger = yes
							any_former_spouse = { MTXXX_raiding_former_consort_interested_in_concubine_trigger = yes }
							any_former_concubinist = { MTXXX_raiding_former_consort_interested_in_concubine_trigger = yes }
						}
					}
					"opinion(scope:mtxxx_concubine)" >= medium_positive_opinion
					has_trait = wrathful
					has_trait = vengeful
					has_trait = zealous
				}
			}
			animation = anger
		}
		triggered_animation = {
			trigger = {
				or = {
					has_trait = honest
					has_trait = humble
					has_trait = compassionate
					has_trait = just
				}
			}
			animation = personality_compassionate
		}
		triggered_animation = {
			trigger = {
				or = {
					has_trait = callous
					has_trait = fickle
					has_trait = arbitrary
					has_trait = arrogant
				}
			}
			animation = thinking
		}
		triggered_animation = {
			trigger = { ai_honor >= medium_positive_ai_value }
			animation = personality_honorable
		}
		triggered_animation = {
			trigger = { ai_rationality >= medium_positive_ai_value }
			animation = personality_rational
		}
		triggered_animation = {
			trigger = { always = yes }
			animation = idle
		}
	}

	immediate = {
		scope:mtxxx_concubine = {
			if = {
				limit = {
					any_former_spouse = { MTXXX_raiding_former_consort_interested_in_concubine_trigger = yes }
				}

				random_former_spouse = {
					limit = {
						MTXXX_raiding_former_consort_interested_in_concubine_trigger = yes
						is_alive = yes
					}
					save_scope_as = mtxxx_former_spouse
					save_scope_as = mtxxx_former_spouse_or_concubinist
				}
			}

			if = {
				limit = {
					any_former_concubinist = { MTXXX_raiding_former_consort_interested_in_concubine_trigger = yes }
				}

				random_former_concubinist = {
					limit = {
						MTXXX_raiding_former_consort_interested_in_concubine_trigger = yes
						is_alive = yes
					}
					save_scope_as = mtxxx_former_concubinist
					save_scope_as = mtxxx_former_spouse_or_concubinist
				}
			}

			scope:raider = {
				add_hook = {
					type = loyalty_hook
					target = prev
				}
			}

			add_opinion = {
				target = scope:raider
				modifier = MTXXX_released_during_raid_opinion
			}

			create_character_memory = {
				type = MTXXX_memory_released_concubine
			
				participants = {
					raider = scope:raider
					holder = scope:holder
				}
			}
		}

		scope:mtxxx_former_spouse_or_concubinist ?= {
			scope:raider = {
				add_hook = {
					type = loyalty_hook
					target = prev
				}
			}

			add_opinion = {
				target = scope:raider
				modifier = MTXXX_released_consort_during_raid_opinion
			}
		}

		scope:mtxxx_concubine.concubinist = {
			send_interface_toast = {
				type = event_generic_neutral
				title = msg_concubine_dismissed_title
				right_icon = scope:mtxxx_concubine
				remove_concubine = scope:mtxxx_concubine
			}
		}
	}

	option = {
		name = MTXXX_raiding.0040.root_marry
		trigger = {
			exists = scope:mtxxx_former_spouse
			scope:mtxxx_former_spouse = {
				can_marry_character_trigger = { CHARACTER = scope:mtxxx_concubine }
			}
			scope:mtxxx_former_spouse = root
		}

		scope:mtxxx_concubine = {
			set_employer = root
			set_location_to_default = yes
		}

		trigger_event = MTXXX_raiding.0050

		ai_chance = {
			base = 100
		}
	}

	option = {
		name = MTXXX_raiding.0040.root_take_concubine
		trigger = {
			exists = scope:mtxxx_former_spouse
			scope:mtxxx_former_spouse = {
				not = { can_marry_character_trigger = { CHARACTER = scope:mtxxx_concubine } }
			}
			scope:mtxxx_former_spouse = root
			scope:mtxxx_former_spouse = {
				allowed_concubines = yes
				allowed_more_concubines = yes
			}
		}

		scope:mtxxx_concubine = {
			set_employer = root
			set_location_to_default = yes
		}

		root = {
			make_concubine = scope:mtxxx_concubine
		}

		ai_chance = {
			base = 100
		}
	}

	option = {
		name = MTXXX_raiding.0040.root_retake_concubine
		trigger = {
			exists = scope:mtxxx_former_concubinist
			scope:mtxxx_former_concubinist = root
			scope:mtxxx_former_concubinist = {
				allowed_concubines = yes
				allowed_more_concubines = yes
			}
		}

		scope:mtxxx_concubine = {
			set_employer = root
			set_location_to_default = yes
		}

		root = {
			make_concubine = scope:mtxxx_concubine
		}

		ai_chance = {
			base = 100
		}
	}

	option = {
		name = MTXXX_raiding.0040.root_add_courtier
		trigger = {
			exists = scope:mtxxx_former_spouse_or_concubinist
			scope:mtxxx_former_spouse_or_concubinist = {
				not = { can_marry_character_trigger = { CHARACTER = scope:mtxxx_concubine } }
			}
			scope:mtxxx_former_spouse_or_concubinist = root
			scope:mtxxx_former_spouse_or_concubinist = {
				nor = {
					allowed_concubines = yes
					allowed_more_concubines = yes
				}
			}
			scope:mtxxx_concubine = {
				any_child = {
					is_courtier_of = scope:mtxxx_former_spouse_or_concubinist
				}
			}
		}

		scope:mtxxx_concubine = {
			set_employer = root
			set_location_to_default = yes
		}

		ai_chance = {
			base = 100
		}
	}

	option = {
		name = MTXXX_raiding.0040.consort_marry
		trigger = {
			exists = scope:mtxxx_former_spouse
			scope:mtxxx_former_spouse = {
				can_marry_character_trigger = { CHARACTER = scope:mtxxx_concubine }
			}
			not = { scope:mtxxx_former_spouse = root }
		}

		scope:mtxxx_concubine = {
			set_employer = scope:mtxxx_former_spouse
			set_location_to_default = yes
		}

		trigger_event = MTXXX_raiding.0050

		ai_chance = {
			base = 100
		}
	}

	option = {
		name = MTXXX_raiding.0040.consort_take_concubine
		trigger = {
			exists = scope:mtxxx_former_spouse
			scope:mtxxx_former_spouse = {
				not = { can_marry_character_trigger = { CHARACTER = scope:mtxxx_concubine } }
			}
			not = { scope:mtxxx_former_spouse = root }
			scope:mtxxx_former_spouse = {
				allowed_concubines = yes
				allowed_more_concubines = yes
			}
		}

		scope:mtxxx_concubine = {
			set_employer = scope:mtxxx_former_spouse
			set_location_to_default = yes
		}

		scope:mtxxx_former_spouse = {
			make_concubine = scope:mtxxx_concubine
		}

		ai_chance = {
			base = 100
		}
	}

	option = {
		name = MTXXX_raiding.0040.concubinist_retake_concubine
		trigger = {
			exists = scope:mtxxx_former_concubinist
			not = { scope:mtxxx_former_concubinist = root }
			scope:mtxxx_former_concubinist = {
				allowed_concubines = yes
				allowed_more_concubines = yes
			}
		}

		scope:mtxxx_concubine = {
			set_employer = scope:mtxxx_former_concubinist
			set_location_to_default = yes
		}

		scope:mtxxx_former_concubinist = {
			make_concubine = scope:mtxxx_concubine
		}

		ai_chance = {
			base = 100
		}
	}

	option = {
		name = MTXXX_raiding.0040.consort_add_courtier
		trigger = {
			exists = scope:mtxxx_former_spouse_or_concubinist
			scope:mtxxx_former_spouse_or_concubinist = {
				not = { can_marry_character_trigger = { CHARACTER = scope:mtxxx_concubine } }
			}
			not = { scope:mtxxx_former_spouse_or_concubinist = root }
			scope:mtxxx_former_spouse_or_concubinist = {
				nor = {
					allowed_concubines = yes
					allowed_more_concubines = yes
				}
			}
			scope:mtxxx_concubine = {
				any_child = {
					is_courtier_of = scope:mtxxx_former_spouse_or_concubinist
				}
			}
		}

		scope:mtxxx_concubine = {
			set_employer = scope:mtxxx_former_spouse_or_concubinist
			set_location_to_default = yes
		}

		ai_chance = {
			base = 100
		}
	}

	option = {
		name = MTXXX_raiding.0040.join_child_court
		trigger = {
			scope:mtxxx_concubine = {
				any_child = {
					is_ruler = yes
				}
			}
			nor = {
				exists = scope:mtxxx_former_spouse
				exists = scope:mtxxx_former_concubinist
			}
		}

		scope:mtxxx_concubine = {
			ordered_child = {
				limit = { is_ruler = yes }
				order_by = "reverse_opinion(scope:mtxxx_concubine)"
				check_range_bounds = no
				save_scope_as = mtxxx_child

				scope:mtxxx_concubine = {
					set_employer = scope:mtxxx_child
					set_location_to_default = yes
				}
			}
		}

		ai_chance = {
			base = 100
		}
	}

	option = {
		name = MTXXX_raiding.0040.join_close_family_member_court
		trigger = {
			scope:mtxxx_concubine = {
				any_close_family_member = {
					is_ruler = yes
				}
			}
			nor = {
				exists = scope:mtxxx_former_spouse
				exists = scope:mtxxx_former_concubinist
			}
		}

		scope:mtxxx_concubine = {
			ordered_close_family_member = {
				limit = { is_ruler = yes }
				order_by = "reverse_opinion(scope:mtxxx_concubine)"
				check_range_bounds = no
				save_scope_as = mtxxx_family_member

				scope:mtxxx_concubine = {
					set_employer = scope:mtxxx_family_member
					set_location_to_default = yes
				}
			}
		}

		ai_chance = {
			base = 100
		}
	}

	option = {
		name = MTXXX_raiding.0040.sanctuary_in_root_court

		scope:mtxxx_concubine = {
			set_employer = root
			set_location_to_default = yes
		}
	}
}

MTXXX_raiding.0050 = {
	hidden = yes

	immediate = {
		if = {
			limit = { is_ai = no }

			trigger_event = MTXXX_raiding.0055
		}
		else = {
			if = {
				limit = {
					scope:mtxxx_former_spouse_or_concubinist = { MTXXX_raiding_root_interested_in_concubine_trigger = yes }
				}

				if = {
					limit = { scope:mtxxx_former_spouse_or_concubinist = { is_male = yes } }

					scope:mtxxx_former_spouse_or_concubinist = { marry = scope:mtxxx_concubine }
				}
				else = {
					scope:mtxxx_former_spouse_or_concubinist = { marry_matrilineal = scope:mtxxx_concubine }
				}
			}
			else_if = {
				limit = {
					scope:mtxxx_concubine = { MTXXX_raiding_root_interested_in_concubine_trigger = yes }
				}

				if = {
					limit = { scope:mtxxx_concubine = { is_male = yes } }

					scope:mtxxx_concubine = { marry = scope:mtxxx_former_spouse_or_concubinist }
				}
				else = {
					scope:mtxxx_concubine = { marry_matrilineal = scope:mtxxx_former_spouse_or_concubinist }
				}
			}
			else_if = {
				limit = {
					exists = scope:mtxxx_former_spouse_or_concubinist.dynasty
				}

				if = {
					limit = { scope:mtxxx_former_spouse_or_concubinist = { is_male = yes } }

					scope:mtxxx_former_spouse_or_concubinist = { marry = scope:mtxxx_concubine }
				}
				else = {
					scope:mtxxx_former_spouse_or_concubinist = { marry_matrilineal = scope:mtxxx_concubine }
				}
			}
			else = {
				if = {
					limit = { scope:mtxxx_concubine = { is_male = yes } }

					scope:mtxxx_concubine = { marry = scope:mtxxx_former_spouse_or_concubinist }
				}
				else = {
					scope:mtxxx_concubine = { marry_matrilineal = scope:mtxxx_former_spouse_or_concubinist }
				}
			}
		}
	}
}

MTXXX_raiding.0055 = {
	type = character_event
	title = MTXXX_raiding.0055.t
	desc = MTXXX_raiding.0055.desc
	theme = marriage

	left_portrait = {
		character = scope:mtxxx_concubine
		animation = personality_compassionate
	}
	right_portrait = {
		character = scope:mtxxx_former_spouse_or_concubinist
		animation = thinking
	}

	option = {
		name = MTXXX_raiding.0055.patrilineal
		scope:mtxxx_former_spouse_or_concubinist = { marry = scope:mtxxx_concubine }
	}

	option = {
		name = MTXXX_raiding.0055.matrilineal
		scope:mtxxx_former_spouse_or_concubinist = { marry_matrilineal = scope:mtxxx_concubine }
	}
}
