﻿namespace = great_holy_war

### Great Holy Wars Events
# by Flavio Verna
# updated by Ewan Cowhig Croft
# 0001 - A Great Holy War is declared.
# 0003 - Players informed of the start of the preparation phase of a GHW.
# 0004 - Sponsor selects a new target Kingdom.
# 0010 - A Faith unlocks GHWs for its Religion.
# 0015 - Excommunication is lifted for joining a GHW.
# 0016 - Sponsor admonishes ruler for unpledging from a GHW.
# 0017 - Confirm unpledging.
# 0018 - Confirm pledging.
# 0020 - AI rulers pledge to Undirected GHW.
# 0092 - AI rulers pledge to Directed GHW.
# 0090 - Holy Orders pledging to GHW.
# 0021 - AI rulers counterpledge to Undirected GHW.
# 0093 - AI rulers counterpledge to Directed GHW.
# 0091 - Holy Orders counterpledging to GHW.
# 0022, 0023 - The Pope calls out unpledged player.
# 0024 - Dead Recipient is replaced.
# 0025 - Converted Recipient is replaced.
# 0026, 0027 - A Beneficiary is selected for pledged AI rulers.
# 0028 - Resetting a Beneficiary upon death.
# 0029 - Resetting a Beneficiary upon conversion.
# 0030 - Resetting a Beneficiary due to illness, excommunication, inheritance or celibacy vow.
# 0040 - Player donates money to the GHW War Chest.
# 0050 - GHW preparation phase starts (defender).
# 0051 - GHW was declared.
# 0052 - GHW was won.
# 0053 - GHW was lost.
# 0055 - GHW failed to start.
# 0060 - Crusader trait gained.
# 0058 - Crusader trait lost.
# 0061 - Beneficiaries rewarded upon GHW victory.
# 0080 - GHW sponsor is replaced upon death.
# 0081, 0082 - Player redirected GHW during preparation phase.

# The Sponsor declares a Great Holy War
great_holy_war.0001 = {
	hidden = yes
	scope = faith
	
	trigger = {
		has_doctrine = doctrine_spiritual_head
		save_temporary_scope_as = the_faith
		exists = religious_head
		NOR = {
			exists = great_holy_war
			exists = var:variable_ghw_cooldown
			religion = { exists = var:variable_first_ghw_cooldown } # Do not event spam after Crusades have been just unlocked.
		}
		religious_head = { is_imprisoned = no }
		religion = { exists = var:variable_ghw_unlocked } #Activated by event for every religion.
		suitable_faith_for_undirected_ghw_trigger = yes #Right combinations of Doctrines to have GHWs. And a valid target exists
	}
	
	immediate = {
		save_scope_as = the_faith

		#Determine Sponsor.
		religious_head = { save_scope_as = ghw_sponsor }
		determine_ghw_target_effect = { FAITH = scope:the_faith }

		# Launch the preparation phase
		start_great_holy_war = {
			target_character = scope:target_character
			target_title = scope:target_kingdom
			delay = { 365 545 }
		}
		scope:target_character.faith = { save_scope_as = target_faith } #Used for counterpledges.

		great_holy_war = {
			set_war_declarer = scope:ghw_sponsor
			if = {
				limit = { is_directed_ghw = no }
				undirected_ghw_recipient_selection_effect = yes
			}
		}
		if = {
			limit = { scope:ghw_sponsor.gold > 0 }
			great_holy_war = {
				change_war_chest_gold = {
					value = scope:ghw_sponsor.gold
					divide = 2
					max = 5000
				}
			}
			scope:ghw_sponsor = {
				remove_short_term_gold = {
					value = gold
					divide = 2
					max = 5000
				}
			}
		}
		great_holy_war = {
			pledge_attacker = scope:ghw_sponsor
			set_variable = {
				name = redirect_cost
				value = 500
			}
		}
		#If no Claimant or Title Holder is available to be the Papal choice and the Crusade has not proceeded far enough to have a favorite beneficiary, select a fallback Recipient so that the CB's tooltip does not appear incomplete.
		if = { #Random Courtier in the Pope's court.
			limit = {
				NOT = { exists = scope:the_faith.great_holy_war.ghw_title_recipient }
				exists = scope:ghw_sponsor
				exists = scope:the_faith.great_holy_war
				scope:ghw_sponsor = {
					any_courtier = {
						is_adult = yes
						is_ai = yes
						NOR = {
							has_trait = excommunicated
							has_trait = incapable
							has_trait = devoted
						}
						faith = scope:the_faith
					}
				}
			}
			scope:ghw_sponsor = {
				random_courtier = {
					alternative_limit = {
						scope:the_faith = { has_doctrine = doctrine_gender_male_dominated }
						is_male = yes
						is_adult = yes
						is_ai = yes
						NOR = {
							has_trait = excommunicated
							has_trait = incapable
							has_trait = devoted
						}
						faith = scope:the_faith
					}
					alternative_limit = {
						scope:the_faith = { has_doctrine = doctrine_gender_female_dominated }
						is_female = yes
						is_adult = yes
						is_ai = yes
						NOR = {
							has_trait = excommunicated
							has_trait = incapable
							has_trait = devoted
						}
						faith = scope:the_faith
					}
					limit = {
						is_adult = yes
						is_ai = yes
						NOR = {
							has_trait = excommunicated
							has_trait = incapable
							has_trait = devoted
						}
						faith = scope:the_faith
					}
					save_scope_as = fallback_recipient
					add_character_flag = flag_is_ghw_fallback_recipient
				}
			}
		}
		else_if = { #Else Generate a fallback recipient.
			limit = {
				NOT = { exists = scope:the_faith.great_holy_war.ghw_title_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 = fallback_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:fallback_recipient = { add_character_flag = flag_is_ghw_fallback_recipient }
		}
		great_holy_war = {
			set_variable = {
				name = var_fallback_recipient
				value = scope:fallback_recipient
			}
		}

		every_player = { #Inform players
			limit = {
				faith = scope:the_faith
				highest_held_title_tier >= tier_county
				this != scope:ghw_sponsor
			}
			trigger_event = great_holy_war.0003 #Send event immediately for player, then delay it for all the AIs.
		}
		every_player = { #Inform defending players
			limit = {
				OR = {
					faith = scope:target_faith #All infidels.
					any_liege_or_above = {
						scope:the_faith.great_holy_war = { has_pledged_defender = prev }
					}
					AND = {
						faith != scope:the_faith
						scope:the_faith.great_holy_war = { has_pledged_defender = prev }
					}
				}
			}
			trigger_event = great_holy_war.0050
		}

		#Attackers Pledge to the Crusade. (AI only)
		every_faith_ruler = {
			limit = {
				is_ai = yes
				is_landed = yes
				highest_held_title_tier >= tier_county
				NOR = {
					this = scope:ghw_sponsor
					root.great_holy_war = { has_pledged_attacker = prev }
					target_is_liege_or_above = scope:target_character
				}
			}
			trigger_event = {
				#AI pledging. There is a chance the AI might pledge after the Crusade starts, but it's a lot less likely.
				id = great_holy_war.0020
				days = { 50 450 }
			}
		}
		every_faith_holy_order = {
			trigger_event = {
				#Holy orders always join
				id = great_holy_war.0090
				days = { 30 60 }
			}
		}

		#Defenders Pledge to the Crusade. (AI only)
		scope:target_character = {
			religion = {
				every_faith = {
					every_faith_ruler = {
						limit = {
							is_ai = yes
							is_landed = yes
							highest_held_title_tier >= tier_county
							NOT = {
								root.great_holy_war = { has_pledged_defender = prev }
							}
						}
						trigger_event = {
							#AI counterpledging. Unlike attackers, defenders should be selected much faster during the preparation phase and then stop.
							id = great_holy_war.0021
							days = { 25 150 }
						}
					}
				}
			}
			faith = {
				every_faith_holy_order = {
					trigger_event = {
						# Holy orders always join
						id = great_holy_war.0091
						days = { 30 60 }
					}
				}
			}
		}
		#EP3 Redirected Crusade setup event
		great_holy_war = {
			if = {
				limit = {
					has_ep3_dlc_trigger = yes
					scope:the_faith = { this = faith:catholic }
					ep3_frankokratia_ghw_trigger = yes
					ep3_frankokratia_financier_exists_trigger = yes
				}
				#start with player
				if = {
					limit = {
						any_player = {
							ep3_frankokratia_story_owner_trigger = yes
						}
					}
					random_player = {
						limit = {
							ep3_frankokratia_story_owner_trigger = yes
						}
						trigger_event = {
							id = ep3_frankokratia_events.0001
							days = 20
						}
					}
				}
				#Next, ai with a closely related claimant who will accept
				else_if = {
					limit = {
						scope:the_faith = {
							any_faith_playable_ruler = {
								ep3_frankokratia_story_owner_trigger = yes
								ep3_frankokratia_will_start_trigger = yes
								save_temporary_scope_as = char_temp
								
								#Thorough check for close relation claimants
								OR = {
									any_close_family_member = {
										OR = {
											is_courtier_of = prev
											is_vassal_of = prev
										}
									}
									#In laws of family in court/realm
									any_close_family_member = {
										OR = {
											is_courtier_of = prev
											is_vassal_of = prev
										}
										ep3_frankokratia_transiting_relative_trigger = yes
										any_spouse = {
											ep3_frankokratia_claimant_trigger = yes
										}
									}
									#Spouse claimant
									any_spouse = {
										ep3_frankokratia_claimant_trigger = yes
									}
									#Spouse's close family in realm/court
									any_spouse = {
										ep3_frankokratia_transiting_relative_trigger = yes
										any_close_family_member = {
											OR = {
												is_courtier_of = prev
												is_vassal_of = prev
											}
											ep3_frankokratia_claimant_trigger = yes
										}
									}
									#friends
									any_relation = {
										type = friend
										ep3_frankokratia_claimant_trigger = yes
									}
									#soulmates
									any_relation = {
										type = soulmate
										ep3_frankokratia_claimant_trigger = yes
									}
								}
							}
						}
					}
					scope:the_faith = {
						ordered_faith_playable_ruler = {
							order_by = {
								value = 0
								add = current_military_strength
							}
							limit = {
								ep3_frankokratia_story_owner_trigger = yes
								ep3_frankokratia_will_start_trigger = yes
								save_temporary_scope_as = char_temp
								
								#Thorough check for close relation claimants
								OR = {
									any_close_family_member = {
										OR = {
											is_courtier_of = prev
											is_vassal_of = prev
										}
									}
									#In laws of family in court/realm
									any_close_family_member = {
										OR = {
											is_courtier_of = prev
											is_vassal_of = prev
										}
										ep3_frankokratia_transiting_relative_trigger = yes
										any_spouse = {
											ep3_frankokratia_claimant_trigger = yes
										}
									}
									#Spouse claimant
									any_spouse = {
										ep3_frankokratia_claimant_trigger = yes
									}
									#Spouse's close family in realm/court
									any_spouse = {
										ep3_frankokratia_transiting_relative_trigger = yes
										any_close_family_member = {
											OR = {
												is_courtier_of = prev
												is_vassal_of = prev
											}
											ep3_frankokratia_claimant_trigger = yes
										}
									}
									#friends
									any_relation = {
										type = friend
										ep3_frankokratia_claimant_trigger = yes
									}
									#soulmates
									any_relation = {
										type = soulmate
										ep3_frankokratia_claimant_trigger = yes
									}
								}
							}
							trigger_event = {
								id = ep3_frankokratia_events.0001
								days = 20
							}
						}
					}
				}
				#Someone with looser relation to claimant who will accept
				else_if = {
					limit = {
						scope:the_faith = {
							any_faith_playable_ruler = {
								ep3_frankokratia_story_owner_trigger = yes
								ep3_frankokratia_will_start_trigger = yes
							}
						}
					}
					scope:the_faith = {
						ordered_faith_playable_ruler = {
							order_by = {
								value = 0
								add = current_military_strength
							}
							limit = {
								ep3_frankokratia_story_owner_trigger = yes
								ep3_frankokratia_will_start_trigger = yes
							}
							trigger_event = {
								id = ep3_frankokratia_events.0001
								days = 20
							}
						}
					}
				}
				#Next, ai with a closely related claimant who might not accept
				else_if = {
					limit = {
						scope:the_faith = {
							any_faith_playable_ruler = {
								ep3_frankokratia_story_owner_trigger = yes
								save_temporary_scope_as = char_temp
								
								#Thorough check for close relation claimants
								OR = {
									any_close_family_member = {
										OR = {
											is_courtier_of = prev
											is_vassal_of = prev
										}
									}
									#In laws of family in court/realm
									any_close_family_member = {
										OR = {
											is_courtier_of = prev
											is_vassal_of = prev
										}
										ep3_frankokratia_transiting_relative_trigger = yes
										any_spouse = {
											ep3_frankokratia_claimant_trigger = yes
										}
									}
									#Spouse claimant
									any_spouse = {
										ep3_frankokratia_claimant_trigger = yes
									}
									#Spouse's close family in realm/court
									any_spouse = {
										ep3_frankokratia_transiting_relative_trigger = yes
										any_close_family_member = {
											OR = {
												is_courtier_of = prev
												is_vassal_of = prev
											}
											ep3_frankokratia_claimant_trigger = yes
										}
									}
									#friends
									any_relation = {
										type = friend
										ep3_frankokratia_claimant_trigger = yes
									}
									#soulmates
									any_relation = {
										type = soulmate
										ep3_frankokratia_claimant_trigger = yes
									}
								}
							}
						}
					}
					scope:the_faith = {
						ordered_faith_playable_ruler = {
							order_by = {
								value = 0
								add = current_military_strength
							}
							limit = {
								ep3_frankokratia_story_owner_trigger = yes
								save_temporary_scope_as = char_temp
								
								#Thorough check for close relation claimants
								OR = {
									any_close_family_member = {
										OR = {
											is_courtier_of = prev
											is_vassal_of = prev
										}
									}
									#In laws of family in court/realm
									any_close_family_member = {
										OR = {
											is_courtier_of = prev
											is_vassal_of = prev
										}
										ep3_frankokratia_transiting_relative_trigger = yes
										any_spouse = {
											ep3_frankokratia_claimant_trigger = yes
										}
									}
									#Spouse claimant
									any_spouse = {
										ep3_frankokratia_claimant_trigger = yes
									}
									#Spouse's close family in realm/court
									any_spouse = {
										ep3_frankokratia_transiting_relative_trigger = yes
										any_close_family_member = {
											OR = {
												is_courtier_of = prev
												is_vassal_of = prev
											}
											ep3_frankokratia_claimant_trigger = yes
										}
									}
									#friends
									any_relation = {
										type = friend
										ep3_frankokratia_claimant_trigger = yes
									}
									#soulmates
									any_relation = {
										type = soulmate
										ep3_frankokratia_claimant_trigger = yes
									}
								}
							}
							trigger_event = {
								id = ep3_frankokratia_events.0001
								days = 20
							}
						}
					}
				}
				#Someone with looser relation to claimant who might not accept
				else_if = {
					limit = {
						scope:the_faith = {
							any_faith_playable_ruler = {
								ep3_frankokratia_story_owner_trigger = yes
							}
						}
					}
					scope:the_faith = {
						ordered_faith_playable_ruler = {
							order_by = {
								value = 0
								add = current_military_strength
							}
							limit = {
								ep3_frankokratia_story_owner_trigger = yes
							}
							trigger_event = {
								id = ep3_frankokratia_events.0001
								days = 20
							}
						}
					}
				}
			}
		}
		if = {
			limit = {
				NOT = {
					exists = global_var:cath_crusade_1
				}
				scope:the_faith = { this = faith:catholic }
			}
			set_global_variable = {
				name = cath_crusade_1
				value = 1
			}
		}
		else_if = {
			limit = {
				scope:the_faith = { this = faith:catholic }
			}
			change_global_variable = {
				name = cath_crusade_1
				add = 1
			}
		}
	}
}

# The preparation for the next Great Holy War begins. Faithful assemble.
great_holy_war.0003 = {
	title = great_holy_war.0003.title
	desc = {
		first_valid = {
			#Catholic flavour
			triggered_desc = {
				desc = great_holy_war.0003.catholic.desc
				trigger = { scope:the_faith = faith:catholic }
			}
			#Everyone else
			desc = great_holy_war.0003.fallback.desc
		}
	}
	theme = faith
	override_background = {
		reference = temple_scope
	}
	left_portrait = scope:ghw_sponsor
	
	immediate = {
		scope:ghw_sponsor = {
			save_scope_as = background_temple_scope
		}
		play_music_cue = "mx_cue_general_crusade_event_2"

		set_variable = {
			name = ghw_pledge_amount
			value = great_holy_war_pledge_amount
		}
		set_variable = {
			name = half_great_holy_war_pledge_amount
			value = {
				value = root.great_holy_war_pledge_amount
				multiply = 0.5
			}
		}
	}
	
	option = {
		name = great_holy_war.0003.a
		trigger = {
			root.faith = scope:the_faith
			NOR = {
				exists = scope:the_faith.great_holy_war.ghw_war
				#Religious head should never unpledge:
				this = scope:the_faith.great_holy_war.ghw_war_declarer
				has_character_flag = ghw_unpledging_cooldown #Safety event.
				#Do not pledge against your own liege:
				any_liege_or_above = { this = scope:the_faith.great_holy_war.ghw_target_character }
			}
			OR = { #Either be independent, or all lieges above you MUST be of the same Faith declaring the war to avoid messy situations of vassals and lieges fighting against each other.
				top_liege = this
				NOT = {
					any_liege_or_above = {
						faith != scope:the_faith
					}
				}
			}
		}
		custom_tooltip = great_holy_war.0003.a.tt
		if = {
			limit = {
				NOT = {
					scope:the_faith.great_holy_war = { has_pledged_attacker = root }
				}
			}
			pledge_ghw_piety_gain_effect = yes
			#Excommunication lifted.
			if = {
				limit = {
					has_trait = excommunicated
					faith = { has_doctrine_parameter = excommunication_active }
				}
				custom_tooltip = great_holy_war.0003.a.lifted_excommunication
				trigger_event = {
					id = great_holy_war.0015
					days = 37
				}
			}
		}
		scope:the_faith.great_holy_war = {
			hidden_effect = { pledge_attacker = root }
			pledge_ghw_war_chest_prestige_effect = yes
			pledge_ghw_war_chest_piety_effect = yes
		}

		ai_chance = {
			base = 10
			modifier = {
				factor = 0.5
				top_liege != this
			}
			modifier = {
				factor = 0.5
				highest_held_title_tier = tier_county
			}
			modifier = {
				factor = 5
				highest_held_title_tier >= tier_kingdom
			}
			modifier = {	#Excommunicated characters like getting rid of the trait.
				factor = 2
				has_trait = excommunicated
			}
		}
	}
	
	option = {
		name = great_holy_war.0003.b
		add_piety = root.var:half_great_holy_war_pledge_amount
		#Excommunication lifted.
		if = {
			limit = {
				has_trait = excommunicated
				faith = { has_doctrine_parameter = excommunication_active }
			}
			trigger_event = {
				id = great_holy_war.0015
				days = 37
			}
		}
		remove_short_term_gold = var:ghw_pledge_amount
		set_variable = {
			name = ghw_war_chest_donated_sum
			value = var:ghw_pledge_amount
		}
		scope:the_faith.great_holy_war = {
			change_war_chest_gold = root.var:ghw_pledge_amount
		}
		if = {
			limit = {
				NOT = { has_character_flag = ghw_pledged_gold }
				is_ai = no
			}
			add_character_flag = { #Used to check whether the Pope will harass for more later.
				flag = ghw_pledged_gold
				days = 7300
			}
		}
		ai_chance = {
			base = 10
		}
	}
	
	option = {
		name = great_holy_war.0003.c
		ai_chance = {
			base = 0
		}
	}
	
	after = {
		remove_variable = ghw_pledge_amount
	}
}

# Rel head selects new target
great_holy_war.0004 = {
	hidden = yes
	scope = ghw
	
	immediate = {
		faith = { save_scope_as = the_faith }
		
		determine_ghw_target_effect = { FAITH = scope:the_faith }
		
		set_great_holy_war_target = { target_title = scope:target_kingdom target_character = scope:target_character }
		if = {
			limit = { exists = var:last_redirector }
			change_variable = {
				name = redirect_cost
				divide = 2
			}
			var:last_redirector = { add_piety = root.var:redirect_cost }
			remove_variable = last_redirector
		}
	}
}

# War is launched
great_holy_war.0005 = {
	hidden = yes
	scope = ghw

	immediate = {
		if = {
			limit = {
				any_pledged_attacker = {
					primary_title = { is_head_of_faith = yes }
					faith = faith:catholic
				}
				exists = global_var:byz_claimant_champion
				global_var:byz_claimant_champion = {
					is_alive = yes
					is_ruler = yes
					any_owned_story = {
						type = frankokratia_story
					}
				}
				exists = title:e_byzantium.holder
			}
			#Check claimant isn't dead, invalidate if they are
			if = {
				limit = {
					global_var:byz_claimant_champion = {
						any_owned_story = {
							type = frankokratia_story
							var:byz_claimant = { is_alive = no }
						}
					}
				}
				global_var:byz_claimant_champion = {
					random_owned_story = {
						type = frankokratia_story
						every_in_list = {
							variable = frankokratia_leaders
							limit = {
								is_ai = no
							}
							trigger_event = ep3_frankokratia_events.0005
						}
					}
				}
			}
			else = {
				ep3_frankokratia_remove_pledged_attackers_effect = yes
			}
		}
		
		start_ghw_war = undirected_great_holy_war
		divide_war_chest = {
			fraction = 0.2
			prestige = no
			gold = no
		}
	}
}

#Unlocking Holy Wars. (on later dates Crusades and Jihads start already unlocked from Character History)
#Hidden check event, fired on_faith pulse.

scripted_trigger great_holy_war_0010_jerusalem_reason_trigger = {
	title:c_jerusalem.holder.top_liege = {
		faith.religion != scope:awakening_faith.religion
	}
}

scripted_trigger great_holy_war_0010_rome_reason_trigger = {
	title:c_roma.holder.top_liege = {
		faith.religion != scope:awakening_faith.religion
	}
}

scripted_trigger great_holy_war_0010_byzantion_reason_trigger = {
	OR = {
		trigger_if = { # Byzantine Empire
			limit = { exists = title:e_byzantium.holder } # Make sure a holder exists beforehand
			title:e_byzantium.holder.religion = {
				NOT = {
					this = religion:christianity_religion
				}
			}
		}
		title:c_byzantion.holder.top_liege.religion = { # Constantinople
			NOT = {
				this = religion:christianity_religion
			}
		}
		title:c_paphlagonia.holder.top_liege.religion = { # Ankyra
			NOT = {
				this = religion:christianity_religion
			}
		}
		title:c_attica.holder.top_liege.religion = { # Athens
			NOT = {
				this = religion:christianity_religion
			}
		}
		title:c_korinthos.holder.top_liege.religion = { # Corinth
			NOT = {
				this = religion:christianity_religion
			}
		}
	}
}

scripted_trigger great_holy_war_0010_europe_reason_trigger = {
	OR = {
		title:c_venaissin.holder.top_liege.religion = { # Provence
			NOT = {
				this = religion:christianity_religion
			}
		}
		title:c_toulouse.holder.top_liege.religion = { # Toulouse
			NOT = {
				this = religion:christianity_religion
			}
		}
		title:c_ile_de_france.holder.top_liege.religion = { # Paris
			NOT = {
				this = religion:christianity_religion
			}
		}
		title:c_cologne.holder.top_liege.religion = { # Cologne
			NOT = {
				this = religion:christianity_religion
			}
		}
		title:c_halberstadt.holder.top_liege.religion = { # Halberstadt
			NOT = {
				this = religion:christianity_religion
			}
		}
	}
}

scripted_trigger great_holy_war_0010_mecca_reason_trigger = {
	title:c_mecca.holder.top_liege = {
		faith.religion != scope:awakening_faith.religion
	}
}

scripted_trigger great_holy_war_0010_centre_reason_trigger = {
	OR = {
		title:c_baghdad.holder.top_liege.religion = { # Baghdad
			NOT = {
				this = religion:christianity_religion
			}
		}
		title:c_damascus.holder.top_liege.religion = { # Damascus
			NOT = {
				this = religion:christianity_religion
			}
		}
		title:c_cairo.holder.top_liege.religion = { # Cairo
			NOT = {
				this = religion:christianity_religion
			}
		}
	}
}

scripted_trigger great_holy_war_0010_africa_reason_trigger = {
	OR = {
		title:c_marrakesh.holder.top_liege.religion = { # Marrakesh
			NOT = {
				this = religion:christianity_religion
			}
		}
		title:c_tunis.holder.top_liege.religion = { # Tunis
			NOT = {
				this = religion:christianity_religion
			}
		}
		title:c_algier.holder.top_liege.religion = { # Algier
			NOT = {
				this = religion:christianity_religion
			}
		}
	}
}

great_holy_war.0010 = {
	hidden = yes
	scope = faith
	
	trigger = {
		suitable_faith_for_any_ghw_trigger = yes # Faith must have right Doctrines.
		NOR = {
			religion = { exists = var:variable_ghw_unlocked }
			has_global_variable = variable_ghw_global_unlocking_cooldown
		}
		religious_head ?= { is_imprisoned = no }
		num_county_followers >= 35 # Faith must have at least SOME legitimacy.
		OR = {
			AND = { # Historical setup.
				title:c_jerusalem.holder = {
					faith.religion != root.religion
				}
				current_date >= 1095.1.1
				faith:catholic = root
			}
			AND = { # Historical setup.
				great_holy_war_0010_byzantion_reason_trigger = yes
				current_date >= 1000.1.1
				faith:catholic = root
			}
			AND = { # Historical setup.
				great_holy_war_0010_europe_reason_trigger = yes
				current_date >= 800.1.1
				faith:catholic = root
			}
			AND = { # Historical setup.
				title:c_roma.holder.top_liege = {
					faith.religion != root.religion
				}
				faith:catholic = root
			}
			current_date >= 1100.1.1 # Free for all after Historical years
			AND = { # Any Faith is seriously under attack.
				any_holy_site = {
					holder.top_liege = {
						faith.religion != root.religion
					}
					count >= 2
				}
				current_date >= 800.1.1
			}
			AND = { # I learned it from you! - Muslims second.
				religion:christianity_religion = {
					exists = var:variable_ghw_unlocked
				}
				religion:islam_religion = root.religion
				any_holy_site = {
					holder.top_liege = {
						faith.religion != root.religion
					}
					count >= 1
				}
			}
			AND = { # And we learned it from you! - Everyone else last.
				religion:christianity_religion = {
					exists = var:variable_ghw_unlocked
				}
				religion:islam_religion = {
					exists = var:variable_ghw_unlocked
				}
				any_holy_site = {
					holder.top_liege = {
						faith.religion != root.religion
					}
					count >= 1
				}
				NOR = {
					OR = { religion = religion:eastern_orthodox_religion religion = religion:catholic_religion religion = religion:protestant_religion }
					religion = religion:islam_religion
				}
			}
		}
	}

	weight_multiplier = {
		base = 1
		modifier = { # Historical push for local Christian control.
			add = 2
			title:c_jerusalem.holder = {
				NOT = { OR = { faith.religion = religion:eastern_orthodox_religion faith.religion = religion:catholic_religion faith.religion = religion:protestant_religion } }
			}
			current_date >= 1095.1.1
			OR = { faith.religion = religion:eastern_orthodox_religion faith.religion = religion:catholic_religion faith.religion = religion:protestant_religion }
		}
		modifier = { # Historical push for overall Islamic control.
			add = 2
			title:c_jerusalem.holder.top_liege = {
				NOT = { faith.religion = religion:islam_religion }
			}
			current_date >= 1095.1.1
			root.religion = religion:islam_religion
		}
	}
	
	immediate = {
		set_global_variable = {
			name = variable_ghw_global_unlocking_cooldown # Slow Down cascade of Religious Heads unlocking Crusades all over the place, trying to outplay ach other.
			value = yes
			days = 3650
		}
		religion = {
			set_variable = {
				name = variable_first_ghw_cooldown # Wait at least a couple of months before firing the first Crusade after this event.
				value = yes
				days = 120
			}	
		}	
		save_scope_as = awakening_faith
		if = {
			limit = {
				exists = religious_head
			}
			religious_head = { save_scope_as = ghw_first_sponsor }
		}
		else = {
			random_theocratic_ruler = {
				limit = { faith = root }
				save_scope_as = ghw_first_sponsor
			}
		}
		if = { # Fallback
			limit = {
				NOT = {
					exists = scope:ghw_first_sponsor
				}
			}
			random_ruler = {
				limit = { faith = root }
				save_scope_as = ghw_first_sponsor
			}
		}
		religion = {
			set_variable = {
				name = variable_ghw_unlocked # Crusades
				value = yes
			}
		}
		every_player = {
			trigger_event = great_holy_war.0011 # Flavor fluff for various Faiths.
		}
	}
}

#Flavor Fluff.
great_holy_war.0011 = {
	type = character_event
	title = {
		first_valid = {
			triggered_desc = {
				trigger = {
					OR = { scope:awakening_faith.religion = religion:eastern_orthodox_religion scope:awakening_faith.religion = religion:catholic_religion scope:awakening_faith.religion = religion:protestant_religion }
				}
				desc = great_holy_war.0011.christian.t
			}
			triggered_desc = {
				trigger = {
					scope:awakening_faith.religion = religion:islam_religion
				}
				desc = great_holy_war.0011.muslim.t
			}
			triggered_desc = { #Pagan Polytheists
				trigger = {
					scope:awakening_faith.religion = {
						is_in_family = rf_pagan
					}
				}
				desc = great_holy_war.0011.pagans.t
			}
			desc = great_holy_war.0011.fallback.t
		}
	}
	desc = {
		first_valid = {
			#Christian Flavor
			triggered_desc = { #Catholic Crusades, because of Rome.
				trigger = {
					scope:awakening_faith = faith:catholic
					exists = scope:ghw_first_sponsor
					great_holy_war_0010_rome_reason_trigger = yes
				}
					desc = great_holy_war.0011.catholic.rome.desc
			}
			triggered_desc = { #Christian Crusades, because of fall of Europe.
				trigger = {
					OR = { scope:awakening_faith.religion = religion:eastern_orthodox_religion scope:awakening_faith.religion = religion:catholic_religion scope:awakening_faith.religion = religion:protestant_religion }
					exists = scope:ghw_first_sponsor
					great_holy_war_0010_europe_reason_trigger = yes
				}
					desc = great_holy_war.0011.christian.europe.desc
			}
			triggered_desc = { #Christian Crusades, because of fall of Byzantium.
				trigger = {
					OR = { scope:awakening_faith.religion = religion:eastern_orthodox_religion scope:awakening_faith.religion = religion:catholic_religion scope:awakening_faith.religion = religion:protestant_religion }
					exists = scope:ghw_first_sponsor
					great_holy_war_0010_byzantion_reason_trigger = yes
				}
					desc = great_holy_war.0011.christian.byzantion.desc
			}
			triggered_desc = { #Catholic Crusades, because of Jerusalem.
				trigger = {
					scope:awakening_faith = faith:catholic
					exists = scope:ghw_first_sponsor
					great_holy_war_0010_jerusalem_reason_trigger = yes
				}
					desc = great_holy_war.0011.catholic.jerusalem.desc
			}
			triggered_desc = { #Christian Crusades, fallback.
				trigger = {
					OR = { scope:awakening_faith.religion = religion:eastern_orthodox_religion scope:awakening_faith.religion = religion:catholic_religion scope:awakening_faith.religion = religion:protestant_religion }
				}
					desc = great_holy_war.0011.christian.fallback.desc
			}

			#Muslim Flavor
			triggered_desc = { #Jihads, because of Mecca.
				trigger = {
					scope:awakening_faith.religion = religion:islam_religion
					exists = scope:ghw_first_sponsor
					great_holy_war_0010_mecca_reason_trigger = yes
				}
					desc = great_holy_war.0011.muslim.mecca.desc
			}
			triggered_desc = { #Jihads, because of Jerusalem.
				trigger = {
					scope:awakening_faith.religion = religion:islam_religion
					exists = scope:ghw_first_sponsor
					great_holy_war_0010_mecca_reason_trigger = yes
				}
					desc = great_holy_war.0011.muslim.jerusalem.desc
			}
			triggered_desc = { #Jihads, because of Arabia/Egypt.
				trigger = {
					scope:awakening_faith.religion = religion:islam_religion
					exists = scope:ghw_first_sponsor
					great_holy_war_0010_centre_reason_trigger = yes
				}
					desc = great_holy_war.0011.muslim.arabia.desc
			}
			triggered_desc = { #Jihads, because of Africa.
				trigger = {
					scope:awakening_faith.religion = religion:islam_religion
					exists = scope:ghw_first_sponsor
					great_holy_war_0010_africa_reason_trigger = yes
				}
					desc = great_holy_war.0011.muslim.africa.desc
			}
			triggered_desc = { #Jihads, fallback.
				trigger = {
					scope:awakening_faith.religion = religion:islam_religion
				}
					desc = great_holy_war.0011.muslim.fallback.desc
			}
			#Generic Flavor
			triggered_desc = { #Generic, with Head.
				trigger = {
					NOR = {
						scope:awakening_faith.religion = religion:islam_religion
						OR = { scope:awakening_faith.religion = religion:eastern_orthodox_religion scope:awakening_faith.religion = religion:catholic_religion scope:awakening_faith.religion = religion:protestant_religion }
					}
					exists = scope:ghw_first_sponsor
				}
					desc = great_holy_war.0011.generic.fallback.head.desc
			}
				desc = great_holy_war.0011.generic.fallback.desc
		}
		first_valid = { #Ending, favorable or not, depending on receiver.
			triggered_desc = { # Crusades are good!
				trigger = {
					OR = {
						faith = scope:awakening_faith
						faith = {
							faith_hostility_level = {
								target = scope:awakening_faith
								value < faith_hostile_level
							}
						}
					}
					faith.religion != scope:awakening_faith.religion
				}
					desc = great_holy_war.0011.generic.ending.positive
			}
			triggered_desc = { # Crusades are bad!
				trigger = {
					NOR = {
						faith = scope:awakening_faith
						faith.religion = scope:awakening_faith.religion
						faith = {
							faith_hostility_level = {
								target = scope:awakening_faith
								value < faith_hostile_level
							}
						}
					}
				}
					desc = great_holy_war.0011.generic.ending.negative
			}
			triggered_desc = { # Co-religionists.
				trigger = {
					faith.religion = scope:awakening_faith.religion
					faith != scope:awakening_faith
					exists = faith.religious_head
				}
					desc = great_holy_war.0011.generic.ending.coreligionist.head
			}
			triggered_desc = { # Co-religionists (no Head).
				trigger = {
					faith.religion = scope:awakening_faith.religion
					faith != scope:awakening_faith
					NOT = { exists = faith.religious_head }
				}
					desc = great_holy_war.0011.generic.ending.coreligionist.autonomous
			}
		}
	}
	theme = faith
	override_background = {
		reference = temple_scope
	}
	left_portrait = {
		character = scope:ghw_first_sponsor
		triggered_animation = {
			trigger = {
				scope:awakening_faith = {
					has_doctrine = doctrine_temporal_head
				}
			}
  			animation = marshal
  		}
		animation = personality_zealous
	}
	
	immediate = {
		scope:ghw_first_sponsor = {
			save_scope_as = background_temple_scope
		}
		play_music_cue = "mx_cue_epic_sacral_moment"

		if = { #Localisation use only
			limit = {
				faith.religion = scope:awakening_faith.religion
				faith != scope:awakening_faith
				exists = faith.religious_head
			}
			faith.religious_head = {
				save_scope_as = other_pope
			}
		}
	}

	option = { #God's will
		name = great_holy_war.0011.a
		trigger = {
			root.faith = scope:awakening_faith
			OR = { scope:awakening_faith.religion = religion:eastern_orthodox_religion scope:awakening_faith.religion = religion:catholic_religion scope:awakening_faith.religion = religion:protestant_religion }
		}
	}
	option = { #Allahu Akhbar
		name = great_holy_war.0011.b
		trigger = {
			root.faith = scope:awakening_faith
			scope:awakening_faith.religion = religion:islam_religion
		}
	}
	option = { #PaganGodName is with us.
		name = great_holy_war.0011.c
		trigger = {
			root.faith = scope:awakening_faith
			NOR = {
				OR = { scope:awakening_faith.religion = religion:eastern_orthodox_religion scope:awakening_faith.religion = religion:catholic_religion scope:awakening_faith.religion = religion:protestant_religion }
				scope:awakening_faith.religion = religion:islam_religion
			}
		}
	}
	option = { #Infidels incoming.
		name = great_holy_war.0011.d
		trigger = {
			root.faith != scope:awakening_faith
			root.faith = {
				faith_hostility_level = {
					target = scope:awakening_faith
					value >= faith_hostile_level
				}
			}
		}
	}
	option = { #Good luck, I suppose...
		name = great_holy_war.0011.e
		trigger = {
			NOR = {
				root.faith = scope:awakening_faith
				root.faith = {
					faith_hostility_level = {
					target = scope:awakening_faith
					value >= faith_hostile_level
					}
				}
			}
		}
	}
	after = {
		custom_tooltip = great_holy_war.0011.tooltip.effect
	}
}

#Excommunication lifted for joining the Crusade.
great_holy_war.0015 = {
	type = letter_event
	opening = {
		desc = great_holy_war.0015.opening
	}
	desc = great_holy_war.0015.desc
	sender = {
		character = scope:scoped_pope
		animation = happiness
	}
	
	trigger = {
		faith.great_holy_war = {
			has_pledged_attacker = root
		}
		has_trait = excommunicated
	}

	immediate = {
		if = { #If Pope died in the meantime.
			limit = {
				OR = {
					NOT = {
						exists = faith.great_holy_war.ghw_war_declarer
					}
					NOT = {
						faith.great_holy_war.ghw_war_declarer = { is_alive = yes }
					}
				}
			}
			faith = { ghw_set_new_war_declarer_effect = yes }
		}
		root.faith.great_holy_war.ghw_war_declarer = { save_scope_as = scoped_pope }
		remove_trait = excommunicated
		add_character_modifier = {
			modifier = excommunication_recently_lifted
			years = 10
		}
	}
	
	option = {
		name = great_holy_war.0015.a
		trigger = { has_trait = cynical }
	}
	option = {
		name = great_holy_war.0015.b
		trigger = {
			NOT = { has_trait = cynical }
		}
	}
}

#Pope upset because ruler unpledged.
great_holy_war.0016 = {
	type = letter_event
	opening = {
		desc = great_holy_war.0015.opening
	}
	desc = {
		first_valid = { #Light Punishment
			triggered_desc = {
				trigger = {
					has_character_flag = ghw_light_punishment
				}
				desc = great_holy_war.0016.desc.a
			}
		}
		first_valid = { #Harsh Punishment
			triggered_desc = {
				trigger = {
					has_character_flag = ghw_harsh_punishment
				}
				desc = great_holy_war.0016.desc.b
			}
		}
		first_valid = { #Excommunication Punishment
			triggered_desc = {
				trigger = {
					has_character_flag = ghw_excommunication
				}
				desc = great_holy_war.0016.desc.c
			}
		}
	}
	sender = {
		character = scope:scoped_pope
		animation = anger
	}
	
	trigger = {
		NOT = {
			faith.great_holy_war = {
				has_pledged_attacker = root
			}
		}
		is_landless_adventurer = no
	}

	immediate = {
		hidden_effect = {
			if = { #If Pope died in the meantime.
				limit = {
					OR = {
						NOT = {
							exists = faith.great_holy_war.ghw_war_declarer
						}
						NOT = {
							faith.great_holy_war.ghw_war_declarer = { is_alive = yes }
						}
					}
				}
				faith = { ghw_set_new_war_declarer_effect = yes }
			}
			root.faith.great_holy_war.ghw_war_declarer = { save_scope_as = scoped_pope }
			random_list = {
				40 = {
					opinion_modifier = {
						who = scope:scoped_pope
						opinion_target = root
					}
					modifier = {
						factor = 2
						highest_held_title_tier = tier_county
					}
					add_character_flag = {
						flag = ghw_light_punishment
						days = 500
					}
				}
				30 = {
					modifier = {
						factor = 1.25
						highest_held_title_tier >= tier_duchy
					}
					add_character_flag = {
						flag = ghw_harsh_punishment
						days = 500
					}
				}
				30 = { #Excommunication available only if target already not excommunicated and religion allows it.
					trigger = {
						NOT = { has_trait = excommunicated }
						faith = { has_doctrine_parameter = excommunication_active }
						highest_held_title_tier >= tier_kingdom
					}
					modifier = { #Fool me once...
						factor = 2
						has_character_modifier = excommunication_recently_lifted
					}
					modifier = {
						factor = 2
						highest_held_title_tier >= tier_empire
					}
					add_character_flag = {
						flag = ghw_excommunication
						days = 500
					}
				}
			}
		}
	}
	
	option = {
		name = great_holy_war.0016.a
		trigger = { has_trait = cynical }
	}
	option = {
		name = great_holy_war.0016.b
		trigger = {
			NOT = { has_trait = cynical }
		}
	}
	after = {
		if = {
			limit = { has_character_flag = ghw_light_punishment }
			scope:scoped_pope = {
				add_opinion = {
					target = root
					modifier = unpledged_from_ghw_opinion
					opinion = -25
				}
			}
		}
		if = {
			limit = { has_character_flag = ghw_harsh_punishment }
			add_piety = medium_piety_loss
			scope:scoped_pope = {
				add_opinion = {
					target = root
					modifier = unpledged_from_ghw_opinion
					opinion = -35
				}
			}
			if = {
				limit = {
					any_theocratic_vassal = { faith = root.faith }
				}
				every_theocratic_vassal = {
					custom = great_holy_war.0016.custom
					limit = { faith = root.faith }
					add_opinion = {
						target = root
						modifier = impious_opinion
						opinion = -35
					}
				}
			}
			if = {
				limit = { exists = cp:councillor_court_chaplain }
				add_opinion = {
					target = root
					modifier = impious_opinion
					opinion = -35
				}
			}
		}
		if = {
			limit = { has_character_flag = ghw_excommunication }
			add_excommunication_actual_effect = yes
			if = {
				limit = { has_character_modifier = excommunication_recently_lifted }
				hidden_effect = { remove_character_modifier = excommunication_recently_lifted }
			}
			scope:scoped_pope = {
				add_opinion = {
					target = root
					modifier = unpledged_from_ghw_opinion
					opinion = -40
				}
			}
		}
		remove_character_flag = ghw_light_punishment
		remove_character_flag = ghw_harsh_punishment
		remove_character_flag = ghw_excommunication
		stress_impact = {
			zealous = medium_stress_impact_gain
		}
	}
}

#Confirmation to unpledge.
great_holy_war.0017 = {
	title = great_holy_war.0017.title
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:scoped_pope = { faith = faith:catholic }
				}
				desc = great_holy_war.0017.desc.a
			}
			triggered_desc = {
				trigger = {
					NOT = {
						scope:scoped_pope = { faith = faith:catholic }
					}
				}
				desc = great_holy_war.0017.desc.b
			}
		}
		}
	theme = faith
	left_portrait = scope:scoped_pope

	immediate = {
		if = {
			limit = {
				NOR = {
					exists = faith.great_holy_war.ghw_war_declarer
					faith.great_holy_war.ghw_war_declarer = { is_alive = yes }
				}
			}
			faith = { ghw_set_new_war_declarer_effect = yes }
		}
		root.faith.great_holy_war.ghw_war_declarer = { save_scope_as = scoped_pope }
		add_character_flag = {
			flag = ghw_unpledging_cooldown
			days = 500
		}
	}
	
	option = {
		name = great_holy_war.0017.a
		if = {
			limit = { #Stop from doing it if the player opens the Crusade Interface and joins from there.
				scope:great_holy_war = { has_pledged_attacker = root }
			}
			root.faith.great_holy_war = {
				unpledge_attacker = root
				unpledge_ghw_war_chest_prestige_effect = yes
				unpledge_ghw_war_chest_piety_effect = yes
			}
		}
		#Piety price
		pledge_ghw_piety_loss_effect = yes
		add_piety_level = -1
		#Set Cooldown
		set_variable = {
			name = variable_ghw_unpledged_cooldown
			value = yes
			days = 60
		}
		#Religious Head is upset
		trigger_event = {
			id = great_holy_war.0016
			days = 37
		}
		remove_character_flag = ghw_unpledging_cooldown
		add_character_flag = { #Used to check whether the Pope will harass for more later.
			flag = ghw_unpledged_once
			days = 550
		}
	}
	option = {
		name = great_holy_war.0017.b
		remove_character_flag = ghw_unpledging_cooldown
	}
}

#Confirmation to pledge.
great_holy_war.0018 = {
	title = great_holy_war.0003.title
	desc = great_holy_war.0018.desc
	theme = faith
	left_portrait = scope:scoped_pope

	immediate = {
		if = {
			limit = {
				OR = {
					NOT = {
						exists = faith.great_holy_war.ghw_war_declarer
					}
					NOT = {
						faith.great_holy_war.ghw_war_declarer = { is_alive = yes }
					}
				}
			}
			faith = { ghw_set_new_war_declarer_effect = yes }
		}
		root.faith.great_holy_war.ghw_war_declarer = { save_scope_as = scoped_pope }
		add_character_flag = {
			flag = ghw_unpledging_cooldown
			days = 500
		}
	}
	
	option = {
		name = great_holy_war.0018.a
		faith.great_holy_war = {
			pledge_attacker = root
			pledge_ghw_war_chest_prestige_effect = yes
			pledge_ghw_war_chest_piety_effect = yes
		}
		#Piety bonus
		root = {
			pledge_ghw_piety_gain_effect = yes
			#Excommunication lifted.
			if = {
				limit = {
					has_trait = excommunicated
					faith = { has_doctrine = tenet_communion }
				}
				trigger_event = {
					id = great_holy_war.0015
					days = 37
				}
			}
		}
		remove_character_flag = ghw_unpledging_cooldown
	}
	option = {
		name = great_holy_war.0018.b
		remove_character_flag = ghw_unpledging_cooldown
	}
}

# AI hidden event to pledge to a Great Holy War during the preparation phase.
# Event is fired when the Pope calls for the Crusade.
# Requires a the_faith scope, defined by event.
great_holy_war.0020 = {
	hidden = yes
	
	trigger = {
		is_ai = yes
		is_ruler = yes
		is_landed = yes
		is_at_war = no
		is_imprisoned = no
		highest_held_title_tier >= tier_county
		NOT = { has_trait = incapable }
		ai_zeal >= 0
		root.faith = scope:the_faith
		NOT = { target_is_liege_or_above = scope:target_character }
		scope:the_faith = { # Crusade still exists
			great_holy_war ?= {
				NOT = { has_pledged_attacker = root }
			}
		}
	}

	immediate = {
		scope:the_faith.great_holy_war = {
			set_variable = {
				name = triple_defenders_strength
				value = {
					value = ghw_defenders_strength
					multiply = 3
				}
			}
			set_variable = {
				name = double_defenders_strength
				value = {
					value = ghw_defenders_strength
					multiply = 2
				}
			}
			set_variable = {
				name = one_and_a_half_of_defenders_strength
				value = {
					value = ghw_defenders_strength
					multiply = 1.5
				}
			}
			set_variable = {
				name = half_of_defenders_strength
				value = {
					value = ghw_defenders_strength
					multiply = 0.5
				}
			}
			set_variable = {
				name = quarter_of_defenders_strength
				value = {
					value = ghw_defenders_strength
					multiply = 0.25
				}
			}
		}
		random_list = {
			25 = { # Pledge.
				trigger = {
					OR = { # Either be independent, or have no liege above you of the defending Faith, to avoid messy situations of vassals and lieges fighting against each other.
						top_liege = this
						any_liege_or_above = {
							count = all
							faith != scope:target_faith
						}
					}
				}
				scope:the_faith.great_holy_war = {
					pledge_attacker = root
					pledge_ghw_war_chest_prestige_effect = yes
					pledge_ghw_war_chest_piety_effect = yes
				}
				pledge_ghw_piety_gain_effect = yes
				# Excommunication lifted.
				if = {
					limit = {
						has_trait = excommunicated
						faith = { has_doctrine_parameter = excommunication_active }
					}
					trigger_event = {
						id = great_holy_war.0015
						days = 37
					}
				}
				if = {
					limit = { is_ai = yes }
					trigger_event = { id = great_holy_war.0026 } # Set Beneficiary automatically.
				}
			}
			75 = { # Do not pledge.
				random = { # When not pledging, there is still a chance that the AI will donate money.
					chance = 35
					modifier = {
						factor = 0.75
						has_trait = greedy
					}
					modifier = {
						factor = 1.5
						has_trait = generous
					}
					modifier = {
						factor = 0.25
						has_trait = cynical
					}
					modifier = {
						factor = 1.75
						has_trait = zealous
					}
					modifier = {
						factor = 1.75
						short_term_gold >= root.great_holy_war_pledge_amount
					}
					ai_value_modifier = { # Raw Zeal
						ai_zeal = 1.0
					}
					scope:the_faith.great_holy_war = {
						change_war_chest_gold = {
							value = root.great_holy_war_pledge_amount
							multiply = 0.5
						}
					}
				}

				modifier = { # More pledging happening at start of preparation phase.
					factor = 0.75
					NOT = { exists = scope:the_faith.great_holy_war.ghw_war }
					scope:the_faith.great_holy_war = { days_until_ghw_launch > 200 }
				}
				modifier = { # Less pledging happening after.
					factor = 1.5
					NOT = { exists = scope:the_faith.great_holy_war.ghw_war }
					scope:the_faith.great_holy_war = { days_until_ghw_launch < 150 }
				}
				modifier = { # Less pledging happening after.
					factor = 1.75
					NOT = { exists = scope:the_faith.great_holy_war.ghw_war }
					scope:the_faith.great_holy_war = { days_until_ghw_launch < 100 }
				}
				modifier = { # Less pledging happening after.
					factor = 1.75
					NOT = { exists = scope:the_faith.great_holy_war.ghw_war }
					scope:the_faith.great_holy_war = { days_until_ghw_launch < 70 }
				}
				modifier = { # More likely if war is close by.
					factor = 0.85
					scope:target_kingdom = {
						capital_vassal.capital_vassal = {
							squared_distance = { target = root.capital_county value < 150000 }
						}
					}
				}
				modifier = { # More likely if war is close by.
					factor = 0.9
					scope:target_kingdom = {
						capital_vassal.capital_vassal = {
							squared_distance = { target = root.capital_county value < 1500000 } # Roughly Rome to Portugal
						}
					}
				}
				modifier = { # Less likely if war is far away.
					factor = 1.5
					scope:target_kingdom = {
						capital_vassal.capital_vassal = {
							squared_distance = { target = root.capital_county value > 2000000 }
						}
					}
				}
				modifier = { # Just too darn far away...
					factor = 1.75
					scope:target_kingdom = {
						capital_vassal.capital_vassal = {
							squared_distance = { target = root.capital_county value > 2500000 }
						}
					}
				}
				modifier = { # Much less likely to happen if the war has started.
					factor = 0.25
					exists = scope:the_faith.great_holy_war.ghw_war
				}
				modifier = { # Don't get too crowded now.
					factor = 2
					scope:the_faith.great_holy_war = { ghw_attackers_strength > ghw_defenders_strength }
				}
				modifier = { # Don't get too crowded now.
					factor = 1.85
					scope:the_faith.great_holy_war = { ghw_attackers_strength > var:triple_defenders_strength }
				}
				modifier = { # Don't get too crowded now.
					factor = 1.75
					scope:the_faith.great_holy_war = { ghw_attackers_strength > var:double_defenders_strength }
				}
				modifier = { # Don't get too crowded now.
					factor = 1.5
					scope:the_faith.great_holy_war = { ghw_attackers_strength > var:one_and_a_half_of_defenders_strength }
				}
				modifier = { # Try to prevent the Crusade from failing before even starting. (Less strength than defenders)
					factor = 0.75
					NOT = { exists = scope:the_faith.great_holy_war.ghw_war }
					scope:the_faith.great_holy_war = { ghw_attackers_strength < ghw_defenders_strength }
				}
				modifier = { # Try to prevent the Crusade from failing before even starting. (Less than half of the defenders' strength)
					factor = 0.5
					NOT = { exists = scope:the_faith.great_holy_war.ghw_war }
					scope:the_faith.great_holy_war = { ghw_attackers_strength < var:half_of_defenders_strength }
				}
				modifier = { # Try to prevent the Crusade from failing before even starting. (Less than quarter of the defenders' strength)
					factor = 0.35
					NOT = { exists = scope:the_faith.great_holy_war.ghw_war }
					scope:the_faith.great_holy_war = { ghw_attackers_strength < var:quarter_of_defenders_strength }
				}
				modifier = { # Slightly more likely to join when fighting for a Holy Site.
					factor = 0.85
					scope:target_kingdom = {
						any_in_de_jure_hierarchy = {
							tier = tier_barony
							continue = { tier > tier_barony }
							is_holy_site_of = scope:the_faith
						}
					}
				}
				modifier = { # Hoping for forgiveness
					factor = 0.8
					has_trait = excommunicated
				}
				ai_value_modifier = { # Raw Zeal
					ai_zeal = -1.0
				}
				modifier = { # Basic traits
					factor = 2
					has_trait = cynical
				}
				modifier = { # Basic traits
					factor = 0.5
					has_trait = zealous
				}
				modifier = { # Tier
					factor = 0.75
					highest_held_title_tier = tier_kingdom
				}
				modifier = { # Tier
					factor = 0.5
					highest_held_title_tier = tier_empire
				}
			}
		}
	}
}

#AI hidden event to pledge to a Directed Great Holy War.
#Event is fired on_declaration.
#Requires a the_faith scope, defined in the cb.
great_holy_war.0092 = {
	hidden = yes
	
	trigger = {
		is_ai = yes
		is_ruler = yes
		is_landed = yes
		is_at_war = no
		is_imprisoned = no
		highest_held_title_tier >= tier_county
		NOT = { has_trait = incapable }
		ai_zeal >= 0
		root.faith = scope:the_faith
		NOT = { target_is_liege_or_above = scope:ghw_defender } #scope:target_character isn't created in the holy war decision where this event is called
		scope:the_faith = { #Crusade still exists
			great_holy_war ?= {
				NOT = { has_pledged_attacker = root }
			}
		}
	}

	immediate = {
		scope:the_faith.great_holy_war = {
			set_variable = {
				name = triple_defenders_strength
				value = {
					value = ghw_defenders_strength
					multiply = 3
				}
			}
			set_variable = {
				name = double_defenders_strength
				value = {
					value = ghw_defenders_strength
					multiply = 2
				}
			}
			set_variable = {
				name = one_and_a_half_of_defenders_strength
				value = {
					value = ghw_defenders_strength
					multiply = 1.5
				}
			}
			set_variable = {
				name = half_of_defenders_strength
				value = {
					value = ghw_defenders_strength
					multiply = 0.5
				}
			}
			set_variable = {
				name = quarter_of_defenders_strength
				value = {
					value = ghw_defenders_strength
					multiply = 0.25
				}
			}
		}
		random_list = {
			35 = { #Pledge.
				trigger = {
					OR = { # Either be independent, or have no liege above you of the defending Faith, to avoid messy situations of vassals and lieges fighting against each other.
						top_liege = this
						any_liege_or_above = {
							count = all
							faith != scope:target_faith
						}
					}
				}
				scope:the_faith.great_holy_war = {
					pledge_attacker = root
					pledge_ghw_war_chest_prestige_effect = yes
					pledge_ghw_war_chest_piety_effect = yes
				}
				if = {
					limit = { is_ai = yes }
					trigger_event = {
						id = great_holy_war.0026 #Set Beneficiary automatically.
					}
				}
			}
			65 = { #Do not pledge.
				
				random = { #When not pledging, there is still a chance that the AI will donate money.
					chance = 35
					modifier = {
						factor = 0.75
						has_trait = greedy
					}
					modifier = {
						factor = 1.5
						has_trait = generous
					}
					modifier = {
						factor = 0.25
						has_trait = cynical
					}
					modifier = {
						factor = 1.75
						has_trait = zealous
					}
					modifier = {
						factor = 1.75
						short_term_gold >= root.great_holy_war_pledge_amount
					}
					ai_value_modifier = { #Raw Zeal
						ai_zeal = 1.0
					}
					scope:the_faith.great_holy_war = {
						change_war_chest_gold = root.great_holy_war_pledge_amount
					}
				}
				modifier = { #More likely if war is close by.
					factor = 0.85
					scope:target_kingdom = {
						capital_vassal.capital_vassal = {
							squared_distance = { target = root.capital_county value < 150000 }
						}
					}
				}
				modifier = { #More likely if war is close by.
					factor = 0.9
					scope:target_kingdom = {
						capital_vassal.capital_vassal = {
							squared_distance = { target = root.capital_county value < 1500000 } # Roughly Rome to Portugal
						}
					}
				}
				modifier = { #Less likely if war is far away.
					factor = 2
					scope:target_kingdom = {
						capital_vassal.capital_vassal = {
							squared_distance = { target = root.capital_county value > 2000000 }
						}
					}
				}
				modifier = { #Just too darn far away...
					factor = 5
					scope:target_kingdom = {
						capital_vassal.capital_vassal = {
							squared_distance = { target = root.capital_county value > 2500000 }
						}
					}
				}
				modifier = { #Slightly more likely to join when fighting for a Holy Site.
					factor = 0.85
					scope:target_kingdom = {
						any_in_de_jure_hierarchy = {
							tier = tier_barony
							continue = { tier > tier_barony }
							is_holy_site_of = scope:the_faith
						}
					}
				}
				ai_value_modifier = { #Raw Zeal
					ai_zeal = -1.0
				}
				modifier = { #Basic traits
					factor = 2
					has_trait = cynical
				}
				modifier = { #Basic traits
					factor = 0.5
					has_trait = zealous
				}
				modifier = { #Tier
					factor = 0.75
					highest_held_title_tier = tier_kingdom
				}
				modifier = { #Tier
					factor = 0.5
					highest_held_title_tier = tier_empire
				}
			}
		}
	}
}

#AI hidden event to have a holy order pledge to a Great Holy War during the preparation phase.
#Event is fired when the Pope calls for the Crusade.
#Requires a the_faith scope, defined by event.
great_holy_war.0090 = {
	hidden = yes
	scope = holy_order

	trigger = {
		scope:the_faith = { #Crusade still exists
			great_holy_war ?= {
				NOT = { has_pledged_attacker = root.leader }
			}
		}
	}

	immediate = {
		scope:the_faith = {
			great_holy_war = {
				pledge_attacker = root.leader
			}
		}
	}
}

#AI hidden event to counterpledge to a Great Holy War during the preparation phase.
#Event is fired when the Pope calls for the Crusade and when the target Kingdom is switched.
#Requires a target_faith scope, defined by event.
#Requires a the_faith scope, defined by event.
great_holy_war.0021 = {
	hidden = yes
	
	trigger = {
		is_ai = yes
		is_ruler = yes
		is_landed = yes
		is_at_war = no
		is_imprisoned = no
		highest_held_title_tier >= tier_county
		NOT = { has_trait = incapable }
		root.faith = scope:the_faith.great_holy_war.ghw_target_character.faith
		scope:the_faith = { #Crusade still exists
			great_holy_war ?= {
				NOT = { has_pledged_defender = root }
			}
			NOT = { exists = great_holy_war.ghw_war } #Must not have started already, only in preparation phase.
		}
	}

	immediate = {
		scope:the_faith.great_holy_war = {
			set_variable = {
				name = triple_attackers_strength
				value = {
					value = ghw_attackers_strength
					multiply = 3
				}
			}
			set_variable = {
				name = double_attackers_strength
				value = {
					value = ghw_attackers_strength
					multiply = 2
				}
			}
			set_variable = {
				name = one_and_a_half_attackers_strength
				value = {
					value = ghw_attackers_strength
					multiply = 1.5
				}
			}
			set_variable = {
				name = half_of_attackers_strength
				value = {
					value = ghw_attackers_strength
					multiply = 0.5
				}
			}
			set_variable = {
				name = quarter_of_attackers_strength
				value = {
					value = ghw_attackers_strength
					multiply = 0.25
				}
			}
		}
		random_list = {
			15 = { #Pledge.
				trigger = {
					OR = { # Either be independent, or have no liege above you of the attacking Faith, to avoid messy situations of vassals and lieges fighting against each other.
						top_liege = this
						any_liege_or_above = {
							count = all
							faith != scope:the_faith
						}
					}
				}
				scope:the_faith.great_holy_war = { pledge_defender = root }
			}
			85 = { #Do not pledge.
				modifier = { #More pledging happening at start of preparation phase.
					factor = 0.75
					NOT = { exists = scope:the_faith.great_holy_war.ghw_war }
					scope:the_faith.great_holy_war = { days_until_ghw_launch > 200 }
				}
				modifier = { #Less pledging happening after.
					factor = 1.5
					NOT = { exists = scope:the_faith.great_holy_war.ghw_war }
					scope:the_faith.great_holy_war = { days_until_ghw_launch < 100 }
				}
				modifier = { #More likely if war is close by.
					factor = 0.5
					scope:target_kingdom = {
						capital_vassal.capital_vassal = {
							squared_distance = { target = root.capital_county value < 25000 }
						}
					}
				}
				modifier = { #More likely if war is close by.
					factor = 0.75
					scope:target_kingdom = {
						capital_vassal.capital_vassal = {
							squared_distance = { target = root.capital_county value < 50000 }
						}
					}
				}
				modifier = { #More likely if war is close by.
					factor = 0.8
					scope:target_kingdom = {
						capital_vassal.capital_vassal = {
							squared_distance = { target = root.capital_county value < 800000 } # Roughly Rome to Portugal
						}
					}
				}
				modifier = { #Less likely if war is close by.
					factor = 1.75
					scope:target_kingdom = {
						capital_vassal.capital_vassal = {
							squared_distance = { target = root.capital_county value > 1000000 }
						}
					}
				}
				modifier = { #Less likely if war is close by.
					factor = 1.75
					scope:target_kingdom = {
						capital_vassal.capital_vassal = {
							squared_distance = { target = root.capital_county value > 1500000 }
						}
					}
				}
				modifier = { #Less likely if war is close by.
					factor = 2
					scope:target_kingdom = {
						capital_vassal.capital_vassal = {
							squared_distance = { target = root.capital_county value > 2000000 }
						}
					}
				}
				modifier = { #Just too darn far away...
					factor = 2.5
					scope:target_kingdom = {
						capital_vassal.capital_vassal = {
							squared_distance = { target = root.capital_county value > 2500000 }
						}
					}
				}
				modifier = { #Much less likely to happen if the war has started.
					factor = 1.75
					exists = scope:the_faith.great_holy_war.ghw_war
				}
				modifier = { #Don't get too crowded now.
					factor = 1.5
					scope:the_faith.great_holy_war = { ghw_defenders_strength > ghw_attackers_strength }
				}
				modifier = { #Don't get too crowded now.
					factor = 1.5
					scope:the_faith.great_holy_war = { ghw_defenders_strength > var:triple_attackers_strength }
				}
				modifier = { #Don't get too crowded now.
					factor = 1.5
					scope:the_faith.great_holy_war = { ghw_defenders_strength > var:double_attackers_strength }
				}
				modifier = { #Don't get too crowded now.
					factor = 1.25
					scope:the_faith.great_holy_war = { ghw_defenders_strength > var:one_and_a_half_attackers_strength }
				}
				modifier = { #Try to balance.
					factor = 0.35
					NOT = { exists = scope:the_faith.great_holy_war.ghw_war }
					scope:the_faith.great_holy_war = { ghw_defenders_strength < var:half_of_attackers_strength }
				}
				modifier = { #Try to balance.
					factor = 0.25
					NOT = { exists = scope:the_faith.great_holy_war.ghw_war }
					scope:the_faith.great_holy_war = { ghw_defenders_strength < var:quarter_of_attackers_strength }
				}
				modifier = { #Slightly more likely to join when fighting for a Holy Site.
					factor = 0.75
					scope:target_kingdom = {
						any_in_de_jure_hierarchy = {
							tier = tier_barony
							continue = { tier > tier_barony }
							is_holy_site_of = scope:target_faith
						}
					}
				}
				ai_value_modifier = { #Raw Zeal
					ai_zeal = -1.0
				}
				modifier = { #Basic traits
					factor = 2
					has_trait = cynical
				}
				modifier = { #Basic traits
					factor = 0.5
					has_trait = zealous
				}
				modifier = { #Tier
					factor = 0.75
					highest_held_title_tier = tier_kingdom
				}
				modifier = { #Tier
					factor = 0.5
					highest_held_title_tier = tier_empire
				}
			}
		}
	}
}

#AI hidden event to counterpledge to a Directed Great Holy War.
#Event is fired on_declaration.
#Requires a target_faith scope, defined in the cb.
#Requires a the_faith scope, defined in the cb.
great_holy_war.0093 = {
	hidden = yes
	
	trigger = {
		is_ai = yes
		is_ruler = yes
		is_landed = yes
		is_at_war = no
		is_imprisoned = no
		highest_held_title_tier >= tier_county
		NOT = { has_trait = incapable }
		root.faith = scope:target_faith
		scope:the_faith = { #Crusade still exists
			great_holy_war ?= {
				NOT = { has_pledged_defender = root }
			}
		}
	}

	immediate = {
		scope:the_faith.great_holy_war = {
			set_variable = {
				name = triple_attackers_strength
				value = {
					value = ghw_attackers_strength
					multiply = 3
				}
			}
			set_variable = {
				name = double_attackers_strength
				value = {
					value = ghw_attackers_strength
					multiply = 2
				}
			}
			set_variable = {
				name = one_and_a_half_attackers_strength
				value = {
					value = ghw_attackers_strength
					multiply = 1.5
				}
			}
			set_variable = {
				name = half_of_attackers_strength
				value = {
					value = ghw_attackers_strength
					multiply = 0.5
				}
			}
			set_variable = {
				name = quarter_of_attackers_strength
				value = {
					value = ghw_attackers_strength
					multiply = 0.25
				}
			}
		}
		random_list = {
			30 = { #Pledge.
				trigger = {
					OR = { # Either be independent, or have no liege above you of the attacking Faith, to avoid messy situations of vassals and lieges fighting against each other.
						top_liege = this
						any_liege_or_above = {
							count = all
							faith != scope:the_faith
						}
					}
				}
				scope:the_faith.great_holy_war = { pledge_defender = root }
			}
			70 = { #Do not pledge.
				modifier = { #More likely if war is close by.
					factor = 0.5
					scope:target_kingdom = {
						capital_vassal.capital_vassal = {
							squared_distance = { target = root.capital_county value < 25000 }
						}
					}
				}
				modifier = { #More likely if war is close by.
					factor = 0.75
					scope:target_kingdom = {
						capital_vassal.capital_vassal = {
							squared_distance = { target = root.capital_county value < 50000 }
						}
					}
				}
				modifier = { #More likely if war is close by.
					factor = 0.8
					scope:target_kingdom = {
						capital_vassal.capital_vassal = {
							squared_distance = { target = root.capital_county value < 800000 } # Roughly Rome to Portugal
						}
					}
				}
				modifier = { #Less likely if war is close by.
					factor = 2
					scope:target_kingdom = {
						capital_vassal.capital_vassal = {
							squared_distance = { target = root.capital_county value > 1000000 }
						}
					}
				}
				modifier = { #Less likely if war is close by.
					factor = 5
					scope:target_kingdom = {
						capital_vassal.capital_vassal = {
							squared_distance = { target = root.capital_county value > 1500000 }
						}
					}
				}
				modifier = { #Less likely if war is close by.
					factor = 6
					scope:target_kingdom = {
						capital_vassal.capital_vassal = {
							squared_distance = { target = root.capital_county value > 2000000 }
						}
					}
				}
				modifier = { #Just too darn far away...
					factor = 10
					scope:target_kingdom = {
						capital_vassal.capital_vassal = {
							squared_distance = { target = root.capital_county value > 2500000 }
						}
					}
				}
				modifier = { #Try to balance.
					factor = 0.35
					NOT = { exists = scope:the_faith.great_holy_war.ghw_war }
					scope:the_faith.great_holy_war = { ghw_defenders_strength < var:half_of_attackers_strength }
				}
				modifier = { #Try to balance.
					factor = 0.25
					NOT = { exists = scope:the_faith.great_holy_war.ghw_war }
					scope:the_faith.great_holy_war = { ghw_defenders_strength < var:quarter_of_attackers_strength }
				}
				modifier = { #Slightly more likely to join when fighting for a Holy Site.
					factor = 0.75
					scope:target_kingdom = {
						any_in_de_jure_hierarchy = {
							tier = tier_barony
							continue = { tier > tier_barony }
							is_holy_site_of = scope:target_faith
						}
					}
				}
				ai_value_modifier = { #Raw Zeal
					ai_zeal = -1.0
				}
				modifier = { #Basic traits
					factor = 2
					has_trait = cynical
				}
				modifier = { #Basic traits
					factor = 0.5
					has_trait = zealous
				}
				modifier = { #Tier
					factor = 0.75
					highest_held_title_tier = tier_kingdom
				}
				modifier = { #Tier
					factor = 0.5
					highest_held_title_tier = tier_empire
				}
			}
		}
	}
}

#AI hidden event to have a holy order counterpledge to a Great Holy War during the preparation phase.
#Event is fired when the Pope calls for the Crusade and when the target Kingdom is switched.
#Requires a the_faith scope, defined by event.
great_holy_war.0091 = {
	hidden = yes
	scope = holy_order

	trigger = {
		scope:the_faith = { #Crusade still exists
			great_holy_war ?= {
				NOT = { has_pledged_defender = root.leader }
			}
			NOT = { exists = scope:the_faith.great_holy_war.ghw_war } #Must not have started already, only in preparation phase.
		}
	}

	immediate = {
		scope:the_faith.great_holy_war = {
			pledge_defender = root.leader
		}
	}
}


great_holy_war.0022 = {
	hidden = yes
	scope = faith
	
	trigger = {
		exists = great_holy_war
		NOT = { exists = great_holy_war.ghw_war }
		great_holy_war = { days_until_ghw_launch <= 200 }
		great_holy_war = { days_until_ghw_launch >= 150 }
		save_temporary_scope_as = the_faith
		any_player = {
			ghw_pope_annoyed_at_lack_of_participation_trigger = yes
			ghw_pope_annoyed_at_lack_of_participation_tier_trigger = yes
			NOT = { has_character_flag = ghw_pope_poking }
			is_landless_adventurer = no
		}
	}

	immediate = {
		save_scope_as = the_faith

		if = {
			limit = { exists = great_holy_war.ghw_target_character }
			great_holy_war.ghw_target_character = { save_scope_as = target_character }
		}
		every_player = {
			limit = {
				ghw_pope_annoyed_at_lack_of_participation_trigger = yes
				ghw_pope_annoyed_at_lack_of_participation_tier_trigger = yes
				NOT = { has_character_flag = ghw_pope_poking }
				is_landless_adventurer = no
			}
			trigger_event = {
				id = great_holy_war.0023
				days = { 25 50 }
			}
		}
	}
}

#Actual event letter to players.
great_holy_war.0023 = {
	type = letter_event
	opening = { desc = great_holy_war.0015.opening }
	desc = {
		first_valid = { #Regular letter
			triggered_desc = {
				trigger = {
					NOT = {
						scope:scoped_pope = {
							has_hook_of_type = {
								target = root
								type = favor_hook
							}
						}
					}
				}
				desc = great_holy_war.0023.desc
			}
		}
		first_valid = { #Hooked player
			triggered_desc = {
				trigger = {
					scope:scoped_pope = {
						has_hook_of_type = {
							target = root
							type = favor_hook
						}
					}
				}
				desc = great_holy_war.0023.desc.hooked
			}
		}
	}
	sender = scope:scoped_pope
	
	trigger = {
		NOT = { exists = scope:the_faith.great_holy_war.ghw_war }
		ghw_pope_annoyed_at_lack_of_participation_trigger = yes
		ghw_pope_annoyed_at_lack_of_participation_tier_trigger = yes
		is_landless_adventurer = no
	}

	immediate = {
		if = { #If the Pope died in the meantime.
			limit = {
				NOR = {
					exists = scope:the_faith.great_holy_war.ghw_war_declarer
					scope:the_faith.great_holy_war.ghw_war_declarer = { is_alive = yes }
				}
			}
			scope:the_faith = { ghw_set_new_war_declarer_effect = yes }
		}
		scope:the_faith.great_holy_war.ghw_war_declarer = { save_scope_as = scoped_pope }
		add_character_flag = { #Do not fire again.
			flag = ghw_pope_poking
			days = 200
		}
		set_variable = {
			name = ghw_pledge_amount
			value = great_holy_war_pledge_amount
		}
		set_variable = {
			name = half_great_holy_war_pledge_amount
			value = {
				value = root.great_holy_war_pledge_amount
				multiply = 0.5
			}
		}
	}
	
	option = {
		name = {
			trigger = { 				
				NOT = {
					scope:scoped_pope = {
						has_hook_of_type = {
							target = root
							type = favor_hook
						}
					}
				}
			}
			text = great_holy_war.0003.a
		}
		name = {
			trigger = {
				scope:scoped_pope = {
					has_hook_of_type = {
						target = root
						type = favor_hook
					}
				}
			}
			text = great_holy_war.0003.a.hooked
		}
		trigger = {
			ghw_pope_annoyed_at_lack_of_participation_trigger = yes
		}
		if = {
			limit = {
				scope:scoped_pope = {
					has_hook_of_type = {
						target = root
						type = favor_hook
					}
				}
			}
			scope:scoped_pope = {
				use_hook = root
			}
			add_character_flag = {
				flag = variable_ghw_papal_hooked_pledge
				days = 500
			}
		}
		if = {
			limit = {
				NOT = {
					scope:the_faith.great_holy_war = { has_pledged_attacker = root }
				}
			}
			pledge_ghw_piety_gain_effect = yes
			#Excommunication lifted.
			if = {
				limit = {
					has_trait = excommunicated
					faith = { has_doctrine_parameter = excommunication_active }
				}
				custom_tooltip = great_holy_war.0003.a.lifted_excommunication
				trigger_event = {
					id = great_holy_war.0015
					days = 37
				}
			}			
			scope:the_faith.great_holy_war = {
				pledge_attacker = root
				pledge_ghw_war_chest_prestige_effect = yes
				pledge_ghw_war_chest_piety_effect = yes
			}
		}
		stress_impact = {
			cynical = minor_stress_impact_gain
			zealous = minor_stress_impact_loss
		}

		ai_chance = {
			base = 10
			modifier = {
				factor = 0.5
				top_liege != this
			}
			modifier = {
				factor = 0.5
				highest_held_title_tier = tier_county
			}
			modifier = {
				factor = 0.1
				top_liege != this
				highest_held_title_tier <= tier_county
			}
			modifier = {
				factor = 5
				highest_held_title_tier >= tier_kingdom
			}
		}
	}
	
	option = {
		name = great_holy_war.0003.b
		trigger = {
			NOT = {
				scope:scoped_pope = {
					has_hook_of_type = {
						target = root
						type = favor_hook
					}
				}
			}
		}
		add_piety = root.var:half_great_holy_war_pledge_amount
		#Excommunication lifted.
		if = {
			limit = {
				has_trait = excommunicated
				faith = { has_doctrine_parameter = excommunication_active }
			}
			trigger_event = {
				id = great_holy_war.0015
				days = 37
			}
		}
		remove_short_term_gold = var:ghw_pledge_amount
		set_variable = {
			name = ghw_war_chest_donated_sum
			value = var:ghw_pledge_amount
		}
		scope:the_faith.great_holy_war = {
			change_war_chest_gold = root.var:ghw_pledge_amount
		}
		if = {
			limit = {
				NOT = { has_character_flag = ghw_pledged_gold }
			}
			add_character_flag = { #Used to check whether the Pope will harass for more later.
				flag = ghw_pledged_gold
				days = 550
			}
		}
		stress_impact = {
			cynical = minor_stress_impact_gain
			greedy = minor_stress_impact_gain
			generous = minor_stress_impact_loss
			zealous = minor_stress_impact_loss
		}
		ai_chance = {
			base = 10
		}
	}
	option = {
		name = great_holy_war.0023.c
		#But I'm already fighting infidels in another war!
		trigger = {
			is_at_war = yes
			NOR = {
				any_war_enemy = { faith = scope:the_faith }
				scope:scoped_pope = {
					has_hook_of_type = {
						target = root
						type = favor_hook
					}
				}
			}
		}
		ai_chance = {
			base = 0
		}
	}
	option = { #No excuses.
		name = great_holy_war.0023.d
		trigger = {
			OR = {
				is_at_war = no
				NOT = {
					any_war_enemy = {
						faith != scope:the_faith
					}
				}
			}
			NOT = {
				scope:scoped_pope = {
					has_hook_of_type = {
						target = root
						type = favor_hook
					}
				}
			}
		}
		#Piety price
		pledge_ghw_piety_loss_effect = yes
		add_piety_level = -1
		#Set Cooldown
		set_variable = {
			name = variable_ghw_unpledged_cooldown
			value = yes
			days = 60
		}
		#Religious Head is upset
		trigger_event = {
			id = great_holy_war.0016
			days = 37
		}
		stress_impact = {
			zealous = minor_stress_impact_gain
			cynical = minor_stress_impact_loss
		}
		ai_chance = {
			base = 0
		}
	}
	after = {
		remove_variable = ghw_pledge_amount
	}
}

#On_death: of Recipient, pick another one.
great_holy_war.0024 = {
	hidden = yes
	scope = character
	
	trigger = {
		exists = faith.great_holy_war
		faith.great_holy_war.ghw_designated_winner = root
	}

	immediate = {
		save_scope_as = former_recipient
		faith.great_holy_war = {
			faith = { save_scope_as = the_faith }
			ghw_target_title = { save_scope_as = target_kingdom }
			if = { #If Pope died in the meantime.
				limit = {
					NOR = {
						exists = scope:the_faith.great_holy_war.ghw_war_declarer
						scope:the_faith.great_holy_war.ghw_war_declarer = { is_alive = yes }
					}
				}
				scope:the_faith = { ghw_set_new_war_declarer_effect = yes }
			}
			scope:the_faith.great_holy_war.ghw_war_declarer = { save_scope_as = ghw_sponsor }
			#Check and set sponsor if needed.
			undirected_ghw_recipient_selection_on_death_effect = yes
		}
		every_player = {
			limit = {
				scope:the_faith.great_holy_war = {
					OR = {
						has_pledged_attacker = prev
						has_pledged_defender = prev
					}
				}
			}
			if = {
				limit = { exists = scope:ghw_claimant }
				send_interface_toast = {
					type = event_toast_effect_bad
					left_icon = scope:former_recipient
					right_icon = scope:ghw_claimant
					title = great_holy_war.0024.dead.recipient.title
					custom_tooltip = great_holy_war.0024.dead.recipient.replaced
				}
			}
			else = {
				send_interface_toast = {
					type = event_toast_effect_bad
					left_icon = scope:former_recipient
					title = great_holy_war.0024.dead.recipient.title
					custom_tooltip = great_holy_war.0024.dead.recipient.open
				}
			}
		}
	}
}

#on_character_faith_change: of Recipient, pick another one.
great_holy_war.0025 = {
	hidden = yes
	scope = character
	
	trigger = {
		exists = scope:old_faith.great_holy_war
		scope:old_faith.great_holy_war.ghw_designated_winner = root
	}

	immediate = {
		scope:old_faith.great_holy_war = {
			faith = { save_scope_as = the_faith }
			ghw_target_title = { save_scope_as = target_kingdom }
			if = { #If Pope died in the meantime.
				limit = {
					OR = {
						NOT = {
							exists = scope:the_faith.great_holy_war.ghw_war_declarer
						}
						NOT = {
							scope:the_faith.great_holy_war.ghw_war_declarer = { is_alive = yes }
						}
					}
				}
				scope:the_faith = { ghw_set_new_war_declarer_effect = yes }
			}
			scope:the_faith.great_holy_war.ghw_war_declarer = { save_scope_as = ghw_sponsor }
			#Check and set sponsor if needed.
			undirected_ghw_recipient_selection_effect = yes
		}
	}
}

#select Beneficiary for the AI automatically. Fired when it joins from great_holy_war.0020.
scripted_trigger great_holy_war_0026_valid_beneficiary_trigger = {
	NOR = {
		has_trait = excommunicated
		has_trait = incapable
		has_trait = devoted
		has_trait_with_flag = debilitating_illness
	}
	faith = root.faith
	is_adult = yes
	is_ruler = no
	any_heir_title = { count = 0 }	#Not heir to anything
	OR = { #Either not married, or not married to a ruler.
		is_married = no
		NOT = {
			any_spouse = {
				is_ruler = yes
			}
		}
	}
}

great_holy_war.0026 = {
	hidden = yes
	scope = character
	
	trigger = {
		exists = faith.great_holy_war
		faith.great_holy_war = {
			has_pledged_attacker = root
		}
	}

	immediate = {
		reset_beneficiary = yes #Reset by default, then, if also an AI, reassign automatically.
		if = {
			limit = { #If potential new beneficiary exists, pick one.
				is_lowborn = no
				dynasty = {
					any_dynasty_member = {
						great_holy_war_0026_valid_beneficiary_trigger = yes
					}
				}
			}
			dynasty = {
				random_dynasty_member = {
					limit = {
						great_holy_war_0026_valid_beneficiary_trigger = yes					
						OR = {
							AND = {
								is_male = yes
								faith = { has_doctrine = doctrine_gender_male_dominated }
							}
							AND = {
								is_female = yes
								faith = { has_doctrine = doctrine_gender_female_dominated }
							}
							NOR = {
								faith = { has_doctrine = doctrine_gender_male_dominated }
								faith = { has_doctrine = doctrine_gender_female_dominated }
							}
						}
						is_close_family_or_spouse_of_root_trigger = yes
						age <= 40
						NOT = { has_relation_rival = root }
						is_married = no
						culture = root.culture
					}
					alternative_limit = {
						great_holy_war_0026_valid_beneficiary_trigger = yes
						OR = {
							AND = {
								is_male = yes
								faith = { has_doctrine = doctrine_gender_male_dominated }
							}
							AND = {
								is_female = yes
								faith = { has_doctrine = doctrine_gender_female_dominated }
							}
							NOR = {
								faith = { has_doctrine = doctrine_gender_male_dominated }
								faith = { has_doctrine = doctrine_gender_female_dominated }
							}
						}
						is_close_family_or_spouse_of_root_trigger = yes
						age <= 40
						NOT = { has_relation_rival = root }
						is_married = no
					}
					alternative_limit = {
						great_holy_war_0026_valid_beneficiary_trigger = yes
						OR = {
							AND = {
								is_male = yes
								faith = { has_doctrine = doctrine_gender_male_dominated }
							}
							AND = {
								is_female = yes
								faith = { has_doctrine = doctrine_gender_female_dominated }
							}
							NOR = {
								faith = { has_doctrine = doctrine_gender_male_dominated }
								faith = { has_doctrine = doctrine_gender_female_dominated }
							}
						}
						is_close_family_or_spouse_of_root_trigger = yes
						age <= 40
						NOT = { has_relation_rival = root }
					}
					alternative_limit = {
						great_holy_war_0026_valid_beneficiary_trigger = yes
						OR = {
							AND = {
								is_male = yes
								faith = { has_doctrine = doctrine_gender_male_dominated }
							}
							AND = {
								is_female = yes
								faith = { has_doctrine = doctrine_gender_female_dominated }
							}
							NOR = {
								faith = { has_doctrine = doctrine_gender_male_dominated }
								faith = { has_doctrine = doctrine_gender_female_dominated }
							}
						}
						age <= 40
					}
					alternative_limit = {
						great_holy_war_0026_valid_beneficiary_trigger = yes
						OR = {
							AND = {
								is_male = yes
								faith = { has_doctrine = doctrine_gender_male_dominated }
							}
							AND = {
								is_female = yes
								faith = { has_doctrine = doctrine_gender_female_dominated }
							}
							NOR = {
								faith = { has_doctrine = doctrine_gender_male_dominated }
								faith = { has_doctrine = doctrine_gender_female_dominated }
							}
						}
					}
					alternative_limit = {
						great_holy_war_0026_valid_beneficiary_trigger = yes
					}
					weight = {
						base = 1
						opinion_modifier = { # Nepotism
							opinion_target = this
							who = root
						}
						modifier = { # More likely to send a soldier into enemy territory.
							add = 25
							has_trait = education_martial
						}
						modifier = { # Skill
							add = 15
							OR = {
								diplomacy >= 12
								martial >= 12
								stewardship >= 12
								intrigue >= 12
								learning >= 12
							}
						}
						modifier = { #Zeal
							add = 5
							has_strong_religious_conviction_trigger = yes
						}
						modifier = { #Virtues
							add = 2
							num_virtuous_traits >= 2
						}
					}
					save_scope_as = scoped_beneficiary
				}
			}
			set_beneficiary = scope:scoped_beneficiary
		}
		else = { #Else reset.
			reset_beneficiary = yes
		}
	}
}

#Same as above, but fired on death of a participant of a ghw, with different scopes.
great_holy_war.0027 = {
	hidden = yes
	scope = character

	trigger = {
		exists = scope:great_holy_war
		exists = scope:replacement.faith.great_holy_war
	}

	immediate = {
		scope:replacement = {
			if = {
				limit = {
					NOT = {
						scope:great_holy_war = { has_pledged_attacker = prev }
					}
				}
				scope:great_holy_war = { pledge_attacker = prev }
			}
			reset_beneficiary = yes #Reset by default, then, if also an AI, reassign automatically.
			if = {
				limit = { is_ai = yes }
				trigger_event = {
					id = great_holy_war.0026
					days = 2
				}
			}
		}
	}
}

#Resetting beneficiary when he becomes invalid for the spot, for whatever reason.
#On_death.
great_holy_war.0028 = {
	hidden = yes
	scope = character
	
	trigger = {
		exists = faith.great_holy_war
		faith.great_holy_war = {
			any_pledged_attacker = {
				ghw_beneficiary ?= root
			}
		}
	}

	immediate = {
		faith.great_holy_war = {
			every_pledged_attacker = {
				limit = {
					ghw_beneficiary ?= root
				}
				ghw_beneficiary = { save_scope_as = former_beneficiary }
				reset_beneficiary = yes #Reset by default, then, if also an AI, reassign automatically.
				if = {
					limit = { is_ai = no } #Send notification to player.
					send_interface_toast = {
						left_icon = root
						title = great_holy_war.0028.dead.beneficiary_title
						custom_tooltip = great_holy_war.0028.dead.beneficiary
					}
				}
				else = {
					trigger_event = {
						id = great_holy_war.0026
						days = 2
					}
				}
			}
		}
	}
}

#On_character_faith_change.
great_holy_war.0029 = {
	hidden = yes
	scope = character
	
	trigger = {
		exists = scope:old_faith.great_holy_war
		scope:old_faith.great_holy_war = {
			any_pledged_attacker = {
				ghw_beneficiary ?= root
			}
		}
	}

	immediate = {
		scope:old_faith.great_holy_war = {
			every_pledged_attacker = {
				limit = {
					ghw_beneficiary ?= root
				}
				ghw_beneficiary = { save_scope_as = former_beneficiary }
				reset_beneficiary = yes #Reset by default, then, if also an AI, reassign automatically.
				if = {
					limit = { is_ai = no } #Send notification to player.
					send_interface_toast = {
						left_icon = root
						title =  great_holy_war.0029.invalid.beneficiary_title
						custom_tooltip = great_holy_war.0029.converted.beneficiary
					}
				}
				else = {
					trigger_event = {
						id = great_holy_war.0026
						days = 2
					}
				}
			}
		}
	}
}

#All other conditions checked on_faith_monthly.

#If sick
#If incapable
#If monk/nun
#If Excommunicated
#If ruler
#If heir

great_holy_war.0030 = {
	hidden = yes
	scope = faith
	
	trigger = {
		great_holy_war ?= {
			any_pledged_attacker = { #If beneficiary has become invalid in the meantime, for whatever reason.
				ghw_beneficiary ?= {
					OR = {
						has_trait = incapable
						has_trait = excommunicated
						has_trait = devoted
						has_trait_with_flag = debilitating_illness
						is_ruler = yes
						any_heir_title = { }	#Shouldn't be heir to anything
					}
				}
			}
		}
	}

	immediate = {
		great_holy_war = {
			every_pledged_attacker = {
				limit = {
					ghw_beneficiary ?= {
						OR = {
							has_trait = incapable
							has_trait = excommunicated
							has_trait = devoted
							has_trait_with_flag = debilitating_illness
							AND = {
								is_ruler = yes
								NOT = { government_has_flag = government_is_landless_adventurer }
							}
							any_heir_title = { }	#Shouldn't be heir to anything
						}
					}
				}
				ghw_beneficiary = {
					save_scope_as = former_beneficiary
				}
				reset_beneficiary = yes #Reset by default, then, if also an AI, reassign automatically.
				if = {
					limit = {
						is_ai = no  #Send notification to player.
						scope:former_beneficiary = {
							OR = {
								has_trait = incapable
								has_trait = excommunicated
								has_trait = devoted
								has_trait_with_flag = debilitating_illness
								is_ruler = yes
								any_heir_title = { }	#Shouldn't be heir to anything
							}
						}
					}
					send_interface_toast = {
						type = event_toast_effect_bad
						left_icon = root
						title = great_holy_war.0029.invalid.beneficiary_title
						scope:former_beneficiary = {
							if = {
								limit = { has_trait = incapable }
								custom_tooltip = great_holy_war.0030.incapable.beneficiary
							}
							else_if = {
								limit = { has_trait = excommunicated }
								custom_tooltip = great_holy_war.0030.excommunicated.beneficiary
							}
							else_if = {
								limit = { has_trait = devoted }
								custom_tooltip = great_holy_war.0030.devoted.beneficiary
							}
							else_if = {
								limit = { has_trait_with_flag = debilitating_illness }
								custom_tooltip = great_holy_war.0030.sick.beneficiary
							}
							else_if = {
								limit = { is_ruler = yes }
								custom_tooltip = great_holy_war.0030.ruler.beneficiary
							}
							else_if = {
								limit = { any_heir_title = { } } #Shouldn't be heir to anything
								custom_tooltip = great_holy_war.0030.heir.beneficiary
							}
						}
					}
				}
				else_if = {
					limit = { is_ai = yes }
					trigger_event = {
						id = great_holy_war.0026
						days = 2
					}
				}
			}
		}
	}
}

# Player wishes to give money to the Crusade.
great_holy_war.0040 = {
	type = character_event
	title = great_holy_war.0003.title
	desc = great_holy_war.0040.desc
	theme = faith
	left_portrait = root

	immediate = {
		add_character_flag = { # Do not spam button.
			flag = ghw_pledging_gold
			days = 550
		}
		set_variable = {
			name = double_great_holy_war_pledge_amount
			value = {
				value = root.great_holy_war_pledge_amount
				multiply = 2
			}
		}
		set_variable = {
			name = triple_great_holy_war_pledge_amount
			value = {
				value = root.great_holy_war_pledge_amount
				multiply = 3
			}
		}

		#Set halves of above to give as Piety.

		set_variable = {
			name = half_great_holy_war_pledge_amount
			value = {
				value = root.great_holy_war_pledge_amount
				multiply = 0.5
			}
		}
		set_variable = {
			name = half_of_triple_great_holy_war_pledge_amount
			value = {
				value = root.var:triple_great_holy_war_pledge_amount
				multiply = 0.5
			}
		}

		if = {
			limit = {
				NOR = {
					exists = faith.great_holy_war.ghw_war_declarer
					faith.great_holy_war.ghw_war_declarer = { is_alive = yes }
				}
			}
			faith = { ghw_set_new_war_declarer_effect = yes }
		}
		root.faith.great_holy_war.ghw_war_declarer = { save_scope_as = scoped_pope }
	}
	
	option = { # Big donation.
		name = great_holy_war.0040.a
		trigger = { gold >= var:triple_great_holy_war_pledge_amount }
		add_piety = var:half_of_triple_great_holy_war_pledge_amount
		remove_short_term_gold = var:triple_great_holy_war_pledge_amount
		set_variable = {
			name = ghw_war_chest_donated_sum
			value = var:triple_great_holy_war_pledge_amount
		}
		scope:great_holy_war = {
			change_war_chest_gold = root.var:triple_great_holy_war_pledge_amount
		}
		scope:scoped_pope = {
			add_opinion = {
				target = root
				modifier = donated_ghw_money_2
			}
		}
		add_character_flag = { # Used to check whether the Pope will harass for more later.
			flag = ghw_pledged_gold
			days = 7300
		}
		stress_impact = {
			cynical = minor_stress_impact_gain
			greedy = medium_stress_impact_gain
		}
	}
	option = { # Medium donation.
		name = great_holy_war.0040.b
		trigger = { gold >= var:double_great_holy_war_pledge_amount }
		add_piety = root.great_holy_war_pledge_amount
		remove_short_term_gold = var:double_great_holy_war_pledge_amount
		set_variable = {
			name = ghw_war_chest_donated_sum
			value = var:double_great_holy_war_pledge_amount
		}

		scope:great_holy_war = { change_war_chest_gold = root.var:double_great_holy_war_pledge_amount }
		scope:scoped_pope = {
			add_opinion = {
				target = root
				modifier = donated_ghw_money_1
			}
		}
		add_character_flag = { # Used to check whether the Pope will harass for more later.
			flag = ghw_pledged_gold
			days = 7300
		}
		stress_impact = {
			cynical = minor_stress_impact_gain
			greedy = medium_stress_impact_gain
		}
	}
	option = { # Small donation.
		name = great_holy_war.0040.c
		trigger = { gold >= great_holy_war_pledge_amount }
		add_piety = var:half_great_holy_war_pledge_amount
		remove_short_term_gold = great_holy_war_pledge_amount
		set_variable = {
			name = ghw_war_chest_donated_sum
			value = great_holy_war_pledge_amount
		}
		scope:great_holy_war = {
			change_war_chest_gold = root.great_holy_war_pledge_amount
		}
		add_character_flag = { # Used to check whether the Pope will harass for more later.
			flag = ghw_pledged_gold
			days = 7300
		}
	}

	option = { #Never mind...
		name = great_holy_war.0040.d
	}
	after = {
		remove_character_flag = ghw_pledging_gold
		remove_variable = double_great_holy_war_pledge_amount
		remove_variable = triple_great_holy_war_pledge_amount
		remove_variable = half_great_holy_war_pledge_amount
		remove_variable = half_of_triple_great_holy_war_pledge_amount
	}
}

#great_holy_war.0050 Crusade preparation phase starts (defender) (fired from great_holy_war.0001)
#great_holy_war.0051 Crusade is declared (attackers/defenders) (fired from cb)
#great_holy_war.0052 crusade was won (attackers/defenders/bystanders) (fired from cb)
#great_holy_war.0053 crusade was lost (attackers/defenders/bystanders) (fired from cb)
#great_holy_war.0055 crusade failed to start (attackers/defenders/bystanders) (fired from on_great_holy_war_invalidation)

#Rulers of target faith are informed of the start of the preparation phase of the GHW against them.
great_holy_war.0050 = {
	title = great_holy_war.0050.title
	desc = {
		desc = great_holy_war.0050.base.desc
		first_valid = {
			#Defender.
			triggered_desc = {
				desc = great_holy_war.0050.defender.desc
				trigger = {
					scope:target_character = root
				}
			}
			#Everybody else.
			triggered_desc = {
				desc = great_holy_war.0050.others.desc
				trigger = {
					scope:target_character != root
				}
			}
		}
	}
	left_portrait = {
		character = scope:ghw_sponsor
		animation = war_attacker
	}
	lower_right_portrait = scope:target_character

	theme = faith
	override_background = {
		reference = armory
	}
	
	immediate = {
		scope:ghw_sponsor = {
			save_scope_as = background_temple_scope
		}
		play_music_cue = "mx_cue_combat_2"
	}

	option = {
		name = great_holy_war.0050.a
	}
}

#On_declaration of the GHW.
great_holy_war.0051 = {
	title = great_holy_war.0050.title
	desc = {
		first_valid = {
			#Catholic Flavor - Attackers
			triggered_desc = {
				trigger = {
					scope:attacker.faith.great_holy_war = {
						has_pledged_attacker = root
					}
					scope:the_faith = faith:catholic
				}
				desc = great_holy_war.0051.catholic.attackers.desc
			}
			#Everybody else's flavor - Attackers.
			triggered_desc = {
				trigger = {
					scope:attacker.faith.great_holy_war = {
						has_pledged_attacker = root
					}
					NOT = { scope:the_faith = faith:catholic }
				}
				desc = great_holy_war.0051.generic.attackers.desc
			}
			#Everybody else's flavor - Defenders.
			triggered_desc = {
				trigger = {
					scope:attacker.faith.great_holy_war = {
						has_pledged_defender = root
					}
				}
				desc = great_holy_war.0051.generic.defenders.desc
			}
		}
	}
	
	theme = faith
	override_background = { reference = temple }

	left_portrait = {
		character = scope:ghw_sponsor
		animation = war_attacker
	}
	lower_right_portrait = {
		character = scope:ghw_defender
		animation = war_defender
	}
	
	immediate = {
		show_as_tooltip = {
			scope:attacker.faith.great_holy_war = {
				divide_war_chest = { fraction = 0.2 }
			}
		}
		play_music_cue = "mx_cue_combat_3"
	}

	option = {
		name = great_holy_war.0011.a
		trigger = {
			scope:attacker.faith.great_holy_war = { has_pledged_attacker = root }
			OR = { root.faith.religion = religion:eastern_orthodox_religion root.faith.religion = religion:catholic_religion root.faith.religion = religion:protestant_religion }
		}

		custom_tooltip = great_holy_war.0051.generic.start.tt
	}
	option = {
		name = great_holy_war.0011.b
		trigger = {
			scope:attacker.faith.great_holy_war = { has_pledged_attacker = root }
			root.faith.religion = religion:islam_religion
		}

		custom_tooltip = great_holy_war.0051.generic.start.tt
	}
	option = {
		name = great_holy_war.0011.c
		trigger = {
			scope:attacker.faith.great_holy_war = { has_pledged_attacker = root }
			NOR = {
				OR = { root.faith.religion = religion:eastern_orthodox_religion root.faith.religion = religion:catholic_religion root.faith.religion = religion:protestant_religion }
				root.faith.religion = religion:islam_religion
			}
		}

		custom_tooltip = great_holy_war.0051.generic.start.tt
	}
	option = {
		name = great_holy_war.0011.d
		trigger = {
			scope:attacker.faith.great_holy_war = { has_pledged_defender = root }
		}

		custom_tooltip = great_holy_war.0051.generic.start.tt
	}
}

#On_declaration of the GHW.
great_holy_war.0071 = {
	title = great_holy_war.0050.title
	desc = {
		first_valid = {
			#Everybody else's flavor - Attackers.
			triggered_desc = {
				trigger = {
					OR = {
						faith = scope:attacker.faith
						is_vassal_of = scope:attacker
					}
				}
				desc = great_holy_war.0071.generic.attackers.desc
			}
			#Everybody else's flavor - Defenders.
			triggered_desc = {
				trigger = {
					OR = {
						faith = scope:defender.faith
						is_vassal_of = scope:defender
					}
				}
				desc = great_holy_war.0071.generic.defenders.desc
			}
		}
	}
	theme = faith
	override_background = {
		reference = armory
	}
	right_portrait = {
		character = scope:ghw_sponsor
		animation = war_attacker
	}
	left_portrait = {
		character = scope:ghw_defender
		animation = war_defender
	}

	
	option = { #Join directed GHW as attacker.
		name = great_holy_war.0011.directed.a
		trigger = {
			is_ai = no
			is_at_war = no
			faith = scope:attacker.faith
			NOR = {
				this = scope:attacker
				scope:attacker.faith.great_holy_war = { has_pledged_attacker = root }
				scope:attacker.faith.great_holy_war = { has_pledged_defender = root }
			}
		}

		custom_tooltip = great_holy_war.0051.generic.start.tt
		scope:attacker.faith.great_holy_war = { pledge_attacker = root }
	}
	
	option = { #Join directed GHW as defender.
		name = great_holy_war.0011.directed.b
		trigger = {
			is_ai = no
			is_at_war = no
			faith = scope:defender.faith
			NOR = {
				this = scope:defender
				scope:attacker.faith.great_holy_war = { has_pledged_defender = root }
				scope:attacker.faith.great_holy_war = { has_pledged_defender = root }
			}
		}

		custom_tooltip = great_holy_war.0051.generic.start.tt
		scope:attacker.faith.great_holy_war = { pledge_defender = root }
	}

	option = {
		name = great_holy_war.0011.a
		trigger = {
			scope:attacker.faith.great_holy_war = { has_pledged_attacker = root }
			OR = { root.faith.religion = religion:eastern_orthodox_religion root.faith.religion = religion:catholic_religion root.faith.religion = religion:protestant_religion }
		}

		custom_tooltip = great_holy_war.0051.generic.start.tt
	}
	option = {
		name = great_holy_war.0011.b
		trigger = {
			scope:attacker.faith.great_holy_war = { has_pledged_attacker = root }
			root.faith.religion = religion:islam_religion
		}

		custom_tooltip = great_holy_war.0051.generic.start.tt
	}
	option = {
		name = great_holy_war.0011.c
		trigger = {
			scope:attacker.faith.great_holy_war = { has_pledged_attacker = root }
			NOR = {
				OR = { root.faith.religion = religion:eastern_orthodox_religion root.faith.religion = religion:catholic_religion root.faith.religion = religion:protestant_religion }
				root.faith.religion = religion:islam_religion
			}
		}

		custom_tooltip = great_holy_war.0051.generic.start.tt
	}
	option = {
		name = great_holy_war.0011.d
		trigger = {
			scope:attacker.faith.great_holy_war = { has_pledged_defender = root }
		}

		custom_tooltip = great_holy_war.0051.generic.start.tt
	}
	option = {
		name = great_holy_war.0050.a
		trigger = {
			scope:attacker.faith != root.faith
		}
		fallback = yes

		custom_tooltip = great_holy_war.0051.generic.start.tt
	}
}

#The Crusade was successful.
great_holy_war.0052 = {
	title = {
		first_valid = { #Title for winners/bystanders
			triggered_desc = {
				trigger = {
					scope:ghw_defender.faith.religion != root.faith.religion
				}
				desc = great_holy_war.0052.title.winners
			}
		}
		first_valid = { #Title for losers
			triggered_desc = {
				trigger = { scope:ghw_defender.faith.religion = root.faith.religion }
				desc = great_holy_war.0052.title.losers
			}
		}
	}
	desc = {
		first_valid = {
			#Everybody else's flavor - Attackers.
			triggered_desc = {
				trigger = { scope:attacker.faith = root.faith }
				desc = great_holy_war.0052.generic.attackers.desc
			}
			#Everybody else's flavor - Defenders.
			triggered_desc = {
				trigger = { scope:ghw_defender.faith.religion = root.faith.religion }
				desc = great_holy_war.0052.generic.defenders.desc
			}
			#Other players.
			triggered_desc = {
				trigger = {
					NOR = {
						scope:attacker.faith = root.faith
						scope:ghw_defender.faith.religion = root.faith.religion
					}
				}
				desc = great_holy_war.0052.generic.outsiders.desc
			}
		}
	}
	theme = faith
	override_background = {
		reference = battlefield
	}
	left_portrait = {
		character = scope:title_recipient
		animation = war_over_win
	}
	right_portrait = {
		character = scope:ghw_defender
		animation = shame
	}
	
	immediate = {
		play_music_cue = "mx_cue_general_crusade_event_3"
		if = {
			limit = {
				has_trait = faith_warrior
				save_temporary_scope_as = char_temp
				scope:attacker.faith.great_holy_war = {
					any_pledged_attacker = { this = scope:char_temp }
				}
			}
			absolve_crusaders_of_gallowsbait_effect = yes
		}
	}
	
	option = {
		name = great_holy_war.0052.a
		trigger = { scope:attacker.faith = root.faith }
	}
	
	option = {
		name = great_holy_war.0052.b
		trigger = { scope:ghw_defender.faith = root.faith }
	}
	
	option = {
		name = great_holy_war.0052.c
		trigger = {
			NOR = {
				scope:attacker.faith = root.faith
				scope:ghw_defender.faith = root.faith
			}
		}
	}

	after = { remove_variable = ghw_war_chest_donated_sum }
}

#The Crusade failed.
great_holy_war.0053 = {
	title = great_holy_war.0053.title
	desc = {
		first_valid = {
			#Everybody else's flavor - Attackers.
			triggered_desc = {
				trigger = { scope:attacker.faith = root.faith }
				desc = great_holy_war.0053.generic.attackers.desc
			}
			#Everybody else's flavor - Defenders.
			triggered_desc = {
				trigger = { scope:ghw_defender.faith.religion = root.faith.religion }
				desc = great_holy_war.0053.generic.defenders.desc
			}
			#Other players.
			triggered_desc = {
				trigger = {
					NOR = {
						scope:attacker.faith = root.faith
						scope:ghw_defender.faith.religion = root.faith.religion
					}
				}
				desc = great_holy_war.0053.generic.outsiders.desc
			}
		}
	}
	theme = faith
	override_background = {
		reference = battlefield
	}
	left_portrait = {
		character = scope:ghw_sponsor
		animation = shame
	}
	right_portrait = {
		character = scope:ghw_defender
		animation = war_over_win
	}

	immediate = {
		play_music_cue = "mx_cue_general_crusade_event_3"
	}

	option = {
		name = great_holy_war.0053.a
		trigger = { scope:attacker.faith = root.faith }
	}
	
	option = {
		name = great_holy_war.0053.b
		trigger = { scope:ghw_defender.faith.religion = root.faith.religion }
	}
	
	option = {
		name = great_holy_war.0052.c
		trigger = {
			NOR = {
				scope:attacker.faith = root.faith
				scope:ghw_defender.faith.religion = root.faith.religion
			}
		}
	}

	after = { remove_variable = ghw_war_chest_donated_sum }
}

#on_great_holy_war_invalidation: Crusade has been invalidated before starting.
great_holy_war.0055 = {
	hidden = yes
	scope = ghw
	
	immediate = {
		save_scope_as = the_ghw

		#Loc keys for localisation fluff.
		ghw_target_character = { save_scope_as = ghw_defender }
		faith = {
			religious_head = { save_scope_as = ghw_sponsor }
			save_scope_as = the_faith
			change_fervor = {
				value =  -10
				desc = fervor_loss_crusade_invalid
			}
			set_variable = {
				name = variable_ghw_cooldown
				value = yes
				years = 30
			}
		}
		every_player = {
			limit = {
				OR = {
					faith = scope:ghw_sponsor.faith
					faith = scope:ghw_defender.faith
				}
			}
			trigger_event = great_holy_war.0056
		}
	}
}

great_holy_war.0056 = {
	title = great_holy_war.0056.title
	desc = great_holy_war.0056.desc
	theme = faith
	override_background = {
		reference = battlefield
	}
	right_portrait = {
		character = scope:ghw_sponsor
		animation = shame
	}
	left_portrait = {
		character = scope:ghw_defender
		animation = war_over_win
	}

	
	option = {
		name = great_holy_war.0056.a
		trigger = { scope:ghw_sponsor.faith = root.faith }

		if = {
			limit = { has_variable = ghw_war_chest_donated_sum }
			add_gold = var:ghw_war_chest_donated_sum
			remove_variable = ghw_war_chest_donated_sum
		}
	}
	
	option = {
		name = great_holy_war.0056.b
		trigger = { scope:ghw_defender.faith = root.faith }
	}
	option = {
		name = great_holy_war.0056.c
		trigger = {
			NOR = {
				scope:ghw_sponsor.faith = root.faith
				scope:ghw_defender.faith = root.faith
			}
		}
	}
}

#Crusader trait gained during GHW.
#Fired on_army_enter_province
#Hidden first event fires for the owner of the army.
#root = owner of the army
#scope:army = the army itself.
great_holy_war.0060 = {
	hidden = yes
	
	trigger = {
		exists = root.faith.great_holy_war
		OR = { #Either army owner or one of his lieges is involved in a GHW.
			any_character_war = {
				OR = {
					using_cb = undirected_great_holy_war
					using_cb = directed_great_holy_war
				}
				is_attacker = root
			}
			any_liege_or_above = {
				faith = root.faith
				any_character_war = {
					OR = {
						using_cb = undirected_great_holy_war
						using_cb = directed_great_holy_war
					}
					is_attacker = root
				}
			}
		}
		scope:army = {
			exists = army_commander
			location = { save_temporary_scope_as = current_location }
			army_commander = {
				NOR = {
					has_trait = crusader_king
					has_trait = faith_warrior
				}
			}
		}
		root.faith.great_holy_war.ghw_target_title = { #The dejure Kingdom targeted by the GHW
			any_in_de_jure_hierarchy = {
				tier = tier_barony
				title_province = { this = scope:current_location } #Any province within it.
				holder ?= { #The province's owner must also be part of the GHW on the opposite side.
					OR = {
						any_character_war = {
							OR = {
								using_cb = undirected_great_holy_war
								using_cb = directed_great_holy_war
							}
							is_defender = prev
						}
						any_liege_or_above = {
							any_character_war = {
								OR = {
									using_cb = undirected_great_holy_war
									using_cb = directed_great_holy_war
								}
								is_defender = prev
							}
						}
					}
				}
			}
		}
	}

	immediate = {
		scope:army.location = { save_scope_as = current_location }
		faith = { save_scope_as = the_faith }
		faith.great_holy_war = { save_scope_as = scoped_ghw }
		faith.great_holy_war.ghw_target_title = { save_scope_as = target_kingdom }
		scope:army = {
			army_commander = { trigger_event = great_holy_war.0057 }
			army_owner = {
				every_knight = {
					limit = {
						location = scope:current_location
					}
					trigger_event = great_holy_war.0057
				}
			}
		}
	}
}

great_holy_war.0057 = {
	title = great_holy_war.0057.title
	desc = {
		first_valid = {
			triggered_desc = { #Crusaders
				trigger = { OR = { faith.religion = religion:eastern_orthodox_religion faith.religion = religion:catholic_religion faith.religion = religion:protestant_religion } }
				desc = great_holy_war.0057.crusader.desc
			}
			triggered_desc = { #Muslims
				trigger = { faith.religion = religion:islam_religion }
				desc = great_holy_war.0057.mujahid.desc
			}
			triggered_desc = { #Everybody else
				trigger = {
					NOR = {
						OR = { faith.religion = religion:eastern_orthodox_religion faith.religion = religion:catholic_religion faith.religion = religion:protestant_religion }
						faith.religion = religion:islam_religion
					}
				}
				desc = great_holy_war.0057.generic.outsiders.desc
			}
		}
	}
	theme = faith
	override_background = {
		reference = wilderness_scope
	}
	left_portrait = {
		character = root
		animation = marshal_shield
	}
	
	trigger = {
		root.faith = scope:the_faith
		exists = faith.great_holy_war
		NOR = {
			has_trait = crusader_king
			has_trait = faith_warrior
		}
		OR = {
			is_commanding_army = yes
			is_knight = yes
		}
	}

	immediate = {
		location = { save_scope_as = background_wilderness_scope }
	}
	
	option = {
		name = great_holy_war.0057.a
		add_trait = faith_warrior
		hidden_effect = { #Punish player sneakery.
			trigger_event = {
				id = great_holy_war.0058
				days = { 15 60 }
			}
		}
	}
}

#Character is no longer in the Holy Land fighting infidels, not even a few weeks after getting the trait!
great_holy_war.0058 = {
	title = great_holy_war.0058.title
	desc = great_holy_war.0058.base.desc
	theme = faith
	left_portrait = {
		character = root
		animation = shame
	}
	
	trigger = {
		exists = faith.great_holy_war #GHW must still be ongoing.
		has_trait = faith_warrior
		is_commanding_army = no #Should have stayed in command.
		is_knight = no
		is_imprisoned = no #Don't penalise captured faith_warriors.
		NOR = {
			has_trait = incapable
			has_trait_with_flag = debilitating_illness
		}
		OR = { #Character or liege must still be at war, obviously.
			any_character_war = {
				OR = {
					using_cb = undirected_great_holy_war
					using_cb = directed_great_holy_war
				}
				is_attacker = root
			}
			any_liege_or_above = {
				faith = root.faith
				any_character_war = {
					OR = {
						using_cb = undirected_great_holy_war
						using_cb = directed_great_holy_war
					}
					is_attacker = root
				}
			}
		}
	}
	
	
	option = {
		name = great_holy_war.0058.a
		if = {
			limit = { is_ai = no } #Punish player more.
			add_piety = major_piety_loss
		}
		if = {
			limit = { has_trait = faith_warrior }
			remove_trait = faith_warrior
		}
	}
}

#on_character_faith_change: check if the character has changed Religion, in which case the Crusader trait should be cleared.
great_holy_war.0059 = {
	hidden = yes
	scope = character
	
	trigger = {
		OR = {
			has_trait = crusader_king
			has_trait = faith_warrior
		}
	}

	immediate = {
		if = {
			limit = {
				has_trait = crusader_king
			}
			remove_trait = crusader_king
		}
		else_if = {
			limit = {
				has_trait = faith_warrior
			}
			remove_trait = faith_warrior
		}
	}
}

#great_holy_war.0060 Taken for hidden event.


#Event firing for all existing Beneficiaries on_victory to give proper modifiers to their dynasty/House.
great_holy_war.0061 = {
	hidden = yes
	scope = character
	
	trigger = {
		is_alive = yes
		is_landed = yes #Must have received land in the Crusade.
	}
	
	immediate = {
		save_scope_as = scoped_beneficiary
		dynasty = {
			add_dynasty_modifier = {
				modifier = dynasty_beneficiary_won_ghw
				years = 30
			}
			every_dynasty_member = {
				limit = { is_ai = no }
				send_interface_message = {
					type = event_great_holy_war_good_message
					title = great_holy_war.0061.title
					desc = great_holy_war.0061.notification
					left_icon = scope:scoped_beneficiary.primary_title
					right_icon = scope:scoped_beneficiary
				}
			}
		}
		if = {
			limit = { OR = { faith.religion = religion:eastern_orthodox_religion faith.religion = religion:catholic_religion faith.religion = religion:protestant_religion } }
			house = {
				add_house_modifier = {
					modifier = house_beneficiary_won_ghw_crusade
					years = 30
				}
			}
		}
		else_if = {
			limit = { faith.religion = religion:islam_religion }
			house = {
				add_house_modifier = {
					modifier = house_beneficiary_won_ghw_jihad
					years = 30
				}
			}
		}
		else = {
			house = {
				add_house_modifier = {
					modifier = house_beneficiary_won_ghw
					years = 30
				}
			}
		}
		# Plus if we're gallowsbait, remove most/all of it — accounting for some weirdnesses from laamps.
		if = {
			limit = {
				NOR = {
					has_trait = crusader_king
					save_temporary_scope_as = char_temp
					scope:attacker.faith.great_holy_war = {
						any_pledged_attacker = { this = scope:char_temp }
					}
				}
			}
			absolve_crusaders_of_gallowsbait_effect = yes
		}
	}
}

great_holy_war.0070 = {
	title = great_holy_war.0070.title
	desc = great_holy_war.0070.base.desc
	theme = faith
	override_background = {
		reference = throne_room_scope
	}

	left_portrait = {
		character = scope:title_recipient
		animation = ecstasy
	}
	
	trigger = { is_ai = no }
	
	immediate = {
		scope:title_recipient = {
			save_scope_as = background_throne_room_scope
		}
	}

	option = {
		name = great_holy_war.0070.a
		set_player_character = scope:title_recipient
	}
	option = { name = great_holy_war.0070.b }
}

#0071 taken for directed GHW notification.

#on_death: of ghw_sponsor, pick a new one immediately.
great_holy_war.0080 = {
	hidden = yes
	scope = character
	
	trigger = {
		exists = faith.great_holy_war
		root = faith.great_holy_war.ghw_war_declarer
	}
	
	immediate = {
		faith = {
			save_scope_as = the_faith
			ghw_set_new_war_declarer_effect = yes
		}
	}
}

#Player redirected GHW during preparation phase. (old defenders)
great_holy_war.0081 = {
	title = great_holy_war.0081.title
	desc = great_holy_war.0081.desc
	theme = faith
	right_portrait = {
		character = scope:actor
		animation = scheme
	}
	left_portrait = scope:second_recipient
	lower_left_portrait = scope:recipient

	
	option = { name = great_holy_war.0081.a }
}

#Player redirected GHW during preparation phase. (new defenders)
great_holy_war.0082 = {
	title = great_holy_war.0081.title
	desc = great_holy_war.0082.desc
	theme = faith
	right_portrait = {
		character = scope:actor
		animation = scheme
	}
	left_portrait = scope:second_recipient
	lower_left_portrait = scope:recipient

	option = { name = great_holy_war.0082.a }
}

#On_death event to reset GHW fallback recipient.
great_holy_war.0083 = {
	hidden = yes

	trigger = {
		exists = faith.great_holy_war
		has_character_flag = flag_is_ghw_fallback_recipient
	}

	immediate = {	
		faith.great_holy_war = { remove_variable = var_fallback_recipient }
		if = {
			limit = { exists = faith.great_holy_war.ghw_war_declarer.capital_province }
			faith.great_holy_war.ghw_war_declarer = { save_scope_as = fallback_host }
		}
		else_if = {
			limit = {
				NOT = { exists = faith.great_holy_war.ghw_war_declarer.capital_province }
			}
			random_ruler = {
				limit = {
					is_landed = yes
					faith = root.faith
					highest_held_title_tier > 1
				}
				save_scope_as = fallback_host
			}
		}
		#Generate a fallback recipient.
		create_character = {
			location = scope:fallback_host.capital_province
			template = new_commander_character
			faith = root.faith
			culture = root.culture
			save_scope_as = fallback_recipient
			gender_female_chance = {
				if = {
					limit = {
						root.faith = { has_doctrine = doctrine_gender_male_dominated }
					}
					add = 0
				}
				else_if = {
					limit = {
						root.faith = { has_doctrine = doctrine_gender_female_dominated }
					}
					add = 100
				}
				else = { add = 50 }
			}
		}
		scope:fallback_recipient = {
			add_character_flag = flag_is_ghw_fallback_recipient
		}
		root.faith.great_holy_war = {
			set_variable = {
				name = var_fallback_recipient
				value = scope:fallback_recipient
			}
		}
	}
}

#On_faith_conversion event to reset GHW fallback recipient.
great_holy_war.0084 = {
	hidden = yes

	trigger = {
		exists = scope:old_faith.great_holy_war
		has_character_flag = flag_is_ghw_fallback_recipient
	}

	immediate = {	
		scope:old_faith.great_holy_war = { remove_variable = var_fallback_recipient }
		if = {
			limit = { exists = scope:old_faith.great_holy_war.ghw_war_declarer.capital_province }
			scope:old_faith.great_holy_war.ghw_war_declarer = { save_scope_as = fallback_host }
		}
		else_if = {
			limit = {
				NOT = { exists = scope:old_faith.great_holy_war.ghw_war_declarer.capital_province }
			}
			scope:old_faith = {
				random_faith_ruler = {
					limit = {
						is_landed = yes
						highest_held_title_tier > tier_barony
					}
					save_scope_as = fallback_host
				}
			}
		
		}
		#Generate a fallback recipient.
		create_character = {
			location = scope:fallback_host.capital_province
			template = new_commander_character
			faith = scope:old_faith
			culture = root.culture
			save_scope_as = fallback_recipient
			gender_female_chance = {
				if = {
					limit = {
						root.faith = { has_doctrine = doctrine_gender_male_dominated }
					}
					add = 0
				}
				else_if = {
					limit = {
						root.faith = { has_doctrine = doctrine_gender_female_dominated }
					}
					add = 100
				}
				else = { add = 50 }
			}
		}
		scope:fallback_recipient = { add_character_flag = flag_is_ghw_fallback_recipient }
		scope:old_faith.great_holy_war = {
			set_variable = {
				name = var_fallback_recipient
				value = scope:fallback_recipient
			}
		}
	}
}

#On_faith_conversion: clear all vassals of the old faith who may be pledged to a GHW.
great_holy_war.0085 = {
	hidden = yes

	trigger = {
		exists = scope:old_faith.great_holy_war
		NOT = { exists = scope:old_faith.great_holy_war.ghw_war }
		any_vassal = {
			faith = scope:old_faith
			faith.great_holy_war = { has_pledged_attacker = prev }
		}
	}

	immediate = {
		save_scope_as = converter
		every_vassal = {
			limit = {
				faith = scope:old_faith
				faith.great_holy_war = { has_pledged_attacker = prev }
			}
			send_interface_toast = {
				type = event_toast_effect_bad
				right_icon = this
				left_icon = scope:converter
				title = great_holy_war.0085.toast
				faith.great_holy_war = {
					unpledge_attacker = prev
					unpledge_ghw_war_chest_prestige_effect = yes
					unpledge_ghw_war_chest_piety_effect = yes
				}
			}
		}
	}
}
