﻿##### scripted effects
# periodically triggered via story
# CB is chosen via events

#culture:x = { has_same_culture_heritage = culture:y }

### disease resistance
disease_immunity_short_effect = {
	add_character_flag = {
		flag = immune_to_disease
		years = 25
	}
}
disease_immunity_long_effect = {
	add_character_flag = {
		flag = immune_to_disease
		years = 50
	}
}

### invader modifiers
apply_invader_modifiers_effect = {
	add_character_flag = is_first_invader_flag # CB is free for that character; only relevant for players
	disease_immunity_long_effect = yes
	add_character_modifier = first_$INVADER_DYNN$_modifier # unique buffs for first invader
	add_character_modifier = historicinvasions_plot_armor # generic buffs for every invader
	add_character_flag = recent_nation_fracturing_faction_war # makes the AI unlikely to start dissolution factions
	add_character_flag = recent_independence_faction_war # makes the AI unlikely to start independence factions
	if = { # make invader immune to viking incursions
		limit = { # trigger of fp1_scandinavian_adventurers.0001
			has_fp1_dlc_trigger = yes
			NOT = { has_game_rule = fp1_scandi_adventurers_off }
			OR = {
				culture:norse = {
					NOT = { has_cultural_era_or_later = culture_era_early_medieval }
				}
				culture:norwegian = {
					NOT = { has_cultural_era_or_later = culture_era_early_medieval }
				}
				culture:danish = {
					NOT = { has_cultural_era_or_later = culture_era_early_medieval }
				}
				culture:swedish = {
					NOT = { has_cultural_era_or_later = culture_era_early_medieval }
				}
			}
		}
		add_character_modifier = fp1_reprieve_from_sa_modifier
	}
	if = {
		limit = { has_character_modifier = first_borjigin_modifier }
		add_character_flag = is_temujin # immunity to all factions
	}
	# transfer house head
	if = {
		limit = {
			scope:story.story_owner = {
				is_house_head = no
				house.house_head = {
					OR = {
						is_ruler = no
						highest_held_title_tier = tier_barony
					}
					is_ai = yes
				}
			}
		}
		scope:story.story_owner.house = { set_house_head = scope:story.story_owner }
	}
	# create character memory
	create_character_memory = {
		type = historicinvasions_became_invader_memory
		participants = {
		}
	}
}

### hand out counties at start of story
give_invader_land_effect = {
	# find a starting location that doesn't screw players over
	if = {
		# are there any AI-held counties within the region?
		limit = {
			any_county_in_region = {
				region = special_$INVADER_DYNN$_conquest_region_start
				count > 0
				holder = { is_ai = yes }
			}
		}
		# pick out a county as starting point and scope upwards to its duchy
		random_county_in_region = { # bigger duchies are more likely to be picked
			# try to only pick duchies with only AI-held counties
			limit = {
				$STARTING_TIER$ = { # usually a duchy; can be any other tier
					any_in_de_jure_hierarchy = {
						tier = tier_county
						count = all
						holder = { is_ai = yes }
					}
				}
			}
			# alternatively, pick any AI-held county
			alternative_limit = { holder = { is_ai = yes } }
			region = special_$INVADER_DYNN$_conquest_region_start
			$STARTING_TIER$ = { save_scope_as = starting_landed_titles } # usually a duchy; can be any other tier
		}
		# then scope downwards to every de jure AI-held county
		scope:starting_landed_titles = {
			every_in_de_jure_hierarchy = {
				limit = {
					tier = tier_county
					holder = { is_ai = yes }
				}
				add_to_list = starting_titles_list
			}
		}
	}
	else = { # fallback; should theoretically never trigger due to check at story start event
		#debug_log = "story used fallback land grant effect"
		#debug_log_date = yes
		#debug_log_scopes = yes
		
		random_county_in_region = {
			region = special_$INVADER_DYNN$_conquest_region_start
			add_to_list = starting_titles_list
		}
	}

	# hand out titles
	create_title_and_vassal_change = {
		type = conquest
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	every_in_list = {
		list = starting_titles_list
		# transfer titles
		change_title_holder = {
			holder = $INVADER_CHAR$
			change = scope:title_change
		}

		#############################
		#####  CULTURE & FAITH  #####
		#############################

		### change nothing
		if = {
			limit = {
				$INVADER_CHAR$ = {
					OR = {
						has_character_modifier = first_rurikid_modifier
					}
				}
			}
		}
		### change faith & culture
		else_if = {
			limit = {
				$INVADER_CHAR$ = {
					OR = {
						has_character_modifier = first_yelu_modifier
						has_character_modifier = first_ottoman_modifier
						has_character_modifier = first_gediminid_modifier
						has_character_modifier = first_song_modifier
						has_character_modifier = first_borjigin_modifier
					}
				}
			}
			set_county_faith = $INVADER_CHAR$.faith
			if = { # do not replace the county culture if the local culture is a hybrid of said culture
				limit = {
					culture = { NOT = { any_parent_culture_or_above = { this = $INVADER_CHAR$.culture } } }
				}
				set_county_culture = $INVADER_CHAR$.culture
			}
		}
		### only sometimes change faith, don't change culture
		else_if = {
			limit = {
				$INVADER_CHAR$ = {
					OR = {
						has_character_modifier = first_keita_modifier
					}
				}
			}
			if = {
				limit = { NOT = { religion = religion:islam_religion } }
				set_county_faith = $INVADER_CHAR$.faith
			}
		}
		### only affect capital county
		else_if = {
			limit = {
				$INVADER_CHAR$ = {
					OR = {
						has_character_modifier = first_anushtiginid_modifier
						has_character_modifier = first_eldiguzid_modifier
						has_character_modifier = first_qara_qoyunlu_modifier
						has_character_modifier = first_aq_qoyunlu_modifier
					}
				}
			}
			duchy = {
				title_capital_county = {
					set_county_faith = $INVADER_CHAR$.faith
					if = { # do not replace the county culture if the local culture is a hybrid of said culture
						limit = {
							culture = { NOT = { any_parent_culture_or_above = { this = $INVADER_CHAR$.culture } } }
						}
						set_county_culture = $INVADER_CHAR$.culture
					}
				}
			}
		}
		### default: always change faith, don't change culture
		else = { set_county_faith = $INVADER_CHAR$.faith }
	}
	# change culture in just one random county
	if = {
		limit = { $INVADER_CHAR$ = { has_character_modifier = first_al-qays_modifier } }
		random_in_list = {
			list = starting_titles_list
			set_county_culture = culture:east_bantu
		}
	}
	scope:starting_landed_titles = { # different from the list
		change_title_holder = {
			holder = $INVADER_CHAR$
			change = scope:title_change
		}
	}
	resolve_title_and_vassal_change = scope:title_change

	# raise control
	every_in_list = {
		list = starting_titles_list
		change_county_control = 100
	}
	every_realm_province = {
		refill_levy = yes
		refill_garrison = yes
	}

	# government
	# if = {
	# 	limit = { $INVADER_CHAR$.capital_county.title_province = { has_holding_type = tribal_holding } }
	# 	$INVADER_CHAR$.capital_county.title_province = { set_holding_type = castle_holding }
	# }
}

### spawn invader court
#todo FOR/WHILE loops
spawn_invader_servants_effect = {
	# spawn warriors
	create_character = {
		template = new_warrior_character
		gender_female_chance = historicinvasions_soldier_female_chance
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
	}
	create_character = {
		template = new_warrior_character
		gender_female_chance = historicinvasions_soldier_female_chance
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
	}
	create_character = {
		template = new_warrior_character
		gender_female_chance = historicinvasions_soldier_female_chance
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
	}
	create_character = {
		template = new_warrior_character
		gender_female_chance = historicinvasions_soldier_female_chance
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
	}
	create_character = {
		template = new_warrior_character
		gender_female_chance = historicinvasions_soldier_female_chance
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
	}
	create_character = {
		template = new_warrior_character
		gender_female_chance = historicinvasions_soldier_female_chance
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
	}
	create_character = {
		template = new_warrior_character
		gender_female_chance = historicinvasions_soldier_female_chance
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
	}
	create_character = {
		template = new_warrior_character
		gender_female_chance = historicinvasions_soldier_female_chance
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
	}

	# spawn commanders
	create_character = {
		template = new_commander_character
		gender_female_chance = historicinvasions_soldier_female_chance
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
	}
	create_character = {
		template = new_commander_character
		gender_female_chance = historicinvasions_soldier_female_chance
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
	}

	# spawn siege engineers
	create_character = {
		template = new_siege_engineer
		gender_female_chance = historicinvasions_soldier_female_chance
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
	}
	create_character = {
		template = new_siege_engineer
		gender_female_chance = historicinvasions_soldier_female_chance
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
	}
	create_character = {
		template = new_siege_engineer
		gender_female_chance = historicinvasions_soldier_female_chance
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
	}
	create_character = {
		template = new_siege_engineer
		gender_female_chance = historicinvasions_soldier_female_chance
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
	}

	# physician
	create_character = {
		template = hi_new_physician_template
		gender_female_chance = historicinvasions_soldier_female_chance
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
	}
}

### hand out the title that serves as an anchor for the invasion story
form_the_invader_title_effect = {
	# give out title
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	# fatimid
	if = {
		limit = { $INVADER_CHAR$ = { has_character_modifier = first_fatimid_modifier } }
		title:d_shiite = {
			change_title_holder = {
				holder = $INVADER_CHAR$
				change = scope:title_change
			}
		}
	}
	# almohad: d_masmudi is handed out in form_the_almohad_title_effect = { ... } due to potential coup mechanic
	$INVADER_TITLE$ = {
		change_title_holder = {
			holder = $INVADER_CHAR$
			change = scope:title_change
		}
		set_landless_title = $LANDLESS_YES_NO$
	}
	becomes_independent = { change = scope:title_change }
	resolve_title_and_vassal_change = scope:title_change

	set_primary_title_to = $INVADER_TITLE$

	# dread
	add_dread = high_dread

	# government
	apply_invader_government_type_effect = { INVADER_CHAR = $INVADER_CHAR$ }

	#############################
	##### TITLE DESTRUCTION #####
	#############################

	# guaranteed title destruction on succession
	### DEPRECIATED, used for localization purposes only
	### has been moved into landed title itself! 'destroy_on_succession = yes'
	if = {
		limit = {
			$INVADER_CHAR$ = {
				OR = {
					has_character_modifier = first_ziyarid_modifier
					has_character_modifier = first_al-qays_modifier

					has_character_modifier = first_chola_modifier # set at end of story
					has_character_modifier = first_knytling_modifier
					has_character_modifier = first_yngling_modifier
					has_character_modifier = first_qarghuyid_modifier
					has_character_modifier = first_vivar_modifier
					has_character_modifier = first_bearn_modifier
					has_character_modifier = first_qara_qoyunlu_modifier
					has_character_modifier = first_aq_qoyunlu_modifier

					has_character_modifier = first_attila_modifier
					has_character_modifier = first_argead_modifier
					has_character_modifier = first_ahom_modifier
				}
			}
		}
		$INVADER_TITLE$ = { set_variable = set_destroy_on_succession_var }
	}

	# conditional title destruction on inheritance
	# title is destroyed on succession if the heir alredy holds a different KINGDOM; "title replacement"
	if = {
		limit = {
			$INVADER_CHAR$ = {
				OR = {
					has_character_modifier = first_ziyarid_modifier
					has_character_modifier = first_al-qays_modifier

					has_character_modifier = first_hauteville_modifier
					has_character_modifier = first_jannabid_modifier
					has_character_modifier = first_normandie_modifier
					#has_character_modifier = first_piast_modifier # set at end of story
					#has_character_modifier = first_eldiguzid_modifier # set at end of story
					#has_character_modifier = first_zunbil_modifier # set at end of story
				}
			}
		}
		$INVADER_TITLE$ = { set_variable = set_destroy_on_gain_same_tier_var }
	}

	# spawn troops; delayed to prevent error.log entry
	# landless invaders: some troops are spawned within the CB itself, not here. this way, they can vanish after the war is finished.
	# see: historicinvasions_spawn_unlanded_army_effect = { ... }
	if = {
		limit = {
			$INVADER_CHAR$ = {
				NOR = {
					has_character_modifier = first_vlaanderen_modifier
					has_character_modifier = first_qarghuyid_modifier
					has_character_modifier = first_vivar_modifier
					has_character_modifier = first_bearn_modifier
				}
			}
		}
		trigger_event = {
			id = $INVADER_DYNN$_invasion.0100
			days = 1
		}
	}

	add_legitimacy_effect = { LEGITIMACY = 1000 } # only works when landed
}

historicinvasions_story_setup_effect = {
	add_to_global_variable_list = {
		name = historicinvasions_story_started
		target = flag:$INVADER_DYNN$_story_started
	}
	set_variable = {
		name = invader_title_var
		value = $INVADER_TITLE$
	}
	var:invader_title_var = {
		apply_historicinvasions_dissolution_protection_effect = yes
	}
	set_variable = {
		name = story_owner_counter
		value = 1
	}
	set_variable = {
		name = lost_invasions_counter
		value = 0
	}
}

### Variable "struggle_block_dissolution_faction"
# saved value:
# 0 = indefinite modifier
# 1 = timed modifier

# scope is a title
apply_historicinvasions_dissolution_protection_effect = {
	# prevent invader realms from being the target of dissolution factions at the beginning of an invasion
	if = {
		limit = {
			NOT = { has_game_rule = historicinvasions_dissolution_protection_off }
			NOT = { exists = var:struggle_block_dissolution_faction }
		}
		# indefinite duration
		set_variable = {
			name = struggle_block_dissolution_faction
			value = 0
		}
	}
}

# scope is a title
remove_historicinvasions_dissolution_protection_effect = {
	if = {
		limit = {
			exists = var:struggle_block_dissolution_faction
		}
		remove_variable = struggle_block_dissolution_faction
	}
}

# scope is a title
apply_temporary_historicinvasions_dissolution_protection_effect = {
	remove_historicinvasions_dissolution_protection_effect = yes
	# prevent invader realms from being the target of dissolution factions after the end of an invasion
	if = {
		limit = {
			NOR = {
				has_game_rule = historicinvasions_dissolution_protection_off
				has_game_rule = historicinvasions_dissolution_protection_0
			}
			NOT = { has_variable = struggle_block_dissolution_faction }
		}
		switch = {
			trigger = has_game_rule
			historicinvasions_dissolution_protection_0 = {} # do nothing
			historicinvasions_dissolution_protection_25 = { set_variable = { name = struggle_block_dissolution_faction years = 25 value = 1 } }
			historicinvasions_dissolution_protection_50 = { set_variable = { name = struggle_block_dissolution_faction years = 50 value = 1 } }
			historicinvasions_dissolution_protection_75 = { set_variable = { name = struggle_block_dissolution_faction years = 75 value = 1 } }
			historicinvasions_dissolution_protection_100 = { set_variable = { name = struggle_block_dissolution_faction years = 100 value = 1 } }
			historicinvasions_dissolution_protection_125 = { set_variable = { name = struggle_block_dissolution_faction years = 125 value = 1 } }
			fallback = {} # do nothing
		}
	}
}

hi_story_switches_to_local_title_effect = {
	scope:story = {
		# grant local title
		if = { # usurp
			limit = {
				title:$LOCAL_TITLE$ = { is_title_created = yes }
			}
			create_title_and_vassal_change = {
				type = usurped
				save_scope_as = title_change
				add_claim_on_loss = no
			}
		}
		else = { # create
			create_title_and_vassal_change = {
				type = created
				save_scope_as = title_change
				add_claim_on_loss = no
			}
		}
		title:$LOCAL_TITLE$ = {
			if = {
				limit = { NOT = { exists = scope:replacement_title_holder } }
				change_title_holder = {
					holder = scope:story.story_owner
					change = scope:title_change
				}
			}
			else = {
				change_title_holder = {
					holder = scope:replacement_title_holder
					change = scope:title_change
				}
			}
		}
		resolve_title_and_vassal_change = scope:title_change
		story_owner = { set_primary_title_to = title:$LOCAL_TITLE$ }

		### transfer dissolition protection
		# permanent (mid-story)
		if = {
			limit = {
				var:invader_title_var = {
					exists = var:struggle_block_dissolution_faction
					var:struggle_block_dissolution_faction = 0 # permanent
				}
			}
			var:invader_title_var = {
				remove_historicinvasions_dissolution_protection_effect = yes
			}
			title:$LOCAL_TITLE$ = {
				apply_historicinvasions_dissolution_protection_effect = yes
			}
		}
		# temporary (post-story)
		else_if = {
			limit = {
				var:invader_title_var = {
					exists = var:struggle_block_dissolution_faction
					var:struggle_block_dissolution_faction = 1 # temporary
				}
			}
			var:invader_title_var = {
				remove_historicinvasions_dissolution_protection_effect = yes
			}
			title:$LOCAL_TITLE$ = {
				apply_temporary_historicinvasions_dissolution_protection_effect = yes
			}
		}

		# switch story to local title
		set_variable = {
			name = invader_title_var
			value = title:$LOCAL_TITLE$
		}
		# destroy invader title
		story_owner = {
			destroy_title = title:$INVADER_TITLE$
		}
	}
}

apply_invader_government_type_effect = {
	# specific
	if = {
		limit = { $INVADER_CHAR$ = { has_character_modifier = first_ziyarid_modifier } }
		change_government = feudal_government
	}
	else_if = {
		limit = {
			$INVADER_CHAR$ = {
				OR = {
					has_character_modifier = first_terteroba_modifier
					has_character_modifier = first_yelu_modifier
					has_character_modifier = first_borjigin_modifier
					#has_character_modifier = first_jochid_modifier
					#has_character_modifier = first_chagatai_modifier
					has_character_modifier = first_attila_modifier
				}
			}
		}
		if = {
			limit = {
				has_mpo_dlc_trigger = yes
			}
			change_government = nomad_government
		}
		else = {
			change_government = tribal_government
		}
	}

	# more generic triggers
	else_if = { # generally, the capital barony is changed to a castle if it might be tribal
		limit = {
			exists = $INVADER_CHAR$.capital_county.title_province
			$INVADER_CHAR$.capital_county.title_province = { has_holding_type = tribal_holding }
		}
		$INVADER_CHAR$ = { change_government = tribal_government }
	}
	else_if = {
		limit = { $INVADER_CHAR$ = { religion = religion:islam_religion } }
		change_government = clan_government
	}
	else_if = {
		limit = { $INVADER_CHAR$ = { religion = religion:christianity_religion } }
		change_government = feudal_government
	}

	# fallback
	else = { change_government = feudal_government }
}

### story owner death
invader_story_on_death_effect = {
	# end story because all scripted territory has been conquered
	if = {
		limit = { story_owner = { completely_controls_region = special_$INVADER_DYNN$_conquest_region_total } }
		debug_log = "ending invasion story because the conquest goal has been achieved"
		end_story = yes
	}
	else_if = {
		limit = { story_owner = { has_character_flag = invasion_ends_after_completed_conquests } }
		debug_log = "ending invasion story because the conquest goal has been achieved (flag)"
		end_story = yes
	}
	# end story because a war was lost to a player; fallback just in case the AI is still waging war
	else_if = {
		limit = { story_owner = { has_character_flag = invasion_ends_after_player_win } }
		debug_log = "ending invasion story because a player was able to defend against the invader (after owner death)"
		end_story = yes
	}
	# end story because a war was lost to another invader; fallback just in case the AI is still waging war
	# else_if = {
	# 	limit = { story_owner = { has_character_flag = invasion_ends_after_loss_to_invader } }
	# 	debug_log = "ending an invasion story because the invader lost a historic invasion war to another invader (after owner death)"
	# 	end_story = yes
	# }
	# end story because a war was lost (set in CB on defeat for special rebellions or uprisings)
	else_if = {
		limit = { story_owner = { has_character_flag = invasion_ends_after_offensive_defeat } }
		debug_log = "ending invasion story because a player was able to defend against the invader (after owner death)"
		end_story = yes
	}
	# end story because invader has suffered too many defeats
	else_if = {
		limit = { story_owner = { has_character_flag = invasion_ends_after_many_defeats } }
		debug_log = "ending invasion story because the invader has lost too many offensive invasion wars (after owner death)"
		end_story = yes
	}
	# end story due to ruler count / succession?
	else_if = {
		limit = { has_variable = end_of_invasion_story }
		debug_log = "ending invasion story due to story_owner_counter (succession variable)"
		end_story = yes
	}
	# if not, try to find a new story owner
	else = {
		change_variable = {
			name = story_owner_counter
			add = 1
		}
		# handle story succession
		story_owner = {
			if = {
				# previous owner has heir
				limit = { any_child = { is_primary_heir_of = scope:story.story_owner } }
				random_child = {
					limit = { is_primary_heir_of = scope:story.story_owner }
					save_scope_as = new_story_owner
				}
			}
			else_if = {
				# previous owner has other heir (not family?)
				limit = { dynasty = { any_dynasty_member = { is_primary_heir_of = scope:story.story_owner } } }				
				dynasty = {
					random_dynasty_member = {
						limit = { is_primary_heir_of = scope:story.story_owner }
						save_scope_as = new_story_owner
					}
				}
			}
			else_if = {
				# previous owner does NOT have a dynasty heir
				limit = { NOT = { dynasty = { any_dynasty_member = { is_primary_heir_of = scope:story.story_owner } } } }
				# do not set a scope = end story
			}
		}
		# did the script find a valid new story owner? if yes, apply buffs & troops
		if = {
			limit = { exists = scope:new_story_owner }
			# transfer story ownership
			make_story_owner = scope:new_story_owner
			# maintenance
			scope:new_story_owner = { invader_succession_bonus_effect = { INVADER_DYNN = $INVADER_DYNN$ } }
		}
		# if not, end story
		else = {
			debug_log = "ending invasion story because a new story owner couldn't be found"
			end_story = yes
		}
	}
}

### bonus on succession
invader_succession_bonus_effect = {
	# modifiers
	disease_immunity_long_effect = yes
	add_character_modifier = historicinvasions_plot_armor
	add_character_flag = recent_nation_fracturing_faction_war # makes the AI unlikely to start dissolution factions
	add_character_flag = recent_independence_faction_war # makes the AI unlikely to start independence factions
	if = { # make invader immune to viking incursions
		limit = { # trigger of fp1_scandinavian_adventurers.0001
			has_fp1_dlc_trigger = yes
			NOT = { has_game_rule = fp1_scandi_adventurers_off }
			OR = {
				culture:norse = {
					NOT = { has_cultural_era_or_later = culture_era_early_medieval }
				}
				culture:norwegian = {
					NOT = { has_cultural_era_or_later = culture_era_early_medieval }
				}
				culture:danish = {
					NOT = { has_cultural_era_or_later = culture_era_early_medieval }
				}
				culture:swedish = {
					NOT = { has_cultural_era_or_later = culture_era_early_medieval }
				}
			}
		}
		add_character_modifier = fp1_reprieve_from_sa_modifier
	}
	# currency
	add_gold = massive_gold_value
	add_prestige = massive_prestige_value
	add_piety = minor_piety_value
	# inherit title on action: dread
	add_character_flag = {
		flag = gain_invader_succession_dread
		days = 2
	}
	# troops
	trigger_event = {
		id = $INVADER_DYNN$_invasion.0100
		days = 1
	}
}

### story end
invader_story_on_end_effect = {
	debug_log_date = yes
	add_to_global_variable_list = {
		name = historicinvasions_story_ended
		target = flag:$INVADER_DYNN$_story_ended
	}
	scope:story.story_owner = {
		remove_character_modifier = historicinvasions_plot_armor
	}
	if = {
		limit = {
			has_fp1_dlc_trigger = yes
			scope:story.story_owner = { has_character_modifier = fp1_reprieve_from_sa_modifier }
		}
		scope:story.story_owner = { remove_character_modifier = fp1_reprieve_from_sa_modifier }
	}
	# temporary dissolution protection
	var:invader_title_var = {
		apply_temporary_historicinvasions_dissolution_protection_effect = yes
	}
	# interface message
	every_player = {
		send_interface_message = {
			type = event_generic_neutral
			title = message_invasion_story_ended_title
			right_icon = scope:story.story_owner
			custom_tooltip = message_story_ended_tooltip
		}
	}
}

### on story end, if the invasion was unsuccessful, destroy the titular invader title
destroy_titular_invader_title_effect = {
	scope:story.var:invader_title_var = {
		if = {
			limit = {
				is_title_created = yes
				is_titular = yes
			}
			holder = {
				destroy_title = prev
				if = { # prevent error.log entry
					limit = {
						is_alive = yes
					}
					add_pressed_claim = prev
				}
			}
		}
	}
}

### succession weirdness trigger
hi_check_for_valid_story_owner_effect = {
	# make title holder the new invasion story owner; if the title is destroyed, end the story
	if = {
		limit = { $INVADER_TITLE$ = { is_title_created = yes } }
		debug_log = "transferred invasion story ownership because the previous owner lost the invader title"
		#debug_log_scopes = yes
		debug_log_date = yes

		# transfer story ownership
		story_owner = {
			remove_character_modifier = historicinvasions_plot_armor
			if = {
				limit = {
					has_fp1_dlc_trigger = yes
					has_character_modifier = fp1_reprieve_from_sa_modifier
				}
				remove_character_modifier = fp1_reprieve_from_sa_modifier
			}
		}
		make_story_owner = $INVADER_TITLE$.holder
		story_owner = {
			add_character_modifier = historicinvasions_plot_armor
			add_character_flag = recent_nation_fracturing_faction_war # makes the AI unlikely to start dissolution factions
			add_character_flag = recent_independence_faction_war # makes the AI unlikely to start independence factions
			if = { # make invader immune to viking incursions
				limit = { # trigger of fp1_scandinavian_adventurers.0001
					has_fp1_dlc_trigger = yes
					NOT = { has_game_rule = fp1_scandi_adventurers_off }
					OR = {
						culture:norse = {
							NOT = { has_cultural_era_or_later = culture_era_early_medieval }
						}
						culture:norwegian = {
							NOT = { has_cultural_era_or_later = culture_era_early_medieval }
						}
						culture:danish = {
							NOT = { has_cultural_era_or_later = culture_era_early_medieval }
						}
						culture:swedish = {
							NOT = { has_cultural_era_or_later = culture_era_early_medieval }
						}
					}
				}
				add_character_modifier = fp1_reprieve_from_sa_modifier
			}
		}
		# optionally make story end faster
		change_variable = {
			name = story_owner_counter
			add = $STORY_COUNTER_INCREASE$
		}
	}
	else = {
		debug_log = "ending invasion story because the invader title is no longer created"
		debug_log_scopes = yes

		end_story = yes
	}
	# I don't know how the story could start without the title existing. Could happen if a player triggers new events in an old save manually? Fallback, just in case.
	if = {
		limit = { NOT = { exists = $INVADER_TITLE$ } }
		debug_log = "ending invasion story because a title somehow stopped existing"
		debug_log_scopes = yes

		end_story = yes
	}
}

### de jure changes
# list all kingdoms within region
historicinvasions_de_jure_potential = {
	every_county_in_region = {
		region = special_$INVADER_DYNN$_conquest_region_de_jure
		$INVADER_TARGET_TITLE_TIER$ = { add_to_list = $INVADER_DYNN$_de_jure_potential_list }
	}
}
# de jure shift into story_owners title if they control more than x%
historicinvasions_de_jure_change = {
	historicinvasions_de_jure_potential = {
		INVADER_DYNN = $INVADER_DYNN$
		INVADER_TARGET_TITLE_TIER = $INVADER_TARGET_TITLE_TIER$
	}
	every_in_list = {
		list = $INVADER_DYNN$_de_jure_potential_list
		limit = {
			any_in_de_jure_hierarchy = {
				filter = { tier = tier_county }
				continue = { tier > tier_county }
				OR = {
					holder = $INVADER_CHAR$
					holder = { target_is_liege_or_above = $INVADER_CHAR$ }
				}
				percent > $DE_JURE_PERCENT$
			}
			NOT = { de_jure_liege = $INVADER_TITLE$ }
		}
		set_de_jure_liege_title = $INVADER_TITLE$
	}
}

### Story war target evaluation
# in-game debug console: "effect [effect_name] = yes"
choose_next_invasion_target_title_effect = {
	# shows every de jure lower tier title neighboring main title that is within a specified geographical region. if one title_province is de facto owned by the scoped title, it is not considered 'neighboring' any more and thus won't be added to the list
	# INVADER_RANGE = neighboring / neighboring_and_across_water
	$INVADER_CHAR$ = {
		random_character_to_title_$INVADER_RANGE$_$INVADER_TARGET_TITLE_TIER$ = { # within scope = character; target = landed title
			limit = {
				any_de_jure_county = {
					title_province = { geographical_region = special_$INVADER_DYNN$_conquest_region_$INVADER_REGION$ }
				}
			}
			save_scope_as = next_invasion_target_title
		}
	}
	### consider tributaries; doesn't quite work
	# $INVADER_CHAR$ = {
	# 	random_neighboring_and_across_water_top_suzerain_realm = {
	# 		limit = {
	# 			any_de_jure_county = {
	# 				title_province = { geographical_region = special_$INVADER_DYNN$_conquest_region_$INVADER_REGION$ }
	# 			}
	# 		}
	# 		random_in_de_facto_hierarchy = {
	# 			limit = {
	# 				tier = tier_county
	# 				title_province = { geographical_region = special_$INVADER_DYNN$_conquest_region_$INVADER_REGION$ }
	# 			}
	# 			$INVADER_TARGET_TITLE_TIER$ = { save_scope_as = next_invasion_target_title }
	# 		}
	# 	}
	# }
}

### Story war target declaration
war_target_evaluation_and_declaration_effect = {
	save_scope_as = invader
	choose_next_invasion_target_title_effect = {
		INVADER_DYNN = $INVADER_DYNN$
		INVADER_CHAR = $INVADER_CHAR$
		INVADER_REGION = $INVADER_REGION$
		INVADER_TARGET_TITLE_TIER = $INVADER_TARGET_TITLE_TIER$
		INVADER_RANGE = $INVADER_RANGE$
	}
	if = {
		limit = { exists = scope:next_invasion_target_title }
		scope:next_invasion_target_title = {
			every_de_jure_top_liege = {
				limit = {
					is_ruler = yes

					# don't hit yourself
					NOR = {
						this = $INVADER_CHAR$
						top_liege = $INVADER_CHAR$
					}

					# additional restrictions
					$INVADER_LIEGE_RESTRICTIONS$
					
					save_temporary_scope_as = truce_check
					NOT = {
						$INVADER_CHAR$ = {
							any_truce_target = {
								this = scope:truce_check
							}
						}
					}
				}
		 		add_to_temporary_list = lieges_to_declare_on
			}
		}
		every_in_list = {
			list = lieges_to_declare_on
			limit = {
				is_ruler = yes

				# An extra check to make sure we haven't snuck any story_owners territory in
				NOR = {
					this = $INVADER_CHAR$
					top_liege = $INVADER_CHAR$
				}

				# additional restrictions
				$INVADER_LIEGE_RESTRICTIONS$

				# Standard conditions
				is_independent_ruler = yes
				NOR = {
					is_at_war_with = $INVADER_CHAR$
					is_in_list = has_been_sent_subjugation_offer
				}
				save_temporary_scope_as = truce_check
				NOT = {
					$INVADER_CHAR$ = {
						any_truce_target = {
							this = scope:truce_check
						}
					}
				}
			}
			add_to_list = has_been_sent_subjugation_offer
			send_subjugation_demand_effect = {
				INVADER_CHAR = $INVADER_CHAR$
			}
		}
	}
	else = {
		#debug_log = "war_target_evaluation_and_declaration_effect FAILED"
		#debug_log_date = yes
		#debug_log_scopes = yes
	}
}

# send subjugation demand effect
send_subjugation_demand_effect = {
	if = { # allied to invader -> subjugation or broken alliance
		limit = {
			is_allied_to = $INVADER_CHAR$
			# but only allow an alliance to delay the invasion once every N years
			NOT = { has_character_flag = delayed_invasion_via_broken_alliance_flag }
		}
		trigger_event = { # accept / refuse & break alliance
			id = historicinvasions_event.2101
			days = { 3 12 }
		}
	}
	else = { # NOT allied to invader -> subjugation or war
		trigger_event = { # accept / refuse & fight
			id = historicinvasions_event.2001
			days = { 3 12 }
		}

		# block invader from using the declare_war_interaction in the meantime, which would result in two wars
		save_temporary_scope_as = subjugation_offer_recipient
		$INVADER_CHAR$ = {
			add_to_variable_list = {
				name = subjugation_offer_under_consideration
				target = scope:subjugation_offer_recipient
			}
		}
	}
}

# apply vassalage after subjugation demand
# similar to surrender_to_invader_decision
hi_apply_subjugation_demand_effect = {
	### defender > attacker
	if = {
		limit = {
			scope:demand_recipient.primary_title.tier > root.primary_title.tier
		}
		# destroy any vassal titles preventing vassalization
		scope:demand_recipient = {
			every_vassal_or_below = {
				limit = { highest_held_title_tier >= root.primary_title.tier }
				every_held_title = {
					limit = {
						tier >= root.primary_title.tier
						is_head_of_faith = no
					}
					holder = {
						destroy_title = prev
						add_pressed_claim = prev
					}
				}
			}
		}
		# build a list of every direct vassal now that everyone is of a lower tier than that of the invader
		scope:demand_recipient = {
			every_vassal = {
				limit = { highest_held_title_tier > tier_barony }
				add_to_list = demand_recipient_vassals_list
			}
		}			
		# HoF vassal becomes independent if they can't be vassalized
		create_title_and_vassal_change = {
			type = independency
			save_scope_as = hof_independency_change
			add_claim_on_loss = no
		}
		every_in_list = {
			list = demand_recipient_vassals_list
			limit = {
				any_held_title = {
					tier >= root.primary_title.tier # otherwise we could've just vassalized them
					is_head_of_faith = yes
				}
			}
			becomes_independent = { change = scope:hof_independency_change }
			remove_from_list = demand_recipient_vassals_list
		}
		resolve_title_and_vassal_change = scope:hof_independency_change
		# transfer vassals
		custom_tooltip = {
			text = surrender_to_invader_decision_vassal_transfer
			create_title_and_vassal_change = {
				type = swear_fealty
				save_scope_as = demand_recipient_vassal_change
				add_claim_on_loss = no
			}
			every_in_list = {
				list = demand_recipient_vassals_list
				change_liege = {
					liege = root
					change = scope:demand_recipient_vassal_change
				}
			}
			resolve_title_and_vassal_change = scope:demand_recipient_vassal_change
		}
		# destroy every held title that is of a higher tier than that of the invader
		scope:demand_recipient = {
			every_held_title = { # includes head of faith titles; players are warned of this
				limit = {
					tier >= root.primary_title.tier
				}
				holder = {
					destroy_title = prev
					add_pressed_claim = prev
				}
			}
		}
	}

	### defender = attacker
	else_if = {
		limit = {
			scope:demand_recipient.primary_title.tier = root.primary_title.tier
		}
		# build a list of all direct vassals
		scope:demand_recipient = {
			every_vassal = {
				limit = { highest_held_title_tier > tier_barony }
				add_to_list = demand_recipient_vassals_list
			}
		}
		# transfer vassals
		custom_tooltip = {
			text = surrender_to_invader_decision_vassal_transfer
			create_title_and_vassal_change = {
				type = swear_fealty
				save_scope_as = demand_recipient_vassal_change
				add_claim_on_loss = no
			}
			every_in_list = {
				list = demand_recipient_vassals_list
				change_liege = {
					liege = root
					change = scope:demand_recipient_vassal_change
				}
			}
			resolve_title_and_vassal_change = scope:demand_recipient_vassal_change
		}
		# destroy every held title that is of an equal tier than that of the invader
		scope:demand_recipient = {
			every_held_title = { # includes head of faith titles; players are warned of this
				limit = {
					tier = root.primary_title.tier
				}
				root = {
					destroy_title = prev
					add_pressed_claim = prev
				}
			}
		}
	}

	### defender < attacker
	create_title_and_vassal_change = {
		type = swear_fealty
		save_scope_as = vassalization_change
		add_claim_on_loss = no
	}
	scope:demand_recipient = {
		change_liege = {
			liege = root
			change = scope:vassalization_change
		}
	}
	resolve_title_and_vassal_change = scope:vassalization_change
}

### story unlanded conquest evaluation
unlanded_choose_start_invasion_target_title_effect = {
	# adds either every or one random title of the chosen tier within a geographical region to a list
	$EVERY_RANDOM$_$INVADER_TARGET_TITLE_TIER$ = {
		limit = {
			any_de_jure_county = {
				title_province = { geographical_region = special_$INVADER_DYNN$_conquest_region_$INVADER_REGION$ }
			}
		}
		add_to_list = start_invasion_target_titles
	}

	# shortcut for story to pick a randomized title due to current technical limitations (explained further down)
	random_in_list = {
		list = start_invasion_target_titles
		save_scope_as = random_start_invasion_target_title
	}
}

### Story unlanded conquest declaration
# currently doesn't demand subjugation
# due to landless invaders holding a special title that allows them to rule without holding a county, accepting subjugation would result in a liege that doesn't hold any counties until they decide to revoke one
unlanded_war_target_evaluation_and_declaration_effect = {
	save_scope_as = invader
	unlanded_choose_start_invasion_target_title_effect = {
		EVERY_RANDOM = $EVERY_RANDOM$
		INVADER_DYNN = $INVADER_DYNN$
		INVADER_TARGET_TITLE_TIER = $INVADER_TARGET_TITLE_TIER$
		INVADER_REGION = $INVADER_REGION$
	}
	if = {
		limit = { # list isn't empty
			list_size = {
				name = start_invasion_target_titles
				value > 0
			}
		}
		every_in_list = { # scope from every targeted title to its top liege, we go from a list of titles to a list of characters
			list = start_invasion_target_titles
			every_de_jure_top_liege = {
				limit = {
					is_ruler = yes
					NOR = {
						this = scope:invader
						top_liege = scope:invader
					}
					$INVADER_LIEGE_RESTRICTIONS$
				}
				add_to_temporary_list = start_lieges_to_declare_on
			}
		}
		every_in_list = {
			list = start_lieges_to_declare_on
			limit = { # now we repeat the above triggers from just above for good measure
				is_ruler = yes
				NOR = {
					this = scope:invader
					top_liege = scope:invader
				}
				$INVADER_LIEGE_RESTRICTIONS$
				is_independent_ruler = yes
			}
			# the following part is repeated for every character in the list we just built
			# if you could feed a list into the 'start_war' effect, we would simply declare for every single de jure title each picked character holds
			# sadly though, you have to feed each title into the effect one after the other, which makes this not modular at all
			# "prev" is the current ruler from the "start_lieges_to_declare_on" list
			# remember that the "start_invasion_target_titles" list contains either one RANDOM title or EVERY title within a region
			scope:invader = { #todo figure out how to feed a list into here; global_var? argument?
				# while = {
				# 	limit = {
				# 		list_size = {
				# 			name = start_invasion_target_titles
				# 			value > 0
				# 		}
				# 	}
				# 	random_in_list = {
				# 		list = start_invasion_target_titles
				# 		save_scope_as = next_invasion_target_title
				# 	}
				# 	start_war = {
				# 		cb = historicinvasions_landless_invasion_war
				# 		target = prev
				# 		target_title = scope:next_invasion_target_title
				# 	}
				# 	scope:next_invasion_target_title = { remove_from_list = start_invasion_target_titles }
				# 	clear_saved_scope = next_invasion_target_title
				# }
				start_war = {
					cb = historicinvasions_landless_invasion_war
					target = prev
					$INVADER_TITLE_TARGETS$ # generic story setting: INVADER_TITLE_TARGETS = "target_title = scope:random_start_invasion_target_title"
				}				
			}
		}
	}
}

# end the story prematurely, because the invader has lost their #th offensive war
# periodically checked during the story
historicinvasions_invasion_ends_after_defeat_effect = {
	### root = story
	# flag story for completion if appropriate
	if = {
		limit = {
			has_game_rule = historicinvasions_invasion_ends_after_defeat_seven
			var:lost_invasions_counter >= 7
		}
		story_owner = { add_character_flag = invasion_ends_after_many_defeats }
	}
	else_if = {
		limit = {
			has_game_rule = historicinvasions_invasion_ends_after_defeat_six
			var:lost_invasions_counter >= 6
		}
		story_owner = { add_character_flag = invasion_ends_after_many_defeats }
	}
	else_if = {
		limit = {
			has_game_rule = historicinvasions_invasion_ends_after_defeat_five
			var:lost_invasions_counter >= 5
		}
		story_owner = { add_character_flag = invasion_ends_after_many_defeats }
	}
	else_if = {
		limit = {
			has_game_rule = historicinvasions_invasion_ends_after_defeat_four
			var:lost_invasions_counter >= 4
		}
		story_owner = { add_character_flag = invasion_ends_after_many_defeats }
	}
	else_if = {
		limit = {
			has_game_rule = historicinvasions_invasion_ends_after_defeat_three
			var:lost_invasions_counter >= 3
		}
		story_owner = { add_character_flag = invasion_ends_after_many_defeats }
	}
	else_if = {
		limit = {
			has_game_rule = historicinvasions_invasion_ends_after_defeat_two
			var:lost_invasions_counter >= 2
		}
		story_owner = { add_character_flag = invasion_ends_after_many_defeats }
	}
	else_if = {
		limit = {
			has_game_rule = historicinvasions_invasion_ends_after_defeat_one
			var:lost_invasions_counter >= 1
		}
		story_owner = { add_character_flag = invasion_ends_after_many_defeats }
	}
	# end story
	if = {
		limit = {
			story_owner = {
				has_character_flag = invasion_ends_after_many_defeats
				NOR = { # these stories only end on owner death, flag prevents scripted attacks
					primary_title = title:e_mongol_empire
					primary_title = title:e_golden_horde
				}
			}
		}
		debug_log = "ending invasion story because the invader has lost too many offensive invasion wars"
		end_story = yes
	}
}

### kingdoms of hejaz and bahrain / oman
divide_arabia_effect = {
	# k_bahrain
	title:d_oman = { add_to_temporary_list = hi_k_bahrain_list }
	title:d_al-hasa = { add_to_temporary_list = hi_k_bahrain_list }
	title:d_bahrain = { add_to_temporary_list = hi_k_bahrain_list }
	hidden_effect = {
		every_in_list = {
			list = hi_k_bahrain_list
			every_in_de_jure_hierarchy = {
				limit = { tier = tier_duchy }
				set_de_jure_liege_title = title:k_bahrain
			}
		}
	}
	title:k_bahrain = { add_to_temporary_list = hi_e_arabia_list }

	# k_hejaz
	title:d_mecca = { add_to_temporary_list = hi_k_hejaz_list }
	title:d_medina = { add_to_temporary_list = hi_k_hejaz_list }
	title:d_shammar = { add_to_temporary_list = hi_k_hejaz_list }
	hidden_effect = {
		every_in_list = {
			list = hi_k_hejaz_list
			every_in_de_jure_hierarchy = {
				limit = { tier = tier_duchy }
				set_de_jure_liege_title = title:k_hejaz
			}
		}
	}
	title:k_hejaz = { add_to_temporary_list = hi_e_arabia_list }

	# k_arabia
	title:k_arabia = {
		set_capital_county = title:c_sharaf
		set_title_name = hi_k_arabia
	}

	# move into e_arabia
	hidden_effect = {
		every_in_list = {
			list = hi_e_arabia_list
			every_in_de_jure_hierarchy = {
				limit = { tier = tier_kingdom }
				set_de_jure_liege_title = title:e_arabia
			}
		}
	}
}

bookmark_cuman-kipchak_empire_effect = {
	if = {
		limit = {
			has_game_rule = default_terteroba_invasion
			game_start_date >= 1000.1.1
			game_start_date <= 1200.1.1
			NOT = { exists = title:e_terteroba.holder }
			exists = title:e_caspian-pontic_steppe
			title:e_caspian-pontic_steppe = { is_titular = no }
		}
		# de jure changes
		every_kingdom = {
			limit = {
				any_de_jure_county = {
					title_province = { geographical_region = special_terteroba_conquest_region_de_jure }
					percent >= 0.6
				}
			}
			add_to_temporary_list = empire_cumania_de_jure_list
		}
		every_in_list = {
			list = empire_cumania_de_jure_list
			every_in_de_jure_hierarchy = {
				limit = {
					tier = tier_kingdom
					NOT = { target_is_de_jure_liege_or_above = title:e_caspian-pontic_steppe }
				}
				set_de_jure_liege_title = title:e_caspian-pontic_steppe
			}
		}
		# block CB
		add_to_global_variable_list = {
			name = historicinvasions_story_ended
			target = flag:terteroba_story_ended
		}
		# clean-up
		title:e_tartaria = {
			every_in_de_jure_hierarchy = {
				limit = { tier = tier_kingdom }
				set_de_jure_liege_title = title:e_turan
			}
		}
	}
}

bookmark_de_jure_changes_effect = { # vanilla start dates: 867.1.1 / 1066.9.15
	# Italia
	if = {
		limit = {
			title:e_italy = { is_titular = no }
			game_start_date >= 1038.1.1
		}
		title:k_sicily = { add_to_temporary_list = hi_e_italy_list }
		title:k_naples = { add_to_temporary_list = hi_e_italy_list }
		title:k_trinacria = { add_to_temporary_list = hi_e_italy_list }
		title:k_venice = { add_to_temporary_list = hi_e_italy_list }
		every_in_list = {
			list = hi_e_italy_list
			every_in_de_jure_hierarchy = {
				limit = { tier = tier_kingdom }
				set_de_jure_liege_title = title:e_italy
			}
		}
	}
	# Balkans
	if = {
		limit = {
			title:e_carpathia = { is_titular = no }
			game_start_date >= 917.1.1 # first Bulgarian Empire conquers d_dyrrachion
		}
		title:k_croatia = { add_to_temporary_list = hi_e_carpathia_list }
		title:k_bosnia = { add_to_temporary_list = hi_e_carpathia_list }
		title:k_serbia = { add_to_temporary_list = hi_e_carpathia_list }
		every_in_list = {
			list = hi_e_carpathia_list
			every_in_de_jure_hierarchy = {
				limit = { tier = tier_kingdom }
				set_de_jure_liege_title = title:e_carpathia
			}
		}
	}
	# HRE
	if = {
		limit = {
			title:e_hre = { is_titular = no }
			game_start_date >= 962.2.2 # coronation of Otto the Great
		}
		title:k_italy = {
			set_de_jure_liege_title = title:e_hre
		}
		if = { # Bohemia
			limit = { game_start_date >= 1002.1.1 }
			title:k_bohemia = {
				set_de_jure_liege_title = title:e_hre
			}
		}
		if = { # Burgundy
			limit = { game_start_date >= 1032.1.1 }
			title:k_burgundy = {
				set_de_jure_liege_title = title:e_hre
			}
		}
		if = { # Pomerania
			limit = { game_start_date >= 1181.1.1 }
			title:k_pomerania = {
				set_de_jure_liege_title = title:e_hre
			}
		}
	}
}

### county is looted by invader
# called by on_county_occupied
invader_loots_county_effect = {
	scope:county = {
		change_development_level = {
			subtract = scope:county.development_level
			multiply = 0.15
			floor = yes
		}
		hidden_effect = {
			every_county_province = {
				add_province_modifier = {
					modifier = recently_looted_modifier
					years = 3
				}
			}
		}
		# to save space in the displayed message
		show_as_tooltip = {
			add_county_modifier = {
				modifier = recently_looted_modifier
				years = 3
			}
		}
	}
	scope:occupant = {
		add_gold = {
			add = scope:county.development_level
			multiply = 5
			min = 1
		}
		if = {
			limit = { dread < 80 }
			add_dread = 2
		}
	}
}

hi_aid_realm_faith_conversion_relaxed_effect = {
	every_vassal = {
		limit = {
			NOR = {
				any_held_title = { is_head_of_faith = yes }
				has_trait = zealous
			}
		}
		add_opinion = {
			modifier = hi_aid_realm_faith_conversion
			target = prev
		}
	}
}

hi_aid_realm_faith_conversion_same_religion_effect = {
	every_vassal = {
		limit = {
			religion = prev.religion
			NOR = {
				any_held_title = { is_head_of_faith = yes }
				has_trait = zealous
			}
		}
		add_opinion = {
			modifier = hi_aid_realm_faith_conversion
			target = prev
		}
	}
}

hi_aid_realm_faith_conversion_same_faith_effect = {
	every_vassal = {
		limit = {
			faith = prev.faith
			NOR = {
				any_held_title = { is_head_of_faith = yes }
				has_trait = zealous
			}
		}
		add_opinion = {
			modifier = hi_aid_realm_faith_conversion
			target = prev
		}
	}
}

# effect to determine the popularity of faiths within a given region, based on county faiths, not character faiths
hi_determine_county_faiths_in_region_effect = {
	# add every unique faith within the region to a list
	every_county_in_region = {
		region = $FAITH_REGION$
		faith = {
			add_to_list = region_faith_list
		}
	}
	# then order every list entry by their number of counties within the region
	ordered_in_list = {
		list = region_faith_list
		limit = {
			$FAITH_RESTRICTIONS$
		}
		# alternative_limit = {}
		order_by = {
			every_county_in_region = {
				region = $FAITH_REGION$
				limit = { faith = prev }
				add = 1
			}
		}
		position = $FAITH_POPULARITY$ # position 0 is 1st in list, postition 1 is 2nd in list, ...
		save_scope_as = most_popular_region_faith
	}
}