namespace = HF

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

### Clean up Events ###
# Clean up event post-Reconquista
character_event = {
    id = HF.49200
   	hide_window = yes
    is_triggered_only = yes
    has_dlc = "Holy Fury"
    trigger = {
    
    }

    immediate = {
    	clr_character_flag = currently_reconquering_iberia
    	clr_global_flag = active_reconquista
    	any_character = {
    		limit = {
    			has_character_flag = reconquista_beneficiary_flag
    		}
    		clr_character_flag = reconquista_beneficiary_flag
    	}
    	any_character = {
    		limit = {
    			has_character_flag = promised_reconquista_ally
    		}
    		clr_character_flag = promised_reconquista_ally
    	}
    	any_character = {
    		limit = {
    			has_character_flag = reconquista_adventurer
    		}
    		clr_character_flag = reconquista_adventurer
    	}
    	any_character = {
    		limit = {
    			has_character_flag = reconquista_sponsor
    		}
    		clr_character_flag = reconquista_sponsor
    	}
    	disband_event_forces = conscripted_reconquista_ships
    	disband_event_forces = reconquista_event_troops_rabble
    }
}

# Clean up event for death in the Reconquista
character_event = {
    id = HF.49201
    hide_window = yes
    is_triggered_only = 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
    	disband_event_forces = reconquista_event_troops_rabble
    	character_event = { id = HF.49200 }
    }
}

# Clean up event for religion conversion in the Reconquista
character_event = {
    id = HF.49202
    hide_window = yes
    is_triggered_only = yes
    has_dlc = "Holy Fury"
    trigger = {
    	has_character_flag = currently_reconquering_iberia
    }

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

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

    immediate = {
    	clr_character_flag = reconquista_beneficiary_flag
    }
}

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

    immediate = {
    	clr_character_flag = reconquista_beneficiary_flag
    }
}

# Clean up event Reconquista Beneficiary receive title
character_event = {
    id = HF.49205
    hide_window = yes
    is_triggered_only = yes
    has_dlc = "Holy Fury"
    trigger = {
    	has_character_flag = reconquista_beneficiary_flag
    }

    immediate = {
    	clr_character_flag = reconquista_beneficiary_flag
    }
}

# Clean up event Reconquista Promised Ally religious conversion
character_event = {
    id = HF.49206
    hide_window = yes
    is_triggered_only = yes
    has_dlc = "Holy Fury"
    trigger = {
    	has_character_flag = promised_reconquista_ally
    	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
			character_event = { id = HF.49200 }
			disband_event_forces = reconquista_event_troops
		}
		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
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_character_flag = ex_reconquistador
	}

	immediate = {
		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
    hide_window = yes
    is_triggered_only = yes
    has_dlc = "Holy Fury"
    trigger = {
    
    }

    immediate = {
    	hidden_tooltip = {
    		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
				}
				letter_event = { id = HF.49213 }
				join_attacker_wars = PREV
			}
		}
    }
}

# Sending letter about joining the Reconquista
character_event = {
    id = HF.49211
    hide_window = yes
    is_triggered_only = yes
    has_dlc = "Holy Fury"
    trigger = {
    
    }

    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"
    trigger = {
    
    }

    immediate = {

    }

    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
	trigger = {
		has_character_flag = promised_reconquista_ally
	}

	immediate = {
		clr_character_flag = promised_reconquista_ally
	}

	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
    			founder = {
    				character = ROOT
    			}
    		}
    	}
    }

    immediate = {

    }

    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
    			founder = {
    				character = ROOT
    			}
    		}
    	}
    }

    immediate = {

    }

    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
	has_dlc = "Holy Fury"
	trigger = {
		reconquista_check_trigger = yes
	}

	option = {
		name = EVTOPTA_HF_49230

		if = {
			limit = {
				FROMFROM = {
					real_tier = COUNT
				}
			}
			transfer_scaled_wealth = { to = FROMFROM value = 0.25}
		}
		else_if = {
			limit = {
				FROMFROM = {
					real_tier = DUKE
				}
			}
			transfer_scaled_wealth = { to = FROMFROM value = 0.5 }
		}
		else_if = {
			limit = {
				FROMFROM = {
					real_tier = KING
				}
			}
			transfer_scaled_wealth = { to = FROMFROM value = 0.75 }
		}
		else_if = {
			limit = {
				FROMFROM = {
					real_tier = EMPEROR
				}
			}
			transfer_scaled_wealth = { to = FROMFROM value = 1 }
		}

		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
				}
				FROMFROM = {
					join_attacker_wars = PREV
				}
			}
		}

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

	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"

	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 = {
			modifier = 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
	has_dlc = "Holy Fury"

	option = {
		name = EVTOPTA_HF_49233

		if = {
			limit = {
				ROOT = {
					real_tier = COUNT
				}
			}
			transfer_scaled_wealth = { from = FROMFROM value = 0.25}
		}
		else_if = {
			limit = {
				ROOT = {
					real_tier = DUKE
				}
			}
			transfer_scaled_wealth = { from = FROMFROM value = 0.5 }
		}
		else_if = {
			limit = {
				ROOT = {
					real_tier = KING
				}
			}
			transfer_scaled_wealth = { from = FROMFROM value = 0.75 }
		}
		else_if = {
			limit = {
				ROOT = {
					real_tier = 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
					}
					ROOT = {
						join_attacker_wars = PREV
					}
				}
			}
		}

		if = {
			limit = {
				trait = content
			}
			remove_trait = content
		}
		else_if = {
			limit = {
				NOT = {
					trait = ambitious
				}
			}
			add_trait = ambitious
		}
	}

	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
    hide_window = yes
    is_triggered_only = yes
    has_dlc = "Holy Fury"
    trigger = {
    	has_character_modifier = reconquista_preparation_duration
    	not_reconquista_check_trigger = yes
    	has_global_flag = active_reconquista
    }

    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 = {
	        		NOT = {
	        			year = 900
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    light_infantry = { 300 300 }
	                    archers = { 100 100 }
	                }
	                earmark = reconquista_event_troops
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else_if = {
	        	limit = {
	        		NOT = {
	        			year = 1000
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    light_infantry = { 400 400 }
	                    archers = { 150 150 }
	                }
	                earmark = reconquista_event_troops
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else_if = {
	        	limit = {
	        		NOT = {
	        			year = 1100
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    light_infantry = { 500 500 }
	                    archers = { 200 200 }
	                }
	                earmark = reconquista_event_troops
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else_if = {
	        	limit = {
	        		NOT = {
	        			year = 1200
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    light_infantry = { 600 600 }
	                    archers = { 250 250 }
	                }
	                earmark = reconquista_event_troops
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else_if = {
	        	limit = {
	        		NOT = {
	        			year = 1300
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    light_infantry = { 700 700 }
	                    archers = { 300 300 }
	                }
	                earmark = reconquista_event_troops
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else_if = {
	        	limit = {
	        		NOT = {
	        			year = 1400
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    light_infantry = { 800 800 }
	                    archers = { 350 350 }
	                }
	                earmark = reconquista_event_troops
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else = {
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    light_infantry = { 900 900 }
	                    archers = { 400 400 }
	                }
	                earmark = reconquista_event_troops
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
        }
    }
    # 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 = {
	        		NOT = {
	        			year = 900
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    light_infantry = { 300 300 }
	                    heavy_infantry = { 200 200 }
	                    archers = { 100 100 }
	                }
	                earmark = reconquista_event_troops
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else_if = {
	        	limit = {
	        		NOT = {
	        			year = 1000
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    light_infantry = { 350 350 }
	                    heavy_infantry = { 250 250 }
	                    archers = { 150 150 }
	                }
	                earmark = reconquista_event_troops
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else_if = {
	        	limit = {
	        		NOT = {
	        			year = 1100
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    light_infantry = { 400 400 }
	                    heavy_infantry = { 300 300 }
	                    archers = { 200 200 }
	                }
	                earmark = reconquista_event_troops
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else_if = {
	        	limit = {
	        		NOT = {
	        			year = 1200
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    light_infantry = { 450 450 }
	                    heavy_infantry = { 350 350 }
	                    archers = { 250 250 }
	                }
	                earmark = reconquista_event_troops
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else_if = {
	        	limit = {
	        		NOT = {
	        			year = 1300
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    light_infantry = { 500 500 }
	                    heavy_infantry = { 400 400 }
	                    archers = { 300 300 }
	                }
	                earmark = reconquista_event_troops
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else_if = {
	        	limit = {
	        		NOT = {
	        			year = 1400
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    light_infantry = { 550 550 }
	                    heavy_infantry = { 450 450 }
	                    archers = { 350 350 }
	                }
	                earmark = reconquista_event_troops
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else = {
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    light_infantry = { 600 600 }
	                    heavy_infantry = { 500 500 }
	                    archers = { 400 400 }
	                }
	                earmark = reconquista_event_troops
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
        }
    }
    # 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 = {
	        		NOT = {
	        			year = 900
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    heavy_infantry = { 200 200 }
	                    pikemen = { 200 200 }
	                    light_cavalry = { 150 150 }
	                    archers = { 100 100 }
	                }
	                earmark = reconquista_event_troops
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else_if = {
	        	limit = {
	        		NOT = {
	        			year = 1000
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    heavy_infantry = { 250 250 }
	                    pikemen = { 250 250 }
	                    light_cavalry = { 200 200 }
	                    archers = { 150 150 }
	                }
	                earmark = reconquista_event_troops
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else_if = {
	        	limit = {
	        		NOT = {
	        			year = 1100
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    heavy_infantry = { 300 300 }
	                    pikemen = { 300 300 }
	                    light_cavalry = { 250 250 }
	                    archers = { 200 200 }
	                }
	                earmark = reconquista_event_troops
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else_if = {
	        	limit = {
	        		NOT = {
	        			year = 1200
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    heavy_infantry = { 350 350 }
	                    pikemen = { 350 350 }
	                    light_cavalry = { 300 300 }
	                    archers = { 250 250 }
	                }
	                earmark = reconquista_event_troops
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else_if = {
	        	limit = {
	        		NOT = {
	        			year = 1300
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    heavy_infantry = { 400 400 }
	                    pikemen = { 400 400 }
	                    light_cavalry = { 350 350 }
	                    archers = { 300 300 }
	                }
	                earmark = reconquista_event_troops
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else_if = {
	        	limit = {
	        		NOT = {
	        			year = 1400
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    heavy_infantry = { 450 450 }
	                    pikemen = { 450 450 }
	                    light_cavalry = { 400 400 }
	                    archers = { 350 350 }
	                }
	                earmark = reconquista_event_troops
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else = {
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    heavy_infantry = { 500 500 }
	                    pikemen = { 500 500 }
	                    light_cavalry = { 450 450 }
	                    archers = { 400 400 }
	                }
	                earmark = reconquista_event_troops
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
    	}
    }
    # 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 = {
	        		NOT = {
	        			year = 900
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    light_infantry = { 300 300 }
	                }
	                earmark = reconquista_event_troops_rabble
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else_if = {
	        	limit = {
	        		NOT = {
	        			year = 1000
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    light_infantry = { 400 400 }
	                }
	                earmark = reconquista_event_troops_rabble
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else_if = {
	        	limit = {
	        		NOT = {
	        			year = 1100
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    light_infantry = { 500 500 }
	                }
	                earmark = reconquista_event_troops_rabble
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else_if = {
	        	limit = {
	        		NOT = {
	        			year = 1200
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    light_infantry = { 600 600 }
	                }
	                earmark = reconquista_event_troops_rabble
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else_if = {
	        	limit = {
	        		NOT = {
	        			year = 1300
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    light_infantry = { 700 700 }
	                }
	                earmark = reconquista_event_troops_rabble
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else_if = {
	        	limit = {
	        		NOT = {
	        			year = 1400
	        		}
	        	}
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    light_infantry = { 800 800 }
	                }
	                earmark = reconquista_event_troops_rabble
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
	        else = {
	        	spawn_unit = {
	                province = capital_scope
	                home = capital_scope
	                owner = ROOT
	                troops = {
	                    light_infantry = { 900 900 }
	                }
	                earmark = reconquista_event_troops_rabble
	                attrition = 1
	                maintenance_multiplier = 0
	                reinforces = no
	            }
	        }
    	}
    }
}

# 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"
    trigger = {
    
    }

    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
    	if = {
    		limit = {
    			NOT = {
    				trait = humble
    			}
    		}
    		add_trait = humble
    	}
    }
    after = {
    	add_character_modifier = {
    		name = reconquista_pope_aid_event_cooldown
    		hidden = yes
    		months = 3
    	}
    }
}

# 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"
    trigger = {
    
    }

    immediate = {
    	hidden_tooltip = {
    		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 = {
    				modifier = opinion_loyal_servant
    				who = ROOT
    			}

    			add_trait = brilliant_strategist
    			# Randomize Culture
    			random_list = {
    				# Keep Catalan
    				3 = {
    				}
    				# Other Iberian Cultures
    				3 = {
    					culture = basque
    				}
    				3 = {
    					culture = castillan
    				}
    				3 = {
    					trigger = {
    						year = 950
    					}
    					culture = portuguese
    				}
    				3 = {
    					trigger = {
    						NOT = {
    							year = 900
    						}
    					}
    					culture = visigothic
    				}
    				# Other close by (normally Catholic) cultures
    				3 = {
    					trigger = {
    						NOT = {
    							year = 850
    						}
    					}
    					culture = suebi
    				}
    				2 = {
    					culture = occitan
    				}
    				1 = {
    					trigger = {
    						year = 850
    					}
    					culture = frankish
    				}
    				1 = {
    					culture = italian
    				}
    				1 = {
    					trigger = {
    						NOT = {
    							year = 900
    						}
    					}
    					culture = lombard
    				}
    				1 = {
    					culture = german
    				}
    				2 = {
    					trigger = {
    						NOT = {
    							year = 850
    						}
    					}
    					culture = old_frankish
    				}
    				2 = {
    					trigger = {
    						year = 950
    					}
    					culture = norman
    				}
    			}
    			# Some sort of huge
    			random_list = {
    				1 = {
    					add_trait = strong
    				}
    				1 = {
    					add_trait = robust
    				}
    			}
    			# 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 = {
			OR = { 
				NOT = { ROOT = { has_law = succ_byzantine_elective } }  #Either not Imperial...
				NOT = { #...Or there are no valid vassal candidates that can be commanders.
					any_vassal = { 
						higher_real_tier_than = BARON
						NOT = { 
							primary_title = { temporary = yes } 
						}
						OR = {
							AND = {
								OR = {
									male_can_hold_minor_title_trigger = yes
									female_can_hold_minor_commander_title_trigger = yes
								}
								OR = {
									NOT = { religion = hindu }
									trait = kshatriya
								}
								is_adult = yes
							}
							has_character_flag = special_marshal
						}
				
						prisoner = no
						NOT = { trait = incapable }
						NOT = { is_inaccessible_trigger = yes }
						NOT = { has_character_flag = guru }
						NOT = { has_character_modifier = refusing_to_lead }
						NOR = {
							has_job_title = job_chancellor
							has_job_title = job_marshal
							has_job_title = job_treasurer
							has_job_title = job_spymaster
							has_job_title = job_spiritual
							has_minor_title = title_commander
						}
					} 
				}
			}
        	OR = {
				AND = {
					tier = COUNT
					NOT = {
						any_realm_character = {
							count = 2
							liege = { character = ROOT }
							has_minor_title = title_commander
						}
					}
				}
				AND = {
					tier = DUKE
					NOT = {
						any_realm_character = {
							count = 4
							liege = { character = ROOT }
							has_minor_title = title_commander
						}
					}
				}
				AND = {
					tier = KING
					is_nomadic = no
					NOT = {
						any_realm_character = {
							count = 6
							liege = { character = ROOT }
							has_minor_title = title_commander
						}
					}
				}
				AND = {
					tier = EMPEROR
					is_nomadic = no
					NOT = {
						any_realm_character = {
							count = 8
							liege = { character = ROOT }
							has_minor_title = title_commander
						}
					}
				}
				AND = {
					tier = KING
					is_nomadic = yes
					NOT = {
						any_realm_character = {
							count = 2
							liege = { character = ROOT }
							has_minor_title = title_commander
						}
					}
				}
				AND = {
					tier = EMPEROR
					is_nomadic = yes
					NOT = {
						any_realm_character = {
							count = 4
							liege = { character = ROOT }
							has_minor_title = title_commander
						}
					}
				}
			}
        }

        event_target:new_commander = {
        	give_minor_title = title_commander
        }
    }
    option = {
    	name = EVTOPTC_HF_49243 
    	trigger = {
    		NOT = {
    			has_character_flag = reconquista_adventurer
    		}
    	}

    	hidden_tooltip = {
			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
    			hidden = yes
    			days = 15
    		}
    	}
    	else = {
    		add_character_modifier = {
    		name = reconquista_commander_event_cooldown
	    		hidden = yes
	    		months = 4
	    	}
    	}
    }
}

# 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 = {
        		NOT = {
        			year = 850
        		}
        	}
        	spawn_fleet = {
	        	province = closest
	        	owner = ROOT
	        	earmark = conscripted_reconquista_ships
	        	troops = {
	        		galleys = { 10 10 }
	        	}
	        }
        }
        else_if = {
        	limit = {
        		NOT = {
        			year = 950
        		}
        	}
        	spawn_fleet = {
	        	province = closest
	        	owner = ROOT
	        	earmark = conscripted_reconquista_ships
	        	troops = {
	        		galleys = { 20 20 }
	        	}
	        }
        }
        else_if = {
        	limit = {
        		NOT = {
        			year = 1050
        		}
        	}
        	spawn_fleet = {
	        	province = closest
	        	owner = ROOT
	        	earmark = conscripted_reconquista_ships
	        	troops = {
	        		galleys = { 30 30 }
	        	}
	        }
        }
        else_if = {
        	limit = {
        		NOT = {
        			year =1150
        		}
        	}
        	spawn_fleet = {
	        	province = closest
	        	owner = ROOT
	        	earmark = conscripted_reconquista_ships
	        	troops = {
	        		galleys = { 40 40 }
	        	}
	        }
        }
        else_if = {
        	limit = {
        		NOT = {
        			year = 1250
        		}
        	}
        	spawn_fleet = {
	        	province = closest
	        	owner = ROOT
	        	earmark = conscripted_reconquista_ships
	        	troops = {
	        		galleys = { 50 50 }
	        	}
	        }
        }
        else_if = {
        	limit = {
        		NOT = {
        			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 = {
        		NOT = {
        			year = 850
        		}
        	}
        	spawn_fleet = {
	        	province = closest
	        	owner = ROOT
	        	earmark = conscripted_reconquista_ships
	        	troops = {
	        		galleys = { 20 20 }
	        	}
	        }
        }
        else_if = {
        	limit = {
        		NOT = {
        			year = 950
        		}
        	}
        	spawn_fleet = {
	        	province = closest
	        	owner = ROOT
	        	earmark = conscripted_reconquista_ships
	        	troops = {
	        		galleys = { 40 40 }
	        	}
	        }
        }
        else_if = {
        	limit = {
        		NOT = {
        			year = 1050
        		}
        	}
        	spawn_fleet = {
	        	province = closest
	        	owner = ROOT
	        	earmark = conscripted_reconquista_ships
	        	troops = {
	        		galleys = { 60 60 }
	        	}
	        }
        }
        else_if = {
        	limit = {
        		NOT = {
        			year =1150
        		}
        	}
        	spawn_fleet = {
	        	province = closest
	        	owner = ROOT
	        	earmark = conscripted_reconquista_ships
	        	troops = {
	        		galleys = { 80 80 }
	        	}
	        }
        }
        else_if = {
        	limit = {
        		NOT = {
        			year = 1250
        		}
        	}
        	spawn_fleet = {
	        	province = closest
	        	owner = ROOT
	        	earmark = conscripted_reconquista_ships
	        	troops = {
	        		galleys = { 100 100 }
	        	}
	        }
        }
        else_if = {
        	limit = {
        		NOT = {
        			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
    			hidden = yes
    			months = 1
    		}
    	}
    	else = {
    		add_character_modifier = {
    		name = reconquista_boat_event_cooldown
	    		hidden = yes
	    		months = 11
	    	}
    	}
    }
}

# 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_dlc = "Holy Fury"

    trigger = {
    	not_reconquista_check_trigger = yes
    	has_global_flag = active_reconquista
    }

    option = {      
        name = EVTOPTA_HF_49249
    }

    after = {
    	# Find suitable target for Reconquista Adventurers
    	if = {
    		limit = {
    			OR = {
    				has_character_flag = reconquista_adventurer
    				has_character_flag = ai_reconquista_preparation_time_run_out
    			}
    		}
    		hidden_tooltip = {
    			# Navarra or Aragon is held by a Muslim
    			if = {
    				limit = {
    					any_province = {
    						county = {
	    						OR = {
	    							region = custom_aragon
	    							region = custom_navarra
	    						}
    						}
    						owner = {
    							OR = {
    								AND = {
    									independent = yes
    									NOT = {
    										religion_group = christian
    									}
    								}
    								top_liege = {
    									NOT = {
    										religion_group = christian
    									}
    								}
    							}    						
							}
    					}
    				}
    				random_list = {
    					# Aragon
    					1 = {
    						trigger = {
    							any_province = {
    								county = {
    									region = custom_aragon
    								}
    								owner = {
    									OR = {
		    								AND = {
		    									independent = yes
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    								top_liege = {
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    							}
    								}
    							}
    						}
    						random_province = {
    							limit = {
    								county = {
    									region = custom_aragon
    								}
    								owner = {
    									OR = {
		    								AND = {
		    									independent = yes
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    								top_liege = {
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    							}
    								}
    							}
    							owner = {
    								if = {
    									limit = {
    										independent = yes
    									}
    									ROOT = {
    										unsafe_war = {
    											target = PREV
    											casus_belli = reconquista_aragon
    										}
    									}
    								}
    								else = {
    									top_liege = {
    										ROOT = {
    											unsafe_war = {
    												target = PREV
    												casus_belli = reconquista_aragon
    											}
    										}
    									}
    								}
    							}
    						}
    					}
    					# Navarra
    					1 = {
    						trigger = {
    							any_province = {
    								county = {
    									region = custom_navarra
    								}
    								owner = {
    									NOT = {
    										religion_group = christian
    									}
    									OR = {
    										independent = yes
    										NOT = {
	    										top_liege = {
	    											religion_group = christian
	    										}
	    									}
    									}
    								}
    							}
    						}
    						random_province = {
    							limit = {
    								county = {
    									region = custom_navarra
    								}
    								owner = {
    									OR = {
		    								AND = {
		    									independent = yes
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    								top_liege = {
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    							}
    								}
    							}
    							owner = {
    								if = {
    									limit = {
    										independent = yes
    									}
    									ROOT = {
    										unsafe_war = {
    											target = PREV
    											casus_belli = reconquista_navarra
    										}
    									}
    								}
    								else = {
    									top_liege = {
    										ROOT = {
    											unsafe_war = {
    												target = PREV
    												casus_belli = reconquista_navarra
    											}
    										}
    									}
    								}
    							}
    						}
    					}
    				}
    			}
    			# Leon, Galicia or Castille is held by a Muslim
    			else_if = {
    				limit = {
    					any_province = {
    						county = {
	    						OR = {
	    							region = custom_leon
	    							region = custom_galicia
	    							region = custom_castille
	    						}
    						}
    						owner = {
    							OR = {
    								AND = {
    									independent = yes
    									NOT = {
    										religion_group = christian
    									}
    								}
    								top_liege = {
    									NOT = {
    										religion_group = christian
    									}
    								}
    							}
    						}
    					}
    				}
    				random_list = {
    					# Leon
    					1 = {
    						trigger = {
    							any_province = {
    								county = {
    									region = custom_leon
    								}
    								owner = {
    									OR = {
		    								AND = {
		    									independent = yes
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    								top_liege = {
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    							}
    								}
    							}
    						}
    						random_province = {
    							limit = {
    								county = {
    									region = custom_leon
    								}
    								owner = {
    									OR = {
		    								AND = {
		    									independent = yes
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    								top_liege = {
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    							}
    								}
    							}
    							owner = {
    								if = {
    									limit = {
    										independent = yes
    									}
    									ROOT = {
    										unsafe_war = {
    											target = PREV
    											casus_belli = reconquista_leon
    										}
    									}
    								}
    								else = {
    									top_liege = {
    										ROOT = {
    											unsafe_war = {
    												target = PREV
    												casus_belli = reconquista_leon
    											}
    										}
    									}
    								}
    							}
    						}
    					}
    					# Galicia
    					1 = {
    						trigger = {
    							any_province = {
    								county = {
    									region = custom_galicia
    								}
    								owner = {
    									OR = {
		    								AND = {
		    									independent = yes
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    								top_liege = {
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    							}
    								}
    							}
    						}
    						random_province = {
    							limit = {
    								county = {
    									region = custom_galicia
    								}
    								owner = {
    									OR = {
		    								AND = {
		    									independent = yes
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    								top_liege = {
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    							}
    								}
    							}
    							owner = {
    								if = {
    									limit = {
    										independent = yes
    									}
    									ROOT = {
    										unsafe_war = {
    											target = PREV
    											casus_belli = reconquista_galicia
    										}
    									}
    								}
    								else = {
    									top_liege = {
    										ROOT = {
    											unsafe_war = {
    												target = PREV
    												casus_belli = reconquista_galicia
    											}
    										}
    									}
    								}
    							}
    						}
    					}
    					# Castille
    					1 = {
    						trigger = {
    							any_province = {
    								county = {
    									region = custom_castille
    								}
    								owner = {
    									OR = {
		    								AND = {
		    									independent = yes
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    								top_liege = {
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    							}
    								}
    							}
    						}
    						random_province = {
    							limit = {
    								county = {
    									region = custom_castille
    								}
    								owner = {
    									OR = {
		    								AND = {
		    									independent = yes
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    								top_liege = {
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    							}
    								}
    							}
    							owner = {
    								if = {
    									limit = {
    										independent = yes
    									}
    									ROOT = {
    										unsafe_war = {
    											target = PREV
    											casus_belli = reconquista_castille
    										}
    									}
    								}
    								else = {
    									top_liege = {
    										ROOT = {
    											unsafe_war = {
    												target = PREV
    												casus_belli = reconquista_castille
    											}
    										}
    									}
    								}
    							}
    						}
    					}
    				}
    			}
    			# Beja or Valencia is held by a Muslim
    			else_if = {
    				limit = {
    					any_province = {
    						county = {
    							OR = {
    								region = custom_beja
    								region = custom_valencia
    							}
    						}
    						owner = {
    							OR = {
    								AND = {
    									independent = yes
    									NOT = {
    										religion_group = christian
    									}
    								}
    								top_liege = {
    									NOT = {
    										religion_group = christian
    									}
    								}
    							}
    						}
    					}
    				}
    				random_list = {
    					# Beja
    					1 = {
    						trigger = {
    							any_province = {
    								county = {
    									region = custom_beja
    								}
    								owner = {
    									OR = {
		    								AND = {
		    									independent = yes
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    								top_liege = {
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    							}
    								}
    							}
    						}
    						random_province = {
    							limit = {
    								county = {
    									region = custom_beja
    								}
    								owner = {
    									OR = {
		    								AND = {
		    									independent = yes
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    								top_liege = {
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    							}
    								}
    							}
    							owner = {
    								if = {
    									limit = {
    										independent = yes
    									}
    									ROOT = {
    										unsafe_war = {
    											target = PREV
    											casus_belli = reconquista_beja
    										}
    									}
    								}
    								else = {
    									top_liege = {
    										ROOT = {
    											unsafe_war = {
    												target = PREV
    												casus_belli = reconquista_beja
    											}
    										}
    									}
    								}
    							}
    						}
    					}
    					# Valencia
    					1 = {
    						trigger = {
    							any_province = {
    								county = {
    									region = custom_valencia
    								}
    								owner = {
    									OR = {
		    								AND = {
		    									independent = yes
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    								top_liege = {
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    							}
    								}
    							}
    						}
    						random_province = {
    							limit = {
    								county = {
    									region = custom_valencia
    								}
    								owner = {
    									OR = {
		    								AND = {
		    									independent = yes
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    								top_liege = {
		    									NOT = {
		    										religion_group = christian
		    									}
		    								}
		    							}
    								}
    							}
    							owner = {
    								if = {
    									limit = {
    										independent = yes
    									}
    									ROOT = {
    										unsafe_war = {
    											target = PREV
    											casus_belli = reconquista_valencia
    										}
    									}
    								}
    								else = {
    									top_liege = {
    										ROOT = {
    											unsafe_war = {
    												target = PREV
    												casus_belli = reconquista_valencia
    											}
    										}
    									}
    								}
    							}
    						}
    					}
    				}
    			}
    			# Cordoba is held by a Muslim
    			else_if = {
    				limit = {
    					any_province = {
    						county = {
    							region = custom_cordoba
    						}
    						owner = {
    							OR = {
    								AND = {
    									independent = yes
    									NOT = {
    										religion_group = christian
    									}
    								}
    								top_liege = {
    									NOT = {
    										religion_group = christian
    									}
    								}
    							}
    						}
    					}
    				}
    				random_province = {
						limit = {
							county = {
								region = custom_cordoba
							}
							owner = {
								OR = {
    								AND = {
    									independent = yes
    									NOT = {
    										religion_group = christian
    									}
    								}
    								top_liege = {
    									NOT = {
    										religion_group = christian
    									}
    								}
    							}
							}
						}
						owner = {
							if = {
								limit = {
									independent = yes
								}
								ROOT = {
									unsafe_war = {
										target = PREV
										casus_belli = reconquista_cordoba
									}
								}
							}
							else = {
								top_liege = {
									ROOT = {
										unsafe_war = {
											target = PREV
											casus_belli = reconquista_cordoba
										}
									}
								}
							}
						}
					}
    			}
    			# Granada is held by a Muslim
    			else_if = {
    				limit = {
    					any_province = {
    						county = {
								region = custom_granada
							}
    						owner = {
    							OR = {
    								AND = {
    									independent = yes
    									NOT = {
    										religion_group = christian
    									}
    								}
    								top_liege = {
    									NOT = {
    										religion_group = christian
    									}
    								}
    							}
    						}
    					}
    				}
					random_province = {
						limit = {
							county = {
								region = custom_granada
							}
							owner = {
								OR = {
    								AND = {
    									independent = yes
    									NOT = {
    										religion_group = christian
    									}
    								}
    								top_liege = {
    									NOT = {
    										religion_group = christian
    									}
    								}
    							}
							}
						}
						owner = {
							if = {
								limit = {
									independent = yes
								}
								ROOT = {
									unsafe_war = {
										target = PREV
										casus_belli = reconquista_granada
									}
								}
							}
							else = {
								top_liege = {
									ROOT = {
										unsafe_war = {
											target = PREV
											casus_belli = reconquista_granada
										}
									}
								}
							}
						}
					}
    			}
    			# Balearic Islands is held by a Muslim
    			else_if = {
    				limit = {
    					any_province = {
    						county = {
								region = custom_balearic
							}
    						owner = {
    							OR = {
    								AND = {
    									independent = yes
    									NOT = {
    										religion_group = christian
    									}
    								}
    								top_liege = {
    									NOT = {
    										religion_group = christian
    									}
    								}
    							}
    						}
    					}
    				}
					random_province = {
						limit = {
							county = {
								region = custom_balearic
							}
							owner = {
								OR = {
    								AND = {
    									independent = yes
    									NOT = {
    										religion_group = christian
    									}
    								}
    								top_liege = {
    									NOT = {
    										religion_group = christian
    									}
    								}
    							}
							}
						}
						owner = {
							if = {
								limit = {
									independent = yes
								}
								ROOT = {
									unsafe_war = {
										target = PREV
										casus_belli = reconquista_balearic
									}
								}
							}
							else = {
								top_liege = {
									ROOT = {
										unsafe_war = {
											target = PREV
											casus_belli = reconquista_balearic
										}
									}
								}
							}
						}
					}
    			}
    			# End the Reconquista
    			else = {
    				primary_title = {
    					destroy_landed_title = THIS
    				}
    				set_global_flag = reconquista_finished
    				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
    desc = EVTDESC_HF_49450
    title = EVTTITLE_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
    }

    immediate = {

    }

    option = {
        name = EVTOPTA_HF_49450
        trigger = {
        	religion_group = FROM
        }
    }

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

# Empire of Hispania created
narrative_event = {
    id = HF.49451
    desc = EVTDESC_HF_49451
    title = EVTTITLE_HF_49451
    picture = GFX_evt_emissary
    border = GFX_event_narrative_frame_religion
    is_triggered_only = yes
    has_dlc = "Holy Fury"
    trigger = {
    	is_alternate_start = no
    	NOT = {
    		has_global_flag = reconquista_finished
    	}
    }

    immediate = {
    	if = {
    		limit = {
    			NOT = {
    				has_global_flag = reconquista_finished
    			}
    		}
    		set_global_flag = reconquista_finished
    	}
    }

    option = {      
        name = EVTOPTA_HF_49451
        trigger = {
        	religion_group = FROM
        }
    }

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

# End of the Reconquista
narrative_event = {
    id = HF.49452
    desc = EVTDESC_HF_49452
    title =EVTTITLE_HF_49452
    picture = GFX_evt_beatification
    border = GFX_event_narrative_frame_religion
    is_triggered_only = yes
    has_dlc = "Holy Fury"
    trigger = {
    	is_alternate_start = no
    	NOT = {
    		has_global_flag = reconquista_finished
    	}
    }

    immediate = {
    	if = {
    		limit = {
    			NOT = {
    				has_global_flag = reconquista_finished
    			}
    		}
    		set_global_flag = reconquista_finished
    	}
    }

    option = {      
        name = EVTOPTA_HF_49452
        trigger = {
        	religion_group = christian
        }
    }

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

## Adventurer Reconquista ##
# Make a fitting adventurer
character_event = {
    id = HF.49460
    hide_window = yes
    is_triggered_only = yes
    has_dlc = "Holy Fury"
    trigger = {
    	has_game_rule = {
    		name = reconquista
    		value = on
    	}
    	war = no
    	NOT = {
    		has_global_flag = active_reconquista
    	}
    	any_demesne_title = {
			higher_tier_than = DUKE
			region = world_europe_west_iberia
		}
		religion_group = christian
		culture_group = iberian
		NOT = {
			has_global_flag = reconquista_finished
		}
		OR = {
			AND = {
				random = 75
				any_province = {
					county = {
						OR = {
							region = custom_castille
							region = custom_leon
							region = custom_galicia
							region = custom_aragon
							region = custom_navarra
						}
					}
					owner = {
						OR = {
							AND = {
								independent = yes
								NOT = {
									religion_group = christian
								}
							}
							top_liege = {
								NOT = {
									religion_group = christian
								}
							}
						}
					}
				}
				tier = KING
			}
			AND = {
				random = 85
				any_province = {
					county = {
						OR = {
							region = custom_castille
							region = custom_leon
							region = custom_galicia
							region = custom_aragon
							region = custom_navarra
						}
					}
					owner = {
						OR = {
							AND = {
								independent = yes
								NOT = {
									religion_group = christian
								}
							}
							top_liege = {
								NOT = {
									religion_group = christian
								}
							}
						}
					}
				}
				tier = EMPEROR
			}
		}
		NOT = {
			has_character_modifier = reconquista_adventurer_cooldown
		}
    }

    immediate = {
    	random_list = {
    		1 = {
    			create_character = {
					culture = ROOT
					religion = ROOT
					age = 24
					dynasty = random
					random_traits = yes
				}
    		}
    		1 = {
    			create_character = {
					culture = ROOT
					religion = ROOT
					age = 24
					female = yes
					dynasty = random
					random_traits = yes
				}
    		}
    	}

		new_character = {
			wealth = 400 
			remove_trait = slow
			remove_trait = imbecile
			remove_trait = weak
			remove_trait = feeble
			random_list = {
				100 = {
					add_trait = genius
				}
				100 = {
					add_trait = shrewd
				}
				100 = {
					add_trait = quick
				}
				100 = {
					add_trait = strong
				}
				100 = {
					add_trait = robust
				}
				1 = {
					trigger = { religion = catholic }
   				    add_artifact = crown_wrapped_helmet
				}
			}
			add_random_commander_trait_effect = yes
			character_event = { id = HF.49461 }
		}
    }

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

# Ping the adventurer 
character_event = {
	id = HF.49461
	hide_window = yes
	is_triggered_only = yes
	has_dlc = "Holy Fury"

	immediate = {
		set_character_flag = reconquista_adventurer
		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
		}
		new_title = { adjective = "RECONQUISTA_ADVENTURE_TITLE_ADJ" } #FIX_ADJ
		spawn_unit = {
            owner = ROOT
            province = event_target:host_capital
            home = event_target:host_capital
            troops = {
                heavy_infantry = { 500 500 }
                pikemen = { 500 500 }
                light_cavalry = { 500 500 }
                archers = { 500 500 }
            }
            earmark = reconquista_event_troops
            attrition = 1
            maintenance_multiplier = 0
        }

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

        if = {
			limit = {
				NOT = {
					has_character_flag = ex_reconquistador
				}
			}
			set_character_flag = ex_reconquistador
		}
		set_character_flag = currently_reconquering_iberia
		set_global_flag = active_reconquista

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

		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
character_event = {
	id = HF.49462
	desc = EVTDESC_HF_49462
	picture = GFX_evt_meeting_hf
	border = GFX_event_normal_frame_religion
	is_triggered_only = yes
	has_dlc = "Holy Fury"

	option = {
		name = EVTOPTA_HF_49462
		piety = 50
		if = {
			limit = {
				NOT = {
					trait = ambitious
				}
			}
			add_trait = ambitious
		}
		set_character_flag = promised_reconquista_ally
		set_character_flag = reconquista_sponsor

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

	option = {
		name = EVTOPTB_HF_49462 
		trigger = {
			ai = no
		}
		piety = -50
	}
}

# 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
    trigger = {
    	has_character_flag = reconquista_sponsor
    }

    immediate = {
    	clr_character_flag = reconquista_sponsor
    }

    option = {      
        name = EVTOPTA_HF_49463
        trigger_switch = {
        	on_trigger = tier
        	baron = { prestige = -50 }
        	count = { prestige = -100 }
        	duke = { prestige = -200 }
        	king = { prestige = -300 }
        	emperor = { prestige = -500 }
        }
        custom_tooltip = {
        	text = reconquista_renowned_sponsor_TT

	        FROMFROM = {
	        	show_scope_change = no
	        	add_character_modifier = {
	        		name = renowned_sponsor
	        		years = 2
	        	}
	        }
        }
    }
    option = {      
        name = EVTOPTB_HF_49463
        trigger_switch = {
        	on_trigger = tier
        	baron = { piety = -25 }
        	count = { piety = -50 }
        	duke = { piety = -100 }
        	king = { piety = -200 }
        	emperor = { piety = -300 }
        }
        custom_tooltip = {
        	text = reconquista_pious_sponsor_TT

	        FROMFROM = {
	        	show_scope_change = no
	        	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 = {
	    		show_scope_change = no
	    		add_character_modifier = {
	    			name = wealthy_sponsor
	    			years = 2
	    		}
	    	}
    	}
    }
    option = {
    	name = EVTOPTD_HF_49463
    }
}

## Flavour Events for Reconquista ##
# Tombola
character_event = {
    id = HF.49470
    hide_window = yes
    is_triggered_only = yes
    has_dlc = "Holy Fury"
    trigger = {
    	religion_group = christian
    	location = {
    		owner = {
    			OR = {
    				AND = {
    					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 = PREVPREV
		    				}
		    			}
		    			independent = yes
    				}
    				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
		    				}
		    			}
    				}
    			}
    			
    		}
    	}
    	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
			}
		}
		prisoner = no
		age = 16
		in_command = yes
		NOT = {
			has_character_modifier = reconquista_flavor_cooldown
		}
    }

    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
    		hidden = yes
    		days = 25
    	}
    }
}

# Reconquista flavor event randomizer
character_event = {
    id = HF.49471
    hide_window = yes
    is_triggered_only = yes
    has_dlc = "Holy Fury"
    ai = no
    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
			}
		}
        religion_group = christian
    }

    immediate = {
        random_list = {
            # Nothing happens
            3 = {}
            1 = {
                character_event = { id = HF.49470 days = 1 random = 15 }
            }
        }
    }
}

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

    immediate = {
    	set_global_flag = reconquista_ai_cooldown
    	333 = {
    		province_event = { id = HF.49481 }
    	}
    }
}

# Ping province
province_event = {
	id = HF.49481
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		333 = {
			province_event = { id = HF.49482 days = 5475 }
		}
	}
}

# Cooldown finished
province_event = {
	id = HF.49482
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		clr_global_flag = reconquista_ai_cooldown
	}
}