# excommunicated_ruler
# depose_antipope
# shepherds_crusade_invasion
# northern_crusade_war
# childrens_crusade
# pepin_religious
# depose_impious_pope

excommunicated_ruler = {
	name = CB_NAME_EXCOMMUNICATED_RULER
	war_name = WAR_NAME_EXCOMMUNICATED_RULER
	sprite = 11
	truce_days = 3650
	sort_priority = 750

	is_permanent = yes
	can_ask_to_join_war = yes
	defender_unoccupied_warscore = yes
	allowed_to_target_tributaries = no
	apply_short_occ_mod = no

	can_use_gui = {
		check_if_crusader_trigger = yes

		# CK2Plus
		# Attacker
		ROOT = {
			can_use_cb_preamble = yes
		}
	}

	can_use = {
		# Attacker
		ROOT = {
			NOT = { excommunicated_for = ROOT }
			mercenary = no

			# CK2Plus
			NOT = { is_vassal_or_below_of = FROM }
		}

		# Defender
		FROM = {
			excommunicated_for = ROOT
			in_revolt = no

			# Not fighting religious enemies
			NOT = {
				any_current_enemy = {
					NOT = { religion_group = ROOT }
				}
			}
		}
	}

	is_valid = {
		# Attacker
		ROOT = {
			OR = {
				NOT = { excommunicated_for = ROOT }
				has_character_flag = flag_excommunicated_for_coronation
			}
		}

		# Defender
		FROM = {
			custom_tooltip = {
				text = tooltip_is_excommunicated_for
				excommunicated_for = ROOT
			}
		}
	}

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_invalidation = {
		attacker = {
			hidden_effect = {
				if = { # Coronation request locks cleared on invalidation or loss, to avoid locking the player in the middle of the negotiation forever
					limit = { has_character_flag = flag_excommunicated_for_coronation }
					character_event = { id = HF.20220 } # Papal Coronation may proceed
				}
			}
		}
	}

	on_success = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with [Root.GetHerHis] excommunication war against [From.GetBestName] (ID: [From.GetID])"

		attacker = {
			show_scope_change = no

			if = {
				limit = { has_character_flag = flag_excommunicated_for_coronation }
				character_event = { id = HF.20220 } # Papal Coronation may proceed
			}

			defender = {
				show_scope_change = no

				primary_title = {
					show_scope_change = no

					trigger_switch = {
						on_trigger = real_tier

						BARON = {
							any_attacker = {
								show_scope_change = no

								participation_scaled_piety = 25
								participation_scaled_prestige = 25
							}
						}

						COUNT = {
							any_attacker = {
								show_scope_change = no

								participation_scaled_piety = 50
								participation_scaled_prestige = 50
							}
						}

						DUKE = {
							any_attacker = {
								show_scope_change = no

								participation_scaled_piety = 100
								participation_scaled_prestige = 100
							}
						}

						KING = {
							any_attacker = {
								show_scope_change = no

								participation_scaled_piety = 200
								participation_scaled_prestige = 200
							}
						}

						EMPEROR = {
							any_attacker = {
								show_scope_change = no

								participation_scaled_piety = 300
								participation_scaled_prestige = 300
							}
						}
					}
				}
			}

			# CK2Plus
			pf_tradition_plus2_effect = yes
		}

		defender = {
			show_scope_change = no

			excommunicate = no
			prestige = -500

			rightful_religious_head_scope = {
				show_scope_change = no

				opinion = {
					name = opinion_repented_sins
					who = PREV
				}
			}

			# CK2Plus
			any_demesne_title = {
				title_reduce_CA_effect = yes
				title_reduce_TO_effect = yes
			}

			# Possible independence for the defender's weakly held vassals
			if = {
				limit = { independent = yes }

				hidden_effect = {
					any_vassal = {
						character_event = {
							id = Plus.430
							days = 3
						}
					}
				}
			}

			# Vanilla but needs to be at the end to work correctly
			abdicate = yes
		}

		# CK2Plus
		hidden_effect = {
			any_attacker = {
				opinion = {
					name = overthrown_ruler
					who = defender
				}

				reverse_opinion = {
					name = overthrew_me
					who = defender
				}
			}
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			piety = -50
			prestige = -100

			hidden_effect = { # Coronation request locks cleared on invalidation or loss, to avoid locking the player in the middle of the negotiation forever
				attacker = {
					clr_character_flag = flag_excommunicated_for_coronation
				}
			}
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			piety = -100
			prestige = -200

			transfer_scaled_wealth = {
				to = defender
				value = 4.0
			}

			# Coronation request locks cleared on invalidation or loss, to avoid locking the player in the middle of the negotiation forever
			clr_character_flag = flag_excommunicated_for_coronation

			# CK2Plus
			pf_tradition_minus2_effect = yes
		}

		defender = {
			show_scope_change = no

			prestige = 100
		}

		# CK2Plus
		any_defender = {
			limit = { NOT = { character = defender } }
			hidden_effect = { participation_scaled_prestige = 100 }
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = {
		factor = 1

		trigger = {
			ROOT = {
				NOT = { trait = cynical }

				OR = {
					trait = zealous
					piety = 250

					opinion = {
						who = FROM
						value < -50
					}
				}
			}
		}
	}
}

depose_antipope = {
	name = CB_NAME_DEPOSE_ANTIPOPE
	war_name = WAR_NAME_DEPOSE_ANTIPOPE
	sprite = 29
	truce_days = 3650
	sort_priority = 750

	is_permanent = yes
	can_ask_to_join_war = yes
	defender_unoccupied_warscore = yes
	attacker_rel_head_is_ally = yes # The attacker can call his (main) Pope into the war
	allowed_to_target_tributaries = no

	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		# Attacker
		ROOT = {
			OR = {
				religion = catholic
				religion = fraticelli
			}

			religion = FROM

			# Does not have an antipope
			rightful_religious_head_scope = {
				NOR = {
					has_claim = k_papacy
					has_claim = d_fraticelli
				}
			}

			mercenary = no
		}

		# Defender
		FROM = {
			# Must have an antipope
			rightful_religious_head_scope = {
				is_vassal_or_below_of = PREV

				OR = {
					has_claim = k_papacy
					has_claim = d_fraticelli
				}
			}
		}
	}

	is_valid = {
		# Attacker
		ROOT = {
			OR = {
				religion = catholic
				religion = fraticelli
			}

			religion = FROM

			rightful_religious_head_scope = {
				NOR = {
					has_claim = k_papacy
					has_claim = d_fraticelli
				}
			}
		}

		# Defender
		FROM = {
			independent = yes

			rightful_religious_head_scope = {
				is_vassal_or_below_of = PREV

				OR = {
					has_claim = k_papacy
					has_claim = d_fraticelli
				}
			}
		}
	}

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_invalidation = {
		# Coronation request locks cleared on invalidation or loss, to avoid locking the player in the middle of the negotiation forever.
		hidden_effect = {
			attacker = {
				clr_character_flag = flag_antipope_for_coronation
			}

			defender = {
				clr_character_flag = flag_antipope_for_coronation
			}
		}
	}

	on_success = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with [Root.GetHerHis] depose antipope CB against [From.GetBestName] (ID: [From.GetID])"

		attacker = {
			show_scope_change = no

			participation_scaled_piety = 500
			participation_scaled_prestige = 250

			religion_authority = {
				name = deposed_antipope
				years = 50
			}

			if = {
				limit = {
					rightful_religious_head_scope = {
						attacker = {
							excommunicated_for = PREV
						}
					}
				}

				excommunicate = no
			}

			if = {
				limit = { has_character_flag = flag_antipope_for_coronation }
				character_event = { id = HF.20220 } # Papal Coronation may proceed
			}

			rightful_religious_head_scope = {
				show_scope_change = no
				opinion = {
					name = opinion_deposed_antipope
					who = attacker
				}
			}
		}

		any_attacker = {
			limit = { NOT = { character = attacker } }

			hidden_effect = {
				participation_scaled_piety = 500
				participation_scaled_prestige = 250
			}
		}

		defender = {
			show_scope_change = no

			piety = -500
			prestige = -250
			pf_tradition_minus2_effect = yes

			rightful_religious_head_scope = {
				show_scope_change = no
				custom_tooltip = {
					text = ANTIPOPE_DEPOSED
					depose_antipope_effect = yes
				}
			}
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			piety = -250
			prestige = -125
			clr_character_flag = flag_antipope_for_coronation
		}

		defender = {
			show_scope_change = no

			piety = 100
			prestige = 50
			clr_character_flag = flag_antipope_for_coronation
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			transfer_scaled_wealth = {
				to = defender
				value = 4.0
			}

			piety = -500
			prestige = -250
			clr_character_flag = flag_antipope_for_coronation
		}

		defender = {
			show_scope_change = no

			piety = 500
			prestige = 250
			clr_character_flag = flag_antipope_for_coronation
			pf_tradition_plus2_effect = yes
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	# CK2Plus
	ai_will_do = {
		factor = 1

		trigger = {
			ROOT = {
				sufficient_treasury_for_war_trigger = yes
				NOT = { trait = cynical }
			}
		}

		mult_modifier = {
			factor = 5
			FROM = { trait = zealous }
		}
	}
}

shepherds_crusade_invasion = {
	name = CB_NAME_SHEPHERDS_CRUSADE
	war_name = WAR_NAME_SHEPHERDS_CRUSADE
	sprite = 9
	truce_days = 0

	check_de_jure_tier = KING
	is_permanent = no
	allow_whitepeace = no
	can_ask_to_join_war = yes
	is_holy_war = yes
	defender_unoccupied_warscore = yes
	allowed_to_target_tributaries = yes

	can_use = {
		# Attacker
		ROOT = {
			has_character_flag = shepherds_leader
			NOT = { religion = FROM }
		}
	}

	is_valid = {
		# Attacker
		ROOT = {
			NOT = { religion = FROM }
		}
	}

	on_attacker_leader_death = {
		any_player = {
			narrative_event = { id = HFP.42310 }
		}
	}

	on_success_title = {
		save_event_target_as = shepherds_kingdom

		if = {
			limit = {
				OR = {
					has_holder = no
					holder = defender

					holder_scope = {
						top_liege = {
							character = defender
						}
					}
				}
			}

			gain_title = attacker
		}
		else = {
			create_title = {
				tier = KING
				name = KINGDOM_OF_THE_SHEPHERDS
				holder = attacker
				culture = attacker
				base_title = event_target:shepherds_kingdom
			}

			add_claim = attacker
		}

		attacker = {
			show_scope_change = no

			gain_settlements_under_title = {
				title = PREV
				enemy = defender
				is_crusade = yes # Even if the title holder is not participating in the war, gain holdings occupied by all Crusade participants
				type = holy_war
			}

			clr_character_flag = shepherds_leader
			set_defacto_liege = attacker

			hidden_effect = {
				dynasty = father_bastard

				if = {
					limit = { is_female = no }

					add_trait = crusader_king

					create_bloodline = {
						type = crusader_commander
					}
				}
				else = {
					add_trait = crusader_queen

					create_bloodline = {
						type = crusader_commander
						inheritance = matrilineal
					}
				}

				set_bloodline_founder_religion_flag_effect = yes

				event_target:shepherds_kingdom = {
					capital_scope = {
						attacker = {
							capital = PREV
						}

					#	save_event_target_as = shepherds_capital

						duchy = {
							save_event_target_as = shepherds_duchy
						}
					}
				}

				primary_title = {
					set_title_flag = non_manual_law_change # No prestige loss

					add_law = {
						law = succ_primogeniture
						cooldown = no
						opinion_effect = no
					}

					add_law = {
						law = centralization_2
						cooldown = no
						opinion_effect = no
					}

					add_law = {
						law = cognatic_succession
						cooldown = no
						opinion_effect = no
					}

					if = {
						limit = { has_dlc = "Conclave" }

						add_law = {
							law = ze_administration_laws_1
							cooldown = no
							opinion_effect = no
						}

						add_law = {
							law = out_of_realm_inheritance_law_1
							cooldown = no
							opinion_effect = no
						}
					}
					else = {
						add_law = {
							law = crown_authority_3
							cooldown = no
							opinion_effect = no
						}
					}

					clr_title_flag = non_manual_law_change # No prestige loss
				}

				character_event = { id = HFP.41090 days = 1 } # Refill the levy, and remove the negative modifiers
				character_event = { id = HFP.41090 days = 3 } # Refill the levy, and remove the negative modifiers

				if = {
					limit = { wealth < 200 }

					clear_wealth = yes
					wealth = 200
				}

				any_demesne_title = {
					limit = {
						OR = {
							location = {
								duchy = {
									NOT = { title = event_target:shepherds_duchy }
								}
							}

							holding_type = city
							holding_type = temple
						}
					}

					if = {
						limit = { holding_type = city }

						create_random_steward = {
							random_traits = yes
							dynasty = random
						}
					}
					else_if = {
						limit = { holding_type = temple }

						create_random_priest = {
							random_traits = yes
							dynasty = random
						}
					}
					else_if = {
						limit = { holding_type = castle }

						create_random_soldier = {
							random_traits = yes
							dynasty = random
						}
					}

					grant_title_no_opinion = new_character

					new_character = {
						add_trait = crusader
						add_trait = brave

						attacker = {
							set_truce = {
								who = PREV
								years = 10
							}
						}
					}
				}

				event_target:shepherds_kingdom = {
					any_direct_de_jure_vassal_title = {
						random_direct_de_jure_vassal_title = {
							limit = {
								holder_scope = {
									top_liege = {
										character = attacker
									}
								}
							}

							holder_scope = {
								gain_title = PREVPREV

								primary_title = {
									any_direct_de_jure_vassal_title = {
										limit = {
											holder_scope = {
												top_liege = {
													character = attacker
												}
											}
										}

										holder_scope = {
											set_defacto_liege = PREVPREVPREV
										}
									}
								}

								set_defacto_liege = attacker
							}
						}
					}
				}

				any_claim = {
					remove_claim = PREV
				}
			}

			if = {
				limit = { can_apply_average_nickname_trigger = yes }

				random_list = {
					10 = { give_nickname = nick_the_crusader }
					10 = { give_nickname = nick_the_holy }
					10 = { give_nickname = nick_the_pilgrim }
					10 = { give_nickname = nick_the_protector }
					10 = { give_nickname = nick_the_guardian }
					40 = { give_nickname = nick_the_sword_of_god }
				}
			}

			check_if_reconquista_finished_effect = yes
		}

		any_player = {
			narrative_event = { id = HFP.42309 } # Victorious Shepherds' Crusade
		}
	}

	on_reverse_demand = {
		# Attackers
		any_attacker = {
			show_scope_change = no

			transfer_scaled_wealth = {
				to = defender
				value = 1.0
			}
			piety = -100
		}

		defender = {
			show_scope_change = no

			piety = 200
			reverse_imprison = attacker
		}

		any_player = {
			narrative_event = { id = HFP.42308 } # Failed Shepherds' Crusade
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

northern_crusade_war = {
	name = CB_NAME_NORTHERN_CRUSADE
	war_name = WAR_NAME_NORTHERN_CRUSADE
	sprite = 9
	truce_days = 1825

	is_permanent = no # Disabled in CK2Plus
	is_holy_war = yes
	check_de_jure_tier = DUKE
	allow_whitepeace = no
	can_ask_to_join_war = yes
	defender_unoccupied_warscore = yes
	allowed_to_target_tributaries = yes

	can_use_gui = {
		# Attacker
		ROOT = {
			prisoner = no
			is_adult = yes
		}
	}

	can_use = {
		# Attacker
		ROOT = {
			OR = {
				has_landed_title = d_teutonic_order
				has_landed_title = k_teutonic_state
			}
		}

		# Defender
		FROM = {
			NOT = { religion_group = ROOT }
		}
	}

	can_use_title = {
		kingdom = {
			title = event_target:northern_crusade_kingdom_title
		}
	}

	is_valid = {
		# Defender
		FROM = {
			NOT = { religion = ROOT }
		}
	}

	on_success = {
		any_attacker = {
			limit = {
				NOR = {
					has_landed_title = d_teutonic_order
					has_landed_title = k_teutonic_state
				}
			}

			piety = 150
		}

		any_defender = {
			piety = -100
		}
	}

	on_success_title = {
		save_event_target_as = northern_crusade_title # For localisation

		# Attacker
		attacker = {
			show_scope_change = no

			vassalize_or_take_under_title = {
				title = PREV
				enemy = defender
				same_religion = yes # Only vassalize rulers of my religion
				is_religious = yes
				type = holy_war
			}

			hidden_effect = {
				# If the Teutonic Order don't have a base to work out from, get one
				if = {
					limit = {
						capital_scope = {
							duchy = {
								title = event_target:northern_crusade_title
							}
						}
					}

					teutonic_order_upgrade_capital_effect = yes
					holy_order_distribute_titles_effect = yes
					character_event = { id = HFP.41090 days = 1 } # Refill the levy, and remove the negative modifiers
					character_event = { id = HFP.41090 days = 3 } # Refill the levy, and remove the negative modifiers
				}

				# Become the Teutonic State, if they aren't already
				if = {
					limit = {
						NOT = { has_landed_title = k_teutonic_state }
					}

					k_teutonic_state = {
						set_title_flag = non_manual_law_change # No prestige loss
						copy_title_history = d_teutonic_order
						copy_title_laws = d_teutonic_order
						clr_title_flag = non_manual_law_change # No prestige loss
					}

					grant_title_no_opinion = k_teutonic_state
					unsafe_destroy_landed_title = d_teutonic_order

					activate_title = {
						title = d_teutonic_order
						status = no
					}
				}

				## Rewards
				# Check who to give artifacts
				any_attacker = {
					limit = {
						NOT = { character = attacker }

						any_war = {
							using_cb = northern_crusade_war

							war_participation = {
								who = PREV
								score > 40
							}
						}
					}

					set_character_flag = northern_crusade_artifact_reward
				}

				# Check who to give money
				any_attacker = {
					limit = {
						NOT = { character = attacker }

						any_war = {
							using_cb = northern_crusade_war

							war_participation = {
								who = PREV
								score > 20
							}
						}
					}

					set_character_flag = northern_crusade_gold_reward
				}

				# Check who to give artifacts
				any_attacker = {
					limit = {
						NOT = { character = attacker }

						any_war = {
							using_cb = northern_crusade_war

							war_participation = {
								who = PREV
								score > 0.01
							}
						}
					}

					letter_event = {
						id = HF.49112 # Reward letter from Teutons after the war
						days = 1
					}
				}

				# Checking for Bloodline
				most_participating_attacker = {
					if = {
						limit = {
							NOT = { character = attacker }
						}

						if = {
							limit = {
								check_variable = {
									which = northern_crusade_ally
									value == 1
								}
							}

							set_variable = {
								which = northern_crusade_ally
								value = 2
							}
						}
						else_if = {
							limit = {
								check_variable = {
									which = northern_crusade_ally
									value >= 2
								}
							}

							narrative_event = { id = HF.49114 } # Northern Crusade Bloodline
						}
						else = {
							set_variable = {
								which = northern_crusade_ally
								value = 1
							}
						}
					}
				}

				## Misc
				# Check if the Northern Crusade is over
				if = {
					limit = {
						PREV = {
							kingdom = {
								title = event_target:northern_crusade_kingdom_title

								NOT = {
									any_direct_de_jure_vassal_title = {
										any_direct_de_jure_vassal_title = {
											holder_scope = {
												NOT = { religion_group = christian }
											}
										}
									}
								}
							}
						}
					}

					clear_global_event_target = northern_crusade_kingdom_title

					religion_authority = {
						name = northern_crusade_won
					}

					attacker = {
						narrative_event = { id = HF.49115 } # Northern Crusade Successful
					}

					any_playable_ruler = {
						narrative_event = { id = HF.49115 } # Northern Crusade Successful
					}

					set_global_flag = northern_crusade_success
					clr_global_flag = northern_crusades_active
				}
			}
		}
	}

	on_reverse_demand = {
		# Attacker
		attacker = {
			show_scope_change = no

			hidden_effect = {
				if = {
					limit = {
						check_variable = {
							which = global_northernCrusadeValue
							value == 1
						}
					}

					set_variable = {
						which = global_northernCrusadeValue
						value = 2
					}
				}
				else_if = {
					limit = {
						check_variable = {
							which = global_northernCrusadeValue
							value >= 2
						}
					}

					attacker = {
						narrative_event = { id = HF.49113 } # The Northern Crusades has failed
					}

					defender = {
						narrative_event = { id = HF.49113 } # The Northern Crusades has failed
					}

					any_playable_ruler = {
						narrative_event = { id = HF.49113 } # The Northern Crusades has failed
					}
				}
				else = {
					set_variable = {
						which = global_northernCrusadeValue
						value = 1
					}
				}
			}
		}

		any_attacker = {
			piety = -100
		}

		any_defender = {
			piety = 150
		}
	}

	attacker_ai_victory_worth = {
		factor = -1
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

childrens_crusade = {
	name = CB_NAME_CRUSADE
	war_name = WAR_NAME_CRUSADE
	sprite = 9
	truce_days = 3650
	battle_warscore_mult = 0.5
	max_defender_occupation_score = 85

	is_permanent = no # Disabled in CK2Plus
	is_holy_war = yes
	check_de_jure_tier = KING # this scans all de jure kingdoms for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	allow_whitepeace = no
	capturing_defender_is_complete_victory = no
	allowed_to_target_tributaries = no

	can_use_gui = {
		# Attacker
		ROOT = {
			religion = catholic
			has_character_flag = flag_childrens_crusade_leader # Only available for the child crusader
			has_global_flag = christian_crusades_unlocked
		}
	}

	can_use = {
		# Attacker
		ROOT = {
			religion = catholic
			has_character_flag = flag_childrens_crusade_leader # Only available for the child crusader
			has_global_flag = christian_crusades_unlocked
		}

		# Defender
		FROM = {
			OR = {
				NOT = { religion_group = ROOT }

				# One is a heresy of the other
				is_heresy_of = ROOT
				is_parent_religion = ROOT

				trigger_if = {
					limit = { religion_group = pagan_group }
					NOT = { religion = ROOT }
				}
			}
		}
	}

	is_valid = {
		# Attacker
		ROOT = {
			has_character_flag = flag_childrens_crusade_leader # Only available for the child crusader
		}

		# Defender
		FROM = {
			OR = {
				NOT = { religion_group = ROOT }

				# One is a heresy of the other
				is_heresy_of = ROOT
				is_parent_religion = ROOT

				AND = {
					religion_group = pagan_group
					NOT = { religion = ROOT }
				}
			}
		}
	}

	is_valid_title = {
		# Defender
		FROM = {
			num_of_realm_counties = {
				value >= 1
				title = PREV
			}
		}
	}

	on_invalidation = {
		attacker = {
			clr_character_flag = flag_childrens_crusade_leader
			remove_character_modifier = children_crusade_morale_0
			remove_character_modifier = children_crusade_morale_1
			remove_character_modifier = children_crusade_morale_2
			remove_character_modifier = children_crusade_morale_3
			remove_character_modifier = children_crusade_morale_4

			any_courtier = {
				limit = {
					is_ruler = no
					is_landed = no
					vassal_of = attacker
				}

				death = { death_reason = death_slavery }
			}

			primary_title = {
				activate_title = {
					title = THIS
					status = no
				}

				destroy_landed_title = THIS
			}

			death = { death_reason = death_slavery }
		}

		any_player = {
			limit = {
				religion_group = christian
				NOT = { character = attacker }
			}

			narrative_event = {
				id = HF.25823 # Notify everyone that the crusade has flopped
				days = 7
			}
		}
	}

	on_add = {
		attacker = {
			show_scope_change = no

			add_trait = crusader
			diplomatic_immunity = no
		}

		any_player = {
			limit = {
				religion_group = defender
				NOT = { character = attacker }
			}

			narrative_event = { id = HF.25824 } # Notify infidels that the children have arrived
		}

		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_success_title = {
		k_jerusalem = {
			show_scope_change = no

			if = {
				limit = { has_holder = no }

				grant_title = attacker
				hidden_effect = { make_primary_title = yes }

				set_title_flag = non_manual_law_change # No prestige loss
				add_law = succ_primogeniture
				add_law = cognatic_succession
				clr_title_flag = non_manual_law_change # No prestige loss
			}

			else_if = {
				limit = {
					holder_scope = {
						NOT = { religion = attacker }
					}
				}

				usurp_title_plus_barony_if_unlanded = {
					target = attacker
					type = holy_war
				}
			}
		}

		c_jerusalem = {
			show_scope_change = no

			if = {
				limit = {
					holder_scope = {
						NOT = { religion = attacker }
					}
				}
			}

			usurp_title_plus_barony_if_unlanded = {
				target = attacker
				type = holy_war
			}

			religion = attacker
		}

		attacker = {
			show_scope_change = no

			participation_scaled_prestige = 500
			participation_scaled_piety = 1000

			religion_authority = {
				name = won_crusade
				years = 20
			}

			hidden_effect = {
				clr_character_flag = flag_childrens_crusade_leader
				remove_character_modifier = children_crusade_morale_0
				remove_character_modifier = children_crusade_morale_1
				remove_character_modifier = children_crusade_morale_2
				remove_character_modifier = children_crusade_morale_3
				remove_character_modifier = children_crusade_morale_4
				remove_character_modifier = voice_of_jesus # Your work here is dones

				if = {
					limit = { is_female = no }

					create_bloodline = {
						type = crusader_kid_bloodline
					}
				}
				else = {
					create_bloodline = {
						type = crusader_kid_bloodline
						inheritance = matrilineal
					}
				}

				set_bloodline_founder_religion_flag_effect = yes

				any_player = {
					limit = {
						religion_group = christian
						NOT = { character = attacker }
					}

					narrative_event = { id = HF.25828 } # Notify everyone that the crusade has succeeded
				}

				# Vassalize and convert all infidels under the title
				vassalize_or_take_under_title = {
					title = PREV
					enemy = defender
					is_religious = yes
					type = holy_war
				}

				if = {
					limit = {
						primary_title = { is_primary_type_title = no } # Mercs, the Pope, Holy Orders, etc

						PREV = {
							OR = {
								has_holder = no

								holder_scope = {
									top_liege = { character = defender }
								}
							}
						}

						NOT = { character = attacker }

						OR = {
							independent = yes

							liege = {
								real_tier = EMPEROR
							}
						}
					}

					usurp_title_only = {
						target = PREV
						type = holy_war
					}

					if = {
						limit = {
							NOT = { culture = PREV }
						}

						save_event_target_as = winning_crusader
						PREV = { conquest_culture = event_target:winning_crusader }
					}
				}

				remove_trait = disinherited

				any_courtier = {
					remove_trait = disinherited
					clr_character_flag = no_court_invites
				}

				if = {
					limit = { is_female = no }

					remove_trait = crusader
					add_trait = crusader_king
				}

				else = {
					remove_trait = crusader
					add_trait = crusader_queen
				}

				if = {
					limit = { this_has_super_cool_nickname_trigger = no }

					random_list = {
						20 = { give_nickname = nick_the_angel_of_location }
						15 = { give_nickname = nick_the_chosen_of_gabriel }

						15 = {
							trigger = { is_female = no }
							give_nickname = nick_the_page_of_lord
						}

						15 = {
							trigger = { is_female = yes }
							give_nickname = nick_the_maid_of_virgin
						}

						15 = { give_nickname = nick_the_messenger_of_christ }
						5 = { give_nickname = nick_the_sword_of_god }
					}
				}
			}

			any_playable_ruler = {
				limit = {
					religion_group = defender
					is_within_diplo_range = defender
					NOT = { character = attacker }
				}

				narrative_event = { id = HF.25827 } # Notify all infidels that the crusade has succeeded. Offer chance of conversion/vassalization if within k_jerusalem
			}
		}

		any_attacker = {
			limit = { NOT = { character = attacker } }

			hidden_effect = {
				participation_scaled_prestige = 500
				participation_scaled_piety = 1000

				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -100
				}
			}
		}

		any_attacker = {
			limit = {
				ai = no
				is_ironman = yes
				NOT = { has_character_flag = achievement_crusader }
			}

			set_character_flag = achievement_crusader
		}

		defender = {
			show_scope_change = no

			piety = -100

			religion_authority = {
				name = lost_crusade
				years = 20
			}
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			piety = -200

			transfer_scaled_wealth = {
				to = defender
				value = 4.0
			}

			if = {
				limit = {
					uses_decadence = yes

					NOT = {
						any_liege = {
							dynasty = PREV
						}
					}
				}

				participation_scaled_decadence = 35
			}

			religion_authority = {
				name = lost_crusade
				years = 20
			}

			any_courtier = {
				limit = {
					is_ruler = no
					is_landed = no
					vassal_of = attacker
				}

				death = { death_reason = death_slavery }
			}

			primary_title = {
				activate_title = {
					title = THIS
					status = no
				}

				destroy_landed_title = THIS
			}

			death = { death_reason = death_slavery }

			hidden_effect = {
				clr_character_flag = flag_childrens_crusade_leader
				remove_character_modifier = children_crusade_morale_0
				remove_character_modifier = children_crusade_morale_1
				remove_character_modifier = children_crusade_morale_2
				remove_character_modifier = children_crusade_morale_3
				remove_character_modifier = children_crusade_morale_4
			}
		}

		any_attacker = {
			limit = {
				uses_decadence = yes

				NOR = {
					any_liege = {
						dynasty = PREV
					}

					character = attacker
				}
			}

			hidden_effect = {
				participation_scaled_decadence = 35
			}
		}

		defender = {
			show_scope_change = no

			piety = 100
			participation_scaled_piety = 300
			participation_scaled_prestige = 150

			religion_authority = {
				name = won_crusade
				years = 20
			}

			if = {
				limit = { uses_decadence = yes }
				participation_scaled_decadence = -100
			}

			roman_victory_triumph_effect = yes
		}

		any_defender = {
			limit = { NOT = { character = defender } }

			hidden_effect = {
				participation_scaled_piety = 300
				participation_scaled_prestige = 150

				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -100
				}
			}
		}

		any_player = {
			limit = {
				religion_group = christian
				NOT = { character = attacker }
			}

			narrative_event = { id = HF.25825 } # Notify everyone that the crusade has failed
		}

		any_player = {
			limit = {
				religion_group = defender
				NOT = { character = attacker }
			}

			narrative_event = { id = HF.25826 } # Notify every pagan that the crusade has failed
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}

# Event-tied cb for Papal Holy War (that is, holy war on behalf of the Papacy)
pepin_religious = {
	name = CB_NAME_PAPAL_RELIGIOUS
	war_name = WAR_NAME_PAPAL_RELIGIOUS
	sprite = 9
	truce_days = 3650
	sort_priority = 790

	is_permanent = no # Disabled in CK2Plus
	is_holy_war = yes
	check_de_jure_tier = DUKE # this scans all dejure duchies for the counties which are held by or vassals(or below) of selected character. Only valid if is_permanent = yes
	allowed_to_target_tributaries = yes
	attacker_rel_head_is_ally = yes # The attacker can call his (main) Pope into the war

	can_use_gui = {
		check_if_crusader_trigger = yes
	}

	can_use = {
		has_dlc = "Holy Fury"

		# Attacker
		ROOT = { # The cb is either available due to Coronation dealings, or as any King or Emperor ruler who has a Pope for Religious Head
			OR = {
				religion = catholic
				religion = fraticelli
			}

			higher_real_tier_than = DUKE
			is_feudal = yes
			independent = yes
			NOT = { same_realm = FROM }

			OR = {
				has_character_flag = flag_pepin_for_coronation

				trigger_if = {
					limit = { religion = catholic }

					rightful_religious_head_scope = {
						has_landed_title = k_papacy
					}
				}
				trigger_else_if = {
					limit = { religion = fraticelli }

					rightful_religious_head_scope = {
						has_landed_title = d_fraticelli
					}
				}
			}
		}

		# Defender
		FROM = {
			is_within_diplo_range = ROOT

			NOR = {
				has_landed_title = k_papacy
				has_landed_title = d_fraticelli
			}

			# Target must either be a pagan or heretic of any government form, or it must be a disliked Catholic/Fraticelli who is not a bishop
			OR = {
				AND = {
					is_theocracy = no
					religion = ROOT

					ROOT = {
						religion_head = {
							opinion = {
								who = FROM
								value < -50
							}
						}
					}
				}

				excommunicated_for = ROOT
				NOT = { religion_group = ROOT }
				is_heresy_of = ROOT
			}
		}
	}

	can_use_title = {
		# The attacker needs to be within diplo range of the target, and the target title must be one of the historical Papal Duchies.
		OR = {
			title = d_latium
			title = d_spoleto
			title = d_ancona
			title = d_ferrara
			title = d_benevento

			dejure_liege_title = {
				OR = {
					title = d_latium
					title = d_spoleto
					title = d_ancona
					title = d_ferrara
					title = d_benevento
				}
			}
		}

		any_de_jure_vassal_title = {
			location = {
				region = world_europe_south_italy
			}
		}
	}

	is_valid = {
		# Attacker
		ROOT = {
			OR = {
				# has_character_flag = flag_pepin_for_coronation

				trigger_if = {
					limit = { religion = catholic }

					rightful_religious_head_scope = {
						has_landed_title = k_papacy
					}
				}
				trigger_else_if = {
					limit = { religion = fraticelli }

					rightful_religious_head_scope = {
						has_landed_title = d_fraticelli
					}
				}
			}

			NOR = {
				religion_group = pagan_group
				same_realm = FROM
			}

			pacifist = no
		}
	}

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_success = {
		# Attackers
		attacker = {
			show_scope_change = no

			participation_scaled_piety = 150
			participation_scaled_prestige = 75

			rightful_religious_head_scope = {
				show_scope_change = no

				opinion = {
					name = opinion_papal_land_donation
					who = attacker
				}
			}

			religion_authority = {
				name = won_holy_war
				years = 20
			}

			hidden_effect = {
				if = {
					limit = { has_ambition = obj_strengthen_religion }

					change_variable = {
						which = strengthen_religion
						value = 1
					}
				}
			}

			if = {
				limit = {
					religion_head = {
						any_artifact = {
							has_artifact_flag = christian

							NOR = {
								has_artifact_flag = very_rare
								has_artifact_flag = unique # Won't give the holiest ones away
							}
						}
					}
				}

				random_list = {
					20 = {
						trigger = {
							religion_head = {
								opinion = {
									who = attacker
									value >= 30
								}
							}
						}

						mult_modifier = {
							factor = 1.5

							religion_head = {
								opinion = {
									who = attacker
									value >= 50
								}
							}
						}

						mult_modifier = {
							factor = 1.5

							religion_head = {
								opinion = {
									who = attacker
									value >= 70
								}
							}
						}

						mult_modifier = {
							factor = 1.5

							religion_head = {
								opinion = {
									who = attacker
									value >= 90
								}
							}
						}

						set_character_flag = flag_pope_gifts_artifact # Effect happens in event
					}

					80 = { } # Nothing happens
				}
			}

			character_event = { id = HF.20224 } # Congratulations! Papal Coronation may proceed if flagged
		}

		any_attacker = {
			limit = { NOT = { character = attacker } }

			hidden_effect = {
				participation_scaled_piety = 75
				participation_scaled_prestige = 50

				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -10
				}

				if = {
					limit = { has_ambition = obj_strengthen_religion }

					change_variable = {
						which = strengthen_religion
						value = 1
					}
				}
			}
		}

		defender = {
			show_scope_change = no

			piety = -100
			prestige = -50

			if = {
				limit = {
					NOT = { has_character_modifier = known_satanist }
				}

				religion_authority = {
					name = lost_holy_war
					years = 20
				}
			}
		}
	}

	on_success_title = {
		custom_tooltip = {
			text = pepin_religious_cb_succ_tip

			attacker = {
				religion_head = {
					vassalize_or_take_under_title = {
						title = PREVPREV
						enemy = defender
						same_religion = yes # Only vassalize rulers of my religion
						is_religious = yes
						type = holy_war
					}
				}
			}
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			piety = -50
			prestige = -100
			clr_character_flag = flag_pepin_for_coronation
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			transfer_scaled_wealth = {
				to = defender
				value = 4.0
			}

			piety = -75
			prestige = -150

			religion_authority = {
				name = lost_holy_war
				years = 20
			}

			if = {
				limit = { uses_decadence = yes }
				participation_scaled_decadence = 20
			}

			clr_character_flag = flag_pepin_for_coronation
		}

		any_attacker = {
			limit = {
				NOT = { character = attacker }
				uses_decadence = yes
			}

			hidden_effect = {
				participation_scaled_decadence = 20
			}
		}

		defender = {
			show_scope_change = no

			participation_scaled_piety = 100
			participation_scaled_prestige = 75

			if = {
				limit = { uses_decadence = yes }
				participation_scaled_decadence = -10
			}

			religion_authority = {
				name = won_holy_war
				years = 20
			}

			hidden_effect = {
				if = {
					limit = { has_ambition = obj_strengthen_religion }

					change_variable = {
						which = strengthen_religion
						value = 1
					}
				}
			}
		}
		any_defender = {
			limit = { NOT = { character = defender } }

			hidden_effect = {
				participation_scaled_piety = 75
				participation_scaled_prestige = 50

				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -10
				}

				if = {
					limit = { has_ambition = obj_strengthen_religion }

					change_variable = {
						which = strengthen_religion
						value = 1
					}
				}
			}
		}
	}

	on_invalidation = {
		attacker = {
			clr_character_flag = flag_pepin_for_coronation
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}

	ai_will_do = {
		factor = 1

		trigger = {
			ROOT = {
				sufficient_treasury_for_war_trigger = yes
			}
		}

		coronation_factor = yes

		mult_modifier = {
			factor = 2
			ROOT = { has_landed_title = e_hre }
		}

		mult_modifier = {
			factor = 2
			ROOT = { has_landed_title = e_francia }
		}

		mult_modifier = {
			factor = 0.25

			ROOT = {
				NOT = { trait = zealous }
			}
		}

		# I like the Pope and I will help him
		mult_modifier = {
			factor = 1.5

			ROOT = {
				religion_head = {
					reverse_opinion = {
						who = ROOT
						value >= 50
					}
				}
			}
		}

		mult_modifier = {
			factor = 1.5

			ROOT = {
				religion_head = {
					reverse_opinion = {
						who = ROOT
						value >= 60
					}
				}
			}
		}

		mult_modifier = {
			factor = 1.5

			ROOT = {
				religion_head = {
					reverse_opinion = {
						who = ROOT
						value >= 70
					}
				}
			}
		}

		mult_modifier = {
			factor = 2

			ROOT = {
				religion_head = {
					reverse_opinion = {
						who = ROOT
						value >= 80
					}
				}
			}
		}

		mult_modifier = {
			factor = 2

			ROOT = {
				religion_head = {
					reverse_opinion = {
						who = ROOT
						value >= 90
					}
				}
			}
		}

		mult_modifier = { # Heathens are holding Italy, take action!
			factor = 5

			ROOT = {
				NOT = { trait = cynical }
			}

			FROM = {
				NOT = { religion = ROOT }
			}
		}

		mult_modifier = { # The Papacy doesn't hold Rome, take action!
			factor = 5

			d_latium = {
				OR = {
					holder_scope = {
						OR = {
							character = FROM
							is_vassal_or_below_of = FROM
						}
					}

					any_de_jure_vassal_title = {
						holder = FROM
					}
				}
			}
		}

		mult_modifier = {
			factor = 2

			d_spoleto = {
				OR = {
					holder_scope = {
						OR = {
							character = FROM
							is_vassal_or_below_of = FROM
						}
					}

					any_de_jure_vassal_title = {
						holder = FROM
					}
				}
			}
		}

		mult_modifier = { # Historical tweaking
			factor = 0.25

			d_benevento = {
				OR = {
					holder_scope = {
						OR = {
							character = FROM
							is_vassal_or_below_of = FROM
						}
					}

					any_de_jure_vassal_title = {
						holder = FROM
					}
				}
			}
		}

		mult_modifier = {
			factor = 0.8

			ROOT = {
				distance_from_realm = {
					who = FROM
					value >= 40
				}
			}
		}

		mult_modifier = {
			factor = 0.8

			ROOT = {
				distance_from_realm = {
					who = FROM
					value >= 80
				}
			}
		}

		mult_modifier = {
			factor = 0.75

			ROOT = {
				distance_from_realm = {
					who = FROM
					value >= 120
				}
			}
		}

		mult_modifier = {
			factor = 0.5

			ROOT = {
				distance_from_realm = {
					who = FROM
					value >= 150
				}
			}
		}

		mult_modifier = {
			factor = 0.25

			ROOT = {
				distance_from_realm = {
					who = FROM
					value >= 200
				}
			}
		}
	}
}

# Impious Saint induced Catholic Civil War
depose_impious_pope = {
	name = CB_NAME_DEPOSE_IMPIOUS_POPE
	war_name = WAR_NAME_DEPOSE_IMPIOUS_POPE
	sprite = 29
	truce_days = 0
	sort_priority = 750

	is_permanent = no
	allow_whitepeace = no
	defender_unoccupied_warscore = yes
	allowed_to_target_tributaries = no

	can_use = {
		# Attacker
		ROOT = {
			religion = FROM
		}

		# Defender
		FROM = {
			has_landed_title = k_papacy
			trait = bad_priest_christian
			independent = yes
			religion = catholic
		}
	}

	is_valid = {
		always = no # Disabled in CK2Plus

		# Defender
		FROM = {
			has_landed_title = k_papacy
			trait = bad_priest_christian
			independent = yes
			religion = catholic
		}
	}

	on_success = {
		custom_tooltip = {
			text = depose_impious_pope_cb_win_TT

			any_attacker = {
				piety = 100
			}
		}

		defender = {
			show_scope_change = no

			abdicate = yes

			religion_authority = {
				name = depose_impious_pope_modifier
			}
		}

		hidden_effect = {
			any_defender = {
				set_character_flag = impious_papal_defender
			}

			any_playable_ruler = {
				limit = { religion = catholic }
				narrative_event = { id = HF.40094 } # The Pope loses and has to abdicate
			}

			attacker = {
				narrative_event = { id = HF.40094 } # The Pope loses and has to abdicate
			}

			k_papacy = {
				owner = {
					save_event_target_as = current_pope
					reverse_imprison = defender
				}
			}

			any_attacker = {
				reverse_opinion = {
					name = opinion_grateful
					who = event_target:current_pope
					years = 10
				}
			}
		}
	}

	on_reverse_demand = {
		custom_tooltip = {
			text = depose_impious_pope_cb_lose_TT

			any_attacker = {
				add_trait = excommunicated
				piety = -100
			}
		}

		defender = {
			show_scope_change = no

			religion_authority = {
				name = failed_to_depose_impious_pope_modifier
			}
		}

		hidden_effect = {
			any_defender = {
				set_character_flag = impious_papal_defender
			}

			any_playable_ruler = {
				limit = { religion = catholic }
				narrative_event = { id = HF.40095 } # The Pope wins
			}

			attacker = {
				narrative_event = { id = HF.40095 } # The Pope wins
			}
		}

	}

	on_attacker_leader_death = {
		if = {
			limit = { has_global_flag = pre_catholic_civil_war }
			end_war = invalid
		}
	}

	attacker_ai_victory_worth = {
		factor = -1 # always accept
	}

	attacker_ai_defeat_worth = {
		factor = 100
	}

	defender_ai_victory_worth = {
		factor = -1 # always accept
	}

	defender_ai_defeat_worth = {
		factor = 100
	}
}