﻿namespace = turkish_migrations

### initial event called on by the on_action
turkish_migrations.0001 = {
	scope = none
	hidden = yes

	immediate = {
		debug_log = "starting periodical turkish migrations"
		add_to_global_variable_list = {
			name = historicinvasions_story_started
			target = flag:turkish_migrations_started
		}
		every_player = { trigger_event = turkish_migrations.1001 }
		trigger_event = turkish_migrations.0010 # periodic event cycle
	}
}

### periodic event cycle
turkish_migrations.0010 = {
	scope = none
	hidden = yes

	# stop conditions
	trigger = {
		current_date < 1220.1.1
		NAND = {
			has_global_variable = mongols_have_appeared
			is_target_in_global_variable_list = {
				name = historicinvasions_story_started
				target = flag:borjigin_story_started
			}
		}
	}

	immediate = {
		# spawn nomad
		turkish_migration_effect = { MIGRATION_REGION = special_turkish_migration_region }
		# keep going
		trigger_event = {
			id = turkish_migrations.0010
			days = { 380 1800 }
		}
	}

	# stop
	on_trigger_fail = {
		debug_log = "periodical turkish migrations have ended"
		debug_log_date = yes
		add_to_global_variable_list = {
			name = historicinvasions_story_ended
			target = flag:turkish_migrations_ended
		}
		every_player = { trigger_event = turkish_migrations.1002 }
	}
}

### player notification: turkic migrations have started
turkish_migrations.1001 = {
	type = character_event
	title = turkish_migrations.1001.t
	desc = turkish_migrations.1001.desc
	
	theme = stewardship

	left_portrait = {
		character = root
		animation = personality_rational
	}
	
	option = {
		name = turkish_migrations.1001.a
		custom_tooltip = turkish_migrations.1001.tt
	}

	option = {
		name = turkish_migrations.1001.b
		trigger = { culture = { has_cultural_pillar = heritage_turkic } }
		custom_tooltip = turkish_migrations.1001.tt
		add_prestige = 50
	}
}

### player notification: turkic migrations have ended
turkish_migrations.1002 = {
	type = character_event
	title = turkish_migrations.1002.t
	desc = turkish_migrations.1002.desc
	
	theme = stewardship

	left_portrait = {
		character = root
		animation = personality_rational
	}
	
	option = {
		name = turkish_migrations.1002.a
		custom_tooltip = turkish_migrations.1002.tt
	}
}

### nomad offers to become vassal
turkish_migrations.2001 = {
	type = letter_event
	opening = turkish_migrations.2001.opening
	desc = turkish_migrations.2001.desc
	sender = scope:turkish_nomad

	immediate = {
		scope:turkish_nomad = {
			set_variable = { # prevent error log spam
				name = number_of_bought_counties_var
				value = 0
			}
		}
	}

	after = {
		scope:turkish_nomad = { remove_variable = number_of_bought_counties_var }
	}

	option = { # sell county
		name = turkish_migrations.2001.b
		trigger = {
			highest_held_title_tier >= tier_duchy
			NOT = { scope:turkish_nomad = { has_character_flag = is_eldiguz_flag } }
			NAND = { # don't sell your only county
				domain_size = 1
				this = scope:turkish_migration_target.holder
			}
			custom_description = { # count is NOT protected from title revocation
				text = vassal_contract_forbids_revocation
				NOT = { scope:turkish_migration_target.holder = { vassal_contract_has_flag = vassal_contract_cannot_revoke_titles } }
			}
			NOT = { scope:turkish_migration_target = scope:offer_recipient.capital_county } # don't sell your capital; fallback, since the script doesn't pick capitals to begin with
		}
		show_as_unavailable = { # trigger failed, but this trigger is true (invserse of one of the above triggers), causes option to appear greyed out
			scope:turkish_migration_target.holder = { vassal_contract_has_flag = vassal_contract_cannot_revoke_titles } # count IS protected from title revocation		
		}
		turkish_nomad_receives_territory_effect = {
			RECEIVED_TIER = county
		}
		ai_chance = {
			base = 0
			modifier = { add = turkish_migrations_accept_value }
		}
	}

	option = { # sell duchy
		name = turkish_migrations.2001.a
		trigger = {
			highest_held_title_tier >= tier_kingdom
			NOT = { scope:turkish_nomad = { has_character_flag = is_eldiguz_flag } }
			scope:turkish_migration_target.duchy = {
				any_in_de_jure_hierarchy = {
					tier = tier_county
					exists = holder
					holder = { is_ai = yes }
					NOT = { this = scope:offer_recipient.capital_county } # don't sell your capital; capital counties aren't picked by the script, but it might have picked a county within the capital duchy
					holder.top_liege = scope:offer_recipient # don't sell land belonging to other realms
				}
				custom_description = { # duke is NOT protected from title revocation
					text = vassal_contract_forbids_revocation
					NOT = { holder ?= { vassal_contract_has_flag = vassal_contract_cannot_revoke_titles } }
				}
			}
		}
		show_as_unavailable = { # trigger failed, but this trigger is true (invserse of one of the above triggers), causes option to appear greyed out
			scope:turkish_migration_target.duchy.holder ?= { vassal_contract_has_flag = vassal_contract_cannot_revoke_titles } # duke IS protected from title revocation
		}
		add_character_modifier = {
			modifier = settled_turkish_nomad_modifier
			years = 15
		}
		turkish_nomad_receives_territory_effect = {
			RECEIVED_TIER = duchy
		}
		scope:turkish_nomad = {
			add_trait = loyal
		}
		ai_chance = {
			base = 0
			modifier = { add = turkish_migrations_accept_value }
			modifier = { factor = 0.3 }
		}
	}

	### delete?
	# option = { # all checks failed for player - sell a different duchy instead
	# 	name = turkish_migrations.2001.c
	# 	add_internal_flag = special
	# 	trigger = {
	# 		# only for players
	# 		is_ai = no
	# 		# .a triggers failed
	# 		NAND = {
	# 			highest_held_title_tier >= tier_kingdom
	# 			scope:turkish_migration_target.duchy = {
	# 				any_in_de_jure_hierarchy = {
	# 					tier = tier_county
	# 					exists = holder
	# 					holder = { is_ai = yes }
	# 					NOT = { this = scope:offer_recipient.capital_county } # don't sell your capital
	# 					holder.top_liege = scope:offer_recipient # don't sell land belonging to other realms
	# 				}
	# 			}
	# 		}
	# 		# .b triggers failed
	# 		NAND = {
	# 			highest_held_title_tier >= tier_duchy
	# 			NAND = { # don't sell your only county
	# 				domain_size = 1
	# 				this = scope:turkish_migration_target.holder
	# 			}
	# 			NOT = { scope:turkish_migration_target = scope:offer_recipient.capital_county } # don't sell your capital; fallback, since the script doesn't pick capitals to begin with
	# 		}
	# 		# look for an alternative duchy
	# 		highest_held_title_tier >= tier_kingdom
	# 		any_sub_realm_duchy = {
	# 			any_in_de_jure_hierarchy = {
	# 				tier = tier_county
	# 				exists = holder
	# 				holder = { is_ai = yes } # problem: only works for is_ai = no, even when those counties are only held by AI??
	# 				NOT = { this = scope:offer_recipient.capital_county } # don't sell your capital
	# 				holder.top_liege = scope:offer_recipient # don't sell land belonging to other realms
	# 			}
	# 		}
	# 	}
	# 	random_sub_realm_county = {
	# 		limit = {
	# 			exists = holder
	# 			holder = { is_ai = yes }
	# 			NOT = { this = scope:offer_recipient.capital_county } # don't sell your capital
	# 			holder.top_liege = scope:offer_recipient # don't sell land belonging to other realms
	# 		}
	# 		save_scope_as = turkish_migration_target	
	# 	}
	# 	add_character_modifier = {
	# 		modifier = settled_turkish_nomad_modifier
	# 		years = 15
	# 	}
	# 	turkish_nomad_receives_territory_effect = {
	# 		RECEIVED_TIER = duchy
	# 	}
	# 	scope:turkish_nomad = {
	# 		add_trait = loyal
	# 		pay_short_term_gold = {
	# 			target = scope:offer_recipient
	# 			gold = 300
	# 		}
	# 	}
	# 	ai_chance = {
	# 		base = 0
	# 		modifier = { add = turkish_migrations_accept_value }
	# 	}
	# }

	option = { # ALL checks failed for player - offer to sell a different county instead
		name = turkish_migrations.2001.d
		add_internal_flag = special
		trigger = {
			# only for players
			is_ai = no
			# .b triggers failed (county)
			NAND = {
				highest_held_title_tier >= tier_duchy
				NAND = { # don't sell your only county
					domain_size = 1
					this = scope:turkish_migration_target.holder
				}
				NOT = { scope:turkish_migration_target.holder ?= { vassal_contract_has_flag = vassal_contract_cannot_revoke_titles } } # count is NOT protected from title revocation
				NOT = { scope:turkish_migration_target = scope:offer_recipient.capital_county } # don't sell your capital; fallback, since the script doesn't pick capitals to begin with
			}
			# .a triggers failed (duchy)
			NAND = {
				highest_held_title_tier >= tier_kingdom
				scope:turkish_migration_target.duchy = {
					any_in_de_jure_hierarchy = {
						tier = tier_county
						exists = holder
						holder = { is_ai = yes }
						NOT = { this = scope:offer_recipient.capital_county } # don't sell your capital
						holder.top_liege = scope:offer_recipient # don't sell land belonging to other realms
					}
				}
				NOT = { scope:turkish_migration_target.duchy.holder ?= { vassal_contract_has_flag = vassal_contract_cannot_revoke_titles } } # duke is NOT protected from title revocation; duke may not exist
			}
			# eldiguz will never trigger this
			NOT = { scope:turkish_nomad = { has_character_flag = is_eldiguz_flag } }
			# is there an alternative AI county that can be revoked?
			highest_held_title_tier >= tier_duchy
			any_sub_realm_county = {
				exists = holder
				holder = {
					is_ai = yes
					NOT = { vassal_contract_has_flag = vassal_contract_cannot_revoke_titles } # count is NOT protected from title revocation
				}
			}
		}
		### effect:
		random_sub_realm_county = {
			limit = {
				exists = holder
				holder = {
					is_ai = yes
					NOT = { vassal_contract_has_flag = vassal_contract_cannot_revoke_titles } # count is NOT protected from title revocation
				}
			}
			save_scope_as = turkish_migration_target	
		}
		turkish_nomad_receives_territory_effect = {
			RECEIVED_TIER = county
		}
		ai_chance = {
			base = 0
			modifier = { add = turkish_migrations_accept_value }
		}
	}

	option = { # 35 Eldiguz
		name = { # "will receive kingdom"
			trigger = {
				OR = {
					title:d_azerbaijan.de_jure_liege = { is_title_created = no }
					title:d_azerbaijan.de_jure_liege.holder = {
						exists = yes
						is_ai = yes
						#this = root # offer recipient
					}
				}
			}
			text = turkish_migrations.2001.eldiguz.kingdom
		}
		name = { # "will receive duchy"
			trigger = {
				NOR = {
					title:d_azerbaijan.de_jure_liege = { is_title_created = no }
					title:d_azerbaijan.de_jure_liege.holder = {
						exists = yes
						is_ai = yes
						#this = root # offer recipient
					}
				}
			}
			text = turkish_migrations.2001.eldiguz.duchy
		}
		add_internal_flag = special
		trigger = {
			highest_held_title_tier >= tier_empire
			scope:turkish_nomad = { has_character_flag = is_eldiguz_flag }
			scope:turkish_migration_target.duchy = {
				any_in_de_jure_hierarchy = {
					count > 0
					tier = tier_county
					exists = holder
					holder = { is_ai = yes } # don't steal player-held counties
					NOT = { this = scope:offer_recipient.capital_county } # don't sell your capital; capital counties aren't picked by the script, but it might have picked a county within the capital duchy
					holder.top_liege = scope:offer_recipient # don't sell land belonging to other realms
				}
				# custom_description = { # duke is NOT protected from title revocation
				# 	text = vassal_contract_forbids_revocation
				# 	NOT = { holder ?= { vassal_contract_has_flag = vassal_contract_cannot_revoke_titles } }
				# }
			}
		}
		# show_as_unavailable = { # trigger failed, but this trigger is true (invserse of one of the above triggers), causes option to appear greyed out
		# 	scope:turkish_migration_target.duchy.holder ?= { vassal_contract_has_flag = vassal_contract_cannot_revoke_titles } # duke IS protected from title revocation
		# }
		add_character_modifier = {
			modifier = settled_turkish_nomad_modifier
			years = 15
		}
		# hand out counties
		turkish_nomad_receives_territory_effect = {
			RECEIVED_TIER = duchy
		}
		scope:turkish_nomad = {
			add_trait = loyal
		}

		### form de jure Azerbaijan
		# if = {
		# 	limit = {
		# 		exists = title:k_azerbaijan
		# 		NOR = {
		# 			has_game_rule = historicinvasions_custom_title_decisions_disabled
		# 			is_target_in_global_variable_list = { # has the decision been taken?
		# 				name = unavailable_unique_decisions
		# 				target = flag:establish_kingdom_azerbaijan_decision
		# 			}
		# 			title:k_azerbaijan = { is_titular = no } # already has de jure land
		# 		}
		# 	}
		# 	add_to_global_variable_list = { # block decision
		# 		name = unavailable_unique_decisions
		# 		target = flag:establish_kingdom_azerbaijan_decision
		# 	}

		# 	title:k_azerbaijan = { set_de_jure_liege_title = title:d_azerbaijan.de_jure_liege.de_jure_liege }

		# 	every_duchy = {
		# 		limit = {
		# 			title_capital_county.title_province ?= { geographical_region = special_azerbaijan_kingdom_region }
		# 			is_titular = no # has de jure land; filters out mercenaries, etc.
		# 		}
		# 		set_de_jure_liege_title = title:k_azerbaijan
		# 	}
		# }

		# KINGDOM
		if = {
			limit = {
				OR = {
					title:d_azerbaijan.de_jure_liege = { is_title_created = no }
					title:d_azerbaijan.de_jure_liege.holder = {
						exists = yes
						is_ai = yes # don't steal player titles
						#this = root # offer recipient
					}
				}
			}

			# grant title
			create_title_and_vassal_change = {
				type = created
				save_scope_as = title_change
				add_claim_on_loss = yes
			}

			title:d_azerbaijan.de_jure_liege = {
				if = {
					limit = {
						#holder = { exists = yes }
						holder ?= { NOT = { this = scope:offer_recipient } }
					} # don't hate yourself
					holder = {
						add_pressed_claim = prev
						add_opinion = {
							modifier = revoked_title
							target = scope:offer_recipient
						}
						add_opinion = {
							modifier = usurped_title
							target = scope:turkish_nomad
						}
					}
				}
				change_title_holder = {
					holder = scope:turkish_nomad
					change = scope:title_change
				}
			}
			resolve_title_and_vassal_change = scope:title_change
		}
		# DUCHY; see turkish_nomad_receives_territory_effect further up

		# only trigger once
		add_to_global_variable_list = {
			name = historicinvasions_story_started
			target = flag:eldiguzid_story_started
		}
		add_to_global_variable_list = {
			name = historicinvasions_story_ended
			target = flag:eldiguzid_story_ended
		}
		ai_chance = {
			base = 0
			modifier = { add = turkish_migrations_accept_value }
			modifier = { factor = 0.3 }
			modifier = { add = 1 } # so this option is always available to the AI
		}
	}

	option = { # war!
		name = turkish_migrations.2001.j
		trigger = { always = yes }
		add_internal_flag = dangerous
		# invader
		if = {
			limit = { scope:turkish_nomad = { has_character_flag = is_eldiguz_flag } } # nomad_invader flag also works
			custom_tooltip = turkish_migrations.2001.eldiguz.tt
			scope:turkish_nomad = { create_story = story_eldiguzid_invasion }
		}
		# regular nomad
		else = {
			custom_tooltip = turkish_migrations.2001.j.tt
			scope:turkish_nomad = {
				start_war = {
					cb = turkic_migration_war
					target = scope:turkish_migration_target.county_controller.top_liege
					target_title = scope:turkish_migration_target.duchy
				}
			}
		}
		ai_chance = {
			base = 0
			modifier = { add = turkish_migrations_refuse_value } # if Eldiguz -> never refuse
		}
		fallback = yes # just in case
	}
}

### culture changes 1 day after war to prevent error.log spam
# called in turkic_migration_war_victory_effect
turkish_migrations.3001 = {
	scope = none
	hidden = yes

	immediate = {
		#debug_log = "nomad LIST - CULTURE / FAITH change (war victory)"
		#debug_log_date = yes
		#debug_log_scopes = yes
		capital_county = {
			random = {
				chance = { # similar effect in "turkish_nomad_receives_territory_effect"
					value = 100
					subtract = development_level
					subtract = development_level
					subtract = development_level
					if = {
						limit = { development_level >= 16 }
						multiply = 0
					}
					else_if = {
						limit = { development_level >= 14 }
						multiply = 0.2
					}
					else_if = {
						limit = { development_level >= 12 }
						multiply = 0.4
					}
					else_if = {
						limit = { development_level >= 10 }
						multiply = 0.6
					}
					else_if = {
						limit = { development_level >= 8 }
						multiply = 0.8
					}
					max = 100
					min = 0
				}
				set_county_culture = root.culture
				set_county_faith = root.faith
			}
		}
	}
}