﻿ttd_major_distance_ai_will_do_modifier = { # Best to avoid a large distance penalty
	modifier = {
		ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = $CHARACTER_1$ SCOPE_NAME = ttd_location_1 }
		ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = $CHARACTER_2$ SCOPE_NAME = ttd_location_2 }
		scope:ttd_location_1 = {
			squared_distance = {
				target = scope:ttd_location_2
				value >= ttd_squared_distance_medium
			}
		}
		factor = ttd_major_distance_factor_value
	}
}

ttd_medium_distance_ai_will_do_modifier = { # Best to avoid a large distance penalty
	modifier = {
		ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = $CHARACTER_1$ SCOPE_NAME = ttd_location_1 }
		ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = $CHARACTER_2$ SCOPE_NAME = ttd_location_2 }
		scope:ttd_location_1 = {
			squared_distance = {
				target = scope:ttd_location_2
				value >= ttd_squared_distance_medium
			}
		}
		factor = ttd_medium_distance_factor_value
	}
}

ttd_minor_distance_ai_will_do_modifier = { # Best to avoid a large distance penalty
	modifier = {
		ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = $CHARACTER_1$ SCOPE_NAME = ttd_location_1 }
		ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = $CHARACTER_2$ SCOPE_NAME = ttd_location_2 }
		scope:ttd_location_1 = {
			squared_distance = {
				target = scope:ttd_location_2
				value >= ttd_squared_distance_medium
			}
		}
		factor = ttd_minor_distance_factor_value
	}
}



ttd_domicile_distance_ai_will_do_modifier = {
	modifier = {
		ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = $CHARACTER$ SCOPE_NAME = ttd_location_1 }
		$DOMICILE$.domicile_location ?= { save_temporary_scope_as = ttd_location_2 }
		scope:ttd_location_1 = {
			squared_distance = {
				target = scope:ttd_location_2
				value >= ttd_squared_distance_medium
			}
		}
		add = ttd_medium_distance_factor_value
	}
}

ttd_title_distance_ai_will_do_modifier = { # Best to avoid a large distance penalty
	modifier = {
		ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = $CHARACTER$ SCOPE_NAME = ttd_location_1 }
		ttd_check_capital_and_save_as_temporary_scope_trigger = { TITLE = $TITLE$ SCOPE_NAME = ttd_location_2 }
		scope:ttd_location_1 = {
			squared_distance = {
				target = scope:ttd_location_2
				value >= ttd_squared_distance_medium
			}
		}
		factor = ttd_medium_distance_factor_value
	}
}

ttd_major_distance_success_chance_modifier = {
	first_valid = {
		modifier = { # Same Location Bonus
			add = 15
			desc = TTD_SAME_LOCATION_BONUS_DESC
			scope:owner ?= { is_alive = yes }
			scope:target ?= {
				is_alive = yes
				is_in_the_same_court_as = scope:owner
			}
		}
		modifier = {
			desc = TTD_DISTANCE_PENALTY_DESC
			ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:owner SCOPE_NAME = ttd_location_1 }
			ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:target SCOPE_NAME = ttd_location_2 }
			scope:ttd_location_1 = {
				squared_distance = {
					target = scope:ttd_location_2
					value >= ttd_squared_distance_none_small
				}
			}
			add = ttd_major_distance_value
		}
	}
}

ttd_medium_distance_success_chance_modifier = {
	first_valid = {
		modifier = { # Same Location Bonus
			add = 10
			desc = TTD_SAME_LOCATION_BONUS_DESC
			scope:owner ?= { is_alive = yes }
			scope:target ?= {
				is_alive = yes
				is_in_the_same_court_as = scope:owner
			}
		}
		modifier = {
			desc = TTD_DISTANCE_PENALTY_DESC
			ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:owner SCOPE_NAME = ttd_location_1 }
			ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:target SCOPE_NAME = ttd_location_2 }
			scope:ttd_location_1 = {
				squared_distance = {
					target = scope:ttd_location_2
					value >= ttd_squared_distance_small
				}
			}
			add = ttd_medium_distance_value
		}
	}
}

ttd_minor_distance_success_chance_modifier = {
	first_valid = {
		modifier = { # Same Location Bonus
			add = 5
			desc = TTD_SAME_LOCATION_BONUS_DESC
			scope:owner ?= { is_alive = yes }
			scope:target ?= {
				is_alive = yes
				is_in_the_same_court_as = scope:owner
			}
		}
		modifier = {
			desc = TTD_DISTANCE_PENALTY_DESC
			ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:owner SCOPE_NAME = ttd_location_1 }
			ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:target SCOPE_NAME = ttd_location_2 }
			scope:ttd_location_1 = {
				squared_distance = {
					target = scope:ttd_location_2
					value >= ttd_squared_distance_medium
				}
			}
			add = ttd_minor_distance_value
		}
	}
}

ttd_liege_distance_success_chance_modifier = {
	first_valid = {
		modifier = { # Same Location Bonus
			add = 10
			desc = TTD_SAME_LOCATION_BONUS_DESC
			scope:owner ?= { is_alive = yes }
			scope:owner.liege ?= {
				is_alive = yes
				is_in_the_same_court_as = scope:owner
			}
		}
		modifier = {
			desc = TTD_DISTANCE_TO_LIEGE_PENALTY_DESC
			ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:owner SCOPE_NAME = ttd_location_1 }
			ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:owner.liege SCOPE_NAME = ttd_location_2 }
			scope:ttd_location_1 = {
				squared_distance = {
					target = scope:ttd_location_2
					value >= ttd_squared_distance_small
				}
			}
			add = ttd_medium_distance_value
		}
	}
}

ttd_top_liege_distance_success_chance_modifier = {
	first_valid = {
		modifier = { # Same Location Bonus
			add = 5
			desc = TTD_SAME_LOCATION_BONUS_DESC
			scope:owner ?= { is_alive = yes }
			scope:owner.top_liege ?= {
				is_alive = yes
				is_in_the_same_court_as = scope:owner
			}
		}
		modifier = {
			desc = TTD_DISTANCE_TO_TOP_LIEGE_PENALTY_DESC
			ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:owner SCOPE_NAME = ttd_location_1 }
			ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:owner.top_liege SCOPE_NAME = ttd_location_2 }
			scope:ttd_location_1 = {
				squared_distance = {
					target = scope:ttd_location_2
					value >= ttd_squared_distance_medium
				}
			}
			add = ttd_minor_distance_value
		}
	}
}

ttd_domicile_distance_success_chance_modifier = {
	modifier = {
		desc = TTD_DISTANCE_TO_DOMICILE_PENALTY_DESC
		ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:owner SCOPE_NAME = ttd_location_1 }
		scope:target.domicile_location ?= { save_temporary_scope_as = ttd_location_2 }
		scope:ttd_location_1 = {
			squared_distance = {
				target = scope:ttd_location_2
				value >= ttd_squared_distance_small
			}
		}
		add = ttd_medium_distance_value
	}
}

ttd_title_distance_success_chance_modifier = {
	first_valid = {
		modifier = { # Same Location Bonus
			add = 10
			desc = TTD_SAME_LOCATION_BONUS_DESC
			scope:owner ?= {
				is_ruler = yes
				any_held_title = {
					this = scope:target
				}
			}
		}
		modifier = {
			desc = TTD_DISTANCE_TO_TITLE_PENALTY_DESC
			ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:owner SCOPE_NAME = ttd_location_1 }
			ttd_check_capital_and_save_as_temporary_scope_trigger = { TITLE = scope:target SCOPE_NAME = ttd_location_2 }
			scope:ttd_location_1 = {
				squared_distance = {
					target = scope:ttd_location_2
					value >= ttd_squared_distance_small
				}
			}
			add = ttd_medium_distance_value
		}
	}
}

ttd_major_distance_ai_accept_modifier = {
	modifier = { # Perfer arrangements that don't involve traveling long distances
		desc = TTD_DISTANCE_PENALTY_ACCEPTANCE_DESC
		ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:actor SCOPE_NAME = ttd_location_1 }
		ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:recipient SCOPE_NAME = ttd_location_2 }
		scope:ttd_location_1 = {
			squared_distance = {
				target = scope:ttd_location_2
				value >= ttd_squared_distance_none_small
			}
		}
		add = ttd_major_distance_value
	}
}

ttd_medium_distance_ai_accept_modifier = {
	modifier = { # Perfer arrangements that don't involve traveling long distances
		desc = TTD_DISTANCE_PENALTY_ACCEPTANCE_DESC
		ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:actor SCOPE_NAME = ttd_location_1 }
		ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:recipient SCOPE_NAME = ttd_location_2 }
		scope:ttd_location_1 = {
			squared_distance = {
				target = scope:ttd_location_2
				value >= ttd_squared_distance_small
			}
		}
		add = ttd_medium_distance_value
	}
}

ttd_minor_distance_ai_accept_modifier = {
	modifier = { # Perfer arrangements that don't involve traveling long distances
		desc = TTD_DISTANCE_PENALTY_ACCEPTANCE_DESC
		ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:actor SCOPE_NAME = ttd_location_1 }
		ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:recipient SCOPE_NAME = ttd_location_2 }
		scope:ttd_location_1 = {
			squared_distance = {
				target = scope:ttd_location_2
				value >= ttd_squared_distance_medium
			}
		}
		add = ttd_minor_distance_value
	}
}

ttd_medium_or_minor_distance_ai_accept_modifier = {
	first_valid = {
		modifier = { # Perfer arrangements that don't involve traveling long distances
			desc = TTD_DISTANCE_PENALTY_ACCEPTANCE_DESC
			scope:secondary_recipient = {
				OR = {
					this = scope:recipient
					is_close_family_of = scope:recipient
					ao_is_adoptive_close_family_of_character_trigger = { CHARACTER = scope:recipient }
				}
			}
			ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:actor SCOPE_NAME = ttd_location_1 }
			ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:recipient SCOPE_NAME = ttd_location_2 }
			scope:ttd_location_1 = {
				squared_distance = {
					target = scope:ttd_location_2
					value >= ttd_squared_distance_small
				}
			}
			add = ttd_medium_distance_value
		}
		modifier = { # Perfer arrangements that don't involve traveling long distances
			desc = TTD_DISTANCE_PENALTY_ACCEPTANCE_DESC
			ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:actor SCOPE_NAME = ttd_location_1 }
			ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:recipient SCOPE_NAME = ttd_location_2 }
			scope:ttd_location_1 = {
				squared_distance = {
					target = scope:ttd_location_2
					value >= ttd_squared_distance_medium
				}
			}
			add = ttd_minor_distance_value
		}
	}
}

ttd_marriage_distance_ai_accept_modifier = {
	first_valid = {
		modifier = { # Perfer alliances that don't involve traveling long distances (except inside realm and neighbours
			desc = TTD_DISTANCE_PENALTY_ACCEPTANCE_ALLIANCE_DESC
			scope:recipient = {
				is_ruler = yes
				yields_alliance = {
					candidate = scope:secondary_recipient
					target = scope:actor
					target_candidate = scope:secondary_actor
				}
				NOR = {
					is_allied_to = scope:actor
					target_is_liege_or_above = scope:actor
					is_tributary_of_suzerain_or_above = scope:actor
					AND = {
						is_landed = yes
						scope:actor = { is_landed = yes }
						OR = { # Exclude sub-vassals as character_is_land_realm_neighbor is inconsistent with them
							scope:actor.liege = liege
							AND = {
								scope:actor.top_liege = scope:actor
								top_liege = this
							}
						}
						character_is_land_realm_neighbor = scope:actor
					}
				}
			}
			scope:actor = {
				NOR = {
					target_is_liege_or_above = scope:recipient
					is_tributary_of_suzerain_or_above = scope:recipient
				}
			}
			ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:actor SCOPE_NAME = ttd_location_1 }
			ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:recipient SCOPE_NAME = ttd_location_2 }
			scope:ttd_location_1 = {
				squared_distance = {
					target = scope:ttd_location_2
					value >= ttd_squared_distance_none_small
				}
			}
			add = ttd_major_distance_value
		}
		modifier = { # Perfer arrangements that don't involve you traveling long distances
			desc = TTD_DISTANCE_PENALTY_ACCEPTANCE_RECIPIENT_DESC
			scope:secondary_recipient = scope:recipient
			ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:actor SCOPE_NAME = ttd_location_1 }
			ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:recipient SCOPE_NAME = ttd_location_2 }
			scope:ttd_location_1 = {
				squared_distance = {
					target = scope:ttd_location_2
					value >= ttd_squared_distance_small
				}
			}
			add = ttd_medium_distance_value
		}
		modifier = { # Perfer arrangements that don't involve family traveling long distances
			desc = TTD_DISTANCE_PENALTY_ACCEPTANCE_CLOSE_FAMILY_DESC
			scope:secondary_recipient = {
				OR = {
					is_close_family_of = scope:recipient
					ao_is_adoptive_close_family_of_character_trigger = { CHARACTER = scope:recipient }
				}
			}
			ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:actor SCOPE_NAME = ttd_location_1 }
			ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:recipient SCOPE_NAME = ttd_location_2 }
			scope:ttd_location_1 = {
				squared_distance = {
					target = scope:ttd_location_2
					value >= ttd_squared_distance_small
				}
			}
			add = ttd_medium_distance_value
		}
		modifier = { # Perfer arrangements that don't involve traveling long distances
			desc = TTD_DISTANCE_PENALTY_ACCEPTANCE_DESC
			ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:actor SCOPE_NAME = ttd_location_1 }
			ttd_check_residence_and_save_as_temporary_scope_trigger = { CHARACTER = scope:recipient SCOPE_NAME = ttd_location_2 }
			scope:ttd_location_1 = {
				squared_distance = {
					target = scope:ttd_location_2
					value >= ttd_squared_distance_medium
				}
			}
			add = ttd_minor_distance_value
		}
	}
}

ttd_marriage_ai_accept_modifier = {
	ttd_marriage_distance_ai_accept_modifier = yes
	
	modifier = {
		add = 10
		scope:actor = { has_variable = ttd_far_off_host }
		scope:actor.var:ttd_far_off_host ?= scope:recipient
		desc = TTD_VISITING_FAR_OFF_REALM
	}
}