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

# max_defender_occupation_score/max_attacker_occupation_score - Defines how much war score it is possible for the defender/attacker to gain by occupying provinces. Defaults to 100. Make sure the CB has ticking warscore if using this or you could end up with never-ending wars
# max_defender_battle_score/max_attacker_battle_score - Defines how much score it is possible for the defender/attacker to gain by fighting battles. Defaults to the defines MAX_WARSCORE_FROM_BATTLE_DEFENDERS/MAX_WARSCORE_FROM_BATTLE_ATTACKERS
# hostages_block_cb	- Defaults to "yes". If set to "no", the defender having close relatives of yours imprisoned does not prevent you from declaring war
# attacker_unoccupied_warscore = yes - If attacker completely unoccupied, attacker will get ticking warscore. Only works if no title is targetted
# defender_unoccupied_warscore = yes - If defender completely unoccupied, defender will get ticking warscore. Only works if no title is targetted
# capturing_attacker_is_complete_victory - Whether capturing the attacker as a prisoner is considered automatic victory for the defender. Defaults to yes. If set to no, capture instead gives CAPTURED_HEIR_WAR_SCORE warscore
# capturing_defender_is_complete_victory - Whether capturing the defender as a prisoner is considered automatic victory for the attacker. Defaults to yes. If set to no, capture instead gives CAPTURED_HEIR_WAR_SCORE warscore

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

	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		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
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_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 }
				}
			}
			if = { #China with no land calculates what to do with nomad provinces
				limit = { 
					is_offmap_governor = yes
					offmap_power = { is_offmap_tag = offmap_china }
					offmap_china = { has_offmap_tmp_flag = WP_is_not_on_map }
					NOT = { wealth = 2500 } #unless already rich from tributes
				}
				set_variable = { which = "WP_nomad_provinces" value = 0 }
				any_realm_province = {
					limit = { 
						has_province_modifier = nomad_agitation
						NOT = { has_settlement_construction = yes }
					}
					province_event = { id = JD.60100 } #increase counter for each nomad province
				}
				character_event = { id = JD.60101 days = 1 } #calculate gold sent from china for building holdings
			}
		}
		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 = roman_pagan
								}
							}
						}
						OR = {
							religion_group = christian
							is_pagan_religion = yes
						}
					}
					character_event = { id = LoR.30 days = 12 }
				}
			}
		}
		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
		}
		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
			}
			hidden_effect = {
				if = { #Makes China have a chance of leaving Expanionist policy...
					limit = {
						has_landed_title = e_china_west_governor
						offmap_china = {
							has_policy = china_expansionist
						}
					}
					random = {
						chance = 50
						offmap_china = {
							set_policy = china_open
							log = "-------------------------------------"
							log = "China Policy Logging:"
							log = "Chinas new Policy is now OPEN"
							log = "-------------------------------------"
						}
					}
					log = "China War Logging: China lost a claim all war against [From.GetBestName]"
				}
			}
		}
		FROM = {
			participation_scaled_prestige = 200
			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 = roman_pagan
							}
						}
					}
					OR = {
						religion_group = christian
						is_pagan_religion = yes
					}
				}
				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

	press_claim = yes

	can_use_title = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		ROOT = { attacking_religious_head_trigger = no }
		#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
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_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 }
		}
		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
				}
			}
		}		
		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 }
		}
		hidden_effect = {
			ROOT = {
				if = {
					limit = {
						has_landed_title = e_china_west_governor
					}
					log = "China War Logging: China lost a claim war against [From.GetBestName]"
				}
				if = { #Makes China have a chance of leaving Expanionist policy...
					limit = {
						has_landed_title = e_china_west_governor
						offmap_china = {
							has_policy = china_expansionist
						}
					}
					random = {
						chance = 50
						offmap_china = {
							set_policy = china_open
							log = "-------------------------------------"
							log = "China Policy Logging:"
							log = "Chinas new Policy is now OPEN"
							log = "-------------------------------------"
						}
					}
				}
			}
		}
	}

	on_reverse_demand_title = {
		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
	
	infamy_modifier = 0.5

	press_claim = yes

	can_use = {
		cb_can_use_preamble = yes
		cb_other_no_nap_with_suzerain_trigger = yes
		attacking_religious_head_trigger = no
		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
					}
				}
			}
		}
		OR = {
			multiplayer = no
			FROMFROM = { ai = yes }
			has_game_rule = {
				name = multiplayer_third_party_claims
				value = on
			}
			AND = {
				has_game_rule = {
					name = multiplayer_third_party_claims
					value = limited
				}
				lower_tier_than = KING
			}
		}
	}

	is_valid_title = {
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
		ROOT = { has_claim = PREV }
	}
	
	on_add = {
		FROM = {
			attacked_tributary_cb_effect = yes
		}
		ROOT = {
			set_character_flag = claim_being_pressed
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}	

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

	can_use = {
		ROOT = {
			mercenary = no
		}
	}

	can_use_title = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		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 = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	on_success = {
		ROOT = {
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			participation_scaled_prestige = 300
		}
		FROM = {
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
		}
		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 = roman_pagan
								}
							}
						}
						OR = {
							religion_group = christian
							is_pagan_religion = yes
						}
					}
					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
		}
		FROM = {
			participation_scaled_prestige = 300
			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 = roman_pagan
							}
						}
					}
					OR = {
						religion_group = christian
						is_pagan_religion = yes
					}
				}
				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

	can_use_gui = {
		ROOT = {
			prestige = 500
		}
	}
	
	on_add = {
		ROOT = { prestige = -500 }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}	
	
	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		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 = {
								is_pagan_religion = yes
								AND = {
									NOT = { is_title_active = d_aztec }
									religion = aztec
								}
							}
							is_reformed_pagan_religion = no
							OR = {
								culture_group = altaic
								culture_group = magyar
								culture_group = mesoamerican
							}
							FROM = {
								NOT = { is_pagan_religion = yes } # Should subjugate these instead
							}
						}
					}
				}
				AND = {
					NOT = { has_dlc = "Horse Lords" }
					independent = yes
					higher_tier_than = COUNT
					OR = {
						AND = {
							is_pagan_religion = yes
							is_reformed_pagan_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 = { is_pagan_religion = yes } # 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 = {
			OR = {
				ai = yes
				multiplayer = no
				NOT = {
					has_game_rule = {
						name = multiplayer_invasions
						value = off
					}
				}
			}
			any_realm_province = {
				OR = {
					county = { de_jure_liege_or_above  = PREVPREVPREV }
					any_neighbor_province = {
						county = { de_jure_liege_or_above  = PREVPREVPREVPREV }
					}
				}
			}
			OR = { # This ensures that the AI doesn't declare this type of war against someone who only holds a few counties within the targeted title...
				ROOT = {
					ai = no
				}
				ROOT = {
					OR = {
						has_landed_title = e_mongol_empire
						has_landed_title = e_mexikha
					}
				}
				FROM = {
					num_of_realm_counties = {
						value = 3
						title = PREV
					}
				}
			}
		}
	}
	
	is_valid_title = {
		FROM = {
			any_realm_province = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}

	on_success = {
		ROOT = {
			prestige = 300
			imperial_decadence_minus_1_effect = yes
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			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
			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 }
					}
				}
			}
			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 = roman_pagan
							}
						}
					}
					OR = {
						religion_group = christian
						is_pagan_religion = yes
					}
				}
				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
				is_pagan_religion = yes
			}
			FROM = {
				culture = mongol
				is_pagan_religion = yes
			}
		}
		modifier = {
			factor = 0
			ROOT = { has_character_flag = no_invasions }
		}
		modifier = {
			factor = 0
			ROOT = {
				has_character_flag = is_temujin
				has_landed_title = e_mongol_empire
				demesne_size = 1
			}
			FROM = {
				has_landed_title = e_china_west_governor
			}
		}
	}
}

crusade = {
	name = CB_NAME_CRUSADE
	war_name = WAR_NAME_CRUSADE
	sprite = 9
	truce_days = 3650
	is_permanent = yes
	is_holy_war = yes
	battle_warscore_mult = 0.5
	max_defender_occupation_score = 85
	check_de_jure_tier = KING # this scans all de jure kingdoms for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	
	allowed_to_target_tributaries = 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 }
		}
		cb_can_use_preamble = yes
		FROM = { NOT = { has_character_modifier = crusade_block_timer } }		
		ROOT = { # The Religious Head
			has_called_crusade = no
			has_secret_religion = 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 = {
					is_pagan_religion = yes
					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 = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		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 = {
					is_pagan_religion = yes
					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
			}
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}
	
	is_valid = {
		ROOT = {
			can_call_crusade = yes
		}
		FROM = {
			OR = {
				NOT = { religion_group = ROOT }
				is_heresy_of = ROOT
				is_parent_religion = ROOT
				AND = {
					is_pagan_religion = yes
					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_province = {
				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
		
		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
			}
		}

		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
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		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
						}
					}
				}
			}
			any_attacker = {
				limit = {
					character = ROOT
					uses_decadence = yes
				}
				participation_scaled_decadence = 35
			}
			any_attacker = {
				limit = {
					NOT = { character = ROOT }
					uses_decadence = yes
				}
				hidden_tooltip = { 
					participation_scaled_decadence = 35
				}
			}
		}
		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 }
			}
		}
	}
}

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

	allowed_to_target_tributaries = no
	
	on_add = {
		hidden_tooltip = {
			ROOT = {
				set_character_flag = used_liberate_opportunity
			}
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

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

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

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

	on_reverse_demand = {
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -250
			prestige = -500
			pf_glory_minus2_effect = yes
		}
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes

			participation_scaled_piety = 200
			participation_scaled_prestige = 100
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_piety = 200
				participation_scaled_prestige = 100
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
				hidden_tooltip = {
					if = {
						limit = {
							has_ambition = obj_strengthen_religion
						}
						change_variable = { which = strengthen_religion value = 1 }
					}
				}
			}
		}
		any_attacker = {
			limit = {
				character = ROOT
				uses_decadence = yes
			}
			participation_scaled_decadence = 20
		}
		any_attacker = {
			limit = {
				NOT = { character = ROOT }
				uses_decadence = yes
			}
			hidden_tooltip = { 
				participation_scaled_decadence = 20
			}
		}
		ROOT = { 
			religion_authority = {
				modifier = lost_holy_war
				years = 20
			}
		}
		FROM = { 
			religion_authority = {
				modifier = won_holy_war
				years = 20
			}
			hidden_tooltip = {
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}			
		}
	}

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

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

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

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

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

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

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

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

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

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

#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
	
	can_use_gui = {
		ROOT = {
			has_secret_religion = no
			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
			}
		}
	}
	
	on_add = {
		ROOT = {
			if = {
				limit = {
					religion = buddhist
					NOT = { culture_group = tibetan_group }
				}
				piety = -250
			}
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = 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 = { is_pagan_religion = yes } }
				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 = {
					is_pagan_religion = yes
					is_reformed_pagan_religion = yes
					has_global_flag = pagan_ghws_unlocked
				}
			}
			OR = {
				ai = no
				NOT = { is_pagan_religion = yes }
				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 = { is_pagan_religion = yes }
					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 = { has_dharmic_religion_trigger = yes }
				PREV = {
					capital_scope = {
						OR = {
							region = world_india_deccan
							region = world_india_bengal
							region = world_india_rajastan
						}
						NOT = { culture_group = tibetan_group }
					}
				}
				piety = 300
			}
			#slow down Tibetan expansion outside of Bengal and Tibet
			OR = {
				ai = no
				PREV = { any_direct_de_jure_vassal_title = { any_direct_de_jure_vassal_title = { is_holy_site = ROOT } } }
				NOT = { has_dharmic_religion_trigger = yes }
				PREV = {
					capital_scope = {
						OR = {
							region = world_himalayas
							region = custom_kamarupa
							region = custom_bengal
							region = world_steppe_tarim
							duchy = { title = d_kumul } # aka Xizhou
						}
					}
					culture_group = tibetan_group
				}
				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 = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		FROM = { NOT = { has_character_modifier = crusade_block_timer } }
		ROOT = { can_use_holy_war_cb = yes }
	}

	is_valid = {
		ROOT = {
			OR = { 
				NOT = { same_realm = FROM }
				AND = { # Tibetan cultures can holy war each other within a realm if they are buddhist and bon
					culture_group = tibetan_group
					OR = {
						religion = buddhist
						religion = bon
					}
					NOT = { religion = FROM }
				}
			}
			NOT = { pacifist = yes }
			OR = {
				NOT = { religion_group = FROM }
				is_heresy_of = FROM
				is_parent_religion = FROM
				AND = {
					has_dharmic_religion_trigger = yes
					FROM = { religion = taoist }
				}
				AND = {
					OR = {
						religion_group = muslim
						is_pagan_religion = yes
						religion = paulician
						AND = {
							culture_group = tibetan_group
							OR = {
								religion = buddhist
								religion = bon
							}
						}
					}
					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 = {
		FROM = {
			imperial_decadence_plus_1_effect = yes
			pf_glory_minus2_effect = yes
			pf_tradition_minus4_effect = yes
		}
		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
			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
					}
				}
			}
			hidden_tooltip = {
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
		}		
		if = {
			limit = {
				ROOT = { is_heretic = no }
			}
			ROOT = {
				religion_authority = {
					modifier = won_holy_war
					years = 20
				}
				hidden_tooltip = {
					if = {
						limit = {
							has_ambition = obj_strengthen_religion
						}
						change_variable = { which = strengthen_religion value = 1 }
					}
				}
				hidden_tooltip = {
					if = {
						limit = {
							has_nickname = no
							is_pagan_religion = yes
						}
						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 = {
		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
		}		
		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
			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
					}
				}
				hidden_tooltip = {
					if = {
						limit = {
							has_ambition = obj_strengthen_religion
						}
						change_variable = { which = strengthen_religion value = 1 }
					}
				}
			}
		}
		any_attacker = {
			limit = {
				character = ROOT
				uses_decadence = yes
			}
			participation_scaled_decadence = 20
		}
		any_attacker = {
			limit = {
				NOT = { character = ROOT }
				uses_decadence = yes
			}
			hidden_tooltip = { 
				participation_scaled_decadence = 20
			}
		}
		if = {
			limit = {
				FROM = { is_heretic = no }
			}
			FROM = { 
				religion_authority = {
					modifier = won_holy_war
					years = 20
				}
				hidden_tooltip = {
					if = {
						limit = {
							has_ambition = obj_strengthen_religion
						}
						change_variable = { which = strengthen_religion value = 1 }
					}
				}
			}
		}
		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 = { has_dharmic_religion_trigger = yes }
			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
	threatening = no
	
	allowed_to_target_tributaries = no

	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = 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
				set_character_flag = ai_flag_never_transfer
			}
			FROM = {
				opinion = {
					who = ROOT
					modifier = opinion_independent
					years = 50
				}
				pf_glory_minus2_effect = yes
			}
		}

		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
			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
		}
		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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	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 }
						}
					}
				}
			}
		}
		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
		}
		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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

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

	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 = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

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

force_vassalization = { # rewrote vanilla CB
	name = CB_NAME_FORCE_VASSALIZATION
	war_name = WAR_NAME_FORCE_VASSALIZATION
	sprite = 32
	truce_days = 365
	is_permanent = no
	can_ask_to_join_war = no
	allowed_to_target_tributaries = no
	display_on_map = no
	threatening = no
	
	defender_unoccupied_warscore = yes
	
	can_use = {
		ROOT = {
			has_character_flag = trying_to_vassalize_tributary
		}
	}
	
	on_add = {
		ROOT = {
			if = {
				limit = {
					FROM = { tier = COUNT }
				}
				prestige = -200
			}
			if = {
				limit = {
					FROM = { tier = DUKE }
				}
				prestige = -500
			}
			if = {
				limit = {
					FROM = { tier = KING }
				}
				prestige = -1200
			}
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}
	
	is_valid = {
		FROM = {
			is_landed = yes
			lower_tier_than = ROOT
		}
	}

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

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

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

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

coastal_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
	
	allow_distant = yes # AI parameter

	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		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
		
		location = {
			port = yes
			has_trade_post = yes
			trade_post_owner = {
				character = ROOT
			}
			
			county = { # Attacker, or a vassal of the attacker, owns a city in the county
				any_direct_de_jure_vassal_title = {
					tier = baron
					is_republic = yes
					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
			}
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_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 coastal county republic war for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"
		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 = 650	#  From  aprox.Venice to East Cyprus and from Venice to Gibraltar
								}
							}	
						}
					}			
				}
			}
			custom_tooltip = {
				text = CTT_REP_IN_RANGE
			}
		}
		else = {
			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
				}
			}
		}
		else = {
			usurp_title = ROOT
		}

		hidden_tooltip = {
			ROOT = {
				remove_character_modifier = rep_cb_cd
				add_character_modifier = {
					name = rep_cb_cd
					duration = 1825
					hidden = no
					inherit = yes
				}
			}
		}		
		
		ROOT = {
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}   

	on_fail = {
		ROOT = {
			prestige = -100
		}
		FROM = {
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		
		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
		}
		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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			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
		}
		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
			participation_scaled_prestige = 100
		}
		ROOT = {
			prestige = -200
			imprison = FROM
			cancel_plot = plot_weaken_fellow_vassal
			pf_glory_minus2_effect = yes
		}
		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
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_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		
		
		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
				}
			}
		}
		
		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

	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		ROOT = {
			has_landed_title = e_roman_empire
			independent = yes
			OR = {
				religion_group = christian
				religion_group = hellenic_pagan_group
			}
			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
				is_pagan_religion = yes
			}
			NOT = { same_realm = FROM }
		}
	}
	
	on_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

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

	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		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 = {
					OR = {
						is_merchant_republic = yes
						higher_tier_than = DUKE
					}
					any_realm_lord = {
						is_patrician = yes
						any_trade_post = {
							owner = {
								OR = {
									character = ROOT
									is_liege_or_above = ROOT
								}
							}
						}
					}
				}
			}
		}
		#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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = 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
	att_ticking_war_score_multiplier = 5.0
	def_ticking_war_score_multiplier = 5.0
	allow_distant = yes
	
	allowed_to_target_tributaries = no

	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		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 = {
			NOT = { is_liege_or_above = ROOT }
		}
	}

	can_use_title = {
		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 = 650	#  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
			}
		}
	}

	is_valid = {
		ROOT = {
			OR = {
				is_merchant_republic = yes
				is_patrician = yes
			}
			NOT = { is_liege_or_above = FROM }
		}
		FROM = {
			NOT = { is_liege_or_above = ROOT }
		}
	}
	
	is_valid_title = {
		has_trade_post = yes
		trade_post_owner = {
			OR = {
				character = FROM
				is_liege_or_above = FROM
			}
		}
	}

	on_add = {
		ROOT = {
			add_character_modifier = {
				name = rep_tp_cd
				duration = 1825
				hidden = no
				inherit = yes
			}
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = 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
		
		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
			}
		}
		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
			}
		}
		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 = 20
	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

	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		ROOT = {
			has_opinion_modifier = { who = FROM modifier = preparing_to_invade }
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}		
	}

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

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

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

	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 }
					}
				}
			}
			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
			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 } }
			}
			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
	
	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		ROOT = {
			mercenary = no
			lower_tier_than = EMPEROR
			OR = {
				AND = {
					is_pagan_religion = yes
					is_reformed_pagan_religion = no
				}
				AND = {
					has_dharmic_religion_trigger = yes
					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 = {
					is_pagan_religion = yes
					OR = {
						is_reformed_pagan_religion = no
						ROOT = { is_reformed_pagan_religion = yes }
					}
				}
				AND = {
					has_dharmic_religion_trigger = yes
					OR = {
						culture_group = indo_aryan_group
						culture_group = dravidian_group
					}			
				}
			}
			NOT = { is_offmap_governor = yes }
			#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
					}
				}
			}
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		ROOT = {
			piety = 100
		}
		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 = {
					ROOT = { is_reformed_pagan_religion = yes }
					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 }
			piety = 50
			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 } }
			}
			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
	threatening = no
	
	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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	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
	threatening = no
	check_de_jure_tier = DUKE

	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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}


	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
	threatening = no
	check_de_jure_tier = DUKE
	
	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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}
 
	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
	threatening = 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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}
	
	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 
						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 = BARON
					is_nomadic = no
					holder_scope = {						
						same_realm = FROM							
						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 = COUNT					
					holder_scope = { 
						same_realm = FROM 
						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 }
					set_defacto_liege = ROOT
				}
				if = {
					limit = { NOT = { 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 }
				}
				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 }
						set_defacto_liege = PREV
					}
					if = {
						limit = { NOT = { 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 }
					}
					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 }
					owner = {
						any_courtier = {
							banish = 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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = 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
					}
				}
				# Special case: Sultanate of Rum creator succeeds in his first conquest
				if = {
					limit = {
						has_character_flag = rum_creator
					}
					character_event = {
						id = 60610
						days = 600
						random = 300
					}
				}
			}
		}
		
		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 }
		}
	}
}

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

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

	is_valid = {
		always = yes
	}

	on_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}
	
	on_success = {
		ROOT = { 
			prestige = 500
			piety = 250
			hidden_tooltip = {
				clr_character_flag = duchy_adventurer
				remove_do_not_disturb_effect = yes
				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_victorious }
						5 = { give_nickname = nick_the_conqueror }
						5 = { give_nickname = nick_the_dragon }
						10 = { give_nickname = nick_the_rel_group }
						10 = { give_nickname = nick_the_tiger }
						10 = { give_nickname = nick_the_world_burner }
						10 = { 
							give_nickname = nick_the_scourge_of_god
							modifier = {
								factor = 0
								event_target:nickname_target = { religion = ROOT }
							}
						}
					}
				}
			}
		}
		FROM = {
			prestige = -500
			piety = -250			
		}
	}

	on_success_title = {
		custom_tooltip = {
			text = other_invasion_succ_tip
			hidden_tooltip = {				
				FROM = { # The adventurer takes ANY demesne title, including the targeted Kingdom, within the area of the targeted kingdom
					any_demesne_title = {
						limit = {
							OR = {
								title = PREVPREV
								de_jure_liege_or_above = PREVPREV
							}
							NOT = { ROOT = { has_character_flag = rum_creator } }
						}
						usurp_title = { target = ROOT type = host }
					}
				}
				
				if = { # Claim the targeted Kingdom if it isn't held by anyone
					limit = {
						has_holder = no
					}
					usurp_title = { target = ROOT type = host }
				}
				
				if = { # Create a Kingdom if the adventurer does not have one by this point
					limit = {
						ROOT = {
							NOR = {
								any_demesne_title = {
									tier = KING
								}
								has_character_flag = rum_creator
							}
						}
					}
					ROOT = {
						create_title = {
							tier = KING
							landless = no
							temporary = no
							custom_created = yes
							culture = ROOT
							holder = ROOT
							name = "INDIAN_MUSLIM_KINGDOM"
							base_title = PREV
							copy_title_laws = yes
						}
					}
				}
				if = {
					limit = {
						ROOT = { has_character_flag = rum_creator }
					}
					k_rum = {
						grant_title = ROOT
					}
				}
				
				ROOT = { # Preserve Vassals under the targeted kingdom
					vassalize_or_take_under_title = { # Take all vassals held by the target under the title
						title = PREV
						enemy = FROM
						type = host
					}
					set_defacto_liege = ROOT # Always go Independent
				}
			}
		}
		
		ROOT = {
			hidden_tooltip = {
				if = {
					limit = {
						has_character_flag = rum_creator
					}
					capital_scope = {
						culture = ROOT
					}
				}
				if = {
					limit = {
						NOT = { has_character_flag = rum_creator }
					}
					capital_scope = {
						reverse_culture = ROOT
					}
					any_child = {
						limit = {
							is_ruler = no
							ai = yes
							#liege = { character = ROOT }
						}
						culture = ROOT
					}
					any_courtier = {
						limit = {
							is_ruler = no
							ai = yes
						}
						culture = ROOT
					}
				}
				
				# Special case: Sultanate of Rum might start yet another war
				if = {
					limit = {
						has_character_flag = rum_creator
					}
					character_event = {
						id = 60610
						days = 600
						random = 300
					}
				}
			}
		}
	}

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

	on_reverse_demand = {
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		
		FROM = {
			prestige = 200
		}
		
		ROOT = {
			clear_wealth = yes
			imprison = FROM
			prestige = -200
			hidden_tooltip = {
				disband_event_forces = yes
				clr_character_flag = duchy_adventurer
			}
		}
	}
	
	on_attacker_leader_death = {
		ROOT = {
			hidden_tooltip = {
				disband_event_forces = yes
				clr_character_flag = duchy_adventurer
			}
		}
		end_war = invalid
	}

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

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

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
	
	can_use_gui = {
		ROOT = {
			piety = 50
		}
	}
	
	on_add = {
		ROOT = { piety = -50 }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}
	
	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		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_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 }
			}
		}

		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 } }
		}
		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
			}
		}
		FROM = {
			participation_scaled_prestige = 200
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
	}
	
	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = { 
		factor = 1
		
		modifier = {
			factor = 0.9 # Prefer using CB's without on_add costs
		}
		modifier = {
			factor = 0
			ROOT = {
				has_character_flag = is_temujin
				has_landed_title = e_mongol_empire
				demesne_size = 1
			}
			FROM = {
				has_landed_title = e_china_west_governor
			}
		}
	}
}

pagan_county_conquest = {
	name = CB_NAME_COUNTY_CONQUEST
	war_name = WAR_NAME_COUNTY_CONQUEST
	sprite = 22
	truce_days = 1825
	hostile_against_others = yes
	is_permanent = yes
	check_all_titles = yes # if permanent, setting this to true will check against all of someones titles, including vassal held titles
	press_claim = yes
	can_ask_to_join_war = no
	
	allowed_to_target_tributaries = no
	
	allow_distant = yes
	
	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		ROOT = {
			mercenary = no
		}
		OR = {
			ROOT = {
				is_nomadic = yes
				NOT = { same_realm = FROM }
				FROM = {
					OR = {
						is_nomadic = yes
						is_tribal = yes
					}
				}
			}
			AND = {
				ROOT = {
					is_pagan_religion = yes
					is_reformed_pagan_religion = no
					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 = {
					is_pagan_religion = yes
					is_reformed_pagan_religion = no
					NOT = { same_realm = FROM }
				}
				FROM = {
					NOT = { religion = ROOT }
				}
			}
		}
	
		OR = {
			FROM = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = FROM
			}
		}
	}

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

	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
		}
		
		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 } }
		}
		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
				}
			}
		}
		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
				}
			}
		}
		modifier = {
			factor = 0
			ROOT = {
				has_character_flag = is_temujin
				has_landed_title = e_mongol_empire
				demesne_size = 1
			}
			FROM = {
				has_landed_title = e_china_west_governor
			}
		}
	}
}

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
	
	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
		cb_no_nap_with_suzerain_trigger = yes
		NOT = {
			ROOT = {
				OR = {
					is_pagan_religion = no
					is_reformed_pagan_religion = yes
				}
				OR = {
					NOR = {
						religion = hindu
						religion = buddhist
					}
					FROM = { NOT = { has_dharmic_religion_trigger = yes } }
				}
				OR = {
					NOT = { religion_group = FROM }
					is_heresy_of = FROM
					is_parent_religion = FROM
					AND = {
						OR = {
							religion_group = muslim
							is_pagan_religion = yes
							religion = paulician
						}
						NOT = { religion = FROM }
					}
				}
				NOR = {
					pacifist = yes
					same_realm = FROM
				}
			}
		}
	}
	
	can_use_title = {
		cb_can_use_preamble = yes
		ROOT = { attacking_religious_head_trigger = no }

		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
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		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
		}
		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
				is_pagan_religion = yes
			}
			FROM = {
				culture = mongol
				is_pagan_religion = yes
			}
		}
		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
		}
		modifier = {
			factor = 0 # AIs will not take the last counties off their liege's direct vassals
			ROOT = {
				same_liege = FROM
			}
			FROM = {
				NOT = {
					any_demesne_title = {
						tier = count
						NOT = { title = PREVPREV }
					}
				}
			}
		}
		modifier = {
			factor = 0
			ROOT = {
				has_character_flag = is_temujin
				has_landed_title = e_mongol_empire
				demesne_size = 1
			}
			FROM = {
				has_landed_title = e_china_west_governor
			}
		}
	}
}

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

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

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

			holder_scope = {
				set_defacto_liege = ROOT
			}
		}

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

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

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

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

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

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

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = { 
		factor = 1
		
		modifier = {
			factor = 0
			ROOT = {
				NOT = { scaled_wealth = 1.25 }
			}
		}
	}
}

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
	
	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		#check to ensure the target is not valid for a holy war
		NOT = {
			ROOT = {
				OR = {
					is_pagan_religion = no
					is_reformed_pagan_religion = yes
				}
				OR = {
					NOR = {
						religion = hindu
						religion = buddhist
					}
					FROM = { NOT = { has_dharmic_religion_trigger = yes } }
				}
				OR = {
					NOT = { religion_group = FROM }
					is_heresy_of = FROM
					is_parent_religion = FROM
					AND = {
						OR = {
							religion_group = muslim
							is_pagan_religion = yes
							religion = paulician
						}
						NOT = { religion = FROM }
					}
				}
				NOR = {
					pacifist = yes
					same_realm = FROM
				}
			}
		}

		ROOT = { attacking_religious_head_trigger = no }
		OR = {
			ROOT = { ai = no }
			FROM = {
				NOT = { is_vassal_or_below = ROOT }
				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
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_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 }
		}
		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
		}
		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
	
	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 = {
		cb_can_use_preamble = yes
		cb_other_no_nap_with_suzerain_trigger = yes
		#check to ensure the target is not valid for a holy war
		NOT = {
			AND = {
				OR = { 
					is_pagan_religion = no 
					is_reformed_pagan_religion = yes 
				} 
				OR = {
					NOR = {
						religion = hindu
						religion = buddhist
					}
					FROM = { NOT = { has_dharmic_religion_trigger = yes } }
				}
				OR = {
					NOT = { religion_group = FROM }
					is_heresy_of = FROM
					is_parent_religion = FROM
					AND = {
						OR = {
							religion_group = muslim
							is_pagan_religion = yes
							religion = paulician
						}
						NOT = { religion = FROM }
					}
				}
				NOR = {
					pacifist = yes
					same_realm = FROM
				}
			}
		}

		attacking_religious_head_trigger = no
		
		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
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	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
			}
		}
		ROOT = {
			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
		}
		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
				is_pagan_religion = yes
			}
			FROM = {
				culture = mongol
				is_pagan_religion = yes
			}
		}
		modifier = { # The AI will never breach a tributary's trust
			factor = 0
			ai_attack_tributary_trigger = yes
		}
		modifier = {
			factor = 0 # AIs will not take the last counties off their liege's direct vassals
			ROOT = {
				same_liege = FROM
			}
			FROM = {
				NOT = {
					any_demesne_title = {
						tier = count
						NOT = { title = PREVPREV }
					}
				}
			}
		}
	}
}

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

	can_use = {
		ROOT = {
			has_character_flag = shia_caliphate_pretender
		}
	}

	is_valid_title = {
		ROOT = {
			has_character_flag = shia_caliphate_pretender
		}
		FROM = {
			any_realm_title = {
				de_jure_liege_or_above = PREVPREV
			}
		}
		d_shiite = {
			has_holder = no
		}
	}
	
	on_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	on_success = {
		clr_global_flag = shia_caliphate_revolt_ongoing
		ROOT = {
			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
					wealth = 500
				}
				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
			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 = 29
	truce_days = 3650
	is_permanent = yes
	can_ask_to_join_war = yes
	
	defender_unoccupied_warscore = yes
	
	attacker_rel_head_is_ally = yes # The attacker can call his (main) Pope into the war
	
	allowed_to_target_tributaries = no
	
	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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}
	
	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
			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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}
	
	on_success = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their install antiking CB against [From.GetBestName] (ID: [From.GetID])"
		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 = {
		cb_can_use_preamble = yes	
		cb_no_nap_with_suzerain_trigger = yes
		ROOT = {
			OR = {
				ai = yes
				multiplayer = no
				NOT = {
					has_game_rule = {
						name = multiplayer_invasions
						value = off
					}
				}
			}
			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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	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 }
					}
				}
			}
			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 } }
			}
			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
		}
		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 = roman_pagan
							}
						}
					}
					OR = {
						religion_group = christian
						is_pagan_religion = yes
					}
				}
				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
	
	can_use = {
		cb_can_use_preamble = yes
		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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}
	
	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			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
	
	defender_unoccupied_warscore = yes
	
	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		ROOT = { attacking_religious_head_trigger = no }
		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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

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

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

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

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

	on_reverse_demand = {
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 3.0
			}
			prestige = -250
			pf_glory_minus2_effect = yes
		}
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			prestige = 250
		}
	}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

# Tributary CB
# TODO: there are several new tributary types. We won't add new CBs just tributary types. Appropriate type should be selected depending on situation.
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
	threatening = no
	allowed_to_target_tributaries = no
	
	defender_unoccupied_warscore = yes

	can_use = {
		ROOT = {
			has_character_flag = trying_to_gain_tributary
		}
	}

	is_valid = {
		NOT = {
			ROOT = {
				rightful_religious_head_scope = {
					character = FROM 
				}
			}
		}
	}
	
	on_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	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
			if = {
				limit = { is_nomadic = yes}
				make_tributary = { who = FROM tributary_type = nomadic }
			}
			else = {
				make_tributary = { who = FROM tributary_type = forced }
			}
			hidden_tooltip = {
				add_character_modifier = {
					name = tributary_timer
					duration = 1825
					hidden = yes
				}
			}
		}
		
		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 = 25
	truce_days = 365
	is_permanent = yes
	hostile_against_others = yes
	can_ask_to_join_war = no
	is_independence = yes

	can_use = {
		has_dlc = "Horse Lords"
		cb_can_use_preamble = yes			
		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 }
			}
		}
		modifier = { 
			factor = 0.75 
			FROM = { 
				is_tributary = { type = voluntary } 
			} 
		}
	}

	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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	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
			}
		}
		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
		}
		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
	
	allow_distant = yes
	
	can_use = {
		has_dlc = "Horse Lords"
		cb_can_use_preamble = yes	
		FROM = {
			is_nomadic = yes
		}
		ROOT = {
			is_nomadic = yes
			same_realm = FROM
			has_feud_with = FROM
		}
		
		ROOT = {
			mercenary = no
		}
	}

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

	is_valid_title = {
		location = {
			NOT = { num_of_settlements = 1 }
		}
		FROM = {
			has_landed_title = PREV
		}
	}
	
	on_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

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

	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		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 }
				is_pagan_religion = yes
				is_reformed_pagan_religion = no
				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
					}
				}
			}
		}
		
		any_de_jure_vassal_title = { # Can only liberate Duchies that are either outside of the Steppe region, or contains a Castle/City/Tribe
			tier = COUNT
			OR = {
				holder_scope = {
					character = FROM
				}
				holder_scope = {
					any_liege = {
						character = FROM
					}
				}
			}
			OR = {
				location = {
					NOT = {
						region = world_steppe
					}
				}
				location = {
					any_province_holding = {
						OR = {
							holding_type = city
							holding_type = castle
							holding_type = tribal
						}
					}
				}
			}
		}
	}

	is_valid = {
		FROM = {
			is_nomadic = yes
		}
		ROOT = {
			is_nomadic = no
			NOT = { pacifist = yes }
			NOT = { same_realm = FROM }
			mercenary = no
		}
	}
	
	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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}
	
	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			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
			}
		}
		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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}
	
	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
	
	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 = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		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
				}
			}
			OR = {
				ai = yes
				multiplayer = no
				NOT = {
					has_game_rule = {
						name = multiplayer_invasions
						value = off
					}
				}
			}
		}
		
		FROM = {
			in_revolt = no
			is_nomadic = no # Nomads cannot be subjugated
			NOT = { is_offmap_governor = yes }
			NOT = {
				any_war = {
					defender = { character = PREVPREV }
					OR = {
						using_cb = nomad_subjugation
						using_cb = pagan_subjugation
					}
				}
			}
			OR = {
				NOT = { tier = EMPEROR }
				ROOT = { ai = no }
			}
		}
		OR = {
			ROOT = {
				ai = no
			}
			FROM = {
				any_realm_title = {
					tier = COUNT
					count = 5
				}
			}
		}
	}
	
	on_add = {
		ROOT = {
			if = {
				limit = {
					NOT = {
						has_opinion_modifier = {
							modifier = declared_independence_nomad
							who = FROM
						}
					}
				}
				prestige = -500
				add_character_modifier = { 
					name = launched_subjugation
					years = 100
					hidden = yes
				}
			}
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	on_success = {
		hidden_tooltip = {
			ROOT = {
				if = {
					limit = {
						has_nickname = no
					}
					FROM = {
						save_event_target_as = nickname_target
					}
					ROOT = { 
						save_event_target_as = nickname_receiver
					}
					random_list = {
						2 = { give_nickname = nick_the_eastern_wind }
						5 = { give_nickname = nick_the_despoiler }
						5 = { 
							give_nickname = nick_the_bane_of_realmname
							modifier = {
								factor = 0
								event_target:nickname_target = { is_nomadic = yes }
							}
							modifier = {
								factor = 0
								event_target:nickname_target = { culture = ROOT }
							}
						}
						5 = { 
							give_nickname = nick_the_culture_marauder
							modifier = {
								factor = 0
								event_target:nickname_target = { is_nomadic = yes }
							}
							modifier = {
								factor = 0
								event_target:nickname_target = { culture = ROOT }
							}
						}
						100 = { }
					}
				}
			}
		}
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			piety = 100
			prestige = 300
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		FROM = {
			piety = -100
			prestige = -100
			
			if = {
				limit = {
					ROOT = { is_reformed_pagan_religion = yes }
					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
			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 } }
			}
			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
		modifier = {
			factor = 0
			ROOT = {
				has_character_flag = is_temujin
				has_landed_title = e_mongol_empire
				demesne_size = 1
			}
			FROM = {
				has_landed_title = e_china_west_governor
			}
		}
	}
} 

# 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

	can_use_gui = {
		ROOT = {
			prestige = 500
			population = 30000
			population_factor = 0.75 # 75% of max
		}
	}
	
	on_add = {
		ROOT = { prestige = -500 }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		ROOT = {
			is_nomadic = yes
			OR = {
				ai = yes
				multiplayer = no
				NOT = {
					has_game_rule = {
						name = multiplayer_invasions
						value = off
					}
				}
			}
		}
		
		ROOT = {
			NOT = { same_realm = FROM }
		}
		
		OR = { # This ensures that the AI doesn't declare this type of war against someone who only holds a few counties within the targeted title...
			ROOT = {
				ai = no
			}
			ROOT = {
				has_landed_title = e_mongol_empire
			}
			FROM = {
				num_of_realm_counties = {
					value = 5
					title = PREV
				}
			}
		}
	}
	
	is_valid_title = {
		FROM = {
			any_realm_province = {
				de_jure_liege_or_above = PREVPREV
			}
		}
	}

	on_success = {
		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 }
					}
				}
			}
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		ROOT = {
			prestige = -250
		}
		log = "Nomad Invasion Failed [Root.GetTitledName]"
	}
	
	on_invalidation = {
		log = "Nomad Invasion Invalidated [Root.GetTitledName]"
	}

	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 } }
			}
			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
				}
			}
		}
		modifier = {
			factor = 0
			ROOT = {
				has_character_flag = is_temujin
				has_landed_title = e_mongol_empire
				demesne_size = 1
			}
			FROM = {
				has_landed_title = e_china_west_governor
			}
		}
	}
}

# 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

	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 }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		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
			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 = { }
				}
			}
			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
		}
		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
				}
			}
		}
		modifier = {
			factor = 0
			ROOT = {
				has_character_flag = is_temujin
				has_landed_title = e_mongol_empire
				demesne_size = 1
			}
			FROM = {
				has_landed_title = e_china_west_governor
			}
		}
	}
}

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

	on_success = {
		ROOT = {
			opinion = {
				modifier = opinion_increased_council_power
				who = FROM
				months = 12
			}
			participation_scaled_prestige = 100
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_success_title = {
		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
		}
		
		FROM = {
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		
		hidden_tooltip = {
			any_attacker = {
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_council_power_ultimatum_timer
					hidden = yes
					duration = 3650
				}
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -200
			prisoner = FROM
		}
		
		FROM = {
			participation_scaled_prestige = 100
		}
		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
	threatening = no
	allowed_to_target_tributaries = no
	
	defender_unoccupied_warscore = yes
	
	can_use = {
		has_dlc = "Horse Lords"
		
		FROM = {
			is_nomadic = yes
		}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

unjust_conquest = { # Border Dispute
	name = CB_NAME_UNJUST_COUNTY_CONQUEST
	war_name = WAR_NAME_UNJUST_COUNTY_CONQUEST
	sprite = 27
	truce_days = 1825
	hostile_against_others = yes
	is_permanent = yes
	check_all_titles = yes # if permanent, setting this to true will check against all of someones titles, including vassal held titles
	press_claim = yes
	can_ask_to_join_war = no
	allowed_to_target_tributaries = no
	infamy_modifier = 2
	
	can_use_gui = {
		ROOT = {
			show_scope_change = no
			conditional_tooltip = { # Pagans have a much easier time using this CB in general
				trigger = { 
					is_pagan_religion = yes
				}
				prestige = 100
			}
			conditional_tooltip = {
				trigger = { 
					tier = COUNT
					religion = FROM
					NOT = { is_pagan_religion = yes }
				}
				piety = 50
				scaled_wealth = { value = 1.5 min = 75 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = COUNT
					religion_group = FROM
					NOT = { religion = FROM }
					NOT = { is_pagan_religion = yes }
				}
				piety = 25
				scaled_wealth = { value = 1 min = 75 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = COUNT
					NOT = { religion_group = FROM }
					NOT = { is_pagan_religion = yes }
				}
				piety = 15
				scaled_wealth = { value = 0.5 min = 75 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = DUKE
					religion = FROM
					NOT = { is_pagan_religion = yes }
				}
				piety = 100
				scaled_wealth = { value = 1.5 min = 100 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = DUKE
					religion_group = FROM
					NOT = { religion = FROM }
					NOT = { is_pagan_religion = yes }
				}
				piety = 50
				scaled_wealth = { value = 1 min = 100 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = DUKE
					NOT = { religion_group = FROM }
					NOT = { is_pagan_religion = yes }
				}
				piety = 25
				scaled_wealth = { value = 0.5 min = 100 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = KING
					religion = FROM
					NOT = { is_pagan_religion = yes }
				}
				piety = 150
				scaled_wealth = { value = 1.5 min = 150 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = KING
					religion_group = FROM
					NOT = { religion = FROM }
					NOT = { is_pagan_religion = yes }
				}
				piety = 100
				scaled_wealth = { value = 1 min = 150 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = KING
					NOT = { religion_group = FROM }
					NOT = { is_pagan_religion = yes }
				}
				piety = 100
				scaled_wealth = { value = 1.0 min = 150 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = EMPEROR
					religion = FROM
					NOT = { is_pagan_religion = yes }
				}
				piety = 300
				scaled_wealth = { value = 1.5 min = 200 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = EMPEROR
					religion_group = FROM
					NOT = { religion = FROM }
					NOT = { is_pagan_religion = yes }
				}
				piety = 200
				scaled_wealth = { value = 1 min = 200 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = EMPEROR
					NOT = { religion_group = FROM }
					NOT = { is_pagan_religion = yes }
				}
				piety = 100
				scaled_wealth = { value = 0.5 min = 200 }
			}
		}
	}

	on_add = {
		ROOT = {
			show_scope_change = no
			if = { # Pagans have a much easier time using this CB in general
				limit = {
					is_pagan_religion = yes
				}
				prestige = -100
			}
			if = {
				limit = {
					tier = COUNT
					religion = FROM
					NOT = { is_pagan_religion = yes }
				}
				piety = -50
				scaled_wealth = { value = -1.5 min = -75 }
			}
			if = {
				limit = {
					tier = COUNT
					religion_group = FROM
					NOT = { religion = FROM }
					NOT = { is_pagan_religion = yes }
				}
				piety = -25
				scaled_wealth = { value = -1 min = -75 }
			}
			if = {
				limit = {
					tier = COUNT
					NOT = { religion_group = FROM }
					NOT = { is_pagan_religion = yes }
				}
				piety = -15
				scaled_wealth = { value = -0.5 min = -75 }
			}
			
			if = {
				limit = {
					tier = DUKE
					religion = FROM
					NOT = { is_pagan_religion = yes }
				}
				piety = -100
				scaled_wealth = { value = -1.5 min = -100 }
			}
			if = {
				limit = {
					tier = DUKE
					religion_group = FROM
					NOT = { religion = FROM }
					NOT = { is_pagan_religion = yes }
				}
				piety = -50
				scaled_wealth = { value = -1 min = -100 }
			}
			if = {
				limit = {
					tier = DUKE
					NOT = { religion_group = FROM }
					NOT = { is_pagan_religion = yes }
				}
				piety = -50
				scaled_wealth = { value = -0.5 min = -100 }
			}
			
			if = {
				limit = {
					tier = KING
					religion = FROM
					NOT = { is_pagan_religion = yes }
				}
				piety = -150
				scaled_wealth = { value = -1.5 min = -150 }
			}
			if = {
				limit = {
					tier = KING
					religion_group = FROM
					NOT = { religion = FROM }
					NOT = { is_pagan_religion = yes }
				}
				piety = -100
				scaled_wealth = { value = -1.0 min = -150 }
			}
			if = {
				limit = {
					tier = KING
					NOT = { religion_group = FROM }
					NOT = { is_pagan_religion = yes }
				}
				piety = -50
				scaled_wealth = { value = -0.5 min = -150 }
			}
			
			if = {
				limit = {
					tier = EMPEROR
					religion = FROM
					NOT = { is_pagan_religion = yes }
				}
				piety = -300
				scaled_wealth = { value = -1.5 min = -200 }
			}
			if = {
				limit = {
					tier = EMPEROR
					religion_group = FROM
					NOT = { religion = FROM }
					NOT = { is_pagan_religion = yes }
				}
				piety = -200
				scaled_wealth = { value = -1 min = -200 }
			}
			if = {
				limit = {
					tier = EMPEROR
					NOT = { religion_group = FROM }
					NOT = { is_pagan_religion = yes }
				}
				piety = -100
				scaled_wealth = { value = -0.5 min = -200 }
			}
						
			if = { # Opinion hit for same religion
				limit = {
					religion = FROM
				}
				if = {
					limit = {
						controls_religion = no
						NOT = {
							is_pagan_religion = yes
						}
					}
					rightful_religious_head_scope = {
						opinion = {
							who = ROOT
							modifier = opinion_declared_unjust_conquest
							years = 25
						}
					}
				}
				custom_tooltip = {
					text = custom_opinion_hit_tooltip
					hidden_effect = {
						generate_tooltip = no
						any_realm_character = {
							limit = {
								controls_religion = no
								religion_group = ROOT
								is_landed = yes
								NOT = {
									character = ROOT
								}
							}
							opinion = {
								who = ROOT
								modifier = opinion_declared_unjust_conquest
								years = 10
							}
						}
						FROM = {
							any_realm_character = {
								limit = {
									controls_religion = no
									religion_group = ROOT
									is_landed = yes
								}
								opinion = {
									who = ROOT
									modifier = opinion_declared_unjust_conquest
									years = 10
								}
							}
						}
					}
				}
			}
			if = { # Opinion hit for same group
				limit = {
					religion_group = FROM
					NOT = { religion = FROM }
				}
				custom_tooltip = {
					text = custom_opinion_hit_tooltip
					hidden_effect = {
						generate_tooltip = no
						any_realm_character = {
							limit = {
								religion_group = ROOT
								is_landed = yes
								NOT = {
									character = ROOT
								}
							}
							opinion = {
								who = ROOT
								modifier = opinion_declared_unjust_conquest
								years = 10
							}
						}
						FROM = {
							any_realm_character = {
								limit = {
									religion_group = ROOT
									is_landed = yes
								}
								opinion = {
									who = ROOT
									modifier = opinion_declared_unjust_conquest
									years = 10
								}
							}
						}
					}
				}
			}
			if = {
				limit = {
					NOT = { religion_group = FROM }
				}
				custom_tooltip = {
					text = custom_opinion_hit_tooltip_enemy
					hidden_effect = {
						generate_tooltip = no
						any_realm_character = {
							limit = {
								religion = FROM
								is_landed = yes
							}
							opinion = {
								who = ROOT
								modifier = opinion_declared_unjust_conquest
								years = 10
							}
						}
						FROM = {
							any_realm_character = {
								limit = {
									religion = ROOT
									is_landed = yes
								}
								opinion = {
									who = ROOT
									modifier = opinion_declared_unjust_conquest
									years = 10
								}
							}
						}
					}
				}
			}
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}
	
	can_use = {
		has_dlc = "Jade Dragon"
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		ROOT = {
			is_republic = no
			NOT = { can_use_holy_war_cb = yes }
			NOT = {
				has_game_rule = {
					name = unjustified_wars
					value = off
				}
			}
			NOT = { is_liege_or_above = FROM }
			mercenary = no
			NOT = { pacifist = yes }
			is_nomadic = no # Use the 'pagan_county_conquest' CB instead
			NOT = {
				religion_group = muslim # Use 'muslim_county_conquest' instead
			}
			NAND = { # Use 'pagan_county_conquest' instead
				is_pagan_religion = yes
				FROM = {
					NOT = {
						religion = ROOT
					}
				}
			}
		}
		FROM = { num_of_count_titles_in_realm = 2 } # Must not be the last province held
	}

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

		FROM = { capital_scope = { NOT = { province = PREVPREV } } } # Can't be used on the capital province of FROM
		
		NOR = {
			ROOT = {
				has_landed_title = PREV
			}
			holder_scope = {
				is_liege_or_above = ROOT
			}
		}
		
		location = {
			any_neighbor_province = {
				owner = {
					OR = {
						character = ROOT
						is_liege_or_above = ROOT
					}
				}
			}
		}
	}

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

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
	}
	on_success_title = {
		if = {
			limit = {
				holder_scope = {
					tier = count
					NOT = { num_of_count_titles = 2 }
					lower_tier_than = ROOT
				}
			}

			holder_scope = {
				set_defacto_liege = ROOT
			}
		}

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

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

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

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

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

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = { 
		factor = 1
		
		modifier = {
			factor = 0.7 # Prefer using CB's without on_add costs
		}
		modifier = {
			factor = 0
			ROOT = {
				independent = no
				same_liege = FROM
			}
		}
		modifier = {
			factor = 0
			ROOT = {
				higher_tier_than = DUKE
				num_of_count_titles_in_realm = 10
			}
		}
		modifier = {
			factor = 0
			ROOT = {
				OR = {
					trait = just
					trait = content
				}
			}
		}
		modifier = {
			factor = 0
			ROOT = {
				NOR = {
					trait = arbitrary
					trait = ambitious
				}
			}
		}
		modifier = {
			factor = 0
			ROOT = {
				has_character_flag = is_temujin
				has_landed_title = e_mongol_empire
				demesne_size = 1
			}
			FROM = {
				has_landed_title = e_china_west_governor
			}
		}
	}
}

#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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}
	
	on_success = {
		any_attacker = {
			participation_scaled_prestige = 100
			hidden_tooltip = {
				disband_event_forces = faction_revolters
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_success_title = { #This is the target title of the faction
		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 }
						}
					}
				}
				usurp_title = {
					target = PREVPREV
					type = faction_demand
				}
				save_event_target_as = new_title_holder
				hidden_tooltip = {
					any_attacker = {
						limit = { NOT = { character = PREV } }
						set_character_flag = helped_win_war
					}
				}
				character_event = { id = 8226 days = 1 }
			}
		}
	}

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

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

### CHINA CBs 2.8
offmap_submission = {
	name = CB_NAME_OFFMAP_SUBMISSION
	war_name = WAR_NAME_OFFMAP_SUBMISSION
	sprite = 30
	truce_days = 365
	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no
	allow_whitepeace = no
	
	defender_unoccupied_warscore = yes
	
	can_use = {
		ROOT = {
			independent = yes
			NOT = { same_realm = FROM }
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}
		
		FROM = {
			in_revolt = no
			is_nomadic = no # Nomads cannot be subjugated
		}
	}

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		FROM = {
			piety = -100
			prestige = -100
			
			custom_tooltip = {
				text = offmap_submission_tip
			}
			
			if = { # Remove Raid China if active
				limit = {
					has_character_modifier = china_raid_active
				}
				remove_character_modifier = china_raid_active
				hidden_effect = {
					add_character_modifier = {
						name = china_raid_grace_cd
						hidden = yes
						years = 5
					}
				}
			}
				
			hidden_tooltip = {
				ROOT = {
					if = {
						limit = {
							num_of_count_titles_in_realm = 0
							ROOT_FROM = {
								num_of_count_titles_in_realm < 2
							}
						}
						ROOT_FROM = {
							any_realm_province = {
								county = {
									usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
								}
							}
						}
					}
					while = {
						count = 20 # We had an infinite loop at one point. It's been fixed, but safety first
						limit = {
							demesne_size_compared_to_limit < 1
							ROOT_FROM = {
								num_of_count_titles_in_realm > 1
							}
						}
						ROOT_FROM = {
							if = { # Take counties that border each other
								limit = {
									any_realm_title = {
										tier = COUNT
										ROOT_FROM = {
											NOT = {
												capital_scope = {
													title = PREVPREV
												}
											}
										}
										location = {
											any_neighbor_province = {
												holder_scope = {
													character = ROOT
												}
											}
										}
									}
								}
								random_realm_province = {
									limit = {
										ROOT_FROM = {
											NOT = {
												capital_scope = {
													title = PREVPREV
												}
											}
										}
										any_neighbor_province = {
											holder_scope = {
												character = ROOT
											}
										}
									}
									county = {
										usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
									}
								}
							}
							else = {
								random_realm_province = {
									limit = {
										ROOT_FROM = {
											NOT = {
												capital_scope = {
													title = PREVPREV
												}
											}
										}
									}
									county = {
										usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
									}
								} 
							}
						}
					}
					if = { #China with no land calculates what to do with nomad provinces
						limit = { 
							is_offmap_governor = yes
							offmap_power = { is_offmap_tag = offmap_china }
							offmap_china = { has_offmap_tmp_flag = WP_is_not_on_map }
							NOT = { wealth = 2500 } #unless already rich from tributes
						}
						set_variable = { which = "WP_nomad_provinces" value = 0 }
						any_realm_province = {
							limit = { 
								has_province_modifier = nomad_agitation
								NOT = { has_settlement_construction = yes }
							}
							province_event = { id = JD.60100 } #increase counter for each nomad province
						}
						character_event = { id = JD.60101 days = 1 } #calculate gold sent from china for building holdings
					}
					if = { #New Empire
						limit = { has_character_flag = is_rebel_general }
						set_global_flag = rebel_general_invasion_success
						primary_title = { 
							set_title_landless = { title = THIS status = no }
							set_title_flag = uses_temple_names_by_script #Titles with this flag give their heirs a temple name via on_death event
							set_title_flag = pretender_chinese_empire #Blocks all grace interactions
							set_title_flag = rebel_general_title
							adjective = e_china_west_governor_adj #Use WP adj as it is "Chinese" but also gives the right one in different languages
							random_list = { #Random Empire name that China doesn't have
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = tang_china } } }
									set_name = "Tang Empire"
									set_title_flag = china_name_Tang
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = jin_china } } }
									set_name = "Jin Empire"
									set_title_flag = china_name_Jin
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = wei_china } } }
									set_name = "Wei Empire"
									set_title_flag = china_name_Wei
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = qi_china } } }
									set_name = "Qi Empire"
									set_title_flag = china_name_Qi
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = zhou_china } } }
									set_name = "Zhou Empire"
									set_title_flag = china_name_Zhou
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = han_china } } }
									set_name = "Han Empire"
									set_title_flag = china_name_Han
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = qin_china } } }
									set_name = "Qin Empire"
									set_title_flag = china_name_Qin
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = yan_china } } }
									set_name = "Yan Empire"
									set_title_flag = china_name_Yan
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = zhao_china } } }
									set_name = "Zhao Empire"
									set_title_flag = china_name_Zhao
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = liao_china } } }
									set_name = "Liao Empire"
									set_title_flag = china_name_Liao
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = yuan_china } } }
									set_name = "Yuan Empire"
									set_title_flag = china_name_Yuan
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = xia_china } } }
									set_name = "Xia Empire"
									set_title_flag = china_name_Xia
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = qing_china } } }
									set_name = "Qing Empire"
									set_title_flag = china_name_Qing
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = dai_china } } }
									set_name = "Dai Empire"
									set_title_flag = china_name_Dai
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = cheng_china } } }
									set_name = "Cheng Empire"
									set_title_flag = china_name_Cheng
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = liang_china } } }
									set_name = "Liang Empire"
									set_title_flag = china_name_Liang
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = song_china } } }
									set_name = "Song Empire"
									set_title_flag = china_name_Song
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = ming_china } } }
									set_name = "Ming Empire"
									set_title_flag = china_name_Ming
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = shu_china } } }
									set_name = "Shu Empire"
									set_title_flag = china_name_Shu
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = wu_china } } }
									set_name = "Wu Empire"
									set_title_flag = china_name_Wu
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = chu_china } } }
									set_name = "Chu Empire"
									set_title_flag = china_name_Chu
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = yue_china } } }
									set_name = "Yue Empire"
									set_title_flag = china_name_Yue
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = yin_china } } }
									set_name = "Yin Empire"
									set_title_flag = china_name_Yin
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = shun_china } } }
									set_name = "Shun Empire"
									set_title_flag = china_name_Shun
								}
							}
							add_law = {
								law = succ_primogeniture
								cooldown = no
								opinion_effect = no
							}
						}
						set_government_type = chinese_imperial_government
						random_list = { #Give a temple name
							1 = { set_name = Gaozu }
							1 = { set_name = Taizu }
							1 = { set_name = Shizu }
							1 = { set_name = Zhaozu }
							1 = { set_name = Jingzu }
							1 = { set_name = Xianzu }
							1 = { set_name = Liezu }
							1 = { set_name = Chengzu }
						}
						set_character_flag = given_temple_name_by_script
						remove_special_character_title = TITLE_GENERAL
						#Some starting stuff
						wealth = 1000
						prestige = 500
						piety = 200
						offmap_china = { set_offmap_flag = no_war_news }
						any_player = {
							limit = {
								has_offmap_news_enabled = offmap_china
								is_within_diplo_range = PREV
							}
							narrative_event = { id = JD.60211 days = 1 }
						}
					}
				}
				if = {
					limit = {
						NOT = {
							any_demesne_title = {
								tier = EMPEROR
							}
						}
					}
					set_defacto_liege = ROOT
					if = {
						limit = {
							ROOT = { has_character_flag = is_rebel_general }
						}
						any_demesne_title = {
							limit = {
								tier = KING
							}
							usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
							add_law = {
								law = succ_primogeniture
								cooldown = no
								opinion_effect = no
							}
						}
					}
					break = yes
				}
				any_vassal = {
					set_defacto_liege = ROOT
					if = {
						limit = { ROOT = { has_character_flag = is_rebel_general } }
						opinion = {
							modifier = opinion_subjugated 
							multiplier = 2
							who = ROOT
						}
					}
				}
				any_demesne_title = {
					limit = {
						tier = EMPEROR
						NOT = { controls_religion = yes }
					}
					destroy_landed_title = THIS
				}
				if = {
					limit = {
						NOT = {
							tier = EMPEROR
						}
					}
					set_defacto_liege = ROOT
					break = yes
				}
				any_demesne_title = {
					limit = {
						NOT = { controls_religion = yes }
					}
					ROOT = {
						grant_title_no_opinion = { target = PREV type = invasion }
					}
				}
			}
		}
	}
	
	on_fail = {
		FROM = {
			piety = 100
			participation_scaled_prestige = 250
			set_character_flag = defeated_china
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 250 }
		}
		ROOT = {
			piety = -100
			if = {
				limit = { has_character_flag = is_rebel_general }
				hidden_effect = {
					any_courtier = {
						move_character = FROM
						imprison = FROM
					}
					primary_title = {
						activate_title = { title = THIS status = no }
						destroy_landed_title = THIS
					}
					move_character = FROM
					ROOT = { imprison = FROM }
				}
			}
		}
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -500
			transfer_scaled_wealth = {
				to = FROM
				value = 5.0
				min = 1000
			}
			if = {
				limit = { has_character_flag = is_rebel_general }
				hidden_effect = {
					any_courtier = {
						move_character = FROM
						imprison = FROM
					}
					primary_title = {
						activate_title = { title = THIS status = no }
						destroy_landed_title = THIS
					}
					move_character = FROM
					ROOT = { imprison = FROM }
				}
			}
			hidden_effect = {
				if = { #Makes China have a chance of leaving Expanionist policy...
					limit = {
						NOT = {
							has_character_flag = is_rebel_general
						}
						is_offmap_tag = offmap_china
						offmap_china = {
							has_policy = china_expansionist
						}
					}
					random = {
						chance = 99 #TODO_CD_MILLS: change to 50
						offmap_china = {
							set_policy = china_open
						}
						log = "China War Logging: China was forced out of Expansionism"
						if = {
							limit = {
								offmap_china = {
									has_status = china_stable
								}
							}
							random = {
								chance = 30
								offmap_china = {
									set_status = china_unrest
								}
								log = "China War Logging: China was forced into Unrest"
							}
						}
					}
				}
				log = "China War Logging: China lost a submission war against [FROM.GetBestName]"
			}
			pf_glory_minus2_effect = yes
		}
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			set_character_flag = defeated_china
			piety = 500
			if = {
				limit = { 
					has_dlc = "Legacy of Rome"
					OR = { 
						has_landed_title = e_byzantium
						has_landed_title = e_roman_empire
					}
					religion_group = christian
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
			participation_scaled_prestige = 2500
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 1000 }
		}
	}

	on_attacker_leader_death = {
		if = {
			limit = { ROOT = { has_character_flag = is_rebel_general } }
			end_war = invalid
			ROOT = {
				any_courtier = { death = { death_reason = death_went_back_to_china } }
				primary_title = {
					activate_title = { title = THIS status = no }
					destroy_landed_title = THIS
				}
				death = { death_reason = death_went_back_to_china }
			}
		}
	}

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

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

tributary_offmap_cb = {
	name = CB_NAME_PERMANENT_TRIBUTARY
	war_name = WAR_NAME_PERMANENT_TRIBUTARY
	sprite = 30
	truce_days = 365
	hostile_against_others = yes
	can_ask_to_join_war = no
	display_on_map = no
	allowed_to_target_tributaries = no
	
	defender_unoccupied_warscore = yes
	
	can_use = {
		ROOT = {
			independent = yes
			NOT = { same_realm = FROM }
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}
		
		FROM = {
			independent = yes
			in_revolt = no
		}
	}

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

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		log = "---------------------"
		log = "China Logging:"
		log = "The [offmap_china.GetShortName] invasion of [From.GetTitledName] has ended"
		log = "The war ended in a victory for China"
		log = "[From.GetTitledName] is now tributary of China"
		log = "---------------------"
		FROM = {
			save_event_target_as = target_loser
			prestige = -100
			custom_tooltip = {
				text = "REMOVE_PREVIOUS_SUZERAIN"
				hidden_tooltip = {
					any_suzerain = {
						event_target:target_loser = {
							remove_tributary = PREV
						}
					}
				}
			}
			if = { # Remove Raid China if active
				limit = {
					has_character_modifier = china_raid_active
				}
				remove_character_modifier = china_raid_active
				hidden_effect = {
					add_character_modifier = {
						name = china_raid_grace_cd
						hidden = yes
						years = 5
					}
				}
			}
		}

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

	on_fail = {
		log = "---------------------"
		log = "China Logging:"
		log = "The [offmap_china.GetShortName] invasion of [From.GetTitledName] has ended"
		log = "The war ended in a white peace"
		log = "---------------------"
		ROOT = {
			prestige = -200
		}
		FROM = {
			prestige = 200
			set_character_flag = defeated_china
		}
	}

	on_reverse_demand = {
		log = "---------------------"
		log = "China Logging:"
		log = "The [offmap_china.GetShortName] invasion of [From.GetTitledName] has ended"
		log = "The war ended in a victory for [From.GetTitledName]"
		log = "---------------------"
		ROOT = {
			prestige = -500
			transfer_scaled_wealth = {
				to = FROM
				value = 5.0
				min = 1000
			}
			pf_glory_minus2_effect = yes
			hidden_effect = {
				if = { #Makes China have a chance of leaving Expansionist policy...
					limit = {
						is_offmap_tag = offmap_china
						offmap_china = {
							has_policy = china_expansionist
						}
					}
					random = {
						chance = 50
						offmap_china = {
							set_policy = china_open
						}
					}
				}
				log = "China War Logging: China lost a I'll-make-you-my-tributary war against [From.GetBestName]"
			}
		}

		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			prestige = 500
			participation_scaled_prestige = 2500
			set_character_flag = defeated_china
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 1000 }
		}
	}
	
	on_attacker_leader_death = {
		#end_war = invalid
	}

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

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

	ai_will_do = {
		factor = 0
	}
}

dismantle_pretender_china_cb = {
	name = CB_NAME_DISMANTLE_PRETENDER_CHINA
	war_name = WAR_NAME_DISMANTLE_PRETENDER_CHINA
	sprite = 30
	truce_days = 365
	hostile_against_others = yes
	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no
	allow_whitepeace = no
	
	defender_unoccupied_warscore = yes
	
	can_use = {
		ROOT = {
			independent = yes
			NOT = { same_realm = FROM }
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}
		
		FROM = {
			primary_title = { has_title_flag = rebel_general_title }
		}
	}

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		log = "---------------------"
		log = "China Logging:"
		log = "The [offmap_china.GetShortName] dismantle pretender on [From.GetTitledName] has ended"
		log = "The war ended in a victory for China"
		log = "[From.GetTitledName] is now dismantled of China"
		log = "---------------------"
		FROM = {
			custom_tooltip = { 
				text = dismantle_pretender_china_success_tt
				hidden_effect = {
					offmap_china = { set_offmap_flag = no_war_news }
					any_player = {
						limit = {
							NOT = { character = ROOT_FROM }
							OR = {
								AND = {
									has_offmap_news_enabled = offmap_china
									is_within_diplo_range = ROOT
								}
								liege = { character = ROOT_FROM }
							}
						}
						narrative_event = { id = JD.60209 }
					}
					any_courtier = { death = { death_reason = death_executed_by_chinese_emperor } }
					primary_title = {
						activate_title = { title = THIS status = no }
						destroy_landed_title = THIS
					}
					death = { death_reason = death_executed_by_chinese_emperor }
				}
			}
		}
		ROOT = {
			prestige = 200
		}
	}

	on_fail = {
		log = "---------------------"
		log = "China Logging:"
		log = "The [offmap_china.GetShortName] dismantle pretender on [From.GetTitledName] has ended"
		log = "The war ended in a white peace for China"
		log = "---------------------"
		FROM = {
			piety = 100
			participation_scaled_prestige = 250
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 250 }
		}
		ROOT = {
			piety = -100
		}
	}

	on_reverse_demand = {
		log = "---------------------"
		log = "China Logging:"
		log = "The [offmap_china.GetShortName] dismantle pretender on [From.GetTitledName] has ended"
		log = "The war ended in a defeat for China"
		log = "---------------------"
		ROOT = {
			piety = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 5.0
				min = 500
			}
			offmap_power = { set_offmap_tmp_flag = pretender_chinese_empire_war_cooldown }
			pf_glory_minus2_effect = yes
		}
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			piety = 200
			participation_scaled_prestige = 1000
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 1000 }
		}
	}
	
	on_attacker_leader_death = {
		log = "---------------------"
		log = "China Logging:"
		log = "The [offmap_china.GetShortName] dismantle pretender on [From.GetTitledName] has ended"
		log = "The war was invalidated for China"
		log = "---------------------"
		#end_war = invalid
	}

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

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

	ai_will_do = {
		factor = 0
	}
}

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

	is_valid = {
		offmap_china = { has_policy = china_isolationist }
	}

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
		}
		FROM = {
			save_event_target_as = target_governor
			offmap_power = { 
				set_offmap_flag = no_policy_news
				set_policy = china_open
		        log = "--------------------------------"
				log = "China Policy Logging:"
				log = "Chinas new Policy is now OPEN"
				log = "-------------------------------------"
				set_offmap_tmp_flag = china_forced_open
				set_offmap_flag = no_war_news
		        custom_tooltip = { text = china_forced_open_tt }
				hidden_effect = {
					any_player = {
						limit = {
							has_offmap_news_enabled = PREV
							is_within_diplo_range = PREV
						}
						narrative_event = { id = JD.60302 }
					}
					clr_offmap_flag = no_policy_news
				}
				clr_offmap_tmp_flag = china_force_open_in_progress
			}
		}
		ROOT = {
			participation_scaled_prestige = 1000
			add_grace_massive_effect = yes
			add_character_modifier = {
				modifier = chinese_favored_in_trade
				inherit = yes
				years = 25
			}
		}
		FROM = {
			transfer_scaled_wealth = {
				to = ROOT
				value = 5.0
				min = 2000
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 300 }
		}
	}

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

	on_reverse_demand = {
		ROOT = {
			prestige = -300
			transfer_scaled_wealth = {
				to = FROM
				value = 5.0
				min = 500
			}
			add_character_modifier = {
				modifier = angered_china_modifier
				inherit = yes
				years = 50
			}
			pf_glory_minus2_effect = yes
		}
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			piety = 200
			participation_scaled_prestige = 100
			hidden_effect = {
				offmap_power = {
					set_offmap_flag = no_war_news
					any_player = {
						limit = {
							has_offmap_news_enabled = PREV
							is_within_diplo_range = PREV
						}
						narrative_event = { id = JD.60303 }
					}
					clr_offmap_tmp_flag = china_force_open_in_progress
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}
	
	on_attacker_leader_death = {
		#end_war = invalid
	}

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

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

	ai_will_do = {
		factor = 0
	}
}

destabilization_china_cb = {
	name = CB_NAME_DESTABILIZATION_CHINA
	war_name = WAR_NAME_DESTABILIZATION_CHINA
	sprite = 30
	truce_days = 365
	hostile_against_others = yes
	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no
	allow_whitepeace = no
	
	defender_unoccupied_warscore = yes
	
	can_use = {
		ROOT = {
			independent = yes
			NOT = { same_realm = FROM }
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}
	}

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

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

	on_reverse_demand = {
		ROOT = {
			piety = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 5.0
				min = 1000
			}
			pf_glory_minus2_effect = yes
		}
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			piety = 500
			participation_scaled_prestige = 2500
			set_character_flag = defeated_china
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 1000 }
		}
	}
	
	on_attacker_leader_death = {
		#end_war = invalid
	}

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

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

	ai_will_do = {
		factor = 0
	}
}

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

	# attacker_unoccupied_warscore = yes - If attacker completely unoccupied, attacker will get ticking warscore. Only works if no title is targetted
	# defender_unoccupied_warscore = yes - If defender completely unoccupied, defender will get ticking warscore. Only works if no title is targetted
	
	can_use = {
		ROOT = {
			independent = yes
			NOT = { same_realm = FROM }
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}
		FROM = {
			is_offmap_governor = yes
			offmap_power = { is_offmap_tag = offmap_china }
		}
	}

	is_valid = { event_target:invasion_of_china_claimant = { is_alive = yes } }

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			show_scope_change = no
			custom_tooltip = {
				text = invasion_of_china_success_tt
				hidden_effect = {
					event_target:invasion_of_china_claimant = {
						betrothed = { break_betrothal = PREV }
						spouse = {
							if = {
								limit = {
									is_ruler = no
									is_primary_heir = no
								}
								leave_court_for_china_effect = yes
							}
						}
						any_child = {
							if = {
								limit = {
									is_ruler = no
									is_primary_heir = no
								}
								leave_court_for_china_effect = yes
							}
						}
						clr_character_flag = no_court_invites
						remove_trait = cannot_marry
						diplomatic_immunity = no
					}
					offmap_china = {
						governor = {
							any_vassal = {
								set_defacto_liege = ROOT
							}
							any_demesne_title = {
								limit = {
									NOT = {
										tier = EMPEROR
									}
								}
								grant_title = ROOT
							}
						}
						ROOT = {
							if = {
								limit = { government = chinese_imperial_government }
								offmap_china = { set_offmap_flag = rename_override }
								primary_title = {
									trigger_switch = {
										on_trigger = has_title_flag
										china_name_Tang = { offmap_china = { set_offmap_name = "tang_china" } }
										china_name_Jin = { offmap_china = { set_offmap_name = "jin_china" } }
										china_name_Wei = { offmap_china = { set_offmap_name = "wei_china" } }
										china_name_Qi = { offmap_china = { set_offmap_name = "qi_china" } }
										china_name_Zhou = { offmap_china = { set_offmap_name = "zhou_china" } }
										china_name_Han = { offmap_china = { set_offmap_name = "han_china" } }
										china_name_Qin = { offmap_china = { set_offmap_name = "qin_china" } }
										china_name_Yan = { offmap_china = { set_offmap_name = "yan_china" } }
										china_name_Zhao = { offmap_china = { set_offmap_name = "zhao_china" } }
										china_name_Liao = { offmap_china = { set_offmap_name = "liao_china" } }
										china_name_Yuan = { offmap_china = { set_offmap_name = "yuan_china" } }
										china_name_Xia = { offmap_china = { set_offmap_name = "xia_china" } }
										china_name_Qing = { offmap_china = { set_offmap_name = "qing_china" } }
										china_name_Dai = { offmap_china = { set_offmap_name = "dai_china" } }
										china_name_Cheng = { offmap_china = { set_offmap_name = "cheng_china" } }
										china_name_Liang = { offmap_china = { set_offmap_name = "liang_china" } }
										china_name_Song = { offmap_china = { set_offmap_name = "song_china" } }
										china_name_Ming = { offmap_china = { set_offmap_name = "ming_china" } }
										china_name_Shu = { offmap_china = { set_offmap_name = "shu_china" } }
										china_name_Wu = { offmap_china = { set_offmap_name = "wu_china" } }
										china_name_Chu = { offmap_china = { set_offmap_name = "chu_china" } }
										china_name_Yue = { offmap_china = { set_offmap_name = "yue_china" } }
										china_name_Yin = { offmap_china = { set_offmap_name = "yin_china" } }
										china_name_Shun = { offmap_china = { set_offmap_name = "shun_china" } }
									}
									clr_title_flag = pretender_chinese_empire
									clr_title_flag = uses_temple_names_by_script
									clear_flags_with_prefix = temple_name_
									clear_flags_with_prefix = china_name_
									set_name = "[Root.Capital.GetName] Empire"
								}
								clr_character_flag = given_temple_name_by_script
								set_name = ""
								if = {
									limit = {
										religion_group = muslim
									}
									set_government_type = muslim_government
								}
								else_if = {
									limit = {
										culture_group = tibetan_group
										OR = {
											religion = bon
											religion = buddhist
										}
										higher_tier_than = BARON
									}
									set_government_type = theocratic_feudal_government
								}
								else = {
									set_government_type = feudal_government
								}
							}
						}
						set_offmap_flag = no_succession_news
						set_offmap_flag = no_war_news
						clr_offmap_flag = china_invaded_player_dynasty
						set_offmap_flag = china_invaded_player_dynasty
						set_offmap_holder = event_target:invasion_of_china_claimant
						clr_offmap_flag = no_succession_news
						clr_offmap_flag = rename_override
					}
					any_player = {
						limit = {
							OR = {
								AND = {
									has_offmap_news_enabled = offmap_china
									is_within_diplo_range = FROM
								}
								liege = { character = ROOT }
							}
						}
						narrative_event = { id = JD.60401 }
					}
				}
			}
			participation_scaled_prestige = 5000
			set_offmap_currency = { offmap = offmap_china value = 5000 }
			wealth = 10000
			add_character_modifier = { name = chinese_favored_in_trade inherit = yes years = 50 }
			if = {
				limit = { NOT = { has_artifact jade_dragon } }
				add_artifact = jade_dragon
			}
			if = {
				limit = { NOT = { has_artifact chinese_dragon_amulet } }
				add_artifact = chinese_dragon_amulet
			}
			if = {
				limit = { NOT = { has_artifact water_clock } }
				add_artifact = water_clock
			}
			if = {
				limit = { NOT = { has_artifact jian_sword } }
				add_artifact = jian_sword
			}
			hidden_effect = { add_character_modifier = { name = peace_deal_with_china inherit = yes years = 50 } }
			remove_character_modifier = war_with_china_modifier
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 2000 }
		}
		clear_global_event_target = invasion_of_china_claimant
	}

	on_invalidation = {
		FROM = {
			piety = 200
			participation_scaled_prestige = 250
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 250 }
		}
		ROOT = {
			prestige = -2500
			remove_character_modifier = war_with_china_modifier
			custom_tooltip = {
				text = invasion_of_china_cb_fail_tt
				hidden_effect = { add_character_modifier = { name = failed_invasion_of_china duration = -1 hidden = yes } }
			}
			hidden_effect = {
				offmap_china = { set_offmap_flag = no_war_news }
				any_player = {
					limit = {
						OR = {
							AND = {
								has_offmap_news_enabled = offmap_china
								is_within_diplo_range = FROM
							}
							liege = { character = ROOT }
						}
					}
					narrative_event = { id = JD.60403 }
				}
			}
			add_character_modifier = {
				modifier = angered_china_modifier
				inherit = yes
				years = 50
			}
		}
		clear_global_event_target = invasion_of_china_claimant
	}

	on_reverse_demand = {
		ROOT = {
			prestige = -3000
			transfer_scaled_wealth = {
				to = FROM
				value = 5.0
				min = 5000
			}
			pf_glory_minus2_effect = yes
			remove_character_modifier = war_with_china_modifier
			custom_tooltip = {
				text = invasion_of_china_cb_fail_tt
				hidden_effect = { add_character_modifier = { name = failed_invasion_of_china duration = -1 hidden = yes } }
			}
			custom_tooltip = {
				text = destabilization_china_success_tt
				hidden_effect = { 
					offmap_china = { set_offmap_flag = no_war_news }
					any_player = {
						limit = {
							OR = {
								AND = {
									has_offmap_news_enabled = offmap_china
									is_within_diplo_range = FROM
								}
								liege = { character = ROOT }
							}
						}
						narrative_event = { id = JD.60402 }
					}
					any_demesne_title = {
						limit = {
							higher_tier_than = DUKE
							NOR = {
								controls_religion = yes
								clan = yes
							}
						}
						destroy_landed_title = THIS
					}
				}
			}
			add_character_modifier = {
				modifier = angered_china_modifier
				inherit = yes
				years = 50
			}
		}
		FROM = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			piety = 200
			participation_scaled_prestige = 1000
			set_character_flag = defeated_china
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 1000 }
		}
		clear_global_event_target = invasion_of_china_claimant
	}
	
	on_attacker_leader_death = {
		#end_war = invalid
	}

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

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

	ai_will_do = {
		factor = 0
	}
}

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

#Plus had this CB before JD, so this will not be DLC locked.
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
	
	infamy_modifier = 0.5
	
	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		#check to ensure the target is not valid for a holy war
		NOT = {
			ROOT = {
				OR = {
					NOT = { is_pagan_religion = yes }
					is_reformed_pagan_religion = yes
				}
				OR = {
					NOR = {
						religion = hindu
						religion = buddhist
					}
					FROM = { NOT = { has_dharmic_religion_trigger = yes } }
				}
				OR = {
					NOT = { religion_group = FROM }
					is_heresy_of = FROM
					is_parent_religion = FROM
					AND = {
						OR = {
							religion_group = muslim
							is_pagan_religion = yes
							religion = paulician
						}
						NOT = { religion = FROM }
					}
				}
				NOR = {
					pacifist = yes
					same_realm = FROM
				}
			}
		}
	}

	can_use_title = {
		ROOT = { attacking_religious_head_trigger = no }
		
		# 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_de_jure_vassal_title = {
						tier = COUNT
						owner = {
							NOT = { character = ROOT }
							NOT = { character = FROM }
							NOT = { is_liege_or_above = ROOT } 
							NOT = { is_vassal_or_below = ROOT }
						}
					}
				}
			}
		}
		# 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
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	on_success = {
		ROOT = {
			add_character_modifier = { name = victory_timer duration = 1 hidden = yes }
			pf_glory_plus2_effect = yes
			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
		}
		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
				is_pagan_religion = yes
			}
			FROM = {
				culture = mongol
				is_pagan_religion = yes
			}
		}
		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
		}
		modifier = {
			factor = 0
			ROOT = {
				has_character_flag = is_temujin
				has_landed_title = e_mongol_empire
				demesne_size = 1
			}
			FROM = {
				has_landed_title = e_china_west_governor
			}
		}
	}
}

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
	
	infamy_modifier = 0.25
	
	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		ROOT = { attacking_religious_head_trigger = no }
		
		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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}
	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_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
	
	infamy_modifier = 0.25

	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		# target is not the same religion as any liege
		NOT = {
			any_liege = { religion = FROM}
		}
		# 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
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	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

	can_use_title = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		# 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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}
	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_CITY_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
	
	allow_distant = yes # AI parameter
	
	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		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
			has_trade_post = yes
			trade_post_owner = {
				character = ROOT
			}

			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 = 650	#  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 = 650	#  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 = { attacking_religious_head_trigger = no }
	}

	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
			}
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_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 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 = 650	#  From  aprox.Venice to East Cyprus and from Venice to Gibraltar
								}
							}	
						}
					}			
				}
			}
			custom_tooltip = {
				text = CTT_REP_IN_RANGE
			}
		}
		else = {
			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
				}
			}
		}
		
		ROOT = {
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_fail = {
		ROOT = {
			prestige = -100
		}
		FROM = {
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		
		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
		}
		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
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_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 } }		
		
		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
			if = {
				limit = {
					NOT = { has_character_flag = no_whitepeace_prison }
				}
				imprison = FROM
			}
			clr_character_flag = no_whitepeace_prison
		}
		
		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 }
		
		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
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_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 } }
		
		# 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 = {
					move = no
				}
			}
		}

		# 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
		if = {
			limit = {
				NOT = { has_character_flag = no_whitepeace_prison }
			}
			imprison = FROM
		}
		clr_character_flag = no_whitepeace_prison
		
		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
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_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

	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		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 }
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	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
	
	infamy_modifier = 0.75

	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	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
	
	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 = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		ROOT = { attacking_religious_head_trigger = no }
		
		# 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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}
  
	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_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_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
	
	infamy_modifier = 0.75

	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	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
	
	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 = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		#check to ensure the target is a valid for a holy war
		ROOT = {
			OR = {
				NOT = { is_pagan_religion = yes }
				is_reformed_pagan_religion = yes
			}
			OR = {
				NOR = {
					religion = hindu
					religion = buddhist
				}
				FROM = { NOT = { has_dharmic_religion_trigger = yes } }
			}
			OR = {
				NOT = { religion_group = FROM }
				is_heresy_of = FROM
				is_parent_religion = FROM
				AND = {
					OR = {
						religion_group = muslim
						is_pagan_religion = yes
						religion = paulician
					}
					NOT = { religion = FROM }
				}
			}
			OR = {
				has_secret_religion = no
				AND = {
					has_secret_religion = yes
					OR = {
						NOT = { secret_religion_group = FROM }
						AND = {
							OR = {
								secret_religion_group = muslim
								secret_is_pagan_religion = yes
								secret_religion = paulician
							}
							NOT = { secret_religion = FROM }
						}
					}
				}
			}
			
			NOR = {
				pacifist = yes
				same_realm = FROM
			}
		}
	}

	can_use_title = {
		# 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
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_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
				is_pagan_religion = yes
			}
			FROM = {
				culture = mongol
				is_pagan_religion = yes
			}
		}
		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
	
	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 = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		#check to ensure the target is a valid for a holy war
		ROOT = {
			OR = {
				NOT = { is_pagan_religion = yes }
				is_reformed_pagan_religion = yes
			}
			OR = {
				NOR = {
					religion = hindu
					religion = buddhist
				}
				FROM = { NOT = { has_dharmic_religion_trigger = yes } }
			}
			OR = {
				NOT = { religion_group = FROM }
				is_heresy_of = FROM
				is_parent_religion = FROM
				AND = {
					OR = {
						religion_group = muslim
						is_pagan_religion = yes
						religion = paulician
					}
					NOT = { religion = FROM }
				}
			}
			OR = {
				has_secret_religion = no
				AND = {
					has_secret_religion = yes
					OR = {
						NOT = { secret_religion_group = FROM }
						AND = {
							OR = {
								secret_religion_group = muslim
								secret_is_pagan_religion = yes
								secret_religion = paulician
							}
							NOT = { secret_religion = FROM }
						}
					}
				}
			}
			NOR = {
				pacifist = yes
				same_realm = FROM
			}
		}
	}
	
	can_use_title = {
		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
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_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
				is_pagan_religion = yes
			}
			FROM = {
				culture = mongol
				is_pagan_religion = yes
			}
		}
		modifier = { # The AI will never breach a tributary's trust
			factor = 0
			ai_attack_tributary_trigger = yes
		}
		modifier = {
			factor = 0 # AIs will not take the last counties off their liege's direct vassals
			ROOT = {
				same_liege = FROM
			}
			FROM = {
				NOT = {
					any_demesne_title = {
						tier = count
						NOT = { title = PREVPREV }
					}
				}
			}
		}
	}
}

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
	
	infamy_modifier = 0.5
	
	can_use_gui = {
		ROOT = {
			top_liege = { religion_group = PREV }
			OR = {
				is_patrician = no
				independent = yes
			}
		}
	}

	can_use = {
		cb_no_nap_with_suzerain_trigger = yes
		#check to ensure the target is a valid for a holy war
		ROOT = {
			OR = {
				NOT = { is_pagan_religion = yes }
				is_reformed_pagan_religion = yes
			}
			OR = {
				NOR = {
					religion = hindu
					religion = buddhist
				}
				FROM = { NOT = { has_dharmic_religion_trigger = yes } }
			}
			OR = {
				NOT = { religion_group = FROM }
				is_heresy_of = FROM
				is_parent_religion = FROM
				AND = {
					OR = {
						religion_group = muslim
						is_pagan_religion = yes
						religion = paulician
					}
					NOT = { religion = FROM }
				}
			}
			OR = {
				has_secret_religion = no
				AND = {
					has_secret_religion = yes
					OR = {
						NOT = { secret_religion_group = FROM }
						AND = {
							OR = {
								secret_religion_group = muslim
								secret_is_pagan_religion = yes
								secret_religion = paulician
							}
							NOT = { secret_religion = FROM }
						}
					}
				}
			}
			NOR = {
				pacifist = yes
				same_realm = FROM
			}
		}
		
		cb_can_use_preamble = yes
		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
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_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
	
	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 = {
		cb_can_use_preamble = yes
		cb_other_no_nap_with_suzerain_trigger = yes
		#check to ensure the target is a valid for a holy war
		OR = {
			NOT = { is_pagan_religion = yes }
			is_reformed_pagan_religion = yes
		}
		OR = {
			NOR = {
				religion = hindu
				religion = buddhist
			}
			FROM = { NOT = { has_dharmic_religion_trigger = yes } }
		}
		OR = {
			NOT = { religion_group = FROM }
			is_heresy_of = FROM
			is_parent_religion = FROM
			AND = {
				OR = {
					religion_group = muslim
					is_pagan_religion = yes
					religion = paulician
				}
				NOT = { religion = FROM }
			}
		}
		OR = {
			has_secret_religion = no
			AND = {
				has_secret_religion = yes
				OR = {
					NOT = { secret_religion_group = FROM }
					AND = {
						OR = {
							secret_religion_group = muslim
							secret_is_pagan_religion = yes
							secret_religion = paulician
						}
						NOT = { secret_religion = FROM }
					}
				}
			}
		}
		NOR = {
			pacifist = yes
			same_realm = FROM
		}

		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
			}
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	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
				is_pagan_religion = yes
			}
			FROM = {
				culture = mongol
				is_pagan_religion = yes
			}
		}
		modifier = { # The AI will never breach a tributary's trust
			factor = 0
			ai_attack_tributary_trigger = yes
		}
		modifier = {
			factor = 0 # AIs will not take the last counties off their liege's direct vassals
			ROOT = {
				same_liege = FROM
			}
			FROM = {
				NOT = {
					any_demesne_title = {
						tier = count
						NOT = { title = PREVPREV }
					}
				}
			}
		}
	}
}

# 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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	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

	can_use_gui = {
		ROOT = {
			has_secret_religion = no
			OR = {
				trait = druze_ajawid
				piety = 1000
				AND = {
					martial = 12
					diplomacy = 12
				}
			}
		}
	}
	
	can_use = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	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
	
	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 = {
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		FROM = { NOT = { 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
			has_secret_religion = 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 = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}
	
	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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}
	
	on_success = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with their resist_viceroy_revocation_cb against [From.GetBestName] (ID: [From.GetID])" 
		ROOT = {
			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 } } }
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	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 = roman_pagan
								}
							}
						}
						OR = {
							religion_group = christian
							is_pagan_religion = yes
						}
					}
					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 = roman_pagan
							}
						}
					}
					OR = {
						religion_group = christian
						is_pagan_religion = yes
					}
				}
				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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}
	
	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 tributary_type = forced }
		}
		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

	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_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	on_success = {
		any_attacker = {
			limit = { 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
	
	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
		}
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	on_success = {
		hidden_tooltip = {
			ROOT = { 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
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_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
	
	can_use_gui = {
		ROOT = {
			is_patrician = no
		}
	}
	
	can_use = {
		has_global_flag = emf_rapid_conquest
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		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])"
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}
	
	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
	
	can_use_gui = {
		ROOT = {
			is_patrician = no
		}
	}
	
	can_use = {
		has_global_flag = emf_rapid_conquest
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		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])"
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	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
	
	can_use_gui = {
		ROOT = {
			is_patrician = no
		}
	}
	
	can_use = {
		has_global_flag = emf_rapid_conquest
		cb_can_use_preamble = yes
		cb_no_nap_with_suzerain_trigger = yes
		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])"
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}
	
	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

# Free Tributary CB
overpower_imperial_suzerain_cb = {
	name = CB_NAME_OVERPOWER_SUZERAIN
	war_name = WAR_NAME_OVERPOWER_SUZERAIN
	sprite = 25
	truce_days = 365
	is_permanent = yes
	hostile_against_others = yes
	can_ask_to_join_war = no

	can_use = {
		has_dlc = "Horse Lords"
		cb_can_use_preamble = yes
		ROOT = {
			is_tributary = { type = imperial }
			suzerain = {
				character = FROM
			}
		}
	}

	is_valid = {
		ROOT = {
			is_tributary = { type = imperial }
			suzerain = {
				character = FROM
			}
		}
	}
	
	on_add = {
		if = {
			limit = { defender = { is_offmap_governor = offmap_china } }
			attacker = {  
				sound_effect = china_angered_emperor
				detract_grace_super_huge_effect = yes 
			}
		}
	}

	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 = { type = imperial }
					suzerain = {
						character = FROM
					}
				}
				suzerain = {
					remove_tributary = PREV
				}
			}
			make_tributary = { who = FROM tributary_type = imperial }
		}
	}

	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 = 0.1
			FROM = { dynasty = ROOT }
		}
		
		modifier = {
			factor = 2
			OR = {
				ROOT = {
					is_nomadic = yes
				}
				FROM = {
					is_nomadic = no
				}
			}
		}
	}
}
