minor_religious_war = {
	icon = 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 }
	
	attacker_score_from_occupation_scale = 150
	attacker_score_from_battles_scale = 150
	defender_score_from_battles_scale = 150

	# Already defined in 00_casus_belli_groups.txt
	allowed_for_character = {
		scope:attacker.faith = {
			NOT = { has_doctrine_parameter = unreformed }
			NOT = { has_doctrine_parameter = holy_wars_forbidden }
		}
		NOR = {
			government_has_flag = government_is_landless_adventurer
			government_has_flag = government_is_nomadic
			government_has_flag = government_is_celestial
		}
	}

	allowed_for_character_display_regardless = {
		trigger_if = {
			limit = {
				highest_held_title_tier >= tier_kingdom
			}
			NOT = { piety_level = -1 }
		}
		trigger_else = {
			trigger_if = {
				limit = {
					is_alive = yes
					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
							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 >= 0
				}
			}
			trigger_else = {
				trigger_if = {
					limit = {
						OR = {
							culture = {
								has_cultural_parameter = cb_lower_piety_level_requirement
							}
							# Iberian Struggle Hostility Reward
							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
				}
			}
		}
		tgp_japan_offensive_wars_ban_trigger = yes
	}

	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
			is_landless_type_title = no
		}
		# Filter for struggles
		trigger_if = {
			limit = {
				scope:defender = {
					any_character_struggle = {
						involvement = involved
						is_struggle_type = iberian_struggle
					}
				}
			}
			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 = {
						top_liege != this
						vassal_contract_has_flag = vassal_contract_cheaper_religious_war
					}
				}
				add = 1
			}
		}

		# As a feudal gov, do not attack owner of tribal holdings  if you have a tribal holding to convert already
		add = {
			every_in_list = {
				list = target_titles
				if = {
					limit = { feudal_clan_tribal_conquest_constraints = yes }
					add = -1000
				}
			}
		}
		
		# 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
			}
		}
		# HOUSE RELATIONS
		add = house_relation_ai_score_value
		# AI in struggles act insularly for wars that don't have a familial or legal basis.
		multiply = struggle_wars_prioritise_struggle_targets_value
		# Tell the Romans to stop going adventuring into the steppe for no gods-damned reason.
		multiply = byzantium_conquests_ai_score_value

		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}

	cost = {
		piety = {
			add = {
				value = 100
				desc = CB_BASE_COST
			}
			multiply = common_cb_piety_cost_multiplier
			multiply = holy_war_cb_piety_cost_multiplier
			if = {
				limit = {
					scope:attacker = {
						any_active_accolade = {
							accolade_parameter = acclaimed_knight_piety_from_battle
						}
					}
				}
				multiply = accolade_piety_reduction_value
				min = 10
			}
			if = {
				limit = {
					scope:attacker = {
						any_active_accolade = {
							accolade_parameter = acclaimed_knight_piety_from_battle_high
						}
					}
				}
				multiply = accolade_piety_reduction_value_high
				min = 10
			}
		}
	}

	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 }
		
		# Legitimacy
		add_legitimacy_attacker_victory_effect = yes
		
		#EP2 accolade glory gain from winning against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_low_effect = yes }

		create_title_and_vassal_change = {
			type = conquest_holy_war
			save_scope_as = change
			add_claim_on_loss = yes
		}

		# EP3 Laamp possibility warning
		scope:defender = { ep3_war_loss_adventurer_tt_effect = 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
		}

		scope:attacker.faith = {
			change_fervor = {
				value = -1
				desc = fervor_loss_religious_war
			}
		}
		scope:defender.faith = {
			change_fervor = {
				value = 3
				desc = fervor_gain_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 }

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_victory_effects = 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
			}
		}

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
	}

	on_defeat_desc = {
		desc = religious_war_defeat_desc
	}

	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_defeat_effect = yes

		scope:defender = { 
			mandala_peacemaker_perk_serenity_effect = yes
			#EP2 accolade glory gain for winning against higher ranked enemy
			accolade_defender_war_end_glory_gain_high_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
		}

		# 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

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_defeat_effects = 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_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150

	max_ai_diplo_distance_to_title = 500
}

religious_war = {
	icon = 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 }
	
	attacker_score_from_occupation_scale = 150
	attacker_score_from_battles_scale = 150
	defender_score_from_battles_scale = 150

	# Additional requirements to the defines in 00_casus_belli_groups.txt
	allowed_for_character = {
		scope:attacker.faith = {
			NOT = { has_doctrine_parameter = unreformed }
			NOT = { has_doctrine_parameter = holy_wars_forbidden }
		}
		NOR = { 
			government_has_flag = government_is_nomadic
			government_has_flag = government_is_celestial
		}
	}

	allowed_for_character_display_regardless = {
		trigger_if = {
			limit = {
				highest_held_title_tier >= tier_kingdom
			}
			NOT = { piety_level = -1 }
		}
		trigger_else = {
			trigger_if = {
				limit = {
					is_alive = yes
					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
							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
							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
				}
			}
		}
		tgp_japan_offensive_wars_ban_trigger = yes
	}

	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
			is_landless_type_title = no
		}
		scope:target = {
			trigger_if = {
				limit = {
					scope:attacker = {
						is_ai = yes
					}
				}
				any_in_de_jure_hierarchy = {
					continue = { tier > tier_county }
					count >= 2
					tier = tier_county
					holder ?= {
						OR = {
							this = scope:defender
							is_vassal_or_below_of = scope:defender
						}
					}
				}
			}
		}
		# Filter for struggles
		trigger_if = {
			limit = {
				scope:defender = {
					any_character_struggle = {
						involvement = involved
						is_struggle_type = iberian_struggle
					}
				}
			}
			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
				}
			}
			if = {
				limit = {
					scope:attacker = {
						any_active_accolade = {
							accolade_parameter = acclaimed_knight_piety_from_battle
						}
					}
				}
				multiply = accolade_piety_reduction_value
				min = 25
			}
			if = {
				limit = {
					scope:attacker = {
						any_active_accolade = {
							accolade_parameter = acclaimed_knight_piety_from_battle_high
						}
					}
				}
				multiply = accolade_piety_reduction_value_high
				min = 25
			}
		}
	}

	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 }
		
		# Legitimacy
		add_legitimacy_attacker_victory_effect = yes

		#EP2 accolade glory gain for winning against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_med_effect = yes }

		create_title_and_vassal_change = {
			type = conquest_holy_war
			save_scope_as = change
			add_claim_on_loss = yes
		}

		# EP3 Laamp possibility warning
		scope:defender = { ep3_war_loss_adventurer_tt_effect = 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
		}

		scope:attacker.faith = {
			change_fervor = {
				value = -2
				desc = fervor_loss_religious_war
			}
		}
		scope:defender.faith = {
			change_fervor = {
				value = 4
				desc = fervor_gain_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 }

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_victory_effects = yes
	}

	on_white_peace_desc = {
		desc = religious_war_white_peace_desc
	}

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

		#EP2 accolade glory gain for sort of winning
		scope:defender = { accolade_defender_war_end_glory_gain_low_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
			}
		}

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
	}

	on_defeat_desc = {
		desc = religious_war_defeat_desc
	}

	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_defeat_effect = yes

		scope:defender = { 
			mandala_peacemaker_perk_serenity_effect = yes
			#EP2 accolade glory gain for winning against higher ranked enemy
			accolade_defender_war_end_glory_gain_high_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
		}

		# 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

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_defeat_effects = 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_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150

	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 = {
						top_liege != this
						vassal_contract_has_flag = vassal_contract_cheaper_religious_war
					}
				}
				add = 1
			}
		}

		# As a feudal gov, do not attack owner of tribal holdings  if you have a tribal holding to convert already
		add = {
			every_in_list = {
				list = target_titles
				if = {
					limit = { feudal_clan_tribal_conquest_constraints = yes }
					add = -1000
				}
			}
		}
		
		# 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
			}
		}
		# HOUSE RELATIONS
		add = house_relation_ai_score_value
		# AI in struggles act insularly for wars that don't have a familial or legal basis.
		multiply = struggle_wars_prioritise_struggle_targets_value
		# Tell the Romans to stop going adventuring into the steppe for no gods-damned reason.
		multiply = byzantium_conquests_ai_score_value
		
		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}
}

major_religious_war = {
	icon = 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 }
		}
		NOR = { 
			government_has_flag = government_is_nomadic
			government_has_flag = government_is_celestial
		}
	}

	allowed_for_character_display_regardless = {
		trigger_if = {
			limit = {
				is_alive = yes
				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 = {
							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
			}
		}
		trigger_else = {
			trigger_if = {
				limit = {
					OR = {
						culture = {
							has_cultural_parameter = cb_lower_piety_level_requirement
						}
						# Iberian Struggle Hostility Reward
						AND = {
							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_if = {
			limit = {
				culture = {
					NOT = {
						has_cultural_parameter = no_limit_to_kingdom_level_holy_wars
					}
				}
			}
			custom_description = {
				text = declared_major_religious_war_flag_tt
				OR = {
					has_character_flag = bonus_major_holy_war
					NOT = { has_character_flag = declared_major_religious_war_flag }
				}
			}
		}
		tgp_japan_offensive_wars_ban_trigger = yes
	}

	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
			is_landless_type_title = no
		}
		# Filter for struggles
		trigger_if = {
			limit = {
				scope:defender = {
					any_character_struggle = {
						involvement = involved
						is_struggle_type = iberian_struggle
					}
				}
			}
			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

		scope:target = {
			trigger_if = {
				limit = {
					scope:attacker = {
						is_ai = yes
					}
				}
				any_in_de_jure_hierarchy = {
					continue = { tier > tier_county }
					count >= 5
					tier = tier_county
					holder ?= {
						OR = {
							this = scope:defender
							is_vassal_or_below_of = scope:defender
						}
					}
				}
			}
		}
	}

	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 = {
		scope:attacker = { remove_character_flag = declared_major_religious_war_flag }
	}

	cost = {
		piety = {
			add = {
				value = 750
				desc = CB_BASE_COST
			}
			multiply = common_cb_piety_cost_multiplier
			multiply = holy_war_cb_piety_cost_multiplier
			if = {
				limit = {
					scope:attacker = {
						any_active_accolade = {
							accolade_parameter = acclaimed_knight_piety_from_battle
						}
					}
				}
				multiply = accolade_piety_reduction_value
				min = 90
			}
			if = {
				limit = {
					scope:attacker = {
						any_active_accolade = {
							accolade_parameter = acclaimed_knight_piety_from_battle_high
						}
					}
				}
				multiply = accolade_piety_reduction_value_high
				min = 90
			}
		}
	}

	on_declaration = {
		on_declared_war = yes
		scope:attacker = {
			if = {
				limit = {
					has_character_flag = bonus_major_holy_war
				}
				remove_character_flag = bonus_major_holy_war
			}
			else = {
				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
					OR = {
						AND = {
							NOT = { has_character_flag = bonus_major_holy_war }
							NOT = { has_character_flag = declared_major_religious_war_flag }
						}
						AND = {
							has_character_flag = bonus_major_holy_war
							has_character_flag = declared_major_religious_war_flag
						}
					}
				}
			}
			desc = major_religious_war_warning_desc
		}
	}

	on_victory = {
		scope:attacker = {
			if = {
				limit = {
					has_variable = k_holy_war_crusader
				}
				add_trait = faith_warrior
			}
		}
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_victory_effect = yes

		#EP2 accolade glory gain for winning against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_high_effect = yes }

		create_title_and_vassal_change = {
			type = conquest_holy_war
			save_scope_as = change
			add_claim_on_loss = yes
		}

		# EP3 Laamp possibility warning
		scope:defender = { ep3_war_loss_adventurer_tt_effect = 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
		}

		scope:attacker.faith = {
			change_fervor = {
				value = -5
				desc = fervor_loss_religious_war
			}
		}
		scope:defender.faith = {
			change_fervor = {
				value = 10
				desc = fervor_gain_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 }

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_victory_effects = yes
	}

	on_white_peace_desc = {
		desc = religious_war_white_peace_desc
	}

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

		#EP2 accolade glory gain for doing ok against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_low_effect = yes }
		scope:defender = { accolade_defender_war_end_glory_gain_med_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

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
	}

	on_defeat_desc = {
		desc = religious_war_defeat_desc
	}

	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_defeat_effect = yes

		scope:defender = { 
			mandala_peacemaker_perk_serenity_effect = yes
			#EP2 accolade glory gain for winning against higher ranked enemy
			accolade_defender_war_end_glory_gain_high_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
				}
			}
		}

		# Truce
		add_truce_attacker_defeat_effect = yes

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

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_defeat_effects = 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_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150

	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 = {
						top_liege != this
						vassal_contract_has_flag = vassal_contract_cheaper_religious_war
					}
				}
				add = 1
			}
		}

		# As a feudal gov, do not attack owner of tribal holdings  if you have a tribal holding to convert already
		add = {
			every_in_list = {
				list = target_titles
				if = {
					limit = { feudal_clan_tribal_conquest_constraints = yes }
					add = -1000
				}
			}
		}
		
		# 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
			}
		}
		# HOUSE RELATIONS
		add = house_relation_ai_score_value
		# AI in struggles act insularly for wars that don't have a familial or legal basis.
		multiply = struggle_wars_prioritise_struggle_targets_value
		# Tell the Romans to stop going adventuring into the steppe for no gods-damned reason.
		multiply = byzantium_conquests_ai_score_value
		multiply = muslim_invasions_prefer_nonmuslims_ai_score_value
		
		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}
}

excommunication_war = {
	icon = excommunication
	group = religious
	ai_only_against_neighbors = yes
	allow_hostages = no

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

	allowed_against_character = {
		scope:defender = {
			#Defender must be an excommunicated member of our faith.
			has_trait = excommunicated
			top_liege = this
			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 }
		
		# Legitimacy
		add_legitimacy_attacker_victory_effect = yes

		#EP2 Accolade glory gain from winning against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_med_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
		}

		# EP3 Laamp possibility warning
		scope:defender = { ep3_war_loss_adventurer_tt_effect = yes }

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

		# Truce
		add_truce_attacker_victory_effect = yes

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_victory_effects = 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 }

		#EP2 Accolade glory gain for defender for being ok
		scope:defender = { accolade_defender_war_end_glory_gain_low_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
			}
		}

		# Truce
		add_truce_white_peace_effect = yes

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_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 }
		
		# Legitimacy
		add_legitimacy_attacker_defeat_effect = yes

		scope:defender = { 
			mandala_peacemaker_perk_serenity_effect = yes
			#EP2 accolade glory gain for winning against higher ranked enemy
			accolade_defender_war_end_glory_gain_high_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

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_defeat_effects = 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
	
	max_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150
	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 = {
						top_liege != this
						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
			}
		}

		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}
}

flowery_war_cb = {
	icon = ducal_conquest_cb
	group = religious_disorganised
	ai_only_against_neighbors = yes
	allow_hostages = no
	
	allowed_for_character = {
		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 }
		}
	}

	# Root is the title
	# scope:attacker is the attacker
	# scope:defender is the defender
	allowed_against_character = {
		scope:defender = {
			top_liege = this
			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
			}
			multiply = common_cb_piety_cost_multiplier
		}
	}

	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 }
		
		# Legitimacy
		add_legitimacy_attacker_victory_effect = yes

		#EP2 Accolade glory gain from winning against higher ranked enemy
		scope:attacker = { accolade_attacker_war_end_glory_gain_low_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
					}
				}
			}
		}

		# 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 = major_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		# Truce
		add_truce_attacker_victory_effect = yes

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_victory_effects = yes

	}

	on_white_peace_desc = {
		desc = flowery_war_cb_white_peace_desc
	}

	on_white_peace = {
		scope:attacker = { 
			show_pow_release_message_effect = yes
			stress_impact = {
				ambitious = medium_stress_impact_gain
				arrogant = medium_stress_impact_gain
			}
		}
		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
				}
			}
		}



		# 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 = major_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

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

		# Truce
		add_truce_attacker_victory_effect = yes

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes
	}

	on_defeat_desc = {
		desc = flowery_war_cb_defeat_desc
	}

	on_defeat = {
		scope:attacker = { show_pow_release_message_effect = yes }
		
		# Legitimacy
		add_legitimacy_attacker_defeat_effect = yes

		scope:defender = { 
			mandala_peacemaker_perk_serenity_effect = yes
			#EP2 accolade glory gain for winning against higher ranked enemy
			accolade_defender_war_end_glory_gain_high_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
					}
				}
			}
		}

		# 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 = major_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		# Truce
		add_truce_attacker_defeat_effect = yes

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

		# EP3: note gold gained from military assistance/join war contracts and their war contribution threshold
		laamp_as_mercenary_payout_tooltip_effect = yes

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_defeat_effects = 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_defender_score_from_occupation = 150
	max_attacker_score_from_occupation = 150

	max_ai_diplo_distance_to_title = 500
	
	ai_score = {
		value = raid_for_captives_ai_score_value
	}

	ai_score_mult = {
		value = 1
		# Increase odds if the character is a vassal with Ghazi Contract
		add = {
			if = {
				limit = {
					scope:attacker = {
						top_liege != this
						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
			}
		}
		
		add = frankokratia_leader_protection_value #Set back to 0. Declaring war on the frankokratia leader can really ruin the whole thing
	}
}

undirected_great_holy_war = {
	group = religious_script_only
	interface_priority = 1000
	icon = undirected_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
	allow_hostages = 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
		on_declared_war = yes
		#Tell non diverting-players about the crazy 4th crusade plan
		if = {
			limit = {
				scope:attacker.faith = { this = faith:catholic }
				exists = global_var:byz_claimant_champion
				global_var:byz_claimant_champion = {
					is_alive = yes
					is_ruler = yes
					any_character_war = {
						using_cb = crusading_claim_cb
					}
				}
			}
			global_var:byz_claimant_champion = {
				random_character_war = {
					limit = {
						using_cb = crusading_claim_cb
					}
					save_scope_as = redirected_crusade
				}
			}
			every_player = {
			 	limit = {
			 		NOT = {
			 			is_attacker_in_war = scope:redirected_crusade
			 		}
			 		ep3_frankokratia_notification_recipient_trigger = yes
			 	}
			 	trigger_event = ep3_frankokratia_events.0031
			}
		}
		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
		}
		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 }
		
		# Legitimacy
		add_legitimacy_attacker_victory_effect = yes

		#EP2 Accolade glory gain to the holy knights of the faith head
		scope:attacker = { accolade_attacker_war_end_glory_gain_low_effect = yes }
		
		scope:attacker.faith.great_holy_war = { # Make sure steppe crusades do not make silly cultures
			every_pledged_attacker = {
				limit = { exists = ghw_beneficiary }
				ghw_beneficiary = {
					add_character_flag = {
						flag = domicile_culture_and_faith_check
						days = 1
					}
					add_to_list = beneficiary_fixing_list
				}
			}
		}

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

		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.culture
					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 = {
						OR = {
							is_ruler = no
							government_has_flag = government_is_landless_adventurer
						}
					}
				}
				scope:title_recipient = {
					add_character_flag = {
						flag = ghw_recipient_will_be_crusader_king
					}
					# Laamps don't get to keep the trait if they pass on their chosen kingdom.
					if = {
						limit = { government_has_flag = government_is_landless_adventurer }
						add_character_flag = laamp_is_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 = {
						limit = {
							has_government = herder_government
						}
						every_held_title = {
							limit = {
								tier = tier_county
								title_province = {
									has_holding_type = herder_holding
								}
							}
							title_province = {
								set_holding_type = nomad_holding
							}
						}
						change_government = nomad_government
						domicile = {
							set_domicile_culture = prev.capital_county.culture
							set_domicile_faith = prev.capital_county.faith
						}
					}
				}
			}
			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
					# Set the appropriate Succession Law
					if = {
						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
					# Plus if we're gallowsbait, remove most/all of it.
					absolve_crusaders_of_gallowsbait_effect = yes
					hidden_effect = { # Because the adventurer government override is active you may end up in awkward situations unless we set your government here
						if = {
							limit = {
								has_government = clan_government
								NOT = { faith.religion = religion:islam_religion }
							}
							change_government = feudal_government
						}
					}
				}
			}
			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 }
					# Plus if we're gallowsbait, remove most/all of it — provided we turned up.
					if = {
						limit = { has_trait = faith_warrior }
						absolve_crusaders_of_gallowsbait_effect = yes
					}
				}
				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 }
					}
				}
			}
			every_in_list = {
				list = beneficiary_fixing_list
				if = {
					limit = {
						has_government = herder_government
					}
					every_held_title = {
						limit = {
							tier = tier_county
							title_province = {
								has_holding_type = herder_holding
							}
						}
						title_province = {
							set_holding_type = nomad_holding
						}
					}
					change_government = nomad_government
					domicile = {
						set_domicile_culture = prev.capital_county.culture
						set_domicile_faith = prev.capital_county.faith
					}
				}
			}
		}
		
		#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
				}
			}
		}

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

			save_scope_as = war_goal_title
		}

		scope:attacker.faith = {
			change_fervor = {
				value = -25
				desc = fervor_loss_great_holy_war
			}
		}
		scope:defender.faith = {
			change_fervor = {
				value = 30
				desc = fervor_gain_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 }

		scope:title_recipient = {
			custom_tooltip = legend_seed_ghw.tt
		}

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_victory_effects = 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 }
		
		# Legitimacy
		add_legitimacy_attacker_defeat_effect = yes

		scope:defender = { 
			mandala_peacemaker_perk_serenity_effect = yes
			#EP2 accolade glory gain for winning against higher ranked enemy
			accolade_defender_war_end_glory_gain_high_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
		}

		scope:attacker = {
			faith = {
				change_fervor = {
					value = -15
					desc = fervor_loss_lost_great_holy_war
				}
			}
		}

		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 }
					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 = {
			if = {
				limit = { exists = scope:title_recipient }
				scope:title_recipient = {
					if = {
						limit = {
							has_government = herder_government
						}
						every_held_title = {
							limit = {
								tier = tier_county
								title_province = {
									has_holding_type = herder_holding
								}
							}
							title_province = {
								set_holding_type = nomad_holding
							}
						}
						change_government = nomad_government
						domicile = {
							set_domicile_culture = prev.capital_county.culture
							set_domicile_faith = prev.capital_county.faith
						}
					}
				}
			}
			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
		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_defeat_effects = yes
	}
}

directed_great_holy_war = {
	group = religious
	icon = directed_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
	allow_hostages = no

	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
					}
				}
			}
			scope:attacker.religious_head = {
				piety_level >= 2
			}
			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 }}
		}
		tgp_japan_offensive_wars_ban_trigger = yes
	}

	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.religious_head = {
				add_piety_level = -1
			}
		}
		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_playable_character = yes
					highest_held_title_tier >= tier_county
					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_playable_character = 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 } }
					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
	}

	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 }
		
		# Legitimacy
		add_legitimacy_attacker_victory_effect = yes

		#EP2 Accolade glory gain to the holy warriors of the faith head
		scope:attacker = { accolade_attacker_war_end_glory_gain_low_effect = yes }
		
		scope:attacker.faith.great_holy_war = { # Make sure steppe crusades do not make silly cultures
			every_pledged_attacker = {
				limit = { exists = ghw_beneficiary }
				ghw_beneficiary = {
					add_character_flag = {
						flag = domicile_culture_and_faith_check
						days = 1
					}
					add_to_list = beneficiary_fixing_list
				}
			}
		}

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

		scope:attacker.faith = {
			change_fervor = {
				value = -25
				desc = fervor_loss_great_holy_war
			}
		}
		scope:defender.faith = {
			change_fervor = {
				value = 30
				desc = fervor_gain_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 }
					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 }

		scope:title_recipient = {
			custom_tooltip = legend_seed_ghw.tt
		}
		
		hidden_effect = {
			every_in_list = {
				list = beneficiary_fixing_list
				if = {
					limit = {
						has_government = herder_government
					}
					every_held_title = {
						limit = {
							tier = tier_county
							title_province = {
								has_holding_type = herder_holding
							}
						}
						title_province = {
							set_holding_type = nomad_holding
						}
					}
					change_government = nomad_government
					domicile = {
						set_domicile_culture = prev.capital_county.culture
						set_domicile_faith = prev.capital_county.faith
					}
				}
			}
		}

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_victory_effects = 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 }
		
		# Legitimacy
		add_legitimacy_attacker_defeat_effect = yes

		scope:defender = { 
			mandala_peacemaker_perk_serenity_effect = yes
			#EP2 accolade glory gain for winning against higher ranked enemy
			accolade_defender_war_end_glory_gain_high_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 }
					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 = {
					value = -15
					desc = fervor_loss_lost_great_holy_war
				}
			}
		}

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

		#Mandalas gain or lose piety/devotion depending on Decree
		mandala_war_defeat_effects = yes
	}

	max_ai_diplo_distance_to_title = 750
}
