story_mongol_invasion = {
	# by Petter Vilberg
	
	on_setup = {
		story_owner = { # Start conquest of all of Mongolia
			start_wars_for_mongolia_effect = yes
		}
		set_variable = {
			name = succession_counter
			value = 0
		}
	}
#
	on_end = {
		debug_log = "Mongol story ended on:"
		debug_log_date = yes
		set_global_variable = {
			name = mongol_story_has_ended
			value = yes
		}
	}
#
	on_owner_death = {
		story_owner = {
			set_variable = {
				name = special_mongol_empire_conquest_region_prio_1_alpha_var
				value = 0
			}
			set_variable = {
				name = special_mongol_empire_conquest_region_prio_1_var
				value = 0
			}
			set_variable = {
				name = special_mongol_empire_conquest_region_prio_2_var
				value = 0
			}
			set_variable = {
				name = special_mongol_empire_conquest_region_prio_3_var
				value = 0
			}
			set_variable = {
				name = special_mongol_empire_conquest_region_prio_4_var
				value = 0
			}
			set_variable = {
				name = special_mongol_empire_conquest_region_prio_5_var
				value = 0
			}
			set_variable = {
				name = outside_conquest_region_var
				value = 0
			}
			every_realm_county = {
				if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_conquest_region_prio_5
						}
					}
					root.story_owner = {
						change_variable = {
							name = special_mongol_empire_conquest_region_prio_5_var
							add = 1
						}
					}
				}
				else_if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_conquest_region_prio_4
						}
					}
					root.story_owner = {
						change_variable = {
							name = special_mongol_empire_conquest_region_prio_4_var
							add = 1
						}
					}
				}
				else_if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_conquest_region_prio_3
						}
					}
					root.story_owner = {
						change_variable = {
							name = special_mongol_empire_conquest_region_prio_3_var
							add = 1
						}
					}
				}
				else_if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_conquest_region_prio_2
						}
					}
					root.story_owner = {
						change_variable = {
							name = special_mongol_empire_conquest_region_prio_2_var
							add = 1
						}
					}
				}
				else_if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_conquest_region_prio_1
						}
					}
					root.story_owner = {
						change_variable = {
							name = special_mongol_empire_conquest_region_prio_1_var
							add = 1
						}
					}
				}
				else_if = {
					limit = {
						title_province = {
							geographical_region = special_mongol_empire_conquest_region_prio_1_alpha
						}
					}
					root.story_owner = {
						change_variable = {
							name = special_mongol_empire_conquest_region_prio_1_alpha_var
							add = 1
						}
					}
				}
				else = {
					root.story_owner = {
						change_variable = {
							name = outside_conquest_region_var
							add = 1
						}
					}
				}
			}
		}
		# Weighted random chance to end the story/break up the Empire
		random = {
			chance = 0
			#Pure realm size - Mongol empire is historical breakup size
			modifier = {
				add = 20
				story_owner = {
					realm_size >= 1087
				}
			}
			#Pure realm size - Mongol empire is max extent size
			modifier = {
				add = 20
				story_owner = {
					realm_size >= 1474
				}
			}
			#Pure realm size - Mongol empire is at total bonus size
			modifier = {
				add = 30
				story_owner = {
					realm_size >= 1725
				}
			}
			modifier = { # If the empire is very successful, increase the chance
				add = 10
				story_owner = {
					#Half of total county count
					var:special_mongol_empire_conquest_region_prio_1_alpha_var >= 80
					var:special_mongol_empire_conquest_region_prio_1_var >= 60
					var:special_mongol_empire_conquest_region_prio_2_var >= 100
					#Just a third
					var:special_mongol_empire_conquest_region_prio_3_var >= 30
					var:special_mongol_empire_conquest_region_prio_4_var >= 100
					var:special_mongol_empire_conquest_region_prio_5_var >= 120
				}
			}
			modifier = { # If the empire is EXTREMELY successful, increase the chance
				add = 20
				story_owner = {
					#Three quarters of total county count
					var:special_mongol_empire_conquest_region_prio_1_alpha_var >= 120
					var:special_mongol_empire_conquest_region_prio_1_var >= 75
					var:special_mongol_empire_conquest_region_prio_2_var >= 200
					#Just half
					var:special_mongol_empire_conquest_region_prio_3_var >= 45
					var:special_mongol_empire_conquest_region_prio_4_var >= 150
					var:special_mongol_empire_conquest_region_prio_5_var >= 180
					var:outside_conquest_region_var >= 100
				}
			}
			modifier = {
				add = 20
				var:succession_counter > 1
			}
			modifier = {
				add = 30
				var:succession_counter > 2
			}
			modifier = { # Historical point
				add = 30
				var:succession_counter > 3
			}
			modifier = { # Guaranteed if there have been five successions
				add = 20
				var:succession_counter > 4
			}
			set_variable = {
				name = end_of_story
				value = yes
			}
		}
		if = {
			limit = {
				exists = var:end_of_story
			}
			# Do the breakup of the empire if possible
			story_owner = {
				if = {
					limit = {
						realm_size >= 50
					}
					debug_log = "Trying to break up the Mongol Empire"
					break_up_mongol_empire_effect = yes
				}
				else = {
					save_scope_as = old_khan
					player_heir = {
						save_scope_as = new_khan
					}
					every_player = {
						trigger_event = mongol_invasion.1005 # Small end to story
					}
				}
			}
			end_story = yes
		}
		else = {
			change_variable = {
				name = succession_counter
				add = 1
			}
			# Handle story succession
			story_owner = {
				save_scope_as = old_khan # for news events
				if = {
					limit = {
						any_child = { # if the Khan has a reasonable heir...
							is_primary_heir_of = scope:story.story_owner
						}
					}
					random_child = {
						limit = {
							is_primary_heir_of = scope:story.story_owner		
						}
						save_scope_as = new_khan # for news events
						save_scope_as = new_story_owner # pass chain to appropriate target
						add_character_flag = {
							flag = gain_mongol_succession_dread
							days = 2
						}
						add_character_modifier = {
							modifier = the_great_khan_modifier
						}
					}
					every_player = {
						trigger_event = {
							id = mongol_invasion.1001
							days = 1
						}
					}
				}
				else_if = {
					limit = {
						dynasty = {
							any_dynasty_member = { # if the Khan has some other heir (not family?)
								is_primary_heir_of = scope:story.story_owner
							}
						}
					}				
					dynasty = {
						random_dynasty_member = {
							limit = {
								is_primary_heir_of = scope:story.story_owner
							}
							save_scope_as = new_khan # for news events
							save_scope_as = new_story_owner # pass chain to appropriate target
							add_character_flag = {
								flag = gain_mongol_succession_dread
								days = 2
							}
							add_character_modifier = {
								modifier = the_great_khan_modifier
							}
						}
					}
					every_player = {
						trigger_event = {
							id = mongol_invasion.1001
							days = 1
						}
					}
				}
				else_if = { # If the empire passes out of the original dynasty, the story will automatically end
					limit = {
						NOT = {
							dynasty = {
								any_dynasty_member = { # if the Khan has some other heir (not family?)
									is_primary_heir_of = scope:story.story_owner
								}
							}
						}
					}
					scope:story.story_owner.primary_heir = {
						save_scope_as = new_khan
					}
					every_player = {
						trigger_event = {
							id = mongol_invasion.1001
							days = 1
						}
					}
				}
			}
			if = {
				limit = { exists = scope:new_story_owner }
				make_story_owner = scope:new_story_owner
				scope:new_story_owner = {
					# Delayed firing of spawning new event troops
					trigger_event = {
						id = mongol_invasion.0100
						days = 1
					}
				}
				debug_log = "Successfully replaced the Mongol Khan"
				debug_log_date = yes
				debug_log_scopes = yes
			}
			else = {
				# Make sure the empire breaks up as best as possible
				story_owner = {
					if = {
						limit = {
							realm_size >= 50
						}
					}
					debug_log = "Due to failing to replace the Khan with a Temujin successor, now trying to break up the empire"
					break_up_mongol_empire_effect = yes
				}

				end_story = yes
				debug_log = "Failed to replace the Mongol Khan"
				debug_log_date = yes
				debug_log_scopes = yes
			}
		}
		story_owner = {
			clear_variable_list = special_mongol_empire_conquest_region_prio_1_alpha_var
			clear_variable_list = special_mongol_empire_conquest_region_prio_1_var
			clear_variable_list = special_mongol_empire_conquest_region_prio_2_var
			clear_variable_list = special_mongol_empire_conquest_region_prio_3_var
			clear_variable_list = special_mongol_empire_conquest_region_prio_4_var
			clear_variable_list = special_mongol_empire_conquest_region_prio_5_var
			clear_variable_list = outside_conquest_region_var
		}
	}



	#Start a new war if none is ongoing
	effect_group = {
		days = { 30 50 }

		trigger = {
			story_owner = {
				is_at_war = no
				gold > 0
				any_held_title = {
					this = title:e_mongol_empire
				}
			}
		}

		triggered_effect = {
			trigger = {
				story_owner = {
					is_ai = yes
				}
			}
			effect = {
				if = {
					limit = {
						has_mpo_dlc_trigger = yes
					}
					story_owner = {
						gok_war_target_evaluation_and_declaration_effect = yes
					}
				}
				else = {
					story_owner = {
						mongol_war_target_evaluation_and_declaration_effect = yes
					}
				}
				debug_log = "The story started a new war for the Mongols"
				debug_log_date = yes
				story_owner = {
					save_scope_as = owner
				}
				debug_log_scopes = yes
			}
		}
	}

	# Story handling in case of succession weirdness
	effect_group = {
		days = 180

		trigger = {
			story_owner = {
				NOT = {
					any_held_title = {
						this = title:e_mongol_empire
					}
				}
			}
		}

		triggered_effect = {
			trigger = { exists = title:e_mongol_empire }
			effect = {
				if = {
					limit = {
						title:e_mongol_empire.holder = {
							culture = culture:mongol
						}
					}
					make_story_owner = title:e_mongol_empire.holder
				}
				else = {
					end_story = yes
				}
			}
		}

		triggered_effect = {
			trigger = { NOT = { exists = title:e_mongol_empire } }
			effect = {
				end_story = yes
			}
		}
	}

	# Set up Mongol Empire de jure stuff
	effect_group = {
		days = 180

		trigger = {
			NOT = { exists = var:has_updated_mongolia_de_jure }
			trigger_if = {
				limit = {
					title:e_mongolia = {
						any_de_jure_county = {
							count >= 3
						}
					}
				}
				title:e_mongolia = {
					any_in_de_jure_hierarchy = {
						continue = { tier > tier_county }
						tier = tier_county
						holder.top_liege = scope:story.story_owner
						count > 3
					}
				}
			}	
			story_owner.capital_county.empire = {
				any_in_de_jure_hierarchy = {
					continue = { tier > tier_county }
					tier = tier_county
					holder.top_liege = scope:story.story_owner
					count > 5
				}
			}
		}

		triggered_effect = {
			trigger = {
				always = yes
			}

			effect = {
				title:e_mongolia = {
					every_in_de_jure_hierarchy = {
						limit = {
							tier = tier_kingdom
						}
						set_de_jure_liege_title = title:e_mongol_empire
					}
				}
				if = {
					limit = {
						exists = title:e_mongolia.holder
					}
					title:e_mongolia.holder = {
						destroy_title = title:e_mongolia
					}
				}
				if = {
					limit = {
						story_owner.capital_county.empire = {
							NOT = {
								this = title:e_mongol_empire
							}
						}
					}
					story_owner.capital_county.empire = {
						save_scope_as = new_de_jure
					}
					scope:new_de_jure = {
						every_in_de_jure_hierarchy = {
							limit = {
								tier = tier_kingdom
							}
							set_de_jure_liege_title = title:e_mongol_empire
						}
					}
				}

				set_variable = {
					name = has_updated_mongolia_de_jure
					value = yes
				}
			}
		}
	}
}
