﻿#MODIFIED VANILLA EVENTS

###DECISIONS LIST###

#form_portugal_decision
#unite_the_spanish_thrones_decision
#unite_africa_decision
#avenge_the_battle_of_tours_decision
#build_grand_church_decision

form_portugal_decision = {
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_found_kingdom.dds"
	}
	decision_group_type = major
	ai_check_interval = 36
	desc = form_portugal_decision_desc

	is_shown = {
		culture = { has_cultural_pillar = heritage_iberian }
		capital_province = { geographical_region = custom_portuguese }
		NOT = {
			is_target_in_global_variable_list = {
				name = unavailable_unique_decisions
				target = flag:form_portugal_decision
			}
		}
		title:k_portugal = {
			is_title_created = no
		}
		highest_held_title_tier <= tier_duchy
		game_start_date < 1139.1.1	
	}

	is_valid = {
		culture = { has_cultural_pillar = heritage_iberian }
		prestige_level >= high_prestige_level
		trigger_if = {
			limit = {
				has_fp2_dlc_trigger = yes
				exists = struggle:iberian_struggle
			}
			OR = {
				custom_tooltip = {
					text = struggle_iberia_form_portugal_decision_correct_phase_tt
					any_character_struggle = {
						involvement = involved
						is_struggle_type = iberian_struggle
						is_struggle_phase = struggle_iberia_phase_opportunity
					}
				}
			}
			OR = {
				completely_controls_region = custom_core_portucale
				AND = {
					has_title = title:d_porto
					has_title = title:d_coimbra
					has_title = title:d_beja
					has_title = title:d_algarve
					has_title = title:d_ribatejo #MPE
				}	
			}			
			OR = {
				has_title = title:c_porto
				has_title = title:d_porto
			}
			# OR = {
			# 	has_title = title:c_lisboa
			# 	has_title = title:d_beja
			# }
			is_independent_ruler = yes
		}
		trigger_else = {
			is_independent_ruler = yes
		}
	}

	is_valid_showing_failures_only = {
		game_start_date < 1139.1.1	
		is_capable_adult = yes
		is_imprisoned = no
		is_at_war = no
		is_landed = yes
	}
	
	effect = {
		save_scope_as = portugal_former
		gain_heroic_legend_seed_tooltip_effect = yes
		show_as_tooltip = { form_portugal_decision_effects = yes } #Actually applied in iberia_north_africa.0001 - title changes, prestige, culture swap etc.

		#Events
		trigger_event = iberia_north_africa.0001
		every_player = {
			limit = {
				NOT = { this = scope:portugal_former }
				is_within_diplo_range = { CHARACTER = scope:portugal_former }
			}
			trigger_event = iberia_north_africa.0002
		}
		# every_held_title = { # And any other held Duchy.
		# 	limit = {
		# 		tier = tier_duchy
		# 	}
		# 	set_de_jure_liege_title = title:k_portugal
		# }
		every_vassal_or_below = {
			limit = {
				is_ai = yes
				exists = capital_province
				capital_province = {
					county = {
						any_this_title_or_de_jure_above = {
							this = title:k_portugal
						}
					}
				}
			}
			trigger_event = iberia_north_africa.0002 #So they can convert
		}
		#Can only happen once
		add_to_global_variable_list = {
			name = unavailable_unique_decisions
			target = flag:form_portugal_decision
		}
		set_global_variable = {
			name = form_portugal_decision
			value = scope:portugal_former
		}
	}

	cost = { gold = { value = 300 } }

	ai_potential = {
		# Only for the AI if independent ruler
		is_independent_ruler = yes
	}

	ai_will_do = {
		base = 100
	}
}



unite_the_spanish_thrones_decision = {
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_found_kingdom.dds"
	}
	decision_group_type = major
	ai_check_interval = 60
	desc = unite_the_spanish_thrones_decision_desc

	is_shown = {
		culture = { has_cultural_pillar = heritage_iberian }
		game_start_date >= 1000.1.1
		OR = {
			calc_true_if = { #At least 2 of the titles has de jure land
				amount >= 2
				title:k_castille = { any_in_de_jure_hierarchy = { tier = tier_county } }
				title:k_leon = { any_in_de_jure_hierarchy = { tier = tier_county } }
				title:k_spanish_galicia = { any_in_de_jure_hierarchy = { tier = tier_county } }
			}
		}
		NOT = {
			is_target_in_global_variable_list = {
				name = unavailable_unique_decisions
				target = flag:unite_the_spanish_thrones_decision
			}
		}
		highest_held_title_tier < tier_empire				 
	}

	is_valid = {
		culture = { has_cultural_pillar = heritage_iberian }
		trigger_if = {
			limit = { title:k_castille = { any_in_de_jure_hierarchy = { tier = tier_county } } } #Has de jure land
			completely_controls = title:k_castille
			has_title = title:k_castille
		}
		trigger_if = {
			limit = { title:k_leon = { any_in_de_jure_hierarchy = { tier = tier_county } } } #Has de jure land
			completely_controls = title:k_leon
			has_title = title:k_leon
		}
		trigger_if = {
			limit = { title:k_spanish_galicia = { any_in_de_jure_hierarchy = { tier = tier_county } } } #Has de jure land
			completely_controls = title:k_spanish_galicia
			has_title = title:k_spanish_galicia
		}
		custom_description = {
   			text = unite_the_spanish_thrones_decision_primary
			OR = {
				has_primary_title = title:k_castille
				has_primary_title = title:k_leon
				has_primary_title = title:k_spanish_galicia
				has_primary_title = title:k_portugal
				has_primary_title = title:k_aragon
				has_primary_title = title:k_navarra
				has_primary_title = title:k_andalusia
				has_primary_title = title:k_badajoz
				has_primary_title = title:k_valencia
				# has_primary_title = title:k_toledo #NB+ removed
			}
		}
		#Must not be too early
		culture = { has_cultural_era_or_later = culture_era_high_medieval }
		OR = { 
			has_realm_law = crown_authority_2
			has_realm_law = crown_authority_3
		}
	}
	is_valid_showing_failures_only = {
		game_start_date < 1230.9.24
		is_landed = yes
	}

	effect = {
		save_scope_as = spain_uniter
		gain_heroic_legend_seed_tooltip_effect = yes
		show_as_tooltip = { unite_the_spanish_thrones_decision_effects = yes } #Actually applied in iberia_north_africa.0005 - prestige, laws, title changes

		#Events
		trigger_event = iberia_north_africa.0005
		every_player = {
			limit = {
				NOT = { this = scope:spain_uniter }
				is_within_diplo_range = { CHARACTER = scope:spain_uniter }
			}
			trigger_event = iberia_north_africa.0006
		}

		#Can only be done once
		add_to_global_variable_list = {
			name = unavailable_unique_decisions
			target = flag:unite_the_spanish_thrones_decision
		}
		set_global_variable = {
			name = unite_the_spanish_thrones_decision
			value = scope:spain_uniter
		}
		set_global_variable = {
			name = unite_the_spanish_thrones_decision_title
			value = scope:spain_uniter.primary_title
		}
	}

	ai_potential = {
		always = yes
	}

	ai_will_do = {
		base = 100
	}
}

##################################################
# Form the Kingdom of Aragon
# by Sean Hughes & Ewan Cowhig Croft
##################################################

form_the_kingdom_of_aragon_decision = {
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_found_kingdom.dds"
	}
	decision_group_type = major

	ai_check_interval = 60

	desc = form_the_kingdom_of_aragon_decision_desc
	selection_tooltip = form_the_kingdom_of_aragon_decision_tooltip

	is_shown = {	
		is_playable_character = yes # Redundancy check
		# Can't form Aragon if Aragon already formed.
		 game_start_date <= 1019.1.1
		# Not relevant for characters who are already kings.
		highest_held_title_tier <= tier_duchy
		# Cast a broad net for folks who might like to try this.
		capital_province = { geographical_region = dlc_fp2_form_aragon_region }
		# Aaaaand it can't have happened already.
		any_sub_realm_county = {
			OR = {
				target_is_de_jure_liege_or_above = title:d_aragon
				target_is_de_jure_liege_or_above = title:d_barcelona
			}
		}
		NOT = {	
			is_target_in_global_variable_list = {
				name = unavailable_unique_decisions
				target = flag:flag_formed_kingdom_of_aragon
			}
		}
	}

	is_valid = {
		is_independent_ruler = yes
		completely_controls = title:d_aragon
		OR = {
			completely_controls = title:d_barcelona
			completely_controls = title:d_valencia
			#completely_controls = title:d_navarra #Err why would the capital of Navarra be given
			
			completely_controls = title:d_zaragoza
			completely_controls = title:d_teruel #MPE MB+
			completely_controls = title:d_catalunya_nova #MPE MB+
			completely_controls = title:d_urgell #MPE MB+
		}
	}
	
	is_valid_showing_failures_only = {
		is_landed = yes
	}
	
	effect = {
		gain_heroic_legend_seed_tooltip_effect = yes
		# Grab scopes for loc.
		save_scope_as = founder
		culture = { save_scope_as = founder_culture }
		culture:aragonese = { save_scope_as = aragonese_culture }
		title:k_aragon = { save_scope_as = k_aragon }
		title:d_aragon = { save_scope_as = d_aragon }
		title:d_valencia = { save_scope_as = d_valencia }
		title:d_barcelona = { save_scope_as = d_barcelona }
		#title:d_navarra = { save_scope_as = d_navarra } #MB+ removed
		
		##
		
		title:d_zaragoza = { save_scope_as = d_zaragoza }
		title:d_teruel = { save_scope_as = d_teruel } #MPE #MB+
		title:d_catalunya_nova = { save_scope_as = d_catalunya_nova } #MPE #MB+
		title:d_urgell = { save_scope_as = d_urgell } #MPE #MB+
		
		# Create kingdom title.
		create_title_and_vassal_change = {
			type = created
			save_scope_as = change
		}
		title:k_aragon = {
			change_title_holder = {
				holder = scope:founder
				change = scope:change
			}
		}
		resolve_title_and_vassal_change = scope:change
		# Gain some prestige.
		add_prestige = major_prestige_gain
		# You shift to Aragonese culture, but will have a chance to change it.
		## We do the actual change inside the event for nicer tooltip grouping.
		show_as_tooltip = { set_culture = culture:aragonese }
		custom_tooltip = form_the_kingdom_of_aragon_decision.tt.form_aragonese
		trigger_event = iberia_north_africa.0131
		# De jure shifts.
		## Take care of the preferred empire.
		if = {
			limit = {
				NOT = { title:k_aragon.empire = title:d_aragon.empire }
			}
			title:k_aragon = { set_de_jure_liege_title = title:d_aragon.empire }
		}
		## The heartlands drift over.
		title:d_aragon = { set_de_jure_liege_title = title:k_aragon }
		## Various neighbouring duchies can be preemptively integrated.
		
		####
		
		if = {
			limit = { completely_controls = title:d_zaragoza }
			title:d_zaragoza = { set_de_jure_liege_title = title:k_aragon }
		}
		else = { custom_tooltip = form_the_kingdom_of_aragon_decision.tt.drift.d_zaragoza }

		#MPE #MB+
		if = {
			limit = { completely_controls = title:d_teruel }
			title:d_teruel = { set_de_jure_liege_title = title:k_aragon } #MPE #MB+
		}
		else = { custom_tooltip = form_the_kingdom_of_aragon_decision.tt.drift.d_teruel }
		
		if = {
			limit = { completely_controls = title:d_catalunya_nova } #MPE #MB+
			title:d_catalunya_nova = { set_de_jure_liege_title = title:k_aragon } #MPE #MB+
		}
		else = { custom_tooltip = form_the_kingdom_of_aragon_decision.tt.drift.d_catalunya_nova } #MPE #MB+
		
		if = {
			limit = { completely_controls = title:d_urgell }
			title:d_urgell = { set_de_jure_liege_title = title:k_aragon }
		}
		else = { custom_tooltip = form_the_kingdom_of_aragon_decision.tt.drift.d_urgell }
	
		#/MPE #/MB+
		####
		
		
		### From Valencia.
		if = {
			limit = { completely_controls = title:d_valencia }
			title:d_valencia = { set_de_jure_liege_title = title:k_aragon }
		}
		else = { custom_tooltip = form_the_kingdom_of_aragon_decision.tt.drift.d_valencia }
		### From Aquitaine.
		if = {
			limit = { completely_controls = title:d_barcelona }
			title:d_barcelona = { set_de_jure_liege_title = title:k_aragon }
		}
		else = { custom_tooltip = form_the_kingdom_of_aragon_decision.tt.drift.d_barcelona }
		#### From Navarre.
		#if = {
		#	limit = { completely_controls = title:d_navarra }
		#	title:d_navarra = { set_de_jure_liege_title = title:k_aragon }
		#}
		#else = { custom_tooltip = form_the_kingdom_of_aragon_decision.tt.drift.d_navarra }
		# Plus, call the place Aragon rather than Zaragoza.
		title:d_aragon = { reset_title_name = yes }
		# Once per game.
		add_to_global_variable_list = {
			name = unavailable_unique_decisions
			target = flag:flag_formed_kingdom_of_aragon
		}
		# Note this for books and such.
		set_global_variable = {
			name = flag_formed_kingdom_of_aragon
			value = scope:founder
		}

		# Reward suggested by Ewan
		house = {
			add_house_modifier = {
				modifier = fp2_aragon_title_reward_house_modifier # Mountains/hills bonus + hard casualty bonus
				years = 200
			}
		}
	}

	cost = {
		gold = {
			# Since we want this to happen, it free for the AI...
			value = 0
			# ... but costs for players.
			if = {
				limit = { is_ai = no }
				add = 200
			}
		}
	}

	ai_potential = {
		any_held_title = { this = title:d_aragon }
	}

	ai_will_do = {
		base = 100
	}
}


#Mediterannean Aragon
aragonese_expansion_decision = {
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_dynasty_house.dds"
	}
	
	desc = aragonese_expansion_decision_desc
	selection_tooltip = aragonese_expansion_decision_tooltip
	decision_group_type = major
	
	ai_check_interval = 1
	is_shown = {
		current_date >= 1230.1.1
		NOT = { game_start_date >= 1285.1.1 }
		is_playable_character = yes
		trigger_if = {
			limit = {
				NOT = { exists = title:k_aragon.holder }
			}
			has_primary_title = title:d_aragon
			has_primary_title = title:d_barcelona
		}
		trigger_else = {
			has_primary_title = title:k_aragon
		}
		NOT = {	#You can only do it once.
			is_target_in_global_variable_list = {
				name = unavailable_unique_decisions
				target = flag:aragonese_expansion_decision
			}
		}
	}
	
	is_valid = {
		is_independent_ruler = yes
		realm_size >= 30
	}	
	
	is_valid_showing_failures_only = {
		is_available_adult = yes
		is_landed = yes
	}

	effect = {
			add_pressed_claim = title:k_sardinia
			add_pressed_claim = title:k_sicily
			add_pressed_claim = title:d_corsica
			add_pressed_claim = title:d_sardinia
			add_pressed_claim = title:d_sicily
			add_pressed_claim = title:d_calabria
			add_pressed_claim = title:d_apulia
			add_pressed_claim = title:d_salerno
			add_pressed_claim = title:d_benevento
			add_pressed_claim = title:d_capua
			#if = {
			#	limit = { title:k_aragon = { any_in_de_jure_hierarchy = { tier = tier_duchy } } }
			#	create_title_and_vassal_change = {
			#		type = created
			#		save_scope_as = title_change
			#		add_claim_on_loss = no
			#	}
			#	title:k_aragon = {
			#		change_title_holder = {
			#			holder = root
			#			change = scope:title_change
			#		}
			#	}
			#	resolve_title_and_vassal_change = scope:title_change
			#	set_primary_title_to = title:k_aragon
			#}
		add_prestige = 1000
		add_gold = 1000
		add_piety = 500
		hidden_effect = {
			set_character_faith_with_conversion = faith:catholic
	        if = {
	        	limit = { government_has_flag = government_is_clan }
	        	change_government = feudal_government
	        }
			add_to_global_variable_list = {
				name = unavailable_unique_decisions
				target = flag:aragonese_expansion_decision
			}
			spawn_army = {
				name = event_troop_default_name
				levies = 3000
				location = capital_province
				inheritable = yes
			}
		}
	}

	ai_potential = {
		always = yes
	}

	ai_will_do = {
		base = 100
		modifier = {
			factor = 0
			age > 35
		}
	}
}
