﻿HRT_initialize_succession = {
	#Succession if HRE is formed
	if = {
		limit = {
			exists = title:e_hre.holder 
			title:c_aachen = {
				NOT = { has_variable = HRT_CF_apostate_active } #CF Compat
			}
		}
		title:e_hre.current_heir = {
			create_story = HRT_emperor_story
			if = {
				limit = {
					NOT = { has_character_flag = HRT_italienzug }
				}
				trigger_event = {
					id = HRT.1001
					days = { 7 14 }
				}
			}
		}
	}
	#Succession before HRE is formed
	else_if = {
		limit = {
			title:c_aachen = {
				NOT = { has_variable = HRT_CF_apostate_active } #CF Compat
			}
			any_HRT_candidati = {
				count > 0
			}
		}
		ordered_HRT_candidati = {
			order_by = HRT_imperator_score
			position = 0
			create_story = HRT_emperor_story
			if = {
				limit = {
					NOT = { has_character_flag = HRT_italienzug }
				}
				trigger_event = {
					id = HRT.1001
					days = { 7 14 }
				}
			}
			#Become Independent
			if = {
				limit = { is_independent_ruler = no }
				create_title_and_vassal_change = {
					type = independency
					save_scope_as = change
					add_claim_on_loss = yes
				}
				becomes_independent = {
					change = scope:change
				}
				resolve_title_and_vassal_change = scope:change
			}
			add_unpressed_claim = title:e_hre
		}
	}
	#if = { TODO Fallback in case no candidate exists
}

#GUI
HRT_update_window_effect = {
	if = {
		limit = {
			exists = title:e_hre.holder 
			title:c_aachen = {
				NOT = { has_variable = HRT_CF_apostate_active } #CF Compat
			}
		}
		set_variable = {
			name = HRT_formed_gui
			value = yes
		}

		title:e_hre = {
			# Imperator
			holder = { save_scope_as = imperator }
			# Electors
			if = {
				limit = {
					has_title_law = princely_elective_succession_law
					exists = scope:imperator
				}
				every_elector = {
					limit = {
						NOT = { this = scope:imperator }
					}
					root = {
						add_to_variable_list = {
							name = HRT_electors
							target = prev
						}
					}
				}
			}
			# Imperial Cities
			title:c_aachen = {
				if = {
					limit = { has_variable_list = HRT_reichsstaedte }
					root = {
						set_variable = {
							name = HRT_reichsstaedte_count
							value = 0
						}
					}
					every_in_list = {
						variable = HRT_reichsstaedte
						root = {
							add_to_variable_list = {
								name = HRT_imperial_cities
								target = prev
							}
							change_variable = {
								name = HRT_reichsstaedte_count
								add = 1
							}
						}
					}
				}
			}
		}
		# Candidati ordered by Vote score
		every_living_character = {
			if = {
				limit = {
					title:e_hre = {
						place_in_line_of_succession = {
							target = prev
							value == 1
						}
					}
				}
				save_scope_as = 0
			}
			else_if = {
				limit = {
					title:e_hre = {
						place_in_line_of_succession = {
							target = prev
							value == 2
						}
					}
				}
				save_scope_as = 1
			}
			else_if = {
				limit = {
					title:e_hre = {
						place_in_line_of_succession = {
							target = prev
							value == 3
						}
					}
				}
				save_scope_as = 2
			}
			else_if = {
				limit = {
					title:e_hre = {
						place_in_line_of_succession = {
							target = prev
							value == 4
						}
					}
				}
				save_scope_as = 3
			}
		}
	}

	else_if = {
		limit = {
			title:c_aachen = {
				NOT = { has_variable = HRT_CF_apostate_active } #CF Compat
			}
		}
		# Imperator
		title:c_aachen.var:HRT_emperor ?= {
			save_scope_as = imperator
		}
		# Imperator's Primary Heir
		scope:imperator ?= {
			primary_heir ?= {
				set_variable = {
					name = HRT_prince_var
					value = yes
				}
			}
		}
		if = {
			limit = {
				any_HRT_candidati = {
					count > 0
				}
			}
			# Expectatus (highest scoring Candidatus)
			ordered_HRT_candidati = {
				order_by = HRT_imperator_score
				position = 0
				save_scope_as = 0
			}
			# 3 highest scoring Candidati
			ordered_HRT_candidati = {
				order_by = HRT_imperator_score
				position = 1
				save_scope_as = 1
			}
			ordered_HRT_candidati = {
				order_by = HRT_imperator_score
				position = 2
				save_scope_as = 2
			}
			ordered_HRT_candidati = {
				order_by = HRT_imperator_score
				position = 3
				save_scope_as = 3
			}
		}
	}

	##Set gui variables
	#Imperator
	if = {
		limit = {
			scope:imperator ?= {
				is_alive = yes
			}
		}
		set_variable = {
			name = HRT_imperator_gui
			value = scope:imperator
		}
		set_variable = {
			name = HRT_authority_prestige_gui
			value = title:c_aachen.var:HRT_emperor.prestige
		}
		set_variable = {
			name = HRT_authority_dread_gui
			value = title:c_aachen.var:HRT_emperor.dread
		}
		set_variable = {
			name = HRT_authority_years_reigned_gui
			value = title:c_aachen.var:HRT_emperor.var:HRT_authority_years_reigned
		}
	}
	else = {
		set_variable = {
			name = HRT_missing_imperator_gui
			value = yes
		}
	}
	#Expectatus
	if = {
		limit = {
			exists = scope:0
		}
		set_variable = {
			name = HRT_expectatus_gui
			value = scope:0
		}
	}
	else = {
		set_variable = {
			name = HRT_missing_expectatus_gui
			value = yes
		}
	}
	#Candidati
	if = {
		limit = {
			exists = scope:1
			NOT = {
				scope:1 = scope:0
			}
		}
		add_to_variable_list = {
			name = HRT_candidati_gui
			target = scope:1
		}
	}
	else = {
		set_variable = {
			name = HRT_missing_1_gui
			value = yes
		}
	}
	if = {
		limit = {
			exists = scope:2
			NOR = {
				scope:2 = scope:0
				scope:2 = scope:1
			}
		}
		add_to_variable_list = {
			name = HRT_candidati_gui
			target = scope:2
		}
	}
	else = {
		set_variable = {
			name = HRT_missing_2_gui
			value = yes
		}
	}
	if = {
		limit = {
			exists = scope:3
			NOR = {
				scope:3 = scope:0
				scope:3 = scope:1
				scope:3 = scope:2
			}
		}
		add_to_variable_list = {
			name = HRT_candidati_gui
			target = scope:3
		}
	}
	else = {
		set_variable = {
			name = HRT_missing_3_gui
			value = yes
		}
	}

	#Display of Player Score
	if = {
		limit = {
			NOR = {
				exists = title:e_hre.holder
				this ?= scope:0
				this ?= scope:1
				this ?= scope:2
				this ?= scope:3
			}
		}
		set_variable = {
			name = HRT_show_player_score
			value = yes
		}
	}
	if = {
		limit = {
			has_variable = HRT_show_player_score
			NOT = { HRT_is_emperor_trigger = yes }
			HRT_is_eligible_candidatus = no
		}
		set_variable = {
			name = HRT_show_player_score_invalid
			value = yes
		}
	}

	# Set up player story cycles for on_death effect
	every_player = {
		limit = {
			NOT = {
				any_owned_story = {
					story_type = HRT_on_player_death_story
				}
			}
		}
		create_story = HRT_on_player_death_story
	}

	#GUI
	set_global_variable = {
		name = HRT_window_is_open
		value = yes
	}
}

#HRT window on state hide
HRT_clear_variables_effect = {
	if = {
		limit = {
			any_HRT_candidati = {
				count > 0
			}
		}
		every_HRT_candidati = { remove_variable = HRT_prince_var }
	}
	if = {
		limit = {
			exists = this
			is_alive = yes
		}
		remove_variable = HRT_imperator_gui
		remove_variable = HRT_expectatus_gui
		clear_variable_list = HRT_candidati_gui
		remove_variable = HRT_missing_1_gui
		remove_variable = HRT_missing_2_gui
		remove_variable = HRT_missing_3_gui
		remove_variable = HRT_missing_expectatus_gui
		remove_variable = HRT_missing_imperator_gui
		remove_variable = HRT_show_player_score
		remove_variable = HRT_show_player_score_invalid
		remove_variable = HRT_formed_gui
		remove_variable = HRT_reichsstaedte_count
		clear_variable_list = HRT_electors
		clear_variable_list = HRT_imperial_cities
	}
	#GUI
	remove_global_variable = HRT_window_is_open
	remove_global_variable = HRT_close_window_temp_var
}

#Close HRT Window if open
HRT_close_window = {
	if = {
		limit = { exists = global_var:HRT_window_is_open }
		set_global_variable = {
			name = HRT_close_window_temp_var
			value = yes
		}
		title:c_aachen.culture = {
			trigger_event = {
				id = HRT.9006
				days = 1
			}
		}
	}
}

### Italienzug ###
HRT_italienzug_completed_log_entry_effect = {
	scope:activity = {
		add_activity_log_entry = {
			key = HRT_italienzug_completed_log
			tags = { completed }
			# this line below adds the entry to the Effects section of the conclusion UI
			show_in_conclusion = yes
			character = root
			custom_tooltip = HRT_italienzug_result_gained_piety
			custom_tooltip = HRT_italienzug_result_gained_prestige
			custom_tooltip = HRT_italienzug_result_gained_dynasty_prestige
			show_as_tooltip = {
				every_attending_character = {
					limit = { NOT = { this = scope:host } }
					custom = EVERY_ACTIVITY_PARTICIPANT_EFFECT
					custom_tooltip = HRT_italienzug_result_gained_piety
					custom_tooltip = HRT_italienzug_result_gained_prestige
				}
			}
		}
	}
}

### Nibelungen ###

HRT_nibelungen_create_artifact_valkyr_helmet_effect = {
	# Get the character the artifact is being made for.
	$OWNER$ = { save_scope_as = owner }

	set_artifact_rarity_famed = yes

	# Create the artifact
	create_artifact = {	
		name = HRT_nibelungen_valkyr_helmet_name
		description = HRT_nibelungen_valkyr_helmet_description
		type = helmet
		visuals = HRT_nibelungen_valkyr_helmet
		wealth = scope:wealth
		quality = scope:quality
		template = HRT_nibelungen_valkyr_helmet_template
		modifier = HRT_nibelungen_valkyr_helmet_modifier
		save_scope_as = newly_created_artifact
		decaying = yes
	}

	scope:newly_created_artifact = {
		set_variable = { name = historical_unique_artifact value = yes }
		set_variable = {
			name = HRT_valkyr_helmet
			value = yes
		}
		save_scope_as = epic
	}
}

HRT_nibelungen_search_completed_log_entry_effect = {
	scope:activity = {
		add_activity_log_entry = {
			key = HRT_nibelungen_search_completed_log
			tags = { completed }
			# this line below adds the entry to the Effects section of the conclusion UI
			show_in_conclusion = yes
			character = root
		}
	}
}

### 2.2 Unity ###
HRT_unity_effect = {
	if = {
		limit = { exists = title:e_hre.holder }
		title:e_hre.holder = {
			# Clear all variables
			title:c_aachen = {
				if = {
					limit = { has_variable_list = HRT_core_cultures }
				}
				every_in_list = {
					variable = HRT_core_cultures
					change_variable ?= {
						name = HRT_core_culture_count
						multiply = 0
					}
					change_variable ?= {
						name = HRT_core_culture_count_weight
						multiply = 0
					}
				}
				clear_variable_list = HRT_core_cultures
			}

			# Count cultures and add to list
			culture = {
				if = {
					limit = {
						NOT = { has_variable = HRT_core_culture_count }
					}
					set_variable = {
						name = HRT_core_culture_count
						value = 0
					}
				}
				if = {
					limit = {
						NOT = { has_variable = HRT_core_culture_count_weight }
					}
					set_variable = {
						name = HRT_core_culture_count_weight
						value = 0
					}
				}
				change_variable = {
					name = HRT_core_culture_count
					add = 1
				}
				change_variable = {
					name = HRT_core_culture_count_weight
					add = 1
				}
				title:c_aachen = {
					if = {
						limit = {
							NOT = {
								is_target_in_variable_list = {
									name = HRT_core_cultures
									target = prev
								}
							}
						}
						add_to_variable_list = {
							name = HRT_core_cultures
							target = prev
						}
					}
				}
			}
			every_realm_county = {
				culture = {
					if = {
						limit = {
							culture_head ?= {
								OR = {
									this ?= title:e_hre.holder
									is_vassal_or_below_of = title:e_hre.holder
								}
							}
						}
						if = {
							limit = {
								NOT = { has_variable = HRT_core_culture_count }
							}
							set_variable = {
								name = HRT_core_culture_count
								value = 0
							}
						}
						if = {
							limit = {
								NOT = { has_variable = HRT_core_culture_count_weight }
							}
							set_variable = {
								name = HRT_core_culture_count_weight
								value = 0
							}
						}
						change_variable = {
							name = HRT_core_culture_count
							add = 1
						}
						change_variable = {
							name = HRT_core_culture_count_weight
							add = 1
						}
						title:c_aachen = {
							if = {
								limit = {
									NOT = {
										is_target_in_variable_list = {
											name = HRT_core_cultures
											target = prev
										}
									}
								}
								add_to_variable_list = {
									name = HRT_core_cultures
									target = prev
								}
							}
						}
					}
				}
			}

			# Modify values
			title:c_aachen = {
				#Add 50% bonus to historical tribes
				every_in_list = {
					variable = HRT_core_cultures
					limit = {
						OR = {
							this = culture:franconian
							any_parent_culture_or_above = {
								this = culture:franconian
							}
							this = culture:saxon
							any_parent_culture_or_above = {
								this = culture:saxon
							}
							this = culture:swabian
							any_parent_culture_or_above = {
								this = culture:swabian
							}
							this = culture:bavarian
							any_parent_culture_or_above = {
								this = culture:bavarian
							}
							this = title:e_hre.holder.culture
						}
					}
					change_variable = {
						name = HRT_core_culture_count
						add = HRT_core_culture_value_half
					}
				}
				#Give malus to non-germans
				every_in_list = {
					variable = HRT_core_cultures
					limit = {
						NOR = {
							has_cultural_pillar = heritage_central_germanic
							has_cultural_pillar = heritage_latin
							has_cultural_pillar = heritage_frankish
							this = title:e_hre.holder.culture
						}
					}
					change_variable = {
						name = HRT_core_culture_count
						subtract = HRT_core_culture_value_half
					}
				}
				#Finally, make sure the Kaiser's culture is always at first place
				every_in_list = {
					variable = HRT_core_cultures
					limit = {
						this = title:e_hre.holder.culture
					}
					change_variable = {
						name = HRT_core_culture_count
						add = 8192 # 8-)
					}
				}
			
				# Grab cultures
				ordered_in_list = {
					variable = HRT_core_cultures
					order_by = { value = var:HRT_core_culture_count }
					position = 0
					save_scope_as = HRT_core_culture_1
				}
				ordered_in_list = {
					variable = HRT_core_cultures
					order_by = { value = var:HRT_core_culture_count }
					position = 1
					save_scope_as = HRT_core_culture_2
				}
				ordered_in_list = {
					variable = HRT_core_cultures
					order_by = { value = var:HRT_core_culture_count }
					position = 2
					save_scope_as = HRT_core_culture_3
				}
				ordered_in_list = {
					variable = HRT_core_cultures
					order_by = { value = var:HRT_core_culture_count }
					position = 3
					save_scope_as = HRT_core_culture_4
				}
				ordered_in_list = {
					variable = HRT_core_cultures
					order_by = { value = var:HRT_core_culture_count }
					position = 4
					save_scope_as = HRT_core_culture_5
				}
			}

			if = {
				limit = {
					exists = scope:HRT_core_culture_1
				}
				set_variable = {
					name = HRT_core_culture_1
					value = scope:HRT_core_culture_1
				}
			}
			if = {
				limit = {
					exists = scope:HRT_core_culture_2
					NOT = {
						scope:HRT_core_culture_2 = scope:HRT_core_culture_1
					}
				}
				set_variable = {
					name = HRT_core_culture_2
					value = scope:HRT_core_culture_2
				}
			}
			if = {
				limit = {
					exists = scope:HRT_core_culture_3
					NOR = {
						scope:HRT_core_culture_3 = scope:HRT_core_culture_1
						scope:HRT_core_culture_3 = scope:HRT_core_culture_2
					}
				}
				set_variable = {
					name = HRT_core_culture_3
					value = scope:HRT_core_culture_3
				}
			}
			if = {
				limit = {
					exists = scope:HRT_core_culture_4
					NOR = {
						scope:HRT_core_culture_4 = scope:HRT_core_culture_1
						scope:HRT_core_culture_4 = scope:HRT_core_culture_2
						scope:HRT_core_culture_4 = scope:HRT_core_culture_3
					}
				}
				set_variable = {
					name = HRT_core_culture_4
					value = scope:HRT_core_culture_4
				}
			}
			if = {
				limit = {
					exists = scope:HRT_core_culture_5
					NOR = {
						scope:HRT_core_culture_5 = scope:HRT_core_culture_1
						scope:HRT_core_culture_5 = scope:HRT_core_culture_2
						scope:HRT_core_culture_5 = scope:HRT_core_culture_3
						scope:HRT_core_culture_5 = scope:HRT_core_culture_4
					}
				}
				set_variable = {
					name = HRT_core_culture_5
					value = scope:HRT_core_culture_5
				}
			}
			# Get final variable on emperor
			set_variable = {
				name = HRT_authority_unity
				value = HRT_total_unity_value
			}
		}
	}
}

HRT_disable_effect = {
	# Hide GUI
	HRT_close_window = yes
	set_global_variable = {
		name = HRT_disabled
		value = yes
	}
	# End Story
	random_owned_story = {
		limit = { story_type = HRT_emperor_story }
		end_story = yes
	}
	# Remove Reichsstadt Modifiers
	title:c_aachen = {
		# 2.5 Reichsstadt
		if = {
			limit = { has_variable_list = HRT_reichsstaedte }
			every_in_list = {
				variable = HRT_reichsstaedte
				remove_variable = HRT_reichsstadt_years
				remove_county_modifier ?= HRT_reichsstadt_1
				remove_county_modifier ?= HRT_reichsstadt_2
				remove_county_modifier ?= HRT_reichsstadt_3
				holder ?= {
					if = {
						limit = {
							has_government = free_imperial_city_government
						}
						change_government = republic_government
					}
				}
				prev = {
					remove_list_variable = {
						name = HRT_reichsstaedte
						target = prev
					}
				}
			}
		}
		# 3.1 centralization
		set_variable = {
			name = HRT_centralization
			value = { 5 20 }
		}
	}
}

# 3.1 Centralization
HRT_assign_centralization_modifier_effect = {
	remove_character_modifier ?= HRT_centralization_0_mod
	remove_character_modifier ?= HRT_centralization_1_mod
	remove_character_modifier ?= HRT_centralization_2_mod
	remove_character_modifier ?= HRT_centralization_3_mod
	remove_character_modifier ?= HRT_centralization_4_mod
	if = {
		limit = { HRT_has_centralization_0_trigger = yes }
		add_character_modifier = HRT_centralization_0_mod
	}
	else_if = {
		limit = { HRT_has_centralization_1_trigger = yes }
		add_character_modifier = HRT_centralization_1_mod
	}
	else_if = {
		limit = { HRT_has_centralization_2_trigger = yes }
		add_character_modifier = HRT_centralization_2_mod
	}
	else_if = {
		limit = { HRT_has_centralization_3_trigger = yes }
		add_character_modifier = HRT_centralization_3_mod
	}
	else_if = {
		limit = { HRT_has_centralization_4_trigger = yes }
		add_character_modifier = HRT_centralization_4_mod
	}
}