﻿# Called every Jan 1st
# Root does not exist

yearly_playable_pulse = {
    on_actions = {
        Population_Control_On_Action
    }
}

Population_Control_On_Action = {
	effect = {
		if = {
			limit = {
				NOT = {
					exists = global_var:there_was_death
				}
				exists = global_var:Population_Control_On
			}
			trigger_event = Population_Control.0001
			set_global_variable = {
				name = there_was_death
				value = 1
				days = 350
			} 
		}
	}
}

Population_Control_presetting = {
	effect = {
		remove_character_flag = Population_Control_target_is_on_succession_line
		if = { #LineOfSuccession_safe
			limit = {
				LineOfSuccession_safe = 1
				any_close_or_extended_family_member = {					
					any_held_title = {
						trigger_if = {
							limit = { NOT = { Barony_Safe = 1 } }
							tier >= tier_county							
						}
						place_in_line_of_succession = {
							target = root
							value <= LineOfSuccession_safe_limit
						}
					}					
				}
			}
			add_character_flag = {
				flag = Population_Control_target_is_on_succession_line
			}
		}

		trigger_event = {
			on_action = Population_Control_death
		}
		#trigger_event = { #for debug
		#	on_action = Population_Control_Debug #for debug
		#} #for debug
	}	
}

Population_Control_death = {
	trigger = {
		trigger_if = {
			limit = { any_player = {count >= 1} }
			any_player = {
				NOR = {
					AND = { #player safe
						player_safe = 1
						this = root
					}
					AND = { #player family safe
						player_family_safe = 1
						NOT = { this = root }
						OR = {
							is_close_family_of = root
							is_consort_of = root
							is_heir_of = root
							this = root.betrothed
							any_close_family_member = {				
								OR = {
									is_consort_of = root
									is_heir_of = root
									this = root.betrothed
								}						
							}							
						}						
					}
					AND = { #player court safe
						player_courtier_safe = 1
						NOT = { this = root }
						this = root.host							
					}
					AND = { #player of family's relation safe
						NOT = { this = root }
						OR = {
							AND = {
								player_relation_safe = 1
								has_any_scripted_relation = root							
							}
							AND = {
								player_family_relation_safe = 1
								OR = {
									any_close_family_member = {
										has_any_scripted_relation = root
									}
									any_heir = {
										has_any_scripted_relation = root
									}
								}
							}
						}	
					}
					AND = { #player scheme safe
						player_scheme_safe = 1
						NOT = { this = root }
						is_scheming_against = { target = root }
					}	
					AND = { #player dynasty safe
						player_dynasty_safe = 1
						NOT = { this = root }
						this.dynasty = {
							any_dynasty_member = {
								age <= 50
								age >= root.age
								count <= player_dynasty_safe_member_count
							}
							this = root.dynasty
						}
					}
					AND = {	#Pinned Character safe
						PinnedCharacter_safe = 1
						NOT = { this = root }
						any_pinned_character = { this = root }
					}
				}
			}
		}
		trigger_else = { always = yes }
		
		NOR = {	
			is_councillor = yes #Councillor safe
			AND = { #DynastyLastStand safe
				is_lowborn = no
				age <= 30
				root.faith = {
					has_dominant_ruling_gender = root
				}
				root.dynasty = {
					dynasty_prestige_level >= Dynasty_laststand_level
					any_dynasty_member = {		
						this.faith = {	has_dominant_ruling_gender = prev	}
						health >= 3
						age >= root.age
						age <= 30
						count <= Dynasty_laststand_members
					}
				}
				Dynasty_laststand_safe = 1
			}
			AND = { #Rulerorheir safe
				OR = {
					is_ruler = yes
					any_heir_title = {
						trigger_if = {
							limit = { NOT = { Barony_Safe = 1 } }
							tier >= tier_county								
						}
						count >= 1
					}
					AND = {
						age <= 50
						any_consort = {
							OR = {
								is_ruler = yes
								any_heir_title = {
									trigger_if = {
										limit = { NOT = { Barony_Safe = 1 } }
										tier >= tier_county								
									}
									count >= 1
								}					
							}
						}						
					}
					AND = { # Least Child safe						
						any_close_family_member	 = {
							trigger_if = {
								limit = { NOT = { Barony_Safe = 1 } }
								highest_held_title_tier >= tier_county								
							}							
							is_ruler = yes
							is_alive = yes					
						}
						trigger_if = {
							limit = {
								root.faith = { has_dominant_ruling_gender = root }
							}
							any_sibling = { #male
								is_alive = yes
								age >= root.age
								health >= 3
								this.faith = { has_dominant_ruling_gender = prev }
								count <= 1
							}
						}
						trigger_else = {
							any_sibling = { #female
								is_alive = yes
								age >= root.age
								health >= 3
								this.faith = { NOT = {has_dominant_ruling_gender = prev} }
								count <= 1
							}
						}
					}
				}
				Rulerorheir_safe = 1
			}
			AND = {	#LineOfSuccession safe
				OR = {
					has_character_flag  = Population_Control_target_is_on_succession_line
					AND = {
						age <= 50
						any_consort = {
							has_character_flag  = Population_Control_target_is_on_succession_line
						}
					}
				}				
				LineOfSuccession_safe = 1
			}

			AND = {	#Pure-blooded safe
				has_trait = pure_blooded
				PureBlooded_safe = 1
			}		
			
			AND = {	#Historical character safe
				has_trait = historical_character
				Historical_safe = 1
			}

			AND = {	#Adventurer safe
				is_landless_adventurer = yes
				Adventurer_safe = 1
			}


		}			

		trigger_if = { #AGOT Dragons safe
			limit = { 
				exists = global_var:AGOT_is_loaded
				AGOT_Dragons_safe = 1 
			}
			NOR = {
				OR = {
					has_trait = dragon
					has_trait = dragonrider
					has_trait = dragonless_dragonrider
					has_trait = dragonwidowed
					has_trait = dragonslayer
				}
			}
		}		
		trigger_else = { always = yes }
		
	}
	
	effect = {		
		#set death chance
		set_local_variable = {
			name = death_chance
			value = death_chance_base
		}

		if = {	limit = { age >= 75	}	change_local_variable = {	name = death_chance		multiply = m_ageover75		}	}		
		if = {	limit = { age >= 65	}	change_local_variable = {	name = death_chance		multiply = m_ageover65		}	}		
		if = {	limit = { age >= 55	}	change_local_variable = {	name = death_chance		multiply = m_ageover55		}	}

		if = {
			limit = { 				
				any_player = { count >= 1 }
				exists = this.location
				any_player = {					
					root.location={ squared_distance = { target = prev.capital_province value >= squared_distance_huge }}	
					count = all				
				}
			}
			change_local_variable = {	name = death_chance		multiply = m_islittlefar		}
		}	
		if = {
			limit = {
				any_player = { count >= 1 }
				exists = this.location
				any_player = {					
					root.location={ squared_distance = { target = prev.capital_province value >= squared_distance_massive }}		
					count = all			
				}
			}
			change_local_variable = {	name = death_chance		multiply = m_isfar		}
		}
		if = {
			limit = {
				any_player = { count >= 1 }
				exists = this.location
				any_player = {
					root.location={ squared_distance = { target = prev.capital_province value >= squared_distance_monstrous }}	
					count = all				
				}
			}
			change_local_variable = {	name = death_chance		multiply = m_isveryfar		}
		}

		if = {
			limit = {
				any_player = { count < 1 }
			}
			change_local_variable = {	name = death_chance		multiply = m_isveryfar		}
		}

		if = {	limit = { health <= 3	}	change_local_variable = {	name = death_chance		multiply = m_littleweakhealth		}	}		
		if = {	limit = { health <= 2	}	change_local_variable = {	name = death_chance		multiply = m_weakhealth		}	}		
		if = {	limit = { health <= 1	}	change_local_variable = {	name = death_chance		multiply = m_veryweakhealth		}	}	
		if = {	limit = { is_imprisoned = yes	}	change_local_variable = {	name = death_chance		multiply = m_isimprisoned		}	}	

		random = {
			chance = local_var:death_chance

			#Set their parents to infertility.
			every_parent = {
				add_character_modifier = {
					modifier = Population_Control_Child_Died_modifier
					days = 3650
				}		
			}

			if = {
				limit = {
					StressNotGained = 1
				}
				every_close_family_member = {
					add_character_modifier = {
						modifier = Population_Control_Stress_Mult_modifier
						days = 3
					}	
				}
				every_relation = {
					add_character_modifier = {
						modifier = Population_Control_Stress_Mult_modifier
						days = 3
					}	
				}
				every_consort = {
					add_character_modifier = {
						modifier = Population_Control_Stress_Mult_modifier
						days = 3
					}
				}
			}
			
			if = {
				limit = { DeathReason = 0 }
				death = {
					death_reason = death_vanished
				}
			}
			else_if = {
				limit = { DeathReason = 1 }
				random_list = {
					10 = {
						death = {
							death_reason = death_bubonic_plague
						}
					}
					10 = {
						death = {
							death_reason = death_smallpox
						}
					}
					10 = {
						death = {
							death_reason = death_typhus
						}
					}
					10 = {
						death = {
							death_reason = death_cancer
						}
					}
					10 = {
						death = {
							death_reason = death_pneumonic
						}
					}
				}
			}
			else_if = {
				limit = { DeathReason = 2 }
				death = {
					death_reason = death_bubonic_plague
				}
			}
			else = {
				random_list = {
					10 = {
						death = natural
					}
					10 = {
						death = {
							death_reason = death_murder
						}
					}
					10 = {
						death = {
							death_reason = death_accident
						}
					}
					10 = {
						death = {
							death_reason = death_script_cruelty
						}
					}
					10 = {
						death = {
							death_reason = death_disappearance
						}
					}
					10 = {
						death = {
							death_reason = death_head_ripped_off
						}
					}
					10 = {
						death = {
							death_reason = death_mysterious
						}
					}
					10 = {
						death = {
							death_reason = death_execution_blot
						}
					}
					10 = {
						death = {
							death_reason = death_eaten
						}
					}
					10 = {
						death = {
							death_reason = death_sun_trial
						}
					}
					10 = {
						death = {
							death_reason = death_literalist_debate
						}
					}
				}
			}
			


		}
		change_global_variable = {
			name = minwipecount
			add = -1
		}
		if = {
			limit = {
				global_var:minwipecount <= 0
			}
			remove_global_variable = wipestart				
		}
	}
	
}	