﻿# This is a fork of the `Extended Marriage Doctrines` fork of `00_marriage_interaction_effects.txt`
# implementation of `update_active_consort_opinion_effect` updated to avoid directly depending on
# doctrine & tradition names.
# Credit to kalanyr

# In RM, I've added some checks for devoted triggers, for example for concubinage acceptance
# A Famuli is ok with being made into a concubine! (even by "force")

marriage_interaction_on_accept_effect = {
	#Use hook
	if = {
		limit = { always = scope: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
				}
			}
		}
	}
	
	#Legitimacy loss for marrying a lowborn
	scope:actor = {
		if = {
			limit = {
				is_valid_for_legitimacy_change = yes
				NOT = { has_variable = lowborn_legitimacy_cooldown }
				this = scope:secondary_actor # We check that you're marrying them yourself
				scope:secondary_recipient = { is_lowborn = yes }
			}
			if = {
				limit = { is_ai = no }
				send_interface_toast = {
					type = event_toast_effect_good
					title = married_lowborn_toast
					left_icon = scope:actor
					add_legitimacy = {
						value = medium_legitimacy_loss
						multiply = scope:actor.primary_title.tier
					}
				}
			}
			else = { # To avoid destroying polygamy AIs
				send_interface_toast = {
					type = event_toast_effect_good
					title = married_lowborn_toast
					left_icon = scope:actor
					add_legitimacy = { value = minor_legitimacy_loss }
				}
				hidden_effect = {
					set_variable = {
						name = lowborn_legitimacy_cooldown
						years = 10
					}
				}
			}
		}
	}

	#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
			}
		}
	}
	# A Grand Wedding was promised
	if = {
		limit = {
			scope:grand_wedding_promise = yes
		}
		set_grand_wedding_betrothal_variables = {
			SPOUSE_1 = scope:secondary_actor
			SPOUSE_2 = scope:secondary_recipient
			HOST = scope:actor
			PROMISEE = scope:recipient
		}
	}

	#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 }
							scope:secondary_actor = { has_been_promised_grand_wedding = yes }
							scope:secondary_recipient = { has_been_promised_grand_wedding = yes }
						}
					}
					send_interface_toast = {
						type = event_toast_effect_good
						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 = {
						type = event_toast_effect_good
						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 = {
							accepts_polygamy = yes
							accepts_concubinage = yes
						}
					}

					#... 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 = {
									accepts_polygamy = yes
									accepts_concubinage = yes
								}
							}
							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
						}
					}
				}
			}
		}
		# Courtly vassals like grand weddings
		scope:secondary_actor = {
			if = {
				limit = {
					any_vassal = {
						has_vassal_stance = courtly
					}
				}
				if = {
					limit = {
						has_been_promised_grand_wedding = yes
					}
					every_vassal = {
						limit = {
							has_vassal_stance = courtly
						}
						add_opinion = {
							target = scope:secondary_actor
							modifier = prestigious_wedding_opinion
						}
					}
				}
			}
		}
		scope:secondary_recipient = {
			if = {
				limit = {
					any_vassal = {
						has_vassal_stance = courtly
					}
				}
				if = {
					limit = {
						has_been_promised_grand_wedding = yes
					}
					every_vassal = {
						limit = {
							has_vassal_stance = courtly
						}
						add_opinion = {
							target = scope:secondary_recipient
							modifier = prestigious_wedding_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
		}
	}

	# Struggle parameters

	#interfaith wedding costs piety
	if = {
		limit = {
			scope:secondary_actor = {
				NOT = { faith = scope:secondary_recipient.faith }
			}
			scope:actor ={
				any_character_struggle = {
					has_struggle_phase_parameter = interfaith_marriages_between_involved_characters_costs_piety
				}
			}
		}
		scope:actor = {
			add_piety = major_piety_loss
		}
	}
	#interfaith wedding provides piety
	if = {
		limit = {
			scope:secondary_actor = {
				NOT = { faith = scope:secondary_recipient.faith }
			}
			scope:actor ={
				any_character_struggle = {
					has_struggle_phase_parameter = interfaith_marriages_between_involved_characters_gives_piety
				}
			}
		}
		scope:actor = {
			add_piety = medium_piety_gain
		}
	}

	#other stuff 
	#Khurramites and Mazdakists gain piety from marrying lowborn
	if = {
		limit = {
			scope:secondary_recipient = {
				is_lowborn = yes 
			}
			scope:actor.faith = {
				has_doctrine = tenet_communal_possessions 
			}
		}
		scope:actor = {
			add_piety = medium_piety_gain
		}
	}
}

concubine_on_accept_effect = {
	scope:recipient = {
		trigger_event = marriage_interaction.0040
		if = {
			# Victim is angry for being forced into concubinage.
			limit = {
				OR = {
					has_trait = celibate
					is_imprisoned = yes
				}
			}
			release_from_prison = yes

			# Regula Devoted don't mind being "forced" into concubinage
			if = {
				limit = {
					is_regula_devoted_trigger = no
				}
				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.
			# But not if they are Magi
			every_close_family_member = {
				if = {
					limit = { 
						NOT = { this = scope:actor } # Don't hate yourself.
						is_regula_trigger = no
					}
					add_opinion = {
						target = scope:actor
						modifier = forced_family_concubine_marriage_opinion
					}
					hidden_effect = {
						send_interface_message = {
							type = msg_family_became_concubine
							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 = { accepts_concubinage = yes }
								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
						}
					}
				}
			}

			# Not up to them anymore...
			if = {
				limit = { has_trait = celibate }
				remove_trait = celibate
			}			
					
			# 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_toast_effect_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 = msg_family_became_concubine
							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 = { accepts_concubinage = yes }
								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_toast_effect_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_toast_effect_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
						}
					}
				}
			}
		}
	}
}

# 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 = { accepts_concubinage = yes }
			}
			if = {
				# Harshest opinion penalty if they are a concubine but believe in monogamy
				limit = {
					accepts_concubinage = no
					accepts_polygamy = no
				}
				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 = {
					accepts_polygamy = yes
				}
				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 = tenet_polyamory }}
			}
			if = {
				limit = {
					$PARTNER$ = { any_concubine = { count >= 1 }}
					NOT = { accepts_concubinage = yes }
				}
				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
				}
			}
			if = {
				limit = {
					$PARTNER$ = { any_spouse = { count >= 2 }}
					accepts_polygamy = no
				}
				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.
				accepts_concubinage = yes
			}
		}
		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/Culture has changed to permit us being a concubine.
				accepts_concubinage = yes
			}
		}
		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/Culture has changed to permit the relationship.
				accepts_concubinage = yes
				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/Culture has changed to permit the relationship.
				#accepts_concubinage = yes
				accepts_polygamy = 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$
		}
	}
}