﻿is_hre_candidate_ruler = {
	top_liege = this
	is_landed = yes
	has_dynasty = yes
	highest_held_title_tier = tier_kingdom
	OR = {
		primary_title = title:k_east_francia
		primary_title = title:k_bavaria
		primary_title = title:k_italy
		primary_title = title:k_lotharingia
		primary_title = title:k_burgundy
		primary_title = {
			tier = tier_kingdom
			title_capital_county.title_province ?= {
				OR = {
					geographical_region = world_europe_west_germania
					geographical_region = custom_northern_italy
				}
			}
		}
	}
	trigger_if = {
		limit = {
			is_ai = yes
			has_game_rule = partial_encourage_hre_creation
		}
		prestige_level >= high_prestige_level
		ai_boldness >= high_positive_ai_value
	}
	OR = {
		ai_boldness >= high_positive_ai_value
		prestige_level >= high_prestige_level
		sub_realm_size >= 50
		has_trait = conqueror
	}
	faith = faith:catholic
	government_has_flag = government_is_feudal
	exists = faith.religious_head
	NOR = {
		has_game_rule = default_hre_creation
		exists = title:e_hre.holder
		exists = title:h_roman_empire.holder
		this = faith.religious_head
		has_trait = excommunicated
		is_at_war_with = faith.religious_head
		AND = {
			exists = title:e_byzantium.holder
			faith = title:e_byzantium.holder.faith
		}
	}
}

is_better_hre_county_target = {
	title_province = {
		OR = {
			AND = {
				geographical_region = world_europe_west_germania
				root.sub_realm_size < 140
			}
			AND = {
				geographical_region = custom_northern_italy
				OR = {
					root.sub_realm_size < 140
					NOT = { root = { has_title = title:k_italy } }
				}
			}
		}
	}
	duchy = { is_contested = no }
	is_neighbor_to_realm = root
	save_temporary_scope_as = possible_county
	OR = {
		duchy = {
			NOT = {
				any_in_de_jure_hierarchy = {
					tier = tier_county
					any_county_province = { has_holding_type = tribal_holding }
				}
			}
		}
		root = { gold > scope:possible_county.total_tribal_conversion_cost_value }
	}
}

is_hre_county_target = {
	title_province = {
		OR = {
			AND = {
				geographical_region = world_europe_west_germania
				root.sub_realm_size < 140
			}
			AND = {
				geographical_region = custom_northern_italy
				OR = {
					root.sub_realm_size < 140
					NOT = { root = { has_title = title:k_italy } }
				}
			}
			AND = {
				geographical_region = custom_burgundy
				root.sub_realm_size < 140
			}
		}
	}
	duchy = { is_contested = no }
	is_neighbor_to_realm = root
	save_temporary_scope_as = possible_county
	OR = {
		duchy = {
			NOT = {
				any_in_de_jure_hierarchy = {
					tier = tier_county
					any_county_province = { has_holding_type = tribal_holding }
				}
			}
		}
		root = { gold > scope:possible_county.total_tribal_conversion_cost_value }
	}
}

is_appropriate_hre_war_target = {
	is_ai = yes
	save_temporary_scope_as = target
	root.strength_with_relevant_allies_vs_target_value > scope:target.one_and_a_half_times_current_strength_with_relevant_allies_vs_root
	any_realm_county = { is_hre_county_target = yes }
	has_no_war_preventing_relation = yes
}

is_better_hre_war_target = {
	is_ai = yes
	save_temporary_scope_as = target
	root.strength_with_relevant_allies_vs_target_value > scope:target.one_and_a_half_times_current_strength_with_relevant_allies_vs_root
	any_realm_county = { is_better_hre_county_target = yes }
	has_no_war_preventing_relation = yes
}

is_hre_claim_war_target = {
	is_ai = yes
	OR = {
		primary_title = title:k_east_francia
		primary_title = title:k_bavaria
		primary_title = title:k_italy
		primary_title = title:k_lotharingia
		primary_title = title:k_burgundy
	}
	primary_title = {
		is_contested = no
		root = { has_claim_on = prev }
	}
	save_temporary_scope_as = target
	root.strength_with_relevant_allies_vs_target_value > scope:target.double_current_strength_with_relevant_allies_vs_root
	has_no_war_preventing_relation = yes
}

