﻿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 = 12
		empire = 12
		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)
		NOR = {	#Title existence cnditions.
			exists = title:e_hre.holder	#Well, that'd be redundant.
			#highest_held_title_tier = tier_empire	#Existing emperors likewise should not be giving up their empire.
		}
		NOR = { #It doesn't make sense for these competing empires.
			is_roman_emperor_trigger = yes
			mpo_has_gok_mongol_empire_trigger = yes
		}
		AND = {	#Faith conditions.
			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
			}
		}
		OR = {	#Government conditions.
			government_has_flag = government_is_feudal
			government_has_flag = government_is_clan
			government_allows = administrative
		}
		exists = faith.religious_head
	}

	is_valid = {
		#Standard requirements.
		top_liege = this
		trigger_if = {
			limit = {
				is_ai = no
			}
			prestige_level >= high_prestige_level
		}
		#Title ownership conditions.
		OR = { #Gotta be King of a core Carolingian Kingdom.
			has_title = title:k_germany
			has_title = title:k_austria
			has_title = title:k_france
			has_title = title:k_italy
			has_title = title:k_switzerland
			any_vassal = {
				OR = {
					has_title = title:k_germany
					has_title = title:k_austria
					has_title = title:k_france
					has_title = title:k_italy
					has_title = title:k_switzerland
				}
			}
		}
		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
				}
			}
		}
		sub_realm_size >= 120	#Plus your various kingdoms should be ruling something.
		#HoF preference conditions.
		trigger_if = {
			limit = {
				is_ai = no
			}
			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.
			}
		}
	}

	is_valid_showing_failures_only = {
		is_available_adult = yes
		faith.religious_head = { is_available_adult = yes }
		NOR = {
			has_trait = excommunicated
			is_at_war_with = faith.religious_head
		}
	}

	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 = {
				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 }
		}
		trigger_event = {
			id = middle_europe_decisions.0015
		}
		#Notify other players.
		every_player = {
			limit = {
				this != root
				is_within_diplo_range = { CHARACTER = root }
			}
			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_austria
									target_is_de_jure_liege_or_above = title:k_bohemia
									target_is_de_jure_liege_or_above = title:k_germany
									target_is_de_jure_liege_or_above = title:k_frisia
									target_is_de_jure_liege_or_above = title:k_switzerland
									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
					is_ai = no
				}
				add = 500
			}
		}
		treasury = {
			value = 0
			if = {
				limit = {
					has_treasury = yes
					is_ai = no
				}
				add = 500
			}
		}
		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_germany
				has_title = title:k_austria
				has_title = title:k_italy
				has_title = title:k_switzerland
				any_vassal = {
					OR = {
						has_title = title:k_germany
						has_title = title:k_austria
						has_title = title:k_italy
						has_title = title:k_switzerland
					}
				}
			}
		}
	}
}