namespace = PlusGreatSchism

#######################################
#
#	CK2Plus Schism Events
#
#######################################
#
#	CATALOG
#
#	001 - 040: Schism and Prevention
#	041 - 050: Greek/Latin Rites
#	051 - 060: The Pentarchy
#	061 - 070: Ecumenical Councils
#	071 - 080: Schism Lead Up
#
#######################################

#######################################
#
#!	001 - 010: Schism and Prevention
#
#######################################
#
#	INDEX
#
#	001: Pre-schism setup
#	002: Schism Prevented
#	003: Narrate Schism Prevented
#	004: Schism Occurs
#	005: Narrate Schism Occurs
#	006: Schism Override
#
#######################################

# 001: Pre-schism setup
# Executed on new game start before 1054 and if the Schism is reverted by game rule
character_event = {
	id = PlusGreatSchism.001

	hide_window = yes
	is_triggered_only = yes # PlusStartUp.001

	trigger = {
		NOT = { has_global_flag = great_schism_happened }
	}

	immediate = {
		set_global_flag = setting_up_pre_schism_setup

		set_variable = {
			which = global_great_schism_tracker
			value = 0
		}

		# Advance tracker if Carolingian Empire already formed
		if = {
			limit = { has_global_flag = carolingian_empire_formed }

			change_variable = {
				which = global_great_schism_tracker
				value = 1
			}
		}

		# Advance tracker if using New Millenium or later bookmark
		if = {
			limit = { year >= 999 }

			set_global_flag = filioque_adopted
			set_global_flag = eucharist_conflict

			change_variable = {
				which = global_great_schism_tracker
				value = 2
			}
		}

		# Set Catholic/Orthodox heresies to Chalcedonian
		set_parent_religion = { religion = cathar parent = chalcedonian }
		set_parent_religion = { religion = waldensian parent = chalcedonian }
		set_parent_religion = { religion = lollard parent = chalcedonian }
		set_parent_religion = { religion = adoptionist parent = chalcedonian }
		set_parent_religion = { religion = bogomilist parent = chalcedonian }
		set_parent_religion = { religion = monothelite parent = chalcedonian }
		# set_parent_religion = { religion = iconoclast parent = chalcedonian } # Iconoclasm is a trait now, so we don't want random Iconoclast heresies popping up

		# Switch religion head titles
		chalcedonian = {
			set_religion_head = k_papacy
			unsafe_destroy_landed_title = k_papacy_chalcedonian

			activate_title = {
				title = k_papacy_chalcedonian
				status = no
			}

			k_papacy = {
				religion = chalcedonian
			}

			k_orthodox = {
				religion = chalcedonian
			}
		}
		catholic = {
			set_religion_head = none
		}
		orthodox = {
			set_religion_head = none
		}
		# iconoclast = {
		#	set_religion_head = none
		# }

		# Convert mercenary captains and courtiers
		any_mercenary_band = {
			limit = {
				holder_scope = {
					OR = {
						religion = catholic
						religion = orthodox
					}
				}
			}

			holder_scope = {
				unsafe_religion = chalcedonian

				any_courtier = {
					unsafe_religion = chalcedonian
				}
			}
		}

		# Convert holy orders
		any_title = {
			limit = {
				holy_order = yes

				holder_scope = {
					holy_order = yes

					OR = {
						religion = catholic
						religion = orthodox
					}
				}
			}

			save_persistent_event_target = {
				name = holy_order_religion
				scope = chalcedonian
			}

			religion = chalcedonian

			holder_scope = {
				unsafe_religion = chalcedonian

				any_courtier = {
					unsafe_religion = chalcedonian
				}
			}
		}

		# Convert Catholic and Orthodox provinces, in starts after 1054
		any_province = {
			limit = {
				OR = {
					religion = catholic
					religion = orthodox
					religion = iconoclast
				}
			}

			religion = chalcedonian
		}

		# Switch any remaining characters, assign missing rites
		any_character = {
			if = {
				limit = {
					religion = chalcedonian

					NOR = {
						trait = latin_rites
						trait = greek_rites
						trait = iconoclast_rites
					}
				}

				if = {
					limit = {
						e_byzantium = {
							holder_scope = {
								trait = iconoclast_rites

								OR = {
									is_liege_or_above_of = PREVPREV
									de_jure_vassal_or_below = PREVPREV
								}
							}
						}

						culture_group = byzantine
					}

					add_trait = iconoclast_rites
				}
				else_if = {
					limit = {
						trigger_if = {
							limit = { location = { region = custom_latin_rites } }

							NOR = {
								culture_group = byzantine
								culture_group = east_slavic

								trigger_if = {
									limit = { culture_group = south_slavic }

									OR = {
										culture = bulgarian
										culture = serbian
									}
								}
							}
						}
						trigger_else = {
							OR = {
								culture_group = latin
								culture_group = italian_group
							}
						}
					}

					add_trait = latin_rites
				}
				else = {
					add_trait = greek_rites
				}
			}
			else_if = {
				limit = {
					OR = {
						religion = catholic
						religion = orthodox
						religion = iconoclast
					}
				}

				# Rites are religious traits, so they are cleared when changing religion
				# Therefore, they have to be handed out after conversion
				if = {
					limit = { religion = catholic }
					set_character_flag = give_latin_rites
					add_trait = latin_rites
				}
				else_if = {
					limit = { religion = orthodox }
					set_character_flag = give_greek_rites
					add_trait = greek_rites
				}
				else_if = {
					limit = { religion = iconoclast }
					set_character_flag = give_iconoclast_rites
					add_trait = iconoclast_rites
				}

				# Workaround for automatic usurpation of theocratic characters
				chalcedonian = { save_event_target_as = target_religion }
				convert_religion_failsafe_effect = yes

				trigger_switch = {
					on_trigger = has_character_flag

					give_latin_rites = {
						clr_character_flag = give_latin_rites
						add_trait = latin_rites
					}

					give_greek_rites = {
						clr_character_flag = give_greek_rites
						add_trait = greek_rites
					}

					give_iconoclast_rites = {
						clr_character_flag = give_iconoclast_rites
						add_trait = iconoclast_rites
					}
				}
			}

			# Manually set investiture succession - not set automatically, due to requiring unset latin_rites trait
			any_demesne_title = {
				limit = { is_law_potential = succ_catholic_bishopric }
				add_law = succ_catholic_bishopric
			}
		}

		# Assign pentarchs to titles
		character_event = { id = PlusGreatSchism.051 }
		clr_global_flag = setting_up_pre_schism_setup

		# Populate the college of cardinals, game doesn't always do this automatically, for some reason
		# Disabled due to not actually working - hardcoding keeps this from working properly, it appears
	#	while = {
	#		count = 9
	#
	#		random_character = {
	#			limit = {
	#				religion = chalcedonian
	#				is_theocracy = yes
	#				is_landed = yes
	#
	#				rightful_religious_head_scope = {
	#					controls_religion = yes
	#					is_within_diplo_range = PREV
	#				}
	#
	#				NOR = {
	#					rightful_religious_head = THIS
	#					has_minor_title = title_chalcedonian_cardinal
	#				}
	#			}
	#
	#			give_minor_title = title_chalcedonian_cardinal
	#		}
	#	}
	}
}

# 002: Schism Prevented (Setup)
character_event = {
	id = PlusGreatSchism.002

	is_triggered_only = yes
	hide_window = yes

	trigger = {
		NOR = {
			has_global_flag = great_schism_prevented
			has_global_flag = great_schism_disabled
		}
	}

	immediate = {
		set_global_flag = great_schism_prevented

		# This is primarily for those firing the event via console
		if = {
			limit = {
				NOR = {
					has_global_flag = patriarch_supreme_is_greek
					has_global_flag = patriarch_supreme_is_latin
				}
			}

			if = {
				limit = {
					k_papacy = {
						holder_scope = {
							OR = {
								trait = greek_rites
								trait = iconoclast_rites
							}
						}
					}
				}
				set_global_flag = patriarch_supreme_is_greek
			}
			else = {
				set_global_flag = patriarch_supreme_is_latin
			}
		}

		# Greek Victory
		if = {
			limit = { has_global_flag = patriarch_supreme_is_greek }

			chalcedonian = {
				set_religion_head = k_orthodox

				# Activate Orthodox-like features, elevating primacy of bishop of Constantinople
				set_can_grant_invasion_cb = invasion
			}

			# Disable Papacy
			k_papacy = {
				unsafe_destroy_landed_title = THIS

				activate_title = {
					title = THIS
					status = no
				}
			}

			# Prepare and fire narrative event
			k_orthodox = {
				holder_scope = {
					narrative_event = { id = PlusGreatSchism.003 }
				}
			}
		}

		# Latin Victory
		else = {
			chalcedonian = {
				# Activate Catholic-like features, elevating primacy of bishop of Rome
				set_pentarchy = no
				set_autocephaly = no
				set_can_grant_invasion_cb = invasion
				set_can_have_antipopes = yes
				set_can_grant_claim = yes
			}

			# Disable Ecumenical Patriarchate
			k_orthodox = {
				unsafe_destroy_landed_title = THIS

				activate_title = {
					title = THIS
					status = no
				}
			}

			# Prepare and fire narrative event
			k_papacy = {
				holder_scope = {
					narrative_event = { id = PlusGreatSchism.003 }
				}
			}

			# Populate the college of cardinals, game doesn't always do this automatically, for some reason
			# Disabled due to not actually working - hardcoding keeps this from working properly, it appears
		#	while = {
		#		count = 9
		#
		#		random_character = {
		#			limit = {
		#				religion = chalcedonian
		#				is_theocracy = yes
		#				is_landed = yes
		#
		#				rightful_religious_head_scope = {
		#					controls_religion = yes
		#					is_within_diplo_range = PREV
		#				}
		#
		#				NOR = {
		#					rightful_religious_head = THIS
		#					has_minor_title = title_chalcedonian_cardinal
		#				}
		#			}
		#
		#			give_minor_title = title_chalcedonian_cardinal
		#		}
		#	}
		}
	}
}

# 003: Narrate Schism Prevented
narrative_event = {
	id = PlusGreatSchism.003
	title = PlusGreatSchism_003_title
	picture = GFX_evt_pope
	border = GFX_event_narrative_frame_religion

	is_triggered_only = yes
	major = yes

	major_trigger = {
		ai = no
	}

	desc = {
		text = PlusGreatSchism_003_text_pope_1
		trigger = {
			ROOT = { has_landed_title = k_papacy }
			FROM = { NOT = { character = ROOT } }
		}
	}
	desc = {
		text = PlusGreatSchism_003_text_pope_2
		trigger = {
			ROOT = { has_landed_title = k_papacy }
			FROM = { character = ROOT }
		}
	}
	desc = {
		text = PlusGreatSchism_003_text_patriarch_1
		trigger = {
			ROOT = { has_landed_title = b_hagiasophia }
			FROM = { NOT = { character = ROOT } }
		}
	}
	desc = {
		text = PlusGreatSchism_003_text_patriarch_2
		trigger = {
			ROOT = { has_landed_title = b_hagiasophia }
			FROM = { character = ROOT }
		}
	}

	option = {
		name = {
			text = PlusGreatSchism_003_1a # Hail to [Root.GetHerHisCap] Holiness
			trigger = { religion = chalcedonian }
		}
		name = {
			text = PlusGreatSchism_003_1b # So there will be no schism? How unexpected
			trigger = {
				religion_group = christian
				NOT = { religion = chalcedonian }
			}
		}
		name = {
			text = PlusGreatSchism_003_1c # This does not bode well...
			trigger = {
				NOT = { religion_group = christian }
			}
		}
	}
}

# 004: Schism Occurs
character_event = {
	id = PlusGreatSchism.004

	hide_window = yes

	only_playable = yes

	trigger = {
		owns = event_target:pulse_province
		year >= 950

		k_papacy = {
			holder_scope = {
				religion = chalcedonian
				trait = latin_rites
			}
		}

		b_hagiasophia = {
			holder_scope = {
				religion = chalcedonian

				OR = {
					trait = greek_rites
					trait = iconoclast_rites
				}
			}
		}

		check_variable = {
			which = global_great_schism_tracker
			value >= 4.0
		}

		NOR = {
			has_global_flag = great_schism_prevented
			has_global_flag = great_schism_happened
			has_global_flag = great_schism_disabled
		}
	}

	mean_time_to_happen = {
		years = 100

		mult_modifier = {
			factor = 0.5
			year >= 1000
		}

		mult_modifier = {
			factor = 0.5
			year >= 1050
		}

		mult_modifier = {
			factor = 0.5
			year >= 1100
		}

		mult_modifier = {
			factor = 0.5

			check_variable = {
				which = global_great_schism_tracker
				value >= 5.0
			}
		}

		mult_modifier = {
			factor = 0.5

			check_variable = {
				which = global_great_schism_tracker
				value >= 6.0
			}
		}

		mult_modifier = {
			factor = 0.5

			check_variable = {
				which = global_great_schism_tracker
				value >= 7.0
			}
		}
	}

	immediate = {
		set_global_flag = great_schism_happened

		b_hagiasophia = {
			holder_scope = {
				if = {
					limit = { trait = greek_rites }

					orthodox = { save_event_target_as = eastern_religion }
					iconoclast = { save_event_target_as = eastern_heresy }
				}
				else = {
					orthodox = { save_event_target_as = eastern_heresy }
					iconoclast = { save_event_target_as = eastern_religion }
				}
			}
		}

		# Convert the Religion Heads
		k_papacy = {
			religion = catholic

			catholic = {
				set_religion_head = PREV
			}

			holder_scope = {
				# Workaround for automatic usurpation of theocratic characters
				chalcedonian = { save_event_target_as = target_religion }
				convert_religion_failsafe_effect = yes
			}
		}

		k_orthodox = {
			religion = orthodox

			orthodox = {
				set_religion_head = PREV
			}

			holder_scope = {
				# Workaround for automatic usurpation of theocratic characters
				chalcedonian = { save_event_target_as = target_religion }
				convert_religion_failsafe_effect = yes
			}
		}

		iconoclast = {
			set_religion_head = d_iconoclast
		}

		## Convert holy orders
		# Exhaustive list to convert, if present:
		# - Knights of Calatrava
		# - Kinghts Hospitaller
		# - Knights of Santiago
		# - Knights Templar
		# - Livonian Order
		# - Teutonic Order
		# - Teutonic State
		# - Brotherhood of the Holy Sepulchre

		d_holy_sepulchre = {
			save_persistent_event_target = {
				name = holy_order_religion
				scope = orthodox
			}

			if = {
				limit = { has_holder = yes }

				religion = orthodox

				holder_scope = {
					unsafe_religion = orthodox

					any_courtier = {
						unsafe_religion = orthodox
					}
				}
			}
		}

		# Convert all the other ones to Catholic
		any_landed_title = {
			limit = {
				holy_order = yes

				holder_scope = {
					religion = chalcedonian
					holy_order = yes
				}
			}

			save_persistent_event_target = {
				name = holy_order_religion
				scope = catholic
			}

			religion = catholic

			holder_scope = {
				unsafe_religion = catholic

				any_courtier = {
					unsafe_religion = catholic
				}
			}
		}

		# Convert mercenary captains and courtiers
		any_mercenary_band = {
			limit = {
				holder_scope = {
					religion = chalcedonian
				}
			}

			holder_scope = {
				trigger_switch = {
					on_trigger = trait

					latin_rites = { unsafe_religion = catholic }
					greek_rites = { unsafe_religion = orthodox }
					iconoclast_rites = { unsafe_religion = iconoclast }
				}
			}

			religion = holder_scope
		}

		# Convert the People
		any_character = {
			limit = {
				OR = {
					religion = chalcedonian
					secret_religion = chalcedonian
				}
			}

			# Workaround for automatic usurpation of theocratic characters
			if = {
				limit = { trait = greek_rites }
				event_target:eastern_religion = { save_event_target_as = target_religion }
			}
			else_if = {
				limit = { trait = iconoclast_rites }
				iconoclast = { save_event_target_as = target_religion }
			}
			else = {
				catholic = { save_event_target_as = target_religion }
			}

			if = {
				limit = { secret_religion = chalcedonian }
				set_secret_religion = event_target:target_religion
			}
			else = {
				convert_religion_failsafe_effect = yes
			}

			# Convert the Land ruled by Chalcedonians
			if = {
				limit = { num_of_count_titles > 0 }

				any_demesne_province = {
					limit = { owned_by = PREV }

					if = {
						limit = { religion = chalcedonian }
						religion = owner
					}
					else_if = {
						limit = { has_province_modifier = secret_chalcedonian_community }

						remove_province_modifier = secret_chalcedonian_community

						if = {
							limit = { owner = { religion = catholic } }

							add_province_modifier = {
								name = secret_catholic_community
								duration = -1
							}
						}
						else_if = {
							limit = { owner = { religion = orthodox } }

							add_province_modifier = {
								name = secret_orthodox_community
								duration = -1
							}
						}
						else = {
							add_province_modifier = {
								name = secret_iconoclast_community
								duration = -1
							}
						}
					}
				}
			}
		}

		# Convert the Land ruled by others
		any_province = {
			limit = { religion = chalcedonian }

			if = {
				limit = { region = custom_latin_rites }
				religion = catholic
			}
			else_if = {
				limit = { region = custom_greek_rites }
				religion = event_target:eastern_religion
			}
		}

		# Reset catholic/orthodox heresies
		set_parent_religion = { religion = cathar parent = catholic }
		set_parent_religion = { religion = waldensian parent = catholic }
		set_parent_religion = { religion = lollard parent = catholic }
		set_parent_religion = { religion = adoptionist parent = catholic }
		set_parent_religion = { religion = event_target:eastern_heresy parent = event_target:eastern_religion }
		set_parent_religion = { religion = iconoclast parent = event_target:eastern_religion }
		set_parent_religion = { religion = monothelite parent = event_target:eastern_religion }

		if = {
			limit = { NOT = { is_title_active = d_bogomilist } }

			set_parent_religion = {
				religion = bogomilist
				parent = event_target:eastern_religion
			}
		}

		set_parent_religion = { religion = chalcedonian parent = 0 }

		# Populate the college of cardinals, game doesn't always do this automatically, for some reason
		# Disabled due to not actually working - hardcoding keeps this from working properly, it appears
	#	while = {
	#		count = 9
	#
	#		random_character = {
	#			limit = {
	#				religion = chalcedonian
	#				is_theocracy = yes
	#				is_landed = yes
	#
	#				rightful_religious_head_scope = {
	#					controls_religion = yes
	#					is_within_diplo_range = PREV
	#				}
	#
	#				NOR = {
	#					rightful_religious_head = THIS
	#					has_minor_title = title_cardinal
	#				}
	#			}
	#
	#			give_minor_title = title_cardinal
	#		}
	#	}

		# Inform players
		any_player = {
			narrative_event = { id = PlusGreatSchism.005 } # Narrate the Schism
		}
	}
}

# 005: Narrate Schism Occurs
narrative_event = {
	id = PlusGreatSchism.005
	title = PlusGreatSchism_005_title
	desc = PlusGreatSchism_005_text
	picture = GFX_evt_great_schism
	border = GFX_event_narrative_frame_religion

	is_triggered_only = yes # 004, above
	hide_from = yes

	option = {
		# Formerly Chalcedonian response
		name = {
			text = PlusGreatSchism_005_1c # This is of great concern...
			trigger = {
				OR = {
					religion = catholic
					religion = orthodox
				}
			}
		}
		# No one else cares
		name = {
			text = PlusGreatSchism_005_1d # So Christianity divides itself yet again
			trigger = {
				NOR = {
					religion = catholic
					religion = orthodox
				}
			}
		}

		show_portrait = k_papacy
		show_portrait = b_hagiasophia
	}
}

# 006: Schism Override
# Fired if Schism is disabled by game rule on starts before 1054
character_event = {
	id = PlusGreatSchism.006

	is_triggered_only = yes
	hide_window = yes

	has_global_flag = great_schism_manual_override

	immediate = {
		set_global_flag = great_schism_happened

		# Convert the Religion Heads
		k_papacy = {
			holder_scope = {
				# Workaround for automatic usurpation of theocratic characters
				catholic = { save_event_target_as = target_religion }
				convert_religion_failsafe_effect = yes
			}
		}

		k_orthodox = {
			holder_scope = {
				if = { # Only convert if not iconoclast (769 start)
					limit = { religion = chalcedonian }

					# Workaround for automatic usurpation of theocratic characters
					orthodox = { save_event_target_as = target_religion }
					convert_religion_failsafe_effect = yes
				}
				else = { # Restore 769 iconoclast controversy setup
					d_iconoclast = {
						activate_title = {
							title = THIS
							status = yes
						}

						gain_title = PREV
					}

					k_orthodox = {
						unsafe_destroy_landed_title = THIS

						activate_title = {
							title = THIS
							status = no
						}
					}

					set_name = "" # Name changes upon becoming a religious head
				}
			}
		}

		# Convert the People
		any_character = {
			limit = { religion = chalcedonian }

			# Workaround for automatic usurpation of theocratic characters
			if = {
				limit = {
					trigger_if = {
						limit = { location = { region = custom_latin_rites } }

						NOR = {
							culture_group = byzantine
							culture_group = east_slavic

							trigger_if = {
								limit = { culture_group = south_slavic }

								OR = {
									culture = bulgarian
									culture = serbian
								}
							}
						}
					}
					trigger_else = {
						OR = {
							culture_group = latin
							culture_group = italian_group
						}
					}
				}

				catholic = { save_event_target_as = target_religion }
			}
			else = {
				orthodox = { save_event_target_as = target_religion }
			}

			convert_religion_failsafe_effect = yes
		}

		# Convert the Land
		any_province = {
			limit = { religion = chalcedonian }

			if = {
				limit = {
					trigger_if = {
						limit = { region = custom_latin_rites }

						NOR = {
							culture_group = byzantine
							culture_group = east_slavic

							trigger_if = {
								limit = { culture_group = south_slavic }

								OR = {
									culture = bulgarian
									culture = serbian
								}
							}
						}
					}
					trigger_else = {
						OR = {
							culture_group = latin
							culture_group = italian_group
						}
					}
				}
				religion = catholic
			}
			else = {
				religion = orthodox
			}
		}

		# Convert mercenary captains and courtiers
		any_mercenary_band = {
			limit = {
				holder_scope = {
					religion = chalcedonian
				}
			}

			holder_scope = {
				unsafe_religion = location
				any_courtier_or_vassal = { unsafe_religion = liege }
			}

			religion = holder_scope
		}

		# Populate the college of cardinals, game doesn't always do this automatically, for some reason
		# Disabled due to not actually working - hardcoding keeps this from working properly, it appears
	#	while = {
	#		count = 9
	#
	#		random_character = {
	#			limit = {
	#				religion = chalcedonian
	#				is_theocracy = yes
	#				is_landed = yes
	#
	#				rightful_religious_head_scope = {
	#					controls_religion = yes
	#					is_within_diplo_range = PREV
	#				}
	#
	#				NOR = {
	#					rightful_religious_head = THIS
	#					has_minor_title = title_cardinal
	#				}
	#			}
	#
	#			give_minor_title = title_cardinal
	#		}
	#	}
	}
}

#!!

#######################################
#
#!	041 - 050: Greek/Latin Rites
#
#######################################
#
#	INDEX
#
#	041: Greek/Latin Rites Control
#	042: Patriarch demands that a priest switch rites
#	043: Relations between Greek/Latin realms
#
#######################################

# 041: Greek/Latin Rites Control
character_event = {
	id = PlusGreatSchism.041
	desc = PlusGreatSchism_041_text
	picture = GFX_evt_icon
	border = GFX_event_normal_frame_religion

	is_triggered_only = yes # on_character_convert_religion

	religion = chalcedonian

	trigger = {
		NOR = {
			has_global_flag = great_schism_prevented
			has_global_flag = great_schism_disabled
			has_global_flag = setting_up_pre_schism_setup
		}
	}

	option = {
		name = PlusGreatSchism_041_1

		add_trait = latin_rites

		# Change from open elective to investiture succession
		any_demesne_title = {
			limit = { is_law_potential = succ_catholic_bishopric }
			add_law = succ_catholic_bishopric
		}

		ai_chance = {
			factor = 50

			trigger = {
				OR = {
					liege = { trait = latin_rites }
					top_liege = { trait = latin_rites }
					rightful_religious_head_scope = { trait = latin_rites }
					any_close_relative = { trait = latin_rites }
				}
			}

			mult_modifier = {
				factor = 2

				OR = {
					culture_group = latin
					culture_group = occitano_romance_group
					culture_group = iberian
					culture_group = italian_group

					top_liege = {
						OR = {
							has_landed_title = e_hre
							has_landed_title = e_karling
						}
					}
				}
			}

			mult_modifier = {
				factor = 5
				capital_scope = { region = custom_latin_rites }
			}

			mult_modifier = {
				factor = 10

				is_adult = no

				any_close_relative = {
					is_parent_of = ROOT
					trait = latin_rites
				}
			}
		}
	}

	option = {
		name = PlusGreatSchism_041_2

		add_trait = greek_rites

		ai_chance = {
			factor = 50

			trigger = {
				OR = {
					liege = { trait = greek_rites }
					top_liege = { trait = greek_rites }
					rightful_religious_head_scope = { trait = greek_rites }
					any_close_relative = { trait = greek_rites }
				}
			}

			mult_modifier = {
				factor = 2

				OR = {
					culture_group = byzantine

					top_liege = {
						OR = {
							has_landed_title = e_byzantium
							has_landed_title = e_roman_empire
						}
					}
				}
			}

			mult_modifier = {
				factor = 5
				capital_scope = { region = custom_greek_rites }
			}

			mult_modifier = {
				factor = 10

				is_adult = no

				any_close_relative = {
					is_parent_of = ROOT
					trait = greek_rites
				}
			}
		}
	}

	option = {
		name = PlusGreatSchism_041_3

		add_trait = iconoclast_rites

		ai_chance = {
			factor = 50

			trigger = {
				OR = {
					liege = { trait = iconoclast_rites }
					top_liege = { trait = iconoclast_rites }
					rightful_religious_head_scope = { trait = iconoclast_rites }
					any_close_relative = { trait = iconoclast_rites }
				}
			}

			mult_modifier = {
				factor = 2

				OR = {
					culture_group = byzantine

					top_liege = {
						OR = {
							has_landed_title = e_byzantium
							has_landed_title = e_roman_empire
						}
					}
				}
			}

			mult_modifier = {
				factor = 5
				capital_scope = { region = custom_greek_rites }
			}

			mult_modifier = {
				factor = 10

				is_adult = no

				any_close_relative = {
					is_parent_of = ROOT
					trait = iconoclast_rites
				}
			}
		}
	}
}

# 042: Patriarch demands that a priest switch rites
character_event = {
	id = PlusGreatSchism.042
	picture = GFX_evt_bishop
	border = GFX_event_normal_frame_religion

	desc = {
		text = PlusGreatSchism_042_latin
		trigger = { trait = greek_rites }
	}
	desc = {
		text = PlusGreatSchism_042_greek
		trigger = { trait = latin_rites }
	}

	only_rulers = yes
	religion = chalcedonian
	min_age = 16
	capable_only = yes
	prisoner = no

	trigger = {
		religion_authority >= 0.50
		is_theocracy = yes
		has_regent = no

		rightful_religious_head_scope = {
			has_regent = no

			NOR = {
				character = ROOT

				has_opinion_modifier = {
					name = refused_to_change_rite
					who = ROOT
				}
			}

			# Must be the religious head of ROOT, if ROOT has one
			trigger_if = {
				limit = { controls_religion = no }

				ROOT = {
					religion_head = {
						always = no
					}
				}
			}

			OR = {
				opinion = { who = ROOT value < 0 }
				is_liege_or_above_of = ROOT
			}

			# Must adhere to different rites
			trigger_if = {
				limit = { trait = latin_rites }
				ROOT = { trait = greek_rites }
			}
			trigger_else_if = {
				limit = { trait = greek_rites }
				ROOT = { trait = latin_rites }
			}
		}
	}

	mean_time_to_happen = {
		years = 10

		mult_modifier = {
			factor = 0.5
			rightful_religious_head_scope = { is_liege_or_above_of = ROOT }
		}

		mult_modifier = {
			factor = 0.8

			rightful_religious_head_scope = {
				opinion = {
					who = ROOT
					value < -20
				}
			}
		}

		mult_modifier = {
			factor = 0.8

			rightful_religious_head_scope = {
				opinion = {
					who = ROOT
					value < -40
				}
			}
		}

		mult_modifier = {
			factor = 0.8

			rightful_religious_head_scope = {
				opinion = {
					who = ROOT
					value < -60
				}
			}
		}

		mult_modifier = {
			factor = 0.8

			rightful_religious_head_scope = {
				opinion = {
					who = ROOT
					value < -80
				}
			}
		}
	}

	immediate = {
		rightful_religious_head_scope = { save_event_target_as = rightful_relhead }

		if = {
			limit = {
				religion_head = {
					always = no
				}
			}

			religion_authority = {
				name = greek-latin_conflict
			}
		}
	}

	option = {
		name = PlusGreatSchism_042_1 # Refuse

		piety = -25

		rightful_religious_head_scope = {
			show_scope_change = no

			opinion = {
				name = refused_to_change_rite
				who = ROOT
				years = 10
			}

			prestige = -25
		}

		ai_chance = {
			factor = 30

			mult_modifier = {
				factor = 0.5

				rightful_religious_head_scope = {
					is_liege_or_above_of = ROOT
					NOT = { is_liege_of = ROOT }
				}
			}

			mult_modifier = {
				factor = 0.1
				rightful_religious_head_scope = { is_liege_of = ROOT }
			}

			mult_modifier = {
				factor = 1.25

				rightful_religious_head_scope = {
					reverse_opinion = {
						who = ROOT
						value >= 0
					}
				}
			}

			mult_modifier = {
				factor = 1.25

				rightful_religious_head_scope = {
					reverse_opinion = {
						who = ROOT
						value >= 20
					}
				}
			}

			mult_modifier = {
				factor = 1.5

				rightful_religious_head_scope = {
					reverse_opinion = {
						who = ROOT
						value >= 40
					}
				}
			}

			mult_modifier = {
				factor = 1.5

				rightful_religious_head_scope = {
					reverse_opinion = {
						who = ROOT
						value >= 60
					}
				}
			}

			mult_modifier = {
				factor = 2

				rightful_religious_head_scope = {
					reverse_opinion = {
						who = ROOT
						value >= 80
					}
				}
			}
		}
	}

	option = {
		name = PlusGreatSchism_042_2 # Agree

		if = {
			limit = {
				rightful_religious_head_scope = {
					trait = greek_rites
				}
			}

			add_trait = greek_rites

			# Change from investiture succession to open elective
			any_demesne_title = {
				limit = { is_law_potential = succ_open_elective }
				add_law = succ_open_elective
			}

			rightful_religious_head_scope = {
				show_scope_change = no

				any_independent_ruler = {
					show_scope_change = no

					if = {
						limit = { trait = latin_rites }

						opinion = {
							name = opinion_disappointed
							who = PREV
							years = 10
						}
					}

					any_realm_lord = {
						show_scope_change = no

						limit = { trait = latin_rites }

						opinion = {
							name = opinion_disappointed
							who = PREVPREV
							years = 10
						}
					}
				}
			}
		}
		else_if = {
			limit = {
				rightful_religious_head_scope = {
					trait = latin_rites
				}
			}

			add_trait = latin_rites

			# Change from open elective to investiture succession
			any_demesne_title = {
				limit = { is_law_potential = succ_catholic_bishopric }
				add_law = succ_catholic_bishopric
			}

			rightful_religious_head_scope = {
				show_scope_change = no

				any_independent_ruler = {
					show_scope_change = no

					if = {
						limit = { trait = greek_rites }

						opinion = {
							name = opinion_disappointed
							who = PREV
							years = 10
						}
					}

					any_realm_lord = {
						show_scope_change = no

						limit = { trait = greek_rites }

						opinion = {
							name = opinion_disappointed
							who = PREVPREV
							years = 10
						}
					}
				}
			}
		}

		ai_chance = {
			factor = 70

			mult_modifier = {
				factor = 1.25

				rightful_religious_head_scope = {
					reverse_opinion = {
						who = ROOT
						value < 0
					}
				}
			}

			mult_modifier = {
				factor = 1.25

				rightful_religious_head_scope = {
					reverse_opinion = {
						who = ROOT
						value < -20
					}
				}
			}

			mult_modifier = {
				factor = 1.5

				rightful_religious_head_scope = {
					reverse_opinion = {
						who = ROOT
						value < -40
					}
				}
			}

			mult_modifier = {
				factor = 1.5

				rightful_religious_head_scope = {
					reverse_opinion = {
						who = ROOT
						value < -60
					}
				}
			}

			mult_modifier = {
				factor = 2

				rightful_religious_head_scope = {
					reverse_opinion = {
						who = ROOT
						value < -80
					}
				}
			}
		}
	}
}

# Give newlyborns rites
character_event = {
	id = PlusGreatSchism.043

	is_triggered_only = yes # on_birth
	hide_window = yes

	religion = chalcedonian

	trigger = {
		any_close_relative = {
			is_parent_of = ROOT
			religion = chalcedonian
		}

		NOR = {
			has_global_flag = great_schism_prevented
			has_global_flag = great_schism_disabled
		}
	}

	immediate = {
		any_close_relative = {
			limit = {
				is_parent_of = ROOT
				religion = chalcedonian
			}

			score_value = {
				value = 1

				additive_modifier = {
					value = 2000
					ai = no
				}

				additive_modifier = {
					value = 1000

					trigger_if = {
						limit = { is_ruler = yes }
						is_liege_of = ROOT
					}
					trigger_else = {
						same_liege = ROOT
					}
				}

				additive_modifier = {
					value = 500
					real_tier = EMPEROR
				}

				additive_modifier = {
					value = 250
					real_tier = KING
				}

				additive_modifier = {
					value = 125
					real_tier = DUKE
				}

				additive_modifier = {
					value = 50
					real_tier = COUNT
				}

				additive_modifier = {
					value = 25
					real_tier = BARON
				}
			}

			trigger_switch = {
				on_trigger = trait

				latin_rites = { ROOT = { add_trait = latin_rites } }
				greek_rites = { ROOT = { add_trait = greek_rites } }
				iconoclast_rites = { ROOT = { add_trait = iconoclast_rites } }
			}
		}
	}
}

# Give generated characters rites
character_event = {
	id = PlusGreatSchism.044

	is_triggered_only = yes # on_host_change
	hide_window = yes

	religion = chalcedonian

	trigger = {
		trigger_if = {
			limit = { FROM = { character = yes } } # There is no previous host, so this character was generated, by any means
			FROM = { has_character_flag = GR30 } # or this was fired in GR.30, due to hardcoded issues
		}

		NOR = {
			trait = latin_rites
			trait = greek_rites
			trait = iconoclast_rites
			has_global_flag = great_schism_prevented
			has_global_flag = great_schism_disabled
		}
	}

	immediate = {
		liege = {
			trigger_switch = {
				on_trigger = trait

				latin_rites = { ROOT = { add_trait = latin_rites } }
				greek_rites = { ROOT = { add_trait = greek_rites } }
				iconoclast_rites = { ROOT = { add_trait = iconoclast_rites } }
			}
		}

		# Change from open elective to investiture succession
		any_demesne_title = {
			limit = { is_law_potential = succ_catholic_bishopric }
			add_law = succ_catholic_bishopric
		}
	}
}

character_event = {
	id = PlusGreatSchism.045

	is_triggered_only = yes # on_new_holder, for newly generated rulers for barony holdings
	hide_window = yes

	religion = chalcedonian

	trigger = {
		NOR = {
			trait = latin_rites
			trait = greek_rites
			trait = iconoclast_rites
			has_global_flag = great_schism_prevented
			has_global_flag = great_schism_disabled
		}
	}

	immediate = {
		liege = {
			trigger_switch = {
				on_trigger = trait

				latin_rites = { ROOT = { add_trait = latin_rites } }
				greek_rites = { ROOT = { add_trait = greek_rites } }
				iconoclast_rites = { ROOT = { add_trait = iconoclast_rites } }
			}
		}

		# Change from open elective to investiture succession
		any_demesne_title = {
			limit = { is_law_potential = succ_catholic_bishopric }
			add_law = succ_catholic_bishopric
		}
	}
}

character_event = {
	id = PlusGreatSchism.046

	is_triggered_only = yes # on_new_holder_inheritance, for generated characters who inherit titles with open elective
	hide_window = yes

	religion = chalcedonian

	trigger = {
		NOR = {
			trait = latin_rites
			trait = greek_rites
			trait = iconoclast_rites
			has_global_flag = great_schism_prevented
			has_global_flag = great_schism_disabled
		}
	}

	immediate = {
		FROMFROM = { # Old holder
			trigger_switch = {
				on_trigger = trait

				latin_rites = { ROOT = { add_trait = latin_rites } }
				greek_rites = { ROOT = { add_trait = greek_rites } }
				iconoclast_rites = { ROOT = { add_trait = iconoclast_rites } }
			}
		}

		# Change from open elective to investiture succession
		any_demesne_title = {
			limit = { is_law_potential = succ_catholic_bishopric }
			add_law = succ_catholic_bishopric
		}
	}
}

#!!

#######################################
#
#!	051 - 060: The Pentarchy
#
#######################################
#
#	INDEX
#
#	051: Assign pentarch to titles
#	052: Update pentarch on new holder
#	053: Update pentarch on religion change
#	054: Update pentarch on titles of new vassal's realm
#
#######################################

# 051: Assign pentarch to titles
# Used for both Chalcedonian and vanilla religions that use pentarchy in any way
# Fired, directly or indirectly, during Plus start-up event
character_event = {
	id = PlusGreatSchism.051

	hide_window = yes
	is_triggered_only = yes # 002 above, and PlusMaintenance.101, for post-1054 starts with Schism in place
	# TODO - trigger from maintenance event

	has_dlc = "Legacy of Rome"

	trigger = {
		always = no # Disabled until the system is entirely functional
	}

	immediate = {
		# Erase everything beforehand. Some later events interfere when converting characters in post-1054 starts
		any_title = {
			set_pentarch_title = none
		}

		# Assign pentarchy allegiance
		any_title = {
			limit = {
				has_holder = yes

				trigger_if = { # Counties are used for the map display in the Religion tab
					limit = { tier = COUNT }

					OR = {
						location = { # Christian lands
							has_pentarchy = yes
						}
						holder_scope = { # or under a Christian (top) liege (Sweden in 1066, for example)
							OR = {
								has_pentarchy = yes

								any_liege = {
									has_pentarchy = yes
								}
							}
						}
					}
				}
				trigger_else = { # Otherwise titles are used to determine religious head, so only the holder is relevant
					holder_scope = {
						has_pentarchy = yes
					}
				}
			}

			# Assign titles to Rome
			if = {
				limit = {
					NOT = { has_global_flag = great_schism_happened } # for post-1054 starts with Schism in place

					OR = {
						# Avoid assigning lands held by Greeks and 'Russian' slavs rulers inside the Latin sphere
						# Do assign lands held by core 'Catholic' cultures outside the Latin sphere (for late starts, if Schism is reverted)
						AND = {
							trigger_if = {
								limit = {
									OR = {
										AND = {
											lower_tier_than = DUKE
											location = {
												NOT = {
													region = custom_latin_rites
												}
											}
										}
										AND = {
											higher_tier_than = COUNT

											holder_scope = {
												has_pentarchy = yes

												any_demesne_title = {
													tier = COUNT

													location = {
														NOT = {
															region = custom_latin_rites
														}
													}
												}
											}
										}
									}
								}

								holder_scope = {
									OR = {
										culture_group = latin
										culture_group = italian_group
									}
								}
							}

							holder_scope = {
								NOR = {
									culture_group = byzantine
									culture_group = east_slavic
								}
							}
						}

						# Non-iconoclast, non-Byzantines under iconoclast Byzantine emperor
						location = {
							has_pentarchy = yes
							NOT = { religion = iconoclast }

							owner = {
								NOR = {
									religion = iconoclast
									culture_group = byzantine
								}
								top_liege = {
									has_landed_title = e_byzantium
									religion = iconoclast
								}
							}
						}
					}

					# Religious heads don't report to the Pope
					trigger_if = {
						limit = { controls_religion = yes }
						title = k_papacy
					}

					# Special check for Mercenary leaders, all others default to Constantinople
					trigger_if = {
						limit = { mercenary = yes }

						capital_scope = {
							region = custom_latin_rites
						}
					}
				}
				log = "[This.GetID]: Pentarch of Rome was assigned the [This.GetFullBaseName]"
				set_pentarch_title = b_roma
			}

			# Assign pentarchs, if they exist
			else_if = {
				limit = {
					# Check if a patriarch exists
					b_antioch = {
						holder_scope = {
							has_pentarchy = yes
							is_theocracy = yes
						}
					}

					OR = {
						# Rightfully under authority of Antioch
						location = { # counties and holdings
							region = custom_pentarch_antioch
						}
						any_direct_de_jure_vassal_title = { # duchies and kingdoms
							OR = {
								location = { # counties, if title was a duchy
									region = custom_pentarch_antioch
								}
								any_direct_de_jure_vassal_title = { # duchies, if previous title was a kingdom
									location = {
										region = custom_pentarch_antioch
									}
								}
							}
						}

						# Belongs to another pentarch that does not exist and this pentarch is closest to it
						AND = {
							b_jerusalem = {
								holder_scope = {
									has_pentarchy = no
								}
							}

							OR = {
								location = { # counties and holdings
									region = custom_pentarch_jerusalem
								}
								any_direct_de_jure_vassal_title = { # duchies and kingdoms
									OR = {
										location = { # counties, if title was a duchy
											region = custom_pentarch_jerusalem
										}
										any_direct_de_jure_vassal_title = { # duchies, if previous title was a kingdom
											location = {
												region = custom_pentarch_jerusalem
											}
										}
									}
								}
							}
						}
						AND = {
							b_alexandria = {
								holder_scope = {
									has_pentarchy = no
								}
							}

							OR = {
								location = { # counties and holdings
									region = custom_pentarch_alexandria
								}
								any_direct_de_jure_vassal_title = { # duchies and kingdoms
									OR = {
										location = { # counties, if title was a duchy
											region = custom_pentarch_alexandria
										}
										any_direct_de_jure_vassal_title = { # duchies, if previous title was a kingdom
											location = {
												region = custom_pentarch_alexandria
											}
										}
									}
								}
							}
						}
					}
				}
				log = "[This.GetID]: Pentarch of Antioch was assigned the [This.GetFullBaseName]"
				set_pentarch_title = b_antioch
			}
			else_if = {
				limit = {
					# Check if a patriarch exists
					b_jerusalem = {
						holder_scope = {
							has_pentarchy = yes
							is_theocracy = yes
						}
					}

					OR = {
						location = { # counties and holdings
							region = custom_pentarch_jerusalem
						}
						any_direct_de_jure_vassal_title = { # duchies
							location = { # counties
								region = custom_pentarch_jerusalem
							}
						}

						# Belongs to another pentarch that does not exist and this pentarch is closest to it
						AND = {
							b_antioch = {
								holder_scope = {
									has_pentarchy = no
								}
							}

							OR = {
								location = { # counties and holdings
									region = custom_pentarch_antioch
								}
								any_direct_de_jure_vassal_title = { # duchies and kingdoms
									OR = {
										location = { # counties, if title was a duchy
											region = custom_pentarch_antioch
										}
										any_direct_de_jure_vassal_title = { # duchies, if previous title was a kingdom
											location = {
												region = custom_pentarch_antioch
											}
										}
									}
								}
							}
						}
						AND = {
							b_alexandria = {
								holder_scope = {
									has_pentarchy = no
								}
							}

							OR = {
								location = { # counties and holdings
									region = custom_pentarch_alexandria
								}
								any_direct_de_jure_vassal_title = { # duchies and kingdoms
									OR = {
										location = { # counties, if title was a duchy
											region = custom_pentarch_alexandria
										}
										any_direct_de_jure_vassal_title = { # duchies, if previous title was a kingdom
											location = {
												region = custom_pentarch_alexandria
											}
										}
									}
								}
							}
						}
					}
				}
				log = "[This.GetID]: Pentarch of Jerusalem was assigned the [This.GetFullBaseName]"
				set_pentarch_title = b_jerusalem
			}
			else_if = {
				limit = {
					# Check if a patriarch exists
					b_alexandria = {
						holder_scope = {
							has_pentarchy = yes
							is_theocracy = yes
						}
					}

					OR = {
						location = { # counties and holdings
							region = custom_pentarch_alexandria
						}
						any_direct_de_jure_vassal_title = { # duchies
							location = { # counties
								region = custom_pentarch_alexandria
							}
						}

						# Belongs to another pentarch that does not exist and this pentarch is closest to it
						AND = {
							b_jerusalem = {
								holder_scope = {
									has_pentarchy = no
								}
							}

							OR = {
								location = { # counties and holdings
									region = custom_pentarch_jerusalem
								}
								any_direct_de_jure_vassal_title = { # duchies and kingdoms
									OR = {
										location = { # counties, if title was a duchy
											region = custom_pentarch_jerusalem
										}
										any_direct_de_jure_vassal_title = { # duchies, if previous title was a kingdom
											location = {
												region = custom_pentarch_jerusalem
											}
										}
									}
								}
							}
						}
						AND = {
							b_antioch = {
								holder_scope = {
									has_pentarchy = no
								}
							}

							OR = {
								location = { # counties and holdings
									region = custom_pentarch_antioch
								}
								any_direct_de_jure_vassal_title = { # duchies and kingdoms
									OR = {
										location = { # counties, if title was a duchy
											region = custom_pentarch_antioch
										}
										any_direct_de_jure_vassal_title = { # duchies, if previous title was a kingdom
											location = {
												region = custom_pentarch_antioch
											}
										}
									}
								}
							}
						}
					}
				}
				log = "[This.GetID]: Pentarch of Alexandria was assigned the [This.GetFullBaseName]"
				set_pentarch_title = b_alexandria
			}

			# Everything else is assigned to Constantinople
			else_if = {
				limit = {
					trigger_if = {
						limit = {
							NOT = { has_global_flag = great_schism_happened }
						}
						holder_scope = { # Crusader states, etc.
							NOR = {
								culture_group = italian_group
								culture_group = latin
							}
						}
					}

					OR = {
						# Not under a kingdom title outside the Latin sphere, which has an autocephalous patriarch
						NOR = {
							trigger_if = {
								limit = { higher_tier_than = DUKE }
								NOR = { # exceptions
									title = e_byzantium
									title = k_orthodox
								}
							}
						}

						# Greeks outside the Greek sphere
						trigger_if = {
							limit = {
								NOT = { region = custom_greek_rites }
								lower_tier_than = DUKE
							}
							OR = {
								location = {
									culture_group = byzantine
								}
								holder_scope = {
									culture_group = byzantine
								}
							}
						}
						trigger_else_if = {
							limit = {
								NOT = { region = custom_greek_rites }
								higher_tier_than = COUNT
							}
							holder_scope = {
								culture_group = byzantine
							}
						}
					}

					controls_religion = no
				}
				log = "[This.GetID]: Pentarch of Constantinople was assigned the [This.GetFullBaseName]"
				set_pentarch_title = b_hagiasophia
			}
		}

		# Reset pentarchy in realms that should have an autocephalous patriarch instead
		any_title = {
			if = {
				limit = {
					higher_tier_than = DUKE
					has_holder = yes

					NOR = { # exceptions
						title = e_byzantium
						title = k_orthodox
					}

					holder_scope = {
						has_autocephaly = yes

						NOR = {
							culture_group = latin
							culture_group = italian_group
							culture_group = magyar
							has_landed_title = k_orthodox
							has_landed_title = e_byzantium
						}

						any_realm_title = {
							tier = COUNT

							location = {
								region = custom_greek_rites
							}
						}
					}
				}

				# Set a title flag for easy checks later and in other events
				set_flag = has_autocephalous_patriarch

				holder_scope = {
					any_title_under = {
						limit = {
							NOR = {
								# Don't reset titles outside de jure kingdom, and still de jure part of Byzantine Empire,
								# should keep Ecumenical Patriarch as religious head (see Bulgaria in 867, Bulgaria and Serbia 936)
								trigger_if = {
									limit = {
										PREVPREV = {
											tier = KING
										}
									}

									kingdom = {
										NOT = { title = PREVPREVPREV } # Outside the de jure kingdom

										OR = {
											holder_scope = {
												has_pentarchy = yes
											}
											empire = {
												NOT = { title = PREVPREVPREVPREV } # Outside the de jure empire

												holder_scope = {
													has_pentarchy = yes
												}
											}
										}
									}
								}
								trigger_else = {
									empire = {
										NOT = { title = PREVPREVPREV } # Outside the de jure empire

										holder_scope = {
											has_pentarchy = yes
										}
									}
								}
							}
						}
						set_pentarch_title = none
						log = "[This.GetID]: Pentarchy was reset on the [This.GetFullBaseName], under an autocephalous patriarch"
					}
				}
			}
		}

		# Appoint court chaplains, if necesssary, to ensure presence of autocephalous patriarchs since religion change fires them
		any_title = {
			limit = { has_title_flag = has_autocephalous_patriarch }

			holder_scope = {
				if = {
					limit = {
						NOT = {
							job_spiritual = {
								always = yes
							}
						}
					}

					random_courtier_or_vassal = {
						limit = {
							can_hold_title = job_spiritual
						}

						preferred_limit = {
							historical = yes
							NOT = { is_close_relative = PREV }
						}
						preferred_limit = {
							NOT = { is_close_relative = PREV }
						}

						give_job_title = job_spiritual
					}
				}
			}
		}

		# Set pentarch flags
		any_title = {
			limit = {
				OR = {
					title = b_roma
					title = b_hagiasophia
					title = b_antioch
					title = b_jerusalem
					title = b_alexandria
				}

				holder_scope = {
					has_pentarchy = yes
					is_theocracy = yes
					NOT = { has_landed_title = k_papacy }
				}
			}

			holder_scope = {
				set_flag = is_pentarch
			}
		}
	}
}

# 052: Update pentarch on new holder
# Fires on_new_holder
#		on_new_holder_inheritance
#		on_new_holder_usurpation
# ROOT is the character
# FROM is the title
# FROMFROM is the old holder
character_event = {
	id = PlusGreatSchism.052

	hide_window = yes
	is_triggered_only = yes

	has_dlc = "Legacy of Rome"

	trigger = {
		always = no # Disabled until the system is entirely functional
	}

	immediate = {
		log = "052: on_new_holder - [From.GetID] - [This.GetTitledNameWithNick] ([This.GetID])"

		if = {
			limit = { has_pentarchy = yes }

			log = "[This.GetTitledNameWithNick] has a pentarchy religion"

			# 1 - Pass on pentarch flag
			if = {
				limit = {
					# Title
					FROM = {
						OR = {
							title = b_hagiasophia
							title = b_antioch
							title = b_jerusalem
							title = b_alexandria
							title = b_roma

							# Testing has shown this to be necessary
							AND = {
								title = c_constantinopolis

								b_hagiasophia = {
									is_capital = yes
								}
							}

							AND = {
								title = c_antioch

								b_antioch = {
									is_capital = yes
								}
							}

							AND = {
								title = c_jerusalem

								b_jerusalem = {
									is_capital = yes
								}
							}

							AND = {
								title = c_alexandria

								b_alexandria = {
									is_capital = yes
								}
							}

							AND = {
								title = c_roma

								b_roma = {
									is_capital = yes
								}
							}
						}
					}

					# New title holder
					is_theocracy = yes
					NOR = { # Non-Papal patriarch of Rome
						has_landed_title = k_papacy
						FROMFROM = { # b_roma/c_roma can be inherited before k_papacy
							has_pentarchy = yes
							has_landed_title = k_papacy
						}
					}

					OR = {
						religion = chalcedonian
						religion = orthodox
						religion = monothelite
						religion = iconoclast
					#	religion = miaphysite
					#	religion = monophysite
					#	religion = nestorian
					#	religion = messalian
					#	religion = apostolic
						religion = paulician
					}
				}

				set_flag = is_pentarch
				log = "[FromFrom.GetTitledNameWithNick] was succeeded as Patriarch of [From.GetFullBaseName] by [Root.GetTitledNameWithNick]"
			}

			# 2 - Handle kingdom/empire titles in realms with an autocephalous patriarch
			if = {
				limit = {
					FROM = {
						higher_tier_than = DUKE
					}
					NOT = {
						rightful_religious_head_scope = {
							has_landed_title = k_papacy
						}
					}
				}

				log = "[From.GetID] is higher than Duke tier"

				# New or existing autocephalous realm receives new kingdom/empire title
				if = {
					limit = {
						OR = {
							NOR = { # New autocephalous realm
								any_demesne_title = { # Doesn't already have a kingdom or empire tier title
									NOT = { title = FROM }
									higher_tier_than = DUKE
								}
								trigger_if = { # Not under an empire without autocephalous patriarchs
									limit = { independent = no }

									liege = {
										NOT = {
											any_demesne_title = {
												has_title_flag = has_autocephalous_patriarch
											}
										}
									}
								}
								rightful_religious_head_scope = { # Not a lackey of the Pope
									has_landed_title = k_papacy
								}
							}
							any_demesne_title = { # Existing autocephalous realm
								NOT = { title = FROM }
								higher_tier_than = DUKE
								has_title_flag = has_autocephalous_patriarch
							}
						}
					}

					log = "[From.GetID] is a new or existing realm with an autocephalous patriarch"

					FROM = {
						set_flag = has_autocephalous_patriarch
						set_pentarch_title = none
						log = "[This.GetID]: Pentarch reset on the [This.GetFullBaseName], under autocephalous patriarch"
					}

					save_event_target_as = event_liege

				#	rightful_religious_head_scope = {
						save_event_target_as = event_liege_religious_head
				#	}

					any_title_under = {
						if = {
							limit = { lower_tier_than = DUKE }
							location = {
								assign_pentarch_CK2Plus_effect = yes
							}
						}
						else = {
							capital_scope = {
								assign_pentarch_CK2Plus_effect = yes
							}
						}
					}

					clear_event_target = event_liege
					clear_event_target = event_liege_religious_head
				}

				# Ruler with autocephalous patriarch loses last kingdom/empire title, should lose autocephalous patriarch
				else_if = {
					limit = {
						FROM = {
							has_title_flag = has_autocephalous_patriarch
						}
						FROMFROM = {
							is_ruler = yes
							has_pentarchy = yes

							NOR = {
								any_demesne_title = { # Doesn't have any other qualifying titles
									NOT = { title = ROOT_FROM }
									higher_tier_than = DUKE
									has_title_flag = has_autocephalous_patriarch
								}
								any_liege = { # Not under a liege that provides an autocephalous patriarch
									any_demesne_title = {
										has_title_flag = has_autocephalous_patriarch
									}
								}
							}
						}
					}

					log = "[From.GetID] can no longer have an autocephalous patriarch"

					FROMFROM = { # Assign regular pentarchs
						k_orthodox = {
							holder_scope = {
								save_event_target_as = event_liege
								save_event_target_as = event_liege_religious_head
							}
						}
						any_title_under = {
							if = {
								limit = { lower_tier_than = DUKE }

								location = {
									assign_pentarch_CK2Plus_effect = yes
								}
							}
							else = {
								capital_scope = {
									assign_pentarch_CK2Plus_effect = yes
								}
							}
						}

						clear_event_target = event_liege
						clear_event_target = event_liege_religious_head
					}
				}
			}
			# 3 - Update pentarch allegiance
			else_if = {
				limit = {
					NOR = { # Not part of a realm with an autocephalous patriarch
						any_demesne_title = {
							higher_tier_than = DUKE
							has_title_flag = has_autocephalous_patriarch
						}
						any_liege = {
							any_demesne_title = {
								higher_tier_than = DUKE
								has_title_flag = has_autocephalous_patriarch
							}
						}
					}
				}

				log = "[From.GetID]'s pentarch allegiance is updated and not under an autocephalous patriarch"

				save_event_target_as = event_liege

				rightful_religious_head_scope = {
					save_event_target_as = event_liege_religious_head
				}

				FROM = {
					if = {
						limit = { lower_tier_than = DUKE }

						location = {
							assign_pentarch_CK2Plus_effect = yes
						}
					}
					else = {
						capital_scope = {
							assign_pentarch_CK2Plus_effect = yes
						}
					}
				}

				clear_event_target = event_liege
				clear_event_target = event_liege_religious_head
			}
		}

		# 4 - Undo autocephalous patriarch if inherited by non-Pentarchy ruler
		if = {
			limit = {
				has_pentarchy = no
				FROM = {
					has_title_flag = has_autocephalous_patriarch
				}
				NOT = {
					any_liege = {
						any_demesne_title = {
							has_title_flag = has_autocephalous_patriarch
						}
					}
				}
			}

			# Revert all titles underneath to pentarch authority, unless under Papal authority
			FROM = {
				clr_flag = has_autocephalous_patriarch
				log = "[This.GetID] no longer has an autocephalous patriarch, due to the religion of [Root.GetTitledNameWithNick] ([Root.GetID])"
			}

			k_orthodox = {
				holder_scope = {
					save_event_target_as = event_liege
					save_event_target_as = event_liege_religious_head
				}
			}

			any_title_under = {
				limit = {
					trigger_if = {
						limit = { tier = COUNT }

						OR = {
							location = { # Christian lands
								has_pentarchy = yes
							}
							holder_scope = { # Held under any Christian liege
								OR = {
									has_pentarchy = yes

									any_liege = {
										has_pentarchy = yes
									}
								}
							}
						}
					}
					trigger_else = {
						holder_scope = {
							has_pentarchy = yes
						}
					}

				#	NOT = {
				#		holder_scope = {
				#			rightful_religious_head_scope = {
				#				has_landed_title = k_papacy
				#			}
				#		}
				#	}
				}

				log = "any_title_under - [This.GetID]"

				if = {
					limit = { tier = COUNT }

					location = {
						if = {
							limit = { region = custom_pentarch_rome }

							PREV = { # Back to title
								set_pentarch_title = b_roma
								log = "[This.GetID]: Pentarch of Rome was assigned the [This.GetFullBaseName]"
							}
						}
						else = {
							assign_pentarch_CK2Plus_effect = yes
						}
					}
				}
				else = {
					if = {
						limit = { lower_tier_than = DUKE }

						location = {
							assign_pentarch_CK2Plus_effect = yes
						}
					}
					else = {
						capital_scope = {
							assign_pentarch_CK2Plus_effect = yes
						}
					}
				}
			}

			clear_event_target = event_liege
			clear_event_target = event_liege_religious_head
		}

		# Assign pentarchy to any titles gained by non-pentarchy rulers under pentarchy rulers without autocephalous patriarchs, such as war conquests
		if = {
			limit = {
				has_pentarchy = no
				FROM = {
					tier = COUNT
				}
				any_liege = {
					has_pentarchy = yes
				}
				NOT = {
					any_liege = {
						any_demesne_title = {
							has_title_flag = has_autocephalous_patriarch
						}
					}
				}
			}

			log = "[From.GetID] is assigned a pentarch because it is held under a liege with pentarchy"

			any_liege = {
				limit = {
					has_pentarchy = yes
				}

				save_event_target_as = event_liege

				rightful_religious_head_scope = {
					save_event_target_as = event_liege_religious_head
				}
			}

			FROM = {
				location = {
					assign_pentarch_CK2Plus_effect = yes
				}
			}

			clear_event_target = event_liege
			clear_event_target = event_liege_religious_head
		}
	}
}

# 053: # Update pentarch on character religion change
# Fires on_character_convert_religion
# ROOT is the character after conversion
# FROM is old religion
character_event = {
	id = PlusGreatSchism.053

	hide_window = yes
	is_triggered_only = yes

	has_dlc = "Legacy of Rome"
	only_rulers = yes

	trigger = {
		always = no # Disabled until the system is entirely functional

		# Changed from a religion without pentarchy to a religion with pentarchy
		trigger_if = {
			limit = { has_pentarchy = yes }

			# Old religion
			FROM = {
				has_pentarchy = no
			}
		}
		# Changed from a religion with pentarchy to a religion without pentarchy
		trigger_else = {
			# Old religion
			FROM = {
				has_pentarchy = yes
			}
		}
	}

	immediate = {
		log = "053: Character converts towards or away from pentarchy - [This.GetTitledNameWithNick] ([This.GetID])"
		# Cases to consider (https://ck2.paradoxwikis.com/Religious_conversion)

		# - Converted due to losing a holy war
		# - Converted due to liege demands
		# - Converted due to court chaplain proselytize job
		# - Converted due to requesting mass conversion (unreformed pagans)

		# - Converted voluntarily (includes various events/decisions)
		# - Converted due to friend
		# - Converted due to fellow clan
		# - Converted due to childhood education

		if = {
			limit = { has_pentarchy = yes }

			if = {
				limit = { # Pentarch allegiance decided by circumstance
					OR = {
						has_character_flag = liege_has_autocephalous_patriarch
						has_character_flag = liege_follows_pope
						has_character_flag = liege_follows_eastern_pentarchs
						has_character_flag = convert_to_attacker_religion
					}
				}

				log = "Pentarch allegiance decided by circumstance - [This.GetTitledNameWithNick] ([This.GetID])"

				if = {
					limit = { # May have been set by 'convert to attacker religion'-decision
						persistent_event_target:external_converter = {
							always = yes
						}
					}

					persistent_event_target:external_converter = {
						save_event_target_as = event_liege

						rightful_religious_head_scope = {
							save_event_target_as = event_liege_religious_head
						}
					}

					# Clean-up
					clear_persistent_event_target = external_converter
				}
				else = {
					liege = {
						save_event_target_as = event_liege

						rightful_religious_head_scope = {
							save_event_target_as = event_liege_religious_head
						}
					}
				}

				any_demesne_title = {
					if = {
						limit = {
							higher_tier_than = DUKE

							PREV = { # Converted ruler
								OR = { # Restrictions to using autocephalous patriarchs
									independent = yes

									liege = {
										any_demesne_title = {
											has_title_flag = has_autocephalous_patriarch
										}
									}
								}
							}
						}
						set_flag = has_autocephalous_patriarch
						log = "Set autocephalous patriarch flag - [Prev.GetTitledNameWithNick] ([Prev.GetID])"
					}

					if = {
						limit = {
							lower_tier_than = DUKE
						}
						location = {
							assign_pentarch_CK2Plus_effect = yes
						}
					}
					else = {
						capital_scope = {
							assign_pentarch_CK2Plus_effect = yes
						}
					}
				}
				any_realm_province = {
					limit = {
						held_under_PREV = yes

						owner = { # Don't mess with vassals' pentarchy assignments
							has_pentarchy = no
						}
					}
					county = {
						location = {
							assign_pentarch_CK2Plus_effect = yes
						}
					}
				}
			}

			else = { # Free choice of pentarch allegiance

				log = "Free choice of pentarch allegiance - [This.GetTitledNameWithNick] ([This.GetID])"

				random_list = {
					10 = { # Choose Pope
						log = "[This.GetTitledNameWithNic] chose the Pope in Rome"
						save_event_target_as = event_liege

						k_papacy = {
							holder_scope = {
								save_event_target_as = event_liege_religious_head
							}
						}

						# Todo: something with distance from and/or opinion of Pope
						#		spouse/concubine/close family/friends (landed only?)

						modifier = { # No more Pope around
							factor = 0

							religion = chalcedonian
							has_global_flag = patriarch_supreme_is_greek
						}
						modifier = {
							factor = 10

							liege = {
								rightful_religious_head_scope = {
									has_landed_title = k_papacy
								}
							}
						}
						modifier = {
							factor = 3

							capital_scope = {
								region = custom_pentarch_rome
							}
						}
						modifier = {
							factor = 0

							NOR = {
								liege = {
									rightful_religious_head_scope = {
										has_landed_title = k_papacy
									}
								}
								liege = {
									any_vassal = {
										NOT = { character = ROOT }
										shares_realm_border_with = ROOT

										rightful_religious_head_scope = {
											has_landed_title = k_papacy
										}
									}
								}
								any_vassal = {
									rightful_religious_head_scope = {
										has_landed_title = k_papacy
									}
								}
								any_neighbor_independent_ruler = {
									rightful_religious_head_scope = {
										has_landed_title = k_papacy
									}
								}
								any_realm_province = {
									region = custom_pentarch_rome
									held_under_PREV = yes
								}
							}
						}
					}
					10 = { # Choose pentarchy
						log = "[This.GetTitledNameWithNick] chose the eastern pentarchs"
						set_flag = chose_pentarchy
						save_event_target_as = event_liege

						k_orthodox = {
							holder_scope = {
								save_event_target_as = event_liege_religious_head
							}
						}

						# Todo: something with distance from and/or opinion of any pentarch/Ecumenical Patriarch
						#		spouse/concubine/close family/friends (landed only?)

						modifier = { # The Pentarchy is no more
							factor = 0

							religion = chalcedonian
							has_global_flag = patriarch_supreme_is_latin
						}
						modifier = {
							factor = 10

							liege = {
								rightful_religious_head_scope = {
									has_character_flag = is_pentarch
								}
							}
						}
						modifier = {
							factor = 3

							capital_scope = {
								NOT = { region = custom_pentarch_rome }
							}
						}
						modifier = {
							factor = 0

							NOR = {
								liege = {
									rightful_religious_head_scope = {
										has_character_flag = is_pentarch
									}
								}
								liege = {
									any_vassal = {
										NOT = { character = ROOT }
										shares_realm_border_with = ROOT

										rightful_religious_head_scope = {
											has_character_flag = is_pentarch
										}
									}
								}
								any_vassal = {
									rightful_religious_head_scope = {
										has_character_flag = is_pentarch
									}
								}
								any_neighbor_independent_ruler = {
									rightful_religious_head_scope = {
										has_character_flag = is_pentarch
									}
								}
								any_realm_province = {
									NOT = { region = custom_pentarch_rome }
									held_under_PREV = yes
								}
							}
						}
					}
				}

				if = {
					limit = { has_flag = chose_pentarchy }

					clr_flag = chose_pentarchy

					any_demesne_title = {
						limit = {
							higher_tier_than = DUKE

							PREV = { # Converted ruler
								OR = { # Restrictions on using autocephalous patriarchs
									independent = yes

									liege = {
										any_demesne_title = {
											has_title_flag = has_autocephalous_patriarch
										}
									}
								}
							}
						}
						set_flag = has_autocephalous_patriarch
					}

					any_title_under = {
						limit = {
							holder_scope = { # Don't reassign titles subservient to Papal authority
								NOR = {
									rightful_religious_head_scope = {
										has_landed_title = k_papacy
									}
									any_liege = {
										is_vassal_or_below_of = ROOT

										rightful_religious_head_scope = {
											has_landed_title = k_papacy
										}
									}
								}
							}
						}

						if = {
							limit = {
								lower_tier_than = DUKE
							}
							location = {
								assign_pentarch_CK2Plus_effect = yes
							}
						}
						else = {
							capital_scope = {
								assign_pentarch_CK2Plus_effect = yes
							}
						}
					}
				}
				else = {
					any_demesne_title = {
						if = {
							limit = {
								lower_tier_than = DUKE
							}
							location = {
								assign_pentarch_CK2Plus_effect = yes
							}
						}
						else = {
							capital_scope = {
								assign_pentarch_CK2Plus_effect = yes
							}
						}
					}
					any_realm_province = {
						limit = {
							held_under_PREV = yes

							owner = { # Don't mess with vassals' pentarchy assignments
								has_pentarchy = no
							}
						}
						county = {
							location = {
								assign_pentarch_CK2Plus_effect = yes
							}
						}
					}
				}
			}

			# Clean-up
			clear_event_target = event_liege
			clear_event_target = event_liege_religious_head

			clr_flag = liege_has_autocephalous_patriarch
			clr_flag = liege_follows_pope
			clr_flag = liege_follows_eastern_pentarchs
			clr_flag = convert_to_attacker_religion
		}

		else = { # Erase pentarch assignment, provided it's no longer needed
			any_title_under = {
				limit = {
					holder_scope = {
						has_pentarchy = no
					}

					trigger_if = {
						limit = { tier = COUNT }

						NOR = {
							location = { # Still needed for on-map display in religion tab
								has_pentarchy = yes
							}

							holder_scope = { # Still needed by someone higher up
								any_liege = {
									has_pentarchy = yes
								}
							}
						}
					}
				}
				log = "[This.GetID]: Pentarchy was reset on the [This.GetFullBaseName], under an autocephalous patriarch"
				set_pentarch_title = none
			}
		}
	}
}

# 054: # Update pentarch on character secret religion change
# Fires on_character_convert_secret_religion
# ROOT is the character after conversion
# FROM is old religion
character_event = {
	id = PlusGreatSchism.054

	hide_window = yes
	is_triggered_only = yes

	has_dlc = "Legacy of Rome"

	trigger = {
		always = no # Disabled until the system is entirely functional

		# Changed from a (secret) religion without pentarchy to a (secret) religion with pentarchy
		trigger_if = {
			limit = { has_pentarchy = yes }

			# Old (secret) religion
			FROM = {
				has_pentarchy = no
			}
		}
		# Changed from a (secret) religion with pentarchy to a (secret) religion without pentarchy
		trigger_else = {
			# Old (secret) religion
			FROM = {
				has_pentarchy = yes
			}
		}

		# Should not fire when the secret religion is reset due to converting to it publicly
		NOR = {
			religion = FROM
			NOT = {
				FROM = {
					always = yes # secret religion has been reset
				}
			}
		}
	}

	immediate = {
		log = "054: on_character_convert_religion"
	}
}

# 055: Update pentarch on titles of new vassal's realm
# Fires on
# - OFFER_VASSALIZATION_INTERACTION_ACCEPT_EVENT
# ROOT is empty (don't use it)
# FROM is the new vassal
# FROMFROM is the liege
# Starting scope is FROMFROM
character_event = {
	id = PlusGreatSchism.055

	hide_window = yes
	is_triggered_only = yes

	has_dlc = "Legacy of Rome"
	religion_group = christian

	trigger = {
		always = no # Disabled until the system is entirely functional
		has_pentarchy = yes
	}

	immediate = {
		log = "055: OFFER_VASSALIZATION_INTERACTION_ACCEPT_EVENT - [This.GetTitledNameWithNick] ([This.GetID])"

		# Set up event targets
		save_event_target_as = event_liege

		rightful_religious_head_scope = {
			save_event_target_as = event_liege_religious_head
		}

		FROM = {
			save_event_target_as = event_vassal
		}

		# Assign county titles, for Religion tab display on map
		event_target:event_vassal = {
			any_title_under = {
				limit = {
					# Assign all counties not already held under any Christian liege, with exception of new liege
					tier = COUNT

					holder_scope = {
						has_pentarchy = no

						NOT = {
							any_liege = {
								NOT = { character = event_target:event_liege } # liege, ROOT_FROMFROM
								has_pentarchy = yes
							}
						}
					}
				}

				location = {
					assign_pentarch_CK2Plus_effect = yes
				}
			}
		}

		clear_event_target = event_vassal
		clear_event_target = event_liege
		clear_event_target = event_liege_religious_head
	}
}

# 056: Update pentarch on titles of new vassal's realm
# Fires on
# - ASK_FOR_VASSALIZATION_INTERACTION_ACCEPT_EVENT
# ROOT is empty (don't use it)
# FROM is the liege
# FROMFROM is the new vassal
# Starting scope is FROMFROM
character_event = {
	id = PlusGreatSchism.056

	hide_window = yes
	is_triggered_only = yes

	has_dlc = "Legacy of Rome"

	trigger = {
		always = no # Disabled until the system is entirely functional

		# Liege
		FROM = {
			has_pentarchy = yes
		}
	}

	immediate = {
		log = "056: ASK_FOR_VASSALIZATION_INTERACTION_ACCEPT_EVENT - [This.GetTitledNameWithNick] ([This.GetID])"

		# Set up event targets
		save_event_target_as = event_vassal

		FROM = {
			save_event_target_as = event_liege

			rightful_religious_head_scope = {
				save_event_target_as = liege_religious_head
			}
		}

		# Assign county titles, for Religion tab display on map
		event_target:event_vassal = {
			any_title_under = {
				limit = {
					# Assign all counties not already held under any Christian liege, with exception of new liege
					tier = COUNT

					holder_scope = {
						has_pentarchy = no

						NOT = {
							any_liege = {
								NOT = { character = event_target:event_liege }
								has_pentarchy = yes
							}
						}
					}
				}

				location = {
					assign_pentarch_CK2Plus_effect = yes
				}
			}
		}

		clear_event_target = event_vassal
		clear_event_target = event_liege
		clear_event_target = event_liege_religious_head
	}
}

# 057: Update pentarch on province after religion change
# Fires on
# - on_county_religion_change
# ROOT is province
# FROM has the old religion
# Starting scope is owner of province
province_event = {
	id = PlusGreatSchism.057

	hide_window = yes
	is_triggered_only = yes

	has_dlc = "Legacy of Rome"

	trigger = {
		always = no # Disabled until the system is entirely functional
	}

	immediate = {
		log = "057: on_county_religion_change - [Root.GetName] ([Root.GetID])"

		if = {
			limit = {
				# Changed from a religion without pentarchy to a religion with pentarchy
				has_pentarchy = yes
				FROM = { has_pentarchy = no }

				# Not already held under any Christian liege
				owner = {
					NOR = {
						has_pentarchy = yes

						any_liege = {
							has_pentarchy = yes
						}
					}
				}
			}

			owner = { # Used to check for autocephalous patriarchs in 'assign_pentarch_CK2Plus_effect' below
				save_event_target_as = event_liege

				rightful_religious_head_scope = {
					save_event_target_as = event_liege_religious_head
				}
			}
			county = {
				location = {
					if = {
						limit = {
							region = custom_pentarch_rome
						}
						PREV = {
							set_pentarch_title = b_roma
							log = "[This.GetID]: Pentarch of Rome was assigned the [This.GetFullBaseName]"
						}
					}
					else = {
						assign_pentarch_CK2Plus_effect = yes
					}
				}
			}

			clear_event_target = event_liege
			clear_event_target = event_liege_religious_head
		}
		else_if = {
			limit = {
				# Changed from a religion with pentarchy to a religion without pentarchy
				has_pentarchy = no
				FROM = { has_pentarchy = yes }

				# Not in a Christian (sub)realm
				owner = {
					NOR = {
						has_pentarchy = yes

						any_liege = {
							has_pentarchy = yes
						}
					}
				}
			}

			county = {
				set_pentarch_title = none
				log = "[This.GetID]: Pentarchy was reset on the [This.GetFullBaseName], no longer in a realm using pentarchy"
			}
		}
	}
}

# 058: Pentarch re-established
# Fires on_new_holder
# ROOT is the title holder
# FROM is the title
# FROMFROM is the old holder
character_event = {
	id = PlusGreatSchism.058

	hide_window = yes
	is_triggered_only = yes

	has_dlc = "Legacy of Rome"
	religion_group = christian

	trigger = {
		always = no # Disabled until the system is entirely functional

		# Title
		FROM = {
			OR = {
				title = b_hagiasophia
				title = b_antioch
				title = b_jerusalem
				title = b_alexandria
				title = b_roma

				# Testing has shown this to be necessary
				AND = {
					title = c_constantinopolis

					b_hagiasophia = {
						is_capital = yes
					}
				}

				AND = {
					title = b_antioch

					b_antioch = {
						is_capital = yes
					}
				}

				AND = {
					title = c_jerusalem

					b_jerusalem = {
						is_capital = yes
					}
				}

				AND = {
					title = c_alexandria

					b_alexandria = {
						is_capital = yes
					}
				}

				AND = {
					title = c_roma

					b_roma = {
						is_capital = yes
					}
				}
			}
		}

		# New title holder
		is_theocracy = yes
		NOT = { has_landed_title = k_papacy } # Non-Papal patriarch of Rome

		OR = {
			religion = chalcedonian
			religion = orthodox
			religion = monothelite
			religion = iconoclast
		#	religion = miaphysite
		#	religion = monophysite
		#	religion = nestorian
		#	religion = messalian
		#	religion = apostolic
			religion = paulician
		}

		# Old title holder
		FROMFROM = {
			OR = {
				is_theocracy = no
				has_landed_title = k_papacy # If Rome is taken from the Pope

				NOR = {
					religion = chalcedonian
					religion = orthodox
					religion = monothelite
					religion = iconoclast
				#	religion = miaphysite
				#	religion = monophysite
				#	religion = nestorian
				#	religion = messalian
				#	religion = apostolic
					religion = paulician
				}
			}
		}
	}

	immediate = {
		log = "058: Pentarch re-established - [Root.GetTitledNameWithNick] ([Root.GetID])"

		# Province distances to pentarchs will have to be recalculated
		clean_up_pentarch_search_CK2Plus_effect = yes

		set_flag = is_pentarch

		any_independent_ruler = {
			limit = {
				OR = {
					has_pentarchy = yes

					any_realm_province = {
						has_pentarchy = yes
						held_under_PREV = yes
					}

					any_realm_lord = {
						has_pentarchy = yes
					}
				}

				NOT = {
					any_demesne_title = {
						higher_tier_than = DUKE
						has_title_flag = has_autocephalous_patriarch
					}
				}
			}

			save_event_target_as = event_liege

			rightful_religious_head_scope = {
				save_event_target_as = event_liege_religious_head
			}

			if = {
				limit = { has_pentarchy = yes }

				any_title_under = {
					limit = {
						OR = {
							tier = COUNT # required for religion tab on-map display

							holder_scope = {
								has_pentarchy = yes
								religion = ROOT
							}
						}

						holder_scope = {
							NOR = {
								# Don't interfere with autocephalous patriarchs of the same religion
								AND = {
									religion = ROOT

									any_demesne_title = {
										higher_tier_than = DUKE
										has_title_flag = has_autocephalous_patriarch
									}
								}

								any_liege = {
									religion = ROOT

									any_demesne_title = {
										higher_tier_than = DUKE
										has_title_flag = has_autocephalous_patriarch
									}
								}

								# Don't re-assign titles held by the Pope's lackeys
								rightful_religious_head_scope = {
									has_landed_title = k_papacy
								}

								any_liege = {
									rightful_religious_head_scope = {
										has_landed_title = k_papacy
									}
								}
							}
						}
					}

					log = "[This.GetID]: Processing pentarchy re-assignment for the [This.GetFullBaseName]"

					if = {
						limit = {
							lower_tier_than = DUKE
						}
						location = {
							assign_pentarch_CK2Plus_effect = yes
						}
					}
					else = {
						capital_scope = {
							assign_pentarch_CK2Plus_effect = yes
						}
					}
				}
			}
			else = {
				any_realm_province = {
					limit = {
						has_pentarchy = yes
						religion = ROOT
						held_under_PREV = yes

						trigger_if = { # the Pope always claims authority over Christians under non-Christian rule inside his purview
							limit = {
								b_roma = {
									holder_scope = {
										has_landed_title = k_papacy
									}
								}
							}
							NOT = { region = custom_pentarch_rome }
						}
					}

					county = {
						log = "[This.GetID]: Processing pentarchy re-assignment for the [This.GetFullBaseName]"

						location = {
							assign_pentarch_CK2Plus_effect = yes
						}
					}
				}
			}

			clear_event_target = event_liege
			clear_event_target = event_liege_religious_head
		}
	}
}

# 059: Pentarch disestablished
# Fires on_new_holder
# ROOT is the title holder
# FROM is the title
# FROMFROM is the old holder
character_event = {
	id = PlusGreatSchism.059

	hide_window = yes
	is_triggered_only = yes

	has_dlc = "Legacy of Rome"

	trigger = {
		always = no # Disabled until the system is entirely functional

		# Title
		FROM = {
			OR = {
				title = b_hagiasophia
				title = b_antioch
				title = b_jerusalem
				title = b_alexandria
				title = b_roma

				# Testing has shown this to be necessary
				AND = {
					title = c_constantinopolis

					b_hagiasophia = {
						is_capital = yes
					}
				}

				AND = {
					title = c_antioch

					b_antioch = {
						is_capital = yes
					}
				}

				AND = {
					title = c_jerusalem

					b_jerusalem = {
						is_capital = yes
					}
				}

				AND = {
					title = c_alexandria

					b_alexandria = {
						is_capital = yes
					}
				}

				AND = {
					title = c_roma

					b_roma = {
						is_capital = yes
					}
				}
			}
		}

		# New title holder
		OR = {
			is_theocracy = no

			NOR = {
				religion = chalcedonian
				religion = orthodox
				religion = monothelite
				religion = iconoclast
			#	religion = miaphysite
			#	religion = monophysite
			#	religion = nestorian
			#	religion = messalian
			#	religion = apostolic
				religion = paulician
			}
		}

		# Old title holder
		FROMFROM = {
			has_character_flag = is_pentarch
		}
	}

	immediate = {
		log = "059: Pentarch disestablished - [FromFrom.GetName] ([FromFrom.GetID])"

		# Province distances to pentarchs will have to be recalculated
		clean_up_pentarch_search_CK2Plus_effect = yes

		# Old title holder
		FROMFROM = {
			clr_flag = is_pentarch
		}

		if = { # ROOT is not included in 'any_independent_ruler' below
			limit = {
				independent = yes

				OR = {
					has_pentarchy = yes

					any_realm_province = {
						has_pentarchy = yes
						held_under_PREV = yes
					}

					any_realm_lord = {
						has_pentarchy = yes
					}
				}
			}

			save_event_target_as = event_liege

			rightful_religious_head_scope = {
				save_event_target_as = event_liege_religious_head
			}

			if = {
				limit = { has_pentarchy = yes }

				any_title_under = {
					limit = {
						OR = {
							tier = COUNT # For on map display in religion tab

							holder_scope = {
								has_pentarchy = yes
							}
						}

						holder_scope = {
							trigger_if = {
								limit = { has_pentarchy = yes }

								NOR = {
									# Don't interfere with autocephalous patriarchs of the same religion
									AND = {
										religion = FROMFROM

										any_demesne_title = {
											higher_tier_than = DUKE
											has_title_flag = has_autocephalous_patriarch
										}
									}

									any_liege = {
										religion = FROMFROM

										any_demesne_title = {
											higher_tier_than = DUKE
											has_title_flag = has_autocephalous_patriarch
										}
									}

									# Don't re-assign titles held by the Pope's lackeys
									rightful_religious_head_scope = {
										has_landed_title = k_papacy
									}

									any_liege = {
										rightful_religious_head_scope = {
											has_landed_title = k_papacy
										}
									}
								}
							}
						}
					}

					log = "[This.GetID]: Processing pentarchy re-assignment for the [This.GetFullBaseName]"

					if = {
						limit = {
							lower_tier_than = DUKE
						}
						location = {
							assign_pentarch_CK2Plus_effect = yes
						}
					}
					else = {
						capital_scope = {
							assign_pentarch_CK2Plus_effect = yes
						}
					}
				}
			}
			else = {
				any_realm_province = {
					limit = {
						has_pentarchy = yes
						held_under_PREV = yes

						trigger_if = {
							limit = {
								b_roma = {
									holder_scope = {
										has_landed_title = k_papacy
									}
								}
							}
							NOT = { region = custom_pentarch_rome }
						}
					}

					county = {
						log = "[This.GetID]: Processing pentarchy re-assignment for the [This.GetFullBaseName]"

						location = {
							assign_pentarch_CK2Plus_effect = yes
						}
					}
				}
			}

			clear_event_target = event_liege
			clear_event_target = event_liege_religious_head
		}

		any_independent_ruler = { # any_independent_ruler and any_playable_ruler do not include the currently scoped ruler
			limit = {
				OR = {
					has_pentarchy = yes

					any_realm_province = {
						has_pentarchy = yes
						held_under_PREV = yes
					}

					any_realm_lord = {
						has_pentarchy = yes
					}
				}

				NOT = {
					any_demesne_title = {
						higher_tier_than = DUKE
						has_title_flag = has_autocephalous_patriarch
					}
				}
			}

			save_event_target_as = event_liege

			rightful_religious_head_scope = {
				save_event_target_as = event_liege_religious_head
			}

			if = {
				limit = { has_pentarchy = yes }

				any_title_under = {
					limit = {
						OR = {
							tier = COUNT

							holder_scope = {
								has_pentarchy = yes
							}
						}

						holder_scope = {
							trigger_if = {
								limit = { has_pentarchy = yes }

								NOR = {
									# Don't interfere with autocephalous patriarchs of the same religion
									AND = {
										religion = FROMFROM

										any_demesne_title = {
											higher_tier_than = DUKE
											has_title_flag = has_autocephalous_patriarch
										}
									}

									any_liege = {
										religion = FROMFROM

										any_demesne_title = {
											higher_tier_than = DUKE
											has_title_flag = has_autocephalous_patriarch
										}
									}

									# Don't re-assign titles held by the Pope's lackeys
									rightful_religious_head_scope = {
										has_landed_title = k_papacy
									}

									any_liege = {
										rightful_religious_head_scope = {
											has_landed_title = k_papacy
										}
									}
								}
							}
						}
					}

					log = "[This.GetID]: Processing pentarchy re-assignment for the [This.GetFullBaseName]"

					if = {
						limit = {
							lower_tier_than = DUKE
						}
						location = {
							assign_pentarch_CK2Plus_effect = yes
						}
					}
					else = {
						capital_scope = {
							assign_pentarch_CK2Plus_effect = yes
						}
					}
				}
			}
			else = {
				any_realm_province = {
					limit = {
						has_pentarchy = yes
						held_under_PREV = yes

						trigger_if = {
							limit = {
								b_roma = {
									holder_scope = {
										has_landed_title = k_papacy
									}
								}
							}
							NOT = { region = custom_pentarch_rome }
						}
					}

					county = {
						log = "[This.GetID]: Processing pentarchy re-assignment for the [This.GetFullBaseName]"

						location = {
							assign_pentarch_CK2Plus_effect = yes
						}
					}
				}
			}

			clear_event_target = event_liege
			clear_event_target = event_liege_religious_head
		}
	}
}

# on_liege_change somehow?
# on_host_change and on_employer_change are insufficient

#!!

#######################################
#
#!	061 - 070: Ecumenical Councils
#
#######################################
#
#	INDEX
#	061: Council called
#	062: Rulers informed and get to choose
#	063: Council Ended
#
#######################################

# 061: Council called
long_character_event = {
	id = PlusGreatSchism.061
	title = PlusGreatSchism_061_title
	picture = GFX_evt_great_schism
	border = GFX_event_long_frame_religion

	# Mend Great Schism
	desc = {
		text = PlusGreatSchism_061_text_1
		trigger = { has_global_flag = great_schism_mending }
	}

	# Enforce Ephesus
	desc = {
		text = PlusGreatSchism_061_text_2
		trigger = { has_global_flag = enforcing_ephesus }
	}

	# Enforce Chalcedon
	desc = {
		text = PlusGreatSchism_061_text_3
		trigger = { has_global_flag = enforcing_chalcedon }
	}

	# Repudiate Ephesus
	desc = {
		text = PlusGreatSchism_061_text_4
		trigger = { has_global_flag = repudiating_ephesus }
	}

	# Repudiate Chalcedon
	desc = {
		text = PlusGreatSchism_061_text_5
		trigger = { has_global_flag = repudiating_chalcedon }
	}

	# Unite Christianity
	desc = {
		text = PlusGreatSchism_061_text_6
		trigger = { has_global_flag = uniting_christianity }
	}

	is_triggered_only = yes

	immediate = {
		# Pick the Location and Chair
		save_event_target_as = council_holder
		religion_head = { save_event_target_as = mender_religion }

		random_realm_province = {
			limit = {
				any_province_holding = {
					holding_type = temple
					holder_scope = { religion = ROOT }
				}

				held_under_PREV = yes
			}

			save_event_target_as = council_location
		}

		# Great Schism
		if = {
			limit = { has_global_flag = great_schism_mending }

			if = {
				limit = { is_catholic_branch = yes }

				random_landed_title = {
					limit = {
						controls_religion = yes

						holder_scope = {
							is_orthodox_branch = yes
							is_heretic = no
						}
					}

					holder_scope = {
						save_event_target_as = target_religion1
					}
				}
			}
			else = {
				random_landed_title = {
					limit = {
						controls_religion = yes

						holder_scope = {
							is_catholic_branch = yes
							is_heretic = no
						}
					}

					holder_scope = {
						save_event_target_as = target_religion1
					}
				}
			}
		}

		# Council of Ephesus
		else_if = {
			limit = { has_global_flag = enforcing_ephesus }

			random_landed_title = {
				limit = {
					controls_religion = yes

					holder_scope = {
						is_nestorian_branch = yes
						is_heretic = no
					}
				}

				holder_scope = {
					save_event_target_as = target_religion1
				}
			}
		}

		# Council of Chalcedon
		else_if = {
			limit = { has_global_flag = enforcing_chalcedon }

			random_landed_title = {
				limit = {
					controls_religion = yes

					holder_scope = {
						is_miaphysite_branch = yes
						is_heretic = no
					}
				}

				holder_scope = {
					save_event_target_as = target_religion1
				}
			}

			random_landed_title = {
				limit = {
					controls_religion = yes

					holder_scope = {
						is_apostolic_branch = yes
						is_heretic = no
					}
				}

				holder_scope = {
					save_event_target_as = target_religion2
				}
			}
		}

		# Repudiate instead
		else_if = {
			limit = {
				OR = {
					has_global_flag = repudiating_ephesus
					has_global_flag = repudiating_chalcedon
				}
			}

			random_landed_title = {
				limit = {
					controls_religion = yes

					holder_scope = {
						is_orthodox_branch = yes
						is_heretic = no
					}
				}

				holder_scope = {
					save_event_target_as = target_religion1
				}
			}
		}

		# Unite Christianity
		else_if = {
			limit = { has_global_flag = uniting_christianity }

			random_landed_title = {
				limit = {
					controls_religion = yes

					holder_scope = {
						is_catholic_branch = yes
						is_heretic = no
					}
				}

				holder_scope = {
					save_event_target_as = target_religion1
				}
			}
		}

		# No Distractions
		add_character_modifier = {
			name = do_not_disturb
			months = 1
			hidden = yes
		}

		# Fire the ending event
		narrative_event = {
			id = PlusGreatSchism.063
			months = 1
		}
	}

	option = {
		name = PlusGreatSchism_061_1

		hidden_effect = {
			# TODO: Let all vassals and their courtiers decide for themselves?
			any_realm_character = {
				limit = {
					OR = {
						religion = event_target:target_religion1
						religion = event_target:target_religion2
					}
					NOT = { trait = zealous }
				}

				# Workaround for automatic usurpation of theocratic characters
				event_target:mender_religion = { religion_scope = { save_event_target_as = target_religion } }
				convert_religion_failsafe_effect = yes
			}

			any_realm_province = {
				limit = {
					OR = {
						religion = event_target:target_religion1
						religion = event_target:target_religion2
					}

					held_under_PREV = yes
				}

				religion = event_target:mender_religion
			}

			any_independent_ruler = {
				limit = {
					OR = {
						religion = event_target:target_religion1
						religion = event_target:target_religion2
					}

					trigger_if = {
						limit = { ai = yes }
						trait = zealous
					}
				}

				narrative_event = {
					id = PlusGreatSchism.062 # Rulers choose
					random = 4
				}
			}
		}
	}
}

# 062: Rulers informed and get to choose
narrative_event = {
	id = PlusGreatSchism.062
	title = PlusGreatSchism_061_title
	picture = GFX_evt_great_schism
	border = GFX_event_narrative_frame_religion

	# Mend Great Schism
	desc = {
		text = PlusGreatSchism_061_text_1
		trigger = { has_global_flag = great_schism_mending }
	}

	# Enforce Ephesus
	desc = {
		text = PlusGreatSchism_061_text_2
		trigger = { has_global_flag = enforcing_ephesus }
	}

	# Enforce Chalcedon
	desc = {
		text = PlusGreatSchism_061_text_3
		trigger = { has_global_flag = enforcing_chalcedon }
	}

	# Repudiate Ephesus
	desc = {
		text = PlusGreatSchism_061_text_4
		trigger = { has_global_flag = repudiating_ephesus }
	}

	# Repudiate Chalcedon
	desc = {
		text = PlusGreatSchism_061_text_5
		trigger = { has_global_flag = repudiating_chalcedon }
	}

	# Unite Christianity
	desc = {
		text = PlusGreatSchism_061_text_6
		trigger = { has_global_flag = uniting_christianity }
	}

	is_triggered_only = yes
	hide_from = yes

	trigger = {
		OR = {
			religion = event_target:target_religion1
			religion = event_target:target_religion2
		}
	}

	# Convert
	option = {
		name = PlusGreatSchism_061_1a

		hidden_effect = {
			# TODO: Let all vassals (and their courtiers) decide for themselves?
			any_realm_character = {
				limit = {
					OR = {
						religion = event_target:target_religion1
						religion = event_target:target_religion2
					}
					NOT = { trait = zealous }
				}

				# Workaround for automatic usurpation of theocratic characters
				event_target:mender_religion = { religion_scope = { save_event_target_as = target_religion } }
				convert_religion_failsafe_effect = yes
			}

			any_realm_province = {
				limit = {
					OR = {
						religion = event_target:target_religion1
						religion = event_target:target_religion2
					}

					held_under_PREV = yes
				}

				religion = event_target:mender_religion
			}
		}

		ai_chance = {
			factor = 75

			trigger = {
				NOR = {
					trait = zealous
					has_character_modifier = voice_of_jesus
					has_character_modifier = voice_of_satan
				}
			}

			mult_modifier = {
				factor = 1.5
				trait = humble
			}

			mult_modifier = {
				factor = 1.5
				trait = craven
			}

			mult_modifier = {
				factor = 1.5
				trait = content
			}
		}
	}

	# Don't Convert
	option = {
		name = PlusGreatSchism_061_1b # Don't Convert

		ai_chance = {
			factor = 25

			trigger = {
				OR = {
					trait = zealous
					trait = lunatic
					trait = proud
					has_education_learning_trigger = yes
					has_character_modifier = voice_of_jesus
					has_character_modifier = voice_of_satan
					capital_holding = { holding_type = temple }
					higher_real_tier_than = DUKE
				}

				NOR = {
					trait = cynical
					has_character_modifier = defied_church
				}
			}

			mult_modifier = {
				factor = 3
				trait = lunatic
			}

			mult_modifier = {
				factor = 1.5
				trait = proud
			}

			mult_modifier = {
				factor = 1.5
				trait = brave
			}

			mult_modifier = {
				factor = 1.5
				trait = detached_priest
			}

			mult_modifier = {
				factor = 2
				trait = dutiful_cleric
			}

			mult_modifier = {
				factor = 2.5
				trait = scholarly_theologian
			}

			mult_modifier = {
				factor = 3
				trait = mastermind_theologian
			}

			mult_modifier = {
				factor = 2
				real_tier = KING
			}

			mult_modifier = {
				factor = 3
				real_tier = EMPEROR
			}
		}

		random = {
			chance = 25
			add_trait_zealous_effect = yes
			remove_trait_silently_cynical_effect = yes
		}
	}
}

# 063: Council Ended
narrative_event = {
	id = PlusGreatSchism.063
	title = PlusGreatSchism_061_title
	picture = GFX_evt_great_schism
	border = GFX_event_narrative_frame_religion

	# Mend Great Schism
	desc = {
		text = PlusGreatSchism_063_text_1
		trigger = { has_global_flag = great_schism_mending }
	}

	# Enforce Ephesus
	desc = {
		text = PlusGreatSchism_063_text_2
		trigger = { has_global_flag = enforcing_ephesus }
	}

	# Enforce Chalcedon
	desc = {
		text = PlusGreatSchism_063_text_3
		trigger = { has_global_flag = enforcing_chalcedon }
	}

	# Repudiate Ephesus
	desc = {
		text = PlusGreatSchism_063_text_4
		trigger = { has_global_flag = repudiating_ephesus }
	}

	# Repudiate Chalcedon
	desc = {
		text = PlusGreatSchism_063_text_5
		trigger = { has_global_flag = repudiating_chalcedon }
	}

	# Unite Christianity
	desc = {
		text = PlusGreatSchism_063_text_6
		trigger = { has_global_flag = uniting_christianity }
	}

	is_triggered_only = yes
	major = yes

	major_trigger = {
		religion_group = christian
	}

	immediate = {
		set_parent_religion = {
			religion = event_target:target_religion1
			parent = event_target:mender_religion
		}

		set_parent_religion = {
			religion = event_target:target_religion2
			parent = event_target:mender_religion
		}
	}

	option = {
		name = PlusGreatSchism_063_1a

		trigger = { religion = event_target:mender_religion }

		if = {
			limit = { character = event_target:council_holder }

			prestige = 1000
			set_character_flag = great_schism_mender
			give_nickname = nick_the_saint

			if = {
				limit = { has_global_flag = great_schism_mending }
				clr_global_flag = great_schism_mending
				set_global_flag = great_schism_mended
			}
			else_if = {
				limit = { has_global_flag = enforcing_ephesus }
				clr_global_flag = enforcing_ephesus
				set_global_flag = ephesus_enforced
			}
			else_if = {
				limit = { has_global_flag = enforcing_chalcedon }
				clr_global_flag = enforcing_chalcedon
				set_global_flag = chalcedon_enforced
			}
			else_if = {
				limit = { has_global_flag = repudiating_ephesus }
				clr_global_flag = repudiating_ephesus
				set_global_flag = ephesus_repudiated
			}
			else_if = {
				limit = { has_global_flag = repudiating_chalcedon }
				clr_global_flag = repudiating_chalcedon
				set_global_flag = chalcedon_repudiated
			}
			else_if = {
				limit = { has_global_flag = uniting_christianity }
				clr_global_flag = uniting_christianity
				set_global_flag = christianity_united
			}
		}
	}

	option = {
		name = {
			text = PlusGreatSchism_063_1b
			trigger = {
				religion_group = christian
				NOT = { religion = event_target:mender_religion }
			}
		}
		name = {
			text = PlusGreatSchism_063_1c
			trigger = {
				NOT = { religion_group = christian }
			}
		}
	}
}

#!!

##############################################################
#
#!	071 - 080: Schism Lead Up
#
##############################################################
#
#	INDEX
#
#	071: Pope appoints Holy Roman Emperor
#	072: Holy Roman Emperor fights the Byzantine Empire
#	073: Rome adopts Filioque
#	074: Jurisdictional dispute over the Balkans
#	075: Conflict over the Eucharist
#	076: Wicked Pope or Patriarch
#	077: Call for Ecumenical Council refused
#
##############################################################

# 071: Pope appoints Holy Roman Emperor
narrative_event = {
	id = PlusGreatSchism.071
	title = PlusGreatSchism_071_title
	desc = PlusGreatSchism_071_text
	picture = GFX_evt_found_hre
	border = GFX_event_narrative_frame_religion

	is_triggered_only = yes
	show_root = yes
	hide_from = yes
	major = yes

	trigger = {
		NOR = {
			has_global_flag = great_schism_prevented
			has_global_flag = great_schism_happened

			has_game_rule = {
				name = great_schism
				value = disabled
			}
		}
	}

	major_trigger = {
		ai = no
		religion_group = christian
	}

	option = {
		name = {
			text = PlusGreatSchism_071_1a
			trigger = {
				religion = chalcedonian
				OR = {
					trait = greek_rites
					trait = iconoclast_rites
				}
			}
		}
		name = {
			text = PlusGreatSchism_071_1b
			trigger = {
				religion = chalcedonian
				trait = latin_rites
			}
		}
		name = {
			text = PlusGreatSchism_071_1c
			trigger = {
				religion_group = christian
				NOT = { religion = chalcedonian }
			}
		}
		name = {
			text = PlusGreatSchism_071_1d
			trigger = {
				NOT = { religion_group = christian }
			}
		}
	}
}

# 072: Holy Roman Emperor fights the Byzantine Empire
narrative_event = {
	id = PlusGreatSchism.072

	title = PlusGreatSchism_072_title
	desc = PlusGreatSchism_072_text
	picture = GFX_evt_melee
	border = GFX_event_narrative_frame_religion

	is_triggered_only = yes
	major = yes
	hide_from = yes

	major_trigger = {
		ai = no
		religion_group = christian
	}

	religion = chalcedonian

	trigger = {
		OR = {
			has_landed_title = e_byzantium
			has_landed_title = e_roman_empire
		}

		is_heretic = no

		any_war = {
			OR = {
				AND = {
					defender = { character = ROOT }
					attacker = {
						religion = ROOT
						NOT = { trait = excommunicated }
						trait = crowned_by_pope
					}
				}

				AND = {
					attacker = { character = ROOT }
					defender = {
						religion = ROOT
						NOT = { trait = excommunicated }
						trait = crowned_by_pope
					}
				}
			}
		}

		NOR = {
			trait = excommunicated
			has_global_flag = attacked_by_papal_emperor
			has_global_flag = great_schism_prevented
			has_global_flag = great_schism_happened

			has_game_rule = {
				name = great_schism
				value = disabled
			}
		}
	}

	weight_multiplier = { days = 30 }

	immediate = {
		log = "SCHISM LEAD-UP: WAR BETWEEN BYZANTIUM AND POPE'S CHAMPION"
		set_global_flag = attacked_by_papal_emperor

		change_variable = {
			which = global_great_schism_tracker
			value = 1
		}

		random_current_enemy = {
			limit = { trait = crowned_by_pope }
			save_event_target_as = papal_emperor
		}
	}

	option = {
		name = {
			text = PlusGreatSchism_072_1a
			trigger = {
				OR = {
					trait = greek_rites
					trait = iconoclast_rites
				}
			}
		}
		name = {
			text = PlusGreatSchism_072_1b
			trigger = { trait = latin_rites }
		}
		name = {
			text = PlusGreatSchism_072_1c
			trigger = {
				religion_group = christian
				NOT = { religion = chalcedonian }
			}
		}
	}

	after = {
		clear_event_target = papal_emperor
	}
}

# 073: Rome adopts Filioque
narrative_event = {
	id = PlusGreatSchism.073
	title = PlusGreatSchism_073_title
	desc = PlusGreatSchism_073_text
	picture = GFX_evt_pope
	border = GFX_event_narrative_frame_religion
	portrait = b_hagiasophia

	major = yes
	show_root = yes

	only_rulers = yes
	religion = chalcedonian

	trigger = {
		has_landed_title = k_papacy
		year >= 900
		trait = latin_rites
		is_heretic = no

		NOR = {
			religion_authority >= 0.75
			has_global_flag = filioque_adopted
			has_global_flag = great_schism_happened
			has_global_flag = great_schism_prevented

			has_game_rule = {
				name = great_schism
				value = disabled
			}
		}
	}

	major_trigger = {
		ai = no
		religion = chalcedonian
	}

	mean_time_to_happen = {
		years = 100

		mult_modifier = {
			factor = 0.75
			year >= 950
		}

		mult_modifier = {
			factor = 0.5
			year >= 1000
		}

		mult_modifier = {
			factor = 0.5
			year >= 1025
		}

		mult_modifier = {
			factor = 0.25
			year >= 1050
		}

		mult_modifier = {
			factor = 0.75
			NOT = { religion_authority >= 0.45 }
		}

		mult_modifier = {
			factor = 0.75
			NOT = { religion_authority >= 0.40  }
		}

		mult_modifier = {
			factor = 0.75
			NOT = { religion_authority >= 0.35 }
		}

		mult_modifier = {
			factor = 0.75
			NOT = { religion_authority >= 0.30 }
		}
	}

	immediate = {
		log = "SCHISM LEAD-UP: FILIOQUE ADOPTED"
		set_global_flag = filioque_adopted

		change_variable = {
			which = global_great_schism_tracker
			value = 1
		}
	}

	option = {
		name = {
			text = PlusGreatSchism_073_1a
			trigger = {
				OR = {
					trait = greek_rites
					trait = iconoclast_rites
				}
			}
		}
		name = {
			text = PlusGreatSchism_073_1b
			trigger = { trait = latin_rites }
		}
		name = {
			text = PlusGreatSchism_073_1c
		}
	}
}

# 074: Jurisdictional dispute over the Balkans
narrative_event = {
	id = PlusGreatSchism.074
	title = PlusGreatSchism_074_title
	desc = PlusGreatSchism_074_text
	picture = GFX_evt_pope
	border = GFX_event_narrative_frame_religion

	major = yes
	hide_from = yes
	show_root = yes

	only_independent = yes
	religion = chalcedonian

	trigger = {
		k_papacy = {
			holder_scope = {
				religion = chalcedonian
				trait = latin_rites
			}
		}

		b_hagiasophia = {
			holder_scope = {
				religion = chalcedonian

				OR = {
					trait = greek_rites
					trait = iconoclast_rites
				}
			}
		}

		# Balkan ruler
		lower_real_tier_than = EMPEROR
		higher_real_tier_than = COUNT
		mercenary = no
		holy_order = no
		is_landed = yes

		capital_scope = {
			religion = chalcedonian

			# TODO: Replace with region?
			kingdom = {
				OR = {
					title = k_bulgaria
					title = k_serbia
					title = k_croatia
					title = k_wallachia
				}
			}

			empire = {
				NOR = {
					title = e_byzantium
					title = e_roman_empire
				}
			}
		}

		NOR = {
			has_global_flag = papal_jurisdiction_dispute
			has_global_flag = great_schism_happened
			has_global_flag = great_schism_prevented

			has_game_rule = {
				name = great_schism
				value = disabled
			}
		}
	}

	major_trigger = {
		ai = no
		religion = chalcedonian
	}

	mean_time_to_happen = {
		days = 1
	}

	immediate = {
		log = "SCHISM LEAD-UP: PAPAL JURISDICTION DISPUTE OVER [Root.PrimaryTitle.GetFullBaseName]"
		set_global_flag = papal_jurisdiction_dispute

		change_variable = {
			which = global_great_schism_tracker
			value = 1
		}
	}

	option = {
		name = {
			text = PlusGreatSchism_074_1a
			trigger = {
				OR = {
					trait = greek_rites
					trait = iconoclast_rites
				}
			}
		}
		name = {
			text = PlusGreatSchism_074_1b
			trigger = { trait = latin_rites }
		}
		name = {
			text = PlusGreatSchism_074_1c
		}

		show_portrait = k_papacy
		show_portrait = b_hagiasophia
	}
}

# 075: Conflict over the Eucharist
narrative_event = {
	id = PlusGreatSchism.075
	title = PlusGreatSchism_075_title
	picture = GFX_evt_bishop
	border = GFX_event_narrative_frame_religion

	desc = {
		text = PlusGreatSchism_075_text_greek
		trigger = {
			ROOT = {
				OR = {
					trait = greek_rites
					trait = iconoclast_rites
				}
			}
		}
	}
	desc = {
		text = PlusGreatSchism_075_text_latin
		trigger = { ROOT = { trait = latin_rites } }
	}

	major = yes
	hide_from = yes
	show_root = yes

	only_rulers = yes
	religion = chalcedonian
	capable_only = yes
	war = no

	trigger = {
		is_adult = yes
		year >= 900
		is_inaccessible_trigger = no

		NOR = {
			liege = { war = yes }
			trait = excommunicated
			has_global_flag = eucharist_conflict
			has_global_flag = great_schism_happened
			has_global_flag = great_schism_prevented

			has_game_rule = {
				name = great_schism
				value = disabled
			}
		}

		rightful_religious_head = ROOT
		is_heretic = no

		NOR = {
			has_landed_title = b_hagiasophia
			has_landed_title = k_papacy
		}
	}

	major_trigger = {
		ai = no
		religion_group = christian
	}

	mean_time_to_happen = {
		years = 50

		mult_modifier = {
			factor = 0.5
			year >= 1000
		}

		mult_modifier = {
			factor = 0.5
			year >= 1025
		}

		mult_modifier = {
			factor = 0.5
			year >= 1050
		}

		mult_modifier = {
			factor = 0.75
			NOT = { religion_authority >= 0.45 }
		}

		mult_modifier = {
			factor = 0.75
			NOT = { religion_authority >= 0.40 }
		}

		mult_modifier = {
			factor = 0.75
			NOT = { religion_authority >= 0.35 }
		}

		mult_modifier = {
			factor = 0.75
			NOT = { religion_authority >= 0.30 }
		}
	}

	immediate = {
		log = "SCHISM LEAD-UP: CONFLICT OVER THE EUCHARIST"
		set_global_flag = eucharist_conflict

		change_variable = {
			which = global_great_schism_tracker
			value = 1
		}
	}

	option = {
		name = {
			text = PlusGreatSchism_075_1a # The Eucharist uses leavened bread
			trigger = {
				OR = {
					trait = greek_rites
					trait = iconoclast_rites
				}
			}
		}
		name = {
			text = PlusGreatSchism_075_1b # The Eucharist uses unleavened bread
			trigger = { trait = latin_rites }
		}
		name = {
			text = PlusGreatSchism_075_1c # All this over bread?
		}
	}
}

# 076: Wicked Pope or Patriarch
narrative_event = {
	id = PlusGreatSchism.076
	title = PlusGreatSchism_076_title
	desc = PlusGreatSchism_076_text
	picture = GFX_evt_pope
	border = GFX_event_narrative_frame_religion
	portrait = event_target:other_patriarch

	major = yes
	show_root = yes

	only_rulers = yes
	religion = chalcedonian

	trigger = {
		trait = bad_priest_christian
		is_heretic = no
		intrigue < 12

		OR = {
			has_landed_title = k_papacy
			has_landed_title = b_hagiasophia
		}

		check_variable = {
			which = global_great_schism_tracker
			value < 4
		}

		NOR = {
			has_global_flag = wicked_pope_patriarch
			has_global_flag = great_schism_happened
			has_global_flag = great_schism_prevented

			has_game_rule = {
				name = great_schism
				value = disabled
			}
		}

		k_papacy = {
			holder_scope = {
				religion = ROOT

				trigger_if = {
					limit = { trait = bad_priest_christian }
					character = ROOT
				}
			}
		}

		b_hagiasophia = {
			holder_scope = {
				religion = ROOT

				trigger_if = {
					limit = { trait = bad_priest_christian }
					character = ROOT
				}
			}
		}
	}

	major_trigger = {
		ai = no
		religion_group = christian
	}

	mean_time_to_happen = {
		years = 10

		mult_modifier = {
			factor = 2
			religion_authority >= 0.70
		}

		mult_modifier = {
			factor = 2
			religion_authority >= 0.80
		}

		mult_modifier = {
			factor = 2
			religion_authority >= 0.90
		}

		mult_modifier = {
			factor = 0.75
			NOT = { religion_authority >= 0.50 }
		}

		mult_modifier = {
			factor = 0.75
			NOT = { religion_authority >= 0.45 }
		}

		mult_modifier = {
			factor = 0.75
			NOT = { religion_authority >= 0.40 }
		}

		mult_modifier = {
			factor = 0.75
			NOT = { religion_authority >= 0.35 }
		}

		mult_modifier = {
			factor = 0.75
			NOT = { religion_authority >= 0.30 }
		}
	}

	immediate = {
		log = "SCHISM LEAD-UP: [Root.GetTitledName] IS WICKED"
		set_global_flag = wicked_pope_patriarch

		change_variable = {
			which = global_great_schism_tracker
			value = 1
		}

		if = {
			limit = { has_landed_title = k_papacy }
			b_hagiasophia = {
				holder_scope = { save_event_target_as = other_patriarch }
			}
		}
		else = {
			k_papacy = {
				holder_scope = { save_event_target_as = other_patriarch }
			}
		}
	}

	option = {
		name = PlusGreatSchism_076_1
	}
}

# 077: Call for Ecumenical Council refused
narrative_event = {
	id = PlusGreatSchism.077
	title = PlusGreatSchism_077_title
	desc = PlusGreatSchism_077_text
	picture = GFX_evt_rival_rulers
	border = GFX_event_narrative_frame_religion
	portrait = b_hagiasophia

	major = yes
	show_root = yes

	only_rulers = yes
	religion = chalcedonian

	trigger = {
		has_landed_title = k_papacy
		year >= 1000
		war = no
		is_heretic = no
		is_inaccessible_trigger = no

		is_variable_equal = {
			which = global_great_schism_tracker
			value >= 3
		}

		b_hagiasophia = {
			holder_scope = {
				OR = {
					trait = greek_rites
					trait = iconoclast_rites
				}

				war = no
				NOT = { trait = excommunicated }
				is_inaccessible_trigger = no
			}
		}

		NOR = {
			trait = excommunicated
			has_global_flag = ecumenical_council_refused
			has_global_flag = great_schism_happened
			has_global_flag = great_schism_prevented

			has_game_rule = {
				name = great_schism
				value = disabled
			}
		}
	}

	major_trigger = {
		ai = no
		religion_group = christian
	}

	mean_time_to_happen = {
		years = 25

		mult_modifier = {
			factor = 0.5
			year >= 1025
		}

		mult_modifier = {
			factor = 0.5
			year >= 1050
		}

		mult_modifier = {
			factor = 1.5
			religion_authority >= 0.70
		}

		mult_modifier = {
			factor = 1.5
			religion_authority >= 0.80
		}

		mult_modifier = {
			factor = 1.5
			religion_authority >= 0.90
		}

		mult_modifier = {
			factor = 0.75
			NOT = { religion_authority >= 0.50 }
		}

		mult_modifier = {
			factor = 0.75
			NOT = { religion_authority >= 0.45 }
		}

		mult_modifier = {
			factor = 0.75
			NOT = { religion_authority >= 0.40 }
		}

		mult_modifier = {
			factor = 0.75
			NOT = { religion_authority >= 0.35 }
		}

		mult_modifier = {
			factor = 0.75
			NOT = { religion_authority >= 0.30 }
		}
	}

	immediate = {
		log = "SCHISM LEAD-UP: CALL FOR ECUMENICAL COUNCIL REFUSED"
		set_global_flag = ecumenical_council_refused

		change_variable = {
			which = global_great_schism_tracker
			value = 1
		}

		b_hagiasophia = {
			holder_scope = { save_event_target_as = current_patriarch }
		}
	}

	option = {
		name = {
			text = PlusGreatSchism_077_1a
			trigger = {
				OR = {
					trait = greek_rites
					trait = iconoclast_rites
				}
			}
		}
		name = {
			text = PlusGreatSchism_077_1b
			trigger = { trait = latin_rites }
		}
		name = {
			text = PlusGreatSchism_077_1c
		}
	}
}

#!!