minor_religious_war = {
	group = religious

	defender_faith_can_join = yes

	combine_into_one = yes
	should_show_war_goal_subview = yes
	mutually_exclusive_titles = { always = yes }

	# Already defined in 00_casus_belli_groups.txt
	allowed_for_character = {
		scope:attacker.faith = {
			NOT = { has_doctrine = unreformed_faith_doctrine }
		}
	}

	allowed_for_character_display_regardless = {
		piety_level >= 1
		trigger_if = {
			limit = { NOT = { has_game_rule = holy_war_cb_default } }
			NOT = { has_character_modifier = mgr_recent_holy_war }
		}
	}

	allowed_against_character = {
		scope:attacker = {
			faith = {
				faith_hostility_level = {
					target = scope:defender.faith
					value >= religious_cb_enabled_hostility_level
				}
			}
			ALL_FALSE = {
				top_liege = scope:defender.top_liege
				liege = scope:defender
			}
		}
	}
	target_titles = neighbor_land_or_water
	target_title_tier = all
	target_de_jure_regions_above = yes
	ignore_effect = change_title_holder

	valid_to_start = {
		scope:target = {
			tier = tier_county
		}
	}

	should_invalidate = {
		OR = {
			NOT = {
				any_in_list = {
					list = target_titles
					any_in_de_jure_hierarchy = {
						tier = tier_county
						holder = {
							OR = {
								this = scope:defender
								target_is_liege_or_above = scope:defender
							}
						}
					}
				}
			}
			#Faith change does not immediately invalidate, it's run through event war_event.3100
			scope:war = { exists = var:invalidate_defender_faith_change }
			scope:war = { exists = var:invalidate_attacker_faith_change }
		}
	}

	on_invalidated_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					NOT = {
						any_in_list = {
							list = target_titles
							any_in_de_jure_hierarchy = {
								tier = tier_county
								holder = {
									OR = {
										this = scope:defender
										target_is_liege_or_above = scope:defender
									}
								}
							}
						}
					}
				}
				desc = msg_religious_war_invalidation_region_message
			}
			triggered_desc = {
				trigger = {
					scope:war = {
						OR = {
							exists = var:invalidate_defender_faith_change
							exists = var:invalidate_attacker_faith_change
						}
					}
				}
				desc = msg_religious_war_invalidated_hostility_level_message
			}
		}
	}

	on_invalidated = {
		
	}

	cost = {
		piety = {
			add = {
				value = 100
				desc = CB_BASE_COST
			}
			# Faith Doctrine Piety Discounts
			if = {
				limit = {
					scope:attacker.faith = {
						has_doctrine_parameter = divine_destiny_holy_war_cost_reduction
					}
				}
				multiply = {
					value = 0.5
					desc = CB_DIVINE_DESTINY_COST
				}
			}

			if = {
				limit = {
					scope:attacker.faith = {
						has_doctrine_parameter = cheaper_holy_wars_active
					}
				}
				multiply = {
					value = 0.8
					desc = CB_REDUCED_HOLY_WAR_COST
				}
			}

			# Innovation Piety Discounts
			if = {
				limit = {
					scope:attacker = {
						culture = {
							has_innovation = innovation_reconquista
						}
					}
				}
				multiply = {
					value = 0.9
					desc = CB_RECONQUISTA_COST
				}
			}

			# Bellum Justum Perk
			if = {
				limit = {
					scope:attacker = {
						has_perk = bellum_justum_perk
					}
				}
				multiply = {
					add = bellum_justum_discount_percentage
					divide = 100
					desc = CB_ATTACKER_BELLUM_JUSTUM
				}
			}

			# Dynasty Warfare Perk
			if = {
				limit = {
					scope:attacker = {
						has_dynasty = yes
						dynasty = {
							has_dynasty_perk = warfare_legacy_2
						}
					}
				}
				multiply = {
					value = warfare_legacy_2_discount
					desc = CB_ATTACKER_DYNASTY_WARFARE
				}
			}

			# Vassal contract in-realm cost reduction
			if = {
				limit = {
					scope:attacker = {
						is_independent_ruler = no
						vassal_contract_has_flag = vassal_contract_war_override
						liege = scope:defender.liege
					}
				}
				multiply = {
					value = war_declaration_rights_allowed_cost_reduction
					desc = "CB_ATTACKER_VASSAL_CONTRACT"
				}
			}

			if = {
				limit = {
					has_game_rule = no_cost_casus_belli_costs
				}
				multiply = {
					value = 0
					desc = CB_GAME_RULE_NO_COST
				}
			}
		}
	}

	on_declaration = {
		on_declared_war = yes
		scope:attacker = { holy_war_cb_cooldown_effect = yes }
	}

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:attacker.faith = {
						has_doctrine = doctrine_pluralism_pluralistic
					}
				}
				desc = religious_war_victory_desc_doctrine_pluralism
			}
			desc = religious_war_victory_desc
		}
	}

	on_victory = {
		scope:attacker = { show_pow_release_message_effect = yes }
		create_title_and_vassal_change = {
			type = conquest_holy_war
			save_scope_as = change
			add_claim_on_loss = yes
		}

		# go through the dejure hierarchy under target titles, transfer titles with same or worse tolerance holders (their religion equaly or less tolerated than the defender's),
		# take the holder as vassal otherwise and don't go deeper
		every_in_list = {
			list = target_titles
			custom_tooltip = RELIGIOUS_CB_TITLE

			conquest_cb_title_transfer = {
				RELIGIOUS_WAR = yes
			}
		}

		every_in_list = {
			list = vassals_taken
			change_liege = {
				liege = scope:attacker
				change = scope:change
			}
		}
		
		every_in_list = {
			list = titles_taken
			change_title_holder = {
				holder = scope:attacker
				change = scope:change
				take_baronies = yes
			}
		}

		resolve_title_and_vassal_change = scope:change

		# Prestige for the attacker's war allies
		add_from_contribution_attackers = {
			prestige = religious_cb_ally_prestige_county
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = religious_cb_ally_prestige_county
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Piety Progress for the Attacker
		every_in_list = {
			list = target_titles
			scope:attacker = {
				add_piety_experience = {
					add = religious_cb_piety_gain_county
					if = {
						limit = {
							scope:attacker.faith = {
								has_doctrine_parameter = bonus_holy_war_piety_active
							}
						}
						multiply = 1.5
					}
				}
			}
		}

		#Fervor
		if = {
			limit = { has_game_rule = default_fervor_hs }
			scope:attacker.faith = { change_fervor = -3 }
			scope:defender.faith = { change_fervor = 3 }
		}
		else_if = {
			limit = { has_game_rule = reduced_fervor_hs }
			scope:attacker.faith = { change_fervor = -1.5 }
			scope:defender.faith = { change_fervor = 1.5 }
		}
		else_if = {
			limit = { has_game_rule = inversed_fervor_hs }
			scope:attacker.faith = { change_fervor = 3 }
			scope:defender.faith = { change_fervor = -3 }
		}
		else_if = {
			limit = { has_game_rule = rinversed_fervor_hs }
			scope:attacker.faith = { change_fervor = 1.5 }
			scope:defender.faith = { change_fervor = -1.5 }
		}
		else = { } #Nothing

		# Truce
		add_truce_attacker_victory_effect = yes

	}

	on_white_peace_desc = {
		desc = religious_war_white_peace_desc
	}

	on_white_peace = {
		scope:attacker = { show_pow_release_message_effect = yes }

		# Prestige for the attacker's war allies
		add_from_contribution_attackers = {
			prestige = religious_cb_ally_prestige_county
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = religious_cb_ally_prestige_county
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Truce
		add_truce_white_peace_effect = yes

		scope:attacker = {
			add_piety = religious_cb_piety_white_peace
		}
	}

	on_defeat_desc = {
		desc = religious_war_defeat_desc
	}

	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }

		# Prestige for the attacker's war allies
		add_from_contribution_attackers = {
			prestige = religious_cb_ally_prestige_county
			opinion = {
				modifier = contributed_in_war
			}
		}
		scope:attacker = {
			every_vassal = {
				limit = { faith = scope:attacker.faith }
				custom = all_attackers_vassals_same_faith
				add_opinion = {
					modifier = liege_lost_declared_religious_war
					target = scope:attacker
				}
			}
		}

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = religious_cb_ally_prestige_county
			opinion = {
				modifier = contributed_in_war
			}
		}

		# piety change
		every_in_list = {
			list = target_titles

			scope:defender = {
				add_piety = {
					add = religious_cb_piety_gain_county
					if = {
						limit = {
							scope:defender.faith = {
								has_doctrine_parameter = bonus_holy_war_piety_active
							}
						}
						multiply = 1.5
					}
				}
			}

			scope:attacker = {
				pay_short_term_income = {
					years = 2
					target = scope:defender
				}
			}
		}

		#Fervor
		if = {
			limit = { has_game_rule = default_fervor_hs }
			scope:attacker.faith = { change_fervor = -3 }
		}
		else_if = {
			limit = { has_game_rule = reduced_fervor_hs }
			scope:attacker.faith = { change_fervor = -1.5 }
		}
		else_if = {
			limit = { has_game_rule = inversed_fervor_hs }
			scope:attacker.faith = { change_fervor = -3 }
		}
		else_if = {
			limit = { has_game_rule = rinversed_fervor_hs }
			scope:attacker.faith = { change_fervor = -1.5 }
		}
		else = { } #Nothing

		# Truce
		add_truce_attacker_defeat_effect = yes

		scope:attacker = {
			save_temporary_scope_as = loser
		}
		on_lost_aggression_war_discontent_loss = yes
	}

	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit

	transfer_behavior = transfer

	attacker_allies_inherit = yes
	defender_allies_inherit = yes

	war_name = "RELIGIOUS_WAR_NAME"
	cb_name = "HOLY_WAR_COUNTY_NAME"

	is_holy_war = yes

	interface_priority = 80

	use_de_jure_wargoal_only = yes

	attacker_wargoal_percentage = 0.8

	max_ai_diplo_distance_to_title = 500
	min_ai_score = 100
}

religious_war = {
	group = religious

	defender_faith_can_join = yes

	combine_into_one = yes
	should_show_war_goal_subview = yes
	mutually_exclusive_titles = { always = yes }

	# Additional requirements to the defines in 00_casus_belli_groups.txt
	allowed_for_character = {
		scope:attacker.faith = {
			NOT = { has_doctrine = unreformed_faith_doctrine }
		}
	}

	allowed_for_character_display_regardless = {
		piety_level >= 2
		trigger_if = {
			limit = { NOT = { has_game_rule = holy_war_cb_default } }
			NOT = { has_character_modifier = mgr_recent_holy_war }
		}
	}

	allowed_against_character = {
		scope:attacker = {
			faith = {
				faith_hostility_level = {
					target = scope:defender.faith
					value >= religious_cb_enabled_hostility_level
				}
			}
			ALL_FALSE = {
				top_liege = scope:defender.top_liege
				liege = scope:defender
			}
		}
	}
	target_titles = neighbor_land_or_water
	target_title_tier = all
	target_de_jure_regions_above = yes
	ignore_effect = change_title_holder

	valid_to_start = {
		scope:target = {
			tier = tier_duchy
		}
	}

	should_invalidate = {
		OR = {
			NOT = {
				any_in_list = {
					list = target_titles
					any_in_de_jure_hierarchy = {
						tier = tier_county
						holder = {
							OR = {
								this = scope:defender
								target_is_liege_or_above = scope:defender
							}
						}
					}
				}
			}
			#Faith change does not immediately invalidate, it's run through event war_event.3100
			scope:war = { exists = var:invalidate_defender_faith_change }
			scope:war = { exists = var:invalidate_attacker_faith_change }
		}
	}

	on_invalidated_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					NOT = {
						any_in_list = {
							list = target_titles
							any_in_de_jure_hierarchy = {
								tier = tier_county
								holder = {
									OR = {
										this = scope:defender
										target_is_liege_or_above = scope:defender
									}
								}
							}
						}
					}
				}
				desc = msg_religious_war_invalidation_region_message
			}
			triggered_desc = {
				trigger = {
					scope:war = {
						OR = {
							exists = var:invalidate_defender_faith_change
							exists = var:invalidate_attacker_faith_change
						}
					}
				}
				desc = msg_religious_war_invalidated_hostility_level_message
			}
		}
	}

	on_invalidated = {
		
	}

	cost = {
		piety = {
			add = {
				value = 200
				desc = CB_BASE_COST
			}
			# Faith Doctrine Piety Discounts
			if = {
				limit = {
					scope:attacker.faith = {
						has_doctrine_parameter = divine_destiny_holy_war_cost_reduction
					}
				}
				multiply = {
					value = 0.5
					desc = CB_DIVINE_DESTINY_COST
				}
			}

			if = {
				limit = {
					scope:attacker.faith = {
						has_doctrine_parameter = cheaper_holy_wars_active
					}
				}
				multiply = {
					value = 0.8
					desc = CB_REDUCED_HOLY_WAR_COST
				}
			}

			# Innovation Piety Discounts
			if = {
				limit = {
					scope:attacker = {
						culture = {
							has_innovation = innovation_reconquista
						}
					}
				}
				multiply = {
					value = 0.9
					desc = CB_RECONQUISTA_COST
				}
			}

			# Bellum Justum Perk
			if = {
				limit = {
					scope:attacker = {
						has_perk = bellum_justum_perk
					}
				}
				multiply = {
					add = bellum_justum_discount_percentage
					divide = 100
					desc = CB_ATTACKER_BELLUM_JUSTUM
				}
			}

			# Dynasty Warfare Perk
			if = {
				limit = {
					scope:attacker = {
						has_dynasty = yes
						dynasty = {
							has_dynasty_perk = warfare_legacy_2
						}
					}
				}
				multiply = {
					value = warfare_legacy_2_discount
					desc = CB_ATTACKER_DYNASTY_WARFARE
				}
			}

			# Vassal contract in-realm cost reduction
			if = {
				limit = {
					scope:attacker = {
						is_independent_ruler = no
						vassal_contract_has_flag = vassal_contract_war_override
						liege = scope:defender.liege
					}
				}
				multiply = {
					value = war_declaration_rights_allowed_cost_reduction
					desc = "CB_ATTACKER_VASSAL_CONTRACT"
				}
			}

			if = {
				limit = {
					has_game_rule = no_cost_casus_belli_costs
				}
				multiply = {
					value = 0
					desc = CB_GAME_RULE_NO_COST
				}
			}
		}
	}

	on_declaration = {
		on_declared_war = yes
		scope:attacker = { holy_war_cb_cooldown_effect = yes }
	}

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:attacker.faith = {
						has_doctrine = doctrine_pluralism_pluralistic
					}
				}
				desc = religious_war_victory_desc_doctrine_pluralism
			}
			desc = religious_war_victory_desc
		}
	}

	on_victory = {
		scope:attacker = { show_pow_release_message_effect = yes }
		create_title_and_vassal_change = {
			type = conquest_holy_war
			save_scope_as = change
			add_claim_on_loss = yes
		}

		# go through the dejure hierarchy under target titles, transfer titles with same or worse tolerance holders (their religion equaly or less tolerated than the defender's),
		# take the holder as vassal otherwise and don't go deeper
		every_in_list = {
			list = target_titles
			custom_tooltip = RELIGIOUS_CB_TITLE

			conquest_cb_title_transfer = {
				RELIGIOUS_WAR = yes
			}
		}

		every_in_list = {
			list = vassals_taken
			change_liege = {
				liege = scope:attacker
				change = scope:change
			}
		}
		
		every_in_list = {
			list = titles_taken
			change_title_holder = {
				holder = scope:attacker
				change = scope:change
				take_baronies = yes
			}
		}


		resolve_title_and_vassal_change = scope:change

		# Prestige for the attacker's war allies
		add_from_contribution_attackers = {
			prestige = religious_cb_ally_prestige_duchy
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = religious_cb_ally_prestige_duchy
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Piety Progress for the Attacker
		every_in_list = {
			list = target_titles
				scope:attacker = {
				add_piety_experience = {
					add = religious_cb_piety_gain_duchy
					if = {
						limit = {
							scope:attacker.faith = {
								has_doctrine_parameter = bonus_holy_war_piety_active
							}
						}
						multiply = 1.5
					}
				}
			}
		}

		#Fervor
		if = {
			limit = { has_game_rule = default_fervor_hs }
			scope:attacker.faith = { change_fervor = -5 }
			scope:defender.faith = { change_fervor = 5 }
		}
		else_if = {
			limit = { has_game_rule = reduced_fervor_hs }
			scope:attacker.faith = { change_fervor = -2.5 }
			scope:defender.faith = { change_fervor = 2.5 }
		}
		else_if = {
			limit = { has_game_rule = inversed_fervor_hs }
			scope:attacker.faith = { change_fervor = 5 }
			scope:defender.faith = { change_fervor = -5 }
		}
		else_if = {
			limit = { has_game_rule = rinversed_fervor_hs }
			scope:attacker.faith = { change_fervor = 2.5 }
			scope:defender.faith = { change_fervor = -2.5 }
		}
		else = { } #Nothing

		# Truce
		add_truce_attacker_victory_effect = yes

	}

	on_white_peace_desc = {
		desc = religious_war_white_peace_desc
	}

	on_white_peace = {
		scope:attacker = { show_pow_release_message_effect = yes }

		# Prestige for the attacker's war allies
		add_from_contribution_attackers = {
			prestige = religious_cb_ally_prestige_duchy
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = religious_cb_ally_prestige_duchy
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Truce
		add_truce_white_peace_effect = yes

		scope:attacker = {
			add_piety = religious_cb_piety_white_peace
		}
	}

	on_defeat_desc = {
		desc = religious_war_defeat_desc
	}

	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }

		# Prestige for the attacker's war allies
		add_from_contribution_attackers = {
			prestige = religious_cb_ally_prestige_duchy
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = religious_cb_ally_prestige_duchy
			opinion = {
				modifier = contributed_in_war
			}
		}

		# piety change
		every_in_list = {
			list = target_titles

			scope:defender = {
				add_piety = {
					add = religious_cb_piety_gain_duchy
					if = {
						limit = {
							scope:defender.faith = {
								has_doctrine_parameter = bonus_holy_war_piety_active
							}
						}
						multiply = 1.5
					}
				}
			}

			scope:attacker = {
				pay_short_term_income = {
					years = 3
					target = scope:defender
				}
			}
		}

		scope:attacker = {
			every_vassal = {
				limit = { faith = scope:attacker.faith }
				custom = all_attackers_vassals_same_faith
				add_opinion = {
					modifier = liege_lost_declared_religious_war
					target = scope:attacker
				}
			}
		}

		#Fervor
		if = {
			limit = { has_game_rule = default_fervor_hs }
			scope:attacker.faith = { change_fervor = -5 }
		}
		else_if = {
			limit = { has_game_rule = reduced_fervor_hs }
			scope:attacker.faith = { change_fervor = -2.5 }
		}
		else_if = {
			limit = { has_game_rule = inversed_fervor_hs }
			scope:attacker.faith = { change_fervor = -5 }
		}
		else_if = {
			limit = { has_game_rule = rinversed_fervor_hs }
			scope:attacker.faith = { change_fervor = -2.5 }
		}
		else = { } #Nothing
		# Truce
		add_truce_attacker_defeat_effect = yes

		scope:attacker = {
			save_temporary_scope_as = loser
		}
		on_lost_aggression_war_discontent_loss = yes
	}

	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit

	transfer_behavior = transfer

	attacker_allies_inherit = yes
	defender_allies_inherit = yes

	war_name = "RELIGIOUS_WAR_NAME"
	cb_name = "HOLY_WAR_DUCHY_NAME"

	is_holy_war = yes

	interface_priority = 80

	use_de_jure_wargoal_only = yes

	attacker_wargoal_percentage = 0.8

	max_ai_diplo_distance_to_title = 500
	min_ai_score = 100
}

major_religious_war = {
	group = religious

	defender_faith_can_join = yes

	combine_into_one = yes
	should_show_war_goal_subview = yes
	mutually_exclusive_titles = { always = yes }

	# Additional requirements to the defines in 00_casus_belli_groups.txt
	allowed_for_character = {
		scope:attacker.faith = {
			NOT = { has_doctrine = unreformed_faith_doctrine }
		}
	}

	allowed_for_character_display_regardless = {
		piety_level >= 4
		trigger_if = {
			limit = { NOT = { has_game_rule = holy_war_cb_default } }
			NOT = { has_character_modifier = mgr_recent_holy_war }
		}
		custom_description = {
			text = declared_major_religious_war_flag_tt
			NOT = { has_character_flag = declared_major_religious_war_flag }
		}
	}

	allowed_against_character = {
		scope:attacker = {
			faith = {
				faith_hostility_level = {
					target = scope:defender.faith
					value >= religious_cb_enabled_hostility_level
				}
			}
			ALL_FALSE = {
				top_liege = scope:defender.top_liege
				liege = scope:defender
			}
		}
	}
	target_titles = neighbor_land_or_water
	target_title_tier = all
	target_de_jure_regions_above = yes
	ignore_effect = change_title_holder

	valid_to_start = {
		scope:target = {
			tier = tier_kingdom
		}
	}

	should_invalidate = {
		OR = {
			NOT = {
				any_in_list = {
					list = target_titles
					any_in_de_jure_hierarchy = {
						tier = tier_county
						holder = {
							OR = {
								this = scope:defender
								target_is_liege_or_above = scope:defender
							}
						}
					}
				}
			}
			#Faith change does not immediately invalidate, it's run through event war_event.3100
			scope:war = { exists = var:invalidate_defender_faith_change }
			scope:war = { exists = var:invalidate_attacker_faith_change }
		}
	}

	on_invalidated_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					NOT = {
						any_in_list = {
							list = target_titles
							any_in_de_jure_hierarchy = {
								tier = tier_county
								holder = {
									OR = {
										this = scope:defender
										target_is_liege_or_above = scope:defender
									}
								}
							}
						}
					}
				}
				desc = msg_religious_war_invalidation_region_message
			}
			triggered_desc = {
				trigger = {
					scope:war = {
						OR = {
							exists = var:invalidate_defender_faith_change
							exists = var:invalidate_attacker_faith_change
						}
					}
				}
				desc = msg_religious_war_invalidated_hostility_level_message
			}
		}
	}
	
	on_invalidated = {
		
	}

	cost = {
		piety = {
			add = {
				value = 750
				desc = CB_BASE_COST
			}
			# Faith Doctrine Piety Discounts
			if = {
				limit = {
					scope:attacker.faith = {
						has_doctrine_parameter = divine_destiny_holy_war_cost_reduction
					}
				}
				multiply = {
					value = 0.5
					desc = CB_DIVINE_DESTINY_COST
				}
			}

			if = {
				limit = {
					scope:attacker.faith = {
						has_doctrine_parameter = cheaper_holy_wars_active
					}
				}
				multiply = {
					value = 0.8
					desc = CB_REDUCED_HOLY_WAR_COST
				}
			}

			# Innovation Piety Discounts
			if = {
				limit = {
					scope:attacker = {
						culture = {
							has_innovation = innovation_reconquista
						}
					}
				}
				multiply = {
					value = 0.9
					desc = CB_RECONQUISTA_COST
				}
			}

			# Bellum Justum Perk
			if = {
				limit = {
					scope:attacker = {
						has_perk = bellum_justum_perk
					}
				}
				multiply = {
					add = bellum_justum_discount_percentage
					divide = 100
					desc = CB_ATTACKER_BELLUM_JUSTUM
				}
			}

			# Dynasty Warfare Perk
			if = {
				limit = {
					scope:attacker = {
						has_dynasty = yes
						dynasty = {
							has_dynasty_perk = warfare_legacy_2
						}
					}
				}
				multiply = {
					value = warfare_legacy_2_discount
					desc = CB_ATTACKER_DYNASTY_WARFARE
				}
			}

			# Vassal contract in-realm cost reduction
			if = {
				limit = {
					scope:attacker = {
						is_independent_ruler = no
						vassal_contract_has_flag = vassal_contract_war_override
						liege = scope:defender.liege
					}
				}
				multiply = {
					value = war_declaration_rights_allowed_cost_reduction
					desc = "CB_ATTACKER_VASSAL_CONTRACT"
				}
			}

			if = {
				limit = {
					has_game_rule = no_cost_casus_belli_costs
				}
				multiply = {
					value = 0
					desc = CB_GAME_RULE_NO_COST
				}
			}
		}
	}

	on_declaration = {
		on_declared_war = yes
		scope:attacker = { 
			add_character_flag = declared_major_religious_war_flag 
			holy_war_cb_cooldown_effect = yes
		}
	}

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:attacker.faith = {
						has_doctrine = doctrine_pluralism_pluralistic
					}
				}
				desc = religious_war_victory_desc_doctrine_pluralism
			}
			desc = religious_war_victory_desc
		}
		triggered_desc = {
			trigger = { scope:attacker = { is_local_player = yes } }
			desc = major_religious_war_warning_desc
		}
	}

	on_victory = {
		scope:attacker = { show_pow_release_message_effect = yes }
		create_title_and_vassal_change = {
			type = conquest_holy_war
			save_scope_as = change
			add_claim_on_loss = yes
		}
		# go through the dejure hierarchy under target titles, transfer titles with same or worse tolerance holders (their religion equaly or less tolerated than the defender's),
		# take the holder as vassal otherwise and don't go deeper
		every_in_list = {
			list = target_titles
			custom_tooltip = RELIGIOUS_CB_TITLE

			conquest_cb_title_transfer = {
				RELIGIOUS_WAR = yes
			}
		}

		every_in_list = {
			list = vassals_taken
			change_liege = {
				liege = scope:attacker
				change = scope:change
			}
		}
		
		every_in_list = {
			list = titles_taken
			change_title_holder = {
				holder = scope:attacker
				change = scope:change
				take_baronies = yes
			}
		}

		resolve_title_and_vassal_change = scope:change

		# Prestige for the attacker's war allies
		add_from_contribution_attackers = {
			prestige = religious_cb_ally_prestige_kingdom
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = religious_cb_ally_prestige_kingdom
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Piety Progress for the Attacker
		every_in_list = {
			list = target_titles
				scope:attacker = {
				add_piety_experience = {
					add = religious_cb_piety_gain_kingdom
					if = {
						limit = {
							scope:attacker.faith = {
								has_doctrine_parameter = bonus_holy_war_piety_active
							}
						}
						multiply = 1.5
					}
				}
			}
		}

		#Fervor
		if = {
			limit = { has_game_rule = default_fervor_hs }
			scope:attacker.faith = { change_fervor = -10 }
			scope:defender.faith = { change_fervor = 10 }
		}
		else_if = {
			limit = { has_game_rule = reduced_fervor_hs }
			scope:attacker.faith = { change_fervor = -5 }
			scope:defender.faith = { change_fervor = 5 }
		}
		else_if = {
			limit = { has_game_rule = inversed_fervor_hs }
			scope:attacker.faith = { change_fervor = 10 }
			scope:defender.faith = { change_fervor = -10 }
		}
		else_if = {
			limit = { has_game_rule = rinversed_fervor_hs }
			scope:attacker.faith = { change_fervor = 5 }
			scope:defender.faith = { change_fervor = -5 }
		}
		else = { } #Nothing
		
		# Truce
		add_truce_attacker_victory_effect = yes

	}

	on_white_peace_desc = {
		desc = religious_war_white_peace_desc
	}

	on_white_peace = {
		scope:attacker = { show_pow_release_message_effect = yes }

		# Prestige for the attacker's war allies
		add_from_contribution_attackers = {
			prestige = religious_cb_ally_prestige_kingdom
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = religious_cb_ally_prestige_kingdom
			opinion = {
				modifier = contributed_in_war
			}
		}

		scope:attacker = {
			add_piety = religious_cb_piety_white_peace
		}

		# Truce
		add_truce_white_peace_effect = yes
	}

	on_defeat_desc = {
		desc = religious_war_defeat_desc
	}

	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }

		# Prestige for the attacker's war allies
		add_from_contribution_attackers = {
			prestige = religious_cb_ally_prestige_kingdom
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = religious_cb_ally_prestige_kingdom
			opinion = {
				modifier = contributed_in_war
			}
		}

		# piety change
		every_in_list = {
			list = target_titles

			scope:defender = {
				add_piety = {
					add = religious_cb_piety_gain_kingdom
					if = {
						limit = {
							scope:defender.faith = {
								has_doctrine_parameter = bonus_holy_war_piety_active
							}
						}
						multiply = 1.5
					}
				}
			}

			scope:attacker = {
				pay_short_term_income = {
					years = 4
					target = scope:defender
				}
			}
		}

		scope:attacker = {
			every_vassal = {
				limit = { faith = scope:attacker.faith }
				custom = all_attackers_vassals_same_faith
				add_opinion = {
					modifier = liege_lost_declared_religious_war
					target = scope:attacker
				}
			}
		}

		#Fervor
		if = {
			limit = { has_game_rule = default_fervor_hs }
			scope:attacker.faith = { change_fervor = -10 }
		}
		else_if = {
			limit = { has_game_rule = reduced_fervor_hs }
			scope:attacker.faith = { change_fervor = -5 }
		}
		else_if = {
			limit = { has_game_rule = inversed_fervor_hs }
			scope:attacker.faith = { change_fervor = -10 }
		}
		else_if = {
			limit = { has_game_rule = rinversed_fervor_hs }
			scope:attacker.faith = { change_fervor = -5 }
		}
		else = { } #Nothing
		# Truce
		add_truce_attacker_defeat_effect = yes

		scope:attacker = {
			save_temporary_scope_as = loser
		}
		on_lost_aggression_war_discontent_loss = yes
	}

	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit

	transfer_behavior = transfer

	attacker_allies_inherit = yes
	defender_allies_inherit = yes

	war_name = "RELIGIOUS_WAR_NAME"
	cb_name = "HOLY_WAR_KINGDOM_NAME"

	is_holy_war = yes

	interface_priority = 80

	use_de_jure_wargoal_only = yes

	attacker_wargoal_percentage = 0.8

	max_ai_diplo_distance_to_title = 500
	min_ai_score = 100
}

excommunication_war = {
	group = religious
	ai_only_against_neighbors = yes

	# Already defined in 00_casus_belli_groups.txt
	# allowed_for_character = {}

	allowed_against_character = {
		scope:defender = {
			#Defender must be an excommunicated member of our faith.
			has_trait = excommunicated
			is_independent_ruler = yes
			faith = {
				has_doctrine_parameter = excommunication_active
				this = scope:attacker.faith
			}
		}

		scope:attacker = {
			#We can't DoW our own liege
			NOT = {
				liege = scope:defender
			}

			trigger_if = {
				limit = { is_ai = no } # Already pre-filtered by the AI due to ai_only_against_neighbors = yes. any_neighboring_top_liege_realm_owner is expensive
				#We have to be neighboring the excommunicated ruler.
				any_neighboring_top_liege_realm_owner = {
					this = scope:defender
				}
			}
		}
	}

	target_de_jure_regions_above = yes

	valid_to_start = {
		always = yes
	}

	on_declaration = {
		on_declared_war = yes
	}

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:defender = { is_local_player = yes }
				}
				desc = excommunication_war_victory_desc_defender
			}
			desc = excommunication_war_victory_desc
		}
	}

	on_victory = {
		scope:attacker = { show_pow_release_message_effect = yes }
		scope:attacker = {
			reverse_add_opinion = {
				modifier = pleased_opinion
				target = faith.religious_head
				opinion = 20
			}
		}

		#Handle all piety gains/losses in scripted effects.
		excommunication_cb_piety_change = {
			WINNER = scope:attacker
			LOSER = scope:defender
		}

		#This is ddown here in it's own section to control how the tooltips are ordered.
		scope:defender = {
			depose = yes
		}

		# Truce
		add_truce_attacker_victory_effect = yes

	}

	on_white_peace_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:defender = { is_local_player = yes }
				}
				desc = excommunication_war_white_peace_desc_defender
			}
			desc = excommunication_war_white_peace_desc
		}
	}

	on_white_peace = {
		scope:attacker = { show_pow_release_message_effect = yes }
		scope:attacker = {
			add_piety = excommunication_cb_piety_white_peace
		}

		# Truce
		add_truce_white_peace_effect = yes
	}

	on_defeat_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:attacker = { is_local_player = yes }
				}
				desc = excommunication_war_defeat_desc_attacker
			}
			triggered_desc = {
				trigger = {
					scope:defender = { is_local_player = yes }
				}
				desc = excommunication_war_defeat_desc_defender
			}
			desc = excommunication_war_defeat_desc
		}
	}

	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		#Handle all piety gains/losses in scripted effects.
		excommunication_cb_piety_change = {
			LOSER = scope:attacker
			WINNER = scope:defender
		}

		scope:defender = {
			remove_trait = excommunicated
			add_character_modifier = {
				modifier = excommunication_recently_lifted
				years = 10
			}
		}

		scope:attacker = {
			reverse_add_opinion = {
				modifier = disappointed_opinion
				target = faith.religious_head
				opinion = -20
			}
		}

		# Truce
		add_truce_attacker_defeat_effect = yes
	}
	
	should_invalidate = {
		scope:defender = {
			NOT = {
				has_trait = excommunicated
			}
		}
	}

	on_invalidated_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:defender = {
						NOT = {
							has_trait = excommunicated
						}
					}
				}
				desc = msg_excommunication_war_invalidation_trait_message
			}
			desc = msg_invalidate_war_title
		}
	}
	
	on_invalidated = {
		scope:attacker = {
			#Used for the piety_change scripted effect.
			save_scope_as = winner
		}

		#Handle all piety gains/losses in scripted effects.
		excommunication_cb_piety_change = {
			WINNER = scope:attacker
			LOSER = scope:defender
		}

		scope:defender = {
			depose = yes

			#Used for the piety_change scripted effect.
			save_scope_as = loser
		}
	}

	on_primary_attacker_death = invalidate
	on_primary_defender_death = inherit

	transfer_behavior = transfer

	attacker_allies_inherit = yes
	defender_allies_inherit = yes

	war_name = "EXCOMMUNICATION_WAR_NAME"

	interface_priority = 80

	use_de_jure_wargoal_only = yes

	attacker_wargoal_percentage = 0.8

	attacker_score_from_occupation_scale = 50
	defender_score_from_occupation_scale = 50
	#attacker_score_from_battles_scale = 50
	#defender_score_from_battles_scale = 50
	max_attacker_score_from_battles = 100
	max_defender_score_from_battles = 100

	max_ai_diplo_distance_to_title = 500
	min_ai_score = 100
}

flowery_war_cb = {
	group = religious_disorganised
	ai_only_against_neighbors = yes

	# Root is the title
	# scope:attacker is the attacker
	# scope:defender is the defender
	allowed_against_character = {
		scope:attacker = {
			faith = { has_doctrine_parameter = flower_war_cb_active }
			NOT = {	#Must not have any leftover sacrificial prisoners/easy ability to get any.
				any_prisoner = { has_character_modifier = designated_human_sacrifice_modifier }
			}
		}

		scope:defender = {
			is_independent_ruler = yes
			trigger_if = {
				limit = { scope:attacker = { is_ai = no } } # Already pre-filtered by the AI due to ai_only_against_neighbors = yes. any_neighboring_top_liege_realm_owner is expensive
				scope:attacker = {
					any_neighboring_top_liege_realm_owner = {
						this = scope:defender
					}
				}
			}
		}
	}

	cost = {
		piety = {
			add = {
				value = 100
				desc = CB_BASE_COST
			}
			# Faith Doctrine Piety Discounts
			if = {
				limit = {
					scope:attacker.faith = {
						has_doctrine_parameter = divine_destiny_holy_war_cost_reduction
					}
				}
				multiply = {
					value = 0.5
					desc = CB_DIVINE_DESTINY_COST
				}
			}

			if = {
				limit = {
					scope:attacker.faith = {
						has_doctrine_parameter = cheaper_holy_wars_active
					}
				}
				multiply = {
					value = 0.8
					desc = CB_REDUCED_HOLY_WAR_COST
				}
			}

			# Innovation Piety Discounts
			if = {
				limit = {
					scope:attacker = {
						culture = {
							has_innovation = innovation_reconquista
						}
					}
				}
				multiply = {
					value = 0.9
					desc = CB_RECONQUISTA_COST
				}
			}

			# Bellum Justum Perk
			if = {
				limit = {
					scope:attacker = {
						has_perk = bellum_justum_perk
					}
				}
				multiply = {
					add = bellum_justum_discount_percentage
					divide = 100
					desc = CB_ATTACKER_BELLUM_JUSTUM
				}
			}

			# Dynasty Warfare Perk
			if = {
				limit = {
					scope:attacker = {
						has_dynasty = yes
						dynasty = {
							has_dynasty_perk = warfare_legacy_2
						}
					}
				}
				multiply = {
					value = warfare_legacy_2_discount
					desc = CB_ATTACKER_DYNASTY_WARFARE
				}
			}

			# Vassal contract in-realm cost reduction
			if = {
				limit = {
					scope:attacker = {
						is_independent_ruler = no
						vassal_contract_has_flag = vassal_contract_war_override
						liege = scope:defender.liege
					}
				}
				multiply = {
					value = war_declaration_rights_allowed_cost_reduction
					desc = "CB_ATTACKER_VASSAL_CONTRACT"
				}
			}

			if = {
				limit = {
					has_game_rule = no_cost_casus_belli_costs
				}
				multiply = {
					value = 0
					desc = CB_GAME_RULE_NO_COST
				}
			}
		}
	}

	should_invalidate = {
		OR = {
			AND = {	#A formal flowery war only invalidates if neither side is an interested participant.
				scope:attacker.faith = {
					NOT = { has_doctrine_parameter = human_sacrifice_active }
				}
				scope:defender.faith = {
					NOT = { has_doctrine_parameter = human_sacrifice_active }
				}
			}
			scope:attacker = {
				any_prisoner = { has_character_modifier = designated_human_sacrifice_modifier }
			}
		}
	}

	on_invalidated_desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:attacker = {
						any_prisoner = { has_character_modifier = designated_human_sacrifice_modifier }
					}
				}
				desc = flowery_war_cb_ended_invalid.desc_other_sacrifices_attacker
			}
			desc = flowery_war_cb_ended_invalid.desc_no_sacrificers
		}
	}

	on_invalidated = {
		
	}

	on_declaration = {
		on_declared_war = yes
	}

	on_victory_desc = {
		desc = flowery_war_cb_victory_desc
	}

	on_victory = {
		scope:attacker = { show_pow_release_message_effect = yes }
		scope:attacker = {
			if = {	#If the attacker enjoys human sacrifice, then this was sacred.
				limit = {
					faith = { has_doctrine_parameter = human_sacrifice_active }
				}
				if = {
					limit = { scope:defender.primary_title.tier = tier_county }
					add_piety = { value = minor_piety_gain }
				}
				if = {
					limit = { scope:defender.primary_title.tier = tier_duchy }
					add_piety = { value = medium_piety_gain }
				}
				if = {
					limit = { scope:defender.primary_title.tier = tier_kingdom }
					add_piety = { value = major_piety_gain }
				}
				if = {
					limit = { scope:defender.primary_title.tier = tier_empire }
					add_piety = { value = massive_piety_gain }
				}
			}
			else = {	#If the attacker has since stopped enjoying human sacrifice, then the war was just prestigious.
				if = {
					limit = { scope:defender.primary_title.tier = tier_county }
					add_prestige = { value = minor_prestige_gain }
				}
				if = {
					limit = { scope:defender.primary_title.tier = tier_duchy }
					add_prestige = { value = medium_prestige_gain }
				}
				if = {
					limit = { scope:defender.primary_title.tier = tier_kingdom }
					add_prestige = { value = major_prestige_gain }
				}
				if = {
					limit = { scope:defender.primary_title.tier = tier_empire }
					add_prestige = { value = massive_prestige_gain }
				}
			}
		}
		scope:defender = {
			if = {	#If the defender shares the faith of the attacker, then this war was sacred for them too.
				limit = {
					faith = { this = scope:attacker.faith }
				}
				if = {
					limit = { scope:attacker.primary_title.tier = tier_county }
					add_piety = {
						value = minor_piety_gain
						multiply = 0.75
					}
				}
				if = {
					limit = { scope:attacker.primary_title.tier = tier_duchy }
					add_piety = {
						value = medium_piety_gain
						multiply = 0.75
					}
				}
				if = {
					limit = { scope:attacker.primary_title.tier = tier_kingdom }
					add_piety = {
						value = major_piety_gain
						multiply = 0.75
					}
				}
				if = {
					limit = { scope:attacker.primary_title.tier = tier_empire }
					add_piety = {
						value = massive_piety_gain
						multiply = 0.75
					}
				}
			}
			else = {	#Otherwise, the war just sucked.
				if = {
					limit = { scope:defender.primary_title.tier = tier_county }
					add_prestige = { value = minor_prestige_loss }
				}
				if = {
					limit = { scope:defender.primary_title.tier = tier_duchy }
					add_prestige = { value = medium_prestige_loss }
				}
				if = {
					limit = { scope:defender.primary_title.tier = tier_kingdom }
					add_prestige = { value = major_prestige_loss }
				}
				if = {
					limit = { scope:defender.primary_title.tier = tier_empire }
					add_prestige = { value = massive_prestige_loss }
				}
			}
		}
		scope:attacker = {	#If the attacker has the appropriate tenet, grab some suitable courtiers from the defender.
			if = {
				limit = {
					faith = { has_doctrine_parameter = human_sacrifice_active }
				}
				custom_tooltip = flowery_war_cb_prisoners.tt_attacker
				hidden_effect = {
					scope:defender = {
						every_courtier_or_guest = { add_to_list = potential_captives_list }
						random_in_list = {	#Nab someone related to the defender, preferring tangentials.
							list = potential_captives_list
							limit = {
								OR = {
									AND = {
										has_dynasty = yes
										dynasty = scope:defender.dynasty
									}
									is_consort_of = scope:defender
								}
							}
							alternative_limit = { always = yes }
							weight = {
								base = 1
								modifier = {
									add = 25
									NOT = { is_close_family_of = scope:defender }
								}
								modifier = {
									add = 15
									NOT = { is_extended_family_of = scope:defender }
								}
							}
							add_to_list = definite_captives_list
							save_temporary_scope_as = future_captive
								scope:attacker = {
									imprison = {
										target = scope:future_captive
										type = dungeon
									}
								}
						}
						random_in_list = {	#Always try to grab a second person.
							list = potential_captives_list
							limit = {
								NOT = { is_in_list = definite_captives_list }
							}
							weight = {
								base = 1
								modifier = {
									add = 40
									NOT = {
										any_claim = {
											useful_courtier_or_guest_claim_trigger = { RULER = scope:defender }
										}
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_chancellor_trigger = { COURT_OWNER = scope:defender }
										diplomacy >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_marshal_trigger = { COURT_OWNER = scope:defender }
										martial >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_steward_trigger = { COURT_OWNER = scope:defender }
										stewardship >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_spymaster_trigger = { COURT_OWNER = scope:defender }
										intrigue >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NOT = { learning >= high_skill_rating }
								}
								modifier = {
									add = 10
									NOT = {	prowess >= high_skill_rating }
								}
								modifier = {
									add = 50
									NOT = { is_close_or_extended_family_of = scope:defender }
								}
							}
							add_to_list = definite_captives_list
							save_temporary_scope_as = future_captive
								scope:attacker = {
									imprison = {
										target = scope:future_captive
										type = dungeon
									}
								}
						}
						random_in_list = {	#Always try to grab a third person.
							list = potential_captives_list
							limit = {
								NOT = { is_in_list = definite_captives_list }
							}
							weight = {
								base = 1
								modifier = {
									add = 40
									NOT = {
										any_claim = {
											useful_courtier_or_guest_claim_trigger = { RULER = scope:defender }
										}
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_chancellor_trigger = { COURT_OWNER = scope:defender }
										diplomacy >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_marshal_trigger = { COURT_OWNER = scope:defender }
										martial >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_steward_trigger = { COURT_OWNER = scope:defender }
										stewardship >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_spymaster_trigger = { COURT_OWNER = scope:defender }
										intrigue >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NOT = { learning >= high_skill_rating }
								}
								modifier = {
									add = 10
									NOT = {	prowess >= high_skill_rating }
								}
								modifier = {
									add = 50
									NOT = { is_close_or_extended_family_of = scope:defender }
								}
							}
							add_to_list = definite_captives_list
							save_temporary_scope_as = future_captive
								scope:attacker = {
									imprison = {
										target = scope:future_captive
										type = dungeon
									}
								}
						}
						if = {	#If at least duchy-tier, try to grab a fourth person.
							limit = { primary_title.tier >= tier_duchy }
							random_in_list = {
								list = potential_captives_list
								limit = {
									NOT = { is_in_list = definite_captives_list }
								}
								weight = {
									base = 1
									modifier = {
										add = 40
										NOT = {
											any_claim = {
												useful_courtier_or_guest_claim_trigger = { RULER = scope:defender }
											}
										}
									}
									modifier = {
										add = 10
										NAND = {
											can_be_chancellor_trigger = { COURT_OWNER = scope:defender }
											diplomacy >= high_skill_rating
										}
									}
									modifier = {
										add = 10
										NAND = {
											can_be_marshal_trigger = { COURT_OWNER = scope:defender }
											martial >= high_skill_rating
										}
									}
									modifier = {
										add = 10
										NAND = {
											can_be_steward_trigger = { COURT_OWNER = scope:defender }
											stewardship >= high_skill_rating
										}
									}
									modifier = {
										add = 10
										NAND = {
											can_be_spymaster_trigger = { COURT_OWNER = scope:defender }
											intrigue >= high_skill_rating
										}
									}
									modifier = {
										add = 10
										NOT = { learning >= high_skill_rating }
									}
									modifier = {
										add = 10
										NOT = { prowess >= high_skill_rating }
									}
								}
								add_to_list = definite_captives_list
								save_temporary_scope_as = future_captive
								scope:attacker = {
									imprison = {
										target = scope:future_captive
										type = dungeon
									}
								}
							}
						}
						if = {	#If at least kingdom-tier, try to grab a fifth person.
							limit = { primary_title.tier >= tier_kingdom }
							random_in_list = {
								list = potential_captives_list
								limit = {
									NOT = { is_in_list = definite_captives_list }
								}
								weight = {
									base = 1
									modifier = {
										add = 40
										NOT = {
											any_claim = {
												useful_courtier_or_guest_claim_trigger = { RULER = scope:defender }
											}
										}
									}
									modifier = {
										add = 10
										NAND = {
											can_be_chancellor_trigger = { COURT_OWNER = scope:defender }
											diplomacy >= high_skill_rating
										}
									}
									modifier = {
										add = 10
										NAND = {
											can_be_marshal_trigger = { COURT_OWNER = scope:defender }
											martial >= high_skill_rating
										}
									}
									modifier = {
										add = 10
										NAND = {
											can_be_steward_trigger = { COURT_OWNER = scope:defender }
											stewardship >= high_skill_rating
										}
									}
									modifier = {
										add = 10
										NAND = {
											can_be_spymaster_trigger = { COURT_OWNER = scope:defender }
											intrigue >= high_skill_rating
										}
									}
									modifier = {
										add = 10
										NOT = { learning >= high_skill_rating }
									}
									modifier = {
										add = 10
										NOT = { prowess >= high_skill_rating }
									}
								}
								add_to_list = definite_captives_list
								save_temporary_scope_as = future_captive
								scope:attacker = {
									imprison = {
										target = scope:future_captive
										type = dungeon
									}
								}
							}
						}
					}
					every_in_list = {
						list = definite_captives_list
						add_character_modifier = {
							modifier = designated_human_sacrifice_modifier
							years = 2
						}
					}
					trigger_event = {
						id = human_sacrifice.0001
						years = 2
					}
				}
			}
		}

		# Prestige for the attacker's war allies
		add_from_contribution_attackers = {
			prestige = {
				value = major_prestige_value
			}
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = {
				value = major_prestige_value
			}
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Truce
		add_truce_attacker_victory_effect = yes

	}

	on_white_peace_desc = {
		desc = flowery_war_cb_white_peace_desc
	}

	on_white_peace = {
		scope:attacker = { show_pow_release_message_effect = yes }
		scope:attacker = {	#Piety loss for the attacker if appropriate, otherwise prestige loss.
			if = {
				limit = {
					faith = { has_doctrine_parameter = human_sacrifice_active }
				}
				add_piety = religious_cb_piety_white_peace
			}
			else = {
				add_prestige = {
					value = medium_prestige_loss
				}
			}
		}



		# Prestige for the attacker's war allies
		add_from_contribution_attackers = {
			prestige = {
				value = major_prestige_value
			}
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = {
				value = major_prestige_value
			}
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Truce
		add_truce_attacker_victory_effect = yes

	}

	on_defeat_desc = {
		desc = flowery_war_cb_defeat_desc
	}

	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		scope:attacker = {
			if = {	#If the attacker shares the faith of the defender, and both still enjoy human sacrifice, then this war was at least sacred for them.
				limit = {
					faith = {
						this = scope:defender.faith
						has_doctrine_parameter = human_sacrifice_active
					}
				}
				if = {
					limit = { scope:defender.primary_title.tier = tier_county }
					add_piety = {
						value = minor_piety_gain
						multiply = 0.75
					}
				}
				if = {
					limit = { scope:defender.primary_title.tier = tier_duchy }
					add_piety = {
						value = medium_piety_gain
						multiply = 0.75
					 }
				}
				if = {
					limit = { scope:defender.primary_title.tier = tier_kingdom }
					add_piety = {
						value = major_piety_gain
						multiply = 0.75
					}
				}
				if = {
					limit = { scope:defender.primary_title.tier = tier_empire }
					add_piety = {
						value = massive_piety_gain
						multiply = 0.75
					}
				}
			}
			else = {	#Otherwise, the war just sucked.
				if = {
					limit = { scope:attacker.primary_title.tier = tier_county }
					add_prestige = { value = minor_prestige_loss }
				}
				if = {
					limit = { scope:attacker.primary_title.tier = tier_duchy }
					add_prestige = { value = medium_prestige_loss }
				}
				if = {
					limit = { scope:attacker.primary_title.tier = tier_kingdom }
					add_prestige = { value = major_prestige_loss }
				}
				if = {
					limit = { scope:attacker.primary_title.tier = tier_empire }
					add_prestige = { value = massive_prestige_loss }
				}
			}
		}
		scope:defender = {
			if = {	#If the defender has the same faith as the attacker, and that faith still enjoys human sacrifice, then this was sacred.
				limit = {
					faith = {
						this = scope:attacker.faith
						has_doctrine_parameter = human_sacrifice_active
					}
				}
				if = {
					limit = { scope:attacker.primary_title.tier = tier_county }
					add_piety = { value = minor_piety_gain }
				}
				if = {
					limit = { scope:attacker.primary_title.tier = tier_duchy }
					add_piety = { value = medium_piety_gain }
				}
				if = {
					limit = { scope:attacker.primary_title.tier = tier_kingdom }
					add_piety = { value = major_piety_gain }
				}
				if = {
					limit = { scope:attacker.primary_title.tier = tier_empire }
					add_piety = { value = massive_piety_gain }
				}
			}
			else = {	#Otherwise, the war was just prestigious.
				if = {
					limit = { scope:attacker.primary_title.tier = tier_county }
					add_prestige = { value = minor_prestige_gain }
				}
				if = {
					limit = { scope:attacker.primary_title.tier = tier_duchy }
					add_prestige = { value = medium_prestige_gain }
				}
				if = {
					limit = { scope:attacker.primary_title.tier = tier_kingdom }
					add_prestige = { value = major_prestige_gain }
				}
				if = {
					limit = { scope:attacker.primary_title.tier = tier_empire }
					add_prestige = { value = massive_prestige_gain }
				}
			}
		}
		scope:defender = {	#If the defender has the appropriate tenet, and doesn't already have sacrificial victims, grab some suitable courtiers from the attacker.
			if = {
				limit = {
					faith = { has_doctrine_parameter = human_sacrifice_active }
					NOT = {	#Must not have any leftover sacrificial prisoners, lest the defender get borked sacrifice-accounting events.
						any_prisoner = { has_character_modifier = designated_human_sacrifice_modifier }
					}
				}
				custom_tooltip = flowery_war_cb_prisoners.tt_defender
				hidden_effect = {
					scope:attacker = {
						every_courtier_or_guest = { add_to_list = potential_captives_list }
						random_in_list = {	#Nab someone related to the attacker, preferring tangentials.
							list = potential_captives_list
							limit = {
								OR = {
									AND = {
										has_dynasty = yes
										dynasty = scope:attacker.dynasty
									}
									is_consort_of = scope:attacker
								}
							}
							alternative_limit = { always = yes }
							weight = {
								base = 1
								modifier = {
									add = 25
									NOT = { is_close_family_of = scope:attacker }
								}
								modifier = {
									add = 15
									NOT = { is_extended_family_of = scope:attacker }
								}
							}
							add_to_list = definite_captives_list
							save_temporary_scope_as = future_captive
								scope:defender = {
									imprison = {
										target = scope:future_captive
										type = dungeon
									}
								}
						}
						random_in_list = {	#Always try to grab a second person.
							list = potential_captives_list
							limit = {
								NOT = { is_in_list = definite_captives_list }
							}
							weight = {
								base = 1
								modifier = {
									add = 40
									NOT = {
										any_claim = {
											useful_courtier_or_guest_claim_trigger = { RULER = scope:attacker }
										}
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_chancellor_trigger = { COURT_OWNER = scope:attacker }
										diplomacy >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_marshal_trigger = { COURT_OWNER = scope:attacker }
										martial >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_steward_trigger = { COURT_OWNER = scope:attacker }
										stewardship >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_spymaster_trigger = { COURT_OWNER = scope:attacker }
										intrigue >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NOT = { learning >= high_skill_rating }
								}
								modifier = {
									add = 10
									NOT = {	prowess >= high_skill_rating }
								}
								modifier = {
									add = 50
									NOT = { is_close_or_extended_family_of = scope:defender }
								}
							}
							add_to_list = definite_captives_list
							save_temporary_scope_as = future_captive
								scope:defender = {
									imprison = {
										target = scope:future_captive
										type = dungeon
									}
								}
						}
						random_in_list = {	#Always try to grab a third person.
							list = potential_captives_list
							limit = {
								NOT = { is_in_list = definite_captives_list }
							}
							weight = {
								base = 1
								modifier = {
									add = 40
									NOT = {
										any_claim = {
											useful_courtier_or_guest_claim_trigger = { RULER = scope:attacker }
										}
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_chancellor_trigger = { COURT_OWNER = scope:attacker }
										diplomacy >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_marshal_trigger = { COURT_OWNER = scope:attacker }
										martial >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_steward_trigger = { COURT_OWNER = scope:attacker }
										stewardship >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NAND = {
										can_be_spymaster_trigger = { COURT_OWNER = scope:attacker }
										intrigue >= high_skill_rating
									}
								}
								modifier = {
									add = 10
									NOT = { learning >= high_skill_rating }
								}
								modifier = {
									add = 10
									NOT = {	prowess >= high_skill_rating }
								}
								modifier = {
									add = 50
									NOT = { is_close_or_extended_family_of = scope:defender }
								}
							}
							add_to_list = definite_captives_list
							save_temporary_scope_as = future_captive
								scope:defender = {
									imprison = {
										target = scope:future_captive
										type = dungeon
									}
								}
						}
						if = {	#If at least duchy-tier, try to grab a fourth person.
							limit = { primary_title.tier >= tier_duchy }
							random_in_list = {
								list = potential_captives_list
								limit = {
									NOT = { is_in_list = definite_captives_list }
								}
								weight = {
									base = 1
									modifier = {
										add = 40
										NOT = {
											any_claim = {
												useful_courtier_or_guest_claim_trigger = { RULER = scope:attacker }
											}
										}
									}
									modifier = {
										add = 10
										NAND = {
											can_be_chancellor_trigger = { COURT_OWNER = scope:attacker }
											diplomacy >= high_skill_rating
										}
									}
									modifier = {
										add = 10
										NAND = {
											can_be_marshal_trigger = { COURT_OWNER = scope:attacker }
											martial >= high_skill_rating
										}
									}
									modifier = {
										add = 10
										NAND = {
											can_be_steward_trigger = { COURT_OWNER = scope:attacker }
											stewardship >= high_skill_rating
										}
									}
									modifier = {
										add = 10
										NAND = {
											can_be_spymaster_trigger = { COURT_OWNER = scope:attacker }
											intrigue >= high_skill_rating
										}
									}
									modifier = {
										add = 10
										NOT = { learning >= high_skill_rating }
									}
									modifier = {
										add = 10
										NOT = { prowess >= high_skill_rating }
									}
								}
								add_to_list = definite_captives_list
								save_temporary_scope_as = future_captive
								scope:defender = {
									imprison = {
										target = scope:future_captive
										type = dungeon
									}
								}
							}
						}
						if = {	#If at least kingdom-tier, try to grab a fifth person.
							limit = { primary_title.tier >= tier_kingdom }
							random_in_list = {
								list = potential_captives_list
								limit = {
									NOT = { is_in_list = definite_captives_list }
								}
								weight = {
									base = 1
									modifier = {
										add = 40
										NOT = {
											any_claim = {
												useful_courtier_or_guest_claim_trigger = { RULER = scope:attacker }
											}
										}
									}
									modifier = {
										add = 10
										NAND = {
											can_be_chancellor_trigger = { COURT_OWNER = scope:attacker }
											diplomacy >= high_skill_rating
										}
									}
									modifier = {
										add = 10
										NAND = {
											can_be_marshal_trigger = { COURT_OWNER = scope:attacker }
											martial >= high_skill_rating
										}
									}
									modifier = {
										add = 10
										NAND = {
											can_be_steward_trigger = { COURT_OWNER = scope:attacker }
											stewardship >= high_skill_rating
										}
									}
									modifier = {
										add = 10
										NAND = {
											can_be_spymaster_trigger = { COURT_OWNER = scope:attacker }
											intrigue >= high_skill_rating
										}
									}
									modifier = {
										add = 10
										NOT = { learning >= high_skill_rating }
									}
									modifier = {
										add = 10
										NOT = { prowess >= high_skill_rating }
									}
								}
								add_to_list = definite_captives_list
								save_temporary_scope_as = future_captive
								scope:defender = {
									imprison = {
										target = scope:future_captive
										type = dungeon
									}
								}
							}
						}
					}
					every_in_list = {
						list = definite_captives_list
						add_character_modifier = {
							modifier = designated_human_sacrifice_modifier
							years = 2
						}
					}
					trigger_event = {
						id = human_sacrifice.0001
						years = 2
					}
				}
			}
		}

		# Prestige for the attacker's war allies
		add_from_contribution_attackers = {
			prestige = {
				value = major_prestige_value
			}
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Prestige for the defender's war allies
		add_from_contribution_defenders = {
			prestige = {
				value = major_prestige_value
			}
			opinion = {
				modifier = contributed_in_war
			}
		}

		# Truce
		add_truce_attacker_victory_effect = yes

		scope:attacker = { save_temporary_scope_as = loser }
		on_lost_aggression_war_discontent_loss = yes
	}

	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit

	attacker_allies_inherit = yes
	defender_allies_inherit = yes

	war_name = "FLOWERY_WAR_CB_NAME"
	war_name_base = "FLOWERY_WAR_WAR_NAME_BASE"
	cb_name = "FLOWERY_WAR_CB_NAME"
	interface_priority = 80

	ticking_war_score_targets_entire_realm = yes
	attacker_ticking_warscore = 0
	attacker_wargoal_percentage = 0.01
	attacker_score_from_occupation_scale = 50
	defender_score_from_occupation_scale = 50
	attacker_score_from_battles_scale = 200
	defender_score_from_battles_scale = 200

	max_attacker_score_from_battles = 200
	max_defender_score_from_battles = 200

	max_ai_diplo_distance_to_title = 500
	min_ai_score = 100
}

undirected_great_holy_war = {
	group = religious_script_only

	war_name = "GREAT_HOLY_WAR_WAR_NAME"

	target_titles = all
	target_title_tier = all
	target_de_jure_regions_above = yes
	use_de_jure_wargoal_only = yes
	check_all_defenders_for_ticking_war_score = yes # All defenders land in the Crusade Kingdom should count
	defender_ticking_warscore = 0.125
	attacker_ticking_warscore = 0.125
	max_defender_score_from_occupation = 50
	max_attacker_score_from_occupation = 150
	full_occupation_by_defender_gives_victory = no
	imprisonment_by_attacker_give_war_score = no
	imprisonment_by_defender_give_war_score = no
	attacker_capital_gives_war_score = no
	defender_capital_gives_war_score = no
	is_great_holy_war = yes
	landless_attacker_needs_armies = no
	
	defender_score_from_battles_scale = 125
	attacker_score_from_battles_scale = 125
	
	max_defender_score_from_battles = 150

	occupation_participation_mult = 0.5
	siege_participation_mult = 1
	battle_participation_mult = 7.5
	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit

	white_peace_possible = no

	allowed_for_character = {
		always = yes
	}

	allowed_for_character_display_regardless = {
		piety_level >= 1
		trigger_if = {
			limit = { NOT = { has_game_rule = holy_war_cb_default } }
			NOT = { has_character_modifier = mgr_recent_holy_war }
		}
	}

	valid_to_start = {
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = { #Any ruler in the dejure of the targeted Kingdom belongs to a Faith that the HoF's Faith is hostile enough to Holy War against.
				continue = { tier > tier_county }
				tier = tier_county
				scope:attacker.faith = {
					is_hostile_enough_for_holy_war_trigger = { FAITH = prev.holder.top_liege.faith }
				}
			}
		}
	}

	on_declaration = {
		scope:attacker.faith.great_holy_war = {
			if = { #Clear forced participation flag.
				limit = {
					any_pledged_attacker = {
						has_character_flag = variable_ghw_papal_hooked_pledge
					}
				}
				every_pledged_attacker = {
					limit = {
						has_character_flag = variable_ghw_papal_hooked_pledge
					}
					remove_character_flag = variable_ghw_papal_hooked_pledge
				}
			}
		}
		scope:attacker.faith.great_holy_war.ghw_target_title = {
			save_scope_as = target_kingdom
		}
		scope:attacker = { #Loc keys for localization fluff.
			save_scope_as = ghw_sponsor
		}
		scope:defender = {
			save_scope_as = ghw_defender
		}
		scope:attacker.faith = {
			save_scope_as = the_faith
		}
		scope:attacker.faith.great_holy_war = {
			save_scope_as = the_ghw
		}
		force_truce_GHW_defenders_effect = yes
		every_player = { #Fluff for attackers/defenders
			limit = {
				scope:attacker.faith.great_holy_war = {
					OR = {
						has_pledged_attacker = prev
						has_pledged_defender = prev
					}
				}
			}
			trigger_event = great_holy_war.0051
		}
		on_declared_war = yes
		scope:attacker = { holy_war_cb_cooldown_effect = yes }
		scope:attacker.faith.great_holy_war = {
			divide_war_chest = {
				fraction = ghw_war_start_handout_percentage
			}
		}

		debug_log = undirected_ghw_debug_loc
	}

	on_invalidated_desc = msg_invalidate_war_title
	
	on_invalidated = {
		scope:attacker.faith.great_holy_war.ghw_target_title = {
			save_scope_as = target_kingdom
		}
		scope:attacker = { #Loc keys for localization fluff.
			save_scope_as = ghw_sponsor
		}
		scope:defender = {
			save_scope_as = ghw_defender
		}
		scope:attacker.faith = {
			save_scope_as = the_faith
		}
		scope:attacker.faith.great_holy_war = {
			save_scope_as = the_ghw
		}
		every_player = {
			limit = { is_within_diplo_range = { CHARACTER = scope:defender } }
			trigger_event = great_holy_war.0056
		}
		hidden_effect = {
			if = { #Clear pledged gold flag.
				limit = {
					any_player = {
						has_character_flag = ghw_pledged_gold
					}
				}
				every_player = {
					limit = {
						has_character_flag = ghw_pledged_gold
					}
					remove_character_flag = ghw_pledged_gold
				}
			}
		}

		debug_log = "Undirected Great Holy War invalidated"
	}

	on_victory_desc = {
		desc = great_holy_war_victory_desc
	}

	on_victory = {
		scope:attacker = { show_pow_release_message_effect = yes }
		create_title_and_vassal_change = {
			type = conquest_holy_war
			save_scope_as = change
			add_claim_on_loss = yes
		}
		scope:attacker.faith.great_holy_war.ghw_target_title = { #Loc keys for localization fluff.
			save_scope_as = target_kingdom
		}
		scope:attacker = {
			save_scope_as = ghw_sponsor
		}
		scope:defender = {
			save_scope_as = ghw_defender
		}
		scope:attacker.faith = {
			save_scope_as = the_faith
		}
		scope:attacker.faith.great_holy_war = {
			save_scope_as = the_ghw
		}

		scope:attacker = {	#Achievement setup.
			every_character_war = {
				limit = {
					is_attacker = scope:attacker
					is_defender = scope:defender
				}
				every_war_attacker = {
					add_achievement_flag = { FLAG = achievement_for_the_faith_flag }
				}
			}
		}

		hidden_effect = {
			if = { #Recipient is going to be the Papal's chosen Recipient or, if the main contributor is overriding the Papal will, his Beneficiary.
				limit = {
					exists = scope:attacker.faith.great_holy_war.ghw_title_recipient
				}
				scope:attacker.faith.great_holy_war.ghw_title_recipient = {
					save_scope_as = title_recipient
				}
			}
			else = { #If no Recipient or Beneficiary, check for any random claimants that might have cropped in the meantime.
				undirected_ghw_recipient_selection_on_victory_effect = yes
			}
			if = {
				limit = {
					exists = scope:the_ghw
					NOT = { exists = scope:title_recipient }
					scope:the_ghw = {
						has_variable = var_fallback_recipient
						var:var_fallback_recipient = {
							is_alive = yes
						}
					}
				}
				scope:the_ghw = {
					var:var_fallback_recipient = {
						save_scope_as = title_recipient
						set_designated_winner = scope:title_recipient
					}
				}
			}
			if = { #If for some reason there is no title recipient selected yet, create character from scratch.
				limit = {
					exists = scope:the_ghw
					NOT = { exists = scope:title_recipient }
					NOT = {
						scope:the_ghw = {
							has_variable = var_fallback_recipient
						}
					}
				}
				create_character = {
					location = scope:ghw_sponsor.capital_province
					template = new_commander_character
					faith = scope:the_faith
					culture = scope:ghw_sponsor
					save_scope_as = title_recipient
					gender_female_chance = {
						if = {
							limit = { scope:the_faith = { has_doctrine = doctrine_gender_male_dominated } }
							add = 0
						}
						else_if = {
							limit = { scope:the_faith = { has_doctrine = doctrine_gender_female_dominated } }
							add = 100
						}
						else = {
							add = 50
						}
					}
				}
				scope:the_faith.great_holy_war = {
					set_designated_winner = scope:title_recipient
				}
			}
			if = { #Check if Recipient is landless to give him the Crusader King buff.
				limit = {
					exists = scope:title_recipient
					scope:title_recipient = { is_ruler = no }
				}
				scope:title_recipient = {
					add_character_flag = {
						flag = ghw_recipient_will_be_crusader_king
					}
				}
			}
		}
		every_in_list = {
			list = target_titles
			save_scope_as = target
		}

		if = { #Tooltip explaining who gets the first title when a Recipient exists (otherwise avoid or it will show the fallback character).
			limit = {
				exists = scope:attacker.faith.great_holy_war.ghw_title_recipient
			}
			show_as_tooltip = {
				scope:target = {
					change_title_holder = {
						holder = scope:attacker.faith.great_holy_war.ghw_title_recipient
						change = scope:change
					}
				}
			}
		}
		else_if = {
			limit = {
				exists = scope:the_ghw
				scope:the_ghw = {
					has_variable = var_fallback_recipient
				}
			}
			scope:the_ghw = {
				var:var_fallback_recipient = {
					save_scope_as = ghw_fallback_recipient
				}
			}
			show_as_tooltip = {
				scope:target = {
					change_title_holder = {
						holder = scope:ghw_fallback_recipient
						change = scope:change
					}
				}
			}
		}

		# Create a dynamic title if the kingdom itself can't be created/taken
		if = {
			limit = { #In the event that, for ex. k_jerusalem exists and is held by a foreign ruler who is not involved in the war in any way.
				exists = scope:the_ghw
				exists = scope:target_kingdom
				exists = scope:target_kingdom.holder
				scope:the_ghw = {
					NOR = {
						#Do not create a titular title if the target is held by a defender.
						has_pledged_defender = scope:target_kingdom.holder
						has_forced_defender = scope:target_kingdom.holder
						#Or, if the title_recipient is the current holder of the target_kingdom.
						scope:title_recipient = scope:target_kingdom.holder
					}
				}
			}
			hidden_effect = {
				random_list = {
					50 = {
						create_dynamic_title = {
							tier = kingdom
							name = CRUSADER_STATE_CRUSADER_TITLE_NAME
						}
					}
					50 = {
						create_dynamic_title = {
							tier = kingdom
							name = CRUSADER_STATE_FAITH_TITLE_NAME
						}
					}
				}
				scope:new_title = {
					change_title_holder = {
						holder = scope:title_recipient
						change = scope:change
					}
				}
			}
			scope:the_ghw = {
				do_ghw_title_handout = scope:change
				divide_war_chest = {}
			}
			scope:new_title = { generate_coa = yes }
		}
		else_if = { #Otherwise hand out titles normally.
			limit = {
				exists = scope:the_ghw
			}
			hidden_effect = {
				if = {
					limit = {
						NAND = {
							exists = scope:target.holder
							scope:target.holder = scope:title_recipient
						}
					}
					scope:target = {
						change_title_holder = {
							holder = scope:title_recipient
							change = scope:change
						}
					}
				}
			}
			scope:the_ghw = {
				do_ghw_title_handout = scope:change
				divide_war_chest = {}
			}
		}

		hidden_effect = {
			every_player = { #Fluff for players
				limit = {
					OR = {
						this = scope:attacker
						this = scope:defender
						any_war_enemy = { this = scope:attacker }
						any_war_enemy = { this = scope:defender }
						is_within_diplo_range = { CHARACTER = scope:defender }
					}
				}
				trigger_event = great_holy_war.0052 #Differentiate depending on faith (compared to scope:attacker)
			}

			if = {
				limit = { exists = scope:title_recipient }
				scope:title_recipient = {
					every_realm_county = { # Crusading troops seizing control
						change_county_control = 50
					}
					every_realm_province = {
						refill_levy = yes
						refill_garrison = yes
					}
				}
			}
			if = { #If Recipient was landless give him the Crusader King buff.
				limit = {
					exists = scope:title_recipient
					scope:title_recipient = { has_character_flag = ghw_recipient_will_be_crusader_king }
				}
				scope:title_recipient = {
					add_trait = crusader_king
					remove_character_flag = ghw_recipient_will_be_crusader_king
					add_realm_law_skip_effects = crown_authority_1
					
					if = { # Set the appropriate Succession Law
						limit = {
							can_have_single_heir_succession_law_trigger = yes
							NOT = {
								has_realm_law = single_heir_succession_law
							}
						}
						add_realm_law_skip_effects = single_heir_succession_law
					}
					else_if = {
						limit = {
							can_have_high_partition_succession_law_trigger = yes
							NOT = {
								has_realm_law = high_partition_succession_law
							}
						}
						add_realm_law_skip_effects = high_partition_succession_law
					}
					else_if = {
						limit = {
							can_have_partition_succession_law_trigger = yes
							NOT = {
								has_realm_law = partition_succession_law
							}
						}
						add_realm_law_skip_effects = partition_succession_law
					}
					
					spawn_army = { # Spawn a temporary army so that the Crusader King isn't helpless, symbolizing Crusaders staying behind
						levies = {
							value = 100
							scope:attacker.faith.great_holy_war = {
								every_pledged_attacker = {
									add = 100
								}
							}
						}
						men_at_arms = {
							type = teutonic_knights
							stacks = {
								value = 1
								scope:attacker.faith.great_holy_war = {
									every_pledged_attacker = {
										add = 0.2
									}
								}
							}
						}
						inheritable = no
						uses_supply = no
						location = capital_province
						name = house_beneficiary_won_ghw
					}
					every_vassal = { # Vassals should not want to revolt too soon after inaguration
						add_opinion = {
							modifier = respect_opinion
							target = scope:title_recipient
							opinion = 50
						}
					}
					add_gold = 500 # Some extra plunder
					add_prestige = 500
					add_piety = 500
				}
			}
			scope:attacker.faith.great_holy_war = { #Give Prestige/Piety modifier to Dynasty/House of the winners' Beneficiaries (only if they gained any land in the GHW).
				every_pledged_attacker = {
					limit = { exists = ghw_beneficiary }
					ghw_beneficiary = { trigger_event = great_holy_war.0061 }
				}
				if = { #Chance to switch-play to Crusader King.
					limit = {
						exists = scope:title_recipient
						exists = scope:title_recipient.dynasty
						scope:title_recipient = {
							is_ai = yes
							has_trait = crusader_king
						}
						any_pledged_attacker = {
							is_ai = no
							dynasty = scope:title_recipient.dynasty
						}
					}
					random_pledged_attacker = {
						limit = {
							is_ai = no
							dynasty = scope:title_recipient.dynasty
							scope:title_recipient = {
								is_ai = yes
								has_trait = crusader_king
							}
						}
						trigger_event = { id = great_holy_war.0070 days = 5 }
					}
				}
			}
		}
		
		#Set 30 years cooldown.
		hidden_effect = {
			scope:attacker.faith = {
				set_variable = {
					name = variable_ghw_cooldown
					value = yes
					years = 30
				}
			}
			if = { #Clear pledged gold flag.
				limit = {
					any_player = {
						has_character_flag = ghw_pledged_gold
					}
				}
				every_player = {
					limit = {
						has_character_flag = ghw_pledged_gold
					}
					remove_character_flag = ghw_pledged_gold
				}
			}
		}

		#Fervor
		if = {
			limit = { has_game_rule = default_fervor_hs }
			scope:attacker.faith = { change_fervor = -30 }
			scope:defender.faith = { change_fervor = 25 }
		}
		else_if = {
			limit = { has_game_rule = reduced_fervor_hs }
			scope:attacker.faith = { change_fervor = -15 }
			scope:defender.faith = { change_fervor = 12.5 }
		}
		else_if = {
			limit = { has_game_rule = inversed_fervor_hs }
			scope:attacker.faith = { change_fervor = 30 }
			scope:defender.faith = { change_fervor = -25 }
		}
		else_if = {
			limit = { has_game_rule = rinversed_fervor_hs }
			scope:attacker.faith = { change_fervor = 15 }
			scope:defender.faith = { change_fervor = -12.5 }
		}
		else = { } #Nothing


		#Truce between the chief beneficiary & all defenders
		add_truce_GHW_attacker_victory_effect = yes
		debug_log = undirected_ghw_victory_debug_message
	}

	on_white_peace_desc = {
		desc = WHITE_PEACE_DISABLED
	}

	on_defeat_desc = {
		desc = great_holy_war_defeat_desc
	}

	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		scope:attacker.faith.great_holy_war = {
			divide_war_chest = {
				defenders = yes
				gold = no
			}
		}

		scope:attacker = {
			faith = {
				change_fervor = -15
			}
		}
		scope:attacker.faith.great_holy_war.ghw_target_title = { #Loc keys for localization fluff.
			save_scope_as = target_kingdom
		}
		scope:attacker = {
			save_scope_as = ghw_sponsor
		}
		scope:defender = {
			save_scope_as = ghw_defender
		}
		scope:attacker.faith = {
			save_scope_as = the_faith
		}
		scope:attacker.faith.great_holy_war = {
			save_scope_as = the_ghw
		}

		scope:defender = {	#Achievement setup.
			every_character_war = {
				limit = {
					is_attacker = scope:attacker
					is_defender = scope:defender
				}
				every_war_defender = {
					add_achievement_flag = { FLAG = achievement_for_the_faith_flag }
				}
			}
		}

		every_player = { #Fluff for players
			limit = {
				OR = {
					this = scope:attacker
					this = scope:defender
					any_war_enemy = { this = scope:attacker }
					any_war_enemy = { this = scope:defender }
					is_within_diplo_range = { CHARACTER = scope:defender }
				}
			}
			trigger_event = great_holy_war.0053 #Differentiate depending on faith (compared to scope:attacker)
		}

		# Truce
		add_truce_attacker_defeat_effect = yes

		#Set 30 years cooldown.
		hidden_effect = {
			scope:attacker.faith = {
				set_variable = {
					name = variable_ghw_cooldown
					value = yes
					years = 30
				}
			}
			if = { #Clear pledged gold flag.
				limit = {
					any_player = {
						has_character_flag = ghw_pledged_gold
					}
				}
				every_player = {
					limit = {
						has_character_flag = ghw_pledged_gold
					}
					remove_character_flag = ghw_pledged_gold
				}
			}
		}
		debug_log = undirected_ghw_defeat_debug_message
	}
}

directed_great_holy_war = {
	group = religious

	war_name = "GREAT_HOLY_WAR_WAR_NAME"
	cb_name = "GREAT_HOLY_WAR_WAR_CB_NAME"

	combine_into_one = yes
	should_show_war_goal_subview = yes
	mutually_exclusive_titles = { always = yes }

	target_titles = all
	target_title_tier = all
	target_de_jure_regions_above = yes
	use_de_jure_wargoal_only = yes
	check_all_defenders_for_ticking_war_score = yes # All defenders land in the Crusade Kingdom should count
	defender_ticking_warscore = 0.05
	attacker_ticking_warscore = 0.05
	max_defender_score_from_occupation = 50
	max_attacker_score_from_occupation = 150
	imprisonment_by_attacker_give_war_score = no
	defender_capital_gives_war_score = no
	is_great_holy_war = yes
	ai_only_against_neighbors = yes

	occupation_participation_mult = 0.5
	siege_participation_mult = 1
	battle_participation_mult = 7.5

	white_peace_possible = no
	gui_attacker_faith_might_join = yes
	gui_defender_faith_might_join = yes
	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit

	allowed_for_character = {
		faith = {
			exists = religious_head
			suitable_faith_for_directed_ghw_trigger = yes #Right Doctrines
			scope:attacker = this.religious_head
			religion = { exists = var:variable_ghw_unlocked } #Activated by event for every religion.
		}
	}

	allowed_for_character_display_regardless = {
		piety_level >= 1
		trigger_if = {
			limit = { NOT = { has_game_rule = holy_war_cb_default } }
			NOT = { has_character_modifier = mgr_recent_holy_war }
		}
	}

	allowed_against_character = {
		scope:attacker.faith = {
			is_hostile_enough_for_holy_war_trigger = { FAITH = scope:defender.faith }
		}
	}

	valid_to_start = {
		scope:attacker.faith = {
			NOR = {
				exists = great_holy_war
				exists = var:variable_ghw_cooldown
				AND = {
					religion = { exists = var:variable_first_ghw_cooldown } # Do not event spam after Jihads have been just unlocked.
					scope:attacker = { is_ai = yes } #If the player is the Caliph, he can bypass this and call the GHW immediately.
				}
			}
		}

		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = { #Any ruler in the dejure of the targeted Kingdom belongs to a Faith that the Caliph's Faith is hostile enough to Holy War against.
				continue = { tier > tier_county }
				tier = tier_county
				scope:attacker.faith = {
					is_hostile_enough_for_holy_war_trigger = { FAITH = prev.holder.top_liege.faith }
				}
			}
		}
	}

	on_declaration = {
		every_in_list = {
			list = target_titles
			save_scope_as = target
		}
		hidden_effect = {
			scope:attacker.faith = {
				start_great_holy_war = {
					target_character = scope:defender
					target_title = scope:target
					war = root.war
				}
				great_holy_war = {
					pledge_attacker = scope:attacker
				}
			}
		}
		scope:attacker.faith.great_holy_war = {
			every_pledged_attacker = { #Add Beneficiary automatically on victory of direct GHW for any attacker that doesn't have one already.
				limit = {
					NOT = {
						exists = ghw_beneficiary
					}
				}
				trigger_event = great_holy_war.0026
			}
		}
		scope:attacker.faith.great_holy_war.ghw_target_title = {
			save_scope_as = target_kingdom
		}
		scope:attacker = { #Loc keys for localization fluff.
			save_scope_as = ghw_sponsor
		}
		scope:defender = {
			save_scope_as = ghw_defender
		}
		scope:defender.faith = {
			save_scope_as = target_faith
		}
		scope:attacker.faith = {
			save_scope_as = the_faith
		}
		scope:attacker.faith.great_holy_war = {
			save_scope_as = the_ghw
		}
		force_truce_GHW_defenders_effect = yes
		hidden_effect = {
			every_ruler = {
				limit = {
					faith = scope:attacker.faith
					is_ai = yes
					is_landed = yes
					highest_held_title_tier >= tier_county
					NOT = { this = scope:ghw_sponsor }
					exists = scope:attacker.faith.great_holy_war
					NOT = { scope:attacker.faith.great_holy_war = { has_pledged_attacker = prev } }
				}
				trigger_event = great_holy_war.0092 #AI pledging.
			}
			every_ruler = {
				limit = {
					faith = scope:defender.faith
					is_ai = yes
					is_landed = yes
					highest_held_title_tier >= tier_county
					exists = scope:attacker.faith.great_holy_war
					NOT = { scope:attacker.faith.great_holy_war = { has_pledged_defender = prev } }
					NOT = { scope:defender = top_liege }
				}
				trigger_event = great_holy_war.0093 #AI counterpledging.
			}
			scope:attacker.faith = {
				every_faith_holy_order = {
					trigger_event = great_holy_war.0090 # Holy orders always join
				}
			}
			scope:defender.faith = {
				every_faith_holy_order = {
					trigger_event = great_holy_war.0091 # Holy orders always join
				}
			}
		}

		every_player = { #Fluff for attackers/defenders
			limit = {
				OR = {
					faith = scope:the_faith
					faith = scope:ghw_defender.faith
					is_vassal_of = scope:attacker
					is_vassal_of = scope:defender
				}
			}
			trigger_event = great_holy_war.0071
		}
		on_declared_war = yes

		debug_log = directed_ghw_debug_loc
	}

	on_victory_desc = {
		desc = great_holy_war_directed_victory_desc
	}

	on_victory = {
		scope:attacker = { show_pow_release_message_effect = yes }
		scope:attacker.faith.great_holy_war.ghw_target_title = { #Loc keys for localization fluff.
			save_scope_as = target_kingdom
		}
		scope:attacker = {
			save_scope_as = ghw_sponsor
		}
		scope:defender = {
			save_scope_as = ghw_defender
		}
		scope:attacker.faith = {
			save_scope_as = the_faith
		}
		scope:attacker.faith.great_holy_war = {
			save_scope_as = the_ghw
		}

		scope:attacker = {	#Achievement setup.
			every_character_war = {
				limit = {
					is_attacker = scope:attacker
					is_defender = scope:defender
				}
				every_war_attacker = {
					add_achievement_flag = { FLAG = achievement_for_the_faith_flag }
				}
			}
		}

		if = {
			limit = { exists = scope:attacker.faith.great_holy_war.ghw_title_recipient }
			scope:attacker.faith.great_holy_war.ghw_title_recipient = {
				save_scope_as = title_recipient
			}
		}
		else = {
			scope:attacker = {
				save_scope_as = title_recipient
			}
		}

		create_title_and_vassal_change = {
			type = conquest_holy_war
			save_scope_as = change
			add_claim_on_loss = yes
		}
		every_in_list = {
			list = target_titles
			save_scope_as = target
		}
		scope:attacker.faith.great_holy_war = {
			show_as_tooltip = { #Otherwise hand out titles normally.
				scope:target = {
					change_title_holder = {
						holder = scope:title_recipient
						change = scope:change
					}
				}
				do_ghw_title_handout = scope:change
			}
		}
		# Create a dynamic title if the kingdom itself can't be created/taken
		hidden_effect = {
			if = {
				limit = { #In the event that, for ex. k_jerusalem exists and is held by a foreign ruler who is not involved in the war in any way.
					exists = scope:target
					exists = scope:target.holder
					scope:target.holder = {
						is_alive = yes
						faith = scope:attacker.faith
					}
				}
				random_list = {
					50 = {
						create_dynamic_title = {
							tier = kingdom
							name = CRUSADER_STATE_CRUSADER_TITLE_NAME
						}
					}
					50 = {
						create_dynamic_title = {
							tier = kingdom
							name = CRUSADER_STATE_FAITH_TITLE_NAME
						}
					}
				}
				scope:attacker.faith.great_holy_war = {
					scope:new_title = {
						change_title_holder = {
							holder = scope:title_recipient
							change = scope:change
						}
					}
					do_ghw_title_handout = scope:change
				}
				scope:new_title = { generate_coa = yes }
			}
			else = { #Otherwise hand out titles normally.
				scope:attacker.faith.great_holy_war = {
					scope:target = {
						change_title_holder = {
							holder = scope:title_recipient
							change = scope:change
						}
					}
					do_ghw_title_handout = scope:change
				}
			}
			scope:title_recipient = {
				every_realm_province = {
					refill_levy = yes
					refill_garrison = yes
				}
			}
		}
		#Set 30 years cooldown.
		hidden_effect = {
			scope:attacker = {
				faith = {
					set_variable = {
						name = variable_ghw_cooldown
						value = yes
						years = 30
					}
				}
			}
			scope:attacker.faith.great_holy_war = { #Give Prestige/Piety modifier to Dynasty/House of the winners' Beneficiaries (only if they gained any land in the GHW).
				every_pledged_attacker = {
					limit = { exists = ghw_beneficiary }
					ghw_beneficiary = { trigger_event = great_holy_war.0061 }
				}
			}
		}

		#Fervor
		if = {
			limit = { has_game_rule = default_fervor_hs }
			scope:attacker.faith = { change_fervor = -30 }
			scope:defender.faith = { change_fervor = 25 }
		}
		else_if = {
			limit = { has_game_rule = reduced_fervor_hs }
			scope:attacker.faith = { change_fervor = -15 }
			scope:defender.faith = { change_fervor = 12.5 }
		}
		else_if = {
			limit = { has_game_rule = inversed_fervor_hs }
			scope:attacker.faith = { change_fervor = 30 }
			scope:defender.faith = { change_fervor = -25 }
		}
		else_if = {
			limit = { has_game_rule = rinversed_fervor_hs }
			scope:attacker.faith = { change_fervor = 15 }
			scope:defender.faith = { change_fervor = -12.5 }
		}
		else = { } #Nothing
		
		#Truce between the chief beneficiary & all defenders
		add_truce_GHW_attacker_victory_effect = yes

		every_player = { #Fluff for players
			limit = {
				OR = {
					this = scope:attacker
					this = scope:defender
					any_war_enemy = { this = scope:attacker }
					any_war_enemy = { this = scope:defender }
					is_within_diplo_range = { CHARACTER = scope:defender }
				}
			}
			trigger_event = great_holy_war.0052 #Differentiate depending on faith (compared to scope:attacker)
		}
	}

	on_white_peace_desc = {
		desc = WHITE_PEACE_DISABLED
	}

	on_defeat_desc = {
		desc = great_holy_war_defeat_desc
	}

	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		scope:attacker.faith.great_holy_war.ghw_target_title = { #Loc keys for localization fluff.
			save_scope_as = target_kingdom
		}
		scope:attacker = {
			save_scope_as = ghw_sponsor
		}
		scope:defender = {
			save_scope_as = ghw_defender
		}
		scope:attacker.faith = {
			save_scope_as = the_faith
		}
		scope:attacker.faith.great_holy_war = {
			save_scope_as = the_ghw
		}

		scope:defender = {	#Achievement setup.
			every_character_war = {
				limit = {
					is_attacker = scope:attacker
					is_defender = scope:defender
				}
				every_war_defender = {
					add_achievement_flag = { FLAG = achievement_for_the_faith_flag }
				}
			}
		}

		every_player = { #Fluff for players
			limit = {
				OR = {
					this = scope:attacker
					this = scope:defender
					any_war_enemy = { this = scope:attacker }
					any_war_enemy = { this = scope:defender }
					is_within_diplo_range = { CHARACTER = scope:defender }
				}
			}
			trigger_event = great_holy_war.0053 #Differentiate depending on faith (compared to scope:attacker)
		}

		# Truce
		add_truce_attacker_defeat_effect = yes

		scope:attacker = {
			faith = {
				change_fervor = -15
			}
		}
		#Set 30 years cooldown.
		hidden_effect = {
			scope:attacker = {
				faith = {
					set_variable = {
						name = variable_ghw_cooldown
						value = yes
						years = 30
					}
				}
			}
		}
	}

	on_invalidated_desc = msg_invalidate_war_title

	max_ai_diplo_distance_to_title = 750
	min_ai_score = 100
}