## normal scopes(including posttitle scopes) ##
# ROOT = receiver
# FROM = giver
# <no scope change> = attacker or receiver
## title scopes ##
# ROOT = receiver
# FROM = giver
# <no scope change> = thirdparty landed title	
#
# the following effects/triggers exists (example execution order: on_success->on_success_title->on_success_posttitle):
# is_valid, is_valid_title, on_add, on_add_title, on_add_posttitle, on_success, on_success_title, on_success_posttitle, on_fail, on_fail_title, on_fail_posttitle, on_reverse_demand, on_reverse_demand_title, on_reverse_demand_posttitle
#
# Added on_attacker_leader_death, on_defender_leader_death and on_thirdparty_death, which all trigger when corresponding character dies
# These three all have war scopes, which currently has the following scope changes: 
# attacker, defender, thirdparty(only valid if thirdparty character is involved), thirdparty_title(only valid if thirdparty title is involved)
#
# ai_will_do: modifies value AI places on the CB compared to other CBs (default: 1)
# can_use_gui: If otherwise valid, the CB is listed in the Diplo View, but you can't declare war unless 'can_use_gui' is also valid (also shows a trigger tooltip.)
#

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

	press_claim = yes

	can_use_title = {
		OR = {
			ROOT = { is_female = no }
			NOT = { ROOT = { religion_group = muslim } }
		}
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
		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 = { is_female = no }
						holder_scope ={ is_female = yes }
					}
					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_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 }
		}
		
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		
		FROM = {
			prestige = -100
		}
	}

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

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

	on_reverse_demand_title = {
		ROOT = {
			remove_claim = PREV
			hidden_tooltip = {
				disband_event_forces = yes
				clr_character_flag = claimant_adventurer
			}
		}
	}
	
	on_attacker_leader_death = {
		if = {
			limit = {
				ROOT = { has_character_flag = claimant_adventurer }
			}
			ROOT = {
				hidden_tooltip = {
					disband_event_forces = yes
					clr_character_flag = claimant_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
	}
}

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

	can_use_title = {
		OR = {
			ROOT = { is_female = no }
			AND = {
				NOT = { ROOT = { religion_group = muslim } }
				NOT = { succ_law_title = { has_law = agnatic_succession } }
			}
		}
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}

		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 = { is_female = no }
						holder_scope ={ is_female = yes }
					}
					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_success = {
		hidden_tooltip = {
			ROOT = {
				opinion = {
					modifier = pressed_my_claim 
					who = PREV
					multiplier = 2
				}
			}
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		FROM = {
			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
					}
				}
			}
		}
	
		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 = {
			
				# 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 = {
						NOT = {
							holder_scope = {
								is_liege_or_above = FROMFROM
							}
						}
						OR = {
							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 }
		}
	}

	on_fail_title = {
		ROOT = {
			prestige = -100
		}
		FROM = {
			FROM = {
				prestige = -100
			}
		}
		
		any_defender = {
			limit = { character = FROM }
			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 = {
			prestige = -150
			imprison = FROM
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 150
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 150 }
		}
	}

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


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

	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 = { is_female = no }
						holder_scope ={ is_female = yes }
					}
					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
				}
			}
		}
	}

	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
			ROOT = { has_claim = PREV }
		}
	}

	on_success = {
		any_attacker = {
			limit = { character = 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 }
			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 }
		}
		
		hidden_tooltip = {
			ROOT = {
				if = {
					limit = {
						has_nickname = no
						PREV = { higher_tier_than = count }
					}
					random = {
						chance = 50
						give_nickname = nick_the_usurper
					}
				}
			}
		}
	}

	on_fail = {
		ROOT = {
			prestige = -100
		}
		any_defender = {
			limit = { character = 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
		}
		
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		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
	}
	
	is_valid = {
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}
}


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

	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_success = {
		any_attacker = {
			limit = { character = 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 }
		
			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
		}
		any_defender = {
			limit = { character = 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 } }
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			imprison = FROM
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		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
	}
	
	is_valid = {
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}
}

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

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

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

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			cancel_plot = plot_gain_vassal_title
		}
		any_defender = {
			limit = { character = 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
	}
}

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

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

		OR = {
			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 = { is_female = no }
						holder_scope ={ is_female = yes }
					}
					is_contested = yes # Is already being contested in some form of Claim or Succession War
				}
			}
		}
		is_vice_royalty = no
	}
			
	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
			ROOT = { has_claim = PREV }
		}
	
		NOT = {
			any_war = { # when used in title scope, checks all wars for that title
				attacker = {
					character = ROOT
				}
			}
		}
	}

	on_success = {
		any_attacker = {
			limit = { 
				OR = {
					character = ROOT 
					character = PREV
				}
			}
			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 }
			
			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 = yes }
		}
		
		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 }
		}
	}

	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
		#	}
		#}
		
		any_defender = {
			limit = { character = 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
		#	}
		#}
		
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		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
	}
	
	is_valid = {
		OR = {
			liege = {
				character = PREV # either independent
			}
			liege = { 
				FROM = { 
					is_liege_or_above = PREV # or have shared liege
				}
			}
		}
	}
}


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

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

	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 = {
			any_claim = {
				limit = {
					holder_scope = {
						OR = {
							character = FROM
							is_liege_or_above = FROM
						}
					}
					ROOT = { has_strong_claim = PREV }
				}
				press_claim = ROOT
			}
		}
	}

	on_success = {
		ROOT = {
			any_claim = {
				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 }
				}
			}
		}
		hidden_tooltip = {
			ROOT = {
				if = {
					limit = {
						has_dlc = "Legacy of Rome"
						OR = { 
							has_landed_title = e_byzantium
							has_landed_title = e_roman_empire
						}
						#religion_group = christian
					}
					character_event = { id = LoR.30 days = 12 }
				}
			}
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

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

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			any_claim = {
				limit = {
					holder_scope = {
						OR = {
							character = FROM
							is_liege_or_above = FROM
						}
					}
					ROOT = { has_strong_claim = PREV }
				}
				ROOT = {
					remove_claim = PREV
				}
			}
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
		}
		FROM = {
			if = {
				limit = { 
					has_dlc = "Legacy of Rome"
					OR = { 
						has_landed_title = e_byzantium
						has_landed_title = e_roman_empire
					}
					#religion_group = christian
				}
				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	
		}
	}
	
	is_valid = {
		FROM = {
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
		}
	}

	is_valid_title = {
		NOT = {
			has_law = succ_seniority
		}
		OR = {
			tier = emperor
			tier = king
			is_primary_holder_title = yes
		}
	}
	
	on_success = {
		FROM = {
			hidden_tooltip = { disband_event_forces = faction_loyalists }
			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
				}
			}
		}
		any_attacker = {
			limit = { character = 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 = {
			prestige = -100
		}
		any_defender = {
			limit = { character = 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_seniority_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			prisoner = FROM
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		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
	}
	
	is_valid = {
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}
}


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	
		}
	}
	
	is_valid = {
		FROM = {
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
		}
	}
		
	is_valid_title = {
		NOT = {
			has_law = succ_primogeniture
		}
		OR = {
			tier = emperor
			tier = king
			is_primary_holder_title = yes
		}
	}

	
	on_success = {
		FROM = {
			hidden_tooltip = { disband_event_forces = faction_loyalists }
			
			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
				}
			}
		}
		
		any_attacker = {
			limit = { character = 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 = primogeniture
	}

	on_fail = {
		ROOT = {
			prestige = -100
		}
		any_defender = {
			limit = { character = 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_primogeniture_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			prisoner = FROM
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		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
	}
	
	is_valid = {
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}
}

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

	is_valid = {
		FROM = {
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
		}
	}

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

#		OR = {
#			AND = {
#				tier = emperor
#				OR = {
#					ROOT = { tier = king }
#					ROOT = { tier = duke }
#				}
#			}
#			AND = {
#				tier = king
#				ROOT = { tier = duke }
#			}
#			AND = {
#				tier = duke
#				ROOT = { tier = count }
#			}
#		}

	}
	
	on_success = {
		FROM = {
		
			hidden_tooltip = { disband_event_forces = faction_loyalists }
			
			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
				}
			}
		}
		any_attacker = {
			limit = { character = 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 = feudal_elective
	}

	on_fail = {
		ROOT = {
			prestige = -100
		}
		any_defender = {
			limit = { character = 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_feudal_elective_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}
	
	on_reverse_demand = {
		ROOT = {
			prestige = -200
			prisoner = FROM
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		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
	}
	
	is_valid = {
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}
}


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
	
	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_success = {
		ROOT = {
			prestige = 200
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		
		hidden_tooltip = {
			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_dlc = "Legacy of Rome"
						OR = { 
							has_landed_title = e_byzantium
							has_landed_title = e_roman_empire
						}
						#religion_group = christian
					}
					character_event = { id = LoR.30 days = 12 }
				}
			}
		}
		
		FROM = {
			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 = {
				higher_tier_than = BARON
				NOT = { culture = ROOT }
			}
			hidden_tooltip = { conquest_culture = ROOT }
		}
	}

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

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 8.0
			}
		}
		FROM = {
			prestige = 200
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
		}
		FROM = {
			if = {
				limit = { 
					has_dlc = "Legacy of Rome"
					OR = { 
						has_landed_title = e_byzantium
						has_landed_title = e_roman_empire
					}
					#religion_group = christian
				}
				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 = {
			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
	}
}

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

	can_use_gui = {
		ROOT = {
			prestige = 500
		}
	}
	
	on_add = {
		ROOT = { prestige = -500 }
	}

	can_use_title = {
		ROOT = {
			OR = {
				AND = {
					independent = yes
					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
					}
					higher_tier_than = COUNT
					OR = {
						has_horde_culture = yes
						has_character_flag = horde_invader
						FROM = {
							NOT = { religion_group = pagan_group } # Should subjugate these instead
						}
					}
				}
				
				AND = {
					mercenary = yes
					NOT = {
						num_of_titles = 2
					}
				}
			}
		}
		
		ROOT = {
			NOT = { is_liege_or_above = FROM }
		}
	}
	
	is_valid_title = {
		FROM = {
			any_realm_title = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}

	on_success = {
		ROOT = {
			prestige = 200
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
		}
		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
					}
					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 = {
		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
					}
				}
			}
		}
	}

	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 }
					}
				}
			}
		}
		any_defender = {
			limit = { character = FROM }
			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
					}
					#religion_group = christian
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
		}
		any_defender = {
			limit = { character = FROM }
			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
		}
		
		# 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
				}
			}
		}
	}
}

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
	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 = {
		ROOT = {
			has_called_crusade = no
			religion_authority = 0.05
			OR = {
				religion_group = zoroastrian_group
				religion_group = jewish_group
				religion_group = god_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
					}
				}
			}
		}
	}

	can_use = {
		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 }
				}
			}
		}
		
		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_title = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}

	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 = {
						PREV = { has_holder = no }
						OR = {
							independent = yes
							is_ruler = no
							liege = {
								tier = EMPEROR
							}
						}
					}
					usurp_title = { target = PREV type = holy_war }
				}
				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
				}
			}
		}
		
		# 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_attacker = {
				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 = { has_holder = no }
						NOT = { character = ROOT }
						OR = {
							independent = yes
							liege = {
								tier = EMPEROR
							}
						}
					}
					usurp_title = { target = PREV type = holy_war }
					
					if = {
						limit = {
							NOT = { culture = PREV }
						}
						hidden_tooltip = { PREV = { conquest_culture = THIS } }
					}
				}
				hidden_tooltip = {
					if = {
						limit = {
							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 = { character = ROOT }
			participation_scaled_prestige = 500
			participation_scaled_piety = 1000
			if = {
				limit = {
					religion_group = muslim
				}
				participation_scaled_decadence = -100
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_prestige = 500
				participation_scaled_piety = 1000
				if = {
					limit = {
						religion_group = muslim
					}
					participation_scaled_decadence = -100
				}
			}
		}
		any_attacker = {
			limit = {
				ai = no
				NOT = { has_character_flag = achievement_crusader }
			}
			set_character_flag = achievement_crusader
		}
		
		ROOT = {
			religion_authority = {
				modifier = won_crusade
				years = 20
			}
		}
		
		FROM = {
			piety = -100
			religion_authority = {
				modifier = lost_crusade
				years = 20
			}			
		}
	}

	on_fail = {
		ROOT = {
			piety = -100
		}
		
		any_defender = {
			limit = { character = FROM }
			participation_scaled_piety = 200
			participation_scaled_prestige = 100
			if = {
				limit = {
					religion_group = muslim
				}
				participation_scaled_decadence = -50
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_piety = 200
				participation_scaled_prestige = 100
				if = {
					limit = {
						religion_group = muslim
					}
					participation_scaled_decadence = -50
				}
			}
		}
		
		FROM = {
			if = {
				limit = { 
					has_dlc = "Legacy of Rome"
					OR = { 
						has_landed_title = e_byzantium
						has_landed_title = e_roman_empire
					}
					#religion_group = christian
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
			
			hidden_tooltip = {
				if = {
					limit = {
						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 = {
			piety = -200
			religion_authority = {
				modifier = lost_crusade
				years = 20
			}
		}
		FROM = {
			piety = 100
			religion_authority = {
				modifier = won_crusade
				years = 20
			}
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_piety = 300
			participation_scaled_prestige = 150
			if = {
				limit = {
					religion_group = muslim
				}
				participation_scaled_decadence = -100
			}
		}
		FROM = {
			if = {
				limit = { 
					has_dlc = "Legacy of Rome"
					OR = { 
						has_landed_title = e_byzantium
						has_landed_title = e_roman_empire
					}
					#religion_group = christian
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
			hidden_tooltip = {
				if = {
					limit = {
						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 = {
						religion_group = muslim
					}
					participation_scaled_decadence = -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
	}
}

# 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

	can_use = {
		ROOT = {
			NOT = { religion_group = pagan_group }
			NOT = { pacifist = yes }
			NOT = { religion = buddhist }
			OR = {
				NOT = { religion = hindu }
				FROM = { NOT = { religion_group = indian_group } }
			}
			NOT = { same_realm = FROM }
			OR = {
				NOT = { religion_group = FROM }
				is_heresy_of = FROM
				is_parent_religion = FROM
				AND = {
					religion_group = muslim
					NOT = { religion = FROM }
					FROM = {
						NOT = { 
							any_current_enemy = {
								NOT = {
									religion_group = muslim
								}
							}
						}
					}
				}
			}
			top_liege = {
				religion_group = ROOT
				NOT = { religion = FROM }
				OR = {
					religion = ROOT
					AND = {
						is_heretic = no
						ROOT = { is_heretic = 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 = {
									year = 1100
									ROOT = { religion_group = muslim }
									FROM = { religion_group = muslim }
								}
								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 = { religion = buddhist }
			NOT = { same_realm = FROM }
			OR = {
				NOT = { religion_group = FROM }
				is_heresy_of = FROM
				is_parent_religion = FROM
				AND = {
					religion_group = muslim
					NOT = { religion = FROM }
				}
			}
			top_liege = {
				NOT = { religion = FROM }
			}
		}
	}
	
	on_success = {
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_piety = 100
			participation_scaled_prestige = 50
			if = {
				limit = {
					religion_group = muslim
				}
				participation_scaled_decadence = -10
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_piety = 100
				participation_scaled_prestige = 50
				if = {
					limit = {
						religion_group = muslim
					}
					participation_scaled_decadence = -10
				}
			}
		}
		
		ROOT = { 
			religion_authority = {
				modifier = won_holy_war
				years = 20
			}
		}
		FROM = { 
			religion_authority = {
				modifier = lost_holy_war
				years = 20
			}		
		}
		FROM = { piety = -100 }
		FROM = { 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
					}
	#				gain_settlements_under_title = {
	#					title = PREV
	#					enemy = FROM
	#				}
				}
			}
		}
	}

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

	on_reverse_demand = {
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -75
			prestige = -150
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_piety = 200
			participation_scaled_prestige = 100
			if = {
				limit = {
					religion_group = muslim
				}
				participation_scaled_decadence = -10
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_piety = 200
				participation_scaled_prestige = 100
				if = {
					limit = {
						religion_group = muslim
					}
					participation_scaled_decadence = -10
				}
			}
		}
		ROOT = { 
			religion_authority = {
				modifier = lost_holy_war
				years = 20
			}
		}
		FROM = { 
			religion_authority = {
				modifier = won_holy_war
				years = 20
			}		
		}
	}

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

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

	is_valid = {
		FROM = {
			excommunicated_for = ROOT
		}
	}

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

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

bid_for_independence = {
	name = CB_NAME_INDEPENDENCE
	war_name = WAR_NAME_INDEPENDENCE
	sprite = 12
	truce_days = 3650
	
	is_permanent = yes
	is_independence = 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
			}
		}
		
		FROM = {
			NOT = {
				any_war = {
					attacker = {
						character = FROM
					}
					OR = {
						using_cb = bid_for_independence
						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
			}
		}
	}

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

		FROM = {
			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_fail = {
		ROOT = {
			prestige = -50
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = -50
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = -50 }
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -100
			prisoner = FROM
		}
		any_attacker = {
			limit = { character = ROOT }
			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 = {
		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 = FROM
							}
							OR = {
								using_cb = bid_for_independence
								using_cb = depose_liege
							}
						}
						AND = { # Cannot depose liege if someone else is already deposing liege
							defender = {
								character = FROM
							}
							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_success = {
		FROM = {
			prestige = -500
			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
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 250
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 250 }
		}
	}

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

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

	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_success = {
		ROOT = {
			primary_title = {
				add_pressed_claim = FROM	
			}
		}
		FROM = {
			prestige = -500
			remove_opinion = {
				who = ROOT
				modifier = declared_war
			}
			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
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 250
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 250 }
		}
	}

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

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

	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 = {
		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_success = {
		ROOT = {
			prestige = 200
			primary_title = {
				add_pressed_claim = FROM	
			}
		}
		FROM = {
			prestige = -500
			remove_opinion = {
				who = ROOT
				modifier = declared_war
			}
			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
		}
	}

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

	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 = {
		ROOT = {
			vassal_of = FROM	
		}
	}
	
	is_valid = {
		FROM = {
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
		}
	}

	is_valid_title = {
		NOT = {
			has_law = succ_gavelkind
		}
		OR = {
			tier = emperor
			tier = king
			is_primary_holder_title = yes
		}
	}
	
	on_success = {
		FROM = {
			hidden_tooltip = { disband_event_forces = faction_loyalists }
			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
				}
			}
		}
		
		any_attacker = {
			limit = { character = 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
		}
		any_defender = {
			limit = { character = 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
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		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
	}
	
	is_valid = {
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}
}

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 = {
		ROOT = {
			vassal_of = FROM
		}
	}
	
	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
				}
			}
		}
	}

	on_success = {
		ROOT = {
			opinion = {
				modifier = opinion_lowered_crown_authority
				who = FROM
				months = 12
			}
		}
		any_attacker = {
			limit = { character = 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 } }
	}

	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
		}
		
		any_defender = {
			limit = { character = 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_lower_CA_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			prisoner = FROM
		}
		
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		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
				}
			}
		}
		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
	}
	
	is_valid = {
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}
}


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

	on_success = {
		ROOT = {
			opinion = {
				modifier = opinion_lowered_tribal_organization
				who = FROM
				months = 12
			}
		}
		any_attacker = {
			limit = { character = 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 } }
	}

	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
		}
		
		any_defender = {
			limit = { character = 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_lower_TO_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			prisoner = FROM
		}
		
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		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
				}
			}
		}
		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
	}
	
	is_valid = {
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}
}


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

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

	can_use_title = {
		tier = BARON
		is_capital = no # Minor Holding
		is_republic = yes
		
		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_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 }
				}
			}
		}
		
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}   

	on_fail = {
		ROOT = {
			prestige = -100
		}
		any_defender = {
			limit = { character = 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
			}
		}
		any_defender = {
			limit = { character = 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

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

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

	on_fail = {
		ROOT = {
			prestige = -100
		}
		any_defender = {
			limit = { character = 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
			}
		}
		any_defender = {
			limit = { character = 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_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_success = {
		any_attacker = {
			participation_scaled_prestige = 100
		}
		FROM = {
			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
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			imprison = FROM
			cancel_plot = plot_weaken_fellow_vassal
		}
		any_defender = {
			limit = { character = 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_success = {
		FROM = {
			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
		}
		any_attacker = {
			limit = { character = ROOT }
			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 = {
							tier = PREVPREV
						}
						usurp_title_plus_barony_if_unlanded = { target = ROOT type = revolt }
						
						if = {
							limit = {
								higher_tier_than = BARON
								NOT = { culture = ROOT }
							}
							hidden_tooltip = { conquest_culture = ROOT }
						}
					}
				}
			}
			
			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 }
				}
			}
		}
	}

	on_fail = {
		ROOT = {
			prestige = -200
		}
		FROM = {
			prestige = 100
		}
		any_defender = {
			limit = { character = FROM }
			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 = {
			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
	
	can_use_gui = {
		ROOT = {
			piety = 1000
		}
	}
	
	on_add = {
		ROOT = { piety = -1000 }
	}

	can_use = {
		ROOT = {
			religion_group = muslim
		}
		
		NOT = {
			# These use 'tribal_invasion' instead:
			ROOT = {
				OR = {
					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 }
		}
	}
	
	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_title = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}

	on_success = {
		ROOT = {
			prestige = 200
		}
		
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
			
			if = {
				limit = {
					religion_group = muslim
					FROM = { NOT = { religion_group = muslim } }
				}
				participation_scaled_decadence = -20
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_prestige = 200 
				if = {
					limit = {
						religion_group = muslim
						FROM = { NOT = { religion_group = muslim } }
					}
					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 title holder is not participating in the war, gain holdings occupied by all Crusade participants
						type = invasion
					}
					
					if = {
						limit = {
							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 = {
			prestige = 100
		}
		FROM = {
			if = {
				limit = {
					has_dlc = "Legacy of Rome"
					OR = { 
						has_landed_title = e_byzantium
						has_landed_title = e_roman_empire
					}
					#religion_group = christian
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
			
			if = {
				limit = {
					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 }
				}
			}
		}
		any_defender = {
			limit = { character = FROM }
			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
			}
			decadence = 10
		}
		FROM = {
			prestige = 200
		}
		FROM = {
			if = {
				limit = { 
					has_dlc = "Legacy of Rome"
					OR = { 
						has_landed_title = e_byzantium
						has_landed_title = e_roman_empire
					}
					#religion_group = christian
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
			if = {
				limit = {
					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 }
				}
			}
		}
		any_defender = {
			limit = { character = FROM }
			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 = 1
		
		modifier = {
			factor = 0.9 # Prefer using CB's without on_add costs
		}
	}
}

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 = {
		ROOT = {
			vassal_of = 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
			}
			independent = yes
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
		}
	}

	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
					}
				}
			}		
		}
		
		any_attacker = {
			limit = { character = 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 }
		
			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
				}
			}
		}
	}

	on_fail = {
		ROOT = {
			prestige = -50
#			prisoner = FROM
			add_character_modifier = {
				name = faction_independence_ultimatum_timer
				hidden = yes
				duration = 3650
			}
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		any_attacker = {
			limit = { character = ROOT }
			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
			}
		}
		
		any_attacker = {
			limit = { character = ROOT }
			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 }
		}
	}

	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

	can_use = {
		ROOT = {
			OR = {
				has_landed_title = e_byzantium
				has_landed_title = e_roman_empire
			}
			independent = yes
			NOT = { same_realm = FROM }
		}
	}	

	can_use_title = {
		OR = {
			de_jure_liege_or_above = k_byzantium
			de_jure_liege_or_above = k_anatolia
			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_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_venice
			de_jure_liege_or_above = k_asturias
		}
	}
	
	is_valid = {
		ROOT = {		
			independent = yes
			NOT = { same_realm = FROM }
		}
	}
	
	on_success = {
		any_attacker = {
			limit = { character = 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 = {
					gain_settlements_under_title = {
						title = PREV
						enemy = FROM
						type = invasion
					}
				}
			}
		}
	}

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

	on_reverse_demand = {
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			ROOT = { prestige = -200 }
		}
		any_defender = {
			limit = { character = 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 = 11
	truce_days = 3650
	
	is_permanent = yes
	can_ask_to_join_war = no

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

	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_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 = {
			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 = 1
	truce_days = 1825
	is_permanent = yes
#	check_all_titles = yes
	check_all_trade_posts = yes
	can_ask_to_join_war = no
	
	allow_distant = 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
					}
				}
			}
		}
		FROM = {
			OR = {
				is_merchant_republic = yes
				is_patrician = yes
			}
			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 = {
			OR = {
				is_merchant_republic = yes
				is_patrician = yes
			}
			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_success_title = {
		
		seize_trade_post = ROOT
		
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 50
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_fail = {
		ROOT = {
			prestige = -50
		}
		any_defender = {
			limit = { character = 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
			}
		}
		any_defender = {
			limit = { character = 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 = {
			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 = 8
	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

	can_use = {
		ROOT = {
			has_opinion_modifier = { who = FROM modifier = preparing_to_invade }
			NOT = { is_liege_or_above = FROM }
		}
	}
	
	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_title = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}

	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 }
					}
				}
			}
		}
		
		any_attacker = {
			limit = { character = ROOT }
			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
		}
		any_defender = {
			limit = { character = FROM }
			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
		}
		FROM = {
			if = {
				limit = { 
					has_dlc = "Legacy of Rome"
					OR = { 
						has_landed_title = e_byzantium
						has_landed_title = e_roman_empire
					}
					#religion_group = christian
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
		}
		any_defender = {
			limit = { character = FROM }
			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
	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
	
	can_use = {
		ROOT = {
			religion_group = pagan_group
			is_reformed_religion = no
			NOT = { religion = aztec }
			NOT = { same_realm = FROM }
			NOT = { is_liege_or_above = FROM }
			has_horde_culture = no # Should use tribal invasion instead
		}
		FROM = {
			religion_group = pagan_group
			OR = {
				is_reformed_religion = no
				is_reformed_religion = ROOT
			}
			NOT = {
				any_war = {
					AND = {
						defender = { character = FROM }
						using_cb = pagan_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_title = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}
	
	is_valid_title = {
		FROM = {
			any_realm_title = {
				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
					days = -1
					hidden = yes
				}
			}
		}
	}

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

	on_reverse_demand = {
		ROOT = {
			piety = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
		}
		FROM = {
			piety = 100
		}
		FROM = {
			if = {
				limit = { 
					has_dlc = "Legacy of Rome"
					OR = { 
						has_landed_title = e_byzantium
						has_landed_title = e_roman_empire
					}
					#religion_group = christian
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

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

# 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
	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 = yes
	
	is_independence = yes
	
	can_use = {
		ROOT = {
			primary_title = { temporary = yes } # Revolter or adventurer trigger
			war = no
			NOT = { is_liege_or_above = FROM }
		}
	}

	can_use_title = {
		tier = count
	}

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

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

	on_fail_title = {
		FROM = {
			prestige = 10
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor 
					who = ROOT
					months = 1200
				}
			}
		}
		ROOT = { 
			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 = {
			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 = yes
	is_holy_war = yes
	
	can_use = {
		ROOT = { has_character_flag = heretic_revolter }
	}
	
	is_valid = {
		FROM = {
			NOT = { religion = ROOT }
		}
	}

	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_fail = {
		ROOT = {
			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 = {
			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 = no	
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = yes
	is_holy_war = yes	
	
	can_use = {
		ROOT = { has_character_flag = religious_revolter }
	}
	
	is_valid = {
		FROM = {
			NOT = { religion = ROOT }
		}
	}
 
	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_fail = {
		ROOT = {
			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 = {
			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
	is_permanent = yes
	# check_all_titles = yes # if permanent, setting this to true will check against all of someones titles, including vassal held titles
	check_de_jure_tier = KING # 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
	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_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 }
					}
				}
			}
		}
	}

	on_fail_title = {
		FROM = {
			prestige = 100
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor 
					who = ROOT
					months = 1200
				}
			}
		}
		ROOT = {
			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 = {
			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
	is_permanent = yes
	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. Only valid if is_permanent = yes

	can_use = {
		ROOT = { has_character_flag = duchy_adventurer }
	}

	is_valid = {
		always = 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
					}
					character_event = {
						id = 60206
						days = 1
					}
				}
				
				# Special case: Timur succeeds in his first conquest
				if = {
					limit = {
						has_character_flag = timur_himself
					}
					character_event = {
						id = 60306
						days = 1
					}
				}
				
				# Special case: Sabuktigin succeeds in his first conquest
				if = {
					limit = {
						has_character_flag = sabuktigin_himself
					}
					character_event = {
						id = 60406
						days = 1
					}
				}
			}
		}
	}

	on_fail = {
		FROM = {
			prestige = 100
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor 
					who = ROOT
					months = 1200
				}
			}
		}
		ROOT = {
			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 = {
			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
	}
}

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
	
	can_use_gui = {
		ROOT = {
			piety = 100
		}
	}
	
	on_add = {
		ROOT = { piety = -100 }
	}

	can_use = {
		ROOT = {
			OR = {
				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 = {
				religion = ROOT
			}
		}
	}
	
	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 = 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 = { character = ROOT }
			participation_scaled_piety = 100
			participation_scaled_prestige = 50
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_piety = 100
				participation_scaled_prestige = 50
			}
		}
		
		ROOT = { 
			religion_authority = {
				modifier = won_holy_war
				years = 20
			}
			
			hidden_tooltip = {
				if = {
					limit = {
						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 = { piety = -100 }
		FROM = { 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
			}
		}
		any_defender = {
			limit = { character = 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
			}
		}
		ROOT = { 
			religion_authority = {
				modifier = lost_holy_war
				years = 20
			}
		}
		FROM = { 
			religion_authority = {
				modifier = won_holy_war
				years = 20
			}		
		}
	}

	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.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
	
	can_use_gui = {
		ROOT = {
			piety = 200
		}
	}
	
	on_add = {
		ROOT = { piety = -200 }
	}

	can_use = {
		ROOT = {
			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_horde_culture = no # Should use tribal invasion instead
			NOT = { has_character_modifier = launched_subjugation }
		}
		FROM = {
			religion = ROOT
		}
	}
	
	can_use_title = {
		FROM = {
			any_realm_title = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}
	
	is_valid_title = {
		FROM = {
			any_realm_title = {
				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
		}
		any_attacker = {
			limit = { character = ROOT }
			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
		}
		any_defender = {
			limit = { character = FROM }
			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
		}
		FROM = {
			if = {
				limit = { 
					has_dlc = "Legacy of Rome"
					OR = { 
						has_landed_title = e_byzantium
						has_landed_title = e_roman_empire
					}
					#religion_group = christian
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

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

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
	press_claim = yes
	can_ask_to_join_war = no
	
	can_use_gui = {
		ROOT = {
			piety = 50
		}
	}
	
	on_add = {
		ROOT = { piety = -50 }
	}
	
	can_use = {
		ROOT = {
			NOT = { is_liege_or_above = FROM }
		}
	}

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

	on_fail_title = {
		ROOT = {
			prestige = -100
		}
		any_defender = {
			limit = { character = 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
			}
		}
		any_defender = {
			limit = { character = 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
		}
	}
}

pagan_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
	press_claim = yes
	can_ask_to_join_war = no
	
	allow_distant = yes
	
	can_use = {
		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 }
				}
				FROM = { NOT = { religion_group = pagan_group } }
				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 = {
		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_success_title = {
		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 }
						}
					}
				}
			}

			usurp_title = ROOT
		}
		
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
			participation_scaled_piety = 50
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_prestige = 100 
				participation_scaled_piety = 50
			}
		}
		
		ROOT = {
			religion_authority = {
				modifier = won_county_conquest
			}
		}
	}

	on_fail_title = {
		ROOT = {
			prestige = -100
			religion_authority = {
				modifier = lost_county_conquest
			}
		}
		any_defender = {
			limit = { character = 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
			}
			religion_authority = {
				modifier = lost_county_conquest
			}
		}
		any_defender = {
			limit = { character = FROM }
			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
	}
}

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

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

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

	on_fail_title = {
		ROOT = {
			prestige = -100
		}
		any_defender = {
			limit = { character = 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
			}
		}
		any_defender = {
			limit = { character = 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
	}	
}

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

	can_use_title = {
		tier = baron
		dejure_liege_title = {
			AND = {
				has_holder = yes
				ROOT = {
					has_landed_title = PREV
				}
			}
		}

		NOT = {
			OR = {
				ROOT = {
					has_landed_title = PREV
				}
				holder_scope = {
					is_liege_or_above = ROOT
				}
			}
		}

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

		NOT = {
			holder_scope = {
				liege = {
					ROOT = {
						liege = {
							character = PREVPREV
						}
					}
				}
			}
		}
		
		FROM = {
			OR = {
				holy_order = no
				NOT = { religion = ROOT }
				NOT = { has_dlc = "Sons of Abraham" }
			}
		}
	}

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

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

	on_fail_title = {
		ROOT = {
			prestige = -50
		}
		any_defender = {
			limit = { character = 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
			}
		}
		any_defender = {
			limit = { character = 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
	}
}

other_dejure_barony_claim = {
	name = CB_NAME_DEJUREBARONYCLAIM
	war_name = WAR_NAME_OTHER_DEJUREBARONYCLAIM
	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

	can_use = {
		NOT = {
			ROOT = {
				character = PREV
			}
		}
	}

	can_use_title = {
		tier = baron

		dejure_liege_title = {
			AND = {
				has_holder = yes
				ROOT = {
					has_landed_title = PREV
				}
			}
		}

		NOT = {
			OR = {
				ROOT = {
					has_landed_title = PREV
				}
				holder_scope = {
					is_liege_or_above = ROOT
				}
			}
		}

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

		NOT = {
			holder_scope = {
				liege = {
					ROOT = {
						liege = {
							character = PREVPREV
						}
					}
				}
			}
		}
		
		FROM = {
			OR = {
				holy_order = no
				NOT = { religion = ROOT }
				NOT = { has_dlc = "Sons of Abraham" }
			}
		}
	}

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

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

	on_success_title = {
		usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
	}

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

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

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


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

	can_use = {
		NOT = {
			ROOT = {
				character = PREV
			}
		}
		
		FROM = {
			OR = {
				holy_order = no
				NOT = { religion = ROOT }
				NOT = { has_dlc = "Sons of Abraham" }
			}
		}
	}

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

		NOT = {
			OR = {
				ROOT = {
					has_landed_title = PREV
				}
				holder_scope = {
					is_liege_or_above = ROOT
				}
			}
		}

		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_success = {
		ROOT = {
			opinion = {
				modifier = pressed_my_claim 
				who = PREV
			}
		}
		any_attacker = {
			limit = { character = ROOT }
			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 = {
				set_defacto_liege = ROOT
			}
		}

		if = {
			limit = {
				holder_scope = {
					OR = {
						higher_tier_than = count
						num_of_count_titles = 2
					}
				}
			}

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

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

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

# 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
	is_permanent = 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

	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_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 }
					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
					}
					usurp_title = { target = PREV type = revolt }
				}
				
				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
		}
		any_defender = {
			limit = { character = FROM }
			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
	}

	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 = 11
	truce_days = 3650
	is_permanent = yes
	can_ask_to_join_war = yes
	
	attacker_rel_head_is_ally = yes # The attacker can call his (main) Pope into the war
	
	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 }
			}
		}
		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_success = {
		any_attacker = {
			limit = { character = 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
			}
		}
		
		ROOT = {
			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 = {
		ROOT = { 
			piety = -250
			prestige = -125
		}
		FROM = { 
			piety = 100
			prestige = 50
		}
	}

	on_reverse_demand = {
		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 = {
		ROOT = {
			vassal_of = FROM
			religion = FROM
			OR = {
				religion = catholic
				religion = fraticelli
			}
		}
	}
	
	is_valid = {
		NOT = {
			holder_scope = {
				character = ROOT
			}
		}
	
		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_success = {
		any_attacker = {
			limit = { character = 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
			}
		}
		
		ROOT = {
			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_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
		}
	}

	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
	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
	press_claim = yes
	can_ask_to_join_war = no
	
	can_use = {
		ROOT = {
			religion = buddhist
			NOT = { same_realm = FROM }
			NOT = { religion_group = FROM }
			top_liege = {
				religion_group = ROOT
				NOT = { religion = FROM }
			}
		}
	}
	
	can_use_title = {
		tier = count
		
		# The attacker needs a border or be at the most two sea zones away
		OR = {
			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 }
			NOT = { religion_group = FROM }
			top_liege = {
				NOT = { religion = 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 = 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 }
			}
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_fail_title = {
		ROOT = {
			prestige = -100
		}
		any_defender = {
			limit = { character = 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
			}
		}
		any_defender = {
			limit = { character = 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
	}
}

# 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
	
	can_use_gui = {
		ROOT = {
			piety = 500
		}
	}
	
	on_add = {
		ROOT = { piety = -500 }
	}
	
	can_use = {
		ROOT = {
			religion_group = indian_group
			NOT = { same_realm = FROM }
			NOT = { is_liege_or_above = FROM }
			has_horde_culture = no # Should use tribal invasion instead
		}
		FROM = {
			religion_group = indian_group
			culture_group = ROOT
			NOT = {
				any_war = {
					AND = {
						defender = { character = FROM }
						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_title = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}
	
	is_valid_title = {
		FROM = {
			any_realm_title = {
				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
					days = -1
					hidden = yes
				}
			}
		}
	}

	on_success = {
		any_attacker = {
			limit = { character = 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 = {
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		ROOT = {
			prestige = -100
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
		}
		any_defender = {
			limit = { character = FROM }
			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
		}
	}
}

# 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 = {
		ROOT = {
			OR = {
				AND = {
					dynasty = 613 # Seljuks
					NOT = { year = 1200 }
				}
				dynasty = 800 # Timurids
			}
		}
		
		ROOT = {
			NOT = { is_liege_or_above = FROM }
		}
	}
	
	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_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 }
					}
				}
			}
		}
		
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
			
			if = {
				limit = {
					religion_group = muslim
					FROM = { NOT = { religion_group = muslim } }
				}
				participation_scaled_decadence = -20
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_prestige = 200 
				if = {
					limit = {
						religion_group = muslim
						FROM = { NOT = { religion_group = muslim } }
					}
					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
					}
				}
			}
		}
	}

	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
					}
					#religion_group = christian
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
		}
		any_defender = {
			limit = { character = FROM }
			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
			}
			decadence = 10
		}
		FROM = {
			prestige = 200
		}
		FROM = {
			if = {
				limit = { 
					has_dlc = "Legacy of Rome"
					OR = { 
						has_landed_title = e_byzantium
						has_landed_title = e_roman_empire
					}
					#religion_group = christian
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
		}
		any_defender = {
			limit = { character = FROM }
			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
	}
}

# Decadence Usurption
cb_decadence_usurption = {
	name = CB_NAME_DECADENCE_USURPTION
	war_name = WAR_NAME_DECADENCE_USURPTION
	sprite = 11
	truce_days = 3650
	is_permanent = yes
	
	can_use = {
		ROOT = { 
			religion_group = muslim 
		}	
	
		FROM = {
			religion_group = muslim
			trait = decadent
			dynasty = ROOT
		}
		
		FROM = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					ROOT = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}
	
	is_valid = {
		FROM = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					ROOT = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}
	
	on_success = {
		any_attacker = {
			limit = { character = 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
	}
}
