﻿#makes errors go kablooey 
fix_variable_error = {
    save_scope_as = $ERROR$ 
    add_to_list = $ERROR$ 
    clear_variable_list = $ERROR$ 
    clear_saved_scope = $ERROR$ 
    set_variable = {name = $ERROR$ value = flag:$ERROR$} 
    set_global_variable = {name = $ERROR$ value = flag:$ERROR$}  
    if = {limit = {OR = {has_character_flag = $ERROR$ var:$ERROR$ = flag:$ERROR$}} remove_variable = $ERROR$ remove_global_variable = $ERROR$}
}

# Found the Kingdom of Oman
found_kingdom_of_oman_scripted_effect = {
	
	create_title_and_vassal_change = {
		type = created
		save_scope_as = change
	}
	
	title:k_oman = {
		change_title_holder = {
			holder = root
			change = scope:change
		}
	}
	
	resolve_title_and_vassal_change = scope:change
	
	# Prestige
	add_prestige = 350

	#custom_tooltip = restore_aquitaine_decision_effect_tooltip
	hidden_effect = {
		title:c_dhofar = { set_de_jure_liege_title = title:k_oman }
		title:d_oman = { set_de_jure_liege_title = title:k_oman }
	}
}

# Restore the Kingdom of Old Saxony
restore_saxony_decision_scripted_effect = {
	title:e_hre.holder = { save_scope_as = scoped_emperor }
	save_scope_as = founder
	
	capital_county = {
		save_scope_as = capital_county_effect
	}
	
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	
	title:k_saxony = {
		change_title_holder = {
			holder = root
			change = scope:title_change
		}
	}
	
	resolve_title_and_vassal_change = scope:title_change
	
	# Prestige
	add_prestige = 350

	# Add Nickname
	set_nickname_effect = { NICKNAME = nick_the_restorer }

	hidden_effect = {
		title:d_holstein = { set_de_jure_liege_title = title:k_saxony }
		title:d_ostfalen = { set_de_jure_liege_title = title:k_saxony }
		title:d_angria = { set_de_jure_liege_title = title:k_saxony }
		title:d_westfalen = { set_de_jure_liege_title = title:k_saxony }
		title:d_anhalt = { set_de_jure_liege_title = title:k_saxony }

		title:k_saxony = {
			set_capital_county = scope:capital_county_effect
		}
	}
	#custom_tooltip = restore_dumnonia_decision_effects_de_jure_tt
}

# Form the Grand Duchy of Saxony
form_grand_duchy_saxony_decision_scripted_effect = {
	title:e_hre.holder = { save_scope_as = scoped_emperor }
	save_scope_as = founder
	
	capital_county = {
		save_scope_as = capital_county_effect
	}
	
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	
	title:k_grand_saxony = {
		change_title_holder = {
			holder = root
			change = scope:title_change
		}
	}
	
	resolve_title_and_vassal_change = scope:title_change

	# Prestige
	add_prestige = 350

	hidden_effect = {
		every_county_in_region = {
			region = custom_grand_saxony
			duchy = { set_de_jure_liege_title = title:k_grand_saxony }
		}
		
		title:k_grand_saxony = {
			set_capital_county = scope:capital_county_effect
			set_de_jure_liege_title = title:e_hre
			set_coa = title:d_angria
		}
	}
	#custom_tooltip = restore_dumnonia_decision_effects_de_jure_tt
}

# Form the Grand Duchy of Franconia
form_grand_duchy_franconia_decision_scripted_effect = {
	title:e_hre.holder = { save_scope_as = scoped_emperor }
	save_scope_as = founder
	
	capital_county = {
		save_scope_as = capital_county_effect
	}
	
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	
	title:k_grand_franconia = {
		change_title_holder = {
			holder = root
			change = scope:title_change
		}
	}
	
	resolve_title_and_vassal_change = scope:title_change

	# Prestige
	add_prestige = 350

	hidden_effect = {
		every_county_in_region = {
			region = custom_core_franconia
			duchy = { set_de_jure_liege_title = title:k_grand_franconia }
		}
		
		title:k_grand_franconia = {
			set_capital_county = scope:capital_county_effect
			set_de_jure_liege_title = title:e_hre
			set_coa = title:d_west_franconia
		}
	}
}

# Form the Grand Duchy of Swabia
form_grand_duchy_swabia_decision_scripted_effect = {
	title:e_hre.holder = { save_scope_as = scoped_emperor }
	save_scope_as = founder
	
	capital_county = {
		save_scope_as = capital_county_effect
	}
	
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	
	title:k_grand_swabia = {
		change_title_holder = {
			holder = root
			change = scope:title_change
		}
	}
	
	resolve_title_and_vassal_change = scope:title_change
	
	# Prestige
	add_prestige = 350

	hidden_effect = {
		every_county_in_region = {
			region = custom_core_swabia
			duchy = { set_de_jure_liege_title = title:k_grand_swabia }
		}
		
		title:k_grand_swabia = {
			set_capital_county = scope:capital_county_effect
			set_de_jure_liege_title = title:e_hre 
			set_coa = title:d_swabia
		}
	}
}

# Form Bulgarian Empire
form_bulgarian_empire_decision_effect = {
	save_scope_as = founder
	
	capital_county = {
		save_scope_as = capital_county_effect
	}
	
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	
	title:e_bulgarian_empire = {
		change_title_holder = {
			holder = root
			change = scope:title_change
		}
	}
	
	resolve_title_and_vassal_change = scope:title_change
	
	# Prestige
	add_prestige = 750
	
	# Also gain a large amount of dynasty prestige.
	dynasty = { add_dynasty_prestige = 500 }
	
	# Add Nickname
	set_nickname_effect = { NICKNAME = nick_the_great }
	
	# Byzantine Emperor hates new pretender
	if = {
		limit = {
			exists = title:e_byzantium.holder
		}
		title:e_byzantium.holder = {
			add_opinion = {
				target = root
				modifier = de_imperial_pretender_opinions
			}
		}
	}

	# Gain Next Level of Crown Authority
	root = {
		if = {
			limit = {
				has_realm_law = crown_authority_0
			}
			add_realm_law_skip_effects = crown_authority_1
		}
		else_if = {
			limit = {
				has_realm_law = crown_authority_1
			}
			add_realm_law_skip_effects = crown_authority_2
		}
		else_if = {
			limit = {
				has_realm_law = crown_authority_2
			}
			add_realm_law_skip_effects = crown_authority_3
		}
	}

	#Sort title laws.
	destroy_title = title:k_bulgaria	#Make Bulgaria inalienable.
	title:e_bulgarian_empire = {
		if = {	#Add Salic law.
			limit = {
				root = {
					OR = {
						has_realm_law = male_preference_law
						has_realm_law = male_only_law
					}
				}
			}
			add_title_law = male_only_law
		}
		if = {	#Ignore Salic law.
			limit = {
				root = { has_realm_law = equal_law }
			}
			add_title_law = equal_law
		}
		if = {	#Invert Salic law. Take that, Clovis, y'big nerd.
			limit = {
				root = {
					OR = {
						has_realm_law = female_preference_law
						has_realm_law = female_only_law
					}
				}
			}
			add_title_law = female_only_law
		}
	}

	# Copy Title History from k_bulgaria
	title:e_bulgarian_empire = {
		copy_title_history = title:k_bulgaria			
	}

	hidden_effect = {
		title:k_bulgaria = {
			every_in_de_jure_hierarchy = {
				limit = { tier = tier_duchy }
				set_de_jure_liege_title = title:e_bulgarian_empire
			}
			set_de_jure_liege_title = title:e_bulgarian_empire
		}
		title:k_dacia = {
			every_in_de_jure_hierarchy = {
				limit = { tier = tier_duchy }
				set_de_jure_liege_title = title:e_bulgarian_empire
			}
			set_de_jure_liege_title = title:e_bulgarian_empire
		}
	}
}

# Reform the Avar Khaganate 
form_avar_khaganate_decision_effect = {
	save_scope_as = founder
	
	capital_county = {
		save_scope_as = capital_county_effect
	}
	
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	
	title:e_avar_khaganate = {
		change_title_holder = {
			holder = root
			change = scope:title_change
		}
	}
	
	resolve_title_and_vassal_change = scope:title_change
	
	# Prestige
	add_prestige = 1000
	
	# Also gain a large amount of dynasty prestige.
	dynasty = { add_dynasty_prestige = 750 }
	
	# Add Nickname
	set_nickname_effect = { NICKNAME = nick_the_great_and_terrible }

	hidden_effect = {
		give_landsproper = yes
	}
}

give_landsproper = {
	# every_county_in_region = {
	# 	region = custom_avaria
	# 	kingdom = {
	# 		set_de_jure_liege_title = title:e_avar_khaganate
	# 	}
	# }

	title:e_carpathia = {
		every_in_de_jure_hierarchy = {
			limit = { tier = tier_kingdom }
			set_de_jure_liege_title = title:e_avar_khaganate
		}
		set_de_jure_liege_title = title:e_avar_khaganate
	}
	title:k_pontic_steppe = {
		every_in_de_jure_hierarchy = {
			limit = { tier = tier_duchy }
			set_de_jure_liege_title = title:e_avar_khaganate
		}
		set_de_jure_liege_title = title:e_avar_khaganate
	}
	title:k_bohemia = {
		every_in_de_jure_hierarchy = {
			limit = { tier = tier_duchy }
			set_de_jure_liege_title = title:e_avar_khaganate
		}
		set_de_jure_liege_title = title:e_avar_khaganate
	}
	title:k_moravia = {
		every_in_de_jure_hierarchy = {
			limit = { tier = tier_duchy }
			set_de_jure_liege_title = title:e_avar_khaganate
		}
		set_de_jure_liege_title = title:e_avar_khaganate
	}
	title:d_osterreich = {
		every_in_de_jure_hierarchy = {
			limit = { tier = tier_county }
			set_de_jure_liege_title = title:k_hungary
		}
		set_de_jure_liege_title = title:k_hungary
	}
	title:d_steyermark = {
		every_in_de_jure_hierarchy = {
			limit = { tier = tier_county }
			set_de_jure_liege_title = title:k_hungary
		}
		set_de_jure_liege_title = title:k_hungary
	}

	title:d_krain = {
		every_in_de_jure_hierarchy = {
			limit = { tier = tier_county }
			set_de_jure_liege_title = title:k_hungary
		}
		set_de_jure_liege_title = title:k_hungary
	}

	title:d_slavonia = {
		every_in_de_jure_hierarchy = {
			limit = { tier = tier_county }
			set_de_jure_liege_title = title:k_hungary
		}
		set_de_jure_liege_title = title:k_hungary
	}

	title:d_lower_bosna = {
		every_in_de_jure_hierarchy = {
			limit = { tier = tier_county }
			set_de_jure_liege_title = title:k_hungary
		}
		set_de_jure_liege_title = title:k_hungary
	}

	title:d_syrmia = {
		every_in_de_jure_hierarchy = {
			limit = { tier = tier_county }
			set_de_jure_liege_title = title:k_hungary
		}
		set_de_jure_liege_title = title:k_hungary
	}
	title:k_hungary = {
		every_in_de_jure_hierarchy = {
			limit = { tier = tier_duchy }
			set_de_jure_liege_title = title:k_hungary
		}
		set_de_jure_liege_title = title:k_hungary
	}
	title:k_dacia = {
		every_in_de_jure_hierarchy = {
			limit = { tier = tier_duchy }
			set_de_jure_liege_title = title:k_hungary
		}
		set_de_jure_liege_title = title:k_hungary
	}
	title:k_moldavia = {
		every_in_de_jure_hierarchy = {
			limit = { tier = tier_duchy }
			set_de_jure_liege_title = title:k_hungary
		}
		set_de_jure_liege_title = title:k_hungary
	}
	title:d_istria = {
		every_in_de_jure_hierarchy = {
			limit = { tier = tier_duchy }
			set_de_jure_liege_title = title:k_hungary
		}
		set_de_jure_liege_title = title:k_italy
	}
	title:e_avar_khaganate = {
		set_capital_county = scope:capital_county_effect
	}
}

avar_test_effect = {

	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}

	every_county_in_region = {
		region = custom_frisia01
		change_title_holder = {
			holder = root
			change = scope:title_change
		}
	}

	resolve_title_and_vassal_change = scope:title_change
}

# Integrate Aquitaine
restore_aquitaine_decision_effect = {
	create_title_and_vassal_change = {
		type = created
		save_scope_as = change
	}
	
	title:k_aquitaine = {
		change_title_holder = {
			holder = root
			change = scope:change
		}
	}
	
	resolve_title_and_vassal_change = scope:change

	# Prestige
	add_prestige = 250

	#custom_tooltip = restore_aquitaine_decision_effect_tooltip
	hidden_effect = {
		every_county_in_region = {
			region = custom_aquitaine
			duchy = { set_de_jure_liege_title = title:k_aquitaine }
		}
	}
}

# Reform Samo's Empire
form_samos_empire_decision_effect = {
	save_scope_as = founder
	
	create_title_and_vassal_change = {
		type = created
		save_scope_as = change
	}
	
	title:k_winideheim = {
		change_title_holder = {
			holder = root
			change = scope:change
		}
	}
	
	resolve_title_and_vassal_change = scope:change
	
	# Prestige
	add_prestige = 750

	# Also gain a large amount of dynasty prestige.
	dynasty = { add_dynasty_prestige = 500 }

	# Add Nickname
	set_nickname_effect = { NICKNAME = nick_the_great }

	# Gain Next Level of Crown Authority
	root = {
		if = {
			limit = {
				has_realm_law = crown_authority_0
			}
			add_realm_law_skip_effects = crown_authority_1
		}
		else_if = {
			limit = {
				has_realm_law = crown_authority_1
			}
			add_realm_law_skip_effects = crown_authority_2
		}
		else_if = {
			limit = {
				has_realm_law = crown_authority_2
			}
			add_realm_law_skip_effects = crown_authority_3
		}
	}

	#custom_tooltip = restore_aquitaine_decision_effect_tooltip
	hidden_effect = {
		title:d_krain = { save_scope_as = samo_krain }
		title:d_carinthia = { save_scope_as = samo_carinthia }
		title:d_istria = { save_scope_as = samo_istria }
		every_held_title = {
			limit = {
				tier = tier_kingdom
				NOT = { this = title:k_winideheim }
			}
			scope:founder = {
				destroy_title = prev
			}
		}
		if = {	#If owned, annex Krain
			limit = {
				title:d_krain = { holder = root }
			}
			title:d_krain = { set_de_jure_liege_title = title:k_winideheim }
		}
		if = {	#If owned, annex Carinthia.
			limit = {
				title:d_carinthia = { holder = root }
			}
			title:d_carinthia = { set_de_jure_liege_title = title:k_winideheim  }
		}
		if = {	#If owned, annex Istria.
			limit = {
				title:d_istria = { holder = root }
			}
			title:d_istria = { set_de_jure_liege_title = title:k_winideheim  }
		}

		every_county_in_region = {
			region = custom_samos_empire
			duchy = { set_de_jure_liege_title = title:k_winideheim }
		}
		title:k_winideheim = { set_de_jure_liege_title = title:e_west_slavia }
	}
	custom_tooltip = form_samos_empire_decision_effect_de_jure_tt
}

# Restore the Frisian Kingdom
restore_frisia_decision_scripted_effect = {
	create_title_and_vassal_change = {
		type = created
		save_scope_as = change
	}
	
	title:k_frisia = {
		change_title_holder = {
			holder = root
			change = scope:change
		}
	}
	
	resolve_title_and_vassal_change = scope:change
	
	# Prestige
	add_prestige = 350

	# Add Nickname
	set_nickname_effect = { NICKNAME = nick_the_restorer }

	#custom_tooltip = restore_aquitaine_decision_effect_tooltip
	hidden_effect = {
		every_county_in_region = {
			region = custom_frisia01
			duchy = { set_de_jure_liege_title = title:k_frisia }
		}
	}
}

# Unify Italy
modified_unify_italy_decision_scripted_effect = {
	save_scope_as = founder
	
	capital_county = {
		save_scope_as = capital_county_effect
	}
	
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	
	title:e_italy = {
		change_title_holder = {
			holder = root
			change = scope:title_change
		}
	}
	
	resolve_title_and_vassal_change = scope:title_change
	
	# Prestige
	add_prestige = 750
	
	# Also gain a large amount of dynasty prestige.
	dynasty = { add_dynasty_prestige = 500 }
	
	# Add Nickname
	set_nickname_effect = { NICKNAME = nick_the_great }
	
	if = {
		limit = {
			exists = title:e_hre.holder
		}
		title:e_hre.holder = {
			add_opinion = {
				target = root
				modifier = de_imperial_pretender_opinions
			}
		}
	}
	
	if = {
		limit = {
			exists = title:e_byzantium.holder
		}
		title:e_byzantium.holder = {
			add_opinion = {
				target = root
				modifier = de_imperial_pretender_opinions
			}
		}
	}
	
	# Gain Next Level of Crown Authority
	root = {
		if = {
			limit = {
				has_realm_law = crown_authority_0
			}
			add_realm_law_skip_effects = crown_authority_1
		}
		else_if = {
			limit = {
				has_realm_law = crown_authority_1
			}
			add_realm_law_skip_effects = crown_authority_2
		}
		else_if = {
			limit = {
				has_realm_law = crown_authority_2
			}
			add_realm_law_skip_effects = crown_authority_3
		}
	}

	#Sort title laws.
	# title:e_italy = {
	# 	if = {	#Add Salic law.
	# 		limit = {
	# 			root = {
	# 				OR = {
	# 					has_realm_law = male_preference_law
	# 					has_realm_law = male_only_law
	# 				}
	# 			}
	# 		}
	# 		add_title_law = male_only_law
	# 	}
	# 	if = {	#Ignore Salic law.
	# 		limit = {
	# 			root = { has_realm_law = equal_law }
	# 		}
	# 		add_title_law = equal_law
	# 	}
	# 	if = {	#Invert Salic law. Take that, Clovis, y'big nerd.
	# 		limit = {
	# 			root = {
	# 				OR = {
	# 					has_realm_law = female_preference_law
	# 					has_realm_law = female_only_law
	# 				}
	# 			}
	# 		}
	# 		add_title_law = female_only_law
	# 	}
	# }

	hidden_effect = {
		title:d_istria = { # Bite Croatia.
			set_de_jure_liege_title = title:k_italy
		}
		title:d_krain = {
			set_de_jure_liege_title = title:k_italy
		}
		title:d_tyrol = {
			set_de_jure_liege_title = title:k_italy
		}
		title:d_carinthia = {
			set_de_jure_liege_title = title:k_italy
		}
		title:k_sicily = { # Then shift all Kingdoms.
			set_de_jure_liege_title = title:e_italy
		}
		title:k_romagna = {
			set_de_jure_liege_title = title:e_italy
		}
		title:k_sardinia = {
			set_de_jure_liege_title = title:e_italy
		}
		title:k_italy = {
			set_de_jure_liege_title = title:e_italy
		}
		title:k_venice = {
			set_de_jure_liege_title = title:e_italy
		}
		title:k_croatia = {
			set_de_jure_liege_title = title:e_italy
		}
		title:k_serbia = {
			set_de_jure_liege_title = title:e_italy
		}
	}
	
}

# # Unite Norse
# unite_norwg_decision_effect = {

# 	# Prestige
# 	add_prestige = 500
	
# 	# Also gain a large amount of dynasty prestige.
# 	dynasty = { add_dynasty_prestige = 250 }
	
# }

# # Unite Danes
# unite_danes_decision_effect = {

# 	# Prestige
# 	add_prestige = 500
	
# 	# Also gain a large amount of dynasty prestige.
# 	dynasty = { add_dynasty_prestige = 250 }
	
# }

# # Unite Swedes
# unite_swedes_decision_effect = {

# 	# Prestige
# 	add_prestige = 500
	
# 	# Also gain a large amount of dynasty prestige.
# 	dynasty = { add_dynasty_prestige = 250 }
	
# }

# # Unite the Anglo Saxons
# unite_the_anglo_saxons_effect = {
# 	save_scope_as = founder
	
# 	# Prestige
# 	add_prestige = 500
	
# 	# Also gain a large amount of dynasty prestige.
# 	dynasty = { add_dynasty_prestige = 300 }
	
# 	# Add Nickname
# 	set_nickname_effect = { NICKNAME = nick_the_great }
	
# }

# Fulfill the Hadith of Conquest
fulfill_the_conquest_hadith_scripted_effect = {
	save_scope_as = founder
	
	# Add Nickname (Mehmed vibes)
	set_nickname_effect = { NICKNAME = nick_the_conqueror }

	# Prestige
	add_prestige = massive_prestige_value
	add_prestige_level = 2
	
	# Piety
	add_piety = major_piety_gain
	add_piety_level = 1
	
	# Also gain a large amount of dynasty prestige.
	dynasty = {
		add_dynasty_prestige = excessive_dynasty_prestige_gain
	}
	
	faith = {
		change_fervor = {
			value = 25
			desc = fervor_gain_conquest_hadith
		}
	}
}

# Restore the Faith of Zoroaster
restore_the_faith_of_zoroaster_decision_generic_effects_scripted_effect = {
	scope:religious_leader = {
		# Assign a new nickname, if appropriate.
		if = {
			limit = {
				OR = {
					has_any_nickname = no
					has_bad_nickname = yes
				}
			}
			give_nickname = nick_the_restorer

		}

		# Finally, convert.
		## We do this here so that it's reformed by the time you switch (giving you a better chance with vassal conversion rates).
		set_character_faith_with_conversion = faith:mazdayasna
	}
	
	# Rise in Fervor due to public restoration
	faith:mazdayasna = {
		change_fervor = {
			value = 25
			desc = fervour_gain_unexpected_resurgence
		}
	}
	
	# Create the new HoF.
	custom_tooltip = favour_the_countryside_basques_decision.tt.spiritual_hof_acquired
	hidden_effect = {
		scope:religious_leader = {
			set_up_dynamic_spiritual_hof_title_effect = { CREATOR = scope:religious_leader }
		}
	}
	
	# We get a small modifier for clergy approval, since they're a bit peeved at the tenet-messing.
	add_character_modifier = fp2_champion_of_the_old_ways_modifier
	
}

# Establish Cairo
establish_cairo_scripted_effect = {
	save_scope_as = founder

	title:c_cairo = {
		# Give it an extra two building slots.
		# title_province = { add_building_slot = 1 }
		# And a decent development boost.
		change_development_level = minor_development_level_gain

		# Cairo gets a huge modernising boost.
		add_county_modifier = {
			modifier = ce_found_cairo_capital_modifier
			years = 50
		}
	}

	# Make Cairo your Realm Capital
	scope:founder = {
		if = { #Usurp if not held personally.
			limit = {
				NOT = { title:c_cairo.holder = { this = root } }
			}
			title:c_cairo.holder = { save_scope_as = target_for_denunciation }
			create_title_and_vassal_change = {
				type = returned
				save_scope_as = change
				add_claim_on_loss = no
			}
			title:c_cairo = {
				change_title_holder = {
					holder = root
					change = scope:change
				}
			}
			resolve_title_and_vassal_change = scope:change

		}
		set_realm_capital = title:c_cairo
	}
	
	# Name-change everything back to relating to Cairo
	title:b_cairo = {
		set_title_name = b_cairo
	}

	title:c_cairo = {
			set_title_name = c_cairo
	}

	title:d_cairo = {
			set_title_name = d_cairo
	}
}

promulgate_the_assizes_of_ariano_scripted_effect = {
	save_scope_as = founder

	# Add Nickname
	if = {
		limit = {
			OR = {
				has_any_nickname = no
				has_bad_nickname = yes
			}
		}
		give_nickname = nick_the_lawgiver
	}

	root.culture = { # Not Tribal Era
		custom_tooltip = the_assizes_of_ariano_are_enshrined_in_tradition
		if = {
			limit = { root = culture_head }
			hidden_effect = {
				remove_culture_tradition = tradition_legalistic
				add_culture_tradition = tradition_assizes_ariano
			}
		}
	}

	# Also gain a large amount of dynasty prestige.
	dynasty = {
		add_dynasty_prestige = 500
	}

	# Your new laws increase your Realm Authority
	if = {
		limit = {
			has_realm_law = crown_authority_0
		}
		add_realm_law_skip_effects= crown_authority_1
	}
	else_if = {
		limit = {
			has_realm_law = crown_authority_1
		}
		add_realm_law_skip_effects = crown_authority_2
	}
	else_if = {
		limit = {
			has_realm_law = crown_authority_2
		}
		add_realm_law_skip_effects = crown_authority_3
	}

	if = {
		limit = { has_title = title:k_sicily }
		title:k_sicily = {
			every_in_de_jure_hierarchy = {
				custom = every_controlled_province_in_custom_sicily
				limit = {
					tier = tier_county
					title_province = { geographical_region = custom_sicily }
					 holder = {
						OR = {
							this = root
							target_is_liege_or_above = root
						}
					}
				}
				add_county_modifier = {
					modifier = ce_promulgated_the_assizes_of_ariano_modifier
					years = 25
				}
			}
		}
	}	

}

establish_the_ducat_coin_decision_scripted_effect = {
	root.culture = {
		if = {
			limit = {
				NOT = { has_innovation = innovation_ducat_trade_coin }
			}
			add_innovation = innovation_ducat_trade_coin
		}
	}	

	add_character_modifier = {
		modifier = ce_ruler_who_established_the_ducat_system_modifier
	}		

	dynasty = {
		# Also gain a large amount of dynasty prestige.
		add_dynasty_prestige = 250
	}

	# Major Regional Centers are home to mints
	if = {
		limit = {
			this = { completely_controls = title:d_sicily }
		}
		title:c_palermo = {	
			title_province = {
				add_province_modifier = {
					modifier = ce_sicilian_mint_modifier
					years = 100					
				}
			}
		}		
	}
	if = {
		limit = {
			this = { completely_controls = title:d_apulia }
		}
		title:c_apulia = {
			title_province = {
				add_province_modifier = {
					modifier = ce_sicilian_mint_modifier
					years = 100					
				}
			}
		}				
	}

	# General effects from the new currency
	if = {
		limit = { has_title = title:k_sicily }
		title:k_sicily = {
			every_in_de_jure_hierarchy = {
				custom = every_controlled_province_in_custom_sicily
				limit = {
					tier = tier_county
					title_province = { geographical_region = custom_sicily }
					 holder = {
						OR = {
							this = root
							target_is_liege_or_above = root
						}
					}
				}
				add_county_modifier = {
					modifier = ce_benefits_of_the_ducat_system_modifier
					years = 25
				}
			}
		}
	}
}

embraced_the_ducat_coin_scripted_effect = {
	if = {
		limit = { has_title = title:k_italy }
		title:k_italy = {
			every_in_de_jure_hierarchy = {
				custom = every_controlled_province_in_custom_sicily
				limit = {
					tier = tier_county
					title_province = { geographical_region = world_europe_south_italy }
					 holder = {
						OR = {
							this = root
							target_is_liege_or_above = root
						}
					}
				}
				add_county_modifier = {
					modifier = ce_benefits_of_the_ducat_system_modifier
					years = 25
				}
			}
		}
	}
	if = {
		limit = { has_title = title:k_romagna }
		title:k_romagna = {
			every_in_de_jure_hierarchy = {
				custom = every_controlled_province_in_custom_sicily
				limit = {
					tier = tier_county
					title_province = { geographical_region = world_europe_south_italy }
					 holder = {
						OR = {
							this = root
							target_is_liege_or_above = root
						}
					}
				}
				add_county_modifier = {
					modifier = ce_benefits_of_the_ducat_system_modifier
					years = 25
				}
			}
		}
	}
	if = {
		limit = { has_title = title:k_sardinia }
		title:k_sardinia = {
			every_in_de_jure_hierarchy = {
				custom = every_controlled_province_in_custom_sicily
				limit = {
					tier = tier_county
					title_province = { geographical_region = world_europe_south_italy }
					 holder = {
						OR = {
							this = root
							target_is_liege_or_above = root
						}
					}
				}
				add_county_modifier = {
					modifier = ce_benefits_of_the_ducat_system_modifier
					years = 25
				}
			}
		}
	}
	if = {
		limit = { has_title = title:k_venice }
		title:k_venice = {
			every_in_de_jure_hierarchy = {
				custom = every_controlled_province_in_custom_sicily
				limit = {
					tier = tier_county
					title_province = { geographical_region = world_europe_south_italy }
					 holder = {
						OR = {
							this = root
							target_is_liege_or_above = root
						}
					}
				}
				add_county_modifier = {
					modifier = ce_benefits_of_the_ducat_system_modifier
					years = 25
				}
			}
		}
	}
	if = {
		limit = { has_title = title:k_genoa }
		title:k_genoa = {
			every_in_de_jure_hierarchy = {
				custom = every_controlled_province_in_custom_sicily
				limit = {
					tier = tier_county
					title_province = { geographical_region = world_europe_south_italy }
					 holder = {
						OR = {
							this = root
							target_is_liege_or_above = root
						}
					}
				}
				add_county_modifier = {
					modifier = ce_benefits_of_the_ducat_system_modifier
					years = 25
				}
			}
		}
	}
	if = {
		limit = { has_title = title:k_pisa }
		title:k_pisa = {
			every_in_de_jure_hierarchy = {
				custom = every_controlled_province_in_custom_sicily
				limit = {
					tier = tier_county
					title_province = { geographical_region = world_europe_south_italy }
					 holder = {
						OR = {
							this = root
							target_is_liege_or_above = root
						}
					}
				}
				add_county_modifier = {
					modifier = ce_benefits_of_the_ducat_system_modifier
					years = 25
				}
			}
		}
	}
}

resettle_sicilian_muslims_decision_scripted_effect = {
	add_character_modifier = {
		modifier = ce_ruler_resettled_sicilian_muslims_modifier
	}

	culture = {
		if = {
			limit = {
				NOT = { has_innovation = innovation_luceran_archery }
			}
			add_innovation = innovation_luceran_archery
		}
	}	

	dynasty = {
		# Also gain a large amount of dynasty prestige.
		add_dynasty_prestige = major_dynasty_prestige_gain
		
		add_dynasty_modifier = {
			modifier = ce_luceran_guard_modifier
		}		
	}

	#Flip Counties
	every_county_in_region = {
		region = ce_island_of_sicily
		custom = every_controlled_province_in_custom_sicily
		limit = {
			target_is_de_facto_liege_or_above = root.primary_title
			faith = faith:ashari 
		}
		set_county_faith = faith:catholic
	}

	title:c_foggia = {
		set_county_faith = faith:ashari

		add_county_modifier = {
			modifier = ce_sicilian_muslim_settlement_modifier
			years = 100				
		}
	}

	title:c_apulia = {
		add_county_modifier = {
			modifier = ce_sicilian_muslim_settlement_modifier
			years = 100				
		}
	}

	spawn_army = {
		uses_supply = yes
		inheritable = yes
		name = custom_sicilian_muslim_event_troops
		men_at_arms = {
			type = sicilian_archers
			stacks = 5
		}
		men_at_arms = {
			type = sicilian_archers
			stacks = 5
		}
		men_at_arms = {
			type = sicilian_archers
			stacks = 5
		}
		men_at_arms = {
			type = mubarizun
			stacks = 5
		}
		location = capital_province
	}	
}

protect_sicilian_muslims_decision_scripted_effect = {
	add_character_modifier = {
		modifier = ce_ruler_protected_sicilian_muslims_modifier
	}

	dynasty = {
		# Also gain a large amount of dynasty prestige.
		add_dynasty_prestige = 250
	}

	#Flip Counties
	every_county_in_region = {
		region = ce_island_of_sicily
		custom = every_controlled_province_in_custom_sicily
		limit = {
			target_is_de_facto_liege_or_above = root.primary_title
		}
		random = {
			chance = 20
			set_county_faith = faith:ashari
			add_county_modifier = {
				modifier = ce_emergent_sicilian_muslim_settlement_modifier
				years = 100				
			}
		}
	}

	#Communities embraced Muslim populace
	every_county_in_region = {
		region = ce_island_of_sicily
		custom = every_controlled_province_in_custom_sicily
		limit = {
			target_is_de_facto_liege_or_above = root.primary_title
			NOT = { faith = faith:ashari }
		}
		random = {
			chance = 33
			add_county_modifier = {
				modifier = ce_embraced_sicilian_muslim_settlement_modifier
				years = 50			
			}
		}
	}

	#Communities discriminate against Muslim populace
	every_county_in_region = {
		region = ce_island_of_sicily
		custom = every_controlled_province_in_custom_sicily
		limit = {
			target_is_de_facto_liege_or_above = root.primary_title
			NOT = { faith = faith:ashari }
			NOT = { has_county_modifier = ce_embraced_sicilian_muslim_settlement_modifier }
		}
		random = {
			chance = 33
			add_county_modifier = {
				modifier = ce_discriminated_sicilian_muslim_settlement_modifier
				years = 50
			}
		}
	}
}

exile_sicilian_muslims_decision_scripted_effect = {
	add_character_modifier = {
		modifier = ce_ruler_exilied_sicilian_muslims_modifier
	}

	dynasty = {
		# Also gain a large amount of dynasty prestige.
		add_dynasty_prestige = 150
	}

	#Flip Counties
	every_county_in_region = {
		region = ce_island_of_sicily
		custom = every_controlled_province_in_custom_sicily
		limit = {
			target_is_de_facto_liege_or_above = root.primary_title
			faith = faith:ashari 
		}
		set_county_faith = faith:catholic
	}

	#Possible negative impact on local trade
	every_county_in_region = {
		region = ce_island_of_sicily
		custom = every_controlled_province_in_custom_sicily
		limit = {
			target_is_de_facto_liege_or_above = root.primary_title
		}
		random = {
			chance = 25
			add_county_modifier = {
				modifier = ce_loss_of_muslims_impacted_economy_modifier
				years = 25
			}
		}
	}
}

massacre_sicilian_muslims_decision_scripted_effect = {
	add_character_modifier = {
		modifier = ce_ruler_massacred_sicilian_muslims_modifier
	}

	dynasty = {
		# Also gain a large amount of dynasty prestige.
		add_dynasty_prestige = 250
	}

		every_vassal = {
			limit = {
				OR = { 
					has_trait = compassionate 
					has_trait = just
					has_trait = forgiving
					has_trait = cynical
				}
			}
			custom = DE_decision_event.0024.kind_hearted_vassals
			add_opinion = {
				target = root
				modifier = disgusted_opinion
				opinion = -50
			}
		}

		every_vassal = {
			limit = {
				faith = root.faith
				OR = { 
					has_trait = zealous 
					has_trait = sadistic
					has_trait = callous
				}
			}
			custom = DE_decision_event.0024.cold_hearted_vassals
			add_opinion = {
				target = root
				modifier = pleased_opinion
				opinion = 25
			}
		}

		every_courtier_or_guest = {
			limit = {
				OR = { 
					has_trait = compassionate 
					has_trait = just
					has_trait = forgiving
					has_trait = cynical
				}
			}
			custom = DE_decision_event.0024.kind_hearted_courtiers_and_guests
			add_opinion = {
				target = root
				modifier = disgusted_opinion
				opinion = -50
			}
		}

		every_courtier_or_guest = {
			limit = {
				faith = root.faith
				OR = { 
					has_trait = zealous 
					has_trait = sadistic
					has_trait = callous
				}
			}
			custom = DE_decision_event.0024.cold_hearted_courtiers_and_guests
			add_opinion = {
				target = root
				modifier = pleased_opinion
				opinion = 25
			}
		}

	#Flip Counties
	every_county_in_region = {
		region = ce_island_of_sicily
		custom = every_controlled_province_in_custom_sicily
		limit = {
			target_is_de_facto_liege_or_above = root.primary_title
			faith = faith:ashari 
		}
		set_county_faith = faith:catholic
	}

	#Echoes of the atrocity
	every_county_in_region = {
		region = ce_island_of_sicily
		custom = every_controlled_province_in_custom_sicily
		limit = {
			target_is_de_facto_liege_or_above = root.primary_title
		}
		random = {
			chance = 33
			add_county_modifier = {
				modifier = ce_echoes_of_an_atrocity_modifier
				years = 25
			}
		}
	}
}

# Form the Kingdom of Illyria
found_kingdom_of_illyria_decision_scripted_effect = {
	save_scope_as = founder
	
	capital_county = {
		save_scope_as = capital_county_effect
	}
	
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	
	title:k_illyria = {
		change_title_holder = {
			holder = root
			change = scope:title_change
		}
	}
	
	resolve_title_and_vassal_change = scope:title_change
	
	# Prestige
	add_prestige = 200

	hidden_effect = {
		title:d_croatia = { set_de_jure_liege_title = title:k_illyria }
		title:d_slavonia = { set_de_jure_liege_title = title:k_illyria }
		title:d_dalmatia = { set_de_jure_liege_title = title:k_illyria }
		title:d_lower_bosna = { set_de_jure_liege_title = title:k_illyria }
		title:d_bosna = { set_de_jure_liege_title = title:k_illyria }
		title:d_usora = { set_de_jure_liege_title = title:k_illyria }
		title:d_ragusa = { set_de_jure_liege_title = title:k_illyria }
		title:d_syrmia = { set_de_jure_liege_title = title:k_illyria }
		title:d_macva = { set_de_jure_liege_title = title:k_illyria }
		title:d_rashka = { set_de_jure_liege_title = title:k_illyria }
		title:d_duklja = { set_de_jure_liege_title = title:k_illyria }
		title:d_vidin = { set_de_jure_liege_title = title:k_illyria }
		
		every_held_title = {
			limit = {
				tier = tier_kingdom
				NOT = { this = title:k_illyria }
			}
			scope:founder = {
				destroy_title = prev
			}
		}
		
		every_held_title = {
			limit = {
				tier = tier_kingdom
				NOT = { this = title:k_illyria }
				OR = { 
					this = title:k_croatia
					this = title:k_serbia
				}
			}
			destroy_title = prev
		}

		title:k_illyria = {
			set_capital_county = scope:capital_county_effect
		}
	}
}

# Reform the Byzantine Military
reform_byz_military_decision_scripted_effect = {
	custom_tooltip = lose_old_maa_gain_new_ones

	culture = {
		if = {
			limit = {
				NOT = { has_innovation = innovation_komnenian_army }
			}
			add_innovation = innovation_komnenian_army
		}
	}
}