namespace = HF

# Written by Joachim
# HF.49200 - HF.49299

### Clean up Events ###
# Clean up event post-Reconquista
character_event = {
	id = HF.49200

	is_triggered_only = yes # on_invalidation, reconquista_ CBs
	hide_window = yes

	has_dlc = "Holy Fury"

	immediate = {
		clr_character_flag = currently_reconquering_iberia
		clr_character_flag = reconquista_adventurer
		disband_event_forces = conscripted_reconquista_ships
		disband_event_forces = reconquista_event_troops_rabble

		any_playable_ruler = {
			clr_character_flag = promised_reconquista_ally
			clr_character_flag = reconquista_sponsor
		}

		# any_character = {
		#	clr_character_flag = reconquista_beneficiary_flag
		# }

		clr_global_flag = active_reconquista
	}
}

# Clean up event for death in the Reconquista
character_event = {
	id = HF.49201

	is_triggered_only = yes # on_death
	hide_window = yes

	has_dlc = "Holy Fury"

	trigger = {
		OR = {
			has_character_flag = currently_reconquering_iberia
			has_character_flag = ex_reconquistador
		}
	}

	immediate = {
		disband_event_forces = reconquista_event_troops
		character_event = { id = HF.49200 }
	}
}

# Clean up event for religion conversion in the Reconquista
character_event = {
	id = HF.49202

	is_triggered_only = yes # on_character_convert_religion
	hide_window = yes

	has_character_flag = currently_reconquering_iberia
	has_dlc = "Holy Fury"

	immediate = {
		disband_event_forces = reconquista_event_troops
		character_event = { id = HF.49200 }
	}
}

# Clean up event Reconquista Beneficiary dead
# character_event = {
#	id = HF.49203
#
#	is_triggered_only = yes
#	hide_window = yes
#
#	has_character_flag = reconquista_beneficiary_flag
#	has_dlc = "Holy Fury"
#
#	immediate = {
#		clr_character_flag = reconquista_beneficiary_flag
#	}
# }

# Clean up event Reconquista Beneficiary religious conversion
# character_event = {
#	id = HF.49204
#
#	is_triggered_only = yes
#	hide_window = yes
#
#	has_character_flag = reconquista_beneficiary_flag
#	has_dlc = "Holy Fury"
#
#	trigger = {
#		NOT = { religion_group = christian }
#	}
#
#	immediate = {
#		clr_character_flag = reconquista_beneficiary_flag
#	}
# }

# Clean up event Reconquista Beneficiary receive title
# character_event = {
#	id = HF.49205
#
#	is_triggered_only = yes # on_new_holder(_inheritance/usurpation)
#	hide_window = yes
#
#	has_character_flag = reconquista_beneficiary_flag
#	has_dlc = "Holy Fury"
#
#	immediate = {
#		clr_character_flag = reconquista_beneficiary_flag
#	}
# }

# Clean up event Reconquista Promised Ally religious conversion
# character_event = {
#	id = HF.49206
#
#	is_triggered_only = yes
#	hide_window = yes
#
#	has_character_flag = promised_reconquista_ally
#	has_dlc = "Holy Fury"
#
#	trigger = {
#		NOT = { religion_group = christian }
#	}
#
#	immediate = {
#		clr_character_flag = promised_reconquista_ally
#	}
# }

# Clean up event if you don't wage a war in 2 years after starting preparations
character_event = {
	id = HF.49207
	desc = EVTDESC_HF_49207
	picture = GFX_evt_bad_news
	border = GFX_event_normal_frame_religion

	is_triggered_only = yes

	has_dlc = "Holy Fury"

	trigger = {
		has_character_modifier = reconquista_preparation_left
	}

	immediate = {
		if = {
			limit = { ai = no }

			remove_character_modifier = reconquista_preparation_left
			disband_event_forces = reconquista_event_troops
			character_event = { id = HF.49200 }
		}
		else = {
			set_character_flag = ai_reconquista_preparation_time_run_out
			character_event = { id = HF.49249 }
		}
	}

	option = {
		name = EVTOPTA_HF_49207
	}
}

# Clean up when an old ex-Reconquistador dies
character_event = {
	id = HF.49208

	is_triggered_only = yes # on_death
	hide_window = yes

	has_character_flag = ex_reconquistador

	immediate = {
		disband_event_forces = conscripted_reconquista_ships
		disband_event_forces = reconquista_event_troops
		disband_event_forces = reconquista_event_troops_rabble
	}
}

### Allies Events ###
# Adding allies to the Reconquista
character_event = {
	id = HF.49210

	is_triggered_only = yes # reconquista_ CBs
	hide_window = yes

	has_dlc = "Holy Fury"

	immediate = {
		remove_character_modifier = reconquista_preparation_left

		any_war = {
			limit = {
				OR = {
					using_cb = reconquista_leon
					using_cb = reconquista_castille
					using_cb = reconquista_aragon
					using_cb = reconquista_galicia
					using_cb = reconquista_beja
					using_cb = reconquista_granada
					using_cb = reconquista_cordoba
					using_cb = reconquista_navarra
					using_cb = reconquista_valencia
					using_cb = reconquista_balearic
				}
			}

			any_playable_ruler = {
				limit = { has_character_flag = promised_reconquista_ally }

				clr_character_flag = promised_reconquista_ally
				letter_event = { id = HF.49213 }
				join_attacker_wars = PREV
			}
		}
	}
}

# Sending letter about joining the Reconquista
character_event = {
	id = HF.49211

	is_triggered_only = yes # promise_to_join_reconquista, HF_realm_decisions.txt
	hide_window = yes

	immediate = {
		FROM = {
			letter_event = {
				id = HF.49212
				days = 3
			}
		}
	}
}

# Receiving letter from ally joining the Reconquista
letter_event = {
	id = HF.49212
	desc = EVTDESC_HF_49212
	border = GFX_event_letter_frame_religion

	is_triggered_only = yes

	has_dlc = "Holy Fury"

	option = {
		name = EVTOPTA_HF_49212

		opinion = {
			name = opinion_grateful
			who = FROM
			years = 5
		}
	}
}

# Sending letters to promised allies, that the wars starts
letter_event = {
	id = HF.49213
	desc = EVTDESC_HF_49213

	is_triggered_only = yes

	ai = no

	option = {
		name = EVTOPTA_HF_49213
	}
}

### Bloodline Events ###
# El Cid Bloodline
narrative_event = {
	id = HF.49220
	desc = EVTDESC_HF_49220
	title = EVTTITLE_HF_49220
	picture = GFX_evt_bloodlines
	border = GFX_event_narrative_frame_religion

	is_triggered_only = yes

	has_dlc = "Holy Fury"

	trigger = {
		NOT = {
			any_owned_bloodline = {
				has_bloodline_flag = reconquista_bloodline
				bloodline_is_active_for = PREV
				founder = { character = ROOT }
			}
		}
	}

	option = {
		name = EVTOPTA_HF_49220

		if = {
			limit = { is_female = no }

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

# Reconquista Bloodline
narrative_event = {
	id = HF.49221
	desc = EVTDESC_HF_49221
	title = EVTTITLE_HF_49221
	picture = GFX_evt_bloodlines
	border = GFX_event_narrative_frame_religion

	is_triggered_only = yes

	has_dlc = "Holy Fury"

	trigger = {
		NOT = {
			any_owned_bloodline = {
				has_bloodline_flag = reconquista_bloodline
				bloodline_is_active_for = PREV
				founder = { character = ROOT }
			}
		}
	}

	option = {
		name = EVTOPTA_HF_49221

		add_artifact = crown_wrapped_helmet

		if = {
			limit = { is_female = no }

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

### Mercenary Events ###
## Mercenary offering services ##
# Receive letter from Mercenary offering services against their own Religion
letter_event = {
	id = HF.49230
	desc = EVTDESC_HF_49230
	border = GFX_event_letter_frame_religion

	is_triggered_only = yes # HF_realm_decisions.txt

	has_dlc = "Holy Fury"

	trigger = {
		reconquista_check_trigger = yes
	}

	option = {
		name = EVTOPTA_HF_49230

		FROMFROM = {
			show_scope_change = no

			trigger_switch = {
				on_trigger = real_tier

				COUNT = { transfer_scaled_wealth = { from = ROOT value = 0.25 } }
				DUKE = { transfer_scaled_wealth = { from = ROOT value = 0.5 } }
				KING = { transfer_scaled_wealth = { from = ROOT value = 0.75 } }
				EMPEROR = { transfer_scaled_wealth = { from = ROOT value = 1 } }
			}

			letter_event = {
				id = HF.49231
				days = 3
			}
		}

		custom_tooltip = {
			text = join_reconquista_war_TT

			any_war = {
				limit = {
					OR = {
						using_cb = reconquista_leon
						using_cb = reconquista_castille
						using_cb = reconquista_aragon
						using_cb = reconquista_galicia
						using_cb = reconquista_beja
						using_cb = reconquista_granada
						using_cb = reconquista_cordoba
						using_cb = reconquista_valencia
						using_cb = reconquista_navarra
					}
				}

				FROMFROM = {
					join_defender_wars = PREV
					join_attacker_wars = PREV
				}
			}
		}
	}

	option = {
		name = EVTOPTB_HF_49230

		FROMFROM = {
			letter_event = {
				id = HF.49232
				days = 3
			}
		}
	}
}

# Mercenary receives letter agreeing to the terms
letter_event = {
	id = HF.49231
	desc = EVTDESC_HF_49231
	border = GFX_event_letter_frame_religion

	is_triggered_only = yes

	has_dlc = "Holy Fury"
	ai = no

	option = {
		name = EVTOPTA_HF_49231
	}
}

# Mercenary receives letter rejecting the offer
letter_event = {
	id = HF.49232
	desc = EVTDESC_HF_49232
	border = GFX_event_letter_frame_religion

	is_triggered_only = yes

	has_dlc = "Holy Fury"

	option = {
		name = EVTOPTA_HF_49232

		opinion = {
			name = insulted
			who = FROM
			years = 10
		}
	}
}

## Mercenary being offered a service ##
# Receive letter from Lord/Lady about fighting their own religion
letter_event = {
	id = HF.49233
	desc = EVTDESC_HF_49233
	border = GFX_event_letter_frame_religion

	is_triggered_only = yes # HF_realm_decisions.txt

	has_dlc = "Holy Fury"

	option = {
		name = EVTOPTA_HF_49233

		trigger_switch = {
			on_trigger = real_tier

			COUNT   = { transfer_scaled_wealth = { from = FROMFROM value = 0.25 } }
			DUKE    = { transfer_scaled_wealth = { from = FROMFROM value = 0.5 } }
			KING    = { transfer_scaled_wealth = { from = FROMFROM value = 0.75 } }
			EMPEROR = { transfer_scaled_wealth = { from = FROMFROM value = 1 } }
		}

		custom_tooltip = {
			text = join_reconquista_war_other_TT

			FROMFROM = {
				any_war = {
					limit = {
						OR = {
							using_cb = reconquista_leon
							using_cb = reconquista_castille
							using_cb = reconquista_aragon
							using_cb = reconquista_galicia
							using_cb = reconquista_beja
							using_cb = reconquista_granada
							using_cb = reconquista_cordoba
							using_cb = reconquista_valencia
							using_cb = reconquista_navarra
						}
					}

					ROOT = {
						join_defender_wars = PREV
						join_attacker_wars = PREV
					}
				}
			}
		}

		add_trait_partially_silently_ambitious_effect = yes
	}

	option = {
		name = EVTOPTB_HF_49233
	}
}

# Lord/Lady receives letter from Mercenary agreeing to the offer
# letter_event = {
# 	id = HF.49234
# 	desc = EVTDESC_HF_49234
# 	border = GFX_event_letter_frame_religion
#
# 	is_triggered_only = yes
#
# 	has_dlc = "Holy Fury"
#
# 	option = {
# 		name = EVTOPTA_HF_49234
# 	}
# }

# Lord/Lady receives letter from Mercenary rejecting the offer
# letter_event = {
# 	id = HF.49235
# 	desc = EVTDESC_HF_49235
# 	border = GFX_event_letter_frame_religion
#
# 	is_triggered_only = yes
#
# 	has_dlc = "Holy Fury"
#
# 	option = {
# 		name = EVTOPTA_HF_49235
# 	}
# }

## Monthly / Bi-monthly Reconquista Preparations Events ##
# Tombola
character_event = {
	id = HF.49240

	is_triggered_only = yes
	hide_window = yes

	has_dlc = "Holy Fury"
	has_global_flag = active_reconquista

	trigger = {
		has_character_modifier = reconquista_preparation_duration
		not_reconquista_check_trigger = yes
	}

	fail_trigger_effect = {
		character_event = { id = HF.49249 }
	}

	immediate = {
		random_list = {
			# Gain troops
			5 = {
				character_event = { id = HF.49241 }
			}

			# Ask the Pope for money / help
			4 = {
				trigger = {
					NOR = {
						has_character_modifier = reconquista_pope_aid_event_cooldown
						has_character_flag = reconquista_adventurer
					}

					rightful_religious_head_scope = {
						always = yes
					}
				}

				character_event = { id = HF.49242 }
			}

			# Come across a great Commander to lead your troops
			2 = {
				trigger = {
					NOT = { has_character_modifier = reconquista_commander_event_cooldown }
				}

				character_event = { id = HF.49243 }
			}

			# Boats
			2 = {
				trigger = {
					NOT = { has_character_modifier = reconquista_boat_event_cooldown }
				}

				character_event = { id = HF.49244 }
			}
		}
	}

	after = {
		if = {
			limit = { has_character_flag = reconquista_adventurer }
			character_event = { id = HF.49240 days = 2 random = 5 }
		}
		else = {
			character_event = { id = HF.49240 days = 25 random = 20 }
		}
	}
}

# Reconquista Preparation Event - Gain troops
character_event = {
	id = HF.49241
	desc = EVTDESC_HF_49241
	picture = GFX_evt_large_army
	border = GFX_event_normal_frame_religion

	is_triggered_only = yes

	has_dlc = "Holy Fury"

	# Invest some gold
	option = {
		name = EVTOPTA_HF_49241

		trigger = {
			NOT = { has_character_flag = reconquista_adventurer }
		}

		scaled_wealth = {
			value = -0.25
			min = -25
			max = -250
		}

		custom_tooltip = {
			text = poorly_trained_troops_TT

			if = {
				limit = { year < 900 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						light_infantry = { 300 300 }
						archers = { 100 100 }
					}
				}
			}
			else_if = {
				limit = { year < 1000 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						light_infantry = { 400 400 }
						archers = { 150 150 }
					}
				}
			}
			else_if = {
				limit = { year < 1100 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						light_infantry = { 500 500 }
						archers = { 200 200 }
					}
				}
			}
			else_if = {
				limit = { year < 1200 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						light_infantry = { 600 600 }
						archers = { 250 250 }
					}
				}
			}
			else_if = {
				limit = { year < 1300 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						light_infantry = { 700 700 }
						archers = { 300 300 }
					}
				}
			}
			else_if = {
				limit = { year < 1400 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						light_infantry = { 800 800 }
						archers = { 350 350 }
					}
				}
			}
			else = {
				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						light_infantry = { 900 900 }
						archers = { 400 400 }
					}
				}
			}
		}
	}

	# Invest quite a lot of gold
	option = {
		name = EVTOPTB_HF_49241

		if = {
			limit = {
				NOT = { has_character_flag = reconquista_adventurer }
			}

			scaled_wealth = {
				value = -0.5
				min = -50
				max = -500
			}
		}

		custom_tooltip = {
			text = trained_troops_TT

			if = {
				limit = { year < 900 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						light_infantry = { 300 300 }
						heavy_infantry = { 200 200 }
						archers = { 100 100 }
					}
				}
			}
			else_if = {
				limit = { year < 1000 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						light_infantry = { 350 350 }
						heavy_infantry = { 250 250 }
						archers = { 150 150 }
					}
				}
			}
			else_if = {
				limit = { year < 1100 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						light_infantry = { 400 400 }
						heavy_infantry = { 300 300 }
						archers = { 200 200 }
					}
				}
			}
			else_if = {
				limit = { year < 1200 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						light_infantry = { 450 450 }
						heavy_infantry = { 350 350 }
						archers = { 250 250 }
					}
				}
			}
			else_if = {
				limit = { year < 1300 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						light_infantry = { 500 500 }
						heavy_infantry = { 400 400 }
						archers = { 300 300 }
					}
				}
			}
			else_if = {
				limit = { year < 1400 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						light_infantry = { 550 550 }
						heavy_infantry = { 450 450 }
						archers = { 350 350 }
					}
				}
			}
			else = {
				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						light_infantry = { 600 600 }
						heavy_infantry = { 500 500 }
						archers = { 400 400 }
					}
				}
			}
		}
	}

	# Invest a crazy amount of gold
	option = {
		name = EVTOPTC_HF_49241

		trigger = {
			NOT = { has_character_flag = reconquista_adventurer }
		}

		scaled_wealth = {
			value = -1
			min = -100
			max = -1000
		}

		custom_tooltip = {
			text = well_trained_troops_TT

			if = {
				limit = { year < 900 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						heavy_infantry = { 200 200 }
						pikemen = { 200 200 }
						light_cavalry = { 150 150 }
						archers = { 100 100 }
					}
				}
			}
			else_if = {
				limit = { year < 1000 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						heavy_infantry = { 250 250 }
						pikemen = { 250 250 }
						light_cavalry = { 200 200 }
						archers = { 150 150 }
					}
				}
			}
			else_if = {
				limit = { year < 1100 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						heavy_infantry = { 300 300 }
						pikemen = { 300 300 }
						light_cavalry = { 250 250 }
						archers = { 200 200 }
					}
				}
			}
			else_if = {
				limit = { year < 1200 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						heavy_infantry = { 350 350 }
						pikemen = { 350 350 }
						light_cavalry = { 300 300 }
						archers = { 250 250 }
					}
				}
			}
			else_if = {
				limit = { year < 1300 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						heavy_infantry = { 400 400 }
						pikemen = { 400 400 }
						light_cavalry = { 350 350 }
						archers = { 300 300 }
					}
				}
			}
			else_if = {
				limit = { year < 1400 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						heavy_infantry = { 450 450 }
						pikemen = { 450 450 }
						light_cavalry = { 400 400 }
						archers = { 350 350 }
					}
				}
			}
			else = {
				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						heavy_infantry = { 500 500 }
						pikemen = { 500 500 }
						light_cavalry = { 450 450 }
						archers = { 400 400 }
					}
				}
			}
		}
	}

	# Invest no gold and hope things work out
	option = {
		name = EVTOPTD_HF_49241

		trigger = {
			NOT = { has_character_flag = reconquista_adventurer }
		}

		prestige = -50

		custom_tooltip = {
			text = peasant_rabble_TT

			if = {
				limit = { year < 900 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops_rabble
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						light_infantry = { 300 300 }
					}
				}
			}
			else_if = {
				limit = { year < 1000 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops_rabble
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						light_infantry = { 400 400 }
					}
				}
			}
			else_if = {
				limit = { year < 1100 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops_rabble
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						light_infantry = { 500 500 }
					}
				}
			}
			else_if = {
				limit = { year < 1200 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops_rabble
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						light_infantry = { 600 600 }
					}
				}
			}
			else_if = {
				limit = { year < 1300 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops_rabble
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						light_infantry = { 700 700 }
					}
				}
			}
			else_if = {
				limit = { year < 1400 }

				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops_rabble
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						light_infantry = { 800 800 }
					}
				}
			}
			else = {
				spawn_unit = {
					province = capital_scope
					home = capital_scope
					owner = ROOT
					earmark = reconquista_event_troops_rabble
					attrition = 1
					maintenance_multiplier = 0
					reinforces = no

					troops = {
						light_infantry = { 900 900 }
					}
				}
			}
		}
	}
}

# Reconquista Preparation Event - Ask the Pope for aid
character_event = {
	id = HF.49242
	desc = EVTDESC_HF_49242
	picture = GFX_evt_church_council
	border = GFX_event_normal_frame_religion

	is_triggered_only = yes

	has_dlc = "Holy Fury"

	option = {
		name = EVTOPTA_HF_49242

		piety = -100

		scaled_wealth = {
			value = 0.5
			min = 50
			max = 500
		}
	}

	option = {
		name = EVTOPTB_HF_49242

		piety = -300

		scaled_wealth = {
			value = 1
			min = 100
			max = 1000
		}
	}

	option = {
		name = EVTOPTC_HF_49242

		piety = 50
		add_trait_fully_silently_humble_effect = yes
	}

	after = {
		add_character_modifier = {
			name = reconquista_pope_aid_event_cooldown
			months = 3
			hidden = yes
		}
	}
}

# Reconquista Preparation Event - Great Commander ahoy!
character_event = {
	id = HF.49243
	desc = EVTDESC_HF_49243
	picture = GFX_evt_dueling_knights_hf
	border = GFX_event_normal_frame_religion

	is_triggered_only = yes

	has_dlc = "Holy Fury"

	immediate = {
		create_character = {
			dynasty = random
			culture = catalan
			religion = ROOT
			health = 5

			attributes = {
				martial = 4
				diplomacy = 0
				stewardship = 2
				intrigue = 0
				learning = 0
			}
		}

		new_character = {
			save_event_target_as = new_commander

			opinion = {
				name = opinion_loyal_servant
				who = ROOT
			}

			add_trait = brilliant_strategist

			# Randomize Culture
			random_list = {
				# Keep Catalan
				3 = { }

				# Other Iberian Cultures
				3 = { culture = basque }
				3 = { culture = castilian }

				3 = {
					trigger = { year >= 950 }
					culture = portuguese
				}

				3 = {
					trigger = { year < 900 }
					culture = visigothic
				}

				# Other close by (normally Catholic) cultures
				3 = {
					trigger = { year < 850 }
					culture = suebi
				}

				2 = { culture = occitan }

				1 = {
					trigger = { year >= 850 }
					culture = french
				}

				1 = { culture = italian }

				1 = {
					trigger = { year < 900 }
					culture = lombard
				}

				1 = { culture = german }

				2 = {
					trigger = { year < 850 }
					culture = frankish
				}

				2 = {
					trigger = { year >= 950 }
					culture = norman
				}
			}

			# Some sort of huge
			random_list = {
				1 = { add_trait = strong }
				1 = { add_trait = brawny }
			}

			# Some sort of lifestyle
			random_list = {
				1 = { add_trait = strategist }
				1 = { add_trait = duelist }
				1 = { add_trait = hunter }
			}

			# Some sort of good trait
			random_list = {
				1 = { add_trait = patient }
				1 = { add_trait = diligent }
			}

			# Some sort of bad trait
			random_list = {
				1 = { add_trait = proud }
				1 = { add_trait = envious }
			}

			# Some potential traits
			random = {
				chance = 40
				add_trait = ambitious
			}

			random = {
				chance = 40
				add_trait = brave
			}

			random = {
				chance = 40
				add_trait = zealous
			}

			# One Commander trait
			add_random_commander_trait_effect = yes
		}
	}

	option = {
		name = EVTOPTA_HF_49243
	}

	option = {
		name = EVTOPTB_HF_49243

		trigger = {
			can_grant_title = title_commander
			byzantines_can_grant_commander_trigger = yes
		}

		event_target:new_commander = {
			show_scope_change = no

			give_minor_title = title_commander
		}
	}

	option = {
		name = EVTOPTC_HF_49243

		trigger = {
			NOT = { has_character_flag = reconquista_adventurer }
		}

		hidden_effect = {
			event_target:new_commander = {
				death = { death_reason = death_missing }
			}
		}
	}

	after = {
		if = {
			limit = { has_character_flag = reconquista_adventurer }

			add_character_modifier = {
				name = reconquista_commander_event_cooldown
				days = 15
				hidden = yes
			}
		}
		else = {
			add_character_modifier = {
				name = reconquista_commander_event_cooldown
				months = 4
				hidden = yes
			}
		}
	}
}

# Reconquista Preparation Event - Boats!?
character_event = {
	id = HF.49244
	desc = EVTDESC_HF_49244
	picture = GFX_evt_ship_voyage_hf
	border = GFX_event_normal_frame_religion

	is_triggered_only = yes

	has_dlc = "Holy Fury"

	option = {
		name = EVTOPTA_HF_49244

		if = {
			limit = {
				NOT = { has_character_flag = reconquista_adventurer }
			}

			scaled_wealth = {
				value = -0.3
				min = -30
				max = -300
			}
		}

		if = {
			limit = { year < 850 }

			spawn_fleet = {
				province = closest
				owner = ROOT
				earmark = conscripted_reconquista_ships

				troops = {
					galleys = { 10 10 }
				}
			}
		}
		else_if = {
			limit = { year < 950 }

			spawn_fleet = {
				province = closest
				owner = ROOT
				earmark = conscripted_reconquista_ships

				troops = {
					galleys = { 20 20 }
				}
			}
		}
		else_if = {
			limit = { year < 1050 }

			spawn_fleet = {
				province = closest
				owner = ROOT
				earmark = conscripted_reconquista_ships

				troops = {
					galleys = { 30 30 }
				}
			}
		}
		else_if = {
			limit = { year < 1150 }

			spawn_fleet = {
				province = closest
				owner = ROOT
				earmark = conscripted_reconquista_ships

				troops = {
					galleys = { 40 40 }
				}
			}
		}
		else_if = {
			limit = { year < 1250 }

			spawn_fleet = {
				province = closest
				owner = ROOT
				earmark = conscripted_reconquista_ships

				troops = {
					galleys = { 50 50 }
				}
			}
		}
		else_if = {
			limit = { year < 1350 }

			spawn_fleet = {
				province = closest
				owner = ROOT
				earmark = conscripted_reconquista_ships

				troops = {
					galleys = { 60 60 }
				}
			}
		}
		else = {
			spawn_fleet = {
				province = closest
				owner = ROOT
				earmark = conscripted_reconquista_ships

				troops = {
					galleys = { 70 70 }
				}
			}
		}
	}

	option = {
		name = EVTOPTB_HF_49244

		if = {
			limit = {
				NOT = { has_character_flag = reconquista_adventurer }
			}

			scaled_wealth = {
				value = -1
				min = -100
				max = -1000
			}
		}

		prestige = 100

		if = {
			limit = { year < 850 }

			spawn_fleet = {
				province = closest
				owner = ROOT
				earmark = conscripted_reconquista_ships

				troops = {
					galleys = { 20 20 }
				}
			}
		}
		else_if = {
			limit = { year < 950 }

			spawn_fleet = {
				province = closest
				owner = ROOT
				earmark = conscripted_reconquista_ships

				troops = {
					galleys = { 40 40 }
				}
			}
		}
		else_if = {
			limit = { year < 1050 }

			spawn_fleet = {
				province = closest
				owner = ROOT
				earmark = conscripted_reconquista_ships

				troops = {
					galleys = { 60 60 }
				}
			}
		}
		else_if = {
			limit = { year < 1150 }

			spawn_fleet = {
				province = closest
				owner = ROOT
				earmark = conscripted_reconquista_ships

				troops = {
					galleys = { 80 80 }
				}
			}
		}
		else_if = {
			limit = { year < 1250 }

			spawn_fleet = {
				province = closest
				owner = ROOT
				earmark = conscripted_reconquista_ships

				troops = {
					galleys = { 100 100 }
				}
			}
		}
		else_if = {
			limit = { year < 1350 }

			spawn_fleet = {
				province = closest
				owner = ROOT
				earmark = conscripted_reconquista_ships

				troops = {
					galleys = { 120 120 }
				}
			}
		}
		else = {
			spawn_fleet = {
				province = closest
				owner = ROOT
				earmark = conscripted_reconquista_ships

				troops = {
					galleys = { 140 140 }
				}
			}
		}
	}

	option = {
		name = EVTOPTC_HF_49244

		trigger = {
			NOT = { has_character_flag = reconquista_adventurer }
		}
	}

	after = {
		if = {
			limit = { has_character_flag = reconquista_adventurer }

			add_character_modifier = {
				name = reconquista_boat_event_cooldown
				months = 1
				hidden = yes
			}
		}
		else = {
			add_character_modifier = {
				name = reconquista_boat_event_cooldown
				months = 11
				hidden = yes
			}
		}
	}
}

# Reconquista Preparation Event - Preparations Finished
character_event = {
	id = HF.49249
	desc = EVTDESC_HF_49249
	picture = GFX_evt_mounted_knights_hf
	border = GFX_event_normal_frame_religion

	is_triggered_only = yes

	has_global_flag = active_reconquista
	has_dlc = "Holy Fury"

	trigger = {
		not_reconquista_check_trigger = yes
	}

	option = {
		name = EVTOPTA_HF_49249

		hidden_effect = {
			# Find suitable target for Reconquista Adventurers
			if = {
				limit = {
					OR = {
						has_character_flag = reconquista_adventurer
						has_character_flag = ai_reconquista_preparation_time_run_out
					}
				}

				if = {
					limit = {
						any_province = {
							county = {
								region = world_europe_west_iberia

								holder_scope = {
									top_liege = {
										NOT = { religion_group = christian }
									}
								}
							}
						}
					}

					random_province = {
						limit = {
							county = {
								region = world_europe_west_iberia

								holder_scope = {
									top_liege = {
										NOT = { religion_group = christian }
									}
								}
							}
						}

						preferred_limit = {
							OR = {
								region = custom_aragon
								region = custom_navarra
							}
						}

						preferred_limit = {
							OR = {
								region = custom_leon
								region = custom_galicia
								region = custom_castille
							}
						}

						preferred_limit = {
							OR = {
								region = custom_beja
								region = custom_valencia
							}
						}

						preferred_limit = { region = custom_cordoba }
						preferred_limit = { region = custom_granada }
						preferred_limit = { region = custom_balearic }

						owner = {
							top_liege = {
								if = {
									limit = { PREVPREV = { region = custom_aragon } }

									reverse_unsafe_war = {
										target = ROOT
										casus_belli = reconquista_aragon
									}
								}
								else_if = {
									limit = { PREVPREV = { region = custom_navarra } }

									reverse_unsafe_war = {
										target = ROOT
										casus_belli = reconquista_navarra
									}
								}
								else_if = {
									limit = { PREVPREV = { region = custom_leon } }

									reverse_unsafe_war = {
										target = ROOT
										casus_belli = reconquista_leon
									}
								}
								else_if = {
									limit = { PREVPREV = { region = custom_galicia } }

									reverse_unsafe_war = {
										target = ROOT
										casus_belli = reconquista_galicia
									}
								}
								else_if = {
									limit = { PREVPREV = { region = custom_castille } }

									reverse_unsafe_war = {
										target = ROOT
										casus_belli = reconquista_castille
									}
								}
								else_if = {
									limit = { PREVPREV = { region = custom_beja } }

									reverse_unsafe_war = {
										target = ROOT
										casus_belli = reconquista_beja
									}
								}
								else_if = {
									limit = { PREVPREV = { region = custom_valencia } }

									reverse_unsafe_war = {
										target = ROOT
										casus_belli = reconquista_valencia
									}
								}
								else_if = {
									limit = { PREVPREV = { region = custom_cordoba } }

									reverse_unsafe_war = {
										target = ROOT
										casus_belli = reconquista_cordoba
									}
								}
								else_if = {
									limit = { PREVPREV = { region = custom_granada } }

									reverse_unsafe_war = {
										target = ROOT
										casus_belli = reconquista_granada
									}
								}
								else_if = {
									limit = { PREVPREV = { region = custom_balearic } }

									reverse_unsafe_war = {
										target = ROOT
										casus_belli = reconquista_balearic
									}
								}
							}
						}
					}
				}

				# End the Reconquista
				else = {
					#set_global_flag = reconquista_finished

					destroy_landed_title = primary_title

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

		clr_character_flag = ai_reconquista_preparation_time_run_out
	}
}

## Reconquista Narrative Events ##
# A Christian prepares Reconquista - Sent to all rulers in the Iberian Peninsula
narrative_event = {
	id = HF.49450
	title = EVTTITLE_HF_49450
	desc = EVTDESC_HF_49450
	picture = GFX_evt_large_army
	border = GFX_event_narrative_frame_religion

	is_triggered_only = yes

	has_dlc = "Holy Fury"

	trigger = {
		is_alternate_start = no
	}

	option = {
		name = {
			text = EVTOPTA_HF_49450
			trigger = { religion_group = FROM }
		}
		name = {
			text = EVTOPTB_HF_49450
			trigger = {
				NOT = { religion_group = FROM }
			}
		}
	}
}

# Empire of Hispania created
narrative_event = {
	id = HF.49451
	title = EVTTITLE_HF_49451
	desc = EVTDESC_HF_49451
	picture = GFX_evt_emissary
	border = GFX_event_narrative_frame_religion

	is_triggered_only = yes # gain_effect, e_hispania

	has_dlc = "Holy Fury"

	trigger = {
		is_alternate_start = no
		NOT = { has_global_flag = reconquista_finished }
	}

	immediate = {
		set_global_flag = reconquista_finished
	}

	option = {
		name = {
			text = EVTOPTA_HF_49451
			trigger = { religion_group = FROM }
		}
		name = {
			text = EVTOPTB_HF_49451
			trigger = {
				NOT = { religion_group = FROM }
			}
		}
	}
}

# End of the Reconquista
narrative_event = {
	id = HF.49452
	title = EVTTITLE_HF_49452
	desc = EVTDESC_HF_49452
	picture = GFX_evt_beatification
	border = GFX_event_narrative_frame_religion

	is_triggered_only = yes # check_if_reconquista_finished_effect, HF.49249

	has_dlc = "Holy Fury"

	trigger = {
		is_alternate_start = no
		NOT = { has_global_flag = reconquista_finished }
	}

	immediate = {
		set_global_flag = reconquista_finished
	}

	option = {
		name = {
			text = EVTOPTA_HF_49452
			trigger = { religion_group = christian }
		}
		name = {
			text = EVTOPTB_HF_49451
			trigger = {
				NOT = { religion_group = christian }
			}
		}
	}
}

## Adventurer Reconquista ##
# Make a fitting adventurer
character_event = {
	id = HF.49460

	is_triggered_only = yes # on_yearly_pulse
	hide_window = yes

	has_dlc = "Holy Fury"
	religion_group = christian
	culture_group = iberian
	war = no

	trigger = {
		has_game_rule = {
			name = reconquista
			value = on
		}

		any_demesne_title = {
			higher_real_tier_than = DUKE
			region = world_europe_west_iberia
		}

		NOR = {
			has_global_flag = active_reconquista
			has_global_flag = reconquista_finished
			has_character_modifier = reconquista_adventurer_cooldown
		}

		any_province = {
			county = {
				OR = {
					region = custom_castille
					region = custom_leon
					region = custom_galicia
					region = custom_aragon
					region = custom_navarra
				}

				holder_scope = {
					top_liege = {
						NOT = { religion_group = christian }
					}
				}
			}
		}

		trigger_if = {
			limit = { real_tier = KING }
			random < 25
		}
		trigger_else = { # Emperor
			random < 15
		}
	}

	immediate = {
		set_global_flag = active_reconquista

		create_character = {
			culture = ROOT
			religion = ROOT
			age = 24
			female = 50
			dynasty = random
			random_traits = yes
		}

		new_character = {
			wealth = 400
			remove_trait = slow
			remove_trait = imbecile
			remove_trait = weak
			remove_trait = frail

			random_list = {
				100 = { add_trait = genius }
				100 = { add_trait = shrewd }
				100 = { add_trait = quick }
				100 = { add_trait = strong }
				100 = { add_trait = brawny }

				1 = {
					trigger = { religion = catholic }
					add_artifact = crown_wrapped_helmet
				}
			}

			add_random_commander_trait_effect = yes
			character_event = { id = HF.49461 }
		}
	}

	after = {
		add_character_modifier = {
			name = reconquista_adventurer_cooldown
			years = 15
			hidden = yes
		}
	}
}

# Ping the adventurer
character_event = {
	id = HF.49461

	is_triggered_only = yes
	hide_window = yes

	has_dlc = "Holy Fury"

	immediate = {
		set_character_flag = reconquista_adventurer
		set_character_flag = ex_reconquistador
		set_character_flag = currently_reconquering_iberia
		#set_global_flag = active_reconquista

		FROM = {
			capital_scope = {
				save_event_target_as = host_capital
			}
		}

		create_title = {
			name = RECONQUISTA_ADVENTURE_TITLE_NAME
			adventurer = yes
			tier = DUKE
			landless = yes
			culture = ROOT
			holder = ROOT
		}

		spawn_unit = {
			owner = ROOT
			province = event_target:host_capital
			home = event_target:host_capital
			earmark = reconquista_event_troops
			attrition = 1
			maintenance_multiplier = 0

			troops = {
				heavy_infantry = { 500 500 }
				pikemen = { 500 500 }
				light_cavalry = { 500 500 }
				archers = { 500 500 }
			}
		}

		spawn_fleet = {
			province = closest
			owner = ROOT
			earmark = conscripted_reconquista_ships

			troops = {
				galleys = { 40 40 }
			}
		}

		add_character_modifier = {
			name = reconquista_preparation_duration
			months = 1
			hidden = yes
		}

		character_event = {
			id = HF.49240
			days = 5
			random = 5
		}

		FROM = {
			letter_event = { id = HF.49462 }
		}
	}
}

# Letter event asking the ruler to back the adventurer's Reconquista campaign
letter_event = {
	id = HF.49462
	desc = EVTDESC_HF_49462
	border = GFX_event_letter_frame_religion

	is_triggered_only = yes

	has_dlc = "Holy Fury"

	option = {
		name = EVTOPTA_HF_49462

		piety = 50
		add_trait_fully_silently_ambitious_effect = yes
		set_character_flag = promised_reconquista_ally
		set_character_flag = reconquista_sponsor

		character_event = {
			id = HF.49463
			days = 10
		}
	}

	option = {
		name = EVTOPTB_HF_49462

		piety = -50

		ai_chance = { factor = 0 }
	}
}

# Sponsor event
character_event = {
	id = HF.49463
	desc = EVTDESC_HF_49463
	picture = GFX_evt_council
	border = GFX_event_normal_frame_religion

	is_triggered_only = yes

	has_character_flag = reconquista_sponsor

	immediate = {
		clr_character_flag = reconquista_sponsor
	}

	option = {
		name = EVTOPTA_HF_49463

		trigger_switch = {
			on_trigger = real_tier

			BARON   = { prestige =  -50 }
			COUNT   = { prestige = -100 }
			DUKE    = { prestige = -200 }
			KING    = { prestige = -300 }
			EMPEROR = { prestige = -500 }
		}

		custom_tooltip = {
			text = reconquista_renowned_sponsor_TT

			FROMFROM = {
				add_character_modifier = {
					name = renowned_sponsor
					years = 2
				}
			}
		}
	}

	option = {
		name = EVTOPTB_HF_49463

		trigger_switch = {
			on_trigger = real_tier

			BARON   = { piety =  -25 }
			COUNT   = { piety =  -50 }
			DUKE    = { piety = -100 }
			KING    = { piety = -200 }
			EMPEROR = { piety = -300 }
		}

		custom_tooltip = {
			text = reconquista_pious_sponsor_TT

			FROMFROM = {
				add_character_modifier = {
					name = pious_sponsor
					years = 2
				}
			}
		}
	}

	option = {
		name = EVTOPTC_HF_49463

		scaled_wealth = {
			value = -1
			min = -100
			max = -1000
		}

		custom_tooltip = {
			text = reconquista_wealthy_sponsor_TT

			FROMFROM = {
				add_character_modifier = {
					name = wealthy_sponsor
					years = 2
				}
			}
		}
	}

	option = {
		name = EVTOPTD_HF_49463
	}
}

## Flavour Events for Reconquista ##
# Tombola
character_event = {
	id = HF.49470

	is_triggered_only = yes # on_yearly_pulse, on_battle_won_leader via HF.49471 (Players only)
	hide_window = yes

	religion_group = christian
	in_command = yes
	has_dlc = "Holy Fury"
	prisoner = no
	min_age = 16

	trigger = {
		NOT = { has_character_modifier = reconquista_flavor_cooldown }

		any_war = {
			OR = {
				using_cb = reconquista_leon
				using_cb = reconquista_castille
				using_cb = reconquista_aragon
				using_cb = reconquista_galicia
				using_cb = reconquista_beja
				using_cb = reconquista_granada
				using_cb = reconquista_cordoba
				using_cb = reconquista_valencia
				using_cb = reconquista_navarra
				using_cb = reconquista_balearic
			}

			any_attacker = {
				character = ROOT
			}
		}

		location = {
			owner = {
				top_liege = {
					NOT = { religion_group = christian }

					any_war = {
						OR = {
							using_cb = reconquista_leon
							using_cb = reconquista_castille
							using_cb = reconquista_aragon
							using_cb = reconquista_galicia
							using_cb = reconquista_beja
							using_cb = reconquista_granada
							using_cb = reconquista_cordoba
							using_cb = reconquista_valencia
							using_cb = reconquista_navarra
							using_cb = reconquista_balearic
						}

						any_defender = {
							character = PREV
						}
					}
				}
			}
		}
	}

	immediate = {
		set_character_flag = reconquista_flavor
		crusade_flavor_event_tombola_effect = yes
	}

	after = {
		set_character_flag = reconquista_flavor

		add_character_modifier = {
			name = reconquista_flavor_cooldown
			days = 25
			hidden = yes
		}
	}
}

# Reconquista flavor event randomizer
character_event = {
	id = HF.49471

	is_triggered_only = yes # on_battle_won_leader
	hide_window = yes

	ai = no
	religion_group = christian
	has_dlc = "Holy Fury"

	trigger = {
		any_war = {
			OR = {
				using_cb = reconquista_leon
				using_cb = reconquista_castille
				using_cb = reconquista_aragon
				using_cb = reconquista_galicia
				using_cb = reconquista_beja
				using_cb = reconquista_granada
				using_cb = reconquista_cordoba
				using_cb = reconquista_valencia
				using_cb = reconquista_navarra
				using_cb = reconquista_balearic
			}

			any_attacker = {
				character = ROOT
			}
		}
	}

	immediate = {
		random = {
			chance = 25

			character_event = {
				id = HF.49470
				days = 1
				random = 15
			}
		}
	}
}

## Reconquista cooldown events for AI
# Cooldown timer
character_event = {
	id = HF.49480

	is_triggered_only = yes # prepare_reconquista, HF_realm_decisions.txt
	hide_window = yes

	immediate = {
		set_global_flag = reconquista_ai_cooldown

		event_target:pulse_province = {
			province_event = {
				id = HF.49481
				years = 15
			}
		}
	}
}

# Cooldown finished
province_event = {
	id = HF.49481

	is_triggered_only = yes
	hide_window = yes

	immediate = {
		clr_global_flag = reconquista_ai_cooldown
	}
}