﻿ttd_check_residence_and_save_as_temporary_scope_trigger = {
	$CHARACTER$ ?= {
		is_alive = yes
		trigger_if = { # Get characters's permanent location if possible
			limit = {
				is_ruler = yes
				exists = capital_province
			}
			capital_province = { save_temporary_scope_as = $SCOPE_NAME$ }
		}
		trigger_else_if = {
			limit = {
				OR = {
					is_landless_adventurer = yes
					is_landless_administrative = yes
					is_landless_nomad = yes
				}
				exists = domicile.domicile_location
			}
			domicile.domicile_location = { save_temporary_scope_as = $SCOPE_NAME$ }
		}
		trigger_else_if = {
			limit = {
				is_hostage = yes
				exists = warden.capital_province
			}
			liege.capital_province = { save_temporary_scope_as = $SCOPE_NAME$ }
		}
		trigger_else_if = {
			limit = {
				is_hostage = yes
				exists = warden.domicile.domicile_location
			}
			liege.domicile.domicile_location = { save_temporary_scope_as = $SCOPE_NAME$ }
		}
		trigger_else_if = {
			limit = {
				is_courtier = yes
				exists = liege.capital_province
			}
			liege.capital_province = { save_temporary_scope_as = $SCOPE_NAME$ }
		}
		trigger_else_if = {
			limit = {
				is_courtier = yes
				exists = liege.domicile.domicile_location
			}
			liege.domicile.domicile_location = { save_temporary_scope_as = $SCOPE_NAME$ }
		}
		trigger_else_if = {
			limit = {
				is_pool_guest = yes
				exists = host.capital_province
			}
			host.capital_province = { save_temporary_scope_as = $SCOPE_NAME$ }
		}
		trigger_else = { # Fallback for mercenaries and wanderers, any else that don't have a captial province
			location ?= { save_temporary_scope_as = $SCOPE_NAME$ } # ? as location does not always exists
		}
	}
}

ttd_check_capital_and_save_as_temporary_scope_trigger = {
	trigger_if = {
		limit = { $TITLE$.tier = tier_barony }
		scope:target = { save_temporary_scope_as = $SCOPE_NAME$ }
	}
	trigger_else_if = {
		limit = { $TITLE$.tier = tier_county }
		scope:target.title_province ?= { save_temporary_scope_as = $SCOPE_NAME$ }
	}
	trigger_else = {
		$TITLE$.title_capital_county.title_province ?= { save_temporary_scope_as = $SCOPE_NAME$ }
	}
}

ttd_potentially_ignore_diplomatic_range_trigger = {
	scope:actor = { has_variable = ttd_far_off_host }
}

ttd_use_diplomatic_range_with_recipient_trigger = {
	NOR = {
		scope:actor.var:ttd_far_off_host = scope:recipient
		scope:recipient = {
			any_liege_or_above = {
				scope:actor.var:ttd_far_off_host = this
			}
		}
		scope:recipient.host ?= { # So it also works for guests
			OR = {
				this = scope:actor.var:ttd_far_off_host
				any_liege_or_above = {
					scope:actor.var:ttd_far_off_host = this
				}
			}
		}
	}
}

ttd_can_learn_foreign_tactic_forder_tirgger = {
	NOT = { has_trait = forder }
	$LOCATION$ = {
		OR = {
			is_coastal = yes
			is_riverside_province = yes
		}
	}
}

ttd_can_learn_foreign_tactic_forest_fighter_tirgger = {
	NOT = { has_trait = forest_fighter }
	$LOCATION$ = {
		OR = {
			terrain = forest
			terrain = taiga
			$HOST_CULTURE$ = {
				OR = {
					has_cultural_parameter = forest_fighter_trait_more_common
					has_cultural_parameter = can_recruit_forest_specialist
				}
			}
		}
	}
}

ttd_can_learn_foreign_tactic_open_terrain_expert_tirgger = {
	NOT = { has_trait = open_terrain_expert }
	$LOCATION$ = {
		OR = {
			terrain = farmlands
			terrain = plains
			terrain = steppe
			$HOST_CULTURE$ = { has_cultural_parameter = can_recruit_open_specialist }
		}
	}
}

ttd_can_learn_foreign_tactic_rough_terrain_expert_tirgger = {
	NOT = { has_trait = rough_terrain_expert }
	$LOCATION$ = {
		OR = {
			terrain = hills
			terrain = mountains
			terrain = wetlands
			$HOST_CULTURE$ = {
				OR = {
					has_cultural_parameter = rough_terrain_expert_trait_more_common
					has_cultural_parameter = can_recruit_mountain_specialist
					has_cultural_parameter = can_recruit_hill_specialist
				}
			}
		}
	}
}

ttd_can_learn_foreign_tactic_desert_warrior_tirgger = {
	NOT = { has_trait = desert_warrior }
	$LOCATION$ = {
		OR = {
			terrain = desert
			terrain = desert_mountains
			terrain = drylands
			terrain = oasis
			$HOST_CULTURE$ = {
				OR = {
					has_cultural_parameter = desert_warrior_trait_more_common
					has_cultural_parameter = can_recruit_dry_specialist
				}
			}
		}
	}
}

ttd_can_learn_foreign_tactic_jungle_stalker_tirgger = {
	NOT = { has_trait = jungle_stalker }
	$LOCATION$ = {
		OR = {
			terrain = jungle
			$HOST_CULTURE$ = {
				OR = {
					has_cultural_parameter = jungle_stalker_trait_more_common
					has_cultural_parameter = can_recruit_jungle_specialist
				}
			}
		}
	}
}

ttd_can_learn_foreign_tactic_winter_soldier_tirgger = {
	NOT = { has_trait = winter_soldier }
	$LOCATION$ = {
		OR = {
			has_province_modifier = winter_mild_modifier
			has_province_modifier = winter_normal_modifier
			has_province_modifier = winter_harsh_modifier
			$HOST_CULTURE$ = {
				OR = {
					has_cultural_parameter = winter_soldier_trait_more_common
					has_cultural_parameter = can_recruit_winter_specialist
				}
			}
		}
	}
}

ttd_can_learn_foreign_tactic_trigger = {
	OR = {
		ttd_can_learn_foreign_tactic_forder_tirgger = { LOCATION = $LOCATION$ }
		ttd_can_learn_foreign_tactic_forest_fighter_tirgger = { HOST_CULTURE = $HOST_CULTURE$ LOCATION = $LOCATION$ }
		ttd_can_learn_foreign_tactic_open_terrain_expert_tirgger = { HOST_CULTURE = $HOST_CULTURE$ LOCATION = $LOCATION$ }
		ttd_can_learn_foreign_tactic_rough_terrain_expert_tirgger = { HOST_CULTURE = $HOST_CULTURE$ LOCATION = $LOCATION$ }
		ttd_can_learn_foreign_tactic_desert_warrior_tirgger = { HOST_CULTURE = $HOST_CULTURE$ LOCATION = $LOCATION$ }
		ttd_can_learn_foreign_tactic_jungle_stalker_tirgger = { HOST_CULTURE = $HOST_CULTURE$ LOCATION = $LOCATION$ }
		ttd_can_learn_foreign_tactic_winter_soldier_tirgger = { HOST_CULTURE = $HOST_CULTURE$ LOCATION = $LOCATION$ }
	}
}

ttd_is_moving_to_new_court = {
	has_variable = ttd_newlywed_traveling_to_court_of
}