
namespace = emf_nwo


# emf_nwo.0 [Isis]
#
# Put Isis in the FROM scope before interacting with player
character_event = {
	id = emf_nwo.0
	
	is_triggered_only = yes
	hide_window = yes

	immediate = {
		random_player = {
			narrative_event = { id = emf_nwo.1 }
		}
	}
}


# emf_nwo.1 [Player]
#
# Would you like independent counts, de jure dukes, or de jure kings?
# Plus explain things a bit and preview the other followup options.
narrative_event = {
	id = emf_nwo.1
	title = emf_nwo.1.title
	desc = emf_nwo.1.desc
	picture = GFX_evt_apocalypto
	border = GFX_event_narrative_frame_war

	is_triggered_only = yes
	
	ai = no

	option = {
		name = emf_nwo.1.opt.a # Independent Counts
		hidden_tooltip = {
			log = "emf_nwo: Independent Counts (emf_nwo_count)"
			set_global_flag = emf_nwo_count
			FROM = { character_event = { id = emf_nwo.2 } }
		}
	}
	
	option = {
		name = emf_nwo.1.opt.b # De Jure Dukes

		hidden_tooltip = {
			log = "emf_nwo: De Jure Dukes (emf_nwo_duke)"
			set_global_flag = emf_nwo_duke
			FROM = { character_event = { id = emf_nwo.8 } }
		}
	}
	
	option = {
		name = emf_nwo.1.opt.c # De Jure Kings
		
		hidden_tooltip = {
			log = "emf_nwo: De Jure Kings (emf_nwo_king)"
			set_global_flag = emf_nwo_king
			FROM = { character_event = { id = emf_nwo.8 } }
		}
	}

	option = {
		name = CANCEL
	}
}


# emf_nwo.2 [Isis]
#
# Bounce
character_event = {
	id = emf_nwo.2
	
	is_triggered_only = yes
	hide_window = yes

	immediate = {
		if = {
			limit = { has_dlc = "Horse Lords" }
			FROM = { letter_event = { id = emf_nwo.13 } }
			break = yes
		}

		FROM = { letter_event = { id = emf_nwo.3 } }
	}
}


# emf_nwo.3 [Player]
#
# What part of the world do you want to be affected by the New World Order?
# - The Whole World
# - Only Catholic Realms
# - Only Christian Realms
# - Only Muslim Realms
letter_event = {
	id = emf_nwo.3
	desc = emf_nwo.3.desc

	is_triggered_only = yes

	option = {
		name = emf_nwo.3.opt.a # The Whole World
		
		hidden_tooltip = {
			FROM = { character_event = { id = emf_nwo.4 } }
		}
	}
	option = {
		name = emf_nwo.3.opt.b # Only Catholic Realms
		
		trigger = {
			k_papal_state = { has_title_flag = schism_happened }
		}
		
		hidden_tooltip = {
			log = "emf_nwo: Excluding non-Catholic... (emf_nwo_only_catholic)"
			set_global_flag = emf_nwo_only_catholic
			FROM = { character_event = { id = emf_nwo.4 } }
		}
	}
	option = {
		name = emf_nwo.3.opt.c # Only Orthodox Realms
		
		trigger = {
			k_papal_state = { has_title_flag = schism_happened }
		}
		
		hidden_tooltip = {
			log = "emf_nwo: Excluding non-Orthodox... (emf_nwo_only_orthodox)"
			set_global_flag = emf_nwo_only_orthodox
			FROM = { character_event = { id = emf_nwo.4 } }
		}
	}		
	option = {
		name = emf_nwo.3.opt.d # Only Christian Realms
		
		hidden_tooltip = {
			log = "emf_nwo: Excluding non-Christian... (emf_nwo_only_christian)"
			set_global_flag = emf_nwo_only_christian
			FROM = { character_event = { id = emf_nwo.4 } }
		}
	}
	option = {
		name = emf_nwo.3.opt.e # Only Muslim Realms
		
		hidden_tooltip = {
			log = "emf_nwo: Excluding non-Muslim... (emf_nwo_only_muslim)"
			set_global_flag = emf_nwo_only_muslim
			FROM = { character_event = { id = emf_nwo.6 } } # Skip asking about the Pope
		}
	}
}


# emf_nwo.4 [Isis]
#
# Bounce
character_event = {
	id = emf_nwo.4
	
	is_triggered_only = yes
	hide_window = yes
	
	immediate = {
		if = {
			limit = {
				k_papal_state = { has_title_flag = schism_happened }
			}
			FROM = { letter_event = { id = emf_nwo.5 } }
		}
		if = {
			limit = {
				k_papal_state = { NOT = { has_title_flag = schism_happened } }
			}
			FROM = { letter_event = { id = emf_nwo.7 } }
		}
	}
}


# emf_nwo.5 [Player]
#
# Would you like to spare the Pope's lands?
letter_event = {
	id = emf_nwo.5
	desc = emf_nwo.5.desc

	is_triggered_only = yes
	
	option = {
		name = emf_nwo.5.opt.a # Yes, spare all of the Papacy
		
		hidden_tooltip = {
			log = "emf_nwo: Excluding Papacy... (emf_nwo_spare_pope)"
			set_global_flag = emf_nwo_spare_pope
			FROM = { character_event = { id = emf_nwo.6 } }
		}
	}
	
	option = {
		name = emf_nwo.5.opt.b # Yes, but only let the Pope have Rome
		
		hidden_tooltip = {
			log = "emf_nwo: Excluding Pope... (emf_nwo_spare_pope_only)"
			set_global_flag = emf_nwo_spare_pope_only
			FROM = { character_event = { id = emf_nwo.6 } }
		}
	}
	
	option = {
		name = emf_nwo.5.opt.c # No, the Pope doesn't need land.

		hidden_tooltip = {
			FROM = { character_event = { id = emf_nwo.6 } }
		}
	}
}


# emf_nwo.6 [Isis]
#
# Bounce
character_event = {
	id = emf_nwo.6
	
	is_triggered_only = yes
	hide_window = yes
	
	immediate = {
		FROM = { letter_event = { id = emf_nwo.7 } }
	}
}


# emf_nwo.7 [Player]
#
# Would you like to spare all merchant republics?
letter_event = {
	id = emf_nwo.7
	desc = emf_nwo.7.desc

	is_triggered_only = yes

	option = {
		name = YES
		
		hidden_tooltip = {
			log = "emf_nwo: Excluding merchant republics... (emf_nwo_spare_mr)"
			set_global_flag = emf_nwo_spare_mr
			
			if = {
				limit = { not = { has_global_flag = emf_nwo_king } }
				FROM = { character_event = { id = emf_nwo.10 } } # Rapid Conquest?
				break = yes
			}
			
			FROM = { character_event = { id = emf_nwo.100 } }
		}
	}
	
	option = {
		name = NO
		trigger = {
			is_patrician = no
		}
		
		hidden_tooltip = {
			if = {
				limit = { not = { has_global_flag = emf_nwo_king } }
				FROM = { character_event = { id = emf_nwo.10 } } # Rapid Conquest?
				break = yes
			}
			
			FROM = { character_event = { id = emf_nwo.100 } }
		}
	}
}


# emf_nwo.8 [Isis]
#
# Bounce
character_event = {
	id = emf_nwo.8
	
	is_triggered_only = yes
	hide_window = yes
	
	immediate = {
		FROM = { letter_event = { id = emf_nwo.9 } }
	}
}


# emf_nwo.9 [Player]
#
# For those rulers which already hold de jure [duchies/kingdoms],
# should they remain the [dukes/kings] of their capital [duchies/kingdoms]?
letter_event = {
	id = emf_nwo.9

	is_triggered_only = yes
	
	desc = {
		text = emf_nwo.9.desc.duke
		trigger = {
			has_global_flag = emf_nwo_duke
		}
	}
	
	desc = {
		text = emf_nwo.9.desc.king
		trigger = {
			has_global_flag = emf_nwo_king
		}
	}

	option = {
		name = emf_nwo.9.opt.a # Yes, but still make players de jure dukes/kings
		
		hidden_tooltip = {
			log = "emf_nwo: Retaining previous rulers... (emf_nwo_retain_rulers, emf_nwo_retain_rulers_not_me)"
			set_global_flag = emf_nwo_retain_rulers
			set_global_flag = emf_nwo_retain_rulers_not_me
			FROM = { character_event = { id = emf_nwo.2 } }
		}
	}
	
	option = {
		name = emf_nwo.9.opt.b # Yes, even if that makes me a vassal
		
		hidden_tooltip = {
			log = "emf_nwo: Retaining previous rulers... (emf_nwo_retain_rulers)"
			set_global_flag = emf_nwo_retain_rulers
			FROM = { character_event = { id = emf_nwo.2 } }
		}
	}
	
	option = {
		name = NO
		
		hidden_tooltip = {
			FROM = { character_event = { id = emf_nwo.2 } }
		}
	}
}


# emf_nwo.10 [Isis]
#
# Bounce
character_event = {
	id = emf_nwo.10
	
	is_triggered_only = yes
	hide_window = yes
	
	immediate = {
		FROM = { letter_event = { id = emf_nwo.11 } }
	}
}


# emf_nwo.11 [Player]
#
# Would you like to enable rapid conquest CBs?
letter_event = {
	id = emf_nwo.11
	desc = emf_nwo.11.desc

	is_triggered_only = yes

	option = {
		name = YES
		
		hidden_tooltip = {
			set_global_flag = emf_rapid_conquest
			FROM = { character_event = { id = emf_nwo.100 } }
		}
	}
	
	option = {
		name = NO
		
		hidden_tooltip = {
			clr_global_flag = emf_rapid_conquest
			FROM = { character_event = { id = emf_nwo.100 } }
		}
	}
}


# emf_nwo.12 [Isis]
#
# Bounce [from emf_nwo.13]
character_event = {
	id = emf_nwo.12
	
	is_triggered_only = yes
	hide_window = yes
	
	immediate = {
		FROM = { letter_event = { id = emf_nwo.3 } } # Which parts of the world?
	}
}


# emf_nwo.13 [Player]
#
# How do you want to handle nomads? [from emf_nwo.2 if HL DLC active]
letter_event = {
	id = emf_nwo.13
	desc = emf_nwo.13.desc

	is_triggered_only = yes
	
	option = {
		name = emf_nwo.13.opt.a # Make every clan independent
		
		hidden_tooltip = {
			FROM = { character_event = { id = emf_nwo.12 } }
		}
	}

	option = {
		name = emf_nwo.13.opt.b # Each clan gets a single province
		
		hidden_tooltip = {
			log = "emf_nwo: Giving each nomadic clan a single province... (emf_nwo_tiny_nomad)"
			set_global_flag = emf_nwo_tiny_nomad
			FROM = { character_event = { id = emf_nwo.12 } }
		}
	}

	option = {
		name = emf_nwo.13.opt.c # Leave them alone
		
		hidden_tooltip = {
			log = "emf_nwo: Excluding nomads... (emf_nwo_spare_nomad)"
			set_global_flag = emf_nwo_spare_nomad
			FROM = { character_event = { id = emf_nwo.12 } }
		}
	}
}


# emf_nwo.100 [Isis]
#
# [CK2Plus extension for ARR]
# Bounce
character_event = {
	id = emf_nwo.100

	is_triggered_only = yes
	hide_window = yes

	immediate = {
		if = {
			limit = { has_dlc = "The Old Gods" }
			FROM = { letter_event = { id = emf_nwo.101 } } # Ask about AR
			break = yes
		}

		FROM = { letter_event = { id = emf_nwo.103 } } # Ask about RNG children
	}
}


# emf_nwo.101 [Player]
#
# [CK2Plus extension for ARR]
# Select Religions
letter_event = {
	id = emf_nwo.101
	desc = emf_nwo.101.desc

	is_triggered_only = yes

	option = {
		name = emf_nwo.101.opt.a # Standard Religions

		custom_tooltip = { text = "emf_nwo.101.opt.a.hover_ctt" }

		hidden_tooltip = {
			FROM = { character_event = { id = emf_nwo.104 } } # Bounce
		}
	}
	option = {
		name = emf_nwo.101.opt.b # Trigger Schism
		
		custom_tooltip = { text = "emf_nwo.101.opt.b.hover_ctt" }
		
		trigger = {
			k_papal_state = { NOT = { has_title_flag = schism_happened } }
		}
		
		hidden_tooltip = {
			set_global_flag = schism_manual_override # Required in order for the event to bypass normal checks	
			FROM = { character_event = { id = emf_nwo.104 } } # Bounce
			d_pentarch_rome = { holder_scope = { character_event = { id = Plus.2285 days = 1 } } } # Trigger the schism, must be delayed slightly or it won't fire
		}
	}
	option = {
		name = emf_nwo.101.opt.c # Ancient Religions

		custom_tooltip = { text = "emf_nwo.101.opt.c.hover_ctt" }

		hidden_tooltip = {
			set_global_flag = emf_nwo_ancient_religions
			hidden_tooltip = {
				FROM = { character_event = { id = emf_nwo.104 } } # Bounce
			}
		}
	}
}

# emf_nwo.102
#
# adds children to NWO characters
character_event = {
	id = emf_nwo.102

	hide_window = yes
	is_triggered_only = yes
	only_playable = yes
	
	immediate = {
		set_character_flag = emf_nwo_child_generated
		random_list = { # Randomly generate at least one child guaranteed if the character is older than 16
			50 = { emf_nwo_generate_male_child = yes } 	# Generate male child
			50 = { emf_nwo_generate_female_child = yes } # Generate female child
		}
		random = { # 50% chance to get a 2nd child
			chance = 50
			random_list = { # Randomly pick the gender
				50 = { emf_nwo_generate_male_child = yes } 	# Generate male child
				50 = { emf_nwo_generate_female_child = yes } # Generate female child
			}
		}
		random_playable_ruler = {
			limit = {
				NOT = { has_character_flag = emf_nwo_child_generated }
				age = 17
				has_global_flag = emf_nwo_generate_children 
				or = {
					and = {
						religion = catholic
						has_global_flag = emf_nwo_only_catholic
					}
					and = {
						religion = orthodox
						has_global_flag = emf_nwo_only_orthodox
					}
					and = {
						religion_group = christian
						has_global_flag = emf_nwo_only_christian
					}
					and = {
						religion_group = muslim
						has_global_flag = emf_nwo_only_muslim
					}
					NOR = {
						has_global_flag = emf_nwo_only_catholic
						has_global_flag = emf_nwo_only_orthodox
						has_global_flag = emf_nwo_only_christian
						has_global_flag = emf_nwo_only_muslim
					}
				}
			}
			character_event = { id = emf_nwo.102 }
		}
	}
}

# emf_nwo.103 [Player]
#
# Generate children?
letter_event = {
	id = emf_nwo.103
	desc = emf_nwo.103.desc

	is_triggered_only = yes

	option = {
		name = emf_nwo.103.opt.a # Yes

		custom_tooltip = { text = "emf_nwo.103.opt.a.hover_ctt" }
		
		hidden_tooltip = {
			set_global_flag = emf_nwo_generate_children
			FROM = { character_event = { id = emf_nwo.20 } } # On with the show!
		}
	}
	option = {
		name = emf_nwo.103.opt.b # No
		
		custom_tooltip = { text = "emf_nwo.103.opt.b.hover_ctt" }
		
		hidden_tooltip = {
			FROM = { character_event = { id = emf_nwo.20 } } # On with the show!
		}
	}
}

# emf_nwo.104 [Isis]
#
# Bounce
character_event = {
	id = emf_nwo.104

	is_triggered_only = yes
	hide_window = yes

	immediate = {
		FROM = { letter_event = { id = emf_nwo.103 } } # Ask about RNG children
	}
}




# 'Would you like to intelligently randomize the religions of the newly formed realms?'
# -> Big Maybe.

# 'Would you like to bless a random selection of independent rulers as Lucky and give them some starting event troops?'
# - Applies to nwo_count and nwo_duke

# 'Would you like to feudalize theocratic and republican counties where possible?'
# -> Maybe. Co
# - Applies to nwo_count


# emf_nwo.20 [Isis]
#
# Now that we have all of our options collected from the player, get down to business.
character_event = {
	id = emf_nwo.20

	is_triggered_only = yes
	hide_window = yes

	immediate = {
		# Convert the provinces of pre-setup regions to ancient religions.
		# This will factor in the player's choice of whose's realms it should choose.
		
		any_playable_ruler = {
			limit = {
				or = {
					top_liege = { religion = catholic }
					not = { has_global_flag = emf_nwo_only_catholic }
				}
				or = {
					top_liege = { religion = orthodox }
					not = { has_global_flag = emf_nwo_only_orthodox }
				}
				or = {
					top_liege = { religion_group = christian }
					not = { has_global_flag = emf_nwo_only_christian }
				}
				or = {
					top_liege = { religion_group = muslim }
					not = { has_global_flag = emf_nwo_only_muslim }
				}
				or = {
					top_liege = { is_nomadic = no }
					not = { has_global_flag = emf_nwo_spare_nomad }
				}
			}
			character_event = { id = emf_nwo.23 } # Cleanup state from prior era
		}
		
		# Destroy all the titles that bind. Note that this step, by far, is the most time-consuming of
		# everything that we do in this customization decision.
		
		any_independent_ruler = {
			limit = {
				higher_tier_than = count
				is_landed = yes
				or = {
					religion = catholic
					not = { has_global_flag = emf_nwo_only_catholic }
				}
				or = {
					religion = orthodox
					not = { has_global_flag = emf_nwo_only_orthodox }
				}
				or = {
					religion_group = christian
					not = { has_global_flag = emf_nwo_only_christian }
				}
				or = {
					religion_group = muslim
					not = { has_global_flag = emf_nwo_only_muslim }
				}
				or = {
					not = { has_landed_title = k_papal_state }
					not = { has_global_flag = emf_nwo_spare_pope }
				}
				or = {
					is_merchant_republic = no
					not = { has_global_flag = emf_nwo_spare_mr }
				}
				or = {
					is_nomadic = no
					not = { has_global_flag = emf_nwo_spare_nomad }
				}
			}
			
			# Preserve holy orders, mercs, e_golden_horde, religious head titles,
			# etc., but otherwise, destroy all held duke-tier or higher titles
			# (including temporary titles). This step takes about 2sec on SWMH
			# on my machine at the time of this writing (that is, when it's done
			# for the whole world).
			
			# For some reason, execution of this step seems to be a bit faster
			# when we destroy all duke-tier titles before all king-tier before
			# all empire-tier for any given realm, so there's a bit of code
			# duplication here toward that end.
			
			any_title_under = {
				limit = {
					tier = duke
					or = {
						is_landless_type_title = no
						temporary = yes
					}
					or = {
						owner = {
							nor = {
								is_merchant_republic = yes
								any_liege = { is_merchant_republic = yes }
							}
						}
						not = { has_global_flag = emf_nwo_spare_mr }
					}
					or = {
						owner = {
							nor = {
								has_landed_title = k_papal_state
								any_liege = { has_landed_title = k_papal_state }
							}
						}
						not = { has_global_flag = emf_nwo_spare_pope }
					}
				}
				if = {
					limit = {
						has_global_flag = emf_nwo_duke
						has_global_flag = emf_nwo_retain_rulers
						owner = {
							capital_holding = { de_jure_liege_or_above = PREVPREV }
						}
					}
					owner = {
						set_character_flag = prev_prince
					}
				}
				unsafe_destroy_landed_title = THIS
			}
			any_title_under = { # Same as above, but for tier = king
				limit = {
					tier = king
					or = {
						and = {
							is_landless_type_title = no
							not = { clan = yes }
						}
						temporary = yes
					}
					or = {
						owner = {
							not = {
								is_merchant_republic = yes
								any_liege = { is_merchant_republic = yes }
							}
						}
						not = { has_global_flag = emf_nwo_spare_mr }
					}
					or = {
						owner = {
							not = {
								has_landed_title = k_papal_state
								any_liege = { has_landed_title = k_papal_state }
							}
						}
						not = { has_global_flag = emf_nwo_spare_pope }
					}
				}
				if = {
					limit = {
						has_global_flag = emf_nwo_king
						has_global_flag = emf_nwo_retain_rulers
						owner = {
							capital_holding = { de_jure_liege_or_above = PREVPREV }
						}
					}
					owner = {
						set_character_flag = prev_prince
					}
				}
				unsafe_destroy_landed_title = THIS
			}
			any_demesne_title = {
				limit = {
					tier = emperor
					or = {
						and = {
							is_landless_type_title = no
							is_nomadic = no
						}
						temporary = yes
					}
				}
				unsafe_destroy_landed_title = THIS
			}
		}
		
		# Now that de jure duke-tier or higher titles are destroyed, make any de jure title
		# structure changes to the map appropriate for NWO (since the old titles will not be
		# held any longer, if they need to be deactivated).
		
		# The duke-tier or higher rulers are going to be a problem. We need them to give up all their
		# land and return to floating around in CKII outer space as independents without earthly
		# connections. Exempt nomads.
		
		any_playable_ruler = {
			limit = {
				higher_tier_than = count
				is_landed = yes
				is_nomadic = no
				primary_title = { is_landless_type_title = yes }
				or = {
					top_liege = { religion = catholic }
					not = { has_global_flag = emf_nwo_only_catholic }
				}
				or = {
					top_liege = { religion = orthodox }
					not = { has_global_flag = emf_nwo_only_orthodox }
				}
				or = {
					top_liege = { religion_group = christian }
					not = { has_global_flag = emf_nwo_only_christian }
				}
				or = {
					top_liege = { religion_group = muslim }
					not = { has_global_flag = emf_nwo_only_muslim }
				}
				or = {
					not = { has_landed_title = k_papal_state }
					nor = {
						has_global_flag = emf_nwo_spare_pope
						has_global_flag = emf_nwo_spare_pope_only
					}
				}
				or = {
					and = {
						is_merchant_republic = no
						not = { any_liege = { is_merchant_republic = yes } }
					}
					not = { has_global_flag = emf_nwo_spare_mr }
				}
			}
			
			# Grant away all your count titles to new characters.
			character_event = { id = emf_nwo.30 }
			
			# And all your barony titles to appropriate characters (owner of their county).
			any_demesne_title = {
				limit = { tier = baron }
				dejure_liege_title = {
					owner = {
						grant_title_no_opinion = PREVPREV
					}
				}
			}
			
			# If your landless title was binding any vassals, they'll be freed in next step.
		}
		
		any_playable_ruler = {
			limit = {
				independent = no
				or = {
					top_liege = { religion = catholic }
					not = { has_global_flag = emf_nwo_only_catholic }
				}
				or = {
					top_liege = { religion = orthodox }
					not = { has_global_flag = emf_nwo_only_orthodox }
				}
				or = {
					top_liege = { religion_group = christian }
					not = { has_global_flag = emf_nwo_only_christian }
				}
				or = {
					top_liege = { religion_group = muslim }
					not = { has_global_flag = emf_nwo_only_muslim }
				}
				or = {
					not = { k_papal_state = { owner = { is_vassal_or_below = PREVPREV } } }
					not = { has_global_flag = emf_nwo_spare_pope }
				}
				or = {
					not = { any_liege = { is_merchant_republic = yes } }
					not = { has_global_flag = emf_nwo_spare_mr }
				}
				or = {
					top_liege = { is_nomadic = no }
					not = { has_global_flag = emf_nwo_spare_nomad }
				}
			}
			set_defacto_liege = THIS
		}
	
		# Now, there are only count-tier playable rulers (besides anybody excluded by customization
		# options). If nwo_spare_mr, merchant republics are all independent, and if nwo_spare_pope,
		# the Papacy is independent, and if nwo_spare_pope_only, the Papacy is independent but only
		# has 1 county personally held by the Pope and no vassals. Currently, just selecting all
		# count-tier independent rulers would be a shortcut for addressing those rulers affected
		# by New World Order.
		
		# These count-tier rulers should be reduced to their capital county, either by finding a
		# vassal baron to grant an excess county, granting to a courtier, or by generating a new
		# character to hold it. In general, all playable rulers in the New World Order "zone"
		# will have only 1 demesne county.
		
		if = {
			limit = { has_global_flag = emf_nwo_tiny_nomad }

			any_independent_ruler = {
				limit = {
					is_nomadic = yes
					or = {
						religion = catholic
						not = { has_global_flag = emf_nwo_only_catholic }
					}
					or = {
						religion = orthodox
						not = { has_global_flag = emf_nwo_only_orthodox }
					}
					or = {
						religion_group = christian
						not = { has_global_flag = emf_nwo_only_christian }
					}
					or = {
						religion_group = muslim
						not = { has_global_flag = emf_nwo_only_muslim }
					}
				}
				
				character_event = { id = emf_nwo.32 } # Grant excess counties to new, random characters (nomadic, recursive)
				character_event = { id = emf_nwo.31 } # Grant excess counties to new, random characters (recursive)
				
				# Seize/vassalize any non-de-jure baronies.
				character_event = { id = emf_nwo.21 }
			}
		}

		any_independent_ruler = {
			limit = {
				tier = count
				or = {
					religion = catholic
					not = { has_global_flag = emf_nwo_only_catholic }
				}
				or = {
					religion = orthodox
					not = { has_global_flag = emf_nwo_only_orthodox }
				}
				or = {
					religion_group = christian
					not = { has_global_flag = emf_nwo_only_christian }
				}
				or = {
					religion_group = muslim
					not = { has_global_flag = emf_nwo_only_muslim }
				}
			}
			
			character_event = { id = emf_nwo.31 } # Grant excess counties to new, random characters (recursive)
			
			# Seize/vassalize any non-de-jure baronies.
			character_event = { id = emf_nwo.21 }
		}
		
		# Now, a couple definitions:
		#   principality - a de jure title of tier determined by nwo_count/nwo_duke/nwo_king setting
		#   prince - character selected to rule a principality. they will be independent when done.
		#   princelings - any of the characters who could have been selected to be a prince.
		
		# Each princeling's principality is determined by their capital's location. When selecting a
		# prince, we require that the principality have no holder (or else it belongs to an excluded
		# realm). Thus, not every princeling will get a chance to be a prince. Those that don't will
		# still be treated as princes of their count-tier capital principality.
		
		# FOR DUKE-TIER AND KING-TIER PRINCIPALITIES:
		
		# For principality tiers greater than count-level, prince selection within the principality follows
		# this preference rule:
		#   ai = no
		#   feudal
		#   tribal
		#   republic
		#   *
		
		# If the option emf_nwo_retain_rulers is selected, we prefer the previous ruler of a given
		# principality if he exists (with preference still given to players unless they specifically
		# select otherwise).
		
		# Otherwise, prince selection is completely random. [ An alternate strategy would be
		# to pick princes that are most likely to succeed, but that's really not even
		# necessarily desired for New World Order. ]
		
		# Due to SWMH's lack of de jure empires in places, the 'Remove Ahistorical Empires' customization,
		# and generally the possibility of dynamic de jure kingdom titles on the map, we cannot work
		# through the principalities top-down through a list of de jure empire titles or even kingdom
		# titles.

		# Instead, we recurse over all princelings, selecting those that have an unheld principality, in the
		# order of our prince selection rules, and we make those random selections princes by granting them
		# the principality and transferring de jure princeling vassals to them.
		
		if = {
			limit = { has_global_flag = emf_nwo_duke }
			if = {
				limit = {
					has_global_flag = emf_nwo_retain_rulers
					not = { has_global_flag = emf_nwo_retain_rulers_not_me }
				}
				character_event = { id = emf_nwo.55 } # Select previous princes (duke) [recursive]
			}
			character_event = { id = emf_nwo.50 } # Select ai = no princes (duke) [recursive]
			if = {
				limit = { has_global_flag = emf_nwo_retain_rulers_not_me }
				character_event = { id = emf_nwo.55 } # Select previous princes (duke) [recursive]
			}
			character_event = { id = emf_nwo.51 } # Select feudal princes (duke) [recursive]
			character_event = { id = emf_nwo.52 } # Select tribal princes (duke) [recursive]
			character_event = { id = emf_nwo.53 } # Select republic princes (duke) [recursive]
			character_event = { id = emf_nwo.54 } # Select any princes (duke) [recursive]
		}
		
		if = {
			limit = { has_global_flag = emf_nwo_king }
			if = {
				limit = {
					has_global_flag = emf_nwo_retain_rulers
					not = { has_global_flag = emf_nwo_retain_rulers_not_me }
				}
				character_event = { id = emf_nwo.65 } # Select previous princes (king) [recursive]
			}
			character_event = { id = emf_nwo.60 } # Select ai = no princes (king) [recursive]
			if = {
				limit = { has_global_flag = emf_nwo_retain_rulers_not_me }
				character_event = { id = emf_nwo.65 } # Select previous princes (king) [recursive]
			}
			character_event = { id = emf_nwo.60 } # Select ai = no princes (king) [recursive]
			character_event = { id = emf_nwo.61 } # Select feudal princes (king) [recursive]
			character_event = { id = emf_nwo.62 } # Select tribal princes (king) [recursive]
			character_event = { id = emf_nwo.63 } # Select republic princes (king) [recursive]
			character_event = { id = emf_nwo.64 } # Select any princes (king) [recursive]
		}
		
		random_playable_ruler = {
			limit = { 
				has_global_flag = emf_nwo_generate_children 
				or = {
					and = {
						religion = catholic
						has_global_flag = emf_nwo_only_catholic
					}
					and = {
						religion = orthodox
						has_global_flag = emf_nwo_only_orthodox
					}
					and = {
						religion_group = christian
						has_global_flag = emf_nwo_only_christian
					}
					and = {
						religion_group = muslim
						has_global_flag = emf_nwo_only_muslim
					}
					NOR = {
						has_global_flag = emf_nwo_only_catholic
						has_global_flag = emf_nwo_only_orthodox
						has_global_flag = emf_nwo_only_christian
						has_global_flag = emf_nwo_only_muslim
					}
				}
			}
			character_event = { id = emf_nwo.102 }
		}
		
		any_independent_ruler = {
			limit = {
				has_global_flag = emf_nwo_ancient_religions
				or = {
					and = {
						religion = catholic
						has_global_flag = emf_nwo_only_catholic
					}
					and = {
						religion = orthodox
						has_global_flag = emf_nwo_only_orthodox
					}
					and = {
						religion_group = christian
						has_global_flag = emf_nwo_only_christian
					}
					and = {
						religion_group = muslim
						has_global_flag = emf_nwo_only_muslim
					}
					NOR = {
						has_global_flag = emf_nwo_only_catholic
						has_global_flag = emf_nwo_only_orthodox
						has_global_flag = emf_nwo_only_christian
						has_global_flag = emf_nwo_only_muslim
					}
				}
			}
			any_realm_province = {
				if = {
					limit = {
						OR = {
							has_game_rule = {
								name = ancient_religions
								value = regions
							}
							has_game_rule = {
								name = ancient_religions
								value = no
							}
						}
					}
					emf_nwo_generate_ancient_religion_provinces = yes
				}				
				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
				}
				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
					}
				}
				if = {
					limit = {
						NOT = {
							has_game_rule = {
								name = ancient_religions
								value = cultures_characters
							}
						}
					}
					capital_scope = {
						reverse_religion = PREV #change religion of independent ruler based on their capital
					}
					any_courtier = {
						religion = PREV #match courtier religion to ruler religion
					}
					any_realm_lord = {
						religion = PREV #match realm lord religion to ruler religion
						any_courtier = {
							religion = PREV #match courtier religion to realm lord religion
						}
					}
				}
				if = {
					limit = {
						has_game_rule = {
							name = ancient_religions
							value = cultures_characters
						}
					}
					capital_scope = {
						emf_nwo_generate_ancient_religion_characters = yes #change religion of independent ruler based on their culture
					}
					any_courtier = {
						emf_nwo_generate_ancient_religion_characters = yes #change courtier religion based on their culture
					}
					any_realm_lord = {
						emf_nwo_generate_ancient_religion_characters = yes #change realm lord religion based on their culture
						any_courtier = {
							emf_nwo_generate_ancient_religion_characters = yes #change courtier religion based on their culture
						}
					}
				}
			}
		}
		
		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
		}
		if = {
			limit = {
				NOT = {
					has_game_rule = {
						name = ancient_religions
						value = cultures_characters
					}
				}
			}
			set_mercenaries_to_ar_effect = yes # Set mercenaries to ancient religions based on their capital
		}
		
		# Cleanup option global flags (this allows the player to re-invoke the customization decision if
		# they want to change options)
		clr_global_flag = emf_nwo_count
		clr_global_flag = emf_nwo_duke
		clr_global_flag = emf_nwo_king
		clr_global_flag = emf_nwo_only_catholic
		clr_global_flag = emf_nwo_only_orthodox
		clr_global_flag = emf_nwo_only_christian
		clr_global_flag = emf_nwo_only_muslim
		clr_global_flag = emf_nwo_retain_rulers
		clr_global_flag = emf_nwo_retain_rulers_not_me
		clr_global_flag = emf_nwo_spare_mr
		clr_global_flag = emf_nwo_spare_pope
		clr_global_flag = emf_nwo_spare_pope_only
		clr_global_flag = emf_nwo_spare_nomad
		clr_global_flag = emf_nwo_tiny_nomad
		clr_global_flag = emf_nwo_generate_children
		
		# Do mark that this was an NWO game, however.
		set_global_flag = emf_nwo
		
		#ASK Player if they want to run NWO again.
		FROM = { letter_event = { id = emf_nwo.99} }
	}
}


# emf_nwo.21 [Princeling]
#
# Seize/vassalize any non-de-jure baronies.
character_event = {
	id = emf_nwo.21
	hide_window = yes
	is_triggered_only = yes
	
	immediate = {
		capital_scope = {
			county = {
				# First, try to vassalize non-de-jure barons
				any_direct_de_jure_vassal_title = {
					limit = {
						owner = {
							tier = baron
							not = { vassal_of = ROOT }
							not = {
								any_demesne_title = {
									not = {
										de_jure_liege = PREVPREVPREV # County
									}
								}
							}
						}
					}
					owner = {
						set_defacto_liege = ROOT
					}
				}
				# Then seize the rest
				any_direct_de_jure_vassal_title = {
					limit = {
						owner = {
							nor = {
								character = ROOT
								is_liege_or_above = ROOT
							}
						}
					}
					grant_title_no_opinion = ROOT
				}
			}
		}
	}
}


# emf_nwo.22 [Prince]
#
# Vassalize the de jure vassal princelings of my principality.
character_event = {
	id = emf_nwo.22
	hide_window = yes
	is_triggered_only = yes
	
	immediate = {
		primary_title = {
			any_de_jure_vassal = {
				limit = {
					tier = count
					independent = yes
					or = {
						religion = catholic
						not = { has_global_flag = emf_nwo_only_catholic }
					}
					or = {
						religion = orthodox
						not = { has_global_flag = emf_nwo_only_orthodox }
					}
					or = {
						religion_group = christian
						not = { has_global_flag = emf_nwo_only_christian }
					}
					or = {
						religion_group = muslim
						not = { has_global_flag = emf_nwo_only_muslim }
					}
				}
				set_defacto_liege = ROOT
			}
		}
		
		if = {
			limit = { tier = king }
			
			# Grant the prince his capital duchy
			capital_scope = {
				duchy = {
					if = {
						limit = { has_holder = no } # Could be held by an unaffected character group
						grant_title = ROOT
					}
				}
			}
			
			# In order to surmount vassal limit issues for large kingdoms (and be more neat),
			# we auto-assign all the unheld de jure duchies of our kingdom to those rulers who
			# hold the nominal capital of the duchy. [We could've just given them to the prince
			# to distribute, but that would result in an overly friendly situation-- and not be
			# automatic. Downside: vassal dukes will tend to be the same for a given start date
			# and de jure kingdom-- /tend/, that is, because they may not be a historical
			# character. ]
			
			any_vassal = {
				limit = {
					tier = count
					capital_scope = {
						duchy = {
							has_holder = no # Not held by some unaffected/protected character class or prince
							capital_scope = { # Nominal capital province of duchy title
								province = PREVPREV # Character capital
							}
						}
					}
				}
				
				# Hand it over (from outer space, so they'll go independent).
				capital_scope = {
					duchy = {
						grant_title = PREVPREV
					}
				}
				
				set_defacto_liege = ROOT # Need to remind them we're still their liege

				# Transfer de jure vassals to the new duke
				primary_title = {
					any_de_jure_vassal = {
						limit = {
							tier = count
							vassal_of = ROOT
						}
						set_defacto_liege = PREVPREV
					}
				}
			}
			
			# Due to an SWMH issue (lack of defined and valid de jure capitals for many duchies), we
			# need to sweep over any de jure duchy titles not assigned above and give them to the prince
			# for immediate distribution.
			
			primary_title = {
				any_direct_de_jure_vassal_title = {
					limit = { has_holder = no }
					grant_title = ROOT
				}
			}
		}
	}
}


# emf_nwo.23 [Playable Ruler]
#
# Cleanup state from prior era.
character_event = {
	id = emf_nwo.23
	hide_window = yes
	is_triggered_only = yes
	
	immediate = {
		# End wars from the prior era
		any_war = {
			limit = {
				or = {
					any_attacker = { character = ROOT }
					any_defender = { character = ROOT }
				}
			}
			end_war = invalid
		}
		
		any_suzerain = {
			remove_tributary = ROOT
		}

		# Disband any event troops (all spawns have been overridden in character history to
		# be marked with the earmark start_troops if they weren't earmarked)
		disband_event_forces = start_troops
		disband_event_forces = norman_byzantine_war
		disband_event_forces = williams_invasion_fleet
		disband_event_forces = norwegian_invasion
		disband_event_forces = saxon_defense_force
			
		# Remove now-irrelevant personal claims they may have (we're changing history here)
		any_claim = {
			limit = {
				or = {
					not = { tier = count } # Allow county claims to remain
					not = { has_global_flag = emf_nwo } # But not on the first run of the customization decision (imprecise but OK)
				}
			}
			remove_claim = PREV
		}
		any_courtier = {
			any_claim = { # Wipe out all courtier claims
				remove_claim = PREV
			}
		}
		
		# Move our capital around for fun and profit (exclude players) ...
		
		if = {
			limit = {
				ai = yes
				is_nomadic = no
			}
			
			# First, potentially relocate based upon province settlement count.
			character_event = { id = emf_nwo.24 }
			
			# Second, if we hold the nominal capital province of the duchy within
			# which our capital is currently located, switch to there (even if it's
			# a downgrade in settlement count).
			capital_scope = {
				duchy = {
					capital_scope = { # Nominal capital, if defined
						if = {
							limit = { ROOT = { owns = PREV } }
							ROOT = { capital = PREV }
						}
					}
				}
			}
		}
	}
}


# emf_nwo.24 [Princeling]
#
# Relocate capital to demesne province with highest number of settlements, but only move if it's actually
# an improvement.
character_event = {
	id = emf_nwo.24
	hide_window = yes
	is_triggered_only = yes
	
	immediate = {
		random_demesne_title = {
			limit = {
				tier = count
				location = { num_of_settlements = 7 }
				ROOT = { capital_scope = { not = { num_of_settlements = 7 } } }
			}
			ROOT = { capital = PREV }
			break = yes
		}
		random_demesne_title = {
			limit = {
				tier = count
				location = { num_of_settlements = 6 }
				ROOT = { capital_scope = { not = { num_of_settlements = 6 } } }
			}
			ROOT = { capital = PREV }
			break = yes
		}
		random_demesne_title = {
			limit = {
				tier = count
				location = { num_of_settlements = 5 }
				ROOT = { capital_scope = { not = { num_of_settlements = 5 } } }
			}
			ROOT = { capital = PREV }
			break = yes
		}
		random_demesne_title = {
			limit = {
				tier = count
				location = { num_of_settlements = 4 }
				ROOT = { capital_scope = { not = { num_of_settlements = 4 } } }
			}
			ROOT = { capital = PREV }
			break = yes
		}
		random_demesne_title = {
			limit = {
				tier = count
				location = { num_of_settlements = 3 }
				ROOT = { capital_scope = { not = { num_of_settlements = 3 } } }
			}
			ROOT = { capital = PREV }
			break = yes
		}
		random_demesne_title = {
			limit = {
				tier = count
				location = { num_of_settlements = 2 }
				ROOT = { capital_scope = { not = { num_of_settlements = 2 } } }
			}
			ROOT = { capital = PREV }
			break = yes
		}
	}
}


# emf_nwo.30 [Ruler of a landless title that's also landed]
#
# Tail-recursive event which grants away all count titles to new characters.
character_event = {
	id = emf_nwo.30
	hide_window = yes
	is_triggered_only = yes
	
	trigger = {
		primary_title = {
			is_landless_type_title = yes
		}
		num_of_count_titles = 1
	}
	
	immediate = {
		random_demesne_title = {
			limit = {
				tier = count
			}
			location = {
				ROOT = {
					emf_nwo_create_male_ruler_effect = yes
					new_character = {
						grant_title_no_opinion = PREVPREVPREV # County
						set_defacto_liege = THIS
					}
				}
			}
		}
		
		repeat_event = { id = emf_nwo.30 }
	}
}

# emf_nwo.31 [COUNT+ Ruler]
#
# Grant excess demesne counties to new characters. [Recursive]
character_event = {
	id = emf_nwo.31
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		num_of_count_titles = 2
	}
	
	immediate = {
		random_demesne_title = {
			limit = {
				tier = count
				location = {
					is_capital = no
				}
			}
			location = {
				ROOT = {
					random_list = {
						50 = { emf_nwo_create_male_ruler_effect = yes }
						50 = {
							modifier = {
								factor = 0
								or = {
									not = { has_global_flag = emf_gender_eq_law }
									PREV = { religion_group = muslim }
									PREV = {
										capital_holding = {
											or = {
												is_theocracy = yes
												is_republic = yes
											}
										}
									}
								}
							}
							emf_nwo_create_female_ruler_effect = yes
						}
					}
					new_character = {
						grant_title_no_opinion = PREVPREVPREV
						set_defacto_liege = THIS
					}
				} # END: ROOT
			} # END: location

			if = {
				limit = {
					not = { has_global_flag = emf_nwo_count }
				}
				add_pressed_claim = ROOT # Strong, inheritable claim on county
			}
			
		}
		
		repeat_event = { id = emf_nwo.31 }
	}
}

# emf_nwo.32 [Nomadic Ruler]
#
# Grant excess demesne counties to new clans, where possible. [Recursive]
character_event = {
	id = emf_nwo.32
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_demesne_title = {
			tier = count
			location = {
				is_capital = no
				has_empty_holding = yes
			}
		}
	}
	
	immediate = {
		random_demesne_title = {
			limit = {
				tier = count
				location = {
					is_capital = no
					has_empty_holding = yes
				}
			}
			location = {
				ROOT = {
					emf_nwo_create_male_ruler_effect = yes
					new_character = {
						grant_title_no_opinion = PREVPREVPREV
						set_government_type = nomadic_government
						set_defacto_liege = THIS
					}
				} # END: ROOT
			} # END: location
		}
		
		repeat_event = { id = emf_nwo.32 }
	}
}


# emf_nwo.50 [Isis]
#
# Select prince (ai = no) for duchy principality. [Recursive]
character_event = {
	id = emf_nwo.50
	
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_independent_ruler = {
			ai = no
			tier = count
			capital_scope = { duchy = { has_holder = no } }
			or = {
				religion = catholic
				not = { has_global_flag = emf_nwo_only_catholic }
			}
			or = {
				religion_group = christian
				not = { has_global_flag = emf_nwo_only_christian }
			}
			or = {
				religion_group = muslim
				not = { has_global_flag = emf_nwo_only_muslim }
			}
		}
	}
	
	immediate = {
		random_independent_ruler = {
			limit = {
				ai = no
				tier = count
				capital_scope = { duchy = { has_holder = no } }
				or = {
					religion = catholic
					not = { has_global_flag = emf_nwo_only_catholic }
				}
				or = {
					religion_group = christian
					not = { has_global_flag = emf_nwo_only_christian }
				}
				or = {
					religion_group = muslim
					not = { has_global_flag = emf_nwo_only_muslim }
				}
			}
			capital_scope = {
				duchy = {
					grant_title = PREVPREV
				}
			}
			character_event = { id = emf_nwo.22 }
		}
		
		repeat_event = { id = emf_nwo.50 }
	}
}


# emf_nwo.51 [Isis]
#
# Select prince (is_feudal = yes) for duchy principality. [Recursive]
character_event = {
	id = emf_nwo.51
	
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_independent_ruler = {
			is_feudal = yes
			capital_holding = { not = { holding_type = city } } # Weird patrician bug
			tier = count
			capital_scope = { duchy = { has_holder = no } }
			or = {
				religion = catholic
				not = { has_global_flag = emf_nwo_only_catholic }
			}
			or = {
				religion_group = christian
				not = { has_global_flag = emf_nwo_only_christian }
			}
			or = {
				religion_group = muslim
				not = { has_global_flag = emf_nwo_only_muslim }
			}
		}
	}
	
	immediate = {
		random_independent_ruler = {
			limit = {
				is_feudal = yes
				tier = count
				capital_holding = { not = { holding_type = city } } # Weird patrician bug
				capital_scope = { duchy = { has_holder = no } }
				or = {
					religion = catholic
					not = { has_global_flag = emf_nwo_only_catholic }
				}
				or = {
					religion_group = christian
					not = { has_global_flag = emf_nwo_only_christian }
				}
				or = {
					religion_group = muslim
					not = { has_global_flag = emf_nwo_only_muslim }
				}
			}
			capital_scope = {
				duchy = {
					grant_title = PREVPREV
				}
			}
			character_event = { id = emf_nwo.22 }
		}
		
		repeat_event = { id = emf_nwo.51 }
	}
}


# emf_nwo.52 [Isis]
#
# Select prince (is_tribal = yes) for duchy principality. [Recursive]
character_event = {
	id = emf_nwo.52
	
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_independent_ruler = {
			is_tribal = yes
			tier = count
			capital_holding = { not = { holding_type = city } } # Weird patrician bug
			capital_scope = { duchy = { has_holder = no } }
			or = {
				religion = catholic
				not = { has_global_flag = emf_nwo_only_catholic }
			}
			or = {
				religion_group = christian
				not = { has_global_flag = emf_nwo_only_christian }
			}
			or = {
				religion_group = muslim
				not = { has_global_flag = emf_nwo_only_muslim }
			}
		}
	}
	
	immediate = {
		random_independent_ruler = {
			limit = {
				is_tribal = yes
				tier = count
				capital_holding = { not = { holding_type = city } } # Weird patrician bug
				capital_scope = { duchy = { has_holder = no } }
				or = {
					religion = catholic
					not = { has_global_flag = emf_nwo_only_catholic }
				}
				or = {
					religion_group = christian
					not = { has_global_flag = emf_nwo_only_christian }
				}
				or = {
					religion_group = muslim
					not = { has_global_flag = emf_nwo_only_muslim }
				}
			}
			capital_scope = {
				duchy = {
					grant_title = PREVPREV
				}
			}
			character_event = { id = emf_nwo.22 }
		}
		
		repeat_event = { id = emf_nwo.52 }
	}
}


# emf_nwo.53 [Isis]
#
# Select prince (is_republic = yes) for duchy principality. [Recursive]
character_event = {
	id = emf_nwo.53
	
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_independent_ruler = {
			is_republic = yes
			tier = count
			capital_scope = { duchy = { has_holder = no } }
			or = {
				religion = catholic
				not = { has_global_flag = emf_nwo_only_catholic }
			}
			or = {
				religion_group = christian
				not = { has_global_flag = emf_nwo_only_christian }
			}
			or = {
				religion_group = muslim
				not = { has_global_flag = emf_nwo_only_muslim }
			}
		}
	}
	
	immediate = {
		random_independent_ruler = {
			limit = {
				is_republic = yes
				tier = count
				capital_scope = { duchy = { has_holder = no } }
				or = {
					religion = catholic
					not = { has_global_flag = emf_nwo_only_catholic }
				}
				or = {
					religion_group = christian
					not = { has_global_flag = emf_nwo_only_christian }
				}
				or = {
					religion_group = muslim
					not = { has_global_flag = emf_nwo_only_muslim }
				}
			}
			capital_scope = {
				duchy = {
					grant_title = PREVPREV
				}
			}
			character_event = { id = emf_nwo.22 }
		}
		
		repeat_event = { id = emf_nwo.53 }
	}
}


# emf_nwo.54 [Isis]
#
# Select prince (any) for duchy principality. [Recursive]
character_event = {
	id = emf_nwo.54
	
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_independent_ruler = {
			tier = count
			capital_scope = { duchy = { has_holder = no } }
			or = {
				religion = catholic
				not = { has_global_flag = emf_nwo_only_catholic }
			}
			or = {
				religion_group = christian
				not = { has_global_flag = emf_nwo_only_christian }
			}
			or = {
				religion_group = muslim
				not = { has_global_flag = emf_nwo_only_muslim }
			}
		}
	}
	
	immediate = {
		random_independent_ruler = {
			limit = {
				tier = count
				capital_scope = { duchy = { has_holder = no } }
				or = {
					religion = catholic
					not = { has_global_flag = emf_nwo_only_catholic }
				}
				or = {
					religion_group = christian
					not = { has_global_flag = emf_nwo_only_christian }
				}
				or = {
					religion_group = muslim
					not = { has_global_flag = emf_nwo_only_muslim }
				}
			}
			capital_scope = {
				duchy = {
					grant_title = PREVPREV
				}
			}
			character_event = { id = emf_nwo.22 }
		}
		
		repeat_event = { id = emf_nwo.54 }
	}
}


# emf_nwo.55 [Isis]
#
# Select previous prince for duchy principality. [Recursive]
character_event = {
	id = emf_nwo.55
	
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_independent_ruler = {
			tier = count
			capital_scope = { duchy = { has_holder = no } }
			has_character_flag = prev_prince
		}
	}
	
	immediate = {
		random_independent_ruler = {
			limit = {
				tier = count
				capital_scope = { duchy = { has_holder = no } }
				has_character_flag = prev_prince
			}
			capital_scope = {
				duchy = {
					grant_title = PREVPREV
				}
			}
			clr_character_flag = prev_prince
			character_event = { id = emf_nwo.22 }
		}
		
		repeat_event = { id = emf_nwo.55 }
	}
}


# emf_nwo.60 [Isis]
#
# Select prince (ai = no) for kingdom principality. [Recursive]
character_event = {
	id = emf_nwo.60
	
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_independent_ruler = {
			ai = no
			tier = count
			capital_scope = { kingdom = { has_holder = no } }
			or = {
				religion = catholic
				not = { has_global_flag = emf_nwo_only_catholic }
			}
			or = {
				religion_group = christian
				not = { has_global_flag = emf_nwo_only_christian }
			}
			or = {
				religion_group = muslim
				not = { has_global_flag = emf_nwo_only_muslim }
			}
		}
	}
	
	immediate = {
		random_independent_ruler = {
			limit = {
				ai = no
				tier = count
				capital_scope = { kingdom = { has_holder = no } }
				or = {
					religion = catholic
					not = { has_global_flag = emf_nwo_only_catholic }
				}
				or = {
					religion_group = christian
					not = { has_global_flag = emf_nwo_only_christian }
				}
				or = {
					religion_group = muslim
					not = { has_global_flag = emf_nwo_only_muslim }
				}
			}
			capital_scope = {
				kingdom = {
					grant_title = PREVPREV
				}
			}
			character_event = { id = emf_nwo.22 }
		}
		
		repeat_event = { id = emf_nwo.60 }
	}
}


# emf_nwo.61 [Isis]
#
# Select prince (is_feudal = yes) for kingdom principality. [Recursive]
character_event = {
	id = emf_nwo.61
	
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_independent_ruler = {
			is_feudal = yes
			tier = count
			capital_holding = { not = { holding_type = city } } # Weird patrician bug
			capital_scope = { kingdom = { has_holder = no } }
			or = {
				religion = catholic
				not = { has_global_flag = emf_nwo_only_catholic }
			}
			or = {
				religion_group = christian
				not = { has_global_flag = emf_nwo_only_christian }
			}
			or = {
				religion_group = muslim
				not = { has_global_flag = emf_nwo_only_muslim }
			}
		}
	}
	
	immediate = {
		random_independent_ruler = {
			limit = {
				is_feudal = yes
				tier = count
				capital_holding = { not = { holding_type = city } } # Weird patrician bug
				capital_scope = { kingdom = { has_holder = no } }
				or = {
					religion = catholic
					not = { has_global_flag = emf_nwo_only_catholic }
				}
				or = {
					religion_group = christian
					not = { has_global_flag = emf_nwo_only_christian }
				}
				or = {
					religion_group = muslim
					not = { has_global_flag = emf_nwo_only_muslim }
				}
			}
			capital_scope = {
				kingdom = {
					grant_title = PREVPREV
				}
			}
			character_event = { id = emf_nwo.22 }
		}
		
		repeat_event = { id = emf_nwo.61 }
	}
}


# emf_nwo.62 [Isis]
#
# Select prince (is_tribal = yes) for kingdom principality. [Recursive]
character_event = {
	id = emf_nwo.62
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_independent_ruler = {
			is_tribal = yes
			tier = count
			capital_holding = { not = { holding_type = city } } # Weird patrician bug
			capital_scope = { kingdom = { has_holder = no } }
			or = {
				religion = catholic
				not = { has_global_flag = emf_nwo_only_catholic }
			}
			or = {
				religion_group = christian
				not = { has_global_flag = emf_nwo_only_christian }
			}
			or = {
				religion_group = muslim
				not = { has_global_flag = emf_nwo_only_muslim }
			}
		}
	}
	
	immediate = {
		random_independent_ruler = {
			limit = {
				is_tribal = yes
				tier = count
				capital_holding = { not = { holding_type = city } } # Weird patrician bug
				capital_scope = { kingdom = { has_holder = no } }
				or = {
					religion = catholic
					not = { has_global_flag = emf_nwo_only_catholic }
				}
				or = {
					religion_group = christian
					not = { has_global_flag = emf_nwo_only_christian }
				}
				or = {
					religion_group = muslim
					not = { has_global_flag = emf_nwo_only_muslim }
				}
			}
			capital_scope = {
				kingdom = {
					grant_title = PREVPREV
				}
			}
			character_event = { id = emf_nwo.22 }
		}
		
		repeat_event = { id = emf_nwo.62 }
	}
}


# emf_nwo.63 [Isis]
#
# Select prince (is_republic = yes) for kingdom principality. [Recursive]
character_event = {
	id = emf_nwo.63
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_independent_ruler = {
			is_republic = yes
			tier = count
			capital_scope = { kingdom = { has_holder = no } }
			or = {
				religion = catholic
				not = { has_global_flag = emf_nwo_only_catholic }
			}
			or = {
				religion_group = christian
				not = { has_global_flag = emf_nwo_only_christian }
			}
			or = {
				religion_group = muslim
				not = { has_global_flag = emf_nwo_only_muslim }
			}
		}
	}
	
	immediate = {
		random_independent_ruler = {
			limit = {
				is_republic = yes
				tier = count
				capital_scope = { kingdom = { has_holder = no } }
				or = {
					religion = catholic
					not = { has_global_flag = emf_nwo_only_catholic }
				}
				or = {
					religion_group = christian
					not = { has_global_flag = emf_nwo_only_christian }
				}
				or = {
					religion_group = muslim
					not = { has_global_flag = emf_nwo_only_muslim }
				}
			}
			capital_scope = {
				kingdom = {
					grant_title = PREVPREV
				}
			}
			character_event = { id = emf_nwo.22 }
		}
		
		repeat_event = { id = emf_nwo.63 }
	}
}


# emf_nwo.64 [Isis]
#
# Select prince (any) for kingdom principality. [Recursive]
character_event = {
	id = emf_nwo.64
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_independent_ruler = {
			tier = count
			capital_scope = { kingdom = { has_holder = no } }
			or = {
				religion = catholic
				not = { has_global_flag = emf_nwo_only_catholic }
			}
			or = {
				religion_group = christian
				not = { has_global_flag = emf_nwo_only_christian }
			}
			or = {
				religion_group = muslim
				not = { has_global_flag = emf_nwo_only_muslim }
			}
		}
	}
	
	immediate = {
		random_independent_ruler = {
			limit = {
				tier = count
				capital_scope = { kingdom = { has_holder = no } }
				or = {
					religion = catholic
					not = { has_global_flag = emf_nwo_only_catholic }
				}
				or = {
					religion_group = christian
					not = { has_global_flag = emf_nwo_only_christian }
				}
				or = {
					religion_group = muslim
					not = { has_global_flag = emf_nwo_only_muslim }
				}
			}
			capital_scope = {
				kingdom = {
					grant_title = PREVPREV
				}
			}
			character_event = { id = emf_nwo.22 }
		}
		
		repeat_event = { id = emf_nwo.64 }
	}
}


# emf_nwo.65 [Isis]
#
# Select previous prince for kingdom principality. [Recursive]
character_event = {
	id = emf_nwo.65
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_independent_ruler = {
			tier = count
			capital_scope = { kingdom = { has_holder = no } }
			has_character_flag = prev_prince
		}
	}
	
	immediate = {
		random_independent_ruler = {
			limit = {
				tier = count
				capital_scope = { kingdom = { has_holder = no } }
				has_character_flag = prev_prince
			}
			capital_scope = {
				kingdom = {
					grant_title = PREVPREV
				}
			}
			clr_character_flag = prev_prince
			character_event = { id = emf_nwo.22 }
		}
		
		repeat_event = { id = emf_nwo.65 }
	}
}

# emf_nwo.99 [Player]
#
# ask player if they want to run NWO again
letter_event = {
	id = emf_nwo.99
	desc = emf_nwo.99.desc
	
	is_triggered_only = yes
	
	option = {
		name = YES
		hidden_tooltip = {
			d_miaphysite = { owner = { character_event = { id = emf_nwo.0 } } }
		}
	}
	option = {
		name = NO
	}
}