﻿namespace = zunbil_invasion

### Spawn invader with the associated story...
zunbil_invasion.0001 = {
	scope = none
	hidden = yes
	
	trigger = {
		exists = title:k_zunbil # update compatibility - new titles in old saves return false
		any_county_in_region = {
			region = special_zunbil_conquest_region_start
			count > 0
			holder = { is_ai = yes }
		}
	}

	on_trigger_fail = {
		debug_log = "zunbil story has failed to trigger; trying again next year"
		debug_log_date = yes
	}

	immediate = {
		debug_log = "zunbil story owner has appeared"
		debug_log_date = yes

		character:188700 = { save_scope_as = barhatakin_zunbil } # dynasty root
		character:188701 = { save_scope_as = rutbil_zunbil }
		character:188716 = { save_scope_as = zere_zunbil }

		scope:rutbil_zunbil.dynasty = {
			# pick an already existing dynasty member
			ordered_dynasty_member = {
				limit = {
					is_landed = yes
					highest_held_title_tier > tier_barony
					capital_county = { title_province = { geographical_region = special_zunbil_conquest_region_total } }
					is_independent_ruler = yes
					is_adult = yes
					is_healthy = yes
					is_imprisoned = no
					historicinvasions_player_may_become_invader_trigger = yes
					historicinvasions_common_existing_character_restrictions_trigger = yes
				}
				alternative_limit = {
					is_landed = yes
					highest_held_title_tier > tier_barony
					capital_county = { title_province = { geographical_region = special_zunbil_conquest_region_total } }
					is_adult = yes
					is_healthy = yes
					is_imprisoned = no
					historicinvasions_player_may_become_invader_trigger = yes
					historicinvasions_common_existing_character_restrictions_trigger = yes
				}
				order_by = realm_size
				save_scope_as = invader
			}
		}
		
		# only spawns a new character if scope:invader is not set
		spawn_zunbil_invader_effect = {
			INVADER_DYNN = zunbil
		}

		# start story
		scope:invader = {
			create_story = story_zunbil_invasion
		}
	}
}

# Event for handling army spawning
zunbil_invasion.0100 = {
	hidden = yes
	immediate = {
		spawn_zunbil_troops_effect = yes
	}
}

# Event for declaring the very first war; values like current_ / max_military_strength aren't immediately filled with a value and return 0 instead
zunbil_invasion.0200 = {
	hidden = yes
	trigger = { is_ai = yes } # don't force a player into a war
	immediate = {
		war_target_evaluation_and_declaration_effect = {
			INVADER_DYNN = zunbil
			INVADER_CHAR = scope:story.story_owner
			INVADER_REGION = total
			INVADER_TARGET_TITLE_TIER = duchy
			INVADER_RANGE = neighboring
			INVADER_LIEGE_RESTRICTIONS = ""
		}
	}
}

##################
# Notification Events
# 1000-1999
##################

### The invader has appeared! Would you like to play as them?
zunbil_invasion.1001 = { # only players see this
	type = character_event
	title = zunbil_invasion.1001.t
	desc = zunbil_invasion.1001.desc
	
	theme = hi_theme_forest

	left_portrait = {
		character = scope:invader
		animation = personality_bold
	}

	trigger = { hi_show_invader_notification_event_trigger = yes }

	immediate = {
		character:188700 = { save_scope_as = barhatakin_zunbil } # dynasty root
		character:188701 = { save_scope_as = rutbil_zunbil } # frist Zunbil ruler
		character:188717 = { save_scope_as = tegin_nandin } # brother of ^, ruler of Hephthalite kingdom
		character:188711 = { save_scope_as = kabak_zunbil } # last Zunbil duke

		character:163163 = { save_scope_as = laith_al-saffar } # Saffarid conqueror of Zabulistan
	}
	
	option = { name = zunbil_invasion.1001.a }

	option = { # play as the newly spawned invader
		name = zunbil_invasion.1001.b
		trigger = { hi_play_as_invader_event_option_trigger = yes }
		add_internal_flag = dangerous
		scope:invader = {
			set_variable = {
				name = historicinvasions_previous_ruler_var
				value = root
				months = 1
			}
		}
		set_player_character = scope:invader
	}
}