# -*- ck2.cb_types -*-
# Audax Validator EnableCommentMetadata

claim = {
	name = CB_NAME_CLAIM
	war_name = WAR_NAME_CLAIM
	sprite = 1
	truce_days = 1825
	hostile_against_others = yes
	can_ask_to_join_war = no
	allowed_to_target_tributaries = no
	press_claim = yes
	infamy_modifier = 0.85
	sort_priority = 1000

	can_use_gui = {
		emf_cb_can_use_gui = yes
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			trigger_if = {
				limit = { is_female = yes }
				emf_antifeminist_religion = no
			}
			trigger_else = {
				NOT = { has_religion_feature = religion_matriarchal }
			}
		}
	}

	can_use_title = {
		owner_under_FROM = yes
		OR = {
			ROOT = { has_flag = claimant_adventurer }
			emf_cb_can_press_claim = yes
		}
		NAND = {
			emf_cb_restore_hre_basic_can_use = yes
			emf_cb_restore_hre_basic_can_use_title = yes
		}
		emf_cb_can_use_title = yes
	}

	is_valid_title = {
		owner_under_FROM = yes
		ROOT = { has_claim = PREV }
	}

	on_add = {
		log = "CB(claim): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(claim): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
	}

	on_success_title = {
		emf_cb_latb = yes
		hidden_tooltip = {
			ROOT = {
				if = {
					limit = {
						has_nickname = no
						lower_tier_than = PREV
						NOT = { PREV = { any_previous_holder = { character = ROOT } } }
					}
					random = {
						chance = 25
						give_nickname = nick_the_usurper
					}
				}
			}
		}
		emf_cb_ROOT_ensure_feudal = yes # When claimant has wrong government, take care of that first.
		usurp_title_plus_barony_if_unlanded = {
			target = ROOT
			type = claim
		}
		if = {
			limit = {
				higher_tier_than = BARON
				NOT = { culture = ROOT }
			}
			hidden_tooltip = { conquest_culture = ROOT }
		}
		ROOT = {
			show_scope_change = no
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		FROM = {
			show_scope_change = no
			prestige = -100
		}

		if = {
			limit = {
				ROOT = { has_flag = claimant_adventurer }
			}
			ROOT = {
				clr_flag = claimant_adventurer
				emf_check_adventurer_government = yes
			}
		}

		hidden_tooltip = { check_holy_order_donation_effect = yes }
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(claim): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

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

	on_reverse_demand = {
		log = "CB(claim): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			show_scope_change = no
			prestige = -150
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		if = {
			limit = { ROOT = { has_flag = claimant_adventurer } }
			ROOT = { imprison = FROM }
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 150
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 150 }
		}
		hidden_effect = {
			ROOT = {
				if = {
					limit = {
						is_offmap_governor = offmap_china
					}
					log = "China War Logging: China lost a claim war against [From.GetBestName]"
				}
				if = { #Makes China have a chance of leaving Expanionist policy...
					limit = {
						is_offmap_governor = offmap_china
						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 = {
		ROOT = {
			show_scope_change = no
			remove_claim = PREV
		}
		ROOT = {
			hidden_tooltip = {
				disband_event_forces = claimant_adventure
				clr_flag = claimant_adventurer
			}
		}
	}

	on_attacker_leader_death = {
		if = {
			limit = {
				ROOT = {
					OR = {
						has_flag = claimant_adventurer
						has_flag = EoD_prophet_attacker
					}
				}
			}
			end_war = invalid
		}
	}

	on_invalidation = {
		log = "CB(claim): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		hidden_tooltip = {
			if = {
				limit = { ROOT = { has_flag = claimant_adventurer } }
				ROOT = {
					disband_event_forces = claimant_adventure
					clr_flag = claimant_adventurer
				}
			}
			if = {
				limit = { ROOT = { has_flag = EoD_prophet_attacker } }
				FROM = { narrative_event = { id = RIP.15116 } }
				ROOT = {
					disband_event_forces = EoD_uprising_troops
					clr_flag = EoD_prophet_attacker
				}
			}
		}
	}

	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 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 = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

other_claim = {
	name = CB_NAME_CLAIM
	war_name = WAR_NAME_OTHER_CLAIM
	sprite = 2
	truce_days = 1825
	hostile_against_others = yes
	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no
	press_claim = yes
	infamy_modifier = 0.5
	sort_priority = 900

	can_use_gui = {
		emf_cb_thirdparty_can_use_gui = yes
	}

	can_use_title = {
		FROMFROM = {
			NOT = { character = ROOT }
			mercenary = no
		}
		owner_under_FROM = yes
		trigger_if = {
			limit = { ROOT = { is_female = yes } }
			ROOT = { emf_antifeminist_religion = no }
			NOT = { succ_law_title = { has_law = agnatic_succession } }
		}
		trigger_else = {
			ROOT = { NOT = { has_religion_feature = religion_matriarchal } }
			NOT = { succ_law_title = { has_law = enatic_succession } }
		}
		FROMFROM = {
			OR = {
				religion_group = ROOT
				ROOT = { emf_syncretized_religion_with_PREV = yes }
			}
			OR = {
				ai = no
				ROOT_FROM = { ai = no }
				lower_tier_than = EMPEROR
				is_close_relative = ROOT
				has_non_aggression_pact_with = ROOT
				is_allied_with = ROOT
				is_friend = ROOT
				dynasty = ROOT
				is_married = ROOT
			}
		}
		ROOT = {
			in_revolt = no
			OR = {
				AND = {
					is_ruler = yes
					is_merchant_republic = yes
				}
				is_patrician = no
			}
		}
		OR = { # AI will only press something that will gain it territory, unless target is player
			FROMFROM = { ai = no } # Player pressers can do whatever they want
			# AI can always press an anti-pope's claim...
			title = k_papal_state
			title = d_fraticelli
			emf_cb_other_claimant_will_be_attacker_vassal = yes # We'll gain territory out of it
			# Don't care if we'll gain territory if the target is a player...
			FROM = { ai = no }
			holder_scope = { ai = no }
		}
		NOT = {
			any_war = { # when used in title scope, checks all wars for that title
				attacker = { character = ROOT }
			}
		}
		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
			}
		}
		emf_cb_can_press_claim = yes
		emf_cb_thirdparty_can_use_title = yes
	}

	is_valid_title = {
		owner_under_FROM = yes
		ROOT = { has_claim = PREV }
	}

	on_add = {
		log = "CB(other_claim): START: title=[FromFrom.GetID]: [This.GetTitledFirstName] of [This.PrimaryTitle.GetBaseName] ([This.GetID]/[This.PrimaryTitle.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (#[From.GetID]/[From.PrimaryTitle.GetID]) on behalf of [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (#[Root.GetID]/[Root.PrimaryTitle.GetID])"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(other_claim): VICTORY: title=[FromFrom.GetID]: [This.GetTitledFirstName] of [This.PrimaryTitle.GetBaseName] ([This.GetID]/[This.PrimaryTitle.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (#[From.GetID]/[From.PrimaryTitle.GetID]) on behalf of [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (#[Root.GetID]/[Root.PrimaryTitle.GetID])"
		emf_cb_victory_other = yes
		hidden_tooltip = {
			ROOT = {
				opinion = {
					modifier = pressed_my_claim
					who = PREV
					multiplier = 2
				}
			}
		}
		ROOT = {
			show_scope_change = no
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		FROM = {
			show_scope_change = no
			prestige = -100
		}
	}

	on_success_title = {
		if = {
			limit = { tier = COUNT }
			emf_cb_latb = yes
		}
		hidden_tooltip = {
			ROOT = {
				if = {
					limit = {
						has_nickname = no
						lower_tier_than = PREV
						NOT = { PREV = { any_previous_holder = { character = ROOT } } }
					}
					random = {
						chance = 15
						give_nickname = nick_the_usurper
					}
				}
			}
		}
		# Anti-Popes take all the gold of the Vatican
		if = {
			limit = {
				OR = {
					title = k_papal_state
					title = d_fraticelli
				}
			}
			FROM = {
				transfer_scaled_wealth = {
					to = ROOT
					value = all
				}
			}
		}
		if = {
			limit = {
				controls_religion = yes
				holder_scope = {
					OR = {
						religion = catholic
						religion = fraticelli
					}
				}
			}
			holder_scope = {
				ROOT = { save_event_target_as = emf_abdicate_real_heir }
				emf_willfully_abdicate = yes
			}
			if = {
				limit = { FROMFROM = { higher_tier_than = PREV } }
				ROOT = {
					set_defacto_liege = FROMFROM
					emf_liege_change = yes
				}
			}
		}
		if = {
			limit = {
				NAND = {
					controls_religion = yes
					holder_scope = {
						OR = {
							religion = catholic
							religion = fraticelli
						}
					}
				}
			}
			if = {
				limit = { emf_cb_other_claimant_will_be_attacker_vassal = yes }
				# Claimant will stay in presser's realm
				ROOT = { set_flag = emf_presser_vassal }
				if = {
					limit = {
						ROOT = { # Claimant is vassal ruler of presser
							is_ruler = yes
							is_liege_or_above = FROMFROM
						}
					}
					custom_tooltip = { text = emf_cb_ctt_other_claim_remain_presser_vassal }
				}
				if = {
					limit = {
						NOT = { # Claimant is not yet a vassal ruler of presser
							ROOT = {
								is_ruler = yes
								is_liege_or_above = FROMFROM
							}
						}
					}
					custom_tooltip = { text = emf_cb_ctt_other_claim_become_presser_vassal }
				}
			}
			if = {
				limit = { emf_cb_other_claimant_will_be_attacker_vassal = no }
				custom_tooltip = { text = emf_cb_ctt_other_claim_not_presser_vassal }
				if = {
					limit = {
						holder_scope = {
							liege = {
								NOT = { character = PREV }
								higher_real_tier_than = ROOT
							}
						}
					}
					holder_scope = {
						liege = {
							save_event_target_as = emf_cb_new_liege
							custom_tooltip = { text = emf_cb_ctt_other_claim_new_liege }
						}
					}
				}
				if = {
					limit = {
						NOT = {
							holder_scope = {
								liege = {
									NOT = { character = PREV }
									higher_real_tier_than = ROOT
								}
							}
						}
						FROM = { liege = { NOT = { character = PREV } } }
					}
					FROM = {
						liege = {
							save_event_target_as = emf_cb_new_liege
							custom_tooltip = { text = emf_cb_ctt_other_claim_new_liege }
						}
					}
				}
			}
			emf_cb_ROOT_ensure_feudal = yes # When claimant has wrong government, take care of that first.
			usurp_title_plus_barony_if_unlanded = {
				target = ROOT
				type = claim
			}
			hidden_tooltip = {
				ROOT = { 
					any_child =  { 
						limit = { 
							is_ruler = no 
							is_married = no 
							prisoner = no 
							is_adult = no
							is_within_diplo_range = ROOT 
						} 
						move_character = ROOT 
					}
					any_spouse =  { 
						limit = { 
							is_ruler = no 
							prisoner = no 
							is_within_diplo_range = ROOT 
						} 
						move_character = ROOT 
					}
					any_consort =  { 
						limit = { 
							is_ruler = no 
							prisoner = no 
							is_within_diplo_range = ROOT 
						} 
						move_character = ROOT 
					}
				}
				ROOT = {
					if = {
						limit = { has_flag = emf_presser_vassal }
						if = {
							limit = { NOT = { is_liege_or_above = FROMFROM } }
							FROMFROM = { save_event_target_as = emf_cb_new_imperial_liege }
							emf_set_viceroy_exemption = yes
							set_defacto_liege = FROMFROM
						}
					}
					if = {
						limit = { NOT = { has_flag = emf_presser_vassal } }
						if = {
							limit = { event_target:emf_cb_new_liege = { always = yes } }
							event_target:emf_cb_new_liege = { save_event_target_as = emf_cb_new_imperial_liege }
							emf_set_viceroy_exemption = yes
							set_defacto_liege = event_target:emf_cb_new_liege
						}
						if = {
							limit = { NOT = { event_target:emf_cb_new_liege = { always = yes } } }
							set_defacto_liege = ROOT
						}
					}
					clr_flag = emf_presser_vassal
					emf_liege_change = yes
				}
			}
		}
		if = {
			limit = {
				higher_tier_than = BARON
				NOT = { culture = ROOT }
			}
			hidden_tooltip = { conquest_culture = ROOT }
		}
		hidden_tooltip = { check_holy_order_donation_effect = yes }
	}

	on_success_posttitle = { emf_cb_thirdparty_on_success_posttitle = yes }

	on_fail = {
		log = "CB(other_claim): WHITEPEACE: title=[FromFrom.GetID]: [This.GetTitledFirstName] of [This.PrimaryTitle.GetBaseName] ([This.GetID]/[This.PrimaryTitle.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (#[From.GetID]/[From.PrimaryTitle.GetID]) on behalf of [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (#[Root.GetID]/[Root.PrimaryTitle.GetID])"
	}

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

	on_reverse_demand = {
		log = "CB(other_claim): DEFEAT: title=[FromFrom.GetID]: [This.GetTitledFirstName] of [This.PrimaryTitle.GetBaseName] ([This.GetID]/[This.PrimaryTitle.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (#[From.GetID]/[From.PrimaryTitle.GetID]) on behalf of [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (#[Root.GetID]/[Root.PrimaryTitle.GetID])"
		emf_cb_defeat_other = yes
		prestige = -150
		transfer_scaled_wealth = {
			to = FROM
			value = 4.0
		}
		ROOT = {
			show_scope_change = no
			prestige = -150
			imprison = FROM
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 150
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 150 }
		}
	}

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

	on_invalidation = {
		log = "CB(other_claim): INVALIDATED: [FromFrom.GetTitledFirstName] of [FromFrom.PrimaryTitle.GetBaseName] ([FromFrom.GetID]/[FromFrom.PrimaryTitle.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (#[From.GetID]/[From.PrimaryTitle.GetID]) on behalf of [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (#[Root.GetID]/[Root.PrimaryTitle.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 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 = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

claim_on_liege = {
	name = CB_NAME_CLAIM
	war_name = WAR_NAME_CLAIM_LIEGE
	sprite = 3
	truce_days = 1825
	hostile_against_others = yes
	allowed_to_target_tributaries = no
	press_claim = yes
	can_call_vassals = no
	#attacker_can_call_allies = no
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	infamy_modifier = 0.85
	sort_priority = 910

	can_use = { emf_cb_can_use = yes }

	can_use_title = {
		owner_under_FROM = yes
		OR = {
			ROOT = { has_flag = faction_claimant_ultimatum_taken } # Faction war. Ignore claim strength.
			emf_cb_can_press_claim = yes
		}
		NOR = {
			FROM = {
				is_patrician = yes
				OR = {
					capital_holding = { title = PREVPREV }
					PREV = { is_primary_holder_title_tier = yes }
				}
			}
			ROOT = { is_patrician = yes }
		}
		emf_cb_can_use_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
					}
				}
			}
		}
	}

	is_valid_title = {
		NOT = { holder = ROOT }
		owner_under_FROM = yes
		OR = {
			has_law = succ_feudal_elective
			has_law = succ_hre_elective
			ROOT = { has_claim = PREV }
		}
	}

	on_add = {
		log = "CB(claim_on_liege): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_dynlevy_begin = yes
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(claim_on_liege): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		ROOT = {
			participation_scaled_prestige = 100
			hidden_tooltip = { disband_event_forces = faction_revolters }
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
			hidden_tooltip = { disband_event_forces = faction_revolters }
		}
	}

	on_success_title = {
		emf_cb_latb = yes
		FROM = {
			hidden_tooltip = { disband_event_forces = faction_loyalists }
			any_demesne_title = {
				limit = {
					higher_tier_than = DUKE
					NOT = { title = PREVPREV }
					emf_laws_is_title_subject_to_default_CA_reduction = yes
				}
				if = {
					limit = { has_law = crown_authority_1 }
					add_law = crown_authority_0
				}
				if = {
					limit = { has_law = crown_authority_2 }
					add_law = crown_authority_1
				}
				if = {
					limit = { has_law = crown_authority_3 }
					add_law = crown_authority_2
				}
				if = {
					limit = { has_law = crown_authority_4 }
					add_law = crown_authority_3
				}
			}
		}
		# In EMF, all title usurpations drop CA, so just show a tooltip and
		# don't really do it here (or else we'd drop it twice).
		if = {
			limit = { has_law = crown_authority_1 }
			tooltip = { add_law = crown_authority_0 }
		}
		if = {
			limit = { has_law = crown_authority_2 }
			tooltip = { add_law = crown_authority_1 }
		}
		if = {
			limit = { has_law = crown_authority_3 }
			tooltip = { add_law = crown_authority_2 }
		}
		if = {
			limit = { has_law = crown_authority_4 }
			tooltip = { add_law = crown_authority_3 }
		}
		if = {
			limit = {
				ROOT = {
					prisoner = yes
					host = { character = FROM }
				}
			}
			ROOT = { prisoner = no }
		}
		if = {
			limit = { is_primary_holder_title = yes }
			FROM = { imprison = ROOT }
		}
		usurp_title_plus_barony_if_unlanded_and_vassals_no_adj = {
			target = ROOT
			type = claim
		}
		ROOT = { emf_dynlevy_update = yes }
		if = {
			limit = {
				higher_tier_than = BARON
				NOT = { culture = ROOT }
			}
			hidden_tooltip = { conquest_culture = ROOT }
		}
		if = {
			limit = { higher_tier_than = FROM }
			FROM = {
				set_defacto_liege = ROOT
				emf_liege_change = yes
			}
		}
		hidden_tooltip = {
			ROOT = {
				if = {
					limit = {
						has_nickname = no
						PREV = { higher_tier_than = COUNT }
						NOT = { PREV = { any_previous_holder = { character = ROOT } } }
					}
					random = {
						chance = 25
						give_nickname = nick_the_usurper
					}
				}
			}
		}
		emf_cb_dynlevy_end = yes
		hidden_tooltip = { check_holy_order_donation_effect = yes }
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(claim_on_liege): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_dynlevy_end = yes
		ROOT = { prestige = -100 }
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 50
		}
		FROM = {
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		hidden_tooltip = {
			any_attacker = {
				limit = {
					OR = {
						vassal_of = FROM
						liege_before_war = { character = FROM }
					}
				}
				disband_event_forces = faction_revolters
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_reverse_demand = {
		log = "CB(claim_on_liege): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		emf_cb_dynlevy_end = yes
		ROOT = { prestige = -150 }
		FROM = {
			participation_scaled_prestige = 100
			hidden_tooltip = { 
				if = { 
					limit = { NOT = { has_opinion_modifier = { modifier = opinion_traitor who = ROOT } } } #Allow free revocation of a title.
					opinion = {
						who = ROOT
						modifier = opinion_traitor
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		hidden_tooltip = {
			any_attacker = {
				limit = {
					OR = {
						vassal_of = FROM
						liege_before_war = { character = FROM }
					}
				}
				disband_event_forces = faction_revolters
				prisoner = FROM
				if = { # Safety to make sure vassal hierarchies are kept the same after the revolt
					limit = { NOT = { vassal_of = FROM } }
					set_defacto_liege = FROM
				}
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

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

	on_invalidation = {
		log = "CB(claim_on_liege): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_dynlevy_end = 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 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 = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

claim_on_liege_plot = {
	name = CB_NAME_CLAIM
	war_name = WAR_NAME_CLAIM_LIEGE
	sprite = 3
	truce_days = 1825
	hostile_against_others = yes
	allowed_to_target_tributaries = no
	press_claim = yes
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	can_ask_to_join_war = no
	infamy_modifier = 0.85

	can_use = { emf_cb_can_use = yes }

	can_use_title = {
		owner_under_FROM = yes
		emf_cb_can_use_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
					}
				}
			}
		}
	}

	is_valid_title = {
		owner_under_FROM = yes
	}

	on_add = {
		log = "CB(claim_on_liege_plot): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_dynlevy_begin = yes
		emf_cb_on_add = yes
	}

	on_success = {
		emf_cb_victory = yes
		ROOT = {
			participation_scaled_prestige = 100
			hidden_tooltip = { disband_event_forces = faction_revolters }
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
			hidden_tooltip = { disband_event_forces = faction_revolters }
		}
	}

	on_success_title = {
		log = "CB(claim_on_liege_plot): VICTORY: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_latb = yes
		FROM = {
			hidden_tooltip = { disband_event_forces = faction_loyalists }
			any_demesne_title = {
				limit = {
					higher_tier_than = DUKE
					NOT = { title = PREVPREV }
					emf_laws_is_title_subject_to_default_CA_reduction = yes
				}
				if = {
					limit = { has_law = crown_authority_1 }
					add_law = crown_authority_0
				}
				if = {
					limit = { has_law = crown_authority_2 }
					add_law = crown_authority_1
				}
				if = {
					limit = { has_law = crown_authority_3 }
					add_law = crown_authority_2
				}
				if = {
					limit = { has_law = crown_authority_4 }
					add_law = crown_authority_3
				}
			}
		}
		# In EMF, all title usurpations drop CA, so just show a tooltip and
		# don't really do it here (or else we'd drop it twice).
		if = {
			limit = { has_law = crown_authority_1 }
			tooltip = { add_law = crown_authority_0 }
		}
		if = {
			limit = { has_law = crown_authority_2 }
			tooltip = { add_law = crown_authority_1 }
		}
		if = {
			limit = { has_law = crown_authority_3 }
			tooltip = { add_law = crown_authority_2 }
		}
		if = {
			limit = { has_law = crown_authority_4 }
			tooltip = { add_law = crown_authority_3 }
		}
		usurp_title_plus_barony_if_unlanded = {
			target = ROOT
			type = claim
		}
		ROOT = { emf_dynlevy_update = yes }
		if = {
			limit = {
				higher_tier_than = BARON
				NOT = { culture = ROOT }
			}
			hidden_tooltip = { conquest_culture = ROOT }
		}
		ROOT = { plot_succeeds = yes }
		emf_cb_dynlevy_end = yes
		hidden_tooltip = { check_holy_order_donation_effect = yes }
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(claim_on_liege_plot): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_dynlevy_end = yes
		ROOT = { prestige = -100 }
		FROM = {
			participation_scaled_prestige = 50
			hidden_tooltip = { disband_event_forces = faction_revolters }
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
			hidden_tooltip = { disband_event_forces = faction_revolters }
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_reverse_demand = {
		log = "CB(claim_on_liege_plot): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		emf_imperial_decay_loss = yes
		emf_cb_dynlevy_end = yes
		ROOT = {
			prestige = -200
			imprison = FROM
		}
		FROM = {
			participation_scaled_prestige = 100
			hidden_tooltip = { disband_event_forces = faction_revolters }
			hidden_tooltip = { 
				if = { 
					limit = { NOT = { has_opinion_modifier = { modifier = opinion_traitor who = ROOT } } } #Allow free revocation of a title.
					opinion = {
						who = ROOT
						modifier = opinion_traitor
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
			hidden_tooltip = { disband_event_forces = faction_revolters }
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

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

	on_invalidation = {
		log = "CB(claim_on_liege_plot): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_dynlevy_end = 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
	}
}

claim_on_vassal_plot = {
	name = CB_NAME_CLAIM_VASSAL
	war_name = WAR_NAME_CLAIM_VASSAL
	sprite = 3
	truce_days = 3650
	hostile_against_others = yes
	reflect_titleholder_change = no
	allowed_to_target_tributaries = no
	press_claim = yes
	attacker_can_call_allies = no
	can_attack_vassals = yes
	infamy_modifier = 0.85

	can_use_title = { holder = FROM }

	is_valid_title = {
		holder = FROM
		ROOT = { has_plot = plot_gain_vassal_title }
	}

	on_add = {
		log = "CB(claim_on_vassal_plot): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		FROM = {
			if = {
				limit = { tier = BARON }
				hidden_tooltip = { character_event = { id = emf_cb.80 } }
			}
		}
	}

	on_success = {
		log = "CB(claim_on_vassal_plot): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		ROOT = {
			if = {
				limit = { NOT = { FROM = { tier = BARON } } }
				prestige = 50
			}
			if = {
				limit = { FROM = { tier = BARON } }
				add_character_modifier = { name = emf_plot_revoke_barony_cooldown duration = 2555 } # 7yr
			}
		}
	}

	on_success_title = {
		emf_cb_latb = yes
		usurp_title_plus_barony_if_unlanded = { target = ROOT type = revoke }
		ROOT = { plot_succeeds = yes }
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(claim_on_vassal_plot): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			prestige = -100
			cancel_plot = plot_gain_vassal_title
		}
		FROM = {
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		log = "CB(claim_on_vassal_plot): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			prestige = -200
			cancel_plot = plot_gain_vassal_title
		}
		FROM = {
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_invalidation = {
		log = "CB(claim_on_vassal_plot): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { cancel_plot = plot_gain_vassal_title }
	}

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

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	on_defender_leader_death = { ROOT = { cancel_plot = plot_gain_vassal_title } }

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

other_claim_on_liege = {
	name = CB_NAME_CLAIM
	war_name = WAR_NAME_OTHER_CLAIM
	sprite = 4
	truce_days = 1825
	hostile_against_others = yes
	can_ask_to_join_war = no
	press_claim = yes
	can_call_vassals = no
	attacker_can_call_allies = no
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	infamy_modifier = 0.5
	sort_priority = 850

	allowed_to_target_tributaries = no

	can_use = { emf_cb_thirdparty_can_use = yes }

	can_use_title = {
		holder = FROM
		OR = {
			ROOT = { is_female = no }
			NOT = { has_law = agnatic_succession }
			AND = {
				lower_tier_than = KING
				is_primary_holder_title = no
			}
			FROMFROM = { has_flag = faction_claimant_ultimatum_taken } # Faction war. Ignore claim strength.
		}
		OR = {
			FROMFROM = { has_flag = faction_claimant_ultimatum_taken } # Faction war. Ignore claim strength.
			emf_cb_can_press_claim = yes
		}
		emf_cb_thirdparty_can_use_title = yes
	}

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

	is_valid_title = {
		NOT = { holder = ROOT }
		owner_under_FROM = yes
		OR = {
			has_law = succ_feudal_elective
			has_law = succ_hre_elective
			ROOT = { has_claim = PREV }
		}
		NOT = {
			any_war = { # when used in title scope, checks all wars for that title
				attacker = { character = ROOT }
			}
		}
	}

	on_add = {
		log = "CB(other_claim_on_liege): START: title=[FromFrom.GetID]: [This.GetTitledFirstName] of [This.PrimaryTitle.GetBaseName] ([This.GetID]/[This.PrimaryTitle.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (#[From.GetID]/[From.PrimaryTitle.GetID]) on behalf of [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (#[Root.GetID]/[Root.PrimaryTitle.GetID])"
		emf_cb_dynlevy_other_begin = yes
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(other_claim_on_liege): VICTORY: title=[FromFrom.GetID]: [This.GetTitledFirstName] of [This.PrimaryTitle.GetBaseName] ([This.GetID]/[This.PrimaryTitle.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (#[From.GetID]/[From.PrimaryTitle.GetID]) on behalf of [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (#[Root.GetID]/[Root.PrimaryTitle.GetID])"
		emf_cb_victory_other = yes
		ROOT = {
			participation_scaled_prestige = 100
			hidden_tooltip = {  disband_event_forces = faction_revolters }
		}
		participation_scaled_prestige = 100
		hidden_tooltip = {  disband_event_forces = faction_revolters }
		any_attacker = {
			limit = {
				NOT = { character = ROOT }
				NOT = { character = PREV }
			}
			hidden_tooltip = {
				participation_scaled_prestige = 100
				disband_event_forces = faction_revolters
			}
		}
		ROOT = {
			opinion = {
				modifier = pressed_my_claim
				who = PREV
			}
		}
	}

	on_success_title = {
		if = {
			limit = { tier = COUNT }
			emf_cb_latb = yes
		}
		# In EMF, all title usurpations drop CA, so just show a tooltip and
		# don't really do it here (or else we'd drop it twice).
		if = {
			limit = { has_law = crown_authority_1 }
			tooltip = { add_law = crown_authority_0 }
		}
		if = {
			limit = { has_law = crown_authority_2 }
			tooltip = { add_law = crown_authority_1 }
		}
		if = {
			limit = { has_law = crown_authority_3 }
			tooltip = { add_law = crown_authority_2 }
		}
		if = {
			limit = { has_law = crown_authority_4 }
			tooltip = { add_law = crown_authority_3 }
		}
		FROM = {
			show_scope_change = no
			hidden_tooltip = { disband_event_forces = faction_loyalists }
			any_demesne_title = {
				limit = {
					higher_tier_than = DUKE
					NOT = { title = PREVPREV }
					emf_laws_is_title_subject_to_default_CA_reduction = yes
				}
				if = {
					limit = { has_law = crown_authority_1 }
					add_law = crown_authority_0
				}
				if = {
					limit = { has_law = crown_authority_2 }
					add_law = crown_authority_1
				}
				if = {
					limit = { has_law = crown_authority_3 }
					add_law = crown_authority_2
				}
				if = {
					limit = { has_law = crown_authority_4 }
					add_law = crown_authority_3
				}
			}
		}
		if = {
			limit = {
				ROOT = {
					prisoner = yes
					host = { character = FROM }
				}
			}
			ROOT = { prisoner = no }
		}
		if = {
			limit = { is_primary_holder_title = yes }
			hidden_tooltip = { FROM = { set_flag = temp_other_claim_on_liege_prison } }
		}
		hidden_tooltip = {
			ROOT = {
				if = {
					limit = {
						has_nickname = no
						lower_tier_than = PREV
						NOT = { PREV = { any_previous_holder = { character = ROOT } } }
					}
					random = {
						chance = 25
						give_nickname = nick_the_usurper
					}
				}
			}
		}
		emf_cb_ROOT_ensure_feudal = yes # When claimant has wrong government, take care of that first.
		usurp_title_plus_barony_if_unlanded_and_vassals_no_adj = {
			target = ROOT
			type = claim
		}
		ROOT = { emf_dynlevy_update = yes }
		if = {
			limit = {
				higher_tier_than = BARON
				NOT = { culture = ROOT }
			}
			hidden_tooltip = { conquest_culture = ROOT }
		}
	}

	on_success_posttitle = {
		hidden_tooltip = {
			if = {
				limit = { top_liege = { ROOT = { NOT = { top_liege = { character = PREVPREV } } } } }
				ROOT = {
					set_defacto_liege = PREV
					emf_liege_change = yes
				}
			}
			if = {
				limit = { top_liege = { ROOT = { NOT = { top_liege = { character = PREVPREV } } } } }
				ROOT = {
					set_defacto_liege = ROOT
					emf_liege_change = yes
				}
			}
			if = {
				limit = {
					FROM = {
						has_flag = temp_other_claim_on_liege_prison
						lower_tier_than = PREV
					}
				}
				FROM = { imprison = ROOT }
			}
			FROM = { clr_flag = temp_other_claim_on_liege_prison }
		}
		emf_cb_dynlevy_other_end = yes
		emf_cb_thirdparty_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(other_claim_on_liege): WHITEPEACE: title=[FromFrom.GetID]: [This.GetTitledFirstName] of [This.PrimaryTitle.GetBaseName] ([This.GetID]/[This.PrimaryTitle.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (#[From.GetID]/[From.PrimaryTitle.GetID]) on behalf of [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (#[Root.GetID]/[Root.PrimaryTitle.GetID])"
		emf_cb_dynlevy_other_end = yes
		prestige = -100
		imprison = FROM
		FROM = {
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		hidden_tooltip = { any_attacker = { disband_event_forces = faction_revolters } }
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_reverse_demand = {
		log = "CB(other_claim_on_liege): DEFEAT: title=[FromFrom.GetID]: [This.GetTitledFirstName] of [This.PrimaryTitle.GetBaseName] ([This.GetID]/[This.PrimaryTitle.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (#[From.GetID]/[From.PrimaryTitle.GetID]) on behalf of [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (#[Root.GetID]/[Root.PrimaryTitle.GetID])"
		emf_cb_defeat_other = yes
		emf_imperial_decay_loss = yes
		emf_cb_dynlevy_other_end = yes
		prestige = -200
		imprison = FROM
		FROM = {
			participation_scaled_prestige = 100
			hidden_tooltip = { 
				if = { 
					limit = { NOT = { has_opinion_modifier = { modifier = opinion_traitor who = ROOT } } } #Allow free revocation of a title.
					opinion = {
						who = ROOT
						modifier = opinion_traitor
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		hidden_tooltip = {
			any_attacker = {
				limit = {
					OR = {
						vassal_of = FROM
						liege_before_war = { character = FROM }
					}
				}
				disband_event_forces = faction_revolters
				prisoner = FROM
				if = { # Safety to make sure vassal hierarchies are kept the same after the revolt
					limit = { NOT = { vassal_of = FROM } }
					set_defacto_liege = FROM
				}
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_invalidation = {
		log = "CB(other_claim_on_liege): INVALIDATED: [FromFrom.GetTitledFirstName] of [FromFrom.PrimaryTitle.GetBaseName] ([FromFrom.GetID]/[FromFrom.PrimaryTitle.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (#[From.GetID]/[From.PrimaryTitle.GetID]) on behalf of [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (#[Root.GetID]/[Root.PrimaryTitle.GetID])"
		emf_cb_dynlevy_other_end = 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 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 = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

claim_all = {
	name = CB_NAME_CLAIM_ALL
	war_name = WAR_NAME_CLAIM_ALL
	sprite = 5
	truce_days = 1825
	is_permanent = yes
	can_ask_to_join_war = no
	is_claim_all = yes
	allowed_to_target_tributaries = no
	sort_priority = 1100

	can_use_gui = {
		emf_cb_can_use_gui = yes
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			NOT = { de_facto_liege = FROM }
			any_claim = {
				count = 2
				ROOT = { has_strong_claim = PREV }
				owner_under_FROM = yes
				NAND = {
					emf_cb_restore_hre_basic_can_use = yes
					emf_cb_restore_hre_basic_can_use_title = yes
				}
				emf_cb_can_use_title = yes
			}
		}
	}

	is_valid = {
		ROOT = {
			any_claim = {
				owner_under_FROM = yes
				ROOT = { has_strong_claim = PREV }
				emf_cb_can_use_title = yes
			}
		}
	}

	on_add = {
		log = "CB(claim_all): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			show_scope_change = no
			any_claim = {
				show_scope_change = no
				limit = {
					owner_under_FROM = yes
					ROOT = { has_strong_claim = PREV }
					emf_cb_can_use_title = yes
				}
				log = "CB(claim_all): ADD_TITLE: [This.GetID]"
				press_claim = ROOT
			}
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(claim_all): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		ROOT = {
			show_scope_change = no
			any_claim = {
				show_scope_change = no
				limit = {
					owner_under_FROM = yes
					ROOT = { has_strong_claim = PREV }
					emf_cb_can_use_title = yes
				}
				if = {
					limit = { tier = COUNT }
					emf_cb_latb = yes
				}
				emf_cb_ROOT_ensure_feudal = yes # When claimant has wrong government, take care of that first.
				usurp_title_plus_barony_if_unlanded = {
					target = ROOT
					type = claim
				}
				if = {
					limit = {
						higher_tier_than = BARON
						NOT = { culture = ROOT }
					}
					hidden_tooltip = { conquest_culture = ROOT }
				}
			}
			hidden_tooltip = {
				if = { #China with no land calculates what to do with nomad provinces
					limit = { 
						is_offmap_governor = offmap_china
						offmap_china = { NOT = { has_offmap_tmp_flag = WP_is_on_map } }
						NOT = { wealth = 2500 } #unless already rich from tributes
					}
					set_variable = { which = "WP_nomad_provinces" value = 0 }
					any_realm_province = {
						limit = { 
							has_province_modifier = nomad_agitation
							NOT = { has_settlement_construction = yes }
						}
						province_event = { id = JD.60100 } #increase counter for each nomad province
					}
					character_event = { id = JD.60101 days = 1 } #calculate gold sent from china for building holdings
				}
			}
		}
		hidden_tooltip = {
			ROOT = {
				if = {
					limit = {
						has_dlc = "Legacy of Rome"
						emf_has_roman_empire = yes
						OR = { 
							religion_group = christian
							religion = hellenic_pagan
							religion = hellenic_pagan_reformed
						}
					}
					character_event = {
						id = LoR.30
						days = 12
					}
				}
			}
		}
		ROOT = {
			show_scope_change = no
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		ROOT = {
			if = {
				limit = {
					character = 1282
					FROM = { character = 7813 }
				}
				reverse_imprison = FROM
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(claim_all): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			show_scope_change = no
			prestige = -150
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 75
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 75 }
		}
	}

	on_reverse_demand = {
		log = "CB(claim_all): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			show_scope_change = no
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			any_claim = {
				show_scope_change = no
				limit = {
					holder_scope = {
						OR = {
							character = FROM
							is_liege_or_above = FROM
						}
					}
					ROOT = { has_strong_claim = PREV }
				}
				ROOT = {
					show_scope_change = no
					remove_claim = PREV
				}
			}
			hidden_effect = {
				if = { #Makes China have a chance of leaving Expanionist policy...
					limit = {
						is_offmap_governor = offmap_china
						offmap_china = {
							has_policy = china_expansionist
						}
					}
					random = {
						chance = 50
						offmap_china = {
							set_policy = china_open
							log = "-------------------------------------"
							log = "China Policy Logging:"
							log = "Chinas new Policy is now OPEN"
							log = "-------------------------------------"
						}
					}
					log = "China War Logging: China lost a claim all war against [From.GetBestName]"
				}
			}
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 200
			if = {
				limit = {
					has_dlc = "Legacy of Rome"
					emf_has_roman_empire = yes
					OR = { 
						religion_group = christian
						religion = hellenic_pagan
						religion = hellenic_pagan_reformed
					}
				}
				hidden_tooltip = {
					character_event = {
						id = LoR.30
						days = 12
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
	}

	on_invalidation = {
		log = "CB(claim_all): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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 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 = 1.5 # Prefer claim_all to claim
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

change_seniority_succession_law = {
	name = CB_NAME_SENIORITY_SUCCESSION_LAW
	war_name = WAR_NAME_SENIORITY_SUCCESSION_LAW
	sprite = 6
	truce_days = 1825
	attacker_can_call_allies = no
	can_ask_to_join_war = no
	major_revolt = yes

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

change_primogeniture_succession_law = {
	name = CB_NAME_PRIMOGENITURE_SUCCESSION_LAW
	war_name = WAR_NAME_PRIOGENITURE_SUCCESSION_LAW
	sprite = 6
	truce_days = 1825
	attacker_can_call_allies = no
	can_ask_to_join_war = no
	major_revolt = yes

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

change_feudal_elective_succession_law = {
	name = CB_NAME_FEUDAL_ELECTIVE_SUCCESSION_LAW
	war_name = WAR_NAME_FEUDAL_ELECTIVE_SUCCESSION_LAW
	sprite = 6
	truce_days = 1825
	attacker_can_call_allies = no
	can_ask_to_join_war = no
	major_revolt = yes
	is_permanent = no

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

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

	can_use_gui = {
		emf_cb_can_use_gui = yes
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = { mercenary = no }
	}

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

	is_valid_title = {
		owner_under_FROM = yes
	}

	on_add = {
		log = "CB(invasion): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(invasion): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		emf_imperial_decay_victory = yes
		ROOT = {
			show_scope_change = no
			prestige = 200
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		hidden_tooltip = {
			ROOT = {
				set_global_flag = emf_latb_upon_gain
				occupy_minors_of_occupied_settlements = FROM
				gain_all_occupied_titles = {
					who = FROM
					type = invasion
				}
				clr_global_flag = emf_latb_upon_gain
				if = {
					limit = { has_nickname = no }
					random_list = {
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_victorious }
						10 = { give_nickname = nick_the_bold }
						10 = { give_nickname = nick_the_conqueror }
					}
				}
				if = {
					limit = { has_nickname = nick_the_bastard }
					give_nickname = nick_the_conqueror
				}
				if = {
					limit = {
						has_dlc = "Legacy of Rome"
						emf_has_roman_empire = yes
						OR = { 
							religion_group = christian
							religion = hellenic_pagan
							religion = hellenic_pagan_reformed
						}
					}
					character_event = {
						id = LoR.30
						days = 12
					}
				}
			}
		}
		FROM = {
			show_scope_change = no
			prestige = -200
		}
	}

	on_success_title = {
		emf_cb_latb = yes
		# Save ROOT's current primary title's succession laws
		save_event_target_as = new_title
		ROOT = {
			primary_title = {
				if = {
					limit = { has_law = succ_gavelkind }
					event_target:new_title = { set_flag = set_gavelkind }
				}
				else_if = {
					limit = { has_law = succ_elective_gavelkind }
					event_target:new_title = { set_flag = set_elective_gavelkind }
				}
				else_if = {
					limit = { has_law = succ_feudal_elective }
					event_target:new_title = { set_flag = set_feudal_elective }
				}
				else_if = {
					limit = { has_law = succ_hre_elective }
					event_target:new_title = { set_flag = set_hre_elective }
				}
				else_if = {
					limit = { has_law = succ_aztec_elective }
					event_target:new_title = { set_flag = set_aztec_elective }
				}
				else_if = {
					limit = { has_law = succ_byzantine_elective }
					event_target:new_title = { set_flag = set_byzantine_elective }
				}
				else_if = {
					limit = { has_law = succ_eldership }
					event_target:new_title = { set_flag = set_eldership }
				}
				else_if = {
					limit = { has_law = succ_primogeniture }
					event_target:new_title = { set_flag = set_primogeniture }
				}
				else_if = {
					limit = { has_law = succ_seniority }
					event_target:new_title = { set_flag = set_seniority }
				}
				else_if = {
					limit = { has_law = succ_tanistry }
					event_target:new_title = { set_flag = set_tanistry }
				}
				else_if = {
					limit = { has_law = succ_pictish_elective }
					event_target:new_title = { set_flag = set_pictish_elective }
				}
				else_if = {
					limit = { has_law = succ_dynastic_elective }
					event_target:new_title = { set_flag = set_dynastic_elective }
				}
				#else_if = {
				#	limit = { has_law = succ_mirath }
				#	event_target:new_title = { set_flag = set_mirath }
				#}
				else_if = {
					limit = { has_law = succ_turkish_succession }
					event_target:new_title = { set_flag = set_turkish_succession }
				}
				else_if = {
					limit = { has_law = succ_ultimogeniture }
					event_target:new_title = { set_flag = set_ultimogeniture }
				}
				# gender
				if = {
					limit = { has_law = agnatic_succession }
					event_target:new_title = { set_flag = set_agnatic_succession }
				}
				else_if = {
					limit = { has_law = cognatic_succession }
					event_target:new_title = { set_flag = set_cognatic_succession }
				}
				else_if = {
					limit = { has_law = true_cognatic_succession }
					event_target:new_title = { set_flag = set_true_cognatic_succession }
				}
				else_if = {
					limit = { has_law = enatic_cognatic_succession }
					event_target:new_title = { set_flag = set_enatic_cognatic_succession }
				}
				else_if = {
					limit = { has_law = enatic_succession }
					event_target:new_title = { set_flag = set_enatic_succession }
				}
			}
		}
		# Usurp the title
		custom_tooltip = {
			text = emf_cb_ctt_invasion_success
			hidden_tooltip = {
				ROOT = { any_vassal = { set_flag = emf_prior_vassal_of_@ROOT } }
				usurp_title_plus_barony_if_unlanded = {
					target = ROOT
					type = invasion
				}
				ROOT = {
					any_vassal = {
						limit = {
							NOT = { has_flag = emf_prior_vassal_of_@ROOT }
							religion = ROOT
						}
						opinion = { who = ROOT modifier = opinion_subjugated }
					}
					any_vassal = {
						limit = {
							NOT = { has_flag = emf_prior_vassal_of_@ROOT }
							NOT = { religion = ROOT }
						}
						opinion = { who = ROOT modifier = opinion_subjugated_other_rel }
					}
					any_vassal = { clr_flag = emf_prior_vassal_of_@ROOT }
				}
				FROM = { emf_ROOT_subjugate_ruler = yes }
				if = {
					limit = { FROM = { is_liege_or_above = ROOT } }
					FROM = { imprison = ROOT }
				}
			}
			if = {
				limit = {
					higher_tier_than = BARON
					NOT = { culture = ROOT }
				}
				hidden_tooltip = { conquest_culture = ROOT }
			}
		}
		# Set new title to former primary title's succession law
		ROOT = { set_flag = emf_no_law_penalties }
		if = {
			limit = {
				has_flag = set_gavelkind
				NOT = { has_law = succ_gavelkind }
				is_law_potential = succ_gavelkind
			}
			add_law = succ_gavelkind
		}
		else_if = {
			limit = {
				has_flag = set_elective_gavelkind
				NOT = { has_law = succ_elective_gavelkind }
				is_law_potential = succ_elective_gavelkind
			}
			add_law = succ_elective_gavelkind
		}
		else_if = {
			limit = {
				has_flag = set_feudal_elective
				NOT = { has_law = succ_feudal_elective }
				is_law_potential = succ_feudal_elective
			}
			add_law = succ_feudal_elective
		}
		else_if = {
			limit = {
				has_flag = set_hre_elective
				NOT = { has_law = succ_hre_elective }
				is_law_potential = succ_hre_elective
			}
			add_law = succ_hre_elective
		}
		else_if = {
			limit = {
				has_flag = set_aztec_elective
				NOT = { has_law = succ_aztec_elective }
				is_law_potential = succ_aztec_elective
			}
			add_law = succ_aztec_elective
		}
		else_if = {
			limit = {
				has_flag = set_byzantine_elective
				NOT = { has_law = succ_byzantine_elective }
				is_law_potential = succ_byzantine_elective
			}
			add_law = succ_byzantine_elective
		}
		else_if = {
			limit = {
				has_flag = set_eldership
				NOT = { has_law = succ_eldership }
				is_law_potential = succ_eldership
			}
			add_law = succ_eldership
		}
		else_if = {
			limit = {
				has_flag = set_primogeniture
				NOT = { has_law = succ_primogeniture }
				is_law_potential = succ_primogeniture
			}
			add_law = succ_primogeniture
		}
		else_if = {
			limit = {
				has_flag = set_seniority
				NOT = { has_law = succ_seniority }
				is_law_potential = succ_seniority
			}
			add_law = succ_seniority
		}
		else_if = {
			limit = {
				has_flag = set_tanistry
				NOT = { has_law = succ_tanistry }
				is_law_potential = succ_tanistry
			}
			add_law = succ_tanistry
		}
		else_if = {
			limit = {
				has_flag = set_pictish_elective
				NOT = { has_law = succ_pictish_elective }
				is_law_potential = succ_pictish_elective
			}
			add_law = succ_pictish_elective
		}
		else_if = {
			limit = {
				has_flag = set_dynastic_elective
				NOT = { has_law = succ_dynastic_elective }
				is_law_potential = succ_dynastic_elective
			}
			add_law = succ_dynastic_elective
		}
		#else_if = {
		#	limit = {
		#		has_flag = set_mirath
		#		NOT = { has_law = succ_mirath }
		#		is_law_potential = succ_mirath
		#	}
		#	add_law = succ_mirath
		#}
		else_if = {
			limit = {
				has_flag = set_turkish_succession
				NOT = { has_law = succ_turkish_succession }
				is_law_potential = succ_turkish_succession
			}
			add_law = succ_turkish_succession
		}
		else_if = {
			limit = {
				has_flag = set_ultimogeniture
				NOT = { has_law = succ_ultimogeniture }
				is_law_potential = succ_ultimogeniture
			}
			add_law = succ_ultimogeniture
		}
		# gender
		if = {
			limit = {
				has_flag = set_agnatic_succession
				NOT = { has_law = agnatic_succession }
			}
			add_law = agnatic_succession
		}
		else_if = {
			limit = {
				has_flag = set_cognatic_succession
				NOT = { has_law = cognatic_succession }
				is_law_potential = cognatic_succession
			}
			add_law = cognatic_succession
		}
		else_if = {
			limit = {
				has_flag = set_true_cognatic_succession
				NOT = { has_law = true_cognatic_succession }
				is_law_potential = true_cognatic_succession
			}
			add_law = true_cognatic_succession
		}
		else_if = {
			limit = {
				has_flag = set_enatic_cognatic_succession
				NOT = { has_law = enatic_cognatic_succession }
				is_law_potential = enatic_cognatic_succession
			}
			add_law = enatic_cognatic_succession
		}
		else_if = {
			limit = {
				has_flag = set_enatic_succession
				NOT = { has_law = enatic_succession }
				is_law_potential = enatic_succession
			}
			add_law = enatic_succession
		}
		clr_flag = set_gavelkind
		clr_flag = set_elective_gavelkind
		clr_flag = set_feudal_elective
		clr_flag = set_hre_elective
		clr_flag = set_aztec_elective
		clr_flag = set_byzantine_elective
		clr_flag = set_eldership
		clr_flag = set_seniority
		clr_flag = set_primogeniture
		clr_flag = set_tanistry
		clr_flag = set_pictish_elective
		clr_flag = set_dynastic_elective
		clr_flag = set_mirath
		clr_flag = set_turkish_succession
		clr_flag = set_ultimogeniture
		clr_flag = set_cognatic_succession
		clr_flag = set_agnatic_succession
		clr_flag = set_true_cognatic_succession
		clr_flag = set_enatic_cognatic_succession
		clr_flag = set_enatic_succession
		ROOT = { clr_flag = emf_no_law_penalties }
		ROOT = {
			if = {
				limit = { character = 140 }
				if = {
					limit = {
						has_dlc = "Holy Fury"
						NOT = {
							any_owned_bloodline = {
								has_bloodline_flag = william_the_conqueror_bloodline
							}
						}
					}
					create_bloodline = {
						type = william_the_conqueror
					}
				}
				if = {
					limit = { # Help AI William by usurping extra land
						ai = yes
						has_random_dejure = no
					}
					d_bedford = {
						usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
						any_de_jure_vassal_title = {
							limit = {
								holder_scope = {
									culture = saxon
									ai = yes # But not from players
									same_realm = ROOT
								}
							}
							usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
						}
					}
					d_canterbury = {
						usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
						any_de_jure_vassal_title = {
							limit = {
								holder_scope = {
									culture = saxon
									ai = yes # But not from players
									same_realm = ROOT
								}
							}
							usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
						}
					}
					d_cornwall = {
						usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
						any_de_jure_vassal_title = {
							limit = {
								holder_scope = {
									culture = saxon
									ai = yes # But not from players
									same_realm = ROOT
								}
							}
							usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
						}
					}
					d_somerset = {
						usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
						any_de_jure_vassal_title = {
							limit = {
								holder_scope = {
									culture = saxon
									ai = yes # But not from players
									same_realm = ROOT
								}
							}
							usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
						}
					}
					d_gloucester = {
						usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
						any_de_jure_vassal_title = {
							limit = {
								holder_scope = {
									culture = saxon
									ai = yes # But not from players
									same_realm = ROOT
								}
							}
							usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
						}
					}
					d_norfolk = {
						usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
						any_de_jure_vassal_title = {
							limit = {
								holder_scope = {
									culture = saxon
									ai = yes # But not from players
									same_realm = ROOT
								}
							}
							usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
						}
					}
				}
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(invasion): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			show_scope_change = no
			prestige = -200
		}
		FROM = {
			show_scope_change = no
			prestige = 100
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_reverse_demand = {
		log = "CB(invasion): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		emf_imperial_decay_loss = yes
		ROOT = {
			show_scope_change = no
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 8.0
			}
		}
		FROM = {
			show_scope_change = no
			prestige = 200
			participation_scaled_prestige = 200
			if = {
				limit = {
					has_dlc = "Legacy of Rome"
					emf_has_roman_empire = yes
					OR = { 
						religion_group = christian
						religion = hellenic_pagan
						religion = hellenic_pagan_reformed
					}
				}
				hidden_tooltip = {
					character_event = {
						id = LoR.30
						days = 12
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
	}

	on_reverse_demand_title = {
		log = "CB(invasion): START: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			show_scope_change = no
			remove_claim = PREV
		}
	}

	on_invalidation = {
		log = "CB(invasion): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

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

	attacker_ai_victory_worth = {
		factor = 150
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = { 
		factor = 1
		modifier = { #Focus on Coronation first.
			factor = 0.1
			ROOT = {
				emf_uses_furry_coronation = yes
				emf_is_crowned = no
			}
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_nomadic = no
				NOT = {
					is_tribal = yes
				}
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}
				}
			}
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_tribal = yes
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
							holding_type = tribal
						}
					}
				}
			}
		}
	}
}

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

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			show_scope_change = no
			trigger_if = {
				limit = {
					mercenary = no
					NOR = {
						has_flag = tribal_inv_requirements_bypass
						has_flag = horde_invader
					}
				}
				custom_tooltip = {
					text = NEEDS_500_PRESTIGE_COST
					prestige = 500
				}
			}
			OR = {
				has_horde_culture = yes
				has_game_rule = { name = no_cooldowns value = on }
				custom_tooltip = {
					text = tribal_invasion_done_ctt
					hidden_tooltip = { NOT = { has_flag = subjugation_done } }
				}
			}
			conditional_tooltip = {
				trigger = { is_patrician = yes }
				custom_tooltip = {
					text = emf_mr_ctt_extended_cbs_unlocked
					has_game_rule = { name = mr_cb_restriction value = off }
				}
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			OR = {
				ai = yes
				multiplayer = no
				NOT = {
					has_game_rule = {
						name = multiplayer_invasions
						value = off
					}
				}
			}
			is_nomadic = no
			OR = {
				AND = {
					has_dlc = "Horse Lords"
					higher_tier_than = COUNT
					OR = {
						has_flag = horde_invader
						AND = {
							independent = yes
							religion_group = pagan_group
							OR = {
								culture_group = altaic
								trigger_if = {
									limit = { emf_has_swmh = yes }
									culture = old_hungarian
								}
								trigger_else = {
									culture_group = magyar
								}
								culture_group = mesoamerican
							}
							FROM = {
								NOT = { religion_group = pagan_group } # Should subjugate these instead
							}
							OR = {
								is_tribal = yes
								primary_title = {
									has_law = succ_elective_gavelkind
								}
								has_landed_title = e_mongol_empire
								has_landed_title = e_mexikha
							}
						}
					}
				}
				AND = {
					NOT = { has_dlc = "Horse Lords" }
					independent = yes
					higher_tier_than = COUNT
					OR = {
						religion_group = pagan_group
						AND = {
							OR = {
								has_horde_culture = yes
								has_flag = horde_invader
							}
							NOT = { religion_group = christian }
						}
					}
					OR = {
						has_flag = horde_invader
						culture_group = altaic
						trigger_if = {
							limit = { emf_has_swmh = yes }
							culture = old_hungarian
						}
						trigger_else = {
							culture_group = magyar
						}
						culture_group = mesoamerican
					}
					OR = {
						has_horde_culture = yes
						has_flag = horde_invader
						FROM = {
							NOT = { religion_group = pagan_group } # Should subjugate these instead
						}
					}
					OR = {
						has_flag = horde_invader
						is_tribal = yes
						primary_title = {
							has_law = succ_elective_gavelkind
						}
					}
				}
				AND = {
					mercenary = yes
					num_of_titles == 1
				}
				AND = {
					has_flag = is_child_of_destiny
					any_owned_bloodline = {
						has_bloodline_flag = founder_invasions
						bloodline_is_active_for = PREV
					}
				}
				AND = {
					NOT = { has_flag = flag_declared_bloodline_invasion }
					any_owned_bloodline = {
						has_bloodline_flag = bloodline_great_conquerors
						bloodline_is_active_for = PREV
					}
				}
				# Blank check
				has_flag = tribal_inv_requirements_bypass
			}
		}
		ROOT = { NOT = { same_realm = FROM } }
	}

	can_use_title = {
		emf_cb_can_use_de_jure_title = yes
		# Vanilla no longer requires ROOT to border the target kingdom.
		# For the AI, we will allow this, because they don't target non-neighbors
		# unless they truly must (perhaps the mongols?). For players, we
		# will still require either a border or simply a county within the target kingdom.
		OR = {
			ROOT = { ai = yes }
			any_direct_de_jure_vassal_title = {
				any_direct_de_jure_vassal_title = {
					OR = {
						owner_under_ROOT = yes
						location = {
							owner_under_FROM = yes
							any_neighbor_province = { owner_under_ROOT = yes }
						}
					}
				}
			}
		}
		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 = { ROOT = { NOT = { same_realm = FROM } } }

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

	on_add = {
		log = "CB(tribal_invasion): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			if = {
				limit = {
					mercenary = no
					NOR = {
						has_flag = tribal_inv_requirements_bypass
						has_flag = horde_invader
					}
				}
				prestige = -500
			}
			if = {
				limit = {
					has_game_rule = { name = no_cooldowns value = off }
					NOR = {
						has_flag = horde_invader
						has_horde_culture = yes
						mercenary = yes
						AND = {
							has_flag = is_child_of_destiny
							any_owned_bloodline = {
								has_bloodline_flag = founder_invasions
								bloodline_is_active_for = PREV
							}
						}
					}
				}
				custom_tooltip = {
					text = tribal_invasion_launch_ctt
					hidden_tooltip = { set_flag = subjugation_done }
				}
			}
		}
		if = {
			limit = { 
				attacker = { 
					any_owned_bloodline = {
						has_bloodline_flag = bloodline_great_conquerors
						bloodline_is_active_for = PREV
					} 
				} 
			}
			attacker = {  
				set_flag = flag_declared_bloodline_invasion
			}
		}
		emf_cb_on_add = yes
	}

	on_success = {
		emf_cb_victory = yes
		emf_imperial_decay_victory = yes
		ROOT = {
			show_scope_change = no
			prestige = 200
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		FROM = {
			show_scope_change = no
			prestige = -200
		}
		if = {
			limit = {
				ROOT = {
					OR = {
						character = 159136
						character = 159137
					}
				}
			}
			ROOT = {
				if = {
					limit = {
						has_dlc = "Holy Fury"
						NOT = {
							any_owned_bloodline = {
								has_bloodline_flag = arpad_bloodline
							}
						}
					}
					create_bloodline = {
						type = arpad
					}
				}
			}
		}
		hidden_tooltip = {
			ROOT = {
				set_global_flag = emf_latb_upon_gain
				occupy_minors_of_occupied_settlements = FROM
				gain_all_occupied_titles = {
					who = FROM
					type = invasion
				}
				clr_global_flag = emf_latb_upon_gain
				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 = "CB(tribal_invasion): VICTORY: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_latb = yes
		custom_tooltip = {
			text = emf_cb_ctt_tribal_invasion_success
			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_flag = aztec_explorers
									has_flag = aztec_second_wave
								}
							}
							culture = nahuatl
							religion = aztec
							clr_flag = aztec_explorers
							clr_flag = aztec_second_wave
						}
					}
				}
			}
		}
	}

	on_success_posttitle = {
		emf_cb_on_success_posttitle = yes
		# First victory by Mexikha creates Followers of Ahuiateteo
		if = {
			limit = {
				e_mexikha = { holder = ROOT }
				has_dlc = "Mystics"
				NOT = {
					has_game_rule = {
						name = devil_worshipers
						value = none
					}
				}
				NOT = { has_global_flag = founded_followers_of_ahuiateteo }
			}
			set_global_flag = founded_followers_of_ahuiateteo
			if = {
				limit = {
					OR = {
						has_global_flag = demon_child_to_aztecs
						trait = hedonist
					}
				}
				join_society = followers_of_ahuiateteo
				set_society_grandmaster = yes
				add_society_currency_massive_effect = yes
			}
			else = {
				random_list = { # setup the Grandmaster
					50 = {
						trigger = {
							NOT = { has_religion_feature = religion_matriarchal }
						}
						create_character = {
							random_traits = yes
							dynasty = random
							religion = aztec
							culture = nahuatl
							female = no
							age = 26
						}
					}
					50 = {
						trigger = {
							OR = {
								NOT = { has_religion_feature = religion_patriarchal }
								has_game_rule = {
									name = gender
									value = all
								}
							}
						}
						create_character = {
							random_traits = yes
							dynasty = random
							religion = aztec
							culture = nahuatl
							female = yes
							age = 26
						}
					}
				}
				new_character = {
					add_trait = hedonist
					join_society = followers_of_ahuiateteo
					set_society_grandmaster = yes
					add_society_currency_massive_effect = yes
					emf_new_character_noble = yes
				}
			}
		}
	}

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

	on_fail_title = {
		log = "CB(tribal_invasion): WHITEPEACE: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	on_reverse_demand = {
		emf_cb_defeat = yes
		emf_imperial_decay_loss = yes
		ROOT = {
			show_scope_change = no
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			show_scope_change = no
			prestige = 200
			participation_scaled_prestige = 200
			hidden_tooltip = {
				if = {
					limit = { has_nickname = no }
					random_list = {
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_hammer }
						10 = { give_nickname = nick_the_avenger }
						10 = { give_nickname = nick_the_lion }
						10 = { give_nickname = nick_the_bold }
					}
				}
			}
		}
		FROM = {
			if = {
				limit = {
					has_dlc = "Legacy of Rome"
					emf_has_roman_empire = yes
					OR = { 
						religion_group = christian
						religion = hellenic_pagan
						religion = hellenic_pagan_reformed
					}
				}
				hidden_tooltip = {
					character_event = {
						id = LoR.30
						days = 12
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
	}

	on_reverse_demand_title = {
		log = "CB(tribal_invasion): DEFEAT: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	on_invalidation = {
		log = "CB(tribal_invasion): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = 500 } # refund
	}

	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 importance placed on this CB: scope is the targeted title, ROOT is the attacking character, FROM is the defending character
	ai_will_do = {
		factor = 1
		modifier = { #Focus on Coronation first.
			factor = 0.1
			ROOT = {
				emf_uses_furry_coronation = yes
				emf_is_crowned = no
			}
		}
		# Try to guide the Hordes in the right direction
		modifier = {
			factor = 0.1
			has_random_dejure = no
			ROOT = {
				OR = {
					dynasty = 613 # Seljuks
					dynasty = 800 # Timurids
					primary_title = { title = e_il-khanate }
				}
			}
			OR = {
				AND = {
					empire = {
						NOR = {
							title = e_persia
							emf_is_roman_empire = yes
							title = e_arabia
						}
					}
					NOT = { # SWMH kingdom titles that have no empire but should be conquered
						title = k_aljazira
						title = k_armenia
						title = k_syria
						title = k_antioch
						title = k_jerusalem
					}
				}
				FROM = {
					primary_title = { title = e_golden_horde }
					NOT = { year = 1300 }
				}
			}
		}
		modifier = {
			factor = 0.1
			has_random_dejure = no
			ROOT = { primary_title = { title = e_golden_horde } }
			OR = {
				empire = {
					OR = {
						title = e_persia
						emf_is_roman_empire = yes
						title = e_arabia
					}
				}
				title = k_aljazira
				title = k_armenia
				title = k_syria
				title = k_antioch
				title = k_jerusalem
				FROM = {
					primary_title = { title = e_il-khanate }
					NOT = { year = 1300 }
				}
			}
		}
		modifier = {
			factor = 0.1
			has_random_dejure = no
			ROOT = {
				trigger_if = {
					limit = { emf_has_swmh = yes }
					culture = old_hungarian
				}
				trigger_else = {
					culture = hungarian
				}
				religion_group = pagan_group
				primary_title = {
					OR = {
						title = k_hungary
						title = e_carpathia
					}
				}
			}
			OR = {
				empire = { title = e_russia }
				trigger_if = {
					limit = { emf_has_swmh = no }
					OR = {
						# Audax Validator "." Ignore_1006
						empire = { title = e_tartaria }
						# Audax Validator "." Ignore_1006
						title = k_crimea
					}
				}
				title = k_taurica
				title = k_galicia
				title = k_poland
			}
		}
		modifier = {
			factor = 0
			ROOT = { has_flag = no_invasions }
		}
		modifier = {
			factor = 0
			ROOT = {
				has_flag = is_temujin
				has_landed_title = e_mongol_empire
				demesne_size = 1
			}
			FROM = {
				is_offmap_governor = offmap_china
			}
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_nomadic = no
				NOT = {
					is_tribal = yes
				}
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}
				}
			}
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_tribal = yes
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
							holding_type = tribal
						}
					}
				}
			}
		}
		modifier = {
			factor = 10
			ROOT = { has_horde_culture = no }
			FROM = { ai = no }
		}
	}
}

crusade = {
	name = CB_NAME_CRUSADE
	war_name = WAR_NAME_CRUSADE
	sprite = 9
	truce_days = 3650
	is_permanent = yes
	is_holy_war = yes
	is_crusade = yes
	battle_warscore_mult = 0.5
	max_defender_occupation_score = 85
	apply_short_occ_mod = 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
	allow_whitepeace = no
	capturing_attacker_is_complete_victory = no
	capturing_defender_is_complete_victory = no
	allowed_to_target_tributaries = no
	sort_priority = 3000

	can_use_gui = {
		trigger_if = {
			limit = { ROOT = { ai = no } }
			emf_cb_can_use_gui = yes
		}
		ROOT = {
			has_called_crusade = no
			religion_authority = 0.05
			trigger_if = {
				limit = { has_flag = reclaim_heresy_crusade }
				always = yes # Scripted invocation so no need to tooltip this
			}
			trigger_else_if = {
				limit = { has_random_religion_names = yes }
				custom_tooltip = {
					text = ALT_GHW_STARTED
					has_global_flag = muslim_jihads_unlocked
					has_global_flag = pagan_ghws_unlocked
					has_global_flag = christian_crusades_unlocked
					has_global_flag = jewish_ghws_unlocked
					has_global_flag = zoroastrian_ghws_unlocked
				}
			}
			trigger_else_if = {
				limit = { religion_group = zoroastrian_group }
				custom_tooltip = {
					text = CTT_ZOROASTRIAN_GHWS_UNLOCKED
					has_global_flag = zoroastrian_ghws_unlocked
				}
			}
			trigger_else_if = {
				limit = { religion_group = jewish_group }
				custom_tooltip = {
					text = CTT_JEWISH_GHWS_UNLOCKED
					has_global_flag = jewish_ghws_unlocked
				}
			}
			trigger_else_if = {
				limit = { religion_group = pagan_group }
				custom_tooltip = {
					text = CTT_PAGAN_GHWS_UNLOCKED
					has_global_flag = pagan_ghws_unlocked
				}
			}
			trigger_else_if = {
				limit = { religion = cathar }
				custom_tooltip = {
					text = CTT_CHRISTIAN_CRUSADES_UNLOCKED
					has_global_flag = christian_crusades_unlocked
				}
				custom_tooltip = {
					text = cathar_holy_war_enabled_ctt
					has_global_flag = cathar_holy_war_enabled
				}
			}
			trigger_else_if = {
				limit = { religion_group = christian }
				custom_tooltip = {
					text = CTT_CHRISTIAN_CRUSADES_UNLOCKED
					has_global_flag = christian_crusades_unlocked
				}
			}
			trigger_else_if = {
				limit = { religion_group = muslim }
				custom_tooltip = {
					text = CTT_MUSLIM_JIHADS_UNLOCKED
					has_global_flag = muslim_jihads_unlocked
				}
			}
		}
	}

	can_use = {
		# emf_cb_can_use might block a crusade from being called for irrelevant reasons
		# ... but do block the mongol and seljuk invasion AIs from somehow being able to call a crusade
		trigger_if = {
			limit = { ROOT = { ai = no } }
			emf_cb_can_use = yes
		}
		trigger_else = {
			ROOT = {
				emf_mongol_invasion_mission = no
				emf_seljuk_invasion_mission = no
			}
		}
		ROOT = {
			mercenary = no
			OR = {
				uses_new_crusade = no
				has_flag = reclaim_heresy_crusade
			}
			can_call_crusade = yes
			any_demesne_title = { lower_tier_than = DUKE }
		}
		FROM = {
			NOT = { religion = ROOT }
			OR = {
				NOR = {
					religion_group = ROOT
					ROOT = { emf_syncretized_religion_with_PREV = yes }
				}
				is_heresy_of = ROOT
				is_parent_religion = ROOT
				ROOT = {
					OR = {
						AND = {
							NOT = { is_reformed_religion = PREV }
							NOT = { emf_syncretized_religion_with_PREV = yes }
							religion_group = pagan_group
						}
						religion = paulician
						religion = kharijite
					}
				}
			}
		}
		ROOT = {
			OR = {
				has_flag = reclaim_heresy_crusade
				NAND = { # Not if the Schism has been mended
					OR = {
						religion = catholic
						religion = fraticelli
					}
					has_global_flag = schism_mended
					NOT = { has_global_flag = cath_schism_mended }
				}
			}
		}
		# Showdown timers
		ROOT = {
			OR = {
				ai = no
				has_flag = reclaim_heresy_crusade
				trigger_if = {
					limit = { religion_group = christian }
					has_global_flag = emf_catholic_crusades_unlocked
				}
				trigger_else_if = {
					limit = { religion_group = muslim }
					has_global_flag = emf_muslim_crusades_unlocked
				}
				trigger_else_if = {
					limit = { religion_group = pagan_group }
					has_global_flag = emf_pagan_crusades_unlocked
				}
				trigger_else = {
					always = yes
				}
			}
		}
	}

	can_use_title = {
		emf_cb_can_use_de_jure_title = yes
		# Not if another crusade is being fought for this title
		NOT = {
			any_war = { # In title scope, checks wars for that title
				OR = {
					using_cb = crusade
					using_cb = new_crusade
				}
			}
		}
		FROM = {
			any_realm_province = { de_jure_liege_or_above = PREVPREV }
			independent = yes # Don't declare Crusades on revolters - chances are high the war will invalidate
			OR = {
				num_of_realm_counties = { value = 6 title = PREV }
				ROOT = { has_flag = reclaim_heresy_crusade }
			}
		}
		# Restrictions on crusade targets for AI religion heads
		OR = {
			ROOT = { ai = no }
			# This is a special chain:
			ROOT = { has_flag = reclaim_heresy_crusade }
			# The target owns a holy site within that kingdom title
			FROM = {
				any_realm_title = {
					de_jure_liege_or_above = PREVPREV
					is_holy_site = ROOT
				}
			}
			any_direct_de_jure_vassal_title = {
				any_direct_de_jure_vassal_title = {
					location = {
						OR = {
							# The target owns a province of their religion within that kingdom title
							AND = {
								owner_under_FROM = yes
								religion = ROOT
							}
							# A ruler of the attacker's religion owns a province in that kingdom or is neighboring it
							owner = { religion = ROOT }
							any_neighbor_province = { owner = { religion = ROOT } }
						}
					}
				}
			}
		}
	}

	is_valid = {
		ROOT = { can_call_crusade = yes }
		FROM = {
			NOT = { religion = ROOT }
			OR = {
				NOR = {
					religion_group = ROOT
					ROOT = { emf_syncretized_religion_with_PREV = yes }
				}
				is_heresy_of = ROOT
				is_parent_religion = ROOT
				ROOT = {
					OR = {
						AND = {
							NOT = { is_reformed_religion = PREV }
							NOT = { emf_syncretized_religion_with_PREV = yes }
							religion_group = pagan_group
						}
						religion = paulician
						religion = kharijite
					}
				}
			}
		}
	}

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

	on_add = {
		log = "CB(crusade): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		hidden_tooltip = {
			ROOT = { set_variable = { which = "crusader_adventurers" value = 0 } }
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(crusade): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		emf_imperial_decay_major_victory = yes
		ROOT = { clr_flag = reclaim_heresy_crusade }
	}

	on_success_title = {
		emf_cb_latb = yes
		save_event_target_as = crusade_title

		# First give it to the kingdom title's current holder, if they're of ROOT's religion
		if = {
			limit = {
				has_holder = yes
				holder_scope = { religion = ROOT }
			}
			holder_scope = {
				gain_settlements_under_title = {
					title = PREV
					enemy = FROM
					is_crusade = yes
					type = holy_war
				}
				hidden_tooltip = {
					emf_usurp_revolter_titles = yes # Handle revolters [immediately]
				}
			}
		}
		# Then give it to the kingdom's de jure liege, if they're of ROOT's religion
		if = {
			limit = {
				OR = {
					has_holder = no
					NOT = { holder_scope = { religion = ROOT } }
				}
				dejure_liege_title = {
					has_holder = yes
					holder_scope = { religion = ROOT }
				}
			}
			dejure_liege_title = {
				holder_scope = {
					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
					}
					hidden_tooltip = {
						emf_usurp_revolter_titles = yes # Handle revolters [immediately]
					}
				}
			}
		}
		# Then give it to the most participating attacker
		if = {
			limit = {
				OR = {
					has_holder = no
					NOT = { holder_scope = { religion = ROOT } }
				}
				NOT = {
					dejure_liege_title = {
						has_holder = yes
						holder_scope = { religion = ROOT }
					}
				}
			}
			hidden_tooltip = {
				most_participating_crusader = { save_event_target_as = crusade_victor }

				# Other crusaders will request fiefs
				any_attacker = {
					limit = {
						ai = yes
						NOT = { character = event_target:crusade_victor }
					}
					character_event = { id = emf_crusades.4 }
				}
				any_attacker = {
					limit = {
						ai = no
						NOT = { character = event_target:crusade_victor }
					}
					character_event = { id = emf_crusades.4 days = 1 }
				}
				# Add a strong, pressed claim to the best_crusade_claimant if not also the most_participating_attacker
				best_crusade_claimant = {
					if = {
						limit = { NOT = { character = event_target:crusade_victor } }
						event_target:crusade_title = { add_pressed_claim = PREV }
					}
				}
				conquest_culture = event_target:crusade_victor
			}
			most_participating_crusader = {
				# Clear original flags first
				any_landed_title = { clr_title_flag = previous_holding }
				any_character = { clr_flag = previous_vassal }

				# Now set flags on the winner's holdings/vassals
				any_demesne_title = { set_flag = previous_holding }
				any_vassal = { set_flag = previous_vassal }
				primary_title = { save_event_target_as = current_title }

				pf_tradition_plus4_effect = yes

				# Actually transfer the crusade titles
				gain_settlements_under_title = {
					title = PREV
					enemy = FROM
					type = holy_war
				}
				hidden_tooltip = {
					emf_usurp_revolter_titles = yes # Handle revolters [immediately]
					if = {
						limit = {
							OR = {
								this_has_lame_nickname_trigger = yes
								has_nickname = no
							}
						}
						random_list = {
							10 = {
								modifier = {
									factor = 0
									NOT = { religion_group = christian }
								}
								give_nickname = nick_the_crusader
							}
							10 = { give_nickname = nick_the_holy }
							10 = { give_nickname = nick_the_glorious }
							10 = { give_nickname = nick_the_lionheart }
							10 = { give_nickname = nick_the_great }
							10 = { give_nickname = nick_the_hammer }
							10 = { give_nickname = nick_the_pilgrim }
							40 = {
								modifier = {
									factor = 0
									religion_group = christian
								}
								give_nickname = nick_the_sword_of_god
							}
						}
					}
				}
				# To whom should the crusade kingdom go?
				hidden_tooltip = { character_event = { id = emf_crusades.2 } }
			}
		}
		any_attacker = {
			limit = {
				ai = no
				NOT = { has_flag = achievement_crusader }
			}
			set_flag = achievement_crusader
		}
		hidden_effect = {
			any_attacker = {
				limit = { NOT = { character = ROOT } }
				pf_tradition_plus4_effect = yes
				participation_scaled_prestige = 500
				participation_scaled_piety = 1000
				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -100
				}
			}
			any_defender = {
				limit = { NOT = { character = FROM } }
				pf_tradition_minus4_effect = yes
			}
		}
		ROOT = {
			show_scope_change = no
			participation_scaled_prestige = 500
			participation_scaled_piety = 1000
			if = {
				limit = { uses_decadence = yes }
				participation_scaled_decadence = -100
			}
			religion_authority = { modifier = won_crusade years = 20 }
			pf_tradition_plus4_effect = yes
		}
		FROM = {
			piety = -100
			religion_authority = { modifier = lost_crusade years = 20 }
			pf_tradition_minus4_effect = yes
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_reverse_demand = {
		log = "CB(crusade): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		emf_imperial_decay_major_loss = yes
		hidden_tooltip = {
			# Deal with crusader adventurers
			any_character = {
				limit = {
					religion = ROOT
					has_flag = crusader_adventurer
				}
				character_event = { id = emf_crusades.24 }
			}
		}
		if = {
			limit = { ROOT = { has_flag = reclaim_heresy_crusade } }
			set_parent_religion = { religion = FROM parent = 0 }
			ROOT = { clr_flag = reclaim_heresy_crusade }
		}
		ROOT = {
			show_scope_change = no
			transfer_scaled_wealth = { to = FROM value = 4.0 }
			piety = -200
			religion_authority = { modifier = lost_crusade years = 20 }
			if = {
				limit = {
					uses_decadence = yes
					NOT = { any_liege = { dynasty = PREV } }
				}
				participation_scaled_decadence = 35
			}
			pf_tradition_minus4_effect = yes
		}
		FROM = {
			piety = 100
			religion_authority = { modifier = won_crusade years = 20 }
			participation_scaled_piety = 300
			participation_scaled_prestige = 150
			if = {
				limit = { uses_decadence = yes }
				participation_scaled_decadence = -100
			}
			pf_tradition_plus4_effect = yes
			hidden_effect = {
				if = {
					limit = {
						has_dlc = "Legacy of Rome"
						emf_has_roman_empire = yes
						OR = { 
							religion_group = christian
							religion = hellenic_pagan
							religion = hellenic_pagan_reformed
						}
					}
					character_event = { id = LoR.30 days = 12 }
				}
				if = {
					limit = {
						OR = {
							this_has_lame_nickname_trigger = yes
							has_nickname = no
						}
					}
					random_list = {
						10 = { give_nickname = nick_the_holy }
						10 = { give_nickname = nick_the_glorious }
						10 = { give_nickname = nick_the_lionheart }
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_hammer }
						20 = {
							trigger = { NOT = { religion_group = christian } }
							give_nickname = nick_the_sword_of_god
						}
					}
				}
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				pf_tradition_minus4_effect = yes
				if = {
					limit = {
						uses_decadence = yes
						NOT = { any_liege = { dynasty = PREV } }
					}
					participation_scaled_decadence = 35
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_piety = 300
				participation_scaled_prestige = 150
				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -100
				}
				pf_tradition_plus4_effect = yes
			}
		}
	}

	on_invalidation_title = {
		log = "CB(crusade): INVALIDATED: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { clr_flag = reclaim_heresy_crusade }
		hidden_tooltip = {
			# Deal with crusader adventurers
			any_character = {
				limit = {
					religion = ROOT
					has_flag = crusader_adventurer
				}
				character_event = { id = emf_crusades.24 }
			}
		}
	}

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

# Crusade using the new crusade system. See crusades.info in the religion folder for documentation
new_crusade = {
	name = CB_NAME_CRUSADE
	war_name = WAR_NAME_CRUSADE
	sprite = 9
	truce_days = 3650
	is_permanent = yes
	is_holy_war = yes
	is_crusade = yes
	battle_warscore_mult = 0.5
	max_defender_occupation_score = 85
	apply_short_occ_mod = 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
	allow_whitepeace = no
	capturing_attacker_is_complete_victory = no
	capturing_defender_is_complete_victory = no
	allowed_to_target_tributaries = no
	can_ask_to_join_war = no # People join via script instead
	
	sort_priority = 3000

	can_use_gui = {
		trigger_if = {
			limit = { ROOT = { ai = no } }
			emf_cb_can_use_gui = yes
		}
		ROOT = {
			uses_new_crusade = yes
			has_called_crusade = no
			religion_authority = 0.05
			trigger_if = {
				limit = { has_random_religion_names = yes }
				custom_tooltip = {
					text = ALT_GHW_STARTED
					has_global_flag = muslim_jihads_unlocked
					has_global_flag = pagan_ghws_unlocked
					has_global_flag = christian_crusades_unlocked
					has_global_flag = jewish_ghws_unlocked
					has_global_flag = zoroastrian_ghws_unlocked
				}
			}
			trigger_else_if = {
				limit = { religion = cathar }
				custom_tooltip = {
					text = CTT_CHRISTIAN_CRUSADES_UNLOCKED
					has_global_flag = christian_crusades_unlocked
				}
				custom_tooltip = {
					text = cathar_holy_war_enabled_ctt
					has_global_flag = cathar_holy_war_enabled
				}
			}
			trigger_else = {
				religion_group = christian
				custom_tooltip = {
					text = CTT_CHRISTIAN_CRUSADES_UNLOCKED
					has_global_flag = christian_crusades_unlocked
				}
			}
		}
	}

	can_use = {
		# emf_cb_can_use might block a crusade from being called for irrelevant reasons
		# ... but do block the mongol and seljuk invasion AIs from somehow being able to call a crusade
		trigger_if = {
			limit = { ROOT = { ai = no } }
			emf_cb_can_use = yes
		}
		trigger_else = {
			ROOT = {
				emf_mongol_invasion_mission = no
				emf_seljuk_invasion_mission = no
			}
		}
		ROOT = {
			uses_new_crusade = yes
			can_call_crusade = yes
			hidden_tooltip = {
				NOT = { has_character_modifier = cancel_crusade_cooldown }
				NOT = { has_flag = reclaim_heresy_crusade }
			}
			mercenary = no
		}
		FROM = {
			NOT = { religion = ROOT }
			OR = {
				NOR = {
					religion_group = ROOT
					ROOT = { emf_syncretized_religion_with_PREV = yes }
				}
				is_heresy_of = ROOT
				is_parent_religion = ROOT
				AND = {
					has_global_flag = 4th_crusade_official
					NOT = { has_global_flag = fourth_crusade_complete }
					has_landed_title = e_byzantium
					NOT = { has_character_flag = fourth_crusade_recipient }
				}
				ROOT = {
					OR = {
						AND = {
							NOT = { is_reformed_religion = PREV }
							NOT = { emf_syncretized_religion_with_PREV = yes }
							religion_group = pagan_group
						}
						religion_group = muslim
						religion = paulician
						religion = kharijite
					}
				}
			}
		}
		ROOT = {
			NOT = { # Not if the Schism has been mended
				AND = {
					OR = {
						religion = catholic
						religion = fraticelli
					}
					has_global_flag = schism_mended
					NOT = { has_global_flag = cath_schism_mended }
				}
			}
		}
	}

	can_use_title = {
		emf_cb_can_use_de_jure_title = yes
		FROM = {
			independent = yes # Don't declare Crusades on revolters - chances are high the war will invalidate
			any_realm_title = {
				tier = COUNT
				de_jure_liege_or_above = PREVPREV
			}
			OR = {
				num_of_realm_counties = { value = 6 title = PREV }
				AND = {
					has_landed_title = e_byzantium
					religion_group = christian
				}
			}
		}
		# Don't let the Pope target the Byzzies for anything but Thessalonika
		trigger_if = {
			limit = {
				FROM = {
					has_landed_title = e_byzantium
					religion_group = christian
				}
			}
			title = k_thrace
		}
		# Restrictions on crusade targets for AI religion heads
		OR = {
			ROOT = { ai = no }
			has_global_flag = 4th_crusade_official
			# The target owns a holy site within that kingdom title
			FROM = {
				any_realm_title = {
					de_jure_liege_or_above = PREVPREV
					is_holy_site = ROOT
				}
			}
			any_direct_de_jure_vassal_title = {
				any_direct_de_jure_vassal_title = {
					location = {
						OR = {
							# The target owns a province of their religion within that kingdom title
							AND = {
								owner_under_FROM = yes
								religion = ROOT
							}
							# A ruler of the target's religion owns a province in that kingdom or is neighboring it
							owner = { religion = ROOT }
							any_neighbor_province = { owner = { religion = ROOT } }
						}
					}
				}
			}
		}
	}
	
	is_valid = {
		ROOT = {
			can_call_crusade = yes
		}
		FROM = {
			NOT = { religion = ROOT }
			OR = {
				NOR = {
					religion_group = ROOT
					ROOT = { emf_syncretized_religion_with_PREV = yes }
				}
				is_heresy_of = ROOT
				is_parent_religion = ROOT
				AND = {
					has_global_flag = 4th_crusade_official
					NOT = { has_global_flag = fourth_crusade_complete }
					has_landed_title = e_byzantium
					NOT = { has_character_flag = fourth_crusade_recipient }
				}
				ROOT = {
					OR = {
						AND = {
							NOT = { is_reformed_religion = PREV }
							NOT = { emf_syncretized_religion_with_PREV = yes }
							religion_group = pagan_group
						}
						religion_group = muslim
						religion = paulician
						religion = kharijite
					}
				}
			}
		}
	}

	is_valid_title = {
		any_de_jure_vassal_title = {
			tier = COUNT
			owner_under_FROM = yes
		}
	}

	on_add = {
		log = "CB(new_crusade): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(new_crusade): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		emf_imperial_decay_major_victory = yes
	}

	on_success_title = {
		emf_cb_latb = yes
		ROOT = {
			crusade_target_title = {
				save_event_target_as = new_crusade_title
				save_event_target_as = emf_cb_title
			}
			official_crusade_recipient = {
				save_event_target_as = new_crusade_recipient
			}
			crusade_target_char = {
				save_event_target_as = new_crusade_target_char
			}
		}
		most_participating_crusader = {
			save_event_target_as = new_crusade_most_participating_member
		}
		custom_tooltip = { text = new_crusade_win_TT }

		# Religion Authority and Prestige/Piety for the winner (Pope)
		ROOT = {
			show_scope_change = no
			participation_scaled_prestige = 500
			participation_scaled_piety = 1000
			religion_authority = { modifier = won_crusade years = 20 }
			pf_tradition_plus4_effect = yes
		}
		# Religion Authority and Prestige/Piety loss for the loser (Target of Crusade)
		FROM = {
			show_scope_change = no
			piety = -100
			religion_authority = { modifier = lost_crusade years = 20 }
			pf_tradition_minus4_effect = yes
		}
		hidden_effect = { 
			any_attacker = {
				limit = { NOT = { character = ROOT } }
				pf_tradition_plus4_effect = yes
			}
			any_defender = {
				limit = { NOT = { character = FROM } }
				pf_tradition_minus4_effect = yes
			}
		}
		hidden_tooltip = {
			if = { # Achievement
				limit = {
					event_target:new_crusade_target_char = {
						has_landed_title = e_byzantium
						religion_group = FROM
					}
				}
				any_attacker = {
					limit = { ai = no }
					set_flag = achievement_venetian_guile
				}
			}
			ROOT = { # NOTE: just guessing ROOT scope... it was in the main title scope, which if anything, would target the defender
				if = {
					limit = {
						ai = yes
						NOT = { crusade_beneficiary = { always = yes } }
					}
					character_event = { id = HFP.41007 }
				}
			}
			# AIs choose their stances
			any_attacker = {
				limit = {
					ai = yes
					NOT = { character = ROOT }
					OR = {
						NOT = { trait = excommunicated }
						check_if_fourth_crusade_trigger = yes
					}
				}
				character_event = { id = HFP.41091 }
			}
			if = {
				limit = { check_if_fourth_crusade_trigger = no }
				any_attacker = {
					limit = { trait = excommunicated }
					set_flag = crusade_papal_stance
					clr_flag = crusade_beneficiary_stance
					clr_flag = crusade_selfish_stance
				}
			}
			# Should we give the seized land to the Official Crusade Recipient?
			if = {
				limit = {
					most_participating_crusader = {
						NOR = {
							has_flag = crusade_selfish_stance
							AND = {
								has_flag = crusade_beneficiary_stance
								crusade_beneficiary = { is_alive = yes }
							}
						}
					}
					event_target:new_crusade_recipient = { is_alive = yes }
				}
				event_target:new_crusade_recipient = {
					set_crusader_king = THIS
					set_flag = crusader_king_flag
				}
			}
			# Give the seized land to the main contributor of the Crusade or his/her Beneficiary
			else_if = {
				limit = {
					most_participating_crusader = {
						holy_order = no
						mercenary = no
						NOR = {
							has_landed_title = k_papal_state
							has_landed_title = d_fraticelli
						}
						OR = {
							NOT = { trait = excommunicated }
							check_if_fourth_crusade_trigger = yes
						}
					}
				}
				most_participating_crusader = {
					# Give the title to your Beneficiary
					if = {
						limit = {
		                    crusade_beneficiary = {
	                            is_alive = yes
	                            is_landed = no
								NOT = { any_heir_title = { always = yes } }
	                            mercenary = no
	                            holy_order = no
								OR = {
									NOT = { trait = excommunicated }
									check_if_fourth_crusade_trigger = yes
								}
		                    }
		                    NOT = { has_flag = crusade_selfish_stance }
						}
						crusade_beneficiary = {
							set_crusader_king = THIS
							set_flag = crusader_king_flag
						}
					}
					# Find an appropriate replacement Beneficiary if there is none selected, or the currently selected one is improper in one way or another
					else_if = {
						limit = {
							ai = yes
		                    any_dynasty_member = {
	                    		is_adult = yes
								NOT = { any_heir_title = { always = yes } }
	                    		is_landed = no
	                    		mercenary = no
	                    		holy_order = no
	                    		religion = PREV
								OR = {
									NOT = { trait = excommunicated }
									check_if_fourth_crusade_trigger = yes
								}
		                    }
							NOT = { has_flag = crusade_selfish_stance }
						}
						random_dynasty_member = {
							limit = {
								is_adult = yes
								NOT = { any_heir_title = { always = yes } }
								is_landed = no
								mercenary = no
								holy_order = no
								religion = PREV
								OR = {
									NOT = { trait = excommunicated }
									check_if_fourth_crusade_trigger = yes
								}
							}
							preferred_limit = {
								is_child_of = PREV
								culture = PREV
								NOT = { practical_age = 45 }
								emf_is_preferred_gender_for_laws_of_PREV = yes
							}
							preferred_limit = {
								is_child_of = PREV
								NOT = { practical_age = 45 }
								emf_is_preferred_gender_for_laws_of_PREV = yes
							}
							preferred_limit = {
								is_child_of = PREV
								emf_is_preferred_gender_for_laws_of_PREV = yes
							}
							preferred_limit = {
								is_vassal_or_below_of = PREV
								culture = PREV
								NOT = { practical_age = 45 }
								emf_is_preferred_gender_for_laws_of_PREV = yes		
							}
							preferred_limit = {
								is_vassal_or_below_of = PREV
								NOT = { practical_age = 45 }
								emf_is_preferred_gender_for_laws_of_PREV = yes		
							}
							preferred_limit = {
								is_vassal_or_below_of = PREV
								emf_is_preferred_gender_for_laws_of_PREV = yes		
							}
							preferred_limit = {
								is_child_of = PREV
								culture = PREV
								NOT = { practical_age = 45 }
							}
							preferred_limit = {
								is_child_of = PREV
								NOT = { practical_age = 45 }
							}
							preferred_limit = {
								is_child_of = PREV
							}
							preferred_limit = {
								is_vassal_or_below_of = PREV
								culture = PREV
								NOT = { practical_age = 45 }
							}
							preferred_limit = {
								is_vassal_or_below_of = PREV
								NOT = { practical_age = 45 }
							}
							preferred_limit = {
								is_vassal_or_below_of = PREV
							}
							preferred_limit = {
								culture = PREV
								NOT = { practical_age = 45 }
							}
							preferred_limit = {
								NOT = { practical_age = 45 }
							}
							preferred_limit = {
								culture = PREV
							}
							set_crusader_king = THIS
							set_flag = crusader_king_flag
						}
					}
					# Take the titles yourself
					else = {
						clr_flag = crusade_beneficiary_stance
						clr_flag = crusade_papal_stance
						set_flag = crusade_selfish_stance
						set_crusader_king = THIS
						set_flag = crusader_king_flag
					}
				}
			}
			# If not applicable, find someone else to take the title
			else = {
				# Find a random Beneficiary within certain limits
				if = {
					limit = {
						most_participating_crusader = {
							any_crusade_participant = {
								is_alive = yes
								is_landed = yes
								holy_order = no
								mercenary = no
								OR = {
									NOT = { trait = excommunicated }
									check_if_fourth_crusade_trigger = yes
								}
								crusade_beneficiary = {
									is_alive = yes
									is_landed = no
									holy_order = no
									NOT = { any_heir_title = { always = yes } }
									age = 12
									OR = {
										NOT = { trait = excommunicated }
										check_if_fourth_crusade_trigger = yes
									}
								}
							}
						}
					}
					most_participating_crusader = {
						random_crusade_participant = {
							limit = {
								is_alive = yes
								is_landed = yes
								holy_order = no
								mercenary = no
								OR = {
									NOT = { trait = excommunicated }
									check_if_fourth_crusade_trigger = yes
								}
								crusade_beneficiary = {
									is_alive = yes
									is_landed = no
									holy_order = no
									NOT = { any_heir_title = { always = yes } }
									age = 12
									OR = {
										NOT = { trait = excommunicated }
										check_if_fourth_crusade_trigger = yes
									}
								}
							}
							preferred_limit = {
								real_tier = EMPEROR
							}
							preferred_limit = {
								real_tier = KING
							}
							crusade_beneficiary = {
		    		        	set_crusader_king = THIS
				                set_flag = crusader_king_flag
							}
						}
					}
				}
				# Find any random Beneficiary, if possible
				else_if = {
					limit = {
						most_participating_crusader = {
							any_crusade_participant = {
								OR = {
									NOT = { trait = excommunicated }
									check_if_fourth_crusade_trigger = yes
								}
								crusade_beneficiary = {
									is_alive = yes
									is_landed = no
									holy_order = no
									NOT = { any_heir_title = { always = yes } }
									OR = {
										NOT = { trait = excommunicated }
										check_if_fourth_crusade_trigger = yes
									}
								}
							}
						}
					}
					most_participating_crusader = {
						random_crusade_participant = {
							limit = {
								OR = {
									NOT = { trait = excommunicated }
									check_if_fourth_crusade_trigger = yes
								}
								crusade_beneficiary = {
									is_alive = yes
									is_landed = no
									holy_order = no
									NOT = { any_heir_title = { always = yes } }
									OR = {
										NOT = { trait = excommunicated }
										check_if_fourth_crusade_trigger = yes
									}
								}
							}
							crusade_beneficiary = {
								set_crusader_king = THIS
								set_flag = crusader_king_flag
							}
						}
					}
				}
				# Find a logical "Beneficiary" from any playable Catholic/Fraticelli ruler, if all else fails
				else = {
					most_participating_crusader = {
						# Try to find a Catholic/Fraticelli ruler
						if = {
							limit = {
								any_playable_ruler = {
									religion = PREV
									NOT = { trait = excommunicated }
									any_dynasty_member = {
										is_alive = yes
										is_dying = no
										mercenary = no
										holy_order = no
										age = 12
										NOT = { any_heir_title = { always = yes } }
										OR = {
											NOT = { trait = excommunicated }
											check_if_fourth_crusade_trigger = yes
										}
									}
								}
							}
							random_playable_ruler = {
								limit = {
									religion = PREV
									any_dynasty_member = {
										is_alive = yes
										is_dying = no
										mercenary = no
										holy_order = no
										age = 12
										NOT = { any_heir_title = { always = yes } }
										OR = {
											NOT = { trait = excommunicated }
											check_if_fourth_crusade_trigger = yes
										}
									}
								}
								preferred_limit = {
									real_tier = EMPEROR
									culture = PREV
								}
								preferred_limit = {
									real_tier = EMPEROR
								}
								preferred_limit = {
									real_tier = KING
									culture = PREV
								}
								preferred_limit = {
									real_tier = KING
								}
								preferred_limit = {
									real_tier = DUKE
									culture = PREV
								}
								preferred_limit = {
									real_tier = DUKE
								}
								preferred_limit = {
									culture = PREV
								}
								random_dynasty_member = {
									limit = {
										religion = PREV
										is_alive = yes
										is_dying = no
										mercenary = no
										holy_order = no
										age = 12
										NOT = { any_heir_title = { always = yes } }
										OR = {
											NOT = { trait = excommunicated }
											check_if_fourth_crusade_trigger = yes
										}
									}
									set_crusader_king = THIS
								}
							}
						}
						# If every other option fails, make the Holy Order Grandmaster the Crusader King
						else = {
							set_crusader_king = THIS
						}
					}
				}
			}

			# Find a backup beneficiary for the other Crusaders, if they don't have one already selected
			any_attacker = {
				limit = {
					NOT = { most_participating_crusader = { character = PREV } }
					ai = yes
					has_flag = crusade_beneficiary_stance
					NOT = {
	                	crusade_beneficiary = {
							is_alive = yes
							is_landed = no
							NOT = { any_heir_title = { always = yes } }
							mercenary = no
							holy_order = no
							age = 12
							OR = {
								NOT = { trait = excommunicated }
								check_if_fourth_crusade_trigger = yes
							}
	                	}
	                }
				}
				save_event_target_as = crusade_attacker
				character_event = { id = HFP.41007 }
			}
			
			# Set fallback flags for characters without anything chosen
			any_attacker = {
				limit = {
					NOR = {
						character = ROOT
						has_character_flag = crusade_papal_stance
						has_character_flag = crusade_beneficiary_stance
						has_character_flag = crusade_selfish_stance
					}
				}
				if = {
					limit = { crusade_beneficiary = { is_alive = yes } }
					set_character_flag = crusade_beneficiary_stance
				}
				else_if = {
					limit = { check_if_fourth_crusade_trigger = yes }
					set_character_flag = crusade_selfish_stance
				}
				else = {
					set_character_flag = crusade_papal_stance
				}
			}
			
			# Set pot amounts for each attacker
			any_attacker = {
				limit = { NOT = { character = ROOT } }
				if = {
					limit = { check_if_fourth_crusade_trigger = yes }
					if = {
						limit = { has_character_flag = crusade_selfish_stance }
						# Make sure selfish Crusaders get as little as possible (reduce the pot for Selfish Crusaders)
						set_crusade_pot_multiplier = 0
					}
					else_if = {
						limit = {
							any_owned_bloodline = {
								has_bloodline_flag = grand_crusader_bloodline
								bloodline_is_active_for = PREV
							}
						}
						set_crusade_pot_multiplier = 1.2
					}
					else = {
						set_crusade_pot_multiplier = 1.0
					}
				}
				else_if = {
					limit = { trait = excommunicated }
					set_crusade_pot_multiplier = 0
				}
				else_if = {
					limit = { has_character_flag = crusade_papal_stance }
					if = {
						limit = {
							any_owned_bloodline = {
								has_bloodline_flag = grand_crusader_bloodline
								bloodline_is_active_for = PREV
							}
						}
						set_crusade_pot_multiplier = 1.44
					}
					else = {
						set_crusade_pot_multiplier = 1.2
					}
				}
				else_if = {
					limit = { has_character_flag = crusade_beneficiary_stance }
					if = {
						limit = { event_target:new_crusade_recipient = { is_alive = yes } }
						if = {
							limit = {
								any_owned_bloodline = {
									has_bloodline_flag = grand_crusader_bloodline
									bloodline_is_active_for = PREV
								}
							}
							set_crusade_pot_multiplier = 1.08
						}
						else = {
							set_crusade_pot_multiplier = 0.9
						}
					}
					else_if = {
						limit = {
							any_owned_bloodline = {
								has_bloodline_flag = grand_crusader_bloodline
								bloodline_is_active_for = PREV
							}
						}
						set_crusade_pot_multiplier = 1.2
					}
					else = {
						set_crusade_pot_multiplier = 1.0
					}
				}
				else_if = {
					limit = { has_character_flag = crusade_selfish_stance }
					# Make sure selfish Crusaders get as little as possible (reduce the pot for Selfish Crusaders)
					set_crusade_pot_multiplier = 0
				}
			}

			# Distribute the Pot and Titles
			ROOT = {
				set_global_flag = distributing_crusade
				any_crusade_participant = {
					limit = {
						ai = no
						most_participating_crusader = { NOT = { character = PREV } }
					}
					character_event = { id = HFP.41060 }
				}
				distribute_crusade_pot = {
					amount = 0.5 # only distribute 50% of the total pot
				}
				any_crusade_participant = { crusade_beneficiary = { set_flag = beneficiary_who_received_titles } }
				# If the winner is a selfish Nomad
				if = {
					limit = {
						most_participating_crusader = {
							is_nomadic = yes
							OR = {
								has_flag = crusade_selfish_stance
								crusader_king = { character = PREV }
							}
						}
					}
					most_participating_crusader = {
						PREVPREV = { pf_liege_change_under_title_begin_effect = yes }
						gain_settlements_under_title = {
							title = PREVPREV
							enemy = FROM
							type = holy_war
						}
						PREVPREV = { pf_liege_change_under_title_end_effect = yes }
					}
					any_playable_ruler = { narrative_event = { id = HFP.41160 } }
				}
				# If it is a Fourth Crusade
				else_if = {
					limit = { check_if_fourth_crusade_trigger = yes }
					# Find a King for Trebizond
					e_byzantium = {
						holder_scope = {
							# Send imperial government validation event to every ruler in Byzantium, empire will be destroyed
							any_realm_lord = { character_event = { id = emf_imperial.3299 days = 1 } }
							if = {
								limit = { NOT = { any_playable_ruler = { has_landed_title = k_trebizond } } }
								random_vassal = {
									limit = {
										OR = {
											has_landed_title = d_trebizond
											has_landed_title = d_armeniacon
										}
									}
									preferred_limit = {
										has_landed_title = d_trebizond
									}
									grant_title_no_opinion = k_trebizond
								}
							}

						}
					}
					# Set vassals for Trebizond
					k_trebizond = {
						holder_scope = {
							e_byzantium = {
								holder_scope = {
									any_vassal = {
										limit = { de_jure_liege = k_trebizond }
										set_defacto_liege = PREVPREVPREV
										emf_liege_change = yes
									}
								}
							}
						}
					}
					# Find a Catholic/Fraticelli lord for Cyprus
					if = {
						limit = {
							OR = {
								trigger_if = {
									limit = { emf_has_swmh = yes }
									c_nicosia = {
										holder_scope = {
											ai = yes
											e_byzantium = { holder_scope = { is_liege_or_above_of = PREVPREV } }
										}
									}
								}
								c_famagusta = {
									holder_scope = {
										ai = yes
										e_byzantium = { holder_scope = { is_liege_or_above_of = PREVPREV } }
									}
								}
								c_limisol = {
									holder_scope = {
										ai = yes
										e_byzantium = { holder_scope = { is_liege_or_above_of = PREVPREV } }
									}
								}
							}
						}
						any_crusade_participant = {
							limit = { NOT = { most_participating_crusader = { character = PREV } } }
							crusade_beneficiary = {
								if = {
									limit = {
										emf_has_swmh = yes
										c_nicosia = {
											holder_scope = {
												ai = yes
												e_byzantium = { holder_scope = { is_liege_or_above_of = PREVPREV } }
											}
										}
									}
									c_nicosia = {
										usurp_title_plus_barony_if_unlanded = { target = PREV type = invasion }
									}
								}
								if = {
									limit = {
										c_famagusta = {
											holder_scope = {
												ai = yes
												e_byzantium = { holder_scope = { is_liege_or_above_of = PREVPREV } }
											}
										}
									}
									c_famagusta = {
										usurp_title_plus_barony_if_unlanded = { target = PREV type = invasion }
									}
								}
								if = {
									limit = {
										c_limisol = {
											holder_scope = {
												ai = yes
												e_byzantium = { holder_scope = { is_liege_or_above_of = PREVPREV } }
											}
										}
									}
									c_limisol = {
										usurp_title_plus_barony_if_unlanded = { target = PREV type = invasion }
									}
								}
								if = {
									limit = {
										trigger_if = {
											limit = { emf_has_swmh = yes }
											calc_true_if = {
												amount = 2
												c_nicosia = { holder = PREV }
												c_famagusta = { holder = PREV }
												c_limisol = { holder = PREV }
											}
										}
										trigger_else = {
											c_famagusta = { holder = PREV }
											c_limisol = { holder = PREV }
										}
									}
									d_cyprus = {
										usurp_title_plus_barony_if_unlanded = { target = PREV type = invasion }
									}
									grant_title = k_cyprus
									d_cyprus = { de_jure_liege = k_cyprus }
								}
								set_defacto_liege = THIS
								emf_liege_change = yes
								character_event = { id = HFP.41090 days = 1 }
								if = {
									limit = {
										NOR = {
											trait = crusader
											trait = crusader_king
											trait = crusader_queen
										}
									}
									add_trait = crusader
								}
							}
							character_event = { id = HFP.41007 }
						}
					}
					# Crete is taken by a Merchant Lord
					if = {
						limit = {
							OR = {
								c_kaneia = {
									holder_scope = {
										ai = yes
										e_byzantium = { holder_scope = { is_liege_or_above_of = PREVPREV } }
									}
								}
								c_chandax = {
									holder_scope = {
										ai = yes
										e_byzantium = { holder_scope = { is_liege_or_above_of = PREVPREV } }
									}
								}
							}
							any_crusade_participant = {
								is_patrician = yes
								higher_tier_than = COUNT
							}
						}
						random_crusade_participant = {
							limit = {
								is_patrician = yes
								higher_tier_than = COUNT
							}
							preferred_limit = {
								has_landed_title = k_venice
							}
							preferred_limit = {
								has_landed_title = d_venice
							}
							preferred_limit = {
								tier = EMPEROR
							}
							preferred_limit = {
								tier = KING
							}
							if = {
								limit = {
									c_kaneia = {
										holder_scope = {
											ai = yes
											e_byzantium = { holder_scope = { is_liege_or_above_of = PREVPREV } }
										}
									}
								}
								c_kaneia = {
									usurp_title_plus_barony_if_unlanded = { target = PREV type = invasion }
								}
							}
							if = {
								limit = {
									c_chandax = {
										holder_scope = {
											ai = yes
											e_byzantium = { holder_scope = { is_liege_or_above_of = PREVPREV } }
										}
									}
								}
								c_chandax = {
									usurp_title_plus_barony_if_unlanded = { target = PREV type = invasion }
								}
							}
							if = {
								limit = {
									c_kaneia = { holder = PREV }
									c_chandax = { holder = PREV }
								}
								d_krete = {
									usurp_title_plus_barony_if_unlanded = { target = PREV type = invasion }
								}
							}
						}
					}
					distribute_crusade_target_title = yes
					#The Crusader King becomes an Emperor and gets a bloodline
					crusader_king = { grant_title_no_opinion = e_latin_empire }
					# Find random vassal and make a king out of them
					e_byzantium = {
						holder_scope = {
							if = {
								limit = { emf_has_swmh = yes }
								random_vassal = {
									limit = {
										tier = DUKE
										ai = yes
									}
									preferred_limit = {
										capital_scope = { de_jure_liege_or_above = k_nikaea }
									}
									preferred_limit = {
										capital_scope = { de_jure_liege_or_above = k_epirus }
									}
									preferred_limit = {
										capital_scope = { de_jure_liege_or_above = k_achaea }
									}
									preferred_limit = {
										capital_scope = { de_jure_liege_or_above = k_anatolia }
									}
									preferred_limit = {
										capital_scope = { de_jure_liege_or_above = k_sicily }
									}
									primary_title = {
										create_title = {
											tier = KING
											landless = no
											temporary = no
											name = "fourth_crusade_greek_kingdom_name"
											holder = PREV
											culture = PREV
											custom_created = yes
											base_title = THIS
											copy_title_laws = yes
										}
									}
								}
							}
							else = {
								random_vassal = {
									limit = {
										tier = DUKE
										ai = yes
									}
									preferred_limit = {
										capital_scope = { de_jure_liege_or_above = k_byzantium }
									}
									preferred_limit = {
										capital_scope = { de_jure_liege_or_above = k_epirus }
									}
									preferred_limit = {
										capital_scope = { de_jure_liege_or_above = k_anatolia }
									}
									preferred_limit = {
										capital_scope = { de_jure_liege_or_above = k_sicily }
									}
									primary_title = {
										create_title = {
											tier = KING
											landless = no
											temporary = no
											name = "fourth_crusade_greek_kingdom_name"
											holder = PREV
											culture = PREV
											custom_created = yes
											base_title = THIS
											copy_title_laws = yes
										}
									}
								}
							}
						}
					}
					# All Byzantine vassals go independent except those neighboring the old Emperor, and he/she gets an inferior Empire title while the Byzantine title is broken
					e_byzantium = {
						holder_scope = {
							any_vassal = {
								limit = { higher_tier_than = BARON }
								set_defacto_liege = THIS
								emf_liege_change = yes
							}
							if = {
								limit = { primary_title = { title = e_byzantium } }
								capital_scope = {
									county = {
										e_byzantium = {
											holder_scope = {
												create_title = {
													tier = EMPEROR
													landless = no
													temporary = no
													name = "fourth_crusade_greek_empire_name"
													holder = THIS
													culture = THIS
													custom_created = yes
													base_title = PREVPREV
												}
											}
										}
									}
								}
								any_demesne_title = {
									limit = {
										tier = EMPEROR
										is_de_jure_title = yes
									}
									emf_destroy_title = yes
								}
							}
							any_neighbor_independent_ruler = {
								limit = {
									ai = yes
									culture_group = PREV
									religion = PREV
									lower_tier_than = KING
								}
								set_defacto_liege = PREV
								emf_liege_change = yes
							}
						}
					}
					if = {
						limit = { event_target:new_crusade_recipient = { has_flag = fourth_crusade_recipient } }
						event_target:new_crusade_recipient = { save_event_target_as = byzantine_refugee }
					}
					# Narrative event explaining that Byzantium has been taken out
					any_playable_ruler = {
						narrative_event = { id = HF.49008 }
					}
					event_target:byzantine_refugee = {
						death = {
							death_reason = death_battle
							killer = FROM
						}
					}
					# Making sure the Byzantine Empire is destroyed
					e_byzantium = { emf_unsafe_destroy_title = yes }
					set_global_flag = fourth_crusade_complete
				}
				# Otherwise
				else = {
					FROM = {
						any_realm_lord = {
							limit = {
								religion = ROOT
								ai = no
								higher_tier_than = BARON
							}
							set_defacto_liege = THIS
							emf_liege_change = yes
						}
					}
					distribute_crusade_target_title = yes
					crusader_king = {
						save_event_target_as = crusade_king_recipient
					}
					any_playable_ruler = {
						narrative_event = { id = HFP.41160 }
					}
				}
				crusader_king = {
					if = {
						limit = {
							has_character_flag = beneficiary_who_received_titles
							NOT = { has_dynasty_modifier = ruling_in_crusader_kingdom }
						}
						add_dynasty_modifier = ruling_in_crusader_kingdom
					}
					any_realm_character = {
						limit = {
							has_character_flag = beneficiary_who_received_titles 
							NOT = { has_dynasty_modifier = ruling_in_crusader_kingdom }
						}
						add_dynasty_modifier = ruling_in_crusader_kingdom
					}
				}
				clr_global_flag = distributing_crusade
			}
			
			# Take care of excommunicated participants
			if = {
				limit = { check_if_fourth_crusade_trigger = no }
				any_attacker = {
					limit = {
						trait = excommunicated
						PREV = {
							war_participation = {
								who = PREV
								score >= 0.001
							}
						}
					}
					character_event = { id = emf_religion.10 days = 3 }
				}
			}

			# If the CK has too much gold
			crusader_king = {
				if = {
					limit = {
						wealth = 2000 
						ai = yes
						any_vassal = {
							always = yes
						}
					}
					while = {
						count = 20
						limit = {
							wealth = 2000
							any_vassal = {
								NOT = { wealth = 1000 }
							}
						}
						random_vassal = {
							limit = {
								NOT = { wealth = 1000 }
							}
							wealth = 350
							crusader_king = {
								wealth = -350
							}
						}
					}
				}
			}
			
			# Achievement
			any_attacker = {
				limit = {
					ai = no
					NOT = { has_flag = achievement_crusader }
				}
				set_flag = achievement_crusader
			}

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

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

			crusader_king = {
				save_event_target_as = new_crusader_king
			}

			# If the player is the most participating Crusader, and gave away the Kingdom to a Beneficiary, trigger event to let them start playing as Beneficiary
			if = {
				limit = {
					most_participating_crusader = {
						ai = no
						NOT = {
							character = event_target:new_crusader_king
						}
						event_target:new_crusader_king = {
							dynasty = PREV
						}
					}
				}
				most_participating_crusader = {
					narrative_event = { id = HFP.41054 days = 3 }
				}
			}
			# Settle the Crusader State with some levies & a non-reinforcing retinue from the war
			# The event is also fired after HFP.41054 if the human player chooses not to take over
			else = {
				event_target:new_crusader_king = { character_event = { id = emf_crusades.30 days = 1 } }
			}

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

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

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

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

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

			# Clean up stances
			any_attacker = {
				clr_flag = crusade_selfish_stance
				clr_flag = crusade_beneficiary_stance
				clr_flag = crusade_papal_stance
			}
			
			ROOT = { character_event = { id = HFP.41199 } }
		}
	}

	on_success_posttitle = {
		emf_cb_on_success_posttitle = yes
	}

	on_reverse_demand = {
		log = "CB(new_crusade): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		emf_imperial_decay_major_loss = yes
		hidden_effect = {
			ROOT = { character_event = { id = HFP.41199 } }
		}
		ROOT = {
			show_scope_change = no
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -200
			if = {
				limit = {
					uses_decadence = yes
					NOT = { any_liege = { dynasty = PREV } }
				}
				participation_scaled_decadence = 35
			}
			religion_authority = { modifier = lost_crusade years = 20 }
			pf_tradition_minus4_effect = yes
		}
		FROM = {
			show_scope_change = no
			participation_scaled_piety = 300
			participation_scaled_prestige = 150
			if = {
				limit = {
					uses_decadence = yes
					NOT = { any_liege = { dynasty = PREV } }
				}
				participation_scaled_decadence = -100
			}
			piety = 100
			religion_authority = { modifier = won_crusade years = 20 }
			pf_tradition_plus4_effect = yes
			if = {
				limit = { 
					has_dlc = "Legacy of Rome"
					emf_has_roman_empire = yes
					OR = { 
						religion_group = christian
						religion = hellenic_pagan
						religion = hellenic_pagan_reformed
					}
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
			hidden_tooltip = {
				if = {
					limit = {
						OR = {
							this_has_lame_nickname_trigger = yes
							has_nickname = no
						}
					}
					random_list = {
						10 = { give_nickname = nick_the_holy }
						10 = { give_nickname = nick_the_glorious }
						10 = { give_nickname = nick_the_lionheart }
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_hammer }
						20 = {
							modifier = {
								factor = 0
								religion_group = christian
							}
							give_nickname = nick_the_sword_of_god
						}
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_piety = 300
				participation_scaled_prestige = 150
				if = {
					limit = {
						uses_decadence = yes
						NOT = { any_liege = { dynasty = PREV } }
					}
					participation_scaled_decadence = -100
				}
				pf_tradition_plus4_effect = yes
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				if = {
					limit = {
						uses_decadence = yes
						NOT = { any_liege = { dynasty = PREV } }
					}
					participation_scaled_decadence = 35
				}
				pf_tradition_minus4_effect = yes
			}
		}
		# Checks if Fourth Crusade
		if = {
			limit = { check_if_fourth_crusade_trigger = yes }
			set_global_flag = fourth_crusade_complete
		}
		# Take care of excommunicated participants
		else = {
			any_attacker = {
				limit = {
					trait = excommunicated
					PREV = {
						war_participation = {
							who = PREV
							score >= 0.001
						}
					}
				}
				character_event = { id = emf_religion.10 days = 3 }
			}
		}
		# Checks if First Crusade
		if = {
			limit = { NOT = { has_global_flag = first_crusade_complete } }
			set_global_flag = first_crusade_complete
		}
		any_defender = {
			limit = {
				ai = no
				religion_group = pagan_group
			}
			set_flag = achievement_pagan_fury
		}

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

	on_invalidation_title = {
		log = "CB(new_crusade): INVALIDATED: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

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

religious = {
	name = CB_NAME_RELIGIOUS
	war_name = WAR_NAME_RELIGIOUS
	sprite = 9
	truce_days = 3650
	is_permanent = yes
	is_holy_war = yes
	can_ask_to_join_war = yes
	check_de_jure_tier = DUKE # this scans all dejure duchies for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	allowed_to_target_tributaries = no
	sort_priority = 800

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			custom_tooltip = {
				text = emf_ctt_no_holy_war_cooldown
				hidden_tooltip = {
					OR = {
						has_game_rule = { name = no_cooldowns value = on }
						NOT = { has_character_modifier = emf_cb_mod_holy_war_cooldown }
					}
				}
			}
			custom_tooltip = {
				text = holy_war_piety_cost_ctt
				hidden_tooltip = {
					OR = {
						piety = 300
						AND = {
							NOT = { has_flag = emf_holy_warred }
							piety = 200
						}
					}
				}
			}
			trigger_if = {
				limit = {
					religion = cathar
					has_random_religion_names = no
				}
				custom_tooltip = {
					text = cathar_holy_war_enabled_ctt
					has_global_flag = cathar_holy_war_enabled
				}
			}
			conditional_tooltip = {
				trigger = { is_patrician = yes }
				custom_tooltip = {
					text = emf_mr_ctt_extended_cbs_unlocked
					has_game_rule = { name = mr_cb_restriction value = off }
				}
			}
			OR = {
				independent = yes
				AND = {
					tier = king
					OR = {
						trait = zealous
						trait = ambitious
					}
					NOR = {
						trait = cynical
						trait = craven
					}
				}
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		emf_cb_can_use_religious = yes
	}

	can_use_title = {
		emf_cb_special_religious_basic_can_use_title = no # Prefer 'Special Holy War' CB
		emf_cb_religious_reconquest_basic_can_use_title = no # Prefer 'Religious Reconquest' CB
		emf_cb_religious_basic_can_use_title = yes
		emf_cb_can_use_de_jure_title = yes
	}

	is_valid = {
		emf_cb_is_valid_religious = yes
	}

	is_valid_title = {
		any_de_jure_vassal_title = { owner_under_FROM = yes }
	}

	on_add = {
		log = "CB(religious): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_add_title = {
		ROOT = {
			if = {
				limit = {
					NOT = {
						any_demesne_title = {
							higher_tier_than = DUKE
							de_jure_vassal_or_below = PREVPREV
						}
					}
				}
				if = {
					limit = { has_flag = emf_holy_warred }
					piety = -300
				}
				if = {
					limit = { NOT = { has_flag = emf_holy_warred } }
					piety = -200
					set_flag = emf_holy_warred
				}
			}
			if = {
				limit = { has_game_rule = { name = no_cooldowns value = off } }
				custom_tooltip = {
					text = emf_cb_ctt_holy_war_cooldown
					hidden_tooltip = {
						add_character_modifier = {
							name = emf_cb_mod_holy_war_cooldown
							duration = 1825
						}
					}
				}
			}
		}
	}

	on_success = {
		log = "CB(religious): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		emf_imperial_decay_victory = yes
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_piety = 100
				participation_scaled_prestige = 150
				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -10
				}
				hidden_tooltip = {
					if = {
						limit = {
							has_ambition = obj_strengthen_religion
						}
						change_variable = { which = strengthen_religion value = 1 }
					}
				}
			}
		}
		ROOT = {
			participation_scaled_piety = 50
			participation_scaled_prestige = 150
			if = {
				limit = { uses_decadence = yes }
				participation_scaled_decadence = -10
			}
			if = {
				limit = { NOT = { religion = 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 }
					}
				}
			}
		}
		FROM = {
			if = {
				limit = { NOT = { religion = ROOT } }
				religion_authority = { modifier = lost_holy_war years = 20 }
			}
		}
		FROM = {
			show_scope_change = no
			piety = -100
			prestige = -150
		}
	}

	on_success_title = {
		emf_cb_religious_on_success_title = yes
		hidden_tooltip = { check_holy_order_donation_effect = yes }
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(religious): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			show_scope_change = no
			piety = -50
			prestige = -100
		}
	}

	on_reverse_demand = {
		log = "CB(religious): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		emf_imperial_decay_loss = yes
		ROOT = {
			show_scope_change = no
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -100
			prestige = -150
		}
		FROM = {
			participation_scaled_piety = 200
			participation_scaled_prestige = 200
			if = {
				limit = { uses_decadence = yes }
				participation_scaled_decadence = -10
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_piety = 200
				participation_scaled_prestige = 200
				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -10
				}
				hidden_tooltip = {
					if = {
						limit = {
							has_ambition = obj_strengthen_religion
							NOT = { religion = ROOT }
						}
						change_variable = { which = strengthen_religion value = 1 }
					}
				}
			}
		}
		any_attacker = {
			limit = {
				character = ROOT
				uses_decadence = yes
				NOT = {
					any_liege = {
						dynasty = PREV
					}
				}
			}
			participation_scaled_decadence = 20
		}
		any_attacker = {
			limit = {
				NOT = { character = ROOT }
				uses_decadence = yes
				NOT = {
					any_liege = {
						dynasty = PREV
					}
				}
			}
			hidden_tooltip = {
				participation_scaled_decadence = 20
			}
		}
		ROOT = {
			if = {
				limit = { NOT = { religion = FROM } }
				religion_authority = {
					modifier = lost_holy_war
					years = 20
				}
			}
		}
		FROM = {
			if = {
				limit = { NOT = { religion = 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 }
					}
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(religious): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { remove_character_modifier = emf_cb_mod_holy_war_cooldown }
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = { 
		factor = 2
		modifier = { #Focus on Coronation first.
			factor = 0.1
			ROOT = {
				emf_uses_furry_coronation = yes
				emf_is_crowned = no
			}
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_nomadic = no
				NOT = {
					is_tribal = yes
				}
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}
				}
			}
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_tribal = yes
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
							holding_type = tribal
						}
					}
				}
			}
		}
		# Try to guide the Hordes in the right direction
		modifier = {
			factor = 0.1
			has_random_dejure = no
			ROOT = {
				OR = {
					dynasty = 613 # Seljuks
					dynasty = 800 # Timurids
					primary_title = { title = e_il-khanate }
				}
			}
			OR = {
				empire = {
					NOT = {
						OR = {
							title = e_persia
							title = e_byzantium
							title = e_arabia
						}
					}
				}
				FROM = {
					primary_title = { title = e_golden_horde }
					NOT = { year = 1300 }
				}
			}
		}
		modifier = {
			factor = 0.1
			has_random_dejure = no
			ROOT = {
				primary_title = { title = e_golden_horde }
			}
			OR = {
				empire = {
					OR = {
						title = e_persia
						title = e_byzantium
						title = e_arabia
					}
				}
				FROM = {
					primary_title = { title = e_il-khanate }
					NOT = { year = 1300 }
				}
			}
		}
		modifier = {
			factor = 2.0
			has_random_dejure = no
			OR = {
				culture = persian
				culture = baloch
				culture = afghan
				culture = turkish
			}
			empire = {
				OR = {
					title = e_persia
					title = e_rajastan
				}
			}
		}
		modifier = { # Prefer direct land borders
			factor = 1.5
			any_direct_de_jure_vassal_title = {
				owner_under_FROM = yes
				location = { any_neighbor_province = { owner_under_ROOT = yes } }
			}
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

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

	can_use_gui = {
		emf_cb_can_use_gui = yes
	}

	can_use = {
		emf_cb_can_use = yes
		FROM = {
			excommunicated_for = ROOT
			in_revolt = no
			NOT = {
				any_current_enemy = {
					NOR = {
						religion_group = ROOT
						ROOT = { emf_syncretized_religion_with_PREV = yes }
					}
				}
			}
		}
		ROOT = {
			NOT = { excommunicated_for = ROOT }
			mercenary = no
		}
	}

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

	on_add = {
		log = "CB(excommunicated_ruler): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(excommunicated_ruler): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		ROOT = {
			hidden_tooltip = {
				if = {
					limit = { has_flag = flag_excommunicated_for_coronation }
					character_event = { id = HF.20220 } #Papal Coronation may proceed.
				}
			}
			if = {
				limit = { FROM = { primary_title = { tier = BARON } } }
				ROOT = {
					piety = 30
					prestige = 50
				}
			}
			if = {
				limit = { FROM = { primary_title = { tier = COUNT } } }
				ROOT = {
					piety = 60
					prestige = 100
				}
			}
			if = {
				limit = { FROM = { primary_title = { tier = DUKE } } }
				ROOT = {
					piety = 90
					prestige = 150
				}
			}
			if = {
				limit = { FROM = { primary_title = { tier = KING } } }
				ROOT = {
					piety = 120
					prestige = 200
				}
			}
			if = {
				limit = { FROM = { primary_title = { tier = EMPEROR } } }
				ROOT = {
					piety = 250
					prestige = 400
				}
			}
		}
		FROM = {
			excommunicate = no
			prestige = -200
			rightful_religious_head_scope = {
				opinion = {
					who = PREV
					modifier = opinion_repented_sins
				}
			}
			reverse_opinion = { who = ROOT modifier = overthrown_ruler }
			opinion = { who = ROOT modifier = overthrew_me }
			emf_abdicate = yes
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(excommunicated_ruler): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			piety = -50
			prestige = -100
		}
		# Coronation request locks cleared on invalidation or loss, to avoid locking the player in the middle of the negotiation forever.
		hidden_tooltip = {
			ROOT = {
				clr_flag = flag_excommunicated_for_coronation 
			}
		}
	}

	on_reverse_demand = {
		log = "CB(excommunicated_ruler): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		ROOT = {
			piety = -100
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = { prestige = 200 }
		# Coronation request locks cleared on invalidation or loss, to avoid locking the player in the middle of the negotiation forever.
		hidden_tooltip = {
			ROOT = {
				clr_flag = flag_excommunicated_for_coronation 
			}
		}
	}

	on_invalidation = {
		log = "CB(excommunicated_ruler): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		# Coronation request locks cleared on invalidation or loss, to avoid locking the player in the middle of the negotiation forever.
		hidden_tooltip = {
			if = {
				limit = {
					ROOT = { has_flag = flag_excommunicated_for_coronation }
				}
				ROOT = {
					character_event = { id = HF.20220 } #Papal Coronation may proceed.
				}
			}
		}
	}

	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 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 = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

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

	can_use_gui = {
		emf_cb_can_use_gui = yes
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			vassal_of = FROM
			higher_tier_than = BARON
			OR = {
				is_feudal = yes
				is_tribal = yes
				higher_tier_than = COUNT
			}
			mercenary = no
			NAND = {
				FROM = { is_merchant_republic = yes }
				is_patrician = yes
			}
			OR = {
				is_nomadic = no
				NOT = {
					clan_opinion = {
						who = FROM
						value = 0
					}
				}
			}
			OR = {
				ai = no
				emf_is_hre_being_restored = no
				FROM = { emf_has_hre_precursor_kingdom = no }
			}
			OR = {
				ai = no
				num_of_count_titles_in_realm >= 10
				NOT = {
					any_realm_title = {
						tier = COUNT
						best_de_jure_liege_title_under_FROM = yes
					}
				}
			}
		}
		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 }
			}
		}
	}

	is_valid = {
		ROOT = {
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = {
					FROM = {
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
			NAND = {
				FROM = { is_merchant_republic = yes }
				is_patrician = yes
			}
		}
		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 = {
		log = "CB(bid_for_independence): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(bid_for_independence): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		if = {
			limit = { FROM = { independent = no } }
			FROM = {
				if = {
					limit = { in_revolt = yes }
					liege_before_war = {
						ROOT = {
							show_scope_change = no
							set_defacto_liege = PREV
							emf_liege_change = yes
						}
					}
				}
				if = {
					limit = { in_revolt = no }
					liege = {
						ROOT = {
							show_scope_change = no
							set_defacto_liege = PREV
							emf_liege_change = yes
						}
					}
				}
			}
		}
		if = {
			limit = { FROM = { independent = yes } }
			ROOT = {
				show_scope_change = no
				set_defacto_liege = ROOT
				emf_liege_change = yes
			}
		}
		hidden_tooltip = {
			ROOT = {
				prestige = 100
				reverse_opinion = { who = FROM modifier = opinion_rightful_subjugation }
				set_flag = ai_flag_never_transfer
			}
		}
		ROOT = {
			show_scope_change = no
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		FROM = {
			show_scope_change = no
			pf_lower_CA_effect = yes
			pf_lower_TO_effect = yes
		}
		FROM = { emf_dynlevy_update = yes }
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(bid_for_independence): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			prestige = -50
			participation_scaled_prestige = -50
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = -50 }
		}
	}

	on_reverse_demand = {
		log = "CB(bid_for_independence): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			prestige = -100
			prisoner = FROM
			participation_scaled_prestige = -100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = -100 }
		}
		FROM = { prestige = 50 }
	}

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

	on_invalidation = {
		log = "CB(bid_for_independence): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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 = no
	can_call_vassals = no
	defender_can_call_allies = no
	major_revolt = no

	can_use = {
		always = no # v2.21 - Disabled - not working well
	}

	is_valid = { always = no }

	on_success = { }

	on_fail = { }

	on_reverse_demand = { }

	attacker_ai_victory_worth = { factor = -1 }

	attacker_ai_defeat_worth = { factor = -1 }

	defender_ai_victory_worth = { factor = -1 }

	defender_ai_defeat_worth = { factor = -1 }

	ai_will_do = { factor = 0 }
}

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

	can_use_gui = {
		emf_cb_can_use_gui = yes
	}

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

	on_add = {
		log = "CB(overthrow_ruler): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_dynlevy_begin = yes
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(overthrow_ruler): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		emf_cb_imperial_decay_overthrow = yes
		ROOT = { primary_title = { add_pressed_claim = FROM } }
		FROM = {
			primary_title = { save_event_target_as = emf_cb_title }
			prestige = -500
			remove_opinion = {
				who = ROOT
				modifier = declared_war
			}
			reverse_opinion = { modifier = overthrown_ruler who = ROOT }
			opinion = { modifier = overthrew_me who = ROOT }
			pf_lower_CA_effect = yes
			pf_lower_TO_effect = yes
			emf_abdicate = yes
		}
		event_target:emf_cb_title = {
			owner = {
				emf_dynlevy_update = yes
			}
		}
		ROOT = {
			participation_scaled_prestige = 250
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 250 }
		}
		emf_cb_dynlevy_end = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(overthrow_ruler): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_dynlevy_end = yes
		ROOT = { prestige = -100 }
		FROM = {
			remove_opinion = {
				who = ROOT
				modifier = declared_war
			}
		}
	}

	on_reverse_demand = {
		log = "CB(overthrow_ruler): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		emf_cb_dynlevy_end = yes
		ROOT = {
			prestige = -200
			prisoner = FROM
		}
		FROM = {
			participation_scaled_prestige = 50
			hidden_tooltip = { 
				if = { 
					limit = { NOT = { has_opinion_modifier = { modifier = opinion_traitor who = ROOT } } } #Allow free revocation of a title.
					opinion = {
						who = ROOT
						modifier = opinion_traitor
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_invalidation = {
		log = "CB(overthrow_ruler): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_dynlevy_end = yes
	}

	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
	}

	# 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 = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

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
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use_gui = {
		emf_cb_can_use_gui = yes
	}

	can_use = {
		emf_cb_can_use = yes
		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 = {
		log = "CB(overthrow_ruler_no_calls): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(overthrow_ruler_no_calls): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		emf_cb_imperial_decay_overthrow = yes
		ROOT = {
			prestige = 200
			primary_title = { add_pressed_claim = FROM }
		}
		FROM = {
			prestige = -500
			remove_opinion = {
				who = ROOT
				modifier = declared_war
			}
			pf_lower_CA_effect = yes
			pf_lower_TO_effect = yes
			reverse_opinion = { modifier = overthrown_ruler who = ROOT }
			opinion = { modifier = overthrew_me who = ROOT }
			emf_abdicate = yes
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(overthrow_ruler_no_calls): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = -100 }
		FROM = {
			prestige = -100
			remove_opinion = {
				who = ROOT
				modifier = declared_war
			}
		}
	}

	on_reverse_demand = {
		log = "CB(overthrow_ruler_no_calls): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			prestige = -200
			prisoner = FROM
		}
		FROM = {
			prestige = 50
			hidden_tooltip = { 
				if = { 
					limit = { NOT = { has_opinion_modifier = { modifier = opinion_traitor who = ROOT } } } #Allow free revocation of a title.
					opinion = {
						who = ROOT
						modifier = opinion_traitor
					}
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(overthrow_ruler_no_calls): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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 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 = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

change_gavelkind_succession_law = {
	name = CB_NAME_GAVELKIND_SUCCESSION_LAW
	war_name = WAR_NAME_GAVELKIND_SUCCESSION_LAW
	sprite = 6
	truce_days = 1825
	attacker_can_call_allies = no
	can_ask_to_join_war = no
	major_revolt = yes

	can_use = { always = no }

	is_valid = { always = no }

	on_success = {}

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

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

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

lower_tribal_organization_law = {
	name = CB_NAME_LOWER_TRIBAL_ORGANIZATION_LAW
	war_name = WAR_NAME_LOWER_TRIBAL_ORGANIZATION_LAW
	sprite = 6
	truce_days = 3650
	can_call_allies = no
	can_ask_to_join_war = no
	#major_revolt = yes
	is_permanent = no

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

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

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			custom_tooltip = {
				text = emf_ctt_no_coastal_republic_cooldown
				hidden_tooltip = {
					OR = {
						has_game_rule = { name = no_cooldowns value = on }
						NOT = { has_character_modifier = emf_cb_mod_republic_cooldown }
					}
				}
			}
		}
	}

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

	can_use_title = {
		tier = BARON
		is_capital = no # Minor Holding
		holding_type = city
		location = {
			port = yes
			# Must have a trade post there already, or city is under rival merchant republic
			OR = {
				trade_post_owner = { under_ROOT = yes }
				PREV = {
					holder_scope = {
						NOT = { same_realm = ROOT }
						OR = {
							is_merchant_republic = yes
							any_liege = { is_merchant_republic = yes }
						}
					}
				}
			}
			# EMF: Restrict bird's eye distance for city seizure
			ROOT = {
				OR = {
					AND = { # For vassal patricians
						is_merchant_republic = no
						any_liege = {
							is_merchant_republic = yes
							any_realm_title = {
								tier = COUNT
								location = {
									NOT = {
										distance = {
											where = PREVPREVPREVPREV
											value = 250
										}
									}
								}
							}
						}
					}
					AND = { # For doges
						is_merchant_republic = yes
						any_realm_title = {
							tier = COUNT
							location = {
								NOT = {
									distance = {
										where = PREVPREVPREV
										value = 250
									}
								}
							}
						}
					}
				}
			}
			# 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 = PREVPREV } }
				}
			}
		}
		emf_cb_can_use_title = yes
	}

	is_valid = {
		ROOT = {
			OR = {
				is_merchant_republic = yes
				is_patrician = yes
			}
			NOT = { is_liege_or_above = FROM }
			NOT = { is_vassal_or_below = FROM }
			NOT = { same_liege = FROM }
		}
	}

	is_valid_title = {
		location = {
			# 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 = PREVPREV } }
				}
			}
		}
	}

	on_add = {
		log = "CB(coastal_republic): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		if = {
			limit = { has_game_rule = { name = no_cooldowns value = off } }
			# FIXME: Give this a polished custom_tooltip
			ROOT = {
				add_character_modifier = {
					name = emf_cb_mod_republic_cooldown
					duration = 1825
				}
			}
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(coastal_republic): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		ROOT = {
			pf_prosperity_plus2_effect = yes
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_success_title = {
		if = {
			limit = {
				location = {
					trade_post_owner = { is_liege_or_above = ROOT }
				}
			}
			location = {
				trade_post_owner = {
					usurp_title = { target = PREVPREV type = invasion }
					break = yes
				}
			}
		}
		usurp_title = { target = ROOT type = invasion }
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(coastal_republic): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = -100 }
		FROM = {
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		log = "CB(coastal_republic): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = { pf_prosperity_minus2_effect = yes }
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_invalidation = {
		log = "CB(coastal_republic): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { remove_character_modifier = emf_cb_mod_republic_cooldown }
	}

	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 = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

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

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			custom_tooltip = {
				text = emf_ctt_no_coastal_republic_cooldown
				hidden_tooltip = {
					OR = {
						has_game_rule = { name = no_cooldowns value = on }
						NOT = { has_character_modifier = emf_cb_mod_republic_cooldown }
					}
				}
			}
		}
	}

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

	can_use_title = {
		tier = COUNT
		location = {
			port = yes
			any_province_holding = {
				holding_type = city
				owner_under_ROOT = yes
			}
			FROM = {
				OR = {
					is_merchant_republic = no
					capital_scope = { NOT = { province = PREVPREV } }
				}
			}
		}
		emf_cb_can_use_title = yes
	}

	is_valid = {
		ROOT = {
			OR = {
				is_merchant_republic = yes
				is_patrician = yes
			}
			NOT = { is_liege_or_above = FROM }
			NOT = { is_vassal_or_below = FROM }
			NOT = { same_liege = FROM }
		}
	}

	is_valid_title = {
		tier = COUNT
		location = {
			port = yes
			any_province_holding = {
				holding_type = city
				owner_under_ROOT = yes
			}
			FROM = {
				OR = {
					is_merchant_republic = no
					capital_scope = { NOT = { province = PREVPREV } }
				}
			}
		}
	}

	on_add = {
		log = "CB(coastal_county_republic): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		if = {
			limit = { has_game_rule = { name = no_cooldowns value = off } }
			# FIXME: Give this a polished custom_tooltip
			ROOT = {
				add_character_modifier = {
					name = emf_cb_mod_republic_cooldown
					duration = 1825
				}
			}
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(coastal_county_republic): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		ROOT = {
			pf_prosperity_plus2_effect = yes
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_success_title = {
		if = { # If a TP is attached to our city, prefer to give the county to the TP owner (and city overlord)
			limit = {
				location = {
					trade_post_owner = {
						under_ROOT = yes
						any_realm_title = {
							tier = BARON
							holding_type = city
							location = { province = PREVPREVPREV }
						}
					}
				}
			}
			location = {
				trade_post_owner = {
					usurp_title = { target = PREVPREV type = invasion }
					break = yes
				}
			}
		}
		# Else, give the county to ROOT (if the above didn't already)
		usurp_title = { target = ROOT type = invasion }
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(coastal_county_republic): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = -100 }
		FROM = {
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		log = "CB(coastal_county_republic): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = { pf_prosperity_minus2_effect = yes }
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_invalidation = {
		log = "CB(coastal_county_republic): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { remove_character_modifier = emf_cb_mod_republic_cooldown }
	}

	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 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 = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

weaken_vassal_plot = {
	name = CB_NAME_WEAKEN_VASSAL
	war_name = WAR_NAME_WEAKEN_VASSAL
	sprite = 3
	truce_days = 3650
	hostile_against_others = yes
	press_claim = yes
	attacker_can_call_allies = no
	can_ask_to_join_war = no

	can_use_gui = { emf_cb_can_use_gui = yes }

	can_use = { emf_cb_can_use = yes }

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

	is_valid_title = {
		owner_under_FROM = yes
	}

	on_add = {
		log = "CB(weaken_vassal_plot): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(weaken_vassal_plot): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		ROOT = { participation_scaled_prestige = 100 }
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			participation_scaled_prestige = 100
		}
		FROM = { pf_lower_CA_effect = yes }
	}

	on_success_title = {
		emf_cb_latb = yes
		FROM = {
			random_vassal = {
				limit = {
					tier = COUNT
					any_demesne_title = {
						tier = COUNT
						dejure_liege_title = { ROOT = { plot_target_title = { title = PREVPREV } } }
					}
				}
				grant_title = PREVPREV
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(weaken_vassal_plot): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		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_reverse_demand = {
		log = "CB(weaken_vassal_plot): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			prestige = -200
			imprison = FROM
			cancel_plot = plot_weaken_fellow_vassal
		}
		FROM = { participation_scaled_prestige = 100 }
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

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

	on_invalidation = {
		log = "CB(weaken_vassal_plot): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { cancel_plot = plot_weaken_fellow_vassal }
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

decadence_invasion = {
	name = CB_NAME_INVASION
	war_name = WAR_NAME_DEC_INVASION
	sprite = 7
	truce_days = 3650
	hostile_against_others = yes
	can_ask_to_join_war = no

	can_use = { always = no }
	is_valid = { always = no }

	on_success = { }

	on_fail = { }

	on_reverse_demand = { }

	on_invalidation = { }

	attacker_ai_victory_worth = {
		factor = 100
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 150
	}
}

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

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = emf_ctt_no_holy_war_cooldown
				hidden_tooltip = {
					OR = {
						has_game_rule = { name = no_cooldowns value = on }
						NOT = { has_character_modifier = emf_cb_mod_holy_war_cooldown }
					}
				}
			}
			custom_tooltip = { text = NEEDS_1000_PIETY_COST hidden_tooltip = { piety = 1000 } }
			conditional_tooltip = {
				trigger = { is_patrician = yes }
				custom_tooltip = {
					text = emf_mr_ctt_extended_cbs_unlocked
					has_game_rule = { name = mr_cb_restriction value = off }
				}
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			OR = {
				ai = yes
				multiplayer = no
				NOT = {
					has_game_rule = {
						name = multiplayer_invasions
						value = off
					}
				}
			}
			religion_group = muslim
			# These use 'tribal_invasion' instead:
			NOR = {
				is_nomadic = yes
				AND = {
					primary_title = { is_tribal_type_title = yes }
					has_horde_culture = yes
				}
				AND = {
					mercenary = yes
					NOT = { num_of_titles = 2 }
				}
			}
		}
		FROM = { NOT = { religion = ROOT } }
		ROOT = {
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}
	}

	can_use_title = {
		emf_cb_can_use_de_jure_title = yes
		# Muslims require the target to have at least 5 counties within the target kingdom
		FROM = {
			num_of_realm_counties = {
				value = 5
				title = PREV
			}
		}
		# The attacker needs a border inside the kingdom
		any_direct_de_jure_vassal_title = {
			any_direct_de_jure_vassal_title = {
				owner_under_FROM = yes
				location = { any_neighbor_province = { owner_under_ROOT = yes } }
			}
		}
	}

	is_valid = {
		ROOT = { religion_group = muslim }
		FROM = { NOT = { religion = ROOT } }
	}

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

	on_add = {
		log = "CB(muslim_invasion): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		ROOT = { piety = -1000 }
		if = {
			limit = { has_game_rule = { name = no_cooldowns value = off } }
			ROOT = {
				custom_tooltip = {
					text = emf_cb_ctt_holy_war_cooldown
					hidden_tooltip = {
						add_character_modifier = {
							name = emf_cb_mod_holy_war_cooldown
							duration = 1825
						}
					}
				}
			}
		}
	}

	on_success = {
		log = "CB(muslim_invasion): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		emf_imperial_decay_victory = yes
		ROOT = {
			show_scope_change = no
			prestige = 200
			hidden_tooltip = {
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
			participation_scaled_prestige = 200
			if = {
				limit = {
					uses_decadence = yes
					FROM = { NOT = { uses_decadence = yes } }
				}
				participation_scaled_decadence = -20
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = 200
				if = {
					limit = {
						uses_decadence = yes
						FROM = { NOT = { uses_decadence = yes } }
					}
					participation_scaled_decadence = -20
				}
				hidden_tooltip = {
					if = {
						limit = {
							has_ambition = obj_strengthen_religion
						}
						change_variable = { which = strengthen_religion value = 1 }
					}
				}
			}
		}
		FROM = {
			show_scope_change = no
			prestige = -200
		}
	}

	on_success_title = {
		emf_cb_latb = 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 = no # Even if the title holder is not participating in the war, gain holdings occupied by all Crusade participants
						type = invasion
					}
					if = {
						limit = {
							OR = {
								this_has_lame_nickname_trigger = yes
								has_nickname = no
							}
						}
						random_list = {
							10 = { give_nickname = nick_the_holy }
							10 = { give_nickname = nick_the_glorious }
							10 = { give_nickname = nick_the_lionheart }
							10 = { give_nickname = nick_the_great }
							10 = { give_nickname = nick_the_hammer }
							10 = { give_nickname = nick_the_magnificent }
						}
					}
				}
			}
		}
		pf_liege_change_under_title_end_effect = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(muslim_invasion): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		FROM = {
			show_scope_change = no
			prestige = 100
			if = {
				limit = {
					has_dlc = "Legacy of Rome"
					emf_has_roman_empire = yes
					OR = { 
						religion_group = christian
						religion = hellenic_pagan
						religion = hellenic_pagan_reformed
					}
				}
				hidden_tooltip = {
					character_event = {
						id = LoR.30
						days = 12
					}
				}
			}
			hidden_effect = {
				if = {
					limit = {
						OR = {
							this_has_lame_nickname_trigger = yes
							has_nickname = no
						}
					}
					random_list = {
						10 = { give_nickname = nick_the_holy }
						10 = { give_nickname = nick_the_glorious }
						10 = { give_nickname = nick_the_lionheart }
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_hammer }
						10 = { give_nickname = nick_the_magnificent }
						10 = { give_nickname = nick_the_avenger }
					}
				}
			}
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		ROOT = {
			show_scope_change = no
			prestige = -200
		}
	}

	on_reverse_demand = {
		log = "CB(muslim_invasion): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		emf_imperial_decay_loss = yes
		ROOT = {
			show_scope_change = no
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			if = {
				limit = {
					uses_decadence = yes
					NOT = { any_liege = { dynasty = PREV } }
				}
				decadence = 10
			}
		}
		FROM = {
			show_scope_change = no
			prestige = 200
			hidden_tooltip = {
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
			if = {
				limit = {
					has_dlc = "Legacy of Rome"
					emf_has_roman_empire = yes
					OR = { 
						religion_group = christian
						religion = hellenic_pagan
						religion = hellenic_pagan_reformed
					}
				}
				hidden_tooltip = {
					character_event = {
						id = LoR.30
						days = 12
					}
				}
			}
			hidden_effect = {
				if = {
					limit = {
						OR = {
							this_has_lame_nickname_trigger = yes
							has_nickname = no
						}
					}
					random_list = {
						10 = { give_nickname = nick_the_holy }
						10 = { give_nickname = nick_the_glorious }
						10 = { give_nickname = nick_the_lionheart }
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_hammer }
						10 = { give_nickname = nick_the_magnificent }
						10 = { give_nickname = nick_the_avenger }
					}
				}
			}
			participation_scaled_prestige = 200
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
			hidden_tooltip = {
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(muslim_invasion): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			show_scope_change = no
			piety = 1000
			remove_character_modifier = emf_cb_mod_holy_war_cooldown
		}
	}

	attacker_ai_victory_worth = {
		factor = 100
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 150
	}

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

cb_faction_independence = {
	name = CB_NAME_INDEPENDENCE
	war_name = WAR_NAME_FACTION_INDEPENDENCE
	sprite = 12
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	can_ask_to_join_war = no
	is_independence = yes

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

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

	on_add = {
		log = "CB(cb_faction_independence): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_dynlevy_begin = yes
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(cb_faction_independence): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		emf_imperial_decay_victory = yes
		FROM = {
			show_scope_change = no
			pf_prosperity_minus2_effect = yes
			hidden_tooltip = { disband_event_forces = faction_loyalists }
			pf_lower_CA_effect = yes
			pf_lower_TO_effect = yes
		}
		any_attacker = {
			show_scope_change = no
			limit = {
				OR = {
					vassal_of = FROM
					liege_before_war = { character = FROM }
				}
			}
			if = {
				limit = { FROM = { liege = { character = PREV } } } #independent = yes and any_independent_ruler exclude revolts
				set_defacto_liege = THIS
			}
			else = {
				FROM = {
					liege = {
						set_defacto_vassal = PREVPREV # any_attacker
					}
				}
			}
			emf_liege_change = yes
			hidden_tooltip = {
				prestige = 100
				reverse_opinion = { who = FROM modifier = opinion_rightful_subjugation }
				set_flag = ai_flag_never_transfer
			}
		}
		ROOT = {
			show_scope_change = no
			participation_scaled_prestige = 100
			hidden_tooltip = { disband_event_forces = faction_revolters }
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = 100
				disband_event_forces = faction_revolters
			}
		}
		emf_cb_dynlevy_end = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(cb_faction_independence): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			show_scope_change = no
			prestige = -50
			add_character_modifier = {
				name = faction_independence_ultimatum_timer
				hidden = yes
				duration = 3650
			}
			hidden_tooltip = { disband_event_forces = faction_revolters }
			participation_scaled_prestige = -50
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = -50
				disband_event_forces = faction_revolters
			}
			add_character_modifier = {
				name = faction_independence_ultimatum_timer
				hidden = yes
				duration = 3650
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
		emf_cb_dynlevy_end = yes
	}

	on_reverse_demand = {
		log = "CB(cb_faction_independence): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		emf_imperial_decay_loss = yes
		ROOT = {
			show_scope_change = no
			prestige = -100
			prisoner = FROM
			add_character_modifier = {
				name = faction_independence_ultimatum_timer
				hidden = yes
				duration = 3650
			}
			hidden_tooltip = { disband_event_forces = faction_revolters }
			participation_scaled_prestige = -100
		}
		any_attacker = {
			limit = {
				NOT = { character = ROOT }
				OR = {
					vassal_of = FROM
					liege_before_war = { character = FROM }
				}
			}
			prisoner = FROM
			participation_scaled_prestige = -100
			hidden_tooltip = {
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_independence_ultimatum_timer
					hidden = yes
					duration = 3650
				}
				if = { # Safety to make sure vassal hierarchies are kept the same after the revolt
					limit = { NOT = { vassal_of = FROM } }
					set_defacto_liege = FROM
				}
			}
		}
		FROM = {
			show_scope_change = no
			prestige = 50
			hidden_tooltip = { disband_event_forces = faction_loyalists }
			hidden_tooltip = {
				if = { 
					limit = { NOT = { has_opinion_modifier = { who = ROOT modifier = opinion_traitor } } } #Allow free revocation of a title.
					opinion = { who = ROOT modifier = opinion_traitor }
				}
			}
		}
		emf_cb_dynlevy_end = yes
	}

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

	on_invalidation = {
		log = "CB(cb_faction_independence): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_dynlevy_end = yes
	}

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


# This casus belli replaces the vanilla one, and instead uses a tier system to determine what kingdoms are valid targets
# To unlock a tier, all tiers before it have to be completely conquered
# Tier 1: Byzantium
# Tier 2: Balkans and Sicily
# Tier 3: Italy, Levant, and Eastern/Central North Africa
# Tier 4: Western North Africa and Iberia
# Tier 5: France
# Tier 6: England, Wales, and (southern) Germany
imperial_reconquest = {
	name = CB_NAME_IMPERIAL
	war_name = WAR_NAME_IMPERIAL
	sprite = 17
	truce_days = 1825
	is_permanent = yes
	check_de_jure_tier = DUKE
	allowed_to_target_tributaries = no
	sort_priority = 1900

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			show_scope_change = no
			trigger_if = {
				limit = {
					NOR = {
						religion = orthodox
						religion = iconoclast
						religion = bogomilist
						religion = monothelite
					}
				}
				prestige >= 500
			}
			trigger_else = {
				trait = strong_basileus
			}
			custom_tooltip = {
				text = NEEDS_200_PRESTIGE_COST
				OR = {
					ai = yes
					prestige >= 200
				}
			}
			OR = {
				religion_group = christian
				religion = hellenic_pagan
				religion = hellenic_pagan_reformed
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			independent = yes
			NOT = { same_realm = FROM }
			primary_title = {
				OR = {
					emf_is_roman_empire = yes
					title = e_latin_empire
				}
				NOT = { has_flag = alternate_byz }
			}
			mercenary = no
		}
	}

	can_use_title = {
		emf_cb_can_use_de_jure_title = yes
		OR = {
			any_direct_de_jure_vassal_title = {
				location = { region = emf_region_ir_tier_1 }
				owner_under_FROM = yes
			}
			trigger_if = {
				limit = {
					any_direct_de_jure_vassal_title = {
						location = { region = emf_region_ir_tier_2 }
						owner_under_FROM = yes
					}
				}
				NOT = {
					any_landed_title = {
						tier = COUNT
						location = { region = emf_region_ir_tier_1 }
						owner_under_ROOT = no
					}
				}
			}
			trigger_else_if = {
				limit = {
					any_direct_de_jure_vassal_title = {
						location = { region = emf_region_ir_tier_3 }
						owner_under_FROM = yes
					}
				}
				NOT = {
					any_landed_title = {
						tier = COUNT
						location = {
							OR = {
								region = emf_region_ir_tier_1
								region = emf_region_ir_tier_2
							}
						}
						owner_under_ROOT = no
					}
				}
			}
			trigger_else_if = {
				limit = {
					any_direct_de_jure_vassal_title = {
						location = { region = emf_region_ir_tier_4 }
						owner_under_FROM = yes
					}
				}
				NOT = {
					any_landed_title = {
						tier = COUNT
						location = {
							OR = {
								region = emf_region_ir_tier_1
								region = emf_region_ir_tier_2
								region = emf_region_ir_tier_3
							}
						}
						owner_under_ROOT = no
					}
				}
			}
			trigger_else_if = {
				limit = {
					any_direct_de_jure_vassal_title = {
						location = { region = emf_region_ir_tier_5 }
						owner_under_FROM = yes
					}
				}
				NOT = {
					any_landed_title = {
						tier = COUNT
						location = {
							OR = {
								region = emf_region_ir_tier_1
								region = emf_region_ir_tier_2
								region = emf_region_ir_tier_3
								region = emf_region_ir_tier_4
							}
						}
						owner_under_ROOT = no
					}
				}
			}
			trigger_else_if = {
				limit = {
					any_direct_de_jure_vassal_title = {
						location = { region = emf_region_ir_tier_6 }
						owner_under_FROM = yes
					}
				}
				NOT = {
					any_landed_title = {
						tier = COUNT
						location = {
							OR = {
								region = emf_region_ir_tier_1
								region = emf_region_ir_tier_2
								region = emf_region_ir_tier_3
								region = emf_region_ir_tier_4
								region = emf_region_ir_tier_5
							}
						}
						owner_under_ROOT = no
					}
				}
			}
		}
	}

	is_valid = {
		ROOT = {
			independent = yes
			NOT = { same_realm = FROM }
			primary_title = {
				OR = {
					emf_is_roman_empire = yes
					title = e_latin_empire
				}
				NOT = { has_flag = alternate_byz }
			}
			OR = {
				religion_group = christian
				religion = hellenic_pagan
				religion = hellenic_pagan_reformed
			}
			mercenary = no
		}
	}

	is_valid_title = {
		any_direct_de_jure_vassal_title = { owner_under_FROM = yes }
	}

	on_add = {
		log = "CB(imperial_reconquest): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		if = {
			limit = { ROOT = { ai = no } }
			ROOT = { prestige = -200 }
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(imperial_reconquest): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		emf_imperial_decay_victory = yes
		ROOT = {
			show_scope_change = no
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		FROM = { prestige = -100 }
	}

	on_success_title = {
		emf_cb_latb = yes
		hidden_effect = {
			if = {
				limit = {
					ROOT = { emf_imperial_government = yes }
					FROM = { emf_imperial_government = no }
				}
				ROOT = {
					any_realm_lord = {
						set_flag = emf_prior_realm_lord_of_@ROOT
					}
				}
			}
		}
		if = {
			limit = {
				owner = {
					# Can't usurp duchy if the owner isn't in the target realm!
					under_FROM = yes
					OR = {
						# Can safely usurp duchy, because owner has another title of at least the same tier
						any_demesne_title = {
							NOR = {
								title = PREVPREV
								lower_tier_than = PREVPREV
							}
						}
						# Can safely usurp duchy, because owner subrealm is 100% contained within de jure duchy
						NOT = {
							any_realm_title = {
								tier = COUNT
								NOT = { de_jure_liege = PREVPREV }
							}
						}
					}
				}
			}
			custom_tooltip = { text = ROOT_USURPS_THIS_TITLE_FROM_HOLDER }
			set_flag = emf_usurp_tmp
		}
		else_if = {
			limit = { has_holder = no }
			grant_title = { target = ROOT type = created }
		}
		custom_tooltip = {
			text = other_invasion_succ_tip
			pf_liege_change_under_title_begin_effect = yes
			ROOT = {
				vassalize_or_take_under_title = {
					title = PREV
					enemy = FROM
					type = invasion
				}
			}
			pf_liege_change_under_title_end_effect = yes
		}
		hidden_effect = {
			if = {
				limit = { has_flag = emf_usurp_tmp }
				usurp_title = { target = ROOT type = invasion }
			}
			clr_flag = emf_usurp_tmp
		}
		hidden_effect = {
			if = {
				limit = {
					ROOT = { emf_imperial_government = yes }
					FROM = { emf_imperial_government = no }
				}
				ROOT = {
					any_realm_lord = {
						limit = {
							OR = {
								is_feudal = yes
								is_tribal = yes
							}
							NOR = {
								has_flag = emf_prior_realm_lord_of_@ROOT
								primary_title = { has_flag = viceroy_exemption }
							}
						}
						opinion = { who = ROOT modifier = opinion_forced_into_imperial_government }
					}
				}
			}
			ROOT = { any_realm_lord = { clr_flag = emf_prior_realm_lord_of_@ROOT } }
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(imperial_reconquest): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = -200 }
	}

	on_reverse_demand = {
		log = "CB(imperial_reconquest): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		emf_imperial_decay_loss = yes
		ROOT = {
			show_scope_change = no
			transfer_scaled_wealth = { to = FROM value = 4.0 }
			prestige = -300
		}
		FROM = {
			show_scope_change = no
			if = {
				limit = { emf_hostile_religion_to_ROOT = yes }
				participation_scaled_piety = 100
			}
			participation_scaled_prestige = 250
			if = {
				limit = { uses_decadence = yes }
				participation_scaled_decadence = -10
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				if = {
					limit = { emf_hostile_religion_to_ROOT = yes }
					participation_scaled_piety = 100
				}
				participation_scaled_prestige = 250
				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -10
				}
			}
		}
	}

	on_invalidation_title = {
		log = "CB(imperial_reconquest): INVALIDATED: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		if = { # Prestige cost refund
			limit = { ROOT = { ai = no } }
			ROOT = { 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 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 = 2
			any_direct_de_jure_vassal_title = {
				owner_under_FROM = yes
				location = { any_neighbor_province = { owner_under_ROOT = yes } }
			}
		}
		modifier = {
			factor = 1.25
			any_direct_de_jure_vassal_title = { owner_under_ROOT = yes }
		}
		modifier = {
			factor = 1.5
			ROOT = { trait = lucky_ruler }
		}
		modifier = {
			factor = 0.5 # Keep normal preference order under holy war if that CB is available
			FROM = {
				OR = {
					NOR = {
						religion_group = ROOT
						ROOT = { emf_syncretized_religion_with_PREV = yes }
					}
					is_heresy_of = ROOT
					is_parent_religion = ROOT
				}
			}
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

embargo_cb = {
	name = CB_NAME_EMBARGO
	war_name = WAR_NAME_EMBARGO
	sprite = 19
	truce_days = 3650
	allowed_to_target_tributaries = no
	is_permanent = yes
	can_ask_to_join_war = no
	
	sort_priority = 2000

	can_use_gui = {
		emf_cb_can_use_gui = yes
	}

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

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

	on_add = {
		log = "CB(embargo_cb): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(embargo_cb): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		ROOT = { pf_prosperity_minus4_effect = yes }
		FROM = { pf_prosperity_minus4_effect = yes }
		ROOT = { embargo = FROM }
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(embargo_cb): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		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 = {
		log = "CB(embargo_cb): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		FROM = { pf_prosperity_plus4_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
				}
			}
		}
		FROM = { prestige = 100 }
	}

	on_invalidation = {
		log = "CB(embargo_cb): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 75
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 75
	}

	# ai importance placed on this CB: scope is the targeted title, ROOT is the attacking character, FROM is the defending character
	ai_will_do = {
		factor = 1
		modifier = { #Focus on Coronation first.
			factor = 0.1
			ROOT = {
				emf_uses_furry_coronation = yes
				emf_is_crowned = no
			}
		}
		modifier = {
			factor = 0
			ROOT = {
				opinion = {
					who = FROM
					value = 25
				}
			}
		}
		modifier = {
			factor = 0.5
			ROOT = {
				opinion = {
					who = FROM
					value = 0
				}
			}
		}
		modifier = {
			factor = 0.5
			ROOT = {
				opinion = {
					who = FROM
					value = -25
				}
			}
		}
	}
}

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

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			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
					}
				}
			}
		}
	}

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

	can_use_title = {
		tier = COUNT
		location = { port = yes }
		has_trade_post = yes
		trade_post_owner = {
			OR = {
				character = FROM
				is_liege_or_above = FROM
			}
			is_patrician = yes # Workaround for bug where sieging down a trade post owned by a non-patrician will destroy the trade post and invalidate the CB
		}
		holder_scope = { NOT = { character = FROM } }
		emf_cb_can_use_title = yes
	}

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

	is_valid_title = {
		has_trade_post = yes
		trade_post_owner = {
			OR = {
				character = FROM
				is_liege_or_above = FROM
			}
		}
		holder_scope = { NOT = { character = FROM } }
	}

	on_add = {
		log = "CB(seize_trade_post): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(seize_trade_post): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		ROOT = { pf_prosperity_plus2_effect = yes }
		FROM = { pf_prosperity_minus2_effect = yes }
	}

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

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(seize_trade_post): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = -50 }
		FROM = { participation_scaled_prestige = 25 }
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 25 }
		}
	}

	on_reverse_demand = {
		log = "CB(seize_trade_post): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		FROM = { pf_prosperity_plus2_effect = yes }
		ROOT = {
			prestige = -100
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
		}
		FROM = { participation_scaled_prestige = 50 }
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_invalidation = {
		log = "CB(seize_trade_post): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 25
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 25
	}

	# ai importance placed on this CB: scope is the targeted title, ROOT is the attacking character, FROM is the defending character
	ai_will_do = {
		factor = 1
		modifier = { #Focus on Coronation first.
			factor = 0.1
			ROOT = {
				emf_uses_furry_coronation = yes
				emf_is_crowned = no
			}
		}
		modifier = {
			factor = 0.25
			ROOT = { NOT = { same_realm = FROM } }
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

viking_invasion = {
	name = CB_NAME_VIKING_INVASION
	war_name = WAR_NAME_VIKING_INVASION
	sprite = 20
	truce_days = 365
	hostile_against_others = no # Vikings share in the spoils...
	is_permanent = yes
	is_holy_war = yes
	can_ask_to_join_war = yes
	check_de_jure_tier = KING # this scans all de jure kingdoms for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	apply_short_occ_mod = no # Do not apply the 'recently_conquered' modifier to Holdings
	allowed_to_target_tributaries = no
	
	sort_priority = 3000

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			show_scope_change = no
			conditional_tooltip = {
				trigger = { is_patrician = yes }
				custom_tooltip = {
					text = emf_mr_ctt_extended_cbs_unlocked
					has_game_rule = { name = mr_cb_restriction value = off }
				}
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			has_opinion_modifier = {
				who = FROM
				modifier = preparing_to_invade
			}
			NOT = { is_liege_or_above = FROM }
			mercenary = no
			trigger_if = {
				limit = {
					ai = yes
					is_patrician = yes
				}
				has_game_rule = { name = mr_cb_restriction value = off }
			}
		}
	}

	can_use_title = {
		emf_cb_can_use_de_jure_title = yes
		is_valid_viking_invasion_target = FROM # Checks that FROM holds the right amount of territory in the kingdom
	}
	
	is_valid = {
		ROOT = {
			show_scope_change = no
			conditional_tooltip = {
				trigger = { is_patrician = yes }
				custom_tooltip = {
					text = emf_mr_ctt_extended_cbs_unlocked
					has_game_rule = { name = mr_cb_restriction value = off }
				}
			}
		}
	}

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

	on_add = {
		log = "CB(viking_invasion): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(viking_invasion): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		emf_imperial_decay_victory = yes
		ROOT = {
			prestige = 500
			piety = 250
			religion_authority = { modifier = won_viking_invasion }
			hidden_tooltip = {
				set_global_flag = emf_latb_upon_gain
				occupy_minors_of_occupied_settlements = FROM
				gain_all_occupied_titles = {
					who = FROM
					type = invasion
				}
				clr_global_flag = emf_latb_upon_gain
				if = {
					limit = { has_nickname = no }
					random_list = {
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_victorious }
						10 = { give_nickname = nick_the_fearless }
						10 = { give_nickname = nick_the_brave }
						10 = { give_nickname = nick_haardraade }
						10 = { give_nickname = nick_the_viking }
						10 = { give_nickname = nick_the_dragon }
						10 = { give_nickname = nick_the_ironside }
					}
				}
			}
			participation_scaled_prestige = 300
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 300 }
		}
		FROM = { prestige = -500 }
	}

	on_success_title = {
		emf_cb_latb = yes
		custom_tooltip = {
			text = emf_cb_ctt_viking_invasion_success
			hidden_tooltip = {
				save_event_target_as = emf_cb_title
				pf_liege_change_under_title_begin_effect = yes
				ROOT = {
					vassalize_or_take_under_title_destroy_duchies = {
						title = PREV
						enemy = FROM
						type = invasion
					}
				}
				pf_liege_change_under_title_end_effect = yes
				ROOT = {
					# Settle the special invasion forces
					disband_event_forces = prepared_invasion
					random_demesne_province = {
						limit = {
							de_jure_liege_or_above = PREVPREV
							OR = {
								NOT = { culture = ROOT }
								NOT = { religion = ROOT }
							}
						}
						culture = ROOT
						religion = ROOT
					}
					# Remove conquest modifiers and refill levies
					character_event = { id = emf_cb.10 }
				}
				if = {
					limit = { FROM = { is_liege_or_above = ROOT } }
					FROM = { imprison = ROOT }
				}
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(viking_invasion): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		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 = {
		log = "CB(viking_invasion): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		emf_imperial_decay_loss = yes
		ROOT = {
			prestige = -500
			piety = -250
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
			hidden_tooltip = { disband_event_forces = prepared_invasion }
			religion_authority = { modifier = lost_viking_invasion }
		}
		FROM = {
			prestige = 300
			if = {
				limit = {
					has_dlc = "Legacy of Rome"
					emf_has_roman_empire = yes
					OR = { 
						religion_group = christian
						religion = hellenic_pagan
						religion = hellenic_pagan_reformed
					}
				}
				hidden_tooltip = {
					character_event = {
						id = LoR.30
						days = 12
					}
				}
			}
			participation_scaled_prestige = 250
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 250 }
		}
	}

	on_invalidation = {
		log = "CB(viking_invasion): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	attacker_ai_victory_worth = {
		factor = 200
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = {
		factor = 1
		modifier = {
			factor = 0
			ROOT = { has_flag = no_invasions }
		}
		modifier = {
			factor = 0
			is_patrician = yes
			NOT = { has_game_rule = { name = mr_cb_restriction value = off } }
		}
	}
}

pagan_subjugation = {
	name = CB_NAME_PAGAN_SUBJUGATION
	war_name = WAR_NAME_PAGAN_SUBJUGATION
	sprite = 8
	truce_days = 365
	is_permanent = yes
	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no
	defender_unoccupied_warscore = yes
	
	sort_priority = 2000

	can_use_gui = {
		emf_cb_pagan_subjugation_can_use_gui = yes
	}

	can_use = {
		emf_cb_can_use = yes
		emf_cb_pagan_subjugation_basic_can_use = yes
		ROOT = { religion = FROM }
	}

	is_valid = {
		emf_cb_pagan_subjugation_is_valid = yes
	}

	on_add = {
		log = "CB(pagan_subjugation): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_pagan_subjugation_on_add = yes
	}

	on_success = {
		log = "CB(pagan_subjugation): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_pagan_subjugation_on_success = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(pagan_subjugation): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_pagan_subjugation_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(pagan_subjugation): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_pagan_subjugation_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(pagan_subjugation): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { remove_character_modifier = launched_subjugation }
	}

	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.15
			NOT = { FROM = { lower_tier_than = ROOT } }
		}
	}

	ai_will_do = {
		factor = 1
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
		modifier = {
			factor = 0
			ROOT = {
				trigger_if = {
					limit = { emf_has_swmh = yes }
					culture = old_hungarian
				}
				trigger_else = {
					culture = hungarian
				}
				religion_group = pagan_group
				capital_scope = { kingdom = { title = k_hungary } }
			}
			NOT = { has_global_flag = emf_conquest_hungary_completed }
			has_global_flag = emf_magyar_migration_completed
			emf_alternate_start = no
		}
		modifier = {
			factor = 0
			OR = {
				ROOT = {
					top_liege = {
						trigger_if = {
							limit = { emf_has_swmh = yes }
							culture = old_hungarian
						}
						trigger_else = {
							culture = hungarian
						}
						religion_group = pagan_group
					}
				}
				FROM = {
					top_liege = {
						trigger_if = {
							limit = { emf_has_swmh = yes }
							culture = old_hungarian
						}
						trigger_else = {
							culture = hungarian
						}
						religion_group = pagan_group
					}
				}
			}
			NOT = { has_global_flag = emf_magyar_migration_completed }
			NOT = { year = 920 }
			emf_alternate_start = no
		}
	}
}

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
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no
	is_independence = yes

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

	can_use_title = {
		tier = COUNT
		emf_cb_can_use_title = yes
	}

	is_valid_title = {
		owner_under_FROM = yes
	}

	on_add = {
		log = "CB(peasant_revolt): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(peasant_revolt): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
	}

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

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(peasant_revolt): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		FROM = {
			prestige = 10
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor
					who = ROOT
					months = 1200
				}
			}
		}
		ROOT = {
			clear_wealth = yes
			imprison = FROM
			hidden_tooltip = {
				add_character_modifier = {
					name = broken_spirit
					days = -1
				}
			}
		}
	}

	on_reverse_demand = {
		log = "CB(peasant_revolt): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		FROM = {
			prestige = 20
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor
					who = ROOT
					months = 1200
				}
				add_character_modifier = {
					name = crushed_revolt
					days = 3650
				}
			}
		}
		ROOT = {
			clear_wealth = yes
			imprison = FROM
			hidden_tooltip = {
				add_character_modifier = {
					name = broken_spirit
					days = -1
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(peasant_revolt): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

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

heretic_revolt = {
	name = CB_NAME_HERETIC_UPRISING
	war_name = WAR_NAME_HERETIC_UPRISING
	sprite = 7
	truce_days = 365
	hostile_against_others = yes
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no
	check_de_jure_tier = DUKE

	can_use = {
		emf_cb_can_use = yes
		ROOT = { has_flag = heretic_revolter }
	}

	is_valid = { FROM = { NOT = { religion = ROOT } } }

	on_add = {
		log = "CB(heretic_revolt): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(heretic_revolt): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		ROOT = {
			prestige = 200
			piety = 100
			set_global_flag = emf_latb_upon_gain
			occupy_minors_of_occupied_settlements = FROM
			gain_all_occupied_titles = {
				who = FROM
				type = revolt
			}
			clr_global_flag = emf_latb_upon_gain
			clr_flag = heretic_revolter
		}
		FROM = {
			prestige = -500
			piety = -250
			religion_authority = { modifier = lost_heretic_revolt }
		}
	}
	
	on_success_title = {
		ROOT = {
			vassalize_or_take_under_title = {
				title = PREV
				enemy = FROM
				same_religion = yes # Only vassalize rulers of my religion
				is_religious = yes
				type = holy_war
			}
		}
		if = {
			limit = {
				controls_religion = no
				holder_scope = {
					OR = {
						character = FROM
						is_liege_or_above = FROM
					}
				}
			}
			usurp_title = { target = ROOT type = holy_war }
		}
		if = {
			limit = {
				has_holder = no
			}
			usurp_title = { target = ROOT type = holy_war }
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(heretic_revolt): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			clear_wealth = yes
			imprison = FROM
			clr_flag = heretic_revolter
			hidden_tooltip = {
				add_character_modifier = {
					name = broken_spirit
					days = -1
				}
			}
		}
		FROM = {
			prestige = 50
			piety = 25
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor
					who = ROOT
					months = 1200
				}
			}
			religion_authority = { modifier = won_heretic_revolt }
		}
	}

	on_reverse_demand = {
		log = "CB(heretic_revolt): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		ROOT = {
			clear_wealth = yes
			imprison = FROM
			clr_flag = heretic_revolter
			hidden_tooltip = {
				add_character_modifier = {
					name = broken_spirit
					days = -1
				}
			}
		}
		FROM = {
			prestige = 100
			piety = 50
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor
					who = ROOT
					months = 1200
				}
				add_character_modifier = {
					name = crushed_revolt
					days = 3650
				}
			}
			religion_authority = { modifier = won_heretic_revolt }
		}
	}

	on_invalidation = {
		log = "CB(heretic_revolt): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	attacker_ai_victory_worth = {
		factor = 100
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 150
	}
}

religious_revolt = {
	name = CB_NAME_HERETIC_UPRISING
	war_name = WAR_NAME_HERETIC_UPRISING
	sprite = 7
	truce_days = 365
	hostile_against_others = yes
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no
	check_de_jure_tier = DUKE

	can_use = { always = no }

	is_valid = { always = no }

	on_invalidation = {
		log = "CB(religious_revolt): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	attacker_ai_victory_worth = {
		factor = 100
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 150
	}
}

liberation_revolt = {
	name = CB_NAME_LIBERATION_REVOLT
	war_name = WAR_NAME_LIBERATION_REVOLT
	sprite = 16
	truce_days = 365
	hostile_against_others = yes
	defender_can_call_allies = no
	check_de_jure_tier = KING # this scans all dejure duchies for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	press_claim = yes
	can_ask_to_join_war = no
	is_independence = yes

	can_use = {
		emf_cb_can_use = yes
		ROOT = { has_flag = nationalist_rebel }
	}

	can_use_title = {
		emf_cb_can_use_de_jure_title = yes
		tier = KING
		has_holder = no
	}

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

	is_valid_title = { has_holder = no }

	on_add = {
		log = "CB(liberation_revolt): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(liberation_revolt): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		ROOT = {
			prestige = 1000
			set_global_flag = emf_latb_upon_gain
			occupy_minors_of_occupied_settlements = FROM
			gain_all_occupied_titles = {
				who = FROM
				type = revolt
			}
			clr_global_flag = emf_latb_upon_gain
			clr_flag = nationalist_rebel
			give_nickname = nick_the_liberator
		}
		FROM = { prestige = -500 }
	}

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

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(liberation_revolt): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		FROM = {
			prestige = 100
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor
					who = ROOT
					months = 1200
				}
			}
		}
		ROOT = {
			clear_wealth = yes
			imprison = FROM
			clr_flag = nationalist_rebel
			hidden_tooltip = {
				add_character_modifier = {
					name = broken_spirit
					days = -1
				}
				remove_trait = inspiring_leader
			}
		}
	}

	on_reverse_demand = {
		log = "CB(liberation_revolt): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		FROM = {
			prestige = 200
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor
					who = ROOT
					months = 1200
				}
				add_character_modifier = {
					name = crushed_major_revolt
					days = 3650
				}
			}
		}
		ROOT = {
			clear_wealth = yes
			imprison = FROM
			clr_flag = nationalist_rebel
			hidden_tooltip = {
				add_character_modifier = {
					name = broken_spirit
					days = -1
				}
				remove_trait = inspiring_leader
			}
		}
	}

	on_invalidation = {
		log = "CB(liberation_revolt): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	attacker_ai_victory_worth = {
		factor = 100
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

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

	can_use = {
		ROOT = { has_flag = duchy_adventurer }
	}

	can_use_title = {
		emf_cb_can_use_de_jure_title = yes
	}

	is_valid_title = {
		any_direct_de_jure_vassal_title = { owner_under_FROM = yes }
	}

	on_add = {
		log = "CB(duchy_adventure): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(duchy_adventure): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		ROOT = {
			prestige = 250
			hidden_tooltip = {
				clr_flag = duchy_adventurer
				give_nickname = nick_the_conqueror
			}
		}
		FROM = { prestige = -250 }
	}

	on_success_title = {
		save_event_target_as = emf_cb_title
		emf_cb_latb = 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
					}
					# Usurp a county if ROOT ended up vassalizing everyone
					if = {
						limit = { is_landed = no }
						random_realm_title = {
							limit = {
								tier = COUNT
								de_jure_liege_or_above = event_target:emf_cb_title
							}
							usurp_title_plus_barony_if_unlanded = { target = ROOT type = host }
							any_de_jure_vassal_title = {
								limit = { owner_under_FROM = yes }
								usurp_title_plus_barony_if_unlanded = { target = ROOT type = host }
							}
						}
					}
				}
			}
		}
		pf_liege_change_under_title_end_effect = yes
		ROOT = {
			hidden_tooltip = {
				if = { # Special case: Timur succeeds in his first conquest
					limit = { has_flag = timur_himself }
					narrative_event = { id = 60306 days = 1 }
				}
				else_if = { # Special case: Sabuktigin succeeds in his first conquest
					limit = { has_flag = sabuktigin_himself }
					narrative_event = { id = 60406 days = 1 }
				}
				else_if = { # Special case: Sultanate of Rum creator succeeds in his first conquest
					limit = { has_flag = rum_creator }
					character_event = { id = 60610 days = 600 random = 300 }
				}
			}
			emf_check_adventurer_government = yes
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(duchy_adventure): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		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 = duchy_adventure
				disband_event_forces = raiding_adventure
				disband_event_forces = timur_duchy_adventure
				disband_event_forces = sabuktigin_duchy_adventure
				clr_flag = duchy_adventurer
			}
		}
		ROOT = { prestige = -100 }
	}

	on_reverse_demand = {
		log = "CB(duchy_adventure): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		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 = duchy_adventure
				disband_event_forces = raiding_adventure
				disband_event_forces = timur_duchy_adventure
				disband_event_forces = sabuktigin_duchy_adventure
				clr_flag = duchy_adventurer
			}
		}
	}

	on_attacker_leader_death = {
		end_war = invalid
	}

	on_invalidation = {
		log = "CB(duchy_adventure): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			hidden_tooltip = {
				disband_event_forces = duchy_adventure
				disband_event_forces = raiding_adventure
				disband_event_forces = timur_duchy_adventure
				disband_event_forces = sabuktigin_duchy_adventure
				clr_flag = duchy_adventurer
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

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

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

	is_valid_title = {
		any_de_jure_vassal_title = { tier = COUNT owner_under_FROM = yes }
	}

	on_add = {
		log = "CB(kingdom_adventure): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}
	
	on_success = {
		log = "CB(kingdom_adventure): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { 
			prestige = 500
			piety = 250
			hidden_tooltip = {
				clr_flag = duchy_adventurer
				clr_flag = do_not_disturb
				if = {
					limit = {
						has_nickname = no
					}
					FROM = {
						save_event_target_as = nickname_target
					}
					ROOT = { 
						save_event_target_as = nickname_receiver
					}
					if = {
						limit = {
							NOT = {
								has_flag = is_child_of_destiny
							}
						}
						random_list = {
							5 = { give_nickname = nick_the_victorious }
							5 = { give_nickname = nick_the_conqueror }
							5 = { give_nickname = nick_the_dragon }
							10 = { give_nickname = nick_the_rel_group }
							10 = { give_nickname = nick_the_tiger }
							10 = { give_nickname = nick_the_world_burner }
							10 = { 
								give_nickname = nick_the_scourge_of_god
								modifier = {
									factor = 0
									event_target:nickname_target = { religion = ROOT }
								}
							}
						}
					}
				}
			}
		}
		FROM = {
			prestige = -500
			piety = -250			
		}
	}

	on_success_title = {
		pf_liege_change_under_title_begin_effect = yes
		custom_tooltip = {
			text = other_invasion_succ_tip
			hidden_tooltip = {
				if = { # Special stuff for player Child of Destiny (AI Children do not have this flag)
					limit = {
						ROOT = { has_flag = child_of_destiny_outwards_focus }
					}
					FROM = { # Ensure that the Child of Destiny always gets one County in the targeted Kingdom (mainly useful when testing with yesmen, but can also happen in rare occurences where the target is imprisoned)
						random_realm_title = {
							limit = {
								tier = COUNT
								de_jure_liege_or_above = PREVPREV
							}
							usurp_title = { target = ROOT type = host }
						}
					}
					ROOT = {
						liege = { save_event_target_as = old_liege_target }
						if = { # Abandon all old titles if you were a vassal
							limit = {
								independent = no
							}
							any_demesne_title = {
								limit = {
									NOR = {
										title = PREVPREV
										de_jure_liege_or_above = PREVPREV
									}
								}
								grant_title = event_target:old_liege_target
							}
							any_vassal = {
								limit = {
									capital_scope = {
										NOR = {
											title = PREVPREVPREV
											de_jure_liege_or_above = PREVPREVPREV
										}
									}
								}
								set_defacto_liege = event_target:old_liege_target
								emf_liege_change = yes
							}
						}
					}
				}
				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_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_flag = rum_creator
							}
						}
					}
					if = {
						limit = {
							ROOT = {
								NOT = {
									has_flag = is_child_of_destiny
								}
							}
						}
						ROOT = {
							create_title = {
								tier = KING
								landless = no
								temporary = no
								custom_created = yes
								culture = ROOT
								holder = ROOT
								name = "INDIAN_MUSLIM_KINGDOM"
								base_title = PREV
								copy_title_laws = yes
							}
						}
					}
					else = {
						ROOT = {
							save_event_target_as = target_name
							create_title = {
								tier = KING
								landless = no
								temporary = no
								custom_created = yes
								culture = ROOT
								holder = ROOT
								name = "DESTINY_KINGDOM"
								base_title = PREV
								copy_title_laws = yes
							}
							capital_scope = {
								county = {
									save_event_target_as = coa_target
								}
							}
							primary_title = {
								set_coa = event_target:coa_target
							}
						}
					}
				}
				if = {
					limit = {
						ROOT = { has_flag = rum_creator }
					}
					k_rum = {
						show_scope_change = no
						grant_title = ROOT
						if = {
							limit = { has_random_dejure = no }
							k_anatolia = {
								any_direct_de_jure_vassal_title = {
									show_scope_change = no
									de_jure_liege = k_rum
								}
								if = {
									limit = { has_holder = no }
									activate_title = { title = THIS status = no }
								}
							}
						}
					}
				}
				
				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
					emf_liege_change = yes
				}

				hidden_effect = {
					if = {
						limit = {
							ROOT = {
								has_flag = is_child_of_destiny
								NOT = {
									religion_group = muslim
								}
							}
						}
						ROOT = {
							any_demesne_title = {
								limit = {
									higher_tier_than = COUNT
								}
								add_law = { law = succ_primogeniture opinion_effect = no }
							}
						}
					}
				}
			}
		}
		pf_liege_change_under_title_end_effect = yes
		
		ROOT = {
			hidden_tooltip = {
				if = {
					limit = {
						has_flag = rum_creator
					}
					capital_scope = {
						culture = ROOT
					}
				}
				else_if = {
					limit = {
						has_flag = is_child_of_destiny
					}
					character_event = { id = HF.303 days = 3 }
				}
				else = {
					capital_scope = {
						ROOT = { culture = PREV }
					}
					any_child = {
						limit = {
							is_ruler = no
							ai = yes
							#vassal_of = ROOT
						}
						culture = ROOT
					}
					any_courtier = {
						limit = {
							is_ruler = no
							ai = yes
						}
						culture = ROOT
					}
				}

				if = { # Give bloodline to Child of Destiny
					limit = {
						has_flag = is_child_of_destiny
						higher_real_tier_than = DUKE
						NOT = {
							any_owned_bloodline = {
								has_bloodline_flag = created_bloodline
							}
						}
					}
					character_event = { id = HF.304 days = 14 }
				}
				
				# Special case: Sultanate of Rum might start yet another war
				if = {
					limit = {
						has_flag = rum_creator
					}
					character_event = {
						id = 60610
						days = 600
						random = 300
					}
				}
			}
		}
	}

	on_fail = {
		log = "CB(kingdom_adventure): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		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_flag = duchy_adventurer
				if = {
					limit = {
						has_flag = is_child_of_destiny
					}
					remove_trait = disinherited
				}
			}
		}
	
		ROOT = { prestige = -100 }

		if = {
			limit = {
				ROOT = {
					has_flag = is_child_of_destiny
				}
			}
			set_global_flag = child_of_destiny_invasion_fail@ROOT
		}
	}

	on_reverse_demand = {
		log = "CB(kingdom_adventure): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		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_flag = duchy_adventurer
				if = {
					limit = {
						has_flag = is_child_of_destiny
					}
					remove_trait = disinherited
				}
			}
		}
		
		if = {
			limit = {
				ROOT = {
					has_flag = is_child_of_destiny
				}
			}
			set_global_flag = child_of_destiny_invasion_reverse_demand@ROOT
		}
	}
	
	on_attacker_leader_death = {
		ROOT = {
			hidden_tooltip = {
				disband_event_forces = yes
				clr_flag = duchy_adventurer
			}
		}
		end_war = invalid
	}

	on_invalidation = {
		log = "CB(kingdom_adventure): INVALIDATED: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			clr_flag = child_of_destiny_made_player_choice
			clr_flag = child_of_destiny_adventure
		}
	}

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

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

pagan_holy_war = {
	name = CB_NAME_RELIGIOUS
	war_name = WAR_NAME_RELIGIOUS
	sprite = 9
	truce_days = 1825
	is_permanent = no

	can_use = { always = no }

	can_use_title = { always = no }

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

caliphal_subjugation = {
	name = CB_NAME_PAGAN_SUBJUGATION
	war_name = WAR_NAME_PAGAN_SUBJUGATION
	sprite = 8
	truce_days = 365
	hostile_against_others = yes
	is_permanent = yes
	can_ask_to_join_war = yes
	check_de_jure_tier = KING # this scans all de jure kingdoms for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	allowed_to_target_tributaries = no
	sort_priority = 1000

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			custom_tooltip = {
				text = NEEDS_200_PIETY_COST
				piety = 200
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			OR = {
				ai = yes
				multiplayer = no
				NOT = {
					has_game_rule = {
						name = multiplayer_invasions
						value = off
					}
				}
			}
			religion_group = muslim
			controls_religion = yes
			is_nomadic = no
			OR = {
				has_horde_culture = no # Should use tribal invasion instead
				has_dlc = "Horse Lords"
			}
			NOT = { has_character_modifier = launched_subjugation }
			mercenary = no
		}
		FROM = {
			religion = ROOT
			NOT = { is_offmap_governor = yes }
		}
	}

	can_use_title = {
		emf_cb_can_use_de_jure_title = yes
		FROM = { any_realm_province = { de_jure_liege_or_above = PREVPREV } }
	}

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

	on_add = {
		log = "CB(caliphal_subjugation): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			piety = -200
			add_character_modifier = {
				name = launched_subjugation
				days = 3650
				hidden = yes
			}
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(caliphal_subjugation): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		ROOT = { pf_tradition_plus4_effect = yes }
		ROOT = {
			prestige = 100
			decadence = -5
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		FROM = { prestige = -200 }
	}

	on_success_title = {
		emf_cb_latb = yes
		pf_liege_change_under_title_begin_effect = yes
		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_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(caliphal_subjugation): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		FROM = {
			prestige = 50
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		ROOT = { prestige = -100 }
	}

	on_reverse_demand = {
		log = "CB(caliphal_subjugation): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = { pf_tradition_minus4_effect = yes }
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
		}
		FROM = {
			prestige = 100
			if = {
				limit = {
					has_dlc = "Legacy of Rome"
					emf_has_roman_empire = yes
					OR = { 
						religion_group = christian
						religion = hellenic_pagan
						religion = hellenic_pagan_reformed
					}
				}
				hidden_tooltip = {
					character_event = {
						id = LoR.30
						days = 12
					}
				}
			}
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_invalidation = {
		log = "CB(caliphal_subjugation): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { remove_character_modifier = launched_subjugation }
	}

	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 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 # Prefer using CB's without on_add costs
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

muslim_county_conquest = {
	name = CB_NAME_COUNTY_CONQUEST
	war_name = WAR_NAME_COUNTY_CONQUEST
	sprite = 16
	truce_days = 1825
	hostile_against_others = yes
	is_permanent = yes
	check_all_titles = yes # if permanent, setting this to true will check against all of someones titles, including vassal held titles
	can_ask_to_join_war = no
	allowed_to_target_tributaries = no
	sort_priority = 700

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = NEEDS_50_PIETY_COST
				piety >= 50
			}
			conditional_tooltip = {
				trigger = { is_patrician = yes }
				custom_tooltip = {
					text = emf_mr_ctt_extended_cbs_unlocked
					has_game_rule = { name = mr_cb_restriction value = off }
				}
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		FROM = { religion_group = muslim }
		ROOT = {
			religion_group = muslim
			NOT = { is_liege_or_above = FROM }
			NOT = { is_vassal_or_below = FROM }
			mercenary = no
		}
	}

	can_use_title = {
		emf_cb_county_conquest_basic_can_use_title = yes
		location = { any_neighbor_province = { owner_under_ROOT = yes } }
		emf_cb_can_use_title = yes
	}

	is_valid_title = {
		owner_under_FROM = yes
	}

	on_add = {
		log = "CB(muslim_county_conquest): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { piety = -50 }
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(muslim_county_conquest): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
	}

	on_success_title = {
		emf_cb_latb = yes
		if = {
			limit = {
				holder_scope = {
					tier = COUNT
					NOT = { num_of_count_titles = 2 }
					lower_tier_than = ROOT
					in_revolt = no
					OR = {
						is_merchant_republic = yes
						is_patrician = no
					}
				}
			}
			holder_scope = {
				emf_ROOT_subjugate_ruler = yes
			}
		}
		if = {
			limit = {
				holder_scope = {
					NAND = {
						tier = COUNT
						NOT = { num_of_count_titles = 2 }
						lower_tier_than = ROOT
						in_revolt = no
						OR = {
							is_merchant_republic = yes
							is_patrician = no
						}
					}
				}
			}
			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 }
					owner_under_FROM = yes
				}
				usurp_title_plus_barony_if_unlanded = {
					target = ROOT
					type = invasion
				}
			}
		}
		ROOT = { participation_scaled_prestige = 100 }
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(muslim_county_conquest): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = -100 }
		FROM = { participation_scaled_prestige = 50 }
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		log = "CB(muslim_county_conquest): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = { participation_scaled_prestige = 100 }
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_invalidation = {
		log = "CB(muslim_county_conquest): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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 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 # Prefer using CB's without on_add costs
		}
		modifier = {
			factor = 0
			ROOT = {
				has_flag = is_temujin
				has_landed_title = e_mongol_empire
				demesne_size = 1
			}
			FROM = {
				is_offmap_governor = offmap_china
			}
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_nomadic = no
				NOT = {
					is_tribal = yes
				}
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}
				}
			}
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_tribal = yes
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
							holding_type = tribal
						}
					}
				}
			}
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

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
	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
	can_ask_to_join_war = no
	allowed_to_target_tributaries = no
	allow_distant = yes
	
	sort_priority = 700

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = emf_ctt_no_holy_war_cooldown
				hidden_tooltip = {
					OR = {
						has_game_rule = { name = no_cooldowns value = on }
						NOT = { has_character_modifier = emf_cb_mod_holy_war_cooldown }
					}
				}
			}
			conditional_tooltip = {
				trigger = { is_patrician = yes }
				custom_tooltip = {
					text = emf_mr_ctt_extended_cbs_unlocked
					has_game_rule = { name = mr_cb_restriction value = off }
				}
			}
			prestige = 200
			piety = 100
			OR = {
				independent = yes
				NOR = {
					trait = content
					trait = craven
				}
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			religion_group = pagan_group
			OR = {
				AND = {
					is_nomadic = yes
					FROM = {
						OR = {
							is_nomadic = yes
							is_tribal = yes
						}
					}
				}
				NOR = {
					religion = FROM
					emf_syncretized_religion_with_FROM = yes
				}
			}
			NOT = { same_realm = FROM }
			mercenary = no
		}
	}

	can_use_title = {
		emf_cb_county_conquest_basic_can_use_title = yes
		OR = {
			AND = {
				OR = {
					ROOT = { religion = norse_pagan }
					ROOT = { religion = norse_pagan_reformed }
				}
				ROOT = { NOT = { has_flag = no_invasions } }
				location = { port = yes }
				has_global_flag = viking_age_started
			}
			location = { any_neighbor_province = { owner_under_ROOT = yes } }
		}
		emf_cb_can_use_title = yes
	}

	is_valid_title = {
		owner_under_FROM = yes
		OR = {
			ROOT = { is_nomadic = yes }
			AND = {
				ROOT = {
					religion_group = pagan_group
					NOT = { same_realm = FROM }
					NOT = { emf_syncretized_religion_with_FROM = yes }
				}
				FROM = { NOT = { religion = ROOT } }
			}
		}
	}

	on_add = {
		log = "CB(pagan_county_conquest): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		if = {
			limit = { has_game_rule = { name = no_cooldowns value = off } }
			ROOT = {
				custom_tooltip = {
					text = emf_cb_ctt_holy_war_cooldown_3yr
					hidden_tooltip = {
						add_character_modifier = {
							name = emf_cb_mod_holy_war_cooldown
							duration = 1095
						}
					}
				}
			}
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(pagan_county_conquest): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
	}

	on_success_title = {
		emf_cb_latb = 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 = {
						under_FROM = yes
						AND = {
							is_liege_or_above = ROOT
							NOT = { culture = ROOT }
							NOT = { religion = ROOT }
						}
					}
				}
			}
			show_scope_change = no
			usurp_title = { target = ROOT type = invasion }
		}
		ROOT = {
			show_scope_change = no
			participation_scaled_prestige = 100
			participation_scaled_piety = 50
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = 100
				participation_scaled_piety = 50
			}
		}
		if = {
			limit = { NOT = { FROM = { religion = ROOT } } }
			ROOT = {
				show_scope_change = no
				religion_authority = { modifier = won_county_conquest }
			}
		}

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

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

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(pagan_county_conquest): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			show_scope_change = no
			prestige = -100
		}
		if = {
			limit = { NOT = { FROM = { religion = ROOT } } }
			ROOT = {
				show_scope_change = no
				religion_authority = { modifier = lost_county_conquest }
			}
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

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

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

	on_reverse_demand = {
		log = "CB(pagan_county_conquest): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		ROOT = {
			show_scope_change = no
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		if = {
			limit = { NOT = { FROM = { religion = ROOT } } }
			ROOT = {
				show_scope_change = no
				religion_authority = { modifier = lost_county_conquest }
			}
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 100
			participation_scaled_piety = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_prestige = 100
				participation_scaled_piety = 50
			}
		}
	}

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

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

	on_invalidation = {
		log = "CB(pagan_county_conquest): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { remove_character_modifier = emf_cb_mod_holy_war_cooldown }
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = {
		factor = 1

		modifier = {
			factor = 4.0
			ROOT = {
				OR = {
					religion = norse_pagan
					religion = norse_pagan_reformed
				}
			}
			location = {
				OR = {
					region = world_europe_north
					region = custom_russia
				}
			}
		}
		modifier = {
			factor = 2.0
			ROOT = {
				OR = {
					religion = norse_pagan
					religion = norse_pagan_reformed
				}
			}
			has_global_flag = viking_age_started
			location = {
				OR = {
					region = world_europe_west_brittania
					region = world_europe_west_francia
				}
			}
		}
		modifier = {
			factor = 2.0
			ROOT = {
				OR = {
					religion = norse_pagan
					religion = norse_pagan_reformed
				}
			}
			has_global_flag = viking_age_started
			FROM = { NOT = { religion_group = pagan_group } }
			location = { port = yes }
		}
		modifier = {
			factor = 0
			ROOT = {
				has_flag = is_temujin
				has_landed_title = e_mongol_empire
				demesne_size = 1
			}
			FROM = {
				is_offmap_governor = offmap_china
			}
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_nomadic = no
				NOT = {
					is_tribal = yes
				}
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}
				}
			}
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_tribal = yes
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
							holding_type = tribal
						}
					}
				}
			}
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

dejure_county_claim = {
	name = CB_NAME_DEJURECLAIM
	war_name = WAR_NAME_DEJURECLAIM
	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
	infamy_modifier = 0.5
	
	sort_priority = 700

	can_use_gui = {
		emf_cb_can_use_gui = yes
		FROM = {
			OR = {
				is_merchant_republic = no
				NOT = { same_realm = ROOT }
			}
			OR = {
				NOT = { has_dlc = "Sons of Abraham" }
				emf_cb_unexpelled_holy_order = no
				NOT = { religion = ROOT }
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			NOT = {
				is_vassal_or_below_of = FROM
			}
		}
	}

	can_use_title = {
		emf_cb_county_conquest_basic_can_use_title = yes
		emf_cb_can_use_title = yes
		OR = {
			any_de_jure_liege_title = { holder = ROOT }
			AND = {
				ROOT = { has_landed_title = k_france }
				de_jure_liege_or_above = k_aquitaine
				k_aquitaine = { has_holder = no }
			}
		}
		NAND = {
			ROOT = { rightful_religious_head = FROM }
			OR = {
				is_holy_site = FROM
				any_direct_de_jure_vassal_title = {
					is_holy_site = FROM
					owner_under_FROM = yes
				}
			}
		}
		# Disable if emf_dejure_duchy_claim would be valid for the duchy that this county is within
		# That is, use emf_dejure_duchy_claim wherever possible instead of dejure_county_claim
		dejure_liege_title = { emf_cb_dejure_duchy_claim_basic_can_use_title = no }
		# Don't show this when the [rare] emf_dejure_kingdom_claim could be used
		NOT = { kingdom = { emf_cb_dejure_kingdom_claim_basic_can_use_title = yes } }
	}

	is_valid_title = {
		owner_under_FROM = yes
		OR = {
			any_de_jure_liege_title = { holder = ROOT }
			AND = {
				ROOT = { has_landed_title = k_france }
				de_jure_liege_or_above = k_aquitaine
				k_aquitaine = { has_holder = no }
			}
		}
	}

	on_add = {
		log = "CB(dejure_county_claim): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(dejure_county_claim): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
	}

	on_success_title = {
		emf_cb_latb = yes
		if = {
			limit = { empire = { holder = ROOT } }
			emf_imperial_decay_victory = yes
		}
		if = {
			limit = {
				owner = {
					tier = COUNT
					is_patrician = no
					num_of_count_titles < 2
				}
			}
			owner = {
				emf_ROOT_subjugate_ruler = yes
			}
		}
		if = {
			limit = {
				owner = {
					OR = {
						higher_tier_than = COUNT
						num_of_count_titles >= 2
						is_patrician = yes
					}
				}
			}
			usurp_title_plus_barony_if_unlanded = {
				target = ROOT
				type = claim
			}
			any_de_jure_vassal_title = { # take all baronies under the one we're fighting for
				limit = {
					has_holder = yes
					NOT = { de_facto_liege = PREV }
					owner = {
						OR = {
							character = FROM
							is_liege_or_above = FROM
						}
					}
				}
				usurp_title_plus_barony_if_unlanded = {
					target = ROOT
					type = claim
				}
			}
		}
		ROOT = { participation_scaled_prestige = 100 }
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		hidden_tooltip = { check_holy_order_donation_effect = yes }
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(dejure_county_claim): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = -100 }
		FROM = { participation_scaled_prestige = 50 }
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		log = "CB(dejure_county_claim): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = { participation_scaled_prestige = 100 }
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_reverse_demand_title = {
		if = {
			limit = { empire = { holder = ROOT } }
			emf_imperial_decay_loss = yes
		}
	}

	on_invalidation = {
		log = "CB(dejure_county_claim): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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 importance placed on this CB: scope is the targeted title, ROOT is the attacking character, FROM is the defending character
	ai_will_do = {
		factor = 1
		modifier = { #Focus on Coronation first.
			factor = 0.1
			ROOT = {
				emf_uses_furry_coronation = yes
				emf_is_crowned = no
			}
		}
		modifier = {
			factor = 0
			ROOT = {
				has_flag = is_temujin
				has_landed_title = e_mongol_empire
				demesne_size = 1
			}
			FROM = {
				is_offmap_governor = offmap_china
			}
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = {
				flag = EMF
				days = 730
			}
		}
	}
}

dejure_barony_claim = {
	name = CB_NAME_DEJUREBARONYCLAIM
	war_name = WAR_NAME_DEJUREBARONYCLAIM
	sprite = 16
	truce_days = 730
	hostile_against_others = yes
	is_permanent = yes
	check_de_jure_tier = COUNT
	can_ask_to_join_war = no
	allowed_to_target_tributaries = no
	infamy_modifier = 0.5
	sort_priority = 650

	can_use_gui = {
		emf_cb_can_use_gui = yes
		OR = {
			NOT = { has_dlc = "Sons of Abraham" }
			FROMFROM = {
				location = {
					any_province_holding = {
						holder_scope = {
							OR = {
								character = ROOT_FROM
								is_liege_or_above = ROOT_FROM
							}
							OR = {
								NOT = { religion = ROOT }
								emf_cb_unexpelled_holy_order = no
							}
							NOT = {
								any_liege = {
									religion = ROOT
									emf_cb_unexpelled_holy_order = yes
								}
							}
						}
					}
				}
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = { NOT = { is_liege_or_above = FROM } }
	}

	can_use_title = {
		tier = COUNT
		owner_under_ROOT = yes
		any_direct_de_jure_vassal_title = {
			owner_under_FROM = yes
			NAND = {
				ROOT = { rightful_religious_head = FROM }
				is_holy_site = FROM
			}
		}
		emf_cb_can_use_title = yes
		#emf_cb_can_use_sahara_title = yes # Commented because we always want to be able to DoW for non-dejure baronies
	}

	on_add = {
		log = "CB(dejure_barony_claim): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(dejure_barony_claim): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
	}

	on_success_title = {
		holder_scope = { save_event_target_as = target_taker }
		any_direct_de_jure_vassal_title = {
			limit = {
				holder_scope = {
					OR = {
						character = FROM
						is_liege_or_above = FROM
					}
					OR = {
						NOT = { religion = ROOT }
						emf_cb_unexpelled_holy_order = no
					}
					NOT = {
						any_liege = {
							religion = ROOT
							emf_cb_unexpelled_holy_order = yes
						}
					}
					NOR = {
						character = event_target:target_taker
						is_liege_or_above = event_target:target_taker
						is_liege_or_above = ROOT
					}
				}
			}
			usurp_title_plus_barony_if_unlanded = {
				target = event_target:target_taker
				type = claim
			}
		}
		ROOT = { participation_scaled_prestige = 50 }
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(dejure_barony_claim): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = -50 }
		FROM = { participation_scaled_prestige = 25 }
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 25 }
		}
	}

	on_reverse_demand = {
		log = "CB(dejure_barony_claim): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			prestige = -100
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = { participation_scaled_prestige = 50 }
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_invalidation = {
		log = "CB(dejure_barony_claim): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 75
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 75
	}

	# 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 = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

other_dejure_barony_claim = {
	name = CB_NAME_DEJUREBARONYCLAIM
	war_name = WAR_NAME_OTHER_DEJUREBARONYCLAIM
	sprite = 16
	truce_days = 730
	is_permanent = no

	can_use = { always = no }

	is_valid_title = { always = no }

	on_success = {}

	on_success_title = {}

	on_fail = {}

	on_reverse_demand = {}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

other_dejure_county_claim = {
	name = CB_NAME_DEJURECLAIM
	war_name = WAR_NAME_OTHER_DEJURECLAIM
	sprite = 16
	truce_days = 1825
	hostile_against_others = yes
	is_permanent = yes
	other_de_jure_claim = yes # Optimization for de jure claims of vassals
	press_claim = yes
	can_ask_to_join_war = no
	allowed_to_target_tributaries = no
	infamy_modifier = 0.3
	
	sort_priority = 700

	can_use_gui = {
		emf_cb_thirdparty_can_use_gui = yes
		OR = {
			NOT = { has_dlc = "Sons of Abraham" }
			NOT = { religion = FROM }
			FROM = { emf_cb_unexpelled_holy_order = no }
		}
	}

	can_use_title = {
		tier = COUNT
		FROMFROM = {
			NOT = { character = ROOT }
			mercenary = no
		}
		OR = {
			any_de_jure_liege_title = { holder = ROOT }
			AND = {
				ROOT = { has_landed_title = k_france }
				de_jure_liege_or_above = k_aquitaine
				k_aquitaine = { has_holder = no }
			}
		}
		owner_under_FROM = yes
		owner_under_FROMFROM = no
		owner_under_ROOT = no
		OR = {
			ROOT = { is_nomadic = yes }
			location = { emf_nomadic_province = no }
			FROMFROM = { ai = no }
		}
		NAND = {
			FROMFROM = { rightful_religious_head = ROOT_FROM }
			OR = {
				is_holy_site = FROM
				any_direct_de_jure_vassal_title = {
					is_holy_site = FROM
					owner_under_FROM = yes
				}
			}
		}
		emf_cb_thirdparty_can_use_title = yes
		emf_cb_thirdparty_can_use_sahara_title = yes
	}

	is_valid_title = {
		owner_under_FROM = yes
		OR = {
			any_de_jure_liege_title = { holder = ROOT }
			AND = {
				ROOT = { has_landed_title = k_france }
				de_jure_liege_or_above = k_aquitaine
				k_aquitaine = { has_holder = no }
			}
		}
	}

	on_add = {
		log = "CB(other_dejure_county_claim): START: title=[FromFrom.GetID]: [This.GetTitledFirstName] of [This.PrimaryTitle.GetBaseName] ([This.GetID]/[This.PrimaryTitle.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (#[From.GetID]/[From.PrimaryTitle.GetID]) on behalf of [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (#[Root.GetID]/[Root.PrimaryTitle.GetID])"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(other_dejure_county_claim): VICTORY: title=[FromFrom.GetID]: [This.GetTitledFirstName] of [This.PrimaryTitle.GetBaseName] ([This.GetID]/[This.PrimaryTitle.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (#[From.GetID]/[From.PrimaryTitle.GetID]) on behalf of [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (#[Root.GetID]/[Root.PrimaryTitle.GetID])"
		# TODO: Setup a scripted effect CB helper for these emf_capital_connected optimizations later
		if = { # Base scope is attacker (unlike in the can_use_title, FROMFROM is NOT the attacker here)
			limit = { same_realm = FROM } # As far as top realm goes, provinces can't change connectedness
			ROOT = { set_flag = skip_capconnect_chk_tmp }
		}
		emf_cb_victory_other = yes
		ROOT = {
			opinion = {
				modifier = pressed_my_claim
				who = PREV
			}
			show_scope_change = no
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_success_title = {
		emf_cb_latb = yes
		if = {
			limit = {
				holder_scope = {
					tier = COUNT
					NOT = { num_of_count_titles = 2 }
				}
			}
			holder_scope = {
				show_scope_change = no
				emf_ROOT_subjugate_ruler = yes
			}
		}
		if = {
			limit = {
				holder_scope = {
					OR = {
						higher_tier_than = COUNT
						num_of_count_titles = 2
					}
				}
			}
			show_scope_change = no
			usurp_title_plus_barony_if_unlanded = {
				target = ROOT
				type = claim
			}
			any_de_jure_vassal_title = { # take all baronies under the one we're fighting for
				show_scope_change = no
				limit = {
					NOT = { de_facto_liege = PREV }
					owner_under_FROM = yes
				}
				usurp_title_plus_barony_if_unlanded = {
					target = ROOT
					type = claim
				}
			}
		}
	}

	on_success_posttitle = { emf_cb_thirdparty_on_success_posttitle = yes }

	on_fail = {
		log = "CB(other_dejure_county_claim): WHITEPEACE: title=[FromFrom.GetID]: [This.GetTitledFirstName] of [This.PrimaryTitle.GetBaseName] ([This.GetID]/[This.PrimaryTitle.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (#[From.GetID]/[From.PrimaryTitle.GetID]) on behalf of [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (#[Root.GetID]/[Root.PrimaryTitle.GetID])"
		prestige = -50
		ROOT = {
			show_scope_change = no
			prestige = -100
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		log = "CB(other_dejure_county_claim): DEFEAT: title=[FromFrom.GetID]: [This.GetTitledFirstName] of [This.PrimaryTitle.GetBaseName] ([This.GetID]/[This.PrimaryTitle.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (#[From.GetID]/[From.PrimaryTitle.GetID]) on behalf of [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (#[Root.GetID]/[Root.PrimaryTitle.GetID])"
		emf_cb_defeat_other = yes
		prestige = -100
		transfer_scaled_wealth = {
			to = FROM
			value = 4.0
		}
		ROOT = {
			show_scope_change = no
			prestige = -200
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_invalidation = {
		log = "CB(other_dejure_county_claim): INVALIDATED: [FromFrom.GetTitledFirstName] of [FromFrom.PrimaryTitle.GetBaseName] ([FromFrom.GetID]/[FromFrom.PrimaryTitle.GetID]) vs. [From.GetTitledFirstName] of [From.PrimaryTitle.GetBaseName] (#[From.GetID]/[From.PrimaryTitle.GetID]) on behalf of [Root.GetTitledFirstName] of [Root.PrimaryTitle.GetBaseName] (#[Root.GetID]/[Root.PrimaryTitle.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 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 = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

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. Only valid if is_permanent = yes
	apply_short_occ_mod = no # Do not apply the 'recently_conquered' modifier to Holdings

	can_use = { always = no }

	is_valid = { always = no }

	on_success = {}

	on_fail = {}

	on_reverse_demand = {}

	attacker_ai_victory_worth = {
		factor = 200
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

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

	can_use_gui = {
		emf_cb_can_use_gui = yes
	}

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

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

	on_add = {
		log = "CB(depose_antipope): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(depose_antipope): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		hidden_tooltip = {
			if = {
				limit = { 
					ROOT = {
						has_flag = flag_antipope_for_coronation
					}
				}
				ROOT = {
					character_event = { id = HF.20220 } #Papal Coronation may proceed.
				}
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_piety = 500
				participation_scaled_prestige = 250
			}
		}
		ROOT = {
			participation_scaled_piety = 500
			participation_scaled_prestige = 250
			religion_authority = {
				modifier = deposed_antipope
				years = 50
			}
			if = {
				limit = { rightful_religious_head_scope = { ROOT = { excommunicated_for = PREV } } }
				excommunicate = no
			}
			rightful_religious_head_scope = {
				opinion = {
					who = ROOT
					modifier = opinion_deposed_antipope
				}
			}
		}
		FROM = {
			piety = -500
			prestige = -250
			rightful_religious_head_scope = {
				if = {
					limit = { has_claim = k_papal_state }
					remove_claim = k_papal_state
				}
				if = {
					limit = { has_claim = d_fraticelli }
					remove_claim = d_fraticelli
				}
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(depose_antipope): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		hidden_tooltip = {
			FROM = {
				clr_flag = flag_antipope_for_coronation
			}
			ROOT = {
				clr_flag = flag_antipope_for_coronation
			}
		}
		ROOT = {
			piety = -250
			prestige = -125
		}
		FROM = {
			piety = 100
			prestige = 50
		}
	}

	on_reverse_demand = {
		log = "CB(depose_antipope): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		hidden_tooltip = {
			FROM = {
				clr_flag = flag_antipope_for_coronation
			}
			ROOT = {
				clr_flag = flag_antipope_for_coronation
			}
		}
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -500
			prestige = -250
		}
		FROM = {
			piety = 500
			prestige = 250
		}
	}

	on_invalidation = {
		log = "CB(depose_antipope): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		# Coronation request locks cleared on invalidation or loss, to avoid locking the player in the middle of the negotiation forever.
		hidden_tooltip = {
			ROOT = {
				clr_flag = flag_antipope_for_coronation 
			}
			FROM = {
				clr_flag = flag_antipope_for_coronation 
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	# 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 = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

cb_install_antiking = {
	name = CB_NAME_INSTALL_ANTIKING
	war_name = WAR_NAME_INSTALL_ANTIKING
	sprite = 11
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	attacker_rel_head_is_ally = yes # The attacker can call his (main) Pope into the war

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			vassal_of = FROM
			religion = FROM
			OR = {
				religion = catholic
				religion = fraticelli
			}
		}
	}

	is_valid_title = {
		holder_scope = { NOT = { character = ROOT } }
	}

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

	on_add = {
		log = "CB(cb_install_antiking): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_dynlevy_begin = yes
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(cb_install_antiking): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		emf_cb_imperial_decay_overthrow = yes
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_piety = 500
				participation_scaled_prestige = 250
			}
		}
		ROOT = {
			participation_scaled_piety = 500
			participation_scaled_prestige = 250
			religion_authority = {
				modifier = deposed_antipope
				years = 50
			}
			if = {
				limit = { religion = catholic }
				k_papal_state = {
					holder_scope = {
						opinion = {
							who = ROOT
							modifier = opinion_deposed_antipope
						}
					}
				}
			}
			if = {
				limit = { religion = fraticelli }
				d_fraticelli = {
					holder_scope = {
						opinion = {
							who = ROOT
							modifier = opinion_deposed_antipope
						}
					}
				}
			}
		}
		FROM = {
			piety = -500
			prestige = -250
			rightful_religious_head_scope = {
				if = {
					limit = { has_claim = k_papal_state }
					remove_claim = k_papal_state
				}
				if = {
					limit = { has_claim = d_fraticelli }
					remove_claim = d_fraticelli
				}
			}
			primary_title = {
				emf_cb_ROOT_ensure_feudal = yes # When claimant has wrong government, take care of that first.
				usurp_title_plus_barony_if_unlanded_and_vassals_no_adj = {
					target = ROOT
					type = revolt
				}
				if = {
					limit = { has_law = investiture_law_1 }
					add_law = investiture_law_0
				}
			}
		}
		hidden_tooltip = {
			ROOT = {
				primary_title = {
					if = {
						limit = { has_law = investiture_law_1 }
						add_law = investiture_law_0
					}
				}
			}
		}
		emf_cb_dynlevy_end = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(cb_install_antiking): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_dynlevy_end = yes
		ROOT = {
			piety = -250
			prestige = -125
			cancel_plot = plot_gain_title
		}
		FROM = {
			piety = 100
			prestige = 50
		}
	}

	on_reverse_demand = {
		log = "CB(cb_install_antiking): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		emf_cb_dynlevy_end = yes
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -500
			prestige = -250
			prisoner = FROM
			cancel_plot = plot_gain_title
		}
		FROM = {
			piety = 500
			prestige = 250
			hidden_tooltip = { 
				if = { 
					limit = { NOT = { has_opinion_modifier = { modifier = opinion_traitor who = ROOT } } } #Allow free revocation of a title.
					opinion = {
						who = ROOT
						modifier = opinion_traitor
					}
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(cb_install_antiking): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_dynlevy_end = yes
		ROOT = { cancel_plot = plot_gain_title }
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

buddhist_holy_war = {
	name = CB_NAME_RELIGIOUS_MINOR
	war_name = WAR_NAME_RELIGIOUS
	sprite = 9
	truce_days = 1825
	hostile_against_others = yes
	is_permanent = yes
	check_all_titles = yes # if permanent, setting this to true will check against all of someones titles, including vassal held titles
	can_ask_to_join_war = no
	allowed_to_target_tributaries = no
	
	sort_priority = 800

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			custom_tooltip = {
				text = NEEDS_100_PIETY_COST
				piety = 100
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			religion = buddhist
			NOT = { same_realm = FROM }
			emf_hostile_religion_to_FROM = yes
			top_liege = {
				emf_hostile_religion_to_ROOT = no
				emf_hostile_religion_to_FROM = yes
			}
			mercenary = no
			is_nomadic = no
		}
	}

	can_use_title = {
		tier = COUNT
		OR = {
			FROM = { has_landed_title = PREV }
			holder_scope = { is_liege_or_above = FROM }
		}
		# The attacker needs a border or be at the most two sea zones away
		location = {
			any_neighbor_province = {
				OR = {
					owner_under_ROOT = yes
					AND = {
						is_land = no
						any_neighbor_province = {
							OR = {
								owner_under_ROOT = yes
								AND = {
									is_land = no
									any_neighbor_province = { owner_under_ROOT = yes }
								}
							}
						}
					}
				}
			}
		}
		emf_cb_can_use_title = yes
	}

	is_valid = {
		ROOT = {
			religion = buddhist
			NOT = { same_realm = FROM }
			emf_hostile_religion_to_FROM = yes
			top_liege = {
				emf_hostile_religion_to_ROOT = no
				emf_hostile_religion_to_FROM = yes
			}
		}
	}

	on_add = {
		log = "CB(buddhist_holy_war): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { piety = -100 }
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(buddhist_holy_war): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		any_attacker = {
			hidden_tooltip = {
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
		}
		
		ROOT = {
			hidden_tooltip = {
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
		}
	}

	on_success_title = {
		emf_cb_latb = yes
		if = {
			limit = {
				holder_scope = {
					tier = COUNT
					NOT = { num_of_count_titles = 2 }
					lower_tier_than = ROOT
				}
			}
			holder_scope = {
				emf_ROOT_subjugate_ruler = yes
			}
		}
		if = {
			limit = {
				holder_scope = {
					OR = {
						higher_tier_than = COUNT
						num_of_count_titles = 2
						NOT = { lower_tier_than = ROOT }
					}
				}
			}
			usurp_title_plus_barony_if_unlanded = {
				target = ROOT
				type = holy_war
			}
			any_de_jure_vassal_title = { # take all baronies under the one we're fighting for
				limit = {
					has_holder = yes
					NOT = { de_facto_liege = PREV }
					holder_scope = {
						OR = {
							character = FROM
							is_liege_or_above = FROM
						}
					}
				}
				usurp_title_plus_barony_if_unlanded = {
					target = ROOT
					type = holy_war
				}
			}
		}
		ROOT = { participation_scaled_prestige = 100 }
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(buddhist_holy_war): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = -100 }
		FROM = { participation_scaled_prestige = 50 }
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		log = "CB(buddhist_holy_war): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			participation_scaled_prestige = 100
			hidden_tooltip = {
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
			hidden_tooltip = {
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(buddhist_holy_war): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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 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 = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

indian_subjugation = {
	name = CB_NAME_PAGAN_SUBJUGATION
	war_name = WAR_NAME_PAGAN_SUBJUGATION
	sprite = 8
	truce_days = 365
	hostile_against_others = yes
	is_permanent = yes
	can_ask_to_join_war = yes
	check_de_jure_tier = KING # this scans all de jure kingdoms for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	allowed_to_target_tributaries = no
	
	sort_priority = 1000

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			piety = 500
			conditional_tooltip = {
				trigger = { is_patrician = yes }
				custom_tooltip = {
					text = emf_mr_ctt_extended_cbs_unlocked
					has_game_rule = { name = mr_cb_restriction value = off }
				}
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			has_dharmic_religion_trigger = yes
			NOT = { same_realm = FROM }
			NOT = { is_liege_or_above = FROM }
			has_horde_culture = no # Should use tribal invasion instead
			is_nomadic = no
			mercenary = no
		}
		FROM = {
			NOT = { is_offmap_governor = yes }
			has_dharmic_religion_trigger = yes
			culture_group = ROOT
			NOT = {
				any_war = {
					defender = { character = PREVPREV }
					using_cb = indian_subjugation
				}
			}
		}
	}

	can_use_title = {
		emf_cb_can_use_de_jure_title = yes
		ROOT = {
			OR = {
				NOT = { has_character_modifier = launched_subjugation }
				AND = {
					has_ambition = obj_become_king
					capital_scope = { kingdom = { title = PREVPREVPREV } }
				}
			}
		}
		FROM = { any_realm_province = { de_jure_liege_or_above = PREVPREV } }
	}

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

	on_add = {
		log = "CB(indian_subjugation): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { piety = -500 }
		emf_cb_on_add = yes
	}

	on_add_title = {
		ROOT = {
			if = {
				limit = {
					OR = {
						NOT = { has_ambition = obj_become_king }
						capital_scope = { kingdom = { NOT = { title = PREVPREVPREV } } }
					}
				}
				add_character_modifier = {
					name = launched_subjugation
					years = 100
					hidden = yes
				}
			}
		}
	}

	on_success = {
		log = "CB(indian_subjugation): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		ROOT = { participation_scaled_prestige = 200 }
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		FROM = { prestige = -200 }
	}

	on_success_title = {
		emf_cb_latb = yes
		# Usurp the target kingdom itself, if the enemy holds it
		if = {
			limit = {
				holder = 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
				emf_liege_change = yes
			}
		}
		pf_liege_change_under_title_begin_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_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(indian_subjugation): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		FROM = { participation_scaled_prestige = 100 }
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		ROOT = { prestige = -100 }
	}

	on_reverse_demand = {
		log = "CB(indian_subjugation): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
		}
		FROM = { participation_scaled_prestige = 200 }
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
	}

	on_invalidation = {
		log = "CB(indian_subjugation): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			remove_character_modifier = launched_subjugation
			piety = 500
		}
	}

	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 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 # Prefer using CB's without on_add costs
		}
		modifier = {
			factor = 0
			ROOT = {
				has_flag = is_temujin
				has_landed_title = e_mongol_empire
				demesne_size = 1
			}
			FROM = {
				is_offmap_governor = offmap_china
			}
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				NOT = {
					is_tribal = yes
				}
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}
				}
			}
		}
		modifier = {
			factor = 0
			FROM = {
				is_nomadic = yes
			}
			ROOT = {
				is_tribal = yes
			}
			NOT = {
				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
							holding_type = tribal
						}
					}
				}
			}
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

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

	can_use_gui = {
		emf_cb_can_use_gui = yes
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			OR = {
				ai = yes
				multiplayer = no
				NOT = {
					has_game_rule = {
						name = multiplayer_invasions
						value = off
					}
				}
			}
			OR = {
				AND = {
					emf_seljuk_dynasty = yes
					NOT = { year = 1200 }
				}
				dynasty = 800 # Timurids
			}
		}
		ROOT = {
			higher_tier_than = COUNT
			NOT = { is_liege_or_above = FROM }
			NOT = { is_vassal_or_below = FROM }
			NOT = { dynasty = FROM }
			mercenary = no
		}
	}

	can_use_title = {
		emf_cb_can_use_de_jure_title = yes
		# Check vassal duchy titles against region due to possibility of DJ drift into a kingdom with foreign capital
		any_direct_de_jure_vassal_title = {
			any_direct_de_jure_vassal_title = {
				location = { region = emf_region_manifest_destiny }
			}
		}
		# 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 = {
					owner_under_ROOT = yes
					location = { any_neighbor_province = { owner_under_ROOT = yes } }
				}
			}
		}
	}

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

	on_add = {
		log = "CB(manifest_destiny_invasion): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(manifest_destiny_invasion): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		emf_imperial_decay_victory = 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
			if = {
				limit = { uses_decadence = yes }
				participation_scaled_decadence = -20
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = 200
				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -20
				}
			}
		}
		FROM = { prestige = -200 }
	}

	on_success_title = {
		emf_cb_latb = yes
		pf_liege_change_under_title_begin_effect = yes
		custom_tooltip = {
			text = other_invasion_succ_tip # tribal_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_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(manifest_destiny_invasion): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		FROM = {
			prestige = 100
			if = {
				limit = {
					has_dlc = "Legacy of Rome"
					emf_has_roman_empire = yes
					OR = { 
						religion_group = christian
						religion = hellenic_pagan
						religion = hellenic_pagan_reformed
					}
				}
				hidden_tooltip = {
					character_event = {
						id = LoR.30
						days = 12
					}
				}
			}
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		ROOT = { prestige = -200 }
	}

	on_reverse_demand = {
		log = "CB(manifest_destiny_invasion): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		emf_imperial_decay_loss = yes
		ROOT = {
			prestige = -400
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			if = {
				limit = {
					uses_decadence = yes
					NOT = { any_liege = { dynasty = PREV } }
				}
				decadence = 10
			}
		}
		FROM = {
			prestige = 200
			if = {
				limit = {
					has_dlc = "Legacy of Rome"
					emf_has_roman_empire = yes
					OR = { 
						religion_group = christian
						religion = hellenic_pagan
						religion = hellenic_pagan_reformed
					}
				}
				hidden_tooltip = {
					character_event = {
						id = LoR.30
						days = 12
					}
				}
			}
			participation_scaled_prestige = 200
			if = {
				limit = { uses_decadence = yes }
				participation_scaled_decadence = -20
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_prestige = 200
				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -20
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(manifest_destiny_invasion): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	attacker_ai_victory_worth = {
		factor = 100
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 150
	}

	ai_will_do = {
		factor = 2
		modifier = { #Focus on Coronation first.
			factor = 0.1
			ROOT = {
				emf_uses_furry_coronation = yes
				emf_is_crowned = no
			}
		}
		modifier = {
			factor = 1.5
			FROM = {
				num_of_realm_counties = {
					value = 2
					title = PREV
				}
			}
		}
		modifier = {
			factor = 1.667
			FROM = {
				num_of_realm_counties = {
					value = 3
					title = PREV
				}
			}
		}
		modifier = {
			factor = 2
			FROM = {
				num_of_realm_counties = {
					value = 4
					title = PREV
				}
			}
		}
	}
}

cb_decadence_usurption = {
	name = CB_NAME_DECADENCE_USURPTION
	war_name = WAR_NAME_DECADENCE_USURPTION
	sprite = 11
	truce_days = 1825
	is_permanent = yes
	allowed_to_target_tributaries = no

	can_use_gui = {
		emf_cb_can_use_gui = yes
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			NOT = { de_facto_liege = FROM }
			religion_group = muslim
			uses_decadence = yes
			mercenary = no
		}
		FROM = {
			religion_group = muslim
			uses_decadence = yes
			trait = decadent
			dynasty = ROOT
			OR = {
				liege = {
					character = PREV # either independent
				}
				liege = {
					ROOT = {
						is_liege_or_above = PREV # or have shared liege
					}
				}
			}
			primary_title = {
				is_primary_type_title = no
				is_landless_type_title = no
			}
		}
	}

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

	on_add = {
		log = "CB(cb_decadence_usurption): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(cb_decadence_usurption): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		ROOT = {
			participation_scaled_piety = 500
			participation_scaled_prestige = 250
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_piety = 500
				participation_scaled_prestige = 250
			}
		}
		FROM = { prisoner = ROOT }
		FROM = {
			primary_title = {
				emf_cb_latb = yes
				usurp_title_plus_barony_if_unlanded = {
					target = ROOT
					type = invasion
				}
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(cb_decadence_usurption): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			piety = -100
			prestige = -125
		}
		FROM = {
			piety = 100
			prestige = 125
		}
	}

	on_reverse_demand = {
		log = "CB(cb_decadence_usurption): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
			piety = -200
			prestige = -250
		}
		FROM = {
			piety = 200
			prestige = 250
		}
	}

	on_invalidation = {
		log = "CB(cb_decadence_usurption): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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 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 = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

rivalry_cb = {
	name = CB_NAME_RIVALRY
	war_name = WAR_NAME_RIVALRY
	sprite = 7
	truce_days = 1825
	is_permanent = yes
	can_ask_to_join_war = no
	allowed_to_target_tributaries = no
	defender_unoccupied_warscore = yes
	
	sort_priority = 900

	can_use_gui = {
		emf_cb_can_use_gui = yes
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = { is_rival = FROM }
		FROM = {
			NOT = {
				has_opinion_modifier = {
					who = ROOT
					modifier = opinion_humiliated_rival
				}
			}
		}
	}

	is_valid = { ROOT = { is_rival = FROM } }

	on_add = {
		log = "CB(rivalry_cb): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(rivalry_cb): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		emf_imperial_decay_victory = yes
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = 250
				add_character_modifier = {
					name = emf_victory_timer
					duration = 1
					hidden = yes
				}
			}
		}
		ROOT = {
			participation_scaled_prestige = 250
			if = {
				limit = {
					ai = no
					sibling = FROM
				}
				set_flag = achievement_sibling_rival_war
			}
			remove_opinion = {
				who = FROM
				modifier = opinion_humiliated_rival
			}
		}
		FROM = {
			prestige = -250
			imprison = ROOT
			opinion = {
				modifier = opinion_humiliated_rival
				who = ROOT
				years = 150
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(rivalry_cb): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = -125 }
		FROM = { prestige = 50 }
	}

	on_reverse_demand = {
		log = "CB(rivalry_cb): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		emf_imperial_decay_loss = yes
		FROM = {
			participation_scaled_prestige = 250
			hidden_tooltip = {
				add_character_modifier = {
					name = emf_victory_timer
					duration = 1
					hidden = yes
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_prestige = 250
				add_character_modifier = {
					name = emf_victory_timer
					duration = 1
					hidden = yes
				}
			}
		}
		ROOT = {
			prestige = -250
			imprison = FROM
			opinion = {
				modifier = opinion_humiliated_rival
				who = FROM
				years = 150
			}
		}
		FROM = {
			remove_opinion = {
				who = ROOT
				modifier = opinion_humiliated_rival
			}
			if = {
				limit = {
					ai = no
					sibling = ROOT
				}
				set_flag = achievement_sibling_rival_war
			}
		}
	}

	on_invalidation = {
		log = "CB(rivalry_cb): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

tributary_cb = {
	name = CB_NAME_TRIBUTARY
	war_name = WAR_NAME_TRIBUTARY
	sprite = 36
	truce_days = 365
	is_permanent = yes
	hostile_against_others = yes
	can_ask_to_join_war = no
	display_on_map = no
	allowed_to_target_tributaries = no
	defender_unoccupied_warscore = yes
	
	sort_priority = 810

	can_use_gui = {
		emf_cb_can_use_gui = yes
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = { is_nomadic = no }
		emf_cb_tributary_basic_can_use = yes
	}

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

	on_add = {
		log = "CB(tributary_cb): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		# Chance to surrender early
		ROOT = { save_event_target_as = tribute_war_suzerain }
		FROM = {
			show_scope_change = no
			save_event_target_as = tribute_war_target
			set_flag = surrender_tributary_cb
			character_event = { id = JD.2500 days = 5 tooltip = surrender_tributary_tt }
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(tributary_cb): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		emf_imperial_decay_victory = yes
		FROM = {
			show_scope_change = no
			save_event_target_as = target_loser
			prestige = -100
			custom_tooltip = {
				text = REMOVE_PREVIOUS_SUZERAIN
				hidden_tooltip = {
					any_suzerain = { event_target:target_loser = { remove_tributary = PREV } }
				}
			}
		}
		ROOT = {
			show_scope_change = no
			prestige = 200
			make_tributary = {
				who = FROM
				percentage = 0.4
			}
		}
		if = {
			limit = { ROOT = { uses_decadence = yes } }
			ROOT = { decadence = -10 }
		}
		if = {
			limit = { ROOT = { is_nomadic = yes } }
			ROOT = {
				clan_title = {
					ROOT = {
						any_vassal = {
							limit = { is_nomadic = yes }
							clan_title = {
								clan_opinion = {
									who = PREVPREVPREV
									modifier = won_tributary_war
									years = 5
								}
							}
						}
					}
				}
			}
		}
		if = {
			limit = { ROOT = { is_tribal = yes } }
			ROOT = {
				any_vassal = {
					limit = { is_tribal = yes }
					opinion = {
						who = PREV
						modifier = won_tributary_war
						years = 5
					}
				}
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(tributary_cb): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			show_scope_change = no
			prestige = -100
		}
		FROM = {
			show_scope_change = no
			prestige = 100
		}
	}

	on_reverse_demand = {
		log = "CB(tributary_cb): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		emf_imperial_decay_loss = yes
		ROOT = {
			show_scope_change = no
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			show_scope_change = no
			prestige = 200
		}
	}

	on_attacker_leader_death = { end_war = invalid }

	on_invalidation = {
		log = "CB(tributary_cb): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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 = { higher_tier_than = DUKE }
		}
		modifier = {
			factor = 0.85
			ROOT = {
				relative_power = {
					who = FROM
					power = 1.2
				}
			}
		}
		modifier = {
			factor = 0.85
			ROOT = {
				relative_power = {
					who = FROM
					power = 1.5
				}
			}
		}
		modifier = {
			factor = 0.8
			ROOT = {
				relative_power = {
					who = FROM
					power = 2.0
				}
			}
		}
		modifier = {
			factor = 0.8
			ROOT = {
				relative_power = {
					who = FROM
					power = 2.5
				}
			}
		}
		modifier = {
			factor = 0.8
			ROOT = {
				relative_power = {
					who = FROM
					power = 3
				}
			}
		}
		modifier = {
			factor = 0.75
			ROOT = {
				relative_power = {
					who = FROM
					power = 4
				}
			}
		}
		modifier = {
			factor = 0.75
			ROOT = {
				relative_power = {
					who = FROM
					power = 5
				}
			}
		}
		modifier = {
			factor = 0.75
			ROOT = {
				relative_power = {
					who = FROM
					power = 6
				}
			}
		}
		modifier = {
			factor = 0.5
			ROOT = {
				relative_power = {
					who = FROM
					power = 8
				}
			}
		}
		modifier = {
			factor = 0.25
			ROOT = {
				relative_power = {
					who = FROM
					power = 10
				}
			}
		}
		modifier = {
			factor = 2
			ROOT = {
				distance_from_realm = {
					who = FROM
					value = 20
				}
			}
		}
		modifier = {
			factor = 2
			ROOT = {
				distance_from_realm = {
					who = FROM
					value = 40
				}
			}
		}
		modifier = {
			factor = 3
			ROOT = {
				distance_from_realm = {
					who = FROM
					value = 80
				}
			}
		}
		modifier = {
			factor = 4
			ROOT = {
				distance_from_realm = {
					who = FROM
					value = 120
				}
			}
		}
	}

	ai_will_do = {
		factor = 1
		modifier = { #Focus on Coronation first.
			factor = 0.1
			ROOT = {
				emf_uses_furry_coronation = yes
				emf_is_crowned = no
			}
		}
		modifier = {
			factor = 0.1 # Low prio CB
		}
		modifier = {
			factor = 0.1 # Nomads are the only ones who like using the CB
			ROOT = { is_nomadic = no }
		}
		modifier = {
			factor = 0.1 # Target has almost no money
			FROM = {
				OR = {
					is_nomadic = yes
					is_tribal = yes
				}
			}
		}
		modifier = {
			factor = 4
			ROOT = { trait = greedy }
		}
		modifier = {
			factor = 0
			ROOT = {
				is_feudal = yes
				NOT = { religion_group = muslim }
			}
			FROM = {
				is_feudal = yes
				NOT = { religion_group = muslim }
			}
		}
		modifier = {
			factor = 0
			emf_is_hre_being_restored = yes
			ROOT = {
				emf_can_restore_hre_someday = yes
			}
			FROM = {
				emf_has_karling_title = yes
			}
		}
		modifier = {
			factor = 0
			ROOT = {
				trigger_if = {
					limit = { emf_has_swmh = yes }
					culture = old_hungarian
				}
				trigger_else = {
					culture = hungarian
				}
				religion_group = pagan_group
				capital_scope = { kingdom = { title = k_hungary } }
			}
			has_global_flag = emf_magyar_migration_completed
			NOT = { has_global_flag = emf_conquest_hungary_completed }
			emf_alternate_start = no
		}
		modifier = {
			factor = 0
			ROOT = {
				top_liege = {
					trigger_if = {
						limit = { emf_has_swmh = yes }
						culture = old_hungarian
					}
					trigger_else = {
						culture = hungarian
					}
					religion_group = pagan_group
				}
			}
			NOT = { has_global_flag = emf_magyar_migration_completed }
			NOT = { year = 920 }
			emf_alternate_start = no
		}
	}
}

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

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

	can_use = {
		emf_cb_can_use = 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
							is_vassal_or_below_of = FROM
							liege_before_war = {
								OR = {
									character = FROM
									is_vassal_or_below_of = FROM
								}
							}
						}
					}
				}
				any_vassal = {
					is_tributary = yes
					suzerain = {
						OR = {
							character = FROM
							is_vassal_or_below_of = FROM
							liege_before_war = {
								OR = {
									character = FROM
									is_vassal_or_below_of = FROM
								}
							}
						}
					}
				}
			}
		}
	}

	on_add = {
		log = "CB(free_tributary_cb): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(free_tributary_cb): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		emf_imperial_decay_victory = yes
		FROM = {
			show_scope_change = no
			prestige = -100
		}
		ROOT = {
			show_scope_change = no
			prestige = 100
			if = {
				limit = {
					is_tributary = yes
					suzerain = {
						OR = {
							character = FROM
							is_vassal_or_below_of = FROM
							liege_before_war = {
								OR = {
									character = FROM
									is_vassal_or_below_of = FROM
								}
							}
						}
					}
				}
				suzerain = { remove_tributary = PREV }
			}
			any_vassal = {
				limit = {
					is_tributary = yes
					suzerain = {
						OR = {
							character = FROM
							is_vassal_or_below_of = FROM
							liege_before_war = {
								OR = {
									character = FROM
									is_vassal_or_below_of = FROM
								}
							}
						}
					}
				}
				suzerain = { remove_tributary = PREV }
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(free_tributary_cb): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			show_scope_change = no
			prestige = -100
		}
		FROM = {
			show_scope_change = no
			prestige = 100
		}
	}

	on_reverse_demand = {
		log = "CB(free_tributary_cb): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		emf_imperial_decay_loss = yes
		ROOT = {
			show_scope_change = no
			prestige = -100
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			show_scope_change = no
			prestige = 100
		}
	}

	on_invalidation = {
		log = "CB(free_tributary_cb): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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.8
			ROOT = {
				relative_power = {
					who = FROM
					power = 1.2
				}
			}
		}
		modifier = {
			factor = 0.8
			ROOT = {
				relative_power = {
					who = FROM
					power = 1.5
				}
			}
		}
		modifier = {
			factor = 0.75
			ROOT = {
				relative_power = {
					who = FROM
					power = 2.0
				}
			}
		}
		modifier = {
			factor = 0.75
			ROOT = {
				relative_power = {
					who = FROM
					power = 2.5
				}
			}
		}
		modifier = {
			factor = 0.75
			ROOT = {
				relative_power = {
					who = FROM
					power = 3
				}
			}
		}
		modifier = {
			factor = 0.7
			ROOT = {
				relative_power = {
					who = FROM
					power = 4
				}
			}
		}
		modifier = {
			factor = 0.7
			ROOT = {
				relative_power = {
					who = FROM
					power = 5
				}
			}
		}
		modifier = {
			factor = 0.7
			ROOT = {
				relative_power = {
					who = FROM
					power = 6
				}
			}
		}
		modifier = {
			factor = 0.4
			ROOT = {
				relative_power = {
					who = FROM
					power = 8
				}
			}
		}
		modifier = {
			factor = 0.1
			ROOT = {
				relative_power = {
					who = FROM
					power = 10
				}
			}
		}
		modifier = {
			factor = 5
			ROOT = {
				distance_from_realm = {
					who = FROM
					value = 20
				}
			}
		}
		modifier = {
			factor = 5
			ROOT = {
				distance_from_realm = {
					who = FROM
					value = 40
				}
			}
		}
		modifier = {
			factor = 5
			ROOT = {
				distance_from_realm = {
					who = FROM
					value = 80
				}
			}
		}
		modifier = {
			factor = 5
			ROOT = {
				distance_from_realm = {
					who = FROM
					value = 120
				}
			}
		}
	}

	ai_will_do = {
		factor = 1
		modifier = {
			factor = 1.5
		}
		modifier = { #Focus on Coronation first.
			factor = 0.1
			ROOT = {
				emf_uses_furry_coronation = yes
				emf_is_crowned = no
			}
		}
		modifier = {
			factor = 4
			ROOT = { trait = greedy }
		}
		modifier = {
			factor = 2
			OR = {
				ROOT = { is_nomadic = yes }
				FROM = { is_nomadic = no }
			}
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

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
	capturing_attacker_is_complete_victory = no

	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
					}
				}
			}
			num_of_count_titles = 1
		}
		FROM = {
			any_demesne_title = {
				NOT = { lower_tier_than = ROOT }
				temporary = no
			}
		}
	}

	on_add = {
		log = "CB(cb_install_khan): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_dynlevy_begin = yes
	}

	on_success = {
		log = "CB(cb_install_khan): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		FROM = {
			show_scope_change = no
			any_demesne_title = {
				limit = { tier = EMPEROR }
				gain_title = {
					target = ROOT
					type = faction_demand
				}
			}
			death = {
				death_reason = death_execution
				killer = ROOT
			}
		}
		emf_cb_dynlevy_end = yes
		ROOT = {
			show_scope_change = no
			participation_scaled_prestige = 250
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 250 }
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(cb_install_khan): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_dynlevy_end = yes
		ROOT = {
			show_scope_change = no
			prestige = -100
		}
	}

	on_reverse_demand = {
		log = "CB(cb_install_khan): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		emf_cb_dynlevy_end = yes
		ROOT = {
			show_scope_change = no
			prestige = -200
			prisoner = FROM
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 50
			hidden_tooltip = { 
				if = { 
					limit = { NOT = { has_opinion_modifier = { modifier = opinion_traitor who = ROOT } } } #Allow free revocation of a title.
					opinion = {
						who = ROOT
						modifier = opinion_traitor
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	on_defender_leader_death = {
		hidden_tooltip = { any_attacker = { letter_event = { id = 251 } } }
		end_war = invalid
	}

	on_invalidation = {
		log = "CB(cb_install_khan): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_dynlevy_end = yes
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

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

	sort_priority = 700

	can_use_gui = {
		emf_cb_can_use_gui = yes
	}

	can_use = {
		emf_cb_can_use = yes
		has_dlc = "Horse Lords"
		ROOT = { is_nomadic = yes }
		FROM = { is_nomadic = yes }
		ROOT = {
			same_realm = FROM
			has_feud_with = FROM
			mercenary = no
		}
	}

	can_use_title = {
		tier = COUNT
		location = { NOT = { num_of_settlements = 1 } }
		FROM = { has_landed_title = PREV }
		emf_cb_can_use_title = yes
	}

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

	on_add = {
		log = "CB(nomad_dispute): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(nomad_dispute): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
	}

	on_success_title = {
		emf_cb_latb = yes
		usurp_title_plus_barony_if_unlanded = {
			target = ROOT
			type = invasion
		}
		ROOT = {
			show_scope_change = no
			participation_scaled_prestige = 100
			participation_scaled_piety = 50
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = 100
				participation_scaled_piety = 50
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(nomad_dispute): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			show_scope_change = no
			prestige = -100
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		log = "CB(nomad_dispute): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			show_scope_change = no
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 100
			participation_scaled_piety = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_prestige = 100
				participation_scaled_piety = 50
			}
		}
	}

	on_invalidation = {
		log = "CB(nomad_dispute): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = {
		factor = 2
		modifier = { #Focus on Coronation first.
			factor = 0.1
			ROOT = {
				emf_uses_furry_coronation = yes
				emf_is_crowned = no
			}
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

cb_liberate_nomad_duchy = {
	name = CB_NAME_LIBERATE_NOMAD
	war_name = WAR_NAME_LIBERATE_NOMAD
	sprite = 8
	truce_days = 1825
	is_permanent = no

	can_use = {
		always = no # Use emf_liberate_nomad_county instead
	}

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

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

	is_valid_title = {
		owner_under_FROM = yes
	}

	on_add = {
		log = "CB(cb_minor_clan_revolt): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(cb_minor_clan_revolt): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
	}

	on_success_title = {
		# No emf_cb_latb here for good reason
		hidden_effect = {
			ROOT = { set_government_type = nomadic_government }
		}
		usurp_title_plus_barony_if_unlanded = {
			target = ROOT
			type = revolt
		}
		hidden_effect = {
			ROOT = {
				primary_title = {
					set_title_nomad = {
						title = THIS
						status = no
					}
				}
				emf_switch_to_feudal_gov = yes
			}
		}
		ROOT = {
			set_government_type = nomadic_government
			set_defacto_liege = FROM
			emf_liege_change = yes
			hidden_tooltip = {
				remove_opinion = {
					who = FROM
					modifier = opinion_evil_tyrant
				}
			}
		}
		FROM = { prestige = -100 }
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(cb_minor_clan_revolt): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		FROM = {
			prestige = 10
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor
					who = ROOT
					months = 1200
				}
			}
		}
		ROOT = {
			clear_wealth = yes
			imprison = FROM
			hidden_tooltip = {
				add_character_modifier = {
					name = broken_spirit
					days = -1
				}
			}
		}
	}

	on_reverse_demand = {
		log = "CB(cb_minor_clan_revolt): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		FROM = {
			prestige = 20
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor
					who = ROOT
					months = 1200
				}
				add_character_modifier = {
					name = crushed_revolt
					days = 3650
				}
			}
		}
		ROOT = {
			clear_wealth = yes
			imprison = FROM
			hidden_tooltip = {
				add_character_modifier = {
					name = broken_spirit
					days = -1
				}
			}
		}
	}

	on_attacker_leader_death = { end_war = invalid }

	on_invalidation = {
		log = "CB(cb_minor_clan_revolt): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

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

nomad_subjugation = {
	name = CB_NAME_PAGAN_SUBJUGATION
	war_name = WAR_NAME_PAGAN_SUBJUGATION
	sprite = 8
	truce_days = 365
	is_permanent = yes
	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no
	defender_unoccupied_warscore = yes
	
	sort_priority = 1100

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			OR = {
				population >= 10000
				FROM = {
					is_tribal = yes
					ROOT = { population >= 2500 }
				}
				has_opinion_modifier = { who = FROM modifier = declared_independence_nomad }
			}
			trigger_if = {
				limit = { NOT = { ROOT = { prestige >= 500 } } }
				OR = {
					custom_tooltip = {
						text = NEEDS_500_PRESTIGE_COST
						prestige >= 500
					}
					has_opinion_modifier = { who = FROM modifier = declared_independence_nomad }
				}
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		FROM = {
			independent = yes
			lower_tier_than = ROOT
			is_nomadic = no # Nomads cannot be subjugated
		}
		ROOT = {
			is_nomadic = yes
			OR = {
				ai = yes
				multiplayer = no
				NOT = { has_game_rule = { name = multiplayer_invasions value = off } }
			}
			OR = {
				NOT = { has_character_modifier = launched_subjugation }
				has_opinion_modifier = { who = FROM modifier = declared_independence_nomad }
			}
			mercenary = no
		}
		FROM = {
			NOT = { is_offmap_governor = yes }
			NOT = {
				any_war = {
					defender = { character = ROOT_FROM }
					OR = {
						using_cb = nomad_subjugation
						using_cb = pagan_subjugation
						using_cb = emf_pagan_subjugation_religious
					}
				}
			}
		}
		ROOT = {
			any_realm_province = {
				owner_under_ROOT = yes
				any_neighbor_province = { owner_under_FROM = yes }
			}
		}
		OR = {
			ROOT = { ai = no }
			FROM = { num_of_count_titles_in_realm > 4 }
		}
	}

	is_valid = {
		FROM = {
			independent = yes
			num_of_count_titles_in_realm > 0
			lower_tier_than = ROOT
		}
	}

	on_add = {
		log = "CB(nomad_subjugation): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			if = {
				limit = { NOT = { has_opinion_modifier = { who = FROM modifier = declared_independence_nomad } } }
				add_character_modifier = {
					name = launched_subjugation
					years = 100
					hidden = yes
				}
				prestige = -500
			}
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(nomad_subjugation): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = 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 = {
						2 = { give_nickname = nick_the_eastern_wind }
						5 = { give_nickname = nick_the_despoiler }
						5 = {
							give_nickname = nick_the_bane_of_realmname
							modifier = {
								factor = 0
								event_target:nickname_target = { is_nomadic = yes }
							}
							modifier = {
								factor = 0
								event_target:nickname_target = { culture = ROOT }
							}
						}
						5 = {
							give_nickname = nick_the_culture_marauder
							modifier = {
								factor = 0
								event_target:nickname_target = { is_nomadic = yes }
							}
							modifier = {
								factor = 0
								event_target:nickname_target = { culture = ROOT }
							}
						}
						100 = { }
					}
				}
			}
		}
		ROOT = {
			piety = 100
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		FROM = {
			piety = -100
			prestige = -100
			emf_ROOT_subjugate_ruler = yes
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(nomad_subjugation): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		FROM = {
			piety = 50
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		ROOT = { piety = -100 }
	}

	on_reverse_demand = {
		log = "CB(nomad_subjugation): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			piety = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
		}
		FROM = {
			piety = 100
			if = {
				limit = {
					has_dlc = "Legacy of Rome"
					emf_has_roman_empire = yes
					OR = { 
						religion_group = christian
						religion = hellenic_pagan
						religion = hellenic_pagan_reformed
					}
				}
				hidden_tooltip = {
					character_event = {
						id = LoR.30
						days = 12
					}
				}
			}
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_invalidation = {
		log = "CB(nomad_subjugation): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			if = {
				limit = {
					NOT = {
						has_opinion_modifier = {
							who = FROM
							modifier = declared_independence_nomad
						}
					}
				}
				remove_character_modifier = launched_subjugation
				prestige = 500
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = {
		factor = 10
		modifier = {
			factor = 0
			ROOT = {
				has_flag = is_temujin
				has_landed_title = e_mongol_empire
				demesne_size = 1
			}
			FROM = {
				is_offmap_governor = offmap_china
			}
		}
		modifier = {
			factor = 2
			FROM = { ai = no }
			had_global_flag = {
				flag = EMF
				days = 730
			}
		}
		modifier = {
			factor = 0
			ROOT = {
				trigger_if = {
					limit = { emf_has_swmh = yes }
					culture = old_hungarian
				}
				trigger_else = {
					culture = hungarian
				}
				religion_group = pagan_group
				capital_scope = { kingdom = { title = k_hungary } }
			}
			NOT = { has_global_flag = emf_conquest_hungary_completed }
			has_global_flag = emf_magyar_migration_completed
			emf_alternate_start = no
		}
		modifier = {
			factor = 0
			OR = {
				ROOT = {
					top_liege = {
						trigger_if = {
							limit = { emf_has_swmh = yes }
							culture = old_hungarian
						}
						trigger_else = {
							culture = hungarian
						}
						religion_group = pagan_group
					}
				}
				FROM = {
					top_liege = {
						trigger_if = {
							limit = { emf_has_swmh = yes }
							culture = old_hungarian
						}
						trigger_else = {
							culture = hungarian
						}
						religion_group = pagan_group
					}
				}
			}
			NOT = { has_global_flag = emf_magyar_migration_completed }
			NOT = { year = 920 }
			emf_alternate_start = no
		}
	}
}

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

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			prestige = 100
			population = 30000
			population_factor = 0.75 # 75% of max
		}
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			is_nomadic = yes
			NOT = { same_realm = FROM }
			OR = {
				ai = yes
				multiplayer = no
				NOT = {
					has_game_rule = {
						name = multiplayer_invasions
						value = off
					}
				}
			}
		}
	}

	can_use_title = { # EMF: only if ROOT has a border with one of FROM's provinces within the kingdom
		emf_cb_can_use_de_jure_title = yes
		any_direct_de_jure_vassal_title = {
			any_direct_de_jure_vassal_title = {
				owner_under_FROM = yes
				location = { any_neighbor_province = { owner_under_ROOT = yes } }
			}
		}
		OR = {
			ROOT = { ai = no }
			FROM = { num_of_realm_counties = { value = 5 title = PREV } }
		}
	}

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

	on_add = {
		log = "CB(nomad_invasion): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		ROOT = { prestige = -100 }
	}

	on_success = {
		log = "CB(nomad_invasion): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		emf_imperial_decay_victory = yes
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		FROM = { prestige = -200 }
		hidden_tooltip = {
			ROOT = {
				set_global_flag = emf_latb_upon_gain
				occupy_minors_of_occupied_settlements = FROM
				gain_all_occupied_titles = {
					who = FROM
					type = invasion
				}
				clr_global_flag = emf_latb_upon_gain
				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 = {
		emf_cb_latb = yes
		pf_liege_change_under_title_begin_effect = yes
		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_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(nomad_invasion): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		FROM = {
			prestige = 100
			hidden_tooltip = {
				if = {
					limit = { has_nickname = no }
					random_list = {
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_hammer }
						10 = { give_nickname = nick_the_avenger }
						10 = { give_nickname = nick_the_lion }
						10 = { give_nickname = nick_the_bold }
					}
				}
			}
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		ROOT = { prestige = -200 }
	}

	on_reverse_demand = {
		log = "CB(nomad_invasion): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		emf_imperial_decay_loss = yes
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			prestige = 200
			hidden_tooltip = {
				if = {
					limit = { has_nickname = no }
					random_list = {
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_hammer }
						10 = { give_nickname = nick_the_avenger }
						10 = { give_nickname = nick_the_lion }
						10 = { give_nickname = nick_the_bold }
					}
				}
			}
			if = {
				limit = {
					has_dlc = "Legacy of Rome"
					emf_has_roman_empire = yes
					OR = { 
						religion_group = christian
						religion = hellenic_pagan
						religion = hellenic_pagan_reformed
					}
				}
				hidden_tooltip = {
					character_event = {
						id = LoR.30
						days = 12
					}
				}
			}
			participation_scaled_prestige = 200
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
	}

	on_invalidation = {
		log = "CB(nomad_invasion): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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
		}
		modifier = {
			factor = 0
			ROOT = {
				has_flag = is_temujin
				has_landed_title = e_mongol_empire
				demesne_size = 1
			}
			FROM = {
				is_offmap_governor = offmap_china
			}
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = {
				flag = EMF
				days = 730
			}
		}
	}
}

nomad_expansion = {
	name = CB_NAME_NOMAD_EXPANSION
	war_name = WAR_NAME_NOMAD_EXPANSION
	sprite = 8
	truce_days = 1825
	is_permanent = yes
	is_holy_war = yes
	check_de_jure_tier = DUKE # this scans all de jure duchies for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	allowed_to_target_tributaries = no
	
	sort_priority = 800

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

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			is_nomadic = yes
			NOT = { same_realm = FROM }
			mercenary = no
		}
	}

	can_use_title = {
		emf_cb_can_use_de_jure_title = yes
		# EMF: only if ROOT has a border with one of FROM's provinces within the duchy
		OR = {
			ROOT = { ai = yes }
			any_direct_de_jure_vassal_title = {
				holder_scope = {
					OR = {
						character = FROM
						is_liege_or_above = FROM
					}
				}
				location = {
					any_neighbor_province = {
						owner = {
							OR = {
								character = ROOT
								is_liege_or_above = ROOT
							}
						}
					}
				}
			}
		}
	}

	is_valid = {
		ROOT = {
			is_nomadic = yes
			NOT = { same_realm = FROM }
		}
	}

	on_add = {
		log = "CB(nomad_expansion): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		ROOT = { prestige = -300 }
	}

	on_success = {
		log = "CB(nomad_expansion): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		emf_imperial_decay_victory = 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_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 = { }
					}
				}
			}
		}
		ROOT = {
			if = {
				limit = { uses_decadence = yes }
				participation_scaled_decadence = -10
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = 200
				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -10
				}
			}
		}
		FROM = { prestige = -200 }
	}

	on_success_title = {
		emf_cb_latb = 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
					}
				}
			}
		}
		pf_liege_change_under_title_end_effect = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(nomad_expansion): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = -200 }
	}

	on_reverse_demand = {
		log = "CB(nomad_expansion): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		emf_imperial_decay_loss = yes
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			prestige = -200
		}
		FROM = {
			participation_scaled_prestige = 300
			if = {
				limit = { uses_decadence = yes }
				participation_scaled_decadence = -10
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_prestige = 300
				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -10
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(nomad_expansion): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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 = 0
			ROOT = {
				has_flag = is_temujin
				has_landed_title = e_mongol_empire
				demesne_size = 1
			}
			FROM = {
				is_offmap_governor = offmap_china
			}
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = {
				flag = EMF
				days = 730
			}
		}
	}
}

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
	capturing_attacker_is_complete_victory = no

	can_use = {
		ROOT = { vassal_of = FROM }
	}

	can_use_title = {
		higher_tier_than = BARON
		is_primary_holder_title = yes
		emf_laws_has_full_council_power = no
	}

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

	on_add = {
		log = "CB(increase_council_power_war): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_dynlevy_begin = yes
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(increase_council_power_war): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		emf_cb_dynlevy_end = yes
		ROOT = {
			opinion = {
				modifier = opinion_increased_council_power
				who = FROM
				months = 12
			}
			participation_scaled_prestige = 100
			hidden_tooltip = { disband_event_forces = faction_revolters }
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
			hidden_tooltip = { disband_event_forces = faction_revolters }
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_success_title = {
		increase_council_power_effect = yes
		increase_vassal_freedom_effect = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(increase_council_power_war): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_dynlevy_end = yes
		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 = {
		log = "CB(increase_council_power_war): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		emf_cb_dynlevy_end = yes
		ROOT = {
			prestige = -200
			prisoner = FROM
		}
		FROM = {
			participation_scaled_prestige = 100
			hidden_tooltip = { 
				if = { 
					limit = { NOT = { has_opinion_modifier = { modifier = opinion_traitor who = ROOT } } } #Allow free revocation of a title.
					opinion = {
						who = ROOT
						modifier = opinion_traitor
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		hidden_tooltip = {
			any_attacker = {
				limit = {
					OR = {
						vassal_of = FROM
						liege_before_war = { character = FROM }
					}
				}
				prisoner = FROM
				disband_event_forces = faction_revolters
				add_character_modifier = {
					name = faction_council_power_ultimatum_timer
					hidden = yes
					duration = 3650
				}
				if = { # Safety to make sure vassal hierarchies are kept the same after the revolt
					limit = { NOT = { vassal_of = FROM } }
					set_defacto_liege = FROM
				}
			}
		}
		FROM = { hidden_tooltip = { disband_event_forces = faction_loyalists } }
	}

	on_invalidation = {
		log = "CB(increase_council_power_war): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_dynlevy_end = 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
	}
}

nomad_humiliate_cb = {
	name = CB_NAME_HUMILIATE
	war_name = WAR_NAME_HUMILIATE
	sprite = 1
	truce_days = 3650
	is_permanent = yes
	hostile_against_others = yes
	can_ask_to_join_war = no
	display_on_map = no
	allowed_to_target_tributaries = no
	defender_unoccupied_warscore = yes
	
	sort_priority = 815
	
	can_use_gui = {
		emf_cb_can_use_gui = yes
	}

	can_use = {
		emf_cb_can_use = yes
		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 = {
						owner_under_ROOT = yes
						AND = {
							is_land = no
							any_neighbor_province = {
								OR = {
									owner_under_ROOT = yes
									AND = {
										is_land = no
										any_neighbor_province = {
											owner_under_ROOT = yes
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}

	on_add = {
		log = "CB(nomad_humiliate_cb): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(nomad_humiliate_cb): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		ROOT = {
			prestige = 300
			steal_population_scaled = {
				target = FROM
				percentage = 0.25
			}
		}
		FROM = { prestige = -300 }
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(nomad_humiliate_cb): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = -100 }
		FROM = { prestige = 100 }
	}

	on_reverse_demand = {
		log = "CB(nomad_humiliate_cb): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = { prestige = 200 }
	}

	on_invalidation = {
		log = "CB(nomad_humiliate_cb): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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 }
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = {
				flag = EMF
				days = 730
			}
		}
	}
}

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 = { always = no }
	is_valid = { always = no }
	attacker_ai_victory_worth = { factor = -1 }
	attacker_ai_defeat_worth = { factor = 100 }
	defender_ai_victory_worth = { factor = -1 }
	defender_ai_defeat_worth = { factor = 100 }
	ai_will_do = { factor = 0 }
}

### Reconquista Wars ###
# Leon & Asturia #
reconquista_leon = {
	name = CB_NAME_RECONQUISTA_LEON
	war_name = WAR_NAME_RECONQUISTA_LEON
	diplo_view_region = custom_leon
	sprite = 9
	truce_days = 3650
	is_permanent = no # EMF: HF Reconquista feature disabled in Furry1 because it is war too OP in present form
	is_holy_war = yes
	battle_warscore_mult = 0.5
	#max_defender_occupation_score = 85
	allow_whitepeace = no

	allowed_to_target_tributaries = yes
	can_ask_to_join_war = yes

	sort_priority = 10000

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

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			has_flag = currently_reconquering_iberia
			not_reconquista_check_trigger = yes
			any_realm_province = {
				owner_under_ROOT = yes
				OR = {
					AND = {
						any_neighbor_province = {
							region = custom_leon
						}
					}
					AND = {
						NOT = {
							any_neighbor_province = {
								region = custom_leon
							}
						}
						NOT = {
							any_neighbor_province = {
								region = world_europe_west_iberia
								owner = {
									OR = {
										AND = {
											independent = yes
											NOT = {
												religion_group = christian
											}
										}
										top_liege = {
											NOT = {
												religion_group = christian
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								region = custom_leon
							}
						}
					}
					AND = {
						NOR = {
							any_neighbor_province = {
								region = custom_leon
							}
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_leon
								}
							}
						}
						NOT = {
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = world_europe_west_iberia
									owner = {
										OR = {
											AND = {
												independent = yes
												NOT = {
													religion_group = christian
												}
											}
											top_liege = {
												NOT = {
													religion_group = christian
												}
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_leon
								}
							}
						}
					}
				}
			}
			conditional_tooltip = {
				trigger = {
					ai = yes
				}
				NOT = {
					has_character_modifier = reconquista_preparation_duration
				}
			}
		}
		FROM = {
			any_realm_province = {
				region = custom_leon
				owner_under_PREV = yes
				OR = {
					de_jure_liege_or_above = d_leon
					de_jure_liege_or_above = d_asturias # CP-3.0: TODO: this is not a valid title on SWMH
				}
			}
			NOR = {
				religion_group = ROOT
				ROOT = { emf_syncretized_religion_with_PREV = yes }
			}
		}
	}

	is_valid = {
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = reconquista_valid_TT
				has_flag = currently_reconquering_iberia
			}
		}
		custom_tooltip = {
			text = reconquista_valid_2_TT
			FROM = {
				NOR = {
					religion_group = ROOT
					ROOT = { emf_syncretized_religion_with_PREV = yes }
				}
			}
		}
	}

	on_add = {
		log = "CB(reconquista_leon): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		custom_tooltip = {
			text = add_allies_reconquista_TT
			ROOT = {
				character_event = { id = HF.49210 }
			}
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(reconquista_leon): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		d_leon = {
			save_event_target_as = first_duchy_title
		}
		d_asturias = {
			save_event_target_as = second_duchy_title
		}
		if = {
			limit = {
				k_asturias = {
					is_de_jure_title = no
				}
			}
			k_leon = {
				save_event_target_as = reconquista_kingdom_title
			}
		}
		else = {
			k_asturias = {
				save_event_target_as = reconquista_kingdom_title
			}
		}
		reconquista_win_effect = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_reverse_demand = {
		log = "CB(reconquista_leon): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		reconquista_lose_effect = yes
	}

	on_invalidation = {
		log = "CB(reconquista_leon): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		hidden_effect = {
			ROOT = {
				character_event = { id = HF.49200 }
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

# Castille & Toledo #
reconquista_castille = {
	name = CB_NAME_RECONQUISTA_CASTILLE
	war_name = WAR_NAME_RECONQUISTA_CASTILLE
	diplo_view_region = custom_castille
	sprite = 9
	truce_days = 3650
	is_permanent = no # EMF: HF Reconquista feature disabled in Furry1 because it is war too OP in present form
	is_holy_war = yes
	battle_warscore_mult = 0.5
	#max_defender_occupation_score = 85
	allow_whitepeace = no

	allowed_to_target_tributaries = yes
	can_ask_to_join_war = yes

	sort_priority = 10000

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

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			has_flag = currently_reconquering_iberia
			not_reconquista_check_trigger = yes
			any_realm_province = {
				OR = {
					AND = {
						any_neighbor_province = {
							region = custom_castille
						}
					}
					AND = {
						NOT = {
							any_neighbor_province = {
								region = custom_castille
							}
						}
						NOT = {
							any_neighbor_province = {
								region = world_europe_west_iberia
								owner = {
									OR = {
										AND = {
											independent = yes
											NOT = {
												religion_group = christian
											}
										}
										top_liege = {
											NOT = {
												religion_group = christian
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								region = custom_castille
							}
						}
					}
					AND = {
						NOR = {
							any_neighbor_province = {
								region = custom_castille
							}
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_castille
								}
							}
						}
						NOT = {
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = world_europe_west_iberia
									owner = {
										OR = {
											AND = {
												independent = yes
												NOT = {
													religion_group = christian
												}
											}
											top_liege = {
												NOT = {
													religion_group = christian
												}
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_castille
								}
							}
						}
					}
				}
			}
			conditional_tooltip = {
				trigger = {
					ai = yes
				}
				NOT = {
					has_character_modifier = reconquista_preparation_duration
				}
			}
		}
		FROM = {
			any_realm_province = {
				region = custom_castille
				owner_under_PREV = yes
				OR = {
					de_jure_liege_or_above = d_castilla
					de_jure_liege_or_above = d_toledo
				}
			}
			NOR = {
				religion_group = ROOT
				ROOT = { emf_syncretized_religion_with_PREV = yes }
			}
		}
	}

	is_valid = {
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = reconquista_valid_TT
				has_flag = currently_reconquering_iberia
			}
		}
		custom_tooltip = {
			text = reconquista_valid_2_TT
			FROM = {
				NOR = {
					religion_group = ROOT
					ROOT = { emf_syncretized_religion_with_PREV = yes }
				}
			}
		}
	}

	on_add = {
		log = "CB(reconquista_castille): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		custom_tooltip = {
			text = add_allies_reconquista_TT
			ROOT = {
				character_event = { id = HF.49210 }
			}
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(reconquista_castille): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		d_castilla = {
			save_event_target_as = first_duchy_title
		}
		d_toledo = {
			save_event_target_as = second_duchy_title
		}
		k_castille = {
			save_event_target_as = reconquista_kingdom_title
		}
		reconquista_win_effect = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_reverse_demand = {
		log = "CB(reconquista_castille): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		reconquista_lose_effect = yes
	}

	on_invalidation = {
		log = "CB(reconquista_castille): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		hidden_effect = {
			ROOT = {
				character_event = { id = HF.49200 }
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

# Aragon & Barcelona #
reconquista_aragon = {
	name = CB_NAME_RECONQUISTA_ARAGON
	war_name = WAR_NAME_RECONQUISTA_ARAGON
	diplo_view_region = custom_aragon
	sprite = 9
	truce_days = 3650
	is_permanent = no # EMF: HF Reconquista feature disabled in Furry1 because it is war too OP in present form
	is_holy_war = yes
	battle_warscore_mult = 0.5
	#max_defender_occupation_score = 85
	allow_whitepeace = no

	allowed_to_target_tributaries = yes
	can_ask_to_join_war = yes

	sort_priority = 10000

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

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			has_flag = currently_reconquering_iberia
			not_reconquista_check_trigger = yes
			any_realm_province = {
				OR = {
					AND = {
						any_neighbor_province = {
							region = custom_aragon
						}
					}
					AND = {
						NOT = {
							any_neighbor_province = {
								region = custom_aragon
							}
						}
						NOT = {
							any_neighbor_province = {
								region = world_europe_west_iberia
								owner = {
									OR = {
										AND = {
											independent = yes
											NOT = {
												religion_group = christian
											}
										}
										top_liege = {
											NOT = {
												religion_group = christian
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								region = custom_aragon
							}
						}
					}
					AND = {
						NOR = {
							any_neighbor_province = {
								region = custom_aragon
							}
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_aragon
								}
							}
						}
						NOT = {
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = world_europe_west_iberia
									owner = {
										OR = {
											AND = {
												independent = yes
												NOT = {
													religion_group = christian
												}
											}
											top_liege = {
												NOT = {
													religion_group = christian
												}
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_aragon
								}
							}
						}
					}
				}
			}
			conditional_tooltip = {
				trigger = {
					ai = yes
				}
				NOT = {
					has_character_modifier = reconquista_preparation_duration
				}
			}
		}
		FROM = {
			any_realm_province = {
				region = custom_aragon
				owner_under_PREV = yes
				OR = {
					de_jure_liege_or_above = d_aragon
					de_jure_liege_or_above = d_barcelona
				}
			}
			NOR = {
				religion_group = ROOT
				ROOT = { emf_syncretized_religion_with_PREV = yes }
			}
		}
	}

	is_valid = {
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = reconquista_valid_TT
				has_flag = currently_reconquering_iberia
			}
		}
		custom_tooltip = {
			text = reconquista_valid_2_TT
			FROM = {
				NOR = {
					religion_group = ROOT
					ROOT = { emf_syncretized_religion_with_PREV = yes }
				}
			}
		}
	}

	on_add = {
		log = "CB(reconquista_aragon): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		custom_tooltip = {
			text = add_allies_reconquista_TT
			ROOT = {
				character_event = { id = HF.49210 }
			}
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(reconquista_aragon): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		d_aragon = {
			save_event_target_as = first_duchy_title
		}
		d_barcelona = {
			save_event_target_as = second_duchy_title
		}
		k_aragon = {
			save_event_target_as = reconquista_kingdom_title
		}
		reconquista_win_effect = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_reverse_demand = {
		log = "CB(reconquista_aragon): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		reconquista_lose_effect = yes
	}

	on_invalidation = {
		log = "CB(reconquista_aragon): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		hidden_effect = {
			ROOT = {
				character_event = { id = HF.49200 }
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

# Galicia & Porto #
reconquista_galicia = {
	name = CB_NAME_RECONQUISTA_GALICIA
	war_name = WAR_NAME_RECONQUISTA_GALICIA
	diplo_view_region = custom_galicia
	sprite = 9
	truce_days = 3650
	is_permanent = no # EMF: HF Reconquista feature disabled in Furry1 because it is war too OP in present form
	is_holy_war = yes
	battle_warscore_mult = 0.5
	#max_defender_occupation_score = 85
	allow_whitepeace = no

	allowed_to_target_tributaries = yes
	can_ask_to_join_war = yes

	sort_priority = 10000

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			prisoner = no
			is_adult = yes
		}
	}
	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			has_flag = currently_reconquering_iberia
			not_reconquista_check_trigger = yes
			any_realm_province = {
				OR = {
					AND = {
						any_neighbor_province = {
							region = custom_galicia
						}
					}
					AND = {
						NOT = {
							any_neighbor_province = {
								region = custom_galicia
							}
						}
						NOT = {
							any_neighbor_province = {
								region = world_europe_west_iberia
								owner = {
									OR = {
										AND = {
											independent = yes
											NOT = {
												religion_group = christian
											}
										}
										top_liege = {
											NOT = {
												religion_group = christian
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								region = custom_galicia
							}
						}
					}
					AND = {
						NOR = {
							any_neighbor_province = {
								region = custom_galicia
							}
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_galicia 
								}
							}
						}
						NOT = {
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = world_europe_west_iberia
									owner = {
										OR = {
											AND = {
												independent = yes
												NOT = {
													religion_group = christian
												}
											}
											top_liege = {
												NOT = {
													religion_group = christian
												}
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_galicia
								}
							}
						}
					}
				}
			}
			conditional_tooltip = {
				trigger = {
					ai = yes
				}
				NOT = {
					has_character_modifier = reconquista_preparation_duration
				}
			}
		}
		FROM = {
			any_realm_province = {
				region = custom_galicia
				owner_under_PREV = yes
				OR = {
					de_jure_liege_or_above = d_galicia
					de_jure_liege_or_above = d_porto
				}
			}
			NOR = {
				religion_group = ROOT
				ROOT = { emf_syncretized_religion_with_PREV = yes }
			}
		}
	}

	is_valid = {
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = reconquista_valid_TT
				has_flag = currently_reconquering_iberia
			}
		}
		custom_tooltip = {
			text = reconquista_valid_2_TT
			FROM = {
				NOR = {
					religion_group = ROOT
					ROOT = { emf_syncretized_religion_with_PREV = yes }
				}
			}
		}
	}

	on_add = {
		log = "CB(reconquista_galicia): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		custom_tooltip = {
			text = add_allies_reconquista_TT
			ROOT = {
				character_event = { id = HF.49210 }
			}
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(reconquista_galicia): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		d_galicia = {
			save_event_target_as = first_duchy_title
		}
		d_porto = {
			save_event_target_as = second_duchy_title
		}
		if = {
			limit = {
				k_spanish_galicia = {
					is_de_jure_title = yes
				}
			}
			k_spanish_galicia = {
				save_event_target_as = reconquista_kingdom_title
			}
		}
		reconquista_win_effect = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_reverse_demand = {
		log = "CB(reconquista_galicia): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		reconquista_lose_effect = yes
	}

	on_invalidation = {
		log = "CB(reconquista_galicia): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		hidden_effect = {
			ROOT = {
				character_event = { id = HF.49200 }
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

# Beja & Algarve #
reconquista_beja = {
	name = CB_NAME_RECONQUISTA_BEJA
	war_name = WAR_NAME_RECONQUISTA_BEJA
	diplo_view_region = custom_beja
	sprite = 9
	truce_days = 3650
	is_permanent = no # EMF: HF Reconquista feature disabled in Furry1 because it is war too OP in present form
	is_holy_war = yes
	battle_warscore_mult = 0.5
	#max_defender_occupation_score = 85
	allow_whitepeace = no

	allowed_to_target_tributaries = yes
	can_ask_to_join_war = yes

	sort_priority = 10000

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

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			has_flag = currently_reconquering_iberia
			not_reconquista_check_trigger = yes			
			any_realm_province = {
				OR = {
					AND = {
						any_neighbor_province = {
							region = custom_beja
						}
					}
					AND = {
						NOT = {
							any_neighbor_province = {
								region = custom_beja
							}
						}
						NOT = {
							any_neighbor_province = {
								region = world_europe_west_iberia
								owner = {
									OR = {
										AND = {
											independent = yes
											NOT = {
												religion_group = christian
											}
										}
										top_liege = {
											NOT = {
												religion_group = christian
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								region = custom_beja
							}
						}
					}
					AND = {
						NOR = {
							any_neighbor_province = {
								region = custom_beja
							}
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_beja 
								}
							}
						}
						NOT = {
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = world_europe_west_iberia
									owner = {
										OR = {
											AND = {
												independent = yes
												NOT = {
													religion_group = christian
												}
											}
											top_liege = {
												NOT = {
													religion_group = christian
												}
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_beja
								}
							}
						}
					}
				}
			}
			conditional_tooltip = {
				trigger = {
					ai = yes
				}
				NOT = {
					has_character_modifier = reconquista_preparation_duration
				}
			}
		}
		FROM = {
			any_realm_province = {
				region = custom_beja
				owner_under_PREV = yes
				OR = {
					de_jure_liege_or_above = d_beja
					de_jure_liege_or_above = d_algarve
				}
			}
			NOR = {
				religion_group = ROOT
				ROOT = { emf_syncretized_religion_with_PREV = yes }
			}
		}
	}

	is_valid = {
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = reconquista_valid_TT
				has_flag = currently_reconquering_iberia
			}
		}
		custom_tooltip = {
			text = reconquista_valid_2_TT
			FROM = {
				NOR = {
					religion_group = ROOT
					ROOT = { emf_syncretized_religion_with_PREV = yes }
				}
			}
		}
	}

	on_add = {
		log = "CB(reconquista_beja): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		custom_tooltip = {
			text = add_allies_reconquista_TT
			ROOT = {
				character_event = { id = HF.49210 }
			}
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(reconquista_beja): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		d_beja = {
			save_event_target_as = first_duchy_title
		}
		d_algarve = {
			save_event_target_as = second_duchy_title
		}
		d_balata = {
			save_event_target_as = third_duchy_title
		}
		reconquista_win_effect = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_reverse_demand = {
		log = "CB(reconquista_beja): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		reconquista_lose_effect = yes
	}

	on_invalidation = {
		log = "CB(reconquista_beja): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		hidden_effect = {
			ROOT = {
				character_event = { id = HF.49200 }
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

# Granada & Sevilla #
reconquista_granada = {
	name = CB_NAME_RECONQUISTA_GRANADA
	war_name = WAR_NAME_RECONQUISTA_GRANADA
	diplo_view_region = custom_granada
	sprite = 9
	truce_days = 3650
	is_permanent = no # EMF: HF Reconquista feature disabled in Furry1 because it is war too OP in present form
	is_holy_war = yes
	battle_warscore_mult = 0.5
	#max_defender_occupation_score = 85
	allow_whitepeace = no

	allowed_to_target_tributaries = yes
	can_ask_to_join_war = yes

	sort_priority = 10000

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

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			has_flag = currently_reconquering_iberia
			not_reconquista_check_trigger = yes	
			any_realm_province = {
				OR = {
					AND = {
						any_neighbor_province = {
							region = custom_granada
						}
					}
					AND = {
						NOT = {
							any_neighbor_province = {
								region = custom_granada
							}
						}
						NOT = {
							any_neighbor_province = {
								region = world_europe_west_iberia
								owner = {
									OR = {
										AND = {
											independent = yes
											NOT = {
												religion_group = christian
											}
										}
										top_liege = {
											NOT = {
												religion_group = christian
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								region = custom_granada
							}
						}
					}
					AND = {
						NOR = {
							any_neighbor_province = {
								region = custom_granada
							}
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_granada 
								}
							}
						}
						NOT = {
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = world_europe_west_iberia
									owner = {
										OR = {
											AND = {
												independent = yes
												NOT = {
													religion_group = christian
												}
											}
											top_liege = {
												NOT = {
													religion_group = christian
												}
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_granada
								}
							}
						}
					}
				}
			}
			conditional_tooltip = {
				trigger = {
					ai = yes
				}
				NOT = {
					has_character_modifier = reconquista_preparation_duration
				}
			}
		}
		FROM = {
			any_realm_province = {
				region = custom_granada
				owner_under_PREV = yes
				OR = {
					de_jure_liege_or_above = d_granada
					de_jure_liege_or_above = d_sevilla
				}
			}
			NOR = {
				religion_group = ROOT
				ROOT = { emf_syncretized_religion_with_PREV = yes }
			}
		}
	}

	is_valid = {
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = reconquista_valid_TT
				has_flag = currently_reconquering_iberia
			}
		}
		custom_tooltip = {
			text = reconquista_valid_2_TT
			FROM = {
				NOR = {
					religion_group = ROOT
					ROOT = { emf_syncretized_religion_with_PREV = yes }
				}
			}
		}
	}

	on_add = {
		log = "CB(reconquista_granada): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		custom_tooltip = {
			text = add_allies_reconquista_TT
			ROOT = {
				character_event = { id = HF.49210 }
			}
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(reconquista_granada): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		d_granada = {
			save_event_target_as = first_duchy_title
		}
		d_sevilla = {
			save_event_target_as = second_duchy_title
		}
		reconquista_win_effect = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_reverse_demand = {
		log = "CB(reconquista_granada): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		reconquista_lose_effect = yes
	}

	on_invalidation = {
		log = "CB(reconquista_granada): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		hidden_effect = {
			ROOT = {
				character_event = { id = HF.49200 }
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

# Cordoba & Badajoz #
reconquista_cordoba = {
	name = CB_NAME_RECONQUISTA_CORDOBA
	war_name = WAR_NAME_RECONQUISTA_CORDOBA
	diplo_view_region = custom_cordoba
	sprite = 9
	truce_days = 3650
	is_permanent = no # EMF: HF Reconquista feature disabled in Furry1 because it is war too OP in present form
	is_holy_war = yes
	battle_warscore_mult = 0.5
	#max_defender_occupation_score = 85
	allow_whitepeace = no

	allowed_to_target_tributaries = yes
	can_ask_to_join_war = yes

	sort_priority = 10000

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

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			has_flag = currently_reconquering_iberia
			not_reconquista_check_trigger = yes			
			any_realm_province = {
				OR = {
					AND = {
						any_neighbor_province = {
							region = custom_cordoba
						}
					}
					AND = {
						NOT = {
							any_neighbor_province = {
								region = custom_cordoba
							}
						}
						NOT = {
							any_neighbor_province = {
								region = world_europe_west_iberia
								owner = {
									OR = {
										AND = {
											independent = yes
											NOT = {
												religion_group = christian
											}
										}
										top_liege = {
											NOT = {
												religion_group = christian
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {	
							is_land = yes
							any_neighbor_province = {
								region = custom_cordoba
							}
						}
					}
					AND = {
						NOR = {
							any_neighbor_province = {
								region = custom_cordoba
							}
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_cordoba 
								}
							}
						}
						NOT = {
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = world_europe_west_iberia
									owner = {
										OR = {
											AND = {
												independent = yes
												NOT = {
													religion_group = christian
												}
											}
											top_liege = {
												NOT = {
													religion_group = christian
												}
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_cordoba
								}
							}
						}
					}
				}
			}
			conditional_tooltip = {
				trigger = {
					ai = yes
				}
				NOT = {
					has_character_modifier = reconquista_preparation_duration
				}
			}
		}
		FROM = {
			any_realm_province = {
				region = custom_cordoba
				owner_under_PREV = yes
				OR = {
					de_jure_liege_or_above = d_cordoba
					de_jure_liege_or_above = d_badajoz
				}
			}
			NOR = {
				religion_group = ROOT
				ROOT = { emf_syncretized_religion_with_PREV = yes }
			}
		}
	}

	is_valid = {
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = reconquista_valid_TT
				has_flag = currently_reconquering_iberia
			}
		}
		custom_tooltip = {
			text = reconquista_valid_2_TT
			FROM = {
				NOR = {
					religion_group = ROOT
					ROOT = { emf_syncretized_religion_with_PREV = yes }
				}
			}
		}
	}

	on_add = {
		log = "CB(reconquista_cordoba): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		custom_tooltip = {
			text = add_allies_reconquista_TT
			ROOT = {
				character_event = { id = HF.49210 }
			}
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(reconquista_cordoba): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		d_cordoba = {
			save_event_target_as = first_duchy_title
		}
		d_badajoz = {
			save_event_target_as = second_duchy_title
		}
		reconquista_win_effect = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_reverse_demand = {
		log = "CB(reconquista_cordoba): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		reconquista_lose_effect = yes
	}

	on_invalidation = {
		log = "CB(reconquista_cordoba): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		hidden_effect = {
			ROOT = {
				character_event = { id = HF.49200 }
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

# Valencia & Murcia #
reconquista_valencia = {
	name = CB_NAME_RECONQUISTA_VALENCIA
	war_name = WAR_NAME_RECONQUISTA_VALENCIA
	diplo_view_region = custom_valencia
	sprite = 9
	truce_days = 3650
	is_permanent = no # EMF: HF Reconquista feature disabled in Furry1 because it is war too OP in present form
	is_holy_war = yes
	battle_warscore_mult = 0.5
	#max_defender_occupation_score = 85
	allow_whitepeace = no

	allowed_to_target_tributaries = yes
	can_ask_to_join_war = yes

	sort_priority = 10000

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

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			has_flag = currently_reconquering_iberia
			not_reconquista_check_trigger = yes
			any_realm_province = {
				OR = {
					AND = {
						any_neighbor_province = {
							region = custom_valencia
						}
					}
					AND = {
						NOT = {
							any_neighbor_province = {
								region = custom_valencia
							}
						}
						NOT = {
							any_neighbor_province = {
								region = world_europe_west_iberia
								owner = {
									OR = {
										AND = {
											independent = yes
											NOT = {
												religion_group = christian
											}
										}
										top_liege = {
											NOT = {
												religion_group = christian
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								region = custom_valencia
							}
						}
					}
					AND = {
						NOR = {
							any_neighbor_province = {
								region = custom_valencia
							}
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_valencia 
								}
							}
						}
						NOT = {
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = world_europe_west_iberia
									owner = {
										OR = {
											AND = {
												independent = yes
												NOT = {
													religion_group = christian
												}
											}
											top_liege = {
												NOT = {
													religion_group = christian
												}
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_valencia
								}
							}
						}
					}
				}
			}
			conditional_tooltip = {
				trigger = {
					ai = yes
				}
				NOT = {
					has_character_modifier = reconquista_preparation_duration
				}
			}
		}
		FROM = {
			any_realm_province = {
				region = custom_valencia
				owner_under_PREV = yes
				OR = {
					de_jure_liege_or_above = d_valencia
					de_jure_liege_or_above = d_murcia
				}
			}
			NOR = {
				religion_group = ROOT
				ROOT = { emf_syncretized_religion_with_PREV = yes }
			}
		}
	}

	is_valid = {
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = reconquista_valid_TT
				has_flag = currently_reconquering_iberia
			}
		}
		custom_tooltip = {
			text = reconquista_valid_2_TT
			FROM = {
				NOR = {
					religion_group = ROOT
					ROOT = { emf_syncretized_religion_with_PREV = yes }
				}
			}
		}
	}

	on_add = {
		log = "CB(reconquista_valencia): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		custom_tooltip = {
			text = add_allies_reconquista_TT
 			ROOT = {
				character_event = { id = HF.49210 }
			}
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(reconquista_valencia): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		d_valencia = {
			save_event_target_as = first_duchy_title
		}
		d_murcia = {
			save_event_target_as = second_duchy_title
		}
		reconquista_win_effect = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_reverse_demand = {
		log = "CB(reconquista_valencia): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		reconquista_lose_effect = yes
	}

	on_invalidation = {
		log = "CB(reconquista_valencia): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		hidden_effect = {
			ROOT = {
				character_event = { id = HF.49200 }
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

# Navarra #
reconquista_navarra = {
	name = CB_NAME_RECONQUISTA_NAVARRA
	war_name = WAR_NAME_RECONQUISTA_NAVARRA
	diplo_view_region = custom_navarra
	sprite = 9
	truce_days = 3650
	is_permanent = no # EMF: HF Reconquista feature disabled in Furry1 because it is war too OP in present form
	is_holy_war = yes
	battle_warscore_mult = 0.5
	#max_defender_occupation_score = 85
	allow_whitepeace = no

	allowed_to_target_tributaries = yes
	can_ask_to_join_war = yes

	sort_priority = 10000

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

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			has_flag = currently_reconquering_iberia
			not_reconquista_check_trigger = yes
			any_realm_province = {
				OR = {
					AND = {
						any_neighbor_province = {
							region = custom_navarra
						}
					}
					AND = {
						NOT = {
							any_neighbor_province = {
								region = custom_navarra
							}
						}
						NOT = {
							any_neighbor_province = {
								region = world_europe_west_iberia
								owner = {
									OR = {
										AND = {
											independent = yes
											NOT = {
												religion_group = christian
											}
										}
										top_liege = {
											NOT = {
												religion_group = christian
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								region = custom_navarra
							}
						}
					}
					AND = {
						NOR = {
							any_neighbor_province = {
								region = custom_navarra
							}
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_navarra 
								}
							}
						}
						NOT = {
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = world_europe_west_iberia
									owner = {
										OR = {
											AND = {
												independent = yes
												NOT = {
													religion_group = christian
												}
											}
											top_liege = {
												NOT = {
													religion_group = christian
												}
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							is_land = yes
							any_neighbor_province = {
								is_land = yes
								any_neighbor_province = {
									region = custom_navarra
								}
							}
						}
					}
				}
			}
			conditional_tooltip = {
				trigger = {
					ai = yes
				}
				NOT = {
					has_character_modifier = reconquista_preparation_duration
				}
			}
		}
		FROM = {
			any_realm_province = {
				region = custom_navarra
				owner_under_PREV = yes
				de_jure_liege_or_above = d_navarra
			}
			NOR = {
				religion_group = ROOT
				ROOT = { emf_syncretized_religion_with_PREV = yes }
			}
		}
	}

	is_valid = {
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = reconquista_valid_TT
				has_flag = currently_reconquering_iberia
			}
		}
		custom_tooltip = {
			text = reconquista_valid_2_TT
			FROM = {
				NOR = {
					religion_group = ROOT
					ROOT = { emf_syncretized_religion_with_PREV = yes }
				}
			}
		}
	}

	on_add = {
		log = "CB(reconquista_navarra): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		custom_tooltip = {
			text = add_allies_reconquista_TT
			ROOT = {
				character_event = { id = HF.49210 }
			}
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(reconquista_navarra): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		d_navarra = {
			save_event_target_as = first_duchy_title
		}
		k_navarra = {
			save_event_target_as = reconquista_kingdom_title
		}
		reconquista_win_effect = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_reverse_demand = {
		log = "CB(reconquista_navarra): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		reconquista_lose_effect = yes
	}

	on_invalidation = {
		log = "CB(reconquista_navarra): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		hidden_effect = {
			ROOT = {
				character_event = { id = HF.49200 }
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

# Balearic Islands #
reconquista_balearic = {
	name = CB_NAME_RECONQUISTA_BALEARIC
	war_name = WAR_NAME_RECONQUISTA_BALEARIC
	diplo_view_region = custom_balearic
	sprite = 9
	truce_days = 3650
	is_permanent = no # EMF: HF Reconquista feature disabled in Furry1 because it is war too OP in present form
	is_holy_war = yes
	battle_warscore_mult = 0.5
	#max_defender_occupation_score = 85
	allow_whitepeace = no

	allowed_to_target_tributaries = yes
	can_ask_to_join_war = yes

	sort_priority = 10000

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

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			has_flag = currently_reconquering_iberia
			not_reconquista_check_trigger = yes			
			any_realm_province = {
				OR = {
					AND = {
						any_neighbor_province = {
							region = custom_balearic
						}
					}
					AND = {
						NOT = {
							any_neighbor_province = {
								region = custom_balearic
							}
						}
						NOT = {
							any_neighbor_province = {
								region = world_europe_west_iberia
								owner = {
									OR = {
										AND = {
											independent = yes
											NOT = {
												religion_group = christian
											}
										}
										top_liege = {
											NOT = {
												religion_group = christian
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							any_neighbor_province = {
								region = custom_balearic
							}
						}
					}
					AND = {
						NOR = {
							any_neighbor_province = {
								region = custom_balearic
							}
							any_neighbor_province = {
								any_neighbor_province = {
									region = custom_balearic 
								}
							}
						}
						NOT = {
							any_neighbor_province = {
								any_neighbor_province = {
									region = world_europe_west_iberia
									owner = {
										OR = {
											AND = {
												independent = yes
												NOT = {
													religion_group = christian
												}
											}
											top_liege = {
												NOT = {
													religion_group = christian
												}
											}
										}
									}
								}
							}
						}
						any_neighbor_province = {
							any_neighbor_province = {
								any_neighbor_province = {
									region = custom_balearic
								}
							}
						}
					}
				}
			}
			conditional_tooltip = {
				trigger = {
					ai = yes
				}
				NOT = {
					has_character_modifier = reconquista_preparation_duration
				}
			}
		}
		FROM = {
			any_realm_province = {
				region = custom_balearic
				owner_under_PREV = yes
				de_jure_liege_or_above = d_mallorca
			}
			NOR = {
				religion_group = ROOT
				ROOT = { emf_syncretized_religion_with_PREV = yes }
			}
		}
	}

	is_valid = {
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = reconquista_valid_TT
				has_flag = currently_reconquering_iberia
			}
		}
		custom_tooltip = {
			text = reconquista_valid_2_TT
			FROM = {
				NOR = {
					religion_group = ROOT
					ROOT = { emf_syncretized_religion_with_PREV = yes }
				}
			}
		}
	}

	on_add = {
		log = "CB(reconquista_balearic): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		custom_tooltip = {
			text = add_allies_reconquista_TT
			ROOT = {
				character_event = { id = HF.49210 }
			}
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(reconquista_balearic): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		d_mallorca = {
			save_event_target_as = first_duchy_title
		}
		reconquista_win_effect = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_reverse_demand = {
		log = "CB(reconquista_balearic): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		reconquista_lose_effect = yes
	}

	on_invalidation = {
		log = "CB(reconquista_balearic): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		hidden_effect = {
			ROOT = {
				character_event = { id = HF.49200 }
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}


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

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

	can_use = {
		always = no
	}

	is_valid = {
		ROOT = {
			custom_tooltip = {
				text = THANKMAR_REBELLION_01_TT
				character = 261394
			}
		}
		FROM = {
			custom_tooltip = {
				text = THANKMAR_REBELLION_02_TT
				character = 1282
			}
			has_landed_title = k_germany
		}
	}

	on_add = {
		log = "CB(thankmars_rebellion): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(thankmars_rebellion): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		custom_tooltip = { text = THANKMARS_REBELLION_VICTORY_TT }
		hidden_tooltip = {
			ROOT = {
				remove_trait = bastard
				any_child_even_if_dead = {
					if = {
						limit = { dynasty = ROOT }
						dynasty = 669
					}
					any_child_even_if_dead = {
						if = {
							limit = { dynasty = ROOT }
							dynasty = 669
						}
						any_child_even_if_dead = {
							limit = { dynasty = ROOT }
							dynasty = 669
						}
					}
				}
				dynasty = 669
				recalc_succession = yes
			}
			FROM = {
				any_demesne_title = {
					grant_title_no_opinion = { target = ROOT type = revolt }
				}
				imprison = ROOT
			}
			any_attacker = {
				limit = {
					lower_tier_than = ROOT
				}
				set_defacto_liege = ROOT
				emf_liege_change = yes
			}
			ROOT = {
				narrative_event = { id = LT.40009 }
			}
		}
	}

	on_success_posttitle = {
		emf_cb_on_success_posttitle = yes
	}

	on_reverse_demand = {
		log = "CB(thankmars_rebellion): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		FROM = {
			show_scope_change = no
			reverse_imprison = ROOT
			narrative_event = { id = LT.40011 }
		}
		any_attacker = {
			show_scope_change = no
			imprison = FROM
		}
		hidden_tooltip = {
			any_attacker = {
				limit = {
					lower_tier_than = FROM
				}
				set_defacto_liege = FROM
				emf_liege_change = yes
			}
		}
	}

	on_invalidation = {
		log = "CB(thankmars_rebellion): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		any_attacker = {
			limit = {
				lower_tier_than = FROM
			}
			set_defacto_liege = FROM
			emf_liege_change = yes
		}
		if = {
			limit = {
				ROOT = {
					is_alive = no
				}
				FROM = {
					is_alive = yes
				}
			}
			any_attacker = {
				imprison = ROOT
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

### 01_cb_types.txt: FIXME/TODO: only until `using_cb` bug is resolved / confirmed resolved ###

dejure_duchy_claim = { # New1
	name = CB_NAME_DEJURECLAIM
	war_name = WAR_NAME_DEJURECLAIM
	sprite = 31
	truce_days = 3650
	hostile_against_others = yes
	#is_permanent = yes
	check_de_jure_tier = DUKE
	press_claim = yes
	can_ask_to_join_war = no
	allowed_to_target_tributaries = no
	
	can_use = { always = no }
	is_valid = { always = no }
	attacker_ai_victory_worth = { factor = -1 }
	attacker_ai_defeat_worth = { factor = 100 }
	defender_ai_victory_worth = { factor = -1 }
	defender_ai_defeat_worth = { factor = 100 }
	ai_will_do = { factor = 0 }
}

force_vassalization = { # New2
	name = CB_NAME_FORCE_VASSALIZATION
	war_name = WAR_NAME_FORCE_VASSALIZATION
	sprite = 32
	truce_days = 365
	is_permanent = yes
	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no
	
	defender_unoccupied_warscore = yes
	
	sort_priority = 830
	
	can_use_gui = {
		emf_cb_can_use_gui = yes
		FROM = {
			show_scope_change = no
			conditional_tooltip = {
				trigger = { 
					tier = COUNT
				}
				ROOT = {
					show_scope_change = no
					OR = { 
						prestige = 250
						custom_tooltip = { 
							text = tooltip_antagonize_vassalize_claim 
							hidden_tooltip = { has_opinion_modifier = { who = FROM modifier = opinion_antagonize_vassalize_claim } } 
						}
					}
				}
			}
			conditional_tooltip = {
				trigger = { 
					tier = DUKE
				}
				ROOT = {
					show_scope_change = no
					OR = { 
						prestige = 1000
						custom_tooltip = { 
							text = tooltip_antagonize_vassalize_claim 
							hidden_tooltip = { has_opinion_modifier = { who = FROM modifier = opinion_antagonize_vassalize_claim } } 
						}
					}
				}
			}
			conditional_tooltip = {
				trigger = { 
					tier = KING
				}
				ROOT = {
					show_scope_change = no
					OR = { 
						prestige = 2500
						custom_tooltip = { 
							text = tooltip_antagonize_vassalize_claim 
							hidden_tooltip = { has_opinion_modifier = { who = FROM modifier = opinion_antagonize_vassalize_claim } } 
						}
					}
				}
			}
		}
	}
	
	can_use = {
		emf_cb_can_use = yes
		has_dlc = "Jade Dragon"
		ROOT = {
			NOT = {
				has_game_rule = {
					name = jade_dragon_cbs
					value = off
				}
			}
			higher_real_tier_than = COUNT
			NOR = {
				same_realm = FROM
				same_liege = FROM
				is_liege_or_above_of = FROM
			}
			mercenary = no
			is_nomadic = no
		}
		FROM = {
			is_landed = yes
			lower_real_tier_than = ROOT
			NOT = { realm_size = 31 }
			in_revolt = no
			is_nomadic = no
			controls_religion = no
			holy_order = no
			mercenary = no
			OR = {
				religion = ROOT
				culture_group = ROOT
			}
			shares_realm_border_with = ROOT
		}
	}
	
	is_valid = {
		ROOT = {
			higher_real_tier_than = COUNT
			NOR = {
				same_realm = FROM
				same_liege = FROM
				is_liege_or_above_of = FROM
			}
		}
		FROM = {
			is_landed = yes
			lower_real_tier_than = ROOT
		}
	}
	
	on_add = {
		log = "CB(force_vassalization): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		FROM = {
			if = {
				limit = {
					tier = COUNT
					NOT = { reverse_has_opinion_modifier = { who = ROOT modifier = opinion_antagonize_vassalize_claim } } 
				}
				ROOT = { prestige = -250 }
			}
			else_if = {
				limit = {
					tier = DUKE
					NOT = { reverse_has_opinion_modifier = { who = ROOT modifier = opinion_antagonize_vassalize_claim } } 
				}
				ROOT = { prestige = -1000 }
			}
			else_if = {
				limit = {
					tier = KING
					NOT = { reverse_has_opinion_modifier = { who = ROOT modifier = opinion_antagonize_vassalize_claim } } 
				}
				ROOT = { prestige = -2500 }
			}
			if = {
				limit = {
					reverse_has_opinion_modifier = { who = ROOT modifier = opinion_antagonize_vassalize_claim }
				}
				ROOT = { remove_opinion = { who = ROOT modifier = opinion_antagonize_vassalize_claim } }
			}
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(force_vassalization): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		ROOT = {
			show_scope_change = no
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		FROM = {
			show_scope_change = no
			prestige = -100
			emf_ROOT_subjugate_ruler = yes
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(force_vassalization): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		log = "CB(force_vassalization): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			show_scope_change = no
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_invalidation = {
		log = "CB(force_vassalization): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

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

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = { 
		factor = 1
		modifier = { #Focus on Coronation first.
			factor = 0.1
			ROOT = {
				emf_uses_furry_coronation = yes
				emf_is_crowned = no
			}
		}
	}
}

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

	allowed_to_target_tributaries = no
	
	sort_priority = 820
	
	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			custom_tooltip = {
				text = NEEDS_3000_PRESTIGE_COST
				prestige = 3000
			}
			custom_tooltip = {
				text = NEEDS_1500_PIETY_COST
				piety = 1500
			}
		}
		FROM = {
			relative_power = {
				who = ROOT
				power = 0.95
			}
		}
	}
	
	on_add = {
		log = "CB(great_realm_conflict): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			prestige = -3000
			piety = -1500
		}
		emf_cb_on_add = yes
	}
	
	can_use = {
		emf_cb_can_use = yes
		has_dlc = "Jade Dragon"
		ROOT = {
			NOT = {
				has_game_rule = {
					name = jade_dragon_cbs
					value = off
				}
			}
		}
		FROM = { #IDK why vanilla copied this here from can_use_gui, but sure.
			relative_power = {
				who = ROOT
				power = 0.95
			}
		}
	}
	
	can_use_title = {
		ROOT = {
			is_nomadic = no
			independent = yes
			realm_size = 200
			OR = {
				tier = KING
				tier = EMPEROR
			}
		}
		FROM = {
			realm_size = 200
			in_revolt = no
			is_nomadic = no
			OR = {
				tier = KING
				tier = EMPEROR
			}
		}
		
		OR = { # This ensures that the AI doesn't declare this type of war against someone who only holds a few counties within the targeted title...
			ROOT = {
				ai = no
			}
			FROM = {
				num_of_realm_counties = {
					value = 5
					title = PREV
				}
			}
		}
		
		any_direct_de_jure_vassal_title = {
			any_direct_de_jure_vassal_title = {
				OR = {
					owner_under_ROOT = yes
					location = {
						any_neighbor_province = {
							trigger_if = {
								limit = { is_land = yes }
								owner_under_ROOT = yes
							}
							trigger_else = {
								any_neighbor_province = {
									trigger_if = {
										limit = { is_land = yes }
										owner_under_ROOT = yes
									}
									trigger_else = {
										any_neighbor_province = { owner_under_ROOT = yes }
									}
								}
							}
						}
					}
				}
			}
		}
		emf_cb_can_use_de_jure_title = yes
	}

	is_valid_title = { FROM = { any_realm_province = { de_jure_liege_or_above = PREVPREV } } }
	
	on_invalidation = {
		log = "CB(great_realm_conflict): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			prestige = 3000
			piety = 1500
		}
	}

	on_success = {
		log = "CB(great_realm_conflict): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		emf_imperial_decay_major_victory = yes
	}

	on_success_title = {
		pf_liege_change_under_title_begin_effect = yes
		if = { # Claim the targeted Kingdom if it is held by the target
			limit = {
				controls_religion = no
				owner_under_FROM = yes
			}
			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 }
		}
		pf_liege_change_under_title_end_effect = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(great_realm_conflict): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

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

	on_reverse_demand = {
		log = "CB(great_realm_conflict): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		emf_imperial_decay_major_loss = yes
		ROOT = {
			show_scope_change = no
			prestige = -3000
			piety = -1500
			transfer_scaled_wealth = {
				to = FROM
				value = 10.0
			}
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 3000
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 1500 }
		}
	}

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

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

	ai_will_do = {
		factor = 1
		modifier = { #Focus on Coronation first.
			factor = 0.1
			ROOT = {
				emf_uses_furry_coronation = yes
				emf_is_crowned = no
			}
		}
	}
}

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

	allowed_to_target_tributaries = no
	
	sort_priority = 720

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			show_scope_change = no
			conditional_tooltip = {
				trigger = { 
					tier = COUNT
				}
				piety = 100
				scaled_wealth = 1.0
			}
			conditional_tooltip = {
				trigger = { 
					tier = DUKE
				}
				piety = 250
				scaled_wealth = 1.0
			}
		}
	}
	
	can_use = {
		emf_cb_can_use = yes
		has_dlc = "Jade Dragon"
		ROOT = {
			NOT = {
				has_game_rule = {
					name = jade_dragon_cbs
					value = off
				}
			}
			NOR = { # These all have other ways to expand in the early game
				religion_group = muslim
				religion_group = pagan_group
				is_nomadic = yes
			}
			OR = {
				tier = COUNT
				tier = DUKE
			}
			NOR = {
				is_liege_or_above = FROM
				is_vassal_or_below = FROM
			}
		}
	}
	
	can_use_title = {
		emf_cb_county_conquest_basic_can_use_title = yes
		location = { any_neighbor_province = { owner_under_ROOT = yes } }
		dejure_liege_title = {
			has_holder = no
			any_direct_de_jure_vassal_title = {
				holder = ROOT
			}
		}
		emf_cb_can_use_title = yes
	}

	is_valid_title = {
		owner_under_FROM = yes
	}
	
	on_add = {
		log = "CB(ducal_county_conquest): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			if = {
				limit = {
					tier = COUNT
				}
				piety = -100
				scaled_wealth = -1.0
			}
			if = {
				limit = {
					tier = DUKE
				}
				piety = -250
				scaled_wealth = -1.0
			}
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(ducal_county_conquest): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = 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 = {
				emf_ROOT_subjugate_ruler = yes
			}
		}
		else = {
			pf_liege_change_under_title_begin_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 = {
					owner_under_FROM = yes
					NOT = {
						de_facto_liege = PREV
					}
				}
				usurp_title_plus_barony_if_unlanded = { target = ROOT type = claim }
			}
			pf_liege_change_under_title_end_effect = yes
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		hidden_tooltip = { check_holy_order_donation_effect = yes }
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(ducal_county_conquest): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			show_scope_change = no
			prestige = -100
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		log = "CB(ducal_county_conquest): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			show_scope_change = no
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_invalidation = {
		log = "CB(ducal_county_conquest): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

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

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = { 
		factor = 1
		modifier = {
			factor = 0
			ROOT = {
				NOT = { scaled_wealth = 1.25 }
			}
		}
		modifier = { #Focus on Coronation first.
			factor = 0.1
			ROOT = {
				emf_uses_furry_coronation = yes
				emf_is_crowned = no
			}
		}
	}
}

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

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			NOT = { is_liege_or_above = FROM }
			NOT = { is_vassal_or_below = FROM }
		}
		FROM = {
			any_realm_character = {
				is_ruler = no
				OR = {
					is_married = ROOT
					is_friend = ROOT
					is_lover = ROOT
					is_close_relative = ROOT
					is_nibling_of = ROOT
					is_aunt_uncle_of = ROOT
					is_cousin_of = ROOT
					dynasty = ROOT
					is_consort = ROOT
				}
				OR = {
					prisoner = yes
					AND = {
						has_any_opinion_modifier = opinion_forced_consort
						ROOT_FROM = {
							any_realm_character = {
								reverse_has_opinion_modifier = {
									modifier = opinion_forced_consort
									who = PREVPREV
								}
							}
						}
					}
				}
			}
		}
	}
	
	is_valid = {
		ROOT = {
			NOT = { is_liege_or_above = FROM }
			NOT = { is_vassal_or_below = FROM }
		}
	}
	
	on_add = {
		log = "CB(hostages_cb): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		FROM = { character_event = { id = JD.2000 days = 5 } } # Chance to execute captives
		emf_cb_on_add = yes
	}
	
	on_success = {
		log = "CB(hostages_cb): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		ROOT = {
			show_scope_change = no
			prestige = 250
		}
		FROM = {
			show_scope_change = no
			prestige = -250
		}
		FROM = {
			show_scope_change = no
			transfer_scaled_wealth = {
				to = ROOT
				value = 3.0
			}
			any_realm_character = {
				limit = {
					is_ruler = no
					OR = {
						is_married = ROOT
						is_friend = ROOT
						is_lover = ROOT
						is_close_relative = ROOT
						is_nibling_of = ROOT
						is_aunt_uncle_of = ROOT
						is_cousin_of = ROOT
						dynasty = ROOT
						is_consort = ROOT
					}
					OR = {
						prisoner = yes
						AND = {
							has_any_opinion_modifier = opinion_forced_consort
							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 = ROOT_FROM
							}
						}
						consort = { remove_consort = PREV }
					}
					if = {
						limit = { # Marriages are annuled
							is_married = yes
							any_spouse = { same_realm = ROOT_FROM }
						}
						any_spouse = {
							limit = { same_realm = ROOT_FROM }
							remove_spouse = PREV
						}
					}
				}
				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
							NOT = { consort = { is_ruler = yes } }
						}
						OR = { # ...or the wife to a landed character
							is_married = no
							NOT = { any_spouse = { is_ruler = yes } }
						}
					}
					move_character = ROOT
				}
				opinion = {
					who = ROOT
					modifier = opinion_my_saviour
					years = 100
				}
			}
			custom_tooltip = {
				text = hostages_random_imprisonment_tt
				hidden_effect = {
					random_realm_character = {
						limit = {
							NOT = { character = PREV }
							is_ruler = no
							prisoner = no
							ai = yes
							OR = {
								is_close_relative = PREV
								is_nibling_of = PREV
								is_aunt_uncle_of = PREV
								is_cousin_of = PREV
							}
						}
						preferred_limit = {
							is_close_relative = PREV
						}
						preferred_limit = {
							OR = {
								is_close_relative = PREV
								is_nibling_of = PREV
								is_aunt_uncle_of = PREV
							}
						}
						imprison = ROOT
					}
				}
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(hostages_cb): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			show_scope_change = no
			prestige = -250
		}
		FROM = {
			show_scope_change = no
			prestige = 250
		}
	}

	on_reverse_demand = {
		log = "CB(hostages_cb): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			show_scope_change = no
			transfer_scaled_wealth = {
				to = FROM
				value = 3.0
			}
			prestige = -250
		}
		FROM = {
			show_scope_change = no
			prestige = 250
		}
	}

	on_invalidation = {
		log = "CB(hostages_cb): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

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

unjust_conquest = { # New7
	name = CB_NAME_UNJUST_COUNTY_CONQUEST
	war_name = WAR_NAME_UNJUST_COUNTY_CONQUEST
	sprite = 27
	truce_days = 1825
	hostile_against_others = yes
	is_permanent = yes
	check_all_titles = yes # if permanent, setting this to true will check against all of someones titles, including vassal held titles
	can_ask_to_join_war = no
	allowed_to_target_tributaries = no
	infamy_modifier = 2
	
	sort_priority = 690
	
	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			show_scope_change = no
			conditional_tooltip = {
				trigger = { 
					tier = COUNT
					religion = FROM
				}
				piety = 150
				scaled_wealth = { value = 3.0 min = 50 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = COUNT
					OR = {
						religion_group = FROM
						emf_syncretized_religion_with_FROM = yes
					}
					NOT = { religion = FROM }
				}
				piety = 75
				scaled_wealth = { value = 2.0 min = 50 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = COUNT
					NOR = {
						religion_group = FROM
						emf_syncretized_religion_with_FROM = yes
					}
				}
				piety = 50
				scaled_wealth = { value = 1.0 min = 50 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = DUKE
					religion = FROM
				}
				piety = 300
				scaled_wealth = { value = 3.0 min = 100 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = DUKE
					OR = {
						religion_group = FROM
						emf_syncretized_religion_with_FROM = yes
					}
					NOT = { religion = FROM }
				}
				piety = 150
				scaled_wealth = { value = 2.0 min = 100 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = DUKE
					NOR = {
						religion_group = FROM
						emf_syncretized_religion_with_FROM = yes
					}
				}
				piety = 75
				scaled_wealth = { value = 1.0 min = 100 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = KING
					religion = FROM
				}
				piety = 450
				scaled_wealth = { value = 3.0 min = 200 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = KING
					OR = {
						religion_group = FROM
						emf_syncretized_religion_with_FROM = yes
					}
					NOT = { religion = FROM }
				}
				piety = 300
				scaled_wealth = { value = 2.0 min = 200 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = KING
					NOR = {
						religion_group = FROM
						emf_syncretized_religion_with_FROM = yes
					}
				}
				piety = 150
				scaled_wealth = { value = 1.0 min = 200 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = EMPEROR
					religion = FROM
				}
				piety = 900
				scaled_wealth = { value = 3.0 min = 400 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = EMPEROR
					OR = {
						religion_group = FROM
						emf_syncretized_religion_with_FROM = yes
					}
					NOT = { religion = FROM }
				}
				piety = 600
				scaled_wealth = { value = 2.0 min = 400 }
			}
			conditional_tooltip = {
				trigger = { 
					tier = EMPEROR
					NOR = {
						religion_group = FROM
						emf_syncretized_religion_with_FROM = yes
					}
				}
				piety = 300
				scaled_wealth = { value = 1.0 min = 400 }
			}
			conditional_tooltip = {
				trigger = { is_patrician = yes }
				custom_tooltip = {
					text = emf_mr_ctt_extended_cbs_unlocked
					has_game_rule = { name = mr_cb_restriction value = off }
				}
			}
		}
	}
	
	on_add = {
		log = "CB(unjust_conquest): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			show_scope_change = no
			if = {
				limit = {
					tier = COUNT
					religion = FROM
				}
				piety = -100
				scaled_wealth = { value = -3.0 min = -100 }
			}
			if = {
				limit = {
					tier = COUNT
					OR = {
						religion_group = FROM
						emf_syncretized_religion_with_FROM = yes
					}
					NOT = { religion = FROM }
				}
				piety = -50
				scaled_wealth = { value = -2.0 min = -100 }
			}
			if = {
				limit = {
					tier = COUNT
					NOR = {
						religion_group = FROM
						emf_syncretized_religion_with_FROM = yes
					}
				}
				piety = -25
				scaled_wealth = { value = -1.0 min = -100 }
			}
			
			if = {
				limit = {
					tier = DUKE
					religion = FROM
				}
				piety = -200
				scaled_wealth = { value = -3.0 min = -100 }
			}
			if = {
				limit = {
					tier = DUKE
					OR = {
						religion_group = FROM
						emf_syncretized_religion_with_FROM = yes
					}
					NOT = { religion = FROM }
				}
				piety = -100
				scaled_wealth = { value = -2.0 min = -100 }
			}
			if = {
				limit = {
					tier = DUKE
					NOR = {
						religion_group = FROM
						emf_syncretized_religion_with_FROM = yes
					}
				}
				piety = -50
				scaled_wealth = { value = -1.0 min = -100 }
			}
			
			if = {
				limit = {
					tier = KING
					religion = FROM
				}
				piety = -300
				scaled_wealth = { value = -3.0 min = -100 }
			}
			if = {
				limit = {
					tier = KING
					OR = {
						religion_group = FROM
						emf_syncretized_religion_with_FROM = yes
					}
					NOT = { religion = FROM }
				}
				piety = -200
				scaled_wealth = { value = -2.0 min = -100 }
			}
			if = {
				limit = {
					tier = KING
					NOR = {
						religion_group = FROM
						emf_syncretized_religion_with_FROM = yes
					}
				}
				piety = -100
				scaled_wealth = { value = -1.0 min = -100 }
			}
			
			if = {
				limit = {
					tier = EMPEROR
					religion = FROM
				}
				piety = -600
				scaled_wealth = { value = -3.0 min = -100 }
			}
			if = {
				limit = {
					tier = EMPEROR
					OR = {
						religion_group = FROM
						emf_syncretized_religion_with_FROM = yes
					}
					NOT = { religion = FROM }
				}
				piety = -400
				scaled_wealth = { value = -2.0 min = -100 }
			}
			if = {
				limit = {
					tier = EMPEROR
					NOR = {
						religion_group = FROM
						emf_syncretized_religion_with_FROM = yes
					}
				}
				piety = -200
				scaled_wealth = { value = -1.0 min = -100 }
			}
						
			if = { # Opinion hit for same religion
				limit = {
					religion = FROM
				}
				if = {
					limit = {
						controls_religion = no
					}
					rightful_religious_head_scope = {
						show_scope_change = no
						opinion = {
							who = ROOT
							modifier = opinion_declared_unjust_conquest
							years = 25
						}
					}
				}
				custom_tooltip = {
					text = custom_opinion_hit_tooltip
					hidden_effect = {
						generate_tooltip = no
						any_realm_lord = {
							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_lord = {
								limit = {
									controls_religion = no
									religion_group = ROOT
									is_landed = yes
								}
								opinion = {
									who = ROOT
									modifier = opinion_declared_unjust_conquest
									years = 10
								}
							}
						}
					}
				}
			}
			else_if = { # Opinion hit for same group
				limit = {
					religion_group = FROM
				}
				custom_tooltip = {
					text = custom_opinion_hit_tooltip
					hidden_effect = {
						generate_tooltip = no
						any_realm_lord = {
							limit = {
								religion_group = ROOT
								is_landed = yes
								NOT = {
									character = ROOT
								}
							}
							opinion = {
								who = ROOT
								modifier = opinion_declared_unjust_conquest
								years = 10
							}
						}
						FROM = {
							any_realm_lord = {
								limit = {
									religion_group = ROOT
									is_landed = yes
								}
								opinion = {
									who = ROOT
									modifier = opinion_declared_unjust_conquest
									years = 10
								}
							}
						}
					}
				}
			}
			else = {
				custom_tooltip = {
					text = custom_opinion_hit_tooltip_enemy
					hidden_effect = {
						generate_tooltip = no
						any_realm_lord = {
							limit = {
								religion = FROM
								is_landed = yes
							}
							opinion = {
								who = ROOT
								modifier = opinion_declared_unjust_conquest
								years = 10
							}
						}
						FROM = {
							any_realm_lord = {
								limit = {
									religion = ROOT
									is_landed = yes
								}
								opinion = {
									who = ROOT
									modifier = opinion_declared_unjust_conquest
									years = 10
								}
							}
						}
					}
				}
			}
		}
		emf_cb_on_add = yes
	}
	
	can_use = {
		emf_cb_can_use = yes
		has_dlc = "Jade Dragon"
		ROOT = {
			NOT = {
				has_game_rule = {
					name = unjustified_wars
					value = off
				}
			}
			NOT = { is_liege_or_above = FROM }
			mercenary = no
			pacifist = no
			is_nomadic = no # Use the 'pagan_county_conquest' CB instead
			NOR = {
				religion_group = muslim # Use 'muslim_county_conquest' instead
				AND = {
					religion_group = pagan_group # Use 'pagan_county_conquest' or 'emf_pagan_county_conquest_nonholy' instead
					NOT = { emf_syncretized_religion_with_FROM = yes }
				}
			}
		}
		OR = {
			emf_shattered_world = no
			AND = {
				emf_shattered_world = yes
				OR = {
					AND = {
						OR = {
							has_global_flag = emf_rapid_conquest
							has_alternate_start_setting = { setting = shattered_cbs option = on_long }
						}
						total_years_played >= 100
					}
					AND = {
						has_alternate_start_setting = { setting = shattered_cbs option = on_limited }
						total_years_played >= 25
					}
				}
			}
			AND = {
				is_shattered_world = yes
				has_alternate_start_setting = { setting = shattered_cbs option = off }
			}
		}
	}

	can_use_title = {
		emf_cb_county_conquest_basic_can_use_title = yes
		location = { any_neighbor_province = { owner_under_ROOT = yes } }
		emf_cb_can_use_title = yes
	}

	is_valid_title = {
		owner_under_FROM = yes
	}

	on_success = {
		log = "CB(unjust_conquest): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
	}

	on_success_title = {
		if = {
			limit = {
				holder_scope = {
					tier = COUNT
					NOT = { num_of_count_titles = 2 }
					lower_tier_than = ROOT
				}
			}
			holder_scope = {
				emf_ROOT_subjugate_ruler = yes
			}
		}
		else = {
			pf_liege_change_under_title_begin_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 = {
					owner_under_FROM = yes
					NOT = {
						de_facto_liege = PREV
					}
				}
				usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
			}
			pf_liege_change_under_title_end_effect = yes
		}
		
		add_pressed_claim = FROM
		
		ROOT = {
			show_scope_change = no
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		hidden_tooltip = { check_holy_order_donation_effect = yes }
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(unjust_conquest): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			show_scope_change = no
			prestige = -100
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		log = "CB(unjust_conquest): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			show_scope_change = no
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = {
			show_scope_change = no
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_invalidation = {
		log = "CB(unjust_conquest): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}
	
	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = { 
		factor = 1
		
		modifier = {
			factor = 0.7 # Prefer using CB's without on_add costs
		}
		modifier = { #Focus on Coronation first.
			factor = 0.1
			ROOT = {
				emf_uses_furry_coronation = yes
				emf_is_crowned = no
			}
		}
		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_flag = is_temujin
				has_landed_title = e_mongol_empire
				demesne_size = 1
			}
			FROM = {
				is_offmap_governor = offmap_china
			}
		}
	}
}

tributary_permanent_cb = { # New8
	name = CB_NAME_PERMANENT_TRIBUTARY
	war_name = WAR_NAME_PERMANENT_TRIBUTARY
	sprite = 37
	truce_days = 365
	is_permanent = yes
	hostile_against_others = yes
	can_ask_to_join_war = no
	display_on_map = no
	allowed_to_target_tributaries = no
	
	defender_unoccupied_warscore = yes
	
	sort_priority = 810
	
	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			relative_realm_size = {
				who = FROM
				size > 1
			}
			prestige = 200
			TECH_MAJESTY = 2
		}
	}
	
	can_use = {
		emf_cb_can_use = yes
		emf_cb_tributary_basic_can_use = yes
		NOT = { has_game_rule = { name = jade_dragon_cbs value = off } }
		ROOT = { is_nomadic = no }
		FROM = { is_nomadic = no }
	}

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

	on_add = {
		log = "CB(tributary_permanent_cb): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		# Chance to surrender early
		ROOT = { save_event_target_as = tribute_war_suzerain }
		FROM = {
			show_scope_change = no
			save_event_target_as = tribute_war_target
			set_flag = surrender_tributary_permanent_cb
			character_event = { id = JD.2500 days = 5 tooltip = surrender_tributary_tt }
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(tributary_permanent_cb): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		FROM = {
			show_scope_change = no
			save_event_target_as = target_loser
			prestige = -100
			custom_tooltip = {
				text = "REMOVE_PREVIOUS_SUZERAIN"
				hidden_tooltip = {
					any_suzerain = {
						event_target:target_loser = {
							remove_tributary = PREV
						}
					}
				}
			}
			opinion = {
				modifier = opinion_forced_tributary 
				who = ROOT
				years = 15
			}
		}

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

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(tributary_permanent_cb): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			show_scope_change = no
			prestige = -100
		}
		FROM = {
			show_scope_change = no
			prestige = 100
		}
	}

	on_reverse_demand = {
		log = "CB(tributary_permanent_cb): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			show_scope_change = no
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}

		FROM = {
			show_scope_change = no
			prestige = 200
		}
	}

	on_invalidation = {
		log = "CB(tributary_permanent_cb): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}
	
	on_attacker_leader_death = {
		end_war = invalid
	}

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

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

	ai_will_do = {
		factor = 1
		
		modifier = { #Focus on Coronation first.
			factor = 0.1
			ROOT = {
				emf_uses_furry_coronation = yes
				emf_is_crowned = no
			}
		}
		modifier = {
			factor = 0.5  # Low prio CB
		}
		modifier = {
			factor = 0.1 # Target has almost no money
			FROM = {
				OR = {
					is_nomadic = yes
					is_tribal = yes
				}
			}
		}
		modifier = {
			factor = 0
			ROOT = {
				is_feudal = yes
				NOT = { religion_group = muslim }
			}
			FROM = {
				is_feudal = yes
				NOT = { religion_group = muslim }
			}
		}
		modifier = {
			factor = 0
			emf_is_hre_being_restored = yes
			ROOT = {
				emf_can_restore_hre_someday = yes
			}
			FROM = {
				emf_has_karling_title = yes
			}
		}
	}
}

tributary_nomad_cb = { # New9
	name = CB_NAME_NOMAD_TRIBUTARY
	war_name = WAR_NAME_NOMAD_TRIBUTARY
	sprite = 37
	truce_days = 365
	is_permanent = yes
	hostile_against_others = yes
	can_ask_to_join_war = no
	display_on_map = no
	allowed_to_target_tributaries = no
	
	defender_unoccupied_warscore = yes
	
	sort_priority = 810
	
	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			custom_tooltip = {
				text = NEEDS_100_PRESTIGE_COST
				hidden_tooltip = { prestige = 100 }
			}
		}
	}
	
	can_use = {
		emf_cb_can_use = yes
		ROOT = { is_nomadic = yes }
		emf_cb_tributary_basic_can_use = yes
	}

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

	on_add = {
		log = "CB(tributary_nomad_cb): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			prestige = -100
		}
		# Chance to surrender early
		ROOT = { save_event_target_as = tribute_war_suzerain }
		FROM = {
			show_scope_change = no
			save_event_target_as = tribute_war_target
			set_flag = surrender_tributary_nomad_cb
			character_event = { id = JD.2500 days = 5 tooltip = surrender_tributary_tt }
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(tributary_nomad_cb): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		FROM = {
			show_scope_change = no
			transfer_scaled_wealth = {
				to = ROOT
				value = 2.0
			}
		}
	
		FROM = {
			show_scope_change = no
			save_event_target_as = target_loser
			prestige = -100
			custom_tooltip = {
				text = "REMOVE_PREVIOUS_SUZERAIN"
				hidden_tooltip = {
					any_suzerain = {
						event_target:target_loser = {
							remove_tributary = PREV
						}
					}
				}
			}
		}

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

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(tributary_nomad_cb): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			show_scope_change = no
			prestige = -100
		}
		FROM = {
			show_scope_change = no
			prestige = 100
		}
	}

	on_reverse_demand = {
		log = "CB(tributary_nomad_cb): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			show_scope_change = no
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}

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

	on_invalidation = {
		log = "CB(tributary_nomad_cb): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

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

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
		
		modifier = {
			factor = 2
			FROM = {
				trait = proud
			}
		}
		modifier = {
			factor = 2
			FROM = {
				trait = brave
			}
		}
		modifier = {
			factor = 2
			FROM = {
				trait = greedy
			}
		}
		modifier = {
			factor = 3
			FROM = {
				tier = KING
			}
		}
		modifier = {
			factor = 0.85
			ROOT = {
				relative_power = { who = FROM power = 1.2 }
			}
		}
		modifier = {
			factor = 0.85
			ROOT = {
				relative_power = { who = FROM power = 1.5 }
			}
		}
		modifier = {
			factor = 0.8
			ROOT = {
				relative_power = { who = FROM power = 2.0 }
			}
		}
		modifier = {
			factor = 0.8
			ROOT = {
				relative_power = { who = FROM power = 2.5 }
			}
		}
		modifier = {
			factor = 0.8
			ROOT = {
				relative_power = { who = FROM power = 3 }
			}
		}
		modifier = {
			factor = 0.75
			ROOT = {
				relative_power = { who = FROM power = 4 }
			}
		}
		modifier = {
			factor = 0.75
			ROOT = {
				relative_power = { who = FROM power = 5 }
			}
		}
		modifier = {
			factor = 0.75
			ROOT = {
				relative_power = { who = FROM power = 6 }
			}
		}
		modifier = {
			factor = 0.5
			ROOT = {
				relative_power = { who = FROM power = 8 }
			}
		}
		modifier = {
			factor = 0.25
			ROOT = {
				relative_power = { who = FROM power = 10 }
			}
		}
		modifier = {
			factor = 2
			ROOT = {
				distance_from_realm = { who = FROM value = 20 }
			}
		}
		modifier = {
			factor = 2
			ROOT = {
				distance_from_realm = { who = FROM value = 40 }
			}
		}
		modifier = {
			factor = 3
			ROOT = {
				distance_from_realm = { who = FROM value = 80 }
			}
		}
		modifier = {
			factor = 4
			ROOT = {
				distance_from_realm = { who = FROM value = 120 }
			}
		}
	}

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

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 = {
		emf_cb_can_use = yes
		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_add = {
		log = "CB(offmap_submission): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(offmap_submission): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		FROM = {
			piety = -100
			prestige = -100
			
			if = { # Remove Raid China if active
				limit = {
					has_character_modifier = china_raid_active
				}
				remove_character_modifier = china_raid_active
				hidden_effect = {
					add_character_modifier = {
						name = china_raid_grace_cd
						hidden = yes
						years = 5
					}
				}
			}
			
			custom_tooltip = {
				text = offmap_submission_tip
			}
				
			hidden_tooltip = {
				ROOT = {
					if = {
						limit = {
							num_of_count_titles_in_realm = 0
							ROOT_FROM = {
								num_of_count_titles_in_realm < 2
							}
						}
						ROOT_FROM = {
							any_realm_province = {
								county = {
									usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
								}
							}
						}
					}
					while = {
						count = 20 # We had an infinite loop at one point. It's been fixed, but safety first
						limit = {
							demesne_size_compared_to_limit < 1
							ROOT_FROM = {
								num_of_count_titles_in_realm > 1
							}
						}
						ROOT_FROM = {
							if = { # Take counties that border each other
								limit = {
									any_realm_province = {
										ROOT_FROM = {
											NOT = {
												capital_scope = {
													province = PREVPREV
												}
											}
										}
										any_neighbor_province = {
											owner = {
												character = ROOT
											}
										}
									}
								}
								random_realm_province = {
									limit = {
										ROOT_FROM = {
											NOT = {
												capital_scope = {
													province = PREVPREV
												}
											}
										}
										any_neighbor_province = {
											owner = {
												character = ROOT
											}
										}
									}
									county = {
										usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
									}
								}
							}
							else = {
								random_realm_province = {
									limit = {
										ROOT_FROM = {
											NOT = {
												capital_scope = {
													province = 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 = offmap_china
							offmap_china = { NOT = { has_offmap_tmp_flag = WP_is_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_flag = is_rebel_general }
						set_global_flag = rebel_general_invasion_success
						primary_title = { 
							set_title_landless = { title = THIS status = no }
							set_flag = uses_temple_names_by_script #Titles with this flag give their heirs a temple name via on_death event
							set_flag = pretender_chinese_empire #Blocks all grace interactions
							set_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_flag = china_name_Tang
									set_coa = e_china_tang
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = jin_china } } }
									set_name = "Jin Empire"
									set_flag = china_name_Jin
									set_coa = e_china_jin
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = wei_china } } }
									set_name = "Wei Empire"
									set_flag = china_name_Wei
									set_coa = e_china_wei
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = qi_china } } }
									set_name = "Qi Empire"
									set_flag = china_name_Qi
									set_coa = e_china_qi
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = zhou_china } } }
									set_name = "Zhou Empire"
									set_flag = china_name_Zhou
									set_coa = e_china_zhou
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = han_china } } }
									set_name = "Han Empire"
									set_flag = china_name_Han
									set_coa = e_china_han
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = qin_china } } }
									set_name = "Qin Empire"
									set_flag = china_name_Qin
									set_coa = e_china_qin
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = yan_china } } }
									set_name = "Yan Empire"
									set_flag = china_name_Yan
									set_coa = e_china_yan
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = zhao_china } } }
									set_name = "Zhao Empire"
									set_flag = china_name_Zhao
									set_coa = e_china_zhao
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = liao_china } } }
									set_name = "Liao Empire"
									set_flag = china_name_Liao
									set_coa = e_china_liao
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = yuan_china } } }
									set_name = "Yuan Empire"
									set_flag = china_name_Yuan
									set_coa = e_china_yuan
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = xia_china } } }
									set_name = "Xia Empire"
									set_flag = china_name_Xia
									set_coa = e_china_xia
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = qing_china } } }
									set_name = "Qing Empire"
									set_flag = china_name_Qing
									set_coa = e_china_qing
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = dai_china } } }
									set_name = "Dai Empire"
									set_flag = china_name_Dai
									set_coa = e_china_dai
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = cheng_china } } }
									set_name = "Cheng Empire"
									set_flag = china_name_Cheng
									set_coa = e_china_cheng
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = liang_china } } }
									set_name = "Liang Empire"
									set_flag = china_name_Liang
									set_coa = e_china_liang
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = song_china } } }
									set_name = "Song Empire"
									set_flag = china_name_Song
									set_coa = e_china_song
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = ming_china } } }
									set_name = "Ming Empire"
									set_flag = china_name_Ming
									set_coa = e_china_ming
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = shu_china } } }
									set_name = "Shu Empire"
									set_flag = china_name_Shu
									set_coa = e_china_shu
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = wu_china } } }
									set_name = "Wu Empire"
									set_flag = china_name_Wu
									set_coa = e_china_wu
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = chu_china } } }
									set_name = "Chu Empire"
									set_flag = china_name_Chu
									set_coa = e_china_chu
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = yue_china } } }
									set_name = "Yue Empire"
									set_flag = china_name_Yue
									set_coa = e_china_yue
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = yin_china } } }
									set_name = "Yin Empire"
									set_flag = china_name_Yin
									set_coa = e_china_yin
								}
								1 = { 
									trigger = { NOT = { offmap_china = { has_offmap_name = shun_china } } }
									set_name = "Shun Empire"
									set_flag = china_name_Shun
									set_coa = e_china_shun
								}
							}
							owner = { set_character_flag = emf_no_law_penalties }
							add_law = {
								law = succ_primogeniture
								cooldown = no
								opinion_effect = no
							}
							add_law = {
								law = administration_2
								cooldown = no
								opinion_effect = no
							}
							owner = { clr_character_flag = emf_no_law_penalties }
						}
						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_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 }
						}
					}
				}
				# inside FROM scope
				if = {
					limit = {
						NOT = {
							any_demesne_title = {
								tier = EMPEROR
							}
						}
					}
					emf_ROOT_subjugate_ruler = yes
					if = {
						limit = {
							ROOT = { has_flag = is_rebel_general }
						}
						any_demesne_title = {
							limit = {
								tier = KING
							}
							usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
							owner = { set_flag = emf_no_law_penalties }
							add_law = {
								law = succ_primogeniture
								cooldown = no
								opinion_effect = no
							}
							owner = { clr_flag = emf_no_law_penalties }
						}
					}
					break = yes
				}
				# FROM has an empire title
				any_vassal = {
					limit = {
						higher_tier_than = BARON
						OR = {
							is_patrician = no
							is_merchant_republic = yes
						}
						in_revolt = no
					}
					emf_ROOT_subjugate_ruler = yes
				}
				any_demesne_title = {
					limit = {
						tier = EMPEROR
						NOT = { controls_religion = yes }
					}
					emf_destroy_title = yes
				}
				if = {
					limit = {
						NOT = {
							tier = EMPEROR
						}
					}
					emf_ROOT_subjugate_ruler = yes
					break = yes
				}
				any_demesne_title = {
					limit = {
						NOT = { controls_religion = yes }
					}
					grant_title_no_opinion = { target = ROOT type = invasion }
				}
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }
	
	on_fail = {
		log = "CB(offmap_submission): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		FROM = {
			piety = 100
			participation_scaled_prestige = 250
			set_flag = defeated_china
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 250 }
		}
		ROOT = {
			piety = -100
		}
		emf_cb_offmap_submission_failure_cleanup = yes
	}

	on_reverse_demand = {
		log = "CB(offmap_submission): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			prestige = -500
			transfer_scaled_wealth = {
				to = FROM
				value = 5.0
				min = 1000
			}
			emf_cb_offmap_submission_failure_cleanup = yes
			hidden_effect = {
				if = { #Makes China have a chance of leaving Expanionist policy...
					limit = {
						NOT = {
							has_flag = is_rebel_general
						}
						is_offmap_tag = offmap_china
						offmap_china = {
							has_policy = china_expansionist
						}
					}
					random = {
						chance = 50
						offmap_china = {
							set_policy = china_open
						}
						log = "China War Logging: China was forced out of Expansionism"
						if = {
							limit = {
								offmap_china = {
									has_status = china_stable
								}
							}
							random = {
								chance = 30
								offmap_china = {
									set_status = china_unrest
								}
								log = "China War Logging: China was forced into Unrest"
							}
						}
					}
				}
				log = "China War Logging: China lost a submission war against [FROM.GetBestName]"
			}
		}
		FROM = {
			set_flag = defeated_china
			piety = 500
			if = {
				limit = { 
					has_dlc = "Legacy of Rome"
					emf_has_roman_empire = yes
					OR = { 
						religion_group = christian
						religion = hellenic_pagan
						religion = hellenic_pagan_reformed
					}
				}
				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 = {
		log = "CB(offmap_submission): ATTACKER_DEATH: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"

		if = {
			limit = { ROOT = { has_flag = is_rebel_general } }
			end_war = invalid
			hidden_effect = {
				ROOT = {
					any_courtier = { death = { death_reason = death_went_back_to_china } }
					primary_title = {
						activate_title = { title = THIS status = no }
						emf_destroy_title = yes
					}
					death = { death_reason = death_went_back_to_china }
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(offmap_submission): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		# FIXME: should we do an emf_cb_offmap_submission_failure_cleanup = yes here too? need to know context of how this
		# CB gets setup and torn down for is_rebel_general.
	}

	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 = {
		emf_cb_can_use = yes
		# NOTE: We do not use emf_cb_tributary_basic_can_use in this CB,
		# because its use is purely scripted (i.e., it's not a permanent CB).
		ROOT = {
			independent = yes
			NOT = { same_realm = FROM }
			mercenary = no
		}
		FROM = {
			independent = yes
			holy_order = no
			num_of_count_titles_in_realm = 1
			in_revolt = no
		}
		NOT = {
			ROOT = {
				rightful_religious_head_scope = {
					character = FROM 
				}
			}
		}
	}

	is_valid = {
		ROOT = {
			independent = yes
			NOT = { same_realm = FROM }
			mercenary = no
		}
		FROM = {
			independent = yes
			holy_order = no
			num_of_count_titles_in_realm = 1
			in_revolt = no
		}
		NOT = {
			ROOT = {
				rightful_religious_head_scope = {
					character = FROM 
				}
			}
		}
	}

	on_add = {
		log = "CB(tributary_offmap_cb): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(tributary_offmap_cb): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		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 = "---------------------"
		emf_cb_victory = yes
		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_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(tributary_offmap_cb): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		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_flag = defeated_china
		}
	}

	on_reverse_demand = {
		log = "CB(tributary_offmap_cb): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		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 = "---------------------"
		emf_cb_defeat = yes
		ROOT = {
			prestige = -500
			transfer_scaled_wealth = {
				to = FROM
				value = 5.0
				min = 1000
			}
			hidden_effect = {
				if = { #Makes China have a chance of leaving Expansionist policy...
					limit = {
						is_offmap_tag = offmap_china
						offmap_china = {
							has_policy = china_expansionist
						}
					}
					random = {
						chance = 50
						offmap_china = {
							set_policy = china_open
						}
					}
				}
				log = "China War Logging: China lost a I'll-make-you-my-tributary war against [From.GetBestName]"
			}
		}

		FROM = {
			piety = 500
			participation_scaled_prestige = 2500
			set_flag = defeated_china
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 1000 }
		}
	}
	
	on_attacker_leader_death = {
		log = "CB(tributary_offmap_cb): ATTACKER_DEATH: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		#end_war = invalid
	}

	on_invalidation = {
		log = "CB(tributary_offmap_cb): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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 = {
		emf_cb_can_use = yes
		ROOT = {
			independent = yes
			NOT = { same_realm = FROM }
			mercenary = no
		}
		FROM = {
			primary_title = { has_flag = rebel_general_title }
		}
	}

	on_add = {
		log = "CB(dismantle_pretender_china_cb): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(dismantle_pretender_china_cb): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		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 = "---------------------"
		emf_cb_victory = yes
		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
								}
								vassal_of = ROOT_FROM
								liege_before_war = { 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 }
						emf_destroy_title = yes
					}
					death = { death_reason = death_executed_by_chinese_emperor }
				}
			}
		}
		ROOT = {
			prestige = 200
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(dismantle_pretender_china_cb): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		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 = "CB(dismantle_pretender_china_cb): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		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 = "---------------------"
		emf_cb_defeat = yes
		ROOT = {
			piety = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 5.0
				min = 500
			}
			offmap_power = { set_offmap_tmp_flag = pretender_chinese_empire_war_cooldown }
		}
		FROM = {
			piety = 200
			participation_scaled_prestige = 1000
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 1000 }
		}
	}
	
	on_attacker_leader_death = {
		log = "CB(dismantle_pretender_china_cb): ATTACKER_DEATH: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		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
	}

	on_invalidation = {
		log = "CB(dismantle_pretender_china_cb): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

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

	is_valid = {
		offmap_china = { has_policy = china_isolationist }
	}

	on_add = {
		log = "CB(force_open_china_cb): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(force_open_china_cb): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = 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_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(force_open_china_cb): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		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 = {
		log = "CB(force_open_china_cb): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			show_scope_change = no
			prestige = -300
			transfer_scaled_wealth = {
				to = FROM
				value = 5.0
				min = 500
			}
			custom_tooltip = {
				text = angered_china_tt #Scripter! Update this is you change the value below.
				add_character_modifier = {
					modifier = angered_china_modifier
					inherit = yes
					years = 50
				}
			}
		}
		FROM = {
			piety = 200
			participation_scaled_prestige = 100
			hidden_effect = {
				offmap_power = {
					set_offmap_flag = no_war_news
					any_player = {
						limit = {
							has_offmap_news_enabled = PREV
							is_within_diplo_range = PREV
						}
						narrative_event = { id = JD.60303 }
					}
					clr_offmap_tmp_flag = china_force_open_in_progress
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}
	
	on_attacker_leader_death = {
		log = "CB(force_open_china_cb): ATTACKER_DEATH: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		#end_war = invalid
	}

	on_invalidation = {
		log = "CB(force_open_china_cb): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

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

	on_add = {
		log = "CB(destabilization_china_cb): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(destabilization_china_cb): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = 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
								}
								vassal_of = ROOT_FROM
								liege_before_war = { character = ROOT_FROM }
							}
						}
						narrative_event = { id = JD.60209 }
					}
					any_demesne_title = {
						limit = {
							higher_tier_than = DUKE
							NOR = {
								controls_religion = yes
								clan = yes
							}
						}
						emf_unsafe_destroy_title = yes
					}
					#death = { death_reason = death_executed_by_chinese_emperor }
				}
			}
		}
		ROOT = {
			prestige = 200
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(destabilization_china_cb): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		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 = "CB(destabilization_china_cb): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			piety = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 5.0
				min = 1000
			}
		}
		FROM = {
			piety = 500
			participation_scaled_prestige = 2500
			set_flag = defeated_china
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 1000 }
		}
	}
	
	on_attacker_leader_death = {
		log = "CB(destabilization_china_cb): ATTACKER_DEATH: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		#end_war = invalid
	}

	on_invalidation = {
		log = "CB(destabilization_china_cb): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

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

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

	ai_will_do = {
		factor = 0
	}
}

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

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

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

	on_add = {
		log = "CB(invasion_of_china_cb): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(invasion_of_china_cb): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		emf_imperial_decay_major_victory = yes
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = invasion_of_china_success_tt
				hidden_effect = {
					remove_character_modifier = china_raid_active
					event_target:invasion_of_china_claimant = {
						betrothed = { break_betrothal = PREV }
						any_spouse = {
							if = {
								limit = {
									is_ruler = no
									is_primary_heir = no
								}
								leave_court_for_china_effect = yes
							}
						}
						any_consort = {
							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
							}
						}
						emf_willfully_abdicate = yes
						any_claim = {
							remove_claim = event_target:invasion_of_china_claimant
						}
						clr_flag = no_court_invites
						remove_trait = cannot_marry
						diplomatic_immunity = no
					}
					offmap_china = {
						governor = {
							any_vassal = {
								emf_ROOT_subjugate_ruler = yes
							}
							any_demesne_title = {
								limit = {
									NOT = {
										tier = EMPEROR
									}
								}
								grant_title_no_opinion = ROOT
							}
						}
						ROOT = {
							if = {
								limit = { government = chinese_imperial_government }
								offmap_china = { set_offmap_flag = rename_override }
								primary_title = {
									trigger_switch = {
										on_trigger = has_flag
										china_name_Tang =  { offmap_china = { set_offmap_name = "tang_china"  governor_title = { set_coa = e_china_tang } } }
										china_name_Jin =   { offmap_china = { set_offmap_name = "jin_china"   governor_title = { set_coa = e_china_jin } } }
										china_name_Wei =   { offmap_china = { set_offmap_name = "wei_china"   governor_title = { set_coa = e_china_wei } } }
										china_name_Qi =	   { offmap_china = { set_offmap_name = "qi_china"	  governor_title = { set_coa = e_china_qi } } }
										china_name_Zhou =  { offmap_china = { set_offmap_name = "zhou_china"  governor_title = { set_coa = e_china_zhou } } }
										china_name_Han =   { offmap_china = { set_offmap_name = "han_china"   governor_title = { set_coa = e_china_han } } }
										china_name_Qin =   { offmap_china = { set_offmap_name = "qin_china"   governor_title = { set_coa = e_china_qin } } }
										china_name_Yan =   { offmap_china = { set_offmap_name = "yan_china"   governor_title = { set_coa = e_china_yan } } }
										china_name_Zhao =  { offmap_china = { set_offmap_name = "zhao_china"  governor_title = { set_coa = e_china_zhao } } }
										china_name_Liao =  { offmap_china = { set_offmap_name = "liao_china"  governor_title = { set_coa = e_china_liao } } }
										china_name_Yuan =  { offmap_china = { set_offmap_name = "yuan_china"  governor_title = { set_coa = e_china_yuan } } }
										china_name_Xia =   { offmap_china = { set_offmap_name = "xia_china"   governor_title = { set_coa = e_china_xia } } }
										china_name_Qing =  { offmap_china = { set_offmap_name = "qing_china"  governor_title = { set_coa = e_china_qing } } }
										china_name_Dai =   { offmap_china = { set_offmap_name = "dai_china"   governor_title = { set_coa = e_china_dai } } }
										china_name_Cheng = { offmap_china = { set_offmap_name = "cheng_china" governor_title = { set_coa = e_china_cheng } } }
										china_name_Liang = { offmap_china = { set_offmap_name = "liang_china" governor_title = { set_coa = e_china_liang } } }
										china_name_Song =  { offmap_china = { set_offmap_name = "song_china"  governor_title = { set_coa = e_china_song } } }
										china_name_Ming =  { offmap_china = { set_offmap_name = "ming_china"  governor_title = { set_coa = e_china_ming } } }
										china_name_Shu =   { offmap_china = { set_offmap_name = "shu_china"   governor_title = { set_coa = e_china_shu } } }
										china_name_Wu =	   { offmap_china = { set_offmap_name = "wu_china"	  governor_title = { set_coa = e_china_wu } } }
										china_name_Chu =   { offmap_china = { set_offmap_name = "chu_china"   governor_title = { set_coa = e_china_chu } } }
										china_name_Yue =   { offmap_china = { set_offmap_name = "yue_china"   governor_title = { set_coa = e_china_yue } } }
										china_name_Yin =   { offmap_china = { set_offmap_name = "yin_china"   governor_title = { set_coa = e_china_yin } } }
										china_name_Shun =  { offmap_china = { set_offmap_name = "shun_china"  governor_title = { set_coa = e_china_shun } } }
									}
									clr_flag = pretender_chinese_empire
									clr_flag = uses_temple_names_by_script
									clear_flags_with_prefix = temple_name_
									clear_flags_with_prefix = china_name_
									set_name = "[Root.Capital.GetName] Empire"
									reset_coa = THIS
								}
								clr_flag = given_temple_name_by_script
								set_name = ""
								if = {
									limit = {
										primary_title = {
											tier = EMPEROR
											has_law = administration_2
										}
									}
									emf_switch_to_imperial = yes
								}
								else = {
									emf_switch_to_feudal_gov = yes
								}
							}
						}
						set_offmap_flag = no_succession_news
						set_offmap_flag = no_war_news
						clr_offmap_flag = china_invaded_player_dynasty
						set_offmap_flag = china_invaded_player_dynasty
						set_offmap_holder = event_target:invasion_of_china_claimant
						clr_offmap_flag = no_succession_news
						clr_offmap_flag = rename_override
						
						if = {
							limit = {
								NOR = {
									has_status = china_stable
									has_status = china_golden_age
								}
							}
							set_offmap_flag = no_status_news
							set_status = china_stable
							clr_offmap_flag = no_status_news
						}
					}
					any_player = {
						limit = {
							OR = {
								AND = {
									has_offmap_news_enabled = offmap_china
									is_within_diplo_range = FROM
								}
								vassal_of = ROOT
								liege_before_war = { character = ROOT }
							}
						}
						narrative_event = { id = JD.60401 }
					}
				}
			}
			participation_scaled_prestige = 5000
			set_offmap_currency = { offmap = offmap_china value = 5000 }
			wealth = 10000
			add_character_modifier = { name = chinese_favored_in_trade inherit = yes years = 50 }
			if = {
				limit = { NOT = { has_artifact = jade_dragon } }
				add_artifact = jade_dragon
			}
			if = {
				limit = { NOT = { has_artifact = chinese_dragon_amulet } }
				add_artifact = chinese_dragon_amulet
			}
			if = {
				limit = { NOT = { has_artifact = water_clock } }
				add_artifact = water_clock
			}
			if = {
				limit = { NOT = { has_artifact = jian_sword } }
				add_artifact = jian_sword
			}
			hidden_effect = { add_character_modifier = { name = peace_deal_with_china inherit = yes years = 50 } }
			remove_character_modifier = china_raid_active
			remove_character_modifier = war_with_china_modifier
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 2000 }
		}
		clear_global_event_target = invasion_of_china_claimant
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_invalidation = {
		log = "CB(invasion_of_china_cb): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		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
							}
							vassal_of = ROOT
							liege_before_war = { character = ROOT }
						}
					}
					narrative_event = { id = JD.60403 }
				}
			}
			custom_tooltip = {
				text = angered_china_tt #Scripter! Update this is you change the value below.
				add_character_modifier = {
					modifier = angered_china_modifier
					inherit = yes
					years = 50
				}
			}
		}
		clear_global_event_target = invasion_of_china_claimant
	}

	on_reverse_demand = {
		log = "CB(invasion_of_china_cb): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		emf_imperial_decay_major_loss = yes
		ROOT = {
			prestige = -3000
			transfer_scaled_wealth = {
				to = FROM
				value = 5.0
				min = 5000
			}
			remove_character_modifier = war_with_china_modifier
			custom_tooltip = {
				text = invasion_of_china_cb_fail_tt
				hidden_effect = { add_character_modifier = { name = failed_invasion_of_china duration = -1 hidden = yes } }
			}
			custom_tooltip = {
				text = destabilization_china_success_tt
				hidden_effect = { 
					offmap_china = { set_offmap_flag = no_war_news }
					any_player = {
						limit = {
							OR = {
								AND = {
									has_offmap_news_enabled = offmap_china
									is_within_diplo_range = FROM
								}
								vassal_of = ROOT
								liege_before_war = { character = ROOT }
							}
						}
						narrative_event = { id = JD.60402 }
					}
					any_demesne_title = {
						limit = {
							higher_tier_than = DUKE
							NOR = {
								controls_religion = yes
								clan = yes
							}
						}
						emf_destroy_title = yes
					}
				}
			}
			custom_tooltip = {
				text = angered_china_tt #Scripter! Update this is you change the value below.
				add_character_modifier = {
					modifier = angered_china_modifier
					inherit = yes
					years = 50
				}
			}
		}
		FROM = {
			piety = 200
			participation_scaled_prestige = 1000
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 1000 }
		}
		clear_global_event_target = invasion_of_china_claimant
	}
	
	on_attacker_leader_death = {
		log = "CB(invasion_of_china_cb): ATTACKER_DEATH: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		#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
	}
}

### CP-3.0 NOTE: NEW CBs IN HF's 01_cb_types ###

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

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

	on_add = {
		log = "CB(truebelievers_bid_for_independence): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(truebelievers_bid_for_independence): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			show_scope_change = no
			religion_authority = {
				modifier = won_holy_war
				years = 20
			}
			prestige = 250
			piety = 100
			participation_scaled_prestige = 250
			participation_scaled_piety = 100
		}
		FROM = {
			show_scope_change = no
			religion_authority = {
				modifier = lost_holy_war
				years = 20
			}
			prestige = -250
			piety = -100
			pf_lower_CA_effect = yes
			pf_lower_TO_effect = yes
		}
		ROOT = {
			if = {
				limit = { FROM = { independent = no } }
				FROM = {
					liege = {
						set_defacto_vassal = PREVPREV
					}
				}
				set_flag = ai_flag_never_transfer
			}
			else = {
				set_defacto_liege = THIS
			}
		}
		hidden_effect = {
			any_attacker = {
				show_scope_change = no
				limit = {
					lower_real_tier_than = ROOT
					OR = {
						is_patrician = no
						is_merchant_republic = yes
					}
				}
				set_defacto_liege = ROOT
			}
			any_attacker = {
				show_scope_change = no
				limit = {
					NOT = { character = ROOT }
					NAND = {
						lower_real_tier_than = ROOT
						OR = {
							is_patrician = no
							is_merchant_republic = yes
						}
					}
				}
				if = {
					limit = { FROM = { independent = no } }
					FROM = {
						liege = {
							set_defacto_vassal = PREVPREV
						}
					}
					set_flag = ai_flag_never_transfer
				}
				else = {
					set_defacto_liege = THIS
				}
			}
			any_attacker = {
				emf_liege_change = yes
				clr_flag = flag_truebelievers_revolter
				reverse_opinion = { who = FROM modifier = opinion_rightful_subjugation }
			}
		}
	}

	on_fail = {
		log = "CB(truebelievers_bid_for_independence): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			prestige = 100
			set_defacto_liege = FROM
			emf_liege_change = yes
		}
		hidden_effect = {
			any_attacker = {
				clr_flag = flag_truebelievers_revolter
			}
		}
	}

	on_reverse_demand = {
		log = "CB(truebelievers_bid_for_independence): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		hidden_effect = {
			any_attacker = {
				clr_flag = flag_truebelievers_revolter
			}
		}
		ROOT = {
			show_scope_change = no
			religion_authority = {
				modifier = lost_holy_war
				years = 20
			}
		}
		FROM = {
			show_scope_change = no
			religion_authority = {
				modifier = won_holy_war
				years = 20
			}	
		}
		ROOT = {
			show_scope_change = no
			participation_scaled_prestige = -250
			participation_scaled_piety = -100
			imprison = FROM
		}
		hidden_effect = {
			any_attacker = {
				limit = { NOT = { character = ROOT } }
				participation_scaled_prestige = -250
				participation_scaled_piety = -100
			}
		}
		FROM = {
			show_scope_change = no
			prestige = 250
			piety = 100
			hidden_effect = { 
				if = { 
					limit = { NOT = { has_opinion_modifier = { modifier = opinion_traitor who = ROOT } } } #Allow free revocation of a title.
					opinion = {
						who = ROOT
						modifier = opinion_traitor
					}
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(truebelievers_bid_for_independence): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		any_attacker = {
			clr_flag = flag_truebelievers_revolter
		}
	} 

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

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

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

	allowed_to_target_tributaries = yes
	attacker_rel_head_is_ally = yes # The attacker can call his (main) Pope into the war
	
	sort_priority = 790

	can_use_gui = {
		emf_cb_can_use_gui = yes
	}

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

	is_valid = {
		ROOT = {
			OR = {
				religion = catholic
				religion = fraticelli
			}
			NOT = { same_realm = FROM }
			OR = { 
				#has_flag = flag_pepin_for_coronation
				AND = { 
					religion = catholic
					rightful_religious_head_scope = { has_landed_title = k_papal_state }
				}
				AND = { 
					religion = fraticelli
					rightful_religious_head_scope = { has_landed_title = d_fraticelli }
				}
			}
		}
		FROM = { 
			is_within_diplo_range = ROOT
			NOT = { has_landed_title = k_papal_state }
			NOT = { has_landed_title = d_fraticelli }
			OR = { 		#Target must either be a pagan or heretic of any government form, or it must be a disliked Catholic/Fraticelli who is not a bishop.
				has_flag = flag_pepin_for_coronation # Once the war fires for Pepin, don't invalidate for him based on changes in status
				AND = { 
					is_theocracy = no 
					religion = ROOT
					# Opinion check is 50 higher than the requirement for starting the war to prevent fluctuations
					ROOT = { 
						religion_head = { 
							NOT = { 
								opinion = { 
									who = FROM
									value = 0
								}
							}
						} 
					}
				}
				excommunicated_for = ROOT
				ROOT = {
					emf_hostile_religion_to_FROM = yes
					religion_head = { emf_hostile_religion_to_FROM = yes }
				}
			}
		}
	}
	
	is_valid_title = { FROM = { any_realm_province = { de_jure_liege_or_above = PREVPREV } } }

	on_add = {
		log = "CB(pepin_religious): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}
	
	on_success = {
		log = "CB(pepin_religious): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		ROOT = {
			if = { 
				limit = { 
					religion_head = { 
						any_artifact = { 
							has_artifact_flag = christian
							NOR =  { 
								has_artifact_flag = very_rare
								has_artifact_flag = unique #Won't give the holiest ones away.
							}
						} 
					} 
				}
				random_list = { 
					20 = { 
						modifier = { 
							factor = 0
							NOT = { 
								religion_head = { 
									opinion = { 
										who = ROOT 
										value = 30
									} 
								} 
							}
						}
						modifier = { 
							factor = 1.5
							religion_head = { 
								opinion = { 
									who = ROOT 
									value = 50
								} 
							}
						}
						modifier = { 
							factor = 1.5
							religion_head = { 
								opinion = { 
									who = ROOT 
									value = 70
								} 
							}
						}
						modifier = { 
							factor = 1.5
							religion_head = { 
								opinion = { 
									who = ROOT 
									value = 90
								} 
							}
						}
						set_flag = flag_pope_gifts_artifact #Effect happens in event. 
					}
					80 = { #Nothing happens
					}
				}
			}
			character_event = { id = HF.20224 } #Congratulations\ Papal Coronation may proceed if flagged.
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_piety = 75
				participation_scaled_prestige = 50
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
				hidden_tooltip = {
					if = {
						limit = {
							has_ambition = obj_strengthen_religion
							NOT = { religion = FROM }
						}
						change_variable = { which = strengthen_religion value = 1 }
					}
				}
			}
		}
		ROOT = { 
			participation_scaled_piety = 150
			participation_scaled_prestige = 75
			rightful_religious_head_scope = {
				opinion = {
					who = ROOT
					modifier = opinion_papal_land_donation
				}
			}
			if = {
				limit = {
					NOT = { religion = 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 }
					}
				}
			}
		}
		FROM = {
			if = {
				limit = {
					NOT = { religion = ROOT }
				}
				religion_authority = {
					modifier = lost_holy_war
					years = 20
				}		
			}
		}
		FROM = { piety = -100 }
		FROM = { prestige = -50 }
	}

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

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(pepin_religious): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { 
			clr_flag = flag_pepin_for_coronation
		}
		ROOT = { 
			piety = -50 
			prestige = -100
		}
	}

	on_reverse_demand = {
		log = "CB(pepin_religious): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		ROOT = { 
			clr_flag = flag_pepin_for_coronation
		}
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -75
			prestige = -150
		}
		FROM = {
			participation_scaled_piety = 100
			participation_scaled_prestige = 75
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_piety = 75
				participation_scaled_prestige = 50
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
				hidden_tooltip = {
					if = {
						limit = {
							has_ambition = obj_strengthen_religion
							NOT = { religion = ROOT }
						}
						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 = {
			if = {
				limit = {
					NOT = { religion = FROM }
				}
				religion_authority = {
					modifier = lost_holy_war
					years = 20
				}
			}
		}
		FROM = {
			if = {
				limit = {
					NOT = { religion = 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 }
					}
				}
			}
		}
	}

	on_invalidation_title = {
		log = "CB(pepin_religious): INVALIDATED: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { 
			clr_flag = flag_pepin_for_coronation
		} 
	}

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

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = { 
		factor = 1 
		modifier = { #Focus on Coronation first.
			factor = 0.1
			ROOT = {
				emf_uses_furry_coronation = yes
				emf_is_crowned = no
			}
		}
		modifier = { 
			factor = 2
			ROOT = {
				primary_title = { emf_is_hre = yes }
			}
		}
		modifier = { 
			factor = 2
			ROOT = {
				has_landed_title = e_france
			}
		}
		modifier = { 
			factor = 0.25
			ROOT = {
				NOT = {
					trait = zealous
				}
			}
		}
		modifier = { #I like the Pope and I will help him.
			factor = 1.5
			ROOT = {
				religion_head = {
					reverse_opinion = { 
						who = ROOT 
						value = 50 
					}
				}
			}
		}
		modifier = { #I like the Pope and I will help him.
			factor = 1.5
			ROOT = {
				religion_head = {
					reverse_opinion = { 
						who = ROOT 
						value = 60 
					}
				}
			}
		}
		modifier = { #I like the Pope and I will help him.
			factor = 1.5
			ROOT = {
				religion_head = {
					reverse_opinion = { 
						who = ROOT 
						value = 70 
					}
				}
			}
		}
		modifier = { #I like the Pope and I will help him.
			factor = 2
			ROOT = {
				religion_head = {
					reverse_opinion = { 
						who = ROOT 
						value = 80 
					}
				}
			}
		}
		modifier = { #I like the Pope and I will help him.
			factor = 2
			ROOT = {
				religion_head = {
					reverse_opinion = { 
						who = ROOT 
						value = 90 
					}
				}
			}
		}
		modifier = { #Heathens are holding Italy, take action!
			factor = 5
			ROOT = {
				NOT = {
					trait = cynical
				}
			}
			FROM = {
				NOT = { religion = ROOT }
			}
		}
		modifier = { #The Papacy doesn't hold Rome, take action!
			factor = 5
			d_latium = {
				OR = {
					owner_under_FROM = yes
					any_de_jure_vassal_title = { owner_under_FROM = yes }
				}
			}
		}
		modifier = { 
			factor = 2
			d_spoleto = {
				OR = {
					owner_under_FROM = yes
					any_de_jure_vassal_title = { owner_under_FROM = yes }
				}
			}
		}
		modifier = { #Historical tweaking
			factor = 0.25
			d_benevento = {
				OR = {
					owner_under_FROM = yes
					any_de_jure_vassal_title = { owner_under_FROM = yes }
				}
			}
		}
		modifier = {
			factor = 0.8
			ROOT = {
				distance_from_realm = { who = FROM value = 40 }
			}
		}
		modifier = {
			factor = 0.8
			ROOT = {
				distance_from_realm = { who = FROM value = 80 }
			}
		}		
		modifier = {
			factor = 0.75
			ROOT = {
				distance_from_realm = { who = FROM value = 120 }
			}
		}
		modifier = {
			factor = 0.5
			ROOT = {
				distance_from_realm = { who = FROM value = 150 }
			}
		}
		modifier = {
			factor = 0.25
			ROOT = {
				distance_from_realm = { who = FROM value = 200 }
			}
		}
	}
}

# Romans rise up after their city's churches have been destroyed by mad Emperor.
roman_christian_revolt = {
	name = CB_NAME_HERETIC_UPRISING
	war_name = WAR_NAME_HERETIC_UPRISING
	sprite = 7
	truce_days = 365
	hostile_against_others = yes
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no
	check_de_jure_tier = DUKE
	is_permanent = no
	battle_warscore_mult = 0.5
	allow_whitepeace = no
	
	can_use = {
		ROOT = { has_flag = roman_christian_revolter }
	}
 
	on_add = {
		log = "CB(roman_christian_revolt): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(roman_christian_revolt): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		ROOT = {
			prestige = 200
			piety = 100
			set_global_flag = emf_latb_upon_gain
			occupy_minors_of_occupied_settlements = FROM
			gain_all_occupied_titles = { who = FROM type = revolt }
			clr_global_flag = emf_latb_upon_gain
			clr_flag = religious_revolter
		}
		FROM = {
			prestige = -500
			piety = -500
			emf_abdicate = yes
			religion_authority = {
				modifier = lost_religious_revolt
			}		
		}
	}
	
	on_success_title = {
		emf_cb_latb = 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
			}
		}
		if = {
			limit = {
				controls_religion = no
				owner_under_FROM = yes
			}
			usurp_title = { target = ROOT type = holy_war }
		}
		if = {
			limit = {
				has_holder = no
			}
			usurp_title = { target = ROOT type = holy_war }
		}
		pf_liege_change_under_title_end_effect = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_reverse_demand = {
		log = "CB(roman_christian_revolt): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		ROOT = {
			clear_wealth = yes
			imprison = FROM
			clr_flag = religious_revolter
			hidden_tooltip = {
				add_character_modifier = { 
					name = broken_spirit
					days = -1
				}
			}
		}
		FROM = {
			prestige = 100
			piety = 200
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor 
					who = ROOT
					months = 1200
				}
				add_character_modifier = { 
					name = crushed_revolt
					days = 3650
				}
			}
			religion_authority = {
				modifier = won_religious_revolt
			}
		}
		hidden_tooltip = { 
			if = { 
				limit = { 
					FROM = { 
						NOR = { 
							religion = hellenic_pagan
							religion = hellenic_pagan_reformed
			 			} 
			 		}
			 	}
				333 = { set_flag = flag_rome_primed_for_conversion } #If the Emperor burning Rome is about to switch to hellenism and wins the war, Rome turns hellenic automatically.
			} 
			if = { 
				limit = { 
					FROM = { 
						OR = { 
							religion = hellenic_pagan
							religion = hellenic_pagan_reformed
			 			} 
			 		}
			 	}
				333 = { religion = FROM } #If he has already revealed his conversion, convert the province immediately.
			} 
		}
	}

	on_invalidation_title = {
		log = "CB(roman_christian_revolt): INVALIDATED: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

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

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

abrahamic_antihellenic_revolt = {
	name = CB_ANTIHELLENIC_WAR
	war_name = WAR_NAME_ANTIHELLENIC_WAR
	sprite = 12
	truce_days = 3650
	major_revolt = yes
	allow_whitepeace = no
	
	attacker_can_call_allies = no
	can_ask_to_join_war = no
	
	is_independence = yes
	
	can_use = {
		ROOT = {
			vassal_of = FROM
		}
	}

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

	on_add = {
		log = "CB(abrahamic_antihellenic_revolt): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(abrahamic_antihellenic_revolt): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		FROM = {
			show_scope_change = no
			pf_lower_CA_effect = yes
			pf_lower_TO_effect = yes
		}
		if = {
			limit = {
				FROM = {
					current_heir = { 
						NOT = { religion_group = pagan_group }
					}
				}
			}
			FROM = { 
				emf_abdicate = yes
			}
		}
		else = {
			ROOT = { save_event_target_as = emf_abdicate_real_heir }
			FROM = { 
				emf_abdicate = yes
			}
		}
		FROM = {
			death = { death_reason = death_execution_burning }
		}
		any_attacker = {
			clr_flag = flag_abrahamic_revolter
		}
		hidden_tooltip = {
			ROOT = {
				clr_flag = flag_abrahamic_revolter
				prestige = 100
				piety = 100
				religion_authority = {
					modifier = won_holy_war
					years = 20
				}
				any_demesne_title = {
					add_pressed_claim = FROM
				}
			}
			FROM = {
				religion_authority = {
					modifier = lost_holy_war
					years = 20
				}	
			}	
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
			participation_scaled_piety = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_prestige = 100
				participation_scaled_piety = 100
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_reverse_demand = {
		log = "CB(abrahamic_antihellenic_revolt): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		hidden_tooltip = {
			any_attacker = {
				clr_flag = flag_abrahamic_revolter
			}
			ROOT = {
				religion_authority = {
					modifier = lost_holy_war
					years = 20
				}
			}
			FROM = {
				religion_authority = {
					modifier = won_holy_war
					years = 20
				}	
			}
			any_player = { narrative_event = { id = HF.23117 } }
			if = {
				limit = {
					FROM = { ai = yes }	
				}
				FROM = { narrative_event = { id = HF.23117 } }		
			}	
		}
		ROOT = {
			prisoner = FROM
		}
		any_attacker = {
			hidden_tooltip = { 
				participation_scaled_prestige = -100
				participation_scaled_piety = -100 
			}
		}
		FROM = {
			prestige = 100
			piety = 100
			hidden_tooltip = { 
				if = { 
					limit = { NOT = { has_opinion_modifier = { modifier = opinion_traitor who = ROOT } } } #Allow free revocation of a title.
					opinion = {
						who = ROOT
						modifier = opinion_traitor
					}
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(abrahamic_antihellenic_revolt): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		any_attacker = {
			clr_flag = flag_abrahamic_revolter
		}
	}

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

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

flower_war_cb = {
	name = CB_NAME_FLOWER_WAR
	war_name = WAR_NAME_FLOWER_WAR
	sprite = 21
	truce_days = 3650
	is_permanent = yes
	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no
	
	sort_priority = 1000
	
	can_use_gui = {
		emf_cb_can_use_gui = yes
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			has_dlc = "Holy Fury"
			emf_ritual_sacrifice_religion = yes
			NOT = { has_religion_feature = religion_peaceful } #Aztecs could reform as Peaceful.
			NOT = { same_realm = FROM }
			NOT = { is_liege_or_above = FROM }
			mercenary = no
			trait = bloodthirsty_gods_3
		}
		FROM = {
			in_revolt = no
			is_nomadic = no
			NOT = { is_offmap_governor = yes }
			NOT = {
				any_war = {
					defender = { character = PREVPREV }
					using_cb = flower_war_cb
				}
			}
		}
	}

	is_valid = {
		ROOT = { 
			emf_ritual_sacrifice_religion = yes
			trait = bloodthirsty_gods_3 
		}
	}

	on_add = {
		log = "CB(flower_war_cb): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(flower_war_cb): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		ROOT = {
			show_scope_change = no
			participation_scaled_piety = 100
			participation_scaled_prestige = 100
			hidden_tooltip = { change_variable = { which = burned_hearts_points value = 5 } }
			religion_authority = {
				modifier = won_flower_war
				years = 30
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_piety = 50 }
		}
		FROM = {
			piety = -100
			prestige = -100
			if = {
				limit = {
					is_reformed_religion = ROOT
					controls_religion = no
				}
				religion = ROOT # Reformed pagans are forced to convert back
			}
			custom_tooltip = {
				text = flower_war_cb_tooltip
			}
			hidden_tooltip = {
				any_courtier = { #Sacrifice part of the court.
					limit = {
						is_ruler = no
					}
					random = { 
						chance = 25 
						modifier = {
							factor = 3
							is_unimportant_trigger = yes
						}
						modifier = {
							factor = 3
							has_minor_title = title_commander
						}
						modifier = { #Less likely to be in battle.
							factor = 0.5
							is_adult = no
						}
						modifier = { #Less likely to be in battle.
							factor = 0.5
							is_adult = no
							is_child_of = FROM
						}
						death = { 
							death_reason = death_execution_aztec_sacrifice 
							killer = ROOT
						}
					}
				}
				opinion = {
					modifier = opinion_abducted_peasants
					who = ROOT
				}
				any_vassal = {
					opinion = {
						modifier = opinion_abducted_peasants 
						who = ROOT
					}
				}				
				any_demesne_province = {
					add_province_modifier = { name = bloodthirsty_flower_war_province years = 10 stacking = yes } 
					change_variable = { which = prosperity_value value = -40 }
					if = { 
						limit = { NOT = { check_variable = { which = prosperity_value value = 0 } } } 
						set_variable = { which = prosperity_value value = 0 }
					}
				}
				any_vassal = {
					limit = {
						higher_tier_than = BARON
					}
					any_demesne_province = { 
						add_province_modifier = { name = bloodthirsty_flower_war_province years = 10 stacking = yes } 
						change_variable = { which = prosperity_value value = -40 } 
						if = { 
							limit = { NOT = { check_variable = { which = prosperity_value value = 0 } } } 
							set_variable = { which = prosperity_value value = 0 }
						}
					}
				}
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }
	
	on_fail = {
		log = "CB(flower_war_cb): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		FROM = {
			show_scope_change = no
			piety = 50
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		ROOT = {
			show_scope_change = no
			piety = -100
		}
	}
	
	on_reverse_demand = {
		log = "CB(flower_war_cb): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		ROOT = {
			show_scope_change = no
			piety = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
			religion_authority = {
				modifier = lost_flower_war
				years = 30
			}
		}
		FROM = {
			show_scope_change = no
			piety = 100
			if = {
				limit = { 
					has_dlc = "Legacy of Rome"
					emf_has_roman_empire = yes
					OR = { 
						religion_group = christian
						religion = hellenic_pagan
						religion = hellenic_pagan_reformed
					}
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

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

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = { 
		factor = 1
		modifier = { #Focus on Coronation first.
			factor = 0.1
			ROOT = {
				emf_uses_furry_coronation = yes
				emf_is_crowned = no
			}
		}
	}
}

childrens_crusade = {
	name = CB_NAME_CRUSADE
	war_name = WAR_NAME_CRUSADE
	sprite = 9
	truce_days = 3650
	is_permanent = yes
	is_holy_war = yes
	battle_warscore_mult = 0.5
	max_defender_occupation_score = 85
	check_de_jure_tier = KING
	allow_whitepeace = no
	capturing_defender_is_complete_victory = no
	
	allowed_to_target_tributaries = no

	can_use = {
		emf_cb_can_use = yes
		has_random_dejure = no
		ROOT = {
			religion = catholic
			has_flag = flag_childrens_crusade_leader #Only available for the child crusader.
			has_global_flag = christian_crusades_unlocked
		}
		FROM = {
			OR = {
				NOR = {
					religion_group = ROOT
					ROOT = { emf_syncretized_religion_with_PREV = yes }
				}
				is_heresy_of = ROOT
				is_parent_religion = ROOT
				AND = {
					religion_group = pagan_group
					ROOT = { NOT = { emf_syncretized_religion_with_PREV = yes } }
					NOT = { religion = ROOT }
				}
			}
		}
	}

	can_use_title = {
		emf_cb_can_use_de_jure_title = yes
	}
	
	is_valid = {
		ROOT = {
			has_flag = flag_childrens_crusade_leader #Only available for the child crusader.
		}
		FROM = {
			OR = {
				NOR = {
					religion_group = ROOT
					ROOT = { emf_syncretized_religion_with_PREV = yes }
				}
				is_heresy_of = ROOT
				is_parent_religion = ROOT
				AND = {
					religion_group = pagan_group
					ROOT = { NOT = { emf_syncretized_religion_with_PREV = yes } }
					NOT = { religion = ROOT }
				}
			}
		}
	}

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

	on_add = {
		log = "CB(childrens_crusade): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { 
			add_trait = crusader 
			diplomatic_immunity = no 
		}
		any_player = { 
			limit = {
				religion_group = FROM
				NOT = {
					character = ROOT
				}
			}
			narrative_event = { id = HF.25824 } #Notify infidels that the children have arrived.
		}
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(childrens_crusade): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		emf_imperial_decay_major_victory = yes
	}

	on_success_title = {
		emf_cb_latb = yes
		ROOT = {
			hidden_tooltip = {
				if = {
					limit = {
						this_has_super_cool_nickname_trigger = no
					}
					random_list = {
						20 = { give_nickname = nick_the_angel_of_location }
						15 = { give_nickname = nick_the_chosen_of_gabriel }
						15 = { 
							trigger = {
								is_female = no
							}
							give_nickname = nick_the_page_of_lord 
						}
						15 = {
							trigger = {
								is_female = yes
							} 
							give_nickname = nick_the_maid_of_virgin 
						}
						15 = { give_nickname = nick_the_messenger_of_christ }
						5 = { give_nickname = nick_the_sword_of_god }
					}
				}
			}
		}
		if = {
			limit = {
				k_jerusalem = { 
					holder_scope = { NOT = { religion = ROOT } } 
				}
			}
			k_jerusalem = { usurp_title_plus_barony_if_unlanded = { target = ROOT type = holy_war } }
		}
		if = {
			limit = {
				k_jerusalem = { 
					has_holder = no
				}
			}
			k_jerusalem = {
				grant_title = ROOT
				hidden_tooltip = { make_primary_title = yes } 
				ROOT = { set_flag = emf_no_law_penalties }
				add_law = { law = succ_primogeniture cooldown = no opinion_effect = no }
				add_law = { law = cognatic_succession cooldown = no opinion_effect = no }
				ROOT = { clr_flag = emf_no_law_penalties }
			}
		}
		if = {
			limit = {
				c_jerusalem = { 
					holder_scope = { NOT = { religion = ROOT } } 
				}
			}
			c_jerusalem = { 
				usurp_title_plus_barony_if_unlanded = { target = ROOT type = holy_war } 
				location = { religion = ROOT }
			}
		}
		hidden_tooltip = {
			ROOT = { 
				clr_flag = flag_childrens_crusade_leader
				remove_character_modifier = children_crusade_morale_0
				remove_character_modifier = children_crusade_morale_1
				remove_character_modifier = children_crusade_morale_2
				remove_character_modifier = children_crusade_morale_3
				remove_character_modifier = children_crusade_morale_4
				remove_character_modifier = voice_of_jesus #Your work here is done.
				if = { 
					limit = { 
						is_female = no
					} 
					create_bloodline = { type = crusader_kid_bloodline }  
				}
				else = {
					create_bloodline = { 
						type = crusader_kid_bloodline 
						inheritance = matrilineal
					}  
				}
				set_bloodline_founder_religion_flag_effect = yes
				any_player = { 
					limit = {
						religion_group = christian
						NOT = {
							character = ROOT
						}
					}
					narrative_event = { id = HF.25828 } #Notify everyone that the crusade has succeeded.
				}
			}
			# Vassalize and convert all infidels under the title.
			pf_liege_change_under_title_begin_effect = yes
			ROOT = {
				vassalize_or_take_under_title = {
					title = PREV
					enemy = FROM
					is_religious = yes
					type = holy_war
				}
			}
			pf_liege_change_under_title_end_effect = yes
			ROOT = {
				remove_trait = disinherited 
				any_courtier = { 
					remove_trait = disinherited
					clr_flag = no_court_invites
				}
				if = {
					limit = {
						is_female = no
					}
					remove_trait = crusader
					add_trait = crusader_king
				}
				else = {
					remove_trait = crusader
					add_trait = crusader_queen
				}
			}
			any_playable_ruler = { 
				limit = {
					religion_group = FROM
					is_within_diplo_range = FROM
					NOT = {
						character = ROOT
					}
				}
				narrative_event = { id = HF.25827 } #Notify all infidels that the crusade has succeeded. Offer chance of conversion/vassalization if within k_jerusalem.
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_prestige = 500
				participation_scaled_piety = 1000
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -100
				}
			}
		}
		any_attacker = {
			limit = {
				ai = no
				NOT = { has_flag = achievement_crusader }
			}
			set_flag = achievement_crusader
		}
		ROOT = {
			participation_scaled_prestige = 500
			participation_scaled_piety = 1000
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -100
			}
			religion_authority = {
				modifier = won_crusade
				years = 20
			}
		}
		FROM = {
			piety = -100
			religion_authority = {
				modifier = lost_crusade
				years = 20
			}			
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_reverse_demand = {
		log = "CB(childrens_crusade): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		emf_imperial_decay_major_loss = yes
		ROOT = {
			hidden_tooltip = {
				clr_flag = flag_childrens_crusade_leader
				remove_character_modifier = children_crusade_morale_0
				remove_character_modifier = children_crusade_morale_1
				remove_character_modifier = children_crusade_morale_2
				remove_character_modifier = children_crusade_morale_3
				remove_character_modifier = children_crusade_morale_4
			}
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -200
			religion_authority = {
				modifier = lost_crusade
				years = 20
			}
			any_courtier = { 
				limit = {
					is_ruler = no
					is_landed = no
					vassal_of = ROOT
				}
				death = { death_reason = death_slavery }
			}
			primary_title = {
				activate_title = { title = THIS status = no }
				emf_unsafe_destroy_title = yes
			}
			death = { death_reason = death_slavery }
		}
		FROM = {
			piety = 100
			religion_authority = {
				modifier = won_crusade
				years = 20
			}
			participation_scaled_piety = 300
			participation_scaled_prestige = 150
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -100
			}
			if = {
				limit = { 
					has_dlc = "Legacy of Rome"
					emf_has_roman_empire = yes
					OR = { 
						religion_group = christian
						religion = hellenic_pagan
						religion = hellenic_pagan_reformed
					}
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
		}
		any_player = { 
			limit = {
				religion_group = christian
				NOT = {
					character = ROOT
				}
			}
			narrative_event = { id = HF.25825 } #Notify everyone that the crusade has failed.
		}
		any_player = { 
			limit = {
				religion_group = FROM
				NOT = {
					character = ROOT
				}
			}
			narrative_event = { id = HF.25826 } #Notify every pagan that the crusade has failed.
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_piety = 300
				participation_scaled_prestige = 150
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -100
				}
			}
		}
		any_attacker = {
			limit = {
				character = ROOT
				uses_decadence = yes
				NOT = {
					any_liege = {
						dynasty = PREV
					}
				}
			}
			participation_scaled_decadence = 35
		}
		any_attacker = {
			limit = {
				NOT = { character = ROOT }
				uses_decadence = yes
				NOT = {
					any_liege = {
						dynasty = PREV
					}
				}
			}
			hidden_tooltip = { 
				participation_scaled_decadence = 35
			}
		}
	}

	on_invalidation_title = { 
		log = "CB(childrens_crusade): INVALIDATED: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { 
			clr_flag = flag_childrens_crusade_leader
			remove_character_modifier = children_crusade_morale_0
			remove_character_modifier = children_crusade_morale_1
			remove_character_modifier = children_crusade_morale_2
			remove_character_modifier = children_crusade_morale_3
			remove_character_modifier = children_crusade_morale_4
			any_courtier = { 
				limit = {
					is_ruler = no
					is_landed = no
					vassal_of = ROOT
				}
				death = { death_reason = death_slavery }
			}
			primary_title = {
				activate_title = { title = THIS status = no }
				emf_unsafe_destroy_title = yes
			}
			death = { death_reason = death_slavery } 
		}
		any_player = { 
			limit = {
				religion_group = christian
				NOT = {
					character = ROOT
				}
			}
			narrative_event = { id = HF.25823 days = 7 } #Notify everyone that the crusade has flopped.
		}
	}

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

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

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

	allowed_to_target_tributaries = no
	
	sort_priority = 4000

	can_use_gui = {
		emf_cb_can_use_gui = yes
	}

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

	is_valid = {
		ROOT = {
			higher_real_tier_than = DUKE
			has_religion_feature = religion_warlike
			is_female = yes
			has_religion_feature = religion_matriarchal
			NOT = { same_realm = FROM }
		}
		FROM = {
			is_nomadic = no
			is_merchant_republic = no
			is_theocracy = no
			is_female = no
			independent = yes
		}
	}
	
	on_add = {
		log = "CB(matriarchal_deposition_war): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}
	
	on_success = {
		log = "CB(matriarchal_deposition_war): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		hidden_tooltip = { 
			any_attacker = {
				limit = { NOT = { character = ROOT } }
				participation_scaled_piety = 50
				participation_scaled_prestige = 25
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
						NOT = { religion = FROM }
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
		}
		ROOT = {
			participation_scaled_piety = 100
			participation_scaled_prestige = 150
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
			if = {
				limit = {
					NOT = { religion = FROM }
				}
				religion_authority = {
					modifier = won_holy_war
					years = 20
				}
			}
		}
		FROM = { piety = -100 }
		FROM = { prestige = -50 }
		custom_tooltip = {
			text = sex_deposition_cb_succ_tip
			hidden_tooltip = {
				FROM = {
					#If dynasty member of proper sex exists, abdicate to it.
					#Else lose main title to vassal.
					#Else lose main title to spawned character.

					#Then, if religious dogma allows it, switch status of Women.
					#Then, if title passed on a spawned character, change succession laws.
					#Finally, if owning Jade Dragon set up permanent Tributary, if Horse Lords set up temporary tributary, else set up non-aggression pact.
					primary_title = { save_event_target_as = title_usurped }
					save_event_target_as = scoped_loser
					if = {
						limit = {
							any_dynasty_member = {
								is_female = yes
								is_incapable = no
								NOT = { has_religion_feature = religion_patriarchal }
							}
						}
						random_dynasty_member = {
							limit = {
								is_female = yes
								is_incapable = no
								NOT = { has_religion_feature = religion_patriarchal }
							}
							preferred_limit = {
								is_child_of = PREV
								is_adult = yes
								religion = ROOT
							}
							preferred_limit = {
								is_close_relative = PREV
								is_adult = yes
								religion = ROOT
							}
							preferred_limit = {
								is_adult = yes
								religion = ROOT
							}
							preferred_limit = {
								is_close_relative = PREV
								is_adult = yes
							}
							preferred_limit = {
								is_adult = yes
							}
							save_event_target_as = new_swapped_ruler
							usurp_title = { target = event_target:title_usurped type = invasion }
							event_target:scoped_loser = {
								any_demesne_title = {
									limit = {
										# CP-3.0 NOTE: uh, don't we want to stop usurpation of religious titles?
										tier = PREVPREV
									}
									event_target:new_swapped_ruler = { usurp_title = { target = PREV type = invasion } }
									add_pressed_claim = PREV
								}
							}
							event_target:scoped_loser = {
								set_defacto_liege = event_target:new_swapped_ruler
								emf_liege_change = yes
							}
						}
					}
					else_if = {
						limit = {
							any_vassal = {
								is_female = yes
								is_incapable = no
								NOT = { has_religion_feature = religion_patriarchal }
							}
						}
						random_vassal = {
							limit = {
								is_female = yes
								is_incapable = no
								NOT = { has_religion_feature = religion_patriarchal }
							}
							preferred_limit = {
								religion = ROOT
								is_adult = yes
								NOT = { practical_age = 30 }
								trait = zealous
							}
							preferred_limit = {
								religion = ROOT
								is_adult = yes
								trait = zealous
							}
							preferred_limit = {
								religion = ROOT
								is_adult = yes
							}
							preferred_limit = {
								is_adult = yes
								has_religion_feature = religion_matriarchal
							}
							preferred_limit = {
								is_adult = yes
							}
							save_event_target_as = new_swapped_ruler
							usurp_title = { target = event_target:title_usurped type = invasion }
							event_target:scoped_loser = {
								any_demesne_title = {
									limit = {
										tier = PREVPREV
									}
									event_target:new_swapped_ruler = { usurp_title = { target = PREV type = invasion } }
									add_pressed_claim = PREV
								}
							}
							event_target:scoped_loser = {
								set_defacto_liege = event_target:new_swapped_ruler
								emf_liege_change = yes
							} 
						}
					}
					else = {
						location = {
							create_character = {
								religion = ROOT
								culture = event_target:scoped_loser
								dynasty = random
								random_traits = yes
								female = yes
								age = 23
								health = 7
								fertility = 0.7
								attributes = {
									martial = 8
									diplomacy = 8
									stewardship = 8
								}
							}
							new_character = {
								emf_dna_set_flags_for_high_health = yes
								remove_trait = cynical
								wealth = 250
								prestige = 300
								piety = 100
								emf_new_character_noble = yes
								save_event_target_as = new_swapped_ruler
								usurp_title = { target = event_target:title_usurped type = invasion }
								event_target:scoped_loser = {
									any_demesne_title = {
										limit = {
											tier = PREVPREV
										}
										event_target:new_swapped_ruler = { usurp_title = { target = PREV type = invasion } }
										add_pressed_claim = PREV
									}
								}
								event_target:scoped_loser = {
									set_defacto_liege = event_target:new_swapped_ruler
									emf_liege_change = yes
								}
							}
						}
					}
					#Titles assigned, now onto laws.
					if = {
						limit = {
							ROOT = {
								has_religion_feature = religion_matriarchal
							}
						}
						event_target:new_swapped_ruler = {
							set_flag = emf_no_law_penalties
							any_demesne_title = {
								if = {
									limit = {
										OR = {
											has_law = agnatic_succession
											has_law = cognatic_succession
											has_law = true_cognatic_succession
										}
									}
									if = {
										limit = { is_law_potential = enatic_succession }
										add_law = {
											law = enatic_succession
											cooldown = no
											opinion_effect = yes
										}
									}
									else_if = {
										limit = { is_law_potential = enatic_cognatic_succession }
										add_law = {
											law = enatic_cognatic_succession
											cooldown = no
											opinion_effect = yes
										}
									}
									else_if = {
										limit = { is_law_potential = true_cognatic_succession }
										add_law = {
											law = true_cognatic_succession
											cooldown = no
											opinion_effect = yes
										}
									}
									else_if = {
										limit = {
											is_law_potential = cognatic_succession
											NOT = { has_law = true_cognatic_succession }
										}
										add_law = {
											law = cognatic_succession
											cooldown = no
											opinion_effect = yes
										}
									}
								}
								if = {
									limit = { is_law_potential = status_of_women_4 }
									add_law = {
										law = status_of_women_4
										cooldown = no
										opinion_effect = no
									}
								}
							} 
							clr_flag = emf_no_law_penalties
							recalc_succession = yes
						}
					}
					#Laws set, now onto pacts.
					if = {
						limit = {
							has_dlc = "Jade Dragon"
							is_tributary = no
						}
						ROOT = {
							make_tributary = { who = event_target:new_swapped_ruler tributary_type = permanent }
						}
					}
					else_if = {
						limit = {
							has_dlc = "Horse Lords"
							is_tributary = no
						}
						ROOT = {
							make_tributary = { who = event_target:new_swapped_ruler percentage = 0.3 }
						}
					}
					else = {
						ROOT = {
							opinion = { 
								modifier = in_non_aggression_pact
								years = 50
								who = event_target:new_swapped_ruler
							}
						}
					}
				}
			}
		}
		if = {
			limit = {
				OR = {
					has_dlc = "Horse Lords"
					has_dlc = "Jade Dragon"
				}
				is_tributary = no
			}
			custom_tooltip = {
				text = sex_deposition_cb_succ_tip_2
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(matriarchal_deposition_war): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { 
			piety = -125 
			prestige = -250
		}
	}

	on_reverse_demand = {
		log = "CB(matriarchal_deposition_war): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -250
			prestige = -500
		}
		FROM = {
			participation_scaled_piety = 200
			participation_scaled_prestige = 100
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_piety = 200
				participation_scaled_prestige = 100
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
				hidden_tooltip = {
					if = {
						limit = {
							has_ambition = obj_strengthen_religion
							NOT = { religion = ROOT }
						}
						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 = {
			if = {
				limit = { NOT = { religion = FROM } }
				religion_authority = {
					modifier = lost_holy_war
					years = 20
				}
			}
		}
		FROM = { 
			if = {
				limit = { NOT = { religion = 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 }
					}
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(matriarchal_deposition_war): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

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

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = { 
		factor = 1
		modifier = { #Focus on Coronation first.
			factor = 0.1
			ROOT = {
				emf_uses_furry_coronation = yes
				emf_is_crowned = no
			}
		}
		modifier = { 
			factor = 10
			ROOT = { religion = FROM } #Intolerable circumstances.
		}
		modifier = { 
			factor = 5
			ROOT = { has_religion_feature = religion_patriarchal }
			FROM = { has_religion_feature = religion_matriarchal }
		}
		modifier = { 
			factor = 5
			ROOT = { has_religion_feature = religion_matriarchal }
			FROM = { has_religion_feature = religion_patriarchal }
		}
	}
}

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

	allowed_to_target_tributaries = no
	
	sort_priority = 4000

	can_use_gui = {
		emf_cb_can_use_gui = yes
	}

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

	is_valid = {
		ROOT = {
			higher_real_tier_than = DUKE
			has_religion_feature = religion_warlike
			is_female = no
			has_religion_feature = religion_patriarchal
			NOT = { same_realm = FROM }
		}
		FROM = {
			is_nomadic = no
			is_merchant_republic = no
			is_theocracy = no
			is_female = yes 
			independent = yes
		}
	}
	
	on_add = {
		log = "CB(patriarchal_deposition_war): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(patriarchal_deposition_war): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		hidden_tooltip = { 
			any_attacker = {
				limit = { NOT = { character = ROOT } }				
				participation_scaled_piety = 50
				participation_scaled_prestige = 25
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
				if = {
					limit = {
						has_ambition = obj_strengthen_religion
						NOT = { religion = FROM }
					}
					change_variable = { which = strengthen_religion value = 1 }
				}
			}
		}
		ROOT = {
			participation_scaled_piety = 100
			participation_scaled_prestige = 150
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
			if = {
				limit = { NOT = { religion = FROM } }
				religion_authority = {
					modifier = won_holy_war
					years = 20
				}
			}
		}
		FROM = { piety = -100 }
		FROM = { prestige = -50 }
		custom_tooltip = {
			text = sex_deposition_cb_succ_tip
			hidden_tooltip = {
				FROM = {
					#If dynasty member of proper sex exists, abdicate to it.
					#Else lose main title to vassal.
					#Else lose main title to spawned character.

					#Then, if religious dogma allows it, switch status of Women.
					#Then, if title passed on a spawned character, change succession laws.
					#Finally, if owning Jade Dragon set up permanent Tributary, if Horse Lords set up temporary tributary, else set up non-aggression pact.
					primary_title = { save_event_target_as = title_usurped }
					save_event_target_as = scoped_loser
					if = {
						limit = {
							any_dynasty_member = {
								is_female = no
								is_incapable = no
								NOT = { has_religion_feature = religion_matriarchal }
							}
						}
						random_dynasty_member = {
							limit = {
								is_female = no
								is_incapable = no
								NOT = { has_religion_feature = religion_matriarchal }
							}
							preferred_limit = {
								is_child_of = PREV
								is_adult = yes
								religion = ROOT
							}
							preferred_limit = {
								is_close_relative = PREV
								is_adult = yes
								religion = ROOT
							}
							preferred_limit = {
								is_adult = yes
								religion = ROOT
							}
							preferred_limit = {
								is_close_relative = PREV
								is_adult = yes
							}
							preferred_limit = {
								is_adult = yes
							}
							save_event_target_as = new_swapped_ruler
							usurp_title = { target = event_target:title_usurped type = invasion }
							event_target:scoped_loser = {
								any_demesne_title = {
									limit = {
										tier = PREVPREV
									}
									event_target:new_swapped_ruler = { usurp_title = { target = PREV type = invasion } }
									add_pressed_claim = PREV
								}
							}
							event_target:scoped_loser = {
								set_defacto_liege = event_target:new_swapped_ruler
								emf_liege_change = yes
							}
						}
					}
					else_if = {
						limit = {
							any_vassal = {
								is_female = no
								is_incapable = no
								NOT = { has_religion_feature = religion_matriarchal }
							}
						}
						random_vassal = {
							limit = {
								is_female = no
								is_incapable = no
								NOT = { has_religion_feature = religion_matriarchal }
							}
							preferred_limit = {
								religion = ROOT
								is_adult = yes
								NOT = { practical_age = 30 }
								trait = zealous
							}
							preferred_limit = {
								religion = ROOT
								is_adult = yes
								trait = zealous
							}
							preferred_limit = {
								religion = ROOT
								is_adult = yes
							}
							preferred_limit = {
								is_adult = yes
								has_religion_feature = religion_patriarchal
							}
							preferred_limit = {
								is_adult = yes
							}
							save_event_target_as = new_swapped_ruler
							usurp_title = { target = event_target:title_usurped type = invasion }
							event_target:scoped_loser = {
								any_demesne_title = {
									limit = {
										tier = PREVPREV
									}
									event_target:new_swapped_ruler = { usurp_title = { target = PREV type = invasion } }
									add_pressed_claim = PREV
								}
							}
							event_target:scoped_loser = {
								set_defacto_liege = event_target:new_swapped_ruler
								emf_liege_change = yes
							}
						}
					}
					else = {
						location = {
							create_character = {
								religion = ROOT
								culture = event_target:scoped_loser 
								dynasty = random
								random_traits = yes
								female = no
								age = 23
								health = 7
								fertility = 0.7
								attributes = {
									martial = 8
									diplomacy = 8
									stewardship = 8
								}
							}
							new_character = {
								emf_dna_set_flags_for_high_health = yes
								remove_trait = cynical
								wealth = 250
								prestige = 300
								piety = 100
								emf_new_character_noble = yes
								save_event_target_as = new_swapped_ruler
								usurp_title = { target = event_target:title_usurped type = invasion }
								event_target:scoped_loser  = {
									any_demesne_title = {
										limit = {
											tier = PREVPREV
										}
										event_target:new_swapped_ruler = { usurp_title = { target = PREV type = invasion } }
										add_pressed_claim = PREV
									}
								}
								event_target:scoped_loser = {
									set_defacto_liege = event_target:new_swapped_ruler
									emf_liege_change = yes
								}
							}
						}
					}
					#Titles assigned, now onto laws.
					if = {
						limit = {
							ROOT = {
								has_religion_feature = religion_patriarchal
							}
						}
						event_target:new_swapped_ruler = {
							set_flag = emf_no_law_penalties
							any_demesne_title = {
								if = {
									limit = {
										OR = {
											has_law = enatic_succession
											has_law = enatic_cognatic_succession
											has_law = true_cognatic_succession
										}
									}
									if = {
										limit = { is_law_potential = agnatic_succession }
										add_law = {
											law = agnatic_succession
											cooldown = no
											opinion_effect = yes
										}
									}
									else_if = {
										limit = { is_law_potential = cognatic_succession }
										add_law = {
											law = cognatic_succession
											cooldown = no
											opinion_effect = yes
										}
									}
									else_if = {
										limit = { is_law_potential = true_cognatic_succession }
										add_law = {
											law = true_cognatic_succession
											cooldown = no
											opinion_effect = yes
										}
									}
									else_if = {
										limit = {
											is_law_potential = enatic_cognatic_succession
											NOT = { has_law = true_cognatic_succession }
										}
										add_law = {
											law = enatic_cognatic_succession
											cooldown = no
											opinion_effect = yes
										}
									}
								}
								if = {
									limit = { is_law_potential = status_of_women_0 }
									add_law = {
										law = status_of_women_0
										cooldown = no
										opinion_effect = no
									}
								}
							} 
							clr_flag = emf_no_law_penalties
							recalc_succession = yes
						}
					}
					#Laws set, now onto pacts.
					if = {
						limit = {
							has_dlc = "Jade Dragon"
							is_tributary = no
						}
						ROOT = {
							make_tributary = { who = event_target:new_swapped_ruler tributary_type = permanent }
						}
					}
					else_if = {
						limit = {
							has_dlc = "Horse Lords"
							is_tributary = no
						}
						ROOT = {
							make_tributary = { who = event_target:new_swapped_ruler percentage = 0.3 }
						}
					}
					else = {
						ROOT = {
							opinion = { 
								modifier = in_non_aggression_pact
								years = 50
								who = event_target:new_swapped_ruler
							}
						}
					}
				}
			}
		}
		if = {
			limit = {
				OR = {
					has_dlc = "Horse Lords"
					has_dlc = "Jade Dragon"
				}
				is_tributary = no
			}
			custom_tooltip = {
				text = sex_deposition_cb_succ_tip_2
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(patriarchal_deposition_war): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { 
			piety = -125 
			prestige = -250
		}
	}

	on_reverse_demand = {
		log = "CB(patriarchal_deposition_war): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -250
			prestige = -500
		}
		FROM = {
			participation_scaled_piety = 200
			participation_scaled_prestige = 100
			if = {
				limit = {
					uses_decadence = yes
				}
				participation_scaled_decadence = -10
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { 
				participation_scaled_piety = 200
				participation_scaled_prestige = 100
				if = {
					limit = {
						uses_decadence = yes
					}
					participation_scaled_decadence = -10
				}
				hidden_tooltip = {
					if = {
						limit = {
							has_ambition = obj_strengthen_religion
							NOT = { religion = ROOT }
						}
						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 = {
			if = {
				limit = { NOT = { religion = FROM } }
				religion_authority = {
					modifier = lost_holy_war
					years = 20
				}
			}
		}
		FROM = { 
			if = {
				limit = { NOT = { religion = 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 }
					}
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(patriarchal_deposition_war): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

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

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	
	defender_ai_defeat_worth = {
		factor = 100
	}
	
	ai_will_do = { 
		factor = 1
		modifier = { #Focus on Coronation first.
			factor = 0.1
			ROOT = {
				emf_uses_furry_coronation = yes
				emf_is_crowned = no
			}
		}
		modifier = { 
			factor = 10
			ROOT = { religion = FROM } #Intolerable circumstances.
		}
		modifier = { 
			factor = 5
			ROOT = { has_religion_feature = religion_patriarchal }
			FROM = { has_religion_feature = religion_matriarchal }
		}
		modifier = { 
			factor = 5
			ROOT = { has_religion_feature = religion_matriarchal }
			FROM = { has_religion_feature = religion_patriarchal }
		}
	}
}

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

	on_add = {
		log = "CB(depose_impious_pope): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(depose_impious_pope): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		custom_tooltip = {
			text = depose_impious_pope_cb_win_TT
			any_attacker = {
				piety = 100
			}
		}
		FROM = {
			show_scope_change = no
			emf_abdicate = yes
		}
		FROM = {
			show_scope_change = no
			religion_authority = {
				modifier = depose_impious_pope_modifier
			}
		}
		hidden_tooltip = {
			any_defender = {
				set_flag = impious_papal_defender
			}
			any_playable_ruler = {
				limit = {
					religion = catholic
				}
				narrative_event = { id = HF.40094 }
			}
			ROOT = {
				narrative_event = { id = HF.40094 }
			}
			k_papal_state = {
				owner = {
					save_event_target_as = current_pope
					reverse_imprison = FROM
				}
			}
			any_attacker = {
				reverse_opinion = {
					modifier = opinion_grateful
					who = event_target:current_pope
					years = 10
				}
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_reverse_demand = {
		log = "CB(depose_impious_pope): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		custom_tooltip = {
			text = depose_impious_pope_cb_lose_TT
			any_attacker = {
				add_trait = excommunicated
				piety = -100
			}
		}
		FROM = {
			show_scope_change = no
			religion_authority = {
				modifier = failed_to_depose_impious_pope_modifier
			}
		}
		hidden_tooltip = {
			any_defender = {
				set_flag = impious_papal_defender
			}
			any_playable_ruler = {
				limit = {
					religion = catholic
				}
				narrative_event = { id = HF.40095 }
			}
			ROOT = {
				narrative_event = { id = HF.40095 }
			}
		}

	}

	on_attacker_leader_death = {
		log = "CB(depose_impious_pope): ATTACKER_DEATH: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		if = {
			limit = { has_global_flag = pre_catholic_civil_war }
			end_war = invalid
		}
	}

	on_invalidation = {
		log = "CB(depose_impious_pope): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

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

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

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

	is_holy_war = yes

	defender_unoccupied_warscore = yes

	allowed_to_target_tributaries = yes

	can_use = {
		always = no # Can only be initiated through special means
	}

	is_valid = {
		FROM = {
			NOT = {
				religion = ROOT
			}
		}
	}

	on_add = {
		log = "CB(shepherds_crusade_invasion): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(shepherds_crusade_invasion): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
	}

	on_success_title = {
		emf_cb_latb = yes
		save_event_target_as = shepherds_kingdom
		if = {
			limit = {
				OR = {
					has_holder = no
					owner_under_FROM = yes
				}
			}
			gain_title = ROOT
		}
		else = {
			create_title = {
				tier = KING
				name = "KINGDOM_OF_THE_SHEPHERDS"
				holder = ROOT
				culture = ROOT
				base_title = event_target:shepherds_kingdom
			}
			add_claim = ROOT
		}
		pf_liege_change_under_title_begin_effect = yes
		ROOT = {
			gain_settlements_under_title = {
				title = PREV
				enemy = FROM
				is_crusade = yes # Even if the title holder is not participating in the war, gain holdings occupied by all Crusade participants
				type = holy_war
			}
		}
		pf_liege_change_under_title_end_effect = yes
		ROOT = {
			show_scope_change = no
			clr_flag = shepherds_leader
			set_defacto_liege = ROOT
			emf_liege_change = yes
			hidden_tooltip = {
				dynasty = father_bastard
				if = {
					limit = {
						is_female = no
					}
					add_trait = crusader_king
					create_bloodline = {
						type = crusader_commander
					}
				}
				else = {
					add_trait = crusader_queen
					create_bloodline = {
						type = crusader_commander
						inheritance = matrilineal
					}
				}
				set_bloodline_founder_religion_flag_effect = yes
				event_target:shepherds_kingdom = {
					capital_scope = {
						ROOT = {
							capital = PREV
						}
						save_event_target_as = shepherds_capital
						duchy = {
							save_event_target_as = shepherds_duchy
						}
					}
				}
				set_flag = emf_no_law_penalties
				primary_title = {
					add_law = {
						law = succ_primogeniture
						cooldown = no
						opinion_effect = no
					}
					add_law = {
						law = centralization_2
						cooldown = no
						opinion_effect = no
					}
					add_law = {
						law = cognatic_succession
						cooldown = no
						opinion_effect = no
					}
					add_law = {
						law = administration_1
						cooldown = no
						opinion_effect = no
					}
					add_law = {
						law = crown_authority_3
						cooldown = no
						opinion_effect = no
					}
				}
				clr_flag = emf_no_law_penalties				
				character_event = { id = HFP.41090 days = 1 }
				if = {
					limit = {
						NOT = {
							wealth = 200
						}
					}
					clear_wealth = yes
					wealth = 200
				}
				any_demesne_title = {
					limit = {
						OR = {
							NOT = {
								location = {
									duchy = {
										title = event_target:shepherds_duchy
									}
								}
							}
							holding_type = city
							holding_type = temple
						}
					}
					if = {
						limit = {
							holding_type = city
						}
						create_random_steward = {
							random_traits = yes
							dynasty = random
							trait = crusader
							trait = brave
						}
						new_character = {
							emf_new_character_steward = yes
						}
					}
					else_if = {
						limit = {
							holding_type = temple
						}
						create_random_priest = {
							random_traits = yes
							dynasty = random
							trait = crusader
							trait = brave
						}
						new_character = {
							emf_new_character_priest = yes
						}
					}
					else_if = {
						limit = {
							holding_type = castle
						}
						create_random_soldier = {
							random_traits = yes
							dynasty = random
							trait = crusader
							trait = brave
						}
						new_character = {
							emf_new_character_noble = yes
						}
					}
					new_character = {
						grant_title_no_opinion = PREV
						ROOT = {
							set_truce = {
								who = PREV
								years = 0
							}
						}
					}
				}
				event_target:shepherds_kingdom = {
					any_direct_de_jure_vassal_title = {
						random_direct_de_jure_vassal_title = {
							limit = {
								owner_under_ROOT = yes
							}
							owner = {
								gain_title = PREVPREV
								primary_title = {
									any_direct_de_jure_vassal_title = {
										limit = {
											owner_under_ROOT = yes
										}
										owner = {
											set_defacto_liege = PREVPREVPREV
											emf_liege_change = yes
										}
									}
								}
								set_defacto_liege = ROOT
								emf_liege_change = yes
							}
						}
					}
				}
				any_claim = {
					limit = { NOT = { title = PREVPREV } }
					remove_claim = ROOT
				}
			}
			if = {
				limit = {
					can_apply_average_nickname_trigger = yes
				}
				random_list = {
					10 = { give_nickname = nick_the_crusader }
					10 = { give_nickname = nick_the_holy }
					10 = { give_nickname = nick_the_pilgrim }
					10 = { give_nickname = nick_the_protector }
					10 = { give_nickname = nick_the_guardian }
					40 = { give_nickname = nick_the_sword_of_god }
				}
			}
		}
		any_playable_ruler = {
			narrative_event = { id = HFP.42309 }
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_reverse_demand = {
		log = "CB(shepherds_crusade_invasion): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		FROM = {
			piety = 200
			reverse_imprison = ROOT
		}
		any_attacker = {
			transfer_scaled_wealth = {
				to = FROM
				value = 1.0
			}
			piety = -100
		}
		any_playable_ruler = {
			narrative_event = { id = HFP.42308 }
		}
	}

	on_attacker_leader_death = {
		log = "CB(shepherds_crusade_invasion): ATTACKER_DEATH: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		any_playable_ruler = {
			narrative_event = { id = HFP.42310 }
		}
	}

	on_invalidation_title = {
		log = "CB(shepherds_crusade_invasion): INVALIDATED: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

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

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

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

	defender_unoccupied_warscore = yes

	allowed_to_target_tributaries = yes

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

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			OR = {
				has_landed_title = d_teutonic_order
				has_landed_title = k_teutonic_state
			}
		}
		FROM = {
			NOR = {
				religion_group = ROOT
				ROOT = { emf_syncretized_religion_with_PREV = yes }
			}
		}
	}

	can_use_title = {
		kingdom = { has_flag = northern_crusade_active_target }
		emf_cb_can_use_de_jure_title = yes
	}

	is_valid = {
		FROM = { NOT = { religion = ROOT } }
	}

	is_valid_title = {
		any_de_jure_vassal_title = { owner_under_FROM = yes }
	}

	on_add = {
		log = "CB(northern_crusade_war): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(northern_crusade_war): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		any_attacker = {
			limit = {
				NOR = {
					has_landed_title = d_teutonic_order
					has_landed_title = k_teutonic_state
				}
			}
			piety = 150
		}
		any_defender = {
			piety = -100
		}
	}

	on_success_title = {
		emf_cb_latb = yes
		save_event_target_as = northern_crusade_title
		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
			}
		}
		pf_liege_change_under_title_end_effect = yes
		hidden_tooltip = {
			ROOT = {
				# If the Teutonic Order don't have a base to work out from, get one
				if = {
					limit = { capital_scope = { duchy = { title = event_target:northern_crusade_title } } }
					teutonic_order_upgrade_capital_effect = yes
					holy_order_distribute_titles_effect = yes
					character_event = { id = HFP.41090 days = 1 }
				}
				# Become the Teutonic State, if they aren't already
				if = {
					limit = { NOT = { has_landed_title = k_teutonic_state } }
					k_teutonic_state = {
						copy_title_history = d_teutonic_order
						copy_title_laws = d_teutonic_order
					}
					grant_title_no_opinion = k_teutonic_state
					d_teutonic_order = { emf_unsafe_destroy_title = yes }
					activate_title = { title = d_teutonic_order status = no }
				}
			}
			## Rewards
			# Check who to give artifacts
			any_attacker = {
				limit = {
					NOT = { character = ROOT }
					any_war = {
						using_cb = northern_crusade_war
						war_participation = {
							who = PREV
							score > 0.4
						}
					}
				}
				set_flag = northern_crusade_artifact_reward
			}
			# Check who to give money
			any_attacker = {
				limit = {
					NOT = { character = ROOT }
					any_war = {
						using_cb = northern_crusade_war
						war_participation = {
							who = PREV
							score > 0.2
						}
					}
				}
				set_flag = northern_crusade_gold_reward
			}
			# Check who to give artifacts
			any_attacker = {
				limit = {
					NOT = { character = ROOT }
					any_war = {
						using_cb = northern_crusade_war
						war_participation = {
							who = PREV
							score > 0.01
						}
					}
				}
				letter_event = { id = HF.49112 days = 1 }
			}
			# Checking for Bloodline
			most_participating_attacker = {
				if = {
					limit = { NOT = { character = ROOT } }
					if = {
						limit = {
							check_variable = { 
								which = northern_crusade_ally 
								value == 1 
							}
						}
						set_variable = {
							which = northern_crusade_ally 
							value = 2
						}
					}
					else_if = {
						limit = {
							check_variable = {
								which = northern_crusade_ally 
								value = 2
							}
						}
						narrative_event = { id = HF.49114 }
					}
					else = {
						set_variable = {
							which = northern_crusade_ally
							value = 1 
						}
					}
				}
			}
			ROOT = {
				## Misc
				# Check if the Northern Crusade is over
				if = {
					limit = {
						event_target:northern_crusade_title = {
							kingdom = {
								has_flag = northern_crusade_active_target
								NOT = {
									any_direct_de_jure_vassal_title = {
										any_direct_de_jure_vassal_title = {
											holder_scope = { NOT = { religion_group = christian } }
										}
									}
								}
							}
						}
					}
					event_target:northern_crusade_title = {
						kingdom = {
							save_event_target_as = northern_crusade_kingdom
							clr_flag = northern_crusade_active_target
						}
					}
					religion_authority = { modifier = northern_crusade_won }
					any_playable_ruler = { narrative_event = { id = HF.49115 } }
					set_global_flag = northern_crusade_success
					clr_global_flag = northern_crusades_active
				}
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_reverse_demand = {
		log = "CB(northern_crusade_war): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		ROOT = { any_attacker = { piety = -100 } }
		FROM = { any_defender = { piety = 150 } }
		hidden_tooltip = {
			if = {
				limit = {
					check_variable = {
						which = global_northernCrusadeValue
						value == 1
					}
				}
				set_variable = {
					which = global_northernCrusadeValue
					value = 2
				}
			}
			else_if = {
				limit = {
					check_variable = {
						which = global_northernCrusadeValue
						value = 2
					}
				}
				any_playable_ruler = { narrative_event = { id = HF.49113 } }				
			}
			else = {
				set_variable = {
					which = global_northernCrusadeValue
					value = 1
				}
			}
		}
	}

	on_invalidation_title = {
		log = "CB(northern_crusade_war): INVALIDATED: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

# 02_cb_types_shattered_world.txt from here #

shattered_world_county_conquest = {
	name = CB_NAME_SHATTERED_COUNTY_CONQUEST
	war_name = WAR_NAME_SHATTERED_COUNTY_CONQUEST
	sprite = 27
	truce_days = 1825
	hostile_against_others = yes
	full_hostility = yes
	is_permanent = yes
	check_all_titles = yes # if permanent, setting this to true will check against all of someones titles, including vassal held titles
	can_ask_to_join_war = no
	allowed_to_target_tributaries = no
	
	infamy_modifier = 2
	
	sort_priority = 795
	
	can_use_gui = {
		emf_cb_can_use_gui = yes
	}
	
	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}
		emf_shattered_world = yes
		OR = {
			NOT = { has_alternate_start_setting = { setting = shattered_cbs option = off } }
			has_global_flag = emf_rapid_conquest
		}
		trigger_if = {
			limit = { has_alternate_start_setting = { setting = shattered_cbs option = on_limited } }
			total_years_played < 25
		}
		trigger_else_if = {
			limit = {
				OR = {
					has_alternate_start_setting = { setting = shattered_cbs option = on_long }
					has_global_flag = emf_rapid_conquest
				}
			}
			total_years_played < 100
		}
	}

	can_use_title = {
		emf_cb_county_conquest_basic_can_use_title = yes
		emf_cb_can_use_title = yes
		location = {
			any_neighbor_province = {
				OR = {
					owner_under_ROOT = yes
					AND = {
						is_land = no
						any_neighbor_province = {
							owner_under_ROOT = yes
						}
					}
				}
			}
		}
	}

	is_valid_title = {
		owner_under_FROM = yes
	}

	on_add = {
		log = "CB(shattered_world_county_conquest): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(shattered_world_county_conquest): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
	}

	on_success_title = {
		emf_cb_latb = yes
		if = {
			limit = {
				holder_scope = {
					tier = count
					NOT = { num_of_count_titles = 2 }
					lower_tier_than = ROOT
				}
			}
			holder_scope = {
				emf_ROOT_subjugate_ruler = yes
			}
		}
		else = {
			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 = {
					NOT = { de_facto_liege = PREV }
					owner_under_FROM = yes
				}
				usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
			}
		}
		add_pressed_claim = FROM
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(shattered_world_county_conquest): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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 = {
		log = "CB(shattered_world_county_conquest): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			prestige = -100
			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 }
		}
	}

	on_invalidation_title = {
		log = "CB(shattered_world_county_conquest): INVALIDATED: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}
	
	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 = {
				independent = no
				same_liege = FROM
			}
		}
	}
}

shattered_invasion = {
	name = CB_NAME_INVASION
	war_name = WAR_NAME_INVASION
	sprite = 8
	truce_days = 3650
	hostile_against_others = yes
	is_permanent = yes
	can_ask_to_join_war = 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
	
	sort_priority = 1100
	
	can_use_gui = {
		emf_cb_can_use_gui = yes
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			has_flag = great_conqueror
		}
		ROOT = {
			NOT = { is_liege_or_above = FROM }
			mercenary = no
		}
	}
	
	can_use_title = {
		# 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 = {
					owner_under_ROOT = yes
					location = {
						any_neighbor_province = {
							owner_under_ROOT = yes
						}
					}
				}
			}
		}
		emf_cb_can_use_de_jure_title = yes
	}
	
	is_valid_title = { FROM = { any_realm_province = { de_jure_liege_or_above = PREVPREV } } }

	on_add = {
		log = "CB(shattered_invasion): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(shattered_invasion): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		emf_imperial_decay_major_victory = yes
		ROOT = {
			prestige = 200
			hidden_tooltip = {
				set_global_flag = emf_latb_upon_gain
				occupy_minors_of_occupied_settlements = FROM
				gain_all_occupied_titles = { who = FROM type = invasion }
				clr_global_flag = emf_latb_upon_gain
				if = {
					limit = {
						has_nickname = no
					}
					random_list = {
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_magnificent }
						10 = { give_nickname = nick_the_glorious }
						10 = { give_nickname = nick_the_conqueror }
					}
				}
			}
			participation_scaled_prestige = 200
			
			if = {
				limit = {
					uses_decadence = yes
					FROM = { NOT = { uses_decadence = yes } }
				}
				participation_scaled_decadence = -20
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { 
				participation_scaled_prestige = 200 
				if = {
					limit = {
						uses_decadence = yes
						FROM = { NOT = { uses_decadence = yes } }
					}
					participation_scaled_decadence = -20
				}
			}
		}
		FROM = {
			prestige = -200
		}
	}
	
	on_success_title = {
		emf_cb_latb = yes
		pf_liege_change_under_title_begin_effect = yes
		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_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(shattered_invasion): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		FROM = {
			prestige = 100
		}
		FROM = {
			if = {
				limit = {
					has_dlc = "Legacy of Rome"
					emf_has_roman_empire = yes
					OR = {
						religion_group = christian
						religion = hellenic_pagan
						religion = hellenic_pagan_reformed
					}
				}
				hidden_tooltip = { character_event = { id = LoR.30 days = 12 } }
			}
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
		ROOT = {
			prestige = -200
		}
	}

	on_reverse_demand = {
		log = "CB(shattered_invasion): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		emf_imperial_decay_major_loss = yes
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			decadence = 10
		}
		FROM = {
			prestige = 200
			if = {
				limit = { 
					has_dlc = "Legacy of Rome"
					emf_has_roman_empire = yes
					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 }
		}
	}

	on_invalidation_title = {
		log = "CB(shattered_invasion): INVALIDATED: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

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

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

### EMF STUFF FROM HERE ###

emf_decadence_revolt = {
	name = CB_NAME_DECADENCE_UPRISING
	war_name = WAR_NAME_DECADENCE_UPRISING
	sprite = 11
	truce_days = 365
	hostile_against_others = yes
	check_all_titles = yes
	press_claim = yes
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no
	is_independence = yes

	can_use = {
		emf_cb_can_use = yes
		ROOT = { has_flag = decadence_revolter }
	}

	can_use_title = {
		tier = count
	}

	is_valid = {
		FROM = {
			religion_group = muslim
			religion_group = ROOT
		}
	}

	is_valid_title = {
		owner_under_FROM = yes
	}

	on_add = {
		log = "CB(emf_decadence_revolt): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(emf_decadence_revolt): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
	}

	on_success_title = {
		emf_cb_latb = yes

		ROOT = {
			hidden_tooltip = {
				if = {
					limit = { dynasty = none }
					dynasty = father_bastard
				}
			}
		}

		usurp_title_plus_barony_if_unlanded = {
			target = ROOT
			type = revolt
		}

		ROOT = {
			prestige = 200
			piety = 100
			occupy_minors_of_occupied_settlements = FROM
			gain_all_occupied_titles = { who = FROM type = revolt }
			set_defacto_liege = ROOT
			clr_flag = decadence_revolter
		}

		FROM = {
			imprison = ROOT
			prestige = -500
			piety = -500
			clr_flag = has_current_revolt
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(emf_decadence_revolt): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"

		ROOT = {
			clear_wealth = yes
			imprison = FROM
			clr_flag = decadence_revolter
			hidden_tooltip = {
				add_character_modifier = {
					name = broken_spirit
					days = 3650
				}
			}
		}
		FROM = {
			prestige = 25
			clr_flag = has_current_revolt
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor
					who = ROOT
					months = 1200
				}
			}
		}
	}

	on_reverse_demand = {
		log = "CB(emf_decadence_revolt): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"

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

	on_invalidation = {
		log = "CB(emf_decadence_revolt): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

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

emf_dejure_duchy_claim = {
	name = CB_NAME_EMF_DEJURE_DUCHY_CLAIM
	war_name = WAR_NAME_EMF_DEJURE_DUCHY_CLAIM
	sprite = 16
	truce_days = 1825
	hostile_against_others = yes
	is_permanent = yes
	check_de_jure_tier = DUKE
	can_ask_to_join_war = no
	allowed_to_target_tributaries = no
	infamy_modifier = 0.5

	sort_priority = 1500

	can_use_gui = {
		emf_cb_can_use_gui = yes
		FROM = {
			OR = {
				is_merchant_republic = no
				NOT = { same_realm = ROOT }
			}
			OR = {
				NOT = { has_dlc = "Sons of Abraham" }
				holy_order = no
				NOT = { religion = ROOT }
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			higher_real_tier_than = DUKE
			NOT = { is_liege_or_above = FROM }
			OR = {
				emf_unreformed_pagan = no
				ai = no
				NOT = { religion = FROM }
			}
		}
	}

	can_use_title = {
		emf_cb_can_use_de_jure_title = yes
		emf_cb_dejure_duchy_claim_basic_can_use_title = yes
		# Don't show this when the [rare] emf_dejure_kingdom_claim could be used
		NOT = { kingdom = { emf_cb_dejure_kingdom_claim_basic_can_use_title = yes } }
	}

	is_valid_title = {
		any_de_jure_vassal_title = { owner_under_FROM = yes }
		OR = {
			any_de_jure_liege_title = { holder = ROOT }
			AND = {
				ROOT = { has_landed_title = k_france }
				de_jure_liege_or_above = k_aquitaine
				k_aquitaine = { has_holder = no }
				NOT = { de_jure_liege_or_above = d_barcelona }
			}
		}
	}

	on_add_title = {
		log = "CB(emf_dejure_duchy_claim): START: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	on_success = { emf_cb_victory = yes }

	on_success_title = {
		log = "CB(emf_dejure_duchy_claim): VICTORY: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_latb = yes

		if = {
			limit = { empire = { holder = ROOT } }
			emf_imperial_decay_victory = yes
		}

		FROM = { prestige = -150 }
		pf_liege_change_under_title_begin_effect = yes
		ROOT = {
			vassalize_or_take_under_title = {
				title = PREV
				enemy = FROM
			}
		}
		pf_liege_change_under_title_end_effect = yes
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 150
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 150 }
		}
		hidden_tooltip = { check_holy_order_donation_effect = yes }
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

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

	on_fail_title = {
		log = "CB(emf_dejure_duchy_claim): WHITEPEACE: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	on_reverse_demand = {
		emf_cb_defeat = yes
		ROOT = {
			prestige = -300
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 150
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 150 }
		}
	}

	on_reverse_demand_title = {
		log = "CB(emf_dejure_duchy_claim): DEFEAT: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"

		if = {
			limit = { empire = { holder = ROOT } }
			emf_imperial_decay_loss = yes
		}
	}

	on_invalidation = {
		log = "CB(emf_dejure_duchy_claim): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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 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 = 1.1
			always = yes
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

emf_dejure_kingdom_claim = {
	name = CB_NAME_EMF_DEJURE_KINGDOM_CLAIM
	war_name = WAR_NAME_EMF_DEJURE_KINGDOM_CLAIM
	sprite = 16
	truce_days = 1825
	hostile_against_others = yes
	is_permanent = yes
	check_de_jure_tier = KING
	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no
	infamy_modifier = 0.5

	sort_priority = 1800

	can_use_gui = {
		emf_cb_can_use_gui = yes
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			tier = EMPEROR
			NOT = { is_liege_or_above = FROM }
		}
	}

	can_use_title = {
		emf_cb_dejure_kingdom_claim_basic_can_use_title = yes
		emf_cb_can_use_de_jure_title = yes
	}

	is_valid_title = {
		FROM = { any_realm_province = { de_jure_liege_or_above = PREVPREV } }
		empire = { holder = ROOT }
	}

	on_add_title = {
		log = "CB(emf_dejure_kingdom_claim): START: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	on_success = { emf_cb_victory = yes }

	on_success_title = {
		log = "CB(emf_dejure_kingdom_claim): VICTORY: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_latb = yes

		if = {
			limit = { empire = { holder = ROOT } }
			emf_imperial_decay_major_victory = yes
		}

		FROM = { prestige = -200 }
		pf_liege_change_under_title_begin_effect = yes
		ROOT = {
			vassalize_or_take_under_title = {
				title = PREV
				enemy = FROM
			}
		}
		pf_liege_change_under_title_end_effect = yes
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		hidden_tooltip = { check_holy_order_donation_effect = yes }
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail_title = {
		log = "CB(emf_dejure_kingdom_claim): WHITEPEACE: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	on_fail = {
		ROOT = { prestige = -200 }
		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 = {
		log = "CB(emf_dejure_kingdom_claim): DEFEAT: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"

		if = {
			limit = { empire = { holder = ROOT } }
			emf_imperial_decay_major_loss = yes
		}
	}

	on_reverse_demand = {
		emf_cb_defeat = yes
		ROOT = {
			prestige = -400
			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 }
		}
	}

	on_invalidation = {
		log = "CB(emf_dejure_kingdom_claim): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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 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 = 1.2
			always = yes
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

emf_druze_invasion = {
	name = CB_NAME_VIKING_INVASION
	war_name = WAR_NAME_VIKING_INVASION
	sprite = 8
	truce_days = 365
	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
	apply_short_occ_mod = no # Do not apply the 'recently_conquered' modifier to Holdings

	allowed_to_target_tributaries = no

	sort_priority = 1800

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			OR = {
				trait = druze_ajawid
				piety = 1000
				AND = {
					martial = 15
					diplomacy = 15
				}
			}
			conditional_tooltip = {
				trigger = { is_patrician = yes }
				custom_tooltip = {
					text = emf_mr_ctt_extended_cbs_unlocked
					has_game_rule = { name = mr_cb_restriction value = off }
				}
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		FROM = {
			religion_group = muslim
			NOT = { religion = ROOT }
		}
		ROOT = {
			religion = druze
			NOT = { is_liege_or_above = FROM }
			NOT = { realm_size = 41 }
			mercenary = no
		}
	}

	can_use_title = {
		emf_cb_can_use_de_jure_title = yes
		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 = {
		log = "CB(emf_druze_invasion): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(emf_druze_invasion): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		emf_imperial_decay_victory = yes
		ROOT = {
			prestige = 500
			piety = 250
			religion_authority = { modifier = won_viking_invasion }
			hidden_tooltip = {
				set_global_flag = emf_latb_upon_gain
				occupy_minors_of_occupied_settlements = FROM
				gain_all_occupied_titles = {
					who = FROM
					type = invasion
				}
				clr_global_flag = emf_latb_upon_gain
			}
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 300
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 300 }
		}
		FROM = { prestige = -500 }
	}

	on_success_title = {
		emf_cb_latb = yes
		custom_tooltip = {
			text = emf_cb_ctt_viking_invasion_success
			hidden_tooltip = {
				save_event_target_as = emf_cb_title
				pf_liege_change_under_title_begin_effect = yes
				ROOT = {
					vassalize_or_take_under_title_destroy_duchies = {
						title = PREV
						enemy = FROM
						type = invasion
					}
				}
				pf_liege_change_under_title_end_effect = yes
				ROOT = {
					random_demesne_province = {
						limit = {
							de_jure_liege_or_above = PREVPREV
							OR = {
								NOT = { culture = ROOT }
								NOT = { religion = ROOT }
							}
						}
						culture = ROOT
						religion = ROOT
					}
					# Remove conquest modifiers and refill levies
					character_event = { id = emf_cb.10 }
				}
				if = {
					limit = { FROM = { is_liege_or_above = ROOT } }
					FROM = { imprison = ROOT }
				}
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(emf_druze_invasion): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		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
			religion_authority = { modifier = lost_viking_invasion }
		}
	}

	on_reverse_demand = {
		log = "CB(emf_druze_invasion): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		emf_imperial_decay_loss = yes
		ROOT = {
			prestige = -500
			piety = -250
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
			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 }
		}
	}

	on_invalidation = {
		log = "CB(viking_invasion): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

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

emf_faction_gender_succ_agnatic = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_GENDER_SUCC_AGNATIC
	sprite = 6
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		emf_cb_faction_can_use = yes
	}

	can_use_title = {
		holder = FROM
		succ_law_title = { title = PREV }
		NOT = { has_law = agnatic_succession }
		is_law_potential = agnatic_succession
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
	}

	is_valid_title = {
		holder = FROM
		succ_law_title = { title = PREV }
		NOT = { has_law = agnatic_succession }
		is_law_potential = agnatic_succession
	}

	on_add = {
		log = "CB(emf_faction_gender_succ_agnatic): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_gender_succ_agnatic): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_success_start = yes
	}

	on_success_title = {
		holder_scope = {
			set_flag = emf_no_law_penalties
		}
		add_law_w_cooldown = agnatic_succession
		holder_scope = {
			clr_flag = emf_no_law_penalties
			recalc_succession = yes
		}
	}

	on_success_posttitle = {
		emf_cb_faction_on_success_end = yes
		emf_cb_on_success_posttitle = yes
	}
	on_fail = {
		log = "CB(emf_faction_gender_succ_agnatic): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_gender_succ_agnatic): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_gender_succ_agnatic): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_faction_gender_succ_cognatic = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_GENDER_SUCC_COGNATIC
	sprite = 6
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		emf_cb_faction_can_use = yes
	}

	can_use_title = {
		holder = FROM
		succ_law_title = { title = PREV }
		NOT = { has_law = cognatic_succession }
		is_law_potential = cognatic_succession
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
	}

	is_valid_title = {
		holder = FROM
		succ_law_title = { title = PREV }
		NOT = { has_law = cognatic_succession }
		is_law_potential = cognatic_succession
	}

	on_add = {
		log = "CB(emf_faction_gender_succ_cognatic): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_gender_succ_cognatic): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_success_start = yes
	}

	on_success_title = {
		holder_scope = {
			set_flag = emf_no_law_penalties
		}
		add_law_w_cooldown = cognatic_succession
		holder_scope = {
			clr_flag = emf_no_law_penalties
			recalc_succession = yes
		}
	}

	on_success_posttitle = {
		emf_cb_faction_on_success_end = yes
		emf_cb_on_success_posttitle = yes
	}
	on_fail = {
		log = "CB(emf_faction_gender_succ_cognatic): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_gender_succ_cognatic): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_gender_succ_cognatic): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_faction_gender_succ_true_cognatic = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_GENDER_SUCC_TRUE_COGNATIC
	sprite = 6
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		emf_cb_faction_can_use = yes
	}

	can_use_title = {
		holder = FROM
		succ_law_title = { title = PREV }
		NOT = { has_law = true_cognatic_succession }
		is_law_potential = true_cognatic_succession
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
	}

	is_valid_title = {
		holder = FROM
		succ_law_title = { title = PREV }
		NOT = { has_law = true_cognatic_succession }
		is_law_potential = true_cognatic_succession
	}

	on_add = {
		log = "CB(emf_faction_gender_succ_true_cognatic): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_gender_succ_true_cognatic): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_success_start = yes
	}

	on_success_title = {
		holder_scope = {
			set_flag = emf_no_law_penalties
		}
		add_law_w_cooldown = true_cognatic_succession
		holder_scope = {
			clr_flag = emf_no_law_penalties
			recalc_succession = yes
		}
	}

	on_success_posttitle = {
		emf_cb_faction_on_success_end = yes
		emf_cb_on_success_posttitle = yes
	}
	on_fail = {
		log = "CB(emf_faction_gender_succ_true_cognatic): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_gender_succ_true_cognatic): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_gender_succ_true_cognatic): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_faction_gender_succ_enatic_cognatic = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_GENDER_SUCC_ENATIC_COGNATIC
	sprite = 6
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		emf_cb_faction_can_use = yes
	}

	can_use_title = {
		holder = FROM
		succ_law_title = { title = PREV }
		NOT = { has_law = enatic_cognatic_succession }
		is_law_potential = enatic_cognatic_succession
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
	}

	is_valid_title = {
		holder = FROM
		succ_law_title = { title = PREV }
		NOT = { has_law = enatic_cognatic_succession }
		is_law_potential = enatic_cognatic_succession
	}

	on_add = {
		log = "CB(emf_faction_gender_succ_enatic_cognatic): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_gender_succ_enatic_cognatic): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_success_start = yes
	}

	on_success_title = {
		holder_scope = {
			set_flag = emf_no_law_penalties
		}
		add_law_w_cooldown = enatic_cognatic_succession
		holder_scope = {
			clr_flag = emf_no_law_penalties
			recalc_succession = yes
		}
	}

	on_success_posttitle = {
		emf_cb_faction_on_success_end = yes
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_faction_gender_succ_enatic_cognatic): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_gender_succ_enatic_cognatic): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_gender_succ_enatic_cognatic): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_faction_gender_succ_enatic = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_GENDER_SUCC_ENATIC
	sprite = 6
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		emf_cb_faction_can_use = yes
	}

	can_use_title = {
		holder = FROM
		succ_law_title = { title = PREV }
		NOT = { has_law = enatic_succession }
		is_law_potential = enatic_succession
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
	}

	is_valid_title = {
		holder = FROM
		succ_law_title = { title = PREV }
		NOT = { has_law = enatic_succession }
		is_law_potential = enatic_succession
	}

	on_add = {
		log = "CB(emf_faction_gender_succ_enatic): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_gender_succ_enatic): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_success_start = yes
	}

	on_success_title = {
		holder_scope = {
			set_flag = emf_no_law_penalties
		}
		add_law_w_cooldown = enatic_succession
		holder_scope = {
			clr_flag = emf_no_law_penalties
			recalc_succession = yes
		}
	}

	on_success_posttitle = {
		emf_cb_faction_on_success_end = yes
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_faction_gender_succ_enatic): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_gender_succ_enatic): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_gender_succ_enatic): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_faction_overthrow = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_OVERTHROW
	sprite = 12
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		emf_cb_faction_can_use = yes
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
	}

	on_add = {
		log = "CB(emf_faction_overthrow): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_overthrow): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_success_start = yes
		emf_cb_faction_on_success_end = yes
	}

	on_success_posttitle = {
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_faction_overthrow): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_overthrow): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_overthrow): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_faction_overthrow_CA = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_OVERTHROW_CA
	sprite = 12
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		emf_cb_faction_can_use = yes
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
		FROM = {
			any_demesne_title = {
				higher_tier_than = DUKE
				crownlaw_title = { title = PREV }
				emf_laws_has_limited_ca_plus = yes
			}
		}
	}

	on_add = {
		log = "CB(emf_faction_overthrow_CA): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_overthrow_CA): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		FROM = { set_flag = emf_always_reduce_CA_tmp }
		emf_cb_faction_on_success_start = yes
		FROM = { clr_flag = emf_always_reduce_CA_tmp }
		emf_cb_faction_on_success_end = yes
	}

	on_success_posttitle = {
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_faction_overthrow_CA): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_overthrow_CA): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_overthrow_CA): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_faction_overthrow_obligations = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_OVERTHROW_OBLIGATIONS
	sprite = 12
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		emf_cb_faction_can_use = yes
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
	}

	on_add = {
		log = "CB(emf_faction_overthrow_obligations): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_overthrow_obligations): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_success_start = yes
		emf_cb_faction_on_success_end = yes
	}

	on_success_posttitle = {
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_faction_overthrow_obligations): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_overthrow_obligations): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_overthrow_obligations): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_faction_overthrow_TO = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_OVERTHROW_TO
	sprite = 12
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		emf_cb_faction_can_use = yes
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
	}

	on_add = {
		log = "CB(emf_faction_overthrow_TO): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_overthrow_TO): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_success_start = yes
		emf_cb_faction_on_success_end = yes
	}

	on_success_posttitle = {
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_faction_overthrow_TO): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_overthrow_TO): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_overthrow_TO): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_faction_succ_aztec_elective = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_SUCC_AZTEC_ELECTIVE
	sprite = 6
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		emf_cb_faction_can_use = yes
	}

	can_use_title = {
		holder = FROM
		NOT = { has_law = succ_aztec_elective }
		is_law_potential = succ_aztec_elective
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
	}

	is_valid_title = {
		holder = FROM
		NOT = { has_law = succ_aztec_elective }
		is_law_potential = succ_aztec_elective
	}

	on_add = {
		log = "CB(emf_faction_succ_aztec_elective): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_succ_aztec_elective): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_success_start = yes
	}

	on_success_title = {
		holder_scope = {
			set_flag = emf_no_law_penalties
		}
		add_law_w_cooldown = succ_aztec_elective
		holder_scope = {
			clr_flag = emf_no_law_penalties
			recalc_succession = yes
		}
	}

	on_success_posttitle = {
		emf_cb_faction_on_success_end = yes
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_faction_succ_aztec_elective): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_succ_aztec_elective): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_succ_aztec_elective): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_faction_succ_byzantine_elective = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_SUCC_BYZANTINE_ELECTIVE
	sprite = 6
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		emf_cb_faction_can_use = yes
	}

	can_use_title = {
		holder = FROM
		NOT = { has_law = succ_byzantine_elective }
		is_law_potential = succ_byzantine_elective
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
	}

	is_valid_title = {
		holder = FROM
		NOT = { has_law = succ_byzantine_elective }
		is_law_potential = succ_byzantine_elective
	}

	on_add = {
		log = "CB(emf_faction_succ_byzantine_elective): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_succ_byzantine_elective): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_success_start = yes
	}

	on_success_title = {
		holder_scope = {
			set_flag = emf_no_law_penalties
		}
		add_law_w_cooldown = succ_byzantine_elective
		holder_scope = {
			clr_flag = emf_no_law_penalties
			recalc_succession = yes
		}
	}

	on_success_posttitle = {
		emf_cb_faction_on_success_end = yes
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_faction_succ_byzantine_elective): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_succ_byzantine_elective): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_succ_byzantine_elective): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_faction_succ_dynastic_elective = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_SUCC_DYNASTIC_ELECTIVE
	sprite = 6
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		emf_cb_faction_can_use = yes
	}

	can_use_title = {
		holder = FROM
		NOT = { has_law = succ_dynastic_elective }
		is_law_potential = succ_dynastic_elective
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
	}

	is_valid_title = {
		holder = FROM
		NOT = { has_law = succ_dynastic_elective }
		is_law_potential = succ_dynastic_elective
	}

	on_add = {
		log = "CB(emf_faction_succ_dynastic_elective): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_succ_dynastic_elective): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_success_start = yes
	}

	on_success_title = {
		holder_scope = {
			set_flag = emf_no_law_penalties
		}
		add_law_w_cooldown = succ_dynastic_elective
		holder_scope = {
			clr_flag = emf_no_law_penalties
			recalc_succession = yes
		}
	}

	on_success_posttitle = {
		emf_cb_faction_on_success_end = yes
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_faction_succ_dynastic_elective): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_succ_dynastic_elective): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_succ_dynastic_elective): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_faction_succ_eldership = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_SUCC_ELDERSHIP
	sprite = 6
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		emf_cb_faction_can_use = yes
	}

	can_use_title = {
		holder = FROM
		NOT = { has_law = succ_eldership }
		is_law_potential = succ_eldership
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
	}

	is_valid_title = {
		holder = FROM
		NOT = { has_law = succ_eldership }
		is_law_potential = succ_eldership
	}

	on_add = {
		log = "CB(emf_faction_succ_eldership): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_succ_eldership): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_success_start = yes
	}

	on_success_title = {
		holder_scope = {
			set_flag = emf_no_law_penalties
		}
		add_law_w_cooldown = succ_eldership
		holder_scope = {
			clr_flag = emf_no_law_penalties
			recalc_succession = yes
		}
	}

	on_success_posttitle = {
		emf_cb_faction_on_success_end = yes
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_faction_succ_eldership): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_succ_eldership): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_succ_eldership): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_faction_succ_elective_gavelkind = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_SUCC_ELECTIVE_GAVELKIND
	sprite = 6
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		emf_cb_faction_can_use = yes
	}

	can_use_title = {
		holder = FROM
		NOT = { has_law = succ_elective_gavelkind }
		is_law_potential = succ_elective_gavelkind
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
	}

	is_valid_title = {
		holder = FROM
		NOT = { has_law = succ_elective_gavelkind }
		is_law_potential = succ_elective_gavelkind
	}

	on_add = {
		log = "CB(emf_faction_succ_elective_gavelkind): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_succ_elective_gavelkind): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_success_start = yes
	}

	on_success_title = {
		holder_scope = {
			set_flag = emf_no_law_penalties
		}
		add_law_w_cooldown = succ_elective_gavelkind
		holder_scope = {
			clr_flag = emf_no_law_penalties
			recalc_succession = yes
		}
	}

	on_success_posttitle = {
		emf_cb_faction_on_success_end = yes
		emf_cb_on_success_posttitle = yes
	}
	on_fail = {
		log = "CB(emf_faction_succ_elective_gavelkind): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_succ_elective_gavelkind): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_succ_elective_gavelkind): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_faction_succ_feudal_elective = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_SUCC_FEUDAL_ELECTIVE
	sprite = 6
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		emf_cb_faction_can_use = yes
	}

	can_use_title = {
		holder = FROM
		NOT = { has_law = succ_feudal_elective }
		is_law_potential = succ_feudal_elective
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
	}

	is_valid_title = {
		holder = FROM
		NOT = { has_law = succ_feudal_elective }
		is_law_potential = succ_feudal_elective
	}

	on_add = {
		log = "CB(emf_faction_succ_feudal_elective): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_succ_feudal_elective): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_success_start = yes
	}

	on_success_title = {
		holder_scope = {
			set_flag = emf_no_law_penalties
		}
		add_law_w_cooldown = succ_feudal_elective
		holder_scope = {
			clr_flag = emf_no_law_penalties
			recalc_succession = yes
		}
	}

	on_success_posttitle = {
		emf_cb_faction_on_success_end = yes
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_faction_succ_feudal_elective): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_succ_feudal_elective): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_succ_feudal_elective): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_faction_succ_gavelkind = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_SUCC_GAVELKIND
	sprite = 6
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		emf_cb_faction_can_use = yes
	}

	can_use_title = {
		holder = FROM
		NOT = { has_law = succ_gavelkind }
		is_law_potential = succ_gavelkind
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
	}

	is_valid_title = {
		holder = FROM
		NOT = { has_law = succ_gavelkind }
		is_law_potential = succ_gavelkind
	}

	on_add = {
		log = "CB(emf_faction_succ_gavelkind): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_succ_gavelkind): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_success_start = yes
	}

	on_success_title = {
		holder_scope = {
			set_flag = emf_no_law_penalties
		}
		add_law_w_cooldown = succ_gavelkind
		holder_scope = {
			clr_flag = emf_no_law_penalties
			recalc_succession = yes
		}
	}

	on_success_posttitle = {
		emf_cb_faction_on_success_end = yes
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_faction_succ_gavelkind): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_succ_gavelkind): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_succ_gavelkind): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_faction_succ_hre_elective = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_SUCC_HRE_ELECTIVE
	sprite = 6
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		emf_cb_faction_can_use = yes
	}

	can_use_title = {
		holder = FROM
		NOT = { has_law = succ_hre_elective }
		is_law_potential = succ_hre_elective
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
	}

	is_valid_title = {
		holder = FROM
		NOT = { has_law = succ_hre_elective }
		is_law_potential = succ_hre_elective
	}

	on_add = {
		log = "CB(emf_faction_succ_hre_elective): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_succ_hre_elective): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_success_start = yes
	}

	on_success_title = {
		holder_scope = {
			set_flag = emf_no_law_penalties
		}
		add_law_w_cooldown = succ_hre_elective
		holder_scope = {
			clr_flag = emf_no_law_penalties
			recalc_succession = yes
		}
	}

	on_success_posttitle = {
		emf_cb_faction_on_success_end = yes
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_faction_succ_hre_elective): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_succ_hre_elective): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_succ_hre_elective): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_faction_succ_mirath = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_SUCC_MIRATH
	sprite = 6
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		always = no
		#emf_cb_faction_can_use = yes
	}

	can_use_title = {
		holder = FROM
		#NOT = { has_law = succ_mirath }
		#is_law_potential = succ_mirath
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
	}

	is_valid_title = {
		holder = FROM
		#NOT = { has_law = succ_mirath }
		#is_law_potential = succ_mirath
	}

	on_add = {
		log = "CB(emf_faction_succ_mirath): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_succ_mirath): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_success_start = yes
	}

	on_success_title = {
		#holder_scope = {
		#	set_flag = emf_no_law_penalties
		#}
		#add_law_w_cooldown = succ_mirath
		#holder_scope = {
		#	clr_flag = emf_no_law_penalties
		#	recalc_succession = yes
		#}
	}

	on_success_posttitle = {
		emf_cb_faction_on_success_end = yes
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_faction_succ_mirath): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_succ_mirath): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_succ_mirath): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_faction_succ_pictish_elective = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_succ_pictish_elective
	sprite = 6
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		emf_cb_faction_can_use = yes
	}

	can_use_title = {
		holder = FROM
		NOT = { has_law = succ_pictish_elective }
		is_law_potential = succ_pictish_elective
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
	}

	is_valid_title = {
		holder = FROM
		NOT = { has_law = succ_pictish_elective }
		is_law_potential = succ_pictish_elective
	}

	on_add = {
		log = "CB(emf_faction_succ_pictish_elective): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_succ_pictish_elective): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_success_start = yes
	}

	on_success_title = {
		holder_scope = {
			set_flag = emf_no_law_penalties
		}
		add_law_w_cooldown = succ_pictish_elective
		holder_scope = {
			clr_flag = emf_no_law_penalties
			recalc_succession = yes
		}
	}

	on_success_posttitle = {
		emf_cb_faction_on_success_end = yes
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_faction_succ_pictish_elective): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_succ_pictish_elective): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_succ_pictish_elective): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_faction_succ_primogeniture = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_SUCC_PRIMOGENITURE
	sprite = 6
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		emf_cb_faction_can_use = yes
	}

	can_use_title = {
		holder = FROM
		NOT = { has_law = succ_primogeniture }
		is_law_potential = succ_primogeniture
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
	}

	is_valid_title = {
		holder = FROM
		NOT = { has_law = succ_primogeniture }
		is_law_potential = succ_primogeniture
	}

	on_add = {
		log = "CB(emf_faction_succ_primogeniture): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_succ_primogeniture): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_success_start = yes
	}

	on_success_title = {
		holder_scope = {
			set_flag = emf_no_law_penalties
		}
		add_law_w_cooldown = succ_primogeniture
		holder_scope = {
			clr_flag = emf_no_law_penalties
			recalc_succession = yes
		}
	}

	on_success_posttitle = {
		emf_cb_faction_on_success_end = yes
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_faction_succ_primogeniture): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_succ_primogeniture): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_succ_primogeniture): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_faction_succ_seniority = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_SUCC_SENIORITY
	sprite = 6
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		emf_cb_faction_can_use = yes
	}

	can_use_title = {
		holder = FROM
		NOT = { has_law = succ_seniority }
		is_law_potential = succ_seniority
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
	}

	is_valid_title = {
		holder = FROM
		NOT = { has_law = succ_seniority }
		is_law_potential = succ_seniority
	}

	on_add = {
		log = "CB(emf_faction_succ_seniority): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_succ_seniority): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_success_start = yes
	}

	on_success_title = {
		holder_scope = {
			set_flag = emf_no_law_penalties
		}
		add_law_w_cooldown = succ_seniority
		holder_scope = {
			clr_flag = emf_no_law_penalties
			recalc_succession = yes
		}
	}

	on_success_posttitle = {
		emf_cb_faction_on_success_end = yes
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_faction_succ_seniority): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_succ_seniority): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_succ_seniority): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_faction_succ_tanistry = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_SUCC_TANISTRY
	sprite = 6
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		emf_cb_faction_can_use = yes
	}

	can_use_title = {
		holder = FROM
		NOT = { has_law = succ_tanistry }
		is_law_potential = succ_tanistry
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
	}

	is_valid_title = {
		holder = FROM
		NOT = { has_law = succ_tanistry }
		is_law_potential = succ_tanistry
	}

	on_add = {
		log = "CB(emf_faction_succ_tanistry): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_succ_tanistry): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_success_start = yes
	}

	on_success_title = {
		holder_scope = {
			set_flag = emf_no_law_penalties
		}
		add_law_w_cooldown = succ_tanistry
		holder_scope = {
			clr_flag = emf_no_law_penalties
			recalc_succession = yes
		}
	}

	on_success_posttitle = {
		emf_cb_faction_on_success_end = yes
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_faction_succ_tanistry): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_succ_tanistry): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_succ_tanistry): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_faction_succ_turkish_succession = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_SUCC_TURKISH_SUCCESSION
	sprite = 6
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		emf_cb_faction_can_use = yes
	}

	can_use_title = {
		holder = FROM
		NOT = { has_law = succ_turkish_succession }
		is_law_potential = succ_turkish_succession
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
	}

	is_valid_title = {
		holder = FROM
		NOT = { has_law = succ_turkish_succession }
		is_law_potential = succ_turkish_succession
	}

	on_add = {
		log = "CB(emf_faction_succ_turkish_succession): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_succ_turkish_succession): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_success_start = yes
	}

	on_success_title = {
		holder_scope = {
			set_flag = emf_no_law_penalties
		}
		add_law_w_cooldown = succ_turkish_succession
		holder_scope = {
			clr_flag = emf_no_law_penalties
			recalc_succession = yes
		}
	}

	on_success_posttitle = {
		emf_cb_faction_on_success_end = yes
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_faction_succ_turkish_succession): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_succ_turkish_succession): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_succ_turkish_succession): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_faction_succ_ultimogeniture = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_EMF_FACTION_SUCC_ULTIMOGENITURE
	sprite = 6
	truce_days = 3650
	major_revolt = yes
	capturing_attacker_is_complete_victory = no
	attacker_can_call_allies = no
	defender_can_call_allies = no
	can_ask_to_join_war = no

	can_use = {
		emf_cb_faction_can_use = yes
	}

	can_use_title = {
		holder = FROM
		NOT = { has_law = succ_ultimogeniture }
		is_law_potential = succ_ultimogeniture
	}

	is_valid = {
		emf_cb_faction_is_valid = yes
	}

	is_valid_title = {
		holder = FROM
		NOT = { has_law = succ_ultimogeniture }
		is_law_potential = succ_ultimogeniture
	}

	on_add = {
		log = "CB(emf_faction_succ_ultimogeniture): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_faction_on_add = yes
	}

	on_success = {
		log = "CB(emf_faction_succ_ultimogeniture): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_success_start = yes
	}

	on_success_title = {
		holder_scope = {
			set_flag = emf_no_law_penalties
		}
		add_law_w_cooldown = succ_ultimogeniture
		holder_scope = {
			clr_flag = emf_no_law_penalties
			recalc_succession = yes
		}
	}

	on_success_posttitle = {
		emf_cb_faction_on_success_end = yes
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_faction_succ_ultimogeniture): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_faction_succ_ultimogeniture): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_faction_succ_ultimogeniture): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_faction_on_invalidation = 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
	}
}

emf_mongol_invasion = {
	name = CB_NAME_INVASION
	war_name = WAR_NAME_INVASION
	sprite = 8
	truce_days = 365
	hostile_against_others = yes
	can_ask_to_join_war = yes
	check_de_jure_tier = KING
	hostages_block_cb = no
	battle_warscore_mult = 2.0 # The same effect as claimant adventure "hosts" battle warscore
	def_ticking_war_score_multiplier = 1.25 # Genghis must deliver quickly, or else

	can_use = {
		ROOT = {
			emf_mongol_invasion_mission = yes
			NOT = { same_realm = FROM }
		}
	}

	can_use_title = {
		# NOTE: Intentionally doesn't comply with `emf_cb_can_use_de_jure_title` for various reasons.
		tier = KING
		any_de_jure_vassal_title = {
			tier = COUNT
			owner_under_FROM = yes
		}
	}

	is_valid = {
		ROOT = {
			has_landed_title = e_mongol_empire
			NOT = { same_realm = FROM }
		}
	}

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

	on_add = {
		log = "CB(emf_mongol_invasion): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		ROOT = { clr_flag = emf_cb_pass_tmp }
		FROM = { emf_remove_suzerain_under_ROOT = yes }
	}

	on_success = {
		log = "CB(emf_mongol_invasion): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		emf_imperial_decay_victory = yes
		ROOT = {
			set_flag = emf_mongol_invasion_war_won
			prestige = 200
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		FROM = { prestige = -200 }
		ROOT = { emf_remove_suzerain_under_FROM = yes }
	}

	on_success_title = {
		emf_cb_latb = yes
		pf_liege_change_under_title_begin_effect = yes
		emf_cb_start_subjugation_opinions = yes
		custom_tooltip = {
			text = tribal_invasion_succ_tip
			hidden_tooltip = {
				# Outright annex all AI-owned nomadic provinces if they're in the golden horde or chagatai regions
				any_de_jure_vassal_title = {
					limit = {
						tier = COUNT
						owner = {
							ai = yes
							under_FROM = yes
						}
						location = {
							emf_nomadic_province = yes
							OR = {
								emf_golden_horde_province = yes
								emf_chagatai_province = yes
							}
						}
					}
					usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
				}
				ROOT = {
					vassalize_or_take_under_title_destroy_duchies = { title = PREV enemy = FROM type = invasion }
					# Make sure we actually get landed if we're still not and it's possible, prefer to take from AI
					if = {
						limit = { is_landed = no any_realm_title = { tier = COUNT } }
						if = {
							limit = { any_realm_title = { tier = COUNT owner = { ai = yes } } }
							random_realm_title = {
								limit = { tier = COUNT owner = { ai = yes } }
								usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
							}
						}
						else = {
							random_realm_title = {
								limit = { tier = COUNT }
								usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
							}
						}
					}
				}
			}
		}
		emf_cb_finish_subjugation_opinions = yes
		pf_liege_change_under_title_end_effect = yes
		hidden_tooltip = {
			save_event_target_as = emf_king_title
			FROM = { save_event_target_as = emf_defender }
			ROOT = { character_event = { id = emf_mongol.1013 } }
		}
	}

	on_success_posttitle = {
		hidden_tooltip = {
			FROM = {
				if = {
					limit = { ai = no }
					reverse_opinion = { who = ROOT modifier = opinion_special_truce }
				}
			}
		}
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_mongol_invasion): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		FROM = {
			if = {
				limit = { ai = no }
				set_global_flag = emf_mongol_invasion_defeat
				# TODO: prompt narrative_event announcing the unbelievable defeat of the mongol scourge
			}
			prestige = 500
			if = {
				limit = { emf_hostile_religion_to_ROOT = yes }
				piety = 200
			}
			hidden_tooltip = {
				if = {
					limit = { has_nickname = no }
					random_list = {
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_hammer }
						10 = { give_nickname = nick_the_avenger }
						10 = { give_nickname = nick_the_lion }
						10 = { give_nickname = nick_the_bold }
					}
				}
			}
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 500
			if = {
				limit = { emf_hostile_religion_to_ROOT = yes }
				participation_scaled_piety = 200
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_prestige = 500
				if = {
					limit = { emf_hostile_religion_to_ROOT = yes }
					participation_scaled_piety = 200
				}
			}
		}
		ROOT = { prestige = -500 }
	}

	on_reverse_demand = {
		log = "CB(emf_mongol_invasion): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		emf_imperial_decay_loss = yes
		set_global_flag = emf_mongol_invasion_defeat
		# TODO: prompt narrative_event announcing the unbelievable defeat of the mongol scourge
		ROOT = {
			prestige = -2000
			transfer_scaled_wealth = {
				to = FROM
				value = 10.0
				min = 1000
			}
		}
		FROM = {
			prestige = 1000
			if = {
				limit = { emf_hostile_religion_to_ROOT = yes }
				piety = 400
			}
			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"
					emf_has_roman_empire = yes
					religion_group = christian
				}
				hidden_tooltip = {
					character_event = {
						id = LoR.30
						days = 12
					}
				}
			}
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 1000
			if = {
				limit = { emf_hostile_religion_to_ROOT = yes }
				participation_scaled_piety = 400
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_prestige = 1000
				if = {
					limit = { emf_hostile_religion_to_ROOT = yes }
					participation_scaled_piety = 400
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(emf_mongol_invasion): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept (note that this differs from the 200 of most invasion CBs)
	}
	attacker_ai_defeat_worth = {
		factor = 100
	}
	defender_ai_victory_worth = {
		factor = -1 # always accept
	}
	defender_ai_defeat_worth = {
		factor = 75
		# TODO: scale this up for targets that are on the fringe of the extended mongol conquest zone.
		# IOW, split the extended conquest region into 2nd- and 3rd-tier, where the 3rd-tier distinction
		# is used to ensure that the AI will fight tooth and nail before giving-up (but 3rd-tier can be
		# targeted just the same as 2nd-tier, unlike 1st-tier which is always targeted before the others
		# and indeed is required to penetrate significantly before unlocking the others at all).
	}
	ai_will_do = {
		factor = 0
	}
}

emf_norman_county_conquest = {
	name = CB_NAME_COUNTY_CONQUEST
	war_name = WAR_NAME_COUNTY_CONQUEST
	sprite = 16
	truce_days = 1095
	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 = yes
	apply_short_occ_mod = no
	allowed_to_target_tributaries = no

	sort_priority = 1400

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			custom_tooltip = {
				text = NEEDS_100_PIETY_COST
				hidden_tooltip = {
					OR = {
						ai = yes
						piety = 100
					}
				}
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			culture = norman
			top_liege = {
				independent = yes
				culture = PREV
			}
			NOR = {
				same_realm = FROM
				culture = FROM
			}
			religion_group = christian
			mercenary = no
			capital_scope = {
				owner = { character = ROOT }
				region = emf_region_norman_sicily
			}
		}
		FROM = {
			NAND = {
				has_landed_title = e_byzantium
				any_realm_province = {
					count = 2
					region = emf_region_norman_sicily
					any_neighbor_province = { owner_under_ROOT = yes }
				}
			}
		}
		has_random_culture_names = no
	}

	can_use_title = {
		emf_cb_county_conquest_basic_can_use_title = yes
		location = {
			region = emf_region_norman_sicily
			any_neighbor_province = {
				OR = {
					owner_under_ROOT = yes
					AND = {
						is_land = no
						any_neighbor_province = {
							owner_under_ROOT = yes
							has_flag = emf_capital_connected
						}
					}
				}
			}
		}
		emf_cb_can_use_title = yes
	}

	is_valid = {
		ROOT = {
			culture = norman
			top_liege = {
				independent = yes
				culture = PREV
			}
			NOR = {
				same_realm = FROM
				culture = FROM
			}
			religion_group = christian
			mercenary = no
		}
	}

	is_valid_title = {
		owner_under_FROM = yes
	}

	on_add = {
		log = "CB(emf_norman_county_conquest): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		ROOT = {
			if = {
				limit = { ai = no }
				piety = -100
			}
		}
	}

	on_success = {
		log = "CB(emf_norman_county_conquest): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
	}

	on_success_title = {
		emf_cb_latb = yes
		any_de_jure_vassal_title = {
			limit = { owner_under_FROM = yes }
			usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
		}
		usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
			if = {
				limit = { emf_hostile_religion_to_FROM = yes }
				participation_scaled_piety = 50
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = 100
				if = {
					limit = { emf_hostile_religion_to_FROM = yes }
					participation_scaled_piety = 50
				}
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(emf_norman_county_conquest): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = -100 }
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 50
			if = {
				limit = { emf_hostile_religion_to_ROOT = yes }
				participation_scaled_piety = 25
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_prestige = 50
				if = {
					limit = { emf_hostile_religion_to_ROOT = yes }
					participation_scaled_piety = 25
				}
			}
		}
	}

	on_reverse_demand = {
		log = "CB(emf_norman_county_conquest): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
			if = {
				limit = { emf_hostile_religion_to_ROOT = yes }
				participation_scaled_piety = 50
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_prestige = 100
				if = {
					limit = { emf_hostile_religion_to_ROOT = yes }
					participation_scaled_piety = 50
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(emf_norman_county_conquest): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			if = { # refund
				limit = { ai = no }
				piety = 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
		additive_modifier = {
			value = -10
			FROM = { has_landed_title = e_byzantium }
		}
		modifier = {
			factor = 0.8
			ROOT = { ai = yes }
		}
	}

	# 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 = 2
		modifier = {
			factor = 2.1
			ROOT = {
				OR = {
					religion_group = FROM
					emf_syncretized_religion_with_FROM = yes
				}
			}
		}
		modifier = {
			factor = 2
			location = { any_neighbor_province = { owner_under_ROOT = yes } }
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

emf_ismaili_caliphate_rising = {
	name = CB_NAME_ISMAILI_CALIPHATE_RISING
	war_name = WAR_NAME_ISMAILI_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. Only valid if is_permanent = yes
	apply_short_occ_mod = no # Do not apply the 'recently_conquered' modifier to Holdings
	battle_warscore_mult = 1.5

	can_use = {
		emf_cb_can_use = yes
		ROOT = { has_flag = ismaili_caliphate_pretender }
	}

	is_valid = {
		ROOT = { has_flag = ismaili_caliphate_pretender }
		d_ismaili = { has_holder = no }
	}

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

	on_add = {
		log = "CB(emf_ismaili_caliphate_rising): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(emf_ismaili_caliphate_rising): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		clr_global_flag = ismaili_caliphate_revolt_ongoing
		emf_cb_holy_victory = yes
		emf_imperial_decay_major_victory = yes
		set_parent_religion = { religion = ismaili parent = 0 }
		ROOT = {
			piety = 500
			religion_authority = { modifier = rise_of_the_ismaili_caliphate }
			clr_flag = ismaili_caliphate_pretender
		}
		FROM = {
			piety = -500
			religion_authority = { modifier = ismaili_victorious1 }
		}
		hidden_effect = {
			set_parent_religion = { religion = qarmatian parent = ismaili }
			set_global_flag = qarmatian_activated
		}
	}

	on_success_title = {
		hidden_effect = {
			save_event_target_as = conquered_title
			ROOT = { narrative_event = { id = emf_heresy.1977 } }
		}
		emf_cb_latb = yes
		custom_tooltip = {
			text = other_invasion_succ_tip
			hidden_tooltip = {
				d_ismaili = {
					usurp_title = { target = ROOT type = holy_war }
				}
				FROM = {
					random_realm_province = {
						limit = {
							owner_under_PREV = yes
							de_jure_liege_or_above = PREVPREV
							NOT = { religion = ROOT }
							religion_group = muslim
						}
						religion = ROOT
					}
					random_realm_province = {
						limit = {
							owner_under_PREV = yes
							de_jure_liege_or_above = PREVPREV
							any_neighbor_province = { religion = ROOT }
							NOT = { religion = ROOT }
							religion_group = muslim
						}
						religion = ROOT
					}
					random_realm_province = {
						limit = {
							owner_under_PREV = yes
							de_jure_liege_or_above = PREVPREV
							any_neighbor_province = { religion = ROOT }
							NOT = { religion = ROOT }
							religion_group = muslim
						}
						religion = ROOT
					}
					random_realm_province = {
						limit = {
							owner_under_PREV = yes
							de_jure_liege_or_above = PREVPREV
							any_neighbor_province = { religion = ROOT }
							NOT = { religion = ROOT }
							religion_group = muslim
						}
						religion = ROOT
					}
					random_realm_province = {
						limit = {
							owner_under_PREV = yes
							de_jure_liege_or_above = PREVPREV
							any_neighbor_province = { religion = ROOT }
							NOT = { religion = ROOT }
							religion_group = muslim
						}
						religion = ROOT
					}
				}
				pf_liege_change_under_title_begin_effect = yes
				ROOT = {
					remove_trait = imamah_claimant
					clr_flag = is_imamah_claimant
					set_global_flag = emf_latb_upon_gain
					occupy_minors_of_occupied_settlements = FROM
					gain_all_occupied_titles = { who = FROM type = revolt }
					clr_global_flag = emf_latb_upon_gain
					vassalize_or_take_under_title_destroy_duchies = {
						title = PREV
						enemy = FROM
						is_religious = yes
						same_religion = yes
						type = revolt
					}
					character_event = { id = emf_cb.10 days = 1 }
					usurp_title = { target = PREV type = revolt }
				}
				pf_liege_change_under_title_end_effect = yes
				if = {
					limit = { FROM = { is_liege_or_above = ROOT } }
					FROM = { imprison = ROOT }
				}
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(emf_ismaili_caliphate_rising): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		clr_global_flag = ismaili_caliphate_revolt_ongoing
		FROM = { piety = 250 }
		ROOT = {
			piety = -250
			clr_flag = ismaili_caliphate_pretender
		}
	}

	on_reverse_demand = {
		log = "CB(emf_ismaili_caliphate_rising): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		clr_global_flag = ismaili_caliphate_revolt_ongoing
		emf_cb_holy_defeat = yes
		emf_imperial_decay_major_loss = yes
		ROOT = {
			piety = -500
			clr_flag = ismaili_caliphate_pretender
			imprison = FROM
		}
		FROM = { piety = 250 }
		any_defender = {
			limit = { character = FROM }
			participation_scaled_piety = 250
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_piety = 250 }
		}
	}

	on_attacker_leader_death = {
		clr_global_flag = ismaili_caliphate_revolt_ongoing
		clr_global_flag = ismaili_caliphate_revolt
		ROOT = { clr_flag = ismaili_caliphate_pretender }
		end_war = invalid
	}

	on_invalidation = {
		log = "CB(emf_ismaili_caliphate_rising): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		clr_global_flag = ismaili_caliphate_revolt_ongoing
		clr_global_flag = ismaili_caliphate_revolt
		ROOT = { clr_flag = ismaili_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
	}
}

emf_liberate_nomad_county = {
	name = CB_NAME_LIBERATE_NOMAD
	war_name = WAR_NAME_LIBERATE_NOMAD
	sprite = 8
	truce_days = 1095
	is_permanent = yes
	check_all_titles = yes
	press_claim = yes
	battle_warscore_mult = 1.25
	allowed_to_target_tributaries = no

	sort_priority = 1200

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			show_scope_change = no
			conditional_tooltip = {
				trigger = { is_patrician = yes }
				custom_tooltip = {
					text = emf_mr_ctt_extended_cbs_unlocked
					has_game_rule = { name = mr_cb_restriction value = off }
				}
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		FROM = { is_nomadic = yes }
		ROOT = {
			is_nomadic = no
			pacifist = no
			mercenary = no
			NOT = { same_realm = FROM }
			# Prefer pagan_county_conquest
			NAND = {
				religion_group = pagan_group
				NOT = { emf_syncretized_religion_with_FROM = yes }
				NOT = { religion = FROM }
			}
			# Prefer emf_pagan_county_conquest_nonholy
			NAND = {
				emf_unreformed_pagan = yes
				religion = FROM
			}
			# Prefer muslim_county_conquest | emf_muslim_county_conquest_religious
			NOT = { religion_group = muslim }
		}
	}

	can_use_title = {
		emf_cb_county_conquest_basic_can_use_title = yes
		location = { any_neighbor_province = { owner_under_ROOT = yes } }
		emf_cb_can_use_title = yes
	}

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

	is_valid_title = {
		owner_under_FROM = yes
		location = { any_neighbor_province = { owner_under_ROOT = yes } }
	}

	on_add_title = {
		log = "CB(emf_liberate_nomad_county): START: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	on_success = {
		emf_cb_victory = yes
		emf_imperial_decay_victory = yes
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
			if = {
				limit = { uses_decadence = yes }
				participation_scaled_decadence = -5
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = 100
				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -5
				}
			}
		}
		FROM = { prestige = -200 }
	}

	on_success_title = {
		log = "CB(emf_liberate_nomad_county): VICTORY: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_latb = yes
		pf_liege_change_under_title_begin_effect = yes
		custom_tooltip = {
			text = liberate_nomad_cb_succ_tip
			hidden_tooltip = {
				ROOT = {
					vassalize_or_take_under_title = {
						title = PREV
						enemy = FROM
						anti_nomad = yes # Vassalize non-nomadic rulers
						type = invasion
					}
				}
			}
		}
		pf_liege_change_under_title_end_effect = yes
		usurp_title = {
			target = ROOT
			type = invasion
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

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

	on_fail_title = {
		log = "CB(emf_liberate_nomad_county): WHITEPEACE: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	on_reverse_demand = {
		emf_cb_defeat = yes
		emf_imperial_decay_loss = yes
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
			prestige = -200
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
			if = {
				limit = { uses_decadence = yes }
				participation_scaled_decadence = -5
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_prestige = 200
				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -5
				}
			}
		}
	}

	on_reverse_demand_title = {
		log = "CB(emf_liberate_nomad_county): DEFEAT: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	on_invalidation = {
		log = "CB(emf_liberate_nomad_county): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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 = { is_tribal = no }
			NOT = {
				any_direct_de_jure_vassal_title = {
					has_holder = yes
					OR = {
						holding_type = castle
						holding_type = city
					}
				}
			}
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

emf_muslim_county_conquest_religious = {
	name = CB_NAME_COUNTY_CONQUEST
	war_name = WAR_NAME_COUNTY_CONQUEST
	sprite = 16
	truce_days = 1825
	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 = no

	sort_priority = 1400

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			custom_tooltip = {
				text = NEEDS_50_PIETY_COST
				hidden_tooltip = { piety = 50 }
			}
			custom_tooltip = {
				text = emf_ctt_no_holy_war_cooldown
				hidden_tooltip = {
					OR = {
						has_game_rule = { name = no_cooldowns value = on }
						NOT = { has_character_modifier = emf_cb_mod_holy_war_cooldown }
					}
				}
			}
			conditional_tooltip = {
				trigger = { is_patrician = yes }
				custom_tooltip = {
					text = emf_mr_ctt_extended_cbs_unlocked
					has_game_rule = { name = mr_cb_restriction value = off }
				}
			}
			OR = {
				independent = yes
				NOR = {
					trait = content
					trait = craven
					trait = cynical
				}
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			religion_group = muslim
			NOR = {
				religion_group = FROM
				emf_syncretized_religion_with_FROM = yes
			}
			NOT = { is_liege_or_above = FROM }
			NOT = { is_vassal_or_below = FROM }
			mercenary = no
		}
	}

	can_use_title = {
		emf_cb_county_conquest_basic_can_use_title = yes
		location = { any_neighbor_province = { owner_under_ROOT = yes } }
		emf_cb_can_use_title = yes
	}

	is_valid_title = {
		owner_under_FROM = yes
		location = { any_neighbor_province = { owner_under_ROOT = yes } }
	}

	on_add = {
		log = "CB(emf_muslim_county_conquest_religious): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		ROOT = { piety = -50 }
		if = {
			limit = { has_game_rule = { name = no_cooldowns value = off } }
			ROOT = {
				custom_tooltip = {
					text = emf_cb_ctt_holy_war_cooldown
					hidden_tooltip = {
						add_character_modifier = {
							name = emf_cb_mod_holy_war_cooldown
							duration = 1825
						}
					}
				}
			}
		}
	}

	on_success = {
		log = "CB(emf_muslim_county_conquest_religious): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		emf_imperial_decay_victory = yes
	}

	on_success_title = {
		if = {
			limit = {
				holder_scope = {
					tier = COUNT
					NOT = { num_of_count_titles = 2 }
					lower_tier_than = ROOT
					in_revolt = no
					OR = {
						is_merchant_republic = yes
						is_patrician = no
					}
				}
			}
			holder_scope = { emf_ROOT_subjugate_ruler = yes }
		}
		else = {
			emf_cb_latb = yes
			usurp_title_plus_barony_if_unlanded = { target = ROOT type = holy_war }
			any_de_jure_vassal_title = { # take all baronies under the one we're fighting for
				limit = {
					has_holder = yes
					NOT = { de_facto_liege = PREV }
					owner_under_FROM = yes
				}
				usurp_title_plus_barony_if_unlanded = { target = ROOT type = holy_war }
			}
		}
		ROOT = { participation_scaled_prestige = 100 }
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(emf_muslim_county_conquest_religious): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = -100 }
		FROM = { participation_scaled_prestige = 50 }
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		log = "CB(emf_muslim_county_conquest_religious): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		emf_imperial_decay_loss = yes
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = { participation_scaled_prestige = 100 }
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_invalidation = {
		log = "CB(emf_muslim_county_conquest_religious): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			remove_character_modifier = emf_cb_mod_holy_war_cooldown
			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 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 # Prefer using CB's without on_add costs
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

# A 3rd-Party County De Jure Claim CB which emperors can use to push appropriate vassal kings' de jure county claims
emf_other_dejure_county_claim_king = {
	name = CB_NAME_EMF_OTHER_DEJURE_COUNTY_CLAIM_KING
	war_name = WAR_NAME_EMF_OTHER_DEJURE_COUNTY_CLAIM_KING
	sprite = 16
	truce_days = 1825
	hostile_against_others = yes
	is_permanent = yes
	check_all_titles = yes
	press_claim = yes # If the liege has a personal claim on the county, he's going to give it up.
	can_ask_to_join_war = no
	infamy_modifier = 0.3

	sort_priority = 1500

	can_use_gui = {
		emf_cb_can_use_gui = yes
		OR = {
			NOT = { has_dlc = "Sons of Abraham" }
			NOT = { religion = FROM }
			FROM = { emf_cb_unexpelled_holy_order = no }
		}
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			independent = yes
			tier = EMPEROR
			NOT = { same_realm = FROM }
			mercenary = no
		}
	}

	can_use_title = {
		tier = COUNT
		owner_under_FROM = yes
		# Receiver is a vassal king and holds the de jure kingdom of this county
		kingdom = {
			owner = {
				vassal_of = ROOT
			}
		}
		# Must NOT be more immediate de jure pretension in the attacker realm (incl. duchies of the kingdom holder)
		# This also de-duplicates `other_dejure_county_claim` (and `dejure_county_claim`).
		dejure_liege_title = { owner_under_ROOT = no }
		# "Catholics" may not press de jure claims for Rome vs. their rightful religious head (i.e., antipope > pope)
		# NOTE: Now extended to any holy site for any religious head.
		NAND = {
			ROOT = { rightful_religious_head = FROM }
			any_de_jure_vassal_title = {
				is_holy_site = FROM
				owner_under_FROM = yes
			}
		}
		emf_cb_can_use_title = yes
	}

	is_valid_title = {
		owner_under_FROM = yes
		kingdom = {
			holder_scope = {
				vassal_of = ROOT # Stays valid if kingdom should become ROOT's
			}
		}
	}

	on_add_title = {
		kingdom = { owner = { save_event_target_as = emf_receiver } }
		log = "CB(emf_other_dejure_county_claim_king): START: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName] on behalf of [emf_receiver.GetTitledFirstName] of [emf_receiver.PrimaryTitle.GetBaseName] ([emf_receiver.GetID]/[emf_receiver.PrimaryTitle.GetID])"
	}

	on_success_title = {
		kingdom = { owner = { save_event_target_as = emf_receiver } }
		log = "CB(emf_other_dejure_county_claim_king): VICTORY: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName] on behalf of [emf_receiver.GetTitledFirstName] of [emf_receiver.PrimaryTitle.GetBaseName] ([emf_receiver.GetID]/[emf_receiver.PrimaryTitle.GetID])"
		emf_cb_victory = yes
		emf_cb_latb = yes
		event_target:emf_receiver = {
			opinion = { who = ROOT modifier = pressed_my_claim }
		}
		if = {
			limit = {
				holder_scope = {
					tier = COUNT
					NOT = { num_of_count_titles = 2 }
					OR = {
						is_patrician = no
						is_merchant_republic = yes
					}
					in_revolt = no
				}
			}
			holder_scope = {
				event_target:emf_receiver = { save_event_target_as = emf_cb_new_imperial_liege }
				emf_set_viceroy_exemption = yes
				set_defacto_liege = event_target:emf_receiver
				hidden_tooltip = {
					if = {
						limit = { vassal_of = event_target:emf_receiver }
						emf_liege_change = yes
						if = {
							limit = { religion = event_target:emf_receiver }
							opinion = { who = event_target:emf_receiver modifier = opinion_subjugated }
						}
						else = {
							opinion = { who = event_target:emf_receiver modifier = opinion_subjugated_other_rel }
						}
					}
				}
			}
		}
		if = {
			limit = {
				holder_scope = {
					NAND = {
						tier = COUNT
						NOT = { num_of_count_titles = 2 }
						OR = {
							is_patrician = no
							is_merchant_republic = yes
						}
						in_revolt = no
					}
				}
			}
			usurp_title_plus_barony_if_unlanded = {
				target = event_target:emf_receiver
				type = claim
			}
			any_de_jure_vassal_title = { # take all baronies under the one we're fighting for
				limit = {
					NOT = { de_facto_liege = PREV }
					owner_under_FROM = yes
				}
				usurp_title_plus_barony_if_unlanded = {
					target = event_target:emf_receiver
					type = claim
				}
			}
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_success_posttitle = { emf_cb_thirdparty_on_success_posttitle = yes }

	on_fail_title = {
		kingdom = { owner = { save_event_target_as = emf_receiver } }
		log = "CB(emf_other_dejure_county_claim_king): WHITEPEACE: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName] on behalf of [emf_receiver.GetTitledFirstName] of [emf_receiver.PrimaryTitle.GetBaseName] ([emf_receiver.GetID]/[emf_receiver.PrimaryTitle.GetID])"
		ROOT = { prestige = -50 }
		event_target:emf_receiver = { prestige = -100 }
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand_title = {
		kingdom = { owner = { save_event_target_as = emf_receiver } }
		log = "CB(emf_other_dejure_county_claim_king): DEFEAT: title=[This.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName] on behalf of [emf_receiver.GetTitledFirstName] of [emf_receiver.PrimaryTitle.GetBaseName] ([emf_receiver.GetID]/[emf_receiver.PrimaryTitle.GetID])"
		emf_cb_defeat = yes
		ROOT = {
			prestige = -100
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		event_target:emf_receiver = { prestige = -200 }
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_invalidation = {
		log = "CB(emf_other_dejure_county_claim_king): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName] on behalf of <N/A>"
	}

	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.1
			location = { any_neighbor_province = { owner_under_ROOT = yes } }
		}
		modifier = {
			factor = 1.1
			location = {
				any_neighbor_province = {
					owner_under_ROOT = yes
					has_flag = emf_capital_connected
				}
			}
		}
		modifier = {
			factor = 1.1
			NOT = { location = { has_flag = emf_capital_connected } }
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = {
				flag = EMF
				days = 730
			}
		}
	}
}

emf_pagan_county_conquest_nonholy = {
	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
	battle_warscore_mult = 0.75

	sort_priority = 1400

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			show_scope_change = no
			custom_tooltip = {
				text = NEEDS_200_PRESTIGE_COST
				prestige >= 200
			}
			conditional_tooltip = {
				trigger = { is_patrician = yes }
				custom_tooltip = {
					text = emf_mr_ctt_extended_cbs_unlocked
					has_game_rule = { name = mr_cb_restriction value = off }
				}
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			emf_unreformed_pagan = yes
			OR = {
				religion = FROM
				FROM = { is_reformed_religion = ROOT }
			}
			NOT = { is_liege_or_above = FROM }
			NOT = { is_vassal_or_below = FROM }
			is_nomadic = no
			mercenary = no
		}
	}

	can_use_title = {
		emf_cb_county_conquest_basic_can_use_title = yes
		location = { any_neighbor_province = { owner_under_ROOT = yes } }
		emf_cb_can_use_title = yes
	}

	is_valid_title = {
		owner_under_FROM = yes
		location = { any_neighbor_province = { owner_under_ROOT = yes } }
	}

	on_add = {
		log = "CB(emf_pagan_county_conquest_nonholy): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		ROOT = { prestige = -200 }
	}

	on_success = {
		log = "CB(emf_pagan_county_conquest_nonholy): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
	}

	on_success_title = {
		emf_cb_latb = yes
		if = {
			limit = {
				holder_scope = {
					tier = COUNT
					NOT = { num_of_count_titles = 2 }
					lower_tier_than = ROOT
				}
			}
			holder_scope = {
				emf_ROOT_subjugate_ruler = yes
			}
		}
		if = {
			limit = {
				holder_scope = {
					NAND = {
						tier = COUNT
						NOT = { num_of_count_titles = 2 }
						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 = {
					NOT = { de_facto_liege = PREV }
					owner_under_FROM = yes
				}
				usurp_title_plus_barony_if_unlanded = {
					target = ROOT
					type = invasion
				}
			}
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 100
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 100 }
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(emf_pagan_county_conquest_nonholy): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { 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 = {
		log = "CB(emf_pagan_county_conquest_nonholy): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			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 }
		}
	}

	on_invalidation = {
		log = "CB(emf_pagan_county_conquest_nonholy): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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 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 # Prefer using CB's without on_add costs
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = {
				flag = EMF
				days = 1825
			}
		}
	}
}

emf_reclaim_byz = {
	name = CB_NAME_EMF_RECLAIM_BYZ
	war_name = WAR_NAME_EMF_RECLAIM_BYZ
	sprite = 17
	truce_days = 1825
	is_permanent = yes
	# is_holy_war = yes
	can_ask_to_join_war = yes
	hostile_against_others = yes
	check_de_jure_tier = DUKE

	allowed_to_target_tributaries = yes

	infamy_modifier = 0.1

	sort_priority = 2000

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			custom_tooltip = {
				text = emf_cb_ctt_capital_is_constantinople
				hidden_tooltip = {
					OR = {
						ai = yes
						capital_scope = {
							c_byzantion = {
								location = {
									province = PREVPREV
								}
							}
						}
					}
				}
			}
		}
	}

	can_use = {
		has_random_dejure = no
		emf_cb_can_use = yes
		c_byzantion = { owner_under_ROOT = yes }
		ROOT = {
			NOT = { same_realm = FROM }
			emf_formation_byz_restorer_potential = yes
			emf_formation_byz_restorer_culrel_allow = yes
		}
		emf_formation_byz_restoration_potential = yes
	}

	can_use_title = {
		OR = {
			de_jure_liege = k_thrace
			AND = {
				emf_has_swmh = no
				de_jure_liege = k_byzantium
			}
			AND = { # Second tier of conquest in case it's necessary for reaching enough realm_size or w/e
				OR = {
					de_jure_liege = k_achaea
					de_jure_liege = k_nikaea
				}
				any_direct_de_jure_vassal_title = { # Must have a direct realm border
					owner_under_FROM = yes
					location = { any_neighbor_province = { owner_under_ROOT = yes } }
				}
				k_thrace = { emf_ROOT_completely_controls_counties = yes }
				OR = {
					emf_has_swmh = yes
					k_byzantium = { emf_ROOT_completely_controls_counties = yes }
				}
			}
		}
		any_direct_de_jure_vassal_title = { owner_under_FROM = yes }
		emf_cb_can_use_de_jure_title = yes
	}

	is_valid = {
		c_byzantion = { owner_under_ROOT = yes }
		ROOT = {
			emf_formation_byz_restorer_potential = yes
			emf_formation_byz_restorer_culrel_allow = yes
		}
		emf_formation_byz_restoration_potential = yes
	}

	is_valid_title = {
		any_de_jure_vassal_title = { owner_under_FROM = yes }
	}

	on_add = {
		log = "CB(emf_reclaim_byz): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		FROM = { emf_remove_suzerain_under_ROOT = yes }
	}

	on_success = {
		log = "CB(emf_reclaim_byz): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
			if = {
				limit = { NOT = { religion = FROM } }
				participation_scaled_piety = 80
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = 200
				if = {
					limit = { NOT = { religion = FROM } }
					participation_scaled_piety = 80
				}
			}
		}
		ROOT = { emf_remove_suzerain_under_FROM = yes }
	}

	on_success_title = {
		emf_cb_latb = yes
		pf_liege_change_under_title_begin_effect = yes
		custom_tooltip = {
			text = emf_cb_ctt_vassalize_or_take_under_title_same_religion
			hidden_tooltip = {
				ROOT = {
					vassalize_or_take_under_title = {
						title = PREV
						enemy = FROM
						is_religious = yes
						same_religion = yes
						type = invasion
					}
				}
			}
		}
		pf_liege_change_under_title_end_effect = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(emf_reclaim_byz): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		FROM = { prestige = 50 }
	}

	on_reverse_demand = {
		log = "CB(emf_reclaim_byz): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = { prestige = -200 }
		FROM = { prestige = 100 }
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
			if = {
				limit = { NOT = { religion = ROOT } }
				participation_scaled_piety = 80
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_prestige = 200
				if = {
					limit = { NOT = { religion = ROOT } }
					participation_scaled_piety = 80
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(emf_reclaim_byz): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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 importance placed on this CB: scope is the targeted title, ROOT is the attacking character, FROM is the defending character
	ai_will_do = {
		factor = 4
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

emf_reclaim_byz_capital = {
	name = CB_NAME_EMF_RECLAIM_BYZ_CAPITAL
	war_name = WAR_NAME_EMF_RECLAIM_BYZ_CAPITAL
	sprite = 17
	truce_days = 1825
	is_permanent = yes
	can_ask_to_join_war = yes
	# hostile_against_others = yes
	check_de_jure_tier = DUKE
	apply_short_occ_mod = no
	def_ticking_war_score_multiplier = 2.0
	att_ticking_war_score_multiplier = 2.0
	allow_distant = yes

	allowed_to_target_tributaries = yes

	infamy_modifier = 0.05

	sort_priority = 2000

	can_use_gui = {
		emf_cb_can_use_gui = yes
		custom_tooltip = {
			text = emf_cb_ctt_reclaim_byz_capital_never_been_used_unless_success_within_last_50yr
			hidden_tooltip = {
				NOT = {
					had_global_flag = {
						flag = emf_reclaim_byz_capital_success
						days = 18250 # 50yr
					}
				}
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		c_byzantion = { owner_under_FROM = yes }
		ROOT = {
			NOT = { same_realm = FROM }
			emf_formation_byz_restorer_potential = yes
			emf_formation_byz_restorer_culrel_allow = yes
		}
		emf_formation_byz_restoration_potential = yes
	}

	can_use_title = {
		title = d_thrace
		emf_cb_can_use_de_jure_title = yes
	}

	is_valid = {
		c_byzantion = { owner_under_FROM = yes }
		ROOT = { emf_formation_byz_restorer_potential = yes }
		emf_formation_byz_restoration_potential = yes
	}

	on_add = {
		log = "CB(emf_reclaim_byz_capital): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		FROM = { emf_remove_suzerain_under_ROOT = yes }
	}

	on_success = {
		log = "CB(emf_reclaim_byz_capital): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 300
			if = {
				limit = { NOT = { religion = FROM } }
				participation_scaled_piety = 120
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = 300
				if = {
					limit = { NOT = { religion = FROM } }
					participation_scaled_piety = 120
				}
			}
		}
		ROOT = { emf_remove_suzerain_under_FROM = yes }
		c_byzantion = {
			emf_cb_latb = yes
			# Since our vassalize_or_take_under_title(d_thrace) happens after we take c_byzantion, we must emulate
			# its same_religion = yes subjugation behavior under c_byzantion ourselves (except w/ tweaks).
			hidden_tooltip = {
				pf_liege_change_under_title_begin_effect = yes
				any_direct_de_jure_vassal_title = {
					limit = {
						owner = {
							under_FROM = yes
							OR = {
								PREV = { holding_type = castle } # Always take castles
								NOT = { religion = ROOT } # Purge if diff-religion
								character = FROM # Purge if FROM
								any_realm_title = { # Purge if has land outside title
									tier = BARON
									NOT = { de_jure_liege_or_above = PREVPREVPREV }
								}
								NOT = { lower_tier_than = ROOT } # Purge if can't subjugate
							}
						}
					}
					gain_title = { target = ROOT type = invasion }
				}
				# Anyone remaining under FROM should be subjugated
				any_de_jure_vassal = {
					limit = { under_FROM = yes }
					emf_ROOT_subjugate_ruler = yes
				}
				gain_title = { target = ROOT type = invasion }
				pf_liege_change_under_title_end_effect = yes
			}
			tooltip = { gain_title = ROOT }
			# Take all castles under c_byzantion. Purely for tooltip purposes, as this was taken care of by the
			# above, hidden "vassalize_or_take_under_title(same_religion - castles)" effect
			location = {
				any_province_holding = {
					limit = {
						holding_type = castle
						owner_under_FROM = yes # still under FROM when tooltip is shown
					}
					tooltip = { gain_title = ROOT }
				}
			}
		}
	}

	on_success_title = {
		emf_cb_latb = yes
		pf_liege_change_under_title_begin_effect = yes
		custom_tooltip = {
			text = emf_cb_ctt_vassalize_or_take_under_title_same_religion
			hidden_tooltip = {
				ROOT = {
					vassalize_or_take_under_title = {
						title = PREV
						enemy = FROM
						is_religious = yes
						same_religion = yes
						type = invasion
					}
				}
			}
		}
		pf_liege_change_under_title_end_effect = yes
	}

	on_success_posttitle = {
		c_byzantion = {
			emf_make_location_ROOT_capital = yes
		}
		hidden_tooltip = {
			# Reset global CB usage success flag timer
			clr_global_flag = emf_reclaim_byz_capital_success
			set_global_flag = emf_reclaim_byz_capital_success
			# Set internal cheevo & handle AI wins w/ special afterwork
			ROOT = {
				clr_flag = emf_context_reclaimed_byz_capital
				set_flag = emf_context_reclaimed_byz_capital
				character_event = { id = emf_empires.30 }
			}
		}
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_reclaim_byz_capital): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		FROM = { prestige = 50 }
	}

	on_reverse_demand = {
		log = "CB(emf_reclaim_byz_capital): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			prestige = -100
		}
		FROM = { prestige = 100 }
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 300
			if = {
				limit = { NOT = { religion = ROOT } }
				participation_scaled_piety = 120
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_prestige = 300
				if = {
					limit = { NOT = { religion = ROOT } }
					participation_scaled_piety = 120
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(emf_reclaim_byz_capital): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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 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 = 2.2
		}
		modifier = {
			factor = 3.0
			ROOT = { has_landed_title = k_thrace }
		}
		modifier = {
			factor = 2.0
			trigger_if = {
				limit = { emf_has_swmh = yes }
				ROOT = { real_tier = KING }
			}
			trigger_else = {
				ROOT = { has_landed_title = k_byzantium }
			}
		}
		modifier = {
			factor = 2.0
			ROOT = { capital_scope = { empire = { title = e_byzantium } } }
		}
		modifier = {
			factor = 1.5
			ROOT = {
				has_flag = emf_context_reclaimed_byz_capital
				NOT = { had_flag = { flag = emf_context_reclaimed_byz_capital days = 3650 } } #10yr
			}
		}
		modifier = {
			factor = 1.5
			ROOT = {
				has_flag = emf_context_reclaimed_byz_capital
				NOT = { had_flag = { flag = emf_context_reclaimed_byz_capital days = 5475 } } #15yr
			}
		}
		modifier = {
			factor = 1.25
			ROOT = {
				has_flag = emf_context_reclaimed_byz_capital
				NOT = { had_flag = { flag = emf_context_reclaimed_byz_capital days = 7300 } } #20yr
			}
		}
		modifier = {
			factor = 1.5
			FROM = {
				NOR = {
					religion_group = ROOT
					ROOT = { emf_syncretized_religion_with_PREV = yes }
				}
			}
		}
		modifier = {
			factor = 1.25
			FROM = {
				OR = {
					religion_group = ROOT
					ROOT = { emf_syncretized_religion_with_PREV = yes }
				}
				NOT = { religion = ROOT }
			}
		}
		modifier = {
			factor = 0.5
			ROOT = { capital_scope = { distance = { where = 496 value = 200 } } }
		}
		modifier = {
			factor = 0.5
			ROOT = { capital_scope = { distance = { where = 496 value = 300 } } }
		}
		modifier = {
			factor = 0.1
			ROOT = { capital_scope = { distance = { where = 496 value = 400 } } }
		}
		modifier = {
			factor = 5.0
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

emf_religious_reconquest = {
	name = CB_NAME_EMF_RELIGIOUS_RECONQUEST
	war_name = WAR_NAME_EMF_RELIGIOUS_RECONQUEST
	sprite = 9
	truce_days = 1825
	is_permanent = yes
	is_holy_war = yes
	can_ask_to_join_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.8

	sort_priority = 1500

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			custom_tooltip = {
				text = emf_ctt_no_holy_war_cooldown
				hidden_tooltip = {
					OR = {
						has_game_rule = { name = no_cooldowns value = on }
						NOT = { has_character_modifier = emf_cb_mod_holy_war_cooldown }
					}
				}
			}
			trigger_if = {
				limit = {
					religion = cathar
					has_random_religion_names = no
				}
				custom_tooltip = {
					text = cathar_holy_war_enabled_ctt
					has_global_flag = cathar_holy_war_enabled
				}
			}
			conditional_tooltip = {
				trigger = { is_patrician = yes }
				custom_tooltip = {
					text = emf_mr_ctt_extended_cbs_unlocked
					has_game_rule = { name = mr_cb_restriction value = off }
				}
			}
			OR = {
				independent = yes
				NOR = {
					trait = cynical
					trait = craven
				}
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		emf_cb_can_use_religious = yes
	}

	can_use_title = {
		emf_cb_religious_reconquest_basic_can_use_title = yes
		emf_cb_special_religious_basic_can_use_title = no # Prefer 'Special Holy War' CB
		emf_cb_religious_basic_can_use_title = yes
		emf_cb_can_use_de_jure_title = yes
	}

	is_valid = {
		emf_cb_is_valid_religious = yes
	}

	is_valid_title = { any_de_jure_vassal_title = { owner_under_FROM = yes } }

	on_add = {
		log = "CB(emf_religious_reconquest): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		if = {
			limit = { has_game_rule = { name = no_cooldowns value = off } }
			ROOT = {
				custom_tooltip = {
					text = emf_cb_ctt_holy_war_cooldown
					hidden_tooltip = {
						add_character_modifier = {
							name = emf_cb_mod_holy_war_cooldown
							duration = 1825
						}
					}
				}
			}
		}
	}

	on_success = {
		log = "CB(emf_religious_reconquest): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		emf_imperial_decay_victory = yes
		ROOT = {
			participation_scaled_piety = 100
			participation_scaled_prestige = 150
			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 = { NOT = { character = ROOT } }
			hidden_tooltip = {
				if = {
					limit = { emf_hostile_religion_to_FROM = yes }
					participation_scaled_piety = 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 }
						}
					}
				}
				participation_scaled_prestige = 150
			}
		}
		ROOT = { religion_authority = { modifier = won_holy_war years = 20 } }
		FROM = { religion_authority = { modifier = lost_holy_war years = 20 } }
		FROM = { piety = -100 }
		FROM = { prestige = -150 }
	}

	on_success_title = {
		emf_cb_religious_on_success_title = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(emf_religious_reconquest): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			piety = -50
			prestige = -100
		}
	}

	on_reverse_demand = {
		log = "CB(emf_religious_reconquest): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		emf_imperial_decay_loss = yes
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -100
			prestige = -150
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_piety = 150
			participation_scaled_prestige = 200
			if = {
				limit = { uses_decadence = yes }
				participation_scaled_decadence = -10
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				if = {
					limit = { emf_hostile_religion_to_ROOT = yes }
					participation_scaled_piety = 150
					if = {
						limit = { uses_decadence = yes }
						participation_scaled_decadence = -10
					}
				}
				participation_scaled_prestige = 200
			}
		}
		ROOT = { religion_authority = { modifier = lost_holy_war years = 20 } }
		FROM = { religion_authority = { modifier = won_holy_war years = 20 } }
	}

	on_invalidation = {
		log = "CB(emf_religious_reconquest): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { remove_character_modifier = emf_cb_mod_holy_war_cooldown }
	}

	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 importance placed on this CB: scope is the targeted title, ROOT is the attacking character, FROM is the defending character
	ai_will_do = {
		factor = 2
		modifier = { # Prefer direct land borders
			factor = 1.5
			any_direct_de_jure_vassal_title = {
				owner_under_FROM = yes
				location = { any_neighbor_province = { owner_under_ROOT = yes } }
			}
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

emf_resist_viceroy_revocation = {
	name = CB_NAME_OVERTHROWRULER
	war_name = WAR_NAME_OVERTHROWRULER
	sprite = 12
	truce_days = 3650

	can_call_vassals = yes
	defender_can_call_allies = no
	can_ask_to_join_war = yes
	major_revolt = yes
	capturing_attacker_is_complete_victory = 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 = {
		log = "CB(emf_resist_viceroy_revocation): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_dynlevy_begin = yes
	}

	on_success = {
		log = "CB(emf_resist_viceroy_revocation): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		ROOT = {
			primary_title = { set_flag = viceroy_exemption }
			emf_switch_to_feudal_gov = yes
		}
		FROM = {
			prestige = -500
			remove_opinion = {
				who = ROOT
				modifier = declared_war
			}
			pf_lower_CA_effect = yes
			pf_lower_TO_effect = yes
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 250
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 250 }
		}
		emf_cb_dynlevy_end = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(emf_resist_viceroy_revocation): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_dynlevy_end = yes
		ROOT = { prestige = -100 }
		FROM = {
			prestige = -100
			remove_opinion = {
				who = ROOT
				modifier = declared_war
			}
		}
	}

	on_reverse_demand = {
		log = "CB(emf_resist_viceroy_revocation): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		emf_cb_dynlevy_end = yes
		ROOT = {
			prestige = -200
			prisoner = FROM
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 50
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
	}

	on_invalidation = {
		log = "CB(emf_resist_viceroy_revocation): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_dynlevy_end = yes
	}

	on_defender_leader_death = {
		hidden_tooltip = { any_attacker = { letter_event = { id = 251 } } }
		end_war = invalid
	}

	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-only Holy Roman Unification CB [player version follows]
emf_restore_hre = {
	name = CB_NAME_EMF_RESTORE_HRE
	war_name = WAR_NAME_EMF_RESTORE_HRE
	sprite = 17
	truce_days = 1825
	is_permanent = yes
	check_all_titles = yes
	hostile_against_others = yes
	attacker_can_call_allies = yes
	defender_can_call_allies = no
	can_ask_to_join_war = no
	apply_short_occ_mod = no
	press_claim = yes
	battle_warscore_mult = 1.25
	infamy_modifier = 0

	can_use = {
		has_random_dejure_or_title_names = no
		emf_cb_can_use = yes
		emf_cb_restore_hre_can_use = yes
	}

	can_use_title = {
		emf_cb_restore_hre_can_use_title = yes
		emf_cb_can_use_title = yes
	}

	is_valid = {
		ROOT = {
			independent = yes
			emf_has_hre_precursor_kingdom = yes
		}
		FROM = {
			independent = yes
			lower_tier_than = EMPEROR
		}
		emf_hre_has_holder = no
	}

	is_valid_title = {
		holder = FROM
	}

	on_add = {
		log = "CB(emf_restore_hre): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		emf_cb_restore_hre_apply_cooldown = yes
	}

	on_success = {
		log = "CB(emf_restore_hre): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		set_global_flag = emf_restore_hre_war_won
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		FROM = { prestige = -200 }
		emf_cb_restore_hre_apply_cooldown = yes
	}

	on_success_title = {
		emf_cb_restore_hre_on_success_title = yes
	}

	on_success_posttitle = {
		if = {
			limit = { FROM = { is_liege_or_above = ROOT } }
			FROM = { imprison = ROOT }
		}
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_restore_hre): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_restore_hre_clear_cooldown = yes
		ROOT = { 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 = {
		log = "CB(emf_restore_hre): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		emf_cb_restore_hre_clear_cooldown = yes
		ROOT = {
			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 }
		}
	}

	on_invalidation = {
		log = "CB(emf_restore_hre): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_restore_hre_clear_cooldown = 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
		additive_modifier = {
			value = -15
			emf_are_karling_usurpers_enabled = yes
			ROOT = { trait = karling_usurper }
			FROM = { NOT = { trait = karling_usurper } }
		}
		additive_modifier = {
			value = -10
			# Defender can afford to accept defeat slightly easier:
			OR = {
				FROM = { num_of_king_titles >= 2 }
				any_independent_ruler = {
					dynasty = FROM
					NOT = { character = FROM }
					any_demesne_title = {
						higher_tier_than = DUKE
						is_primary_type_title = no
						is_de_jure_title = yes
					}
				}
			}
		}
	}

	# 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 = 4
		modifier = { # Focus on the most core kingdom titles for traditional HRE formation
			factor = 1.35
			OR = {
				title = k_italy
				AND = {
					ROOT = { has_landed_title = k_germany }
					title = k_lotharingia
				}
			}
		}
		modifier = {
			factor = 1.25
			any_direct_de_jure_vassal_title = {
				any_direct_de_jure_vassal_title = {
					owner_under_FROM = yes
					location = { any_neighbor_province = { owner_under_ROOT = yes } }
				}
			}
		}
		modifier = {
			factor = 1.25
			any_direct_de_jure_vassal_title = {
				any_direct_de_jure_vassal_title = {
					owner_under_FROM = yes
					location = {
						any_neighbor_province = {
							owner_under_ROOT = yes
							any_neighbor_province = { owner_under_ROOT = yes }
						}
					}
				}
			}
		}
		modifier = {
			factor = 1.4
			any_direct_de_jure_vassal_title = {
				any_direct_de_jure_vassal_title = {
					owner_under_FROM = yes
					location = {
						any_neighbor_province = {
							owner_under_ROOT = yes
							has_flag = emf_capital_connected
						}
					}
				}
			}
		}
		modifier = {
			factor = 5
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

# Player-only -- virtual weak claim on all Carolingien kingdoms by all other such kingdom holders [temporary]
emf_restore_hre_player = {
	name = CB_NAME_EMF_RESTORE_HRE
	war_name = WAR_NAME_EMF_RESTORE_HRE
	sprite = 17
	truce_days = 1825
	is_permanent = yes
	check_all_titles = yes
	hostile_against_others = yes
	attacker_can_call_allies = yes
	defender_can_call_allies = yes
	can_ask_to_join_war = yes
	press_claim = yes

	sort_priority = 2100

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			has_regent = no
			custom_tooltip = {
				text = NEEDS_1000_PRESTIGE_COST
				hidden_tooltip = { prestige = 1000 }
			}
		}
	}

	can_use = {
		has_random_dejure_or_title_names = no
		emf_cb_can_use = yes
		ROOT = { ai = no }
		FROM = {
			independent = yes
			tier = KING
		}
		emf_is_hre_being_restored = yes
		ROOT = { emf_can_restore_hre_someday = yes }
	}

	can_use_title = {
		holder = FROM
		emf_is_hre_precursor_kingdom = yes
		emf_cb_is_weak_claim_pressable = yes # Just want to know if a [virtual] weak claim could be pressed
		any_direct_de_jure_vassal_title = {
			any_direct_de_jure_vassal_title = {
				OR = {
					owner_under_ROOT = yes
					location = {
						any_neighbor_province = {
							is_land = yes
							OR = {
								owner_under_ROOT = yes
								any_neighbor_province = { owner_under_ROOT = yes }
							}
						}
					}
				}
			}
		}
		emf_cb_can_use_title = yes
	}

	is_valid = {
		ROOT = {
			independent = yes
			emf_has_hre_precursor_kingdom = yes
		}
		FROM = {
			independent = yes
			lower_tier_than = EMPEROR
		}
		emf_hre_has_holder = no
	}

	is_valid_title = {
		holder = FROM
	}

	on_add = {
		log = "CB(emf_restore_hre_player): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		ROOT = { prestige = -1000 }
	}

	on_success = {
		log = "CB(emf_restore_hre_player): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		set_global_flag = emf_restore_hre_war_won
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
	}

	on_success_title = {
		emf_cb_restore_hre_on_success_title = yes
	}

	on_success_posttitle = {
		if = {
			limit = { FROM = { is_liege_or_above = ROOT } }
			FROM = { imprison = ROOT }
		}
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_restore_hre_player): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = -100 }
		FROM = { prestige = 100 }
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 150
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 150 }
		}
	}

	on_reverse_demand = {
		log = "CB(emf_restore_hre_player): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			prestige = -250
			transfer_scaled_wealth = {
				to = FROM
				value = 8.0
			}
		}
		FROM = { prestige = 250 }
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 250
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 250 }
		}
	}

	on_invalidation = {
		log = "CB(emf_restore_hre_player): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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_revassalize = {
	name = CB_NAME_EMF_REVASSALIZE
	war_name = WAR_NAME_EMF_REVASSALIZE
	sprite = 8
	truce_days = 1825
	is_permanent = yes
	can_ask_to_join_war = no

	allowed_to_target_tributaries = yes

	infamy_modifier = 0.5

	sort_priority = 1850

	can_use_gui = { emf_cb_can_use_gui = yes }

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			OR = {
				independent = yes
				same_liege = FROM
			}
			higher_tier_than = FROM
			has_opinion_modifier = { who = FROM modifier = opinion_rightful_subjugation }
			in_revolt = no
			mercenary = no
			is_landed = yes
		}
		FROM = {
			in_revolt = no
			OR = {
				is_merchant_republic = yes
				is_patrician = no
			}
			is_landed = yes
			OR = {
				religion = ROOT
				NOR = {
					controls_religion = yes
					holy_order = yes
				}
			}
			any_realm_province = {
				any_neighbor_province = {
					OR = {
						owner_under_ROOT = yes
						any_neighbor_province = { owner_under_ROOT = yes }
					}
				}
			}
		}
	}

	is_valid = {
		ROOT = {
			OR = {
				independent = yes
				same_liege = FROM
			}
			higher_tier_than = FROM
			in_revolt = no
			mercenary = no
			is_landed = yes
		}
		FROM = {
			in_revolt = no
			OR = {
				is_merchant_republic = yes
				is_patrician = no
			}
			is_landed = yes
			OR = {
				religion = ROOT
				NOR = {
					controls_religion = yes
					holy_order = yes
				}
			}
		}
	}

	on_add = {
		log = "CB(emf_revassalize): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		# Cancel any suzerainty status, as this CB allows targeting tributaries.
		FROM = { emf_remove_suzerain_under_ROOT = yes }
	}

	on_success = {
		log = "CB(emf_revassalize): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		emf_imperial_decay_victory = yes
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 50
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 50 }
		}
		FROM = { emf_ROOT_subjugate_ruler = yes }
		hidden_tooltip = {
			ROOT = { remove_opinion = { who = FROM modifier = opinion_rightful_subjugation } }
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(emf_revassalize): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = -50 }
		FROM = { prestige = 50 }
	}

	on_reverse_demand = {
		log = "CB(emf_revassalize): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			prestige = -200
		}
		FROM = { prestige = 100 }
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 200
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 200 }
		}
		hidden_tooltip = {
			ROOT = { remove_opinion = { who = FROM modifier = opinion_rightful_subjugation } }
		}
	}

	on_invalidation = {
		log = "CB(emf_revassalize): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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.3
			FROM = {
				any_realm_province = {
					any_neighbor_province = { owner_under_ROOT = yes }
				}
			}
		}
		modifier = {
			factor = 1.2
			FROM = {
				OR = {
					religion_group = ROOT
					ROOT = { emf_syncretized_religion_with_PREV = yes }
				}
				culture_group = ROOT
			}
		}
		modifier = {
			factor = 3
			FROM = { opinion = { who = ROOT value = 0 } }
			ROOT = { opinion = { who = FROM value = 0 } }
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = {
				flag = EMF
				days = 730
			}
		}
	}
}

emf_special_religious = {
	name = CB_NAME_RELIGIOUS
	war_name = WAR_NAME_RELIGIOUS
	sprite = 9
	truce_days = 1825
	is_permanent = yes
	is_holy_war = yes
	can_ask_to_join_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.6

	sort_priority = 1500

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			show_scope_change = no
			conditional_tooltip = {
				trigger = { is_patrician = yes }
				custom_tooltip = {
					text = emf_mr_ctt_extended_cbs_unlocked
					has_game_rule = { name = mr_cb_restriction value = off }
				}
			}
			OR = {
				independent = yes
				NOR = {
					trait = cynical
					trait = craven
					trait = content
				}
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			NOR = {
				religion_group = FROM
				emf_syncretized_religion_with_FROM = yes
			}
		}
		emf_cb_can_use_religious = yes
	}

	can_use_title = {
		emf_cb_special_religious_basic_can_use_title = yes
		emf_cb_religious_basic_can_use_title = yes
		emf_cb_can_use_de_jure_title = yes
	}

	is_valid = {
		emf_cb_is_valid_religious = yes
	}

	is_valid_title = { any_de_jure_vassal_title = { owner_under_FROM = yes } }

	on_add = {
		log = "CB(emf_special_religious): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(emf_special_religious): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		emf_imperial_decay_victory = yes
		ROOT = {
			participation_scaled_piety = 100
			participation_scaled_prestige = 150
			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 = { NOT = { character = ROOT } }
			hidden_tooltip = {
				if = {
					limit = { emf_hostile_religion_to_FROM = yes }
					participation_scaled_piety = 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 }
						}
					}
				}
				participation_scaled_prestige = 150
			}
		}
		ROOT = { religion_authority = { modifier = won_holy_war years = 20 } }
		FROM = { religion_authority = { modifier = lost_holy_war years = 20 } }
		FROM = { piety = -100 }
		FROM = { prestige = -150 }
	}

	on_success_title = {
		emf_cb_religious_on_success_title = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(emf_special_religious): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			piety = -50
			prestige = -100
		}
	}

	on_reverse_demand = {
		log = "CB(emf_special_religious): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		emf_imperial_decay_loss = yes
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			piety = -100
			prestige = -150
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_piety = 150
			participation_scaled_prestige = 200
			if = {
				limit = { uses_decadence = yes }
				participation_scaled_decadence = -10
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				if = {
					limit = { emf_hostile_religion_to_ROOT = yes }
					participation_scaled_piety = 150
					if = {
						limit = { uses_decadence = yes }
						participation_scaled_decadence = -10
					}
				}
				participation_scaled_prestige = 200
			}
		}
		ROOT = { religion_authority = { modifier = lost_holy_war years = 20 } }
		FROM = { religion_authority = { modifier = won_holy_war years = 20 } }
	}

	on_invalidation = {
		log = "CB(emf_special_religious): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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 importance placed on this CB: scope is the targeted title, ROOT is the attacking character, FROM is the defending character
	ai_will_do = {
		factor = 2
		modifier = { # Prefer direct land borders
			factor = 1.5
			any_direct_de_jure_vassal_title = {
				owner_under_FROM = yes
				location = { any_neighbor_province = { owner_under_ROOT = yes } }
			}
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

emf_subjugation = {
	name = CB_NAME_EMF_SUBJUGATION
	war_name = WAR_NAME_EMF_SUBJUGATION
	sprite = 16
	truce_days = 1825
	is_permanent = yes
	can_ask_to_join_war = yes

	allowed_to_target_tributaries = yes # Upon success, tributary relations are nullified

	# really not a very big deal-- defender's entire realm is already a rightful part of the attacker realm. also, usage
	# of the CB to increase
	infamy_modifier = 0.15

	sort_priority = 1500

	can_use_gui = { emf_cb_can_use_gui = yes }

	# Cannot seem to get even a ROOT = { always = no }
	# can_use_gui to prevent war declaration, even though
	# tooltip shows that the conditions are not met.
	#
	# Truly mysterious, but there's probably something weird about this CB and can_use_gui that I do not understand.
	#
	# FIXME: Retry can_use_gui. Above note was from patch 2.3. Stuff gets fixed.

	can_use = {
		emf_cb_can_use = yes
		# BEGIN: what should ideally go in can_use_gui (well, with some updated CTTs, as this has aged a bit now)
		ROOT = {
			# Tribal/nomadic rulers do not recognize the notion of de jure vassalage, so they don't get to use
			# this force-de-jure-fealty CB.
			is_tribal = no
			is_nomadic = no
			religion_group = FROM # This does indeed mean pagans may use this CB on unrelated-religion pagans
			NOR = {
				is_heresy_of = FROM
				is_parent_religion = FROM
			}
		}
		FROM = {
			in_revolt = no
			OR = {
				lower_tier_than = DUKE
				AND = {
					tier = DUKE
					custom_tooltip = {
						text = emf_cb_ctt_defender_affected_by_your_medium_ca
						hidden_tooltip = {
							crownlaw_title = {
								owner_under_ROOT = yes
								emf_laws_has_medium_ca_plus = yes
							}
						}
					}
				}
				AND = {
					tier = KING
					custom_tooltip = {
						text = emf_cb_ctt_defender_affected_by_your_high_ca
						hidden_tooltip = {
							crownlaw_title = {
								owner_under_ROOT = yes
								emf_laws_has_high_ca_plus = yes
							}
						}
					}
				}
			}
		} # END: what should ideally go in can_use_gui
		ROOT = {
			higher_tier_than = FROM
			OR = {
				independent = yes
				AND = {
					same_liege = FROM
					FROM = {
						OR = {
							higher_tier_than = COUNT
							NOT = {
								primary_title = {
									dejure_liege_title = {
										owner = {
											OR = {
												is_liege_or_above = ROOT
												is_vassal_or_below = ROOT
											}
										}
									}
								}
							}
						}
					}
				}
			}
			mercenary = no
		}
		FROM = {
			# Nomads cannot be [safely] vassalized (and an appropriate such war target could never arise)
			is_nomadic = no
			# Can use this on MRs but not regular patricians (can't vassalize regular patricians)
			OR = {
				is_merchant_republic = yes
				is_patrician = no
			}
			# Not possible to [safely] vassalize revolters
			in_revolt = no
			# Cannot target rebels (i.e., provincial rebels or e_rebels/e_pirates-- not same as *revolters*)
			rebel = no
			# No temporary titles (cannot be vassalized, IIRC, but in any case, we also don't want to allow it)
			NOT = { any_demesne_title = { temporary = yes } }
			# NOTE: All other types of landless-type title holders are fine if ROOT is actually de jure liege to
			# their entire realm, including said landless titles (see magic below).
		}
		FROM = {
			# All of FROM's realm counties must be de jure vassal to some title under ROOT, and all of FROM's
			# realm settlements/baronies must be the same, unless those titles happen to be under an MR or an HO.
			NOT = {
				any_realm_title = {
					OR = {
						tier = COUNT
						AND = {
							tier = BARON
							owner = {
								NOR = {
									is_merchant_republic = yes
									any_liege = { is_merchant_republic = yes }
									holy_order = yes
									any_liege = { holy_order = yes }
								}
							}
						}
					}
					NOT = { any_de_jure_liege_title = { owner_under_ROOT = yes } }
				}
			}
			# All of FROM's held DUKE+ landless-type titles must be de jure vassal to some title under ROOT.
			# Allows the subjugation of *de jure vassal* holy orders, mercenaries, religious heads, and such
			# without worrying about landless-type titles that are below DUKE-tier (e.g., family palaces) being
			# de jure.
			NOT = {
				any_demesne_title = {
					is_landless_type_title = yes
					higher_tier_than = COUNT
					NOT = { any_de_jure_liege_title = { owner_under_ROOT = yes } }
				}
			}
		}
	}

	is_valid = {
		# + If the religious relations required for DoW change during the war, it shall not invalidate the war.
		# + While we do not require the entire realm of FROM to remain de jure vassal to ROOT after war has
		#   been declared, we do still enforce the invariants regarding de jure vassal landless-type titles.
		ROOT = {
			OR = {
				independent = yes
				same_liege = FROM
			}
			higher_tier_than = FROM
		}
		FROM = {
			is_nomadic = no
			OR = {
				is_merchant_republic = yes
				is_patrician = no
			}
			in_revolt = no
			rebel = no
			NOT = { any_demesne_title = { temporary = yes } }
			NOT = {
				any_demesne_title = {
					is_landless_type_title = yes
					higher_tier_than = COUNT
					NOT = { any_de_jure_liege_title = { owner_under_ROOT = yes } }
				}
			}
		}
	}

	on_add = {
		log = "CB(emf_subjugation): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		# Cancel any suzerainty status, as this CB allows targeting tributaries.
		FROM = { emf_remove_suzerain_under_ROOT = yes }
	}

	on_success = {
		log = "CB(emf_subjugation): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		if = {
			limit = { FROM = { tier = BARON } }
			any_attacker = {
				limit = { character = ROOT }
				participation_scaled_prestige = 25
			}
			any_attacker = {
				limit = { NOT = { character = ROOT } }
				hidden_tooltip = { participation_scaled_prestige = 25 }
			}
		}
		if = {
			limit = { FROM = { tier = COUNT } }
			any_attacker = {
				limit = { character = ROOT }
				participation_scaled_prestige = 50
			}
			any_attacker = {
				limit = { NOT = { character = ROOT } }
				hidden_tooltip = { participation_scaled_prestige = 50 }
			}
		}
		if = {
			limit = { FROM = { tier = DUKE } }
			any_attacker = {
				limit = { character = ROOT }
				participation_scaled_prestige = 100
			}
			any_attacker = {
				limit = { NOT = { character = ROOT } }
				hidden_tooltip = { participation_scaled_prestige = 100 }
			}
		}
		if = {
			limit = { FROM = { tier = KING } }
			any_attacker = {
				limit = { character = ROOT }
				participation_scaled_prestige = 200
			}
			any_attacker = {
				limit = { NOT = { character = ROOT } }
				hidden_tooltip = { participation_scaled_prestige = 200 }
			}
		}

		FROM = {
			emf_ROOT_subjugate_ruler = yes

			# IDEA: actually make the subjugation event-based, to allow the subjugated to choose a reaction and
			# flavorfully reflect the fact that this CB, by its nature, is merely forcing a rightful vassal
			# (from the perspective of the subjugator) to swear fealty. indeed, it may even be appropriate for
			# the subjugator to potentially choose to pay some portion of war reparations. if the subjugated
			# ruler reacts poorly to the forced oath of fealty (and potential lack of reparations), then we can
			# give them an inherited TOM which makes them far more likely to seek independence at their next
			# opportunity (and other such things). oh, another thing that ROOT might choose to do here is to
			# renounce any claims he may have upon the titles of FROM.
			#
			# while I'm tossing around ideas in the comments here, it's worth noting that I'd eventually like
			# to replace the 'Offer Vassalization' diplo-action with a targeted_decision and then only allow use
			# of this CB if a diplomatic vassalization attempt has already been recently made and rejected.
			# however, comprehensive gating of all CBs while the AI is attempting the 'mission' to diplo-
			# vassalize a de jure vassal would also be required (quite easy with our current CB infrastructure).
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(emf_subjugation): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = -25 }
		FROM = { prestige = 25 }
	}

	on_reverse_demand = {
		log = "CB(emf_subjugation): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		if = {
			limit = { ROOT = { tier = COUNT } }
			any_defender = {
				limit = { character = FROM }
				participation_scaled_prestige = 50
			}
			any_defender = {
				limit = { NOT = { character = FROM } }
				hidden_tooltip = { participation_scaled_prestige = 50 }
			}
		}
		if = {
			limit = { ROOT = { tier = DUKE } }
			any_defender = {
				limit = { character = FROM }
				participation_scaled_prestige = 100
			}
			any_defender = {
				limit = { NOT = { character = FROM } }
				hidden_tooltip = { participation_scaled_prestige = 100 }
			}
		}
		if = {
			limit = { ROOT = { tier = KING } }
			any_defender = {
				limit = { character = FROM }
				participation_scaled_prestige = 200
			}
			any_defender = {
				limit = { NOT = { character = FROM } }
				hidden_tooltip = { participation_scaled_prestige = 200 }
			}
		}
		if = {
			limit = { ROOT = { tier = EMPEROR } }
			any_defender = {
				limit = { character = FROM }
				participation_scaled_prestige = 400
			}
			any_defender = {
				limit = { NOT = { character = FROM } }
				hidden_tooltip = { participation_scaled_prestige = 400 }
			}
		}
	}

	on_invalidation = {
		log = "CB(emf_subjugation): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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.8
			# Defender respects the notion of rightful de jure vassalage
			ROOT = { is_feudal = yes }
			FROM = { is_feudal = yes }
		}
	}

	ai_will_do = {
		factor = 1
		# Prefer using this [essentially peaceful] CB generally
		modifier = {
			factor = 1.1
			always = yes
		}
		# Greatly prefer to use this CB rather than alternatives
		# (which likely will usurp titles) when the attacker
		# and defender both do not dislike each other
		modifier = {
			factor = 4
			# FIXME: check whether `as_if_liege = yes` works with regular `opinion` and not just `opinion_diff`
			FROM = { opinion = { who = ROOT value = 0 } }
			ROOT = { opinion = { who = FROM value = 0 } }
		}
		# If ROOT likes FROM a LOT, you'd think: definitely _do not_ use the CB, but lack of such modifiers in other
		# CBs would make this incoherent, because then ROOT'll just likely use a CB upon FROM which will usurp
		# at least some of their titles).
		# If ROOT likes FROM, prefer to use this CB (and if FROM doesn't dislike ROOT, *definitely* prefer it -- 6.6)
		modifier = {
			factor = 1.5
			ROOT = { opinion = { who = FROM value = 20 } }
		}
		modifier = {
			factor = 10 # Could reach 66, which is perfectly fine
			FROM = { ai = no }
			had_global_flag = {
				flag = EMF
				days = 730
			}
		}
	}
}

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

	can_use = {
		emf_cb_can_use = yes
		ROOT = { has_flag = duchy_adventurer }
	}

	is_valid = { always = yes }

	on_add = {
		log = "CB(normandy_adventure): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(normandy_adventure): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		ROOT = {
			prestige = 250
			hidden_tooltip = {
				clr_flag = duchy_adventurer
			}
		}
		FROM = { prestige = -250 }
	}

	on_success_title = {
		emf_cb_latb = yes
		pf_liege_change_under_title_begin_effect = yes

		if = {
			limit = {
				c_arques = { owner_under_FROM = yes }
			}
			c_arques = {
				usurp_title_plus_barony_if_unlanded = {
					target = ROOT
					type = invasion
				}
			}
		}
		if = {
			limit = {
				c_eu = { owner_under_FROM = yes }
			}
			c_eu = {
				usurp_title_plus_barony_if_unlanded = {
					target = ROOT
					type = invasion
				}
			}
		}
		if = {
			limit = {
				c_vexin = { owner_under_FROM = yes }
			}
			c_vexin = {
				usurp_title_plus_barony_if_unlanded = {
					target = ROOT
					type = invasion
				}
			}
		}

		pf_liege_change_under_title_end_effect = yes
		ROOT = {
			hidden_tooltip = {
				character_event = {
					id = emf_adventurer.2752
					days = 1
				}
			}
			emf_check_adventurer_government = yes
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(normandy_adventure): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		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 = normandy_adventure
				clr_flag = duchy_adventurer
			}
		}
		ROOT = { prestige = -100 }
	}

	on_reverse_demand = {
		log = "CB(normandy_adventure): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		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 = normandy_adventure
				clr_flag = duchy_adventurer
			}
		}
	}

	on_attacker_leader_death = {
		end_war = invalid
	}

	on_invalidation = {
		log = "CB(normandy_adventure): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			hidden_tooltip = {
				disband_event_forces = normandy_adventure
				clr_flag = duchy_adventurer
			}
		}
	}

	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_magyar_invasion = {
	name = CB_NAME_MAGYAR_INVASION
	war_name = WAR_NAME_MAGYAR_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

	sort_priority = 1200 # think this is only script-invoked, though?

	can_use = {
		emf_cb_can_use = yes
		ROOT = {
			trigger_if = {
				limit = { emf_has_swmh = yes }
				culture = old_hungarian
			}
			trigger_else = {
				culture = hungarian
			}
			higher_real_tier_than = DUKE
			independent = yes
			OR = {
				is_nomadic = yes
				is_tribal = yes
			}
			any_realm_province = { region = emf_region_etelkoz }
			has_global_flag = emf_magyar_migration_started
			NOR = {
				has_global_flag = emf_magyar_migration_completed
				any_realm_title = { de_jure_liege_or_above = k_hungary }
				num_of_count_titles_in_realm = 12
			}
		}
		FROM = {
			any_realm_province = {
				OR = {
					county = { is_occupied = no }
					controlled_by = ROOT
				}
				region = emf_region_lower_carpathia
			}
			NOR = {
				reverse_has_truce = ROOT
				is_allied_with = ROOT
				has_non_aggression_pact_with = ROOT
				pays_tribute_to = ROOT
			}
		}
		emf_alternate_start = no
	}

	can_use_title = {
		title = k_hungary
	}

	is_valid = {
		FROM = {
			independent = yes
			any_realm_province = {
				OR = {
					county = { is_occupied = no }
					controlled_by = ROOT
				}
				region = emf_region_lower_carpathia
			}
		}
	}

	on_add = {
		log = "CB(emf_magyar_invasion): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success_title = {
		log = "CB(emf_magyar_invasion): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		FROM = {
			prestige = -250
		}
		pf_liege_change_under_title_begin_effect = yes
		ROOT = {
			prestige = 250
			gain_settlements_under_title = {
				title = PREV
				enemy = FROM
				is_crusade = yes
				type = holy_war
			}
			custom_tooltip = {
				text = WILL_MIGRATE_TO_HUNGARY
				hidden_tooltip = {
					character_event = { id = emf_magyar.3002 }
				}
			}
		}
		pf_liege_change_under_title_end_effect = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(emf_magyar_invasion): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = -100 }
		clr_global_flag = emf_magyar_migration_started
	}

	on_reverse_demand = {
		log = "CB(emf_magyar_invasion): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			prestige = -250
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
		}
		FROM = { prestige = 250 }
	}

	on_invalidation = {
		log = "CB(emf_magyar_invasion): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		clr_global_flag = emf_magyar_migration_started
	}

	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_title = {
						tier = COUNT
						de_jure_liege_or_above = k_hungary
						is_occupied = no
						count = 3
					}
				}
			}
		}
	}
}

emf_magyar_conquest_hungary = {
	name = CB_NAME_MAGYAR_CONQUEST
	war_name = WAR_NAME_MAGYAR_CONQUEST
	sprite = 8
	truce_days = 3650
	is_permanent = yes
	hostile_against_others = yes
	can_ask_to_join_war = yes
	check_all_titles = yes # if permanent, setting this to true will check against all of someones titles, including vassal held titles
	allowed_to_target_tributaries = no

	sort_priority = 1500

	can_use = {
		emf_cb_can_use = yes
		has_global_flag = emf_magyar_migration_completed
		NOT = { has_global_flag = emf_conquest_hungary_completed }
		ROOT = {
			trigger_if = {
				limit = { emf_has_swmh = yes }
				culture = old_hungarian
			}
			trigger_else = {
				culture = hungarian
			}
			independent = yes
			capital_scope = { kingdom = { title = k_hungary } }
			is_tribal = yes
		}
		emf_alternate_start = no
	}

	can_use_title = {
		tier = COUNT
		emf_cb_can_use_de_jure_title = yes
		# The attacker needs at least one county in the target duchy, or a land border
		location = {
			region = emf_region_carpathia
			owner_under_FROM = yes
			any_neighbor_province = { owner_under_ROOT = yes }
		}
	}

	is_valid_title = {
		owner_under_FROM = yes
	}

	on_add = {
		log = "CB(emf_magyar_conquest_hungary): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(emf_magyar_conquest_hungary): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 150
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = { participation_scaled_prestige = 150 }
		}
		FROM = { prestige = -150 }
	}

	on_success_title = {
		emf_cb_latb = yes
		usurp_title_plus_barony_if_unlanded = {
			target = ROOT
			type = invasion
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(emf_magyar_conquest_hungary): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			prestige = -100
		}
	}

	on_reverse_demand = {
		log = "CB(emf_magyar_conquest_hungary): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		emf_imperial_decay_loss = yes
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
			ROOT = { prestige = -250 }
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 250
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = { participation_scaled_prestige = 250 }
		}
	}

	on_invalidation = {
		log = "CB(emf_magyar_conquest_hungary): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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 importance placed on this CB: scope is the targeted title, ROOT is the attacking character, FROM is the defending character
	ai_will_do = {
		factor = 5
		modifier = { # Prefer direct land borders
			factor = 1.5
			any_direct_de_jure_vassal_title = {
				owner_under_FROM = yes
				location = { any_neighbor_province = { owner_under_ROOT = yes } }
			}
		}
	}
}

emf_magyar_raid = {
	name = CB_NAME_MAGYAR_RAID
	war_name = WAR_NAME_MAGYAR_RAID
	sprite = 8
	truce_days = 3650
	allowed_to_target_tributaries = no
	is_permanent = yes
	can_ask_to_join_war = no

	sort_priority = 1200

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

	can_use = {
		emf_cb_can_use = yes
		has_global_flag = emf_conquest_hungary_completed
		ROOT = {
			trigger_if = {
				limit = { emf_has_swmh = yes }
				culture = old_hungarian
			}
			trigger_else = {
				culture = hungarian
			}
			capital_scope = { kingdom = { title = k_hungary } }
			religion_group = pagan_group
		}
		FROM = {
			independent = yes
		}
		emf_alternate_start = no
	}

	is_valid = {
		ROOT = {
			trigger_if = {
				limit = { emf_has_swmh = yes }
				culture = old_hungarian
			}
			trigger_else = {
				culture = hungarian
			}
			capital_scope = { region = emf_region_carpathia }
			religion_group = pagan_group
		}
		FROM = {
			independent = yes
		}
	}

	on_add = {
		log = "CB(emf_magyar_raid): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(emf_magyar_raid): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		emf_imperial_decay_victory = yes
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 250
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = 250
				add_character_modifier = {
					name = emf_victory_timer
					duration = 1
					hidden = yes
				}
			}
		}
		FROM = {
			prestige = -250
			transfer_scaled_wealth = {
				to = ROOT
				value = 2.0
			}
			any_realm_lord = {
				limit = { capital_holding = { controlled_by = ROOT } }
				transfer_scaled_wealth = {
					to = ROOT
					value = 2.0
				}
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(emf_magyar_raid): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = -125 }
		FROM = { prestige = 50 }
	}

	on_reverse_demand = {
		log = "CB(emf_magyar_raid): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		emf_imperial_decay_loss = yes
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 250
			hidden_tooltip = {
				add_character_modifier = {
					name = emf_victory_timer
					duration = 1
					hidden = yes
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_prestige = 250
				add_character_modifier = {
					name = emf_victory_timer
					duration = 1
					hidden = yes
				}
			}
		}
		ROOT = {
			prestige = -250
			imprison = FROM
			transfer_scaled_wealth = {
				to = ROOT
				value = 4.0
			}
		}
	}

	on_invalidation = {
		log = "CB(emf_magyar_raid): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = {
		factor = 1
		modifier = {
			factor = 0.5
			NOT = { has_global_flag = emf_conquest_hungary_completed }
		}
		modifier = {
			factor = 0
			ROOT = {
				OR = {
					trait = content
					trait = craven
					NOT = { martial = 8 }
				}
			}
		}
		modifier = {
			factor = 0
			capital_scope = { region = emf_region_carpathia }
		}
	}
}

emf_lodbrok_invasion = {
	name = CB_NAME_VIKING_INVASION
	war_name = WAR_NAME_VIKING_INVASION
	sprite = 20
	truce_days = 365
	is_permanent = no # only scripted invocation possible
	infamy_modifier = 0
	hostile_against_others = no
	check_de_jure_tier = DUKE
	apply_short_occ_mod = no

	can_use = {
		emf_cb_can_use = yes
	}

	can_use_title = {
		emf_cb_can_use_de_jure_title = yes
	}

	is_valid = {
		ROOT = { religion = norse_pagan }
	}

	is_valid_title = {
		OR = {
			any_de_jure_vassal_title = { owner_under_FROM = yes }
			AND = {
				has_flag = emf_lodbrok_invasion_of_northumberland
				OR = {
					d_lancaster = { any_de_jure_vassal_title = { owner_under_FROM = yes } }
					d_lothian = { any_de_jure_vassal_title = { owner_under_FROM = yes } }
				}
			}
			AND = {
				has_flag = emf_lodbrok_invasion_of_mercia
				d_norfolk = { any_de_jure_vassal_title = { owner_under_FROM = yes } }
			}
		}
	}

	on_add = {
		log = "CB(emf_lodbrok_invasion): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(emf_lodbrok_invasion): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		ROOT = {
			prestige = 200
			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_viking }
						10 = { give_nickname = nick_the_dragon }
						10 = { give_nickname = nick_the_sword_of_god }
					}
				}
			}
			religion_authority = { modifier = won_viking_invasion }
		}
		FROM = { prestige = -200 }
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_piety = 100
			participation_scaled_prestige = 150
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			participation_scaled_piety = 100
			participation_scaled_prestige = 150
		}
	}

	on_success_title = {
		any_attacker = {
			limit = { has_landed_title = d_western_isles }
			save_event_target_as = emf_ivar
		}
		any_attacker = {
			limit = { dynasty = 7515 }
			save_event_target_as = emf_ubbe
		}
		any_attacker = {
			limit = { has_landed_title = d_york }
			save_event_target_as = emf_halfdan
		}

		log = "---> emf_ivar=[emf_ivar.GetID], emf_ubbe=[emf_ubbe.GetID], emf_halfdan=[emf_halfdan.GetID] (must participate in war to not be blank)"

		ROOT = { save_event_target_as = emf_receiver }
		emf_cb_lodbrok_invasion_on_success_duchy = yes

		trigger_switch = {
			on_trigger = has_flag
			emf_lodbrok_invasion_of_northumberland = {
				d_lancaster = {
					emf_cb_lodbrok_invasion_on_success_duchy = yes
				}
				d_lothian = {
					if = {
						limit = { event_target:emf_ivar = { always = yes } }
						event_target:emf_ivar = { save_event_target_as = emf_receiver }
					}
					if = {
						limit = { NOT = { event_target:emf_ivar = { always = yes } } }
						ROOT = { save_event_target_as = emf_receiver }
					}
					emf_cb_lodbrok_invasion_on_success_duchy = yes
				}
				c_163103 = { # Aella's fate
					if = {
						limit = {
							character = FROM
							OR = {
								prisoner = no
								host = { under_ROOT = yes }
								host = { character = event_target:emf_ivar }
								host = { is_liege_or_above = event_target:emf_ivar }
								host = { character = event_target:emf_ubbe }
								host = { is_liege_or_above = event_target:emf_ubbe }
							}
						}
						imprison = ROOT
					}
					# If both Aella and ROOT are AI (and we successfully imprisoned him), just execute him right here.
					if = {
						limit = {
							host = { character = ROOT }
							ai = yes
							ROOT = { ai = yes }
						}
						death = { death_reason = death_execution killer = ROOT }
					}
				}
			}
			emf_lodbrok_invasion_of_mercia = {
				d_norfolk = {
					if = {
						limit = { event_target:emf_ubbe = { always = yes } }
						event_target:emf_ubbe = { save_event_target_as = emf_receiver }
					}
					if = {
						limit = { NOT = { event_target:emf_ubbe = { always = yes } } }
						ROOT = { save_event_target_as = emf_receiver }
					}
					emf_cb_lodbrok_invasion_on_success_duchy = yes
				}
			}
			emf_lodbrok_invasion_of_east_anglia = {
				hidden_tooltip = {
					# Give counties in York back to Halfdan if AI & Ubbe.
					random_independent_ruler = {
						limit = {
							NOT = { event_target:emf_halfdan = { always = yes } }
							has_landed_title = d_york
							religion = ROOT
						}
						save_event_target_as = emf_halfdan
					}
					if = {
						limit = {
							ROOT = {
								ai = yes
								independent = yes
								NOT = { character = event_target:emf_halfdan }
								NOT = { has_landed_title = d_york }
								any_demesne_province = {
									NOT = { duchy = { title = d_york } }
								}
								any_demesne_province = { duchy = { title = d_york } }
							}
						}
						ROOT = {
							any_demesne_title = {
								limit = {
									tier = COUNT
									de_jure_liege_or_above = d_york
								}
								grant_title = event_target:emf_halfdan
							}
							any_demesne_title = {
								limit = {
									tier = BARON
									de_jure_liege_or_above = d_york
								}
								grant_title = event_target:emf_halfdan
							}
						}
						d_york = {
							any_de_jure_vassal = {
								limit = { vassal_of = ROOT }
								set_defacto_liege = event_target:emf_halfdan
								emf_liege_change = yes
								event_target:emf_halfdan = { opinion = { who = ROOT modifier = opinion_vassal_transfer } }
							}
						}
					}
				}

				# Give Ubbe a small standing retinue, as he actually got weaker for winning this war and can't stand
				# on his own.
				ROOT = {
					capital_scope = {
						ROOT = {
							spawn_unit = {
								province = PREV
								troops = {
									light_infantry = { 350 350 }
									archers = { 250 250 }
									heavy_infantry = { 175 175 }
									light_cavalry = { 125 125 }
									knights = { 25 25 }
								}
								reinforces = yes
								attrition = 1.0
								earmark = ubbe_post_invasion_retinue
							}
							spawn_unit = {
								province = PREV
								troops = {
									light_infantry = { 350 350 }
									archers = { 250 250 }
									heavy_infantry = { 175 175 }
									light_cavalry = { 125 125 }
									knights = { 25 25 }
								}
								reinforces = yes
								attrition = 1.0
								earmark = ubbe_post_invasion_retinue
							}
							spawn_unit = {
								province = PREV
								troops = {
									light_infantry = { 350 350 }
									archers = { 250 250 }
									heavy_infantry = { 175 175 }
									light_cavalry = { 125 125 }
									knights = { 25 25 }
								}
								reinforces = yes
								attrition = 1.0
								earmark = ubbe_post_invasion_retinue
							}
						}
					}
				}
			}
		}

		if = {
			limit = { FROM = { is_liege_or_above = ROOT } }
			FROM = { imprison = ROOT }
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(emf_lodbrok_invasion): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		FROM = {
			prestige = 100
		}
		ROOT = {
			prestige = -100
		}
	}

	on_reverse_demand = {
		log = "CB(emf_lodbrok_invasion): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		ROOT = {
			transfer_scaled_wealth = { to = FROM value = 4.0 }
			prestige = -200
			religion_authority = { modifier = lost_viking_invasion }
		}
		FROM = { prestige = 200 }
		any_defender = {
			limit = { character = FROM }
			participation_scaled_piety = 100
			participation_scaled_prestige = 150
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			participation_scaled_piety = 100
			participation_scaled_prestige = 150
		}
	}

	on_invalidation = {
		log = "CB(emf_lodbrok_invasion): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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.85
			ROOT = { ai = yes }
		}
	}
}

emf_norman_border_war = {
	name = CB_NAME_EMF_NORMAN_BORDER_WAR
	war_name = WAR_NAME_EMF_NORMAN_BORDER_WAR
	sprite = 16
	truce_days = 1095
	hostile_against_others = yes
	is_permanent = yes
	can_ask_to_join_war = yes
	apply_short_occ_mod = no
	allowed_to_target_tributaries = no

	sort_priority = 2000

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			show_scope_change = no
			conditional_tooltip = {
				trigger = { ai = no }
				custom_tooltip = {
					text = NEEDS_200_PIETY_COST
					hidden_tooltip = { piety = 200 }
				}
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		FROM = { has_landed_title = e_byzantium }
		ROOT = {
			culture = norman
			top_liege = {
				independent = yes
				culture = PREV
			}
			NOR = {
				same_realm = FROM
				culture = FROM
			}
			religion_group = christian
			mercenary = no
			capital_scope = {
				owner = { character = ROOT }
				region = emf_region_norman_sicily
			}
		}
		FROM = {
			any_realm_province = {
				count = 2
				region = emf_region_norman_sicily
				any_neighbor_province = { owner_under_ROOT = yes }
			}
		}
		has_random_culture_names = no
		has_random_title_names = no
	}

	is_valid = {
		ROOT = {
			culture = norman
			top_liege = {
				independent = yes
				culture = PREV
			}
			NOR = {
				same_realm = FROM
				culture = FROM
			}
			religion_group = christian
			mercenary = no
		}
		FROM = {
			any_realm_province = {
				region = emf_region_norman_sicily
				any_neighbor_province = { owner_under_ROOT = yes }
			}
		}
	}

	on_add = {
		log = "CB(emf_norman_border_war): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		ROOT = {
			if = {
				limit = { ai = no }
				piety = -200
			}
		}
	}

	on_success = {
		log = "CB(emf_norman_border_war): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		FROM = {
			any_realm_province = {
				limit = {
					region = emf_region_norman_sicily
					any_neighbor_province = { owner_under_ROOT = yes }
				}
				county = {
					emf_cb_latb = yes
					hidden_tooltip = {
						any_direct_de_jure_vassal_title = {
							limit = {
								owner_under_PREVPREVPREV = yes
								is_allowed_holding_type = ROOT
							}
							usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
						}
					}
					usurp_title_plus_barony_if_unlanded = { target = ROOT type = invasion }
				}
			}
		}
		any_attacker = {
			limit = { character = ROOT }
			participation_scaled_prestige = 200
			if = {
				limit = { emf_hostile_religion_to_FROM = yes }
				participation_scaled_piety = 50
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = 200
				if = {
					limit = { emf_hostile_religion_to_FROM = yes }
					participation_scaled_piety = 50
				}
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(emf_norman_border_war): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = -100 }
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 50
			if = {
				limit = { emf_hostile_religion_to_ROOT = yes }
				participation_scaled_piety = 25
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_prestige = 50
				if = {
					limit = { emf_hostile_religion_to_ROOT = yes }
					participation_scaled_piety = 25
				}
			}
		}
	}

	on_reverse_demand = {
		log = "CB(emf_norman_border_war): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			prestige = -200
			transfer_scaled_wealth = {
				to = FROM
				value = 6.0
				min = 800
			}
		}
		any_defender = {
			limit = { character = FROM }
			participation_scaled_prestige = 100
			if = {
				limit = { emf_hostile_religion_to_ROOT = yes }
				participation_scaled_piety = 50
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_prestige = 200
				if = {
					limit = { emf_hostile_religion_to_ROOT = yes }
					participation_scaled_piety = 100
				}
			}
		}
	}

	on_invalidation = {
		log = "CB(emf_norman_border_war): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			if = { # refund
				limit = { ai = no }
				piety = 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 = 90
		modifier = {
			factor = 0.75
			ROOT = { ai = yes }
		}
	}

	# 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 = 4
		modifier = {
			factor = 2.1
			ROOT = {
				OR = {
					religion_group = FROM
					emf_syncretized_religion_with_FROM = yes
				}
			}
		}
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
	}
}

emf_revolt_religious = {
	name = EMF_REVOLT_CB_NAME_RELIGIOUS
	war_name = EMF_REVOLT_WAR_NAME_RELIGIOUS
	sprite = 7
	truce_days = 365
	hostile_against_others = yes
	is_claim_all = yes
	defender_can_call_allies = no
	can_ask_to_join_war = yes
	apply_short_occ_mod = no
	allowed_to_target_tributaries = no
	is_permanent = no

	can_use = {
		ROOT = {
			has_flag = emf_revolt_religious_leader
			emf_hostile_religion_to_FROM = yes
			any_claim = {
				tier = COUNT
				owner_under_FROM = yes
			}
		}
	}

	is_valid = {
		ROOT = {
			emf_hostile_religion_to_FROM = yes
			any_claim = {
				tier = COUNT
				owner_under_FROM = yes
			}
		}
	}

	on_add = {
		log = "CB(emf_revolt_religious): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
		hidden_tooltip = {
			ROOT = {
				any_claim = {
					limit = {
						tier = COUNT
						owner_under_FROM = yes
					}
					press_claim = ROOT
				}
			}
		}
	}

	on_success = {
		log = "CB(emf_revolt_religious): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_victory = yes
		emf_revolt_religious_cb_on_success = yes
		emf_revolt_religious_cb_cleanup = yes
		emf_check_adventurer_government = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(emf_revolt_religious): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = {
			clear_wealth = yes
			imprison = FROM
			emf_revolt_religious_cb_cleanup = yes
			hidden_tooltip = {
				add_character_modifier = {
					name = broken_spirit
					days = -1
				}
			}
		}
		FROM = {
			prestige = 50
			piety = 25
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor
					who = ROOT
					months = 1200
				}
			}
			religion_authority = { modifier = won_religious_revolt }
		}
	}

	on_reverse_demand = {
		log = "CB(emf_revolt_religious): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_holy_defeat = yes
		ROOT = {
			clear_wealth = yes
			imprison = FROM
			emf_revolt_religious_cb_cleanup = yes
			hidden_tooltip = {
				add_character_modifier = {
					name = broken_spirit
					days = -1
				}
			}
		}
		FROM = {
			prestige = 100
			piety = 50
			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_invalidation = {
		log = "CB(emf_revolt_religious): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_revolt_religious_cb_cleanup = yes
	}

	attacker_ai_victory_worth = {
		factor = 100
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 150
	}
}

emf_seljuk_adventure = {
	name = CB_NAME_DUCHY_ADVENTURE
	war_name = WAR_DUCHY_ADVENTURE
	sprite = 9
	truce_days = 365
	is_permanent = no
	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.
	battle_warscore_mult = 1.5
	capturing_attacker_is_complete_victory = no

	can_use = {
		ROOT = { has_flag = emf_cb_pass_tmp }
	}

	can_use_title = {
		tier = DUKE
	}

	is_valid_title = {
		any_direct_de_jure_vassal_title = { owner_under_FROM = yes }
	}

	on_add = {
		log = "CB(emf_seljuk_adventure): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { clr_flag = emf_cb_pass_tmp }
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(emf_seljuk_adventure): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		ROOT = {
			prestige = 250
			hidden_tooltip = { add_trait = adventurer }
		}
		FROM = { prestige = -250 }
	}

	on_success_title = {
		save_event_target_as = emf_cb_title
		emf_cb_latb = yes
		pf_liege_change_under_title_begin_effect = yes
		custom_tooltip = {
			text = other_invasion_succ_tip
			hidden_tooltip = {
				ROOT = {
					emf_cb_start_subjugation_opinions = yes
					vassalize_or_take_under_title = {
						title = PREV
						enemy = FROM
						type = host
					}
					# Usurp a county in case ROOT ended up vassalizing everyone
					random_realm_title = {
						limit = {
							tier = COUNT
							de_jure_liege_or_above = event_target:emf_cb_title
							NOT = { holder = ROOT }
							OR = {
								title = c_merv
								title = c_tus
								title = c_dihistan
							}
						}
						usurp_title_plus_barony_if_unlanded = { target = ROOT type = host }
						any_de_jure_vassal_title = {
							limit = {
								tier = BARON
								owner = {
									OR = {
										is_liege_or_above = ROOT
										under_FROM = yes
									}
								}
								OR = {
									holding_type = castle
									holding_type = temple
								}
							}
							usurp_title_plus_barony_if_unlanded = { target = ROOT type = host }
						}
					}
					# If still unlanded, don't prefer our target provinces
					if = {
						limit = { is_landed = no }
						random_realm_title = {
							limit = {
								tier = COUNT
								de_jure_liege_or_above = event_target:emf_cb_title
							}
							usurp_title_plus_barony_if_unlanded = { target = ROOT type = host }
							any_de_jure_vassal_title = {
								limit = {
									tier = BARON
									owner = {
										OR = {
											is_liege_or_above = ROOT
											under_FROM = yes
										}
									}
									OR = {
										holding_type = castle
										holding_type = temple
									}
								}
								usurp_title_plus_barony_if_unlanded = { target = ROOT type = host }
							}
						}
					}
				}
				# Usurp/create the target duchy
				usurp_title = { target = ROOT type = host }
				ROOT = {
					emf_cb_finish_subjugation_opinions = yes
				}
			}
		}
		pf_liege_change_under_title_end_effect = yes
		emf_seljuk_adventure_cb_cleanup = yes
		hidden_effect = {
			# Setup possible event targets for custom localisation in narrative event
			random_landed_title = {
				limit = {
					mercenary = yes
					ROOT = { has_flag = emf_seljuk_origin_merc_captain_of_@PREV }
				}
				ROOT = { clr_flag = emf_seljuk_origin_merc_captain_of_@PREV }
				save_event_target_as = emf_seljuk_mercenary_title
			}
			random_landed_title = {
				limit = {
					mercenary = yes
					ROOT = { has_flag = emf_seljuk_origin_merc_commander_of_@PREV }
				}
				ROOT = { clr_flag = emf_seljuk_origin_merc_commander_of_@PREV }
				save_event_target_as = emf_seljuk_merc_commander_title
			}
			random_playable_ruler = {
				limit = {
					ROOT = { has_flag = emf_seljuk_origin_former_vassal_of_@PREV }
				}
				ROOT = { clr_flag = emf_seljuk_origin_former_vassal_of_@PREV }
				save_event_target_as = emf_seljuk_former_liege
			}
			# Do it
			ROOT = {
				save_event_target_as = emf_seljuk
				narrative_event = { id = emf_seljuk.10 }
				any_player = {
					limit = { NOT = { character = PREV } }
					narrative_event = { id = emf_seljuk.10 }
				}
			}
		}
	}

	on_success_posttitle = {
		ROOT = {
			if = {
				limit = {
					prisoner = yes
					host = { ai = yes }
				}
				prisoner = no
			}
			if = {
				limit = { independent = no }
				set_defacto_liege = ROOT # no emf_liege_change nec.
			}
		}
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_seljuk_adventure): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		FROM = {
			prestige = 100
			hidden_tooltip = {
				opinion = {
					modifier = opinion_rebel_traitor
					who = ROOT
					months = 1200
				}
			}
		}
		ROOT = {
			clear_wealth = yes
			imprison = FROM
			prestige = -100
		}
		emf_seljuk_adventure_cb_cleanup = yes
	}

	on_reverse_demand = {
		log = "CB(emf_seljuk_adventure): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			transfer_scaled_wealth = {
				to = FROM
				value = 4.0
			}
		}
		FROM = { prestige = 200 }
		ROOT = {
			clear_wealth = yes
			imprison = FROM
			prestige = -200
		}
		emf_seljuk_adventure_cb_cleanup = yes
	}

	on_attacker_leader_death = {
		log = "CB(emf_seljuk_adventure): ATTACKER_DEATH: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		end_war = invalid
	}

	on_invalidation = {
		log = "CB(emf_seljuk_adventure): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_seljuk_adventure_cb_cleanup = yes
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 120
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
		additive_modifier = {
			value = -30
			NOT = { has_game_rule = { name = railroading value = passive } }
		}
	}
}

emf_seljuk_invasion = {
	name = CB_NAME_INVASION
	war_name = WAR_NAME_INVASION
	sprite = 8
	truce_days = 730
	hostile_against_others = yes
	is_permanent = no
	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
	battle_warscore_mult = 2.0
	max_attacker_battle_score = 125
	max_defender_battle_score = 150
	hostages_block_cb = no
	allowed_to_target_tributaries = yes

	can_use = {
		ROOT = {
			emf_seljuk_invasion_mission = yes
			NOT = { same_realm = FROM }
		}
	}

	can_use_title = {
		tier = KING
	}

	is_valid = {
		ROOT = { NOT = { same_realm = FROM } }
	}

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

	on_add = {
		log = "CB(emf_seljuk_invasion): START: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(emf_seljuk_invasion): VICTORY: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		set_global_flag = emf_seljuk_latest_invasion_done
		hidden_tooltip = { change_variable = { which = global_emf_seljuk_invasion_wins value = 1 } }
		if = {
			limit = { ROOT = { emf_hostile_religion_to_FROM = yes } }
			emf_cb_holy_victory = yes
		}
		else = {
			emf_cb_victory = yes
		}
		emf_imperial_decay_victory = yes
		ROOT = {
			hidden_tooltip = {
				if = {
					limit = { has_nickname = no }
					random_list = {
						10 = {
							trigger = { NOT = { father_even_if_dead = { has_nickname = nick_the_great } } }
							give_nickname = nick_the_great
						}
						10 = {
							trigger = { NOT = { father_even_if_dead = { has_nickname = nick_the_magnificent } } }
							give_nickname = nick_the_magnificent
						}
						10 = {
							trigger = { NOT = { father_even_if_dead = { has_nickname = nick_the_glorious } } }
							give_nickname = nick_the_glorious
						}
						10 = {
							trigger = { NOT = { father_even_if_dead = { has_nickname = nick_the_conqueror } } }
							give_nickname = nick_the_conqueror
						}
						20 = { }
					}
				}
			}
			prestige = 200
			participation_scaled_prestige = 200
			if = {
				limit = { emf_hostile_religion_to_FROM = yes }
				piety = 100
				participation_scaled_piety = 100
			}
		}
		any_attacker = {
			limit = { NOT = { character = ROOT } }
			hidden_tooltip = {
				participation_scaled_prestige = 200
				if = {
					limit = { emf_hostile_religion_to_FROM = yes }
					participation_scaled_piety = 100
				}
			}
		}
		FROM = { prestige = -200 }
	}

	on_success_title = {
		emf_cb_latb = yes
		pf_liege_change_under_title_begin_effect = yes
		emf_cb_start_subjugation_opinions = yes
		custom_tooltip = {
			text = other_invasion_succ_tip # tribal_invasion_succ_tip
			hidden_tooltip = {
				# On to standard affairs...
				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
					}
				}
				# If we can usurp/create the target kingdom title, do so.
				if = {
					limit = {
						owner_under_ROOT = no
						NOT = { empire = { holder = ROOT } } # Never if we already hold its de jure empire
						ROOT = { is_landed_title_allowed = PREV }
						is_de_jure_title = yes # JIC it somehow became titular during the war, then disregard it
					}
					# But first verify that we have >=51% of the title's de jure counties
					any_de_jure_vassal_title = {
						limit = { tier = COUNT }
						change_variable = { which = local_n value = 1 }
						if = {
							limit = { owner_under_ROOT = yes }
							change_variable = { which = local_ours value = 1 }
						}
					}
					divide_variable = { which = local_ours which = local_n }
					set_variable = { which = local_our_pct which = local_ours }
					multiply_variable = { which = local_our_pct value = 100 }
					log = "--> we control [local_our_pct.GetValue]% of target title [This.GetID]"
					if = {
						limit = { check_variable = { which = local_ours value >= 0.51 } }
						if = {
							limit = { has_holder = yes }
							ROOT = { any_vassal = { set_flag = emf_vassal_prior_to_usurp } }
							log = "--> usurping title: [This.GetID]"
							usurp_title_only = { target = ROOT type = usurp }
							ROOT = {
								any_vassal = {
									limit = { NOT = { has_flag = emf_vassal_prior_to_usurp } }
									log = "----> ASSERT: vassal was transferred to us despite using usurp_title_only, getting rid of them."
									if = {
										limit = { higher_tier_than = BARON }
										set_defacto_liege = THIS
									}
									else = {
										capital_holding = {
											dejure_liege_title = {
												owner = {
													set_defacto_vassal = PREVPREVPREV
												}
											}
										}
									}
									emf_liege_change = yes
								}
								any_vassal = { clr_flag = emf_vassal_prior_to_usurp }
							}
						}
						else = {
							log = "--> creating title: [This.GetID]"
							grant_title = { target = ROOT type = created }
						}
					}
				}
			}
		}
		emf_cb_finish_subjugation_opinions = yes
		pf_liege_change_under_title_end_effect = yes
	}

	on_success_posttitle = {
		hidden_tooltip = {
			FROM = {
				if = {
					limit = { ai = no }
					reverse_opinion = { who = ROOT modifier = opinion_special_truce }
				}
			}
			ROOT = {
				character_event = { id = emf_seljuk.55 days = 1 } # Check Persian Empire creation requirements
			}
		}
		emf_cb_on_success_posttitle = yes
	}

	on_fail = {
		log = "CB(emf_seljuk_invasion): WHITEPEACE: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		set_global_flag = emf_seljuk_latest_invasion_done
		hidden_tooltip = { change_variable = { which = global_emf_seljuk_invasion_losses value = 1 } }
		FROM = {
			prestige = 100
			if = {
				limit = {
					has_dlc = "Legacy of Rome"
					emf_has_roman_empire = yes
					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 = -200 }
	}

	on_reverse_demand = {
		log = "CB(emf_seljuk_invasion): DEFEAT: title=[FromFrom.GetID]: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		set_global_flag = emf_seljuk_latest_invasion_done
		hidden_tooltip = { change_variable = { which = global_emf_seljuk_invasion_losses value = 1 } }
		if = {
			limit = { FROM = { emf_hostile_religion_to_ROOT = yes } }
			emf_cb_holy_defeat = yes
		}
		else = {
			emf_cb_defeat = yes
		}
		emf_imperial_decay_loss = yes
		ROOT = {
			prestige = -400
			transfer_scaled_wealth = { to = FROM value = 4.0 }
		}
		FROM = {
			if = {
				limit = {
					has_dlc = "Legacy of Rome"
					emf_has_roman_empire = yes
					religion_group = christian
				}
				hidden_tooltip = {
					character_event = {
						id = LoR.30
						days = 12
					}
				}
			}
			prestige = 400
			participation_scaled_prestige = 400
			if = {
				limit = { emf_hostile_religion_to_ROOT = yes }
				piety = 200
				participation_scaled_piety = 200
			}
		}
		any_defender = {
			limit = { NOT = { character = FROM } }
			hidden_tooltip = {
				participation_scaled_prestige = 400
				if = {
					limit = { emf_hostile_religion_to_ROOT = yes }
					participation_scaled_piety = 200
				}
			}
		}
	}

	on_attacker_leader_death = {
		log = "CB(emf_seljuk_invasion): ATTACKER_DEATH: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	on_invalidation = {
		log = "CB(emf_seljuk_invasion): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	attacker_ai_victory_worth = {
		factor = 100
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}


emf_pagan_subjugation_religious = {
	name = CB_NAME_PAGAN_SUBJUGATION
	war_name = WAR_NAME_PAGAN_SUBJUGATION
	sprite = 8
	truce_days = 365
	is_permanent = yes
	is_holy_war = yes
	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no
	defender_unoccupied_warscore = yes
	
	sort_priority = 2000

	can_use_gui = {
		emf_cb_pagan_subjugation_can_use_gui = yes
	}

	can_use = {
		emf_cb_pagan_subjugation_basic_can_use = yes
		NOT = { ROOT = { religion = FROM } }
		# Aztecs may only use this CB to attack the reformed version of their faith:
		OR = {
			NOT = { ROOT = { religion = aztec } }
			FROM = { religion = aztec_reformed }
		}
	}

	is_valid = {
		emf_cb_pagan_subjugation_is_valid = yes
	}

	on_add = {
		log = "CB(emf_pagan_subjugation_religious): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_pagan_subjugation_on_add = yes
	}

	on_success = {
		log = "CB(emf_pagan_subjugation_religious): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_pagan_subjugation_on_success = yes
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(emf_pagan_subjugation_religious): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_pagan_subjugation_on_fail = yes
	}

	on_reverse_demand = {
		log = "CB(emf_pagan_subjugation_religious): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_pagan_subjugation_on_reverse_demand = yes
	}

	on_invalidation = {
		log = "CB(emf_pagan_subjugation_religious): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { remove_character_modifier = launched_subjugation }
	}

	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.15
			NOT = { FROM = { lower_tier_than = ROOT } }
		}
	}

	ai_will_do = {
		factor = 1
		modifier = {
			factor = 10
			FROM = { ai = no }
			had_global_flag = { flag = EMF days = 730 }
		}
		modifier = {
			factor = 0
			ROOT = {
				trigger_if = {
					limit = { emf_has_swmh = yes }
					culture = old_hungarian
				}
				trigger_else = {
					culture = hungarian
				}
				religion_group = pagan_group
				capital_scope = { kingdom = { title = k_hungary } }
			}
			NOT = { has_global_flag = emf_conquest_hungary_completed }
			has_global_flag = emf_magyar_migration_completed
			emf_alternate_start = no
		}
		modifier = {
			factor = 0
			OR = {
				ROOT = {
					top_liege = {
						trigger_if = {
							limit = { emf_has_swmh = yes }
							culture = old_hungarian
						}
						trigger_else = {
							culture = hungarian
						}
						religion_group = pagan_group
					}
				}
				FROM = {
					top_liege = {
						trigger_if = {
							limit = { emf_has_swmh = yes }
							culture = old_hungarian
						}
						trigger_else = {
							culture = hungarian
						}
						religion_group = pagan_group
					}
				}
			}
			NOT = { has_global_flag = emf_magyar_migration_completed }
			NOT = { year = 920 }
			emf_alternate_start = no
		}
	}
}

### OLDER PB STUFF IN USE BY NEW WORLD ORDER (NWO) -- RAPID CONQUEST CBS -- FROM HERE TO END OF FILE ###

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

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			show_scope_change = no
			conditional_tooltip = {
				trigger = { is_patrician = yes }
				custom_tooltip = {
					text = emf_mr_ctt_extended_cbs_unlocked
					has_game_rule = { name = mr_cb_restriction value = off }
				}
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		emf_shattered_world = yes
		OR = {
			NOT = { has_alternate_start_setting = { setting = shattered_cbs option = off } }
			has_global_flag = emf_rapid_conquest
		}
		trigger_if = {
			limit = { has_alternate_start_setting = { setting = shattered_cbs option = on_limited } }
			total_years_played < 25
		}
		trigger_else_if = {
			limit = {
				OR = {
					has_alternate_start_setting = { setting = shattered_cbs option = on_long }
					has_global_flag = emf_rapid_conquest
				}
			}
			total_years_played < 100
		}
		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_under_FROM = yes
						}
					}
					lower_tier_than = DUKE
				}
			}
			independent = yes
			OR = {
				NOT = { any_vassal = { tier = COUNT } }
				capital_scope = {
					any_neighbor_province = {
						owner_under_FROM = yes
					}
				}
			}
		}
		FROM = {
			independent = yes
			NOT = { num_of_count_titles = 3 }
			any_realm_province = {
				owner_under_PREV = yes
				any_neighbor_province = {
					owner_under_ROOT = yes
				}
			}
			NOT = { any_demesne_title = { temporary = yes } }
		}
	}

	on_add = {
		log = "CB(minor_conquest): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(minor_conquest): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		ROOT = { prestige = 100 }
		FROM = {
			prestige = -100
			any_demesne_title = {
				limit = { tier = COUNT }
				emf_cb_latb = yes
				usurp_title_plus_barony_if_unlanded = {
					target = ROOT
					type = invasion
				}
			}
			any_realm_lord = {
				any_demesne_title = {
					limit = { tier = COUNT }
					emf_cb_latb = yes
					usurp_title_plus_barony_if_unlanded = {
						target = ROOT
						type = invasion
					}
				}
			}
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(minor_conquest): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = -25 }
	}

	on_reverse_demand = {
		log = "CB(minor_conquest): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			prestige = -50
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
		}
		FROM = { prestige = 50 }
	}

	on_invalidation = {
		log = "CB(minor_conquest): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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 = {
	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
	infamy_modifier = 0

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			show_scope_change = no
			conditional_tooltip = {
				trigger = { is_patrician = yes }
				custom_tooltip = {
					text = emf_mr_ctt_extended_cbs_unlocked
					has_game_rule = { name = mr_cb_restriction value = off }
				}
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		emf_shattered_world = yes
		OR = {
			NOT = { has_alternate_start_setting = { setting = shattered_cbs option = off } }
			has_global_flag = emf_rapid_conquest
		}
		trigger_if = {
			limit = { has_alternate_start_setting = { setting = shattered_cbs option = on_limited } }
			total_years_played < 25
		}
		trigger_else_if = {
			limit = {
				OR = {
					has_alternate_start_setting = { setting = shattered_cbs option = on_long }
					has_global_flag = emf_rapid_conquest
				}
			}
			total_years_played < 100
		}
		ROOT = {
			independent = yes
			higher_tier_than = COUNT
			primary_title = { is_primary_type_title = no } # EMF: e.g., k_orthodox can't use this
		}
		FROM = {
			independent = yes
			OR = {
				tier = COUNT
				NOT = { realm_size = 10 }
			}
			any_realm_province = {
				owner_under_PREV = yes
				any_neighbor_province = {
					owner_under_ROOT = yes
				}
			}
			NOR = { # EMF: Can't subjugate higher-tier landless characters (k_orthodox, etc.) or revolters
				controls_religion = yes
				primary_title = { is_primary_type_title = yes }
				any_demesne_title = { temporary = yes }
				is_nomadic = yes
			}
		}
	}

	on_add = {
		log = "CB(subjugation): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(subjugation): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		ROOT = { prestige = 100 }
		FROM = {
			prestige = -100
			any_demesne_title = {
				limit = { NOT = { lower_tier_than = ROOT } }
				usurp_title_plus_barony_if_unlanded = {
					target = ROOT
					type = invasion
				}
			}
			emf_ROOT_subjugate_ruler = yes
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(subjugation): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = -25 }
	}

	on_reverse_demand = {
		log = "CB(subjugation): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			prestige = -50
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
		}
		FROM = { prestige = 50 }
	}

	on_invalidation = {
		log = "CB(subjugation): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

	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_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
	infamy_modifier = 0

	can_use_gui = {
		emf_cb_can_use_gui = yes
		ROOT = {
			show_scope_change = no
			conditional_tooltip = {
				trigger = { is_patrician = yes }
				custom_tooltip = {
					text = emf_mr_ctt_extended_cbs_unlocked
					has_game_rule = { name = mr_cb_restriction value = off }
				}
			}
		}
	}

	can_use = {
		emf_cb_can_use = yes
		emf_shattered_world = yes
		OR = {
			NOT = { has_alternate_start_setting = { setting = shattered_cbs option = off } }
			has_global_flag = emf_rapid_conquest
		}
		trigger_if = {
			limit = { has_alternate_start_setting = { setting = shattered_cbs option = on_limited } }
			total_years_played < 25
		}
		trigger_else_if = {
			limit = {
				OR = {
					has_alternate_start_setting = { setting = shattered_cbs option = on_long }
					has_global_flag = emf_rapid_conquest
				}
			}
			total_years_played < 100
		}
		ROOT = {
			higher_tier_than = DUKE
			independent = yes
		}
		FROM = {
			independent = yes
			tier = DUKE
			any_realm_province = {
				owner_under_PREV = yes
				any_neighbor_province = {
					owner_under_ROOT = yes
				}
			}
			NOT = { realm_size = 20 }
			NOR = {
				controls_religion = yes
				primary_title = { is_primary_type_title = yes }
				any_demesne_title = { temporary = yes }
				is_nomadic = yes
			}
		}
	}

	on_add = {
		log = "CB(subjugation_duke): START: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_on_add = yes
	}

	on_success = {
		log = "CB(subjugation_duke): VICTORY: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_victory = yes
		ROOT = { prestige = 100 }
		FROM = {
			prestige = -100
			emf_ROOT_subjugate_ruler = yes
		}
	}

	on_success_posttitle = { emf_cb_on_success_posttitle = yes }

	on_fail = {
		log = "CB(subjugation_duke): WHITEPEACE: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		ROOT = { prestige = -25 }
	}

	on_reverse_demand = {
		log = "CB(subjugation_duke): DEFEAT: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
		emf_cb_defeat = yes
		ROOT = {
			prestige = -50
			transfer_scaled_wealth = {
				to = FROM
				value = 2.0
			}
		}
		FROM = { prestige = 50 }
	}

	on_invalidation = {
		log = "CB(subjugation_duke): INVALIDATED: [Root.EMF_GetDebugName] vs. [From.EMF_GetDebugName]"
	}

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