### CHINA CBs 2.8

# offmap_submission
# tributary_offmap_cb
# dismantle_pretender_china_cb
# force_open_china_cb
# destabilization_china_cb
# invasion_of_china_cb

offmap_submission = {
	name = CB_NAME_OFFMAP_SUBMISSION
	war_name = WAR_NAME_OFFMAP_SUBMISSION
	sprite = 30
	truce_days = 365

	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no
	allow_whitepeace = no
	defender_unoccupied_warscore = yes

	can_use = {
		# Attacker
		ROOT = {
			independent = yes
			mercenary = no

			NOR = {
				same_realm = FROM
				is_vassal_or_below_of = FROM
			}
		}

		# Defender
		FROM = {
			in_revolt = no
			is_nomadic = no # Nomads cannot be subjugated
		}
	}

	on_success = {
		defender = {
			show_scope_change = no

			piety = -100
			prestige = -100

			if = { # Remove Raid China if active
				limit = { has_character_modifier = china_raid_active }

				remove_character_modifier = china_raid_active

				add_character_modifier = {
					name = china_raid_grace_cd
					years = 5
					hidden = yes
				}
			}

			custom_tooltip = {
				text = offmap_submission_tip

				attacker = {
					if = {
						limit = {
							num_of_count_titles_in_realm >= 0

							defender = {
								num_of_count_titles_in_realm < 2
							}
						}

						defender = {
							any_realm_province = {
								county = {
									usurp_title_plus_barony_if_unlanded = {
										target = attacker
										type = invasion
									}
								}
							}
						}
					}

					while = {
						count = 20 # We had an infinite loop at one point. It's been fixed, but safety first

						limit = {
							demesne_size_compared_to_limit < 1

							defender = {
								num_of_count_titles_in_realm > 1
							}
						}

						defender = {
							if = { # Take counties that border each other
								limit = {
									any_realm_title = {
										real_tier = COUNT

										defender = {
											capital_scope = {
												NOT = { province = PREVPREV }
											}
										}

										location = {
											any_neighbor_province = {
												owned_by = attacker
											}
										}
									}
								}

								random_realm_province = {
									limit = {
										defender = {
											NOT = { is_capital = PREV }
										}

										any_neighbor_province = {
											owned_by = attacker
										}
									}

									county = {
										usurp_title_plus_barony_if_unlanded = {
											target = attacker
											type = invasion
										}
									}
								}
							}
							else = {
								random_realm_province = {
									limit = {
										defender = {
											NOT = { is_capital = PREV }
										}
									}

									county = {
										usurp_title_plus_barony_if_unlanded = {
											target = attacker
											type = invasion
										}
									}
								}
							}
						}
					}

					hidden_effect = {
						if = { # China with no land calculates what to do with nomad provinces
							limit = {
								is_offmap_governor = yes
								offmap_power = { is_offmap_tag = offmap_china }
								offmap_china = { has_offmap_tmp_flag = WP_is_not_on_map }
								wealth < 2500 # unless already rich from tributes
							}

							set_variable = {
								which = local_WP_nomad_gold_from_china
								value = 0
							}

							any_realm_province = {
								limit = {
									has_province_modifier = nomad_agitation
									has_settlement_construction = no
								}
								change_variable = { which = local_WP_nomad_gold_from_china value = 1 }
							}

							character_event = {
								id = JD.60101 # calculate gold sent from China for building holdings
								days = 1
							}
						}
					}

					if = { # New Empire
						limit = { has_character_flag = is_rebel_general }
						set_global_flag = rebel_general_invasion_success

						primary_title = {
							set_title_landless = {
								title = THIS
								status = no
							}

							set_title_flag = uses_temple_names_by_script # Titles with this flag give their heirs a temple name via on_death event
							set_title_flag = pretender_chinese_empire # Blocks all grace interactions
							set_title_flag = rebel_general_title
							adjective = e_china_west_governor_adj # Use WP adj as it is "Chinese" but also gives the right one in different languages

							random_list = { # Random Empire name that China doesn't have
								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = tang_china } } }
									set_name = "Tang Empire"
									set_title_flag = china_name_Tang
								}

								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = jin_china } } }
									set_name = "Jin Empire"
									set_title_flag = china_name_Jin
								}

								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = wei_china } } }
									set_name = "Wei Empire"
									set_title_flag = china_name_Wei
								}

								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = qi_china } } }
									set_name = "Qi Empire"
									set_title_flag = china_name_Qi
								}

								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = zhou_china } } }
									set_name = "Zhou Empire"
									set_title_flag = china_name_Zhou
								}

								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = han_china } } }
									set_name = "Han Empire"
									set_title_flag = china_name_Han
								}

								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = qin_china } } }
									set_name = "Qin Empire"
									set_title_flag = china_name_Qin
								}

								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = yan_china } } }
									set_name = "Yan Empire"
									set_title_flag = china_name_Yan
								}

								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = zhao_china } } }
									set_name = "Zhao Empire"
									set_title_flag = china_name_Zhao
								}

								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = liao_china } } }
									set_name = "Liao Empire"
									set_title_flag = china_name_Liao
								}

								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = yuan_china } } }
									set_name = "Yuan Empire"
									set_title_flag = china_name_Yuan
								}

								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = xia_china } } }
									set_name = "Xia Empire"
									set_title_flag = china_name_Xia
								}

								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = qing_china } } }
									set_name = "Qing Empire"
									set_title_flag = china_name_Qing
								}

								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = dai_china } } }
									set_name = "Dai Empire"
									set_title_flag = china_name_Dai
								}

								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = cheng_china } } }
									set_name = "Cheng Empire"
									set_title_flag = china_name_Cheng
								}

								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = liang_china } } }
									set_name = "Liang Empire"
									set_title_flag = china_name_Liang
								}

								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = song_china } } }
									set_name = "Song Empire"
									set_title_flag = china_name_Song
								}

								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = ming_china } } }
									set_name = "Ming Empire"
									set_title_flag = china_name_Ming
								}

								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = shu_china } } }
									set_name = "Shu Empire"
									set_title_flag = china_name_Shu
								}

								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = wu_china } } }
									set_name = "Wu Empire"
									set_title_flag = china_name_Wu
								}

								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = chu_china } } }
									set_name = "Chu Empire"
									set_title_flag = china_name_Chu
								}

								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = yue_china } } }
									set_name = "Yue Empire"
									set_title_flag = china_name_Yue
								}

								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = yin_china } } }
									set_name = "Yin Empire"
									set_title_flag = china_name_Yin
								}

								1 = {
									trigger = { offmap_china = { NOT = { has_offmap_name = shun_china } } }
									set_name = "Shun Empire"
									set_title_flag = china_name_Shun
								}
							}

							add_law = {
								law = succ_primogeniture
								cooldown = no
								opinion_effect = no
							}
						}

						set_government_type = chinese_imperial_government

						random_list = { # Give a temple name
							1 = { set_name = Gaozu }
							1 = { set_name = Taizu }
							1 = { set_name = Shizu }
							1 = { set_name = Zhaozu }
							1 = { set_name = Jingzu }
							1 = { set_name = Xianzu }
							1 = { set_name = Liezu }
							1 = { set_name = Chengzu }
						}

						set_character_flag = given_temple_name_by_script
						remove_special_character_title = TITLE_GENERAL

						# Some starting stuff
						wealth = 1000
						prestige = 500
						piety = 200

						offmap_china = { set_offmap_flag = no_war_news }

						any_player = {
							limit = {
								has_offmap_news_enabled = offmap_china
								is_within_diplo_range = PREV
							}

							narrative_event = {
								id = JD.60211 # News Event: Inform General won and founded Empire
								days = 1
							}
						}
					}
				}

				if = {
					limit = { lower_real_tier_than = EMPEROR }

					set_defacto_liege = ROOT

					if = {
						limit = {
							attacker = { has_character_flag = is_rebel_general }
						}

						any_demesne_title = {
							limit = { real_tier = KING }

							usurp_title_plus_barony_if_unlanded = {
								target = attacker
								type = invasion
							}

							add_law = {
								law = succ_primogeniture
								cooldown = no
								opinion_effect = no
							}
						}
					}
				}
				else = {
					any_vassal = {
						set_defacto_liege = ROOT

						if = {
							limit = { attacker = { has_character_flag = is_rebel_general } }

							opinion = {
								name  = opinion_subjugated
								multiplier = 2
								who = attacker
							}
						}
					}

					any_demesne_title = {
						limit = {
							real_tier = EMPEROR
							controls_religion = no
						}

						destroy_landed_title = THIS
					}

					if = {
						limit = { lower_real_tier_than = EMPEROR }

						set_defacto_liege = ROOT
					}
					else = {
						any_demesne_title = {
							limit = { controls_religion = no }

							attacker = {
								grant_title_no_opinion = {
									target = PREV
									type = invasion
								}
							}
						}
					}
				}
			}
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			piety = -100

			if = {
				limit = { has_character_flag = is_rebel_general }

				hidden_effect = {
					any_courtier = {
						move_character = defender
						imprison = defender
					}

					primary_title = {
						activate_title = {
							title = THIS
							status = no
						}

						destroy_landed_title = THIS
					}

					move_character = defender

					attacker = {
						imprison = defender
					}
				}
			}
		}

		defender = {
			show_scope_change = no

			piety = 100
			participation_scaled_prestige = 250
			set_character_flag = defeated_china
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 250 }
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			prestige = -500

			transfer_scaled_wealth = {
				to = defender
				value = 5.0
				min = 1000
			}

			hidden_effect = {
				if = {
					limit = { has_character_flag = is_rebel_general }

					any_courtier = {
						move_character = defender
						imprison = defender
					}

					primary_title = {
						activate_title = {
							title = THIS
							status = no
						}

						destroy_landed_title = THIS
					}

					move_character = defender

					attacker = {
						imprison = defender
					}
				}

				else_if = { # Makes China have a chance of leaving Expanionist policy
					limit = {
						is_offmap_tag = offmap_china

						offmap_china = {
							has_policy = china_expansionist
						}
					}

					random = {
						chance = 50

						offmap_china = {
							set_policy = china_open
						}

						log = "China War Logging: China was forced out of Expansionism"

						if = {
							limit = {
								offmap_china = {
									has_status = china_stable
								}
							}

							random = {
								chance = 30

								offmap_china = {
									set_status = china_unrest
								}

								log = "China War Logging: China was forced into Unrest"
							}
						}
					}
				}

				log = "China War Logging: China lost a submission war against [From.GetBestName]"
			}
		}

		defender = {
			show_scope_change = no

			set_character_flag = defeated_china
			piety = 500
			participation_scaled_prestige = 2500
			roman_victory_triumph_effect = yes
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 1000 }
		}
	}

	on_attacker_leader_death = {
		attacker = {
			if = {
				limit = { has_character_flag = is_rebel_general }

				end_war = invalid

				any_courtier = {
					death = { death_reason = death_went_back_to_china }
				}

				primary_title = {
					activate_title = {
						title = THIS
						status = no
					}

					destroy_landed_title = THIS
				}

				death = { death_reason = death_went_back_to_china }
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = {
		factor = 0
	}
}

tributary_offmap_cb = {
	name = CB_NAME_PERMANENT_TRIBUTARY
	war_name = WAR_NAME_PERMANENT_TRIBUTARY
	sprite = 30
	truce_days = 365
	hostile_against_others = yes
	can_ask_to_join_war = no
	display_on_map = no
	allowed_to_target_tributaries = no

	defender_unoccupied_warscore = yes

	can_use = {
		# Attacker
		ROOT = {
			independent = yes
			mercenary = no

			NOR = {
				same_realm = FROM
				is_vassal_or_below_of = FROM
			}
		}

		# Defender
		FROM = {
			independent = yes
			in_revolt = no
		}
	}

	is_valid = {
		ROOT = {
			NOT = { rightful_religious_head = FROM }
		}
	}

	on_success = {
		log = "---------------------"
		log = "China Logging:"
		log = "The [offmap_china.GetShortName] invasion of [From.GetTitledName] has ended"
		log = "The war ended in a victory for China"
		log = "[From.GetTitledName] is now tributary of China"
		log = "---------------------"

		defender = {
			show_scope_change = no

			if = {
				limit = { is_tributary = yes }
				remove_tributary = suzerain
			}
		}

		attacker = {
			show_scope_change = no

			make_tributary = {
				who = defender
				tributary_type = offmap
			}

			prestige = 200
		}

		defender = {
			show_scope_change = no

			save_event_target_as = target_loser
			prestige = -100

			custom_tooltip = {
				text = REMOVE_PREVIOUS_SUZERAIN

				any_suzerain = {
					remove_tributary = event_target:target_loser
				}
			}

			if = { # Remove Raid China if active
				limit = { has_character_modifier = china_raid_active }

				remove_character_modifier = china_raid_active

				add_character_modifier = {
					name = china_raid_grace_cd
					years = 5
					hidden = yes
				}
			}
		}
	}

	on_fail = {
		log = "---------------------"
		log = "China Logging:"
		log = "The [offmap_china.GetShortName] invasion of [From.GetTitledName] has ended"
		log = "The war ended in a white peace"
		log = "---------------------"

		attacker = {
			show_scope_change = no

			prestige = -200
		}

		defender = {
			show_scope_change = no

			prestige = 200
			set_character_flag = defeated_china
		}
	}

	on_reverse_demand = {
		log = "---------------------"
		log = "China Logging:"
		log = "The [offmap_china.GetShortName] invasion of [From.GetTitledName] has ended"
		log = "The war ended in a victory for [From.GetTitledName]"
		log = "---------------------"

		attacker = {
			show_scope_change = no

			prestige = -500

			transfer_scaled_wealth = {
				to = defender
				value = 5.0
				min = 1000
			}

			hidden_effect = {
				if = { # Makes China have a chance of leaving Expansionist policy...
					limit = {
						is_offmap_tag = offmap_china

						offmap_china = {
							has_policy = china_expansionist
						}
					}

					random = {
						chance = 50

						offmap_china = {
							set_policy = china_open
						}
					}
				}

				log = "China War Logging: China lost a I'll-make-you-my-tributary war against [From.GetBestName]"
			}
		}

		defender = {
			show_scope_change = no

			piety = 500
			participation_scaled_prestige = 2500
			set_character_flag = defeated_china
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 1000 }
		}
	}

	on_attacker_leader_death = {
		# end_war = invalid
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = {
		factor = 0
	}
}

dismantle_pretender_china_cb = {
	name = CB_NAME_DISMANTLE_PRETENDER_CHINA
	war_name = WAR_NAME_DISMANTLE_PRETENDER_CHINA
	sprite = 30
	truce_days = 365

	hostile_against_others = yes
	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no
	allow_whitepeace = no
	defender_unoccupied_warscore = yes

	can_use = {
		# Attacker
		ROOT = {
			independent = yes
			mercenary = no

			NOR = {
				same_realm = FROM
				is_vassal_or_below_of = FROM
			}
		}

		# Defender
		FROM = {
			primary_title = { has_title_flag = rebel_general_title }
		}
	}

	on_success = {
		log = "---------------------"
		log = "China Logging:"
		log = "The [offmap_china.GetShortName] dismantle pretender on [From.GetTitledName] has ended"
		log = "The war ended in a victory for China"
		log = "[From.GetTitledName] is now dismantled of China"
		log = "---------------------"

		attacker = {
			show_scope_change = no

			prestige = 200
		}

		defender = {
			custom_tooltip = {
				text = dismantle_pretender_china_success_tt

				offmap_china = { set_offmap_flag = no_war_news }

				any_player = {
					limit = {
						OR = {
							AND = {
								has_offmap_news_enabled = offmap_china
								is_within_diplo_range = attacker
							}

							vassal_of = defender
						}

						NOT = { character = defender }
					}

					narrative_event = { id = JD.60209 } # News Event: Title invaded by China destroyed
				}

				any_courtier = {
					death = { death_reason = death_executed_by_chinese_emperor }
				}

				primary_title = {
					activate_title = {
						title = THIS
						status = no
					}

					destroy_landed_title = THIS
				}

				death = { death_reason = death_executed_by_chinese_emperor }
			}
		}
	}

	on_fail = {
		log = "---------------------"
		log = "China Logging:"
		log = "The [offmap_china.GetShortName] dismantle pretender on [From.GetTitledName] has ended"
		log = "The war ended in a white peace for China"
		log = "---------------------"

		attacker = {
			show_scope_change = no

			piety = -100
		}

		defender = {
			show_scope_change = no

			piety = 100
			participation_scaled_prestige = 250
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 250 }
		}
	}

	on_reverse_demand = {
		log = "---------------------"
		log = "China Logging:"
		log = "The [offmap_china.GetShortName] dismantle pretender on [From.GetTitledName] has ended"
		log = "The war ended in a defeat for China"
		log = "---------------------"

		attacker = {
			show_scope_change = no

			piety = -200

			transfer_scaled_wealth = {
				to = defender
				value = 5.0
				min = 500
			}

			offmap_power = {
				set_offmap_tmp_flag = pretender_chinese_empire_war_cooldown
			}
		}

		defender = {
			show_scope_change = no

			piety = 200
			participation_scaled_prestige = 1000
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 1000 }
		}
	}

	on_attacker_leader_death = {
		log = "---------------------"
		log = "China Logging:"
		log = "The [offmap_china.GetShortName] dismantle pretender on [From.GetTitledName] has ended"
		log = "The war was invalidated for China"
		log = "---------------------"
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = {
		factor = 0
	}
}

force_open_china_cb = {
	name = CB_NAME_FORCE_OPEN_CHINA
	war_name = WAR_NAME_FORCE_OPEN_CHINA
	sprite = 30
	truce_days = 365
	max_attacker_battle_score = 100

	hostile_against_others = yes
	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no
	allow_whitepeace = no
	capturing_defender_is_complete_victory = no

	can_use = {
		# Attacker
		ROOT = {
			independent = yes
			mercenary = no

			NOR = {
				same_realm = FROM
				is_vassal_or_below_of = FROM
			}
		}

		# Defender
		FROM = {
			is_offmap_governor = yes
			offmap_power = { is_offmap_tag = offmap_china }
		}
	}

	is_valid = {
		offmap_china = { has_policy = china_isolationist }
	}

	on_success = {
		attacker = {
			show_scope_change = no

			participation_scaled_prestige = 1000
			add_grace_massive_effect = yes

			add_character_modifier = {
				name = chinese_favored_in_trade
				years = 25
				inherit = yes
			}
		}

		any_attacker = {
			limit = { NOT = { character = attacker } }
			hidden_effect = { participation_scaled_prestige = 300 }
		}

		defender = {
			show_scope_change = no

			transfer_scaled_wealth = {
				to = attacker
				value = 5.0
				min = 2000
			}

			save_event_target_as = the_protector_general

			offmap_power = {
				set_offmap_flag = no_policy_news
				set_policy = china_open

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

				set_offmap_tmp_flag = china_forced_open
				set_offmap_flag = no_war_news

				custom_tooltip = {
					text = china_forced_open_tt

					any_player = {
						limit = {
							has_offmap_news_enabled = PREV
							is_within_diplo_range = PREV
						}

						narrative_event = { id = JD.60302 }
					}

					clr_offmap_flag = no_policy_news
				}

				clr_offmap_tmp_flag = china_force_open_in_progress
			}
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			prestige = -150
		}

		defender = {
			show_scope_change = no

			piety = 100
			participation_scaled_prestige = 50

			hidden_effect = {
				offmap_power = { clr_offmap_tmp_flag = china_force_open_in_progress }
			}
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			prestige = -300

			transfer_scaled_wealth = {
				to = defender
				value = 5.0
				min = 500
			}

			custom_tooltip = {
				text = angered_china_tt # Scripter! Update this is you change the value below.

				add_character_modifier = {
					name = angered_china_modifier
					years = 50
					inherit = yes
				}
			}
		}

		defender = {
			show_scope_change = no

			piety = 200
			participation_scaled_prestige = 100

			hidden_effect = {
				offmap_power = {
					set_offmap_flag = no_war_news

					any_player = {
						limit = {
							has_offmap_news_enabled = PREV
							is_within_diplo_range = PREV
						}

						narrative_event = { id = JD.60303 } # News Event: Fail to force open China by war
					}

					clr_offmap_tmp_flag = china_force_open_in_progress
				}
			}
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 100 }
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = {
		factor = 0
	}
}

destabilization_china_cb = {
	name = CB_NAME_DESTABILIZATION_CHINA
	war_name = WAR_NAME_DESTABILIZATION_CHINA
	sprite = 30
	truce_days = 365

	hostile_against_others = yes
	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no
	allow_whitepeace = no
	defender_unoccupied_warscore = yes

	can_use = {
		# Attacker
		ROOT = {
			independent = yes
			mercenary = no

			NOR = {
				same_realm = FROM
				is_vassal_or_below_of = FROM
			}
		}
	}

	on_success = {
		attacker = {
			show_scope_change = no

			prestige = 200
		}

		defender = {
			custom_tooltip = {
				text = destabilization_china_success_tt

				offmap_china = {
					set_offmap_flag = no_war_news
				}

				any_player = {
					limit = {
						OR = {
							AND = {
								has_offmap_news_enabled = offmap_china
								is_within_diplo_range = attacker
							}

							vassal_of = defender
						}

						NOT = { character = defender }
					}

					narrative_event = { id = JD.60209 } # News Event: Title invaded by China destroyed
				}

				any_demesne_title = {
					limit = {
						higher_real_tier_than = DUKE
						controls_religion = no
						clan = no
					}

					unsafe_destroy_landed_title = THIS
				}
			}
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			piety = -100
		}

		defender = {
			show_scope_change = no

			piety = 100
			participation_scaled_prestige = 250
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 250 }
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			piety = -200

			transfer_scaled_wealth = {
				to = defender
				value = 5.0
				min = 1000
			}
		}

		defender = {
			show_scope_change = no

			piety = 500
			participation_scaled_prestige = 2500
			set_character_flag = defeated_china
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 1000 }
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = {
		factor = 0
	}
}

invasion_of_china_cb = {
	name = CB_NAME_INVASION_OF_CHINA
	war_name = WAR_NAME_INVASION_OF_CHINA
	sprite = 30
	truce_days = 365
	battle_warscore_mult = 1.5
	max_attacker_occupation_score = 50
	max_defender_occupation_score = 50
	max_attacker_battle_score = 100
	third_party_portrait = event_target:invasion_of_china_claimant

	hostile_against_others = yes
	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no
	allow_whitepeace = no
	capturing_defender_is_complete_victory = no

	# attacker_unoccupied_warscore = yes - If attacker completely unoccupied, attacker will get ticking warscore. Only works if no title is targetted
	# defender_unoccupied_warscore = yes - If defender completely unoccupied, defender will get ticking warscore. Only works if no title is targetted

	can_use = {
		# Attacker
		ROOT = {
			independent = yes
			mercenary = no

			NOR = {
				same_realm = FROM
				is_vassal_or_below_of = FROM
			}
		}

		# Defender
		FROM = {
			is_offmap_governor = yes
			offmap_power = { is_offmap_tag = offmap_china }
		}
	}

	is_valid = {
		event_target:invasion_of_china_claimant = {
			is_alive = yes
		}
	}

	on_success = {
		attacker = {
			show_scope_change = no

			custom_tooltip = {
				text = invasion_of_china_success_tt

				remove_character_modifier = china_raid_active
				
				### KCC ###
				if = {
					limit = { kcc_china_has_persecuted_trigger = yes }
					1155 = { province_event = { id = kccpersecute.0043 days = 10 } }
				}

				event_target:invasion_of_china_claimant = {
					betrothed = { break_betrothal = PREV }

					any_spouse = {
						if = {
							limit = {
								is_ruler = no
								is_primary_heir = no
							}

							leave_court_for_china_effect = yes
						}
					}

					any_child = {
						if = {
							limit = {
								is_ruler = no
								is_primary_heir = no
							}

							leave_court_for_china_effect = yes
						}
					}

					abdicate = yes

					any_claim = {
						remove_claim = event_target:invasion_of_china_claimant
					}

					clr_character_flag = no_court_invites
					remove_trait = cannot_marry
					diplomatic_immunity = no
					
					### KCC ###
					if = {
						limit = {
							true_religion = catholic
							NOT = { has_global_flag = catholics_in_china }
						}
						set_global_flag = catholics_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_catholics" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = cathar
							NOT = { has_global_flag = cathars_in_china }
						}
						set_global_flag = cathars_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_cathars" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = fraticelli
							NOT = { has_global_flag = fraticellis_in_china }
						}
						set_global_flag = fraticellis_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_fraticellis" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = waldensian
							NOT = { has_global_flag = waldensians_in_china }
						}
						set_global_flag = waldensians_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_waldensians" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = lollard
							NOT = { has_global_flag = lollards_in_china }
						}
						set_global_flag = lollards_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_lollards" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = orthodox
							NOT = { has_global_flag = orthodoxy_in_china }
						}
						set_global_flag = orthodoxy_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_orthodoxs" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = miaphysite
							NOT = { has_global_flag = miaphysites_in_china }
						}
						set_global_flag = miaphysites_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_miaphysites" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = monophysite
							NOT = { has_global_flag = monophysites_in_china }
						}
						set_global_flag = monophysites_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_monophysites" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = bogomilist
							NOT = { has_global_flag = bogomilists_in_china }
						}
						set_global_flag = bogomilists_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_bogomilists" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = monothelite
							NOT = { has_global_flag = monothelites_in_china }
						}
						set_global_flag = monothelites_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_monothelites" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = iconoclast
							NOT = { has_global_flag = iconoclasts_in_china }
						}
						set_global_flag = iconoclasts_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_iconoclasts" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = paulician
							NOT = { has_global_flag = paulicians_in_china }
						}
						set_global_flag = paulicians_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_paulicians" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = nestorian
							NOT = { has_global_flag = nestorians_in_china }
						}
						set_global_flag = nestorians_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_nestorians" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = messalian
							NOT = { has_global_flag = messalians_in_china }
						}
						set_global_flag = messalians_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_messalians" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = sunni
							NOT = { has_global_flag = sunnis_in_china }
						}
						set_global_flag = sunnis_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_sunnis" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = zikri
							NOT = { has_global_flag = zikris_in_china }
						}
						set_global_flag = zikris_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_zikris" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = ibadi
							NOT = { has_global_flag = ibadis_in_china }
						}
						set_global_flag = ibadis_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_ibadis" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = kharijite
							NOT = { has_global_flag = kharijites_in_china }
						}
						set_global_flag = kharijites_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_kharijites" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = shiite
							NOT = { has_global_flag = shiites_in_china }
						}
						set_global_flag = shiites_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_shiites" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = qarmatian
							NOT = { has_global_flag = qarmatian_in_china }
						}
						set_global_flag = qarmatian_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_qarmatians" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = hurufi
							NOT = { has_global_flag = hurufis_in_china }
						}
						set_global_flag = hurufis_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_hurufis" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = zoroastrian
							NOT = { has_global_flag = zoroastrians_in_china }
						}
						set_global_flag = zoroastrians_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_zoroastrians" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = mazdaki
							NOT = { has_global_flag = mazdakis_in_china }
						}
						set_global_flag = mazdakis_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_mazdakis" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = manichaean
							NOT = { has_global_flag = manicheans_in_china }
						}
						set_global_flag = manicheans_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_manicheans" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = buddhist
							NOT = { has_global_flag = buddhists_in_china }
						}
						set_global_flag = buddhists_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_buddhists" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = jain
							NOT = { has_global_flag = jains_in_china }
						}
						set_global_flag = jains_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_jains" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = norse_pagan_reformed
							NOT = { has_global_flag = norse_in_china }
						}
						set_global_flag = norse_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_norse_pagan_reformeds" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = tengri_pagan_reformed
							NOT = { has_global_flag = tengri_in_china }
						}
						set_global_flag = tengri_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_tengri_pagan_reformeds" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = baltic_pagan_reformed
							NOT = { has_global_flag = baltics_in_china }
						}
						set_global_flag = baltics_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_baltic_pagan_reformeds" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = finnish_pagan_reformed
							NOT = { has_global_flag = finnish_in_china }
						}
						set_global_flag = finnish_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_finnish_pagan_reformeds" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = aztec_pagan_reformed
							NOT = { has_global_flag = aztecs_in_china }
						}
						set_global_flag = aztecs_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_aztec_reformeds" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = slavic_pagan_reformed
							NOT = { has_global_flag = slavs_in_china }
						}
						set_global_flag = slavs_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_slavic_pagan_reformeds" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = west_african_pagan_reformed
							NOT = { has_global_flag = africans_in_china }
						}
						set_global_flag = africans_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_west_african_pagan_reformeds" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = zun_pagan_reformed
							NOT = { has_global_flag = zun_in_china }
						}
						set_global_flag = zun_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_zun_pagan_reformeds" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = hellenic_pagan_reformed
							NOT = { has_global_flag = hellenics_in_china }
						}
						set_global_flag = hellenics_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_hellenic_pagan_reformeds" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = bon_pagan_reformed
							NOT = { has_global_flag = bon_in_china }
						}
						set_global_flag = bon_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_bon_reformeds" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = celtic_pagan_reformed
							NOT = { has_global_flag = celts_in_china }
						}
						set_global_flag = celts_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_druidic_reformeds" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = egyptian_pagan_reformed
							NOT = { has_global_flag = egyptians_in_china }
						}
						set_global_flag = egyptians_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_egyptian_pagans" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = aten_pagan
							NOT = { has_global_flag = atenics_in_china }
						}
						set_global_flag = atenics_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_aten_pagans" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = britannic_pagan
							NOT = { has_global_flag = britannics_in_china }
						}
						set_global_flag = britannics_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_britannic_pagans" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = promethean_pagan
							NOT = { has_global_flag = prometheans_in_china }
						}
						set_global_flag = prometheans_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_promethean_pagans" value = 1.5 } }
					}
					#Christian Immersion
					if = {
						limit = {
							true_religion = adoptionist
							NOT = { has_global_flag = adoptionists_in_china }
						}
						set_global_flag = adoptionists_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_adoptionists" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = free_spirits
							NOT = { has_global_flag = free_spirits_in_china }
						}
						set_global_flag = free_spirits_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_free_spirits" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = barlaamite
							NOT = { has_global_flag = barlaamites_in_china }
						}
						set_global_flag = barlaamites_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_barlaamites" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = judaizer
							NOT = { has_global_flag = judaizers_in_china }
						}
						set_global_flag = judaizers_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_judaizers" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = mandean
							NOT = { has_global_flag = mandeans_in_china }
						}
						set_global_flag = mandeans_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_mandeans" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = chaldean
							NOT = { has_global_flag = chaldeans_in_china }
						}
						set_global_flag = chaldeans_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_chaldeans" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = apollinarian
							NOT = { has_global_flag = apollinarians_in_china }
						}
						set_global_flag = apollinarians_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_apollinarians" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = yarsan
							NOT = { has_global_flag = yarsans_in_china }
						}
						set_global_flag = yarsans_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_yarsans" value = 1.5 } }
					}
					if = {
						limit = {
							true_religion = syriac
							NOT = { has_global_flag = syriacs_in_china }
						}
						set_global_flag = syriacs_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_syriacs" value = 1.5 } }
					}
					#Last Vanilla Religions
					if = {
						limit = {
							has_game_rule = {
								name = kcc_who_can_preach
								value = kcc_preach_yes
							}
							true_religion = yazidi
							NOT = { has_global_flag = yazidis_in_china }
						}
						set_global_flag = yazidis_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_yazidis" value = 1.5 } }
					}
					if = {
						limit = {
							has_game_rule = {
								name = kcc_who_can_preach
								value = kcc_preach_yes
							}
							true_religion = druze
							NOT = { has_global_flag = druze_in_china }
						}
						set_global_flag = druze_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_druzes" value = 1.5 } }
					}
					if = {
						limit = {
							has_game_rule = {
								name = kcc_who_can_preach
								value = kcc_preach_yes
							}
							true_religion = khurmazta
							NOT = { has_global_flag = khurmaztas_in_china }
						}
						set_global_flag = khurmaztas_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_khurmaztas" value = 1.5 } }
					}
					if = {
						limit = {
							has_game_rule = {
								name = kcc_who_can_preach
								value = kcc_preach_yes
							}
							true_religion = jewish
							NOT = { has_global_flag = jews_in_china }
						}
						set_global_flag = jews_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_jewishs" value = 1.5 } }
					}
					if = {
						limit = {
							has_game_rule = {
								name = kcc_who_can_preach
								value = kcc_preach_yes
							}
							true_religion = samaritan
							NOT = { has_global_flag = samaritans_in_china }
						}
						set_global_flag = samaritans_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_samaritans" value = 1.5 } }
					}
					if = {
						limit = {
							has_game_rule = {
								name = kcc_who_can_preach
								value = kcc_preach_yes
							}
							true_religion = karaite
							NOT = { has_global_flag = karaites_in_china }
						}
						set_global_flag = karaites_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_karaites" value = 1.5 } }
					}
					if = {
						limit = {
							has_game_rule = {
								name = kcc_who_can_preach
								value = kcc_preach_yes
							}
							true_religion = chasidish
							NOT = { has_global_flag = chasidish_in_china }
						}
						set_global_flag = chasidish_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_chasidishs" value = 1.5 } }
					}
					if = {
						limit = {
							has_game_rule = {
								name = kcc_who_can_preach
								value = kcc_preach_yes
							}
							true_religion = hindu
							NOT = { has_global_flag = hindus_in_china }
						}
						set_global_flag = hindus_in_china
						hidden_tooltip = { set_variable = { which = "global_chinese_hindus" value = 1.5 } }
					}
					convert_to_secret_religion = yes
				}

				offmap_china = {
					set_offmap_flag = no_policy_news
					set_policy = china_open
					log = "-------------------------------------"
					log = "China Policy Logging:"
					log = "Chinas new Policy is now OPEN"
					log = "-------------------------------------"
					clr_offmap_flag = no_policy_news

					governor = {
						any_vassal = {
							set_defacto_liege = ROOT
						}

						any_demesne_title = {
							limit = { lower_real_tier_than = EMPEROR }
							grant_title = attacker
						}
					}

					attacker = {
						if = {
							limit = { government = chinese_imperial_government }

							offmap_china = { set_offmap_flag = rename_override }

							primary_title = {
								trigger_switch = {
									on_trigger = has_title_flag

									china_name_Tang = { offmap_china = { set_offmap_name = "tang_china" } }
									china_name_Jin = { offmap_china = { set_offmap_name = "jin_china" } }
									china_name_Wei = { offmap_china = { set_offmap_name = "wei_china" } }
									china_name_Qi = { offmap_china = { set_offmap_name = "qi_china" } }
									china_name_Zhou = { offmap_china = { set_offmap_name = "zhou_china" } }
									china_name_Han = { offmap_china = { set_offmap_name = "han_china" } }
									china_name_Qin = { offmap_china = { set_offmap_name = "qin_china" } }
									china_name_Yan = { offmap_china = { set_offmap_name = "yan_china" } }
									china_name_Zhao = { offmap_china = { set_offmap_name = "zhao_china" } }
									china_name_Liao = { offmap_china = { set_offmap_name = "liao_china" } }
									china_name_Yuan = { offmap_china = { set_offmap_name = "yuan_china" } }
									china_name_Xia = { offmap_china = { set_offmap_name = "xia_china" } }
									china_name_Qing = { offmap_china = { set_offmap_name = "qing_china" } }
									china_name_Dai = { offmap_china = { set_offmap_name = "dai_china" } }
									china_name_Cheng = { offmap_china = { set_offmap_name = "cheng_china" } }
									china_name_Liang = { offmap_china = { set_offmap_name = "liang_china" } }
									china_name_Song = { offmap_china = { set_offmap_name = "song_china" } }
									china_name_Ming = { offmap_china = { set_offmap_name = "ming_china" } }
									china_name_Shu = { offmap_china = { set_offmap_name = "shu_china" } }
									china_name_Wu = { offmap_china = { set_offmap_name = "wu_china" } }
									china_name_Chu = { offmap_china = { set_offmap_name = "chu_china" } }
									china_name_Yue = { offmap_china = { set_offmap_name = "yue_china" } }
									china_name_Yin = { offmap_china = { set_offmap_name = "yin_china" } }
									china_name_Shun = { offmap_china = { set_offmap_name = "shun_china" } }
								}

								clr_title_flag = pretender_chinese_empire
								clr_title_flag = uses_temple_names_by_script
								clear_flags_with_prefix = temple_name_
								clear_flags_with_prefix = china_name_
								set_name = "[Root.Capital.GetName] Empire"
							}

							clr_character_flag = given_temple_name_by_script
							set_name = ""

							set_correct_feudal_government_type_effect = yes
						}
					}

					set_offmap_flag = no_succession_news
					set_offmap_flag = no_war_news
					clr_offmap_flag = china_invaded_player_dynasty
					set_offmap_flag = china_invaded_player_dynasty
					set_offmap_holder = event_target:invasion_of_china_claimant
					clr_offmap_flag = no_succession_news
					clr_offmap_flag = rename_override

					if = {
						limit = {
							NOR = {
								has_status = china_stable
								has_status = china_golden_age
							}
						}

						set_offmap_flag = no_status_news
						set_status = china_stable
						clr_offmap_flag = no_status_news
					}
				}

				any_player = {
					limit = {
						OR = {
							AND = {
								has_offmap_news_enabled = offmap_china
								is_within_diplo_range = defender
							}

							vassal_of = attacker
						}
					}

					narrative_event = { id = JD.60401 } # News Event: Character wins invasion of China
				}
			}

			participation_scaled_prestige = 5000

			set_offmap_currency = {
				offmap = offmap_china
				value = 5000
			}

			wealth = 10000

			add_character_modifier = {
				name = chinese_favored_in_trade
				years = 50
				inherit = yes
			}

			if = {
				limit = { NOT = { has_artifact = jade_dragon } }
				add_artifact = jade_dragon
			}
			if = {
				limit = { NOT = { has_artifact = chinese_dragon_amulet } }
				add_artifact = chinese_dragon_amulet
			}
			if = {
				limit = { NOT = { has_artifact = water_clock } }
				add_artifact = water_clock
			}
			if = {
				limit = { NOT = { has_artifact = jian_sword } }
				add_artifact = jian_sword
			}

			hidden_effect = {
				add_character_modifier = {
					name = peace_deal_with_china
					inherit = yes
					years = 50
				}
			}

			remove_character_modifier = china_raid_active
			remove_character_modifier = war_with_china_modifier
			
			### KCC ###
			if = { 
				limit = { this_has_super_cool_nickname_trigger = no }
				give_nickname = nick_the_dragontamer_kcc
			}
			if = {
				limit = {
					has_dlc = "Holy Fury"
					is_female = no
					NOT = { any_owned_bloodline = { has_bloodline_flag = bloodline_kcc_chinese_command } }
				}
				create_bloodline = {
					type = kcc_dragontamer
				}
				sound_effect = bloodline_added
			}
			else_if = {
				limit = {
					has_dlc = "Holy Fury"
					is_female = yes
					NOT = { any_owned_bloodline = { has_bloodline_flag = bloodline_kcc_chinese_command } }
				}
				create_bloodline = {
					type = kcc_dragontamer
					inheritance = matrilineal
				}
				sound_effect = bloodline_added
			}
			if = {
				limit = {
					NOT = { has_character_flag = taught_chinese_strategy }
				}
				set_character_flag = taught_chinese_strategy
			}
			custom_tooltip = {
				text = kcc_military_secrets
				if = {
					limit = {
						NOR = {
							trait = logistics_expert
							trait = master_of_flame
							trait = sapper
							trait = levy_coordinator
						}
					}
					random_list = {
						25 = { add_trait = logistics_expert }
						25 = { add_trait = master_of_flame }
						25 = { add_trait = sapper }
						25 = { add_trait = levy_coordinator }
					}
				}
				else = {
					change_martial = 2
				}
			}
			hidden_effect = {
				if = {
					limit = {
						religion = catholic
						NOT = { has_global_flag = catholics_in_china }
					}
					set_global_flag = catholics_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_catholics" value = 1.5 } }
				}
				if = {
					limit = {
						religion = cathar
						NOT = { has_global_flag = cathars_in_china }
					}
					set_global_flag = cathars_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_cathars" value = 1.5 } }
				}
				if = {
					limit = {
						religion = fraticelli
						NOT = { has_global_flag = fraticellis_in_china }
					}
					set_global_flag = fraticellis_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_fraticellis" value = 1.5 } }
				}
				if = {
					limit = {
						religion = waldensian
						NOT = { has_global_flag = waldensians_in_china }
					}
					set_global_flag = waldensians_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_waldensians" value = 1.5 } }
				}
				if = {
					limit = {
						religion = lollard
						NOT = { has_global_flag = lollards_in_china }
					}
					set_global_flag = lollards_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_lollards" value = 1.5 } }
				}
				if = {
					limit = {
						religion = orthodox
						NOT = { has_global_flag = orthodoxy_in_china }
					}
					set_global_flag = orthodoxy_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_orthodoxs" value = 1.5 } }
				}
				if = {
					limit = {
						religion = miaphysite
						NOT = { has_global_flag = miaphysites_in_china }
					}
					set_global_flag = miaphysites_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_miaphysites" value = 1.5 } }
				}
				if = {
					limit = {
						religion = monophysite
						NOT = { has_global_flag = monophysites_in_china }
					}
					set_global_flag = monophysites_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_monophysites" value = 1.5 } }
				}
				if = {
					limit = {
						religion = bogomilist
						NOT = { has_global_flag = bogomilists_in_china }
					}
					set_global_flag = bogomilists_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_bogomilists" value = 1.5 } }
				}
				if = {
					limit = {
						religion = monothelite
						NOT = { has_global_flag = monothelites_in_china }
					}
					set_global_flag = monothelites_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_monothelites" value = 1.5 } }
				}
				if = {
					limit = {
						religion = iconoclast
						NOT = { has_global_flag = iconoclasts_in_china }
					}
					set_global_flag = iconoclasts_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_iconoclasts" value = 1.5 } }
				}
				if = {
					limit = {
						religion = paulician
						NOT = { has_global_flag = paulicians_in_china }
					}
					set_global_flag = paulicians_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_paulicians" value = 1.5 } }
				}
				if = {
					limit = {
						religion = nestorian
						NOT = { has_global_flag = nestorians_in_china }
					}
					set_global_flag = nestorians_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_nestorians" value = 1.5 } }
				}
				if = {
					limit = {
						religion = messalian
						NOT = { has_global_flag = messalians_in_china }
					}
					set_global_flag = messalians_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_messalians" value = 1.5 } }
				}
				if = {
					limit = {
						religion = sunni
						NOT = { has_global_flag = sunnis_in_china }
					}
					set_global_flag = sunnis_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_sunnis" value = 1.5 } }
				}
				if = {
					limit = {
						religion = zikri
						NOT = { has_global_flag = zikris_in_china }
					}
					set_global_flag = zikris_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_zikris" value = 1.5 } }
				}
				if = {
					limit = {
						religion = ibadi
						NOT = { has_global_flag = ibadis_in_china }
					}
					set_global_flag = ibadis_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_ibadis" value = 1.5 } }
				}
				if = {
					limit = {
						religion = kharijite
						NOT = { has_global_flag = kharijites_in_china }
					}
					set_global_flag = kharijites_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_kharijites" value = 1.5 } }
				}
				if = {
					limit = {
						religion = shiite
						NOT = { has_global_flag = shiites_in_china }
					}
					set_global_flag = shiites_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_shiites" value = 1.5 } }
				}
				if = {
					limit = {
						religion = qarmatian
						NOT = { has_global_flag = qarmatian_in_china }
					}
					set_global_flag = qarmatian_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_qarmatians" value = 1.5 } }
				}
				if = {
					limit = {
						religion = hurufi
						NOT = { has_global_flag = hurufis_in_china }
					}
					set_global_flag = hurufis_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_hurufis" value = 1.5 } }
				}
				if = {
					limit = {
						religion = zoroastrian
						NOT = { has_global_flag = zoroastrians_in_china }
					}
					set_global_flag = zoroastrians_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_zoroastrians" value = 1.5 } }
				}
				if = {
					limit = {
						religion = mazdaki
						NOT = { has_global_flag = mazdakis_in_china }
					}
					set_global_flag = mazdakis_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_mazdakis" value = 1.5 } }
				}
				if = {
					limit = {
						religion = manichaean
						NOT = { has_global_flag = manicheans_in_china }
					}
					set_global_flag = manicheans_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_manicheans" value = 1.5 } }
				}
				if = {
					limit = {
						religion = buddhist
						NOT = { has_global_flag = buddhists_in_china }
					}
					set_global_flag = buddhists_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_buddhists" value = 1.5 } }
				}
				if = {
					limit = {
						religion = jain
						NOT = { has_global_flag = jains_in_china }
					}
					set_global_flag = jains_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_jains" value = 1.5 } }
				}
				if = {
					limit = {
						religion = norse_pagan_reformed
						NOT = { has_global_flag = norse_in_china }
					}
					set_global_flag = norse_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_norse_pagan_reformeds" value = 1.5 } }
				}
				if = {
					limit = {
						religion = tengri_pagan_reformed
						NOT = { has_global_flag = tengri_in_china }
					}
					set_global_flag = tengri_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_tengri_pagan_reformeds" value = 1.5 } }
				}
				if = {
					limit = {
						religion = baltic_pagan_reformed
						NOT = { has_global_flag = baltics_in_china }
					}
					set_global_flag = baltics_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_baltic_pagan_reformeds" value = 1.5 } }
				}
				if = {
					limit = {
						religion = finnish_pagan_reformed
						NOT = { has_global_flag = finnish_in_china }
					}
					set_global_flag = finnish_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_finnish_pagan_reformeds" value = 1.5 } }
				}
				if = {
					limit = {
						religion = aztec_pagan_reformed
						NOT = { has_global_flag = aztecs_in_china }
					}
					set_global_flag = aztecs_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_aztec_reformeds" value = 1.5 } }
				}
				if = {
					limit = {
						religion = slavic_pagan_reformed
						NOT = { has_global_flag = slavs_in_china }
					}
					set_global_flag = slavs_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_slavic_pagan_reformeds" value = 1.5 } }
				}
				if = {
					limit = {
						religion = west_african_pagan_reformed
						NOT = { has_global_flag = africans_in_china }
					}
					set_global_flag = africans_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_west_african_pagan_reformeds" value = 1.5 } }
				}
				if = {
					limit = {
						religion = zun_pagan_reformed
						NOT = { has_global_flag = zun_in_china }
					}
					set_global_flag = zun_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_zun_pagan_reformeds" value = 1.5 } }
				}
				if = {
					limit = {
						religion = hellenic_pagan_reformed
						NOT = { has_global_flag = hellenics_in_china }
					}
					set_global_flag = hellenics_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_hellenic_pagan_reformeds" value = 1.5 } }
				}
				if = {
					limit = {
						religion = bon_pagan_reformed
						NOT = { has_global_flag = bon_in_china }
					}
					set_global_flag = bon_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_bon_reformeds" value = 1.5 } }
				}
				if = {
					limit = {
						religion = celtic_pagan_reformed
						NOT = { has_global_flag = celts_in_china }
					}
					set_global_flag = celts_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_druidic_reformeds" value = 1.5 } }
				}
				if = {
					limit = {
						religion = egyptian_pagan_reformed
						NOT = { has_global_flag = egyptians_in_china }
					}
					set_global_flag = egyptians_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_egyptian_pagans" value = 1.5 } }
				}
				if = {
					limit = {
						religion = aten_pagan
						NOT = { has_global_flag = atenics_in_china }
					}
					set_global_flag = atenics_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_aten_pagans" value = 1.5 } }
				}
				if = {
					limit = {
						religion = britannic_pagan
						NOT = { has_global_flag = britannics_in_china }
					}
					set_global_flag = britannics_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_britannic_pagans" value = 1.5 } }
				}
				if = {
					limit = {
						religion = promethean_pagan
						NOT = { has_global_flag = prometheans_in_china }
					}
					set_global_flag = prometheans_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_promethean_pagans" value = 1.5 } }
				}
				#Christian Immersion
				if = {
					limit = {
						religion = adoptionist
						NOT = { has_global_flag = adoptionists_in_china }
					}
					set_global_flag = adoptionists_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_adoptionists" value = 1.5 } }
				}
				if = {
					limit = {
						religion = free_spirits
						NOT = { has_global_flag = free_spirits_in_china }
					}
					set_global_flag = free_spirits_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_free_spirits" value = 1.5 } }
				}
				if = {
					limit = {
						religion = barlaamite
						NOT = { has_global_flag = barlaamites_in_china }
					}
					set_global_flag = barlaamites_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_barlaamites" value = 1.5 } }
				}
				if = {
					limit = {
						religion = judaizer
						NOT = { has_global_flag = judaizers_in_china }
					}
					set_global_flag = judaizers_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_judaizers" value = 1.5 } }
				}
				if = {
					limit = {
						religion = mandean
						NOT = { has_global_flag = mandeans_in_china }
					}
					set_global_flag = mandeans_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_mandeans" value = 1.5 } }
				}
				if = {
					limit = {
						religion = chaldean
						NOT = { has_global_flag = chaldeans_in_china }
					}
					set_global_flag = chaldeans_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_chaldeans" value = 1.5 } }
				}
				if = {
					limit = {
						religion = apollinarian
						NOT = { has_global_flag = apollinarians_in_china }
					}
					set_global_flag = apollinarians_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_apollinarians" value = 1.5 } }
				}
				if = {
					limit = {
						religion = yarsan
						NOT = { has_global_flag = yarsans_in_china }
					}
					set_global_flag = yarsans_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_yarsans" value = 1.5 } }
				}
				if = {
					limit = {
						religion = syriac
						NOT = { has_global_flag = syriacs_in_china }
					}
					set_global_flag = syriacs_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_syriacs" value = 1.5 } }
				}
				#Last Vanilla Religions
				if = {
					limit = {
						has_game_rule = {
							name = kcc_who_can_preach
							value = kcc_preach_yes
						}
						religion = yazidi
						NOT = { has_global_flag = yazidis_in_china }
					}
					set_global_flag = yazidis_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_yazidis" value = 1.5 } }
				}
				if = {
					limit = {
						has_game_rule = {
							name = kcc_who_can_preach
							value = kcc_preach_yes
						}
						religion = druze
						NOT = { has_global_flag = druze_in_china }
					}
					set_global_flag = druze_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_druzes" value = 1.5 } }
				}
				if = {
					limit = {
						has_game_rule = {
							name = kcc_who_can_preach
							value = kcc_preach_yes
						}
						religion = khurmazta
						NOT = { has_global_flag = khurmaztas_in_china }
					}
					set_global_flag = khurmaztas_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_khurmaztas" value = 1.5 } }
				}
				if = {
					limit = {
						has_game_rule = {
							name = kcc_who_can_preach
							value = kcc_preach_yes
						}
						religion = jewish
						NOT = { has_global_flag = jews_in_china }
					}
					set_global_flag = jews_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_jewishs" value = 1.5 } }
				}
				if = {
					limit = {
						has_game_rule = {
							name = kcc_who_can_preach
							value = kcc_preach_yes
						}
						religion = samaritan
						NOT = { has_global_flag = samaritans_in_china }
					}
					set_global_flag = samaritans_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_samaritans" value = 1.5 } }
				}
				if = {
					limit = {
						has_game_rule = {
							name = kcc_who_can_preach
							value = kcc_preach_yes
						}
						religion = karaite
						NOT = { has_global_flag = karaites_in_china }
					}
					set_global_flag = karaites_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_karaites" value = 1.5 } }
				}
				if = {
					limit = {
						has_game_rule = {
							name = kcc_who_can_preach
							value = kcc_preach_yes
						}
						religion = chasidish
						NOT = { has_global_flag = chasidish_in_china }
					}
					set_global_flag = chasidish_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_chasidishs" value = 1.5 } }
				}
				if = {
					limit = {
						has_game_rule = {
							name = kcc_who_can_preach
							value = kcc_preach_yes
						}
						religion = hindu
						NOT = { has_global_flag = hindus_in_china }
					}
					set_global_flag = hindus_in_china
					hidden_tooltip = { set_variable = { which = "global_chinese_hindus" value = 1.5 } }
				}
			}
		}

		any_attacker = {
			limit = { NOT = { character = attacker } }
			hidden_effect = { participation_scaled_prestige = 2000 }
		}

		clear_global_event_target = invasion_of_china_claimant
	}

	on_invalidation = {
		attacker = {
			show_scope_change = no

			prestige = -2500
			remove_character_modifier = war_with_china_modifier

			custom_tooltip = {
				text = invasion_of_china_cb_fail_tt

				add_character_modifier = {
					name = failed_invasion_of_china
					duration = -1
					hidden = yes
				}
			}

			hidden_effect = {
				offmap_china = { set_offmap_flag = no_war_news }

				any_player = {
					limit = {
						OR = {
							AND = {
								has_offmap_news_enabled = offmap_china
								is_within_diplo_range = defender
							}

							vassal_of = attacker
						}
					}

					narrative_event = { id = JD.60403 } # News Event: Claimant on China dies, invasion over
				}
			}

			custom_tooltip = {
				text = angered_china_tt # Scripter! Update this is you change the value below.

				add_character_modifier = {
					name = angered_china_modifier
					years = 50
					inherit = yes
				}
			}
		}

		defender = {
			show_scope_change = no

			piety = 200
			participation_scaled_prestige = 250
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 250 }
		}

		clear_global_event_target = invasion_of_china_claimant
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			prestige = -3000

			transfer_scaled_wealth = {
				to = defender
				value = 5.0
				min = 5000
			}

			remove_character_modifier = war_with_china_modifier

			custom_tooltip = {
				text = invasion_of_china_cb_fail_tt

				add_character_modifier = {
					name = failed_invasion_of_china
					duration = -1
					hidden = yes
				}
			}

			custom_tooltip = {
				text = destabilization_china_success_tt

				offmap_china = { set_offmap_flag = no_war_news }

				any_player = {
					limit = {
						OR = {
							AND = {
								has_offmap_news_enabled = offmap_china
								is_within_diplo_range = defender
							}

							vassal_of = attacker
						}
					}

					narrative_event = { id = JD.60402 } # News Event: Character loses invasion of China
				}

				any_demesne_title = {
					limit = {
						higher_real_tier_than = DUKE
						controls_religion = no
						clan = no
					}

					destroy_landed_title = THIS
				}
			}

			custom_tooltip = {
				text = angered_china_tt # Scripter! Update this is you change the value below.

				add_character_modifier = {
					name = angered_china_modifier
					years = 50
					inherit = yes
				}
			}
		}

		defender = {
			show_scope_change = no

			piety = 200
			participation_scaled_prestige = 1000
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 1000 }
		}

		clear_global_event_target = invasion_of_china_claimant
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = {
		factor = 0
	}
}
