﻿namespace = succession_crisis_arrival

# ============================================================
# EVENT: ARRIVAL ROUTER
# ============================================================

succession_crisis_arrival.0001 = {
	type = character_event
	hidden = yes

	immediate = {
		scope:crisis_title = {
			holder = {
				save_scope_as = current_ruler
			}
		}

		location = {
			save_scope_as = crisis_destination
		}

		if = {
			limit = {
				NOT = { this = scope:current_ruler }
			}
			save_scope_as = claimant
		}

		if = {
			limit = {
				has_character_flag = race_participant
			}
			if = {
				limit = {
					NOT = {
						any_in_list = {
							list = active_crisis_rebels
							has_character_flag = crisis_race_winner
						}
					}
					NOT = {
						scope:crisis_heir = { has_character_flag = crisis_race_winner }
					}
				}
				add_character_flag = crisis_race_winner

				if = {
					limit = {
						OR = {
							NOT = { this = scope:current_ruler }
							NOT = { scope:current_ruler.capital_county = scope:crisis_destination.county }
						}
					}
				}

				if = {
					limit = { this = scope:current_ruler }
					if = {
						limit = {
							var:reasons_for_crisis >= 4
							any_in_list = {
								list = active_crisis_rebels
							}
						}
						trigger_event = { id = succession_crisis_heir_event.0002 days = 1 }
					}
					else = {
						trigger_event = { id = succession_crisis_heir_event.0005 days = 1 }
					}
					remove_character_flag = race_participant
				}
				else = {
					trigger_event = { id = succession_crisis_claimant_event.0002 days = 1 }
					remove_character_flag = race_participant
				}
			}
			else = {
				remove_character_flag = race_participant
			}
		}
	}
}
# ============================================================
# EVENT: ARRIVAL ROUTER CONSOLIDAITON
# ============================================================

succession_crisis_arrival.0002 = {
	hidden = yes

	immediate = {
		save_scope_as = asker

		if = {
			limit = {
				has_character_flag = visiting_vassal
			}
			
			if = {
				limit = { exists = current_travel_plan }
				current_travel_plan = {
					pause_travel_plan = yes
				}
			}

			if = {
				limit = {
					scope:selected_consolidation_vassal = { is_ai = no }
				}
				scope:selected_consolidation_vassal = {
					trigger_event = { id = succession_crisis_vassal.0002 }
				}
			}
			else = {
				if = {
					limit = {
						scope:selected_consolidation_vassal = {
							opinion = {
								target = root
								value > 30
							}
						}
					}
					trigger_event = { id = sc_power_consolidation.0002 }
				}
				else = {
					trigger_event = { id = sc_power_consolidation.0003 }
				}
			}
		}
	}
}