﻿ep3_frankokratia_save_financier_effect = {
	#Start with Venice, then any Italian republic
	title:e_italy = {
		random_in_de_jure_hierarchy = {
			limit = {
				this = title:k_venice
				title:k_venice.holder = {
					ep3_frankokratia_financier_trigger = yes
					government_has_flag = government_is_republic
				}
			}
			alternative_limit = {
				NOT = { this = title:k_venice }
				holder ?= {
					highest_held_title_tier >= tier_county
					government_has_flag = government_is_republic
					ep3_frankokratia_financier_trigger = yes
				}
			}
			holder = { save_scope_as = financier }
		}
	}
	#A Sicilian coastal republic
	if = {
		limit = {
			NOT = { exists = scope:financier }
		}
		title:k_sicily = {
			random_in_de_jure_hierarchy = {
				limit = {
					holder ?= {
						highest_held_title_tier >= tier_county
						government_has_flag = government_is_republic
						ep3_frankokratia_financier_trigger = yes
					}
				}
				holder = { save_scope_as = financier }
			}
		}
	}
	#A Croatian coastal republic
	if = {
		limit = {
			NOT = { exists = scope:financier }
		}
		title:k_croatia = {
			random_in_de_jure_hierarchy = {
				limit = {
					holder ?= {
						highest_held_title_tier >= tier_county
						government_has_flag = government_is_republic
						ep3_frankokratia_financier_trigger = yes
					}
				}
				holder = { save_scope_as = financier }
			}
		}
	}
	#A Burgundian coastal republic
	if = {
		limit = {
			NOT = { exists = scope:financier }
		}
		title:k_burgundy = {
			random_in_de_jure_hierarchy = {
				limit = {
					holder ?= {
						highest_held_title_tier >= tier_county
						government_has_flag = government_is_republic
						ep3_frankokratia_financier_trigger = yes
					}
				}
				holder = { save_scope_as = financier }
			}
		}
	}
	#An Aquitainian coastal republic
	if = {
		limit = {
			NOT = { exists = scope:financier }
		}
		title:k_aquitaine = {
			random_in_de_jure_hierarchy = {
				limit = {
					holder ?= {
						highest_held_title_tier >= tier_county
						government_has_flag = government_is_republic
						ep3_frankokratia_financier_trigger = yes
					}
				}
				holder = { save_scope_as = financier }
			}
		}
	}
	#An Iberian coastal republic
	if = {
		limit = {
			NOT = { exists = scope:financier }
		}
		title:e_spain = {
			random_in_de_jure_hierarchy = {
				limit = {
					holder ?= {
						highest_held_title_tier >= tier_county
						government_has_flag = government_is_republic
						ep3_frankokratia_financier_trigger = yes
					}
				}
				holder = { save_scope_as = financier }
			}
		}
	}
	#A wealthy Italian ruler
	if = {
		limit = {
			NOT = { exists = scope:financier }
		}
		title:e_italy = {
			random_in_de_jure_hierarchy = {
				limit = {
					holder ?= {
						highest_held_title_tier >= tier_county
						highest_held_title_tier < tier_empire
						ep3_frankokratia_financier_trigger = yes
					}
				}
				holder = { save_scope_as = financier }
			}
		}
	}
	#A wealthy Sicilian ruler
	if = {
		limit = {
			NOT = { exists = scope:financier }
		}
		title:k_sicily = {
			random_in_de_jure_hierarchy = {
				limit = {
					holder ?= {
						highest_held_title_tier >= tier_county
						highest_held_title_tier < tier_empire
						ep3_frankokratia_financier_trigger = yes
					}
				}
				holder = { save_scope_as = financier }
			}
		}
	}
	#A wealthy Croatian ruler
	if = {
		limit = {
			NOT = { exists = scope:financier }
		}
		title:k_croatia = {
			random_in_de_jure_hierarchy = {
				limit = {
					holder ?= {
						highest_held_title_tier >= tier_county
						highest_held_title_tier < tier_empire
						ep3_frankokratia_financier_trigger = yes
					}
				}
				holder = { save_scope_as = financier }
			}
		}
	}
	#Make sure they have enough money to be a financier
	hidden_effect = {
		scope:financier = {
			add_gold = 500
		}
	}
}

ep3_frankokratia_story_setup_save_vars_effect = {
	#save variables for the story
	set_variable = {
		name = ghw_sponsor
		value = scope:ghw_sponsor
	}
	set_variable = {
		name = financier_title
		value = scope:financier.primary_title
	}
	set_variable = {
		name = byz_emperor
		value = scope:byz_emperor
	}
	set_variable = {
		name = byz_claimant
		value = scope:byz_claimant
	}
	scope:byz_claimant = {
		add_character_flag = byz_claimant_flag
	}
}

ep3_frankokratia_remove_pledged_attackers_effect = {
	global_var:byz_claimant_champion ?= {
		#save neeeded scopes
		save_scope_as = byz_claimant_champion
		random_owned_story = {
			type = frankokratia_story
		 	limit = {
		 		exists = var:byz_claimant
		 	}
		 	var:byz_claimant = {
		 		save_scope_as = byz_claimant
		 	}
		}
		#Start war
		start_war = {
			casus_belli = crusading_claim_cb
			target = title:e_byzantium.holder
			claimant = scope:byz_claimant
			target_title = title:e_byzantium
		}
		random_character_war = {
			limit = { using_cb = crusading_claim_cb }
			save_scope_as = frankokratia_war
		}
		#Add the claimant if they can join
		if = {
			limit = {
				scope:byz_claimant = { is_ruler = yes }
			}
			scope:frankokratia_war = {
				add_attacker = scope:byz_claimant
			}
		}
	}
	#Remove from GHW
	if = {
		limit = {
			has_pledged_attacker = scope:byz_claimant_champion
		}
		unpledge_attacker = scope:byz_claimant_champion
	}
	if = {
		limit = {
			has_pledged_attacker = scope:byz_claimant
		}
		unpledge_attacker = scope:byz_claimant
	}
	#Redirected Crusader Fluff event
	scope:byz_claimant_champion = {
		trigger_event = ep3_frankokratia_events.0030
	}
	#Do the same for all committed rulers
	every_pledged_attacker = {
		limit = {
			has_character_flag = joining_frankokratia
			this != root.faith.religious_head
		}
		#Unpledge from GHW
		faith.great_holy_war = {
			unpledge_attacker = prev
		}
		#ADD TO WAR
		scope:frankokratia_war = {
			add_attacker = prev
		}
		#Redirected Crusader Fluff event
		trigger_event = ep3_frankokratia_events.0030
	}
	scope:byz_claimant_champion = {
		random_owned_story = {
			type = frankokratia_story
			if = {
				limit = {
					global_var:financier_title.holder ?= {
						NOT = {
							any_character_war = {
								using_cb = crusading_claim_cb
							}
						}
					}
				}
				scope:frankokratia_war = {
					add_attacker = global_var:financier_title.holder
				}
			}
			every_in_list = {
				variable = frankokratia_leaders
				limit = {
					NOT = {
						any_character_war = {
							using_cb = crusading_claim_cb
						}
					}
					is_ruler = yes
					is_alive = yes
				}
				scope:frankokratia_war = {
					add_attacker = prev
				}
				#Redirected Crusader Fluff event
				trigger_event = ep3_frankokratia_events.0030
			}
		}
	}
}

ep3_frankokratia_save_financier_sate_lands_effect = {
	if = {
		limit = {
			any_realm_county = {
				duchy = title:d_istria
				NOT = { is_in_list = financier_sate_titles }
			}
			OR = {
				#The duchy can be created
				NOT = { exists = title:d_istria.holder }
				#The duchy can be taken
				title:d_istria.holder = {
					is_vassal_or_below_of = title:e_byzantium.holder
					is_ai = yes
					NOT = { is_powerful_vassal_of = title:e_byzantium.holder }
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
		}
		title:d_istria = { save_scope_as = financier_sate_reward }
	}
	else_if = {
		limit = {
			any_realm_county = {
				duchy = title:d_ragusa
				NOT = { is_in_list = financier_sate_titles }
			}
			OR = {
				#The duchy can be created
				NOT = { exists = title:d_ragusa.holder }
				#The duchy can be taken
				title:d_ragusa.holder = {
					is_vassal_or_below_of = title:e_byzantium.holder
					is_ai = yes
					NOT = { is_powerful_vassal_of = title:e_byzantium.holder }
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
		}
		title:d_dalmatia = {
			save_scope_as = financier_sate_reward_duchy
			add_to_list = financier_sate_titles
		}
	}
	else_if = {
		limit = {
			any_realm_county = {
				duchy = title:d_dalmatia
				NOT = { is_in_list = financier_sate_titles }
			}
			OR = {
				#The duchy can be created
				NOT = { exists = title:d_dalmatia.holder }
				#The duchy can be taken
				title:d_dalmatia.holder = {
					is_vassal_or_below_of = title:e_byzantium.holder
					is_ai = yes
					NOT = { is_powerful_vassal_of = title:e_byzantium.holder }
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
		}
		title:d_dalmatia = {
			save_scope_as = financier_sate_reward_duchy
			add_to_list = financier_sate_titles
		}
	}
	else_if = {
		limit = {
			any_realm_county = {
				duchy = title:d_cephalonia
				NOT = { is_in_list = financier_sate_titles }
			}
			OR = {
				#The duchy can be created
				NOT = { exists = title:d_cephalonia.holder }
				#The duchy can be taken
				title:d_cephalonia.holder = {
					is_vassal_or_below_of = title:e_byzantium.holder
					is_ai = yes
					NOT = { is_powerful_vassal_of = title:e_byzantium.holder }
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
		}
		title:d_cephalonia = {
			save_scope_as = financier_sate_reward_duchy
			add_to_list = financier_sate_titles
		}
	}
	else_if = {
		limit = {
			any_realm_county = {
				duchy = title:d_krete
				NOT = { is_in_list = financier_sate_titles }
			}
			OR = {
				#The duchy can be created
				NOT = { exists = title:d_krete.holder }
				#The duchy can be taken
				title:d_krete.holder = {
					is_vassal_or_below_of = title:e_byzantium.holder
					is_ai = yes
					NOT = { is_powerful_vassal_of = title:e_byzantium.holder }
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
		}
		title:d_krete = {
			save_scope_as = financier_sate_reward_duchy
			add_to_list = financier_sate_titles
		}
	}
	else_if = {
		limit = {
			any_realm_county = {
				duchy = title:d_cyprus
				NOT = { is_in_list = financier_sate_titles }
			}
			OR = {
				#The duchy can be created
				NOT = { exists = title:d_cyprus.holder }
				#The duchy can be taken
				title:d_krete.holder = {
					is_vassal_or_below_of = title:e_byzantium.holder
					is_ai = yes
					NOT = { is_powerful_vassal_of = title:e_byzantium.holder }
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
		}
		title:d_cyprus = {
			save_scope_as = financier_sate_reward_duchy
			add_to_list = financier_sate_titles
		}
	}
	else_if = {
		limit = {
			any_realm_county = {
				duchy = title:d_crimea
				NOT = { is_in_list = financier_sate_titles }
			}
			OR = {
				#The duchy can be created
				NOT = { exists = title:d_crimea.holder }
				#The duchy can be taken
				title:d_crimea.holder = {
					is_vassal_or_below_of = title:e_byzantium.holder
					is_ai = yes
					NOT = { is_powerful_vassal_of = title:e_byzantium.holder }
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
		}
		title:d_crimea = {
			save_scope_as = financier_sate_reward_duchy
			add_to_list = financier_sate_titles
		}
	}
	#Moving on to counties they'd be interested in
	else_if = {
		limit = {
			any_realm_county = {
				NOT = { is_in_list = financier_sate_titles }
				duchy = title:d_istria
				has_coastal_province = yes
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
		}
		random_realm_county = {
			limit = {
				NOT = { is_in_list = financier_sate_titles }
				duchy = title:d_istria
				has_coastal_province = yes
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
			save_scope_as = financier_sate_reward_county_1
			add_to_list = financier_sate_titles
		}
	}
	else_if = {
		limit = {
			any_realm_county = {
				NOT = { is_in_list = financier_sate_titles }
				duchy = title:d_ragusa
				has_coastal_province = yes
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
		}
		random_realm_county = {
			limit = {
				NOT = { is_in_list = financier_sate_titles }
				duchy = title:d_ragusa
				has_coastal_province = yes
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
			if = {
				limit = { exists = scope:financier_sate_reward_county_1 }
				save_scope_as = financier_sate_reward_county_2
			}
			else = { save_scope_as = financier_sate_reward_county_1 }
			add_to_list = financier_sate_titles
		}
	}
	else_if = {
		limit = {
			any_realm_county = {
				NOT = { is_in_list = financier_sate_titles }
				duchy = title:d_dalmatia
				has_coastal_province = yes
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
		}
		random_realm_county = {
			limit = {
				NOT = { is_in_list = financier_sate_titles }
				duchy = title:d_dalmatia
				has_coastal_province = yes
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
			if = {
				limit = { exists = scope:financier_sate_reward_county_1 }
				save_scope_as = financier_sate_reward_county_2
			}
			else = { save_scope_as = financier_sate_reward_county_1 }
			add_to_list = financier_sate_titles
		}
	}
	else_if = {
		limit = {
			any_realm_county = {
				NOT = { is_in_list = financier_sate_titles }
				duchy = title:d_cephalonia
				has_coastal_province = yes
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
		}
		random_realm_county = {
			limit = {
				NOT = { is_in_list = financier_sate_titles }
				duchy = title:d_cephalonia
				has_coastal_province = yes
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
			if = {
				limit = { exists = scope:financier_sate_reward_county_1 }
				save_scope_as = financier_sate_reward_county_2
			}
			else = { save_scope_as = financier_sate_reward_county_1 }
			add_to_list = financier_sate_titles
		}
	}
	else_if = {
		limit = {
			any_realm_county = {
				NOT = { is_in_list = financier_sate_titles }
				duchy = title:d_krete
				has_coastal_province = yes
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
		}
		random_realm_county = {
			limit = {
				NOT = { is_in_list = financier_sate_titles }
				duchy = title:d_krete
				has_coastal_province = yes
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
			if = {
				limit = { exists = scope:financier_sate_reward_county_1 }
				save_scope_as = financier_sate_reward_county_2
			}
			else = { save_scope_as = financier_sate_reward_county_1 }
			add_to_list = financier_sate_titles
		}
	}

	else_if = {
		limit = {
			any_realm_county = {
				NOT = { is_in_list = financier_sate_titles }
				duchy = title:d_cyprus
				has_coastal_province = yes
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
		}
		random_realm_county = {
			limit = {
				NOT = { is_in_list = financier_sate_titles }
				duchy = title:d_cyprus
				has_coastal_province = yes
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
			if = {
				limit = { exists = scope:financier_sate_reward_county_1 }
				save_scope_as = financier_sate_reward_county_2
			}
			else = { save_scope_as = financier_sate_reward_county_1 }
			add_to_list = financier_sate_titles
		}
	}
	else_if = {
		limit = {
			any_realm_county = {
				NOT = { is_in_list = financier_sate_titles }
				duchy = title:d_crimea
				has_coastal_province = yes
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
		}
		random_realm_county = {
			limit = {
				NOT = { is_in_list = financier_sate_titles }
				duchy = title:d_crimea
				has_coastal_province = yes
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
			if = {
				limit = { exists = scope:financier_sate_reward_county_1 }
				save_scope_as = financier_sate_reward_county_2
			}
			else = { save_scope_as = financier_sate_reward_county_1 }
			add_to_list = financier_sate_titles
		}
	}
	#Specific counties of interest
	else_if = {
		limit = {
			any_realm_county = {
				NOT = { is_in_list = financier_sate_titles }
				this = title:c_zadar
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
		}
		random_realm_county = {
			limit = {
				NOT = { is_in_list = financier_sate_titles }
				this = title:c_zadar
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
			if = {
				limit = { exists = scope:financier_sate_reward_county_1 }
				save_scope_as = financier_sate_reward_county_2
			}
			else = { save_scope_as = financier_sate_reward_county_1 }
			add_to_list = financier_sate_titles
		}
	}
	else_if = {
		limit = {
			any_realm_county = {
				NOT = { is_in_list = financier_sate_titles }
				this = title:c_cherso
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
		}
		random_realm_county = {
			limit = {
				NOT = { is_in_list = financier_sate_titles }
				this = title:c_cherso
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
			if = {
				limit = { exists = scope:financier_sate_reward_county_1 }
				save_scope_as = financier_sate_reward_county_2
			}
			else = { save_scope_as = financier_sate_reward_county_1 }
			add_to_list = financier_sate_titles
		}
	}
	else_if = {
		limit = {
			any_realm_county = {
				NOT = { is_in_list = financier_sate_titles }
				this = title:c_dyrrachion
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
		}
		random_realm_county = {
			limit = {
				NOT = { is_in_list = financier_sate_titles }
				this = title:c_dyrrachion
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
			if = {
				limit = { exists = scope:financier_sate_reward_county_1 }
				save_scope_as = financier_sate_reward_county_2
			}
			else = { save_scope_as = financier_sate_reward_county_1 }
			add_to_list = financier_sate_titles
		}
	}
	else_if = {
		limit = {
			any_realm_county = {
				NOT = { is_in_list = financier_sate_titles }
				this = title:c_euboea
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
		}
		random_realm_county = {
			limit = {
				NOT = { is_in_list = financier_sate_titles }
				this = title:c_euboea
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
			if = {
				limit = { exists = scope:financier_sate_reward_county_1 }
				save_scope_as = financier_sate_reward_county_2
			}
			else = { save_scope_as = financier_sate_reward_county_1 }
			add_to_list = financier_sate_titles
		}
	}
	else_if = {
		limit = {
			any_realm_county = {
				NOT = { is_in_list = financier_sate_titles }
				this = title:c_messenia
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
		}
		random_realm_county = {
			limit = {
				NOT = { is_in_list = financier_sate_titles }
				this = title:c_messenia
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
			if = {
				limit = { exists = scope:financier_sate_reward_county_1 }
				save_scope_as = financier_sate_reward_county_2
			}
			else = { save_scope_as = financier_sate_reward_county_1 }
			add_to_list = financier_sate_titles
		}
	}
	#Grab any coastal county
	else_if = {
		limit = {
			any_realm_county = {
				NOT = { is_in_list = financier_sate_titles }
				has_coastal_province = yes
				holder = {
					is_ai = yes
					NOT = { is_at_war_with = title:e_byzantium.holder }
				}
			}
		}
		random_realm_county = {
			limit = {
				NOT = { is_in_list = financier_sate_titles }
				has_coastal_province = yes
				holder = {
					is_ai = yes
					NOT = {
						is_at_war_with = title:e_byzantium.holder
					}
				}
			}
			if = {
				limit = { exists = scope:financier_sate_reward_county_1 }
				save_scope_as = financier_sate_reward_county_2
			}
			else = { save_scope_as = financier_sate_reward_county_1 }
			add_to_list = financier_sate_titles
		}
	}

	#IF we got a duchy, add all the valid counties in it to the list
	if = {
		limit = {
			exists = scope:financier_sate_reward_duchy
		}
		every_realm_county = {
			limit = {
				duchy = scope:financier_sate_reward_duchy
				holder = {
					is_ai = yes
					OR = {
						this = title:e_byzantium.holder
						NOT = {
							is_at_war_with = title:e_byzantium.holder
						}
					}
				}
			}
			add_to_list = financier_sate_titles
		}
	}
}

ep3_frankokratia_give_financier_sate_lands_effect = {
	#If there's a duchy to give, give all titles in it
	if = {
		limit = {
			exists = scope:financier_sate_reward_duchy
		}
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = title_change
			add_claim_on_loss = yes
		}
		every_in_list = {
			list = financier_sate_titles
			change_title_holder = {
				holder = scope:financier
				change = scope:title_change
				take_baronies = yes
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	else = {
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = title_change
			add_claim_on_loss = yes
		}
		scope:financier_sate_reward_county_1 = {
			change_title_holder = {
				holder = scope:financier
				change = scope:title_change
				take_baronies = yes
			}
		}
		if = {
			limit = {
				exists = scope:financier_sate_reward_county_2
			}
			scope:financier_sate_reward_county_2 = {
				change_title_holder = {
					holder = scope:financier
					change = scope:title_change
					take_baronies = yes
				}
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
}

ep3_frankokratia_gather_governor_funds_effect = {
	if = {
		limit = {
			any_vassal = {
				is_governor = yes
				NOT = { is_at_war_with = title:e_byzantium.holder }
			}
		}
		every_vassal = {
			limit = {
				is_governor = yes
				NOT = { is_at_war_with = title:e_byzantium.holder }
			}
			hidden_effect = {
				pay_short_term_gold = {
					target = title:e_byzantium.holder
					gold = 150
				}
			}
			add_to_list = paying_off_crusaders
		}
	}
	else_if = {
		limit = {
			any_vassal = {
				is_governor = no
				NOT = { is_at_war_with = title:e_byzantium.holder }
			}
		}
		every_vassal = {
			limit = {
				is_governor = no
				NOT = { is_at_war_with = title:e_byzantium.holder }
			}
			hidden_effect = {
				pay_short_term_gold = {
					target = title:e_byzantium.holder
					gold = 50
				}
			}
			add_to_list = paying_off_crusaders
		}
	}
	scope:attacker = {
		random_owned_story = {
			type = frankokratia_story
			save_scope_as = frankokratia_story
		}
	}
	save_scope_value_as = {
		name = crusader_payoff_total
		value = scope:attacker.pay_off_crusaders_value
	}
	save_scope_value_as = {
		name = payoff_per_crusader
		value = {
			value = 0
			add = scope:crusader_payoff_total
			divide = {
				value = 1
				scope:frankokratia_story = {
					every_in_list = {
						variable = frankokratia_leaders
						limit = {
							this != scope:attacker
						}
						add = 1
					}
				}
				subtract = 1
				min = 1	
			}
		}
	}
}

ep3_frankokratia_capital_sacking_effect = {
	custom_tooltip = latin_empire_creation_tooltip
	custom_tooltip = latin_empire_land_distribution_tooltip
	add_dread = massive_dread_gain
	custom_tooltip = {
		text = every_crusader_gets_paid_sacking
		add_short_term_gold = ep3_sacking_byz_capital_payoff_value
	}
	custom_tooltip = byzantine_empire_destroyed_tooltip
	
	hidden_effect = {
		scope:frankokratia_story = {
			every_in_list = {
				variable = frankokratia_leaders
				limit = {
					this != scope:attacker
				}
				add_dread = medium_dread_gain
			}
		}
	}
	
	scope:claimant.capital_county = {
		change_county_control = -100
		if = {
			limit = {
				scope:claimant.capital_county = {
					development_level >= 90
				}
			}
			change_development_level = -20
		}
		else_if = {
			limit = {
				scope:claimant.capital_county = {
					development_level >= 60
				}
			}
			change_development_level = -15
		}
		else_if = {
			limit = {
				scope:claimant.capital_county = {
					development_level >= 40
				}
			}
			change_development_level = -10
		}
		else = {
			change_development_level = -5
		}
		custom_tooltip = byz_capital_building_destroyed_tooltip
	}
	scope:claimant.primary_title = {
		state_faith = {
			change_fervor = {
				value = major_fervor_value
				desc = imperial_capital_sacked_fervor_gain
			}
		}
	}
	
	hidden_effect = {
		scope:claimant.capital_province = {
			destroy_random_building_variable_effect = yes
			destroy_random_building_effect = yes
		}
		scope:claimant.capital_province = {
			destroy_random_building_variable_effect = yes
			destroy_random_building_effect = yes
		}
		scope:claimant.capital_province = {
			destroy_random_building_variable_effect = yes
			destroy_random_building_effect = yes
		}
	}
	
	faith:catholic = {
		change_fervor = {
			value = medium_fervor_loss
			desc = imperial_capital_sacked_fervor_gain
		}
	}
	hidden_effect = {
		if = {
			limit = {
				NOT = {
					culture = {
						this = scope:claimant.capital_county.culture
					}
				}
			}
			culture = {
				change_cultural_acceptance = {
					target = scope:claimant.capital_county.culture
					value = massive_cultural_acceptance_loss
					desc = sacked_imperial_capital_cultural_acceptance
				}
			}
		}
		if = {
			limit = {
				NOR = {
					scope:financier.culture = {
						this = root.culture
					}
					scope:financier.culture = {
						this = scope:claimant.capital_county.culture
					}
				}
			}
			scope:financier.culture = {
				change_cultural_acceptance = {
					target = scope:claimant.capital_county.culture
					value = massive_cultural_acceptance_loss
					desc = sacked_imperial_capital_cultural_acceptance
				}
			}
		}
	}
	hidden_effect = {
		#Destroy emperor's personal MaA
		scope:claimant = {
			if = {
				limit = {
					any_maa_regiment = {
						is_title_maa_regiment = no
						is_maa_type = cataphract
					}
				}
				random_maa_regiment = {
					limit = {
						is_title_maa_regiment = no
						is_maa_type = cataphract
					}
					destroy_maa_regiment = yes
				}
			}
			else_if = {
				limit = {
					any_maa_regiment = {
						is_title_maa_regiment = no
					}
				}
				random_maa_regiment = {
					limit = {
						is_title_maa_regiment = no
					}
					destroy_maa_regiment = yes
				}
			}
		}
	}
	if = {
		limit = {
			is_allied_to = scope:claimant
		}
		break_alliance = scope:claimant
	}
	
	add_trait = despoiler_of_byzantium
	custom_tooltip = frankokratia_leaders_gain_despoiler_trait
	hidden_effect = {
		scope:frankokratia_story = {
			every_in_list = {
				variable = frankokratia_leaders
				limit = {
					this != scope:attacker
				}
				add_trait = despoiler_of_byzantium
			}
		}
	}
	trigger_event = ep3_frankokratia_events.0042
}

ep3_frankokratia_kill_byzantines_effect = {
	#Byzantines (and misc locals) are slaughtered in the taking of the city
	title:e_byzantium.holder = {
		#Courtiers of emperor
		every_courtier_or_guest = {
			limit = {
				NOT = {
					any_heir_title = {
						holder ?= {
							this = global_var:byz_claimant_champion
						}
					}
				}
			}
			add_to_list = byz_civilians
		}
		#Local pool characters
		every_pool_character = {
			province = title:e_byzantium.holder.capital_province
			limit = {
				NOT = {
					any_heir_title = {
						holder ?= {
							this = global_var:byz_claimant_champion
						}
					}
				}
			}
			add_to_list = byz_civilians
		}
		#ai landless house heads in the capital
		every_vassal = {
			limit = {
				is_ai = yes
				is_landless_administrative = yes
				domicile ?= {
					domicile_location.county = {
						this = title:e_byzantium.holder.capital_county
					}
				}
			}
			add_to_list = byz_civilians
		}
		#Courtiers of landless house heads in the capital
		every_vassal = {
			limit = {
				is_landless_administrative = yes
				domicile ?= {
					domicile_location.county = {
						this = title:e_byzantium.holder.capital_county
					}
				}
			}
			every_courtier_or_guest = {
				limit = {
					NOT = {
						any_heir_title = {
							holder ?= {
								this = global_var:byz_claimant_champion
							}
						}
					}
				}
				add_to_list = byz_civilians
			}
		}
	}
	ordered_in_list = {
		order_by = {
			value = 1
			#I suppose they would probably try to avoid killing Catholics... if possible
			if = {
				limit = {
					NOR = {
						faith = faith:catholic
						this = scope:defender
						is_close_family_of = scope:defender
					}
				}
				add = 1
			}
		}
		list = byz_civilians
		max = {
			value = list_size:byz_civilians
			divide = 3
		}
		add_to_global_variable_list = {
		 	name = slaughtered_byzantines_list
		 	target = this
		 	days = 30
		}
	}
	set_global_variable = {
		name = slaughtered_byzantines
		value = {
			value = 0
			every_in_global_list = {
				variable = slaughtered_byzantines_list
				add = 1
			}
		}
		days = 30
	}
	
	#Let everyone know this happened
	scope:frankokratia_story = {
		every_in_list = {
			variable = frankokratia_leaders
			limit = {
				is_ai = no
			}
			add_to_list = byz_slaughter_recipients
		}
	}
	title:e_byzantium.holder = {
		if = {
			limit = {
				is_ai = no
			}
			add_to_list = byz_slaughter_recipients
		}
		every_vassal_or_below = {
			limit = {
				is_ai = no
			}
			add_to_list = byz_slaughter_recipients
		}
	}
	every_in_list = {
		list = byz_slaughter_recipients
		trigger_event = ep3_frankokratia_events.0046
	}
	
	hidden_effect = {
		every_in_global_list = {
			variable = slaughtered_byzantines_list
			death = {
				death_reason = death_frankokratia_sacking
			}
		}
	}
}

ep3_frankokratia_paying_off_latins_effect = {
	#If crusader state exists - all troops go to crusader king and crusaders get piety
	if = {
		limit = {
			scope:frankokratia_story = {
				has_variable = crusader_kingdom
				exists = var:crusader_kingdom.holder
				var:crusader_kingdom.holder = {
					faith = scope:attacker.faith
					NOR = {
						is_at_war_with = scope:attacker
						is_at_war_with = scope:claimant
					}
				}
			}
		}
		scope:frankokratia_story = {
			var:crusader_kingdom.holder = {
				save_scope_as = crusader_king
			}
		}
		scope:crusader_king = {
			spawn_army = {
				name = ep3_frankokratia_reward_army_name
				uses_supply = yes
				levies = 3400
				men_at_arms = {
					type = cataphract
					stacks = ep3_byz_regiments_ck_value
				}
				location = scope:claimant.capital_province
			}
		}
		scope:attacker = {
			add_piety = massive_piety_gain
		}
		if = {
			limit = {
				scope:attacker = {
					has_trait = excommunicated
				}
			}
			scope:attacker = { remove_trait = excommunicated }
		}
		custom_tooltip = frankokratia_leaders_lose_excommunicated
	}
	else = {
		scope:attacker = {
			add_piety = major_piety_gain
			spawn_army = {
				name = ep3_frankokratia_reward_army_name
				uses_supply = yes
				levies = ep3_byz_reward_levy_size_value
				men_at_arms = {
					type = cataphract
					stacks = ep3_byz_regiments_gained_value
				}
				location = scope:claimant.capital_province
				inheritable = no
			}
			custom_tooltip = frankokratia_leaders_gain_identical_force
		}
	}
	if = {
		limit = {
			exists = scope:financier_sate_reward_duchy
		}
		show_as_tooltip = {
			create_title_and_vassal_change = {
				type = granted
				save_scope_as = title_change
				add_claim_on_loss = yes
			}
			scope:financier_sate_reward_duchy = {
				change_title_holder = {
					holder = scope:financier
					change = scope:title_change
					take_baronies = yes
				}
			}
			resolve_title_and_vassal_change = scope:title_change
		}
		hidden_effect = {
			title:e_byzantium.holder = {
				ep3_frankokratia_give_financier_sate_lands_effect = yes
			}
		}
	}
	else_if = {
		limit = {
			exists = scope:financier_sate_reward_county_1
		}
		title:e_byzantium.holder = {
			ep3_frankokratia_give_financier_sate_lands_effect = yes
		}
	}
	add_legitimacy = major_legitimacy_loss
	add_tyranny = massive_tyranny_gain
	
	add_character_modifier = {
		modifier = bent_before_crusaders_modifier
		years = 10
	}
	#Army transfer - Emperor loses MaA regiments
	if = {
		limit = {
			exists = scope:regiment_2
		}
		custom_tooltip = 2_byzantine_regiments_given_over_tooltip
		hidden_effect = {
			scope:regiment_1 = { destroy_maa_regiment = yes }
		}
	}
	else_if = {
		limit = {
			exists = scope:regiment_1
		}
		custom_tooltip = 1_byzantine_regiment_given_over_tooltip
		hidden_effect = {
			scope:regiment_1 = { destroy_maa_regiment = yes }
			scope:regiment_2 = { destroy_maa_regiment = yes }
		}
	}
	
	capital_county = {
		add_county_modifier = {
			modifier = crippled_by_imperial_debt_modifier
			years = 25
		}
	}

	# Show how much gold is paid out to every crusader
	ep3_frankokratia_gather_governor_funds_effect = yes
	custom_tooltip = {
		text = every_crusader_gets_paid
		scope:frankokratia_story = {
			every_in_list = {
				variable = frankokratia_leaders
				scope:claimant = {
					pay_short_term_gold = {
						target = prev
						gold = scope:payoff_per_crusader
					}
				}
			}
		}
	}
}

ep3_frankokratia_designate_latin_empire_effect = {
	#GO FOR CONSTANTINOPLE/THESSALONIKA AS CAPITAL KINGDOM FIRST
	if = {
		limit = {
			title:e_byzantium.holder = {
				any_realm_county = {
					this = title:c_byzantion
				}
			}
			title:c_byzantion.holder = {
				is_ai = yes
				OR = {
					this = title:e_byzantium.holder
					NOT = {
						is_at_war_with = title:e_byzantium.holder
					}
				}
			}
		}
		title:c_byzantion = { save_scope_as = latin_capital }
		title:b_constantinople = {
			save_scope_as = latin_capital_barony
		}
		#Try to save thessalonika as the most natural capital kingdom
		if = {
			limit = {
				title:k_thessalonika = {
					ep3_frankokratia_latin_kingdom_available_trigger = yes
				}
			}
			title:k_thessalonika = { save_scope_as = latin_kingdom_1 }
		}
		#Start going through other capital kingdoms in order of logic - they really should have one
		else_if = {
			limit = {
				title:k_nikaea = {
					ep3_frankokratia_latin_kingdom_available_trigger = yes
				}
			}
			title:k_nikaea = { save_scope_as = latin_kingdom_1 }
		}
		else_if = {
			limit = {
				title:k_hellas = {
					ep3_frankokratia_latin_kingdom_available_trigger = yes
				}
			}
			title:k_hellas = { save_scope_as = latin_kingdom_1 }
		}
		else_if = {
			limit = {
				title:k_bulgaria = {
					ep3_frankokratia_latin_kingdom_available_trigger = yes
				}
			}
			title:k_bulgaria = { save_scope_as = latin_kingdom_1 }
		}
		else_if = {
			limit = {
				title:e_byzantium = {
					any_in_de_jure_hierarchy = {
						tier = tier_kingdom
						ep3_frankokratia_latin_kingdom_available_trigger = yes
						any_de_jure_county = {
							is_coastal_county = yes
						}
					}
				}
			}
			title:e_byzantium = {
				random_in_de_jure_hierarchy = {
					limit = {
						tier = tier_kingdom
						ep3_frankokratia_latin_kingdom_available_trigger = yes
						any_de_jure_county = {
							is_coastal_county = yes
						}
					}
					save_scope_as = latin_kingdom_1
				}
			}
		}
		else_if = {
			limit = {
				NOT = { exists = scope:latin_kingdom_1 }
			}
			title:e_byzantium = {
				random_in_de_jure_hierarchy = {
					limit = {
						tier = tier_kingdom
						ep3_frankokratia_latin_kingdom_available_trigger = yes
					}
					save_scope_as = latin_kingdom_1
				}
			}
		}
		#The latins also took Hellas, first prio
		if = {
			limit = {
				title:e_byzantium.holder = {
					any_realm_county = {
						kingdom = title:k_hellas
						title:k_hellas = {
							this != scope:latin_kingdom_1
							ep3_frankokratia_latin_kingdom_available_trigger = yes
						}
					}
				}
			}
			title:k_hellas = { save_scope_as = latin_kingdom_2 }
		}
		#The latins had a short-lived attempt at ruling southern Bulgaria
		else_if = {
			limit = {
				title:e_byzantium.holder = {
					any_realm_county = {
						kingdom = title:k_bulgaria
						title:k_bulgaria = {
							this != scope:latin_kingdom_1
							ep3_frankokratia_latin_kingdom_available_trigger = yes
						}
					}
				}
			}
			title:k_bulgaria = { save_scope_as = latin_kingdom_2 }
		}
		#Krete, why not
		else_if = {
			limit = {
				title:e_byzantium.holder = {
					any_realm_county = {
						kingdom = title:k_krete
						title:k_krete = {
							this != scope:latin_kingdom_1
							ep3_frankokratia_latin_kingdom_available_trigger = yes
						}
					}
				}
			}
			title:k_krete = { save_scope_as = latin_kingdom_2 }
		}
		#The latins had some of Nikaea I guess
		else_if = {
			limit = {
				title:e_byzantium.holder = {
					any_realm_county = {
						kingdom = title:k_nikaea
						title:k_nikaea = {
							this != scope:latin_kingdom_1
							ep3_frankokratia_latin_kingdom_available_trigger = yes
						}
					}
				}
			}
			title:k_nikaea = { save_scope_as = latin_kingdom_2 }
		}
		#Cyprus, why not
		else_if = {
			limit = {
				title:e_byzantium.holder = {
					any_realm_county = {
						kingdom = title:k_cyprus
						title:k_cyprus = {
							this != scope:latin_kingdom_1
							ep3_frankokratia_latin_kingdom_available_trigger = yes
						}
					}
				}
			}
			title:k_cyprus = { save_scope_as = latin_kingdom_2 }
		}
	}
	#NO CONSTANTINOPLE - WHATEVER THE IMPERIAL CAPITAL IS
	else = {
		title:e_byzantium.holder.capital_county = {
			save_scope_as = latin_capital
		}
		title:e_byzantium.holder.capital_barony = {
			save_scope_as = latin_capital_barony
		}
		#Is the kingdom available as capital kingdom?
		if = {
			limit = {
				scope:latin_capital.kingdom = {
					ep3_frankokratia_latin_kingdom_available_trigger = yes
				}
			}
			scope:latin_capital.kingdom = { save_scope_as = latin_kingdom_1 }
		}
		#Is any neighboring kingdom available to be capital?
		else_if = {
			limit = {
				title:e_byzantium = {
					any_in_de_jure_hierarchy = {
						tier = tier_kingdom
						ep3_frankokratia_latin_kingdom_available_trigger = yes
						any_de_jure_county = {
							#Belongs to the empire
							OR = {
								holder = {
									this = title:e_byzantium.holder
								}
								holder = {
									is_vassal_or_below_of = title:e_byzantium.holder
									NOT = {
										is_at_war_with = title:e_byzantium.holder
									}
								}
							}
							#Neighbours main kingdom
							any_neighboring_county = {
								kingdom = scope:latin_capital.kingdom
							}
						}
					}
				}
			}
			title:e_byzantium = {
				random_in_de_jure_hierarchy = {
					limit = {
						tier = tier_kingdom
						OR = {
							NOT = { exists = holder }
							holder = {
								this = title:e_byzantium.holder
							}
						}
						any_de_jure_county = {
							#Belongs to the empire
							OR = {
								holder = title:e_byzantium.holder
								holder = {
									is_vassal_or_below_of = title:e_byzantium.holder
									NOT = {
										is_at_war_with = title:e_byzantium.holder
									}
								}
							}
							#Neighbours main kingdom
							any_neighboring_county = {
								kingdom = scope:latin_capital.kingdom
							}
						}
					}
					save_scope_as = latin_kingdom_1
				}
			}
		}
		#Need to start looking at random capital kingdoms
		else_if = {
			limit = {
				title:e_byzantium = {
					any_in_de_jure_hierarchy = {
						tier = tier_kingdom
						ep3_frankokratia_latin_kingdom_available_trigger = yes
						any_de_jure_county = {
							is_coastal_county = yes
						}
					}
				}
			}
			title:e_byzantium = {
				random_in_de_jure_hierarchy = {
					limit = {
						tier = tier_kingdom
						ep3_frankokratia_latin_kingdom_available_trigger = yes
						any_de_jure_county = {
							is_coastal_county = yes
						}
					}
					save_scope_as = latin_kingdom_1
				}
			}
		}
		else_if = {
			limit = {
				NOT = { exists = scope:latin_kingdom_1 }
			}
			title:e_byzantium = {
				random_in_de_jure_hierarchy = {
					limit = {
						tier = tier_kingdom
						ep3_frankokratia_latin_kingdom_available_trigger = yes
					}
					save_scope_as = latin_kingdom_1
				}
			}
		}
		#Give a random neighboring kingdom as a second one
		if = {
			limit = {
				title:e_byzantium = {
					any_in_de_jure_hierarchy = {
						tier = tier_kingdom
						this != scope:latin_kingdom_1
						ep3_frankokratia_latin_kingdom_available_trigger = yes
						any_de_jure_county = {
							#Belongs to the empire
							OR = {
								holder = {
									this = title:e_byzantium.holder
								}
								holder = {
									is_vassal_or_below_of = title:e_byzantium.holder
									NOT = {
										is_at_war_with = title:e_byzantium.holder
									}
								}
							}
							#Neighbours main kingdom
							any_neighboring_county = {
								kingdom = scope:latin_kingdom_1
							}
						}
					}
				}
			}
			title:e_byzantium = {
				random_in_de_jure_hierarchy = {
					limit = {
						tier = tier_kingdom
						this != scope:latin_kingdom_1
						ep3_frankokratia_latin_kingdom_available_trigger = yes
						any_de_jure_county = {
							#Belongs to the empire
							OR = {
								holder = {
									this = title:e_byzantium.holder
								}
								holder = {
									is_vassal_or_below_of = title:e_byzantium.holder
									NOT = {
										is_at_war_with = title:e_byzantium.holder
									}
								}
							}
							#Neighbours main kingdom
							any_neighboring_county = {
								kingdom = scope:latin_kingdom_1
							}
						}
					}
					save_scope_as = latin_kingdom_2
				}
			}
		}
	}
	scope:latin_capital = {
		add_to_list = latin_emperor_titles
	}
	scope:latin_kingdom_1 ?= {
		add_to_list = latin_emperor_titles
		set_global_variable = {
			name = latin_kingdom_1
			value = scope:latin_kingdom_1
			days = 60
		}
	}
	scope:latin_kingdom_2 ?= {
		add_to_list = latin_emperor_titles
		set_global_variable = {
			name = latin_kingdom_2
			value = scope:latin_kingdom_2
			days = 60
		}
	}
	title:e_latin_empire = {
		add_to_list = latin_emperor_titles
	}
}

ep3_latin_empire_designate_financier_lands_effect = {
	#First, give aegean islands counties
	title:e_byzantium.holder = {
		if = {
			limit = {
				scope:latin_capital.kingdom = {
					this = title:k_thessalonika
				}
				any_realm_county = {
					NOT = { is_in_list = financier_sate_titles }
					duchy = title:d_aegean_islands
					holder = {
						is_ai = yes
						NOT = {
							is_at_war_with = title:e_byzantium.holder
						}
					}
				}
			}
			title:d_aegean_islands = {
				save_scope_as = financier_latin_emp_duchy
			}
		}
		#Not possible? Give them counties from a duchy with a coastal province in the latin kingdom
		else_if = {
			limit = {
				any_realm_county = {
					NOR = {
						is_in_list = financier_sate_titles
						this = scope:latin_capital
					}
					kingdom = scope:latin_capital.kingdom
					has_coastal_province = yes
					holder = {
						is_ai = yes
						NOT = {
							is_at_war_with = title:e_byzantium.holder
						}
					}
				}
			}
			random_realm_county = {
				limit = {
					NOR = {
						is_in_list = financier_sate_titles
						this = scope:latin_capital
					}
					kingdom = scope:latin_capital.kingdom
					has_coastal_province = yes
					holder = {
						is_ai = yes
						NOT = {
							is_at_war_with = title:e_byzantium.holder
						}
					}
				}
				duchy = { save_scope_as = financier_latin_emp_duchy }
			}
		}
		#Not possible? Give them counties from a duchy with a coastal province that borders the latin kingdom
		else_if = {
			limit = {
				any_realm_county = {
					any_neighboring_county = {
						kingdom = scope:latin_capital.kingdom
					}
					NOR = {
						is_in_list = financier_sate_titles
						this = scope:latin_capital
					}
					has_coastal_province = yes
					holder = {
						is_ai = yes
						NOT = {
							is_at_war_with = title:e_byzantium.holder
						}
					}
				}
			}
			random_realm_county = {
				limit = {
					NOR = {
						is_in_list = financier_sate_titles
						this = scope:latin_capital
					}
					any_neighboring_county = {
						kingdom = scope:latin_capital.kingdom
					}
					has_coastal_province = yes
					holder = {
						is_ai = yes
						NOT = {
							is_at_war_with = title:e_byzantium.holder
						}
					}
				}
				duchy = { save_scope_as = financier_latin_emp_duchy }
			}
		}
		#Not possible? Run the sate rewards effect again and add more to that list
		else = {
			ep3_frankokratia_save_financier_sate_lands_effect = yes
		}
	
		#Give all counties in the duchy
		if = {
			limit = {
				exists = scope:financier_latin_emp_duchy
			}
			every_realm_county = {
				limit = {
					duchy = scope:financier_latin_emp_duchy
					holder = {
						is_ai = yes
						NOT = {
							is_at_war_with = title:e_byzantium.holder
						}
					}
				}
				add_to_list = financier_latin_emp_titles
			}
		}
		#Give the duchy the financier's counties are in, if possible
		if = {
			limit = {
				scope:financier_latin_emp_duchy ?= {
					NOT = { is_in_list = financier_sate_titles }
					OR = {
						NOT = { exists = holder }
						holder = {
							is_ai = yes
							NOT = {
								is_at_war_with = title:e_byzantium.holder
							}
							OR = {
								this = title:e_byzantium.holder
								is_vassal_or_below_of = title:e_byzantium.holder
							}
						}
					}
				}
			}
			scope:financier_latin_emp_duchy = {
				add_to_list = financier_latin_emp_titles
			}
		}
		#GIVE THE SATE REWARD TITLES as well as the prior stuff
		every_in_list = {
			list = financier_sate_titles
			add_to_list = financier_latin_emp_titles
		}
	}
}

ep3_frankokratia_grant_latin_empire_effect = {
	#Make it less likely frankokratia will happen again
	set_global_variable = {
		name = frankokratia_occurred 
		value = current_date
	}
	scope:latin_capital.kingdom = {
		every_de_jure_county = {
			limit = {
				this != scope:latin_capital
				holder = {
					OR = {
						this = title:e_byzantium.holder
						AND = {
							is_ai = yes
							is_vassal_or_below_of = title:e_byzantium.holder
							NOT = {
								is_at_war_with = title:e_byzantium.holder
							}
						}
					}
					this != scope:financier
				}
			}
			add_to_list = latin_emperor_titles
		}
	}
	scope:latin_capital = {
		if = {
			limit = {
				duchy = {
					OR = {
						NOT = { exists = holder }
						holder = {
							is_ai = yes
							OR = {
								is_vassal_or_below_of = title:e_byzantium.holder
								this = title:e_byzantium.holder
							}
							NOT = {
								is_at_war_with = title:e_byzantium.holder
							}
						}
					}
				}
			}
			duchy = {
				add_to_list = latin_emperor_titles
			}
		}
	}
	show_as_tooltip = {
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = title_change
			add_claim_on_loss = yes
		}
		every_in_list = {
			list = latin_emperor_titles
			limit = {
				tier > tier_county
			}
			change_title_holder = {
				holder = scope:latin_emperor
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	hidden_effect = {
		#Titles for emperor
		create_title_and_vassal_change = {
			type = created
			save_scope_as = title_change
			add_claim_on_loss = yes
		}
		every_in_list = {
			list = latin_emperor_titles
			change_title_holder = {
				holder = scope:latin_emperor
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
	}
	scope:latin_emperor = {
		#Gains Latin Empire special troops
		if = {
			limit = {
				scope:frankokratia_story = {
					any_in_list = {
						variable = frankokratia_leaders
						culture = {
							has_cultural_parameter = unlock_maa_conrois
						}
					}
					any_in_list = {
						variable = frankokratia_leaders
						culture = {
							has_innovation = innovation_advanced_bowmaking
						}
					}
				}
			}
			spawn_army = {
				name = ep3_latin_emp_army_name
				uses_supply = yes
				inheritable = yes
				levies = latin_emp_army_levies_value
				men_at_arms = {
					type = conrois
					stacks = latin_emp_army_maa_value
				}
				men_at_arms = {
					type = crossbowmen
					stacks = latin_emp_army_maa_value
				}
				location = scope:latin_capital_barony.title_province
			}
		}
		else_if = {
			limit = {
				scope:frankokratia_story = {
					any_in_list = {
						variable = frankokratia_leaders
						culture = {
							has_cultural_parameter = unlock_maa_conrois
						}
					}
					any_in_list = {
						variable = frankokratia_leaders
						culture = {
							has_innovation = innovation_quilted_armor
						}
					}
				}
				
			}
			spawn_army = {
				name = ep3_latin_emp_army_name
				uses_supply = yes
				inheritable = yes
				levies = latin_emp_army_levies_value
				men_at_arms = {
					type = conrois
					stacks = latin_emp_army_maa_value
				}
				men_at_arms = {
					type = armored_footmen
					stacks = latin_emp_army_maa_value
				}
				location = scope:latin_capital_barony.title_province
			}
		}
		else_if = {
			limit = {
				scope:frankokratia_story = {
					any_in_list = {
						variable = frankokratia_leaders
						culture = {
							has_cultural_parameter = unlock_maa_conrois
						}
					}
				}
			}
			spawn_army = {
				name = ep3_latin_emp_army_name
				uses_supply = yes
				inheritable = yes
				levies = latin_emp_army_levies_value
				men_at_arms = {
					type = conrois
					stacks = latin_emp_army_maa_value
				}
				men_at_arms = {
					type = pikemen_unit
					stacks = latin_emp_army_maa_value
				}
				location = scope:latin_capital_barony.title_province
			}
		}
		else_if = {
			limit = {
				scope:frankokratia_story = {
					any_in_list = {
						variable = frankokratia_leaders
						culture = {
							has_innovation = innovation_arched_saddle
						}
					}
					any_in_list = {
						variable = frankokratia_leaders
						culture = {
							has_innovation = innovation_advanced_bowmaking
						}
					}
				}
			}
			spawn_army = {
				name = ep3_latin_emp_army_name
				uses_supply = yes
				inheritable = yes
				levies = latin_emp_army_levies_value
				men_at_arms = {
					type = armored_horsemen
					stacks = latin_emp_army_maa_value
				}
				men_at_arms = {
					type = crossbowmen
					stacks = latin_emp_army_maa_value
				}
				location = scope:latin_capital_barony.title_province
			}
		}
		else_if = {
			limit = {
				scope:frankokratia_story = {
					any_in_list = {
						variable = frankokratia_leaders
						culture = {
							has_innovation = innovation_arched_saddle
						}
					}
					any_in_list = {
						variable = frankokratia_leaders
						culture = {
							has_innovation = innovation_quilted_armor
						}
					}
				}
			}
			spawn_army = {
				name = ep3_latin_emp_army_name
				uses_supply = yes
				inheritable = yes
				levies = latin_emp_army_levies_value
				men_at_arms = {
					type = armored_horsemen
					stacks = latin_emp_army_maa_value
				}
				men_at_arms = {
					type = armored_footmen
					stacks = latin_emp_army_maa_value
				}
				location = scope:latin_capital_barony.title_province
			}
		}
		else_if = {
			limit = {
				scope:frankokratia_story = {
					any_in_list = {
						variable = frankokratia_leaders
						culture = {
							has_innovation = innovation_arched_saddle
						}
					}
				}
			}
			spawn_army = {
				name = ep3_latin_emp_army_name
				uses_supply = yes
				inheritable = yes
				levies = latin_emp_army_levies_value
				men_at_arms = {
					type = armored_horsemen
					stacks = latin_emp_army_maa_value
				}
				men_at_arms = {
					type = pikemen_unit
					stacks = latin_emp_army_maa_value
				}
				location = scope:latin_capital_barony.title_province
			}
		}
		else_if = {
			limit = {
				scope:frankokratia_story = {
					any_in_list = {
						variable = frankokratia_leaders
						culture = {
							has_innovation = innovation_quilted_armor
						}
					}
					any_in_list = {
						variable = frankokratia_leaders
						culture = {
							has_innovation = innovation_advanced_bowmaking
						}
					}
				}
			}
			spawn_army = {
				name = ep3_latin_emp_army_name
				uses_supply = yes
				inheritable = yes
				levies = latin_emp_army_levies_value
				men_at_arms = {
					type = armored_footmen
					stacks = latin_emp_army_maa_value
				}
				men_at_arms = {
					type = crossbowmen
					stacks = latin_emp_army_maa_value
				}
				location = scope:latin_capital_barony.title_province
			}
		}
		else_if = {
			limit = {
				scope:frankokratia_story = {
					any_in_list = {
						variable = frankokratia_leaders
						culture = {
							has_innovation = innovation_quilted_armor
						}
					}
				}
			}
			spawn_army = {
				name = ep3_latin_emp_army_name
				uses_supply = yes
				inheritable = yes
				levies = latin_emp_army_levies_value
				men_at_arms = {
					type = armored_footmen
					stacks = latin_emp_army_maa_value
				}
				men_at_arms = {
					type = light_horsemen
					stacks = latin_emp_army_maa_value
				}
				location = scope:latin_capital_barony.title_province
			}
		}
		else_if = {
			limit = {
				scope:frankokratia_story = {
					any_in_list = {
						variable = frankokratia_leaders
						culture = {
							has_innovation = innovation_advanced_bowmaking
						}
					}
				}
			}
			spawn_army = {
				name = ep3_latin_emp_army_name
				uses_supply = yes
				inheritable = yes
				levies = latin_emp_army_levies_value
				men_at_arms = {
					type = crossbowmen
					stacks = latin_emp_army_maa_value
				}
				men_at_arms = {
					type = light_horsemen
					stacks = latin_emp_army_maa_value
				}
				location = scope:latin_capital_barony.title_province
			}
		}
		else = {
			spawn_army = {
				name = ep3_latin_emp_army_name
				uses_supply = yes
				inheritable = yes
				levies = latin_emp_army_levies_value
				men_at_arms = {
					type = light_horsemen
					stacks = latin_emp_army_maa_value
				}
				men_at_arms = {
					type = pikemen_unit
					stacks = latin_emp_army_maa_value
				}
				location = scope:latin_capital_barony.title_province
			}
		}
	}
	#Change succession law?
	scope:latin_emperor = {
		add_realm_law = crown_authority_2
	}
	#Stop this admin stuff
	if = {
		limit = {
			scope:latin_emperor = {
				government_has_flag = government_is_administrative
			}
		}
		scope:latin_emperor = {
			change_government = feudal_government
		}
	}
}

ep3_frankokratia_clear_old_lands_change_capital_effect = {
	if = {
		limit = {
			ep3_frankokratia_discard_old_lands_trigger = yes
			exists = scope:latin_kingdom_1
			exists = scope:latin_kingdom_2
			this = scope:latin_empire_receiver
		}
		custom_tooltip = old_titles_vassals_emp_beneficiary_2_kings_tooltip
	}
	else_if = {
		limit = {
			ep3_frankokratia_discard_old_lands_trigger = yes
			exists = scope:latin_kingdom_1
			this = scope:latin_empire_receiver
		}
		custom_tooltip = old_titles_vassals_emp_beneficiary_1_king_tooltip
	}
	else_if = {
		limit = {
			this = scope:latin_empire_receiver
		}
		custom_tooltip = old_titles_vassals_emp_beneficiary_no_king_tooltip
	}
	else = {
		custom_tooltip = old_titles_vassals_lord_beneficiary_tooltip
	}
	if = {
		limit = {
			this != scope:latin_empire_receiver
		}
		scope:beneficiary = {
			save_scope_as = old_title_gainer
		}
	}
	else = {
		scope:emp_beneficiary = {
			save_scope_as = old_title_gainer
		}
	}
	hidden_effect = {
		if = {
			limit = {
				OR = {
					any_in_list = {
						list = root_old_vassals
						count >= 1
					}
					any_in_list = {
						list = root_old_titles
						count >= 1
					}
				}
			}
			#Remove all of character's other titles - pass to heir?
			create_title_and_vassal_change = {
				type = granted
				save_scope_as = change
				add_claim_on_loss = no
			}
			every_in_list = {
				list = root_old_vassals
				change_liege = {
					liege = scope:old_title_gainer
					change = scope:change
				}
			}
			every_in_list = {
				list = root_old_titles
				change_title_holder = {
					holder = scope:old_title_gainer
					change = scope:change
				}
			}
			resolve_title_and_vassal_change = scope:change
			#Need to give titles back to their former liege
			if = {
				limit = {
					has_variable = old_liege
				}
				var:old_liege = {
					save_scope_as = old_liege
				}
				scope:old_title_gainer = {
					create_title_and_vassal_change = {
						type = granted
						save_scope_as = change
						add_claim_on_loss = no
					}
					change_liege = {
						liege = scope:old_liege
						change = scope:change
					}
					resolve_title_and_vassal_change = scope:change
				}
			}
			##Or need to make titles independent
			else_if = {
				limit = {
					scope:old_title_gainer = {
						highest_held_title_tier < tier_empire
					}
				}
				scope:old_title_gainer = {
					create_title_and_vassal_change = {
						type = independency
						save_scope_as = change
						add_claim_on_loss = no
					}
					becomes_independent = { change = scope:change }
					resolve_title_and_vassal_change = scope:change
				}
			}
		}
		if = {
			limit = {
				any_realm_county = {
					this = title:c_byzantion
				}
				capital_county = {
					NOT = {
						this = title:c_byzantion
					}
				}
			}
			set_realm_capital = title:c_byzantion
		}
	}
}

ep3_frankokratia_distribute_lands_to_crusaders_effect = {

	#save a list of all titles there are to give out
	#Counties in latin kingdom
	every_in_list = {
		list = latin_emperor_titles
		limit = {
			NOR = {
				this = scope:latin_capital
				#Don't give out financier's lands, unless financier is latin emp
				holder = {
					this = scope:financier
					this != scope:latin_emperor
				}
				this = title:c_byzantion
			}
			tier = tier_county
			NOT = {
				duchy = {
					holder = scope:latin_emperor
				}
			}
		}
		add_to_list = available_latin_kingdom_counties
		add_to_list = available_latin_emp_counties
	}
	#Counties on border with latin capital
	scope:latin_emperor = {
		every_realm_county = {
			limit = {
				this = scope:latin_capital
			}
			every_neighboring_county = {
				limit = {
					OR = {
						holder = title:e_byzantium.holder
						holder = {
							is_ai = yes
							is_vassal_or_below_of = title:e_byzantium.holder
							NOT = {
								is_at_war_with = title:e_byzantium.holder
							}
						}
					}
				}
				add_to_list = available_latin_neighbor_counties
				add_to_list = available_latin_emp_counties
			}
		}
	}
	#Border counties belonging to kingdom owned by latin emperor
	if = {
		limit = {
			exists = scope:latin_kingdom_2
		}
		scope:latin_emperor = {
			every_realm_county = {
				every_neighboring_county = {
					limit = {
						kingdom = scope:latin_kingdom_2
						OR = {
							holder = title:e_byzantium.holder
							holder = {
								is_ai = yes
								is_vassal_or_below_of = title:e_byzantium.holder
								NOT = {
									is_at_war_with = title:e_byzantium.holder
								}
							}
						}
					}
					add_to_list = available_latin_neighbor_counties
					add_to_list = available_latin_emp_counties
				}
			}
		}
	}
	
	##Save duchies in the latin emp that can be given out
	if = {
		limit = {
			any_in_list = {
				list = available_latin_kingdom_counties
				duchy = {
					OR = {
						NOT = { exists = holder }
						holder = title:e_byzantium.holder
						holder = {
							is_ai = yes
							is_vassal_or_below_of = title:e_byzantium.holder
							NOT = {
								is_at_war_with = title:e_byzantium.holder
							}
						}
					}
				}
			}
		}
		every_in_list = {
			list = available_latin_kingdom_counties
			limit = {
				duchy = {
					OR = {
						NOT = { exists = holder }
						holder = title:e_byzantium.holder
						holder = {
							is_ai = yes
							is_vassal_or_below_of = title:e_byzantium.holder
							NOT = {
								is_at_war_with = title:e_byzantium.holder
							}
						}
						NOT = {
							holder = {
								this = scope:financier
								this != scope:latin_emperor
							}
						}
					}
				}
			}
			duchy = {
				add_to_list = available_latin_kingdom_duchies
				add_to_list = available_latin_emp_duchies
			}
		}
	}
	#Save neighboring duchies that can be given out
	if = {
		limit = {
			any_in_list = {
				list = available_latin_neighbor_counties
				duchy = {
					OR = {
						NOT = { exists = holder }
						holder = title:e_byzantium.holder
						holder = {
							is_ai = yes
							is_vassal_or_below_of = title:e_byzantium.holder
							NOT = {
								is_at_war_with = title:e_byzantium.holder
							}
						}
					}
				}
			}
		}
		every_in_list = {
			list = available_latin_neighbor_counties
			limit = {
				duchy = {
					OR = {
						NOT = { exists = holder }
						holder = title:e_byzantium.holder
						holder = {
							is_ai = yes
							is_vassal_or_below_of = title:e_byzantium.holder
							NOT = {
								is_at_war_with = title:e_byzantium.holder
							}
						}
						NOT = {
							holder = {
								this = scope:financier
								this != scope:latin_emperor
							}
						}
					}
				}
			}
			duchy = {
				add_to_list = available_latin_neighbor_duchies
				add_to_list = available_latin_emp_duchies
			}
		}
	}

	while = {
		limit = {
			#Ends when counties or empire duchies (which should all have empire counties) are out
			OR = {
				any_in_list = {
					list = available_latin_kingdom_counties
					count >= 1
				}
				any_in_list = {
					list = available_latin_emp_duchies
					count >= 1
				}
			}
		}
		if = {
			limit = {
				scope:frankokratia_story = {
					any_in_list = {
						variable = frankokratia_leaders
						is_ruler = yes
						NOR = {
							is_in_list = designated_latin_emp_titles
							this = scope:financier
							this = scope:latin_empire_receiver
						}
					}
				}
			}
			#Save player first
			if = {
				limit = {
					scope:frankokratia_story = {
						any_in_list = {
							variable = frankokratia_leaders
							is_ruler = yes
							is_ai = no
							NOR = {
								is_in_list = designated_latin_emp_titles
								this = scope:financier
								this = scope:latin_empire_receiver
							}
						}
					}
				}
				#Grant duchy and county to strongest player
				scope:frankokratia_story = {
					ordered_in_list = {
						variable = frankokratia_leaders
						order_by = max_military_strength
						limit = {
							is_ruler = yes
							is_ai = no
							NOR = {
								is_in_list = designated_latin_emp_titles
								this = scope:financier
								this = scope:latin_empire_receiver
							}
						}
						add_to_list = designated_latin_emp_titles
						save_scope_as = latin_title_candidate
					}
				}
			}
			#save AI
			else = {
				scope:frankokratia_story = {
					ordered_in_list = {
						variable = frankokratia_leaders
						order_by = max_military_strength
						limit = {
							is_ruler = yes
							NOR = {
								is_in_list = designated_latin_emp_titles
								this = scope:financier
								this = scope:latin_empire_receiver
							}
						}
						add_to_list = designated_latin_emp_titles
						save_scope_as = latin_title_candidate
					}
				}
			}
			#Save titles they're getting

			#Give them a latin kingdom duchy and county
			if = {
				limit = {
					any_in_list = {
						list = available_latin_kingdom_duchies
						count >= 1
					}
				}
				random_in_list = {
					list = available_latin_kingdom_duchies
					remove_from_list = available_latin_kingdom_duchies
					remove_from_list = available_latin_emp_duchies
					save_scope_as = duchy_to_grant
					title_capital_county = { save_scope_as = duchy_capital }
				}
				random_in_list = {
					list = available_latin_kingdom_counties
					save_temporary_scope_as = county_temp
					limit = {
						this = scope:duchy_capital
					}
					alternative_limit = {
						duchy = scope:duchy_to_grant
					}
					remove_from_list = available_latin_kingdom_counties
					remove_from_list = available_latin_emp_counties
					save_scope_as = county_to_grant
				}
				scope:latin_title_candidate = {
					#Set the variable so it can be granted when character gets event
					set_variable = {
						name = latin_duchy
						value = scope:duchy_to_grant
					}
					set_variable = {
						name = latin_county
						value = scope:county_to_grant
					}
				}
			}
			#Give them a neighboring county and duchy
			else_if = {
				limit = {
					any_in_list = {
						list = available_latin_neighbor_duchies
						count >= 1
					}
				}
				random_in_list = {
					list = available_latin_neighbor_duchies
					remove_from_list = available_latin_neighbor_duchies
					remove_from_list = available_latin_emp_duchies
					save_scope_as = duchy_to_grant
					title_capital_county = { save_scope_as = duchy_capital }
				}
				random_in_list = {
					list = available_latin_neighbor_counties
					save_temporary_scope_as = county_temp
					limit = {
						duchy = scope:duchy_to_grant
					}
					remove_from_list = available_latin_neighbor_counties
					remove_from_list = available_latin_emp_counties
					save_scope_as = county_to_grant
				}
				scope:latin_title_candidate = {
					#Set the variable so it can be granted when character gets event
					set_variable = {
						name = latin_duchy
						value = scope:duchy_to_grant
					}
					set_variable = {
						name = latin_county
						value = scope:county_to_grant
					}
				}
			}
			#Give them a latin kingdom county
			else = {
				random_in_list = {
					list = available_latin_kingdom_counties
					remove_from_list = available_latin_kingdom_counties
					remove_from_list = available_latin_emp_counties
					save_scope_as = county_to_grant
				}
				scope:latin_title_candidate = {
					#Set the variable so it can be granted when character gets event
					set_variable = {
						name = latin_county
						value = scope:county_to_grant
					}
				}
			}
		}
		#No one is left to give titles to - end this
		else = {
			every_in_list = {
				list = available_latin_emp_counties
				remove_from_list = available_latin_emp_counties
				if = {
					limit = { is_in_list = available_latin_kingdom_counties }
					remove_from_list = available_latin_kingdom_counties
				}
				if = {
					limit = { is_in_list = available_latin_neighbor_counties }
					remove_from_list = available_latin_neighbor_counties
				}
				if = {
					limit = { is_in_list = available_latin_emp_counties }
					remove_from_list = available_latin_emp_counties
				}
			}
		}
		
		
			#Grant county to strongest player

			#Grant duchy and county to strongest ai
			#Grant county to strongest ai
		#Order them by military strength?
		#Start with every county that isn't capital in kingdom of latin emp goes to a crusader
		#Any bordering counties in other kingdom latins have
		#Any additional bordering counties, as needed until ll crusaders get one
		#Any coastal county in empire

		#Save flag that removes them from this process

		#Add to beneficiary titles list
	}
}

ep3_frankokratia_grant_latin_lands_effect = {
	root = {
		var:latin_county = {
			add_to_list = my_latin_titles
		}
		if = {
			limit = {
				has_variable = latin_duchy
			}
			var:latin_duchy = {
				add_to_list = my_latin_titles
			}
			destroy_title = var:latin_duchy
		}
	}


	create_title_and_vassal_change = {
		type = granted
		save_scope_as = title_change
	}
	every_in_list = {
		list = my_latin_titles
		change_title_holder = {
			holder = scope:latin_title_gainer
			change = scope:title_change
		}
	}
	scope:latin_title_gainer = {
		change_liege = {
			liege = scope:latin_emperor
			change = scope:title_change
		}
	}
	resolve_title_and_vassal_change = scope:title_change
	

	#Remove so this doesn't cause title stealing
	every_in_list = {
		list = my_latin_titles
		remove_from_list = my_latin_titles
	}

	
	#Make sure they're feudal
	if = {
		limit = {
			scope:latin_empire_receiver = {
				this = scope:financier
			}
		}
		if = {
			limit = {
				OR = {
					religion = religion:islam_religion
					culture = { has_cultural_pillar = heritage_iranian }
					culture = { has_cultural_pillar = heritage_turkic }
					culture = { has_cultural_pillar = heritage_arabic }
				}
			}
			scope:latin_title_gainer = { change_government = clan_government }
		}
		else = {
			scope:latin_title_gainer = { change_government = feudal_government }
		}
	}
}

ep3_latin_empire_grant_financier_lands_effect = {
	add_to_list = gained_latin_emp_titles
	#Title switch
	create_title_and_vassal_change = {
		type = conquest
		save_scope_as = title_change
		add_claim_on_loss = yes
	}
	every_in_list = {
		list = financier_latin_emp_titles
		change_title_holder = {
			holder = scope:financier
			change = scope:title_change
			take_baronies = yes
		}
	}
	
	resolve_title_and_vassal_change = scope:title_change
}

ep3_frankokratia_pick_emperor_beneficiary_effect = {
	save_scope_as = frankokratia_crusader
	#SET FIRST emp_beneficiary VARIABLE
	#Dominant gender close family
	random_close_family_member = {
		limit = {
			ep3_frankokratia_beneficiary_trigger = yes
			ep3_frankokratia_beneficiary_gender_trigger = yes
			dynasty = scope:frankokratia_crusader.dynasty
		}
		scope:frankokratia_crusader = {
			set_variable = {
				name = emp_beneficiary
				value = prev
			}
		}
	}
	#Dominant gender house member
	if = {
		limit = {
			NOT = { has_variable = emp_beneficiary }
		}
		house = {
			random_house_member = {
				limit = {
					ep3_frankokratia_beneficiary_trigger = yes
					ep3_frankokratia_beneficiary_gender_trigger = yes
				}
				scope:frankokratia_crusader = {
					set_variable = {
						name = emp_beneficiary
						value = prev
					}
				}
			}
		}
	}
	#Dominant gender dynasty member
	if = {
		limit = {
			NOT = { has_variable = emp_beneficiary }
		}
		dynasty = {
			random_dynasty_member = {
				limit = {
					ep3_frankokratia_beneficiary_trigger = yes
					ep3_frankokratia_beneficiary_gender_trigger = yes
				}
				scope:frankokratia_crusader = {
					set_variable = {
						name = emp_beneficiary
						value = prev
					}
				}
			}
		}
	}
	#Close family any gender
	if = {
		limit = {
			NOT = { has_variable = emp_beneficiary }
		}
		random_close_family_member = {
			limit = {
				ep3_frankokratia_beneficiary_trigger = yes
				ep3_frankokratia_beneficiary_gender_soft_trigger = yes
			}
			scope:frankokratia_crusader = {
				set_variable = {
					name = emp_beneficiary
					value = prev
				}
			}
		}
	}
	#dynasty member any gender
	if = {
		limit = {
			NOT = { has_variable = emp_beneficiary }
		}
		dynasty = {
			random_dynasty_member = {
				limit = {
					ep3_frankokratia_beneficiary_trigger = yes
					ep3_frankokratia_beneficiary_gender_soft_trigger = yes
				}
				scope:frankokratia_crusader = {
					set_variable = {
						name = emp_beneficiary
						value = prev
					}
				}
			}
		}
	}
	#Same culture dominant gender courtier
	if = {
		limit = {
			NOT = { has_variable = emp_beneficiary }
		}
		random_courtier_or_guest = {
			limit = {
				ep3_frankokratia_beneficiary_trigger = yes
				culture = scope:frankokratia_crusader.culture
				ep3_frankokratia_beneficiary_gender_trigger = yes
			}
			scope:frankokratia_crusader = {
				set_variable = {
					name = emp_beneficiary
					value = prev
				}
			}
		}
	}
	#any courtier
	if = {
		limit = {
			NOT = { has_variable = emp_beneficiary }
		}
		random_courtier_or_guest = {
			limit = {
				ep3_frankokratia_beneficiary_trigger = yes
				culture = scope:frankokratia_crusader.culture
				ep3_frankokratia_beneficiary_gender_soft_trigger = yes
			}
			scope:frankokratia_crusader = {
				set_variable = {
					name = emp_beneficiary
					value = prev
				}
			}
		}
	}
	#pool character
	if = {
		limit = {
			NOT = { has_variable = emp_beneficiary }
		}
		random_pool_character = {
			province = scope:frankokratia_crusader.capital_province
			limit = {
				ep3_frankokratia_beneficiary_trigger = yes
			}
			scope:frankokratia_crusader = {
				set_variable = {
					name = emp_beneficiary
					value = prev
				}
			}
		}
	}
	if = {
		limit = {
			NOT = { has_variable = emp_beneficiary }
		}
		#Create character
		create_character = {
			location = scope:claimant.location
			template = ep3_frankokratia_crusader_template
			faith = scope:frankokratia_crusader.faith
			culture = scope:frankokratia_crusader.culture
			save_scope_as = new_latin_lord
		}
		scope:frankokratia_crusader = {
			set_variable = {
				name = emp_beneficiary
				value = scope:new_latin_lord
			}
		}
	}
	var:emp_beneficiary = { save_scope_as = emp_beneficiary }

	#SET SECOND emp_beneficiary VARIABLE
	##Dominant gender close family
	random_close_family_member = {
		limit = {
			ep3_frankokratia_beneficiary_trigger = yes
			ep3_frankokratia_beneficiary_gender_trigger = yes
			dynasty = scope:frankokratia_crusader.dynasty
			this != scope:emp_beneficiary
		}
		scope:frankokratia_crusader = {
			set_variable = {
				name = emp_beneficiary_2
				value = prev
			}
		}
	}
	#Dominant gender house member
	if = {
		limit = {
			NOT = { has_variable = emp_beneficiary_2 }
		}
		house = {
			random_house_member = {
				limit = {
					ep3_frankokratia_beneficiary_trigger = yes
					ep3_frankokratia_beneficiary_gender_trigger = yes
					this != scope:emp_beneficiary
				}
				scope:frankokratia_crusader = {
					set_variable = {
						name = emp_beneficiary_2
						value = prev
					}
				}
			}
		}
	}
	#Dominant gender dynasty member
	if = {
		limit = {
			NOT = { has_variable = emp_beneficiary_2 }
		}
		dynasty = {
			random_dynasty_member = {
				limit = {
					ep3_frankokratia_beneficiary_trigger = yes
					ep3_frankokratia_beneficiary_gender_trigger = yes
					this != scope:emp_beneficiary
				}
				scope:frankokratia_crusader = {
					set_variable = {
						name = emp_beneficiary_2
						value = prev
					}
				}
			}
		}
	}
	#Close family any gender
	if = {
		limit = {
			NOT = { has_variable = emp_beneficiary_2 }
		}
		random_close_family_member = {
			limit = {
				ep3_frankokratia_beneficiary_trigger = yes
				ep3_frankokratia_beneficiary_gender_soft_trigger = yes
				this != scope:emp_beneficiary
			}
			scope:frankokratia_crusader = {
				set_variable = {
					name = emp_beneficiary_2
					value = prev
				}
			}
		}
	}
	#dynasty member any gender
	if = {
		limit = {
			NOT = { has_variable = emp_beneficiary_2 }
		}
		dynasty = {
			random_dynasty_member = {
				limit = {
					ep3_frankokratia_beneficiary_trigger = yes
					ep3_frankokratia_beneficiary_gender_soft_trigger = yes
					this != scope:emp_beneficiary
				}
				scope:frankokratia_crusader = {
					set_variable = {
						name = emp_beneficiary_2
						value = prev
					}
				}
			}
		}
	}
	#Same culture dominant gender courtier
	if = {
		limit = {
			NOT = { has_variable = emp_beneficiary_2 }
		}
		random_courtier_or_guest = {
			limit = {
				ep3_frankokratia_beneficiary_trigger = yes
				culture = scope:frankokratia_crusader.culture
				ep3_frankokratia_beneficiary_gender_trigger = yes
				this != scope:emp_beneficiary
			}
			scope:frankokratia_crusader = {
				set_variable = {
					name = emp_beneficiary_2
					value = prev
				}
			}
		}
	}
	#any courtier
	if = {
		limit = {
			NOT = { has_variable = emp_beneficiary_2 }
		}
		random_courtier_or_guest = {
			limit = {
				ep3_frankokratia_beneficiary_trigger = yes
				culture = scope:frankokratia_crusader.culture
				ep3_frankokratia_beneficiary_gender_soft_trigger = yes
				this != scope:emp_beneficiary
			}
			scope:frankokratia_crusader = {
				set_variable = {
					name = emp_beneficiary_2
					value = prev
				}
			}
		}
	}
	#pool character
	if = {
		limit = {
			NOT = { has_variable = emp_beneficiary_2 }
		}
		random_pool_character = {
			province = scope:frankokratia_crusader.capital_province
			limit = {
				ep3_frankokratia_beneficiary_trigger = yes
				this != scope:emp_beneficiary
			}
			scope:frankokratia_crusader = {
				set_variable = {
					name = emp_beneficiary_2
					value = prev
				}
			}
		}
	}
	if = {
		limit = {
			has_variable = emp_beneficiary_2
		}
		var:emp_beneficiary_2 = { save_scope_as = emp_beneficiary_2 }
	}

	if = {
		limit = {
			has_variable = emp_beneficiary_2
		}
		#SET THIRD emp_beneficiary VARIABLE
		##Dominant gender close family
		random_close_family_member = {
			limit = {
				ep3_frankokratia_beneficiary_trigger = yes
				ep3_frankokratia_beneficiary_gender_trigger = yes
				dynasty = scope:frankokratia_crusader.dynasty
				NOR = {
					this = scope:emp_beneficiary
					this = scope:emp_beneficiary_2
				}
			}
			scope:frankokratia_crusader = {
				set_variable = {
					name = emp_beneficiary_3
					value = prev
				}
			}
		}
		#Dominant gender house member
		if = {
			limit = {
				NOT = { has_variable = emp_beneficiary_3 }
			}
			house = {
				random_house_member = {
					limit = {
						ep3_frankokratia_beneficiary_trigger = yes
						ep3_frankokratia_beneficiary_gender_trigger = yes
						NOR = {
							this = scope:emp_beneficiary
							this = scope:emp_beneficiary_2
						}
					}
					scope:frankokratia_crusader = {
						set_variable = {
							name = emp_beneficiary_3
							value = prev
						}
					}
				}
			}
			
		}
		#Dominant gender dynasty member
		if = {
			limit = {
				NOT = { has_variable = emp_beneficiary_3 }
			}
			dynasty = {
				random_dynasty_member = {
					limit = {
						ep3_frankokratia_beneficiary_trigger = yes
						ep3_frankokratia_beneficiary_gender_trigger = yes
						NOR = {
							this = scope:emp_beneficiary
							this = scope:emp_beneficiary_2
						}
					}
					scope:frankokratia_crusader = {
						set_variable = {
							name = emp_beneficiary_3
							value = prev
						}
					}
				}
			}
		}
		#Close family any gender
		if = {
			limit = {
				NOT = { has_variable = emp_beneficiary_3 }
			}
			random_close_family_member = {
				limit = {
					ep3_frankokratia_beneficiary_trigger = yes
					ep3_frankokratia_beneficiary_gender_soft_trigger = yes
					NOR = {
						this = scope:emp_beneficiary
						this = scope:emp_beneficiary_2
					}
				}
				scope:frankokratia_crusader = {
					set_variable = {
						name = emp_beneficiary_3
						value = prev
					}
				}
			}
		}
		#dynasty member any gender
		if = {
			limit = {
				NOT = { has_variable = emp_beneficiary_3 }
			}
			dynasty = {
				random_dynasty_member = {
					limit = {
						ep3_frankokratia_beneficiary_trigger = yes
						ep3_frankokratia_beneficiary_gender_soft_trigger = yes
						NOR = {
							this = scope:emp_beneficiary
							this = scope:emp_beneficiary_2
						}
					}
					scope:frankokratia_crusader = {
						set_variable = {
							name = emp_beneficiary_3
							value = prev
						}
					}
				}
			}
		}
		#Same culture dominant gender courtier
		if = {
			limit = {
				NOT = { has_variable = emp_beneficiary_3 }
			}
			random_courtier_or_guest = {
				limit = {
					ep3_frankokratia_beneficiary_trigger = yes
					culture = scope:frankokratia_crusader.culture
					ep3_frankokratia_beneficiary_gender_trigger = yes
					NOR = {
						this = scope:emp_beneficiary
						this = scope:emp_beneficiary_2
					}
				}
				scope:frankokratia_crusader = {
					set_variable = {
						name = emp_beneficiary_3
						value = prev
					}
				}
			}
		}
		#any courtier
		if = {
			limit = {
				NOT = { has_variable = emp_beneficiary_3 }
			}
			random_courtier_or_guest = {
				limit = {
					ep3_frankokratia_beneficiary_trigger = yes
					culture = scope:frankokratia_crusader.culture
					ep3_frankokratia_beneficiary_gender_soft_trigger = yes
					NOR = {
						this = scope:emp_beneficiary
						this = scope:emp_beneficiary_2
					}
				}
				scope:frankokratia_crusader = {
					set_variable = {
						name = emp_beneficiary_3
						value = prev
					}
				}
			}
		}
		#pool character
		if = {
			limit = {
				NOT = { has_variable = emp_beneficiary_3 }
			}
			random_pool_character = {
				province = scope:frankokratia_crusader.capital_province
				limit = {
					ep3_frankokratia_beneficiary_trigger = yes
					NOR = {
						this = scope:emp_beneficiary
						this = scope:emp_beneficiary_2
					}
				}
				scope:frankokratia_crusader = {
					set_variable = {
						name = emp_beneficiary_3
						value = prev
					}
				}
			}
		}
		if = {
			limit = {
				has_variable = emp_beneficiary_3
			}
			var:emp_beneficiary_3 = { save_scope_as = emp_beneficiary_3 }
		}
	}
}

ep3_frankokratia_pick_crusader_beneficiary_effect = {
	save_scope_as = frankokratia_crusader
	#SET FIRST BENEFICIARY VARIABLE
	#Dominant gender close family
	random_close_family_member = {
		limit = {
			ep3_frankokratia_beneficiary_trigger = yes
			ep3_frankokratia_beneficiary_gender_trigger = yes
			dynasty = scope:frankokratia_crusader.dynasty
		}
		scope:frankokratia_crusader = {
			set_variable = {
				name = beneficiary
				value = prev
			}
		}
	}
	#Dominant gender house member
	if = {
		limit = {
			NOT = { has_variable = beneficiary }
		}
		house ?= {
			random_house_member = {
				limit = {
					ep3_frankokratia_beneficiary_trigger = yes
					ep3_frankokratia_beneficiary_gender_trigger = yes
				}
				scope:frankokratia_crusader = {
					set_variable = {
						name = beneficiary
						value = prev
					}
				}
			}
		}
		
	}
	#Dominant gender dynasty member
	if = {
		limit = {
			NOT = { has_variable = beneficiary }
		}
		dynasty ?= {
			random_dynasty_member = {
				limit = {
					ep3_frankokratia_beneficiary_trigger = yes
					ep3_frankokratia_beneficiary_gender_trigger = yes
				}
				scope:frankokratia_crusader = {
					set_variable = {
						name = beneficiary
						value = prev
					}
				}
			}
		}
	}
	#Close family any gender
	if = {
		limit = {
			NOT = { has_variable = beneficiary }
		}
		random_close_family_member = {
			limit = {
				ep3_frankokratia_beneficiary_trigger = yes
				ep3_frankokratia_beneficiary_gender_soft_trigger = yes
			}
			scope:frankokratia_crusader = {
				set_variable = {
					name = beneficiary
					value = prev
				}
			}
		}
	}
	#dynasty member any gender
	if = {
		limit = {
			NOT = { has_variable = beneficiary }
		}
		dynasty ?= {
			random_dynasty_member = {
				limit = {
					ep3_frankokratia_beneficiary_trigger = yes
					ep3_frankokratia_beneficiary_gender_soft_trigger = yes
				}
				scope:frankokratia_crusader = {
					set_variable = {
						name = beneficiary
						value = prev
					}
				}
			}
		}
	}
	#Same culture dominant gender courtier
	if = {
		limit = {
			NOT = { has_variable = beneficiary }
		}
		random_courtier_or_guest = {
			limit = {
				ep3_frankokratia_beneficiary_trigger = yes
				culture = scope:frankokratia_crusader.culture
				ep3_frankokratia_beneficiary_gender_trigger = yes
			}
			scope:frankokratia_crusader = {
				set_variable = {
					name = beneficiary
					value = prev
				}
			}
		}
	}
	#any courtier
	if = {
		limit = {
			NOT = { has_variable = beneficiary }
		}
		random_courtier_or_guest = {
			limit = {
				ep3_frankokratia_beneficiary_trigger = yes
				culture = scope:frankokratia_crusader.culture
				ep3_frankokratia_beneficiary_gender_soft_trigger = yes
			}
			scope:frankokratia_crusader = {
				set_variable = {
					name = beneficiary
					value = prev
				}
			}
		}
	}
	#pool character
	if = {
		limit = {
			NOT = { has_variable = beneficiary }
		}
		random_pool_character = {
			province = scope:frankokratia_crusader.capital_province
			limit = {
				ep3_frankokratia_beneficiary_trigger = yes
			}
			scope:frankokratia_crusader = {
				set_variable = {
					name = beneficiary
					value = prev
				}
			}
		}
	}
	if = {
		limit = {
			NOT = { has_variable = beneficiary }
		}
		#Create character
		create_character = {
			location = scope:latin_emperor.location
			template = ep3_frankokratia_crusader_template
			faith = scope:frankokratia_crusader.faith
			culture = scope:frankokratia_crusader.culture
			save_scope_as = new_latin_lord
		}
		scope:frankokratia_crusader = {
			set_variable = {
				name = beneficiary
				value = scope:new_latin_lord
			}
		}
	}


	#SET SECOND BENEFICIARY VARIABLE
	##Dominant gender close family
	random_close_family_member = {
		limit = {
			ep3_frankokratia_beneficiary_trigger = yes
			ep3_frankokratia_beneficiary_gender_trigger = yes
			dynasty = scope:frankokratia_crusader.dynasty
			this != scope:frankokratia_crusader.var:beneficiary
		}
		scope:frankokratia_crusader = {
			set_variable = {
				name = beneficiary_2
				value = prev
			}
		}
	}
	#Dominant gender house member
	if = {
		limit = {
			NOT = { has_variable = beneficiary_2 }
		}
		house ?= {
			random_house_member = {
				limit = {
					ep3_frankokratia_beneficiary_trigger = yes
					ep3_frankokratia_beneficiary_gender_trigger = yes
					this != scope:frankokratia_crusader.var:beneficiary
				}
				scope:frankokratia_crusader = {
					set_variable = {
						name = beneficiary_2
						value = prev
					}
				}
			}
		}
	}
	#Dominant gender dynasty member
	if = {
		limit = {
			NOT = { has_variable = beneficiary_2 }
		}
		dynasty ?= {
			random_dynasty_member = {
				limit = {
					ep3_frankokratia_beneficiary_trigger = yes
					ep3_frankokratia_beneficiary_gender_trigger = yes
					this != scope:frankokratia_crusader.var:beneficiary
				}
				scope:frankokratia_crusader = {
					set_variable = {
						name = beneficiary_2
						value = prev
					}
				}
			}
		}
	}
	#Close family any gender
	if = {
		limit = {
			NOT = { has_variable = beneficiary_2 }
		}
		random_close_family_member = {
			limit = {
				ep3_frankokratia_beneficiary_trigger = yes
				ep3_frankokratia_beneficiary_gender_soft_trigger = yes
				this != scope:frankokratia_crusader.var:beneficiary
			}
			scope:frankokratia_crusader = {
				set_variable = {
					name = beneficiary_2
					value = prev
				}
			}
		}
	}
	#dynasty member any gender
	if = {
		limit = {
			NOT = { has_variable = beneficiary_2 }
		}
		dynasty ?= {
			random_dynasty_member = {
				limit = {
					ep3_frankokratia_beneficiary_trigger = yes
					ep3_frankokratia_beneficiary_gender_soft_trigger = yes
					this != scope:frankokratia_crusader.var:beneficiary
				}
				scope:frankokratia_crusader = {
					set_variable = {
						name = beneficiary_2
						value = prev
					}
				}
			}
		}
	}
	#Same culture dominant gender courtier
	if = {
		limit = {
			NOT = { has_variable = beneficiary_2 }
		}
		random_courtier_or_guest = {
			limit = {
				ep3_frankokratia_beneficiary_trigger = yes
				culture = scope:frankokratia_crusader.culture
				ep3_frankokratia_beneficiary_gender_trigger = yes
				this != scope:frankokratia_crusader.var:beneficiary
			}
			scope:frankokratia_crusader = {
				set_variable = {
					name = beneficiary_2
					value = prev
				}
			}
		}
	}
	#any courtier
	if = {
		limit = {
			NOT = { has_variable = beneficiary_2 }
		}
		random_courtier_or_guest = {
			limit = {
				ep3_frankokratia_beneficiary_trigger = yes
				culture = scope:frankokratia_crusader.culture
				ep3_frankokratia_beneficiary_gender_soft_trigger = yes
				this != scope:frankokratia_crusader.var:beneficiary
			}
			scope:frankokratia_crusader = {
				set_variable = {
					name = beneficiary_2
					value = prev
				}
			}
		}
	}
	#pool character
	if = {
		limit = {
			NOT = { has_variable = beneficiary_2 }
		}
		random_pool_character = {
			province = scope:frankokratia_crusader.capital_province
			limit = {
				ep3_frankokratia_beneficiary_trigger = yes
				this != scope:frankokratia_crusader.var:beneficiary
			}
			scope:frankokratia_crusader = {
				set_variable = {
					name = beneficiary_2
					value = prev
				}
			}
		}
	}
	if = {
		limit = {
			has_variable = beneficiary_2
		}
		#SET THIRD BENEFICIARY VARIABLE
		##Dominant gender close family
		random_close_family_member = {
			limit = {
				ep3_frankokratia_beneficiary_trigger = yes
				ep3_frankokratia_beneficiary_gender_trigger = yes
				dynasty = scope:frankokratia_crusader.dynasty
				NOR = {
					this = scope:frankokratia_crusader.var:beneficiary
					this = scope:frankokratia_crusader.var:beneficiary_2
				}
			}
			scope:frankokratia_crusader = {
				set_variable = {
					name = beneficiary_3
					value = prev
				}
			}
		}#Dominant gender house member
		if = {
			limit = {
				NOT = { has_variable = beneficiary_3 }
			}
			house ?= {
				random_house_member = {
					limit = {
						ep3_frankokratia_beneficiary_trigger = yes
						ep3_frankokratia_beneficiary_gender_trigger = yes
						NOR = {
							this = scope:frankokratia_crusader.var:beneficiary
							this = scope:frankokratia_crusader.var:beneficiary_2
						}
					}
					scope:frankokratia_crusader = {
						set_variable = {
							name = beneficiary_3
							value = prev
						}
					}
				}
			}
			
		}
		#Dominant gender dynasty member
		if = {
			limit = {
				NOT = { has_variable = beneficiary_3 }
			}
			dynasty ?= {
				random_dynasty_member = {
					limit = {
						ep3_frankokratia_beneficiary_trigger = yes
						ep3_frankokratia_beneficiary_gender_trigger = yes
						NOR = {
							this = scope:frankokratia_crusader.var:beneficiary
							this = scope:frankokratia_crusader.var:beneficiary_2
						}
					}
					scope:frankokratia_crusader = {
						set_variable = {
							name = beneficiary_3
							value = prev
						}
					}
				}
			}
		}
		#Close family any gender
		if = {
			limit = {
				NOT = { has_variable = beneficiary_3 }
			}
			random_close_family_member = {
				limit = {
					ep3_frankokratia_beneficiary_trigger = yes
					ep3_frankokratia_beneficiary_gender_soft_trigger = yes
					NOR = {
						this = scope:frankokratia_crusader.var:beneficiary
						this = scope:frankokratia_crusader.var:beneficiary_2
					}
				}
				scope:frankokratia_crusader = {
					set_variable = {
						name = beneficiary_3
						value = prev
					}
				}
			}
		}
		#dynasty member any gender
		if = {
			limit = {
				NOT = { has_variable = beneficiary_3 }
			}
			dynasty ?= {
				random_dynasty_member = {
					limit = {
						ep3_frankokratia_beneficiary_trigger = yes
						ep3_frankokratia_beneficiary_gender_soft_trigger = yes
						NOR = {
							this = scope:frankokratia_crusader.var:beneficiary
							this = scope:frankokratia_crusader.var:beneficiary_2
						}
					}
					scope:frankokratia_crusader = {
						set_variable = {
							name = beneficiary_3
							value = prev
						}
					}
				}
			}
		}
		#Same culture dominant gender courtier
		if = {
			limit = {
				NOT = { has_variable = beneficiary_3 }
			}
			random_courtier_or_guest = {
				limit = {
					ep3_frankokratia_beneficiary_trigger = yes
					culture = scope:frankokratia_crusader.culture
					ep3_frankokratia_beneficiary_gender_trigger = yes
					NOR = {
						this = scope:frankokratia_crusader.var:beneficiary
						this = scope:frankokratia_crusader.var:beneficiary_2
					}
				}
				scope:frankokratia_crusader = {
					set_variable = {
						name = beneficiary_3
						value = prev
					}
				}
			}
		}
		#any courtier
		if = {
			limit = {
				NOT = { has_variable = beneficiary_3 }
			}
			random_courtier_or_guest = {
				limit = {
					ep3_frankokratia_beneficiary_trigger = yes
					culture = scope:frankokratia_crusader.culture
					ep3_frankokratia_beneficiary_gender_soft_trigger = yes
					NOR = {
						this = scope:frankokratia_crusader.var:beneficiary
						this = scope:frankokratia_crusader.var:beneficiary_2
					}
				}
				scope:frankokratia_crusader = {
					set_variable = {
						name = beneficiary_3
						value = prev
					}
				}
			}
		}
		#pool character
		if = {
			limit = {
				NOT = { has_variable = beneficiary_3 }
			}
			random_pool_character = {
				province = scope:frankokratia_crusader.capital_province
				limit = {
					ep3_frankokratia_beneficiary_trigger = yes
					NOR = {
						this = scope:frankokratia_crusader.var:beneficiary
						this = scope:frankokratia_crusader.var:beneficiary_2
					}
				}
				scope:frankokratia_crusader = {
					set_variable = {
						name = beneficiary_3
						value = prev
					}
				}
			}
		}
	}
}

ep3_frankokratia_form_byzantine_kingdoms_effect = {
	every_vassal = {
		limit = {
			is_landless_ruler = yes
			is_ai = yes
			highest_held_title_tier < tier_kingdom
			
		}
		add_to_list = byz_landless
	}
	every_vassal = {
		limit = {
			is_ai = yes
			is_landed = yes
			NOT = {
				government_has_flag = government_is_theocracy
			}
		}
		add_to_list = byz_vassals
	}
	#Create player kings and kings for players
	every_vassal_or_below = {
		limit = {
			OR = {
				has_variable = frankokratia_becoming_king
				AND = {
					has_variable = frankokratia_gaining_vassal
					highest_held_title_tier < tier_kingdom
					capital_county = {
						kingdom = {
							NOT = {
								exists = holder
							}
						}
					}
					highest_held_title_tier >= tier_duchy
					#Must be free of any liege that isn't the emperor
					NOT = {
						liege = {
							highest_held_title_tier < tier_empire
						}
					}
					OR = {
						is_powerful_vassal_of = title:e_byzantium.holder
						house ?= { is_powerful_family = yes }
						#Let's just always let Nikaea do this, they special
						capital_county = {
							kingdom = title:k_nikaea
							NOT = {
								exists = title:k_nikaea.holder
							}
						}
					}
				}
			}
		}
		save_scope_as = player_byz_king
		capital_county.kingdom = {
			save_scope_as = player_byz_kingdom
		}
		create_character_memory = {
			type = frankokratia_byz_independent_memory
		}
		create_title_and_vassal_change = {
			type = created
			save_scope_as = change
			add_claim_on_loss = no
		}
		scope:player_byz_kingdom = {
			change_title_holder = {
				holder = scope:player_byz_king
				change = scope:change
			}
		}
		resolve_title_and_vassal_change = scope:change
	}
	#Move players under their kings
	every_vassal = {
		limit = {
			has_variable = frankokratia_gaining_vassal
			var:frankokratia_gaining_vassal = {
				NOT = {
					is_vassal_or_below_of = prev
				}
			}
		}
		save_scope_as = new_daddy
		var:frankokratia_gaining_vassal = {
			create_title_and_vassal_change = {
				type = granted
				save_scope_as = change
				add_claim_on_loss = no
			}
			change_liege = {
				liege = scope:new_daddy
				change = scope:change
			}
			resolve_title_and_vassal_change = scope:change
		}
	}
	#Make players independent
	every_vassal_or_below = {
		limit = {
			has_variable = frankokratia_independent
			NOT = {
				liege = title:e_byzantium.holder
			}
		}
		if = {
			limit = {
				OR = {
					religion = religion:islam_religion
					culture = { has_cultural_pillar = heritage_iranian }
					culture = { has_cultural_pillar = heritage_turkic }
					culture = { has_cultural_pillar = heritage_arabic }
				}
				highest_held_title_tier < tier_kingdom
				
			}
			change_government = clan_government
		}
		else_if = {
			limit = {
				highest_held_title_tier < tier_kingdom
			}
			change_government = feudal_government
		}
		create_character_memory = {
			type = frankokratia_byz_independent_memory
		}
		create_title_and_vassal_change = {
			type = independency
			save_scope_as = change
		}
		becomes_independent = { change = scope:change }
		resolve_title_and_vassal_change = scope:change
	}
	
	#Get every available kingdom that has a duke's capital in it
	if = {
		limit = {
			any_realm_county = {
				empire = title:e_byzantium
				kingdom = {
					NOR = {
						exists = holder
						AND = {
							exists = scope:latin_kingdom_1
							this = scope:latin_kingdom_1
						}
						AND = {
							exists = scope:latin_kingdom_2
							this = scope:latin_kingdom_2
						}
						is_in_list = available_byz_kingdoms
					}
				}
				save_temporary_scope_as = county_temp
				holder.capital_county = scope:county_temp
				holder = {
					is_ai = yes
					highest_held_title_tier >= tier_duchy
					OR = {
						is_powerful_vassal_of = title:e_byzantium.holder
						house ?= { is_powerful_family = yes }
						#If people in e_byz are weak, make sure nikaea is still created
						AND = {
							NOT = {
								any_in_list = {
									list = byz_vassals
									count = 3
									highest_held_title_tier >= tier_duchy
									capital_county = {
										kingdom = {
											this = { is_in_list = available_byz_kingdoms }
											NOT = { exists = holder }
										}
									}
									OR = {
										house = { is_powerful_family = yes }
										is_powerful_vassal_of = title:e_byzantium.holder
									}
								}
							}
							capital_county = {
								kingdom = title:k_nikaea
								NOT = {
									exists = title:k_nikaea.holder
								}
							}
						}

					}
				}
			}
		}
		every_realm_county = {
			limit = {
				empire = title:e_byzantium
				kingdom = {
					NOR = {
						exists = holder
						AND = {
							exists = scope:latin_kingdom_1
							this = scope:latin_kingdom_1
						}
						AND = {
							exists = scope:latin_kingdom_2
							this = scope:latin_kingdom_2
						}
						is_in_list = available_byz_kingdoms
					}
				}
				save_temporary_scope_as = county_temp
				
				holder.capital_county = scope:county_temp
				holder = {
					is_ai = yes
					highest_held_title_tier >= tier_duchy
					OR = {
						is_powerful_vassal_of = title:e_byzantium.holder
						house = { is_powerful_family = yes }
						#If people in e_byz are weak, make sure nikaea is still created
						AND = {
							NOT = {
								any_in_list = {
									list = byz_vassals
									count = 3
									highest_held_title_tier >= tier_duchy
									capital_county = {
										kingdom = {
											this = { is_in_list = available_byz_kingdoms }
											NOT = { exists = holder }
										}
									}
									OR = {
										house = { is_powerful_family = yes }
										is_powerful_vassal_of = title:e_byzantium.holder
									}
								}
							}
							capital_county = {
								kingdom = title:k_nikaea
								NOT = {
									exists = title:k_nikaea.holder
								}
							}
						}
					}
				}
			}
			kingdom = {
				add_to_list = available_byz_kingdoms
			}
		}
	}
	
	#Assign new kingdoms
	if = {
		limit = {
			any_in_list = {
				list = available_byz_kingdoms
				tier = tier_kingdom
			}
		}
		while = {
			limit = {
				any_in_list = {
					list = available_byz_kingdoms
					NOT = { exists = holder }
				}
			}
			ordered_in_list = {
				list = byz_vassals
				order_by = max_military_strength
				limit = {
					capital_county = {
						kingdom = {
							this = { is_in_list = available_byz_kingdoms }
							NOT = { exists = holder }
						}
					}
					highest_held_title_tier >= tier_duchy
					OR = {
						is_powerful_vassal_of = title:e_byzantium.holder
						house = { is_powerful_family = yes }
						#If people in e_byz are weak, make sure nikaea is still created
						AND = {
							NOT = {
								any_in_list = {
									list = byz_vassals
									count = 3
									highest_held_title_tier >= tier_duchy
									capital_county = {
										kingdom = {
											this = { is_in_list = available_byz_kingdoms }
											NOT = { exists = holder }
										}
									}
									OR = {
										house = { is_powerful_family = yes }
										is_powerful_vassal_of = title:e_byzantium.holder
									}
								}
							}
							capital_county = {
								kingdom = title:k_nikaea
								NOT = {
									exists = title:k_nikaea.holder
								}
							}
						}
					}
				}
				create_character_memory = {
					type = frankokratia_byz_independent_memory
				}
				save_scope_as = new_byz_king
				capital_county.kingdom = {
					save_scope_as = new_byz_kingdom
				}
				create_title_and_vassal_change = {
					type = created
					save_scope_as = change
					add_claim_on_loss = no
				}
				scope:new_byz_kingdom = {
					change_title_holder = {
						holder = scope:new_byz_king
						change = scope:change
					}
				}
				resolve_title_and_vassal_change = scope:change
			}
		}
	}
	#Move de jure neighbour vassals into kingdoms
	if = {
		limit = {
			any_vassal = {
				is_in_list = byz_vassals
				highest_held_title_tier <= tier_duchy
				government_has_flag = government_is_administrative
				OR = {
					ai_boldness <= medium_negative_ai_value
					ai_honor >= medium_positive_ai_value
					ai_energy <= medium_negative_ai_value
					has_trait = loyal
				}
				capital_county = {
					save_temporary_scope_as = prime_title
					kingdom.holder ?= {
						OR = {
							is_in_list = byz_vassals
							this = title:e_byzantium.holder
						}
					}
				}
				#One of heir counties borders the king's personal counties
				any_sub_realm_county = {
					any_neighboring_county = {
						holder = {
							OR = {
								this = scope:prime_title.kingdom.holder
								any_liege_or_above = {
									this = scope:prime_title.kingdom.holder
								}
							}
						}
					}
				}
			}
		}
		while = {
			limit = {
				any_vassal = {
					is_in_list = byz_vassals
					highest_held_title_tier <= tier_duchy
					government_has_flag = government_is_administrative
					OR = {
						ai_boldness <= medium_negative_ai_value
						ai_honor >= medium_positive_ai_value
						ai_energy <= medium_negative_ai_value
						has_trait = loyal
					}
					capital_county = {
						save_temporary_scope_as = prime_title
						kingdom.holder ?= {
							OR = {
								is_in_list = byz_vassals
								this = title:e_byzantium.holder
							}
						}
					}
					any_sub_realm_county = {
						any_neighboring_county = {
							holder = {
								OR = {
									this = scope:prime_title.kingdom.holder
									any_liege_or_above = {
										this = scope:prime_title.kingdom.holder
									}
								}
							}
						}
					}
				}
			}
			random_in_list = {
				list = byz_vassals
				limit = {
					is_in_list = byz_vassals
					highest_held_title_tier <= tier_duchy
					government_has_flag = government_is_administrative
					OR = {
						ai_boldness <= medium_negative_ai_value
						ai_honor >= medium_positive_ai_value
						ai_energy <= medium_negative_ai_value
						has_trait = loyal
					}
					capital_county = {
						save_temporary_scope_as = prime_title
						kingdom.holder ?= {
							OR = {
								is_in_list = byz_vassals
								this = title:e_byzantium.holder
							}
						}
					}
					any_sub_realm_county = {
						any_neighboring_county = {
							holder = {
								OR = {
									this = scope:prime_title.kingdom.holder
									any_liege_or_above = {
										this = scope:prime_title.kingdom.holder
									}
								}
							}
						}
					}
				}
				save_scope_as = vassalizing_vassal
				random_in_list = {
					list = byz_vassals
					limit = {
						is_in_list = byz_vassals
						any_held_title = {
							title_tier = kingdom
							any_de_jure_county = {
								save_temporary_scope_as = county_temp
								scope:vassalizing_vassal.capital_county = {
									this = scope:county_temp
								}
							}
						}
					}
					save_scope_as = new_liege
				}
				create_title_and_vassal_change = {
					type = granted
					save_scope_as = change
					add_claim_on_loss = no
				}
				change_liege = {
					liege = scope:new_liege
					change = scope:change
				}
				resolve_title_and_vassal_change = scope:change
			}
		}
	}
	#Move landless into kingdoms
	if = {
		limit = {
			any_vassal = {
				is_in_list = byz_landless
			}
			any_vassal = {
				highest_held_title_tier >= tier_kingdom
				government_has_flag = government_is_administrative
			}
		}
		while = {
			limit = {
				any_in_list = {
					list = byz_landless
					exists = house
					has_domicile = yes
				}
			}
			if = {
				limit = {
					any_in_list = {
						list = byz_landless
						house ?= {
							any_house_member = {
								any_held_title = {
									title_tier = kingdom
								}
								highest_held_title_tier >= tier_kingdom
								government_has_flag = government_is_administrative
							}
						}
					}
				}
				random_in_list = {
					list = byz_landless
					limit = {
						house ?= {
							any_house_member = {
								any_held_title = {
									title_tier = kingdom
								}
								highest_held_title_tier >= tier_kingdom
								government_has_flag = government_is_administrative
							}
						}
					}
					house = {
						random_house_member = {
							limit = {
								any_held_title = {
									title_tier = kingdom
								}
								highest_held_title_tier >= tier_kingdom
								government_has_flag = government_is_administrative
							}
							save_scope_as = new_byz_liege
						}
					}
					create_title_and_vassal_change = {
						type = granted
						save_scope_as = change
						add_claim_on_loss = no
					}
					change_liege = {
						liege = scope:new_byz_liege
						change = scope:change
					}
					resolve_title_and_vassal_change = scope:change
					remove_from_list = byz_landless
					if = {
						limit = {
							has_domicile = yes
						}
						domicile = {
							move_domicile = scope:new_byz_liege.capital_province
						}
					}
				}
			}
			else_if = {
				limit = {
					any_in_list = {
						list = byz_landless
						has_domicile = yes
						domicile.domicile_location = {
							kingdom = {
								holder ?= {
									government_has_flag = government_is_administrative
									liege = title:e_byzantium.holder
								}
							}
						}
					}
				}
				random_in_list = {
					list = byz_landless
					limit = {
						has_domicile = yes
						domicile.domicile_location = {
							kingdom = {
								holder ?= {
									government_has_flag = government_is_administrative
									liege = title:e_byzantium.holder
								}
							}
						}
					}
					domicile.domicile_location = {
						kingdom = {
							holder = {
								save_scope_as = new_byz_liege
							}
						}
					}
					create_title_and_vassal_change = {
						type = granted
						save_scope_as = change
						add_claim_on_loss = no
					}
					change_liege = {
						liege = scope:new_byz_liege
						change = scope:change
					}
					resolve_title_and_vassal_change = scope:change
					remove_from_list = byz_landless
					if = {
						limit = {
							has_domicile = yes
						}
						domicile = {
							move_domicile = scope:new_byz_liege.capital_province
						}
					}
				}
			}
			else = {
				random_in_list = {
					list = byz_landless
					title:e_byzantium.holder = {
						random_vassal = {
							limit = {
								highest_held_title_tier >= tier_kingdom
								government_has_flag = government_is_administrative
							}
							save_scope_as = new_byz_liege
						}
					}
					create_title_and_vassal_change = {
						type = granted
						save_scope_as = change
						add_claim_on_loss = no
					}
					change_liege = {
						liege = scope:new_byz_liege
						change = scope:change
					}
					resolve_title_and_vassal_change = scope:change
					remove_from_list = byz_landless
					if = {
						limit = {
							has_domicile = yes
						}
						domicile = {
							move_domicile = scope:new_byz_liege.capital_province
						}
					}
				}
			}
		}
	}

	#Make admin kings house heads if they ain't
	every_vassal = {
		limit = {
			highest_held_title_tier = tier_kingdom
			government_has_flag = government_is_administrative
			exists = house
			save_temporary_scope_as = house_king
			house.house_head = {
				#Not already house head
				this != scope:house_king
				highest_held_title_tier < tier_kingdom
				#Only remove player house head status if they're in the realm
				OR = {
					is_ai = yes
					top_liege = scope:house_king.liege
				}
			}
			#If there's more than one king of their house, they are the strongest
			NOT = {
				house = {
					any_house_member = {
						highest_held_title_tier = tier_kingdom
						government_has_flag = government_is_administrative
						max_military_strength > scope:house_king.max_military_strength
					}
				}
			}
		}
		house = {
			set_house_head = prev
		}
		domicile = {
			move_domicile = prev.capital_province
		}
	}

	#Make governor house heads to save noble families being destroyed
	every_vassal = {
		limit = {
			highest_held_title_tier = tier_kingdom
		}
		every_vassal_or_below = {
			limit = {
				highest_held_title_tier >= tier_county
				government_has_flag = government_is_administrative
				exists = house
				save_temporary_scope_as = house_chud
				house.house_head = {
					#Not already house head
					this != scope:house_chud
					government_has_flag = government_is_administrative
					highest_held_title_tier < tier_kingdom
					#Only remove player house head status if they're in the realm
					OR = {
						is_ai = yes
						liege ?= scope:house_chud.top_liege
					}
					#Liege is the Byzantine emperor
					liege ?= {
						top_liege = this
					}
					#If there's more than one admin survivor of their house, they are the strongest
					NOT = {
						house = {
							any_house_member = {
								OR = {
									highest_held_title_tier = tier_kingdom
									AND = {
										liege ?= {
											highest_held_title_tier = tier_kingdom
										}
										max_military_strength > scope:house_chud.max_military_strength
									}
								}
								government_has_flag = government_is_administrative
							}
						}
					}
				}
			}
			house = {
				set_house_head = prev
			}
			domicile = {
				move_domicile = prev.capital_province
			}
		}
	}

	#Switch anyone not under a king to feudal
	every_vassal_or_below = {
		limit = {
			is_landed = yes
			government_has_flag = government_is_administrative
			highest_held_title_tier < tier_kingdom
			OR = {
				liege = {
					highest_held_title_tier < tier_kingdom
				}
				liege = title:e_byzantium.holder
			}
		}
		if = {
			limit = {
				OR = {
					religion = religion:islam_religion
					culture = { has_cultural_pillar = heritage_iranian }
					culture = { has_cultural_pillar = heritage_turkic }
					culture = { has_cultural_pillar = heritage_arabic }
				}
			}
			change_government = clan_government
		}
		else = {
			change_government = feudal_government
		}
	}
}

ep3_frankokratia_byzantine_independence_effect = {
	every_vassal_or_below = {
		limit = {
			is_landed = yes
			is_ai = yes
			highest_held_title_tier < tier_kingdom
			highest_held_title_tier >= tier_county
			liege = {
				save_temporary_scope_as = temp_byz_liege
			}
			OR = {
				#They have a strong reason and the last piece is personality
				AND = {
					ai_boldness > high_negative_ai_value
					ai_honor < high_positive_ai_value
					ai_energy > high_negative_ai_value
					OR = {
						trigger_if = {
							limit = {
								liege ?= {
									government_has_flag = government_is_administrative
								}
							}
							NOT = {
								government_has_flag = government_is_administrative
							}
						}
						liege = {
							governor_efficiency < 0.7
						}
						culture = {
							this != scope:temp_byz_liege.culture
						}
						faith = {
							this != scope:temp_byz_liege.faith
						}
						opinion = {
							target = liege
							value <= -50
						}
						#They are not de jure to the liege
						NOT = {
							primary_title = {
								target_is_de_jure_liege_or_above = scope:temp_byz_liege.primary_title
							}
						}
					}
				}
				#Their only reason is an inclination
				AND = {
					ai_boldness >= low_positive_ai_value
					ai_honor <= low_negative_ai_value
					ai_energy >= low_positive_ai_value
					OR = {
						liege = {
							influence_level <= 2
						}
						opinion = {
							target = liege
							value <= -25
						}
						liege = {
							governor_efficiency < 0.85
						}
					}
				}
			}
			NOR = {
				has_trait = loyal
				opinion = {
					target = liege
					value >= 80
				}
				liege = {
					influence_level >= 4
				}
				liege = {
					governor_efficiency >= 1.4
				}
			}
		}
		if = {
			limit = {
				OR = {
					religion = religion:islam_religion
					culture = { has_cultural_pillar = heritage_iranian }
					culture = { has_cultural_pillar = heritage_turkic }
					culture = { has_cultural_pillar = heritage_arabic }
				}
				highest_held_title_tier < tier_kingdom
				
			}
			change_government = clan_government
		}
		else_if = {
			limit = {
				highest_held_title_tier < tier_kingdom
			}
			change_government = feudal_government
		}
		create_character_memory = {
			type = frankokratia_byz_independent_memory
		}
		create_title_and_vassal_change = {
			type = independency
			save_scope_as = change
		}
		becomes_independent = { change = scope:change }
		resolve_title_and_vassal_change = scope:change
	}
}

ep3_frankokratia_vassalize_latin_counts_effect = {
	#Move de jure neighbour vassals into kingdoms
	if = {
		limit = {
			#Duchy titles have been assigned
			any_vassal = {
				highest_held_title_tier = tier_duchy
			}
			#At least one count is under the duchy
			any_vassal = {
				highest_held_title_tier = tier_county
				primary_title.duchy = {
					holder ?= {
						is_vassal_of = scope:latin_emperor
					}
				}
			}
		}
		every_vassal = {
			limit = {
				highest_held_title_tier = tier_county
				primary_title.duchy = {
					holder ?= {
						is_vassal_of = scope:latin_emperor
					}
				}
			}
			add_to_list = counts_to_distribute
		}
		while = {
			limit = {
				any_vassal = {
					is_in_list = counts_to_distribute
				}
			}
			random_in_list = {
				list = counts_to_distribute
				save_scope_as = vassalizing_vassal
				primary_title.duchy.holder = {
					save_scope_as = new_liege
				}
				create_title_and_vassal_change = {
					type = granted
					save_scope_as = change
					add_claim_on_loss = no
				}
				change_liege = {
					liege = scope:new_liege
					change = scope:change
				}
				resolve_title_and_vassal_change = scope:change
			}
		}
	}
	#Give out leftover counties
	if = {
		limit = {
			any_held_title = {
				title_tier = county
				duchy = {
					holder ?= {
						is_vassal_or_below_of = scope:latin_emperor
					}
				}
			}
		}
		while = {
			limit = {
				any_held_title = {
					title_tier = county
					duchy = {
						holder ?= {
							is_vassal_or_below_of = scope:latin_emperor
						}
					}
				}
			}
			random_held_title = {
				title_tier = county
				limit = {
					duchy = {
						holder ?= {
							is_vassal_or_below_of = scope:latin_emperor
						}
					}
				}
				duchy.holder = {
					save_scope_as = latin_vassal
				}
				save_scope_as = county_to_grant
			}
			create_title_and_vassal_change = {
				type = granted
				save_scope_as = title_change
			}
			scope:county_to_grant = {
				change_title_holder = {
					holder = scope:latin_vassal
					change = scope:title_change
				}
			}
			resolve_title_and_vassal_change = scope:title_change
		}
	}
}

ep3_frankokratia_end_story_effect = {
	global_var:byz_claimant_champion = {
		remove_character_modifier = prepared_for_daring_deeds_modifier
		remove_character_flag = pope_condemned_crusader_claim
		remove_character_flag = pope_endorsed_crusader_claim
		random_owned_story = {
			type = frankokratia_story
			var:claimant ?= {
				hidden_effect = {
					remove_character_modifier = eager_for_empire_modifier
					remove_character_flag = swore_to_convert_byz_catholic
					remove_character_flag = paying_off_latins
					remove_character_flag = ep3_frankokratia_events.0041_succeeded_duel
				}
			}
			every_in_list = {
				variable = frankokratia_leaders
				hidden_effect = {
					remove_character_modifier = grand_crusader_fleet_modifier
					remove_character_flag = joining_frankokratia
					remove_character_flag = had_event_frankokratia_0020
					remove_character_flag = dont_send_general_event_franko
					remove_variable = old_liege
					remove_variable = latin_county
					remove_variable = emp_beneficiary
					remove_variable = emp_beneficiary_2
					remove_variable = emp_beneficiary_3
					remove_variable = beneficiary
					remove_variable = beneficiary_2
					remove_variable = beneficiary_3
					remove_variable = latin_duchy
					clear_variable_list = lost_titles
				}
			}
			end_story = yes
		}
		remove_global_variable = byz_claimant_champion
		remove_global_variable = financier_title
		remove_global_variable = slaughtered_byzantines
		remove_global_variable = latin_kingdom_1
		remove_global_variable = latin_kingdom_2
		clear_global_variable_list = slaughtered_byzantines_list
	}
}

greek_anarchy_army_spawn_effect = {
	random_list = {
		5 = {
			trigger = {
				OR = {
					culture = {
						has_cultural_parameter = unlock_maa_cataphract
					}
					scope:defender = {
						any_realm_county = {
							culture = {
								has_cultural_parameter = unlock_maa_cataphract
							}
						}
					}
					scope:defender = {
						culture = {
							has_cultural_parameter = unlock_maa_cataphract
						}
					}
				}
			}
			#Cool heavy cav should be less likely for petty wars
			modifier = {
				NOT = {
					scope:defender = {
						highest_held_title_tier >= tier_kingdom
					}
				}
				add = -4
			}
			if = {
				limit = {
					scope:defender = {
						culture = {
							has_cultural_parameter = unlock_maa_cataphract
						}
					}
				}
				scope:defender.culture = { save_scope_as = army_culture }
			}
			else_if = {
				limit = {
					scope:defender = {
						any_realm_county = {
							culture = {
								has_cultural_parameter = unlock_maa_cataphract
							}
						}
					}
				}
				scope:defender = {
					random_realm_county = {
						limit = {
							culture = {
								has_cultural_parameter = unlock_maa_cataphract
							}
						}
						culture = { save_scope_as = army_culture }
					}
				}
			}
			else = {
				culture = {
					save_scope_as = army_culture
				}
			}
			spawn_army = {
				name = greek_anarchy_war_greek_army_name
				uses_supply = yes
				inheritable = no
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = cataphract
					stacks = greek_anarchy_army_heavy_cav_value
				}
				location = scope:attacker.capital_province
			}
		}
		4 = {
			trigger = {
				OR = {
					culture = {
						has_cultural_parameter = unlock_maa_ballistrai
					}
					scope:defender = {
						culture = {
							has_cultural_parameter = unlock_maa_ballistrai
						}
					}
					scope:defender = {
						any_realm_county = {
							culture = {
								has_cultural_parameter = unlock_maa_ballistrai
							}
						}
					}
				}
			}
			if = {
				limit = {
					scope:defender = {
						culture = {
							has_cultural_parameter = unlock_maa_ballistrai
						}
					}
				}
				scope:defender.culture = { save_scope_as = army_culture }
			}
			else_if = {
				limit = {
					scope:defender = {
						any_realm_county = {
							culture = {
								has_cultural_parameter = unlock_maa_ballistrai
							}
						}
					}
				}
				scope:defender = {
					random_realm_county = {
						limit = {
							culture = {
								has_cultural_parameter = unlock_maa_ballistrai
							}
						}
						culture = { save_scope_as = army_culture }
					}
				}
			}
			else = {
				culture = {
					save_scope_as = army_culture
				}
			}
			spawn_army = {
				name = greek_anarchy_war_greek_army_name
				uses_supply = yes
				inheritable = no
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = ballistrai
					stacks = greek_anarchy_army_maa_value
				}
				location = scope:attacker.capital_province
			}
		}
		4 = {
			trigger = {
				OR = {
					culture = {
						has_cultural_parameter = unlock_maa_skoutatoi
					}
					scope:defender = {
						any_realm_county = {
							culture = {
								has_cultural_parameter = unlock_maa_skoutatoi
							}
						}
					}
					scope:defender = {
						culture = {
							has_cultural_parameter = unlock_maa_skoutatoi
						}
					}
				}
			}
			if = {
				limit = {
					scope:defender = {
						culture = {
							has_cultural_parameter = unlock_maa_skoutatoi
						}
					}
				}
				scope:defender.culture = { save_scope_as = army_culture }
			}
			else_if = {
				limit = {
					scope:defender = {
						any_realm_county = {
							culture = {
								has_cultural_parameter = unlock_maa_skoutatoi
							}
						}
					}
				}
				scope:defender = {
					random_realm_county = {
						limit = {
							culture = {
								has_cultural_parameter = unlock_maa_skoutatoi
							}
						}
						culture = { save_scope_as = army_culture }
					}
				}
			}
			else = {
				culture = {
					save_scope_as = army_culture
				}
			}
			spawn_army = {
				name = greek_anarchy_war_greek_army_name
				uses_supply = yes
				inheritable = no
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = skoutatoi
					stacks = greek_anarchy_army_maa_value
				}
				location = scope:attacker.capital_province
			}
		}
		4 = {
			trigger = {
				OR = {
					culture = {
						has_cultural_parameter = unlock_maa_akritai
					}
					scope:defender = {
						any_realm_county = {
							culture = {
								has_cultural_parameter = unlock_maa_akritai
							}
							county_has_province_with_terrain = {
								terrain = mountains
							}
						}
					}
					scope:defender = {
						culture = {
							has_cultural_parameter = unlock_maa_akritai
						}
					}
				}
			}
			if = {
				limit = {
					scope:defender = {
						culture = {
							has_cultural_parameter = unlock_maa_akritai
						}
					}
				}
				scope:defender.culture = { save_scope_as = army_culture }
			}
			else_if = {
				limit = {
					scope:defender = {
						any_realm_county = {
							culture = {
								has_cultural_parameter = unlock_maa_akritai
							}
						}
					}
				}
				scope:defender = {
					random_realm_county = {
						limit = {
							culture = {
								has_cultural_parameter = unlock_maa_akritai
							}
						}
						culture = { save_scope_as = army_culture }
					}
				}
			}
			else = {
				culture = {
					save_scope_as = army_culture
				}
			}
			spawn_army = {
				name = greek_anarchy_war_greek_army_name
				uses_supply = yes
				inheritable = no
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = akritai
					stacks = greek_anarchy_army_weak_maa_value
				}
				location = scope:attacker.capital_province
			}
		}
		#Armenians
		10 = {
			trigger = {
				OR = {
					culture = {
						has_cultural_parameter = unlock_maa_ayrudzi
					}
					scope:defender = {
						culture = {
							has_cultural_parameter = unlock_maa_ayrudzi
						}
					}
					scope:defender = {
						any_realm_county = {
							culture = {
								has_cultural_parameter = unlock_maa_ayrudzi
							}
						}
					}
				}
			}
			if = {
				limit = {
					scope:defender = {
						culture = {
							has_cultural_parameter = unlock_maa_ayrudzi
						}
					}
				}
				scope:defender.culture = { save_scope_as = army_culture }
			}
			else_if = {
				limit = {
					scope:defender = {
						any_realm_county = {
							culture = {
								has_cultural_parameter = unlock_maa_ayrudzi
							}
						}
					}
				}
				scope:defender = {
					random_realm_county = {
						limit = {
							culture = {
								has_cultural_parameter = unlock_maa_ayrudzi
							}
						}
						culture = { save_scope_as = army_culture }
					}
				}
			}
			else = {
				culture = {
					save_scope_as = army_culture
				}
			}
			spawn_army = {
				name = greek_anarchy_war_neighbor_army_name
				uses_supply = yes
				inheritable = no
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = ayrudzi
					stacks = greek_anarchy_army_maa_value
				}
				location = scope:attacker.capital_province
			}
		}
		#Georgians
		10 = {
			trigger = {
				OR = {
					culture = {
						has_cultural_parameter = unlock_maa_monaspa
					}
					scope:defender = {
						culture = {
							has_cultural_parameter = unlock_maa_monaspa
						}
					}
					scope:defender = {
						any_realm_county = {
							culture = {
								has_cultural_parameter = unlock_maa_monaspa
							}
						}
					}
				}
			}
			if = {
				limit = {
					scope:defender = {
						culture = {
							has_cultural_parameter = unlock_maa_monaspa
						}
					}
				}
				scope:defender.culture = { save_scope_as = army_culture }
			}
			else_if = {
				limit = {
					scope:defender = {
						any_realm_county = {
							culture = {
								has_cultural_parameter = unlock_maa_monaspa
							}
						}
					}
				}
				scope:defender = {
					random_realm_county = {
						limit = {
							culture = {
								has_cultural_parameter = unlock_maa_monaspa
							}
						}
						culture = { save_scope_as = army_culture }
					}
				}
			}
			else = {
				culture = {
					save_scope_as = army_culture
				}
			}
			spawn_army = {
				name = greek_anarchy_war_neighbor_army_name
				uses_supply = yes
				inheritable = no
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = monaspa
					stacks = greek_anarchy_army_heavy_cav_value
				}
				location = scope:attacker.capital_province
			}
		}
		#Cataphract archers
		10 = {
			trigger = {
				OR = {
					culture = {
						has_cultural_parameter = unlock_maa_cataphract_archers
					}
					scope:defender = {
						culture = {
							has_cultural_parameter = unlock_maa_cataphract_archers
						}
					}
					scope:defender = {
						any_realm_county = {
							culture = {
								has_cultural_parameter = unlock_maa_cataphract_archers
							}
						}
					}
				}
			}
			if = {
				limit = {
					scope:defender = {
						culture = {
							has_cultural_parameter = unlock_maa_cataphract_archers
						}
					}
				}
				scope:defender.culture = { save_scope_as = army_culture }
			}
			else_if = {
				limit = {
					scope:defender = {
						any_realm_county = {
							culture = {
								has_cultural_parameter = unlock_maa_cataphract_archers
							}
						}
					}
				}
				scope:defender = {
					random_realm_county = {
						limit = {
							culture = {
								has_cultural_parameter = unlock_maa_cataphract_archers
							}
						}
						culture = { save_scope_as = army_culture }
					}
				}
			}
			else = {
				culture = {
					save_scope_as = army_culture
				}
			}
			spawn_army = {
				name = greek_anarchy_war_neighbor_army_name
				uses_supply = yes
				inheritable = no
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = cataphract_archers
					stacks = greek_anarchy_army_heavy_cav_value
				}
				location = scope:attacker.capital_province
			}
		}
		#Konni folk get konni
		10 = {
			trigger = {
				OR = {
					culture = {
						has_cultural_parameter = unlock_maa_hussar
					}
					scope:defender = {
						culture = {
							has_cultural_parameter = unlock_maa_hussar
						}
					}
					scope:defender = {
						any_realm_county = {
							culture = {
								has_cultural_parameter = unlock_maa_hussar
							}
						}
					}
				}
			}
			if = {
				limit = {
					scope:defender = {
						culture = {
							has_cultural_parameter = unlock_maa_hussar
						}
					}
				}
				scope:defender.culture = { save_scope_as = army_culture }
			}
			else_if = {
				limit = {
					scope:defender = {
						any_realm_county = {
							culture = {
								has_cultural_parameter = unlock_maa_hussar
							}
						}
					}
				}
				scope:defender = {
					random_realm_county = {
						limit = {
							culture = {
								has_cultural_parameter = unlock_maa_hussar
							}
						}
						culture = { save_scope_as = army_culture }
					}
				}
			}
			else = {
				culture = {
					save_scope_as = army_culture
				}
			}
			spawn_army = {
				name = greek_anarchy_war_neighbor_army_name
				uses_supply = yes
				inheritable = no
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = hussar
					stacks = greek_anarchy_army_maa_value
				}
				location = scope:attacker.capital_province
			}
		}
		#Varangians
		10 = {
			trigger = {
				OR = {
					culture = {
						has_cultural_parameter = unlock_maa_varangian_veterans
					}
					culture = {
						this = culture:greek
					}
					culture = {
						any_parent_culture_or_above = {
							this = culture:greek
						}
					}
					scope:defender = {
						culture = {
							has_cultural_parameter = unlock_maa_varangian_veterans
						}
					}
					scope:defender = {
						any_realm_county = {
							culture = {
								has_cultural_parameter = unlock_maa_varangian_veterans
							}
						}
					}
				}
			}
			modifier = {
				OR = {
					culture = {
						this = culture:greek
					}
					culture = {
						any_parent_culture_or_above = {
							this = culture:greek
						}
					}
				}
				highest_held_title_tier <= tier_duchy
				add = -8
			}
			spawn_army = {
				name = greek_anarchy_war_generic_army_name_2
				uses_supply = yes
				inheritable = no
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = varangian_veterans
					stacks = greek_anarchy_army_maa_value
				}
				location = scope:attacker.capital_province
			}
		}
		#Horse archers
		7 = {
			trigger = {
				OR = {
					culture = {
						culture_has_archer_cavalry_maa = yes
					}
					scope:defender = {
						any_realm_county = {
							culture = {
								culture_has_archer_cavalry_maa = yes
							}
						}
					}
					scope:defender = {
						any_vassal = {
							OR = {
								culture = { culture_has_archer_cavalry_maa = yes }
								mpo_can_recruit_nomad_maa_trigger = yes
							}
						}
					}
					scope:defender = {
						OR = {
							culture = { culture_has_archer_cavalry_maa = yes }
							mpo_can_recruit_nomad_maa_trigger = yes
						}
					}
				}
			}
			if = {
				limit = {
					scope:defender = {
						any_realm_county = {
							culture = {
								culture_has_archer_cavalry_maa = yes
							}
						}
					}
				}
				scope:defender = {
					random_realm_county = {
						limit = {
							culture = {
								culture_has_archer_cavalry_maa = yes
							}
						}
						culture = { save_scope_as = army_culture }
					}
				}
			}
			else_if = {
				limit = {
					scope:defender = {
						OR = {
							culture = { culture_has_archer_cavalry_maa = yes }
							mpo_can_recruit_nomad_maa_trigger = yes
						}
					}
				}
				scope:defender = {
					culture = {
						save_scope_as = army_culture
					}
				}
			}
			else_if = {
				limit = {
					scope:defender = {
						any_vassal = {
							OR = {
								culture = { culture_has_archer_cavalry_maa = yes }
								mpo_can_recruit_nomad_maa_trigger = yes
							}
						}
					}
				}
				scope:defender = {
					random_vassal = {
						limit = {
							OR = {
								culture = { culture_has_archer_cavalry_maa = yes }
								mpo_can_recruit_nomad_maa_trigger = yes
							}
						}
						culture = { save_scope_as = army_culture }
					}
				}
			}
			else = {
				culture = {
					save_scope_as = army_culture
				}
			}
			spawn_army = {
				name = greek_anarchy_war_nomad_army_name
				uses_supply = yes
				inheritable = no
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = horse_archers
					stacks = greek_anarchy_army_maa_value
				}
				location = scope:attacker.capital_province
			}
		}
		#Crossbowmen
		2 = {
			trigger = {
				OR = {
					culture = {
						OR = {
							has_innovation = innovation_advanced_bowmaking
							has_innovation = innovation_repeating_crossbow
						}
					}
					scope:defender = {
						any_realm_county = {
							culture = {
								OR = {
									has_innovation = innovation_advanced_bowmaking
									has_innovation = innovation_repeating_crossbow
								}
							}
						}
					}
				}
			}
			spawn_army = {
				name = greek_anarchy_war_generic_army_name_2
				uses_supply = yes
				inheritable = no
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = crossbowmen
					stacks = greek_anarchy_army_maa_value
				}
				location = scope:attacker.capital_province
			}
		}
		#Norman knights
		2 = {
			trigger = {
				OR = {
					culture = {
						has_cultural_parameter = unlock_maa_conrois
					}
					top_liege = {
						any_vassal = {
							highest_held_title_tier >= tier_county
							culture = {
								has_cultural_parameter = unlock_maa_conrois
							}
						}
					}
					any_realm_county = {
						culture = {
							has_cultural_parameter = unlock_maa_conrois
						}
					}
					#Normans should be just runnin' wild
					culture:norman = {
						exists = culture_head
						has_cultural_parameter = unlock_maa_conrois
					}
				}
			}
			modifier = {
				OR = {
					culture = {
						has_cultural_parameter = unlock_maa_conrois
					}
					top_liege = {
						any_vassal = {
							highest_held_title_tier >= tier_county
							culture = {
								has_cultural_parameter = unlock_maa_conrois
							}
						}
					}
					any_realm_county = {
						culture = {
							has_cultural_parameter = unlock_maa_conrois
						}
					}
				}
				add = 18
			}
			if = {
				limit = {
					culture = {
						has_cultural_parameter = unlock_maa_conrois
					}
				}
				culture = {
					save_scope_as = army_culture
				}
			}
			else_if = {
				limit = {
					top_liege = {
						any_vassal = {
							culture = {
								has_cultural_parameter = unlock_maa_conrois
							}
						}
					}
				}
				top_liege = {
					random_vassal = {
						limit = {
							culture = {
								has_cultural_parameter = unlock_maa_conrois
							}
						}
						culture = {
							save_scope_as = army_culture
						}
					}
				}
			}
			else_if = {
				limit = {
					any_realm_county = {
						culture = {
							has_cultural_parameter = unlock_maa_conrois
						}
					}
				}
				random_realm_county = {
					limit = {
						culture = {
							has_cultural_parameter = unlock_maa_conrois
						}
					}
					culture = {
						save_scope_as = army_culture
					}
				}
			}
			else = {
				culture:norman = {
					save_scope_as = army_culture
				}
			}
			spawn_army = {
				name = greek_anarchy_war_crusaders_army_name
				uses_supply = yes
				inheritable = no
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = conrois
					stacks = greek_anarchy_army_heavy_cav_value
				}
				location = scope:attacker.capital_province
			}
		}
		#Longbowmen
		10 = {
			trigger = {
				OR = {
					culture = {
						has_cultural_parameter = unlock_maa_longbowmen
					}
					top_liege = {
						any_vassal = {
							highest_held_title_tier >= tier_county
							culture = {
								has_cultural_parameter = unlock_maa_longbowmen
							}
						}
					}
					any_realm_county = {
						culture = {
							has_cultural_parameter = unlock_maa_longbowmen
						}
					}
				}
			}
			if = {
				limit = {
					culture = {
						has_cultural_parameter = unlock_maa_longbowmen
					}
				}
				culture = {
					save_scope_as = army_culture
				}
			}
			else_if = {
				limit = {
					top_liege = {
						any_vassal = {
							culture = {
								has_cultural_parameter = unlock_maa_longbowmen
							}
						}
					}
				}
				top_liege = {
					random_vassal = {
						limit = {
							culture = {
								has_cultural_parameter = unlock_maa_longbowmen
							}
						}
						culture = {
							save_scope_as = army_culture
						}
					}
				}
			}
			else = {
				random_realm_county = {
					limit = {
						culture = {
							has_cultural_parameter = unlock_maa_longbowmen
						}
					}
					culture = {
						save_scope_as = army_culture
					}
				}
			}
			spawn_army = {
				name = greek_anarchy_war_crusaders_army_name
				uses_supply = yes
				inheritable = no
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = longbowmen
					stacks = greek_anarchy_army_maa_value
				}
				location = scope:attacker.capital_province
			}
		}
		#Picchieri
		10 = {
			trigger = {
				OR = {
					culture = {
						has_innovation = innovation_pike_columns
					}
					top_liege = {
						any_vassal = {
							highest_held_title_tier >= tier_county
							culture = {
								has_innovation = innovation_pike_columns
							}
						}
					}
					any_realm_county = {
						culture = {
							has_innovation = innovation_pike_columns
						}
					}
				}
			}
			if = {
				limit = {
					culture = {
						has_innovation = innovation_pike_columns
					}
				}
				culture = {
					save_scope_as = army_culture
				}
			}
			else_if = {
				limit = {
					top_liege = {
						any_vassal = {
							culture = {
								has_innovation = innovation_pike_columns
							}
						}
					}
				}
				top_liege = {
					random_vassal = {
						limit = {
							culture = {
								has_innovation = innovation_pike_columns
							}
						}
						culture = {
							save_scope_as = army_culture
						}
					}
				}
			}
			else = {
				random_realm_county = {
					limit = {
						culture = {
							has_innovation = innovation_pike_columns
						}
					}
					culture = {
						save_scope_as = army_culture
					}
				}
			}
			spawn_army = {
				name = greek_anarchy_war_crusaders_army_name
				uses_supply = yes
				inheritable = no
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = picchieri
					stacks = greek_anarchy_army_maa_value
				}
				location = scope:attacker.capital_province
			}
		}
		#Goedendag
		10 = {
			trigger = {
				OR = {
					culture = {
						has_innovation = innovation_adaptive_militia
					}
					top_liege = {
						any_vassal = {
							highest_held_title_tier >= tier_county
							culture = {
								has_innovation = innovation_adaptive_militia
							}
						}
					}
					any_realm_county = {
						culture = {
							has_innovation = innovation_adaptive_militia
						}
					}
				}
			}
			if = {
				limit = {
					culture = {
						has_innovation = innovation_adaptive_militia
					}
				}
				culture = {
					save_scope_as = army_culture
				}
			}
			else_if = {
				limit = {
					top_liege = {
						any_vassal = {
							culture = {
								has_innovation = innovation_adaptive_militia
							}
						}
					}
				}
				top_liege = {
					random_vassal = {
						limit = {
							culture = {
								has_innovation = innovation_adaptive_militia
							}
						}
						culture = {
							save_scope_as = army_culture
						}
					}
				}
			}
			else = {
				random_realm_county = {
					limit = {
						culture = {
							has_innovation = innovation_adaptive_militia
						}
					}
					culture = {
						save_scope_as = army_culture
					}
				}
			}
			spawn_army = {
				name = greek_anarchy_war_crusaders_army_name
				uses_supply = yes
				inheritable = no
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = goedendag
					stacks = greek_anarchy_army_weak_maa_value
				}
				location = scope:attacker.capital_province
			}
		}
		#Caballeros
		10 = {
			trigger = {
				OR = {
					culture = {
						has_innovation = innovation_caballeros
					}
					top_liege = {
						any_vassal = {
							highest_held_title_tier >= tier_county
							culture = {
								has_innovation = innovation_caballeros
							}
						}
					}
					any_realm_county = {
						culture = {
							has_innovation = innovation_caballeros
						}
					}
				}
			}
			if = {
				limit = {
					culture = {
						has_innovation = innovation_caballeros
					}
				}
				culture = {
					save_scope_as = army_culture
				}
			}
			else_if = {
				limit = {
					top_liege = {
						any_vassal = {
							culture = {
								has_innovation = innovation_caballeros
							}
						}
					}
				}
				top_liege = {
					random_vassal = {
						limit = {
							culture = {
								has_innovation = innovation_caballeros
							}
						}
						culture = {
							save_scope_as = army_culture
						}
					}
				}
			}
			else = {
				random_realm_county = {
					limit = {
						culture = {
							has_innovation = innovation_caballeros
						}
					}
					culture = {
						save_scope_as = army_culture
					}
				}
			}
			spawn_army = {
				name = greek_anarchy_war_crusaders_army_name
				uses_supply = yes
				inheritable = no
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = caballero
					stacks = greek_anarchy_army_maa_value
				}
				location = scope:attacker.capital_province
			}
		}
		#Hobbies
		10 = {
			trigger = {
				OR = {
					culture = {
						has_innovation = innovation_hobbies
					}
					top_liege = {
						any_vassal = {
							highest_held_title_tier >= tier_county
							culture = {
								has_innovation = innovation_hobbies
							}
						}
					}
					any_realm_county = {
						culture = {
							has_innovation = innovation_hobbies
						}
					}
				}
			}
			if = {
				limit = {
					culture = {
						has_innovation = innovation_hobbies
					}
				}
				culture = {
					save_scope_as = army_culture
				}
			}
			else_if = {
				limit = {
					top_liege = {
						any_vassal = {
							culture = {
								has_innovation = innovation_hobbies
							}
						}
					}
				}
				top_liege = {
					random_vassal = {
						limit = {
							culture = {
								has_innovation = innovation_hobbies
							}
						}
						culture = {
							save_scope_as = army_culture
						}
					}
				}
			}
			else = {
				random_realm_county = {
					limit = {
						culture = {
							has_innovation = innovation_hobbies
						}
					}
					culture = {
						save_scope_as = army_culture
					}
				}
			}
			spawn_army = {
				name = greek_anarchy_war_crusaders_army_name
				uses_supply = yes
				inheritable = no
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = hobelar
					stacks = greek_anarchy_army_maa_value
				}
				location = scope:attacker.capital_province
			}
		}
		#Schiltron
		10 = {
			trigger = {
				OR = {
					culture = {
						has_innovation = innovation_rectilinear_schiltron
					}
					top_liege = {
						any_vassal = {
							highest_held_title_tier >= tier_county
							culture = {
								has_innovation = innovation_rectilinear_schiltron
							}
						}
					}
					any_realm_county = {
						culture = {
							has_innovation = innovation_rectilinear_schiltron
						}
					}
				}
			}
			if = {
				limit = {
					culture = {
						has_innovation = innovation_rectilinear_schiltron
					}
				}
				culture = {
					save_scope_as = army_culture
				}
			}
			else_if = {
				limit = {
					top_liege = {
						any_vassal = {
							culture = {
								has_innovation = innovation_rectilinear_schiltron
							}
						}
					}
				}
				top_liege = {
					random_vassal = {
						limit = {
							culture = {
								has_innovation = innovation_rectilinear_schiltron
							}
						}
						culture = {
							save_scope_as = army_culture
						}
					}
				}
			}
			else = {
				random_realm_county = {
					limit = {
						culture = {
							has_innovation = innovation_rectilinear_schiltron
						}
					}
					culture = {
						save_scope_as = army_culture
					}
				}
			}
			spawn_army = {
				name = greek_anarchy_war_crusaders_army_name
				uses_supply = yes
				inheritable = no
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = schiltron
					stacks = greek_anarchy_army_maa_value
				}
				location = scope:attacker.capital_province
			}
		}
		#Valets
		10 = {
			trigger = {
				OR = {
					culture = {
						has_innovation = innovation_valets
					}
					top_liege = {
						any_vassal = {
							highest_held_title_tier >= tier_county
							culture = {
								has_innovation = innovation_valets
							}
						}
					}
					any_realm_county = {
						culture = {
							has_innovation = innovation_valets
						}
					}
				}
				scope:defender = {
					highest_held_title_tier >= tier_kingdom
				}
			}
			if = {
				limit = {
					culture = {
						has_innovation = innovation_valets
					}
				}
				culture = {
					save_scope_as = army_culture
				}
			}
			else_if = {
				limit = {
					top_liege = {
						any_vassal = {
							culture = {
								has_innovation = innovation_valets
							}
						}
					}
				}
				top_liege = {
					random_vassal = {
						limit = {
							culture = {
								has_innovation = innovation_valets
							}
						}
						culture = {
							save_scope_as = army_culture
						}
					}
				}
			}
			else = {
				random_realm_county = {
					limit = {
						culture = {
							has_innovation = innovation_valets
						}
					}
					culture = {
						save_scope_as = army_culture
					}
				}
			}
			spawn_army = {
				name = greek_anarchy_war_crusaders_army_name
				uses_supply = yes
				inheritable = no
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = gendarme
					stacks = greek_anarchy_army_heavy_cav_value
				}
				location = scope:attacker.capital_province
			}
		}
		#landsknecht
		10 = {
			trigger = {
				OR = {
					culture = {
						has_innovation = innovation_zweihanders
					}
					top_liege = {
						any_vassal = {
							highest_held_title_tier >= tier_county
							culture = {
								has_innovation = innovation_zweihanders
							}
						}
					}
					any_realm_county = {
						culture = {
							has_innovation = innovation_zweihanders
						}
					}
				}
			}
			if = {
				limit = {
					culture = {
						has_innovation = innovation_zweihanders
					}
				}
				culture = {
					save_scope_as = army_culture
				}
			}
			else_if = {
				limit = {
					top_liege = {
						any_vassal = {
							culture = {
								has_innovation = innovation_zweihanders
							}
						}
					}
				}
				top_liege = {
					random_vassal = {
						limit = {
							culture = {
								has_innovation = innovation_zweihanders
							}
						}
						culture = {
							save_scope_as = army_culture
						}
					}
				}
			}
			else = {
				random_realm_county = {
					limit = {
						culture = {
							has_innovation = innovation_zweihanders
						}
					}
					culture = {
						save_scope_as = army_culture
					}
				}
			}
			spawn_army = {
				name = greek_anarchy_war_crusaders_army_name
				uses_supply = yes
				inheritable = no
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = landsknecht
					stacks = greek_anarchy_army_maa_value
				}
				location = scope:attacker.capital_province
			}
		}
		#Huscarls
		10 = {
			trigger = {
				OR = {
					culture = {
						has_cultural_parameter = unlock_maa_huscarls
					}
					top_liege = {
						any_vassal = {
							highest_held_title_tier >= tier_county
							culture = {
								has_cultural_parameter = unlock_maa_huscarls
							}
						}
					}
					any_realm_county = {
						culture = {
							has_cultural_parameter = unlock_maa_huscarls
						}
					}
				}
			}
			if = {
				limit = {
					culture = {
						has_cultural_parameter = unlock_maa_huscarls
					}
				}
				culture = {
					save_scope_as = army_culture
				}
			}
			else_if = {
				limit = {
					top_liege = {
						any_vassal = {
							culture = {
								has_cultural_parameter = unlock_maa_huscarls
							}
						}
					}
				}
				top_liege = {
					random_vassal = {
						limit = {
							culture = {
								has_cultural_parameter = unlock_maa_huscarls
							}
						}
						culture = {
							save_scope_as = army_culture
						}
					}
				}
			}
			else = {
				random_realm_county = {
					limit = {
						culture = {
							has_cultural_parameter = unlock_maa_huscarls
						}
					}
					culture = {
						save_scope_as = army_culture
					}
				}
			}
			spawn_army = {
				name = greek_anarchy_war_crusaders_army_name
				uses_supply = yes
				inheritable = no
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = huscarl
					stacks = greek_anarchy_army_maa_value
				}
				location = scope:attacker.capital_province
			}
		}
		#Camels
		5 = {
			trigger = {
				OR = {
					culture = {
						has_innovation = innovation_war_camels
					}
					scope:defender = {
						any_realm_county = {
							culture = {
								has_innovation = innovation_war_camels
							}
							county_has_province_with_terrain = {
								terrain = drylands
								terrain = desert
							}
						}
					}
				}
			}
			if = {
				limit = {
					scope:defender = {
						any_realm_county = {
							culture = {
								has_innovation = innovation_war_camels
							}
							county_has_province_with_terrain = {
								terrain = drylands
								terrain = desert
							}
						}
					}
				}
				scope:defender = {
					random_realm_county = {
						limit = {
							culture = {
								has_innovation = innovation_war_camels
							}
							county_has_province_with_terrain = {
								terrain = drylands
								terrain = desert
							}
						}
						culture = {
							save_scope_as = army_culture
						}
					}
				}
			}
			else = {
				culture = {
					save_scope_as = army_culture
				}
			}
			spawn_army = {
				name = greek_anarchy_war_nomad_army_name
				uses_supply = yes
				inheritable = no
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = camel_rider
					stacks = greek_anarchy_army_maa_value
				}
				location = scope:attacker.capital_province
			}
		}
		#Heavy cav
		4 = {
			trigger = {
				OR = {
					culture = {
						has_innovation = innovation_arched_saddle
					}
					scope:defender = {
						culture = {
							has_innovation = innovation_arched_saddle
						}
					}
					scope:defender = {
						any_realm_county = {
							culture = {
								has_innovation = innovation_arched_saddle
							}
						}
					}
				}
				scope:defender = {
					highest_held_title_tier >= tier_kingdom
				}
			}
			spawn_army = {
				name = greek_anarchy_war_generic_army_name_2
				inheritable = no
				uses_supply = yes
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = armored_horsemen
					stacks = greek_anarchy_army_heavy_cav_value
				}
				location = scope:attacker.capital_province
			}
		}
		#Heavy Infantry
		1 = {
			trigger = {
				OR = {
					culture = {
						has_innovation = innovation_quilted_armor
					}
					scope:defender = {
						culture = {
							has_innovation = innovation_quilted_armor
						}
					}
					scope:defender = {
						any_realm_county = {
							culture = {
								has_innovation = innovation_quilted_armor
							}
						}
					}
				}
			}
			spawn_army = {
				name = greek_anarchy_war_generic_army_name_2
				inheritable = no
				uses_supply = yes
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = armored_footmen
					stacks = greek_anarchy_army_maa_value
				}
				location = scope:attacker.capital_province
			}
		}
		#Pikemen
		2 = {
			trigger = {
				scope:defender = {
					any_realm_county = {
						county_has_province_with_terrain = {
							terrain = mountains
						}
					}
				}
			}
			spawn_army = {
				name = greek_anarchy_war_generic_army_name_2
				inheritable = no
				uses_supply = yes
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = pikemen_unit
					stacks = greek_anarchy_army_maa_value
				}
				location = scope:attacker.capital_province
			}
		}
		#Archers
		2 = {
			trigger = {
				scope:defender = {
					any_realm_county = {
						county_has_province_with_terrain = {
							terrain = forest
						}
					}
				}
			}
			spawn_army = {
				name = greek_anarchy_war_generic_army_name_2
				inheritable = no
				uses_supply = yes
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = bowmen
					stacks = greek_anarchy_army_maa_value
				}
				location = scope:attacker.capital_province
			}
		}
		#Light Horsemen
		1 = {
			spawn_army = {
				name = greek_anarchy_war_generic_army_name_2
				inheritable = no
				uses_supply = yes
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = light_horsemen
					stacks = greek_anarchy_army_maa_value
				}
				location = scope:attacker.capital_province
			}
		}
		#Light Footmen
		1 = {
			spawn_army = {
				name = greek_anarchy_war_generic_army_name_2
				inheritable = no
				uses_supply = yes
				levies = greek_anarchy_army_levy_value
				men_at_arms = {
					type = light_footmen
					stacks = greek_anarchy_army_weak_maa_value
				}
				location = scope:attacker.capital_province
			}
		}
	}
}

expunge_latin_dominion_effect = {
	if = {
		limit = {
			OR = {
				ep3_orthodox_faith_trigger = yes
				faith = faith:hellenic_pagan
			}
		}
		add_character_modifier = {
			modifier = ascendant_byz_reclaimant_ortho_modifier
			years = 20
		}
	}
	else = {
		add_character_modifier = {
			modifier = ascendant_byz_reclaimant_modifier
			years = 20
		}
	}
}

frankokratia_latin_emp_end_msg_effect = {
	save_scope_as = latin_emp
	faith = {
		every_faith_ruler = {
			limit = {
				is_ai = no
				NOT = {
					has_character_flag = had_ep3_frankokratia_events_0100
				}
			}
			add_to_list = latin_destroyed_recipients
		}
	}
	title:e_latin_empire = {
		every_in_de_jure_hierarchy = {
			if = {
				limit = {
					holder ?= {
						is_ai = no
						NOR = {
							is_in_list = latin_destroyed_recipients
							has_character_flag = had_ep3_frankokratia_events_0100
						}
					}
				}
				holder = {
					add_to_list = latin_destroyed_recipients
				}
			}
		}
	}
	title:e_byzantium = {
		every_in_de_jure_hierarchy = {
			if = {
				limit = {
					holder ?= {
						is_ai = no
						NOR = {
							is_in_list = latin_destroyed_recipients
							has_character_flag = had_ep3_frankokratia_events_0100
						}
					}
				}
				holder = {
					add_to_list = latin_destroyed_recipients
				}
			}
		}
	}
	if = {
		limit = {
			#Notification looks weird if it's Byzantine empire no longer being a Latin
			NOT = {
				any_held_title = {
					this = title:e_byzantium
				}
			}
		}
		every_in_list = {
			list = latin_destroyed_recipients
			limit = {
				this != root
			}
			trigger_event = ep3_frankokratia_events.0100
		}
		#Trigger one day after for root, switch the titles
		if = {
			limit = {
				NOT = {
					has_character_flag = had_ep3_frankokratia_events_0100
				}
			}
			trigger_event = {
				id = ep3_frankokratia_events.0100
				days = 1
			}
		}
	}
}

frankokratia_remove__war_leader_var_effect = {
	if = {
		limit = {
			any_character_war = {
				using_cb = crusading_claim_cb
				any_war_participant = {
					this = scope:recipient
				}
				any_war_attacker = {
					any_owned_story = {
						type = frankokratia_story
						any_in_list = {
							variable = frankokratia_leaders
							this = scope:actor
						}
					}
				}
			}
		}
		random_character_war = {
			limit = {
				any_war_participant = {
					any_owned_story = {
						type = frankokratia_story
						any_in_list = {
							variable = frankokratia_leaders
							this = scope:actor
						}
					}
				}
			}
			random_war_participant = {
				limit = {
					any_owned_story = {
						type = frankokratia_story
						any_in_list = {
							variable = frankokratia_leaders
							this = scope:actor
						}
					}
				}
				random_owned_story = {
					type = frankokratia_story
					limit = {
						any_in_list = {
							variable = frankokratia_leaders
							this = scope:actor
						}
					}
					remove_list_variable = {
						name = frankokratia_leaders
						target = scope:actor
					}
				}
			}
		}
	}
}
