﻿## Vanilla ##
yearly_global_pulse = {
	first_valid_on_action = { 
		calyver_rabbinism_lose_polygamy_check
	}
}

random_yearly_playable_pulse = {
	first_valid_on_action = { 
		calyver_random_yearly_playable_events
	}
}

quarterly_playable_pulse = {
	first_valid_on_action = { 
		calyver_ruler_events_on_actions
	}
}

# Called from code once a year for all characters, at a random point that year (individually for each character)
# Root is the character
random_yearly_everyone_pulse = {
	first_valid_on_action = { 
		calyver_consort_events_on_actions
	}
}

## Harem Politics ##
calyver_random_yearly_playable_events = {
	trigger = {			# On_actions can have triggers. If an on_action fires and its trigger returns false, nothing happens
		hp_accepts_harems = yes
	}
	random_events = {	# A single event will be picked to fire
		
		chance_to_happen = 70	# A percentage chance determining whether the events involved will be evaluated at all

		chance_of_no_event = {
			value = 0
			if = {
				limit = {
					is_ai = yes
					highest_held_title_tier < tier_duchy
				}
				add = 75 # AI counts aren't particularly important
			}
			if = { #Less drama for only polygamous rulers
				limit = {
					hp_accepts_polygamy = yes
					hp_accepts_concubinage = no
				}
				add = 25
			}
			if = { # Tribal/Nomadic rulers don't have a well-defined harem
				limit = {
					OR = {
						government_has_flag = government_is_nomadic
						government_has_flag = government_is_tribal
					}
				}
				add = 50
			}
			if = {
				limit = {
					hp_accepts_harems = no
				}
				add = 100 # Monogamous rulers don't apply
			}
		}

		100 = harem_politics.0001 	# Begging event
		75 = harem_politics.0002	# Hui Guifei tortures Hailan
		100 = harem_politics.0004   # Promoting a concubine
		100 = harem_politics.0005   # Demoting a spouse
		75 = harem_politics.0006	# Choosing an adoptive parent for a parentless child
		100 = harem_politics.0008	# A consort or parent asks you to title a relative
		25 = harem_politics.0009	# Meeting Consort Ling - Chun Fei version
		25 = harem_politics.0010	# Meeting Consort Ling - Jia Fei version
		100 = harem_politics.0011	# Choosing a night companion
		100 = harem_politics.0012	# Poisoning an unwanted consort
		75 = harem_politics.1013	# Qi is jealous of Xin
		25 = harem_politics.0016	# Your concubines are performing
		25 = harem_politics.0017	# Meeting Consort Ling - She's a maid
		100 = harem_politics.0019	# Yu disrespects Zhen Huan
		75 = harem_politics.0020	# You meet your consorts in a walk
		100 = harem_politics.0022	# Noble Consort Hua makes Zhen Huan miscarry
		100 = harem_politics.0024	# Summoning a consort to perform for you
		150 = harem_politics.0028	# A consort helps you relieve stress
		75 = harem_politics.0029	# A ruler asks you to promote their relative
		#75 = harem_politics.0030	# Choosing between a vassal's child or their servant
		75 = harem_politics.0031	# A consort asks you to promote a relative
		75 = harem_politics.0032	# Meeting Concubine Mei
		#100 = harem_politics.0034	# Someone asks you to release a consort
		#75 = harem_politics.0035	# A consort is stirring the pot
		100 = harem_politics.0036	# The Empress Dowager lectures the Empress
		120 = harem_politics.0037	# Muslim flavor - A matron presents you with concubines
		100 = harem_politics.1039	# A reserved night
		100 = harem_politics.0040	# The Empress Dowager pushes you to favor an ally
		150 = harem_politics.0041	# Chaotic harem
		75 = harem_politics.1042	# Meeting Hailan
		100 = harem_politics.0043	# Meeting Nurbanu
		50 = harem_politics.0047	# One of your consorts is praying to get your attention
		100 = harem_politics.1049	# Princess Gaoyang asks you to free Wu
		100 = harem_politics.1050	# Concubine Chun visits Concubine Jia
		100 = harem_politics.0051	# Consort Yang of Elegance offers you osmanthus soup
		100 = harem_politics.0053	# The Consort of Kindness performs at a banquet
		150 = harem_politics.1054	# A consort asks you to favor another one
		# 100 = harem_politics.1044	# Two harem members clash over expenses
		100 = harem_politics.1045	# Hui is jealous of Ruyi
		795 = 0		# Having a "0" entry means that there is a chance no event fires, even if there are other valid events. Good for making sure that rare events don't always fire just because every other possible event is invalid.
	}
}

calyver_consort_events_on_actions = {
	trigger = {			# On_actions can have triggers. If an on_action fires and its trigger returns false, nothing happens
		any_consort = {
			is_landed_or_landless_administrative = yes
			hp_accepts_harems = yes
		}
	}
	random_events = {	# A single event will be picked to fire
		chance_to_happen = 75	# A percentage chance determining whether the events involved will be evaluated at all

		chance_of_no_event = {
			value = 60
			if = { # Consorts of AI rulers aren't particularly important
				limit = {
					is_ai = yes
					NOT = {
						any_consort = {
							is_landed_or_landless_administrative = yes
							hp_accepts_harems = yes
							any_consort = {
								count > 5
							}
						}
					}
				}
				add = 15
			}
			if = { # Tribal/Nomadic rulers don't have a well-defined harem
				limit = {
					NOT = {
						any_consort = {
							is_landed_or_landless_administrative = yes
							has_settled_harem_trigger = yes
						}
					}
				}
				add = 15
			}
		}

		100 = consort_events.0001 	# Hui Guifei punishes Mei
		100 = consort_events.1002   # Error suppression - Hua Fei tortures Xia
		100 = consort_events.0004   # A badmouthing concubine
		100 = consort_events.0005	# The morning greeting
		100 = consort_events.0006	# Yanwan is courting favor

		100 = consort_events.0011   # Scheming and diplomatic consorts
		100 = consort_events.0012	# Choosing an ally
		100 = consort_events.0013	# Killing the main heir
		100 = consort_events.0014	# I'm favored!
		100 = consort_events.0015	# I'm unfavored
		100 = consort_events.0016	# No longer demoted
		100 = consort_events.0017	# Killing a higher-ranked consort
		50 = consort_events.0019	# Poisoning a consort

		1000 = 0		# Having a "0" entry means that there is a chance no event fires, even if there are other valid events. Good for making sure that rare events don't always fire just because every other possible event is invalid.
	}
}

calyver_ruler_events_on_actions = {
	trigger = {			# On_actions can have triggers. If an on_action fires and its trigger returns false, nothing happens
		hp_accepts_harems = yes
		is_landed_or_landless_administrative = yes
	}
	on_actions = {
		delay = { days = { 7 14 } }
		calyver_consort_punishment_on_actions
		delay = { days = { 2 9 } }
		calyver_consort_relations_on_actions
		delay = { days = { 15 28 } }
		calyver_consort_favor_on_actions
		delay = { days = { 29 56 } }
		calyver_consort_traits_on_actions
		delay = { days = { 57 112 } }
		calyver_consort_secrets_on_actions
		delay = { days = { 113 224 } }
		calyver_consort_influence_on_actions
		delay = { days = { 225 448 } }
		calyver_consort_schemes_on_actions
	}
}
calyver_consort_punishment_on_actions = {
	random_events = {	# A single event will be picked to fire

		3 = consort_events.0007	# Hidden events - An authority punishes a random consort
		13 = 0		# Having a "0" entry means that there is a chance no event fires, even if there are other valid events. Good for making sure that rare events don't always fire just because every other possible event is invalid.
	}
}
calyver_consort_relations_on_actions = {
	random_events = {	# A single event will be picked to fire
		chance_to_happen = 75	# A percentage chance determining whether the events involved will be evaluated at all
		chance_of_no_event = {
			value = 0
			if = {
				limit = {
					any_consort = {
						count <= 4
					}
				}
				add = 25
			}
			if = { # Consorts of AI rulers aren't particularly important
				limit = {
					is_ai = yes
				}
				add = 35
			}
			if = { # Tribal/Nomadic rulers don't have a well-defined harem
				limit = {
					has_settled_harem_trigger = no
				}
				add = 75
			}
		}
		1 = consort_events.0008	# Hidden events - Two consorts become friends
		1 = consort_events.0009	# Hidden events - Two consorts become enemies
		# 500 = 0		# Having a "0" entry means that there is a chance no event fires, even if there are other valid events. Good for making sure that rare events don't always fire just because every other possible event is invalid.
	}
}
calyver_consort_favor_on_actions = {
	# trigger = {
	# 	NOT = { has_game_rule = consort_favor_off }
	# }
	random_events = {	# A single event will be picked to fire
		chance_to_happen = 50	# A percentage chance determining whether the events involved will be evaluated at all

		1 = consort_events.0010	# Hidden events - Unfavored consorts
		10 = consort_favor_valuation.0006	# Hidden events - Increasing a consort's favor
		6 = harem_politics.0048	# A ruler asks you to favor their relative
		60 = 0		# Having a "0" entry means that there is a chance no event fires, even if there are other valid events. Good for making sure that rare events don't always fire just because every other possible event is invalid.
	}
}
calyver_consort_traits_on_actions = {
	random_events = {	# A single event will be picked to fire
		chance_to_happen = 50	# A percentage chance determining whether the events involved will be evaluated at all
		chance_of_no_event = {
			value = 0
			if = { # Consorts of AI rulers aren't particularly important
				limit = {
					is_ai = yes
					any_consort = {
						count < 6
					}
				}
				add = 30
			}
			if = { # Tribal/Nomadic rulers don't have a well-defined harem
				limit = {
					has_settled_harem_trigger = no
				}
				add = 75
			}
		}

		2 = consort_events.0042	# Hidden events - A consort gets a pet
		1 = consort_events.0028		# Hidden events - A consort becomes a lifestyle_poet
		1 = consort_events.0029	# Hidden events - A consort becomes a seducer
		48 = 0		# Having a "0" entry means that there is a chance no event fires, even if there are other valid events. Good for making sure that rare events don't always fire just because every other possible event is invalid.
	}
}
calyver_consort_secrets_on_actions = {
	random_events = {	# A single event will be picked to fire
		chance_to_happen = 50	# A percentage chance determining whether the events involved will be evaluated at all
		chance_of_no_event = {
			value = 0
			if = { # Consorts of AI rulers aren't particularly important
				limit = {
					is_ai = yes
					any_consort = {
						count < 6
					}
				}
				add = 30
			}
			if = { # Tribal/Nomadic rulers don't have a well-defined harem
				limit = {
					has_settled_harem_trigger = no
				}
				add = 75
			}
		}

		1 = consort_events.0030	# Hidden events - Consort learns of a courtier's secrets
		1 = consort_events.0031	# Hidden events - Consort learns of one of your known secrets
		1 = consort_events.0032	# Hidden events - Consort learns of one of your secrets
		2 = consort_events.0033	# Hidden events - Consort leaks a secret to a vassal
		52 = 0		# Having a "0" entry means that there is a chance no event fires, even if there are other valid events. Good for making sure that rare events don't always fire just because every other possible event is invalid.
	}
}
calyver_consort_influence_on_actions = {
	trigger = {
		government_has_flag = government_has_influence
	}
	random_events = {	# A single event will be picked to fire
		chance_to_happen = 50	# A percentage chance determining whether the events involved will be evaluated at all
		chance_of_no_event = {
			value = 0
			if = { # Consorts of AI rulers aren't particularly important
				limit = {
					is_ai = yes
					any_consort = {
						count < 6
					}
				}
				add = 30
			}
			if = { # Tribal/Nomadic rulers don't have a well-defined harem
				limit = {
					has_settled_harem_trigger = no
				}
				add = 75
			}
		}

		1 = consort_events.0035	# Hidden events - Influential consort gives influence to family
		5 = 0		# Having a "0" entry means that there is a chance no event fires, even if there are other valid events. Good for making sure that rare events don't always fire just because every other possible event is invalid.
	}
}
calyver_consort_schemes_on_actions = {
	random_events = {	# A single event will be picked to fire
		chance_to_happen = 50	# A percentage chance determining whether the events involved will be evaluated at all
		chance_of_no_event = {
			value = 0
			if = { # Consorts of AI rulers aren't particularly important
				limit = {
					is_ai = yes
					any_consort = {
						count < 6
					}
				}
				add = 30
			}
			if = { # Tribal/Nomadic rulers don't have a well-defined harem
				limit = {
					has_settled_harem_trigger = no
				}
				add = 75
			}
		}

		15 = consort_events.0036	# Hidden events - A consort murders an heir
		15 = consort_events.0027	# Hidden events - A consort causes a miscarriage
		15 = consort_events.0041	# Hidden events - A consort murders another consort
		45 = consort_events.0046	# Hidden events - A consort murders another consort - simpler version
		500 = consort_events.0047	# Hidden events - Cancel obsolete murder schemes
		150 = 0		# Having a "0" entry means that there is a chance no event fires, even if there are other valid events. Good for making sure that rare events don't always fire just because every other possible event is invalid.
	}
}

#The standard in game check can't be met if this mod is working properly (no faith will have doctrine_polygamy) so we can just add our own redirect to the stack
calyver_rabbinism_lose_polygamy_check = {
	effect = {
		# Rabbinicism becomes Monogamous
		if = {
			limit = {
				# Rabbi Gershom bans polygamy sometime in the 11th century
				current_date > "1000.01.01"
				# Make sure this hasn't happened already
				faith:rabbinism = {
					has_doctrine_parameter = allows_polygamy
					NOR = {
						has_variable = rabbinicism_accepted_monogamy
						has_variable = rabbinicism_rejected_monogamy
					}
				}
			}
			religion:judaism_religion = {
				every_faith = {
					limit = {
						has_doctrine_parameter = allows_polygamy
						has_doctrine = temple_authority_rabbinic_doctrine
					}

					if = {
						limit = {
							has_doctrine = doctrine_polygamy
						}
						set_variable = {
							name = faith_polygamy_doctrine
							value =  doctrine:doctrine_polygamy
						}
					}
					if = {
						limit = {
							has_doctrine = doctrine_polygamy_4
						}
						set_variable = {
							name = faith_polygamy_doctrine
							value =  doctrine:doctrine_polygamy_4
						}
					}
					if = {
						limit = {
							has_doctrine = doctrine_polygamy_10
						}
						set_variable = {
							name = faith_polygamy_doctrine
							value =  doctrine:doctrine_polygamy_10
						}
					}
					if = {
						limit = {
							has_doctrine = doctrine_polygamy_15
						}
						set_variable = {
							name = faith_polygamy_doctrine
							value =  doctrine:doctrine_polygamy_15
						}
					}
					if = {
						limit = {
							has_doctrine = doctrine_polygamy_25
						}
						set_variable = {
							name = faith_polygamy_doctrine
							value =  doctrine:doctrine_polygamy_25
						}
					}
					if = {
						limit = {
							has_doctrine = doctrine_polygamy_50
						}
						set_variable = {
							name = faith_polygamy_doctrine
							value =  doctrine:doctrine_polygamy_50
						}
					}
					if = {
						limit = {
							has_doctrine = doctrine_polygamy_100
						}
						set_variable = {
							name = faith_polygamy_doctrine
							value =  doctrine:doctrine_polygamy_100
						}
					}

					if = {
						limit = {
							has_doctrine = doctrine_marriage_realm_celebration_disallowed
							NOT = { has_doctrine_parameter = allows_concubinage }
						}
						set_variable = {
							name = faith_swap_realm_celebration
							value = yes
						}
					} 
					else = {
						set_variable = {
							name = faith_swap_realm_celebration
							value = no
						}
					}

					if = {
						limit = {
							exists = religious_head
						}
						if = {
							limit = {
								religious_head = { top_liege != this }
								religious_head.top_liege.faith = this
							}
							religious_head.top_liege = { trigger_event = jewish_events.0001 }
						}
						else_if = {
							limit = {
								religious_head = { top_liege != this }
								religious_head.liege.faith = this
							}
							religious_head.liege = { trigger_event = jewish_events.0001 }
						}
						else = {
							religious_head = { trigger_event = jewish_events.0001 }
						}
					}
					else_if = {
						limit = {
							any_player = { # Checking players is more performant than checking faith characters and limiting it to players
								faith = prev
							}
						}
						random_player = {
							limit = { faith = prev }
							trigger_event = jewish_events.0001
						}
					}
					else = {
						random_faith_character = {
							trigger_event = jewish_events.0001
						}
					}
				}
			}
		}
	}
}