﻿
common_ruler_recruit_local_interaction = {
	category = interaction_category_diplomacy
	icon = guest
	common_interaction = yes

	desc = common_ruler_recruit_local_interaction_desc
	interface_priority = 20

	needs_recipient_to_open = no

	is_shown = {
		scope:actor = {
			is_ruler = yes
			is_landless_adventurer = no
		}
		scope:secondary_actor ?= {
			is_adult = yes
			OR = {
				this = scope:actor
				is_courtier_of = scope:actor
			}
			location = scope:actor.location
		}
	}

	is_valid_showing_failures_only = {
		custom_tooltip = {
			text = rcrl_you_dont_have_chancellor_tt
			exists = scope:actor.cp:councillor_chancellor
		}
		scope:secondary_actor = {
			has_short_disease_trigger = no
		}
	}

	send_option = {
		is_valid = {
			scope:actor = {
				has_usable_hook = scope:recipient
			}
		}
		flag = hook
		localization = SCHEME_HOOK
	}

	send_option = {
		is_valid = {
			scope:actor = {
				gold >= 20
			}
			NOR = {
				scope:secondary_recipient ?= { has_trait = generous }
				scope:secondary_recipient ?= { has_trait = humble }
			}
		}
		flag = gold
		localization = USE_GOLD
	}

	send_options_exclusive = no

	redirect = {
		if = {
			limit = {
				scope:recipient = {
					this = scope:actor
				}
			}
			scope:actor = {
				save_scope_as = secondary_actor
			}
		}
		else_if = {
			limit = {
				scope:recipient = {
					is_courtier_of = scope:actor
				}	
			}
			scope:recipient = {
				save_scope_as = secondary_actor
			}
		}

		random_pool_character = {
			province = scope:actor.location
			limit = {
				is_available_adult = yes
			}
			save_scope_as = secondary_recipient
		}
		scope:secondary_recipient ?= {
			location.county.holder = {
				save_scope_as = recipient
			}
		}
		if = {
			limit = {
				scope:recipient = {
					OR = {
						this = scope:actor
						is_vassal_or_below_of = scope:actor
					}
				}
			}
			scope:actor.cp:councillor_chancellor ?= {
				save_scope_as = recipient
			}
		}

		clear_saved_scope = secondary_recipient
		
	}

	cooldown = { months = adventurer_recruit_local_interaction_major_cooldown }

	populate_actor_list = {
		scope:actor = {
			every_courtier = {
				limit = {
					basic_is_available_ai = yes
					has_short_disease_trigger = no
					location = scope:actor.location
				}
				add_to_list = characters
			}
		}
		scope:actor = {
			if = {
				limit = {
					is_alive = yes
					is_imprisoned = no
					is_incapable = no
					has_short_disease_trigger = no
				}
				add_to_list = characters
			}
		}
	}

	populate_recipient_list = {
		every_pool_character = {
			province = scope:actor.location
			limit = {
				is_available_adult = yes
			}
			add_to_list = characters
		}
	}

	cost = {
		prestige = { 
			value = 100
			if = {
				limit = {
					scope:secondary_recipient ?= {
						is_lowborn = no
					}
				}
				add = 100
			}
		}
		piety = {
			value = 0
			if = {
				limit = {
					scope:secondary_recipient ?= {
						faith = {
							faith_hostility_level = {
								target = scope:actor.faith
								value > 2
							}
						}
					}
				}
				add = 50
				if = {
					limit = {
						scope:secondary_recipient ?= {
							is_lowborn = no
						}
					}
					add = 50
				}
			}
			
		}
	}

	on_accept = {
		rcrl_recruit_local_interaction_on_accept_effect = yes
		if = {
			limit = {
				scope:secondary_recipient ?= {
					is_lowborn = yes
				}
				scope:actor = {
					is_ruler = yes
					is_landless_ruler = no
				}
			}
			scope:actor = {
				custom_tooltip = rcrl_courtly_vassal_disapproves_recruit_lowborn
				hidden_effect = {
					every_vassal = {
						limit = {
							has_vassal_stance = courtly
						}
						add_opinion = {
							target = scope:actor
							modifier = rcrl_courtly_lowborn_recruit_opinion
						}
					}
				}
			}
		}
		if = {
			limit = {
				scope:hook = yes
			}
			scope:actor = {
				use_hook = scope:recipient
			}
			scope:secondary_recipient ?= {
				add_opinion = {
					modifier = demanded_recruitment
					target = scope:actor
				}
				add_opinion = {
					modifier = demanded_recruitment
					target = scope:recipient
					opinion = -10
				}
			}
		}
		else = {
			scope:secondary_recipient ?= {
				add_opinion = {
					modifier = kindness_opinion
					target = scope:secondary_actor
					opinion = 5
				}
			}
			scope:secondary_actor = {
				add_opinion = {
					modifier = friendliness_opinion
					target = scope:secondary_recipient
					opinion = 5
				}
				if = {
					limit = {
						can_set_relation_potential_friend_trigger = { CHARACTER = scope:secondary_recipient }
					}
					set_relation_potential_friend = scope:secondary_recipient
					scope:secondary_recipient = {
						show_as_tooltip = {
							set_relation_potential_friend = scope:secondary_actor
						}
					}
				}
			}
		}
		if = {
			limit = {
				scope:gold = yes
			}
			scope:actor = {
				pay_short_term_gold = {
					target = scope:secondary_recipient
					gold = 20
				}
			}
			scope:secondary_recipient ?= {
				add_opinion = {
					modifier = gift_opinion
					target = scope:secondary_actor
					opinion = 5
				}
			}
		}
	}
	
	auto_accept = no

	ai_accept = {
 		base = -50

		modifier = {
			scope:hook = yes
			add = 25
			desc = SCHEME_HOOK_USED
		}

		modifier = {
			add = {
				value = scope:secondary_actor.diplomacy
			}
			desc = rcrl_recruit_local_people_sway_my_diplomacy_reason
		}

 		opinion_modifier = {
			trigger = {
				scope:recipient = {
					opinion = {
						value > 0
						target = scope:actor
					}
				}
			}
 			opinion_target = scope:actor
 			multiplier = 0.25
 		}

		opinion_modifier = {
			trigger = {
				scope:recipient = {
					opinion = {
						value < 0
						target = scope:actor
					}
				}
			}
 			opinion_target = scope:actor
 			multiplier = 0.5
 		}

		opinion_modifier = {
			trigger = {
				exists = scope:secondary_recipient
			}
			who = scope:secondary_recipient
			opinion_target = scope:secondary_actor
			multiplier = 0.5
		}

 		#Relations & relatives (with actor)
 		modifier = {
			scope:secondary_recipient ?= {	
 				has_relation_lover = scope:actor
			}
			add = 100
 			desc = RCRL_AI_YOUR_LOVER
 		}

 		modifier = {
			scope:secondary_recipient ?= {
 				has_relation_friend = scope:actor
			}
 			add = 75
 			desc = RCRL_AI_YOUR_FRIEND
 		}

 		modifier = {
			scope:secondary_recipient ?= {
	 			is_child_of = scope:actor
			}
			add = 160
 			desc = RCRL_AI_YOUR_CHILD
 		}

 		modifier = {
			scope:secondary_recipient ?= {
 				this.dynasty.dynast = scope:actor
			}
 			add = 30
 			desc = RCRL_AI_YOU_ARE_THE_DYNAST
 		}

 		modifier = {
			scope:secondary_recipient ?= {
 				this.house.house_head = scope:actor
			}
 			add = 60
 			desc = RCRL_AI_YOU_ARE_THE_HOUSE_HEAD
 		}
		
		modifier = {
			scope:secondary_recipient ?= {
 				NOT = { is_child_of = scope:actor }
 				NOT = { is_close_family_of = scope:actor }
				is_extended_family_of = scope:actor
			}
 			add = 30
 			desc = RCRL_AI_YOUR_RELATIVE
 		}

 		modifier = {
			scope:secondary_recipient ?= {
 				NOT = { is_child_of = scope:actor }
 				is_close_family_of = scope:actor
			}
 			add = 75
 			desc = RCRL_AI_YOUR_RELATIVE
 		}

 		modifier = {
			scope:secondary_recipient ?= {
 				is_spouse_of = scope:actor
			}
 			add = 160
 			desc = RCRL_AI_YOUR_SPOUSE
 		}

 		#Family (at location)
 		modifier = {
 			exists = location
			scope:secondary_recipient ?= {
				any_consort_not_in_traveling_family_trigger = yes
			}
 			desc = AI_LEAVING_MY_SPOUSE
 			add = -100
 		}

 		modifier = {
 			exists = location
			scope:secondary_recipient ?= {
	 			any_child_not_in_traveling_family_trigger = yes
			}
			desc = AI_LEAVING_MY_CHILD
 			add = -100
 		}

 		modifier = {
 			add = 500
 			desc = AI_WANDERLUST
			is_child_of = scope:actor
 			has_character_modifier = lust_for_adventure
 		}

		
		compatibility_modifier = {
			trigger = {
				exists = scope:secondary_recipient
			}
			who = scope:secondary_recipient
			compatibility_target = scope:secondary_actor
			min = -50
			max = 40
			multiplier = 1.5
		}

		#Wandering characters (who aren't doing anything else)
		modifier = {
 			add = {
				value = 10
				
				if = {
					limit = {
						scope:secondary_recipient ?= {
							faith = scope:actor.faith
						}
					}
					add = 10
				}
				else_if = {
					limit = {
						scope:secondary_recipient ?= {
							faith = { #Same religion - But faith should not be considered Hostile or Evil
								religion = scope:actor.faith.religion
								faith_hostility_level = {
									target = scope:actor.faith
									value < 2
								}
							}
						}
					}
					add = 5
				}
				if = {
					limit = {
						scope:secondary_recipient ?= {
							culture = scope:actor.culture
						}
					}
					add = 10
				}
				else_if = {
					limit = {
						scope:secondary_recipient ?= {
							culture = {
								has_same_culture_heritage = scope:actor.culture
							}
						}
					}
					add = 5
				}
				if = {
					limit = {
						scope:secondary_recipient ?= {
							has_trait = trusting
						}
					}
					add = 10
				}
				if = {
					limit = {
						scope:secondary_recipient ?= {
							OR = {
								has_trait = lazy
								has_trait = shy
							}
						}
					}
					add = -10
				}
			}
 			desc = AI_CAN_BE_PERSUADED_TO_STAY
			
			NOT = { has_relation_rival = scope:actor }
 		}

		modifier = {
			trigger = {
				exists = scope:secondary_recipient
			}
			scope:actor = {
				tier_difference = {
					target = scope:secondary_recipient
					value >= 1
				}
			}
			add = {
				value = 10
				if = {
					limit = {
						scope:actor = {
							tier_difference = {
								target = scope:secondary_recipient
								value = 2
							}
						}
					}
					add = 10
				}
				if = {
					limit = {
						scope:actor = {
							tier_difference = {
								target = scope:secondary_recipient
								value = 3
							}
						}
					}
					add = 20
				}
				if = {
					limit = {
						scope:actor = {
							tier_difference = {
								target = scope:secondary_recipient
								value = 4
							}
						}
					}
					add = 30
				}
				if = {
					limit = {
						scope:actor = {
							tier_difference = {
								target = scope:secondary_recipient
								value = 5
							}
						}
					}
					add = 40
				}
			}
			desc = RANK_DIFFERENCE_REASON
		}

		modifier = {
			add = {
				value = 20
				if = {
					limit = {
						scope:secondary_recipient = {
							has_trait = greedy
						}
					}
					add = 20
				}
			}
			scope:gold = yes
			desc = USING_GOLD
		}

	}
	
	ai_targets = { ai_recipients = self }
	ai_frequency = 240
	
	ai_potential = {
		is_landless_adventurer = yes
	}
	
	ai_will_do = {
		base = 0
	}
}
