
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 = {
			limit = { is_multiplayer_host_character = yes }
			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

		log = "emf_nwo: Independent Counts (emf_nwo_count)"
		set_global_flag = emf_nwo_count
	}

	option = {
		name = emf_nwo.1.opt.b # De Jure Dukes

		log = "emf_nwo: De Jure Dukes (emf_nwo_duke)"
		set_global_flag = emf_nwo_duke
	}

	option = {
		name = emf_nwo.1.opt.c # De Jure Kings

		log = "emf_nwo: De Jure Kings (emf_nwo_king)"
		set_global_flag = emf_nwo_king
	}

	option = {
		name = CANCEL
	}

	after = {
		hidden_effect = {
			FROM = {
				if = {
					limit = { has_global_flag = emf_nwo_count }
					character_event = { id = emf_nwo.2 }
				}
				else_if = {
					limit = {
						OR = {
							has_global_flag = emf_nwo_duke
							has_global_flag = emf_nwo_king
						}
					}

					character_event = { id = emf_nwo.8 }
				}
			}
		}
	}
}


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

	is_triggered_only = yes
	hide_window = yes

	immediate = {
		FROM = {
			if = {
				limit = { has_dlc = "Horse Lords" }
				letter_event = { id = emf_nwo.13 }
			}
			else = {
				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
	}

	option = {
		name = emf_nwo.3.opt.b # Only Catholic Realms

		trigger = {
			has_global_flag = schism_happened
		}

		log = "emf_nwo: Excluding non-Catholic... (emf_nwo_only_catholic)"
		set_global_flag = emf_nwo_only_catholic
	}

	option = {
		name = emf_nwo.3.opt.c # Only Orthodox Realms

		trigger = {
			has_global_flag = schism_happened
		}

		log = "emf_nwo: Excluding non-Orthodox... (emf_nwo_only_orthodox)"
		set_global_flag = emf_nwo_only_orthodox
	}

	option = {
		name = emf_nwo.3.opt.d # Only Christian Realms

		log = "emf_nwo: Excluding non-Christian... (emf_nwo_only_christian)"
		set_global_flag = emf_nwo_only_christian
	}

	option = {
		name = emf_nwo.3.opt.e # Only Muslim Realms

		log = "emf_nwo: Excluding non-Muslim... (emf_nwo_only_muslim)"
		set_global_flag = emf_nwo_only_muslim
	}

	after = {
		hidden_effect = {
			FROM = {
				if = {
					limit = { has_global_flag = emf_nwo_only_muslim }
					character_event = { id = emf_nwo.6 } # Skip asking about the Pope
				}
				else = {
					character_event = { id = emf_nwo.4 } # Ask about the Pope
				}
			}
		}
	}
}


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

	is_triggered_only = yes
	hide_window = yes

	immediate = {
		FROM = {
			if = {
				limit = { has_global_flag = schism_happened }
				letter_event = { id = emf_nwo.5 }
			}
			else = {
				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

		log = "emf_nwo: Excluding Papacy... (emf_nwo_spare_pope)"
		set_global_flag = emf_nwo_spare_pope
	}

	option = {
		name = emf_nwo.5.opt.b # Yes, but only let the Pope have Rome

		log = "emf_nwo: Excluding Pope... (emf_nwo_spare_pope_only)"
		set_global_flag = emf_nwo_spare_pope_only
	}

	option = {
		name = emf_nwo.5.opt.c # No, the Pope doesn't need land.
	}

	after = {
		hidden_effect = {
			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

		log = "emf_nwo: Excluding merchant republics... (emf_nwo_spare_mr)"
		set_global_flag = emf_nwo_spare_mr
	}

	option = {
		name = NO

		trigger = {
			is_patrician = no
		}
	}

	after = {
		hidden_effect = {
			FROM = {
				if = {
					limit = { has_global_flag = emf_nwo_king }
					character_event = { id = emf_nwo.100 }
				}
				else = {
					character_event = { id = emf_nwo.10 } # Rapid Conquest?
				}
			}
		}
	}
}


# 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

	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 }
	}

	is_triggered_only = yes

	option = {
		name = emf_nwo.9.opt.a # Yes, but still make players de jure dukes/kings

		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
	}

	option = {
		name = emf_nwo.9.opt.b # Yes, even if that makes me a vassal

		log = "emf_nwo: Retaining previous rulers... (emf_nwo_retain_rulers)"
		set_global_flag = emf_nwo_retain_rulers
	}

	option = {
		name = NO
	}

	after = {
		hidden_effect = {
			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

		set_global_flag = emf_rapid_conquest
	}

	option = {
		name = NO

		clr_global_flag = emf_rapid_conquest
	}

	after = {
		hidden_effect = {
			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
	}

	option = {
		name = emf_nwo.13.opt.b # Each clan gets a single province

		hidden_effect = {
			log = "emf_nwo: Giving each nomadic clan a single province... (emf_nwo_tiny_nomad)"
			set_global_flag = emf_nwo_tiny_nomad
		}
	}

	option = {
		name = emf_nwo.13.opt.c # Leave them alone

		hidden_effect = {
			log = "emf_nwo: Excluding nomads... (emf_nwo_spare_nomad)"
			set_global_flag = emf_nwo_spare_nomad
		}
	}

	after = {
		hidden_effect = {
			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 = {
		FROM = {
			if = {
				limit = { has_dlc = "The Old Gods" }
				letter_event = { id = emf_nwo.101 } # Ask about AR
			}
			else = {
				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 }
	}

	option = {
		name = emf_nwo.101.opt.c # Ancient Religions

		custom_tooltip = {
			text = emf_nwo.101.opt.c.hover_ctt
			set_global_flag = emf_nwo_ancient_religions
		}
	}

	after = {
		hidden_effect = {
			FROM = { character_event = { id = emf_nwo.104 } } # Bounce
		}
	}
}

# 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
			set_global_flag = emf_nwo_generate_children
		}
	}

	option = {
		name = emf_nwo.103.opt.b # No

		custom_tooltip = { text = emf_nwo.103.opt.b.hover_ctt }
	}

	after = {
		hidden_effect = {
			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 = {
		# This will factor in the player's choice of whose's realms it should choose.
		any_playable_ruler = {
			limit = {
				trigger_if = {
					limit = { has_global_flag = emf_nwo_only_catholic }
					top_liege = { religion = catholic }
				}
				trigger_else_if = {
					limit = { has_global_flag = emf_nwo_only_orthodox }
					top_liege = { religion = orthodox }
				}
				trigger_else_if = {
					limit = { has_global_flag = emf_nwo_only_christian }
					top_liege = { religion_group = christian }
				}
				trigger_else_if = {
					limit = { has_global_flag = emf_nwo_only_muslim }
					top_liege = { religion_group = muslim }
				}

				trigger_if = {
					limit = { top_liege = { is_nomadic = yes } }
					NOT = { has_global_flag = emf_nwo_spare_nomad }
				}
			}

			## Clean-up state from prior era
			save_event_target_as = unclean_ruler

			# End wars from the prior era
			any_war = {
				end_war = invalid
			}

			any_suzerain = {
				remove_tributary = PREV
			}

			# 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 = {
					NAND = {
						tier = COUNT # Allow county claims to remain
						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.
				random_demesne_province = {
					limit = { owned_by = event_target:unclean_ruler }

					preferred_limit = {
						num_of_settlements >= 7
						event_target:unclean_ruler = { capital_scope = { num_of_settlements < 7 } }
					}

					preferred_limit = {
						num_of_settlements >= 6
						event_target:unclean_ruler = { capital_scope = { num_of_settlements < 6 } }
					}

					preferred_limit = {
						num_of_settlements >= 5
						event_target:unclean_ruler = { capital_scope = { num_of_settlements < 5 } }
					}

					preferred_limit = {
						num_of_settlements >= 4
						event_target:unclean_ruler = { capital_scope = { num_of_settlements < 4 } }
					}

					preferred_limit = {
						num_of_settlements >= 3
						event_target:unclean_ruler = { capital_scope = { num_of_settlements < 3 } }
					}

					preferred_limit = {
						num_of_settlements >= 2
						event_target:unclean_ruler = { capital_scope = { num_of_settlements < 2 } }
					}

					event_target:unclean_ruler = { capital = PREV }
				}

				# 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 = { owned_by = event_target:unclean_ruler }
								event_target:unclean_ruler = { capital = PREV }
							}
						}
					}
				}
			}
		}

		# 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

				trigger_if = {
					limit = { has_global_flag = emf_nwo_only_catholic }
					religion = catholic
				}
				trigger_else_if = {
					limit = { has_global_flag = emf_nwo_only_orthodox }
					religion = orthodox
				}
				trigger_else_if = {
					limit = { has_global_flag = emf_nwo_only_christian }
					religion_group = christian
				}
				trigger_else_if = {
					limit = { has_global_flag = emf_nwo_only_muslim }
					religion_group = muslim
				}

				trigger_if = {
					limit = { has_landed_title = k_papacy }
					NOT = { has_global_flag = emf_nwo_spare_pope }
				}
				trigger_else_if = {
					limit = { is_merchant_republic = yes }
					NOT = { has_global_flag = emf_nwo_spare_mr }
				}
				trigger_else_if = {
					limit = { is_nomadic = yes }
					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 = {
					higher_tier_than = COUNT
					clan = no

					trigger_if = {
						limit = { is_landless_type_title = yes }
						temporary = yes
					}

					trigger_if = {
						limit = { has_global_flag = emf_nwo_spare_mr }

						holder_scope = {
							is_merchant_republic = no

							NOT = {
								any_liege = { is_merchant_republic = yes }
							}
						}
					}

					trigger_if = {
						limit = { has_global_flag = emf_nwo_spare_pope }

						holder_scope = {
							NOR = {
								has_landed_title = k_papacy
								any_liege = { has_landed_title = k_papacy }
							}
						}
					}
				}

				if = {
					limit = {
						has_global_flag = emf_nwo_duke
						has_global_flag = emf_nwo_retain_rulers

						holder_scope = {
							capital_holding = { de_jure_liege_or_above = PREVPREV }
						}
					}

					holder_scope = {
						set_character_flag = prev_prince
					}
				}

				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 = {
				primary_title = { is_landless_type_title = yes }
				higher_tier_than = COUNT
				is_landed = yes
				is_nomadic = no

				trigger_if = {
					limit = { has_global_flag = emf_nwo_only_catholic }
					top_liege = { religion = catholic }
				}
				trigger_else_if = {
					limit = { has_global_flag = emf_nwo_only_orthodox }
					top_liege = { religion = orthodox }
				}
				trigger_else_if = {
					limit = { has_global_flag = emf_nwo_only_christian }
					top_liege = { religion_group = christian }
				}
				trigger_else_if = {
					limit = { has_global_flag = emf_nwo_only_muslim }
					top_liege = { religion_group = muslim }
				}

				trigger_if = {
					limit = { has_landed_title = k_papacy }

					NOR = {
						has_global_flag = emf_nwo_spare_pope
						has_global_flag = emf_nwo_spare_pope_only
					}
				}

				trigger_if = {
					limit = { has_global_flag = emf_nwo_spare_mr }

					is_merchant_republic = no
					NOT = { any_liege = { is_merchant_republic = yes } }
				}
			}

			save_event_target_as = playable_ruler

			# Grant away all your count titles to new characters.
			any_demesne_title = {
				limit = { real_tier = COUNT }

				save_event_target_as = demesne_title

				location = {
					event_target:playable_ruler = {
						random_list = {
							50 = {
								trigger = {
									OR = {
										has_gender_equal_game_rule_trigger = yes
										PREV = {
											religion_scope = {
												NOT = { has_religion_feature = religion_matriarchal }
											}
										}
									}
								}

								emf_nwo_create_male_ruler_effect = yes
							}

							50 = {
								trigger = {
									OR = {
										has_gender_equal_game_rule_trigger = yes
										PREV = { is_feminist_trigger = yes }
									}
								}

								emf_nwo_create_female_ruler_effect = yes
							}
						}

						new_character = {
							gain_title = event_target:demesne_title
							set_defacto_liege = THIS
						}
					}
				}

				clear_event_target = demesne_title
			}

			# And all your barony titles to appropriate characters (owner of their county).
			any_demesne_title = {
				limit = { real_tier = BARON }

				dejure_liege_title = {
					holder_scope  = {
						gain_title = PREVPREV
					}
				}
			}

			# If your landless title was binding any vassals, they'll be freed in next step.
			clear_event_target = playable_ruler
		}

		any_playable_ruler = {
			limit = {
				independent = no

				trigger_if = {
					limit = { has_global_flag = emf_nwo_only_catholic }
					top_liege = { religion = catholic }
				}
				trigger_else_if = {
					limit = { has_global_flag = emf_nwo_only_orthodox }
					top_liege = { religion = orthodox }
				}
				trigger_else_if = {
					limit = { has_global_flag = emf_nwo_only_christian }
					top_liege = { religion_group = christian }
				}
				trigger_else_if = {
					limit = { has_global_flag = emf_nwo_only_muslim }
					top_liege = { religion_group = muslim }
				}

				trigger_if = {
					limit = { is_vassal_or_below_of = k_papacy }
					NOT = { has_global_flag = emf_nwo_spare_pope }
				}

				trigger_if = {
					limit = { any_liege = { is_merchant_republic = yes } }
					NOT = { has_global_flag = emf_nwo_spare_mr }
				}

				trigger_if = {
					limit = { top_liege = { is_nomadic = yes } }
					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.

		any_independent_ruler = {
			limit = {
				trigger_if = {
					limit = { is_nomadic = yes }
					has_global_flag = emf_nwo_tiny_nomad
				}
				trigger_else = {
					tier = COUNT
				}

				trigger_if = {
					limit = { has_global_flag = emf_nwo_only_catholic }
					religion = catholic
				}
				trigger_else_if = {
					limit = { has_global_flag = emf_nwo_only_orthodox }
					religion = orthodox
				}
				trigger_else_if = {
					limit = { has_global_flag = emf_nwo_only_christian }
					religion_group = christian
				}
				trigger_else_if = {
					limit = { has_global_flag = emf_nwo_only_muslim }
					religion_group = muslim
				}
			}

			save_event_target_as = independent_ruler

			# Grant excess nomadic counties to new, random characters
			while = {
				limit = {
					is_nomadic = yes

					any_demesne_title = {
						tier = COUNT

						location = {
							is_capital = no
							num_of_settlements == 0
						}
					}
				}

				random_demesne_title = {
					limit = {
						tier = COUNT

						location = {
							is_capital = no
							num_of_settlements == 0
						}
					}

					location = {
						event_target:independent_ruler = {
							random_list = {
								50 = {
									trigger = {
										OR = {
											has_gender_equal_game_rule_trigger = yes
											PREV = {
												religion_scope = {
													NOT = { has_religion_feature = religion_matriarchal }
												}
											}
										}
									}

									emf_nwo_create_male_ruler_effect = yes
								}

								50 = {
									trigger = {
										OR = {
											has_gender_equal_game_rule_trigger = yes
											PREV = { is_feminist_trigger = yes }
										}
									}

									emf_nwo_create_female_ruler_effect = yes
								}
							}

							new_character = {
								gain_title = PREVPREVPREV
								set_government_type = nomadic_government
								set_defacto_liege = THIS
							}
						}
					}
				}
			}

			# Grant excess counties to new, random characters
			while = {
				limit = { num_of_count_titles >= 2 }

				random_demesne_title = {
					limit = {
						tier = COUNT

						location = {
							is_capital = no
						}
					}

					location = {
						event_target:independent_ruler = {
							random_list = {
								50 = {
									trigger = {
										OR = {
											has_gender_equal_game_rule_trigger = yes
											PREV = {
												religion_scope = {
													NOT = { has_religion_feature = religion_matriarchal }
												}
											}
										}
									}

									emf_nwo_create_male_ruler_effect = yes
								}

								50 = {
									trigger = {
										OR = {
											has_gender_equal_game_rule_trigger = yes
											PREV = { is_feminist_trigger = yes }
										}
									}

									emf_nwo_create_female_ruler_effect = yes
								}
							}

							new_character = {
								gain_title = PREVPREVPREV
								set_defacto_liege = THIS
							}
						}
					}

				#	if = {
				#		limit = {
				#			NOT = { has_global_flag = emf_nwo_count }
				#		}
				#
				#		pressed_claim = ROOT # Strong, inheritable claim on county
				#	}
				}
			}

			# Seize/vassalize any non-de-jure baronies.
			capital_scope = {
				county = {
					any_direct_de_jure_vassal_title = {
						limit = {
							holder_scope = {
								NOT = { is_vassal_or_below_of = event_target:independent_ruler }
							}
						}

						# First, try to vassalize non-de-jure barons
						if = {
							limit = {
								holder_scope = {
									tier = BARON

									NOT = {
										any_demesne_title = {
											NOT = { de_jure_liege = PREVPREVPREV } # County
										}
									}
								}
							}

							holder_scope = {
								set_defacto_liege = event_target:independent_ruler
							}
						}
						# Then seize the rest
						else = {
							gain_title = event_target:independent_ruler
						}
					}
				}
			}
		}

		# 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:
		#   player
		#   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 = { NOT = { has_global_flag = emf_nwo_count } }

			while = {
				limit = {
					any_independent_ruler = {
						tier = COUNT

						trigger_if = {
							limit = { has_global_flag = emf_nwo_duke }
							capital_scope = { duchy = { has_holder = no } }
						}
						trigger_else = {
							has_global_flag = emf_nwo_king
							capital_scope = { kingdom = { has_holder = no } }
						}

						trigger_if = {
							limit = { has_global_flag = emf_nwo_only_catholic }
							religion = catholic
						}
						trigger_else_if = {
							limit = { has_global_flag = emf_nwo_only_orthodox }
							religion = orthodox
						}
						trigger_else_if = {
							limit = { has_global_flag = emf_nwo_only_christian }
							religion_group = christian
						}
						trigger_else_if = {
							limit = { has_global_flag = emf_nwo_only_muslim }
							religion_group = muslim
						}
					}
				}

				random_independent_ruler = {
					limit = {
						tier = COUNT

						trigger_if = {
							limit = { has_global_flag = emf_nwo_duke }
							capital_scope = { duchy = { has_holder = no } }
						}
						trigger_else = {
							has_global_flag = emf_nwo_king
							capital_scope = { kingdom = { has_holder = no } }
						}

						trigger_if = {
							limit = { has_global_flag = emf_nwo_only_catholic }
							religion = catholic
						}
						trigger_else_if = {
							limit = { has_global_flag = emf_nwo_only_orthodox }
							religion = orthodox
						}
						trigger_else_if = {
							limit = { has_global_flag = emf_nwo_only_christian }
							religion_group = christian
						}
						trigger_else_if = {
							limit = { has_global_flag = emf_nwo_only_muslim }
							religion_group = muslim
						}
					}

					# Restore with player preference
					preferred_limit = {
						ai = no
						has_global_flag = emf_nwo_retain_rulers_not_me
					}

					# Restore former princes
					preferred_limit = {
						has_character_flag = prev_prince
						has_global_flag = emf_nwo_retain_rulers
					}

					# Select feudal princes
					preferred_limit = {
						is_feudal = yes
					}

					# Select tribal princes
					preferred_limit = {
						is_tribal = yes
					}

					# Select republican princes
					preferred_limit = {
						is_republic = yes
					}

					# Fallback

					capital_scope = {
						if = {
							limit = { has_global_flag = emf_nwo_duke }
							duchy = { gain_title = PREVPREV }
						}
						else = {
							kingdom = { gain_title = PREVPREV }
						}
					}

					clr_character_flag = prev_prince

					# Vassalize the de jure vassal princelings of my principality.
					primary_title = {
						any_de_jure_vassal = {
							limit = {
								independent = yes
								tier = COUNT

								trigger_if = {
									limit = { has_global_flag = emf_nwo_only_catholic }
									religion = catholic
								}
								trigger_else_if = {
									limit = { has_global_flag = emf_nwo_only_orthodox }
									religion = orthodox
								}
								trigger_else_if = {
									limit = { has_global_flag = emf_nwo_only_christian }
									religion_group = christian
								}
								trigger_else_if = {
									limit = { has_global_flag = emf_nwo_only_muslim }
									religion_group = muslim
								}
							}

							set_defacto_liege = PREVPREV
						}
					}

					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
									gain_title = PREVPREV
								}
							}
						}

						# 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 = {
									gain_title = PREVPREV
								}
							}

							set_defacto_liege = PREV # 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 = PREVPREV
									}

									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 }
					#			gain_title = ROOT
					#		}
					#	}
					}

					any_claim = {
						limit = { holder_scope = { is_vassal_or_below_of = PREVPREV } }
						remove_claim = PREV
					}
				}
			}
		}

		if = {
			limit = { has_global_flag = emf_nwo_generate_children }

			any_playable_ruler = {
				limit = {
					trigger_if = {
						limit = { has_global_flag = emf_nwo_only_catholic }
						religion = catholic
					}
					trigger_else_if = {
						limit = { has_global_flag = emf_nwo_only_orthodox }
						religion = orthodox
					}
					trigger_else_if = {
						limit = { has_global_flag = emf_nwo_only_christian }
						religion_group = christian
					}
					trigger_else_if = {
						limit = { has_global_flag = emf_nwo_only_muslim }
						religion_group = muslim
					}
				}

				# Randomly generate at least one child guaranteed if the character is older than 16
				emf_nwo_generate_child = yes

				random = { # 50% chance to get a 2nd child
					chance = 50
					emf_nwo_generate_child = yes
				}
			}
		}

		# Convert the provinces of pre-setup regions to ancient religions.
		if = {
			limit = { has_global_flag = emf_nwo_ancient_religions }

			any_independent_ruler = {
				limit = {
					trigger_if = {
						limit = { has_global_flag = emf_nwo_only_catholic }
						religion = catholic
					}
					trigger_else_if = {
						limit = { has_global_flag = emf_nwo_only_orthodox }
						religion = orthodox
					}
					trigger_else_if = {
						limit = { has_global_flag = emf_nwo_only_christian }
						religion_group = christian
					}
					trigger_else_if = {
						limit = { has_global_flag = emf_nwo_only_muslim }
						religion_group = muslim
					}
				}

				any_realm_province = {
					limit = {
						OR = {
							has_castle = yes
							has_temple = yes
							has_city = yes
							has_tribal = yes
						}

						held_under_PREV = yes
					}

					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_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
		}

		# 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.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

		event_target:pulse_province = {
			owner = {
				character_event = {
					id = emf_nwo.0
				#	portrait = no
				}
			}
		}
	}

	option = {
		name = NO
	}
}