### NEW CBs 2.8
dejure_duchy_claim = { # New1
	name = CB_NAME_DEJURECLAIM
	war_name = WAR_NAME_DEJURECLAIM
	sprite = 31
	truce_days = 3650
	hostile_against_others = yes
	is_permanent = yes
	check_de_jure_tier = DUKE
	press_claim = yes
	can_ask_to_join_war = no

	allowed_to_target_tributaries = no
	
	sort_priority = 790

	can_use_gui = {
		check_if_crusader_trigger = yes
		ROOT = {
			show_scope_change = no
			conditional_tooltip = {
				trigger = { 
					tier = DUKE
				}
				prestige = 250
			}
			conditional_tooltip = {
				trigger = { 
					tier = KING
				}
				prestige = 500
			}
			conditional_tooltip = {
				trigger = { 
					tier = EMPEROR
				}
				prestige = 1000
			}
		}
	}
	
	on_add = {
		ROOT = {
			if = {
				limit = {
					tier = DUKE
				}
				prestige = -250
			}
			if = {
				limit = {
					tier = KING
				}
				prestige = -500
			}
			if = {
				limit = {
					tier = EMPEROR
				}
				prestige = -1000
			}
		}
		hidden_tooltip = { fire_haruspicy_event_effect = yes }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes
			}
		}
	}
	
	can_use_title = {
		
		OR = {
			holder = ROOT
			kingdom = {
				holder = ROOT
			}
			empire = {
				holder = ROOT
			}
		}
		
		FROM = { # Can only De Jure claim duchies from the same religion group
			religion_group = ROOT
			NOT = {
				is_liege_of = ROOT
			}
		}
		
		OR = { # This ensures that the AI doesn't declare this type of war against someone who only holds one county within the targeted title...
			ROOT = {
				ai = no
			}
			any_de_jure_vassal_title = {
				count = 2
				tier = count
				holder_scope = {
					same_realm = FROM
				}
			}
		}
		
		NAND = { # Though different sects of Muslims must Holy War each other instead
			ROOT = {
				NOT = {
					religion = FROM
				}
			}
			ROOT = {
				religion_group = muslim
			}
			FROM = {
				religion_group = muslim
			}
		}
		
		FROM = {
			OR = {
				holy_order = no
				NOT = { religion = ROOT }
				NOT = { has_dlc = "Sons of Abraham" }
				OR = {
					AND = {
						primary_title = { title = d_knights_templar }
						ROOT = {
							has_character_modifier = expelled_d_knights_templar
						}
					}
					AND = {
						primary_title = { title = d_knights_hospitaler }
						ROOT = {
							has_character_modifier = expelled_d_knights_hospitaler
						}
					}
					AND = {
						primary_title = { title = d_teutonic_order }
						ROOT = {
							has_character_modifier = expelled_d_teutonic_order
						}
					}
					AND = {
						primary_title = { title = d_jomsvikings }
						ROOT = {
							has_character_modifier = expelled_d_jomsvikings
						}
					}
					AND = {
						primary_title = { title = d_hashshashin }
						ROOT = {
							has_character_modifier = expelled_d_hashshashin
						}
					}
					AND = {
						primary_title = { title = d_bektashi }
						ROOT = {
							has_character_modifier = expelled_d_bektashi
						}
					}
					AND = {
						primary_title = { title = d_haruriyyah }
						ROOT = {
							has_character_modifier = expelled_d_haruriyyah
						}
					}
					AND = {
						primary_title = { title = d_holy_sepulchre }
						ROOT = {
							has_character_modifier = expelled_d_holy_sepulchre
						}
					}
					AND = {
						primary_title = { title = d_saint_anthony }
						ROOT = {
							has_character_modifier = expelled_d_saint_anthony
						}
					}
					AND = {
						primary_title = { title = d_immortals }
						ROOT = {
							has_character_modifier = expelled_d_immortals
						}
					}
					AND = {
						primary_title = { title = d_zealots }
						ROOT = {
							has_character_modifier = expelled_d_zealots
						}
					}
					AND = {
						primary_title = { title = d_sky_lords }
						ROOT = {
							has_character_modifier = expelled_d_sky_lords
						}
					}
					AND = {
						primary_title = { title = d_spirit_guardians }
						ROOT = {
							has_character_modifier = expelled_d_spirit_guardians
						}
					}
					AND = {
						primary_title = { title = d_warriors_perun }
						ROOT = {
							has_character_modifier = expelled_d_warriors_perun
						}
					}
					AND = {
						primary_title = { title = d_chosen_perkunas }
						ROOT = {
							has_character_modifier = expelled_d_chosen_perkunas
						}
					}
					AND = {
						primary_title = { title = d_sons_kaleva }
						ROOT = {
							has_character_modifier = expelled_d_sons_kaleva
						}
					}
					AND = {
						primary_title = { title = d_huitzilopochtli }
						ROOT = {
							has_character_modifier = expelled_d_huitzilopochtli
						}
					}
					AND = {
						primary_title = { title = d_knights_santiago }
						ROOT = {
							has_character_modifier = expelled_d_knights_santiago
						}
					}
					AND = {
						primary_title = { title = d_knights_calatrava }
						ROOT = {
							has_character_modifier = expelled_d_knights_calatrava
						}
					}
					AND = {
						primary_title = { title = d_saint_addai }
						ROOT = {
							has_character_modifier = expelled_d_saint_addai
						}
					}
					AND = {
						primary_title = { title = d_zun_warriors }
						ROOT = {
							has_character_modifier = expelled_d_zun_warriors
						}
					}
				}
			}
		}
	}
	
	can_use = {
		has_dlc = "Jade Dragon"
		ROOT = {
			NOT = {
				has_game_rule = {
					name = jade_dragon_cbs
					value = off
				}
			}
		}
	}

	on_success_title = {
		ROOT = {
			vassalize_or_take_under_title = {
				title = PREV
				enemy = FROM
				#anti_nomad = yes # Vassalize non-nomadic rulers
				type = claim
			}
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}

		hidden_tooltip = {
			check_war_effect = yes
			FROM = {
				landless_pope_effect = yes
			}
		}

		
	}

	on_fail_title = {
		ROOT = {
			show_scope_change = no
			prestige = -100
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		ROOT = {
			show_scope_change = no
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 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 = 1
		modifier = {
			factor = 0
			ROOT = {
				has_character_flag = is_temujin
				has_landed_title = e_mongol_empire
				demesne_size = 1
			}
			FROM = {
				has_landed_title = e_china_west_governor
			}
		}
		modifier = { #Focus on Coronation first.
			factor = 0.1
			has_dlc = "Holy Fury"
			ROOT = {
				is_feudal = yes
				OR = {
					religion = catholic
					religion = fraticelli
				}
				NOR = {
					trait = crowned_by_priest
					trait = crowned_by_bishop
					trait = crowned_by_pope
					trait = crowned_by_myself
				}
			}
		}
	}
}

force_vassalization = { # New2
	name = CB_NAME_FORCE_VASSALIZATION
	war_name = WAR_NAME_FORCE_VASSALIZATION
	sprite = 32
	truce_days = 365
	is_permanent = yes
	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no
	
	defender_unoccupied_warscore = yes
	
	sort_priority = 830
	
	can_use_gui = {
		check_if_crusader_trigger = yes
		FROM = {
			show_scope_change = no
			conditional_tooltip = {
				trigger = { 
					tier = COUNT
				}
				ROOT = {
					show_scope_change = no
					OR = { 
						prestige = 250
						custom_tooltip = { 
							text = tooltip_antagonize_vassalize_claim 
							hidden_tooltip = { has_opinion_modifier = { who = FROM modifier = opinion_antagonize_vassalize_claim } } 
						}
					}
				}
			}
			conditional_tooltip = {
				trigger = { 
					tier = DUKE
				}
				ROOT = {
					show_scope_change = no
					OR = { 
						prestige = 1000
						custom_tooltip = { 
							text = tooltip_antagonize_vassalize_claim 
							hidden_tooltip = { has_opinion_modifier = { who = FROM modifier = opinion_antagonize_vassalize_claim } } 
						}
					}
				}
			}
			conditional_tooltip = {
				trigger = { 
					tier = KING
				}
				ROOT = {
					show_scope_change = no
					OR = { 
						prestige = 2500
						custom_tooltip = { 
							text = tooltip_antagonize_vassalize_claim 
							hidden_tooltip = { has_opinion_modifier = { who = FROM modifier = opinion_antagonize_vassalize_claim } } 
						}
					}
				}
			}
		}
	}
	
	on_add = {
		FROM = {
			if = {
				limit = {
					tier = COUNT					
					NOT = { reverse_has_opinion_modifier = { who = ROOT modifier = opinion_antagonize_vassalize_claim } } 
				}
				ROOT = {
					prestige = -250
				}
			}
			if = {
				limit = {
					tier = DUKE
					NOT = { reverse_has_opinion_modifier = { who = ROOT modifier = opinion_antagonize_vassalize_claim } } 
				}
				ROOT = {
					prestige = -1000
				}
			}
			if = {
				limit = {
					tier = KING
					NOT = { reverse_has_opinion_modifier = { who = ROOT modifier = opinion_antagonize_vassalize_claim } } 
				}
				ROOT = {
					prestige = -2500
				}
			}
			if = {
				limit = {
					reverse_has_opinion_modifier = { who = ROOT modifier = opinion_antagonize_vassalize_claim }
				}
				ROOT = {
					remove_opinion = { who = ROOT modifier = opinion_antagonize_vassalize_claim }
				}
			}
		}
		hidden_tooltip = { fire_haruspicy_event_effect = yes }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes
			}
		}
	}
	
	can_use = {
		has_dlc = "Jade Dragon"
		ROOT = {
			NOT = {
				has_game_rule = {
					name = jade_dragon_cbs
					value = off
				}
			}
			higher_tier_than = count
			NOT = { same_realm = FROM }
			NOT = { is_liege_or_above = FROM }
			mercenary = no
			is_nomadic = no
		}
		
		FROM = {
			lower_tier_than = ROOT
			NOT = {
				realm_size = 31
			}
			in_revolt = no
			is_nomadic = no
			controls_religion = no
			holy_order = no
			OR = {
				religion = ROOT
				culture_group = ROOT
			}
			shares_realm_border_with = ROOT
		}
	}
	
	is_valid = {
		FROM = {
			is_landed = yes
			lower_tier_than = ROOT
		}
	}

	on_success = {
		ROOT = {
			show_scope_change = no
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		FROM = {
			show_scope_change = no
			prestige = -100
			set_defacto_liege = ROOT
		}

		hidden_tooltip = {
			check_if_reconquista_finished_effect = yes
			FROM = {
				landless_pope_effect = yes
			}
		}
	}
	
	on_fail = {
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		ROOT = {
			show_scope_change = no
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 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 = 1
		modifier = { #Focus on Coronation first.
			factor = 0.1
			has_dlc = "Holy Fury"
			ROOT = {
				is_feudal = yes
				OR = {
					religion = catholic
					religion = fraticelli
				}
				NOR = {
					trait = crowned_by_priest
					trait = crowned_by_bishop
					trait = crowned_by_pope
					trait = crowned_by_myself
				}
			}
		}
	}
}

great_realm_conflict = { # New3
	name = CB_NAME_GREATREALMCONFLICT
	war_name = WAR_NAME_GREATREALMCONFLICT
	sprite = 33
	truce_days = 3650
	hostile_against_others = yes
	is_permanent = yes
	check_de_jure_tier = KING
	can_ask_to_join_war = no

	allowed_to_target_tributaries = no
	
	sort_priority = 820

	can_use_gui = {
		check_if_crusader_trigger = yes
		ROOT = {
			prestige = 3000
			piety = 1500
		}
		FROM = {
			relative_power = {
				who = ROOT
				power = 0.95
			}
		}
	}
	
	on_add = {
		ROOT = {
			prestige = -3000
			piety = -1500
		}
		hidden_tooltip = { fire_haruspicy_event_effect = yes }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes
			}
		}
	}
	
	can_use = {
		has_dlc = "Jade Dragon"
		ROOT = {
			NOT = {
				has_game_rule = {
					name = jade_dragon_cbs
					value = off
				}
			}
		}
		FROM = {
			relative_power = {
				who = ROOT
				power = 0.95
			}
		}
	}
	
	can_use_title = {
		ROOT = {
			is_nomadic = no
			independent = yes
			realm_size = 200
			OR = {
				tier = KING
				tier = EMPEROR
			}
		}
		FROM = {
			realm_size = 200
			in_revolt = no
			is_nomadic = no
			OR = {
				tier = KING
				tier = EMPEROR
			}
		}
		
		OR = { # This ensures that the AI doesn't declare this type of war against someone who only holds a few counties within the targeted title...
			ROOT = {
				ai = no
			}
			FROM = {
				num_of_realm_counties = {
					value = 5
					title = PREV
				}
			}
		}
		
		any_direct_de_jure_vassal_title = {
			any_direct_de_jure_vassal_title = {
				OR = {
					holder_scope = {
						OR = {
							character = ROOT
							is_liege_or_above = ROOT
						}
					}
					location = {
						any_neighbor_province = {
							OR = {
								AND = {
									has_owner = yes
									owner = {
										OR = {
											character = ROOT
											is_liege_or_above = ROOT
										}
									}
								}
								AND = {
									is_land = no
									any_neighbor_province = {
										OR = {
											AND = {
												has_owner = yes
												owner = {
													OR = {
														character = ROOT
														is_liege_or_above = ROOT
													}
												}
											}
											AND = {
												is_land = no
												any_neighbor_province = {
													owner = {
														OR = {
															character = ROOT
															is_liege_or_above = ROOT
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}

	is_valid_title = {
		FROM = {
			any_realm_province = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}
	
	on_invalidation = {
		ROOT = {
			prestige = 3000
			piety = 1500
		}
	}

	on_success_title = {
		if = { # Claim the targeted Kingdom if it is held by the target
			limit = {
				controls_religion = no
				holder_scope = {
					OR = {
						character = FROM
						is_liege_or_above = FROM
					}
				}
			}
			usurp_title = { target = ROOT type = invasion }
		}
		ROOT = {
			vassalize_or_take_under_title = {
				title = PREV
				enemy = FROM
				type = invasion
			}
			hidden_tooltip = {
				vassalize_or_take_under_title = {
					title = PREV
					enemy = FROM
					type = invasion
				}
			}
		}
		
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}

		hidden_tooltip = {
			check_if_reconquista_finished_effect = yes
			FROM = {
				landless_pope_effect = yes
			}
		}
	}

	on_fail_title = {
		ROOT = {
			show_scope_change = no
			prestige = -1500
			piety = -750
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		ROOT = {
			show_scope_change = no
			prestige = -3000
			piety = -1500
			transfer_scaled_wealth = {
				to = FROM
				value = 10.0
			}
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 3000
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 1500 }
		}
	}

	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 = 1
		modifier = { #Focus on Coronation first.
			factor = 0.1
			has_dlc = "Holy Fury"
			ROOT = {
				is_feudal = yes
				OR = {
					religion = catholic
					religion = fraticelli
				}
				NOR = {
					trait = crowned_by_priest
					trait = crowned_by_bishop
					trait = crowned_by_pope
					trait = crowned_by_myself
				}
			}
		}
	}
}

ducal_county_conquest = { # New4
	name = CB_NAME_DUCALCOUNTYCONQUEST
	war_name = WAR_NAME_DUCALCOUNTYCONQUEST
	sprite = 34
	truce_days = 3650
	hostile_against_others = yes
	is_permanent = yes
	check_all_titles = yes # if permanent, setting this to true will check against all of someones titles, including vassal held titles
	press_claim = yes
	can_ask_to_join_war = no

	allowed_to_target_tributaries = no
	
	sort_priority = 720
	
	can_use_gui = {
		check_if_crusader_trigger = yes
		ROOT = {
			show_scope_change = no
			conditional_tooltip = {
				trigger = { 
					tier = COUNT
				}
				piety = 100
				scaled_wealth = 1.0
			}
			conditional_tooltip = {
				trigger = { 
					tier = DUKE
				}
				piety = 250
				scaled_wealth = 1.0
			}
		}
	}
	
	on_add = {
		ROOT = {
			if = {
				limit = {
					tier = COUNT
				}
				piety = -100
				scaled_wealth = -1.0
			}
			if = {
				limit = {
					tier = DUKE
				}
				piety = -250
				scaled_wealth = -1.0
			}
		}
		hidden_tooltip = { fire_haruspicy_event_effect = yes }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}
	
	can_use = {
		has_dlc = "Jade Dragon"
		ROOT = {
			NOT = {
				has_game_rule = {
					name = jade_dragon_cbs
					value = off
				}
			}
		}
	}
	
	can_use_title = {
		ROOT = {
			NOR = { # These all have other ways to expand in the early game
				religion_group = muslim
				religion_group = pagan_group
				is_nomadic = yes
			}
			OR = { # Only Counts and Dukes can use this
				tier = COUNT
				tier = DUKE
			}
		}
	
		tier = COUNT
		
		dejure_liege_title = {
			has_holder = no
			any_de_jure_vassal_title = {
				holder = ROOT
			}
		}
		
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
		
		
		NOR = {
			ROOT = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = ROOT
			}
			holder_scope = {
				ROOT = {
					any_liege = {
						character = PREVPREV
					}
				}
			}
		}
	}

	is_valid_title = {
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
	}

	on_success_title = {
		if = { # If the attacker is a duke, and the target is a one-county ruler, vassalize them
			limit = {
				ROOT = {
					tier = DUKE
				}
				holder_scope = {
					tier = COUNT
					is_patrician = no
					NOT = { num_of_count_titles = 2 }
					lower_tier_than = ROOT
				}
			}

			holder_scope = {
				set_defacto_liege = ROOT
			}
		}

		if = {
			limit = {
				holder_scope = {
					OR = {
						ROOT = {
							tier = COUNT
						}
						higher_tier_than = COUNT
						num_of_count_titles = 2
						is_patrician = yes
					}
				}
			}

			usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
			any_de_jure_vassal_title = { # take all baronies under the one we're fighting for
				limit = {
					has_holder = yes
					NOT = {
						de_facto_liege = PREV
					}
					holder_scope = {
						OR = {
							character = FROM
							is_liege_or_above = FROM
						}
					}
				}

				usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}

		hidden_tooltip = {
			check_war_effect = yes
			FROM = {
				landless_pope_effect = yes
			}
		}
	}

	on_fail_title = {
		ROOT = {
			show_scope_change = no
			prestige = -100
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		ROOT = {
			show_scope_change = no
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 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 = 1
		
		modifier = {
			factor = 0
			ROOT = {
				NOT = { scaled_wealth = 1.25 }
			}
		}
		modifier = { #Focus on Coronation first.
			factor = 0.1
			has_dlc = "Holy Fury"
			ROOT = {
				is_feudal = yes
				OR = {
					religion = catholic
					religion = fraticelli
				}
				NOR = {
					trait = crowned_by_priest
					trait = crowned_by_bishop
					trait = crowned_by_pope
					trait = crowned_by_myself
				}
			}
		}
	}
}

hostages_cb = { # New5
	name = CB_NAME_HOSTAGES
	war_name = WAR_NAME_HOSTAGES
	sprite = 25
	truce_days = 3650
	is_permanent = yes
	can_ask_to_join_war = no
	allowed_to_target_tributaries = no
	
	defender_unoccupied_warscore = yes
	
	hostages_block_cb = no
	
	sort_priority = 100000 # When there are hostages you can usually not declare any other war, so we want this first
	
	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		ROOT = {
			independent = yes
		}
		FROM = {
			any_realm_character = {
				is_landed = no
				OR = {
					any_spouse = { character = ROOT }
					is_friend = ROOT
					is_close_relative = ROOT
					dynasty = ROOT
					is_consort = ROOT
				}
				OR = {
					prisoner = yes
					ROOT_FROM = {
						any_realm_character = {
							reverse_has_opinion_modifier = {
								modifier = opinion_forced_consort
								who = PREVPREV
							}
						}
					}
				}
			}
		}
	}
	
	is_valid = {
		always = yes
	}
	
	on_add = {
		FROM = { character_event = { id = JD.2000 days = 5 } } # Chance to execute captives
		hidden_tooltip = { fire_haruspicy_event_effect = yes }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes
			}
		}
	}
	
	on_success = {
		ROOT = {
			show_scope_change = no
			prestige = 250
		}
		FROM = {
			show_scope_change = no
			prestige = -250
		}
		FROM = {
			show_scope_change = no
			save_event_target_as = character_kidnapper
			transfer_scaled_wealth = {
				to = ROOT
				value = 3.0
			}
			any_realm_character = {
				limit = {
					is_landed = no
					OR = {
						any_spouse = { character = ROOT }
						is_friend = ROOT
						is_close_relative = ROOT
						dynasty = ROOT
						is_consort = ROOT
					}
					OR = {
						prisoner = yes
						ROOT_FROM = {
							any_realm_character = {
								reverse_has_opinion_modifier = {
									modifier = opinion_forced_consort
									who = PREVPREV
								}
							}
						}
					}
				}
				hidden_effect = {
					if = { # Concubines are released...
						limit = {
							is_consort = yes
							consort = { # ...if they are taken by someone in the same realm of your target
								same_realm = event_target:character_kidnapper
							}
						}
						consort = {
							remove_consort = PREV
						}
					}
					if = {
						limit = { # Marriages are annuled
							is_married = yes
							any_spouse = { same_realm = event_target:character_kidnapper }
						}
						remove_spouse = yes
					}
				}
				if = { # Release prisoners
					limit = {
						prisoner = yes
					}
					imprison = no
				}
				if = { # Move to the saviours court...
					limit = {
						OR = { # ...unless you are a concubine to a landed character...
							is_consort = no
							AND = {
								is_consort = yes
								spouse = {
									is_landed = no
								}
							}
						}
						OR = { # ...or the wife to a landed character
							is_married = no
							AND = {
								is_married = yes
								spouse = {
									is_landed = no
								}
							}
						}
					}
					move_character = ROOT
				}
				opinion = {
					who = ROOT
					modifier = opinion_my_saviour
					years = 100
				}
			}
			custom_tooltip = {
				text = hostages_random_imprisonment_tt
				hidden_effect = {
					random_realm_character = {
						limit = {
							NOT = {
								character = PREV
							}
							prisoner = no
							ai = yes
							is_close_relative = PREV
						}
						imprison = ROOT
					}
				}
			}
		}
	}

	on_fail = {
		ROOT = {
			show_scope_change = no
			prestige = -250
		}
		FROM = { 
			show_scope_change = no
			prestige = 250
		}
	}

	on_reverse_demand = {
		ROOT = {
			show_scope_change = no
			transfer_scaled_wealth = {
				to = FROM
				value = 3.0
			}
			prestige = -250
		}
		FROM = { 
			show_scope_change = no
			prestige = 250
		}
	}

	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
		
		modifier = {
			factor = 2
			FROM = {
				trait = proud
			}
		}
		
		modifier = {
			factor = 3
			FROM = {
				tier = KING
			}
		}

		modifier = {
			factor = 0.85
			ROOT = {
				relative_power = { who = FROM power = 1.2 }
			}
		}

		modifier = {
			factor = 0.85
			ROOT = {
				relative_power = { who = FROM power = 1.5 }
			}
		}

		modifier = {
			factor = 0.8
			ROOT = {
				relative_power = { who = FROM power = 2.0 }
			}
		}

		modifier = {
			factor = 0.8
			ROOT = {
				relative_power = { who = FROM power = 2.5 }
			}
		}

		modifier = {
			factor = 0.8
			ROOT = {
				relative_power = { who = FROM power = 3 }
			}
		}

		modifier = {
			factor = 0.75
			ROOT = {
				relative_power = { who = FROM power = 4 }
			}
		}

		modifier = {
			factor = 0.75
			ROOT = {
				relative_power = { who = FROM power = 5 }
			}
		}

		modifier = {
			factor = 0.75
			ROOT = {
				relative_power = { who = FROM power = 6 }
			}
		}

		modifier = {
			factor = 0.5
			ROOT = {
				relative_power = { who = FROM power = 8 }
			}
		}

		modifier = {
			factor = 0.25
			ROOT = {
				relative_power = { who = FROM power = 10 }
			}
		}

		modifier = {
			factor = 2
			ROOT = {
				distance_from_realm = { who = FROM value = 20 }
			}
		}

		modifier = {
			factor = 2
			ROOT = {
				distance_from_realm = { who = FROM value = 40 }
			}
		}
		
		modifier = {
			factor = 3
			ROOT = {
				distance_from_realm = { who = FROM value = 80 }
			}
		}
		
		modifier = {
			factor = 4
			ROOT = {
				distance_from_realm = { who = FROM value = 120 }
			}
		}
	}
	
	ai_will_do = {
		factor = 0 # Player Only
	}
}

liberate_religion = { # New6
	name = CB_NAME_LIBERATE_RELIGION
	war_name = WAR_NAME_LIBERATE_RELIGION
	sprite = 35
	truce_days = 3650
	is_permanent = yes
	is_holy_war = yes
	check_de_jure_tier = KING
	apply_short_occ_mod = no # Do not apply the 'recently_conquered' modifier to Holdings

	allowed_to_target_tributaries = no
	
	sort_priority = 4000
	
	on_add = {
		hidden_tooltip = {
			if = { 
				limit = {
					NOT = {
						ROOT = {
							any_owned_bloodline = {  
								has_bloodline_flag = bloodline_religious_liberation
								bloodline_is_active_for = PREV
							}
						}
					}
				}
				ROOT = {
					set_character_flag = used_liberate_opportunity
				}
			}
		}
		hidden_tooltip = { fire_haruspicy_event_effect = yes }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes
			}
		}
	}

	can_use = {
		OR = {
			has_dlc = "Holy Fury" 
			has_dlc = "Jade Dragon"
		}
		ROOT = {
			OR = {
				any_owned_bloodline = {  
					has_bloodline_flag = bloodline_religious_liberation
					bloodline_is_active_for = PREV
				}
				NOT = {
					has_game_rule = {
						name = jade_dragon_cbs
						value = off
					}
				}
			}
			independent = yes
			OR = {
				any_owned_bloodline = {  
					has_bloodline_flag = bloodline_religious_liberation
					bloodline_is_active_for = PREV
				}
				has_character_flag = had_liberate_opportunity
			}
			NOT = {
				has_character_flag = used_liberate_opportunity
			}
			is_nomadic = no
			mercenary = no
			NOT = { pacifist = yes }
			NOT = { same_realm = FROM }
			NOT = { religion = FROM }
			top_liege = {
				religion = ROOT
			}
		}
	}
	
	can_use_title = {
		any_de_jure_vassal_title = {
			tier = COUNT
			holder_scope = {
				OR = {
					character = FROM
					is_liege_or_above = FROM
				}
			}
			location = {
				religion = ROOT
			}
		}
		# The attacker needs at least one county in the target kingdom, or a border, or be only two sea zones away from one of your counties
		any_de_jure_vassal_title = {
			OR = {
				holder_scope = {
					OR = {
						character = ROOT
						is_liege_or_above = ROOT
					}
				}
				location = {
					any_neighbor_province = {
						OR = {
							AND = {
								has_owner = yes
								owner = {
									OR = {
										character = ROOT
										is_liege_or_above = ROOT
									}
								}
							}
							AND = {
								is_land = no
								OR = {
									ROOT = { religion_group = muslim }
									FROM = { religion_group = muslim }
									AND = {
										ROOT = { religion_group = pagan_group }
										has_global_flag = pagan_ghws_unlocked
									}
									AND = {
										ROOT = { religion_group = christian }
										has_global_flag = christian_crusades_unlocked
									}
									AND = {
										ROOT = { religion_group = muslim }
										has_global_flag = muslim_jihads_unlocked
									}
								}
								any_neighbor_province = {
									OR = {
										AND = {
											has_owner = yes
											owner = {
												OR = {
													character = ROOT
													is_liege_or_above = ROOT
												}
											}
										}
										AND = {
											is_land = no
											any_neighbor_province = {
												owner = {
													OR = {
														character = ROOT
														is_liege_or_above = ROOT
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}

	is_valid = {
		ROOT = {
			NOT = { pacifist = yes }
			NOT = { same_realm = FROM }
			NOT = { religion = FROM }
			top_liege = {
				NOT = { religion = FROM }
			}
		}
	}
	
	is_valid_title = {
		FROM = {
			any_realm_province = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}
	
	on_success = {
		hidden_tooltip = { 
			any_attacker = {
				limit = { NOT = { character = ROOT } }
				
				participation_scaled_piety = 100
				participation_scaled_prestige = 50
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
		}
		
		ROOT = {
			show_scope_change = no
			participation_scaled_piety = 500
			participation_scaled_prestige = 250
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
			religion_authority = {
				modifier = won_holy_war
				years = 20
			}
		}
		FROM = { show_scope_change = no piety = -100 }
		FROM = { show_scope_change = no prestige = -50 }
	}

	on_success_title = {
		custom_tooltip = {
			text = religious_liberation_cb_succ_tip
			hidden_tooltip = {
				FROM = {
					if = {
						limit = {
							any_realm_title = {
								tier = COUNT
								kingdom = { title = PREVPREVPREV }
								location = {
									religion = ROOT
								}
							}
						}
						random_realm_title = {
							limit = {
								tier = COUNT
								kingdom = { title = PREVPREVPREV }
								location = {
									religion = ROOT
								}
							}
							location = {
								create_character = {
									religion = ROOT
									culture = THIS
									dynasty = random
									random_traits = yes
									female = no
									age = 20
									health = 7
									fertility = 0.7
									attributes = {
										martial = 10
										diplomacy = 10
										stewardship = 10
									}
									flag = is_religious_liberation_ruler
								}
								new_character = {
									remove_trait = cynical
									add_trait = zealous
									wealth = 500
									prestige = 500
									piety = 500
									save_event_target_as = new_ruler
									usurp_title = { target = PREVPREV type = invasion }
								}
							}
						}
					}
					if = {
						limit = {
							NOR = {
								event_target:new_ruler = { is_alive = yes }
								any_realm_title = {
									tier = COUNT
									kingdom = { title = PREVPREVPREV }
									location = {
										religion = ROOT
									}
								}
							}
						}
						random_realm_title = {
							limit = {
								tier = COUNT
								kingdom = { title = PREVPREVPREV }
							}
							location = {
								create_character = {
									religion = ROOT
									culture = THIS
									dynasty = random
									random_traits = yes
									female = no
									age = 20
									health = 7
									fertility = 0.7
									attributes = {
										martial = 10
										diplomacy = 10
										stewardship = 10
									}
									flag = is_religious_liberation_ruler
								}
								new_character = {
									remove_trait = cynical
									add_trait = zealous
									wealth = 500
									prestige = 500
									piety = 500
									save_event_target_as = new_ruler
									usurp_title = { target = PREVPREV type = invasion }
								}
							}
						}
					}
				}
				if = {
					limit = {
						has_holder = no
					}
					usurp_title = { target = event_target:new_ruler type = invasion }
				}
				else = {
					event_target:new_ruler = {
						create_title = {
							tier = KING
							landless = no
							temporary = no
							custom_created = yes
							culture = event_target:new_ruler
							holder = event_target:new_ruler
							name = "LIBERATED_KINGDOM"
							base_title = PREV
						}
					}
				}
				
				event_target:new_ruler = {
					vassalize_or_take_under_title = {
						title = PREV
						enemy = FROM
						same_religion = yes # Only vassalize rulers of my religion
						is_religious = yes
						type = holy_war
						as_if_allied_to = ROOT
					}
					set_defacto_liege = THIS
				}
				
				if = {
					limit = {
						has_holder = yes
						holder_scope = {
							OR = {
								character = FROM
								is_liege_or_above = FROM
							}
						}
					}
					destroy_landed_title = THIS
					usurp_title_only = { target = event_target:new_ruler type = invasion }
					make_primary_title = yes
					event_target:new_ruler = {
						any_demesne_title = {
							limit = {
								tier = KING
								is_primary_holder_title = no
							}
							destroy_landed_title = THIS
						}
					}
				}
				
				ROOT = {
					hidden_tooltip = {
						if = {
							limit = {
								has_ambition = obj_strengthen_religion
							}
							change_variable = { which = strengthen_religion value = 1 }
						}
					}
					make_tributary = { who = event_target:new_ruler tributary_type = permanent }
				}
			}
		}

		hidden_tooltip = {
			check_if_reconquista_finished_effect = yes
			FROM = {
				landless_pope_effect = yes
			}
		}
	}
	
	is_valid_title = {
		FROM = {
			any_realm_province = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}

	on_fail = {
		ROOT = { 
			show_scope_change = no
			piety = -125 
			prestige = -250
		}
	}
	
	on_invalidation = {
		ROOT = {
			clr_character_flag = used_liberate_opportunity
		}
	}

	on_reverse_demand = {
		ROOT = {
			show_scope_change = no
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -250
			prestige = -500
		}
		FROM = {
			show_scope_change = no
			participation_scaled_piety = 200
			participation_scaled_prestige = 100
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_piety = 200
				participation_scaled_prestige = 100
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
				hidden_tooltip = {
					if = {
						limit = {
							has_ambition = obj_strengthen_religion
						}
						change_variable = { which = strengthen_religion value = 1 }
					}
				}
			}
		}
		any_attacker = {
			limit = {
				character = ROOT
				uses_decadence = yes
			}
			participation_scaled_decadence = 20
		}
		any_attacker = {
			limit = {
				NOT = { character = ROOT }
				uses_decadence = yes
			}
			hidden_tooltip = { 
				participation_scaled_decadence = 20
			}
		}
		ROOT = { 
			religion_authority = {
				modifier = lost_holy_war
				years = 20
			}
		}
		FROM = { 
			religion_authority = {
				modifier = won_holy_war
				years = 20
			}
			hidden_tooltip = {
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}			
		}
	}

	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 = 1
		modifier = { #Focus on Coronation first.
			factor = 0.1
			has_dlc = "Holy Fury"
			ROOT = {
				is_feudal = yes
				OR = {
					religion = catholic
					religion = fraticelli
				}
				NOR = {
					trait = crowned_by_priest
					trait = crowned_by_bishop
					trait = crowned_by_pope
					trait = crowned_by_myself
				}
			}
		}
	}
}

unjust_conquest = { # New7
	name = CB_NAME_UNJUST_COUNTY_CONQUEST
	war_name = WAR_NAME_UNJUST_COUNTY_CONQUEST
	sprite = 27
	truce_days = 1825
	hostile_against_others = yes
	is_permanent = yes
	check_all_titles = yes # if permanent, setting this to true will check against all of someones titles, including vassal held titles
	can_ask_to_join_war = no
	allowed_to_target_tributaries = no
	infamy_modifier = 2
	
	sort_priority = 690

	can_use_gui = {
		check_if_crusader_trigger = yes
		ROOT = {
			show_scope_change = no
			conditional_tooltip = { # Pagans have a much easier time using this CB in general
				trigger = { 
					religion_group = pagan_group
				}
				prestige = 100
			}
			conditional_tooltip = {
				trigger = { 
					tier = COUNT
					religion = FROM
					NOT = { religion_group = pagan_group }
				}
				piety = 100
				scaled_wealth = { value = 3.0 min = 50 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = COUNT
					religion_group = FROM
					NOT = { religion = FROM }
					NOT = { religion_group = pagan_group }
				}
				piety = 50
				scaled_wealth = { value = 2.0 min = 50 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = COUNT
					NOT = { religion_group = FROM }
					NOT = { religion_group = pagan_group }
				}
				piety = 25
				scaled_wealth = { value = 1.0 min = 50 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = DUKE
					religion = FROM
					NOT = { religion_group = pagan_group }
				}
				piety = 200
				scaled_wealth = { value = 3.0 min = 100 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = DUKE
					religion_group = FROM
					NOT = { religion = FROM }
					NOT = { religion_group = pagan_group }
				}
				piety = 100
				scaled_wealth = { value = 2.0 min = 100 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = DUKE
					NOT = { religion_group = FROM }
					NOT = { religion_group = pagan_group }
				}
				piety = 50
				scaled_wealth = { value = 1.0 min = 100 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = KING
					religion = FROM
					NOT = { religion_group = pagan_group }
				}
				piety = 300
				scaled_wealth = { value = 3.0 min = 200 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = KING
					religion_group = FROM
					NOT = { religion = FROM }
					NOT = { religion_group = pagan_group }
				}
				piety = 200
				scaled_wealth = { value = 2.0 min = 200 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = KING
					NOT = { religion_group = FROM }
					NOT = { religion_group = pagan_group }
				}
				piety = 100
				scaled_wealth = { value = 1.0 min = 200 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = EMPEROR
					religion = FROM
					NOT = { religion_group = pagan_group }
				}
				piety = 600
				scaled_wealth = { value = 3.0 min = 400 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = EMPEROR
					religion_group = FROM
					NOT = { religion = FROM }
					NOT = { religion_group = pagan_group }
				}
				piety = 400
				scaled_wealth = { value = 2.0 min = 400 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = EMPEROR
					NOT = { religion_group = FROM }
					NOT = { religion_group = pagan_group }
				}
				piety = 200
				scaled_wealth = { value = 1.0 min = 400 }
			}
		}
	}
	
	on_add = {
		ROOT = {
			show_scope_change = no
			if = { # Pagans have a much easier time using this CB in general
				limit = {
					religion_group = pagan_group
				}
				prestige = -100
			}
			if = {
				limit = {
					tier = COUNT
					religion = FROM
					NOT = { religion_group = pagan_group }
				}
				piety = -100
				scaled_wealth = { value = -3.0 min = -100 }
			}
			if = {
				limit = {
					tier = COUNT
					religion_group = FROM
					NOT = { religion = FROM }
					NOT = { religion_group = pagan_group }
				}
				piety = -50
				scaled_wealth = { value = -2.0 min = -100 }
			}
			if = {
				limit = {
					tier = COUNT
					NOT = { religion_group = FROM }
					NOT = { religion_group = pagan_group }
				}
				piety = -25
				scaled_wealth = { value = -1.0 min = -100 }
			}
			
			if = {
				limit = {
					tier = DUKE
					religion = FROM
					NOT = { religion_group = pagan_group }
				}
				piety = -200
				scaled_wealth = { value = -3.0 min = -100 }
			}
			if = {
				limit = {
					tier = DUKE
					religion_group = FROM
					NOT = { religion = FROM }
					NOT = { religion_group = pagan_group }
				}
				piety = -100
				scaled_wealth = { value = -2.0 min = -100 }
			}
			if = {
				limit = {
					tier = DUKE
					NOT = { religion_group = FROM }
					NOT = { religion_group = pagan_group }
				}
				piety = -50
				scaled_wealth = { value = -1.0 min = -100 }
			}
			
			if = {
				limit = {
					tier = KING
					religion = FROM
					NOT = { religion_group = pagan_group }
				}
				piety = -300
				scaled_wealth = { value = -3.0 min = -100 }
			}
			if = {
				limit = {
					tier = KING
					religion_group = FROM
					NOT = { religion = FROM }
					NOT = { religion_group = pagan_group }
				}
				piety = -200
				scaled_wealth = { value = -2.0 min = -100 }
			}
			if = {
				limit = {
					tier = KING
					NOT = { religion_group = FROM }
					NOT = { religion_group = pagan_group }
				}
				piety = -100
				scaled_wealth = { value = -1.0 min = -100 }
			}
			
			if = {
				limit = {
					tier = EMPEROR
					religion = FROM
					NOT = { religion_group = pagan_group }
				}
				piety = -600
				scaled_wealth = { value = -3.0 min = -100 }
			}
			if = {
				limit = {
					tier = EMPEROR
					religion_group = FROM
					NOT = { religion = FROM }
					NOT = { religion_group = pagan_group }
				}
				piety = -400
				scaled_wealth = { value = -2.0 min = -100 }
			}
			if = {
				limit = {
					tier = EMPEROR
					NOT = { religion_group = FROM }
					NOT = { religion_group = pagan_group }
				}
				piety = -200
				scaled_wealth = { value = -1.0 min = -100 }
			}
						
			if = { # Opinion hit for same religion
				limit = {
					religion = FROM
				}
				if = {
					limit = {
						controls_religion = no
						NOT = {
							religion_group = pagan_group
						}
					}
					rightful_religious_head_scope = {
						show_scope_change = no
						opinion = {
							who = ROOT
							modifier = opinion_declared_unjust_conquest
							years = 25
						}
					}
				}
				custom_tooltip = {
					text = custom_opinion_hit_tooltip
					hidden_effect = {
						generate_tooltip = no
						any_realm_character = {
							limit = {
								controls_religion = no
								religion_group = ROOT
								is_landed = yes
								NOT = {
									character = ROOT
								}
							}
							opinion = {
								who = ROOT
								modifier = opinion_declared_unjust_conquest
								years = 10
							}
						}
						FROM = {
							any_realm_character = {
								limit = {
									controls_religion = no
									religion_group = ROOT
									is_landed = yes
								}
								opinion = {
									who = ROOT
									modifier = opinion_declared_unjust_conquest
									years = 10
								}
							}
						}
					}
				}
			}
			if = { # Opinion hit for same group
				limit = {
					religion_group = FROM
					NOT = { religion = FROM }
				}
				custom_tooltip = {
					text = custom_opinion_hit_tooltip
					hidden_effect = {
						generate_tooltip = no
						any_realm_character = {
							limit = {
								religion_group = ROOT
								is_landed = yes
								NOT = {
									character = ROOT
								}
							}
							opinion = {
								who = ROOT
								modifier = opinion_declared_unjust_conquest
								years = 10
							}
						}
						FROM = {
							any_realm_character = {
								limit = {
									religion_group = ROOT
									is_landed = yes
								}
								opinion = {
									who = ROOT
									modifier = opinion_declared_unjust_conquest
									years = 10
								}
							}
						}
					}
				}
			}
			if = {
				limit = {
					NOT = { religion_group = FROM }
				}
				custom_tooltip = {
					text = custom_opinion_hit_tooltip_enemy
					hidden_effect = {
						generate_tooltip = no
						any_realm_character = {
							limit = {
								religion = FROM
								is_landed = yes
							}
							opinion = {
								who = ROOT
								modifier = opinion_declared_unjust_conquest
								years = 10
							}
						}
						FROM = {
							any_realm_character = {
								limit = {
									religion = ROOT
									is_landed = yes
								}
								opinion = {
									who = ROOT
									modifier = opinion_declared_unjust_conquest
									years = 10
								}
							}
						}
					}
				}
			}
		}
		hidden_tooltip = { fire_haruspicy_event_effect = yes }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes
			}
		}
	}
	
	can_use = {
		has_dlc = "Jade Dragon"
		ROOT = {
			NOT = {
				has_game_rule = {
					name = unjustified_wars
					value = off
				}
			}
			NOT = { is_liege_or_above = FROM }
			mercenary = no
			NOT = { pacifist = yes }
			is_nomadic = no # Use the 'pagan_county_conquest' CB instead
			NOT = {
				religion_group = muslim # Use 'muslim_county_conquest' instead
			}
			NAND = { # Use 'pagan_county_conquest' instead
				religion_group = pagan_group
				FROM = {
					NOT = {
						religion = ROOT
					}
				}
			}
		}
		OR = {
			is_shattered_world = no
			AND = {
				is_shattered_world = yes
				has_alternate_start_setting = {
					setting = shattered_cbs
					option = on_limited
				}
				total_years_played = 25
			}
			AND = {
				is_shattered_world = yes
				has_alternate_start_setting = {
					setting = shattered_cbs
					option = on_long
				}
				total_years_played = 100
			}
			AND = {
				is_shattered_world = yes
				has_alternate_start_setting = {
					setting = shattered_cbs
					option = off
				}
			}
		}
	}

	can_use_title = {
		tier = count
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
				NOT = { same_realm = ROOT }
			}
		}

		NOR = {
			ROOT = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = ROOT
			}
		}
		
		location = {
			any_neighbor_province = {
				owner = {
					OR = {
						character = ROOT
						is_liege_or_above = ROOT
					}
				}
			}
		}
	}

	is_valid_title = {
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
	}

	on_success_title = {
		if = {
			limit = {
				holder_scope = {
					tier = count
					NOT = { num_of_count_titles = 2 }
					lower_tier_than = ROOT
				}
			}

			holder_scope = {
				set_defacto_liege = ROOT
			}
		}

		if = {
			limit = {
				holder_scope = {
					OR = {
						higher_tier_than = count
						num_of_count_titles = 2
						NOT = { lower_tier_than = ROOT }
					}
				}
			}

			usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
			any_de_jure_vassal_title = { # take all baronies under the one we're fighting for
				limit = {
					has_holder = yes
					NOT = {
						de_facto_liege = PREV
					}
					holder_scope = {
						is_liege_or_above = FROM
					}
				}

				usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
			}
		}
		
		add_pressed_claim = FROM
		
		ROOT = {
			show_scope_change = no
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}

		hidden_tooltip = {
			check_war_effect = yes
			FROM = {
				landless_pope_effect = yes
			}
		}
	}

	on_fail_title = {
		ROOT = {
			show_scope_change = no
			prestige = -100
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		ROOT = {
			show_scope_change = no
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 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 = 1
		
		modifier = {
			factor = 0.7 # Prefer using CB's without on_add costs
		}
		modifier = { #Focus on Coronation first.
			factor = 0.1
			has_dlc = "Holy Fury"
			ROOT = {
				is_feudal = yes
				OR = {
					religion = catholic
					religion = fraticelli
				}
				NOR = {
					trait = crowned_by_priest
					trait = crowned_by_bishop
					trait = crowned_by_pope
					trait = crowned_by_myself
				}
			}
		}
		modifier = {
			factor = 0
			ROOT = {
				independent = no
				same_liege = FROM
			}
		}
		modifier = {
			factor = 0
			ROOT = {
				higher_tier_than = DUKE
				num_of_count_titles_in_realm = 10
			}
		}
		modifier = {
			factor = 0
			ROOT = {
				OR = {
					trait = just
					trait = content
				}
			}
		}
		modifier = {
			factor = 0
			ROOT = {
				NOR = {
					trait = arbitrary
					trait = ambitious
				}
			}
		}
		modifier = {
			factor = 0
			ROOT = {
				has_character_flag = is_temujin
				has_landed_title = e_mongol_empire
				demesne_size = 1
			}
			FROM = {
				has_landed_title = e_china_west_governor
			}
		}
	}
}

tributary_permanent_cb = { # New8
	name = CB_NAME_PERMANENT_TRIBUTARY
	war_name = WAR_NAME_PERMANENT_TRIBUTARY
	sprite = 37
	truce_days = 365
	is_permanent = yes
	hostile_against_others = yes
	can_ask_to_join_war = no
	display_on_map = no
	allowed_to_target_tributaries = no
	
	defender_unoccupied_warscore = yes
	
	sort_priority = 810
	
	can_use_gui = {
		check_if_crusader_trigger = yes
		ROOT = {
			relative_realm_size = {
				who = FROM 
				size > 1
			}
			conditional_tooltip = {
				trigger = {
					capital_scope = {
						NOT = {
							region = world_europe
						}
					}
				}
				prestige = 200
				TECH_MAJESTY = 2
			}
			conditional_tooltip = {
				trigger = {
					capital_scope = {
						region = world_europe
					}
				}
				prestige = 200
				TECH_MAJESTY = 4
			}
		}
	}
	
	can_use = {
		has_dlc = "Jade Dragon"

		ROOT = {
			NOT = {
				has_game_rule = {
					name = jade_dragon_cbs
					value = off
				}
			}
			is_nomadic = no
			independent = yes
			mercenary = no
			OR = {
				is_tributary = no
				is_tributary = {
					type = offmap
				}
			}
			NOT = { is_vassal_or_below = FROM }
			NOT = { is_liege_or_above = FROM }
			NOT = {
				rightful_religious_head_scope = {
					character = FROM
				}
			}
			NAND = {
				holy_order = yes
				religion = FROM
			}
		}
		FROM = {
			is_nomadic = no
			higher_tier_than = BARON
			NOT = {
				is_offmap_governor = yes
			}
			NAND = {
				holy_order = yes
				religion = ROOT
			}
			independent = yes
			NOT = {
				suzerain = {
					OR = {
						character = ROOT
						any_liege = {
							character = ROOT
						}
					}
				}
			}
		}
		
		# The attacker needs a border, or be at most two sea zones away from one of the target's counties
		FROM = {
			any_realm_province = {
				any_neighbor_province = {
					OR = {
						AND = {
							has_owner = yes
							owner = {
								OR = {
									character = ROOT
									is_liege_or_above = ROOT
									top_liege = {
										is_tributary = {
											type = permanent
											suzerain = ROOT
										}
									}
								}
							}
						}
						AND = {
							is_land = no
							any_neighbor_province = {
								OR = {
									AND = {
										has_owner = yes
										owner = {
											OR = {
												character = ROOT
												is_liege_or_above = ROOT
											}
										}
									}
									AND = {
										is_land = no
										any_neighbor_province = {
											owner = {
												OR = {
													character = ROOT
													is_liege_or_above = ROOT
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		
		# The defender is not in any tributary war
		FROM = {
			NOR = {
				any_war = {
					using_cb = tributary_cb
				}
				any_war = {
					using_cb = tributary_permanent_cb
				}
				any_war = {
					using_cb = tributary_nomad_cb
				}
			}
		}
	}

	is_valid = {
		NOT = {
			ROOT = {
				rightful_religious_head_scope = {
					character = FROM 
				}
			}
		}
	}

	on_add = {
		# Chance to surrender early
		ROOT = { save_event_target_as = tribute_war_suzerain }
		FROM = {
			show_scope_change = no
			save_event_target_as = tribute_war_target
			set_character_flag = surrender_tributary_permanent_cb
			character_event = { id = JD.2500 days = 5 tooltip = surrender_tributary_tt }
		}
		hidden_tooltip = { fire_haruspicy_event_effect = yes }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	on_success = {
		FROM = {
			show_scope_change = no
			save_event_target_as = target_loser
			prestige = -100
			custom_tooltip = {
				text = "REMOVE_PREVIOUS_SUZERAIN"
				hidden_tooltip = {
					any_suzerain = {
						event_target:target_loser = {
							remove_tributary = PREV
						}
					}
				}
			}
			opinion = {
				modifier = opinion_forced_tributary 
				who = ROOT
				years = 15
			}
		}

		ROOT = {
			show_scope_change = no
			prestige = 200
			make_tributary = { who = FROM tributary_type = permanent }
		}
		
		if  = {
			limit = {
				ROOT = {
					uses_decadence = yes
				}
			}
			ROOT = {
				decadence = -10
			}
		}
		if  = {
			limit = {
				ROOT = {
					is_nomadic = yes
				}
			}
			ROOT = {
				clan_title = {
					ROOT = {
						any_vassal = {
							limit = { is_nomadic = yes }
							clan_title = {
								clan_opinion = {
									who = PREVPREVPREV
									modifier = won_tributary_war
									years = 5
								}
							}
						}
					}
				}
			}
		}
		
		if = {
			limit = {
				ROOT = {
					is_tribal = yes
				}
			}
			ROOT = {
				any_vassal = {
					limit = { is_tribal = yes }
					opinion = {
						who = PREV
						modifier = won_tributary_war
						years = 5
					}
				}
			}
		}
	}

	on_fail = {
		ROOT = {
			show_scope_change = no
			prestige = -100
		}
		FROM = {
			show_scope_change = no
			prestige = 100
		}
	}

	on_reverse_demand = {
		ROOT = {
			show_scope_change = no
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}

		FROM = {
			show_scope_change = no
			prestige = 200
		}
	}
	
	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 = 1
		
		modifier = {
			factor = 0.5  # Low prio CB
		}
		modifier = { #Focus on Coronation first.
			factor = 0.1
			has_dlc = "Holy Fury"
			ROOT = {
				is_feudal = yes
				OR = {
					religion = catholic
					religion = fraticelli
				}
				NOR = {
					trait = crowned_by_priest
					trait = crowned_by_bishop
					trait = crowned_by_pope
					trait = crowned_by_myself
				}
			}
		}
	}
}

tributary_nomad_cb = { # New9
	name = CB_NAME_NOMAD_TRIBUTARY
	war_name = WAR_NAME_NOMAD_TRIBUTARY
	sprite = 37
	truce_days = 365
	is_permanent = yes
	hostile_against_others = yes
	can_ask_to_join_war = no
	display_on_map = no
	allowed_to_target_tributaries = no
	
	defender_unoccupied_warscore = yes
	
	sort_priority = 810
	
	can_use_gui = {
		check_if_crusader_trigger = yes
		ROOT = {
			prestige = 100
		}
	}
	
	can_use = {
		ROOT = {
			is_nomadic = yes
			mercenary = no
			OR = {
				is_tributary = no
				is_tributary = {
					type = offmap
				}
			}
			NOT = { is_vassal_or_below = FROM }
			NOT = { is_liege_or_above = FROM }
			NOT = {
				rightful_religious_head_scope = {
					character = FROM
				}
			}
			NAND = {
				holy_order = yes
				religion = FROM
			}
		}
		FROM = {
			higher_tier_than = BARON
			NOT = {
				is_offmap_governor = yes
			}
			NAND = {
				holy_order = yes
				religion = ROOT
			}
			independent = yes
			NOT = {
				suzerain = {
					OR = {
						character = ROOT
						any_liege = {
							character = ROOT
						}
					}
				}
			}
		}
		
		# The attacker needs a border, or be at most two sea zones away from one of the target's counties
		FROM = {
			any_realm_province = {
				any_neighbor_province = {
					OR = {
						AND = {
							has_owner = yes
							owner = {
								OR = {
									character = ROOT
									is_liege_or_above = ROOT
									top_liege = {
										is_tributary = {
											type = nomadic
											suzerain = ROOT
										}
									}
								}
							}
						}
						AND = {
							is_land = no
							any_neighbor_province = {
								OR = {
									AND = {
										has_owner = yes
										owner = {
											OR = {
												character = ROOT
												is_liege_or_above = ROOT
											}
										}
									}
									AND = {
										is_land = no
										any_neighbor_province = {
											owner = {
												OR = {
													character = ROOT
													is_liege_or_above = ROOT
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		
		# The defender is not in any tributary war
		FROM = {
			NOR = {
				any_war = {
					using_cb = tributary_cb
				}
				any_war = {
					using_cb = tributary_permanent_cb
				}
				any_war = {
					using_cb = tributary_nomad_cb
				}
			}
		}
	}

	is_valid = {
		NOT = {
			ROOT = {
				rightful_religious_head_scope = {
					character = FROM 
				}
			}
		}
	}

	on_add = {
		# Chance to surrender early
		ROOT = { save_event_target_as = tribute_war_suzerain }
		FROM = {
			show_scope_change = no
			save_event_target_as = tribute_war_target
			set_character_flag = surrender_tributary_nomad_cb
			character_event = { id = JD.2500 days = 5 tooltip = surrender_tributary_tt }
		}
		hidden_tooltip = { fire_haruspicy_event_effect = yes }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = { 
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	on_success = {
		FROM = {
			show_scope_change = no
			transfer_scaled_wealth = {
				to = ROOT
				value = 2.0
			}
		}
	
		FROM = {
			show_scope_change = no
			save_event_target_as = target_loser
			prestige = -100
			custom_tooltip = {
				text = "REMOVE_PREVIOUS_SUZERAIN"
				hidden_tooltip = {
					any_suzerain = {
						event_target:target_loser = {
							remove_tributary = PREV
						}
					}
				}
			}
		}

		ROOT = {
			show_scope_change = no
			prestige = 200
			make_tributary = { who = FROM tributary_type = nomadic }
		}
		
		if  = {
			limit = {
				ROOT = {
					uses_decadence = yes
				}
			}
			ROOT = {
				decadence = -10
			}
		}
		if  = {
			limit = {
				ROOT = {
					is_nomadic = yes
				}
			}
			ROOT = {
				clan_title = {
					ROOT = {
						any_vassal = {
							limit = { is_nomadic = yes }
							clan_title = {
								clan_opinion = {
									who = PREVPREVPREV
									modifier = won_tributary_war
									years = 20
								}
							}
						}
					}
				}
			}
		}
		
		if = {
			limit = {
				ROOT = {
					is_tribal = yes
				}
			}
			ROOT = {
				any_vassal = {
					limit = { is_tribal = yes }
					opinion = {
						who = PREV
						modifier = won_tributary_war
						years = 20
					}
				}
			}
		}
	}

	on_fail = {
		ROOT = {
			show_scope_change = no
			prestige = -100
		}
		FROM = {
			show_scope_change = no
			prestige = 100
		}
	}

	on_reverse_demand = {
		ROOT = {
			show_scope_change = no
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}

		FROM = {
			show_scope_change = no
			prestige = 200
		}
	}
	
	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
		
		modifier = {
			factor = 2
			FROM = {
				trait = proud
			}
		}
		
		modifier = {
			factor = 2
			FROM = {
				trait = brave
			}
		}
		
		modifier = {
			factor = 2
			FROM = {
				trait = greedy
			}
		}
		
		modifier = {
			factor = 3
			FROM = {
				tier = KING
			}
		}

		modifier = {
			factor = 0.85
			ROOT = {
				relative_power = { who = FROM power = 1.2 }
			}
		}

		modifier = {
			factor = 0.85
			ROOT = {
				relative_power = { who = FROM power = 1.5 }
			}
		}

		modifier = {
			factor = 0.8
			ROOT = {
				relative_power = { who = FROM power = 2.0 }
			}
		}

		modifier = {
			factor = 0.8
			ROOT = {
				relative_power = { who = FROM power = 2.5 }
			}
		}

		modifier = {
			factor = 0.8
			ROOT = {
				relative_power = { who = FROM power = 3 }
			}
		}

		modifier = {
			factor = 0.75
			ROOT = {
				relative_power = { who = FROM power = 4 }
			}
		}

		modifier = {
			factor = 0.75
			ROOT = {
				relative_power = { who = FROM power = 5 }
			}
		}

		modifier = {
			factor = 0.75
			ROOT = {
				relative_power = { who = FROM power = 6 }
			}
		}

		modifier = {
			factor = 0.5
			ROOT = {
				relative_power = { who = FROM power = 8 }
			}
		}

		modifier = {
			factor = 0.25
			ROOT = {
				relative_power = { who = FROM power = 10 }
			}
		}

		modifier = {
			factor = 2
			ROOT = {
				distance_from_realm = { who = FROM value = 20 }
			}
		}

		modifier = {
			factor = 2
			ROOT = {
				distance_from_realm = { who = FROM value = 40 }
			}
		}
		
		modifier = {
			factor = 3
			ROOT = {
				distance_from_realm = { who = FROM value = 80 }
			}
		}
		
		modifier = {
			factor = 4
			ROOT = {
				distance_from_realm = { who = FROM value = 120 }
			}
		}
	}

	ai_will_do = {
		factor = 1
		
		modifier = { # In general, Nomads only want to make smaller nomads their tributaries
			factor = 0.1
			AND = {
				ROOT = {
					NOT = {
						relative_realm_size = {
							who = FROM 
							size > 1.1
						}
					}
				}
				FROM = {
					is_nomadic = yes
				}
			}
		}
		modifier = { # The Mongol Empire just wants to take land
			factor = 0
			ROOT = {
				has_landed_title = e_mongol_empire
			}
		}
	}
}

### CHINA CBs 2.8
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 = {
		ROOT = {
			independent = yes
			NOT = { same_realm = FROM }
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}
		
		FROM = {
			in_revolt = no
			is_nomadic = no # Nomads cannot be subjugated
		}
	}

	on_success = {
		FROM = {
			piety = -100
			prestige = -100
			
			if = { # Remove Raid China if active
				limit = {
					has_character_modifier = china_raid_active
				}
				remove_character_modifier = china_raid_active
				hidden_effect = {
					add_character_modifier = {
						name = china_raid_grace_cd
						hidden = yes
						years = 5
					}
				}
			}
			
			custom_tooltip = {
				text = offmap_submission_tip
			}
				
			hidden_tooltip = {
				ROOT = {
					if = {
						limit = {
							num_of_count_titles_in_realm = 0
							ROOT_FROM = {
								num_of_count_titles_in_realm < 2
							}
						}
						ROOT_FROM = {
							any_realm_province = {
								county = {
									usurp_title_plus_barony_if_unlanded = { target = ROOT 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
							ROOT_FROM = {
								num_of_count_titles_in_realm > 1
							}
						}
						ROOT_FROM = {
							if = { # Take counties that border each other
								limit = {
									any_realm_title = {
										tier = COUNT
										ROOT_FROM = {
											NOT = {
												capital_scope = {
													title = PREVPREV
												}
											}
										}
										location = {
											any_neighbor_province = {
												holder_scope = {
													character = ROOT
												}
											}
										}
									}
								}
								random_realm_province = {
									limit = {
										ROOT_FROM = {
											NOT = {
												capital_scope = {
													title = PREVPREV
												}
											}
										}
										any_neighbor_province = {
											holder_scope = {
												character = ROOT
											}
										}
									}
									county = {
										usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
									}
								}
							}
							else = {
								random_realm_province = {
									limit = {
										ROOT_FROM = {
											NOT = {
												capital_scope = {
													title = PREVPREV
												}
											}
										}
									}
									county = {
										usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
									}
								} 
							}
						}
					}
					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 }
							NOT = { wealth = 2500 } #unless already rich from tributes
						}
						set_variable = { which = "WP_nomad_provinces" value = 0 }
						any_realm_province = {
							limit = { 
								has_province_modifier = nomad_agitation
								NOT = { has_settlement_construction = yes }
							}
							province_event = { id = JD.60100 } #increase counter for each nomad province
						}
						character_event = { id = JD.60101 days = 1 } #calculate gold sent from china for building holdings
					}
					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 = { NOT = { offmap_china = { has_offmap_name = tang_china } } }
									set_name = "Tang Empire"
									set_title_flag = china_name_Tang
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = jin_china } } }
									set_name = "Jin Empire"
									set_title_flag = china_name_Jin
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = wei_china } } }
									set_name = "Wei Empire"
									set_title_flag = china_name_Wei
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = qi_china } } }
									set_name = "Qi Empire"
									set_title_flag = china_name_Qi
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = zhou_china } } }
									set_name = "Zhou Empire"
									set_title_flag = china_name_Zhou
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = han_china } } }
									set_name = "Han Empire"
									set_title_flag = china_name_Han
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = qin_china } } }
									set_name = "Qin Empire"
									set_title_flag = china_name_Qin
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = yan_china } } }
									set_name = "Yan Empire"
									set_title_flag = china_name_Yan
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = zhao_china } } }
									set_name = "Zhao Empire"
									set_title_flag = china_name_Zhao
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = liao_china } } }
									set_name = "Liao Empire"
									set_title_flag = china_name_Liao
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = yuan_china } } }
									set_name = "Yuan Empire"
									set_title_flag = china_name_Yuan
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = xia_china } } }
									set_name = "Xia Empire"
									set_title_flag = china_name_Xia
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = qing_china } } }
									set_name = "Qing Empire"
									set_title_flag = china_name_Qing
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = dai_china } } }
									set_name = "Dai Empire"
									set_title_flag = china_name_Dai
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = cheng_china } } }
									set_name = "Cheng Empire"
									set_title_flag = china_name_Cheng
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = liang_china } } }
									set_name = "Liang Empire"
									set_title_flag = china_name_Liang
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = song_china } } }
									set_name = "Song Empire"
									set_title_flag = china_name_Song
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = ming_china } } }
									set_name = "Ming Empire"
									set_title_flag = china_name_Ming
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = shu_china } } }
									set_name = "Shu Empire"
									set_title_flag = china_name_Shu
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = wu_china } } }
									set_name = "Wu Empire"
									set_title_flag = china_name_Wu
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = chu_china } } }
									set_name = "Chu Empire"
									set_title_flag = china_name_Chu
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = yue_china } } }
									set_name = "Yue Empire"
									set_title_flag = china_name_Yue
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = yin_china } } }
									set_name = "Yin Empire"
									set_title_flag = china_name_Yin
								}
								1 = { 
									trigger = { NOT = { offmap_china = { 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 days = 1 }
						}
					}
				}
				if = {
					limit = {
						NOT = {
							any_demesne_title = {
								tier = EMPEROR
							}
						}
					}
					set_defacto_liege = ROOT
					if = {
						limit = {
							ROOT = { has_character_flag = is_rebel_general }
						}
						any_demesne_title = {
							limit = {
								tier = KING
							}
							usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
							add_law = {
								law = succ_primogeniture
								cooldown = no
								opinion_effect = no
							}
						}
					}
					break = yes
				}
				any_vassal = {
					set_defacto_liege = ROOT
					if = {
						limit = { ROOT = { has_character_flag = is_rebel_general } }
						opinion = {
							modifier = opinion_subjugated 
							multiplier = 2
							who = ROOT
						}
					}
				}
				any_demesne_title = {
					limit = {
						tier = EMPEROR
						NOT = { controls_religion = yes }
					}
					destroy_landed_title = THIS
				}
				if = {
					limit = {
						NOT = {
							tier = EMPEROR
						}
					}
					set_defacto_liege = ROOT
					break = yes
				}
				any_demesne_title = {
					limit = {
						NOT = { controls_religion = yes }
					}
					ROOT = {
						grant_title_no_opinion = { target = PREV type = invasion }
					}
				}
			}
		}
	}
	
	on_fail = {
		FROM = {
			piety = 100
			participation_scaled_prestige = 250
			set_character_flag = defeated_china
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 250 }
		}
		ROOT = {
			piety = -100
			if = {
				limit = { has_character_flag = is_rebel_general }
				hidden_effect = {
					any_courtier = {
						move_character = FROM
						imprison = FROM
					}
					primary_title = {
						activate_title = { title = THIS status = no }
						destroy_landed_title = THIS
					}
					move_character = FROM
					ROOT = { imprison = FROM }
				}
			}
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -500
			transfer_scaled_wealth = {
				to = FROM
				value = 5.0
				min = 1000
			}
			if = {
				limit = { has_character_flag = is_rebel_general }
				hidden_effect = {
					any_courtier = {
						move_character = FROM
						imprison = FROM
					}
					primary_title = {
						activate_title = { title = THIS status = no }
						destroy_landed_title = THIS
					}
					move_character = FROM
					ROOT = { imprison = FROM }
				}
			}
			hidden_effect = {
				if = { #Makes China have a chance of leaving Expanionist policy...
					limit = {
						NOT = {
							has_character_flag = is_rebel_general
						}
						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]"
			}
		}
		FROM = {
			set_character_flag = defeated_china
			piety = 500
			if = {
				limit = { 
					has_dlc = "Legacy of Rome"
					OR = { 
						has_landed_title = e_byzantium
						has_landed_title = e_roman_empire
					}
					OR = { 
						religion_group = christian
						religion = hellenic_pagan
						religion = hellenic_pagan_reformed
						religion = promethean_pagan
						religion = titan_pagan
					}
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
			participation_scaled_prestige = 2500
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 1000 }
		}
	}

	on_attacker_leader_death = {
		if = {
			limit = { ROOT = { has_character_flag = is_rebel_general } }
			end_war = invalid
			ROOT = {
				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 = {
		ROOT = {
			independent = yes
			NOT = { same_realm = FROM }
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}
		
		FROM = {
			independent = yes
			in_revolt = no
		}
	}

	is_valid = {
		NOT = {
			ROOT = {
				rightful_religious_head_scope = {
					character = 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 = "---------------------"
		FROM = {
			save_event_target_as = target_loser
			prestige = -100
			custom_tooltip = {
				text = "REMOVE_PREVIOUS_SUZERAIN"
				hidden_tooltip = {
					any_suzerain = {
						event_target:target_loser = {
							remove_tributary = PREV
						}
					}
				}
			}
			if = { # Remove Raid China if active
				limit = {
					has_character_modifier = china_raid_active
				}
				remove_character_modifier = china_raid_active
				hidden_effect = {
					add_character_modifier = {
						name = china_raid_grace_cd
						hidden = yes
						years = 5
					}
				}
			}
		}

		ROOT = {
			prestige = 200
			make_tributary = { who = FROM tributary_type = offmap }
		}
	}

	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 = "---------------------"
		ROOT = {
			prestige = -200
		}
		FROM = {
			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 = "---------------------"
		ROOT = {
			prestige = -500
			transfer_scaled_wealth = {
				to = FROM
				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]"
			}
		}

		FROM = {
			piety = 500
			participation_scaled_prestige = 2500
			set_character_flag = defeated_china
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 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 = {
		ROOT = {
			independent = yes
			NOT = { same_realm = FROM }
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}
		
		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 = "---------------------"
		FROM = {
			custom_tooltip = { 
				text = dismantle_pretender_china_success_tt
				hidden_effect = {
					offmap_china = { set_offmap_flag = no_war_news }
					any_player = {
						limit = {
							NOT = { character = ROOT_FROM }
							OR = {
								AND = {
									has_offmap_news_enabled = offmap_china
									is_within_diplo_range = ROOT
								}
								liege = { character = ROOT_FROM }
							}
						}
						narrative_event = { id = JD.60209 }
					}
					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 }
				}
			}
		}
		ROOT = {
			prestige = 200
		}
	}

	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 = "---------------------"
		FROM = {
			piety = 100
			participation_scaled_prestige = 250
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 250 }
		}
		ROOT = {
			piety = -100
		}
	}

	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 = "---------------------"
		ROOT = {
			piety = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 5.0
				min = 500
			}
			offmap_power = { set_offmap_tmp_flag = pretender_chinese_empire_war_cooldown }
		}
		FROM = {
			piety = 200
			participation_scaled_prestige = 1000
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 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 = "---------------------"
		#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
	}
}

force_open_china_cb = {
	name = CB_NAME_FORCE_OPEN_CHINA
	war_name = WAR_NAME_FORCE_OPEN_CHINA
	sprite = 30
	truce_days = 365
	hostile_against_others = yes
	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no
	allow_whitepeace = no
	max_attacker_battle_score = 100
	capturing_defender_is_complete_victory = no
	
	can_use = {
		ROOT = {
			independent = yes
			NOT = { same_realm = FROM }
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}
		
		FROM = {
			is_offmap_governor = yes
			offmap_power = { is_offmap_tag = offmap_china }
		}
	}

	is_valid = {
		offmap_china = { has_policy = china_isolationist }
	}

	on_success = {
		FROM = {
			save_event_target_as = target_governor
			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 }
				hidden_effect = {
					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
			}
		}
		ROOT = {
			participation_scaled_prestige = 1000
			add_grace_massive_effect = yes
			add_character_modifier = {
				modifier = chinese_favored_in_trade
				inherit = yes
				years = 25
			}
		}
		FROM = {
			transfer_scaled_wealth = {
				to = ROOT
				value = 5.0
				min = 2000
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 300 }
		}
	}

	on_fail = {
		FROM = {
			piety = 100
			participation_scaled_prestige = 50
			hidden_effect = { offmap_power = { clr_offmap_tmp_flag = china_force_open_in_progress } }
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		ROOT = {
			prestige = -150
		}
	}

	on_reverse_demand = {
		ROOT = {
			show_scope_change = no
			prestige = -300
			transfer_scaled_wealth = {
				to = FROM
				value = 5.0
				min = 500
			}
			custom_tooltip = {
				text = angered_china_tt #Scripter! Update this is you change the value below.
				add_character_modifier = {
					modifier = angered_china_modifier
					inherit = yes
					years = 50
				}
			}
		}
		FROM = {
			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 }
					}
					clr_offmap_tmp_flag = china_force_open_in_progress
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}
	
	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
	}
}

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 = {
		ROOT = {
			independent = yes
			NOT = { same_realm = FROM }
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}
	}

	on_success = {
		FROM = {
			custom_tooltip = {
				text = destabilization_china_success_tt
				hidden_effect = {
					offmap_china = { set_offmap_flag = no_war_news }
					any_player = {
						limit = {
							NOT = { character = ROOT_FROM }
							OR = {
								AND = {
									has_offmap_news_enabled = offmap_china
									is_within_diplo_range = ROOT
								}
								liege = { character = ROOT_FROM }
							}
						}
						narrative_event = { id = JD.60209 }
					}
					any_demesne_title = {
						limit = {
							higher_tier_than = DUKE
							NOR = {
								controls_religion = yes
								clan = yes
							}
						}
						unsafe_destroy_landed_title = THIS
					}
					#death = { death_reason = death_executed_by_chinese_emperor }
				}
			}
		}
		ROOT = {
			prestige = 200
		}
	}

	on_fail = {
		FROM = {
			piety = 100
			participation_scaled_prestige = 250
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 250 }
		}
		ROOT = {
			piety = -100
		}
	}

	on_reverse_demand = {
		ROOT = {
			piety = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 5.0
				min = 1000
			}
		}
		FROM = {
			piety = 500
			participation_scaled_prestige = 2500
			set_character_flag = defeated_china
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 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
	}
}

invasion_of_china_cb = {
	name = CB_NAME_INVASION_OF_CHINA
	war_name = WAR_NAME_INVASION_OF_CHINA
	sprite = 30
	truce_days = 365
	hostile_against_others = yes
	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no
	allow_whitepeace = no
	battle_warscore_mult = 1.5
	max_attacker_occupation_score = 50
	max_defender_occupation_score = 50
	max_attacker_battle_score = 100
	capturing_defender_is_complete_victory = no
	third_party_portrait = event_target:invasion_of_china_claimant

	# 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 = {
		ROOT = {
			independent = yes
			NOT = { same_realm = FROM }
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}
		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 = {
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = invasion_of_china_success_tt
				hidden_effect = {
					if = {
						limit = { kcc_china_has_persecuted_trigger = yes }
						1513 = { province_event = { id = kccpersecute.0043 days = 10 } }
					}
					remove_character_modifier = china_raid_active
					event_target:invasion_of_china_claimant = {
						betrothed = { break_betrothal = PREV }
						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
						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 = manichean
								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_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_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_pagan_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_kemetisms" 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_megalithic_reformeds" 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_cybelene_reformeds" 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_zalmoxian_reformeds" 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 } }
						}
						#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 = 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 = {
						governor = {
							any_vassal = {
								set_defacto_liege = ROOT
							}
							any_demesne_title = {
								limit = {
									NOT = {
										tier = EMPEROR
									}
								}
								grant_title = ROOT
							}
						}
						ROOT = {
							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 = ""
								if = {
									limit = {
										religion_group = muslim
									}
									set_government_type = muslim_government
								}
								else_if = {
									limit = {
										culture_group = tibetan_group
										OR = {
											religion = bon
											religion = buddhist
										}
										higher_tier_than = BARON
									}
									set_government_type = theocratic_feudal_government
								}
								else = {
									set_government_type = feudal_government
								}
							}
						}
						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 = FROM
								}
								liege = { character = ROOT }
							}
						}
						narrative_event = { id = JD.60401 }
					}
				}
			}
			participation_scaled_prestige = 5000
			set_offmap_currency = { offmap = offmap_china value = 5000 }
			wealth = 10000
			add_character_modifier = { name = chinese_favored_in_trade inherit = yes years = 50 }
			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
			if = { 
				limit = { 
					this_has_super_cool_nickname_trigger = no 
				}
				ROOT = { 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 = manichean
						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_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_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_pagan_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_kemetisms" 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_megalithic_reformeds" 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_cybelene_reformeds" 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_zalmoxian_reformeds" 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 } }
				}
				#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 = 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 = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 2000 }
		}
		clear_global_event_target = invasion_of_china_claimant
	}

	on_invalidation = {
		FROM = {
			piety = 200
			participation_scaled_prestige = 250
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 250 }
		}
		ROOT = {
			prestige = -2500
			remove_character_modifier = war_with_china_modifier
			custom_tooltip = {
				text = invasion_of_china_cb_fail_tt
				hidden_effect = { 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 = FROM
							}
							liege = { character = ROOT }
						}
					}
					narrative_event = { id = JD.60403 }
				}
			}
			custom_tooltip = {
				text = angered_china_tt #Scripter! Update this is you change the value below.
				add_character_modifier = {
					modifier = angered_china_modifier
					inherit = yes
					years = 50
				}
			}
		}
		clear_global_event_target = invasion_of_china_claimant
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -3000
			transfer_scaled_wealth = {
				to = FROM
				value = 5.0
				min = 5000
			}
			remove_character_modifier = war_with_china_modifier
			custom_tooltip = {
				text = invasion_of_china_cb_fail_tt
				hidden_effect = { add_character_modifier = { name = failed_invasion_of_china duration = -1 hidden = yes } }
			}
			custom_tooltip = {
				text = destabilization_china_success_tt
				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 = FROM
								}
								liege = { character = ROOT }
							}
						}
						narrative_event = { id = JD.60402 }
					}
					any_demesne_title = {
						limit = {
							higher_tier_than = DUKE
							NOR = {
								controls_religion = yes
								clan = yes
							}
						}
						destroy_landed_title = THIS
					}
				}
			}
			custom_tooltip = {
				text = angered_china_tt #Scripter! Update this is you change the value below.
				add_character_modifier = {
					modifier = angered_china_modifier
					inherit = yes
					years = 50
				}
			}
		}
		FROM = {
			piety = 200
			participation_scaled_prestige = 1000
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 1000 }
		}
		clear_global_event_target = invasion_of_china_claimant
	}
	
	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
	}
}

truebelievers_bid_for_independence = {
	name = CB_NAME_INDEPENDENCE
	war_name = WAR_NAME_TRIBAL_INDEPENDENCE #Faction Independence
	sprite = 12
	truce_days = 3650
	major_revolt = yes
	
	attacker_can_call_allies = no
	can_ask_to_join_war = no
	
	is_independence = yes
	
	can_use = {
		ROOT = {
			vassal_of = FROM
		}
	}

	is_valid = {
		ROOT = {
				NOT = { religion = FROM }
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
		FROM = {
			any_demesne_title = {
				NOT = { lower_tier_than = ROOT }
				temporary = no
			}
			independent = yes
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
		}
	}

	on_success = {
		#FROM = { 
		#	any_vassal = { 
		#			limit = { has_character_flag = independent_joining_flag }
		#			set_defacto_liege = THIS
		#			clr_character_flag = independent_joining_flag
		#		} 
		#	}
		if = {
			limit = {
				FROM = {
					independent = no
					liege = { religion = ROOT }
				}
			}
			FROM = {
				if = {
					limit = {
						in_revolt = yes
					}
					liege_before_war = {
						#ROOT = {
						#	set_defacto_liege = PREV
						#}
						any_attacker = {
							set_defacto_liege = PREV
						}
					}
				}
				if = {
					limit = {
						in_revolt = no
					}
					liege = {
						#ROOT = {
						#	set_defacto_liege = PREV
						#}
						any_attacker = {
							set_defacto_liege = PREV
						}
					}
				}
			}
		}

		if = {
			limit = {
				FROM = {
					OR = { independent = yes
						liege = { religion = ROOT }
					}
				}
			}
			#ROOT = {
			#	set_defacto_liege = ROOT
			#}
			any_attacker = {
				set_defacto_liege = ROOT
				clr_character_flag = flag_truebelievers_revolter
			}
		}

		hidden_tooltip = {
			ROOT = {
				clr_character_flag = flag_truebelievers_revolter
				prestige = 100
				piety = 100
				religion_authority = {
					modifier = won_holy_war
					years = 20
				}
				any_demesne_title = {
					add_pressed_claim = FROM
				}
			}
			FROM = {
				religion_authority = {
					modifier = lost_holy_war
					years = 20
				}	
			}	
		}
		
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
			participation_scaled_piety = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_prestige = 100
				participation_scaled_piety = 100

			}
		}

		FROM = {
			any_demesne_title = { #All titles
				limit = {
					OR = {
						tier = duke
						tier = king
						tier = emperor
					}
					#is_titular = no
					NOT = {	has_law = tribal_organization_0	}
				}
				if = {
					limit = { has_law = tribal_organization_1 }
					add_law = tribal_organization_0
				}
				if = {
					limit = { has_law = tribal_organization_2 }
					add_law = tribal_organization_0
				}
				if = {
					limit = { has_law = tribal_organization_3 }
					add_law = tribal_organization_1
				}
				if = {
					limit = { has_law = tribal_organization_4 }
					add_law = tribal_organization_2
				}
			}
		}

		hidden_tooltip = {
			check_if_reconquista_finished_effect = yes
		}
	}

	on_fail = {
		if = {
			limit = {
				FROM = {
					independent = no
				}
			}
			FROM = {
				if = {
					limit = {
						in_revolt = yes
					}
					liege_before_war = {
						ROOT = {
							set_defacto_liege = PREV
						}
					}
				}
				if = {
					limit = {
						in_revolt = no
					}
					liege = {
						ROOT = {
							set_defacto_liege = PREV
						}
						any_vassal = { limit = { has_character_flag = independent_joining_flag }
							set_defacto_liege = PREV
							clr_character_flag = independent_joining_flag
						}
					}
				}
			}
		}

		if = {
			limit = {
				FROM = {
					independent = yes
				}
			}
			ROOT = {
				set_defacto_liege = FROM
			}
						any_vassal = { limit = { has_character_flag = independent_joining_flag }
							set_defacto_liege = PREV
							clr_character_flag = independent_joining_flag
						}
		}

		hidden_tooltip = {
			ROOT = {
				prestige = 100
				any_demesne_title = {
					add_pressed_claim = FROM
				}
			}
			any_attacker = {
				clr_character_flag = flag_truebelievers_revolter
			}
		}
	}

	on_reverse_demand = {
		hidden_tooltip = {
			any_attacker = {
				clr_character_flag = flag_truebelievers_revolter
			}
			ROOT = {
				religion_authority = {
					modifier = lost_holy_war
					years = 20
				}
			}
			FROM = {
				religion_authority = {
					modifier = won_holy_war
					years = 20
				}	
			}	
		}
		ROOT = {
			prisoner = FROM
		}
		any_attacker = {
			hidden_tooltip = { 
				participation_scaled_prestige = -100
				participation_scaled_piety = -100 
			}
		}
		FROM = {
			prestige = 100
			piety = 100
			hidden_tooltip = { 
				if = { 
					limit = { NOT = { has_opinion_modifier = { modifier = opinion_traitor who = ROOT } } } #Allow free revocation of a title.
					opinion = {
						who = ROOT
						modifier = opinion_traitor
					}
				}
			}
		}
	}
	on_invalidation = {
		any_attacker = {
			clr_character_flag = flag_truebelievers_revolter
		}
	} 

	attacker_ai_victory_worth = {
		factor = -1 #always accept
	}
	
	attacker_ai_defeat_worth = {
		factor = 150
	}

	defender_ai_victory_worth = {
		factor = -1 #always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
	}
}

# Event-tied cb for Papal Holy War (that is, holy war on behalf of the Papacy)
pepin_religious = {
	name = CB_NAME_PAPAL_RELIGIOUS
	war_name = WAR_NAME_PAPAL_RELIGIOUS
	sprite = 9
	truce_days = 3650
	is_permanent = yes
	is_holy_war = yes
	check_de_jure_tier = DUKE # this scans all dejure duchies for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes

	allowed_to_target_tributaries = yes
	attacker_rel_head_is_ally = yes # The attacker can call his (main) Pope into the war
	
	sort_priority = 790
	
	on_add = {
		hidden_tooltip = { fire_haruspicy_event_effect = yes }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		ROOT = { #The cb is either available due to Coronation dealings, or as any King or Emperor ruler who has a Pope for Religious Head.
			has_dlc = "Holy Fury"
			higher_tier_than = DUKE 
			OR = { 
				has_character_flag = flag_pepin_for_coronation 
				AND = { 
					religion = catholic
					rightful_religious_head_scope = { has_landed_title = k_papal_state }
				}
				AND = { 
					religion = fraticelli
					rightful_religious_head_scope = { has_landed_title = d_fraticelli }
				}
			}
			#war = no 
			is_feudal = yes
			independent = yes
			OR = {
				religion = catholic
				religion = fraticelli
			}
			NOT = { same_realm = FROM }
		#	OR = {
		#		NOT = { religion_group = FROM }
		#		is_parent_religion = FROM
		#	}
		}
		FROM = { 
			is_within_diplo_range = ROOT
			NOT = { has_landed_title = k_papal_state }
			NOT = { has_landed_title = d_fraticelli }
			OR = { 		#Target must either be a pagan or heretic of any government form, or it must be a disliked Catholic/Fraticelli who is not a bishop.
				AND = { 
					is_theocracy = no 
					religion = ROOT
					ROOT = { 
						religion_head = { 
							NOT = { 
								opinion = { 
									who = FROM
									value = -50
								}
							}
						} 
					} 
				}
				excommunicated_for = ROOT
				NOT = { religion_group = ROOT } 
				is_heresy_of = ROOT
			}
		}
	}
	
	can_use_title = {
		# The attacker needs to be within diplo range of the target, and the target title must be one of the historical Papal Duchies.
		OR = { 
			title = d_latium
			title = d_spoleto
			title = d_ancona
			title = d_ferrara
			title = d_benevento

			d_latium = {
				any_de_jure_vassal_title = { title = PREVPREV }
			}
			d_spoleto = {
				any_de_jure_vassal_title = { title = PREVPREV }
			}
			d_ancona = {
				any_de_jure_vassal_title = { title = PREVPREV }
			}
			d_ferrara = {
				any_de_jure_vassal_title = { title = PREVPREV }
			}
			d_benevento = {
				any_de_jure_vassal_title = { title = PREVPREV }
			}
		}
		any_de_jure_vassal_title = {
			location = {
				region = world_europe_south_italy
			}
		}
	}

	is_valid = {
		ROOT = {
			OR = { 
				#has_character_flag = flag_pepin_for_coronation 
				AND = { 
					religion = catholic
					rightful_religious_head_scope = { has_landed_title = k_papal_state }
				}
				AND = { 
					religion = fraticelli
					rightful_religious_head_scope = { has_landed_title = d_fraticelli }
				}
			}
			NOT = { religion_group = pagan_group }
			NOT = { pacifist = yes }
			NOT = { same_realm = FROM }
			OR = {
				religion = catholic
				religion = fraticelli
			}
		}
	}

	on_invalidation = {
		ROOT = { 
			clr_character_flag = flag_pepin_for_coronation
		} 
	}
	
	on_success = {
		ROOT = {
			if = { 
				limit = { 
					religion_head = { 
						any_artifact = { 
							has_artifact_flag = christian
							NOR =  { 
								has_artifact_flag = very_rare
								has_artifact_flag = unique #Won't give the holiest ones away.
							}
						} 
					} 
				}
				random_list = { 
					20 = { 
						modifier = { 
							factor = 0
							NOT = { 
								religion_head = { 
									opinion = { 
										who = ROOT 
										value = 30
									} 
								} 
							}
						}
						modifier = { 
							factor = 1.5
							religion_head = { 
								opinion = { 
									who = ROOT 
									value = 50
								} 
							}
						}
						modifier = { 
							factor = 1.5
							religion_head = { 
								opinion = { 
									who = ROOT 
									value = 70
								} 
							}
						}
						modifier = { 
							factor = 1.5
							religion_head = { 
								opinion = { 
									who = ROOT 
									value = 90
								} 
							}
						}
						set_character_flag = flag_pope_gifts_artifact #Effect happens in event. 
					}
					80 = { #Nothing happens
					}
				}
			}
			character_event = { id = HF.20224 } #Congratulations\ Papal Coronation may proceed if flagged.
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_piety = 75
				participation_scaled_prestige = 50
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
				hidden_tooltip = {
					if = {
						limit = {
							has_ambition = obj_strengthen_religion
						}
						change_variable = { which = strengthen_religion value = 1 }
					}
				}
			}
		}
		
		ROOT = { 
			participation_scaled_piety = 150
			participation_scaled_prestige = 75

			rightful_religious_head_scope = {
				opinion = {
					who = ROOT
					modifier = opinion_papal_land_donation
				}
			}
			religion_authority = {
				modifier = won_holy_war
				years = 20
			}
			hidden_tooltip = {
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
		}
		FROM = {
			if = {
				limit = {
					NOT = {
						has_character_modifier = known_satanist
					}
				}
				religion_authority = {
					modifier = lost_holy_war
					years = 20
				}		
			}
		}
		FROM = { piety = -100 }
		FROM = { prestige = -50 }
	}

	on_success_title = {
		custom_tooltip = {
			text = pepin_religious_cb_succ_tip
			hidden_tooltip = {
				ROOT = {
					religion_head = { 
						vassalize_or_take_under_title = {
							title = PREVPREV
							enemy = FROM
							same_religion = yes # Only vassalize rulers of my religion
							is_religious = yes
							type = holy_war
						}
					}
				}
			}
		}
	}

	on_fail = {
		ROOT = { 
			clr_character_flag = flag_pepin_for_coronation
		}
		ROOT = { 
			piety = -50 
			prestige = -100
		}
	}

	on_reverse_demand = {
		ROOT = { 
			clr_character_flag = flag_pepin_for_coronation
		}
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -75
			prestige = -150
		}
		FROM = {
			participation_scaled_piety = 100
			participation_scaled_prestige = 75
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_piety = 75
				participation_scaled_prestige = 50
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
				hidden_tooltip = {
					if = {
						limit = {
							has_ambition = obj_strengthen_religion
						}
						change_variable = { which = strengthen_religion value = 1 }
					}
				}
			}
		}
		any_attacker = {
			limit = {
				character = ROOT
				uses_decadence = yes
			}
			participation_scaled_decadence = 20
		}
		any_attacker = {
			limit = {
				NOT = { character = ROOT }
				uses_decadence = yes
			}
			hidden_tooltip = { 
				participation_scaled_decadence = 20
			}
		}
		ROOT = { 
			religion_authority = {
				modifier = lost_holy_war
				years = 20
			}
		}
		FROM = { 
			religion_authority = {
				modifier = won_holy_war
				years = 20
			}
			hidden_tooltip = {
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
		}
	}

	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 = 1 
		modifier = { #Focus on Coronation first.
			factor = 0.1
			has_dlc = "Holy Fury"
			ROOT = {
				is_feudal = yes
				OR = {
					religion = catholic
					religion = fraticelli
				}
				NOR = {
					trait = crowned_by_priest
					trait = crowned_by_bishop
					trait = crowned_by_pope
					trait = crowned_by_myself
				}
			}
		}
		modifier = { 
			factor = 2
			ROOT = {
				has_landed_title = e_hre
			}
		}
		modifier = { 
			factor = 2
			ROOT = {
				has_landed_title = e_france
			}
		}
		modifier = { 
			factor = 0.25
			ROOT = {
				NOT = {
					trait = zealous
				}
			}
		}
		modifier = { #I like the Pope and I will help him.
			factor = 1.5
			religion_head = {
				reverse_opinion = { 
					who = ROOT 
					value = 50 
				}
			}
		}
		modifier = { #I like the Pope and I will help him.
			factor = 1.5
			religion_head = {
				reverse_opinion = { 
					who = ROOT 
					value = 60 
				}
			}
		}
		modifier = { #I like the Pope and I will help him.
			factor = 1.5
			religion_head = {
				reverse_opinion = { 
					who = ROOT 
					value = 70 
				}
			}
		}
		modifier = { #I like the Pope and I will help him.
			factor = 2
			religion_head = {
				reverse_opinion = { 
					who = ROOT 
					value = 80 
				}
			}
		}
		modifier = { #I like the Pope and I will help him.
			factor = 2
			religion_head = {
				reverse_opinion = { 
					who = ROOT 
					value = 90 
				}
			}
		}
		modifier = { #Heathens are holding Italy, take action!
			factor = 5
			ROOT = {
				NOT = {
					trait = cynical
				}
			}
			FROM = {
				NOT = { religion = ROOT }
			}
		}
		modifier = { #The Papacy doesn't hold Rome, take action!
			factor = 5
			FROM = {
				OR = {
					has_landed_title = d_latium
					any_vassal = { has_landed_title = d_latium }
					d_latium = {
						any_de_jure_vassal_title = { owner = { character = PREVPREVPREV } }
					}
				}
			}
		}
		modifier = { 
			factor = 2
			FROM = {
				OR = {
					has_landed_title = d_spoleto
					any_vassal = { has_landed_title = d_spoleto }
					d_spoleto = {
						any_de_jure_vassal_title = { owner = { character = PREVPREVPREV } }
					}
				}
			}
		}
		modifier = { #Historical tweaking
			factor = 0.25
			FROM = {
				OR = {
					has_landed_title = d_benevento
					any_vassal = { has_landed_title = d_benevento }
					d_benevento = {
						any_de_jure_vassal_title = { owner = { character = PREVPREVPREV } }
					}
				}
			}
		}
		modifier = {
			factor = 0.8
			ROOT = {
				distance_from_realm = { who = FROM value = 40 }
			}
		}
		modifier = {
			factor = 0.8
			ROOT = {
				distance_from_realm = { who = FROM value = 80 }
			}
		}		
		modifier = {
			factor = 0.75
			ROOT = {
				distance_from_realm = { who = FROM value = 120 }
			}
		}
		modifier = {
			factor = 0.5
			ROOT = {
				distance_from_realm = { who = FROM value = 150 }
			}
		}
		modifier = {
			factor = 0.25
			ROOT = {
				distance_from_realm = { who = FROM value = 200 }
			}
		}
	}
}

# Romans rise up after their city's churches have been destroyed by mad Emperor.
roman_christian_revolt = {
	name = CB_NAME_HERETIC_UPRISING
	war_name = WAR_NAME_HERETIC_UPRISING
	sprite = 7
	truce_days = 365
	hostile_against_others = yes
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no
	check_de_jure_tier = DUKE
	is_permanent = yes
	battle_warscore_mult = 0.5
	allow_whitepeace = no
	
	can_use = {
		ROOT = { has_character_flag = roman_christian_revolter }
	}

	on_add = {
		hidden_tooltip = { fire_haruspicy_event_effect = yes }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}
 
	on_success = {
		ROOT = {
			prestige = 200
			piety = 100
			occupy_minors_of_occupied_settlements = FROM
			gain_all_occupied_titles = { who = FROM type = revolt }
			clr_character_flag = religious_revolter
		}
		
		FROM = {
			prestige = -500
			piety = -500
			abdicate = yes
			religion_authority = {
				modifier = lost_religious_revolt
			}		
		}
	}
	
	on_success_title = {
		ROOT = {
			vassalize_or_take_under_title = {
				title = PREV
				enemy = FROM
				same_religion = yes # Only vassalize rulers of my religion
				is_religious = yes
				type = holy_war
			}
		}
		if = {
			limit = {
				controls_religion = no
				holder_scope = {
					OR = {
						character = FROM
						is_liege_or_above = FROM
					}
				}
			}
			usurp_title = { target = ROOT type = holy_war }
		}
		if = {
			limit = {
				has_holder = no
			}
			usurp_title = { target = ROOT type = holy_war }
		}
	}

	#on_fail = {
		#ROOT = {
			#clear_wealth = yes
			#imprison = FROM
			#clr_character_flag = religious_revolter
			#hidden_tooltip = {
				#add_character_modifier = { 
					#name = broken_spirit
					#days = -1
				#}
			#}
		#}
		#FROM = {
			#prestige = 50
			#piety = 100
			#hidden_tooltip = {
				#opinion = {
					#modifier = opinion_rebel_traitor 
					#who = ROOT
					#months = 1200
				#}
			#}
			#religion_authority = {
				#modifier = won_religious_revolt
			#}
		#}
	#}

	on_reverse_demand = {
		ROOT = {
			clear_wealth = yes
			imprison = FROM
			clr_character_flag = religious_revolter
			hidden_tooltip = {
				add_character_modifier = { 
					name = broken_spirit
					days = -1
				}
			}
		}
		FROM = {
			prestige = 100
			piety = 200
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor 
					who = ROOT
					months = 1200
				}
				add_character_modifier = { 
					name = crushed_revolt
					days = 3650
				}
			}
			religion_authority = {
				modifier = won_religious_revolt
			}
		}
		hidden_tooltip = { 
			if = { 
				limit = { 
					FROM = { 
						NOR = { 
							religion = hellenic_pagan
							religion = hellenic_pagan_reformed
							religion = promethean_pagan
							religion = titan_pagan
			 			} 
			 		}
			 	}
				333 = { set_province_flag = flag_rome_primed_for_conversion } #If the Emperor burning Rome is about to switch to hellenism and wins the war, Rome turns hellenic automatically.
			} 
			if = { 
				limit = { 
					FROM = { 
						OR = { 
							religion = hellenic_pagan
							religion = hellenic_pagan_reformed
							religion = promethean_pagan
							religion = titan_pagan
			 			} 
			 		}
			 	}
				333 = { religion = FROM } #If he has already revealed his conversion, convert the province immediately.
			} 
		}
	}

	attacker_ai_victory_worth = {
		factor = 100
	}
	
	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 150
	}
}

abrahamic_antihellenic_revolt = {
	name = CB_ANTIHELLENIC_WAR
	war_name = WAR_NAME_ANTIHELLENIC_WAR
	sprite = 12
	truce_days = 3650
	major_revolt = yes
	allow_whitepeace = no
	
	attacker_can_call_allies = no
	can_ask_to_join_war = no
	
	is_independence = yes
	
	can_use = {
		ROOT = {
			vassal_of = FROM
		}
	}
	on_add = {
		hidden_tooltip = { fire_haruspicy_event_effect = yes }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	is_valid = {
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
		FROM = {
			any_demesne_title = {
				NOT = { lower_tier_than = ROOT }
				temporary = no
			}
			independent = yes
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
		}
	}

	on_success = {
		#FROM = { 
		#	any_vassal = { 
		#			limit = { has_character_flag = independent_joining_flag }
		#			set_defacto_liege = THIS
		#			clr_character_flag = independent_joining_flag
		#		} 
		#	}

		if = {
			limit = {
				FROM = {
					current_heir = { 
						NOT = { religion_group = pagan_group }
					}
				}
			}
			FROM = { 
				abdicate = yes 
				death = { death_reason = death_execution_burning }
			}
		}
		if = {
			limit = {
				FROM = {
					current_heir = { 
						religion_group = pagan_group
					}
				}
			}
			FROM = { 
				abdicate_to = ROOT 
				death = { death_reason = death_execution_burning }
			}
		}
		any_attacker = {
			clr_character_flag = flag_abrahamic_revolter
		}

		hidden_tooltip = {
			ROOT = {
				clr_character_flag = flag_abrahamic_revolter
				prestige = 100
				piety = 100
				religion_authority = {
					modifier = won_holy_war
					years = 20
				}
				any_demesne_title = {
					add_pressed_claim = FROM
				}
			}
			FROM = {
				religion_authority = {
					modifier = lost_holy_war
					years = 20
				}	
			}	
		}
		
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
			participation_scaled_piety = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_prestige = 100
				participation_scaled_piety = 100

			}
		}
	}

	#on_fail = {
		#if = {
			#limit = {
				#FROM = {
					#independent = yes
				#}
			#}
			#ROOT = {
				#set_defacto_liege = FROM
			#}
			#any_vassal = { 
				#limit = { has_character_flag = independent_joining_flag }
				#set_defacto_liege = PREV
				#clr_character_flag = independent_joining_flag
			#}
		#}
		#hidden_tooltip = {
			#ROOT = {
				#prestige = 100
				#any_demesne_title = {
					#add_pressed_claim = FROM
				#}
			#}
			#any_attacker = {
				#clr_character_flag = flag_abrahamic_revolter
			#}
		#}
	#}

	on_reverse_demand = {
		hidden_tooltip = {
			any_attacker = {
				clr_character_flag = flag_abrahamic_revolter
			}
			ROOT = {
				religion_authority = {
					modifier = lost_holy_war
					years = 20
				}
			}
			FROM = {
				religion_authority = {
					modifier = won_holy_war
					years = 20
				}	
			}
			any_player = { narrative_event = { id = HF.23117 } }
			if = {
				limit = {
					FROM = { ai = yes }	
				}
				FROM = { narrative_event = { id = HF.23117 } }		
			}	
		}
		ROOT = {
			prisoner = FROM
		}
		any_attacker = {
			hidden_tooltip = { 
				participation_scaled_prestige = -100
				participation_scaled_piety = -100 
			}
		}
		FROM = {
			prestige = 100
			piety = 100
			hidden_tooltip = { 
				if = { 
					limit = { NOT = { has_opinion_modifier = { modifier = opinion_traitor who = ROOT } } } #Allow free revocation of a title.
					opinion = {
						who = ROOT
						modifier = opinion_traitor
					}
				}
			}
		}
	}
	on_invalidation = {
		any_attacker = {
			clr_character_flag = flag_abrahamic_revolter
		}
	} 

	#on_attacker_leader_death = {
		#hidden_tooltip = {
			#any_defender = {
				#letter_event = {
					#id = 251
				#}
			#}
		#}
		#end_war = invalid
	#}

	attacker_ai_victory_worth = {
		factor = -1 #always accept
	}
	
	attacker_ai_defeat_worth = {
		factor = 150
	}

	defender_ai_victory_worth = {
		factor = -1 #always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
	}
}

flower_war_cb = {
	name = CB_NAME_FLOWER_WAR
	war_name = WAR_NAME_FLOWER_WAR
	sprite = 21
	truce_days = 3650
	is_permanent = yes
	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no
	
	sort_priority = 1000
	
	can_use = {
		ROOT = {
			has_dlc = "Holy Fury"
			OR = {
				has_religion_feature = religion_ritual_sacrifice
				religion = aztec
				religion = aztec_reformed
			}
			NOT = { has_religion_feature = religion_peaceful } #Aztecs could reform as Peaceful.
			NOT = { same_realm = FROM }
			NOT = { is_liege_or_above = FROM }
			mercenary = no
			trait = bloodthirsty_gods_3
		}
		
		
		FROM = {
			in_revolt = no
			is_nomadic = no
			NOT = { is_offmap_governor = yes }
			NOT = {
				any_war = {
					defender = { character = PREVPREV }
					using_cb = flower_war_cb
				}
			}
		}
	}
	is_valid = {
		ROOT = { 
			OR = {
				has_religion_feature = religion_ritual_sacrifice
				religion = aztec
				religion = aztec_reformed
			}
			trait = bloodthirsty_gods_3 
		}
	}

	on_add = {
		hidden_tooltip = { fire_haruspicy_event_effect = yes }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	on_success = {
		ROOT = {
			show_scope_change = no
			participation_scaled_piety = 100
			participation_scaled_prestige = 100
			hidden_tooltip = { change_variable = { which = burned_hearts_points value = 5 } }
			religion_authority = {
				modifier = won_flower_war
				years = 30
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_piety = 50 }
		}
		FROM = {
			piety = -100
			prestige = -100
			
			if = {
				limit = {
					is_reformed_religion = ROOT
					controls_religion = no
				}
				religion = ROOT # Reformed pagans are forced to convert back
			}
			
			custom_tooltip = {
				text = flower_war_cb_tooltip
			}
				
			hidden_tooltip = {
				any_courtier = { #Sacrifice part of the court.
					limit = {
						is_ruler = no
					}
					random = { 
						chance = 25 
						modifier = {
							factor = 3
							is_unimportant_trigger = yes
						}
						modifier = {
							factor = 3
							has_minor_title = title_commander
						}
						modifier = { #Less likely to be in battle.
							factor = 0.5
							is_adult = no
						}
						modifier = { #Less likely to be in battle.
							factor = 0.5
							is_adult = no
							is_child_of = FROM
						}
						death = { 
							death_reason = death_execution_aztec_sacrifice 
							killer = ROOT
						}
					}
				}
				opinion = {
					modifier = opinion_abducted_peasants
					who = ROOT
				}
				any_vassal = {
					opinion = {
						modifier = opinion_abducted_peasants 
						who = ROOT
					}
				}				
				any_demesne_province = {
					add_province_modifier = { name = bloodthirsty_flower_war_province years = 10 stacking = yes } 
					change_variable = { which = prosperity_value value = -40 }
					if = { 
						limit = { NOT = { check_variable = { which = prosperity_value value = 0 } } } 
						set_variable = { which = prosperity_value value = 0 }
					}
				}
				any_vassal = {
					limit = {
						higher_tier_than = BARON
					}
					any_demesne_province = { 
						add_province_modifier = { name = bloodthirsty_flower_war_province years = 10 stacking = yes } 
						change_variable = { which = prosperity_value value = -40 } 
						if = { 
							limit = { NOT = { check_variable = { which = prosperity_value value = 0 } } } 
							set_variable = { which = prosperity_value value = 0 }
						}
					}
				}
			}
		}
	}
	
	on_fail = {
		FROM = {
			show_scope_change = no
			piety = 50
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		ROOT = {
			show_scope_change = no
			piety = -100
		}
	}
	
	on_reverse_demand = {
		ROOT = {
			show_scope_change = no
			piety = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
			religion_authority = {
				modifier = lost_flower_war
				years = 30
			}
		}
		FROM = {
			show_scope_change = no
			piety = 100
			if = {
				limit = { 
					has_dlc = "Legacy of Rome"
					OR = { 
						has_landed_title = e_byzantium
						has_landed_title = e_roman_empire
					}
					OR = { 
						religion_group = christian
						religion = hellenic_pagan
						religion = hellenic_pagan_reformed
						religion = promethean_pagan
						religion = titan_pagan
					}
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 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 = 1
		modifier = { #Focus on Coronation first.
			factor = 0.1
			has_dlc = "Holy Fury"
			ROOT = {
				is_feudal = yes
				OR = {
					religion = catholic
					religion = fraticelli
				}
				NOR = {
					trait = crowned_by_priest
					trait = crowned_by_bishop
					trait = crowned_by_pope
					trait = crowned_by_myself
				}
			}
		}
	}
}

childrens_crusade = {
	name = CB_NAME_CRUSADE
	war_name = WAR_NAME_CRUSADE
	sprite = 9
	truce_days = 3650
	is_permanent = yes
	is_holy_war = yes
	battle_warscore_mult = 0.5
	max_defender_occupation_score = 85
	check_de_jure_tier = KING # this scans all de jure kingdoms for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	allow_whitepeace = no
	capturing_defender_is_complete_victory = no
	
	allowed_to_target_tributaries = no

	can_use_gui = {
		ROOT = {
			religion = catholic
			has_character_flag = flag_childrens_crusade_leader #Only available for the child crusader.
			has_global_flag = christian_crusades_unlocked
		}
	}

	can_use = {
		ROOT = {
			religion = catholic
			has_character_flag = flag_childrens_crusade_leader #Only available for the child crusader.
			has_global_flag = christian_crusades_unlocked
		}
		
		FROM = {
			OR = {
				NOT = { religion_group = ROOT }
				is_heresy_of = ROOT
				is_parent_religion = ROOT
				AND = {
					religion_group = pagan_group
					NOT = { religion = ROOT }
				}
			}
		}
	}
	
	is_valid = {
		ROOT = {
			has_character_flag = flag_childrens_crusade_leader #Only available for the child crusader.
		}
		FROM = {
			OR = {
				NOT = { religion_group = ROOT }
				is_heresy_of = ROOT
				is_parent_religion = ROOT
				AND = {
					religion_group = pagan_group
					NOT = { religion = ROOT }
				}
			}
		}
	}

	is_valid_title = {
		FROM = {
			any_realm_province = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}

	on_invalidation = { 
		ROOT = { 
			clr_character_flag = flag_childrens_crusade_leader
			remove_character_modifier = children_crusade_morale_0
			remove_character_modifier = children_crusade_morale_1
			remove_character_modifier = children_crusade_morale_2
			remove_character_modifier = children_crusade_morale_3
			remove_character_modifier = children_crusade_morale_4
			any_courtier = { 
				limit = {
					is_ruler = no
					is_landed = no
					liege = { character = ROOT }
				}
				death = { death_reason = death_slavery }
			}
			primary_title = {
				activate_title = { title = THIS status = no }
				destroy_landed_title = THIS
			}
			death = { death_reason = death_slavery } 
		}
		any_player = { 
				limit = {
				religion_group = christian
				NOT = {
					character = ROOT
				}
			}
			narrative_event = { id = HF.25823 days = 7 } #Notify everyone that the crusade has flopped.
		}
	}

	on_add = {
		hidden_tooltip = { fire_haruspicy_event_effect = yes }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
		ROOT = { 
			add_trait = crusader 
			diplomatic_immunity = no 
		}
		any_player = { 
				limit = {
				religion_group = FROM
				NOT = {
					character = ROOT
				}
			}
			narrative_event = { id = HF.25824 } #Notify infidels that the children have arrived.
		}

	}

	on_success_title = {
		ROOT = {
			hidden_tooltip = {
				if = {
					limit = {
						this_has_super_cool_nickname_trigger = no
					}
					random_list = {
						20 = { give_nickname = nick_the_angel_of_location }
						15 = { give_nickname = nick_the_chosen_of_gabriel }
						15 = { 
							trigger = {
								is_female = no
							}
							give_nickname = nick_the_page_of_lord 
						}
						15 = {
							trigger = {
								is_female = yes
							} 
							give_nickname = nick_the_maid_of_virgin 
						}
						15 = { give_nickname = nick_the_messenger_of_christ }
						5 = { give_nickname = nick_the_sword_of_god }
					}
				}
			}
		}
		if = {
			limit = {
				k_jerusalem = { 
					holder_scope = { NOT = { religion = ROOT } } 
				}
			}
			k_jerusalem = { usurp_title_plus_barony_if_unlanded = { target = ROOT type = holy_war } }
		}
		if = {
			limit = {
				k_jerusalem = { 
					has_holder = no
				}
			}
			k_jerusalem = {
				grant_title = ROOT
				hidden_tooltip = { make_primary_title = yes } 
				add_law = succ_primogeniture
				add_law = cognatic_succession
			}
		}
		if = {
			limit = {
				c_jerusalem = { 
					holder_scope = { NOT = { religion = ROOT } } 
				}
			}
			c_jerusalem = { 
				usurp_title_plus_barony_if_unlanded = { target = ROOT type = holy_war } 
				religion = ROOT
			}
		}
		hidden_tooltip = {
			ROOT = { 
				clr_character_flag = flag_childrens_crusade_leader
				remove_character_modifier = children_crusade_morale_0
				remove_character_modifier = children_crusade_morale_1
				remove_character_modifier = children_crusade_morale_2
				remove_character_modifier = children_crusade_morale_3
				remove_character_modifier = children_crusade_morale_4
				remove_character_modifier = voice_of_jesus #Your work here is done.
				if = { 
					limit = { 
						is_female = no
					} 
					create_bloodline = { type = crusader_kid_bloodline }  
				}
				if = { 
					limit = { 
						is_female = yes
					} 
					create_bloodline = { 
						type = crusader_kid_bloodline 
						inheritance = matrilineal
					}  
				}
				set_bloodline_founder_religion_flag_effect = yes
				any_player = { 
					limit = {
						religion_group = christian
						NOT = {
							character = ROOT
						}
					}
					narrative_event = { id = HF.25828 } #Notify everyone that the crusade has succeeded.
				}
			}
			
			
			# Vassalize and convert all infidels under the title.
				ROOT = {
					vassalize_or_take_under_title = {
						title = PREV
						enemy = FROM
						is_religious = yes
						type = holy_war
					}
					if  = {
						limit = {
							primary_title = { is_primary_type_title = no } # Mercs, the Pope, Holy Orders, etc
							PREV = { 
								OR = {
									has_holder = no 
									holder_scope = {
										top_liege = { character = FROM }
									}
								}
							}
							NOT = { character = ROOT }
							OR = {
								independent = yes
								liege = {
									tier = EMPEROR
								}
							}
						}
						usurp_title_only = { target = PREV type = holy_war }
						
						if = {
							limit = {
								NOT = { culture = PREV }
							}
							hidden_tooltip = { 
								save_event_target_as = winning_crusader
								PREV = { conquest_culture = event_target:winning_crusader } 
							}
						}
					}
					remove_trait = disinherited 
					any_courtier = { 
						remove_trait = disinherited
						clr_character_flag = no_court_invites
					}
					if = {
						limit = {
							is_female = no
						}
						remove_trait = crusader
						add_trait = crusader_king
					}
					if = {
						limit = {
							is_female = yes
						}
						remove_trait = crusader
						add_trait = crusader_queen
					}
				}
			any_playable_ruler = { 
				limit = {
					religion_group = FROM
					is_within_diplo_range = FROM
					NOT = {
						character = ROOT
					}
				}
				narrative_event = { id = HF.25827 } #Notify all infidels that the crusade has succeeded. Offer chance of conversion/vassalization if within k_jerusalem.
			}
		}
		
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_prestige = 500
				participation_scaled_piety = 1000
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -100
				}
			}
		}
		any_attacker = {
			limit = {
				ai = no
				NOT = { has_character_flag = achievement_crusader }
			}
			set_character_flag = achievement_crusader
		}
		
		ROOT = {
			participation_scaled_prestige = 500
			participation_scaled_piety = 1000
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -100
			}
			religion_authority = {
				modifier = won_crusade
				years = 20
			}
		}
		
		FROM = {
			piety = -100
			religion_authority = {
				modifier = lost_crusade
				years = 20
			}			
		}
	}

	on_reverse_demand = {
		ROOT = {
			hidden_tooltip = {
				clr_character_flag = flag_childrens_crusade_leader
				remove_character_modifier = children_crusade_morale_0
				remove_character_modifier = children_crusade_morale_1
				remove_character_modifier = children_crusade_morale_2
				remove_character_modifier = children_crusade_morale_3
				remove_character_modifier = children_crusade_morale_4
			}
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -200
			religion_authority = {
				modifier = lost_crusade
				years = 20
			}
			any_courtier = { 
				limit = {
					is_ruler = no
					is_landed = no
					liege = { character = ROOT }
				}
				death = { death_reason = death_slavery }
			}
			primary_title = {
				activate_title = { title = THIS status = no }
				destroy_landed_title = THIS
			}
			death = { death_reason = death_slavery }
		}
		FROM = {
			piety = 100
			religion_authority = {
				modifier = won_crusade
				years = 20
			}
			participation_scaled_piety = 300
			participation_scaled_prestige = 150
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -100
			}
			if = {
				limit = { 
					has_dlc = "Legacy of Rome"
					OR = { 
						has_landed_title = e_byzantium
						has_landed_title = e_roman_empire
					}
					OR = { 
						religion_group = christian
						religion = hellenic_pagan
						religion = hellenic_pagan_reformed
						religion = promethean_pagan
						religion = titan_pagan
					}
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
		}
		any_player = { 
			limit = {
				religion_group = christian
				NOT = {
					character = ROOT
				}
			}
			narrative_event = { id = HF.25825 } #Notify everyone that the crusade has failed.
		}
		any_player = { 
			limit = {
				religion_group = FROM
				NOT = {
					character = ROOT
				}
			}
			narrative_event = { id = HF.25826 } #Notify every pagan that the crusade has failed.
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_piety = 300
				participation_scaled_prestige = 150
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -100
				}
			}
		}
		any_attacker = {
			limit = {
				character = ROOT
				uses_decadence = yes
				NOT = {
					any_liege = {
						dynasty = PREV
					}
				}
			}
			participation_scaled_decadence = 35
		}
		any_attacker = {
			limit = {
				NOT = { character = ROOT }
				uses_decadence = yes
				NOT = {
					any_liege = {
						dynasty = PREV
					}
				}
			}
			hidden_tooltip = { 
				participation_scaled_decadence = 35
			}
		}
	}

	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
	}
}

#Warmongering + PMatriarchy = Sex Deposition cb
#Matriarchal version
matriarchal_deposition_war = { 
	name = CB_NAME_MATRIARCHAL_DEPOSITION
	war_name = WAR_NAME_SEX_DEPOSITION
	sprite = 35
	truce_days = 3650
	is_permanent = yes
	can_ask_to_join_war = yes
	is_holy_war = yes

	allowed_to_target_tributaries = no
	
	sort_priority = 4000
	
	on_add = {
		hidden_tooltip = { fire_haruspicy_event_effect = yes }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes
			}
		}
	}

	can_use = {
		has_dlc = "Holy Fury"
		ROOT = {
			higher_real_tier_than = DUKE
			has_religion_feature = religion_warlike
			is_female = yes
			has_religion_feature = religion_matriarchal
			is_landed = yes
			is_nomadic = no
			mercenary = no
			NOT = { same_realm = FROM }
			top_liege = {
				religion = ROOT
			}
			any_neighbor_independent_ruler = {
				character = FROM
			}
		}
		FROM = {
			is_nomadic = no
			is_merchant_republic = no
			is_theocracy = no
			is_female = no 
			independent = yes
		}
	}

	is_valid = {
		ROOT = {
			higher_real_tier_than = DUKE
			has_religion_feature = religion_warlike
			is_female = yes
			has_religion_feature = religion_matriarchal
			FROM = { is_female = no independent = yes }
			NOT = { same_realm = FROM }
		}
		FROM = {
			is_nomadic = no
			is_merchant_republic = no
			is_theocracy = no
		}
	}
	
	on_success = {
		hidden_tooltip = { 
			any_attacker = {
				limit = { NOT = { character = ROOT } }
				
				participation_scaled_piety = 50
				participation_scaled_prestige = 25
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
		}
		
		ROOT = {
			participation_scaled_piety = 100
			participation_scaled_prestige = 150
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
			religion_authority = {
				modifier = won_holy_war
				years = 20
			}
		}
		FROM = { piety = -100 }
		FROM = { prestige = -50 }
		custom_tooltip = {
			text = sex_deposition_cb_succ_tip
			hidden_tooltip = {
				FROM = {
					#If dynasty member of proper sex exists, abdicate to it.
					#Else lose main title to vassal.
					#Else lose main title to spawned character.

					#Then, if religious dogma allows it, switch status of Women.
					#Then, if title passed on a spawned character, change succession laws.
					#Finally, if owning Jade Dragon set up permanent Tributary, if Horse Lords set up temporary tributary, else set up non-aggression pact.
					primary_title = { save_event_target_as = title_usurped }
					save_event_target_as = scoped_loser
					if = {
						limit = {
							any_dynasty_member = {
								is_female = yes
								NOT = { trait = incapable }
								NOT = { has_religion_feature = religion_patriarchal }
							}
						}
						random_dynasty_member = {
							limit = {
								is_female = yes
								NOT = { trait = incapable }
								NOT = { has_religion_feature = religion_patriarchal }
							}
							preferred_limit = {
								is_child_of = PREV
								is_adult = yes
								religion = ROOT
							}
							preferred_limit = {
								is_close_relative = PREV
								is_adult = yes
								religion = ROOT
							}
							preferred_limit = {
								is_adult = yes
								religion = ROOT
							}
							preferred_limit = {
								is_close_relative = PREV
								is_adult = yes
							}
							preferred_limit = {
								is_adult = yes
							}
							save_event_target_as = new_swapped_ruler
							usurp_title = { target = event_target:title_usurped type = invasion }
							event_target:scoped_loser = {
								any_demesne_title = {
									limit = {
										tier = PREVPREV
									}
									event_target:new_swapped_ruler = { usurp_title = { target = PREV type = invasion } }
									add_pressed_claim = PREV
								}
							}
							event_target:scoped_loser = { set_defacto_liege = event_target:new_swapped_ruler } 
						}
					}
					else_if = {
						limit = {
							any_vassal = {
								is_female = yes
								NOT = { trait = incapable }
								NOT = { has_religion_feature = religion_patriarchal }
							}
						}
						random_vassal = {
							limit = {
								is_female = yes
								NOT = { trait = incapable }
								NOT = { has_religion_feature = religion_patriarchal }
							}
							preferred_limit = {
								religion = ROOT
								is_adult = yes
								NOT = { age = 30 }
								trait = zealous
							}
							preferred_limit = {
								religion = ROOT
								is_adult = yes
								trait = zealous
							}
							preferred_limit = {
								religion = ROOT
								is_adult = yes
							}
							preferred_limit = {
								is_adult = yes
								has_religion_feature = religion_matriarchal
							}
							preferred_limit = {
								is_adult = yes
							}
							save_event_target_as = new_swapped_ruler
							usurp_title = { target = event_target:title_usurped type = invasion }
							event_target:scoped_loser = {
								any_demesne_title = {
									limit = {
										tier = PREVPREV
									}
									event_target:new_swapped_ruler = { usurp_title = { target = PREV type = invasion } }
									add_pressed_claim = PREV
								}
							}
							event_target:scoped_loser = { set_defacto_liege = event_target:new_swapped_ruler } 
						}
					}
					else = {
						location = {
							create_character = {
								religion = ROOT
								culture = event_target:scoped_loser
								dynasty = random
								random_traits = yes
								female = yes
								age = 23
								health = 7
								fertility = 0.7
								attributes = {
									martial = 8
									diplomacy = 8
									stewardship = 8
								}
							}
							new_character = {
								remove_trait = cynical
								wealth = 250
								prestige = 300
								piety = 100
								save_event_target_as = new_swapped_ruler
								usurp_title = { target = event_target:title_usurped type = invasion }
								event_target:scoped_loser = {
									any_demesne_title = {
										limit = {
											tier = PREVPREV
										}
										event_target:new_swapped_ruler = { usurp_title = { target = PREV type = invasion } }
										add_pressed_claim = PREV
									}
								}
								event_target:scoped_loser = { set_defacto_liege = event_target:new_swapped_ruler }
							}
						}
					}

					#Titles assigned, now onto laws.
					if = {
						limit = {
							ROOT = {
								has_religion_feature = religion_matriarchal
							}
							event_target:new_swapped_ruler = {
								NOR = {
									has_religion_feature = religion_patriarchal
									has_religion_feature = religion_equal
									has_religion_feature = religion_feature_bon
								}
							}
						}
						event_target:new_swapped_ruler = {
							any_demesne_title = { 
								add_law = {
									law = enatic_succession
									cooldown = no
									opinion_effect = no
								}
								add_law = {
									law = status_of_women_4
									cooldown = no
									opinion_effect = no
								}
							} 
							recalc_succession = yes
						}
					}

					#Laws set, now onto pacts.
					if = {
						limit = {
							has_dlc = "Jade Dragon"
							is_tributary = no
						}
						ROOT = {
							make_tributary = { who = event_target:new_swapped_ruler tributary_type = permanent }
						}
					}
					else_if = {
						limit = {
							has_dlc = "Horse Lords"
							is_tributary = no
						}
						ROOT = {
							make_tributary = { who = event_target:new_swapped_ruler percentage = 0.3 }
						}
					}
					else = {
						ROOT = {
							opinion = { 
								modifier = in_non_aggression_pact
								years = 50
								who = event_target:new_swapped_ruler
							}
						}
					}
				}
			}
		}
		if = {
			limit = {
				OR = {
					has_dlc = "Horse Lords"
					has_dlc = "Jade Dragon"
				}
				is_tributary = no
			}
			custom_tooltip = {
				text = sex_deposition_cb_succ_tip_2
			}
		}
	}

	on_fail = {
		ROOT = { 
			piety = -125 
			prestige = -250
		}
	}

	on_reverse_demand = {
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -250
			prestige = -500
		}
		FROM = {
			participation_scaled_piety = 200
			participation_scaled_prestige = 100
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_piety = 200
				participation_scaled_prestige = 100
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
				hidden_tooltip = {
					if = {
						limit = {
							has_ambition = obj_strengthen_religion
						}
						change_variable = { which = strengthen_religion value = 1 }
					}
				}
			}
		}
		any_attacker = {
			limit = {
				character = ROOT
				uses_decadence = yes
			}
			participation_scaled_decadence = 20
		}
		any_attacker = {
			limit = {
				NOT = { character = ROOT }
				uses_decadence = yes
			}
			hidden_tooltip = { 
				participation_scaled_decadence = 20
			}
		}
		ROOT = { 
			religion_authority = {
				modifier = lost_holy_war
				years = 20
			}
		}
		FROM = { 
			if = {
				limit = { NOT = { has_religion_feature = religion_matriarchal } }
				religion_authority = {
					modifier = won_holy_war
					years = 20
				}
			}
			hidden_tooltip = {
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}			
		}
	}

	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 = 1
		modifier = { #Focus on Coronation first.
			factor = 0.1
			has_dlc = "Holy Fury"
			ROOT = {
				is_feudal = yes
				OR = {
					religion = catholic
					religion = fraticelli
				}
				NOR = {
					trait = crowned_by_priest
					trait = crowned_by_bishop
					trait = crowned_by_pope
					trait = crowned_by_myself
				}
			}
		}
		modifier = { 
			factor = 10
			ROOT = { religion = FROM } #Intolerable circumstances.
		}
		modifier = { 
			factor = 5
			ROOT = { has_religion_feature = religion_patriarchal }
			FROM = { has_religion_feature = religion_matriarchal }
		}
		modifier = { 
			factor = 5
			ROOT = { has_religion_feature = religion_matriarchal }
			FROM = { has_religion_feature = religion_patriarchal }
		}
	}
}

#Patriarchal version
patriarchal_deposition_war = { 
	name = CB_NAME_PATRIARCHAL_DEPOSITION
	war_name = WAR_NAME_SEX_DEPOSITION
	sprite = 35
	truce_days = 3650
	is_permanent = yes
	can_ask_to_join_war = yes
	is_holy_war = yes

	allowed_to_target_tributaries = no
	
	sort_priority = 4000
	
	on_add = {
		hidden_tooltip = { fire_haruspicy_event_effect = yes }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes
			}
		}
	}

	can_use = {
		has_dlc = "Holy Fury"
		ROOT = {
			higher_real_tier_than = DUKE
			has_religion_feature = religion_warlike
			is_female = no
			has_religion_feature = religion_patriarchal
			FROM = { is_female = yes independent = yes }
			is_landed = yes
			is_nomadic = no
			mercenary = no
			NOT = { same_realm = FROM }
			top_liege = {
				religion = ROOT
			}
			any_neighbor_independent_ruler = {
				character = FROM
			}
		}
		FROM = {
			is_nomadic = no
			is_merchant_republic = no
			is_theocracy = no
		}
	}

	is_valid = {
		ROOT = {
			higher_real_tier_than = DUKE
			has_religion_feature = religion_warlike
			is_female = no
			has_religion_feature = religion_patriarchal
			NOT = { same_realm = FROM }
		}
		FROM = {
			is_nomadic = no
			is_merchant_republic = no
			is_theocracy = no
			is_female = yes 
			independent = yes
		}
	}
	
	on_success = {
		hidden_tooltip = { 
			any_attacker = {
				limit = { NOT = { character = ROOT } }
				
				participation_scaled_piety = 50
				participation_scaled_prestige = 25
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
		}
		
		ROOT = {
			participation_scaled_piety = 100
			participation_scaled_prestige = 150
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
			religion_authority = {
				modifier = won_holy_war
				years = 20
			}
		}
		FROM = { piety = -100 }
		FROM = { prestige = -50 }
		custom_tooltip = {
			text = sex_deposition_cb_succ_tip
			hidden_tooltip = {
				FROM = {
					#If dynasty member of proper sex exists, abdicate to it.
					#Else lose main title to vassal.
					#Else lose main title to spawned character.

					#Then, if religious dogma allows it, switch status of Women.
					#Then, if title passed on a spawned character, change succession laws.
					#Finally, if owning Jade Dragon set up permanent Tributary, if Horse Lords set up temporary tributary, else set up non-aggression pact.
					primary_title = { save_event_target_as = title_usurped }
					save_event_target_as = scoped_loser
					if = {
						limit = {
							any_dynasty_member = {
								is_female = no
								NOT = { trait = incapable }
								NOT = { has_religion_feature = religion_matriarchal }
							}
						}
						random_dynasty_member = {
							limit = {
								is_female = no
								NOT = { trait = incapable }
								NOT = { has_religion_feature = religion_matriarchal }
							}
							preferred_limit = {
								is_child_of = PREV
								is_adult = yes
								religion = ROOT
							}
							preferred_limit = {
								is_close_relative = PREV
								is_adult = yes
								religion = ROOT
							}
							preferred_limit = {
								is_adult = yes
								religion = ROOT
							}
							preferred_limit = {
								is_close_relative = PREV
								is_adult = yes
							}
							preferred_limit = {
								is_adult = yes
							}
							save_event_target_as = new_swapped_ruler
							usurp_title = { target = event_target:title_usurped type = invasion }
							event_target:scoped_loser = {
								any_demesne_title = {
									limit = {
										tier = PREVPREV
									}
									event_target:new_swapped_ruler = { usurp_title = { target = PREV type = invasion } }
									add_pressed_claim = PREV
								}
							}
							event_target:scoped_loser = { set_defacto_liege = event_target:new_swapped_ruler } 
						}
					}
					else_if = {
						limit = {
							any_vassal = {
								is_female = no
								NOT = { trait = incapable }
								NOT = { has_religion_feature = religion_matriarchal }
							}
						}
						random_vassal = {
							limit = {
								is_female = no
								NOT = { trait = incapable }
								NOT = { has_religion_feature = religion_matriarchal }
							}
							preferred_limit = {
								religion = ROOT
								is_adult = yes
								NOT = { age = 30 }
								trait = zealous
							}
							preferred_limit = {
								religion = ROOT
								is_adult = yes
								trait = zealous
							}
							preferred_limit = {
								religion = ROOT
								is_adult = yes
							}
							preferred_limit = {
								is_adult = yes
								has_religion_feature = religion_patriarchal
							}
							preferred_limit = {
								is_adult = yes
							}
							save_event_target_as = new_swapped_ruler
							usurp_title = { target = event_target:title_usurped type = invasion }
							event_target:scoped_loser = {
								any_demesne_title = {
									limit = {
										tier = PREVPREV
									}
									event_target:new_swapped_ruler = { usurp_title = { target = PREV type = invasion } }
									add_pressed_claim = PREV
								}
							}
							event_target:scoped_loser = { set_defacto_liege = event_target:new_swapped_ruler } 
						}
					}
					else = {
						location = {
							create_character = {
								religion = ROOT
								culture = event_target:scoped_loser 
								dynasty = random
								random_traits = yes
								female = no
								age = 23
								health = 7
								fertility = 0.7
								attributes = {
									martial = 8
									diplomacy = 8
									stewardship = 8
								}
							}
							new_character = {
								remove_trait = cynical
								wealth = 250
								prestige = 300
								piety = 100
								save_event_target_as = new_swapped_ruler
								usurp_title = { target = event_target:title_usurped type = invasion }
								event_target:scoped_loser  = {
									any_demesne_title = {
										limit = {
											tier = PREVPREV
										}
										event_target:new_swapped_ruler = { usurp_title = { target = PREV type = invasion } }
										add_pressed_claim = PREV
									}
								}
								event_target:scoped_loser  = { set_defacto_liege = event_target:new_swapped_ruler }
							}
						}
					}

					#Titles assigned, now onto laws.
					if = {
						limit = {
							ROOT = {
								has_religion_feature = religion_patriarchal
							}
							event_target:new_swapped_ruler = {
								NOR = {
									has_religion_feature = religion_matriarchal
									has_religion_feature = religion_equal
									has_religion_feature = religion_feature_bon
								}
							}
						}
						event_target:new_swapped_ruler = {
							any_demesne_title = { 
								add_law = {
									law = agnatic_succession
									cooldown = no
									opinion_effect = no
								}
								add_law = {
									law = status_of_women_0
									cooldown = no
									opinion_effect = no
								}
							} 
							recalc_succession = yes
						}
					}

					#Laws set, now onto pacts.
					if = {
						limit = {
							has_dlc = "Jade Dragon"
							is_tributary = no
						}
						ROOT = {
							make_tributary = { who = event_target:new_swapped_ruler tributary_type = permanent }
						}
					}
					else_if = {
						limit = {
							has_dlc = "Horse Lords"
							is_tributary = no
						}
						ROOT = {
							make_tributary = { who = event_target:new_swapped_ruler percentage = 0.3 }
						}
					}
					else = {
						ROOT = {
							opinion = { 
								modifier = in_non_aggression_pact
								years = 50
								who = event_target:new_swapped_ruler
							}
						}
					}
				}
			}
		}
		if = {
			limit = {
				OR = {
					has_dlc = "Horse Lords"
					has_dlc = "Jade Dragon"
				}
				is_tributary = no
			}
			custom_tooltip = {
				text = sex_deposition_cb_succ_tip_2
			}
		}
	}

	on_fail = {
		ROOT = { 
			piety = -125 
			prestige = -250
		}
	}

	on_reverse_demand = {
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -250
			prestige = -500
		}
		FROM = {
			participation_scaled_piety = 200
			participation_scaled_prestige = 100
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_piety = 200
				participation_scaled_prestige = 100
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
				hidden_tooltip = {
					if = {
						limit = {
							has_ambition = obj_strengthen_religion
						}
						change_variable = { which = strengthen_religion value = 1 }
					}
				}
			}
		}
		any_attacker = {
			limit = {
				character = ROOT
				uses_decadence = yes
			}
			participation_scaled_decadence = 20
		}
		any_attacker = {
			limit = {
				NOT = { character = ROOT }
				uses_decadence = yes
			}
			hidden_tooltip = { 
				participation_scaled_decadence = 20
			}
		}
		ROOT = { 
			religion_authority = {
				modifier = lost_holy_war
				years = 20
			}
		}
		FROM = { 
			if = {
				limit = { NOT = { has_religion_feature = religion_patriarchal } }
				religion_authority = {
					modifier = won_holy_war
					years = 20
				}
			}
			hidden_tooltip = {
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}			
		}
	}

	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 = 1
		modifier = { #Focus on Coronation first.
			factor = 0.1
			has_dlc = "Holy Fury"
			ROOT = {
				is_feudal = yes
				OR = {
					religion = catholic
					religion = fraticelli
				}
				NOR = {
					trait = crowned_by_priest
					trait = crowned_by_bishop
					trait = crowned_by_pope
					trait = crowned_by_myself
				}
			}
		}
		modifier = { 
			factor = 10
			ROOT = { religion = FROM } #Intolerable circumstances.
		}
		modifier = { 
			factor = 5
			ROOT = { has_religion_feature = religion_patriarchal }
			FROM = { has_religion_feature = religion_matriarchal }
		}
		modifier = { 
			factor = 5
			ROOT = { has_religion_feature = religion_matriarchal }
			FROM = { has_religion_feature = religion_patriarchal }
		}
	}
}

# Impious Saint induced Catholic Civil War
depose_impious_pope = {
	name = CB_NAME_DEPOSE_IMPIOUS_POPE
	war_name = WAR_NAME_DEPOSE_IMPIOUS_POPE
	sprite = 29
	truce_days = 0
	is_permanent = yes
	allow_whitepeace = no
	
	defender_unoccupied_warscore = yes
	
	allowed_to_target_tributaries = no
	
	sort_priority = 750
	
	can_use = {
		always = no #Can only be initiated through special means
	}
	
	is_valid = {
		FROM = {
			trait = bad_priest_christian
			hidden_tooltip = {
				religion = catholic
				has_landed_title = k_papal_state
			}
			independent = yes
		}
	}

	on_success = {
		custom_tooltip = {
			text = depose_impious_pope_cb_win_TT
			any_attacker = {
				piety = 100
			}
		}
		FROM = {
			show_scope_change = no
			abdicate = yes
		}

		FROM = {
			show_scope_change = no
			religion_authority = {
				modifier = depose_impious_pope_modifier
			}
		}

		hidden_tooltip = {
			any_defender = {
				set_character_flag = impious_papal_defender
			}
			any_playable_ruler = {
				limit = {
					religion = catholic
				}
				narrative_event = { id = HF.40094 }
			}
			ROOT = {
				narrative_event = { id = HF.40094 }
			}
			k_papal_state = {
				owner = {
					save_event_target_as = current_pope
					reverse_imprison = FROM
				}
			}
			any_attacker = {
				reverse_opinion = {
					modifier = opinion_grateful
					who = event_target:current_pope
					years = 10
				}
			}
		}
	}

	on_reverse_demand = {
		custom_tooltip = {
			text = depose_impious_pope_cb_lose_TT
			any_attacker = {
				add_trait = excommunicated
				piety = -100
			}
		}

		FROM = {
			show_scope_change = no
			religion_authority = {
				modifier = failed_to_depose_impious_pope_modifier
			}
		}

		hidden_tooltip = {
			any_defender = {
				set_character_flag = impious_papal_defender
			}
			any_playable_ruler = {
				limit = {
					religion = catholic
				}
				narrative_event = { id = HF.40095 }
			}
			ROOT = {
				narrative_event = { id = HF.40095 }
			}
		}

	}

	on_attacker_leader_death = {
		if = { limit = { has_global_flag = pre_catholic_civil_war }
			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
	}
}

# Shepherds' Crusade
shepherds_crusade_invasion = {
	name = CB_NAME_SHEPHERDS_CRUSADE
	war_name = WAR_NAME_SHEPHERDS_CRUSADE
	sprite = 9
	truce_days = 0
	check_de_jure_tier = KING
	is_permanent = yes
	allow_whitepeace = no
	can_ask_to_join_war = yes

	is_holy_war = yes

	defender_unoccupied_warscore = yes

	allowed_to_target_tributaries = yes

	can_use = {
		always = no # Can only be initiated through special means
	}
	is_valid = {
		FROM = {
			NOT = {
				religion = ROOT
			}
		}
	}

	on_attacker_leader_death = {
		any_playable_ruler = {
			narrative_event = { id = HFP.42310 }
		}
	}

	on_success_title = {
		save_event_target_as = shepherds_kingdom
		if = {
			limit = {
				OR = {
					NOT = {
						owner = {
							always = yes
						}
					}
					owner = {
						character = FROM
					}
					owner = {
						top_liege = {
							character = FROM
						}
					}
				}
			}
			gain_title = ROOT
		}
		else = {
			create_title = {
				tier = KING
				name = "KINGDOM_OF_THE_SHEPHERDS"
				holder = ROOT
				culture = ROOT
				base_title = event_target:shepherds_kingdom
			}
			add_claim = ROOT
		}
		ROOT = {
			gain_settlements_under_title = {
				title = PREV
				enemy = FROM
				is_crusade = yes # Even if the title holder is not participating in the war, gain holdings occupied by all Crusade participants
				type = holy_war
			}
			clr_character_flag = shepherds_leader
			set_defacto_liege = ROOT
			hidden_tooltip = {
				dynasty = father_bastard
				if = {
					limit = {
						is_female = no
					}
					add_trait = crusader_king
					create_bloodline = {
						type = crusader_commander
					}
				}
				if = {
					limit = {
						is_female = yes
					}
					add_trait = crusader_queen
					create_bloodline = {
						type = crusader_commander
						inheritance = matrilineal
					}
				}
				set_bloodline_founder_religion_flag_effect = yes
				event_target:shepherds_kingdom = {
					capital_scope = {
						ROOT = {
							capital = PREV
						}
						save_event_target_as = shepherds_capital
						DUCHY = {
							save_event_target_as = shepherds_duchy
						}
					}
				}
				primary_title = {
					add_law = {
						law = succ_primogeniture
						cooldown = no
						opinion_effect = no
					}
					add_law = {
						law = centralization_2
						cooldown = no
						opinion_effect = no
					}
					add_law = {
						law = cognatic_succession
						cooldown = no
						opinion_effect = no
					}
					if = {
						limit = {
							has_dlc = "Zeus"
						}
						add_law = {
							law = ze_administration_laws_1
							cooldown = no
							opinion_effect = no
						}
						add_law = {
							law = out_of_realm_inheritance_law_1
							cooldown = no
							opinion_effect = no
						}
					}
					else_if = {
						limit = {
							NOT = { has_dlc = "Zeus" }
						}
						add_law = {
							law = crown_authority_3
							cooldown = no
							opinion_effect = no
						}
					}
				}
				
				character_event = { id = HFP.41090 days = 1 }
				character_event = { id = HFP.41090 days = 3 }

				if = {
					limit = {
						NOT = {
							wealth = 200
						}
					}
					clear_wealth = yes
					wealth = 200
				}

				any_demesne_title = {
					limit = {
						OR = {
							NOT = {
								location = {
									DUCHY = {
										title = event_target:shepherds_duchy
									}
								}
							}
							holding_type = CITY
							holding_type = TEMPLE
						}
					}
					if = {
						limit = {
							holding_type = CITY
						}
						create_random_steward = {
							random_traits = yes
							dynasty = random
						}
					}
					else_if = {
						limit = {
							holding_type = TEMPLE
						}
						create_random_priest = {
							random_traits = yes
							dynasty = random
						}
					}
					else_if = {
						limit = {
							holding_type = CASTLE
						}
						create_random_soldier = {
							random_traits = yes
							dynasty = random
						}
					}
					grant_title_no_opinion = new_character
					new_character = {
						add_trait = crusader
						add_trait = brave
						ROOT = {
							set_truce = {
								who = PREV
								years = 0
							}
						}
					}
				}
				event_target:shepherds_kingdom = {
					any_direct_de_jure_vassal_title = {
						random_direct_de_jure_vassal_title = {
							limit = {
								owner = {
									top_liege = {
										character = ROOT
									}
								}
							}
							owner = {
								gain_title = PREVPREV
								primary_title = {
									any_direct_de_jure_vassal_title = {
										limit = {
											owner = {
												top_liege = {
													character = ROOT
												}
											}
										}
										owner = {
											set_defacto_liege = PREVPREVPREV
										}
									}
								}
								set_defacto_liege = ROOT
							}
						}
					}
				}
				any_claim = {
					remove_claim = PREV
				}
			}
			if = {
				limit = {
					can_apply_average_nickname_trigger = yes
				}
				random_list = {
					10 = { give_nickname = nick_the_crusader }
					10 = { give_nickname = nick_the_holy }
					10 = { give_nickname = nick_the_pilgrim }
					10 = { give_nickname = nick_the_protector }
					10 = { give_nickname = nick_the_guardian }
					40 = { give_nickname = nick_the_sword_of_god }
				}
			}
		}
		any_playable_ruler = {
			narrative_event = { id = HFP.42309 }
		}

		ROOT = {
			check_if_reconquista_finished_effect = yes
		}
	}
	on_reverse_demand = {
		FROM = {
			piety = 200
			reverse_imprison = ROOT
		}
		any_attacker = {
			transfer_scaled_wealth = {
				to = FROM
				value = 1.0
			}
			piety = -100
		}
		any_playable_ruler = {
			narrative_event = { id = HFP.42308 }
		}
	}

	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
	}
}

# Northern Crusade
northern_crusade_war = {
	name = CB_NAME_NORTHERN_CRUSADE
	war_name = WAR_NAME_NORTHERN_CRUSADE
	sprite = 9
	truce_days = 1825
	is_permanent = yes
	is_holy_war = yes
	check_de_jure_tier = DUKE
	allow_whitepeace = no
	can_ask_to_join_war = yes

	defender_unoccupied_warscore = yes

	allowed_to_target_tributaries = yes

	can_use_gui = {
		ROOT = {
			prisoner = no
			is_adult = yes
		}
	}

	can_use = {
		ROOT = {
			OR = {
				has_landed_title = d_teutonic_order
				has_landed_title = k_teutonic_state
			}
		}
		FROM = {
			NOT = {
				religion_group = ROOT
			}
		}
	}
	can_use_title = {
		kingdom = {
			has_title_flag = northern_crusade_active_target
		}
	}
	is_valid = {
		FROM = {
			NOT = {
				religion = ROOT
			}
		}
	}
	on_success = {
		any_attacker = {
			limit = {
				NOR = {
					has_landed_title = d_teutonic_order
					has_landed_title = k_teutonic_state
				}
			}
			piety = 150
		}
		any_defender = {
			piety = -100
		}
	}
	on_success_title = {
		save_event_target_as = northern_crusade_title

		ROOT = {
			vassalize_or_take_under_title = {
				title = PREV
				enemy = FROM
				same_religion = yes # Only vassalize rulers of my religion
				is_religious = yes
				type = holy_war
			}

			hidden_tooltip = {
				# If the Teutonic Order don't have a base to work out from, get one
				if = {
					limit = {
						capital_scope = {
							duchy = {
								title = PREVPREVPREV
							}
						}
					}
					teutonic_order_upgrade_capital_effect = yes
					holy_order_distribute_titles_effect = yes
					character_event = { id = HFP.41090 days = 1 }
					character_event = { id = HFP.41090 days = 3 }
				}
			
				# Become the Teutonic State, if they aren't already
				if = {
					limit = {
						NOT = {
							has_landed_title = k_teutonic_state
						}
					}
					k_teutonic_state = {
						copy_title_history = d_teutonic_order
						copy_title_laws = d_teutonic_order
					}
					grant_title_no_opinion = k_teutonic_state
					unsafe_destroy_landed_title = d_teutonic_order
					activate_title = { title = d_teutonic_order status = no }
				}

				## Rewards
				# Check who to give artifacts
				any_attacker = {
					limit = {
						NOT = {
							character = ROOT
						}
						any_war = {
							using_cb = northern_crusade_war
							war_participation = {
								who = PREV
								score > 0.4
							}
						}
					}
					set_character_flag = northern_crusade_artifact_reward
				}
				# Check who to give money
				any_attacker = {
					limit = {
						NOT = {
							character = ROOT
						}
						any_war = {
							using_cb = northern_crusade_war
							war_participation = {
								who = PREV
								score > 0.2
							}
						}
					}
					set_character_flag = northern_crusade_gold_reward
				}
				# Check who to give artifacts
				any_attacker = {
					limit = {
						NOT = {
							character = ROOT
						}
						any_war = {
							using_cb = northern_crusade_war
							war_participation = {
								who = PREV
								score > 0.01
							}
						}
					}
					letter_event = { id = HF.49112 days = 1 }
				}
				# Checking for Bloodline
				most_participating_attacker = {
					if = {
						limit = {
							NOT = {
								character = ROOT
							}
						}

						if = {
							limit = {
								check_variable = { 
									which = northern_crusade_ally 
									value == 1 
								}
							}
							set_variable = {
								which = northern_crusade_ally 
								value = 2
							}
						}
						else_if = {
							limit = {
								check_variable = {
									which = northern_crusade_ally 
									value = 2
								}
							}
							character_event = { id = HF.49114 }
						}
						else = {
							set_variable = {
								which = northern_crusade_ally
								value = 1 
							}
						}
					}
				}

				## Misc
				# Check if the Northern Crusade is over
				if = {
					limit = {
						PREV = {
							kingdom = {
								has_title_flag = northern_crusade_active_target
								NOT = {
									any_direct_de_jure_vassal_title = {
										any_direct_de_jure_vassal_title = {
											owner = {
												NOT = {
													religion_group = christian
												}
											}
										}
									}
								}
							}
						}
					}
					PREV = {
						kingdom = {
							save_event_target_as = northern_crusade_kingdom
							clr_title_flag = northern_crusade_active_target
						}
					}
					religion_authority = {
						modifier = northern_crusade_won
					}
					ROOT = {
						narrative_event = { id = HF.49115 }
					}
					any_playable_ruler = {
						narrative_event = { id = HF.49115 }
					}
					set_global_flag = northern_crusade_success
					clr_global_flag = northern_crusades_active
				}
			}
		}
	}
	on_reverse_demand = {
	
		ROOT = {
			any_attacker = {
				piety = -100
			}
		}

		FROM = {
			any_defender = {
				piety = 150
			}
		}

		hidden_tooltip = {
			ROOT = {
				if = {
					limit = {
						check_variable = {
							which = northernCrusadeValue
							value == 1
						}
					}
					set_variable = {
						which = northernCrusadeValue
						value = 2
					}
				}
				else_if = {
					limit = {
						check_variable = {
							which = northernCrusadeValue
							value = 2
						}
					}
					ROOT = {
						narrative_event = { id = HF.49113 }
					}
					FROM = {
						narrative_event = { id = HF.49113 }
					}
					any_playable_ruler = {
						narrative_event = { id = HF.49113 }
					}				
				}
				else = {
					set_variable = {
						which = northernCrusadeValue
						value = 1
					}
				}
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}
