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

secure_high_kingdom_north_sea_scripted_effect = {
	scope:founder = {
		#Standard effects.
		##Adjust the de jure capital appropriately first up, since otherwise we won't be able to see your preferred title.
		hidden_effect = {
			#If it's Denmark, we do nothing, as Denmark has the capital & CoA by default.
			#If it's England, we make the capital London/York.
			if = {
				limit = { primary_title = title:k_england }
				title:e_north_sea = {
					set_capital_county = title:k_england.title_capital_county
				}
			}
			#If it's Norway, we make the capital Steinkjer.
			if = {
				limit = { primary_title = title:k_norway }
				title:e_north_sea = {
					set_capital_county = title:c_trandheim.title_capital_county
				}
			}
		}
		##Hand over the High Kingdom.
		create_title_and_vassal_change = {
			type = created
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		title:e_north_sea = {
			change_title_holder = {
				holder = scope:founder
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
		##Configure the new de jure setup.
		title:k_england = { set_de_jure_liege_title = title:e_north_sea }
		title:k_denmark = { set_de_jure_liege_title = title:e_north_sea }
		title:k_norway = { set_de_jure_liege_title = title:e_north_sea }
		##Rename rump-Britannia, as it hardly covers the whole island any more.
		title:e_britannia = { set_title_name = e_albia }
		## Completely controlled duchies of k_daneland are transferred back to k_england.
		if = {
			limit = {
				title:k_daneland = {
					any_in_de_jure_hierarchy = {
						tier = tier_duchy
						save_temporary_scope_as = current_duchy
						scope:founder = { completely_controls = scope:current_duchy }
					}
				}
			}
			custom_tooltip = secure_high_kingdom_north_sea_scripted_effect.daneland_reabsorbed.tt
			hidden_effect = {
				title:k_daneland = {
					every_in_de_jure_hierarchy = {
						limit = {
							tier = tier_duchy
							save_temporary_scope_as = current_duchy
							scope:founder = { completely_controls = scope:current_duchy }
						}
						set_de_jure_liege_title = title:k_england
					}
				}
			}
		}

		#Variable effects.
		##Unreformed tribals.
		if = {
			limit = {
				government_has_flag = government_is_tribal
				faith = { has_doctrine_parameter = unreformed }
			}
			#First, set a flag for achievements.
			add_achievement_flag_effect = { FLAG = canute_the_greater_achievement_flag }
			#You become feudal.
			convert_whole_realm_to_feudalism_effect = yes
			#To keep you stable, we give you a nice permanent modifier.
			if = {
				limit = { is_male = yes }
				add_character_modifier = { modifier = high_king_of_the_seas_fp1_modifier }
			}
			else = {
				add_character_modifier = { modifier = high_queen_of_the_seas_fp1_modifier }
			}
			#And a historically-appropriate nickname.
			give_nickname = nick_the_sea_king
		}
		##Reformed pagans.
		else_if = {
			limit = {
				faith.religion = { is_in_family = rf_pagan }
				NOT = {
					faith = { has_doctrine_parameter = unreformed }
				}
			}
			#We give you a nice hefty conversion boost against other reformed faiths for twenty years.
			if = {
				limit = { is_male = yes }
				add_character_modifier = {
					modifier = defiant_high_king_conversion_boost_modifier
					years = 20
				}
			}
			else = {
				add_character_modifier = {
					modifier = defiant_high_queen_conversion_boost_modifier
					years = 20
				}
			}
			#And an appropriate nickname.
			give_nickname = nick_the_defiant
		}
		##Everyone else (e.g., Christians, unreformed feudals, etc).
		else = {
			#Loyalty hooks & huge opinion boost from all agreeable vassals.
			every_vassal = {
				limit = {
					is_powerful_vassal = yes
					opinion = {
						target = scope:founder
						value >= 1
					}
				}
				if = {
					limit = {
						scope:founder = { is_male = yes }
					}
					add_opinion = {
						modifier = king_of_the_north_sea_opinion
						target = scope:founder
					}
				}
				else = {
					add_opinion = {
						modifier = queen_of_the_north_sea_opinion
						target = scope:founder
					}
				}
				save_temporary_scope_as = current_vassal
				scope:founder = {
					add_hook = {
						type = loyalty_hook
						target = scope:current_vassal
					}
				}
			}
			#And a suitable nickname.
			give_nickname = nick_the_great
		}
	}
}

found_capital_of_rus_scripted_effect = {
	#Reward fame to compensate for reduced development gain.
	scope:new_rus_capital.holder = {
		add_prestige_experience = major_prestige_gain
	}
	scope:new_rus_capital = {
		#Boost local development a few levels; majority of development gain is handled in the modifiers now.
		if = {
			limit = { development_level < 5 }
			change_development_level = {
				value = 5
				subtract = scope:new_rus_capital.development_level
			}
		}
		else_if = {
			limit = { development_level < 8 }
			change_development_level = {
				value = 8
				subtract = scope:new_rus_capital.development_level
			}
		}
		else_if = {
			limit = { development_level < 12 }
			change_development_level = {
				value = 12
				subtract = scope:new_rus_capital.development_level
			}
		}
		#If control is less than full, boost it to full.
		if = {
			limit = { county_control < full_county_control }
			change_county_control = 100
		}
		#Add county modifier giving solid opinion for a decent chunk of time.
		add_county_modifier = {
			modifier = capital_of_rus_opinion_modifier
			years = 20
		}
		#Add county modifier giving intense benefits for a century.
		add_county_modifier = {
			modifier = capital_of_rus_misc_benefits_modifier
			years = 100
		}
		#If this county isn't the de jure capital of e_russia, make it so.
		if = {
			limit = {
				NOT = { this = title:e_russia.title_capital_county }
			}
			title:e_russia = { set_capital_county = scope:new_rus_capital }
		}
	}
}

fp1_apply_generic_stele_bonuses_effect = {
	add_piety = major_piety_gain
	add_prestige = major_prestige_gain
	add_legitimacy = medium_legitimacy_gain
	every_vassal = {
		limit = { fp1_can_raise_stele_trigger = yes }
		custom = fp1_raise_runestone_vassals
		add_opinion = {
			modifier = pleased_opinion
			target = root
			opinion = 10
		}
	}
	# Saga in Stone achievement tracking.
	if = {
		limit = {
			# Grab the player and any dynasty members of theirs.
			any_player = { dynasty = root.dynasty }
		}
		dynasty = {
			# Start a count if we don't have one.
			if = {
				limit = {
					NOT = { exists = var:saga_in_stone_achievement_tally }
				}
				set_variable = {
					name = saga_in_stone_achievement_tally
					value = 1
				}
			}
			# Otherwise, increment it.
			else = {
				change_variable = {
					name = saga_in_stone_achievement_tally
					add = 1
				}
			}
		}
	}
}

elevate_mann_and_the_isles_scripted_effect = {
	# You gain the Legacy of Piracy dynasty modifier.
	dynasty = {
		add_dynasty_modifier = {
			modifier = fp1_legacy_of_piracy_modifier
			years = 100
		}
		# Also gain a large amount of dynasty prestige.
		add_dynasty_prestige = excessive_dynasty_prestige_gain
	}
	# Cool nickname distributed elsewhere.
	# Mann becomes a kingdom.
	title:d_the_isles = { set_de_jure_liege_title = title:k_mann_the_isles }
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
	}
	title:k_mann_the_isles = {
		change_title_holder = {
			holder = scope:founder
			change = scope:title_change
		}
	}
	resolve_title_and_vassal_change = scope:title_change
	# Buff the Isle of Mann.
	title:c_isle_of_man = {
		# Give it some extra building slots
		title_province = { 
			add_province_modifier = elevate_mann_and_the_isles_modifier
		}
		# And a decent development boost.
		change_development_level = medium_development_level_gain
	}
	# Make it your capital if it wasn't already.
	if = {
		limit = {
			NOT = { capital_county = title:c_isle_of_man }
		}
		set_realm_capital = title:c_isle_of_man
	}
	# And convert it to your culture if it wasn't some flavour of North Germanic.
	if = {
		limit = {
			NOR = {
				title:c_isle_of_man.culture = {
					has_cultural_pillar = heritage_north_germanic
				}
				title:c_isle_of_man.culture = culture:norman
			}
		}
		title:c_isle_of_man = { set_county_culture = scope:founder.culture }
	}
	# Have a huge, terrifying event army.
	spawn_army = {
		levies = 5000
		men_at_arms = {
			type = jomsviking_pirates
			stacks = 5
		}
		men_at_arms = {
			type = jomsviking_pirates
			stacks = 5
		}
		men_at_arms = {
			type = varangian_veterans
			stacks = 5
		}
		men_at_arms = {
			type = bondi
			stacks = 5
		}
		men_at_arms = {
			type = bondi
			stacks = 5
		}
		location = title:c_isle_of_man.title_province
		name = fp1_mann_event_troops
	}
	# Mann gets a huge modernising boost.
	title:c_isle_of_man = {
		add_county_modifier = {
			modifier = fp1_mann_pirate_capital_modifier
			years = 100
		}
	}
	# If completely controlled, you integrate the duchies of the Western Isles, the Northern Isles, and Strathclyde.
	## The Western Isles.
	if = {
		limit = { completely_controls = title:d_northern_isles }
		title:d_northern_isles = { set_de_jure_liege_title = title:k_mann_the_isles }
	}
	## The Northern Isles.
	if = {
		limit = { completely_controls = title:d_western_isles }
		title:d_western_isles = { set_de_jure_liege_title = title:k_mann_the_isles }
	}
	## Strathclyde.
	if = {
		limit = { completely_controls = title:d_galloway }
		title:d_galloway = { set_de_jure_liege_title = title:k_mann_the_isles }
	}
	# Plus, if you weren't already, become feudal.
	if = {
		limit = { government_has_flag = government_is_tribal }
		convert_whole_realm_to_feudalism_effect = yes
	}
}

fp1_remove_stele_effect = {
	if = {
		limit = { has_county_modifier = fp1_stele_ancestors_strong }
		remove_county_modifier = fp1_stele_ancestors_strong
	}
	if = {
		limit = { has_county_modifier = fp1_stele_ancestors_weak }
		remove_county_modifier = fp1_stele_ancestors_weak
	}
	if = {
		limit = { has_county_modifier = fp1_stele_conquest_strong }
		remove_county_modifier = fp1_stele_conquest_strong
	}
	if = {
		limit = { has_county_modifier = fp1_stele_conquest_weak }
		remove_county_modifier = fp1_stele_conquest_weak
	}
	if = {
		limit = { has_county_modifier = fp1_stele_promotion_strong }
		remove_county_modifier = fp1_stele_promotion_strong
	}
	if = {
		limit = { has_county_modifier = fp1_stele_promotion_weak }
		remove_county_modifier = fp1_stele_promotion_weak
	}
}

forge_the_jomsvikings_scripted_effect = {
	# Display simplified results.
	custom_tooltip = create_holy_order_decision_effect_message
	# Select a random county.
	random_held_title = {
		limit = { fp1_valid_county_for_jomsvikings_trigger = yes }
		save_scope_as = designated_county
	}
	# Select an appropriate barony.
	## If there's an empty space in the capital, we create a castle.
	if = {
		limit = {
			scope:designated_county = {
				any_county_province = { has_holding = no }
			}
		}
		scope:designated_county = {
			random_county_province = {
				limit = { has_holding = no }
				set_holding_type = castle_holding
				barony = { save_scope_as = leased_barony }
			}
		}
	}
	## Otherwise, grab an existing non-capital castle/city.
	else = {
		scope:designated_county = {
			random_county_province = {
				limit = {
					is_county_capital = no
					OR = {
						has_holding_type = castle_holding
						has_holding_type = city_holding
					}
				}
				barony = { save_scope_as = leased_barony }
			}
		}
	}
	# Create a suitable leader for the Jomsvikings.
	create_character = {
		template = fp1_jomsviking_bigwig
		location = scope:leased_barony.title_province
		save_scope_as = leader
	}
	# Create the actual holy order.
	create_holy_order_neutral_effect = {
		LEADER = scope:leader
		CAPITAL = scope:leased_barony
		NEW_HO_SCOPE = new_holy_order
		FOUNDER = scope:founder
	}
	# Make the local settlement Jomsberg.
	## Rename it.
	scope:leased_barony = { set_title_name = b_jomsberg }
	## Give it some buildings.
	hidden_effect = {
		scope:leased_barony.title_province = {
			# Walls & Towers.
			if = {
				limit = {
					NOT = { has_building_or_higher = curtain_walls_01 }
				}
				# If it doesn't have a free building slot, give it one.
				if = {
					limit = { free_building_slots = 0 }
					add_province_modifier = extra_building_slot
				}
				# Add the building.
				add_building = curtain_walls_01
			}
			# Barracks.
			if = {
				limit = {
					NOT = { has_building_or_higher = barracks_01 }
				}
				# If it doesn't have a free building slot, give it one.
				if = {
					limit = { free_building_slots = 0 }
					add_province_modifier = extra_building_slot
				}
				# Add the building.
				add_building = barracks_01
			}
			# Military Camps.
			if = {
				limit = {
					NOT = { has_building_or_higher = military_camps_01 }
				}
				# If it doesn't have a free building slot, give it one.
				if = {
					limit = { free_building_slots = 0 }
					add_province_modifier = extra_building_slot
				}
				# Add the building.
				add_building = military_camps_01
			}
		}
	}
	## Mark it so it can be renamed if the order is ever destroyed.
	set_global_variable = {
		name = jomsberg_location
		value = scope:leased_barony
	}
	# And the Grandmaster of Jomsberg certainly looks favourably on their patron.
	## Hidden because the title doesn't play nicely with characters created in the same immediate block.
	hidden_effect = {
		scope:founder = { set_relation_friend = { reason = friend_forging_jomsvikings target = scope:leader } }
	}
	# Mark the Jomsvikings as existing & make them easily accessible
	set_global_variable = {
		name = jomsvikings_title
		value = scope:leader.primary_title
	}
	# Some minor misc effects.
	create_holy_order_effect = yes
	scope:leader.primary_title = { set_coa = ho_jomsvikings }
	# Flag the Jomsvikings as forged.
	hidden_effect = {
		add_to_global_variable_list = {
			name = unavailable_unique_decisions
			target = flag:flag_forged_jomvikings
		}
	}
}

forge_the_jomsvikings_maa_reward_effect = {
	spawn_army = {
		levies = { add = 500 }
		men_at_arms = {
			type = jomsviking_pirates
			stacks = 4
		}
		men_at_arms = {
			type = varangian_veterans
			stacks = 2
		}
		men_at_arms = {
			type = bondi
			stacks = 4
		}
		inheritable = yes
		uses_supply = yes
		location = scope:founder.capital_province
		name = fp1_jomsvikings_event_troops
	}
}

forge_the_jomsvikings_decision_tooltip_effect = {
	# Standard info.
	custom_tooltip = create_holy_order_decision_effect_message
	create_holy_order_effect = yes
	# And the free army.
	forge_the_jomsvikings_maa_reward_effect = yes
}

fp1_remove_humsac_offended_counties_effect = {
	every_sub_realm_county = {
		if = {
			limit = { has_county_modifier = held_human_sacrifice_fp1_modifier }
			remove_county_modifier = held_human_sacrifice_fp1_modifier
		}
	}
}

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

designate_gruesome_festivals_sacrifice_interaction_effect = {
	add_character_modifier = gruesome_festivals_designated_sacrifice_fp1_modifier
	add_opinion = {
		target = scope:actor
		modifier = hate_opinion
		opinion = -50
	}
}

designate_gruesome_festivals_sacrifice_modifier_removed_effect = {
	send_interface_toast = {
		title = designate_gruesome_festivals_sacrifice_modifier_removed_effect.t
		left_icon = $DESIGNATOR$
		remove_character_modifier = gruesome_festivals_designated_sacrifice_fp1_modifier
		# Removed manually.
		if = {
			limit = {
				exists = scope:removal_type
				scope:removal_type = flag:manual
			}
			custom_tooltip = designate_gruesome_festivals_sacrifice_modifier_removed_effect.manual.tt
		}
		# Removed due to a new chief sacrifice being designated.
		else_if = {
			limit = {
				exists = scope:removal_type
				scope:removal_type = flag:override
			}
			custom_tooltip = designate_gruesome_festivals_sacrifice_modifier_removed_effect.override.tt
		}
		# Removed due to the chain invalidating.
		else = { custom_tooltip = designate_gruesome_festivals_sacrifice_modifier_removed_effect.invalidation.tt }
	}
}

# Either-side effects for a tbc ending with death.
fp1_tbc_death_effect = {
	# Check to see if this is a duel to the death between those of equal rank.
	## We check this here in case an *untimely* death causes either participant to go up a rank.
	## Equal rank: level playing field, maximum honour.
	if = {
		limit = { $TBC_VICTOR$.primary_title.tier = $TBC_LOSER$.primary_title.tier }
		$TBC_VICTOR$ = { add_prestige = medium_prestige_gain }
	}
	## Unequal rank: challenging your betters but clearly it was merited.
	else_if = {
		limit = { $TBC_VICTOR$.primary_title.tier < $TBC_LOSER$.primary_title.tier }
		$TBC_VICTOR$ = { add_prestige = minor_prestige_gain }
	}
	$TBC_LOSER$ = {
		death = {
			killer = $TBC_VICTOR$
			death_reason = death_duel
		}
	}
}

# Recipient-side effects for a tbc ending with a cash settlement.
fp1_tbc_gold_effect = {
	$TBC_LOSER$ = {
		pay_short_term_gold = {
			target = $TBC_VICTOR$
			gold = major_gold_value
		}
	}
}

# Recipient-side effects for a tbc ending with an agreed favour.
fp1_tbc_hook_effect = {
	$TBC_VICTOR$ = {
		add_hook = {
			type = trial_by_combat_hook
			target = $TBC_LOSER$
		}
	}
}

# Recipient-side effects for a tbc ending with utter humiliation.
fp1_tbc_humiliation_effect = {
	$TBC_LOSER$ = {
		every_vassal = {
			custom = every_belligerent_vassal
			limit = {
				has_vassal_stance = belligerent
			}
			add_opinion = {
				modifier = liege_lost_duel
				target = $TBC_LOSER$
			}
		}
		add_stress = massive_stress_gain
	}
	$TBC_VICTOR$ = {
		every_vassal = {
			custom = every_belligerent_vassal
			limit = {
				has_vassal_stance = belligerent
			}
			custom = every_belligerent_vassal
			add_opinion = {
				modifier = liege_won_duel
				target = $TBC_VICTOR$
			}
		}
	}
}

# Actor-side effects for losing most types of tbc.
fp1_tbc_actor_defeated_effect = {
	$TBC_LOSER$ = {
		every_vassal = {
			custom = every_belligerent_vassal
			limit = {
				has_vassal_stance = belligerent
			}
			add_opinion = {
				modifier = liege_lost_duel
				target = $TBC_LOSER$
			}
		}
		add_stress = major_stress_gain
	}
	$TBC_VICTOR$ = {
		every_vassal = {
			custom = every_belligerent_vassal
			limit = {
				has_vassal_stance = belligerent
			}
			custom = every_belligerent_vassal
			add_opinion = {
				modifier = liege_won_duel
				target = $TBC_VICTOR$
			}
		}
	}
}

try_create_challenge_to_single_combat = {
	if = {
		limit = {
			is_imprisoned = no
			root = { has_imprisonment_reason = prev }
		}
		try_create_important_action = {
			important_action_type = action_can_challenge_to_single_combat
			actor = root
			recipient = this
		}
	}
}

replace_with_learning_trait_effect = {
	if = {
		limit = { has_trait = education_intrigue_1 }
		remove_trait = education_intrigue_1
	}
	else_if = {
		limit = { has_trait = education_intrigue_2 }
		remove_trait = education_intrigue_2
	}
	else_if = {
		limit = { has_trait = education_intrigue_3 }
		remove_trait = education_intrigue_3
	}
	else_if = {
		limit = { has_trait = education_intrigue_4 }
		remove_trait = education_intrigue_4
	}
	else_if = {
		limit = { has_trait = education_intrigue_5 }
		remove_trait = education_intrigue_5
	}
	else_if = {
		limit = { has_trait = education_diplomacy_1 }
		remove_trait = education_diplomacy_1
	}
	else_if = {
		limit = { has_trait = education_diplomacy_2 }
		remove_trait = education_diplomacy_2
	}
	else_if = {
		limit = { has_trait = education_diplomacy_3 }
		remove_trait = education_diplomacy_3
	}
	else_if = {
		limit = { has_trait = education_diplomacy_4 }
		remove_trait = education_diplomacy_4
	}
	else_if = {
		limit = { has_trait = education_diplomacy_5 }
		remove_trait = education_diplomacy_5
	}
	else_if = {
		limit = { has_trait = education_stewardship_1 }
		remove_trait = education_stewardship_1
	}
	else_if = {
		limit = { has_trait = education_stewardship_2 }
		remove_trait = education_stewardship_2
	}
	else_if = {
		limit = { has_trait = education_stewardship_3 }
		remove_trait = education_stewardship_3
	}
	else_if = {
		limit = { has_trait = education_stewardship_4 }
		remove_trait = education_stewardship_4
	}
	else_if = {
		limit = { has_trait = education_stewardship_5 }
		remove_trait = education_stewardship_5
	}
	else_if = {
		limit = { has_trait = education_martial_1 }
		remove_trait = education_martial_1
	}
	else_if = {
		limit = { has_trait = education_martial_2 }
		remove_trait = education_martial_2
	}
	else_if = {
		limit = { has_trait = education_martial_3 }
		remove_trait = education_martial_3
	}
	else_if = {
		limit = { has_trait = education_martial_4 }
		remove_trait = education_martial_4
	}
	else_if = {
		limit = { has_trait = education_martial_5 }
		remove_trait = education_martial_5
	}
	random_list = {
		2 = { add_trait = education_learning_2 }
		2 = { add_trait = education_learning_3 }
		1 = { add_trait = education_learning_4 }
	}
}

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

clean_scandinavian_adventurer_effect = {
	scope:attacker = {
		every_held_title = {
			limit = { has_variable = temporary_title }
			save_scope_as = destroyable_title
			scope:attacker = { destroy_title = scope:destroyable_title }
		}
		remove_character_modifier = scandinavian_adventurer_fp1_modifier
	}
}

fp1_add_realm_size_appropriate_sa_reprieve_effect = {
	# Depending on the sub realm size of $VICTIM$, we give them a scaling amount of time not to be hit by SA attacks.
	## The smallest realms get a long reprieve.
	if = {
		limit = { sub_realm_size <= tiny_realm_size }
		add_character_modifier = {
			modifier = fp1_reprieve_from_sa_modifier
			years = 20
		}
	}
	## Then a decent reprieve.
	else_if = {
		limit = { sub_realm_size <= minor_realm_size }
		add_character_modifier = {
			modifier = fp1_reprieve_from_sa_modifier
			years = 15
		}
	}
	## Then a short reprieve.
	else_if = {
		limit = { sub_realm_size <= medium_realm_size }
		add_character_modifier = {
			modifier = fp1_reprieve_from_sa_modifier
			years = 10
		}
	}
	## Then you're clearly too big and juicy a target for much reprieve at all.
	else = {
		add_character_modifier = {
			modifier = fp1_reprieve_from_sa_modifier
			years = 5
		}
	}
}

fp1_remember_recent_conquest_victory_effect = {
	if = {
		limit = {
			# DLC check.
			has_fp1_dlc_trigger = yes
			# Is the culture eligible?
			fp1_can_raise_stele_trigger = yes
		}
		set_variable = {
			name = recent_conquest_victory
			value = scope:defender
			years = 5
		}
	}
}

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

nithing_character_creation_scripted_effect = {
	add_trait = disinherited
	add_character_modifier = { modifier = nithing_fp1_modifier }
}

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

initial_decentralized_rule_friends_modifier_effect = {
	if = {
		limit = {
			exists = dynasty
			is_ruler = yes
		}
		if = {
			limit = {
				NOT = { has_variable = decentralized_rule_perk_value } 
			}
			set_variable = {
				name = decentralized_rule_perk_value
				value = 0
			}
		}
		
		every_relation = {
			type = friend
			add_to_temporary_list = decentralized_rule_friends
		}
		every_relation = {
			type = best_friend
			add_to_temporary_list = decentralized_rule_friends
		}

		if = {
			limit = {
				exists = var:decentralized_rule_perk_value
				save_temporary_scope_as = affected_friend
				var:decentralized_rule_perk_value < decentralized_rule_max
				any_in_list = {
					list = decentralized_rule_friends
					count > scope:affected_friend.var:decentralized_rule_perk_value 
				}
			}
			add_character_modifier = {
				modifier = decentralized_rule_perk_modifier
			}
			change_variable = {
				name = decentralized_rule_perk_value
				add = 1
			}
		}	
	}
}

decentralized_rule_friends_modifier_effect = {
	if = {
		limit = {
			dynasty ?= {
				has_dynasty_perk = fp1_adventure_legacy_5
			}
			is_ruler = yes
		}
		if = {
			limit = {
				NOT = { has_variable = decentralized_rule_perk_value } 
			}
			set_variable = {
				name = decentralized_rule_perk_value
				value = 0
			}
		}
		
		every_relation = {
			type = friend
			add_to_temporary_list = decentralized_rule_friends
		}
		every_relation = {
			type = best_friend
			add_to_temporary_list = decentralized_rule_friends
		}

		if = {
			limit = {
				exists = var:decentralized_rule_perk_value
				save_temporary_scope_as = affected_friend
				var:decentralized_rule_perk_value < decentralized_rule_max
				any_in_list = {
					list = decentralized_rule_friends
					count > scope:affected_friend.var:decentralized_rule_perk_value 
				}
			}
			add_character_modifier = {
				modifier = decentralized_rule_perk_modifier
			}
			change_variable = {
				name = decentralized_rule_perk_value
				add = 1
			}
		}	
	}
}

decentralized_rule_removal_effect = {
	if = {
		limit = {
			dynasty ?= {
				has_dynasty_perk = fp1_adventure_legacy_5
			}
		}

		every_relation = {
			type = friend
			add_to_temporary_list = decentralized_rule_friends
		}
		every_relation = {
			type = best_friend
			add_to_temporary_list = decentralized_rule_friends
		}

		if = {
			limit = {
				exists = var:decentralized_rule_perk_value
				save_temporary_scope_as = affected_friend
				var:decentralized_rule_perk_value <= decentralized_rule_max
				var:decentralized_rule_perk_value > 0
				any_in_list = {
					list = decentralized_rule_friends
					count < var:decentralized_rule_perk_value
					NOT = { has_character_flag = about_to_die_but_not_dead_yet_flag  }
				}
			}
			
			if = {
				limit = {
					has_character_modifier = decentralized_rule_perk_modifier
				}
				remove_character_modifier = decentralized_rule_perk_modifier
			}
		
			change_variable = {
				name = decentralized_rule_perk_value
				subtract = 1
			}
		}
	}
}

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

calculate_authority_cooldown_break_effect = {
	if = {
		limit = {
			has_variable = crown_authority_cooldown
			realm_law_use_crown_authority = yes
		}
		# Set it up if it doesn't exist.
		if = {
			limit = {
				NOT = { has_variable = authority_cooldown_break_tally }
			}
			set_variable = {
				name = authority_cooldown_break_tally
				value = authority_cooldown_breaker_value
			}
			# And increment it once, since they've just taken the first use and we give no freebies.
			change_variable = {
				name = authority_cooldown_break_tally
				add = authority_cooldown_breaker_value
			}
		}
		# Otherwise, increment it.
		else_if = {
			limit = { has_variable = authority_cooldown_break_tally }
			change_variable = {
				name = authority_cooldown_break_tally
				add = authority_cooldown_breaker_value
			}
		}
	}
	else_if = {
		limit = {
			has_variable = tribal_authority_cooldown
			realm_law_use_tribal_authority = yes
		}
		# Set it up if it doesn't exist.
		if = {
			limit = {
				NOT = { has_variable = authority_cooldown_break_tally }
			}
			set_variable = {
				name = authority_cooldown_break_tally
				value = authority_cooldown_breaker_value
			}
			# And increment it once, since they've just taken the first use and we give no freebies.
			change_variable = {
				name = authority_cooldown_break_tally
				add = authority_cooldown_breaker_value
			}
		}
		# Otherwise, increment it.
		else_if = {
			limit = { has_variable = authority_cooldown_break_tally }
			change_variable = {
				name = authority_cooldown_break_tally
				add = authority_cooldown_breaker_value
			}
		}
	}
	else_if = {
		limit = {
			has_variable = imperial_bureaucracy_cooldown
			realm_law_use_imperial_bureaucracy = yes
		}
		# Set it up if it doesn't exist.
		if = {
			limit = {
				NOT = { has_variable = authority_cooldown_break_tally }
			}
			set_variable = {
				name = authority_cooldown_break_tally
				value = authority_cooldown_breaker_value
			}
			# And increment it once, since they've just taken the first use and we give no freebies.
			change_variable = {
				name = authority_cooldown_break_tally
				add = authority_cooldown_breaker_value
			}
		}
		# Otherwise, increment it.
		else_if = {
			limit = { has_variable = authority_cooldown_break_tally }
			change_variable = {
				name = authority_cooldown_break_tally
				add = authority_cooldown_breaker_value
			}
		}
	}
}
