#Scripted effects relating to Murder Schemes

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

#abduct_outcome_roll_effect
#abduct_success_effect
#abduct_failure_effect

######################################################################
# EFFECTS
######################################a################################

elope_opinion_list_builder_effect = {
	$OWNER$ = {
		every_spouse = {
			limit = { NOT = { this = $TARGET$ } }
			add_to_temporary_list = $LIST_NAME$
		}
	}
	$TARGET$ = {
		every_spouse = {
			limit = { NOT = { this = $OWNER$ } }
			add_to_temporary_list = $LIST_NAME$
		}
		liege = {
			if = {
				limit = {
					NOR = {
						is_in_list = $LIST_NAME$
						this = $OWNER$
					}
				}
				add_to_temporary_list = $LIST_NAME$
			}
		}
	}
}

elope_success_effect = {
	elope_opinion_list_builder_effect = { OWNER = $OWNER$ TARGET = $TARGET$ LIST_NAME = elope_opinion_list }

	#Because this can be used in events or interaction window, we save root_scope here
	if = {
		limit = { exists = scope:actor }
		scope:actor = { save_temporary_scope_as = root_scope }
	}
	else = {
		root = { save_temporary_scope_as = root_scope }
	}

	#Target prisoner release
	$TARGET$ = {
		if = {
			limit = { is_imprisoned = yes }
			release_from_prison = yes
		}
	}

	$OWNER$ = {
		#Tooltip for marriage
		show_as_tooltip = {
			if = {
				limit = { is_female = yes }
				marry_matrilineal_if_possible = { CHARACTER = $TARGET$ }
			}
			else = {
				marry = $TARGET$
			}
		}

		#Owner's personal consequences (hidden or not), divorce happens here
		if = {
			limit = {
				scope:root_scope = $OWNER$
			}	
			elope_success_personal_consequences_effect = { CHARACTER = $OWNER$ }
		}
		else = {
			hidden_effect = { elope_success_personal_consequences_effect = { CHARACTER = $OWNER$ } }
		}
	}

	#Opinions
	ordered_in_list = {
		list = elope_opinion_list
		max = 10 #To make sure we include all
		check_range_bounds = no
		order_by = ai_vengefulness

		#Towards owner
		if = {
			limit = {
				OR = { #Owner wants to see everything, root wants to see their own
					scope:root_scope = $OWNER$
					scope:root_scope = this
				}
			}
			elope_success_opinion_rivalry_effect = { CHARACTER = $OWNER$ }
		}
		else = {
			hidden_effect = { elope_success_opinion_rivalry_effect = { CHARACTER = $OWNER$ } }
		}

		#Towards target
		if = {
			limit = { scope:root_scope = this } #Root wants to see their own
			elope_success_opinion_rivalry_effect = { CHARACTER = $TARGET$ }
		}
		else = {
			hidden_effect = { elope_success_opinion_rivalry_effect = { CHARACTER = $TARGET$ } }
		}
	}


	hidden_effect = {
		#Target's personal consequences, divorce happens here
		elope_success_personal_consequences_effect = { CHARACTER = $TARGET$ }

		#Marriage
		$OWNER$ = { #We do this last so old spouses remain when we run personal consequences effect
			if = {
				limit = { is_female = yes }
				marry_matrilineal_if_possible = { CHARACTER = $TARGET$ }
			}
			else = {
				marry = $TARGET$
			}
		}
	}
}

elope_failure_discovered_effect = {
	elope_opinion_list_builder_effect = { OWNER = $OWNER$ TARGET = $TARGET$ LIST_NAME = elope_opinion_list }

	#Expose lover secret
	$OWNER$ = {
		random_secret = {
			limit = {
				secret_type = secret_lover
				secret_target = $TARGET$
			}
			
			if = {
				limit = { $DISCOVERER$ = { is_ai = yes } }
				expose_secret = $DISCOVERER$
			}
			else = {
				reveal_to = $DISCOVERER$
			}
		}
	}

	#Opinions
	ordered_in_list = {
		list = elope_opinion_list
		max = 10 #To make sure we include all
		check_range_bounds = no
		order_by = ai_vengefulness

		#Towards owner
		if = {
			limit = {
				OR = { #Owner wants to see everything, root wants to see their own
					root = $OWNER$
					root = this
				}
			}
			elope_failure_discovered_opinion_rivalry_effect = { CHARACTER = $OWNER$ }
		}
		else = {
			hidden_effect = { elope_failure_discovered_opinion_rivalry_effect = { CHARACTER = $OWNER$ } }
		}

		#Towards target
		if = {
			limit = { root = this } #Root wants to see their own
			elope_failure_discovered_opinion_rivalry_effect = { CHARACTER = $TARGET$ }
		}
		else = {
			hidden_effect = { elope_failure_discovered_opinion_rivalry_effect = { CHARACTER = $TARGET$ } }
		}
	}
}

elope_success_opinion_rivalry_effect = {
	#Crime opinion
	add_opinion = {
		target = $CHARACTER$
		modifier = elopement_opinion
	}

	#Rivalry
	if = {
		limit = {
			can_set_relation_rival_trigger = { CHARACTER = $CHARACTER$ }
			is_ai = yes
			trigger_if = {
				limit = { is_spouse_of = $CHARACTER$ }
				ai_vengefulness >= low_positive_ai_value
			}
			trigger_else = {
				ai_vengefulness > 0
			}
		}
		if = {
			limit = {
				can_set_relation_nemesis_trigger = { CHARACTER = $CHARACTER$ }
				ai_vengefulness >= medium_positive_ai_value
			}
			set_relation_nemesis = $CHARACTER$
		}
		else = {
			set_relation_rival = $CHARACTER$
		}
	}
	else_if = {
		limit = {
			can_set_relation_potential_rival_trigger = { CHARACTER = $CHARACTER$ }
		}
		hidden_effect = { set_relation_potential_rival = $CHARACTER$ }
	}
}

elope_success_personal_consequences_effect = {
	$CHARACTER$ = {

		#Divorce
		if = {
			limit = {
				is_married = yes
			}
			every_spouse = {
				save_temporary_scope_as = eloper_divorcee

				divorce_effect = {
					DIVORCER = $CHARACTER$
					DIVORCEE = scope:eloper_divorcee
				}
				if = {
					limit = { has_relation_lover = $CHARACTER$ }
					remove_relation_lover = $CHARACTER$
				}
			}

			#Divorce consequences
			if = {
				limit = { faith = { NOT = { has_doctrine = doctrine_divorce_allowed } } }
				add_prestige_level = -1
				add_piety_level = -1

				if = {
					limit = {
						faith = { has_doctrine = tenet_communion }
						NOT = { has_trait = excommunicated }
					}
					add_trait = excommunicated
				}
			}
		}

		#Break betrothal
		if = {
			limit = { is_betrothed = yes }
			reverse_add_opinion = {
				modifier = broke_betrothal_opinion
				target = betrothed
			}
			break_betrothal = betrothed
		}

		#End concubinage
		if = {
			limit = { is_concubine = yes }
			liege = {
				remove_concubine = $CHARACTER$

				if = {
					limit = { has_relation_lover = $CHARACTER$ }
					remove_relation_lover = $CHARACTER$
				}
			}
			add_prestige_level = -1
		}
	}
}

elope_failure_discovered_opinion_rivalry_effect = {
	add_opinion = {
		target = $CHARACTER$
		modifier = attempted_elopement_opinion
	}

	#Rivalry
	if = {
		limit = {
			can_set_relation_potential_rival_trigger = { CHARACTER = $CHARACTER$ }
		}
		hidden_effect = { set_relation_potential_rival = $CHARACTER$ }
	}
}