﻿#CHANGES: GHWs now require a minimum number of counties held within the kingdom, Crusader Kingdoms now pacify locals to avoid immediate peasant revolts, Crusade cooldown is longer (but can be skipped at max Fervor). Defenders now gain more war score from battles and more ticking warscore (since imprisonment and capital occupation don't count for GHWs, and counter-occupation is usually unrealistic, but they need SOME way to eventually win the war).

undirected_great_holy_war = {
	group = religious_script_only

	war_name = "GREAT_HOLY_WAR_WAR_NAME"

	target_titles = all
	target_title_tier = all
	target_de_jure_regions_above = yes
	use_de_jure_wargoal_only = yes
	check_all_defenders_for_ticking_war_score = yes # All defenders land in the Crusade Kingdom should count
	defender_ticking_warscore = 0.03
	attacker_ticking_warscore = 0.05
	defender_score_from_battles_scale = 100
	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

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

	white_peace_possible = no

	allowed_for_character = {
		always = yes
	}

	valid_to_start = {		
		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = { #Any independent 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				
				#CHANGE: check both that top liege is of appropriate faith AND that they hold enough counties in the target kingdom (6 seems like a good number, maybe more?)
				holder.top_liege = {
					scope:attacker.faith = {
						is_hostile_enough_for_holy_war_trigger = { FAITH = prev.faith }
					}
					any_realm_county = {
						count > 6
						target_is_de_jure_liege_or_above = scope:target
					}
				}
			}
		}
	}

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

		debug_log = undirected_ghw_debug_loc
	}

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

		debug_log = "Undirected Great Holy War invalidated"
	}

	on_victory_desc = {
		desc = great_holy_war_victory_desc
	}

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

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

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

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

		##If a valid usurpable capital duchy exists, grab that too.
		#scope:attacker.faith.great_holy_war.ghw_target_title.title_capital_county.duchy = {
		#	save_temporary_scope_as = target_title_duchy_potential
		#	if = {
		#		limit = {
		#			OR = {
		#				is_title_created = no
		#				scope:attacker.faith.great_holy_war = {
		#					OR = {
		#						any_pledged_defender = { this = scope:target_title_duchy_potential.holder }
		#						any_pledged_defender = {
		#							any_vassal_or_below = { this = scope:target_title_duchy_potential.holder }
		#						}
		#					}
		#				}
		#			}
		#		}
		#		
		#		change_title_holder = {
		#			holder = scope:title_recipient
		#			change = scope:change
		#		}
		#	}
		#}

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

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

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

		scope:attacker.faith = {
			change_fervor = -30
		}
		scope:defender.faith = {
			change_fervor = 25
		}

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

	on_white_peace_desc = {
		desc = WHITE_PEACE_DISABLED
	}

	on_defeat_desc = {
		desc = great_holy_war_defeat_desc
	}

	on_defeat = {
		scope:attacker.faith.great_holy_war = {
			divide_war_chest = {
				defenders = yes
				gold = no
			}
		}
		
		scope:attacker.faith = {
			change_fervor = -25
		}
		scope:defender.faith = {
			change_fervor = 20
		}
		scope:attacker.faith.great_holy_war.ghw_target_title = { #Loc keys for localization fluff.
			save_scope_as = target_kingdom
		}
		scope:attacker = {
			save_scope_as = ghw_sponsor
		}
		scope:defender = {
			save_scope_as = ghw_defender
		}
		scope:attacker.faith = {
			save_scope_as = the_faith
		}
		scope:attacker.faith.great_holy_war = {
			save_scope_as = the_ghw
		}

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

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

		# Truce
		add_truce_attacker_defeat_effect = yes

		#CHANGE: Set 25 years cooldown (instead of 5)
		hidden_effect = {
			scope:attacker.faith = {
				set_variable = {
					name = variable_ghw_cooldown
					value = yes
					years = 25
				}
			}
			if = { #Clear pledged gold flag.
				limit = {
					any_player = {
						has_character_flag = ghw_pledged_gold
					}
				}
				every_player = {
					limit = {
						has_character_flag = ghw_pledged_gold
					}
					remove_character_flag = ghw_pledged_gold
				}
			}
		}
		debug_log = undirected_ghw_defeat_debug_message
	}
}

directed_great_holy_war = {
	group = religious

	war_name = "GREAT_HOLY_WAR_WAR_NAME"
	cb_name = "GREAT_HOLY_WAR_WAR_CB_NAME"

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

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

	occupation_participation_mult = 0.5
	siege_participation_mult = 1
	battle_participation_mult = 7.5

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

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

	allowed_against_character = {
		scope:attacker.faith = {
			is_hostile_enough_for_holy_war_trigger = { FAITH = scope:defender.faith }
		}
		scope:defender = {
			any_realm_county = {
				count > 6
				target_is_de_jure_liege_or_above = scope:target
			}
		}
	}

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

		scope:target = {
			tier = tier_kingdom
			any_in_de_jure_hierarchy = { #Any ruler in the dejure of the targeted Kingdom belongs to a Faith that the Caliph's Faith is hostile enough to Holy War against.
				continue = { tier > tier_county }
				tier = tier_county
				#CHANGE: check both that top liege is of appropriate faith AND that they hold enough counties in the target kingdom (6 seems like a good number, maybe more?)
				holder.top_liege = {
					scope:attacker.faith = {
						is_hostile_enough_for_holy_war_trigger = { FAITH = prev.faith }
					}
					any_realm_county = {
						count > 6
						target_is_de_jure_liege_or_above = scope:target
					}
				}
			}
		}
	}

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

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

		debug_log = directed_ghw_debug_loc
	}

	on_victory_desc = {
		desc = great_holy_war_directed_victory_desc
	}

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

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

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

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

		every_in_list = {
			list = target_titles
			save_scope_as = target
		}

		scope:attacker.faith.great_holy_war = {
			show_as_tooltip = { #Otherwise hand out titles normally.
				scope:target = {
					change_title_holder = {
						holder = scope:title_recipient
						change = scope:change
					}
				}
				do_ghw_title_handout = scope:change
			}
		}
		# Create a dynamic title if the kingdom itself can't be created/taken
		hidden_effect = {
			if = {
				limit = { #In the event that, for ex. k_jerusalem exists and is held by a foreign ruler who is not involved in the war in any way.
					exists = scope:target
					exists = scope:target.holder
					scope:target.holder = {
						is_alive = yes
						faith = scope:attacker.faith
					}
				}
				random_list = {
					50 = {
						create_dynamic_title = {
							tier = kingdom
							name = CRUSADER_STATE_CRUSADER_TITLE_NAME
						}
					}
					50 = {
						create_dynamic_title = {
							tier = kingdom
							name = CRUSADER_STATE_FAITH_TITLE_NAME
						}
					}
				}
				scope:attacker.faith.great_holy_war = {
					scope:new_title = {
						change_title_holder = {
							holder = scope:title_recipient
							change = scope:change
						}
					}
					do_ghw_title_handout = scope:change
				}
				scope:new_title = { generate_coa = yes }
			}
			else = { #Otherwise hand out titles normally.
				scope:attacker.faith.great_holy_war = {
					scope:target = {
						change_title_holder = {
							holder = scope:title_recipient
							change = scope:change
						}
					}
					do_ghw_title_handout = scope:change
				}
			}
			scope:title_recipient = {
				every_realm_province = {
					refill_levy = yes
					refill_garrison = yes
				}
				every_realm_county = { #CHANGE: locals should chill out for a bit - consider tweaking this amount
					add_county_modifier = {
						modifier = county_increased_opinion_modifier
						years = 20
					}
				}
			}
		}
		#CHANGE: Set 25 years cooldown (instead of 5)
		hidden_effect = {
			scope:attacker = {
				faith = {
					set_variable = {
						name = variable_ghw_cooldown
						value = yes
						years = 25
					}
				}
			}
			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 }
				}
			}
		}

		scope:attacker.faith = {
			change_fervor = -30
		}
		scope:defender.faith = {
			change_fervor = 25
		}

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

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

	on_white_peace_desc = {
		desc = WHITE_PEACE_DISABLED
	}

	on_defeat_desc = {
		desc = great_holy_war_defeat_desc
	}

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

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

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

		# Truce
		add_truce_attacker_defeat_effect = yes

		scope:attacker.faith = {			
			change_fervor = -25			
		}
		scope:defender.faith = {
			change_fervor = 20
		}
		#CHANGE: Set 25 years cooldown (instead of 5)
		hidden_effect = {
			scope:attacker = {
				faith = {
					set_variable = {
						name = variable_ghw_cooldown
						value = yes
						years = 25
					}
				}
			}
		}
	}

	on_invalidated_desc = msg_invalidate_war_title

	max_ai_diplo_distance_to_title = 750
	min_ai_score = 100
}