# 12
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
	sort_priority = 3000

	is_permanent = yes
	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
	is_crusade = yes
	allowed_to_target_tributaries = no

	can_use_gui = {
		has_game_started = yes
		has_global_flag = plus_rules_applied

		# Attacker
		ROOT = {
			uses_new_crusade = no
			has_called_crusade = no
			religion_authority >= 0.40
			mercenary = no

			trigger_if = {
				limit = {
					has_alternate_start_parameter = {
						key = religion_names
						value = random
					}
				}

				custom_tooltip = {
					text = ALT_GHW_STARTED

					trigger_if = {
						limit = {
							NOT = {
								has_game_rule = {
									name = crusade_rules
									value = activate_immediately
								}
							}
						}

						has_global_flag = muslim_jihads_unlocked
						has_global_flag = pagan_ghws_unlocked
						has_global_flag = christian_crusades_unlocked
					}
				}
			}

			trigger_else = {
				OR = {
					has_game_rule = {
						name = crusade_rules
						value = activate_immediately
					}

					religion_group = zoroastrian_group
					religion_group = jewish_group

					trigger_if = {
						limit = { religion_group = pagan_group }

						custom_tooltip = {
							text = CTT_PAGAN_GHWS_UNLOCKED
							has_global_flag = pagan_ghws_unlocked
						}
					}
					trigger_else_if = {
						limit = { religion_group = christian }

						custom_tooltip = {
							text = CTT_CHRISTIAN_CRUSADES_UNLOCKED
							has_global_flag = christian_crusades_unlocked
						}
					}
					trigger_else_if = {
						limit = { religion_group = muslim }

						custom_tooltip = {
							text = CTT_MUSLIM_JIHADS_UNLOCKED
							has_global_flag = muslim_jihads_unlocked
						}
					}
				}
			}

			# CK2Plus
			can_use_cb_preamble = yes
			has_secret_religion = no
			NOT = { has_character_modifier = crusade_timer }

			trigger_if = {
				limit = { trait = bad_priest_christian }
				has_global_flag = fourth_crusade_happened
			}
		}

		# CK2Plus
		FROM = {
			NOT = { has_character_modifier = holy_truce_timer }
		}

		trigger_if = {
			limit = {
				has_game_rule = {
					name = crusade_delay_rules
					value = one_year
				}
			}
			total_years_played >= 1
		}
		trigger_else_if = {
			limit = {
				has_game_rule = {
					name = crusade_delay_rules
					value = three_years
				}
			}
			total_years_played >= 3
		}
		trigger_else_if = {
			limit = {
				has_game_rule = {
					name = crusade_delay_rules
					value = five_years
				}
			}
			total_years_played >= 5
		}
		trigger_else_if = {
			limit = {
				has_game_rule = {
					name = crusade_delay_rules
					value = ten_years
				}
			}
			total_years_played >= 10
		}
		trigger_else_if = {
			limit = {
				has_game_rule = {
					name = crusade_delay_rules
					value = twenty_years
				}
			}
			total_years_played >= 20
		}
	}

	can_use = {
		# Attacker
		ROOT = {
			can_call_crusade = yes
			uses_new_crusade = no

			# CK2Plus
			is_heretic = no

			# Religion check
			OR = {
				NOT = { religion_group = FROM }

				# One is a heretic of the other
				is_heresy_of = FROM
				is_parent_religion = FROM

				trigger_if = {
					limit = {
						OR = {
							religion_group = pagan_group
							religion = paulician
							religion = kharijite
						}
					}

					NOT = { religion = FROM }
				}

				# CK2Plus
				trigger_else_if = {
					limit = { religion = cathar }
					has_global_flag = cathar_war
				}

				trigger_if = {
					limit = { has_global_flag = fourth_crusade_ongoing }

					OR = {
						religion = catholic
						religion = cathar
						religion = fraticelli
						religion = waldensian
						religion = lollard
						religion = adoptionist
					}
				}
			}

			# Not if the Schism has been mended
			trigger_if = {
				limit = { has_global_flag = schism_mended }

				NOR = {
					religion = catholic
					religion = fraticelli

					# CK2Plus
					religion = cathar
				}
			}
		}

		# Defender
		FROM = {
			# Don't declare Crusades on revolters - chances are high the war will invalidate
			independent = yes
		}
	}

	can_use_title = {
		# Defender
		FROM = {
			num_of_realm_counties = {
				value >= 6
				title = PREV
			}
		}

		# CK2Plus
		trigger_if = {
			limit = { has_holder = yes }

			holder_scope = {
				OR = {
					character = FROM
					is_vassal_or_below_of = FROM
					religion = ROOT
				}
			}
		}
	}

	is_valid = {
		# Attacker
		ROOT = {
			can_call_crusade = yes

			# Religion check
			OR = {
				NOT = { religion_group = FROM }

				# One is a heretic of the other
				is_heresy_of = FROM
				is_parent_religion = FROM

				trigger_if = {
					limit = {
						OR = {
							religion_group = pagan_group
							religion = paulician
							religion = kharijite
						}
					}

					NOT = { religion = FROM }
				}

				# CK2Plus
				trigger_else_if = {
					limit = { religion = cathar }
					has_global_flag = cathar_war
				}

				trigger_if = {
					limit = { has_global_flag = fourth_crusade_ongoing }

					OR = {
						religion = catholic
						religion = cathar
						religion = fraticelli
						religion = waldensian
						religion = lollard
						religion = adoptionist
					}
				}
			}
		}
	}

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

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes

		# CK2Plus
		ROOT = {
			add_character_modifier = {
				name = crusade_timer
				years = 40
				inherit = yes
			}
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with [Root.GetHerHis] crusade for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"

		# Should we give the seized land to its current king?
		if = {
			limit = {
				has_holder = yes

				holder_scope = {
					religion = attacker
					NOT = { same_realm = defender }
				}
			}

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

		# CK2Plus: Should we give the siezed land to its de jure liege?
		else_if = {
			limit = {
				dejure_liege_title = {
					has_holder = yes

					holder_scope = {
						religion = attacker
						NOT = { same_realm = defender }
					}
				}
			}

			dejure_liege_title = {
				holder_scope = {
					gain_settlements_under_title = {
						title = PREVPREV
						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
					}
				}
			}
		}

		# Should we give the seized land to a claimant of our faith?
		else_if = {
			limit = {
				best_crusade_claimant = {
					always = yes # There is a "best" claimant
				}
			}

			# Claimants must be of ROOT's religion, and are ranked by strength of claim and actual contribution to the war
			best_crusade_claimant = {
				show_scope_change = no

				if = {
					limit = { is_ruler = no }
					set_character_flag = set_feudal_gov
				}

				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
				}

				if = {
					limit = {
						top_liege = {
							character = defender
						}
					}

					set_defacto_liege = THIS
				}

				if = { # transfer targeted kingdom title
					limit = {
						PREV = {
							OR = {
								has_holder = no

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

						OR = {
							independent = yes
							is_ruler = no

							liege = {
								real_tier = EMPEROR
							}
						}
					}

					usurp_title = {
						target = PREV
						type = holy_war
					}
				}

				if = {
					limit = { has_character_flag = set_feudal_gov }
					set_government_type = feudal_government
				}

				clr_character_flag = set_feudal_gov
			}
		}

		# Give the seized land to the main contributor of the Crusade
		else = {
			most_participating_crusader = {
				gain_settlements_under_title = {
					title = PREV
					enemy = defender
					type = holy_war
				}

				if = { # transfer targeted kingdom title
					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 }
						}

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

				hidden_effect = {
					if = {
						limit = {
							trigger_if = {
								limit = { has_nickname = yes }
								this_has_lame_nickname_trigger = yes
							}
						}

						random_list = {
							10 = {
								trigger = { religion_group = christian }
								give_nickname = nick_the_crusader
							}

							10 = { give_nickname = nick_the_holy }
							10 = { give_nickname = nick_the_glorious }
							10 = { give_nickname = nick_the_lionheart }
							10 = { give_nickname = nick_the_great }
							10 = { give_nickname = nick_the_hammer }
							10 = { give_nickname = nick_the_pilgrim }

							40 = {
								trigger = { NOT = { religion_group = christian } }
								give_nickname = nick_the_sword_of_god
							}
						}
					}
				}
			}
		}

		any_attacker = {
			limit = { NOT = { character = ROOT } }

			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
		}

		attacker = {
			show_scope_change = no

			participation_scaled_prestige = 500
			participation_scaled_piety = 1000

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

			religion_authority = {
				name = won_crusade
				years = 20
			}

			# CK2Plus
			hidden_effect = {
				if = {
					limit = {
						is_heretic = no

						OR = {
							religion = catholic
							religion = cathar
							religion = fraticelli
							religion = waldensian
							religion = lollard
							religion = adoptionist
						}

						OR = {
							has_global_flag = catholic_crusade_failed
							has_global_flag = fourth_crusade_ongoing
						}
					}

					clr_global_flag = catholic_crusade_failed
					clr_global_flag = fourth_crusade_ongoing
				}
			}
		}

		defender = {
			show_scope_change = no

			piety = -500

			religion_authority = {
				name = lost_crusade
				years = 20
			}

			# CK2Plus
			imperial_decadence_plus_2_effect = yes
			pf_tradition_minus4_effect = yes
		}

		hidden_effect = {
			check_if_reconquista_finished_effect = yes

			defender = {
				landless_pope_effect = yes
			}
		}

		# CK2Plus
		hidden_effect = {
			destroy_tradeposts_effect = yes

			holder_scope = {
				add_character_modifier = {
					name = new_land_timer
					duration = 1
					hidden = yes
				}
			}

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

				prestige = -250
			}

			# Maintenance - shatter defender's empire if it is too small
			defender = {
				character_event = { id = realmPlus.100 }
			}
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			piety = -200
		}

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

			hidden_effect = {
				participation_scaled_piety = 200
				participation_scaled_prestige = 100

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

			# CK2Plus
			pf_tradition_minus2_effect = yes
		}

		defender = {
			show_scope_change = no

			participation_scaled_piety = 200
			participation_scaled_prestige = 100

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

			hidden_effect = {
				roman_victory_triumph_effect = yes

				if = {
					limit = {
						trigger_if = {
							limit = { has_nickname = yes }
							this_has_lame_nickname_trigger = yes
						}
					}

					random_list = {
						10 = { give_nickname = nick_the_holy }
						10 = { give_nickname = nick_the_glorious }
						10 = { give_nickname = nick_the_lionheart }
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_hammer }

						20 = {
							trigger = { NOT = { religion_group = christian } }
							give_nickname = nick_the_sword_of_god
						}
					}
				}
			}

			# CK2Plus
			pf_tradition_minus4_effect = yes
		}

		# CK2Plus
		any_attacker = {
			limit = { NOT = { character = attacker } }
			prestige = -100
		}

		hidden_effect = {
			attacker = {
				if = {
					limit = {
						is_catholic_branch = yes
						is_heretic = no
					}

					set_global_flag = catholic_crusade_failed

					if = {
						limit = { has_global_flag = fourth_crusade_ongoing }
						clr_global_flag = fourth_crusade_ongoing
					}
				}
			}
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			transfer_scaled_wealth = {
				to = defender
				value = 4.0
			}

			piety = -500

			religion_authority = {
				name = lost_crusade
				years = 20
			}

			if = {
				limit = {
					uses_decadence = yes

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

				participation_scaled_decadence = 35
			}

			# CK2Plus
			imperial_decadence_plus_2_effect = yes
			pf_tradition_minus4_effect = yes

			hidden_effect = {
				if = {
					limit = {
						is_catholic_branch = yes
						is_heretic = no
					}

					set_global_flag = catholic_crusade_failed

					if = {
						limit = { has_global_flag = fourth_crusade_ongoing }
						clr_global_flag = fourth_crusade_ongoing
					}
				}
			}
		}

		any_attacker = {
			limit = {
				uses_decadence = yes

				NOR = {
					character = attacker

					any_liege = {
						dynasty = PREV
					}
				}
			}

			hidden_effect = {
				participation_scaled_decadence = 35
				prestige = -250
			}
		}

		defender = {
			show_scope_change = no

			piety = 100
			participation_scaled_piety = 500
			participation_scaled_prestige = 1000

			religion_authority = {
				name = won_crusade
				years = 20
			}

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

			hidden_effect = {
				roman_victory_triumph_effect = yes

				if = {
					limit = {
						trigger_if = {
							limit = { has_nickname = yes }
							this_has_lame_nickname_trigger = yes
						}
					}

					random_list = {
						10 = { give_nickname = nick_the_holy }
						10 = { give_nickname = nick_the_glorious }
						10 = { give_nickname = nick_the_lionheart }
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_hammer }

						20 = {
							trigger = { NOT = { religion_group = christian } }
							give_nickname = nick_the_sword_of_god
						}
					}
				}
			}
		}

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

			hidden_effect = {
				participation_scaled_piety = 500
				participation_scaled_prestige = 1000

				if = {
					limit = { uses_decadence = yes }
					participation_scaled_decadence = -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
	}

	# CK2Plus
	ai_will_do = {
		factor = 1

		trigger = {
			ROOT = {
				sufficient_treasury_for_war_trigger = yes

				trigger_if = {
					limit = { religion = ibadi }
					trait = zealous
				}
			}

			OR = {
				# The target owns a holy site within that kingdom title
				any_de_jure_vassal_title = {
					is_holy_site = ROOT

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

				# The target owns a province of the religion within that kingdom title
				any_de_jure_vassal_title = {
					real_tier = COUNT

					location = {
						religion = ROOT

						owner = {
							OR = {
								character = FROM
								is_vassal_or_below_of = FROM
							}
						}
					}
				}

				# A ruler of the religion either owns a province in that kingdom or is neighboring
				any_de_jure_vassal_title = {
					real_tier = COUNT

					location = {
						OR = {
							owner = { religion = ROOT }

							any_neighbor_province = {
								owner = { religion = ROOT }
							}
						}
					}
				}
			}
		}

		mult_modifier = {
			factor = 0.1

			ROOT = {
				religion_group = muslim
				NOT = { religion = kharijite }
			}

			FROM = { religion = ibadi }
		}
	}
}

# 13: Crusade using the new crusade system. See crusade.info in the religion folder for documentation
new_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
	sort_priority = 3000

	is_permanent = yes
	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
	is_crusade = yes
	allow_whitepeace = no
	allowed_to_target_tributaries = no
	can_ask_to_join_war = no # People join via script instead

	can_use_gui = {
		has_game_started = yes
		has_global_flag = plus_rules_applied

		# Attacker
		ROOT = {
			uses_new_crusade = yes
			has_called_crusade = no
			religion_authority >= 0.40
			mercenary = no

			trigger_if = {
				limit = {
					has_alternate_start_parameter = {
						key = religion_names
						value = random
					}
				}

				custom_tooltip = {
					text = ALT_GHW_STARTED

					trigger_if = {
						limit = {
							NOT = {
								has_game_rule = {
									name = crusade_rules
									value = activate_immediately
								}
							}
						}

						has_global_flag = muslim_jihads_unlocked
						has_global_flag = pagan_ghws_unlocked
						has_global_flag = christian_crusades_unlocked
					}
				}
			}

			trigger_else = {
				religion_group = christian

				custom_tooltip = {
					text = CTT_CHRISTIAN_CRUSADES_UNLOCKED

					OR = {
						has_global_flag = christian_crusades_unlocked

						has_game_rule = {
							name = crusade_rules
							value = activate_immediately
						}
					}
				}
			}

			# CK2Plus
			has_secret_religion = no
			NOT = { has_character_modifier = crusade_timer }

			trigger_if = {
				limit = { trait = bad_priest_christian }
				has_global_flag = fourth_crusade_happened
			}
		}

		# CK2Plus
		FROM = {
			NOT = { has_character_modifier = holy_truce_timer }
		}

		trigger_if = {
			limit = {
				has_game_rule = {
					name = crusade_delay_rules
					value = one_year
				}
			}
			total_years_played >= 1
		}
		trigger_else_if = {
			limit = {
				has_game_rule = {
					name = crusade_delay_rules
					value = three_years
				}
			}
			total_years_played >= 3
		}
		trigger_else_if = {
			limit = {
				has_game_rule = {
					name = crusade_delay_rules
					value = five_years
				}
			}
			total_years_played >= 5
		}
		trigger_else_if = {
			limit = {
				has_game_rule = {
					name = crusade_delay_rules
					value = ten_years
				}
			}
			total_years_played >= 10
		}
		trigger_else_if = {
			limit = {
				has_game_rule = {
					name = crusade_delay_rules
					value = twenty_years
				}
			}
			total_years_played >= 20
		}
	}

	can_use = {
		# Attacker
		ROOT = {
			can_call_crusade = yes
			uses_new_crusade = yes
			NOT = { has_character_modifier = cancel_crusade_cooldown }

			# CK2Plus
			trigger_if = {
				limit = { religion = cathar }
				has_global_flag = cathar_war
			}
		}

		# Defender
		FROM = {
			independent = yes # Don't declare Crusades on revolters - chances are high the war will invalidate

			NOT = { religion = ROOT }

			OR = {
				NOT = { religion_group = ROOT }
				religion_group = pagan_group

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

				# Fourth Crusade
				trigger_if = {
					limit = { has_character_flag = fourth_crusade_target }
					NOT = { has_character_flag = fourth_crusade_recipient }
				}
				trigger_else_if = {
					limit = { has_global_flag = 4th_crusade_official }
					has_landed_title = e_byzantium
				}
			}
		}

		# Not if the Schism has been mended
		trigger_if = {
			limit = { has_global_flag = schism_mended }

			NOR = {
				religion = catholic
				religion = fraticelli

				# CK2Plus
				religion = cathar
			}
		}
	}

	can_use_title = {
		# Defender
		FROM = {
			trigger_if = {
				limit = {
					NOT = {
						num_of_realm_counties = {
							value >= 6
							title = PREV
						}
					}
				}

				PREV = { title = k_greece }

				has_landed_title = e_byzantium
				religion_group = christian

				num_of_realm_counties = {
					value >= 1
					title = PREV
				}
			}
		}

		# CK2Plus
		trigger_if = {
			limit = { has_holder = yes }

			holder_scope = {
				OR = {
					character = FROM
					is_vassal_or_below_of = FROM
					religion = ROOT
				}
			}
		}
	}

	is_valid = {
		# Attacker
		ROOT = {
			can_call_crusade = yes
		}

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

			# Religion check
			OR = {
				NOT = { religion_group = ROOT }

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

				religion_group = pagan_group

				trigger_if = {
					limit = { has_global_flag = 4th_crusade_official }
					has_landed_title = e_byzantium
				}

				trigger_else_if = {
					limit = { has_character_flag = fourth_crusade_target }
					NOT = { has_character_flag = fourth_crusade_recipient }
				}

				# CK2Plus
				ROOT = { religion = paulician }
			}
		}
	}

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

	on_add = {
		fire_haruspicy_event_effect = yes
		anger_china_effect = yes

		# CK2Plus
		ROOT = {
			add_character_modifier = {
				name = "crusade_timer"
				years = 40
				inherit = yes
			}
		}
	}

	on_success_title = {
		log = "[Root.GetBestName] (ID: [Root.GetID]) succeeded with [Root.GetHerHis] Holy Fury crusade for the [This.GetFullName] against [From.GetBestName] (ID: [From.GetID])"

		custom_tooltip = {
			text = new_crusade_win_TT
		}

		# Religion Authority and Prestige/Piety for the winner (Pope)
		attacker = {
			show_scope_change = no

			participation_scaled_prestige = 500
			participation_scaled_piety = 1000

			religion_authority = {
				name = won_crusade
				years = 20
			}

			crusade_target_title = {
				save_global_event_target_as = new_crusade_title
			}

			official_crusade_recipient = {
				save_event_target_as = new_crusade_recipient
			}

			crusade_target_char = {
				save_event_target_as = new_crusade_target_char
			}

			if = {
				limit = {
					is_catholic_branch = yes
					is_heretic = no

					OR = {
						has_global_flag = catholic_crusade_failed
						has_global_flag = fourth_crusade_ongoing
					}
				}

				clr_global_flag = catholic_crusade_failed
				clr_global_flag = fourth_crusade_ongoing
			}

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

		most_participating_crusader = {
			save_event_target_as = new_crusade_most_participating_member
		}

		# Religion Authority and Prestige/Piety loss for the loser (Target of Crusade)
		defender = {
			show_scope_change = no

			piety = -500

			religion_authority = {
				name = lost_crusade
				years = 20
			}

			# CK2Plus
			imperial_decadence_plus_2_effect = yes
			pf_tradition_minus4_effect = yes
		}

		hidden_effect = {
			if = { # Achievement
				limit = {
					event_target:new_crusade_target_char = {
						has_landed_title = e_byzantium
						religion_group = defender
					}
				}

				any_attacker = {
					limit = {
						ai = no
						is_ironman = yes
					}

					set_character_flag = achievement_venetian_guile
				}
			}

			any_attacker = {
				if = {
					limit = {
						ai = yes

						NOT = {
							crusade_beneficiary = {
								always = yes
							}
						}
					}

					character_event = { id = HFP.41007 } # Beneficiary event - AI only
				}
			}

			# Should we give the seized land to the Official Crusade Recipient?
			if = {
				limit = {
					most_participating_crusader = {
						official_crusade_recipient = {
							is_alive = yes
							religion = attacker
						}

						NOR = {
							has_character_flag = crusade_selfish_stance
							has_character_flag = crusade_beneficiary_stance
						}
					}
				}

				most_participating_crusader = {
					official_crusade_recipient = {
						set_crusader_king = THIS
					}
				}
			}

			# Give the seized land to the main contributor of the Crusade or his/her Beneficiary
			else_if = {
				limit = {
					most_participating_crusader = {
						trigger_if = {
							limit = { ai = yes }

							crusade_beneficiary = {
								is_alive = yes
							}
						}

						holy_order = no
						mercenary = no

						NOR = {
							has_landed_title = k_papacy
							has_landed_title = d_fraticelli
						}
					}
				}

				most_participating_crusader = {
					# Give the title to your Beneficiary
					if = {
						limit = {
							crusade_beneficiary = {
								is_alive = yes
								is_ruler = no

								NOT = {
									any_heir_title = {
										always = yes
									}
								}

								mercenary = no
								holy_order = no
							}

							NOT = { has_character_flag = crusade_selfish_stance }
						}

						crusade_beneficiary = {
							set_crusader_king = THIS
							set_character_flag = crusader_king_flag
						}
					}

					# Find an appropriate replacement Beneficiary if there is none selected, or the currently selected one is improper in one way or another
					else_if = {
						limit = {
							ai = yes
							NOT = { has_character_flag = crusade_selfish_stance }

							any_dynasty_member = {
								is_adult = yes
								religion = PREV
								is_ruler = no

								NOT = {
									any_heir_title = {
										always = yes
									}
								}

								mercenary = no
								holy_order = no
							}
						}

						any_dynasty_member = {
							limit = {
								is_adult = yes
								religion = PREV
								is_ruler = no

								NOT = {
									any_heir_title = {
										always = yes
									}
								}
							}

							score_value = {
								value = 1

								additive_modifier = {
									value = 1000
									preferred_gender_based_on_laws_trigger = yes
								}

								additive_modifier = {
									value = 500
									is_child_of = PREV
								}

								additive_modifier = {
									value = 250
									is_vassal_or_below_of = PREV
								}

								additive_modifier = {
									value = 125
									practical_age < 45
								}

								additive_modifier = {
									value = 62
									culture = PREV
								}
							}

							set_crusader_king = THIS
							set_character_flag = crusader_king_flag
						}
					}

					# Take the titles yourself
					else = {
						if = {
							limit = {
								any_owned_bloodline = {
									has_bloodline_flag = grand_crusader_bloodline
									bloodline_is_active_for = PREV
								}
							}
							set_crusade_pot_multiplier = 0.2
						}
						else = {
							set_crusade_pot_multiplier = 0
						}

						set_crusader_king = THIS
						set_character_flag = crusader_king_flag
					}
				}
			}

			# If not applicable, find someone else to take the title
			else = {
				# Find a random Beneficiary within certain limits
				if = {
					limit = {
						most_participating_crusader = {
							any_crusade_participant = {
								is_alive = yes
								is_landed = yes
								holy_order = no
								mercenary = no

								crusade_beneficiary = {
									is_alive = yes
									is_ruler = no
									age >= 12

									NOT = {
										any_heir_title = {
											always = yes
										}
									}
								}
							}
						}
					}

					most_participating_crusader = {
						random_crusade_participant = {
							limit = {
								is_alive = yes
								is_landed = yes
								holy_order = no
								mercenary = no

								crusade_beneficiary = {
									is_alive = yes
									age >= 12
									is_ruler = no

									NOT = {
										any_heir_title = {
											always = yes
										}
									}

								}
							}

							preferred_limit = {
								real_tier = EMPEROR
							}

							preferred_limit = {
								real_tier = KING
							}

							crusade_beneficiary = {
								set_crusader_king = THIS
								set_character_flag = crusader_king_flag
							}
						}
					}
				}

				# Find any random Beneficiary, if possible
				else_if = {
					limit = {
						most_participating_crusader = {
							any_crusade_participant = {
								crusade_beneficiary = {
									is_alive = yes
									is_ruler = no
									holy_order = no

									NOT = {
										any_heir_title = {
											always = yes
										}
									}
								}
							}
						}
					}

					most_participating_crusader = {
						random_crusade_participant = {
							limit = {
								crusade_beneficiary = {
									is_alive = yes
									is_ruler = no
									holy_order = no

									NOT = {
										any_heir_title = {
											always = yes
										}
									}
								}
							}

							crusade_beneficiary = {
								set_crusader_king = THIS
								set_character_flag = crusader_king_flag
							}
						}
					}
				}
				# Find a logical "Beneficiary" from any playable Catholic/Fraticelli ruler, if all else fails
				else = {
					most_participating_crusader = {
						# Try to find a Catholic/Fraticelli ruler
						if = {
							limit = {
								any_playable_ruler = {
									religion = PREV

									any_dynasty_member = {
										is_alive = yes
										is_dying = no
										mercenary = no
										holy_order = no
										age >= 12

										NOT = {
											any_heir_title = {
												always = yes
											}
										}
									}
								}
							}

							random_playable_ruler = {
								limit = {
									religion = PREV

									any_dynasty_member = {
										is_alive = yes
										age >= 12
										is_dying = no
										mercenary = no
										holy_order = no

										NOT = {
											any_heir_title = {
												always = yes
											}
										}
									}
								}

								preferred_limit = {
									real_tier = EMPEROR
									culture = PREV
								}

								preferred_limit = {
									real_tier = EMPEROR
								}

								preferred_limit = {
									real_tier = KING
									culture = PREV
								}

								preferred_limit = {
									real_tier = KING
								}

								preferred_limit = {
									real_tier = DUKE
									culture = PREV
								}

								preferred_limit = {
									real_tier = DUKE
								}

								preferred_limit = {
									culture = PREV
								}

								random_dynasty_member = {
									limit = {
										religion = PREV
										is_alive = yes
										is_dying = no
										mercenary = no
										holy_order = no
										age >= 12

										NOT = {
											any_heir_title = {
												always = yes
											}
										}
									}

									set_crusader_king = THIS
								}
							}
						}

						# If every other option fails, make the Holy Order Grandmaster the Crusader King
						else = {
							set_crusader_king = THIS
						}
					}
				}
			}

			# Find a backup beneficiary for the other Crusaders, if they don't have one already selected
			any_attacker = {
				limit = {
					most_participating_crusader = {
						NOT = { character = PREV }
					}

					ai = yes

					OR = {
						crusade_beneficiary = {
							OR = {
								is_alive = no
								is_landed = yes

								any_heir_title = {
									always = yes
								}

								mercenary = yes
								holy_order = yes
							}
						}

						NOT = {
							crusade_beneficiary = {
								always = yes
							}
						}
					}
				}

				# save_event_target_as = crusade_attacker
				character_event = { id = HFP.41007 } # Beneficiary event - AI only
			}

			# Make sure selfish Crusaders get as little as possible (reduce the pot for Selfish Crusaders)
			any_attacker = {
				limit = { has_character_flag = crusade_selfish_stance }
				set_crusade_pot_multiplier = 0
			}

			# Distribute the Pot and Titles
			attacker = {
				set_global_flag = distributing_crusade

				any_crusade_participant = {
					limit = {
						ai = no

						most_participating_crusader = {
							NOT = {	character = PREV }
						}
					}

					character_event = { id = HFP.41060 } # Pre-event to play as a Beneficiary
				}

				distribute_crusade_pot = {
					amount = 1
				}

				any_crusade_participant = {
					crusade_beneficiary = {
						set_character_flag = beneficiary_who_received_titles
					}
				}

				# If the winner is a selfish Nomad
				if = {
					limit = {
						most_participating_crusader = {
							is_nomadic = yes

							OR = {
								has_character_flag = crusade_selfish_stance

								crusader_king = {
									character = PREV
								}
							}
						}
					}

					most_participating_crusader = {
						gain_settlements_under_title = {
							title = PREVPREV
							enemy = defender
							type = holy_war
						}
					}

					any_playable_ruler = {
						narrative_event = { id = HFP.41160 } # Winning Crusade
					}
				}

				# If it is a Fourth Crusade
				# NOTE: drastically different for CK2Plus
				else_if = {
					limit = {
						has_global_flag = 4th_crusade_official

						crusade_target_title = {
							title = k_greece
						}

						defender = {
							religion_group = attacker
							NOT = { religion = attacker }
						}

						crusade_target_char = {
							has_landed_title = e_byzantium
						}
					}

					# SLIGHTLY less railroading here please and thank you (removed Cyprus and Trebizond changes from vanilla)
					PREV = { #scope to the target title, prep for distribution
						hidden_effect = { destroy_tradeposts_effect = yes }
					}

					# Set safety flags to avoid prestige loss
					# Cleared below in on_success_posttitle
					crusade_target_title = {
						set_title_flag = non_manual_law_change # No prestige loss

						any_de_jure_vassal_title = {
							set_title_flag = non_manual_law_change # No prestige loss
						}
					}

					# Actually distribute the titles
					distribute_crusade_target_title = yes

					# The Crusader King becomes an Emperor and gets a bloodline
					crusader_king = {
						grant_title_no_opinion = e_latin_empire
					}

					e_latin_empire = {
						set_title_flag = non_manual_law_change # No prestige loss
						copy_title_laws = PREVPREV
						add_law = crown_authority_3
						clr_title_flag = non_manual_law_change # No prestige loss
					}

					# Find random Byzantine duke and make a king out of them
					e_byzantium = {
						holder_scope = {
							random_vassal = {
								limit = {
									real_tier = DUKE
									ai = yes
								}

								preferred_limit = {
									de_jure_liege = k_greece
								}

								preferred_limit = {
									de_jure_liege = k_anatolia
								}

								preferred_limit = {
									de_jure_liege = k_sicily
								}

								primary_title = {
									create_title = {
										tier = KING
										landless = no
										temporary = no
										name = fourth_crusade_greek_kingdom_name
										holder = PREV
										culture = PREV
										custom_created = yes
										base_title = THIS
										copy_title_laws = yes
									}
								}
							}
						}
					}

					# All Byzantine vassals go independent except those neighboring the old Emperor, and he/she gets an inferior Empire title while the Byzantine title is broken
					e_byzantium = {
						holder_scope = {
							any_vassal = {
								limit = {
									higher_real_tier_than = BARON

									NAND = { # These stay part of the empire
										ai = yes
										culture = PREV
										religion = PREV
										lower_real_tier_than = KING

										any_demesne_province = {
											owned_by = PREV

											any_neighbor_province = {
												owned_by = PREVPREVPREV
											}
										}
									}
								}

								set_defacto_liege = THIS
							}

							if = {
								limit = {
									primary_title = {
										title = e_byzantium
									}
								}

								capital_scope = {
									create_title = {
										tier = EMPEROR
										landless = no
										temporary = no
										name = fourth_crusade_greek_empire_name
										holder = PREV
										culture = PREV
										custom_created = yes
										base_title = THIS
									}
								}

								any_demesne_title = {
									limit = {
										real_tier = EMPEROR
										is_titular = no
									}

									destroy_landed_title = THIS
								}
							}
						}
					}

					# Crete is taken by a Merchant Lord
					if = {
						limit = {
							c_kaneia = {
								holder_scope = {
									ai = yes

									OR = {
										any_war = {
											using_cb = new_crusade
											any_defender = { character = PREVPREV }
										}

										any_liege = {
											any_war = {
												using_cb = new_crusade
												any_defender = { character = PREVPREV }
											}
										}
									}
								}
							}

							c_chandax = {
								holder_scope = {
									ai = yes

									OR = {
										any_war = {
											using_cb = new_crusade
											any_defender = { character = PREVPREV }
										}

										any_liege = {
											any_war = {
												using_cb = new_crusade
												any_defender = { character = PREVPREV }
											}
										}
									}
								}
							}

							any_crusade_participant = {
								is_patrician = yes
								higher_real_tier_than = COUNT
							}
						}

						random_crusade_participant = {
							limit = {
								is_patrician = yes
								higher_real_tier_than = COUNT
							}

							preferred_limit = {
								has_landed_title = k_venice
							}

							preferred_limit = {
								has_landed_title = d_venice
							}

							preferred_limit = {
								real_tier = EMPEROR
							}

							preferred_limit = {
								real_tier = KING
							}

							grant_title_no_opinion = d_krete
							grant_title_no_opinion = c_kaneia
							grant_title_no_opinion = c_chandax
						}
					}

					if = {
						limit = {
							official_crusade_recipient = {
								has_character_flag = fourth_crusade_recipient
							}
						}

						official_crusade_recipient = {
							save_event_target_as = byzantine_refugee
						}
					}

					# Narrative event explaining that Byzantium has been taken out
					any_character = {
						narrative_event = { id = HF.49008 } # Byzantium falls
					}

					event_target:byzantine_refugee = {
						death = {
							death_reason = death_battle
							killer = defender
						}
					}

					# Making sure the Byzantine Empire is destroyed
					e_byzantium = {
						unsafe_destroy_landed_title = THIS
					}
				}

				# Otherwise
				else = {
					defender = {
						any_player = {
							limit = {
								is_vassal_or_below_of = PREV
								religion = attacker
								higher_real_tier_than = BARON
							}

							set_defacto_liege = THIS
						}
					}

					# Set safety flags to avoid prestige loss
					# Cleared below in on_success_posttitle
					crusade_target_title = {
						set_title_flag = non_manual_law_change # No prestige loss

						any_de_jure_vassal_title = {
							set_title_flag = non_manual_law_change # No prestige loss
						}
					}

					# Actually distribute the titles
					distribute_crusade_target_title = yes

					crusader_king = {
						save_event_target_as = crusade_king_recipient
					}

					any_playable_ruler = {
						narrative_event = { id = HFP.41160 } # Winning Crusade
					}
				}

				crusader_king = {
					if = {
						limit = {
							has_character_flag = beneficiary_who_received_titles
							NOT = { has_dynasty_modifier = ruling_in_crusader_kingdom }
						}

						add_dynasty_modifier = ruling_in_crusader_kingdom
					}

					any_realm_character = {
						limit = {
							has_character_flag = beneficiary_who_received_titles
							NOT = { has_dynasty_modifier = ruling_in_crusader_kingdom }
						}

						add_dynasty_modifier = ruling_in_crusader_kingdom
					}
				}

				clr_global_flag = distributing_crusade
			}

			# If the CK has too much gold
			crusader_king = {
				if = {
					limit = {
						wealth >= 2000
						ai = yes
						num_of_vassals >= 1
					}

					while = {
						count = 20

						limit = { wealth >= 2000 }

						random_vassal = {
							limit = { wealth <= 1000 }

							wealth = 350

							crusader_king = {
								wealth = -350
							}
						}
					}
				}
			}

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

				set_character_flag = achievement_crusader
			}

			# Nickname for the most participating Crusader
			most_participating_crusader = {
				if = {
					limit = { can_apply_average_nickname_trigger = yes }

					random_list = {
						10 = { give_nickname = nick_the_crusader }
						10 = { give_nickname = nick_the_glorious }
						10 = { give_nickname = nick_the_lionheart }
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_hammer }
						40 = { give_nickname = nick_the_sword_of_god }
					}
				}
			}

			# Nickname and effects for the new Crusader King, if it is a Beneficiary or the Official Crusade Recipient (not for Selfish Crusaders)
			crusader_king = {
				hidden_effect = {
					if = {
						limit = {
							OR = {
								most_participating_crusader = {
									NOT = { character = PREV }
								}

								attacker = {
									official_crusade_recipient = {
										character = PREV
									}
								}
							}
						}

						opinion = {
							name = opinion_granted_crusader_kingdom
							who = most_participating_crusader
							years = 15
						}

						crusader_king = {
							setting_up_the_crusader_kingdom_effect = yes
							create_crusader_king_crown_effect = yes
						}
					}
				}
			}

			crusader_king = {
				save_event_target_as = new_crusader_king
			}

			# If the player is the most participating Crusader, and gave away the Kingdom to a Beneficiary, trigger event to let them start playing as Beneficiary
			if = {
				limit = {
					most_participating_crusader = {
						ai = no
						dynasty = event_target:new_crusader_king
						NOT = { character = event_target:new_crusader_king }

					}
				}

				most_participating_crusader = {
					narrative_event = {
						id = HFP.41054 # Play as Crusader King / Queen
						days = 3
					}
				}
			}

			# Check most participating Crusader if they are potential Crusader material
			most_participating_crusader = {
				if = {
					limit = {
						NOR = {
							has_character_flag = grandCrusader
							trait = crusader_king
						}
					}

					set_character_flag = grandCrusader
				}
				else = {
					narrative_event = { id = HF.49020 }
				}
			}

			# Release the CK if they are in jail
			crusader_king = {
				imprison = no
			}

			# Special bloodline for the recipient of the first Crusade
			if = {
				limit = {
					NOT = { has_global_flag = first_crusade_complete }

					most_participating_crusader = {
						crusader_king = {
							NOT = { character = PREV }
						}
					}
				}

				if = {
					limit = { has_dlc = "Holy Fury" }

					crusader_king = {
						if = {
							limit = { is_female = yes }

							create_bloodline = {
								type = crusade_lord
								religion = THIS
								inheritance = matrilineal
							}
						}
						else = {
							create_bloodline = {
								type = crusade_lord
								religion = THIS
								inheritance = patrilineal
							}
						}

						set_bloodline_founder_religion_flag_effect = yes
					}
				}
			}

			# Checks if First Crusade
			if = {
				limit = {
					NOT = { has_global_flag = first_crusade_complete }
				}

				set_global_flag = first_crusade_complete
			}

			# Adds a negative opinion modifier for selfish Crusaders
			any_crusade_participant = {
				limit = {
					OR = {
						has_character_flag = crusade_selfish_stance

						AND = {
							NOR = {
								has_character_flag = crusade_papal_stance
								has_character_flag = crusade_beneficiary_stance
							}

							most_participating_crusader = {
								crusader_king = {
									character = PREV
									character = PREVPREV
								}
							}
						}
					}
				}

				if = {
					limit = {
						NOT = {
							attacker = {
								has_opinion_modifier = {
									name = opinion_selfish_crusader
									who = PREVPREV
								}
							}
						}
					}

					attacker = {
						opinion = {
							name = opinion_selfish_crusader
							who = PREVPREV
							years = 20
						}
					}
				}
			}

			# Clean up stances
			any_attacker = {
				clr_character_flag = crusade_selfish_stance
				clr_character_flag = crusade_beneficiary_stance
				clr_character_flag = crusade_papal_stance
			}

			attacker = {
				character_event = { id = HFP.41199 }
			}
		}

		hidden_effect = {
			check_if_reconquista_finished_effect = yes

			defender = {
				landless_pope_effect = yes
			}
		}

		# CK2Plus

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

				participation_scaled_piety = 1000
				participation_scaled_prestige = 500

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

			any_defender = {
				limit = { NOT = { character = defender } }
				prestige = -250
			}

			# Maintenance - shatter FROM empire if it's become too small
			defender = {
				character_event = { id = realmPlus.100 }
			}
		}
	}

	on_success_posttitle = {
		# Clear safety flags used to avoid prestige loss
		# Needs to happen here since laws are not evaluated instantly when distributing titles
		# new_crusade_title must be a global event target, regular event targets don't seem to transfer between these on_success_ clauses
		event_target:new_crusade_title = {
			clr_title_flag = non_manual_law_change # No prestige loss

			any_de_jure_vassal_title = {
				clr_title_flag = non_manual_law_change # No prestige loss
			}
		}

		clear_global_event_target = new_crusade_title
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			piety = -200
			character_event = { id = HFP.41199 } # Cleanup Event

			# CK2Plus
			if = {
				limit = {
					is_catholic_branch = yes
					is_heretic = no
				}

				set_global_flag = catholic_crusade_failed

				if = {
					limit = { has_global_flag = fourth_crusade_ongoing }
					clr_global_flag = fourth_crusade_ongoing
				}
			}
		}

		# CK2Plus
		any_attacker = {
			limit = { NOT = { character = attacker } }
			prestige = -100
		}

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

			hidden_effect = {
				participation_scaled_piety = 200
				participation_scaled_prestige = 100

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

			# CK2Plus
			pf_tradition_minus2_effect = yes
		}

		defender = {
			show_scope_change = no

			participation_scaled_piety = 200
			participation_scaled_prestige = 100

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

			hidden_effect = {
				if = {
					limit = {
						trigger_if = {
							limit = { has_nickname = yes }
							this_has_lame_nickname_trigger = yes
						}
					}

					random_list = {
						10 = { give_nickname = nick_the_holy }
						10 = { give_nickname = nick_the_glorious }
						10 = { give_nickname = nick_the_lionheart }
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_hammer }

						20 = {
							trigger = { NOT = { religion_group = christian } }
							give_nickname = nick_the_sword_of_god
						}
					}
				}

				roman_victory_triumph_effect = yes
			}

			# CK2Plus
			pf_tradition_minus4_effect = yes
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			transfer_scaled_wealth = {
				to = defender
				value = 4.0
			}

			piety = -500

			religion_authority = {
				name = lost_crusade
				years = 20
			}

			character_event = { id = HFP.41199 } # Cleanup Event

			if = {
				limit = {
					uses_decadence = yes

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

				participation_scaled_decadence = 35
			}

			# CK2Plus
			imperial_decadence_plus_2_effect = yes
			pf_tradition_minus4_effect = yes

			if = {
				limit = {
					is_catholic_branch = yes
					is_heretic = no
				}

				set_global_flag = catholic_crusade_failed

				if = {
					limit = { has_global_flag = fourth_crusade_ongoing }
					clr_global_flag = fourth_crusade_ongoing
				}
			}
		}

		any_attacker = {
			limit = {
				uses_decadence = yes

				NOR = {
					character = attacker

					any_liege = {
						dynasty = PREV
					}
				}
			}

			hidden_effect = {
				participation_scaled_decadence = 35

				# CK2Plus
				prestige = -250
			}
		}

		defender = {
			show_scope_change = no

			piety = 100
			participation_scaled_piety = 500
			participation_scaled_prestige = 1000

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

			religion_authority = {
				name = won_crusade
				years = 20
			}

			hidden_effect = {
				if = {
					limit = {
						trigger_if = {
							limit = { has_nickname = yes }
							this_has_lame_nickname_trigger = yes
						}
					}

					random_list = {
						10 = { give_nickname = nick_the_holy }
						10 = { give_nickname = nick_the_glorious }
						10 = { give_nickname = nick_the_lionheart }
						10 = { give_nickname = nick_the_great }
						10 = { give_nickname = nick_the_hammer }

						20 = {
							trigger = { NOT = { religion_group = christian } }
							give_nickname = nick_the_sword_of_god
						}
					}
				}

				roman_victory_triumph_effect = yes
			}
		}

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

			hidden_effect = {
				participation_scaled_piety = 500
				participation_scaled_prestige = 1000

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

		# Checks if First Crusade
		if = {
			limit = {
				NOT = { has_global_flag = first_crusade_complete }
			}

			set_global_flag = first_crusade_complete
		}

		any_defender = {
			limit = {
				ai = no
				religion_group = pagan_group
				is_ironman = yes
			}

			set_character_flag = achievement_pagan_fury
		}

		# Chance to start a Shepherd's Crusade
		attacker = {
			show_scope_change = no

			random = {
				chance = 15

				if = {
					limit = {
						has_game_rule = {
							name = shepherds_crusade
							value = on
						}

						religion = catholic
					}

					trigger_switch = {
						on_trigger = religion

						catholic = { set_global_flag = catholic_shepherds_crusade }
						fraticelli = { set_global_flag = fraticelli_shepherds_crusade }
					}
				}

				character_event = {
					id = HFP.42300 # Start of event chain
					days = 3
				}
			}
		}
	}

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

# 14: Regular Holy War CB - Replaced by catch-all 'holy_war' in CK2Plus
religious = {
	name = CB_NAME_RELIGIOUS
	war_name = WAR_NAME_RELIGIOUS
	sprite = 9
	truce_days = 3650
	sort_priority = 800

	is_permanent = yes
	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 = no

	can_use_gui = {
		# Attacker
		ROOT = {
			trigger_if = {
				limit = { religion = buddhist }
				piety >= 250
			}
		}
	}

	can_use = {
		always = no # Disabled in CK2Plus

		# Attacker
		ROOT = {
			# General requirements
			is_nomadic = no
			pacifist = no
			mercenary = no

			NOR = {
				religion_group = pagan_group
				religion = taoist
				same_realm = FROM
			}

			# Northern Crusade
			trigger_if = {
				limit = { has_global_flag = northern_crusades_active }

				NOR = {
					has_landed_title = k_teutonic_state
					has_landed_title = d_teutonic_order
				}
			}

			# Reconquista check
			reconquista_check_trigger = no
			NOT = { has_character_flag = currently_reconquering_iberia }

			# Hindu check
			trigger_if = {
				limit = {
					religion = hindu
					FROM = { has_dharmic_religion_trigger = yes }
				}

				FROM = { has_character_modifier = known_satanist }
				NOT = { has_character_modifier = known_satanist }
			}

			# Requirements for holy war in same religion group
			trigger_if = {
				limit = { religion_group = FROM }

				OR = {
					# One is a heretic of the other
					is_heresy_of = FROM
					is_parent_religion = FROM

					# Dharmics can fight Taoists
					trigger_if = {
						limit = { has_dharmic_religion_trigger = yes }
						FROM = { religion = taoist }
					}

					# Can holy war known demon worshippers in he same religion group
					trigger_if = {
						limit = { FROM = { has_character_modifier = known_satanist } }
						NOT = { has_character_modifier = known_satanist }
					}
				}
			}

			# Requirements for Muslims
			trigger_else_if = {
				limit = { religion_group = muslim }

				NOT = { religion = FROM }

				FROM = {
					NOT = { # Not fighting religious enemies
						any_current_enemy = {
							NOT = { religion_group = muslim }
						}
					}
				}
			}

			top_liege = {
				religion_group = ROOT

				# Can holy war known devil worshippers of top liege's religion
				trigger_if = {
					limit = { religion = FROM }

					FROM = { has_character_modifier = known_satanist }
					NOT = { has_character_modifier = known_satanist }

					ROOT = {
						NOT = { has_character_modifier = known_satanist }
					}
				}

				# Religion must match, or neither is a heretic
				OR = {
					religion = ROOT

					AND = {
						is_heretic = no
						ROOT = { is_heretic = no }
					}
				}
			}
		}
	}

	can_use_title = {
		# The attacker needs at least one county in the target duchy, or a border, or be only two sea zones away from one of your counties
		any_direct_de_jure_vassal_title = {
			OR = {
				holder_scope = { # attacker owns a county in target duchy
					OR = {
						character = ROOT
						is_vassal_or_below_of = ROOT
					}
				}

				location = {
					any_neighbor_province = {
						trigger_if = {
							limit = { has_owner = yes } # not wasteland or sea zone

							owner = {
								OR = {
									character = ROOT
									is_vassal_or_below_of = ROOT
								}
							}
						}

						trigger_else = {
							is_land = no # first sea zone

							OR = { # religion requirements for attack across sea zones
								ROOT = { religion_group = muslim }
								FROM = { religion_group = muslim }

								# CK2Plus
								has_game_rule = {
									name = crusade_rules
									value = activate_immediately
								}

								trigger_if = {
									limit = { ROOT = { religion_group = pagan_group } }
									has_global_flag = pagan_ghws_unlocked
								}
								trigger_else_if = {
									limit = { ROOT = { religion_group = christian } }
									has_global_flag = christian_crusades_unlocked
								}
								trigger_else = {
									ROOT = { religion_group = muslim }
									has_global_flag = muslim_jihads_unlocked
								}
							}

							any_neighbor_province = { # county one sea zone away
								trigger_if = {
									limit = { has_owner = yes } # not wasteland or sea zone

									owner = {
										OR = {
											character = ROOT
											is_vassal_or_below_of = ROOT
										}
									}
								}

								trigger_else = {
									is_land = no # second seazone

									any_neighbor_province = { # county two sea zones away
										owner = {
											OR = {
												character = ROOT
												is_vassal_or_below_of = ROOT
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}

	is_valid = {
		# Attacker
		ROOT = {
			NOR = {
				religion_group = pagan_group
				same_realm = FROM
			}

			pacifist = no

			# Requirements for holy war in same religion group
			trigger_if = {
				limit = { religion_group = FROM }
				OR = {
					# One is a heretic of the other
					is_heresy_of = FROM
					is_parent_religion = FROM

					# Dharmics can fight Taoists
					trigger_if = {
						limit = { has_dharmic_religion_trigger = yes }
						FROM = { religion = taoist }
					}

					# Can holy war known demon worshippers in the same religion group
					AND = {
						FROM = { has_character_modifier = known_satanist }
						NOT = { has_character_modifier = known_satanist }
					}
				}
			}

			# Requirements for Muslims
			trigger_else_if = {
				limit = { religion_group = muslim }
				NOT = { religion = FROM }
			}

			top_liege = {
				trigger_if = {
					limit = { religion = FROM }

					FROM = { has_character_modifier = known_satanist }
					NOT = { has_character_modifier = known_satanist }

					ROOT = {
						NOT = { has_character_modifier = known_satanist }
					}
				}
			}
		}
	}

	on_add = {
		attacker = {
			show_scope_change = no

			if = {
				limit = { religion = buddhist }
				piety = -250
			}
		}

		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_success = {
		attacker = {
			show_scope_change = no

			participation_scaled_piety = 100
			participation_scaled_prestige = 50

			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_attacker = {
			limit = { NOT = { character = attacker } }

			hidden_effect = {
				participation_scaled_piety = 100
				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 = {
		attacker = {
			show_scope_change = no

			if = { # Standard Holy War behaviour for Non-Muslims and Muslims fighting other Muslims
				limit = {
					trigger_if = {
						limit = { religion_group = muslim }
						religion_group = defender
					}
				}

				custom_tooltip = {
					text = religious_cb_succ_tip

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

			else = { # Muslims fighting Non-Muslims preserve vassals
				custom_tooltip = {
					text = religious_cb_succ_muslim_tip

					vassalize_or_take_under_title = {
						title = PREV
						enemy = defender
						is_religious = yes
						type = holy_war
					}
				}
			}
		}

		hidden_effect = {
			check_war_effect = yes

			defender = {
				landless_pope_effect = yes
			}
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			piety = -50
			prestige = -100
		}
	}

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

		any_attacker = {
			limit = {
				uses_decadence = yes

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

			hidden_effect = {
				participation_scaled_decadence = 20
			}
		}

		defender = {
			show_scope_change = no

			participation_scaled_piety = 200
			participation_scaled_prestige = 100

			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 = 200
				participation_scaled_prestige = 100

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

				if = {
					limit = { has_ambition = obj_strengthen_religion }

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

	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 = {
			trigger_if = {
				limit = {
					ROOT = {
						is_tribal = yes
					}

					FROM = {
						is_nomadic = yes
					}
				}

				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
							holding_type = tribal
						}
					}
				}
			}
			trigger_else_if = {
				limit = {
					ROOT = {
						is_nomadic = no
						is_tribal = no
					}

					FROM = {
						is_nomadic = yes
					}

				}

				any_direct_de_jure_vassal_title = {
					any_direct_de_jure_vassal_title = {
						OR = {
							holding_type = castle
							holding_type = city
						}
					}
				}
			}
		}

		coronation_factor = yes

		# Try to guide the Hordes in the right direction
		mult_modifier = {
			factor = 0.1

			ROOT = {
				OR = {
					dynasty = 613 # Seljuks
					dynasty = 800 # Timurids

					primary_title = {
						title = e_ilkhanate
					}
				}
			}

			OR = {
				empire = {
					NOR = {
						title = e_persia
						title = e_byzantium
						title = e_arabia
					}
				}

				FROM = {
					primary_title = {
						title = e_golden_horde
					}

					year < 1300
				}
			}
		}

		mult_modifier = {
			factor = 0.1

			ROOT = {
				primary_title = {
					title = e_golden_horde
				}
			}

			OR = {
				empire = {
					OR = {
						title = e_persia
						title = e_byzantium
						title = e_arabia
					}
				}

				FROM = {
					primary_title = {
						title = e_ilkhanate
					}

					year < 1300
				}
			}
		}

		mult_modifier = {
			factor = 2.0

			OR = {
				culture = persian
				culture = baloch
				culture = afghan
				culture = turkish
			}

			empire = {
				OR = {
					title = e_persia
					title = e_rajastan
				}
			}
		}
	}
}

liberate_religion = { # New6
	name = CB_NAME_LIBERATE_RELIGION
	war_name = WAR_NAME_LIBERATE_RELIGION
	sprite = 35
	truce_days = 3650
	sort_priority = 4000

	is_permanent = yes
	is_holy_war = yes
	check_de_jure_tier = KING
	apply_short_occ_mod = no # Do not apply the 'recently_conquered' modifier to Holdings
	allowed_to_target_tributaries = no

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

	can_use = {
		OR = {
			has_dlc = "Holy Fury"
			has_dlc = "Jade Dragon"
		}

		# Attacker
		ROOT = {
			independent = yes

			OR = {
				any_owned_bloodline = {
					has_bloodline_flag = bloodline_religious_liberation
					bloodline_is_active_for = PREV
				}

				trigger_if = {
					limit = { has_character_flag = had_liberate_opportunity }

					NOT = {
						has_game_rule = {
							name = jade_dragon_cbs
							value = off
						}
					}
				}
			}

			is_nomadic = no
			pacifist = no
			mercenary = no

			NOR = {
				same_realm = FROM
				religion = FROM
				has_character_flag = used_liberate_opportunity
			}
		}
	}

	can_use_title = {
		any_de_jure_vassal_title = {
			real_tier = COUNT

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

				NOT = { religion = ROOT }
			}

			location = {
				religion = ROOT
			}
		}

		# The attacker needs at least one county in the target kingdom, or a border, or be only two sea zones away from one of your counties
		any_de_jure_vassal_title = {
			OR = {
				holder_scope = { # own a county
					OR = {
						character = ROOT
						is_vassal_or_below_of = ROOT
					}
				}

				location = {
					any_neighbor_province = { # have a border
						trigger_if = {
							limit = { has_owner = yes }

							owner = {
								OR = {
									character = ROOT
									is_vassal_or_below_of = ROOT
								}
							}
						}
						trigger_else = {
							is_land = no # first sea zone

							OR = {
								ROOT = { religion_group = muslim }
								FROM = { religion_group = muslim }

								trigger_if = {
									limit = { ROOT = { religion_group = pagan_group } }
									has_global_flag = pagan_ghws_unlocked
								}
								trigger_else_if = {
									limit = { ROOT = { religion_group = christian } }
									has_global_flag = christian_crusades_unlocked
								}
								trigger_else = {
									ROOT = { religion_group = muslim }
									has_global_flag = muslim_jihads_unlocked
								}

								# CK2Plus
								has_game_rule = {
									name = crusade_rules
									value = activate_immediately
								}
							}

							any_neighbor_province = {
								trigger_if = {
									limit = { has_owner = yes }

									owner = {
										OR = {
											character = ROOT
											is_vassal_or_below_of = ROOT
										}
									}
								}
								trigger_else = {
									is_land = no # second sea zone

									any_neighbor_province = {
										owner = {
											OR = {
												character = ROOT
												is_vassal_or_below_of = ROOT
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}

	is_valid = {
		# Attacker
		ROOT = {
			pacifist = no

			NOR = {
				same_realm = FROM
				religion = FROM
			}

			top_liege = {
				NOT = { religion = FROM }
			}
		}
	}

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

	on_add = {
		attacker = {
			hidden_effect = {
				if = {
					limit = {
						NOT = {
							any_owned_bloodline = {
								has_bloodline_flag = bloodline_religious_liberation
								bloodline_is_active_for = PREV
							}
						}
					}

					set_character_flag = used_liberate_opportunity
				}
			}
		}

		fire_haruspicy_event_effect = yes
		anger_china_effect = yes
	}

	on_success = {
		attacker = {
			show_scope_change = no

			participation_scaled_piety = 500
			participation_scaled_prestige = 250

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

			religion_authority = {
				name = won_holy_war
				years = 20
			}
		}

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

			hidden_effect = {
				participation_scaled_piety = 100
				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
		}
	}

	on_success_title = {
		custom_tooltip = {
			text = religious_liberation_cb_succ_tip

			defender = {
				random_realm_title = {
					limit = {
						real_tier = COUNT

						kingdom = { title = PREVPREVPREV }
					}

					preferred_limit = {
						location = {
							religion = ROOT
						}
					}

					location = {
						create_character = {
							religion = attacker
							culture = THIS
							dynasty = random
							random_traits = yes
							female = no
							age = 20
							health = 7
							fertility = 0.7
							flag = is_religious_liberation_ruler

							attributes = {
								martial = 10
								diplomacy = 10
								stewardship = 10
							}
						}

						new_character = {
							remove_trait = cynical
							add_trait = zealous
							wealth = 500
							prestige = 500
							piety = 500
							save_event_target_as = new_ruler

							usurp_title = {
								target = PREVPREV
								type = invasion
							}
						}
					}
				}
			}

			if = {
				limit = {
					has_holder = yes

					holder_scope = {
						OR = {
							character = defender
							is_vassal_or_below_of = defender
						}
					}
				}

				usurp_title_only = {
					target = event_target:new_ruler
					type = invasion
				}

				make_primary_title = yes
			}
			else = {
				event_target:new_ruler = {
					create_title = {
						tier = KING
						landless = no
						temporary = no
						custom_created = yes
						culture = event_target:new_ruler
						holder = event_target:new_ruler
						name = LIBERATED_KINGDOM
						base_title = PREV
					}
				}
			}

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

				set_defacto_liege = THIS
			}

			attacker = {
				if = {
					limit = { has_ambition = obj_strengthen_religion }

					change_variable = {
						which = strengthen_religion
						value = 1
					}
				}

				make_tributary = {
					who = event_target:new_ruler
					tributary_type = permanent
				}
			}
		}

		hidden_effect = {
			check_if_reconquista_finished_effect = yes

			defender = {
				landless_pope_effect = yes
			}
		}
	}

	on_fail = {
		attacker = {
			show_scope_change = no

			piety = -125
			prestige = -250
		}
	}

	on_invalidation = {
		attacker = {
			clr_character_flag = used_liberate_opportunity
		}
	}

	on_reverse_demand = {
		attacker = {
			show_scope_change = no

			transfer_scaled_wealth = {
				to = defender
				value = 4.0
			}

			piety = -250
			prestige = -500

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

			religion_authority = {
				name = lost_holy_war
				years = 20
			}
		}

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

			hidden_effect = {
				participation_scaled_decadence = 20
			}
		}

		defender = {
			show_scope_change = no

			participation_scaled_piety = 200
			participation_scaled_prestige = 100

			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 = 200
				participation_scaled_prestige = 100

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

				if = {
					limit = { has_ambition = obj_strengthen_religion }

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

	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

				trigger_if = {
					limit = { controls_religion = yes }
					is_landed = yes
				}
			}
		}

		coronation_factor = yes
	}
}

