namespace = Plus
namespace = trigger_schism

# reserved Plus.2700 to Plus.2749

# Manually trigger the schism at any time
character_event = {
	id = trigger_schism.1
	desc = EVTDESC_Plus_2700
	picture = GFX_evt_great_schism

	is_triggered_only = yes
	hide_from = yes

	option = {
		name = EVTOPTA_Plus_2700

		hidden_effect = {
			set_global_flag = schism_manual_override # Required in order for the event to bypass normal checks

			event_target:pulse_province = {
				owner = {
					character_event = { id = PlusGreatSchism.004 } # Trigger the schism
				}
			}
		}
	}

	option = {
		name = EVTOPTB_Plus_2700
	}
}

# This event deals with the setup of the scenario before conversion
character_event = {
	id = Plus.2702

	is_triggered_only = yes # PlusStartUp.002
	hide_window = yes

	trigger = {
		NOT = { has_global_flag = ancient_religions_start }
	}

	immediate = {
		set_global_flag = ancient_religions_start

		# First deal with the religious heads
		destroy_landed_title = d_sunni
		destroy_landed_title = d_shiite

		if = {
			limit = {
				NOT = {
					has_game_rule = {
						name = ancient_religions
						value = specific_provinces
					}
				}
			}

			# Distribute the pope's titles
			k_papacy = {
				holder_scope = {
					any_vassal = {
						limit = { higher_real_tier_than = BARON }
						set_defacto_liege = THIS
						save_event_target_as = holder_of_papal_lands
					}

					any_demesne_title = {
						limit = {
							OR = {
								real_tier = COUNT
								real_tier = DUKE
							}
						}

						grant_title_no_opinion = event_target:holder_of_papal_lands
					}

					any_demesne_title = {
						limit = { real_tier = BARON }

						create_character = {
							random_traits = yes
							age = 21
							female = no
							culture = PREVPREV
							religion = PREVPREV
							dynasty = random
						}

						new_character = {
							grant_title_no_opinion = PREV
							set_defacto_liege = THIS
						}
					}
				}
			}
		}

		# Does the HRE exist? If so we have some cleaning up to do. First switch it to the German empire
		if = {
			limit = {
				e_hre = {
					has_holder = yes

					OR = {
						NOT = {
							has_game_rule = {
								name = ancient_religions
								value = specific_provinces
							}
						}

						capital_scope = {
							OR = {
								region = custom_germany
								region = custom_frisia
							}
						}
					}
				}
			}

			e_hre = {
				holder_scope = {
					save_event_target_as = hre_kaiser

					if = {
						limit = { trait = crowned_by_pope }
						remove_trait = crowned_by_pope
						add_trait = crowned # Lets keep ordinary trait here, papal coronation shouldn't matter to pagans.
					}
				}
			}


			e_germania = {
				if = { # Activate e_germania if needed
					limit = { NOT = { is_title_active = THIS } }

					activate_title = {
						title = THIS
						status = yes
					}
				}

				# Copy the HRE's laws to Germany
				set_title_flag = non_manual_law_change # No prestige loss
				copy_title_laws = e_hre
				gain_title = event_target:hre_kaiser
				make_primary_title = yes
				clr_title_flag = non_manual_law_change # No prestige loss
			}

			e_hre = {
				# Replace e_hre claims with e_germania claims
				any_claimant = {
					if = {
						limit = { has_strong_pressed_claim = e_hre }
						add_pressed_claim = e_germania
						remove_claim = e_hre
					}
					else_if = {
						limit = { has_strong_claim = e_hre }
						add_claim = e_germania
						remove_claim = e_hre
					}
					else_if = {
						limit = { has_weak_pressed_claim = e_hre }
						add_weak_pressed_claim = e_germania
						remove_claim = e_hre
					}
					else_if = {
						limit = { has_weak_claim = e_hre }
						add_weak_claim = e_germania
						remove_claim = e_hre
					}
				}

				# Migrate over the HRE's de Jure vassals
				any_direct_de_jure_vassal_title = {
					de_jure_liege = e_germania
				}

				destroy_landed_title = yes
			}
		}

		# Now that the world is set up, convert them all to Ancient Relgions
		any_independent_ruler = {
			any_realm_province = {
				limit = {
					OR = {
						has_castle = yes
						has_temple = yes
						has_city = yes
						has_tribal = yes
					}

					held_under_PREV = yes
				}

				if = {
					limit = {
						has_game_rule = {
							name = ancient_religions
							value = regions
						}
					}

					emf_nwo_generate_ancient_religion_provinces_region = yes
				}
				else_if = {
					limit = {
						OR = {
							has_game_rule = {
								name = ancient_religions
								value = cultures
							}

							has_game_rule = {
								name = ancient_religions
								value = cultures_characters
							}
						}
					}

					emf_nwo_generate_ancient_religion_provinces_culture = yes
				}
				else_if = {
					limit = {
						has_game_rule = {
							name = ancient_religions
							value = specific_provinces
						}
					}

					emf_nwo_generate_ancient_religion_provinces_specific = yes
				}
			}

			# This will convert all the characters of the world to appropriate religions
			if = {
				limit = {
					capital_scope = {
						has_province_flag = ar_setup
					}
				}

				# Change religion based on culture
				if = {
					limit = {
						has_game_rule = {
							name = ancient_religions
							value = cultures_characters
						}
					}

					capital_scope = {
						emf_nwo_generate_ancient_religion_province_character_culture = yes # save target_religion
						religion = event_target:target_religion
					}

					if = {
						limit = { event_target:target_religion = { always = yes } }
						convert_religion_failsafe_effect = yes # use_target_religion
					}

					any_courtier = {
						limit = { is_ruler = no }

						emf_nwo_generate_ancient_religion_province_character_culture = yes # save target_religion

						if = {
							limit = { event_target:target_religion = { always = yes } }
							convert_religion_failsafe_effect = yes # use_target_religion
						}
					}

					any_realm_lord = {
						emf_nwo_generate_ancient_religion_province_character_culture = yes # save target_religion

						if = {
							limit = { event_target:target_religion = { always = yes } }
							convert_religion_failsafe_effect = yes # use_target_religion
						}

						any_courtier = {
							limit = { is_ruler = no }

							emf_nwo_generate_ancient_religion_province_character_culture = yes # save target_religion

							if = {
								limit = { event_target:target_religion = { always = yes } }
								convert_religion_failsafe_effect = yes # use_target_religion
							}
						}
					}
				}
				else = {
					# change religion of independent ruler based on their capital
					capital_scope = {
						religion_scope = { save_event_target_as = target_religion }
					}

					convert_religion_failsafe_effect = yes

					# Match courtier religion to liege religion
					any_courtier = {
						limit = { is_ruler = no }
						religion = liege
					}

					# Match vassal religion to top liege religion
					any_realm_lord = {
						capital_scope = { religion_scope = { save_event_target_as = target_religion } }
						convert_religion_failsafe_effect = yes

						# Match courtier religion to liege religion
						any_courtier = {
							limit = { is_ruler = no }
							religion = liege
						}
					}
				}
			}
		}

		if = {
			limit = {
				has_game_rule = {
					name = ancient_religions
					value = cultures_characters
				}
			}

			set_mercenaries_to_ar_culture_effect = yes # Set mercenaries to ancient religions based on their culture
		}
		else = {
			set_mercenaries_to_ar_effect = yes # Set mercenaries to ancient religions based on their capital
		}
	}
}

# Notify Player of normal end date
character_event = {
	id = Plus.2706
	desc = EVTDESC_Plus_2706
	picture = GFX_evt_game_over

	is_triggered_only = yes # on_yearly_pulse
	hide_from = yes

	ai = no

	trigger = {
		year >= 1453
		NOT = { has_global_flag = players_informed_of_end }
	}

	immediate = {
		set_global_flag = players_informed_of_end
	}

	option = {
		name = EVTOPTA_Plus_2706 # Just a bit longer!
	}
}