﻿setup_hunter_leader_effect = {
	save_scope_as = faction
	random_faction_county_member = {
		save_scope_as = hunter_county
	}
	create_character = {
			location = scope:hunter_county.title_province
			template = POD_mortal_hunter_template
			faith = faith:shadowinquisition
			culture = scope:hunter_county.culture
			# name = 
			# dynasty = none
			# prowess = 7			
			# faith = faith:catholic
			# trait = education_martial_4
			# trait = supehunter
			# culture = scope:hunter_county.culture
		gender_female_chance = {
				add = 100
		}
		after_creation = {
			remove_trait = mortal
			add_trait = supehunter
			if = {
				limit = {
					scope:hunter_county.title_province.county.holder = { has_trait = kueijin }
				}
				add_trait = shih
			}
			else = {
				POD_give_orison_effect = yes
				POD_give_random_endowments_effect = yes
			}
			POD_character_hunter_faith_initialization_effect = yes
			POD_set_resonance_effect = yes
		}
		save_scope_as = hunter_leader
	}
	scope:hunter_leader = {
		#add_character_flag = hunter_faction_random_hunter
		add_character_flag = had_POD_character_initialization
	}


	create_dynamic_title = {
		tier = duchy
		name = FACTION_HUNTER_TITLE_NAME
	}
	create_title_and_vassal_change = {
			type = created
			save_scope_as = change
			add_claim_on_loss = no
		}

	scope:new_title = {
		set_capital_county = scope:hunter_county
		set_landless_title = yes
		set_destroy_on_succession = yes
		set_delete_on_destroy = yes
		set_no_automatic_claims = yes
		set_definitive_form = yes
		change_title_holder = {
			holder = scope:hunter_leader
			change = scope:change
		}

		set_variable = {
			name = faction
			value = scope:faction
		}

		# avoid unused variable error. This variable is referenced in code
		var:faction = {}
	}
	resolve_title_and_vassal_change = scope:change
	scope:hunter_leader = {
		set_variable = {
			name = hunter_title
			value = scope:new_title
		}
	}
	scope:new_title = { generate_coa = factions }

	scope:hunter_leader = {
		set_variable = {
			name = rebel_leader_hunters
			value = scope:faction
		}
		join_faction_skip_check = scope:faction
	}

	set_special_character = scope:hunter_leader
}


# hunter_faction_demands_enforced = {
	# # Save scopes for later usage.
	
	# $FACTION$ = {
		# every_faction_county_member = {
			# custom = hunter_faction_every_county
			# add_to_list = siezed_counties
		# }
		# #hidden_effect = {
		# #	destroy_faction = yes
		# #}
	# }
	
	# $FACTION_LEADER$ = {
		# joined_faction = {
			# save_scope_as = popular_faction
			# every_faction_member = {
				# add_to_list = faction_members
			# }
			# faction_target = {
				# save_scope_as = faction_target
			# }
		# }
	# }

	# # Compile a list of all counties belonging to the faction.
	# scope:popular_faction = {
		# every_faction_county_member = {
			# add_to_list = siezed_counties
		# }

	# }
	# # Additionally, if we're at war (and not just pressing demands), add all occupied counties and connected counties of the correct culture/faith.
	# if = {
		# limit = {
			# $FACTION_LEADER$ = {
				# is_at_war_with = scope:faction_target
			# }
		# }
		# scope:faction_target = {
			# every_sub_realm_county = {
				# limit = {
					# county_controller = $FACTION_LEADER$
				# }
				# add_to_list = siezed_counties
			# }
		# }

		# every_in_list = {
			# list = siezed_counties
			# add_adjacent_valid_counties_to_list = {
				# FAITH = $FACTION_LEADER$.faith
				# TOP_LIEGE = scope:faction_target
				# LIST = siezed_counties
			# }
		# }
	# }

	# # Select a 'capital' county for the faction leader, this will be removed from the siezed county list as we will handle it separately.
	# scope:popular_faction = {
		# ordered_faction_county_member = {
			# order_by = {
				# value = total_county_levies

				# multiply = {
					# value = 1


					# # Additional 50% bonus points for counties located in the de jure area of the liege title we want to sieze.
					# if = {
						# limit = {
							# target_is_de_jure_liege_or_above = $TARGET_TITLE$
						# }
						# add = 0.50
					# }
				# }
			# }

			# #remove_from_list = siezed_counties
			# save_scope_as = capital_county
		# }
	# }

	# # CHANGE ZERO: Give the leader a county to stop landless characters from screwing up the hierarchy.
	# hidden_effect = {
		# create_title_and_vassal_change = change_zero
		# scope:change_zero = {
			# set_title_and_vassal_change_type = conquest_populist
			# set_add_claim_on_loss = yes
		# }
		# scope:capital_county = {
			# change_title_holder = {
				# holder = $FACTION_LEADER$
				# change = scope:change_zero
				# take_baronies = no
			# }
		# }
		# resolve_title_and_vassal_change = scope:change_zero
	# }


	# # CHANGE ONE: Give the leader an appropriately-ranked title.
	# create_title_and_vassal_change = change_one
	# if = {
		# # If 11+ counties were taken, give them a Kingdom.
		# limit = {
			# any_in_list = {
				# list = siezed_counties
				# count >= 11
			# }
		# }

		# # Check if we can create or usurp a De Jure Kingdom from the map
		# every_in_list = {
			# list = siezed_counties
			# kingdom = {
				# if = {
					# limit = {
						# OR = {
							# NOT = { exists = holder}
							# trigger_if = {
								# limit = { exists = holder}
								# holder.top_liege = scope:faction_target

								# # Don't usurp the primary title from independent rulers if they don't have an equal-tier title to replace it with.
								# trigger_if = {
									# limit = {
										# this = holder.primary_title
										# holder = holder.top_liege
									# }
									# holder = {
										# any_held_title = {
											# tier = tier_kingdom
											# count >= 2
										# }
									# }
								# }
							# }
						# }
						# NAND = {
							# this = title:k_east_francia
							# exists = title:e_hre.holder
						# }
					# }
					# add_to_list = potential_kingdoms
				# }
			# }
		# }

		# # If we found one or more valid De Jure Kingdoms, pick the best one for us to take.
		# if = {
			# limit = {
				# any_in_list = {
					# list = potential_kingdoms
					# count >= 1
					# NAND = {
						# this = title:k_east_francia
						# exists = title:e_hre.holder
					# }
				# }
			# }
			# ordered_in_list = {
				# list = potential_kingdoms
				# limit = {
					# NAND = {
						# this = title:k_east_francia
						# exists = title:e_hre.holder
					# }
				# }

				# order_by = {
					# value = 1
					# every_in_list = {
						# list = siezed_counties
						# limit = { kingdom = prev }
						# add = 1
					# }
				# }

				# if = {
					# limit = {
						# NOT = { exists = holder}
					# }
					# scope:change_one = { set_title_and_vassal_change_type = created	}
				# }
				# else = {
					# scope:change_one = { set_title_and_vassal_change_type = conquest }
				# }
				# change_title_holder = {
					# holder = $FACTION_LEADER$
					# change = scope:change_one
				# }
			# }
		# }
	# }

	# else_if = {
		# # If 5+ counties were taken, give them a Duchy.
		# limit = {
			# any_in_list = {
				# list = siezed_counties
				# count >= 5
			# }
		# }

		# # Check if we can create or usurp a De Jure Duchy from the map
		# every_in_list = {
			# list = siezed_counties
			# duchy = {
				# if = {
					# limit = {
						# OR = {
							# NOT = { exists = holder}
							# trigger_if = {
								# limit = { exists = holder}
								# holder.top_liege = scope:faction_target

								# # Don't usurp the primary title from independent rulers if they don't have an equal-tier title to replace it with.
								# trigger_if = {
									# limit = {
										# this = holder.primary_title
										# holder = holder.top_liege
									# }
									# holder = {
										# any_held_title = {
											# tier = tier_duchy
											# count >= 2
										# }
									# }
								# }
							# }
						# }
					# }
					# add_to_list = potential_duchies
				# }
			# }
		# }

		# # If we found one or more valid De Jure Duchies, pick the best one for us to take.
		# if = {
			# limit = {
				# any_in_list = {
					# list = potential_duchies
					# count >= 1
				# }
			# }
			# ordered_in_list = {
				# list = potential_duchies

				# order_by = {
					# value = 1
					# every_in_list = {
						# list = siezed_counties
						# limit = { duchy = prev }
						# add = 1
					# }
				# }

				# if = {
					# limit = {
						# NOT = { exists = holder}
					# }
					# scope:change_one = { set_title_and_vassal_change_type = created	}
				# }
				# else = {
					# scope:change_one = { set_title_and_vassal_change_type = conquest }
				# }
				# change_title_holder = {
					# holder = $FACTION_LEADER$
					# change = scope:change_one
				# }
			# }
		# }
		# # Otherwise, create a new titular Duchy title.
		# else = {
			# create_popular_revolt_title_effect = {
				# FACTION = scope:popular_faction
				# FACTION_LEADER = $FACTION_LEADER$
				# TIER = duchy
			# }
			# scope:change_one = {

				# set_title_and_vassal_change_type = created
			# }
			# scope:new_title = {
				# change_title_holder = {
					# holder = $FACTION_LEADER$
					# change = scope:change_one
				# }
			# }
		# }
	# }

	# # If 4 or fewer counties were taken, leader only gets the county titles themselves.
	# resolve_title_and_vassal_change = scope:change_one

	# # CHANGE TWO: Transfer the siezed counties to the leader.
	# create_title_and_vassal_change = change_two
	# scope:change_two = {
		# set_title_and_vassal_change_type = conquest
		# set_add_claim_on_loss = yes
	# }
	# every_in_list = {
		# list = siezed_counties

		# change_title_holder = {
			# holder = $FACTION_LEADER$
			# change = scope:change_two
			# take_baronies = no
		# }
	# }
	# resolve_title_and_vassal_change = scope:change_two

	# # CHANGE THREE: Split the leader off into an independent realm.
	# create_title_and_vassal_change = change_three
	# scope:change_three = {
		# set_title_and_vassal_change_type = independency
		# set_add_claim_on_loss = yes
	# }

	# resolve_title_and_vassal_change = scope:change_three

	# # CHANGE FOUR: If there are any disconnected counties, split them off via exclave independence rules.
	# scope:capital_county = {
		# split_noncontiguous_counties_from_revolt_effect = {
			# FACTION_LEADER = $FACTION_LEADER$
		# }
	# }

	# # If the faction still exists, dissolve it (it's no longer relevant).
	# hidden_effect = {
		# if = {
			# limit = {
				# exists = scope:popular_faction
			# }
			# scope:popular_faction = {
				# destroy_faction = yes
			# }
		# }
	# }

	# #Set up government type.
	# hidden_effect = {
		# $FACTION_LEADER$ = {
				# change_government = feudal_government
		# }
	# }
# }