﻿# spawn invader, before story
spawn_terteroba_invader_effect = {
	random_county_in_region = {
		region = special_$INVADER_DYNN$_conquest_region_start
		title_province = { save_scope_as = invader_spawn_location }
	}

	# INVADER
	create_character = {
		template = blush_terteroba_character_template
		location = scope:invader_spawn_location
		save_scope_as = invader
	}

	scope:invader = {
		# randomly set culture to be either Cuman or Kipchak
		random_list = {
			1 = { set_culture = culture:cuman }
			1 = { set_culture = culture:kipchak }
		}

		# other
		add_gold = 2000
		add_prestige = 2500
		add_piety = 150
		
		dynasty = {
			set_dynasty_name = hi_dynn_terteroba
			set_coa = title:e_terteroba

			add_dynasty_prestige_level = 1
			add_dynasty_prestige = 500

			$INVADER_DYNN$_add_dynasty_perks_effect = yes
		}
		house = {
			set_house_name = hi_dynn_terteroba
		}
	}

	every_player = {
		trigger_event = $INVADER_DYNN$_invasion.1001 # Notification: Start of invasion
	}
}

terteroba_add_dynasty_perks_effect = {
	add_dynasty_perk = warfare_legacy_1
}

terteroba_maintenance_effect = {
	apply_invader_modifiers_effect = {
		INVADER_DYNN = $INVADER_DYNN$
	}
	give_invader_land_effect = {
		INVADER_DYNN = $INVADER_DYNN$
		INVADER_CHAR = $INVADER_CHAR$
		STARTING_TIER = $STARTING_TIER$
	}
	spawn_$INVADER_DYNN$_family_effect = {
		INVADER_CHAR = $INVADER_CHAR$
	}
	spawn_invader_servants_effect = {
		INVADER_CHAR = $INVADER_CHAR$
	}
	form_the_invader_title_effect = {
		INVADER_DYNN = $INVADER_DYNN$
		INVADER_CHAR = $INVADER_CHAR$
		INVADER_TITLE = $INVADER_TITLE$
		LANDLESS_YES_NO = $LANDLESS_YES_NO$
	}
}

### populating the life of the invader character
spawn_terteroba_family_effect = {
	# spouse
	create_character = {
		age = { 20 30 }
		template = hi_random_spouse_character_template
		dynasty = none
		gender = female
		employer = $INVADER_CHAR$
		faith = $INVADER_CHAR$.faith
		culture = $INVADER_CHAR$.culture
		save_scope_as = blush_spouse
		after_creation = {
			marry = $INVADER_CHAR$
		}
	}

	# children
	create_character = {
		template = saru_terteroba_character_template
		dynasty = inherit
		culture = $INVADER_CHAR$.culture
		mother = scope:blush_spouse
		father = $INVADER_CHAR$
		employer = $INVADER_CHAR$
	}
	create_character = {
		template = aepak_terteroba_character_template
		dynasty = inherit
		culture = $INVADER_CHAR$.culture
		mother = scope:blush_spouse
		father = $INVADER_CHAR$
		employer = $INVADER_CHAR$
	}
	create_character = {
		template = anna_terteroba_character_template
		dynasty = inherit
		culture = $INVADER_CHAR$.culture
		mother = scope:blush_spouse
		father = $INVADER_CHAR$
		employer = $INVADER_CHAR$
	}
}

# Make sure invader has troops
spawn_terteroba_troops_effect = {
	if = {
		limit = { has_character_flag = is_first_invader_flag }
		spawn_terteroba_army_effect = yes
	}
	spawn_terteroba_army_effect = yes
	spawn_terteroba_army_effect = yes
}
spawn_terteroba_army_effect = {
	spawn_army = {
		name = terteroba_event_troops
		location = capital_province
		uses_supply = no
		inheritable = no
		
		levies = 250
		men_at_arms = {
			type = horse_archers
			stacks = 7
		}
		men_at_arms = {
			type = light_horsemen
			stacks = 12
		}
		men_at_arms = {
			type = light_footmen
			stacks = 3
		}
		# siege
		men_at_arms = {
			type = onager
			stacks = 3
		}
	}
}

# immitate the 1066 culture map more
terteroba_culture_migration_decision_effect = {
	# spread ROOT culture (either cuman or kipchak)
	random_sub_realm_county = {
		limit = {
			tier = tier_county
			title_province = { geographical_region = special_terteroba_culture_region }
			culture = {
				has_cultural_pillar = heritage_turkic
				NOR = {
					this = culture:cuman
					any_parent_culture_or_above = { this = culture:cuman }
					this = culture:kipchak
					any_parent_culture_or_above = { this = culture:kipchak }
				}
			}
		}
		set_county_culture = ROOT.culture
		set_county_faith = ROOT.faith
	}
}

terteroba_empire_title_rename_effect = {
	title:e_terteroba = {
		if = {
			limit = {
				is_title_created = yes
				holder.culture = culture:cuman
			}
			set_capital_county = title:k_cuman.title_capital_county
			set_title_name = e_terteroba_cuman
		}
		else_if = {
			limit = {
				is_title_created = yes
				holder.culture = culture:kipchak
			}
			set_capital_county = title:k_kipchak.title_capital_county
			set_title_name = e_terteroba_kipchak
		}
		else = {
			debug_log = "title:e_terteroba not renamed"
		}
	}
}