﻿travl_travel_on_departure = {
	trigger = {
		has_variable = travl_travel_sender
		has_variable = travl_travel_receiver
	}

	effect = {
		var:travl_travel_sender ?= { save_temporary_scope_as = sender }
		var:travl_travel_receiver ?= { save_temporary_scope_as = receiver }
		var:travl_travel_leader ?= { save_temporary_scope_as = leader }
		scope:leader ?= {
			if = {
				limit = { is_alive = yes }
				travl_get_companions_effect = { COMPANIONS = companions }
			}
		}

		# Ensure all characters are at the current location
		current_travel_plan ?= {
			current_location = { save_temporary_scope_as = current_location }
		}
		if = {
			limit = { exists = scope:current_location }
			travl_ensure_all_locations_effect = { LOCATION = scope:current_location }
		}

		if = {
			limit = { scope:leader ?= this }

			# Get all travelers (leader, companions, and their entourages)
			travl_get_travelers_effect = {
				LEADER = scope:leader
				COMPANIONS = companions
				TRAVELERS = travelers
			}

			# Filter only new travelers
			every_in_list = {
				list = travelers
				limit = { NOT = { has_variable = travl_departure_message_sent } }
				add_to_temporary_list = new_travelers
			}

			# Get message recipients (sender and / or recieiver)
			travl_get_message_recipients_effect = {
				SENDER = scope:sender
				RECEIVER = scope:receiver
				RECIPIENTS = recipients
			}

			# Send departure messages
			travl_send_messages_effect = {
				RECIPIENTS = recipients
				TRAVELERS = new_travelers
				TITLE = travl_msg_departed_for_court
				TEXT = travl_departed_for_court_tt
				TEXT_ADDED_CHARACTER = travl_departed_for_court_joined_tt
				OBJECT = scope:receiver
			}

			# Send departure messages to players hostile to a prison guard receiver
			if = {
				limit = {
					has_character_modifier = travl_prison_guard_modifier
					NOT = { has_variable = travl_departure_message_sent }
				}
				save_temporary_scope_as = prison_guard
				every_player = {
					limit = {
						travl_is_hostile_character_trigger = { CHARACTER = scope:receiver }
					}
					travl_send_message_effect = {
						CHARACTER = scope:prison_guard
						TRAVELERS = new_travelers
						TITLE = travl_msg_prison_guard_departed
						TEXT = travl_departed_for_court_tt
						TEXT_ADDED_CHARACTER = travl_prison_guard_departed_tt
						OBJECT = scope:receiver
					}
				}
			}

			# Mark new travelers to avoid second messages
			every_in_list = {
				list = new_travelers
				set_variable = { name = travl_departure_message_sent value = yes days = 2 }
			}
		}

		# Log travelers departures
		if = {
			limit = { debug_only = yes }
			if = {
				limit = { NOT = { has_variable = travl_departure_logged } }
				if = {
					limit = { scope:leader ?= this }
					travl_debug_log_effect = { MSG = travl_debug_msg_character_departed_leader }
				}
				else = {
					travl_debug_log_effect = { MSG = travl_debug_msg_character_departed_companion }
				}
				set_variable = { name = travl_departure_logged value = yes days = 2 }
			}
			current_travel_plan ?= {
				travel_plan_owner = { save_temporary_scope_as = owner }
				every_entourage_character = {
					limit = { NOT = { has_variable = travl_departure_logged } }
					travl_debug_log_2_effect = {
						MSG = travl_debug_msg_character_departed_entourage
						NAME1 = receiver TYPE1 = flag:character
						NAME2 = owner TYPE2 = flag:character
					}
					set_variable = { name = travl_departure_logged value = yes days = 2 }
				}
			}
		}

		# Trigger travl_travel_on_departure for a second time
		# to inform about travelers joining on the next day
		if = {
			limit = { NOT = { has_variable = travl_second_on_departure_fired } }
			trigger_event = {
				on_action = travl_travel_on_departure
				delayed = yes
			}
			set_variable = { name = travl_second_on_departure_fired value = yes days = 2 }
		}
	}
}

travl_travel_on_arrival = {
	trigger = {
		has_variable = travl_travel_sender
		has_variable = travl_travel_receiver
	}

	effect = {
		var:travl_travel_sender ?= { save_temporary_scope_as = sender }
		var:travl_travel_receiver ?= { save_temporary_scope_as = receiver }
		var:travl_travel_leader ?= { save_temporary_scope_as = leader }
		scope:leader ?= {
			if = {
				limit = { is_alive = yes }
				travl_get_companions_effect = { COMPANIONS = companions }
			}
		}

		if = {
			limit = { scope:leader ?= this }

			# Ensure companions arrive as well
			every_in_list = {
				list = companions
				current_travel_plan ?= {
					add_destination_progress = { years = 2 }
				}
			}

			# Get all travelers (leader, companions, and their entourages)
			travl_get_travelers_effect = {
				LEADER = scope:leader
				COMPANIONS = companions
				TRAVELERS = travelers
			}

			# Get message recipients (sender and / or recieiver)
			travl_get_message_recipients_effect = {
				SENDER = scope:sender
				RECEIVER = scope:receiver
				RECIPIENTS = recipients
			}

			# Send arrival messages
			travl_send_messages_effect = {
				RECIPIENTS = recipients
				TRAVELERS = travelers
				TITLE = travl_msg_arrived_at_court
				TEXT = travl_arrived_at_court_tt
				TEXT_ADDED_CHARACTER = travl_arrived_at_court_joined_tt
				OBJECT = scope:receiver
			}
		}

		# Log travelers arrivals
		if = {
			limit = { debug_only = yes }
			if = {
				limit = { scope:leader ?= this }
				travl_debug_log_effect = { MSG = travl_debug_msg_character_arrived_leader }
			}
			else = {
				travl_debug_log_effect = { MSG = travl_debug_msg_character_arrived_companion }
			}
			current_travel_plan ?= {
				travel_plan_owner = { save_temporary_scope_as = owner }
				every_entourage_character = {
					travl_debug_log_2_effect = {
						MSG = travl_debug_msg_character_arrived_entourage
						NAME1 = receiver TYPE1 = flag:character
						NAME2 = owner TYPE2 = flag:character
					}
				}
			}
		}

		# CK3 1.19 sends imprisoned rulers back to their capital on arrival, so check and fix after arrival
		# Very rarely (~1%), they are sent back to their capital on the second day, could be fixed, but ok as is
		trigger_event = {
			on_action = travl_travel_on_1_day_after_arrival
			days = 1
		}

		# Maybe gain the traveler trait if adult and unplayable
		if = {
			limit = {
				is_adult = yes
				is_playable_character = no
				NOT = { has_trait = lifestyle_traveler }
			}
			random_list = {
				20 = { add_trait = lifestyle_traveler }
				80 = {}
			}
		}

		# Remove travel variables
		travl_remove_sender_and_receiver_effect = yes
		travl_remove_prisoner_flags_effect = yes
		travl_remove_leader_and_companions_effect = yes

		# Move added characters back to pool
		travl_move_added_characters_to_pool_effect = yes
	}
}

travl_travel_on_1_day_after_arrival = {
	effect = {
		if = {
			limit = {
				exists = default_location
				location != default_location
				is_travelling = no
				is_in_army = no
			}
			travl_debug_log_default_location_effect = { MSG = travl_debug_msg_setting_default_location_after_arrival }
			set_location = default_location
		}
	}
}

# Arrival in any province during travel
on_travel_plan_movement = {
	on_actions = { travl_on_travel_plan_movement travl_on_travel_plan_update_location }
}

travl_on_travel_plan_movement = {
	trigger = {
		has_variable = travl_travel_sender
		has_variable = travl_travel_receiver
	}

	effect = {
		#travl_debug_log_effect = { MSG = travl_debug_msg_travel_moved }

		var:travl_travel_sender ?= { save_temporary_scope_as = sender }
		var:travl_travel_receiver ?= { save_temporary_scope_as = receiver }
		var:travl_travel_leader ?= { save_temporary_scope_as = leader }
		scope:leader ?= {
			if = {
				limit = { is_alive = yes }
				travl_get_companions_effect = { COMPANIONS = companions }
			}
		}

		if = {
			limit = { scope:leader ?= this }

			# Get all travelers (leader, companions, and their entourages)
			travl_get_travelers_effect = {
				LEADER = scope:leader
				COMPANIONS = companions
				TRAVELERS = travelers
			}

			# If the receiver is no longer valid, trigger on_invalid_receiver event
			# If the destination is no longer valid, trigger on_invalid_destination event
			if = {
				limit = {
					scope:receiver ?= {
						NAND = {
							is_alive = yes
							exists = capital_province
						}
					}
				}
				every_in_list = {
					list = travelers
					trigger_event = {
						on_action = travl_on_invalid_receiver
					}
				}
			}
			else = {
				# Determine destination
				travl_set_destination_effect = { RECEIVER = scope:receiver }

				# Trigger travl_on_invalid_destination event for all travelers if needed
				if = {
					limit = {
						exists = scope:destination
						current_travel_plan ?= {
							NOT = { final_destination_province = scope:destination }
						}
					}
					every_in_list = {
						list = travelers
						trigger_event = {
							on_action = travl_on_invalid_destination
						}
					}
				}
			}
		}

		# Enable or disable prisoner interactions
		travl_enable_or_disable_prisoner_interactions_effect = yes
	}

	on_actions = {
		travl_travel_danger_events
		travl_prisoner_events
	}
}

travl_travel_danger_events = {
	random_events = {
		chance_of_no_event = {
			value = 100
			subtract = travl_travel_danger_value
		}
		100 = 0
		 50 = travl_travel_danger.0001 # Danger: Bandits
		200 = travl_travel_danger.0002 # Danger: Missing Companion
		100 = travl_travel_danger.0003 # Danger: Lost
		 50 = travl_travel_danger.0004 # Danger: Out of Water
		100 = travl_travel_danger.0005 # Danger: Aftermath
		 20 = travl_travel_danger.0006 # Danger: Sea Storm
		200 = travl_travel_danger.0007 # Danger: Avalanche
		 50 = travl_travel_danger.0008 # Danger: Snowbound
		200 = travl_travel_danger.0009 # Danger: Ripped Sails
		 50 = travl_travel_danger.0010 # Danger: Brush Fire
		 50 = travl_travel_danger.0011 # Danger: Mosquitoes
		100 = travl_travel_danger.0012 # Danger: Beast Attack
		 20 = travl_travel_danger.0013 # Danger: Spoiled Food
		200 = travl_travel_danger.0014 # Danger: Sickness
		500 = travl_travel_danger.0015 # Danger: Carrion
	}
}

travl_prisoner_events = {
	random_events = {
		chance_of_no_event = {
			value = 100
			subtract = travl_prisoner_event_chance_value
		}
		100 = 0
		100 = travl_prisoner.0001 # Escape from Prison
	}
	events = {
		travl_prisoner.0002 # Hostile Army
	}
}

# Starting a travel plan (starting to travel to the first destination)
on_travel_plan_start = {
	on_actions = { travl_on_travel_plan_update_location }
}

# Arrival in a destination province during travel (a travel plan can have multiple sequential destinations)
on_travel_plan_arrival = {
	on_actions = { travl_on_travel_plan_update_location }
}

# Completed a travel plan (arrived at final destination, or completed through script)
# A travel plan ends either via 'on_travel_plan_complete' or 'on_travel_plan_abort'
on_travel_plan_complete = {
	on_actions = { travl_on_travel_plan_update_location }
}

# Aborted a travel plan (travel plan was not completed, or aborted through script)
# A travel plan ends either via 'on_travel_plan_complete' or 'on_travel_plan_abort'
# This usually means everyone gets teleported to their default location after this.
on_travel_plan_abort = {
	on_actions = { travl_on_travel_plan_abort }
}

travl_on_travel_plan_abort = {
	trigger = {
		has_variable = travl_travel_sender
		has_variable = travl_travel_receiver
		NOT = { has_variable = travl_suppress_events }
	}

	effect = {
		travl_debug_log_effect = { MSG = travl_debug_msg_travel_aborted }

		# Remove travel variables
		travl_remove_sender_and_receiver_effect = yes
		travl_remove_prisoner_flags_effect = yes
		travl_remove_leader_or_companion_effect = yes

		# Move added characters back to pool
		travl_move_added_characters_to_pool_effect = yes
	}
}

# Cancelled a travel plan. Travel plan was cancelled by player interaction, and has
# been rerouted back home. (the travel plan doesn't end yet until they arrive home)
# People in the travel plan were removed from associated activities.
on_travel_plan_cancel = {
	on_actions = { travl_on_travel_plan_update_location }
}

travl_on_travel_plan_update_location = {
	effect = {
		travl_set_all_locations_effect = yes
	}
}

# If the travel leader gets removed (not replaced), this will trigger.
# Root - The owner of the travel plan
# scope:travel_plan - The travel plan
# scope:old_travel_leader - The now removed leader.
on_travel_leader_removed = {
	on_actions = { travl_on_travel_leader_removed }
}

travl_on_travel_leader_removed = {
	effect = {
		scope:old_travel_leader ?= {
			travl_move_added_character_to_pool_effect = yes
		}
	}
}

# Triggered for the new leader when the leader changed.
travl_on_leader_changed = {
	effect = {
		# Free traveling prisoners without a prison guard
		if = {
			limit = {
				is_imprisoned = yes
				scope:old_leader = {
					has_character_modifier = travl_prison_guard_modifier
					current_travel_plan ?= {
						is_aborted = yes
					}
				}
			}

			# Let prisoners escape and notify recipients
			travl_event_get_prisoners_effect = { PRISONERS = prisoners }
			travl_escape_from_prison_prisoners_effect = { PRISONERS = prisoners }
		}
	}
}

# Triggered for the leader when a companion was removed.
travl_on_companion_removed = {
	effect = {
		# Abort travel for prison guards without prisoners
		if = {
			limit = {
				has_character_modifier = travl_prison_guard_modifier
				any_in_list = {
					variable = travl_travel_companions
					is_alive = yes
					count = 0
				}
			}

			travl_debug_log_effect = { MSG = travl_debug_msg_aborting_travel_for_prison_guard }
			current_travel_plan ?= {
				abort_travel_plan = yes
			}
		}
	}
}
