﻿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_parameter = unreformed }
		}
	}

	allowed_for_character_display_regardless = {
		trigger_if = {
			limit = {
				has_variable_list = humsacd_hofs
				is_target_in_variable_list = {
					name = humsacd_hofs
					target = scope:attacker.faith
				}
			}
			trigger_if = {
				limit = {
					OR = {
						culture = {
							has_cultural_parameter = cb_lower_piety_level_requirement
						}
						# Iberian Struggle Hostility Reward
						AND = {
							exists = house
							house = {
								OR = {
									has_house_modifier = fp2_struggle_hostility_house_faith_modifier
									has_house_modifier = fp2_struggle_hostility_house_combined_modifier
								}
							}
						}
					}
				}
				piety_level >= -1
			}
			trigger_else = {
				piety_level >= 0
			}
		}
		trigger_else = {
			trigger_if = {
				limit = {
					OR = {
						culture = {
							has_cultural_parameter = cb_lower_piety_level_requirement
						}
						# Iberian Struggle Hostility Reward
						AND = {
							exists = house
							house = {
								OR = {
									has_house_modifier = fp2_struggle_hostility_house_faith_modifier
									has_house_modifier = fp2_struggle_hostility_house_combined_modifier
								}
							}
						}
					}
				}
				piety_level >= 0
			}
			trigger_else = {
				piety_level >= 1
			}
		}
	}

	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
		}
		# Filter for struggles
		trigger_if = {
			limit = {
				scope:defender = {
					any_character_struggle = {
						involvement = involved
					}
				}
			}
			are_holy_wars_disabled_in_struggle_phase_trigger = yes
		}
		are_holy_wars_disabled_by_struggle_conciliation_trigger = no # Disabled against other involved faiths in FP2 Conciliation ending
	}

	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 = {
		
	}
	
	ai_score_mult = {
		value = 1
		# Increase odds if the character is a vassal with Ghazi Contract
		add = {
			if = {
				limit = {
					scope:attacker = {
						is_independent_ruler = no
						vassal_contract_has_flag = vassal_contract_cheaper_religious_war
					}
				}
				add = 1
			}
		}
		# Holy War are limited to connected neighbour to limit big empire expansions over weak realms.
		add = {
			if = {
				limit = {  religious_war_vassals_constraints = yes }
				add = -1000
			}
		}

		# AI in struggles act insularly for wars that don't have a familial or legal basis.
		multiply = struggle_wars_prioritise_struggle_targets_value
	}

	cost = {
		piety = {
			add = {
				value = 100
				desc = CB_BASE_COST
			}
			multiply = common_cb_piety_cost_multiplier
			multiply = holy_war_cb_piety_cost_multiplier
		}
	}

	on_declaration = {
		on_declared_war = 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

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:defender
			FAME_BASE = religious_cb_ally_prestige_county
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

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

		ordered_in_list = {
			list = target_titles
			order_by = tier
			max = 1

			save_scope_as = war_goal_title
		}
		#Modded Inverted
		scope:attacker.faith = {
			change_fervor = {
				value = 3
				desc = fervor_gain_attacker_religious_war
			}
		}
		scope:defender.faith = {
			change_fervor = {
				value = -1
				desc = fervor_loss_defender_religious_war
			}
		}

		# Truce
		add_truce_attacker_victory_effect = yes

		# FP1: note the victory for future memorialisation via stele (if applicable).
		scope:attacker = { fp1_remember_recent_conquest_victory_effect = yes }
	}

	on_white_peace_desc = {
		desc = religious_war_white_peace_desc
	}

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

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:defender # Not important as the scales are identical
			FAME_BASE = religious_cb_ally_prestige_county
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		# Truce
		add_truce_white_peace_effect = yes

		scope:attacker = {
			add_piety = religious_cb_piety_white_peace
			stress_impact = {
				ambitious = medium_stress_impact_gain
				arrogant = medium_stress_impact_gain
			}
		}

		scope:defender = {
			stress_impact = {
				arrogant = medium_stress_impact_gain
			}
		}
		#Modded New
		scope:attacker.faith = {
			change_fervor = {
				value = -1
				desc = fervor_loss_attacker_religious_war
			}
		}
		scope:defender.faith = {
			change_fervor = {
				value = 1
				desc = fervor_gain_defender_religious_war
			}
		}		
	}

	on_defeat_desc = {
		desc = religious_war_defeat_desc
	}

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

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

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:defender
			FAME_BASE = religious_cb_ally_prestige_county
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}
		#Modded New
		scope:attacker.faith = {
			change_fervor = {
				value = -1
				desc = fervor_loss_attacker_religious_war
			}
		}
		scope:defender.faith = {
			change_fervor = {
				value = 3
				desc = fervor_gain_defender_religious_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 = {
				if = {
					limit = {
						monthly_character_income > 0
					}
					pay_short_term_income = {
						years = 2
						target = scope:defender
					}
				}
				else = {
					pay_short_term_gold = {
						target = scope:defender
						gold = medium_gold_value
					}
				}
			}
		}

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

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_parameter = unreformed }
		}
	}

	allowed_for_character_display_regardless = {
		trigger_if = {
			limit = {
				has_variable_list = humsacd_hofs
				is_target_in_variable_list = {
					name = humsacd_hofs
					target = scope:attacker.faith
				}
			}
			trigger_if = {
				limit = {
					OR = {
						culture = {
							has_cultural_parameter = cb_lower_piety_level_requirement
						}
						# Iberian Struggle Hostility Reward
						AND = {
							exists = house
							house = {
								OR = {
									has_house_modifier = fp2_struggle_hostility_house_faith_modifier
									has_house_modifier = fp2_struggle_hostility_house_combined_modifier
								}
							}
						}
					}
				}
				piety_level >= 0
			}
			trigger_else = {
				piety_level >= 1
			}
		}
		trigger_else = {
			trigger_if = {
				limit = {
					OR = {
						culture = {
							has_cultural_parameter = cb_lower_piety_level_requirement
						}
						# Iberian Struggle Hostility Reward
						AND = {
							exists = house
							house = {
								OR = {
									has_house_modifier = fp2_struggle_hostility_house_faith_modifier
									has_house_modifier = fp2_struggle_hostility_house_combined_modifier
								}
							}
						}
					}
				}
				piety_level >= 1
			}
			trigger_else = {
				piety_level >= 2
			}
		}
	}

	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
		}
		# Filter for struggles
		trigger_if = {
			limit = {
				scope:defender = {
					any_character_struggle = {
						involvement = involved
					}
				}
			}
			are_holy_wars_disabled_in_struggle_phase_trigger = yes
		}
		are_holy_wars_disabled_by_struggle_conciliation_trigger = no # Disabled against other involved faiths in FP2 Conciliation ending
	}

	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
			}
			multiply = common_cb_piety_cost_multiplier
			multiply = holy_war_cb_piety_cost_multiplier
			# Half price for mean Iberians
			if = {
				limit = {
					scope:attacker.house = {
						OR = {
							has_house_modifier = fp2_struggle_hostility_house_faith_modifier
							has_house_modifier = fp2_struggle_hostility_house_combined_modifier
						}
					}
					any_in_global_list = {
						variable = fp2_struggle_ending_faith_list
						this = scope:attacker.faith
					}
					any_in_global_list = {
						variable = fp2_struggle_ending_faith_list
						this = scope:defender.faith
					}
				}
				divide = {
					value = 2
					desc = CB_COST_FP2_STRUGGLE_HOSTILITY_FAITH_DISCOUNT
				}
			}
			# Double price for failure Iberians
			if = {
				limit = {
					has_global_variable = fp2_struggle_compromise_ending
					any_in_global_list = {
						variable = fp2_struggle_ending_faith_list
						this = scope:attacker.faith
					}
					any_in_global_list = {
						variable = fp2_struggle_ending_faith_list
						this = scope:defender.faith
					}
				}
				multiply = {
					value = 2
					desc = CB_COST_FP2_STRUGGLE_COMPROMISE_MULTIPLIER
				}
			}
		}
	}

	on_declaration = {
		on_declared_war = 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

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker
			FAME_BASE = religious_cb_ally_prestige_duchy
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

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

		ordered_in_list = {
			list = target_titles
			order_by = tier
			max = 1

			save_scope_as = war_goal_title
		}

		#Modded Inverted
		scope:attacker.faith = {
			change_fervor = {
				value = 4
				desc = fervor_gain_attacker_religious_war
			}
		}
		scope:defender.faith = {
			change_fervor = {
				value = -2
				desc = fervor_loss_defender_religious_war
			}
		}

		# Truce
		add_truce_attacker_victory_effect = yes

		# FP1: note the victory for future memorialisation via stele (if applicable).
		scope:attacker = { fp1_remember_recent_conquest_victory_effect = yes }
	}

	on_white_peace_desc = {
		desc = religious_war_white_peace_desc
	}

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

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker # Not important as the scales are identical
			FAME_BASE = religious_cb_ally_prestige_duchy
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		# Truce
		add_truce_white_peace_effect = yes

		scope:attacker = {
			add_piety = religious_cb_piety_white_peace
			stress_impact = {
				ambitious = medium_stress_impact_gain
				arrogant = medium_stress_impact_gain
			}
		}

		scope:defender = {
			stress_impact = {
				arrogant = medium_stress_impact_gain
			}
		}
		
		#Modded New
		scope:attacker.faith = {
			change_fervor = {
				value = -2
				desc = fervor_loss_attacker_religious_war
			}
		}
		scope:defender.faith = {
			change_fervor = {
				value = 2
				desc = fervor_gain_defender_religious_war
			}
		}		
	}

	on_defeat_desc = {
		desc = religious_war_defeat_desc
	}

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

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:defender
			FAME_BASE = religious_cb_ally_prestige_duchy
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}
		#Modded New
		scope:attacker.faith = {
			change_fervor = {
				value = -2
				desc = fervor_loss_attacker_religious_war
			}
		}
		scope:defender.faith = {
			change_fervor = {
				value = 4
				desc = fervor_gain_defender_religious_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 = {
				if = {
					limit = {
						monthly_character_income > 0
					}
					pay_short_term_income = {
						years = 3
						target = scope:defender
					}
				}
				else = {
					pay_short_term_gold = {
						target = scope:defender
						gold = medium_gold_value
					}
				}
			}
		}

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

		# 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

	ai_score_mult = {
		value = 1
		# Increase odds if the character is a vassal with Ghazi Contract
		add = {
			if = {
				limit = {
					scope:attacker = {
						is_independent_ruler = no
						vassal_contract_has_flag = vassal_contract_cheaper_religious_war
					}
				}
				add = 1
			}
		}
		# Holy War are limited to connected neighbour to limit big empire expansions over weak realms.
		add = {
			if = {
				limit = {  religious_war_vassals_constraints = yes }
				add = -1000
			}
		}

		# AI in struggles act insularly for wars that don't have a familial or legal basis.
		multiply = struggle_wars_prioritise_struggle_targets_value
	}
}

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_parameter = unreformed }
		}
	}

	allowed_for_character_display_regardless = {
		trigger_if = {
			limit = {
				has_variable_list = humsacd_hofs
				is_target_in_variable_list = {
					name = humsacd_hofs
					target = scope:attacker.faith
				}
			}
			trigger_if = {
				limit = {
					OR = {
						culture = {
							has_cultural_parameter = cb_lower_piety_level_requirement
						}
						# Iberian Struggle Hostility Reward
						AND = {
							exists = house
							house = {
								OR = {
									has_house_modifier = fp2_struggle_hostility_house_faith_modifier
									has_house_modifier = fp2_struggle_hostility_house_combined_modifier
								}
							}
						}
					}
				}
				piety_level >= 2
			}
			trigger_else = {
				piety_level >= 3
			}
		}
		trigger_else = {
			trigger_if = {
				limit = {
					OR = {
						culture = {
							has_cultural_parameter = cb_lower_piety_level_requirement
						}
						# Iberian Struggle Hostility Reward
						AND = {
							exists = house
							house = {
								OR = {
									has_house_modifier = fp2_struggle_hostility_house_faith_modifier
									has_house_modifier = fp2_struggle_hostility_house_combined_modifier
								}
							}
						} 
					}
				}
				piety_level >= 3
			}
			trigger_else = {
				piety_level >= 4
			}
		}
		trigger_if = {
			limit = {
				culture = {
					NOT = {
						has_cultural_parameter = no_limit_to_kingdom_level_holy_wars
					}
				}
			}
			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
		}
		# Filter for struggles
		trigger_if = {
			limit = {
				scope:defender = {
					any_character_struggle = {
						involvement = involved
					}
				}
			}
			are_holy_wars_disabled_in_struggle_phase_trigger = yes
		}
		are_holy_wars_disabled_by_struggle_conciliation_trigger = no # Disabled against other involved faiths in FP2 Conciliation ending
	}

	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
			}
			multiply = common_cb_piety_cost_multiplier
			multiply = holy_war_cb_piety_cost_multiplier
		}
	}

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

	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

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker
			FAME_BASE = religious_cb_ally_prestige_kingdom
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

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

		ordered_in_list = {
			list = target_titles
			order_by = tier
			max = 1

			save_scope_as = war_goal_title
		}

		#Modded Inverted & double fervour loss for large faiths of losing defenders
		scope:attacker.faith = {
			change_fervor = {
				value = 10
				desc = fervor_gain_attacker_religious_war
			}
		}
		scope:defender.faith = {
			if = {
				limit = {
					num_county_followers > 150
				}				
				change_fervor = {
					value = -10
					desc = fervor_loss_defender_religious_war
				}
			}
			else = {
				change_fervor = {
					value = -5
					desc = fervor_loss_defender_religious_war
				}				
			}
		}

		# Truce
		add_truce_attacker_victory_effect = yes

		# FP1: note the victory for future memorialisation via stele (if applicable).
		scope:attacker = { fp1_remember_recent_conquest_victory_effect = yes }
	}

	on_white_peace_desc = {
		desc = religious_war_white_peace_desc
	}

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

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker # Not important as the scales are identical
			FAME_BASE = religious_cb_ally_prestige_kingdom
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		scope:attacker = {
			add_piety = religious_cb_piety_white_peace
			stress_impact = {
				ambitious = medium_stress_impact_gain
				arrogant = medium_stress_impact_gain
			}
		}

		scope:defender = {
			stress_impact = {
				arrogant = medium_stress_impact_gain
			}
		}

		# Truce
		add_truce_white_peace_effect = yes
		#Modded New
		scope:attacker.faith = {
			change_fervor = {
				value = -3
				desc = fervor_loss_attacker_religious_war
			}
		}
																 
		scope:defender.faith = {
			change_fervor = {
				value = 3
				desc = fervor_gain_defender_religious_war		
			}
		}			
	}

	on_defeat_desc = {
		desc = religious_war_defeat_desc
	}

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

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:defender
			FAME_BASE = religious_cb_ally_prestige_kingdom
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		# 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 = {
				if = {
					limit = {
						monthly_character_income > 0
					}
					pay_short_term_income = {
						years = 4
						target = scope:defender
					}
				}
				else = {
					pay_short_term_gold = {
						target = scope:defender
						gold = medium_gold_value
					}
				}
			}
		}

		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
				}
			}
		}
		#Modded Inverted & double fervour loss for large faiths of losing attackers
		scope:defender.faith = {
			change_fervor = {
				value = 10
				desc = fervor_gain_religious_war
			}
		}
																 
		scope:attacker.faith = {
			if = {
				limit = {
					num_county_followers > 150
				}				
				change_fervor = {
					value = -10
					desc = fervor_loss_attacker_religious_war
				}
			}
			else = {
				change_fervor = {
					value = -5
					desc = fervor_gain_defender_religious_war
				}				
			}
		}	

		# 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

	ai_score_mult = {
		value = 1
		# Increase odds if the character is a vassal with Ghazi Contract
		add = {
			if = {
				limit = {
					scope:attacker = {
						is_independent_ruler = no
						vassal_contract_has_flag = vassal_contract_cheaper_religious_war
					}
				}
				add = 1
			}
		}
		# Holy War are limited to connected neighbour to limit big empire expansions over weak realms.
		add = {
			if = {
				limit = {  religious_war_vassals_constraints = yes }
				add = -1000
			}
		}

		# AI in struggles act insularly for wars that don't have a familial or legal basis.
		multiply = struggle_wars_prioritise_struggle_targets_value
	}
}

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
			NOR = {
				liege = scope:defender
			#No pot calling the kettle black
				has_trait = excommunicated
			}

			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
		#Modded New
		scope:attacker.faith = {
			change_fervor = {
				value = 2
				desc = fervor_gain_attacker_religious_war
			}
		}		
	}

	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
			stress_impact = {
				ambitious = medium_stress_impact_gain
				arrogant = medium_stress_impact_gain
			}
		}

		scope:defender = {
			stress_impact = {
				arrogant = medium_stress_impact_gain
			}
		}
		#Modded New
		scope:attacker.faith = {
			change_fervor = {
				value = -1
				desc = fervor_loss_attacker_religious_war
			}
		}		

		# 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
			}
		}
		#Modded New
		scope:attacker.faith = {
			change_fervor = {
				value = -2
				desc = fervor_loss_attacker_religious_war
			}
		}
		# 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 = {

			#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

	ai_score_mult = {
		value = 1
		# Increase odds if the character is a vassal with Ghazi Contract
		add = {
			if = {
				limit = {
					scope:attacker = {
						is_independent_ruler = no
						vassal_contract_has_flag = vassal_contract_cheaper_religious_war
					}
				}
				add = 1
			}
		}
		# Holy War are limited to connected neighbour to limit big empire expansions over weak realms.
		add = {
			if = {
				limit = {  religious_war_vassals_constraints = yes }
				add = -1000
			}
		}
	}
}

undirected_great_holy_war = {
	group = religious_script_only
	interface_priority = 1000
	icon = great_holy_war

	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
	transfer_behavior = transfer

	white_peace_possible = no

	allowed_for_character = {
		always = yes
	}

	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.faith.great_holy_war = {
			divide_war_chest = {
				fraction = ghw_war_start_handout_percentage
			}
		}

		# First of the Crusader Kings achievement tracking.
		if = {
			limit = {
				scope:attacker.faith = faith:catholic
				NOT = { exists = global_var:had_first_catholic_crusade }
			}
			# Credit where chredit is due.
			scope:attacker.faith.great_holy_war = {
				every_pledged_attacker = {
					limit = { fp1_achievement_culture_norse_trigger = yes }
					add_achievement_flag_effect = { FLAG = first_of_the_crusader_kings_achievement_flag }
				}
			}
			# Mark the first crusade as having happened.
			set_global_variable = {
				name = had_first_catholic_crusade
				value = yes
			}
		}

		debug_log = undirected_ghw_debug_loc
	}

	should_invalidate = {
		scope:attacker.faith = scope:defender.faith # Primary defender has converted
	}

	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_effect = { FLAG = achievement_for_the_faith_flag }
				}
			}
		}

		scope:defender.faith = {		#needed for hold_court.6150
			set_variable = {
				name = 6150_lost_ghw_recently		
				years = 5
			}
		}

		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 }
						#Modded for compatibility with Better Crusade Notifications by shattered_logic
						is_ai = no
						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
						#Modded New County Pacification
						add_county_modifier = {
							modifier = county_increased_opinion_modifier_crusade
							years = 25
						}						
					}
					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
				}
				#Modded new GHW victor marker for Fervour Midpoint Rebalance mod mechanics
				set_variable = {
					name = variable_ghw_victor
					value = yes
					years = 10
				}				
			}
			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
				}
			}
		}

		ordered_in_list = {
			list = target_titles
			order_by = tier
			max = 1

			save_scope_as = war_goal_title
		}

		#Modded Inverted & New
		scope:attacker.faith = {
			change_fervor = {
				value = 30
				desc = fervor_gain_great_holy_war
			}
		}
		scope:defender.faith = {
			change_fervor = {
				value = -50
				desc = fervor_loss_great_holy_war
			}
		}

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

		# FP1: note the victory for future memorialisation via stele (if applicable).
		scope:attacker = { fp1_remember_recent_conquest_victory_effect = yes }
	}

	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.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
		}
		#Modded
		scope:attacker = {
			faith = {
				change_fervor = {
					value = -50
					desc = fervor_loss_lost_great_holy_war
				}
				set_variable = {			#needed for hold_court.6150
					name = 6150_lost_ghw_recently
					years = 5
				}
			}
		}
		#Modded New
		scope:defender.faith = {
			change_fervor = {
				value = 30
				desc = fervor_gain_great_holy_war
			}
			set_variable = {
				name = variable_ghw_victor
				value = yes
				years = 10
			}			
		}
		scope:defender = {	#Achievement setup.
			every_character_war = {
				limit = {
					is_attacker = scope:attacker
					is_defender = scope:defender
				}
				every_war_defender = {
					add_achievement_flag_effect = { 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 }
					#Modded for compatibility with Better Crusade Notifications by shattered_logic
					is_ai = no
					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
	icon = great_holy_war
	interface_priority = 1000

	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
	transfer_behavior = transfer

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

	allowed_for_character_display_regardless = {
		custom_description = {
			text = great_holy_war_cooldown
			NOT = {	scope:attacker.faith = { exists = var:variable_ghw_cooldown }}
		}
	}

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

	valid_to_start = {
		scope:attacker.faith = {
			NOR = {
				# No active GHW already.
				exists = great_holy_war 

				# If GHWs have just been unlocked, AI rulers will wait a little bit before declaring them.
				trigger_if = {
					limit = { scope:attacker = { is_ai = yes } }
					religion = { exists = var:variable_first_ghw_cooldown }
				}
			}
		}

		scope:target = {
			tier = tier_kingdom
			trigger_if = {
				limit = {
					scope:attacker = {
						is_ai = yes
					}
				}
				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 }
					count >= 5 # The AI doesn't Jihad for an area of less than 5 Counties, for reference, Jerusalem is 8 Counties
					tier = tier_county
					scope:attacker.faith = {
						is_hostile_enough_for_holy_war_trigger = { FAITH = prev.holder.top_liege.faith }
					}
				}
			}
			trigger_else = {
				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 }
					}
				}
			}
		}

		#The war seems to be launching without a valid defender sometimes, so we double-check that one is set here
		exists = scope:defender
	}

	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
					#Modded for compatibility with Better Crusade Notifications by shattered_logic
					is_ai = no
				}
			}
			trigger_event = great_holy_war.0071
		}
		on_declared_war = yes

		debug_log = directed_ghw_debug_loc
	}

	should_invalidate = {
		scope:attacker.faith = scope:defender.faith # Primary defender has converted
	}

	on_invalidated_desc = msg_invalidate_war_title

	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_effect = { 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 = {
			#Modded New County Pacificiation
				every_realm_county = { # Crusading troops seizing control
					change_county_control = 50
					add_county_modifier = {
						modifier = county_increased_opinion_modifier_crusade
						years = 25
					}
				}
				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
					}
					#Modded new GHW victor marker for Fervour Midpoint Rebalance mod mechanisms
					set_variable = {
						name = variable_ghw_victor
						value = yes
						years = 10
					}
				}
			}
			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 }
				}
			}
		}

		ordered_in_list = {
			list = target_titles
			order_by = tier
			max = 1

			save_scope_as = war_goal_title
		}

		#Modded Inverted
		scope:attacker.faith = {
			change_fervor = {
				value = 30
				desc = fervor_gain_great_holy_war
			}
		}
		scope:defender.faith = {
			change_fervor = {
				value = -50
				desc = fervor_loss_great_holy_war
			}
		}

		#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 }
					#Modded for compatibility with Better Crusade Notifications by shattered_logic
					is_ai = no
					is_within_diplo_range = { CHARACTER = scope:defender }
				}
			}
			trigger_event = great_holy_war.0052 #Differentiate depending on faith (compared to scope:attacker)
		}

		# FP1: note the victory for future memorialisation via stele (if applicable).
		scope:attacker = { fp1_remember_recent_conquest_victory_effect = yes }
	}

	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_effect = { 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 }
					#Modded for compatibility with Better Crusade Notifications by shattered_logic
					is_ai = no
					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

		#Modded Inverted & New
		scope:attacker = {
			faith = {
				change_fervor = {
					value = -50
					desc = fervor_loss_lost_great_holy_war
				}
			}
		}
		scope:defender.faith = {
			change_fervor = {
				value = 30
				desc = fervor_gain_great_holy_war
			}
			#Modded New GHW Victor Marker for Fervour Midpoint Rebalance mod mechanisms
			set_variable = {
				name = variable_ghw_victor
				value = yes
				years = 10
			}
		}

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