###########################################
#                                         #
# Chinese Status, Policy & News Events    #
#                                         #
# ID JD.50000-JD.51000                    #
#                                         #
###########################################

namespace = JD

# Written by:
# Henrik Fhraeus
# Alexander Oltner

# New Chinese Emperor. Pick a new Policy and send a News Event!
# Hidden. Fired from on_offmap_ruler_changed.
character_event = {
	id = JD.50000

	offmap = only
	is_triggered_only = yes # on_offmap_ruler_changed
	hide_window = yes

	has_dlc = "Jade Dragon"

	trigger = { is_offmap_tag = offmap_china }

	immediate = {
		FROMFROM = {
			set_offmap_flag = no_policy_news # Don't fire the News event in 'on_offmap_policy_changed'

			random_list = {
				100 = { # OPEN
					mult_modifier = {
						factor = 1.5
						has_status = china_stable
					}
					mult_modifier = {
						factor = 5
						has_policy = china_open
						days_since_policy_change < 10950 #30 years
					}

					set_policy = china_open

					log = "-------------------------------------"
					log = "China Policy Logging:"
					log = "Chinas new Policy is now OPEN"
					log = "-------------------------------------"
				}

				20 = { # ISOLATIONIST
					trigger = {
						trigger_if = {
							limit = { has_offmap_tmp_flag = china_forced_open }
							had_offmap_tmp_flag = { flag = china_forced_open years >= 50 }
						}
					}

					mult_modifier = {
						factor = 10
						has_offmap_flag = china_isolationist_trend
					}
					mult_modifier = { # Hard times... (negative Status)
						factor = 2
						china_is_suffering_trigger = yes
					}
					mult_modifier = {
						factor = 0.5
						has_status = china_golden_age
					}
					mult_modifier = {
						factor = 5
						has_policy = china_isolationist
						days_since_policy_change < 10950 #30 years
					}

					set_policy = china_isolationist
					clr_offmap_flag = china_isolationist_trend
					clr_offmap_tmp_flag = china_forced_open

					log = "-------------------------------------"
					log = "China Policy Logging:"
					log = "Chinas new Policy is now ISOLATIONIST"
					log = "-------------------------------------"
				}

				10 = { # EXPANSIONIST
					trigger = {
						NOT = { has_offmap_flag = china_badly_beaten_3 }
						check_variable = { which = global_china_change_to_expansionist_count value < 6 }
					}

					mult_modifier = {
						factor = 20
						has_offmap_flag = china_expansionist_trend
					}
					mult_modifier = {
						factor = 2
						has_status = china_golden_age
					}
					mult_modifier = {
						factor = 0.75
						has_offmap_flag = china_badly_beaten_1
					}
					mult_modifier = {
						factor = 0.5
						has_offmap_flag = china_badly_beaten_2
					}
					mult_modifier = {
						factor = 10
						has_policy = china_expansionist
						days_since_policy_change < 14600 #40 years
					}
					mult_modifier = {
						factor = 0.1
						has_policy = china_expansionist
						days_since_policy_change >= 32850 #90 years
					}
					mult_modifier = {
						factor = 0.2
						check_variable = { which = global_china_change_to_expansionist_count value >= 3 }
					}

					if = {
						limit = { NOT = { has_policy = china_expansionist } }
						change_variable = { which = global_china_change_to_expansionist_count value = 1 }
					}

					set_policy = china_expansionist
					clr_offmap_flag = china_expansionist_trend

					log = "-------------------------------------"
					log = "China Policy Logging:"
					log = "Chinas new Policy is now EXPANSIONIST"
					log = "-------------------------------------"
				}
			}
		}

		if = {
			limit = {
				FROMFROM = { NOT = { has_offmap_flag = no_succession_news } }
			}

			any_player = {
				limit = {
					has_offmap_news_enabled = FROMFROM
					is_within_diplo_range = ROOT
				}
				narrative_event = { id = JD.50001 }
			}
		}

		FROMFROM = {
			clr_offmap_flag = no_policy_news
			clr_offmap_flag = no_succession_news
		}
	}
}

# News from China: A new Emperor
narrative_event = {
 	id = JD.50001
	title = NEWS_FROM_CHINA
	picture = GFX_evt_china_golden_age
	portrait = offmap_china
	window = EventWindowOffmap
	background = GFX_event_window_news_from_china

	desc = {
		text = EVTDESC_JD_50001
		trigger = {
			FROM = {
				dynasty = ROOT_FROMFROM

				trigger_if = {
					limit = { prev_policy = china_open }
					has_policy = china_open
				}
				trigger_else_if = {
					limit = { prev_policy = china_isolationist }
					has_policy = china_isolationist
				}
				trigger_else = {
					prev_policy = china_expansionist
					has_policy = china_expansionist
				}
			}
		}
	}
	desc = {
		text = EVTDESC_JD_50001_NEW_DYN
		trigger = {
			FROM = {
				NOT = { dynasty = ROOT_FROMFROM }

				trigger_if = {
					limit = { prev_policy = china_open }
					has_policy = china_open
				}
				trigger_else_if = {
					limit = { prev_policy = china_isolationist }
					has_policy = china_isolationist
				}
				trigger_else = {
					prev_policy = china_expansionist
					has_policy = china_expansionist
				}
			}
		}
	}
	desc = {
		text = EVTDESC_JD_50001_NEW_POLICY
		trigger = {
			FROM = {
				dynasty = ROOT_FROMFROM

				trigger_if = {
					limit = { prev_policy = china_open }
					NOT = { has_policy = china_open }
				}
				trigger_else_if = {
					limit = { prev_policy = china_isolationist }
					NOT = { has_policy = china_isolationist }
				}
				trigger_else = {
					prev_policy = china_expansionist
					NOT = { has_policy = china_expansionist }
				}
			}
		}
	}
	desc = {
		text = EVTDESC_JD_50001_NEW_DYN_NEW_POLICY
		trigger = {
			FROM = {
				NOT = { dynasty = ROOT_FROMFROM }

				trigger_if = {
					limit = { prev_policy = china_open }
					NOT = { has_policy = china_open }
				}
				trigger_else_if = {
					limit = { prev_policy = china_isolationist }
					NOT = { has_policy = china_isolationist }
				}
				trigger_else = {
					prev_policy = china_expansionist
					NOT = { has_policy = china_expansionist }
				}
			}
		}
	}

	is_triggered_only = yes

	has_dlc = "Jade Dragon"

	option = {
		name = {
			text = EVTOPT_JD_50001_OPEN
			trigger = {
				FROM = { has_policy = china_open }
			}
		}
		name = {
			text = EVTOPT_JD_50001_ISO
			trigger = {
				FROM = { has_policy = china_isolationist }
			}
		}
		name = {
			text = EVTOPT_JD_50001_EXP
			trigger = {
				FROM = { has_policy = china_expansionist }
			}
		}

		trigger = {
			OR = {
				NOT = { has_character_modifier = peace_deal_with_china }
				FROM = { dynasty = ROOT_FROMFROM }
			}
		}
	}

	option = {
		name = EVTOPT_JD_50302_BROKEN_PEACE_DEAL

		trigger = {
			has_character_modifier = peace_deal_with_china

			FROM = {
				NOT = { dynasty = ROOT_FROMFROM }
			}
		}

		remove_character_modifier = peace_deal_with_china
	}

	after = {
		if = {
			limit = { has_character_flag = was_tributary_of_china }

			clr_character_flag = was_tributary_of_china

			tooltip = {
				FROM = {
					show_scope_change = no

					governor = {
						show_scope_change = no

						remove_tributary = ROOT
					}
				}
			}
		}
	}
}

# New Policy Picked.
# Hidden. Fired from on_offmap_policy_changed.
# Root = Governor
# From = Offmap
character_event = {
	id = JD.50020

	is_triggered_only = yes # on_offmap_policy_changed
	hide_window = yes

	has_dlc = "Jade Dragon"

	trigger = {
		FROM = {
			NOT = { has_offmap_flag = no_policy_news }
			is_offmap_tag = offmap_china
		}
	}

	immediate = {
		any_player = {
			limit = {
				has_offmap_news_enabled = FROM
				is_within_diplo_range = ROOT
			}

			narrative_event = { id = JD.50021 }
		}
	}
}

# News from China: New Policy Picked
narrative_event = {
	id = JD.50021
	title = NEWS_FROM_CHINA
	portrait = offmap_china
	window = EventWindowOffmap
	background = GFX_event_window_news_from_china

	desc = {
		text = EVTDESC_JD_50021_OPEN
		picture = GFX_evt_china_open
		trigger = { FROMFROM = { has_policy = china_open } }
	}
	desc = {
		text = EVTDESC_JD_50021_ISO
		picture = GFX_evt_china_isolationist
		trigger = { FROMFROM = { has_policy = china_isolationist } }
	}
	desc = {
		text = EVTDESC_JD_50021_EXP
		picture = GFX_evt_china_expansionist
		trigger = { FROMFROM = { has_policy = china_expansionist } }
	}

	is_triggered_only = yes

	has_dlc = "Jade Dragon"

	option = {
		name = {
			text = EVTOPT_JD_50001_OPEN
			trigger = { FROMFROM = { has_policy = china_open } }
		}
		name = {
			text = EVTOPT_JD_50021_ISO
			trigger = { FROMFROM = { has_policy = china_isolationist } }
		}
		name = {
			text = EVTOPT_JD_50001_EXP
			trigger = { FROMFROM = { has_policy = china_expansionist } }
		}
	}
}


# New China Status
# Hidden. Fired from on_offmap_status_changed.
# Root = Governor
# From = Offmap
character_event = {
	id = JD.50030

	is_triggered_only = yes # on_offmap_status_changed
	hide_window = yes

	has_dlc = "Jade Dragon"

	trigger = { FROM = { is_offmap_tag = offmap_china } }

	immediate = {
		FROM = {
			clr_offmap_flag = china_mongol_invasion
			clr_offmap_flag = china_jurchen_invasion
		}

		if = {
			limit = {
				FROM = {
					NOT = { has_offmap_flag = no_status_news }
				}
			}

			any_player = {
				limit = {
					has_offmap_news_enabled = FROM
					is_within_diplo_range = ROOT
				}

				narrative_event = { id = JD.50031 }
			}
		}
	}
}

# News from China: New Status
narrative_event = {
	id = JD.50031
	title = NEWS_FROM_CHINA
	portrait = offmap_china
	window = EventWindowOffmap
	background = GFX_event_window_news_from_china

	desc = {
		text = EVTDESC_JD_50031_STABLE
		picture = GFX_evt_china_stable
		trigger = { FROMFROM = { has_status = china_stable } }
	}
	desc = {
		text = EVTDESC_JD_50031_UNREST
		picture = GFX_evt_china_unrest
		trigger = { FROMFROM = { has_status = china_unrest } }
	}
	desc = {
		text = EVTDESC_JD_50031_FAMINE
		picture = GFX_evt_china_famine
		trigger = { FROMFROM = { has_status = china_famine } }
	}
	desc = {
		text = EVTDESC_JD_50031_PLAGUE
		picture = GFX_evt_china_devastating_plague
		trigger = { FROMFROM = { has_status = china_plague } }
	}
	desc = {
		text = EVTDESC_JD_50031_MONGOLS
		picture = GFX_evt_china_mongols_invade
		trigger = { FROMFROM = { has_status = china_mongol_invasion } }
	}
	desc = {
		text = EVTDESC_JD_50031_JURCHENS
		picture = GFX_evt_china_mongols_invade
		trigger = { FROMFROM = { has_status = china_jurchen_invasion } }
	}
	desc = {
		text = EVTDESC_JD_50031_CW
		picture = GFX_evt_china_civil_war
		trigger = { FROMFROM = { has_status = china_civil_war } }
	}
	desc = {
		text = EVTDESC_JD_50031_GOLDEN
		picture = GFX_evt_china_golden_age
		trigger = { FROMFROM = { has_status = china_golden_age } }
	}

	is_triggered_only = yes

	has_dlc = "Jade Dragon"

	option = {
		name = INTERESTING
	}
}

# Status returns to 'Stable'
# Hidden. Fired from on_offmap_yearly_pulse.
# Root = Governor
# From = Offmap
character_event = {
	id = JD.50100

	is_triggered_only = yes # on_offmap_yearly_pulse, random_events
	hide_window = yes

	has_dlc = "Jade Dragon"

	trigger = {
		FROM = {
			is_offmap_tag = offmap_china

			NOR = {
				has_status = china_stable
				has_status = china_mongol_invasion # Resolution handled by JD.50160
				has_status = china_jurchen_invasion # Resolution handled by JD.50200
				has_status = china_civil_war # Resolution handled by JD.50300
			}

			# Unrest last for at least 2 years before becoming Stable/Civil War
			trigger_if = {
				limit = {
					has_status = china_unrest
					has_offmap_flag = china_had_unrest
				}

				had_offmap_tmp_flag = { flag = china_had_unrest years >= 2 }
			}
			# Famines last for at least 4 years
			trigger_else_if = {
				limit = {
					has_status = china_famine
					has_offmap_flag = china_had_famine
				}

				had_offmap_tmp_flag = { flag = china_had_famine years >= 4 }
			}
			# Plagues last for at least 2 years
			trigger_else_if = {
				limit = {
					has_status = china_plague
					has_offmap_flag = china_had_plague
				}

				had_offmap_tmp_flag = { flag = china_had_plague years >= 2 }
			}
			# Golden ages last at least 50 years
			trigger_else_if = {
				limit = {
					has_status = china_golden_age
					has_offmap_flag = china_had_golden_age
				}

				had_offmap_tmp_flag = { flag = china_had_golden_age years >= 50 }
			}
		}
	}

	weight_multiplier = {
		factor = 1

		mult_modifier = {
			factor = 2
			FROM = {
				has_status = china_unrest
				has_offmap_flag = china_had_unrest
				had_offmap_tmp_flag = { flag = china_had_unrest years >= 8 }
			}
		}
		mult_modifier = {
			factor = 1.5
			FROM = {
				has_status = china_unrest
				has_offmap_flag = china_had_unrest
				had_offmap_tmp_flag = { flag = china_had_unrest years >= 10 }
			}
		}
		mult_modifier = {
			factor = 1.5
			FROM = {
				has_status = china_unrest
				has_offmap_flag = china_had_unrest
				had_offmap_tmp_flag = { flag = china_had_unrest years >= 12 }
			}
		}
		mult_modifier = {
			factor = 1.5
			FROM = {
				has_status = china_unrest
				has_offmap_flag = china_had_unrest
				had_offmap_tmp_flag = { flag = china_had_unrest years >= 14 }
			}
		}
		mult_modifier = {
			factor = 1.5
			FROM = {
				has_status = china_unrest
				has_offmap_flag = china_had_unrest
				had_offmap_tmp_flag = { flag = china_had_unrest years >= 16 }
			}
		}
		mult_modifier = {
			factor = 1.25
			FROM = {
				has_status = china_famine
				has_offmap_flag = china_had_famine
				had_offmap_tmp_flag = { flag = china_had_famine years >= 6 }
			}
		}
		mult_modifier = {
			factor = 1.25
			FROM = {
				has_status = china_plague
				has_offmap_flag = china_had_plague
				had_offmap_tmp_flag = { flag = china_had_plague years >= 3 }
			}
		}
		mult_modifier = {
			factor = 1.5
			FROM = {
				has_status = china_famine
				has_offmap_flag = china_had_famine
				had_offmap_tmp_flag = { flag = china_had_famine years >= 8 }
			}
		}
		mult_modifier = {
			factor = 1.5
			FROM = {
				has_status = china_plague
				has_offmap_flag = china_had_plague
				had_offmap_tmp_flag = { flag = china_had_plague years >= 5 }
			}
		}
		mult_modifier = {
			factor = 2
			FROM = {
				has_status = china_famine
				has_offmap_flag = china_had_famine
				had_offmap_tmp_flag = { flag = china_had_famine years >= 10 }
			}
		}
		mult_modifier = {
			factor = 2
			FROM = {
				has_status = china_plague
				has_offmap_flag = china_had_plague
				had_offmap_tmp_flag = { flag = china_had_plague years >= 7 }
			}
		}
		mult_modifier = {
			factor = 1.25
			FROM = {
				has_status = china_golden_age
				has_offmap_flag = china_had_golden_age
				had_offmap_tmp_flag = { flag = china_had_golden_age years >= 60 }
			}
		}
		mult_modifier = {
			factor = 1.5
			FROM = {
				has_status = china_golden_age
				has_offmap_flag = china_had_golden_age
				had_offmap_tmp_flag = { flag = china_had_golden_age years >= 70 }
			}
		}
		mult_modifier = {
			factor = 2
			FROM = {
				has_status = china_golden_age
				has_offmap_flag = china_had_golden_age
				had_offmap_tmp_flag = { flag = china_had_golden_age years >= 80 }
			}
		}
	}

	immediate = {
		FROM = {
			set_status = china_stable # Players will be notified by JD.50030
		}
	}
}

# Status changes to 'Unrest'
# Hidden. Fired from on_offmap_yearly_pulse.
# Root = Governor
# From = Offmap
character_event = {
	id = JD.50120

	is_triggered_only = yes # on_offmap_yearly_pulse, random_events
	hide_window = yes

	has_dlc = "Jade Dragon"

	trigger = {
		FROM = {
			is_offmap_tag = offmap_china

			NOR = {
				has_status = china_unrest
				has_status = china_mongol_invasion # Resolution handled by JD.50160
				has_status = china_jurchen_invasion # Resolution handled by JD.50200
				has_status = china_civil_war # Resolution handled by JD.50300
			}

			# Famines last for at least 4 years
			trigger_if = {
				limit = {
					has_status = china_famine
					has_offmap_flag = china_had_famine
				}

				had_offmap_tmp_flag = { flag = china_had_famine years >= 4 }
			}
			# Plagues last for at least 2 years
			trigger_else_if = {
				limit = {
					has_status = china_plague
					has_offmap_flag = china_had_plague
				}

				had_offmap_tmp_flag = { flag = china_had_plague years >= 2 }
			}
		}
	}

	weight_multiplier = {
		factor = 1

		mult_modifier = { # Slightly less likely to get unrest if things are fine
			factor = 0.75
			FROM = {
				has_status = china_stable
			}
		}
		mult_modifier = { # Golden ages are very unlikely to end with Unrest
			factor = 0.01
			FROM = { has_status = china_golden_age }
		}
		mult_modifier = { # Slightly more likely to get Unrest if non-Chinese emperor
			factor = 1.25
			FROM = {
				holder_scope = {
					NOT = { culture_group = chinese_group }
				}
			}
		}
		mult_modifier = {
			factor = 1.25
			FROM = {
				has_status = china_famine
				has_offmap_flag = china_had_famine
				had_offmap_tmp_flag = { flag = china_had_famine years >= 6 }
			}
		}
		mult_modifier = {
			factor = 1.25
			FROM = {
				has_status = china_plague
				has_offmap_flag = china_had_plague
				had_offmap_tmp_flag = { flag = china_had_plague years >= 3 }
			}
		}
		mult_modifier = {
			factor = 1.5
			FROM = {
				has_status = china_famine
				has_offmap_flag = china_had_famine
				had_offmap_tmp_flag = { flag = china_had_famine years >= 8 }
			}
		}
		mult_modifier = {
			factor = 1.5
			FROM = {
				has_status = china_plague
				has_offmap_flag = china_had_plague
				had_offmap_tmp_flag = { flag = china_had_plague years >= 5 }
			}
		}
		mult_modifier = {
			factor = 2
			FROM = {
				has_status = china_famine
				has_offmap_flag = china_had_famine
				had_offmap_tmp_flag = { flag = china_had_famine years >= 10 }
			}
		}
		mult_modifier = {
			factor = 2
			FROM = {
				has_status = china_plague
				has_offmap_flag = china_had_plague
				had_offmap_tmp_flag = { flag = china_had_plague years >= 7 }
			}
		}
	}

	immediate = {
		FROM = {
			set_offmap_flag = china_had_unrest
			set_status = china_unrest # Players will be notified by JD.50030
		}
	}
}

# Status changes to 'Mongol Invasion'
# Hidden. Fired from on_offmap_yearly_pulse.
# Root = Governor
# From = Offmap
character_event = {
	id = JD.50140

	is_triggered_only = yes # on_offmap_yearly_pulse, random_events
	hide_window = yes

	has_dlc = "Jade Dragon"

	trigger = {
		FROM = {
			is_offmap_tag = offmap_china

			NOR = {
				offmap_ruler = { culture = mongol }
				has_status = china_mongol_invasion # Resolution handled by JD.50160
				has_status = china_jurchen_invasion # Resolution handled by JD.50200
				has_status = china_civil_war # Resolution handled by JD.50300
			}

			trigger_if = {
				limit = { has_offmap_flag = china_invaded_player_dynasty }
				had_offmap_tmp_flag = { flag = china_invaded_player_dynasty years >= 200 }
			}

			# Famines last for at least 4 years
			trigger_if = {
				limit = {
					has_status = china_famine
					has_offmap_flag = china_had_famine
				}

				had_offmap_tmp_flag = { flag = china_had_famine years >= 4 }
			}
			# Plagues last for at least 2 years
			trigger_else_if = {
				limit = {
					has_status = china_plague
					has_offmap_flag = china_had_plague
				}

				had_offmap_tmp_flag = { flag = china_had_plague years >= 2 }
			}
		}
	}

	weight_multiplier = {
		factor = 1

		mult_modifier = { # Unrest increases the chance of an invasion
			factor = 3
			FROM = {
				has_status = china_unrest
				has_offmap_flag = china_had_unrest
				had_offmap_tmp_flag = { flag = china_had_unrest years >= 2 }
			}
		}
		mult_modifier = {
			factor = 1.5
			FROM = {
				has_status = china_unrest
				has_offmap_flag = china_had_unrest
				had_offmap_tmp_flag = { flag = china_had_unrest years >= 4 }
			}
		}
		mult_modifier = {
			factor = 1.5
			FROM = {
				has_status = china_unrest
				has_offmap_flag = china_had_unrest
				had_offmap_tmp_flag = { flag = china_had_unrest years >= 6 }
			}
		}
		mult_modifier = {
			factor = 1.25
			FROM = {
				has_status = china_famine
				has_offmap_flag = china_had_famine
				had_offmap_tmp_flag = { flag = china_had_famine years >= 6 }
			}
		}
		mult_modifier = {
			factor = 1.25
			FROM = {
				has_status = china_plague
				has_offmap_flag = china_had_plague
				had_offmap_tmp_flag = { flag = china_had_plague years >= 3 }
			}
		}
		mult_modifier = {
			factor = 1.5
			FROM = {
				has_status = china_famine
				has_offmap_flag = china_had_famine
				had_offmap_tmp_flag = { flag = china_had_famine years >= 8 }
			}
		}
		mult_modifier = {
			factor = 1.5
			FROM = {
				has_status = china_plague
				has_offmap_flag = china_had_plague
				had_offmap_tmp_flag = { flag = china_had_plague years >= 5 }
			}
		}
		mult_modifier = {
			factor = 2
			FROM = {
				has_status = china_famine
				has_offmap_flag = china_had_famine
				had_offmap_tmp_flag = { flag = china_had_famine years >= 10 }
			}
		}
		mult_modifier = {
			factor = 2
			FROM = {
				has_status = china_plague
				has_offmap_flag = china_had_plague
				had_offmap_tmp_flag = { flag = china_had_plague years >= 7 }
			}
		}
		mult_modifier = {
			factor = 0.3
			NOT = { has_global_flag = mongol_empire_arrival }
		}
		mult_modifier = {
			factor = 0.5
			FROM = { has_status = china_golden_age }
		}
		mult_modifier = {
			factor = 0.5
			FROM = { has_offmap_flag = china_mongols_won_1 }
		}
		mult_modifier = {
			factor = 0.25
			FROM = { has_offmap_flag = china_mongols_won_2 }
		}
		mult_modifier = {
			factor = 0.1
			FROM = { has_offmap_flag = china_mongols_won_3 }
		}
	}

	immediate = {
		FROM = {
			set_offmap_flag = no_status_news
			set_status = china_mongol_invasion
			set_offmap_flag = china_mongol_invasion
			clr_offmap_flag = no_status_news
		}

		any_player = {
			limit = {
				has_offmap_news_enabled = FROM
				is_within_diplo_range = ROOT
			}

			narrative_event = { id = JD.50141 }
		}
	}
}

# News from China: Mongol Invasion
narrative_event = {
	id = JD.50141
	title = NEWS_FROM_CHINA
	picture = GFX_evt_mongols
	portrait = offmap_china
	window = EventWindowOffmap
	background = GFX_event_window_news_from_china

	desc = {
		text = EVTDESC_JD_50141_BOOST
		trigger = {
			offmap_china = {
				OR = {
					prev_status = china_famine
					prev_status = china_unrest
					prev_status = china_plague
				}
			}
		}
	}
	desc = {
		text = EVTDESC_JD_50141_PENALTY
		trigger = {
			offmap_china = {
				prev_status = china_golden_age
			}
		}
	}
	desc = {
		text = EVTDESC_JD_50031_MONGOLS # Can be the same as the standard status change notification
		trigger = {
			offmap_china = {
				NOR = {
					prev_status = china_famine
					prev_status = china_unrest
					prev_status = china_plague
					prev_status = china_golden_age
				}
			}
		}
	}

	is_triggered_only = yes

	has_dlc = "Jade Dragon"

	option = {
		name = EVTOPT_JD_50141
	}
}

# Chance for an end to the 'Mongol Invasion' Status
# Hidden. Fired from on_offmap_yearly_pulse
# Root = Governor
# From = Offmap
character_event = {
	id = JD.50160

	is_triggered_only = yes # on_offmap_yearly_pulse, random_events
	hide_window = yes

	has_dlc = "Jade Dragon"

	trigger = {
		FROM = {
			is_offmap_tag = offmap_china
			has_status = china_mongol_invasion
		}
	}

	weight_multiplier = {
		factor = 1

		mult_modifier = {
			factor = 2
			FROM = { had_offmap_tmp_flag = { flag = china_mongol_invasion years >= 2 } }
		}
		mult_modifier = {
			factor = 2
			FROM = { had_offmap_tmp_flag = { flag = china_mongol_invasion years >= 4 } }
		}
		mult_modifier = {
			factor = 2
			FROM = { had_offmap_tmp_flag = { flag = china_mongol_invasion years >= 8 } }
		}
		mult_modifier = {
			factor = 5
			FROM = { had_offmap_tmp_flag = { flag = china_mongol_invasion years >= 10 } }
		}
	}

	immediate = {
		random_list = {
			100 = { # China won!
				mult_modifier = {
					factor = 3
					FROM = { prev_status = china_golden_age }
				}

				FROM = {
					set_offmap_flag = no_status_news
					set_status = china_stable
					clr_offmap_flag = no_status_news

					trigger_switch = {
						on_trigger = has_offmap_flag

						china_badly_beaten_1 = {
							clr_offmap_flag = china_badly_beaten_1
						}

						china_badly_beaten_2 = {
							clr_offmap_flag = china_badly_beaten_2
							set_offmap_flag = china_badly_beaten_1
						}

						china_badly_beaten_3 = {
							clr_offmap_flag = china_badly_beaten_3
							set_offmap_flag = china_badly_beaten_2
						}
					}
				}

				any_player = {
					limit = {
						has_offmap_news_enabled = FROM
						is_within_diplo_range = ROOT
					}

					narrative_event = { id = JD.50161 }
				}
			}

			100 = { # Mongols won!
				mult_modifier = {
					factor = 3

					FROM = {
						OR = {
							prev_status = china_unrest
							prev_status = china_famine
							prev_status = china_plague
						}
					}
				}

				mult_modifier = { # The Borjigins are more likely to win
					factor = 5
					NOT = { has_global_flag = borjigin_china }

					any_independent_ruler = {
						OR = {
							dynasty = 11100
							dynasty = 1051210
						}

						has_landed_title = e_mongol_empire
						ai = yes
					}
				}

				FROM = {
					set_offmap_flag = no_status_news
					set_status = china_unrest
					set_offmap_flag = china_had_unrest
					clr_offmap_flag = no_status_news

					set_offmap_flag = china_expansionist_trend # Likely for the new ruler to pick Expansionist policy
					clr_offmap_flag = china_badly_beaten_3
					clr_offmap_flag = china_badly_beaten_2
					clr_offmap_flag = china_badly_beaten_1

					if = {
						limit = { has_offmap_flag = china_mongols_won_2 }
						set_offmap_flag = china_mongols_won_3
						clr_offmap_flag = china_mongols_won_2
					}
					else_if = {
						limit = { has_offmap_flag = china_mongols_won_1 }
						set_offmap_flag = china_mongols_won_2
						clr_offmap_flag = china_mongols_won_1
					}
					else_if = {
						limit = {
							NOT = { has_offmap_flag = china_mongols_won_3 }
						}

						set_offmap_flag = china_mongols_won_1
					}

					set_offmap_flag = no_succession_news
				}

				if = { # If the Borjigin's are around, it's guaranteed to be a Borjigin emperor
					limit = {
						NOT = { has_global_flag = borjigin_china }

						any_independent_ruler = {
							OR = {
								dynasty = 11100
								dynasty = 1051210
							}

							has_landed_title = e_mongol_empire
							ai = yes
						}
					}

					random_independent_ruler = {
						limit = {
							OR = {
								dynasty = 11100
								dynasty = 1051210
							}

							has_landed_title = e_mongol_empire
							ai = yes
						}
						save_event_target_as = mongol_china_invader
					}
				}

				# A new ruler ascends to the Dragon Throne. The fallout is handled by JD.10005 and JD.50000 (from 'on_offmap_ruler_changed')
				random_list = {
					100 = {
						mult_modifier = {
							factor = 2
							year >= 1200
						}

						if = { # Borjigin Emperor if they are around
							limit = {
								NOT = { has_global_flag = borjigin_china }
								event_target:mongol_china_invader = { is_alive = yes }
							}

							random_list = {
								50 = {
									trigger = { has_gender_equal_game_rule_trigger = yes }

									create_character = {
										age = 20
										random_traits = yes
										female = yes
										religion = event_target:mongol_china_invader
										culture = event_target:mongol_china_invader
										dynasty = event_target:mongol_china_invader
										add_trait = brilliant_strategist

										attributes = {
											martial = 10
										}
									}
								}
								50 = {
									create_character = {
										age = 20
										random_traits = yes
										female = no
										religion = event_target:mongol_china_invader
										culture = event_target:mongol_china_invader
										dynasty = event_target:mongol_china_invader
										add_trait = brilliant_strategist

										attributes = {
											martial = 10
										}
									}
								}
							}
							new_character = {
								if = {
									limit = {
										event_target:mongol_china_invader = { age >= 36 }
									}

									set_father = event_target:mongol_china_invader
								}
								else_if = {
									limit = {
										event_target:mongol_china_invader = {
											age < 36
											father_even_if_dead = { age >= 36 }
										}
									}

									event_target:mongol_china_invader = {
										father_even_if_dead = {
											save_event_target_as = mongol_china_invader_father
										}
									}

									set_father = event_target:mongol_china_invader_father
								}
								else = {
									create_character = {
										age = 60
										random_traits = yes
										female = no
										religion = event_target:mongol_china_invader
										culture = event_target:mongol_china_invader
										dynasty = event_target:mongol_china_invader
									}

									new_character = {
										death = { death_reason = death_in_china_historic }
										save_event_target_as = new_china_ruler_father
									}

									set_father = event_target:new_china_ruler_father
								}

								FROM = {
									set_offmap_holder = PREV
								}
							}

							offmap_china = { # Mongols take all western protectorate lands for themselves
								governor = {
									any_vassal = {
										set_defacto_liege = event_target:mongol_china_invader
									}

									any_demesne_title = {
										limit = { lower_real_tier_than = EMPEROR }
										grant_title = event_target:mongol_china_invader
									}
								}
							}

							event_target:mongol_china_invader = { # The Borjigin who invaded China gets a bunch of Grace, Prestige, Gold, a Peace Deal and a few Siege Engineers when winning
								wealth = 1000
								prestige = 1000

								add_offmap_currency = {
									offmap = offmap_china
									value = 5000
								}
								add_character_modifier = {
									name = peace_deal_with_china
									years = 50
									inherit = yes
								}

								create_random_soldier = {
									age = 30
									dynasty = actually_culture
									religion = ROOT
									culture = ROOT
									female = no
									random_traits = yes

									attributes = {
										diplomacy = 2
										learning = 2
										stewardship = 2
										intrigue = 2
										martial = 12
									}
								}

								new_character = {
									set_character_flag = originated_from_chinese_court
									set_character_flag = no_court_invites
									set_character_flag = taught_chinese_strategy

									remove_trait = weak
									remove_trait = slow
									remove_trait = imbecile
									remove_trait = dull
									remove_trait = craven

									add_trait = sapper
								}

								create_random_soldier = {
									age = 30
									dynasty = actually_culture
									religion = ROOT
									culture = ROOT
									female = no
									random_traits = yes

									attributes = {
										diplomacy = 2
										learning = 2
										stewardship = 2
										intrigue = 2
										martial = 12
									}
								}

								new_character = {
									set_character_flag = originated_from_chinese_court
									set_character_flag = no_court_invites
									set_character_flag = taught_chinese_strategy

									remove_trait = weak
									remove_trait = slow
									remove_trait = imbecile
									remove_trait = dull
									remove_trait = craven

									add_trait = sapper
								}

								create_random_soldier = {
									age = 30
									dynasty = actually_culture
									religion = ROOT
									culture = ROOT
									female = no
									random_traits = yes

									attributes = {
										diplomacy = 2
										learning = 2
										stewardship = 2
										intrigue = 2
										martial = 12
									}
								}

								new_character = {
									set_character_flag = originated_from_chinese_court
									set_character_flag = no_court_invites
									set_character_flag = taught_chinese_strategy

									remove_trait = weak
									remove_trait = slow
									remove_trait = imbecile
									remove_trait = dull
									remove_trait = craven

									add_trait = sapper
								}
							}

							set_global_flag = borjigin_china
						}
						else = { # If no Borjigins, business as usual
							random_list = {
								50 = {
									trigger = { has_gender_equal_game_rule_trigger = yes }

									create_character = {
										age = 35
										random_traits = yes
										female = yes
										religion = taoist # A bit strange, but necessary
										culture = mongol
										dynasty = actually_culture
										add_trait = brilliant_strategist

										attributes = {
											martial = 10
										}
									}
								}

								50 = {
									create_character = {
										age = 35
										random_traits = yes
										female = no
										religion = taoist # A bit strange, but necessary
										culture = mongol
										dynasty = actually_culture
										add_trait = brilliant_strategist

										attributes = {
											martial = 10
										}
									}
								}
							}

							new_character = {
								create_character = {
									age = 60
									random_traits = yes
									female = no
									religion = THIS
									culture = THIS
									dynasty = THIS
								}

								new_character = {
									death = { death_reason = death_in_china_historic }
									save_event_target_as = new_china_ruler_father
								}

								set_father = event_target:new_china_ruler_father

								FROM = {
									set_offmap_holder = PREV
								}

								random_list = {
									100 = { }
									 10 = { religion = tengri_pagan }
									 10 = { religion = buddhist }
									  5 = { religion = nestorian }
									  5 = { religion = sunni }
								}
							}
						}
					}

					35 = {
						trigger = {
							NOT = { event_target:mongol_china_invader = { is_alive = yes } }
						}

						mult_modifier = {
							factor = 3
							year < 1200
						}

						random_list = {
							50 = {
								trigger = { has_gender_equal_game_rule_trigger = yes }

								create_character = {
									age = 35
									random_traits = yes
									female = yes
									religion = taoist # A bit strange, but necessary
									culture = khitan
									dynasty = actually_culture
									add_trait = brilliant_strategist

									attributes = {
										martial = 10
									}
								}
							}

							50 = {
								create_character = {
									age = 35
									random_traits = yes
									female = no
									religion = taoist # A bit strange, but necessary
									culture = khitan
									dynasty = actually_culture
									add_trait = brilliant_strategist

									attributes = {
										martial = 10
									}
								}
							}
						}

						new_character = {
							create_character = {
								age = 60
								random_traits = yes
								female = no
								religion = THIS
								culture = THIS
								dynasty = THIS
							}

							new_character = {
								death = { death_reason = death_in_china_historic }
								save_event_target_as = new_china_ruler_father
							}

							set_father = event_target:new_china_ruler_father

							FROM = {
								set_offmap_holder = PREV
							}

							random_list = {
								100 = { }
								 10 = { religion = tengri_pagan }
								 10 = { religion = buddhist }
								  5 = { religion = nestorian }
								  5 = { religion = sunni }
							}
						}
					}

					25 = {
						trigger = {
							NOT = { event_target:mongol_china_invader = { is_alive = yes } }
						}

						random_list = {
							50 = {
								trigger = { has_gender_equal_game_rule_trigger = yes }

								create_character = {
									age = 35
									random_traits = yes
									female = yes
									religion = taoist # A bit strange, but necessary
									culture = uyghur
									dynasty = actually_culture
									add_trait = brilliant_strategist

									attributes = {
										martial = 10
									}
								}
							}

							50 = {
								create_character = {
									age = 35
									random_traits = yes
									female = no
									religion = taoist # A bit strange, but necessary
									culture = uyghur
									dynasty = actually_culture
									add_trait = brilliant_strategist

									attributes = {
										martial = 10
									}
								}
							}
						}

						new_character = {
							create_character = {
								age = 60
								random_traits = yes
								female = no
								religion = THIS
								culture = THIS
								dynasty = THIS
							}

							new_character = {
								death = { death_reason = death_in_china_historic }
								save_event_target_as = new_china_ruler_father
							}

							set_father = event_target:new_china_ruler_father

							FROM = {
								set_offmap_holder = PREV
							}

							random_list = {
								100 = { }
								 10 = { religion = tengri_pagan }
								 10 = { religion = buddhist }
								  5 = { religion = nestorian }
								  5 = { religion = sunni }
							}
						}
					}

					25 = {
						trigger = {
							NOT = { event_target:mongol_china_invader = { is_alive = yes } }
						}

						random_list = {
							50 = {
								trigger = { has_gender_equal_game_rule_trigger = yes }

								create_character = {
									age = 35
									random_traits = yes
									female = yes
									religion = taoist # A bit strange, but necessary
									culture = kirghiz
									dynasty = actually_culture
									add_trait = brilliant_strategist

									attributes = {
										martial = 10
									}
								}
							}

							50 = {
								create_character = {
									age = 35
									random_traits = yes
									female = no
									religion = taoist # A bit strange, but necessary
									culture = kirghiz
									dynasty = actually_culture
									add_trait = brilliant_strategist

									attributes = {
										martial = 10
									}
								}
							}
						}

						new_character = {
							create_character = {
								age = 60
								random_traits = yes
								female = no
								religion = THIS
								culture = THIS
								dynasty = THIS
							}

							new_character = {
								death = { death_reason = death_in_china_historic }
								save_event_target_as = new_china_ruler_father
							}

							set_father = event_target:new_china_ruler_father

							FROM = {
								set_offmap_holder = PREV
							}

							random_list = {
								100 = { }
								 10 = { religion = tengri_pagan }
								 10 = { religion = buddhist }
								  5 = { religion = nestorian }
								  5 = { religion = sunni }
							}
						}
					}
				}

				any_player = {
					limit = {
						has_offmap_news_enabled = FROM
						is_within_diplo_range = ROOT
					}

					narrative_event = { id = JD.50162 }
				}

				if = {
					limit = {
						NOR = {
							has_game_rule = {
								name = chinese_invasions
								value = none
							}

							has_game_rule = {
								name = adventurers
								value = none
							}
						}
					}

					random_list = {
						50 = {
							character_event = { id = JD.10113 days = 15 } #Spawns a Displaced Prince adventurer...
							set_character_flag = spawning_a_displaced_royal
							log = "China Logging:"
							log = "Attempting to spawn a Displaced Prince, as aftermath to event JD.50160 (end of Being Invaded by Mongols). Character should spawn in 15 days."
						}

						50 = {
							character_event = { id = JD.10109 days = 15 } #Spawns a Jurchen tribe leader...
							set_character_flag = spawning_a_jurchen_invader
							log = "China Logging:"
							log = "Attempting to spawn a Jurchen Invader, as aftermath to event JD.50160 (end of Being Invaded by Mongols). Character should spawn in 15 days."
						}
					}
				}
			}
		}
	}
}

# News from China: Mongol Invasion Fails
narrative_event = {
	id = JD.50161
	title = NEWS_FROM_CHINA
	desc = EVTDESC_JD_50161
	picture = GFX_evt_china_mongols_invade
	portrait = offmap_china
	window = EventWindowOffmap
	background = GFX_event_window_news_from_china

	is_triggered_only = yes

	has_dlc = "Jade Dragon"

	option = {
		name = EVTOPT_JD_50161
	}
}

# News from China: Mongol Invasion Succeeds
narrative_event = {
	id = JD.50162
	title = NEWS_FROM_CHINA
	desc = EVTDESC_JD_50162
	picture = GFX_evt_tengri_throneroom_oldgods
	portrait = offmap_china
	window = EventWindowOffmap
	background = GFX_event_window_news_from_china

	is_triggered_only = yes

	has_dlc = "Jade Dragon"

	option = {
		name = EVTOPT_JD_50162

		trigger = {
			NOT = { has_character_modifier = peace_deal_with_china }
		}
	}

	option = {
		name = EVTOPT_JD_50302_BROKEN_PEACE_DEAL

		trigger = { has_character_modifier = peace_deal_with_china }

		remove_character_modifier = peace_deal_with_china
	}
}

# Status changes to 'Jurchen Invasion'
# Hidden. Fired from on_offmap_yearly_pulse.
# Root = Governor
# From = Offmap
character_event = {
	id = JD.50180

	is_triggered_only = yes # on_offmap_yearly_pulse, random_events
	hide_window = yes

	has_dlc = "Jade Dragon"

	trigger = {
		FROM = {
			is_offmap_tag = offmap_china

			NOR = {
				offmap_ruler = { culture = jurchen }
				has_status = china_mongol_invasion # Resolution handled by JD.50160
				has_status = china_jurchen_invasion # Resolution handled by JD.50200
				has_status = china_civil_war # Resolution handled by JD.50300
			}

			trigger_if = {
				limit = { has_offmap_flag = china_invaded_player_dynasty }
				had_offmap_tmp_flag = { flag = china_invaded_player_dynasty years >= 200 }
			}

			# Famines last for at least 4 years
			trigger_if = {
				limit = {
					has_status = china_famine
					has_offmap_flag = china_had_famine
				}

				had_offmap_tmp_flag = { flag = china_had_famine years >= 4 }
			}
			# Plagues last for at least 2 years
			trigger_else_if = {
				limit = {
					has_status = china_plague
					has_offmap_flag = china_had_plague
				}

				had_offmap_tmp_flag = { flag = china_had_plague years >= 2 }
			}
		}
	}

	weight_multiplier = {
		factor = 1

		mult_modifier = { # Unrest increases the chance of an invasion
			factor = 3
			FROM = {
				has_status = china_unrest
				has_offmap_flag = china_had_unrest
				had_offmap_tmp_flag = { flag = china_had_unrest years >= 2 }
			}
		}
		mult_modifier = {
			factor = 1.5
			FROM = {
				has_status = china_unrest
				has_offmap_flag = china_had_unrest
				had_offmap_tmp_flag = { flag = china_had_unrest years >= 4 }
			}
		}
		mult_modifier = {
			factor = 1.5
			FROM = {
				has_status = china_unrest
				has_offmap_flag = china_had_unrest
				had_offmap_tmp_flag = { flag = china_had_unrest years >= 6 }
			}
		}
		mult_modifier = {
			factor = 1.25
			FROM = {
				has_status = china_famine
				has_offmap_flag = china_had_famine
				had_offmap_tmp_flag = { flag = china_had_famine years >= 6 }
			}
		}
		mult_modifier = {
			factor = 1.25
			FROM = {
				has_status = china_plague
				has_offmap_flag = china_had_plague
				had_offmap_tmp_flag = { flag = china_had_plague years >= 3 }
			}
		}
		mult_modifier = {
			factor = 1.5
			FROM = {
				has_status = china_famine
				has_offmap_flag = china_had_famine
				had_offmap_tmp_flag = { flag = china_had_famine years >= 8 }
			}
		}
		mult_modifier = {
			factor = 1.5
			FROM = {
				has_status = china_plague
				has_offmap_flag = china_had_plague
				had_offmap_tmp_flag = { flag = china_had_plague years >= 5 }
			}
		}
		mult_modifier = {
			factor = 2
			FROM = {
				has_status = china_famine
				has_offmap_flag = china_had_famine
				had_offmap_tmp_flag = { flag = china_had_famine years >= 10 }
			}
		}
		mult_modifier = {
			factor = 2
			FROM = {
				has_status = china_plague
				has_offmap_flag = china_had_plague
				had_offmap_tmp_flag = { flag = china_had_plague years >= 7 }
			}
		}
		mult_modifier = {
			factor = 0.5
			FROM = { has_status = china_golden_age }
		}
		mult_modifier = {
			factor = 0.5
			FROM = { has_offmap_flag = china_mongols_won_1 }
		}
		mult_modifier = {
			factor = 0.25
			FROM = { has_offmap_flag = china_mongols_won_2 }
		}
		mult_modifier = {
			factor = 0.1
			FROM = { has_offmap_flag = china_mongols_won_3 }
		}
	}

	immediate = {
		FROM = {
			set_offmap_flag = no_status_news
			set_status = china_jurchen_invasion
			set_offmap_flag = china_jurchen_invasion
			clr_offmap_flag = no_status_news
		}

		any_player = {
			limit = {
				has_offmap_news_enabled = FROM
				is_within_diplo_range = ROOT
			}

			narrative_event = { id = JD.50181 }
		}
	}
}

# News from China: Jurchen Invasion
narrative_event = {
	id = JD.50181
	title = NEWS_FROM_CHINA
	picture = GFX_evt_china_mongols_invade
	portrait = offmap_china
	window = EventWindowOffmap
	background = GFX_event_window_news_from_china

	desc = {
		text = EVTDESC_JD_50181_BOOST
		trigger = {
			FROMFROM = {
				OR = {
					prev_status = china_famine
					prev_status = china_unrest
					prev_status = china_plague
				}
			}
		}
	}
	desc = {
		text = EVTDESC_JD_50181_PENALTY
		trigger = {
			FROMFROM = {
				prev_status = china_golden_age
			}
		}
	}
	desc = {
		text = EVTDESC_JD_50031_JURCHENS # Can be the same as the standard status change notification
		trigger = {
			FROMFROM = {
				NOR = {
					prev_status = china_famine
					prev_status = china_unrest
					prev_status = china_plague
					prev_status = china_golden_age
				}
			}
		}
	}

	is_triggered_only = yes

	has_dlc = "Jade Dragon"

	option = {
		name = EVTOPT_JD_50141
	}
}

# Chance for an end to the 'Jurchen Invasion' Status
# Hidden. Fired from on_offmap_yearly_pulse
# Root = Governor
# From = Offmap
character_event = {
	id = JD.50200

	is_triggered_only = yes # on_offmap_yearly_pulse, random_events
	hide_window = yes

	has_dlc = "Jade Dragon"

	trigger = {
		FROM = {
			is_offmap_tag = offmap_china
			has_status = china_jurchen_invasion
		}
	}

	weight_multiplier = {
		factor = 1

		mult_modifier = {
			factor = 2
			FROM = { had_offmap_tmp_flag = { flag = china_jurchen_invasion years >= 2 } }
		}
		mult_modifier = {
			factor = 2
			FROM = { had_offmap_tmp_flag = { flag = china_jurchen_invasion years >= 4 } }
		}
		mult_modifier = {
			factor = 2
			FROM = { had_offmap_tmp_flag = { flag = china_jurchen_invasion years >= 8 } }
		}
		mult_modifier = {
			factor = 5
			FROM = { had_offmap_tmp_flag = { flag = china_jurchen_invasion years >= 10 } }
		}
	}

	immediate = {
		random_list = {
			100 = { # China won!
				mult_modifier = {
					factor = 3
					FROM = { prev_status = china_golden_age }
				}

				FROM = {
					set_offmap_flag = no_status_news
					set_status = china_stable
					clr_offmap_flag = no_status_news

					trigger_switch = {
						on_trigger = has_offmap_flag

						china_badly_beaten_1 = {
							clr_offmap_flag = china_badly_beaten_1
						}

						china_badly_beaten_2 = {
							clr_offmap_flag = china_badly_beaten_2
							set_offmap_flag = china_badly_beaten_1
						}

						china_badly_beaten_3 = {
							clr_offmap_flag = china_badly_beaten_3
							set_offmap_flag = china_badly_beaten_2
						}
					}
				}

				any_player = {
					limit = {
						has_offmap_news_enabled = FROM
						is_within_diplo_range = ROOT
					}

					narrative_event = { id = JD.50201 }
				}
			}

			100 = { # Jurchen won!
				mult_modifier = {
					factor = 3

					FROM = {
						OR = {
							prev_status = china_unrest
							prev_status = china_famine
							prev_status = china_plague
						}
					}
				}

				FROM = {
					set_offmap_flag = no_status_news
					set_status = china_unrest
					set_offmap_flag = china_had_unrest
					clr_offmap_flag = no_status_news

					clr_offmap_flag = china_badly_beaten_3
					clr_offmap_flag = china_badly_beaten_2
					clr_offmap_flag = china_badly_beaten_1

					if = {
						limit = { has_offmap_flag = china_mongols_won_2 }
						set_offmap_flag = china_mongols_won_3
						clr_offmap_flag = china_mongols_won_2
					}

					else_if = {
						limit = { has_offmap_flag = china_mongols_won_1 }
						set_offmap_flag = china_mongols_won_2
						clr_offmap_flag = china_mongols_won_1
					}

					else_if = {
						limit = {
							NOT = { has_offmap_flag = china_mongols_won_3 }
						}
						set_offmap_flag = china_mongols_won_1
					}

					set_offmap_flag = no_succession_news
				}

				# A new ruler ascends to the Dragon Throne. The fallout is handled by JD.10005 and JD.50000 (from 'on_offmap_ruler_changed')
				create_character = { #The father
					age = 60
					random_traits = yes
					female = no
					religion = THIS
					culture = jurchen
					dynasty = THIS
				}

				new_character = {
					PREV = { set_father = PREV }
					death = { death_reason = death_in_china_historic }
				}

				random_list = {
					50 = {
						trigger = { has_gender_equal_game_rule_trigger = yes }

						create_character = {
							age = 30
							random_traits = yes
							female = yes
							religion = taoist # A bit strange, but necessary
							culture = jurchen
							dynasty = actually_culture
							add_trait = brilliant_strategist

							attributes = {
								martial = 10
							}
						}
					}

					50 = {
						create_character = {
							age = 30
							random_traits = yes
							female = no
							religion = taoist # A bit strange, but necessary
							culture = jurchen
							dynasty = actually_culture
							add_trait = brilliant_strategist

							attributes = {
								martial = 10
							}
						}
					}
				}

				new_character = {
					create_character = {
						age = 60
						random_traits = yes
						female = no
						religion = THIS
						culture = THIS
						dynasty = THIS
					}

					new_character = {
						death = { death_reason = death_in_china_historic }
						save_event_target_as = new_china_ruler_father
					}

					set_father = event_target:new_china_ruler_father

					FROM = {
						set_offmap_holder = PREV
					}

					random_list = {
						100 = { }
						 10 = { religion = tengri_pagan }
						 10 = { religion = buddhist }
						  5 = { religion = nestorian }
						  5 = { religion = sunni }
					}
				}

				any_player = {
					limit = {
						has_offmap_news_enabled = FROM
						is_within_diplo_range = ROOT
					}

					narrative_event = { id = JD.50202 }
				}

				if = {
					limit = {
						NOR = {
							has_game_rule = {
								name = chinese_invasions
								value = none
							}

							has_game_rule = {
								name = adventurers
								value = none
							}
						}
					}

					random_list = {
						50 = {
							character_event = { id = JD.10113 days = 15 } #Spawns a Displaced Prince adventurer...
							set_character_flag = spawning_a_displaced_royal
							log = "China Logging:"
							log = "Attempting to spawn a Displaced Prince, as aftermath to event JD.50200 (end of Being Invaded by Jurchens). Character should spawn in 15 days."
						}

						50 = {
							character_event = { id = JD.10109 days = 15 } #Spawns a Jurchen tribe leader...
							set_character_flag = spawning_a_jurchen_invader
							log = "China Logging:"
							log = "Attempting to spawn a Jurchen Invader, as aftermath to event JD.50200 (end of Being Invaded by Mongols). Character should spawn in 15 days."
						}
					}
				}
			}
		}
	}
}

# News from China: Jurchen Invasion Fails
narrative_event = {
	id = JD.50201
	title = NEWS_FROM_CHINA
	desc = EVTDESC_JD_50201
	picture = GFX_evt_china_mongols_invade
	portrait = offmap_china
	window = EventWindowOffmap
	background = GFX_event_window_news_from_china

	is_triggered_only = yes

	has_dlc = "Jade Dragon"

	option = {
		name = EVTOPT_JD_50201
	}
}

# News from China: Jurchen Invasion Succeeds
narrative_event = {
	id = JD.50202
	title = NEWS_FROM_CHINA
	desc = EVTDESC_JD_50202
	picture = GFX_evt_tengri_throneroom_oldgods
	portrait = offmap_china
	window = EventWindowOffmap
	background = GFX_event_window_news_from_china

	is_triggered_only = yes

	has_dlc = "Jade Dragon"

	option = {
		name = EVTOPT_JD_50202

		trigger = {
			NOT = { has_character_modifier = peace_deal_with_china }
		}
	}

	option = {
		name = EVTOPT_JD_50302_BROKEN_PEACE_DEAL

		trigger = { has_character_modifier = peace_deal_with_china }

		remove_character_modifier = peace_deal_with_china
	}
}

# Status changes to 'Great Famine'
# Hidden. Fired from on_offmap_yearly_pulse.
# Root = Governor
# From = Offmap
character_event = {
	id = JD.50220

	is_triggered_only = yes # on_offmap_yearly_pulse, random_events
	hide_window = yes

	has_dlc = "Jade Dragon"

	trigger = {
		FROM = {
			is_offmap_tag = offmap_china

			NOR = {
				has_status = china_famine
				has_status = china_mongol_invasion # Resolution handled by JD.50160
				has_status = china_jurchen_invasion # Resolution handled by JD.50200
				has_status = china_civil_war # Resolution handled by JD.50300
			}

			# Famines don't tend to happen too close to each other
			trigger_if = {
				limit = { has_offmap_flag = china_had_famine }
				had_offmap_tmp_flag = { flag = china_had_famine years >= 90 }
			}
			# Golden Ages tend to not end with a Famine or Plague
			trigger_if = {
				limit = {
					has_status = china_golden_age
					has_offmap_flag = china_had_golden_age
				}

				had_offmap_tmp_flag = { flag = china_had_golden_age years >= 50 }
			}
		}
	}

	weight_multiplier = {
		factor = 1

		mult_modifier = { # Slightly less likely to get unrest if things are fine
			factor = 0.75

			FROM = {
				OR = {
					has_status = china_stable
					has_status = china_golden_age
				}
			}
		}
	}

	immediate = {
		FROM = {
			set_offmap_flag = china_had_famine
			set_status = china_famine # Players will be notified by JD.50030
		}
	}
}

# Status changes to 'Devastating Plague'
# Hidden. Fired from on_offmap_yearly_pulse.
# Root = Governor
# From = Offmap
character_event = {
	id = JD.50240

	has_dlc = "Jade Dragon"

	is_triggered_only = yes # on_offmap_yearly_pulse, random_events
	hide_window = yes

	trigger = {
		FROM = {
			is_offmap_tag = offmap_china

			NOR = {
				has_status = china_plague
				has_status = china_mongol_invasion # Resolution handled by JD.50160
				has_status = china_jurchen_invasion # Resolution handled by JD.50200
				has_status = china_civil_war # Resolution handled by JD.50300
			}

			# Famines don't tend to happen too close to each other
			trigger_if = {
				limit = { has_offmap_flag = china_had_plague }
				had_offmap_tmp_flag = { flag = china_had_plague years >= 90 }
			}
			# Golden Ages tend to not end with a Famine or Plague
			trigger_if = {
				limit = {
					has_status = china_golden_age
					has_offmap_flag = china_had_golden_age
				}

				had_offmap_tmp_flag = { flag = china_had_golden_age years >= 50 }
			}
		}
	}

	weight_multiplier = {
		factor = 1

		mult_modifier = { # Slightly less likely to get unrest if things are fine
			factor = 0.75

			FROM = {
				OR = {
					has_status = china_stable
					has_status = china_golden_age
				}
			}
		}
	}

	immediate = {
		FROM = {
			set_offmap_flag = china_had_plague
			set_status = china_plague # Players will be notified by JD.50030
		}
	}
}

# Status changes to 'Golden Age'
# Hidden. Fired from on_offmap_yearly_pulse.
# Root = Governor
# From = Offmap
character_event = {
	id = JD.50260

	has_dlc = "Jade Dragon"

	is_triggered_only = yes # on_offmap_yearly_pulse, random_events
	hide_window = yes

	trigger = {
		FROM = {
			is_offmap_tag = offmap_china
			has_status = china_stable

			# China tends to not enter Golden Ages frequently
			trigger_if = {
				limit = { has_offmap_flag = china_had_golden_age }
				had_offmap_tmp_flag = { flag = china_had_golden_age years >= 200 }
			}
		}
	}

	weight_multiplier = {
		factor = 1

		mult_modifier = { # But they tend to have one at least every 250 years
			factor = 2

			FROM = {
				has_offmap_flag = china_had_golden_age
				had_offmap_tmp_flag = { flag = china_had_golden_age years >= 250 }
			}
		}
	}

	immediate = {
		FROM = {
			set_offmap_flag = china_had_golden_age
			set_status = china_golden_age # Players will be notified by JD.50030
		}
	}
}

# Status changes to 'Civil War'
# Hidden. Fired from on_offmap_yearly_pulse.
# Root = Governor
# From = Offmap
character_event = {
	id = JD.50280

	has_dlc = "Jade Dragon"

	is_triggered_only = yes # on_offmap_yearly_pulse, random_events
	hide_window = yes

	trigger = {
		FROM = {
			is_offmap_tag = offmap_china

			NOR = {
				has_status = china_mongol_invasion # Resolution handled by JD.50160
				has_status = china_jurchen_invasion # Resolution handled by JD.50200
				has_status = china_civil_war # Resolution handled by JD.50300
			}

			trigger_if = {
				limit = { has_offmap_flag = china_invaded_player_dynasty }
				had_offmap_tmp_flag = { flag = china_invaded_player_dynasty years >= 200 }
			}

			# Unrest last for at least 2 years before becoming Stable/Civil War
			trigger_if = {
				limit = {
					has_status = china_unrest
					has_offmap_flag = china_had_unrest
				}
				had_offmap_tmp_flag = { flag = china_had_unrest years >= 2 }
			}
		}
	}

	weight_multiplier = {
		factor = 1

		mult_modifier = {
			factor = 0.01
			FROM = { has_status = china_golden_age }
		}
		mult_modifier = {
			factor = 0.1
			FROM = { has_status = china_stable }
		}
		mult_modifier = {
			factor = 0.1
			FROM = { NOT = { has_status = china_unrest } }
		}
	}

	immediate = {
		FROM = {
			set_offmap_flag = no_status_news
			set_status = china_civil_war
			set_offmap_flag = china_civil_war
			clr_offmap_flag = no_status_news
		}

		any_player = {
			limit = {
				has_offmap_news_enabled = FROM
				is_within_diplo_range = ROOT
			}

			narrative_event = { id = JD.50281 }
		}
	}
}

# News from China: Civil War
narrative_event = {
	id = JD.50281
	title = NEWS_FROM_CHINA
	picture = GFX_evt_china_civil_war
	portrait = offmap_china
	window = EventWindowOffmap
	background = GFX_event_window_news_from_china

	desc = {
		text = EVTDESC_JD_50281_UNREST
		trigger = {
			FROMFROM = {
				prev_status = china_unrest
			}
		}
	}
	desc = {
		text = EVTDESC_JD_50031_CW # Can use the default description
		trigger = {
			FROMFROM = {
				NOT = { prev_status = china_unrest }
			}
		}
	}

	is_triggered_only = yes

	has_dlc = "Jade Dragon"

	option = {
		name = EVTOPT_JD_50281
	}
}

# Chance for an end to the 'Civil War' Status
# Hidden. Fired from on_offmap_yearly_pulse
# Root = Governor
# From = Offmap
character_event = {
	id = JD.50300

	is_triggered_only = yes # on_offmap_yearly_pulse, random_events
	hide_window = yes

	has_dlc = "Jade Dragon"

	trigger = {
		FROM = {
			is_offmap_tag = offmap_china
			has_status = china_civil_war
			had_offmap_tmp_flag = { flag = china_civil_war years >= 2 }
		}
	}

	weight_multiplier = {
		factor = 1

		mult_modifier = {
			factor = 2
			FROM = { had_offmap_tmp_flag = { flag = china_civil_war years >= 3 } }
		}
		mult_modifier = {
			factor = 2
			FROM = { had_offmap_tmp_flag = { flag = china_civil_war years >= 5 } }
		}
		mult_modifier = {
			factor = 2
			FROM = { had_offmap_tmp_flag = { flag = china_civil_war years >= 8 } }
		}
		mult_modifier = {
			factor = 5
			FROM = { had_offmap_tmp_flag = { flag = china_civil_war years >= 10 } }
		}
	}

	immediate = {
		clr_offmap_flag = china_civil_war

		random_list = {
			70 = { # The loyalists won!
				FROM = {
					set_offmap_flag = no_status_news
					set_status = china_stable
					clr_offmap_flag = no_status_news
				}

				any_player = {
					limit = {
						has_offmap_news_enabled = FROM
						is_within_diplo_range = ROOT
					}

					narrative_event = { id = JD.50301 }
				}

				if = {
					limit = {
						NOR = {
							has_game_rule = {
								name = chinese_invasions
								value = none
							}

							has_game_rule = {
								name = adventurers
								value = none
							}
						}
					}

					random_list = {
						50 = {
							character_event = { id = JD.10109 days = 15 } #Spawns a Jurchen tribe leader...
							set_character_flag = spawning_a_jurchen_invader
							log = "China Logging:"
							log = "Attempting to spawn a Jurchen Invader, as aftermath to event JD.50300 (end of Civil War - the loyalists won). Character should spawn in 15 days."
						}

						50 = {
							trigger = {
								trigger_if = {
									limit = { has_global_flag = rebel_general_invasion_success }
									had_global_flag = {  flag = rebel_general_invasion_success years >= 150 }
								}
							}

							character_event = { id = JD.60200 days = 15 } #Spawns a Rebel General
							set_character_flag = spawning_a_rebel_general
							log = "China Logging:"
							log = "Attempting to spawn a Rebel General, as aftermath to event JD.50300 (end of Civil War - the loyalists won). Character should spawn in 15 days."
						}
					}
				}
			}
			100 = { # The rebels won - new Dynasty!
				mult_modifier = {
					factor = 2.5

					FROM = {
						holder_scope = {
							NOT = { culture_group = chinese_group } # A foreign dynasty is more likely to be ousted
						}
					}
				}

				FROM = {
					set_offmap_flag = no_status_news

					random_list = {
						50 = { set_status = china_stable }
						50 = { set_status = china_unrest set_offmap_flag = china_had_unrest }
					}

					clr_offmap_flag = no_status_news
					set_offmap_flag = no_succession_news

					if = {
						limit = {
							holder_scope = {
								NOT = { culture_group = chinese_group }
							}
						}

						set_offmap_flag = foreign_dynasty_ousted
					}
				}

				# A new ruler ascends to the Dragon Throne. The fallout is handled by JD.10005 and JD.50000 (from 'on_offmap_ruler_changed')
				random_list = {
					50 = {
						trigger = { has_gender_equal_game_rule_trigger = yes }

						create_character = {
							age = 30
							random_traits = yes
							female = yes
							religion = taoist # A bit strange, but necessary
							culture = han
							dynasty = actually_culture
							add_trait = brilliant_strategist

							attributes = {
								martial = 10
							}
						}
					}

					50 = {
						create_character = {
							age = 30
							random_traits = yes
							female = no
							religion = taoist # A bit strange, but necessary
							culture = han
							dynasty = actually_culture
							add_trait = brilliant_strategist

							attributes = {
								martial = 10
							}
						}
					}
				}

				new_character = {
					create_character = {
						age = 60
						random_traits = yes
						female = no
						religion = THIS
						culture = THIS
						dynasty = THIS
					}

					new_character = {
						death = { death_reason = death_in_china_historic }
						save_event_target_as = new_china_ruler_father
					}

					set_father = event_target:new_china_ruler_father

					FROM = {
						set_offmap_holder = PREV
					}
				}

				any_player = {
					limit = {
						has_offmap_news_enabled = FROM
						is_within_diplo_range = ROOT
					}

					narrative_event = { id = JD.50302 }
				}

				FROM = { clr_offmap_flag = foreign_dynasty_ousted }

				if = {
					limit = {
						NOR = {
							has_game_rule = {
								name = chinese_invasions
								value = none
							}

							has_game_rule = {
								name = adventurers
								value = none
							}
						}
					}

					random_list = {
						50 = {
							character_event = { id = JD.10113 days = 15 } #Spawns a Displaced Prince adventurer...
							set_character_flag = spawning_a_displaced_royal
							log = "China Logging:"
							log = "Attempting to spawn a Displaced Prince, as aftermath to event JD.50300 (end of Civil War - the rebels won). Character should spawn in 15 days."
						}

						50 = {
							character_event = { id = JD.10109 days = 15 } #Spawns a Jurchen tribe leader...
							set_character_flag = spawning_a_jurchen_invader
							log = "China Logging:"
							log = "Attempting to spawn a Jurchen Invader, as aftermath to event JD.50300 (end of Civil War - the rebels won). Character should spawn in 15 days."
						}
					}
				}
			}
			70 = { # The rebels won - old Dynasty pretender
				mult_modifier = {
					factor = 0.5

					FROM = {
						holder_scope = {
							NOT = { culture_group = chinese_group }
						}
					}
				}

				FROM = {
					set_offmap_flag = no_status_news
					set_status = china_stable
					clr_offmap_flag = no_status_news
					set_offmap_flag = no_succession_news

					holder_scope = {
						death = { death_reason = death_offmap }
					}
				}

				any_player = {
					limit = {
						has_offmap_news_enabled = FROM
						is_within_diplo_range = ROOT
					}

					narrative_event = { id = JD.50303 days = 1 }
				}

				if = {
					limit = {
						NOR = {
							has_game_rule = {
								name = chinese_invasions
								value = none
							}

							has_game_rule = {
								name = adventurers
								value = none
							}
						}
					}

					random = {
						chance = 90
						character_event = { id = JD.10109 days = 15 } #Spawns a Jurchen tribe leader...
						set_character_flag = spawning_a_jurchen_invader
						log = "China Logging:"
						log = "Attempting to spawn a Jurchen Invader, as aftermath to event JD.50300 (end of Civil War - the rebels won (old dynasty pretender)). Character should spawn in 15 days."
					}
				}
			}
		}
	}
}

# News from China: Civil War Ends - Loyalists Win!
narrative_event = {
	id = JD.50301
	title = NEWS_FROM_CHINA
	desc = EVTDESC_JD_50301
	picture = GFX_evt_china_civil_war
	portrait = offmap_china
	window = EventWindowOffmap
	background = GFX_event_window_news_from_china

	is_triggered_only = yes

	has_dlc = "Jade Dragon"

	option = {
		name = EVTOPT_JD_50301
	}
}

# News from China: Civil War Ends - Rebels Win, New Dynasty!
narrative_event = {
	id = JD.50302
	title = NEWS_FROM_CHINA
	picture = GFX_evt_china_civil_war
	portrait = offmap_china
	window = EventWindowOffmap
	background = GFX_event_window_news_from_china

	desc = {
		text = EVTDESC_JD_50302_HAN_DYNASTY
		trigger = {
			FROMFROM = {
				has_offmap_flag = foreign_dynasty_ousted
			}
		}
	}
	desc = {
		text = EVTDESC_JD_50302_NEW_DYNASTY
		trigger = {
			FROMFROM = {
				NOT = { has_offmap_flag = foreign_dynasty_ousted }
			}
		}
	}

	is_triggered_only = yes
	hide_new = yes

	has_dlc = "Jade Dragon"

	option = {
		name = EVTOPT_JD_50301

		trigger = {
			NOT = { has_character_modifier = peace_deal_with_china }
		}
	}

	option = {
		name = EVTOPT_JD_50302_BROKEN_PEACE_DEAL

		trigger = { has_character_modifier = peace_deal_with_china }

		remove_character_modifier = peace_deal_with_china
	}
}

# News from China: Civil War Ends - Rebels Win but Old Dynasty stays in power
narrative_event = {
	id = JD.50303
	title = NEWS_FROM_CHINA
	desc = EVTDESC_JD_50303
	picture = GFX_evt_china_civil_war
	portrait = offmap_china
	window = EventWindowOffmap
	background = GFX_event_window_news_from_china

	is_triggered_only = yes

	has_dlc = "Jade Dragon"

	option = {
		name = EVTOPT_JD_50301
	}
}

character_event = { # Update Silk Road values
	id = JD.50400

	is_triggered_only = yes # on_offmap_policy_changed, on_offmap_status_changed
	hide_window = yes

	has_dlc = "Jade Dragon"

	trigger = { FROM = { is_offmap_tag = offmap_china } }

	immediate = {
		FROM = {
			if = {
				limit = { has_status = china_golden_age }

				set_trade_route_value_multiplier = {
					which = silk_road
					value = 1.5
				}
			}
			else_if = {
				limit = { has_status = china_unrest }

				set_trade_route_value_multiplier = {
					which = silk_road
					value = 0.7
				}
			}
			else_if = {
				limit = {
					OR = {
						has_status = china_civil_war
						has_status = china_mongol_invasion
						has_status = china_jurchen_invasion
					}
				}

				set_trade_route_value_multiplier = {
					which = silk_road
					value = 0.4
				}
			}
			else_if = {
				limit = { has_status = china_famine }

				set_trade_route_value_multiplier = {
					which = silk_road
					value = 0.2
				}
			}
			else_if = {
				limit = { has_status = china_plague }

				set_trade_route_value_multiplier = {
					which = silk_road
					value = 0.15
				}
			}
			else_if = {
				limit = { has_policy = china_isolationist }

				set_trade_route_value_multiplier = {
					which = silk_road
					value = 0.1
				}
			}
			else = {
				set_trade_route_value_multiplier = {
					which = silk_road
					value = 1
				}
			}
		}
	}
}

character_event = { # Update Silk Road values on startup
	id = JD.50401

	is_triggered_only = yes # on_startup
	hide_window = yes

	has_dlc = "Jade Dragon"

	trigger = {
		is_multiplayer_host_character = yes
		is_save_game = no
	}

	immediate = {
		offmap_china = {
			if = {
				limit = { has_status = china_golden_age }

				set_trade_route_value_multiplier = {
					which = silk_road
					value = 2
				}
			}
			else_if = {
				limit = { has_status = china_unrest }

				set_trade_route_value_multiplier = {
					which = silk_road
					value = 0.75
				}
			}
			else_if = {
				limit = {
					OR = {
						has_status = china_civil_war
						has_status = china_mongol_invasion
						has_status = china_jurchen_invasion
					}
				}

				set_trade_route_value_multiplier = {
					which = silk_road
					value = 0.5
				}
			}
			else_if = {
				limit = { has_status = china_famine }

				set_trade_route_value_multiplier = {
					which = silk_road
					value = 0.25
				}
			}
			else_if = {
				limit = { has_status = china_plague }

				set_trade_route_value_multiplier = {
					which = silk_road
					value = 0.1
				}
			}
			else_if = {
				limit = { has_policy = china_isolationist }

				set_trade_route_value_multiplier = {
					which = silk_road
					value = 0
				}
			}
			else = {
				set_trade_route_value_multiplier = {
					which = silk_road
					value = 1
				}
			}
		}
	}
}

character_event = {
	id = JD.30500

	hide_window = yes
	is_triggered_only = yes # on_offmap_yearly_pulse

	trigger = { FROM = { is_offmap_tag = offmap_china } }

	immediate = {
		change_variable = {
			which = global_temp_status_years
			value = 1
		}

		change_variable = {
			which = global_temp_policy_years
			value = 1
		}
	}
}

character_event = {
	id = JD.30501

	hide_window = yes
	is_triggered_only = yes # on_offmap_policy_changed

	trigger = { FROM = { is_offmap_tag = offmap_china } }

	immediate = {
		FROM = {
			print_china_policy_update_effect = yes
		}
	}
}

character_event = {
	id = JD.30502

	hide_window = yes
	is_triggered_only = yes # on_offmap_status_changed

	trigger = { FROM = { is_offmap_tag = offmap_china } }

	immediate = {
		FROM = {
			print_china_status_update_effect = yes
		}
	}
}

character_event = {
	id = JD.30503

	hide_window = yes
	is_triggered_only = yes # on_offmap_ruler_changed

	trigger = { FROMFROM = { is_offmap_tag = offmap_china } }

	immediate = {
		print_china_ruler_update_effect = yes
	}
}

character_event = { # Clean up living members of the chinese dynasty
	id = JD.30504

	hide_window = yes
	is_triggered_only = yes # on_startup

	only_playable = yes

	trigger = {
		offmap_china = {
			governor = {
				character = ROOT
			}
		}

		is_save_game = no
	}

	immediate = {
		offmap_china = {
			offmap_ruler = {
				any_dynasty_member = {
					limit = {
						NOR = {
							is_offmap_ruler = yes
							is_ruler = yes
							dynasty = 11100
							dynasty = 1051210
						}
					}

					death = { death_reason = death_went_back_to_china }
				}
			}

			offmap_prev_ruler = {
				any_dynasty_member = {
					limit = {
						NOR = {
							is_offmap_ruler = yes
							is_ruler = yes
							dynasty = 11100
							dynasty = 1051210
						}
					}

					death = { death_reason = death_went_back_to_china }
				}
			}
		}
	}
}