﻿mentoring = {
	# Basic Setup
	skill = learning
	desc = "mentoring_desc_general"
	success_desc = "MENTORING_SCHEME_SUCCESS_DESC"
	icon =	mentor
	illustration = "gfx/interface/illustrations/event_scenes/tgp_examination_room.dds"
	category = personal
	target_type = character

	is_secret = no
	is_basic = yes

	# Parameters
	speed_per_skill_point = -2
	spymaster_speed_per_skill_point = 0	
	uses_resistance = no
	base_progress_goal = 300
	base_maximum_success = 95
	minimum_success = 10

	# Core Triggers
	allow = {
		is_adult = yes
		is_imprisoned = no
		NOT = { has_trait = incapable }
	}

	valid = {
		scope:owner = {
			is_imprisoned = no
			NOT = {
				has_character_modifier = partially_mute_modifier
			}
			NOT = { has_trait = incapable }
			NOT = { is_at_war_with = scope:target }
			# Here we check the valid condition depending on what you chose to study
			trigger_if = {
				limit = {
					exists = scope:scheme.var:mentoring_merit
				}
				merit_level < scope:target.merit_level
			}
			trigger_else_if = {
				limit = {
					exists = scope:scheme.var:mentoring_confucian_education
				}
				OR = {
					NOT = { has_trait = confucian_education }
					has_trait_xp = {
						trait = confucian_education
						value < "scope:target.has_trait_xp(confucian_education)"
					}
				}
			}
			trigger_else_if = {
				limit = {
					exists = scope:scheme.var:mentoring_logistician
				}
				OR = {
					NOT = { has_trait = logistician }
					has_trait_xp = {
						trait = logistician
						value < "scope:target.has_trait_xp(logistician)"
					}
				}
			}
			trigger_else_if = {
				limit = {
					exists = scope:scheme.var:mentoring_military_engineer
				}
				OR = {
					NOT = { has_trait = military_engineer }
					has_trait_xp = {
						trait = military_engineer
						value < "scope:target.has_trait_xp(military_engineer)"
					}
				}
			}
			trigger_else_if = {
				limit = {
					exists = scope:scheme.var:mentoring_aggressive_attacker
				}
				OR = {
					NOT = { has_trait = aggressive_attacker }
					has_trait_xp = {
						trait = aggressive_attacker
						value < "scope:target.has_trait_xp(aggressive_attacker)"
					}
				}
			}
			trigger_else_if = {
				limit = {
					exists = scope:scheme.var:mentoring_unyielding_defender
				}
				OR = {
					NOT = { has_trait = unyielding_defender }
					has_trait_xp = {
						trait = unyielding_defender
						value < "scope:target.has_trait_xp(unyielding_defender)"
					}
				}
			}
			trigger_else_if = {
				limit = {
					exists = scope:scheme.var:mentoring_forder
				}
				OR = {
					NOT = { has_trait = forder }
					has_trait_xp = {
						trait = forder
						value < "scope:target.has_trait_xp(forder)"
					}
				}
			}
			trigger_else_if = {
				limit = {
					exists = scope:scheme.var:mentoring_flexible_leader
				}
				OR = {
					NOT = { has_trait = flexible_leader }
					has_trait_xp = {
						trait = flexible_leader
						value < "scope:target.has_trait_xp(flexible_leader)"
					}
				}
			}
			trigger_else_if = {
				limit = {
					exists = scope:scheme.var:mentoring_desert_warrior
				}
				OR = {
					NOT = { has_trait = desert_warrior }
					has_trait_xp = {
						trait = desert_warrior
						value < "scope:target.has_trait_xp(desert_warrior)"
					}
				}
			}
			trigger_else_if = {
				limit = {
					exists = scope:scheme.var:mentoring_jungle_stalker
				}
				OR = {
					NOT = { has_trait = jungle_stalker }
					has_trait_xp = {
						trait = jungle_stalker
						value < "scope:target.has_trait_xp(jungle_stalker)"
					}
				}
			}
			trigger_else_if = {
				limit = {
					exists = scope:scheme.var:mentoring_reaver
				}
				OR = {
					NOT = { has_trait = reaver }
					has_trait_xp = {
						trait = reaver
						value < "scope:target.has_trait_xp(reaver)"
					}
				}
			}
			trigger_else_if = {
				limit = {
					exists = scope:scheme.var:mentoring_reckless
				}
				OR = {
					NOT = { has_trait = reckless }
					has_trait_xp = {
						trait = reckless
						value < "scope:target.has_trait_xp(reckless)"
					}
				}
			}
			trigger_else_if = {
				limit = {
					exists = scope:scheme.var:mentoring_holy_warrior
				}
				OR = {
					NOT = { has_trait = holy_warrior }
					has_trait_xp = {
						trait = holy_warrior
						value < "scope:target.has_trait_xp(holy_warrior)"
					}
				}
			}
			trigger_else_if = {
				limit = {
					exists = scope:scheme.var:mentoring_open_terrain_expert
				}
				OR = {
					NOT = { has_trait = open_terrain_expert }
					has_trait_xp = {
						trait = open_terrain_expert
						value < "scope:target.has_trait_xp(open_terrain_expert)"
					}
				}
			}
			trigger_else_if = {
				limit = {
					exists = scope:scheme.var:mentoring_rough_terrain_expert
				}
				OR = {
					NOT = { has_trait = rough_terrain_expert }
					has_trait_xp = {
						trait = rough_terrain_expert
						value < "scope:target.has_trait_xp(rough_terrain_expert)"
					}
				}
			}
			trigger_else_if = {
				limit = {
					exists = scope:scheme.var:mentoring_forest_fighter
				}
				OR = {
					NOT = { has_trait = forest_fighter }
					has_trait_xp = {
						trait = forest_fighter
						value < "scope:target.has_trait_xp(forest_fighter)"
					}
				}
			}
			trigger_else_if = {
				limit = {
					exists = scope:scheme.var:mentoring_cautious_leader
				}
				OR = {
					NOT = { has_trait = cautious_leader }
					has_trait_xp = {
						trait = cautious_leader
						value < "scope:target.has_trait_xp(cautious_leader)"
					}
				}
			}
			trigger_else_if = {
				limit = {
					exists = scope:scheme.var:mentoring_organizer
				}
				OR = {
					NOT = { has_trait = organizer }
					has_trait_xp = {
						trait = organizer
						value < "scope:target.has_trait_xp(organizer)"
					}
				}
			}
			trigger_else = {
				limit = {
					exists = scope:scheme.var:mentoring_winter_soldier
				}
				OR = {
					NOT = { has_trait = winter_soldier }
					has_trait_xp = {
						trait = winter_soldier
						value < "scope:target.has_trait_xp(winter_soldier)"
					}
				}
			}
		}
		scope:target = {
			is_adult = yes
			is_imprisoned = no
			top_liege = scope:owner.top_liege
			NOT = { has_relation_rival = scope:owner }
		}
	}

	odds_prediction = {
		add = odds_mentoring_scheme_misc_value
		min = 0
	}

	# Base Chances
	base_success_chance = {
		base = 10
		# Skill
		compare_modifier = {
			desc = "SCHEME_MY_LEARNING"
			value = scope:owner.learning
			multiplier = 1
		}
		modifier = {
			add = scope:target.learning
			desc = "SCHEME_TARGET_LEARNING"
		}
		modifier = { # Add some extra success based on martial if we are studying commander traits
			add = {
				value = scope:target.martial
				multiply = 0.5
			}
			desc = "SCHEME_TARGET_MARTIAL"
			scope:scheme ?= {
				OR = {
					has_variable = mentoring_logistician
					has_variable = mentoring_military_engineer
					has_variable = mentoring_aggressive_attacker
					has_variable = mentoring_unyielding_defender
					has_variable = mentoring_forder
					has_variable = mentoring_flexible_leader
					has_variable = mentoring_desert_warrior
					has_variable = mentoring_jungle_stalker
					has_variable = mentoring_reaver
					has_variable = mentoring_reckless
					has_variable = mentoring_holy_warrior
					has_variable = mentoring_open_terrain_expert
					has_variable = mentoring_rough_terrain_expert
					has_variable = mentoring_forest_fighter
					has_variable = mentoring_cautious_leader
					has_variable = mentoring_organizer
					has_variable = mentoring_winter_soldier
				}
			}
		}
		# Diarchs are better at schemes within their liege's realm
		diarch_scheming_within_realm_bonus_modifier = yes
		## grand secretary director
		modifier = {
			add = 10
			scope:owner.liege ?= {
				has_active_mandate = promote_merit
			}
			desc = promote_merit_bonus
		}
		# Opinion
		opinion_modifier = {
			who = scope:target
			opinion_target = scope:owner
			multiplier = 0.5
		}
		# Relations
		modifier = {
			add = 10
			desc = SCHEME_TARGET_FRIEND
			scope:owner = { has_relation_friend = scope:target }
		}
		# Personality traits
		modifier = {
			add = -10
			desc = SCHEME_TARGET_LAZY
			scope:target = { has_trait = lazy }
		}
		modifier = {
			add = -10
			desc = SCHEME_OWNER_LAZY
			scope:owner = { has_trait = lazy }
		}
		modifier = {
			add = 5
			desc = SCHEME_TARGET_DILIGENT
			scope:target = { has_trait = diligent }
		}
		modifier = {
			add = 10
			desc = SCHEME_OWNER_DILIGENT
			scope:owner = { has_trait = diligent }
		}
		modifier = {
			add = 5
			desc = SCHEME_TARGET_IMPATIENT
			scope:target = { has_trait = impatient }
		}
		modifier = {
			add = 10
			desc = SCHEME_OWNER_IMPATIENT
			scope:owner = { has_trait = impatient }
		}
		modifier = {
			add = 10
			desc = SCHEME_OWNER_STUBBORN
			scope:owner = { has_trait = stubborn }
		}
		modifier = {
			add = 10
			desc = SCHEME_OWNER_AMBITIOUS
			scope:owner = { has_trait = ambitious }
		}
		# Fam
		modifier = {
			add = 10
			desc = SCHEME_TARGET_CHILD
			scope:owner = { is_parent_of = scope:target }
		}
		modifier = {
			add = 5
			desc = SCHEME_TARGET_CLOSE_FAMILY
			scope:owner = { is_close_family_of = scope:target }
			NOT = { scope:owner = { is_parent_of = scope:target } }
		}
		modifier = {
			add = 5
			desc = SCHEME_TARGET_SAME_HOUSE
			scope:owner.house = scope:target.house
		}
		# Trait Compatibility
		modifier = {
			scope:owner = {
				trait_compatibility = { target = scope:target value >= low_positive_trait_compatibility }
			}
			add = {
				value = 5
				scope:owner = {
					if = {
						limit = {
							trait_compatibility = { target = scope:target value >= medium_positive_trait_compatibility }
						}
						add = 5
						if = {
							limit = {
								trait_compatibility = { target = scope:target value >= high_positive_trait_compatibility }
							}
							add = 5
						}
					}
				}
			}
			desc = TRAIT_COMPATIBILITY
		}
		modifier = {
			scope:owner = {
				trait_compatibility = { target = scope:target value <= low_negative_trait_compatibility }
			}
			add = {
				value = -5
				scope:owner = {
					if = {
						limit = {
							trait_compatibility = { target = scope:target value <= medium_negative_trait_compatibility }
						}
						add = -5
						if = {
							limit = {
								trait_compatibility = { target = scope:target value <= high_negative_trait_compatibility }
							}
							add = -5
						}
					}
				}
			}
			desc = TRAIT_COMPATIBILITY
		}

		# Distance Penalty
		ttd_medium_distance_success_chance_modifier = yes
	}

	# On Actions
	on_phase_completed = {
		# Save scopes
		save_scope_as = scheme
		scheme_target_character = { save_scope_as = target }
		scheme_owner = { save_scope_as = owner }
		# Let's see if we made progress
		random = {
			chance = scope:scheme.scheme_success_chance
			save_scope_value_as = {
				name = scheme_successful
				value = yes
			}
		}
		# Determine the outcome
		scope:owner = {
			if = {
				limit = { exists = scope:scheme_successful }
				trigger_event = { id = mentoring_scheme_event.0001 }
			}
			else = {
				trigger_event = { id = mentoring_scheme_event.0002 }
			}
		}
	}

	## Invalidation
	on_invalidated = {
		scheme_target_character = { save_scope_as = target }
		scheme_owner = {
			save_scope_as = owner
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = mentoring_invalidated_title
				left_icon = scope:target
				
				if = { # Invalidation due to death
					limit = {
						scope:target = { is_alive = no }
						NOT = { block_death_event_trigger = { DEAD = scope:target } }
					}
					custom_tooltip = scheme_target_died
				}
				else_if = { # Diplo range: is it relevant here?
					limit = {
						scope:target = { 
							NOT = { in_diplomatic_range = scope:owner } 
						}
					}
					custom_tooltip = scheme_target_not_in_diplomatic_range
				}
				else = {
					custom_tooltip = scheme_target_has_nothing_to_teach
				}
			}
		}
	}
}
