# IDs JD.10106, 10107, 10131
#     JD.30030 - 30053

namespace = JD

character_event = {
	id = JD.30020

	hide_window = yes
	is_triggered_only = yes # on_war_started

	has_dlc = "Jade Dragon"

	trigger = {
		# China is the aggressor
		offmap_china = {
			governor = {
				character = FROM
			}
		}
	}

	immediate = {
	#	save_event_target_as = china_war_opponent

		# Abort Kowtow chain if necessary
		if = {
			limit = { trait = kow_tow_travels }

			set_character_flag = kow_tow_abort_war
			character_event = { id = JD.32299 }
		}

		FROM = {
		#	save_event_target_as = china_war_governor
		#	save_event_target_as = china_war_aggressor
			# Swap the From and root scope and merge up with JD.30030
			character_event = { id = JD.30040 }
		}

		log = "JD.30020 - This: [This.GetBestName] From: [From.GetBestName]"
	}
}

character_event = {
	id = JD.30030

	hide_window = yes
	is_triggered_only = yes # on_war_started

	has_dlc = "Jade Dragon"

	trigger = {
		# China is the defender
		offmap_china = {
			governor = {
				character = ROOT
			}
		}
	}

	immediate = {
	#	save_event_target_as = china_war_governor
	#	save_event_target_as = china_war_aggressor
		FROM = {
		#	save_event_target_as = china_war_opponent

			# Abort Kowtow chain if necessary
			if = {
				limit = { trait = kow_tow_travels }

				set_character_flag = kow_tow_abort_war
				character_event = { id = JD.32299 }
			}

			# Remove Kowtow trait if character is takes hostile actions
			remove_trait = kow_tow_completed_tier_1
			remove_trait = kow_tow_completed_tier_2
			remove_trait = kow_tow_completed_tier_3
		}

		# Keep the From and root scope and merge up with JD.30020
		repeat_event = { id = JD.30040 }
		log = "JD.30030 - This: [This.GetBestName] From: [From.GetBestName]"
	}
}

character_event = {
	id = JD.30040

	hide_window = yes
	is_triggered_only = yes

	has_dlc = "Jade Dragon"

	immediate = {
		offmap_china = {
			if = {
				limit = {
					NOT = { has_offmap_flag = china_entered_war }
				}

				set_offmap_flag = china_entered_war
			}
		}

		if = { # Variables set elsewhere, run effects and notification event
			limit = { NOT = { has_offmap_tmp_flag = china_war_variables_set } }

			### To manipulate the troop spawning for specific situations modify the parameter variables (china_war_troop_quantity, china_war_troop_quality, china_war_target_match_mult, and china_war_minimum_commander_martial) after the following block.
			set_variable = {
				which = local_china_war_troop_quantity
				value = 8
				# 8 regiments of ~500 units
			}

			set_variable = {
				which = local_china_war_target_match_mult
				value = 0.95
			}

			set_variable = {
				which = local_china_war_troop_quality
				value = 4
				# Value between 1-7
			}

			set_variable = {
				which = local_china_war_minimum_commander_martial
				value = 14
			}
			###
		}
		else = {
			clr_offmap_tmp_flag = china_war_variables_set
		}

		# Manipulate the parameter variables dependent on the current status in China.
		china_war_modify_variables_by_status_effect = yes

		# Manipulate the parameter variables dependent on the current century.
		china_war_modify_variables_by_century_effect = yes

		### Match mult calculation against the opponents troops.
		export_to_variable = {
			which = local_china_temp_troop_calc
			value = realm_levies_plus_allies
			who = FROM
		}

		divide_variable = {
			which = local_china_temp_troop_calc
			value = 500
		}

		multiply_variable = {
			which = local_china_temp_troop_calc
			which = local_china_war_target_match_mult
		}

		change_variable = {
			which = local_china_war_troop_quantity
			which = local_china_temp_troop_calc
		}
		###

		### Remove troops for a portion of levies of the western protectorate.
		export_to_variable = {
			which = local_china_temp_troop_calc
			value = realm_levies
		}

		subtract_variable = {
			which = local_china_temp_troop_calc
			value = 7500 ## AI extra consideration from the spawned troops needs to be removed here
		}

		divide_variable = {
			which = local_china_temp_troop_calc
			value = 780 # For every 780 troops of levies Mainland will send 500 troops less to help.
		}

		subtract_variable = {
			which = local_china_war_troop_quantity
			which = local_china_temp_troop_calc
		}
		###

		if = {
			limit = {
				NOT = {
					any_courtier_or_vassal = {
						count >= 3
						martial = local_china_war_minimum_commander_martial
					}
				}
			}

			# Not enough proficient commanders, create more
			# Custom effect utilizes china_war_minimum_commander_martial variable
			log = "JD.30040 - spawning commanders"
			china_spawn_commander_effect = yes
			china_spawn_commander_effect = yes
			china_spawn_commander_effect = yes
		}

		if = {
			limit = {
				check_variable = {
					which = local_china_war_troop_quantity
					value > 0
				}
			}

			# Hardcap at 175k troopspawn
			if = {
				limit = {
					check_variable = {
						which = local_china_war_troop_quantity
						value > 350
					}
				}

				set_variable = {
					which = local_china_war_troop_quantity
					value = 350
				}
			}

			# Troop spawn override
			if = {
				limit = {
					check_variable = {
						which = local_china_war_troop_quantity_override
						value > 0
					}

					check_variable = {
						which = local_china_war_troop_quantity_override
						which = local_china_war_troop_quantity
					}
				}

				set_variable = {
					which = local_china_war_troop_quantity
					which = local_china_war_troop_quantity_override
				}

				set_variable = {
					which = local_china_war_troop_quantity_override
					value = 0
				}
			}

			if = {
				limit = {
					is_landed = yes

					capital_scope = {
						region = custom_eastern_edge_of_map
					}
				}

				capital_scope = {
					save_event_target_as = spawn_province
				}
			}
			else_if = {
				limit = {
					FROM = {
						capital_scope = {
							NOT = { region = world_india }
						}
					}
				}

				random_list = {
					10 = { c_anxi       = { location = { save_event_target_as = spawn_province } } }
					10 = { c_dunhuang   = { location = { save_event_target_as = spawn_province } } }
					10 = { c_jiuquan    = { location = { save_event_target_as = spawn_province } } }
					10 = { c_kumul      = { location = { save_event_target_as = spawn_province } } } # Yizhou
					10 = { c_kara_khoja = { location = { save_event_target_as = spawn_province } } } # Gaochang
					10 = { c_luntai     = { location = { save_event_target_as = spawn_province } } } # Luntai
				}

				# Currently not compatible with hardcap 200k
				# if = { # Spawning more than 11k troops will split the stack.
				#	limit = {
				#		check_variable = {
				#			which = china_war_troop_quantity
				#			value > 22
				#		}
				#	}
				#	set_variable = {
				#		which = china_war_troop_quantity_temp
				#		which = china_war_troop_quantity
				#	}
				#	divide_variable = {
				#		which = china_war_troop_quantity
				#		value = 2
				#	}
				#	subtract_variable = {
				#		which = china_war_troop_quantity_temp
				#		which = china_war_troop_quantity
				#	}
				#	china_war_spawn_troops_effect = yes
				#	# Requires the current things to function properly:
				#	# event_target:spawn_province
				#	# variable: china_war_troop_quality
				#	# variable: china_war_troop_quantity
				#	set_variable = {
				#		which = china_war_troop_quantity
				#		which = china_war_troop_quantity_temp
				#	}
				#	1514 = { # ejin
				#		save_event_target_as = spawn_province
				#	}
				# }

				china_war_spawn_troops_effect = yes
				# Requires the current things to function properly:
				# event_target:spawn_province
				# variable: china_war_troop_quality
				# variable: china_war_troop_quantity
			}
			else = {
				offmap_china = { set_offmap_flag = troop_spawn_india_temp }

				c_samatata = {
					location = {
						save_event_target_as = spawn_province

						sea_zone = { # Ganges Eelta
							save_event_target_as = fleet_spawn_location
						}
					}
				}

				set_variable = {
					which = local_boat_spawns
					which = local_china_war_troop_quantity
				}

				multiply_variable = {
					which = local_boat_spawns
					value = 5
				}

				while = {
					limit = {
						check_variable = {
							which = local_boat_spawns
							value >= 1
						}
					}

					change_variable = {
						which = local_boat_spawns
						value = -1
					}

					spawn_fleet = {
						province = event_target:fleet_spawn_location
						owner = ROOT
						disband_on_peace = yes
						merge = yes

						troops = {
							galleys = { 1 1 }
						}
					}
				}

				china_war_spawn_troops_effect = yes
				# Requires the current things to function properly:
				# event_target:spawn_province
				# variable: china_war_troop_quality
				# variable: china_war_troop_quantity
			}

			FROM = { # Notify enemy of extra troops.
				character_event = { id = JD.30045 }
			}
		}
	}
}

character_event = {
	id = JD.30045
	picture = GFX_evt_china_invades
	border = GFX_event_normal_frame_war

	desc = {
		text = EVTDESC_JD_30045
		trigger = {
			NOT = { offmap_china = { has_offmap_flag = troop_spawn_india_temp } }
		}
	}
	desc = {
		text = EVTDESC_JD_30045_INDIA
		trigger = {
			offmap_china = { has_offmap_flag = troop_spawn_india_temp }
		}
	}

	is_triggered_only = yes

	has_dlc = "Jade Dragon"

	option = {
		name = EVTOPTA_JD_30045

		if = {
			limit = {
				offmap_china = { has_offmap_flag = troop_spawn_india_temp }
			}

			custom_tooltip = { text = EVTTOOLTIPB_JD_30045 }

			hidden_effect = {
				offmap_china = { clr_offmap_flag = troop_spawn_india_temp }
			}
		}
		else = {
			custom_tooltip = { text = EVTTOOLTIPA_JD_30045 }
		}
	}
}

character_event = {
	id = JD.30047
	desc = EVTDESC_JD_30047
	picture = GFX_evt_china_rebel_general
	border = GFX_event_normal_frame_war

	is_triggered_only = yes

	has_dlc = "Jade Dragon"

	option = {
		name = EVTOPTA_JD_30047

		if = {
			limit = {
				offmap_china = { has_offmap_flag = troop_spawn_india_temp }
			}

			custom_tooltip = { text = EVTTOOLTIPB_JD_30045 }

			hidden_effect = {
				offmap_china = { clr_offmap_flag = troop_spawn_india_temp }
			}
		}
		else = {
			custom_tooltip = { text = EVTTOOLTIPA_JD_30045 }
		}
	}
}

character_event = {
	id = JD.30050

	hide_window = yes
	is_triggered_only = yes # on_war_ended_invalid, on_war_ended_whitepeace

	has_dlc = "Jade Dragon"

	trigger = {
		# China is a main participant
		offmap_china = {
			governor = {
				OR = {
					character = ROOT
					character = FROM
				}
			}
		}
	}

	immediate = {
		character_event = { id = JD.30053 days = 1 } # Delayed event to clear flags properly

		if = {
			limit = {
				offmap_china = {
					governor = {
						character = ROOT
					}
				}
			}

			### China was the defender...
			offmap_china = { set_offmap_tmp_flag = china_was_defender_but_white_peace }
			ROOT = { save_event_target_as = the_protector_general }
			FROM = { save_event_target_as = the_aggressor }
		}
		else = {
			### China was the aggressor...
			offmap_china = { set_offmap_tmp_flag = china_was_aggressor_but_white_peace }
			FROM = { save_event_target_as = the_protector_general }
			ROOT = { save_event_target_as = the_defender }
		}

		if = {
			limit = { using_unimportant_cb = no } # Makes sure this is not sent during unimportant wars (revolts, etc)...

			any_player = {
				limit = {
					has_offmap_news_enabled = offmap_china
					is_within_diplo_range = event_target:the_protector_general
				}

				narrative_event = { id = JD.10131 days = 1 } # News: "China white peace'd with someone"
			}
		}
	}
}

character_event = {
	id = JD.30051

	hide_window = yes
	is_triggered_only = yes # on_war_ended_victory

	has_dlc = "Jade Dragon"

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

	immediate = {
		character_event = { id = JD.30053 days = 1 } # Delayed event to clear flags properly

		if = {
			limit = {
				offmap_china = {
					governor = {
						character = ROOT
					}
				}
			}

			### China lost a defensive war
			if = {
				limit = { using_unimportant_cb = no } # Makes sure this is not sent during unimportant wars (revolts, etc)...

				ROOT = { save_event_target_as = the_protector_general }
				FROM = { save_event_target_as = the_winner }

				offmap_china = {
					set_offmap_tmp_flag = china_lost_defensive_war
				}

				any_player = {
					limit = {
						has_offmap_news_enabled = offmap_china
						is_within_diplo_range = event_target:the_protector_general
					}
					narrative_event = { id = JD.10106 days = 1 } # News: "China lost a war"
				}
			}
		}
		else = {
			### China won an offensive war
			if = {
				limit = { using_unimportant_cb = no } # Makes sure this is not sent during unimportant wars (revolts, etc)...

				FROM = { save_event_target_as = the_protector_general }
				ROOT = { save_event_target_as = the_loser }

				offmap_china = {
					set_offmap_tmp_flag = china_won_offensive_war
				}

				any_player = {
					limit = {
						has_offmap_news_enabled = offmap_china
						is_within_diplo_range = event_target:the_protector_general
					}

					narrative_event = { id = JD.10107 days = 1 } # News: "China won a war"
				}
			}
		}
	}
}

character_event = {
	id = JD.30052

	hide_window = yes
	is_triggered_only = yes # on_war_ended_defeat

	has_dlc = "Jade Dragon"

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

	immediate = {
		character_event = { id = JD.30053 days = 1 } # Delayed event to clear flags properly

		if = {
			limit = {
				offmap_china = {
					governor = {
						character = ROOT
					}
				}
			}

			### China won a defensive war
			if = {
				limit = { using_unimportant_cb = no } # Makes sure this is not sent during unimportant wars (revolts, etc)...

				ROOT = { save_event_target_as = the_protector_general }
				FROM = { save_event_target_as = the_loser }

				offmap_china = {
					set_offmap_tmp_flag = china_won_defensive_war
				}

				any_player = {
					limit = {
						has_offmap_news_enabled = offmap_china
						is_within_diplo_range = event_target:the_protector_general
					}

					narrative_event = { id = JD.10107 days = 1 } # News: "China won a war"
				}
			}
		}
		else = {
			### China lost an offensive war
			if = {
				limit = { using_unimportant_cb = no } # Makes sure this is not sent during unimportant wars (revolts, etc)...

				FROM = { save_event_target_as = the_protector_general }
				ROOT = { save_event_target_as = the_winner }

				offmap_china = {
					set_offmap_tmp_flag = china_lost_offensive_war
				}

				any_player = {
					limit = {
						has_offmap_news_enabled = offmap_china
						is_within_diplo_range = event_target:the_protector_general
					}

					narrative_event = { id = JD.10106 days = 1 } # News: "China lost a war"
				}
			}
		}
	}
}

character_event = {
	id = JD.30053

	hide_window = yes
	is_triggered_only = yes

	has_dlc = "Jade Dragon"

	immediate = {
		offmap_china = {
			if = {
				limit = {
					governor = { war = no }
				}

				clr_offmap_flag = china_sent_war_reinforcements
				clr_offmap_flag = china_entered_war
			}
		}
	}
}

narrative_event = { # NEWS: visible event for player (China lost a war)
	id = JD.10106
	title = NEWS_FROM_CHINA
	picture = GFX_evt_china_civil_war
	portrait = offmap_china
	window = EventWindowOffmap
	background = GFX_event_window_news_from_china

	desc = {
		text = EVTDESC_JD_10106_A
		trigger = {
			offmap_china = {
				has_offmap_tmp_flag = china_lost_offensive_war
			}
		}
	}
	desc = {
		text = EVTDESC_JD_10106_B
		trigger = {
			offmap_china = {
				has_offmap_tmp_flag = china_lost_defensive_war
			}
		}
	}

	is_triggered_only = yes

	has_dlc = "Jade Dragon"

	trigger = {
		NOT = { offmap_china = { has_offmap_flag = no_war_news } }
	}

	fail_trigger_effect = {
		offmap_china = { clr_offmap_flag = no_war_news }
	}

	option = {
		name = {
			text = EVTOPTA_JD_10106
			trigger = {
				character = FROMFROM # if you, the player who receives this event, was the winner of the war
			}
		}
		name = {
			text = EVTOPTB_JD_10106
			trigger = {
				NOT = { character = FROMFROM } # if not
			}
		}

		custom_tooltip = { text = EVTOPTA_JD_10106_TT }

		show_portrait = event_target:the_winner

		hidden_effect = {
			offmap_china = {
				clr_offmap_tmp_flag = china_lost_offensive_war
				clr_offmap_tmp_flag = china_lost_defensive_war
			}
		}
	}
}

narrative_event = { # NEWS: visible event for player (China won a war)
	id = JD.10107
	title = NEWS_FROM_CHINA
	picture = GFX_evt_china_expansionist
	portrait = offmap_china
	window = EventWindowOffmap
	background = GFX_event_window_news_from_china

	desc = {
		text = EVTDESC_JD_10107_A
		trigger = {
			offmap_china = {
				has_offmap_tmp_flag = china_won_defensive_war
			}
		}
	}
	desc = {
		text = EVTDESC_JD_10107_B
		trigger = {
			offmap_china = {
				has_offmap_tmp_flag = china_won_offensive_war
			}
		}
	}

	is_triggered_only = yes

	has_dlc = "Jade Dragon"

	trigger = {
		NOT = { offmap_china = { has_offmap_flag = no_war_news } }
	}

	fail_trigger_effect = {
		offmap_china = { clr_offmap_flag = no_war_news }
	}

	option = {
		name = {
			text = EVTOPTA_JD_10107
			trigger = { character = FROM } # if you, the player who receives this event, was the loser of the war
		}
		name = {
			text = EVTOPTB_JD_10107
			trigger = { NOT = { character = FROM } } # if not
		}

		custom_tooltip = { text = EVTOPTA_JD_10107_TT }

		show_portrait = event_target:the_loser

		hidden_effect = {
			offmap_china = {
				clr_offmap_tmp_flag = china_won_defensive_war
				clr_offmap_tmp_flag = china_won_offensive_war
			}
		}
	}
}

narrative_event = { # NEWS: visible event for player (China had White Peace in a war)
	id = JD.10131
	title = NEWS_FROM_CHINA
	picture = GFX_evt_china_isolationist
	portrait = offmap_china
	window = EventWindowOffmap
	background = GFX_event_window_news_from_china

	desc = {
		text = EVTDESC_JD_10131_A
		trigger = {
			offmap_china = {
				has_offmap_tmp_flag = china_was_defender_but_white_peace
			}
		}
	}
	desc = {
		text = EVTDESC_JD_10131_B
		trigger = {
			offmap_china = {
				has_offmap_tmp_flag = china_was_aggressor_but_white_peace
			}
		}
	}

	is_triggered_only = yes

	has_dlc = "Jade Dragon"

	trigger = {
		NOT = { offmap_china = { has_offmap_flag = no_war_news } }
	}

	fail_trigger_effect = {
		offmap_china = { clr_offmap_flag = no_war_news }
	}

	option = {
		name = {
			text = EVTOPTA_JD_10131
			trigger = { # if you, the player who receives this event, was in this war
				OR = {
					character = FROM
					character = FROMFROM
				}
			}
		}
		name = {
			text = EVTOPTB_JD_10131
			trigger = { # if not
				NOR = {
					character = FROM
					character = FROMFROM
				}
			}
		}

		if = {
			limit = {
				offmap_china = { has_offmap_tmp_flag = china_was_defender_but_white_peace }
			}

			custom_tooltip = { text = EVTOPTA_JD_10131_TT }

			show_portrait = event_target:the_aggressor
		}
		else_if = {
			limit = {
				offmap_china = { has_offmap_tmp_flag = china_was_aggressor_but_white_peace }
			}

			custom_tooltip = { text = EVTOPTB_JD_10131_TT }

			show_portrait = event_target:the_defender
		}

		hidden_effect = {
			offmap_china = {
				clr_offmap_tmp_flag = china_was_defender_but_white_peace
				clr_offmap_tmp_flag = china_was_aggressor_but_white_peace
			}
		}
	}
}