﻿##################################################
# Decisions
##################################################

##### Struggle Ending #####

### Common

# Add all Involved cultures with counties to a list
fp2_struggle_ending_culture_list_effect = {
	every_culture_global = {		
		limit = {
			save_temporary_scope_as = this_culture
			struggle:iberian_struggle = { is_culture_involved_in_struggle = scope:this_culture }
			culture_number_of_counties > 0
		}
		save_scope_as = culture_scope
		add_to_global_variable_list = {
			name = fp2_struggle_ending_culture_list
			target = scope:culture_scope
		}
	}
}

# Add all Involved independent rulers to a list
fp2_stuggle_ending_involved_list_effect = {
	save_scope_as = struggle_ender
	struggle:iberian_struggle = {
		every_involved_ruler = {
			limit = {
				is_independent_ruler = yes
				primary_title = { is_mercenary_company = no }
			}
			add_to_list = struggle_involvees
		}
	}
	# Add all involved cultures with counties to a list, for war checks
	fp2_struggle_ending_culture_list_effect = yes
	# Add all involved faiths with counties to a list, for marriage and holy war checks
	fp2_struggle_ending_faith_list_effect = yes
}

### Hostility

# Add all Involved faiths with counties to a list
fp2_struggle_ending_faith_list_effect = {
	every_religion_global = {
		limit = {
			any_faith = {
				save_temporary_scope_as = this_faith
				struggle:iberian_struggle = { is_faith_involved_in_struggle = scope:this_faith }
				num_county_followers > 0
			}
		}
		every_faith = {
			limit = {
				struggle:iberian_struggle = { is_faith_involved_in_struggle = prev }
				num_county_followers > 0
			}
			save_scope_as = faith_scope
			add_to_global_variable_list = {
				name = fp2_struggle_ending_faith_list
				target = scope:faith_scope
			}
		}
	}
}

# Set cultural acceptance between all Involved cultures
fp2_struggle_hostility_cultural_acceptance_effect = {
	hidden_effect = {
		while = {
			limit = {
				any_in_global_list = {
					variable = fp2_struggle_ending_culture_list
					NOT = { has_variable = fp2_struggle_hostility_flag }
				}
			}
			ordered_in_global_list = {
				variable = fp2_struggle_ending_culture_list
				limit = {
					NOT = { has_variable = fp2_struggle_hostility_flag }
				}
				order_by = culture_number_of_counties
				save_scope_as = culture_scope
				set_variable = fp2_struggle_hostility_flag # variable ensures acceptance is only lost once per couple
			}
			every_in_global_list = {
				variable = fp2_struggle_ending_culture_list
				limit = {
					NOR = {
						has_variable = fp2_struggle_hostility_flag
						this = scope:culture_scope
					}
				}
				change_cultural_acceptance = {
					target = scope:culture_scope
					value = fp2_struggle_hostility_culture_acceptance_value
					desc = fp2_struggle_hostility_acceptance_tt
				}
			}
		}
		every_in_global_list = {
			variable = fp2_struggle_ending_culture_list
			remove_variable = fp2_struggle_hostility_flag
		}
	}
}

# Set cultural acceptance between all Involved cultures
fp2_struggle_hostility_cultural_acceptance_large_effect = {
	hidden_effect = {
		while = {
			limit = {
				any_in_global_list = {
					variable = fp2_struggle_ending_culture_list
					NOT = { has_variable = fp2_struggle_hostility_flag }
				}
			}
			ordered_in_global_list = {
				variable = fp2_struggle_ending_culture_list
				limit = {
					NOT = { has_variable = fp2_struggle_hostility_flag }
				}
				order_by = culture_number_of_counties
				save_scope_as = culture_scope
				set_variable = fp2_struggle_hostility_flag # variable ensures acceptance is only lost once per couple
			}
			every_in_global_list = {
				variable = fp2_struggle_ending_culture_list
				limit = {
					NOR = {
						has_variable = fp2_struggle_hostility_flag
						this = scope:culture_scope
					}
				}
				change_cultural_acceptance = {
					target = scope:culture_scope
					value = fp2_struggle_hostility_culture_acceptance_value
					desc = fp2_struggle_hostility_acceptance_tt
				}
			}
		}
		every_in_global_list = {
			variable = fp2_struggle_ending_culture_list
			remove_variable = fp2_struggle_hostility_flag
		}
	}
}


### Compromise

# Creates a new de jure kingdom from a duchy
fp2_struggle_compromise_create_new_kingdom_effect = {
	add_to_list = duchy_kingdom
	create_dynamic_title = {
		tier = kingdom
		name = $TYPE$_CREATED_TITLE_NAME
	}
	scope:old_title = {
		# Edge case for kingdoms which do not control their own capital duchy
		if = {
			limit = {
				title_capital_county = de_jure_liege.title_capital_county
				exists = de_jure_liege.holder
				NOT = { holder = de_jure_liege.holder }
			}
			de_jure_liege = {
				# So kingdom has a capital
				set_capital_county = this.holder.capital_county
				# So titles do not share CoAs
				set_coa = this.holder.house
			}
		}
		set_de_jure_liege_title = scope:new_title
	}
	scope:new_title = {
		set_coa = $FETCH$
		set_color_from_title = $FETCH$
		set_capital_county = $FETCH$.title_capital_county
		set_de_jure_liege_title = title:e_spain
	}
}

# Create new de jure kingdoms from relevant duchies
fp2_struggle_compromise_duchies_to_kingdoms_effect = {
	hidden_effect = {
		title:d_zaragoza = { add_to_list = titular_iberian_duchies }
		title:e_spain = {
			# Cycle through de jure duchies held by independent dukes
			every_in_de_jure_hierarchy = {
				# Prefer primary titles
				limit = {
					this = this.holder.primary_title
					fp2_struggle_ending_compromise_independent_duchy_trigger = yes
				}
				# Then capital titles
				alternative_limit = {
					this.title_capital_county = this.kingdom.title_capital_county
					fp2_struggle_ending_compromise_independent_duchy_trigger = yes
				}
				alternative_limit = { fp2_struggle_ending_compromise_independent_duchy_trigger = yes }
				save_scope_as = old_title
				fp2_struggle_compromise_create_new_kingdom_effect = { TYPE = NEW FETCH = scope:old_title }
			}
			# Cycle through titular duchies held by independent dukes (e.g. Zaragoza)
			every_in_list = {
				list = titular_iberian_duchies
				save_scope_as = special_duchy
				if = {
					# Independent holder who controls at least 50% a de jure duchy, and no kingdom has the same as that duchy
					limit = { fp2_struggle_ending_compromise_titular_trigger = yes }
					title:e_spain = {
						random_in_de_jure_hierarchy = {
							limit = {
								tier = tier_duchy
								any_direct_de_jure_vassal_title = {
									percent >= 0.5
									holder.top_liege = scope:special_duchy.holder
								}
							}
							save_scope_as = old_title
						}
					}
					fp2_struggle_compromise_create_new_kingdom_effect = { TYPE = SPECIAL FETCH = scope:special_duchy }
				}
			}
			# Cycle through uncreated de jure duchies to split if needed (e.g. Coimbra)
			every_in_de_jure_hierarchy = {
				limit = { fp2_struggle_ending_compromise_split_duchy_trigger = yes }
			    save_scope_as = old_title
				fp2_struggle_compromise_create_new_kingdom_effect = { TYPE = NEW FETCH = scope:old_title }
			}
		}
	}
}

# Moves duchies to another kingdom
fp2_struggle_compromise_transfer_duchies_effect = {
	title:e_spain = {
		while = {
			limit = {
				any_in_de_jure_hierarchy = { fp2_struggle_compromise_transfer_duchy_trigger = yes }
			}
			every_in_de_jure_hierarchy = {
				limit = { fp2_struggle_compromise_transfer_duchy_trigger = yes }
				add_to_list = transferred_duchies
				# To ensure kingdom's capitals are always inside them
				if = {
					limit = { title_capital_county = kingdom.title_capital_county }
					kingdom = {
						save_scope_as = old_kingdom
						if = {
							limit = {
								any_direct_de_jure_vassal_title = {
									NOT = { is_in_list = transferred_duchies }
									title_capital_county.kingdom = scope:old_kingdom
								}
							}
							random_direct_de_jure_vassal_title = {
								limit = {
									NOT = { is_in_list = transferred_duchies }
									title_capital_county.kingdom = scope:old_kingdom
								}
								title_capital_county = { save_scope_as = new_capital }
							}
							set_capital_county = scope:new_capital
						}
					}
				}
				if = {
					limit = { title_capital_county.holder.top_liege.primary_title.tier = tier_kingdom }
					set_de_jure_liege_title = title_capital_county.holder.top_liege.primary_title
				}
				else = { set_de_jure_liege_title = title_capital_county.holder.top_liege.capital_county.kingdom }
			}
		}
	}
}

# Creates a new de jure empire from a kingdom
fp2_struggle_compromise_create_new_empire_effect = {
	add_to_list = kingdom_empire
	save_scope_as = old_title
	create_dynamic_title = {
		tier = empire
		name = NEW_CREATED_TITLE_NAME
	}
	scope:old_title = { set_de_jure_liege_title = scope:new_title }
	scope:new_title = {
		add_to_list = new_empire_list
		set_coa = scope:old_title
		set_color_from_title = scope:old_title
		set_capital_county = scope:old_title.title_capital_county
	}
}

# Create new de jure empires from relevant kingdoms
fp2_struggle_compromise_kingdoms_to_empires_effect = {
	hidden_effect = {
		title:e_spain = {
			every_in_de_jure_hierarchy = {
				limit = {
					tier = tier_kingdom
					any_in_de_jure_hierarchy = {
						count >= fp2_struggle_compromise_ending_minimal_empire_size
						tier = tier_county
					}
				}
				fp2_struggle_compromise_create_new_empire_effect = yes
			}
		}
	}
}

# Distribute remaining Kingdoms to neighboring empires
fp2_struggle_compromise_distribute_kingdoms_to_neighbouring_empires_effect = {
	hidden_effect = {
		title:e_spain = {
			every_in_de_jure_hierarchy = {
				limit = { tier = tier_kingdom }
				save_scope_as = kingdom_to_distribute
				random_title_to_title_neighboring_and_across_water_kingdom = {
					limit = { # Prefer land borders
						any_this_title_or_de_jure_above = { is_in_list = new_empire_list }
						any_title_to_title_neighboring_kingdom = { this = scope:kingdom_to_distribute }
					}
					alternative_limit = { # Then sea borders
						any_this_title_or_de_jure_above = { is_in_list = new_empire_list }
					}
					weight = {
						modifier = { # Value one from Iberia for small empire
							add = {
								value = 2
								subtract = {
									value = 0
									every_in_de_jure_hierarchy = {
										add = 0.25
									}
								}
								min = 0
							}
							any_in_de_jure_hierarchy = {
			 					tier = tier_county
								title_province = { geographical_region = world_europe_west_iberia }
							}
						}
						modifier = { # Value smaller empire
							add = {
								value = 2
								subtract = {
									value = 0
									every_in_de_jure_hierarchy = {
										limit = { tier = tier_kingdom }
										add = 0.5
									}
								}
								min = 0
							}
						}
						modifier = { # Value most shared borders
							add = {
								value = 0
								every_in_de_jure_hierarchy = {
									limit = {
										tier = tier_county
										any_neighboring_county = { target_is_de_jure_liege_or_above = scope:kingdom_to_distribute }
									}
									add = 1
								}
								min = 0
							}
						}
					}
					add_to_list = transferred_kingdoms
					scope:kingdom_to_distribute = { set_de_jure_liege_title = prev.de_jure_liege }
				}
			}
		}
	}
}

fp2_struggle_compromise_distribute_the_actual_titles = {
	struggle:iberian_struggle = {
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = change
		}
		every_involved_ruler = {
			limit = {
				save_temporary_scope_as = new_owner_temp
				primary_title = {
					tier = tier_duchy
					exists = de_jure_liege
					de_jure_liege = { fp2_struggle_ending_compromise_create_title_trigger = yes }
				}
			}
			save_scope_as = new_owner
			primary_title.de_jure_liege = {
				change_title_holder = {
					holder = scope:new_owner
					change = scope:change
				}
				add_to_list = assigned_title
			}
		}
		every_involved_ruler = {
			limit = {
				save_temporary_scope_as = new_owner_temp
				primary_title = {
					tier = tier_kingdom
					exists = de_jure_liege
					de_jure_liege = { fp2_struggle_ending_compromise_create_title_trigger = yes }
				}
			}
			save_scope_as = new_owner
			primary_title.de_jure_liege = {
				change_title_holder = {
					holder = scope:new_owner
					change = scope:change
				}
				add_to_list = assigned_title
			}
		}
		resolve_title_and_vassal_change = scope:change
	}
}

# Add house modifiers to Involved rulers; Didn't rename the key for localization cost reasons
fp2_struggle_compromise_modifier_rewards_effect = {
	struggle:iberian_struggle = {
		show_as_tooltip = {
			every_involved_ruler = {
				limit = { this = root }
				custom = fp2_struggle_compromise_house_rewards_tt
				fp2_struggle_compromise_modifier_rewards_personal_house_effect = yes
			}
			every_involved_ruler = {
				limit = { this = root }
				custom = fp2_struggle_compromise_defensive_rewards_tt
				fp2_struggle_compromise_modifier_rewards_personal_county_effect = yes
			}
		}
		hidden_effect = {
			every_involved_ruler = {
				limit = {
					is_independent_ruler = yes
					primary_title = { is_mercenary_company = no }
					exists = house
					NOT = {
						house = { has_house_modifier = fp2_struggle_compromise_house_reward_modifier }
					}
				}
				fp2_struggle_compromise_modifier_rewards_personal_house_effect = yes
			}
			every_involved_ruler = {
				limit = {
					is_independent_ruler = yes
					primary_title = { is_mercenary_company = no }
				}
				fp2_struggle_compromise_modifier_rewards_personal_county_effect = yes
			}
		}
	}
}

# Add house modifiers to a single character (used in tooltips);
fp2_struggle_compromise_modifier_rewards_personal_house_effect = {
	house = {
		custom_tooltip = fp2_struggle_compromise_house_enforce_truce_rewards_tt
		add_house_modifier = {
			modifier = fp2_struggle_compromise_house_reward_modifier
		}
	}
}

# Add county modifiers to a single character (used in tooltips)
fp2_struggle_compromise_modifier_rewards_personal_county_effect = {
	capital_county = {
		add_county_modifier = {
			modifier = fp2_struggle_compromise_defensive_reward_modifier
		}
	}
}

# Add truces between all Involved rulers
fp2_struggle_compromise_truce_effect = {
	hidden_effect = {
		if = {
			limit = {
				struggle:iberian_struggle = {
					any_involved_ruler = {
						count > 1
						is_independent_ruler = yes
						primary_title = { is_mercenary_company = no }
					}
				}
			}
			struggle:iberian_struggle = {
				every_involved_ruler = {
					limit = {
						is_independent_ruler = yes
						primary_title = { is_mercenary_company = no }
					}
					add_to_list = fp2_struggle_compromise_truce_list
				}
			}
			while = {
				limit = {
					any_in_list = {
						list = fp2_struggle_compromise_truce_list
						NOT = { has_variable = fp2_struggle_compromise_flag }
					}
				}
				ordered_in_list = {
					list = fp2_struggle_compromise_truce_list
					limit = {
						NOT = { has_variable = fp2_struggle_compromise_flag }
					}
					order_by = realm_size
					save_scope_as = realm_scope
					set_variable = fp2_struggle_compromise_flag # variable ensures truces are set only once per couple
				}
				every_in_list = {
					list = fp2_struggle_compromise_truce_list
					limit = {
						NOR = {
							has_variable = fp2_struggle_compromise_flag
							this = scope:realm_scope
							has_truce = scope:realm_scope
						}
					}
					add_truce_both_ways = {
						character = scope:realm_scope
						days = 3650
						name = TRUCE_FP2_COMPROMISE
					}
				}
			}
			every_in_list = {
				list = fp2_struggle_compromise_truce_list
				remove_variable = fp2_struggle_compromise_flag
			}
		}
	}
}

### Conciliation

# Add House modifiers to all Involved rulers
fp2_struggle_conciliation_modifier_rewards_effect = {
	struggle:iberian_struggle = {
		every_involved_ruler = {
			limit = { this = root }
			custom = fp2_struggle_compromise_house_rewards_tt
			show_as_tooltip = { fp2_struggle_conciliation_modifier_rewards_personal_effect = yes }
		}
		every_involved_ruler = {
			limit = {
				is_independent_ruler = yes
				primary_title = { is_mercenary_company = no }
				exists = house
				NOT = {
					house = { has_house_modifier = fp2_struggle_conciliation_house_reward_modifier }
				}
			}
			hidden_effect = { fp2_struggle_conciliation_modifier_rewards_personal_effect = yes }
		}
	}
}

# Add House modifier to a single character (used in tooltips)
fp2_struggle_conciliation_modifier_rewards_personal_effect = {
	house = {
		add_house_modifier = {
			modifier = fp2_struggle_conciliation_house_reward_modifier
		}
	}
}

# Add cultural acceptance between Involved cultures
fp2_struggle_conciliation_cultural_acceptance_effect = {
	hidden_effect = {
		while = {
			limit = {
				any_in_global_list = {
					variable = fp2_struggle_ending_culture_list
					NOT = { has_variable = fp2_struggle_conciliation_flag }
				}
			}
			ordered_in_global_list = {
				variable = fp2_struggle_ending_culture_list
				limit = {
					NOT = { has_variable = fp2_struggle_conciliation_flag }
				}
				order_by = culture_number_of_counties
				save_scope_as = culture_scope
				set_variable = fp2_struggle_conciliation_flag # variable ensures acceptance is only gained once per couple
			}
			every_in_global_list = {
				variable = fp2_struggle_ending_culture_list
				limit = {
					NOR = {
						has_variable = fp2_struggle_conciliation_flag
						this = scope:culture_scope
					}
				}
				change_cultural_acceptance = {
					target = scope:culture_scope
					value = fp2_struggle_conciliation_culture_acceptance_value
					desc = fp2_struggle_conciliation_acceptance_tt
				}
			}
		}
		every_in_global_list = {
			variable = fp2_struggle_ending_culture_list
			remove_variable = fp2_struggle_conciliation_flag
		}
	}
}

##################################################
# Interactions
##################################################

synergise_friendship_interaction_bonus_individual_effect = {
	$SENIOR_CHARACTER$ = {
		# Diplomacy education.
		if = {
			limit = { has_trait = education_diplomacy }
			add_character_modifier = fp2_single_synergy_diplomacy_modifier
			$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_single_synergy_diplomacy_modifier }
		}
		# Martial education.
		if = {
			limit = { has_trait = education_martial }
			add_character_modifier = fp2_single_synergy_martial_modifier
			$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_single_synergy_martial_modifier }
		}
		# Stewardship education.
		if = {
			limit = { has_trait = education_stewardship }
			add_character_modifier = fp2_single_synergy_stewardship_modifier
			$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_single_synergy_stewardship_modifier }
		}
		# Intrigue education.
		if = {
			limit = { has_trait = education_intrigue }
			add_character_modifier = fp2_single_synergy_intrigue_modifier
			$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_single_synergy_intrigue_modifier }
		}
		# Learning education.
		if = {
			limit = { has_trait = education_learning }
			add_character_modifier = fp2_single_synergy_learning_modifier
			$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_single_synergy_learning_modifier }
		}
	}
}

# Being $SENIOR_CHARACTER$ doesn't actually do anything in this variant, just maintaining term parity with the other synergy effect.
synergise_friendship_interaction_bonus_combined_effect = {
	$SENIOR_CHARACTER$ = {
		# Diplomacy combi-synergies.
		if = {
			limit = { has_trait = education_diplomacy }
			# Diplomacy-Diplomacy
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_diplomacy }
				}
				add_character_modifier = fp2_double_synergy_diplomacy_diplomacy_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_diplomacy_diplomacy_modifier }
			}
			# Diplomacy-Martial
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_martial }
				}
				add_character_modifier = fp2_double_synergy_diplomacy_martial_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_diplomacy_martial_modifier }
			}
			# Diplomacy-Stewardship
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_stewardship }
				}
				add_character_modifier = fp2_double_synergy_diplomacy_stewardship_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_diplomacy_stewardship_modifier }
			}
			# Diplomacy-Intrigue
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_intrigue }
				}
				add_character_modifier = fp2_double_synergy_diplomacy_intrigue_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_diplomacy_intrigue_modifier }
			}
			# Diplomacy-Learning
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_learning }
				}
				add_character_modifier = fp2_double_synergy_diplomacy_learning_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_diplomacy_learning_modifier }
			}
		}
		# Martial combi-synergies.
		if = {
			limit = { has_trait = education_martial }
			# Martial-Diplomacy
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_diplomacy }
				}
				add_character_modifier = fp2_double_synergy_martial_diplomacy_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_martial_diplomacy_modifier }
			}
			# Martial-Martial
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_martial }
				}
				add_character_modifier = fp2_double_synergy_martial_martial_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_martial_martial_modifier }
			}
			# Martial-Stewardship
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_stewardship }
				}
				add_character_modifier = fp2_double_synergy_martial_stewardship_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_martial_stewardship_modifier }
			}
			# Martial-Intrigue
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_intrigue }
				}
				add_character_modifier = fp2_double_synergy_martial_intrigue_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_martial_intrigue_modifier }
			}
			# Martial-Learning
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_learning }
				}
				add_character_modifier = fp2_double_synergy_martial_learning_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_martial_learning_modifier }
			}
		}
		# Stewardship combi-synergies.
		if = {
			limit = { has_trait = education_stewardship }
			# Stewardship-Diplomacy
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_diplomacy }
				}
				add_character_modifier = fp2_double_synergy_stewardship_diplomacy_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_stewardship_diplomacy_modifier }
			}
			# Stewardship-Martial
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_martial }
				}
				add_character_modifier = fp2_double_synergy_stewardship_martial_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_stewardship_martial_modifier }
			}
			# Stewardship-Stewardship
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_stewardship }
				}
				add_character_modifier = fp2_double_synergy_stewardship_stewardship_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_stewardship_stewardship_modifier }
			}
			# Stewardship-Intrigue
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_intrigue }
				}
				add_character_modifier = fp2_double_synergy_stewardship_intrigue_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_stewardship_intrigue_modifier }
			}
			# Stewardship-Learning
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_learning }
				}
				add_character_modifier = fp2_double_synergy_stewardship_learning_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_stewardship_learning_modifier }
			}
		}
		# Intrigue combi-synergies.
		if = {
			limit = { has_trait = education_intrigue }
			# Intrigue-Diplomacy
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_diplomacy }
				}
				add_character_modifier = fp2_double_synergy_intrigue_diplomacy_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_intrigue_diplomacy_modifier }
			}
			# Intrigue-Martial
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_martial }
				}
				add_character_modifier = fp2_double_synergy_intrigue_martial_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_intrigue_martial_modifier }
			}
			# Intrigue-Stewardship
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_stewardship }
				}
				add_character_modifier = fp2_double_synergy_intrigue_stewardship_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_intrigue_stewardship_modifier }
			}
			# Intrigue-Intrigue
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_intrigue }
				}
				add_character_modifier = fp2_double_synergy_intrigue_intrigue_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_intrigue_intrigue_modifier }
			}
			# Intrigue-Learning
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_learning }
				}
				add_character_modifier = fp2_double_synergy_intrigue_learning_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_intrigue_learning_modifier }
			}
		}
		# Learning combi-synergies.
		if = {
			limit = { has_trait = education_learning }
			# Learning-Diplomacy
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_diplomacy }
				}
				add_character_modifier = fp2_double_synergy_learning_diplomacy_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_learning_diplomacy_modifier }
			}
			# Learning-Martial
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_martial }
				}
				add_character_modifier = fp2_double_synergy_learning_martial_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_learning_martial_modifier }
			}
			# Learning-Stewardship
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_stewardship }
				}
				add_character_modifier = fp2_double_synergy_learning_stewardship_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_learning_stewardship_modifier }
			}
			# Learning-Intrigue
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_intrigue }
				}
				add_character_modifier = fp2_double_synergy_learning_intrigue_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_learning_intrigue_modifier }
			}
			# Learning-Learning
			if = {
				limit = {
					$JUNIOR_CHARACTER$ = { has_trait = education_learning }
				}
				add_character_modifier = fp2_double_synergy_learning_learning_modifier
				$JUNIOR_CHARACTER$ = { add_character_modifier = fp2_double_synergy_learning_learning_modifier }
			}
		}
	}
}

synergise_friendship_interaction_actual_effect = {
	if = {
		limit = { always = scope:synergy_bonus_actor }
		synergise_friendship_interaction_bonus_individual_effect = {
			SENIOR_CHARACTER = scope:actor
			JUNIOR_CHARACTER = scope:recipient
		}
	}
	if = {
		limit = { always = scope:synergy_bonus_recipient }
		synergise_friendship_interaction_bonus_individual_effect = {
			SENIOR_CHARACTER = scope:recipient
			JUNIOR_CHARACTER = scope:actor
		}
	}
	if = {
		limit = { always = scope:synergy_bonus_mixed }
		synergise_friendship_interaction_bonus_combined_effect = {
			SENIOR_CHARACTER = scope:actor
			JUNIOR_CHARACTER = scope:recipient
		}
	}
}

remove_every_best_friend_synergy_bonus_modifier_effect = {
	if = {
		limit = { has_character_modifier = fp2_single_synergy_diplomacy_modifier }
		remove_character_modifier = fp2_single_synergy_diplomacy_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_single_synergy_martial_modifier }
		remove_character_modifier = fp2_single_synergy_martial_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_single_synergy_stewardship_modifier }
		remove_character_modifier = fp2_single_synergy_stewardship_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_single_synergy_intrigue_modifier }
		remove_character_modifier = fp2_single_synergy_intrigue_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_single_synergy_learning_modifier }
		remove_character_modifier = fp2_single_synergy_learning_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_diplomacy_diplomacy_modifier }
		remove_character_modifier = fp2_double_synergy_diplomacy_diplomacy_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_diplomacy_martial_modifier }
		remove_character_modifier = fp2_double_synergy_diplomacy_martial_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_diplomacy_stewardship_modifier }
		remove_character_modifier = fp2_double_synergy_diplomacy_stewardship_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_diplomacy_intrigue_modifier }
		remove_character_modifier = fp2_double_synergy_diplomacy_intrigue_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_diplomacy_learning_modifier }
		remove_character_modifier = fp2_double_synergy_diplomacy_learning_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_martial_diplomacy_modifier }
		remove_character_modifier = fp2_double_synergy_martial_diplomacy_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_martial_martial_modifier }
		remove_character_modifier = fp2_double_synergy_martial_martial_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_martial_stewardship_modifier }
		remove_character_modifier = fp2_double_synergy_martial_stewardship_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_martial_intrigue_modifier }
		remove_character_modifier = fp2_double_synergy_martial_intrigue_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_martial_learning_modifier }
		remove_character_modifier = fp2_double_synergy_martial_learning_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_stewardship_diplomacy_modifier }
		remove_character_modifier = fp2_double_synergy_stewardship_diplomacy_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_stewardship_martial_modifier }
		remove_character_modifier = fp2_double_synergy_stewardship_martial_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_stewardship_stewardship_modifier }
		remove_character_modifier = fp2_double_synergy_stewardship_stewardship_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_stewardship_intrigue_modifier }
		remove_character_modifier = fp2_double_synergy_stewardship_intrigue_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_stewardship_learning_modifier }
		remove_character_modifier = fp2_double_synergy_stewardship_learning_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_intrigue_diplomacy_modifier }
		remove_character_modifier = fp2_double_synergy_intrigue_diplomacy_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_intrigue_martial_modifier }
		remove_character_modifier = fp2_double_synergy_intrigue_martial_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_intrigue_stewardship_modifier }
		remove_character_modifier = fp2_double_synergy_intrigue_stewardship_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_intrigue_intrigue_modifier }
		remove_character_modifier = fp2_double_synergy_intrigue_intrigue_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_intrigue_learning_modifier }
		remove_character_modifier = fp2_double_synergy_intrigue_learning_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_learning_diplomacy_modifier }
		remove_character_modifier = fp2_double_synergy_learning_diplomacy_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_learning_martial_modifier }
		remove_character_modifier = fp2_double_synergy_learning_martial_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_learning_stewardship_modifier }
		remove_character_modifier = fp2_double_synergy_learning_stewardship_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_learning_intrigue_modifier }
		remove_character_modifier = fp2_double_synergy_learning_intrigue_modifier
	}
	if = {
		limit = { has_character_modifier = fp2_double_synergy_learning_learning_modifier }
		remove_character_modifier = fp2_double_synergy_learning_learning_modifier
	}
}

purchase_truce_interaction_work_out_truce_days_effect = {
	if = {
		limit = { always = scope:pt_sum_small }
		add_truce_both_ways = {
			character = $TARGET$
			days = purchase_truce_interaction_truce_days_small_value
			name = TRUCE_PURCHASED
		}
	}
	else_if = {
		limit = { always = scope:pt_sum_large }
		add_truce_both_ways = {
			character = $TARGET$
			days = purchase_truce_interaction_truce_days_large_value
			name = TRUCE_PURCHASED
		}
	}
	else = {
		add_truce_both_ways = {
			character = $TARGET$
			days = purchase_truce_interaction_truce_days_medium_value
			name = TRUCE_PURCHASED
		}
	}
}

purchase_truce_interaction_work_out_purchase_cost_effect = {
	scope:actor = {
		# Small cost.
		if = {
			limit = { always = scope:pt_sum_small }
			pay_short_term_gold = {
				target = scope:recipient
				gold = {
					value = 0
					add = pt_sum_small_value
				}
			}
		}
		# Large cost.
		else_if = {
			limit = { always = scope:pt_sum_large }
			pay_short_term_gold = {
				target = scope:recipient
				gold = {
					value = 0
					add = pt_sum_large_value
				}
			}
		}
		# Medium cost; we do this last as a fallback.
		else = {
			pay_short_term_gold = {
				target = scope:recipient
				gold = {
					value = 0
					add = pt_sum_medium_value
				}
			}
		}
	}
}

purchase_truce_interaction_activate_catalyst_effect = {
	if = {
		limit = {
			any_character_struggle = {
				involvement = involved
				activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
					CATALYST = catalyst_bought_important_truce
					CHAR = scope:recipient
				}
			}
		}
		every_character_struggle = {
			involvement = involved
			limit = {
				activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
					CATALYST = catalyst_bought_important_truce
					CHAR = scope:recipient
				}
			}
			activate_struggle_catalyst = {
				catalyst = catalyst_bought_important_truce
				character = scope:actor
			}
		}
	}

	# Generic truce catalayst
	if = {
		limit = {
			any_character_struggle = {
				involvement = involved
				activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
					CATALYST = catalyst_sign_truce_outside_war
					CHAR = scope:recipient
				}
			}
		}
		every_character_struggle = {
			involvement = involved
			limit = {
				activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
					CATALYST = catalyst_sign_truce_outside_war
					CHAR = scope:recipient
				}
			}
			activate_struggle_catalyst = {
				catalyst = catalyst_sign_truce_outside_war
				character = scope:actor
			}
		}
	}
}

clean_revolt_county_modifiers_from_faction_members_effect = {
	every_faction_county_member = {
		if = {
			limit = { has_county_modifier = small_investment_in_revolt_modifier	}
			remove_county_modifier = small_investment_in_revolt_modifier
		}
		else_if = {
			limit = { has_county_modifier = medium_investment_in_revolt_modifier	}
			remove_county_modifier = medium_investment_in_revolt_modifier
		}
		else_if = {
			limit = { has_county_modifier = high_investment_in_revolt_modifier	}
			remove_county_modifier = high_investment_in_revolt_modifier
		}
	}
}

generate_troops_from_revolt_county_modifiers_effect = {
	if = {
		limit = {
			scope:attacker = {
				joined_faction = {
					any_faction_county_member = {
						OR = {
							has_county_modifier = small_investment_in_revolt_modifier
							has_county_modifier = medium_investment_in_revolt_modifier
							has_county_modifier = high_investment_in_revolt_modifier
						}
					}
				}
			}
		}
		scope:attacker = {
			joined_faction = {
				every_faction_county_member = {
					limit = {
						OR = {
							has_county_modifier = small_investment_in_revolt_modifier
							has_county_modifier = medium_investment_in_revolt_modifier
							has_county_modifier = high_investment_in_revolt_modifier
						}
					}

					random_county_province = {
						save_scope_as = chosen_location
					}
					scope:attacker = {

						set_variable = {
							name = number_rebel_reinforcements
							value = {
								add = {
									if = {
										limit = { prev = { has_county_modifier = small_investment_in_revolt_modifier } }
										add = 250
									}
									if = {
										limit = { prev = {  has_county_modifier = medium_investment_in_revolt_modifier	} }
										add = 750
									}
									if = {
										limit = { prev = {  has_county_modifier = high_investment_in_revolt_modifier	} }
										add = 2000
									}
								}
							}
						}

						spawn_army = {
							name = event_troop_default_name
							levies = var:number_rebel_reinforcements
							location = scope:chosen_location
							war = scope:war
						}
					}
				}

		clean_revolt_county_modifiers_from_faction_members_effect = yes

			}
		}

	}
}

fp2_contract_assistance_war_pay_effect = {
	if = {
		limit = {
			has_fp2_dlc_trigger = yes
			any_war_participant = {
				has_variable = owed_contract_assistance_war
				var:owed_contract_assistance_war = scope:war
			}
		}
		every_war_participant = {
			limit = {
				has_variable = owed_contract_assistance_war
				var:owed_contract_assistance_war = scope:war
			}
			remove_variable = owed_contract_assistance_war
			save_scope_as = contract_assistance_helper
			if = {
				limit = { is_attacker_in_war = scope:war }
				scope:war.primary_attacker = { save_scope_as = contract_assistance_hirer }
			}
			else = {
				scope:war.primary_defender = { save_scope_as = contract_assistance_hirer }
			}
			if = {
				limit = {
					scope:war = {
						war_contribution = {
							target = scope:contract_assistance_helper
							value >= scope:contract_assistance_helper.var:owed_contract_assistance_contribution
						}
					}
				}
				scope:contract_assistance_hirer = {
					send_interface_message = {
						type = event_gold_neutral
						title = contract_assistance_war_msg_title
						left_icon = scope:contract_assistance_helper
						custom_tooltip = contract_assistance_war_msg_hirer_paid_desc
						scope:contract_assistance_helper = {
							add_gold = scope:contract_assistance_helper.var:owed_contract_assistance_gold
						}
						#pay_short_term_gold = {
						#	target = scope:contract_assistance_helper
						#	gold = scope:contract_assistance_helper.var:owed_contract_assistance_gold
						#}
					}
				}
				scope:contract_assistance_helper = {
					send_interface_message = {
						type = event_gold_good
						title = contract_assistance_war_msg_title
						left_icon = scope:contract_assistance_hirer
						custom_tooltip = contract_assistance_war_msg_helper_paid_desc
						show_as_tooltip = {
							scope:contract_assistance_helper = {
								add_gold = scope:contract_assistance_helper.var:owed_contract_assistance_gold
							}
							#scope:contract_assistance_hirer = {
							#	pay_short_term_gold = {
							#		target = scope:contract_assistance_helper
							#		gold = scope:contract_assistance_helper.var:owed_contract_assistance_gold
							#	}
							#}
						}
					}
				}
			}
			else = {
				scope:contract_assistance_hirer = {
					send_interface_message = {
						type = event_gold_neutral
						title = contract_assistance_war_msg_title
						left_icon = scope:contract_assistance_helper
						custom_tooltip = contract_assistance_war_msg_hirer_kept_desc
					}
				}
				scope:contract_assistance_helper = {
					add_character_flag = {
						flag = fp2_contract_assistance_failure
						years = 10
					}
					send_interface_message = {
						type = event_gold_bad
						title = contract_assistance_war_msg_title
						left_icon = scope:contract_assistance_hirer
						custom_tooltip = contract_assistance_war_msg_helper_kept_desc
					}
				}
			}
			remove_variable = owed_contract_assistance_contribution
			remove_variable = owed_contract_assistance_gold
		}
	}
}

fp2_contract_assistance_invalidated_effect = {
	if = {
		limit = {
			has_fp2_dlc_trigger = yes
			any_war_participant = {
				has_variable = owed_contract_assistance_war
				var:owed_contract_assistance_war = scope:war
			}
		}
		every_war_participant = {
			limit = {
				has_variable = owed_contract_assistance_war
				var:owed_contract_assistance_war = scope:war
			}
			remove_variable = owed_contract_assistance_war
			remove_variable = owed_contract_assistance_contribution
			remove_variable = owed_contract_assistance_gold
			save_scope_as = contract_assistance_helper
			if = {
				limit = { is_attacker_in_war = scope:war }
				scope:war.primary_attacker = { save_scope_as = contract_assistance_hirer }
			}
			else = {
				scope:war.primary_defender = { save_scope_as = contract_assistance_hirer }
			}
			scope:contract_assistance_hirer = {
				send_interface_message = {
					type = event_gold_neutral
					title = contract_assistance_war_msg_title
					left_icon = scope:contract_assistance_helper
					custom_tooltip = contract_assistance_war_msg_hirer_invalidated_desc
				}
			}
			scope:contract_assistance_helper = {
				send_interface_message = {
					type = event_gold_neutral
					title = contract_assistance_war_msg_title					
					left_icon = scope:contract_assistance_hirer
					custom_tooltip = contract_assistance_war_msg_helper_invalidated_desc
				}
			}
		}
	}
}
		
invite_special_guest_to_activity_effect = {
	if = {
		limit = {
			exists = var:special_guest
		}

		var:special_guest = {
			save_scope_as = the_special_guest
		}

		save_scope_as = sender

		scope:the_special_guest = {
			add_to_activity = scope:activity

			set_variable = {
				name = booked_for_a_party
				value = scope:sender
				days = 20
			}
		}
		
		remove_variable = special_guest
	}
}

##################################################
# Struggle
##################################################

fp2_struggle_apply_ai_agenda_effect = {
	# Apply effects
	add_character_flag = {
		flag = agenda_towards_$INTENT$
	}
	root = {
		change_variable = {
			name = agents_for_$INTENT$
			add = 1
		}
	}
}

gifting_leads_towards_friendship_effect = {
	if = {
		# then verify the right struggle phase
		limit = {
			is_diff_faith_or_culture_trigger = {
				CHAR = scope:actor
				STATUS = involved
			}
			any_character_struggle = {
				involvement = involved
				has_struggle_phase_parameter = struggle_gifts_involved_diff_faith_culture_leads_to_friendship
			}
		}
		progress_towards_friend_effect = {
			REASON = friend_struggle_gift
			CHARACTER = scope:actor
			OPINION = 0
		}
	}
}

##################################################
# CBs
##################################################



##################################################
# Yearlies
##################################################

fp2_lyonese_monk_return_territories_effect = {
	if = {
		limit = {
			scope:acolyte = { is_playable_character = yes }
		}
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = change
		}
		scope:acolyte = {
			every_held_title = {
				limit = { tier >= tier_county }
				change_title_holder = {
					holder = scope:acolyte_host
					change = scope:change
				}
			}
			resolve_title_and_vassal_change = scope:change
		}
	}
}

fp2_lyonese_monk_cancel_chain_effect = {
	# Tidy up the story.
	scope:story = { end_story = yes }
	# Tidy up scope:acolyte.
	## We do this via some mild scope wizardry.
	save_scope_value_as = {
		name = remove_acolyte
		value = flag:$REMOVAL$
	}
	## Either they sod off.
	if = {
		limit = { scope:remove_acolyte = flag:banished }
		fp2_lyonese_monk_return_territories_effect = yes
		scope:acolyte = {
			move_to_pool = yes
			silent_disappearance_effect = yes
		}
	}
	## Or they're killed during apprehension.
	else_if = {
		limit = { scope:remove_acolyte = flag:scuffle }
		fp2_lyonese_monk_return_territories_effect = yes
		scope:acolyte = {
			custom_tooltip = fp2_lyonese_monk_cancel_chain_effect.tt
			hidden_effect = {
				death = {
					death_reason = death_scuffle_with_soldiers
					killer = scope:acolyte_host
				}
			}
		}
	}
	## Or else they're given to the Pope.
	else_if = {
		limit = { scope:remove_acolyte = flag:pope }
		fp2_lyonese_monk_return_territories_effect = yes
		# So we walk through the narrative...
		scope:acolyte = {
			if = {
				limit = { is_playable_character = no }
				select_and_move_to_pool_effect = yes
			}
		}
		rightfully_imprison_character_effect = {
			TARGET = scope:acolyte
			IMPRISONER = scope:pope
		}
		scope:acolyte = {
			hidden_effect = {
				death = {
					death_reason = death_dungeon
					killer = scope:pope
				}
			}
		}
	}
}

##################################################
# Legacies
##################################################



##################################################
# Governments
##################################################



##################################################
# Casus Belli
##################################################

fp2_border_raid_damage_effect = {
	change_county_control = fp2_border_raid_control_change_value
	change_development_level = fp2_border_raid_development_change_value
	if = {
		limit = { exists = scope:raid_province }
		show_as_tooltip = { destroy_random_building_effect = yes }
	}
	scope:attacker = { add_gold = fp2_border_raid_gold_change_value }
}

##################################################
# Historical Artefacts
##################################################

create_artifact_goblet_chalice_of_dona_urraca = {
	$OWNER$ = { save_scope_as = owner }
	set_artifact_rarity_famed = yes
	scope:owner = {
		create_artifact = {
			name = fp2_chalice_dona_urraca
			description = fp2_chalice_dona_urraca_desc
			template = fp2_chalice_dona_urraca_template
			type = miscellaneous_when_not_court
			visuals = fp2_chalice_dona_urraca
			wealth = scope:wealth
			quality = scope:quality
			modifier = chalice_of_dona_urraca_modifier
			save_scope_as = newly_created_artifact
		}
	}
	scope:newly_created_artifact = {
		set_variable = {
			name = dlc_potential_court_artefact
			value = yes
		}
		set_variable = {
			name = historical_unique_artifact
			value = yes
		}
		set_variable = {
			name = relic
			value = flag:christian
		}
	}
}

create_artifact_various_aquamanile_santiago = {
	$OWNER$ = { save_scope_as = owner }
	character:3206 = { save_scope_as = reforger }
	set_artifact_rarity_illustrious = yes
	scope:owner = {
		create_artifact = {
			name = fp2_santiago_aquamanile
			description = fp2_santiago_aquamanile_desc
			template = fp2_santiago_aquamanile_template
			type = miscellaneous_when_not_court
			visuals = fp2_santiago_aquamanile
			wealth = scope:wealth
			quality = scope:quality
			modifier = former_bells_of_santiago_modifier
			decaying = no
			save_scope_as = newly_created_artifact
		}
	}
	scope:newly_created_artifact = {
		set_variable = {
			name = dlc_potential_court_artefact
			value = yes
		}
		set_variable = {
			name = historical_unique_artifact
			value = yes
		}
		set_variable = {
			name = relic
			value = flag:iberian_struggle
		}
	}
}

create_artifact_various_bells_santiago = {
	$OWNER$ = { save_scope_as = owner }
	set_artifact_rarity_illustrious = yes
	scope:owner = {
		create_artifact = {
			name = fp2_santiago_bells
			description = fp2_santiago_bells_desc
			template = fp2_santiago_bells_template
			type = miscellaneous_when_not_court
			visuals = fp2_santiago_bells
			wealth = scope:wealth
			quality = scope:quality
			modifier = bells_of_santiago_modifier
			decaying = no
			save_scope_as = newly_created_artifact
		}
	}
	scope:newly_created_artifact = {
		set_variable = {
			name = dlc_potential_court_artefact
			value = yes
		}
		set_variable = {
			name = historical_unique_artifact
			value = yes
		}
		set_variable = {
			name = relic
			value = flag:iberian_struggle
		}
	}
}

create_artifact_fp2_votive_crowns_effect = {
	$OWNER$ = { save_scope_as = owner }
	set_artifact_rarity_famed = yes
	scope:owner = {
		create_artifact = {
			name = fp2_votive_crowns
			description = fp2_votive_crowns_desc
			template = crown_christian_relic_template
			type = regalia
			visuals = fp2_votive_crowns
			wealth = scope:wealth
			quality = scope:quality
			modifier = votive_crown_modifier
			save_scope_as = newly_created_artifact
		}
	}
	scope:newly_created_artifact = {
		set_variable = {
			name = dlc_potential_court_artefact
			value = yes
		}
	}
}

##################################################
# Other Artefacts
##################################################

create_artifact_fp2_armillary_sphere_core_effect = {
	create_artifact = {	
		name = fp2_armillary_sphere
		# Description will be regenerated in the post-creation effects, since we need to nab materials.
		description = placeholder 
		creator = scope:maker
		type = $TYPE$
		visuals = fp2_armillary_sphere
		wealth = scope:wealth
		quality = scope:quality
		modifier = artifact_placeholder_modifier
		save_scope_as = newly_created_artifact
	}
}

create_artifact_fp2_armillary_sphere_effect = {
	# Set up general scopes.
	$OWNER$ = { save_scope_as = owner }
	$MAKER$ = { save_scope_as = maker }
	# Begin creating.
	hidden_effect_new_object = {
		# Get artifact quality, wealth, and materials.
		get_artifact_quality_effect = yes
		get_artifact_wealth_effect = yes
		# Create the artifact.
		scope:owner = {
			if = {
				limit = { has_royal_court = yes }
				create_artifact_fp2_armillary_sphere_core_effect = { TYPE = scientific_apparatus }
			}
			else = {
				create_artifact_fp2_armillary_sphere_core_effect = { TYPE = miscellaneous_when_not_court }
			}
		}
		scope:newly_created_artifact = {
			# Flag this as something that can be reforged for free, if relevant.
			if = {
				limit = {
					$OWNER$ = { has_royal_court = no }
				}
				set_variable = {
					name = dlc_potential_court_artefact
					value = yes
				}
			}
			set_artifact_feature_group = generic_material_wood
			set_artifact_feature_group = decoration_material_inlay
			set_artifact_description = fp2_armillary_sphere_desc
			# Let's add modifiers!
			add_scaled_artifact_modifier_learning_lifestyle_xp_effect = yes
			if = {
				limit = {
					OR = {
						rarity = famed
						rarity = illustrious
					}
				}
				add_artifact_modifier = artifact_learning_per_prestige_level_modifier
			}
			add_scaled_artifact_modifier_grandeur_big_effect = yes
			# Remove the placeholder.
			remove_artifact_modifier = artifact_placeholder_modifier
		}
	}
}

create_artifact_fp2_2p_chess_board_effect = {
	$OWNER$ = { save_scope_as = owner }
	set_artifact_rarity_common = yes
	create_artifact = {
		name = fp2_2p_chess_board
		description = fp2_2p_chess_board_desc
		type = miscellaneous_when_not_court
		visuals = fp2_2p_chess_board
		wealth = scope:wealth
		quality = scope:quality
		modifier = artifact_stress_gain_2_modifier
		save_scope_as = newly_created_artifact
	}
	scope:newly_created_artifact = {
		# Flag this as something that can be reforged for free.
		set_variable = {
			name = dlc_potential_court_artefact
			value = yes
		}
		# And used in board games.
		set_variable = {
			name = can_be_used_for_board_games
			value = yes
		}
		hidden_effect = { add_artifact_modifier = artifact_befriend_scheme_phase_duration_add_2_modifier }
	}
}

create_artifact_fp2_4p_chess_board_effect = {
	$OWNER$ = { save_scope_as = owner }
	set_artifact_rarity_masterwork = yes
	create_artifact = {
		name = fp2_4p_chess_board
		description = fp2_4p_chess_board_desc
		type = miscellaneous_when_not_court
		visuals = fp2_4p_chess_board
		wealth = scope:wealth
		quality = scope:quality
		modifier = artifact_stress_gain_4_modifier
		save_scope_as = newly_created_artifact
	}
	scope:newly_created_artifact = {
		# Flag this as something that can be reforged for free.
		set_variable = {
			name = dlc_potential_court_artefact
			value = yes
		}
		# And used in board games.
		set_variable = {
			name = can_be_used_for_board_games
			value = yes
		}
		hidden_effect = { add_artifact_modifier = artifact_befriend_scheme_phase_duration_add_4_modifier }
	}
}

create_artifact_fp2_andalusian_aquamanile_core_effect = {
	create_artifact = {
		name = fp2_andalusian_aquamanile
		# Description will be regenerated in the post-creation effects, since we need to nab materials.
		description = placeholder 
		type = $TYPE$
		visuals = fp2_andalusian_aquamanile
		wealth = scope:wealth
		quality = scope:quality
		modifier = artifact_negate_health_penalty_add_2_modifier
		save_scope_as = newly_created_artifact
	}
}

create_artifact_fp2_andalusian_aquamanile_effect = {
	# Set up general scopes.
	$OWNER$ = { save_scope_as = owner }
	$SMITH$ = { save_scope_as = maker }
	# Begin creating.
	hidden_effect_new_object = {
		# Get artifact quality, wealth, and materials.
		get_artifact_quality_effect = yes
		get_artifact_wealth_effect = yes
		# Create the artifact.
		scope:owner = {
			if = {
				limit = { has_royal_court = yes }
				create_artifact_fp2_andalusian_aquamanile_core_effect = { TYPE = jug }
			}
			else = {
				create_artifact_fp2_andalusian_aquamanile_core_effect = { TYPE = miscellaneous_when_not_court }
			}
		}
		scope:newly_created_artifact = {
			# Flag this as something that can be reforged for free, if relevant.
			if = {
				limit = {
					$OWNER$ = { has_royal_court = no }
				}
				set_variable = {
					name = dlc_potential_court_artefact
					value = yes
				}
			}
			set_artifact_feature_group = goblet_material
			set_artifact_feature_group = decoration_material_inlay
			set_artifact_description = fp2_andalusian_aquamanile_desc
			# Let's add modifiers!
			## Illustrious gets to be very swanky.
			if = {
				limit = { rarity = illustrious }
				add_2_scaled_artifact_modifier_devotion_effect = yes
				add_scaled_artifact_modifier_grandeur_small_effect = yes
			}
			## Common is a bit meh.
			else_if = {
				limit = { rarity = common }
				add_scaled_artifact_modifier_devotion_effect = yes
			}
			## Everything else is decent.
			else = { add_2_scaled_artifact_modifier_devotion_effect = yes }
		}
	}
}

fp2_enforce_truce_opinion_effect = {
	if = {
		limit = { scope:target.attacker_war_score >= fp2_enforce_truce_war_score_value }
		reverse_add_opinion = {
			target = scope:target.primary_attacker
			modifier = fp2_enforced_truce_opinion
			opinion = -20
		}
		reverse_add_opinion = {
			target = scope:target.primary_defender
			modifier = fp2_enforced_truce_opinion
			opinion = 20
		}
	}
	else_if = {
		limit = { scope:target.defender_war_score >= fp2_enforce_truce_war_score_value }
		reverse_add_opinion = {
			target = scope:target.primary_defender
			modifier = fp2_enforced_truce_opinion
			opinion = -20
		}
		reverse_add_opinion = {
			target = scope:target.primary_attacker
			modifier = fp2_enforced_truce_opinion
			opinion = 20
		}
	}
	else = {
		reverse_add_opinion = {
			target = scope:target.primary_defender
			modifier = fp2_enforced_truce_opinion
			opinion = -10
		}
		reverse_add_opinion = {
			target = scope:target.primary_attacker
			modifier = fp2_enforced_truce_opinion
			opinion = -10
		}
	}
}

##############################################################
# Generic Scripted Effect: Get Lowest/Highest Player Skill
# By Hugo Cortell
################################ 

# (simplified for readability)

get_lowest_player_skill_effect = {
	save_scope_value_as = {
		name = lowest_player_skill
		value = flag:diplomacy
	}
	save_scope_value_as = {
		name = lowest_player_skill_value
		value = diplomacy
	}
	if = {
		limit = { martial < scope:lowest_player_skill_value }
		save_scope_value_as = {
			name = lowest_player_skill
			value = flag:martial
		}
		save_scope_value_as = {
			name = lowest_player_skill_value
			value = martial
		}
	}
	if = {
		limit = { stewardship < scope:lowest_player_skill_value }
		save_scope_value_as = {
			name = lowest_player_skill
			value = flag:stewardship
		}
		save_scope_value_as = {
			name = lowest_player_skill_value
			value = stewardship
		}
	}
	if = {
		limit = { intrigue < scope:lowest_player_skill_value }
		save_scope_value_as = {
			name = lowest_player_skill
			value = flag:intrigue
		}
		save_scope_value_as = {
			name = lowest_player_skill_value
			value = intrigue
		}
	}
	if = {
		limit = { learning < scope:lowest_player_skill_value }
		save_scope_value_as = {
			name = lowest_player_skill
			value = flag:learning
		}
		save_scope_value_as = {
			name = lowest_player_skill_value
			value = learning
		}
	}
}

get_highest_player_skill_effect = {
	save_scope_value_as = {
		name = highest_player_skill
		value = flag:diplomacy
	}
	save_scope_value_as = {
		name = highest_player_skill_value
		value = diplomacy
	}
	if = {
		limit = { martial > scope:highest_player_skill_value }
		save_scope_value_as = {
			name = highest_player_skill
			value = flag:martial
		}
		save_scope_value_as = {
			name = highest_player_skill_value
			value = martial
		}
	}
	if = {
		limit = { stewardship > scope:highest_player_skill_value }
		save_scope_value_as = {
			name = highest_player_skill
			value = flag:stewardship
		}
		save_scope_value_as = {
			name = highest_player_skill_value
			value = stewardship
		}
	}
	if = {
		limit = { intrigue > scope:highest_player_skill_value }
		save_scope_value_as = {
			name = highest_player_skill
			value = flag:intrigue
		}
		save_scope_value_as = {
			name = highest_player_skill_value
			value = intrigue
		}
	}
	if = {
		limit = { learning > scope:highest_player_skill_value }
		save_scope_value_as = {
			name = highest_player_skill
			value = flag:learning
		}
		save_scope_value_as = {
			name = highest_player_skill_value
			value = learning
		}
	}
}

fp2_struggle_found_kingdom_toledo_empire_effect = {
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	title:k_toledo = {
		change_title_holder = {
			holder = root
			change = scope:title_change
		}
	}
	resolve_title_and_vassal_change = scope:title_change
	title:d_toledo = { add_to_list = kingdom_toledo_absorb_list }
	if = {
		limit = { NOT = { exists = scope:has_kingdom_toledo_absorb_list_been_created } }
		ordered_held_title = {
			limit = {
				tier = tier_duchy
				is_titular = no
				NOT = { this = title:d_toledo }
				root = { completely_controls = prev }
				any_title_to_title_neighboring_and_across_water_duchy = { this = title:d_toledo }
				de_jure_liege = {
					any_in_de_jure_hierarchy = {
						count > 2
						tier = tier_duchy
					}
				}
			}
			order_by = {
				value = 0
				every_de_jure_county = {
					limit = { any_neighboring_county = { de_jure_liege = title:d_toledo } }
					add = 1
				}
			}
			add_to_list = kingdom_toledo_absorb_list
		}
	}
	every_in_list = {
		list = kingdom_toledo_absorb_list
		if = {
			limit = {
				exists = de_jure_liege.holder
				NOT = { de_jure_liege.holder = root }
			}
			de_jure_liege.holder = { add_to_list = kingdom_toledo_opinion_list }
		}
		set_de_jure_liege_title = title:k_toledo
	}
	hidden_effect = { title:k_toledo = { set_de_jure_liege_title = title:e_spain } }
	every_in_list = {
		list = kingdom_toledo_opinion_list
		limit = { is_ai = yes }
		add_opinion = {
			target = root
			modifier = absorbed_de_jure_opinion
		}
	}
	#Prestige
	add_prestige = massive_prestige_gain
	add_character_modifier = {
		modifier = fp2_form_kingdom_toledo_modifier
		years = 10
	}
}

fp2_struggle_hostility_ender_effect = {
	give_nickname = nick_the_conquistador
}

fp2_struggle_compromise_ender_effect = {
	give_nickname = nick_the_pragmatic
	dynasty = { add_dynasty_prestige = 10000 }
}

fp2_struggle_compromise_tooltip_effect = {
	# De Jure Kingdoms will become De Jure Empires
	custom_tooltip = struggle_compromise_duchy_transfer_tt
	custom_tooltip = fp2_struggle_compromise_create_new_empires_tt
	# RIP Hispania
	custom_tooltip = fp2_struggle_compromise_destroy_hispania_tt
	if = {
		limit = {
			struggle:iberian_struggle = {
				any_involved_ruler = {
					count > 1
					is_independent_ruler = yes
					primary_title = { is_mercenary_company = no }
				}
			}
			# Every Involved ruler gets a 10 year truce with every other
			custom_tooltip = fp2_struggle_compromise_truce_tt
		}
	}
	custom_tooltip = fp2_struggle_compromise_war_cost_tt
}

fp2_struggle_conciliation_ender_effect = {
	give_nickname = nick_the_equitable
	dynasty = { add_dynasty_prestige = 10000 }
}

fp2_struggle_conciliation_tooltip_effect = {
	# Hispania is available!
	custom_tooltip = fp2_struggle_can_create_empire_of_hispania_tt
	# All Involved cultures gain acceptance of each other
	custom_tooltip = fp2_struggle_conciliation_culture_tt
	# Interfaith marriage unlocked for Involved cultures
	custom_tooltip = fp2_struggle_conciliation_marriage_unlocked_tt
	# Declaring holy wars is perma-disabled for locals in the region
	custom_tooltip = fp2_struggle_conciliation_holy_war_disabled_tt 
	# Involved cultures/faiths can offer to join defensive wars with outsiders targetting Iberia
	custom_tooltip = fp2_struggle_conciliation_join_war_tt
	# Caravanserai line of buildings unlocked for Iberia
	custom_tooltip = fp2_struggle_conciliation_caravanserai_unlock_tt
}
