﻿# Characters getting married
# root = Major partner of the marriage
# scope:spouse = Minor partner of the marriage
on_marriage = { # Will not fire from historical marriages
	effect = {
		add_achievement_flag_effect = { FLAG = achievement_until_death_do_us_part_flag }
		marriage_soulmate_exclusivity_confrontation_effect = { SPOUSE_1 = root SPOUSE_2 = scope:spouse }

		vassals_dislike_marry_lowborn_effect = {
			SPOUSE = scope:spouse
			RULER = root
		}
		
		create_character_memory = {
			type = married
			participants = {
				spouse = scope:spouse
			}
		}
		scope:spouse = {
			create_character_memory = {
				type = married
				participants = {
					spouse = root
				}
			}
		}

		# Update opinion effects for the new spouse as well as all other spouses/concubines.
		scope:spouse = {
			update_active_consort_opinion_effect = { PARTNER = root }
		}
		every_consort = {
			limit = { this != scope:spouse }
			update_active_consort_opinion_effect = { PARTNER = root }
		}
		
		#If the mother isn't the "major" party in the marriage we need to trigger 0001 for her
		if = {
			limit = {
				scope:spouse = {
					is_female = yes
					OR = {
						AND = {
							OR = {
								has_character_flag = unmarried_bastard_pregnancy
								has_character_flag = bastard_pregnancy
							}
							pregnancy_real_father = { this = root }
						}
						any_secret = {
							type = secret_unmarried_illegitimate_child
							secret_target = {
								real_father ?= {
									any_spouse = { this = scope:spouse }
									this = root  #To make sure it is the new spouse
								}
							}
						}
					}
				}
			}
			save_scope_as = spouse_scope
			scope:spouse = {
				scope:spouse_scope = {
					save_scope_as = spouse
				}
				trigger_event = marriage_effect.0001
			}
		}
		#Enters into blood brotherhood family bond
		if = {
			limit = {
				scope:spouse = {
					has_blood_brother = yes	
					any_relation = {
						type = blood_brother
						NOT = {
							is_close_family_of = root
						}
					}
				}
			}
			scope:spouse = {
				random_relation = {
					type = blood_brother
					save_scope_as = other_blood_brother
				}
			}
			mpo_blood_brother_new_family_member_opinion_effect = yes
		}
		scope:spouse = {
			if = {
				limit = {
					root = {
						has_blood_brother = yes
						any_relation = {
							type = blood_brother
							NOT = {
								is_close_family_of = scope:spouse
							}
						}
					}
				}
				root = {
					random_relation = {
						type = blood_brother
						save_scope_as = other_blood_brother
					}
				}
				mpo_blood_brother_new_family_member_opinion_effect = yes
			}
		}
		#Remove poverty clothes, you're married to a noble now!
		if = {
			limit = {
				OR = {
					has_character_flag = peasant_outfit
					scope:spouse = {
						has_character_flag = peasant_outfit
					}
				}
				OR = {
					scope:spouse = {
						is_lowborn = no
					}
					is_lowborn = no
				}
			}
			if = {
				limit = { has_character_flag = peasant_outfit }
				remove_character_flag = peasant_outfit
			}
			if = {
				limit = { scope:spouse = { has_character_flag = peasant_outfit } }
				scope:spouse = { remove_character_flag = peasant_outfit }
			}
		}

		# STRATIFIED SOCIETY CULTURAL TRADITION
		if = {
			limit = {
				culture = {
					has_cultural_tradition = tradition_stratified_society
				}
				government_has_flag = government_has_influence
				is_independent_ruler = yes
				exists = scope:spouse.house
				scope:spouse.house.house_head = {
					top_liege = root
					culture = root.culture
				}
			}
			scope:spouse.house.house_head = {
				change_influence = {
					value = monumental_influence_value
				}
			}
		}
	}

	events = {
		#If mother marries real_father:
		marriage_effect.0001 # For mother (also clears the secret and sets the father)
		marriage_effect.1001 # If you marry your lover - root fires it off
		delay = { days = { 5 10 }}
		marriage_effect.2001 # Monogamous wedding event
		ep3_emperor_yearly.2040 # EP3 Byzantine flavor if you are remarrying
	}
}

# Characters get divorced in any way
# root = character initiating the divorce
# scope:spouse = the divorced spouse
# scope:reason = flag:script or flag:faith
on_divorce = { # Will not fire from historical divorces
	effect = {
		save_scope_as = spouse_2
		scope:spouse = {
			trigger_event = marriage_effect.3001 #Flag if you really liked your spouse, to enable suicide
		}
		send_divorce_notifications_effect = { CHARACTER = root SPOUSE = scope:spouse }
		send_divorce_notifications_effect = { CHARACTER = scope:spouse SPOUSE = root }

		# Update opinion effects for the divorced spouse as well as all remaining spouses/concubines.
		scope:spouse = {
			save_scope_as = is_being_divorced
			update_active_consort_opinion_effect = { PARTNER = root }
		}
		every_consort = {
			limit = { this != scope:spouse }
			update_active_consort_opinion_effect = { PARTNER = root }
		}
		#Remove blood brother opinion
		if = {
			limit = {
				scope:spouse = {
					has_blood_brother = yes
					any_relation = {
						type = blood_brother
						root = {
							has_opinion_modifier = {
								modifier = blood_brother_close_family_opinion
								target = prev
							}
						}
					}
				}
			}
			scope:spouse = {
				random_relation = {
					type = blood_brother
					save_scope_as = other_blood_brother
				}
			}
			mpo_blood_brother_remove_family_opinion_effect = yes
		}
		if = {
			limit = {
				has_blood_brother = yes
				any_relation = {
					type = blood_brother
					scope:spouse = {
						has_opinion_modifier = {
							modifier = blood_brother_close_family_opinion
							target = prev
						}
					}
				}
			}
			root = {
				random_relation = {
					type = blood_brother
					save_scope_as = other_blood_brother
				}
			}
			scope:spouse = {
				mpo_blood_brother_remove_family_opinion_effect = yes
			}
		}
		# HOUSE RELATIONS
		if = {
			limit = {
				exists = house
				exists = scope:spouse.house
			}
			house = {
				change_house_relation_effect = {
					HOUSE = scope:spouse.house
					VALUE = house_relation_damage_minor_value
					REASON = divorce
					CHAR = root
					TARGET_CHAR = scope:spouse
					TITLE = scope:dummy_gender
				}
			}
		}
		#Unop: Add a new divorce memory
		create_character_memory = {
			type = divorced
			participants = {
				spouse = scope:spouse
			}
		}
		scope:spouse = {
			create_character_memory = {
				type = divorced
				participants = {
					spouse = root
				}
			}
		}
	}
}

# Character becomes another's concubine
# root = the concubinist
# scope:concubine = the concubine
on_concubinage = {
	effect = {
		if = {  # For if real_father becomes consort of mother (also clears the secret and sets the father)
			limit = {
				root = {
					is_female = yes
					OR = {
						AND = {
							OR = {
								has_character_flag = unmarried_bastard_pregnancy
								has_character_flag = bastard_pregnancy
							}
							pregnancy_real_father = { this = scope:concubine }
						}
						any_secret = {
							type = secret_unmarried_illegitimate_child
							secret_target = {
								real_father ?= {
									any_consort = { this = ROOT }
									this = scope:concubine  #To make sure it is the new partner
								}
							}
						}
					}
				}
			}
			scope:concubine = {
				save_scope_as = consort
			}
			root = {
				trigger_event = marriage_effect.0011
			}
		}
		if = { # For if mother becomes concubine of real_father (also clears the secret and sets the father)
			limit = {
				scope:concubine = {
					is_female = yes
					OR = {
						AND = {
							OR = {
								has_character_flag = unmarried_bastard_pregnancy
								has_character_flag = bastard_pregnancy
							}
							pregnancy_real_father = { this = root }
						}
						any_secret = {
							type = secret_unmarried_illegitimate_child
							secret_target = {
								real_father ?= {
									any_consort = { this = ROOT }
									this = root  #To make sure it is the new partner
								}
							}
						}
					}
				}
			}
			root = {
				save_scope_as = consort
			}
			scope:concubine = {
				trigger_event = marriage_effect.0011
			}
		}

		# Update opinion effects for the new spouse as well as all other spouses/concubines.
		scope:concubine = {
			update_active_consort_opinion_effect = { PARTNER = root }
		}
		every_consort = {
			limit = { this != scope:concubine }
			update_active_consort_opinion_effect = { PARTNER = root }
		}
	}
}

# Character ceases to be another's concubine
# root = the concubinist
# scope:concubine = the concubine
# scope:reason = flag:death, flag:faith, or flag:script
on_concubinage_end = {
	effect = {
		scope:concubine = {
			save_scope_as = is_being_set_aside
			update_active_consort_opinion_effect = { PARTNER = root }
		}
		every_consort = {
			limit = { this != scope:concubine }
			update_active_consort_opinion_effect = { PARTNER = root }
		}
	}
}


# root = first person in betrothal
# scope:second = second person in betrothal
# scope:reason = flag:timeout, flag:death, flag:faith, or flag:script
on_betrothal_broken = {
	effect = {
		if = {
			limit = {
				scope:reason = flag:timeout
			}
			trigger_event = marriage_interaction.0003
		}
		else = {
			send_break_betrothal_notifications_effect = { CHARACTER = root BETROTHED = scope:second }
			send_break_betrothal_notifications_effect = { CHARACTER = scope:second BETROTHED = root }
		}
		# HOUSE RELATIONS
		if = {
			limit = {
				exists = house
				exists = scope:second.house
				exists = scope:second
			}
			house = {
				change_house_relation_effect = {
					HOUSE = scope:second.house
					VALUE = house_relation_damage_minor_value
					REASON = betrothal_broken
					CHAR = root
					TARGET_CHAR = scope:second
					TITLE = scope:dummy_gender
				}
			}
		}
	}
}
