﻿namespace = historicinvasions_event

### GAME OVER prevention notification
historicinvasions_event.1000 = {
	type = letter_event
	opening = historicinvasions_event.1000.opening
	desc = historicinvasions_event.1000.desc
	sender = scope:attacker

	# accept gift, receive troops
	option = {
		name = historicinvasions_event.1000.a
		custom_tooltip = historicinvasions_event.1000.a.tt
	}

	# refuse gift, become landless wanderer
	option = {
		trigger = { always = no }
		add_internal_flag = dangerous
		name = historicinvasions_event.1000.b
	}
}

##### Demands for Subjugation #####
# inspired by Petter Vilberg, adjusted by me over time

### Event Structure:
# 2001: invader demands subjugation of demand_recipient (non-ally)
# 2002: subjugation accepted
# 2003: subjugation refused -> war declaration

# 2101: invader demands subjugation of demand_recipient (ally)
# 2102: subjugation accepted
# 2103: subjugation refused -> alliance broken

# related: 'surrender_to_invader_decision'


##############################
# Demands for Subjugation (non-allies)
##############################

# invader demands subjugation
historicinvasions_event.2001 = {
	type = letter_event
	opening = historicinvasions_event.2001.opening
	desc = historicinvasions_event.2001.desc
	sender = {
		character = scope:story.story_owner
		animation = personality_bold
	}

	trigger = {
		NOR = {
			is_at_war_with = scope:story.story_owner
			root = scope:story.story_owner
		}
	}

	immediate = {
		save_scope_as = demand_recipient
		
		# Ayyubid truce purchase; base game localization
		save_scope_as = actor
		scope:story.story_owner = { save_scope_as = recipient }
	}

	# submit to invader
	option = {
		# dynamic option name
		name = {
			trigger = {
				highest_held_title_tier > scope:story.story_owner.primary_title.tier
			}
			text = historicinvasions_event.2001.a.higher
		}
		name = {
			trigger = {
				highest_held_title_tier = scope:story.story_owner.primary_title.tier
			}
			text = historicinvasions_event.2001.a.equal
		}
		name = {
			trigger = {
				highest_held_title_tier < scope:story.story_owner.primary_title.tier
			}
			text = historicinvasions_event.2001.a.lower
		}

		# option restrictions
		trigger = {
			trigger_if = { # perhaps limit it so a certain realm size? consider HoF vassals?
				limit = {
					is_ai = yes
					highest_held_title_tier > scope:story.story_owner.primary_title.tier
				}
				always = no
			}
		}

		# tooltips explaining what is about to happen if this option is picked
		if = { # higher tier
			limit = {
				highest_held_title_tier > scope:story.story_owner.primary_title.tier
			}
			custom_tooltip = historicinvasions_event.2001.a.higher_tt
		}
		else_if = { # equal tier
			limit = {
				highest_held_title_tier = scope:story.story_owner.primary_title.tier
			}
			custom_tooltip = historicinvasions_event.2001.a.equal_tt
		}
		else = { # lower tier
			custom_tooltip = historicinvasions_event.2001.a.lower_tt
		}
		
		# HoF destruction tooltip warning
		if = {
			limit = {
				any_held_title = {
					is_head_of_faith = yes
					tier >= scope:story.story_owner.primary_title.tier
				}
			}
			custom_tooltip = surrender_to_invader_decision_hof_destruction_tt
		}

		# apply vassalage effect
		hidden_effect = {
			scope:story.story_owner = {
				trigger_event = historicinvasions_event.2002
			}
		}

		# truce
		add_truce_both_ways = {
			character = scope:story.story_owner
			override = yes
			years = historicinvasions_swear_fealty_truce_value
			name = TRUCE_SWEAR_FEALTY
		}

		# AI logic
		ai_chance = {
			base = 0
			modifier = { add = historicinvasions_ai_subjugation_accept_value }
		}
	}

	# resist and fight
	option = {
		name = historicinvasions_event.2001.b
		add_internal_flag = dangerous

		# which titles will the invader attempt to conquer?
		custom_tooltip = historicinvasions_event.2001.b.tt

		# break alliance if an alliance has saved this character from an invasion before
		if = {
			limit = {
				is_allied_to = scope:story.story_owner
				has_character_flag = delayed_invasion_via_broken_alliance_flag
			}
			break_alliance = scope:story.story_owner
		}

		# trigger war declaration
		hidden_effect = {
			scope:story.story_owner = {
				trigger_event = historicinvasions_event.2003
			}
		}

		# AI logic
		ai_chance = {
			base = 0
			modifier = { add = historicinvasions_ai_subjugation_refuse_value }
			modifier = { add = 1 }
		}
	}

	# resist, disrespect Genghis Khan and fight
	option = {
		name = historicinvasions_event.2001.d
		add_internal_flag = dangerous
		trigger = {
			scope:story.story_owner = { primary_title = title:e_mongol_empire }
		}
		# this might have consequences...
		custom_tooltip = historicinvasions_event.2001.d.tt
		# which titles will the invader attempt to conquer?
		custom_tooltip = historicinvasions_event.2001.b.tt

		# break alliance if an alliance has saved this character from an invasion before
		if = {
			limit = {
				is_allied_to = scope:story.story_owner
				has_character_flag = delayed_invasion_via_broken_alliance_flag
			}
			break_alliance = scope:story.story_owner
		}

		# trigger war declaration
		hidden_effect = {
			scope:story.story_owner = {
				trigger_event = historicinvasions_event.2003
			}
			add_character_flag = insulted_mongol_emperor_flag
		}

		add_prestige = 250

		# AI logic
		ai_chance = {
			base = 0
			modifier = { add = historicinvasions_ai_subjugation_refuse_value }
			modifier = { factor = 0.01 }
			modifier = { add = 1 }
		}
	}

	# Jerusalem buys truce from Ayyubid Sultanate
	option = {
		name = historicinvasions_event.2001.e
		add_internal_flag = special
		trigger = {
			scope:story.story_owner = {
				any_owned_story = { story_type = story_ayyubid_invasion }
			}
			scope:demand_recipient = {
				religion = religion:christianity_religion
				is_independent_ruler = yes
				primary_title = title:k_jerusalem
				highest_held_title_tier = tier_kingdom
				realm_size < 12
			}
			NOT = { title:k_jerusalem = { has_variable = negotiated_truce_with_ayyubid_var } }
		}

		pay_short_term_gold = {
			target = scope:story.story_owner
			gold = {
				value = 50
				multiply = realm_size
			}
		}

		if = {
			limit = { scope:demand_recipient = { has_trait = cancer } }
			add_truce_both_ways = {
				character = scope:story.story_owner
				override = yes
				years = 100
				name = TRUCE_PURCHASED
			}
		}
		else = {
			add_truce_both_ways = {
				character = scope:story.story_owner
				override = yes
				years = 7
				name = TRUCE_PURCHASED
			}
		}

		# only once
		title:k_jerusalem = {
			set_variable = {
				name = negotiated_truce_with_ayyubid_var
				value = yes
			}
		}

		# remove from the list blocking war declarations via character interaction
		scope:story.story_owner = {
			remove_list_variable = {
				name = subjugation_offer_under_consideration
				target = scope:demand_recipient
			}
		}

		# AI logic
		ai_chance = {
			base = 0
			modifier = { add = historicinvasions_ai_subjugation_refuse_value }
			modifier = { add = 1 }
			modifier = { factor = 100 }
		}
	}
}

# subjugation accepted
# similar to .2102
# (root = invader character)
historicinvasions_event.2002 = {
	type = letter_event
	opening = historicinvasions_event.2002.opening
	desc = historicinvasions_event.2002.desc
	sender = scope:demand_recipient

	immediate = {
		hi_apply_subjugation_demand_effect = yes
	}

	after = {
		# remove from the list blocking war declarations via character interaction
		remove_list_variable = {
			name = subjugation_offer_under_consideration
			target = scope:demand_recipient
		}
	}

	# wise choice
	option = {
		name = historicinvasions_event.2002.a
	}
}

# subjugation refused -> war declaration
# (root = invader character)
historicinvasions_event.2003 = {
	type = letter_event
	opening = historicinvasions_event.2003.opening
	desc = historicinvasions_event.2003.desc
	sender = {
		character = scope:demand_recipient
		animation = personality_bold
	}

	immediate = {}

	after = {
		# remove from the list blocking war declarations via character interaction
		remove_list_variable = {
			name = subjugation_offer_under_consideration
			target = scope:demand_recipient
		}
	}

	# war declaration
	option = {
		name = historicinvasions_event.2003.a
		start_war = {
			cb = historicinvasions_invasion_war
			target = scope:demand_recipient
			target_title = scope:next_invasion_target_title
		}
		ai_chance = {
			base = 100
		}
	}
}


######################################
# Demands for Subjugation (allies)
# similar to the previous event chains, except that these are not part of the 'subjugation_offer_under_consideration' list blocking war declarations
######################################

# invader demands subjugation from an ally
# vaguely similar to .2001
historicinvasions_event.2101 = {
	type = letter_event
	opening = historicinvasions_event.2001.opening
	desc = historicinvasions_event.2101.desc
	sender = {
		character = scope:story.story_owner
		animation = personality_bold
	}

	trigger = {
		NOT = { root = scope:story.story_owner }
	}
	
	immediate = {
		save_scope_as = demand_recipient
	}

	# submit to invader
	option = {
		name = historicinvasions_event.2101.a
		trigger = { always = yes }

		# tooltips explaining what is about to happen if this option is picked
		if = { # higher tier
			limit = {
				highest_held_title_tier > scope:story.story_owner.primary_title.tier
			}
			custom_tooltip = historicinvasions_event.2001.a.higher_tt
		}
		else_if = { # equal tier
			limit = {
				highest_held_title_tier = scope:story.story_owner.primary_title.tier
			}
			custom_tooltip = historicinvasions_event.2001.a.equal_tt
		}
		else = { # lower tier
			custom_tooltip = historicinvasions_event.2001.a.lower_tt
		}

		# HoF destruction warning
		if = {
			limit = {
				any_held_title = {
					is_head_of_faith = yes
					tier >= scope:story.story_owner.primary_title.tier
				}
			}
			custom_tooltip = surrender_to_invader_decision_hof_destruction_tt
		}

		# apply vassalage effect
		hidden_effect = {
			scope:story.story_owner = {
				trigger_event = historicinvasions_event.2102
			}
		}

		# truce
		add_truce_both_ways = {
			character = scope:story.story_owner
			override = yes
			years = historicinvasions_swear_fealty_truce_value
			name = TRUCE_SWEAR_FEALTY
		}

		# AI logic
		ai_chance = {
			base = 0
			modifier = { add = historicinvasions_ai_subjugation_accept_value }
		}
	}

	# refuse -> alliance broken
	option = {
		name = historicinvasions_event.2101.b
		add_internal_flag = special
		custom_tooltip = historicinvasions_event.2101.b.tt

		# tooltip
		show_as_tooltip = {
			break_alliance = scope:story.story_owner
		}

		# apply broken alliance
		hidden_effect = {
			scope:story.story_owner = {
				trigger_event = historicinvasions_event.2103
			}
		}

		# AI logic
		ai_chance = {
			base = 0
			modifier = { add = historicinvasions_ai_subjugation_refuse_value }
		}
	}
}

# subjugation accepted by ally
# similar to .2002
# (root = invader character)
historicinvasions_event.2102 = {
	type = letter_event
	opening = historicinvasions_event.2002.opening
	desc = historicinvasions_event.2002.desc
	sender = scope:demand_recipient

	immediate = {
		hi_apply_subjugation_demand_effect = yes
	}

	after = {
		# remove from the list blocking war declarations via character interaction; should not be in here, but safety first
		remove_list_variable = {
			name = subjugation_offer_under_consideration
			target = scope:demand_recipient
		}
	}

	# wise choice
	option = {
		name = historicinvasions_event.2002.a
	}
}

# subjugation refused by ally -> alliance broken, no war
# (root = invader character)
historicinvasions_event.2103 = {
	type = letter_event
	opening = historicinvasions_event.2003.opening
	desc = historicinvasions_event.2003.desc
	sender = {
		character = scope:demand_recipient
		animation = personality_bold
	}

	immediate = {}

	after = {
		# remove from the list blocking war declarations via character interaction; should not be in here, but safety first
		remove_list_variable = {
			name = subjugation_offer_under_consideration
			target = scope:demand_recipient
		}
	}

	# no war declaration
	option = {
		name = historicinvasions_event.2103.a

		break_alliance = scope:demand_recipient
		scope:demand_recipient = {
			add_character_flag = {
				flag = delayed_invasion_via_broken_alliance_flag
				years = 20
			}
		}

		# opinion & rivalry
		add_opinion = {
			modifier = refused_alliance_opinion
			target = scope:demand_recipient
		}
		if = {
			limit = {
				NOR = {
					has_relation_rival = scope:demand_recipient
					has_relation_potential_rival = scope:demand_recipient
				}
			}
			set_relation_potential_rival = scope:demand_recipient
		}

		ai_chance = {
			base = 100
		}
	}
}



### MISC ###
# Kurdish kingdom - convert to Yazidism?
historicinvasions_event.4000 = {
	type = character_event
	title = historicinvasions_event.4000.t
	desc = historicinvasions_event.4000.desc

	theme = faith

	left_portrait = {
		character = root
		animation = happiness
	}

	# don't convert at all
	option = {
		name = historicinvasions_event.4000.a

		add_piety = 150

		ai_chance = {
			base = 10
			modifier = {
				add = 1000
				OR = {
					has_trait = zealous
				}
			}
		}
	}

	# convert to Yazidism
	option = {
		name = historicinvasions_event.4000.b
		add_internal_flag = dangerous

		set_character_faith_with_conversion = faith:yazidi
		capital_county = { set_county_faith = faith:yazidi }
		if = {
			limit = {
				has_fp3_dlc_trigger = yes
				exists = struggle:persian_struggle
				struggle:persian_struggle = {
					NOT = { is_faith_involved_in_struggle = faith:yazidi }
				}
			}
			struggle:persian_struggle = {
				set_faith_as_involved = faith:yazidi
			}
		}

		ai_chance = {
			base = 85
			modifier = {
				add = 110
				OR = {
					has_trait = cynical
				}
			}
		}
	}

	# convert back to Zoroastrianism
	option = {
		name = historicinvasions_event.4000.c
		add_internal_flag = dangerous

		set_character_faith_with_conversion = faith:zurvanism # Behafaridism
		capital_county = { set_county_faith = faith:zurvanism }
		if = {
			limit = {
				has_fp3_dlc_trigger = yes
				exists = struggle:persian_struggle
				struggle:persian_struggle = {
					NOT = { is_faith_involved_in_struggle = faith:zurvanism }
				}
			}
			struggle:persian_struggle = {
				set_faith_as_involved = faith:zurvanism
			}
		}

		ai_chance = {
			base = 5
			modifier = {
				add = 15
				OR = {
					has_trait = cynical
				}
			}
		}
	}
}

# in-built compatibility information
historicinvasions_event.9500 = {
	type = character_event
	title = historicinvasions_event.9500.t
	desc = {
		desc = historicinvasions_event.9500.desc
		triggered_desc = {
			trigger = { is_moregermankingdoms_active = yes }
			desc = historicinvasions_event.9500.desc.moregermankingdoms
		}
		triggered_desc = {
			trigger = { is_nodissolutionfactions_active = yes }
			desc = historicinvasions_event.9500.desc.nodissolutionfactions
		}
		triggered_desc = {
			trigger = { is_RICE_loaded = yes }
			desc = historicinvasions_event.9500.desc.rice
		}
		triggered_desc = { # only set during on_game_start_after_lobby
			trigger = { exists = global_var:GG_is_loaded }
			desc = historicinvasions_event.9500.desc.gamerulegadget
		}
		triggered_desc = {
			trigger = { ME_is_loaded = yes }
			desc = historicinvasions_event.9500.desc.muslimenhancements
		}
	}

	theme = default

	trigger = { # make sure it only appears if compatibility changes were in consideration at game start
		OR = {
			NAND = { # in-built compatibility changes are triggered if at least one of those mods is active
				is_moregermankingdoms_active = no
				is_nodissolutionfactions_active = no
				is_RICE_loaded = no
				NOT = { exists = global_var:GG_is_loaded }
				ME_is_loaded = no
			}
		}
	}
	
	option = { name = historicinvasions_event.9500.a } # dismiss event
}