## 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_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 = yes
	is_claim_all = yes
	
	allowed_to_target_tributaries = yes
	allowed_to_target_suzerains = no

	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		ROOT = {
			attacking_religious_head_trigger = no
			any_claim = {
				holder_scope = {
					OR = {
						character = FROM
						is_liege_or_above = FROM
					}
				}
				is_usable_claim_trigger = yes
				count = 2
			}
		}
		#only cynics can press claims against same-religion holy orders?
		OR = {
			FROM = { NOT = { religion = ROOT } }
			ROOT = { trait = cynical }
			ROOT = { ai = no }
			FROM = { ai = no }
			FROM = { higher_tier_than = duke }
			FROM = { holy_order = no }
		}
	}

	is_valid = {
		ROOT = {
			any_claim = {
				holder_scope = {
					OR = {
						character = FROM
						is_liege_or_above = FROM
					}
				}
				is_usable_claim_trigger = yes				
			}
		}
	}

	on_add = {
		ROOT = {
			any_claim = {
				limit = {
					holder_scope = {
						OR = {
							character = FROM
							is_liege_or_above = FROM
						}
					}
					is_usable_claim_trigger = yes
				}
				press_claim = ROOT
			}
		}
		FROM = {
			attacked_tributary_cb_effect = yes
		}
	}

	on_success = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their Claim All war against [From.GetBestName] (ID: [From.GetID])"
		FROM = { save_event_target_as = pf_lc_enemy }
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			any_claim = {
				limit = {
					holder_scope = {
						OR = {
							character = FROM
							is_liege_or_above = FROM
						}
					}
					is_usable_claim_trigger = yes
				}
				title_reduce_CA_effect = yes
				title_reduce_TO_effect = yes
				empty_province_effect = yes
				pf_liege_change_under_title_begin_effect = yes
				usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
				if = {
					limit = {
						higher_tier_than = BARON
						NOT = { culture = ROOT }
					}
					hidden_tooltip = { conquest_culture = ROOT }
				}
			}
		}
		pf_liege_change_under_title_end_effect = yes
		hidden_tooltip = {
			ROOT = {
				if = {
					limit = {
						has_dlc = "Legacy of Rome"
						OR = { 
							has_landed_title = e_byzantium
							has_landed_title = e_roman_empire
							AND = {
								independent = yes
								higher_tier_than = COUNT
								OR = {
									culture = roman
									religion = hellenic_pagan
								}
							}
						}
						OR = {
							religion_group = christian
							religion_group = pagan_group
						}
					}
					character_event = { id = LoR.30 days = 12 }
				}
			}
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		FROM = { pf_glory_minus2_effect = yes }
	}

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

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			pf_glory_minus2_effect = yes
			any_claim = {
				limit = {
					holder_scope = {
						OR = {
							character = FROM
							is_liege_or_above = FROM
						}
					}
					is_usable_claim_trigger = yes
				}
				remove_claim = ROOT
			}
		}
		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
						AND = {
							independent = yes
							higher_tier_than = COUNT
							OR = {
								culture = roman
								religion = hellenic_pagan
							}
						}
					}
					OR = {
						religion_group = christian
						religion_group = pagan_group
					}
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
	}

	on_attacker_leader_death = {
		if = {
			limit = { ROOT = { has_character_flag = claimant_adventurer } }
			ROOT = {
				hidden_tooltip = {
					disband_event_forces = claimant_adventure
					clr_character_flag = claimant_adventurer
				}
			}
			end_war = invalid
		}
	}	
	
	on_invalidation = {
		log = "CB(claim_all): INVALIDATED: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
	}

	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 = 1.2
			ROOT = {
				realm_levy_diff = { who = FROM value = 5000 }
			}
		}
		modifier = { # The AI will never breach a tributary's trust
			factor = 0
			ai_attack_tributary_trigger = yes
		}
	}
}

claim = {
	name = CB_NAME_CLAIM
	war_name = WAR_NAME_CLAIM
	sprite = 1
	truce_days = 3650
	hostile_against_others = yes
	can_ask_to_join_war = yes
	
	infamy_modifier = 0.85
	
	allowed_to_target_tributaries = yes
	allowed_to_target_suzerains = no

	press_claim = yes

	can_use_title = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { attacking_religious_head_trigger = no }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		#AI non-adventurers will only press claims to different religion groups if the title is duke+
		OR = {
			ROOT = { ai = no }
			higher_tier_than = count
			ROOT = { religion_group = FROM }
			ROOT = { has_character_flag = claimant_adventurer }
		}				
		#disallow non-adventurer females from pressing claims unless they're already a ruler or they're non-muslim and the title is not agnatic
		ROOT = {
			OR = {
				is_female = no
				has_character_flag = claimant_adventurer
				is_ruler = yes
				NOT = { religion_group = muslim }
				religion = druze
			}
		}
		#disallow females from pressing claims on agnatic nomadic titles
		OR = {
			ROOT = { ai = no }
			FROM = { is_nomadic = no }
			ROOT = { is_female = no }
			NOT = { has_law = agnatic_succession }
		}
		
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
		ROOT = {
			OR = {
				is_ruler = no
				primary_title = {
					OR = {
						is_primary_type_title = no # Static Mercs, the Pope, Holy Orders, etc
						higher_tier_than = PREVPREV
						tier = PREVPREV
					}
				}
			}
		}
		OR = { 
			ROOT = { has_strong_claim = PREV }
			ROOT = { has_character_flag = claimant_adventurer  }
			AND = {
				ROOT = { has_weak_claim = PREV }
				OR = {
					is_contested = yes # is already being contested in some form of Claim or Succession War
					is_pretender = yes # Checks vs ROOT character
					holder_scope = { has_regent = yes }
					AND = {
						higher_tier_than = duke
						holder_scope = { trait = uncrowned }
					}
					AND = {
						succ_law_title = {
							NOR = {
								has_law = enatic_succession
								has_law = enatic_cognatic_succession
								has_law = true_cognatic_succession
							}
						}
						holder_scope = { is_female = yes }
					}
					AND = {
						succ_law_title = {
							OR = {
								has_law = enatic_succession
								has_law = enatic_cognatic_succession
							}
						}
						holder_scope = { is_female = no }
					}
					AND = {
						ROOT = {
							is_female = yes
							culture = basque
						}
						OR = {
							de_jure_liege_or_above = k_navarra
							title = k_navarra
						}
					}
				}
			}
		}
		#only cynics or adventurers can press claims on same-faith holy orders unless one is a player
		OR = {
			ROOT = { has_character_flag = claimant_adventurer }
			FROM = { NOT = { religion = ROOT } }
			ROOT = { trait = cynical }
			ROOT = { ai = no }
			FROM = { ai = no }
			FROM = { higher_tier_than = duke }
			FROM = { holy_order = no }
		}
		#AI will not fight for baronies held by a patriarch
		can_attack_patriarch_trigger = yes
	}

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

	on_add = {
		FROM = {
			attacked_tributary_cb_effect = yes
		}
	}	
	
	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their Claim war on the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		#decentralize target realm
		title_reduce_CA_effect = yes
		title_reduce_TO_effect = yes
		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
					}
				}
			}
		}
		empty_province_effect = yes
		pf_liege_change_under_title_begin_effect = yes
		usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
		pf_liege_change_under_title_end_effect = yes
		if = {
			limit = {
				higher_tier_than = BARON
				NOT = { culture = ROOT }
			}
			hidden_tooltip = { conquest_culture = ROOT }
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		FROM = {
			prestige = -100
			pf_glory_minus2_effect = yes
		}
	}

	on_fail_title = {
		ROOT = {
			prestige = -100
			hidden_tooltip = {
				if = {
					limit = { has_character_flag = claimant_adventurer }
					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 = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			pf_glory_minus2_effect = yes
		}
		if = {
			limit = {
				ROOT = { has_character_flag = claimant_adventurer }
			}
			ROOT = { imprison = FROM }
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
	}

	on_reverse_demand_title = {
		remove_claim = ROOT
		ROOT = {
			hidden_tooltip = {
				if = {
					limit = { has_character_flag = claimant_adventurer }
					disband_event_forces = yes
					clr_character_flag = claimant_adventurer
				}
			}
		}
	}
	
	on_attacker_leader_death = {
		if = {
			limit = { ROOT = { has_character_flag = EoD_prophet_attacker } }
			FROM = { narrative_event = { id = RIP.15116 } }
		}
		if = {
			limit = {
				ROOT = { 
					OR = {
						has_character_flag = claimant_adventurer
						has_character_flag = EoD_prophet_attacker
					}
				}
			}
			ROOT = {
				hidden_tooltip = {
					disband_event_forces = claimant_adventurer
					clr_character_flag = claimant_adventurer
					clr_character_flag = EoD_prophet_attacker
				}
			}
			end_war = invalid
		}
	}
	
	on_invalidation = {
		log = "CB(claim): INVALIDATED: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
	}

	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 = 1.2
			ROOT = {
				realm_levy_diff = { who = FROM value = 5000 }
			}
		}
		modifier = {
			factor = 0
			is_contested = yes
		}
		modifier = { # The AI will never breach a tributary's trust
			factor = 0
			ai_attack_tributary_trigger = yes
		}
	}
}

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 = yes

	allowed_to_target_tributaries = yes
	allowed_to_target_suzerains = no
	
	infamy_modifier = 0.5

	press_claim = yes

	can_use = {
		ai_not_busy_trigger = yes
		attacking_religious_head_trigger = no
		NOT = { has_character_modifier = holy_truce_timer }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		ROOT = { mercenary = no }
	}

	can_use_title = {
		#the AI won't press claims on behalf of characters of a different religion group unless the title's duke+
		OR = {
			FROMFROM = { ai = no }
			higher_tier_than = count
			ROOT = { religion_group = FROMFROM }
		}
		#you can't press claims against someone you installed into that title
		holder_scope = {
			NOT = {
				has_opinion_modifier = {
					who = FROMFROM
					modifier = pressed_my_claim
				}
			}
		}
		ROOT = {
			OR = {
				is_ruler = no
				primary_title = {
					OR = {
						is_primary_type_title = no # Static Mercs, the Pope, Holy Orders, etc
						higher_tier_than = PREVPREV
						tier = PREVPREV
					}
				}
			}
		}
		#the AI will only press a non-muslim woman's claim on an agnatic title if she already has a son or is formidable
		OR = {
			FROMFROM = { ai = no }
			FROMFROM = { is_feminist_trigger = yes }
			NOT = { has_law = agnatic_succession }
			ROOT = { is_female = no }
			ROOT = {
				is_female = yes
				OR = {
					NOT = { religion_group = muslim }
					religion = druze
				}
				NOT = { age = 40 }
				OR = {
					any_child = {
						is_female = no
						OR = {
							dynasty = PREV
							dynasty = FROMFROM
						}
						NOT = { trait = bastard }
					}
					calc_true_if = {
						amount = 2
						diplomacy = 12
						diplomacy = 20
						intrigue = 12
						prestige = 1000
						is_ruler = yes
					}
				}
			}
		}
		
		#disallow ai females from pressing claims on agnatic nomadic titles
		OR = {
			FROMFROM = { ai = no }
			FROM = { is_nomadic = no }
			ROOT = { is_female = no }
			NOT = { has_law = agnatic_succession }
		}
		
		#cannot enforce claims for non-ruler muslim women, period
		ROOT = {
			OR = {
				is_female = no
				is_ruler = yes
				NOT = { religion_group = muslim }
				religion = druze
			}
		}
		
		#presser is DJL of target title, or presser is a liege of claimant and is either a player or has a higher tier title than target, or is a friend/spouse
		OR = {
			de_jure_liege_or_above = FROMFROM
			ROOT = {
				any_liege = {
					character = FROMFROM
					ai = no
				}
			}
			ROOT = {
				any_liege = {
					character = FROMFROM
					primary_title = { higher_tier_than = PREVPREVPREV }
				}
			}
			ROOT = {
				any_liege = {
					character = FROMFROM
					OR = {
						is_friend = PREV
						is_married = PREV
					}
				}
			}
		}
		#presser is DJL of target title, is player liege of claimant, or the claimant is an antipope, already has a higher tier title, or is a dynasty member/friend/lover/spouse
		OR = {
			de_jure_liege_or_above = FROMFROM
			AND = {
				controls_religion = yes
				holder_scope = { 
					OR = {
						religion = catholic
						religion = fraticelli
					}
				}
			}
			ROOT = {
				any_liege = {
					character = FROMFROM
					ai = no
				}
			}
			ROOT = { primary_title = { higher_tier_than = PREVPREV } }			
			ROOT = {
				OR = {
					any_dynasty_member = { character = FROMFROM }
					any_lover = { character = FROMFROM }
					any_spouse = { character = FROMFROM }
					any_friend = { character = FROMFROM }
				}
			}
		}
		#the target either has the title or is liege of the title's holder
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
		#can not press a claim for someone who's already attacking the target for the title
		NOT = {
			any_war = { # when used in title scope, checks all wars for that title
				attacker = {
					character = ROOT
				}
			}
		}
		#can only press weak claims for a pretender, a contested title, or if the target is uncrowned/has a regent/is female
		is_usable_claim_trigger = yes
		
		#only cynics can press claims against holy orders of their religion unless they have other kingdoms.
		OR = {
			FROM = { NOT = { religion = FROMFROM } }
			FROMFROM = { trait = cynical }
			FROMFROM = { ai = no }
			FROM = { ai = no }
			FROM = { higher_tier_than = duke }
			FROM = { holy_order = no }
		}
		#AI will not fight for baronies held by a patriarch
		OR = {
			FROMFROM = { ai = no }
			ROOT = { NOT = { religion = FROM } }
			NOR = {
				title = b_roma
				title = b_hagiasophia
				title = b_alexandria
				title = b_antiocheia
				title = b_jerusalem
			}
			ROOT = {
				higher_real_tier_than = DUKE
				NOT = { same_realm = FROM }
			}
			AND = {
				title = b_roma
				FROM = {
					NOT = { has_landed_title = d_pentarch_rome }
				}
			}
			AND = {
				title = b_hagiasophia
				FROM = {
					NOR = {
						has_landed_title = d_pentarch_constantinople
						has_landed_title = d_latin_constantinople
					}
				}
			}
			AND = {
				title = b_alexandria
				FROM = {
					NOR = {
						has_landed_title = d_pentarch_alexandria
						has_landed_title = d_latin_alexandria
					}
				}
			}
			AND = {
				title = b_antiocheia
				FROM = {
					NOR = {
						has_landed_title = d_pentarch_antioch
						has_landed_title = d_latin_antioch
					}
				}
			}
			AND = {
				title = b_jerusalem
				FROM = {
					NOR = {
						has_landed_title = d_pentarch_jerusalem
						has_landed_title = d_latin_jerusalem
					}
				}
			}
		}
	}

	is_valid_title = {
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
		ROOT = { has_claim = PREV }
	}
	
	on_add = {
		FROM = {
			attacked_tributary_cb_effect = yes
		}
		ROOT = {
			set_character_flag = claim_being_pressed
		}
	}	

	on_success = {
		hidden_tooltip = {
			ROOT = { add_character_modifier = { name = new_land_timer duration = 1 hidden = yes } }
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			ROOT = {
				opinion = {
					modifier = pressed_my_claim 
					who = PREV
					multiplier = 2
				}
				hidden_tooltip = {
					clr_character_flag = claim_being_pressed
				}
			}
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		FROM = {
			prestige = -200
			pf_glory_minus2_effect = yes
		}
	}
	
	on_fail = {
		ROOT = {
			hidden_tooltip = {
				clr_character_flag = claim_being_pressed
			}
		}
	}
	
	on_invalidation = {
		ROOT = {
			hidden_tooltip = {
				clr_character_flag = claim_being_pressed
			}
		}
	}

	on_success_title = {
		log = "[FromFrom.GetBestName] (ID: [FromFrom.GetID]) succeeded with their war for [Root.GetBestName]'s (ID: [Root.GetID]) claim on the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		#decentralize target title
		title_reduce_CA_effect = yes
		title_reduce_TO_effect = yes
		pf_liege_change_under_title_begin_effect = yes
		hidden_tooltip = {
			ROOT = {
				if = {
					limit = {
						has_nickname = no
						lower_tier_than = PREV
					}
					random = {
						chance = 50
						give_nickname = nick_the_usurper
					}
				}
			}
			# Anti-Popes take all the gold of the Vatican
			if = {
				limit = {
					OR = {
						title = k_papal_state
						title = d_fraticelli
					}
				}
				FROM = {
					transfer_scaled_wealth = {
						to = ROOT
						value = all
					}
				}
			}
		}
		if = {
			#antipopes
			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 
							}
						}
					}
				}
			}

			# check if we qualify for staying in the realm & inform player of result with a tooltip
			if = {
				limit = {
					OR = {
						de_jure_liege_or_above = FROMFROM						
						ROOT = {
							any_liege = {
								character = FROMFROM
								primary_title = { higher_tier_than = PREVPREVPREV }
							}
						}						
					}
					OR = {
						de_jure_liege_or_above = FROMFROM
						ROOT = { primary_title = { higher_tier_than = PREVPREV } }
						ROOT = { primary_title = { tier = PREVPREV } }
						ROOT = {
							any_dynasty_member = {
								character = FROMFROM
							}
						}
					}
				}
				# mark as staying in the realm
				ROOT = { set_character_flag = stay_in_realm }
				custom_tooltip = { text = CUSTOMTOOLTIP1 }
			}
			if = {
				limit = {
					OR = {
						NOT = {
							OR = {
								de_jure_liege_or_above = FROMFROM
								ROOT = {
									any_liege = {
										character = FROMFROM
										primary_title = { higher_tier_than = PREVPREVPREV }
									}
								}
							}
						}
						NOR = {
							de_jure_liege_or_above = FROMFROM
							ROOT = { primary_title = { higher_tier_than = PREVPREV } }
							ROOT = { primary_title = { tier = PREVPREV } }
							ROOT = {
								any_dynasty_member = {
									character = FROMFROM
								}
							}
						}
					}
				}
				custom_tooltip = { text = CUSTOMTOOLTIP2 }
			}

			#determine who is the liege of loser in the war
			if = {
				limit = {
					ROOT = {
						NOT = { has_character_flag = stay_in_realm }
					}
					FROM = { independent = no } 
				}
				FROM = {
					liege = { set_character_flag = from_liege }
				}
				set_global_flag = from_has_liege
			}

			empty_province_effect = yes
			usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }

			# try and vassalise
			hidden_tooltip = {
				# claimant stays in realm
				if = {
					limit = {
						ROOT = { has_character_flag = stay_in_realm }
						NOT = { 
						 	ROOT = { is_liege_or_above = FROMFROM }
						}
					}
					ROOT = { set_defacto_liege = FROMFROM }
				}
				#claimant leaves realm - if loser was vassal, vassalize to their liege. If loser was independent, claimant goes independent.
				if = {
					limit = {
						has_global_flag = from_has_liege
						ROOT = {
							NOT = { has_character_flag = stay_in_realm }
						}
					}
					any_playable_ruler = {
						limit = { has_character_flag = from_liege }
						ROOT = { set_defacto_liege = PREV }
						clr_character_flag = from_liege
					}
				}
				if = {
					limit = {
						NOT = { has_global_flag = from_has_liege } 
						ROOT = {
							NOT = { has_character_flag = stay_in_realm }
						}
					}
					ROOT = { set_defacto_liege = ROOT }
				}
				clr_global_flag = from_has_liege
				ROOT = { clr_character_flag = stay_in_realm }
			}
		}
	
		pf_liege_change_under_title_end_effect = yes
		
		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 = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		prestige = -200
		transfer_scaled_wealth = {
			to = FROM
			value = 4.0
		}
		pf_glory_minus2_effect = yes
		ROOT = {
			prestige = -200
			imprison = FROM
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
	}

	on_reverse_demand_title = {
		remove_claim = ROOT
	}

	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 = 10
			controls_religion = yes
		}
		modifier = {
			factor = 0
			is_contested = yes
		}
		modifier = { # The AI will never breach a tributary's trust
			factor = 0
			ai_attack_tributary_trigger = yes
		}
	}
}

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

	can_use = {
		ROOT = {
			mercenary = no
		}
	}

	can_use_title = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		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 = {
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		FROM = {
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}

		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 300
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 300 }
		}
		ROOT = {
			if = {
				limit = {
					has_nickname = no
				}
				random_list = {
					10 = { give_nickname = nick_the_great }
					10 = { give_nickname = nick_the_victorious }
					10 = { give_nickname = nick_the_bold }
					10 = { give_nickname = nick_the_conqueror }
				}
			}
			if = {
				limit = {
					has_nickname = nick_the_bastard
				}
				give_nickname = nick_the_conqueror
			}
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their Invasion CB of [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		any_de_jure_vassal_title = {
			limit = {
				NOT = { holder = FROM }
				tier = COUNT
				location = { controlled_by = ROOT }
			}
			add_claim = ROOT
			duchy = {
				if = {
					limit = {
						has_holder = yes
						NOT = { holder = FROM }
					}
					add_claim = ROOT
				}
			}
		}
		empty_province_effect = yes
		any_de_jure_vassal_title = {
			limit = {
				holder = FROM
				higher_tier_than = BARON
			}
			usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
		}
		hidden_tooltip = {
			ROOT = {
				if = {
					limit = {
						has_dlc = "Legacy of Rome"
						OR = { 
							has_landed_title = e_byzantium
							has_landed_title = e_roman_empire
							AND = {
								independent = yes
								higher_tier_than = COUNT
								OR = {
									culture = roman
									religion = hellenic_pagan
								}
							}
						}
						OR = {
							religion_group = christian
							religion_group = pagan_group
						}
					}
					character_event = { id = LoR.30 days = 12 }
				}
			}
		}
		usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
		if = {
			limit = {
				higher_tier_than = BARON
				NOT = { culture = ROOT }
			}
			hidden_tooltip = { conquest_culture = ROOT }
		}
	}

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

	on_reverse_demand = {
		FROM = {
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -300
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 300
		}
		FROM = {
			if = {
				limit = { 
					has_dlc = "Legacy of Rome"
					OR = { 
						has_landed_title = e_byzantium
						has_landed_title = e_roman_empire

						AND = {
							independent = yes
							higher_tier_than = COUNT
							OR = {
								culture = roman
								religion = hellenic_pagan
							}
						}
					}
					OR = {
						religion_group = christian
						religion_group = pagan_group
					}
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 300 }
		}
	}

	on_reverse_demand_title = {
		remove_claim = ROOT
	}

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

		end_war = whitepeace
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	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_TRIBAL_INVASION
	war_name = WAR_NAME_TRIBAL_INVASION
	sprite = 8
	truce_days = 3650
	hostile_against_others = yes
	is_permanent = yes
	can_ask_to_join_war = yes
	check_de_jure_tier = KING # this scans all de jure kingdoms for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no

	can_use_gui = {
		ROOT = {
			prestige = 500
		}
	}
	
	on_add = {
		ROOT = { prestige = -500 }
	}	
	
	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		ROOT = {
			independent = yes
			is_nomadic = no
			NOT = { is_liege_or_above = FROM }
			OR = {
				AND = {
					has_dlc = "Horse Lords"
					higher_tier_than = COUNT
					OR = {
						has_character_flag = horde_invader
						AND = {
							independent = yes
							OR = {
								religion_group = pagan_group
								AND = {
									NOT = { is_title_active = d_aztec }
									religion = aztec
								}
							}
							is_reformed_religion = no
							OR = {
								culture_group = altaic
								culture_group = magyar
								culture_group = mesoamerican
							}
							FROM = {
								NOT = { religion_group = pagan_group } # Should subjugate these instead
							}
						}
					}
				}
				AND = {
					NOT = { has_dlc = "Horse Lords" }
					independent = yes
					higher_tier_than = COUNT
					OR = {
						AND = {
							religion_group = pagan_group
							is_reformed_religion = no
						}
						AND = {
							religion = aztec
							NOT = { is_title_active = d_aztec }
						}
						AND = {
							OR = {
								has_horde_culture = yes
								has_character_flag = horde_invader
							}
							NOT = { religion_group = christian }
						}
					}
					OR = {
						culture_group = altaic
						culture_group = magyar
						culture_group = mesoamerican
					}
					OR = {
						has_horde_culture = yes
						has_character_flag = horde_invader
						FROM = {
							NOT = { religion_group = pagan_group } # Should subjugate these instead
						}
					}
				}
				AND = {
					mercenary = yes
					NOT = { num_of_titles = 2 }
				}
			}
		}
		ROOT = {
			NOT = { same_realm = FROM }
		}
	}

	can_use_title = {
		# The attacker needs at least one county in the target kingdom, or a border
		ROOT = {
			any_realm_province = {
				OR = {
					county = { de_jure_liege_or_above  = PREVPREVPREV }
					any_neighbor_province = {
						county = { de_jure_liege_or_above  = PREVPREVPREVPREV }
					}
				}
			}
		}
	}
	
	is_valid_title = {
		FROM = {
			any_realm_title = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}

	on_success = {
		ROOT = {
			prestige = 300
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 300
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 300 }
		}
		FROM = {
			prestige = -300
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}
		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 = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their Tribal Invasion CB of the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		empty_province_effect = yes
		custom_tooltip = {
			text = tribal_invasion_succ_tip
			hidden_tooltip = {
				pf_liege_change_under_title_begin_effect = yes
				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
					}
				}
				pf_liege_change_under_title_end_effect = yes
				if = {
					limit = {
						e_mexikha = {
							holder = ROOT
						}
					}
					ROOT = {
						any_realm_province = {
							limit = {
								OR = {
									has_province_flag = aztec_explorers
									has_province_flag = aztec_second_wave
								}
							}
							culture = nahuatl
							religion = aztec
							clr_province_flag = aztec_explorers
							clr_province_flag = aztec_second_wave
						}
					}
				}
			}
		}
	}

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

	on_reverse_demand = {
		FROM = {
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -300
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}
		FROM = {
			prestige = 300
			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 = 300
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 300 }
		}
		FROM = {
			if = {
				limit = { 
					has_dlc = "Legacy of Rome"
					OR = { 
						has_landed_title = e_byzantium
						has_landed_title = e_roman_empire
						AND = {
							independent = yes
							higher_tier_than = COUNT
							OR = {
								culture = roman
								religion = hellenic_pagan
							}
						}
					}
					OR = {
						religion_group = christian
						religion_group = pagan_group
					}
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}
	
	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 = {
				capital_scope = {
					OR = {
						region = custom_bulgaria
						region = custom_croatia
						region = custom_serbia
						region = custom_wallachia				
						region = custom_crimea
						region = custom_cuman
						region = custom_perm
						region = custom_volga
						region = custom_rus
						region = world_india_bengal
						region = world_india_deccan
						region = world_india_rajastan						
					}
				}
				FROM = {
					primary_title = { title = e_golden_horde }
					NOT = { year = 1300 }
				}
				FROM = {
					primary_title = { title = e_chagatai }
					NOT = { year = 1300 }
				}
			}
		}
		modifier = {
			factor = 2.0
			ROOT = {
				OR = {
					dynasty = 613 # Seljuks
					dynasty = 800 # Timurids
					primary_title = { title = e_il-khanate }
				}
			}
			capital_scope = { region = custom_persia }
		}
		modifier = {
			factor = 2.0
			ROOT = { primary_title = { title = e_golden_horde } }
			capital_scope = {
				OR = {
					region = custom_cuman
					region = custom_perm
					region = custom_volga
				}
			}
		}
		modifier = {
			factor = 1.5
			ROOT = {
				primary_title = { title = e_mongol_empire }
				NOT = { any_realm_province = { region = custom_cuman } }
			}
			capital_scope = { region = custom_cuman }
		}
		modifier = {
			factor = 2.0
			ROOT = {
				primary_title = { title = e_mongol_empire }
				NOT = { any_realm_province = { region = custom_persia } }
			}
			capital_scope = { region = custom_persia }
		}
		modifier = {
			factor = 2.0
			ROOT = { primary_title = { title = e_mongol_empire } }
			capital_scope = { region = custom_khwarizm }
		}
		modifier = {
			factor = 0.1
			ROOT = {
				primary_title = { title = e_mongol_empire }
				any_realm_province = { region = custom_cuman }
				any_realm_province = { region = custom_persia }
				any_realm_province = { region = custom_khwarizm }
			}
			capital_scope = {
				NOR = {
					region = custom_persia
					region = custom_khwarizm
					region = custom_afghanistan
					region = custom_baluchistan
					region = custom_cuman
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = { primary_title = { title = e_golden_horde } }
			OR = {
				capital_scope = {
					OR = {
						region = custom_persia
						region = custom_baluchistan
						region = custom_afghanistan
						region = custom_mesopotamia
						region = custom_azerbaijan
						region = custom_armenia
						region = custom_trebizond
						region = custom_nikaea
						region = custom_byzantium
						region = custom_norway
						region = custom_sweden
						region = custom_syria
						region = custom_arabia
						region = custom_jerusalem
						region = custom_egypt
						region = custom_africa
						region = world_india_bengal
						region = world_india_deccan
						region = world_india_rajastan
					}
				}
				FROM = {
					primary_title = { title = e_il-khanate }
					NOT = { year = 1300 }
				}
				FROM = {
					primary_title = { title = e_chagatai }
					NOT = { year = 1300 }
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = {
				primary_title = { title = e_chagatai }
			}
			OR = {
				capital_scope = {
					NOR = {
						region = custom_baluchistan
						region = custom_afghanistan
						region = world_india_bengal
						region = world_india_deccan
						region = world_india_rajastan
					}
				}
				FROM = {
					primary_title = { title = e_il-khanate }
					NOT = { year = 1300 }
				}
				FROM = {
					primary_title = { title = e_chagatai }
					NOT = { year = 1300 }
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = { culture = hungarian }
			capital_scope = {
				OR = {
					region = custom_cuman
					region = custom_volga
					region = custom_alania
					region = custom_crimea
					region = custom_novgorod
					region = custom_muscovy
					region = custom_rus
					region = custom_byzantium
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = { culture = khazar }
			capital_scope = {
				OR = {
					region = world_steppe_tarim
					region = world_steppe_east
					region = custom_cuman
					region = custom_perm
					region = custom_volga
				}
			}
		}
		modifier = {
			factor = 1.5
			ROOT = { culture = hungarian }
			capital_scope = {
				OR = {
					region = custom_bulgaria
					region = custom_carpathia
					region = custom_croatia
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = {
				OR = {
					primary_title = { title = e_mongol_empire }
					primary_title = { title = e_chagatai }
					primary_title = { title = e_golden_horde }
					primary_title = { title = e_il-khanate }
				}
				culture = FROM
			}
		}
		modifier = {
			factor = 0
			ROOT = {
				culture = mongol
				religion_group = pagan_group
			}
			FROM = {
				culture = mongol
				religion_group = pagan_group
			}
		}
		modifier = {
			factor = 0
			ROOT = { has_character_flag = no_invasions }
		}
	}
}

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
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no
	
	can_use_gui = {
		OR = {
			AND = {
				NOT = { has_global_flag = delayed_crusades_1 }
				NOT = { has_global_flag = delayed_crusades_2 }
				NOT = { has_global_flag = delayed_crusades_3 }
				NOT = { has_global_flag = delayed_crusades_4 }
			}
			had_global_flag = { flag = delayed_crusades_1 days = 365 }
			had_global_flag = { flag = delayed_crusades_2 days = 1095 }
			had_global_flag = { flag = delayed_crusades_3 days = 1825 }
			had_global_flag = { flag = delayed_crusades_4 days = 3650 }
		}
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { 
			NOR = { 
				has_character_modifier = holy_truce_timer
				has_character_modifier = crusade_block_timer 
			} 
		}		
		ROOT = { # The Religious Head
			has_called_crusade = no
			religion_authority = 0.4
			NOT = { 
				has_character_modifier = crusade_timer 
				AND = {
					trait = bad_priest_christian
					NOT = { has_global_flag = fourth_crusade_happened }
				}
			}
			OR = {
				religion_group = zoroastrian_group
				religion_group = jewish_group
				AND = {
					religion_group = pagan_group
					custom_tooltip = {
						text = CTT_PAGAN_GHWS_UNLOCKED
						has_global_flag = pagan_ghws_unlocked
					}
				}
				AND = { 
					religion_group = christian
					custom_tooltip = {
						text = CTT_CHRISTIAN_CRUSADES_UNLOCKED
						has_global_flag = christian_crusades_unlocked
					}
				}
				AND = {
					religion_group = muslim
					custom_tooltip = {
						text = CTT_MUSLIM_JIHADS_UNLOCKED
						has_global_flag = muslim_jihads_unlocked
					}
				}
			}
			mercenary = no
		}
	}

	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }	
		ROOT = {
			can_call_crusade = yes
			any_demesne_title = { NOT = { controls_religion = yes } }
			OR = {
				NOT = { religion = cathar }
				has_global_flag = cathar_war
			}
		}
		
		FROM = {
			OR = {
				NOT = { religion_group = ROOT }
				is_heresy_of = ROOT
				is_parent_religion = ROOT
				AND = {
					religion_group = pagan_group
					NOT = { religion = ROOT }
				}
				AND = {
					ROOT = { religion = paulician }
					NOT = { religion = ROOT }
				}
				AND = {
					ROOT = { religion = kharijite }
					NOT = { religion = ROOT }
				}
				AND = {
					ROOT = {
						OR = {
							religion = catholic
							religion = cathar
							religion = fraticelli
							religion = waldensian
							religion = lollard
							religion = adoptionist
						}
						is_heretic = no
					}				
					has_global_flag = fourth_crusade_ongoing 
				}
			}
		}
	}

	can_use_title = {
		FROM = {
			any_realm_title = {
				de_jure_liege_or_above = PREVPREV
			}
			
			independent = yes
			in_revolt = no # Don't declare Crusades on revolters - chances are high the war will invalidate
			
			num_of_realm_counties = {
				value = 6
				title = PREV
			}
			#target needs to either hold the target title,
			#be the title's overlord,
			#or the title needs to have no holder/holder of ROOT's religious group
			OR = {
				has_landed_title = PREV
				any_vassal = { has_landed_title = PREVPREV }
				PREV = { has_holder = no }
				PREV = { holder_scope = { religion = ROOT } }				
			}
		}
		
		OR = {
			# religion head is a player
			ROOT = { ai = no }
			# the target owns a holy site within that kingdom title
			FROM = {
				any_realm_title = {
					de_jure_liege_or_above = PREVPREV
					is_holy_site = ROOT
				}
			}
			# the target owns a province of the religion within that kingdom title
			FROM = {
				any_realm_title = {
					tier = COUNT
					de_jure_liege_or_above = PREVPREV
					location = { religion = ROOT }
				}
			}
			# a ruler of the religion either owns a province in that kingdom or is neighboring
			any_direct_de_jure_vassal_title = {
				any_direct_de_jure_vassal_title = {
					location = {
						OR = {
							owner = { religion = ROOT }
							any_neighbor_province = { owner = { religion = ROOT } }
						}
					}
				}
			}
		}
	}

	on_add = {
		ROOT = {
			add_character_modifier = {
				name = "crusade_timer"
				duration = 14600
				hidden = no
				inherit = yes
			}
		}
	}
	
	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 }
				}
				AND = {
					ROOT = { religion = paulician }
					NOT = { religion = ROOT }
				}
				AND = {
					ROOT = { religion = kharijite }
					NOT = { religion = ROOT }
				}
				AND = {
					ROOT = {
						OR = {
							religion = catholic
							religion = cathar
							religion = fraticelli
							religion = waldensian
							religion = lollard
							religion = adoptionist
						}
						is_heretic = no
					}				
					has_global_flag = fourth_crusade_ongoing 
				}
			}
		}
	}

	is_valid_title = {
		FROM = {
			any_realm_title = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their crusade for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		#change imperial decadence
		ROOT = { imperial_decadence_minus_2_effect = yes }
		FROM = { imperial_decadence_plus_2_effect = yes }
		
		hidden_tooltip = {
			any_attacker = {
				add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			}
			ROOT = {
				if = {
					limit = {
						OR = {
							religion = catholic
							religion = cathar
							religion = fraticelli
							religion = waldensian
							religion = lollard
							religion = adoptionist
						}
						is_heretic = no
						OR = {
							has_global_flag = catholic_crusade_failed
							has_global_flag = fourth_crusade_ongoing
						}
					}
					clr_global_flag = catholic_crusade_failed
					clr_global_flag = fourth_crusade_ongoing
				}
			}
		}
		
		pf_liege_change_under_title_begin_effect = yes
		
		#first give the target kingdom to its current holder, if the holder is the same religion
		if = {
			limit = {
				has_holder = yes
				holder_scope = {
					religion = ROOT
					NOT = { same_realm = FROM }
				}
			}
			holder_scope = {
				add_character_modifier = { name = new_land_timer duration = 1 hidden = yes }
				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
				}
			}
		}
		#then give the target kingdom to its de jure liege, if the holder is the same religion
		if = {
			limit = {
				OR = {
					has_holder = no
					holder_scope = { NOT = { religion = ROOT } }
					holder_scope = { same_realm = FROM }
				}
				dejure_liege_title = {
					has_holder = yes
					holder_scope = {
						religion = ROOT
						NOT = { same_realm = FROM }
					}
				}
			}
			dejure_liege_title = {
				holder_scope = {
					add_character_modifier = { name = new_land_timer duration = 1 hidden = yes }
					gain_settlements_under_title = {
						title = PREVPREV
						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
					}
				}
			}
		}
		#then give the target kingdom to the most participating attacker
		if = {
			limit = {
				OR = {
					has_holder = no
					holder_scope = { NOT = { religion = ROOT } }
					holder_scope = { same_realm = FROM }
				}
				NOT = {
					dejure_liege_title = {
						has_holder = yes
						holder_scope = {
							religion = ROOT
							NOT = { same_realm = FROM }
						}
					}
				}
			}
			hidden_tooltip = { 
				most_participating_crusader = {
					set_character_flag = crusade_victor
				}
				#other crusaders will request fiefs
				any_attacker = {
					limit = {
						NOT = { has_character_flag = crusade_victor }
						is_feudal = yes
						higher_tier_than = BARON
						demesne_size = 1
						NOT = { religion_group = muslim }
					}
					character_event = { id = Plus.401 }
				}
				#add a strong claim to the best_crusade_claimant if not also the most_participating_attacker
				if = {
					limit = {
						best_crusade_claimant = {
							NOT = { has_character_flag = crusade_victor }
							is_alive = yes
						}
					}
					best_crusade_claimant = { PREV = { add_pressed_claim = PREV } }
				}
				most_participating_crusader = {
					clr_character_flag = crusade_victor
				}
			}
			most_participating_crusader = {
				# Clear original flags first
 				any_landed_title = { clr_title_flag = previous_holding }
 				any_character = { clr_character_flag = previous_vassal }
 
				any_demesne_title = { set_title_flag = previous_holding }
				any_vassal = { set_character_flag = previous_vassal }
				primary_title = { save_event_target_as = current_title }
				PREV = {
					any_de_jure_vassal_title = {
						limit = {
							tier = COUNT
							holder_scope = { same_realm = FROM }
							location = {
								NOT = { any_province_holding = { NOT = { holding_type = nomad } } }
								NOT = { culture = PREVPREVPREV }
							}
						}
						location = {
							build_holding = { type = tribal }
							any_province_holding = {
								limit = { NOT = { holding_type = nomad } }
								make_capital_holding = yes
							}
						}
					}
				}
				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
				}
				#who to give crusade gains to?
				hidden_tooltip = {
					character_event = { id = Plus.400 }
				}
				if = {
					limit = { NOT = { culture = PREV } }
					hidden_tooltip = { 
						save_event_target_as = winning_crusader
						PREV = { conquest_culture = event_target:winning_crusader } 
					}
				}
				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
							}
						}
					}
				}
			}
		}

		pf_liege_change_under_title_end_effect = yes
		
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_piety = 1000
			participation_scaled_prestige = 500
			dynasty_head = {
				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -100
				}
			}
			#faction mood boost
			pf_tradition_plus4_effect = yes
			pf_glory_plus2_effect = yes
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_piety = 1000
				participation_scaled_prestige = 500
				dynasty_head = {
					if = {
						limit = { uses_decadence = yes }
						participation_scaled_decadence = -100
					}
				}
				pf_tradition_plus4_effect = yes
				pf_glory_plus2_effect = yes
			}
		}
		any_attacker = {
			limit = {
				ai = no
				NOT = { has_character_flag = achievement_crusader }
			}
			set_character_flag = achievement_crusader
		}		

		FROM = {
			prestige = -500
			pf_glory_minus2_effect = yes
			pf_tradition_minus4_effect = yes
		}
		hidden_tooltip = {
			any_defender = {
				limit = { NOT = { character = FROM } }
				prestige = -250
			}
		}
		if = {
			limit = {
				ROOT = { is_heretic = no }
			}
			ROOT = {
				religion_authority = {
					modifier = won_crusade
					years = 20
				}
			}
		}
		if = {
			limit = {
				FROM = { is_heretic = no }
			}
			FROM = {
				religion_authority = {
					modifier = lost_crusade
					years = 20
				}
			}
		}
		#maintenance - shatter FROM empire if it's become too small (NYR)
		hidden_tooltip = { FROM = { character_event = { id = Plus.303 } } }
	}

	on_fail = {
		hidden_tooltip = {
			if = {
				limit = {
					ROOT = { 
						OR = {
							religion = chalcedonian
							religion = catholic
							religion = cathar
							religion = fraticelli
							religion = waldensian
							religion = lollard
							religion = adoptionist
						}
						is_heretic = no
					}
				}
				set_global_flag = catholic_crusade_failed
			}
			if = {
				limit = {
					ROOT = {
						OR = {
							religion = catholic
							religion = cathar
							religion = fraticelli
							religion = waldensian
							religion = lollard
							religion = adoptionist
						}
						is_heretic = no
					}				
					has_global_flag = fourth_crusade_ongoing 
				}
				clr_global_flag = fourth_crusade_ongoing
			}			
		}
		ROOT = {
			prestige = -200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			prestige = -100
		}
		FROM = {
			participation_scaled_prestige = 100
			participation_scaled_piety = 200
			dynasty_head = {
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -50
				}
			}
			pf_tradition_minus4_effect = yes
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_prestige = 100 
				participation_scaled_piety = 200 
				dynasty_head = {
					if = {
						limit = {
							uses_decadence = yes
						}
						participation_scaled_decadence = -50
					}
				}
			}
			pf_tradition_minus2_effect = yes
		}
	}

	on_reverse_demand = {
		#change imperial decadence
		FROM = { imperial_decadence_minus_2_effect = yes }
		ROOT = { imperial_decadence_plus_2_effect = yes }

		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			pf_tradition_plus4_effect = yes
		}
		hidden_tooltip = {
			if = {
				limit = {
					ROOT = {
						OR = {
							religion = chalcedonian
							religion = catholic
							religion = cathar
							religion = fraticelli
							religion = waldensian
							religion = lollard
							religion = adoptionist
						}
						is_heretic = no
					}
				}
				set_global_flag = catholic_crusade_failed
			}
			if = {
				limit = {
					ROOT = {
						OR = {
							religion = catholic
							religion = cathar
							religion = fraticelli
							religion = waldensian
							religion = lollard
							religion = adoptionist
						}
						is_heretic = no
					}				
					has_global_flag = fourth_crusade_ongoing 
				}
				clr_global_flag = fourth_crusade_ongoing
			}			
		}
		ROOT = {
			prestige = -500
			pf_glory_minus2_effect = yes
			pf_tradition_minus4_effect = yes
		}
		FROM = {
			participation_scaled_prestige = 1000
			participation_scaled_piety = 500
			dynasty_head = {
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -100
				}
			}
			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
						}
					}
				}
			}
		}
		hidden_tooltip = {
			any_attacker = {
				limit = { NOT = { character = ROOT } }
				prestige = -250
			}
			any_defender = {
				limit = { NOT = { character = FROM } }
				hidden_tooltip = {
					pf_glory_plus2_effect = yes
					pf_tradition_plus4_effect = yes
					participation_scaled_prestige = 1000 
					participation_scaled_piety = 500 
					dynasty_head = {
						if = {
							limit = {
								uses_decadence = yes
							}
							participation_scaled_decadence = -100
						}
					}
				}
			}
		}
		if = {
			limit = {
				FROM = { is_heretic = no }
			}
			FROM = {
				religion_authority = {
					modifier = won_crusade
					years = 20
				}
			}
		}
		if = {
			limit = {
				ROOT = { is_heretic = no }
			}
			ROOT = {
				religion_authority = {
					modifier = lost_crusade
					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.1
			ROOT = {
				religion_group = muslim
				NOT = { religion = kharijite }
			}
			FROM = { religion = ibadi }
		}
		modifier = {
			factor = 0.1
			FROM = {
				religion_group = muslim
				NOT = { religion = kharijite }
			}
			ROOT = { religion = ibadi }
		}
		modifier = {
			factor = 0
			ROOT = {
				religion = ibadi
				NOT = { trait = zealous }
			}
		}
	}
}

#Holy War (renamed from vanilla "religious" CB, consolidated vanilla "religious" and "pagan_holy_war" CBs)
holy_war = {
	name = CB_NAME_RELIGIOUS
	war_name = WAR_NAME_RELIGIOUS
	sprite = 9
	truce_days = 3650
	is_permanent = yes
	can_ask_to_join_war = yes
	is_holy_war = yes
	check_de_jure_tier = DUKE
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no
	
	can_use_gui = {
		ROOT = {
			OR = {
				custom_tooltip = {
					text = HOLY_WAR_REGULAR
					hidden_tooltip = {
						piety = 100
						NOT = { has_character_modifier = holy_war_timer }
					}
				}
				custom_tooltip = {
					text = HOLY_WAR_KHARIJITE
					hidden_tooltip = {
						piety = 100
						religion = kharijite
					}
				}
				piety = 250
				custom_tooltip = {
					text = HOLY_WAR_SANCTIONED
					hidden_tooltip = {
						has_opinion_modifier = { who = FROM modifier = holy_war_sanction }
					}
				}
				controls_religion = yes
			}
		}
	}
	
	can_use_title = {
		FROM = {
			#neighbours or close
			OR = {
				any_realm_province = {
					duchy = { title = PREVPREVPREV }
					any_neighbor_province = {
						owner = {
							OR = {
								is_liege_or_above = ROOT
								character = ROOT
							}
						}
					}
				}
				any_realm_province = {
					duchy = { title = PREVPREVPREV }
					ROOT = { 
						any_realm_province = {
							NOT = { distance = { where = PREVPREV value = 75 } }
							owner = {
								OR = {
									is_liege_or_above = PREVPREV
									character = PREVPREV
								}
							}
						}
					}
				}
			}
		}
		
		#use reconquista instead
		NAND = {
			capital_scope = { region = world_europe_west_iberia }
			ROOT = {
				capital_scope = { region = world_europe_west_iberia }
				independent = yes
				religion_group = christian
				is_heretic = no
			}
			FROM = {
				independent = yes
				religion_group = muslim
			}
		}
		
		ROOT = {
			num_of_count_titles = 1
			is_nomadic = no
			mercenary = no
			#slow down holy wars between pagans and christians/muslims
			OR = {
				ai = no
				FROM = { NOT = { religion_group = pagan_group } }
				has_opinion_modifier = { who = FROM modifier = holy_war_sanction }
				piety = 200
				AND = {
					religion_group = christian
					has_global_flag = christian_crusades_unlocked
				}
				AND = {
					religion_group = muslim
					has_global_flag = muslim_jihads_unlocked
				} 
				AND = {
					religion_group = pagan_group
					is_reformed_religion = yes
					has_global_flag = pagan_ghws_unlocked
				}
			}
			OR = {
				ai = no
				NOT = { religion_group = pagan_group }
				has_opinion_modifier = { who = FROM modifier = holy_war_sanction }
				piety = 200
				AND = {
					FROM = { religion_group = christian }
					has_global_flag = christian_crusades_unlocked
				}
				AND = {
					FROM = { religion_group = muslim }
					has_global_flag = muslim_jihads_unlocked
				} 
				AND = {
					FROM = { religion_group = pagan_group }
					has_global_flag = pagan_ghws_unlocked
				}
			}
			#big grey blobs
			OR = {
				ai = no
				PREV = { any_direct_de_jure_vassal_title = { any_direct_de_jure_vassal_title = { is_holy_site = ROOT } } }
				NOR = {
					has_landed_title = e_hre
					has_landed_title = e_karling
					any_liege = { has_landed_title = e_hre }
					any_liege = { has_landed_title = e_karling }
				}
				has_opinion_modifier = { who = FROM modifier = holy_war_sanction }
				piety = 300
			}
			#avoid invading west africa
			OR = {
				ai = no
				PREV = { any_direct_de_jure_vassal_title = { any_direct_de_jure_vassal_title = { is_holy_site = ROOT } } }
				AND = {
					culture_group = west_african
					FROM = { culture_group = west_african }
				}
				AND = {
					NOT = { culture_group = west_african }
					FROM = { NOT = { culture_group = west_african } }
				}
				has_opinion_modifier = { who = FROM modifier = holy_war_sanction }
				piety = 300
			}
			#avoid invading east africa
			OR = {
				ai = no
				PREV = { any_direct_de_jure_vassal_title = { any_direct_de_jure_vassal_title = { is_holy_site = ROOT } } }
				AND = {
					culture_group = east_african
					FROM = { culture_group = east_african }
				}
				AND = {
					NOT = { culture_group = east_african }
					FROM = { NOT = { culture_group = east_african } }
				}
				has_opinion_modifier = { who = FROM modifier = holy_war_sanction }
				piety = 300
			}
			#slow down Indian expansion outside of India
			OR = {
				ai = no
				PREV = { any_direct_de_jure_vassal_title = { any_direct_de_jure_vassal_title = { is_holy_site = ROOT } } }
				NOT = { religion_group = indian_group }
				PREV = {
					capital_scope = {
						OR = {
							region = world_india_deccan
							region = world_india_bengal
							region = world_india_rajastan
						}
					}
				}
				piety = 300
			}
			#orthodox AI should avoid holy wars for the most part
			OR = {
				ai = no
				NOR = {
					religion = orthodox
					religion = bogomilist
					religion = nestorian
					religion = monothelite
					religion = iconoclast
				}
				FROM = { religion_group = muslim }
				PREV = { kingdom = { ROOT = { capital_scope = { kingdom = { title = PREVPREVPREV} } } } }
				PREV = { any_direct_de_jure_vassal_title = { any_direct_de_jure_vassal_title = { is_holy_site = ROOT } } }
				piety = 400
			}
			#christians should leave tengris alone
			OR = {
				ai = no
				NOT = { religion_group = christian }
				FROM = { NOT = { religion = tengri_pagan } }
				has_global_flag = mongol_horde_arrival
				PREV = { any_direct_de_jure_vassal_title = { any_direct_de_jure_vassal_title = { is_holy_site = ROOT } } }
				piety = 400
			}
			#jewish AI should avoid holy wars for the most part
			OR = {
				ai = no
				NOT = { religion_group = jewish_group }
				PREV = { any_direct_de_jure_vassal_title = { any_direct_de_jure_vassal_title = { is_holy_site = ROOT } } }
				piety = 400
			}
			#AI will usually only use the CB once every 5 years
			OR = {
				ai = no
				NOT = { has_character_modifier = holy_war_timer }
				PREV = { any_direct_de_jure_vassal_title = { any_direct_de_jure_vassal_title = { is_holy_site = ROOT } } }
				has_opinion_modifier = { who = FROM modifier = holy_war_sanction }
			}
		}
	}
	
	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { 
			NOR = { 
				has_character_modifier = holy_truce_timer
				has_character_modifier = crusade_block_timer 
			} 
		}
		ROOT = {
			top_liege = { religion_group = PREV }
			OR = {
				NOT = { religion_group = pagan_group }
				is_reformed_religion = yes
				any_realm_title = {
					count = 3
					is_holy_site = ROOT
				}
			}
			OR = {
				is_patrician = no
				independent = yes
			}
			OR = {
				NOR = {
					religion = hindu
					religion = buddhist
				}
				FROM = { NOT = { religion_group = indian_group } }
			}
			OR = {
				NOT = { religion = cathar }
				has_global_flag = cathar_war
			}
			OR = {
				NOT = { religion_group = FROM }
				is_heresy_of = FROM
				is_parent_religion = FROM
				AND = {
					OR = {
						religion_group = muslim
						religion_group = pagan_group
						religion = paulician
					}
					NOT = { religion = FROM }
				}
			}
			NOR = {
				pacifist = yes
				same_realm = FROM
			}
		}
	}

	is_valid = {
		ROOT = {
			NOT = { same_realm = FROM }
			NOT = { pacifist = yes }
			OR = {
				NOT = { religion_group = FROM }
				is_heresy_of = FROM
				is_parent_religion = FROM
				AND = {
					OR = {
						religion_group = muslim
						religion_group = pagan_group
						religion = paulician
					}
					NOT = { religion = FROM }
				}
			}
		}
	}
	
	on_add_title = {
		if = {
			limit = {
				any_direct_de_jure_vassal_title = { any_direct_de_jure_vassal_title = { is_holy_site = ROOT } }
			}
			ROOT = { custom_tooltip = { text = NO_PIETY_COST_HOLY } }
		}
		if = {
			limit = {
				NOT = { any_direct_de_jure_vassal_title = { any_direct_de_jure_vassal_title = { is_holy_site = ROOT } } }
				ROOT = { has_opinion_modifier = { who = FROM modifier = holy_war_sanction } }
			}
			ROOT = { custom_tooltip = { text = NO_PIETY_COST_SANCTION } }
		}
		if = {
			limit = {
				NOT = {
					any_direct_de_jure_vassal_title = { any_direct_de_jure_vassal_title = { is_holy_site = ROOT } }
					ROOT = { has_opinion_modifier = { who = FROM modifier = holy_war_sanction } }
				}
				ROOT = {
					OR = {
						NOT = { has_character_modifier = holy_war_timer }
						controls_religion = yes
						religion = kharijite
					}
				}
			}
			ROOT = { piety = -100 }
		}
		if = {
			limit = {
				NOR = {
					any_direct_de_jure_vassal_title = { any_direct_de_jure_vassal_title = { is_holy_site = ROOT } }
					ROOT = { has_opinion_modifier = { who = FROM modifier = holy_war_sanction } }
				}
				ROOT = {
					has_character_modifier = holy_war_timer
					controls_religion = no
					NOT = { religion = kharijite }
				}
			}
			ROOT = { piety = -250 }
		}
		if = {
			limit = {
				ROOT = { has_opinion_modifier = { who = FROM modifier = holy_war_sanction } }
			}
			hidden_tooltip = {
				ROOT = { remove_opinion = { who = FROM modifier = holy_war_sanction } }
			}
		}
	}
	
	on_success = {
		ROOT = {
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			pf_tradition_plus4_effect = yes
		}
		FROM = {
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
			pf_tradition_minus4_effect = yes
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_piety = 200
			participation_scaled_prestige = 200
			dynasty_head = {
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_piety = 200
				participation_scaled_prestige = 200 
				dynasty_head = {
					if = {
						limit = {
							uses_decadence = yes
						}
						participation_scaled_decadence = -10
					}
				}
			}
		}		
		if = {
			limit = {
				ROOT = { is_heretic = no }
			}
			ROOT = {
				religion_authority = {
					modifier = won_holy_war
					years = 20
				}
				hidden_tooltip = {
					if = {
						limit = {
							has_nickname = no
							religion_group = pagan_group
						}
						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 }
						}
					}
				}
			}
		}
		if = {
			limit = {
				FROM = { is_heretic = no }
			}
			FROM = { 
				religion_authority = {
					modifier = lost_holy_war
					years = 20
				}
			}
		}
	}
	
	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their holy war for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		hidden_tooltip = {
			if = {
				limit = { 
					has_holder = yes
					holder_scope = { 
						OR = {
							character = FROM
							is_liege_or_above = FROM
						}
					}
				}
				set_global_flag = usurp_duchy
			}
		}
		empty_province_effect = yes
		pf_liege_change_under_title_begin_effect = yes
		
		ROOT = {
			hidden_tooltip = { add_character_modifier = { name = "holy_war_timer" duration = 1825 } }
			vassalize_or_take_under_title = {
				title = PREV
				enemy = FROM
				same_religion = yes # Only vassalize rulers of my religion
				is_religious = yes
				type = holy_war
			}
		}
		
		hidden_tooltip = {
			if = {
				limit = { 
					has_global_flag = usurp_duchy
				}
				destroy_landed_title = yes
				usurp_title = ROOT
				clr_global_flag = usurp_duchy
			}
			#pick temple to grant if ruler has promised one
			ROOT = {
				if = {
					limit = { has_character_flag = pledged_temple }
					random_demesne_title = {
						limit = {
							tier = BARON
							holding_type = temple
							location = { duchy = { title = PREVPREVPREVPREV } }
						}
						save_event_target_as = granted_temple
						ROOT = { character_event = { id = Plus.1659 } }
					}
					clr_character_flag = pledged_temple
				}
			}
		}
		
		pf_liege_change_under_title_end_effect = yes
	}

	on_fail = {
		ROOT = {
			clr_character_flag = pledged_temple
			hidden_tooltip = { add_character_modifier = { name = "holy_war_timer" duration = 1825 } }
			piety = -100
			prestige = -100
		}
	}
	
	on_reverse_demand = {
		FROM = {
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			pf_tradition_plus4_effect = yes
		}
		ROOT = {
			imperial_decadence_plus_1_effect = yes
			hidden_tooltip = { add_character_modifier = { name = "holy_war_timer" duration = 1825 } }
			clr_character_flag = pledged_temple
			piety = -200
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			pf_glory_minus2_effect = yes
			pf_tradition_minus4_effect = yes
		}		
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
			participation_scaled_piety = 200
			dynasty_head = {
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_prestige = 200 
				participation_scaled_piety = 200 
				dynasty_head = {
					if = {
						limit = {
							uses_decadence = yes
						}
						participation_scaled_decadence = -10
					}
				}
			}
		}
		if = {
			limit = {
				FROM = { is_heretic = no }
			}
			FROM = { 
				religion_authority = {
					modifier = won_holy_war
					years = 20
				}
			}
		}
		if = {
			limit = {
				ROOT = { is_heretic = no }
			}
			ROOT = {
				religion_authority = {
					modifier = lost_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
			FROM = {
				NOT = {
					any_realm_province = {
						duchy = { title = PREVPREVPREV }
						any_neighbor_province = { owner = { same_realm = ROOT } }
					}
				}
			}
			ROOT = {
				NOT = {
					any_realm_province = {
						TECH_NAVAL = 1
						port = yes
					}
				}
			}
		}
		modifier = {
			factor = 1.2
			FROM = {
				independent = yes
				NOT = { any_realm_title = { tier = COUNT count = 4 } }
			}
		}
 		modifier = {
			factor = 1.2
			any_direct_de_jure_vassal_title = {
				location = { religion = ROOT }
			}
		}
		modifier = {
			factor = 25.0
			ROOT = {
				has_opinion_modifier = { who = FROM modifier = holy_war_sanction }
			}
		}
		modifier = {
			factor = 1.2
			ROOT = {
				trait = zealous
			}
		}	
		modifier = {
			factor = 0.8
			ROOT = {
				trait = cynical
			}
		}
		modifier = {
			factor = 1.2
			any_direct_de_jure_vassal_title = {
				any_direct_de_jure_vassal_title = {
					is_holy_site = ROOT
				}
			}
		}
		modifier = {
			factor = 1.1
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						is_holy_site = ROOT
					}
				}
			}
			kingdom = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						any_direct_de_jure_vassal_title = {
							is_holy_site = ROOT
						}
					}
				}
			}
		}
		# 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 = {
				capital_scope = {
					OR = {
						region = custom_bulgaria
						region = custom_croatia
						region = custom_serbia
						region = custom_wallachia				
						region = custom_crimea
						region = custom_cuman
						region = custom_perm
						region = custom_volga
						region = custom_rus
						region = world_india_bengal
						region = world_india_deccan
						region = world_india_rajastan						
					}
				}
				FROM = {
					primary_title = { title = e_golden_horde }
					NOT = { year = 1300 }
				}
				FROM = {
					primary_title = { title = e_chagatai }
					NOT = { year = 1300 }
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = { primary_title = { title = e_golden_horde } }
			OR = {
				capital_scope = {
					OR = {
						region = custom_persia
						region = custom_baluchistan
						region = custom_afghanistan
						region = custom_mesopotamia
						region = custom_azerbaijan
						region = custom_armenia
						region = custom_trebizond
						region = custom_nikaea
						region = custom_byzantium
						region = custom_norway
						region = custom_sweden
						region = custom_syria
						region = custom_arabia
						region = custom_jerusalem
						region = custom_egypt
						region = custom_africa
						region = world_india_bengal
						region = world_india_deccan
						region = world_india_rajastan
					}
				}
				FROM = {
					primary_title = { title = e_il-khanate }
					NOT = { year = 1300 }
				}
				FROM = {
					primary_title = { title = e_chagatai }
					NOT = { year = 1300 }
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = {
				primary_title = { title = e_chagatai }
			}
			OR = {
				capital_scope = {
					NOR = {
						region = custom_baluchistan
						region = custom_afghanistan
						region = world_india_bengal
						region = world_india_deccan
						region = world_india_rajastan
					}
				}
				FROM = {
					primary_title = { title = e_il-khanate }
					NOT = { year = 1300 }
				}
				FROM = {
					primary_title = { title = e_chagatai }
					NOT = { year = 1300 }
				}
			}
		}
		modifier = {
			factor = 2.0
			ROOT = {
				OR = {
					culture = persian
					culture = baloch
					culture = afghan
					culture = turkish
				}
			}
			capital_scope = {
				OR = {
					#Persian Empire
					region = custom_persia
					region = custom_afghanistan
					region = custom_mesopotamia
					region = custom_azerbaijan
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = { religion_group = indian_group }
			capital_scope = {
				OR = {
					region = custom_persia
					region = custom_khwarizm
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = {
				OR = {
					has_landed_title = e_byzantium
					any_liege = { has_landed_title = e_byzantium }
				}
			}
			capital_scope = {
				OR = {
					region = custom_cuman
					region = custom_alania
					region = custom_volga
					region = custom_rus
				}
			}
		}
		modifier = {
			factor = 0.5
			ROOT = {
				OR = {
					has_landed_title = e_hre
					has_landed_title = e_karling
					any_liege = { has_landed_title = e_hre }
					any_liege = { has_landed_title = e_karling }
				}
			}
			capital_scope = {
				OR = {
					region = custom_denmark
					region = custom_england
				}
			}
		}
		modifier = {
			factor = 2.0
			ROOT = {
				OR = {
					has_character_flag = is_charlemagne
					has_landed_title = e_karling
					any_liege = { has_landed_title = e_karling }
					has_landed_title = e_hre
					any_liege = { has_landed_title = e_hre }
				}
			}
			capital_scope = {
				OR = {
					region = custom_germany
					region = custom_pomerania
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = { culture = hungarian }
			capital_scope = {
				OR = {
					region = custom_cuman
					region = custom_volga
					region = custom_alania
					region = custom_novgorod
					region = custom_muscovy
					region = custom_rus
					region = custom_byzantium
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = { culture = khazar }
			capital_scope = {
				OR = {
					region = world_steppe_tarim
					region = world_steppe_east
					region = custom_cuman
					region = custom_perm
					region = custom_volga
				}
			}
		}
		modifier = {
			factor = 1.5
			ROOT = { culture = hungarian }
			capital_scope = {
				OR = {
					region = custom_bulgaria
					region = custom_carpathia
					region = custom_croatia
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = {
				religion_group = muslim
				NOT = { religion = kharijite }
			}
			FROM = { religion = ibadi }
		}
		modifier = {
			factor = 0.1
			FROM = {
				religion_group = muslim
				NOT = { religion = kharijite }
			}
			ROOT = { religion = ibadi }
		}
		modifier = {
			factor = 0
			ROOT = {
				religion = ibadi
				NOT = { trait = zealous }
			}
		}
	}
}

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

	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		ROOT = {
			NOT = {
				is_liege_or_above = FROM
			}
		}
		FROM = {
			excommunicated_for = ROOT
		}
		FROM = {
			OR = {
				ai = no
				NOT = { trait = cynical }
			}
			OR = {
				ai = no
				trait = zealous
				piety = 250
				NOT = { opinion = { who = ROOT value = -50 } }
			}
		}
		ROOT = {
			mercenary = no
		}
	}

	is_valid = {
		FROM = {
			excommunicated_for = ROOT
		}
	}

	on_success = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their excommunication war against [From.GetBestName] (ID: [From.GetID])"
		hidden_tooltip = {
			any_attacker = { 		
				opinion = { modifier = overthrown_ruler who = FROM }
				reverse_opinion = { modifier = overthrew_me who = FROM }
			} 
		}
		#possible independence for the defender's weakly held vassals
		if = {
			limit = {
				FROM = { independent = yes }
			}
			hidden_tooltip = {
				FROM = { 
					any_vassal = {
						character_event = { id = Plus.430 days = 3 } 
					}
				}
			}
		}
		
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			pf_tradition_plus2_effect = yes
		}
		if = {
			limit = {
				FROM = {
					primary_title = {
						tier = BARON
					}
				}
			}
			any_attacker = {
				limit = { character = ROOT }
				participation_scaled_prestige = 25
				participation_scaled_piety = 25
			}
			any_attacker = {
				limit = { NOT = { character = ROOT } }
				hidden_tooltip = { 
					participation_scaled_prestige = 25
					participation_scaled_piety = 25
				}
			}
		}
		if = {
			limit = {
				FROM = {
					primary_title = {
						tier = COUNT
					}
				}
			}
			any_attacker = {
				limit = { character = ROOT }
				participation_scaled_prestige = 50
				participation_scaled_piety = 50
			}
			any_attacker = {
				limit = { NOT = { character = ROOT } }
				hidden_tooltip = { 
					participation_scaled_prestige = 50
					participation_scaled_piety = 50
				}
			}
		}
		if = {
			limit = {
				FROM = {
					primary_title = {
						tier = DUKE
					}
				}
			}
			any_attacker = {
				limit = { character = ROOT }
				participation_scaled_prestige = 100
				participation_scaled_piety = 100
			}
			any_attacker = {
				limit = { NOT = { character = ROOT } }
				hidden_tooltip = { 
					participation_scaled_prestige = 100
					participation_scaled_piety = 100
				}
			}
		}
		if = {
			limit = {
				FROM = {
					primary_title = {
						tier = KING
					}
				}
			}
			any_attacker = {
				limit = { character = ROOT }
				participation_scaled_prestige = 200
				participation_scaled_piety = 200
			}
			any_attacker = {
				limit = { NOT = { character = ROOT } }
				hidden_tooltip = { 
					participation_scaled_prestige = 200
					participation_scaled_piety = 200
				}
			}
		}
		if = {
			limit = {
				FROM = {
					primary_title = {
						tier = EMPEROR
					}
				}
			}
			any_attacker = {
				limit = { character = ROOT }
				participation_scaled_prestige = 300
				participation_scaled_piety = 300
			}
			any_attacker = {
				limit = { NOT = { character = ROOT } }
				hidden_tooltip = { 
					participation_scaled_prestige = 300
					participation_scaled_piety = 300
				}
			}
		}

		FROM = {
			excommunicate = no
			prestige = -500
			rightful_religious_head_scope = {
				opinion = {
					who = PREV
					modifier = opinion_repented_sins
				}
			}
			any_demesne_title = {
				title_reduce_CA_effect = yes
				title_reduce_TO_effect = yes
			}
			#pass on dishonorable/tyrant traits to new ruler
			hidden_tooltip = { character_event = { id = Plus.306 } }
			abdicate = yes
		}
	}

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

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			piety = -100
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			pf_glory_minus2_effect = yes
			pf_tradition_minus2_effect = yes
		}
		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
	}
}

bid_for_independence = {
	name = CB_NAME_INDEPENDENCE
	war_name = WAR_NAME_INDEPENDENCE
	sprite = 12
	truce_days = 3650

	is_permanent = yes
	is_independence = yes
	can_ask_to_join_war = no

	can_use = {
		ROOT = {
			de_facto_liege = FROM
			mercenary = no			
			OR = {
				is_nomadic = no
				NOT = {
					clan_opinion = { who = FROM value = 0 }
				}
			}
		}

		FROM = {
			NOT = {
				any_war = {
					attacker = {
						character = PREVPREV
					}
					OR = {
						using_cb = bid_for_independence
						using_cb = depose_liege
					}
				}
			}
			any_demesne_title = {
				NOT = { lower_tier_than = ROOT }
				temporary = no
			}
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
			
			# No independence factions in merchant republics
			NOT = {
				AND = {
					is_merchant_republic = yes
					liege = {
						is_merchant_republic = yes
					}
				}
			}
		}
	}

	is_valid = {
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
		FROM = {
			any_demesne_title = {
				NOT = { lower_tier_than = ROOT }
				temporary = no
			}
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
			# No independence factions in merchant republics
			NOT = {
				AND = {
					is_merchant_republic = yes
					liege_before_war = {
						is_merchant_republic = yes
					}
				}
			}
		}
	}

	on_success = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their bid for independence against [From.GetBestName] (ID: [From.GetID])"
		if = {
			limit = {
				FROM = { independent = no }
			}
			FROM = {
				liege = {
					ROOT = {
						set_defacto_liege = PREV
						pf_liege_change_effect = yes
					}
				}
			}
		}
		if = {
			limit = {
				FROM = { independent = yes }
			}
			ROOT = {
				set_defacto_liege = ROOT
				pf_liege_change_effect = yes
			}
			
			#weakly held territories may declare independence
			hidden_tooltip = { 
				FROM = { 
					any_vassal = { 
						character_event = { id = Plus.430 days = 3 }
					}
				}
			}
		}

		hidden_tooltip = {
			ROOT = {
				prestige = 100
				add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
				pf_glory_plus2_effect = yes
			}
			FROM = {
				opinion = {
					who = ROOT
					modifier = opinion_independent
					years = 50
				}
				pf_glory_minus2_effect = yes
			}
		}

		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 = {
				limit = { can_destroy_title_trigger = yes }
				custom_tooltip = { text = TITLE_ENDANGERED_NO_NAME }
				set_title_flag = destroy_title
			}
			any_demesne_title = {
				title_reduce_CA_effect = yes
				title_reduce_TO_effect = yes
			}
			hidden_tooltip = {
				# destroy primary title if it has been reduced too much
				if = {
					limit = { primary_title = { has_title_flag = destroy_title } }
					character_event = { id = Plus.1620 days = 4 }
				}
			}
		}
	}

	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
			pf_glory_minus2_effect = yes
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = -100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = -100 }
		}
		FROM = {
			prestige = 50
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
	}
	
	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 = yes

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

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

	on_success = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their depose_liege cb against [From.GetBestName] (ID: [From.GetID])"
		hidden_tooltip = {
			any_attacker = { 		
				opinion = { modifier = overthrown_ruler who = FROM }
				reverse_opinion = { modifier = overthrew_me who = FROM }
			} 
		}
		FROM = {
			prestige = -500
			any_demesne_title = {
				limit = { can_destroy_title_trigger = yes }
				custom_tooltip = { text = TITLE_ENDANGERED_NO_NAME }
				save_event_target_as = former_primary_title
				set_title_flag = destroy_title
			}
			any_demesne_title = {
				title_reduce_CA_effect = yes
				title_reduce_TO_effect = yes
			}
			if = {
				limit = { has_dlc = "Zeus" }
				any_demesne_title = { # All titles
					limit = {
						OR = {
							tier = duke
							tier = king
							tier = emperor
						}
						NOT = { has_law = revoke_title_law_0 }
					}
					if = {
						limit = { has_law = revoke_title_law_1 }
						add_law = revoke_title_law_0
					}
					if = {
						limit = { has_law = revoke_title_law_2 }
						add_law = revoke_title_law_0
					}
				}
			}
			abdicate_to_most_liked_by = ROOT
			hidden_tooltip = {
				event_target:former_primary_title = {
					holder_scope = {
						# destroy primary title if it has been reduced too much
						if = {
							limit = { primary_title = { has_title_flag = destroy_title } }
							character_event = { id = Plus.1620 days = 4 }
						}
					}
				}
			}
		}
		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
	is_tyranny_cb = 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 = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their overthrow_ruler CB against [From.GetBestName] (ID: [From.GetID])"
		hidden_tooltip = {
			any_attacker = { 		
				opinion = { modifier = overthrown_ruler who = FROM }
				reverse_opinion = { modifier = overthrew_me who = FROM }
			} 
		} 
		ROOT = {
			primary_title = {
				add_pressed_claim = FROM	
			}
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		FROM = {
			prestige = -500
			remove_opinion = {
				who = ROOT
				modifier = declared_war
			}
			any_demesne_title = {
				limit = { can_destroy_title_trigger = yes }
				custom_tooltip = { text = TITLE_ENDANGERED_NO_NAME }
				set_title_flag = destroy_title
			}
			any_demesne_title = {
				title_reduce_CA_effect = yes
				title_reduce_TO_effect = yes
			}
			hidden_tooltip = {
				# destroy primary title if it has been reduced too much
				if = {
					limit = { primary_title = { has_title_flag = destroy_title } }
					current_heir = { character_event = { id = Plus.1620 days = 4 } }
				}
			}
			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 = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -200
			prisoner = FROM
			pf_glory_minus2_effect = yes
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_defender_leader_death = {
		hidden_tooltip = {
			any_attacker = {
				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
	}
}

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_add = {
	}
	on_success = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their overthrow_ruler_no_calls CB against [From.GetBestName] (ID: [From.GetID])"
		hidden_tooltip = {
			any_attacker = { 		
				opinion = { modifier = overthrown_ruler who = FROM }
				reverse_opinion = { modifier = overthrew_me who = FROM }
			} 
		} 
		ROOT = {
			primary_title = {
				add_pressed_claim = FROM	
			}
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		FROM = {
			prestige = -500
			remove_opinion = {
				who = ROOT
				modifier = declared_war
			}
			any_demesne_title = {
				limit = { can_destroy_title_trigger = yes }
				custom_tooltip = { text = TITLE_ENDANGERED_NO_NAME }
				set_title_flag = destroy_title
			}
			any_demesne_title = {
				title_reduce_CA_effect = yes
				title_reduce_TO_effect = yes
			}
			hidden_tooltip = {
				# destroy primary title if it has been reduced too much
				if = {
					limit = { primary_title = { has_title_flag = destroy_title } }
					current_heir = { character_event = { id = Plus.1620 days = 4 } }
				}
			}
			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 = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -200
			prisoner = FROM
			pf_glory_minus2_effect = yes
		}
		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
	}
}

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
	#is_holy_war = yes
	can_ask_to_join_war = yes
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no

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

	can_use_title = {
		tier = COUNT
		location = {
			has_trade_post = yes
			trade_post_owner = {
				character = ROOT
			}
		}
	}

	is_valid = {
		ROOT = {
			OR = {
				is_merchant_republic = yes
				is_patrician = yes
			}
			NOT = { same_realm = FROM }
		}
	}

	is_valid_title = {
		tier = COUNT
		location = {
			has_trade_post = yes
			trade_post_owner = {
				character = ROOT
			}
		}
	}

	on_add = {
		any_playable_ruler = {
			limit = {
				any_demesne_province = {
					has_trade_post = yes
					trade_post_owner = {
						character = ROOT
					}
				}
			}
			opinion = {
				who = ROOT
				modifier = opinion_aggressive_republic
				years = 10
			}
		}
	}

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		FROM = {
			pf_glory_minus2_effect = yes
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their coastal republic war for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		empty_province_effect = yes
		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 }
				}
			}
		}
	}

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

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			pf_glory_minus2_effect = yes
		}
		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
	}
	
	ai_will_do = {
		factor = 1
		modifier = {
			factor = 0
			ROOT = {
				NOR = {
					trait = cynical
					trait = ambitious
				}
				rightful_religious_head_scope = { character = FROM }
			}
		}
	}
}

coastal_county_republic = { # Take a county if we have a city (barony) here
	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 = yes
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no
	
	allow_distant = yes # AI parameter

	can_use = {
		ROOT = {
			attacking_religious_head_trigger = no
			OR = {
				is_merchant_republic = yes
				is_patrician = yes
			}
			NOR = { 
				is_liege_or_above = FROM
				is_vassal_or_below = FROM 
				same_liege = FROM
				has_character_modifier = rep_cb_cd
			}
			mercenary = no
		}
	}

	can_use_title = {
		tier = COUNT
		
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }			
		
		location = {
			port = yes
			
			county = {
				any_direct_de_jure_vassal_title = {
					holder_scope = {
						OR = {
							character = ROOT
							is_liege_or_above = ROOT
						}
					}
				}
			}			
			
			# Not allowed to use this CB to take cities in the capital province of a rival merchant republic
			FROM = { 
				OR = {
					is_merchant_republic = no
					capital_scope = {
						NOT = { province_id = PREVPREV }
					}
				}
			}
		}
	}

	is_valid = {
		ROOT = {
			OR = {
				is_merchant_republic = yes
				is_patrician = yes
			}
		}
	}
	
	on_add = {
		ROOT = {
			add_character_modifier = {
				name = rep_cb_cd
				duration = 1825
				hidden = no
				inherit = yes
			}
		}
	}	

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		FROM = {
			pf_glory_minus2_effect = yes
		}
	}
	
	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their coastal county republic war for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		if = {
			limit = {
				NOT = {
					location = {
						trade_post_owner = {
							is_liege_or_above = ROOT
						}
					}
				}
			}
			usurp_title = ROOT
		}
		if = {
			limit = {
				location = {
					port = yes
					ROOT = {
						capital_scope = {
							NOT = {
								distance = {
									where = PREVPREV # Comparing the distance between ROOT's capital and the title in question
									value = 590	#  From  aprox.Venice to East Cyprus and from Venice to Gibraltar
								}
							}	
						}
					}			
				}
			}
			custom_tooltip = {
				text = CTT_REP_IN_RANGE
			}
		}
		if = {
			limit = {
				location = {
					port = yes
					ROOT = {
						capital_scope = {
							distance = {
								where = PREVPREV # Comparing the distance between ROOT's capital and the title in question
								value = 590	#  From  aprox.Venice to East Cyprus and from Venice to Gibraltar
							}	
						}
					}			
				}			
			}
			custom_tooltip = {
				text = CTT_REP_OUT_RANGE
			}			
		}		
		if = {
			limit = {
				location = {
					trade_post_owner = {
						is_liege_or_above = ROOT
					}
				}
			}
			location = {
				trade_post_owner = {
					usurp_title = PREVPREV
				}
			}
		}

		hidden_tooltip = {
			ROOT = {
				remove_character_modifier = rep_cb_cd
				add_character_modifier = {
					name = rep_cb_cd
					duration = 1825
					hidden = no
					inherit = yes
				}
			}
		}		
		
		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 }
		}
		
		hidden_tooltip = {
			ROOT = { remove_character_modifier = rep_cb_cd }
		}			
		
	}

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			pf_glory_minus2_effect = yes
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		
		hidden_tooltip = {
			ROOT = { remove_character_modifier = rep_cb_cd }
		}			
		
	}

	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_will_do = {
		factor = 1
		modifier = {
			factor = 0
			ROOT = {
				NOR = {
					trait = cynical
					trait = ambitious
				}
				rightful_religious_head_scope = { character = FROM }
			}
		}
	}
}

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
	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 = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		FROM = {
			pf_glory_minus2_effect = yes
		}
	}

	on_success_title = {
		title_reduce_CA_effect = yes
		title_reduce_TO_effect = yes

		FROM = {
			random_vassal = {
				limit = {
					is_primary_heir = no
					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_invalidation = {
		ROOT = {
			cancel_plot = plot_weaken_fellow_vassal
		}
		
	}

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -200
			imprison = FROM
			cancel_plot = plot_weaken_fellow_vassal
			pf_glory_minus2_effect = yes
		}
		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 = {
		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
	}
}

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
	
	can_use = {
		ROOT = {
			vassal_of = FROM
			has_opinion_modifier = { who = FROM modifier = revolting_against }
		}
	}

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

	on_add = {
		log = "CB(cb_faction_independence): START: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
		FROM = { pf_log_state_effect = yes }
		ROOT = { pf_log_state_effect = yes }
		#emf_cb_dynlevy_begin_effect = yes
	}	
	
	on_success = {
		log = "CB(cb_faction_independence): VICTORY: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
		
		# add victory modifier for the 'Win a War' ambition
		any_attacker = { add_character_modifier = { name = victory_timer duration = 1 hidden = yes } }
		
		# apply immediate effects to the revolters
		hidden_tooltip = {
			any_attacker = {
				limit = { has_opinion_modifier = { who = FROM modifier = revolting_against } }
				
				# get Glory faction boost
				pf_glory_plus2_effect = yes
				
				# and also are grateful to each other for their victory
				FROM = {
					any_opinion_modifier_target = {
						limit = {
							reverse_has_opinion_modifier = {
								who = PREV
								modifier = revolting_against_me
							}
							NOT = { character = PREVPREV }
						}
						reverse_opinion = {
							who = PREVPREV
							modifier = opinion_very_grateful
							years = 10
						}
					}
				}
			}
		}
		
		# revolters will become independent
		if = {
			limit = { FROM = { independent = yes } }
			FROM = {
				any_opinion_modifier_target = {
					limit = { pf_part_of_this_revolt_trigger = yes }
					set_defacto_liege = THIS
					hidden_tooltip = {
						prestige = 100
						reverse_opinion = { who = PREV modifier = opinion_independent years = 50 }
						#weakly held territories may declare independence
						any_vassal = { character_event = { id = Plus.430 days = 3 } }
					}
				}
				#weakly held territories may declare independence
				hidden_tooltip = {
					any_vassal = {
						limit = { pf_part_of_this_revolt_trigger = no }
						character_event = { id = Plus.430 days = 3 }
					}
				}
			}
		}
		
		hidden_tooltip = {
			any_attacker = {
				limit = {
					leads_faction = faction_separatist
					independent = yes
				}
				set_character_flag = former_faction_leader
				pf_clear_war_flags_effect = yes
			}
			any_attacker = {
				limit = { 
					in_faction = faction_separatist
					independent = yes
				}
				pf_clear_supporter_flags_effect = yes
				pf_clear_war_flags_effect = yes
			}
		}
		
		# clean up remaining TOMs
		pf_civil_war_cleanup_effect = yes		
		
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = 200
			}
		}

		FROM = {
			prestige = -500
			pf_glory_minus2_effect = yes
			
			# increase imperial decadence
			imperial_decadence_plus_1_effect = yes
			
			# check whether title will be destroyed (CK2Plus only)
			any_demesne_title = {
				limit = { can_destroy_title_trigger = yes }
				custom_tooltip = { text = TITLE_ENDANGERED_NO_NAME }
				log = "[This.GetFullName] should be destroyed as a result of the revolt"
				set_title_flag = destroy_title
			}
		
			# always lower CA/TO, even if it wasn't a revolt demand
			pf_lower_CA_effect = yes # lower Crown Authority
			pf_lower_TO_effect = yes # lower Tribal Organization
			
			# check whether the primary title should be destroyed due to CA loss
			if = {
				limit = { primary_title = { has_title_flag = destroy_title } }
				hidden_tooltip = { character_event = { id = Plus.1620 days = 4 } } #destroy title
			}
		}
		
		# change faction moods
		FROM = { hidden_tooltip = { character_event = { id = PlusFaction.200 days = 1 } } }
		ROOT = { hidden_tooltip = { character_event = { id = PlusFaction.200 days = 1 } } }

		# reset contiguous territory province flags
		hidden_tooltip = { d_miaphysite = { holder_scope = { character_event = { id = Plus.307 days = 1 } } } }
		
		#emf_cb_dynlevy_end_effect = yes
	}

	on_fail = {
		log = "CB(cb_faction_independence): WHITEPEACE: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
		
		#emf_cb_dynlevy_end_effect = yes

		# clean up remaining TOMs 
		pf_civil_war_cleanup_effect = yes
		
		hidden_tooltip = {
			FROM = {
				remove_opinion = {
					who = ROOT
					modifier = declared_war
				}
			}
		}
		
		ROOT = {
			prestige = -100
			prisoner = FROM
			add_character_modifier = {
				name = faction_independence_ultimatum_timer
				hidden = yes
				duration = 3650
			}
		}
		
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = -100 }
			add_character_modifier = {
				name = faction_independence_ultimatum_timer
				hidden = yes
				duration = 3650
			}
		}
	}

	on_reverse_demand = {
		log = "CB(cb_faction_independence): DEFEAT: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"

		#emf_cb_obj_win_war_reverse_effect = yes
		#emf_cb_dynlevy_end_effect = yes		
		
		FROM = {
			prestige = 200
			# the defender loses 1 Imperial Decadence
			imperial_decadence_minus_1_effect = yes
			# receive Glory faction boost
			pf_glory_plus2_effect = yes
			# add victory modifier for the 'Win a War' ambition
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			# remove any previously-existing loser TOMs
			pf_civil_war_clear_old_opinions_effect = yes
		}
		
		# apply immediate effects to the revolters
		any_attacker = {
			limit = { has_opinion_modifier = { who = FROM modifier = revolting_against } }
			imprison = FROM
			hidden_tooltip = {
				reverse_opinion = { who = FROM modifier = opinion_civil_war_loser months = 60 }
				opinion = { who = FROM modifier = opinion_coerced_into_leaving_faction months = 60 }
			}
		}
		
		# mark loyalists and neutrals for the reward event
		hidden_tooltip = {
			FROM = {
				any_vassal = {
					limit = { reverse_has_opinion_modifier = { who = PREV modifier = pf_loyal_to_me } }
					reverse_opinion = { who = PREV modifier = opinion_civil_war_supporter months = 60 }
				}
				any_vassal = {
					limit = { reverse_has_opinion_modifier = { who = PREV modifier = pf_neutral_to_me } }
					reverse_opinion = { who = PREV modifier = opinion_civil_war_neutral months = 60 }
				}
			}
		}
		
		# clean up remaining civil war state
		pf_civil_war_cleanup_effect = yes
		
		ROOT = {
			prestige = -200
			pf_glory_minus2_effect = yes
			add_character_modifier = {
				name = faction_independence_ultimatum_timer
				hidden = yes
				duration = 3650
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			participation_scaled_prestige = -200
			hidden_tooltip = {
				add_character_modifier = {
					name = faction_independence_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		
		# post-civil war event to reward allies
		FROM = {
			hidden_tooltip = { character_event = { id = PlusFaction.450 } }
		}
		
		# change faction moods
		hidden_tooltip = {
			FROM = { character_event = { id = PlusFaction.200 days = 1 } }
			ROOT = { character_event = { id = PlusFaction.200 days = 1 } }
		}
	}

	on_invalidation = {
		log = "CB(cb_faction_independence): INVALIDATED: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"

		#emf_cb_dynlevy_end_effect = yes
		
		# clean up remaining civil war state
		pf_civil_war_cleanup_effect = yes
	}	
	
	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
	}
}

imperial_reconquest = {
	name = CB_NAME_IMPERIAL
	war_name = WAR_NAME_IMPERIAL
	sprite = 17
	truce_days = 3650
	is_permanent = yes
	is_holy_war = yes
	check_de_jure_tier = DUKE # this scans all dejure duchies for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no

	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		ROOT = {
			has_landed_title = e_roman_empire
			independent = yes
			OR = {
				religion_group = christian
				religion = hellenic_pagan
			}
			NOT = { same_realm = FROM }
			mercenary = no
		}
	}

	can_use_title = {
		capital_scope = {
			OR = {
				region = custom_byzantium
				region = custom_nikaea
				region = custom_trebizond
				region = custom_armenia
				region = custom_bulgaria
				region = custom_serbia
				region = custom_croatia
				region = custom_italy
				region = custom_sicily
				region = custom_germany
				region = custom_burgundy
				region = custom_france
				region = custom_aquitaine
				region = custom_brittany
				region = custom_england
				region = custom_wales
				region = custom_frisia
				region = custom_lotharingia
				region = world_europe_west_iberia
				region = custom_syria
				region = custom_jerusalem
				region = custom_egypt
				region = custom_africa
			}
		}
		FROM = {
			#neighbours or close
			OR = {
				any_realm_province = {
					duchy = { title = PREVPREVPREV }
					any_neighbor_province = {
						owner = { same_realm = ROOT }
					}
				}
				any_realm_province = {
					duchy = { title = PREVPREVPREV }
					ROOT = { 
						any_realm_province = {
							NOT = { distance = { where = PREVPREV value = 150 } }
							owner = { same_realm = ROOT }
						}
					}
				}
			}
		}
	}

	is_valid = {
		ROOT = {
			independent = yes
			OR = {
				religion_group = christian
				religion_group = pagan_group
			}
			NOT = { same_realm = FROM }
		}
	}

	on_success = {
		ROOT = {
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = 200
			}
		}
		FROM = {
			prestige = -200
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their imperial reconquest of the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		empty_province_effect = yes
		
		pf_liege_change_under_title_begin_effect = yes
		
		custom_tooltip = {
			text = other_invasion_succ_tip
			hidden_tooltip = {
				ROOT = {
					vassalize_or_take_under_title = {
						title = PREV
						enemy = FROM
						type = invasion
					}
				}
			}
		}
		if = {
			limit = {
				holder_scope = { character = FROM }
			}
			usurp_title_only = { target = ROOT type = invasion }
		}
		
		pf_liege_change_under_title_end_effect = yes
	}

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

	on_reverse_demand = {
		FROM = {
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			imperial_decadence_plus_1_effect = yes
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			prestige = -200
			pf_glory_minus2_effect = yes
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_piety = 100
			participation_scaled_prestige = 200
			dynasty_head = {
				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
				dynasty_head = {
					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
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no

	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		ROOT = {
			independent = yes
			NOT = { has_embargo = FROM }
			mercenary = no
			OR = {
				ai = no
				NOR = {
					trait = charitable
					opinion = { who = FROM value = 0 }
					AND = {
						NOR = {
							trait = greedy
							trait = envious
						}
						opinion = { who = FROM value = -25 }
					}
				}
			}
		}
		FROM = {
			OR = {
				AND = {
					is_merchant_republic = yes
					any_trade_post = {
						owner = {
							OR = {
								character = ROOT
								is_liege_or_above = ROOT
							}
						}
					}
				}
				AND = {
					higher_tier_than = DUKE
					any_realm_lord = {
						is_merchant_republic = yes
						any_trade_post = {
							owner = {
								OR = {
									character = ROOT
									is_liege_or_above = ROOT
								}
							}
						}
					}
				}
			}
		}
		#the AI needs at least three trade posts to attack
		OR = {
			ROOT = { ai = no }
			FROM = {
				OR = {
					AND = {
						is_merchant_republic = yes
						any_trade_post = {
							count = 3
							owner = { same_realm = ROOT }
						}
					}
					AND = {
						higher_tier_than = DUKE
						any_realm_lord = {
							is_merchant_republic = yes
							any_trade_post = {
								count = 3
								owner = { same_realm = ROOT }
							}
						}
					}
				}
			}
		}
	}

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

	on_success = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their embargo war against [From.GetBestName] (ID: [From.GetID])"
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			embargo = FROM
		}
		FROM = {
			pf_glory_minus2_effect = yes
		}
	}

	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 = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		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
				}
			}
		}
		ROOT = {
			pf_glory_minus2_effect = yes
		}
		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.9
			always = yes
		}		
		modifier = {
			factor = 0.5
			ROOT = {
				opinion = { who = FROM value = -25 }
			}
		}
		modifier = {
			factor = 0
			ROOT = {
				opinion = { who = FROM value = 0 }
			}
		}
	}
}

seize_trade_post = {
	name = CB_NAME_SEIZE_TRADE_POST
	war_name = WAR_NAME_SEIZE_TRADE_POST
	sprite = 1
	truce_days = 3650
	is_permanent = yes
#	check_all_titles = yes
	check_all_trade_posts = yes
	can_ask_to_join_war = yes
	ticking_war_score_multiplier = 5.0
	allow_distant = yes
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no

	can_use = {
		ROOT = {
			OR = {
				is_merchant_republic = yes
				is_patrician = yes
			}
			NOR = { 
				is_liege_or_above = FROM 
				has_character_modifier = rep_tp_cd
			}

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

	can_use_title = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }	
		tier = COUNT
		location = {
			port = yes
			
			# Within range of the capital
			ROOT = {
				capital_scope = {
					NOT = {
						distance = {
							where = PREVPREV # Comparing the distance between ROOT's capital and the title in question
							value = 590	#  From  aprox.Venice to East Cyprus and from Venice to Gibraltar
						}
					}	
				}
			}
		}
		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_add = {
		ROOT = {
			add_character_modifier = {
				name = rep_tp_cd
				duration = 1825
				hidden = no
				inherit = yes
			}
		}
	}	

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			pf_prosperity_plus2_effect = yes
		}
		FROM = {
			pf_glory_minus2_effect = yes
			pf_prosperity_minus2_effect = yes
		}
	}
		
	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their seize_trade_post CB against [From.GetBestName] (ID: [From.GetID])"
		seize_trade_post = ROOT
		
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		
		hidden_tooltip = {
			ROOT = {
				remove_character_modifier = rep_tp_cd
				add_character_modifier = {
					name = rep_tp_cd
					duration = 1825
					hidden = no
					inherit = yes
				}
			}
		}
	}

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

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -100
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
			pf_glory_minus2_effect = yes
			hidden_tooltip = {
				remove_character_modifier = rep_tp_cd
			}
		}
		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 = 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
	can_ask_to_join_war = yes
	check_de_jure_tier = KING # this scans all de jure kingdoms for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	apply_short_occ_mod = no # Do not apply the 'recently_conquered' modifier to Holdings
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no

	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		ROOT = {
			has_opinion_modifier = { who = FROM modifier = preparing_to_invade }
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}		
	}

	can_use_title = {
		is_valid_viking_invasion_target = FROM # Checks that FROM holds the right amount of territory in the kingdom
	}

	is_valid_title = {
		FROM = {
			any_realm_title = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}

	on_success = {
		ROOT = {
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		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
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their viking invasion for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		empty_province_effect = yes
		pf_liege_change_under_title_begin_effect = yes
		
		custom_tooltip = {
			text = tribal_invasion_succ_tip
			hidden_tooltip = {
				#attacker gains one province belonging to the target for certain
				if = {
					limit = { NOT = { capital_scope = { owner = { character = FROM } } } }
					FROM = {
						random_demesne_province = {
							limit = { county = { de_jure_liege_or_above = PREVPREVPREV } }
							culture = ROOT
							religion = ROOT
						}
					}
				}
				if = {
					limit = { capital_scope = { owner = { character = FROM } } }
					capital_scope = {
						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
						is_crusade = yes
						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 }
				}
			}
		}
		
		pf_liege_change_under_title_end_effect = yes
	}

	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 = {
		FROM = {
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -500
			piety = -250
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
			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 = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
	
	# 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.9
			NOT = {
				capital_scope = {
					OR = {
						region = custom_north_sea
						region = custom_england
						region = custom_scotland
						region = custom_ireland
						region = custom_wales
						region = custom_livonia
						region = custom_muscovy
						region = custom_novgorod
						region = custom_rus
						region = custom_volga
					}
				}
			}
		}
		modifier = {
			factor = 0
			ROOT = { has_character_flag = no_invasions }
		}
	}	
}

# 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
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no
	
	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		ROOT = {
			mercenary = no
			lower_tier_than = EMPEROR
			OR = {
				AND = {
					religion_group = pagan_group
					is_reformed_religion = no
				}
				religion = titan_pagan
				AND = {
					religion_group = indian_group
					OR = {
						culture_group = indo_aryan_group
						culture_group = dravidian_group
					}
				}
			}
			NOT = { same_realm = FROM }
			NOT = { is_liege_or_above = FROM }
			has_horde_culture = no # Should use tribal invasion instead
			OR = {
				NOT = { has_character_modifier = launched_subjugation }
				has_ambition = obj_become_king
			}
		}
		FROM = {
			#is either an unreformed pagan or is a reformed version of the attacker's religion
			#Alternatively ROOT and FROM must both be an Indian religion and culture
			in_revolt = no
			is_nomadic = no
			OR = {
				AND = {
					religion_group = pagan_group
					OR = {
						is_reformed_religion = no
						is_reformed_religion = ROOT
					}
				}
				AND = {
					religion_group = indian_group
					OR = {
						culture_group = indo_aryan_group
						culture_group = dravidian_group
					}			
				}
			}
			#not already a defender in a subjugation war
			NOT = {
				any_war = {
					defender = { character = PREVPREV }
					OR = {
						using_cb = pagan_subjugation
						using_cb = nomad_subjugation
					}
				}
			}
		}
	}
	
	can_use_title = {
		FROM = {
			any_realm_title = {
				de_jure_liege_or_above = PREVPREV
			}
		}
		ROOT = {
			capital_scope = {
				kingdom = { title = PREVPREVPREV }
			}
		}
		OR = {
			holder = ROOT
			ROOT = { lower_tier_than = KING }
		}
		#the ai will only attack neighbors (or those very close)
		OR = {
			ROOT = { ai = no }
			FROM = {
				OR = {
					any_realm_province = {
						kingdom = { title = PREVPREVPREV }
						any_neighbor_province = {
							owner = {
								OR = {
									is_liege_or_above = ROOT
									character = ROOT
								}
							}
						}
					}
					any_realm_province = {
						kingdom = { title = PREVPREVPREV }
						ROOT = { 
							any_realm_province = {
								NOT = { distance = { where = PREVPREV value = 75 } }
								owner = {
									OR = {
										is_liege_or_above = PREVPREV
										character = PREVPREV
									}
								}
							}
						}
					}
				}
			}
		}
	}
	
	is_valid_title = {
		FROM = {
			any_realm_title = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}
	
	on_add = {
		if = {
			limit = {
				ROOT = { has_ambition = obj_become_king }
			}
			ROOT = {
				custom_tooltip = { text = PAGAN_SUBJ_NO_MODIFIER }
			}
		}
		if = {
			limit = {
				ROOT = { NOT = { has_ambition = obj_become_king } }
			}
			ROOT = {
				custom_tooltip = { text = PAGAN_SUBJ_ADD_MODIFIER }
				hidden_tooltip = {
					add_character_modifier = { 
						name = launched_subjugation
						years = 100
						hidden = yes
					}
				}
			}
		}
	}

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			piety = 100
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		FROM = {
			piety = -100
			prestige = -200
			pf_glory_minus2_effect = yes
			hidden_tooltip = {
				opinion = {
					modifier = opinion_subjugated 
					who = ROOT
				}
				any_vassal = {
					opinion = {
						modifier = opinion_subjugated 
						who = ROOT
					}
				}
			}
			if = {
				limit = {
					is_reformed_religion = ROOT
					controls_religion = no
				}
				religion = ROOT # Reformed pagans are forced to convert back
			}
		}
	}
	
	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their pagan subjugation of the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		empty_province_effect = yes
		pf_liege_change_under_title_begin_effect = yes
		
		# 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
				pf_liege_change_effect = yes
			}
		}
		
		# 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
					}
				}
			}
		}
		
		pf_liege_change_under_title_end_effect = yes
	}
	
	on_invalidation = {
		ROOT = {
			remove_character_modifier = launched_subjugation
		}
	}

	on_fail = {
		FROM = { add_character_modifier = { name = victory_timer duration = 1 hidden = yes } }
		FROM = {
			piety = 50
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		ROOT = {
			piety = -100
		}
	}

	on_reverse_demand = {
		ROOT = {
			piety = -100
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
			pf_glory_minus2_effect = yes
		}
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			piety = 100
			pf_glory_plus2_effect = yes
		}
		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 = -1
	}
	
	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = { 
		factor = 1
		modifier = {
			factor = 0
			FROM = {
				NOT = {
					any_realm_province = {
						kingdom = { title = PREVPREVPREV }
						any_neighbor_province = { owner = { same_realm = ROOT } }
					}
				}
			}
			ROOT = {
				NOT = {
					any_realm_province = {
						TECH_NAVAL = 1
						port = yes
					}
				}
			}
		}
		modifier = {
			factor = 0
			FROM = {
				culture = hungarian
				has_global_flag = magyar_1066_invasion
			}
		}
		# 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 = {
				location = {
					NOR = {
						region = world_europe_south_east
						region = world_asia_minor
						region = world_middle_east
						region = world_persia
					}
				}
				FROM = {
					primary_title = { title = e_golden_horde }
					NOT = { year = 1300 }
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = { culture = hungarian }
			capital_scope = {
				OR = {
					region = custom_cuman
					region = custom_volga
					region = custom_alania
					region = custom_novgorod
					region = custom_muscovy
					region = custom_rus
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = { culture = khazar }
			capital_scope = {
				OR = {
					region = world_steppe_tarim
					region = world_steppe_east
					region = custom_cuman
					region = custom_perm
					region = custom_volga
				}
			}
		}
		modifier = {
			factor = 2.0
			ROOT = { culture = hungarian }
			capital_scope = {
				OR = {
					region = custom_bulgaria
					region = custom_carpathia
				}
			}
		}
	}
}

# The CB used by "Peasant revolter" characters
peasant_revolt = {
	name = CB_NAME_PEASANT_CONQUEST
	war_name = WAR_NAME_PEASANT_CONQUEST
	sprite = 16
	truce_days = 365
	hostile_against_others = yes
	check_all_titles = yes # 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
	is_independence = yes

	can_use = {
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		ROOT = {
			primary_title = { temporary = yes } # Revolter or adventurer trigger
			war = no
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}
	}

	can_use_title = {
		tier = count
	}

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

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their peasant revolt for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			hidden_tooltip = {
				wealth = 100
				if = {
					limit = { dynasty = none }
					dynasty = father_bastard
				}
			}
		}
		usurp_title_plus_barony_if_unlanded = { target = ROOT type = revolt }
		ROOT = { set_defacto_liege = ROOT }
		FROM = {
			prestige = -100
			clr_character_flag = has_current_revolt
		}
	}

	on_fail_title = {
		FROM = {
			clr_character_flag = has_current_revolt
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor
					who = ROOT
					months = 1200
				}
			}
		}
		ROOT = {
			imprison = FROM
			hidden_tooltip = {
				add_character_modifier = { 
					name = broken_spirit
					days = 3650
				}
			}
		}
	}

	on_reverse_demand = {
		FROM = { add_character_modifier = { name = victory_timer duration = 1 hidden = yes } }
		FROM = {
			prestige = 100
			clr_character_flag = has_current_revolt
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor
					who = ROOT
					months = 1200
				}
				add_character_modifier = { 
					name = crushed_major_revolt
					days = 3650
				}				
			}
		}
		ROOT = {
			imprison = FROM 
			hidden_tooltip = {
				add_character_modifier = { 
					name = broken_spirit
					days = 3650
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(peasant_revolt): INVALIDATED: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

# The CB used by "Heretic revolter" characters
heretic_revolt = {
	name = CB_NAME_HERETIC_UPRISING
	war_name = WAR_NAME_HERETIC_UPRISING
	sprite = 7
	truce_days = 365
	hostile_against_others = yes
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		ROOT = { has_character_flag = heretic_revolter }
	}
	
	can_use_title = {
		tier = COUNT
	}
	
	is_valid_title = {
		FROM = {
			NOT = { religion = ROOT }
			any_realm_province = {
				religion = ROOT
				duchy = {
					any_direct_de_jure_vassal_title = {
						title = PREVPREVPREVPREV
					}
				}
			}
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their heretic revolt for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			hidden_tooltip = {
				wealth = 100
				if = {
					limit = { dynasty = none }
					dynasty = father_bastard
				}
			}
			prestige = 200
			piety = 100
			occupy_minors_of_occupied_settlements = FROM
			gain_all_occupied_titles = { who = FROM type = revolt }
			clr_character_flag = heretic_revolter
		}
		
		dejure_liege_title  = {
			any_direct_de_jure_vassal_title = {
				limit = {
					tier = COUNT
					location = { religion = ROOT }
					holder_scope = { same_realm = FROM }
				}
				usurp_title_plus_barony_if_unlanded = { target = ROOT type = revolt }
			}
		}

		FROM = {
			prestige = -500
			piety = -500
			clr_character_flag = has_current_revolt
			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 = 3650
				}
			}
		}
		FROM = {
			prestige = 50
			piety = 100
			clr_character_flag = has_current_revolt
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor
					who = ROOT
					months = 1200
				}
			}
			religion_authority = {
				modifier = won_heretic_revolt
			}
		}
	}

	on_reverse_demand = {
		FROM = { add_character_modifier = { name = victory_timer duration = 1 hidden = yes } }
		ROOT = {
			imprison = FROM
			clr_character_flag = heretic_revolter
			hidden_tooltip = {
				add_character_modifier = { 
					name = broken_spirit
					days = 3650
				}
			}
		}
		FROM = {
			prestige = 100
			piety = 200
			clr_character_flag = has_current_revolt
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor
					who = ROOT
					months = 1200
				}
				add_character_modifier = { 
					name = crushed_major_revolt
					days = 3650
				}				
			}
			religion_authority = {
				modifier = won_heretic_revolt
			}
		}
	}

	on_reverse_demand_title = {
		hidden_tooltip = {
			location = {
				add_province_modifier = {
					name = rebellion_heretic_province
					duration = 5
				}
				province_event = { id = TOG.1032 days = 1 }
			}
		}
	}
	
	on_invalidation = {
		log = "CB(heretic_revolt): INVALIDATED: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 150
	}
}

# The CB used by "Religious revolter" characters
religious_revolt = {
	name = CB_NAME_HERETIC_UPRISING
	war_name = WAR_NAME_HERETIC_UPRISING
	sprite = 7
	truce_days = 365
	hostile_against_others = yes
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no
	
	can_use = {
		ROOT = { has_character_flag = religious_revolter }
	}
	
	can_use_title = {
		tier = COUNT
	}

	is_valid_title = {
		FROM = {
			NOT = { religion = ROOT }
			any_realm_province = {
				religion = ROOT
				duchy = {
					any_direct_de_jure_vassal_title = {
						title = PREVPREVPREVPREV
					}
				}
			}
		}
	}
 
	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their [Root.Religion.GetName] religious revolt for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			hidden_tooltip = {
				if = {
					limit = { dynasty = none }
					dynasty = father_bastard
				}
				wealth = 100
			}
			prestige = 200
			piety = 100
			occupy_minors_of_occupied_settlements = FROM
			gain_all_occupied_titles = { who = FROM type = revolt }
			clr_character_flag = religious_revolter
		}
		
		dejure_liege_title  = {
			any_direct_de_jure_vassal_title = {
				limit = {
					tier = COUNT
					location = { religion = ROOT }
					holder_scope = { same_realm = FROM }
				}
				usurp_title_plus_barony_if_unlanded = { target = ROOT type = revolt }
			}
		}
		
		FROM = {
			prestige = -500
			piety = -500
			clr_character_flag = has_current_revolt
			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 = 3650
				}
			}
		}
		FROM = {
			prestige = 50
			piety = 100
			clr_character_flag = has_current_revolt
			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 = 3650
				}
			}
		}
		FROM = {
			prestige = 100
			piety = 200
			clr_character_flag = has_current_revolt
			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
			}
		}
	}
	
	on_reverse_demand_title = {
		hidden_tooltip = {
			location = {
				add_province_modifier = {
					name = rebellion_religion_province
					duration = 5
				}
				province_event = { id = TOG.1072 days = 1 }
			}
		}
	}

	on_invalidation = {
		log = "CB(religious_revolt): INVALIDATED: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
	}
	
	attacker_ai_victory_worth = {
		factor = -1
	}
	
	attacker_ai_defeat_worth = {
		factor = 100
	}

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

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

	can_use = {
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		ROOT = { has_character_flag = nationalist_rebel }
	}

	can_use_title = {
		tier = king
		culture = ROOT
	}

	is_valid = {
		ROOT = {
			NOT = { culture = FROM }
			has_character_flag = nationalist_rebel
		}
	}

	is_valid_title = {
		FROM = {
			any_realm_title = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}
	
	on_success = {
		log = "CB(liberation_revolt): VICTORY: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
		
		# add victory modifier for the 'Win a War' ambition
		any_attacker = { add_character_modifier = { name = victory_timer duration = 1 hidden = yes } }
		
		ROOT = {
			# create new dynasty if the revolter is lowborn
			hidden_tooltip = {
				if = {
					limit = { dynasty = none }
					dynasty = father_bastard
				}
				if = {
					limit = { has_character_flag = began_revolt }
					wealth = 100
				}
			}
			# give liberator nickname
			if = {
				limit = { has_nickname = no }
				give_nickname = nick_the_liberator
			}
			prestige = 500
			hidden_tooltip = { disband_event_forces = nationalist_rebels }
		}
		FROM = {
			prestige = -500
			imperial_decadence_plus_1_effect = yes
			clr_character_flag = has_current_revolt
		}
		hidden_tooltip = {
			any_attacker = {
				limit = {
					has_character_flag = nationalist_rebel
					NOT = { character = ROOT }
					NOT = { is_liege_or_above = ROOT }
				}
				clr_character_flag = nationalist_rebel
				set_character_flag = will_vassalize
			}
			ROOT = {
				any_realm_lord = {
					limit = {
						has_character_flag = nationalist_rebel
						NOT = { character = PREV }
					}					
					clr_character_flag = nationalist_rebel
					set_character_flag = will_vassalize
				}
			}
			any_attacker = {
				limit = { NOT = { character = ROOT } }
				participation_scaled_prestige = 250
			}
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their [Root.Culture.GetName] liberation revolt of [This.GetName] against [From.GetBestName] (ID: [From.GetID])"
		#tooltips for the CB results
		ROOT = { custom_tooltip = { text = LIBERATION_TOOLTIP } }
		if = {
			limit = {
				has_holder = no
				any_previous_holder = { always = yes }
				ROOT = { NOT = { num_of_count_titles = 5 } }
			}
			ROOT = { custom_tooltip = { text = LIBERATION_TOOLTIP2 } }
		}
		any_attacker = {
			limit = {
				has_character_flag = nationalist_rebel
				NOT = { character = ROOT }
				NOT = { is_liege_or_above = ROOT }
			}
			custom_tooltip = { text = LIBERATION_TOOLTIP4 }
		}
		ROOT = {
			any_realm_lord = {
				limit = {
					has_character_flag = nationalist_rebel
					NOT = { character = PREV }
				}
				custom_tooltip = { text = LIBERATION_TOOLTIP4 }
			}
		}

		pf_liege_change_under_title_begin_effect = yes
		
		hidden_tooltip = {
			#usurp any titles in the kingdom held by the top liege or any vassal who is
			#his culture, not the attacker's culture group, or their capital is elsewhere
			any_de_jure_vassal_title = {
				limit = {
					tier = DUKE
					holder_scope = { same_realm = FROM }
				}
				if = {
					limit = {
						holder_scope = {
							OR = {
								character = FROM
								culture = FROM
								NOT = { culture_group = ROOT }
								NOT = { capital_scope = { kingdom = { title = PREVPREVPREVPREV } } }
							}
						}
					}
					set_title_flag = nationalist_usurp
				}
			}
			any_de_jure_vassal_title = {
				limit = {
					tier = COUNT
					holder_scope = { same_realm = FROM }
				}
				if = {
					limit = {
						holder_scope = {
							OR = {
								character = FROM
								culture = FROM
								NOT = { culture_group = ROOT }
								NOT = { capital_scope = { kingdom = { title = PREVPREVPREVPREV } } }
							}
						}
					}
					log = "----> [Root.GetBestName] usurps the [This.GetFullName] (Liberation Revolt)"
					usurp_title = { target = ROOT type = revolt }
				}
			}
			any_de_jure_vassal_title = {
				limit = {
					tier = BARON
					is_nomadic = no
					holder_scope = {
						OR = {
							same_realm = FROM
							character = ROOT
						}
					}
				}
				if = {
					limit = {
						holder_scope = {
							OR = {
								character = FROM
								culture = FROM
								NOT = { culture_group = ROOT }
								NOT = { capital_scope = { kingdom = { title = PREVPREVPREVPREV } } }
							}
						}
					}
					log = "----> [Root.GetBestName] usurps the [This.GetFullName] (Liberation Revolt)"
					usurp_title = { target = ROOT type = revolt }
				}
			}
			any_title = {
				limit = { has_title_flag = nationalist_usurp }
				clr_title_flag = nationalist_usurp
				log = "----> [Root.GetBestName] usurps the [This.GetFullName] (Liberation Revolt)"
				usurp_title = { target = ROOT type = revolt }
			}
		}
		ROOT = {
			#take kingdom title if it is owned by the top liege or a vassal
			if = {
				limit = {
					PREV = {
						has_holder = yes
						OR = {
							holder_scope = { character = FROM }
							holder_scope = { any_liege = { character = FROM } }
						}
					}
				}
				log = "----> [Root.GetBestName] usurps the [Prev.GetFullName] (Liberation Revolt)"
				usurp_title = { target = PREV type = revolt }
			}
			#take kingdom title if nobody holds it (but it was previously created)
			#and at least five counties are held
			if = {
				limit = {
					num_of_count_titles = 5
					PREV = {
						has_holder = no
						any_previous_holder = { always = yes }
					}
				}
				usurp_title = { target = PREV type = revolt }
			}
		}
		hidden_tooltip = {
			#vassalize any lower tier vassals of the same culture group
			any_de_jure_vassal_title = {
				limit = {
					holder_scope = {
						top_liege = { character = FROM }
						lower_real_tier_than = ROOT
						NOT = {
							any_liege = {
								culture_group = ROOT
								NOT = { character = FROM }
							}
						}
					}
				}
				holder_scope = {
					log = "----> [This.GetBestName] is vassalized by [Root.GetBestName] (Liberation Revolt)"
					set_defacto_liege = ROOT
				}
			}
			#usurp any vassals who cannot be vassalized
			any_de_jure_vassal_title = {
				limit = {
					holder_scope = {
						top_liege = { character = FROM }
						NOT = { lower_real_tier_than = ROOT }
					}
				}
				log = "----> [Root.GetBestName] usurps the [This.GetFullName] (Liberation Revolt)"
				usurp_title_plus_barony_if_unlanded = { target = ROOT type = revolt }
			}
		}
		ROOT = {
			set_defacto_liege = THIS
			clr_character_flag = nationalist_rebel
		}
		#vassalize any allies and set opinions
		hidden_tooltip = {
			any_attacker = {
				limit = {
					has_character_flag = will_vassalize
					NOT = { character = ROOT }
					NOT = { is_liege_or_above = ROOT }
				}
				log = "----> [This.GetBestName] is a fellow rebel and is thus vassalized to [Root.GetBestName] (Liberation Revolt)"
				clr_character_flag = will_vassalize
				if = {
					limit = { lower_real_tier_than = ROOT }
					opinion = { modifier = in_non_aggression_pact years = 20 who = ROOT origin_description = HELPED_WIN_REVOLT }
					reverse_opinion = { modifier = in_non_aggression_pact years = 20 who = ROOT origin_description = HELPED_WIN_REVOLT }
					set_defacto_liege = ROOT
				}
				if = {
					limit = { NOT = { lower_real_tier_than = ROOT } }
					add_alliance = { who = ROOT years = 20 }
				}
				reverse_opinion = { who = ROOT modifier = opinion_very_grateful years = 20 }
				opinion = { who = ROOT modifier = opinion_very_grateful years = 20 }
			}
			ROOT = {
				any_realm_lord = {
					limit = { has_character_flag = will_vassalize }
					log = "----> [This.GetBestName] is a fellow rebel and is thus vassalized to [Root.GetBestName] (Liberation Revolt)"
					clr_character_flag = will_vassalize
					if = {
						limit = { lower_real_tier_than = PREV }
						opinion = { modifier = in_non_aggression_pact years = 20 who = PREV origin_description = HELPED_WIN_REVOLT }
						reverse_opinion = { modifier = in_non_aggression_pact years = 20 who = PREV origin_description = HELPED_WIN_REVOLT }
						set_defacto_liege = PREV
					}
					if = {
						limit = { NOT = { lower_real_tier_than = PREV } }
						add_alliance = { who = PREV years = 20 }
					}
					reverse_opinion = { who = PREV modifier = opinion_very_grateful years = 20 }
					opinion = { who = PREV modifier = opinion_very_grateful years = 20 }
				}
			}
		}

		pf_liege_change_under_title_end_effect = yes

		ROOT = {
			hidden_tooltip = {
				# destroy their revolt title
				any_demesne_title = {
					limit = { temporary = yes }
					unsafe_destroy_landed_title = THIS
				}
				# make sure revolter doesn't have an unacceptable government type
				if = {
					limit = { has_character_flag = began_revolt }
					clr_character_flag = began_revolt
					landless_government_check_effect = yes
				}
			}
		}
	}

	on_fail = {
		log = "CB(liberation_revolt): WHITEPEACE: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
		
		# re-vassalize rebels
		hidden_tooltip = {
			any_attacker = {
				limit = {
					has_character_flag = nationalist_rebel
					NOT = { has_character_flag = began_revolt }
					NOT = { is_liege_or_above = ROOT }
					NOT = { character = ROOT }
				}
				clr_character_flag = nationalist_rebel
				participation_scaled_prestige = -200
				reverse_opinion = { who = FROM modifier = opinion_civil_war_loser months = 60 }
				opinion = { who = FROM modifier = opinion_coerced_into_leaving_faction months = 60 }
				if = {
					limit = { NOT = { has_any_opinion_modifier = liberation_revolt_against } }
					log = "----> [This.GetBestName] is a rebel who has been re-vasslized to [From.GetBestName] (Liberation Revolt)"
					set_defacto_liege = FROM
				}
				if = {
					limit = { has_any_opinion_modifier = liberation_revolt_against }
					random_opinion_modifier_target = {
						limit = { reverse_has_opinion_modifier = { who = PREV modifier = liberation_revolt_against } }
						log = "----> [Prev.GetBestName] is a rebel who has been re-vasslized to [This.GetBestName] (Liberation Revolt)"
						reverse_remove_opinion = { who = PREV modifier = liberation_revolt_against }
						set_defacto_vassal = PREV
					}
				}
			}
			ROOT = {
				# first re-vassalize vassals
				any_realm_lord = {
					limit = {
						has_character_flag = nationalist_rebel
						NOT = { has_character_flag = began_revolt }
					}
					clr_character_flag = nationalist_rebel
					if = {
						limit = { NOT = { has_any_opinion_modifier = liberation_revolt_against } }
						log = "----> [This.GetBestName] is a rebel who has been re-vasslized to [From.GetBestName] (Liberation Revolt)"
						set_defacto_liege = FROM
					}
					if = {
						limit = { has_any_opinion_modifier = liberation_revolt_against }
						random_opinion_modifier_target = {
							limit = { reverse_has_opinion_modifier = { who = PREV modifier = liberation_revolt_against } }
							log = "----> [Prev.GetBestName] is a rebel who has been re-vasslized to [This.GetBestName] (Liberation Revolt)"
							reverse_remove_opinion = { who = PREV modifier = liberation_revolt_against }
							set_defacto_vassal = PREV
						}
					}
				}
				# then destroy their revolt title
				any_demesne_title = {
					limit = { temporary = yes }
					unsafe_destroy_landed_title = THIS
				}					
				if = {
					limit = {
						has_character_flag = nationalist_rebel
						NOT = { has_character_flag = began_revolt }
					}
					clr_character_flag = nationalist_rebel
					if = {
						limit = { NOT = { has_any_opinion_modifier = liberation_revolt_against } }
						log = "----> [This.GetBestName] is a rebel who has been re-vasslized to [From.GetBestName] (Liberation Revolt)"
						set_defacto_liege = FROM
					}
					if = {
						limit = { has_any_opinion_modifier = liberation_revolt_against }
						random_opinion_modifier_target = {
							limit = { reverse_has_opinion_modifier = { who = PREV modifier = liberation_revolt_against } }
							log = "----> [Prev.GetBestName] is a rebel who has been re-vasslized to [This.GetBestName] (Liberation Revolt)"
							reverse_remove_opinion = { who = PREV modifier = liberation_revolt_against }
							set_defacto_vassal = PREV
						}
					}
				}
			}
		}
		
		FROM = {
			clr_character_flag = has_current_revolt
			prestige = 200
			hidden_tooltip = {
				opinion = { modifier = opinion_rebel_traitor who = ROOT months = 1200 }
			}
		}
		
		ROOT = {
			prestige = -200
			hidden_tooltip = {
				disband_event_forces = nationalist_rebels
				disband_event_forces = nationalist_rebels_1
			}
			if = {
				limit = { NOT = { has_character_flag = began_revolt } }
				random_courtier = {
					limit = { has_character_flag = began_revolt }
					clr_character_flag = began_revolt
					imprison = FROM
					hidden_tooltip = {
						reverse_opinion = { modifier = opinion_rebel_traitor who = FROM months = 1200 }
						add_character_modifier = { name = broken_spirit days = 3650 }
						remove_trait = inspiring_leader
					}
				}
			}
			if = {
				limit = { has_character_flag = began_revolt }
				imprison = FROM
				clr_character_flag = nationalist_rebel
				clr_character_flag = began_revolt
				hidden_tooltip = {
					reverse_opinion = { modifier = opinion_rebel_traitor who = FROM months = 1200 }
					add_character_modifier = { name = broken_spirit days = 3650 }
					remove_trait = inspiring_leader
				}
			}
		}
	}

	on_reverse_demand = {
		log = "CB(liberation_revolt): DEFEAT: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
		
		FROM = {
			clr_character_flag = has_current_revolt
			prestige = 200
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			hidden_tooltip = {
				opinion = { modifier = opinion_rebel_traitor who = ROOT months = 1200 }
				add_character_modifier = { name = crushed_major_revolt days = 3650 }
			}
		}
		
		# re-vassalize rebels
		hidden_tooltip = {
			any_attacker = {
				limit = {
					has_character_flag = nationalist_rebel
					NOT = { has_character_flag = began_revolt }
					NOT = { is_liege_or_above = ROOT }
					NOT = { character = ROOT }
				}
				clr_character_flag = nationalist_rebel
				participation_scaled_prestige = -200
				reverse_opinion = { who = FROM modifier = opinion_civil_war_loser months = 60 }
				opinion = { who = FROM modifier = opinion_coerced_into_leaving_faction months = 60 }
				imprison = FROM
				if = {
					limit = { NOT = { has_any_opinion_modifier = liberation_revolt_against } }
					log = "----> [This.GetBestName] is a rebel who has been re-vasslized to [From.GetBestName] (Liberation Revolt)"
					set_defacto_liege = FROM
				}
				if = {
					limit = { has_any_opinion_modifier = liberation_revolt_against }
					random_opinion_modifier_target = {
						limit = { reverse_has_opinion_modifier = { who = PREV modifier = liberation_revolt_against } }
						log = "----> [Prev.GetBestName] is a rebel who has been re-vasslized to [This.GetBestName] (Liberation Revolt)"
						reverse_remove_opinion = { who = PREV modifier = liberation_revolt_against }
						set_defacto_vassal = PREV
					}
				}
			}
			ROOT = {
				# first re-vassalize vassals
				any_realm_lord = {
					limit = {
						has_character_flag = nationalist_rebel
						NOT = { has_character_flag = began_revolt }
					}
					clr_character_flag = nationalist_rebel
					participation_scaled_prestige = -200
					reverse_opinion = { who = FROM modifier = opinion_civil_war_loser months = 60 }
					opinion = { who = FROM modifier = opinion_coerced_into_leaving_faction months = 60 }
					imprison = FROM
					if = {
						limit = { NOT = { has_any_opinion_modifier = liberation_revolt_against } }
						log = "----> [This.GetBestName] is a rebel who has been re-vasslized to [From.GetBestName] (Liberation Revolt)"
						set_defacto_liege = FROM
					}
					if = {
						limit = { has_any_opinion_modifier = liberation_revolt_against }
						random_opinion_modifier_target = {
							limit = { reverse_has_opinion_modifier = { who = PREV modifier = liberation_revolt_against } }
							log = "----> [Prev.GetBestName] is a rebel who has been re-vasslized to [This.GetBestName] (Liberation Revolt)"
							reverse_remove_opinion = { who = PREV modifier = liberation_revolt_against }
							set_defacto_vassal = PREV
						}
					}
				}
				# then destroy their revolt title
				any_demesne_title = {
					limit = { temporary = yes }
					unsafe_destroy_landed_title = THIS
				}					
				if = {
					limit = {
						has_character_flag = nationalist_rebel
						NOT = { has_character_flag = began_revolt }
					}
					clr_character_flag = nationalist_rebel
					reverse_opinion = { who = FROM modifier = opinion_civil_war_loser months = 60 }
					opinion = { who = FROM modifier = opinion_coerced_into_leaving_faction months = 60 }
					if = {
						limit = { NOT = { has_any_opinion_modifier = liberation_revolt_against } }
						log = "----> [This.GetBestName] is a rebel who has been re-vasslized to [From.GetBestName] (Liberation Revolt)"
						set_defacto_liege = FROM
					}
					if = {
						limit = { has_any_opinion_modifier = liberation_revolt_against }
						random_opinion_modifier_target = {
							limit = { reverse_has_opinion_modifier = { who = PREV modifier = liberation_revolt_against } }
							log = "----> [Prev.GetBestName] is a rebel who has been re-vasslized to [This.GetBestName] (Liberation Revolt)"
							reverse_remove_opinion = { who = PREV modifier = liberation_revolt_against }
							set_defacto_vassal = PREV
						}
					}
				}
			}
		}
		
		ROOT = {
			prestige = -200
			hidden_tooltip = {
				disband_event_forces = nationalist_rebels
				disband_event_forces = nationalist_rebels_1
			}
			if = {
				limit = { NOT = { has_character_flag = began_revolt } }
				clr_character_flag = nationalist_rebel
				imprison = FROM
				hidden_tooltip = {
					reverse_opinion = { who = FROM modifier = opinion_rebel_traitor months = 60 }
				}
				random_courtier = {
					limit = { has_character_flag = began_revolt }
					clr_character_flag = began_revolt
					imprison = FROM
					hidden_tooltip = {
						reverse_opinion = { modifier = opinion_rebel_traitor who = FROM months = 1200 }
						add_character_modifier = { name = broken_spirit days = 3650 }
						remove_trait = inspiring_leader
					}
				}
			}
			if = {
				limit = { has_character_flag = began_revolt }
				imprison = FROM
				clr_character_flag = nationalist_rebel
				clr_character_flag = began_revolt
				hidden_tooltip = {
					reverse_opinion = { modifier = opinion_rebel_traitor who = FROM months = 1200 }
					add_character_modifier = { name = broken_spirit days = 3650 }
					remove_trait = inspiring_leader
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(liberation_revolt): INVALIDATED: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
		
		# re-vassalize rebels
		hidden_tooltip = {
			any_attacker = {
				limit = {
					has_character_flag = nationalist_rebel
					NOT = { has_character_flag = began_revolt }
					NOT = { is_liege_or_above = ROOT }
					NOT = { character = ROOT }
				}
				clr_character_flag = nationalist_rebel
				participation_scaled_prestige = -200
				reverse_opinion = { who = FROM modifier = opinion_civil_war_loser months = 60 }
				opinion = { who = FROM modifier = opinion_coerced_into_leaving_faction months = 60 }
				if = {
					limit = { NOT = { has_any_opinion_modifier = liberation_revolt_against } }
					log = "----> [This.GetBestName] is a rebel who has been re-vasslized to [From.GetBestName] (Liberation Revolt)"
					set_defacto_liege = FROM
				}
				if = {
					limit = { has_any_opinion_modifier = liberation_revolt_against }
					random_opinion_modifier_target = {
						limit = { reverse_has_opinion_modifier = { who = PREV modifier = liberation_revolt_against } }
						log = "----> [Prev.GetBestName] is a rebel who has been re-vasslized to [This.GetBestName] (Liberation Revolt)"
						reverse_remove_opinion = { who = PREV modifier = liberation_revolt_against }
						set_defacto_vassal = PREV
					}
				}
			}
			ROOT = {
				# first re-vassalize vassals
				any_realm_lord = {
					limit = {
						has_character_flag = nationalist_rebel
						NOT = { has_character_flag = began_revolt }
					}
					clr_character_flag = nationalist_rebel
					if = {
						limit = { NOT = { has_any_opinion_modifier = liberation_revolt_against } }
						log = "----> [This.GetBestName] is a rebel who has been re-vasslized to [From.GetBestName] (Liberation Revolt)"
						set_defacto_liege = FROM
					}
					if = {
						limit = { has_any_opinion_modifier = liberation_revolt_against }
						random_opinion_modifier_target = {
							limit = { reverse_has_opinion_modifier = { who = PREV modifier = liberation_revolt_against } }
							log = "----> [Prev.GetBestName] is a rebel who has been re-vasslized to [This.GetBestName] (Liberation Revolt)"
							reverse_remove_opinion = { who = PREV modifier = liberation_revolt_against }
							set_defacto_vassal = PREV
						}
					}
				}
				# then destroy their revolt title
				any_demesne_title = {
					limit = { temporary = yes }
					unsafe_destroy_landed_title = THIS
				}					
				if = {
					limit = {
						has_character_flag = nationalist_rebel
						NOT = { has_character_flag = began_revolt }
					}
					clr_character_flag = nationalist_rebel
					if = {
						limit = { NOT = { has_any_opinion_modifier = liberation_revolt_against } }
						log = "----> [This.GetBestName] is a rebel who has been re-vasslized to [From.GetBestName] (Liberation Revolt)"
						set_defacto_liege = FROM
					}
					if = {
						limit = { has_any_opinion_modifier = liberation_revolt_against }
						random_opinion_modifier_target = {
							limit = { reverse_has_opinion_modifier = { who = PREV modifier = liberation_revolt_against } }
							log = "----> [Prev.GetBestName] is a rebel who has been re-vasslized to [This.GetBestName] (Liberation Revolt)"
							reverse_remove_opinion = { who = PREV modifier = liberation_revolt_against }
							set_defacto_vassal = PREV
						}
					}
				}
			}
		}
		
		FROM = {
			clr_character_flag = has_current_revolt
		}
		
		ROOT = {
			hidden_tooltip = {
				disband_event_forces = nationalist_rebels
				disband_event_forces = nationalist_rebels_1
			}
			if = {
				limit = { NOT = { has_character_flag = began_revolt } }
				random_courtier = {
					limit = { has_character_flag = began_revolt }
					clr_character_flag = began_revolt
				}
			}
			if = {
				limit = { has_character_flag = began_revolt }
				clr_character_flag = nationalist_rebel
				clr_character_flag = began_revolt
			}
		}
	}
	
	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

duchy_adventure = {
	name = CB_NAME_DUCHY_ADVENTURE
	war_name = WAR_DUCHY_ADVENTURE
	sprite = 9
	truce_days = 3650
	hostile_against_others = yes
	check_de_jure_tier = DUKE # this scans all dejure duchies for the counties which are held by or vassals(or below) of selected character.

	can_use = {
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		ROOT = { has_character_flag = duchy_adventurer }
	}

	is_valid = {
		always = yes
	}

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			prestige = 250
			hidden_tooltip = {
				wealth = 500
				clr_character_flag = duchy_adventurer
			}
		}
		FROM = {
			prestige = -250
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their duchy adventure for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		empty_province_effect = yes
		pf_liege_change_under_title_begin_effect = yes
		
		custom_tooltip = {
			text = other_invasion_succ_tip
			hidden_tooltip = {
				ROOT = {
					vassalize_or_take_under_title = {
						title = PREV
						enemy = FROM
						type = host
					}
				}
			}
		}
		
		pf_liege_change_under_title_end_effect = yes
		
		ROOT = {
			hidden_tooltip = {
				# Special case: Seljuk succeeds in his first conquest
				if = {
					limit = {
						has_character_flag = seljuk_himself
					}
					narrative_event = {
						id = 60206
						days = 1
					}
				}
				
				# Special case: Timur succeeds in his first conquest
				if = {
					limit = {
						has_character_flag = timur_himself
					}
					narrative_event = {
						id = 60306
						days = 1
					}
				}
				
				# Special case: Sabuktigin succeeds in his first conquest
				if = {
					limit = {
						has_character_flag = sabuktigin_himself
					}
					narrative_event = {
						id = 60406
						days = 1
					}
				}
				
				# Special case: Rollo succeeds in his invasion of Normandy
				if = {
					limit = {
						has_character_flag = is_rollo
					}
					character_event = {
						id = Plus.2752
						days = 1
					}
				}
			}
		}
		
		ROOT = {
			hidden_tooltip = {
				# destroy their revolt title
				any_demesne_title = {
					limit = { temporary = yes }
					unsafe_destroy_landed_title = THIS
				}
				# make sure revolter doesn't have an unacceptable government type
				landless_government_check_effect = yes
			}
		}
	}

	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 = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}

		FROM = {
			prestige = 250
			wealth = 100
		}

		ROOT = {
			imprison = FROM
			prestige = -250
			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
	}

	on_invalidation = {
		log = "CB(duchy_adventure): INVALIDATED: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
		modifier = {
			factor = 0.25
			ROOT = { has_character_flag = is_rollo }
		}
	}
}

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
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no
	
	can_use_gui = {
		ROOT = {
			piety = 50
		}
	}
	
	on_add = {
		ROOT = { piety = -50 }
	}
	
	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		ROOT = {
			mercenary = no
			is_nomadic = no # Use the 'pagan_county_conquest' CB instead
			independent = yes
			religion_group = muslim
			religion = FROM
			NOT = { has_character_modifier = county_conquest_timer }
			NOT = { is_liege_or_above = FROM }
		}
		ROOT = { #Special Cases
			NOR = {
				AND = {   
					has_landed_title = e_il-khanate
					religion = tengri_pagan
				} 
				AND = {   
					has_landed_title = e_golden_horde
					religion = tengri_pagan
				}       
				has_character_flag = horde_seljuk
				has_character_flag = horde_seljuk2
				has_character_flag = alp_arslan
				has_landed_title = e_timurids
			}
		}
	}

	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
				}
			}
		}
		
		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 = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their muslim county conquest for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}

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

			holder_scope = {
				set_viceroy_exempt_effect = yes
				set_defacto_liege = ROOT
				pf_liege_change_effect = yes
			}
		}
		if = {
			limit = {
				holder_scope = {
					OR = {
						higher_tier_than = count
						num_of_count_titles = 2
						NOT = { lower_tier_than = ROOT }
					}
				}
			}
			empty_province_effect = yes
			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 = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		FROM = {
			pf_glory_minus2_effect = yes
		}
	}

	on_fail_title = {
		ROOT = {
			prestige = -100
			hidden_tooltip = { add_character_modifier = { name = "county_conquest_timer" duration = 1825 } }
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		
		ROOT = {
			prestige = -200
			pf_glory_minus2_effect = yes
			if = {
				limit = {
					has_global_flag = hardcore_mode
					ROOT = { ai = no }
				}
				hidden_tooltip = { ROOT = { add_character_modifier = { name = "county_conquest_timer" duration = 3650 } } }
			}
			if = {
				limit = {
					NOT = { has_global_flag = hardcore_mode }
				}
				hidden_tooltip = { ROOT = { add_character_modifier = { name = "county_conquest_timer" duration = 1825 } } }
			}
			transfer_scaled_wealth = {
				to = FROM
				value = 4.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 = -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
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no
	
	allow_distant = yes
	
	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		ROOT = {
			mercenary = no
		}
		OR = {
			ROOT = {
				is_nomadic = yes
				NOT = { same_realm = FROM }
				FROM = {
					OR = {
						is_nomadic = yes
						is_tribal = yes
					}
				}
			}
			AND = {
				ROOT = {
					religion_group = pagan_group
					OR = {
						is_reformed_religion = no
						religion = titan_pagan
					}
					NOT = { same_realm = FROM }
				}
				FROM = {
					NOT = { religion = ROOT }
				}
			}
		}
		ROOT = { #Special Cases
			NOR = {
				AND = {   
					has_landed_title = e_il-khanate
					religion = tengri_pagan
				} 
				AND = {   
					has_landed_title = e_golden_horde
					religion = tengri_pagan
				}
				AND = {   
					has_landed_title = e_chagatai
					religion = tengri_pagan
				}
				AND = {   
					has_landed_title = e_mongol_empire
					religion = tengri_pagan
				} 
				has_character_flag = horde_seljuk
				has_character_flag = horde_seljuk2
				has_character_flag = alp_arslan
				has_landed_title = e_timurids
			}
		}
	}

	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 = {
			#either a seafaring culture targeting a port in the same sea zone area
			AND = {
				ROOT = {
					is_seafarer = yes
					NOT = { has_character_flag = no_invasions }
				}
				location = { port = yes }
				has_global_flag = viking_age_started
				OR = {
					AND = {
						ROOT = { any_realm_province = { region = custom_baltic_sea_zone } }
						location = { region = custom_baltic_sea_zone }
					}
					AND = {
						ROOT = { any_realm_province = { region = custom_north_sea_zone } }
						location = { region = custom_north_sea_zone }
					}
					AND = {
						ROOT = { any_realm_province = { region = custom_atlantic_zone } }
						location = { region = custom_atlantic_zone }
					}
					AND = {
						ROOT = { any_realm_province = { region = custom_west_med_zone } }
						location = { region = custom_west_med_zone }
					}
					AND = {
						ROOT = { any_realm_province = { region = custom_east_med_zone } }
						location = { region = custom_east_med_zone }
					}
					AND = {
						ROOT = { any_realm_province = { region = custom_black_sea_zone } }
						location = { region = custom_black_sea_zone }
					}
				}
			}
			#or it's a port within a fairly close range
			location = {
				port = yes
				ROOT = {
					any_realm_province = {
						NOT = {
							distance = {
								where = PREVPREV
								value = 75
							}
						}
					}
				}
			}
			#or it's neighboring
			location = {
				any_neighbor_province = {
					owner = {
						OR = {
							character = ROOT
							is_liege_or_above = ROOT
						}
					}
				}
			}
		}
	}

	is_valid_title = {
		OR = {
			ROOT = {
				is_nomadic = yes
			}
			AND = {
				ROOT = {
					religion_group = pagan_group
					OR = {
						is_reformed_religion = no
						religion = titan_pagan
					}
					NOT = { same_realm = FROM }
				}
				FROM = {
					NOT = { religion = ROOT }
				}
			}
		}
	
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their pagan county conquest of the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			pf_tradition_plus2_effect = yes
		}
		empty_province_effect = yes
		
		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
						is_liege_or_above = ROOT
					}
					OR = {
						NOT = { culture = ROOT }
						NOT = { religion = ROOT }
					}
				}
			}
			usurp_title = ROOT
		}
		
		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
			}
		}
		
		if = {
			limit = {
				NOT = { FROM = { religion = ROOT } }
			}
			ROOT = {
				religion_authority = {
					modifier = won_county_conquest
				}
			}
		}
		
		FROM = {
			pf_glory_minus2_effect = yes
		}
	}

	on_fail_title = {
		ROOT = {
			prestige = -100
			piety = -50
			hidden_tooltip = { add_character_modifier = { name = "county_conquest_timer" duration = 1825 } }
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		
		ROOT = {
			prestige = -200
			piety = -100
			pf_glory_minus2_effect = yes
			pf_tradition_minus2_effect = yes
			if = {
				limit = {
					has_global_flag = hardcore_mode
					ROOT = { ai = no }
				}
				hidden_tooltip = { ROOT = { add_character_modifier = { name = "county_conquest_timer" duration = 3650 } } }
			}
			if = {
				limit = {
					NOT = { has_global_flag = hardcore_mode }
				}
				hidden_tooltip = { ROOT = { add_character_modifier = { name = "county_conquest_timer" duration = 1825 } } }
			}
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		if = {
			limit = {
				NOT = { FROM = { religion = ROOT } }
			}
			ROOT = {
				religion_authority = {
					modifier = lost_county_conquest
				}
			}
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
			participation_scaled_piety = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_prestige = 200 
				participation_scaled_piety = 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
			FROM = {
				NOT = {
					any_realm_province = {
						county = { title = PREVPREVPREV }
						any_neighbor_province = { owner = { same_realm = ROOT } }
					}
				}
			}
			ROOT = {
				NOT = {
					any_realm_province = {
						TECH_NAVAL = 1
						port = yes
					}
				}
			}
		}
		modifier = {
			factor = 0
			location = {
				NOT = {
					any_neighbor_province = {
						owner = { same_realm = ROOT }
					}
				}
				OR = {
					region = custom_perm
					duchy = { title = d_kola }
					duchy = { title = d_karelia }
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = {
				is_seafarer = yes
				religion_group = FROM
			}
			location = {
				NOR = {
					region = custom_novgorod
					region = custom_muscovy
					region = custom_livonia
					region = custom_rus
					any_neighbor_province = {
						owner = { same_realm = ROOT }
					}
				}
			}
		}
		modifier = {
			factor = 1.5
			ROOT = { is_seafarer = yes }
			location = {
				OR = {
					region = custom_novgorod
					region = custom_livonia
					region = custom_rus
				}
			}
		}
		modifier = {
			factor = 0
			FROM = {
				culture = hungarian
				has_global_flag = magyar_1066_invasion
			}
		}
		modifier = {
			factor = 2.0
			ROOT = { culture_group = east_slavic }
			capital_scope = {
				OR = {
					region = custom_cuman
					region = custom_volga
					region = custom_alania
					region = custom_crimea
					region = custom_novgorod
					region = custom_muscovy
					region = custom_rus
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = { culture = hungarian }
			capital_scope = {
				OR = {
					region = custom_cuman
					region = custom_volga
					region = custom_alania
					region = custom_novgorod
					region = custom_muscovy
					region = custom_rus
				}
			}
		}
		modifier = {
			factor = 2.0
			ROOT = { culture = hungarian }
			capital_scope = {
				OR = {
					region = custom_bulgaria
					region = custom_carpathia
				}
			}
		}
	}
}

dejure_county_claim = { #ducal
	name = CB_NAME_DEJURECLAIM
	war_name = WAR_NAME_DEJURECLAIM
	sprite = 16
	truce_days = 3650
	hostile_against_others = yes
	is_permanent = yes
	#is_holy_war = 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 = yes
	
	infamy_modifier = 0.5
	
	allowed_to_target_tributaries = yes
	allowed_to_target_suzerains = no
	
	can_use_gui = {
		FROMFROM = {
			OR = {
				dejure_liege_title = {
					holder = ROOT
				}
				kingdom = {
					holder = ROOT
					custom_tooltip = {
						text = DE_JURE_TOOLTIP1
						hidden_tooltip = {
							OR = {
								has_law = crown_authority_2
								has_law = crown_authority_3
								has_law = crown_authority_4
							}
						}
					}
				}
				empire = {
					holder = ROOT
					custom_tooltip = {
						text = DE_JURE_TOOLTIP1
						hidden_tooltip = {
							OR = {
								has_law = crown_authority_2
								has_law = crown_authority_3
								has_law = crown_authority_4
							}
						}
					}
				}
			}
		}
	}

	can_use = {
		#check to ensure the target is not valid for a holy war
		NOT = {
			ROOT = {
				OR = {
					NOT = { religion_group = pagan_group }
					is_reformed_religion = yes
				}
				OR = {
					NOR = {
						religion = hindu
						religion = buddhist
					}
					FROM = { NOT = { religion_group = indian_group } }
				}
				OR = {
					NOT = { religion_group = FROM }
					is_heresy_of = FROM
					is_parent_religion = FROM
					AND = {
						OR = {
							religion_group = muslim
							religion_group = pagan_group
							religion = paulician
						}
						NOT = { religion = FROM }
					}
				}
				NOR = {
					pacifist = yes
					same_realm = FROM
				}
			}
		}
	}
	
	can_use_title = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { attacking_religious_head_trigger = no }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }

		tier = count
		
		#target owns the county or it belongs to their vassal
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
		#attacker holds either the de jure kingdom, empire or liege title
		OR = {
			empire = { holder = ROOT }
			kingdom = { holder = ROOT }
			dejure_liege_title = { holder = ROOT }
		}
		
		#attacker does not have the county title and the title holder is not their liege
		NOT = {
			OR = {
				ROOT = {
					has_landed_title = PREV
				}
				holder_scope = {
					is_liege_or_above = ROOT
				}
			}
		}
		#AI will only attack a holy order if they are a player, of a different religion or are a king/emperor
		OR = {
			ROOT = { ai = no }
			FROM = {
				OR = {
					holy_order = no
					higher_tier_than = duke
					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_add = {
		if = {
			limit = {
				FROM = {
					religion = ROOT
					holy_order = yes
				}
			}
			ROOT = { piety = -500 }
		}
		FROM = {
			attacked_tributary_cb_effect = yes
		}
	}

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		FROM = {
			pf_glory_minus2_effect = yes
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their de jure county claim on the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		#change imperial decadence
		if = {
			limit = { empire = { ROOT = { has_landed_title = PREV } } }
			ROOT = { imperial_decadence_minus_1_effect = yes }
		}
		
		empty_province_effect = 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 }
		}
	}

	on_fail = {
		prestige = -100
	}

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			pf_glory_minus2_effect = yes
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
	}

	on_reverse_demand_title = {
		#change imperial decadence
		if = {
			limit = { empire = { ROOT = { has_landed_title = PREV } } }
			ROOT = { imperial_decadence_plus_1_effect = yes }
		}
	}
	
	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = {
		factor = 1
		modifier = {
			factor = 0
			ROOT = {
				culture = mongol
				religion_group = pagan_group
			}
			FROM = {
				culture = mongol
				religion_group = pagan_group
			}
		}
		modifier = {
			factor = 0
			ROOT = {
				NOR = {
					trait = cynical
					trait = ambitious
				}
				rightful_religious_head_scope = { character = FROM }
			}
		}
		modifier = { # The AI will never breach a tributary's trust
			factor = 0
			ai_attack_tributary_trigger = yes
		}
	}
}

dejure_barony_claim = {
	name = CB_NAME_DEJUREBARONYCLAIM
	war_name = WAR_NAME_DEJUREBARONYCLAIM
	sprite = 16
	truce_days = 1825
	hostile_against_others = yes
	is_permanent = yes
	#is_holy_war = yes
	check_de_jure_tier = COUNT
	can_ask_to_join_war = yes
	
	infamy_modifier = 0.5
	
	allowed_to_target_tributaries = yes
	allowed_to_target_suzerains = no
	
	can_use = {
		#check to ensure the target is not valid for a holy war
		NOT = {
			ROOT = {
				OR = {
					NOT = { religion_group = pagan_group }
					is_reformed_religion = yes
				}
				OR = {
					NOR = {
						religion = hindu
						religion = buddhist
					}
					FROM = { NOT = { religion_group = indian_group } }
				}
				OR = {
					NOT = { religion_group = FROM }
					is_heresy_of = FROM
					is_parent_religion = FROM
					AND = {
						OR = {
							religion_group = muslim
							religion_group = pagan_group
							religion = paulician
						}
						NOT = { religion = FROM }
					}
				}
				NOR = {
					pacifist = yes
					same_realm = FROM
				}
			}
		}
		
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { attacking_religious_head_trigger = no }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		OR = {
			ROOT = { ai = no }
			FROM = {
				OR = {
					ai = no
					holy_order = no
					higher_tier_than = duke
					NOT = { religion = ROOT }
					NOT = { has_dlc = "Sons of Abraham" }
				}
			}
		}
	}

	can_use_title = {
		#de jure liege title belongs to attacker or a vassal
		has_holder = yes
		holder_scope = {
			OR = {
				character = ROOT
				is_liege_or_above = ROOT
			}
		}
		#at least one barony belongs to the target
		any_direct_de_jure_vassal_title = {
			holder_scope = {
				OR = {
					character = FROM
					is_liege_or_above = FROM
				}
				NOR = {
					character = ROOT
					is_liege_or_above = ROOT
				}
			}
			#not a pentarch title for my own religion
			can_attack_patriarch_trigger = yes
		}
	}

	on_add = {
		if = {
			limit = {
				FROM = {
					religion = PREV
					holy_order = yes
				}
			}
			piety = -500
		}
		FROM = {
			attacked_tributary_cb_effect = yes
		}
	}

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		FROM = {
			pf_glory_minus2_effect = yes
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their de jure barony claim on the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		holder_scope = {
			save_event_target_as = target_taker
		}	
		any_direct_de_jure_vassal_title = {
			limit = {
				holder_scope = {
					OR = {
						character = FROM
						is_liege_or_above = FROM
					}
					NOR = {
						character = event_target:target_taker
						is_liege_or_above = event_target:target_taker
						is_liege_or_above = ROOT
					}
				}
			}
			usurp_title_plus_barony_if_unlanded = { target = event_target:target_taker type = claim }
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_fail = {
		prestige = -50
	}

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -100
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			pf_glory_minus2_effect = yes
		}
		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
			ROOT = {
				NOR = {
					trait = cynical
					trait = ambitious
				}
				rightful_religious_head_scope = { character = FROM }
			}
		}
		modifier = { # The AI will never breach a tributary's trust
			factor = 0
			ai_attack_tributary_trigger = yes
		}
	}
}

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 = yes
	
	allowed_to_target_tributaries = yes
	allowed_to_target_suzerains = no
	
	infamy_modifier = 0.3
	
	can_use_gui = {
		FROMFROM = {
			OR = {
				dejure_liege_title = {
					holder = ROOT
				}
				kingdom = {
					holder = ROOT
					custom_tooltip = {
						text = DE_JURE_TOOLTIP1
						hidden_tooltip = {
							OR = {
								has_law = crown_authority_2
								has_law = crown_authority_3
								has_law = crown_authority_4
							}
						}
					}
				}
				empire = {
					holder = ROOT
					custom_tooltip = {
						text = DE_JURE_TOOLTIP1
						hidden_tooltip = {
							OR = {
								has_law = crown_authority_2
								has_law = crown_authority_3
								has_law = crown_authority_4
							}
						}
					}
				}
			}
		}
	}
	
	can_use = {
		#check to ensure the target is not valid for a holy war
		NOT = {
			AND = {
				OR = {
					NOT = { religion_group = pagan_group }
					is_reformed_religion = yes
				}
				OR = {
					NOR = {
						religion = hindu
						religion = buddhist
					}
					FROM = { NOT = { religion_group = indian_group } }
				}
				OR = {
					NOT = { religion_group = FROM }
					is_heresy_of = FROM
					is_parent_religion = FROM
					AND = {
						OR = {
							religion_group = muslim
							religion_group = pagan_group
							religion = paulician
						}
						NOT = { religion = FROM }
					}
				}
				NOR = {
					pacifist = yes
					same_realm = FROM
				}
			}
		}

		ai_not_busy_trigger = yes
		attacking_religious_head_trigger = no
		NOT = { has_character_modifier = holy_truce_timer }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		
		NOT = {
			character = ROOT
		}
		
		#supported ruler is a vassal
		ROOT = {
			any_liege = {
				character = PREVPREV
			}
		}		
		#AI will only attack a holy order if they are a player, of a different religion or are a king/emperor
		OR = {
			ai = no
			FROM = {
				OR = {
					ai = no
					holy_order = no
					higher_tier_than = duke
					NOT = { religion = PREV }
					NOT = { has_dlc = "Sons of Abraham" }
				}
			}
		}
	}

	can_use_title = {
		tier = count
		#supporter ruler owns the de jure liege kingdom, empire or title of the county
		OR = {
			empire = { holder = ROOT }
			kingdom = { holder = ROOT }
			dejure_liege_title = { holder = ROOT }
		}
		#supporter ruler does not own the county, and it is not owned by their liege
		holder_scope = {
			NOR = {
				character = ROOT
				character = FROMFROM
				is_liege_or_above = ROOT
				is_liege_or_above = FROMFROM # The potential attacker
			}
		}
		#target owns the county or it is owned by their vassal
		OR = {
			FROM = { has_landed_title = PREV }
			holder_scope = { is_liege_or_above = FROM }
		}
		OR = {
			ROOT = {
				NOT = { same_liege = FROM }
			}
			FROM = {
				any_demesne_title = {
					NOT = { title = PREVPREV }
					higher_tier_than = BARON
				}
			}
		}
	}

	is_valid_title = {
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
	}
	
	on_add = {
		if = {
			limit = {
				FROM = {
					religion = FROMFROM
					holy_order = yes
				}
			}
			FROMFROM = { piety = -500 }
		}
		FROM = {
			attacked_tributary_cb_effect = yes
		}
		ROOT = {
			set_character_flag = claim_being_pressed
		}
	}

	on_success = {
		ROOT = { add_character_modifier = { name = new_land_timer duration = 1 hidden = yes } }
		add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
		pf_glory_plus2_effect = yes
		ROOT = {
			opinion = {
				modifier = pressed_my_claim
				who = PREV
			}
			hidden_tooltip = {
				clr_character_flag = claim_being_pressed
			}
		}
		any_attacker = {
			limit = { character = PREV }
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = PREV } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		FROM = {
			pf_glory_minus2_effect = yes
		}
	}

	on_success_title = {
		log = "[FromFrom.GetBestName] (ID: [FromFrom.GetID]) succeeded with [Root.GetBestName]'s (ID: [Root.GetID]) de jure county claim for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		empty_province_effect = 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 }
		}
	}

	on_fail = {
		prestige = -100
		ROOT = {
			hidden_tooltip = {
				clr_character_flag = claim_being_pressed
			}
		}
	}
	
	on_invalidation = {
		ROOT = {
			hidden_tooltip = {
				clr_character_flag = claim_being_pressed
			}
		}
	}

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		prestige = -200
		transfer_scaled_wealth = {
			to = FROM
			value = 4.0
		}
		pf_glory_minus2_effect = yes
		ROOT = {
			prestige = -200
		}
		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 = -1 # always accept
	}
	
	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = {
		factor = 1
		modifier = {
			factor = 0
			ROOT = {
				culture = mongol
				religion_group = pagan_group
			}
			FROM = {
				culture = mongol
				religion_group = pagan_group
			}
		}
		modifier = { # The AI will never breach a tributary's trust
			factor = 0
			ai_attack_tributary_trigger = yes
		}
	}
}

# The special CB for the Rise of the Shi'a Caliphate
shia_caliphate_rising = {
	name = CB_NAME_SHIA_CALIPHATE_RISING
	war_name = WAR_NAME_SHIA_CALIPHATE_RISING
	sprite = 8
	truce_days = 365
	hostile_against_others = no
	check_de_jure_tier = KING # this scans all de jure kingdoms for the counties which are held by or vassals(or below) of selected character.
	apply_short_occ_mod = no # Do not apply the 'recently_conquered' modifier to Holdings

	can_use = {
		ROOT = {
			has_character_flag = shia_caliphate_pretender
		}
	}

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

	on_success = {
		clr_global_flag = shia_caliphate_revolt_ongoing
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			piety = 500
			religion_authority = {
				modifier = rise_of_the_shia_caliphate
			}
			clr_character_flag = shia_caliphate_pretender
		}
		FROM = {
			piety = -500
			imperial_decadence_plus_2_effect = yes
			religion_authority = {
				modifier = shia_victorious1
			}
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with shia caliphate rising for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		custom_tooltip = {
			text = other_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
					}
				}
				pf_liege_change_under_title_begin_effect = yes
				ROOT = {
					remove_trait = shiite_claimant
					occupy_minors_of_occupied_settlements = FROM
					gain_all_occupied_titles = { who = FROM type = revolt }
					if = {
						limit = {
							PREV = {
								OR = {
									has_holder = no
									holder_scope = {
										character = FROM
									}
								}
							}
						}
						usurp_title = { target = PREV type = revolt }
					}
					if = {
						limit = {
							NOT = {
								ROOT = {
									any_demesne_title = {
										tier = KING
									}
								}
							}
						}
						create_title = {
							tier = KING
							landless = no
							temporary = no
							custom_created = yes
							culture = ROOT
							holder = ROOT
							name = "SHIA_CUSTOM_FALLBACK"
							base_title = PREV
							copy_title_laws = yes
						}
					}
					vassalize_or_take_under_title = {
						title = PREV
						enemy = FROM
						is_religious = yes
						type = revolt
					}

					any_demesne_title = {
						limit = { tier = BARON }
						set_title_flag = refill_levy
					}
					character_event = { id = Plus.352 days = 3 } #Refill the levies
				}
				pf_liege_change_under_title_end_effect = yes
				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 = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			piety = 250
			imperial_decadence_minus_2_effect = yes
		}
		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
	}

	on_invalidation = {
		log = "CB(shia_caliphate_rising): INVALIDATED: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
		clr_global_flag = shia_caliphate_revolt_ongoing
		ROOT = {
			clr_character_flag = shia_caliphate_pretender
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	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
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no
	
	can_use = {
		ROOT = {
			religion = FROM
			OR = {
				religion = catholic
				religion = fraticelli
			}
			rightful_religious_head_scope = {
				NOT = { has_claim = k_papal_state }
				NOT = { has_claim = d_fraticelli }
			}
			mercenary = no
			OR = {
				ai = no
				NOT = { trait = cynical }
			}
		}
		FROM = {
			independent = yes
			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 = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their depose antipope CB against [From.GetBestName] (ID: [From.GetID])"
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		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
			pf_glory_minus2_effect = yes
			pf_tradition_minus2_effect = yes			
			rightful_religious_head_scope = {
				character_event = { id = Plus.350 tooltip = ANTIPOPE_DEPOSED }
			}
		}
	}

	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
			pf_glory_minus2_effect = yes
		}
		
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			piety = 500
			prestige = 250
			pf_glory_plus2_effect = yes
			pf_tradition_plus2_effect = yes
		}
	}

	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 = 5
			ROOT = { trait = zealous }
		}
	}
}

# 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_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
			}
		}
		FROM = {
			rightful_religious_head_scope = {
				is_liege_or_above = PREV
				OR = {
					has_claim = k_papal_state
					has_claim = d_fraticelli
				}
			}
		}
	}
	
	is_valid_title = {
		NOT = {
			holder_scope = {
				character = ROOT
			}
		}
	}
	
	is_valid = {
		ROOT = {
			religion = FROM
			OR = {
				religion = catholic
				religion = fraticelli
			}
		}
		FROM = {
			rightful_religious_head_scope = {
				is_liege_or_above = PREV
				OR = {
					has_claim = k_papal_state
					has_claim = d_fraticelli
				}
			}
		}
	}
	
	on_success = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their install antiking CB against [From.GetBestName] (ID: [From.GetID])"
		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 = {
			pf_glory_plus2_effect = yes
			pf_tradition_plus4_effect = yes
			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
			
			rightful_religious_head_scope = {
				character_event = { id = Plus.350 tooltip = ANTIPOPE_DEPOSED }
			}
			
			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
		}
		FROM = { 
			piety = 100
			prestige = 50
		}
	}

	on_reverse_demand = {
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -500
			prestige = -250
			prisoner = FROM
			pf_glory_minus2_effect = yes
			pf_tradition_minus2_effect = yes
		}
		
		FROM = { 
			piety = 500
			prestige = 250
			imperial_decadence_minus_1_effect = yes
			pf_glory_plus2_effect = yes
			pf_tradition_plus2_effect = yes
		}
	}

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

# 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 = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }	
		ROOT = {
			OR = {
				AND = {
					dynasty = 613 # Seljuks
					NOT = { year = 1200 }
					has_regent = no
				}
				AND = {
					dynasty = 800 #Timurids
					has_regent = no
				}
			}
		}
		
		ROOT = {
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}
	}
	
	can_use_title = {
		capital_scope = {
			OR = {
				region = custom_persia
				region = custom_baluchistan
				region = custom_afghanistan
				region = custom_mesopotamia
				region = custom_azerbaijan
				region = custom_armenia
				region = custom_trebizond
				region = custom_nikaea
				region = custom_syria
				region = custom_arabia
				region = custom_jerusalem
				region = custom_egypt
			}
		}
		
		# 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
		ROOT = {
			any_realm_province = {
				OR = {
					county = { de_jure_liege_or_above  = PREVPREVPREV }
					any_neighbor_province = {
						county = { de_jure_liege_or_above  = PREVPREVPREVPREV }
					}
				}
			}
		}
	}
	
	is_valid = {
		always = yes
	}
	
	is_valid_title = {
		FROM = {
			any_realm_title = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}

	on_success = {
		ROOT = {
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		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
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_prestige = 200 
				dynasty_head = {
					if = {
						limit = {
							uses_decadence = yes
							FROM = { NOT = { uses_decadence = yes } }
						}
						participation_scaled_decadence = -20
					}
				}
			}
		}
		FROM = {
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}
	}
	
	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their manifest destiny invasion for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		empty_province_effect = yes
		pf_liege_change_under_title_begin_effect = yes
		
		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
					}
				}
			}
		}
		
		pf_liege_change_under_title_end_effect = yes
	}

	on_fail = {
		FROM = {
			prestige = 100
		}
		FROM = {
			if = {
				limit = {
					has_dlc = "Legacy of Rome"
					OR = { 
						has_landed_title = e_byzantium
						has_landed_title = e_roman_empire
					}
					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 = -100
		}
	}

	on_reverse_demand = {
		FROM = {
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		FROM = {
			if = {
				limit = { 
					has_dlc = "Legacy of Rome"
					OR = { 
						has_landed_title = e_byzantium
						has_landed_title = e_roman_empire

					AND = {
							independent = yes
							higher_tier_than = COUNT
							OR = {
								culture = roman
								religion = hellenic_pagan
							}
						}
					}
					OR = {
						religion_group = christian
						religion_group = pagan_group
					}
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}
	
	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 = 5
			always = yes
		}
	}
}

# Decadence Usurption
cb_decadence_usurption = {
	name = CB_NAME_DECADENCE_USURPTION
	war_name = WAR_NAME_DECADENCE_USURPTION
	sprite = 11
	truce_days = 3650
	is_permanent = yes
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no
	
	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		ROOT = { 
			religion_group = muslim
		}	
	
		FROM = {
			religion = ROOT
			trait = decadent
			dynasty = ROOT
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					ROOT = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
		ROOT = {
			holy_order = no
			mercenary = no
		}
	}
	
	is_valid = {
		FROM = {
			trait = decadent
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					ROOT = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}
	
	is_valid_title = {
		ROOT = { has_landed_title = PREV }
	}
	
	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		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 = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their decadence usurpation against the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		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 = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
			piety = -500
			prestige = -250
			pf_glory_minus2_effect = yes
		}
		
		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
	}
}

# Rivalry CB
rivalry_cb = {
	name = CB_NAME_RIVALRY
	war_name = WAR_NAME_RIVALRY
	sprite = 7
	truce_days = 3650
	is_permanent = yes
	can_ask_to_join_war = no
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no
	
	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { attacking_religious_head_trigger = no }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = {
			NOT = {
				has_opinion_modifier = {
					who = ROOT
					modifier = opinion_humiliated_rival
				}
			}
			OR = {
				is_rival = ROOT
				is_foe = ROOT
				AND = {
					OR = {
						has_plot = plot_kill_character
						has_plot = plot_kill_spouse
						has_plot = plot_take_revenge
					}
					plot_is_known_by = ROOT
					plot_target_char = {
						OR = {
							character = ROOT
							is_child_of = ROOT
							is_mother = ROOT
							is_father = ROOT
							is_married = ROOT
						}
					}
				}
			}
		}
	}
	
	is_valid = {
		FROM = {
			OR = {
				is_rival = ROOT
				is_foe = ROOT
				AND = {
					OR = {
						has_plot = plot_kill_character
						has_plot = plot_kill_spouse
						has_plot = plot_take_revenge
					}
					plot_is_known_by = ROOT
					plot_target_char = {
						OR = {
							character = ROOT
							is_child_of = ROOT
							is_mother = ROOT
							is_father = ROOT
							is_married = ROOT
						}
					}
				}
			}
		}
	}
	
	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}		
		
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 250
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_prestige = 250
			}
		}
		
		ROOT = {
			if = {
				limit = {
					ai = no
					FROM = {
						any_sibling = {
							character = ROOT
						}
					}
				}
				set_character_flag = achievement_sibling_rival_war
			}
			remove_opinion = {
				who = FROM
				modifier = opinion_humiliated_rival
			}
		}
		
		FROM = { 
			prestige = -250
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
			
			imprison = ROOT
			opinion = {
				modifier = opinion_humiliated_rival
				who = ROOT
				years = 150
			}
		}
	}

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

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 250
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_prestige = 250
			}
		}
		ROOT = {
			prestige = -250
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
			imprison = FROM
			opinion = {
				modifier = opinion_humiliated_rival
				who = FROM
				years = 150
			}
		}
		
		FROM = { 
			remove_opinion = {
				who = ROOT
				modifier = opinion_humiliated_rival
			}
			if = {
				limit = {
					ai = no
					ROOT = {
						any_sibling = {
							character = PREVPREV
						}
					}
				}
				set_character_flag = achievement_sibling_rival_war
			}
		}
	}

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

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

# Tributary CB
tributary_cb = {
	name = CB_NAME_TRIBUTARY
	war_name = WAR_NAME_TRIBUTARY
	sprite = 1
	truce_days = 365
	is_permanent = no
	hostile_against_others = yes
	can_ask_to_join_war = no
	display_on_map = no
	coalition_threat = no
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no

	can_use = {
		ROOT = {
			has_character_flag = trying_to_gain_tributary
		}
	}

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

	on_success = {
		FROM = {
			save_event_target_as = target_loser
			prestige = -200
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
			custom_tooltip = {
				text = "REMOVE_PREVIOUS_SUZERAIN"
				hidden_tooltip = {
					any_suzerain = {
						event_target:target_loser = {
							remove_tributary = PREV
						}
					}
				}
			}
		}

		ROOT = {
			prestige = 200
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			make_tributary = { who = FROM percentage = 0.3 }
		}
		
		if  = {
			limit = {
				ROOT = {
					uses_decadence = yes
				}
			}
			ROOT = {
				decadence = -10
			}
		}
		if  = {
			limit = {
				ROOT = {
					is_nomadic = yes
				}
			}
			ROOT = {
				clan_title = {
					ROOT = {
						any_vassal = {
							limit = { is_nomadic = yes }
							clan_title = {
								clan_opinion = {
									who = PREVPREVPREV
									modifier = won_tributary_war
									years = 5
								}
							}
						}
					}
				}
			}
		}
		
		if  = {
			limit = {
				ROOT = {
					is_tribal = yes
				}
			}
			ROOT = {
				any_vassal = {
					limit = { is_tribal = yes }
					opinion = {
						who = PREV
						modifier = won_tributary_war
						years = 5
					}
				}
			}
		}
	}

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

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

		FROM = {
			prestige = 200
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
	}
	
	on_attacker_leader_death = {
		end_war = invalid
	}

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

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
		
		modifier = {
			factor = 2
			FROM = {
				trait = proud
			}
		}
		
		modifier = {
			factor = 2
			FROM = {
				trait = brave
			}
		}
		
		modifier = {
			factor = 2
			FROM = {
				trait = greedy
			}
		}
		
		modifier = {
			factor = 3
			FROM = {
				tier = KING
			}
		}

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

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

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

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

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

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

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

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

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

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

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

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

	ai_will_do = {
		factor = 0
	}
}

# Free Tributary CB
free_tributary_cb = {
	name = CB_NAME_FREE_TRIBUTARY
	war_name = WAR_NAME_FREE_TRIBUTARY
	sprite = 1
	truce_days = 365
	is_permanent = yes
	hostile_against_others = yes
	can_ask_to_join_war = no

	can_use = {
		has_dlc = "Horse Lords"
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }			
		ROOT = {
			OR = {
				AND = {
					is_tributary = yes
					suzerain = {
						OR = {
							character = FROM
							any_liege = {
								character = FROM
							}
						}
					}
				}
				any_vassal = {
					is_tributary = yes
					suzerain = {
						OR = {
							character = FROM
							any_liege = {
								character = FROM
							}
						}
					}
				}
			}
		}
	}

	is_valid = {
		ROOT = {
			OR = {
				AND = {
					is_tributary = yes
					suzerain = {
						OR = {
							character = FROM
							any_liege = {
								character = FROM
							}
						}
					}
				}
				any_vassal = {
					is_tributary = yes
					suzerain = {
						OR = {
							character = FROM
							any_liege = {
								character = FROM
							}
						}
					}
				}
			}
		}
	}

	on_success = {
		FROM = {
			prestige = -200
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}

		ROOT = {
			prestige = 200
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			if = {
				limit = {
					is_tributary = yes
					suzerain = {
						OR = {
							character = FROM
							any_liege = {
								character = FROM
							}
						}
					}
				}
				suzerain = {
					remove_tributary = PREV
				}
			}
			any_vassal = {
				limit = {
					is_tributary = yes
					suzerain = {
						OR = {
							character = FROM
							any_liege = {
								character = FROM
							}
						}
					}
				}
				suzerain = {
					remove_tributary = PREV
				}
			}
		}
	}

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

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

		FROM = {
			prestige = 200
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
	}
	
	on_attacker_leader_death = {
		end_war = invalid
	}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	ai_will_do = { 
		factor = 1
		
		modifier = {
			factor = 1.5
		}

		modifier = {
			factor = 4
			ROOT = {
				trait = greedy
			}
		}
		
		modifier = {
			factor = 2
			OR = {
				ROOT = {
					is_nomadic = yes
				}
				FROM = {
					is_nomadic = no
				}
			}
		}
	}
}

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

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

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

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		
		FROM = {
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
			any_demesne_title = {
				limit = { tier = EMPEROR }
				gain_title = { target = ROOT type = faction_demand }
			}
			death = {
				death_reason = death_execution
				killer = 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 = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -200
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
			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
	}
	
	on_defender_leader_death = {
		hidden_tooltip = {
			any_attacker = {
				letter_event = {
					id = 251
				}
			}
		}
		
		end_war = invalid
	}

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

# Nomad Dispute CB
nomad_dispute = {
	name = CB_NAME_DISPUTE
	war_name = CB_NAME_DISPUTE
	sprite = 16
	truce_days = 1095
	hostile_against_others = yes
	is_permanent = yes
	check_all_titles = yes
	press_claim = yes
	can_ask_to_join_war = no
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no
	
	allow_distant = yes
	
	can_use = {
		has_dlc = "Horse Lords"
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }	
		FROM = {
			is_nomadic = yes
		}
		ROOT = {
			is_nomadic = yes
			same_realm = FROM
			has_feud_with = FROM
		}
		
		ROOT = {
			mercenary = no
		}
	}

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

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

	on_success_title = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}		
		
		empty_province_effect = yes
		usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
		
		FROM = {
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}
		
		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
			}
		}
	}

	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 = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		
		ROOT = {
			prestige = -200
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		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
	}
	
	ai_will_do = {
		factor = 1
		modifier = {
			factor = 2
			always = yes
		}
	}
	
}

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

	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = {
			is_nomadic = yes
		}
		ROOT = {
			is_nomadic = no
			NOT = { pacifist = yes }
			NOT = { same_realm = FROM }
			mercenary = no
		}
		#use pagan county conquest instead
		NAND = {
			ROOT = {
				NOT = { has_character_modifier = county_conquest_timer }
				religion_group = pagan_group
				OR = {
					is_reformed_religion = no
					religion = titan_pagan
				}
				NOT = { same_realm = FROM }
			}
			FROM = {
				NOT = { religion = ROOT }
			}
		}
		#use muslim county conquest instead
		NOT = {
			ROOT = {
				religion_group = muslim
				religion = FROM
				NOT = { has_character_modifier = county_conquest_timer }
			}
		}
	}
	
	can_use_title = {
		tier = count
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
		
		# Needs a land border
		location = {
			any_neighbor_province = {
				owner = {
					OR = {
						character = ROOT
						is_liege_or_above = ROOT
					}
				}
			}
		}
	}

	is_valid = {
		FROM = {
			is_nomadic = yes
		}
		ROOT = {
			is_nomadic = no
			NOT = { pacifist = yes }
			NOT = { same_realm = FROM }
			mercenary = no
		}
	}
	
	is_valid_title = {
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
		
		# Needs a land border
		location = {
			any_neighbor_province = {
				owner = {
					OR = {
						character = ROOT
						is_liege_or_above = ROOT
					}
				}
			}
		}
	}
	
	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_prestige = 200
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
			}
		}
		
		FROM = {
			prestige = -200
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}
	}

	on_success_title = {
		empty_province_effect = yes
		pf_liege_change_under_title_begin_effect = yes
		
		ROOT = {
			vassalize_or_take_under_title = {
				title = PREV
				enemy = FROM
				anti_nomad = yes # Vassalize non-nomadic rulers
				type = invasion
			}
		}
		usurp_title = { target = ROOT type = invasion }
		
		pf_liege_change_under_title_end_effect = yes
	}

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

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -200
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				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
	}
	
	ai_will_do = {
		factor = 1
		modifier = {
			factor = 0
			ROOT = { is_tribal = no }
			NOR = {
				ROOT = { trait = ambitious }
				location = { culture = ROOT }
				any_direct_de_jure_vassal_title = {
					OR = {
						AND = {
							holding_type = temple
							holder_scope = { religion = ROOT }
						}
						holding_type = castle
						holding_type = city
					}
				}
			}
		}
		modifier = {
			factor = 0
			location = { region = world_steppe_west }
			ROOT = {
				OR = {
					has_landed_title = e_byzantium
					any_liege = { has_landed_title = e_byzantium }
				}
			}
		}
		modifier = {
			factor = 0
			e_placeholder = {
				NOT = { had_title_flag = { flag = startup days = 365 } }
			}
		}
	}
}

# The CB used by "Minor Clan" revolters in Horse Lords
cb_minor_clan_revolt = {
	name = CB_NAME_MINOR_CLAN_REVOLT
	war_name = WAR_NAME_MINOR_CLAN_REVOLT
	sprite = 16
	truce_days = 365
	hostile_against_others = no
	is_permanent = yes
	check_all_titles = yes # if permanent, setting this to true will check against all of someones titles, including vassal held titles
	press_claim = yes
	
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no
	
	is_independence = yes
	
	can_use = {
		ROOT = {
			primary_title = { temporary = yes } # Revolter or adventurer trigger
			war = no
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}
	}

	can_use_title = {
		tier = count
	}

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

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their minor clan revolt against [From.GetBestName] (ID: [From.GetID])"
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}	
		
		usurp_title_plus_barony_if_unlanded = { target = ROOT type = revolt }
	
		ROOT = {
			primary_title = {
				set_title_nomad = { title = THIS status = no }
			}
			switch_to_feudal_effect = yes
		}
		ROOT = {		
			set_government_type = nomadic_government
			set_defacto_liege = FROM
			
			hidden_tooltip = {
				wealth = 100
				remove_opinion = {
					who = FROM
					modifier = opinion_evil_tyrant
				}
			}
		}
		FROM = {
			clr_character_flag = has_current_revolt
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
			prestige = -100
		}
	}

	on_fail_title = {
		FROM = {
			clr_character_flag = has_current_revolt
			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 = {
			clr_character_flag = has_current_revolt
			prestige = 20
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			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
				}
			}
		}
	}
	
	on_attacker_leader_death = {
		end_war = invalid
	}

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

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

# Nomads can subjugate non-Nomads
nomad_subjugation = {
	name = CB_NAME_PAGAN_SUBJUGATION
	war_name = WAR_NAME_PAGAN_SUBJUGATION
	sprite = 8
	truce_days = 365
	is_permanent = yes
	can_ask_to_join_war = yes
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no
	
	can_use_gui = {
	#higher_tier_than between ROOT and FROM would be better but its tooltip doesn't say anything about comparing character tiers
		ROOT = {
			independent = yes
			OR = {
				prestige = 500
				has_opinion_modifier = {
					modifier = declared_independence_nomad
					who = FROM
				}
			}
		}
		OR = {
			ROOT = { higher_tier_than = king }
			NOT = { FROM = { tier = emperor } }
		}
		FROM = {
			is_nomadic = no
			OR = {
				AND = {
					is_tribal = yes
					ROOT = { population = 2500 }
				}
				ROOT = { population = 10000 }
				has_opinion_modifier = {
					modifier = declared_independence_nomad
					who = FROM
				}
			}
		}
	}
	
	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }	
		ROOT = {
			is_nomadic = yes
			NOT = { same_realm = FROM }
			NOT = { is_liege_or_above = FROM }
			mercenary = no
			OR = {
				NOT = { has_character_modifier = launched_subjugation }
				has_opinion_modifier = {
					modifier = declared_independence_nomad
					who = FROM
				}
			}
		}
		
		FROM = {
			in_revolt = no
			is_nomadic = no # Nomads cannot be subjugated
			NOT = {
				any_war = {
					defender = { character = PREVPREV }
					OR = {
						using_cb = nomad_subjugation
						using_cb = pagan_subjugation
					}
				}
			}
			OR = {
				NOT = { tier = EMPEROR }
				ROOT = { ai = no }
			}
		}
	}
	
	on_add = {
		ROOT = {
			if = {
				limit = {
					NOT = {
						has_opinion_modifier = {
							modifier = declared_independence_nomad
							who = FROM
						}
					}
				}
				prestige = -500
				add_character_modifier = { 
					name = launched_subjugation
					years = 100
					hidden = yes
				}
			}
		}
	}

	on_success = {
		hidden_tooltip = {
			ROOT = {
				if = {
					limit = {
						has_nickname = no
					}
					FROM = {
						save_event_target_as = nickname_target
					}
					ROOT = { 
						save_event_target_as = nickname_receiver
					}
					random_list = {
						2 = { give_nickname = nick_the_eastern_wind }
						5 = { give_nickname = nick_the_despoiler }
						5 = { 
							give_nickname = nick_the_bane_of_realmname
							modifier = {
								factor = 0
								event_target:nickname_target = { is_nomadic = yes }
							}
							modifier = {
								factor = 0
								event_target:nickname_target = { culture = ROOT }
							}
						}
						5 = { 
							give_nickname = nick_the_culture_marauder
							modifier = {
								factor = 0
								event_target:nickname_target = { is_nomadic = yes }
							}
							modifier = {
								factor = 0
								event_target:nickname_target = { culture = ROOT }
							}
						}
						100 = { }
					}
				}
			}
		}
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			piety = 100
			prestige = 300
		}
		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
			}
			
			custom_tooltip = {
				text = tribal_subjugation_tip
			}
				
			hidden_tooltip = {
				opinion = {
					modifier = opinion_subjugated 
					who = ROOT
				}
				any_vassal = {
					opinion = {
						modifier = opinion_subjugated 
						who = ROOT
					}
				}
				
				any_demesne_title = {
					limit = {
						higher_tier_than = ROOT
						NOT = { controls_religion = yes }
					}
					ROOT = { grant_title_no_opinion = { target = PREV type = invasion } }
					add_pressed_claim = PREV
				}
				
				any_demesne_title = {
					limit = {
						tier = ROOT
						NOT = { controls_religion = yes }
					}
					ROOT = { grant_title_no_opinion = { target = PREV type = invasion } }
					add_pressed_claim = PREV
				}
				if = {
					limit = {
						NOT = {
							any_demesne_title = {
								controls_religion = yes
							}
						}
					}
					set_defacto_liege = ROOT
					pf_liege_change_effect = yes
					break = yes
				}
				any_vassal = {
					set_defacto_liege = ROOT
					pf_liege_change_effect = yes
				}
				any_demesne_title = {
					limit = {
						NOT = { controls_religion = yes }
					}
					ROOT = {
						grant_title_no_opinion = { target = PREV type = invasion }
					}
				}
			}
		}
	}
	
	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_invalidation = {
		if = {
			limit = {
				NOT = {
					has_opinion_modifier = {
						modifier = declared_independence_nomad
						who = FROM
					}
				}
			}
			ROOT = {
				remove_character_modifier = launched_subjugation
			}
		}	
	}

	on_reverse_demand = {
		ROOT = {
			piety = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
		}
		FROM = {
			piety = 100
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
		}
		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 = 10
	}
} 

# Nomad Invasion
nomad_invasion = {
	name = CB_NAME_INVASION
	war_name = WAR_NAME_INVASION
	sprite = 8
	truce_days = 3650
	hostile_against_others = yes
	is_permanent = yes
	is_holy_war = yes
	can_ask_to_join_war = yes
	check_de_jure_tier = KING # this scans all de jure kingdoms for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no

	can_use_gui = {
		ROOT = {
			prestige = 500
			population = 30000
			population_factor = 0.75 # 75% of max
		}
	}
	
	on_add = {
		ROOT = { prestige = -500 }
	}

	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }	
		ROOT = {
			is_nomadic = yes
		}
		
		ROOT = {
			NOT = { same_realm = FROM }
		}
	}
	
	is_valid_title = {
		FROM = {
			any_realm_title = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			prestige = 500
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 500 }
		}
		FROM = {
			prestige = -250
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}
		hidden_tooltip = {
			ROOT = {
				if = {
					limit = {
						has_nickname = no
					}
					FROM = {
						save_event_target_as = nickname_target
					}
					ROOT = { 
						save_event_target_as = nickname_receiver
					}
					random_list = {
						5 = { give_nickname = nick_the_great }
						5 = { give_nickname = nick_the_victorious }
						5 = { give_nickname = nick_the_conqueror }
						5 = { give_nickname = nick_the_brave }
						10 = { give_nickname = nick_the_dragon }
						10 = { give_nickname = nick_the_terrible }
						10 = { give_nickname = nick_the_eastern_wind }
						25 = { give_nickname = nick_the_despoiler }
						25 = { 
							give_nickname = nick_the_culture_marauder
							modifier = {
								factor = 0
								event_target:nickname_target = { is_nomadic = yes }
							}
							modifier = {
								factor = 0
								event_target:nickname_target = { culture = ROOT }
							}
						}
						50 = { 
							give_nickname = nick_the_bane_of_realmname
							modifier = {
								factor = 0
								event_target:nickname_target = { is_nomadic = yes }
							}
							modifier = {
								factor = 0
								event_target:nickname_target = { culture = ROOT }
							}
						}
						50 = { 
							give_nickname = nick_the_slayer_of_culture
							modifier = {
								factor = 0
								event_target:nickname_target = { culture = ROOT }
							}
						}
						50 = { 
							give_nickname = nick_the_scourge_of_god
							modifier = {
								factor = 0
								event_target:nickname_target = { religion = ROOT }
							}
						}
					}
				}
			}
		}
	}
	
	on_success_title = {
		empty_province_effect = yes
		pf_liege_change_under_title_begin_effect = yes
		hidden_tooltip = {
			ROOT = {
				occupy_minors_of_occupied_settlements = FROM
				gain_all_occupied_titles = { who = FROM type = invasion }
			}
		}
		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
					}
				}
			}
		}
		pf_liege_change_under_title_end_effect = yes
	}

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

	on_reverse_demand = {
		ROOT = {
			prestige = -250
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			prestige = 250
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			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 = 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
	}
	
	ai_will_do = { 
		factor = 8
		
		modifier = {
			factor = 0.9 # Prefer using CB's without on_add costs
		}
		
		# Try to guide the Hordes in the right direction
		modifier = {
			factor = 0.1
			ROOT = {
				OR = {
					dynasty = 613 # Seljuks
					dynasty = 800 # Timurids
					primary_title = { title = e_il-khanate }
				}
			}
			OR = {
				capital_scope = {
					NOR = {
						region = world_europe_south_east
						region = world_asia_minor
						region = world_middle_east
						region = world_persia
					}
				}
				FROM = {
					primary_title = { title = e_golden_horde }
					NOT = { year = 1300 }
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = {
				primary_title = { title = e_golden_horde }
			}
			OR = {
				capital_scope = {
					OR = {
						region = world_europe_south_east
						region = world_asia_minor
						region = world_middle_east
						region = world_persia
					}
				}
				FROM = {
					primary_title = { title = e_il-khanate }
					NOT = { year = 1300 }
				}
			}
		}
		modifier = {
			factor = 0
			ROOT = { culture = hungarian }
			capital_scope = {
				OR = {
					region = custom_cuman
					region = custom_volga
					region = custom_alania
					region = custom_novgorod
					region = custom_muscovy
					region = custom_rus
				}
			}
		}
		modifier = {
			factor = 0
			ROOT = { culture = khazar }
			capital_scope = {
				OR = {
					region = world_steppe_tarim
					region = world_steppe_east
					region = custom_cuman
					region = custom_perm
					region = custom_volga
				}
			}
		}
		modifier = {
			factor = 2.0
			ROOT = { culture = hungarian }
			capital_scope = {
				OR = {
					region = custom_bulgaria
					region = custom_carpathia
				}
			}
		}
	}
}

# Nomads can grab entire duchies, if they have the Prestige
nomad_expansion = {
	name = CB_NAME_NOMAD_EXPANSION
	war_name = WAR_NAME_NOMAD_EXPANSION
	sprite = 8
	truce_days = 3650
	is_permanent = yes
	is_holy_war = yes
	check_de_jure_tier = DUKE # this scans all de jure duchies for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no

	can_use_gui = {
		ROOT = {
			prestige = 250
			population_factor = 0.5
		}
		FROM = {
			OR = {
				is_nomadic = yes # Nomads no population requirement
				AND = {
					is_tribal = yes
					ROOT = { population = 5000 } # Tribals need 5k
				}
				ROOT = { population = 15000 } # Everyone else needs 15k
			}
		}
	}
	
	on_add = {
		ROOT = { prestige = -250 }
	}

	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }	
		ROOT = {
			is_nomadic = yes
			NOT = { same_realm = FROM }
			mercenary = no
		}
	}
	
	can_use_title = {
		# The attacker needs at least one county in the target duchy, or a border
		any_direct_de_jure_vassal_title = {
			OR = {
				holder_scope = {
					OR = {
						character = ROOT
						is_liege_or_above = ROOT
					}
				}
				location = {
					any_neighbor_province = {
						has_owner = yes
						owner = {
							OR = {
								character = ROOT
								is_liege_or_above = ROOT
							}
						}
					}
				}
			}
		}
	}

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

	on_success_title = {
		empty_province_effect = yes
		
		pf_liege_change_under_title_begin_effect = yes
		
		custom_tooltip = {
			text = pagan_subjugation_tip
			hidden_tooltip = {
				ROOT = {
					subjugate_or_take_under_title = {
						title = PREV
						enemy = FROM
#						same_religion = yes # Only vassalize rulers of my religion
					}
				}
			}
		}
		
		pf_liege_change_under_title_end_effect = yes
	}

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

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			prestige = -200
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				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
	}
	
	ai_will_do = { 
		factor = 2
		
		# Try to guide the Hordes in the right direction
		modifier = {
			factor = 0.1
			ROOT = {
				OR = {
					dynasty = 613 # Seljuks
					dynasty = 800 # Timurids
					primary_title = { title = e_il-khanate }
				}
			}
			OR = {
				capital_scope = {
					NOR = {
						region = world_europe_south_east
						region = world_asia_minor
						region = world_middle_east
						region = world_persia
					}
				}
				FROM = {
					primary_title = { title = e_golden_horde }
					NOT = { year = 1300 }
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = {
				primary_title = { title = e_golden_horde }
			}
			OR = {
				capital_scope = {
					OR = {
						region = world_europe_south_east
						region = world_asia_minor
						region = world_middle_east
						region = world_persia
					}
				}
				FROM = {
					primary_title = { title = e_il-khanate }
					NOT = { year = 1300 }
				}
			}
		}
		modifier = {
			factor = 2.0
			OR = {
				culture = persian
				culture = baloch
				culture = afghan
				culture = turkish
			}
			capital_scope = {
				OR = {
					region = world_persia
					region = world_india_rajastan
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = { culture = hungarian }
			capital_scope = {
				OR = {
					region = custom_cuman
					region = custom_volga
					region = custom_alania
					region = custom_novgorod
					region = custom_muscovy
					region = custom_rus
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = { culture = khazar }
			capital_scope = {
				OR = {
					region = world_steppe_tarim
					region = world_steppe_east
					region = custom_cuman
					region = custom_perm
					region = custom_volga
				}
			}
		}
		modifier = {
			factor = 2.0
			ROOT = { culture = hungarian }
			capital_scope = {
				OR = {
					region = custom_bulgaria
					region = custom_carpathia
				}
			}
		}
	}
}

#64
increase_council_power_war = { # Started from faction
	name = CB_NAME_INCREASE_COUNCIL_POWER
	war_name = WAR_NAME_INCREASE_COUNCIL_POWER
	sprite = 6
	truce_days = 3650
	can_call_allies = no
	can_ask_to_join_war = no
	major_revolt = yes
	
	can_use = {
		ROOT = {
			vassal_of = FROM
		}
	}
	
	can_use_title = {
		higher_tier_than = BARON
		OR = {
			has_law = law_voting_power_0
			has_law = banish_voting_power_0
			has_law = execution_voting_power_0
			has_law = revoke_title_voting_power_0
			has_law = grant_title_voting_power_0
			has_law = imprison_voting_power_0
			has_law = war_voting_power_0
		}
		is_primary_holder_title = yes
	}
	
	is_valid = {
		FROM = {
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
		}
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}

	on_success = {
		ROOT = {
			opinion = {
				modifier = opinion_increased_council_power
				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 = law_voting_power_0 }
			add_law_w_cooldown = law_voting_power_1
			break = yes
		}
		if = {
			limit = { has_law = war_voting_power_0 }
			add_law_w_cooldown = war_voting_power_1
			break = yes
		}
		if = {
			limit = { has_law = revoke_title_voting_power_0 }
			add_law_w_cooldown = revoke_title_voting_power_1
			break = yes
		}
		if = {
			limit = { has_law = imprison_voting_power_0 }
			add_law_w_cooldown = imprison_voting_power_1
			break = yes
		}
		if = {
			limit = { has_law = grant_title_voting_power_0 }
			add_law_w_cooldown = grant_title_voting_power_1
			break = yes
		}
		if = {
			limit = { has_law = banish_voting_power_0 }
			add_law_w_cooldown = banish_voting_power_1
			break = yes
		}
		if = {
			limit = { has_law = execution_voting_power_0 }
			add_law_w_cooldown = execution_voting_power_1
			break = yes
		}
	}

	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_council_power_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_council_power_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

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

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

# 55
nomad_humiliate_cb = {
	name = CB_NAME_HUMILIATE
	war_name = WAR_NAME_HUMILIATE
	sprite = 1
	truce_days = 3650
	is_permanent = yes
	hostile_against_others = yes
	can_ask_to_join_war = no
	display_on_map = no
	coalition_threat = no
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no
	
	can_use = {
		has_dlc = "Horse Lords"
		
		FROM = {
			is_nomadic = yes
		}

		ROOT = {
			is_nomadic = yes
			mercenary = no
			OR = {
				AND = {
					same_realm = FROM
					has_feud_with = FROM
				}
				FROM = {
					is_nomadic = yes
					independent = yes
				}
			}
		}
		
		# The attacker needs a border, or be at most two sea zones away from one of the target's counties
		FROM = {
			any_realm_province = {
				any_neighbor_province = {
					OR = {
						AND = {
							has_owner = yes
							owner = {
								OR = {
									character = ROOT
									is_liege_or_above = ROOT
								}
							}
						}
						AND = {
							is_land = no
							any_neighbor_province = {
								OR = {
									AND = {
										has_owner = yes
										owner = {
											OR = {
												character = ROOT
												is_liege_or_above = ROOT
											}
										}
									}
									AND = {
										is_land = no
										any_neighbor_province = {
											owner = {
												OR = {
													character = ROOT
													is_liege_or_above = ROOT
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}

	on_success = {
		ROOT = {
			prestige = 300
			steal_population_scaled = {
				target = FROM
				percentage = 0.25
			}
		}
		FROM = {
			prestige = -300
		}
	}

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

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

		FROM = {
			prestige = 200
		}
	}
	
	on_attacker_leader_death = {
		end_war = invalid
	}

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

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
		
		modifier = {
			factor = 2
			FROM = {
				trait = proud
			}
		}
		
		modifier = {
			factor = 2
			FROM = {
				trait = brave
			}
		}
		
		modifier = {
			factor = 2
			FROM = {
				trait = greedy
			}
		}
		
		modifier = {
			factor = 3
			FROM = {
				tier = KING
			}
		}

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

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

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

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

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

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

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

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

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

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

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

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

	ai_will_do = {
		factor = 1
		
		modifier = {
			factor = 0.1 # Low prio CB
		}
		
		modifier = {
			factor = 0.1 # Target has almost no money
			FROM = {
				OR = {
					is_nomadic = yes
					is_tribal = yes
				}
			}
		}

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

#64
powerful_vassal_takeover_war = { # Started from faction
	name = CB_NAME_POWERFUL_VASSAL_TAKEOVER
	war_name = WAR_NAME_POWERFUL_VASSAL_TAKEOVER
	sprite = 6
	truce_days = 3650
	can_call_allies = no
	can_ask_to_join_war = no
	major_revolt = yes
	
	can_use = {
		ROOT = {
			vassal_of = FROM
		}
	}
	
	can_use_title = {
		higher_tier_than = BARON
		is_primary_holder_title = yes
	}
	
	is_valid = {
		FROM = {
			in_revolt = no
			has_character_flag = powerful_vassal_takeover_target
		}
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
		}
	}

	on_success = {
		any_attacker = {
			participation_scaled_prestige = 100
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_success_title = { #This is the target title of the faction
		if = {
			limit = { has_law = law_voting_power_0 }
			add_law_w_cooldown = law_voting_power_1 
		}
		if = {
			limit = { has_law = war_voting_power_0 }
			add_law_w_cooldown = war_voting_power_1
		}
		if = {
			limit = { NOT =  { has_law = succ_feudal_elective } }
			add_law_w_cooldown = succ_feudal_elective
		}
		holder_scope = {
			clr_character_flag = powerful_vassal_takeover_target
			most_popular_vassal = {
				limit = {
					higher_tier_than = BARON
					is_feudal = yes
					prisoner = no
					NOR = {
						is_inaccessible_trigger = yes
						trait = incapable
						trait = eunuch
					}
					age = 16
					#can_hold_title = PREVPREV
					OR = {
						is_female = no
						AND = {
							is_female = yes
							ROOT = { has_law = true_cognatic_succession }
						}
					}
				}
				grant_title = PREVPREV
				save_event_target_as = new_title_holder
				hidden_tooltip = {
					any_attacker = {
						limit = { NOT = { character = PREV } }
						set_character_flag = helped_win_war
					}
				}
				character_event = { id = 8226 days = 1 }
			}
		}
	}

	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_powerful_vassal_takeover_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		FROM = { 
			hidden_tooltip = { 
				disband_event_forces = faction_loyalists
				clr_character_flag = powerful_vassal_takeover_target
			}
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			prisoner = FROM
		}
		
		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_powerful_vassal_takeover_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		FROM = { 
			hidden_tooltip = { 
				disband_event_forces = faction_loyalists
				clr_character_flag = powerful_vassal_takeover_target
			}
		}
	}

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

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

## Start of temp_fix
# 01_CK2Plus_cb_types start
## normal scopes(including posttitle scopes) ##
# ROOT = actor
# FROM = recipient
# <no scope change> = thirdparty character
## title scopes ##
# ROOT = actor
# FROM = recipient
# <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)
#

truce_notifier = {
	name = CB_NAME_TRUCE_NOTIFIER
	war_name = WAR_NAME_TRUCE_NOTIFIER
	sprite = 1
	truce_days = 3650
	is_permanent = yes
	#is_holy_war = yes
	can_ask_to_join_war = yes

	can_use = {
		OR = {
			ROOT = { has_character_modifier = holy_truce_timer }
			FROM = { has_character_modifier = holy_truce_timer }
		}
		ai = no
	}

	is_valid = {
		always = no
	}

	on_success = {
	}

	on_fail = {
	}

	on_reverse_demand = {
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

dejure_duchy_claim = {
	name = CB_NAME_DEJURECLAIM
	war_name = WAR_NAME_DEJURECLAIM
	sprite = 16
	truce_days = 3650
	hostile_against_others = yes
	is_permanent = yes
	#is_holy_war = 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 = yes
	
	allowed_to_target_tributaries = yes
	allowed_to_target_suzerains = no
	
	infamy_modifier = 0.5
	
	can_use = {
		#check to ensure the target is not valid for a holy war
		NOT = {
			ROOT = {
				OR = {
					NOT = { religion_group = pagan_group }
					is_reformed_religion = yes
				}
				OR = {
					NOR = {
						religion = hindu
						religion = buddhist
					}
					FROM = { NOT = { religion_group = indian_group } }
				}
				OR = {
					NOT = { religion_group = FROM }
					is_heresy_of = FROM
					is_parent_religion = FROM
					AND = {
						OR = {
							religion_group = muslim
							religion_group = pagan_group
							religion = paulician
						}
						NOT = { religion = FROM }
					}
				}
				NOR = {
					pacifist = yes
					same_realm = FROM
				}
			}
		}
	}

	can_use_title = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { attacking_religious_head_trigger = no }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		
		# target title is Duke level and non-titular
		is_titular = no
		tier = duke
		# either the target has the title or their top liege is the target
		OR = {
			FROM = { has_landed_title = PREV }
			holder_scope = { is_liege_or_above = FROM }
		}
		# either a king/emperor is using it to reclaim own de jure duchy
		# or the attacker has a county in the duchy & neither the target or anyone else who isn't a liege or vassal of the attacker does
		OR = {
			empire = {
				has_holder = yes
				OR = {
					has_law = crown_authority_2
					has_law = crown_authority_3
					has_law = crown_authority_4
				}
				ROOT = { has_landed_title = PREV }
			}
			kingdom = {
				has_holder = yes
				OR = {
					has_law = crown_authority_2
					has_law = crown_authority_3
					has_law = crown_authority_4
				}
				ROOT = { has_landed_title = PREV }
			}
			AND = {
				ROOT = {
					num_of_realm_counties = {
						value = 1
						title = PREV
					}
				}
				NOT = {
					FROM = {
						num_of_realm_counties = {
							value = 1
							title = PREV
						}
					}
					any_playable_ruler = {
						NOT = { character = ROOT }
						NOT = { character = FROM }
						NOT = { is_liege_or_above = ROOT } 
						NOT = { is_vassal_or_below = ROOT }
						num_of_realm_counties = {
							value = 1
							title = PREV
						}
					}
				}
			}
		}
		# neither the attacker nor their top liege has the title
		NOR = {
			ROOT = { has_landed_title = PREV }
			holder_scope = { is_liege_or_above = ROOT }
		}
		
		# AI will only use on AI holy orders if they're kings/emperors or of a different religion
		OR = {
			ROOT = { ai = no }
			FROM = {
				OR = {
					ai = no
					holy_order = no
					higher_tier_than = duke
					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_add = {
		# lose 500 piety if the target is a holy order of the same religion
		if = {
			limit = {
				FROM = {
					religion = ROOT
					holy_order = yes
				}
			}
			ROOT = { piety = -500 }
		}
		FROM = {
			attacked_tributary_cb_effect = yes
		}
	}

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		FROM = {
			pf_glory_minus2_effect = yes
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their duchy de jure claim for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		#change imperial decadence
		if = {
			limit = {
				empire = {
					ROOT = { has_landed_title = PREV }
					is_primary_holder_title = yes
				}
			}
			ROOT = { imperial_decadence_minus_1_effect = yes }
		}
		
		empty_province_effect = yes	
		usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
	}

	on_fail = {
		prestige = -100
	}

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			pf_glory_minus2_effect = yes
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
	}

	on_reverse_demand_title = {
		#change imperial decadence
		if = {
			limit = {
				empire = {
					ROOT = { has_landed_title = PREV }
					is_primary_holder_title = yes
				}
			}
			ROOT = { imperial_decadence_plus_1_effect = yes }
		}
	}
	
	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = {
		factor = 1
		modifier = {
			factor = 0
			ROOT = {
				culture = mongol
				religion_group = pagan_group
			}
			FROM = {
				culture = mongol
				religion_group = pagan_group
			}
		}
		modifier = {
			factor = 0
			ROOT = {
				NOR = {
					trait = cynical
					trait = ambitious
				}
				rightful_religious_head_scope = { character = FROM }
			}
		}
		modifier = { # The AI will never breach a tributary's trust
			factor = 0
			ai_attack_tributary_trigger = yes
		}
	}
}

vassalize = { # Subjugate
	name = CB_NAME_VASSALIZE
	war_name = WAR_NAME_VASSALIZE
	sprite = 16
	truce_days = 3650
	is_permanent = yes
	can_ask_to_join_war = yes
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no
	
	infamy_modifier = 0.25
	
	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { attacking_religious_head_trigger = no }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		
		ROOT = {
			is_tribal = no
			is_nomadic = no
		}
		# target is a non-rebelling count+ and the same religion and culture group as the attacker or a former vassal
		FROM = {
			is_tribal = no
			is_nomadic = no
			in_revolt = no
			NOT = { trait = pf_rebel }
			higher_tier_than = BARON
			OR = {
				AND = {
					religion = ROOT
					culture_group = ROOT
				}
				reverse_has_opinion_modifier = { who = ROOT modifier = opinion_independent }
				has_character_flag = is_rollo
			}
		}
		FROM = {
			# target is either independent or is of the same realm but under a different liege
			OR = {
				independent = yes
				AND = {
					same_realm = ROOT
					NOT = { is_liege_or_above = ROOT }
				}
			}
			# is a lower rank than the attacker
			lower_tier_than = ROOT
			# the attacker is their de jure liege or a former liege
			OR = {
				de_jure_liege_or_above = ROOT
				reverse_has_opinion_modifier = { who = ROOT modifier = opinion_independent }
			}
		}
	}

	is_valid = {
		FROM = {
			in_revolt = no
			NOT = { trait = pf_rebel }
			OR = {
				independent = yes
				AND = {
					same_realm = ROOT
					NOT = { is_liege_or_above = ROOT }
				}
			}
			lower_tier_than = ROOT
			OR = {
				de_jure_liege_or_above = ROOT
				reverse_has_opinion_modifier = { who = ROOT modifier = opinion_independent }
			}
		}
	}

	on_success = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their vassalize CB against [From.GetBestName] (ID: [From.GetID])"
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		FROM = {
			hidden_tooltip = {
				opinion = {
					who = ROOT
					modifier = force_vassalized
					months = 240
				}
			}
			set_viceroy_exempt_effect = yes
			set_defacto_liege = ROOT
			pf_liege_change_effect = yes
			
			#imprison the target if they originally became independent
			if = {
				limit = { reverse_has_opinion_modifier = { who = ROOT modifier = opinion_independent } }
				hidden_tooltip = { reverse_remove_opinion = { who = ROOT modifier = opinion_independent } }
				imprison = ROOT
			}
			
			pf_glory_minus2_effect = yes
		}
	}

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

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			pf_glory_minus2_effect = yes
		}
		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
			ROOT = {
				NOR = {
					opinion = { who = FROM value = -25 }
					reverse_opinion = { who = FROM value = -25 }
					has_opinion_modifier = { who = FROM modifier = opinion_independent }
				}
			}
		}
		modifier = {
			factor = 1.5
			ROOT = {
				opinion = { who = FROM value = 10 }
				reverse_opinion = { who = FROM value = 10 }
			}
		}
		modifier = {
			factor = 1.5
			ROOT = { has_opinion_modifier = { who = FROM modifier = opinion_independent } }
		}
	}
}

restore_papacy = {
	name = CB_NAME_RESTORE_PAPACY
	war_name = WAR_NAME_RESTORE_PAPACY
	sprite = 10
	truce_days = 3650
	is_permanent = yes
	#is_holy_war = yes
	can_ask_to_join_war = yes
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no
	
	infamy_modifier = 0.25

	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		
		# the target is not part of the attacker's realm
		NOT = {
			FROM = {
				same_realm = ROOT
			}
		}
		# the target is not already involved in a restore papacy war
		NOT = {
			FROM = {
				any_war = {
					using_cb = restore_papacy
				}
			}
		}
		# the AI will not attempt to use this CB a second time
		NOT = {
			ROOT = {
				ai = yes
				has_character_flag = restore_papacy_attempted
			}
		}
		# the attacker is catholic & catholicism has a RA of 20%+
		ROOT = {
			religion = catholic
			religion_authority = 0.2
		}
		FROM = {
			# the target is not the pope
			NOT = { has_landed_title = k_papal_state }
			# the target is either the pope's liege or owns Rome or has a realm lord who owns Rome
			OR = {
				k_papal_state = { holder_scope = { any_liege = { character = FROM } } }
				has_landed_title = c_roma
				any_realm_lord = {
					has_landed_title = c_roma
				}
			}
			# the pope is either independent or is a vassal of the target or the attacker
			OR = {
				k_papal_state = { holder_scope = { independent = yes } }
				k_papal_state = { holder_scope = { any_liege = { character = FROM } } }
				k_papal_state = { holder_scope = { any_liege = { character = ROOT } } }
			}
		}
	}

	on_add = {
		ROOT = {
			set_character_flag = restore_papacy_attempted
		}
	}

	on_success = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their restore papacy CB against [From.GetBestName] (ID: [From.GetID])"
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		# the Pope regains Rome if owned by the target's realm and becomes independent if vassalized
		FROM = {
			if = {
				limit = {
					OR = {
						has_landed_title = c_roma
						any_realm_lord = {
							has_landed_title = c_roma
						}
					}
				}
				k_papal_state = {
					holder_scope = {
						add_character_modifier = { name = new_land_timer duration = 1 hidden = yes }
						c_roma = {
							usurp_title_plus_barony_if_unlanded = { target = PREV type = claim }
						}
					}
				}
			}
			if = {
				limit = {
					k_papal_state = {
						holder_scope = {
							any_liege = { character = FROM }
						}
					}
				}
				k_papal_state = {
					holder_scope = {
						set_defacto_liege = THIS
						pf_liege_change_effect = yes
					}
				}
			}
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
			participation_scaled_piety = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 participation_scaled_piety = 100 }
		}
		FROM = {
			prestige = -200
			pf_glory_minus2_effect = yes
		}
	}

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

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -200
			piety = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			pf_glory_minus2_effect = yes
		}
		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 = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = { 
		factor = 2
		modifier = {
			factor = 5
			ROOT = { trait = zealous }
		}
		modifier = {
			factor = 0
			ROOT = { trait = cynical }
		}
	}
}

restore_byzantium = { #Restore Byzantium
	name = CB_NAME_RESTOREBYZANTIUM
	war_name = WAR_NAME_RESTOREBYZANTIUM
	sprite = 16
	truce_days = 3650
	is_permanent = yes
	#is_holy_war = yes
	check_de_jure_tier = EMPEROR
	can_ask_to_join_war = yes
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no

	can_use_title = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		
		# the Byzantine Empire does not exist
		e_byzantium = { has_holder = no }
		# the attacker is an independent Greek Orthodox (or a heretic) king or emperor
		ROOT = {
			OR = {
				religion = orthodox
				religion = bogomilist
				religion = nestorian
				religion = monothelite
				religion = iconoclast
			}
			culture_group = byzantine
			independent = yes
			higher_tier_than = duke
		}
		# the defender is either not Orthodox (or a heretic) or not Greek
		FROM = {
			OR = {
				NOR = { 
					religion = orthodox
					religion = bogomilist
					religion = nestorian
					religion = monothelite
					religion = iconoclast
				}
				NOT = { culture_group = byzantine }
			}
		}
		title = e_byzantium
		has_holder = no
		# byzantion's holder is in the target's realm
		c_byzantion = { holder_scope = { same_realm = FROM } }
		NOT = {
			OR = {
				ROOT = {
					has_landed_title = PREV
				}
				holder_scope = {
					is_liege_or_above = ROOT
				}
			}
		}
	}

	is_valid_title = {
		ROOT = {
			OR = {
				religion = orthodox
				religion = bogomilist
				religion = nestorian
				religion = monothelite
				religion = iconoclast
			}
			culture_group = byzantine
			independent = yes
		}

		FROM = {
			OR = {
				NOR = {
					religion = orthodox
					religion = bogomilist
					religion = nestorian
					religion = monothelite
					religion = iconoclast
				}
				NOT = { culture_group = byzantine }
			}
		}

		title = e_byzantium
		has_holder = no
		c_byzantion = { holder_scope = { same_realm = FROM } }

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

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			imperial_decadence_minus_1_effect = yes
			pf_glory_plus2_effect = yes
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		FROM = {
			prestige = -200
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their restore byzantium CB for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		hidden_tooltip = {
			any_de_jure_vassal_title = { # take all baronies under the one we're fighting for
				limit = {
					has_holder = yes
					holder_scope = {
						OR = {
							character = FROM
							is_liege_or_above = FROM
						}
					}
				}
				usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
			}
		}
		tooltip = { e_byzantium = { gain_title = ROOT } }
		ROOT = { set_character_flag = centralize_without_penalties }
		hidden_tooltip = {
			if = {
				limit = {
					c_byzantion = { holder_scope = { same_realm = ROOT } }
				}
				e_byzantium = {
					revoke_law = investiture_law_0
					revoke_law = investiture_law_1
					add_law = crown_authority_1
					add_law = council_privileges_0
					add_law = succ_primogeniture
					add_law = cognatic_succession
					gain_title = ROOT
				}
			}
		}
		ROOT = { clr_character_flag = centralize_without_penalties }
		#shatter empire if it's too small
		hidden_tooltip = { FROM = { character_event = { id = Plus.303 } } }
	}

	on_fail = {
		prestige = -100
	}

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			imperial_decadence_minus_1_effect = yes
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}
		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 = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

coastal_city_republic = { #Take a coastal city (Barony Tier)
	name = CB_NAME_COASTAL_COUNTY_REPUBLIC
	war_name = WAR_NAME_COASTAL_CITY_REPUBLIC
	sprite = 1
	truce_days = 3650
	is_permanent = yes
	check_all_titles = yes
	can_ask_to_join_war = yes
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no
	
	allow_distant = yes # AI parameter
	
	can_use = {
		ROOT = {
			OR = {
				is_merchant_republic = yes
				is_patrician = yes
			}
			NOR = { 
				is_liege_or_above = FROM
				is_vassal_or_below = FROM 
				same_liege = FROM
				has_character_modifier = rep_cb_cd
			}
			prisoner = no
			has_regent = no
		}
	}
	
	can_use_title = {
		location = {
			port = yes
			OR = {
				# Within Free CB range
				ROOT = {
					capital_scope = {
						NOT = {
							distance = {
								where = PREVPREV # Comparing the distance between ROOT's capital and the title in question
								value = 590	#  From  aprox.Venice to East Cyprus and from Venice to Gibraltar
							}
						}	
					}
				}
				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 = { # Prevent the AI from overextending itself, it won't understand the mechanic anyways
				ROOT = {
					ai = yes
					capital_scope = {
						distance = {
							where = PREVPREV # Comparing the distance between ROOT's capital and the title in question
							value = 590	#  From  aprox.Venice to East Cyprus and from Venice to Gibraltar
						}	
					}
				}
			}
		}
		tier = BARON
		is_republic = yes # Cities only
		is_patrician = no
		NOT = { owner = { character = FROM } } # Can't take the primary holding away from someone	
		
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { attacking_religious_head_trigger = no }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
	}

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

	is_valid_title = {
		NOT = { owner = { character = FROM } } # Can't take the primary holding away from someone
	}
	
	on_add = {
		ROOT = {
			add_character_modifier = {
				name = rep_cb_cd
				duration = 1825
				hidden = no
				inherit = yes
			}
		}
	}

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		FROM = {
			pf_glory_minus2_effect = yes
		}
	}
	
	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their coastal city republic war for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		usurp_title = ROOT
		
		if = {
			limit = {
				location = {
					port = yes
					ROOT = {
						capital_scope = {
							NOT = {
								distance = {
									where = PREVPREV # Comparing the distance between ROOT's capital and the title in question
									value = 590	#  From  aprox.Venice to East Cyprus and from Venice to Gibraltar
								}
							}	
						}
					}			
				}
			}
			custom_tooltip = {
				text = CTT_REP_IN_RANGE
			}
		}
		if = {
			limit = {
				location = {
					port = yes
					ROOT = {
						capital_scope = {
							distance = {
								where = PREVPREV # Comparing the distance between ROOT's capital and the title in question
								value = 590	#  From  aprox.Venice to East Cyprus and from Venice to Gibraltar
							}	
						}
					}			
				}			
			}
			custom_tooltip = {
				text = CTT_REP_OUT_RANGE
			}			
		}		
		
		hidden_tooltip = {
			ROOT = {
				remove_character_modifier = rep_cb_cd
				add_character_modifier = {
					name = rep_cb_cd
					duration = 1825
					hidden = no
					inherit = yes
				}
			}
		}
		
		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 }
		}
		
		hidden_tooltip = {
			ROOT = { remove_character_modifier = rep_cb_cd }
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			pf_glory_minus2_effect = yes
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		hidden_tooltip = {
			ROOT = { remove_character_modifier = rep_cb_cd }
		}
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
	}

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

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = {
		factor = 1
		modifier = {
			factor = 0
			ROOT = {
				NOR = {
					trait = cynical
					trait = ambitious
				}
				rightful_religious_head_scope = { character = FROM }
			}
		}
	}
}

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

	can_call_allies = no
	can_ask_to_join_war = no
	major_revolt = yes
	
	infamy_modifier = 0.85
	
	can_use_title = {
		FROM = {
			has_landed_title = PREV
			reverse_has_opinion_modifier = { who = ROOT modifier = revolting_against }
		}
	}

	is_valid_title = {
		FROM = {
			has_landed_title = PREV
			reverse_has_opinion_modifier = { who = ROOT modifier = revolting_against }
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
		}
	}

	on_add = {
		log = "CB(claim_on_liege_plot): START: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
		FROM = { pf_log_state_effect = yes }
		ROOT = { pf_log_state_effect = yes }
		#emf_cb_dynlevy_begin_effect = yes
	}	
	
	on_success = {
		# add victory modifier for the 'Win a War' ambition
		any_attacker = { add_character_modifier = { name = victory_timer duration = 1 hidden = yes } }		
		
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}	
		FROM = {
			prestige = -500
			imprison = ROOT
			
			# existing vassals may declare independence
			if = {
			 	limit = { independent = yes }
			 	hidden_tooltip = { any_vassal = { character_event = { id = Plus.430 days = 3 } } }
			}
		}
	}
	
	on_success_title = {
		log = "CB(claim_on_liege_plot): VICTORY: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
		
		# Prevent on_new_holder / on_unlanded edge case handlers from tearing-down any civil war state while the CB
		# completes due to, e.g., liege changes or gained titles.
		set_global_flag = pf_teardown_lock
		
		# the claimant has the wrong government type, take care of that first.
		if = {
			limit = {
				higher_tier_than = ROOT
				holder_scope = {
					OR = {
						is_feudal = yes
						is_tribal = yes
					}
				}
				ROOT = {
					is_ruler = yes
					OR = {
						is_theocracy = yes
						is_republic = yes
					}
				}
			}
			ROOT = { abdicate = yes }
		}
		# usurp primary titles
		if = {
			limit = {
				is_primary_holder_title = yes
			}
			FROM = {
				# check to see if title will be destroyed
				any_demesne_title = {
					limit = { can_destroy_title_trigger = yes }
					custom_tooltip = { text = TITLE_ENDANGERED_NO_NAME }
					set_title_flag = destroy_title
				}
				# reduce CA & TO
				any_demesne_title = {
					title_reduce_CA_effect = yes
					title_reduce_TO_effect = yes
				}
				# usurp all non-primary titles first
				any_demesne_title = {
					limit = {
						is_primary_holder_title_tier = yes
						is_primary_holder_title = no
					}
					usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
				}
				# usurp primary title
				primary_title = {
					usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
					hidden_tooltip = { make_primary_title = yes }
				}
				# ruler stays within the realm
				set_defacto_liege = ROOT
				# destroy primary title if it has been reduced too much
				hidden_tooltip = {
					if = {
						limit = { primary_title = { has_title_flag = destroy_title } }
						character_event = { id = Plus.1620 days = 4 }
					}
				}
			}
			# check to see if the ruler needs to give the claimant a capital
			hidden_tooltip = {
				if = {
					limit = {
						FROM = {
							ROOT = { 
								primary_title = { 
									NOT = { has_law = succ_feudal_elective } 
									FROM = {
										capital_scope = {
											de_jure_liege_or_above = PREVPREV
										}
									}
								} 
							}
							OR = {
								num_of_count_titles = 2
								primary_title = { higher_tier_than = count }
							}
						}
					}
					FROM = {
						capital_scope = {
							county = {
								usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
							}
						}
					}			
				}	
			}
			# set proper liege statuses
			hidden_tooltip = {
				FROM = {
					set_defacto_liege = ROOT 
					any_vassal = {
						limit = { NOT = { primary_title = { de_jure_liege_or_above = PREVPREV } } }
						set_defacto_liege = ROOT 
					}
				}
			}			
		}
		# usurp non-primary titles
		if = {
			limit = {
				is_primary_holder_title = no
				holder = FROM
			}
			title_reduce_CA_effect = yes
			title_reduce_TO_effect = yes
			usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
		}

		clr_global_flag = pf_teardown_lock		
		
		# figure out who are the supporters, losers, and neutrals
		hidden_tooltip = {
			ROOT = {
				# clear previous civil war opinions
				pf_civil_war_clear_old_opinions_effect = yes
					
				# mark rebels as supporters
				FROM = {
					any_opinion_modifier_target = {
						limit = {
							pf_part_of_this_revolt_trigger = yes
							NOT = { character = PREVPREV }
						}
						reverse_opinion = {
							who = PREVPREV
							modifier = opinion_civil_war_supporter
							months = 60
						}
					}
				}
					
				# mark former liege and loyalists as losers
				any_vassal = {
					limit = {
						has_opinion_modifier = {
							who = FROM
							modifier = pf_loyal_toward
						}
					}
					reverse_opinion = {
						who = LIEGE
						modifier = opinion_civil_war_loser
						months = 60
					}
				}
				FROM = {
					reverse_opinion = {
						who = PREV
						modifier = opinion_civil_war_loser
						months = 60
					}
				}
					
				# remember those who chose to be neutral
				any_vassal = {
					limit = {
						has_opinion_modifier = {
							who = FROM
							modifier = pf_neutral_toward
						}
					}
					reverse_opinion = {
						who = LIEGE
						modifier = opinion_civil_war_neutral
						months = 60
					}
				}
			}
		}
		
		# check whether the primary title should be destroyed due to CA loss
		if = {
			limit = { has_title_flag = destroy_title }
			hidden_tooltip = {
				ROOT = { character_event = { id = Plus.1620 days = 4 } } #destroy title
			}
		}

		# clean up remaining TOMs
		pf_civil_war_cleanup_effect = yes

		# post-civil war event to reward allies
		hidden_tooltip = {
			ROOT = {
				#emf_dynlevy_update_effect = yes
				hidden_tooltip = { character_event = { id = PlusFaction.450 } }
			}
		}
		
		# change faction moods
		FROM = { hidden_tooltip = { character_event = { id = PlusFaction.200 days = 1 } } }
		ROOT = { hidden_tooltip = { character_event = { id = PlusFaction.200 days = 1 } } }
		
		#emf_cb_dynlevy_end_effect = yes
	}

	on_fail = {
		log = "CB(claim_on_liege_plot): WHITEPEACE: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"

		#emf_cb_dynlevy_end_effect = yes
		
		# clean up remaining civil war state
		pf_civil_war_cleanup_effect = yes
		
		ROOT = {
			prestige = -100
			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 = {
			FROM = {
				remove_opinion = {
					who = ROOT
					modifier = declared_war
				}
			}
		}
	}

	on_reverse_demand = {
		log = "CB(claim_on_liege_plot): DEFEAT: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"

		#emf_cb_obj_win_war_reverse_effect = yes
		#emf_cb_dynlevy_end_effect = yes
		
		FROM = {
			# the defender loses 1 Imperial Decadence
			imperial_decadence_minus_1_effect = yes
			# add victory modifier for the 'Win a War' ambition
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			# remove any previously-existing loser TOMs
			pf_civil_war_clear_old_opinions_effect = yes
		}
		
		# apply immediate effects to the revolters
		any_attacker = {
			limit = { has_opinion_modifier = { who = FROM modifier = revolting_against } }
			imprison = FROM
			hidden_tooltip = {
				reverse_opinion = { who = FROM modifier = opinion_civil_war_loser months = 60 }
				opinion = { who = FROM modifier = opinion_coerced_into_leaving_faction months = 60 }
			}
		}

		# mark loyalists and neutrals for the reward event
		hidden_tooltip = {
			FROM = {
				any_vassal = {
					limit = { reverse_has_opinion_modifier = { who = PREV modifier = pf_loyal_to_me } }
					reverse_opinion = { who = PREV modifier = opinion_civil_war_supporter months = 60 }
				}
				any_vassal = {
					limit = { reverse_has_opinion_modifier = { who = PREV modifier = pf_neutral_to_me } }
					reverse_opinion = { who = PREV modifier = opinion_civil_war_neutral months = 60 }
				}
			}
		}
		
		# clean up remaining TOMs
		pf_civil_war_cleanup_effect = yes
		
		ROOT = { prestige = -200 }
		
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		
		# post-civil war event to reward allies
		FROM = {
			hidden_tooltip = { character_event = { id = PlusFaction.450 } }
		}
		
		# change faction moods
		hidden_tooltip = {
			FROM = { character_event = { id = PlusFaction.200 days = 1 } }
			ROOT = { character_event = { id = PlusFaction.200 days = 1 } }
		}
	}
	
	on_reverse_demand_title = {
		remove_claim = ROOT
		ROOT = {
			custom_tooltip = { text = MAY_NOT_BECOME_PRETENDER_AGAIN }
			hidden_tooltip = {
				reverse_opinion = {
					who = FROM
					modifier = failed_to_claim_my_title
					years = 100
				}
			}
		}
	}
	
	on_attacker_leader_death = {
		end_war = invalid
	}
	
	on_invalidation = {
		log = "CB(claim_on_liege_plot): INVALIDATED: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"

		#emf_cb_dynlevy_end_effect = yes
		
		# clean up remaining civil war state
		pf_civil_war_cleanup_effect = yes
	}	
	
	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_on_liege_plot = {
	name = CB_NAME_CLAIM
	war_name = WAR_NAME_OTHER_PRETENDER
	sprite = 3
	truce_days = 3650
	hostile_against_others = yes

	can_call_allies = no
	can_ask_to_join_war = no
	major_revolt = yes
	
	infamy_modifier = 0.5
	
	can_use_title = {
		FROM = {
			has_landed_title = PREV
			reverse_has_opinion_modifier = { who = FROMFROM modifier = revolting_against }
		}
	}

	is_valid_title = {
		FROM = {
			has_landed_title = PREV
			reverse_has_opinion_modifier = { who = FROMFROM modifier = revolting_against }
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
		}
	}
	
	on_add = {
		log = "CB(other_claim_on_liege_plot): START: [This.GetTitledFirstName] of [This.PrimaryTitle.GetBaseName] (ID: [This.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID]), supporting claimant [Root.GetBestName] (ID: [Root.GetID])"
		FROM = { pf_log_state_effect = yes }
		pf_log_state_effect = yes
		#emf_cb_dynlevy_begin_effect = yes
		ROOT = {
			set_character_flag = claim_being_pressed
		}
	}

	on_success = {
		# add victory modifier for the 'Win a War' ambition
		any_attacker = { add_character_modifier = { name = victory_timer duration = 1 hidden = yes } }
		
		# claimant is grateful to the attacker
		ROOT = {
			add_character_modifier = { name = new_land_timer duration = 1 hidden = yes }
			opinion = { who = PREV modifier = pressed_my_claim }
			hidden_tooltip = {
				clr_character_flag = claim_being_pressed
			}
		}

		any_attacker = {
			limit = { 
				OR = {
					character = ROOT 
					character = PREV
				}
			}
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { 
				NOR = {
					character = ROOT 
					character = PREV
				}
			}
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		
		FROM = {
			prestige = -500
			imprison = ROOT
			
			# existing vassals may declare independence
			if = {
			 	limit = { independent = yes }
			 	hidden_tooltip = { any_vassal = { character_event = { id = Plus.430 days = 3 } } }
			}
		}
	}
	
	on_success_title = {
		log = "CB(other_claim_on_liege_plot): VICTORY: [FromFrom.GetTitledFirstName] of [FromFrom.PrimaryTitle.GetBaseName] (ID: [FromFrom.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID]), supporting claimant [Root.GetBestName] (ID: [Root.GetID])"

		# check to see if the claimant should stay with their current liege
		if = {
			limit = { FROM = { independent = no } }
			FROM = {
				set_character_flag = has_liege
				liege = { save_event_target_as = target_liege }
			}
		}
		if = {
			limit = {
				ROOT = {
					independent = no
					is_ruler = yes
					NOT = { lower_tier_than = PREV }
				}
			}
			ROOT = {
				set_character_flag = stay_in_realm
				liege = { save_event_target_as = claimant_liege }
			}
		}
		if = {
			limit = {
				OR = {
					ROOT = { is_ruler = no }
					AND = {
						FROM = { NOT = { has_character_flag = has_liege } }
						ROOT = { higher_tier_than = PREV }
					}
					AND = {
						FROM = { has_character_flag = has_liege }
						ROOT = { lower_tier_than = PREV }
					}
				}
			}
			ROOT = { set_character_flag = leave_realm }
		}

		# Prevent on_new_holder / on_unlanded edge case handlers from tearing-down any civil war state while the CB
		# completes due to, e.g., liege changes or gained titles.
		set_global_flag = pf_teardown_lock
		
		# the claimant has the wrong government type, take care of that first.
		if = {
			limit = {
				higher_tier_than = ROOT
				holder_scope = {
					OR = {
						is_feudal = yes
						is_tribal = yes
					}
				}
				ROOT = {
					is_ruler = yes
					OR = {
						is_theocracy = yes
						is_republic = yes
					}
				}
			}
			ROOT = { abdicate = yes }
		}

		# usurp primary title
		if = {
			limit = { is_primary_holder_title = yes }
			FROM = {
				# check whether title will be destroyed
				any_demesne_title = {
					limit = { can_destroy_title_trigger = yes }
					custom_tooltip = { text = TITLE_ENDANGERED_NO_NAME }
					set_title_flag = destroy_title
				}
				
				# reduce CA & TO
				any_demesne_title = {
					title_reduce_CA_effect = yes
					title_reduce_TO_effect = yes
				}
				
				# usurp primary and primary-tier titles
				any_demesne_title = {
					limit = {
						is_primary_holder_title_tier = yes
						is_primary_holder_title = no
					}
					usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
				}
				primary_title = {
					usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
					hidden_tooltip = { make_primary_title = yes }
				}
				set_defacto_liege = ROOT
				
				# destroy primary title if it has been reduced too much
				hidden_tooltip = {
					if = {
						limit = { primary_title = { has_title_flag = destroy_title } }
						character_event = { id = Plus.1620 days = 4 }
					}
				}				
			}
			
			# check whether usurper needs to be given the capital county
			hidden_tooltip = {
				if = {
					limit = {
						FROM = {
							ROOT = { 
								primary_title = { 
									NOT = { has_law = succ_feudal_elective } 
									FROM = {
										capital_scope = {
											de_jure_liege_or_above = PREVPREV
										}
									}
								} 
							}
							OR = {
								num_of_count_titles = 2
								primary_title = { higher_tier_than = count }
							}
						}
					}
					FROM = {
						capital_scope = {
							county = {
								usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
							}
						}
					}			
				}	
			}
			
			# set proper liege statuses
			hidden_tooltip = {
				FROM = { 
					set_defacto_liege = ROOT 
					any_vassal = {
						limit = {
							NOT = { primary_title = { de_jure_liege_or_above = PREVPREV } }
						}
						set_defacto_liege = ROOT 
					}
				}
			}			
		}
		
		# usurp non-primary titles
		if = {
			limit = {
				is_primary_holder_title = no
				holder = FROM
			}
			title_reduce_CA_effect = yes
			title_reduce_TO_effect = yes
			usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }					
		}
		
		# assign the claimant to a new liege or go independent, if necessary
		if = {
			limit = {
				FROM = { has_character_flag = has_liege }
				ROOT = { has_character_flag = leave_realm }
			}
			ROOT = { set_defacto_liege = event_target:target_liege }
		}
		if = {
			limit = {
				FROM = { NOT = { has_character_flag = has_liege } }
				ROOT = { has_character_flag = leave_realm }
			}
			ROOT = { set_defacto_liege = THIS }
		}
		if = {
			limit = {
				ROOT = { has_character_flag = stay_in_realm }
			}
			ROOT = { set_defacto_liege = event_target:claimant_liege }
		}
		FROM = { clr_character_flag = has_liege }
		ROOT = { clr_character_flag = leave_realm }
		ROOT = { clr_character_flag = stay_in_realm }
		
		clr_global_flag = pf_teardown_lock
		
		# figure out who are the supporters, losers, and neutrals
		hidden_tooltip = {
			ROOT = {
				# clear previous civil war opinions
				pf_civil_war_clear_old_opinions_effect = yes
					
				# mark rebels as supporters
				FROM = {
					any_opinion_modifier_target = {
						limit = {
							pf_part_of_this_revolt_trigger = yes
							NOT = { character = PREVPREV }
						}
						reverse_opinion = {
							who = PREVPREV
							modifier = opinion_civil_war_supporter
							months = 60
						}
					}
				}
					
				# mark former liege and loyalists as losers
				any_vassal = {
					limit = {
						has_opinion_modifier = {
							who = FROM
							modifier = pf_loyal_toward
						}
					}
					reverse_opinion = {
						who = LIEGE
						modifier = opinion_civil_war_loser
						months = 60
					}
				}
				FROM = {
					reverse_opinion = {
						who = PREV
						modifier = opinion_civil_war_loser
						months = 60
					}
				}
					
				# remember those who chose to be neutral
				any_vassal = {
					limit = {
						has_opinion_modifier = {
							who = FROM
							modifier = pf_neutral_toward
						}
					}
					reverse_opinion = {
						who = LIEGE
						modifier = opinion_civil_war_neutral
						months = 60
					}
				}
			}
		}
		
		# check whether the primary title should be destroyed due to CA loss
		if = {
			limit = { has_title_flag = destroy_title }
			hidden_tooltip = {
				ROOT = { character_event = { id = Plus.1620 days = 4 } } #destroy title
			}
		}

		# clean up remaining TOMs
		pf_civil_war_cleanup_effect = yes

		# post-civil war event to reward allies
		hidden_tooltip = {
			ROOT = {
				#emf_dynlevy_update_effect = yes
				hidden_tooltip = { character_event = { id = PlusFaction.450 } }
			}
		}
		
		# change faction moods
		FROM = { hidden_tooltip = { character_event = { id = PlusFaction.200 days = 1 } } }
		ROOT = { hidden_tooltip = { character_event = { id = PlusFaction.200 days = 1 } } }
		
		#emf_cb_dynlevy_end_effect = yes
	}

	on_fail = {
		log = "CB(other_claim_on_liege_plot): WHITEPEACE: [This.GetTitledFirstName] of [This.PrimaryTitle.GetBaseName] (ID: [This.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID]), supporting claimant [Root.GetBestName] (ID: [Root.GetID])"

		#emf_cb_dynlevy_end_effect = yes
		
		# clean up remaining civil war state
		pf_civil_war_cleanup_effect = yes
		
		prestige = -100
		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 = {
			FROM = {
				remove_opinion = {
					who = PREV
					modifier = declared_war
				}
			}
			ROOT = {
				clr_character_flag = claim_being_pressed
			}
		}
	}

	on_reverse_demand = {
		log = "CB(other_claim_on_liege_plot): DEFEAT: [This.GetTitledFirstName] of [This.PrimaryTitle.GetBaseName] (ID: [This.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID]), supporting claimant [Root.GetBestName] (ID: [Root.GetID])"

		#emf_cb_obj_win_war_reverse_effect = yes
		#emf_cb_dynlevy_end_effect = yes
		
		FROM = {
			# the defender loses 1 Imperial Decadence
			imperial_decadence_minus_1_effect = yes
			# add victory modifier for the 'Win a War' ambition
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			# remove any previously-existing loser TOMs
			pf_civil_war_clear_old_opinions_effect = yes
		}

		# apply immediate effects to the revolters
		any_attacker = {
			limit = { has_opinion_modifier = { who = FROM modifier = revolting_against } }
			imprison = FROM
			hidden_tooltip = {
				reverse_opinion = { who = FROM modifier = opinion_civil_war_loser months = 60 }
				opinion = { who = FROM modifier = opinion_coerced_into_leaving_faction months = 60 }
			}
		}

		# mark loyalists and neutrals for the reward event
		hidden_tooltip = {
			FROM = {
				any_vassal = {
					limit = { reverse_has_opinion_modifier = { who = PREV modifier = pf_loyal_to_me } }
					reverse_opinion = { who = PREV modifier = opinion_civil_war_supporter months = 60 }
				}
				any_vassal = {
					limit = { reverse_has_opinion_modifier = { who = PREV modifier = pf_neutral_to_me } }
					reverse_opinion = { who = PREV modifier = opinion_civil_war_neutral months = 60 }
				}
			}
		}
		
		# clean up remaining TOMs
		pf_civil_war_cleanup_effect = yes
		
		prestige = -200
		
		# imprison the claimant if they are part of the realm but not a rebel
		if = {	
			limit = {
				ROOT = {
					any_liege = {
						character = FROM
						NOT = { has_opinion_modifier = { who = PREV modifier = opinion_civil_war_loser } }
					}
				}
			}
			ROOT = {
				imprison = FROM
				hidden_tooltip = {
					reverse_opinion = { who = FROM modifier = opinion_civil_war_loser months = 60 }	
				}
			}
		}
		
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		
		# post-civil war event to reward allies
		FROM = {
			hidden_tooltip = { character_event = { id = PlusFaction.450 } }
		}
		
		# change faction moods
		hidden_tooltip = {
			FROM = { character_event = { id = PlusFaction.200 days = 1 } }
			ROOT = { character_event = { id = PlusFaction.200 days = 1 } }
		}
	}
	
	on_reverse_demand_title = {
		if = {
			limit = {
				OR = {
					any_attacker = { character = ROOT }
					ROOT = { any_liege = { character = FROM } }
				}
			}
			remove_claim = ROOT
		}
		ROOT = {
			custom_tooltip = { text = MAY_NOT_BECOME_PRETENDER_AGAIN }
			hidden_tooltip = {
				reverse_opinion = {
					who = FROM
					modifier = failed_to_claim_my_title
					years = 100
				}
			}
		}
	}
	
	on_invalidation = {
		log = "CB(other_claim_on_liege_plot): INVALIDATED: [FromFrom.GetTitledFirstName] of [FromFrom.PrimaryTitle.GetBaseName] (ID: [FromFrom.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID]), supporting claimant [Root.GetBestName] (ID: [Root.GetID])"

		#emf_cb_dynlevy_end_effect = yes
		
		# clean up remaining civil war state
		pf_civil_war_cleanup_effect = yes
		ROOT = {
			hidden_tooltip = {
				clr_character_flag = claim_being_pressed
			}
		}
	}
	
	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
	}
}

religious_overthrow_ruler = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_FACTION_DEPOSE
	sprite = 26
	truce_days = 3650
	
	major_revolt = yes
	can_call_allies = no
	can_ask_to_join_war = no
	
	infamy_modifier = 0
	
	can_use = {
		ROOT = {
			vassal_of = FROM
			has_opinion_modifier = { who = FROM modifier = revolting_against }
		}
	}

	is_valid = {
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = { 
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
			has_opinion_modifier = { who = FROM modifier = revolting_against }
		}
		FROM = {
			any_demesne_title = {
				NOT = { lower_tier_than = ROOT }
				temporary = no
			}
			OR = {
				is_parent_religion = ROOT
				is_heresy_of = ROOT
			}
			NOT = { religion = ROOT }
			in_revolt = no
			liege_before_war = {
				in_revolt = no
				liege_before_war = {
					in_revolt = no
				}
			}
		}
	}

	on_add = {
		log = "CB(cb_faction_overthrow_ruler): START: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
		FROM = { pf_log_state_effect = yes }
		ROOT = { pf_log_state_effect = yes }
		#emf_cb_dynlevy_begin_effect = yes
	}	
	
	on_success = {
		log = "[Root.GetBestName] succeeded with their religious_overthrow_ruler CB against [From.GetBestName]"
		
		# add victory modifier for the 'Win a War' ambition
		any_attacker = { add_character_modifier = { name = victory_timer duration = 1 hidden = yes } }
		
		# apply immediate effects to the revolters
		hidden_tooltip = {
			any_attacker = {
				limit = { has_opinion_modifier = { who = FROM modifier = revolting_against } }
				
				# get tradition faction bonus
				hidden_tooltip = { pf_tradition_plus4_effect = yes }
				
				# the revolters remember who they overthrew
				opinion = { modifier = overthrown_ruler who = FROM }
				reverse_opinion = { modifier = overthrew_me who = FROM }
				
				# and also are grateful to each other for their victory
				FROM = {
					any_opinion_modifier_target = {
						limit = {
							reverse_has_opinion_modifier = {
								who = PREV
								modifier = revolting_against_me
							}
							NOT = { character = PREVPREV }
						}
						reverse_opinion = {
							who = PREVPREV
							modifier = opinion_very_grateful
							years = 10
						}
					}
				}
			}
		}		
		
		# apply revolt effects to the defender
		FROM = {
			prestige = -500
			save_event_target_as = current_holder
			
			# existing vassals may declare independence
			if = {
			 	limit = { independent = yes }
			 	hidden_tooltip = { any_vassal = { character_event = { id = Plus.430 days = 3 } } }
			}
			
			# check whether title will be destroyed (CK2Plus only)
			any_demesne_title = {
			 	limit = { can_destroy_title_trigger = yes }
			 	custom_tooltip = { text = TITLE_ENDANGERED_NO_NAME }
			 	log = "[This.GetFullName] should be destroyed as a result of the revolt"
			 	set_title_flag = destroy_title
			}
		
			# always lower CA/TO
			pf_lower_CA_effect = yes # lower Crown Authority
			pf_lower_TO_effect = yes # lower Tribal Organization

			# Prevent on_new_holder / on_unlanded edge case handlers from tearing-down any civil war state while the CB
			# completes due to, e.g., liege changes or gained titles.
			set_global_flag = pf_teardown_lock			
		
			# save event targets for primary title and current liege
			primary_title = { save_event_target_as = former_primary_title }
			if = {
				limit = { independent = no }
				liege = { save_event_target_as = current_liege }
			}

			# abdicate if heir is of attacker's religion
			if = {
				limit = { current_heir = { religion = ROOT } }
				hidden_tooltip = { character_event = { id = Plus.306 } } #pass on tyrant/dishonorable traits to heir
				abdicate = yes
			}
			
			# then find a proper usurper of the attacker's religion
			if = {
				limit = {
					is_ruler = yes
					current_heir = { NOT = { religion = ROOT } }
				}
				# rebel leader usurps if there is no rebel with a claim or a pretender to primary title
				if = {
					limit = {
						event_target:current_holder = {
							NOR = {
								any_opinion_modifier_target = {
									religion = ROOT
									pf_part_of_this_revolt_trigger = yes
									OR = {
										has_claim = event_target:former_primary_title
										any_pretender_title = { title = event_target:former_primary_title }
									}
								}
								any_realm_character = {
									religion = ROOT
									pf_part_of_this_revolt_trigger = no
									OR = {
										has_strong_claim = event_target:former_primary_title
										any_pretender_title = { title = event_target:former_primary_title }
									}
								}
							}
						}
					}
					ROOT = {
						event_target:current_holder = { abdicate_to = PREV }
						if = {
							limit = { event_target:current_liege = { always = yes } }
							hidden_tooltip = { set_defacto_liege = event_target:current_liege }
						}
					}
				}
				# give primary title to realm pretender if no rebels have a claim or are a pretender
				if = {
					limit = {
						event_target:current_holder = {
							any_realm_character = {
								religion = ROOT
								pf_part_of_this_revolt_trigger = no
								OR = {
									has_strong_claim = event_target:former_primary_title
									any_pretender_title = { title = event_target:former_primary_title }
								}
							}
							NOT = {
								any_opinion_modifier_target = {
									religion = ROOT
									pf_part_of_this_revolt_trigger = yes
									OR = {
										has_claim = event_target:former_primary_title
										any_pretender_title = { title = event_target:former_primary_title }
									}
								}
							}
						}
					}
					event_target:current_holder = {
						random_realm_character = {
							limit = {
								religion = ROOT
								pf_part_of_this_revolt_trigger = no
								OR = {
									has_strong_claim = event_target:former_primary_title
									any_pretender_title = { title = event_target:former_primary_title }
								}
							}
							event_target:current_holder = { abdicate_to = PREV }
							if = {
								limit = { event_target:current_liege = { always = yes } }
								hidden_tooltip = { set_defacto_liege = event_target:current_liege }
							}
						}
					}
				}				
				# give primary title to rebel with weak claim if none with a strong claim or pretender exists
				if = {
					limit = {
						event_target:current_holder = {
							any_opinion_modifier_target = {
								religion = ROOT
								pf_part_of_this_revolt_trigger = yes
								has_weak_claim = event_target:former_primary_title
							}
							NOT = {
								any_opinion_modifier_target = {
									religion = ROOT
									pf_part_of_this_revolt_trigger = yes
									OR = {
										has_strong_claim = event_target:former_primary_title
										any_pretender_title = { title = event_target:former_primary_title }
									}
								}
							}
						}
					}
					event_target:current_holder = {
						random_opinion_modifier_target = {
							limit = {
								religion = ROOT
								pf_part_of_this_revolt_trigger = yes
								has_weak_claim = event_target:former_primary_title
							}
							event_target:current_holder = { abdicate_to = PREV }
							if = {
								limit = { event_target:current_liege = { always = yes } }
								hidden_tooltip = { set_defacto_liege = event_target:current_liege }
							}
						}
					}
				}
				# give primary title to rebel with a strong claim if no rebel pretender exists
				if = {
					limit = {
						event_target:current_holder = {
							any_opinion_modifier_target = {
								religion = ROOT
								pf_part_of_this_revolt_trigger = yes
								has_strong_claim = event_target:former_primary_title
							}
							NOT = {
								any_opinion_modifier_target = {
									religion = ROOT
									pf_part_of_this_revolt_trigger = yes
									any_pretender_title = { title = event_target:former_primary_title }
								}
							}
						}
					}
					event_target:current_holder = {
						random_opinion_modifier_target = {
							limit = {
								religion = ROOT
								pf_part_of_this_revolt_trigger = yes
								has_strong_claim = event_target:former_primary_title
							}
							event_target:current_holder = { abdicate_to = PREV }
							if = {
								limit = { event_target:current_liege = { always = yes } }
								hidden_tooltip = { set_defacto_liege = event_target:current_liege }
							}
						}
					}
				}
				# give primary title to rebel who is a pretender
				if = {
					limit = {
						event_target:current_holder = {
							any_opinion_modifier_target = {
								religion = ROOT
								pf_part_of_this_revolt_trigger = yes
								any_pretender_title = { title = event_target:former_primary_title }
							}
						}
					}
					event_target:current_holder = {
						random_opinion_modifier_target = {
							limit = {
								religion = ROOT
								pf_part_of_this_revolt_trigger = yes
								any_pretender_title = { title = event_target:former_primary_title }
							}
							event_target:current_holder = { abdicate_to = PREV }
							if = {
								limit = { event_target:current_liege = { always = yes } }
								hidden_tooltip = { set_defacto_liege = event_target:current_liege }
							}
						}
					}
				}
			}
		}
		
		clr_global_flag = pf_teardown_lock
		
		# figure out who are the supporters, losers, and neutrals
		hidden_tooltip = {
			event_target:former_primary_title = {
				holder_scope = {
					# clear previous civil war opinions
					pf_civil_war_clear_old_opinions_effect = yes
					
					# mark rebels as supporters
					event_target:current_holder = {
						any_opinion_modifier_target = {
							limit = {
								pf_part_of_this_revolt_trigger = yes
								NOT = { character = PREVPREV }
							}
							reverse_opinion = {
								who = PREVPREV
								modifier = opinion_civil_war_supporter
								months = 60
							}
						}
					}
					
					# mark former liege and loyalists as losers
					any_vassal = {
						limit = {
							has_opinion_modifier = {
								who = event_target:current_holder
								modifier = pf_loyal_toward
							}
						}
						reverse_opinion = {
							who = LIEGE
							modifier = opinion_civil_war_loser
							months = 60
						}
					}
					event_target:current_holder = {
						reverse_opinion = {
							who = PREV
							modifier = opinion_civil_war_loser
							months = 60
						}
					}
					
					# remember those who chose to be neutral
					any_vassal = {
						limit = {
							has_opinion_modifier = {
								who = event_target:current_holder
								modifier = pf_neutral_toward
							}
						}
						reverse_opinion = {
							who = LIEGE
							modifier = opinion_civil_war_neutral
							months = 60
						}
					}
				}
			}
		}
		
		# check whether the primary title should be destroyed due to CA loss
		if = {
			limit = { event_target:former_primary_title = { has_title_flag = destroy_title } }
			hidden_tooltip = {
				event_target:former_primary_title = {
					holder_scope = {
						character_event = { id = Plus.1620 days = 4 } #destroy title
					}
				}
			}
		}

		# clean up remaining TOMs
		pf_civil_war_cleanup_effect = yes	
		
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 250
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 250 }
		}
		
		# post-civil war event to reward allies
		event_target:former_primary_title = {
			holder_scope = {
				#emf_dynlevy_update_effect = yes
				hidden_tooltip = { character_event = { id = PlusFaction.450 } }
			}
		}
		
		# change faction moods
		FROM = { hidden_tooltip = { character_event = { id = PlusFaction.200 days = 1 } } }
		ROOT = { hidden_tooltip = { character_event = { id = PlusFaction.200 days = 1 } } }
		
		#emf_cb_dynlevy_end_effect = yes
	}

	on_fail = {
		log = "CB(religious_overthrow_ruler): WHITEPEACE: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"

		#emf_cb_dynlevy_end_effect = yes
		
		# clean up remaining civil war state
		pf_civil_war_cleanup_effect = yes
		
		ROOT = {
			prestige = -100
			pf_tradition_minus2_effect = yes
		}
		hidden_tooltip = {
			FROM = {
				remove_opinion = {
					who = ROOT
					modifier = declared_war
				}
			}
		}
	}

	on_reverse_demand = {
		log = "CB(religious_overthrow_ruler): DEFEAT: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"

		#emf_cb_obj_win_war_reverse_effect = yes
		#emf_cb_dynlevy_end_effect = yes
		
		FROM = {
			# the defender loses 1 Imperial Decadence
			imperial_decadence_minus_1_effect = yes
			# the defender gains Tradition faction boost
			pf_tradition_plus4_effect = yes
			# add victory modifier for the 'Win a War' ambition
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			# remove any previously-existing loser TOMs
			pf_civil_war_clear_old_opinions_effect = yes
		}
		
		# apply immediate effects to the revolters
		any_attacker = {
			limit = { has_opinion_modifier = { who = FROM modifier = revolting_against } }
			imprison = FROM
			hidden_tooltip = {
				reverse_opinion = { who = FROM modifier = opinion_civil_war_loser months = 60 }
				opinion = { who = FROM modifier = opinion_coerced_into_leaving_faction months = 60 }
			}
		}

		# mark loyalists and neutrals for the reward event
		hidden_tooltip = {
			FROM = {
				any_vassal = {
					limit = { reverse_has_opinion_modifier = { who = PREV modifier = pf_loyal_to_me } }
					reverse_opinion = { who = PREV modifier = opinion_civil_war_supporter months = 60 }
				}
				any_vassal = {
					limit = { reverse_has_opinion_modifier = { who = PREV modifier = pf_neutral_to_me } }
					reverse_opinion = { who = PREV modifier = opinion_civil_war_neutral months = 60 }
				}
			}
		}

		# clean up remaining civil war state
		pf_civil_war_cleanup_effect = yes
		
		ROOT = {
			prestige = -200
			pf_tradition_minus4_effect = yes
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		
		# post-civil war event to reward allies
		FROM = {
			hidden_tooltip = { character_event = { id = PlusFaction.450 } }
		}
		
		# change faction moods
		hidden_tooltip = {
			FROM = { character_event = { id = PlusFaction.200 days = 1 } }
			ROOT = { character_event = { id = PlusFaction.200 days = 1 } }
		}
	}

	on_invalidation = {
		log = "CB(religious_overthrow_ruler): INVALIDATED: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"

		#emf_cb_dynlevy_end_effect = yes
		
		# clean up remaining civil war state
		pf_civil_war_cleanup_effect = yes
	}
	
	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
	}
}

italian_reconquest = {
	name = CB_NAME_ITALIAN_RECONQUEST
	war_name = WAR_ITALIAN_RECONQUEST
	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
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no

	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		
		ROOT = {
			# the attacker is a Christian with the italian_reconquest modifier
			has_character_modifier = italian_reconquest
			religion_group = christian
			# the Italian reconquest has begun and not succeeded or failed
			has_global_flag = italian_reconquest_started
			NOR = { 
				has_global_flag = italian_rec_success
				has_global_flag = italian_rec_failure
			}
		}
	}
	
	can_use_title = {
		# the target is an independent non-Christian who owns a duchy in Italy
		FROM = {
			NOT = { religion_group = christian }
			independent = yes # Don't attack revolters - chances are high the war will invalidate
		}	
		OR = {
			title = d_benevento
			title = d_capua
			title = d_apulia
			title = d_salerno
			title = d_calabria
			title = d_sicily
		}
	}
	
	on_add = {
		# the Pope will automatically join the war on the attacker's side
		ROOT = {
			character_event = { id = Plus.507 }
		}
	}

	is_valid = {
		ROOT = {
			has_character_modifier = italian_reconquest
		}
		NOR = { 
			has_global_flag = italian_rec_success
			has_global_flag = italian_rec_failure
		}
	}
	
	on_attacker_leader_death = {
		# give tivoli back to the pope
		if = {
			limit = {
				b_tivoli = {
					owner = {
						character = ROOT
					}
				}
			}
			ROOT = {
				religion_head = {
					b_tivoli = {
						gain_title = PREV
					}
				}
			}
		}
		# disband army and inform everyone of the reclamation failure
		disband_event_forces = yes
		end_war = invalid
		set_global_flag = italian_rec_failure
		any_playable_ruler = { narrative_event = { id = Plus.517 } }
	}

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			pf_tradition_plus4_effect = yes
		}
		FROM = {
			pf_glory_minus2_effect = yes
			pf_tradition_minus4_effect = yes
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_piety = 50
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_piety = 50 participation_scaled_prestige = 100 }
		}
		# the first time the CB is successful the attacker gets a claim to Sicily
		if = {
			limit = {
				NOT = { has_global_flag = first_war_success }
			}
			set_global_flag = first_war_success
			ROOT = { 
				add_character_modifier = { name = "holy_truce_timer" duration = 3650 } 
				k_sicily = { add_pressed_claim = ROOT } 
			}
		}
		# so long as the attacker is not a heretic then Christianity gets the holy war RA modifier
		if = {
			limit = {
				ROOT = { is_heretic = no }
			}
			ROOT = {
				religion_authority = {
					modifier = won_holy_war
					years = 20
				}
			}
		}
		# so long as the defender is not a heretic then their religion gets the lost holy war RA modifier
		if = {
			limit = {
				FROM = { is_heretic = no }
			}
			FROM = { 
				religion_authority = {
					modifier = lost_holy_war
					years = 20
				}
			}
		}
		# remove the attacker's adventurer flag if they have it
		ROOT = {
			hidden_tooltip = {
				if = {
					limit = { has_character_flag = duchy_adventurer }
					clr_character_flag = duchy_adventurer
				}
			}
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their Italian reconquest for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		# the attacker gains the duchy and gives b_tivoli to the Pope
		hidden_tooltip = {
			if = {
				limit = { 
					has_holder = yes
					holder_scope = { 
						OR = {
							character = FROM
							is_liege_or_above = FROM
						}
					}
				}
				set_global_flag = usurp_duchy
			}
		}
		
		pf_liege_change_under_title_begin_effect = yes
		
		ROOT = {
			gain_settlements_under_title = {
				title = PREV
				enemy = FROM
				type = invasion
			}
		}
		hidden_tooltip = {
			if = {
				limit = { 
					has_global_flag = usurp_duchy
				}
				destroy_landed_title = yes
				usurp_title = ROOT
				clr_global_flag = usurp_duchy
			}
			if = {
				limit = {
					b_tivoli = {
						owner = {
							character = ROOT
						}
					}
				}
				ROOT = {
					religion_head = {
						b_tivoli = {
							gain_title = PREV
						}
					}
				}
			}			
		}
		
		pf_liege_change_under_title_end_effect = yes
	}

	on_fail = {	
		FROM = {
			prestige = 100
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor
					who = ROOT
					months = 1200
				}
			}
		}
		if = {
			limit = {
				b_tivoli = {
					holder_scope = {
						character = ROOT
					}
				}
			}
			ROOT = {
				religion_head = {
					b_tivoli = {
						gain_title = PREV
					}
				}
			}
		}		
		ROOT = {
			wealth = -500
			prestige = -100 
		}
		# narrative notification to the world that the invasion failed
		if = {
			limit = {
				NOT = { has_global_flag = first_war_success }
			}
			set_global_flag = italian_rec_failure
			any_playable_ruler = { narrative_event = { id = Plus.517 } }
		}		
	}

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			wealth = -100
		}

		if = {
			limit = {
				b_tivoli = {
					holder_scope = {
						character = ROOT
					}
				}
			}
			ROOT = {
				religion_head = {
					b_tivoli = {
						gain_title = PREV
					}
				}
			}
		}		

		FROM = {
			prestige = 200
			wealth = 100
		}

		ROOT = {
			prestige = -200
			pf_glory_minus2_effect = yes
		}
	}

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

carolingian_reconquest = {
	name = CB_NAME_CAROLINGIAN
	war_name = WAR_NAME_CAROLINGIAN
	sprite = 17
	truce_days = 3650
	is_permanent = yes
	#is_holy_war = yes
	check_de_jure_tier = DUKE # this scans all dejure duchies for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no
	
	infamy_modifier = 0.75

	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		
		ROOT = {
			higher_real_tier_than = DUKE
			independent = yes
			OR = {
				religion = catholic
				religion = chalcedonian
			}
			rightful_religious_head_scope = {
				NOT = { character = PREV }
				NOT = { character = FROM }
				OR = {
					has_landed_title = k_papal_state
					has_landed_title = k_papal_chalcedon
				}
			}
			NOR = {
				same_realm = FROM
				is_title_active = e_karling
				has_character_flag = former_carolingian_emperor
				e_hre = { has_title_flag = hre_formed }
			}
		}
		FROM = {
			religion_group = ROOT
			NOT = { is_heresy_of = ROOT }
			NOT = { is_parent_religion = ROOT }
		}
	}

	can_use_title = {
		de_jure_liege_or_above = e_hre
		# either the target has the title or their top liege is the target
		OR = {
			FROM = { has_landed_title = PREV }
			holder_scope = { is_liege_or_above = FROM }
		}
	}

	is_valid = {
		ROOT = {
			independent = yes
			OR = {
				religion = catholic
				religion = chalcedonian
			}
			NOT = { same_realm = FROM }
		}
	}
	
	is_valid_title = {
		# either the target has the title or their top liege is the target
		OR = {
			FROM = { has_landed_title = PREV }
			holder_scope = { is_liege_or_above = FROM }
		}
	}

	on_success = {
		ROOT = {
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = 100
			}
		}
		FROM = {
			prestige = -100
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their Carolingian reconquest for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		
		pf_liege_change_under_title_begin_effect = yes
		
		custom_tooltip = {
			text = tribal_invasion_succ_tip
			hidden_tooltip = {
				ROOT = {
					vassalize_or_take_under_title = {
						title = PREV
						enemy = FROM
						type = invasion
					}
				}
			}
		}
		if = {
			limit = {
				holder_scope = { character = FROM }
			}
			usurp_title_only = { target = ROOT type = invasion }
		}
		
		pf_liege_change_under_title_end_effect = yes
	}

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

	on_reverse_demand = {
		FROM = {
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			prestige = -200
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_piety = 100
			participation_scaled_prestige = 200
			dynasty_head = {
				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
				dynasty_head = {
					if = {
						limit = { uses_decadence = yes }
						participation_scaled_decadence = -10
					}
				}
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = {
		factor = 1
		modifier = {
			factor = 0
			ROOT = { NOT = { has_landed_title = k_germany } }
			k_germany = {
				has_holder = yes
				holder_scope = { realm_size = 75 }
			}
		}
	}
}

byzantine_vassalize = {
	name = CB_NAME_BYZANTINE_CONQUEST
	war_name = WAR_NAME_BYZANTINE_VASSALIZE
	sprite = 17
	truce_days = 3650
	is_permanent = yes
	#is_holy_war = 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
	hostile_against_others = yes
	can_ask_to_join_war = yes
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no
	
	infamy_modifier = 0.5
	
	can_use_gui = {
		ROOT = { trait = strong_basileus }
		e_byzantium = {
			custom_tooltip = {
				text = DE_JURE_TOOLTIP1
				hidden_tooltip = {
					OR = {
						has_law = crown_authority_2
						has_law = crown_authority_3
						has_law = crown_authority_4
					}
				}
			}
		}
	}

	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { attacking_religious_head_trigger = no }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }  
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		
		# target is a neighbor or close
		FROM = {
			OR = {
				any_realm_province = {
					any_neighbor_province = {
						owner = {
							OR = {
								same_realm = ROOT
								character = ROOT
							}
						}
					}
				}
				any_realm_province = {
					ROOT = { 
						any_realm_province = {
							NOT = {
								distance = { 
									where = PREVPREV
									value = 75
								}
							}
						}
					}
				}
			}
		}
		
		# attacker is a strong byzantine emperor
		ROOT = {
			independent = yes
			has_landed_title = e_byzantium
		}
	}

	can_use_title = {
		tier = DUKE
		is_titular = no
		capital_scope = {
			OR = {
				region = custom_byzantium
				region = custom_nikaea
				region = custom_armenia
				region = custom_bulgaria
				region = custom_serbia
				region = custom_croatia
				region = custom_trebizond
				region = custom_sicily
				region = custom_syria
				region = custom_jerusalem
			}
		}
		# either the target has the title or their top liege is the target
		# or the title is not held and de jure counties are
		OR = {
			FROM = { has_landed_title = PREV }
			holder_scope = { is_liege_or_above = FROM }
			AND = {
				has_holder = no
				any_direct_de_jure_vassal_title = {
					OR = {
						FROM = { has_landed_title = PREV }
						holder_scope = { is_liege_or_above = FROM }
					}
				}
			}
		}
		# target is an independent ruler of the same religion group, as is their top liege
		holder_scope = {
			religion_group = ROOT
			NOR = {
				is_heresy_of = ROOT
				is_parent_religion = ROOT
			}
			top_liege = {
				religion_group = ROOT
				NOR = {
					is_heresy_of = ROOT
					is_parent_religion = ROOT
				}
			}
		}
	}
	
	is_valid_title = {
		ROOT = {
			independent = yes
			has_landed_title = e_byzantium
			trait = strong_basileus
		}
		# either the target has the title or their top liege is the target
		# or the title is not held and de jure counties are
		OR = {
			FROM = { has_landed_title = PREV }
			holder_scope = { is_liege_or_above = FROM }
			AND = {
				has_holder = no
				any_direct_de_jure_vassal_title = {
					OR = {
						FROM = { has_landed_title = PREV }
						holder_scope = { is_liege_or_above = FROM }
					}
				}
			}
		}
		# target is an independent ruler of the same religion group, as is their top liege
		holder_scope = {
			religion_group = ROOT
			NOR = {
				is_heresy_of = ROOT
				is_parent_religion = ROOT
			}
			top_liege = {
				religion_group = ROOT
				NOR = {
					is_heresy_of = ROOT
					is_parent_religion = ROOT
				}
			}
		}
	}
  
	on_success = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their Byzantine subjugate CB against [From.GetBestName] (ID: [From.GetID])"
		ROOT = {
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		FROM = {
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}
	}
	
	on_success_title = {
		pf_liege_change_under_title_begin_effect = yes
		
		if = {
			#duke title has a holder
			limit = { has_holder = yes }
			if = {
				#holder is a king or lower who doesn't hold any other county titles outside the region
				limit = {
					holder_scope = {
						lower_tier_than = ROOT
						NOT = {
							any_demesne_title = {
								tier = COUNT
								location = {
									NOR = {
										region = custom_byzantium
										region = custom_nikaea
										region = custom_armenia
										region = custom_bulgaria
										region = custom_serbia
										region = custom_croatia
										region = custom_trebizond
										region = custom_sicily
										region = custom_syria
										region = custom_jerusalem
									}
								}
							}
						}
					}
				}
				#holder is either a king/emperor or has other duke titles outside the region
				holder_scope = {
					hidden_tooltip = {
						opinion = {
							who = ROOT
							modifier = force_vassalized
							months = 240
						}
					}
					set_viceroy_exempt_effect = yes
					set_defacto_liege = ROOT
				}
			}
			if = {
				limit = {
					holder_scope = {
						OR = {
							tier = EMPEROR
							any_demesne_title = {
								tier = COUNT
								location = {
									NOR = {
										region = custom_byzantium
										region = custom_nikaea
										region = custom_armenia
										region = custom_bulgaria
										region = custom_serbia
										region = custom_croatia
										region = custom_trebizond
										region = custom_sicily
										region = custom_syria
										region = custom_jerusalem
									}
								}
							}
						}
					}
				}
				usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
			}
		}
		if = {
			#duke title is not held by anyone
			limit = { has_holder = no }
			#usurp any counties held by emperors or dukes+ with titles outside the area
			any_direct_de_jure_vassal_title = {
				limit = {
					holder_scope = {
						OR = {
							character = FROM
							is_liege_or_above = FROM
						}
						OR = {
							tier = EMPEROR
							AND = {
								lower_tier_than = ROOT
								any_demesne_title = {
									tier = COUNT
									location = {
										NOR = {
											region = custom_byzantium
											region = custom_nikaea
											region = custom_armenia
											region = custom_bulgaria
											region = custom_serbia
											region = custom_croatia
											region = custom_trebizond
											region = custom_sicily
											region = custom_syria
											region = custom_jerusalem
										}
									}
								}
							}
						}
					}
				}
				usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
			}
			#vassalize any de jure counts in the duchy who have no counties elsewhere
			any_direct_de_jure_vassal_title = {
				limit = {
					holder_scope = {
						OR = {
							character = FROM
							is_liege_or_above = FROM
						}
						lower_tier_than = ROOT
						NOT = {
							any_demesne_title = {
								tier = COUNT
								location = {
									NOR = {
										region = custom_byzantium
										region = custom_nikaea
										region = custom_armenia
										region = custom_bulgaria
										region = custom_serbia
										region = custom_croatia
										region = custom_trebizond
										region = custom_sicily
										region = custom_syria
										region = custom_jerusalem
									}
								}
							}
						}
					}
				}
				holder_scope = {
					hidden_tooltip = {
						if = {
							limit = {
								NOT = {
									has_opinion_modifier = { who = ROOT modifier = force_vassalized }
								}
							}
							opinion = {
								who = ROOT
								modifier = force_vassalized
								months = 240
							}
						}
					}
					set_viceroy_exempt_effect = yes
					set_defacto_liege = ROOT
				}
			}
		}
		
		pf_liege_change_under_title_end_effect = yes
	}

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

	on_reverse_demand = {
		FROM = {
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			remove_trait = strong_basileus
			hidden_tooltip = { add_trait = weak_basileus }
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}
		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 = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = {
		factor = 0.9
	}
}

carolingian_manifest_destiny = {
	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
	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
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no
	
	infamy_modifier = 0.75

	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		
		ROOT = {
			dynasty = 25061
			independent = yes
			OR = {
				religion = catholic
				religion = chalcedonian
			}
			higher_real_tier_than = DUKE
			NOT = { e_hre = { has_title_flag = hre_formed } }
			NOT = { same_realm = FROM }
			NOT = { rightful_religious_head = FROM }
		}
		FROM = {
			NOT = { dynasty = ROOT }
			religion = ROOT
			NOT = { has_landed_title = k_papal_state }
			NOT = { has_landed_title = k_papal_chalcedon }
		}
	}

	can_use_title = {
		OR = {
			AND = {
				e_karling = { NOT = { has_title_flag = carolingian_empire_formed } }
				kingdom = {
					OR = {
						title = k_france
						title = k_lotharingia
						title = k_bavaria
						title = k_saxony
						title = k_italy
						title = k_lombardy
					}
				}
			}
			AND = {
				ROOT = { trait = paragon }
				kingdom = {
					OR = {
						title = k_france
						title = k_lotharingia
						title = k_bavaria
						title = k_saxony
						title = k_germany
						title = k_italy
						title = k_lombardy
						title = k_frisia
						title = k_burgundy
						title = k_aquitaine
					}
				}
			}
		}
		
		# 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 = {
		ROOT = {
			dynasty = 25061
			independent = yes
			OR = {
				religion = catholic
				religion = chalcedonian
			}
			NOT = { same_realm = FROM }
		}
	}

	on_success = {
		ROOT = {
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = 100
			}
		}
		FROM = {
			prestige = -100
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their Carolingian manifest destiny for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		
		pf_liege_change_under_title_begin_effect = yes
		
		custom_tooltip = {
			text = tribal_invasion_succ_tip
			hidden_tooltip = {
				ROOT = {
					vassalize_or_take_under_title = {
						title = PREV
						enemy = FROM
						same_religion = yes
						type = invasion
					}
				}
			}
		}
		if = {
			limit = {
				holder_scope = { character = FROM }
			}
			usurp_title_only = { target = ROOT type = invasion }
		}
		
		pf_liege_change_under_title_end_effect = yes
	}

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

	on_reverse_demand = {
		FROM = {
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			prestige = -200
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_piety = 100
			participation_scaled_prestige = 200
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_piety = 100
				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
	}
}

dejure_duchy_claim_holy = {
	name = CB_NAME_DEJURECLAIM
	war_name = WAR_NAME_DEJURECLAIM
	sprite = 16
	truce_days = 3650
	hostile_against_others = yes
	is_permanent = yes
	is_holy_war = 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 = yes
	
	allowed_to_target_tributaries = yes
	allowed_to_target_suzerains = no
	
	infamy_modifier = 0.5
	
	can_use_gui = {
		ROOT = {
			top_liege = { religion_group = PREV }
			OR = {
				is_patrician = no
				independent = yes
			}
		}
		#attacker holds either the empire or liege title
		FROMFROM = {
			OR = {
				kingdom = {
					holder = ROOT
					custom_tooltip = {
						text = DE_JURE_TOOLTIP1
						hidden_tooltip = {
							OR = {
								has_law = crown_authority_2
								has_law = crown_authority_3
								has_law = crown_authority_4
							}
						}
					}
				}
				empire = {
					holder = ROOT
					custom_tooltip = {
						text = DE_JURE_TOOLTIP1
						hidden_tooltip = {
							OR = {
								has_law = crown_authority_2
								has_law = crown_authority_3
								has_law = crown_authority_4
							}
						}
					}
				}
			}
		}
	}

	can_use = {
		#check to ensure the target is a valid for a holy war
		ROOT = {
			OR = {
				NOT = { religion_group = pagan_group }
				is_reformed_religion = yes
			}
			OR = {
				NOR = {
					religion = hindu
					religion = buddhist
				}
				FROM = { NOT = { religion_group = indian_group } }
			}
			OR = {
				NOT = { religion_group = FROM }
				is_heresy_of = FROM
				is_parent_religion = FROM
				AND = {
					OR = {
						religion_group = muslim
						religion_group = pagan_group
						religion = paulician
					}
					NOT = { religion = FROM }
				}
			}
			NOR = {
				pacifist = yes
				same_realm = FROM
			}
		}
	}

	can_use_title = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		
		# target title is Duke level and non-titular
		is_titular = no
		tier = DUKE
		# either the target has the title or their top liege is the target
		OR = {
			FROM = { has_landed_title = PREV }
			holder_scope = { is_liege_or_above = FROM }
		}
		# a king/emperor is using it to reclaim own de jure duchy
		OR = {
			empire = { holder = ROOT }
			kingdom = { holder = ROOT }
		}
		# neither the attacker nor their top liege has the title
		NOT = {
			ROOT = { has_landed_title = PREV }
			holder_scope = { is_liege_or_above = ROOT }
		}
		
		# AI will only use on AI holy orders if they're kings/emperors or of a different religion
		OR = {
			ROOT = { ai = no }
			FROM = {
				OR = {
					ai = no
					holy_order = no
					higher_tier_than = duke
					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_add = {
		# lose 500 piety if the target is a holy order of the same religion
		if = {
			limit = {
				FROM = {
					religion = ROOT
					holy_order = yes
				}
			}
			ROOT = { piety = -500 }
		}
		FROM = {
			attacked_tributary_cb_effect = yes
		}
	}

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		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 }
			hidden_tooltip = { participation_scaled_piety = 100 }
		}
		FROM = {
			pf_glory_minus2_effect = yes
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their holy de jure duchy claim for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		#change imperial decadence
		if = {
			limit = {
				empire = {
					ROOT = { has_landed_title = PREV }
					is_primary_holder_title = yes
				}
			}
			ROOT = { imperial_decadence_minus_1_effect = yes }
		}
		
		empty_province_effect = yes
		
		pf_liege_change_under_title_begin_effect = yes
		
		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
			}
		}
		usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
		
		pf_liege_change_under_title_end_effect = yes
	}

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

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			piety = -100
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			pf_glory_minus2_effect = yes
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
			participation_scaled_piety = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
			hidden_tooltip = { participation_scaled_piety = 100 }
		}
	}

	on_reverse_demand_title = {
		#change imperial decadence
		if = {
			limit = {
				empire = {
					ROOT = { has_landed_title = PREV }
					is_primary_holder_title = yes
				}
			}
			ROOT = { imperial_decadence_plus_1_effect = yes }
		}
	}
	
	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = {
		factor = 1
		modifier = {
			factor = 0
			ROOT = {
				culture = mongol
				religion_group = pagan_group
			}
			FROM = {
				culture = mongol
				religion_group = pagan_group
			}
		}
		modifier = { # The AI will never breach a tributary's trust
			factor = 0
			ai_attack_tributary_trigger = yes
		}
	}
}

dejure_county_claim_holy = { #ducal
	name = CB_NAME_DEJURECLAIM
	war_name = WAR_NAME_DEJURECLAIM
	sprite = 16
	truce_days = 3650
	hostile_against_others = yes
	is_permanent = yes
	is_holy_war = 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 = yes
	
	allowed_to_target_tributaries = yes
	allowed_to_target_suzerains = no
	
	infamy_modifier = 0.5
	
	can_use_gui = {
		ROOT = {
			top_liege = { religion_group = PREV }
			OR = {
				is_patrician = no
				independent = yes
			}
		}
		#attacker holds either the de jure kingdom, empire or liege title
		FROMFROM = {
			OR = {
				dejure_liege_title = {
					holder = ROOT
				}
				kingdom = {
					holder = ROOT
					custom_tooltip = {
						text = DE_JURE_TOOLTIP1
						hidden_tooltip = {
							OR = {
								has_law = crown_authority_2
								has_law = crown_authority_3
								has_law = crown_authority_4
							}
						}
					}
				}
				empire = {
					holder = ROOT
					custom_tooltip = {
						text = DE_JURE_TOOLTIP1
						hidden_tooltip = {
							OR = {
								has_law = crown_authority_2
								has_law = crown_authority_3
								has_law = crown_authority_4
							}
						}
					}
				}
			}
		}
	}

	can_use = {
		#check to ensure the target is a valid for a holy war
		ROOT = {
			OR = {
				NOT = { religion_group = pagan_group }
				is_reformed_religion = yes
			}
			OR = {
				NOR = {
					religion = hindu
					religion = buddhist
				}
				FROM = { NOT = { religion_group = indian_group } }
			}
			OR = {
				NOT = { religion_group = FROM }
				is_heresy_of = FROM
				is_parent_religion = FROM
				AND = {
					OR = {
						religion_group = muslim
						religion_group = pagan_group
						religion = paulician
					}
					NOT = { religion = FROM }
				}
			}
			NOR = {
				pacifist = yes
				same_realm = FROM
			}
		}
	}
	
	can_use_title = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		
		tier = count
		
		#target owns the county or it belongs to their vassal
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
		
		#attacker holds either the de jure kingdom, empire or liege title
		OR = {
			empire = { holder = ROOT }
			kingdom = { holder = ROOT }
			dejure_liege_title = { holder = ROOT }
		}
		
		#attacker does not have the county title and the title holder is not their liege
		NOT = {
			OR = {
				ROOT = {
					has_landed_title = PREV
				}
				holder_scope = {
					is_liege_or_above = ROOT
				}
			}
		}
		#AI will only attack a holy order if they are a player, of a different religion or are a king/emperor
		OR = {
			ROOT = { ai = no }
			FROM = {
				OR = {
					ai = no
					holy_order = no
					higher_tier_than = duke
					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_add = {
		if = {
			limit = {
				FROM = {
					religion = ROOT
					holy_order = yes
				}
			}
			ROOT = { piety = -500 }
		}
		FROM = {
			attacked_tributary_cb_effect = yes
		}
	}

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		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 }
			hidden_tooltip = { participation_scaled_piety = 100 }
		}
		FROM = {
			pf_glory_minus2_effect = yes
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their holy de jure county claim for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		#change imperial decadence
		if = {
			limit = {
				empire = {
					ROOT = { has_landed_title = PREV }
					is_primary_holder_title = yes
				}
			}
			ROOT = { imperial_decadence_minus_1_effect = yes }
		}
		empty_province_effect = yes
		
		pf_liege_change_under_title_begin_effect = yes
		
		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
			}
		}
		usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
		
		pf_liege_change_under_title_end_effect = yes
	}

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

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -200
			piety = -100
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			pf_glory_minus2_effect = yes
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
			participation_scaled_piety = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
			hidden_tooltip = { participation_scaled_piety = 100 }
		}
	}
	
	on_reverse_demand_title = {
		#change imperial decadence
		if = {
			limit = {
				empire = {
					ROOT = { has_landed_title = PREV }
					is_primary_holder_title = yes
				}
			}
			ROOT = { imperial_decadence_plus_1_effect = yes }
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = {
		factor = 1
		modifier = {
			factor = 0
			ROOT = {
				culture = mongol
				religion_group = pagan_group
			}
			FROM = {
				culture = mongol
				religion_group = pagan_group
			}
		}
		modifier = { # The AI will never breach a tributary's trust
			factor = 0
			ai_attack_tributary_trigger = yes
		}
	}
}

dejure_barony_claim_holy = {
	name = CB_NAME_DEJUREBARONYCLAIM
	war_name = WAR_NAME_DEJUREBARONYCLAIM
	sprite = 16
	truce_days = 1825
	hostile_against_others = yes
	is_permanent = yes
	is_holy_war = yes
	check_de_jure_tier = COUNT
	can_ask_to_join_war = yes
	
	allowed_to_target_tributaries = yes
	allowed_to_target_suzerains = no
	
	infamy_modifier = 0.5
	
	can_use_gui = {
		ROOT = {
			top_liege = { religion_group = PREV }
			OR = {
				is_patrician = no
				independent = yes
			}
		}
	}

	can_use = {
		#check to ensure the target is a valid for a holy war
		ROOT = {
			OR = {
				NOT = { religion_group = pagan_group }
				is_reformed_religion = yes
			}
			OR = {
				NOR = {
					religion = hindu
					religion = buddhist
				}
				FROM = { NOT = { religion_group = indian_group } }
			}
			OR = {
				NOT = { religion_group = FROM }
				is_heresy_of = FROM
				is_parent_religion = FROM
				AND = {
					OR = {
						religion_group = muslim
						religion_group = pagan_group
						religion = paulician
					}
					NOT = { religion = FROM }
				}
			}
			NOR = {
				pacifist = yes
				same_realm = FROM
			}
		}
		
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		OR = {
			ROOT = { ai = no }
			FROM = {
				OR = {
					ai = no
					holy_order = no
					higher_tier_than = duke
					NOT = { religion = ROOT }
					NOT = { has_dlc = "Sons of Abraham" }
				}
			}
		}
	}

	can_use_title = {
		#de jure liege title belongs to attacker or a vassal
		has_holder = yes
		holder_scope = {
			OR = {
				character = ROOT
				is_liege_or_above = ROOT
			}
		}
		any_direct_de_jure_vassal_title = {
			holder_scope = {
				OR = {
					character = FROM
					is_liege_or_above = FROM
				}
				NOR = {
					character = ROOT
					is_liege_or_above = ROOT
				}
			}
		}
	}

	on_add = {
		if = {
			limit = {
				FROM = {
					religion = PREV
					holy_order = yes
				}
			}
			piety = -500
		}
		FROM = {
			attacked_tributary_cb_effect = yes
		}
	}

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		FROM = {
			pf_glory_minus2_effect = yes
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their holy de jure barony claim for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		holder_scope = {
			save_event_target_as = target_taker
		}	
		any_direct_de_jure_vassal_title = {
			limit = {
				holder_scope = {
					OR = {
						character = FROM
						is_liege_or_above = FROM
					}
					NOR = {
						character = event_target:target_taker
						is_liege_or_above = event_target:target_taker
						is_liege_or_above = ROOT
					}
				}
			}
			usurp_title_plus_barony_if_unlanded = { target = event_target:target_taker type = claim }
		}		
		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 }
			hidden_tooltip = { participation_scaled_piety = 50 }
		}
	}

	on_fail = {
		prestige = -50
		piety = -25
	}

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -100
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			pf_glory_minus2_effect = yes
		}
		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 }
			hidden_tooltip = { 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
	}

}

other_dejure_county_claim_holy = {
	name = CB_NAME_DEJURECLAIM
	war_name = WAR_NAME_OTHER_DEJURECLAIM
	sprite = 16
	truce_days = 3650
	hostile_against_others = yes
	is_permanent = yes
	is_holy_war = yes
	other_de_jure_claim = yes # Optimization for de jure claims of vassals
	press_claim = yes
	can_ask_to_join_war = yes
	
	allowed_to_target_tributaries = yes
	allowed_to_target_suzerains = no
	
	infamy_modifier = 0.3
	
	can_use_gui = {
		top_liege = { religion_group = PREV }
		OR = {
			is_patrician = no
			independent = yes
		}
		#attacker holds either the de jure kingdom, empire or liege title
		FROMFROM = {
			OR = {
				dejure_liege_title = {
					holder = ROOT
				}
				kingdom = {
					holder = ROOT
					custom_tooltip = {
						text = DE_JURE_TOOLTIP1
						hidden_tooltip = {
							OR = {
								has_law = crown_authority_2
								has_law = crown_authority_3
								has_law = crown_authority_4
							}
						}
					}
				}
				empire = {
					holder = ROOT
					custom_tooltip = {
						text = DE_JURE_TOOLTIP1
						hidden_tooltip = {
							OR = {
								has_law = crown_authority_2
								has_law = crown_authority_3
								has_law = crown_authority_4
							}
						}
					}
				}
			}
		}
	}
	
	can_use = {
		#check to ensure the target is a valid for a holy war
		OR = {
			NOT = { religion_group = pagan_group }
			is_reformed_religion = yes
		}
		OR = {
			NOR = {
				religion = hindu
				religion = buddhist
			}
			FROM = { NOT = { religion_group = indian_group } }
		}
		OR = {
			NOT = { religion_group = FROM }
			is_heresy_of = FROM
			is_parent_religion = FROM
			AND = {
				OR = {
					religion_group = muslim
					religion_group = pagan_group
					religion = paulician
				}
				NOT = { religion = FROM }
			}
		}
		NOR = {
			pacifist = yes
			same_realm = FROM
		}

		ai_not_busy_trigger = yes
		NOT = { has_character_modifier = holy_truce_timer }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		mercenary = no	
		NOT = {
			ROOT = {
				character = PREV
			}
		}
		#supported ruler is a vassal
		ROOT = {
			any_liege = {
				character = PREVPREV
			}
		}		
		#AI will only attack a holy order if they are a player, of a different religion or are a king/emperor
		OR = {
			ai = no
			FROM = {
				OR = {
					ai = no
					holy_order = no
					higher_tier_than = duke
					NOT = { religion = PREV }
					NOT = { has_dlc = "Sons of Abraham" }
				}
			}
		}
	}

	can_use_title = {
		tier = count
		#supporter ruler owns the de jure liege kingdom, empire or title of the county
		OR = {
			empire = { holder = ROOT }
			kingdom = { holder = ROOT }
			dejure_liege_title = { holder = ROOT }
		}
		#supporter ruler does not own the county, and it is not owned by their liege
		NOT = {
			OR = {
				ROOT = { has_landed_title = PREV }
				holder_scope = { is_liege_or_above = ROOT }
			}
		}
		#target owns the county or it is owned by their vassal
		OR = {
			FROM = { has_landed_title = PREV }
			holder_scope = { is_liege_or_above = FROM }
		}
	}

	is_valid_title = {
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
	}
	
	on_add = {
		if = {
			limit = {
				FROM = {
					religion = FROMFROM
					holy_order = yes
				}
			}
			FROMFROM = { piety = -500 }
		}
		FROM = {
			attacked_tributary_cb_effect = yes
		}
		ROOT = {
			hidden_tooltip = {
				set_character_flag = claim_being_pressed
			}
		}
	}

	on_success = {
		log = "[FromFrom.GetBestName] (ID: [FromFrom.GetID]) succeeded with [Root.GetBestName]'s (ID: [Root.GetID]) holy de jure county claim for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		ROOT = { add_character_modifier = { name = new_land_timer duration = 1 hidden = yes } }
		add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
		pf_glory_plus2_effect = yes
		ROOT = {
			opinion = {
				modifier = pressed_my_claim
				who = PREV
			}
			hidden_tooltip = {
				clr_character_flag = claim_being_pressed
			}
		}
		any_attacker = {
			limit = { character = PREV }
			participation_scaled_prestige = 200
			participation_scaled_piety = 100
		}
		any_attacker = {
			limit = { NOT = { character = PREV } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
			hidden_tooltip = { participation_scaled_piety = 100 }
		}
		FROM = {
			pf_glory_minus2_effect = yes
		}
	}

	on_success_title = {
		empty_province_effect = yes
		
		pf_liege_change_under_title_begin_effect = yes
		
		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
			}
		}
		usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
		
		pf_liege_change_under_title_end_effect = yes
	}

	on_fail = {
		prestige = -100
		piety = -50
		ROOT = {
			hidden_tooltip = {
				clr_character_flag = claim_being_pressed
			}
		}
	}
	
	on_invalidation = {
		ROOT = {
			hidden_tooltip = {
				clr_character_flag = claim_being_pressed
			}
		}
	}

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		prestige = -200
		piety = -100
		transfer_scaled_wealth = {
			to = FROM
			value = 4.0
		}
		pf_glory_minus2_effect = yes
		ROOT = {
			prestige = -200
			piety = -100
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
			participation_scaled_piety = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
			hidden_tooltip = { participation_scaled_piety = 100 }
		}
	}

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

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = {
		factor = 1
		modifier = {
			factor = 0
			ROOT = {
				culture = mongol
				religion_group = pagan_group
			}
			FROM = {
				culture = mongol
				religion_group = pagan_group
			}
		}
		modifier = { # The AI will never breach a tributary's trust
			factor = 0
			ai_attack_tributary_trigger = yes
		}
	}
}

# The CB used by "Decadence revolter" characters
decadence_revolt = {
	name = CB_NAME_DECADENCE_UPRISING
	war_name = WAR_NAME_DECADENCE_UPRISING
	sprite = 11
	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
	press_claim = yes
	can_ask_to_join_war = no
	is_independence = yes
	
	can_use = {
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		ROOT = { has_character_flag = decadence_revolter }
	}
	
	can_use_title = {
		tier = count
	}

	is_valid = {
		FROM = {
			religion_group = muslim
			religion_group = ROOT
		}
	}
	
	is_valid_title = {
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their decadence revolt against [From.GetBestName] (ID: [From.GetID])"
		ROOT = {
			hidden_tooltip = {
				if = {
					limit = { dynasty = none }
					dynasty = father_bastard
				}
			}
		}
		
		usurp_title_plus_barony_if_unlanded = { target = ROOT type = revolt }
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			prestige = 200
			piety = 100
			occupy_minors_of_occupied_settlements = FROM
			gain_all_occupied_titles = { who = FROM type = revolt }
			set_defacto_liege = ROOT
			clr_character_flag = decadence_revolter
		}
		
		FROM = {
			imprison = ROOT
			prestige = -500
			piety = -500
			clr_character_flag = has_current_revolt		
		}
	}

	on_fail = {
		ROOT = {
			imprison = FROM
			clr_character_flag = decadence_revolter
			hidden_tooltip = {
				add_character_modifier = { 
					name = broken_spirit
					days = 3650
				}
			}
		}
		FROM = {
			prestige = 50
			clr_character_flag = has_current_revolt
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor 
					who = ROOT
					months = 1200
				}
			}
		}
	}

	on_reverse_demand = {
		ROOT = {
			imprison = FROM
			clr_character_flag = decadence_revolter
			hidden_tooltip = {
				add_character_modifier = { 
					name = broken_spirit
					days = 3650
				}
			}
		}
		FROM = {
			prestige = 100
			piety = 100
			clr_character_flag = has_current_revolt
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor 
					who = ROOT
					months = 1200
				}
				add_character_modifier = { 
					name = crushed_revolt
					days = 3650
				}
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}
	
	attacker_ai_defeat_worth = {
		factor = 100
	}

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

# The Prepared Invasion CB of the Druze
druze_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
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no

	can_use_gui = {
		ROOT = {
			OR = {
				trait = druze_ajawid
				piety = 1000
				AND = {
					martial = 12
					diplomacy = 12
				}
			}
		}
	}
	
	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { religion_group = muslim }
		ROOT = {
			religion = druze
			NOT = { is_liege_or_above = FROM }
			NOT = { realm_size = 41 }
			mercenary = no
		}		
	}

	can_use_title = {
		is_valid_viking_invasion_target = FROM # Checks that FROM holds the right amount of territory in the kingdom
	}

	is_valid_title = {
		FROM = {
			any_realm_title = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}

	on_success = {
		ROOT = {
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = 500
			piety = 250
			religion_authority = {
				modifier = won_viking_invasion
			}
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 300
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 300 }
		}
		FROM = {
			prestige = -500
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their druze invasion for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		empty_province_effect = yes
		
		pf_liege_change_under_title_begin_effect = yes
		
		custom_tooltip = {
			text = tribal_invasion_succ_tip
			hidden_tooltip = {
				#attacker gains one province belonging to the target for certain
				if = {
					limit = { NOT = { capital_scope = { owner = { character = FROM } } } }
					FROM = {
						random_demesne_province = {
							limit = { county = { de_jure_liege_or_above = PREVPREVPREV } }
							culture = ROOT
							religion = ROOT
						}
					}
				}
				if = {
					limit = { capital_scope = { owner = { character = FROM } } }
					capital_scope = {
						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
						is_crusade = yes
						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 }
				}
			}
		}
		
		pf_liege_change_under_title_end_effect = yes
	}

	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 = {
		FROM = {
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -500
			piety = -250
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
			hidden_tooltip = {
				disband_event_forces = prepared_invasion
			}
			religion_authority = {
				modifier = lost_viking_invasion
			}
		}
		FROM = {
			prestige = 300
		}
		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 = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}	
}

reconquista_cb = {
	name = CB_NAME_RECONQUISTA
	war_name = WAR_NAME_RECONQUISTA
	sprite = 9
	truce_days = 3650
	is_permanent = yes
	can_ask_to_join_war = yes
	is_holy_war = yes
	check_de_jure_tier = DUKE
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no
	
	infamy_modifier = 0.75
	
	can_use_title = {
		capital_scope = { region = world_europe_west_iberia }
		FROM = {
			#must be neighbors
			any_realm_province = {
				duchy = { title = PREVPREVPREV }
				any_neighbor_province = {
					owner = { same_realm = ROOT }
				}
			}
		}
		ROOT = {
			#AI will usually only use the CB once every 5 years
			OR = {
				ai = no
				NOT = { has_character_modifier = holy_war_timer }
				PREV = { any_direct_de_jure_vassal_title = { any_direct_de_jure_vassal_title = { is_holy_site = ROOT } } }
			}
		}
	}
	
	can_use = {
		ROOT = { ai_not_busy_trigger = yes }
		FROM = { 
			NOR = { 
				has_character_modifier = holy_truce_timer
				has_character_modifier = crusade_block_timer 
			} 
		}
		ROOT = {
			capital_scope = { region = world_europe_west_iberia }
			religion_group = christian
			is_heretic = no
			independent = yes
			num_of_count_titles = 1
			is_nomadic = no
			mercenary = no
		}
		FROM = {
			independent = yes
			religion_group = muslim
		}
	}

	is_valid = {
		ROOT = {
			religion_group = christian
			is_heretic = no
		}
	}
	
	is_valid_title = {
		FROM = {
			any_realm_province = {
				duchy = { title = PREVPREVPREV }
			}
		}
	}
	
	on_add_title = {
		if = {
			limit = {
				ROOT = { has_opinion_modifier = { who = FROM modifier = holy_war_sanction } }
			}
			hidden_tooltip = {
				ROOT = { remove_opinion = { who = FROM modifier = holy_war_sanction } }
			}
		}
	}
	
	on_success = {
		ROOT = {
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			pf_tradition_plus4_effect = yes
		}
		FROM = {
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
			pf_tradition_minus4_effect = yes
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_piety = 200
			participation_scaled_prestige = 200
			dynasty_head = {
				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -10
				}
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_piety = 200
				participation_scaled_prestige = 200 
				dynasty_head = {
					if = {
						limit = { uses_decadence = yes }
						participation_scaled_decadence = -10
					}
				}
			}
		}		
		ROOT = {
			religion_authority = {
				modifier = won_holy_war
				years = 20
			}
		}
		if = {
			limit = { FROM = { is_heretic = no } }
			FROM = { 
				religion_authority = {
					modifier = lost_holy_war
					years = 20
				}
			}
		}
	}
	
	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their reconquista of the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		hidden_tooltip = {
			if = {
				limit = { 
					has_holder = yes
					holder_scope = { 
						OR = {
							character = FROM
							is_liege_or_above = FROM
						}
					}
				}
				set_global_flag = usurp_duchy
			}
		}
		empty_province_effect = yes
		pf_liege_change_under_title_begin_effect = yes
		
		ROOT = {
			hidden_tooltip = { add_character_modifier = { name = "holy_war_timer" duration = 1825 } }
			vassalize_or_take_under_title = {
				title = PREV
				enemy = FROM
				same_religion = yes # Only vassalize rulers of my religion
				is_religious = yes
				type = holy_war
			}
		}
		
		hidden_tooltip = {
			if = {
				limit = { has_global_flag = usurp_duchy }
				destroy_landed_title = yes
				usurp_title = ROOT
				clr_global_flag = usurp_duchy
			}
		}
		
		pf_liege_change_under_title_end_effect = yes
	}

	on_fail = {
		ROOT = {
			hidden_tooltip = { add_character_modifier = { name = "holy_war_timer" duration = 1825 } }
			piety = -100
			prestige = -100
		}
	}
	
	on_reverse_demand = {
		FROM = {
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			pf_tradition_plus4_effect = yes
		}
		ROOT = {
			imperial_decadence_plus_1_effect = yes
			hidden_tooltip = { add_character_modifier = { name = "holy_war_timer" duration = 1825 } }
			clr_character_flag = pledged_temple
			piety = -200
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			pf_glory_minus2_effect = yes
			pf_tradition_minus4_effect = yes
		}		
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
			participation_scaled_piety = 200
			dynasty_head = {
				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -10
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_prestige = 200 
				participation_scaled_piety = 200 
				dynasty_head = {
					if = {
						limit = { uses_decadence = yes }
						participation_scaled_decadence = -10
					}
				}
			}
		}
		if = {
			limit = { FROM = { is_heretic = no } }
			FROM = { 
				religion_authority = {
					modifier = won_holy_war
					years = 20
				}
			}
		}
		ROOT = {
			religion_authority = {
				modifier = lost_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 = 1.5
			FROM = {
				independent = yes
				NOT = { any_realm_title = { tier = COUNT count = 4 } }
			}
		}
 		modifier = {
			factor = 1.5
			any_direct_de_jure_vassal_title = {
				location = { religion = ROOT }
			}
		}
 		modifier = {
			factor = 0.75
			NOT = {
				any_direct_de_jure_vassal_title = {
					location = { religion = ROOT }
				}
			}
		}
		modifier = {
			factor = 1.2
			ROOT = { trait = zealous }
		}	
		modifier = {
			factor = 0.8
			ROOT = { trait = cynical }
		}
		modifier = {
			factor = 1.2
			any_direct_de_jure_vassal_title = {
				any_direct_de_jure_vassal_title = {
					is_holy_site = ROOT
				}
			}
		}
		modifier = {
			factor = 0
			FROM = { relative_power = { who = ROOT power = 1.5 } }
		}
		modifier = {
			factor = 0.5
			FROM = { realm_levy_diff = { who = ROOT value = 1000 } }
		}
		modifier = {
			factor = 1.5
			ROOT = {
				realm_levy_diff = { who = FROM value = 2500 }
				relative_power = { who = FROM power = 1.5 }
			}
		}
	}
}

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

	can_use = {
		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 = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their resist_viceroy_revocation_cb against [From.GetBestName] (ID: [From.GetID])" 
		ROOT = {
			primary_title = { set_title_flag = viceroy_exemption }
			switch_to_feudal_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		FROM = {
			prestige = -500
			remove_opinion = {
				who = ROOT
				modifier = declared_war
			}
			any_demesne_title = {
				limit = { can_destroy_title_trigger = yes }
				custom_tooltip = { text = TITLE_ENDANGERED_NO_NAME }
				set_title_flag = destroy_title
			}
			any_demesne_title = {
				title_reduce_CA_effect = yes
				title_reduce_TO_effect = yes
			}
			hidden_tooltip = {
				# destroy primary title if it has been reduced too much
				if = {
					limit = { primary_title = { has_title_flag = destroy_title } }
					character_event = { id = Plus.1620 days = 4 }
				}
			}
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 250
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 250 }
		}
	}

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

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -200
			prisoner = FROM
			pf_glory_minus2_effect = yes
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}
	
	on_attacker_leader_death = {
		end_war = invalid
	}

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

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
	}
}
# 01_CK2Plus_cb_types end
# 02_CK2Plus_historical_cbs start
### Historical CB's used for bookmarks

aghlabid_867 = {
	name = CB_NAME_RELIGIOUS
	war_name = WAR_NAME_RELIGIOUS
	sprite = 9
	truce_days = 3650
	is_permanent = yes
	can_ask_to_join_war = no
	is_holy_war = yes
	check_de_jure_tier = DUKE
	
	infamy_modifier = 0

	can_use_title = {
		always = no
	}

	can_use = {
		always = no
	}

	is_valid = {
		always = yes
	}

	on_add = {
		hidden_tooltip = { ROOT = { add_character_modifier = { name = "holy_war_timer" duration = 3650 } } }
	}

	on_success = {
		ROOT = { 
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes } 
			add_character_modifier = { name = "holy_war_timer" duration = 3650 }
			pf_glory_plus2_effect = yes
			pf_tradition_plus2_effect = yes
		}
		FROM = {
			pf_glory_minus2_effect = yes
			pf_tradition_minus2_effect = yes
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_piety = 50
			participation_scaled_prestige = 100
			dynasty_head = {
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_piety = 50 
				participation_scaled_prestige = 100 
				dynasty_head = {
					if = {
						limit = {
							uses_decadence = yes
						}
						participation_scaled_decadence = -10
					}
				}
			}
		}
		d_calabria = {
			holder_scope = { 
				set_defacto_liege = THIS
			}
		}
		c_lecce = {
			holder_scope = { 
				set_defacto_liege = THIS
			}
		}				
		if = {
			limit = {
				ROOT = { is_heretic = no }
			}
			ROOT = {
				religion_authority = {
					modifier = won_holy_war
					years = 20
				}
			}
		}
		if = {
			limit = {
				FROM = { is_heretic = no }
			}
			FROM = { 
				religion_authority = {
					modifier = lost_holy_war
					years = 20
				}
				add_weak_pressed_claim = c_lecce
				add_weak_pressed_claim = c_consenza
				add_weak_pressed_claim = c_reggio
				add_weak_pressed_claim = d_calabria
			}
		}				
	}

	on_success_title = {
		log = "[Root.GetBestName] succeeded with their aghlabid_867 CB for the [This.GetFullName] against [From.GetBestName]"
		hidden_tooltip = {
			if = {
				limit = { 
					has_holder = yes
					holder_scope = { 
						OR = {
							character = FROM
							is_liege_or_above = FROM
						}
					}
				}
				set_global_flag = usurp_duchy
			}
		}
		ROOT = {
			gain_settlements_under_title = {
				title = PREV
				enemy = FROM
			}			
		}
		hidden_tooltip = {
			if = {
				limit = { 
					has_global_flag = usurp_duchy
				}
				destroy_landed_title = yes
				usurp_title = ROOT
				clr_global_flag = usurp_duchy
			}
		}
	}

	on_fail = {
		ROOT = {
			piety = -100
			prestige = -100
			add_character_modifier = { name = "holy_war_timer" duration = 3650 }
		}
	}

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			pf_tradition_plus2_effect = yes
		}
		ROOT = {
			piety = -200
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			pf_glory_minus2_effect = yes
			pf_tradition_minus2_effect = yes
		}		
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
			participation_scaled_piety = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_prestige = 200
				participation_scaled_piety = 100
			}
		}
		if = {
			limit = {
				FROM = { is_heretic = no }
			}
			FROM = { 
				religion_authority = {
					modifier = won_holy_war
					years = 20
				}
			}
		}
		if = {
			limit = {
				ROOT = { is_heretic = no }
			}
			ROOT = {
				religion_authority = {
					modifier = lost_holy_war
					years = 20
				}
				add_character_modifier = { name = "holy_war_timer" duration = 3650 }
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

#This is a duplicate of the CK2+ Invasion CB, with the ability to call allies removed.  Used for historical Invasions like 1066 William/Harald attacking England.
invasion_nc = {
	name = CB_NAME_INVASION_NO_CALLS
	war_name = WAR_NAME_INVASION_NO_CALLS
	sprite = 7
	truce_days = 3650
	hostile_against_others = yes
	can_call_allies = no
	can_ask_to_join_war = no

	can_use_title = {
		always = no
	}

	can_use = {
		always = no
	}

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

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = { add_character_modifier = { name = "holy_truce_timer" duration = 1820 } }
		FROM = {
			pf_glory_minus2_effect = yes
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		ROOT = {
			give_nickname = nick_the_conqueror
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] succeeded with their invasion_nc CB for the [This.GetFullName] against [From.GetBestName]"
		hidden_tooltip = {
			any_de_jure_vassal = {
				any_demesne_title = {
					limit = {
						NOT = { tier = BARON }
						kingdom = { title = PREVPREVPREV }
					}
					add_pressed_claim = ROOT
				}
			}
			FROM = {
				any_demesne_title = {
					limit = {
						kingdom = { title = PREVPREVPREV }
					}
					usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
				}
			}
			ROOT = {
				if = {
					limit = {
						has_dlc = "Legacy of Rome"
						OR = { 
							has_landed_title = e_byzantium
							has_landed_title = e_roman_empire
							AND = {
								independent = yes
								higher_tier_than = COUNT
								OR = {
									culture = roman
									religion = hellenic_pagan
								}
							}
						}
						OR = {
							religion_group = christian
							religion_group = pagan_group
						}
					}
					character_event = { id = LoR.30 days = 12 }
				}
			}
		}
		usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
		hidden_tooltip = {
			if = {
				limit = { 
					ROOT = { ai = yes always = no }
				}
				ROOT = { add_character_modifier = { name = "holy_truce_timer" duration = 3650 }	}
			}
		}
		if = {
			limit = {
				higher_tier_than = BARON
				NOT = { culture = ROOT }
			}
			hidden_tooltip = { conquest_culture = ROOT }
		}
	}

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

	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			pf_glory_minus2_effect = yes
		}
		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

						AND = {
							independent = yes
							higher_tier_than = COUNT
							OR = {
								culture = roman
								religion = hellenic_pagan
							}
						}
					}
					OR = {
						religion_group = christian
						religion_group = pagan_group
					}
				}
				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 = {
		remove_claim = ROOT
	}

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

		end_war = whitepeace
	}

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

magyar_invasion = {
	name = CB_NAME_TRIBAL_INVASION
	war_name = WAR_NAME_TRIBAL_INVASION
	sprite = 8
	truce_days = 3650
	hostile_against_others = yes
	is_permanent = yes
	can_ask_to_join_war = no
	check_de_jure_tier = KING # this scans all de jure kingdoms for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	
	infamy_modifier = 0

	can_use = {
		ROOT = {
			culture = hungarian
			NOT = { any_realm_province = { region = custom_carpathia } }
			has_global_flag = magyar_1066_invasion
			NOT = { has_global_flag = hungary_formed }
		}
	}
	
	can_use_title = {
		title = k_carpathia
	}
	
	is_valid_title = {
		title = k_carpathia
	}

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			prestige = 500
			wealth = 500
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		FROM = {
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}
		hidden_tooltip = {
			ROOT = {
				disband_event_forces = magyar_forces
				occupy_minors_of_occupied_settlements = FROM
				gain_all_occupied_titles = { who = FROM type = invasion }
			}
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] succeeded with their magyar invasion for the [This.GetFullName] against [From.GetBestName]"
		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
					}
				}
			}
			ROOT = {
				if = {
					limit = { has_nickname = no }
					give_nickname = nick_the_great
				}
				set_global_flag = hungary_formed
				if = {
					limit = { is_nomadic = no }
					if = {
						limit = {
							any_realm_lord = { has_landed_title = c_pest }
						}
						c_pest = {
							usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
						}
					}
					if = {
						limit = { has_landed_title = c_pest }
						c_pest = {
							location = {
								culture = hungarian
								religion = ROOT
								ROOT = { capital = PREV }
							}
						}
					}
					# The Hungarian culture in the Ukraine is lost
					hidden_tooltip = {
						any_realm_province = {
							limit = {
								culture = hungarian
								kingdom = {
									NOR = {
										title = k_hungary
										title = k_carpathia
									}
								}
								any_neighbor_province = {
									NOT = { culture = hungarian }
								}
							}
							random_neighbor_province = {
								limit = {
									NOT = { culture = hungarian }
								}
								reverse_culture = PREV
								reverse_religion = PREV
							}
						}
					}
					# Lose control of the Ukraininan lands
					any_realm_province = {
						limit = {
							kingdom = {
								NOR = {
									title = k_hungary
									title = k_carpathia
								}
							}
							owner = {
								# Human vassals get to keep their stuff
								OR = {
									ai = yes
									character = ROOT
								}
							}
						}
						custom_tooltip = {
							text = BREAKS_AWAY
							hidden_tooltip = {
								county = {
									create_character = {
										random_traits = yes
										dynasty = RANDOM
										religion = PREV
										culture = PREV
										female = no
										age = 25
										trait = peasant_leader
									}
									new_character = {
										usurp_title_plus_barony_if_unlanded = { target = PREV type = invasion }
										set_defacto_liege = THIS
									}
									remove_claim = ROOT
								}
							}
						}
					}
					d_pecs = {
						de_jure_liege = k_hungary
					}
					d_esztergom = {
						de_jure_liege = k_hungary
					}	
					d_nyitra = {
						de_jure_liege = k_hungary
					}	
					d_ungvar = {
						de_jure_liege = k_hungary
					}	
					d_pest = {
						de_jure_liege = k_hungary
					}	
					d_transylvania = {
						de_jure_liege = k_hungary
					}	
					d_temes = {
						de_jure_liege = k_hungary
					}
					switch_to_feudal_effect = yes
					hidden_tooltip = {
						k_carpathia = { destroy_landed_title = yes }
						k_hungary = { reset_coa = THIS }
						narrative_event = { id = 62910 }
						any_demesne_title = {
							limit = { tier = baron }
							set_title_flag = refill_levy
						}
						character_event = { id = Plus.352 days = 3 }
					}
				}
			}
		}
	}

	on_fail = {
		ROOT = {
			prestige = -100
			clr_global_flag = magyar_1066_invasion
			set_global_flag = magyar_defeat
			disband_event_forces = magyar_forces
		}
		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 = {
		FROM = {
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			clr_global_flag = magyar_1066_invasion
			set_global_flag = magyar_defeat
			disband_event_forces = magyar_forces
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.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 = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

almoravid_1066 = { # War between the Almoravids and Ghana that forces Ghana to become muslim and a tribute state
	name = CB_NAME_TRIBUTARY
	war_name = WAR_NAME_TRIBUTARY
	sprite = 9
	truce_days = 3650
	is_permanent = yes
	can_ask_to_join_war = no
	is_holy_war = no
	check_de_jure_tier = DUKE
	
	infamy_modifier = 0

	can_use_title = {
		always = no
	}

	can_use = {
		always = no
	}

	is_valid = {
		ROOT = { religion_group = muslim }
		FROM = { NOT = { religion_group = muslim } }
	}
	
	on_success = {
		FROM = {
			save_event_target_as = target_loser
			pf_glory_minus2_effect = yes
			pf_tradition_minus2_effect = yes
			custom_tooltip = {
				text = "REMOVE_PREVIOUS_SUZERAIN"
				hidden_tooltip = {
					any_suzerain = {
						event_target:target_loser = {
							remove_tributary = PREV
						}
					}
				}
			}
		}
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			pf_tradition_plus2_effect = yes
			make_tributary = { who = FROM percentage = 0.4 }
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
			participation_scaled_piety = 100
			dynasty_head = {
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			participation_scaled_prestige = 100
			participation_scaled_piety = 50
			dynasty_head = {
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
			}
		}
		FROM = {
			religion = ROOT
			any_courtier = {
				religion = ROOT
			}
			any_vassal = {
				religion = ROOT
				any_courtier = {
					religion = ROOT
				}
			}
		}
	}
	on_fail = {
		ROOT = {
			piety = -100
			prestige = -100
		}
	}
	on_reverse_demand = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			pf_tradition_plus2_effect = yes
		}
		ROOT = {
			piety = -200
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			pf_glory_minus2_effect = yes
			pf_tradition_minus2_effect = yes
		}		
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
			participation_scaled_piety = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_prestige = 200
				participation_scaled_piety = 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
	}
}

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

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

	infamy_modifier = 0.85

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

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

	on_success = {
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		
		FROM = {
			# existing vassals may declare independence
			if = {
			 	limit = { independent = yes }
			 	hidden_tooltip = { any_vassal = { character_event = { id = Plus.430 days = 3 } } }
			}
		
			# reduce CA & TO
			any_demesne_title = {
				title_reduce_CA_effect = yes
				title_reduce_TO_effect = yes
			}
		}
	}
	
	on_success_title = {
		usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
		
		# ruler stays within the realm
		hidden_tooltip = {
			FROM = { set_defacto_liege = ROOT }
		}
		
		if = {
			limit = {
				higher_tier_than = BARON
				NOT = { culture = ROOT }
			}
			hidden_tooltip = { conquest_culture = ROOT }
		}
	}

	on_fail = {
		ROOT = {
			prestige = -100
			hidden_tooltip = { set_defacto_liege = FROM }
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		
		FROM = {
			remove_opinion = { who = ROOT modifier = declared_war }
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			hidden_tooltip = { set_defacto_liege = FROM }
			imprison = FROM
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}
	
	on_reverse_demand_title = {
		remove_claim = ROOT
		ROOT = {
			custom_tooltip = { text = MAY_NOT_BECOME_PRETENDER_AGAIN }
			hidden_tooltip = {
				reverse_opinion = {
					who = FROM
					modifier = failed_to_claim_my_title
					years = 100
				}
			}
		}
	}
	
	on_attacker_leader_death = {
		hidden_tooltip = {
			any_defender = {
				letter_event = {
					id = 251
				}
			}
		}
		end_war = invalid
	}
	
	on_invalidation = {
		ROOT = { set_defacto_liege = FROM }
	}
	
	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_anarchy_cb = {
	name = CB_NAME_CLAIM
	war_name = WAR_NAME_OTHER_CLAIM
	sprite = 2
	truce_days = 3650
	hostile_against_others = yes
	can_ask_to_join_war = yes
	is_permanent = no

	allowed_to_target_tributaries = yes
	allowed_to_target_suzerains = no
	
	infamy_modifier = 0.5

	press_claim = yes

	can_use = {
		always = no
	}

	can_use_title = {
		always = no
	}

	is_valid = {
		always = yes
	}
	is_valid_title = {
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
		ROOT = { has_claim = PREV }
	}
	
	on_add = {
		FROM = {
			attacked_tributary_cb_effect = yes
		}
		ROOT = {
			set_character_flag = claim_being_pressed
		}
	}	

	on_success = {
		hidden_tooltip = {
			ROOT = { add_character_modifier = { name = new_land_timer duration = 1 hidden = yes } }
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			ROOT = {
				opinion = {
					modifier = pressed_my_claim 
					who = PREV
					multiplier = 2
				}
				hidden_tooltip = {
					clr_character_flag = claim_being_pressed
				}
			}
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		FROM = {
			prestige = -200
			pf_glory_minus2_effect = yes
		}
	}
	
	on_fail = {
		ROOT = {
			hidden_tooltip = {
				clr_character_flag = claim_being_pressed
			}
		}
	}
	
	on_invalidation = {
		ROOT = {
			hidden_tooltip = {
				clr_character_flag = claim_being_pressed
			}
		}
		any_attacker = {
			limit = { NOT = { culture = scottish } }
			set_defacto_liege = k_england
		}
	}

	on_success_title = {
		log = "[FromFrom.GetBestName] (ID: [FromFrom.GetID]) succeeded with their war for [Root.GetBestName]'s (ID: [Root.GetID]) claim on the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		#decentralize target title
		title_reduce_CA_effect = yes
		title_reduce_TO_effect = yes
		pf_liege_change_under_title_begin_effect = yes
		hidden_tooltip = {
			ROOT = {
				if = {
					limit = {
						has_nickname = no
						lower_tier_than = PREV
					}
					random = {
						chance = 50
						give_nickname = nick_the_usurper
					}
				}
			}
			any_attacker = {
				limit = { NOT = { culture = scottish } }
				set_defacto_liege = k_england
			}
			# Anti-Popes take all the gold of the Vatican
			if = {
				limit = {
					OR = {
						title = k_papal_state
						title = d_fraticelli
					}
				}
				FROM = {
					transfer_scaled_wealth = {
						to = ROOT
						value = all
					}
				}
			}
		}
		if = {
			#antipopes
			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 
							}
						}
					}
				}
			}

			# check if we qualify for staying in the realm & inform player of result with a tooltip
			if = {
				limit = {
					OR = {
						de_jure_liege_or_above = FROMFROM						
						ROOT = {
							any_liege = {
								character = FROMFROM
								primary_title = { higher_tier_than = PREVPREVPREV }
							}
						}						
					}
					OR = {
						de_jure_liege_or_above = FROMFROM
						ROOT = { primary_title = { higher_tier_than = PREVPREV } }
						ROOT = { primary_title = { tier = PREVPREV } }
						ROOT = {
							any_dynasty_member = {
								character = FROMFROM
							}
						}
					}
				}
				# mark as staying in the realm
				ROOT = { set_character_flag = stay_in_realm }
				custom_tooltip = { text = CUSTOMTOOLTIP1 }
			}
			if = {
				limit = {
					OR = {
						NOT = {
							OR = {
								de_jure_liege_or_above = FROMFROM
								ROOT = {
									any_liege = {
										character = FROMFROM
										primary_title = { higher_tier_than = PREVPREVPREV }
									}
								}
							}
						}
						NOR = {
							de_jure_liege_or_above = FROMFROM
							ROOT = { primary_title = { higher_tier_than = PREVPREV } }
							ROOT = { primary_title = { tier = PREVPREV } }
							ROOT = {
								any_dynasty_member = {
									character = FROMFROM
								}
							}
						}
					}
				}
				custom_tooltip = { text = CUSTOMTOOLTIP2 }
			}

			#determine who is the liege of loser in the war
			if = {
				limit = {
					ROOT = {
						NOT = { has_character_flag = stay_in_realm }
					}
					FROM = { independent = no } 
				}
				FROM = {
					liege = { set_character_flag = from_liege }
				}
				set_global_flag = from_has_liege
			}

			empty_province_effect = yes
			usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }

			# try and vassalise
			hidden_tooltip = {
				# claimant stays in realm
				if = {
					limit = {
						ROOT = { has_character_flag = stay_in_realm }
						NOT = { 
						 	ROOT = { is_liege_or_above = FROMFROM }
						}
					}
					ROOT = { set_defacto_liege = FROMFROM }
				}
				#claimant leaves realm - if loser was vassal, vassalize to their liege. If loser was independent, claimant goes independent.
				if = {
					limit = {
						has_global_flag = from_has_liege
						ROOT = {
							NOT = { has_character_flag = stay_in_realm }
						}
					}
					any_playable_ruler = {
						limit = { has_character_flag = from_liege }
						ROOT = { set_defacto_liege = PREV }
						clr_character_flag = from_liege
					}
				}
				if = {
					limit = {
						NOT = { has_global_flag = from_has_liege } 
						ROOT = {
							NOT = { has_character_flag = stay_in_realm }
						}
					}
					ROOT = { set_defacto_liege = ROOT }
				}
				clr_global_flag = from_has_liege
				ROOT = { clr_character_flag = stay_in_realm }
			}
		}
	
		pf_liege_change_under_title_end_effect = yes
		
		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 = {
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		prestige = -200
		transfer_scaled_wealth = {
			to = FROM
			value = 4.0
		}
		pf_glory_minus2_effect = yes
		ROOT = {
			prestige = -200
			imprison = FROM
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		any_attacker = {
			limit = { 
				NOT = { culture = scottish } 
				NOT = { character = ROOT }
			}
			hidden_tooltip = {
				set_defacto_liege = FROM
				imprison = FROM
			}
		}
	}

	on_reverse_demand_title = {
		remove_claim = ROOT
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = { 
		factor = 0
	}
}
# 02_CK2Plus_historical_cbs end
# cb_faction_overthrow_ruler start
## normal scopes(including posttitle scopes) ##
# ROOT = actor
# FROM = recipient
# <no scope change> = thirdparty character
## title scopes ##
# ROOT = actor
# FROM = recipient
# <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)
#

cb_faction_overthrow_ruler = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_FACTION_DEPOSE
	sprite = 12
	truce_days = 3650
	
	major_revolt = yes
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no
	
	infamy_modifier = 0
	
	can_use = {
		ROOT = {
			vassal_of = FROM
			has_opinion_modifier = { who = FROM modifier = revolting_against }
		}
	}

	is_valid = {
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = {
					FROM = { 
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
			has_opinion_modifier = { who = FROM modifier = revolting_against }
		}
		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
				}
			}
		}
		OR = {
			# Never invalidate the revolt due to demands being impossible to fulfill if still the original liege
			ROOT = { has_character_flag = original_war_liege }			
			# Succession law wars are invalid if the non-original liege doesn't hold the succession title
			NOT = { FROM = { has_character_flag = faction_succession_war } }
			AND = {
				ROOT = { has_character_flag = demands_seniority }
				FROM = {
					any_demesne_title = {
						higher_tier_than = COUNT
						has_title_flag = change_seniority
					}
				}
			}
			AND = {
				ROOT = { has_character_flag = demands_primogeniture }
				FROM = {
					any_demesne_title = {
						higher_tier_than = COUNT
						has_title_flag = change_primogeniture
					}
				}
			}
			AND = {
				ROOT = { has_character_flag = demands_gavelkind }
				FROM = {
					any_demesne_title = {
						higher_tier_than = COUNT
						has_title_flag = change_gavelkind
					}
				}
			}
			AND = {
				ROOT = { has_character_flag = demands_feudal_elective }
				FROM = {
					any_demesne_title = {
						higher_tier_than = COUNT
						has_title_flag = change_feudal_elective
					}
				}
			}
		}
	}

	on_add = {
		log = "CB(cb_faction_overthrow_ruler): START: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
		FROM = { pf_log_state_effect = yes }
		ROOT = { pf_log_state_effect = yes }
		#emf_cb_dynlevy_begin_effect = yes
	}
	
	on_success = {
		log = "CB(cb_faction_overthrow_ruler): VICTORY: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
		
		# the defender loses 10 Imperial Decadence ("out with the old, in with the new")
		if = {
			limit = { FROM = { uses_imperial_decadence_trigger = yes } }
			FROM = {
				custom_tooltip = { text = IMPERIAL_DECADENCE_MINUS_10 }
				hidden_tooltip = {
					primary_title = {
						change_variable = { which = "imperial_decadence" value = -10 }
						set_title_flag = skip_decadence_gain
					}
				}
			}
		}

		# add victory modifier for the 'Win a War' ambition
		any_attacker = { add_character_modifier = { name = victory_timer duration = 1 hidden = yes } }
		
		# apply immediate effects to the revolters
		hidden_tooltip = {
			any_attacker = {
				limit = { has_opinion_modifier = { who = FROM modifier = revolting_against } }
				
				# the revolters remember who they overthrew
				opinion = { modifier = overthrown_ruler who = FROM }
				reverse_opinion = { modifier = overthrew_me who = FROM }
				
				# and also are grateful to each other for their victory
				FROM = {
					any_opinion_modifier_target = {
						limit = {
							reverse_has_opinion_modifier = {
								who = PREV
								modifier = revolting_against_me
							}
							NOT = { character = PREVPREV }
						}
						reverse_opinion = {
							who = PREVPREV
							modifier = opinion_very_grateful
							years = 10
						}
					}
				}
			}
		}
		
		# apply revolt effects to the defender
		FROM = {
			prestige = -500
			
			# existing vassals may declare independence
			if = {
			 	limit = { independent = yes }
			 	hidden_tooltip = { any_vassal = { character_event = { id = Plus.430 days = 3 } } }
			}

			# check whether title will be destroyed (CK2Plus only)
			any_demesne_title = {
			 	limit = { can_destroy_title_trigger = yes }
			 	custom_tooltip = { text = TITLE_ENDANGERED_NO_NAME }
			 	log = "[This.GetFullName] should be destroyed as a result of the revolt"
			 	set_title_flag = destroy_title
			}
		
			# always lower CA/TO, even if it wasn't a revolt demand
			pf_lower_CA_effect = yes # lower Crown Authority
			pf_lower_TO_effect = yes # lower Tribal Organization		
		
			# enforce any rebel demands that prompted the civil war
			if = {
				limit = { ROOT = { has_character_flag = demands_revert_succession } }
				primary_title = { pf_revert_succession_law_effect = yes }
			}
			if = {
				limit = { ROOT = { has_character_flag = demands_lower_feudal_obligations } }
				pf_lower_feudal_obligations_effect = yes
			}
			if = {
				limit = { ROOT = { has_character_flag = demands_lower_tribal_obligations } }
				pf_lower_tribal_obligations_effect = yes
			}
			if = {
				limit = { ROOT = { has_character_flag = demands_lower_city_obligations } }
				pf_lower_city_obligations_effect = yes
			}
			if = {
				limit = { ROOT = { has_character_flag = demands_lower_temple_obligations } }
				pf_lower_temple_obligations_effect = yes
			}
			if = {
				limit = { ROOT = { has_character_flag = demands_council_life_terms } }
				primary_title = { add_law_w_cooldown = council_privileges_1 }
			}
			if = {
				limit = { ROOT = { has_character_flag = demands_seniority } }
				random_demesne_title = {
					limit = { has_title_flag = change_seniority }
					succession_w_cooldown = seniority
					clr_title_flag = change_seniority
				}
				recalc_succession = yes
			}
			if = {
				limit = { ROOT = { has_character_flag = demands_primogeniture } }
				random_demesne_title = {
					limit = { has_title_flag = change_primogeniture }
					succession_w_cooldown = primogeniture
					clr_title_flag = change_primogeniture
				}
				recalc_succession = yes
			}
			if = {
				limit = { ROOT = { has_character_flag = demands_gavelkind } }
				random_demesne_title = {
					limit = { has_title_flag = change_gavelkind }
					succession_w_cooldown = gavelkind
					clr_title_flag = change_gavelkind
				}
				recalc_succession = yes
			}
			if = {
				limit = { ROOT = { has_character_flag = demands_feudal_elective } }
				random_demesne_title = {
					limit = { has_title_flag = change_feudal_elective }
					succession_w_cooldown = feudal_elective
					clr_title_flag = change_feudal_elective
				}
				recalc_succession = yes
			}
			
			# Prevent on_new_holder / on_unlanded edge case handlers from tearing-down any civil war state while the CB
			# completes due to, e.g., liege changes or gained titles.
			set_global_flag = pf_teardown_lock
		
			# surrender any non-capital counties to rebels with claims
			save_event_target_as = current_holder
			any_demesne_title = {
				limit = {
					tier = COUNT
					location = { is_capital = no }
					can_be_given_away = yes
				}
				# give the county to de jure lieges if no rebels with claims exist
				if = {
					limit = {
						event_target:current_holder = {
							any_opinion_modifier_target = {
								pf_part_of_this_revolt_trigger = yes
								any_demesne_title = {
									tier = DUKE
									de_jure_vassal_or_below = PREVPREVPREV
								}
							}
							NOT = {
								any_opinion_modifier_target = {
									pf_part_of_this_revolt_trigger = yes
									has_claim = PREVPREV
								}
							}
						}
					}
					event_target:current_holder = {
						random_opinion_modifier_target = {
							limit = {
								pf_part_of_this_revolt_trigger = yes
								any_demesne_title = {
									tier = DUKE
									de_jure_vassal_or_below = PREVPREVPREV
								}
							}
							usurp_title_plus_barony_if_unlanded = {
								target = PREVPREV
								type = faction_demand
							}
						}
					}
				}
				# give the county to rebels with a weak claim if no rebels with strong claims exist
				if = {
					limit = {
						event_target:current_holder = {
							any_opinion_modifier_target = {
								pf_part_of_this_revolt_trigger = yes
								has_weak_claim = PREVPREV
								can_inherit_trigger = yes
							}
							NOT = {
								any_opinion_modifier_target = {
									pf_part_of_this_revolt_trigger = yes
									has_strong_claim = PREVPREV
									can_inherit_trigger = yes
								}
							}
						}
					}
					event_target:current_holder = {
						random_opinion_modifier_target = {
							limit = {
								pf_part_of_this_revolt_trigger = yes
								has_weak_claim = PREVPREV
								can_inherit_trigger = yes
							}
							usurp_title_plus_barony_if_unlanded = {
								target = PREVPREV
								type = faction_demand
							}
						}
					}
				}
				# give the county to rebels with a strong claim
				if = {
					limit = {
						event_target:current_holder = {
							any_opinion_modifier_target = {
								pf_part_of_this_revolt_trigger = yes
								has_strong_claim = PREVPREV
								can_inherit_trigger = yes
							}
						}
					}
					event_target:current_holder = {
						random_opinion_modifier_target = {
							limit = {
								pf_part_of_this_revolt_trigger = yes
								has_strong_claim = PREVPREV
								can_inherit_trigger = yes
							}
							usurp_title_plus_barony_if_unlanded = {
								target = PREVPREV
								type = faction_demand
							}
						}
					}
				}
			}
			
			# transfer vassals to their de jure lieges, giving preference to the
			# rebels with the lowest-tier held titles that are de jure liege to
			# each vassal (prefer most specific de jure liege) ...

			# king-tier de jure liege title vassal transfer to rebels
			any_vassal = {
				limit = {
					pf_part_of_this_revolt_trigger = no
					capital_holding = {
						event_target:current_holder = {
							any_opinion_modifier_target = {
								pf_part_of_this_revolt_trigger = yes
								higher_real_tier_than = PREVPREVPREV
								any_demesne_title = {
									tier = KING
									de_jure_vassal_or_below = PREVPREVPREV
								}
							}
							NOT = {
								any_opinion_modifier_target = {
									pf_part_of_this_revolt_trigger = yes
									higher_real_tier_than = PREVPREVPREV
									any_demesne_title = {
										lower_tier_than = KING
										de_jure_vassal_or_below = PREVPREVPREV
									}
								}
							}
						}
					}
				}
				capital_holding = {
					event_target:current_holder = {
						random_opinion_modifier_target = {
							limit = {
								pf_part_of_this_revolt_trigger = yes
								higher_real_tier_than = PREVPREVPREV
								any_demesne_title = {
									tier = KING
									de_jure_vassal_or_below = PREVPREVPREV
								}
							}
							set_defacto_vassal = PREVPREVPREV
							#emf_dynlevy_update_effect = yes
						}
					}
				}
			}
			# duke-tier de jure liege title vassal transfer to rebels
			any_vassal = {
				limit = {
					pf_part_of_this_revolt_trigger = no
					capital_holding = {
						event_target:current_holder = {
							any_opinion_modifier_target = {
								pf_part_of_this_revolt_trigger = yes
								higher_real_tier_than = PREVPREVPREV
								any_demesne_title = {
									tier = DUKE
									de_jure_vassal_or_below = PREVPREVPREV
								}
							}
							NOT = {
								any_opinion_modifier_target = {
									pf_part_of_this_revolt_trigger = yes
									higher_real_tier_than = PREVPREVPREV
									any_demesne_title = {
										lower_tier_than = DUKE
										de_jure_vassal_or_below = PREVPREVPREV
									}
								}
							}
						}
					}
				}
				capital_holding = {
					event_target:current_holder = {
						random_opinion_modifier_target = {
							limit = {
								pf_part_of_this_revolt_trigger = yes
								higher_real_tier_than = PREVPREVPREV
								any_demesne_title = {
									tier = DUKE
									de_jure_vassal_or_below = PREVPREVPREV
								}
							}
							set_defacto_vassal = PREVPREVPREV # any_vassal
							#emf_dynlevy_update_effect = yes
						}
					}
				}
			}
			# count-tier de jure liege title vassal transfer to rebels
			any_vassal = {
				limit = {
					pf_part_of_this_revolt_trigger = no
					capital_holding = {
						event_target:current_holder = {
							any_opinion_modifier_target = {
								pf_part_of_this_revolt_trigger = yes
								higher_real_tier_than = PREVPREVPREV
								any_demesne_title = {
									tier = COUNT
									de_jure_vassal_or_below = PREVPREVPREV
								}
							}
						}
					}
				}
				capital_holding = {
					event_target:current_holder = {
						random_opinion_modifier_target = {
							limit = {
								pf_part_of_this_revolt_trigger = yes
								higher_real_tier_than = PREVPREVPREV
								any_demesne_title = {
									tier = COUNT
									de_jure_vassal_or_below = PREVPREVPREV
								}
							}
							set_defacto_vassal = PREVPREVPREV
						}
					}
				}
			}
			
			# save event targets for primary title and current liege
			primary_title = { save_event_target_as = former_primary_title }
			if = {
				limit = { independent = no }
				liege = { save_event_target_as = current_liege }
			}
			
			# check whether defender should have titles usurped
			if = {
				limit = {
					# this is not a succession law revolt
					NOT = { has_character_flag = faction_succession_war }
					# is not a merchant republic
					is_merchant_republic = no
					OR = {
						# or the current ruler is not an adult
						is_adult = no
						# or this is the original liege and the current heir is not an adult
						AND = {
							ROOT = { has_character_flag = original_war_liege }
							current_heir = { is_adult = no }
						}
						# or a rebel has a claim on the primary title or is a pretender/heir
						any_opinion_modifier_target = {
							pf_part_of_this_revolt_trigger = yes
							OR = {
								has_claim = event_target:former_primary_title
								any_pretender_title = {
									title = event_target:former_primary_title
								}
								any_heir_title = {
									title = event_target:former_primary_title
								}
							}
						}
					}
				}
				# grant non-primary same-tier titles to rebels with claims
				any_demesne_title = {
					limit = {
						can_be_given_away = yes
						is_primary_holder_title_tier = yes
						is_primary_holder_title = no
					}
					# give the title to rebel with a weak claim if none with a strong claim exists
					if = {
						limit = {
							event_target:current_holder = {
								any_opinion_modifier_target = {
									pf_part_of_this_revolt_trigger = yes
									has_weak_claim = PREVPREV
									can_inherit_trigger = yes
								}
								NOT = {
									any_opinion_modifier_target = {
										pf_part_of_this_revolt_trigger = yes
										OR = {
											has_strong_claim = PREVPREV
											any_pretender_title = {
												title = PREVPREVPREV
											}
											any_heir_title = {
												title = PREVPREVPREV
											}
										}
										can_inherit_trigger = yes
									}
								}
							}
						}
						event_target:current_holder = {
							random_opinion_modifier_target = {
								limit = {
									pf_part_of_this_revolt_trigger = yes
									has_weak_claim = PREVPREV
									can_inherit_trigger = yes
								}
								usurp_title_plus_barony_if_unlanded = {
									target = PREVPREV
									type = faction_demand
								}
								#emf_dynlevy_update_effect = yes
							}
							if = {
								limit = { event_target:current_liege = { always = yes } }
								hidden_tooltip = {
									set_defacto_liege = event_target:current_liege
								}
							}
						}
					}
					# give the title to rebel with a strong claim or is a pretender/heir
					if = {
						limit = {
							event_target:current_holder = {
								any_opinion_modifier_target = {
									pf_part_of_this_revolt_trigger = yes
									OR = {
										has_strong_claim = PREVPREV
										any_pretender_title = {
											title = PREVPREVPREV
										}
										any_heir_title = {
											title = PREVPREVPREV
										}
									}
									can_inherit_trigger = yes
								}
							}
						}
						event_target:current_holder = {
							random_opinion_modifier_target = {
								limit = {
									pf_part_of_this_revolt_trigger = yes
									OR = {
										has_strong_claim = PREVPREV
										any_pretender_title = {
											title = PREVPREVPREV
										}
										any_heir_title = {
											title = PREVPREVPREV
										}
									}
									can_inherit_trigger = yes
								}
								usurp_title_plus_barony_if_unlanded = {
									target = PREVPREV
									type = faction_demand
								}
								#emf_dynlevy_update_effect = yes
							}
							if = {
								limit = { event_target:current_liege = { always = yes } }
								hidden_tooltip = {
									set_defacto_liege = event_target:current_liege
								}
							}
						}
					}
				}

				# just abdicate if there is no rebel with a claim or a pretender/heir to primary title
				if = {
					limit = {
						event_target:current_holder = {
							has_landed_title = event_target:former_primary_title
							NOT = {
								any_opinion_modifier_target = {
									pf_part_of_this_revolt_trigger = yes
									OR = {
										has_claim = event_target:former_primary_title
										any_pretender_title = { title = event_target:former_primary_title }
										any_heir_title = { title = event_target:former_primary_title }
									}
									can_inherit_trigger = yes
								}
							}
						}
					}
					hidden_tooltip = { character_event = { id = Plus.306 } } #pass on tyrant/dishonorable traits
					abdicate = yes
				}
				
				# give primary title to rebel with weak claim if none with a strong claim or pretender/heir exists
				if = {
					limit = {
						event_target:current_holder = {
							has_landed_title = event_target:former_primary_title
							any_opinion_modifier_target = {
								pf_part_of_this_revolt_trigger = yes
								has_weak_claim = event_target:former_primary_title
								can_inherit_trigger = yes
							}
							NOT = {
								any_opinion_modifier_target = {
									pf_part_of_this_revolt_trigger = yes
									OR = {
										has_strong_claim = event_target:former_primary_title
										any_pretender_title = { title = event_target:former_primary_title }
										any_heir_title = { title = event_target:former_primary_title }
									}
									can_inherit_trigger = yes
								}
							}
						}
					}
					event_target:current_holder = {
						random_opinion_modifier_target = {
							limit = {
								pf_part_of_this_revolt_trigger = yes
								has_weak_claim = event_target:former_primary_title
								can_inherit_trigger = yes
							}
							event_target:former_primary_title = {
								usurp_title_plus_barony_if_unlanded = {
									target = PREV
									type = faction_demand
								}
							}
							if = {
								limit = { event_target:current_liege = { always = yes } }
								hidden_tooltip = {
									set_defacto_liege = event_target:current_liege
								}
							}
						}
					}
				}

				# give primary title to rebel with a strong claim if no rebel pretender/heir exists
				if = {
					limit = {
						event_target:current_holder = {
							has_landed_title = event_target:former_primary_title
							any_opinion_modifier_target = {
								pf_part_of_this_revolt_trigger = yes
								has_strong_claim = event_target:former_primary_title
								can_inherit_trigger = yes
							}
							NOT = {
								any_opinion_modifier_target = {
									pf_part_of_this_revolt_trigger = yes
									OR = {
										any_pretender_title = { title = event_target:former_primary_title }
										any_heir_title = { title = event_target:former_primary_title }
									}
									can_inherit_trigger = yes
								}
							}
						}
					}
					event_target:current_holder = {
						random_opinion_modifier_target = {
							limit = {
								pf_part_of_this_revolt_trigger = yes
								has_strong_claim = event_target:former_primary_title
								can_inherit_trigger = yes
							}
							event_target:former_primary_title = {
								usurp_title_plus_barony_if_unlanded = {
									target = PREV
									type = faction_demand
								}
							}
							if = {
								limit = { event_target:current_liege = { always = yes } }
								hidden_tooltip = {
									set_defacto_liege = event_target:current_liege
								}
							}
						}
					}
				}

				# give primary title to rebel who is a pretender/heir
				if = {
					limit = {
						event_target:current_holder = {
							has_landed_title = event_target:former_primary_title
							any_opinion_modifier_target = {
								pf_part_of_this_revolt_trigger = yes
								OR = {
									any_pretender_title = { title = event_target:former_primary_title }
									any_heir_title = { title = event_target:former_primary_title }
								}
								can_inherit_trigger = yes
							}
						}
					}
					event_target:current_holder = {
						random_opinion_modifier_target = {
							limit = {
								pf_part_of_this_revolt_trigger = yes
								OR = {
									any_pretender_title = { title = event_target:former_primary_title }
									any_heir_title = { title = event_target:former_primary_title }
								}
								can_inherit_trigger = yes
							}
							event_target:former_primary_title = {
								usurp_title_plus_barony_if_unlanded = {
									target = PREV
									type = faction_demand
								}
							}
							if = {
								limit = { event_target:current_liege = { always = yes } }
								hidden_tooltip = {
									set_defacto_liege = event_target:current_liege
								}
							}
						}
					}
				}
			}
			
			# check whether defender should simply abdicate
			if = {
				limit = {
					event_target:current_holder = { has_landed_title = event_target:former_primary_title }
					NAND = {
						# this is not a succession law revolt
						NOT = { has_character_flag = faction_succession_war }
						# is not a merchant republic
						is_merchant_republic = no
						OR = {
							# or the current ruler is not an adult
							is_adult = no
							# or this is the original liege and the current heir is not an adult
							AND = {
								ROOT = { has_character_flag = original_war_liege }
								current_heir = { is_adult = no }
							}
							# or a rebel has a claim on the primary title or is a pretender/heir
							any_opinion_modifier_target = {
								pf_part_of_this_revolt_trigger = yes
								OR = {
									has_claim = event_target:former_primary_title
									any_pretender_title = {
										title = event_target:former_primary_title
									}
									any_heir_title = {
										title = event_target:former_primary_title
									}
								}
								can_inherit_trigger = yes
							}
						}
					}
				}
				hidden_tooltip = { character_event = { id = Plus.306 } } #pass on tyrant/dishonorable traits
				abdicate = yes
			}
		}
		
		clr_global_flag = pf_teardown_lock
					
		# figure out who are the supporters, losers, and neutrals
		if = {
			# primary title has changed hands
			limit = {
				event_target:former_primary_title = {
					holder_scope = {
						NOT = { character = FROM }
					}
				}
			}
			hidden_tooltip = {
				event_target:former_primary_title = {
					holder_scope = {
						# clear previous civil war opinions
						pf_civil_war_clear_old_opinions_effect = yes
						
						# mark rebels as supporters
						event_target:current_holder = {
							any_opinion_modifier_target = {
								limit = {
									pf_part_of_this_revolt_trigger = yes
									NOT = { character = PREVPREV }
								}
								reverse_opinion = {
									who = PREVPREV
									modifier = opinion_civil_war_supporter
									months = 60
								}
							}
						}
						
						# mark former liege and loyalists as losers
						any_vassal = {
							limit = {
								has_opinion_modifier = {
									who = event_target:current_holder
									modifier = pf_loyal_toward
								}
							}
							reverse_opinion = {
								who = LIEGE
								modifier = opinion_civil_war_loser
								months = 60
							}
						}
						event_target:current_holder = {
							reverse_opinion = {
								who = PREV
								modifier = opinion_civil_war_loser
								months = 60
							}
						}
						
						# remember those who chose to be neutral
						any_vassal = {
							limit = {
								has_opinion_modifier = {
									who = event_target:current_holder
									modifier = pf_neutral_toward
								}
							}
							reverse_opinion = {
								who = LIEGE
								modifier = opinion_civil_war_neutral
								months = 60
							}
						}
					}
				}
			}
		}

		# check whether the primary title should be destroyed due to CA loss
		if = {
			limit = { event_target:former_primary_title = { has_title_flag = destroy_title } }
			hidden_tooltip = {
				event_target:former_primary_title = {
					holder_scope = {
						character_event = { id = Plus.1620 days = 4 } #destroy title
					}
				}
			}
		}

		# clean up remaining civil war state
		pf_civil_war_cleanup_effect = yes

		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 250
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 250 }
		}
		
		# post-civil war event to reward allies (if title has changed hands)
		if = {
			limit = {
				event_target:former_primary_title = {
					holder_scope = {
						NOT = { character = FROM }
					}
				}
			}
			event_target:former_primary_title = {
				holder_scope = {
					#emf_dynlevy_update_effect = yes
					hidden_tooltip = { character_event = { id = PlusFaction.450 } }
				}
			}
		}
		
		# change faction moods
		FROM = { hidden_tooltip = { character_event = { id = PlusFaction.200 days = 1 } } }
		ROOT = { hidden_tooltip = { character_event = { id = PlusFaction.200 days = 1 } } }
		
		#emf_cb_dynlevy_end_effect = yes
	}

	on_fail = {
		log = "CB(cb_faction_overthrow_ruler): WHITEPEACE: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"

		#emf_cb_dynlevy_end_effect = yes
		
		# clean up remaining civil war state
		pf_civil_war_cleanup_effect = yes
		
		ROOT = {
			prestige = -100
		}
		hidden_tooltip = {
			FROM = {
				remove_opinion = {
					who = ROOT
					modifier = declared_war
				}
			}
		}
	}

	on_reverse_demand = {
		log = "CB(cb_faction_overthrow_ruler): DEFEAT: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"

		#emf_cb_obj_win_war_reverse_effect = yes
		#emf_cb_dynlevy_end_effect = yes
		
		FROM = {
			# the defender loses 1 Imperial Decadence
			imperial_decadence_minus_1_effect = yes
			# add victory modifier for the 'Win a War' ambition
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_civil_war_clear_old_opinions_effect = yes
		}
		
		# apply immediate effects to the revolters
		any_attacker = {
			limit = { has_opinion_modifier = { who = FROM modifier = revolting_against } }
			imprison = FROM
			hidden_tooltip = {
				reverse_opinion = { who = FROM modifier = opinion_civil_war_loser months = 60 }
				opinion = { who = FROM modifier = opinion_coerced_into_leaving_faction months = 60 }
			}
		}

		# mark loyalists and neutrals for the reward event
		hidden_tooltip = {
			FROM = {
				any_vassal = {
					limit = { reverse_has_opinion_modifier = { who = PREV modifier = pf_loyal_to_me } }
					reverse_opinion = { who = PREV modifier = opinion_civil_war_supporter months = 60 }
				}
				any_vassal = {
					limit = { reverse_has_opinion_modifier = { who = PREV modifier = pf_neutral_to_me } }
					reverse_opinion = { who = PREV modifier = opinion_civil_war_neutral months = 60 }
				}
			}
		}
		
		# clean up remaining civil war state
		pf_civil_war_cleanup_effect = yes
		
		ROOT = {
			prestige = -250
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		
		# post-civil war event to reward allies
		FROM = {
			hidden_tooltip = { character_event = { id = PlusFaction.450 } }
		}
		
		# change faction moods
		hidden_tooltip = {
			FROM = { character_event = { id = PlusFaction.200 days = 1 } }
			ROOT = { character_event = { id = PlusFaction.200 days = 1 } }
		}
	}
	
	on_invalidation = {
		log = "CB(cb_faction_overthrow_ruler): INVALIDATED: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"

		#emf_cb_dynlevy_end_effect = yes
		
		# clean up remaining civil war state
		pf_civil_war_cleanup_effect = yes
	}
	
	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
	}
}
# cb_faction_overthrow_ruler end
# emf_rapid_conquest start
#County conquest
minor_conquest = {
	name = CB_NAME_MINOR_CONQUEST
	war_name = WAR_NAME_MINOR_CONQUEST
	sprite = 8
	truce_days = 1825
	hostile_against_others = yes
	is_permanent = yes
	can_ask_to_join_war = no
	can_call_allies = no
	#check_de_jure_tier = COUNT # this scans all dejure duchies for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no
	
	can_use_gui = {
		ROOT = {
			is_patrician = no
		}
	}
	
	can_use = {
		has_global_flag = emf_rapid_conquest
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }		
		ROOT = {
			primary_title = { is_primary_type_title = no } # EMF: k_orthodox and k_papal_state, e.g., can't use this
			NOT = { num_of_count_titles = 8 }
			OR = {
				NOT = { num_of_count_titles = 5 }
				AND = {
					capital_scope = {
						any_neighbor_province = {
							owner = {
								OR = {
									character = FROM
									is_liege_or_above = FROM
								}
							}
						}
					}
					lower_tier_than = duke
				}
			}
			independent = yes
			OR = {
				NOT = { any_vassal = { tier = count } }
				capital_scope = {
					any_neighbor_province = {
						owner = {
							OR = {
								character = FROM
								is_liege_or_above = FROM
							}
						}
					}
				}
			}
		}
		FROM = {
			NOT = { num_of_count_titles = 3 }
			any_realm_province = {
				any_neighbor_province = {
					owner = {
						OR = {
							character = ROOT
							is_liege_or_above = ROOT
						}
					}
				}
			}
			NOT = {
				any_demesne_title = {
					temporary = yes
				}
			}
		}
	}

	on_add = {
		log = "CB(minor_conquest): START: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
	}
	
	on_success = {
		log = "CB(minor_conquest): VICTORY: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"

		emf_cb_victory_effect = yes
		ROOT = {
			prestige = 100
		}
		FROM = {
			prestige = -100
			any_demesne_title = {
				limit = { tier = count }
				emf_cb_nomadic_province_effect = yes
				usurp_title = { target = ROOT type = invasion }
			}
			any_realm_lord = {
				any_demesne_title = {
					limit = { tier = count }
					emf_cb_nomadic_province_effect = yes
					usurp_title = { target = ROOT type = invasion }
				}
			}
		}
	}
	
	on_fail = {
		log = "CB(minor_conquest): WHITEPEACE: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"

		ROOT = {
			prestige = -25
		}
	}
	
	on_reverse_demand = {
		log = "CB(minor_conquest): DEFEAT: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"

		emf_cb_defeat_effect = yes
		ROOT = {
			prestige = -50
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
		}
		FROM = {
			prestige = 50
		}
	}

	on_invalidation = {
		log = "CB(minor_conquest): INVALIDATED: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
	}
	
	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
	}
}

#Subjugation
subjugation = {
	name = CB_NAME_SUBJUGATION
	war_name = WAR_NAME_SUBJUGATION
	sprite = 8
	truce_days = 1825
	hostile_against_others = yes
	is_permanent = yes
	can_ask_to_join_war = no
	can_call_allies = no
	#check_de_jure_tier = COUNT # this scans all dejure duchies for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no
	
	can_use_gui = {
		ROOT = {
			is_patrician = no
		}
	}
	
	can_use = {
		has_global_flag = emf_rapid_conquest
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		ROOT = {
			independent = yes
			higher_tier_than = count
			primary_title = { is_primary_type_title = no } # EMF: e.g., k_orthodox can't use this
		}
		FROM = {
			OR = {
				tier = count
				NOT = { realm_size = 10 }
			}
			any_realm_province = {
				any_neighbor_province = {
					owner = {
						OR = {
							character = ROOT
							is_liege_or_above = ROOT
						}
					}
				}
			}
			NOR = { # EMF: Can't subjugate higher-tier landless characters (k_orthodox, etc.) or revolters
				primary_title = { is_primary_type_title = yes }
				any_demesne_title = {
					temporary = yes
				}
				is_nomadic = yes
			}
		}
	}
	
	on_add = {
		log = "CB(subjugation): START: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
	}

	on_success = {
		log = "CB(subjugation): VICTORY: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
		emf_cb_victory_effect = yes
		ROOT = {
			prestige = 100
		}
		FROM = {
			prestige = -100
			any_demesne_title = {
				limit = { NOT = { lower_tier_than = ROOT } }
				usurp_title = { target = ROOT type = invasion }
			}
			set_defacto_liege = ROOT
		}
	}
	
	on_fail = {
		log = "CB(subjugation): WHITEPEACE: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
		ROOT = {
			prestige = -25
		}
	}
	
	on_reverse_demand = {
		log = "CB(subjugation): DEFEAT: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
		emf_cb_defeat_effect = yes
		ROOT = {
			prestige = -50
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
		}
		FROM = {
			prestige = 50
		}
	}

	on_invalidation = {
		log = "CB(subjugation): INVALIDATED: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
	}

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

#Subjugation
subjugation_duke = {
	name = CB_NAME_SUBJUGATION
	war_name = WAR_NAME_SUBJUGATION
	sprite = 8
	truce_days = 1825
	hostile_against_others = yes
	is_permanent = yes
	can_ask_to_join_war = no
	can_call_allies = no
	#check_de_jure_tier = COUNT # this scans all dejure duchies for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	
	allowed_to_target_tributaries = no
	allowed_to_target_suzerains = no
	
	can_use_gui = {
		ROOT = {
			is_patrician = no
		}
	}
	
	can_use = {
		has_global_flag = emf_rapid_conquest
		ROOT = { ai_not_busy_trigger = yes }
		ROOT = { NOT = { has_character_modifier = holy_truce_timer } }
		FROM = { NOT = { has_character_modifier = holy_truce_timer } }
		ROOT = {
			higher_tier_than = duke
			independent = yes
		}
		FROM = {
			tier = duke
			any_realm_province = {
				any_neighbor_province = {
					owner = {
						OR = {
							character = ROOT
							is_liege_or_above = ROOT
						}
					}
				}
			}
			NOT = { realm_size = 20 }
			NOR = {
				primary_title = { is_primary_type_title = yes }
				any_demesne_title = {
					temporary = yes
				}
			}
		}
	}

	on_add = {
		log = "CB(subjugation_duke): START: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
	}
	
	on_success = {
		log = "CB(subjugation_duke): VICTORY: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
		emf_cb_victory_effect = yes
		ROOT = {
			prestige = 100
		}
		FROM = {
			prestige = -100
			set_defacto_liege = ROOT
		}
	}
	
	on_fail = {
		log = "CB(subjugation_duke): WHITEPEACE: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
		ROOT = {
			prestige = -25
		}
	}
	
	on_reverse_demand = {
		log = "CB(subjugation_duke): DEFEAT: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
		emf_cb_defeat_effect = yes
		ROOT = {
			prestige = -50
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
		}
		FROM = {
			prestige = 50
		}
	}

	on_invalidation = {
		log = "CB(subjugation_duke): INVALIDATED: [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (ID: [Root.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (ID: [From.GetID])"
	}
	
	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
	}
}
# emf_rapid_conquest end