﻿agotma_instigator = {
	# Basic Setup
	skill = intrigue
	success_desc = "AGOTMA_INSTIGATOR_SUCCESS_DESC"
	icon = icon_scheme_agotma_instigator
	illustration = "gfx/interface/illustrations/event_scenes/corridor.dds"
	category = personal
	target_type = character
	is_secret = no
	is_basic = yes
	uses_resistance = no
	cooldown = { years = 5 }

	# Parameters
	speed_per_skill_point = -2.5
	spymaster_speed_per_skill_point = 0	
	uses_resistance = no
	base_progress_goal = 365
	base_maximum_success = 95
	minimum_success = 5

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

	allow = {
		is_adult = yes
		is_travelling = no
		is_playable_character = yes
		highest_held_title_tier >= tier_county
		NOT = {
				has_trait = incapable
				has_character_modifier = agotma_instigator_final_modifier
				scope:target = scope:owner
		}
		scope:target = {
			OR = {
				AND = {
					is_playable_character = yes
					highest_held_title_tier >= tier_county
				}
				AND = {
					NOT = { is_playable_character = yes }
					any_claim = {
						count >= 1
						exists = holder
						tier = tier_hegemony
						holder = {
							NOT = { is_vassal_or_below_of = scope:recipient }
						}
						NOT = { holder = scope:target.father }
						NOT = { holder = scope:target.mother }
					}
					OR = {
						has_trait = ambitious
						has_trait = brave
						has_trait = education_diplomacy_4
						has_trait = education_diplomacy_5
						has_trait = education_martial_4
						has_trait = education_martial_5
						has_trait = education_stewardship_4
						has_trait = education_stewardship_5
						has_trait = education_intrigue_4
						has_trait = education_intrigue_5
						has_trait = education_learning_4
						has_trait = education_learning_5
					}
				}
			}
			NOT = {
				has_trait = content
				has_trait = craven
			}
			is_adult = yes
			is_claimant = yes
			any_claim = {
				count >= 1
				exists = holder
				tier >= tier_duchy
				holder = {
					NOT = { is_vassal_or_below_of = scope:target }
					NOT = { is_parent_of = scope:target }
					NOT = { is_spouse_of = scope:target }
					scope:recipient = {
						save_temporary_opinion_value_as = {
							name = opinion_of_holder
							target = prev
						}
					}
					scope:opinion_of_holder < 25
				}
			}
		}
		OR = {
			AND = {
				scope:actor = { is_ai = yes }
				has_game_rule = agotma_instigator_enabled
			}
			scope:actor = { is_ai = no }
		}
		OR = {
			AND = {
				scope:actor = { is_ai = yes }
				scope:target.top_liege.primary_title = { NOT = { has_variable = agotma_instigator_started } }
			}
			scope:actor = { is_ai = no }
		}
	}

	valid = {
		is_imprisoned = no
		# You're too far away
		scope:target = {
			OR = {
				exists = location
				in_diplomatic_range = scope:owner
			}
		}

		OR = {
			AND = {
				is_ai = yes
				scope:target.top_liege.primary_title ?= { NOT = { has_variable = agotma_instigator_started } }
			}
			AND = {
				is_ai = no
				scope:target = { NOT = { has_character_flag = agotma_instigated } }
			}
		}
		scope:target = { is_at_war = no }
	}


	base_success_chance = {
		base = 100
	}

	on_phase_completed = {
		scheme_target_character = {
			save_scope_as = target
		}

		scheme_owner = {
			save_scope_as = owner
			#Do I want to proceed to a roll?
			if = {
				limit = {
					is_ai = no
				}
				if = {
					limit = { scope:owner = { is_ai = no } }
					trigger_event = agotma_instigator_outcome.0001
					scope:target = { add_character_flag = agotma_instigated }
				}
				else = {
					random_list = {
						50 = {
							trigger_event = agotma_instigator_outcome.0001
							scope:target = { add_character_flag = agotma_instigated }
						}
						50 = { trigger_event = agotma_buybetrothal_outcome.9999 }
					}
				}
			}
			#Jump straight to roll
			else = {
				if = {
					limit = { scope:owner = { is_ai = no } }
					trigger_event = agotma_instigator_outcome.0002
					scope:target = { add_character_flag = agotma_instigated }
				}
				else = {
					random_list = {
						50 = {
							trigger_event = agotma_instigator_outcome.0002
							scope:target = { add_character_flag = agotma_instigated }
						}
						50 = { trigger_event = agotma_buybetrothal_outcome.9999 }
					}
				}
			}
		}

		scope:target.top_liege.primary_title = {
			if = {
				limit = { NOT = { has_variable = agotma_instigator_started } }
				set_variable = {
					name = agotma_instigator_started
					years = 15
				}
			}
		}
	}

	on_invalidated = {
		scheme_target_character = {
			save_scope_as = target
		}
		scheme_owner = {
			save_scope_as = owner
		}

		if = {
			limit = {
				scope:target.top_liege.primary_title ?= { has_variable = agotma_instigator_started }
				scope:owner = { is_ai = yes }
			}
			scope:owner = {
				trigger_event = agotma_buybetrothal_outcome.9999
			}
		}

		if = {
			limit = {
				scope:target = { is_alive = no }
			}
			scope:owner = {
				trigger_event = agotma_instigator_outcome.0006
			}
		}
	}

	on_monthly = {
		save_scope_as = scheme
		scheme_owner = { save_scope_as = owner }
		scheme_target_character = { save_scope_as = target }

		#already revealed!
		if = {
			limit = {
				NOT = { scheme_progress = 10 }
				scope:target = { 
					NOT = { is_claimant = yes }
					}
			}
			scope:owner = {
				trigger_event = agotma_instigator_outcome.0007
			}
			scope:scheme = {
				end_scheme = yes
			}
		}
	}
}



