﻿restore_holy_roman_empire_decision = {
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_dynasty_house.dds"
	}
	decision_group_type = major

	ai_check_interval_by_tier = {
		barony = 0
		county = 0
		duchy = 0
		kingdom = 8
		empire = 8
		hegemony = 0
	}

	desc = restore_holy_roman_empire_decision_desc
	selection_tooltip = restore_holy_roman_empire_decision_tooltip

	is_shown = {
		is_landed_or_landless_administrative = yes
		exists = dynasty # Everyone should have a dynasty, but in case they don't...
		NOT = { has_global_variable = flag_restored_roman_empire } # Not if Rome already exists (prevent Rome from restoring HRE)
		
		# Title existence conditions
		NOR = {
			exists = title:e_hre.holder	#Well, that'd be redundant.
			exists = title:e_france.holder	#And that'd just be weird.
			highest_held_title_tier = tier_empire	#Existing emperors likewise should not be giving up their empire.
		}
		
		# Faith conditions
		exists = faith.religious_head
		this != faith.religious_head	#Nice try, Mr. Pope.
		faith = {
			religion_tag = christianity_religion
			has_doctrine_parameter = spiritual_head_of_faith	#Gotta be invested with the authority by someone other than yourself.
		}
		NAND = {	#No need for a competing empire if your faith controls the ERE.
			exists = title:e_byzantium.holder
			faith = title:e_byzantium.holder.faith
		}
		
		# Government conditions
		OR = {	#Government conditions.
			government_has_flag = government_is_feudal
			government_has_flag = government_is_clan
			government_allows = administrative
		}
	}

	is_valid = {
		#Title ownership conditions.
		OR = {
			has_title = title:k_east_francia
			has_title = title:k_bavaria
			has_title = title:k_france
			has_title = title:k_lotharingia
			has_title = title:k_italy
			any_held_title = {
				title_tier = kingdom
				is_titular = no
				title_capital_county.title_province = {
					OR = {
						geographical_region = world_europe_west_germania
						geographical_region = custom_northern_italy
					}
				}
			}
		}
	}

	is_valid_showing_failures_only = {
		is_at_war = no
		is_available_adult = yes
		
		# also putting these here so a player sees them
		top_liege = this
		sub_realm_size >= 120 #Plus your various kingdoms should be ruling something.
		trigger_if = {
			limit = { is_ai = no }
			prestige_level >= high_prestige_level
		}
		trigger_if = {
			limit = { is_ai = no }
			custom_description = {	#And have some additional royal dignity for good measure.
				text = decision_refound_hre_own_three_kingdoms.tt
				any_held_title = {
					title_tier = kingdom
					count >= 3
				}
			}
		}
		
		# putting this here so a player knows they need to have the Kingdom of Italy title (if it isn't titular)
		trigger_if = {
			limit = {
				is_ai = no
				title:k_italy = { is_titular = no }
			}
			has_title = title:k_italy
		}
		
		# for the AI, they won't know they need to have the title - so as long as they *could* create the title, they're good
		trigger_else_if = {
			limit = {
				title:k_italy = { is_titular = no }
			}
			OR = {
				has_title = title:k_italy
				title:k_italy = {
					any_de_jure_county = {
						percent > 0.50
						county_held_or_vassal_to_target_trigger = { TARGET = root }
					}
				}
			}
		}
		trigger_else = {
			always = yes
		}
		faith.religious_head = { is_available_adult = yes }
		NOR = {
			has_trait = excommunicated
			is_at_war_with = faith.religious_head
		}
		
		#HoF preference conditions - moved this here so a player can see what they need in order to do this
		trigger_if = {
			limit = { is_ai = no }
			custom_description = {
				text = has_friendly_pope
				subject = root.faith.religious_head
				OR = {
					faith.religious_head = {	#And they have to actually like you enough to want to invest you.
						opinion = {
							target = root
							value >= high_positive_opinion
						}
					}
					root = { has_strong_usable_hook = faith.religious_head }	#Or you have a strong hook on them that you can actually use.
					root = { has_weak_hook = faith.religious_head }	#Or a regular hook, to be fair, provided you meet the other criteria.
				}
			}
		}
	}

	effect = {
		show_as_tooltip = {
			restore_holy_roman_empire_decision_scripted_effect = yes
		}
		gain_heroic_legend_seed_tooltip_effect = yes
		save_scope_as = founder
		faith.religious_head = { save_scope_as = founder_hof }
		if = {
			limit = { has_realm_law = confederate_partition_succession_law }
			add_realm_law_skip_effects = partition_succession_law
		}
		if = {
			limit = {
				faith.religious_head = { is_playable_character = yes }
			}
			faith.religious_head.capital_province = { save_scope_as = ceremony_locale }
		}
		else = {
			capital_province = { save_scope_as = ceremony_locale }
		}
		if = {
			limit = {
				faith = faith:catholic
				title:k_romagna.holder = faith.religious_head
			}
			create_title_and_vassal_change = {
				type = granted
				save_scope_as = change
				add_claim_on_loss = no
			}
			every_held_title = {
				limit = {
					this.kingdom = title:k_romagna
					NOT = { this = scope:founder.capital_county }
				}
				change_title_holder_include_vassals = {
					holder = scope:founder.faith.religious_head
					change = scope:change
				}
			}
			every_vassal = {
				limit = { this.primary_title.kingdom = title:k_romagna }
				change_liege = {
					liege = scope:founder.faith.religious_head
					change = scope:change
				}
			}
			resolve_title_and_vassal_change = scope:change
		}
		trigger_event = {
			id = middle_europe_decisions.0015
		}
		every_held_title = {
			limit = { tier = tier_kingdom }
			root = { destroy_title = prev }
		}
		#Notify other players.
		every_player = {
			limit = {
				this != root
				OR = {
					religion = root.religion
					in_diplomatic_range = root # changed this because is_within_diplo_range doesn't work properly
				}
			}
			trigger_event = middle_europe_decisions.0016
		}
		if = {
			limit = {
				is_ai = no
				NOT = { exists = global_var:restore_holy_roman_empire_decision  }
			}
			set_global_variable = {
				name = restore_holy_roman_empire_decision
				value = root
			}
		}
		# Assign Dynamic Prince-Electors
		hidden_effect = {
			if = {
				limit = { has_global_variable_list = hre_elector_list }
				clear_global_variable_list = hre_elector_list
			}
			while = {
				count = 7
				random_vassal = {
					limit = {
						capital_province.empire = title:e_hre # must be de-jure vassal of empire
						OR = { # prince-bishopric or duchy
							AND = {
								government_has_flag = government_is_theocracy
								primary_title.tier >= tier_county
							}
							primary_title.tier >= tier_duchy
						}
						NOT = { # not already selected
							is_target_in_global_variable_list = {
								name = hre_elector_list
								target = primary_title
							}
						}
					}
					weight = {
						modifier = { # major vassals heavily weighted
							factor = 10
							is_powerful_vassal = yes
						}
						modifier = { # same culture as emperor preferred
							factor = 4
							culture = root.culture
						}
						modifier = { # we don't mind going outside Germania, but prefer it
							factor = 1.5
							trigger = { exists = primary_title.title_province }
							primary_title.title_province.barony = {
								OR = {
									target_is_de_jure_liege_or_above = title:k_bavaria
									target_is_de_jure_liege_or_above = title:k_bohemia
									target_is_de_jure_liege_or_above = title:k_east_francia
									target_is_de_jure_liege_or_above = title:k_frisia
									target_is_de_jure_liege_or_above = title:k_lotharingia
									target_is_de_jure_liege_or_above = title:k_pomerania
								}
							}
						}
						modifier = { # we want at least 3 theocracies ideally
							factor = 10
							any_in_global_list = {
								variable = hre_elector_list
								count < 3
								holder = { government_has_flag = government_is_theocracy }
							}
							government_has_flag = government_is_theocracy
						}
						modifier = { # 3 is enough theocracies unless we can't find feudal
							factor = 0.1
							any_in_global_list = {
								variable = hre_elector_list
								count >= 3
								holder = { government_has_flag = government_is_theocracy }
							}
							government_has_flag = government_is_theocracy
						}
					}
					add_to_global_variable_list = {
						name = hre_elector_list
						target = primary_title
					}
				}
			}
			hre_elector_list_save_effect = yes # saves list scopes to title as variables for tooltip
		}
	}

	cost = {
		gold = {
			value = 0
			if = {
				limit = { has_treasury = no }
				if = {
					limit = { is_ai = no }
					add = 500
				}
				else = {
					add = 200
				}
			}
		}
		treasury = {
			value = 0
			if = {
				limit = { has_treasury = yes }
				if = {
					limit = { is_ai = no }
					add = 500
				}
				else = {
					add = 200
				}
			}
		}
		piety = 200
	}

	ai_potential = {
		top_liege = this
		highest_held_title_tier >= tier_kingdom
	}

	ai_will_do = {
		base = 100

		modifier = { # We don't want a solo-France to form the HRE
			factor = 0
			NOR = {
				has_title = title:k_east_francia
				has_title = title:k_bavaria
				has_title = title:k_lotharingia
				has_title = title:k_italy
			}
		}
	}
}

