﻿populate_steppe_culture_faith_lists_effect = {
	### clean slate; lists can't have duplicates
	# every_in_list = {
	# 	list = steppe_turkic_culture_list
	# 	remove_from_list = steppe_turkic_culture_list
	# }
	# every_in_list = {
	# 	list = steppe_faith_list
	# 	remove_from_list = steppe_faith_list
	# }

	### populate culture and faith lists

	# every_culture_global = {
	# 	limit = {
	# 		OR = {
	# 			has_cultural_pillar = heritage_turkic
	# 			has_cultural_pillar = language_turkic
	# 		}
	# 	}
	# 	add_to_list = global_turkic_culture_list
	# 	debug_log_scopes = yes
	# }

	# turkic steppe cultures
	every_county_in_region = {
		region = world_steppe
		limit = {
			culture = {
				OR = {
					has_cultural_pillar = heritage_turkic
					any_parent_culture_or_above = { has_cultural_pillar = heritage_turkic }
					has_cultural_pillar = language_turkic
					any_parent_culture_or_above = { has_cultural_pillar = language_turkic }
				}
			}
		}
		culture = { add_to_list = steppe_turkic_culture_list }
	}
	# nomadic steppe faiths
	every_county_in_region = {
		region = world_steppe
		limit = {
			culture = {
				OR = {
					has_cultural_pillar = heritage_turkic
					any_parent_culture_or_above = { has_cultural_pillar = heritage_turkic }
					has_cultural_pillar = language_turkic
					any_parent_culture_or_above = { has_cultural_pillar = language_turkic }
					has_cultural_pillar = heritage_mongolic
					any_parent_culture_or_above = { has_cultural_pillar = heritage_mongolic }
					has_cultural_pillar = language_mongolic
					any_parent_culture_or_above = { has_cultural_pillar = language_mongolic }
				}
			}
		}
		faith = { add_to_list = steppe_faith_list }
	}

	# pick random culture & faith
	random_in_list = {
		list = steppe_turkic_culture_list
		save_scope_as = steppe_turkic_culture
	}
	random_in_list = {
		list = steppe_faith_list
		# filter out Siberian and Finnish religions as well as some Christian faiths
		limit = {
			NOR = {
				religion = religion:siberian_religion
				religion = religion:finno_ugric_religion
				this = faith:catholic
				this = faith:orthodox
			}
		}
		alternative_limit = {
			NOR = {
				religion = religion:siberian_religion
				religion = religion:finno_ugric_religion
				this = faith:catholic
			}
		}
		alternative_limit = {
			NOR = {
				religion = religion:siberian_religion
				religion = religion:finno_ugric_religion
			}
		}
		# but, as a fallback, just pick any
		alternative_limit = {
			always = yes
		}
		save_scope_as = steppe_faith
	}

	#debug_log = "nomad LIST - CULTURE / FAITH"
	#debug_log_date = yes
	#debug_log_scopes = yes
}

### create nomad
turkish_migration_effect = {
	# pick spawn location
	random_county_in_region = {
		region = world_steppe
		title_province = { save_scope_as = turkish_nomad_spawn_location }
	}

	# pick target title
	turkish_migration_target_evaluation_effect = { MIGRATION_REGION = $MIGRATION_REGION$ } # sets scope:turkish_migration_target

	# populate current list of cultures and faiths present in the steppe and pick one of each randomly
	populate_steppe_culture_faith_lists_effect = yes

	# if a target title is found, go further
	if = {
		limit = { exists = scope:turkish_migration_target }
		# debug log
		#debug_log = "a turkish migration character has appeared"
		#debug_log_date = yes
		# this is 35 Eldiguz; we need to make sure that the scope:turkish_nomad is newly set at the very beginning, otherwise it will linger
		if = {
			limit = {
				exists = scope:turkish_nomad
				scope:turkish_nomad = { has_character_flag = block_scripted_nomad_creation }
			}
			remove_character_flag = block_scripted_nomad_creation
		}
		# otherwise create a nomad
		else = {
			if = {
				limit = { has_game_rule = inversed_gender_equality }
				create_character = {
					template = turkish_nomad_adventurer_character_template
					culture = scope:steppe_turkic_culture
					faith = scope:steppe_faith
					dynasty = generate
					gender = female
					location = scope:turkish_nomad_spawn_location
					save_scope_as = turkish_nomad
				}
			}
			else_if = {
				limit = { has_game_rule = full_gender_equality }
				create_character = {
					template = turkish_nomad_adventurer_character_template
					culture = scope:steppe_turkic_culture
					faith = scope:steppe_faith
					dynasty = generate
					gender_female_chance = 50
					location = scope:turkish_nomad_spawn_location
					save_scope_as = turkish_nomad
				}
			}
			else = {
				create_character = {
					template = turkish_nomad_adventurer_character_template
					culture = scope:steppe_turkic_culture
					faith = scope:steppe_faith
					gender = male
					dynasty = generate
					location = scope:turkish_nomad_spawn_location
					save_scope_as = turkish_nomad
				}
			}
		}

		# create temporary title
		create_dynamic_title = {
			tier = duchy
			name = turkish_nomad_title
		}
		create_title_and_vassal_change = {
			type = created
			save_scope_as = title_change
			add_claim_on_loss = no
		}
		scope:new_title = {
			set_capital_county = scope:turkish_migration_target.county
			set_landless_title = yes
			set_delete_on_destroy = yes
			set_no_automatic_claims = yes
			set_can_be_named_after_dynasty = yes
			set_destroy_on_gain_same_tier = yes
			set_destroy_on_succession = no
			change_title_holder = {
				holder = scope:turkish_nomad
				change = scope:title_change
			}
		}
		resolve_title_and_vassal_change = scope:title_change
		scope:new_title = {
			#set_title_name = hi_turkish_nomad_title_new
			generate_coa = yes
			set_color_from_title = scope:turkish_migration_target
		}

		scope:turkish_nomad = {
			set_variable = {
				name = turkish_nomad_title
				value = scope:new_title
			}
			# set_variable = {
			# 	name = turkish_migration_target
			# 	value = scope:turkish_migration_target
			# }
		}

		### send vassalage offer
		scope:turkish_migration_target.county_controller.top_liege = {
			save_scope_as = offer_recipient
			trigger_event = turkish_migrations.2001
		}
	}
	# but if no title is found, do nothing. might happen if invader targets an emperor who doesn't hold land within the starting region (failing empire)
	else = {
		debug_log = "valid turkish migration target title not found"
		debug_log_scopes = yes
		debug_log_date = yes
	}
}

### pick target title
turkish_migration_target_evaluation_effect = {
	# invader
	if = {
		limit = {
			exists = scope:turkish_nomad # scope is set further down, except for Eldiguz
			scope:turkish_nomad = { has_character_flag = is_eldiguz_flag }
		}
		title:d_azerbaijan.de_jure_liege.de_jure_liege = { # Eldiguz will simply spawn as an invader in event eldiguzid_invasion.0001 if the empire is NOT created
			holder = {
				random_realm_county = {
					limit = {
						exists = holder
						holder = { is_ai = yes }
						title_province = { geographical_region = $MIGRATION_REGION$ }
					}
					alternative_limit = {
						exists = holder
						title_province = { geographical_region = $MIGRATION_REGION$ }
					}
					county = { save_scope_as = turkish_migration_target }
					duchy = { save_scope_as = turkish_migration_target_duchy } # localization
				}
			}
		}
	}
	# regular nomad
	else = {
		random_county_in_region = {
			region = $MIGRATION_REGION$
			limit = {
				exists = holder
				holder = { is_ai = yes }
				NOR = {
					# certain character modifiers protect from nomads
					holder.top_liege = { has_character_modifier = historicinvasions_plot_armor }
					holder.top_liege = { has_character_modifier = defended_from_turkish_nomad_modifier }
					holder.top_liege = { has_character_modifier = settled_turkish_nomad_modifier }
					# don't pick realm capital counties
					this = holder.top_liege.capital_county
				}
			}
			alternative_limit = { # ignores plot armor modifier
				exists = holder
				holder = { is_ai = yes }
				NOR = {
					# certain character modifiers protect from nomads
					holder.top_liege = { has_character_modifier = defended_from_turkish_nomad_modifier }
					holder.top_liege = { has_character_modifier = settled_turkish_nomad_modifier }
					# don't pick realm capital counties
					this = holder.top_liege.capital_county 
				}
			}
			county = { save_scope_as = turkish_migration_target }
			duchy = { save_scope_as = turkish_migration_target_duchy } # localization
		}
	}
}

### nomad buys land and becomes a vassal
# on average, each duchy within the targeted region consists of 4 counties, 4 * 75g = 300g
turkish_nomad_receives_territory_effect = {
	# hand out counties
	create_title_and_vassal_change = {
		type = granted
		save_scope_as = change_one
	}
	if = { # duchy
		limit = { tier_$RECEIVED_TIER$ = tier_duchy }
		scope:turkish_migration_target.$RECEIVED_TIER$ = {
			every_in_de_jure_hierarchy = {
				limit = {
					tier = tier_county
					exists = holder
					holder = { is_ai = yes }
					NOT = { this = holder.top_liege.capital_county } # don't sell your capital
					holder.top_liege = scope:offer_recipient # don't sell land belonging to other realms
				}
				add_to_list = received_counties
			}
		}
		every_in_list = {
			list = received_counties
			change_title_holder = {
				holder = scope:turkish_nomad
				change = scope:change_one
			}
			if = {
				limit = { NOT = { holder = { this = scope:offer_recipient } } } # don't hate yourself
				holder = {
					add_pressed_claim = prev
					add_to_list = angry_previous_holders_list
				}
			}
			scope:turkish_nomad = {
				change_variable = {
					name = number_of_bought_counties_var
					add = 1
				}
			}
			remove_from_list = received_counties
		}
		scope:turkish_nomad = {
			custom_tooltip = {
				text = turkish_migrations.2001.nomad_pays_gold.tt
				pay_short_term_gold = {
					target = scope:offer_recipient
					gold = {
						value = 75
						multiply = scope:turkish_nomad.var:number_of_bought_counties_var
						min = 0
					}
				}
			}
		}
	}
	else = { # county
		scope:turkish_migration_target = {
			change_title_holder = {
				holder = scope:turkish_nomad
				change = scope:change_one
			}
			if = {
				limit = { NOT = { holder = { this = scope:offer_recipient } } } # don't hate yourself
				holder = {
					add_pressed_claim = prev
					add_to_list = angry_previous_holders_list
				}
			}
		}
		scope:turkish_nomad = {
			change_variable = {
				name = number_of_bought_counties_var
				add = 1
			}
			pay_short_term_gold = {
				target = scope:offer_recipient
				gold = 75
			}
		}
	}
	resolve_title_and_vassal_change = scope:change_one

	# previous holders become angry
	every_in_list = {
		list = angry_previous_holders_list
		add_opinion = {
			modifier = revoked_title
			target = scope:offer_recipient
		}
		add_opinion = {
			modifier = usurped_title
			target = scope:turkish_nomad
		}
	}

	# swear fealty
	create_title_and_vassal_change = {
		type = swear_fealty
		save_scope_as = change_two
	}
	scope:turkish_nomad = {
		if = {
			limit = {
				has_variable = turkish_nomad_title
				highest_held_title_tier > tier_$RECEIVED_TIER$
			}
			hidden_effect = { destroy_title = this.var:turkish_nomad_title }
		}
		change_liege = {
			liege = scope:offer_recipient
			change = scope:change_two
		}
	}
	resolve_title_and_vassal_change = scope:change_two

	# tribe moves in, too - CULTURE / FAITH
	scope:turkish_migration_target = {
		random = {
			chance = { # similar effect in "turkish_migrations.3001"
				value = 100
				subtract = scope:turkish_migration_target.development_level
				subtract = scope:turkish_migration_target.development_level
				subtract = scope:turkish_migration_target.development_level
				if = {
					limit = { scope:turkish_migration_target.development_level >= 16 }
					multiply = 0
				}
				else_if = {
					limit = { scope:turkish_migration_target.development_level >= 14 }
					multiply = 0.2
				}
				else_if = {
					limit = { scope:turkish_migration_target.development_level >= 12 }
					multiply = 0.4
				}
				else_if = {
					limit = { scope:turkish_migration_target.development_level >= 10 }
					multiply = 0.6
				}
				else_if = {
					limit = { scope:turkish_migration_target.development_level >= 8 }
					multiply = 0.8
				}
				max = 100
				min = 0
			}
			set_county_culture = scope:turkish_nomad.culture
			set_county_faith = scope:turkish_nomad.faith
		}
		#debug_log = "nomad LIST - CULTURE / FAITH change (bought)"
		#debug_log_date = yes
		#debug_log_scopes = yes
	}

	# maintenance
	scope:turkish_nomad = {
		set_realm_capital = scope:turkish_migration_target
		add_truce_both_ways = {
			character = scope:offer_recipient
			override = yes
			years = historicinvasions_swear_fealty_truce_value
			name = TRUCE_SWEAR_FEALTY
		}
		add_opinion = {
			target = scope:offer_recipient
			modifier = received_title_county
		}
		progress_towards_friend_effect = {
			REASON = friend_granted_title
			CHARACTER = scope:offer_recipient
			OPINION = 0
		}
		if = {
			limit = { highest_held_title_tier > tier_county }
			primary_title = { set_landless_title = no }
		}
		# generate family
		trigger_event = game_rule.1001
		# convert bastard trait
		if = {
			limit = { has_trait = bastard }
			remove_trait = bastard
			add_trait = bastard_founder
		}
		# apply government form of their new liege
		if = {
			limit = {
				scope:offer_recipient = { has_government = clan_government }
			}
			change_government = clan_government
		}
		else_if = {
			limit = {
				scope:offer_recipient = { has_government = feudal_government }
			}
			change_government = feudal_government
		}
		else_if = {
			limit = {
				scope:offer_recipient = { has_government = tribal_government }
			}
			change_government = tribal_government
		}
		# army
		spawn_army = {
			name = turkish_nomad_event_troops
			location = scope:turkish_nomad.primary_title.title_capital_county.title_province
			uses_supply = yes
			inheritable = yes
			
			levies = 200
			men_at_arms = {
				type = horse_archers
				stacks = 3
			}
			men_at_arms = {
				type = light_horsemen
				stacks = 3
			}
		}
	}
}

turkic_migration_war_declaration_effect = {
	scope:attacker = { save_scope_as = turkish_nomad }
	if = {
		limit = { scope:defender.current_military_strength > 15000 }
		turkic_migration_war_declaration_troop_effect = yes
		turkic_migration_war_declaration_troop_effect = yes
		turkic_migration_war_declaration_troop_effect = yes
	}
	else_if = {
		limit = { scope:defender.current_military_strength > 9000 }
		turkic_migration_war_declaration_troop_effect = yes
		turkic_migration_war_declaration_troop_effect = yes
	}
	else = {
		turkic_migration_war_declaration_troop_effect = yes
	}
}
turkic_migration_war_declaration_troop_effect = {
	# 3000
	spawn_army = {
		name = turkish_nomad_event_troops
		location = scope:turkish_nomad.primary_title.title_capital_county.title_province
		war = scope:war
		uses_supply = no
		inheritable = no
		
		levies = 350
		men_at_arms = {
			type = horse_archers
			stacks = 16
		}
		men_at_arms = {
			type = light_horsemen
			stacks = 9
		}
		men_at_arms = {
			type = armored_horsemen
			stacks = 3
		}
		# siege
		men_at_arms = {
			type = mangonel
			stacks = 5
		}
	}
}

turkic_migration_war_victory_effect = {
	scope:attacker = { save_scope_as = turkish_nomad }
	scope:turkish_nomad = {
		# tribe moves in, too; via event so I can scope to their capital county 1 day later - since everything happens at once, they don't have a capital yet (the effect still works though - but it causes hundreds of error.log entries)
		# I could simply scope to the original migration target, but that might not be their chosen capital
		trigger_event = {
			id = turkish_migrations.3001
			days = 1
		}
		# switch over to a landed title
		if = {
			limit = { highest_held_title_tier > tier_county }
			primary_title = { set_landless_title = no }
		}
		# generate family
		trigger_event = game_rule.1001
		# convert bastard trait
		if = {
			limit = { has_trait = bastard }
			remove_trait = bastard
			add_trait = bastard_founder
		}
		# army
		spawn_army = {
			name = turkish_nomad_event_troops
			location = scope:turkish_nomad.primary_title.title_capital_county.title_province
			uses_supply = yes
			inheritable = yes
			
			levies = 500
			men_at_arms = {
				type = horse_archers
				stacks = 5
			}
			men_at_arms = {
				type = light_horsemen
				stacks = 3
			}
			men_at_arms = {
				type = armored_horsemen
				stacks = 1
			}
		}
	}
}