﻿namespace = great_holy_war
#CHANGES: allow for GHW cooldown to be skipped if Fervor is at 100

# 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
			#CHANGE: If fervor is 100 cooldown can be skipped
			AND = {
				exists = var:variable_ghw_cooldown
				fervor <= 99
			}
			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 = 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 = 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
				NOT = { 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 = {
						NOT = { 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_ruler = {
			limit = {
				faith = scope:the_faith
				is_ai = yes
				is_landed = yes
				highest_held_title_tier >= tier_county
				NOT = { this = scope:ghw_sponsor }
				NOT = {
					root.great_holy_war = { has_pledged_attacker = prev }
				}
			}
			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)
		every_ruler = {
			limit = {
				faith.religion = scope:target_character.faith.religion
				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 }
			}
		}
		scope:target_character = {
			faith = {
				every_faith_holy_order = {
					trigger_event = {
						# Holy orders always join
						id = great_holy_war.0091
						days = { 30 60 }
					}
				}
			}
		}
	}
}