# 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
		hidden = yes
		duration = 45
	}

        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
	}

	if = {
		limit = {
			trait = excommunicated
		}
		rightful_religious_head_scope = {
			character_event = { id = HFP.41056 }
		}
	}

	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
		hidden = yes
		duration = 365
	}

	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
	}

	if = {
		limit = {
			any_province = {
				OR = {
					has_province_modifier = tomb_of_a_saint
					has_province_modifier = pillaged_tomb_of_a_saint
				}
			}
		}
		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
			}
		}
		#set_character_flag = saints_exists
	}

	random_list = {
		#Crusade Weapons
		20 = {
			modifier = {
				factor = 0
				OR = {
					has_artifact = sword_tier_2_new
					has_artifact = sword_tier_3_vengeance
				}
			}
			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 = {
			modifier = {
				factor = 0
				OR = {
					has_artifact = lance_tier_2_new
					has_artifact = lance_tier_3_impaler
				}
			}
			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 = {
			modifier = {
				factor = 0
				OR = {
					has_artifact = axe_tier_2_new
					has_artifact = axe_tier_3_ravager
				}
			}
			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 = {
			modifier = {
				factor = 0
				OR = {
					has_artifact = mace_tier_2_new
					has_artifact = mace_tier_3_bone_breaker
				}
			}
			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 = {
			modifier = {
				factor = 0
				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 Saints created throughout the game
		2 = {
			trigger = {
				has_character_flag = saints_exists
			}
			add_artifact = saintly_finger
			new_artifact = {
				set_name = crusade_saint_finger_name
				set_description = crusade_saint_finger_description
			}
		}
		2 = {
			trigger = {
				has_character_flag = saints_exists
			}
			add_artifact = saintly_bone
			new_artifact = {
				set_name = crusade_saint_bone_name
				set_description = crusade_saint_bone_description
			}
		}
		2 = {
			trigger = {
				has_character_flag = saints_exists
			}
			add_artifact = saintly_skull_tier_1
			new_artifact = {
				set_name = crusade_skull_name
				set_description = crusade_skull_description
			}
		}
		2 = {
			trigger = {
				has_character_flag = saints_exists
			}
			add_artifact = saintly_skull_tier_2
			new_artifact = {
				set_name = crusade_skull_name
				set_description = crusade_skull_description
			}
		}
		2 = {
			trigger = {
				has_character_flag = saints_exists
			}
			add_artifact = saintly_tongue
			new_artifact = {
				set_name = crusade_tongue_name
				set_description = crusade_tongue_description
			}
		}
		#Artifacts based on placeholder Saints
		2 = {
			modifier = {
				factor = 0.5
				has_character_flag = saints_exists
			}
			add_artifact = saintly_finger
			new_artifact = {
				set_name = crusade_saintly_finger_name_placeholder
				set_description = crusade_saintly_finger_description_placeholder
			}
		}
		2 = {
			modifier = {
				factor = 0.5
				has_character_flag = saints_exists
			}
			add_artifact = saintly_bone
			new_artifact = {
				set_name = crusade_saintly_bone_name_placeholder
				set_description = crusade_saintly_bone_description_placeholder
			}
		}
		2 = {
			modifier = {
				factor = 0.5
				has_character_flag = saints_exists
			}
			add_artifact = saintly_skull_tier_1
			new_artifact = {
				set_name = crusade_saintly_skull_name_placeholder
				set_description = crusade_saintly_skull_description_placeholder
			}
		}
		2 = {
			modifier = {
				factor = 0.5
				has_character_flag = saints_exists
			}
			add_artifact = saintly_skull_tier_2
			new_artifact = {
				set_name = crusade_saintly_skull_name_placeholder
				set_description = crusade_saintly_skull_description_placeholder
			}
		}
		2 = {
			modifier = {
				factor = 0.5
				has_character_flag = saints_exists
			}
			add_artifact = saintly_tongue
			new_artifact = {
				set_name = crusade_saintly_tongue_name_placeholder
				set_description = crusade_saintly_tongue_description_placeholder
			}
		}
	}

	clr_character_flag = saints_exists
	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
	}
	if = {
		limit = {
			is_female = yes
		}
		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 = "Zeus"
			}
			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_if = {
			limit = {
				NOT = { has_dlc = "Zeus" }
			}
			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 = "Zeus"
				}
				add_law = {
					law = ze_administration_laws_1
					cooldown = no
					opinion_effect = no
				}
			}
		}
	}
	capital_scope = {
		religion = PREV
	}

	if = {
		limit = {
			NOT = {
				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 = {
				AND = {
					any_war = {
						using_cb = new_crusade
					}
					has_pledged_crusade_participation = yes
				}
				any_liege = {
					AND = {
						any_war = {
							using_cb = new_crusade
						}
						has_pledged_crusade_participation = yes
					}
				}
			}
			event_target:enemy_commander = {
				OR = {
					any_war = {
						using_cb = new_crusade
					}
					any_liege = {
						any_war = {
							using_Cb = new_crusade
						}
					}
				}
				NOT = {
					religion = ROOT
				}
			}
			NOT = {
				check_variable = { which = crusadeCommander value = 0 }
			}
		}
		change_variable = { which = crusadeCommander value = 1 }
	}
	else_if = {
		limit = {
			OR = {
				AND = {
					any_war = {
						using_cb = new_crusade
					}
					has_pledged_crusade_participation = yes
				}
				any_liege = {
					AND = {
						any_war = {
							using_cb = new_crusade
						}
						has_pledged_crusade_participation = yes
					}
				}
			}
			event_target:enemy_commander = {
				OR = {
					any_war = {
						using_cb = new_crusade
					}
					any_liege = {
						any_war = {
							using_Cb = new_crusade
						}
					}
				}
				NOT = {
					religion = ROOT
				}
			}
		}
		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 = {
				AND = {
					any_war = {
						using_cb = new_crusade
					}
					has_pledged_crusade_participation = yes
				}
				any_liege = {
					AND = {
						any_war = {
							using_cb = new_crusade
						}
						has_pledged_crusade_participation = yes
					}
				}
			}
			event_target:enemy_commander = {
				OR = {
					any_war = {
						using_cb = new_crusade
					}
					any_liege = {
						any_war = {
							using_Cb = new_crusade
						}
					}
				}
				NOT = {
					religion = ROOT
				}
			}
			NOT = {
				check_variable = { which = crusadeCommander value = 0 }
			}
		}
		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
			NOT = { any_artifact = { has_artifact_flag = crusader_crown } }
			NOT = { primary_title = { has_title_flag = flag_title_crusader_crown } } #Spawn only once per-Kingdom.
		} 
		random = { 
			chance = 40
			modifier = { 
				factor = 1.75
				has_landed_title = k_jerusalem
			}
			modifier = { 
				factor = 1.5
				capital_scope = { region = world_middle_east_arabia }
			}
			primary_title = { set_title_flag = flag_title_crusader_crown } #Let this kind of artifact only spawn once per conquered Crusader Kingdom.
			add_artifact = crown_crusader_king
			new_artifact = {
				set_description = crown_crusader_king_desc_1
				random_list = { #Randomize names to avoid too much repetition.
					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
				NOR = {
					has_character_flag = northern_crusade_flavor
					has_character_flag = reconquista_flavor
				}
			}
			character_event = { id = HFP.42201 }
		}
		# Seeing the local culture
		3 = {
			trigger = {
				location = {
					NOT = {
						culture = ROOT
					}
				}
			}
			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 = {
				is_alternate_start = no
				ai = 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
				}
				OR = {
					AND = {
						is_title_active = d_teutonic_order
						d_teutonic_order = {
							owner = {
								religion = ROOT
							}
						}
					}
					AND = {
						is_title_active = k_teutonic_state
						k_teutonic_state = {
							owner = {
								religion = ROOT
							}
						}
					}
					AND = {
						is_title_active = d_knights_templar
						d_knights_templar = {
							owner = {
								religion = ROOT
							}
						}
					}
					AND = {
						is_title_active = d_livonian_order
						d_livonian_order = {
							owner = {
								religion = ROOT
							}
						}
					}
					AND = {
						is_title_active = d_knights_hospitaler
						d_knights_hospitaler = {
							owner = {
								religion = ROOT
							}
						}
					}
				}
			}
			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
				}
				OR = {
					AND = {
						is_title_active = d_teutonic_order
						d_teutonic_order = {
							owner = {
								religion = ROOT
							}
						}
					}
					AND = {
						is_title_active = k_teutonic_state
						k_teutonic_state = {
							owner = {
								religion = ROOT
							}
						}
					}
					AND = {
						is_title_active = d_knights_templar
						d_knights_templar = {
							owner = {
								religion = ROOT
							}
						}
					}
					AND = {
						is_title_active = d_livonian_order
						d_livonian_order = {
							owner = {
								religion = ROOT
							}
						}
					}
					AND = {
						is_title_active = d_knights_hospitaler
						d_knights_hospitaler = {
							owner = {
								religion = ROOT
							}
						}
					}
				}
			}
			character_event = { id = HFP.42213 }
		}
		# Crusader Brothel
		3 = {
			character_event = { id = HFP.42214 }
		}
	}
}

alternate_crusade_artifact_distribution_effect = {
	custom_tooltip = {
		text = alternate_crusade_artifact_distribution_effect_TT

		random_list = {
			#Crusade Weapons
			20 = {
				modifier = {
					factor = 0
					OR = {
						has_artifact = sword_tier_2_new
						has_artifact = sword_tier_3_vengeance
					}
				}
				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 = {
				modifier = {
					factor = 0
					OR = {
						has_artifact = lance_tier_2_new
						has_artifact = lance_tier_3_impaler
					}
				}
				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 = {
				modifier = {
					factor = 0
					OR = {
						has_artifact = axe_tier_2_new
						has_artifact = axe_tier_3_ravager
					}
				}
				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 = {
				modifier = {
					factor = 0
					OR = {
						has_artifact = mace_tier_2_new
						has_artifact = mace_tier_3_bone_breaker
					}
				}
				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 = {
				modifier = {
					factor = 0
					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 = {
	ROOT = {
		if = {
			limit = {
				event_target:first_duchy_title = {
					owner = {
						OR = {
							character = FROM
							top_liege = {
								character = FROM
							}
						}
						NOT = {
							primary_title = {
								title = PREVPREV
							}
						}
					}
				}
			}
			destroy_landed_title = event_target:first_duchy_title
		}
		if = {
			limit = {
				event_target:second_duchy_title = {
					owner = {
						OR = {
							character = FROM
							top_liege = {
								character = FROM
							}
						}
						NOT = {
							primary_title = {
								title = PREVPREV
							}
						}
					}
				}
			}
			destroy_landed_title = event_target:second_duchy_title
		}

		show_scope_change = no
		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 = FROM
								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 = FROM
								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:second_duchy_title
								enemy = FROM
								same_religion = yes # Only vassalize rulers of my religion
								is_religious = yes
								type = holy_war
							}
						}
					}
				}	

				hidden_tooltip = {
					if = {
						limit = {
							event_target:first_duchy_title = {
								OR = {
									owner = {
										OR = {
											character = FROM
											any_liege = {
												character = FROM
											}
										}
									}
									NOT = {
										owner = {
											always = yes
										}
									}
								}
								any_direct_de_jure_vassal_title = {
									owner = {
										character = PREVPREV
									}
								}

							}
						}
						grant_title_no_opinion = event_target:first_duchy_title
					}
					if = {
						limit = {
							event_target:second_duchy_title = {
								OR = {
									owner = {
										OR = {
											character = FROM
											any_liege = {
												character = FROM
											}
										}
									}
									NOT = {
										owner = {
											always = yes
										}
									}
								}
								any_direct_de_jure_vassal_title = {
									owner = {
										character = PREVPREV
									}
								}
							}
						}
						grant_title_no_opinion = event_target:second_duchy_title
					}
					if = {
						limit = {
							event_target:reconquista_kingdom_title = {
								OR = {
									NOT = {
										owner = {
											always = yes
										}
									}
									owner = {
										OR = {
											character = FROM
											any_liege = {
												character = FROM
											}
										}
									}
								}
							}
							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
									}
								}
							}
						}
						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
						}
					}
					if = {
						limit = {
							NOR = {
								has_landed_title = event_target:first_duchy_title
								has_landed_title = event_target:second_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 }

				ROOT = {
					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 = FROM
							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 = FROM
							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:second_duchy_title
							enemy = FROM
							same_religion = yes # Only vassalize rulers of my religion
							is_religious = yes
							type = holy_war
						}
					}
				}
			}

			hidden_tooltip = {
				if = {
					limit = {
						event_target:first_duchy_title = {
							OR = {
								owner = {
									OR = {
										character = FROM
										any_liege = {
											character = FROM
										}
									}
								}
								NOT = {
									owner = {
										always = yes
									}
								}
							}
							any_direct_de_jure_vassal_title = {
								owner = {
									character = ROOT
								}
							}

						}
					}
					grant_title_no_opinion = event_target:first_duchy_title
				}
				if = {
					limit = {
						event_target:second_duchy_title = {
							OR = {
								owner = {
									OR = {
										character = FROM
										any_liege = {
											character = FROM
										}
									}
								}
								NOT = {
									owner = {
										always = yes
									}
								}
							}
							any_direct_de_jure_vassal_title = {
								owner = {
									character = ROOT
								}
							}
						}
					}
					grant_title_no_opinion = event_target:second_duchy_title
				}
				if = {
					limit = {
						event_target:third_duchy_title = {
							OR = {
								owner = {
									OR = {
										character = FROM
										any_liege = {
											character = FROM
										}
									}
								}
								NOT = {
									owner = {
										always = yes
									}
								}
							}
							any_direct_de_jure_vassal_title = {
								owner = {
									character = ROOT
								}
							}
						}
					}
					grant_title_no_opinion = event_target:third_duchy_title
				}
				if = {
					limit = {
						event_target:reconquista_kingdom_title = {
							OR = {
								NOT = {
									owner = {
										always = yes
									}
								}
								owner = {
									OR = {
										character = FROM
										any_liege = {
											character = FROM
										}
									}
								}
							}
						}
						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
								}
							}
						}
						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
					}
				}
				if = {
					limit = {
						NOR = {
							has_landed_title = event_target:first_duchy_title
							has_landed_title = event_target:second_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

					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 = {
			modifier = reconquista_won
			years = 20
		}

		any_attacker = {
			hidden_tooltip = {
				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
						}
						NOT = {
							check_variable = {
								which = "reconquista_defender"
								value = 2
							}
						}
						religion_group = christian
					}
					character_event = { id = HF.49221 }
				}
			}
		}

		check_if_reconquista_finished_effect = yes

		character_event = { id = HF.49200 }
	}
}

reconquista_lose_effect = {
	any_attacker = {
		piety = -100
	}
	any_defender = {
		participation_scaled_piety = 200
		if = {
			limit = {
				uses_decadence = yes
			}
			participation_scaled_decadence = -10
		}
		hidden_tooltip = {
			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
				}
				character_event = { id = HF.49220 }
			}
		}
	}
	FROM = {
		show_scope_change = no
		reverse_imprison = ROOT
	}

	ROOT = { 
		disband_event_forces = reconquista_event_troops

		show_scope_change = no
		transfer_scaled_wealth = {
			to = FROM
			value = 5.0
		}
		religion_authority = {
			modifier = reconquista_lost
			years = 20
		}
	}
	FROM = { 
		show_scope_change = no
		religion_authority = {
			modifier = stopped_reconquista
			years = 20
		}
		hidden_tooltip = {
			if = {
				limit = {
					has_ambition = obj_strengthen_religion
				}
				change_variable = { which = strengthen_religion value = 1 }
			}
		}
	}

	ROOT = {
		character_event = { id = HF.49200 }
	}
}

check_if_reconquista_finished_effect = {
	if = {
		limit = {
			NOT = {
				has_global_flag = reconquista_finished
			}
			NOT = {
				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 = {
						OR = {
							AND = {
								independent = yes
								NOT = {
									religion_group = christian
								}
							}
							AND = {
								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 }
		}
	}
}

check_war_effect = {
	check_if_reconquista_finished_effect = yes
	check_holy_order_donation_effect = yes
}