﻿lmf_dynasty_control_story_cycle = {

	on_setup = {
		set_variable = { name = owner_dynasty value = story_owner.dynasty }
		set_variable = { name = countdown value = 10 }
		story_owner = {
			dynasty = {
				set_variable = { name = exempt_from_dynasty_control value = 1 years = 50 }
			}
		}
	}
	
	on_owner_death = {
		# first give it to the primary heir
		if = {
			limit = {
				story_owner = { primary_heir.dynasty ?= root.story_owner.dynasty }
			}
			make_story_owner = root.story_owner.primary_heir
		}
		# then the player heir, if different
		else_if = {
			limit = {
				story_owner = { player_heir.dynasty ?= root.story_owner.dynasty }
			}
			make_story_owner = root.story_owner.player_heir
		}
		# then give it to the eldest landed dynasty member
		else_if = {
			limit = {
				var:owner_dynasty = {
					any_dynasty_member = {
						NOT = { this = root.story_owner }
						is_landed = yes
					}
				}
			}
			var:owner_dynasty = {
				ordered_dynasty_member = {
					limit = {
						NOT = { this = root.story_owner }
						is_landed = yes
					}
					order_by = age
					save_scope_as = new_dynast
				}
			}
			make_story_owner = scope:new_dynast
		}
		# failing that, end the story
		else = {
			end_story = yes
		}
	}
	
	on_end = {
	}
	
	effect_group = {
		days = { 180 365 }
		first_valid = {
			# has the countdown run out?
			triggered_effect = {
				trigger = {
					OR = {
						NOT = { exists = var:countdown }
						NOT = { exists = var:owner_dynasty }
						var:countdown < 1
					}
				}
				effect = {
					end_story = yes
				}
			}
			# is there somehow no dynasty any longer?
			triggered_effect = {
				trigger = {
					NOT = { exists = root.var:owner_dynasty.dynast }
				}
				effect = {
					end_story = yes
				}
			}
			# has the story somehow fallen to the wrong dynasty?
			triggered_effect = {
				trigger = {
					story_owner = {
						OR = {
							has_dynasty = no
							NOT = { dynasty = root.var:owner_dynasty }
						}
					}
				}
				effect = {
					make_story_owner = root.var:owner_dynasty.dynast
				}
			}
			# set the regular effects in motion
			triggered_effect = {
				trigger = {
					always = yes
				}
				effect = {
					# first check if there are players in this dyansty
					if = {
						limit = {
							any_player = { dynasty = root.var:owner_dynasty }
						}
						every_player = {
							limit = { dynasty = root.var:owner_dynasty }
							add_to_list = dynasty_exceptions
							every_spouse = { add_to_list = dynasty_exceptions }
							player_heir ?= {
								add_to_list = dynasty_exceptions
								every_spouse = { add_to_list = dynasty_exceptions }
							}
						}
					}
					# reduce the countdown
					change_variable = { name = countdown add = -1 }
					# now do the effects
					var:owner_dynasty = {
						# pregnant female dynasty member in a matrilineal marriage has a greater chance of miscarriage/death
						random_dynasty_member = {
							limit = {
								is_female = yes
								has_trait = pregnant
								is_married = yes
								is_dominant_marriage_partner_of = { SPOUSE = primary_spouse }
								birth_will_happen_as_usual_trigger = yes
								NOR = {
									is_in_list = dynasty_exceptions
									AND = {
										is_courtier = yes
										liege = { is_in_list = dynasty_exceptions }
									}
								}
							}
							random_list = {
								30 = {
									# birth ends prematurely (unless birth happens first)
									remove_character_flag ?= birth_will_go_smoothly
									remove_character_flag ?= birth_child_will_become_sickly
									remove_character_flag ?= birth_mother_will_become_ill
									trigger_event = { id = pregnancy.2101 days = { 15 60 } }
								}
								10 = {
									# mother will die during childbirth
									remove_character_flag ?= birth_will_go_smoothly
									remove_character_flag ?= birth_child_will_become_sickly
									remove_character_flag ?= birth_mother_will_become_ill
									add_character_flag = { flag = birth_mother_will_die days = 270 }
								}
								20 = {
									# nothing changes
								}
							}
						}
						# dynasty member with a pregnant wife has a greater chance of miscarriage/death
						random_dynasty_member = {
							limit = {
								is_male = yes
								is_married = yes
								is_dominant_marriage_partner_of = { SPOUSE = primary_spouse }
								primary_spouse = {
									has_trait = pregnant
									birth_will_happen_as_usual_trigger = yes
								}
								NOR = {
									is_in_list = dynasty_exceptions
									AND = {
										is_courtier = yes
										liege = { is_in_list = dynasty_exceptions }
									}
								}
							}
							primary_spouse = {
								random_list = {
									30 = {
										# birth ends prematurely (unless birth happens first)
										remove_character_flag ?= birth_will_go_smoothly
										remove_character_flag ?= birth_child_will_become_sickly
										remove_character_flag ?= birth_mother_will_become_ill
										trigger_event = { id = pregnancy.2101 days = { 5 30 } }
									}
									10 = {
										# mother will die during childbirth
										remove_character_flag ?= birth_will_go_smoothly
										remove_character_flag ?= birth_child_will_become_sickly
										remove_character_flag ?= birth_mother_will_become_ill
										add_character_flag = { flag = birth_mother_will_die days = 270 }
									}
									20 = {
										# nothing changes
									}
								}
							}
						}
						# married adult who already has at least two dynasty children will stop having children
						while = {
							count = 2
							random_dynasty_member = {
								limit = {
									is_married = yes
									is_dominant_marriage_partner_of = { SPOUSE = primary_spouse }
									trigger_if = {
										limit = { is_female = yes }
										fertility > 0.10
									}
									NOR = {
										has_character_flag = lmf_no_children
										is_in_list = dynasty_exceptions
										has_relation_unfruitful_marriage = primary_spouse
										has_trait = pregnant
										primary_spouse = { has_trait = pregnant }
										AND = {
											is_courtier = yes
											liege = { is_in_list = dynasty_exceptions }
										}
									}
									any_child = {
										dynasty ?= root.var:owner_dynasty
										count > 1
									}
								}
								weight = {
									base = 1
									modifier = {
										add = highest_held_title_tier
										is_ruler = yes
									}
									modifier = {
										add = 2
										is_ruler = no
										any_heir_title = { tier >= tier_duchy }
									}
									modifier = {
										add = 1
										any_child = {
											dynasty ?= root.var:owner_dynasty
											count > 2
										}
									}
									modifier = {
										add = 1
										any_child = {
											dynasty ?= root.var:owner_dynasty
											count > 3
										}
									}
								}
								add_character_flag = lmf_no_children
							}
						}
						if = {
							limit = {
								any_dynasty_member = { count > 100 }
							}
							random_dynasty_member = {
								limit = {
									is_married = yes
									is_dominant_marriage_partner_of = { SPOUSE = primary_spouse }
									trigger_if = {
										limit = { is_female = yes }
										fertility > 0.10
									}
									NOR = {
										has_character_flag = lmf_no_children
										is_in_list = dynasty_exceptions
										has_relation_unfruitful_marriage = primary_spouse
										has_trait = pregnant
										primary_spouse = { has_trait = pregnant }
										AND = {
											is_courtier = yes
											liege = { is_in_list = dynasty_exceptions }
										}
									}
									any_child = {
										dynasty ?= root.var:owner_dynasty
										count > 1
									}
								}
								weight = {
									base = 1
									modifier = {
										add = highest_held_title_tier
										is_ruler = yes
									}
									modifier = {
										add = 2
										is_ruler = no
										any_heir_title = { tier >= tier_duchy }
									}
									modifier = {
										add = 1
										any_child = {
											dynasty ?= root.var:owner_dynasty
											count > 2
										}
									}
									modifier = {
										add = 1
										any_child = {
											dynasty ?= root.var:owner_dynasty
											count > 3
										}
									}
								}
								add_character_flag = lmf_no_children
							}
						}
						# chance of accidental death
						random_dynasty_member = {
							limit = {
								OR = {
									has_trait = sickly
									age >= 12
									is_ruler = yes
								}
								is_physically_able = yes
								trigger_if = {
									limit = {
										is_married = yes
										primary_spouse = {
											has_dynasty = yes
											dynasty != root.var:owner_dynasty
										}
									}
									is_dominant_marriage_partner_of = { SPOUSE = primary_spouse }
								}
								OR = {
									fertility > 0.10
									child_expected_to_be_fertile = yes
								}
								NOR = {
									has_trait = pregnant
									has_trait = devoted
									has_trait = order_member
									has_trait = eunuch
									has_character_flag = lmf_no_children
									is_in_list = dynasty_exceptions
									AND = {
										is_courtier = yes
										liege = { is_in_list = dynasty_exceptions }
									}
								}
							}
							weight = {
								base = 1
								modifier = {
									add = 3
									OR = {
										has_trait = ill
										has_trait = sickly
										has_serious_disease_trigger = yes
									}
								}
								modifier = {
									add = 2
									is_ruler = yes
								}
								modifier = {
									add = 1
									OR = {
										has_trait = faltering_heart
										has_trait = fragile_bones
									}
								}
								modifier = {
									add = 1
									is_ruler = no
									OR = {
										any_heir_title = {}
										is_preferred_gender_of_ruler_realm_law = { RULER = this }
									}
								}
								modifier = {
									add = -1
									is_ruler = no
									is_courtier = no
								}
							}
							random_list = {
								1 = {
									trigger = {
										is_adult = yes
										NOT = { has_trait = infirm }
									}
									death = { death_reason = death_horse_riding_accident }
								}
								1 = {
									death = { death_reason = death_fell_bedchamber }
								}
								1 = {
									death = { death_reason = death_drowned }
								}
								2 = {
									death = { death_reason = death_accident }
								}
								3 = {
									modifier = {
										add = 10
										has_trait = ill
									}
									trigger = {
										OR = {
											has_trait = ill
											has_serious_disease_trigger = no
										}
									}
									if = {
										limit = {
											NOT = { has_trait = ill }
										}
										add_trait = ill
									}
									death = { death_reason = death_ill }
								}
								100 = {
									trigger = { has_trait = sickly }
									death = { death_reason = death_sickly }
								}								
								10 = {
									trigger = { has_trait = cancer }
									death = { death_reason = death_cancer }
								}
								10 = {
									trigger = { has_trait = leper }
									death = { death_reason = death_leper }
								}
								10 = {
									trigger = { has_trait = typhus }
									death = { death_reason = death_typhus }
								}
								10 = {
									trigger = { has_trait = smallpox }
									death = { death_reason = death_smallpox }
								}
								100 = {
									trigger = { has_trait = bubonic_plague }
									death = { death_reason = death_bubonic_plague }
								}
								10 = {
									trigger = { has_trait = ergotism }
									death = { death_reason = death_ergotism }
								}
								10 = {
									trigger = { has_trait = measles }
									death = { death_reason = death_measles }
								}
								10 = {
									trigger = { has_trait = dysentery }
									death = { death_reason = death_dysentery }
								}
								10 = {
									trigger = { has_trait = pneumonic }
									death = { death_reason = death_pneumonic }
								}
								10 = {
									trigger = { has_trait = consumption }
									death = { death_reason = death_consumption }
								}
								5 = {
									trigger = { has_trait = fragile_bones }
									death = { death_reason = death_broken_bones }
								}
								5 = {
									trigger = { has_trait = faltering_heart }
									death = { death_reason = death_heart_attack }
								}
								3 = {
									trigger = { age >= 65 }
									death = { death_reason = death_old_age }
								}
								5 = {
									trigger = { stress >= high_stress }
									death = { death_reason = death_stress }
								}
								1 = {}
							}
						}					
					}
				}
			}
		}
	}
}

