﻿namespace = rus_invasion

# Rus invader demands subjugation
rus_invasion.1 = {
	type = letter_event
	opening = rus_invasion.1.opening
	desc = rus_invasion.1.desc
	sender = {
		character = scope:rus_conqueror
		animation = personality_bold
	}
	trigger = {
		NOT = { is_at_war_with = scope:rus_conqueror }
		NOT = { is_vassal_of = scope:rus_conqueror }
		NOT = { root = scope:rus_conqueror }
	}

	immediate = {
		save_scope_as = demand_recipient
	}

	option = { # Submit
		name = rus_invasion.1.a
		custom_tooltip = rus_invasion.1.a.tt
		hidden_effect = {
			scope:rus_conqueror = {
				trigger_event = rus_invasion.2
			}
		}
		ai_chance = {
			base = 50
			# More likely if you're just a count
			modifier = {
				add = 20
				highest_held_title_tier = tier_county
			}
			# Unlikely if you're Arrogant
			modifier = {
				factor = 0.5
				has_trait = arrogant
			}
			# Unlikely if you're brave
			modifier = {
				factor = 0.3
				has_trait = brave
			}
			# Likely if you're a Craven
			modifier = {
				factor = 1.4
				has_trait = craven
			}
			# Lower chance if you're decently large
			modifier = {
				factor = 0.5
				realm_size >= 4 
			}
			# Little chance of accepting if you're large
			modifier = {
				factor = 0.3
				realm_size >= 8 
			}
			# More likely if you only have one province.
			modifier = {
				add = 15
				realm_size = 1
			}
		}
	}

	option = { # Resist
		name = rus_invasion.1.b
		custom_tooltip = rus_invasion.1.b.tt
		add_internal_flag = dangerous
		hidden_effect = {
			scope:rus_conqueror = {
				trigger_event = rus_invasion.3
			}
		}
		ai_chance = {
			base = 100
		}
	}
}

# Subjugation accepted
rus_invasion.2 = {
	type = letter_event
	opening = rus_invasion.2.opening
	desc = rus_invasion.2.desc
	sender = scope:demand_recipient

	immediate = {
		create_title_and_vassal_change = {
			type = swear_fealty
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		scope:demand_recipient = {
			change_liege = {
				liege = root
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change

		scope:rus_conqueror = {
			start_wars_for_rus_effect = yes
			spawn_army = {
				name = event_troop_default_name
				levies = 300
				location = root.capital_province
				uses_supply = no
				inheritable = no
					}
			add_gold = 30
		}

		debug_log = "Someone accepted Rus subjugation"
		debug_log_scopes = yes
	}

	option = { # Wise choice
		name = rus_invasion.2.a
	}
}

# Subjugation refused
rus_invasion.3 = {
	type = letter_event
	opening = rus_invasion.3.opening
	desc = rus_invasion.3.desc
	sender = {
		character = scope:demand_recipient
		animation = personality_bold
	}

	immediate = {
		hidden_effect = {
			scope:demand_recipient.primary_title = {
				save_scope_as = target_title
			}
		}
		# Remove from the list blocking war declarations
		remove_list_variable = {
			name = subjugation_offer_under_consideration
			target = scope:demand_recipient
		}

		debug_log = "Someone refused Rus subjugation"
		debug_log_scopes = yes
	}

	option = { # War it is
		name = rus_invasion.3.a
		if = {
			limit = {
				exists = scope:target_title.empire
			}
			start_war = {
				cb = rus_invasion_war
				target = scope:target_title.holder
				target_title = title:e_russia
			}

		}
		else = {
			debug_log = "Failed to find a target empire for a Rus invasion!"
			debug_log_scopes = yes

			start_war = {
				cb = rus_invasion_war
				target = scope:target_title.holder
				target_title = title:e_russia
			}
		}
	}
}

# Immediate Game Rule
rus_invasion.4 = {
	hidden = yes
	scope = none

	trigger = {
		game_start_date <= 867.1.1
		has_game_rule = rus_launch_immediate
	}
# Has to happen with a delay or it doesn't work.
	immediate = {
		character:40605 = {
			trigger_event = {
				id = rus_invasion.45
				days = 2
			}
		}
	}

}
# Actually trigger the invasion effects
rus_invasion.45 = {
	scope = character
	hidden = yes
	# No cheesing it so the player gets it
	trigger = { is_ai = yes }
	#Give him the kingdom title
	immediate = {
		create_title_and_vassal_change = {
			type = created
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		title:k_novgorod = {
			change_title_holder = {
				holder = root
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
		set_primary_title_to = title:k_novgorod

		# Health boost to reduce chances of an anti-climactic end, especially since Rurik is kind of old
		add_character_modifier = { modifier = aging_gracefully_modifier }
		#add_trait = conqueror
		add_character_flag = undertaking_rus_invasion
		start_wars_for_rus_effect = yes
		#6,000 MAA because if you're using this game rule, you probably want it a bit railroaded
		spawn_army = {
			name = event_troop_default_name
			men_at_arms = {
				type = druzhina
				stacks = 30
			}
			men_at_arms = {
				type = light_footmen
				stacks = 30
			}	
			location = root.capital_province
			uses_supply = no
			inheritable = yes
		}
		#Take care of notification events.
		every_player = {
			limit = {
				capital_province = {
					geographical_region = custom_brus_kievian_rus
				}
				NOT = { this = scope:rus_conqueror }
			}
			trigger_event = { 
				id = rus_invasion.8
				days = { 5 10 } 
			}
		}
	}
}

# Seize Kiev Event - based off Primary Chronicle account
rus_invasion.5 = {
	type = character_event
	desc = rus_invasion.5.desc
	title = rus_invasion.5.title
	theme = war

	trigger = {
		is_independent_ruler = yes
		# The AI only uses it if they are doing the invasion.
		OR = {
			has_character_flag = undertaking_rus_invasion
			is_ai = no
		}
		NOR = {
			is_target_in_global_variable_list = {
				name = unavailable_unique_decisions
				target = flag:flag_founded_capital_of_rus
			}
			has_character_flag = had_kiev_event
		}
		title:c_kiev.holder = {
			is_ai = yes
			NOT = {
				is_allied_to = root
				dynasty = root.dynasty
			}
		}
	}

	immediate = {
		add_character_flag = had_kiev_event
	}

	left_portrait = {
		character = root
		animation = personality_honorable
	}
	right_portrait = {
		character = title:c_kiev.holder
		animation = fear
	}

option = {
	name = rus_invasion.5.a
	# Have the player pay this small price for this historical occurrence
	if = {
		limit = { is_ai = no }
		add_tyranny = 15
		add_prestige = medium_prestige_value
		add_piety = medium_piety_value
	}
	#Kill Dyre
	title:c_kiev.holder = {
		death = {
		death_reason = death_battle
		killer = root
		}
	}
	#Transfer Kiev
	create_title_and_vassal_change = {
		type = conquest
		save_scope_as = title_change
		add_claim_on_loss = yes
		}
	title:d_kiev = {
			every_de_jure_county = {
			limit = {
				holder = title:c_kiev.holder
			}
			change_title_holder = {
				holder = root
				change = scope:title_change
			}
		}
	}
	resolve_title_and_vassal_change = scope:title_change

	# Make it the capital
	set_realm_capital = title:c_kiev

	#If the AI is doing the Conquest of Rus, make them form Kiev and set it as their primary kingdom.
	if = {
		limit = {
			is_ai = yes
			has_character_flag = undertaking_rus_invasion
			title:k_ruthenia = { exists = no }
		}
		create_title_and_vassal_change = {
			type = created
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		title:k_ruthenia = {
			change_title_holder = {
				holder = root
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
		set_primary_title_to = title:k_ruthenia

		# Found a new capital for the Rus effects
		save_scope_as = founder
		scope:founder.capital_county = { save_scope_as = new_rus_capital }
		show_as_tooltip = { found_capital_of_rus_scripted_effect = yes }
		#If they meet the criteria for the Northern Lords one, trigger that.
		if = {
			limit = {
				OR = {
					culture = { has_cultural_pillar = heritage_north_germanic }
					has_culture = culture:norman
					has_character_modifier = embraced_local_traditions_modifier
					AND = {
						exists = dynasty
						dynasty = {
							has_dynasty_modifier = settled_norse_dynasty_modifier
						}
					}
				}
			}
			trigger_event = { id = fp1_major_decisions.0011 }
		}
		#Otherwise, trigger the alternative mod one.
		else = { trigger_event = { id = rus_invasion.7 } }

		hidden_effect = {
			add_to_global_variable_list = {
				name = unavailable_unique_decisions
				target = flag:flag_founded_capital_of_rus
			}
		}
	}

	ai_chance = { base = 100 }

	}

option = {
	name = rus_invasion.5.b
	if = {
		limit = {
			is_ai = yes
			has_character_flag = undertaking_rus_invasion
		}
		save_scope_as = founder
		scope:founder.capital_county = { save_scope_as = new_rus_capital }
		show_as_tooltip = { found_capital_of_rus_scripted_effect = yes }
		
		#If they meet the criteria for the Northern Lords one, trigger that.
		if = {
			limit = {
				OR = {
					culture = { has_cultural_pillar = heritage_north_germanic }
					has_culture = culture:norman
					has_character_modifier = embraced_local_traditions_modifier
					AND = {
						exists = dynasty
						dynasty = {
							has_dynasty_modifier = settled_norse_dynasty_modifier
						}
					}
				}
			}
			trigger_event = { id = fp1_major_decisions.0011 }
		}
		#Otherwise, trigger the alternative mod one.
		else = { trigger_event = { id = rus_invasion.7 } }
		# Make sure it doesn't happen again.
		hidden_effect = {
			add_to_global_variable_list = {
				name = unavailable_unique_decisions
				target = flag:flag_founded_capital_of_rus
			}
		}
	}

	add_piety = 100
	reverse_add_opinion = {
		target = title:c_kiev.holder
		modifier = brus_kiev_spared
		opinion = 30
	}

	ai_chance = {
		base = 20
		modifier = {
			add = 20
			has_trait = stubborn
		}
	}
	}
}

#Event to restart the effect with a delay so that the war trigger works properly (for starting the wars against empires/Khazaria)
rus_invasion.6 = {
	scope = character
	hidden = yes

	trigger = { is_at_war_as_attacker = no }
	immediate = { start_wars_for_rus_effect = yes }
}

# Non-Norse version of the Establish Capital for Rus decision
rus_invasion.7 = {
	type = character_event
	title = rus_invasion.7.t
	desc = rus_invasion.7.desc
	theme = realm

	left_portrait = {
		character = scope:founder
		animation = personality_honorable
	}
	immediate = {
		play_music_cue = "mx_cue_epic_sacral_moment"
	}

	#Kiev shall be my capital.
	option = {
		name = rus_invasion.7.a

		trigger = {
			#Must have title:c_kiev in order to make it your capital.
			any_sub_realm_county = { this = title:c_kiev }
			#And Kiev cannot be held by a player who has _only_ title:c_kiev, unless that player is you.
			title:c_kiev.holder = {
				OR = {
					is_ai = yes
					any_held_title = {
						count >= 1
						tier = tier_county
						NOT = { this = title:c_kiev }
					}
					this = scope:founder
				}
			}
		}

		#Organise the usurpation/capital move of title:c_kiev if appropriate.
		if = {
			limit = {
				NOT = { title:c_kiev.holder = scope:founder }
			}
			create_title_and_vassal_change = {
				type = usurped
				save_scope_as = change
				add_claim_on_loss = yes
			}
			title:c_kiev = {
				change_title_holder = {
					holder = root
					change = scope:change
				}
			}
			resolve_title_and_vassal_change = scope:change
		}
		set_realm_capital = title:c_kiev
		#Otherwise, set title:c_kiev to be scope:new_rus_capital & go through with the effect.
		title:c_kiev = {
			save_scope_as = new_rus_capital
			found_capital_of_rus_scripted_effect = yes
		}

		ai_chance = {
			#AI should always take this option if it's available.
			base = 100
		}
	}

	#My current capital shall be my capital!
	option = {
		name = rus_invasion.7.b

		#Not generally necessary, but just to stop some weird loc eventualities.
		trigger = {
			NOT = { capital_county = title:c_kiev }
		}

		#We try to use the player's current capital, but if they've moved it outside of e_russia, then we go with the default set in the standard decision entry.
		if = {
			limit = { capital_county.empire = title:e_russia }
			#Formatted a little inefficiently for readability.
			capital_county = { save_scope_as = new_rus_capital }
		}
		#Then enact the actual effects.
		found_capital_of_rus_scripted_effect = yes

		ai_chance = {
			#AI will only take this option if they have no other choice (i.e., can't get Kiev).
			base = 0
		}
	}
}

#Notify players of invasion beginning
rus_invasion.8 = {
	type = character_event
	desc = rus_invasion.8.desc
	title = rus_invasion.8.title
	theme = diplomacy

	left_portrait = {
		character = scope:rus_conqueror
		animation = personality_honorable
	}

	immediate = {}

	option = { name = rus_invasion.8.a }
}

# Remnants of the Khazars
rus_invasion.9 = {
	type = character_event
	desc = rus_invasion.9.desc
	title = rus_invasion.9.title
	theme =  stewardship_domain_focus 

	trigger = {}

	immediate = {
		ordered_ruler = {
			limit = {
				faith.religion = { this = religion:judaism_religion }
				culture = culture:radhanite
				is_landed = yes
			}
			order_by = highest_held_title_tier
			save_scope_as = jewish_emissary
		}
		create_character = {
				location = root.capital_province
				template = radhanite_trader_character
				save_scope_as = radhanite
			}
	}

	left_portrait = {
		character = scope:radhanite
		animation = worry
	}
	right_portrait = {
		character = scope:jewish_emissary
		animation = idle
	}

	option = { 
	# Embrace them
	name = rus_invasion.9.a 
		# Apply modifier to Kiev and capital, if not Kiev

		title:c_kiev = {
			add_county_modifier = {
				modifier = brus_flourishing_jewish_modifier
				years = 20
			}
		}

		random_sub_realm_county = {
			limit = { NOT = { this = root.capital_county } }
			set_county_culture = culture:radhanite
			add_county_modifier = {
				modifier = brus_flourishing_jewish_modifier
				years = 30
			}
		}

		#Increase Radhanite acceptance
		root.culture = {
			change_cultural_acceptance = {
			target = culture:radhanite
			value = 10
			desc = cultural_acceptance_historical_relations
			}
		}
		# A Radhanite trader joins your court
		add_courtier = scope:radhanite
		
		ai_chance = {
			base = 80
		}
	}
	# Kick them out
	option = { 
		name = rus_invasion.9.b 
		add_gold = 50

		stress_impact = {
			generous = medium_stress_impact_gain
			compassionate = medium_stress_impact_gain
		}
		#Remove 1 dev from Kiev
		every_sub_realm_county = {
			limit = {
				this = title:c_kiev
			}
			change_development_level = -1
		}
		#Varangian economy modifier
		add_character_modifier = {
			modifier = brus_varangian_economy
			years = 20
		}
		#These cultures will be unhappy
		culture:radhanite = {
			change_cultural_acceptance = {
			target = root.culture
			value = -10
			desc = cultural_acceptance_historical_relations
			}
		}
		ai_chance = {
			base = 20
			modifier = {
				has_trait = zealous
				add = 50
			}
		}
	}
}

#Assert Control Event for Player
rus_invasion.10 = {
	type = character_event
	desc = rus_invasion.10.desc
	title = rus_invasion.10.title

	theme = crown 

	left_portrait = {
		character = root
		animation = personality_honorable
	}

	trigger = {
		has_character_flag = undertaking_rus_invasion
	}

	immediate = { play_music_cue = "mx_cue_epic_sacral_moment" }

	option = { 
		name = rus_invasion.10.a 
		custom_tooltip = rus_invasion.10.a.tt
	}
}

#Byzantine Trade Deal After Raiding
rus_invasion.11 = {
	type = character_event
	desc = rus_invasion.11.desc
	title = rus_invasion.11.title

	theme = dread

	left_portrait = {
		character = root
		animation = personality_honorable
	}

	right_portrait = {
		character = title:e_byzantium.holder
		animation = personality_rational
	}

	trigger = {}

	immediate = {
		add_character_flag = negotiated_greek_trade
		save_scope_as = rus_raider
	}

	# Accept a trade deal
	option = { 
		name = rus_invasion.11.a 
		# Let the effects happen if the Byzantine Emperor is an AI. If player, we have to offer them a choice event too.
		if = {
			limit = {
				title:e_byzantium.holder = { is_ai = yes }
			}
			add_truce_one_way = {
				character = title:e_byzantium.holder
				override = yes
				years = 30
				name = rus_trade_deal_truce
			}
			add_character_modifier = {
				modifier = brus_greek_trade_treaty
				years = 20
			}
			remove_short_term_gold = minor_gold_value
			add_dread = -10
			reverse_add_opinion = {
				target = title:e_byzantium.holder
				modifier = rus_trade_opinion
			}
			title:e_byzantium.holder = {
				add_character_modifier = {
					modifier = brus_varangian_trade_treaty
					years = 15
				}
				add_character_flag = negotiated_greek_trade
			}
		}
		else = {
			title:e_byzantium.holder = { 
				trigger_event = { 
					id = rus_invasion.112 
					days = 10
				}
			}
		}
	}
	# No deal
	option = { 
		name = rus_invasion.11.b
		add_dread = 5
		add_prestige = medium_prestige_value
	}
}

#Byzantine Trade Deal for Player Emperor
rus_invasion.112 = {
	type = character_event
	desc = rus_invasion.112.desc
	title = rus_invasion.112.title

	theme = dread

	left_portrait = {
		character = root
		animation = personality_honorable
	}
	right_portrait = {
		character = scope:rus_raider
		animation = aggressive_hammer 
	}

	# Can't be between two players as this screws up the event chain.
	trigger = {}
	immediate = {
		add_character_flag = negotiated_greek_trade
	}

	# Offer trade deal
	option = { 
		name = rus_invasion.112.a 
		remove_short_term_gold = 20
		add_piety = {
			subtract = minor_piety_value
		}
		add_character_modifier = {
			modifier = brus_varangian_trade_treaty
			years = 20
		}
		reverse_add_opinion = {
			target = scope:rus_raider
			modifier = rus_trade_opinion
		}
		scope:rus_raider = {
			add_truce_one_way = {
				character = title:e_byzantium.holder
				years = 30
				name = rus_trade_deal_truce
			}
			add_character_modifier = {
				modifier = brus_greek_trade_treaty
				years = 20
			}
		}
	}

	# No deal
	option = { 
		name = rus_invasion.112.b
		add_prestige = minor_prestige_value
	}
}
