namespace = ming_crisis

# Mandate Questioned
country_event = {
	id = ming_crisis.1
	title = "ming_crisis.1.t"
	desc = "ming_crisis.1.d"
	picture = CIVIL_WAR_eventPicture
	
	is_triggered_only = yes
	fire_only_once = yes
	
	immediate = {
		hidden_effect = {
			random_owned_province = {
				limit = {
					is_state = yes
					superregion = china_superregion
				}
				spawn_rebels = {
					type = anti_tax_rebels 
					size = 6
				}
			}
			random_owned_province = {
				limit = {
					is_state = yes
					superregion = china_superregion
				}
				spawn_rebels = {
					type = anti_tax_rebels 
					size = 6
				}
			}
			random_owned_province = {
				limit = {
					is_state = yes
					superregion = china_superregion
				}
				spawn_rebels = {
					type = anti_tax_rebels 
					size = 6
				}
			}
			random_owned_province = {
				limit = {
					is_state = yes
					superregion = china_superregion
				}
				spawn_rebels = {
					type = anti_tax_rebels 
					size = 6
				}
			}
			random_owned_province = {
				limit = {
					is_state = yes
					superregion = china_superregion
				}
				spawn_rebels = {
					type = anti_tax_rebels 
					size = 6
				}
			}
		}
	}
	
	option = {
		name = ming_crisis.1.a
		add_stability = -2
		add_mandate = -35
		add_corruption = 5
	}
}

# The Mandate Restored
country_event = {
	id = ming_crisis.2
	title = "ming_crisis.2.t"
	desc = "ming_crisis.2.d"
	picture = CIVIL_WAR_eventPicture
	
	is_triggered_only = yes
	
	option = {
		name = ming_crisis.2.a
		add_stability = 1
		add_mandate = 20
		
		if = { 
			limit = { 
				has_estate = estate_eunuchs 
				num_of_estate_privileges = { 
					estate = estate_eunuchs 
					value = 1 
				}
			}
			add_ruler_modifier = {
				name = mng_undisputed_mandate_modifier
				duration = -1
			}
		}
	}
}

# Li Zicheng's rebellion and the Shun dynasty
country_event = {
	id = ming_crisis.3
	title = "ming_crisis.3.t"
	desc = "ming_crisis.3.d"
	picture = ANGRY_MOB_eventPicture
	
	is_triggered_only = yes
	fire_only_once = yes
	major = yes
	
	trigger = {
		NOT = { exists = CSH }
		num_of_owned_provinces_with = {
			value = 10
			region = north_china_region
			controlled_by = REB
		}
		any_owned_province = {
			OR = {
				area = gansu_area
				area = west_gansu_area
				area = shaanxi_area
				area = shanxi_area
				area = hebei_area
				area = south_hebei_area
				area = liaoning_area
			}
		}
	}
	
	immediate = {
		hidden_effect = {
			every_owned_province = {
				limit = {
					OR = {
						controlled_by = REB
						controlled_by = ROOT
					}
					OR = {
						area = gansu_area
						area = west_gansu_area
						area = shaanxi_area
						area = shanxi_area
						area = hebei_area
						area = south_hebei_area
						area = liaoning_area
					}
					OR = {
						controlled_by = REB
						controlled_by = ROOT
					}
				}
				add_core = CSH
				clear_rebels = yes
			}
			every_owned_province = { #get rid of bordergore from random Mongolian provinces
				limit = {
					region = mongolia_region
					any_neighbor_province = {
						is_core = CSH
					}
					OR = {
						controlled_by = REB
						controlled_by = ROOT
					}
				}
				add_core = CSH
				clear_rebels = yes
			}
			release = CSH
			CSH = {
				capital_scope = {
					build_to_forcelimit = {
						infantry = 0.6
						cavalry = 0.3
						artillery = 0.1
					}
				}
				change_government = monarchy
				add_government_reform = chinese_warlord
				adopt_reform_progress = ROOT
			}
			if = {
				limit = {
					any_country = {
						NOT = { is_neighbor_of = ROOT }
						is_subject_of_type = tributary_state
						is_subject_of = ROOT
					}
				}
				every_country = {
					limit = {
						NOT = { is_neighbor_of = ROOT }
						is_subject_of_type = tributary_state
						is_subject_of = ROOT
					}
					country_event = {
						id = ming_crisis.10
					}
				}
			}
		}
	}
	
	option = {
		name = ming_crisis.3.a
		add_stability = -2
		add_mandate = -10
		CSH = {
			define_ruler = {
				name = "b"
				dynasty = "Ng"
			}
			add_yearly_manpower = 15
			add_years_of_income = 15
		}
		if = {
			limit = {
				has_dlc = "Mandate of Heaven"
				is_emperor_of_china = yes
			}
			CSH = {
				declare_war_with_cb = {
					who = root
					casus_belli = cb_take_mandate
				}
			}
		}
		else = {
			CSH = {
				declare_war = root
			}
		}
	}
	
	option = {
		name = ming_crisis.3.b
		trigger = {
			ai = no
		}
		add_stability = -2
		add_mandate = -10
		CSH = {
			define_ruler = {
				name = "b"
				dynasty = "Ng"
			}
			add_yearly_manpower = 15
			add_years_of_income = 15
		}
		if = {
			limit = {
				has_dlc = "Mandate of Heaven"
				is_emperor_of_china = yes
			}
			CSH = {
				declare_war_with_cb = {
					who = root
					casus_belli = cb_take_mandate
				}
			}
		}
		else = {
			CSH = {
				declare_war = root
			}
		}
		switch_tag = CSH
	}

	after = {
		hidden_effect = {
			CSH = {
				if = {
					limit = {
						any_neighbor_country = {
							has_reform = steppe_horde
							total_own_and_non_tributary_subject_development = 300
							is_subject = no
							#NOT = { truce_with = ROOT }
							NOT = { alliance_with = ROOT }
							#NOT = { is_subject_of = ROOT }
						}
					}
					random_neighbor_country = {
						limit = {
							has_reform = steppe_horde
							total_own_and_non_tributary_subject_development = 300
							is_subject = no
							#NOT = { truce_with = ROOT }
							NOT = { alliance_with = ROOT }
							#NOT = { is_subject_of = ROOT }
						}
						country_event = { id = ming_crisis.9 days = 1 }
					}
				}
				else_if = {
					limit = {
						any_neighbor_country = {
							tag = MCH
							is_free_or_tributary_trigger = yes
							NOT = { alliance_with = root }
						}
					}
					MCH = {
						country_event = { id = ming_crisis.9 days = 1 }
					}
				}
			}
		}
	}
}

# Yunnan Protectorate
country_event = {
	id = ming_crisis.4
	title = "ming_crisis.4.t"
	desc = "ming_crisis.4.d"
	picture = BAD_WITH_MONARCH_eventPicture
	
	is_triggered_only = yes
	fire_only_once = yes
	major = yes
	
	trigger = {
		NOT = { exists = CDL }
		num_of_owned_provinces_with = {
			value = 10
			region = xinan_region
			controlled_by = REB
		}
		any_owned_province = {
			OR = {
				area = yun_gui_area
				area = chuannan_area
				area = yun_gui_borderland_area
			}
		}
	}
	
	immediate = {
		hidden_effect = {
			every_owned_province = {
				limit = {
					OR = {
						controlled_by = REB
						controlled_by = ROOT
					}
					OR = {
						area = yun_gui_area
						area = chuannan_area
						area = yun_gui_borderland_area
					}
					OR = {
						controlled_by = REB
						controlled_by = ROOT
					}
					NOT = { is_core = CDL }
				}
				add_core = CDL
				clear_rebels = yes
			}
			release = CDL
			CDL = {
				capital_scope = {
					build_to_forcelimit = {
						infantry = 0.6
						cavalry = 0.3
						artillery = 0.1
					}
				}
				change_government = monarchy
				add_government_reform = chinese_warlord
				adopt_reform_progress = ROOT
			}
			if = {
				limit = {
					any_country = {
						NOT = { is_neighbor_of = ROOT }
						is_subject_of_type = tributary_state
						is_subject_of = ROOT
					}
				}
				every_country = {
					limit = {
						NOT = { is_neighbor_of = ROOT }
						is_subject_of_type = tributary_state
						is_subject_of = ROOT
					}
					country_event = {
						id = ming_crisis.10
					}
				}
			}
		}
	}
	
	option = {
		name = ming_crisis.4.a
		ai_chance = { factor = 1 }
		add_mandate = -5
		CDL = { add_liberty_desire = 100 }
	}
	option = {
		name = ming_crisis.4.b
		ai_chance = { factor = 0 }
		add_mandate = 15
		CDL = {
			declare_war_with_cb = {
				who = root
				casus_belli = cb_independence_war
			}
		}
	}
	option = {
		name = ming_crisis.4.c
		trigger = { ai = no }
		add_mandate = 15
		switch_tag = CDL
		CDL = {
			declare_war_with_cb = {
				who = root
				casus_belli = cb_independence_war
			}
		}
	}
}

# Losing Control in the South
country_event = {
	id = ming_crisis.5
	title = "ming_crisis.5.t"
	desc = "ming_crisis.5.d"
	picture = BAD_WITH_MONARCH_eventPicture
	
	is_triggered_only = yes
	fire_only_once = yes
	major = yes
	
	trigger = {
		OR = {
			NOT = { exists = WUU }
			NOT = { exists = YUE }
		}
		num_of_owned_provinces_with = {
			value = 10
			region = south_china_region
			controlled_by = REB
		}
		any_owned_province = {
			OR = {
				area = zhejiang_area
				area = fujian_area
				area = guangdong_area
				area = west_guangdong_area
				area = guangxi_area
			}
		}
	}
	
	immediate = {
		hidden_effect = {
			if = {
				limit = {
					NOT = {
						exists = WUU
					}
				}
				hidden_effect = {
					every_owned_province = {
						limit = {
							OR = {
								controlled_by = REB
								controlled_by = ROOT
							}
							OR = {
								area = zhejiang_area
								area = fujian_area
							}
							OR = {
								controlled_by = REB
								controlled_by = ROOT
							}
							NOT = { is_core = WUU }
						}
						add_core = WUU
						clear_rebels = yes
					}
					release = WUU
					WUU = {
						capital_scope = {
							build_to_forcelimit = {
								infantry = 0.6
								cavalry = 0.3
								artillery = 0.1
							}
						}
						change_government = monarchy
						add_government_reform = chinese_warlord
						adopt_reform_progress = ROOT
					}
				}
			}
			if = {
				limit = {
					NOT = {
						exists = YUE
					}
				}
				hidden_effect = {
					every_owned_province = {
						limit = {
							OR = {
								controlled_by = REB
								controlled_by = ROOT
							}
							OR = {
								area = guangdong_area
								area = west_guangdong_area
								area = guangxi_area
							}
							OR = {
								controlled_by = REB
								controlled_by = ROOT
							}
							NOT = { is_core = YUE }
						}
						add_core = YUE
						clear_rebels = yes
					}
					release = YUE
					YUE = {
						capital_scope = {
							build_to_forcelimit = {
								infantry = 0.6
								cavalry = 0.3
								artillery = 0.1
							}
						}
						change_government = monarchy
						add_government_reform = chinese_warlord
						adopt_reform_progress = ROOT
					}
				}
			}
			if = {
				limit = {
					any_country = {
						NOT = { is_neighbor_of = ROOT }
						is_subject_of_type = tributary_state
						is_subject_of = ROOT
					}
				}
				every_country = {
					limit = {
						NOT = { is_neighbor_of = ROOT }
						is_subject_of_type = tributary_state
						is_subject_of = ROOT
					}
					country_event = {
						id = ming_crisis.10
					}
				}
			}
		}		
	}
	
	option = {
		name = ming_crisis.5.a
		ai_chance = { factor = 1 }
		add_mandate = -5
		if = {
			limit = {
				exists = YUE
				overlord_of = YUE
			}
			YUE = { add_liberty_desire = 100 }
		}
		if = {
			limit = {
				exists = WUU
				overlord_of = WUU
			}
			WUU = { add_liberty_desire = 100 }
		}
	}
	option = {
		name = ming_crisis.5.b
		ai_chance = { factor = 0 }
		add_mandate = 15
		if = {
			limit = {
				exists = YUE
				YUE = {
					is_subject_of = ROOT
				}
			}
			YUE = {
				declare_war_with_cb = {
					who = root
					casus_belli = cb_independence_war
				}
			}
		}
		if = {
			limit = {
				exists = WUU
				WUU = {
					is_subject_of = ROOT
				}
			}
			WUU = {
				declare_war_with_cb = {
					who = root
					casus_belli = cb_independence_war
				}
			}
		}
	}
	option = {
		name = ming_crisis.5.c
		trigger = { ai = no }
		ai_chance = { factor = 0 }
		add_mandate = 15
		switch_tag = YUE
		if = {
			limit = {
				exists = YUE
				YUE = {
					is_subject_of = ROOT
				}
			}
			YUE = {
				declare_war_with_cb = {
					who = root
					casus_belli = cb_independence_war
				}
			}
		}
		if = {
			limit = {
				exists = WUU
				WUU = {
					is_subject_of = ROOT
				}
			}
			WUU = {
				declare_war_with_cb = {
					who = root
					casus_belli = cb_independence_war
				}
			}
		}
	}
	option = {
		name = ming_crisis.5.e
		trigger = { ai = no }
		ai_chance = { factor = 0 }
		add_mandate = 15
		switch_tag = WUU
		if = {
			limit = {
				exists = YUE
				YUE = {
					is_subject_of = ROOT
				}
			}
			YUE = {
				declare_war_with_cb = {
					who = root
					casus_belli = cb_independence_war
				}
			}
		}
		if = {
			limit = {
				exists = WUU
				WUU = {
					is_subject_of = ROOT
				}
			}
			WUU = {
				declare_war_with_cb = {
					who = root
					casus_belli = cb_independence_war
				}
			}
		}
	}
}

# Peasants revolt in [area]
country_event = {
	id = ming_crisis.6
	title = "ming_crisis.6.t"
	desc = "ming_crisis.6.d"
	picture = ANGRY_MOB_eventPicture
	goto = ming_disaster_rebel_province
	is_triggered_only = yes
	
	trigger = {
		any_owned_province = {
			is_state = yes
			superregion = china_superregion
			NOT = { local_autonomy = 75 }
		}
	}
	
	immediate = {
		hidden_effect = {
			random_owned_province = {
				limit = {
					is_state = yes
					superregion = china_superregion
					NOT = { local_autonomy = 75 }
				}
				save_event_target_as = ming_disaster_rebel_province
			}
		}
	}
	
	option = {
		name = ming_crisis.6.a
		ai_chance = { factor = 1 }
		add_mandate = 5
		event_target:ming_disaster_rebel_province = {
			spawn_rebels = {
				type = anti_tax_rebels 
				size = 6
			}
		}
	}
	option = {
		name = ming_crisis.6.b
		ai_chance = { factor = 0 }
		add_mandate = -10
		event_target:ming_disaster_rebel_province = {
			area = {
				limit = {
					owned_by = ROOT
				}
				add_local_autonomy = 50
			}
		}
	}
}


# Gatekeeper for ending depending on whether you kept the Mandate or not
country_event = {
	id = ming_crisis.7
	title = none
	desc = none
	picture = none
	hidden = yes
	
	is_triggered_only = yes
	fire_only_once = yes
	
	immediate = {
		on_non_estate_disaster_end = yes
		hidden_effect = {
			set_country_flag = ming_crisis_happened
		}
		if = {
			limit = {
				is_emperor_of_china = yes
			}
			country_event = { id = ming_crisis.2 }
		}
		else = {
			country_event = { id = ming_crisis.8 }
		}
	}
	option = { }
}

# The Mandate Restored
country_event = {
	id = ming_crisis.8
	title = "ming_crisis.8.t"
	desc = "ming_crisis.8.d"
	picture = CIVIL_WAR_eventPicture
	
	is_triggered_only = yes

	immediate = {
		hidden_effect = {
			random_country = {
				limit = {
					is_emperor_of_china = yes
				}
				save_event_target_as = emperor_of_china
			}
		}
	}
	
	option = {
		name = ming_crisis.8.a
		add_stability = 1
	}
}


# Manchu/Nomadic Intervention on Shun
country_event = {
	id = ming_crisis.9
	title = "ming_crisis.9.t"
	desc = "ming_crisis.9.d"
	picture = ANGRY_MOB_eventPicture
	
	is_triggered_only = yes
	major = yes
	
	option = {
		name = ming_crisis.9.A

		liaoning_area = {
			limit = {
				owned_by = CSH
				NOT = { is_claim = ROOT }
				NOT = { is_core = ROOT }
			}
			add_claim = ROOT
		}

		hebei_area = {
			limit = {
				owned_by = CSH
				NOT = { is_claim = ROOT }
				NOT = { is_core = ROOT }
			}
			add_claim = ROOT
		}

		south_hebei_area = {
			limit = {
				owned_by = CSH
				NOT = { is_claim = ROOT }
				NOT = { is_core = ROOT }
			}
			add_claim = ROOT
		}

		shanxi_area = {
			limit = {
				owned_by = CSH
				NOT = { is_claim = ROOT }
				NOT = { is_core = ROOT }
			}
			add_claim = ROOT
		}
		
		if = {
			limit = {
				has_casus_belli = {
					type = cb_horde_vs_civ
					target = CSH
				}
			}
			declare_war_with_cb = {
				who = CSH
				casus_belli = cb_horde_vs_civ
			}
		}
		else = {
			declare_war_with_cb = {
				who = CSH
				casus_belli = cb_conquest
			}
		}

		ai_chance = {
			factor = 5
			modifier = {
				factor = 0.67
				has_opinion = {
					who = from
					value = 10
				}
			}
			modifier = {
				factor = 0.5
				has_opinion = {
					who = from
					value = 50
				}
			}
			modifier = {
				factor = 0.2
				has_opinion = {
					who = from
					value = 100
				}
			}
			modifier = {
				factor = 0.5
				is_at_war = yes
				NOT = { war_score = 0 }
			}
			modifier = {
				factor = 0
				is_at_war = yes
				NOT = { war_score = -15 }
			}
			modifier = {
				factor = 0.2
				is_in_large_debt = yes
			}
		}
	}

	option = {
		name = ming_crisis.9.B

		add_prestige = -20

		ai_chance = {
			factor = 1
			modifier = {
				factor = 0.01
				OR = {
					ai_attitude = {
						who = from
						attitude = attitude_hostile
					}
					is_rival = from
				}
			}
		}
	}
}

country_event = {
	id = ming_crisis.10
	title = "ming_crisis.10.t"
	desc = "ming_crisis.10.d"
	picture = CIVIL_WAR_eventPicture
	
	is_triggered_only = yes
	
	option = {
		name = ming_crisis.10.a
		grant_independence = yes
		ai_chance = {
			factor = 1
		}
	}
	option = {
		name = ming_crisis.10.b
		custom_tooltip = ming_crisis.10.tt
		ai_chance = {
			factor = 0
		}
	}
}
	
# Ming falls apart!
country_event = {
	id = ming_crisis.11
	title = "ming_crisis.11.t"
	desc = "ming_crisis.11.d"
	picture = ANGRY_MOB_eventPicture
	goto = ming_disaster_nationalist_province
	is_triggered_only = yes
	
	trigger = {
		any_owned_province = {
			superregion = china_superregion
			any_core_country = {
				NOT = { tag = ROOT }
				NOT = { tag = CSH }
				NOT = { tag = WUU }
				NOT = { tag = YUE }
				exists = no
			}
		}
	}
	
	immediate = {
		hidden_effect = {
			random_owned_province = {
				limit = {
					superregion = china_superregion
					any_core_country = {
						NOT = { tag = ROOT }
						NOT = { tag = CSH }
						NOT = { tag = WUU }
						NOT = { tag = YUE }
						exists = no
					}
				}
				save_event_target_as = ming_disaster_nationalist_province
				random_core_country = {
					limit = {
						NOT = { tag = ROOT }
						NOT = { tag = CSH }
						NOT = { tag = WUU }
						NOT = { tag = YUE }
						exists = no
					}
					save_event_target_as = ming_disaster_new_tag
				}
			}
		}
	}
	
	option = {
		name = ming_crisis.11.a
		add_mandate = 5
		event_target:ming_disaster_new_tag = {
			event_target:ming_disaster_nationalist_province = {
				spawn_rebels = {
					type = nationalist_rebels
					friend = PREV
					size = 5
				}
			}
		}
		ai_chance = {
			factor = 100
		}
	}
	
	option = {
		name = ming_crisis.11.b
		add_mandate = -5
		event_target:ming_disaster_nationalist_province = {
			area = {
				limit = {
					owned_by = ROOT
				}
				add_local_autonomy = 10
			}
		}
		ai_chance = {
			factor = 0
		}
	}
}