﻿namespace = histmod

scripted_trigger is_highly_skilled_warrior = {
	has_education_martial_trigger = yes
	# one of martial or prowess is at least 15+
	OR = {
		martial >= high_skill_rating
		prowess >= high_skill_rating
	}
	# while the other is at least 10+
	martial >= medium_skill_rating
	prowess >= medium_skill_rating
	save_temporary_scope_as = character
	NOR = {
		# who isn't reserved for later
		this = character:163119
		# and hasn't already been included in a list
		is_target_in_global_variable_list = { name = western_scandinavian_adventurer_list target = scope:character }
		is_target_in_global_variable_list = { name = eastern_scandinavian_adventurer_list target = scope:character }
	}
}

scripted_effect remove_existing_rollo_effect = {
	character:242 = {
		if = {
			limit = {
				has_dlc_feature = roads_to_power
				has_government = landless_adventurer_government
			}
			every_held_title = {
				character:242 = { destroy_title = prev }
			}
		}
		set_to_lowborn = yes
		silent_disappearance_effect = yes
	}
}

scripted_effect create_non_rollo_hrolfr_effect = {
	create_character = {
		name = "Hrolfr"
		age = 4
		gender = male
		father = character:186004
		mother = character:186005
		dynasty_house = character:186004.house
		employer = character:186004
		culture = culture:norse
		faith = faith:norse_pagan
		random_traits = yes
		after_creation = {
			copy_inheritable_appearance_from = character:186006
		}
	}
}

# Check every five years for new adventurers to add into the scandi adventurer pool
histmod.0050 = {
	hidden = yes
	
	trigger = {
		has_fp1_dlc_trigger = yes
		NOT = { has_game_rule = fp1_scandi_adventurers_off }
		is_landed = yes
	}
	
	immediate = {
		every_courtier_or_guest = {
			limit = {
				valid_scandinavian_adventurer = yes
				is_highly_skilled_warrior = yes
			}
			# Norse, Norwegians, & Danes go into the western adventurer list.
			if = {
				limit = {
					OR = {
						has_culture = culture:norse
						has_culture = culture:norwegian
						has_culture = culture:danish
					}
				}
				add_to_global_variable_list = {
					name = western_scandinavian_adventurer_list
					target = this
				}
			}
			# Norse & Swedes go into the eastern adventurer list.
			if = {
				limit = {
					OR = {
						has_culture = culture:norse
						has_culture = culture:swedish
					}
				}
				add_to_global_variable_list = {
					name = eastern_scandinavian_adventurer_list
					target = this
				}
			}
		}
	
		if = {
			limit = { top_liege = this }
			every_realm_province = { add_to_temporary_list = pool_provinces }
			
			every_in_list = {
				list = pool_provinces
				save_temporary_scope_as = pool_province

				every_pool_character = {
					province = scope:pool_province
					limit = {
						valid_scandinavian_adventurer = yes
						is_highly_skilled_warrior = yes
					}
					# Norse, Norwegians, & Danes go into the western adventurer list.
					if = {
						limit = {
							OR = {
								has_culture = culture:norse
								has_culture = culture:norwegian
								has_culture = culture:danish
							}
						}
						add_to_global_variable_list = {
							name = western_scandinavian_adventurer_list
							target = this
						}
					}
					# Norse & Swedes go into the eastern adventurer list.
					if = {
						limit = {
							OR = {
								has_culture = culture:norse
								has_culture = culture:swedish
							}
						}
						add_to_global_variable_list = {
							name = eastern_scandinavian_adventurer_list
							target = this
						}
					}
				}
			}
		}
	}
}

# Initial MH setup ( before game rules are checked )
histmod.0059 = {
	scope = none
	hidden = yes
	
	trigger = {
		game_start_date = 867.1.1
		has_fp1_dlc_trigger = yes
	}
	
	immediate = {
		# edits to base Rollo (for appearance only)
		character:242 = {
			add_character_flag = is_rollo
			change_current_weight = 50
		}
		
		### Changes to the Jarl of Møre
		
		# swapping rulers of Trøndheim and Møre, since they're in the wrong places
		histmod_simple_gain_title_effect = { TITLE = c_norwegian_more CHARACTER = character:186004 }
		histmod_simple_gain_title_effect = { TITLE = c_trandheim CHARACTER = character:280021 }
		
		# Jarl of Møre should marry Hild
		character:186004 = { marry = character:186005 }
		
		# Get rid of his current eldest legitimate son (need to make new parents first)
		character:186006 = { save_scope_as = dead_son }
		create_character = {
			location = scope:dead_son.location
			culture = scope:dead_son.culture
			faith = scope:dead_son.faith
			gender_female_chance = 0
			dynasty = none
			age = 30
			save_scope_as = new_dummy_male
			after_creation = {
				death = { death_reason = death_disappearance }
			}
		}
		create_character = {
			location = scope:dead_son.location
			culture = scope:dead_son.culture
			faith = scope:dead_son.faith
			gender_female_chance = 100
			dynasty = none
			age = 30
			save_scope_as = new_dummy_female
			after_creation = {
				death = { death_reason = death_disappearance }
			}
		}
		character:186006 = {
			set_mother = scope:new_dummy_female
			set_father = scope:new_dummy_male
			set_to_lowborn = yes
			silent_disappearance_effect = yes
		}
		
		# the Jarl's bastard sons should already be adult
		character:186008 = {
			set_age = 19
			random_character_trait = {
				limit = { has_trait_category = childhood }
				character:186008 = { remove_trait = prev }
			}
			add_trait = education_diplomacy_3
			add_trait = generous
			add_trait = lustful
			add_trait = wrathful
			set_sexuality = heterosexual
		}
		character:186010 = {
			set_age = 17
			random_character_trait = {
				limit = { has_trait_category = childhood }
				character:186010 = { remove_trait = prev }
			}
			add_trait = education_martial_3
			add_trait = strong
			add_trait = gregarious
			add_trait = stubborn
			add_trait = vengeful
			set_sexuality = heterosexual
		}
		character:186009 = {
			set_age = 16
			random_character_trait = {
				limit = { has_trait_category = childhood }
				character:186009 = { remove_trait = prev }
			}
			add_trait = education_martial_4
			add_trait = brave
			add_trait = lustful
			add_trait = greedy
			add_trait = athletic
			add_prowess_skill = 5
			add_martial_skill = 3
		}
		# Thorir should be the eldest legitimate son
		character:186007 = {
			set_age = 7
			set_sexuality = heterosexual
		}
		# adding Ivar as the second eldest legitimate son
		create_character = {
			name = "Ivar"
			age = 5
			gender = male
			father = character:186004
			mother = character:186005
			dynasty_house = character:186004.house
			employer = character:186004
			culture = culture:norse
			faith = faith:norse_pagan
			random_traits = yes
		}
	
		### Changes to Orkney and Shetland
		
		# make them both Pictish and Insular
		title:c_orkney = {
			set_county_culture = culture:pictish
			set_county_faith = faith:insular_celtic
		}
		title:c_shetland = {
			set_county_culture = culture:pictish
			set_county_faith = faith:insular_celtic
		}
		# give them new Pictish rulers
		create_character = {
			location = title:c_orkney.title_province
			culture = culture:pictish
			faith = faith:insular_celtic
			dynasty = generate
			gender_female_chance = 0
			age = { 30 50 }
			random_traits = yes
			save_scope_as = orkney_ruler
		}
		create_character = {
			location = title:c_shetland.title_province
			culture = culture:pictish
			faith = faith:insular_celtic
			dynasty = generate
			gender_female_chance = 0
			age = { 30 50 }
			random_traits = yes
			save_scope_as = shetland_ruler
		}
		histmod_simple_gain_title_effect = { TITLE = c_orkney CHARACTER = scope:orkney_ruler }
		histmod_simple_gain_title_effect = { TITLE = c_shetland CHARACTER = scope:shetland_ruler }	
	}
}

# After game start Rollo setup
histmod.0060 = {
	scope = none
	hidden = yes
	
	trigger = {
		game_start_date = 867.1.1
		has_fp1_dlc_trigger = yes
		NOT = { has_game_rule = fp1_scandi_adventurers_off }
	}
	
	immediate = {
		# RICE loaded, game rule set to "Default Vanilla Rollo", we assume RICE's events are being followed
		if = {
			limit = {
				exists = global_var:is_RICE_loaded_global_variable
				has_game_rule = rollo_exists_vanilla
			}
			# Jarl of Møre will not be called af Møre
			character:186004 = {
				house = { set_house_name = dynn_TrO_nde }
				dynasty = { set_dynasty_name = dynn_TrO_nde }
			}
			# recreate the previous Hrolfr, but a bit younger
			create_non_rollo_hrolfr_effect = yes
		}
		# otherwise we assume the player is playing Rollo or just wants Rollo to exist early
		else_if = {
			limit = { has_game_rule = rollo_exists_vanilla }
			# upgrade rollo
			character:242 = {
				add_trait = physique_good_2
				add_character_flag = { flag = immune_to_disease years = 40 }
				add_prowess_skill = 6
				give_nickname = nick_the_walker
				set_sexuality = heterosexual
				house = {
					set_house_name_from_house = character:186004.house
					set_coa = c_norwegian_more_norse
				}
				dynasty = {
					set_dynasty_name = dynn_afMore
					set_coa = c_norwegian_more_norse
				}
				title:d_laamp_hrolfr = { set_coa = c_norwegian_more_norse }
				# an AI Rollo will wait 19 years to marry Poppa
				if = {
					limit = { is_ai = yes }
					add_trait = histmod_will_not_wed
					trigger_event = { id = histmod.0205 years = 19 } # Rollo meets Poppa
				}
				else = {
					# player gets the event in 5 years but can get married earlier if they want
					trigger_event = { id = histmod.0205 years = 5 } # Rollo meets Poppa
				}
				# also, when the "Rollo Always Invades" rule is set, an AI Rollo gets a modifier to keep him from using the LAAMP cb's
				if = {
					limit = {
						is_ai = yes
						has_game_rule = rollo_always_invades
					}
					add_character_modifier = {
						modifier = histmod_save_for_planned_war
					}
					# and it'll be removed in 900AD
					trigger_event = { id = histmod.0206 years = 33 } # remove Rollo modifier
					# also some extra gold
					add_gold = 150
				}
			}
			# save that rollo as the rollo we need to reference
			set_global_variable = { name = rollo_adventurer value = character:242 }
			# Jarl of Møre will not be called af Møre
			character:186004 = {
				house = { set_house_name = dynn_TrO_nde }
				dynasty = { set_dynasty_name = dynn_TrO_nde }
			}
			# recreate the previous Hrolfr, but a bit younger
			create_non_rollo_hrolfr_effect = yes
		}
		# game rule set to "Rollo Exists in Möre"
		else_if = {
			limit = { has_game_rule = rollo_is_norwegian }
			# get rid of the current Rollo
			remove_existing_rollo_effect = yes
			# let's make sure Thorir doesn't randomly die of disease
			character:186007 = {
				add_character_flag = { flag = immune_to_disease years = 40 }
			}
			# create a younger son to the Jarl of Møre who will be Rollo
			create_character = {
				name = "Hrolfr"
				age = 4
				gender = male
				father = character:186004
				mother = character:186005
				dynasty_house = character:186004.house
				employer = character:186004
				culture = culture:norse
				faith = faith:norse_pagan
				trait = physique_good_2
				trait = rowdy
				random_traits = yes
				save_scope_as = rollo
				after_creation = {
					add_character_flag = is_rollo
					set_focus = education_martial
					add_character_flag = { flag = immune_to_disease years = 40 }
					copy_inheritable_appearance_from = character:242
					set_variable = { name = education_martial_variable value = age }
					set_sexuality = heterosexual
					scope:rollo = { remove_bad_congenital_traits = yes }
					trigger_event = { id = histmod.0200 years = 8 } # Rollo becomes brave
					trigger_event = { id = histmod.0202 years = 11 } # Young Rollo is nearly an adult
					trigger_event = { id = histmod.0204 years = 15 } # Rollo becomes an adventurer (if he has not already)
					trigger_event = { id = histmod.0205 years = 19 } # Rollo meets Poppa
					trigger_event = { id = histmod.0206 years = 33 } # Remove Rollo modifier
				}
			}
			# save that rollo as the rollo we need to reference
			set_global_variable = { name = rollo_adventurer value = scope:rollo }
			# his father should be friends with Harald
			character:186004 = {
				add_character_flag = { flag = immune_to_disease years = 10 }
				set_random_friendship_reason = { TARGET = character:144000 }
			}
		}
		# game rule set to "Rollo Appears Later"
		else = {
			# get rid of the current Rollo
			remove_existing_rollo_effect = yes
			# recreate the previous Hrolfr, but a bit younger
			create_non_rollo_hrolfr_effect = yes
			# fire the event to spawn Rollo in 900AD
			title:c_norwegian_more = {
				trigger_event = {
					id = histmod.0061
					years = 33
				}
			}
		}
	}
}

# Spawn Rollo in northern Denmark
histmod.0061 = {
	scope = none
	hidden = yes
	
	immediate = {
		create_character = {
			name = "Hrolfr"
			age = 40
			gender = male
			location = title:c_sjaelland.title_province
			culture = culture:norse
			faith = faith:norse_pagan
			dynasty = generate
			trait = lustful
			trait = arrogant
			trait = strong
			trait = brave
			trait = education_martial_4
			trait = viking
			trait = physique_good_2
			martial = 7
			diplomacy = 6
			intrigue = 8
			stewardship = 7
			learning = 5
			prowess = 15
			random_traits = no
			save_scope_as = rollo
			after_creation = {
				add_character_flag = is_rollo
				add_character_flag = prioritised_scandinavian_adventurer
				copy_inheritable_appearance_from = character:242
				add_character_flag = { flag = immune_to_disease years = 30 }
				add_gold = 250
				
				house = { set_house_name = dynn_afFasge }
				dynasty = { set_dynasty_name = dynn_afFasge }
				
				give_nickname = nick_the_walker
				set_sexuality = heterosexual
				add_to_global_variable_list = { name = western_scandinavian_adventurer_list target = this }
			}
		}
		# save that rollo as the rollo we need to reference
		set_global_variable = { name = rollo_adventurer value = scope:rollo }
		create_character = {
			name = "Poppa"
			age = 30
			gender = female
			location = scope:rollo.location
			culture = culture:french
			faith = faith:catholic
			dynasty = generate
			trait = education_learning_3
			martial = 7
			diplomacy = 7
			intrigue = 5
			stewardship = 7
			random_traits = yes
			save_scope_as = poppa
			after_creation = {
				marry = scope:rollo
				set_sexuality = heterosexual
				add_character_flag = { flag = immune_to_disease years = 30 }
				dynasty = { set_dynasty_name = dynn_deBayeaux }
				house = { set_house_name = dynn_deBayeaux }
				scope:poppa = { remove_bad_congenital_traits = yes }
			}
		}
		create_character = {
			name = "Vilhjálmr"
			age = 7
			gender = male
			father = scope:rollo
			mother = scope:poppa
			dynasty = none
			location = scope:rollo.location
			culture = culture:norse
			faith = faith:norse_pagan
			dynasty = none
			trait = physique_good_1
			trait = bossy
			random_traits = yes
			save_scope_as = william
			after_creation = {
				set_house = scope:rollo.house
				add_character_flag = is_william_longsword
				set_relation_guardian = scope:rollo
				set_focus = education_martial
				add_character_flag = { flag = immune_to_disease years = 30 }
				set_variable = { name = education_martial_variable value = age }
				scope:william = { remove_bad_congenital_traits = yes }
			}
		}
		if = {
			limit = { has_dlc_feature = roads_to_power }
			scope:rollo = {
				character_becomes_adventurer_effect = { ADVENTURER = scope:rollo BAND = d_laamp_hrolfr COA = scope:rollo.dynasty }
			}
		}
	}
}

scripted_trigger can_vassalize_attacking_adventurer_trigger = {
	# Must be able to vassalise a duke.
	highest_held_title_tier >= tier_kingdom
	# Target duchy doesn't contain a player that isn't scope:defender.
	trigger_if = {
		limit = {
			scope:attacker = { is_ai = yes }
		}
		NOT = {
			any_player = {
				# First, see if they're a vassal of scope:defender.
				any_liege_or_above = { this = scope:defender }
				# And if they are, make sure they have no titles held within the appropriate lands.
				save_temporary_scope_as = vassal_player
				any_in_list = {
					list = target_titles
					holder = scope:vassal_player
				}
			}
		}
	}
	# Scope:defender must control at least some lands outside of the target duchy.
	any_held_title = {
		NOT = { is_in_list = target_titles }
	}
}

# Initial event spawned by Rollo Invasion cb or laamp Scandi Adventurer cb
histmod.0102 = {
	type = character_event
	title = histmod.0102.t
	desc = histmod.0102.desc
	theme = war
	left_portrait = {
		character = scope:defender
		animation = worry
	}
	right_portrait = {
		character = scope:attacker
		animation = aggressive_axe
	}
	override_background = { reference = fp1_beached_longships }

	trigger = {
		# DLC check.
		has_fp1_dlc_trigger = yes
		# Make sure the war is still ongoing
		any_character_war = {
			OR = {
				using_cb = fp1_rollo_adventurer_conquest
				using_cb = histmod_laamp_adventurer_duchy_invasion
			}
			primary_attacker = scope:attacker
			primary_defender = scope:defender
		}
	}

	immediate = {
		# Grab the CB for general reference.
		random_character_war = {
			limit = {
				OR = {
					using_cb = fp1_rollo_adventurer_conquest
					using_cb = histmod_laamp_adventurer_duchy_invasion
				}
				primary_attacker = scope:attacker
				primary_defender = scope:defender
			}
			save_scope_as = sa_war
		}

		# Check for alliance marriage candidate, if this is for Normandy
		if = {
			limit = {
				is_ai = yes
				scope:attacker.culture = culture:norse
				any_in_list = {
					list = target_titles
					this = title:c_rouen
					culture = culture:french
				}
				NOT = { has_game_rule = always_invades_no_special_rules }
			}
		}
	}

	# Never! Back to your frozen hellscape of a home, scum!
	option = {
		name = fp1_trade_events.1131.a

		# Gain a bit of prestige for your boasting.
		add_prestige = medium_prestige_gain
		# Stress gain trade-off handled in stress impact.
		# Clarify that this is merely another trade-off option.
		custom_tooltip = fp1_trade_events.1131.d.tt

		stress_impact = {
			base = medium_stress_gain
			arrogant = minor_stress_impact_loss
			ambitious = minor_stress_impact_loss
			stubborn = minor_stress_impact_loss
			vengeful = minor_stress_impact_loss
			humble = medium_stress_impact_gain
			content = medium_stress_impact_gain
			fickle = medium_stress_impact_gain
			forgiving = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_vengefulness = 0.5
				ai_energy = 0.25
			}
			modifier = {	# Weight up for stress.
				add = 10
				has_trait = arrogant
			}
			modifier = {	# Weight up for stress.
				add = 10
				has_trait = ambitious
			}
			modifier = {	# Weight up for stress.
				add = 10
				has_trait = stubborn
			}
			modifier = {	# Weight up for stress.
				add = 10
				has_trait = vengeful
			}
			modifier = {	# Weight down for stress.
				add = -20
				has_trait = humble
			}
			modifier = {	# Weight down for stress.
				add = -20
				has_trait = content
			}
			modifier = {	# Weight down for stress.
				add = -20
				has_trait = fickle
			}
			modifier = {	# Weight down for stress.
				add = -20
				has_trait = forgiving
			}
		}
	}
	
	# Well, if it's land they want...
	option = {
		name = fp1_trade_events.1131.b
		trigger = {
			can_vassalize_attacking_adventurer_trigger = yes
		}
		stress_impact = {
			generous = minor_stress_impact_loss
			humble = minor_stress_impact_loss
			content = minor_stress_impact_loss
			fickle = minor_stress_impact_loss
			forgiving = minor_stress_impact_loss
			greedy = major_stress_impact_gain
			arrogant = major_stress_impact_gain
			ambitious = major_stress_impact_gain
			stubborn = major_stress_impact_gain
			vengeful = major_stress_impact_gain
		}
		save_scope_value_as = {
			name = war_result
			value = flag:is_negotiated
		}
		if = {
			limit = {
				scope:attacker = { is_ai = no }
			}
			custom_tooltip = offer_deal_player_scandi_adventurer
			show_as_tooltip = {
				add_prestige = major_prestige_loss
				scandi_adventurer_deal_effect = yes
				fp1_add_realm_size_appropriate_sa_reprieve_effect = yes
			}
			scope:attacker = { trigger_event = histmod.0104 }
		}
		else = {
			add_prestige = major_prestige_loss
			scandi_adventurer_deal_effect = yes
			fp1_add_realm_size_appropriate_sa_reprieve_effect = yes
		}
		ai_chance = {
			base = 20
			ai_value_modifier = {
				ai_rationality = 0.25
				ai_greed = -0.5
				ai_vengefulness = -0.75
			}
			modifier = {	# Weight up for stress.
				add = 10
				has_trait = generous
			}
			modifier = {	# Weight up for stress.
				add = 10
				has_trait = humble
			}
			modifier = {	# Weight up for stress.
				add = 10
				has_trait = content
			}
			modifier = {	# Weight up for stress.
				add = 10
				has_trait = fickle
			}
			modifier = {	# Weight up for stress.
				add = 10
				has_trait = forgiving
			}
			modifier = {	# Weight down for stress.
				add = -30
				has_trait = greedy
			}
			modifier = {	# Weight down for stress.
				add = -30
				has_trait = arrogant
			}
			modifier = {	# Weight down for stress.
				add = -30
				has_trait = ambitious
			}
			modifier = {	# Weight down for stress.
				add = -30
				has_trait = stubborn
			}
			modifier = {	# Weight down for stress.
				add = -30
				has_trait = vengeful
			}
		}
	}
	
	# In my experience, soldiers prefer gold to glory.
	option = {
		name = fp1_trade_events.1131.c
		trigger = {
			can_make_expensive_purchase_trigger = { PRICE = monumental_gold_value }
			# not if this is for Normandy
			NAND = {
				scope:attacker.culture = culture:norse
				any_in_list = {
					list = target_titles
					this = title:d_normandy
				}
				title:c_rouen = { culture = culture:french }
				NOT = { has_game_rule = always_invades_no_special_rules }
			}
		}

		# Pay scope:attacker's troops to sod off...
		custom_tooltip = fp1_trade_events.1131.c.tt
		pay_short_term_gold = {
			target = scope:attacker
			gold = monumental_gold_value
		}
		# ... thereby winning the war.
		scope:sa_war = { end_war = defender }
		reverse_add_opinion = {
			target = scope:attacker
			modifier = humiliated_opinion
			opinion = -50
		}
		# Add a breather before the next SA.
		fp1_add_realm_size_appropriate_sa_reprieve_effect = yes

		stress_impact = {
			generous = minor_stress_impact_loss
			humble = minor_stress_impact_loss
			content = minor_stress_impact_loss
			fickle = minor_stress_impact_loss
			forgiving = minor_stress_impact_loss
			greedy = major_stress_impact_gain
			arrogant = major_stress_impact_gain
			ambitious = major_stress_impact_gain
			stubborn = major_stress_impact_gain
			vengeful = major_stress_impact_gain
		}
		ai_chance = {
			# Base allowed to stay high, since the fiscal purchase here is pretty high and the AI usually won't make it anyway.
			base = 100
			ai_value_modifier = {
				ai_rationality = 0.25
				ai_greed = -0.5
				ai_vengefulness = -0.75
			}
			modifier = {	# Weight up for stress.
				add = 10
				has_trait = generous
			}
			modifier = {	# Weight up for stress.
				add = 10
				has_trait = humble
			}
			modifier = {	# Weight up for stress.
				add = 10
				has_trait = content
			}
			modifier = {	# Weight up for stress.
				add = 10
				has_trait = fickle
			}
			modifier = {	# Weight up for stress.
				add = 10
				has_trait = forgiving
			}
			modifier = {	# Weight down for stress.
				add = -30
				has_trait = greedy
			}
			modifier = {	# Weight down for stress.
				add = -30
				has_trait = arrogant
			}
			modifier = {	# Weight down for stress.
				add = -30
				has_trait = ambitious
			}
			modifier = {	# Weight down for stress.
				add = -30
				has_trait = stubborn
			}
			modifier = {	# Weight down for stress.
				add = -30
				has_trait = vengeful
			}
		}
	}	
	
	# Then we'll banish these Nords to the sea.
	option = {
		name = fp1_trade_events.1131.d

		# Opt-out.
		custom_tooltip = fp1_trade_events.1131.d.tt

		# No stress impact for the opt-out.
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_energy = -0.25
				ai_vengefulness = -0.25
			}
		}
	}
}

# French king considers an offer of vassalage once the war is over
histmod.0103 = {
	type = character_event
	title = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:war_result = flag:defender_victory }
				desc = histmod.0103.t.victory
			}
			triggered_desc = {
				trigger = { scope:war_result = flag:white_peace }
				desc = histmod.0103.t.whitepeace
			}
			desc = histmod.0103.t.defeat
		}
	}
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:war_result = flag:defender_victory }
				desc = histmod.0103.desc.victory
			}
			triggered_desc = {
				trigger = { scope:war_result = flag:white_peace }
				desc = histmod.0103.desc.whitepeace
			}
			desc = histmod.0103.desc.defeat
		}
		desc = histmod.0103.desc.mid
		first_valid = {
			triggered_desc = {
				trigger = {
					NOT = { scope:war_result = flag:defender_defeat }
				}
				desc = histmod.0103.desc.receive
			}
			desc = histmod.0103.desc.keep
		}
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:alliance_marriage }
				desc = histmod.0103.desc.marry
			}
			desc = histmod.0103.desc.alliance
		}
		desc = histmod.0103.desc.end
	}
	theme = war
	
	left_portrait = {
		character = scope:defender
		animation = thinking
	}
	right_portrait = {
		character = scope:attacker
		triggered_animation = {
			trigger = { scope:war_result = flag:defender_defeat }
			animation = war_over_win
		}
		triggered_animation = {
			trigger = { scope:war_result = flag:white_peace }
			animation = war_over_tie
		}
		animation = war_over_loss
	}
	lower_right_portrait = scope:alliance_marriage
	override_background = { reference = battlefield }
	
	trigger = {
		can_vassalize_attacking_adventurer_trigger = yes
		scope:attacker.culture = culture:norse
		any_in_list = {
			list = target_titles
			this = title:d_normandy
		}
		title:c_rouen = { culture = culture:french }
		NOR = {
			has_game_rule = always_invades_no_special_rules
			has_game_rule = no_forced_normandy_invasion
			scope:attacker = { has_character_flag = no_more_adventurer_negotiation }
			AND = {
				has_game_rule = rollo_exists_vanilla
				scope:attacker = { has_character_flag = is_rollo }
				exists = global_var:is_RICE_loaded_global_variable
			}
		}
		trigger_if = {
			limit = { scope:war_result = flag:defender_defeat }
			scope:attacker = { top_liege = this }
			title:d_normandy.holder = scope:attacker
		}
		trigger_else = {
			title:c_rouen.holder.top_liege = root
		}
	}

	immediate = {
		# check if there's a candidate for an alliance marriage
		if = {
			limit = {
				scope:attacker = { is_male = yes }
				NOT = { scope:war_result = flag:defender_victory }
			}
			every_close_or_extended_family_member = {
				limit = {
					is_good_alliance_marriage_candidate = { PROPOSER = scope:defender RECIPIENT = scope:attacker }
				}
				add_to_list = potential_alliance_marriage
			}
			if = {
				limit = {
					list_size = { name = potential_alliance_marriage value > 0 }
				}
				ordered_in_list = {
					list = potential_alliance_marriage
					order_by = {
						value = age
						if = {
							limit = { is_close_family_of = scope:defender }
							add = 20
						}
					}
					save_scope_as = alliance_marriage
				}
			}
		}
	}
	
	option = {
		name = histmod.0103.a # Send the offer
		trigger = { scope:attacker = { is_ai = no } }
		custom_tooltip = offer_deal_player_scandi_adventurer
		show_as_tooltip = {
			add_prestige = major_prestige_gain
			scandi_adventurer_deal_effect = yes
			if = {
				limit = { has_character_modifier = fp1_reprieve_from_sa_modifier }
				hidden_effect = { remove_character_modifier = fp1_reprieve_from_sa_modifier }
			}
			add_character_modifier = {
				modifier = fp1_reprieve_from_sa_modifier
				years = 25
			}
		}
		scope:attacker = { trigger_event = histmod.0104 }
	}
	option = {
		name = histmod.0103.b # It's a deal
		trigger = { scope:attacker = { is_ai = yes } }
		add_prestige = major_prestige_gain
		scandi_adventurer_deal_effect = yes
		if = {
			limit = { has_character_modifier = fp1_reprieve_from_sa_modifier }
			hidden_effect = { remove_character_modifier = fp1_reprieve_from_sa_modifier }
		}
		add_character_modifier = {
			modifier = fp1_reprieve_from_sa_modifier
			years = 25
		}
	}
	option = {
		name = histmod.0103.c # Absolutely not!
		trigger = { is_ai = no }
		every_vassal = {
			limit = {
				any_held_title = {
					title_tier = county
					target_is_de_jure_liege_or_above = title:d_normandy
				}
			}
			add_opinion = {
				target = root
				modifier = grateful_opinion
				opinion = 20
			}
		}
	}
}

# Offer sent to player adventurer
histmod.0104 = {
	type = letter_event
	opening = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:war_result = flag:is_negotiated }
				desc = histmod.0104.opening.is.negotiated
			}
			triggered_desc = {
				trigger = { scope:war_result = flag:defender_victory }
				desc = histmod.0104.opening.victory
			}
			triggered_desc = {
				trigger = { scope:war_result = flag:white_peace }
				desc = histmod.0104.opening.whitepeace
			}
			desc = histmod.0104.opening.defeat
		}
	}
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:war_result = flag:is_negotiated }
				desc = histmod.0104.desc.is.negotiated
			}
			triggered_desc = {
				trigger = { scope:war_result = flag:defender_victory }
				desc = histmod.0104.desc.victory
			}
			triggered_desc = {
				trigger = { scope:war_result = flag:white_peace }
				desc = histmod.0104.desc.whitepeace
			}
			desc = histmod.0104.desc.defeat
		}
		desc = histmod.0104.desc.mid
		first_valid = {
			triggered_desc = {
				trigger = { scope:war_result = flag:is_negotiated }
				desc = histmod.0104.desc.receive.neg
			}
			triggered_desc = {
				trigger = {
					NOT = { scope:war_result = flag:defender_defeat }
				}
				desc = histmod.0104.desc.receive
			}
			desc = histmod.0104.desc.keep
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:alliance_marriage
					scope:attacker = { is_male = yes }
					NOT = { scope:war_result = flag:defender_victory }
				}
				desc = histmod.0104.desc.marry
			}
			triggered_desc = {
				trigger = {
					NOT = { has_game_rule = always_invades_no_special_rules }
					scope:attacker.culture = culture:norse
					any_in_list = {
						list = target_titles
						this = title:d_normandy
					}
					title:c_rouen = { culture = culture:french }
				}
				desc = histmod.0104.desc.alliance
			}
			desc =  histmod.0104.desc.truce
		}
		desc = histmod.0104.desc.end
	}
	sender = {
		character = scope:defender
		triggered_animation = {
			trigger = { scope:war_result = flag:defender_defeat }
			animation = throne_room_bow_1
		}
		animation = thinking
	}
	
	option = {
		name = histmod.0104.a #I agree
		scandi_adventurer_deal_effect = yes
		scope:defender = {
			hidden_effect = {
				if = {
					limit = { scope:war_result = flag:is_negotiated }
					add_prestige = major_prestige_loss
					fp1_add_realm_size_appropriate_sa_reprieve_effect = yes
				}
				else = {
					add_prestige = major_prestige_gain
					scandi_adventurer_deal_effect = yes
					if = {
						limit = { has_character_modifier = fp1_reprieve_from_sa_modifier }
						hidden_effect = { remove_character_modifier = fp1_reprieve_from_sa_modifier }
					}
					add_character_modifier = {
						modifier = fp1_reprieve_from_sa_modifier
						years = 25
					}
				}
			}
		}
	}
	option = {
		name = histmod.0104.b #Begone with you
		add_character_flag = no_more_adventurer_negotiation
	}
}

# Rollo changes to Norman culture
histmod.0105 = {
	type = character_event
	title = histmod.0105.t
	desc = histmod.0105.desc
	
	theme = realm
	right_portrait = {
		character = root
		animation = thinking
	}
	
	immediate = {
		hidden_effect = {
			top_liege = { save_scope_as = my_liege }
			add_to_global_variable_list = {
				name = unavailable_unique_decisions
				target = flag:flag_formed_normandy_decision_effect
			}
		}
	}
	
	option = {
		name = histmod.0105.a
		custom_tooltip = create_new_dynasty_normandie_tt
		histmod_create_dynasty_effect = { NAME = dynn_Normandie PREFIX = dynnp_de COA = d_normandy }

		if = {
			limit = { has_character_flag = is_rollo }
			change_first_name = "Rollo"
			dynasty = {
				random_dynasty_member = {
					limit = {
						has_character_flag = is_william_longsword
						culture = culture:norman
					}
					change_first_name = "William"
				}
			}
		}
		else_if = {
			limit = { has_character_flag = is_william_longsword }
			change_first_name = "William"
		}
		if = {
			limit = { has_trait = bastard }
			remove_trait = bastard
			add_trait = bastard_founder
		}
		
		# give up current claims
		every_claim = {
			root = { remove_claim = prev }
		}
		
		ai_chance = {
			base = 100
		}
	}
	option = {
		name = histmod.0105.b
		ai_chance = {
			base = 0
		}
	}
}

# Rollo becomes brave
histmod.0200 = {
	type = character_event
	hidden = yes
	
	trigger = {
		is_alive = yes
		culture = culture:norse
		is_ruler = no
	}
	
	immediate = {
		if = {
			limit = {
				liege ?= { is_ai = no }
				OR = {
					has_trait = craven
					NOT = { has_trait = brave }
				}
			}
			save_scope_as = rollo
			liege = { trigger_event = histmod.0201 }
		}
		else = {
			if = {
				limit = { has_trait = craven }
				remove_trait = craven
				add_trait = brave
			}
			else_if = {
				limit = {
					NOT = { has_trait = brave }
					any_character_trait = {
						has_trait_category = personality
						NOR = {
							this = trait:arrogant
							this = trait:lustful
						}
					}
				}
				random_character_trait = {
					limit = {
						has_trait_category = personality
						NOR = {
							this = trait:arrogant
							this = trait:lustful
						}
					}
					root = {
						remove_trait = prev
						add_trait = brave
					}
				}
			}
			else_if = {
				limit = {
					NOT = { has_trait = brave }
					number_of_personality_traits < 3
				}
				add_trait = brave
			}
		}
	
		#add the will not wed trait
		if = {
			limit = {
				OR = {
					is_courtier = no
					liege = { is_ai = yes }
				}
			}
			if = {
				limit = { exists = betrothed }
				break_betrothal = root.betrothed
			}
			add_trait = histmod_will_not_wed
		}
	}
}

histmod.0201 = {
	type = character_event
	title = histmod.0201.t
	desc = histmod.0201.desc
	theme = family
	
	left_portrait = {
		character = scope:rollo
		animation = personality_bold
	}
	override_background = { reference = army_camp }
	
	immediate = {
		hidden_effect = {
			scope:rollo = {
				if = {
					limit = { has_trait = craven }
					trait:craven = { save_scope_as = trait_to_remove }
					trait:brave = { save_scope_as = trait_to_add }
				}
				else_if = {
					limit = {
						NOT = { has_trait = brave }
						any_character_trait = {
							has_trait_category = personality
							NOR = {
								this = trait:arrogant
								this = trait:lustful
							}
						}
					}
					trait:brave = { save_scope_as = trait_to_add }
					random_character_trait = {
						limit = {
							has_trait_category = personality
							NOR = {
								this = trait:arrogant
								this = trait:lustful
							}
						}
						save_scope_as = trait_to_remove
					}
				}
				else_if = {
					limit = {
						NOT = { has_trait = brave }
						number_of_personality_traits < 3
					}
					trait:brave = { save_scope_as = trait_to_add }
				}
			}
		}
	}
	
	option = {
		name = histmod.0201.a
		scope:rollo = {
			if = {
				limit = { exists = scope:trait_to_remove }
				remove_trait = scope:trait_to_remove
			}
			if = {
				limit = { exists = scope:trait_to_add }
				add_trait = scope:trait_to_add
			}
		}
	}
}

# Young Rollo is nearly an adult
histmod.0202 = {
	type = character_event
	hidden = yes
	
	trigger = {
		is_alive = yes
		culture = culture:norse
		is_ruler = no
	}
	
	immediate = {
		if = {
			limit = {
				liege ?= { is_ai = no }
				OR = {
					has_trait = chaste
					has_trait = humble
					NOT = { has_trait = lustful }
					NOT = { has_trait = arrogant }
				}
			}
			save_scope_as = rollo
			liege = { trigger_event = histmod.0203 }
		}
		else = {
			if = {
				limit = { has_trait = chaste }
				remove_trait = chaste
				add_trait = lustful
			}
			else_if = {
				limit = {
					NOT = { has_trait = lustful }
					any_character_trait = {
						has_trait_category = personality
						NOR = {
							this = trait:brave
							this = trait:arrogant
							this = trait:lustful
						}
					}
				}
				random_character_trait = {
					limit = {
						has_trait_category = personality
						NOR = {
							this = trait:brave
							this = trait:arrogant
							this = trait:lustful
						}
					}
					root = {
						remove_trait = prev
						add_trait = lustful
					}
				}
			}
			else_if = {
				limit = {
					NOT = { has_trait = lustful }
					number_of_personality_traits < 3
				}
				add_trait = lustful
			}
			
			if = {
				limit = { has_trait = humble }
				remove_trait = humble
				add_trait = arrogant
			}
			else_if = {
				limit = {
					NOT = { has_trait = arrogant }
					any_character_trait = {
						has_trait_category = personality
						NOR = {
							this = trait:brave
							this = trait:arrogant
							this = trait:lustful
						}
					}
				}
				random_character_trait = {
					limit = {
						has_trait_category = personality
						NOR = {
							this = trait:brave
							this = trait:arrogant
							this = trait:lustful
						}
					}
					root = {
						remove_trait = prev
						add_trait = arrogant
					}
				}
			}
			else_if = {
				limit = {
					NOT = { has_trait = arrogant }
					number_of_personality_traits < 3
				}
				add_trait = arrogant
			}
		}
		if = {
			limit = {
				is_ai = yes
				has_game_rule = rollo_always_invades
			}
			add_character_modifier = {
				modifier = histmod_save_for_planned_war
			}
		}
	}
}

histmod.0203 = {
	type = character_event
	title = histmod.0203.t
	desc = histmod.0203.desc
	theme = family
	
	left_portrait = {
		character = scope:rollo
		animation = personality_bold
	}
	
	immediate = {
		hidden_effect = {
			scope:rollo = {
				if = {
					limit = { has_trait = chaste }
					trait:chaste = { save_scope_as = trait1_to_remove }
					trait:lustful = { save_scope_as = trait1_to_add }
				}
				else_if = {
					limit = {
						NOT = { has_trait = lustful }
						any_character_trait = {
							has_trait_category = personality
							NOR = {
								this = trait:brave
								this = trait:arrogant
								this = trait:lustful
								this = trait:humble
							}
						}
					}
					trait:lustful = { save_scope_as = trait1_to_add }
					random_character_trait = {
						limit = {
							has_trait_category = personality
							NOR = {
								this = trait:brave
								this = trait:arrogant
								this = trait:lustful
								this = trait:humble
							}
						}
						save_scope_as = trait1_to_remove
					}
				}
				else_if = {
					limit = {
						NOT = { has_trait = lustful }
						number_of_personality_traits < 3
					}
					trait:lustful = { save_scope_as = trait_to_add }
				}
				
				if = {
					limit = { has_trait = humble }
					trait:humble = { save_scope_as = trait2_to_remove }
					trait:arrogant = { save_scope_as = trait2_to_add }
				}
				else_if = {
					limit = {
						NOT = { has_trait = arrogant }
						any_character_trait = {
							has_trait_category = personality
							NOR = {
								this = trait:brave
								this = trait:arrogant
								this = trait:lustful
								AND = {
									exists = scope:trait1_to_remove
									this = scope:trait1_to_remove
								}
							}
						}
					}
					trait:arrogant = { save_scope_as = trait2_to_add }
					random_character_trait = {
						limit = {
							has_trait_category = personality
							NOR = {
								this = trait:brave
								this = trait:arrogant
								this = trait:lustful
								AND = {
									exists = scope:trait1_to_remove
									this = scope:trait1_to_remove
								}
							}
						}
						save_scope_as = trait2_to_remove
					}
				}
				else_if = {
					limit = {
						NOT = { has_trait = arrogant }
						number_of_personality_traits < 3
					}
					trait:arrogant = { save_scope_as = trait2_to_add }
				}
			}
		}
	}
	
	option = {
		name = histmod.0203.a
		scope:rollo = {
			if = {
				limit = { exists = scope:trait1_to_remove }
				remove_trait = scope:trait1_to_remove
			}
			if = {
				limit = { exists = scope:trait2_to_remove }
				remove_trait = scope:trait2_to_remove
			}
			if = {
				limit = { exists = scope:trait1_to_add }
				add_trait = scope:trait1_to_add
			}
			if = {
				limit = { exists = scope:trait2_to_add }
				add_trait = scope:trait2_to_add
			}
		}
	}
}

# Rollo becomes an adventurer (if he has not already)
histmod.0204 = {
	type = character_event
	hidden = yes
	
	trigger = {
		is_alive = yes
		culture = culture:norse
		is_ruler = no
		has_dlc_feature = roads_to_power
		is_valid_for_laampdom = yes
	}
	
	on_trigger_fail = {
		if = {
			limit = {
				is_alive = yes
				culture = culture:norse
				is_ai = yes
			}
			add_character_flag = prioritised_scandinavian_adventurer
			add_to_global_variable_list = { name = western_scandinavian_adventurer_list target = this }
			if = {
				limit = { has_any_nickname = no }
				give_nickname = nick_the_walker
			}
		}
		if = {
			limit = {
				is_alive = yes
				OR = {
					is_ai = no
					is_landed = yes
				}
				has_character_modifier = histmod_save_for_planned_war
			}
			remove_character_modifier = histmod_save_for_planned_war
			add_martial_skill = 1
			add_prowess_skill = 1
		}
	}
	
	immediate = {
		save_scope_as = rollo
		add_gold = 250
		character_becomes_adventurer_effect = { ADVENTURER = scope:rollo BAND = d_laamp_hrolfr COA = scope:rollo.dynasty }
		add_character_flag = prioritised_scandinavian_adventurer
		add_to_global_variable_list = { name = western_scandinavian_adventurer_list target = this }
		if = {
			limit = { has_any_nickname = no }
			give_nickname = nick_the_walker
		}
	}
}

# Rollo meets Poppa
histmod.0205 = {
	type = character_event
	title = histmod.0205.t
	desc = histmod.0205.desc
	theme = love
	
	left_portrait = {
		character = scope:poppa
		animation = flirtation
	}
	override_background = { reference = bp1_crossroads_inn }
	
	trigger = {
		is_alive = yes
		culture = culture:norse
		has_government = landless_adventurer_government
		has_trait = lustful
		has_sexuality = heterosexual
	}
	
	on_trigger_fail = {
		if = {
			limit = {
				is_alive = yes
				has_trait = histmod_will_not_wed
			}
			remove_trait = histmod_will_not_wed
		}
		if = {
			limit = {
				is_alive = yes
				OR = {
					is_ai = no
					is_landed = yes
				}
				has_character_modifier = histmod_save_for_planned_war
			}
			remove_character_modifier = histmod_save_for_planned_war
			add_martial_skill = 1
			add_prowess_skill = 1
		}
	}
	
	immediate = {
		hidden_effect = {
			create_character = {
				name = "Poppa"
				age = 16
				gender = female
				location = root.location
				culture = culture:french
				faith = faith:catholic
				dynasty = generate
				trait = education_learning_3
				trait = beauty_good_1
				martial = 7
				diplomacy = 7
				intrigue = 5
				stewardship = 7
				random_traits = yes
				save_scope_as = poppa
				after_creation = {
					set_sexuality = heterosexual
					add_character_flag = { flag = immune_to_disease years = 30 }
					dynasty = { set_dynasty_name = dynn_deBayeaux }
					house = { set_house_name = dynn_deBayeaux }
					scope:poppa = { remove_bad_congenital_traits = yes }
				}
			}
		}
		had_sex_with_effect = {
			CHARACTER = scope:poppa
			PREGNANCY_CHANCE = pregnancy_chance
		}
		if = {
			limit = { has_trait = histmod_will_not_wed }
			remove_trait = histmod_will_not_wed
		}
		if = {
			limit = {
				is_ai = no
				has_character_modifier = histmod_save_for_planned_war
			}
			remove_character_modifier = histmod_save_for_planned_war
			add_martial_skill = 1
			add_prowess_skill = 1
		}
	}
	
	option = {
		name = histmod.0205.a #I'll marry her!
		trigger = {
			can_marry_character_trigger = { CHARACTER = scope:poppa }
		}
		stress_impact = { base = major_stress_loss }
		marry = scope:poppa
		if = {
			limit = { num_of_relation_lover < 1 }
			set_relation_lover = {
				target = scope:poppa
				reason = fell_in_love_during_raid
			}
		}
		ai_chance = {
			base = 500
		}
	}
	option = {
		name = histmod.0205.b #I'll make her my concubine
		trigger = {
			allowed_concubines = yes
			number_of_concubines = 0
		}
		stress_impact = { base = medium_stress_loss }
		make_concubine = scope:poppa
		if = {
			limit = { num_of_relation_lover < 1 }
			set_relation_lover = {
				target = scope:poppa
				reason = fell_in_love_during_raid
			}
		}
		ai_chance = {
			base = 200
		}
	}
	option = {
		name = histmod.0205.c #She and I will be lovers
		trigger = {
			num_of_relation_lover < 1
		}
		stress_impact = { base = medium_stress_loss }
		add_courtier = scope:poppa
		set_relation_lover = {
			target = scope:poppa
			reason = fell_in_love_during_raid
		}
		ai_chance = {
			base = 100
		}
	}
	option = {
		name = histmod.0205.d #It was only a passing thing
		ai_chance = {
			base = 1
		}
	}
}

# Remove the Rollo modifier
histmod.0206 = {
	type = character_event
	hidden = yes
	
	trigger = {
		is_alive = yes
		has_character_modifier = histmod_save_for_planned_war
	}
	
	immediate = {
		remove_character_modifier = histmod_save_for_planned_war
		add_character_flag = prioritised_scandinavian_adventurer
		add_martial_skill = 1
		add_prowess_skill = 1
		if = {
			limit = { is_ruler = no }
			add_to_global_variable_list = { name = western_scandinavian_adventurer_list target = this }
		}
	}
}

### Karling Story events

scripted_trigger is_directly_related_to_player = {
	save_temporary_scope_as = potential_player_relative
	any_player = {
		OR = {
			is_parent_of = scope:potential_player_relative
			is_grandparent_of = scope:potential_player_relative
		}
	}
}

# Random misfortune might befall someone in the Karling line, checked every year
histmod.0300 = {
	type = character_event
	hidden = yes
	
	trigger = {
		dynasty = { this = dynasty:25061 }
	}
	
	on_trigger_fail = {
		if = {
			limit = { exists = dynasty:25061.dynast }
			scope:story = { make_story_owner = dynasty:25061.dynast }
		}
	}
	
	immediate = {
		dynasty = {
			# first count the number of inheritable men left
			every_dynasty_member = {
				limit = {
					dynasty = root.dynasty
					is_male = yes
					NOR = {
						has_trait = bastard
						has_trait = inbred
						has_trait = infirm
						has_trait = eunuch
						has_trait = devoted
						has_trait = order_member
						is_incapable = yes
						is_directly_related_to_player = yes
					}
				}
				add_to_list = inheritable_men
			}
			if = {
				limit = {
					list_size = { name = inheritable_men value > 2 }
				}
				# a pregnant female Karling in a matrilineal marriage have a greater chance of miscarriage/death
				random_dynasty_member = {
					limit = {
						has_trait = pregnant
						is_married = yes
						matrilinear_marriage = yes
						is_ai = yes
						birth_will_happen_as_usual_trigger = yes
						dynasty = root.dynasty
						is_directly_related_to_player = no
						NOT = { has_game_rule = events_karling_decline_rule }
					}
					random_list = {
						30 = {
							# birth ends prematurely (unless birth happens first)
							remove_character_flag ?= birth_will_go_smoothly
							remove_character_flag ?= birth_child_will_become_sickly
							remove_character_flag ?= birth_mother_will_become_ill
							trigger_event = { id = pregnancy.2101 days = { 15 60 } }
						}
						10 = {
							# mother will die during childbirth
							remove_character_flag ?= birth_will_go_smoothly
							remove_character_flag ?= birth_child_will_become_sickly
							remove_character_flag ?= birth_mother_will_become_ill
							add_character_flag = { flag = birth_mother_will_die days = 270 }
						}
						20 = {
							modifier = {
								factor = 0.5
								list_size = { name = inheritable_men value > 5 }
							}
							# nothing changes
						}
					}
				}
				# a Karling with a pregnant wife might have a greater chance of miscarriage/death
				random_dynasty_member = {
					limit = {
						is_married = yes
						patrilinear_marriage = yes
						is_ai = yes
						primary_spouse = {
							has_trait = pregnant
							birth_will_happen_as_usual_trigger = yes
						}
						NOR = {
							AND = {
								has_game_rule = events_karling_decline_rule
								NOR = {
									this = character:90107
									this = character:30228
								}
							}
							AND = {
								is_landed = yes
								highest_held_title_tier >= tier_duchy
								primary_heir = {
									OR = {
										has_title = title:k_france
										has_title = title:k_aquitaine
									}
								}
							}
						}
						dynasty = root.dynasty
						is_directly_related_to_player = no
					}
					primary_spouse = {
						random_list = {
							30 = {
								# birth ends prematurely (unless birth happens first)
								remove_character_flag ?= birth_will_go_smoothly
								remove_character_flag ?= birth_child_will_become_sickly
								remove_character_flag ?= birth_mother_will_become_ill
								trigger_event = { id = pregnancy.2101 days = { 5 30 } }
							}
							10 = {
								modifier = {
									add = -10
									NOT = { exists = global_var:lmf_is_loaded }
									OR = {
										has_game_rule = all_karling_decline_rule
										has_game_rule = events_karling_decline_rule
									}
									OR = {
										primary_spouse = character:90107
										primary_spouse = character:30228
									}
								}
								# mother will die during childbirth
								remove_character_flag ?= birth_will_go_smoothly
								remove_character_flag ?= birth_child_will_become_sickly
								remove_character_flag ?= birth_mother_will_become_ill
								add_character_flag = { flag = birth_mother_will_die days = 270 }
							}
							20 = {
								modifier = {
									factor = 0.5
									list_size = { name = inheritable_men value > 5 }
								}
								modifier = {
									factor = 0
									NOT = { exists = global_var:lmf_is_loaded }
									OR = {
										has_game_rule = all_karling_decline_rule
										has_game_rule = events_karling_decline_rule
									}
									OR = {
										primary_spouse = character:90107
										primary_spouse = character:30228
									}
								}
								# nothing changes
							}
						}
					}
				}
				# adult Karlings that already have two children might become barren (LMF only)
				random_dynasty_member = {
					limit = {
						exists = global_var:lmf_is_loaded
						is_married = yes
						is_ai = yes
						NOR = {
							has_character_flag = lmf_no_children
							has_game_rule = events_karling_decline_rule
						}
						any_child = {
							NOT = { has_trait = bastard }
							age >= 5
							count > 1
						}
						dynasty = root.dynasty
						is_directly_related_to_player = no
					}
					random = {
						chance = 30
						modifier = {
							add = 20
							list_size = { name = inheritable_men value > 5 }
						}
						add_character_flag = lmf_no_children
					}
				}
			}
			if = {
				limit = {
					list_size = { name = inheritable_men value > 1 }
				}
				# chance of accidental death
				random_dynasty_member = {
					limit = {
						save_temporary_scope_as = potential_death
						OR = {
							is_adult = yes
							is_ruler = yes
						}
						is_ai = yes
						fertility > 0
						dynasty = root.dynasty
						NOR = {
							is_directly_related_to_player = yes
							# some characters we don't want to kill off with an event until at least their historical death date
							AND = {
								OR = {
									has_game_rule = all_karling_decline_rule
									has_game_rule = events_karling_decline_rule
								}
								this = character:144998 #Lothair II
								current_year < 875
							}
							AND = {
								OR = {
									has_game_rule = all_karling_decline_rule
									has_game_rule = events_karling_decline_rule
								}
								this = character:90107 #Ludwig the German
								current_year < 876
							}
							AND = {
								OR = {
									has_game_rule = all_karling_decline_rule
									has_game_rule = events_karling_decline_rule
								}
								this = character:90104 #Charles the Bald
								current_year < 877
							}
							AND = {
								OR = {
									has_game_rule = all_karling_decline_rule
									has_game_rule = events_karling_decline_rule
								}
								this = character:90123 #Louis the Stammerer
								current_year < 879
							}
							AND = {
								OR = {
									has_game_rule = all_karling_decline_rule
									has_game_rule = events_karling_decline_rule
								}
								this = character:42018 #Karlmann of Bavaria
								current_year < 880
							}
							AND = {
								OR = {
									has_game_rule = all_karling_decline_rule
									has_game_rule = events_karling_decline_rule
								}
								this = character:42019 #Ludwig the Younger
								current_year < 882
							}
							AND = {
								OR = {
									has_game_rule = all_karling_decline_rule
									has_game_rule = events_karling_decline_rule
								}
								this = character:90105 #Charles the Fat
								current_year < 888
							}
							# we only want certain characters if the "events only" game rule is set
							AND = {
								has_game_rule = events_karling_decline_rule
								NOR = {
									this = character:144998 #Lothair II
									this = character:90107 #Ludwig the German
									this = character:90104 #Charles the Bald
									this = character:90123 #Louis the Stammerer
									this = character:42018 #Karlmann of Bavaria
									this = character:42019 #Ludwig the Younger
									this = character:90105 #Charles the Fat
								}
							}
							# don't worry about Karlings who can't inherit
							has_trait = bastard
							has_trait = devoted
							has_trait = order_member
							# don't worry about men in matrilinear marriages to non-Karlings
							AND = {
								is_male = yes
								is_married = yes
								matrilinear_marriage = yes
								primary_spouse = { has_dynasty = yes }
								NOT = { dynasty = primary_spouse.dynasty }
							}
							# don't worry about women in patrilinear marriages to non-Karlings
							AND = {
								is_female = yes
								is_married = yes
								patrilinear_marriage = yes
								primary_spouse = { has_dynasty = yes }
								NOT = { dynasty = primary_spouse.dynasty }
							}
							# don't hand all this land to France/Aquitaine
							title:k_france.holder ?= {
								any_heir_title = {
									OR = {
										this = title:k_east_francia
										this = title:k_bavaria
										this = title:k_italy
										this = title:k_lotharingia
									}
									OR = {
										holder = scope:potential_death
										place_in_line_of_succession = { target = scope:potential_death value < 1 }
									}		
									place_in_line_of_succession = { target = title:k_france.holder value < 2 }
								}
							}
							title:k_aquitaine.holder ?= {
								any_heir_title = {
									OR = {
										this = title:k_east_francia
										this = title:k_bavaria
										this = title:k_italy
										this = title:k_lotharingia
									}
									OR = {
										holder = scope:potential_death
										place_in_line_of_succession = { target = scope:potential_death value < 1 }
									}
									place_in_line_of_succession = { target = title:k_aquitaine.holder value < 2 }
								}
							}
						}
					}
					random_list = {
						1 = {
							trigger = {
								NOT = { has_trait = infirm }
							}
							death = { death_reason = death_horse_riding_accident }
						}
						1 = {
							death = { death_reason = death_fell_bedchamber }
						}
						1 = {
							death = { death_reason = death_drowned }
						}
						2 = {
							death = { death_reason = death_accident }
						}
						3 = {
							modifier = {
								add = 10
								has_trait = ill
							}
							trigger = {
								OR = {
									has_trait = ill
									has_any_debilitating_illness = no
								}
							}
							if = {
								limit = {
									NOT = { has_trait = ill }
								}
								add_trait = ill
							}
							death = { death_reason = death_ill }
						}
						5 = {
							trigger = { stress >= high_stress }
							death = { death_reason = death_stress }
						}
						1 = {
							modifier = {
								add = 2
								is_ruler = yes
							}
						}
					}
				}
			}
		}
	}
}

# Treaty of Meerssen
histmod.0301 = {
	type = character_event
	hidden = yes
	
	trigger = {
		OR = {
			has_game_rule = all_karling_decline_rule
			has_game_rule = events_karling_decline_rule
		}
		primary_heir = {
			is_ai = yes
			# Louis the Younger is inheriting and both West/East Francia are ruled by Karlings
			trigger_if = {
				limit = { has_title = title:k_italy }
				NOR = {
					has_title = title:k_france
					has_title = title:k_east_francia
				}
				title:k_france.holder = {
					is_ai = yes
					NOT = { has_title = title:k_east_francia }
					dynasty = title:k_italy.holder.dynasty
				}
				title:k_east_francia.holder = {
					is_ai = yes
					dynasty = title:k_italy.holder.dynasty
				}
			}
			# Ludwig the German is inheriting and West Francia is ruled by a Karling
			trigger_else_if = {
				limit = { has_title = title:k_east_francia }
				NOT = { has_title = title:k_france }
				title:k_france.holder = {
					is_ai = yes
					dynasty = title:k_east_francia.holder.dynasty
				}
			}
			trigger_else = {
				always = no
			}
		}
	}
	
	immediate = {
		save_scope_as = lothair
		primary_heir = { save_scope_as = lothair_heir }
		title:k_france.holder = { save_scope_as = charles_the_bald }
		title:k_east_francia.holder = { save_scope_as = ludwig_the_german }
		
		# see if hughes gets any titles
		if = {
			limit = {
				character:90102 = {
					is_alive = yes
					is_child_of = root
					NOR = {
						has_trait = eunuch
						has_trait = devoted
						has_trait = order_member
						has_trait = disinherited
						has_trait = gallivanter
						this = scope:lothair_heir
					}
					trigger_if = {
						limit = { is_ruler = yes }
						top_liege = root
						highest_held_title_tier = tier_county
					}
				}
			}
			character:90102 = { save_scope_as = hughes }
			
			if = {
				limit = {
					has_title = title:k_italy
					title:k_italy = {
						any_in_de_jure_hierarchy = {
							tier = tier_county
							holder = root
						}
					}
				}
				save_scope_value_as = { name = hughes_gets_italy value = yes }
				title:k_italy = { add_to_list = hugh_titles }
				title:d_provence = { add_to_list = hugh_titles }
				title:d_savoie = { add_to_list = french_titles }
				title:d_dauphine = { add_to_list = french_titles }
				title:d_romagna = { add_to_list = hugh_titles }
				title:d_ancona = { add_to_list = hugh_titles }
				title:d_spoleto = { add_to_list = hugh_titles }
				title:k_italy = {
					every_in_de_jure_hierarchy = {
						limit = { holder ?= root }
						add_to_list = hugh_titles
					}
				}
			}
			else_if = {
				limit = {
					character:90102 = {
						OR = {
							is_ruler = no
							NOT = {
								any_held_title = {
									tier = tier_county
									duchy = {
										OR = {
											this = title:d_brabant
											this = title:d_luxembourg
											this = title:d_bar
											this = title:d_upper_burgundy
											this = title:d_dauphine
											kingdom = title:k_france
											kingdom = title:k_aquitaine
										}
									}
								}
							}
						}
					}
				}
				save_scope_value_as = { name = hughes_gets_alsace value = yes }
				title:d_alsace = { add_to_list = hugh_titles }
				title:c_strassburg = { add_to_list = hugh_titles }
				title:c_colmar = { add_to_list = hugh_titles }
			}
			
			if = {
				limit = { list_size:hugh_titles > 0 }
				create_title_and_vassal_change = {
					type = granted
					save_scope_as = change
					add_claim_on_loss = no
				}
				every_in_list = {
					list = hugh_titles
					every_in_de_jure_hierarchy = {
						limit = {
							OR = {
								tier = tier_kingdom
								tier = tier_duchy
								tier = tier_county
							}
							holder ?= {
								OR = {
									this = root
									target_is_liege_or_above = root
								}
							}
						}
						if = {
							limit = {
								OR = {
									holder = root
									this = title:c_colmar
									this = title:c_strassburg
								}
								trigger_if = {
									limit = { exists = holder }
									holder = { is_ai = yes }
								}
							}
							add_to_temporary_list = hugh_titles_taken
						}
						else_if = {
							holder = { add_to_temporary_list = hugh_vassals_taken }
						}
					}
				}
				
				every_in_list = {
					list = hugh_titles_taken
					change_title_holder = {
						holder = scope:hughes
						change = scope:change
						take_baronies = yes
					}
				}
				every_in_list = {
					list = hugh_vassals_taken
					if = {
						limit = { highest_held_title_tier < scope:hughes.highest_held_title_tier }
						change_liege = {
							liege = scope:hughes
							change = scope:change
						}
					}
					add_truce_both_ways = {
						character = scope:hughes
						days = 1825
						override = yes
						name = TREATY_OF_MEERSSEN_TRUCE_NAME
					}
				}
				resolve_title_and_vassal_change = scope:change
			}
			
			every_held_title = {
				limit = { tier >= tier_county }
				scope:hughes = { add_pressed_claim = prev }
			}
		}
			
		# try to end wars between the participants
		scope:charles_the_bald = {
			every_character_war = {
				limit = {
					primary_attacker = scope:charles_the_bald
					primary_defender = {
						OR = {
							this = root
							this = scope:ludwig_the_german
						}
					}
				}
				end_war = invalidated
			}
			every_character_war = {
				limit = {
					NOT = { primary_attacker = scope:charles_the_bald }
					any_war_defender = {
						OR = {
							this = root
							this = scope:ludwig_the_german
						}
					}
				}
				remove_participant = scope:charles_the_bald
			}
		}
		scope:ludwig_the_german = {
			every_character_war = {
				limit = {
					primary_attacker = scope:ludwig_the_german
					primary_defender = {
						OR = {
							this = root
							this = scope:charles_the_bald
						}
					}
				}
				end_war = invalidated
			}
			every_character_war = {
				limit = {
					NOT = { primary_attacker = scope:ludwig_the_german }
					any_war_defender = {
						OR = {
							this = root
							this = scope:charles_the_bald
						}
					}
				}
				remove_participant = scope:ludwig_the_german
			}
		}
	
		# king of france receives western territories
		title:d_brabant = { add_to_list = french_titles }
		title:d_luxembourg = { add_to_list = french_titles }
		title:d_bar = { add_to_list = french_titles }
		title:d_upper_burgundy = { add_to_list = french_titles }
		title:d_dauphine = { add_to_list = french_titles }
		title:k_france = { add_to_list = french_titles }
		if = {
			limit = { has_game_rule = no_aquitaine_subsumed }
			title:k_aquitaine = { add_to_list = french_titles }
		}
		
		create_title_and_vassal_change = {
			type = usurped
			save_scope_as = change
			add_claim_on_loss = yes
		}
		every_in_list = {
			list = french_titles
			every_in_de_jure_hierarchy = {
				limit = {
					OR = {
						tier = tier_duchy
						tier = tier_county
					}
					holder ?= {
						OR = {
							this = root
							liege ?= root
							AND = {
								this = { has_title = title:k_italy }
								this = character:90102
							}
						}
					}
				}
				if = {
					limit = { holder.primary_title.tier >= scope:charles_the_bald.primary_title.tier }
					add_to_temporary_list = french_titles_taken
				}
				else = {
					holder = { add_to_temporary_list = french_vassals_taken }
				}
			}
		}
		every_in_list = {
			list = french_titles_taken
			change_title_holder = {
				holder = scope:charles_the_bald
				change = scope:change
				take_baronies = yes
			}
			scope:lothair_heir = {
				if = {
					limit = {
						NOR = {
							this = scope:charles_the_bald
							this = scope:ludwig_the_german
						}
					}
					add_pressed_claim = prev
				}
			}
		}
		every_in_list = {
			list = french_vassals_taken
			change_liege = {
				liege = scope:charles_the_bald
				change = scope:change
			}
			add_truce_both_ways = {
				character = scope:charles_the_bald
				days = 1825
				override = yes
				name = TREATY_OF_MEERSSEN_TRUCE_NAME
			}
		}
		resolve_title_and_vassal_change = scope:change
		
		# king of germany receives eastern territories
		title:d_julich = { add_to_list = german_titles }
		title:d_upper_lorraine = { add_to_list = german_titles }
		title:d_lower_lorraine = { add_to_list = german_titles }
		title:d_alsace = { add_to_list = german_titles }
		title:d_transjurania = { add_to_list = german_titles }
		title:k_east_francia = { add_to_list = german_titles }
		title:d_holland = { add_to_list = german_titles }
		title:d_gelre = { add_to_list = german_titles }
		title:d_utrecht = { add_to_list = german_titles }
		title:d_frisia = { add_to_list = german_titles }
		
		create_title_and_vassal_change = {
			type = usurped
			save_scope_as = change
			add_claim_on_loss = yes
		}
		every_in_list = {
			list = german_titles
			every_in_de_jure_hierarchy = {
				limit = {
					OR = {
						tier = tier_duchy
						tier = tier_county
					}
					holder ?= {
						OR = {
							this = root
							liege ?= root
							AND = {
								this = { has_title = title:k_italy }
								this = character:90102
							}
						}
					}
				}
				if = {
					limit = { holder.primary_title.tier >= scope:ludwig_the_german.primary_title.tier }
					add_to_temporary_list = german_titles_taken
				}
				else = {
					holder = { add_to_temporary_list = german_vassals_taken }
				}
			}
		}
		every_in_list = {
			list = german_titles_taken
			change_title_holder = {
				holder = scope:ludwig_the_german
				change = scope:change
				take_baronies = yes
			}
			scope:lothair_heir = {
				if = {
					limit = {
						NOR = {
							this = scope:charles_the_bald
							this = scope:ludwig_the_german
						}
					}
					add_pressed_claim = prev
				}
			}
		}
		every_in_list = {
			list = german_vassals_taken
			change_liege = {
				liege = scope:ludwig_the_german
				change = scope:change
			}
			add_truce_both_ways = {
				character = scope:ludwig_the_german
				days = 1825
				override = yes
				name = TREATY_OF_MEERSSEN_TRUCE_NAME
			}
		}
		resolve_title_and_vassal_change = scope:change
		
		title:k_lotharingia = {
			every_in_de_jure_hierarchy = {
				limit = { tier = tier_duchy }
				if = {
					limit = { is_in_list = french_titles }
					set_de_jure_liege_title = title:k_france
				}
				else = {
					set_de_jure_liege_title = title:k_east_francia
				}
			}
			if = {
				limit = { holder ?= root }
				root = { destroy_title = prev }
			}
		}
		
		# destroy any kingdom-tier titles
		if = {
			limit = {
				scope:lothair_heir = {
					OR = {
						this = scope:ludwig_the_german
						this = scope:charles_the_bald
						has_title = title:k_italy
					}
				}
			}
			every_held_title = {
				limit = { tier = tier_kingdom }
				root = { destroy_title = prev }
			}
		}
	
		# brief truce between West Francia and East Francia
		scope:charles_the_bald = {
			add_truce_both_ways = {
				character = scope:ludwig_the_german
				days = 1825
				override = yes
				name = TREATY_OF_MEERSSEN_TRUCE_NAME
			}
		}
	
		# inform players
		every_player = {
			limit = {
				OR = {
					in_diplomatic_range  = scope:ludwig_the_german
					in_diplomatic_range = scope:charles_the_bald
				}
			}
			trigger_event = { id = histmod.0302 days = 1 }
		}
	}
}

histmod.0302 = {
	type = character_event
	title = histmod.0302.t
	desc = {
		desc = histmod.0302.desc.start
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:lothair_heir = {
						NOR = {
							this = scope:ludwig_the_german
							this = scope:charles_the_bald
						}
					}
				}
				desc = histmod.0302.desc.younger
			}
			desc = histmod.0302.desc.bald
		}
		desc = histmod.0302.desc.middle
		triggered_desc = {
			trigger = { exists = scope:hughes_gets_italy }
			desc = histmod.0302.desc.italy
		}
		triggered_desc = {
			trigger = { exists = scope:hughes_gets_alsace }
			desc = histmod.0302.desc.alsace
		}
		triggered_desc = {
			trigger = {
				scope:lothair_heir = {
					NOR = {
						this = scope:ludwig_the_german
						this = scope:charles_the_bald
					}
				}
			}
			desc = histmod.0302.desc.younger2
		}
		desc = histmod.0302.desc.end
	}
	theme = realm
	
	left_portrait = {
		character = scope:charles_the_bald
		animation = throne_room_conversation_3
	}
	right_portrait = {
		character = scope:ludwig_the_german
		animation = thinking
	}
	lower_right_portrait = scope:lothair
	lower_left_portrait = scope:hughes
	lower_center_portrait = {
		trigger = {
			scope:lothair_heir = {
				NOR = {
					this = scope:ludwig_the_german
					this = scope:charles_the_bald
				}
			}
		}
		character = scope:lothair_heir
	}
	override_background = { reference = throne_room }
	
	immediate = {
		scope:ludwig_the_german = { save_scope_as = bg_override_char }
	}
	
	option = {
		name = histmod.0302.a
	}
}

# Possible Karling overthrow if primary heir is a child
histmod.0310 = {
	type = character_event
	hidden = yes
	
	trigger = {
		is_ai = yes
		# is a Karling king of one of the core Francia kingdoms
		dynasty = { this = dynasty:25061 }
		is_directly_related_to_player = no
		OR = {
			primary_title = title:k_france
			primary_title = title:k_aquitaine
			primary_title = title:k_east_francia
			primary_title = title:k_italy
			primary_title = title:k_bavaria
			primary_title = title:k_burgundy
		}
		# can only happen once
		primary_title = {
			NOT = { has_variable = had_karling_overthrow }
		}
		# and the primary heir is a child Karling
		primary_heir = {
			is_ai = yes
			has_dynasty = yes
			dynasty = { this = dynasty:25061 }
			is_adult = no
			is_directly_related_to_player = no
		}
		# there is at least one powerful unaligned de jure vassal who will claim the throne
		any_vassal = {
			is_powerful_vassal = yes
			has_government = feudal_government
			is_physically_able_ai_adult = yes
			is_male = yes
			ai_boldness >= 0
			ai_greed >= 0
			OR = {
				highest_held_title_tier = tier_duchy
				AND = {
					highest_held_title_tier = tier_county
					domain_size >= 3
				}
			}
			capital_county.kingdom.holder = root
			NOR = {
				dynasty = root.dynasty
				has_relation_friend = root
				has_relation_friend = root.primary_heir
				is_allied_to = root
				is_allied_to = root.primary_heir
				any_close_family_member = { is_hostage_of = root }
				AND = {
					exists = root.faith.religious_head
					this = root.faith.religious_head
				}
			}
		}
		# and the number of unaligned vassals is greater then those aligned
		num_unaligned_vassals > num_aligned_vassals
	}
	
	immediate = {
		primary_title = {
			save_scope_as = claimed_title
			set_variable = { name = had_karling_overthrow value = yes }
		}
		primary_heir = { save_scope_as = previous_heir }
		save_scope_as = previous_king
		
		# save the claimant (preference for a de jure vassal in the primary kingdom)
		ordered_vassal = {
			limit = {
				is_powerful_vassal = yes
				has_government = feudal_government
				is_physically_able_ai_adult = yes
				is_male = yes
				ai_boldness >= 0
				ai_greed >= 0
				OR = {
					highest_held_title_tier = tier_duchy
					AND = {
						highest_held_title_tier = tier_county
						domain_size >= 3
					}
				}
				capital_county.kingdom = root.primary_title
				NOR = {
					dynasty = root.dynasty
					has_relation_friend = root
					has_relation_friend = root.primary_heir
					is_allied_to = root
					is_allied_to = root.primary_heir
					any_close_family_member = { is_hostage_of = root }
					AND = {
						exists = root.faith.religious_head
						this = root.faith.religious_head
					}
				}
			}
			alternative_limit = {
				is_powerful_vassal = yes
				has_government = feudal_government
				is_physically_able_ai_adult = yes
				is_male = yes
				ai_boldness >= 0
				ai_greed >= 0
				OR = {
					highest_held_title_tier = tier_duchy
					AND = {
						highest_held_title_tier = tier_county
						domain_size >= 3
					}
				}
				capital_county.kingdom.holder = root
				NOR = {
					dynasty = root.dynasty
					has_relation_friend = root
					has_relation_friend = root.primary_heir
					is_allied_to = root
					is_allied_to = root.primary_heir
					any_close_family_member = { is_hostage_of = root }
					AND = {
						exists = root.faith.religious_head
						this = root.faith.religious_head
					}
				}
			}
			order_by = current_military_strength
			save_scope_as = new_claimant
			primary_title = { save_scope_as = new_claimant_title }
		}
		# and the other objecting vassals who will be their allies
		every_vassal = {
			limit = {
				is_physically_able_ai_adult = yes
				highest_held_title_tier >= tier_county
				capital_county.kingdom.holder = root
				opinion = { target = scope:new_claimant value >= 0 }
				NOR = {
					this = scope:new_claimant
					has_relation_rival = scope:new_claimant
					is_at_war_with = scope:new_claimant
					dynasty = root.dynasty
					has_relation_friend = root
					has_relation_friend = root.primary_heir
					is_allied_to = root
					is_allied_to = root.primary_heir
					any_close_family_member = { is_hostage_of = root }
					AND = {
						exists = root.faith.religious_head
						this = root.faith.religious_head
					}
				}
			}
			add_to_list = new_claimant_allies
		}
		
		# now fire the event to the new heir to take place right after his ascension
		primary_heir = {
			trigger_event = { id = histmod.0311 days = 1 }
		}
	}
}

histmod.0311 = {
	type = character_event
	hidden = yes
	
	immediate = {
		# first give all kingdom-level titles to the claimant
		every_vassal = {
			limit = { NOT = { this = scope:new_claimant } }
			add_to_list = vassals_taken
		}
		add_to_list = vassals_taken
		create_title_and_vassal_change = {
			type = usurped
			save_scope_as = change
			add_claim_on_loss = yes
		}
		every_held_title = {
			limit = { tier = tier_kingdom }
			change_title_holder = {
				holder = scope:new_claimant
				change = scope:change
			}
		}
		every_in_list = {
			list = vassals_taken
			change_liege = {
				liege = scope:new_claimant
				change = scope:change
			}
		}
		resolve_title_and_vassal_change = scope:change
		
		# and ally any fellow objectors
		if = {
			limit = {
				list_size = { name = new_claimant_allies value > 0 }
			}
			every_in_list = {
				list = new_claimant_allies
				limit = {
					NOT = { is_allied_to = scope:new_claimant }
				}
				save_temporary_scope_as = new_ally
				create_alliance = {
					target = scope:new_claimant
					allied_through_owner = scope:new_ally
					allied_through_target = scope:new_claimant
				}
				progress_towards_friend_effect = {
					CHARACTER = scope:new_claimant
					OPINION = 50
					REASON = friend_after_subjugation
				}
				reverse_add_opinion = { target = scope:new_claimant modifier = working_together_hre_opinion }
				hidden_effect = {
					add_opinion = { target = scope:new_claimant modifier = working_together_hre_opinion }
					add_opinion = { target = scope:new_claimant modifier = event_negotiated_alliance_opinion }
					reverse_add_opinion = { target = scope:new_claimant modifier = event_negotiated_alliance_opinion }
				}
			}
		}
		
		# change titles to feudal elective if needed
		scope:new_claimant = {
			if = {
				limit = {
					NOR = {
						culture = { has_cultural_parameter = witenagemot_succession_enabled }
						culture = { has_cultural_parameter = scandinavian_elective_enabled }
						culture = { has_cultural_parameter = tribal_elective_enabled }
					}
				}
				every_held_title = {
					limit = {
						tier >= tier_duchy
						is_temporal_head_of_faith_trigger = no
						NOT = { has_title_law = feudal_elective_succession_law }
					}
					add_title_law = feudal_elective_succession_law 
				}
			}
		}
		
		every_player = {
			limit = { in_diplomatic_range = scope:new_claimant }
			trigger_event = histmod.0312
		}
	}
}
	
histmod.0312 = {
	type = character_event
	title = histmod.0312.t
	desc = histmod.0312.desc
	theme = realm
	
	left_portrait = {
		character = scope:new_claimant
		animation = X
	}
	right_portrait = {
		character = scope:previous_heir
		animation = X
	}
	lower_right_portrait = scope:previous_king
	override_background = { reference = throne_room }
	
	option = {
		name = histmod.0312.a
	}
}

### Conqueror events

# Rurik scripted to die in 879, if still alive
histmod.0350 = {
	type = character_event
	
	hidden = yes
	
	trigger = {
		is_ai = yes
		is_alive = yes
		is_playable_character = yes
		player_heir = { is_adult = yes }
	}
	
	on_trigger_fail = {
		if = {
			limit = {
				is_ai = yes
				is_alive = yes
				is_playable_character = yes
			}
			trigger_event = { id = histmod.0350 days = { 730 1095 } }
		}
	}
	
	immediate = {
		death = { death_reason = death_accident }
	}
}

# Stop being a conqueror
histmod.0400 = {
	type = character_event
	title = histmod.0400.t
	desc = {
		desc = histmod.0400.desc.start
		first_valid = {
			triggered_desc = {
				trigger = {
					has_character_flag = histmod_seeking_hre
					has_title = title:e_hre
				}
				desc = histmod.0400.desc.hre
			}
			desc = histmod.0400.desc.title
		}
		desc = histmod.0400.desc.end
	}
	theme = war
	override_background = { reference = courtyard }
	left_portrait = {
		character = root
		animation = celebrate_sword
	}
	
	trigger = {
	}

	immediate = {
		save_scope_as = root_scope
		every_owned_story = {
			limit = { story_type = histmod_conqueror_story_cycle }
			end_story = yes
		}
		add_character_flag = used_to_be_conqueror
	}

	option = {
		name = histmod.0400.a
		flavor = histmod_conqueror_end_flavor
		remove_character_modifier = ai_extreme_conqueror_modifier
		remove_character_modifier = ai_weak_conqueror_modifier
		remove_trait = conqueror
		ai_chance = {
			base = 100 
		}
	}
}

### Hauteville events

# Bounce event to start first wave
histmod.0500 = {
	hidden = yes
	scope = none
	
	trigger = {
		current_year >= 1035
		# make sure there's at least one county of norman culture remaining
		trigger_if = {
			limit = {
				NOT = { culture = culture:norman }
			}
			culture:norman = {
				any_culture_county = { count > 0 }
			}
		}
		# and there's a duchy available to attack, if we're going to be attacking right away
		trigger_if = {
			limit = { should_be_invading_sicily = yes }
			any_county_in_region = {
				region = custom_sicily
				save_temporary_scope_as = sicily_county
				NOR = {
					# not Malta, thank you
					this = title:c_malta
					# no conquerors
					holder = { has_trait = conqueror }
					# we don't want to attack a county in a duchy that includes a player's capital
					duchy = {
						any_de_jure_county = {
							holder = {
								is_ai = no
								OR = {
									capital_county.duchy = scope:sicily_county.duchy
									domain_size < 6
								}
							}
						}
					}
					# and we don't want to attack the realms of players that aren't very large
					holder = {
						any_liege_or_above = {
							is_ai = no
							sub_realm_size < 10
						}
					}
				}
				# for the historical arrival, let's target a duchy that can get us at least 3 counties and isn't terribly strong
				trigger_if = {
					limit = { current_year <= 1040 }
					holder.top_liege = {
						strength_with_relevant_allies_value <= 5000
						any_sub_realm_county = {
							duchy = scope:sicily_county.duchy
							count > 2
						}
					}
				}
				# for an arrival up to 10 years later, we loosen up the requirements a bit
				trigger_else_if = {
					limit = { current_year <= 1050 }
					holder.top_liege = {
						strength_with_relevant_allies_value <= 8000
						any_sub_realm_county = {
							duchy = scope:sicily_county.duchy
							count > 1
						}
					}
				}
				# after that, it's anything
				trigger_else = {
					always = yes
				}
			}
			# plus a place for us to land that isn't an island or owned by the same top liege
			any_county_in_region = {
				region = world_europe_south_italy
				NOR = {
					holder.top_liege = scope:sicily_county.holder.top_liege
					this = title:c_malta
					kingdom = title:k_sardinia
				}
			}
		}
	}
	
	on_trigger_fail = {
		# let's refire the event in 3 years time, so long as there's ANY Norman counties still existing
		# at the very latest, the adventurers will spawn in after 1048
		if = {
			limit = {
				culture:norman = {
					any_culture_county = { count > 0 }
				}
			}
			title:c_avranches = {
				trigger_event = { id = histmod.0500 years = 3 }
			}
		}
	}
	
	immediate = {
		# create the first wave
		create_first_wave_hautevilles = yes
		
		# trigger the second wave
		title:c_avranches = {
			trigger_event = { id = histmod.0510 years = 9 }
		}
		
		debug_log = "William spawned"
		debug_log_scopes = yes
		
		if = {
			limit = { should_be_invading_sicily = yes }
			scope:target_county.holder.top_liege = {
				trigger_event = histmod.0501
			}
		}
	}
}

# Holder of target county warned of Norman rebellion
histmod.0501 = {
	type = character_event
	title = histmod.0501.t
	desc = histmod.0501.desc
	theme = war
	
	left_portrait = {
		character = scope:william
		animation = celebrate_sword
	}
	override_background = { reference = burning_building }
	
	immediate = {
		scope:william = { add_character_flag = wear_armor }
		scope:target_county.duchy = {
			save_scope_as = target_area
			if = {
				limit = {
					OR = {
						NOT = { exists = holder }
						holder = root
						holder = { target_is_liege_or_above = root }
					}
				}
				add_to_list = target_titles
			}
			every_in_de_jure_hierarchy = {
				limit = {
					tier = tier_county
					holder ?= {
						OR = {
							this = root
							target_is_liege_or_above = root
						}
					}
				}
				add_to_list = target_titles
			}	
		}
	}
	
	option = {
		name = histmod.0501.a #Just give it to them
		trigger = {
			OR = {
				highest_held_title_tier > tier_duchy
				sub_realm_size > 6
			}
			any_held_title = {
				NOT = { is_in_list = target_titles }
			}
		}
		add_prestige = major_prestige_loss
		stress_impact = {
			generous = minor_stress_impact_loss
			forgiving = minor_stress_impact_loss
			greedy = major_stress_impact_gain
			arrogant = medium_stress_impact_gain
			ambitious = medium_stress_impact_gain
			stubborn = medium_stress_impact_gain
			vengeful = medium_stress_impact_gain
		}
		# first give William the adventurer trait
		hidden_effect = {
			scope:william = {
				if = {
					limit = {
						NOT = { has_trait = adventurer }
					}
					add_trait = adventurer
				}
				every_courtier = {
					limit = { NOT = { has_trait = adventurer_follower } }
					set_variable = { name = former_camp_leader value = scope:william }
					add_trait = adventurer_follower
				}
			}
		}
		# then transfer the titles
		create_title_and_vassal_change = {
			type = conquest
			save_scope_as = change
			add_claim_on_loss = yes
		}
		every_in_list = {
			list = target_titles
			change_title_holder = {
				holder = scope:william
				change = scope:change
				take_baronies = yes
			}
		}
		resolve_title_and_vassal_change = scope:change
		# now give William a boost to start off
		hidden_effect = {
			scope:william = {
				add_gold = 250
				add_prestige = 500
				every_held_county = { change_county_control = 50 }
			}
		}
		ai_chance = {
			base = 0
			modifier = {
				add = 20
				is_at_war = yes
			}
			modifier = {
				add = 100
				current_military_strength < 3000
			}
			ai_value_modifier = {
				ai_boldness = -0.50
			}
			min = 0
		}
	}
	option = {
		name = histmod.0501.b #We will drive them out!
		add_prestige = medium_prestige_gain
		ai_chance = {
			base = 100
			modifier = {
				add = -50
				current_military_strength < 3000
			}
			modifier = {
				add = 100
				current_military_strength > 6000
			}
			ai_value_modifier = {
				ai_boldness = 0.50
				ai_vengefulness = 0.50
			}
			min = 1
		}
		scope:william = {
			start_war = {
				casus_belli = ep3_laamp_adventurer_duchy_invasion
				target = root
				target_title = scope:target_county.duchy
			}
			hidden_effect = {
				save_scope_as = scandi_adventurer
				scope:target_county = { save_scope_as = adventurer_target }
				random_character_war = {
					limit = { using_cb = ep3_laamp_adventurer_duchy_invasion }
					save_scope_as = scandi_war
				}
				if = {
					limit = { scandi_levy_total > 5000 }
					spawn_army = {
						levies = {
							add = scandi_levy_total
							subtract = 500
							multiply = 0.5
							round = yes
						}
						war = scope:scandi_war
						inheritable = no
						location = scope:iron_arm_start
						name = norman_adventurer_rebel_event_troops
					}
					spawn_army = {
						levies = {
							add = scandi_levy_total
							subtract = 500
							multiply = 0.5
							round = yes
						}
						war = scope:scandi_war
						inheritable = no
						location = scope:iron_arm_start
						name = norman_adventurer_rebel_event_troops
					}
				}
				else_if = {
					limit = { scandi_levy_total > 1000 }
					spawn_army = {
						levies = {
							add = scandi_levy_total
							subtract = 1000
						}
						war = scope:scandi_war
						inheritable = no
						location = scope:iron_arm_start
						name = norman_adventurer_rebel_event_troops
					}
				}
				spawn_army = {
					levies = 1000
					men_at_arms = {
						type = light_footmen
						stacks = {
							value = scandi_maa_total
							multiply = 0.5
							round = yes
						}
					}
					men_at_arms = {
						type = light_horsemen
						stacks = {
							value = scandi_maa_total
							multiply = 0.5
							round = yes
						}
					}
					inheritable = no
					location = scope:iron_arm_start
					name = norman_adventurer_event_troops
				}			
			}
		}
	}
	
	after = {
		scope:william = { remove_character_flag = wear_armor }
	}
}

# Bounce event to start second wave
histmod.0510 = {
	hidden = yes
	scope = none
	
	immediate = {
		create_second_wave_hautevilles = yes
		
		title:c_avranches = {
			trigger_event = { id = histmod.0512 years = 5 }
		}
		
		scope:robert.dynasty = {
			every_dynasty_member = {
				limit = {
					is_ruler = yes
					OR = {
						has_government = landless_adventurer_government
						capital_county.kingdom ?= title:k_sicily
					}
				}
				trigger_event = histmod.0511
			}
		}
	}
}

# Informing relatives of Robert's arrival
histmod.0511 = {
	type = character_event
	title = histmod.0511.t
	desc = histmod.0511.desc
	theme = family
	
	left_portrait = scope:robert
	override_background = { reference = docks }
	
	option = {
		name = histmod.0511.a
		if = {
			limit = { is_ai = yes }
			set_relation_friend = scope:robert
		}
		else = {
			add_opinion = { target = scope:robert modifier = respect_opinion opinion = 30 }
			reverse_add_opinion = { target = scope:robert modifier = respect_opinion opinion = 30 }
		}
	}
}

# Serlo II comes to join the gang
histmod.0512 = {
	hidden = yes
	scope = none
	
	trigger = {
		scope:serlo_son ?= {
			is_ai = yes
			is_alive = yes
			is_ruler = no
			is_councillor = no
			primary_spouse ?= {
				is_ruler = no
				NOT = {
					any_heir_title = {
						place_in_line_of_succession = {
							target = scope:potential
							value < 2
						}
					}
				}
			}
		}
	}
	
	immediate = {
		if = {
			limit = {
				scope:robert = {
					is_alive = yes
					is_ruler = yes
				}
			}
			scope:robert = {
				add_courtier = scope:serlo_son
			}
		}
		else = {
			scope:serlo_son.dynasty = {
				random_dynasty_member = {
					limit = { is_ruler = yes }
					weight = {
						base = 1
						modifier = {
							add = 10
							has_government = landless_adventurer_government
						}
						modifier = {
							add = 5
							is_ai = no
						}
						modifier = {
							add = {
								value = primary_title.tier
								multiply = 3
							}
							is_landed = yes
						}
					}
					add_courtier = scope:serlo_son
				}
			}
		}
	}
}

### Conversion events

# Magyars convert to Catholicism
histmod.0600 = {
	type = character_event
	title = histmod.0600.t
	desc = histmod.0600.desc
	theme = faith
	
	left_portrait = {
		character = scope:scoped_ruler
		animation = prayer
	}
	
	immediate = {
		scope:scoped_ruler = { save_scope_as = bg_override_char }
	}
	
	#Catholics are happy about this unexpected turn of events!
	option = {
		name = histmod.0600.a
		trigger = { faith = faith:catholic }
		add_stress = minor_stress_loss
	}
	
	#Pagans are horrified
	option = {
		name = histmod.0600.b
		trigger = {
			religion = { is_in_family = rf_pagan }
		}
	}
	
	#Magyar_pagans vassals may choose to also convert
	option = {
		name = histmod.0600.c
		trigger = {
			top_liege = scope:scoped_ruler
			faith = faith:magyar_pagan
		}
		set_character_faith_with_conversion = faith:catholic
	}
	
	#Christians are at least somewhat pleased
	option = {
		name = histmod.0600.d
		trigger = {
			religion = religion:christianity_religion
			NOT = { faith = faith:catholic }
		}
	}
	
	#Everyone else
	option = {
		name = histmod.0600.e
		trigger = {
			NOR = {
				religion = religion:christianity_religion
				religion = { is_in_family = rf_pagan }
			}
		}
	}
}

# Russia convert to Orthodox
histmod.0601 = {
	type = character_event
	title = histmod.0601.t
	desc = histmod.0601.desc
	theme = faith
	
	left_portrait = {
		character = scope:scoped_ruler
		animation = prayer
	}
	
	immediate = {
		scope:scoped_ruler = { save_scope_as = bg_override_char }
	}
	
	#Orthodox are happy about this unexpected turn of events!
	option = {
		name = histmod.0600.a
		trigger = { faith = faith:orthodox }
		add_stress = minor_stress_loss
	}
	
	#Pagans are horrified
	option = {
		name = histmod.0600.b
		trigger = {
			religion = { is_in_family = rf_pagan }
		}
	}
	
	#Slavic pagan vassals may choose to also convert
	option = {
		name = histmod.0600.c
		trigger = {
			top_liege = scope:scoped_ruler
			faith = faith:slavic_pagan
		}
		set_character_faith_with_conversion = faith:orthodox
	}
	
	#Christians are at least somewhat pleased
	option = {
		name = histmod.0600.d
		trigger = {
			religion = religion:christianity_religion
			NOT = { faith = faith:orthodox }
		}
	}
	
	#Everyone else
	option = {
		name = histmod.0600.e
		trigger = {
			NOR = {
				religion = religion:christianity_religion
				religion = { is_in_family = rf_pagan }
			}
		}
	}
}

