﻿# Interactions relating to the education of children


# Designate guardian and ward in your own court
educate_child_interaction = {
	category = interaction_category_friendly
	common_interaction = yes
	icon = child

	send_name = SEND_PROPOSAL
	desc = educate_child_interaction_desc

	# secondary_actor = guardian
	# actor = guardian.liege - can be same as guardian
	# secondary_recipient = ward
	# recipient = ward.liege - can be same as ward

	redirect = {
		if = {
			limit = {
				scope:actor = { is_ai = no }
			}
			scope:actor = {
				#If the character performing the action is a child, let's set ourselves as the ward
				if = {
					limit = {
						is_adult = no
						is_physically_able = yes
						num_of_relation_guardian = 0
					}
					save_scope_as = secondary_recipient
					## modif
					if = {
						limit = {
							any_courtier = {
								is_physically_able_adult = yes
								num_of_relation_ward < 2
							}
						}
						ordered_courtier = {
							limit = {
								is_physically_able_adult = yes
								num_of_relation_ward < 2
							}
							order_by = AuGu_check_weight_AI_value   
							max = 1
							save_scope_as = secondary_actor
						}
					}
					## modif
				}
				#If the character peforming the action is an adult, let's set ourselves as the guardian
				else_if = {
					limit = {
						is_physically_able_adult = yes
						num_of_relation_ward < 2
						scope:recipient != scope:secondary_recipient
					}
					save_scope_as = secondary_actor
				}
				#Otherwise just pick whoever chum for the guardian position
				else_if = {
					limit = {
						any_courtier = {
							is_physically_able_adult = yes
							num_of_relation_ward < 2
						}
					}
					## modif
					ordered_courtier = {
						limit = {
							is_physically_able_adult = yes
							num_of_relation_ward < 2
						}
						order_by = AuGu_check_weight_AI_value
						max = 1
						## modif
						save_scope_as = secondary_actor
					}
				}
				if = {
					limit = {
						exists = employer
						employer != scope:actor
					}

					employer = {
						save_scope_as = actor
					}
				}
			}

			scope:recipient = {
				if = {
					limit = {
						is_physically_able_adult = yes
						num_of_relation_ward < 2
					}
					save_scope_as = secondary_actor
				}
				else = {
					save_scope_as = secondary_recipient
				}

				if = {
					limit = {
						exists = employer
						employer != scope:recipient
					}

					employer = {
						save_scope_as = recipient
					}
				}
			}
		}
	}

	populate_actor_list = {
		scope:actor = {
			if = {
				limit = {
					is_physically_able_adult = yes
					num_of_relation_ward < 2
				}
				add_to_list = characters
			}
			every_courtier = {
				limit = {
					is_physically_able_adult = yes
					num_of_relation_ward < 2
				}
				add_to_list = characters
			}
		}
	}

	populate_recipient_list = {
		scope:recipient = {
			if = {
				limit = {
					is_adult = no
					is_physically_able = yes
					num_of_relation_guardian = 0
					trigger_if = {
						limit = {
							scope:actor = {
								is_landless_adventurer = no
							}
						}
						is_available = yes
					}
					trigger_else = {
						is_available_allow_travelling = yes
					}
					trigger_if = { #If a hostage then only if scope:actor is the hostage taker
						limit = {
							is_hostage = yes
						}
						is_hostage_of = scope:actor
					}
				}
				add_to_list = characters
			}
			every_courtier = {
				limit = {
					is_available_quick = {
						adult = no
						alive = yes
						imprisoned = no
						incapable = no
					}
					num_of_relation_guardian = 0
					trigger_if = {
						limit = {
							scope:actor = {
								is_landless_adventurer = no
							}
						}
						is_available = yes
					}
					trigger_else = {
						is_available_allow_travelling = yes
					}
					trigger_if = {
						limit = {
							scope:actor = { is_ai = yes }
						}
						is_close_family_of = scope:actor
					}
					trigger_if = { #If a hostage then only if scope:actor is the hostage taker
						limit = {
							is_hostage = yes
						}
						is_hostage_of = scope:actor
					}
				}
				add_to_list = characters
			}
		}
	}

	greeting = positive
	notification_text = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:actor = scope:secondary_actor
					scope:recipient != scope:secondary_recipient
				}
				desc = EDUCATE_RECIPIENT_CHILD_PERSONALLY_REQUEST
			}
			triggered_desc = {
				trigger = {
					scope:actor = scope:secondary_actor
					scope:recipient = scope:secondary_recipient
				}
				desc = EDUCATE_RECIPIENT_PERSONALLY_REQUEST
			}
			triggered_desc = {
				trigger = {
					scope:recipient != scope:secondary_recipient
					scope:actor != scope:secondary_actor
				}
				desc = EDUCATE_RECIPIENT_CHILD_REQUEST
			}
			triggered_desc = {
				trigger = {
					scope:recipient = scope:secondary_recipient
					scope:actor != scope:secondary_actor
				}
				desc = EDUCATE_RECIPIENT_REQUEST
			}
		}
	}

	prompt = {
		first_valid = {
			triggered_desc = {
				trigger = {
					NOT = { exists = scope:secondary_actor }
				}
				desc = EDUCATE_SELECT_YOUR_GUARDIAN
			}
			triggered_desc = {
					trigger = {
						NOT = { exists = scope:secondary_recipient }
					}
				desc = EDUCATE_SELECT_YOUR_WARD
			}
		}
	}

	is_shown = {
		scope:recipient = {
			is_in_the_same_court_as = scope:actor
		}
		trigger_if = {
			limit = {
				exists = scope:secondary_actor
			}
			scope:secondary_actor = {
				is_adult = yes
				num_of_relation_ward < 2
			}
		}
		trigger_if = {
			limit = {
				exists = scope:secondary_recipient
			}
			scope:secondary_recipient = {
				is_adult = no
				num_of_relation_guardian = 0
			}
		}
	}

	is_valid_showing_failures_only = {
		scope:recipient = {
			is_imprisoned = no
			NOT = { has_trait = incapable }
			trigger_if = {
				limit = {
					this != scope:actor
				}
				is_courtier_of = scope:actor
			}

		}
		trigger_if = {
			limit = {
				exists = scope:secondary_actor
			}
			scope:secondary_actor = {
				is_imprisoned = no
				NOT = { has_trait = incapable }
			}
		}
		trigger_if = {
			limit = {
				exists = scope:secondary_recipient
			}
			scope:secondary_recipient = {
				is_imprisoned = no
				trigger_if = {
					limit = {
						scope:actor = {
							is_landless_adventurer = no
						}
					}
					is_travelling = no #The child in question should not already have a travel plan
				}
				NOT = { has_trait = incapable }
				NOT = { exists = var:hostage_travelling_to_warden }
				trigger_if = {
					limit = { is_hostage = yes }
					is_courtier_of = scope:actor
				}
			}
		}
		custom_description = {
			text = character_to_guard_exists
			subject = scope:actor
			scope:actor = {
				OR = {
					any_courtier = {
						is_adult = yes
						num_of_relation_ward < 2
						is_imprisoned = no
						NOT = { has_trait = incapable }
					}
					AND = {
						is_adult = yes
						num_of_relation_ward < 2
						is_imprisoned = no
						NOT = { has_trait = incapable }
					}
				}
			}
		}
		custom_description = {
			text = character_to_educate_exists
			object = scope:actor
			scope:actor = {
				OR = {
					any_courtier = {
						is_adult = no
						num_of_relation_guardian = 0
						is_imprisoned = no
						NOT = { has_trait = incapable }
					}
					AND = {
						is_adult = no
						num_of_relation_guardian = 0
						is_imprisoned = no
						NOT = { has_trait = incapable }
					}
				}
			}
		}
		trigger_if = { # Ward
			limit = { exists = scope:secondary_recipient }
			scope:secondary_recipient = {
				custom_tooltip = {
					text = ward_travelling_tt
					NOT = { exists = var:ward_travelling_to_guardian }
				}
				custom_tooltip = {
					text = hostage_travelling_tt
					NOT = { exists = var:hostage_travelling_to_warden }
				}
				# Structured this way for tooltip purposes
				trigger_if = {
					limit = { exists = var:ward_waiting_for_guardian }
					var:ward_waiting_for_guardian = {
						custom_tooltip = {
							text = guardian_travelling_tt
							NOT = { exists = scope:secondary_recipient.var:ward_waiting_for_guardian }
						}
					}
				}
			}
		}
		trigger_if = { # Guardian
			limit = { exists = scope:secondary_actor }
			scope:secondary_actor = {
				custom_tooltip = {
					text = guardian_travelling_tt
					NOT = { exists = var:guardian_travelling_to_ward }
				}
				custom_tooltip = {
					text = hostage_travelling_tt
					NOT = { exists = var:hostage_travelling_to_warden }
				}
				# Structured this way for tooltip purposes
				trigger_if = {
					limit = { exists = var:guardian_waiting_for_ward }
					var:guardian_waiting_for_ward = {
						custom_tooltip = {
							text = ward_travelling_tt
							NOT = { scope:secondary_actor.num_of_relation_ward >= 1 }
						}
					}
				}
			}
		}
		#limit range for landless adventurers
		trigger_if = {
			limit = {
				scope:actor = {
					has_government = landless_adventurer_government
				}
			}
			#Has to be used instead of diplo range checks in laamp to landed interactions
			ep3_laamp_diplo_range_trigger = {
				TARGET = scope:recipient
				LAAMP = scope:actor
			}
		}
	}

	can_be_picked = { #Potential Issue: This is only evaluated if both secondary recipient/actor are unselected TIT-49632
		trigger_if = {
			limit = {
				OR = {
					AND = {
						is_ruler = no
						liege ?= {
							is_landless_adventurer = no
						}
					}
					AND = {
						is_ruler = yes
						is_landless_adventurer = no
					}
				}
			}
			is_available = yes
		}
		trigger_else = {
			is_available_allow_travelling = yes
		}
		is_at_home = yes
		# Under offer
		custom_tooltip = {
			text = ward_already_offered_awaiting_response_tt
			NOT = { has_character_flag = under_offer_as_ward_flag }
		}
		custom_tooltip = {
			text = guardian_already_offered_awaiting_response_tt
			NOT = { has_character_flag = under_offer_as_guardian_flag }
		}
		# Already offered
		custom_tooltip = {
			text = ward_already_offered_awaiting_travel_tt
			NOR = {
				exists = var:ward_travelling_to_guardian
				exists = var:ward_waiting_for_guardian
			}
		}
		custom_tooltip = {
			text = guardian_already_offered_awaiting_travel_tt
			NOR = {
				exists = var:guardian_travelling_to_ward
				AND = {
					exists = var:guardian_waiting_for_ward
					any_relation = { type = ward }
				}
			}
		}
		# Hostage
		custom_tooltip = {
			text = hostage_travelling_tt
			NOT = { exists = var:hostage_travelling_to_warden }
		}
		# Guardian has too many students
		num_of_relation_ward <= 2
		trigger_if = {
			limit = { exists = var:character_requested_as_educator }
			num_of_relation_ward <= 1
		}
	}

	on_accept = {
		scope:secondary_recipient = {
			save_scope_as = ward
			scope:secondary_actor = { save_scope_as = guardian }
			scope:recipient = { save_scope_as = ward_liege }
			scope:actor = { save_scope_as = guardian_liege }
		}

		if = {
			limit = { scope:ward.age >= 15 }
			custom_description_no_bullet = {
				text = ward_nearing_adulthood_tt
				object = scope:ward
			}
		}

		scope:guardian_liege = {
			send_interface_message = {
				type = event_childhood_neutral
				title = educate_child_interaction_notification
				left_icon = scope:guardian
				right_icon = scope:ward
				educate_child_interaction_effect = yes
				show_as_tooltip = {
					scope:ward = {
						if = {
							limit = { exists = var:ward_travelling_to_guardian }
							custom_tooltip = ward_departs_tt
						}
						else_if = {
							limit = { exists = scope:guardian.var:guardian_travelling_to_ward }
							scope:guardian = { custom_tooltip = guardian_departs_tt }
						}
						else = {
							set_relation_guardian = scope:guardian
						}
					}
				}
			}
		}
		#to see the "becomes guardian of x" effect in the interaction confirmation window
		show_as_tooltip = {
			scope:guardian  = { set_relation_ward = scope:ward }
		}
	}

	auto_accept = yes

	ai_accept = {
		base = 100
	}

	send_option = {
		is_shown = {
			exists = scope:secondary_actor
			exists = scope:secondary_recipient
			scope:secondary_actor.culture != scope:secondary_recipient.culture
		}
		flag = convert_culture
		localization = EDUCATE_CHILD_OPTION_CONVERT_CULTURE
		current_description = {
			first_valid = {
				triggered_desc = {
					trigger = {
						exists = scope:secondary_actor
						scope:secondary_actor = { is_adult = no }
					}
					desc = EDUCATE_CHILD_OPTION_CONVERT_CULTURE_TT_TO_RECIPIENT
				}
				triggered_desc = {
					trigger = {
						exists = scope:secondary_recipient
						scope:secondary_recipient = { is_adult = no }
					}
					desc = EDUCATE_CHILD_OPTION_CONVERT_CULTURE_TT_TO_ACTOR
				}
			}
		}
	}
	send_option = {
		is_shown = {
			exists = scope:secondary_actor
			exists = scope:secondary_recipient
			scope:secondary_actor.faith != scope:secondary_recipient.faith
		}
		flag = convert_faith
		localization = EDUCATE_CHILD_OPTION_CONVERT_FAITH
		current_description = {
			first_valid = {
				triggered_desc = {
					trigger = {
						exists = scope:secondary_actor
						scope:secondary_actor = { is_adult = no }
					}
					desc = EDUCATE_CHILD_OPTION_CONVERT_FAITH_TT_TO_RECIPIENT
				}
				triggered_desc = {
					trigger = {
						exists = scope:secondary_recipient
						scope:secondary_recipient = { is_adult = no }
					}
					desc = EDUCATE_CHILD_OPTION_CONVERT_FAITH_TT_TO_ACTOR
				}
			}
		}
	}
	send_option = {
		is_shown = {
			scope:recipient = {
				custom_tooltip = {
					text = activity_education_no_culture_center #needed for error supression
					OR = {
						any_realm_province = {
							is_higher_studies_location = yes
						}
						any_realm_county = {
							has_county_modifier = pandidakterion_university_modifier
						}
					}
				}
			}
		}
		is_valid = {
			scope:actor.gold >= scope:actor.medium_gold_value
		}
		flag = send_to_university
		localization = EDUCATION_UNIVERSITY
	}
	send_option = {
		is_shown = {
			exists = scope:actor
			exists = scope:secondary_actor
			scope:secondary_actor != scope:actor
		}
		is_valid = {
			scope:actor = {
				has_usable_hook = scope:secondary_actor
			}
		}
		flag = education_hook
		localization = EDUCATION_HOOK
	}

	should_use_extra_icon = {
		scope:actor = { has_usable_hook = scope:recipient }
	}
	extra_icon = "gfx/interface/icons/character_interactions/hook_icon.dds"

	send_options_exclusive = no

	ai_targets = {
		ai_recipients = self
	}

	ai_frequency_by_tier = {
		barony = 60
		county = 36
		duchy = 12
		kingdom = 12
		empire = 12
		hegemony = 12
	}

	ai_potential = {
		OR = {
			AND = {
				is_adult = no
				is_physically_able = yes
				num_of_relation_guardian = 0
			}
			any_courtier = {
				is_adult = no
				is_physically_able = yes
				num_of_relation_guardian = 0
				is_close_family_of = root
			}
		}
	}

	ai_will_do = {
		base = 100

		modifier = { # Slight preference for older heirs
			add = scope:secondary_recipient.age
		}

		modifier = { # Prefer to educate your own heirs
			add = 900
			scope:actor = scope:secondary_actor
			 scope:actor = { ai_sociability > low_positive_ai_value }
			scope:secondary_recipient = {
				is_heir_of = scope:actor
			}
			
		}

		## modif
		AuGu_AI_select_guardian_modifier = {
			GUARDIAN = scope:secondary_actor
			WARD = scope:secondary_recipient
			LIEGE = scope:actor
		}
		## modif

		modifier = { # Generally prefer your own flesh & blood to educate children
			add = 50
			exists = scope:secondary_actor.dynasty
			exists = scope:actor.dynasty
			scope:secondary_actor.dynasty = scope:actor.dynasty
		}

		modifier = { # Also prefer friends & lovers
			add = 50
			scope:secondary_actor = {
				OR = {
					has_relation_lover = scope:actor
					has_relation_friend = scope:actor
					has_secret_relation_lover = scope:actor
					has_relation_soulmate = scope:actor
					has_relation_best_friend = scope:actor
				}
			}
		}

		modifier = { # The AI shouldn't change culture/faith of their children
			add = -1000
				scope:secondary_recipient.culture = scope:actor.culture
				scope:secondary_recipient.faith = scope:actor.faith
				OR = {
					scope:convert_culture = yes
					scope:convert_faith = yes
				}
		}

		modifier = { # The AI should not convert child of vassals with protected religious right
			add = -1000
			scope:convert_faith = yes
			scope:recipient = { is_vassal_of = scope:actor }
			scope:recipient = { vassal_contract_has_flag = religiously_protected }
		}

		modifier = { # If your kids aren't of your culture/faith, make them so!
			add = 900
			scope:secondary_actor.culture = scope:actor.culture
			scope:secondary_actor.faith = scope:actor.faith
			NOR = {
				scope:secondary_recipient.culture = scope:actor.culture
				scope:secondary_recipient.faith = scope:actor.faith
			}
			scope:convert_culture = yes
			scope:convert_faith = yes
		}

		modifier = { # If your kids aren't of your culture/faith, make them so!
			add = 500
			scope:secondary_actor.culture = scope:actor.culture
			scope:secondary_recipient.culture != scope:actor.culture
			scope:convert_culture = yes
		}

		modifier = { # If your kids aren't of your culture/faith, make them so!
			add = 500
			scope:secondary_actor.faith = scope:actor.faith
			scope:secondary_recipient.faith != scope:actor.faith
			scope:convert_faith = yes
		}

		modifier = { # Rivals are not welcome
			add = -1000
			scope:secondary_recipient = {
				OR = {
					has_relation_rival = scope:actor
					has_relation_nemesis = scope:actor
				}
			}
		}

		modifier = { # Random peasants can only dream about educating noble children!
			add = -1000
			scope:actor = {
				is_lowborn = no
			}
			scope:secondary_recipient = {
				is_lowborn = yes
				NOR = {
					is_councillor = yes
					is_knight = yes
				}
			}
		}

		modifier = { # Don't care about random children
			factor = 0
			scope:secondary_recipient = {
				NOR = {
					is_close_family_of = scope:actor
					this = scope:actor
				}
			}
		}

		modifier = { # Don't spend gold on this unless very, very rich
			factor = 0
			scope:send_to_university = yes

			scope:actor.short_term_gold < scope:actor.monumental_gold_value
		}
	}
}

