code:
marriage_interaction_on_accept_effect = {
	#Use hook
	if = {
		limit = { always = scope:marriage_hook }
		scope:actor = {
			use_hook = scope:recipient
		}
		#To make sure betrothals can't be broken if a hook was used
		if = {
			limit = {
				OR = {
					scope:secondary_actor = { is_adult = no }
					scope:secondary_recipient = { is_adult = no }
				}
			}
			scope:secondary_actor = {
				set_variable = {
					name = hook_used_for_betrothal
					value = scope:secondary_recipient
				}
			}
		}
	}

	#No longer concubine
	if = {
		limit = {
			scope:secondary_recipient = { is_concubine = yes }
		}
		scope:secondary_recipient = {
			random_consort = {
				limit = {
					any_concubine = { this = scope:secondary_recipient }
				}
				remove_concubine = scope:secondary_recipient
			}
		}
	}
	if = {
		limit = {
			scope:secondary_actor = { is_concubine = yes }
		}
		scope:secondary_actor = {
			random_consort = {
				limit = {
					any_concubine = { this = scope:secondary_actor }
				}
				remove_concubine = scope:secondary_actor
			}
		}
	}

	#Marriage notification events
	hidden_effect = {
		scope:actor = {
			if = {
				limit = { NOT = { this = scope:recipient } }
				trigger_event = marriage_interaction.0010
			}
			else = { #In my own court
				if = {
					limit = { #Betrothal?
						OR = {
							scope:secondary_actor = { is_adult = no }
							scope:secondary_recipient = { is_adult = no }
						}
					}
					send_interface_toast = {
						title = arrange_marriage_interaction_accept_betrothal_toast
						left_icon = scope:secondary_actor
						right_icon = scope:secondary_recipient
						custom_tooltip  = arrange_marriage_interaction_accept_betrothal_toast_desc
					}
				}
				else = {
					send_interface_toast = {
						title = arrange_marriage_interaction_accept_toast
						left_icon = scope:secondary_actor
						right_icon = scope:secondary_recipient
						custom_tooltip  = arrange_marriage_interaction_accept_toast_desc
					}
				}
			}
		}

		# Apply any relevant opinion penalties for relatives of spouse.
		scope:secondary_actor = {
			if = { # Polygamy
				limit = {
					any_consort = {
						count >= 2
					}
				}
				every_consort = {
					# If the spouse themselves does not believe in polygamy...
					limit = {
						NOR = {
							culture = { has_cultural_tradition = tradition_polygamous }
							faith = { is_polygamous = yes }
							faith = { has_doctrine = doctrine_concubines }
						}
					}

					#... then family members who also do not believe in polygamy aren't happy about having a relative forced into polygamous union with you.
					every_close_family_member = {
						if = {
							limit = {
								NOR = {
									culture = { has_cultural_tradition = tradition_polygamous }
									faith = { has_doctrine = doctrine_polygamy }
									faith = { has_doctrine = doctrine_concubines }
								}
							}
							add_opinion = {
								target = scope:secondary_actor
								modifier = relative_in_blasphemous_union_opinion
							}
						}
					}

					# NOTE: Spouse's own opinions handled in the 'on_marriage' code on_action inside of marriage_concubinage.txt, and are not included here.
				}
			}
		}
		scope:secondary_recipient = {
			if = { #Same-sex relations
				limit = {
					allowed_to_marry_same_sex_trigger = no
					sex_same_as = scope:secondary_actor
				}
				every_close_family_member = {
					if = {
						limit = {
							allowed_to_marry_same_sex_trigger = no
						}
						add_opinion = {
							target = scope:secondary_actor
							modifier = relative_in_blasphemous_union_opinion
						}
					}
				}
			}
		}
	}

	###MANAGEMENT SCRIPT FOR VARIOUS EVENTS/CONTENT###

	#If your spouse councillor had boosted the chance of marriage, remove modifier
	if = {
		limit = {
			scope:secondary_actor = {
				has_character_modifier = heir_easier_to_marry_off_modifier
			}
			scope:actor = { player_heir = scope:secondary_actor }
		}
		scope:secondary_actor = {
			remove_character_modifier = heir_easier_to_marry_off_modifier
		}
	}
	if = {
		limit = {
			scope:secondary_actor = {
				is_child_of = scope:actor
				has_character_modifier = child_easier_to_marry_off_modifier
			}
		}
		scope:secondary_actor = {
			remove_character_modifier = child_easier_to_marry_off_modifier
		}
	}

	#AI Break up consideration after marriage
	scope:secondary_recipient = {
		if = {
			limit = {
				is_adult = yes
				is_ai = yes
			}
			scope:secondary_actor = { save_scope_as = new_spouse_secondary_actor }
			trigger_event = lover.0205
		}
	}
	scope:secondary_actor = {
		if = {
			limit = {
				is_adult = yes
				is_ai = yes
			}
			scope:secondary_recipient = { save_scope_as = new_spouse_secondary_recipient }
			trigger_event = lover.0205
		}
	}
}

concubine_on_accept_effect = {
	scope:recipient = {
		trigger_event = marriage_interaction.0040
		if = {
			# Victim is angry for being forced into concubinage.
			limit = {
				is_imprisoned = yes
			}
			release_from_prison = yes
			add_opinion = {
				target = scope:actor
				modifier = forced_me_concubine_marriage_opinion
			}

			# Family members of victim are also angry that you've forced them into concubinage.
			every_close_family_member = {
				if = {
					limit = {
						NOT = { this = scope:actor } # Don't hate yourself.
					}
					add_opinion = {
						target = scope:actor
						modifier = forced_family_concubine_marriage_opinion
					}
					hidden_effect = {
						send_interface_message = {
							type = event_generic_neutral
							title = msg_close_family_member_taken_as_concubine_title
							right_icon = scope:recipient
							left_icon = scope:secondary_actor
							show_as_tooltip = { scope:secondary_actor = { make_concubine = scope:recipient } }
						}
					}

					# Family members are even *more* angry if they don't believe in concubines/polyamory (different from polygamy). Also if they don't accept same-sex relationships
					if = {
						limit = {
							OR = {
								NOT = { faith = { has_doctrine = doctrine_concubines } }
								NOT = { faith = { has_doctrine = tenet_polyamory } }
								AND = {
									allowed_to_marry_same_sex_trigger = no
									scope:recipient = { sex_same_as = scope:secondary_actor }
								}
							}
						}
						add_opinion = {
							target = scope:actor
							modifier = relative_in_blasphemous_union_opinion
						}
					}
				}
			}

			# Spouses, if any exist, are also furious.
			if = {
				limit = {
					is_married = yes
				}
				every_spouse = {
					show_as_tooltip = {
						add_opinion = {
							target = scope:actor
							modifier = forced_spouse_concubine_marriage_opinion
						}
						divorce = scope:recipient # no additional opinion hit, no scripted effect
					}
					trigger_event = marriage_interaction.0041
				}
			}

			# Stealing concubines pisses the former concubinist off.
			if = {
				limit = {
					is_concubine = yes
				}
				this.concubinist = {
					show_as_tooltip = {
						add_opinion = {
							target = scope:secondary_actor
							modifier = stole_concubine_opinion
						}
						remove_concubine = scope:recipient # no additional opinion hit, no scripted effect
					}
					trigger_event = marriage_interaction.0041
				}
			}

			# Break any betrothals which may exist (without additional opinion penalties).
			if = {
				limit = { exists = betrothed }
				betrothed = { trigger_event = marriage_interaction.0041 }
				break_betrothal = betrothed
			}

			# Having concubines means they're now no longer yours.
			if = {
				limit = { number_of_concubines > 0	}
				every_concubine = {
					scope:recipient = {
						remove_concubine = prev
					}
				}
			}
		}
	}

	# Send us a notification about what we just did.
	scope:secondary_actor = {
		hidden_effect = {
			send_interface_toast = {
				type = event_generic_neutral
				title = msg_concubine_taken_title
				right_icon = scope:recipient
				make_concubine = scope:recipient
			}
		}
	}
}

#This effect has some small differences to the previous one since this one involves one extra person (the one you give a concubine to)
concubine_offer_on_accept_effect = {
	scope:secondary_actor = {
		trigger_event = marriage_interaction.0040
		if = {
			# Victim is angry for being forced into concubinage.
			limit = {
				is_imprisoned = yes
			}
			add_character_flag = {
				flag = block_release_from_prison_event
				days = 3
			}
			release_from_prison = yes
			add_opinion = {
				target = scope:actor
				modifier = forced_me_concubine_marriage_opinion
			}

			# Family members of victim are also angry that you've forced them into concubinage.
			every_close_family_member = {
				if = {
					limit = {
						NOT = { this = scope:actor } # Don't hate yourself.
					}
					add_opinion = {
						target = scope:actor
						modifier = forced_family_concubine_marriage_opinion
					}
					hidden_effect = {
						send_interface_message = {
							type = event_generic_neutral
							title = msg_close_family_member_offered_as_concubine_title
							right_icon = scope:recipient
							left_icon = scope:secondary_actor
							show_as_tooltip = { scope:secondary_actor = { make_concubine = scope:recipient } }
						}
					}

					# Family members are even *more* angry if they don't believe in concubines/polyamory (different from polygamy). Also if they don't accept same-sex relationships
					if = {
						limit = {
							OR = {
								NOT = { faith = { has_doctrine = doctrine_concubines } }
								NOT = { faith = { has_doctrine = tenet_polyamory } }
								AND = {
									allowed_to_marry_same_sex_trigger = no
									scope:recipient = { sex_same_as = scope:secondary_actor }
								}
							}
						}
						add_opinion = {
							target = scope:actor
							modifier = relative_in_blasphemous_union_opinion
						}
					}
				}
			}

			# Spouses, if any exist, are also furious.
			if = {
				limit = {
					is_married = yes
					#A check to make sure you haven't somehow married them during the consideration time
					NOT = { any_spouse = { this = scope:recipient } }
				}
				every_spouse = {
					add_opinion = {
						target = scope:actor
						modifier = forced_spouse_concubine_marriage_opinion
					}
					divorce = scope:secondary_actor # no additional opinion hit, no scripted effect
					save_scope_value_as = { # For tooltips in the coming event
						name = was_spouse
						value = yes
					}
					trigger_event = marriage_interaction.0043
				}
			}

			# Stealing concubines pisses the former concubinist off.
			if = {
				limit = {
					is_concubine = yes
					#A check to make sure you haven't somehow gotten them as concubine during the consideration time
					NOT = { any_consort = { this = scope:recipient } }
				}
				this.concubinist = {
					if = {
						limit = {
							NOT = {
								this = scope:actor
							}
						}
						add_opinion = {
							target = scope:actor
							modifier = stole_concubine_opinion
						}
						remove_concubine = scope:secondary_actor # no additional opinion hit, no scripted effect
						save_scope_value_as = { # For tooltips in the coming event
							name = was_concubine
							value = yes
						}
						trigger_event = marriage_interaction.0043
					}
					else = {
						remove_concubine = scope:secondary_actor # no additional opinion hit, no scripted effect
					}
				}
			}

			# Break any betrothals which may exist (without additional opinion penalties).
			if = {
				limit = {
					exists = betrothed
					#A check to make sure you haven't somehow gotten betrothed to them during the consideration time
					NOT = { betrothed = scope:recipient }
				}
				betrothed = { trigger_event = marriage_interaction.0043 }
				break_betrothal = betrothed
			}

			# Having concubines means they're now no longer yours.
			if = {
				limit = { number_of_concubines > 0	}
				every_concubine = {
					scope:secondary_actor = {
						remove_concubine = prev
					}
				}
			}
		}
		if = {
			limit = {
				is_concubine_of = scope:actor
			}
			scope:actor = {
				remove_concubine = scope:secondary_actor
			}
		}
		remove_character_flag = has_been_offered_as_concubine
	}

	# Send us a notification about what we just did. If the secondary_actor somehow ended up as a consort (spouse/concubine) or betrothed to recipient during the consideration time of this request we'll send a message about that instead (should only happen to AI tho)
	if = {
		limit = { #Invalidation message
			scope:secondary_actor = {
				OR = {
					any_consort = { count >= 1 }
					exists = betrothed
				}
			}
		}
		scope:secondary_actor = {
			# If they've somehow already become entangled with the recipient we tell this
			if = {
				limit = {
					OR = {
						is_consort_of = scope:recipient
						AND = {
							exists = betrothed
							betrothed = scope:recipient
						}
					}
				}
				scope:recipient = {
					save_scope_as = spouse
				}
			}
			# Else, if they've married or gotten betrothed to someone else, we say this
			else_if = {
				limit = {
					exists = betrothed
				}
				betrothed = {
					save_scope_as = spouse
				}
			}
			else = {
				random_consort = {
					limit = {
						this = scope:secondary_actor.primary_spouse
					}
					alternative_limit = {
						always = yes
					}
					save_scope_as = spouse
				}
			}
		}
		scope:actor = {
			send_interface_toast = {
				type = event_generic_neutral
				title = msg_concubine_offered_invalidated_title
				right_icon = scope:recipient
				left_icon = scope:secondary_actor
				if = {
					limit = {
						scope:secondary_actor = { is_consort_of = scope:spouse }
					}
					custom_tooltip = msg_concubine_offered_invalidated_consort
				}
				else = {
					custom_tooltip = msg_concubine_offered_invalidated_betrothed
				}
			}
		}
		scope:recipient = {
			send_interface_toast = {
				type = event_generic_neutral
				title = msg_concubine_offered_invalidated_recipient_title
				right_icon = scope:actor
				left_icon = scope:secondary_actor
				if = {
					limit = {
						scope:secondary_actor = { is_consort_of = scope:spouse }
					}
					custom_tooltip = msg_concubine_offered_invalidated_consort
				}
				else = {
					custom_tooltip = msg_concubine_offered_invalidated_betrothed
				}
			}
		}
	}
	else = { #Proper notification
		scope:actor = {
			send_interface_toast = {
				type = event_generic_neutral
				title = msg_concubine_offered_title
				right_icon = scope:recipient
				left_icon = scope:secondary_actor
				scope:recipient = {
					make_concubine = scope:secondary_actor
					if = { #Scale opinion gain a bit depending on how cool the concubine is
						limit = {
							scope:secondary_actor = {
								OR = {
									has_trait = beauty_good_2
									has_trait = beauty_good_3
									num_of_good_genetic_traits > 1
									has_relation_lover = scope:recipient
									has_relation_soulmate = scope:recipient
								}
								trigger_if = {
									limit = {
										is_female = yes
									}
									age < define:NChildbirth|MAX_FEMALE_REPRODUCTION_AGE
								}
							}
						}
						add_opinion = {
							target = scope:actor
							modifier = grateful_opinion
							opinion = 40
						}
					}
					else_if = {
						limit = {
							scope:secondary_actor = {
								OR = {
									is_lowborn = no
									has_trait = beauty_good_1
									num_of_good_genetic_traits > 0
									has_relation_friend = scope:recipient
									has_relation_best_friend = scope:recipient
								}
								trigger_if = {
									limit = {
										is_female = yes
									}
									age < define:NChildbirth|MAX_FEMALE_REPRODUCTION_AGE
								}
							}
						}
						add_opinion = {
							target = scope:actor
							modifier = grateful_opinion
							opinion = 30
						}
					}
					else = {
						add_opinion = {
							target = scope:actor
							modifier = grateful_opinion
							opinion = 20
						}
					}
				}
			}
		}
	}
}

divorce_effect = {
	$DIVORCER$ = {
		if = {
			limit = {
				any_spouse = {
					this = $DIVORCEE$
				}
			}
			divorce = $DIVORCEE$
		}

		reverse_add_opinion = {
			modifier = divorced_me_opinion
			target = $DIVORCEE$
		}
	}
	# Opinion hit
	$DIVORCEE$ = {
		every_close_or_extended_family_member = {
			limit = { NOT = { this = $DIVORCER$ } } # don't hate yourself just because of some incest
			custom = all_family_members
			add_opinion = {
				modifier = divorced_close_kin
				target = $DIVORCER$
			}
		}
	}

	if = {
		limit = {
			$DIVORCER$ = {
				has_character_modifier = rejected_from_marriage_bed_modifier
			}
		}
		$DIVORCER$ = { remove_character_modifier = rejected_from_marriage_bed_modifier }
	}
	if = {
		limit = {
			$DIVORCEE$ = {
				has_character_modifier = rejected_from_marriage_bed_modifier
			}
		}
		$DIVORCEE$ = { remove_character_modifier = rejected_from_marriage_bed_modifier }
	}
}

less_verbose_divorce_effect = {
	$DIVORCER$ = {
		if = {
			limit = {
				any_spouse = {
					this = $DIVORCEE$
				}
			}
			divorce = $DIVORCEE$
		}

		hidden_effect = {
			reverse_add_opinion = {
				modifier = divorced_me_opinion
				target = $DIVORCEE$
			}
		}
	}
	hidden_effect = {
		# Opinion hit
		$DIVORCEE$ = {
			every_close_or_extended_family_member = {
				limit = { NOT = { this = $DIVORCER$ } } # don't hate yourself just because of some incest
				custom = all_family_members
				add_opinion = {
					modifier = divorced_close_kin
					target = $DIVORCER$
				}
			}
		}

		if = {
			limit = {
				$DIVORCER$ = {
					has_character_modifier = rejected_from_marriage_bed_modifier
				}
			}
			$DIVORCER$ = { remove_character_modifier = rejected_from_marriage_bed_modifier }
		}
		if = {
			limit = {
				$DIVORCEE$ = {
					has_character_modifier = rejected_from_marriage_bed_modifier
				}
			}
			$DIVORCEE$ = { remove_character_modifier = rejected_from_marriage_bed_modifier }
		}
	}
}

marriage_soulmate_exclusivity_confrontation_effect = {
	$SPOUSE_1$ = { add_to_temporary_list = new_spouses }
	$SPOUSE_2$ = { add_to_temporary_list = new_spouses }

	every_in_list = {
		list = new_spouses
		limit = {
			any_relation = {
				type = soulmate
				NOT = { is_in_list = new_spouses }
			}
		}
		add_character_flag = {
			flag = can_be_confronted_about_lover_exclusivity
			days = 1825
		}
	}
}

# CHARACTER = character we check for if they should send a message
# SPOUSE = the spouse in this divorce
# scope:reason = flag:script or flag:faith
send_divorce_notifications_effect = {
	$CHARACTER$ = {
		if = {
			limit = {
				is_ai = no
			}
			switch = {
				trigger = scope:reason
				flag:script = {
					send_interface_message = {
						type = msg_divorce
						left_icon = $SPOUSE$
					}
				}
				flag:faith = {
					send_interface_message = {
						type = msg_spouse_invalidated
						left_icon = $SPOUSE$
					}
				}
			}
		}
		else_if = {
			limit = {
				any_heir_to_title = {
					exists = holder
					holder = { is_ai = no }
				}
			}
			every_heir_to_title = {
				limit = {
					exists = holder
					holder = { is_ai = no }
				}
				holder = {
					if = {
						limit = {
							NOT = { is_in_list = had_notification_sent }
						}
						add_to_list = had_notification_sent
						switch = {
							trigger = scope:reason
							flag:script = {
								send_interface_message = {
									type = msg_divorce_player_heir
									left_icon = $CHARACTER$
									right_icon = $SPOUSE$
								}
							}
							flag:faith = {
								send_interface_message = {
									type = msg_spouse_invalidated_player_heir
									left_icon = $CHARACTER$
									right_icon = $SPOUSE$
								}
							}
						}
					}
				}
			}
		}
	}
}

# CHARACTER = character we check for if they should send a message
# BETROTHED = the other person in the betrothal
# scope:reason = flag:timeout, flag:death, flag:faith, or flag:script
send_break_betrothal_notifications_effect = {
	$CHARACTER$ = {
		if = {
			limit = {
				is_ai = no
			}
			switch = {
				trigger = scope:reason
				flag:script = {
					send_interface_message = {
						type = msg_break_betrothal
						left_icon = $BETROTHED$
					}
				}
				flag:faith = {
					send_interface_message = {
						type = msg_betrothal_invalidated
						left_icon = $BETROTHED$
					}
				}
				# flag:death is hanlded by normal death management events
				# flag:timeout is hanlded by caller separately
			}
		}
		else_if = {
			limit = {
				any_heir_to_title = {
					exists = holder
					holder = { is_ai = no }
				}
			}
			every_heir_to_title = {
				limit = {
					holder = { is_ai = no }
				}
				holder = {
					if = {
						limit = {
							NOT = { is_in_list = had_notification_sent }
						}
						add_to_list = had_notification_sent
						switch = {
							trigger = scope:reason
							flag:script = {
								send_interface_message = {
									type = msg_break_betrothal_player_heir
									left_icon = $CHARACTER$
									right_icon = $BETROTHED$
								}
							}
							flag:faith = {
								send_interface_message = {
									type = msg_betrothal_invalidated_player_heir
									left_icon = $CHARACTER$
									right_icon = $BETROTHED$
								}
							}
							# flag:death is hanlded by normal death management events
							# flag:timeout is hanlded by caller separately
						}
					}
				}
			}
		}
	}
}

# When you take spouses/concubines whose Faith does not permit that union, they will gain opinion penalties.
# This effect adds/updates/replaces/removes those opinion penalties, as approprite to the current situation.
update_active_consort_opinion_effect = {

	# If we currently have any existing consort opinion modifiers, check if they should be downgraded from permanent modifiers to decaying modifiers.
	downgrade_invalid_consort_opinions_effect = { PARTNER = $PARTNER$ }

	if = {
		# If this opinion change was triggered by being divorced/set-aside, we may still technically be married/joined to PARTNER at this moment, but we won't be very shortly and should skip this step.
		limit = {
			NOR = {
				exists = scope:is_being_divorced
				exists = scope:is_being_set_aside
			}
		}

		# Oterwise, apply any new consort opinion effects that are applicable.
		if = {
			# Characters who do not believe in concubinage, but are forced into it, have the harshest opinion penalties.
			limit = {
				is_concubine_of = $PARTNER$
				NOT = { faith = { has_doctrine = doctrine_concubines }}
			}
			if = {
				# Harshest opinion penalty if they are a concubine but believe in monogamy
				limit = { faith = { has_doctrine = doctrine_monogamy }}
				add_opinion = {
					target = $PARTNER$
					modifier = concubine_with_monogamous_faith_opinion
				}
			}
			else_if = {
				# Lesser-but-still-harsh opinion penalty if they are a concubine but believe in polygamy
				limit = {
					OR = {
						culture = { has_cultural_tradition = tradition_polygamous }
						faith = { has_doctrine = doctrine_polygamy }
					}
				}
				add_opinion = {
					target = $PARTNER$
					modifier = concubine_with_polygamous_faith_opinion
				}
			}
		}
		else_if = {
			# Characters who are legitimate spouses of their partner, but do not approve of their partner's other spouses/concubines, will have milder opinion penalties.
			limit = {
				is_spouse_of = $PARTNER$
				$PARTNER$ = { any_consort = { count >= 2 }}
				NOT = { faith = { has_doctrine = doctrine_concubines }}
				NOT = { faith = { has_doctrine = tenet_polyamory }}
			}
			if = {
				limit = {
					$PARTNER$ = { any_concubine = { count >= 1 }}
				}
				add_opinion = {
					target = $PARTNER$
					modifier = spouse_does_not_believe_in_concubines_opinion
				}
				#Remove the opinion modifier if you flip-flop and go get concubines after getting rid of them all...
				remove_opinion = {
					target = $PARTNER$
					modifier = spouse_does_not_believe_in_former_concubines_opinion
				}
			}
			else_if = {
				limit = {
					NOR = {
						culture = { has_cultural_tradition = tradition_polygamous }
						faith = { is_polygamous = yes }

					}
				}
				add_opinion = {
					target = $PARTNER$
					modifier = polygamous_marriage_opinion
				}
			}
		}
		#Same-sex not accepted penalty
		if = {
			limit = {
				allowed_to_marry_same_sex_trigger = no
				sex_same_as = $PARTNER$
			}
			add_opinion = {
				target = $PARTNER$
				modifier = same_sex_with_no_acceptance_opinion
			}
		}
	}
}

# Checks if we have any permanent opinion modifiers which are no longer applicable. If so, downgrade them to decaying opinion modifiers.
downgrade_invalid_consort_opinions_effect = {
	if = {
		limit = {
			has_opinion_modifier = {
				modifier = concubine_with_monogamous_faith_opinion
				target = $PARTNER$
			}
			OR = {
				# We are no longer a concubine of PARTNER...
				NOT = { is_concubine_of = $PARTNER$ }
				# ...or will no longer be a concubine of them.
				trigger_if = {
					limit = { exists = scope:is_being_set_aside}
					scope:is_being_set_aside = this
				}

				# Or, our Faith has changed to permit us being a concubine.
				faith = { has_doctrine = doctrine_concubines }
			}
		}
		remove_opinion = {
			modifier = concubine_with_monogamous_faith_opinion
			target = $PARTNER$
		}
		add_opinion = {
			modifier = formerly_concubine_with_monogamous_faith_opinion
			target = $PARTNER$
		}
	}

	if = {
		limit = {
			has_opinion_modifier = {
				modifier = concubine_with_polygamous_faith_opinion
				target = $PARTNER$
			}
			OR = {
				# We are no longer a concubine of PARTNER...
				NOT = { is_concubine_of = $PARTNER$ }
				# ...or will no longer be a concubine of them.
				trigger_if = {
					limit = { exists = scope:is_being_set_aside}
					scope:is_being_set_aside = this
				}

				# Or, our Faith has changed to permit us being a concubine.
				faith = { has_doctrine = doctrine_concubines }
			}
		}
		remove_opinion = {
			modifier = concubine_with_polygamous_faith_opinion
			target = $PARTNER$
		}
		add_opinion = {
			modifier = formerly_concubine_with_polygamous_faith_opinion
			target = $PARTNER$
		}
	}

	if = {
		limit = {
			has_opinion_modifier = {
				modifier = spouse_does_not_believe_in_concubines_opinion
				target = $PARTNER$
			}
			OR = {
				# We are no longer married to PARTNER...
				NOT = { is_spouse_of = $PARTNER$ }
				# ...or will no longer be married to them.
				trigger_if = {
					limit = { exists = scope:is_being_divorced}
					scope:is_being_divorced = this
				}

				# Or, the offending relationship no longer exists.
				$PARTNER$ = { any_concubine = {	count = 0 }}
				AND = {
					# The last remaining concubine is being set aside, leaving none.
					exists = scope:is_being_set_aside
					$PARTNER$ = { any_concubine = { count = 1 }}
				}

				# Or, our Faith has changed to permit the relationship.
				faith = { has_doctrine = doctrine_concubines }
				faith = { has_doctrine = tenet_polyamory }
			}
		}
		remove_opinion = {
			modifier = spouse_does_not_believe_in_concubines_opinion
			target = $PARTNER$
		}
		add_opinion = {
			modifier = spouse_does_not_believe_in_former_concubines_opinion
			target = $PARTNER$
		}
	}

	if = {
		limit = {
			has_opinion_modifier = {
				modifier = polygamous_marriage_opinion
				target = $PARTNER$
			}
			OR = {
				# We are no longer married to PARTNER...
				NOT = { is_spouse_of = $PARTNER$ }
				# ...or will no longer be married to them.
				trigger_if = {
					limit = { exists = scope:is_being_divorced}
					scope:is_being_divorced = this
				}

				# Or, the offending relationship no longer exists.
				$PARTNER$ = { any_spouse = { count = 1 }}
				AND = {
					# One of the 2 remaining spouses will be divorced next tick, leaving only 1.
					exists = scope:is_being_divorced
					$PARTNER$ = { any_spouse = { count = 2 }}
				}

				# Or, our Faith has changed to permit the relationship.
				faith = { has_doctrine = doctrine_concubines }
				culture = { has_cultural_tradition = tradition_polygamous }
				faith = { is_polygamous = yes }
				faith = { has_doctrine = tenet_polyamory }
			}
		}
		remove_opinion = {
			modifier = polygamous_marriage_opinion
			target = $PARTNER$
		}
		add_opinion = {
			modifier = former_polygamous_marriage_opinion
			target = $PARTNER$
		}
	}

	if = {
		limit = {
			has_opinion_modifier = {
				modifier = same_sex_with_no_acceptance_opinion
				target = $PARTNER$
			}
			OR = {
				# We are no partner of PARTNER...
				NOT = { is_consort_of = $PARTNER$ }
				# ...or will no longer be married to them
				trigger_if = {
					limit = { exists = scope:is_being_divorced}
					scope:is_being_divorced = this
				}
				# ...or will no longer be a concubine of them
				trigger_if = {
					limit = { exists = scope:is_being_set_aside}
					scope:is_being_set_aside = this
				}

				# Or, the offending relationship no longer exists.
				sex_opposite_of = $PARTNER$

				# Or, our Faith has changed to permit the relationship.
				allowed_to_marry_same_sex_trigger = yes
			}
		}
		remove_opinion = {
			modifier = same_sex_with_no_acceptance_opinion
			target = $PARTNER$
		}
		add_opinion = {
			modifier = former_same_sex_with_no_acceptance_opinion
			target = $PARTNER$
		}
	}
}


code:
regula_bloodline_addition_effect = {
    add_trait = $BLOODLINE$
    every_child = { # Sons/Daughters
        add_trait = $BLOODLINE$
        every_child = { # Grandsons/Grandaughters
            add_trait = $BLOODLINE$
            every_child = { # Great-grandkids
            add_trait = $BLOODLINE$
            }
        }
    }
}

regula_bloodline_dynasty_addition_effect = {
    add_trait = $BLOODLINE$
    dynasty= {
        every_dynasty_member = {
             add_trait = $BLOODLINE$
        }
    }
}

license: mit


code:
#########################
# Contubernalis Effects #
#########################

# Helper effects for the Contubernalis effect

# Turns the currently scoped character into a contubernalis
# Assumes the character is a women
# Effects are:
# 1. Make Family/Spouses not like you
regula_turn_into_contubernalis = {

	# Make family and spouse not like you
	every_close_family_member = { # Opinion hits first.
		limit = { NOT = { this = scope:actor } }
		add_to_temporary_list = victim_family_list
	}

	every_spouse = {
		limit = {
			NOR = {
				this = scope:actor
				is_in_list = victim_family_list
			}
		}
		add_to_temporary_list = victim_family_list
	}

	# If player has a reason to "execute" contubernalis target, their family dont consider it a crime
	if = {
		limit = {
			any_in_list = {
				list = victim_family_list
				count > 0
			}
		}
		if = {
			limit = {
				scope:actor = {
					has_execute_reason = scope:recipient
				}
			}
			every_in_list = {
				list = victim_family_list
				custom = all_close_family_and_spouses
				add_opinion = {
					target = scope:actor
					modifier = regula_contubernalis_close_family_opinion
				}
			}
		}
		else = {
			every_in_list = {
				list = victim_family_list
				custom = all_close_family_and_spouses
				add_opinion = {
					target = scope:actor
					modifier = regula_contubernalis_close_family_crime_opinion
				}
			}
		}
	}

	# Victim's dynasty hates executioner
	if = {
		limit = {
			exists = dynasty
			exists = scope:actor.dynasty
			NOT = { dynasty = scope:actor.dynasty }
			NOT = {
				scope:actor = {
					has_execute_reason = scope:recipient
				}
			}
		}
		dynasty = {
			every_dynasty_member = {
				limit = {
					NOR = {
						this = scope:recipient
						is_in_list = victim_family_list
					}
				}
				custom = all_dynasty_members
				add_to_temporary_list = victim_dynasty_list
				add_opinion = {
					target = scope:actor
					modifier = regula_contubernalis_dynasty_member_opinion
				}
			}
		}
	}

	# Victim's friends and lovers
	every_relation = {
		type = friend
		limit = {
			NOR = {
				this = scope:recipient
				is_in_list = victim_family_list
				is_in_list = victim_dynasty_list
			}
		}
		add_to_list = victim_close_relations_list
	}
	every_relation = {
		type = lover
		limit = {
			NOR = {
				this = scope:recipient
				is_in_list = victim_family_list
				is_in_list = victim_dynasty_list
				is_in_list = victim_close_relations_list
			}
		}
		add_to_list = victim_close_relations_list
	}
	if = {
		limit = {
			any_in_list = {
				list = victim_close_relations_list
				always = yes
			}
		}
		every_in_list = {
			list = victim_close_relations_list
			custom = all_friends_and_lovers
			add_opinion = {
				target = scope:actor
				modifier = regula_contubernalis_close_relation_opinion
			}
		}
	}

	# Depose and divorce.
	if = {
		limit = { is_ruler = yes }
		depose = yes
	}
	if = {
		limit = { is_married = yes }
		every_spouse = {
			divorce = scope:recipient
		}
	}
	if = {
		limit = { exists = betrothed }
		break_betrothal = betrothed
	}

	# Clear existing traits and mold into slave
	clear_traits = yes
	add_trait = lustful
	add_trait = zealous
	add_trait = humble
	add_trait = contubernalis
	set_immortal_age = 20
	set_character_faith = global_var:magister_character.faith
	set_sexuality = bisexual
	carn_increase_beauty_one_step_effect = yes
	carn_remove_all_major_disfigurements_effect = yes
	carn_remove_all_minor_disfigurements_effect = yes
	carn_remove_all_wounds_effect = yes

	carn_had_sex_with_effect = {
		CHARACTER_1 = scope:actor
		CHARACTER_2 = scope:recipient
		C1_PREGNANCY_CHANCE = pregnancy_chance
		C2_PREGNANCY_CHANCE = pregnancy_chance
		STRESS_EFFECTS = yes
		DRAMA = no
	}
}


code:
regula_create_champion_giant_effect = {
	#Create them
	create_character = {
		save_temporary_scope_as = created_character
		location = $WHO$.capital_province
		culture = $WHO$.culture
		faith = $WHO$.faith
		gender = female
		template = regula_champion_giant_character
	}
}

regula_create_commander_effect = {
	#Create them
	create_character = {
		save_temporary_scope_as = created_character
		location = $WHO$.capital_province
		culture = $WHO$.culture
		faith = $WHO$.faith
		gender = female
		template = regula_commander_character
	}
}

regula_create_orgy_guest_effect = {
	#Create them
	create_character = {
		save_temporary_scope_as = created_orgy_guest
		location = $WHO$.capital_province
		culture = $WHO$.culture
		faith = $WHO$.faith
		gender = female
		template = regula_beautiful_character
	}
	hidden_effect = {
		scope:created_orgy_guest = {
			add_trait = lustful
		}

		#If the created character is a lesbian they will become bisexual nine times in ten.
		if = {
			limit = {
				$WHO$ = { is_attracted_to_gender_of = scope:created_orgy_guest }
				NOT = { scope:created_orgy_guest = { is_attracted_to_gender_of = $WHO$ } }
			}
			random = {
				chance = 90
				scope:created_orgy_guest = {
					set_sexuality = bisexual
				}
			}
		}
	}
}

regula_create_placeholder_character_effect = {
	create_character = {
		save_temporary_scope_as = created_character
		location = $WHO$.capital_province
		culture = $WHO$.culture
		faith = $WHO$.faith
		gender = female
		age = 0
		random_traits = no
		dynasty = none
	}
}

# Each Inititate is specialised
# 1. The Solider - Giant, Strong style traits, good martial and prowess
# 2. The Priestess - High Learning, Zealous
# 3. The Spy - High Intrigue, prowess
# 4. The Lady - decent stats all around, content, good vassal/wife
# 5. The Breeder - Good congential traits, chance for pure blooded, not so great stat line
regula_create_inititate_effect = {

	switch = {
		trigger = $inititate_type$
			1 = {
				switch = {
					trigger = $inititate_rarity$
					1 = {
						create_character = {
							save_temporary_scope_as = created_character
							location = $WHO$.capital_province
							culture = $WHO$.culture
							faith = $WHO$.faith
							gender = female
							template = regula_inititate_solider_common_character
						}
					}
					2 = {
						create_character = {
							save_temporary_scope_as = created_character
							location = $WHO$.capital_province
							culture = $WHO$.culture
							faith = $WHO$.faith
							gender = female
							template = regula_inititate_solider_noble_character
						}
					}
					3 = {
						create_character = {
							save_temporary_scope_as = created_character
							location = $WHO$.capital_province
							culture = $WHO$.culture
							faith = $WHO$.faith
							gender = female
							template = regula_inititate_solider_royal_character
						}
					}
				}
			}

			2 = {
				switch = {
					trigger = $inititate_rarity$
					1 = {
						create_character = {
							save_temporary_scope_as = created_character
							location = $WHO$.capital_province
							culture = $WHO$.culture
							faith = $WHO$.faith
							gender = female
							template = regula_inititate_priestess_common_character
						}
					}
					2 = {
						create_character = {
							save_temporary_scope_as = created_character
							location = $WHO$.capital_province
							culture = $WHO$.culture
							faith = $WHO$.faith
							gender = female
							template = regula_inititate_priestess_noble_character
						}
					}
					3 = {
						create_character = {
							save_temporary_scope_as = created_character
							location = $WHO$.capital_province
							culture = $WHO$.culture
							faith = $WHO$.faith
							gender = female
							template = regula_inititate_priestess_royal_character
						}
					}
				}
			}

			3 = {
				switch = {
					trigger = $inititate_rarity$
					1 = {
						create_character = {
							save_temporary_scope_as = created_character
							location = $WHO$.capital_province
							culture = $WHO$.culture
							faith = $WHO$.faith
							gender = female
							template = regula_inititate_lady_common_character
						}
					}
					2 = {
						create_character = {
							save_temporary_scope_as = created_character
							location = $WHO$.capital_province
							culture = $WHO$.culture
							faith = $WHO$.faith
							gender = female
							template = regula_inititate_lady_noble_character
						}
					}
					3 = {
						create_character = {
							save_temporary_scope_as = created_character
							location = $WHO$.capital_province
							culture = $WHO$.culture
							faith = $WHO$.faith
							gender = female
							template = regula_inititate_lady_royal_character
						}
					}
				}
			}

			4 = {
				switch = {
					trigger = $inititate_rarity$
					1 = {
						create_character = {
							save_temporary_scope_as = created_character
							location = $WHO$.capital_province
							culture = $WHO$.culture
							faith = $WHO$.faith
							gender = female
							template = regula_inititate_spy_common_character
						}
					}
					2 = {
						create_character = {
							save_temporary_scope_as = created_character
							location = $WHO$.capital_province
							culture = $WHO$.culture
							faith = $WHO$.faith
							gender = female
							template = regula_inititate_spy_noble_character
						}
					}
					3 = {
						create_character = {
							save_temporary_scope_as = created_character
							location = $WHO$.capital_province
							culture = $WHO$.culture
							faith = $WHO$.faith
							gender = female
							template = regula_inititate_spy_royal_character
						}
					}
				}
			}

			5 = {
				switch = {
					trigger = $inititate_rarity$
					1 = {
						create_character = {
							save_temporary_scope_as = created_character
							location = $WHO$.capital_province
							culture = $WHO$.culture
							faith = $WHO$.faith
							gender = female
							template = regula_inititate_breeder_common_character
						}
					}
					2 = {
						create_character = {
							save_temporary_scope_as = created_character
							location = $WHO$.capital_province
							culture = $WHO$.culture
							faith = $WHO$.faith
							gender = female
							template = regula_inititate_breeder_noble_character
						}
					}
					3 = {
						create_character = {
							save_temporary_scope_as = created_character
							location = $WHO$.capital_province
							culture = $WHO$.culture
							faith = $WHO$.faith
							gender = female
							template = regula_inititate_breeder_royal_character
						}
					}
				}
			}
		}

		# add Mulsa trait if we have the holy site

		if = {
			limit = {
				$WHO$.faith = {
					controls_holy_site_with_flag = holy_site_reg_mulsa_fascinare_flag
				}
			}
			hidden_effect = {
				scope:created_character = {
					add_trait = mulsa
				}
			}
		}
}


code:
######################################################################
# OUTCOME EFFECTS
######################################################################

fascinare_block_character_effect = {
	if = {
		limit = { root = scope:target }
		custom_tooltip = fascinare_outcome.permanent_block

		hidden_effect = {
			add_opinion = {
				target = $CHARACTER$
				modifier = fascinare_permanent_blocker_opinion
			}
		}
	}
	else = {
		add_opinion = {
			target = $CHARACTER$
			modifier = fascinare_permanent_blocker_opinion
		}
	}
}

fascinare_cooldown_for_character_effect = {
	if = {
		limit = { root = scope:target }
		custom_tooltip = seduce_outcome.cooldown

		hidden_effect = {
			add_opinion = {
				target = $CHARACTER$
				modifier = fascinare_regular_cooldown_opinion
			}
		}
	}
	else = {
		add_opinion = {
			target = $CHARACTER$
			modifier = fascinare_regular_cooldown_opinion
		}
	}
}

fascinare_outcome_publicised_attempted_crimes_or_nothing_effect = {
	$TARGET$ = {
		#Grab everyone relevant who might be interested in the attempt.
		every_close_family_member = { add_to_list = relevant_parties }
		every_consort = {
			limit = {
				NOT = { is_in_list = relevant_parties }
			}
			add_to_list = relevant_parties
		}
		if = {
			limit = {
				exists = liege
				liege = {
					NOT = { is_in_list = relevant_parties }
				}
			}
			add_to_list = relevant_parties
		}
		if = {
			limit = {
				exists = court_owner
				court_owner = {
					NOT = { is_in_list = relevant_parties }
				}
			}
			add_to_list = relevant_parties
		}
		every_vassal = {
			limit = {
				NOT = { is_in_list = relevant_parties }
			}
			add_to_list = relevant_parties
		}
		every_relation = {
			type = friend
			limit = {
				NOT = { is_in_list = relevant_parties }
			}
			add_to_list = relevant_parties
		}
		every_relation = {
			type = lover
			limit = {
				NOT = { is_in_list = relevant_parties }
			}
			add_to_list = relevant_parties
		}
		$OWNER$ = {
			every_close_family_member = {
				limit = {
					NOT = { is_in_list = relevant_parties }
				}
				add_to_list = relevant_parties
			}
		}
	}
}

# Main Fascinare effect, change religeon, add mulsa trait, check for infecta virus and add to tally
# Scope should be the target character
fascinare_success_effect = {
	add_trait = mulsa
	set_character_faith = global_var:magister_character.faith
	hidden_effect = { # No take-backs.
		add_character_flag = {
			flag = converted_by_forced_conversion_interaction
			years = 10
		}
	}
	change_global_variable = {
		name = regula_fascinare_tally
		add = 1
	}
	if = {
		limit = { # Scope is the target  ### UPDATE - Add code restricting it to the magister.
			age >= 16
			is_male = no
			$CHARACTER$ = {
				has_trait = magister_trait_group
				faith = {
					controls_holy_site_with_flag = holy_site_regula_virus_flag
				}
			}
		}
		add_trait = regula_virus
		save_scope_as = regula_patient_zero

		global_var:magister_character = {
			create_story = story_regula_infecta_chain
			random_owned_story = {
				limit = {
					story_type = story_regula_infecta_chain
					NOT = { has_variable = patient_zero }
				}
				set_variable = {
					name = patient_zero
					value = scope:regula_patient_zero
				}
				add_to_variable_list = {
					name = new_converts
					target = scope:regula_patient_zero
				}
			}
		}

		trigger_event = {
			id = regula_virus.1000
			days = { 15 60 }
		}
	}
}

# Extra effects if ward, add tally and give Dynasty prestige if the ward is not part of your dynasty, (based on their highest title)
# Scope should be the ward character
ward_enslavement_success_effect = {
	change_global_variable = {
		name = regula_obedience_tally
		add = 1
	}
	if = {
		limit = {
			NOT = { global_var:magister_character.dynasty = dynasty }
		}
		if = {
			limit = {
				is_ruler = yes
			}
			global_var:magister_character.dynasty = {
				add_dynasty_prestige = scope:teen.regula_ward_highest_title_to_dynasty_prestige
			}
		}
	}
}

##################
# HELPER EFFECTS #
##################

# Here we add bonus start progress based on the "closeness" of the target
# It probally wound't take a year to Fascinare your wife who you sleep with ^_^

# We'll start with opinion, then add bonuses/maluses
# then divide by 10 to get bonus progress

# Factors to consider are
# - Opinion status
# - Relationship (marriage/family/friend/rival)
# - Distance to them (in your court/vassal court/foreign court)
# - Intimidation status
# TODO: add more?

# scope:scheme = Fascinare Scheme
# scope:owner = Magister
# scope:target = Fascinare target
add_bonus_fascinare_progress = {

	set_variable = {
		name = fascinare_bonus
		value = 0
	}

	# Opinion check
	scope:target = {
		save_temporary_opinion_value_as = {
			name = opinion_value
			target = scope:owner
		}
	}
	change_variable = {
		name = fascinare_bonus
		add = scope:opinion_value
	}
	# Divide it by 5 as well
	change_variable = {
		name = fascinare_bonus
		divide = 5
	}

	# Same Religion?
	if = {
		limit = {
			scope:target = {has_religion = scope:owner.religion}
		}
		change_variable = {name = fascinare_bonus add = 10}
	}
	else = {
		change_variable = {name = fascinare_bonus subtract = 10}
	}

	# Same Culture?
	if = {
		limit = {
			scope:target = {has_culture = scope:owner.culture}
		}
		change_variable = {name = fascinare_bonus add = 10}
	}

	# Check relationship status
	# Family
	if = {
		limit = {
			scope:target = {is_consort_of = scope:owner}
		}
		change_variable = {name = fascinare_bonus add = 30}
	}
	if = {
		limit = {
			scope:target = {is_close_family_of = scope:owner}
		}
		change_variable = {name = fascinare_bonus add = 20}
	}
	if = {
		limit = {
			OR = {
				scope:target = {is_extended_family_of = scope:owner}
				scope:target.dynasty = scope:owner.dynasty
			}
		}
		change_variable = {name = fascinare_bonus add = 10}
	}

	# Special friend/rival/lover status
	if = {
		limit = {
			scope:target = {has_relation_lover = scope:owner}
		}
		change_variable = {name = fascinare_bonus add = 50}
	}
	if = {	# bruh
		limit = {
			scope:target = {has_relation_soulmate = scope:owner}
		}
		change_variable = {name = fascinare_bonus add = 100}
	}

	if = {
		limit = {
			scope:target = {has_relation_friend = scope:owner}
		}
		change_variable = {name = fascinare_bonus add = 10}
	}
	if = {
		limit = {
			scope:target = {has_relation_best_friend = scope:owner}
		}
		change_variable = {name = fascinare_bonus add = 20}
	}

	if = {
		limit = {
			scope:target = {has_relation_nemesis = scope:owner}
		}
		change_variable = {name = fascinare_bonus subtract = 30}
	}
	if = {
		limit = {
			scope:target = {has_relation_nemesis = scope:owner}
		}
		change_variable = {name = fascinare_bonus subtract = 50}
	}

	# Check Distance
	if = { # These are people in our court/nearby
		limit = {
			scope:target = {
				OR = {
					is_at_same_location = scope:owner
					is_vassal_or_below_of = scope:owner
					is_courtier_of = scope:owner
					is_foreign_court_guest_of = scope:owner
					is_pool_guest_of = scope:owner
				}
			}
		}
		change_variable = {name = fascinare_bonus add = 20}
	}
	else = { # Otherwise they are in a foreign court, gives a malus
		change_variable = {name = fascinare_bonus subtract = 20}
	}

	# Check Intimidation
	if = {
		limit = {
			scope:target = {
				has_dread_level_towards ={
					target = scope:owner
					level = 2
				}
			}
		}
		change_variable = {name = fascinare_bonus add = 20}
	}
	if = {
		limit = {
			scope:target = {
				has_dread_level_towards ={
					target = scope:owner
					level = 1
				}
			}
		}
		change_variable = {name = fascinare_bonus add = 10}
	}

	# Add scheme progress based on fascinare_bonus/10
	# Make sure we aren't out of range by clamping
	clamp_variable = {
		name = fascinare_bonus
		max = 90
		min = 0
	}

	change_variable = {
		name = fascinare_bonus
		divide  = 10
	}

	add_scheme_progress = var:fascinare_bonus

}


code:
regula_forge_inheritance_law_success_effect = {
    add_realm_law_skip_effects = regula_covert_vassal_succession_law
}


code:
regula_initialize_magister_effect = {
	set_global_variable = {
		name = magister_character
		value = root
	}

	# Reset Bloodline tallys if we have single life rule
	if = {
		limit = {
			has_game_rule = regula_bloodline_tally_goals_single_life
		}
		set_global_variable = {
			name = regula_fascinare_tally
			value = 0
		}
		set_global_variable = {
			name = regula_domitans_tally
			value = 0
		}
		set_global_variable = {
			name = regula_contubernalis_tally
			value = 0
		}
		set_global_variable = {
			name = regula_domination_war_tally
			value = 0
		}
		set_global_variable = {
			name = regula_servitude_war_tally
			value = 0
		}
		set_global_variable = {
			name = regula_potestas_queen_tally
			value = 0
		}
		set_global_variable = {
			name = regula_obedience_tally
			value = 0
		}
	}
}

regula_first_secret_conversion = {
	regula_create_placeholder_character_effect = { WHO = root }
	scope:created_character = { save_scope_as = regula_placeholder }
	set_global_variable = {
		name = regula_secret_keeper
		value = scope:regula_placeholder
	}
	# set_global_variable = {
	# 	name = proto_magister_character
	# 	value = root
	# }
	add_secret = {
		type = regula_covert_conversion
		target = global_var:regula_secret_keeper
	}
	global_var:regula_secret_keeper = { # Two can keep a secret, if one is dead.
		death = {
			death_reason = death_vanished
		}
	}
}

regula_secret_conversion = {
	add_secret = {
		type = regula_covert_conversion
		target = global_var:regula_secret_keeper
	}
    add_realm_law_skip_effects = regula_covert_vassal_succession_law
	random_secret = {
		limit = {
			secret_type = regula_covert_conversion
			NOT = { is_known_by = scope:actor }
		}
		reveal_to = scope:actor
	}
}

regula_secret_reboot = {
	add_secret = {
		type = regula_covert_conversion
		target = global_var:regula_secret_keeper
	}
	every_vassal_or_below = {  ### Update: Non-functional
		limit = {
			any_secret = {
				secret_type = regula_covert_conversion
			}
		}
		random_secret = {
			limit = {
				secret_type = regula_covert_conversion
				NOT = { is_known_by = scope:actor }
			}
			reveal_to = scope:actor
		}
		every_courtier_or_guest = {
			limit = {
				any_secret = {
					secret_type = regula_covert_conversion
				}
			}
			random_secret = {
				limit = {
					secret_type = regula_covert_conversion
					NOT = { is_known_by = scope:actor }
				}
				reveal_to = scope:actor
			}
		}
	}
	every_courtier_or_guest = {
		limit = {
			any_secret = {
				secret_type = regula_covert_conversion
			}
		}
		random_secret = {
			limit = {
				secret_type = regula_covert_conversion
				NOT = { is_known_by = scope:actor }
			}
			reveal_to = scope:actor
		}
	}
	# global_var:regula_secret_keeper = {
	# 	random_targeting_secret = {
	# 		limit = {
	# 			secret_type = regula_covert_conversion
	# 			secret_owner = {
	# 				is_male = yes
	# 			}
	# 		}
	# 		save_scope_as = regula_secret
	# 		scope:regula_secret.secret_owner = {
	# 			save_scope_as = proto_magister
	# 		}
	# 	}
	# 	every_targeting_secret = {
	# 		limit = {
	# 		}
	# 		reveal_to = scope:proto_magister
	# 	}
	# }
}

regula_secret_conversion_reveal_effect = {
	every_held_title = {
		limit = {
			tier = tier_county
		}
		set_county_faith = faith:regula_default
	}
	demand_conversion_interaction_effect = yes
	every_vassal_or_below = {
		if = {
			limit = {
				any_secret = {
					secret_type = regula_covert_conversion
				}
			}
			set_character_faith = faith:regula_default
			add_character_modifier = {
				modifier = regula_initial_spellbound
			}
			add_trait = mulsa
			demand_conversion_interaction_effect = yes
			every_held_title = {
				limit = {
					tier = tier_county
				}
				set_county_faith = faith:regula_default
			}
		}
		every_courtier_or_guest = {
			limit = {
				any_secret = {
					secret_type = regula_covert_conversion
				}
			}
			set_character_faith = faith:regula_default
			add_trait = mulsa
			demand_conversion_interaction_effect = yes
			every_held_title = {
				limit = {
					tier = tier_county
				}
				set_county_faith = faith:regula_default
			}
		}
	}
	every_courtier_or_guest = {
		limit = {
			any_secret = {
				secret_type = regula_covert_conversion
			}
		}
		set_character_faith = faith:regula_default
		add_trait = mulsa
		demand_conversion_interaction_effect = yes
		every_held_title = {
			limit = {
				tier = tier_county
			}
			set_county_faith = faith:regula_default
		}
	}
}


code:
#Scripted effects relating to the Instiga Discordia Scheme

#####################################################################
# EFFECT LIST
#####################################################################
# !!! Remember to add all new effects with a short description here !!!

#regula_instiga_discordia_discovered_success_effect
#regula_instiga_discordia_discovered_failure_effect

######################################################################
# EFFECTS
######################################################################

#Sets up the Outcome Roll values
regula_instiga_discordia_discovered_success_effect = {
	scope:target = {
		if = {
			limit = {
				scope:owner = {
					is_spouse_of = scope:target
				}
			}
			add_opinion = {
				target = scope:owner
				modifier = spouse_ill_rumors_opinion
			}
		}
		else = {
			add_opinion = {
				target = scope:owner
				modifier = smeared_opinion
			}
		}
	}
}

regula_instiga_discordia_discovered_failure_effect = {
	scope:target = {
		if = {
			limit = {
				scope:owner = {
					is_spouse_of = scope:target
				}
			}
			add_opinion = {
				target = scope:owner
				modifier = spouse_suspecting_motives_opinion
			}
		}
		else = {
			add_opinion = {
				target = scope:owner
				modifier = ignored_concerns
			}
		}
	}
}


code:
regula_make_paelex_interaction_effect = {
	scope:recipient = {
			# Spouses, if any exist, are also furious.
		if = {
			limit = {
				is_married = yes
			}
			if = {
				limit = {
					NOT = {
						is_consort_of = scope:actor
					}
				}
				every_spouse = {
					show_as_tooltip = {
						if = {   ### Husband understands if they're also Magesterian.
							limit = { is_regula_trigger = no
							}
							add_opinion = {
								target = scope:actor
								modifier = forced_spouse_concubine_marriage_opinion
							}
						}
						divorce = scope:recipient # no additional opinion hit, no scripted effect
					}
					trigger_event = marriage_interaction.0041
				}
			}
		}

		# Stealing concubines pisses the former concubinist off.
		if = {
			limit = {
				is_concubine = yes
				NOT = { is_concubine_of = scope:actor }
			}
			this.concubinist = {
				show_as_tooltip = {
					add_opinion = {
						target = scope:actor
						modifier = stole_concubine_opinion
					}
					remove_concubine = scope:recipient # no additional opinion hit, no scripted effect
				}
				trigger_event = marriage_interaction.0041
			}
		}

		# Break any betrothals which may exist (without additional opinion penalties).
		if = {
			limit = { exists = betrothed }
			betrothed = { trigger_event = marriage_interaction.0041 }
			break_betrothal = betrothed
		}

		# Domitans pregnancy trigger
		hidden_effect = {
			if = {
				limit = {
					is_pregnant = yes
				}
				#REVERT 25
				random = {
					chance = 25
					scope:recipient = {
						trigger_event = regula_paelex_event.1040
					}
				}
			}
		}

		# Iterate the Domitans Tribunal counter, so long as this isn't a claim orba action.
		if = {
			limit = {
				NOT = {
					has_trait = orba
				}
			}
			change_global_variable = {
				name = regula_domitans_tally
				add = 1
			}
		}
	}
}

# Replaces wife.  If they're paelex/domina sticks them in the harem, otherwise gives normal divorce penalty.  No longer requied.
# regula_promote_paelex_divorce_effect = {
# 	every_spouse = { # If the PC has multiple wives then this will hit them all. Probably not an issue.
# 		divorce = scope:actor
# 		if = {
# 			limit = {
# 				OR = {
# 					has_trait = domina
# 					has_trait = paelex
# 				}
# 			}
# 			scope:actor = {
# 				make_concubine = prev
# 			}
# 			if = {
# 				limit = { has_trait = domina }
# 			}
# 			remove_trait = domina
# 			add_trait = paelex
# 			add_opinion = {
# 				modifier = demoted_domina_opinion
# 				target = scope:actor
# 			}
# 		}
# 		else = {
# 			add_opinion = {
# 				modifier = divorced_me_opinion
# 				target = scope:actor
# 			}
# 			hidden_effect = {
# 				if = {
# 					limit = {
# 						has_opinion_modifier = {
# 							modifier = spouse_does_not_believe_in_concubines_opinion
# 							target = scope:actor
# 						}
# 					}
# 					remove_opinion = {
# 						modifier = spouse_does_not_believe_in_concubines_opinion
# 						target = scope:actor
# 					}
# 					add_opinion = {
# 						modifier = spouse_does_not_believe_in_former_concubines_opinion
# 						target = scope:actor
# 					}
# 				}
# 			}
# 		}
# 	}
# }


code:
# Event that allow us to configure the chance for each mutare event occuring
# Depending on our magisters piety level, we have a different chance for each effect
regula_mutare_corpus_mental_boost_effect = {

	# Piety level 0
	if = {
		limit = { global_var:magister_character = { piety_level = 0 }}
		random_list = {
			30 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_1 }
				trigger_event = regula_mutare_corpus_event.0011
			}
			50 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_2 }
				trigger_event = regula_mutare_corpus_event.0012
			}
			20 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_3 }
				trigger_event = regula_mutare_corpus_event.0013
			}
		}
	}

	# Piety level 1
	if = {
		limit = { global_var:magister_character = { piety_level = 1 }}
		random_list = {
			10 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_1 }
				trigger_event = regula_mutare_corpus_event.0011
			}
			20 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_2 }
				trigger_event = regula_mutare_corpus_event.0012
			}
			60 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_3 }
				trigger_event = regula_mutare_corpus_event.0013
			}
			10 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_4 }
				trigger_event = regula_mutare_corpus_event.0014
			}
		}
	}

	# Piety level 2
	if = {
		limit = { global_var:magister_character = { piety_level = 2 }}
		random_list = {
			5 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_1 }
				trigger_event = regula_mutare_corpus_event.0011
			}
			10 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_2 }
				trigger_event = regula_mutare_corpus_event.0012
			}
			50 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_3 }
				trigger_event = regula_mutare_corpus_event.0013
			}
			30 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_4 }
				trigger_event = regula_mutare_corpus_event.0014
			}
			5 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_5 }
				trigger_event = regula_mutare_corpus_event.0015
			}
		}
	}

	# Piety level 3
	if = {
		limit = { global_var:magister_character = { piety_level = 3 }}
		random_list = {
			5 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_2 }
				trigger_event = regula_mutare_corpus_event.0012
			}
			40 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_3 }
				trigger_event = regula_mutare_corpus_event.0013
			}
			40 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_4 }
				trigger_event = regula_mutare_corpus_event.0014
			}
			15 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_5 }
				trigger_event = regula_mutare_corpus_event.0015
			}
		}
	}

	# Piety level 4
	if = {
		limit = { global_var:magister_character = { piety_level = 4 }}
		random_list = {
			60 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_4 }
				trigger_event = regula_mutare_corpus_event.0014
			}
			40 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_5 }
				trigger_event = regula_mutare_corpus_event.0015
			}
		}
	}

	# Piety level 5
	if = {
		limit = { global_var:magister_character = { piety_level = 5 }}
		random_list = {
			20 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_4 }
				trigger_event = regula_mutare_corpus_event.0014
			}
			80 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_5 }
				trigger_event = regula_mutare_corpus_event.0015
			}
		}
	}
}

regula_mutare_corpus_physical_boost_effect = {

	# Piety level 0
	if = {
		limit = { global_var:magister_character = { piety_level = 0 }}
		random_list = {
			30 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_1 }
				trigger_event = regula_mutare_corpus_event.0021
			}
			50 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_2 }
				trigger_event = regula_mutare_corpus_event.0022
			}
			20 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_3 }
				trigger_event = regula_mutare_corpus_event.0023
			}
		}
	}

	# Piety level 1
	if = {
		limit = { global_var:magister_character = { piety_level = 1 }}
		random_list = {
			10 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_1 }
				trigger_event = regula_mutare_corpus_event.0021
			}
			20 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_2 }
				trigger_event = regula_mutare_corpus_event.0022
			}
			60 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_3 }
				trigger_event = regula_mutare_corpus_event.0023
			}
			10 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_4 }
				trigger_event = regula_mutare_corpus_event.0024
			}
		}
	}

	# Piety level 2
	if = {
		limit = { global_var:magister_character = { piety_level = 2 }}
		random_list = {
			5 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_1 }
				trigger_event = regula_mutare_corpus_event.0021
			}
			10 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_2 }
				trigger_event = regula_mutare_corpus_event.0022
			}
			50 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_3 }
				trigger_event = regula_mutare_corpus_event.0023
			}
			30 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_4 }
				trigger_event = regula_mutare_corpus_event.0024
			}
			5 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_5 }
				trigger_event = regula_mutare_corpus_event.0025
			}
		}
	}

	# Piety level 3
	if = {
		limit = { global_var:magister_character = { piety_level = 3 }}
		random_list = {
			5 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_2 }
				trigger_event = regula_mutare_corpus_event.0022
			}
			40 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_3 }
				trigger_event = regula_mutare_corpus_event.0023
			}
			40 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_4 }
				trigger_event = regula_mutare_corpus_event.0024
			}
			15 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_5 }
				trigger_event = regula_mutare_corpus_event.0025
			}
		}
	}

	# Piety level 4
	if = {
		limit = { global_var:magister_character = { piety_level = 4 }}
		random_list = {
			60 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_4 }
				trigger_event = regula_mutare_corpus_event.0024
			}
			40 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_5 }
				trigger_event = regula_mutare_corpus_event.0025
			}
		}
	}

	# Piety level 5
	if = {
		limit = { global_var:magister_character = { piety_level = 5 }}
		random_list = {
			20 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_4 }
				trigger_event = regula_mutare_corpus_event.0024
			}
			80 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_5 }
				trigger_event = regula_mutare_corpus_event.0025
			}
		}
	}
}

regula_mutare_corpus_sexual_boost_effect = {

	# Piety level 0
	if = {
		limit = { global_var:magister_character = { piety_level = 0 }}
		random_list = {
			30 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_1 }
				trigger_event = regula_mutare_corpus_event.0031
			}
			50 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_2 }
				trigger_event = regula_mutare_corpus_event.0032
			}
			20 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_3 }
				trigger_event = regula_mutare_corpus_event.0033
			}
		}
	}

	# Piety level 1
	if = {
		limit = { global_var:magister_character = { piety_level = 1 }}
		random_list = {
			10 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_1 }
				trigger_event = regula_mutare_corpus_event.0031
			}
			20 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_2 }
				trigger_event = regula_mutare_corpus_event.0032
			}
			60 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_3 }
				trigger_event = regula_mutare_corpus_event.0033
			}
			10 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_4 }
				trigger_event = regula_mutare_corpus_event.0034
			}
		}
	}

	# Piety level 2
	if = {
		limit = { global_var:magister_character = { piety_level = 2 }}
		random_list = {
			5 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_1 }
				trigger_event = regula_mutare_corpus_event.0031
			}
			10 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_2 }
				trigger_event = regula_mutare_corpus_event.0032
			}
			50 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_3 }
				trigger_event = regula_mutare_corpus_event.0033
			}
			30 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_4 }
				trigger_event = regula_mutare_corpus_event.0034
			}
			5 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_5 }
				trigger_event = regula_mutare_corpus_event.0035
			}
		}
	}

	# Piety level 3
	if = {
		limit = { global_var:magister_character = { piety_level = 3 }}
		random_list = {
			5 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_2 }
				trigger_event = regula_mutare_corpus_event.0032
			}
			40 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_3 }
				trigger_event = regula_mutare_corpus_event.0033
			}
			40 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_4 }
				trigger_event = regula_mutare_corpus_event.0034
			}
			15 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_5 }
				trigger_event = regula_mutare_corpus_event.0035
			}
		}
	}

	# Piety level 4
	if = {
		limit = { global_var:magister_character = { piety_level = 4 }}
		random_list = {
			60 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_4 }
				trigger_event = regula_mutare_corpus_event.0034
			}
			40 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_5 }
				trigger_event = regula_mutare_corpus_event.0035
			}
		}
	}

	# Piety level 5
	if = {
		limit = { global_var:magister_character = { piety_level = 5 }}
		random_list = {
			20 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_4 }
				trigger_event = regula_mutare_corpus_event.0034
			}
			80 = {
				custom_description_no_bullet = { text = regula_mutare_corpus_outcome_5 }
				trigger_event = regula_mutare_corpus_event.0035
			}
		}
	}
}

# Helper effects for Mutare Corpus

# regula_mutare_corpus_repair_mind_single 				- Repairs a single bad mental trait
# regula_mutare_corpus_repair_physical_single 				- Repairs a single bad physical trait
# regula_mutare_corpus_give_good_mental_trait 			- Gives a random good mental trait
# regula_rank_up_intelligence_trait 					- Increases mental trait by
# regula_mutare_corpus_give_good_physical_trait_effect	= Gives a random good physical trait
# increase_wounds_effect
# rank_up_education_effect
# carn_increase_beauty_one_step_effect
# carn_decrease_beauty_one_step_effect
# carn_increase_intellect_one_step_effect
# carn_decrease_intellect_one_step_effect
# carn_increase_physique_one_step_effect
# carn_decrease_physique_one_step_effect
# carn_remove_random_negative_congenital_trait_effect
# carn_remove_all_negative_congenital_traits_effect
# carn_heal_wounds_one_step_effect
# carn_remove_all_wounds_effect
# carn_remove_random_minor_disfigurement_effect
# carn_remove_all_minor_disfigurements_effect
# carn_remove_random_major_disfigurement_effect
# carn_remove_all_major_disfigurements_effect
# carn_recover_from_all_diseases_effect

# Mental Effects
regula_mutare_corpus_repair_mind_single_effect = {
	random_list = {
		1 = {
			trigger = { has_trait = depressed_1 }
			remove_trait = depressed_1
		}
		1 = {
			trigger = { has_trait = depressed_genetic }
			remove_trait = depressed_genetic
		}
		1 = {
			trigger = { has_trait = lunatic_1 }
			remove_trait = lunatic_1
		}
		1 = {
			trigger = { has_trait = lunatic_genetic }
			remove_trait = lunatic_genetic
		}
		1 = {
			trigger = { has_trait = possessed_1 }
			remove_trait = possessed_1
		}
		1 = {
			trigger = { has_trait = possessed_genetic }
			remove_trait = possessed_genetic
		}
		1 = {
			trigger = { has_trait = drunkard}
			remove_trait = drunkard
		}
		1 = {
			trigger = { has_trait =  hashishiyah }
			remove_trait = hashishiyah
		}
		1 = {
			trigger = { has_trait = reclusive }
			remove_trait = reclusive
		}
		1 = {
			trigger = { has_trait = irritable }
			remove_trait = irritable
		}
		1 = {
			trigger = { has_trait = flagellant }
			remove_trait = flagellant
		}
		1 = {
			trigger = { has_trait = profligate }
			remove_trait = profligate
		}
		1 = {
			trigger = { has_trait = contrite }
			remove_trait = contrite
		}
		1 = {
			trigger = { has_trait = inappetetic }
			remove_trait = inappetetic
		}
		1 = {
			trigger = { has_trait = rakish }
			remove_trait = rakish
		}
	}
}

regula_mutare_corpus_give_good_mental_trait_effect = {
	random_list = {
		1 = {
			trigger = { NOT = { has_trait = journaller } }
			add_trait = journaller
		}
		1 = {
			trigger = { NOT = { has_trait = confider } }
			add_trait = confider
		}
		1 = {
			trigger = { NOT = { has_trait = shrewd } }
			add_trait = shrewd
		}
		1 = {
			trigger = { NOT = { has_trait = loyal } }
			add_trait = loyal
		}
		1 = {
			trigger = { NOT = { has_trait = athletic } }
			add_trait = athletic # This is technically a "stress" trait, so adding it to this list
		}
		1 = {
			trigger = { NOT = { has_trait = physician_1 } }
			add_trait = physician_1
		}
		1 = {
			trigger = { has_trait = lifestyle_physician }
			change_trait_rank = {
				trait = lifestyle_physician
				rank = 1
			}
		}
	}
}

regula_rank_up_intelligence_trait_effect = {
	if = {
		limit = { NOT= { has_trait = intellect_good_3 } }
		if = {
			limit = { has_trait = intellect_good }
			change_trait_rank = {
				trait = intellect_good
				rank = 1
			}
		}
		else_if = {
			limit = { has_trait = intellect_bad }
			change_trait_rank = {
				trait = intellect_bad
				rank = -1
			}
		}
		else = {
			add_trait = intellect_good_1
		}
	}
}

# Physical Effects
regula_mutare_corpus_repair_physical_single_effect = {
	random_list = {
		1 = {
			if = {
				limit = { has_trait = wounded }
				change_trait_rank = {
					trait = wounded
					rank = -1
				}
			}
		}
		1 = {
			trigger = { has_trait = scarred }
			remove_trait = scarred
		}
		1 = {
			trigger = { has_trait = one_eyed }
			remove_trait = one_eyed
		}
		1 = {
			trigger = { has_trait = one_legged }
			remove_trait = one_legged
		}
		1 = {
			trigger = { has_trait = spindly }
			remove_trait = spindly
		}
		1 = {
			trigger = { has_trait = wheezing}
			remove_trait = wheezing
		}
		1 = {
			trigger = { has_trait = blind }
			remove_trait = blind
		}
		1 = {
			trigger = { has_trait = hunchbacked }
			remove_trait = hunchbacked
		}
		1 = {
			trigger = { has_trait = disfigured }
			remove_trait = disfigured
		}
		1 = {
			trigger = { has_trait = maimed}
			remove_trait = maimed
		}
		1 = {
			trigger = { has_trait = leper}
			remove_trait = leper
		}
	}
}

regula_mutare_corpus_give_good_physical_trait_effect = {
	random_list = {
		1 = {
			trigger = { has_trait = lifestyle_blademaster }
			change_trait_rank = {
				trait = lifestyle_blademaster
				rank = 1
			}
		}
		1 = {
			trigger = { has_trait = lifestyle_hunter }
			change_trait_rank = {
				trait = lifestyle_hunter
				rank = 1
			}
		}
		1 = {
			trigger = { NOT = { has_trait = blademaster_1 } }
			add_trait = blademaster_1
		}
		1 = {
			trigger = { NOT = { has_trait = hunter_1 } }
			add_trait = hunter_1
		}
		1 = {
			trigger = { NOT = { has_trait = strong } }
			add_trait = strong
		}
	}
}

regula_rank_up_physical_trait_effect = {
	if = {
		limit = { NOT= { has_trait = physique_good_3 } }
		if = {
			limit = { has_trait = physique_good }
			change_trait_rank = {
				trait = physique_good
				rank = 1
			}
		}
		else_if = {
			limit = { has_trait = physique_bad }
			change_trait_rank = {
				trait = physique_bad
				rank = -1
			}
		}
		else = {
			add_trait = physique_good_1
		}
	}
}

# Sexual/Body Effects
regula_mutare_corpus_cure_disease_single_effect = {
	random_list = {
		1 = {
			trigger = { has_trait = infirm }
			remove_trait = infirm
		}
		1 = {
			trigger = { has_trait = incapable }
			remove_trait = incapable
		}
		1 = {
			trigger = { has_trait = gout_ridden }
			remove_trait = gout_ridden
		}
		1 = {
			trigger = { has_trait = consumption }
			remove_trait = consumption
		}
		1 = {
			trigger = { has_trait = typhus}
			remove_trait = typhus
		}
		1 = {
			trigger = { has_trait = bubonic_plague }
			remove_trait = bubonic_plague
		}
		1 = {
			trigger = { has_trait = smallpox }
			remove_trait = smallpox
		}
		1 = {
			trigger = { has_trait = ill }
			remove_trait = ill
		}
		1 = {
			trigger = { has_trait = pneumonic}
			remove_trait = pneumonic
		}
		1 = {
			trigger = { has_trait = great_pox}
			remove_trait = great_pox
		}
		1 = {
			trigger = { has_trait = early_great_pox}
			remove_trait = early_great_pox
		}
		1 = {
			trigger = { has_trait = lovers_pox}
			remove_trait = lovers_pox
		}
		1 = {
			trigger = { has_trait = leper}
			remove_trait = leper
		}
	}
}

regula_rank_up_beauty_trait_effect = {
	if = {
		limit = { NOT= { has_trait = beauty_good_3 } }
		if = {
			limit = { has_trait = beauty_good }
			change_trait_rank = {
				trait = beauty_good
				rank = 1
			}
		}
		else_if = {
			limit = { has_trait = beauty_bad }
			change_trait_rank = {
				trait = beauty_bad
				rank = -1
			}
		}
		else = {
			add_trait = beauty_good_1
		}
	}
}


code:
regula_bdsm_session_effect = {
	$SUBMISSIVE$ = {
		if = {
			limit = {
				OR = {
					has_trait = humble
					has_trait = craven
					has_trait = flagellant
					has_trait = contrite
					has_trait = deviant ### UPDATE - Add carn traits?
				}
			}
			add_opinion = {
				target = $DOMINANT$
				modifier = regula_bdsm_positive
			}
			add_stress = minor_stress_loss
		}
		else = {
			add_opinion = {
				target = $DOMINANT$
				modifier = regula_bdsm_negative
			}
			add_stress = medium_stress_gain
		}
	}
}


code:
#Scripted effects relating to the Rapta Maritus Scheme

#####################################################################
# EFFECT LIST
#####################################################################
# !!! Remember to add all new effects with a short description here !!!

#rapta_maritus_outcome_roll_effect
#rapta_maritus_success_effect
#rapta_maritus_failure_effect

######################################################################
# EFFECTS
######################################################################

#Sets up the Outcome Roll values
rapta_maritus_outcome_roll_setup_effect = {
	#Discovery Roll setup
	save_scope_value_as = {
		name = discovery_chance
		value = {
			value = 100
			subtract = scope:scheme.scheme_secrecy
		}
	}
}

#Roll to check whether a rapta_maritus Scheme is a success or a failure, and whether the owner is discovered
rapta_maritus_outcome_roll_effect = {
	#SUCCESS ROLL
	random = {
		chance = scope:scheme.scheme_success_chance
		custom_tooltip = rapta_maritus_successful_roll_tt
		save_scope_value_as = {
			name = scheme_successful
			value = yes
		}
	}
	### End Success roll
	#DISCOVERY ROLL
	custom_label = rapta_maritus_success_discovery_tt
	random = {
		chance = scope:discovery_chance
		custom_tooltip = rapta_maritus_become_discovered_roll_tt
		save_scope_value_as = {
			name = scheme_discovered
			value = yes
		}
	}
	### End Discovery roll

	hidden_effect = {
		#FIRE CORRECT ON ACTIONS
		if = {
			limit = {
				exists = scope:scheme_successful
			}
			trigger_event = {
				on_action = rapta_maritus_succeeded
			}
		}
		else = {
			trigger_event = {
				on_action = rapta_maritus_failed
			}
		}
	}
}

#Used in the immediate aftermath of the Scheme Owner's success event
rapta_maritus_success_effect = {
	scope:target = {
		add_character_flag = {
			flag = was_abducted_block_notification_event
			days = 2
		}
		trigger_event = rapta_maritus_outcome.5001
	}

	global_var:magister_character = {
		trigger_event = rapta_maritus_outcome.7001 # Letter event for magister.
	}

	hard_imprison_character_effect = {  # This decides where the kidnapped person goes.  May need a saved scope:magister.
		TARGET = scope:target
		IMPRISONER = global_var:magister_character
	}
}

rapta_maritus_failure_effect = {
	#Add Watchful Modifier to the target
	scope:target = {
		show_as_tooltip = {
			add_character_modifier = {
				modifier = watchful_modifier
				days = watchful_modifier_duration
			}
		}
	}

	global_var:magister_character = {
		trigger_event = rapta_maritus_outcome.7002 # Letter event for magister.
	}

	scope:target = {
		trigger_event = rapta_maritus_outcome.5002
	}
}

successful_rapta_maritus_outcome_event_option_effect = {
	if = {
		limit = {
			faith = {
				has_doctrine_parameter = piety_gain_from_successful_intrigue_schemes
			}
		}
		add_piety = minor_piety_gain
	}

	scope:scheme = {
		end_scheme = yes
	}
}


code:
regula_faith_swap = {
	global_var:magister_character = {
		if = {
			limit = {
				is_independent_ruler = no
			}
			every_held_title = {
				limit = {
					tier = tier_county
					faith.religion = religion:regula_religion
				}
				set_county_faith = global_var:magister_character.faith
			}
			every_liege_or_above = { # Yes, this will double-dip.
				if = {
					limit = {
						faith.religion = religion:regula_religion
					}
					set_character_faith = global_var:magister_character.faith
				}
				every_vassal_or_below = {
					every_courtier_or_guest = {
						limit = {
							faith.religion = religion:regula_religion
						}
						set_character_faith = global_var:magister_character.faith
					}
					if = {
						limit = {
							faith.religion = religion:regula_religion
						}
						set_character_faith = global_var:magister_character.faith
					}
					every_held_title = {
						limit = {
							tier = tier_county
							faith.religion = religion:regula_religion
						}
						set_county_faith = global_var:magister_character.faith
					}
				}
				every_courtier_or_guest = {
					limit = {
						faith.religion = religion:regula_religion
					}
					set_character_faith = global_var:magister_character.faith
				}
				if = {
					limit = {
						faith.religion = religion:regula_religion
					}
					set_character_faith = global_var:magister_character.faith
				}
				every_held_title = {
					limit = {
						tier = tier_county
						faith.religion = religion:regula_religion
					}
					set_county_faith = global_var:magister_character.faith
				}
			}
		}
		else = {
			every_held_title = {
				limit = {
					tier = tier_county
					faith.religion = religion:regula_religion
				}
				set_county_faith = global_var:magister_character.faith
			}
			every_vassal_or_below = {
				if = {
					limit = {
						faith.religion = religion:regula_religion
					}
					set_character_faith = global_var:magister_character.faith
				}
				every_courtier_or_guest = {
					limit = {
						faith.religion = religion:regula_religion
					}
					set_character_faith = global_var:magister_character.faith
				}
				every_held_title = {
					limit = {
						tier = tier_county
						faith.religion = religion:regula_religion
					}
					set_county_faith = global_var:magister_character.faith
				}
			}
			every_courtier_or_guest = {
				limit = {
					faith.religion = religion:regula_religion
				}
				set_character_faith = global_var:magister_character.faith
			}
		}
	}
}

regula_holy_site_deactivation = {
	# Continent Holy Sites
	deactivate_holy_site = reg_jerusalem
	deactivate_holy_site = reg_brescia
	deactivate_holy_site = reg_alexandria
	deactivate_holy_site = reg_carthage
	deactivate_holy_site = reg_lesbos
	deactivate_holy_site = reg_rome
	deactivate_holy_site = reg_toulouse
	deactivate_holy_site = reg_krakow
	deactivate_holy_site = reg_gotland
	deactivate_holy_site = reg_zeeland
	deactivate_holy_site = reg_baghdad
	deactivate_holy_site = reg_veria
	deactivate_holy_site = reg_tyre
	deactivate_holy_site = reg_yazd
	deactivate_holy_site = reg_lahur
	deactivate_holy_site = reg_djenne
	deactivate_holy_site = reg_wenyon
	deactivate_holy_site = reg_daura
	deactivate_holy_site = reg_jara
	deactivate_holy_site = reg_oyo

	# Empire Holy Sites
	# Britain
	deactivate_holy_site = reg_britain_dublin
	deactivate_holy_site = reg_britain_glamorgan
	deactivate_holy_site = reg_britain_gowrie
	deactivate_holy_site = reg_britain_middlesex
	deactivate_holy_site = reg_britain_west_riding
}


code:
regula_titulum_novis_interaction_effect = {
    # Check how much piety it will cost
    scope:actor = {
        add_piety = {
            subtract = scope:target.regula_titulum_novis_cost
        }
	}
}


code:
contract_regula_virus_effect = {
	save_temporary_scope_as = newest_convert
	set_character_faith = global_var:magister_character.faith
	global_var:magister_character = {
		random_owned_story = {
			limit = {
				story_type = story_regula_infecta_chain
				var:patient_zero = scope:regula_patient_zero
			}
			add_to_variable_list = {
				name = new_converts
				target = scope:newest_convert
			}
		}
	}
	if = {
		limit = {
			is_male = no
			age >= 16
			NOT = { has_trait = devoted_trait_group }
		}
		add_trait = mulsa
		add_trait = regula_virus
	}
}

contract_regula_virus_relation_effect = {
	save_temporary_scope_as = newest_convert
	set_character_faith = global_var:magister_character.faith
	global_var:magister_character = {
		random_owned_story = {
			limit = {
				story_type = story_regula_infecta_chain
				var:patient_zero = scope:regula_patient_zero
			}
			add_to_variable_list = {
				name = new_converts
				target = scope:newest_convert
			}
		}
	}
	if = {
		limit = {
			is_male = no
			age >= 16
			NOT = { has_trait = devoted_trait_group }
		}
		add_trait = mulsa
	}
}

create_regula_virus_list_effect = {
	$CONTAGION_COURT_OWNER$ = {
		if = {
			limit = {
				NOR = {
					this = $SICK_CHARACTER$
					is_in_list = contagion_list
				}
				age >= 16
				is_male = no
			}
			add_to_temporary_list = contagion_list
		}
		every_courtier_or_guest = {
			limit = {
				NOR = {
					this = $SICK_CHARACTER$
					is_in_list = contagion_list
				}
				age >= 16
				is_male = no
			}
			add_to_temporary_list = contagion_list
		}
		every_vassal = {
			limit = {
				NOR = {
					this = $SICK_CHARACTER$
					is_in_list = contagion_list
				}
				age >= 16
				is_male = no
			}
			add_to_temporary_list = contagion_list
		}
	}
	$SICK_CHARACTER$ = {
		every_scheme = {
			limit = {
				friendly_scheme_trigger = yes
				is_male = no
				NOT = { scheme_target = {
					is_in_list = contagion_list
				} }
			}
			scheme_target = { add_to_temporary_list = contagion_list }
		}
		every_targeting_scheme = {
			limit = {
				friendly_scheme_trigger = yes
				is_male = no
				NOT = { scheme_owner = {
					is_in_list = contagion_list
				} }
			}
			scheme_owner = { add_to_temporary_list = contagion_list }
		}
	}
}


code:
revela_secretum_assign_appropriate_type_effect = {
	debug_log = "Running Fabricate Hook's Assign Appropriate Hook effect"
	assert_if = {
		limit = {
			scope:owner = {
				has_strong_hook = scope:revela_target
			}
		}
		text = "The Magister already has a Strong Hook on the Target! Why is this effect running?"
	}
	assert_if = {
		limit = {
			scope:revela_target = { is_ruler = yes }
			NOT = { exists = scope:strong_hook_against_ruler }
			scope:owner = { has_hook = scope:revela_target }
		}
		text = "The Magister has a hook against a ruler target, and the strong hook roll failed. Something has gone wrong to get here!"
	}

	save_scope_value_as = {
		name = hook_type
		value = flag:$TYPE$
	}
	# Go through the Hook types, and assign the appropriate one
	if = {
		limit = {
			scope:hook_type = flag:loyalty_hook
		}
		# Check rulers first (to give weaker Hooks)
		if = {
			limit = {
				scope:revela_target = { is_ruler = yes }
				NOT = { exists = scope:strong_hook_against_ruler } # Set by on_ready in the Scheme
			}
			add_hook = {
				type = indebted_hook
				target = scope:revela_target
			}
		}
		else = {
			add_hook = {
				type = loyalty_hook
				target = scope:revela_target
			}
		}
	}
	else_if = {
		limit = {
			scope:hook_type = flag:fabrication_hook
		}
		# Check rulers first (to give weaker Hooks)
		if = {
			limit = {
				scope:revela_target = { is_ruler = yes }
				NOT = { exists = scope:strong_hook_against_ruler } # Set by on_ready in the Scheme
			}
			add_hook = {
				type = manipulation_hook
				target = scope:revela_target
			}
			scope:revela_target = {
				add_opinion = {
					target = scope:revela_recipient
					modifier = fabricate_hook_threatened_me
					years = 5
				}
			}
		}
		else = {
			add_hook = {
				type = fabrication_hook
				target = scope:revela_target
			}
			scope:revela_target = {
				add_opinion = {
					target = scope:revela_recipient
					modifier = fabricate_hook_threatened_me
					years = 5
				}
			}
		}
	}
	else_if = {
		limit = {
			scope:hook_type = flag:life_threat_hook
		}
		debug_log = "Attempting to set life threat hook"
		# Check rulers first (to give weaker Hooks)
		if = {
			limit = {
				scope:revela_target = { is_ruler = yes }
				NOT = { exists = scope:strong_hook_against_ruler } # Set by on_ready in the Scheme
			}
			debug_log = "Trying to set regular threat hook against ruler"
			add_hook = {
				type = threat_hook
				target = scope:revela_target
			}
			scope:revela_target = {
				add_opinion = {
					target = scope:revela_recipient
					modifier = fabricate_hook_threatened_me
					years = 10
				}
			}
		}
		else = {
			if = {
				limit = { exists = scope:strong_hook_against_ruler }
				debug_log = "Trying to set life_threat_hook against a ruler"
			}
			else = {
				debug_log = "Setting life_threat_hook against non-ruler"
				assert_if = {
					limit = { scope:revela_target = { is_ruler = yes } }
					text = "There is no ruler strong hook roll, but we're still trying to set a life_threat_hook on them!"
				}
			}
			add_hook = {
				type = life_threat_hook
				target = scope:revela_target
			}
			scope:revela_target = {
				add_opinion = {
					target = scope:revela_recipient
					modifier = fabricate_hook_threatened_me
					years = 10
				}
			}
		}
	}
	# Error catch
	else = {
		assert_if = {
			limit = { always = yes } # It should never get here
			text = "Fabricate Hook assignment effect did not have a Hook type set when trying to assign a Hook!"
		}
	}
}

revela_secretum_block_scheme_owner_from_further_schemes_effect = {
	add_opinion = {
		target = scope:revela_recipient
		modifier = fabricated_hook_against_court_opinion
		years = 5
	}
	custom_tooltip = fabricate_hook_i_may_not_fabricate_again
}

revela_secretum_discovery_effect = {
	show_as_tooltip = { # Target dislikes you
		custom_tooltip = fabricate_hook_i_may_not_fabricate_again
		scope:revela_target = {
			add_opinion = {
				target = scope:revela_recipient
				modifier = fabricated_hook_against_court_opinion
				years = 5
			}
		}
		if = {
			limit = {
				exists = scope:revela_target.court_owner
				NOT = { scope:revela_target = scope:revela_target.court_owner }
			}
			scope:revela_target.court_owner = {
				add_opinion = {
					target = scope:revela_recipient
					modifier = fabricated_hook_against_court_opinion
					years = 5
				}
			}
		}
	}

	hidden_effect  = {
		# Set a blocker for Fabricating further Hooks against Court in question
		if = {
			limit = {
				exists = scope:revela_target.court_owner
			}
			scope:revela_target.court_owner = {
				send_interface_message = {
					type = revela_secretum_blocked
					left_icon = scope:revela_recipient

					revela_secretum_block_scheme_owner_from_further_schemes_effect = yes
				}
			}
		}
		if = {
			limit = {
				OR = {
					NOT = { exists = scope:target.court_owner }
					NOT = { scope:target.court_owner = scope:target }
				}
			}
			scope:target = {
				add_opinion = {
					target = scope:owner
					modifier = fabricated_hook_against_court_opinion
					years = 5
				}
			}
		}
	}
}

#Used in event options
revela_secretum_success_effect = {
	revela_secretum_assign_appropriate_type_effect = {
		TYPE = $TYPE$ # Sets the correct hook type to give
	}
}