﻿############################
# Foster Legitimacy Scheme
# (Only for Governors against the Emperor)
############################

foster_legitimacy_scheme = {
	# Basic Setup
	skill = diplomacy
	desc = foster_legitimacy_scheme_desc_general
	success_desc = "FOSTER_LEGITIMACY_SUCCESS_DESC"
	icon = icon_scheme_foster_legitimacy
	illustration = "gfx/interface/illustrations/event_scenes/corridor.dds"
	target_type = character
	category = political
	is_secret = no
	is_basic = yes
	cooldown = { years = 5 }
	
	# Parameters
	speed_per_skill_point = -2.5
	spymaster_speed_per_skill_point = 0	
	uses_resistance = no
	base_progress_goal = 180
	base_maximum_success = 95
	minimum_success = 5

	freeze_scheme_when_traveling = yes
	freeze_scheme_when_traveling_target = no
	
	# Only for Governors against the Emperor
	allow = {
		is_adult = yes
		is_imprisoned = no
		is_governor = yes
		scope:target = {
			this = root.liege
		}
	}
	
	# Core Triggers
	valid = {
		is_alive = yes
		is_adult = yes
		is_imprisoned = no
		is_incapable = no
		scope:target = {
			is_alive = yes
		}
	}

	odds_prediction = {
		add = base_odds_prediction_target_is_char_value
		add = odds_skill_contribution_diplomacy_value
		add = odds_foster_legitimacy_scheme_misc_value
		min = 0
	}

	# Base Chances
	base_success_chance = {
		base = 0
		
		# Skill
		compare_modifier = {
			desc = "SCHEME_YOUR_DIPLOMACY"
			value = scope:owner.diplomacy
			multiplier = 2
		}
		compare_modifier = {
			desc = "SCHEME_LIEGE_DIPLOMACY"
			value = scope:target.diplomacy
		}
		
		# Popular Opinion of Target's Capital
		compare_modifier = {
			desc = "SCHEME_LIEGE_POPULAR_OPINION"
			value = scope:target.capital_county.county_opinion
		}
		
		# Traits
		modifier = {
			add = 10
			desc = overseer_trait_desc
			scope:owner = {
				has_trait = overseer
			}
		}

		modifier = {
			scope:owner = { has_character_modifier = poet_for_diplo_schemes_modifier }
			desc = poet_for_diplo_schemes_modifier
			add = 15
		}

		# house_head_request_interaction
		modifier = {
			add = personal_scheme_variable_list_value
			scope:owner = {
				has_variable_list = supporting_personal_schemes
			}
			desc = HOUSE_HEAD_SCHEME_SUPPORT_DESC
		}
		modifier = {
			add = -10
			scope:owner = { has_character_modifier = personal_schemes_distracted_modifier }
			desc = personal_schemes_distracted_modifier
		}
		# Estate
		modifier = {
			scope:owner.domicile ?= {
				has_domicile_parameter = increased_success_personal_schemes_1
			}
			add = estate_increased_personal_scheme_success_1_value
		}
		modifier = {
			scope:owner.domicile ?= {
				has_domicile_parameter = increased_success_personal_schemes_2
			}
			add = estate_increased_personal_scheme_success_2_value
		}
		modifier = {
			scope:owner.domicile ?= {
				has_domicile_parameter = increased_success_personal_schemes_3
			}
			add = estate_increased_personal_scheme_success_3_value
		}

		# Distance Penalty
		ttd_medium_distance_success_chance_modifier = yes
	}
	
	# On Actions
	on_phase_completed = {
		save_scope_as = scheme
		scope:scheme = {
			scheme_target_character = { save_scope_as = target }
			scheme_owner = {
				save_scope_as = owner

				#SUCCESS ROLL
				random = {
					chance = scope:scheme.scheme_success_chance

					save_scope_value_as = {
						name = scheme_successful
						value = yes
					}
				}

				if = {
					limit = {
						exists = scope:scheme_successful
					}
					trigger_event = {
						id = scheme_critical_moments.4001
						days = 1
					}
				}
				else = {
					trigger_event = {
						id = scheme_critical_moments.4002
						days = 1
					}	
				}
			}
		}
	}
	
	on_invalidated = {
		scheme_target_character = {
			save_scope_as = target
		}
		scheme_owner = {
			save_scope_as = owner
		}
		# Invalidation due to death
		if = {
			limit = {
				scope:target = { is_alive = no }
				scope:owner = { is_alive = yes }
			}
			scope:owner = {
				send_interface_toast = {
					title = damage_legitimacy_scheme_invalidated_title
					left_icon = scope:target
					custom_description_no_bullet = {
						object = scope:target
						text = scheme_target_died
					}
				}
			}
		}
		# They're not your liege anymore
		else_if = {
			limit = {
				scope:target = {
					this != scope:owner.liege
				}
			}
			scope:owner = {
				send_interface_toast = {
					title = damage_legitimacy_scheme_invalidated_title
					left_icon = scope:target
					custom_description_no_bullet = {
						object = scope:target
						text = scheme_target_not_liege
					}
				}
			}
		}
		# You're not a governor
		else_if = {
			limit = {
				scope:target = {
					this != scope:owner.liege
				}
			}
			scope:owner = {
				send_interface_toast = {
					title = damage_legitimacy_scheme_invalidated_title
					left_icon = scope:target
					custom_description_no_bullet = {
						object = scope:target
						text = scheme_owner_not_admin
					}
				}
			}
		}
	}
}
