﻿namespace = courtier_guest_management

#COURTIERS/GUESTS LEAVING
# 0001 - Courtier is planning to leave
# 0102 - Courtier with claim is wanting to leave
# 0500 - Guests leaves
# 0999 - Maintenance - Force move to pool

#GUESTS ARRIVING
# 1001 - Guest arriving, message if they're great

#POOL CHARACTERS
# 2001 - "Develops" the character and gives those without location a location
# 2002 - Pool repopulation

#COURTIERS AND GUESTS DOING STUFF
# 3001 - Sets relationships and other secrets

# Player landless are now IMMUNE from divine intervention

scripted_trigger courtier_guest_management_1001_exceptional_claimant_trigger = {
	any_claim = { neighboring_useful_courtier_or_guest_claim_trigger = { RULER = scope:host } }
}

courtier_guest_management.1001 = {
	hidden = yes

	immediate = {
		scope:guest = {
			every_traveling_family_member = {
				add_to_temporary_list = guest_arrival_list
			}
		}
		
		# To update Adventure Inspiration to make sure that the target destination isn't in the realm they just arrived to. If it is we remove it and make them pick a new destination!
		every_in_list = {
			list = guest_arrival_list
			limit = {
				exists = inspiration
				inspiration = { has_inspiration_type = adventure_inspiration }
				exists = var:adventure_destination #They know where they want to go
			}
			get_adventure_inspiration_start_location = { ADVENTURER = this }
			if = {
				limit = {
					scope:host = {
						any_sub_realm_county = {
							this = scope:location.county
						}
					}
				}
				remove_variable = adventure_destination
				if = {
					limit = {
						adventure_inspiration_average_skill_value >= high_inspiration_skill
					}
					set_adventure_location_effect = {
						BASE_SCOPE = this
						NO_DESTINATION = yes
					}
				}
				else = {
					set_adventure_location_effect = {
						BASE_SCOPE = this
						NO_DESTINATION = no
					}
				}
			}
		}

		###DEVELOPMENT EFFECT###
		#Because the characters spend so little time in the pool, we also run it now

		every_in_list = {
			list = guest_arrival_list
			limit = { is_ai = yes is_adult = yes }
			pool_character_development_effect = yes
		}

		###EXCEPTIONAL GUEST TOAST###
		if = {
			limit = { scope:host = { is_ai = no } } #This is just interface messages, AI doesn't care

			every_in_list = {
				list = guest_arrival_list
				limit = {
					courtier_guest_management_1001_exceptional_claimant_trigger = yes
				}
				save_scope_as = exceptional_guest

				#CLAIMANT MESSAGE
				if = {
					limit = { courtier_guest_management_1001_exceptional_claimant_trigger = yes }
					random_claim = {
						limit = {
							neighboring_useful_courtier_or_guest_claim_trigger = { RULER = scope:host }
							tier = tier_kingdom
							scope:host.highest_held_title_tier > tier_kingdom
						}
						alternative_limit = {
							neighboring_useful_courtier_or_guest_claim_trigger = { RULER = scope:host }
							tier = tier_duchy
							scope:host.highest_held_title_tier > tier_duchy
						}
						alternative_limit = {
							neighboring_useful_courtier_or_guest_claim_trigger = { RULER = scope:host }
							tier = tier_county
							scope:host.highest_held_title_tier > tier_county
						}
						save_scope_as = guest_claim
					}
					scope:host = {
						send_interface_toast = {
							type = msg_notable_guest
							left_icon = scope:exceptional_guest
							right_icon = scope:guest_claim
							title = courtier_guest_management.1001.t
							desc = courtier_guest_management.1001.claimant.desc
						}
					}
				}
			}
		}
	}
}

###################
# POOL CHARACTERS #
###################

#This events gives pool characters relations, skills, traits and secrets. If they lack a location, they're given a location.
courtier_guest_management.2001 = {
	hidden = yes
	trigger = {
		is_pool_character = yes # Can cease to be in the pool between it queuing and executing
	}

	immediate = {
		#Am I lost in the void?
		if = {
			limit = { NOT = { exists = location } }

			#Oops, no good! Go someplace. (family is also moved with this effect)
			fallback_move_to_pool_effect = yes
		}

		#Don't I have the "last_visited_ruler" variable? (= it ran out or I'm from some weird source that doesn't set var)
		if = {
			limit = { NOT = { exists = var:last_visited_ruler } }

			if = { # If character is prune-able, prune.
				limit = {
					pool_character_is_pruneable_trigger = yes
				}
				if = {
					limit = { age < 65 }
					death = { death_reason = death_accident }
				}
				else = {
					death = { death_reason = death_old_age }
				}
			}
			else = { # Otherwise, go somewhere else.
				fallback_move_to_pool_effect = yes
			}
		}

		if = {
			limit = { is_alive = yes } #Because we might just have killed you and then 🎵nothing else matters🎵

			#Develop
			if = {
				limit = { is_adult = yes is_ai = yes }
				pool_character_development_effect = yes
			}

			#Is my pool full? Then move
			if = {
				limit = {
				    is_ai = yes
					location = {
						exists = province_owner
						local_pool_is_full_trigger = yes
					}
				}

				location.province_owner = {
					random_neighboring_and_across_water_top_liege_realm_owner = {
						limit = {
							exists = capital_province
							capital_province = { local_pool_is_full_trigger = no }
						}
						capital_province = { save_scope_as = destination }
					}
				}

				if = {
					limit = { exists = scope:destination }
					every_traveling_family_member = {
						limit = { NOT = { is_in_pool_at = scope:destination } }
						move_to_pool_at = scope:destination
					}
				}
				else = {
					fallback_move_to_pool_effect = yes
				}
			}
		}

	}
}


