﻿namespace = komnenos_invasion

### Spawn invader with the associated story...
komnenos_invasion.0001 = {
	scope = none
	hidden = yes
	
	trigger = {
		exists = title:e_byzantium # update compatibility - new titles in old saves return false
		title:e_byzantium = { is_title_created = yes }
	}

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

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

		title:e_byzantium.holder = {
			save_scope_as = invader
			trigger_event = {
				id = komnenos_invasion.0005
				days = 0
			}
		}
	}
}

# would you like to become an invader?
komnenos_invasion.0005 = {
	type = character_event
	title = komnenos_invasion.0005.t
	desc = {
		desc = komnenos_invasion.history_intro
		desc = komnenos_invasion.0005.desc
	}

	theme = hi_theme_farmlands

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

	trigger = {}

	immediate = {
		save_scope_as = invader
		faith = { save_scope_as = old_faith }

		# determine how many Orthodox counties (how much development) will be "looted" by converting to Iconoclasm
		set_variable = { # prevent error log spam
			name = dev_from_old_faith_counties_var
			value = 0
		}
		every_sub_realm_county = {
			limit = {
				faith = scope:old_faith
			}
			scope:invader = {
				change_variable = {
					name = dev_from_old_faith_counties_var
					add = prev.development_level
				}
			}
		}
	}

	option = { # accept; start invasion
		name = komnenos_invasion.0005.a
		add_internal_flag = special

		custom_tooltip = {
			text = komnenos_invasion.0005.a.tt
			create_story = story_komnenos_invasion
		}

		add_gold = {
			add = scope:invader.var:dev_from_old_faith_counties_var
			multiply = 5
			min = 0
			max = 2500
		}

		hidden_effect = { hi_aid_realm_faith_conversion_same_faith_effect = yes }
		set_character_faith_with_conversion = faith:iconoclast
		scope:invader.faith = { save_scope_as = new_faith }

		if = { # is immediately removed one day after?
			limit = { NOT = { has_trait = heresiarch } }
			add_trait = heresiarch
		}

		ai_chance = { base = 100 }
	}

	option = { # decline; skip invasion
		name = komnenos_invasion.0005.b

		add_piety = 50

		add_to_global_variable_list = {
			name = historicinvasions_story_started
			target = flag:komnenos_story_started
		}
		add_to_global_variable_list = {
			name = historicinvasions_story_ended
			target = flag:komnenos_story_ended
		}

		ai_chance = { base = 0 }
	}
}

# Event for handling army spawning
komnenos_invasion.0100 = {
	hidden = yes
	immediate = {
		spawn_komnenos_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
komnenos_invasion.0200 = {
	hidden = yes
	trigger = { # don't force a player into a war
		is_ai = yes
		is_at_war = no
	}
	immediate = {
		war_target_evaluation_and_declaration_effect = {
			INVADER_DYNN = komnenos
			INVADER_CHAR = scope:story.story_owner
			INVADER_REGION = 1
			INVADER_TARGET_TITLE_TIER = kingdom
			INVADER_RANGE = neighboring_and_across_water
			INVADER_LIEGE_RESTRICTIONS = ""
		}
	}
}

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

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

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

	trigger = {
		hi_show_invader_notification_event_trigger = yes
		NOT = { this = scope:invader }
	}

	immediate = {}
	
	option = { name = komnenos_invasion.1001.a }

	option = { # play as the newly spawned invader
		name = komnenos_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
	}
}