﻿#########################
# Contubernalis Effects #
#########################

# Helper effects for the Contubernalis effect

# Adds tyranny to the scoped character as a result of making the given
# character into a contubernalis.
#
# scope = the character creating a contubernalis.
# scope:actor = same as scope.
# scope:recipient = the character becoming a contubernalis.
regula_gain_create_contubernalis_tyranny = {
	if = {
		# If we have an execute reason, we shouldn't actually gain tyranny.
		limit = {
			has_execute_reason = scope:recipient
		}
		custom_tooltip = IS_ALLOWED_TO_CONTUBERNALIS_DESC
	}
	else_if = {
		limit = {
			regula_contubernalis_tyranny_gain > 0
		}
		add_tyranny = regula_contubernalis_tyranny_gain
	}
}

# Assumes the victim is a women and actor is a man
# Effects are:
# 1. Make Family/Spouses/Dynasty not like Magister
# 2. Depose/Divorce victim
# 3. Remove Devoted traits if victim has one
# 4. Change victim into slave by replacing personality and adding Contubernalis trait (plus other minor effects)
# 5. Have sex with target
# 6. Remove human sacrafice modifier if victim has it (and give a little extra piety)

# Variables are
# ACTOR - The person turning the VICIM into a contubernalis
# VICTIM - The person being rended into a contubernalis
regula_turn_into_contubernalis = {

	# Relationship hits
	$VICTIM$ = {
		# Make family and spouse not like actor
		every_close_family_member = { # Opinion hits first.
			limit = { NOT = { this = $ACTOR$ } }
			add_to_temporary_list = victim_family_list
		}
		every_spouse = {
			limit = {
				NOR = {
					this = $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 = {
					$ACTOR$ = {
						has_execute_reason = $VICTIM$
					}
				}
				every_in_list = {
					list = victim_family_list
					custom = all_close_family_and_spouses
					add_opinion = {
						target = $ACTOR$
						modifier = regula_contubernalis_close_family_opinion
					}
				}
			}
			else = {
				every_in_list = {
					list = victim_family_list
					custom = all_close_family_and_spouses
					add_opinion = {
						target = $ACTOR$
						modifier = regula_contubernalis_close_family_crime_opinion
					}
				}
			}
		}
		# Victim's dynasty hates executioner
		if = {
			limit = {
				exists = dynasty
				exists = $ACTOR$.dynasty
				NOT = { dynasty = $ACTOR$.dynasty }
				NOT = {
					$ACTOR$ = {
						has_execute_reason = $VICTIM$
					}
				}
			}
			dynasty = {
				every_dynasty_member = {
					limit = {
						NOR = {
							this = $VICTIM$
							is_in_list = victim_family_list
						}
					}
					custom = all_dynasty_members
					add_to_temporary_list = victim_dynasty_list
					add_opinion = {
						target = $ACTOR$
						modifier = regula_contubernalis_dynasty_member_opinion
					}
				}
			}
		}

		# Victim's friends and lovers
		every_relation = {
			type = friend
			limit = {
				NOR = {
					this = $VICTIM$
					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 = $VICTIM$
					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 = $ACTOR$
					modifier = regula_contubernalis_close_relation_opinion
				}
			}
		}
	}

	# Depose and divorce.
	$VICTIM$ = {
		if = {
			limit = { is_ruler = yes }
			depose = yes
		}
		if = {
			limit = { is_married = yes }
			every_spouse = {
				divorce = $VICTIM$
			}
		}
		if = {
			limit = { exists = betrothed }
			break_betrothal = betrothed
		}

		# If pregnant, lose the baby ;_;
		# This removes visible pregnancies and unknown ones
		# But only tells player if pregnancy is visible
		if = {
			limit = { is_pregnant = yes }
			if = {
				limit = { has_trait = pregnant }
				end_pregnancy = yes
				pregnancy_cleanup_effect = yes
			}
			else = {
				hidden_effect = {
					end_pregnancy = yes
					pregnancy_cleanup_effect = yes
				}
			}
		}


		# Mold Contubernalis, current effects are:
		# - Remove existing Personality, replace with Lustful, Zealous, Humble
		# - Add Contubernalis trait and set immortal age to 20
		# - Set Faith to Magi, Sexuality to bisexual
		# - Increase beauty one step
		# - Remove all major/minor disfigurements and wounds.
		# - Sex with Magister
		custom_description = {
			text = remove_personality_on_soul_drain_effect
			subject = $VICTIM$
			remove_all_personality_traits = yes
			add_trait = lustful
			add_trait = zealous
			add_trait = humble
		}

		# Remove devoted trait if we have one
		if = {
			limit = { has_trait = devoted_trait_group }
			$VICTIM$ = {
				regula_remove_devoted_traits = yes
			}
		}

		add_trait_force_tooltip = contubernalis
		set_immortal_age = 20

		if = {
			limit = {
				faith != $ACTOR$.faith
			}
			set_character_faith = $ACTOR$.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
	}

	$ACTOR$ = {
		regula_sex_with_target_normal = { TARGET = $VICTIM$ }
	}

	# Gain a little piety bonus if they were a "designated sacrifice"
	if = {
		limit = { 
			$VICTIM$ = {
				has_character_modifier = designated_human_sacrifice_modifier
			}
			$ACTOR$ = {
				faith = {
					has_doctrine_parameter = human_sacrifice_active
				}
			}
		}
		$ACTOR$ = {
			add_piety = 250
		}
		$VICTIM$ = {
			remove_character_modifier = designated_human_sacrifice_modifier
		}
	}
}
