﻿namespace = bme_turkish_adventurers

#	Startup event, delaying the initial cycle.
bme_turkish_adventurers.0001 = {
	type = empty
	hidden = yes

	trigger = {
		NOT = { has_game_rule = turkish_adventurers_off }
	}

	immediate = {
		trigger_event = {
			id = bme_turkish_adventurers.0002
			years = 5
		}
	}
}

bme_turkish_adventurers.0002 = {
	type = empty
	hidden = yes

	immediate = {

		# choose the target duchy

		ordered_county_in_region = {
			region = world_me_persia
			limit = {
				bme_valid_migration_target_for_turkish_nomads = yes
			}
			order_by = { value = bme_preferred_adventure_location }

			save_temporary_scope_as = target_land
		}

		# choose the adventurer

		create_character = { # backup character
			location = title:c_mangyshlak.title_province
			faith = faith:ashari
			culture = culture:turkish # oghuz
			gender = male
			martial = 10
			diplomacy = 10
			intrigue = 10
			stewardship = 10
			prowess = 10
			trait = education_martial_4
			# save_scope_as = turkish_adventurer
			after_creation = {
				add_trait = rough_terrain_expert
			}
		}

		every_living_character = {
			limit = {
				bme_valid_turkish_adventurer = yes
			}
			add_to_temporary_list = turkish_adventurer_list
		}

		if = {
			limit = {
				any_in_list = {
					list = turkish_adventurer_list
					bme_valid_turkish_adventurer = yes
				}
			}
			ordered_in_list = {
				list = turkish_adventurer_list
				limit = { bme_valid_turkish_adventurer = yes }
				order_by = { value = bme_preferred_turkish_adventurer }
	
				add_character_modifier = bme_turkish_adventurer_modifier
				add_trait = adventurer
				# Loan a little gold in case they're in debt/to keep them going.
				add_gold = scandinavian_adventurer_efficacy_loan_value
				save_scope_as = turkish_adventurer
			}
		}
		else = { # create another backup character? not really necessary.
		}

		# here we go

		if = {
			limit = {
				NOT = {
					title:e_persia = { exists = holder }
				}
				exists = scope:target_land
				exists = scope:turkish_adventurer
			}

			# create Turkish Nomads title
			
			create_dynamic_title = {
				tier = duchy
				name = turkish_nomads_title_name
			}
			create_title_and_vassal_change = {
				type = created
				save_scope_as = change
				add_claim_on_loss = no
			}
			scope:new_title = {
				set_capital_county = title:c_mangyshlak
				set_landless_title = yes
				set_destroy_on_gain_same_tier = yes
				set_no_automatic_claims = yes
				set_can_be_named_after_dynasty = yes
				change_title_holder = {
					holder = scope:turkish_adventurer
					change = scope:change
				}
			}
			resolve_title_and_vassal_change = scope:change
	
			# war stuff
	
			scope:turkish_adventurer = {
	
				# start the war
	
				start_war = {
					casus_belli = bme_turkish_adventurer_conquest
					target = scope:target_land.holder.top_liege
					target_title = scope:target_land.duchy
				}
	
				# assemble the army
	
				spawn_army = {
					levies = {
						add = {
							#Base of 400.
							add = scope:target_land.holder.top_liege.max_military_strength
							#Multiply that by the realm size of the target's top_liege.
							multiply = 1.5
							#Account for allies, adding more without just nullifying them.
							scope:target_land.holder.top_liege = {
								every_ally = { add = this.max_military_strength }
							}
							#Cut it off so things don't get too ridiculous.
							max = 8000
							#Make sure all adventurers have a moderately respectable force.
							min = 1000
						}
					}
					inheritable = no
					location = title:b_yangadzha.title_province
					name = turkish_adventurer_event_troops
				}
				spawn_army = {
					men_at_arms = {
						type = horse_archers
						stacks = {
							value = 1
							multiply = scope:target_land.holder.top_liege.primary_title.tier
						}
					}
					inheritable = yes
					location = title:b_yangadzha.title_province
					name = turkish_adventurer_event_troops
				}
				spawn_army = {
					men_at_arms = {
						type = light_horsemen
						stacks = {
							value = 1
							multiply = scope:target_land.holder.top_liege.primary_title.tier
						}
					}
					inheritable = yes
					location = title:b_yangadzha.title_province
					name = turkish_adventurer_event_troops
				}
			}

			#Trigger the next wave of events.
			if = { # Rare
				limit = { has_game_rule = turkish_adventurers_apocalyptic }
				trigger_event = {
					id = bme_turkish_adventurers.0002
					days = { 300 420 }
				}
			}
			if = { # Occasional
				limit = { has_game_rule = turkish_adventurers_frequent }
				trigger_event = {
					id = bme_turkish_adventurers.0002
					days = { 1725 1910 }
				}
			}
			if = { # Frequent
				limit = { has_game_rule = turkish_adventurers_occasional }
				trigger_event = {
					id = bme_turkish_adventurers.0002
					days = { 3540 3725 }
				}
			}
			if = { # Apocalyptic
				limit = { has_game_rule = turkish_adventurers_rare }
				trigger_event = {
					id = bme_turkish_adventurers.0002
					days = { 5350 5600 }
				}
			}
		}
		else = { # failed. try again next year.
			trigger_event = {
				id = bme_turkish_adventurers.0002
				days = { 300 420 }
			}
		}
	}
}
