# pagan_county_conquest
# pagan_subjugation
# pagan_holy_war
# viking_invasion
# tribal_invasion
# matriarchal_deposition_war
# patriarchal_deposition_war
# flower_war_cb
# truebelievers_bid_for_independence

pagan_county_conquest = {
	name = CB_NAME_COUNTY_CONQUEST
	war_name = WAR_NAME_COUNTY_CONQUEST
	sprite = 22
	truce_days = 1825
	sort_priority = 700

	hostile_against_others = yes
	is_permanent = yes
	check_all_titles = yes # if permanent, setting this to true will check against all of someones titles, including vassal held titles
	can_ask_to_join_war = no
	allow_distant = yes
	allowed_to_target_tributaries = no
	press_claim = yes

	can_use_gui = {
		# CK2Plus
		# Attacker
		ROOT = {
			can_use_cb_preamble = yes
		}
	}

	can_use = {
		# Attacker
		ROOT = {
			NOT = { same_realm = FROM }
			mercenary = no

			trigger_if = {
				limit = { is_nomadic = yes }

				# Defender
				FROM = {
					OR = {
						is_nomadic = yes
						is_tribal = yes
					}
				}
			}
			trigger_else = {
				is_unreformed_pagan_trigger = yes
				NOT = { religion = FROM }
			}

			# CK2Plus
			# Special Cases
			# TODO: Move this to ai_will_do?
			NOR = {
				trigger_if = {
					limit = {
						OR = {
							has_landed_title = e_ilkhanate
							has_landed_title = e_golden_horde
							has_landed_title = e_chagatai
							has_landed_title = e_mongol_empire
						}
					}

					religion = tengri_pagan
				}

				has_character_flag = horde_seljuk
				has_character_flag = horde_seljuk2
				has_character_flag = alp_arslan
				has_landed_title = e_timurids
			}
		}
	}

	can_use_title = {
		real_tier = COUNT

		holder_scope = {
			OR = {
				character = FROM
				is_vassal_or_below_of = FROM
			}
		}

		OR = {
			# Either a seafaring culture targeting a coastal province in the same sea zone area
			trigger_if = {
				limit = {
					has_global_flag = viking_age_started

					ROOT = {
						is_seafarer = yes
						NOT = { has_character_flag = no_invasions }
					}
				}

				location = {
					is_coastal = yes

					trigger_if = {
						limit = { region = custom_atlantic_zone }

						ROOT = {
							any_realm_province = {
								region = custom_atlantic_zone
								held_under_PREV = yes
							}
						}
					}
					trigger_else_if = {
						limit = { region = custom_baltic_sea_zone }

						ROOT = {
							any_realm_province = {
								region = custom_baltic_sea_zone
								held_under_PREV = yes
							}
						}
					}
					trigger_else = {
						region = custom_black_sea_zone

						ROOT = {
							any_realm_province = {
								region = custom_black_sea_zone
								held_under_PREV = yes
							}
						}
					}
					trigger_else_if = {
						limit = { region = custom_east_med_zone }

						ROOT = {
							any_realm_province = {
								region = custom_east_med_zone
								held_under_PREV = yes
							}
						}
					}
					trigger_else_if = {
						limit = { region = custom_north_sea_zone }

						ROOT = {
							any_realm_province = {
								region = custom_north_sea_zone
								held_under_PREV = yes
							}
						}
					}
					trigger_else_if = {
						limit = { region = custom_west_med_zone }

						ROOT = {
							any_realm_province = {
								region = custom_west_med_zone
								held_under_PREV = yes
							}
						}
					}
				}
			}

			# or it's a coastal province within a fairly close range
			location = {
				is_coastal = yes

				distance_from_realm = {
					who = ROOT
					value < 75
				}
			}

			# or it's neighboring
			location = {
				any_neighbor_province = {
					owner = {
						OR = {
							character = ROOT
							is_vassal_or_below_of = ROOT
						}
					}
				}
			}
		}

		# Check to ensure the target is not valid for a holy war
		dejure_liege_title = {
			ROOT = {
				NOT = {
					can_use_cb = {
						target = FROM
						casus_belli = holy_war
						thirdparty_title = PREV
						only_check_triggers = yes
					}
				}
			}
		}
	}

	is_valid = {
		# Attacker
		ROOT = {
			trigger_if = {
				limit = { is_nomadic = no }

				is_unreformed_pagan_trigger = yes

				NOR = {
					religion = FROM
					same_realm = FROM
				}
			}
		}
	}

	is_valid_title = {
		holder_scope = {
			OR = {
				character = FROM
				is_vassal_or_below_of = FROM
			}
		}
	}

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with [Root.GetHerHis] pagan county conquest of the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"

		attacker = {
			pf_tradition_plus2_effect = yes
		}

		hidden_effect = {
			destroy_tradeposts_effect = yes
		}

		usurp_title_plus_barony_if_unlanded = {
			target = attacker
			type = invasion
		}

		any_de_jure_vassal_title = { # take all baronies too
			limit = {
				has_holder = yes

				holder_scope = {
					OR = {
						character = defender
						is_vassal_or_below_of = defender
						is_vassal_or_below_of = attacker
					}

					NAND = {
						culture = attacker
						religion = attacker
					}
				}
			}

			usurp_title = attacker
		}

		attacker = {
			show_scope_change = no

			participation_scaled_prestige = 200
			participation_scaled_piety = 100

			if = {
				limit = {
					NOT = { religion = defender }
				}

				religion_authority = {
					name = won_county_conquest
				}
			}
		}

		any_attacker = {
			limit = { NOT = { character = attacker } }

			hidden_effect = {
				participation_scaled_prestige = 200
				participation_scaled_piety = 100
			}
		}

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

		if = {
			limit = {
				event_target:attacker_target = { has_quest = quest_warrior_lodge_war }
				is_quest_target_of = event_target:attacker_target
			}

			event_target:attacker_target = {
				show_scope_change = no

				tooltip = {
					clr_quest = quest_warrior_lodge_war
				}

				set_character_flag = won_war@event_target:defender_target

				# Handled by on_action event from here
			}
		}
	}

	on_fail_title = {
		attacker = {
			show_scope_change = no

			prestige = -100
			piety = -50

			hidden_effect = {
				add_character_modifier = {
					name = county_conquest_timer
					years = 5
				}
			}
		}

		defender = {
			show_scope_change = no

			participation_scaled_prestige = 50
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 50 }
		}

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

		if = {
			limit = {
				event_target:attacker_target = { has_quest = quest_warrior_lodge_war }
				is_quest_target_of = event_target:attacker_target
			}

			event_target:attacker_target = {
				show_scope_change = no

				tooltip = {
					clr_quest = quest_warrior_lodge_war
				}

				set_character_flag = white_peace@event_target:defender_target

				# Handled by on_action event from here
			}
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			prestige = -200
			piety = -100

			transfer_scaled_wealth = {
				to = defender
				value = 4.0
			}

			if = {
				limit = {
					NOT = { religion = defender }
				}

				religion_authority = {
					name = lost_county_conquest
				}
			}

			# CK2Plus
			pf_tradition_minus2_effect = yes

			hidden_effect = {
				if = {
					limit = {
						ai = no
						has_global_flag = hardcore_mode
					}

					add_character_modifier = {
						name = county_conquest_timer
						years = 10
					}
				}
				else = {
					add_character_modifier = {
						name = county_conquest_timer
						years = 5
					}
				}
			}
		}

		defender = {
			show_scope_change = no

			participation_scaled_prestige = 200
			participation_scaled_piety = 100
		}

		any_defender = {
			limit = { NOT = { character = defender } }

			hidden_effect = {
				participation_scaled_prestige = 200
				participation_scaled_piety = 100
			}
		}
	}

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

		if = {
			limit = {
				event_target:attacker_target = { has_quest = quest_warrior_lodge_war }
				is_quest_target_of = event_target:attacker_target
			}

			event_target:attacker_target = {
				show_scope_change = no

				tooltip = {
					clr_quest = quest_warrior_lodge_war
				}

				set_character_flag = lost_war@event_target:defender_target

				# Handled by on_action event from here
			}
		}
	}

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

		trigger = {
			ROOT = {
				sufficient_treasury_for_war_trigger = yes
			}

		#	trigger_if = {
		#		limit = {
		#			ROOT = {
		#				is_tribal = yes
		#			}
		#
		#			FROM = {
		#				is_nomadic = yes
		#			}
		#		}
		#
		#		any_direct_de_jure_vassal_title = {
		#			any_direct_de_jure_vassal_title = {
		#				OR = {
		#					holding_type = castle
		#					holding_type = city
		#					holding_type = tribal
		#				}
		#			}
		#		}
		#	}
		#	trigger_else_if = {
		#		limit = {
		#			ROOT = {
		#				is_nomadic = no
		#				is_tribal = no
		#			}
		#
		#			FROM = {
		#				is_nomadic = yes
		#			}
		#		}
		#
		#		any_direct_de_jure_vassal_title = {
		#			any_direct_de_jure_vassal_title = {
		#				OR = {
		#					holding_type = castle
		#					holding_type = city
		#				}
		#			}
		#		}
		#	}

			trigger_if = {
				limit = {
					ROOT = {
						has_character_flag = is_temujin
						has_landed_title = e_mongol_empire
						is_landed = yes
					}
				}

				FROM = {
					NOT = { has_landed_title = e_china_west_governor }
				}
			}

			location = {
				# Don't attack into isolated regions without bordering them
				trigger_if = {
					limit = {
						OR = {
							region = custom_perm
							de_jure_liege_or_above = d_kola
							de_jure_liege_or_above = d_karelia
						}
					}

					any_neighbor_province = {
						owner = {
							OR = {
								character = ROOT
								is_vassal_or_below_of = ROOT
							}
						}
					}
				}

				# Must have boats if not owning an adjacent province
				trigger_if = {
					limit = {
						NOT = {
							any_neighbor_province = {
								owner = {
									OR = {
										character = ROOT
										is_vassal_or_below_of = ROOT
									}
								}
							}
						}
					}

					ROOT = {
						any_realm_province = {
							is_coastal = yes
							TECH_NAVAL >= 1
							held_under_PREV = yes
						}
					}
				}
			}

			FROM = {
				trigger_if = {
					limit = { culture = hungarian }
					NOT = { has_global_flag = magyar_1066_invasion }
				}
			}
		}

	#	mult_modifier = {
	#		factor = 4.0
	#
	#		ROOT = {
	#			religion_openly_norse_or_reformed_trigger = yes
	#		}
	#
	#		OR = {
	#			empire = { title = e_scandinavia }
	#			kingdom = { title = k_novgorod }
	#		}
	#	}
	#
	#	mult_modifier = {
	#		factor = 2.0
	#
	#		ROOT = {
	#			religion_openly_norse_or_reformed_trigger = yes
	#		}
	#
	#		has_global_flag = viking_age_started
	#
	#		OR = {
	#			empire = { title = e_britannia }
	#
	#			kingdom = {
	#				OR = {
	#					title = k_brittany
	#					title = k_france
	#					title = k_frisia
	#				}
	#			}
	#		}
	#	}
	#
	#	mult_modifier = {
	#		factor = 2.0
	#
	#		ROOT = {
	#			religion_openly_norse_or_reformed_trigger = yes
	#		}
	#
	#		location = {
	#			is_coastal = yes
	#		}
	#
	#		has_global_flag = viking_age_started
	#
	#		FROM = {
	#			NOT = { religion_group = pagan_group }
	#		}
	#	}

		# CK2Plus
		mult_modifier = {
			factor = 0.1

			ROOT = {
				is_seafarer = yes
				religion_group = FROM
			}

			location = {
				OR = {
					region = custom_novgorod
					region = custom_muscovy
					region = custom_livonia
					region = custom_ruthenia

					any_neighbor_province = {
						owner = { same_realm = ROOT }
					}
				}
			}
		}

		mult_modifier = {
			factor = 1.5

			ROOT = {
				is_seafarer = yes
			}

			location = {
				OR = {
					region = custom_novgorod
					region = custom_livonia
					region = custom_ruthenia
				}
			}
		}

		mult_modifier = {
			factor = 2.0

			ROOT = {
				culture_group = east_slavic
			}

			capital_scope = {
				OR = {
					region = custom_cuman
					region = custom_volga_bulgaria
					region = custom_alania
					region = custom_crimea
					region = custom_novgorod
					region = custom_muscovy
					region = custom_ruthenia
				}
			}
		}

		mult_modifier = {
			factor = 0.1

			ROOT = {
				culture = hungarian
			}

			capital_scope = {
				OR = {
					region = custom_cuman
					region = custom_volga_bulgaria
					region = custom_alania
					region = custom_novgorod
					region = custom_muscovy
					region = custom_ruthenia
				}
			}
		}

		mult_modifier = {
			factor = 2.0

			ROOT = {
				culture = hungarian
			}

			capital_scope = {
				OR = {
					region = custom_bulgaria
					region = custom_carpathia
				}
			}
		}
	}
}

# Pagans can subjugate other pagans
# CK2Plus: Also contains reworked indian_subjugation functionality
pagan_subjugation = {
	name = CB_NAME_PAGAN_SUBJUGATION
	war_name = WAR_NAME_PAGAN_SUBJUGATION
	sprite = 8
	truce_days = 365
	sort_priority = 2000

	is_permanent = yes
	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no
	defender_unoccupied_warscore = yes
	hostile_against_others = 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 = {
		# CK2Plus
		# Attacker
		ROOT = {
			can_use_cb_preamble = yes
		}
	}

	can_use = {
		ROOT = {
			trigger_if = {
				limit = { has_dharmic_religion_trigger = yes  }

				OR = {
					culture_group = indo_aryan_group
					culture_group = dravidian_group
				}

				# Both share an Indian religion and culture group
				FROM = {
					culture_group = ROOT
					has_dharmic_religion_trigger = yes
				}
			}
			trigger_else = {
				is_unreformed_pagan_trigger = yes

				# Is either an unreformed pagan or is a reformed version of the attacker's religion
				FROM = {
					religion_group = ROOT

					trigger_if = {
						limit = { is_reformed_religion = yes }
						is_reformed_religion = ROOT
					}
				}
			}

			lower_real_tier_than = EMPEROR
			NOT = { same_realm = FROM }
			is_nomadic = no # Should use nomadic subjugation instead
			has_horde_culture = no # Should use tribal invasion instead
			mercenary = no

			trigger_if = {
				limit = { has_character_modifier = launched_subjugation }
				has_ambition = obj_become_king
			}
		}

		FROM = {
			in_revolt = no
			is_nomadic = no

			NOR = {
				is_offmap_governor = yes

				# Not already a defender in a subjugation war
				any_war = {
					defender = { character = PREVPREV }

					OR = {
						using_cb = pagan_subjugation
						using_cb = nomad_subjugation
					}
				}
			}
		}
	}

	can_use_title = {
		# Attacker
		ROOT = {
			capital_scope = {
				kingdom = { title = PREVPREVPREV }
			}

			OR = {
				has_landed_title = PREV
				lower_real_tier_than = KING
			}
		}

		# Defender
		FROM = {
			num_of_realm_counties = {
				value >= 1
				title = PREV
			}
		}
	}

	is_valid_title = {
		FROM = {
			num_of_realm_counties = {
				value >= 1
				title = PREV
			}
		}
	}

	on_add = {
		attacker = {
			show_scope_change = no

			if = {
				limit = { has_ambition = obj_become_king }
				custom_tooltip = { text = PAGAN_SUBJ_NO_MODIFIER }
			}
			else = {
				custom_tooltip = {
					text = PAGAN_SUBJ_ADD_MODIFIER

					add_character_modifier = {
						name = launched_subjugation
						years = 100
						hidden = yes
					}
				}
			}
		}

		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_success = {
		attacker = {
			show_scope_change = no

			piety = 100
			participation_scaled_prestige = 200
		}

		any_attacker = {
			limit = { NOT = { character = attacker } }
			hidden_effect = { participation_scaled_prestige = 200 }
		}

		defender = {
			show_scope_change = no

			piety = -100
			prestige = -200

		#	if = {
		#		limit = {
		#			is_reformed_religion = attacker
		#			controls_religion = no
		#		}
		#
		#		religion = attacker # Reformed pagans are forced to convert back
		#	}

			custom_tooltip = {
				text = tribal_subjugation_tip

				opinion = {
					name = opinion_subjugated
					who = attacker
				}

				any_vassal = {
					if = {
						limit = {
							OR = {
								religion = attacker
								is_reformed_religion = attacker
							}
						}

						opinion = {
							name = opinion_subjugated
							who = attacker
						}
					}
					else = {
						opinion = {
							name = opinion_subjugated_other_rel
							who = attacker
						}
					}
				}

				any_demesne_title = {
					limit = {
						OR = {
							higher_real_tier_than = attacker
							real_tier = attacker
						}
					}

					attacker = {
						usurp_title = {
							target = PREV
							type = invasion
						}
					}

					add_pressed_claim = PREV
				}

				set_defacto_liege = attacker
			}
		}
	}

	# CK2Plus
	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with [Root.GetHerHis] pagan subjugation of the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"

		# Usurp the target kingdom itself, if the enemy holds it
		if = {
			limit = { holder = defender }

			usurp_title = {
				target = attacker
				type = invasion
			}
		}

		# Take the enemy's duchies within the target kingdom, if we are the same or lower tier
		defender = {
			show_scope_change = no

			if = {
				limit = {
					NOT = { lower_real_tier_than = FROM }
				}
			}

			any_demesne_title = {
				show_scope_change = no

				limit = {
					real_tier = DUKE

					OR = {
						title = PREVPREV
						de_jure_liege_or_above = PREVPREV
					}
				}

				usurp_title = {
					target = attacker
					type = invasion
				}
			}
		}

		# Have I become the vassal of the loser due to taking a duchy or kingdom?
		# TODO: Ensure this can't set an attacker independent if originally a vassal in another realms
		attacker = {
			show_scope_change = no

			if = {
				limit = { is_vassal_or_below_of = defender }

				set_defacto_liege = ROOT
				pf_liege_change_effect = yes
			}
		}

		# Vassalize the rest
		custom_tooltip = {
			text = pagan_subjugation_tip

			ROOT = {
				subjugate_or_take_under_title = { # If the target only has territory within the kingdom, he is simply vassalized
					title = PREV
					enemy = FROM
				}
			}
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			piety = -100
		}

		defender = {
			show_scope_change = no

			participation_scaled_prestige = 100
			piety = 50
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 100 }
		}
	}

	on_invalidation = {
		attacker = {
			show_scope_change = no

			remove_character_modifier = launched_subjugation
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			piety = -200

			transfer_scaled_wealth = {
				to = defender
				value = 2.0
			}
		}

		defender = {
			show_scope_change = no

			participation_scaled_prestige = 200
			piety = 100
			roman_victory_triumph_effect = yes
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 200 }
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = {
		factor = 1

		# CK2Plus
		trigger = {
			ROOT = {
				# The AI will only attack neighbors (or those very close)
				OR = {
					shares_realm_border_with = FROM
					distance_from_realm = { who = FROM value < 75 }
				}

				sufficient_treasury_for_war_trigger = yes
			}

			FROM = {
				trigger_if = {
					limit = { culture = hungarian }
					NOT = { has_global_flag = magyar_1066_invasion }
				}
			}
		}

		coronation_factor = yes

	#	Below probably not needed to due attacker requiring a province in the target kingdom
	#	modifier = {
	#		factor = 0
	#
	#		FROM = {
	#			NOT = {
	#				any_realm_province = {
	#					kingdom = { title = PREVPREVPREV }
	#					held_under_PREV = yes
	#
	#					any_neighbor_province = {
	#						owner = {
	#							same_realm = ROOT
	#						}
	#					}
	#				}
	#			}
	#		}
	#
	#		ROOT = {
	#			NOT = {
	#				any_realm_province = {
	#					is_coastal = yes
	#					TECH_NAVAL >= 1
	#					held_under_PREV = yes
	#				}
	#			}
	#		}
	#	}

		# Try to guide the Hordes in the right direction
		modifier = {
			factor = 0.1

			ROOT = {
				OR = {
					dynasty = 613 # Seljuks
					dynasty = 800 # Timurids
					primary_title = { title = e_ilkhanate }
				}
			}

			OR = {
				location = {
					NOR = {
						region = world_europe_south_east
						region = world_asia_minor
						region = world_middle_east
						region = world_persia
					}
				}

				FROM = {
					primary_title = { title = e_golden_horde }
					year < 1300
				}
			}
		}

		mult_modifier = {
			factor = 0.1

			ROOT = { culture = hungarian }

			capital_scope = {
				OR = {
					region = custom_cuman
					region = custom_volga_bulgaria
					region = custom_alania
					region = custom_novgorod
					region = custom_muscovy
					region = custom_ruthenia
				}
			}
		}

		mult_modifier = {
			factor = 0.1

			ROOT = { culture = khazar }

			capital_scope = {
				OR = {
					region = world_steppe_tarim
					region = world_steppe_east
					region = custom_cuman
					region = custom_perm
					region = custom_volga_bulgaria
				}
			}
		}

		mult_modifier = {
			factor = 2.0

			ROOT = { culture = hungarian }

			capital_scope = {
				OR = {
					region = custom_bulgaria
					region = custom_carpathia
				}
			}
		}
	}
}

# Disabled in CK2Plus - see 'holy_war'
pagan_holy_war = {
	name = CB_NAME_RELIGIOUS
	war_name = WAR_NAME_RELIGIOUS
	sprite = 9
	truce_days = 1825
	sort_priority = 800

	is_permanent = no # Disabled in CK2Plus
	is_holy_war = yes
	check_de_jure_tier = DUKE # this scans all dejure duchies for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	allowed_to_target_tributaries = no

	can_use_gui = {
		# Attacker
		ROOT = {
			piety >= 100
		}
	}

	can_use = {
		always = no # Disabled in CK2Plus
		# Attacker
		ROOT = {
			religion_group = pagan_group

			trigger_if = {
				limit = { is_reformed_religion = no }
				religion = aztec_pagan
			}

			religion = top_liege

			NOR = {
				religion = FROM
				same_realm = FROM
				has_religion_feature = religion_peaceful
			}

			mercenary = no
		}
	}

	can_use_title = {
		# The attacker needs at least one county in the target kingdom, or a border, or be only two sea zones away from one of your counties
		any_direct_de_jure_vassal_title = {
			OR = {
				holder_scope = { # part of attacker's realm
					OR = {
						character = ROOT
						is_vassal_or_below_of = ROOT
					}
				}

				location = {
					any_neighbor_province = { # bordering province
						trigger_if = {
							limit = { has_owner = yes }

							owner = { # part of attacker's realm
								OR = {
									character = ROOT
									is_vassal_or_below_of = ROOT
								}
							}
						}
						trigger_else = {
							is_land = no # first sea zone

							any_neighbor_province = {
								trigger_if = {
									limit = { has_owner = yes }

									owner = { # part of attacker's realm
										OR = {
											character = ROOT
											is_vassal_or_below_of = ROOT
										}
									}
								}
								trigger_else = {
									is_land = no # second sea zone

									any_neighbor_province = {
										owner = { # part of attacker's realm
											OR = {
												character = ROOT
												is_vassal_or_below_of = ROOT
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}

	is_valid = {
		# Attacker
		ROOT = {
			is_reformed_religion = yes

			NOR = {
				same_realm = FROM
				religion = FROM
				top_liege = { religion = FROM }
			}
		}
	}

	on_add = {
		attacker = {
			show_scope_change = no
			piety = -100
		}

		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_success = {
		attacker = {
			show_scope_change = no

			participation_scaled_piety = 100
			participation_scaled_prestige = 50

			religion_authority = {
				name = won_holy_war
				years = 20
			}

			hidden_effect = {
				if = {
					limit = { has_ambition = obj_strengthen_religion }

					change_variable = {
						which = strengthen_religion
						value = 1
					}
				}

				if = {
					limit = {
						trigger_if = {
							limit = { has_nickname = yes }
							this_has_lame_nickname_trigger = yes
						}
					}

					random_list = {
						10 = { give_nickname = nick_the_dragon }
						10 = { give_nickname = nick_the_holy }
						10 = { give_nickname = nick_bloodaxe }
						10 = { give_nickname = nick_the_great }
						40 = { give_nickname = nick_the_sword_of_god }
					}
				}
			}
		}

		any_attacker = {
			limit = { NOT = { character = attacker } }

			hidden_effect = {
				participation_scaled_piety = 100
				participation_scaled_prestige = 50

				if = {
					limit = { has_ambition = obj_strengthen_religion }

					change_variable = {
						which = strengthen_religion
						value = 1
					}
				}
			}
		}

		defender = {
			show_scope_change = no

			prestige = -50
			piety = -100

			religion_authority = {
				name = lost_holy_war
				years = 20
			}
		}
	}

	on_success_title = {
		attacker = {
			show_scope_change = no

			custom_tooltip = {
				text = religious_cb_succ_tip

				vassalize_or_take_under_title = {
					title = PREV
					enemy = defender
					same_religion = yes # Only vassalize rulers of my religion
					is_religious = yes
					type = holy_war
				}
			}
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			piety = -50
			prestige = -100
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			transfer_scaled_wealth = {
				to = defender
				value = 4.0
			}

			religion_authority = {
				name = lost_holy_war
				years = 20
			}
		}

		defender = {
			show_scope_change = no

			participation_scaled_piety = 200
			participation_scaled_prestige = 100

			religion_authority = {
				name = won_holy_war
				years = 20
			}

			hidden_effect = {
				if = {
					limit = { has_ambition = obj_strengthen_religion }

					change_variable = {
						which = strengthen_religion
						value = 1
					}
				}
			}
		}

		any_defender = {
			limit = { NOT = { character = defender } }

			hidden_effect = {
				participation_scaled_piety = 200
				participation_scaled_prestige = 100

				if = {
					limit = { has_ambition = obj_strengthen_religion }

					change_variable = {
						which = strengthen_religion
						value = 1
					}
				}
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = {
		factor = 0.9 # Prefer using CB's without on_add costs

		trigger = {
			trigger_if = {
				limit = {
					ROOT = {
						is_tribal = yes
					}

					FROM = {
						is_nomadic = yes
					}
				}

				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
							holding_type = tribal
						}
					}
				}
			}
			trigger_else_if = {
				limit = {
					ROOT = {
						is_nomadic = no
						is_tribal = no
					}

					FROM = {
						is_nomadic = yes
					}

				}

				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}
				}
			}
		}

		mult_modifier = {
			factor = 0.1

			ROOT = {
				culture = hungarian

				primary_title = {
					OR = {
						title = k_hungary
						title = e_carpathia
					}
				}
			}

			empire = {
				OR = {
					title = e_tartaria
					title = e_rus
				}
			}
		}
	}
}

# The Prepared Invasion CB of Norse Heathens
viking_invasion = {
	name = CB_NAME_VIKING_INVASION
	war_name = WAR_NAME_VIKING_INVASION
	sprite = 20
	truce_days = 365
	sort_priority = 3000

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

	can_use_gui = {
		# CK2Plus
		# Attacker
		ROOT = {
			can_use_cb_preamble = yes
		}
	}

	can_use = {
		# Attacker
		ROOT = {
			has_opinion_modifier = {
				name = preparing_to_invade
				who = FROM
			}

			NOT = { is_vassal_or_below_of = FROM }
			mercenary = no
		}
	}

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

	is_valid_title = {
		# Defender
		FROM = {
			num_of_realm_counties = {
				value >= 1
				title = PREV
			}
		}
	}

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with [Root.GetHerHis] viking invasion for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"

		custom_tooltip = {
			text = tribal_invasion_succ_tip

			# Attacker gains one province belonging to the target for certain
			capital_scope = {
				if = {
					limit = { owned_by = FROM }

					culture = ROOT
					religion = ROOT
				}

				else = {
					defender = {
						random_demesne_province = {
							limit = {
								de_jure_liege_or_above = PREVPREVPREV
								owned_by = PREV
							}

							culture = ROOT
							religion = ROOT
						}
					}
				}
			}

			attacker = {
				occupy_minors_of_occupied_settlements = defender

				gain_all_occupied_titles = {
					who = defender
					type = invasion
				}

				vassalize_or_take_under_title_destroy_duchies = {
					title = PREV
					enemy = defender
					type = invasion
					is_crusade = yes
				}

				# Settle the special invasion forces
				disband_event_forces = prepared_invasion

				any_demesne_title = {
					limit = { real_tier = BARON }

					remove_holding_modifier = recently_conquered
					refill_holding_levy = yes
				}
			}

			defender = {
				if = {
					limit = { is_vassal_or_below_of = attacker }
					imprison = attacker
				}
			}
		}

		attacker = {
			show_scope_change = no

			prestige = 500
			participation_scaled_prestige = 300
			piety = 250

			religion_authority = {
				name = won_viking_invasion
			}

			hidden_effect = {
				if = {
					limit = { has_nickname = no }

					random_list = {
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_victorious }
						10 = { give_nickname = nick_the_fearless }
						10 = { give_nickname = nick_the_brave }
						10 = { give_nickname = nick_haardraade }
						10 = { give_nickname = nick_the_viking }
						10 = { give_nickname = nick_the_dragon }
						10 = { give_nickname = nick_the_ironside }
					}
				}
			}
		}

		any_attacker = {
			limit = { NOT = { character = attacker } }
			hidden_effect = { participation_scaled_prestige = 300 }
		}

		defender = {
			show_scope_change = no

			prestige = -500
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			prestige = -300
			piety = -100

			religion_authority = {
				name = lost_viking_invasion
			}

			hidden_effect = {
				disband_event_forces = prepared_invasion
			}
		}

		defender = {
			show_scope_change = no

			prestige = 200
			participation_scaled_prestige = 200
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 200 }
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			prestige = -500
			piety = -250

			transfer_scaled_wealth = {
				to = defender
				value = 2.0
			}

			religion_authority = {
				name = lost_viking_invasion
			}

			hidden_effect = {
				disband_event_forces = prepared_invasion
			}
		}

		defender = {
			show_scope_change = no

			prestige = 300
			participation_scaled_prestige = 250
			roman_victory_triumph_effect = yes
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 250 }
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	# CK2Plus
	ai_will_do = {
		factor = 1

		trigger = {
			ROOT = {
				sufficient_treasury_for_war_trigger = yes
				NOT = { has_character_flag = no_invasions }
			}
		}

		modifier = {
			factor = 0.9

			capital_scope = {
				NOR = {
					region = custom_north_sea
					region = custom_england
					region = custom_scotland
					region = world_europe_west_ireland
					region = custom_wales
					region = custom_livonia
					region = custom_muscovy
					region = custom_novgorod
					region = custom_ruthenia
					region = custom_volga_bulgaria
				}
			}
		}
	}
}

tribal_invasion = {
	name = CB_NAME_TRIBAL_INVASION
	war_name = WAR_NAME_TRIBAL_INVASION
	sprite = 8
	truce_days = 1825
	sort_priority = 2000

	is_permanent = yes
	hostile_against_others = yes
	can_ask_to_join_war = yes
	check_de_jure_tier = KING # this scans all de jure kingdoms for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	allowed_to_target_tributaries = no

	can_use_gui = {
		# Attacker
		ROOT = {
			prestige >= 500

			# CK2Plus
			can_use_cb_preamble = yes
		}

		check_if_crusader_trigger = yes
	}

	can_use = {
		# Attacker
		ROOT = {
			NOT = { same_realm = FROM }
			is_nomadic = no

			trigger_if = {
				limit = {
					ai = no
					multiplayer = yes
				}

				NOT = {
					has_game_rule = {
						name = multiplayer_invasions
						value = off
					}
				}
			}

			OR = {
				AND = {
					independent = yes
					higher_real_tier_than = COUNT

					OR = {
						culture_group = altaic
						culture_group = magyar
						culture_group = mesoamerican
					}

					OR = {
						is_tribal = yes
						has_law = succ_elective_gavelkind

						trigger_if = {
							limit = { has_dlc = "Horse Lords" }

							OR = {
								has_landed_title = e_mongol_empire
								has_landed_title = e_aztec_empire
							}
						}
					}

					trigger_if = {
						limit = { has_horde_culture = yes }

						NOT = { religion_group = christian }
					}
					trigger_else = {
						religion_group = pagan_group

						# Should subjugate these instead
						FROM = {
							NOT = { religion_group = pagan_group }
						}
					}
				}

				AND = {
					independent = yes
					higher_real_tier_than = COUNT
					has_character_flag = horde_invader

					trigger_if = {
						limit = {
							NOT = { has_dlc = "Horse Lords" }
						}

						NOT = { religion_group = christian }
					}
				}

				AND = {
					mercenary = yes
					num_of_titles < 2
				}

				AND = {
					has_character_flag = is_child_of_destiny

					any_owned_bloodline = {
						has_bloodline_flag = founder_invasions
						bloodline_is_active_for = PREV
					}
				}

				AND = {
					NOT = { has_character_flag = flag_declared_bloodline_invasion }

					any_owned_bloodline = {
						has_bloodline_flag = bloodline_great_conquerors
						bloodline_is_active_for = PREV
					}
				}
			}
		}

		# The attacker needs at least one county in the target kingdom, or a border
		any_de_jure_vassal_title = {
			real_tier = COUNT

			location = {
				OR = {
					owner = {
						OR = {
							character = ROOT
							is_vassal_or_below_of = ROOT
						}
					}

					any_neighbor_province = {
						owner = {
							OR = {
								character = ROOT
								is_vassal_or_below_of = ROOT
							}
						}
					}
				}
			}
		}
	}

	is_valid_title = {
		# Defender
		FROM = {
			num_of_realm_counties = {
				value >= 1
				title = PREV
			}
		}
	}

	on_add = {
		attacker = {
			show_scope_change = no

			prestige = -500

			if = {
				limit = {
					any_owned_bloodline = {
						has_bloodline_flag = bloodline_great_conquerors
						bloodline_is_active_for = PREV
					}
				}

				set_character_flag = flag_declared_bloodline_invasion
			}
		}

		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_success = {
		attacker = {
			show_scope_change = no

			prestige = 300
			participation_scaled_prestige = 300

			hidden_effect = {
				occupy_minors_of_occupied_settlements = defender

				gain_all_occupied_titles = {
					who = defender
					type = invasion
				}

				if = {
					limit = { has_nickname = no }

					random_list = {
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_victorious }
						10 = { give_nickname = nick_the_conqueror }
						10 = { give_nickname = nick_the_brave }
						10 = { give_nickname = nick_the_dragon }
						10 = { give_nickname = nick_the_terrible }
					}
				}
			}
		}

		any_attacker = {
			limit = { NOT = { character = attacker } }
			hidden_effect = { participation_scaled_prestige = 300 }
		}

		defender = {
			show_scope_change = no

			prestige = -300
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with [Root.GetHerHis] Tribal Invasion CB of the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"

		attacker = {
			show_scope_change = no

			if = {
				limit = {
					OR = {
						character = c_159136 # rpd
						character = c_159137 # lmos
					}

					has_dlc = "Holy Fury"

					NOT = {
						any_owned_bloodline = {
							has_bloodline_flag = arpad_bloodline
						}
					}
				}

				create_bloodline = {
					type = arpad
				}
			}
		}

		custom_tooltip = {
			text = tribal_invasion_succ_tip

			attacker = {
				vassalize_or_take_under_title_destroy_duchies = {
					title = PREV
					enemy = defender
					is_crusade = yes # Even if the title holder is not participating in the war, gain holdings occupied by all Crusade participants
					type = invasion
				}

				if = {
					limit = { has_landed_title = e_aztec_empire }

					any_realm_province = {
						limit = {
							OR = {
								has_province_flag = aztec_explorers
								has_province_flag = aztec_second_wave
							}

							held_under_PREV = yes
						}

						culture = nahua
						religion = aztec_pagan
						clr_province_flag = aztec_explorers
						clr_province_flag = aztec_second_wave
					}
				}
			}
		}

		hidden_effect = {
			check_if_reconquista_finished_effect = yes

			defender = {
				landless_pope_effect = yes
			}
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			prestige = -150
		}

		defender = {
			show_scope_change = no

			prestige = 150
			participation_scaled_prestige = 150

			hidden_effect = {
				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 = defender } }
			hidden_effect = { participation_scaled_prestige = 150 }
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			prestige = -300

			transfer_scaled_wealth = {
				to = defender
				value = 4.0
			}
		}

		defender = {
			show_scope_change = no

			prestige = 300
			participation_scaled_prestige = 300

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

				roman_victory_triumph_effect = yes
			}
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 300 }
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = {
		factor = 0.9 # Prefer using CB's without on_add costs

		trigger = {
			FROM = {
				sufficient_treasury_for_war_trigger = yes
			}

			trigger_if = {
				limit = {
					ROOT = {
						is_tribal = yes
					}

					FROM = {
						is_nomadic = yes
					}
				}

				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
							holding_type = tribal
						}
					}
				}
			}
			trigger_else_if = {
				limit = {
					ROOT = {
						is_nomadic = no
						is_tribal = no
					}

					FROM = {
						is_nomadic = yes
					}

				}

				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}
				}
			}

			trigger_if = {
				limit = {
					ROOT = {
						has_character_flag = is_temujin
						has_landed_title = e_mongol_empire
						is_landed = yes
					}
				}

				FROM = {
					NOT = { has_landed_title = e_china_west_governor }
				}
			}

			# This ensures that the AI doesn't declare this type of war against someone who only holds a few counties within the targeted title.
			trigger_if = {
				limit = {
					# Attacker
					ROOT = {
						OR = {
							has_landed_title = e_mongol_empire
							has_landed_title = e_aztec_empire
						}
					}
				}

				# Defender
				FROM = {
					num_of_realm_counties = {
						value >= 3
						title = PREV
					}
				}
			}
		}

		coronation_factor = yes

		# TODO: Review these
		# Try to guide the Hordes in the right direction
		mult_modifier = {
			factor = 0.1

			attacker = {
				OR = {
					dynasty = 613 # Seljuks
					dynasty = 800 # Timurids

					primary_title = {
						title = e_ilkhanate
					}
				}
			}

			OR = {
				empire = {
					NOR = {
						title = e_persia
						title = e_byzantium
						title = e_arabia
					}
				}

				defender = {
					primary_title = {
						title = e_golden_horde
					}

					year < 1300
				}

				# CK2Plus
				defender = {
					primary_title = { title = e_chagatai }
					year < 1300
				}
			}
		}

		mult_modifier = {
			factor = 0.1

			attacker = {
				primary_title = {
					title = e_golden_horde
				}
			}

			OR = {
				empire = {
					OR = {
						title = e_persia
						title = e_byzantium
						title = e_arabia
					}
				}

				defender = {
					primary_title = {
						title = e_ilkhanate
					}

					year < 1300
				}
			}
		}

		mult_modifier = {
			factor = 0.1

			attacker = {
				culture = hungarian

				primary_title = {
					OR = {
						title = k_hungary
						title = e_carpathia
					}
				}
			}

			empire = {
				OR = {
					title = e_tartaria
					title = e_rus
				}
			}
		}

		# CK2Plus
		# Prefer bigger targets
		mult_modifier = {
			factor = 1.5
			attacker = { real_tier = KING }
		}

		mult_modifier = {
			factor = 2.0
			attacker = { real_tier = EMPEROR }
		}

		mult_modifier = {
			factor = 2.0

			attacker = {
				OR = {
					dynasty = 613 # Seljuks
					dynasty = 800 # Timurids
					primary_title = { title = e_ilkhanate }

					trigger_if = {
						limit = {
							NOT = {
								any_realm_province = {
									region = custom_persia
									held_under_PREV = yes
								}
							}
						}

						primary_title = { title = e_mongol_empire }
					}
				}
			}

			capital_scope = { region = custom_persia }
		}

		mult_modifier = {
			factor = 2.0

			attacker = {
				primary_title = { title = e_golden_horde }
			}

			capital_scope = {
				OR = {
					region = custom_cuman
					region = custom_perm
					region = custom_volga_bulgaria
				}
			}
		}

		mult_modifier = {
			factor = 1.5

			attacker = {
				primary_title = { title = e_mongol_empire }

				NOT = {
					any_realm_province = {
						region = custom_cuman
						held_under_PREV = yes
					}
				}
			}

			capital_scope = { region = custom_cuman }
		}

		mult_modifier = {
			factor = 2.0

			attacker = {
				primary_title = {
					title = e_mongol_empire
				}
			}

			capital_scope = { region = custom_khwarizm }
		}

		mult_modifier = {
			factor = 0.1

			attacker = {
				primary_title = {
					title = e_golden_horde
				}
			}

			OR = {
				capital_scope = {
					OR = {
						region = custom_persia
						region = custom_baluchistan
						region = custom_afghanistan
						region = custom_mesopotamia
						region = custom_azerbaijan
						region = custom_armenia
						region = custom_trebizond
						region = custom_nikaea
						region = custom_byzantium
						region = custom_norway
						region = custom_sweden
						region = custom_syria
						region = custom_arabia
						region = custom_jerusalem
						region = custom_egypt
						region = custom_africa
						region = world_india_bengal
						region = world_india_deccan
						region = world_india_rajastan
					}
				}

				defender = {
					primary_title = { title = e_ilkhanate }
					year < 1300
				}

				defender = {
					primary_title = { title = e_chagatai }
					year < 1300
				}
			}
		}

		mult_modifier = {
			factor = 0.1

			attacker = {
				primary_title = { title = e_chagatai }
			}

			OR = {
				capital_scope = {
					NOR = {
						region = custom_baluchistan
						region = custom_afghanistan
						region = world_india_bengal
						region = world_india_deccan
						region = world_india_rajastan
					}
				}

				defender = {
					primary_title = { title = e_ilkhanate }
					year < 1300
				}

				defender = {
					primary_title = { title = e_chagatai }
					year < 1300
				}
			}
		}

		mult_modifier = {
			factor = 0.1

			attacker = { culture = hungarian }

			capital_scope = {
				OR = {
					region = custom_cuman
					region = custom_volga_bulgaria
					region = custom_alania
					region = custom_crimea
					region = custom_novgorod
					region = custom_muscovy
					region = custom_ruthenia
					region = custom_byzantium
				}
			}
		}

		mult_modifier = {
			factor = 0.1

			attacker = { culture = khazar }

			capital_scope = {
				OR = {
					region = world_steppe_tarim
					region = world_steppe_east
					region = custom_cuman
					region = custom_perm
					region = custom_volga_bulgaria
				}
			}
		}

		mult_modifier = {
			factor = 1.5

			attacker = { culture = hungarian }

			capital_scope = {
				OR = {
					region = custom_bulgaria
					region = custom_carpathia
					region = custom_croatia
				}
			}
		}

		mult_modifier = {
			factor = 0.1

			attacker = {
				primary_title = {
					OR = {
						title = e_mongol_empire
						title = e_chagatai
						title = e_golden_horde
						title = e_ilkhanate
					}
				}

				culture = FROM
			}
		}

		mult_modifier = {
			factor = 5.0
			attacker = { trait = bloodlust }
		}
	}
}

# Warmongering + Matriarchy = Sex Deposition cb
matriarchal_deposition_war = {
	name = CB_NAME_MATRIARCHAL_DEPOSITION
	war_name = WAR_NAME_SEX_DEPOSITION
	sprite = 35
	truce_days = 3650
	sort_priority = 4000

	is_permanent = no # Disabled in CK2Plus
	can_ask_to_join_war = yes
	is_holy_war = yes
	allowed_to_target_tributaries = no

	can_use = {
		has_dlc = "Holy Fury"

		ROOT = {
			has_religion_feature = religion_matriarchal
			has_religion_feature = religion_warlike
			higher_real_tier_than = DUKE
			shares_realm_border_with = FROM
			religion = top_liege
			is_female = yes
			is_landed = yes
			is_nomadic = no
			mercenary = no
			NOT = { same_realm = FROM }
		}

		FROM = {
			is_nomadic = no
			is_merchant_republic = no
			is_theocracy = no
			is_female = no
			independent = yes
		}
	}

	is_valid = {
		# Attacker
		ROOT = {
			has_religion_feature = religion_matriarchal
			has_religion_feature = religion_warlike
			higher_real_tier_than = DUKE
			is_female = yes
			NOT = { same_realm = FROM }
		}

		FROM = {
			independent = yes
			is_female = no
			is_nomadic = no
			is_merchant_republic = no
			is_theocracy = no
		}
	}

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_success = {
		attacker = {
			show_scope_change = no

			participation_scaled_piety = 100
			participation_scaled_prestige = 150

			if = {
				limit = { uses_decadence = yes }
				participation_scaled_decadence = -10
			}

			religion_authority = {
				name = won_holy_war
				years = 20
			}
		}

		any_attacker = {
			limit = { NOT = { character = attacker } }

			hidden_effect = {
				participation_scaled_piety = 50
				participation_scaled_prestige = 25

				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -10
				}

				if = {
					limit = { has_ambition = obj_strengthen_religion }

					change_variable = {
						which = strengthen_religion
						value = 1
					}
				}
			}
		}

		defender = {
			show_scope_change = no
			piety = -100
			prestige = -50

			custom_tooltip = {
				text = sex_deposition_cb_succ_tip

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

						preferred_limit = {
							is_close_relative = PREV
							is_adult = yes
							religion = attacker
						}

						preferred_limit = {
							is_adult = yes
							religion = attacker
						}

						preferred_limit = {
							is_close_relative = PREV
							is_adult = yes
						}

						preferred_limit = {
							is_adult = yes
						}

						save_event_target_as = new_swapped_ruler

						usurp_title_plus_barony_if_unlanded = {
							target = event_target:title_usurped
							type = invasion
						}

						event_target:scoped_loser = {
							any_demesne_title = {
								limit = { real_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
						}
					}
				}
				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 = attacker
							is_adult = yes
							practical_age < 30
							trait = zealous
						}

						preferred_limit = {
							religion = attacker
							is_adult = yes
							trait = zealous
						}

						preferred_limit = {
							religion = attacker
							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_plus_barony_if_unlanded = {
							target = event_target:title_usurped
							type = invasion
						}

						event_target:scoped_loser = {
							any_demesne_title = {
								limit = { real_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
						}
					}
				}
				else = {
					location = {
						create_character = {
							religion = attacker
							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 = {
							remove_trait = cynical
							wealth = 250
							prestige = 300
							piety = 100
							save_event_target_as = new_swapped_ruler

							usurp_title_plus_barony_if_unlanded = {
								target = event_target:title_usurped
								type = invasion
							}

							event_target:scoped_loser = {
								any_demesne_title = {
									limit = { real_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
							}
						}
					}
				}

				# Titles assigned, now onto laws
				if = {
					limit = {
						attacker = {
							has_religion_feature = religion_matriarchal
						}

						event_target:new_swapped_ruler = {
							NOR = {
								has_religion_feature = religion_patriarchal
								has_religion_feature = religion_equal
								has_religion_feature = religion_feature_bon
							}
						}
					}

					event_target:new_swapped_ruler = {
						any_demesne_title = {
							set_title_flag = non_manual_law_change # No prestige loss

							add_law = {
								law = enatic_succession
								cooldown = no
								opinion_effect = no
							}

							add_law = {
								law = status_of_women_4
								cooldown = no
								opinion_effect = no
							}

							clr_title_flag = non_manual_law_change # No prestige loss
						}

						recalc_succession = yes
					}
				}

				# Laws set, now onto pacts
				if = {
					limit = {
						has_dlc = "Jade Dragon"
						is_tributary = no
					}

					attacker = {
						make_tributary = {
							who = event_target:new_swapped_ruler
							tributary_type = permanent
						}
					}
				}
				else_if = {
					limit = {
						has_dlc = "Horse Lords"
						is_tributary = no
					}

					attacker = {
						make_tributary = {
							who = event_target:new_swapped_ruler
							percentage = 0.3
						}
					}
				}
				else = {
					attacker = {
						opinion = {
							name = 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_fail = {
		attacker = {
			show_scope_change = no

			piety = -125
			prestige = -250
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			transfer_scaled_wealth = {
				to = defender
				value = 4.0
			}

			piety = -250
			prestige = -500

			religion_authority = {
				name = lost_holy_war
				years = 20
			}

			if = {
				limit = { uses_decadence = yes }
				participation_scaled_decadence = 20
			}
		}
		any_attacker = {
			limit = {
				NOT = { character = attacker}
				uses_decadence = yes
			}

			hidden_effect = {
				participation_scaled_decadence = 20
			}
		}

		defender = {
			show_scope_change = no

			participation_scaled_piety = 200
			participation_scaled_prestige = 100

			if = {
				limit = { uses_decadence = yes }
				participation_scaled_decadence = -10
			}

			if = {
				limit = { NOT = { has_religion_feature = religion_matriarchal } }

				religion_authority = {
					name = won_holy_war
					years = 20
				}
			}

			hidden_effect = {
				if = {
					limit = { has_ambition = obj_strengthen_religion }

					change_variable = {
						which = strengthen_religion
						value = 1
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = defender } }

			hidden_effect = {
				participation_scaled_piety = 200
				participation_scaled_prestige = 100

				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -10
				}

				if = {
					limit = { has_ambition = obj_strengthen_religion }

					change_variable = {
						which = strengthen_religion
						value = 1
					}
				}
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = {
		factor = 1

		trigger = {
			ROOT = {
				sufficient_treasury_for_war_trigger = yes
			}
		}

		coronation_factor = yes

		mult_modifier = {
			factor = 10
			ROOT = { religion = FROM } # Intolerable circumstances
		}

		mult_modifier = {
			factor = 5

			ROOT = { has_religion_feature = religion_patriarchal }
			FROM = { has_religion_feature = religion_matriarchal }
		}

		mult_modifier = {
			factor = 5

			ROOT = { has_religion_feature = religion_matriarchal }
			FROM = { has_religion_feature = religion_patriarchal }
		}
	}
}

patriarchal_deposition_war = {
	name = CB_NAME_PATRIARCHAL_DEPOSITION
	war_name = WAR_NAME_SEX_DEPOSITION
	sprite = 35
	truce_days = 3650
	sort_priority = 4000

	is_permanent = no # Disabled in CK2Plus
	can_ask_to_join_war = yes
	is_holy_war = yes
	allowed_to_target_tributaries = no

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	can_use = {
		has_dlc = "Holy Fury"

		# Attacker
		ROOT = {
			has_religion_feature = religion_patriarchal
			has_religion_feature = religion_warlike
			higher_real_tier_than = DUKE
			shares_realm_border_with = FROM
			religion = top_liege
			is_female = no
			is_landed = yes
			is_nomadic = no
			mercenary = no
			NOT = { same_realm = FROM }
		}

		# Defender
		FROM = {
			independent = yes
			is_female = yes
			is_nomadic = no
			is_merchant_republic = no
			is_theocracy = no
		}
	}

	is_valid = {
		# Attacker
		ROOT = {
			has_religion_feature = religion_patriarchal
			has_religion_feature = religion_warlike
			higher_real_tier_than = DUKE
			is_female = no
			NOT = { same_realm = FROM }
		}

		# Defender
		FROM = {
			independent = yes
			is_female = yes
			is_nomadic = no
			is_merchant_republic = no
			is_theocracy = no
		}
	}

	on_success = {
		attacker = {
			show_scope_change = no

			participation_scaled_piety = 100
			participation_scaled_prestige = 150

			if = {
				limit = { uses_decadence = yes }
				participation_scaled_decadence = -10
			}

			religion_authority = {
				name = won_holy_war
				years = 20
			}
		}

		any_attacker = {
			limit = { NOT = { character = attacker } }

			hidden_effect = {
				participation_scaled_piety = 50
				participation_scaled_prestige = 25

				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -10
				}

				if = {
					limit = { has_ambition = obj_strengthen_religion }

					change_variable = {
						which = strengthen_religion
						value = 1
					}
				}
			}
		}

		defender = {
			show_scope_change = no
			piety = -100
			prestige = -50

			custom_tooltip = {
				text = sex_deposition_cb_succ_tip

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

						preferred_limit = {
							is_close_relative = PREV
							is_adult = yes
							religion = attacker
						}

						preferred_limit = {
							is_adult = yes
							religion = attacker
						}

						preferred_limit = {
							is_close_relative = PREV
							is_adult = yes
						}

						preferred_limit = {
							is_adult = yes
						}

						save_event_target_as = new_swapped_ruler

						usurp_title_plus_barony_if_unlanded = {
							target = event_target:title_usurped
							type = invasion
						}

						event_target:scoped_loser = {
							any_demesne_title = {
								limit = { real_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
						}
					}
				}

				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 = attacker
							is_adult = yes
							practical_age < 30
							trait = zealous
						}

						preferred_limit = {
							religion = attacker
							is_adult = yes
							trait = zealous
						}

						preferred_limit = {
							religion = attacker
							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_plus_barony_if_unlanded = {
							target = event_target:title_usurped
							type = invasion
						}

						event_target:scoped_loser = {
							any_demesne_title = {
								limit = { real_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
						}
					}
				}

				else = {
					location = {
						create_character = {
							religion = attacker
							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 = {
							remove_trait = cynical
							wealth = 250
							prestige = 300
							piety = 100
							save_event_target_as = new_swapped_ruler

							usurp_title_plus_barony_if_unlanded = {
								target = event_target:title_usurped
								type = invasion
							}

							event_target:scoped_loser = {
								any_demesne_title = {
									limit = { real_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
							}
						}
					}
				}

				# Titles assigned, now onto laws
				if = {
					limit = {
						attacker = {
							has_religion_feature = religion_patriarchal
						}

						event_target:new_swapped_ruler = {
							NOR = {
								has_religion_feature = religion_matriarchal
								has_religion_feature = religion_equal
								has_religion_feature = religion_feature_bon
							}
						}
					}

					event_target:new_swapped_ruler = {
						any_demesne_title = {
							set_title_flag = non_manual_law_change # No prestige loss

							add_law = {
								law = agnatic_succession
								cooldown = no
								opinion_effect = no
							}

							add_law = {
								law = status_of_women_0
								cooldown = no
								opinion_effect = no
							}

							clr_title_flag = non_manual_law_change # No prestige loss
						}

						recalc_succession = yes
					}
				}

				# Laws set, now onto pacts
				if = {
					limit = {
						has_dlc = "Jade Dragon"
						is_tributary = no
					}

					attacker = {
						make_tributary = {
							who = event_target:new_swapped_ruler
							tributary_type = permanent
						}
					}
				}
				else_if = {
					limit = {
						has_dlc = "Horse Lords"
						is_tributary = no
					}

					attacker = {
						make_tributary = {
							who = event_target:new_swapped_ruler
							percentage = 0.3
						}
					}
				}
				else = {
					attacker = {
						opinion = {
							name = 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_fail = {
		attacker = {
			show_scope_change = no

			piety = -125
			prestige = -250
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			transfer_scaled_wealth = {
				to = defender
				value = 4.0
			}

			piety = -250
			prestige = -500

			if = {
				limit = { uses_decadence = yes }
				participation_scaled_decadence = 20
			}

			religion_authority = {
				name = lost_holy_war
				years = 20
			}
		}

		any_attacker = {
			limit = {
				uses_decadence = yes
				NOT = { character = attacker }
			}

			hidden_effect = {
				participation_scaled_decadence = 20
			}
		}

		defender = {
			show_scope_change = no

			participation_scaled_piety = 200
			participation_scaled_prestige = 100

			if = {
				limit = { uses_decadence = yes }
				participation_scaled_decadence = -10
			}
		}
		any_defender = {
			limit = { NOT = { character = defender } }

			hidden_effect = {
				participation_scaled_piety = 200
				participation_scaled_prestige = 100

				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -10
				}

				if = {
					limit = { has_ambition = obj_strengthen_religion }

					change_variable = {
						which = strengthen_religion
						value = 1
					}
				}
			}

			if = {
				limit = { NOT = { has_religion_feature = religion_patriarchal } }

				religion_authority = {
					name = won_holy_war
					years = 20
				}
			}
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = {
		factor = 1

		trigger = {
			ROOT = {
				sufficient_treasury_for_war_trigger = yes
			}
		}

		coronation_factor = yes

		mult_modifier = {
			factor = 10
			ROOT = { religion = FROM } # Intolerable circumstances
		}

		mult_modifier = {
			factor = 5

			ROOT = { has_religion_feature = religion_patriarchal }
			FROM = { has_religion_feature = religion_matriarchal }
		}

		mult_modifier = {
			factor = 5

			ROOT = { has_religion_feature = religion_matriarchal }
			FROM = { has_religion_feature = religion_patriarchal }
		}
	}
}

flower_war_cb = {
	name = CB_NAME_FLOWER_WAR
	war_name = WAR_NAME_FLOWER_WAR
	sprite = 21
	truce_days = 3650
	sort_priority = 1000

	is_permanent = no # Disabled in CK2Plus
	can_ask_to_join_war = yes
	allowed_to_target_tributaries = no

	can_use = {
		has_dlc = "Holy Fury"

		# Attacker
		ROOT = {
			trait = bloodthirsty_gods_3

			OR = {
				has_religion_feature = religion_ritual_sacrifice
				religion_openly_aztec_or_reformed_trigger = yes
			}

			mercenary = no

			NOR = {
				has_religion_feature = religion_peaceful # Aztecs could reform as Peaceful
				same_realm = FROM
				is_vassal_or_below_of = FROM
			}
		}

		# Defender
		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 = {
		# Attacker
		ROOT = {
			trait = bloodthirsty_gods_3

			OR = {
				has_religion_feature = religion_ritual_sacrifice
				religion_openly_aztec_or_reformed_trigger = yes
			}
		}
	}

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_success = {
		# Attackers
		attacker = {
			show_scope_change = no
			participation_scaled_piety = 100
			participation_scaled_prestige = 100

			hidden_effect = {
				change_variable = {
					which = burned_hearts_points
					value = 5
				}
			}

			religion_authority = {
				name = won_flower_war
				years = 30
			}
		}

		any_attacker = {
			limit = { NOT = { character = attacker } }
			hidden_effect = { participation_scaled_piety = 50 }
		}

		defender = {
			show_scope_change = no

			piety = -100
			prestige = -100

			if = {
				limit = {
					is_reformed_religion = attacker
					controls_religion = no
				}

				religion = attacker # Reformed pagans are forced to convert back
			}

			custom_tooltip = {
				text = flower_war_cb_tooltip

				any_courtier = { # Sacrifice part of the court
					limit = { is_ruler = no }

					random = {
						chance = 25

						mult_modifier = {
							factor = 3
							is_unimportant_trigger = yes
						}

						mult_modifier = {
							factor = 3
							has_minor_title = title_commander
						}

						mult_modifier = { # Less likely to be in battle
							factor = 0.5
							is_adult = no
						}

						mult_modifier = { # Less likely to be in battle
							factor = 0.5

							is_child_of = defender
							is_adult = no
						}

						death = {
							death_reason = death_execution_aztec_sacrifice
							killer = attacker
						}
					}
				}

				opinion = {
					name = opinion_abducted_peasants
					who = attacker
				}

				any_vassal = {
					opinion = {
						name = opinion_abducted_peasants
						who = attacker
					}
				}

				any_demesne_province = {
					limit = { owned_by = PREV }

					add_province_modifier = {
						name = bloodthirsty_flower_war_province
						years = 10
						stacking = yes
					}

					change_variable = {
						which = prosperity_value
						value = -40
					}

					if = {
						limit = {
							check_variable = {
								which = prosperity_value
								value < 0
							}
						}

						set_variable = {
							which = prosperity_value
							value = 0
						}
					}
				}

				any_vassal = {
					limit = { higher_real_tier_than = BARON }

					any_demesne_province = {
						limit = { owned_by = PREV }

						add_province_modifier = {
							name = bloodthirsty_flower_war_province
							years = 10
							stacking = yes
						}

						change_variable = {
							which = prosperity_value
							value = -40
						}

						if = {
							limit = {
								check_variable = {
									which = prosperity_value
									value < 0
								}
							}

							set_variable = {
								which = prosperity_value
								value = 0
							}
						}
					}
				}
			}
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			piety = -100
		}

		defender = {
			show_scope_change = no

			piety = 50
			participation_scaled_prestige = 50
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 50 }
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			piety = -200

			transfer_scaled_wealth = {
				to = defender
				value = 2.0
			}

			religion_authority = {
				name = lost_flower_war
				years = 30
			}
		}

		defender = {
			show_scope_change = no

			piety = 100
			participation_scaled_prestige = 100
			roman_victory_triumph_effect = yes
		}

		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { 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

		trigger = {
			ROOT = {
				sufficient_treasury_for_war_trigger = yes
			}
		}

		coronation_factor = yes
	}
}

# Vassals may revolt after a mass baptism
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
	is_permanent = no # Disabled in CK2Plus

	can_use = {
		# Attacker
		ROOT = {
			vassal_of = FROM
		}
	}

	is_valid = {
		# Attacker
		ROOT = {
			NOT = { religion = FROM }
			liege_before_war = { character = FROM }
		}

		# Defender
		FROM = {
			independent = yes
			in_revolt = no

			any_demesne_title = {
				NOT = { lower_real_tier_than = ROOT }
				temporary = no
			}

			liege_before_war = {
				in_revolt = no

				liege_before_war = {
					in_revolt = no
				}
			}
		}
	}

	on_success = {
		defender = {
			if = {
				limit = {
					independent = no
					liege = { religion = ROOT }
				}
				if = {
					limit = { in_revolt = yes }

					liege_before_war = {
						any_attacker = {
							set_defacto_liege = PREV
						}
					}
				}
				else = {
					liege = {
						any_attacker = {
							set_defacto_liege = PREV
						}
					}
				}
			}

			else_if = {
				limit = {
					OR = {
						independent = yes
						liege = { religion = ROOT }
					}
				}

				any_attacker = {
					set_defacto_liege = ROOT
					clr_character_flag = flag_truebelievers_revolter
				}
			}
		}

		attacker = {
			show_scope_change = no

			prestige = 100
			piety = 100
			participation_scaled_prestige = 100
			participation_scaled_piety = 100

			hidden_effect = {
				clr_character_flag = flag_truebelievers_revolter

				religion_authority = {
					name = won_holy_war
					years = 20
				}

				any_demesne_title = {
					add_pressed_claim = defender
				}
			}
		}

		any_attacker = {
			limit = { NOT = { character = attacker } }

			hidden_effect = {
				participation_scaled_prestige = 100
				participation_scaled_piety = 100
			}
		}

		defender = {
			any_demesne_title = { # All titles
				limit = {
					higher_real_tier_than = COUNT
					NOT = {	has_law = tribal_organization_0	}
				}

				set_title_flag = non_manual_law_change # No prestige loss

				trigger_switch = { # Lower tribal organization
					on_trigger = has_law

					tribal_organization_1 = { add_law = tribal_organization_0 }
					tribal_organization_2 = { add_law = tribal_organization_1 }
					tribal_organization_3 = { add_law = tribal_organization_2 }
					tribal_organization_4 = { add_law = tribal_organization_3 }
				}

				clr_title_flag = non_manual_law_change # No prestige loss
			}

			hidden_effect = {
				religion_authority = {
					name = lost_holy_war
					years = 20
				}
			}
		}

		check_if_reconquista_finished_effect = yes
	}

	on_fail = {
		hidden_effect = {
			attacker = {
				prestige = 100

				any_demesne_title = {
					add_pressed_claim = defender
				}
			}

			any_attacker = {
				clr_character_flag = flag_truebelievers_revolter
			}
		}

		defender = {
			if = {
				limit = { independent = no }

				if = {
					limit = { in_revolt = yes }

					liege_before_war = {
						attacker = {
							set_defacto_liege = PREV
						}
					}
				}
				else = {
					liege = {
						attacker = {
							set_defacto_liege = PREV
						}

						any_vassal = {
							limit = { has_character_flag = independent_joining_flag }

							set_defacto_liege = PREV
							clr_character_flag = independent_joining_flag
						}
					}
				}
			}

			else = {
				attacker = {
					set_defacto_liege = FROM
				}

				any_vassal = {
					limit = { has_character_flag = independent_joining_flag }

					set_defacto_liege = PREV
					clr_character_flag = independent_joining_flag
				}
			}
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			imprison = defender

			hidden_effect = {
				religion_authority = {
					name = lost_holy_war
					years = 20
				}
			}
		}

		any_attacker = {
			hidden_effect = {
				participation_scaled_prestige = -100
				participation_scaled_piety = -100
				clr_character_flag = flag_truebelievers_revolter
			}
		}

		defender = {
			show_scope_change = no

			prestige = 100
			piety = 100

			hidden_effect = {
				religion_authority = {
					name = won_holy_war
					years = 20
				}

				if = { # Allow free revocation of a title
					limit = {
						NOT = {
							has_opinion_modifier = {
								name = opinion_traitor
								who = attacker
							}
						}
					}

					opinion = {
						name = opinion_traitor
						who = attacker
					}
				}
			}
		}
	}

	on_invalidation = {
		any_attacker = {
			clr_character_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
	}
}