﻿# Chance of having a lowborn
hp_set_consort_to_lowborn_effect = {
	if = {
		limit = {
			scope:liege = {
				OR = {
					religion = religion:islam_religion
					has_unsettled_government_trigger = yes
					AND = {
						hp_accepts_concubinage = yes
						allowed_more_concubines = yes
					}
				}
			}
			is_lowborn = no
		}
		random = {
			chance = 50
			modifier = {
				scope:liege = {
					has_variable = family_background_important
				}
				add = -20
			}
			modifier = {
				scope:liege = {
					allowed_more_spouses = yes
					has_settled_harem_trigger = yes
				}
				add = -20
			}
			modifier = {
				scope:liege = {
					government_allows = administrative
					is_independent_ruler = no
				}
				add = 50
			}
			modifier = {
				scope:liege = {
					religion = religion:islam_religion
				}
				add = 100
			}
			set_to_lowborn = yes
		}
	}
}
# Sovereigns have a higher chance of having consorts with good traits
hp_sovereign_bride_genetics_effect = {

	random = {
		chance = 15
		modifier = { # Emperors
			add = 5
			scope:liege = {
				highest_held_title_tier >= tier_empire
			}
		}
		modifier = { #Kings
			add = 10
			scope:liege = {
				highest_held_title_tier >= tier_kingdom
			}
		}
		modifier = {
			scope:liege = { has_variable = beauty_important }
			add = 10
		}
		# For balance
		modifier = {
			any_in_list = {
				list = presented_candidates_list
				num_of_good_genetic_traits >= 1
				count > 0
			}
			add = -10
		}
		modifier = {
			any_in_list = {
				list = presented_candidates_list
				num_of_good_genetic_traits >= 1
				count > 1
			}
			add = -5
		}
		modifier = {
			any_in_list = {
				list = presented_candidates_list
				num_of_good_genetic_traits >= 1
				count > 2
			}
			add = -5
		}
		random_list = {
			1 = { # Superior genetics
				modifier = { #Emperors
					add = 5
					scope:liege = {
						highest_held_title_tier >= tier_empire
					}
				}
				modifier = { #Kings
					add = 3
					scope:liege = {
						highest_held_title_tier >= tier_kingdom
					}
				}
				random_list = {
					20 = { # Beauty
						modifier = {
							scope:liege = { has_variable = beauty_important }
							add = 5
						}
						add_trait = beauty_good_3
					}
					9 = { # Intellect
						modifier = {
							scope:liege = { has_variable = personality_skills_important }
							add = 10
						}
						add_trait = intellect_good_3
					}
					9 = { # Physique
						modifier = {
							scope:liege = { has_variable = beauty_important }
							add = 5
						}
						add_trait = physique_good_3
					}
					2 = { # Fecund													
						add_trait = fecund
					}
				}											
			}
			4 = { # Good genetics
				modifier = { #Emperors
					add = 5
					scope:liege = {
						highest_held_title_tier >= tier_empire
					}
				}
				modifier = { #Kings
					add = 3
					scope:liege = {
						highest_held_title_tier >= tier_kingdom
					}
				}
				random_list = {
					4 = { # Beauty
						modifier = {
							scope:liege = { has_variable = beauty_important }
							add = 2
						}
						add_trait = beauty_good_2
					}
					1 = { # Intellect
						modifier = {
							scope:liege = { has_variable = personality_skills_important }
							add = 5
						}
						add_trait = intellect_good_2
					}
					1 = { # Physique
						modifier = {
							scope:liege = { has_variable = beauty_important }
							add = 2
						}
						add_trait = physique_good_2
					}
				}											
			}
			25 = { # Decent genetics
				random_list = {
					4 = { # Beauty
						modifier = {
							scope:liege = { has_variable = beauty_important }
							add = 2
						}
						add_trait = beauty_good_1
					}
					1 = { # Intellect
						modifier = {
							scope:liege = { has_variable = personality_skills_important }
							add = 5
						}
						add_trait = intellect_good_1
					}
					1 = { # Physique
						modifier = {
							scope:liege = { has_variable = beauty_important }
							add = 2
						}
						add_trait = physique_good_1
					}
				}											
			}
		}
	}
}
# Regular jarya have a lower chance of having good traits
hp_regular_jarya_genetics_effect = {
	# Lower chance genetic traits for regular Muslim concubines (dukes/below)
	random = {
		chance = 10  # Lower base chance
		random_list = {
			1 = { # Superior genetics
				random_list = {
					20 = { # Beauty
						add_trait = beauty_good_3
					}
					9 = { # Intellect
						add_trait = intellect_good_3
					}
					9 = { # Physique
						add_trait = physique_good_3
					}
					2 = { # Fecund													
						add_trait = fecund
					}
				}											
			}
			4 = { # Good genetics
				random_list = {
					4 = { # Beauty	
						add_trait = beauty_good_2
					}
					1 = { # Intellect
						add_trait = intellect_good_2
					}
					1 = { # Physique
						add_trait = physique_good_2
					}
				}											
			}
			25 = { # Decent genetics
				random_list = {
					4 = { # Beauty	
						add_trait = beauty_good_1
					}
					1 = { # Intellect
						add_trait = intellect_good_1
					}
					1 = { # Physique
						add_trait = physique_good_1
					}
				}											
			}
		}
	}
}
# Chance of having better skills for sovereign lieges
add_random_skill_to_sovereign_candidate_effect = {
	if = { # Skills
		limit = {
			scope:liege = { has_variable = personality_skills_important }
			min_personality_and_skills_trigger = no
		}
		random = {
			chance = 30
			random_list = {
				1 = { # Diplomacy
					save_scope_value_as = {
						name = skill_gain
						value = {
							value = average_skill_level
							subtract = diplomacy
							add = {
								integer_range = { min = 0 max = 7 }
							}
						}
					}
					add_diplomacy_skill = scope:skill_gain
				}
				1 = { # Learning
					save_scope_value_as = {
						name = skill_gain
						value = {
							value = average_skill_level
							subtract = learning
							add = {
								integer_range = { min = 0 max = 7 }
							}
						}
					}
					add_learning_skill = scope:skill_gain
				}
				1 = { # Prowess
					trigger = {
						can_be_knight_trigger = { ARMY_OWNER = scope:liege }
					}
					save_scope_value_as = {
						name = skill_gain
						value = {
							value = average_skill_level
							subtract = prowess
							add = {
								integer_range = { min = 0 max = 7 }
							}
						}
					}
					add_prowess_skill = scope:skill_gain
				}
			}
		}
	}
	random = {
		chance = 15
		modifier = {
			scope:liege = { has_variable = personality_skills_important }
			add = 5
		}
		add_trait = lifestyle_poet
	}
}
# Chance of having the poet trait
add_random_skill_to_regular_candidate_effect = {
	random = {
		chance = 7
		add_trait = lifestyle_poet
	}
}
# After jarya creation
hp_after_jarya_creation_effect = {
	hp_set_consort_to_lowborn_effect = yes
	add_character_flag = {
		flag = abducted_non_believer
		years = 5
	}
	# Liege is a sovereign
	if = {
		limit = {
			scope:liege = {
				can_have_imperial_consorts_trigger = yes
			}
		}
		# Better genetics
		hp_sovereign_bride_genetics_effect = yes
		add_random_skill_to_sovereign_candidate_effect = yes
		# High chance religion change
		if = {
			limit = {
				faith != scope:liege.faith
			}
			random = {
				chance = 60  # 60% chance
				modifier = {
					scope:liege = {
						has_trait = zealous
					}
					add = 20
				}
				set_character_faith = scope:liege.faith
			}
		}
	}
	else = {
		# Regular chance of good genetics and religion change
		hp_regular_jarya_genetics_effect = yes
		add_random_skill_to_regular_candidate_effect = yes
		# Low chance religion change for bride show and admin vassals
		if = {
			limit = {
				faith != scope:liege.faith
			}
			random = {
				chance = 30  # 30% chance
				modifier = {
					scope:liege = {
						has_trait = zealous
					}
					add = 30
				}
				set_character_faith = scope:liege.faith
			}
		}
	}
}
# For easter egg consorts
hp_after_special_consort_creation_effect = {
	hp_set_consort_to_lowborn_effect = yes
	if = {
		limit = {
			scope:liege.religion = religion:islam_religion
		}
		add_character_flag = {
			flag = abducted_non_believer
			years = 5
		}
	}
	if = {
		limit = {
			faith != scope:liege.faith
		}
		set_character_faith = scope:liege.faith
	}
}
# For the bride show decision
bs_after_consort_creation_effect = {
	hp_set_consort_to_lowborn_effect = yes
	# Sovereigns should have better traits
	if = {
		limit = {
			scope:liege = {
				highest_held_title_tier >= tier_kingdom
				OR = {
					is_independent_ruler = yes
					NOT = { government_allows = administrative }
				}
			}
		}
		# Better genetics
		hp_sovereign_bride_genetics_effect = yes
		add_random_skill_to_sovereign_candidate_effect = yes
	}
	else = {
		# Regular chance of good traits
		hp_regular_jarya_genetics_effect = yes
		add_random_skill_to_regular_candidate_effect = yes
	}
}

# Location for concubine creation
hp_select_concubine_location_effect = {
	if = {
		limit = {
			NOT = { exists = scope:concubine_location }
		}
		scope:liege = {
			if = {
				limit = {
					religion = religion:islam_religion
				}
				# 1) CHECK FOR NEIGHBORS FIRST, THEN ADD THEIR PROVINCES
				if = {
					limit = {
						any_neighboring_and_across_water_top_liege_realm_owner = {
							religion != religion:islam_religion
							any_realm_province = {
								religion != religion:islam_religion
							}
						}
					}
					every_neighboring_and_across_water_top_liege_realm_owner = {
						limit = {
							religion != religion:islam_religion
						}
						every_realm_province = {
							limit = {
								religion != religion:islam_religion
							}
							add_to_temporary_list = concubine_location_list
						}
					}
				}
				# 2) IF FEWER THAN 3, ADD OWN REALM
				if = {
					limit = {
						any_in_list = {
							list = concubine_location_list
							count = 0
						}
						any_realm_province = {
							NOR = {
								religion = religion:islam_religion
								is_in_list = concubine_location_list
							}
						}
					}
					every_realm_province = {
						limit = {
							NOR = {
								religion = religion:islam_religion
								is_in_list = concubine_location_list
							}
						}
						add_to_temporary_list = concubine_location_list
					}
				}
				# 3) IF STILL FEWER THAN 3, ADD TOP LIEGE'S REALM
				if = {
					limit = {
						any_in_list = {
							list = concubine_location_list
							count = 0
						}
						top_liege ?= {
							this != scope:liege
							any_realm_province = {
								NOR = {
									religion = religion:islam_religion
									is_in_list = concubine_location_list
								}
							}
						}
					}
					top_liege = {
						every_realm_province = {
							limit = {
								NOR = {
									religion = religion:islam_religion
									is_in_list = concubine_location_list
								}
							}
							add_to_temporary_list = concubine_location_list
						}
					}
				}
			}
			# PICK A LOCATION
			if = {
				limit = {
					any_in_list = {
						list = concubine_location_list
						count = 1
					}
				}
				random_in_list = {
					list = concubine_location_list
					save_scope_as = concubine_location
				}
			}
			else_if = {
				limit = {
					any_in_list = {
						list = concubine_location_list
						count > 1
					}
				}
				ordered_in_list = {
					list = concubine_location_list
					order_by = {
						value = 1
						add = recruit_muslim_concubines_county_value
					}
					save_scope_as = concubine_location
				}
			}
			else_if = {
				limit = {
					exists = capital_province
				}
				capital_province = {
					save_scope_as = concubine_location
				}
			}
			else_if = {
				limit = {
					exists = location.province_owner
				}
				location.county.title_province = {
					save_scope_as = concubine_location
				}
			}
			else_if = {
				limit = {
					any_neighboring_province = {
						exists = county
					}
				}
				random_neighboring_province = {
					limit = {
						exists = county
					}
					save_scope_as = concubine_location
				}
			}
			else = { # Last resort
				random_province = {
					save_scope_as = concubine_location
				}
			}
		}
	}
}

hp_character_creation_effect = {
	# Choose the reference location
	hp_select_concubine_location_effect = yes
	create_character = {
		location = root.capital_province
		template = $CHARACTER$_template
		save_scope_as = $CHARACTER$
	}
}

# Events #
# Have sex with a consort
had_sex_with_consort_effect = {
	had_sex_with_effect = {
		CHARACTER = scope:recipient
		PREGNANCY_CHANCE = pregnancy_chance
	}
	random_list = {
		10 = { # A very good night
			modifier = {
				scope:recipient = {
					opinion = {
						target = scope:actor
						value >= 50
					}
				}
				add = 50
			}
			opinion_modifier = {
				who = scope:recipient
				opinion_target = scope:actor
				multiplier = 0.5
			}
			compatibility_modifier = {
				who = scope:recipient
				compatibility_target = scope:actor
				min = -25
				max = 25
				multiplier = 0.5
			}
			opinion_modifier = {
				who = scope:actor
				opinion_target = scope:recipient
				multiplier = 0.5
			}
			modifier = {
				has_relation_soulmate = scope:recipient
				add = 80
			}
			modifier = {
				has_relation_lover = scope:recipient
				add = 65
			}
			modifier = {
				has_relation_best_friend = scope:recipient
				add = 15
			}
			modifier = {
				has_relation_friend = scope:recipient
				add = 10
			}
			modifier = {
				has_trait = lustful
				add = 10
			}
			modifier = {
				scope:recipient = {
					has_trait = lustful
				}
				add = 10
			}
			modifier = {
				has_trait = gregarious
				add = 10
			}
			modifier = {
				scope:recipient = {
					has_trait = gregarious
				}
				add = 10
			}
			set_local_variable = {
				name = fantastic_night_variable
				value = yes
			}
			if = {
				limit = { 
					NOR = { 
						has_relation_lover = scope:recipient
						has_relation_soulmate = scope:recipient
					}
				}
				progress_towards_lover_effect = {
					CHARACTER = scope:recipient
					REASON = lover_seduction_general
					OPINION = 20
				}
			}
			else_if = {
				limit = {
					scope:recipient = {
						NOT = {
							has_opinion_modifier = {
								modifier = romance_opinion
								target = scope:actor
							}
						}
					}
				}
				add_opinion = {
					target = scope:recipient
					modifier = romance_opinion
					opinion = 10
				}
				scope:recipient = {
					add_opinion = {
						target = scope:actor
						modifier = romance_opinion
						opinion = 10
					}
				}
			}
			if = {
				limit = {
					NOT = {
						has_trait = chaste
					}
				}
				stress_impact = {
					base = minor_stress_impact_loss
				}
			}
			scope:recipient = {
				calyver_favor_medium_gain_effect = yes
			}
		}
		50 = { # A good night
			opinion_modifier = {
				who = scope:recipient
				opinion_target = scope:actor
				multiplier = 0.5
			}
			compatibility_modifier = {
				who = scope:recipient
				compatibility_target = scope:actor
				min = -25
				max = 25
				multiplier = 0.5
			}
			opinion_modifier = {
				who = scope:actor
				opinion_target = scope:recipient
				multiplier = 0.5
			}
			modifier = {
				has_relation_soulmate = scope:recipient
				add = 25
			}
			modifier = {
				has_relation_lover = scope:recipient
				add = 15
			}
			modifier = {
				has_relation_best_friend = scope:recipient
				add = 15
			}
			modifier = {
				has_relation_friend = scope:recipient
				add = 10
			}
			modifier = {
				has_trait = lustful
				add = 10
			}
			modifier = {
				scope:recipient = {
					has_trait = lustful
				}
				add = 10
			}
			modifier = {
				has_trait = gregarious
				add = 10
			}
			modifier = {
				scope:recipient = {
					has_trait = gregarious
				}
				add = 10
			}
			set_local_variable = {
				name = good_night_variable
				value = yes
			}
			if = {
				limit = { 
					NOR = { 
						has_relation_potential_lover = scope:recipient
						has_relation_lover = scope:recipient
						has_relation_soulmate = scope:recipient
					}
				}
				progress_towards_lover_effect = {
					CHARACTER = scope:recipient
					REASON = lover_seduction_general
					OPINION = 10
				}
			}
			else_if = {
				limit = {
					scope:recipient = {
						NOT = {
							has_opinion_modifier = {
								modifier = romance_opinion
								target = scope:actor
							}
						}
					}
				}
				add_opinion = {
					target = scope:recipient
					modifier = romance_opinion
					opinion = 5
				}
				scope:recipient = {
					add_opinion = {
						target = scope:actor
						modifier = romance_opinion
						opinion = 5
					}
				}
			}
			if = {
				limit = {
					NOT = {
						has_trait = chaste
					}
				}
				stress_impact = {
					base = minor_stress_impact_loss
				}
			}
			scope:recipient = {
				calyver_favor_moderate_gain_effect = yes
			}
		}
		50 = { # A normal night
			modifier = {
				scope:recipient = {
					opinion = {
						target = scope:actor
						value <= 20
					}
				}
				add = 40
			}
			opinion_modifier = {
				who = scope:recipient
				opinion_target = scope:actor
				multiplier = 0.5
			}
			compatibility_modifier = {
				who = scope:recipient
				compatibility_target = scope:actor
				min = -25
				max = 25
				multiplier = 0.5
			}
			opinion_modifier = {
				who = scope:actor
				opinion_target = scope:recipient
				multiplier = 0.5
			}
			modifier = {
				has_relation_lover = scope:recipient
				add = 65
			}
			modifier = {
				has_relation_friend = scope:recipient
				add = 10
			}
			set_local_variable = {
				name = regular_night_variable
				value = yes
			}
			scope:recipient = {
				calyver_favor_minor_gain_effect = yes
				if = {
					limit = {
						NOT = {
							has_opinion_modifier = {
								modifier = romance_opinion
								target = scope:actor
							}
						}
					}
					add_opinion = {
						target = scope:actor
						modifier = romance_opinion
						opinion = 5
					}
					scope:actor = {
						add_opinion = {
							target = scope:recipient
							modifier = romance_opinion
							opinion = 5
						}
					}
				}
			}
			if = {
				limit = {
					NOT = {
						has_trait = chaste
					}
				}
				stress_impact = {
					base = minor_stress_impact_loss
				}
			}
		}
		20 = { # A boring night
			modifier = {
				scope:recipient = {
					opinion = {
						target = scope:actor
						value <= 20
					}
				}
				add = 50
			}
			opinion_modifier = {
				who = scope:recipient
				opinion_target = scope:actor
				multiplier = -0.2
			}
			compatibility_modifier = {
				who = scope:recipient
				compatibility_target = scope:actor
				min = -25
				max = 25
				multiplier = -0.5
			}
			opinion_modifier = {
				who = scope:actor
				opinion_target = scope:recipient
				multiplier = -0.2
			}
			modifier = {
				has_relation_rival = scope:recipient
				add = 65
			}
			modifier = {
				has_relation_nemesis = scope:recipient
				add = 85
			}
			modifier = {
				has_trait = chaste
				add = 15
			}
			modifier = {
				scope:recipient = {
					has_trait = chaste
				}
				add = 15
			}
			modifier = {
				has_trait = shy
				add = 10
			}
			modifier = {
				scope:recipient = {
					has_trait = shy
				}
				add = 10
			}
			modifier = {
				NOT = {
					is_attracted_to_gender_of = scope:recipient
				}
				add = 20
			}
			set_local_variable = {
				name = boring_night_variable
				value = yes
			}
		}
	}
}

# Select a random title
hp_select_random_title_effect = {
	if = { # Find a valid barony to give away
		limit = {
			any_held_title = {
				tier = tier_barony
				is_capital_barony = no
				is_leased_out = no
				NOR = { 
					THIS = ROOT.capital_barony 
					title_province = { 
						OR = {
							has_holding_type = city_holding 
							has_holding_type = church_holding
						}
					}
				}
			}
		}
		ordered_held_title = {
			limit = {
				tier = tier_barony
				is_capital_barony = no
				is_leased_out = no
				NOR = { 
					THIS = ROOT.capital_barony 
					title_province = { 
						OR = {
							has_holding_type = city_holding 
							has_holding_type = church_holding
						}
					}
				}
			}
			order_by = {
				value = 0
				subtract = title_province.combined_building_level
			}
			save_scope_as = worst_title
		}
	}
	else = { # Find a valid county to give away
		ordered_held_title = {
			limit = {
				tier = tier_county
				NOT = { THIS = ROOT.capital_county }
			}
			order_by = {
				value = 0
				subtract = development_level
			}
			save_scope_as = worst_title
		}
	}
}
hp_grant_worst_title_effect = {
	send_interface_toast = {
		type = event_generic_neutral
		left_icon = scope:consort_relative
		title = harem_politics.0008.a.tt
		ROOT = {
			create_title_and_vassal_change = {
				type = granted
				save_scope_as = change
				add_claim_on_loss = no
			}
			scope:worst_title = {
				change_title_holder = {
					holder = scope:consort_relative
					change = scope:change
				}
			}
			resolve_title_and_vassal_change = scope:change
			scope:consort_relative = {
				if = {
					limit = { scope:worst_title.tier = tier_barony }
					add_opinion = {
						target = ROOT
						modifier = received_title_barony
					}
				}
				else = {
					add_opinion = {
						target = ROOT
						modifier = received_title_county
					}
				}
				if = {
					limit = {
						NOT = {
							is_allied_to = root
						}
					}
					if = {
						limit = {
							is_close_family_of = $CONSORT$
						}
						create_alliance = {
							target = root
							allied_through_owner = root
							allied_through_target = $CONSORT$
						}
					}
					else = {
						add_truce_both_ways = {
							character = root
							years = 4
							name = TRUCE_HAREM
						}
					}
				}
			}
			if = {
				limit = {
					can_add_hook = {
						target = scope:consort_relative
						type = indebted_hook
					}
				}
				add_hook = {
					target = scope:consort_relative
					type = indebted_hook
				}
			}
		}
	}
}

appoint_target_to_available_high_consort_court_position_effect = {
	# LIEGE is the liege appointing the target
	# CONSORT is the target
	$CONSORT$ = {
		save_scope_as = consort_target
		custom_tooltip = will_gain_high_consort_court_position_tooltip
	}
	hidden_effect = {
		$LIEGE$ = {
			if = {
				limit = {
					can_employ_char_to_high_consort_court_position_trigger = {
						CONSORT = $CONSORT$
					}
				}
				random_list = {
					1 = {
						trigger = {
							can_appoint_char_to_court_position = {
								CHAR = $CONSORT$
								COURT_POS = grand_consort_court_position
							}
						}
						modifier = {
							add = $CONSORT$.aptitude:grand_consort_court_position
						}
						modifier = {
							$CONSORT$ = {
								any_child = {
									is_child_of = $LIEGE$
									is_primary_heir_of = $LIEGE$
								}
							}
							add = 2
						}
						appoint_court_position = {
							recipient = $CONSORT$
							court_position = grand_consort_court_position
						}
					}
					1 = {
						trigger = {
							can_appoint_char_to_court_position = {
								CHAR = $CONSORT$
								COURT_POS = pure_consort_court_position
							}
						}
						modifier = {
							add = $CONSORT$.aptitude:pure_consort_court_position
						}
						appoint_court_position = {
							recipient = $CONSORT$
							court_position = pure_consort_court_position
						}
					}
					1 = {
						trigger = {
							can_appoint_char_to_court_position = {
								CHAR = $CONSORT$
								COURT_POS = virtuous_consort_court_position
							}
						}
						modifier = {
							add = $CONSORT$.aptitude:virtuous_consort_court_position
						}
						appoint_court_position = {
							recipient = $CONSORT$
							court_position = virtuous_consort_court_position
						}
					}
					1 = {
						trigger = {
							can_appoint_char_to_court_position = {
								CHAR = $CONSORT$
								COURT_POS = worthy_consort_court_position
							}
						}
						modifier = {
							add = $CONSORT$.aptitude:worthy_consort_court_position
						}
						appoint_court_position = {
							recipient = $CONSORT$
							court_position = worthy_consort_court_position
						}
					}
				}
			}
		}
	}
}