namespace = fp1_jomsvikings

##################################################
# #Standard Events
# 0001 - 0010	Jomsvikings offer a Retainer
# 0011 - 0020	Jomsvikings terrorise their neighbours
# 0021 - 0030	Jomsvikings send Event Troops
# 0031 - 0040	SPARE
# 0041 - 0050	Jomsvikings challenge a Dishonourable Norse Pagan Ruler to Holmgang
#
# #Special Events
# 1001 - 1010	Jomsvikings Destroyed
# 1011 - 1020	Jomsvikings Formed Autonomously
##################################################





##################################################
# #Generic Scripted Triggers

scripted_trigger fp1_pious_enough_for_jomsvikings_reward_trigger = {
	# You must share their faith.
	faith = global_var:jomsvikings_title.holder.faith
	# Piety level minimum.
	piety_level >= high_piety_level
	# Sin maximum.
	num_sinful_traits <= 1
	# Piety to spend.
	piety >= major_piety_value
	# The Grandmaster needs to not hate you also.
	global_var:jomsvikings_title.holder = {
		has_any_bad_relationship_with_root_trigger = no 
		opinion = {
			target = root
			value >= low_negative_opinion
		}
	}
}

scripted_trigger fp1_jomsvikings_exist_in_range_trigger = {
	# Do they exist at all?
	exists = global_var:jomsvikings_title
	# Are they within diplomatic range?
	exists = global_var:jomsvikings_title.holder
	in_diplomatic_range = global_var:jomsvikings_title.holder
}

##################################################
# #Standard Events

##################################################
# Jomsvikings offer a Retainer
# by Ewan Cowhig Croft
# 0001 - 0010
##################################################

#	You are rewarded for your piety with a loyal warrior.
fp1_jomsvikings.0001 = {
	type = character_event
	title = fp1_jomsvikings.0001.t
	desc = {
		desc = fp1_jomsvikings.0001.desc.intro
		# Describe scope:retainer_a.
		first_valid = {
			triggered_desc = {
				trigger = { scope:retainer_a_descriptor = flag:zealous }
				desc = fp1_jomsvikings.0001.desc.zealous
			}
			triggered_desc = {
				trigger = { scope:retainer_a_descriptor = flag:wrathful }
				desc = fp1_jomsvikings.0001.desc.wrathful
			}
			triggered_desc = {
				trigger = { scope:retainer_a_descriptor = flag:impatient }
				desc = fp1_jomsvikings.0001.desc.impatient
			}
			triggered_desc = {
				trigger = { scope:retainer_a_descriptor = flag:arrogant }
				desc = fp1_jomsvikings.0001.desc.arrogant
			}
			triggered_desc = {
				trigger = { scope:retainer_a_descriptor = flag:brave }
				desc = fp1_jomsvikings.0001.desc.brave
			}
			triggered_desc = {
				trigger = { scope:retainer_a_descriptor = flag:ambitious }
				desc = fp1_jomsvikings.0001.desc.ambitious
			}
			triggered_desc = {
				trigger = { scope:retainer_a_descriptor = flag:diligent }
				desc = fp1_jomsvikings.0001.desc.diligent
			}
			triggered_desc = {
				trigger = { scope:retainer_a_descriptor = flag:gregarious }
				desc = fp1_jomsvikings.0001.desc.gregarious
			}
			triggered_desc = {
				trigger = { scope:retainer_a_descriptor = flag:stubborn }
				desc = fp1_jomsvikings.0001.desc.stubborn
			}
			triggered_desc = {
				trigger = { scope:retainer_a_descriptor = flag:fickle }
				desc = fp1_jomsvikings.0001.desc.fickle
			}
			triggered_desc = {
				trigger = { scope:retainer_a_descriptor = flag:vengeful }
				desc = fp1_jomsvikings.0001.desc.vengeful
			}
			triggered_desc = {
				trigger = { scope:retainer_a_descriptor = flag:viking }
				desc = fp1_jomsvikings.0001.desc.viking
			}
		}
		desc = fp1_jomsvikings.0001.desc.connector
		# Describe scope:retainer_b.
		first_valid = {
			triggered_desc = {
				trigger = { scope:retainer_b_descriptor = flag:zealous }
				desc = fp1_jomsvikings.0001.desc.zealous
			}
			triggered_desc = {
				trigger = { scope:retainer_b_descriptor = flag:wrathful }
				desc = fp1_jomsvikings.0001.desc.wrathful
			}
			triggered_desc = {
				trigger = { scope:retainer_b_descriptor = flag:impatient }
				desc = fp1_jomsvikings.0001.desc.impatient
			}
			triggered_desc = {
				trigger = { scope:retainer_b_descriptor = flag:arrogant }
				desc = fp1_jomsvikings.0001.desc.arrogant
			}
			triggered_desc = {
				trigger = { scope:retainer_b_descriptor = flag:brave }
				desc = fp1_jomsvikings.0001.desc.brave
			}
			triggered_desc = {
				trigger = { scope:retainer_b_descriptor = flag:ambitious }
				desc = fp1_jomsvikings.0001.desc.ambitious
			}
			triggered_desc = {
				trigger = { scope:retainer_b_descriptor = flag:diligent }
				desc = fp1_jomsvikings.0001.desc.diligent
			}
			triggered_desc = {
				trigger = { scope:retainer_b_descriptor = flag:gregarious }
				desc = fp1_jomsvikings.0001.desc.gregarious
			}
			triggered_desc = {
				trigger = { scope:retainer_b_descriptor = flag:stubborn }
				desc = fp1_jomsvikings.0001.desc.stubborn
			}
			triggered_desc = {
				trigger = { scope:retainer_b_descriptor = flag:fickle }
				desc = fp1_jomsvikings.0001.desc.fickle
			}
			triggered_desc = {
				trigger = { scope:retainer_b_descriptor = flag:vengeful }
				desc = fp1_jomsvikings.0001.desc.vengeful
			}
			triggered_desc = {
				trigger = { scope:retainer_b_descriptor = flag:viking }
				desc = fp1_jomsvikings.0001.desc.viking
			}
		}
		desc = fp1_jomsvikings.0001.desc.outro
	}
	theme = faith
	left_portrait = {
		character = scope:retainer_a
		animation = personality_zealous
	}
	right_portrait = {
		character = scope:retainer_b
		animation = admiration
	}
	lower_center_portrait = scope:leader
	override_background = { reference = throne_room }

	trigger = {
		# DLC check.
		has_fp1_dlc_trigger = yes
		# The Jomsvikings must exist; check this before the usual standard checks for best efficiency.
		fp1_jomsvikings_exist_in_range_trigger = yes
		# Standard checks.
		is_available_at_peace_adult = yes
		NOT = { has_character_flag = had_event_fp1_jomsvikings_0001 }
		# You must fulfill their generic piety requirements.
		fp1_pious_enough_for_jomsvikings_reward_trigger = yes
	}

	weight_multiplier = {
		base = 1
		
		# Weight up for excess levels of piety & high levels of prestige.
		fp1_jomsvikings_approve_of_extra_piety_and_prestige_modifier = yes
	}

	immediate = {
		add_character_flag = {
			flag = had_event_fp1_jomsvikings_0001
			days = 1825
		}
		# Grab the Grandmaster.
		global_var:jomsvikings_title.holder = { save_scope_as = leader }
		# Generate a brace of possible retainers.
		## First a martial-focused character.
		create_character = {
			location = root.capital_province
			template = fp1_jomsviking_retainer_martial
			save_scope_as = retainer_a
		}
		## Then a prowess-focused character.
		create_character = {
			location = root.capital_province
			template = fp1_jomsviking_retainer_prowess
			save_scope_as = retainer_b
		}
		# Generate a descriptive string for each retainer.
		## Twelve options of 8.3% each.
		## First, retainer_a.
		scope:retainer_a = {
			random_list = {
				# Zealous
				100 = {
					trigger = { has_trait = zealous }
					save_scope_value_as = {
						name = retainer_a_descriptor
						value = flag:zealous
					}
				}
				# Wrathful
				100 = {
					trigger = { has_trait = wrathful }
					save_scope_value_as = {
						name = retainer_a_descriptor
						value = flag:wrathful
					}
				}
				# Impatient
				100 = {
					trigger = { has_trait = impatient }
					save_scope_value_as = {
						name = retainer_a_descriptor
						value = flag:impatient
					}
				}
				# Arrogant
				100 = {
					trigger = { has_trait = arrogant }
					save_scope_value_as = {
						name = retainer_a_descriptor
						value = flag:arrogant
					}
				}
				# Brave
				100 = {
					trigger = { has_trait = brave }
					save_scope_value_as = {
						name = retainer_a_descriptor
						value = flag:brave
					}
				}
				# Ambitious
				100 = {
					trigger = { has_trait = ambitious }
					save_scope_value_as = {
						name = retainer_a_descriptor
						value = flag:ambitious
					}
				}
				# Diligent
				100 = {
					trigger = { has_trait = diligent }
					save_scope_value_as = {
						name = retainer_a_descriptor
						value = flag:diligent
					}
				}
				# Gregarious
				100 = {
					trigger = { has_trait = gregarious }
					save_scope_value_as = {
						name = retainer_a_descriptor
						value = flag:gregarious
					}
				}
				# Stubborn
				100 = {
					trigger = { has_trait = stubborn }
					save_scope_value_as = {
						name = retainer_a_descriptor
						value = flag:stubborn
					}
				}
				# Fickle
				100 = {
					trigger = { has_trait = fickle }
					save_scope_value_as = {
						name = retainer_a_descriptor
						value = flag:fickle
					}
				}
				# Vengeful
				100 = {
					trigger = { has_trait = vengeful }
					save_scope_value_as = {
						name = retainer_a_descriptor
						value = flag:vengeful
					}
				}
				# Viking
				100 = {
					trigger = { has_trait = viking }
					save_scope_value_as = {
						name = retainer_a_descriptor
						value = flag:viking
					}
				}
			}
		}
		## Then, retainer_b.
		scope:retainer_b = {
			random_list = {
				# Zealous
				100 = {
					trigger = {
						has_trait = zealous
						NOT = { scope:retainer_a_descriptor = flag:zealous }
					}
					save_scope_value_as = {
						name = retainer_b_descriptor
						value = flag:zealous
					}
				}
				# Wrathful
				100 = {
					trigger = {
						has_trait = wrathful
						NOT = { scope:retainer_a_descriptor = flag:wrathful }
					}
					save_scope_value_as = {
						name = retainer_b_descriptor
						value = flag:wrathful
					}
				}
				# Impatient
				100 = {
					trigger = {
						has_trait = impatient
						NOT = { scope:retainer_a_descriptor = flag:impatient }
					}
					save_scope_value_as = {
						name = retainer_b_descriptor
						value = flag:impatient
					}
				}
				# Arrogant
				100 = {
					trigger = {
						has_trait = arrogant
						NOT = { scope:retainer_a_descriptor = flag:arrogant }
					}
					save_scope_value_as = {
						name = retainer_b_descriptor
						value = flag:arrogant
					}
				}
				# Brave
				100 = {
					trigger = {
						has_trait = brave
						NOT = { scope:retainer_a_descriptor = flag:brave }
					}
					save_scope_value_as = {
						name = retainer_b_descriptor
						value = flag:brave
					}
				}
				# Ambitious
				100 = {
					trigger = {
						has_trait = ambitious
						NOT = { scope:retainer_a_descriptor = flag:ambitious }
					}
					save_scope_value_as = {
						name = retainer_b_descriptor
						value = flag:ambitious
					}
				}
				# Diligent
				100 = {
					trigger = {
						has_trait = diligent
						NOT = { scope:retainer_a_descriptor = flag:diligent }
					}
					save_scope_value_as = {
						name = retainer_b_descriptor
						value = flag:diligent
					}
				}
				# Gregarious
				100 = {
					trigger = {
						has_trait = gregarious
						NOT = { scope:retainer_a_descriptor = flag:gregarious }
					}
					save_scope_value_as = {
						name = retainer_b_descriptor
						value = flag:gregarious
					}
				}
				# Stubborn
				100 = {
					trigger = {
						has_trait = stubborn
						NOT = { scope:retainer_a_descriptor = flag:stubborn }
					}
					save_scope_value_as = {
						name = retainer_b_descriptor
						value = flag:stubborn
					}
				}
				# Fickle
				100 = {
					trigger = {
						has_trait = fickle
						NOT = { scope:retainer_a_descriptor = flag:fickle }
					}
					save_scope_value_as = {
						name = retainer_b_descriptor
						value = flag:fickle
					}
				}
				# Vengeful
				100 = {
					trigger = {
						has_trait = vengeful
						NOT = { scope:retainer_a_descriptor = flag:vengeful }
					}
					save_scope_value_as = {
						name = retainer_b_descriptor
						value = flag:vengeful
					}
				}
				# Viking
				100 = {
					trigger = {
						has_trait = viking
						NOT = { scope:retainer_a_descriptor = flag:viking }
					}
					save_scope_value_as = {
						name = retainer_b_descriptor
						value = flag:viking
					}
				}
			}
		}
	}

	# Scope:retainer_a looks formidable!
	option = {
		name = fp1_jomsvikings.0001.a

		# Spend the piety cost.
		add_piety = major_piety_loss
		# Gain the courtier.
		add_courtier = scope:retainer_a
		# Send the other lad packing back to the Jomsvikings.
		hidden_effect = {
			scope:leader = { add_courtier = scope:retainer_b }
		}

		stress_impact = {
			trusting = minor_stress_impact_loss
			zealous = medium_stress_impact_loss
			paranoid = medium_stress_impact_gain
			cynical = major_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_zeal = 0.75
				ai_sociability = 0.25
			}
			modifier = {	# Weight up for stress.
				add = 10
				has_trait = trusting
			}
			modifier = {	# Weight up for stress.
				add = 20
				has_trait = zealous
			}
			modifier = {	# Weight down for stress.
				add = -20
				has_trait = paranoid
			}
			modifier = {	# Weight down for stress.
				add = -30
				has_trait = cynical
			}
		}
	}
	
	# Scope:retainer_b shall serve me well.
	option = {
		name = fp1_jomsvikings.0001.b

		# Spend the piety cost.
		add_piety = major_piety_loss
		# Gain the courtier.
		add_courtier = scope:retainer_b
		# Send the other lad packing back to the Jomsvikings.
		hidden_effect = {
			scope:leader = { add_courtier = scope:retainer_a }
		}

		stress_impact = {
			trusting = minor_stress_impact_loss
			zealous = medium_stress_impact_loss
			paranoid = medium_stress_impact_gain
			cynical = major_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_zeal = 0.75
				ai_sociability = 0.25
			}
			modifier = {	# Weight up for stress.
				add = 10
				has_trait = trusting
			}
			modifier = {	# Weight up for stress.
				add = 20
				has_trait = zealous
			}
			modifier = {	# Weight down for stress.
				add = -20
				has_trait = paranoid
			}
			modifier = {	# Weight down for stress.
				add = -30
				has_trait = cynical
			}
		}
	}
	
	# I'm fine, thanks.
	option = {
		name = fp1_jomsvikings.0001.c

		# Gain a little consolation prestige.
		add_prestige = minor_prestige_gain
		# Send both lads packing.
		scope:leader = {
			hidden_effect = {
				add_courtier = scope:retainer_a
				add_courtier = scope:retainer_b
			}
		}
		# The Grandmaster considers this an insult.
		scope:leader = {
			add_opinion = {
				target = root
				modifier = insult_opinion
				opinion = -30
			}
			hidden_effect = {
				if = {
					limit = {
						can_set_relation_potential_rival_trigger = { CHARACTER = root }
					}
					set_relation_potential_rival = root
				}
			}
		}

		stress_impact = {
			paranoid = medium_stress_impact_loss
			cynical = major_stress_impact_loss
			trusting = minor_stress_impact_gain
			zealous = major_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_zeal = -0.5
				ai_rationality = -0.25
			}
			modifier = {	# Weight up for stress.
				add = 20
				has_trait = paranoid
			}
			modifier = {	# Weight up for stress.
				add = 30
				has_trait = cynical
			}
			modifier = {	# Weight down for stress.
				add = -10
				has_trait = trusting
			}
			modifier = {	# Weight down for stress.
				add = -30
				has_trait = zealous
			}
		}
	}
}

##################################################
# Jomsvikings terrorise their neighbours
# by Ewan Cowhig Croft
# 0011 - 0020
##################################################

scripted_trigger fp1_viable_target_for_jomsviking_aggression_trigger = {
	holder = root
	
	# The Jomsvikings won't pillage faithful non-reformist Asatru. Much.
	NOT = { faith = faith:folketro }

	# Otherwise, they need something coastal or nearby.
	OR = {
		title_province = { is_coastal = yes }

		# TIT-25456
		any_neighboring_county = {
			save_temporary_scope_as = current_county
			exists = global_var:jomsvikings_title.holder.faith
			global_var:jomsvikings_title.holder.faith = {
				any_faith_holy_order = {
					title = global_var:jomsvikings_title
					any_leased_title = { county = scope:current_county }
				}
			}
		}
	}
}

scripted_trigger fp1_jomsviking_raid_source_one_wales_trigger = {
	this.leader.capital_province = {
		squared_distance = {
			target = scope:target_county.title_province
			value <= squared_distance_small
		}
	}
}

scripted_trigger fp1_jomsviking_raid_source_one_ireland_trigger = {
	this.leader.capital_province = {
		squared_distance = {
			target = scope:target_county.title_province
			value <= squared_distance_medium
		}
	}
}

scripted_trigger fp1_jomsviking_raid_source_one_france_trigger = {
	this.leader.capital_province = {
		squared_distance = {
			target = scope:target_county.title_province
			value <= squared_distance_large
		}
	}
}

scripted_trigger fp1_jomsviking_raid_source_one_hre_trigger = {
	this.leader.capital_province = {
		squared_distance = {
			target = scope:target_county.title_province
			value <= squared_distance_huge
		}
	}
}

#	You are singled out as a traitor/viable target.
fp1_jomsvikings.0011 = {
	type = character_event
	title = {
		first_valid = {
			# Reformed Asatruans.
			triggered_desc = {
				trigger = {
					religion = religion:folkgerman_religion
					faith = {
						NOT = { has_doctrine_parameter = unreformed }
					}
				}
				desc = fp1_jomsvikings.0011.t.reformed_norse
			}
			# Reformed non-Asatruans.
			triggered_desc = {
				trigger = {
					NAND = {
						religion = religion:folkgerman_religion
						faith = { has_doctrine_parameter = unreformed }
					}
				}
				desc = fp1_jomsvikings.0011.t.reformed_other
			}
			# Unreformed/fallback.
			desc = fp1_jomsvikings.0011.t.fallback
		}
	}
	desc = {
		desc = fp1_jomsvikings.0011.desc.intro
		# Is this an ideological raid or an economic one?
		first_valid = {
			# Ideological, reformist.
			triggered_desc = {
				trigger = {
					religion = religion:folkgerman_religion
					faith = {
						NOT = { has_doctrine_parameter = unreformed }
					}
				}
				desc = fp1_jomsvikings.0011.desc.ideological_reformist
			}
			# Ideological, other.
			triggered_desc = {
				trigger = {
					faith = {
						NOT = { has_doctrine_parameter = unreformed }
					}
				}
				desc = fp1_jomsvikings.0011.desc.ideological_other
			}
			# Economic.
			desc = fp1_jomsvikings.0011.desc.economic
		}
		desc = fp1_jomsvikings.0011.desc.outro
	}
	theme = war
	left_portrait = {
		character = root
		animation = shock
	}
	right_portrait = {
		character = scope:leader
		animation = rage
	}
	lower_left_portrait = scope:benefactor
	override_background = { reference = burning_building }

	trigger = {
		# DLC check.
		has_fp1_dlc_trigger = yes
		# The Jomsvikings must exist; check this before the usual standard checks for best efficiency.
		fp1_jomsvikings_exist_in_range_trigger = yes
		# Standard checks.
		is_available_at_peace_adult = yes
		NOT = { has_character_flag = had_event_fp1_jomsvikings_0011 }
		# Must not be following the unreformed version of Norse paganism.
		NOT = { faith = global_var:jomsvikings_title.holder.faith }
		# Out of fairness/for performance, restrict this to dukes & above.
		highest_held_title_tier >= tier_duchy
		# You must either hold a valid coastal province, or else border someone who hosts the Jomsvikings.
		any_sub_realm_county = { fp1_viable_target_for_jomsviking_aggression_trigger = yes }
	}

	weight_multiplier = {
		base = 1

		# Extra rage is reserved for Germanic reformists, as they're the traitoriest traitors.
		modifier = {
			add = 1
			religion = religion:folkgerman_religion
			faith = {
				NOT = { has_doctrine_parameter = unreformed }
			}
		}
	}

	immediate = {
		play_music_cue = "mx_cue_combat_2"
		# Set up a cooldowns.
		## Reformed Germanics get a shorter cooldown.
		if = {
			limit = {
				religion = religion:folkgerman_religion
				faith = {
					NOT = { has_doctrine_parameter = unreformed }
				}
			}
			add_character_flag = {
				flag = had_event_fp1_jomsvikings_0011
				days = 1825
			}
		}
		## Everyone else gets a decent breather.
		else = {
			add_character_flag = {
				flag = had_event_fp1_jomsvikings_0011
				days = 5475
			}
		}
		# Grab the Grandmaster.
		global_var:jomsvikings_title.holder = { save_scope_as = leader }
		# Flag a suitable target.
		## First, add everything to a list.
		every_sub_realm_county = {
			limit = { fp1_viable_target_for_jomsviking_aggression_trigger = yes }
			add_to_list = viable_jomsviking_targets_list
		}
		## Then, sort through that list and pick out a painful target.
		ordered_in_list = {
			list = viable_jomsviking_targets_list
			order_by = fp1_jomvsvikings_raid_value
			save_scope_as = target_county
		}
		# And an origin for the Jomsvikings.
		scope:leader.faith = {
			random_faith_holy_order = {
				limit = { leader = scope:leader }
				# Preferring something closer.
				## First, we check to see if it's even necessary to look at distance, since distance is an expensive check.
				if = {
					limit = {
						# Since this is purely for loc, we can just flat rule it out for non-players.
						root = { is_ai = no }
						# Otherwise, we're just looking to see if the Jomsvikings have more than two leased holdings.
						num_leased_titles >= 2
					}
					# Try successive distances.
					## About a Wales or so away.
					if = {
						limit = { fp1_jomsviking_raid_source_one_wales_trigger = yes }
						random_leased_title = {
							limit = { fp1_jomsviking_raid_source_one_wales_trigger = yes }
							save_scope_as = raid_source
						}
					}
					## About an Ireland away.
					else_if = {
						limit = { fp1_jomsviking_raid_source_one_ireland_trigger = yes }
						random_leased_title = {
							limit = { fp1_jomsviking_raid_source_one_ireland_trigger = yes }
							save_scope_as = raid_source
						}
					}
					## About a France away.
					else_if = {
						limit = { fp1_jomsviking_raid_source_one_france_trigger = yes }
						random_leased_title = {
							limit = { fp1_jomsviking_raid_source_one_france_trigger = yes }
							save_scope_as = raid_source
						}
					}
					## About an HRE away.
					else_if = {
						limit = { fp1_jomsviking_raid_source_one_hre_trigger = yes }
						random_leased_title = {
							limit = { fp1_jomsviking_raid_source_one_hre_trigger = yes }
							save_scope_as = raid_source
						}
					}
					## Still nothing? Uhhhh, sod it, just pick at random.
					else = {
						random_leased_title = { save_scope_as = raid_source }
					}
				}
				# But otherwise we just grab their capital or w/e.
				else = {
					random_leased_title = { save_scope_as = raid_source }
				}
			}
		}
		# Check to see if there's a good neighbour to import clergy from.
		## Only necessary if root is reformed, since unreformed don't have this option.
		if = {
			limit = {
				faith = {
					NOT = { has_doctrine_parameter = unreformed }
				}
			}
			random_neighboring_and_across_water_top_liege_realm_owner = {
				limit = {
					faith = root.faith
					# Filter out anyone who sincerely hates you.
					has_any_bad_relationship_with_root_trigger = no
					opinion = {
						target = root
						value >= very_high_negative_opinion
					}
				}
				save_scope_as = benefactor
			}
		}
	}

	# Try to out-strategise the pirates.
	option = {
		name = fp1_jomsvikings.0011.a

		# Martial duel to fend off the lightning assault.
		duel = {
			skill = martial
			target = scope:leader
			# You outmanoeuvre the pirates.
			## Really gotta balance in the player's favour, as the Jomsvikings tend to have OP leaders.
			60 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				desc = fp1_jomsvikings.0011.a.tt_success
				send_interface_toast = {
					title = fp1_jomsvikings.0011.a.tt_success
					left_icon = scope:leader
					# Tiny control loss.
					scope:target_county = { change_county_control = miniscule_county_control_loss }
					# Decent prestige gain for unreformed.
					if = {
						limit = {
							faith = { has_doctrine_parameter = unreformed }
						}
						add_prestige = medium_prestige_gain
					}
					# Decent piety gain for reformed.
					else = { add_piety = medium_piety_gain }
				}
			}
			# The Jomsvikings sack *everything*.
			40 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				desc = fp1_jomsvikings.0011.a.tt_failure
				send_interface_toast = {	
					title = fp1_jomsvikings.0011.a.tt_failure
					left_icon = scope:leader
					# Colossal control loss.
					scope:target_county = { change_county_control = massive_county_control_loss }
					# Minor prestige loss for unreformed.
					if = {
						limit = {
							faith = { has_doctrine_parameter = unreformed }
						}
						add_prestige = minor_prestige_loss
					}
					# Minor piety loss for reformed.
					else = { add_piety = minor_piety_loss }
				}
			}
		}

		stress_impact = {
			diligent = minor_stress_impact_loss
			arrogant = medium_stress_impact_loss
			vengeful = major_stress_impact_loss
			lazy = minor_stress_impact_gain
			humble = medium_stress_impact_gain
			forgiving = major_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 0.5
				ai_vengefulness = 0.5
			}
			modifier = {	# Weight up for stress.
				add = 10
				has_trait = diligent
			}
			modifier = {	# Weight up for stress.
				add = 20
				has_trait = arrogant
			}
			modifier = {	# Weight up for stress.
				add = 30
				has_trait = vengeful
			}
			modifier = {	# Weight down for stress.
				add = -10
				has_trait = lazy
			}
			modifier = {	# Weight down for stress.
				add = -20
				has_trait = humble
			}
			modifier = {	# Weight down for stress.
				add = -30
				has_trait = forgiving
			}
		}
	}
	
	# Pay to import foreign clergy.
	option = {
		name = {
			trigger = { exists = scope:benefactor }
			text = fp1_jomsvikings.0011.b.benefactor
		}
		name = {
			trigger = {
				NOT = { exists = scope:benefactor }
			}
			text = fp1_jomsvikings.0011.b.fallback
		}
		# Must be some flavour of reformed for this to be an issue.
		trigger = {
			faith = {
				NOT = { has_doctrine_parameter = unreformed }
			}
			# As there's gold involved, the character should either be a player or have enough to spend.
			OR = {
				is_ai = no
				short_term_gold >= medium_gold_value
			}
		}

		# Deduct gold.
		## This goes to scope:benefactor if they exist.
		if = {
			limit = { exists = scope:benefactor }
			pay_short_term_gold = {
				target = scope:benefactor
				gold = medium_gold_value
			}
		}
		## And otherwise is just vomited into the ether.
		remove_short_term_gold = medium_gold_value
		# If they exist, improve scope:benefactor's opinion of you.
		if = {
			limit = { exists = scope:benefactor }
			reverse_add_opinion = {
				target = scope:benefactor
				modifier = pleased_opinion
				opinion = 30
			}
		}
		# Lose control, but not that much.
		scope:target_county = { change_county_control = medium_county_control_loss }

		stress_impact = {
			generous = minor_stress_impact_loss
			forgiving = minor_stress_impact_loss
			greedy = major_stress_impact_gain
			vengeful = major_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_greed = -0.25
				ai_vengefulness = -0.75
			}
			modifier = {	# Weight up for stress.
				add = 10
				has_trait = generous
			}
			modifier = {	# Weight up for stress.
				add = 10
				has_trait = forgiving
			}
			modifier = {	# Weight down for stress.
				add = -20
				has_trait = greedy
			}
			modifier = {	# Weight down for stress.
				add = -20
				has_trait = vengeful
			}
		}
	}
	
	# Pay a gafol to stop the attacks.
	option = {
		name = fp1_jomsvikings.0011.c
		# The Jomsvikings will only accept tribute from non-threatening unreformeds.
		trigger = {
			faith = { has_doctrine_parameter = unreformed }
			# As there's gold involved, the character should either be a player or have enough to spend.
			OR = {
				is_ai = no
				short_term_gold >= medium_gold_value
			}
		}

		# Inform root that they won't lose any control.
		custom_tooltip = fp1_jomsvikings.0011.c.tt
		# Pay the gold straight to scope:leader.
		pay_treasury_or_gold = {
			target = scope:leader
			value = medium_treasury_or_gold_value
		}

		stress_impact = {
			generous = minor_stress_impact_loss
			forgiving = minor_stress_impact_loss
			greedy = major_stress_impact_gain
			vengeful = major_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_greed = -0.25
				ai_vengefulness = -0.75
			}
			modifier = {	# Weight up for stress.
				add = 10
				has_trait = generous
			}
			modifier = {	# Weight up for stress.
				add = 10
				has_trait = forgiving
			}
			modifier = {	# Weight down for stress.
				add = -20
				has_trait = greedy
			}
			modifier = {	# Weight down for stress.
				add = -20
				has_trait = vengeful
			}
		}
	}
	
	# How is this anything but a local problem?
	option = {
		name = fp1_jomsvikings.0011.d

		# Inform root that they let the county burn.
		custom_tooltip = fp1_jomsvikings.0011.d.tt
		# Reduce control.
		scope:target_county = { change_county_control = extreme_county_control_loss }

		stress_impact = {
			lazy = medium_stress_impact_loss
			callous = medium_stress_impact_loss
			sadistic = medium_stress_impact_loss
			diligent = minor_stress_impact_gain
			compassionate = medium_stress_impact_gain
			vengeful = major_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_energy = -0.25
				ai_boldness = -0.25
				ai_honor = -0.5
				ai_vengefulness = -0.5
			}
			modifier = {	# Weight up for stress.
				add = 20
				has_trait = lazy
			}
			modifier = {	# Weight up for stress.
				add = 20
				has_trait = callous
			}
			modifier = {	# Weight up for stress.
				add = 20
				has_trait = sadistic
			}
			modifier = {	# Weight down for stress.
				add = -10
				has_trait = diligent
			}
			modifier = {	# Weight down for stress.
				add = -20
				has_trait = compassionate
			}
			modifier = {	# Weight down for stress.
				add = -30
				has_trait = vengeful
			}
		}
	}
}

##################################################
# Jomsvikings send Event Troops
# by Ewan Cowhig Croft
# 0021 - 0030
##################################################

#	The Jomsvikings offer you a force of loyal pirates.
fp1_jomsvikings.0021 = {
	type = character_event
	title = fp1_jomsvikings.0021.t
	desc = fp1_jomsvikings.0021.desc
	theme = faith
	right_portrait = {
		character = scope:leader
		animation = admiration
	}
	override_background = { reference = fp1_beached_longships }

	trigger = {
		# DLC check.
		has_fp1_dlc_trigger = yes
		# The Jomsvikings must exist; check this before the usual standard checks for best efficiency.
		fp1_jomsvikings_exist_in_range_trigger = yes
		# Standard checks.
		is_available_at_peace_adult = yes
		NOT = { has_character_flag = had_event_fp1_jomsvikings_0021 }
		# You must fulfill their generic piety requirements.
		fp1_pious_enough_for_jomsvikings_reward_trigger = yes
	}

	weight_multiplier = {
		base = 1
		
		# Weight up for excess levels of piety & high levels of prestige.
		fp1_jomsvikings_approve_of_extra_piety_and_prestige_modifier = yes
	}

	immediate = {
		add_character_flag = {
			flag = had_event_fp1_jomsvikings_0021
			days = 1825
		}
		# Grab the Grandmaster.
		global_var:jomsvikings_title.holder = { save_scope_as = leader }
	}

	# Pirates? In *my* good Asatru household? It's more likely than you'd think!
	option = {
		name = fp1_jomsvikings.0021.a

		# Spend the piety cost.
		add_piety = major_piety_loss
		# Explanatory tooltip.
		custom_tooltip = fp1_jomsvikings.0021.a.tt
		# Give the actual troops.
		spawn_army = {
			levies = 250
			men_at_arms = {
				type = jomsviking_pirates
				stacks = 4
			}
			location = root.capital_province
			origin = scope:leader.capital_province
			name = fp1_jomsvikings_event_troops
		}

		stress_impact = {
			ambitious = medium_stress_impact_loss
			zealous = medium_stress_impact_loss
			content = medium_stress_impact_gain
			cynical = major_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_zeal = 0.75
				ai_sociability = 0.25
			}
			modifier = {	# Weight up for stress.
				add = 20
				has_trait = ambitious
			}
			modifier = {	# Weight up for stress.
				add = 20
				has_trait = zealous
			}
			modifier = {	# Weight down for stress.
				add = -20
				has_trait = content
			}
			modifier = {	# Weight down for stress.
				add = -30
				has_trait = cynical
			}
		}
	}
	
	# Perhaps some more reputable volunteers?
	option = {
		name = fp1_jomsvikings.0021.b

		# Spend the piety cost.
		add_piety = major_piety_loss
		# Explanatory tooltip.
		custom_tooltip = fp1_jomsvikings.0021.b.tt
		# Give the actual troops.
		spawn_army = {
			levies = 250
			men_at_arms = {
				type = bondi
				stacks = 4
			}
			location = root.capital_province
			origin = scope:leader.capital_province
			name = fp1_jomsvikings_event_troops
		}

		stress_impact = {
			ambitious = medium_stress_impact_loss
			zealous = medium_stress_impact_loss
			content = medium_stress_impact_gain
			cynical = major_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_zeal = 0.75
				ai_sociability = 0.25
			}
			modifier = {	# Weight up for stress.
				add = 20
				has_trait = ambitious
			}
			modifier = {	# Weight up for stress.
				add = 20
				has_trait = zealous
			}
			modifier = {	# Weight down for stress.
				add = -20
				has_trait = content
			}
			modifier = {	# Weight down for stress.
				add = -30
				has_trait = cynical
			}
		}
	}
	
	# I'm fine, thanks.
	option = {
		name = fp1_jomsvikings.0021.c

		# Gain a little consolation prestige.
		add_prestige = minor_prestige_gain
		# The Grandmaster considers this an insult.
		scope:leader = {
			add_opinion = {
				target = root
				modifier = insult_opinion
				opinion = -30
			}
			hidden_effect = {
				if = {
					limit = {
						can_set_relation_potential_rival_trigger = { CHARACTER = root }
					}
					set_relation_potential_rival = root
				}
			}
		}

		stress_impact = {
			paranoid = medium_stress_impact_loss
			cynical = major_stress_impact_loss
			trusting = minor_stress_impact_gain
			zealous = major_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_zeal = -0.5
				ai_rationality = -0.25
			}
			modifier = {	# Weight up for stress.
				add = 20
				has_trait = paranoid
			}
			modifier = {	# Weight up for stress.
				add = 30
				has_trait = cynical
			}
			modifier = {	# Weight down for stress.
				add = -10
				has_trait = trusting
			}
			modifier = {	# Weight down for stress.
				add = -30
				has_trait = zealous
			}
		}
	}
}

##################################################
# Jomsvikings challenge a Dishonourable Norse Pagan Ruler to Holmgang
# by Ewan Cowhig Croft
# 0041 - 0050
##################################################

scripted_trigger fp1_majorly_irked_jomsvikings_trigger = {
	OR = {
		## Trucebreaker.
		has_character_modifier = broke_truce
		## High tyranny.
		tyranny >= high_tyranny
	}
}

#	Considering you a blight on the faith, the Grandmaster of the Jomsvikings challenges you to the death.
fp1_jomsvikings.0041 = {
	type = character_event
	title = fp1_jomsvikings.0041.t
	desc = fp1_jomsvikings.0041.desc
	theme = death
	left_portrait = {
		character = root
		# Craven characters are terrifed.
		triggered_animation = {
			trigger = { has_trait = craven }
			animation = fear
		}
		# Everyone else is just shocked.
		triggered_animation = {
			trigger = {
				NOT = { has_trait = craven }
			}
			animation = shock
		}
	}
	right_portrait = {
		character = scope:leader
		animation = rage
	}
	override_background = { reference = throne_room }

	trigger = {
		# DLC check.
		has_fp1_dlc_trigger = yes
		# The Jomsvikings must exist; check this before the usual standard checks for best efficiency.
		fp1_jomsvikings_exist_in_range_trigger = yes
		# Standard checks; make sure that scope:leader is available also.
		is_landed = yes
		is_available_at_peace_adult = yes
		global_var:jomsvikings_title.holder = { is_available_at_peace_adult = yes }
		NOT = { has_character_flag = had_event_fp1_jomsvikings_0041 }
		# Must be following the unreformed version of Norse paganism.
		faith = global_var:jomsvikings_title.holder.faith
		# Eligibility checks.
		## For holmgangs in general.
		### Root.
		can_start_single_combat_trigger = yes
		can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = global_var:jomsvikings_title.holder }
		### Scope:leader.
		global_var:jomsvikings_title.holder = {
			can_start_single_combat_trigger = yes
			# No need to check whether scope:leader is an eligible warrior, as they're already the head of a warrior order.
		}
		## Is scope:leader either actually at all competent in a fight or dumb enough to think they are?
		global_var:jomsvikings_title.holder = {
			OR = {
				prowess >= medium_scsk
				has_trait = intellect_bad
			}
		}
		# You must unequivocally a bastard for them to think you need to be removed.
		OR = {
			# Any serious reason is valid.
			fp1_majorly_irked_jomsvikings_trigger = yes
			# But a large amount of minor reasons are also good.
			calc_true_if = {
				amount >= 4
				# The sinful.
				num_sinful_traits >= 2
				has_trait = cynical
				# The shameful.
				prestige_level <= 0
				piety_level <= 0
				tyranny >= medium_tyranny
				dread >= high_dread
				# Criminals.
				## Kinslayer.
				AND = {
					has_trait = kinslayer_1
					faith = { has_doctrine_parameter = kinslaying_any_dynasty_member_crime }
				}
				AND = {
					has_trait = kinslayer_2
					faith = { has_doctrine_parameter = kinslaying_extended_family_crime }
				}
				AND = {
					has_trait = kinslayer_3
					faith = { has_doctrine_parameter = kinslaying_close_kin_crime }
				}
				## Deviant.
				AND = {
					has_trait = deviant
					faith = { has_doctrine_parameter = deviancy_illegal }
				}
				## Cannibal.
				AND = {
					has_trait = cannibal
					NOT = {
						faith = { has_doctrine_parameter = cannibalism_legal }
					}
				}
				## Sodomite.
				AND = {
					has_trait = sodomite
					faith = { has_doctrine_parameter = homosexuality_illegal }
				}
				## Incestuous.
				AND = {
					has_trait = incestuous
					NOT = {
						faith = { has_doctrine_parameter = consanguinity_unrestricted_incest }
					}
				}
				## Adulterer/Fornicator
				AND = {
					OR = {
						has_trait = adulterer
						has_trait = fornicator
					}
					trait_is_criminal_in_faith_trigger = {
						FAITH = faith
						TRAIT = trait:adulterer
						GENDER_CHARACTER = this
					}
				}
				## Murderer.
				has_trait = murderer
				## Witch.
				AND = {
					has_trait = witch
					faith = { has_doctrine_parameter = witchcraft_illegal }
				}
			}
		}
	}

	weight_multiplier = {
		base = 1
		
		# We don't want to see this in the wild too much, so weight down against AI.
		modifier = {
			add = -0.5
			is_ai = yes
		}

		# Weight it down a bit more if you lack any of the major reasons.
		modifier = {
			add = -0.25
			fp1_majorly_irked_jomsvikings_trigger = no
		}
	}

	immediate = {
		play_music_cue = "mx_cue_murder"
		add_character_flag = {
			flag = had_event_fp1_jomsvikings_0041
			days = 7300
		}
		# Grab the Grandmaster.
		global_var:jomsvikings_title.holder = { save_scope_as = leader }
		# Rip the Grandmaster's shirt off.
		scope:leader = { add_character_flag = single_combat_stripped_to_waist }
		# Is the Grandmaster hopelessly outmatched? Try to boost them up a little if so.
		hidden_effect = {
			if = {
				limit = {
					prowess_diff = {
						target = scope:leader
						value >= 10
					}
				}
				scope:leader = {
					# Give them berserker.
					if = {
						limit = {
							NOT = { has_trait = berserker }
						}
						add_trait = berserker
					}
					# Or, failing that, raider.
					else_if = {
						limit = {
							NOT = { has_trait = viking }
						}
						add_trait = viking
					}
					# Or else just a slight base prowess boost.
					else = { add_prowess_skill = 4 }
				}
			}
		}
	}

	# I'll send you to Valhöll with a smile.
	option = {
		name = fp1_jomsvikings.0041.a

		# First, strip to the waist.
		add_character_flag = single_combat_stripped_to_waist
		# It's time to d-d-d-duel!
		custom_tooltip = fp1_jomsvikings.0041.a.tt
		configure_start_single_combat_effect = {
			SC_INITIATOR = scope:leader
			SC_ATTACKER = scope:leader
			SC_DEFENDER = root
			FATALITY = always
			FIXED = no
			LOCALE = throne_room
			OUTPUT_EVENT = fp1_jomsvikings.0043
			INVALIDATION_EVENT = fp1_jomsvikings.0042
		}
		# Show possible results.
		show_as_tooltip = {
			random_list = {
				# Root wins.
				50 = {
					show_chance = no
					desc = fp1_jomsvikings.0041.a.tt_success
					scope:leader = {
						death = {
							killer = root
							death_reason = death_duel
						}
					}
					add_prestige = major_prestige_gain
					add_kinslayer_trait_or_nothing_effect = { VICTIM = scope:leader }
				}
				# Scope:leader wins.
				50 = {
					show_chance = no
					desc = fp1_jomsvikings.0041.a.tt_failure
					death = {
						killer = scope:leader
						death_reason = death_duel
					}
					scope:leader = {
						add_prestige = major_prestige_gain
						add_kinslayer_trait_or_nothing_effect = { VICTIM = root }
					}
				}
			}
		}

		stress_impact = {
			arrogant = minor_stress_impact_loss
			brave = major_stress_impact_loss
			craven = major_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_honor = 0.5
				ai_vengefulness = 0.25
				ai_boldness = -0.25
			}
			modifier = {	# Weight up for stress.
				add = 10
				has_trait = arrogant
			}
			modifier = {	# Weight up for stress.
				add = 30
				has_trait = brave
			}
			modifier = {	# Weight down for stress.
				add = -30
				has_trait = craven
			}
		}
	}
	
	# You were a fool to come alone. Guards!
	option = {
		name = fp1_jomsvikings.0041.b

		# If possible, scope:leader immediately becomes your nemesis.
		if = {
			limit = {
				NOT = { has_relation_nemesis = scope:leader }
			}
			# No matter what relationship you had or didn't have, they become your nemesis.
			if = {
				limit = { has_relation_best_friend = scope:leader }
				remove_relation_best_friend = scope:leader
				set_relation_nemesis = {
					target = scope:leader
					reason = nemesis_refused_holmgang
				}
			}
			# If you have no relation or were friends, they become your rival.
			else_if = {
				limit = { has_relation_friend = scope:leader }
				remove_relation_friend = scope:leader
				set_relation_nemesis = {
					target = scope:leader
					reason = nemesis_refused_holmgang
				}
			}
			else = {
				set_relation_nemesis = {
					target = scope:leader
					reason = nemesis_refused_holmgang
				}
			}
		}
		else = {
			reverse_add_opinion = {
				target = scope:leader
				modifier = hate_opinion
				opinion = -50
			}
		}
		# You attempt to capture scope:leader.
		duel = {
			skill = martial
			target = scope:leader
			# Scope:leader is shackled & hauled off to the dungeon.
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -40
				}
				desc = fp1_jomsvikings.0041.b.tt_success
				send_interface_toast = {
					title = fp1_jomsvikings.0041.b.tt_success
					left_icon = scope:leader
					# Scope:leader is tossed straight into the dungeon.
					rightfully_imprison_character_effect = {
						TARGET = scope:leader
						IMPRISONER = root
					}
					hidden_effect = {
						scope:leader = { change_prison_type = dungeon }
					}
					# You still lose a prestige level, since you're being an utter bastard.
					add_prestige_level = -1
					# But you do gain a good chunk of dread.
					add_dread = medium_dread_gain
				}
			}
			# Scope:leader effects a daring escape.
			50 = {
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -40
				}
				desc = fp1_jomsvikings.0041.b.tt_failure
				send_interface_toast = {	
					title = fp1_jomsvikings.0041.b.tt_failure
					left_icon = scope:leader
					# You lose a prestige level *and* take heftier penalties for your dishonour.
					add_prestige_level = -1
					add_prestige = monumental_prestige_loss
				}
			}
		}

		stress_impact = {
			vengeful = medium_stress_impact_loss
			arbitrary = major_stress_impact_loss
			forgiving = medium_stress_impact_gain
			just = major_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_vengefulness = 0.5
				ai_honor = -0.75
			}
			modifier = {	# Weight up for stress.
				add = 20
				has_trait = vengeful
			}
			modifier = {	# Weight up for stress.
				add = 30
				has_trait = arbitrary
			}
			modifier = {	# Weight down for stress.
				add = -20
				has_trait = forgiving
			}
			modifier = {	# Weight down for stress.
				add = -30
				has_trait = just
			}
		}
	}

	# Hah, right, because I just fight any random pirate that walks into my castle?
	option = {
		name = fp1_jomsvikings.0041.c

		# If possible, scope:leader immediately becomes your rival.
		if = {
			limit = {
				NOR = {
					has_relation_rival = scope:leader
					has_relation_nemesis = scope:leader
				}
			}
			# If you were best friends, they become your nemesis.
			if = {
				limit = { has_relation_best_friend = scope:leader }
				remove_relation_best_friend = scope:leader
				set_relation_nemesis = {
					target = scope:leader
					reason = rival_refused_holmgang
				}
			}
			# If you have no relation or were friends, they become your rival.
			else_if = {
				limit = { has_relation_friend = scope:leader }
				remove_relation_friend = scope:leader
				set_relation_rival = {
					target = scope:leader
					reason = rival_refused_holmgang
				}
			}
			else = {
				set_relation_rival = {
					target = scope:leader
					reason = rival_refused_holmgang
				}
			}
		}
		else = {
			reverse_add_opinion = {
				target = scope:leader
				modifier = hate_opinion
				opinion = -50
			}
		}
		# And you take some hefty prestige penalties for your refusal.
		add_prestige_level = -1
		add_prestige = massive_prestige_loss

		stress_impact = {
			craven = major_stress_impact_loss
			arrogant = minor_stress_impact_gain
			brave = major_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_rationality = 0.25
				ai_vengefulness = -0.25
				ai_boldness = -0.5
			}
			modifier = {	# Weight up for stress.
				add = 30
				has_trait = craven
			}
			modifier = {	# Weight down for stress.
				add = -10
				has_trait = arrogant
			}
			modifier = {	# Weight down for stress.
				add = -30
				has_trait = brave
			}
		}
	}
}

#	Bout has invalidated, inform affected parties.
fp1_jomsvikings.0042 = {
	hidden = yes

	immediate = {
		# Inform root that the duel has invalidated.
		root = {
			send_interface_toast = {	
				title = fp1_jomsvikings.0042.trigger_failure
				left_icon = scope:leader
			}
		}
	}
}

#	Bout ended, sort after effects.
fp1_jomsvikings.0043 = {
	hidden = yes

	immediate = {
		# If scope:leader won, give them their prestige/clothes and apply kinslayer stuff.
		if = {
			limit = { scope:sc_victor = scope:leader }
			add_prestige = major_prestige_gain
			remove_character_flag = single_combat_stripped_to_waist
			single_combat_have_slain_kin_effect = {
				ATTACKER = scope:leader
				DEFENDER = root
			}
		}
		# Otherwise, root won.
		else = {
			# Nothing complex here, just put your shirt back on & send out the event after a small delay (for layering).
			root = {
				remove_character_flag = single_combat_stripped_to_waist
				trigger_event = {
					id = fp1_jomsvikings.0044
					days = 1
				}
			}
		}
	}
}

#	Root won the bout, process their ending.
fp1_jomsvikings.0044 = {
	type = character_event
	title = fp1_jomsvikings.0044.t
	desc = fp1_jomsvikings.0044.desc
	theme = death
	left_portrait = {
		character = root
		animation = personality_bold
	}
	right_portrait = {
		character = scope:leader
		animation = fear
	}
	override_background = { reference = throne_room }

	trigger = {
		# DLC check.
		has_fp1_dlc_trigger = yes
	}

	immediate = {
		play_music_cue = "mx_cue_combat_stinger"
		# Mention the death of scope:leader.
		show_as_tooltip = {
			scope:leader = {
				death = {
					killer = root
					death_reason = death_duel
				}
			}
		}
		# Hand out root's prestige.
		add_prestige = major_prestige_gain
		scope:leader = {
			add_character_flag = is_naked
		}
	}

	# Anyone *else* want to have a go?
	option = {
		name = fp1_jomsvikings.0044.a

		# And a fair amount of stress loss for coming through that.
		add_stress = massive_stress_loss

		# No stress impact for the standard option.
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 0.5
				ai_vengefulness = 0.25
			}
		}
	}
	
	# And the justness of my rule is proven again.
	option = {
		name = fp1_jomsvikings.0044.b
		# No point reducing tyranny if you don't have any.
		trigger = { tyranny >= 1 }

		# Get a decent tyranny reduction for your troubles.
		add_tyranny = massive_tyranny_loss

		stress_impact = {
			deceitful = major_stress_impact_loss
			arbitrary = major_stress_impact_loss
			honest = major_stress_impact_gain
			just = major_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_rationality = 0.5
				ai_honor = -0.5
			}
			modifier = {	# Weight up for stress.
				add = 30
				has_trait = deceitful
			}
			modifier = {	# Weight up for stress.
				add = 30
				has_trait = arbitrary
			}
			modifier = {	# Weight down for stress.
				add = 30
				has_trait = honest
			}
			modifier = {	# Weight down for stress.
				add = 30
				has_trait = just
			}
		}
	}
	
	# This simply shows that Asatru is an unworthy faith...
	option = {
		name = fp1_jomsvikings.0044.c
		# AI doesn't use this functionality, so we don't want them taking this option.
		trigger = { is_ai = no }

		# Gain a modifier making it substantially cheaper to convert to other faiths.
		add_character_modifier = {
			modifier = fp1_jomsvikings_caused_faith_crisis_modifier
			years = 10
		}

		stress_impact = {
			cynical = major_stress_impact_loss
			zealous = major_stress_impact_gain
		}
		ai_chance = {
			# AI is banned, so this is unimportant.
			base = 0
		}
	}
	after = {
		scope:leader = {
			remove_character_flag = is_naked
		}
	}
}


##################################################
# #Special Events

##################################################
# Jomsvikings Destroyed
# by Ewan Cowhig Croft
# 1001 - 1010
##################################################

fp1_jomsvikings.1001 = {
	type = character_event
	title = fp1_jomsvikings.1001.t
	desc = {
		desc = fp1_jomsvikings.1001.desc.intro
		first_valid = {
			# If someone reformed Norse paganism, the Jomsvikings lament it.
			triggered_desc = {
				trigger = {
					religion:germanic_religion = {
						any_faith = {
							NOT = { has_doctrine_parameter = unreformed }
						}
					}
				}
				desc = fp1_jomsvikings.1001.desc.reformed_asatru_exists
			}
			# Otherwise, just a general big RIP.
			desc = fp1_jomsvikings.1001.desc.fallback
		}
		desc = fp1_jomsvikings.1001.desc.outro
	}
	theme = faith
	override_background = { reference = fp1_tribal_temple }
	left_portrait = {
		character = scope:leader
		animation = shame
	}

	trigger = {
		# DLC check.
		has_fp1_dlc_trigger = yes
	}

	immediate = {
		# Huehuehue.
		play_music_cue = "mx_cue_peace_ensues"
		custom_tooltip = fp1_jomsvikings.1001.holy_order_destroyed
	}

	# The gods forsake us!
	option = {
		name = fp1_jomsvikings.1001.a
		trigger = { faith = faith:folketro }

		# No more benefits for pious Norse pagans.
		custom_tooltip = fp1_jomsvikings.1001.a.tt

		# No stress for a notification event.
		ai_chance = {
			# Notification event needs no AI chance.
			base = 100
		}
	}
	
	# Good riddance to bad rubbish!
	option = {
		name = fp1_jomsvikings.1001.b
		trigger = {
			NOT = { faith = faith:folketro }
		}

		# No more Jomsviking piracy!
		custom_tooltip = fp1_jomsvikings.1001.b.tt

		# No stress for a notification event.
		ai_chance = {
			# Notification event needs no AI chance.
			base = 100
		}
	}
	
	# Yhomz-who?
	option = {
		name = fp1_jomsvikings.1001.c
		trigger = {
			NOT = { faith = faith:folketro }
		}

		# No more Jomsviking piracy!
		custom_tooltip = fp1_jomsvikings.1001.c.tt

		# No stress for a notification event.
		ai_chance = {
			# Notification event needs no AI chance.
			base = 100
		}
	}
}


##################################################
# Jomsvikings Formed Autonomously
# by Ewan Cowhig Croft
# 1011 - 1020
##################################################

#	The Jomsvikings are founded automagically.
fp1_jomsvikings.1011 = {
	scope = none
	hidden = yes

	# Trigger block handled in yearly_on_actions.

	immediate = {
		# Collate eligible targets in the region.
		every_county_in_region = {
			region = dlc_fp1_region_non_scandinavian_southern_baltic
			limit = {
				holder = { fp1_ruler_eligible_for_jomsviking_coup = yes }
			}
			add_to_list = jomsvikings_coup_targets_list
		}
		# Sort through to pick the best.
		ordered_in_list = {
			list = jomsvikings_coup_targets_list
			order_by = fp1_jomsvikings_coup_priority_value
			save_scope_as = coup_target
		}
		## Grab the soon-to-be former holder for loc.
		scope:coup_target.holder = { save_scope_as = victim }
		# Transfer the target title to its new owner.
		## First, create said-owner.
		create_character = {
			template = fp1_jomsviking_bigwig
			location = scope:coup_target.title_province
			save_scope_as = founder
		}
		## Second, give that owner the actual title.
		create_title_and_vassal_change = {
			type = conquest
			save_scope_as = change
			add_claim_on_loss = yes
		}
		scope:coup_target = {
			change_title_holder = {
				holder = scope:founder
				change = scope:change
			}
		}
		resolve_title_and_vassal_change = scope:change
		## Third, give scope:founder their independence if they lacked it.
		scope:founder = {
			if = {
				limit = { top_liege != this }
				create_title_and_vassal_change = {
					type = independency
					save_scope_as = change
				}
				change_liege_or_become_independent = {
					CHANGE = scope:change
					VASSAL = this
				}
				resolve_title_and_vassal_change = scope:change
			}
		}
		## Fourth, give the owner some event troops & cash to keep them afloat.
		scope:founder = {
			spawn_army = {
				levies = { add = 1000 }
				inheritable = yes
				uses_supply = yes
				location = scope:coup_target.title_province
				name = fp1_jomsvikings_event_troops
			}
			forge_the_jomsvikings_maa_reward_effect = yes
		}
		# Create the Jomsvikings.
		scope:founder = { forge_the_jomsvikings_scripted_effect = yes }
		# Send out notification events.
		save_scope_value_as = {
			name = jomsvikings_autofounded
			value = yes
		}
		every_player = {
			limit = {
				OR = {
					religion = religion:folkgerman_religion
					save_temporary_scope_as = neighbouring_players
					scope:founder = {
						any_neighboring_and_across_water_top_liege_realm_owner = { this = scope:neighbouring_players }
					}
				}
				# No need to exempt scope:founder, as they're always an AI in this variant.
			}
			trigger_event = fp1_major_decisions.0033
		}
	}
}
