# pledge_crusade_effect
# unpledge_crusade_effect
# defender_pledge_crusade_effect
# defender_unpledge_crusade_effect
# contribute_to_crusade_pot_effect
# crusade_give_artifact_effect
# select_crusade_receiver_stance_effect
# setting_up_the_crusader_kingdom_effect
# battle_commander_counter_positive_effect
# battle_commander_counter_negative_effect
# crusader_commander_counter_positive_effect
# crusader_commander_counter_negative_effect
# create_crusader_king_crown_effect
# crusade_flavor_event_tombola_effect
# alternate_crusade_artifact_distribution_effect
# reconquista_win_effect
# reconquista_lose_effect
# check_if_reconquista_finished_effect
# check_war_effect
# move_childrens_crusade_region_effect

# Referenced by the "Join Crusade" button in the Crusade view for Crusades using the new Crusade system
pledge_crusade_effect = {
	pledge_crusade_participation = yes
	piety = 100

	add_character_modifier = {
		name = recently_pledged_to_crusade_cooldown
		days = 45
		hidden = yes
	}

	trigger_switch = {
		on_trigger = real_tier

		EMPEROR = { add_to_crusade_prestige_pot = 2000 add_to_crusade_piety_pot = 800 }
		KING    = { add_to_crusade_prestige_pot = 1000 add_to_crusade_piety_pot = 400 }
		DUKE    = { add_to_crusade_prestige_pot =  500 add_to_crusade_piety_pot = 200 }
		COUNT   = { add_to_crusade_prestige_pot =  250 add_to_crusade_piety_pot = 100 }
		BARON   = { add_to_crusade_prestige_pot =  125 add_to_crusade_piety_pot = 100 }
	}

	random = {
		chance = 50
		add_to_crusade_artifact_pot = 1
	}

	if = {
		limit = { trait = excommunicated }

		letter_event = { id = HFP.41057 }
	}

	remove_character_modifier = not_pledged_to_crusade
}

# Referenced by the "Leave Crusade" button in the Crusade view for Crusades using the new Crusade system
unpledge_crusade_effect = {
	pledge_crusade_participation = no
	piety = -300

	add_character_modifier = {
		name = recently_unpledged_to_crusade_cooldown
		years = 1
		hidden = yes
	}

	trigger_switch = {
		on_trigger = tier

		EMPEROR = { add_to_crusade_prestige_pot = -2000 add_to_crusade_piety_pot = -800 }
		KING    = { add_to_crusade_prestige_pot = -1000 add_to_crusade_piety_pot = -400 }
		DUKE    = { add_to_crusade_prestige_pot =  -500 add_to_crusade_piety_pot = -200 }
		COUNT   = { add_to_crusade_prestige_pot =  -250 add_to_crusade_piety_pot = -100 }
		BARON   = { add_to_crusade_prestige_pot =  -125 add_to_crusade_piety_pot = -100 }
	}

	random = {
		chance = 50
		add_to_crusade_artifact_pot = -1
	}

	add_character_modifier = {
		name = not_pledged_to_crusade
		years = 3
	}

	set_character_flag = asked_to_pledge
}

# Referenced by the "Join Crusade" button in the Crusade defender view for Crusades using the new Crusade system
defender_pledge_crusade_effect = {
	pledge_crusade_defense = yes
}

# Referenced by the "Leave Crusade" button in the Crusade defender view for Crusades using the new Crusade system
defender_unpledge_crusade_effect = {
	pledge_crusade_defense = no
}

# Referenced by the "Contribute to Pot" button in the Crusade view for Crusades using the new Crusade system
contribute_to_crusade_pot_effect = {
	character_event = { id = HFP.41088 }
}

# Referenced by getting artifacts from a Crusade
crusade_give_artifact_effect = {
	crusade_target_title = {
		save_event_target_as = crusade_title
	}

	random_province = {
		limit = {
			OR = {
				has_province_modifier = tomb_of_a_saint
				has_province_modifier = pillaged_tomb_of_a_saint
			}
		}

		event_target:province_saint = {
			save_event_target_as = found_saint
		}
	}

	random_list = {
		# Crusade Weapons
		20 = {
			trigger = {
				NOR = {
					has_artifact = sword_tier_2_new
					has_artifact = sword_tier_3_vengeance
				}
			}

			mult_modifier = {
				factor = 0.05

				OR = {
					has_artifact = lance_tier_2_new
					has_artifact = lance_tier_3_impaler
					has_artifact = axe_tier_2_new
					has_artifact = axe_tier_3_ravager
					has_artifact = mace_tier_2_new
					has_artifact = mace_tier_3_bone_breaker
				}
			}

			set_character_flag = sword_tier_1

			if = {
				limit = { has_character_flag = crusader_king_flag }
				add_artifact = sword_tier_3_vengeance
			}
			else = {
				add_artifact = sword_tier_2_new
			}

			new_artifact = {
				set_name = crusade_artifact_name
				set_description = crusade_artifact_description_sword
			}

			clr_character_flag = sword_tier_1
		}

		20 = {
			trigger = {
				NOR = {
					has_artifact = lance_tier_2_new
					has_artifact = lance_tier_3_impaler
				}
			}

			mult_modifier = {
				factor = 0.05

				OR = {
					has_artifact = sword_tier_2_new
					has_artifact = sword_tier_3_vengeance
					has_artifact = axe_tier_2_new
					has_artifact = axe_tier_3_ravager
					has_artifact = mace_tier_2_new
					has_artifact = mace_tier_3_bone_breaker
				}
			}

			set_character_flag = lance_tier_2

			if = {
				limit = { has_character_flag = crusader_king_flag }
				add_artifact = lance_tier_3_impaler
			}
			else = {
				add_artifact = lance_tier_2_new
			}

			new_artifact = {
				set_name = crusade_artifact_name
				set_description = crusade_artifact_description_lance
			}

			clr_character_flag = lance_tier_2
		}

		20 = {
			trigger = {
				NOR = {
					has_artifact = axe_tier_2_new
					has_artifact = axe_tier_3_ravager
				}
			}

			mult_modifier = {
				factor = 0.05

				OR = {
					has_artifact = sword_tier_2_new
					has_artifact = sword_tier_3_vengeance
					has_artifact = lance_tier_2_new
					has_artifact = lance_tier_3_impaler
					has_artifact = mace_tier_2_new
					has_artifact = mace_tier_3_bone_breaker
				}
			}

			set_character_flag = axe_tier_2

			if = {
				limit = { has_character_flag = crusader_king_flag }
				add_artifact = axe_tier_3_ravager
			}
			else = {
				add_artifact = axe_tier_2_new
			}

			new_artifact = {
				set_name = crusade_artifact_name
				set_description = crusade_artifact_description_ax
			}

			clr_character_flag = axe_tier_2
		}

		20 = {
			trigger = {
				NOR = {
					has_artifact = mace_tier_2_new
					has_artifact = mace_tier_3_bone_breaker
				}
			}

			mult_modifier = {
				factor = 0.05

				OR = {
					has_artifact = sword_tier_2_new
					has_artifact = sword_tier_3_vengeance
					has_artifact = lance_tier_2_new
					has_artifact = lance_tier_3_impaler
					has_artifact = axe_tier_2_new
					has_artifact = axe_tier_3_ravager
				}
			}

			set_character_flag = mace_tier_2

			if = {
				limit = { has_character_flag = crusader_king_flag }
				add_artifact = mace_tier_3_bone_breaker
			}
			else = {
				add_artifact = mace_tier_2_new
			}

			new_artifact = {
				set_name = crusade_artifact_name
				set_description = crusade_artifact_description_mace
			}

			clr_character_flag = mace_tier_2
		}

		# Random Christian associated artifacts
		5 = { add_artifact = fragment_of_true_cross }
		5 = { add_artifact = holy_foreskin }
		5 = { add_artifact = weeping_statue }

		# Crusade Armor
		20 = {
			trigger = {
				NOT = { has_artifact = armor_tier_2 }
			}

			set_character_flag = armor_tier_2

			if = {
				limit = { has_character_flag = crusader_king_flag }
				add_artifact = armor_tier_3_champion
			}
			else = {
				add_artifact = armor_tier_2
			}

			new_artifact = {
				set_name = crusade_artifact_name
				set_description = crusade_artifact_description_armor
			}

			clr_character_flag = armor_tier_2
		}

		# Artifacts based on placeholder Saints
		2 = {
			trigger = {
				event_target:found_saint = { always = yes }
			}

			add_artifact = saintly_finger

			new_artifact = {
				set_name = crusade_saintly_finger_name
				set_description = crusade_saintly_finger_description
			}
		}

		2 = {
			trigger = {
				event_target:found_saint = { always = yes }
			}

			add_artifact = saintly_bone

			new_artifact = {
				set_name = crusade_saintly_bone_name
				set_description = crusade_saintly_bone_description
			}
		}

		2 = {
			trigger = {
				event_target:found_saint = { always = yes }
			}

			add_artifact = saintly_skull_tier_1

			new_artifact = {
				set_name = crusade_saintly_skull_name
				set_description = crusade_saintly_skull_description
			}
		}

		2 = {
			trigger = {
				event_target:found_saint = { always = yes }
			}

			add_artifact = saintly_skull_tier_2

			new_artifact = {
				set_name = crusade_saintly_skull_name
				set_description = crusade_saintly_skull_description
			}
		}

		2 = {
			trigger = {
				event_target:found_saint = { always = yes }
			}

			add_artifact = saintly_tongue

			new_artifact = {
				set_name = crusade_saintly_tongue_name
				set_description = crusade_saintly_tongue_description
			}
		}

		# Artifacts based on placeholder Saints
		2 = {
			mult_modifier = {
				factor = 0.5
				event_target:found_saint = { always = yes }
			}

			add_artifact = saintly_finger

			new_artifact = {
				set_name = crusade_saintly_finger_name_placeholder
				set_description = crusade_saintly_finger_description_placeholder
			}
		}

		2 = {
			mult_modifier = {
				factor = 0.5
				event_target:found_saint = { always = yes }
			}

			add_artifact = saintly_bone

			new_artifact = {
				set_name = crusade_saintly_bone_name_placeholder
				set_description = crusade_saintly_bone_description_placeholder
			}
		}

		2 = {
			mult_modifier = {
				factor = 0.5
				event_target:found_saint = { always = yes }
			}

			add_artifact = saintly_skull_tier_1

			new_artifact = {
				set_name = crusade_saintly_skull_name_placeholder
				set_description = crusade_saintly_skull_description_placeholder
			}
		}

		2 = {
			mult_modifier = {
				factor = 0.5
				event_target:found_saint = { always = yes }
			}

			add_artifact = saintly_skull_tier_2

			new_artifact = {
				set_name = crusade_saintly_skull_name_placeholder
				set_description = crusade_saintly_skull_description_placeholder
			}
		}

		2 = {
			mult_modifier = {
				factor = 0.5
				event_target:found_saint = { always = yes }
			}

			add_artifact = saintly_tongue

			new_artifact = {
				set_name = crusade_saintly_tongue_name_placeholder
				set_description = crusade_saintly_tongue_description_placeholder
			}
		}
	}

	clr_character_flag = crusader_king_flag
}

# Referenced by the "Select Receiver Stance" button in the Crusade view for Crusades using the new Crusade system
select_crusade_receiver_stance_effect = {
	character_event = { id = HFP.41091 }
}

# Crusader King/Queen set up
setting_up_the_crusader_kingdom_effect = {
	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 }
		}
	}

	if = {
		limit = { is_female = no }
		add_trait = crusader_king
	}
	else = {
		add_trait = crusader_queen
	}

	character_event = { id = HFP.41090 days = 1 }
	character_event = { id = HFP.41090 days = 3 }

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

		save_persistent_event_target = {
			name = coronation_priest_per
			scope = ROOT
		}

		add_trait = crowned_by_pope
	}

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

	any_realm_lord = {
		limit = {
			NOR = {
				government = theocracy_government
				government = republic_government
			}
		}

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

	reverse_religion = capital_scope

	if = {
		limit = { wealth < 200 }

		clear_wealth = yes
		wealth = 200
	}

	any_vassal = {
		limit = {
			NOT = { religion_group = christian }
		}

		set_defacto_liege = THIS
	}
}

battle_commander_counter_positive_effect = {
	crusader_commander_counter_positive_effect = yes
}

battle_commander_counter_negative_effect = {
	crusader_commander_counter_negative_effect = yes
}

# Crusader Commander counter
crusader_commander_counter_positive_effect = {
	if = {
		limit = {
			OR = {
				trigger_if = {
					limit = {
						any_war = {
							using_cb = new_crusade
						}
					}

					has_pledged_crusade_participation = yes
				}

				any_liege = {
					any_war = {
						using_cb = new_crusade
					}

					has_pledged_crusade_participation = yes
				}
			}

			event_target:enemy_commander = {
				NOT = { religion = ROOT }

				OR = {
					any_war = {
						using_cb = new_crusade
					}

					any_liege = {
						any_war = {
							using_cb = new_crusade
						}
					}
				}
			}
		}

		if = {
			limit = {
				check_variable = {
					which = crusadeCommander
					value >= 1
				}
			}

			change_variable = {
				which = crusadeCommander
				value = 1
			}
		}
		else = {
			set_variable = {
				which = crusadeCommander
				value = 1
			}
		}

		if = {
			limit = {
				check_variable = {
					which = crusadeCommander
					value >= 20
				}

				NOT = {
					any_owned_bloodline = {
						has_bloodline_flag = battle_survivor_bloodline

						ROOT = {
							is_bloodline_founder_of = PREV
						}
					}
				}
			}

			narrative_event = { id = HF.49021 }
		}
	}
}

crusader_commander_counter_negative_effect = {
	if = {
		limit = {
			OR = {
				trigger_if = {
					limit = {
						any_war = {
							using_cb = new_crusade
						}
					}

					has_pledged_crusade_participation = yes
				}

				any_liege = {
					any_war = {
						using_cb = new_crusade
					}

					has_pledged_crusade_participation = yes
				}
			}

			event_target:enemy_commander = {
				NOT = { religion = ROOT }

				OR = {
					any_war = {
						using_cb = new_crusade
					}

					any_liege = {
						any_war = {
							using_cb = new_crusade
						}
					}
				}
			}
		}

		if = {
			limit = {
				check_variable = {
					which = crusadeCommander
					value >= 1
				}
			}

			change_variable = {
				which = crusadeCommander
				value = -1
			}
		}
	}
}

create_crusader_king_crown_effect = { # Applied after Crusade is over, on the Beneficiary
	if = {
		limit = {
			higher_real_tier_than = DUKE

			NOR = {
				any_artifact = { has_artifact_flag = crusader_crown }
				primary_title = { has_title_flag = flag_title_crusader_crown } # Spawn only once per-Kingdom
			}
		}

		random = {
			chance = 40

			mult_modifier = {
				factor = 1.75
				has_landed_title = k_jerusalem
			}

			mult_modifier = {
				factor = 1.5
				capital_scope = { region = world_middle_east_arabia }
			}

			# Let this kind of artifact only spawn once per conquered Crusader Kingdom
			primary_title = {
				set_title_flag = flag_title_crusader_crown
			}

			add_artifact = crown_crusader_king

			new_artifact = {
				set_description = crown_crusader_king_desc_1

				# Randomize names to avoid too much repetition
				random_list = {
					20 = { set_name = crown_crusader_king_random_1 }
					20 = { set_name = crown_crusader_king_random_2 }
					20 = { set_name = crown_crusader_king_random_3 }
					20 = { set_name = crown_crusader_king_random_4 }
					20 = { set_name = crown_crusader_king_random_5 }
					20 = { set_name = crown_crusader_king_random_6 }
					20 = { set_name = crown_crusader_king_random_7 }
					20 = { set_name = crown_crusader_king_random_8 }
				}
			}
		}
	}
}

crusade_flavor_event_tombola_effect = {
	random_list = {
		# Nothing Happens
		90 = {
			trigger = {
				NOR = {
					has_global_flag = qa_testing
					has_character_flag = northern_crusade_flavor
					has_character_flag = reconquista_flavor
				}
			}
		}

		######################
		### Generic Events ###
		######################

		# Meet other Crusaders
		3 = {
			trigger = {
				ai = no

				any_crusade_participant = {
					ai = yes
					is_adult = yes

					NOR = {
						is_foe = ROOT
						is_friend = ROOT
					}
				}

				NOR = {
					has_character_flag = northern_crusade_flavor
					has_character_flag = reconquista_flavor
				}
			}

			character_event = { id = HFP.42201 }
		}

		# Seeing the local culture
		3 = {
			trigger = {
				NOT = { culture = location }
			}

			character_event = { id = HFP.42202 }
		}

		# Dead Crusaders
		3 = {
			character_event = { id = HFP.42203 }
		}

		# Meeting caravan
		3 = {
			trigger = {
				NOT = { has_character_modifier = crusade_event_meeting_caravan_cooldown }
			}

			character_event = { id = HFP.42204 }
		}

		# Reminiscing about home
		1 = {
			trigger = {
				NOT = { has_character_modifier = crusade_event_reminiscing_about_home_cooldown }
			}

			character_event = { id = HFP.42205 }
		}

		# The Holy Comet
		3 = {
			character_event = { id = HFP.42206 }
		}

		# Questioning the faith
		1 = {
			trigger = {
				NOT = { has_character_modifier = crusade_event_questioning_the_faith_cooldown }
			}

			character_event = { id = HFP.42207 }
		}

		# The little group of Faithful
		1 = {
			trigger = {
				ai = no
				is_alternate_start = no
			}

			character_event = { id = HFP.42208 }
		}

		# The little thief
		2 = {
			character_event = { id = HFP.42209 }
		}

		# The wise man
		3 = {
			character_event = { id = HFP.42210 }
		}

		# Coming across a local festival
		3 = {
			character_event = { id = HFP.42211 }
		}

		# Guided by God or Alcohol
		2 = {
			trigger = {
				NOR = {
					has_character_flag = northern_crusade_flavor
					has_character_flag = reconquista_flavor
				}

				trigger_if = {
					limit = { is_title_active = d_teutonic_order }
					religion = d_teutonic_order
				}
				trigger_else_if = {
					limit = { is_title_active = k_teutonic_state }
					religion = k_teutonic_state
				}
				trigger_else_if = {
					limit = { is_title_active = d_knights_templar }
					religion = d_knights_templar
				}
				trigger_else_if = {
					limit = { is_title_active = d_livonian_order }
					religion = d_livonian_order
				}
				trigger_else = {
					is_title_active = d_knights_hospitaller
					religion = d_knights_hospitaller
				}
			}

			character_event = { id = HFP.42212 }
		}

		# Meeting a Holy Order
		3 = {
			trigger = {
				ai = no

				NOR = {
					has_character_flag = northern_crusade_flavor
					has_character_flag = reconquista_flavor
				}

				any_playable_ruler = {
					holy_order = yes
					religion = ROOT

					NOT = {
						has_opinion_modifier = {
							name = opinion_insulted
							who = ROOT
						}
					}
				}
			}

			character_event = { id = HFP.42213 }
		}

		# Crusader Brothel
		3 = {
			character_event = { id = HFP.42214 }
		}

		# The men are restless - unused
		# 3 = {
		# 	trigger = {
		# 		NOT = { has_character_modifier = crusade_great_general }
		# 	}
		#
		# 	character_event = { id = HFP.42215 }
		# }
	}
}

alternate_crusade_artifact_distribution_effect = {
	custom_tooltip = {
		text = alternate_crusade_artifact_distribution_effect_TT

		random_list = {
			# Crusade Weapons
			20 = {
				trigger = {
					NOR = {
						has_artifact = sword_tier_2_new
						has_artifact = sword_tier_3_vengeance
					}
				}

				mult_modifier = {
					factor = 0.05

					OR = {
						has_artifact = lance_tier_2_new
						has_artifact = lance_tier_3_impaler
						has_artifact = axe_tier_2_new
						has_artifact = axe_tier_3_ravager
						has_artifact = mace_tier_2_new
						has_artifact = mace_tier_3_bone_breaker
					}
				}

				set_character_flag = sword_tier_1

				if = {
					limit = { has_character_flag = crusader_king_flag }
					add_artifact = sword_tier_3_vengeance
				}
				else = {
					add_artifact = sword_tier_2_new
				}

				new_artifact = {
					set_name = alternate_crusade_artifact_name
					set_description = alternate_crusade_artifact_description
				}

				clr_character_flag = sword_tier_1
			}

			20 = {
				trigger = {
					NOR = {
						has_artifact = lance_tier_2_new
						has_artifact = lance_tier_3_impaler
					}
				}

				mult_modifier = {
					factor = 0.05

					OR = {
						has_artifact = sword_tier_2_new
						has_artifact = sword_tier_3_vengeance
						has_artifact = axe_tier_2_new
						has_artifact = axe_tier_3_ravager
						has_artifact = mace_tier_2_new
						has_artifact = mace_tier_3_bone_breaker
					}
				}

				set_character_flag = lance_tier_2

				if = {
					limit = { has_character_flag = crusader_king_flag }
					add_artifact = lance_tier_3_impaler
				}
				else = {
					add_artifact = lance_tier_2_new
				}

				new_artifact = {
					set_name = alternate_crusade_artifact_name
					set_description = alternate_crusade_artifact_description
				}

				clr_character_flag = lance_tier_2
			}

			20 = {
				trigger = {
					NOR = {
						has_artifact = axe_tier_2_new
						has_artifact = axe_tier_3_ravager
					}
				}

				mult_modifier = {
					factor = 0.05

					OR = {
						has_artifact = sword_tier_2_new
						has_artifact = sword_tier_3_vengeance
						has_artifact = lance_tier_2_new
						has_artifact = lance_tier_3_impaler
						has_artifact = mace_tier_2_new
						has_artifact = mace_tier_3_bone_breaker
					}
				}

				set_character_flag = axe_tier_2

				if = {
					limit = { has_character_flag = crusader_king_flag }
					add_artifact = axe_tier_3_ravager
				}
				else = {
					add_artifact = axe_tier_2_new
				}

				new_artifact = {
					set_name = alternate_crusade_artifact_name
					set_description = alternate_crusade_artifact_description
				}

				clr_character_flag = axe_tier_2
			}

			20 = {
				trigger = {
					NOR = {
						has_artifact = mace_tier_2_new
						has_artifact = mace_tier_3_bone_breaker
					}
				}

				mult_modifier = {
					factor = 0.05

					OR = {
						has_artifact = sword_tier_2_new
						has_artifact = sword_tier_3_vengeance
						has_artifact = lance_tier_2_new
						has_artifact = lance_tier_3_impaler
						has_artifact = axe_tier_2_new
						has_artifact = axe_tier_3_ravager
					}
				}

				set_character_flag = mace_tier_2

				if = {
					limit = { has_character_flag = crusader_king_flag }
					add_artifact = mace_tier_3_bone_breaker
				}
				else = {
					add_artifact = mace_tier_2_new
				}

				new_artifact = {
					set_name = alternate_crusade_artifact_name
					set_description = alternate_crusade_artifact_description
				}

				clr_character_flag = mace_tier_2
			}

			# Random Christian associated artifacts
			5 = { add_artifact = fragment_of_true_cross }
			5 = { add_artifact = holy_foreskin }
			5 = { add_artifact = weeping_statue }

			# Crusade Armor
			20 = {
				trigger = {
					NOT = { has_artifact = armor_tier_2 }
				}

				set_character_flag = armor_tier_2

				if = {
					limit = { has_character_flag = crusader_king_flag }
					add_artifact = armor_tier_3_champion
				}
				else = {
					add_artifact = armor_tier_2
				}

				new_artifact = {
					set_name = alternate_crusade_artifact_name
					set_description = alternate_crusade_artifact_description
				}

				clr_character_flag = armor_tier_2
			}

			# Artifacts based on placeholder Saints
			2 = { add_artifact = saintly_finger }
			2 = { add_artifact = saintly_bone }
			2 = { add_artifact = saintly_skull_tier_1 }
			2 = { add_artifact = saintly_skull_tier_2 }
			2 = { add_artifact = saintly_tongue }
		}
	}
}

reconquista_win_effect = {
	attacker = {
		show_scope_change = no

		if = {
			limit = {
				event_target:first_duchy_title = {
					holder_scope = {
						OR = {
							character = defender
							is_liege_or_above = defender
						}

						primary_title = {
							NOT = { title = PREVPREV }
						}
					}
				}
			}

			destroy_landed_title = event_target:first_duchy_title
		}

		if = {
			limit = {
				event_target:second_duchy_title = {
					holder_scope = {
						OR = {
							character = defender
							is_liege_or_above = defender
						}

						primary_title = {
							NOT = { title = PREVPREV }
						}
					}
				}
			}

			destroy_landed_title = event_target:second_duchy_title
		}

		if = {
			limit = {
				event_target:third_duchy_title = {
					holder_scope = {
						OR = {
							character = defender
							is_liege_or_above = defender
						}

						primary_title = {
							NOT = { title = PREVPREV }
						}
					}
				}
			}

			destroy_landed_title = event_target:third_duchy_title
		}

		# if = {
		#	limit = {
		#		any_dynasty_member = {
		#			has_character_flag = reconquista_beneficiary_flag
		#			is_alive = yes
		#		}
		#	}
		#
		#	random_dynasty_member = {
		#		show_scope_change = no
		#
		#		limit = {
		#			has_character_flag = reconquista_beneficiary_flag
		#			is_alive = yes
		#		}
		#
		#		event_target:first_duchy_title = {
		#			show_scope_change = no
		#
		#			custom_tooltip = {
		#				text = reconquista_vassalize_TT
		#
		#				PREV = {
		#					vassalize_or_take_under_title_destroy_duchies = {
		#						title = event_target:first_duchy_title
		#						enemy = defender
		#						same_religion = yes # Only vassalize rulers of my religion
		#						is_religious = yes
		#						type = holy_war
		#					}
		#				}
		#			}
		#		}
		#
		#		event_target:second_duchy_title = {
		#			show_scope_change = no
		#
		#			custom_tooltip = {
		#				text = reconquista_vassalize_TT
		#
		#				PREV = {
		#					vassalize_or_take_under_title_destroy_duchies = {
		#						title = event_target:second_duchy_title
		#						enemy = defender
		#						same_religion = yes # Only vassalize rulers of my religion
		#						is_religious = yes
		#						type = holy_war
		#					}
		#				}
		#			}
		#		}
		#
		#		event_target:third_duchy_title = {
		#			show_scope_change = no
		#
		#			custom_tooltip = {
		#				text = reconquista_vassalize_TT
		#
		#				PREV = {
		#					vassalize_or_take_under_title_destroy_duchies = {
		#						title = event_target:third_duchy_title
		#						enemy = defender
		#						same_religion = yes # Only vassalize rulers of my religion
		#						is_religious = yes
		#						type = holy_war
		#					}
		#				}
		#			}
		#		}
		#
		#		hidden_effect = {
		#			if = {
		#				limit = {
		#					event_target:first_duchy_title = {
		#						trigger_if = {
		#							limit = { has_holder = yes }
		#
		#							holder_scope = {
		#								OR = {
		#									character = defender
		#									is_liege_or_above = defender
		#								}
		#							}
		#						}
		#
		#						any_direct_de_jure_vassal_title = {
		#							holder = PREVPREV
		#						}
		#
		#					}
		#				}
		#
		#				grant_title_no_opinion = event_target:first_duchy_title
		#			}
		#
		#			if = {
		#				limit = {
		#					event_target:second_duchy_title = {
		#						trigger_if = {
		#							limit = { has_holder = yes }
		#
		#							holder_scope = {
		#								OR = {
		#									character = defender
		#									is_liege_or_above = defender
		#								}
		#							}
		#						}
		#
		#						any_direct_de_jure_vassal_title = {
		#							holder = PREVPREV
		#						}
		#					}
		#				}
		#
		#				grant_title_no_opinion = event_target:second_duchy_title
		#			}
		#
		#			if = {
		#				limit = {
		#					event_target:third_duchy_title = {
		#						trigger_if = {
		#							limit = { has_holder = yes }
		#
		#							holder_scope = {
		#								OR = {
		#									character = defender
		#									is_liege_or_above = defender
		#								}
		#							}
		#						}
		#
		#						any_direct_de_jure_vassal_title = {
		#							holder = PREVPREV
		#						}
		#					}
		#				}
		#
		#				grant_title_no_opinion = event_target:third_duchy_title
		#			}
		#
		#			if = {
		#				limit = {
		#					event_target:reconquista_kingdom_title = {
		#						trigger_if = {
		#							limit = { has_holder = yes }
		#
		#							holder_scope = {
		#								OR = {
		#									character = defender
		#									is_liege_or_above = defender
		#								}
		#							}
		#						}
		#					}
		#
		#					has_landed_title = event_target:first_duchy_title
		#
		#					OR = {
		#						has_landed_title = event_target:second_duchy_title
		#
		#						NOT = {
		#							event_target:second_duchy_title = {
		#								always = yes
		#							}
		#						}
		#					}
		#
		#					OR = {
		#						has_landed_title = event_target:third_duchy_title
		#
		#						NOT = {
		#							event_target:third_duchy_title = {
		#								always = yes
		#							}
		#						}
		#					}
		#				}
		#
		#				grant_title_no_opinion = event_target:reconquista_kingdom_title
		#
		#				event_target:first_duchy_title = {
		#					de_jure_liege = event_target:reconquista_kingdom_title
		#				}
		#
		#				event_target:second_duchy_title = {
		#					de_jure_liege = event_target:reconquista_kingdom_title
		#				}
		#
		#				event_target:third_duchy_title = {
		#					de_jure_liege = event_target:reconquista_kingdom_title
		#				}
		#			}
		#
		#			if = {
		#				limit = {
		#					NOR = {
		#						has_landed_title = event_target:first_duchy_title
		#						has_landed_title = event_target:second_duchy_title
		#						has_landed_title = event_target:third_duchy_title
		#						has_landed_title = event_target:reconquista_kingdom_title
		#					}
		#				}
		#
		#				event_target:first_duchy_title = {
		#					create_title = {
		#						tier = DUKE
		#						holder = PREV
		#						custom_created = yes
		#						culture = PREV
		#						base_title = THIS
		#						name = reconquista_duchy_name
		#					}
		#				}
		#			}
		#
		#			holy_order_distribute_titles_effect = yes
		#			set_defacto_liege = THIS
		#		}
		#
		#		character_event = {
		#			id = HFP.41090
		#			days = 1
		#		}
		#
		#		attacker = {
		#			show_scope_change = no
		#
		#			make_tributary = {
		#				who = PREV
		#			}
		#		}
		#	}
		# }

		# else = {
			event_target:first_duchy_title = {
				show_scope_change = no

				custom_tooltip = {
					text = reconquista_vassalize_TT

					PREV = {
						vassalize_or_take_under_title_destroy_duchies = {
							title = event_target:first_duchy_title
							enemy = defender
							same_religion = yes # Only vassalize rulers of my religion
							is_religious = yes
							type = holy_war
						}
					}
				}
			}

			event_target:second_duchy_title = {
				show_scope_change = no

				custom_tooltip = {
					text = reconquista_vassalize_TT

					PREV = {
						vassalize_or_take_under_title_destroy_duchies = {
							title = event_target:second_duchy_title
							enemy = defender
							same_religion = yes # Only vassalize rulers of my religion
							is_religious = yes
							type = holy_war
						}
					}
				}
			}

			event_target:third_duchy_title = {
				show_scope_change = no

				custom_tooltip = {
					text = reconquista_vassalize_TT

					PREV = {
						vassalize_or_take_under_title_destroy_duchies = {
							title = event_target:third_duchy_title
							enemy = defender
							same_religion = yes # Only vassalize rulers of my religion
							is_religious = yes
							type = holy_war
						}
					}
				}
			}

			hidden_effect = {
				if = {
					limit = {
						event_target:first_duchy_title = {
							trigger_if = {
								limit = { has_holder = yes }

								holder_scope = {
									OR = {
										character = defender
										is_liege_or_above = defender
									}
								}
							}

							any_direct_de_jure_vassal_title = {
								holder = attacker
							}

						}
					}

					grant_title_no_opinion = event_target:first_duchy_title
				}

				if = {
					limit = {
						event_target:second_duchy_title = {
							trigger_if = {
								limit = { has_holder = yes }

								holder_scope = {
									OR = {
										character = defender
										is_liege_or_above = defender
									}
								}
							}

							any_direct_de_jure_vassal_title = {
								holder = attacker
							}
						}
					}

					grant_title_no_opinion = event_target:second_duchy_title
				}

				if = {
					limit = {
						event_target:third_duchy_title = {
							trigger_if = {
								limit = { has_holder = yes }

								holder_scope = {
									OR = {
										character = defender
										is_liege_or_above = defender
									}
								}
							}

							any_direct_de_jure_vassal_title = {
								holder = attacker
							}
						}
					}

					grant_title_no_opinion = event_target:third_duchy_title
				}

				if = {
					limit = {
						event_target:reconquista_kingdom_title = {
							trigger_if = {
								limit = { has_holder = yes }

								holder_scope = {
									OR = {
										character = defender
										is_liege_or_above = defender
									}
								}
							}
						}

						has_landed_title = event_target:first_duchy_title

						OR = {
							has_landed_title = event_target:second_duchy_title

							NOT = {
								event_target:second_duchy_title = {
									always = yes
								}
							}
						}

						OR = {
							has_landed_title = event_target:third_duchy_title

							NOT = {
								event_target:third_duchy_title = {
									always = yes
								}
							}
						}

						ai = yes
					}

					grant_title_no_opinion = event_target:reconquista_kingdom_title

					event_target:first_duchy_title = {
						de_jure_liege = event_target:reconquista_kingdom_title
					}

					event_target:second_duchy_title = {
						de_jure_liege = event_target:reconquista_kingdom_title
					}

					event_target:third_duchy_title = {
						de_jure_liege = event_target:reconquista_kingdom_title
					}
				}

				if = {
					limit = {
						NOR = {
							has_landed_title = event_target:first_duchy_title
							has_landed_title = event_target:second_duchy_title
							has_landed_title = event_target:third_duchy_title
							has_landed_title = event_target:reconquista_kingdom_title
						}

						ai = yes
					}

					event_target:first_duchy_title = {
						create_title = {
							tier = DUKE
							holder = PREV
							custom_created = yes
							culture = PREV
							base_title = THIS
							name = reconquista_duchy_name
						}
					}
				}

				if = {
					limit = { ai = yes }
					holy_order_distribute_titles_effect = yes
				}

				if = {
					limit = { has_character_flag = reconquista_adventurer }

					set_defacto_liege = ROOT # attacker

					primary_title = {
						add_law = succ_primogeniture
						add_law = revoke_title_law_2
						add_law = ze_administration_laws_0
						add_law = law_voting_power_0
						add_law = war_voting_power_0
						add_law = revoke_title_voting_power_0
						add_law = imprison_voting_power_0
						add_law = grant_title_voting_power_0
						add_law = banish_voting_power_0
						add_law = execution_voting_power_0
						add_law = succession_voting_power_0
						add_law = centralization_3
					}

					character_event = {
						id = HFP.41090
						days = 1
					}
				}
			}
		# }

		religion_authority = {
			name = reconquista_won
			years = 20
		}

		any_attacker = {
			hidden_effect = {
				if = {
					limit = {
						check_variable = {
							which = reconquista_attacker
							value >= 1
						}
					}

					change_variable = {
						which = reconquista_attacker
						value = 1
					}
				}
				else = {
					set_variable = {
						which = reconquista_attacker
						value = 1
					}
				}
				if = {
					limit = {
						check_variable = {
							which = reconquista_attacker
							value >= 4
						}

						check_variable = {
							which = reconquista_defender
							value < 2
						}

						religion_group = christian
					}

					narrative_event = { id = HF.49221 }
				}
			}
		}

		check_if_reconquista_finished_effect = yes

		character_event = { id = HF.49200 }
	}
}

reconquista_lose_effect = {
	attacker = {
		show_scope_change = no

		disband_event_forces = reconquista_event_troops

		transfer_scaled_wealth = {
			to = defender
			value = 5.0
		}

		religion_authority = {
			name = reconquista_lost
			years = 20
		}

		character_event = { id = HF.49200 }
	}

	any_attacker = {
		show_scope_change = no

		piety = -100
	}

	defender = {
		show_scope_change = no

		reverse_imprison = attacker

		religion_authority = {
			name = stopped_reconquista
			years = 20
		}

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

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

	any_defender = {
		show_scope_change = no

		participation_scaled_piety = 200

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

		hidden_effect = {
			if = {
				limit = {
					check_variable = {
						which = reconquista_defender
						value >= 1
					}
				}

				change_variable = {
					which = reconquista_defender
					value = 1
				}
			}
			else = {
				set_variable = {
					which = reconquista_defender
					value = 1
				}
			}

			if = {
				limit = {
					check_variable = {
						which = reconquista_defender
						value >= 2
					}

					check_variable = {
						which = reconquista_attacker
						value >= 2
					}

					religion_group = christian
				}

				narrative_event = { id = HF.49220 }
			}
		}
	}
}

check_if_reconquista_finished_effect = {
	if = {
		limit = {
			NOR = {
				has_global_flag = reconquista_finished

				any_province = {
					county = {
						OR = {
							region = custom_leon
							region = custom_galicia
							region = custom_beja
							region = custom_granada
							region = custom_castille
							region = custom_cordoba
							region = custom_valencia
							region = custom_navarra
							region = custom_balearic
						}
					}

					owner = {
						trigger_if = {
							limit = { independent = yes }
							NOT = { religion_group = christian }
						}
						trigger_else = {
							top_liege = {
								NOT = { religion_group = christian }
							}
						}
					}
				}
			}
		}

		set_global_flag = reconquista_finished

		ROOT = {
			narrative_event = { id = HF.49452 }
		}

		any_playable_ruler = {
			narrative_event = { id = HF.49452 }
		}
	}
}

# TODO: rename poorly named effect?
check_war_effect = {
	check_if_reconquista_finished_effect = yes
	check_holy_order_donation_effect = yes
}

move_childrens_crusade_region_effect = {
	if = { # If at any point during the preparation Jerusalem is no longer a valid target, or a real Crusade starts, stop everything
		limit = {
			OR = {
				k_jerusalem = {
					trigger_if = {
						limit = { has_holder = yes }

						holder_scope = {
							religion_group = christian
						}
					}
				}

				c_jerusalem = {
					holder_scope = {
						OR = {
							religion_group = christian
							is_crusade_target = yes
						}
					}
				}

				religion_head = {
					OR = {
						is_preparing_crusade = yes

						any_war = {
							OR = {
								using_cb = crusade
								using_cb = new_crusade
							}
						}
					}
				}
			}
		}

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

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

		activate_title = {
			title = primary_title
			status = no
		}

		destroy_landed_title = primary_title
		clr_character_flag = flag_childrens_crusade_leader
		add_trait = lunatic
		piety = -150
		remove_trait = disinherited

		set_variable = {
			which = children_crusade_forces
			value = 0
		}

		set_variable = {
			which = children_crusade_morale
			value = 0
		}

		set_variable = {
			which = children_crusade_heavy_troops
			value = 0
		}

		any_courtier = {
			remove_trait = disinherited
			clr_character_flag = no_court_invites
		}
	}
	else_if = { # Move to Genoa first
		limit = { has_character_flag = flag_children_at_beginning }

		primary_title = {
			set_preferred_capital = c_genoa # Historically traveled here
		}

		change_variable = {
			which = children_crusade_forces
			value = 1
		}

		change_variable = {
			which = children_crusade_morale
			value = -1
		}

		clr_character_flag = flag_children_at_beginning
		set_character_flag = flag_children_at_genoa

		if = {
			limit = {
				persistent_event_target:childrens_crusade_sponsor_per = { is_alive = no }
			}

			change_variable = {
				which = children_crusade_morale
				value = -2
			}
		}

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

			narrative_event = {
				id = HF.25815 # Notify everyone that the crusade has reached Genoa and the sea did not part before them
				days = 30
			}
		}

		narrative_event = {
			id = HF.25808 # Ping back to main event to check travel progress
			days = 50
			random = 100
		}
	}
	else_if = { # Then shipwreck to Achaia
		limit = { has_character_flag = flag_children_at_genoa }

		primary_title = {
			set_preferred_capital = c_achaia
		}

		change_variable = {
			which = children_crusade_morale
			value = -1
		}

		clr_character_flag = flag_children_at_genoa
		set_character_flag = flag_children_at_achaia

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

			narrative_event = {
				id = HF.25816 # Notify everyone that the crusade has shipwrecked into Achaia
				days = 30
			}
		}

		if = {
			limit = {
				persistent_event_target:childrens_crusade_sponsor_per = { is_alive = no }
			}

			random = {
				chance = 25

				change_variable = {
					which = children_crusade_forces
					value = -1
				}
			}
		}

		narrative_event = {
			id = HF.25808 # Ping back to main event to check travel progress
			days = 50
			random = 100
		}
	}
	else_if = { # Then crawl to Nikaea
		limit = { has_character_flag = flag_children_at_achaia }

		primary_title = {
			set_preferred_capital = c_nikaea
		}

		clr_character_flag = flag_children_at_achaia
		set_character_flag = flag_children_at_nikaea

		# Base forces score is 3 = circa 15.000 poorly armed troops
		change_variable = {
			which = children_crusade_forces
			value = 1
		}

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

			narrative_event = {
				id = HF.25817 # Notify everyone that the crusade has crawled its way to Nikaea
				days = 30
			}
		}

		narrative_event = {
			id = HF.25808 # Ping back to main event to check travel progress
			days = 50
			random = 100
		}
	}
	else_if = { # We actually got there. Start the war
		limit = { has_character_flag = flag_children_at_nikaea }

		primary_title = {
			set_preferred_capital = c_antioch # Where the troops will spawn
		}

		clr_character_flag = flag_children_at_nikaea
		set_character_flag = flag_children_at_antioch

		narrative_event = {
			id = HF.25820 # START THE CRUSADE
			days = 25
		}

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

			narrative_event = {
				id = HF.25818 # Notify everyone that the crusade has actually reached Antioch and is ready to engage
				days = 30
			}
		}
	}
	else = { # Move directly to Antioch in case something went wrong
		primary_title = {
			set_preferred_capital = c_antioch # Historically traveled here
		}

		clr_character_flag = flag_children_at_achaia
		clr_character_flag = flag_children_at_nikaea
		clr_character_flag = flag_children_at_genoa
		set_character_flag = flag_children_at_antioch

		change_variable = {
			which = children_crusade_forces
			value = 1
		}
		change_variable = {
			which = children_crusade_morale
			value = -1
		}

		if = {
			limit = {
				persistent_event_target:childrens_crusade_sponsor_per = { is_alive = no }
			}

			change_variable = {
				which = children_crusade_morale
				value = -2
			}
		}

		narrative_event = {
			id = HF.25820 # START THE CRUSADE
			days = 25
		}

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

			narrative_event = {
				id = HF.25818 # Notify everyone that the crusade has actually reached Antioch and is ready to engage
				days = 30
			}
		}
	}
}