##################################################
### FRANKOKRATIA TRIGGERS
##################################################
ep3_frankokratia_ghw_trigger = {
	#There is an emperor of Byzantium
	exists = title:e_byzantium.holder
	#There is NOT a Latin emperor
	NOT = { exists = title:e_latin_empire.holder }
	#Byzantine emperor is Christian, but not Catholic
	title:e_byzantium.holder.faith = {
		NOT = { this = faith:catholic }
		OR = { religion = religion:eastern_orthodox_religion religion = religion:catholic_religion religion = religion:protestant_religion }
	}
	trigger_if = {
		limit = {
			title:e_byzantium.holder = {
				government_has_flag = government_is_special_administrative
			}
		}
		#Byzantine state faith is not Catholic
		title:e_byzantium.holder.primary_title = {
			state_faith = {
				NOT = { this = faith:catholic }
				OR = { religion = religion:eastern_orthodox_religion religion = religion:catholic_religion religion = religion:protestant_religion }
			}
		}
	}
	
	trigger_if = {
		limit = {
			has_game_rule = historicity_frankokratia_default
		}
		OR = {
			exists = global_var:cath_crusade_1
			current_date >= 1178.1.1
		}
	}
	trigger_else_if = {
		limit = {
			has_game_rule = historicity_frankokratia_blocked
		}
		always = no
	}
	trigger_else_if = {
		limit = {
			has_game_rule = historicity_frankokratia_fourth_crusade
		}
		OR = {
			AND = {
				current_date < 1178.1.1
				exists = global_var:cath_crusade_1
				global_var:cath_crusade_1 >= 3
			}
			AND = {
				current_date >= 1178.1.1
				exists = global_var:cath_crusade_1
			}
		}
	}
	trigger_else = {
		#historicity_frankokratia_high game rule has no start date-based restrictions
	}
	
	#Byz emperor or By capital is some kind of Greek
	OR = {
		title:e_byzantium.holder.culture = {
			OR = {
				this = culture:greek
				any_parent_culture_or_above = {
					this = culture:greek
				}
				has_cultural_pillar = heritage_byzantine
			}
		}
		title:e_byzantium.holder.capital_county.culture = {
			OR = {
				this = culture:greek
				any_parent_culture_or_above = {
					this = culture:greek
				}
				has_cultural_pillar = heritage_byzantine
			}
		}
	}
	#Looser requirements if we really want it to happen
	trigger_if = {
		limit = {
			current_date >= 1200.1.1
			#Latin empire hasn't been made before
			NOT = {
				has_global_variable = frankokratia_occurred
			}
			exists = global_var:cath_crusade_1
		}
		#A target that Mediterranean control could help reach.. and a bit more
		ghw_target_title = {
			OR = {
				title_capital_county.title_province = { geographical_region = world_middle_east_arabia }
				title_capital_county.title_province = { geographical_region = world_middle_east_jerusalem }
				title_capital_county.title_province = { geographical_region = world_asia_minor }
				title_capital_county.title_province = { geographical_region = world_africa_north }
				title_capital_county.title_province = { geographical_region = world_europe_west_iberia }
				this = title:k_caucasus
				this = title:k_pontic_steppe
				this = title:k_cyprus
				this = title:k_krete
				this = title:k_hellas
				this = title:k_sardinia
			}
		}
	}
	trigger_else = {
		#A target that Mediterranean control could help reach
		ghw_target_title = {
			OR = {
				title_capital_county.title_province = { geographical_region = world_middle_east_arabia }
				title_capital_county.title_province = { geographical_region = world_middle_east_jerusalem }
				title_capital_county.title_province = { geographical_region = world_asia_minor }
				title_capital_county.title_province = { geographical_region = world_africa_north }
				this = title:k_caucasus
				this = title:k_pontic_steppe
				this = title:k_cyprus
				this = title:k_krete
				this = title:k_hellas
			}
		}
	}
}

ep3_frankokratia_transiting_relative_trigger = {
	OR = {
		is_ruler = no
		AND = {
			exists = scope:char_temp
			highest_held_title_tier <= scope:char_temp.highest_held_title_tier
		}
		AND = {
			exists = root
			highest_held_title_tier <= root.highest_held_title_tier
		}
		highest_held_title_tier < tier_county
		NOT = {
			religion = religion:catholic_religion
		}
		is_imprisoned = yes
		is_incapable = yes
		capital_province = {
			NOR = {
				geographical_region = world_europe_south_italy
				geographical_region = world_europe_west_francia
				geographical_region = world_europe_west_germania
				geographical_region = world_europe_west_iberia
				geographical_region = custom_hungary
				geographical_region = custom_west_slavia
				kingdom = title:k_croatia
				kingdom = title:k_bavaria
			}
		}
		is_vassal_of = title:e_byzantium.holder
		is_at_war_with = title:e_byzantium.holder
	}
	is_ai = yes
}

ep3_frankokratia_story_owner_trigger = {
	save_temporary_scope_as = char_temp
	religion = religion:catholic_religion
	is_playable_character = yes
	highest_held_title_tier >= tier_county
	highest_held_title_tier <= tier_kingdom
	exists = faith.religious_head
	faith.religious_head = { is_ai = yes }
	scope:char_temp != faith.religious_head
	exists = scope:char_temp.faith.great_holy_war
	#Not pals with the Byzantine emperor
	exists = title:e_byzantium.holder
	is_imprisoned = no
	is_incapable = no
	#Use this to eliminate owners that might lose the story
	OR = {
		exists = primary_heir
		exists = player_heir
	}
	NOR = {
		is_allied_to = title:e_byzantium.holder
		top_liege = title:e_byzantium.holder
		is_at_war_with = title:e_byzantium.holder
		has_truce = title:e_byzantium.holder
		has_relation_friend = title:e_byzantium.holder
		has_relation_lover = title:e_byzantium.holder
		has_relation_soulmate = title:e_byzantium.holder
	}
	#Needs to be interesteed in working with Latin shipbuilders
	capital_province = {
		OR = {
			geographical_region = world_europe_south_italy
			geographical_region = world_europe_west_francia
			geographical_region = world_europe_west_germania
			geographical_region = world_europe_west_iberia
			geographical_region = custom_hungary
			geographical_region = custom_west_slavia
			kingdom = title:k_croatia
			kingdom = title:k_bavaria
		}
	}
	#Needs to be related to a claimant
	OR = {
		#Close family claimant
		any_close_or_extended_family_member = {
			OR = {
				ep3_frankokratia_claimant_trigger = yes

				AND = {
					ep3_frankokratia_transiting_relative_trigger = yes
					#In-law claimant
					any_spouse = {
						ep3_frankokratia_claimant_trigger = yes
					}
				}
			}
		}
		#Close family in-law
		any_close_family_member = {
			ep3_frankokratia_transiting_relative_trigger = yes
			any_spouse = {
				ep3_frankokratia_transiting_relative_trigger = yes
				any_close_family_member = {
					ep3_frankokratia_claimant_trigger = yes
				}
			}
		}
		#Spouse claimant
		any_spouse = {
			OR = {
				ep3_frankokratia_claimant_trigger = yes
				AND = {
					ep3_frankokratia_transiting_relative_trigger = yes
					any_close_family_member = {
						ep3_frankokratia_claimant_trigger = yes
					}
				}
				
			}
		}
		#Courtier claimant
		any_courtier_or_guest = {
			is_foreign_court_guest = no
			ep3_frankokratia_claimant_trigger = yes
		}
		#friends
		any_relation = {
			type = friend
			ep3_frankokratia_claimant_trigger = yes
		}
		#soulmates
		any_relation = {
			type = soulmate
			ep3_frankokratia_claimant_trigger = yes
		}
		#any pool character
		any_pool_character = {
			province = scope:char_temp.capital_province
			ep3_frankokratia_claimant_trigger = yes
		}
		
	}
	#Should be vaguely Western in culture
	culture = {
		OR = {
			has_cultural_pillar = heritage_goidelic
			has_cultural_pillar = heritage_west_germanic
			has_cultural_pillar = heritage_brythonic
			has_cultural_pillar = heritage_north_germanic
			has_cultural_pillar = heritage_iberian
			has_cultural_pillar = heritage_latin
			has_cultural_pillar = heritage_south_slavic
			has_cultural_pillar = heritage_magyar
			has_cultural_pillar = heritage_west_slavic
			has_cultural_pillar = heritage_central_germanic
			has_cultural_pillar = heritage_frankish
		}
	}
}

ep3_frankokratia_claimant_trigger = {
	trigger_if = {
		limit = {
			exists = scope:char_temp
		}
		highest_held_title_tier < scope:char_temp.highest_held_title_tier
	}
	OR = {
		has_claim_on = title:e_byzantium
		AND = {
			title:e_byzantium.holder.capital_county = title:c_byzantion
			has_claim_on = title:k_thessalonika
		}
		AND = {
			title:e_byzantium.holder.capital_county = title:c_byzantion
			has_claim_on = title:d_thrace
		}
		AND = {
			title:e_byzantium.holder.capital_county = title:c_byzantion
			has_claim_on = title:c_byzantion
		}
		AND = {
			is_child_of = title:e_byzantium.holder
			title:e_byzantium.holder = {
				has_realm_law = equal_law
			}
		}
		AND = {
			is_child_of = title:e_byzantium.holder
			is_male = yes
			title:e_byzantium.holder = {
				OR = {
					has_realm_law = male_preference_law
					has_realm_law = male_only_law
				}
			}
		}
		AND = {
			is_child_of = title:e_byzantium.holder
			is_female = yes
			title:e_byzantium.holder = {
				OR = {
					has_realm_law = female_preference_law
					has_realm_law = female_only_law
				}
			}
		}
	}
	age >= 10
	is_imprisoned = no
	is_healthy = yes
	is_ai = yes
	OR = {
		has_claim_on = title:e_byzantium
		ai_boldness >= 0
		ai_greed >= 0
		ai_honor <= 0
	}
	NOR = {
		is_vassal_of = title:e_byzantium.holder
		is_consort_of = title:e_byzantium.holder
		has_relation_soulmate = title:e_byzantium.holder
		has_relation_friend = title:e_byzantium.holder
		has_relation_best_friend = title:e_byzantium.holder
	}
	NAND = {
		OR = {
			is_landed = yes
			is_landless_ruler = yes
		}
		top_liege = title:e_byzantium.holder
	}
}

ep3_frankokratia_will_start_trigger = {
	NOR = {
		has_trait = just
		has_trait = humble
		has_trait = content
		has_trait = lazy
		has_trait = craven
		has_trait = loyal
	}
	OR = {
		has_trait = greedy
		has_trait = eccentric
		has_trait = ambitious
		has_trait = fickle
		has_trait = arbitrary
		AND = {
			ai_rationality <= 0
			ai_boldness >= 0
			ai_honor <= 0
			ai_energy >= 0
		}
	}
	
}

ep3_frankokratia_financier_trigger = {
	religion = religion:catholic_religion
	is_ai = yes
	#Not pals with byz emperor
	NOR = {
		is_allied_to = title:e_byzantium.holder
		is_vassal_of = title:e_byzantium.holder
		has_relation_friend = title:e_byzantium.holder
		has_relation_lover = title:e_byzantium.holder
		has_relation_soulmate = title:e_byzantium.holder
		AND = {
			exists = faith.religious_head
			this = faith.religious_head
		}
	}
	#Gotta have money to be a financier
	gold >= 0
	#Your capital needs to be somewhere useful
	capital_county = {
		title_province = {
			OR = {
				geographical_region = world_europe_south
				geographical_region = world_europe_west_iberia
				geographical_region = world_europe_west_francia
			}
		}
		is_coastal_county = yes
	}
	is_at_war = no
}

ep3_frankokratia_financier_sub_trigger = {
	highest_held_title_tier >= tier_county
	government_has_flag = government_is_republic
	ep3_frankokratia_financier_trigger = yes
}

ep3_frankokratia_financier_exists_trigger = {
	OR = {
		AND = {
			exists = title:k_venice.holder
			title:k_venice.holder = {
				ep3_frankokratia_financier_sub_trigger = yes
			}
		}
		title:e_italy = {
			any_in_de_jure_hierarchy = {
				count >= 1
				holder ?= {
					ep3_frankokratia_financier_sub_trigger = yes
				}
			}
		}
		title:k_sicily = {
			any_in_de_jure_hierarchy = {
				count >= 1
				holder ?= {
					ep3_frankokratia_financier_sub_trigger = yes
				}
			}
		}
		title:k_croatia = {
			any_in_de_jure_hierarchy = {
				count >= 1
				holder ?= {
					ep3_frankokratia_financier_sub_trigger = yes
				}
			}
		}
		title:k_burgundy = {
			any_in_de_jure_hierarchy = {
				count >= 1
				holder ?= {
					ep3_frankokratia_financier_sub_trigger = yes
				}
			}
		}
		title:k_aquitaine = {
			any_in_de_jure_hierarchy = {
				count >= 1
				holder ?= {
					ep3_frankokratia_financier_sub_trigger = yes
				}
			}
		}
		title:e_spain = {
			any_in_de_jure_hierarchy = {
				count >= 1
				holder ?= {
					ep3_frankokratia_financier_sub_trigger = yes
				}
			}
		}
		title:e_italy = {
			any_in_de_jure_hierarchy = {
				count >= 1
				holder ?= {
					highest_held_title_tier >= tier_county
					highest_held_title_tier < tier_empire
					ep3_frankokratia_financier_trigger = yes
				}
			}
		}
		title:k_sicily = {
			any_in_de_jure_hierarchy = {
				count >= 1
				holder ?= {
					highest_held_title_tier >= tier_county
					highest_held_title_tier < tier_empire
					ep3_frankokratia_financier_trigger = yes
				}
			}
		}
		title:k_croatia = {
			any_in_de_jure_hierarchy = {
				count >= 1
				holder ?= {
					highest_held_title_tier >= tier_county
					highest_held_title_tier < tier_empire
					ep3_frankokratia_financier_trigger = yes
				}
			}
		}
	}	
}

ep3_frankokratia_valid_joiner_trigger = {
	NOR = {
		has_character_flag = had_event_frankokratia_0020
		this = faith.religious_head
		any_liege_or_above = {
			this = title:e_byzantium.holder
		}
		is_allied_to = title:e_byzantium.holder
		is_vassal_of = title:e_byzantium.holder
		has_truce = title:e_byzantium.holder
		has_relation_friend = title:e_byzantium.holder
		any_owned_story = {
			type = frankokratia_story
		}
	}
}

ep3_frankokratia_latin_kingdom_available_trigger = {
	OR = {
		NOT = { exists = holder }
		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
			}
		}
	}
	#At least 2 counties in the kingdom belong to byzantine emperor
	any_de_jure_county = {
		count >= 2
		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
				}
			}
		}
	}
}

ep3_frankokratia_discard_old_lands_trigger = {
	NAND = {
		primary_title = {
			tier <= tier_empire
			empire = title:e_byzantium
		}
		any_sub_realm_county = {
			empire = title:e_byzantium
		}
	}
}

ep3_frankokratia_beneficiary_trigger = {
	is_ruler = no
	is_imprisoned = no
	is_adult = yes
	faith = scope:frankokratia_crusader.faith
	NOR = {
		this = scope:financier
		any_heir_title = { }
		has_trait = incapable
		has_trait = devoted
		AND = {
			scope:frankokratia_crusader.faith = {
				has_doctrine = doctrine_theocracy_temporal
			}
			has_council_position = councillor_court_chaplain
		}
	}
	OR = { #Either not married, or not married to a ruler.
		is_married = no
		NOT = {
			any_spouse = {
				is_ruler = yes
			}
		}
	}
	OR = {
		AND = {
			is_male = yes
			age < 50
		}
		AND = {
			is_female = yes
			age <= 35
		}
		any_child = {
			count >= 1
			is_ruler = no
			ep3_frankokratia_beneficiary_gender_soft_trigger = yes
		}
	}
}

ep3_frankokratia_beneficiary_gender_trigger = {
	trigger_if = {
		limit = {
			scope:frankokratia_crusader = {
				OR = {
					has_realm_law = male_preference_law
					has_realm_law = male_only_law
				}
			}
		}
		is_male = yes
	}
	trigger_else_if = {
		limit = {
			scope:frankokratia_crusader = {
				OR = {
					has_realm_law = female_preference_law
					has_realm_law = female_only_law
				}
			}
		}
		is_female = yes
	}
	trigger_else = {
		scope:frankokratia_crusader = {
			has_realm_law = equal_law
		}
	}
}

ep3_frankokratia_beneficiary_gender_soft_trigger = {
	trigger_if = {
		limit = {
			scope:frankokratia_crusader = {
				has_realm_law = male_only_law
			}
		}
		is_male = yes
	}
	trigger_else_if = {
		limit = {
			scope:frankokratia_crusader = {
				has_realm_law = female_only_law
			}
		}
		is_female = yes
	}
	trigger_else = {
		scope:frankokratia_crusader = {
			OR = {
				has_realm_law = male_preference_law
				has_realm_law = female_preference_law
				has_realm_law = equal_law
			}
		}
	}
}

ep3_frankokratia_notification_recipient_trigger = {
	OR = {
		#Rulers in the Byz region
		capital_province = {
			county = {
				empire = title:e_byzantium
			}
		}
		#Most affected Christians
		religion = religion:catholic_religion
		faith = faith:orthodox
		faith = faith:armenian_apostolic
		#Target faith of crusade
		faith = scope:defender.faith
		#Vassals of crusade defender
		liege ?= scope:defender
		#Vassals of byz emp
		AND = {
			exists = title:e_byzantium.holder
			liege ?= title:e_byzantium.holder
		}
		#Byz emp
		AND = {
			exists = title:e_byzantium.holder
			this ?= title:e_byzantium.holder
		}
		is_in_list = non_christian_franko_leaders
	}
}

ep3_orthodox_faith_trigger = {
	religion = religion:eastern_orthodox_religion
}
