# 0
claim = {
	name = CB_NAME_CLAIM
	war_name = WAR_NAME_CLAIM
	sprite = 1
	truce_days = 3650
	hostile_against_others = yes
	can_ask_to_join_war = no

	allowed_to_target_tributaries = no
	
	press_claim = yes

	infamy_modifier = 0.85
	
	sort_priority = 1000
	
	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use_title = {
		OR = {
			ROOT = { is_female = no }
			AND = { 
				NOT = { ROOT = { religion_group = muslim } }
				NOT = { ROOT = { has_religion_feature = religion_patriarchal } } 
			}
		}
		OR = {
			ROOT = { is_female = yes }
			NOT = { ROOT = { has_religion_feature = religion_matriarchal } } 
		}
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
		
		ROOT = {
			OR = {
				is_ruler = no
				primary_title = {
					OR = {
						is_primary_type_title = no # Static Mercs, the Pope, Holy Orders, etc
						higher_tier_than = PREVPREV
						tier = PREVPREV
					}
				}
			}
		}
		
		OR = { 
			ROOT = { has_strong_claim = PREV }
			ROOT = { has_character_flag = claimant_adventurer  }
			AND = {
				ROOT = { has_weak_claim = PREV }
				OR = {
					is_pretender = yes # Checks vs ROOT character
					holder_scope = { has_regent = yes }
					AND = {
						ROOT = { 
							OR = { 
								is_female = no 
								has_law = status_of_women_4
								has_religion_feature = religion_matriarchal
								has_religion_feature = religion_equal
								has_religion_feature = religion_feature_bon
							} 
						}
						holder_scope = { 
							AND = { 
								is_female = yes 
								NOR = { 
									has_religion_feature = religion_matriarchal
									has_religion_feature = religion_equal
									has_religion_feature = religion_feature_bon
									has_law = status_of_women_4 
									has_game_rule = { name = gender value = all } 
								} 
							} 
						}
					}
					is_contested = yes # Is already being contested in some form of Claim or Succession War
				}
			}
		}
	}

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

	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_title = {
		hidden_tooltip = {
			ROOT = {
				clr_character_flag = claimant_adventurer
				if = {
					limit = {
						has_nickname = no
						lower_tier_than = PREV
					}
					random = {
						chance = 50
						give_nickname = nick_the_usurper
					}
				}
			}
		}
	
		usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
		
		if = {
			limit = {
				higher_tier_than = BARON
				NOT = { culture = ROOT }
			}
			hidden_tooltip = { conquest_culture = ROOT }
		}
		
		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
		}

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

	on_fail_title = {
		ROOT = {
			show_scope_change = no
			prestige = -100
			hidden_tooltip = {
				disband_event_forces = yes
				clr_character_flag = claimant_adventurer
			}
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_reverse_demand = {
		ROOT = {
			show_scope_change = no
			prestige = -150
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		if = {
			limit = {
				ROOT = { has_character_flag = claimant_adventurer }
			}
			ROOT = { imprison = FROM }
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 150
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 150 }
		}
		hidden_effect = {
			ROOT = {
				if = {
					limit = {
						has_landed_title = e_china_west_governor
					}
					log = "China War Logging: China lost a claim war against [From.GetBestName]"
				}
				if = { #Makes China have a chance of leaving Expanionist policy...
					limit = {
						has_landed_title = e_china_west_governor
						offmap_china = {
							has_policy = china_expansionist
						}
					}
					random = {
						chance = 50
						offmap_china = {
							set_policy = china_open
							log = "-------------------------------------"
							log = "China Policy Logging:"
							log = "Chinas new Policy is now OPEN"
							log = "-------------------------------------"
						}
					}
				}
			}
		}
	}

	on_reverse_demand_title = {
		#any_claim = { #Does not work.
			#remove_claim = ROOT
		#}
		ROOT = { show_scope_change = no remove_claim = PREV }
		ROOT = {
			hidden_tooltip = {
				disband_event_forces = yes
				clr_character_flag = claimant_adventurer
			}
		}
	}
	
	on_attacker_leader_death = {
		if = {
			limit = {
				ROOT = { has_character_flag = EoD_prophet_attacker }
			}
			FROM = { narrative_event = { id = RIP.15116 } }
		}
		if = {
			limit = {
				ROOT = { 
					OR = {
						has_character_flag = claimant_adventurer
						has_character_flag = EoD_prophet_attacker
					}
				}
			}
			ROOT = {
				hidden_tooltip = {
					disband_event_forces = yes
					clr_character_flag = claimant_adventurer
					clr_character_flag = EoD_prophet_attacker
				}
			}
			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
	}
}

# 1
other_claim = {
	name = CB_NAME_CLAIM
	war_name = WAR_NAME_OTHER_CLAIM
	sprite = 2
	truce_days = 3650
	hostile_against_others = yes
	can_ask_to_join_war = no

	press_claim = yes
	
	allowed_to_target_tributaries = no

	infamy_modifier = 0.5
	
	sort_priority = 900

	can_use = {
		ROOT = {
			mercenary = no
		}
	}

	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use_title = {
		OR = {
			ROOT = { is_female = no }
			AND = {
				NOT = { ROOT = { religion_group = muslim } }
				NOT = { ROOT = { has_religion_feature = religion_patriarchal } }
				NOT = { succ_law_title = { has_law = agnatic_succession } }
			}
		}
		OR = {
			ROOT = { is_female = yes }
			AND = {
				NOT = { ROOT = { has_religion_feature = religion_matriarchal } }
				NOT = { succ_law_title = { has_law = enatic_succession } }
			}
		}
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
		
		ROOT = {
			OR = {
				is_ruler = no
				primary_title = {
					OR = {
						is_primary_type_title = no # Static Mercs, the Pope, Holy Orders, etc
						higher_tier_than = PREVPREV
						tier = PREVPREV
					}
				}
			}
		}
        
		NOT = {
			any_war = { # when used in title scope, checks all wars for that title
				attacker = {
					character = ROOT
				}
			}
		}
		
		OR = { 
			ROOT = { has_strong_claim = PREV }
			AND = {
				ROOT = { has_weak_claim = PREV }
				OR = {
					is_pretender = yes # Checks vs ROOT character
					holder_scope = { has_regent = yes }
					AND = {
						ROOT = { OR = { is_female = no  has_law = status_of_women_4 } }
						holder_scope ={ AND = { is_female = yes NOR = { has_law = status_of_women_4 has_game_rule = { name = gender value = all } } } }
					}
					is_contested = yes # Is already being contested in some form of Claim or Succession War
				}
			}
		}
		
		OR = {
			multiplayer = no
			FROMFROM = { ai = yes }
			has_game_rule = {
				name = multiplayer_third_party_claims
				value = on
			}
			AND = {
				has_game_rule = {
					name = multiplayer_third_party_claims
					value = limited
				}
				lower_tier_than = KING
			}
		}
	}

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

	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 = {
		hidden_tooltip = {
			ROOT = {
				opinion = {
					modifier = pressed_my_claim 
					who = PREV
					multiplier = 2
				}
			}
		}
		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
		}
	}

	on_success_title = {
		hidden_tooltip = {
			ROOT = {
				if = {
					limit = {
						has_nickname = no
						lower_tier_than = PREV
					}
					random = {
						chance = 50
						give_nickname = nick_the_usurper
					}
				}
			}
			
			# Anti-Popes take all the gold of the Vatican
			if = {
				limit = {
					OR = {
						title = k_papal_state
						title = d_fraticelli
					}
				}
				FROM = {
					transfer_scaled_wealth = {
						to = ROOT
						value = all
					}
				}
			}
		}
	
		if = {
			limit = {
				controls_religion = yes
				holder_scope = { 
					OR = {
						religion = catholic
						religion = fraticelli
					}
				}
			}
			holder_scope = {
				abdicate_to = ROOT
			}
			if = {
				limit = {
					FROMFROM = { higher_tier_than = PREV }
				}
				ROOT = { set_defacto_liege = FROMFROM }
			}
		}
		
		if = {
			limit = {
				OR = {
					controls_religion = no
					NOT = { 
						holder_scope = {
							OR = {
								religion = catholic 
								religion = fraticelli 
							}
						}
					}
				}
			}
			
			if = {
				limit = {
					holder_scope = {
						same_realm = FROMFROM # The current holder and the one pressing my claim are in the same larger realm
					}
				}
				ROOT = {
					set_character_flag = other_claim_tmp_flag
				}
			}
			
			if = {
				limit = {
					ROOT = {
						is_ruler = yes
						is_liege_or_above = FROMFROM # The claimant is a vassal ruler of the presser
					}
				}
				ROOT = {
					set_character_flag = claimant_is_vassal_ruler_flag
				}
			}

			usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
			
			hidden_tooltip = {
				ROOT = { 
					any_child =  { 
						limit = { 
							is_ruler = no 
							is_married = no 
							prisoner = no 
							is_adult = no
							is_within_diplo_range = ROOT 
						} 
						move_character = ROOT 
					}
					any_spouse =  { 
						limit = { 
							is_ruler = no 
							prisoner = no 
							is_within_diplo_range = ROOT 
						} 
						move_character = ROOT 
					}
					any_consort =  { 
						limit = { 
							is_ruler = no 
							prisoner = no 
							is_within_diplo_range = ROOT 
						} 
						move_character = ROOT 
					}
				}
				# If we were erroneously vassalized, break free
				if = {
					limit = {
						NOT = { ROOT = { has_character_flag = claimant_is_vassal_ruler_flag } }
						holder_scope = {
							is_liege_or_above = FROMFROM
						}
						NOT = { de_jure_liege_or_above = FROMFROM }
						ROOT = {
							NOT = { dynasty = FROMFROM }
						}
					}
					ROOT = { set_defacto_liege = ROOT }
				}
			
				# try and vassalize, but only conditionally
				if = {
					limit = {
						holder_scope = {
							NOR = {
								is_liege_or_above = FROMFROM
								character = FROMFROM
							}
						}
						OR = {
							holder_scope = { de_jure_liege_or_above = FROMFROM }
							ROOT = { has_character_flag = claimant_is_vassal_ruler_flag }
							ROOT = { dynasty = FROMFROM }
						}
					}
					ROOT = {
						set_defacto_liege = FROMFROM
					}
				}

				# check if the previous effect managed to make us part of the same realm
				# if not, we'll break the other realm
				if = {
					limit = {
						NOT = { ROOT = { has_character_flag = other_claim_tmp_flag } }
						FROM = {
							FROM = {
								top_liege = {
									ROOT = {
										top_liege = {
											NOT = {
												character = PREVPREV
											}
										}
									}
								}
							}
						}
					}
					ROOT = {
						set_defacto_liege = ROOT
					}
				}
				
				# If we have become independent from our mutual liege, restore that vassal tie
				if = {
					limit = {
						ROOT = { 
							has_character_flag = other_claim_tmp_flag 
							de_facto_liege = ROOT
						}
					}
					FROM = {
						FROM = {
							top_liege = {
								ROOT = { 
									set_defacto_liege = PREV
								}
							}
						}
					}
				}
				
				ROOT = { 
					clr_character_flag = other_claim_tmp_flag 
					clr_character_flag = claimant_is_vassal_ruler_flag 
				}
			}
		}
		
		if = {
			limit = {
				higher_tier_than = BARON
				NOT = { culture = ROOT }
			}
			hidden_tooltip = { conquest_culture = ROOT }
		}

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

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

	on_reverse_demand = {
		prestige = -150
		transfer_scaled_wealth = {
			to = FROM
			value = 4.0
		}
		ROOT = {
			show_scope_change = no
			prestige = -150
			imprison = FROM
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 150
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 150 }
		}
	}

	on_reverse_demand_title = {
		ROOT = { show_scope_change = no remove_claim = PREV }
	}

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

# 2
claim_on_liege = {
	name = CB_NAME_CLAIM
	war_name = WAR_NAME_CLAIM_LIEGE
	sprite = 3
	truce_days = 3650
	hostile_against_others = yes

	press_claim = yes
	can_call_vassals = no
	#attacker_can_call_allies = no
	major_revolt = yes

	allowed_to_target_tributaries = no

	infamy_modifier = 0.85
	
	sort_priority = 910

	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use_title = {
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
		OR = { 
			ROOT = { has_strong_claim = PREV }
			ROOT = { has_character_flag = faction_claimant_ultimatum_taken } # Faction war. Ignore claim strength.
			AND = {
				ROOT = { has_weak_claim = PREV }
				OR = {
					is_pretender = yes # Checks vs ROOT character
					holder_scope = { has_regent = yes }
					AND = {
						ROOT = { OR = { is_female = no has_law = status_of_women_4 } }
						holder_scope ={ AND = { is_female = yes NOR = { has_law = status_of_women_4 has_game_rule = { name = gender value = all } } } }
					}
					is_contested = yes # Is already being contested in some form of Claim or Succession War
				}
			}
		}
		is_vice_royalty = no
	}
	
	is_valid = {
		FROM = {
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
		}
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}

	is_valid_title = {
		NOT = { 
			holder_scope = { 
				character = ROOT 
			} 
		}
		
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
		OR = {
			has_law = succ_feudal_elective
			has_law = succ_hre_elective
			ROOT = { has_claim = PREV }
		}
	}

	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 = {
			participation_scaled_prestige = 100
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		
		FROM = {
			hidden_tooltip = { disband_event_forces = faction_loyalists }
			
			if = {
				limit = { NOT = { has_dlc = "Zeus" } }
				any_demesne_title = { # All titles
					limit = {
						OR = {
							tier = king
							tier = emperor
						}
	#					is_titular = no
						NOT = { has_law = crown_authority_0 }
					}
					if = {
						limit = { has_law = crown_authority_1 }
						add_law = crown_authority_0
					}
					if = {
						limit = { has_law = crown_authority_2 }
						add_law = crown_authority_1
					}
					if = {
						limit = { has_law = crown_authority_3 }
						add_law = crown_authority_2
					}
					if = {
						limit = { has_law = crown_authority_4 }
						add_law = crown_authority_3
					}
				}
			}
		}
		if = {
			limit = {
				ROOT = {
					prisoner = yes
					host = { character = FROM  }
				}
			}
			ROOT = { prisoner = no }
		}
	}
	
	on_success_title = {
		if = {
			limit = {
				is_primary_holder_title = yes
			}
			FROM = { imprison = ROOT }
		}
		
		usurp_title_plus_barony_if_unlanded_and_vassals_no_adj = { target = ROOT type = claim }
		
		if = {
			limit = {
				higher_tier_than = BARON
				NOT = { culture = ROOT }
			}
			hidden_tooltip = { conquest_culture = ROOT }
		}
		
		if = {
			limit = {
				higher_tier_than = FROM
			}
			
			FROM = {
				set_defacto_liege = ROOT
			}
		}
		
		hidden_tooltip = {
			ROOT = {
				if = {
					limit = {
						has_nickname = no
						PREV = { higher_tier_than = count }
					}
					random = {
						chance = 50
						give_nickname = nick_the_usurper
					}
				}
			}
		}

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

	on_fail = {
		ROOT = {
			prestige = -100
		}
		FROM = {
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		
		hidden_tooltip = {
			any_attacker = {
				limit = { liege = { character = FROM } }
				disband_event_forces = faction_revolters
			}
		}
		
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -150
		}
		
		FROM = {
			participation_scaled_prestige = 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
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		
		hidden_tooltip = {
			any_attacker = {
				limit = { liege = { character = FROM } }
				disband_event_forces = faction_revolters
				prisoner = FROM
			}
		}
		
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}
	
	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 = 100
	}

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


# 3
claim_on_liege_plot = {
	name = CB_NAME_CLAIM
	war_name = WAR_NAME_CLAIM_LIEGE
	sprite = 3
	truce_days = 3650
	hostile_against_others = yes

	press_claim = yes
	major_revolt = yes
	attacker_can_call_allies = no
	can_ask_to_join_war = no
	
	allowed_to_target_tributaries = no

	infamy_modifier = 0.85

	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use_title = {
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
	}
	
	is_valid = {
		FROM = {
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
		}
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}

	is_valid_title = {
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = 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 
			}
		}
	}

	on_success = {
		ROOT = {
			participation_scaled_prestige = 100
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		
		FROM = {
			
			hidden_tooltip = { disband_event_forces = faction_loyalists }
		
			if = {
				limit = { NOT = { has_dlc = "Zeus" } }
				any_demesne_title = { # All titles
					limit = {
						OR = {
							tier = king
							tier = emperor
						}
						#is_titular = no
						NOT = { has_law = crown_authority_0 }
					}
					if = {
						limit = { has_law = crown_authority_1 }
						add_law = crown_authority_0
					}
					if = {
						limit = { has_law = crown_authority_2 }
						add_law = crown_authority_1
					}
					if = {
						limit = { has_law = crown_authority_3 }
						add_law = crown_authority_2
					}
					if = {
						limit = { has_law = crown_authority_4 }
						add_law = crown_authority_3
					}
				}
			}
		}
	}
	
	on_success_title = {
		usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
		
		if = {
			limit = {
				higher_tier_than = BARON
				NOT = { culture = ROOT }
			}
			hidden_tooltip = { conquest_culture = ROOT }
		}
		ROOT = {
			plot_succeeds = yes
		}
	}

	on_fail = {
		ROOT = {
			prestige = -100
		}
		FROM = {
			participation_scaled_prestige = 50
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }

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

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			imprison = FROM
		}
		FROM = {
			participation_scaled_prestige = 100
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
			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
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}
	
	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 = 100
	}

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


# 4
claim_on_vassal_plot = {
	name = CB_NAME_CLAIM_VASSAL
	war_name = WAR_NAME_CLAIM_VASSAL
	sprite = 3
	truce_days = 3650
	hostile_against_others = yes
	reflect_titleholder_change = no

	press_claim = yes
	attacker_can_call_allies = no
	can_attack_vassals = yes

	allowed_to_target_tributaries = no

	infamy_modifier = 0.85

	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use_title = {
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				de_facto_liege = ROOT
			}
		}
	}

	is_valid_title = {
		FROM = {
			has_landed_title = PREV
		}
		ROOT = {
			has_plot = plot_gain_vassal_title
		}
	}

	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 = 50
		}
	}
	
	on_success_title = {
		usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
		ROOT = { plot_succeeds = yes }

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

	on_fail = {
		ROOT = {
			prestige = -100
			cancel_plot = plot_gain_vassal_title
		}
		FROM = {
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}
	
	on_invalidation = {
		ROOT = {
			cancel_plot = plot_gain_vassal_title
		}
		
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			cancel_plot = plot_gain_vassal_title
		}
		FROM = {
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}
	
	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 = 100
	}
	
	on_defender_leader_death = {
		ROOT = {
			cancel_plot = plot_gain_vassal_title
		}
	}

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

# 5
other_claim_on_liege = {
	name = CB_NAME_CLAIM
	war_name = WAR_NAME_OTHER_CLAIM
	sprite = 4
	truce_days = 3650
	hostile_against_others = yes
	can_ask_to_join_war = no

	press_claim = yes
	can_call_vassals = no
	#attacker_can_call_allies = no
	major_revolt = yes
	
	allowed_to_target_tributaries = no

	infamy_modifier = 0.5
	
	sort_priority = 850

	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use_title = {
		OR = {
			ROOT = { is_female = no }
			NOT = { has_law = agnatic_succession }
			AND = {
				lower_tier_than = KING
				is_primary_holder_title = no
			}
			FROMFROM = { has_character_flag = faction_claimant_ultimatum_taken } # Faction war. Ignore claim strength.
		}

		FROM = {
			has_landed_title = PREV
		}
		
		OR = { 
			ROOT = { has_strong_claim = PREV }
			FROMFROM = { has_character_flag = faction_claimant_ultimatum_taken } # Faction war. Ignore claim strength.
			AND = {
				ROOT = { has_weak_claim = PREV }
				OR = {
					is_pretender = yes # Checks vs ROOT character
					holder_scope = { has_regent = yes }
					AND = {
						ROOT = { OR = { is_female = no has_law = status_of_women_4 } }
						holder_scope ={ AND = { is_female = yes NOR = { has_law = status_of_women_4 has_game_rule = { name = gender value = all } } } }
					}
					is_contested = yes # Is already being contested in some form of Claim or Succession War
				}
			}
		}
		is_vice_royalty = no
	}
	
	is_valid = {
		FROM = {
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
		}
		OR = {
			liege = {
				character = PREV # either independent
			}
			liege = { 
				FROM = { 
					is_liege_or_above = PREV # or have shared liege
				}
			}
		}
	}
			
	is_valid_title = {
		NOT = { 
			holder_scope = { 
				character = ROOT 
			} 
		}	
	
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
		
		OR = {
			has_law = succ_feudal_elective
			has_law = succ_hre_elective
			ROOT = { has_claim = PREV }
		}
	
		NOT = {
			any_war = { # when used in title scope, checks all wars for that title
				attacker = {
					character = ROOT
				}
			}
		}
	}

	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 = {
			participation_scaled_prestige = 100
			hidden_tooltip = {  disband_event_forces = faction_revolters }
		}
		participation_scaled_prestige = 100
		hidden_tooltip = {  disband_event_forces = faction_revolters }
		any_attacker = {
			limit = { 
				NOT = { character = ROOT } 
				NOT = { character = PREV }
			}
			hidden_tooltip = { 
				participation_scaled_prestige = 100 
				disband_event_forces = faction_revolters
			}
		}
		ROOT = {
			opinion = {
				modifier = pressed_my_claim 
				who = PREV
			}
		}

		FROM = {
			hidden_tooltip = { disband_event_forces = faction_loyalists }
			
			if = {
				limit = { NOT = { has_dlc = "Zeus" } }
				any_demesne_title = { # All titles
					limit = {
						OR = {
							tier = king
							tier = emperor
						}
						#is_titular = no
						NOT = { has_law = crown_authority_0 }
					}
					if = {
						limit = { has_law = crown_authority_1 }
						add_law = crown_authority_0
					}
					if = {
						limit = { has_law = crown_authority_2 }
						add_law = crown_authority_1
					}
					if = {
						limit = { has_law = crown_authority_3 }
						add_law = crown_authority_2
					}
					if = {
						limit = { has_law = crown_authority_4 }
						add_law = crown_authority_3
					}
				}
			}
		}
		
		if = {
			limit = {
				ROOT = {
					prisoner = yes
					host = { character = FROM  }
				}
			}
			ROOT = { prisoner = no }
		}
	}
	
	on_success_title = {
		if = {
			limit = {
				is_primary_holder_title = yes
			}
			hidden_tooltip = {
				FROM = { set_character_flag = temp_other_claim_on_liege_prison }
			}
		}
		
		hidden_tooltip = {
			ROOT = {
				if = {
					limit = {
						has_nickname = no
						lower_tier_than = PREV
					}
					random = {
						chance = 50
						give_nickname = nick_the_usurper
					}
				}
			}
		}
		
		# If the claimant has the wrong government type, take care of that first.
		if = {
			limit = {
				higher_tier_than = ROOT
				holder_scope = {
					is_feudal = yes
				}
				ROOT = {
					is_ruler = yes
					OR = {
						is_theocracy = yes
						is_republic = yes
					}
				}
			}
			ROOT = {
				abdicate = {
					move = no
				}
			}
		}
		
		usurp_title_plus_barony_if_unlanded_and_vassals_no_adj = { target = ROOT type = claim }
		
		if = {
			limit = {
				higher_tier_than = BARON
				NOT = { culture = ROOT }
			}
			hidden_tooltip = { conquest_culture = ROOT }
		}

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

	on_success_posttitle = {
		hidden_tooltip = {
			if = {
				limit = {
					top_liege = {
						ROOT = {
							NOT = {
								top_liege = {
									character = PREVPREV
								}
							}
						}
					}
				}

				ROOT = {
					set_defacto_liege = PREV
				}
			}

			if = {
				limit = {
					top_liege = {
						ROOT = {
							NOT = {
								top_liege = {
									character = PREVPREV
								}
							}
						}
					}
				}

				ROOT = {
					set_defacto_liege = ROOT
				}
			}
			
			if = {
				limit = {
					FROM = { 
						has_character_flag = temp_other_claim_on_liege_prison 
						lower_tier_than = PREV
					}
				}
				FROM = { 
					imprison = ROOT
				}
			}
			FROM = { clr_character_flag = temp_other_claim_on_liege_prison }
		}
	}

	on_fail = {
		prestige = -100
		
		imprison = FROM
		#if = { #Issue CKTWO-233
		#	limit = {
		#		OR = {
		#			is_liege_of = ROOT
		#			ROOT = { is_liege_or_above = FROM }
		#		}
		#	}
		#	ROOT = {
		#		imprison = FROM
		#	}
		#}
		
		FROM = {
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		
		hidden_tooltip = {
			any_attacker = {
				disband_event_forces = faction_revolters
			}
		}
		
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_reverse_demand = {
		prestige = -200
		
		imprison = FROM
		#if = { #Issue CKTWO-233
		#	limit = {
		#		OR = {
		#			is_liege_of = ROOT
		#			ROOT = { is_liege_or_above = FROM }
		#		}
		#	}
		#	ROOT = {
		#		imprison = FROM
		#	}
		#}
		
		FROM = {
			participation_scaled_prestige = 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
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		
		hidden_tooltip = {
			any_attacker = {
				limit = { liege = { character = FROM } }
				disband_event_forces = faction_revolters
				prisoner = FROM
			}
		}
		
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}
	
	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
	}
}

# 6
claim_all = {
	name = CB_NAME_CLAIM_ALL
	war_name = WAR_NAME_CLAIM_ALL
	sprite = 5
	truce_days = 3650
	is_permanent = yes
	can_ask_to_join_war = no
	is_claim_all = yes

	allowed_to_target_tributaries = no
	
	sort_priority = 1100

	can_use = {
		ROOT = {
			total_claims = {
				who = FROM
				strong = yes # Count only strong claims
				value = 2
			}
		}
	}

	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	is_valid = {
		ROOT = {
			total_claims = {
				who = FROM
				strong = yes # Count only strong claims
				value = 1 # must have at least one claim left
			}
		}
	}

	on_add = {
		ROOT = {
			show_scope_change = no
			any_claim = {
				show_scope_change = no
				limit = {
					holder_scope = {
						OR = {
							character = FROM
							is_liege_or_above = FROM
						}
					}
					ROOT = { has_strong_claim = PREV }
				}
				press_claim = ROOT
			}
		}
		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
			any_claim = {
				show_scope_change = no
				limit = {
					holder_scope = {
						OR = {
							character = FROM
							is_liege_or_above = FROM
						}
					}
					ROOT = { has_strong_claim = PREV }
				}
				usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
				if = {
					limit = {
						higher_tier_than = BARON
						NOT = { culture = ROOT }
					}
					hidden_tooltip = { conquest_culture = ROOT }
				}
			}
			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
			}
		}
		hidden_tooltip = {
			ROOT = {
				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
						}
					}
					character_event = { id = LoR.30 days = 12 }
				}
			}
		}
		ROOT = {
			show_scope_change = no
			participation_scaled_prestige = 100
		}
		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
			}
			if = {
				limit = {
					has_character_flag = is_otto
					FROM = {
						has_character_flag = is_giselbert
					}
				}
				reverse_imprison = FROM
			}
		}
	}

	on_fail = {
		ROOT = {
			show_scope_change = no
			prestige = -150
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 75
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 75 }
		}
	}

	on_reverse_demand = {
		ROOT = {
			show_scope_change = no
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			any_claim = {
				show_scope_change = no
				limit = {
					holder_scope = {
						OR = {
							character = FROM
							is_liege_or_above = FROM
						}
					}
					ROOT = { has_strong_claim = PREV }
				}
				ROOT = { show_scope_change = no remove_claim = PREV }
			}
			hidden_effect = {
				if = { #Makes China have a chance of leaving Expanionist policy...
					limit = {
						has_landed_title = e_china_west_governor
						offmap_china = {
							has_policy = china_expansionist
						}
					}
					random = {
						chance = 50
						offmap_china = {
							set_policy = china_open
							log = "-------------------------------------"
							log = "China Policy Logging:"
							log = "Chinas new Policy is now OPEN"
							log = "-------------------------------------"
						}
					}
					log = "China War Logging: China lost a claim all war against [From.GetBestName]"
				}
			}
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 200
			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
					}
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}

	}

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


change_seniority_succession_law = {
	name = CB_NAME_SENIORITY_SUCCESSION_LAW
	war_name = WAR_NAME_SENIORITY_SUCCESSION_LAW
	sprite = 6
	truce_days = 3650
	attacker_can_call_allies = no
	can_ask_to_join_war = no
	major_revolt = yes
	
	can_use = {
		ROOT = {
			vassal_of = FROM	
		}
	}

	can_use_gui = {
		check_if_crusader_trigger = yes
	}
	
	is_valid = {
		FROM = {
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
		}
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}

	is_valid_title = {
		NOT = {
			has_law = succ_seniority
		}
		OR = {
			tier = emperor
			tier = king
			is_primary_holder_title = yes
		}
	}

	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 = {
		FROM = {
			hidden_tooltip = { disband_event_forces = faction_loyalists }
			
			if = {
				limit = { NOT = { has_dlc = "Zeus" } }
				any_demesne_title = { # All titles
					limit = {
						OR = {
							tier = king
							tier = emperor
						}
						#is_titular = no
						NOT = { has_law = crown_authority_0 }
					}
					if = {
						limit = { has_law = crown_authority_1 }
						add_law = crown_authority_0
					}
					if = {
						limit = { has_law = crown_authority_2 }
						add_law = crown_authority_1
					}
					if = {
						limit = { has_law = crown_authority_3 }
						add_law = crown_authority_2
					}
					if = {
						limit = { has_law = crown_authority_4 }
						add_law = crown_authority_3
					}
				}
			}
		}
		ROOT = {
			participation_scaled_prestige = 100
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = 100 
				disband_event_forces = faction_revolters
			}
		}
	}

	on_success_title = {
		succession_w_cooldown = seniority
	}

	on_fail = {
		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 }
		}
		
		hidden_tooltip = {
			any_attacker = {
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_succ_seniority_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_reverse_demand = {
		ROOT = {
			show_scope_change = no
			prestige = -200
			prisoner = FROM
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 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
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		
		hidden_tooltip = {
			any_attacker = {
				limit = { liege = { character = FROM } }
				prisoner = FROM
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_succ_seniority_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

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


change_primogeniture_succession_law = {
	name = CB_NAME_PRIMOGENITURE_SUCCESSION_LAW
	war_name = WAR_NAME_PRIOGENITURE_SUCCESSION_LAW
	sprite = 6
	truce_days = 3650
	attacker_can_call_allies = no
	can_ask_to_join_war = no
	major_revolt = yes
	
	can_use = {
		ROOT = {
			vassal_of = FROM	
		}
	}

	can_use_gui = {
		check_if_crusader_trigger = yes
	}
	
	is_valid = {
		FROM = {
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
		}
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}
		
	is_valid_title = {
		NOT = {
			has_law = succ_primogeniture
		}
		OR = {
			tier = emperor
			tier = king
			is_primary_holder_title = yes
		}
	}

	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 = {
		FROM = {
			hidden_tooltip = { disband_event_forces = faction_loyalists }
			
			if = {
				limit = { NOT = { has_dlc = "Zeus" } }
				any_demesne_title = { # All titles
					limit = {
						OR = {
							tier = king
							tier = emperor
						}
						#is_titular = no
						NOT = { has_law = crown_authority_0 }
					}
					if = {
						limit = { has_law = crown_authority_1 }
						add_law = crown_authority_0
					}
					if = {
						limit = { has_law = crown_authority_2 }
						add_law = crown_authority_1
					}
					if = {
						limit = { has_law = crown_authority_3 }
						add_law = crown_authority_2
					}
					if = {
						limit = { has_law = crown_authority_4 }
						add_law = crown_authority_3
					}
				}
			}
		}
		
		ROOT = {
			show_scope_change = no
			participation_scaled_prestige = 100
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = 100 
				disband_event_forces = faction_revolters
			}
		}
	}

	on_success_title = {
		succession_w_cooldown = primogeniture
	}

	on_fail = {
		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 }
		}
		
		hidden_tooltip = {
			any_attacker = {
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_succ_primogeniture_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_reverse_demand = {
		ROOT = {
			show_scope_change = no
			prestige = -200
			prisoner = FROM
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 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
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		
		hidden_tooltip = {
			any_attacker = {
				limit = { liege = { character = FROM } }
				prisoner = FROM
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_succ_primogeniture_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

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

change_feudal_elective_succession_law = {
	name = CB_NAME_FEUDAL_ELECTIVE_SUCCESSION_LAW
	war_name = WAR_NAME_FEUDAL_ELECTIVE_SUCCESSION_LAW
	sprite = 6
	truce_days = 3650
	attacker_can_call_allies = no
	can_ask_to_join_war = no
	major_revolt = yes
	
	can_use = {
		ROOT = {
			vassal_of = FROM	
		}
	}

	can_use_gui = {
		check_if_crusader_trigger = yes
	}

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

	is_valid_title = {
		NOR = {
			has_law = succ_feudal_elective
			has_law = succ_hre_elective
		}
		OR = {
			tier = emperor
			tier = king
			is_primary_holder_title = yes
		}

	}
	
	on_success = {
		FROM = {
		
			hidden_tooltip = { disband_event_forces = faction_loyalists }
			if = {
				limit = { NOT = { has_dlc = "Zeus" } }
				any_demesne_title = { # All titles
					limit = {
						OR = {
							tier = king
							tier = emperor
						}
	#					is_titular = no
						NOT = { has_law = crown_authority_0 }
					}
					if = {
						limit = { has_law = crown_authority_1 }
						add_law = crown_authority_0
					}
					if = {
						limit = { has_law = crown_authority_2 }
						add_law = crown_authority_1
					}
					if = {
						limit = { has_law = crown_authority_3 }
						add_law = crown_authority_2
					}
					if = {
						limit = { has_law = crown_authority_4 }
						add_law = crown_authority_3
					}
				}
			}
		}
		ROOT = {
			show_scope_change = no
			participation_scaled_prestige = 100
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = 100 
				disband_event_forces = faction_revolters
			}
		}
	}
	
	on_success_title = {
		if = { 
			limit = {
				NOR = { 
					title = e_hre 
					has_title_flag = alternate_hre
				}
			}
			add_law_w_cooldown = succ_feudal_elective
			#succession_w_cooldown = {
			#	base_type = feudal_elective
			#	voting_rules = feudal_elective
			#}
		}
		if = { 
			limit = {
				OR = { 
					title = e_hre 
					has_title_flag = alternate_hre
				}
			}
			add_law_w_cooldown = succ_hre_elective
			#succession_w_cooldown = {
			#	base_type = feudal_elective
			#	voting_rules = hre_feudal_elective
			#}
		}
	}

	on_fail = {
		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 }
		}
		
		hidden_tooltip = {
			any_attacker = {
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_succ_feudal_elective_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}
	
	on_reverse_demand = {
		ROOT = {
			show_scope_change = no
			prestige = -200
			prisoner = FROM
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 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
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		
		hidden_tooltip = {
			any_attacker = {
				limit = { liege = { character = FROM } }
				prisoner = FROM
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_succ_feudal_elective_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

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


invasion = {
	name = CB_NAME_INVASION
	war_name = WAR_NAME_INVASION
	sprite = 7
	truce_days = 3650
	hostile_against_others = yes
	can_ask_to_join_war = no
	
	allowed_to_target_tributaries = no
	
	sort_priority = 2000

	can_use = {
		ROOT = {
			mercenary = no
		}
	}

	can_use_gui = {
		check_if_crusader_trigger = yes
	}
	
	can_use_title = {
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
	}
	
	is_valid_title = {
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = 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 
			}
		}
	}

	on_success = {
		ROOT = {
			show_scope_change = no
			prestige = 200
		}
		ROOT = {
			show_scope_change = no
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		
		hidden_tooltip = {
			FROM = {
				any_vassal = {
					set_character_flag = preserve_vassal_structure_@ROOT
				}
			}
			ROOT = {
				occupy_minors_of_occupied_settlements = FROM
				gain_all_occupied_titles = FROM
				
				if = {
					limit = {
						has_nickname = no
					}
					random_list = {
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_victorious }
						10 = { give_nickname = nick_the_bold }
						10 = { give_nickname = nick_the_conqueror }
					}
				}
				
				if = {
					limit = {
						has_nickname = nick_the_bastard
					}
					give_nickname = nick_the_conqueror
				}
				
				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
						}
					}
					character_event = { id = LoR.30 days = 12 }
				}
			}
		}
		
		FROM = {
			show_scope_change = no
			prestige = -200
		}
	}

	on_success_title = {
		custom_tooltip = {
			text = invasion_succ_tip
			hidden_tooltip = {
				usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
				FROM = { set_defacto_liege = ROOT }
				if = {
					limit = {
						FROM = { is_liege_or_above = ROOT }
					}
					FROM = { imprison = ROOT }
				}
				if = {
					limit = {
						any_independent_ruler = {
							has_character_flag = preserve_vassal_structure_@ROOT
						}
					}
					any_independent_ruler = {
						limit = {
							has_character_flag = preserve_vassal_structure_@ROOT
						}
						set_defacto_liege = ROOT
					}
				}
				ROOT = {
					any_vassal = {
						clr_character_flag = preserve_vassal_structure_@ROOT
					}
				}
			}
		}
		
		if = {
			limit = {
				higher_tier_than = BARON
				NOT = { culture = ROOT }
			}
			hidden_tooltip = { conquest_culture = ROOT }
		}
		
		if = {
			limit = { ROOT = { character = 140 } }
			ROOT = {
				if = {
					limit = {
						has_dlc = "Holy Fury"
						NOT = {
							any_owned_bloodline = {
								has_bloodline_flag = william_the_conqueror_bloodline
							}
						}
					}
					create_bloodline = {
						type = william_the_conqueror
					}
				}
			}
			hidden_tooltip = {
				add_law = succ_primogeniture
				ROOT = {
					any_vassal = {
						limit = {
							culture = saxon
						}
						opinion = {
							modifier = opinion_subjugated 
							who = ROOT
						}
					}
					if = {
						limit = { # Help AI William by usurping extra land
							ai = yes
						}
						d_bedford = {
							usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
							any_de_jure_vassal_title = {
								limit = {
									holder_scope = {
										culture = saxon
										ai = yes # But not from players
										same_realm = ROOT
									}
								}
								usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
							}
						}
						d_canterbury = {
							usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
							any_de_jure_vassal_title = {
								limit = {
									holder_scope = {
										culture = saxon
										ai = yes # But not from players
										same_realm = ROOT
									}
								}
								usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
							}
						}
						d_cornwall = {
							usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
							any_de_jure_vassal_title = {
								limit = {
									holder_scope = {
										culture = saxon
										ai = yes # But not from players
										same_realm = ROOT
									}
								}
								usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
							}
						}
						d_somerset = {
							usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
							any_de_jure_vassal_title = {
								limit = {
									holder_scope = {
										culture = saxon
										ai = yes # But not from players
										same_realm = ROOT
									}
								}
								usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
							}
						}
						d_gloucester = {
							usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
							any_de_jure_vassal_title = {
								limit = {
									holder_scope = {
										culture = saxon
										ai = yes # But not from players
										same_realm = ROOT
									}
								}
								usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
							}
						}
						d_norfolk = {
							usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
							any_de_jure_vassal_title = {
								limit = {
									holder_scope = {
										culture = saxon
										ai = yes # But not from players
										same_realm = ROOT
									}
								}
								usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
							}
						}
					}
					if = {
						limit = {
							c_middlesex = {
								holder_scope = {
									character = ROOT
								}
							}
						}
						c_middlesex = {
							capital_holding = {
								ROOT = { capital = PREV }
							}
						}
					}
				}
			}
		}

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

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

	on_reverse_demand = {
		ROOT = {
			show_scope_change = no
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 8.0
			}
		}
		FROM = {
			show_scope_change = no
			prestige = 200
			participation_scaled_prestige = 200
			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
						}
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
	}

	on_reverse_demand_title = {
		ROOT = { show_scope_change = no remove_claim = PREV }
	}

	on_attacker_leader_death = {
		hidden_tooltip = {
			any_defender = {
				letter_event = {
					id = 251
				}
			}
		}

		end_war = whitepeace
	}

	attacker_ai_victory_worth = {
		factor = 150
	}
	
	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 = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_nomadic = no
				NOT = {
					is_tribal = yes
				}
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}
				}
			}
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_tribal = yes
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
							holding_type = tribal
						}
					}
				}
			}
		}
	}
}

tribal_invasion = {
	name = CB_NAME_INVASION
	war_name = WAR_NAME_INVASION
	sprite = 8
	truce_days = 3650
	hostile_against_others = yes
	is_permanent = yes
	can_ask_to_join_war = yes
	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
	
	allowed_to_target_tributaries = no
	
	sort_priority = 2000

	can_use_gui = {
		ROOT = {
			prestige = 500
		}
		check_if_crusader_trigger = yes
	}
	
	on_add = {
		ROOT = { show_scope_change = no prestige = -500 }
		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 
			}
		}
		if = {
			limit = { 
				attacker = { 
					any_owned_bloodline = {
						has_bloodline_flag = bloodline_great_conquerors
					} 
				} 
			}
			attacker = {  
				set_character_flag = flag_declared_bloodline_invasion
			}
		}
	}

	can_use_title = {
		ROOT = {
			OR = {
				ai = yes
				multiplayer = no
				NOT = {
					has_game_rule = {
						name = multiplayer_invasions
						value = off
					}
				}
			}
			is_nomadic = no
			OR = {
				AND = {
					has_dlc = "Horse Lords"
					higher_tier_than = COUNT
					OR = {
						has_character_flag = horde_invader
						AND = {
							independent = yes
							religion_group = pagan_group
							OR = {
								culture_group = altaic
								culture_group = magyar
								culture_group = mesoamerican
							}
							FROM = {
								NOT = { religion_group = pagan_group } # Should subjugate these instead
							}
							OR = {
								is_tribal = yes
								primary_title = {
									has_law = succ_elective_gavelkind
								}
								has_landed_title = e_mongol_empire
								has_landed_title = e_mexikha
							}
						}
					}
				}
				AND = {
					NOT = { has_dlc = "Horse Lords" }
					independent = yes
					higher_tier_than = COUNT
					OR = {
						religion_group = pagan_group
						AND = {
							OR = {
								has_horde_culture = yes
								has_character_flag = horde_invader
							}
							NOT = { religion_group = christian }
						}
					}
					OR = {
						culture_group = altaic
						culture_group = magyar
						culture_group = mesoamerican
					}
					OR = {
						has_horde_culture = yes
						has_character_flag = horde_invader
						FROM = {
							NOT = { religion_group = pagan_group } # Should subjugate these instead
						}
					}
					OR = {
						has_character_flag = horde_invader
						is_tribal = yes
						primary_title = {
							has_law = succ_elective_gavelkind
						}
					}
				}
				AND = {
					mercenary = yes
					NOT = {
						num_of_titles = 2
					}
				}
				AND = {
					has_character_flag = is_child_of_destiny
					any_owned_bloodline = {
						has_bloodline_flag = founder_invasions
					}
				}
				AND = {
					NOT = { has_character_flag = flag_declared_bloodline_invasion }
					any_owned_bloodline = {
						has_bloodline_flag = bloodline_great_conquerors
					}
				}
			}
		}
		
		ROOT = {
			NOT = { same_realm = FROM }
		}
		
		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
			}
			ROOT = {
				OR = {
					has_landed_title = e_mongol_empire
					has_landed_title = e_mexikha
				}
			}
			FROM = {
				num_of_realm_counties = {
					value = 3
					title = PREV
				}
			}
		}
	}
	
	is_valid_title = {
		FROM = {
			any_realm_province = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}

	on_success = {
		ROOT = {
			show_scope_change = no
			prestige = 200
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		FROM = {
			show_scope_change = no
			prestige = -200
		}
		hidden_tooltip = {
			ROOT = {
				occupy_minors_of_occupied_settlements = FROM
				gain_all_occupied_titles = { who = FROM type = invasion }
				
				if = {
					limit = {
						has_nickname = no
					}
					random_list = {
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_victorious }
						10 = { give_nickname = nick_the_conqueror }
						10 = { give_nickname = nick_the_brave }
						10 = { give_nickname = nick_the_dragon }
						10 = { give_nickname = nick_the_terrible }
					}
				}
			}
		}
	}
	
	on_success_title = {
	
		if = {
			limit = {
				ROOT = {
					OR = {
						character = 159136
						character = 159137
					}
				}
			}
			ROOT = {
				if = {
					limit = {
						has_dlc = "Holy Fury"
						NOT = {
							any_owned_bloodline = {
								has_bloodline_flag = arpad_bloodline
							}
						}
					}
					create_bloodline = {
						type = arpad
					}
				}
			}
		}
		
		custom_tooltip = {
			text = tribal_invasion_succ_tip
			hidden_tooltip = {
				ROOT = {
					vassalize_or_take_under_title_destroy_duchies = {
						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 = invasion
					}
				}
				if = {
					limit = {
						e_mexikha = {
							holder = ROOT
						}
					}
					ROOT = {
						any_realm_province = {
							limit = {
								OR = {
									has_province_flag = aztec_explorers
									has_province_flag = aztec_second_wave
								}
							}
							culture = nahuatl
							religion = aztec
							clr_province_flag = aztec_explorers
							clr_province_flag = aztec_second_wave
						}
					}
				}
			}
		}

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

	on_fail = {
		FROM = {
			show_scope_change = no
			prestige = 100
			
			hidden_tooltip = {
				if = {
					limit = {
						has_nickname = no
					}
					random_list = {
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_hammer }
						10 = { give_nickname = nick_the_avenger }
						10 = { give_nickname = nick_the_lion }
						10 = { give_nickname = nick_the_bold }
					}
				}
			}
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		ROOT = {
			show_scope_change = no
			prestige = -200
		}
	}

	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
			hidden_tooltip = {
				if = {
					limit = {
						has_nickname = no
					}
					random_list = {
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_hammer }
						10 = { give_nickname = nick_the_avenger }
						10 = { give_nickname = nick_the_lion }
						10 = { give_nickname = nick_the_bold }
					}
				}
			}
			
			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
						}
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
			participation_scaled_prestige = 200
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
	}

	attacker_ai_victory_worth = {
		factor = 200
	}
	
	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.9 # 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
				}
			}
		}
		
		# Try to guide the Hordes in the right direction
		modifier = {
			factor = 0.1
			ROOT = {
				OR = {
					dynasty = 613 # Seljuks
					dynasty = 800 # Timurids
					primary_title = { title = e_il-khanate }
				}
			}
			OR = {
				empire = {
					NOT = {
						OR = {
							title = e_persia
							title = e_byzantium
							title = e_arabia
						}
					}
				}
				FROM = {
					primary_title = { title = e_golden_horde }
					NOT = { year = 1300 }
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = {
				primary_title = { title = e_golden_horde }
			}
			OR = {
				empire = {
					OR = {
						title = e_persia
						title = e_byzantium
						title = e_arabia
					}
				}
				FROM = {
					primary_title = { title = e_il-khanate }
					NOT = { year = 1300 }
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = {
				culture = hungarian
				primary_title = {
					OR = {
						title = k_hungary
						title = e_carpathia
					}
				}
			}
			empire = {
				OR = {
					title = e_tartaria
					title = e_russia
				}
			}
		}
		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 = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_nomadic = no
				NOT = {
					is_tribal = yes
				}
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}
				}
			}
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_tribal = yes
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
							holding_type = tribal
						}
					}
				}
			}
		}
	}
}

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
	is_crusade = yes
	
	allowed_to_target_tributaries = no
	
	sort_priority = 3000

	can_use_gui = {
		ROOT = {
			uses_new_crusade = no
			has_called_crusade = no
			religion_authority = 0.05
			conditional_tooltip = {
				trigger = {
					is_alternate_start = yes
					has_alternate_start_parameter = {
						key = religion_names
						value = random
					}
				}
				custom_tooltip = {
					text = ALT_GHW_STARTED
					has_global_flag = muslim_jihads_unlocked
					has_global_flag = pagan_ghws_unlocked
					has_global_flag = christian_crusades_unlocked
				}
			}
			conditional_tooltip = {
				trigger = {
					NOT = {
						has_alternate_start_parameter = {
							key = religion_names
							value = random
						}
					}
				}
				OR = {
					religion_group = zoroastrian_group
					religion_group = jewish_group
					AND = {
						religion_group = pagan_group
						custom_tooltip = {
							text = CTT_PAGAN_GHWS_UNLOCKED
							has_global_flag = pagan_ghws_unlocked
						}
					}
					AND = { 
						religion_group = christian
						custom_tooltip = {
							text = CTT_CHRISTIAN_CRUSADES_UNLOCKED
							has_global_flag = christian_crusades_unlocked
						}
					}
					AND = {
						religion_group = muslim
						custom_tooltip = {
							text = CTT_MUSLIM_JIHADS_UNLOCKED
							has_global_flag = muslim_jihads_unlocked
						}
					}
				}
			}
			mercenary = no
		}
	}

	can_use = {
		ROOT = {
			uses_new_crusade = no
			can_call_crusade = yes
		}
		
		FROM = {
			OR = {
				NOT = { religion_group = ROOT }
				is_heresy_of = ROOT
				is_parent_religion = ROOT
				AND = {
					religion_group = pagan_group
					NOT = { religion = ROOT }
				}
			}
		}
		
		NOT = { # Not if the Schism has been mended
			AND = {
				OR = {
					religion = catholic
					religion = fraticelli
				}
				has_global_flag = schism_mended
			}
		}
	}

	can_use_title = {
		FROM = {
			any_realm_title = {
				de_jure_liege_or_above = PREVPREV
			}
			
			independent = yes # Don't declare Crusades on revolters - chances are high the war will invalidate
			
			num_of_realm_counties = {
				value = 6
				title = PREV
			}
		}
	}
	
	is_valid = {
		ROOT = {
			can_call_crusade = yes
		}
		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_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_title = {
	
		# Should we give the seized land to its current king?
		if = {
			limit = {
				has_holder = yes
				holder_scope = {
					religion = ROOT
				}
			}
			holder_scope = {
				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
				}
			}
		}
		
		# Should we give the seized land to a claimant of our faith?
		if = {
			limit = {
				OR = {
					has_holder = no
					holder_scope = {
						NOT = { religion = ROOT }
					}
				}
				
				best_crusade_claimant = {
					always = yes # There is a "best" claimant
				}
			}
			
			# Claimants must be of ROOT's religion, and are ranked by strength of claim and actual contribution to the war
			best_crusade_claimant = {
				if = {
					limit = { is_ruler = no }
					set_character_flag = set_feudal_gov
				}
				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
				}
				if = {
					limit = {
						top_liege = {
							character = FROM
						}
					}
					set_defacto_liege = THIS
				}
				if  = {
					limit = {
						PREV = {
							OR = { 
								has_holder = no
								holder_scope = { 
									top_liege = { character = FROM }
								}
							} 
						}
						OR = {
							independent = yes
							is_ruler = no
							liege = {
								tier = EMPEROR
							}
						}
					}
					usurp_title = { target = PREV type = holy_war }
				}
				if = {
					limit = { has_character_flag = set_feudal_gov }
					set_government_type = feudal_government
				}
				clr_character_flag = set_feudal_gov
			}
		}
		
		# Give the seized land to the main contributor of the Crusade
		if = {
			limit = {
				OR = {
					has_holder = no
					NOT = {
						holder_scope = {
							religion = ROOT
						}
					}
				}
				NOT = {
					best_crusade_claimant = {
						always = yes
					}
				}
			}
			most_participating_crusader = {
				gain_settlements_under_title = {
					title = PREV
					enemy = FROM
					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 } 
						}
					}
				}
				hidden_tooltip = {
					if = {
						limit = {
							OR = {
								this_has_lame_nickname_trigger = yes
								has_nickname = no
							}
						}
						random_list = {
							10 = {
								modifier = {
									factor = 0
									NOT = { religion_group = christian }
								}
								give_nickname = nick_the_crusader
							}
							10 = { give_nickname = nick_the_holy }
							10 = { give_nickname = nick_the_glorious }
							10 = { give_nickname = nick_the_lionheart }
							10 = { give_nickname = nick_the_great }
							10 = { give_nickname = nick_the_hammer }
							10 = { give_nickname = nick_the_pilgrim }
							40 = {
								modifier = {
									factor = 0
									religion_group = christian
								}
								give_nickname = nick_the_sword_of_god
							}
						}
					}
				}
			}
		}
		
		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 = {
			show_scope_change = no
			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
			}			
		}

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

	on_fail = {
		ROOT = {
			show_scope_change = no
			piety = -100
		}
		
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_piety = 200
				participation_scaled_prestige = 100
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -50
				}
			}
		}
		
		FROM = {
			participation_scaled_piety = 200
			participation_scaled_prestige = 100
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -50
			}
			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
						}
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
			
			hidden_tooltip = {
				if = {
					limit = {
						OR = {
							this_has_lame_nickname_trigger = yes
							has_nickname = no
						}
					}
					hidden_effect = {
						random_list = {
							10 = { give_nickname = nick_the_holy }
							10 = { give_nickname = nick_the_glorious }
							10 = { give_nickname = nick_the_lionheart }
							10 = { give_nickname = nick_the_great }
							10 = { give_nickname = nick_the_hammer }
							20 = {
								modifier = {
									factor = 0
									religion_group = christian
								}
								give_nickname = nick_the_sword_of_god
							}
						}
					}
				}
			}
		}
	}

	on_reverse_demand = {
		ROOT = {
			show_scope_change = no
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -200
			religion_authority = {
				modifier = lost_crusade
				years = 20
			}
		}
		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
						}
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
			hidden_tooltip = {
				if = {
					limit = {
						OR = {
							this_has_lame_nickname_trigger = yes
							has_nickname = no
						}
					}
					hidden_effect = {
						random_list = {
							10 = { give_nickname = nick_the_holy }
							10 = { give_nickname = nick_the_glorious }
							10 = { give_nickname = nick_the_lionheart }
							10 = { give_nickname = nick_the_great }
							10 = { give_nickname = nick_the_hammer }
							20 = {
								modifier = {
									factor = 0
									religion_group = christian
								}
								give_nickname = nick_the_sword_of_god
							}
						}
					}
				}
			}
		}
		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
	}
}

# Crusade using the new crusade system. See crusades.info in the religion folder for documentation
new_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
	is_crusade = yes
	allow_whitepeace = no
	
	allowed_to_target_tributaries = no
	can_ask_to_join_war = no # People join via script instead
	
	sort_priority = 3000

	can_use_gui = {
		ROOT = {
			uses_new_crusade = yes
			has_called_crusade = no
			religion_authority = 0.05
			conditional_tooltip = {
				trigger = {
					is_alternate_start = yes
					has_alternate_start_parameter = {
						key = religion_names
						value = random
					}
				}
				custom_tooltip = {
					text = ALT_GHW_STARTED
					has_global_flag = muslim_jihads_unlocked
					has_global_flag = pagan_ghws_unlocked
					has_global_flag = christian_crusades_unlocked
				}
			}
			conditional_tooltip = {
				trigger = {
					NOT = {
						has_alternate_start_parameter = {
							key = religion_names
							value = random
						}
					}
				}
				religion_group = christian
				custom_tooltip = {
					text = CTT_CHRISTIAN_CRUSADES_UNLOCKED
					has_global_flag = christian_crusades_unlocked
				}
			}
			mercenary = no
		}
	}

	can_use = {
		ROOT = {
			uses_new_crusade = yes
			can_call_crusade = yes
			hidden_tooltip = {
				NOT = { has_character_modifier = cancel_crusade_cooldown }
			}
		}
		
		FROM = {
			NOT = { religion = ROOT}
			OR = {
				NOT = { religion_group = ROOT }
				AND = {
					has_character_flag = fourth_crusade_target
					NOR = { 
						has_character_flag = fourth_crusade_recipient 
						religion = ROOT
					}
				}
				AND = {
					has_global_flag = 4th_crusade_official
					has_landed_title = e_byzantium
					NOT = { religion = ROOT }
				}
				is_heresy_of = ROOT
				is_parent_religion = ROOT
				AND = {
					religion_group = pagan_group
					NOT = { religion = ROOT }
				}
			}
		}
		
		NOT = { # Not if the Schism has been mended
			AND = {
				OR = {
					religion = catholic
					religion = fraticelli
				}
				has_global_flag = schism_mended
			}
		}
	}

	can_use_title = {
		FROM = {
			any_realm_title = {
				de_jure_liege_or_above = PREVPREV
			}
			
			independent = yes # Don't declare Crusades on revolters - chances are high the war will invalidate
			
			OR = {
				num_of_realm_counties = {
					value = 6
					title = PREV
				}
				PREV = {
					title = k_thrace	
					FROM = { 
						has_landed_title = e_byzantium 
						religion_group = christian
					}
				} 
			}
		}
	}
	
	is_valid = {
		ROOT = {
			can_call_crusade = yes
		}
		FROM = {
			NOT = { religion = ROOT}
			OR = {
				NOT = { religion = 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_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_title = {
		ROOT = {
			crusade_target_title = {
				save_event_target_as = new_crusade_title
			}
		}

		ROOT = {
			official_crusade_recipient = {
				save_event_target_as = new_crusade_recipient
			}
		}

		most_participating_crusader = {
			save_event_target_as = new_crusade_most_participating_member
		}

		ROOT = {
			crusade_target_char = {
				save_event_target_as = new_crusade_target_char
			}
		}

		custom_tooltip = {
			text = new_crusade_win_TT
		}

		# Religion Authority and Prestige/Piety for the winner (Pope)
		ROOT = {
			show_scope_change = no
			participation_scaled_prestige = 500
			participation_scaled_piety = 1000
			religion_authority = {
				modifier = won_crusade
				years = 20
			}
		}
		
		# Religion Authority and Prestige/Piety loss for the loser (Target of Crusade)
		FROM = {
			piety = -100
			religion_authority = {
				modifier = lost_crusade
				years = 20
			}			
		}

		hidden_tooltip = {
		
			if = { # Achievement
				limit = {
					event_target:new_crusade_target_char = {
						has_landed_title = e_byzantium
						religion_group = FROM
					}
				}
				any_attacker = {
					limit = {
						ai = no
					}
					set_character_flag = achievement_venetian_guile
				}
			}

			if = {
				limit = {
					ai = yes
					NOT = {
						crusade_beneficiary = {
							always = yes
						}
					}
				}
				character_event = { id = HFP.41007 }
			}
		
			# Should we give the seized land to the Official Crusade Recipient?
			if = {
				limit = {
					most_participating_crusader = {
						NOT = {
							has_character_flag = crusade_selfish_stance
							has_character_flag = crusade_beneficiary_stance
						}
						official_crusade_recipient = {
							is_alive = yes
							religion = ROOT
						}
					}
				}
				most_participating_crusader = {
					official_crusade_recipient = {
						set_crusader_king = this
					}
				}
			}

			# Give the seized land to the main contributor of the Crusade or his/her Beneficiary
			else_if = {
				limit = {
					most_participating_crusader = {
						holy_order = no
						mercenary = no
						NOR = {
							has_landed_title = k_papal_state
							has_landed_title = d_fraticelli
						}
						OR = {
							AND = {
								ai = yes
								crusade_beneficiary = {
									is_alive = yes
								}
							}
							ai = no
						}
					}
				}
				most_participating_crusader = {
					# Give the title to your Beneficiary
					if = {
						limit = {
		                    crusade_beneficiary = {
	                            is_alive = yes
	                            is_landed = no
								NOT = {
									any_heir_title = {
										always = yes
									}
								}
	                            mercenary = no
	                            holy_order = no
		                    }
		                    NOT = {
		                    	has_character_flag = crusade_selfish_stance
		                    }
						}
						crusade_beneficiary = {
							set_crusader_king = THIS
							set_character_flag = crusader_king_flag
						}
					}
					# Find an appropriate replacement Beneficiary if there is none selected, or the currently selected one is improper in one way or another
					else_if = {
						limit = {
							ai = yes
		                    any_dynasty_member = {
	                    		age = 16
								NOT = {
									any_heir_title = {
										always = yes
									}
								}
	                    		is_landed = no
	                    		mercenary = no
	                    		holy_order = no
	                    		religion = PREV
		                    }
							NOT = {
								has_character_flag = crusade_selfish_stance
							}
						}
						random_dynasty_member = {
							limit = {
								age = 16
								NOT = {
									any_heir_title = {
										always = yes
									}
								}
								is_landed = no
								mercenary = no
								holy_order = no
								religion = PREV
							}
							preferred_limit = {
								is_child_of = PREV
								culture = PREV
								NOT = {
									age = 45
								}
								preferred_gender_based_on_laws_trigger = yes
							}
							preferred_limit = {
								is_child_of = PREV
								NOT = {
									age = 45
								}
								preferred_gender_based_on_laws_trigger = yes
							}
							preferred_limit = {
								is_child_of = PREV
								preferred_gender_based_on_laws_trigger = yes
							}
							preferred_limit = {
								is_liege_or_above = PREV
								culture = PREV
								NOT = {
									age = 45
								}
								preferred_gender_based_on_laws_trigger = yes		
							}
							preferred_limit = {
								is_liege_or_above = PREV
								NOT = {
									age = 45
								}
								preferred_gender_based_on_laws_trigger = yes		
							}
							preferred_limit = {
								is_liege_or_above = PREV
								preferred_gender_based_on_laws_trigger = yes		
							}
							preferred_limit = {
								is_child_of = PREV
								culture = PREV
								NOT = {
									age = 45
								}
							}
							preferred_limit = {
								is_child_of = PREV
								NOT = {
									age = 45
								}
							}
							preferred_limit = {
								is_child_of = PREV
							}
							preferred_limit = {
								is_liege_or_above = PREV
								culture = PREV
								NOT = {
									age = 45
								}
							}
							preferred_limit = {
								is_liege_or_above = PREV
								NOT = {
									age = 45
								}
							}
							preferred_limit = {
								is_liege_or_above = PREV
							}
							preferred_limit = {
								culture = PREV
								NOT = {
									age = 45
								}
							}
							preferred_limit = {
								NOT = {
									age = 45
								}
							}
							preferred_limit = {
								culture = PREV
							}
							set_crusader_king = THIS
							set_character_flag = crusader_king_flag
						}
					}
					# Take the titles yourself
					else = {
						if = {
							limit = {
								any_owned_bloodline = {
									has_bloodline_flag = grand_crusader_bloodline
									bloodline_is_active_for = PREV
								}
							}
							set_crusade_pot_multiplier = 0.2
						}
						else = {
							set_crusade_pot_multiplier = 0
						}
						set_crusader_king = THIS
						set_character_flag = crusader_king_flag
					}
				}
			}

			# If not applicable, find someone else to take the title
			else = {
				# Find a random Beneficiary within certain limits
				if = {
					limit = {
						most_participating_crusader = {
							any_crusade_participant = {
								is_alive = yes
								is_landed = yes
								holy_order = no
								mercenary = no
								crusade_beneficiary = {
									is_alive = yes
									is_landed = no
									holy_order = no
									NOT = {
			                            any_heir_title = { 
			                                always = yes
			                            }
									}
									age = 12
								}
							}
						}
					}
					most_participating_crusader = {
						random_crusade_participant = {
							limit = {
								is_alive = yes
								is_landed = yes
								holy_order = no
								mercenary = no
								crusade_beneficiary = {
									is_alive = yes
									is_landed = no
									holy_order = no
									NOT = {
			                            any_heir_title = { 
			                                always = yes
			                            }
									}
									age = 12
								}
							}
							preferred_limit = {
								tier = EMPEROR
							}
							preferred_limit = {
								tier = KING
							}
							crusade_beneficiary = {
		    		        	set_crusader_king = THIS
				                set_character_flag = crusader_king_flag
							}
						}
					}
				}
				# Find any random Beneficiary, if possible
				else_if = {
					limit = {
						most_participating_crusader = {
							any_crusade_participant = {
								crusade_beneficiary = {
									is_alive = yes
									is_landed = no
									holy_order = no
									NOT = {
										any_heir_title = {
											always = yes
										}
									}
								}
							}
						}
					}
					most_participating_crusader = {
						random_crusade_participant = {
							limit = {
								crusade_beneficiary = {
									is_alive = yes
									is_landed = no
									holy_order = no
									NOT = {
										any_heir_title = {
											always = yes
										}
									}
								}
							}
							crusade_beneficiary = {
								set_crusader_king = THIS
								set_character_flag = crusader_king_flag
							}
						}
					}
				}
				# Find a logical "Beneficiary" from any playable Catholic/Fraticelli ruler, if all else fails
				else = {
					most_participating_crusader = {
						# Try to find a Catholic/Fraticelli ruler
						if = {
							limit = {
								any_playable_ruler = {
									religion = PREV
									any_dynasty_member = {
										is_alive = yes
										is_dying = no
										mercenary = no
										holy_order = no
										age = 12
										NOT = {
											any_heir_title = {
												always = yes
											}
										}
									}
								}
							}
							random_playable_ruler = {
								limit = {
									religion = PREV
									any_dynasty_member = {
										is_alive = yes
										is_dying = no
										mercenary = no
										holy_order = no
										age = 12
										NOT = {
											any_heir_title = {
												always = yes
											}
										}
									}
								}
								preferred_limit = {
									tier = EMPEROR
									culture = PREV
								}
								preferred_limit = {
									tier = EMPEROR
								}
								preferred_limit = {
									tier = KING
									culture = PREV
								}
								preferred_limit = {
									tier = KING
								}
								preferred_limit = {
									tier = DUKE
									culture = PREV
								}
								preferred_limit = {
									tier = DUKE
								}
								preferred_limit = {
									culture = PREV
								}
								random_dynasty_member = {
									limit = {
										religion = PREV
										is_alive = yes
										is_dying = no
										mercenary = no
										holy_order = no
										age = 12
										NOT = {
											any_heir_title = {
												always = yes
											}
										}
									}
									set_crusader_king = THIS
								}
							}
						}
						# If every other option fails, make the Holy Order Grandmaster the Crusader King
						else = {
							set_crusader_king = THIS
						}
					}
				}
			}

			# Find a backup beneficiary for the other Crusaders, if they don't have one already selected
			any_attacker = {
				limit = {
					NOT = {
						most_participating_crusader = {
							character = PREV
						}
					}
					ai = yes
					OR = {
	                	crusade_beneficiary = {
	                    	OR = {
	                        	is_alive = no
	                        	is_landed = yes
								NOT = {
									any_heir_title = {
										always = yes
									}
								}
	                        	mercenary = yes
	                        	holy_order = yes
	                    	}
	                	}
	                	NOT = {
	                    	crusade_beneficiary = {
	                        	always = yes
	                    	}
	                	}
	                }
				}
				save_event_target_as = crusade_attacker
				character_event = { id = HFP.41007 }
			}

			# Make sure selfish Crusaders get as little as possible (reduce the pot for Selfish Crusaders)
			any_attacker = {
				limit = {
					has_character_flag = crusade_selfish_stance
				}
				set_crusade_pot_multiplier = 0
			}

			# Distribute the Pot and Titles
			ROOT = {
				set_global_flag = distributing_crusade
				any_crusade_participant = {
					limit = {
						ai = no
						most_participating_crusader = {
							NOT = {
								character = PREV
							}
						}
					}
					character_event = { id = HFP.41060 }
				}
				distribute_crusade_pot = {
					amount = 1
				}
				any_crusade_participant = {
					crusade_beneficiary = {
						set_character_flag = beneficiary_who_received_titles
					}
				}
				# If the winner is a selfish Nomad
				if = {
					limit = {
						most_participating_crusader = {
							is_nomadic = yes
							OR = {
								has_character_flag = crusade_selfish_stance
								crusader_king = {
									character = PREV
								}
							}
						}
					}
					most_participating_crusader = {
						gain_settlements_under_title = {
							title = PREVPREV
							enemy = FROM
							type = holy_war
						}
					}
					any_playable_ruler = {
						narrative_event = { id = HFP.41160 }
					}
				}
				# If it is a Fourth Crusade
				else_if = {
					limit = {
						has_global_flag = 4th_crusade_official
						crusade_target_title = {
							title = k_thrace
						}
						FROM = {
							religion_group = ROOT
						}
						FROM = {
							NOT = {
								religion = ROOT
							}
						}
						crusade_target_char = {
							e_byzantium = {
								owner = {
									character = PREVPREV
								}
							}
						}
					}
					# Find a King for Trebizond
					e_byzantium = {
						owner = {
							if = {
								limit = {
									NOT = {
										any_playable_ruler = {
											has_landed_title = k_trebizond
										}
									}
								}
								random_vassal = {
									limit = {
										OR = {
											has_landed_title = d_trebizond
											has_landed_title = d_armeniacon
										}
									}
									preferred_limit = {
										has_landed_title = d_trebizond
									}
									grant_title_no_opinion = k_trebizond
								}
							}

						}
					}
					# Set vassals for Trebizond
					k_trebizond = {
						owner = {
							e_byzantium = {
								owner = {
									any_vassal = {
										limit = {
											de_jure_liege = k_trebizond
										}
										set_defacto_liege = PREVPREVPREV
									}
								}
							}
						}
					}
					# Find a Catholic/Fraticelli lord for Cyprus
					if = {
						limit = {
							c_famagusta = {
								owner = {
									religion = FROM
									culture = FROM
									ai = yes
								}
							}
							c_limisol = {
								owner = {
									religion = FROM
									culture = FROM
									ai = yes
								}
							}
							k_cyprus = {
								has_holder = no
							}
						}
						any_crusade_participant = {
							limit = {
								ai = yes
								NOT = {
									most_participating_crusader = {
										character = PREV
									}
								}
							}
							crusade_beneficiary = {
								grant_title_no_opinion = c_famagusta
								grant_title_no_opinion = c_limisol
								grant_title_no_opinion = d_cyprus
								grant_title_no_opinion = k_cyprus
								d_cyprus = {
									de_jure_liege = k_cyprus
								}
								set_defacto_liege = THIS
								character_event = { id = HFP.41090 days = 1 }
								if = {
									limit = {
										NOR = {
											trait = crusader
											trait = crusader_king
											trait = crusader_queen
										}
									}
									add_trait = crusader
								}
							}
							character_event = { id = HFP.41007 }
						}
					}
					distribute_crusade_target_title = yes
					#The Crusader King becomes an Emperor and gets a bloodline
					crusader_king = {
						grant_title_no_opinion = e_latin_empire
					}
					# Find random vassal and make a king out of them
					e_byzantium = {
						owner = {
							random_vassal = {
								limit = {
									tier = DUKE
									ai = yes
								}
								preferred_limit = {
									de_jure_liege = k_byzantium
								}
								preferred_limit = {
									de_jure_liege = k_anatolia
								}
								preferred_limit = {
									de_jure_liege = k_sicily
								}
								primary_title = {
									create_title = {
										tier = KING
										landless = no
										temporary = no
										name = "fourth_crusade_greek_kingdom_name"
										holder = PREV
										culture = PREV
										custom_created = yes
										base_title = THIS
										copy_title_laws = yes
									}
								}
							}
						}
					}
					# All Byzantine vassals go independent except those neighboring the old Emperor, and he/she gets an inferior Empire title while the Byzantine title is broken
					e_byzantium = {
						owner = {
							any_vassal = {
								limit = {
									higher_tier_than = BARON
								}
								set_defacto_liege = this
							}
							if = {
								limit = {
									primary_title = {
										title = e_byzantium
									}
									has_landed_title = c_nikaea
								}
								grant_title = e_nicaea
								e_nicaea = {
									copy_title_laws = e_byzantium
									copy_title_history = e_byzantium
									make_primary_title = yes
								}
								any_demesne_title = {
									limit = {
										tier = EMPEROR
										NOT = {
											title = e_nicaea
										}
									}
									destroy_landed_title = THIS
								}
								any_neighbor_independent_ruler = {
									limit = {
										ai = yes
										culture = PREV
										religion = PREV
										lower_tier_than = KING
									}
									set_defacto_liege = PREV
								}
							}
							else_if = {
								limit = {
									primary_title = {
										title = e_byzantium
									}
								}
								capital_scope = {
									create_title = {
										tier = EMPEROR
										landless = no
										temporary = no
										name = "fourth_crusade_greek_empire_name"
										holder = PREV
										culture = PREV
										custom_created = yes
										base_title = THIS
									}
								}
								any_demesne_title = {
									limit = {
										tier = EMPEROR
										is_titular = no
									}
									destroy_landed_title = THIS
								}
							}
							any_neighbor_independent_ruler = {
								limit = {
									ai = yes
									culture = PREV
									religion = PREV
									lower_tier_than = KING
								}
								set_defacto_liege = PREV
							}
						}
					}
					# Crete is taken by a Merchant Lord
					if = {
						limit = {
							c_kaneia = {
								owner = {
									ai = yes
								}
							} 
							c_chandax = {
								owner = {
									ai = yes
								}
							}
							any_crusade_participant = {
								is_patrician = yes
								higher_tier_than = COUNT
							}
						}
						random_crusade_participant = {
							limit = {
								is_patrician = yes
								higher_tier_than = COUNT
							}
							preferred_limit = {
								has_landed_title = k_venice
							}
							preferred_limit = {
								has_landed_title = d_venice
							}
							preferred_limit = {
								tier = EMPEROR
							}
							preferred_limit = {
								tier = KING
							}
							grant_title_no_opinion = d_krete 
							grant_title_no_opinion = c_kaneia
							grant_title_no_opinion = c_chandax
						}
					}

					if = {
						limit = {
							official_crusade_recipient = {
								has_character_flag = fourth_crusade_recipient
							}
						}
						official_crusade_recipient = {
							save_event_target_as = byzantine_refugee
						}
					}
					# Narrative event explaining that Byzantium has been taken out
					any_character = {
						narrative_event = { id = HF.49008 }
					}
					event_target:byzantine_refugee = {
						death = {
							death_reason = death_battle
							killer = FROM
						}
					}
					# Making sure the Byzantine Empire is destroyed
					e_byzantium = {
						unsafe_destroy_landed_title = THIS
					}
				}
				# Otherwise
				else = {
					FROM = {
						any_realm_character = {
							limit = {
								religion = ROOT
								ai = no
								higher_tier_than = BARON
							}
							set_defacto_liege = THIS
						}
					}
					distribute_crusade_target_title = yes
					crusader_king = {
						save_event_target_as = crusade_king_recipient
					}
					any_playable_ruler = {
						narrative_event = { id = HFP.41160 }
					}
				}
				crusader_king = {
					if = {
						limit = {
							has_character_flag = beneficiary_who_received_titles
							NOT = { has_dynasty_modifier = ruling_in_crusader_kingdom }
						}
						add_dynasty_modifier = ruling_in_crusader_kingdom
					}
					any_realm_character = {
						limit = {
							has_character_flag = beneficiary_who_received_titles 
							NOT = { has_dynasty_modifier = ruling_in_crusader_kingdom }
						}
						add_dynasty_modifier = ruling_in_crusader_kingdom
					}
				}
				clr_global_flag = distributing_crusade
			}

			# If the CK has too much gold
			crusader_king = {
				if = {
					limit = {
						wealth = 2000 
						ai = yes
						any_vassal = {
							always = yes
						}
					}
					set_variable = { which = count value = 20 }
					while = {
						limit = {
							AND = {
								wealth = 2000
								check_variable = { which = count value = 1 }
							}
						}
						random_vassal = {
							limit = {
								NOT = {
									wealth = 1000
								}
							}
							wealth = 350
							crusader_king = {
								wealth = -350
							}
						}
						subtract_variable = { which = count value = 1 }
					}
				}
			}
			
			# Achievement
			any_attacker = {
				limit = {
					ai = no
					NOT = { has_character_flag = achievement_crusader }
				}
				set_character_flag = achievement_crusader
			}

			# Nickname for the most participating Crusader
			most_participating_crusader = {
				if = {
					limit = {
						can_apply_average_nickname_trigger = yes
					}
					random_list = {
						10 = { give_nickname = nick_the_crusader }
						10 = { give_nickname = nick_the_glorious }
						10 = { give_nickname = nick_the_lionheart }
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_hammer }
						40 = { give_nickname = nick_the_sword_of_god }
					}
				}
			}

			# Nickname and effects for the new Crusader King, if it is a Beneficiary or the Official Crusade Recipient (not for Selfish Crusaders)
			crusader_king = {
				hidden_tooltip = {
					if = {
						limit = {
							OR = {
								most_participating_crusader = {
									NOT = {
										character = PREV
									}
								}
								ROOT = {
									official_crusade_recipient = {
										character = PREV
									}
								}
							}
						}
						opinion = {
							who = most_participating_crusader
							modifier = opinion_granted_crusader_kingdom
							years = 15
						}
						crusader_king = {
							setting_up_the_crusader_kingdom_effect = yes
							create_crusader_king_crown_effect = yes
						}
					}
				}
			}

			crusader_king = {
				save_event_target_as = new_crusader_king
			}

			# If the player is the most participating Crusader, and gave away the Kingdom to a Beneficiary, trigger event to let them start playing as Beneficiary
			if = {
				limit = {
					most_participating_crusader = {
						ai = no
						NOT = {
							character = event_target:new_crusader_king
						}
						event_target:new_crusader_king = {
							dynasty = PREV
						}
					}
				}
				most_participating_crusader = {
					narrative_event = { id = HFP.41054 days = 3 }
				}
			}

			# Check most participating Crusader if they are potential Crusader material
			most_participating_crusader = {
				if = {
					limit = {
						NOR = {
							has_character_flag = grandCrusader
							trait = crusader_king
						}
					}
					set_character_flag = grandCrusader
				}
				else_if = {
					limit = {
						OR = {
							has_character_flag = grandCrusader
							trait = crusader_king
						}
					}
					narrative_event = { id = HF.49020 }
				}
			}

			# Release the CK if they are in jail
			crusader_king = {
				imprison = no
			}

			# Special bloodline for the recipient of the first Crusade
			if = {
				limit = {
					NOT = {
						has_global_flag = first_crusade_complete
					}
					most_participating_crusader = {
						NOT = {
							crusader_king = {
								character = PREV
							}
						}
					}
				}
				if = {
					limit = {
						has_dlc = "Holy Fury"
					}
					if = {
						limit = {
							crusader_king = {
								is_female = yes
							}
						}
						crusader_king = {
							create_bloodline = {
								type = crusade_lord
								religion = THIS
								inheritance = matrilineal
							}
							set_bloodline_founder_religion_flag_effect = yes
						}
					}
					else = {
						crusader_king = {
							create_bloodline = {
								type = crusade_lord
								religion = THIS
								inheritance = patrilineal
							}
							set_bloodline_founder_religion_flag_effect = yes
						}
					}
				}
			}

			# Checks if First Crusade
			if = {
				limit = {
					NOT = {
						has_global_flag = first_crusade_complete
					}
				}
				set_global_flag = first_crusade_complete
			}

			# Adds a negative opinion modifier for selfish Crusaders
			any_crusade_participant = {
				limit = {
					OR = {
						has_character_flag = crusade_selfish_stance
						AND = {
							NOR = {
								has_character_flag = crusade_papal_stance
                				has_character_flag = crusade_beneficiary_stance
							}
							most_participating_crusader = {
								crusader_king = {
									character = PREV
									character = PREVPREV
								}
							}
						}
					}
				}
				if = {
					limit = {
						NOT = {
							ROOT = {
								has_opinion_modifier = {
									who = PREVPREV
									modifier = opinion_selfish_crusader
								}
							}
						}
					}
					ROOT = {
						opinion = {
							who = PREVPREV
							modifier = opinion_selfish_crusader
							years = 20
						}
					}
				}
			}

			# Clean up stances
			any_attacker = {
				clr_character_flag = crusade_selfish_stance
				clr_character_flag = crusade_beneficiary_stance
				clr_character_flag = crusade_papal_stance
			}

			ROOT = {
				character_event = { id = HFP.41199 }
			}
		}

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

	on_fail = {
		ROOT = {
			character_event = { id = HFP.41199}
		}

		ROOT = {
			show_scope_change = no
			piety = -100
		}
		
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_piety = 200
				participation_scaled_prestige = 100
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -50
				}
			}
		}
		
		FROM = {
			participation_scaled_piety = 200
			participation_scaled_prestige = 100
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -50
			}
			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
						}
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
			
			hidden_tooltip = {
				if = {
					limit = {
						OR = {
							this_has_lame_nickname_trigger = yes
							has_nickname = no
						}
					}
					random_list = {
						10 = { give_nickname = nick_the_holy }
						10 = { give_nickname = nick_the_glorious }
						10 = { give_nickname = nick_the_lionheart }
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_hammer }
						20 = {
							modifier = {
								factor = 0
								religion_group = christian
							}
							give_nickname = nick_the_sword_of_god
						}
					}
				}
			}
		}
	}

	on_reverse_demand = {
		ROOT = {
			character_event = { id = HFP.41199 }
		}

		ROOT = {
			show_scope_change = no
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -200
			religion_authority = {
				modifier = lost_crusade
				years = 20
			}
		}
		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
						}
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
			hidden_tooltip = {
				if = {
					limit = {
						OR = {
							this_has_lame_nickname_trigger = yes
							has_nickname = no
						}
					}
					random_list = {
						10 = { give_nickname = nick_the_holy }
						10 = { give_nickname = nick_the_glorious }
						10 = { give_nickname = nick_the_lionheart }
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_hammer }
						20 = {
							modifier = {
								factor = 0
								religion_group = christian
							}
							give_nickname = nick_the_sword_of_god
						}
					}
				}
			}
		}
		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
			}
		}

		# Checks if First Crusade
		if = {
			limit = {
				NOT = {
					has_global_flag = first_crusade_complete
				}
			}
			set_global_flag = first_crusade_complete
		}
		any_defender = {
			limit = {
				ai = no
				religion_group = pagan_group
			}
			set_character_flag = achievement_pagan_fury
		}

		# Chance to start a Shepherd's Crusade
		random = {
			chance = 15
			if = {
				limit = {
					has_game_rule = {
						name = shepherds_crusade
						value = on
					}
					ROOT = {
						religion = catholic
					}
				}
				set_global_flag = catholic_shepherds_crusade
			}
			else_if = {
				limit = {
					has_game_rule = {
						name = shepherds_crusade
						value = on
					}
					ROOT = {
						religion = fraticelli
					}
				}
				set_global_flag = fraticelli_shepherds_crusade
			}
			ROOT = {
				character_event = { id = HFP.42300 days = 3}
			}
		}
	}

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

# Regular Holy War CB
religious = {
	name = CB_NAME_RELIGIOUS
	war_name = WAR_NAME_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 = no
	
	sort_priority = 800
	
	can_use_gui = {
		ROOT = {
			OR = {
				NOT = { religion = buddhist }
				piety = 250
			}
		}
	}
	
	on_add = {
		ROOT = {
			show_scope_change = no
			if = {
				limit = {
					religion = buddhist
				}
				piety = -250
			}
		}
		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 = {
		ROOT = {
			is_nomadic = no
			NOT = { religion_group = pagan_group }
			NOT = { pacifist = yes }
			NOT = { religion = taoist }
			NAND = {
				OR = {
					has_landed_title = k_teutonic_state
					has_landed_title = d_teutonic_order
				}
				has_global_flag = northern_crusades_active
			}
			NOR = {
				has_character_flag = currently_reconquering_iberia
				reconquista_check_trigger = yes
			}
			OR = {
				NOT = { religion = hindu }
				FROM = { NOT = { has_dharmic_religion_trigger = yes } }
				AND = {
					religion_group = FROM
					FROM = {
						has_character_modifier = known_satanist
					}
					NOT = {
						has_character_modifier = known_satanist
					}
				}
			}
			NOT = { same_realm = FROM }
			OR = {
				NOT = { religion_group = FROM }
				is_heresy_of = FROM
				is_parent_religion = FROM
				AND = {
					has_dharmic_religion_trigger = yes
					FROM = { religion = taoist }
				}
				AND = {
					religion_group = muslim
					NOT = { religion = FROM }
					FROM = {
						NOT = { 
							any_current_enemy = {
								NOT = {
									religion_group = muslim
								}
							}
						}
					}
				}
				AND = {
					religion_group = FROM
					FROM = {
						has_character_modifier = known_satanist
					}
					NOT = {
						has_character_modifier = known_satanist
					}
				}
			}
			top_liege = {
				religion_group = ROOT
				OR = {
					NOT = { religion = FROM }
					AND = {
						religion = FROM
						FROM = {
							has_character_modifier = known_satanist
						}
						NOT = {
							has_character_modifier = known_satanist
						}
						ROOT = {
							NOT = {
								has_character_modifier = known_satanist
							}
						}
					}
				}
				OR = {
					religion = ROOT
					AND = {
						is_heretic = no
						ROOT = { is_heretic = no }
					}
				}
			}
			mercenary = no
		}
	}
	
	can_use_title = {
		# 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_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
								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 = { religion_group = pagan_group }
			NOT = { pacifist = yes }
			NOT = { same_realm = FROM }
			OR = {
				NOT = { religion_group = FROM }
				is_heresy_of = FROM
				is_parent_religion = FROM
				AND = {
					has_dharmic_religion_trigger = yes
					FROM = { religion = taoist }
				}
				AND = {
					religion_group = FROM
					FROM = {
						has_character_modifier = known_satanist
					}
					ROOT = {
						NOT = {
							has_character_modifier = known_satanist
						}
					}
				}
				AND = {
					religion_group = muslim
					NOT = { religion = FROM }
				}
			}
			top_liege = {
				OR = {
					NOT = { religion = FROM }
					AND = {
						religion = FROM
						FROM = {
							has_character_modifier = known_satanist
						}
						NOT = {
							has_character_modifier = known_satanist
						}
						ROOT = {
							NOT = {
								has_character_modifier = known_satanist
							}
						}
					}
				}
			}
		}
	}
	
	on_success = {
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_piety = 100
				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 = 100
			participation_scaled_prestige = 50
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
			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 = { show_scope_change = no piety = -100 }
		FROM = { show_scope_change = no prestige = -50 }
	}

	on_success_title = {
		if = { # Standard Holy War behaviour for Non-Muslims and Muslims fighting other Muslims
			show_scope_change = no
			limit = {
				OR = {
					ROOT = {
						NOT = {
							religion_group = muslim
						}
					}
					AND = {
						ROOT = {
							religion_group = muslim
						}
						FROM = {
							religion_group = muslim
						}
					}
				}
			}
			custom_tooltip = {
				text = religious_cb_succ_tip
				hidden_tooltip = {
					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 = { # Muslims fighting Non-Muslims preserve vassals
			show_scope_change = no
			limit = {
				ROOT = {
					religion_group = muslim
				}
				FROM = {
					NOT = {
						religion_group = muslim
					}
				}
			}
			custom_tooltip = {
				text = religious_cb_succ_muslim_tip
				hidden_tooltip = {
					ROOT = {
						vassalize_or_take_under_title = {
							title = PREV
							enemy = FROM
							is_religious = yes
							type = holy_war
						}
					}
				}
			}
		}

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

	on_fail = {
		ROOT = {
			show_scope_change = no
			piety = -50 
			prestige = -100
		}
	}

	on_reverse_demand = {
		ROOT = {
			show_scope_change = no
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -75
			prestige = -150
		}
		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
				NOT = {
					any_liege = {
						dynasty = PREV
					}
				}
			}
			participation_scaled_decadence = 20
		}
		any_attacker = {
			limit = {
				NOT = { character = ROOT }
				uses_decadence = yes
				NOT = {
					any_liege = {
						dynasty = PREV
					}
				}
			}
			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 = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_nomadic = no
				NOT = {
					is_tribal = yes
				}
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}
				}
			}
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_tribal = yes
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
							holding_type = tribal
						}
					}
				}
			}
		}
		
		# Try to guide the Hordes in the right direction
		modifier = {
			factor = 0.1
			ROOT = {
				OR = {
					dynasty = 613 # Seljuks
					dynasty = 800 # Timurids
					primary_title = { title = e_il-khanate }
				}
			}
			OR = {
				empire = {
					NOT = {
						OR = {
							title = e_persia
							title = e_byzantium
							title = e_arabia
						}
					}
				}
				FROM = {
					primary_title = { title = e_golden_horde }
					NOT = { year = 1300 }
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = {
				primary_title = { title = e_golden_horde }
			}
			OR = {
				empire = {
					OR = {
						title = e_persia
						title = e_byzantium
						title = e_arabia
					}
				}
				FROM = {
					primary_title = { title = e_il-khanate }
					NOT = { year = 1300 }
				}
			}
		}
		modifier = {
			factor = 2.0
			OR = {
				culture = persian
				culture = baloch
				culture = afghan
				culture = turkish
			}
			empire = {
				OR = {
					title = e_persia
					title = e_rajastan
				}
			}
		}
	}
}

excommunicated_ruler = {
	name = CB_NAME_EXCOMMUNICATED_RULER
	war_name = WAR_NAME_EXCOMMUNICATED_RULER
	sprite = 11
	truce_days = 3650
	
	is_permanent = yes
	can_ask_to_join_war = no
	
	defender_unoccupied_warscore = yes

	allowed_to_target_tributaries = no
	
	sort_priority = 750

	can_use = {
		FROM = {
			excommunicated_for = ROOT
			in_revolt = no
			NOT = { 
				any_current_enemy = {
					NOT = {
						religion_group = ROOT
					}
				}
			}
		}
		ROOT = {
			NOT = { excommunicated_for = ROOT }
			mercenary = no
		}
	}

	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	is_valid = {
		FROM = {
			custom_tooltip = {
				text = tooltip_is_excommunicated_for
				hidden_tooltip = {
					excommunicated_for = ROOT
				}
			}
		}
		OR = { 
			ROOT = { has_character_flag = flag_excommunicated_for_coronation }
			ROOT = { NOT = { excommunicated_for = ROOT } }
		}
	}

	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_invalidation = {
		# Coronation request locks cleared on invalidation or loss, to avoid locking the player in the middle of the negotiation forever.
		hidden_tooltip = {
			if = {
				limit = {
					ROOT = { has_character_flag = flag_excommunicated_for_coronation }
				}
				ROOT = {
					character_event = { id = HF.20220 } #Papal Coronation may proceed.
				}
			}
		}
	}

	on_success = {
		if = {
			limit = { 
				ROOT = { has_character_flag = flag_excommunicated_for_coronation }
				}
			ROOT = {
				character_event = { id = HF.20220 } #Papal Coronation may proceed.
			}
		}
		ROOT = {
			if = {
				limit = {
					FROM = {
						primary_title = {
							tier = BARON
						}
					}
				}
				ROOT = {
					piety = 10
					prestige = 50
				}
			}
			if = {
				limit = {
					FROM = {
						primary_title = {
							tier = COUNT
						}
					}
				}
				ROOT = {
					piety = 20
					prestige = 100
				}
			}
			if = {
				limit = {
					FROM = {
						primary_title = {
							tier = DUKE
						}
					}
				}
				ROOT = {
					piety = 30
					prestige = 150
				}
			}
			if = {
				limit = {
					FROM = {
						primary_title = {
							tier = KING
						}
					}
				}
				ROOT = {
					piety = 50
					prestige = 200
				}
			}
			if = {
				limit = {
					FROM = {
						primary_title = {
							tier = EMPEROR
						}
					}
				}
				ROOT = {
					piety = 70
					prestige = 400
				}
			}
		}

		FROM = {
			excommunicate = no
			prestige = -200
			rightful_religious_head_scope = {
				opinion = {
					who = PREV
					modifier = opinion_repented_sins
				}
			}
			abdicate = yes
		}
	}

	on_fail = {
		ROOT = {
			piety = -50
			prestige = -100
		}
		# Coronation request locks cleared on invalidation or loss, to avoid locking the player in the middle of the negotiation forever.
		hidden_tooltip = {
			ROOT = {
				clr_character_flag = flag_excommunicated_for_coronation 
			}
		}
	}

	on_reverse_demand = {
		ROOT = {
			piety = -100
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			prestige = 200
		}
		# Coronation request locks cleared on invalidation or loss, to avoid locking the player in the middle of the negotiation forever.
		hidden_tooltip = {
			ROOT = {
				clr_character_flag = flag_excommunicated_for_coronation 
			}
		}
	}


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

bid_for_independence = {
	name = CB_NAME_INDEPENDENCE
	war_name = WAR_NAME_INDEPENDENCE
	sprite = 12
	truce_days = 3650
	
	is_permanent = yes
	is_independence = yes
	
	sort_priority = 1500

	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		ROOT = {
			de_facto_liege = FROM
			
			higher_tier_than = BARON
			OR = {
				ai = yes
				is_feudal = yes
				is_tribal = yes
				higher_tier_than = COUNT
			}
			mercenary = no
			
			OR = {
				is_nomadic = no
				NOT = {
					clan_opinion = { who = FROM value = 0 }
				}
			}
		}

		FROM = {
			NOT = {
				any_war = {
					attacker = {
						character = PREVPREV
					}
					OR = {
						using_cb = bid_for_independence
						using_cb = depose_liege
					}
				}
			}
			any_demesne_title = {
				NOT = { lower_tier_than = ROOT }
				temporary = no
			}
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
			
			# No independence factions in merchant republics
			NOT = {
				AND = {
					is_merchant_republic = yes
					liege = {
						is_merchant_republic = 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
			}
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
			# No independence factions in merchant republics
			NOT = {
				AND = {
					is_merchant_republic = yes
					liege_before_war = {
						is_merchant_republic = yes
					}
				}
			}
		}
	}

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

		if = {
			limit = {
				FROM = {
					independent = yes
				}
			}
			ROOT = {
				set_defacto_liege = ROOT
			}
		}

		hidden_tooltip = {
			ROOT = {
				prestige = 100
				any_demesne_title = {
					add_pressed_claim = FROM
				}
				set_character_flag = ai_flag_never_transfer
			}
		}
		
		ROOT = {
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}

		FROM = {
			if = {
				limit = { NOT = { has_dlc = "Zeus" } }
				any_demesne_title = { # All titles
					limit = {
						OR = {
							tier = king
							tier = emperor
						}
						#is_titular = no
						NOT = { has_law = crown_authority_0 }
					}
					if = {
						limit = { has_law = crown_authority_1 }
						add_law = crown_authority_0
					}
					if = {
						limit = { has_law = crown_authority_2 }
						add_law = crown_authority_1
					}
					if = {
						limit = { has_law = crown_authority_3 }
						add_law = crown_authority_2
					}
					if = {
						limit = { has_law = crown_authority_4 }
						add_law = crown_authority_3
					}
				}
			}
		}

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

	on_fail = {
		ROOT = {
			prestige = -50
			participation_scaled_prestige = -50
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = -50 }
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -100
			prisoner = FROM
			participation_scaled_prestige = -100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = -100 }
		}
		FROM = {
			prestige = 50
		}
	}

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

depose_liege = {
	name = CB_NAME_DEPOSE_LIEGE
	war_name = WAR_NAME_DEPOSE_LIEGE
	sprite = 12
	truce_days = 3650
	
	is_permanent = yes
	can_call_vassals = no
	major_revolt = no

	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		always = no # v2.21 - Disabled - not working well
		ROOT = {
			de_facto_liege = FROM
		}
		
		FROM = {
			is_feudal = yes
			primary_title = {
				is_vice_royalty = no
			}
			NOT = {
				any_war = {
					OR = {
						AND = { # Cannot depose liege when our liege is currently involved in such a war
							attacker = {
								character = PREVPREV
							}
							OR = {
								using_cb = bid_for_independence
								using_cb = depose_liege
							}
						}
						AND = { # Cannot depose liege if someone else is already deposing liege
							defender = {
								character = PREVPREV
							}
							using_cb = depose_liege
						}
					}
				}
			}
		}
	}

	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
			}
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
		}
	}

	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 = {
		FROM = {
			prestige = -500
			if = {
				limit = { NOT = { has_dlc = "Zeus" } }
				any_demesne_title = { # All titles
					limit = {
						OR = {
							tier = king
							tier = emperor
						}
						#is_titular = no
						NOT = { has_law = crown_authority_0 }
					}
					if = {
						limit = { has_law = crown_authority_1 }
						add_law = crown_authority_0
					}
					if = {
						limit = { has_law = crown_authority_2 }
						add_law = crown_authority_1
					}
					if = {
						limit = { has_law = crown_authority_3 }
						add_law = crown_authority_2
					}
					if = {
						limit = { has_law = crown_authority_4 }
						add_law = crown_authority_3
					}
				}
			}

			abdicate_to_most_liked_by = ROOT
		}
		ROOT = {
			participation_scaled_prestige = 250
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 250 }
		}

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

	on_fail = {
		ROOT = {
			prestige = -100
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			prisoner = FROM
		}
		FROM = {
			participation_scaled_prestige = 50
			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
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

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

overthrow_ruler = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_OVERTHROWRULER
	sprite = 12
	truce_days = 3650
	
	is_revolt_cb = yes
	can_call_vassals = yes
	attacker_can_call_allies = no
	major_revolt = yes
	is_tyranny_cb = yes

	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		FROM = {
			primary_title = {
				is_vice_royalty = no
			}
		}
		ROOT = {
			de_facto_liege = FROM
		}
	}

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

	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 = {
			primary_title = {
				add_pressed_claim = FROM	
			}
		}
		FROM = {
			prestige = -500
			remove_opinion = {
				who = ROOT
				modifier = declared_war
			}
			if = {
				limit = { NOT = { has_dlc = "Zeus" } }
				any_demesne_title = { # All titles
					limit = {
						OR = {
							tier = king
							tier = emperor
						}
						#is_titular = no
						NOT = { has_law = crown_authority_0 }
					}
					if = {
						limit = { has_law = crown_authority_1 }
						add_law = crown_authority_0
					}
					if = {
						limit = { has_law = crown_authority_2 }
						add_law = crown_authority_0
					}
					if = {
						limit = { has_law = crown_authority_3 }
						add_law = crown_authority_1
					}
					if = {
						limit = { has_law = crown_authority_4 }
						add_law = crown_authority_2
					}
				}
			}
			if = {
				limit = { has_dlc = "Zeus" }
				any_demesne_title = { # All titles
					limit = {
						OR = {
							tier = duke
							tier = king
							tier = emperor
						}
						NOT = { has_law = revoke_title_law_0 }
					}
					if = {
						limit = { has_law = revoke_title_law_1 }
						add_law = revoke_title_law_0
					}
					if = {
						limit = { has_law = revoke_title_law_2 }
						add_law = revoke_title_law_0
					}
				}
			}
			abdicate = yes
		}
		ROOT = {
			participation_scaled_prestige = 250
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 250 }
		}

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

	on_fail = {
		ROOT = {
			prestige = -100
		}
		FROM = {
			remove_opinion = {
				who = ROOT
				modifier = declared_war
			}
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			prisoner = FROM
		}
		FROM = {
			participation_scaled_prestige = 50
			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
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		hidden_tooltip = {
			any_attacker = {
				limit = { liege = { character = FROM } }
				prisoner = FROM
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_council_power_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	attacker_ai_defeat_worth = {
		factor = 100
	}
	
	on_defender_leader_death = {
		hidden_tooltip = {
			any_attacker = {
				letter_event = {
					id = 251
				}
			}
		}
		
		end_war = invalid
	}

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

overthrow_ruler_no_calls = {
	name = CB_NAME_OVERTHROWRULER_NC
	war_name = WAR_NAME_OVERTHROWRULER_NC
	sprite = 12
	truce_days = 3650
	
	is_revolt_cb = yes
	can_call_vassals = no
	can_ask_to_join_war = no

	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		FROM = {
			primary_title = {
				is_vice_royalty = no
			}
		}
		ROOT = {
			de_facto_liege = FROM
		}
	}

	is_valid = {
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
		FROM = {
			higher_tier_than = ROOT
		}
	}

	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
			primary_title = {
				add_pressed_claim = FROM	
			}
		}
		FROM = {
			prestige = -500
			remove_opinion = {
				who = ROOT
				modifier = declared_war
			}
			if = {
				limit = { NOT = { has_dlc = "Zeus" } }
				any_demesne_title = { # All titles
					limit = {
						OR = {
							tier = king
							tier = emperor
						}
						#is_titular = no
						NOT = { has_law = crown_authority_0 }
					}
					if = {
						limit = { has_law = crown_authority_1 }
						add_law = crown_authority_0
					}
					if = {
						limit = { has_law = crown_authority_2 }
						add_law = crown_authority_0
					}
					if = {
						limit = { has_law = crown_authority_3 }
						add_law = crown_authority_1
					}
					if = {
						limit = { has_law = crown_authority_4 }
						add_law = crown_authority_2
					}
				}
			}
			abdicate = yes
		}

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

	on_fail = {
		ROOT = {
			prestige = -100
		}
		
		FROM = {
			prestige = -100
			remove_opinion = {
				who = ROOT
				modifier = declared_war
			}
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			prisoner = FROM
		}
		FROM = {
			prestige = 50
			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
					}
				}
			}
		}
	}

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

change_gavelkind_succession_law = {
	name = CB_NAME_GAVELKIND_SUCCESSION_LAW
	war_name = WAR_NAME_GAVELKIND_SUCCESSION_LAW
	sprite = 6
	truce_days = 3650
	attacker_can_call_allies = no
	can_ask_to_join_war = no
	major_revolt = yes
	
	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		ROOT = {
			vassal_of = FROM	
		}
	}
	
	is_valid = {
		FROM = {
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
		}
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}

	is_valid_title = {
		NOT = {
			has_law = succ_gavelkind
		}
		OR = {
			tier = emperor
			tier = king
			is_primary_holder_title = yes
		}
	}

	on_add = {
		hidden_tooltip = { fire_haruspicy_event_effect = yes }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = { detract_grace_super_huge_effect = yes }
		}
	}
	
	on_success = {
		FROM = {
			hidden_tooltip = { disband_event_forces = faction_loyalists }
			if = {
				limit = { NOT = { has_dlc = "Zeus" } }
				any_demesne_title = { # All titles
					if = {
						limit = {
							OR = {
								tier = king
								tier = emperor
							}
		#					is_titular = no
							NOT = { has_law = crown_authority_0 }
						}
						if = {
							limit = { has_law = crown_authority_1 }
							add_law = crown_authority_0
						}
						if = {
							limit = { has_law = crown_authority_2 }
							add_law = crown_authority_1
						}
						if = {
							limit = { has_law = crown_authority_3 }
							add_law = crown_authority_2
						}
						if = {
							limit = { has_law = crown_authority_4 }
							add_law = crown_authority_3
						}
					}
				}
			}
		}
		
		ROOT = {
			participation_scaled_prestige = 100
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = 100 
				disband_event_forces = faction_revolters
			}
		}
	}

	on_success_title = {
		succession_w_cooldown = gavelkind
	}

	on_fail = {
		ROOT = {
			prestige = -100
		}
		FROM = {
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		
		hidden_tooltip = {
			any_attacker = {
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_succ_gavelkind_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			prisoner = FROM
		}
		FROM = {
			participation_scaled_prestige = 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
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		
		hidden_tooltip = {
			any_attacker = {
				limit = { liege = { character = FROM } }
				prisoner = FROM
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_succ_gavelkind_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

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

lower_crown_authority_law = {
	name = CB_NAME_LOWER_CROWN_AUTHORITY_LAW
	war_name = WAR_NAME_LOWER_CROWN_AUTHORITY_LAW
	sprite = 6
	truce_days = 3650
	can_call_allies = no
	can_ask_to_join_war = no
	major_revolt = yes
	
	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		ROOT = {
			vassal_of = FROM
		}
		NOT = { has_dlc = "Zeus" }
	}
	
	can_use_title = {
		holder_scope = { independent = yes }
		OR = {
			tier = king
			tier = emperor
		}
		OR = {
			has_law = crown_authority_1
			has_law = crown_authority_2
			has_law = crown_authority_3
			has_law = crown_authority_4
		}
	}
	
	is_valid = {
		FROM = {
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
		}
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}

	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 = {
			opinion = {
				modifier = opinion_lowered_crown_authority
				who = FROM
				months = 12
			}
			participation_scaled_prestige = 100
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_success_title = {
		if = {
			limit = { has_law = crown_authority_1 }
			add_law_w_cooldown = crown_authority_0
		}
		if = {
			limit = { has_law = crown_authority_2 }
			add_law_w_cooldown = crown_authority_1
		}
		if = {
			limit = { has_law = crown_authority_3 }
			add_law_w_cooldown = crown_authority_2
		}
		if = {
			limit = { has_law = crown_authority_4 }
			add_law_w_cooldown = crown_authority_3
		}
	}

	on_fail = {
		ROOT = {
			prestige = -100
		}
		
		FROM = {
			participation_scaled_prestige = 50
			hidden_tooltip = { disband_event_forces = faction_loyalists }
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		
		hidden_tooltip = {
			any_attacker = {
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_lower_CA_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			prisoner = FROM
		}
		
		FROM = {
			participation_scaled_prestige = 100
			hidden_tooltip = { disband_event_forces = faction_loyalists }
			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
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		
		hidden_tooltip = {
			any_attacker = {
				limit = { liege = { character = FROM } }
				prisoner = FROM
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_lower_CA_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
	}

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


lower_tribal_organization_law = {
	name = CB_NAME_LOWER_TRIBAL_ORGANIZATION_LAW
	war_name = WAR_NAME_LOWER_TRIBAL_ORGANIZATION_LAW
	sprite = 6
	truce_days = 3650
	can_call_allies = no
	can_ask_to_join_war = no
	major_revolt = yes
	
	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		ROOT = {
			vassal_of = FROM
		}
	}
	
	can_use_title = {
		holder_scope = { independent = yes }
		OR = {
			tier = duke
			tier = king
			tier = emperor
		}
		OR = {
			has_law = tribal_organization_1
			has_law = tribal_organization_2
			has_law = tribal_organization_3
			has_law = tribal_organization_4
		}
	}
	
	is_valid = {
		FROM = {
			is_tribal = yes
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
		}
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}

	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 = {
			opinion = {
				modifier = opinion_lowered_tribal_organization
				who = FROM
				months = 12
			}
			participation_scaled_prestige = 100
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_success_title = {
		if = {
			limit = { has_law = tribal_organization_1 }
			add_law_w_cooldown = tribal_organization_0
		}
		if = {
			limit = { has_law = tribal_organization_2 }
			add_law_w_cooldown = tribal_organization_1
		}
		if = {
			limit = { has_law = tribal_organization_3 }
			add_law_w_cooldown = tribal_organization_2
		}
		if = {
			limit = { has_law = tribal_organization_4 }
			add_law_w_cooldown = tribal_organization_3
		}
	}

	on_fail = {
		ROOT = {
			prestige = -100
		}
		
		FROM = {
			participation_scaled_prestige = 50
			hidden_tooltip = { disband_event_forces = faction_loyalists }
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		
		hidden_tooltip = {
			any_attacker = {
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_lower_TO_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			prisoner = FROM
		}
		
		FROM = {
			participation_scaled_prestige = 100
			hidden_tooltip = { disband_event_forces = faction_loyalists }
			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
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		
		hidden_tooltip = {
			any_attacker = {
				limit = { liege = { character = FROM } }
				prisoner = FROM
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_lower_TO_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
	}

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

coastal_republic = {
	name = CB_NAME_COASTAL_REPUBLIC
	war_name = WAR_NAME_COASTAL_REPUBLIC
	sprite = 1
	truce_days = 3650
	is_permanent = yes
	check_all_titles = yes
	can_ask_to_join_war = no
	
	allow_distant = yes # AI parameter

	allowed_to_target_tributaries = no
	
	sort_priority = 700

	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		ROOT = {
			OR = {
				is_merchant_republic = yes
				is_patrician = yes
			}
			NOT = { is_liege_or_above = FROM }
			NOT = { is_vassal_or_below = FROM }
			NOT = { same_liege = FROM }
			mercenary = no
		}
	}

	can_use_title = {
		tier = BARON
		is_capital = no # Minor Holding
		holding_type = city
		
		location = {
			port = yes
			
			# Must have a trade post there already
			OR = {
				PREV = {
					holder_scope = {
						NOT = { same_realm = ROOT }
						OR = {
							is_patrician = yes
							is_merchant_republic = yes
						}
					}
				}
				trade_post_owner = {
					OR = {
						character = ROOT
						is_liege_or_above = ROOT
					}
				}
			}
			
			# Not allowed to use this CB to take cities in the capital province of a rival merchant republic
			FROM = { 
				OR = {
					is_merchant_republic = no
					capital_scope = {
						NOT = { province_id = PREVPREV }
					}
				}
			}
		}
	}

	is_valid = {
		ROOT = {
			OR = {
				is_merchant_republic = yes
				is_patrician = yes
			}
		}
	}

	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_title = {
		if = {
			limit = {
				NOT = {
					location = {
						trade_post_owner = {
							is_liege_or_above = ROOT
						}
					}
				}
			}
			usurp_title = { target = ROOT type = invasion }
		}
		
		if = {
			limit = {
				location = {
					trade_post_owner = {
						is_liege_or_above = ROOT
					}
				}
			}
			location = {
				trade_post_owner = {
					usurp_title = { target = PREVPREV type = invasion }
				}
			}
		}
		
		ROOT = {
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}   

	on_fail = {
		ROOT = {
			prestige = -100
		}
		FROM = {
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			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 = 50 
	}

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

coastal_county_republic = {
	name = CB_NAME_COASTAL_COUNTY_REPUBLIC
	war_name = WAR_NAME_COASTAL_COUNTY_REPUBLIC
	sprite = 1
	truce_days = 3650
	is_permanent = yes
	check_all_titles = yes
	can_ask_to_join_war = no
	
	allow_distant = yes # AI parameter

	allowed_to_target_tributaries = no
	
	sort_priority = 700

	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		ROOT = {
			OR = {
				is_merchant_republic = yes
				is_patrician = yes
			}
			NOT = { is_liege_or_above = FROM }
			NOT = { is_vassal_or_below = FROM }
			NOT = { same_liege = FROM }
			mercenary = no
		}
	}

	can_use_title = {
		tier = COUNT
		location = {
			port = yes
			county = {
				any_direct_de_jure_vassal_title = {
					holder_scope = {
						OR = {
							character = ROOT
							is_liege_or_above = ROOT
						}
					}
				}
			}
			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 = {
		ROOT = {
			OR = {
				is_merchant_republic = yes
				is_patrician = yes
			}
		}
	}

	is_valid_title = {
		tier = COUNT
		location = {
			port = yes
			county = {
				any_direct_de_jure_vassal_title = {
					holder_scope = {
						OR = {
							character = ROOT
							is_liege_or_above = ROOT
						}
					}
				}
			}
		}
	}

	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_title = {
		if = {
			limit = {
				any_direct_de_jure_vassal_title = {
					holder_scope = {
						character = ROOT
					}
				}
			}
			usurp_title = ROOT
		}
		
		if = {
			limit = {
				NOT = {
					any_direct_de_jure_vassal_title = {
						holder_scope = {
							character = ROOT
						}
					}
				}
			}
			any_de_jure_vassal_title = {
				limit = {
					holder_scope = {
						is_liege_or_above = ROOT
					}
				}
				holder_scope = {
					usurp_title = PREVPREV
					set_defacto_liege = ROOT
				}
			}
		}
		
		ROOT = {
			participation_scaled_prestige = 100
		}
		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 = {
		ROOT = {
			prestige = -100
		}
		FROM = {
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			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
	}
}


weaken_vassal_plot = {
	name = CB_NAME_WEAKEN_VASSAL
	war_name = WAR_NAME_WEAKEN_VASSAL
	sprite = 3
	truce_days = 3650
	hostile_against_others = yes

	press_claim = yes
	attacker_can_call_allies = no
	can_ask_to_join_war = no

	can_use_gui = {
		check_if_crusader_trigger = yes
	}

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

	is_valid_title = {
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = 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 
			}
		}
	}

	on_success = {
		ROOT = {
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = {
				NOT = { character = ROOT }
			}
			participation_scaled_prestige = 100
		}
		FROM = {
			if = {
				limit = { NOT = { has_dlc = "Zeus" } }
				any_demesne_title = { # All titles
					limit = {
						OR = {
							tier = king
							tier = emperor
						}
	#					is_titular = no
						NOT = { has_law = crown_authority_0 }
					}
					if = {
						limit = { has_law = crown_authority_1 }
						add_law = crown_authority_0
					}
					if = {
						limit = { has_law = crown_authority_2 }
						add_law = crown_authority_1
					}
					if = {
						limit = { has_law = crown_authority_3 }
						add_law = crown_authority_2
					}
					if = {
						limit = { has_law = crown_authority_4 }
						add_law = crown_authority_3
					}
				}
			}
		}
	}
	
	on_success_title = {
		FROM = {
			random_vassal = {
				limit = {
					tier = count
					any_demesne_title = {
						tier = count
						dejure_liege_title = {
							ROOT = {
								plot_target_title = {
									title = PREVPREV
								}
							}
						}
					}
				}
				grant_title = PREVPREV
			}
		}
	}

	on_fail = {
		ROOT = {
			prestige = -100
			cancel_plot = plot_weaken_fellow_vassal
		}
		FROM = {
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}
	
	on_invalidation = {
		ROOT = {
			cancel_plot = plot_weaken_fellow_vassal
		}
		
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			imprison = FROM
			cancel_plot = plot_weaken_fellow_vassal
		}
		FROM = {
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}
	
	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 = 100
	}

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

decadence_invasion = {
	name = CB_NAME_INVASION
	war_name = WAR_NAME_DEC_INVASION
	sprite = 7
	truce_days = 3650
	hostile_against_others = yes
	can_ask_to_join_war = no
	
	can_use = {
		ROOT = {
			has_character_flag = decadence_invader
		}
	}

	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 = {
		any_playable_ruler = {
			limit = {
				ai = no
				NOT = { character = FROM }
				NOT = { dynasty = FROM }
				NOT = {
					any_liege = {
						character = FROM
					}
				}
				OR = {
					FROM = { higher_tier_than = DUKE }
					any_realm_province = {
						any_neighbor_province = {
							owner = {
								any_liege = {
									character = FROM
								}
							}
						}
					}
				}
			}
			narrative_event = { id = 91241 }
		}
		
		FROM = {
			if = {
				limit = { NOT = { has_dlc = "Zeus" } }
				any_demesne_title = { # All titles
					if = {
						limit = {
							OR = {
								tier = king
								tier = emperor
							}
							NOT = { has_law = crown_authority_0 }
						}
						add_law = crown_authority_0
					}
				}
			}
		}
		
		ROOT = {
			prestige = 200
			piety = 100
			occupy_minors_of_occupied_settlements = FROM
			gain_all_occupied_titles = { who = FROM type = revolt }
			give_nickname = nick_the_conqueror
			participation_scaled_prestige = 200
			participation_scaled_piety = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_prestige = 200
				participation_scaled_piety = 100
			}
		}
		
		FROM = {
			prestige = -200
			primary_title = {
				holder_scope = {
					any_demesne_title = {
						limit = {
							higher_tier_than = COUNT
							NOT = {
								tier = PREVPREV
							}
						}
						usurp_title_plus_barony_if_unlanded = { target = ROOT type = revolt }
						
						if = {
							limit = {
								NOT = { culture = ROOT }
							}
							hidden_tooltip = { conquest_culture = ROOT }
						}
					}
					any_demesne_title = {
						limit = {
							tier = PREVPREV
						}
						destroy_landed_title = THIS
					}
				}
			}
			
			set_defacto_liege = ROOT
			imprison = ROOT
			
			hidden_tooltip = {
				if = {
					limit = {
						has_nickname = no
					}
					random_list = {
						10 = { give_nickname = nick_the_decadent }
						10 = { give_nickname = nick_the_unready }
						10 = { give_nickname = nick_the_ill_ruler }
					}
				}
			}
		}
		
		hidden_tooltip = {
			ROOT = { 
				disband_event_forces = decadence_revolters
				
				# Powerful vassals can choose independence
				#any_vassal = {
				#	limit = {
				#		NOT = { character = FROM }
				#		higher_tier_than = COUNT
				#		prisoner = no
				#	}
				#	character_event = { id = 91240 }
				#}
			}
		}

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

	on_fail = {
		ROOT = {
			prestige = -200
		}
		FROM = {
			prestige = 100
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		hidden_tooltip = {
			ROOT = { disband_event_forces = decadence_revolters }
		}
	}

	on_reverse_demand = {
		ROOT = { set_defacto_liege = FROM }
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 8.0
			}
		}
		FROM = {
			prestige = 200
		}
		ROOT = { 
			imprison = FROM
			disband_event_forces = decadence_revolters
		}
		if = {
			limit = {
				FROM = {
					ai = no
					NOT = { has_character_flag = achievement_decadent_warrior }
				}
			}
			FROM = { set_character_flag = achievement_decadent_warrior }
		}
	}

	on_reverse_demand_title = {
		ROOT = { show_scope_change = no remove_claim = PREV }
	}

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

muslim_invasion = {
	name = CB_NAME_INVASION
	war_name = WAR_NAME_INVASION
	sprite = 8
	truce_days = 3650
	hostile_against_others = yes
	is_permanent = yes
	is_holy_war = yes
	can_ask_to_join_war = yes
	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
	
	allowed_to_target_tributaries = no
	
	sort_priority = 2000

	can_use_gui = {
		ROOT = {
			piety = 1000
		}
	}
	
	on_add = {
		hidden_tooltip = { fire_haruspicy_event_effect = yes }
		ROOT = { piety = -1000 }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	can_use = {
		ROOT = {
			OR = {
				ai = yes
				multiplayer = no
				NOT = {
					has_game_rule = {
						name = multiplayer_invasions
						value = off
					}
				}
			}
			religion_group = muslim
			
			# These use 'tribal_invasion' instead:
			NOR = {
				is_nomadic = yes
				AND = {
					primary_title = { is_tribal_type_title = yes }
					has_horde_culture = yes
				}
				AND = {
					mercenary = yes
					NOT = {
						num_of_titles = 2
					}
				}
			}
		}
		
		FROM = { NOT = { religion = ROOT } }
		
		ROOT = {
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}
	}
	
	can_use_title = {
		# Muslims require the target to have at least 6 counties within the target kingdom
		FROM = {
			num_of_realm_counties = {
				value = 6
				title = PREV
			}
		}
		# The attacker needs at least one county in the target kingdom, or a border
		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 = {
							owner = {
								OR = {
									character = ROOT
									is_liege_or_above = ROOT
								}
							}
						}
					}
				}
			}
		}
	}
	
	is_valid = {
		ROOT = {
			religion_group = muslim
		}
		FROM = { NOT = { religion = ROOT } }
	}
	
	is_valid_title = {
		FROM = {
			any_realm_province = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}

	on_success = {
		ROOT = {
			show_scope_change = no
			prestige = 200
			hidden_tooltip = {
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
			participation_scaled_prestige = 200
			if = {
				limit = {
					uses_decadence = yes
					FROM = { NOT = { uses_decadence = yes } }
				}
				participation_scaled_decadence = -20
			}
		}
		
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_prestige = 200 
				if = {
					limit = {
						uses_decadence = yes
						FROM = { NOT = { uses_decadence = yes } }
					}
					participation_scaled_decadence = -20
				}
				hidden_tooltip = {
					if = {
						limit = {
							has_ambition = obj_strengthen_religion
						}
						change_variable = { which = strengthen_religion value = 1 }
					}
				}
			}
		}
		FROM = {
			show_scope_change = no
			prestige = -200
		}
	}
	
	on_success_title = {
		custom_tooltip = { 
			text = other_invasion_succ_tip
			hidden_tooltip = {
				ROOT = {
					vassalize_or_take_under_title_destroy_duchies = {
						title = PREV
						enemy = FROM
						is_crusade = no # Even if the title holder is not participating in the war, gain holdings occupied by all Crusade participants
						type = invasion
					}
					if = {
						limit = {
							OR = {
								this_has_lame_nickname_trigger = yes
								has_nickname = no
							}
						}
						random_list = {
							10 = { give_nickname = nick_the_holy }
							10 = { give_nickname = nick_the_glorious }
							10 = { give_nickname = nick_the_lionheart }
							10 = { give_nickname = nick_the_great }
							10 = { give_nickname = nick_the_hammer }
							10 = { give_nickname = nick_the_magnificent }
						}
					}
				}
			}
		}
	}

	on_fail = {
		FROM = {
			show_scope_change = no
			prestige = 100
		}
		FROM = {
			show_scope_change = no
			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
						}
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
			hidden_effect = {
				if = {
					limit = {
						OR = {
							this_has_lame_nickname_trigger = yes
							has_nickname = no
						}
					}
					random_list = {
						10 = { give_nickname = nick_the_holy }
						10 = { give_nickname = nick_the_glorious }
						10 = { give_nickname = nick_the_lionheart }
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_hammer }
						10 = { give_nickname = nick_the_magnificent }
						10 = { give_nickname = nick_the_avenger }
					}
				}
			}
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		ROOT = {
			show_scope_change = no
			prestige = -200
		}
	}
	
	on_invalidation = {
		ROOT = {
			show_scope_change = no
			piety = 1000
		}
	}

	on_reverse_demand = {
		ROOT = {
			show_scope_change = no
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			if = {
				limit = {
					NOT = {
						any_liege = {
							dynasty = PREV
						}
					}
				}
				decadence = 10
			}
		}
		FROM = {
			show_scope_change = no
			prestige = 200
			hidden_tooltip = {
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
			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
						}
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
			hidden_effect = {
				if = {
					limit = {
						OR = {
							this_has_lame_nickname_trigger = yes
							has_nickname = no
						}
					}
					random_list = {
						10 = { give_nickname = nick_the_holy }
						10 = { give_nickname = nick_the_glorious }
						10 = { give_nickname = nick_the_lionheart }
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_hammer }
						10 = { give_nickname = nick_the_magnificent }
						10 = { give_nickname = nick_the_avenger }
					}
				}
			}
			participation_scaled_prestige = 200
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
			hidden_tooltip = {
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
		}
	}

	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
	}
	
	ai_will_do = { 
		factor = 1
		
		modifier = {
			factor = 0.9 # Prefer using CB's without on_add costs
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_nomadic = no
				NOT = {
					is_tribal = yes
				}
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}
				}
			}
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_tribal = yes
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
							holding_type = tribal
						}
					}
				}
			}
		}
	}
}

cb_faction_independence = {
	name = CB_NAME_INDEPENDENCE
	war_name = WAR_NAME_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_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		ROOT = {
			vassal_of = FROM
			mercenary = no
		}
	}

	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_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 = {
		if = {
			limit = {
				FROM = {
					independent = yes
				}
			}
			any_attacker = {
				limit = {
					OR = {
						vassal_of = FROM
						liege_before_war = {
							character = FROM
						}
					}
				}
				set_defacto_liege = THIS
				hidden_tooltip = {
					prestige = 100
					any_demesne_title = {
						limit = {
							controls_religion = no
							higher_tier_than = BARON
							FROM = {
								OR = { # If the old liege can use the subjugation CB, give no claims
									NOT = { religion_group = pagan_group }
									NOT = { culture_group = PREVPREV }
								}
							}
						}
						add_weak_pressed_claim = FROM
					}
					set_character_flag = ai_flag_never_transfer
				}
			}		
		}
		
		ROOT = {
			participation_scaled_prestige = 100
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = 100
				disband_event_forces = faction_revolters
			}
		}

		FROM = {
			
			hidden_tooltip = { disband_event_forces = faction_loyalists }
			if = {
				limit = { NOT = { has_dlc = "Zeus" } }
				any_demesne_title = { # All titles
					limit = {
						OR = {
							tier = king
							tier = emperor
						}
						NOT = { has_law = crown_authority_0 }
					}
					if = {
						limit = { has_law = crown_authority_1 }
						add_law = crown_authority_0
					}
					if = {
						limit = { has_law = crown_authority_2 }
						add_law = crown_authority_1
					}
					if = {
						limit = { has_law = crown_authority_3 }
						add_law = crown_authority_2
					}
					if = {
						limit = { has_law = crown_authority_4 }
						add_law = crown_authority_3
					}
				}
			}
		}

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

	on_fail = {
		ROOT = {
			prestige = -50
			add_character_modifier = {
				name = faction_independence_ultimatum_timer
				hidden = yes
				duration = 3650
			}
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
			participation_scaled_prestige = -50
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = -50 
				disband_event_forces = faction_revolters
			}
			add_character_modifier = {
				name = faction_independence_ultimatum_timer
				hidden = yes
				duration = 3650
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -100
			prisoner = FROM
			add_character_modifier = {
				name = faction_independence_ultimatum_timer
				hidden = yes
				duration = 3650
			}
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
			participation_scaled_prestige = -100
		}
		
		any_attacker = {
			limit = { 
				NOT = { character = ROOT }
				liege = { character = FROM } 
			}
			prisoner = FROM
			participation_scaled_prestige = -100
			hidden_tooltip = {
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_independence_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		
		FROM = {
			prestige = 50
			hidden_tooltip = { disband_event_forces = faction_loyalists }
			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_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
	}
}

imperial_reconquest = {
	name = CB_NAME_IMPERIAL
	war_name = WAR_NAME_IMPERIAL
	sprite = 17
	truce_days = 3650
	is_permanent = 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 = no
	
	sort_priority = 1900

	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		ROOT = {
			has_landed_title = e_roman_empire
			independent = yes
			OR = { 
				religion_group = christian
				religion = hellenic_pagan
				religion = hellenic_pagan_reformed
			}
			NOT = { same_realm = FROM }
			mercenary = no
		}
		FROM = {
			NOT = { religion_group = muslim }
		}
	}

	can_use_title = {
		OR = {
			de_jure_liege_or_above = k_byzantium
			de_jure_liege_or_above = k_thrace
			de_jure_liege_or_above = k_anatolia
			de_jure_liege_or_above = k_epirus
			de_jure_liege_or_above = k_trebizond
			de_jure_liege_or_above = k_armenia
			de_jure_liege_or_above = k_bulgaria
			de_jure_liege_or_above = k_serbia
			de_jure_liege_or_above = k_croatia
			de_jure_liege_or_above = k_italy
			de_jure_liege_or_above = k_sicily
			de_jure_liege_or_above = k_bavaria
			de_jure_liege_or_above = k_burgundy
			de_jure_liege_or_above = k_france
			de_jure_liege_or_above = k_aquitaine
			de_jure_liege_or_above = k_brittany
			de_jure_liege_or_above = k_england
			de_jure_liege_or_above = k_wales
			de_jure_liege_or_above = k_frisia
			de_jure_liege_or_above = k_lotharingia
			de_jure_liege_or_above = k_aragon
			de_jure_liege_or_above = k_castille
			de_jure_liege_or_above = k_andalusia
			de_jure_liege_or_above = k_badajoz
			de_jure_liege_or_above = k_valencia
			de_jure_liege_or_above = k_leon
			de_jure_liege_or_above = k_spanish_galicia
			de_jure_liege_or_above = k_portugal
			de_jure_liege_or_above = k_navarra
			de_jure_liege_or_above = k_syria
			de_jure_liege_or_above = k_jerusalem
			de_jure_liege_or_above = k_egypt
			de_jure_liege_or_above = k_africa
			de_jure_liege_or_above = k_mauretania
			de_jure_liege_or_above = k_papal_state
			de_jure_liege_or_above = k_papacy
			de_jure_liege_or_above = k_asturias
			de_jure_liege_or_above = k_sardinia
			de_jure_liege_or_above = k_antioch
			de_jure_liege_or_above = k_cyprus
			de_jure_liege_or_above = k_amalfi
			de_jure_liege_or_above = k_venice
			de_jure_liege_or_above = k_pisa
			de_jure_liege_or_above = k_genoa
			de_jure_liege_or_above = k_switzerland
			de_jure_liege_or_above = k_romagna
			de_jure_liege_or_above = k_carinthia
		}
	}
	
	is_valid = {
		ROOT = {
			independent = yes
			OR = { 
				religion_group = christian
				religion = hellenic_pagan
				religion = hellenic_pagan_reformed
			}
			NOT = { same_realm = 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 
			}
		}
	}
	
	on_success = {
		ROOT = {
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_prestige = 100
			}
		}
		
		FROM = { prestige = -100 }
	}

	on_success_title = {
		custom_tooltip = { 
			text = other_invasion_succ_tip
			hidden_tooltip = {
				ROOT = {
					vassalize_or_take_under_title = {
						title = PREV
						enemy = FROM
						type = invasion
					}
				}
			}
		}
		if = {
			limit = {
				holder_scope = { character = FROM }
			}
			usurp_title_only = { target = ROOT type = invasion }
		}

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

	on_fail = {
		ROOT = { prestige = -150 }
	}

	on_reverse_demand = {
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			ROOT = { prestige = -200 }
		}
		FROM = {
			participation_scaled_piety = 100
			participation_scaled_prestige = 200
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_piety = 100
				participation_scaled_prestige = 200
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
			}
		}
	}

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

# Embargo Merchant Republic CB
embargo_cb = {
	name = CB_NAME_EMBARGO
	war_name = WAR_NAME_EMBARGO
	sprite = 19
	truce_days = 3650
	
	is_permanent = yes
	can_ask_to_join_war = no

	allowed_to_target_tributaries = no
	
	sort_priority = 2000

	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		ROOT = {
			independent = yes
			NOT = { has_embargo = FROM }
			mercenary = no
		}
		FROM = {
			OR = {
				AND = {
					is_merchant_republic = yes
					any_trade_post = {
						owner = {
							OR = {
								character = ROOT
								is_liege_or_above = ROOT
							}
						}
					}
				}
				AND = {
					OR = {
						is_merchant_republic = yes
						higher_tier_than = DUKE
					}
					any_realm_lord = {
						is_patrician = yes
						any_trade_post = {
							owner = {
								OR = {
									character = ROOT
									is_liege_or_above = ROOT
								}
							}
						}
					}
				}
			}
		}
	}

	is_valid = {
		ROOT = {
			independent = yes
			NOT = { has_embargo = FROM }
		}
		FROM = {
			OR = {
				is_merchant_republic = yes
				AND = {
					higher_tier_than = DUKE
					any_realm_lord = {
						is_merchant_republic = yes
					}
				}
			}
		}
	}

	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 = {
			embargo = FROM
			if = {
				limit = {
					NOT = { religion = FROM }
				}
				piety = 200
			}
		}
	}

	on_fail = {
		if = {
			limit = {
				NOT = {
					any_attacker = {
						NOT = { character = ROOT } 
						is_merchant_republic = yes
					}
				}
			}
			ROOT = {
				prestige = -100
			}
		}
		
		if = {
			limit = {
				any_attacker = {
					NOT = { character = ROOT } 
					is_merchant_republic = yes
				}
			}
			any_attacker = {
				limit = { 
					NOT = { character = ROOT } 
					is_merchant_republic = yes
				}
				prestige = -100
			}
		}
		
		FROM = {
			prestige = 50
		}
	}

	on_reverse_demand = {
		if = {
			limit = {
				NOT = {
					any_attacker = {
						NOT = { character = ROOT } 
						is_merchant_republic = yes
					}
				}
			}
			
			ROOT = {
				prestige = -200
				transfer_scaled_wealth = {
					to = FROM
					value = 4.0
				}
			}
		}
		
		if = {
			limit = {
				any_attacker = {
					NOT = { character = ROOT } 
					is_merchant_republic = yes
				}
			}
			any_attacker = {
				limit = { 
					NOT = { character = ROOT } 
					is_merchant_republic = yes
				}
				prestige = -200
				transfer_scaled_wealth = {
					to = FROM
					value = 4.0
				}
			}
		}
		
		FROM = {
			prestige = 100
		}
	}

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

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 50
	}
	
	# ai importance placed on this CB: scope is the targeted title, ROOT is the attacking character, FROM is the defending character
	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 = 0
			ROOT = {
				opinion = { who = FROM value = 25 }
			}
		}
		modifier = {
			factor = 0.5
			ROOT = {
				opinion = { who = FROM value = 0 }
			}
		}		
		modifier = {
			factor = 0.5
			ROOT = {
				opinion = { who = FROM value = -25 }
			}
		}			
	}		
}

seize_trade_post = {
	name = CB_NAME_SEIZE_TRADE_POST
	war_name = WAR_NAME_SEIZE_TRADE_POST
	sprite = 18
	truce_days = 1825
	is_permanent = yes
	check_all_trade_posts = yes
	can_ask_to_join_war = no
	att_ticking_war_score_multiplier = 5.0
	def_ticking_war_score_multiplier = 5.0
	allow_distant = yes

	allowed_to_target_tributaries = no
	
	sort_priority = 1900

	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		ROOT = {
			OR = {
				is_merchant_republic = yes
				is_patrician = yes
			}
			NOT = { is_liege_or_above = FROM }

			OR = {
				NOT = { same_realm = FROM }
				
				# We cannot overtly attack much weaker Patricians in the same republic
				NOT = { 
					num_of_trade_post_diff = {
						character = FROM
						value = 3
					}
				}
			}
			mercenary = no
		}
		FROM = {
			NOT = { is_liege_or_above = ROOT }
		}
	}

	can_use_title = {
		tier = COUNT
		location = {
			port = yes
		}
		has_trade_post = yes
		trade_post_owner = {
			OR = {
				character = FROM
				is_liege_or_above = FROM
			}
		}
		holder_scope = {
			NOT = { character = FROM }
		}
	}

	is_valid = {
		ROOT = {
			OR = {
				is_merchant_republic = yes
				is_patrician = yes
			}
			NOT = { is_liege_or_above = FROM }
		}
		FROM = {
			NOT = { is_liege_or_above = ROOT }
		}
	}
	
	is_valid_title = {
		has_trade_post = yes
		trade_post_owner = {
			OR = {
				character = FROM
				is_liege_or_above = FROM
			}
		}
		holder_scope = {
			NOT = { character = 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 
			}
		}
	}

	on_success_title = {
		
		seize_trade_post = ROOT
		
		ROOT = {
			participation_scaled_prestige = 50
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_fail = {
		ROOT = {
			prestige = -50
		}
		FROM = {
			participation_scaled_prestige = 25
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 25 }
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -100
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
		}
		FROM = {
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

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

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

	# ai importance placed on this CB: scope is the targeted title, ROOT is the attacking character, FROM is the defending character
	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 = 0.25
			ROOT = {
				NOT = { same_realm = FROM }
			}
		}
	}
}

# The Prepared Invasion CB of Norse Heathens
viking_invasion = {
	name = CB_NAME_VIKING_INVASION
	war_name = WAR_NAME_VIKING_INVASION
	sprite = 20
	truce_days = 365
	hostile_against_others = no # Vikings share in the spoils...
	is_permanent = yes
	is_holy_war = yes
	can_ask_to_join_war = yes
	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
	apply_short_occ_mod = no # Do not apply the 'recently_conquered' modifier to Holdings

	allowed_to_target_tributaries = no
	
	sort_priority = 3000

	can_use = {
		ROOT = {
			has_opinion_modifier = { who = FROM modifier = preparing_to_invade }
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}
	}
	
	can_use_title = {
		is_valid_viking_invasion_target = FROM # Checks that FROM holds the right amount of territory in the kingdom
	}
	
	is_valid_title = {
		FROM = {
			any_realm_province = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}

	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 = 500
			piety = 250
			religion_authority = {
				modifier = won_viking_invasion
			}
			
			hidden_tooltip = {
				if = {
					limit = {
						has_nickname = no
					}
					random_list = {
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_victorious }
						10 = { give_nickname = nick_the_fearless }
						10 = { give_nickname = nick_the_brave }
						10 = { give_nickname = nick_haardraade }
						10 = { give_nickname = nick_the_viking }
						10 = { give_nickname = nick_the_dragon }
						10 = { give_nickname = nick_the_ironside }
					}
				}
			}
			participation_scaled_prestige = 300
		}
		
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 300 }
		}
		FROM = {
			prestige = -500
		}
	}
	
	on_success_title = {
		custom_tooltip = { 
			text = tribal_invasion_succ_tip
			hidden_tooltip = {
				FROM = {
					random_realm_province = {
						limit = {
							de_jure_liege_or_above = PREVPREV
							OR = {
								NOT = { culture = ROOT }
								NOT = { religion = ROOT }
							}
						}
						culture = ROOT
						religion = ROOT
					}
				}
				ROOT = {
					occupy_minors_of_occupied_settlements = FROM
					gain_all_occupied_titles = { who = FROM type = invasion }
					vassalize_or_take_under_title_destroy_duchies = {
						title = PREV
						enemy = FROM
						type = invasion
					}
					
					# Settle the special invasion forces
					disband_event_forces = prepared_invasion
					any_demesne_title = {
						limit = { tier = BARON }
						remove_holding_modifier = recently_conquered
						refill_holding_levy = yes
					}
				}
				
				if = {
					limit = {
						FROM = { is_liege_or_above = ROOT }
					}
					FROM = { imprison = ROOT }
				}
			}
		}
	}

	on_fail = {
		FROM = {
			prestige = 200
			participation_scaled_prestige = 200
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		ROOT = {
			prestige = -300
			piety = -100
			hidden_tooltip = {
				disband_event_forces = prepared_invasion
			}
			religion_authority = {
				modifier = lost_viking_invasion
			}
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -500
			piety = -250
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
			hidden_tooltip = {
				disband_event_forces = prepared_invasion
			}
			religion_authority = {
				modifier = lost_viking_invasion
			}
		}
		FROM = {
			prestige = 300
			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
						}
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
			participation_scaled_prestige = 250
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 250 }
		}
	}

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

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

# Pagans can subjugate other pagans
pagan_subjugation = {
	name = CB_NAME_PAGAN_SUBJUGATION
	war_name = WAR_NAME_PAGAN_SUBJUGATION
	sprite = 8
	truce_days = 365
	is_permanent = yes
	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no
	defender_unoccupied_warscore = yes
	
	sort_priority = 2000
	
	can_use = {
		ROOT = {
			religion_group = pagan_group
			is_reformed_religion = no
			NOT = { religion = hellenic_pagan }
			NOT = { same_realm = FROM }
			NOT = { is_liege_or_above = FROM }
			mercenary = no
			OR = {
				AND = {
					has_dlc = "Horse Lords"
					is_nomadic = no
				}
				AND = {
					NOT = { has_dlc = "Horse Lords" }
					has_horde_culture = no # Should use tribal invasion instead
				}
			}		
			OR = {
				is_tribal = yes
				primary_title = {
					has_law = succ_elective_gavelkind
				}
			}
			
			OR = {
				NOT = { has_character_modifier = launched_subjugation }
				AND = {
					has_ambition = obj_become_king
					capital_scope = {
						kingdom = {
							FROM = {
								any_realm_title = {
									kingdom = {
										title = PREVPREVPREV
									}
								}
							}
						}
					}
				}
			}
		}
		
		OR = {
			NOT = { ROOT = { religion = aztec } }
			FROM = { religion = aztec }
		}
		
		FROM = {
			in_revolt = no
			religion_group = pagan_group
			is_nomadic = no
			NOT = { is_offmap_governor = yes }
			OR = {
				is_reformed_religion = no
				is_reformed_religion = ROOT
			}
			NOT = {
				any_war = {
					defender = { character = PREVPREV }
					OR = {
						using_cb = pagan_subjugation
						using_cb = nomad_subjugation
					}
				}
			}
		}
	}
	
	on_add = {
		ROOT = {
			add_character_modifier = { 
				name = launched_subjugation
				years = 100
				hidden = yes
			}
		}
		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 = {
			piety = 100
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		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 = tribal_subjugation_tip
			}
				
			hidden_tooltip = {
				opinion = {
					modifier = opinion_subjugated 
					who = ROOT
				}
				any_vassal = {
					opinion = {
						modifier = opinion_subjugated 
						who = ROOT
					}
				}				
				any_demesne_title = {
					limit = {
						higher_tier_than = ROOT
					}
					ROOT = { usurp_title = { target = PREV type = invasion } }
					add_pressed_claim = PREV
				}
				any_demesne_title = {
					limit = {
						tier = ROOT
					}
					ROOT = { usurp_title = { target = PREV type = invasion } }
					add_pressed_claim = PREV
				}
				set_defacto_liege = ROOT
			}
		}
	}
	
	on_fail = {
		FROM = {
			piety = 50
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		ROOT = {
			piety = -100
		}
	}
	
	on_invalidation = {
		ROOT = {
			remove_character_modifier = launched_subjugation
		}
	}

	on_reverse_demand = {
		ROOT = {
			piety = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
		}
		FROM = {
			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
						}
				}
				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
				}
			}
		}
	}
}

# The CB used by "Peasant revolter" characters
peasant_revolt = {
	name = CB_NAME_PEASANT_CONQUEST
	war_name = WAR_NAME_PEASANT_CONQUEST
	sprite = 16
	truce_days = 365
	hostile_against_others = yes
	check_all_titles = yes # setting this to true will check against all of someones titles, including vassal held titles
	press_claim = yes
	
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no
	
	is_independence = yes
	
	can_use = {
		ROOT = {
			primary_title = { temporary = yes } # Revolter or adventurer trigger
			war = no
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}
	}

	can_use_title = {
		tier = count
	}

	is_valid_title = {
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = 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 
			}
		}
	}

	on_success_title = {
		usurp_title_plus_barony_if_unlanded = { target = ROOT type = revolt }
		ROOT = { set_defacto_liege = ROOT }
		FROM = {
			prestige = -100
		}

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

	on_fail_title = {
		FROM = {
			prestige = 10
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor 
					who = ROOT
					months = 1200
				}
			}
		}
		ROOT = {
			clear_wealth = yes
			imprison = FROM
			hidden_tooltip = {
				add_character_modifier = { 
					name = broken_spirit
					days = -1
				}
			}
		}
	}

	on_reverse_demand = {
		FROM = {
			prestige = 20
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor 
					who = ROOT
					months = 1200
				}
				add_character_modifier = { 
					name = crushed_revolt
					days = 3650
				}
			}
		}
		ROOT = {
			clear_wealth = yes
			imprison = FROM 
			hidden_tooltip = {
				add_character_modifier = { 
					name = broken_spirit
					days = -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
	}
}

# The CB used by "Heretic revolter" characters
heretic_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
	
	can_use = {
		ROOT = { has_character_flag = heretic_revolter }
	}
	
	is_valid = {
		FROM = {
			NOT = { religion = ROOT }
		}
	}

	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 = heretic_revolter
		}
		
		FROM = {
			prestige = -500
			piety = -500
			religion_authority = {
				modifier = lost_heretic_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 }
		}

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

	on_fail = {
		ROOT = {
			clear_wealth = yes
			imprison = FROM
			clr_character_flag = heretic_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_heretic_revolt
			}
		}
	}

	on_reverse_demand = {
		ROOT = {
			clear_wealth = yes
			imprison = FROM
			clr_character_flag = heretic_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_heretic_revolt
			}
		}
	}

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

# The CB used by "Religious revolter" characters
religious_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
	
	can_use = {
		ROOT = { has_character_flag = religious_revolter }
	}
	
	is_valid = {
		FROM = {
			NOT = { religion = ROOT }
		}
	}

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

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

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

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

# The CB used by "Nationalist Revolter" characters
liberation_revolt = {
	name = CB_NAME_LIBERATION_REVOLT
	war_name = WAR_NAME_LIBERATION_REVOLT
	sprite = 16
	truce_days = 365
	hostile_against_others = yes
	check_de_jure_tier = KING # this scans all dejure duchies for the counties which are held by or vassals(or below) of selected character
	press_claim = yes
	can_ask_to_join_war = no
	is_independence = yes
	
	can_use = {
		ROOT = { has_character_flag = nationalist_rebel }
	}

	can_use_title = {
		tier = king
		has_holder = no
	}
	
	is_valid = {
		FROM = {
			NOT = { culture = ROOT }
		}
	}

	is_valid_title = {
		has_holder = no
	}

	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 = 1000
			occupy_minors_of_occupied_settlements = FROM
			gain_all_occupied_titles = { who = FROM type = revolt }
			clr_character_flag = nationalist_rebel
			give_nickname = nick_the_liberator
		}
		FROM = {
			prestige = -500
		}
	}

	on_success_title = {
		custom_tooltip = {
			text = tribal_invasion_succ_tip
			hidden_tooltip = {
				ROOT = {
					vassalize_or_take_under_title = {
						title = PREV
						enemy = FROM
						is_revolt = yes
						type = revolt
					}
					if = {
						limit = {
							num_of_count_titles = 5
							PREV = { 
								OR = {
									has_holder = no 
									holder_scope = {
										character = FROM
									}
								}
							}
						}
						usurp_title = { target = PREV type = revolt }
					}
				}
			}
		}

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

	on_fail_title = {
		FROM = {
			prestige = 100
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor 
					who = ROOT
					months = 1200
				}
			}
		}
		ROOT = {
			clear_wealth = yes
			imprison = FROM
			clr_character_flag = nationalist_rebel
			hidden_tooltip = {
				add_character_modifier = { 
					name = broken_spirit
					days = -1
				}
				remove_trait = inspiring_leader
			}
		}
	}

	on_reverse_demand = {
		FROM = {
			prestige = 200
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor 
					who = ROOT
					months = 1200
				}
				add_character_modifier = { 
					name = crushed_major_revolt
					days = 3650
				}
			}
		}
		ROOT = {
			clear_wealth = yes
			imprison = FROM
			clr_character_flag = nationalist_rebel
			hidden_tooltip = {
				add_character_modifier = { 
					name = broken_spirit
					days = -1
				}
				remove_trait = inspiring_leader
			}
		}
	}

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

duchy_adventure = {
	name = CB_NAME_DUCHY_ADVENTURE
	war_name = WAR_DUCHY_ADVENTURE
	sprite = 9
	truce_days = 3650
	hostile_against_others = 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

	can_use = {
		ROOT = { has_character_flag = duchy_adventurer }
	}

	is_valid = {
		always = yes
	}

	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 = 250
			hidden_tooltip = {
				clr_character_flag = duchy_adventurer
				give_nickname = nick_the_conqueror
			}
		}
		FROM = {
			prestige = -250 
		}
	}

	on_success_title = {
		custom_tooltip = {
			text = other_invasion_succ_tip
			hidden_tooltip = {
				ROOT = {
					vassalize_or_take_under_title = {
						title = PREV
						enemy = FROM
						type = host
					}
				}
			}
		}
		
		ROOT = {
			hidden_tooltip = {
				# Special case: Seljuk succeeds in his first conquest
				if = {
					limit = {
						has_character_flag = seljuk_himself
					}
					narrative_event = {
						id = 60206
						days = 1
					}
				}
				
				# Special case: Timur succeeds in his first conquest
				if = {
					limit = {
						has_character_flag = timur_himself
					}
					narrative_event = {
						id = 60306
						days = 1
					}
				}
				
				# Special case: Sabuktigin succeeds in his first conquest
				if = {
					limit = {
						has_character_flag = sabuktigin_himself
					}
					narrative_event = {
						id = 60406
						days = 1
					}
				}
				
				# Special case: Sultanate of Rum creator succeeds in his first conquest
				if = {
					limit = {
						has_character_flag = rum_creator
					}
					character_event = {
						id = 60610
						days = 600
						random = 300
					}
				}
			}
		}

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

	on_fail = {
		FROM = {
			prestige = 100
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor 
					who = ROOT
					months = 1200
				}
			}
		}
		ROOT = {
			clear_wealth = yes
			imprison = FROM 
			hidden_tooltip = {
				disband_event_forces = yes
				clr_character_flag = duchy_adventurer
			}
		}
	
		ROOT = { prestige = -100 }
	}

	on_reverse_demand = {
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		
		FROM = {
			prestige = 200
		}
		
		ROOT = {
			clear_wealth = yes
			imprison = FROM
			prestige = -200
			hidden_tooltip = {
				disband_event_forces = yes
				clr_character_flag = duchy_adventurer
			}
		}
	}
	
	on_attacker_leader_death = {
		ROOT = {
			hidden_tooltip = {
				disband_event_forces = yes
				clr_character_flag = duchy_adventurer
			}
		}
		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
	}
}

kingdom_adventure = { # Used mainly for Muslims adventuring into India
	name = CB_NAME_DUCHY_ADVENTURE
	war_name = WAR_DUCHY_ADVENTURE
	sprite = 9
	truce_days = 3650
	hostile_against_others = yes
	check_de_jure_tier = KING # this scans all dejure duchies for the counties which are held by or vassals(or below) of selected character

	can_use = {
		ROOT = {
			OR = {
				has_character_flag = duchy_adventurer
				has_character_flag = rum_creator
			}
		}
	}

	is_valid = {
		always = yes
	}

	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 = 500
			piety = 250
			hidden_tooltip = {
				clr_character_flag = duchy_adventurer
				clr_character_flag = do_not_disturb
				if = {
					limit = {
						has_nickname = no
					}
					FROM = {
						save_event_target_as = nickname_target
					}
					ROOT = { 
						save_event_target_as = nickname_receiver
					}
					if = {
						limit = {
							NOT = {
								has_character_flag = is_child_of_destiny
							}
						}
						random_list = {
							5 = { give_nickname = nick_the_victorious }
							5 = { give_nickname = nick_the_conqueror }
							5 = { give_nickname = nick_the_dragon }
							10 = { give_nickname = nick_the_rel_group }
							10 = { give_nickname = nick_the_tiger }
							10 = { give_nickname = nick_the_world_burner }
							10 = { 
								give_nickname = nick_the_scourge_of_god
								modifier = {
									factor = 0
									event_target:nickname_target = { religion = ROOT }
								}
							}
						}
					}
				}
			}
		}
		FROM = {
			prestige = -500
			piety = -250			
		}
	}

	on_success_title = {
		custom_tooltip = {
			text = other_invasion_succ_tip
			hidden_tooltip = {
			
				if = { # Special stuff for player Child of Destiny (AI Children do not have this flag)
					limit = {
						ROOT = { has_character_flag = child_of_destiny_outwards_focus }
					}
					FROM = { # Ensure that the Child of Destiny always gets one County in the targeted Kingdom (mainly useful when testing with yesmen, but can also happen in rare occurences where the target is imprisoned)
						random_realm_title = {
							limit = {
								tier = COUNT
								de_jure_liege_or_above = PREVPREV
							}
							usurp_title = { target = ROOT type = host }
						}
					}
					ROOT = {
						liege = { save_event_target_as = old_liege_target}
						if = { # Abandon all old titles if you were a vassal
							limit = {
								independent = no
							}
							any_demesne_title = {
								limit = {
									NOR = {
										title = PREVPREV
										de_jure_liege_or_above = PREVPREV
									}
								}
								grant_title = event_target:old_liege_target
							}
							any_vassal = {
								limit = {
									capital_scope = {
										NOR = {
											title = PREVPREVPREV
											de_jure_liege_or_above = PREVPREVPREV
										}
									}
								}
								set_defacto_liege = event_target:old_liege_target
							}
						}
					}
				}
				
				FROM = { # The adventurer takes ANY demesne title, including the targeted Kingdom, within the area of the targeted kingdom
					any_demesne_title = {
						limit = {
							OR = {
								title = PREVPREV
								de_jure_liege_or_above = PREVPREV
							}
							NOT = { ROOT = { has_character_flag = rum_creator } }
						}
						usurp_title = { target = ROOT type = host }
					}
				}
				
				if = { # Claim the targeted Kingdom if it isn't held by anyone
					limit = {
						has_holder = no
					}
					usurp_title = { target = ROOT type = host }
				}
				
				if = { # Create a Kingdom if the adventurer does not have one by this point
					limit = {
						ROOT = {
							NOR = {
								any_demesne_title = {
									tier = KING
								}
								has_character_flag = rum_creator
							}
						}
					}
					if = {
						limit = {
							ROOT = {
								NOT = {
									has_character_flag = is_child_of_destiny
								}
							}
						}
						ROOT = {
							create_title = {
								tier = KING
								landless = no
								temporary = no
								custom_created = yes
								culture = ROOT
								holder = ROOT
								name = "INDIAN_MUSLIM_KINGDOM"
								base_title = PREV
								copy_title_laws = yes
							}
						}
					}
					else = {
						ROOT = {
							save_event_target_as = target_name
							create_title = {
								tier = KING
								landless = no
								temporary = no
								custom_created = yes
								culture = ROOT
								holder = ROOT
								name = "DESTINY_KINGDOM"
								base_title = PREV
								copy_title_laws = yes
							}
							capital_scope = {
								county = {
									save_event_target_as = coa_target
								}
							}
							primary_title = {
								set_coa = event_target:coa_target
							}
						}
					}
				}
				if = {
					limit = {
						ROOT = { has_character_flag = rum_creator }
					}
					k_rum = {
						grant_title = ROOT
					}
				}
				
				ROOT = { # Preserve Vassals under the targeted kingdom
					vassalize_or_take_under_title = { # Take all vassals held by the target under the title
						title = PREV
						enemy = FROM
						type = host
					}
					set_defacto_liege = ROOT # Always go Independent
				}
				
				hidden_effect = {
					if = {
						limit = {
							ROOT = {
								has_character_flag = is_child_of_destiny
								NOT = {
									religion_group = muslim
								}
							}
						}
						ROOT = {
							any_demesne_title = {
								limit = {
									higher_tier_than = COUNT
								}
								add_law = succ_primogeniture
							}
						}
					}
				}
			}
		}
		
		ROOT = {
			hidden_tooltip = {
				if = {
					limit = {
						has_character_flag = rum_creator
					}
					capital_scope = {
						culture = ROOT
					}
				}
				else_if = {
					limit = {
						has_character_flag = is_child_of_destiny
					}
					character_event = { id = HF.303 days = 3 }
				}
				else = {
					capital_scope = {
						reverse_culture = ROOT
					}
					any_child = {
						limit = {
							is_ruler = no
							ai = yes
							#liege = { character = ROOT }
						}
						culture = ROOT
					}
					any_courtier = {
						limit = {
							is_ruler = no
							ai = yes
						}
						culture = ROOT
					}
				}
				
				if = { # Give bloodline to Child of Destiny
					limit = {
						has_character_flag = is_child_of_destiny
						higher_real_tier_than = DUKE
						NOT = {
							any_owned_bloodline = {
								has_bloodline_flag = created_bloodline
							}
						}
					}
					character_event = { id = HF.304 days = 14 }
				}
				
				# Special case: Sultanate of Rum might start yet another war
				if = {
					limit = {
						has_character_flag = rum_creator
					}
					character_event = {
						id = 60610
						days = 600
						random = 300
					}
				}
			}
		}

		hidden_tooltip = {
			check_if_reconquista_finished_effect = yes
			FROM = {
				landless_pope_effect = yes
			}
		}
	}
	
	on_invalidation = {
		ROOT = {
			clr_character_flag = child_of_destiny_made_player_choice
			clr_character_flag = child_of_destiny_adventure
		}
	}

	on_fail = {
		FROM = {
			prestige = 100
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor 
					who = ROOT
					months = 1200
				}
			}
		}
		ROOT = {
			clear_wealth = yes
			imprison = FROM 
			hidden_tooltip = {
				disband_event_forces = yes
				clr_character_flag = duchy_adventurer
				if = {
					limit = {
						has_character_flag = is_child_of_destiny
					}
					remove_trait = disinherited
				}
			}
		}
	
		ROOT = { prestige = -100 }
		
		if = {
			limit = {
				ROOT = {
					has_character_flag = is_child_of_destiny
				}
			}
			set_global_flag = child_of_destiny_invasion_fail@ROOT
		}
	}

	on_reverse_demand = {
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		
		FROM = {
			prestige = 200
		}
		
		ROOT = {
			clear_wealth = yes
			imprison = FROM
			prestige = -200
			hidden_tooltip = {
				disband_event_forces = yes
				clr_character_flag = duchy_adventurer
				if = {
					limit = {
						has_character_flag = is_child_of_destiny
					}
					remove_trait = disinherited
				}
			}
		}
		
		if = {
			limit = {
				ROOT = {
					has_character_flag = is_child_of_destiny
				}
			}
			set_global_flag = child_of_destiny_invasion_reverse_demand@ROOT
		}
	}
	
	on_attacker_leader_death = {
		ROOT = {
			hidden_tooltip = {
				disband_event_forces = yes
				clr_character_flag = duchy_adventurer
			}
		}
		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
	}
}

pagan_holy_war = {
	name = CB_NAME_RELIGIOUS
	war_name = WAR_NAME_RELIGIOUS
	sprite = 9
	truce_days = 1825
	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 = no
	
	sort_priority = 800
	
	can_use_gui = {
		ROOT = {
			piety = 100
		}
	}
	
	on_add = {
		ROOT = { piety = -100 }
		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 = {
		ROOT = {
			OR = {
				religion = bon_reformed
				religion = hellenic_pagan_reformed
				religion = norse_pagan_reformed
				religion = tengri_pagan_reformed
				religion = baltic_pagan_reformed
				religion = slavic_pagan_reformed
				religion = finnish_pagan_reformed
				religion = aztec_reformed
				religion = aztec
				religion = west_african_pagan_reformed
				religion = zun_pagan_reformed
			}
			NOT = { has_religion_feature = religion_peaceful }
			NOT = { same_realm = FROM }
			NOT = { religion = FROM }
			top_liege = {
				religion = ROOT
			}
			mercenary = no
		}
	}
	
	can_use_title = {
		# 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_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 = {
		ROOT = {
			OR = {
				religion = bon_reformed
				religion = hellenic_pagan_reformed
				religion = norse_pagan_reformed
				religion = tengri_pagan_reformed
				religion = baltic_pagan_reformed
				religion = slavic_pagan_reformed
				religion = finnish_pagan_reformed
				religion = aztec_reformed
				religion = aztec
				religion = west_african_pagan_reformed
				religion = zun_pagan_reformed
			}
			NOT = { same_realm = FROM }
			NOT = { religion = FROM }
			top_liege = {
				NOT = { religion = FROM }
			}
		}
	}
	
	on_success = {
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_piety = 100
				participation_scaled_prestige = 50
			}
			hidden_tooltip = {
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
		}
		
		ROOT = {
			show_scope_change = no
			participation_scaled_piety = 100
			participation_scaled_prestige = 50
			religion_authority = {
				modifier = won_holy_war
				years = 20
			}
			hidden_tooltip = {
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
			hidden_tooltip = {
				if = {
					limit = {
						OR = {
							this_has_lame_nickname_trigger = yes
							has_nickname = no
						}
					}
					random_list = {
						10 = { give_nickname = nick_the_dragon }
						10 = { give_nickname = nick_the_holy }
						10 = { give_nickname = nick_bloodaxe }
						10 = { give_nickname = nick_the_great }
						40 = { give_nickname = nick_the_sword_of_god }
					}
				}
			}
		}
		
		FROM = { 
			religion_authority = {
				modifier = lost_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_cb_succ_tip
			hidden_tooltip = {
				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
					}
				}
			}
		}
	}

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

	on_reverse_demand = {
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			participation_scaled_piety = 200
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_piety = 200
				participation_scaled_prestige = 100
			}
			hidden_tooltip = {
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
		}
		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 = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_nomadic = no
				NOT = {
					is_tribal = yes
				}
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}
				}
			}
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_tribal = yes
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
							holding_type = tribal
						}
					}
				}
			}
		}
		
		modifier = {
			factor = 0.9 # Prefer using CB's without on_add costs
		}
		
		modifier = {
			factor = 0.1
			ROOT = {
				culture = hungarian
				primary_title = {
					OR = {
						title = k_hungary
						title = e_carpathia
					}
				}
			}
			empire = {
				OR = {
					title = e_tartaria
					title = e_russia
				}
			}
		}
	}
}

# Caliphs can subjugate people of their own religion
caliphal_subjugation = {
	name = CB_NAME_PAGAN_SUBJUGATION
	war_name = WAR_NAME_PAGAN_SUBJUGATION
	sprite = 8
	truce_days = 365
	hostile_against_others = yes
	is_permanent = yes
	can_ask_to_join_war = yes
	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
	allowed_to_target_tributaries = no
	
	sort_priority = 1000
	
	can_use_gui = {
		ROOT = {
			piety = 200
		}
	}
	
	on_add = {
		ROOT = { piety = -200 }
		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 = {
		ROOT = {
			OR = {
				ai = yes
				multiplayer = no
				NOT = {
					has_game_rule = {
						name = multiplayer_invasions
						value = off
					}
				}
			}
			religion_group = muslim
			OR = {
				has_landed_title = d_sunni
				has_landed_title = d_shiite
				has_landed_title = d_ibadi
				has_landed_title = d_kharijite
				has_landed_title = d_yazidi
				has_landed_title = d_hurufi
			}
			is_nomadic = no
			OR = {
				has_horde_culture = no # Should use tribal invasion instead
				has_dlc = "Horse Lords"
			}
			NOT = { has_character_modifier = launched_subjugation }
			mercenary = no
		}
		FROM = {
			religion = ROOT
			NOT = { is_offmap_governor = yes }
		}
	}
	
	can_use_title = {
		FROM = {
			any_realm_province = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}
	
	is_valid_title = {
		FROM = {
			any_realm_province = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}
	
	on_add_title = {
		ROOT = {
			add_character_modifier = { 
				name = launched_subjugation
				days = 3650
				hidden = yes
			}
		}
	}

	on_success = {
		ROOT = {
			prestige = 100
			decadence = -5
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		FROM = {
			prestige = -200
		}
	}
	
	on_success_title = {
		custom_tooltip = {
			text = pagan_subjugation_tip
			hidden_tooltip = {
				ROOT = {
					subjugate_or_take_under_title = { # If the target only has territory within the kingdom, he is simply vassalized
						title = PREV
						enemy = FROM
					}
				}
			}
		}
	}

	on_fail = {
		FROM = {
			prestige = 50
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		ROOT = {
			prestige = -100
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
		}
		FROM = {
			prestige = 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
						}
				}
				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 = {
			factor = 0.9 # Prefer using CB's without on_add costs
		}
	}
}

muslim_county_conquest = {
	name = CB_NAME_COUNTY_CONQUEST
	war_name = WAR_NAME_COUNTY_CONQUEST
	sprite = 16
	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
	
	sort_priority = 700
	
	can_use_gui = {
		ROOT = {
			piety = 50
		}
	}
	
	on_add = {
		ROOT = { piety = -50 }
		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 = {
		ROOT = {
			NOT = { is_liege_or_above = FROM }
			mercenary = no
			is_nomadic = no # Use the 'pagan_county_conquest' CB instead
		}
	}

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

		NOT = {
			OR = {
				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 = {
						OR = {
							character = FROM
							is_liege_or_above = FROM
						}
					}
				}

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

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

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			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.9 # Prefer using CB's without on_add costs
		}
		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 = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_nomadic = no
				NOT = {
					is_tribal = yes
				}
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}
				}
			}
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_tribal = yes
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
							holding_type = tribal
						}
					}
				}
			}
		}
	}
}

# 42
pagan_county_conquest = {
	name = CB_NAME_COUNTY_CONQUEST
	war_name = WAR_NAME_COUNTY_CONQUEST
	sprite = 22
	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
	
	allow_distant = yes
	allowed_to_target_tributaries = no
	
	sort_priority = 700
	
	can_use = {
		ROOT = {
			mercenary = no
		}
		OR = {
			ROOT = {
				is_nomadic = yes
				NOT = { same_realm = FROM }
				FROM = {
					OR = {
						is_nomadic = yes
						is_tribal = yes
					}
				}
			}
			AND = {
				ROOT = {
					religion_group = pagan_group
					NOT = { same_realm = FROM }
				}
				FROM = {
					NOT = { religion = ROOT }
				}
			}
		}
	}

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

		NOT = {
			OR = {
				ROOT = {
					has_landed_title = PREV
				}
				holder_scope = {
					is_liege_or_above = ROOT
				}
			}
		}
		
		OR = {
			AND = {
				OR = {
					ROOT = { religion = norse_pagan }
					ROOT = { religion = norse_pagan_reformed }
				}
				location = {
					port = yes
				}
				has_global_flag = viking_age_started
			}
			location = {
				any_neighbor_province = {
					owner = {
						OR = {
							character = ROOT
							is_liege_or_above = ROOT
						}
					}
				}
			}
		}
	}

	is_valid_title = {
		OR = {
			ROOT = {
				is_nomadic = yes
			}
			AND = {
				ROOT = {
					religion_group = pagan_group
					NOT = { same_realm = FROM }
				}
				FROM = {
					NOT = { religion = ROOT }
				}
			}
		}
	
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = 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 
			}
		}
	}

	on_success_title = { #winning the war
		usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
		
		any_de_jure_vassal_title = { # take all baronies too
			limit = {
				has_holder = yes
				holder_scope = {
					OR = {
						character = FROM
						is_liege_or_above = FROM
						AND = {
							is_liege_or_above = ROOT
							NOT = { culture = ROOT }
							NOT = { religion = ROOT }
						}
					}
				}
			}
			show_scope_change = no
			usurp_title = ROOT
		}
		
		ROOT = {
			show_scope_change = no
			participation_scaled_prestige = 100
			participation_scaled_piety = 50
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_prestige = 100 
				participation_scaled_piety = 50
			}
		}
		
		if = {
			limit = {
				NOT = { FROM = { religion = ROOT } }
			}
			ROOT = {
				show_scope_change = no
				religion_authority = {
					modifier = won_county_conquest
				}
			}
		}

		#To handle Warrior Lodge mission "Wage War" completion...
		attacker = { save_event_target_as = attacker_target }
		defender = { save_event_target_as = defender_target }

		if = { 
			limit = {
				event_target:attacker_target = { has_quest = quest_warrior_lodge_war }
				is_quest_target_of = event_target:attacker_target
			}
			event_target:attacker_target = {
				show_scope_change = no
				tooltip = { clr_quest = quest_warrior_lodge_war }
				set_character_flag = won_war@event_target:defender_target
				#character_event = { id = HF.10514 days = 3 } #Sent from on_action instead
			}
		}
	}

	on_fail_title = { #white peace
		ROOT = {
			show_scope_change = no
			prestige = -100
		}
		
		if = {
			limit = {
				NOT = { FROM = { religion = ROOT } }
			}
			ROOT = {
				show_scope_change = no
				religion_authority = {
					modifier = lost_county_conquest
				}
			}
		}
	
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}

		#To handle Warrior Lodge mission "Wage War" completion...
		attacker = { save_event_target_as = attacker_target }
		defender = { save_event_target_as = defender_target }

		if = { 
			limit = {
				event_target:attacker_target = { has_quest = quest_warrior_lodge_war }
				is_quest_target_of = event_target:attacker_target
			}
			event_target:attacker_target = {
				show_scope_change = no
				tooltip = { clr_quest = quest_warrior_lodge_war }
				set_character_flag = white_peace@event_target:defender_target
				#character_event = { id = HF.10514 days = 3 } #Sent from on_action instead
			}
		}
	}

	on_reverse_demand = { #losing the war
		ROOT = {
			show_scope_change = no
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		
		if = {
			limit = {
				NOT = { FROM = { religion = ROOT } }
			}
			ROOT = {
				show_scope_change = no
				religion_authority = {
					modifier = lost_county_conquest
				}
			}
		}
		
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 100
			participation_scaled_piety = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_prestige = 100 
				participation_scaled_piety = 50
			}
		}
	}
	on_reverse_demand_title = {
		#To handle Warrior Lodge mission "Wage War" completion...
		attacker = { save_event_target_as = attacker_target }
		defender = { save_event_target_as = defender_target }

		if = { 
			limit = {
				event_target:attacker_target = { has_quest = quest_warrior_lodge_war }
				is_quest_target_of = event_target:attacker_target
			}
			event_target:attacker_target = {
				show_scope_change = no
				tooltip = { clr_quest = quest_warrior_lodge_war }
				set_character_flag = lost_war@event_target:defender_target
				#character_event = { id = HF.10514 days = 3 } #Sent from on_action instead
			}
		}
	}

	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 = 4.0
			ROOT = {
				OR = {
					religion = norse_pagan
					religion = norse_pagan_reformed
				}
			}
			OR = {
				empire = { title = e_scandinavia }
				kingdom = { title = k_rus }
			}
		}
		modifier = {
			factor = 2.0
			ROOT = {
				OR = {
					religion = norse_pagan
					religion = norse_pagan_reformed
				}
			}
			has_global_flag = viking_age_started
			OR = {
				empire = { title = e_britannia }
				kingdom = { title = k_brittany }
				kingdom = { title = k_france }
				kingdom = { title = k_frisia }
			}
		}
		modifier = {
			factor = 2.0
			ROOT = {
				OR = {
					religion = norse_pagan
					religion = norse_pagan_reformed
				}
			}
			has_global_flag = viking_age_started
			FROM = { NOT = { religion_group = pagan_group } }
			location = { port = yes }
		}
		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 = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_nomadic = no
				NOT = {
					is_tribal = yes
				}
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}
				}
			}
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_tribal = yes
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
							holding_type = tribal
						}
					}
				}
			}
		}
	}
}

# 43
dejure_county_claim = {
	name = CB_NAME_DEJURECLAIM
	war_name = WAR_NAME_DEJURECLAIM
	sprite = 16
	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

	infamy_modifier = 0.5
	
	sort_priority = 700
	
	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use_title = {
		tier = count
		
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
		
		OR = {
			kingdom = {
				holder = ROOT
			}
			empire = {
				holder = ROOT
			}
			dejure_liege_title = {
				holder = ROOT
			}
		}
		
		NOT = {
			OR = {
				ROOT = {
					has_landed_title = PREV
				}
				holder_scope = {
					is_liege_or_above = ROOT
				}
			}
		}
		
		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
						}
					}
				}
			}
		}
	}

	is_valid_title = {
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = 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 
			}
		}
	}

	on_success_title = {
		if = {
			limit = {
				holder_scope = {
					tier = count
					is_patrician = no
					NOT = {
						num_of_count_titles = 2
					}
				}
			}

			holder_scope = {
				set_defacto_liege = ROOT
			}
		}

		if = {
			limit = {
				holder_scope = {
					OR = {
						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 }
			}
		}
		ROOT = {
			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 = {
			prestige = -100
		}
		FROM = {
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			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
				}
			}
		}
		
		modifier = {
			factor = 0 # AIs will not take the last counties off their liege's direct vassals
			ROOT = {
				same_liege = FROM
			}
			FROM = {
				NOT = {
					any_demesne_title = {
						tier = count
						NOT = { title = PREVPREV }
					}
				}
			}
		}
		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
			}
		}
	}
}

# 44
dejure_barony_claim = {
	name = CB_NAME_DEJUREBARONYCLAIM
	war_name = WAR_NAME_DEJUREBARONYCLAIM
	sprite = 16
	truce_days = 3650
	hostile_against_others = yes
	is_permanent = yes
	check_de_jure_tier = COUNT
	can_ask_to_join_war = no
	allowed_to_target_tributaries = no

	infamy_modifier = 0.5
	
	sort_priority = 650
	
	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		FROM = {
			NOT = { is_vassal_or_below = ROOT }
			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_title = {
		has_holder = yes
		holder_scope = {
			OR = {
				character = ROOT
				is_liege_or_above = ROOT
			}
		}
		any_direct_de_jure_vassal_title = {
			holder_scope = {
				OR = {
					character = FROM
					is_liege_or_above = FROM
				}
				NOR = {
					character = ROOT
					is_liege_or_above = ROOT
				}
			}
		}
	}

	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_title = {
		holder_scope = {
			save_event_target_as = target_taker
		}
		
		any_direct_de_jure_vassal_title = {
			limit = {
				holder_scope = {
					OR = {
						character = FROM
						is_liege_or_above = FROM
					}
					NOR = {
						character = event_target:target_taker
						is_liege_or_above = event_target:target_taker
						is_liege_or_above = ROOT
					}
				}
			}
			
			usurp_title_plus_barony_if_unlanded = { target = event_target:target_taker type = claim }
		}
	
		ROOT = {
			participation_scaled_prestige = 50
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_fail_title = {
		ROOT = {
			prestige = -50
		}
		FROM = {
			participation_scaled_prestige = 25
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 25 }
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -100
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

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

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

# 45 - OBSOLETE AND TURNED OFF
other_dejure_barony_claim = {
	name = CB_NAME_DEJUREBARONYCLAIM
	war_name = WAR_NAME_OTHER_DEJUREBARONYCLAIM
	sprite = 16
	truce_days = 3650
	is_permanent = no

	can_use = {
		always = no # Obsolete, disabled
	}

	is_valid_title = {
		always = no
	}

	on_success = {
	}

	on_success_title = {
	}

	on_fail = {
	}

	on_reverse_demand = {
	}

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

# 46
other_dejure_county_claim = {
	name = CB_NAME_DEJURECLAIM
	war_name = WAR_NAME_OTHER_DEJURECLAIM
	sprite = 16
	truce_days = 3650
	hostile_against_others = yes
	is_permanent = yes
	other_de_jure_claim = yes # Optimization for de jure claims of vassals
	press_claim = yes
	can_ask_to_join_war = no

	allowed_to_target_tributaries = no

	infamy_modifier = 0.3
	
	sort_priority = 700
	
	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		NOT = {
			character = ROOT
		}
		
		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
						}
					}
				}
			}
		}
		
		mercenary = no
	}

	can_use_title = {
		tier = count
		dejure_liege_title = {
			has_holder = yes
			ROOT = {
				has_landed_title = PREV
			}
		}

		holder_scope = {
			NOR = {
				character = ROOT
				character = FROMFROM
				is_liege_or_above = ROOT
				is_liege_or_above = FROMFROM # The potential attacker
			}
		}

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

	is_valid_title = {
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = 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 
			}
		}
	}

	on_success = {
		ROOT = {
			opinion = {
				modifier = pressed_my_claim 
				who = PREV
			}
			show_scope_change = no
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

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

			holder_scope = {
				show_scope_change = no
				set_defacto_liege = ROOT
			}
		}

		if = {
			limit = {
				holder_scope = {
					OR = {
						higher_tier_than = count
						num_of_count_titles = 2
					}
				}
			}
			show_scope_change = no
			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
				show_scope_change = no
				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 }
			}
		}

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

	on_fail = {
		prestige = -50
		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 = { #Attacker lost
		prestige = -100
		transfer_scaled_wealth = {
			to = FROM
			value = 4.0
		}
		ROOT = {
			show_scope_change = no
			prestige = -200
		}
		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
	}
}

# The special CB for the Rise of the Shi'a Caliphate
shia_caliphate_rising = {
	name = CB_NAME_SHIA_CALIPHATE_RISING
	war_name = WAR_NAME_SHIA_CALIPHATE_RISING
	sprite = 8
	truce_days = 365
	hostile_against_others = no
	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
	apply_short_occ_mod = no # Do not apply the 'recently_conquered' modifier to Holdings

	can_use = {
		ROOT = {
			has_character_flag = shia_caliphate_pretender
		}
	}
	
	is_valid_title = {
		ROOT = {
			has_character_flag = shia_caliphate_pretender
		}
		FROM = {
			any_realm_title = {
				de_jure_liege_or_above = PREVPREV
			}
		}
		d_shiite = {
			has_holder = no
		}
	}

	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 = {
		clr_global_flag = shia_caliphate_revolt_ongoing
		ROOT = {
			piety = 500
			religion_authority = {
				modifier = rise_of_the_shia_caliphate
			}
			clr_character_flag = shia_caliphate_pretender
		}
		FROM = {
			piety = -500
		}
	}
	
	on_success_title = {
		custom_tooltip = {
			text = tribal_invasion_succ_tip
			hidden_tooltip = {
				d_shiite = {
					usurp_title = { target = ROOT type = holy_war }
				}
				FROM = {
					random_realm_province = {
						limit = {
							de_jure_liege_or_above = PREVPREV
							NOT = { religion = ROOT }
							religion_group = muslim
						}
						religion = ROOT
					}
					random_realm_province = {
						limit = {
							de_jure_liege_or_above = PREVPREV
							NOT = { religion = ROOT }
							religion_group = muslim
						}
						religion = ROOT
					}
					random_realm_province = {
						limit = {
							de_jure_liege_or_above = PREVPREV
							NOT = { religion = ROOT }
							religion_group = muslim
						}
						religion = ROOT
					}
					random_realm_province = {
						limit = {
							de_jure_liege_or_above = PREVPREV
							NOT = { religion = ROOT }
							religion_group = muslim
						}
						religion = ROOT
					}
					random_realm_province = {
						limit = {
							de_jure_liege_or_above = PREVPREV
							NOT = { religion = ROOT }
							religion_group = muslim
						}
						religion = ROOT
					}
				}
				ROOT = {
					occupy_minors_of_occupied_settlements = FROM
					gain_all_occupied_titles = { who = FROM type = revolt }
					if = {
						limit = {
							PREV = {
								OR = {
									has_holder = no
									holder_scope = {
										character = FROM
									}
								}
							}
						}
						usurp_title = { target = PREV type = revolt }
					}
					if = {
						limit = {
							NOT = {
								ROOT = {
									any_demesne_title = {
										tier = KING
									}
								}
							}
						}
						create_title = {
							tier = KING
							landless = no
							temporary = no
							custom_created = yes
							culture = ROOT
							holder = ROOT
							name = "SHIA_CUSTOM_FALLBACK"
							base_title = PREV
							copy_title_laws = yes
						}
					}
					
					vassalize_or_take_under_title = {
						title = PREV
						enemy = FROM
						is_religious = yes
						type = revolt
					}
					
					any_demesne_title = {
						limit = { tier = BARON }
						remove_holding_modifier = recently_conquered
						refill_holding_levy = yes
					}
					wealth = 500
				}
				
				if = {
					limit = {
						FROM = { is_liege_or_above = ROOT }
					}
					FROM = { imprison = ROOT }
				}
			}
		}
	}

	on_fail = {
		clr_global_flag = shia_caliphate_revolt_ongoing
		FROM = {
			piety = 250
		}
		ROOT = {
			piety = -250
			clr_character_flag = shia_caliphate_pretender
		}
	}

	on_reverse_demand = {
		clr_global_flag = shia_caliphate_revolt_ongoing
		ROOT = {
			piety = -500
			clr_character_flag = shia_caliphate_pretender
			imprison = FROM
		}
		FROM = {
			piety = 250
			participation_scaled_piety = 250
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_piety = 250 }
		}
	}
	
	on_attacker_leader_death = {
		clr_global_flag = shia_caliphate_revolt_ongoing
		ROOT = {
			clr_character_flag = shia_caliphate_pretender
		}
		end_war = invalid
	}
	
	on_invalidation = {
		clr_global_flag = shia_caliphate_revolt_ongoing
	}

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

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

depose_antipope = {
	name = CB_NAME_DEPOSE_ANTIPOPE
	war_name = WAR_NAME_DEPOSE_ANTIPOPE
	sprite = 29
	truce_days = 3650
	is_permanent = yes
	can_ask_to_join_war = yes
	
	defender_unoccupied_warscore = yes
	
	attacker_rel_head_is_ally = yes # The attacker can call his (main) Pope into the war
	allowed_to_target_tributaries = no
	
	sort_priority = 750
	
	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		ROOT = {
			religion = FROM
			OR = {
				religion = catholic
				religion = fraticelli
			}
			rightful_religious_head_scope = {
				NOT = { has_claim = k_papal_state }
				NOT = { has_claim = d_fraticelli }
			}
			mercenary = no
		}
		FROM = {
			rightful_religious_head_scope = {
				is_liege_or_above = PREV
				OR = {
					has_claim = k_papal_state
					has_claim = d_fraticelli
				}
			}
		}
	}
	
	is_valid = {
		ROOT = {
			religion = FROM
			OR = {
				religion = catholic
				religion = fraticelli
			}
			rightful_religious_head_scope = {
				NOT = { has_claim = k_papal_state }
				NOT = { has_claim = d_fraticelli }
			}
		}
		FROM = {
			rightful_religious_head_scope = {
				is_liege_or_above = PREV
				OR = {
					has_claim = k_papal_state
					has_claim = d_fraticelli
				}
			}
		}
	}

	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_invalidation = {
		# Coronation request locks cleared on invalidation or loss, to avoid locking the player in the middle of the negotiation forever.
		hidden_tooltip = {
			ROOT = {
				clr_character_flag = flag_antipope_for_coronation 
			}
			FROM = {
				clr_character_flag = flag_antipope_for_coronation 
			}
		}
	}

	on_success = {
	if = {
		limit = { 
			ROOT = {
				has_character_flag = flag_antipope_for_coronation
			}
		}
		ROOT = {
			character_event = { id = HF.20220 } #Papal Coronation may proceed.
		}
	}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_piety = 500
				participation_scaled_prestige = 250
			}
		}
		
		ROOT = {
			participation_scaled_piety = 500
			participation_scaled_prestige = 250
			religion_authority = {
				modifier = deposed_antipope
				years = 50
			}
			
			if = {
				limit = {
					rightful_religious_head_scope = {
						ROOT = {
							excommunicated_for = PREV
						}
					}
				}
				excommunicate = no
			}
			
			rightful_religious_head_scope = {
				opinion = {
					who = ROOT
					modifier = opinion_deposed_antipope
				}
			}
		}
		
		FROM = { 
			piety = -500
			prestige = -250
			
			rightful_religious_head_scope = {
				remove_claim = k_papal_state
				remove_claim = d_fraticelli
			}
		}
	}

	on_fail = {
		hidden_tooltip = {
			FROM = {
				clr_character_flag = flag_antipope_for_coronation
			}
			ROOT = {
				clr_character_flag = flag_antipope_for_coronation
			}
		}
		ROOT = { 
			piety = -250
			prestige = -125
		}
		FROM = { 
			piety = 100
			prestige = 50
		}
	}

	on_reverse_demand = {
		hidden_tooltip = {
			FROM = {
				clr_character_flag = flag_antipope_for_coronation
			}
			ROOT = {
				clr_character_flag = flag_antipope_for_coronation
			}
		}
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -500
			prestige = -250
		}
		
		FROM = { 
			piety = 500
			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
	}
}

# Faction cb to install antiking
cb_install_antiking = {
	name = CB_NAME_INSTALL_ANTIKING
	war_name = WAR_NAME_INSTALL_ANTIKING
	sprite = 11
	truce_days = 3650
	
	major_revolt = yes
	attacker_can_call_allies = no
	
	attacker_rel_head_is_ally = yes # The attacker can call his (main) Pope into the war
	
	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		ROOT = {
			vassal_of = FROM
			religion = FROM
			OR = {
				religion = catholic
				religion = fraticelli
			}
		}
	}
	
	is_valid_title = {
		NOT = {
			holder_scope = {
				character = ROOT
			}
		}
	}
	is_valid = {
		ROOT = {
			religion = FROM
			OR = {
				religion = catholic
				religion = fraticelli
			}
		}
		
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}

	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 = {
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_piety = 500
				participation_scaled_prestige = 250
			}
		}
		
		ROOT = {
			participation_scaled_piety = 500
			participation_scaled_prestige = 250
			religion_authority = {
				modifier = deposed_antipope
				years = 50
			}
			
			if = {
				limit = {
					religion = catholic
				}
				k_papal_state = {
					holder_scope = {
						opinion = {
							who = ROOT
							modifier = opinion_deposed_antipope
						}
					}
				}
			}
			if = {
				limit = {
					religion = fraticelli
				}
				d_fraticelli = {
					holder_scope = {
						opinion = {
							who = ROOT
							modifier = opinion_deposed_antipope
						}
					}
				}
			}
		}
		
		FROM = { 
			piety = -500
			prestige = -250
			
			hidden_tooltip = {
				rightful_religious_head_scope = {
					remove_claim = k_papal_state
					remove_claim = d_fraticelli
				}
			}
			
			primary_title = {
				usurp_title = { target = ROOT type = revolt }
			}
		}
		if = {
			limit = { primary_title = { has_law = investiture_law_1 } }
			primary_title = { add_law_w_cooldown = investiture_law_0 }
		}
	}

	on_fail = {
		ROOT = { 
			piety = -250
			prestige = -125
			cancel_plot = plot_gain_title
		}
		FROM = { 
			piety = 100
			prestige = 50
		}
	}
	
	on_invalidation = {
		ROOT = {
			cancel_plot = plot_gain_title
		}
	}
	

	on_reverse_demand = {
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -500
			prestige = -250
			prisoner = FROM
			cancel_plot = plot_gain_title
		}
		
		FROM = { 
			piety = 500
			prestige = 250
			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
					}
				}
			}
		}
	}

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

buddhist_holy_war = {
	name = CB_NAME_RELIGIOUS_MINOR
	war_name = WAR_NAME_RELIGIOUS
	sprite = 9
	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
	
	sort_priority = 800
	
	can_use = {
		ROOT = {
			religion = buddhist
			NOT = { same_realm = FROM }
			OR = {
				NOT = { religion_group = FROM }
				FROM = { religion = taoist }
				AND = {
					religion_group = FROM
					FROM = {
						has_character_modifier = known_satanist
					}
					NOT = {
						has_character_modifier = known_satanist
					}
				}
			}
			top_liege = {
				religion_group = ROOT
				OR = {
					NOT = { religion = FROM }
					FROM = { religion = taoist }
					AND = {
						religion_group = FROM
						FROM = {
							has_character_modifier = known_satanist
						}
						NOT = {
							has_character_modifier = known_satanist
						}
						ROOT = {
							NOT = {
								has_character_modifier = known_satanist
							}
						}
					}
				}
			}
			mercenary = no
			is_nomadic = no
		}
	}
	
	can_use_title = {
		tier = count
		
		# The attacker needs a border or be at the most two sea zones away
		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 = {
		ROOT = {
			religion = buddhist
			NOT = { same_realm = FROM }
			OR = {
				NOT = { religion_group = FROM }
				FROM = { religion = taoist }
				AND = {
					religion_group = FROM
					FROM = {
						has_character_modifier = known_satanist
					}
					NOT = {
						has_character_modifier = known_satanist
					}
				}
			}
			top_liege = {
				OR = {
					NOT = { religion = FROM }
					FROM = { religion = taoist }
					AND = {
						religion_group = FROM
						FROM = {
							has_character_modifier = known_satanist
						}
						NOT = {
							has_character_modifier = known_satanist
						}
						ROOT = {
							NOT = {
								has_character_modifier = known_satanist
							}
						}
					}
				}
			}
		}
	}

	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 = {
		any_attacker = {
			hidden_tooltip = {
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
		}
		
		ROOT = {
			hidden_tooltip = {
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
		}
	}

	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 = holy_war }
			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 = holy_war }
			}
		}
		ROOT = {
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

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

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			participation_scaled_prestige = 100
			hidden_tooltip = {
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
			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 = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_nomadic = no
				NOT = {
					is_tribal = yes
				}
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}
				}
			}
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_tribal = yes
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
							holding_type = tribal
						}
					}
				}
			}
		}
	}
}

# 51: The Indian religions can subjugate each other
indian_subjugation = {
	name = CB_NAME_PAGAN_SUBJUGATION
	war_name = WAR_NAME_PAGAN_SUBJUGATION
	sprite = 8
	truce_days = 365
	hostile_against_others = yes
	is_permanent = yes
	can_ask_to_join_war = yes
	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
	allowed_to_target_tributaries = no
	
	sort_priority = 1000
	
	can_use_gui = {
		ROOT = {
			piety = 500
		}
	}
	
	on_add = {
		ROOT = { piety = -500 }
		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 = {
		ROOT = {
			has_dharmic_religion_trigger = yes
			NOT = { same_realm = FROM }
			NOT = { is_liege_or_above = FROM }
			has_horde_culture = no # Should use tribal invasion instead
			is_nomadic = no
			mercenary = no
		}
		FROM = {
			NOT = { is_offmap_governor = yes }
			has_dharmic_religion_trigger = yes
			culture_group = ROOT
			NOT = {
				any_war = {
					defender = { character = PREVPREV }
					using_cb = indian_subjugation
				}
			}
		}
	}
	
	can_use_title = {
		ROOT = {
			OR = {
				NOT = { has_character_modifier = launched_subjugation }
				AND = {
					has_ambition = obj_become_king
					capital_scope = {
						kingdom = {
							title = PREVPREVPREV
						}
					}
				}
			}
		}

		FROM = {
			any_realm_province = {
				de_jure_liege_or_above = PREVPREV
			}
		}
		
		#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
		#		}
		#	}
		#}
	}
	
	is_valid_title = {
		FROM = {
			any_realm_province = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}
	
	on_add_title = {
		ROOT = {
			if = {
				limit = {
					OR = {
						NOT = { has_ambition = obj_become_king }
						capital_scope = {
							kingdom = {
								NOT = { title = PREVPREVPREV }
							}
						}
					}
				}
				add_character_modifier = { 
					name = launched_subjugation
					years = 100
					hidden = yes
				}
			}
		}
	}

	on_success = {
		ROOT = {
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		FROM = {
			prestige = -200
		}
	}
	
	on_success_title = {
		# Usurp the target kingdom itself, if the enemy holds it
		if = {
			limit = {
				holder_scope = {
					character = FROM
				}
				NOT = { ROOT = { tier = EMPEROR } }
			}
			ROOT = { usurp_title = { target = PREV type = invasion } }
		}
		
		if = {
			# Take the enemy's duchies within the target kingdom, if we are the same or lower tier
			limit = {
				ROOT = {
					NOT = { higher_tier_than = FROM }
				}
			}
			FROM = {
				any_demesne_title = {
					limit = {
						tier = DUKE
						OR = {
							title = PREVPREV
							de_jure_liege_or_above = PREVPREV
						}
					}
					ROOT = { usurp_title = { target = PREV type = invasion } }
				}
			}
		}
		
		# Have I become the vassal of the loser due to taking a duchy or kingdom?
		if = {
			limit = {
				ROOT = {
					is_liege_or_above = FROM	
				}
			}
			ROOT = {
				set_defacto_liege = ROOT
			}
		}
		
		# Vassalize the rest
		custom_tooltip = {
			text = pagan_subjugation_tip
			hidden_tooltip = {
				ROOT = {
					subjugate_or_take_under_title = { # If the target only has territory within the kingdom, he is simply vassalized
						title = PREV
						enemy = FROM
					}
				}
			}
		}
	}

	on_fail = {
		FROM = {
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		ROOT = {
			prestige = -100
		}
	}
	
	on_invalidation = {
		ROOT = {
			remove_character_modifier = launched_subjugation
			piety = 500
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
		}
		FROM = {
			participation_scaled_prestige = 200
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
	}

	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.9 # Prefer using CB's without on_add costs
		}
		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 = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				NOT = {
					is_tribal = yes
				}
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}
				}
			}
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_tribal = yes
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
							holding_type = tribal
						}
					}
				}
			}
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_nomadic = no
				NOT = {
					is_tribal = yes
				}
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}
				}
			}
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_tribal = yes
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
							holding_type = tribal
						}
					}
				}
			}
		}
	}
}

# 52: This can be used for various special cases; the great conquerors
manifest_destiny_invasion = {
	name = CB_NAME_INVASION
	war_name = WAR_NAME_INVASION
	sprite = 8
	truce_days = 3650
	hostile_against_others = yes
	is_permanent = yes
	is_holy_war = yes
	can_ask_to_join_war = yes
	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
	
	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		ROOT = {
			OR = {
				ai = yes
				multiplayer = no
				NOT = {
					has_game_rule = {
						name = multiplayer_invasions
						value = off
					}
				}
			}
			OR = {
				AND = {
					dynasty = 613 # Seljuks
					NOT = { year = 1200 }
				}
				dynasty = 800 # Timurids
			}
		}
		
		ROOT = {
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}
	}
	
	can_use_title = {
		empire = {
			OR = {
				title = e_persia
				title = e_byzantium
				title = e_arabia
			}
		}
		
		# Target must have at least 3 counties within the target kingdom
		FROM = {
			num_of_realm_counties = {
				value = 3
				title = PREV
			}
		}
		
		# The attacker needs at least one county in the target kingdom, or a border
		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 = {
							owner = {
								OR = {
									character = ROOT
									is_liege_or_above = ROOT
								}
							}
						}
					}
				}
			}
		}
	}
	
	is_valid = {
		always = yes
	}
	
	is_valid_title = {
		FROM = {
			any_realm_title = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}

	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
			
			hidden_tooltip = {
				if = {
					limit = {
						has_nickname = no
					}
					random_list = {
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_magnificent }
						10 = { give_nickname = nick_the_glorious }
						10 = { give_nickname = nick_the_conqueror }
					}
				}
			}
			participation_scaled_prestige = 200
			
			if = {
				limit = {
					uses_decadence = yes
					FROM = { NOT = { uses_decadence = yes } }
				}
				participation_scaled_decadence = -20
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_prestige = 200 
				if = {
					limit = {
						uses_decadence = yes
						FROM = { NOT = { uses_decadence = yes } }
					}
					participation_scaled_decadence = -20
				}
			}
		}
		FROM = {
			prestige = -200
		}
	}
	
	on_success_title = {
		custom_tooltip = { 
			text = other_invasion_succ_tip
			hidden_tooltip = {
				ROOT = {
					vassalize_or_take_under_title_destroy_duchies = {
						title = PREV
						enemy = FROM
						is_crusade = yes # Even if the kingdom title holder is not participating in the war, gain holdings occupied by all participants
						type = invasion
					}
				}
			}
		}

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

	on_fail = {
		FROM = {
			prestige = 100
		}
		FROM = {
			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
						}
				}
				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 }
		}
		ROOT = {
			prestige = -200
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			if = {
				limit = {
					NOT = {
						any_liege = {
							dynasty = PREV
						}
					}
				}
				decadence = 10
			}
		}
		FROM = {
			prestige = 200
			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
						}
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
			participation_scaled_prestige = 200
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
	}

	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
	}
	
	ai_will_do = { 
		factor = 10
		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
				}
			}
		}
	}
}

# 53: Decadence Usurption
cb_decadence_usurption = {
	name = CB_NAME_DECADENCE_USURPTION
	war_name = WAR_NAME_DECADENCE_USURPTION
	sprite = 11
	truce_days = 3650
	is_permanent = yes
	allowed_to_target_tributaries = no
	
	can_use = {
		ROOT = { 
			religion_group = muslim
		}
	
		FROM = {
			religion_group = muslim
			trait = decadent
			dynasty = ROOT
			
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					ROOT = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
		
		ROOT = {
			mercenary = no
		}
	}
	
	is_valid = {
		FROM = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					ROOT = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}

	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 = {
			participation_scaled_piety = 500
			participation_scaled_prestige = 250
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_piety = 500
				participation_scaled_prestige = 250
			}
		}
		FROM = {
			prisoner = ROOT
		}
	}
	
	on_success_title = {
		usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
	}

	on_fail = {
		ROOT = { 
			piety = -250
			prestige = -125
		}
		FROM = { 
			piety = 100
			prestige = 50
		}
	}

	on_reverse_demand = {
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
			piety = -500
			prestige = -250
		}
		
		FROM = { 
			piety = 500
			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
	}
}

# 54
rivalry_cb = {
	name = CB_NAME_RIVALRY
	war_name = WAR_NAME_RIVALRY
	sprite = 7
	truce_days = 3650
	is_permanent = yes
	can_ask_to_join_war = no
	allowed_to_target_tributaries = no
	
	defender_unoccupied_warscore = yes
	
	sort_priority = 900
	
	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		ROOT = {
			is_rival = FROM
		}
		FROM = {
			NOT = {
				has_opinion_modifier = {
					who = ROOT
					modifier = opinion_humiliated_rival
				}
			}
		}
	}
	
	is_valid = {
		ROOT = {
			is_rival = 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 
			}
		}
	}
	
	on_success = {
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_prestige = 250
			}
		}
		
		ROOT = {
			participation_scaled_prestige = 250
			if = {
				limit = {
					ai = no
					FROM = {
						any_sibling = {
							character = ROOT
						}
					}
				}
				set_character_flag = achievement_sibling_rival_war
			}
			remove_opinion = {
				who = FROM
				modifier = opinion_humiliated_rival
			}
		}
		
		FROM = { 
			prestige = -250
			
			imprison = ROOT
			opinion = {
				modifier = opinion_humiliated_rival
				who = ROOT
				years = 150
			}
		}
	}

	on_fail = {
		ROOT = { 
			prestige = -125
		}
		FROM = { 
			prestige = 50
		}
	}

	on_reverse_demand = {
		FROM = {
			participation_scaled_prestige = 250
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_prestige = 250
			}
		}
		ROOT = {
			prestige = -250
			imprison = FROM
			opinion = {
				modifier = opinion_humiliated_rival
				who = FROM
				years = 150
			}
		}
		
		FROM = { 
			remove_opinion = {
				who = ROOT
				modifier = opinion_humiliated_rival
			}
			if = {
				limit = {
					ai = no
					ROOT = {
						any_sibling = {
							character = PREVPREV
						}
					}
				}
				set_character_flag = achievement_sibling_rival_war
			}
		}
	}

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

# 55
tributary_cb = {
	name = CB_NAME_TRIBUTARY
	war_name = WAR_NAME_TRIBUTARY
	sprite = 36
	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
	}

	can_use = {
		has_dlc = "Horse Lords"

		ROOT = {
			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
			}
			independent = yes
		}
		FROM = {
			higher_tier_than = BARON
			NOT = {
				is_offmap_governor = yes
			}
			NAND = {
				holy_order = yes
				religion = ROOT
			}
			independent = yes
		}
		
		# 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
								}
							}
						}
						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_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
						}
					}
				}
			}
		}

		ROOT = {
			show_scope_change = no
			prestige = 200
			make_tributary = { who = FROM percentage = 0.4 }
		}
		
		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
		
		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 = { #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.1 # Low prio CB
		}
		modifier = {
			factor = 0.1 # Nomads are the only ones who like using the CB
			ROOT = {
				is_nomadic = no
			}
		}
		
		modifier = {
			factor = 0.1 # Target has almost no money
			FROM = {
				OR = {
					is_nomadic = yes
					is_tribal = yes
				}
			}
		}

		modifier = {
			factor = 4
			ROOT = {
				trait = greedy
			}
		}
	}
}

# 56
free_tributary_cb = {
	name = CB_NAME_FREE_TRIBUTARY
	war_name = WAR_NAME_FREE_TRIBUTARY
	sprite = 25
	truce_days = 365
	is_permanent = yes
	hostile_against_others = yes
	can_ask_to_join_war = no
	is_independence = yes
	
	sort_priority = 1100

	can_use = {
		ROOT = {
			OR = {
				AND = {
					is_tributary = yes
					OR = {
						AND = {
							has_dlc = "Horse Lords"
							OR = {
								is_tributary = { type = nomadic }
								is_tributary = { type = default }
							}
						}
						AND = {
							has_dlc = "Jade Dragon"
							OR = {
								is_tributary = { type = permanent }
								is_tributary = { type = offmap }
							}
						}
					}
					suzerain = {
						OR = {
							character = FROM
							any_liege = {
								character = FROM
							}
						}
					}
				}
				any_vassal = {
					is_tributary = yes
					OR = {
						AND = {
							has_dlc = "Horse Lords"
							OR = {
								is_tributary = { type = nomadic }
								is_tributary = { type = default }
							}
						}
						AND = {
							has_dlc = "Jade Dragon"
							OR = {
								is_tributary = { type = permanent }
								is_tributary = { type = offmap }
							}
						}
					}
					suzerain = {
						OR = {
							character = FROM
							any_liege = {
								character = FROM
							}
						}
					}
				}
			}
		}
	}

	can_use_gui = {
		check_if_crusader_trigger = yes
		ROOT = { #Cannot use CB if can take decision to stop paying tribute
			conditional_tooltip = {
				trigger = { is_decision_potential = tributary_independence_china }
				NOT = { is_decision_allowed = tributary_independence_china }
			}
			conditional_tooltip = {
				trigger = { is_decision_potential = tributary_independence_normal }
				NOT = { is_decision_allowed = tributary_independence_normal }
			}
		}
	}

	is_valid = {
		ROOT = {
			OR = {
				AND = {
					is_tributary = yes
					suzerain = {
						OR = {
							character = FROM
							any_liege = {
								character = FROM
							}
						}
					}
				}
				any_vassal = {
					is_tributary = yes
					suzerain = {
						OR = {
							character = FROM
							any_liege = {
								character = 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 
			}
		}
	}

	on_success = {
		FROM = {
			show_scope_change = no
			prestige = -100
		}

		ROOT = {
			show_scope_change = no
			prestige = 100
			if = {
				limit = {
					is_tributary = yes
					suzerain = {
						OR = {
							character = FROM
							any_liege = {
								character = FROM
							}
						}
					}
				}
				suzerain = {
					remove_tributary = PREV
				}
			}
			any_vassal = {
				limit = {
					is_tributary = yes
					suzerain = {
						OR = {
							character = FROM
							any_liege = {
								character = FROM
							}
						}
					}
				}
				suzerain = {
					remove_tributary = PREV
				}
			}
		}
	}

	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 = -100
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}

		FROM = {
			show_scope_change = no
			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

		modifier = {
			factor = 1.5
			FROM = {
				trait = proud
			}
		}
		
		modifier = {
			factor = 1.5
			FROM = {
				trait = brave
			}
		}
		
		modifier = {
			factor = 1.5
			FROM = {
				trait = greedy
			}
		}
		
		modifier = {
			factor = 2
			FROM = {
				tier = KING
			}
		}

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

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

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

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

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

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

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

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

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

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

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

		modifier = {
			factor = 5
			ROOT = {
				distance_from_realm = { who = FROM value = 40 }
			}
		}
		
		modifier = {
			factor = 5
			ROOT = {
				distance_from_realm = { who = FROM value = 80 }
			}
		}
		
		modifier = {
			factor = 5
			ROOT = {
				distance_from_realm = { who = FROM value = 120 }
			}
		}
	}

	ai_will_do = { 
		factor = 1
		
		modifier = {
			factor = 1.5
		}
		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 = 4
			ROOT = {
				trait = greedy
			}
		}
		
		modifier = {
			factor = 2
			OR = {
				ROOT = {
					is_nomadic = yes
				}
				FROM = {
					is_nomadic = no
				}
			}
		}
	}
}

# 57
cb_install_khan = {
	name = CB_NAME_INSTALLKHAN
	war_name = WAR_NAME_INSTALLKHAN
	sprite = 12
	truce_days = 3650
	
	is_revolt_cb = no
	can_call_vassals = yes
	attacker_can_call_allies = no
	major_revolt = yes

	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		has_dlc = "Horse Lords"
		
		ROOT = {
			is_nomadic = yes
		}
	}

	is_valid = {
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
			any_demesne_title = {
				tier = count # has some land
			}
		}
		FROM = {
			any_demesne_title = {
				NOT = { lower_tier_than = ROOT }
				temporary = no
			}
		}
	}

	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 = {
		FROM = {
			any_demesne_title = {
				limit = { tier = EMPEROR }
				gain_title = { target = ROOT type = faction_demand }
			}
			death = {
				death_reason = death_execution
				killer = ROOT
			}
		}
		ROOT = {
			participation_scaled_prestige = 250
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 250 }
		}
	}

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

	on_reverse_demand = {
		ROOT = {
			show_scope_change = no
			prestige = -200
			prisoner = FROM
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 50
			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
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	attacker_ai_defeat_worth = {
		factor = 100
	}
	
	on_defender_leader_death = {
		hidden_tooltip = {
			any_attacker = {
				letter_event = {
					id = 251
				}
			}
		}
		
		end_war = invalid
	}

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

# 58
nomad_dispute = {
	name = CB_NAME_DISPUTE
	war_name = CB_NAME_DISPUTE
	sprite = 16
	truce_days = 1095
	hostile_against_others = yes
	is_permanent = yes
	check_all_titles = yes
	can_ask_to_join_war = no
	
	allow_distant = yes

	allowed_to_target_tributaries = no
	
	sort_priority = 700
	
	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		has_dlc = "Horse Lords"

		FROM = {
			is_nomadic = yes
		}
		ROOT = {
			is_nomadic = yes
			same_realm = FROM
			has_feud_with = FROM
		}
		
		ROOT = {
			mercenary = no
		}
	}

	can_use_title = {
		tier = count
		location = {
			NOT = { num_of_settlements = 1 }
		}
		FROM = {
			has_landed_title = PREV
		}
	}

	is_valid_title = {
		location = {
			NOT = { num_of_settlements = 1 }
		}
		FROM = {
			has_landed_title = PREV
		}
	}

	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_title = {
		usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
		
		ROOT = {
			show_scope_change = no
			participation_scaled_prestige = 100
			participation_scaled_piety = 50
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_prestige = 100 
				participation_scaled_piety = 50
			}
		}
	}

	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
			participation_scaled_piety = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_prestige = 100 
				participation_scaled_piety = 50
			}
		}
	}

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

# 59: CB for non-nomads to seize a duchy off a nomad horde
cb_liberate_nomad_duchy = {
	name = CB_NAME_LIBERATE_NOMAD
	war_name = WAR_NAME_LIBERATE_NOMAD
	sprite = 8
	truce_days = 3650
	is_permanent = 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 = no

	infamy_modifier = 0.75
	
	sort_priority = 810
	
	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		FROM = {
			is_nomadic = yes
		}
		ROOT = {
			is_nomadic = no
			NOT = { pacifist = yes }
			NOT = { same_realm = FROM }
			mercenary = no
		}
	}
	
	can_use_title = {
		# 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_direct_de_jure_vassal_title = {
			OR = {
				holder_scope = {
					OR = {
						character = ROOT
						is_liege_or_above = ROOT
					}
				}
				location = {
					any_neighbor_province = {
						has_owner = yes
						owner = {
							OR = {
								character = ROOT
								is_liege_or_above = ROOT
							}
						}
					}
				}
			}
		}
		
		any_de_jure_vassal_title = { # Can only liberate Duchies that are either outside of the Steppe region, or contains a Castle/City/Tribe
			tier = COUNT
			OR = {
				holder_scope = {
					character = FROM
				}
				holder_scope = {
					any_liege = {
						character = FROM
					}
				}
			}
			OR = {
				location = {
					NOT = {
						region = world_steppe
					}
				}
				location = {
					any_province_holding = {
						OR = {
							holding_type = city
							holding_type = castle
							holding_type = tribal
						}
					}
				}
			}
		}
	}

	is_valid = {
		FROM = {
			is_nomadic = yes
		}
		ROOT = {
			is_nomadic = no
			NOT = { pacifist = yes }
			NOT = { same_realm = FROM }
			mercenary = no
		}
	}

	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_prestige = 200
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_prestige = 200
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
			}
		}
		
		FROM = { show_scope_change = no prestige = -200 }
	}

	on_success_title = {
		check_holy_order_donation_effect = yes

		custom_tooltip = {
			text = liberate_nomad_cb_succ_tip
			hidden_tooltip = {
				ROOT = {
					vassalize_or_take_under_title = {
						title = PREV
						enemy = FROM
						anti_nomad = yes # Vassalize non-nomadic rulers
						type = invasion
					}
				}
			}
		}
	}

	on_fail = {
		ROOT = {
			show_scope_change = no
			prestige = -200
		}
	}

	on_reverse_demand = {
		ROOT = {
			show_scope_change = no
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			prestige = -200
		}
		FROM = {
			participation_scaled_prestige = 300
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_prestige = 300
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
			}
		}
	}

	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 = 0
			ROOT = {
				NOT = {
					is_tribal = yes
				}
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}
				}
			}
		}
		modifier = {
			factor = 0
			ROOT = {
				is_tribal = yes
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
							holding_type = tribal
						}
					}
				}
			}
		}
	}
}

# 60: The CB used by "Minor Clan" revolters in Horse Lords
cb_minor_clan_revolt = {
	name = CB_NAME_MINOR_CLAN_REVOLT
	war_name = WAR_NAME_MINOR_CLAN_REVOLT
	sprite = 16
	truce_days = 365
	hostile_against_others = no
	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
	
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no
	
	is_independence = yes
	
	can_use = {
		ROOT = {
			primary_title = { temporary = yes } # Revolter or adventurer trigger
			war = no
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}
	}

	can_use_title = {
		tier = count
	}

	is_valid_title = {
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = 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 
			}
		}
	}

	on_success_title = {
		usurp_title_plus_barony_if_unlanded = { target = ROOT type = revolt }
	
		ROOT = {
			primary_title = {
				set_title_nomad = { title = THIS status = no }
			}
			
			set_government_type = feudal_government
		}
	
		ROOT = {		
			set_government_type = nomadic_government
			set_defacto_liege = FROM
			
			hidden_tooltip = {
				remove_opinion = {
					who = FROM
					modifier = opinion_evil_tyrant
				}
			}
		}
		FROM = {
			prestige = -100
		}
	}

	on_fail_title = {
		FROM = {
			prestige = 10
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor 
					who = ROOT
					months = 1200
				}
			}
		}
		ROOT = {
			clear_wealth = yes
			imprison = FROM
			hidden_tooltip = {
				add_character_modifier = { 
					name = broken_spirit
					days = -1
				}
			}
		}
	}

	on_reverse_demand = {
		FROM = {
			prestige = 20
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor 
					who = ROOT
					months = 1200
				}
				add_character_modifier = { 
					name = crushed_revolt
					days = 3650
				}
			}
		}
		ROOT = {
			clear_wealth = yes
			imprison = FROM 
			hidden_tooltip = {
				add_character_modifier = { 
					name = broken_spirit
					days = -1
				}
			}
		}
	}
	
	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
	}
}

# 61: Nomads can subjugate non-Nomads
nomad_subjugation = {
	name = CB_NAME_PAGAN_SUBJUGATION
	war_name = WAR_NAME_PAGAN_SUBJUGATION
	sprite = 8
	truce_days = 365
	is_permanent = yes
	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no
	defender_unoccupied_warscore = yes
	
	sort_priority = 1100
	
	can_use_gui = {
	#higher_tier_than between ROOT and FROM would be better but its tooltip doesn't say anything about comparing character tiers
		OR = {
			ROOT = {
				higher_tier_than = king
			}
			NOT = {
				FROM = {
					tier = emperor
				}
			}
		}
		FROM = {
			is_nomadic = no
			OR = {
				AND = {
					is_tribal = yes
					ROOT = {
						population = 2500
					}
				}
				ROOT = {
					population = 10000
				}
				has_opinion_modifier = {
					modifier = declared_independence_nomad
					who = FROM
				}
			}
		}
		ROOT = {
			OR = {
				prestige = 500
				has_opinion_modifier = {
					modifier = declared_independence_nomad
					who = FROM
				}
			}
		}
	}
	
	can_use = {
		ROOT = {
			OR = {
				ai = yes
				multiplayer = no
				NOT = {
					has_game_rule = {
						name = multiplayer_invasions
						value = off
					}
				}
			}
			is_nomadic = yes
			NOT = { same_realm = FROM }
			NOT = { is_liege_or_above = FROM }
			mercenary = no
			OR = {
				NOT = { has_character_modifier = launched_subjugation }
				has_opinion_modifier = {
					modifier = declared_independence_nomad
					who = FROM
				}
			}
		}
		
		FROM = {
			in_revolt = no
			is_nomadic = no # Nomads cannot be subjugated
			NOT = { is_offmap_governor = yes }
			NOT = {
				any_war = {
					defender = { character = PREVPREV }
					OR = {
						using_cb = nomad_subjugation
						using_cb = pagan_subjugation
					}
				}
			}
		}
		
		OR = {
			ROOT = {
				ai = no
			}
			FROM = {
				any_realm_title = {
					tier = COUNT
					count = 5
				}
			}
		}
	}
	
	on_add = {
		ROOT = {
			if = {
				limit = {
					NOT = {
						has_opinion_modifier = {
							modifier = declared_independence_nomad
							who = FROM
						}
					}
				}
				prestige = -500
				add_character_modifier = { 
					name = launched_subjugation
					years = 100
					hidden = yes
				}
			}
		}
		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 = {
		hidden_tooltip = {
			ROOT = {
				if = {
					limit = {
						has_nickname = no
					}
					FROM = {
						save_event_target_as = nickname_target
					}
					ROOT = { 
						save_event_target_as = nickname_receiver
					}
					random_list = {
						2 = { give_nickname = nick_the_eastern_wind }
						5 = { give_nickname = nick_the_despoiler }
						5 = { 
							give_nickname = nick_the_bane_of_realmname
							modifier = {
								factor = 0
								event_target:nickname_target = { is_nomadic = yes }
							}
							modifier = {
								factor = 0
								event_target:nickname_target = { culture = ROOT }
							}
						}
						5 = { 
							give_nickname = nick_the_culture_marauder
							modifier = {
								factor = 0
								event_target:nickname_target = { is_nomadic = yes }
							}
							modifier = {
								factor = 0
								event_target:nickname_target = { culture = ROOT }
							}
						}
						100 = { }
					}
				}
			}
		}
		ROOT = {
			piety = 100
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		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 = tribal_subjugation_tip
			}
				
			hidden_tooltip = {
				opinion = {
					modifier = opinion_subjugated 
					who = ROOT
				}
				any_vassal = {
					opinion = {
						modifier = opinion_subjugated 
						who = ROOT
					}
				}
				
				any_demesne_title = {
					limit = {
						higher_tier_than = ROOT
						NOT = { controls_religion = yes }
					}
					ROOT = { grant_title_no_opinion = { target = PREV type = invasion } }
					add_pressed_claim = PREV
				}
				
				any_demesne_title = {
					limit = {
						tier = ROOT
						NOT = { controls_religion = yes }
					}
					ROOT = { grant_title_no_opinion = { target = PREV type = invasion } }
					add_pressed_claim = PREV
				}

				if = {
					limit = {
						NOT = {
							any_demesne_title = {
								controls_religion = yes
							}
						}
					}
					set_defacto_liege = ROOT
					break = yes
				}
				any_vassal = {
					set_defacto_liege = ROOT
				}
				any_demesne_title = {
					limit = {
						NOT = { controls_religion = yes }
					}
					ROOT = {
						grant_title_no_opinion = { target = PREV type = invasion }
					}
				}
			}
		}

		hidden_tooltip = {
			check_if_reconquista_finished_effect = yes
			FROM = {
				landless_pope_effect = yes
			}
		}
	}
	
	on_fail = {
		FROM = {
			piety = 50
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		ROOT = {
			piety = -100
		}
	}
	
	on_invalidation = {
		if = {
			limit = {
				NOT = {
					has_opinion_modifier = {
						modifier = declared_independence_nomad
						who = FROM
					}
				}
			}
			ROOT = {
				remove_character_modifier = launched_subjugation
			}
		}
	}

	on_reverse_demand = {
		ROOT = {
			piety = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
		}
		FROM = {
			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
						}
				}
				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 = 10
		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
			}
		}
	}
}

# 62:
nomad_invasion = {
	name = CB_NAME_INVASION
	war_name = WAR_NAME_INVASION
	sprite = 8
	truce_days = 3650
	hostile_against_others = yes
	is_permanent = yes
	is_holy_war = yes
	can_ask_to_join_war = yes
	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
	
	allowed_to_target_tributaries = no
	
	sort_priority = 1000
	
	can_use_gui = {
		ROOT = {
			prestige = 100
			population = 30000
			population_factor = 0.75 # 75% of max
		}
	}
	
	on_add = {
		ROOT = { prestige = -100 }
		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 = {
		ROOT = {
			is_nomadic = yes
			OR = {
				ai = yes
				multiplayer = no
				NOT = {
					has_game_rule = {
						name = multiplayer_invasions
						value = off
					}
				}
			}
		}
		
		ROOT = {
			NOT = { same_realm = FROM }
		}
		
		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
			}
			ROOT = {
				has_landed_title = e_mongol_empire
			}
			FROM = {
				num_of_realm_counties = {
					value = 5
					title = PREV
				}
			}
		}
	}
	
	is_valid_title = {
		FROM = {
			any_realm_province = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}

	on_success = {
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		FROM = {
			prestige = -200
		}
		hidden_tooltip = {
			ROOT = {
				occupy_minors_of_occupied_settlements = FROM
				gain_all_occupied_titles = { who = FROM type = invasion }
				
				if = {
					limit = {
						has_nickname = no
					}
					FROM = {
						save_event_target_as = nickname_target
					}
					ROOT = { 
						save_event_target_as = nickname_receiver
					}
					random_list = {
						5 = { give_nickname = nick_the_great }
						5 = { give_nickname = nick_the_victorious }
						5 = { give_nickname = nick_the_conqueror }
						5 = { give_nickname = nick_the_brave }
						10 = { give_nickname = nick_the_dragon }
						10 = { give_nickname = nick_the_terrible }
						10 = { give_nickname = nick_the_eastern_wind }
						25 = { give_nickname = nick_the_despoiler }
						25 = { 
							give_nickname = nick_the_culture_marauder
							modifier = {
								factor = 0
								event_target:nickname_target = { is_nomadic = yes }
							}
							modifier = {
								factor = 0
								event_target:nickname_target = { culture = ROOT }
							}
						}
						50 = { 
							give_nickname = nick_the_bane_of_realmname
							modifier = {
								factor = 0
								event_target:nickname_target = { is_nomadic = yes }
							}
							modifier = {
								factor = 0
								event_target:nickname_target = { culture = ROOT }
							}
						}
						50 = { 
							give_nickname = nick_the_slayer_of_culture
							modifier = {
								factor = 0
								event_target:nickname_target = { culture = ROOT }
							}
						}
						50 = { 
							give_nickname = nick_the_scourge_of_god
							modifier = {
								factor = 0
								event_target:nickname_target = { religion = ROOT }
							}
						}
					}
				}
			}
		}
	}
	
	on_success_title = {
		custom_tooltip = {
			text = tribal_invasion_succ_tip
			hidden_tooltip = {
				ROOT = {
					vassalize_or_take_under_title_destroy_duchies = {
						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 = invasion
					}
				}
			}
		}

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

	on_fail = {
		FROM = {
			prestige = 100
			
			hidden_tooltip = {
				if = {
					limit = {
						has_nickname = no
					}
					random_list = {
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_hammer }
						10 = { give_nickname = nick_the_avenger }
						10 = { give_nickname = nick_the_lion }
						10 = { give_nickname = nick_the_bold }
					}
				}
			}
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		ROOT = {
			prestige = -200
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			prestige = 200
			hidden_tooltip = {
				if = {
					limit = {
						has_nickname = no
					}
					random_list = {
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_hammer }
						10 = { give_nickname = nick_the_avenger }
						10 = { give_nickname = nick_the_lion }
						10 = { give_nickname = nick_the_bold }
					}
				}
			}
			
			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
						}
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
			participation_scaled_prestige = 200
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
	}

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

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = { 
		factor = 8
		
		modifier = {
			factor = 0.9 # Prefer using CB's without on_add costs
		}
		
		# Try to guide the Hordes in the right direction
		modifier = {
			factor = 0.1
			ROOT = {
				OR = {
					dynasty = 613 # Seljuks
					dynasty = 800 # Timurids
					primary_title = { title = e_il-khanate }
				}
			}
			OR = {
				empire = {
					NOT = {
						OR = {
							title = e_persia
							title = e_byzantium
							title = e_arabia
						}
					}
				}
				FROM = {
					primary_title = { title = e_golden_horde }
					NOT = { year = 1300 }
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = {
				primary_title = { title = e_golden_horde }
			}
			OR = {
				empire = {
					OR = {
						title = e_persia
						title = e_byzantium
						title = e_arabia
					}
				}
				FROM = {
					primary_title = { title = e_il-khanate }
					NOT = { year = 1300 }
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = {
				culture = hungarian
				primary_title = {
					OR = {
						title = k_hungary
						title = e_carpathia
					}
				}
			}
			empire = {
				OR = {
					title = e_tartaria
					title = e_russia
				}
			}
		}
		modifier = {
			factor = 100
			ROOT = {
				primary_title = { title = e_mongol_empire }
			}
			empire = {
				OR = {
					title = e_tartaria
					title = e_turkestan
					title = e_pontic_steppe
					title = e_idel_ural
					title = e_russia
					title = e_persia
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = {
				primary_title = { title = e_mongol_empire }
			}
			empire = {
				OR = {
					title = e_tibet
					title = e_rajastan
					title = e_bengal
					title = e_deccan
				}
			}
		}
		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
			}
		}
	}
}

# 63: Nomads can grab entire duchies, if they have the Prestige
nomad_expansion = {
	name = CB_NAME_NOMAD_EXPANSION
	war_name = WAR_NAME_NOMAD_EXPANSION
	sprite = 8
	truce_days = 3650
	is_permanent = yes
	is_holy_war = yes
	check_de_jure_tier = DUKE # this scans all de jure 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 = no
	
	sort_priority = 800
	
	can_use_gui = {
		ROOT = {
			prestige = 300
			population_factor = 0.5
		}
		FROM = {
			OR = {
				is_nomadic = yes # Nomads no population requirement
				AND = {
					is_tribal = yes
					ROOT = { population = 5000 } # Tribals need 5k
				}
				ROOT = { population = 15000 } # Everyone else needs 15k
			}
		}
	}
	
	on_add = {
		ROOT = { prestige = -300 }
		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 = {
		ROOT = {
			is_nomadic = yes
			NOT = { same_realm = FROM }
			mercenary = no
		}
	}
	
	can_use_title = {
		# The attacker needs at least one county in the target duchy, or a border
		any_direct_de_jure_vassal_title = {
			OR = {
				holder_scope = {
					OR = {
						character = ROOT
						is_liege_or_above = ROOT
					}
				}
				location = {
					any_neighbor_province = {
						has_owner = yes
						owner = {
							OR = {
								character = ROOT
								is_liege_or_above = ROOT
							}
						}
					}
				}
			}
		}
	}

	is_valid = {
		ROOT = {
			NOR = {
				pays_tribute_to = FROM
				any_liege = {
					OR = {
						pays_tribute_to = FROM
						FROM = {
							pays_tribute_to = PREV
						}
					}
				}
			}
			is_nomadic = yes
			NOT = { same_realm = FROM }
		}
	}
	
	on_success = {
		ROOT = {
			if = {
				limit = {
					has_nickname = no
				}
				FROM = {
						save_event_target_as = nickname_target
					}
				ROOT = { 
					save_event_target_as = nickname_receiver
				}
				random_list = {
					5 = { give_nickname = nick_the_eastern_wind }
					5 = { give_nickname = nick_the_despoiler }
					5 = {
						give_nickname = nick_the_culture_marauder
						modifier = {
							factor = 0
							event_target:nickname_target = { is_nomadic = yes }
						}
						modifier = {
							factor = 0
							event_target:nickname_target = { culture = ROOT }
						}
					}
					5 = { 
						give_nickname = nick_the_bane_of_realmname
						modifier = {
							factor = 0
							event_target:nickname_target = { is_nomadic = yes }
						}
						modifier = {
							factor = 0
							event_target:nickname_target = { culture = ROOT }
						}
					}
					5 = { 
						give_nickname = nick_the_slayer_of_culture
						modifier = {
							factor = 0
							event_target:nickname_target = { culture = ROOT }
						}
					}
					5 = { 
						give_nickname = nick_the_scourge_of_god
						modifier = {
							factor = 0
							event_target:nickname_target = { religion = ROOT }
						}
					}
					100 = { }
				}
			}
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_prestige = 200
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
			}
		}
		FROM = { prestige = -200 }
	}

	on_success_title = {
		custom_tooltip = {
			text = pagan_subjugation_tip
			hidden_tooltip = {
				ROOT = {
					subjugate_or_take_under_title = {
						title = PREV
						enemy = FROM
					}
				}
			}
		}

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

	on_fail = {
		ROOT = { 
			prestige = -200
		}
	}

	on_reverse_demand = {
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			prestige = -200
		}
		FROM = {
			participation_scaled_prestige = 300
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_prestige = 300
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
			}
		}
	}

	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 = 2
		
		# Try to guide the Hordes in the right direction
		modifier = {
			factor = 0.1
			ROOT = {
				OR = {
					dynasty = 613 # Seljuks
					dynasty = 800 # Timurids
					primary_title = { title = e_il-khanate }
				}
			}
			OR = {
				empire = {
					NOT = {
						OR = {
							title = e_persia
							title = e_byzantium
							title = e_arabia
						}
					}
				}
				FROM = {
					primary_title = { title = e_golden_horde }
					NOT = { year = 1300 }
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = {
				primary_title = { title = e_golden_horde }
			}
			OR = {
				empire = {
					OR = {
						title = e_persia
						title = e_byzantium
						title = e_arabia
					}
				}
				FROM = {
					primary_title = { title = e_il-khanate }
					NOT = { year = 1300 }
				}
			}
		}
		modifier = {
			factor = 2.0
			OR = {
				culture = persian
				culture = baloch
				culture = afghan
				culture = turkish
			}
			empire = {
				OR = {
					title = e_persia
					title = e_rajastan
				}
			}
		}
		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
			}
		}
	}
}

#64
increase_council_power_war = { # Started from faction
	name = CB_NAME_INCREASE_COUNCIL_POWER
	war_name = WAR_NAME_INCREASE_COUNCIL_POWER
	sprite = 6
	truce_days = 3650
	can_call_allies = no
	can_ask_to_join_war = no
	major_revolt = yes
	
	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		ROOT = {
			vassal_of = FROM
		}
	}
	
	can_use_title = {
		higher_tier_than = BARON
		OR = {
			has_law = law_voting_power_0
			has_law = banish_voting_power_0
			has_law = execution_voting_power_0
			has_law = revoke_title_voting_power_0
			has_law = grant_title_voting_power_0
			has_law = imprison_voting_power_0
			has_law = war_voting_power_0
		}
		is_primary_holder_title = yes
	}
	
	is_valid = {
		FROM = {
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
		}
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}

	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 = {
			opinion = {
				modifier = opinion_increased_council_power
				who = FROM
				months = 12
			}
			participation_scaled_prestige = 100
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_success_title = {
		increase_council_power_effect = yes
		increase_vassal_freedom_effect = yes
	}

	on_fail = {
		ROOT = {
			prestige = -100
		}
		
		FROM = {
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		
		hidden_tooltip = {
			any_attacker = {
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_council_power_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			prisoner = FROM
		}
		
		FROM = {
			participation_scaled_prestige = 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
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		
		hidden_tooltip = {
			any_attacker = {
				limit = { liege = { character = FROM } }
				prisoner = FROM
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_council_power_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

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

# 55
nomad_humiliate_cb = {
	name = CB_NAME_HUMILIATE
	war_name = WAR_NAME_HUMILIATE
	sprite = 1
	truce_days = 3650
	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 = 815
	
	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		has_dlc = "Horse Lords"
		
		FROM = {
			is_nomadic = yes
		}

		ROOT = {
			is_nomadic = yes
			mercenary = no
			OR = {
				AND = {
					same_realm = FROM
					has_feud_with = FROM
				}
				FROM = {
					is_nomadic = yes
					independent = yes
				}
			}
		}
		
		# 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
								}
							}
						}
						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
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}

	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 = 300
			steal_population_scaled = {
				target = FROM
				percentage = 0.25
			}
		}
		FROM = {
			prestige = -300
		}
	}

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

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

		FROM = {
			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 = {
			factor = 0.1 # Low prio CB
		}
		
		modifier = {
			factor = 0.1 # Target has almost no money
			FROM = {
				OR = {
					is_nomadic = yes
					is_tribal = yes
				}
			}
		}

		modifier = {
			factor = 4
			ROOT = {
				trait = greedy
			}
		}
	}
}

#64
powerful_vassal_takeover_war = { # Started from faction
	name = CB_NAME_POWERFUL_VASSAL_TAKEOVER
	war_name = WAR_NAME_POWERFUL_VASSAL_TAKEOVER
	sprite = 6
	truce_days = 3650
	can_call_allies = no
	can_ask_to_join_war = no
	major_revolt = yes
	
	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		ROOT = {
			vassal_of = FROM
		}
	}
	
	can_use_title = {
		higher_tier_than = BARON
		is_primary_holder_title = yes
	}
	
	is_valid = {
		FROM = {
			in_revolt = no
			has_character_flag = powerful_vassal_takeover_target
		}
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}

	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 = {
		any_attacker = {
			participation_scaled_prestige = 100
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_success_title = { #This is the target title of the faction
		increase_council_power_effect = yes
		increase_vassal_freedom_effect = yes
		increase_vassal_freedom_effect = yes
		if = {
			limit = { has_law = succession_voting_power_0 }
			add_law_w_cooldown = succession_voting_power_1
		}
		if = { 
			limit = { #Should not be changed from Imperial Elective
				NOT = {	has_law = succ_byzantine_elective }
			}
			custom_tooltip = {
				text = overthrow_text
				hidden_tooltip = {
					if = {
						limit = { 
							OR = { 
								title = e_hre
								has_title_flag = alternate_hre
							}
							NOR = { has_law = succ_feudal_elective has_law = succ_hre_elective }
							has_law = law_voting_power_1
							has_law = banish_voting_power_1
							has_law = execution_voting_power_1
							has_law = revoke_title_voting_power_1
							has_law = grant_title_voting_power_1
							has_law = imprison_voting_power_1
							has_law = war_voting_power_1						
						}
						add_law_w_cooldown = succ_hre_elective
					}
					if = {
						limit = { 
							NOR = { 
								title = e_hre 
								has_title_flag = alternate_hre
							}
							NOR = { has_law = succ_feudal_elective has_law = succ_hre_elective }
							has_law = law_voting_power_1
							has_law = banish_voting_power_1
							has_law = execution_voting_power_1
							has_law = revoke_title_voting_power_1
							has_law = grant_title_voting_power_1
							has_law = imprison_voting_power_1
							has_law = war_voting_power_1						
						}
						add_law_w_cooldown = succ_feudal_elective
					}
					if = {
						limit = {
							NOT = { has_law = succ_gavelkind }
							NOR = { has_law = succ_feudal_elective has_law = succ_hre_elective }
							NAND = {
								has_law = succ_feudal_elective
								has_law = law_voting_power_1
								has_law = banish_voting_power_1
								has_law = execution_voting_power_1
								has_law = revoke_title_voting_power_1
								has_law = grant_title_voting_power_1
								has_law = imprison_voting_power_1
								has_law = war_voting_power_1						
							}
						}
						add_law_w_cooldown = succ_gavelkind
					}
				}
			}
		}
		holder_scope = {
			clr_character_flag = powerful_vassal_takeover_target
			most_popular_vassal = {
				limit = {
					higher_tier_than = BARON
					is_feudal = yes
					prisoner = no
					NOR = {
						is_inaccessible_trigger = yes
						trait = incapable
						trait = eunuch
					}
					age = 16
					#can_hold_title = PREVPREV
					OR = {
						is_female = no
						AND = {
							is_female = yes
							ROOT = { 
								OR = {
									has_law = true_cognatic_succession
									has_law = enatic_cognatic_succession
									has_law = enatic_succession
								}
							}
						}
					}
				}
				usurp_title = {
					target = PREVPREV
					type = faction_demand
				}
				save_event_target_as = new_title_holder
				hidden_tooltip = {
					any_attacker = {
						limit = { NOT = { character = PREV } }
						set_character_flag = helped_win_war
					}
				}
				character_event = { id = 8226 days = 1 }
			}
		}

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

	on_fail = {
		ROOT = {
			prestige = -100
		}
		
		FROM = {
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		
		hidden_tooltip = {
			any_attacker = {
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_powerful_vassal_takeover_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		FROM = { 
			hidden_tooltip = { 
				disband_event_forces = faction_loyalists
				clr_character_flag = powerful_vassal_takeover_target
			}
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			prisoner = FROM
		}

		
		FROM = {
			participation_scaled_prestige = 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
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		
		hidden_tooltip = {
			any_attacker = {
				limit = { liege = { character = FROM } }
				prisoner = FROM
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_powerful_vassal_takeover_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		FROM = { 
			hidden_tooltip = { 
				disband_event_forces = faction_loyalists
				clr_character_flag = powerful_vassal_takeover_target
			}
		}
	}

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

### Reconquista Wars ###
# Leon & Asturia #
reconquista_leon = {
	name = CB_NAME_RECONQUISTA_LEON
	war_name = WAR_NAME_RECONQUISTA_LEON
	diplo_view_region = custom_leon
	sprite = 9
	truce_days = 3650
	is_permanent = yes
	is_holy_war = yes
	battle_warscore_mult = 0.5
	#max_defender_occupation_score = 85
	allow_whitepeace = no

	allowed_to_target_tributaries = yes
	can_ask_to_join_war = yes

	sort_priority = 10000

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

	can_use = {
		ROOT = {
			has_character_flag = currently_reconquering_iberia
			not_reconquista_check_trigger = yes

			any_realm_province = {
				OR = {
					AND = {
						any_neighbor_province = {
							region = custom_leon
						}
					}
					AND = {
						NOT = {
							any_neighbor_province = {
								region = custom_leon
							}
						}
						NOT = {
							any_neighbor_province = {
								region = world_europe_west_iberia
								owner = {
									OR = {
										AND = {
											independent = yes
											NOT = {
												religion_group = christian
											}
										}
										top_liege = {
											NOT = {
												religion_group = christian
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								region = custom_leon
							}
						}
					}
					AND = {
						NOR = {
							any_neighbor_province = {
								region = custom_leon
							}
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_leon
								}
							}
						}
						NOT = {
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = world_europe_west_iberia
									owner = {
										OR = {
											AND = {
												independent = yes
												NOT = {
													religion_group = christian
												}
											}
											top_liege = {
												NOT = {
													religion_group = christian
												}
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_leon
								}
							}
						}
					}
				}
			}

			conditional_tooltip = {
				trigger = {
					ai = yes
				}

				NOT = {
					has_character_modifier = reconquista_preparation_duration
				}
			}
		}

		FROM = {
			any_realm_province = {
				region = custom_leon
			}
			NOT = {
				religion_group = ROOT
			}
		}
	}

	is_valid = {
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = reconquista_valid_TT

				has_character_flag = currently_reconquering_iberia
			}
		}
		custom_tooltip = {
			text = reconquista_valid_2_TT

			FROM = {
				NOT = {
					religion_group = ROOT
				}
			}
		}
	}

	on_add = {
		custom_tooltip = { text = add_allies_reconquista_TT }
		ROOT = {
			character_event = { id = HF.49210 }
		}
	}

	on_success = {

		d_leon = {
			save_event_target_as = first_duchy_title
		}

		d_asturias = {
			save_event_target_as = second_duchy_title
		}
		if = {
			limit = {
				k_asturias = {
					is_titular = yes
				}
			}
			k_leon = {
				save_event_target_as = reconquista_kingdom_title
			}
		}
		else = {
			k_asturias = {
				save_event_target_as = reconquista_kingdom_title
			}
		}
		

		reconquista_win_effect = yes
	}

	on_reverse_demand = {
		reconquista_lose_effect = yes
	}

	on_invalidation = {
		ROOT = {
			character_event = { id = HF.49200 }
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

# Castille & Toledo #
reconquista_castille = {
	name = CB_NAME_RECONQUISTA_CASTILLE
	war_name = WAR_NAME_RECONQUISTA_CASTILLE
	diplo_view_region = custom_castille
	sprite = 9
	truce_days = 3650
	is_permanent = yes
	is_holy_war = yes
	battle_warscore_mult = 0.5
	#max_defender_occupation_score = 85
	allow_whitepeace = no

	allowed_to_target_tributaries = yes
	can_ask_to_join_war = yes

	sort_priority = 10000

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

	can_use = {
		ROOT = {
			has_character_flag = currently_reconquering_iberia
			not_reconquista_check_trigger = yes
			
			any_realm_province = {
				OR = {
					AND = {
						any_neighbor_province = {
							region = custom_castille
						}
					}
					AND = {
						NOT = {
							any_neighbor_province = {
								region = custom_castille
							}
						}
						NOT = {
							any_neighbor_province = {
								region = world_europe_west_iberia
								owner = {
									OR = {
										AND = {
											independent = yes
											NOT = {
												religion_group = christian
											}
										}
										top_liege = {
											NOT = {
												religion_group = christian
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								region = custom_castille
							}
						}
					}
					AND = {
						NOR = {
							any_neighbor_province = {
								region = custom_castille
							}
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_castille
								}
							}
						}
						NOT = {
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = world_europe_west_iberia
									owner = {
										OR = {
											AND = {
												independent = yes
												NOT = {
													religion_group = christian
												}
											}
											top_liege = {
												NOT = {
													religion_group = christian
												}
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_castille
								}
							}
						}
					}
				}
			}

			conditional_tooltip = {
				trigger = {
					ai = yes
				}

				NOT = {
					has_character_modifier = reconquista_preparation_duration
				}
			}
		}

		FROM = {
			any_realm_province = {
				region = custom_castille
			}
			NOT = {
				religion_group = ROOT
			}
		}
	}

	is_valid = {
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = reconquista_valid_TT

				has_character_flag = currently_reconquering_iberia
			}
		}
		custom_tooltip = {
			text = reconquista_valid_2_TT

			FROM = {
				NOT = {
					religion_group = ROOT
				}
			}
		}
	}

	on_add = {
		custom_tooltip = { text = add_allies_reconquista_TT }
		ROOT = {
			character_event = { id = HF.49210 }
		}
	}

	on_success = {

		d_castilla = {
			save_event_target_as = first_duchy_title
		}

		d_toledo = {
			save_event_target_as = second_duchy_title
		}
		k_castille = {
			save_event_target_as = reconquista_kingdom_title
		}

		reconquista_win_effect = yes
	}

	on_reverse_demand = {
		reconquista_lose_effect = yes
	}

	on_invalidation = {
		ROOT = {
			character_event = { id = HF.49200 }
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

# Aragon & Barcelona #
reconquista_aragon = {
	name = CB_NAME_RECONQUISTA_ARAGON
	war_name = WAR_NAME_RECONQUISTA_ARAGON
	diplo_view_region = custom_aragon
	sprite = 9
	truce_days = 3650
	is_permanent = yes
	is_holy_war = yes
	battle_warscore_mult = 0.5
	#max_defender_occupation_score = 85
	allow_whitepeace = no

	allowed_to_target_tributaries = yes
	can_ask_to_join_war = yes

	sort_priority = 10000

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

	can_use = {
		ROOT = {
			has_character_flag = currently_reconquering_iberia
			not_reconquista_check_trigger = yes
			
			any_realm_province = {
				OR = {
					AND = {
						any_neighbor_province = {
							region = custom_aragon
						}
					}
					AND = {
						NOT = {
							any_neighbor_province = {
								region = custom_aragon
							}
						}
						NOT = {
							any_neighbor_province = {
								region = world_europe_west_iberia
								owner = {
									OR = {
										AND = {
											independent = yes
											NOT = {
												religion_group = christian
											}
										}
										top_liege = {
											NOT = {
												religion_group = christian
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								region = custom_aragon
							}
						}
					}
					AND = {
						NOR = {
							any_neighbor_province = {
								region = custom_aragon
							}
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_aragon
								}
							}
						}
						NOT = {
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = world_europe_west_iberia
									owner = {
										OR = {
											AND = {
												independent = yes
												NOT = {
													religion_group = christian
												}
											}
											top_liege = {
												NOT = {
													religion_group = christian
												}
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_aragon
								}
							}
						}
					}
				}
			}

			conditional_tooltip = {
				trigger = {
					ai = yes
				}

				NOT = {
					has_character_modifier = reconquista_preparation_duration
				}
			}
		}

		FROM = {
			any_realm_province = {
				region = custom_aragon
			}
			NOT = {
				religion_group = ROOT
			}
		}
	}

	is_valid = {
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = reconquista_valid_TT

				has_character_flag = currently_reconquering_iberia
			}
		}
		custom_tooltip = {
			text = reconquista_valid_2_TT

			FROM = {
				NOT = {
					religion_group = ROOT
				}
			}
		}
	}

	on_add = {
		custom_tooltip = { text = add_allies_reconquista_TT }
		ROOT = {
			character_event = { id = HF.49210 }
		}
	}

	on_success = {

		d_aragon = {
			save_event_target_as = first_duchy_title
		}

		d_barcelona = {
			save_event_target_as = second_duchy_title
		}
		k_aragon = {
			save_event_target_as = reconquista_kingdom_title
		}

		reconquista_win_effect = yes
	}

	on_reverse_demand = {
		reconquista_lose_effect = yes
	}

	on_invalidation = {
		ROOT = {
			character_event = { id = HF.49200 }
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

# Galicia & Porto #
reconquista_galicia = {
	name = CB_NAME_RECONQUISTA_GALICIA
	war_name = WAR_NAME_RECONQUISTA_GALICIA
	diplo_view_region = custom_galicia
	sprite = 9
	truce_days = 3650
	is_permanent = yes
	is_holy_war = yes
	battle_warscore_mult = 0.5
	#max_defender_occupation_score = 85
	allow_whitepeace = no

	allowed_to_target_tributaries = yes
	can_ask_to_join_war = yes

	sort_priority = 10000

	can_use_gui = {
		ROOT = {
			prisoner = no
			is_adult = yes
		}
	}
	can_use = {
		ROOT = {
			has_character_flag = currently_reconquering_iberia
			not_reconquista_check_trigger = yes
			
			any_realm_province = {
				OR = {
					AND = {
						any_neighbor_province = {
							region = custom_galicia
						}
					}
					AND = {
						NOT = {
							any_neighbor_province = {
								region = custom_galicia
							}
						}
						NOT = {
							any_neighbor_province = {
								region = world_europe_west_iberia
								owner = {
									OR = {
										AND = {
											independent = yes
											NOT = {
												religion_group = christian
											}
										}
										top_liege = {
											NOT = {
												religion_group = christian
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								region = custom_galicia
							}
						}
					}
					AND = {
						NOR = {
							any_neighbor_province = {
								region = custom_galicia
							}
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_galicia 
								}
							}
						}
						NOT = {
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = world_europe_west_iberia
									owner = {
										OR = {
											AND = {
												independent = yes
												NOT = {
													religion_group = christian
												}
											}
											top_liege = {
												NOT = {
													religion_group = christian
												}
											}
										}
									}
								}
							}
						}

						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_galicia
								}
							}
						}
					}
				}
			}

			conditional_tooltip = {
				trigger = {
					ai = yes
				}

				NOT = {
					has_character_modifier = reconquista_preparation_duration
				}
			}
		}

		FROM = {
			any_realm_province = {
				region = custom_galicia
			}
			NOT = {
				religion_group = ROOT
			}
		}
	}

	is_valid = {
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = reconquista_valid_TT

				has_character_flag = currently_reconquering_iberia
			}
		}
		custom_tooltip = {
			text = reconquista_valid_2_TT

			FROM = {
				NOT = {
					religion_group = ROOT
				}
			}
		}
	}

	on_add = {
		custom_tooltip = { text = add_allies_reconquista_TT }
		ROOT = {
			character_event = { id = HF.49210 }
		}
	}

	on_success = {

		d_galicia = {
			save_event_target_as = first_duchy_title
		}

		d_porto = {
			save_event_target_as = second_duchy_title
		}
		if = {
			limit = {
				k_spanish_galicia = {
					is_titular = no
				}
			}
			k_spanish_galicia = {
				save_event_target_as = reconquista_kingdom_title
			}
		}

		reconquista_win_effect = yes
	}

	on_reverse_demand = {
		reconquista_lose_effect = yes
	}

	on_invalidation = {
		ROOT = {
			character_event = { id = HF.49200 }
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

# Beja & Algarve #
reconquista_beja = {
	name = CB_NAME_RECONQUISTA_BEJA
	war_name = WAR_NAME_RECONQUISTA_BEJA
	diplo_view_region = custom_beja
	sprite = 9
	truce_days = 3650
	is_permanent = yes
	is_holy_war = yes
	battle_warscore_mult = 0.5
	#max_defender_occupation_score = 85
	allow_whitepeace = no

	allowed_to_target_tributaries = yes
	can_ask_to_join_war = yes

	sort_priority = 10000

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

	can_use = {
		ROOT = {
			has_character_flag = currently_reconquering_iberia
			not_reconquista_check_trigger = yes
			
			any_realm_province = {
				OR = {
					AND = {
						any_neighbor_province = {
							region = custom_beja
						}
					}
					AND = {
						NOT = {
							any_neighbor_province = {
								region = custom_beja
							}
						}
						NOT = {
							any_neighbor_province = {
								region = world_europe_west_iberia
								owner = {
									OR = {
										AND = {
											independent = yes
											NOT = {
												religion_group = christian
											}
										}
										top_liege = {
											NOT = {
												religion_group = christian
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								region = custom_beja
							}
						}
					}
					AND = {
						NOR = {
							any_neighbor_province = {
								region = custom_beja
							}
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_beja 
								}
							}
						}
						NOT = {
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = world_europe_west_iberia
									owner = {
										OR = {
											AND = {
												independent = yes
												NOT = {
													religion_group = christian
												}
											}
											top_liege = {
												NOT = {
													religion_group = christian
												}
											}
										}
									}
								}
							}
						}

						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_beja
								}
							}
						}
					}
				}
			}

			conditional_tooltip = {
				trigger = {
					ai = yes
				}

				NOT = {
					has_character_modifier = reconquista_preparation_duration
				}
			}
		}

		FROM = {
			any_realm_province = {
				region = custom_beja
			}
			NOT = {
				religion_group = ROOT
			}
		}
	}

	is_valid = {
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = reconquista_valid_TT

				has_character_flag = currently_reconquering_iberia
			}
		}
		custom_tooltip = {
			text = reconquista_valid_2_TT

			FROM = {
				NOT = {
					religion_group = ROOT
				}
			}
		}
	}

	on_add = {
		custom_tooltip = { text = add_allies_reconquista_TT }
		ROOT = {
			character_event = { id = HF.49210 }
		}
	}

	on_success = {

		d_beja = {
			save_event_target_as = first_duchy_title
		}

		d_algarve = {
			save_event_target_as = second_duchy_title
		}

		d_balata = {
			save_event_target_as = third_duchy_title
		}

		reconquista_win_effect = yes
	}

	on_reverse_demand = {
		reconquista_lose_effect = yes
	}

	on_invalidation = {
		ROOT = {
			character_event = { id = HF.49200 }
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

# Granada & Sevilla #
reconquista_granada = {
	name = CB_NAME_RECONQUISTA_GRANADA
	war_name = WAR_NAME_RECONQUISTA_GRANADA
	diplo_view_region = custom_granada
	sprite = 9
	truce_days = 3650
	is_permanent = yes
	is_holy_war = yes
	battle_warscore_mult = 0.5
	#max_defender_occupation_score = 85
	allow_whitepeace = no

	allowed_to_target_tributaries = yes
	can_ask_to_join_war = yes

	sort_priority = 10000

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

	can_use = {
		ROOT = {
			has_character_flag = currently_reconquering_iberia
			not_reconquista_check_trigger = yes	

			any_realm_province = {
				OR = {
					AND = {
						any_neighbor_province = {
							region = custom_granada
						}
					}
					AND = {
						NOT = {
							any_neighbor_province = {
								region = custom_granada
							}
						}
						NOT = {
							any_neighbor_province = {
								region = world_europe_west_iberia
								owner = {
									OR = {
										AND = {
											independent = yes
											NOT = {
												religion_group = christian
											}
										}
										top_liege = {
											NOT = {
												religion_group = christian
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								region = custom_granada
							}
						}
					}
					AND = {
						NOR = {
							any_neighbor_province = {
								region = custom_granada
							}
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_granada 
								}
							}
						}
						NOT = {
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = world_europe_west_iberia
									owner = {
										OR = {
											AND = {
												independent = yes
												NOT = {
													religion_group = christian
												}
											}
											top_liege = {
												NOT = {
													religion_group = christian
												}
											}
										}
									}
								}
							}
						}

						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_granada
								}
							}
						}
					}
				}
			}

			conditional_tooltip = {
				trigger = {
					ai = yes
				}

				NOT = {
					has_character_modifier = reconquista_preparation_duration
				}
			}
		}

		FROM = {
			any_realm_province = {
				region = custom_granada
			}
			NOT = {
				religion_group = ROOT
			}
		}
	}

	is_valid = {
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = reconquista_valid_TT

				has_character_flag = currently_reconquering_iberia
			}
		}
		custom_tooltip = {
			text = reconquista_valid_2_TT

			FROM = {
				NOT = {
					religion_group = ROOT
				}
			}
		}
	}

	on_add = {
		custom_tooltip = { text = add_allies_reconquista_TT }
		ROOT = {
			character_event = { id = HF.49210 }
		}
	}

	on_success = {

		d_granada = {
			save_event_target_as = first_duchy_title
		}

		d_sevilla = {
			save_event_target_as = second_duchy_title
		}

		reconquista_win_effect = yes
	}

	on_reverse_demand = {
		reconquista_lose_effect = yes
	}

	on_invalidation = {
		ROOT = {
			character_event = { id = HF.49200 }
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

# Cordoba & Badajoz #
reconquista_cordoba = {
	name = CB_NAME_RECONQUISTA_CORDOBA
	war_name = WAR_NAME_RECONQUISTA_CORDOBA
	diplo_view_region = custom_cordoba
	sprite = 9
	truce_days = 3650
	is_permanent = yes
	is_holy_war = yes
	battle_warscore_mult = 0.5
	#max_defender_occupation_score = 85
	allow_whitepeace = no

	allowed_to_target_tributaries = yes
	can_ask_to_join_war = yes

	sort_priority = 10000

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

	can_use = {
		ROOT = {
			has_character_flag = currently_reconquering_iberia
			not_reconquista_check_trigger = yes
			
			any_realm_province = {
				OR = {
					AND = {
						any_neighbor_province = {
							region = custom_cordoba
						}
					}
					AND = {
						NOT = {
							any_neighbor_province = {
								region = custom_cordoba
							}
						}
						NOT = {
							any_neighbor_province = {
								region = world_europe_west_iberia
								owner = {
									OR = {
										AND = {
											independent = yes
											NOT = {
												religion_group = christian
											}
										}
										top_liege = {
											NOT = {
												religion_group = christian
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {	
							is_land = yes
							any_neighbor_province = {
								region = custom_cordoba
							}
						}
					}
					AND = {
						NOR = {
							any_neighbor_province = {
								region = custom_cordoba
							}
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_cordoba 
								}
							}
						}
						NOT = {
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = world_europe_west_iberia
									owner = {
										OR = {
											AND = {
												independent = yes
												NOT = {
													religion_group = christian
												}
											}
											top_liege = {
												NOT = {
													religion_group = christian
												}
											}
										}
									}
								}
							}
						}

						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_cordoba
								}
							}
						}
					}
				}
			}

			conditional_tooltip = {
				trigger = {
					ai = yes
				}

				NOT = {
					has_character_modifier = reconquista_preparation_duration
				}
			}
		}

		FROM = {
			any_realm_province = {
				region = custom_cordoba
			}
			NOT = {
				religion_group = ROOT
			}
		}
	}

	is_valid = {
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = reconquista_valid_TT

				has_character_flag = currently_reconquering_iberia
			}
		}
		custom_tooltip = {
			text = reconquista_valid_2_TT

			FROM = {
				NOT = {
					religion_group = ROOT
				}
			}
		}
	}

	on_add = {
		custom_tooltip = { text = add_allies_reconquista_TT }
		ROOT = {
			character_event = { id = HF.49210 }
		}
	}

	on_success = {

		d_cordoba = {
			save_event_target_as = first_duchy_title
		}

		d_badajoz = {
			save_event_target_as = second_duchy_title
		}

		reconquista_win_effect = yes
	}

	on_reverse_demand = {
		reconquista_lose_effect = yes
	}

	on_invalidation = {
		ROOT = {
			character_event = { id = HF.49200 }
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

# Valencia & Murcia #
reconquista_valencia = {
	name = CB_NAME_RECONQUISTA_VALENCIA
	war_name = WAR_NAME_RECONQUISTA_VALENCIA
	diplo_view_region = custom_valencia
	sprite = 9
	truce_days = 3650
	is_permanent = yes
	is_holy_war = yes
	battle_warscore_mult = 0.5
	#max_defender_occupation_score = 85
	allow_whitepeace = no

	allowed_to_target_tributaries = yes
	can_ask_to_join_war = yes

	sort_priority = 10000

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

	can_use = {
		ROOT = {
			has_character_flag = currently_reconquering_iberia
			not_reconquista_check_trigger = yes
			
			any_realm_province = {
				OR = {
					AND = {
						any_neighbor_province = {
							region = custom_valencia
						}
					}
					AND = {
						NOT = {
							any_neighbor_province = {
								region = custom_valencia
							}
						}
						NOT = {
							any_neighbor_province = {
								region = world_europe_west_iberia
								owner = {
									OR = {
										AND = {
											independent = yes
											NOT = {
												religion_group = christian
											}
										}
										top_liege = {
											NOT = {
												religion_group = christian
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								region = custom_valencia
							}
						}
					}
					AND = {
						NOR = {
							any_neighbor_province = {
								region = custom_valencia
							}
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_valencia 
								}
							}
						}
						NOT = {
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = world_europe_west_iberia
									owner = {
										OR = {
											AND = {
												independent = yes
												NOT = {
													religion_group = christian
												}
											}
											top_liege = {
												NOT = {
													religion_group = christian
												}
											}
										}
									}
								}
							}
						}

						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_valencia
								}
							}
						}
					}
				}
			}

			conditional_tooltip = {
				trigger = {
					ai = yes
				}

				NOT = {
					has_character_modifier = reconquista_preparation_duration
				}
			}
		}

		FROM = {
			any_realm_province = {
				region = custom_valencia
			}
			NOT = {
				religion_group = ROOT
			}
		}
	}

	is_valid = {
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = reconquista_valid_TT

				has_character_flag = currently_reconquering_iberia
			}
		}
		custom_tooltip = {
			text = reconquista_valid_2_TT

			FROM = {
				NOT = {
					religion_group = ROOT
				}
			}
		}
	}

	on_add = {
		custom_tooltip = { text = add_allies_reconquista_TT }
		ROOT = {
			character_event = { id = HF.49210 }
		}
	}

	on_success = {

		d_valencia = {
			save_event_target_as = first_duchy_title
		}

		d_murcia = {
			save_event_target_as = second_duchy_title
		}

		reconquista_win_effect = yes
	}

	on_reverse_demand = {
		reconquista_lose_effect = yes
	}

	on_invalidation = {
		ROOT = {
			character_event = { id = HF.49200 }
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

# Navarra #
reconquista_navarra = {
	name = CB_NAME_RECONQUISTA_NAVARRA
	war_name = WAR_NAME_RECONQUISTA_NAVARRA
	diplo_view_region = custom_navarra
	sprite = 9
	truce_days = 3650
	is_permanent = yes
	is_holy_war = yes
	battle_warscore_mult = 0.5
	#max_defender_occupation_score = 85
	allow_whitepeace = no

	allowed_to_target_tributaries = yes
	can_ask_to_join_war = yes

	sort_priority = 10000

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

	can_use = {
		ROOT = {
			has_character_flag = currently_reconquering_iberia
			not_reconquista_check_trigger = yes
			
			any_realm_province = {
				OR = {
					AND = {
						any_neighbor_province = {
							region = custom_navarra
						}
					}
					AND = {
						NOT = {
							any_neighbor_province = {
								region = custom_navarra
							}
						}
						NOT = {
							any_neighbor_province = {
								region = world_europe_west_iberia
								owner = {
									OR = {
										AND = {
											independent = yes
											NOT = {
												religion_group = christian
											}
										}
										top_liege = {
											NOT = {
												religion_group = christian
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								region = custom_navarra
							}
						}
					}
					AND = {
						NOR = {
							any_neighbor_province = {
								region = custom_navarra
							}
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_navarra 
								}
							}
						}
						NOT = {
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = world_europe_west_iberia
									owner = {
										OR = {
											AND = {
												independent = yes
												NOT = {
													religion_group = christian
												}
											}
											top_liege = {
												NOT = {
													religion_group = christian
												}
											}
										}
									}
								}
							}
						}

						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_navarra
								}
							}
						}
					}
				}
			}

			conditional_tooltip = {
				trigger = {
					ai = yes
				}

				NOT = {
					has_character_modifier = reconquista_preparation_duration
				}
			}
		}

		FROM = {
			any_realm_province = {
				region = custom_navarra
			}
			NOT = {
				religion_group = ROOT
			}
		}
	}

	is_valid = {
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = reconquista_valid_TT

				has_character_flag = currently_reconquering_iberia
			}
		}
		custom_tooltip = {
			text = reconquista_valid_2_TT

			FROM = {
				NOT = {
					religion_group = ROOT
				}
			}
		}
	}

	on_add = {
		custom_tooltip = { text = add_allies_reconquista_TT }
		ROOT = {
			character_event = { id = HF.49210 }
		}
	}

	on_success = {

		d_navarra = {
			save_event_target_as = first_duchy_title
		}
		k_navarra = {
			save_event_target_as = reconquista_kingdom_title
		}

		reconquista_win_effect = yes
	}

	on_reverse_demand = {
		reconquista_lose_effect = yes
	}

	on_invalidation = {
		ROOT = {
			character_event = { id = HF.49200 }
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

# Balearic Islands #
reconquista_balearic = {
	name = CB_NAME_RECONQUISTA_BALEARIC
	war_name = WAR_NAME_RECONQUISTA_BALEARIC
	diplo_view_region = custom_balearic
	sprite = 9
	truce_days = 3650
	is_permanent = yes
	is_holy_war = yes
	battle_warscore_mult = 0.5
	#max_defender_occupation_score = 85
	allow_whitepeace = no

	allowed_to_target_tributaries = yes
	can_ask_to_join_war = yes

	sort_priority = 10000

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

	can_use = {
		ROOT = {
			has_character_flag = currently_reconquering_iberia
			not_reconquista_check_trigger = yes
			
			any_realm_province = {
				OR = {
					AND = {
						any_neighbor_province = {
							region = custom_balearic
						}
					}
					AND = {
						NOT = {
							any_neighbor_province = {
								region = custom_balearic
							}
						}
						NOT = {
							any_neighbor_province = {
								region = world_europe_west_iberia
								owner = {
									OR = {
										AND = {
											independent = yes
											NOT = {
												religion_group = christian
											}
										}
										top_liege = {
											NOT = {
												religion_group = christian
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							any_neighbor_province = {
								region = custom_balearic
							}
						}
					}
					AND = {
						NOR = {
							any_neighbor_province = {
								region = custom_balearic
							}
							any_neighbor_province = {
								any_neighbor_province = {
									region = custom_balearic 
								}
							}
						}
						NOT = {
							any_neighbor_province = {
								any_neighbor_province = {
									region = world_europe_west_iberia
									owner = {
										OR = {
											AND = {
												independent = yes
												NOT = {
													religion_group = christian
												}
											}
											top_liege = {
												NOT = {
													religion_group = christian
												}
											}
										}
									}
								}
							}
						}

						any_neighbor_province = {
							any_neighbor_province = {
								any_neighbor_province = {
									region = custom_balearic
								}
							}
						}
					}
				}
			}

			conditional_tooltip = {
				trigger = {
					ai = yes
				}

				NOT = {
					has_character_modifier = reconquista_preparation_duration
				}
			}
		}

		FROM = {
			any_realm_province = {
				region = custom_balearic
			}
			NOT = {
				religion_group = ROOT
			}
		}
	}

	is_valid = {
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = reconquista_valid_TT

				has_character_flag = currently_reconquering_iberia
			}
		}
		custom_tooltip = {
			text = reconquista_valid_2_TT

			FROM = {
				NOT = {
					religion_group = ROOT
				}
			}
		}
	}

	on_add = {
		custom_tooltip = { text = add_allies_reconquista_TT }
		ROOT = {
			character_event = { id = HF.49210 }
		}
	}

	on_success = {

		d_mallorca = {
			save_event_target_as = first_duchy_title
		}

		reconquista_win_effect = yes
	}

	on_reverse_demand = {
		reconquista_lose_effect = yes
	}

	on_invalidation = {
		ROOT = {
			character_event = { id = HF.49200 }
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

### Otto Specific CBs
thankmars_rebellion = {
	name = CB_NAME_THANKMARS_REBELLION
	war_name = WAR_NAME_THANKMARS_REBELLION
	diplo_view_region = world_europe_west_germania
	sprite = 12
	truce_days = 3650
	is_permanent = no
	allow_whitepeace = no

	allowed_to_target_tributaries = no
	can_ask_to_join_war = no
	major_revolt = yes
	can_call_allies = no

	can_use = {
		always = no
	}

	is_valid = {
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = THANKMAR_REBELLION_01_TT
				has_character_flag = is_thankmar
			}
		}
		FROM = {
			show_scope_change = no
			custom_tooltip = {
				text = THANKMAR_REBELLION_02_TT
				has_character_flag = is_otto
			}
		}
	}

	on_invalidation = {
		any_attacker = {
			set_defacto_liege = FROM
		}
		if = {
			limit = {
				ROOT = {
					is_alive = no
				}
				FROM = {
					is_alive = yes
				}
			}
			any_attacker = {
				imprison = ROOT
			}
		}
	}

	on_success = {
		custom_tooltip = { text = THANKMARS_REBELLION_VICTORY_TT }
		hidden_tooltip = {
			ROOT = {
				remove_trait = bastard
				dynasty = 669
				any_child = {
					limit = {
						NOT = {
							trait = bastard
						}
					}
					dynasty = 669
				}
			}

			FROM = {
				any_demesne_title = {
					grant_title_no_opinion = ROOT
				}
				imprison = ROOT
			}

			any_attacker = {
				set_defacto_liege = ROOT
			}

			ROOT = {
				narrative_event = { id = LT.40009 }
			}
		}
	}

	on_reverse_demand = {
		FROM = {
			show_scope_change = no
			reverse_imprison = ROOT
			narrative_event = { id = LT.40011 }
		}
		any_attacker = {
			show_scope_change = no
			imprison = FROM
		}
		hidden_tooltip = {
			any_attacker = {
				set_defacto_liege = FROM
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}