﻿##################################################
# Dynastic Cycle Scripted Effects
##################################################

# Effect called when claiming the Mandate of Heaven
tgp_claim_mandate_of_heaven_effect = {
	save_temporary_scope_as = new_emperor_temp
	every_vassal = {
		limit = {
			any_held_title = {
				OR = {
					exists = var:ceremonial_title
					exists = var:administrative_ui_special_title
				}
			}
		}
		random_held_title = {
			limit = { exists = var:ceremonial_title }
			save_scope_as = ceremonial_title
		}
		save_temporary_scope_as = ceremonial_liege
		# TGP end the ceremonial liege
		# MESSAGE
		hidden_effect = {
			every_player = {
				limit = { top_liege = scope:ceremonial_liege.top_liege }
				send_interface_message = {
					type = event_generic_bad
					title = tgp_destroy_ceremonial_liege_throne_title
					left_icon = scope:ceremonial_liege
					right_icon = scope:new_emperor_temp
					show_as_tooltip = {
						scope:new_emperor_temp = { destroy_title = scope:ceremonial_title }
						scope:ceremonial_liege = { add_trait = former_emperor }
					}
				}
			}
		}
		# Actually destroy
		scope:ceremonial_liege = { add_trait = former_emperor }
		scope:new_emperor_temp = { destroy_title = scope:ceremonial_title }
	}
	set_variable = {
		name = claimed_mandate_var
		value = yes
		days = 30
	}
	# Grant the Hegemon title
	create_title_and_vassal_change = {
		type = created
		save_scope_as = mandate_change
		add_claim_on_loss = no
	}
	title:h_china = {
		change_title_holder = {
			holder = scope:new_emperor_temp
			change = scope:mandate_change
		}
	}
	resolve_title_and_vassal_change = scope:mandate_change
	# Adjust tributaries
	if = {
		limit = {
			number_of_tributaries > 0
		}
		# Vassalize tributaries within China (except if it's a player)
		create_title_and_vassal_change = {
			type = swear_fealty
			save_scope_as = mandate_vassalize_tributaries_change
		}
		every_tributary = {
			limit = {
				is_ai = yes
				primary_title = {
					target_is_de_jure_liege_or_above = title:h_china
				}
			}
			hidden_effect = { end_tributary = yes }
			change_liege = {
				liege = scope:new_emperor_temp
				change = scope:mandate_vassalize_tributaries_change
			}
		}
		resolve_title_and_vassal_change = scope:mandate_vassalize_tributaries_change
		# Turn your other tributaries into hegemonic
		every_tributary = {
			custom = custom.every_tributary
			limit = {
				NAND = {
					is_ai = yes
					primary_title = {
						target_is_de_jure_liege_or_above = title:h_china
					}
				}
			}
			start_tributary = {
				contract_group = tributary_celestial
				suzerain = scope:new_emperor_temp
			}
		}
	}
	scope:new_emperor_temp = {
		# Get some gold back from any owned domicile
		nomad_domicile_refund_effect = yes
		# Become celestial
		if = {
			limit = {
				NOT = { has_government = celestial_government }
			}
			tgp_domicile_conversion_when_changing_government_type = {
				NEW_DOMICILE_TYPE = east_asian_estate
				NEW_GOVERNMENT_TYPE = celestial_government
			}
			change_government = celestial_government
		}
		# Get the de jure capital if possible
		if = {
			limit = {
				title:h_china.title_capital_county.holder ?= {
					OR = {
						any_liege_or_above = { this = scope:new_emperor_temp }
						this = scope:new_emperor_temp
					}
				}
			}
			if = {
				limit = {
					title:h_china.title_capital_county.holder = {
						this != scope:new_emperor_temp
					}
				}
				create_title_and_vassal_change = {
					type = revoked
					save_scope_as = mandate_capital_change
					add_claim_on_loss = no
				}
				title:h_china.title_capital_county = {
					change_title_holder = {
						holder = scope:new_emperor_temp
						change = scope:mandate_capital_change
					}
				}
				resolve_title_and_vassal_change = scope:mandate_capital_change
			}
			set_realm_capital = title:h_china.title_capital_county
		}
		# Provide the new emperor with suitable rewards
		hidden_effect = {
			add_treasury = {
				value = gold
				min = 1000
			}
			change_influence = {
				value = monumental_influence_gain
				multiply = 4
			}
			add_legitimacy = mandate_legitimacy_max
		}
		dynasty = {
			add_dynasty_prestige = excessive_dynasty_prestige_value
		}
		add_character_modifier = claim_mandate_decision_modifier
		# Increase control of every owned county within China
		hidden_effect = {
			every_realm_county = {
				custom = custom_every_realm_county
				limit = {
					target_is_de_jure_liege_or_above = title:h_china
				}
				change_county_control = 25
			}
		}
		show_as_tooltip = { # We show this as a tooltip because it looks better than the above effect
			title:h_china = { change_county_control = 25 }
		}
		# Populate the emperor's court with high merit characters
		hidden_effect = {
			while = {
				count = 5
				
				create_character = {
					template = tgp_minister_administrator_template
					employer = scope:new_emperor_temp
					faith = scope:new_emperor_temp.faith
					culture = scope:new_emperor_temp.culture
					save_scope_as = mandate_new_administrator_temp
				}
				scope:mandate_new_administrator_temp ?= {
					change_merit = empire_starting_merit_value
					assign_completed_exams_based_on_merit_effect = yes
				}
				create_character = {
					template = tgp_minister_diplomat_template
					employer = scope:new_emperor_temp
					faith = scope:new_emperor_temp.faith
					culture = scope:new_emperor_temp.culture
					save_scope_as = mandate_new_diplomat_temp
				}
				scope:mandate_new_diplomat_temp ?= {
					change_merit = empire_starting_merit_value
					assign_completed_exams_based_on_merit_effect = yes
				}
				create_character = {
					template = tgp_minister_scholar_template
					employer = scope:new_emperor_temp
					faith = scope:new_emperor_temp.faith
					culture = scope:new_emperor_temp.culture
					save_scope_as = mandate_new_scholar_temp
				}
				scope:mandate_new_scholar_temp ?= {
					change_merit = empire_starting_merit_value
					assign_completed_exams_based_on_merit_effect = yes
				}
				create_character = {
					template = tgp_minister_scholar_template
					employer = scope:new_emperor_temp
					faith = scope:new_emperor_temp.faith
					culture = scope:new_emperor_temp.culture
					save_scope_as = mandate_new_scholar_temp
				}
				scope:mandate_new_scholar_temp ?= {
					change_merit = empire_starting_merit_value
					assign_completed_exams_based_on_merit_effect = yes
				}
				create_character = {
					template = tgp_minister_commander_template
					employer = scope:new_emperor_temp
					faith = scope:new_emperor_temp.faith
					culture = scope:new_emperor_temp.culture
					save_scope_as = mandate_new_commander_temp
				}
				scope:mandate_new_commander_temp ?= {
					change_merit = empire_starting_merit_value
					assign_completed_exams_based_on_merit_effect = yes
				}
			}
			every_courtier = {
				limit = {
					merit >= 3000
				}
				random = {
					chance = 50
					create_noble_family_effect = { GOVERNMENT_GIVER = scope:new_emperor_temp }
				}
			}
		}
	}
	# Convert eligible vassals to celestial
	custom_tooltip = {
		text = claim_mandate_turn_vassal_celestial_desc
		every_vassal_or_below = {
			limit = {
				highest_held_title_tier >= tier_county
				NOR = {
					# Don't change republics or theocracies
					government_has_flag = government_is_republic
					government_has_flag = government_is_theocracy
					# And just in case, don't change any unfit governments
					government_has_flag = cannot_be_vassal_or_liege
				}
				capital_province ?= {
					OR = {
						has_holding_type = castle_holding
						has_holding_type = city_holding
						has_holding_type = temple_citadel_holding
					}
				}
			}
			if = {
				limit = {
					# No need to change if they are celestial already
					NOT = { government_has_flag = government_is_celestial }
				}
				change_to_administrative_effect = yes
			}
			# Give characters some merit and exams based on their position
			change_merit = {
				value = assign_merit_based_on_tier_value
				add = {
					value = age
					multiply = 2
				}
				add = {
					value = sum_of_all_skills_value
					multiply = 2
				}
			}
			assign_completed_exams_based_on_merit_effect = yes
		}
	}
	hidden_effect = {
		create_title_and_vassal_change = {
			type = swear_fealty
			save_scope_as = mandate_vassalize_celestial_families_change
		}
		# Vassalize any stray celestial noble families or independent realms within the Cycle's region - we also give them merit to fill out the candidate pools
		every_independent_ruler = {
			limit = {
				capital_province.county ?= {
					target_is_de_jure_liege_or_above = title:h_china
				}
				NOT = {	government_has_flag = cannot_be_vassal_or_liege }
				OR = {
					government_has_flag = government_is_celestial
					culture = {
						has_same_culture_heritage = scope:new_emperor_temp.culture
					}
					dynasty = scope:new_emperor_temp.dynasty
				}
			}
			change_liege = {
				liege = scope:new_emperor_temp
				change = scope:mandate_vassalize_celestial_families_change
			}
			change_merit = {
				value = { 3000 8000 }
				add = {
					value = age
					multiply = 2
				}
				add = {
					value = sum_of_all_skills_value
					multiply = 2
				}
			}
			assign_completed_exams_based_on_merit_effect = yes
			if = {
				limit = {
					highest_held_title_tier >= tier_county
					NOR = {
						# Don't change republics or theocracies
						government_has_flag = government_is_republic
						government_has_flag = government_is_theocracy
						# No need to change if they are celestial already
						government_has_flag = government_is_celestial
					}
					capital_province ?= {
						OR = {
							has_holding_type = castle_holding
							has_holding_type = city_holding
							has_holding_type = temple_citadel_holding
						}
					}
				}
				change_to_administrative_effect = yes
			}
		}
		resolve_title_and_vassal_change = scope:mandate_vassalize_celestial_families_change
		# Ensure that we get a family title and a domicile if we don't
		change_to_administrative_effect = yes
		# set up the council holder as dejure liege of the ministry title, as this effect is not called with the same scope as council onactions
		scope:new_emperor_temp = {
			save_scope_as = councillor_liege
		}
		# Make sure ministers are assigned correctly
		fill_the_ministry_effect = yes
	}
}

# Effect called when entering the Chaos Phase
tgp_chaos_shattering_effect = {
	hidden_effect = {
		title:h_china = {
			holder = { save_temporary_scope_as = old_emperor_temp }
			# Reset imperial examination activity
			set_variable = {
				name = years_since_imperial_examination
				value = 0
			}
		}
		#put every vassal of the current hegemon in a list
		situation:dynastic_cycle = {
			every_situation_participant = {
				limit = {
	   				top_liege = scope:old_emperor_temp
				}
				add_to_list = former_vassals
			}
		}
		scope:old_emperor_temp = {
			#destroy the title of china and all Empires that the Hegemon holds
			every_held_title = {
				limit = {
					tier > tier_kingdom
				}
				scope:old_emperor_temp = {
					destroy_title = prev
				}
			}
			create_title_and_vassal_change = {
				type = independency
				save_scope_as = chaos_change
				add_claim_on_loss = yes
			}
			every_vassal = {
				limit = {
					#breakaway all non-dejure vassals
					primary_title = {
						NOT = { target_is_de_jure_liege_or_above = scope:old_emperor_temp.primary_title }
					}
				}
				becomes_independent = { change = scope:chaos_change }
			}
			resolve_title_and_vassal_change = scope:chaos_change
		}
		# destroy all empire and king level titles if holders have not enough power
		every_in_list = {
			list = former_vassals
			if = {
				limit = {
					is_landed = yes
					is_ai = yes
					OR = {
						AND = {
							highest_held_title_tier = tier_empire
							var:movement_power_individual < high_movement_power_value
						}
						AND = {
							highest_held_title_tier = tier_kingdom
							var:movement_power_individual < decent_movement_power_value
						}
					}
				}
				every_held_title = {
					limit = {
						tier > tier_duchy
					}
					prev = {
						destroy_title = prev
					}
				}
			}
		}
		every_in_list = {
			list = former_vassals
			# Destroy any held minister title
			destroy_held_ministry_titles_effect = yes
		}
		every_in_list = {
			list = former_vassals
			# let the former vassals of the hegemon become regular conquerors
			ai_chance_to_become_conqueror_effect = yes
			# put movement leaders and elders into a list
			if = {
				limit = {
					OR = {
						num_of_relation_disciple > 0
						has_variable = former_movement_leader
					}
					is_independent_ruler = yes
					is_landed = yes
				}
				add_to_list = new_lieges
			}
		}
		# remake the hierarchy by vassalizing or tributarizing non-leader movement members and disciples
		every_in_list = {
			list = new_lieges
			save_scope_as = new_liege
			every_in_list = {
				list = former_vassals
				limit = {
					OR = {
						scope:new_liege.var:former_movement_leader ?= var:former_movement_member
						has_relation_elder = scope:new_liege
					}
					this != scope:new_liege
				}
				save_scope_as = member
				# if lower tier than the potential new liege and is neighboring them or anyone that is also neighboring new liege, vassalize them
				if = {
					limit = {
						highest_held_title_tier < scope:new_liege.highest_held_title_tier
						OR = {
							primary_title = { is_neighbor_to_realm = scope:new_liege }
							any_in_list = {
								list = former_vassals
								this != scope:new_liege
								this != scope:member
								primary_title = {
									is_neighbor_to_realm = scope:new_liege
									is_neighbor_to_realm = scope:member
								}
								is_tributary_of = scope:new_liege
							}
						}
					}
					create_title_and_vassal_change = {
						type = swear_fealty
						save_scope_as = elder_hierarchy_change
						add_claim_on_loss = no
					}
					change_liege = {
						liege = scope:new_liege
						change = scope:elder_hierarchy_change
					}
					resolve_title_and_vassal_change = scope:elder_hierarchy_change
					scope:new_liege = {
						add_to_variable_list = {
							name = chaos_new_vassals
							target = scope:member
						}
					}
				}
				# otherwise turn into tributary
				else_if = {
					limit = {
						top_liege = scope:member
						is_landed = yes
					}
					start_tributary_interaction_effect = {
						TRIBUTARY = scope:member
						SUZERAIN = scope:new_liege
					}
					scope:new_liege = {
						add_to_variable_list = {
							name = chaos_new_tributaries
							target = scope:member
						}
					}
				}
			}
		}
		# Assign unlanded family heads to a suitable realm
		every_in_list = {
			list = former_vassals
			if = {
				limit = {
					any_held_title = { is_noble_family_title = yes }
					is_landed = no
					this = top_liege
				}
				create_title_and_vassal_change = {
					type = swear_fealty
					save_scope_as = unlanded_liege_change
					add_claim_on_loss = no
				}
				# Save the top liege of the domicile's location - If ruler's tier is above county
				if = {
					limit = {
						domicile.domicile_location.county.holder.top_liege = {
							highest_held_title_tier > tier_county
						}
					}
					domicile.domicile_location.county.holder.top_liege = {
						save_temporary_scope_as = unlanded_new_liege
					}
				}
				# Otherwise - Try and find a neighboring ruler
				else_if = {
					limit = {
						domicile.domicile_location.county.holder = {
							any_neighboring_top_liege_realm_owner = {
								highest_held_title_tier > tier_county
							}
						}
					}
					domicile.domicile_location.county.holder = {
						random_neighboring_top_liege_realm_owner = {
							limit = {
								highest_held_title_tier > tier_county
							}
							save_temporary_scope_as = unlanded_new_liege
						}
					}
				}
				change_liege = {
					liege = scope:unlanded_new_liege
					change = scope:unlanded_liege_change
				}
				resolve_title_and_vassal_change = scope:unlanded_liege_change
			}
		}
		# create a story that will make characters pursue taking the mandate
		situation:dynastic_cycle = {
			every_situation_participant = {
				if = {
					limit = {
						NOR = {
							has_trait = conqueror
							any_owned_story = {
						 		story_type = story_conqueror
							}
							any_owned_story = {
							 	story_type = story_greatest_of_khans
							}
							any_owned_story = {
								story_type = story_mongol_invasion
							}
						}
						is_landed = yes
						highest_held_title_tier >= tier_duchy
						is_tributary = no
						is_adult = yes
						is_incapable = no
						# run the ai personality triggers only for non elders and non movement leaders
						trigger_if = {
							limit = {
								NOT = {
									is_in_list = new_lieges
								}
							}
							ai_has_cautious_personality = no
							ai_boldness >= 0
							ai_greed >= 0
						}
					}
					create_story = story_take_mandate_of_heaven
				}
			}
		}
	}
	#depose the previous hegemon from all landed titles
	scope:old_emperor_temp = {
		force_step_down_landed_titles = yes
	}
	hidden_effect = {
		situation:dynastic_cycle = {
			every_situation_participant = {
				remove_variable = movement_member
				recalculate_participant_group = situation:dynastic_cycle
			}
		}
	}
}

# Saves Chinese Emperor, or the last one
tgp_save_son_of_heaven_or_previous_effect = {
	title:h_china.holder ?= { save_scope_as = situation_liege }
	if = {
		limit = { NOT = { exists = scope:situation_liege } }
		title:h_china.previous_holder ?= { save_scope_as = situation_liege }
	}
}

# Advances Dynastic Wuking element
tgp_update_wuking_element_effect = {
	situation:dynastic_cycle = {
		if = {
			limit = { exists = var:wuking_element }
			switch = {
				trigger = var:wuking_element
				flag:metal = {
					tgp_set_wuking_element_effect = { ELEMENT = water }
				}
				flag:water = {
					tgp_set_wuking_element_effect = { ELEMENT = wood }
				}
				flag:wood = {
					tgp_set_wuking_element_effect = { ELEMENT = fire }
				}
				flag:fire = {
					tgp_set_wuking_element_effect = { ELEMENT = earth }
				}
				flag:earth = {
					tgp_set_wuking_element_effect = { ELEMENT = metal }
				}
			}
		}
	}
}

# Adds Wuking element variable
tgp_set_wuking_element_effect = {
	# Set from new creation
	if = {
		limit = {
			NOT = { exists = title:h_china.holder.house.var:wuking_element }
		}
		title:h_china.holder.house ?= {
			set_variable = { name = wuking_element value = flag:$ELEMENT$ }
		}
	}
	# Set from the new or returning dynasty
	set_variable = { name = wuking_element value = flag:$ELEMENT$ }
}

tgp_treasury_raided_effect = {
	if = {
		limit = {
			exists = title:h_china
			scope:county = {
				this = title:h_china.title_capital_county
				holder ?= {
					has_treasury = yes
					treasury > 0
				}
			}
		}
		add_loot = treasury_raided_loot_value
		title:h_china.holder = {
			remove_treasury = treasury_raided_loot_value
			add_legitimacy_effect = { LEGITIMACY = medium_legitimacy_loss }
		}
		if = {
			limit = {
				situation:dynastic_cycle ?= {
					situation_top_has_catalyst = catalyst_imperial_treasury_raided
				}
			}
			trigger_situation_catalyst = catalyst_imperial_treasury_raided
		}
	}
}

tgp_activate_catalyst_against_hegemon_effect = {
	if = {
		limit = {
			exists = title:h_china
			title:h_china.holder ?= $HEGEMON$
			any_character_situation = {
				situation:dynastic_cycle ?= this
				situation_top_has_catalyst = $CATALYST$
			}
		}
		situation:dynastic_cycle = {
			trigger_situation_catalyst = $CATALYST$
		}
	}
}

tgp_assign_china_realm_name_options_effect = {
	random_list = {
		pick = 3
		unique = yes
		0 = { # Ba
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_ba_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_ba
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_ba }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_ba }
		}
		0 = { # Cai
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_cai_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_cai
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_cai }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_cai }
		}
		0 = { # Cao
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_cao_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_cao
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_cao }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_cao }
		}
		0 = { # Chen
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_chen_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_chen
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_chen }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_chen }
		}
		0 = { # Chu
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_chu_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_chu
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_chu }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_chu }
		}
		0 = { # Dai
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_dai_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_dai
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_dai }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_dai }
		}
		0 = { # Dian
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_dian_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_dian
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_dian }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_dian }
		}
		0 = { # Gan
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_gan_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_gan
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_gan }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_gan }
		}
		0 = { # Gui
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_gui_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_gui
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_gui }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_gui }
		}
		0 = { # Han
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_han_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_han
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_han }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_han }
		}
		0 = { # Huai
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_huai_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_huai
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_huai }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_huai }
		}
		0 = { # Jin
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_jin_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_jin
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_jin }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_jin }
		}
		0 = { # Jing
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_jing_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_jing
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_jing }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_jing }
		}
		0 = { # Lai
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_lai_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_lai
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_lai }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_lai }
		}
		0 = { # Liang
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_liang_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_liang
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_liang }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_liang }
		}
		0 = { # Lu
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_lu_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_lu
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_lu }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_lu }
		}
		0 = { # Min
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_min_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_min
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_min }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_min }
		}
		0 = { # Qi
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_qi_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_qi
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_qi }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_qi }
		}
		0 = { # Qian
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_qian_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_qian
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_qian }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_qian }
		}
		0 = { # Shu
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_shu_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_shu
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_shu }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_shu }
		}
		0 = { # Song
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_song_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_song
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_song }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_song }
		}
		0 = { # Sui
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_sui_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_sui
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_sui }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_sui }
		}
		0 = { # Tang
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_tang_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_tang
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_tang }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_tang }
		}
		0 = { # Wei
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_wei_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_wei
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_wei }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_wei }
		}
		0 = { # Wu
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_wu_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_wu
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_wu }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_wu }
		}
		0 = { # Xia
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_xia_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_xia
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_xia }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_xia }
		}
		0 = { # Xiang
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_xiang_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_xiang
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_xiang }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_xiang }
		}
		0 = { # Xing
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_xing_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_xing
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_xing }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_xing }
		}
		0 = { # Xu
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_xu_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_xu
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_xu }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_xu }
		}
		0 = { # Yang
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_yang_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_yang
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_yang }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_yang }
		}
		0 = { # Yin
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_yin_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_yin
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_yin }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_yin }
		}
		0 = { # Yong
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_yong_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_yong
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_yong }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_yong }
		}
		0 = { # Yue
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_yue_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_yue
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_yue }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_yue }
		}
		0 = { # Zhao
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_zhao_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_zhao
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_zhao }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_zhao }
		}
		0 = { # Zheng
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_zheng_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_zheng
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_zheng }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_zheng }
		}
		0 = { # Zhou
			modifier = {
				add = 100
				capital_province ?= {
					geographical_region = tgp_zhou_region
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_zhou
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_zhou }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_zhou }
		}
		0 = { # Yuan
			modifier = {
				add = 1000
				culture = {
					has_cultural_pillar = heritage_mongolic
				}
			}
			modifier = {
				factor = 0
				OR = {
					is_title_localization_key_used = dynn_title_yuan
					is_target_in_variable_list = { name = realm_title_names target = flag:dynn_title_yuan }
				}
			}
			add_to_variable_list = { name = realm_title_names target = flag:dynn_title_yuan }
		}
	}
}

# Custom situation history entry setup on phase change
tgp_dynastic_cycle_record_history_phase_change_effect = {

	# protect from double history entries
	if = {
		limit = {
			situation:dynastic_cycle = {
				OR = {
					NOT = { exists = var:last_recorded_phase_change }
					AND = {
						exists = var:last_recorded_phase_change
						OR = {
							var:last_recorded_phase_change != $PHASE_NUMBER$
							var:last_recorded_phase_change = 4
						}
					}
				}
			}
		}
		# When we enter Division, we only want to show the old hegemon
		if = {
			limit = {
				situation:dynastic_cycle = {
					situation_current_phase = situation_dynastic_cycle_phase_chaos
				}
			}
			situation:dynastic_cycle = {
				record_situation_special_event = {
					key = situation_dynastic_cycle_era_change_history_entry
					phase = $PHASE$
					variables = {
						old_hegemon = title:h_china.previous_holder
					}
				}
			}
		}
		# When we restore China after division, we only show the new hegemon
		else_if = {
			limit = {
				#exists = title:h_china.holder
				#title:h_china.holder = { is_valid_celestial_dynasty = yes }
				situation:dynastic_cycle = {
					exists = var:last_recorded_phase_change
					var:last_recorded_phase_change = 6
				}
			}
			situation:dynastic_cycle = {
				record_situation_special_event = {
					key = situation_dynastic_cycle_era_change_history_entry
					phase = $PHASE$
					variables = {
						new_hegemon = title:h_china.holder
					}
				}
			}
		}
		# If we replace the ruling dynasty, show new and old hegemon
		else_if = {
			limit = {
				exists = title:h_china.holder
				exists = title:h_china.previous_holder
				title:h_china.holder.dynasty != title:h_china.previous_holder.dynasty
			}
			situation:dynastic_cycle = {
				record_situation_special_event = {
					key = situation_dynastic_cycle_era_change_history_entry
					phase = $PHASE$
					variables = {
						new_hegemon = title:h_china.holder
						old_hegemon = title:h_china.previous_holder
					}
				}
			}
		}
		# Else, show none
		else = {
			situation:dynastic_cycle = {
				record_situation_special_event = {
					key = situation_dynastic_cycle_era_change_history_entry
					phase = $PHASE$
				}
			}
		}
		situation:dynastic_cycle = {
			set_variable = {
				name = last_recorded_phase_change
				value = $PHASE_NUMBER$
			}
		}
	}
}

tgp_dynastic_cycle_add_to_movement_effect = {
	$CHAR$ = { save_temporary_scope_as = other_ruler }
	custom_tooltip = tgp_movement_events_0010_add_to_movement_effect_tt
	if = {
		limit = {
			top_participant_group:dynastic_cycle ?= {
				participant_group_type = expansion_movement
			}
		}
		$CHAR$ = {
			set_variable = {
				name = movement_member
				value = flag:expansion
			}
		}
	}
	else_if = {
		limit = {
			top_participant_group:dynastic_cycle ?= {
				participant_group_type = advancement_movement
			}
		}
		$CHAR$ = {
			set_variable = {
				name = movement_member
				value = flag:advancement
			}
		}
	}
	else_if = {
		limit = {
			top_participant_group:dynastic_cycle ?= {
				participant_group_type = pro_hegemon_movement
			}
		}
		$CHAR$ = {
			set_variable = {
				name = movement_member
				value = flag:pro_hegemon
			}
		}
	}
	else_if = {
		limit = {
			top_participant_group:dynastic_cycle ?= {
				participant_group_type = conservative_movement
			}
		}
		$CHAR$ = {
			set_variable = {
				name = movement_member
				value = flag:conservative
			}
		}
	}
	else_if = {
		limit = {
			top_participant_group:dynastic_cycle ?= {
				participant_group_type = undecided_movement
			}
		}
		$CHAR$ = {
			set_variable = {
				name = movement_member
				value = flag:undecided
			}
		}
	}
}

tgp_catalyst_from_decision_effect = {
	switch = {
		trigger = participant_group_type
		pro_hegemon_movement = {
			situation:dynastic_cycle = {
				if = {
					limit = {
						situation_top_has_catalyst = catalyst_house_head_consulted_heaven_advancement_positive
						root.top_liege.hegemon_favored_stability_phase_value >= 0
					}
					trigger_situation_catalyst = catalyst_house_head_consulted_heaven_advancement_positive
				}
				else_if = {
					limit = {
						situation_top_has_catalyst = catalyst_house_head_consulted_heaven_expansion_positive
						root.top_liege.hegemon_favored_stability_phase_value < 0
					}
					trigger_situation_catalyst = catalyst_house_head_consulted_heaven_expansion_positive
				}
				else = {
					trigger_situation_catalyst = catalyst_house_head_consulted_heaven_chaos_negative
				}
			}
		}
		advancement_movement = {
			situation:dynastic_cycle = {
				if = {
					limit = {
						situation_top_has_catalyst = catalyst_house_head_consulted_heaven_advancement_positive
					}
					trigger_situation_catalyst = catalyst_house_head_consulted_heaven_advancement_positive
				}
				else_if = {
					limit = {
						situation_current_phase = situation_dynastic_cycle_phase_stability_advancement
					}
					trigger_situation_catalyst = catalyst_house_head_consulted_heaven_chaos_negative
				}
				else = {
					trigger_situation_catalyst = catalyst_house_head_consulted_heaven_chaos_positive
				}
			}
		}
		expansion_movement = {
			situation:dynastic_cycle = {
				if = {
					limit = {
						situation_top_has_catalyst = catalyst_house_head_consulted_heaven_expansion_positive
					}
					trigger_situation_catalyst = catalyst_house_head_consulted_heaven_expansion_positive
				}
				else_if = {
					limit = {
						situation_current_phase = situation_dynastic_cycle_phase_stability_expansion
					}
					trigger_situation_catalyst = catalyst_house_head_consulted_heaven_chaos_negative
				}
				else = {
					trigger_situation_catalyst = catalyst_house_head_consulted_heaven_chaos_positive
				}
			}
		}
		conservative_movement = {
			situation:dynastic_cycle = {
				if = {
					limit = {
						situation_top_has_catalyst = catalyst_house_head_consulted_heaven_advancement_positive
						root.top_liege.hegemon_favored_stability_phase_value < 0 #flipped on purpose
					}
					trigger_situation_catalyst = catalyst_house_head_consulted_heaven_advancement_positive
				}
				else_if = {
					limit = {
						situation_top_has_catalyst = catalyst_house_head_consulted_heaven_expansion_positive
						root.top_liege.hegemon_favored_stability_phase_value >= 0 #flipped on purpose
					}
					trigger_situation_catalyst = catalyst_house_head_consulted_heaven_expansion_positive
				}
				else = {
					trigger_situation_catalyst = catalyst_house_head_consulted_heaven_chaos_positive
				}
			}
		}
		undecided_movement = {
			situation:dynastic_cycle = {
				trigger_situation_catalyst = catalyst_house_head_consulted_heaven_chaos_negative
			}
		}
	}
}
