﻿on_death = {
	on_actions = {
		succession_crisis_pulse
	}
}

# ============================================================
# SUCCESSION CRISIS PULSE
# ============================================================
succession_crisis_pulse = {
	trigger = {
		is_landed = yes
		highest_held_title_tier >= tier_kingdom
		is_eligible_for_succession_crisis = yes
		exists = primary_heir
		primary_heir = {
			is_eligible_for_succession_crisis = yes
		}
	}

	effect = {
		save_scope_as = dead_predecessor
		primary_title = { save_scope_as = crisis_title }
		
		primary_heir = { 
			save_scope_as = crisis_heir 
			location = { save_scope_as = heir_original_location }
		}

		if = {
			limit = {
				scope:dead_predecessor = {
					has_character_flag = active_succession_crisis
					OR = {
						any_character_war = {
							primary_defender = scope:dead_predecessor
							using_cb = succession_crisis_cb
						}
						any_character_war = {
							primary_attacker = scope:dead_predecessor
							using_cb = succession_crisis_cb
						}
					}
				}
			}

			if = {
				limit = {
					scope:dead_predecessor = {
						has_character_flag = active_succession_crisis
					}
				}
				scope:crisis_heir = {
					trigger_event = { id = succession_crisis_claimant_event.0009 days = 1 }
				}
			}
			else = {
				scope:crisis_heir = {
					trigger_event = { id = succession_crisis_heir_event.0009 days = 1 }
				}
			}
		}
		else = {
			scope:crisis_heir = {
				add_character_flag = crisis_heir_flag
				set_variable = {
					name = crisis_title
					value = scope:crisis_title
				}
			}
			
			capital_province = { save_scope_as = crisis_destination }

			if = {
				limit = {
					scope:crisis_title = { tier = tier_hegemony }
					scope:dead_predecessor = { has_government = celestial_government }

					exists = scope:crisis_heir
					scope:crisis_heir = {
						is_alive = yes
						is_ai = yes
						location = scope:crisis_destination
					}
				}

				scope:crisis_heir = {
					scope:dead_predecessor = {
						random_vassal = {
							limit = {
								NOT = { this = scope:crisis_heir }
							}

							capital_province = {
								save_scope_as = heir_relocation_destination
							}
						}
					}

					set_location = scope:heir_relocation_destination
				}
			}

			if = {
				limit = {
					scope:crisis_title = { tier = tier_hegemony }
					scope:dead_predecessor = { has_government = celestial_government }
				}

				if = {
					limit = {
						scope:crisis_title = {
							any_claimant = {
								has_claim_on = scope:crisis_title
								has_character_flag = crisis_special_character
								location = scope:crisis_destination
								is_ai = yes
								NOT = { this = scope:crisis_heir }
								OR = {
									AND = {
										is_ruler = yes
										highest_held_title_tier >= tier_county
									}
									house = root.house
								}
								NOT = {
									AND = {
										house = root.house
										is_female = yes
									}
								}
							}
						}
					}

					scope:crisis_title = {
						random_claimant = {
							limit = {
								has_claim_on = scope:crisis_title
								is_eligible_for_succession_crisis = yes
								has_character_flag = crisis_special_character
								location = scope:crisis_destination
								is_ai = yes
								NOT = { this = scope:crisis_heir }
								OR = {
									AND = {
										is_ruler = yes
										highest_held_title_tier >= tier_county
									}
									house = root.house
								}
								NOT = {
									AND = {
										house = root.house
										is_female = yes
									}
								}
							}

							save_scope_as = relocating_claimant

							scope:dead_predecessor = {
								random_vassal = {
									limit = {
										NOT = { this = scope:crisis_heir }
										NOT = { this = scope:relocating_claimant }
									}

									capital_province = {
										save_scope_as = special_character_destination
									}
								}
							}

							set_location = scope:special_character_destination
						}
					}
				}

				else = {
					scope:crisis_title = {
						random_claimant = {
							limit = {
								has_claim_on = scope:crisis_title
								is_eligible_crisis_claimant = yes
								is_eligible_for_succession_crisis = yes
								NOT = { this = scope:crisis_heir }
								NOT = { this = root }
								location = scope:crisis_destination
								is_ai = yes
								OR = {
									AND = {
										is_ruler = yes
										highest_held_title_tier >= tier_county
									}
									house = root.house
								}
								NOT = {
									AND = {
										house = root.house
										is_female = yes
									}
								}
							}

							save_scope_as = relocating_claimant

							scope:dead_predecessor = {
								random_vassal = {
									limit = {
										NOT = { this = scope:crisis_heir }
										NOT = { this = scope:relocating_claimant }
									}

									capital_province = {
										save_scope_as = special_character_destination
									}
								}
							}

							set_location = scope:special_character_destination
						}
					}
				}
			}

			if = {
				limit = {
					scope:crisis_title = { tier = tier_hegemony }
					scope:dead_predecessor = { has_government = celestial_government }
				}

				scope:crisis_title = {
					random_claimant = {
						limit = {
							has_claim_on = scope:crisis_title
							is_eligible_crisis_claimant = yes
							is_eligible_for_succession_crisis = yes
							NOT = { this = scope:crisis_heir }
							NOT = { this = root }
							OR = {
								AND = {
									is_ruler = yes
									highest_held_title_tier >= tier_county
								}
								house = root.house
							}
							NOT = {
								AND = {
									house = root.house
									is_female = yes
								}
							}
						}

						save_scope_as = relocating_claimant

						scope:dead_predecessor = {
							random_vassal = {
								limit = {
									NOT = { this = scope:crisis_heir }
									NOT = { this = scope:relocating_claimant }
								}

								capital_province = {
									save_scope_as = random_claimant_destination
								}
							}
						}

						set_location = scope:random_claimant_destination
					}
				}
			}

			if = {
				limit = {
					exists = scope:crisis_heir
					scope:crisis_heir = { is_alive = yes }
				}

				scope:crisis_heir = {
					if = {
						limit = { exists = scope:crisis_heir }
						scope:crisis_heir = {
							location = { save_scope_as = heir_original_location }
						}
					}

					if = {
						limit = { exists = scope:crisis_title }
						scope:crisis_title = {
							every_claimant = {
								limit = {
									has_claim_on = scope:crisis_title
									is_eligible_crisis_claimant = yes
									is_eligible_for_succession_crisis = yes
									NOT = { this = scope:crisis_heir }
									NOT = { this = root }
									
									NOT = { location = scope:crisis_destination }
									NOT = { location = scope:heir_original_location }
									OR = {
										AND = {
											is_ruler = yes
											highest_held_title_tier >= tier_county
										}
										house = root.house
									}
									NOT = {
										AND = {
											house = root.house
											is_female = yes
										}
									}
								}

								add_to_list = potential_rebels
							}
						}
					}

					if = {
						limit = {
							OR = {
								NOT = {
									any_held_title = {
										tier >= scope:crisis_title.tier
										NOT = { this = scope:crisis_title }
									}
								}
								any_in_list = { list = potential_rebels }
							}
						}

						if = {
							limit = {
								any_in_list = {
									list = potential_rebels
									has_character_flag = crisis_special_character
								}
							}

							random_in_list = {
								list = potential_rebels
								limit = { has_character_flag = crisis_special_character }
								save_scope_as = primary_claimant
							}
						}
						else = {
							random_in_list = {
								list = potential_rebels
								weight = {
									base = 100

									modifier = {
										add = 100
										max_military_strength >= 3000
									}

									modifier = {
										add = 500
										house = scope:dead_predecessor.house
									}

									modifier = {
										factor = 0.1
										is_female = yes
									}

									modifier = {
										add = 10000
										is_ai = no
									}
								}

								save_scope_as = primary_claimant
							}
						}
						
						if = {
							limit = { exists = scope:primary_claimant }

							scope:primary_claimant = {
								add_to_list = active_crisis_rebels
								add_character_flag = active_crisis_rebel
								set_variable = {
									name = crisis_title
									value = scope:crisis_title
								}

								if = {
									limit = {
										is_landless_adventurer = yes
										is_ai = yes
									}
									scope:dead_predecessor = {
										random_vassal = {
											limit = {
												NOT = { this = scope:crisis_heir }
											}
											capital_province = {
												save_scope_as = landless_claimant_destination
											}
										}
									}
									set_location = scope:landless_claimant_destination
								}
							}
							scope:crisis_title = {
								set_variable = {
									name = primary_claimant
									value = scope:primary_claimant
								}
							}
						}

						calculate_succession_instability_points = yes

						if = {
							limit = {
								var:reasons_for_crisis < 4
							}

							add_character_flag = race_participant
							trigger_event = { id = succession_crisis_heir_event.0007 days = 1 }
						}
						else_if = {
							limit = {
								var:reasons_for_crisis >= 4
								any_in_list = { list = active_crisis_rebels }
							}

							add_character_flag = race_participant

							every_in_list = {
								list = active_crisis_rebels
								add_character_flag = race_participant
							}

							if = {
								limit = {
									exists = scope:crisis_heir
									exists = scope:crisis_destination
									scope:crisis_heir = { location = scope:crisis_destination }
								}

								scope:crisis_heir = {
									add_character_flag = crisis_race_winner
									trigger_event = { id = succession_crisis_arrival.0001 days = 1 }
								}
							}

							if = {
								limit = {
									exists = scope:crisis_heir
									NOT = { scope:crisis_heir = { location = scope:crisis_destination } }
								}

								scope:crisis_heir = {
									trigger_event = { id = succession_crisis_heir_event.0001 days = 1 }
								}
							}

							every_in_list = {
								list = active_crisis_rebels

								if = {
									limit = {
										exists = scope:crisis_heir
										exists = scope:crisis_destination
										scope:crisis_heir = { location = scope:crisis_destination }
									}

									save_scope_as = claimant
									trigger_event = { id = succession_crisis_claimant_event.0004 days = 1 }
								}
								else_if = {
									limit = {
										location = scope:crisis_destination
									}

									add_character_flag = crisis_race_winner
									trigger_event = { id = succession_crisis_arrival.0001 days = 1 }
								}
								else = {
									trigger_event = { id = succession_crisis_claimant_event.0001 days = 1 }
								}
							}
						}
						else = {
							add_character_flag = race_participant
							trigger_event = { id = succession_crisis_heir_event.0007 days = 1 }
						}
					}
					else = {
						scope:crisis_heir = {
							add_character_flag = race_participant
							add_character_flag = crisis_race_winner
							trigger_event = { id = succession_crisis_arrival.0001 days = 1 }
						}
					}
				}
			}
		}
	}
}