# IDs JD.10005, JD.31000

namespace = JD

### Stuff that should probably be handled upon new dynasty for the emperor ###
#- new policy?
#- new status?
#- Western Protectorate ruler change?
#- Title (of China) name change
#- Policy change infrastructure (ended up being another file)
#- Handle tributaries
#- Handle grace values
#- Handle agreements? (Non-agression pacts break)

# TEMPLATE STUFFS/Milla
#### Test events for delayed event bouncing
# character_event = { # initial event fired (hidden)
#	id = JD.10001
#	desc = EVTDESC_JD_10001
#	picture = GFX_evt_crusaders
#	border = GFX_event_normal_frame_war
#	is_triggered_only = yes
#	offmap = allow #(MUST HAVE)
#
#	trigger = {
#
#	}
#
#	immediate = {
#		set_character_flag = sent_event_01
#	}
#
#	option = {
#		name = EVTOPTA_JD_10001
#		custom_tooltip = { text = EVTOPTA_JD_10001_TT }
#		character_event = { id = JD.10002 }
#	}
# }
#
# character_event = { # delayed event that sends actual event to whomever (hidden)
#	id = JD.10002
#	hide_window = yes
#	is_triggered_only = yes
#	offmap = allow # offmap = allow (MUST HAVE)
#	immediate = {
#		any_playable_ruler = {
#			character_event = { id = JD.10003 } # send a visible event (like NEWS!)
#		}
#		set_character_flag = sent_event_02
#	}
# }
#
# character_event = { # Actual important and visible event for player
#	id = JD.10003
#	desc = EVTDESC_JD_10005
#	picture = GFX_evt_crusaders
#	border = GFX_event_normal_frame_war
#
#	is_triggered_only = yes
#
#	option = {
#		name = EVTOPTA_JD_10001
#		custom_tooltip = { text = EVTOPTA_JD_10001_TT }
#		set_character_flag = received_event_03
#		# character_event = { id = JD.10002 days = 15 } # potential follow-up
#	}
# }

# on_offmap_ruler_changed: An offmap power changes its ruler. Triggers for the new ruler
# Root = New ruler
# From = Old ruler
# FromFrom = Offmap

# In case of dynasty change...  (fired from om_action: on_offmap_ruler_changed)
character_event = {
	id = JD.10005

	hide_window = yes
	is_triggered_only = yes # on_offmap_ruler_changed
	offmap = only # alternative would be "offmap = allow" (but that'd *include* them, not be *only* for them)

	has_dlc = "Jade Dragon"

	trigger = {
		is_offmap_tag = offmap_china
		NOT = { dynasty = FROM }
	}

	immediate = {
		if = {
			limit = {
				NOT = {
					any_player = {
						dynasty = ROOT
					}
				}
			}

			clr_offmap_flag = china_invaded_player_dynasty
		}

		# Sack the current western governor or not...
		random_list = {
			50 = {
				mult_modifier = {
					factor = 10

					trigger_if = {
						limit = { culture = mongol }

						governor = {
							NOT = { culture = mongol }
						}
					}
					trigger_else = {
						culture = jurchen

						governor = {
							NOT = { culture = jurchen }
						}
					}
				}
				mult_modifier = {
					factor = 1.5
					has_status = china_civil_war
				}
				mult_modifier = {
					factor = 0.5
					has_status = china_stable
				}

				governor = {
					trigger_switch = {
						on_trigger = has_character_flag

						spawning_a_displaced_royal = {
							offmap_china = {
								set_offmap_flag = waiting_to_spawn_a_displaced_royal
							}
						}

						spawning_a_jurchen_invader = {
							offmap_china = {
								set_offmap_flag = waiting_to_spawn_a_jurchen_invader
							}
						}

						spawning_a_rebel_general = {
							offmap_china = {
								set_offmap_flag = waiting_to_spawn_a_rebel_general
							}
						}
					}

					death = { death_reason = death_went_back_to_china }
				}
			}

			50 = {
				trigger = {
					offmap_china = {
						NOT = { has_offmap_tmp_flag = china_invaded_player_dynasty }
					}
				}
			}
		}

		# Pick a new state (Emperor dynasty) name...
		if = {
			limit = { NOT = { has_offmap_flag = rename_override } }

			random_list = {
				# All cultures...
				30 = { # more likely, in general
					trigger = { NOT = { has_offmap_name = tang_china } }

					mult_modifier = { # more likely to be picked by Shatuo Karluk Turks
						factor = 3

						OR = {
							culture = karluk
							culture = turkish
						}
					}

					set_offmap_name = tang_china
				}

				30 = { # more likely, in general
					trigger = { NOT = { has_offmap_name = jin_china } }

					mult_modifier = { # more likely to be picked by Shatuo Karluk Turks
						factor = 3

						OR = {
							culture = karluk
							culture = turkish
						}
					}

					mult_modifier = { # more likely if jurchen
						factor = 3
						culture = jurchen
					}

					set_offmap_name = jin_china
				}

				20 = {
					trigger = { NOT = { has_offmap_name = wei_china } }
					set_offmap_name = wei_china
				}

				20 = {
					trigger = { NOT = { has_offmap_name = qi_china } }
					set_offmap_name = qi_china
				}

				30 = { # more likely, in general
					trigger = { NOT = { has_offmap_name = zhou_china } }
					set_offmap_name = zhou_china
				}

				30 = { # more likely, in general
					trigger = { NOT = { has_offmap_name = han_china } }

					mult_modifier = { # more likely to be picked by Shatuo Karluk Turks
						factor = 3

						OR = {
							culture = karluk
							culture = turkish
						}
					}

					set_offmap_name = han_china
				}

				30 = { # more likely, in general
					trigger = { NOT = { has_offmap_name = qin_china } }
					set_offmap_name = qin_china
				}

				30 = { # more likely, in general
					trigger = { NOT = { has_offmap_name = yan_china } }
					set_offmap_name = yan_china
				}

				20 = {
					trigger = { NOT = { has_offmap_name = zhao_china } }
					set_offmap_name = zhao_china
				}

				# Foreign cultures...
				100 = {
					trigger = {
						NOT = { has_offmap_name = liao_china }
						culture = khitan
					}

					set_offmap_name = liao_china
				}

				100 = {
					trigger = {
						NOT = { has_offmap_name = yuan_china }
						culture = mongol
					}

					mult_modifier = { # more likely if Borjigin
						factor = 100

						OR = {
							dynasty = 11100
							dynasty = 1051210
						}
					}

					set_offmap_name = yuan_china
				}

				100 = {
					trigger = {
						NOT = { has_offmap_name = xia_china }

						OR = {
							culture = tangut
							culture = jurchen
						}
					}

					set_offmap_name = xia_china
				}

				60 = {
					trigger = {
						NOT = { has_offmap_name = qing_china }
						culture = jurchen
					}

					mult_modifier = { # less likely to be picked early game
						factor = 0.1
						year < 1200
					}

					set_offmap_name = qing_china
				}

				10 = { # less likely, in general
					trigger = { NOT = { has_offmap_name = dai_china } }
					set_offmap_name = dai_china
				}

				10 = { # less likely, in general
					trigger = { NOT = { has_offmap_name = cheng_china } }
					set_offmap_name = cheng_china
				}

				# Han culture... (Picked with a lot less frequency than the above ones)
				5 = { # less likely, in general
					trigger = {
						NOT = { has_offmap_name = liang_china }
						culture = han
					}

					set_offmap_name = liang_china
				}

				10 = { # more likely within the category of Han, but less likely in general
					trigger = {
						NOT = { has_offmap_name = song_china }
						culture = han
					}

					set_offmap_name = song_china
				}

				5 = { # less likely, in general
					trigger = {
						NOT = { has_offmap_name = ming_china }
						culture = han
					}

					mult_modifier = { # less likely to be picked early game
						factor = 0.1
						year < 1200
					}

					set_offmap_name = ming_china
				}

				5 = { # less likely, in general
					trigger = {
						NOT = { has_offmap_name = shu_china }
						culture = han
					}

					set_offmap_name = shu_china
				}

				5 = { # less likely, in general
					trigger = {
						NOT = { has_offmap_name = wu_china }
						culture = han
					}

					set_offmap_name = wu_china
				}

				5 = { # less likely, in general
					trigger = {
						NOT = { has_offmap_name = chu_china }
						culture = han
					}

					set_offmap_name = chu_china
				}

				5 = { # less likely, in general
					trigger = {
						NOT = { has_offmap_name = yue_china }
						culture = han
					}

					set_offmap_name = yue_china
				}

				3 = { # even less likely, in general
					trigger = {
						NOT = { has_offmap_name = yin_china }
						culture = han
					}

					set_offmap_name = yin_china
				}

				3 = { # even less likely, in general
					trigger = {
						NOT = { has_offmap_name = shun_china }
						culture = han
					}

					mult_modifier = { # less likely to be picked early game
						factor = 0.1
						year < 1200
					}

					set_offmap_name = shun_china
				}

				3 = { # even less likely, in general
					trigger = {
						NOT = { has_offmap_name = qi_china }
						culture = han
					}

					set_offmap_name = qi_china
				}
			}
		}

		any_playable_ruler = {
			# Reset Cooldowns
			remove_character_modifier = chinese_grace_send_gift_timer
			remove_character_modifier = chinese_grace_send_eunuch_timer
			remove_character_modifier = chinese_grace_send_concubine_timer
			remove_character_modifier = chinese_grace_tributary_timer
			remove_character_modifier = jd_china_boon_cd
			remove_character_modifier = china_force_open_cooldown
			remove_character_modifier = angered_china_modifier
			remove_character_modifier = chinese_imperial_trade_contract
			remove_character_modifier = china_raid_grace_cd

			# Handle Kowtow trait
			remove_trait = kow_tow_completed_tier_1
			remove_trait = kow_tow_completed_tier_2
			remove_trait = kow_tow_completed_tier_3
			clr_character_flag = went_on_kow_tow_pilgrimage

			# Break peace deal
			if = {
				limit = {
					trigger_if = {
						limit = { ai = no }

						NAND = {
							has_offmap_news_enabled = FROMFROM
							is_within_diplo_range = ROOT
						}
					}
				}

				remove_character_modifier = peace_deal_with_china
			}
		}

		# Handle tributaries...
		governor = {
			any_tributary = {
				limit = { is_tributary = { type = offmap } }
				remove_tributary = PREV
				set_character_flag = was_tributary_of_china # for new dynasty notification event
			}
		}
	}
}




# on_startup event for dealing with old saves not having a proper name or government for the Chinese Empire...
# character_event = {
# 	id = JD.10006
#
# 	is_triggered_only = yes
# 	hide_window = yes
#
# 	ai = no
#
# 	trigger = {
# 		offmap_china = {
# 			has_offmap_name = china_china
# 		}
# 	}
#
# 	immediate = {
# 		offmap_china = {
# 			governor = {
# 				set_government_type = confucian_bureaucracy
# 			}
# 			set_government_type = confucian_bureaucracy
# 			random_list = {
# 				10 = { # pretty likely
# 					set_offmap_name = song_china
# 				}
# 				5 = { # less likely
# 					set_offmap_name = liang_china
# 				}
# 				5 = { # less likely
# 					mult_modifier = { # even less likely to be picked early game
# 						factor = 0.1
# 						NOT = { year = 1200 }
# 					}
# 					set_offmap_name = ming_china
# 				}
# 				5 = { # less likely
# 					set_offmap_name = shu_china
# 				}
# 				5 = { # less likely
# 					set_offmap_name = wu_china
# 				}
# 				5 = { # less likely
# 					set_offmap_name = chu_china
# 				}
# 				5 = { # less likely
# 					set_offmap_name = yue_china
# 				}
# 				3 = { # even less likely
# 					set_offmap_name = yin_china
# 				}
# 				3 = { # even less likely
# 					set_offmap_name = qi_china
# 				}
# 				3 = { # even less likely
# 					mult_modifier = { # less likely to be picked early game
# 						factor = 0.1
# 						NOT = { year = 1200 }
# 					}
# 					set_offmap_name = shun_china
# 				}
# 			}
# 		}
# 	}
# }

character_event = {
	id = JD.31000

	hide_window = yes
	is_triggered_only = yes # on_offmap_ruler_changed
	offmap = only

	immediate = {
		if = {
			limit = {
				FROMFROM = {
					is_offmap_tag = offmap_china
				}
			}

			log = "-------------------------------------"
			log = "China Logging:"
			log = "Chinese succession - New Culture: [Root.Culture.GetName]"
			log = "Chinese succession - Old Culture: [From.Culture.GetName]"
			log = "Chinese succession - New Religion: [Root.Religion.GetName]"
			log = "Chinese succession - Old Religion: [From.Religion.GetName]"
			log = "Chinese succession - New Dynasty: [Root.GetOnlyDynastyName]"
			log = "Chinese succession - Old Dynasty: [From.GetOnlyDynastyName]"
			log = "-------------------------------------"
		}

#		if = {
#			limit = {
#				is_female = yes
#			}
#			change_variable = {
#				which = global_china_amount_of_female_rulers
#				value = 1
#			}
#		}
#		if = {
#			limit = {
#				NOT = { is_female = yes }
#			}
#			change_variable = {
#				which = global_china_amount_of_male_rulers
#				value = 1
#			}
#		}
#		if = {
#			limit = {
#				NOT = { dynasty = FROM }
#			}
#			change_variable = {
#				which = global_china_amount_of_dynastic_changes
#				value = 1
#			}
#		}
#		if = {
#			limit = {
#				culture = han
#				NOT = { culture = FROM }
#			}
#			change_variable = {
#				which = global_china_amount_of_culture_swaps_to_han
#				value = 1
#			}
#		}
#		if = {
#			limit = {
#				culture = jurchen
#				NOT = { culture = FROM }
#			}
#			change_variable = {
#				which = global_china_amount_of_culture_swaps_to_jurchen
#				value = 1
#			}
#		}
#		if = {
#			limit = {
#				culture = mongol
#				NOT = { culture = FROM }
#			}
#			change_variable = {
#				which = global_china_amount_of_culture_swaps_to_mongol
#				value = 1
#			}
#		}
#		if = {
#			limit = {
#				culture = khitan
#				NOT = { culture = FROM }
#			}
#			change_variable = {
#				which = global_china_amount_of_culture_swaps_to_khitan
#				value = 1
#			}
#		}
#		if = {
#			limit = {
#				culture = uyghur
#				NOT = { culture = FROM }
#			}
#			change_variable = {
#				which = global_china_amount_of_culture_swaps_to_uyghur
#				value = 1
#			}
#		}
#		if = {
#			limit = {
#				culture = kirghiz
#				NOT = { culture = FROM }
#			}
#			change_variable = {
#				which = global_china_amount_of_culture_swaps_to_kirghiz
#				value = 1
#			}
#		}
#		if = {
#			limit = {
#				NOR = {
#					culture = han
#					culture = jurchen
#					culture = mongol
#					culture = khitan
#					culture = uyghur
#					culture = kirghiz
#				}
#				NOT = { culture = FROM }
#			}
#			change_variable = {
#				which = global_china_amount_of_culture_swaps_to_something_else
#				value = 1
#			}
#		}
#		if = {
#			limit = {
#				religion = taoist
#				NOT = { religion = FROM }
#			}
#			change_variable = {
#				which = global_china_amount_of_religion_swaps_to_taoist
#				value = 1
#			}
#		}
#		if = {
#			limit = {
#				religion = tengri_pagan
#				NOT = { religion = FROM }
#			}
#			change_variable = {
#				which = global_china_amount_of_religion_swaps_to_tengri
#				value = 1
#			}
#		}
#		if = {
#			limit = {
#				NOR = {
#					religion = taoist
#					religion = tengri_pagan
#				}
#				NOT = { culture = FROM }
#			}
#			change_variable = {
#				which = global_china_amount_of_religion_swaps_to_something_else
#				value = 1
#			}
#		}
	}
}