namespace = varangian

# Events about the Varangian Guard
	# varangian.0001 - Find a potential Varangian and fire chain, by Petter Vilberg
	# varangian.0002 - Event for the liege of the Varangian candidate, by Petter Vilberg
	# varangian.0003 - Emperor gets a new varangian, by Petter Vilberg
	# varangian.1001 - Varangian decides to return, by Petter Vilberg
	# varangian.1002 - Varangian comes home to liege, by Petter Vilberg

	# Ongoing Varangian events
	# varangian.2001 - Become Best Friends with another Varangian, by Petter Vilberg
	# varangian.2002 - Become Lover with a fellow Varangian, by Petter Vilberg
	# varangian.2003 - Become Friends with a local ruler, by Petter Vilberg
	# varangian.2004 - Become Rivals with a local ruler, by Petter Vilberg
	# varangian.2005 - Marry your lover, by Petter Vilberg
	# varangian.2008 - Get wounded, by Petter Vilberg
	# varangian.2011 - Become lover with a local, by Petter Vilberg
	# varangian.2012 - Increase Prowess, by Petter Vilberg
	# varangian.2013 - Increase Martial, by Petter Vilberg
	# varangian.2014 - Increase Learning, by Petter Vilberg
	# varangian.2015 - Increase Diplomacy, by Petter Vilberg
	# varangian.2016 - Become Brave, by Petter Vilberg
	# varangian.2017 - Become Ambitious, by Petter Vilberg
	# varangian.2018 - Become Diligent, by Petter Vilberg

# Evaluate whether the target is valid for varangians
scripted_trigger varangian_0001_available_varangian_trigger = {
	can_be_knight_trigger = {
		ARMY_OWNER = title:e_byzantium.holder
	}
	age < 40
	is_married = no
	NOR = {
		has_character_flag = has_asked_to_be_varangian
		has_trait = varangian
	}
	religion = religion:germanic_religion
	NOR = {
		faith = {
			has_doctrine_parameter = pacifist_opinion_active
		}
		has_trait = craven
		has_truce = title:e_byzantium.holder
		is_at_war_with = title:e_byzantium.holder
		has_relation_rival = title:e_byzantium.holder
	}
}

varangian.0001 = {
	hidden = yes

	trigger = {
		religion = religion:germanic_religion
		NOR = {
			faith = {
				has_doctrine_parameter = pacifist_opinion_active
			}
			has_character_flag = had_recent_varangian_sendoff
		}
		is_at_war = no
		age >= 6
		exists = title:e_byzantium.holder
		NOR = {
			this = title:e_byzantium.holder
			has_truce = title:e_byzantium.holder
			is_at_war_with = title:e_byzantium.holder
			any_sub_realm_county = {
				this = title:c_byzantion
			}
			has_relation_rival = title:e_byzantium.holder
		}
		any_courtier = {
			varangian_0001_available_varangian_trigger = yes
		}
	}

	immediate = {
		# Find a suitable courtier
		random_courtier = {
			limit = {
				varangian_0001_available_varangian_trigger = yes
			}
			weight = {
				base = 1
				compare_modifier = {
					value = martial
					multiplier = 0.25
				}
				compare_modifier = {
					value = prowess
					multiplier = 0.5
				}
				modifier = {
					add = 5
					OR = {
						has_trait = brave
						has_trait = arrogant
					}
				}
				modifier = {
					add = -1
					has_trait = content
				}
				modifier = {
					add = -1
					has_trait = lazy
				}
				modifier = {
					add = -1
					has_trait = craven
				}
				modifier = {
					add = 3
					is_of_major_interest_to_root_trigger = yes
				}
				modifier = {
					add = 2
					is_of_minor_interest_to_root_trigger = yes
				}
				modifier = {
					add = 1
					exists = dynasty
				}
				# Reduce chances if they seem likely to inherit stuff soon
				modifier = {
					factor = 0.2
					any_heir_title = {
						holder = {
							this = root
							OR = {
								age >= 60
								health <= poor_health
							}
						}
					}
				}
			}
			add_character_flag = has_asked_to_be_varangian
			save_scope_as = varangian_candidate
		}

		title:e_byzantium.holder = {
			save_scope_as = emperor
			if = {
				limit = { capital_county = title:c_byzantion }
				set_local_variable = {
					name = to_miklagard
					value = yes
				}
			}
		}

		# Send event to court owner
		trigger_event = varangian.0002
	}
}


# Varangian requests permission to leave
varangian.0002 = {
	type = character_event
	title = varangian.0002.t
	desc = {
		desc = varangian.0002.opening
		first_valid = {
			triggered_desc = {
				trigger = { exists = local_var:to_miklagard }
				desc = varangian.0002.miklagard
			}
			desc = varangian.0002.elsewhere
		}
		desc = varangian.0002.end
	}
	theme = martial
	left_portrait = scope:varangian_candidate
	lower_right_portrait = scope:emperor

	immediate = {
		add_character_flag = {
			flag = had_recent_varangian_sendoff
			years = 5
		}
	}
	
	option = { # Approve
		name = varangian.0002.a
		show_as_tooltip = {
			scope:varangian_candidate = {
				visit_court_of = scope:emperor
				add_trait = varangian
			}
		}
		hidden_effect = {
			scope:emperor = {
				trigger_event = varangian.0003
			}
		}
		debug_log = "Someone got sent as a Varangian"
		debug_log_scopes = yes
		ai_chance = {
			base = 100
		}
	}

	option = { # Keep them
		name = varangian.0002.b
		scope:varangian_candidate = {
			add_opinion = {
				target = root
				modifier = angry_opinion
				opinion = -20
			}
		}
		ai_chance = {
			base = 0
		}
	}
}

# Emperor gets a new varangian
varangian.0003 = {
	hidden = yes

	immediate = {
		send_interface_message = {
			type = event_martial_text_good
			left_icon = scope:varangian_candidate
			title = varangian_arrived.title
			desc = varangian_arrived.desc
			add_visiting_courtier = scope:varangian_candidate
			scope:varangian_candidate = {
				add_trait = varangian
				add_character_flag = is_currently_varangian
				set_variable = {
					name = years_as_varangian
					value = 1 # Set to one year ahead of time for the sake of event evaluation
				}
				trigger_event = {
					on_action = varangian_ongoing
					days = { 340 380 }
				}
			}
		}
	}
}


# Return home eventually
scripted_trigger varangian_1001_partner_can_be_brought_home_trigger = {
	is_alive = yes
	is_landed = no
	any_heir_title = {
		always = no
	}
}

varangian.1001 = {
	hidden = yes

	trigger = {
		exists = liege
		var:years_as_varangian > 5
	}

	weight_multiplier = {
		base = 1
		modifier = {
			add = {
				value = var:years_as_varangian
				subtract = 5
				multiply = 0.25
			}
			always = yes
		}
	}

	immediate = {
		if = {
			limit = {
				exists = var:spouse_from_varangian_guard
				var:spouse_from_varangian_guard = {
					varangian_1001_partner_can_be_brought_home_trigger = yes
					is_consort_of = root
				}
			}
			var:spouse_from_varangian_guard = {
				save_scope_as = new_partner
			}
		}
		else_if = {
			limit = {
				exists = var:lover_from_varangian_guard
				var:lover_from_varangian_guard = {
					varangian_1001_partner_can_be_brought_home_trigger = yes
					has_relation_lover = root
				}
			}
			var:lover_from_varangian_guard = {
				save_scope_as = new_partner
			}
		}
		remove_character_flag = is_currently_varangian
		save_scope_as = returning_varangian
		if = {
			limit = { exists = title:e_byzantium.holder }
			title:e_byzantium.holder = {
				save_scope_as = emperor
			}
		}
		liege = {
			trigger_event = varangian.1002
		}
	}
}

# Liege - Varangian returns home
varangian.1002 = {
	type = character_event
	title = varangian.1002.t
	desc = {
		desc = varangian.1002.opening
		triggered_desc = {
			trigger = {
				exists = scope:new_partner
			}
			desc = varangian.1002.new_partner
		}
		desc = varangian.1002.ending
	}
	theme = martial
	left_portrait = {
		character = scope:returning_varangian
		animation = pride
	}
	lower_left_portrait = scope:new_partner
	lower_right_portrait = scope:emperor
	
	immediate = {
		scope:returning_varangian = {
			return_to_court = yes
		}
		hidden_effect = {
			if = {
				limit = {
					exists = scope:new_partner
					NOT = { scope:new_partner = { is_courtier_of = root } }
				}
				scope:returning_varangian.court_owner = {
					add_courtier = scope:new_partner
				}
			}
		}
		debug_log = "Someone got home from the Varangian Guard"
		debug_log_scopes = yes
	}

	option = { # Wow
		name = varangian.1002.a
		scope:returning_varangian = {
			add_prestige = medium_prestige_gain
			add_gold = medium_gold_value
		}
	}
}


# Return home because of inheritance
# varangian.1003

# Liege - Varangian has returned home to be vassal
# varangian.1004


# Become friends with local Varangian
scripted_trigger varangian_2001_varangian_friend_trigger = {
	has_character_flag = is_currently_varangian
	is_ai = yes
	NOR = {
		this = root
		has_relation_rival = root
		has_relation_lover = root
	}
}

varangian.2001 = {
	hidden = yes

	trigger = {
		# Only if you don't have any friends
		any_relation = {
			type = best_friend
			always = no
		}
		exists = host
		host = {
			any_courtier_or_guest = {
				varangian_2001_varangian_friend_trigger = yes
			}
		}
	}

	immediate = {
		host = {
			random_courtier_or_guest = {
				limit = {
					varangian_2001_varangian_friend_trigger = yes
				}
				weight = {
					base = 1
					opinion_modifier = {
						opinion_target = root
						multiplier = 0.1 # +10 at 100
					}
					modifier = {
						add = 10
						OR = {
							has_relation_potential_friend = root
							has_relation_friend = root
						}
					}
				}
				save_scope_as = varangian_friend
			}
		}
		set_relation_best_friend = scope:varangian_friend
	}
}

# Become lover with a fellow Varangian
scripted_trigger varangian_2002_varangian_lover_trigger = {	
	has_character_flag = is_currently_varangian
	is_ai = yes
	is_adult = yes
	save_temporary_scope_as = potential_cheater
	OR = {
		is_married = no
		any_spouse = {
			count = all
			save_temporary_scope_as = partner
			scope:potential_cheater = { might_cheat_on_partner_trigger = { PARTNER = scope:partner } }
		}
	}
	is_attracted_to_gender_of = root
	root = {
		is_attracted_to_gender_of = prev
	}
	NOR = {
		this = root
		has_relation_rival = root
		has_relation_lover = root
	}
	opinion = {
		target = root
		value > 10
	}
	reverse_opinion = {
		target = root
		value > 0
	}
}

varangian.2002 = {
	hidden = yes

	trigger = {
		# Only if you don't have any lovers
		any_relation = {
			type = lover
			always = no
		}
		mgr_can_seduce = yes #game rule
		exists = host
		host = {
			any_courtier_or_guest = {
				varangian_2002_varangian_lover_trigger = yes
			}
		}
	}

	immediate = {
		host = {
			random_courtier_or_guest = {
				limit = {
					varangian_2002_varangian_lover_trigger = yes
				}
				weight = {
					base = 1
					opinion_modifier = {
						opinion_target = root
						multiplier = 0.1 # +10 at 100
					}
					modifier = {
						add = 10
						has_relation_potential_lover = root
					}
				}
				save_scope_as = varangian_lover
			}
		}
		set_relation_lover = scope:varangian_lover
		set_variable = {
			name = lover_from_varangian_guard
			value = scope:varangian_lover
		}
	}
}

# Become friends with local ruler
scripted_trigger varangian_2003_local_ruler_friend_trigger = {	
	is_ai = yes
	opinion = {
		target = root
		value > 0
	}
	reverse_opinion = {
		target = root
		value > 0
	}
	NOR = {
		this = root
		has_relation_rival = root
		has_relation_lover = root
	}
}

varangian.2003 = {
	hidden = yes

	trigger = {
		# Only if you don't have any friends
		any_relation = {
			type = friend
			always = no
		}
		exists = host
		host = {
			any_vassal = {
				varangian_2003_local_ruler_friend_trigger = yes
			}
		}
	}

	immediate = {
		host = {
			random_vassal = {
				limit = {
					varangian_2003_local_ruler_friend_trigger = yes
				}
				weight = {
					base = 1
					opinion_modifier = {
						opinion_target = root
						multiplier = 0.1 # +10 at 100
					}
					modifier = {
						add = 10
						has_relation_potential_friend = root
					}
				}
				save_scope_as = varangian_friend
			}
		}
		set_relation_lover = scope:varangian_friend
	}
}

# Become rivals with local ruler
scripted_trigger varangian_2004_local_ruler_rival_trigger = {	
	is_ai = yes
	opinion = {
		target = root
		value < 0
	}
	reverse_opinion = {
		target = root
		value < 0
	}
	NOR = {
		this = root
		has_relation_friend = root
		has_relation_lover = root
	}
}

varangian.2004 = {
	hidden = yes

	trigger = {
		# Only if you don't have any rival
		any_relation = {
			type = rival
			always = no
		}
		exists = host
		host = {
			any_vassal = {
				varangian_2004_local_ruler_rival_trigger = yes
			}
		}
	}

	immediate = {
		host = {
			random_vassal = {
				limit = {
					varangian_2004_local_ruler_rival_trigger = yes
				}
				weight = {
					base = 1
					opinion_modifier = {
						opinion_target = root
						multiplier = 0.1 # +10 at 100
					}
					modifier = {
						add = 10
						has_relation_potential_friend = root
					}
				}
				save_scope_as = varangian_friend
			}
		}
		set_relation_lover = scope:varangian_friend
	}
}

# Marry Lover
scripted_trigger varangian_2005_lover_to_marry = {
	is_ai = yes
	is_married = no
	can_marry_character_trigger = {
		CHARACTER = root
	}
	OR = {
		is_in_the_same_court_as_or_guest = root
		top_liege = root.host
		AND = {
			exists = host
			host.top_liege = root.host
		}
	}
}

scripted_trigger varangian_2005_should_marry_this_character_matrilineally = {
	is_female = yes
	OR = {
		AND = {
			is_landed = yes
			$OTHER_CHARACTER$ = { # Not if they are heir to anything
				any_heir_title = {
					always = no
				}
			}
		}
		AND = {
			faith = {
				has_doctrine = doctrine_gender_female_dominated
			}
			$OTHER_CHARACTER$ = {
				faith = {
					OR = {
						has_doctrine = doctrine_gender_female_dominated
						has_doctrine = doctrine_gender_equal
					}
				}
			}
		}
	}
}

varangian.2005 = {
	hidden = yes

	trigger = {
		is_married = no
		any_relation = {
			type = lover
			varangian_2005_lover_to_marry = yes
		}
	}

	immediate = {
		random_relation = {
			type = lover
			limit = {
				varangian_2005_lover_to_marry = yes
			}
			save_scope_as = lover_to_marry
		}
		if = {
			limit = {
				OR = {
					varangian_2005_should_marry_this_character_matrilineally = { OTHER_CHARACTER = scope:lover_to_marry }
					scope:lover_to_marry = {
						varangian_2005_should_marry_this_character_matrilineally = { OTHER_CHARACTER = root }
					}
				}
			}
			marry_matrilineal_if_possible = { CHARACTER = scope:lover_to_marry }
		}
		else = {
			marry = scope:lover_to_marry
		}
		set_variable = {
			name = spouse_from_varangian_guard
			value = scope:lover_to_marry
		}
	}
}

# Get Murdered
# varangian.2006

# Liege - notification they were murdered

# Get Wounded
varangian.2008 = {
	hidden = yes

	trigger = {
		NOR = {
			has_trait = wounded
			has_character_flag = had_severe_injury_in_varangian_guard
		}
	}

	immediate = {
		add_character_flag = had_severe_injury_in_varangian_guard
		random_list = {
			10 = {
				add_trait = wounded_2
			}
			5 = {
				add_trait = wounded_3
			}
			5 = {
				trigger = {
					NOT = { has_trait = one_eyed }
				}
				add_trait = one_eyed
			}
			5 = {
				trigger = {
					NOT = { has_trait = one_legged }
				}
				add_trait = one_legged
			}
			2 = {
				trigger = {
					NOT = {
						has_trait = eunuch
						is_male = yes
					}
				}
				add_trait = eunuch
			}
			2 = {
				trigger = {
					NOT = {
						has_trait = blind
						is_male = yes
					}
				}
				add_trait = blind
			}
		}
	}
}

# Get Killed in battle
# varangian.2009

# Liege - notification they were killed
# varangian.2010

# Become lover with a local
scripted_trigger varangian_2011_local_lover_trigger = {
	is_ai = yes
	is_adult = yes
	is_attracted_to_gender_of = root
	root = {
		is_attracted_to_gender_of = prev
	}
	save_temporary_scope_as = potential_cheater
	OR = {
		is_married = no
		any_spouse = {
			count = all
			save_temporary_scope_as = partner
			scope:potential_cheater = { might_cheat_on_partner_trigger = { PARTNER = scope:partner } }
		}
	}
	NOR = {
		this = root
		has_relation_rival = root
		has_relation_lover = root
	}
	opinion = {
		target = root
		value > 10
	}
	reverse_opinion = {
		target = root
		value > 0
	}
}

varangian.2011 = {
	hidden = yes

	trigger = {
		exists = host
		mgr_can_seduce = yes #game rule
	}

	immediate = {
		random_list = {
			10 = { # Find someone suitable
				trigger = {
					host = {
						OR = {
							varangian_2011_local_lover_trigger = yes
							any_courtier = {
								varangian_2011_local_lover_trigger = yes
							}
							any_vassal = {
								varangian_2011_local_lover_trigger = yes
								any_courtier = {
									varangian_2011_local_lover_trigger = yes
								}
							}
						}
					}
				}
				host = {
					if = {
						limit = { varangian_2011_local_lover_trigger = yes }
						add_to_list = potential_lover
					}
					every_courtier = {
						limit = { varangian_2011_local_lover_trigger = yes }
						add_to_list = potential_lover
					}
					every_vassal = {
						if = {
							limit = { varangian_2011_local_lover_trigger = yes }
							add_to_list = potential_lover
						}
						every_courtier = {
							limit = { varangian_2011_local_lover_trigger = yes }
							add_to_list = potential_lover
						}
					}
				}
				random_in_list = {
					list = potential_lover
					weight = {
						base = 1
						modifier = {
							add = 5
							is_landed = no
						}
					}
					save_scope_as = lover
				}
			}
			10 = { # Create a beautiful peasant
				create_character = {
					template = beautiful_peasant_character
					age = { age_compared_to_root_minus_ten_always_adult age_compared_to_root_plus_ten_always_adult }
					gender_female_chance = root_attraction_based_female_chance
					employer = root.host
					faith = root.host.faith
					culture = root.host.culture
					save_scope_as = lover
				}
			}
			10 = { # Create a noble
				create_character = {
					template = pool_repopulate_spouse
					age = { age_compared_to_root_minus_ten_always_adult age_compared_to_root_plus_ten_always_adult }
					gender_female_chance = root_attraction_based_female_chance
					employer = root.host
					faith = root.host.faith
					culture = root.host.culture
					dynasty = generate
					save_scope_as = lover
				}
			}
			5 = { # Create a witch
				create_character = {
					template = witchy_template
					age = { age_compared_to_root_minus_ten_always_adult age_compared_to_root_plus_ten_always_adult }
					gender_female_chance = root_attraction_based_female_chance
					employer = root.host
					faith = root.host.faith
					culture = root.host.culture
					save_scope_as = lover
				}
			}
			5 = { # Create a witch with a Dyansty
				create_character = {
					template = witchy_template
					age = { age_compared_to_root_minus_ten_always_adult age_compared_to_root_plus_ten_always_adult }
					gender_female_chance = root_attraction_based_female_chance
					employer = root.host
					faith = root.host.faith
					culture = root.host.culture
					dynasty = generate
					save_scope_as = lover
				}
			}
		}
		set_relation_lover = scope:lover
	}
}

# Increase Prowess
varangian.2012 = {
	hidden = yes

	trigger = {
		NOT = { has_character_flag = varangian_recently_increased_prowess }
	}

	weight_multiplier = {
		base = 1
		compare_modifier = { # Reduce chance for each current Prowess over 15
			value = prowess
			multiplier = -0.1
			offset = -15
			trigger = {
				prowess > 15
			}
		}
	}

	immediate = {
		add_character_flag = {
			flag = varangian_recently_increased_prowess
			years = 2
		}
		add_prowess_skill = 1
	}
}

# Increase Martial
varangian.2013 = {
	hidden = yes

	trigger = {
		NOT = { has_character_flag = varangian_recently_increased_martial }
	}

	weight_multiplier = {
		base = 1
		compare_modifier = { # Reduce chance for each current martial over 15
			value = martial
			multiplier = -0.1
			offset = -15
			trigger = {
				martial > 15
			}
		}
	}

	immediate = {
		add_character_flag = {
			flag = varangian_recently_increased_martial
			years = 2
		}
		add_martial_skill = 1
	}
}

# Increase Learning
varangian.2014 = {
	hidden = yes

	trigger = {
		NOT = { has_character_flag = varangian_recently_increased_learning }
	}

	weight_multiplier = {
		base = 1
		compare_modifier = { # Reduce chance for each current learning over 15
			value = learning
			multiplier = -0.1
			offset = -15
			trigger = {
				learning > 15
			}
		}
	}

	immediate = {
		add_character_flag = {
			flag = varangian_recently_increased_learning
			years = 2
		}
		add_learning_skill = 1
	}
}

# Increase Diplomacy
varangian.2015 = {
	hidden = yes

	trigger = {
		NOT = { has_character_flag = varangian_recently_increased_diplomacy }
	}

	weight_multiplier = {
		base = 1
		compare_modifier = { # Reduce chance for each current diplomacy over 15
			value = diplomacy
			multiplier = -0.1
			offset = -15
			trigger = {
				diplomacy > 15
			}
		}
	}

	immediate = {
		add_character_flag = {
			flag = varangian_recently_increased_diplomacy
			years = 2
		}
		add_diplomacy_skill = 1
	}
}

# Become Brave
varangian.2016 = {
	hidden = yes

	trigger = {
		OR = {
			has_trait = craven
			has_trait = lazy
			has_trait = content
		}
		NOT = { has_trait = brave }
	}

	weight_multiplier = {
		base = 1
	}

	immediate = {
		if = {
			limit = { has_trait = craven }
			remove_trait = craven
		}
		else_if = {
			limit = { has_trait = lazy }
			remove_trait = lazy
		}
		else = {
			limit = { has_trait = content }
			remove_trait = content
		}
		add_trait = brave
	}
}

# Become Ambitious
varangian.2017 = {
	hidden = yes

	trigger = {
		OR = {
			has_trait = craven
			has_trait = lazy
			has_trait = content
		}
		NOT = { has_trait = ambitious }
	}

	weight_multiplier = {
		base = 1
	}

	immediate = {
		if = {
			limit = { has_trait = content }
			remove_trait = content
		}
		else_if = {
			limit = { has_trait = lazy }
			remove_trait = lazy
		}
		else = {
			limit = { has_trait = craven }
			remove_trait = craven
		}
		add_trait = ambitious
	}
}

# Become Diligent
varangian.2018 = {
	hidden = yes

	trigger = {
		OR = {
			has_trait = craven
			has_trait = lazy
			has_trait = content
		}
		NOT = { has_trait = diligent }
	}

	weight_multiplier = {
		base = 1
	}

	immediate = {
		if = {
			limit = { has_trait = lazy }
			remove_trait = lazy
		}
		else_if = {
			limit = { has_trait = diligent }
			remove_trait = diligent
		}
		else = {
			limit = { has_trait = craven }
			remove_trait = craven
		}
		add_trait = diligent
	}
}

# Liege: Varangian has died saving the Emeperor's life
	# by Petter Vilberg
varangian.3001 = {
	type = character_event
	title = varangian.3001.t
	desc = varangian.3001.desc
	theme = murder_scheme
	left_portrait = scope:varangian
	right_portrait = {
		character = scope:emperor
		animation = sadness
	}

	option = {
		name = varangian.3001.a
		add_gold = major_gold_value
	}
}

# Emperor: Varangians are great fighters

