﻿replace_councillor = {
	
	skill = diplomacy
	hostile = yes
	
	allow = {	
		is_adult = yes
		is_imprisoned = no
		is_councillor = no
	}
	
	valid = {
		scope:owner = {
			exists = liege_or_court_owner
		}
		scope:target = {
			is_councillor = yes
			liege_or_court_owner = scope:owner.liege_or_court_owner
		}
		custom_description = {
			text = replace_councillor_eligible
			object = scope:target
			OR = {
				AND = {
					scope:target = { has_council_position = councillor_chancellor }
					scope:owner = { can_be_chancellor_trigger = { COURT_OWNER = scope:owner.liege_or_court_owner } }
				}
				AND = {
					scope:target = { has_council_position = councillor_steward }
					scope:owner = { can_be_steward_trigger = { COURT_OWNER = scope:owner.liege_or_court_owner } }
				}
				AND = {
					scope:target = { has_council_position = councillor_marshal }
					scope:owner = { can_be_marshal_trigger = { COURT_OWNER = scope:owner.liege_or_court_owner } }
				}
				AND = {
					scope:target = { has_council_position = councillor_spymaster }
					scope:owner = { can_be_spymaster_trigger = { COURT_OWNER = scope:owner.liege_or_court_owner } }
				}
				AND = {
					scope:target = { has_council_position = councillor_court_chaplain }
					scope:owner = { can_be_court_chaplain_trigger = { COURT_OWNER = scope:owner.liege_or_court_owner } }
					scope:owner.liege_or_court_owner = {
						faith = {
							NOT = {
								has_doctrine_parameter = clerical_appointment_fixed
							}
						}
					}	
				}
			}
		}
		# Special AI exceptions
		NOT = {
			scope:owner = {
				is_ai = yes
				has_opinion_modifier = {
					target = scope:target
					modifier = repentant_opinion
				}
			}
		}
	}
	
	valid_agent = {
		liege_or_court_owner = scope:owner.liege_or_court_owner
		NOT = { this = scope:owner.liege_or_court_owner }
	}
	
	agent_join_chance = {
		base = 0
		
		modifier = {
			add = -20
			desc = BASE_RELUCTANCE
		}

		#Is Owner sending a gift in the invite interaction?
		modifier = {
			add = 10
			exists = scope:gift
			always = scope:gift
			NOT = {
				OR = {
					scope:target = { is_parent_of = root }
					root = { is_parent_of = scope:target }
				}
			}
			desc = "SCHEME_WITH_GIFT"
		}
		
		#Am I already sponsoring someone?
		modifier = {
			add = 40
			reverse_has_opinion_modifier = { target = scope:owner modifier = opinion_council_sponsor }
			desc = "REPLACE_COUNCILLOR_AGENT_JOIN_SUPPORTER_OWNER"
		}
		modifier = {
			add = -1000
			reverse_has_opinion_modifier = { target = scope:target modifier = opinion_council_sponsor }
			desc = "REPLACE_COUNCILLOR_AGENT_JOIN_SUPPORTER_TARGET"
		}
		
		# Comparative Skill Effects (multiplied by honor)
		modifier = {
			desc = "REPLACE_COUNCILLOR_COMPARITIVE_SKILL_AGENT"		
			add = {
				if = {
					limit = { scope:target = { has_council_position = councillor_chancellor } }
					value = scope:owner.diplomacy
					subtract = scope:target.diplomacy
				}	
				else_if = {
					limit = { scope:target = { has_council_position = councillor_steward } }
					value = scope:owner.stewardship
					subtract = scope:target.stewardship
				}
				else_if = {
					limit = { scope:target = { has_council_position = councillor_marshal } }
					value = scope:owner.martial
					subtract = scope:target.martial
				}
				else_if = {
					limit = { scope:target = { has_council_position = councillor_spymaster } }
					value = scope:owner.intrigue
					subtract = scope:target.intrigue
				}
				else_if = {
					limit = { scope:target = { has_council_position = councillor_court_chaplain } }
					value = scope:owner.learning
					subtract = scope:target.learning
				}
				multiply = 5
				multiply = { #honor multiplier
					value = 1
					if = { 
						limit = { ai_honor <= 0 }
						add = {
							value = ai_honor
							multiply = 0.005 # Used to be 0.025
						}
					}
					else = {
						add = {
							value = ai_honor
							multiply = 0.01 # Used to be 0.008
						}
					}
				}
			}
		}

		#Target will never scheme against themselves
		modifier = {
			add = -1000
			this = scope:target
			desc = SCHEME_NOT_AGAINST_SELF
		}
		
		#Target is not diplomatically available
		modifier = {
			add = -1000
			is_diplomatically_available = no
			desc = SCHEME_NOT_DIPLOMATICALLY_AVAILABLE
		}

		# Agents dont want to murder their descendants
		modifier = {
			add = -25
			OR = {
				is_great_grandparent_of = scope:target
				is_parent_of = scope:target
			}
			desc = SCHEME_NOT_AGAINST_DESCENDANTS
		}

		# Weak hook used
		modifier = {
			exists = scope:hook
			scope:hook = yes
			add = 100
			desc = SCHEME_WEAK_HOOK_USED
		}
		
		# Skulduggery Focus
		modifier = {
			scope:owner = {
				has_focus = intrigue_skulduggery_focus
			}
			add = 10
			desc = SCHEME_SKULDUGGERY_FOCUS
		}
		
		# Duty Focus
		modifier = {
			scope:target = {
				has_focus = stewardship_duty_focus
			}
			add = -5
			desc = SCHEME_DUTY_FOCUS
		}
		
		# Vengeance ai modifier + crimes/slights against me
		modifier = {
			trigger = {
				ai_vengefulness > 0
				NOT = { this = scope:target }
				OR = {
					has_opinion_modifier = {
						modifier = murdered_close_family_crime
						target = scope:target
					}
					has_opinion_modifier = {
						modifier = attempted_murder_close_family_crime
						target = scope:target
					}
					has_opinion_modifier = {
						modifier = executed_close_family
						target = scope:target
					}
					has_opinion_modifier = {
						modifier = slept_with_spouse_discovered_opinion
						target = scope:target
					}
					has_opinion_modifier = {
						modifier = slept_with_spouse_exposed_opinion
						target = scope:target
					}
					has_opinion_modifier = {
						modifier = unfaithful_spouse_discovered_opinion
						target = scope:target
					}
					has_opinion_modifier = {
						modifier = unfaithful_spouse_exposed_opinion
						target = scope:target
					}
					has_opinion_modifier = {
						modifier = refused_to_renounce_lover_opinion
						target = scope:target
					}
					has_opinion_modifier = {
						modifier = betrayed_our_promise
						target = scope:target
					}
					has_opinion_modifier = {
						modifier = botched_my_treatment_crime_opinion
						target = scope:target
					}
					has_opinion_modifier = {
						modifier = botched_treatment_of_kin_crime_opinion
						target = scope:target
					}
					has_opinion_modifier = {
						modifier = smeared_opinion
						target = scope:target
					}
					has_opinion_modifier = {
						modifier = insulted_opinion
						target = scope:target
					}
					has_opinion_modifier = {
						modifier = mocked_me_in_time_of_need
						target = scope:target
					}
					has_opinion_modifier = {
						modifier = loved_one_dungeon_death
						target = scope:target
					}
					has_opinion_modifier = {
						modifier = abandoned_me_opinion
						target = scope:target
					}
					has_opinion_modifier = {
						modifier = reason_child_died
						target = scope:target
					}
					has_opinion_modifier = {
						modifier = falsely_accused
						target = scope:target
					}
				}
				opinion = {
					target = scope:target
					value <= 0
				}
			}
			add = {
				value = ai_vengefulness
				multiply = 2
			}
			desc = SCHEME_I_MUST_TAKE_VENGEANCE
		}
			
		# Cowardice
		modifier = {
			trigger = {
				ai_boldness < 0
			}
			add = {
				value = ai_boldness
			}
			desc = SCHEME_I_AM_A_COWARD
		}
		
		# Intimidated
		modifier = {
			trigger = {
				has_dread_level_towards = {
					target = scope:target
					level = 1
				}
			}
			add = -100
			desc = SCHEME_I_AM_INTIMIDATED
		}
		# Cowed
		modifier = {
			trigger = {
				has_dread_level_towards = {
					target = scope:target
					level = 2
				}
			}
			add = -1000
			desc = SCHEME_I_AM_COWED
		}
		
		# Zeal vs other Faiths
		modifier = {
			trigger = {
				ai_zeal > 10
				NOT = {
					faith = {
						faith_hostility_level = {
							target = scope:target.faith
							value = faith_fully_accepted_level
						}
					}
				}
			}
			add = {
				value = ai_zeal
				if = {
					limit = {
						faith = {
							faith_hostility_level = {
								target = scope:target.faith
								value = faith_astray_level
							}
						}
					}
					multiply = 0.25
				}
				else = {
					multiply = 0.5
				}
			}
			desc = SCHEME_ZEALOUS_AGAINST_HEATHEN
		}
		
		# How much does the Agent like the Target? Modified by AI honor
		# Positive honor multiplier: Ranges -1 to -5
		# Negative honor multiplier: Ranges -1 to -0.2
		# Then multiplied by 1.5
		opinion_modifier = {
			trigger = {
				NOT = { this = scope:target }
				opinion = {
					target = scope:target
					value >= low_positive_opinion
				}
			}
			who = this
			opinion_target = scope:target
			multiplier = {
				value = -1
				if = {
					limit = { this.ai_honor >= 0 }
					subtract = {
						value = this.ai_honor
						multiply = 0.025
					}
				}
				else = {
					subtract = {
						value = this.ai_honor
						multiply = 0.008
					}
				}
				multiply = 1.5
			}
			desc = SCHEME_I_LIKE_TARGET
		}
		
		# How much does the Agent dislike the Target? Modified by AI honor
		opinion_modifier = {
			trigger = {
				NOT = { this = scope:target }
				opinion = {
					target = scope:target
					value <= low_negative_opinion
				}
			}
			who = this
			opinion_target = scope:target
			multiplier = {
				value = -0.5
				if = {
					limit = { ai_honor <= 0 }
					add = {
						value = ai_honor
						multiply = 0.00625 # Used to be 0.025
					}
				}
				else = {
					add = {
						value = ai_honor
						multiply = 0.005 # Used to be 0.008
					}
				}
			}
			desc = SCHEME_I_DISLIKE_TARGET
		}
		
		#How much does Agent like Owner?
		opinion_modifier = {
			trigger = {
				NOT = { this = scope:owner }
			}
			who = this
			opinion_target = scope:owner
			multiplier = 0.25
			#step = 5
			desc = SCHEME_MY_OPINION_OF_YOU
		}

		#Rivals are more keen on seeing their enemy dead
		modifier = {
			add = 10
			has_relation_rival = scope:target
			desc = SCHEME_AGAINST_RIVAL
		}

		# Nemesis should be even more inclined
		modifier = {
			add = 1000
			has_relation_nemesis = scope:target
			desc = SCHEME_AGAINST_RIVAL
		}

		#Friends don't want to kill friends
		modifier = {
			add = -20
			has_relation_friend = scope:target
			desc = SCHEME_AGAINST_FRIEND
		}

		# Best Friend should be even less inclined
		modifier = {
			add = -2000
			has_relation_best_friend = scope:target
			desc = SCHEME_AGAINST_FRIEND
		}

		#A Spouse that likes their spouse does not want them dead
		modifier = {
			add = -10
			trigger = {
				ai_compassion >= 0
				is_consort_of = scope:target
			}
			opinion = {
				target = scope:target
				value > low_positive_opinion
			}
			desc = SCHEME_AGAINST_SPOUSE
		}
		
		# A spiteful spouse will want to murder you
		modifier = {
			add = 10
			trigger = {
				ai_compassion < 0
				is_consort_of = scope:target
			}
			opinion = {
				target = scope:target
				value < low_negative_opinion
			}
			desc = SCHEME_AGAINST_SPOUSE_POSITIVE
		}

		#Characters whose Faith has 'Sacred Lies' are inherently predisposed to joining schemes
		modifier = {
			add = 20
			faith = {
				has_doctrine_parameter = adherents_more_likely_to_join_schemes
			}
			desc = SCHEME_IS_SACRED
		}

		# Reduced chance for Lovers from the Mortal Adoration Perk
		modifier = {
			add = -25
			scope:target = { has_perk = mortal_adoration_perk }
			has_relation_lover = scope:target
			desc = MORTAL_ADORATION_REDUCED_HOSTILE_SCHEME_JOIN_CHANCE
		}
	}
	
	base_success_chance = {
		base = 25
		
		#Councillor was given position as part of a negotiation
		# modifier = { 
			# factor = 0.33
			# NOT = { trait = ruthless }
			# NOT = { trait = arbitrary }
			# FROMFROM = { 
				# plot_target_char = { 
					# has_character_modifier = negotiated_council_postion
				# }
			# }			
		# }
		# modifier = { 
			# factor = 0
			# trait = honorable
			# FROMFROM = { 
				# plot_target_char = { 
					# has_character_modifier = negotiated_council_postion
				# }
			# }			
		# }
		
		# Comparative Skill Effects (multiplied by honor)
		modifier = {
			desc = "REPLACE_COUNCILLOR_COMPARITIVE_SKILL"		
			add = {
				if = {
					limit = { scope:target = { has_council_position = councillor_chancellor } }
					value = scope:owner.diplomacy
					subtract = scope:target.diplomacy
				}	
				else_if = {
					limit = { scope:target = { has_council_position = councillor_steward } }
					value = scope:owner.stewardship
					subtract = scope:target.stewardship
				}
				else_if = {
					limit = { scope:target = { has_council_position = councillor_marshal } }
					value = scope:owner.martial
					subtract = scope:target.martial
				}
				else_if = {
					limit = { scope:target = { has_council_position = councillor_spymaster } }
					value = scope:owner.intrigue
					subtract = scope:target.intrigue
				}
				else_if = {
					limit = { scope:target = { has_council_position = councillor_court_chaplain } }
					value = scope:owner.learning
					subtract = scope:target.learning
				}
				multiply = 5
				multiply = { 
					value = 1
					if = { #honor multiplier
						limit = { scope:owner.liege_or_court_owner.ai_honor <= 0 }
						add = {
							value = scope:owner.liege_or_court_owner.ai_honor
							multiply = 0.005 # Used to be 0.025
						}
					}
					else = {
						add = {
							value = scope:owner.liege_or_court_owner.ai_honor
							multiply = 0.01 # Used to be 0.008
						}
					}
				}
			}
		}

		# Personal Opinion Effects
		opinion_modifier = {
			who = scope:owner.liege_or_court_owner
			opinion_target = scope:owner
			multiplier = 0.25
			#step = 5
			desc = REPLACE_COUNCILLOR_LIEGE_OPINION_OWNER
		}
		opinion_modifier = {
			who = scope:owner.liege_or_court_owner
			opinion_target = scope:target
			multiplier = -0.25
			#step = 5
			desc = REPLACE_COUNCILLOR_LIEGE_OPINION_TARGET
		}

		##Power Effects##
		modifier = {
			desc = "REPLACE_COUNCILLOR_POWERFUL_VASSAL_OWNER"
			add = 30
			scope:owner = {
				is_powerful_vassal = yes
			}
		}
		modifier = {
			desc = "REPLACE_COUNCILLOR_POWERFUL_VASSAL_TARGET"
			add = -30
			scope:target = {
				is_powerful_vassal = yes
			}
		}
		modifier = {
			desc = "REPLACE_COUNCILLOR_IS_RULER_OWNER"
			add = 15
			scope:owner = {
				is_ruler = yes
				NOT = { is_powerful_vassal = yes }
			}
		}
		modifier = {
			desc = "REPLACE_COUNCILLOR_IS_RULER_TARGET"
			add = -15
			scope:target = {
				is_ruler = yes
				NOT = { is_powerful_vassal = yes }
			}
		}
		#TODO: Relative power to liege
		
		##Influence##
		modifier = {
			desc = "REPLACE_COUNCILLOR_IS_RELATIVE_OWNER"
			add = 10
			scope:owner.liege_or_court_owner = {
				is_close_or_extended_family_of = scope:owner
				NOT = { is_spouse_of = scope:owner }
			}
		}
		modifier = {
			desc = "REPLACE_COUNCILLOR_IS_MARRIED_OWNER"
			add = 15
			scope:owner.liege_or_court_owner = {
				is_spouse_of = scope:owner
			}
		}
		modifier = {
			desc = "REPLACE_COUNCILLOR_IS_LOVER_OWNER"
			add = 5
			scope:owner.liege_or_court_owner = {
				has_relation_lover = scope:owner
			}
		}
		modifier = {
			desc = "REPLACE_COUNCILLOR_IS_RELATIVE_TARGET"
			add = 10
			scope:target.liege_or_court_owner = {
				is_close_or_extended_family_of = scope:target
				NOT = { is_spouse_of = scope:target }
			}
		}
		modifier = {
			desc = "REPLACE_COUNCILLOR_IS_MARRIED_TARGET"
			add = 15
			scope:target.liege_or_court_owner = {
				is_spouse_of = scope:target
			}
		}
		modifier = {
			desc = "REPLACE_COUNCILLOR_IS_LOVER_TARGET"
			add = 5
			scope:target.liege_or_court_owner = {
				has_relation_lover = scope:target
			}
		}
		
		#Influence of target's supporters
		modifier = {
			desc = "REPLACE_COUNCILLOR_TARGET_SUPPORTER_POWERFUL_VASSAL"
			add = -30
			scope:target.liege_or_court_owner = {
				any_vassal = {
					is_powerful_vassal = yes
					reverse_has_opinion_modifier = { target = scope:target modifier = opinion_council_sponsor }
				}	
			}
		}
		modifier = {
			desc = "REPLACE_COUNCILLOR_TARGET_SUPPORTER_INFLUENTIAL"
			add = -15
			scope:target.liege_or_court_owner = {
				OR = {
					any_courtier_or_guest = {
						reverse_has_opinion_modifier = { target = scope:target modifier = opinion_council_sponsor }
						OR = {
							is_councillor = yes
							is_spouse_of = scope:owner.liege_or_court_owner
							is_close_or_extended_family_of = scope:owner.liege_or_court_owner
							has_relation_lover = scope:owner.liege_or_court_owner
						}
					}	
					any_vassal = {
						reverse_has_opinion_modifier = { target = scope:target modifier = opinion_council_sponsor }
						OR = {
							is_councillor = yes
							is_spouse_of = scope:owner.liege_or_court_owner
							is_close_or_extended_family_of = scope:owner.liege_or_court_owner
							has_relation_lover = scope:owner.liege_or_court_owner
						}
					}
				}	
			}
		}
		
		modifier = {
			add = -30
			always = scope:exposed
			desc = "SCHEME_IS_EXPOSED"
		}
	}
	
	agent_success_chance = {
		base = 0

		compare_modifier = {
			desc = "SCHEME_AGENT_DIPLOMACY"
			value = diplomacy
		}
		
		##Power Effects##
		modifier = {
			desc = "REPLACE_COUNCILLOR_POWERFUL_VASSAL_AGENT"
			add = 30
			is_powerful_vassal = yes
		}
		modifier = {
			desc = "REPLACE_COUNCILLOR_IS_RULER_AGENT"
			add = 15
			is_ruler = yes
			NOT = { is_powerful_vassal = yes }
		}
		
		#TODO: Relative power to liege
		
		##Influence##
		modifier = {
			desc = "REPLACE_COUNCILLOR_IS_COUNCILLOR_AGENT"
			add = 10
			is_councillor = yes
		}
		modifier = {
			desc = "REPLACE_COUNCILLOR_IS_RELATIVE_AGENT"
			add = 10
			is_close_or_extended_family_of = scope:owner.liege_or_court_owner
			NOT = { is_spouse_of = scope:owner.liege_or_court_owner }
		}
		modifier = {
			desc = "REPLACE_COUNCILLOR_IS_MARRIED_AGENT"
			add = 15
			is_spouse_of = scope:owner.liege_or_court_owner
		}
		modifier = {
			desc = "REPLACE_COUNCILLOR_IS_LOVER_AGENT"
			add = 5
			has_relation_lover = scope:owner.liege_or_court_owner
		}
		
	}
	
	agent_leave_threshold = -10
		
	cooldown = { years = 5 }
	is_secret = yes
	base_secrecy = 10

	on_ready = {
		scope:owner = {
			trigger_event = {
				id = council_plot.199
				days = 1
			}
		}	
	}

	on_monthly = {
		hostile_scheme_discovery_chance_effect = yes
		
		# if = {
			# limit = {
				# NOT = { exists = scope:discovery_event_happening }
			# }			
			# scheme_owner = {
				# trigger_event = {
					# on_action = claim_throne_ongoing
					# days = { 1 30 }
				# }
			# }
		# }
	}

	
	on_invalidated = {
		scheme_owner = {
			if = {
				limit = { is_councillor = no }
				trigger_event = council_plot.9999
			}	
		}
	}
	
	
	power_per_skill_point = 1
	power_per_agent_skill_point = 1
	spymaster_power_per_skill_point = 0 # Needed to stop the Spymaster from affecting the scheme
	resistance_per_skill_point = 2
	spymaster_resistance_per_skill_point = 0 # Needed to stop the Spymaster from affecting the scheme
	
	minimum_progress_chance = 75
	maximum_progress_chance = 100
	minimum_success = 0
	maximum_secrecy = 100
	maximum_success = 100
	
	success_desc = "REPLACE_COUNCILLOR_SUCCESS_DESC"
	discovery_desc = "REPLACE_COUNCILLOR_DISCOVERY_DESC"
}
