﻿convert_to_witchcraft = {
	# Basic Setup
	skill = learning
	desc = convert_to_witchcraft_general_desc
	success_desc = "CONVERT_TO_WITCHCRAFT_SUCCESS_DESC"
	icon = icon_scheme_convert_to_witchcraft
	illustration = "gfx/interface/illustrations/event_scenes/corridor.dds"
	target_type = character
	is_secret = yes
	maximum_breaches = 5
	is_basic = yes
	cooldown = { months = 2 }
	
	# Parameters
	speed_per_skill_point = -1
	speed_per_target_skill_point = 0.5
	base_progress_goal = 185
	maximum_secrecy = 95
	base_maximum_success = 95
	minimum_success = 5
	freeze_scheme_when_traveling = yes
	freeze_scheme_when_traveling_target = yes

	# Core Triggers
	allow = {
		is_witch_trigger = yes
		is_adult = yes
		is_imprisoned = no
		is_in_army = no 

		custom_tooltip = { 
		text = "at_war_with_target_or_lieges" 
		is_at_war_with_target_and_its_lieges = no
		}
		is_available_for_personal_scheme = yes 
		trigger_if = {  
			limit = { is_ai = no }
		can_be_target_of_befriend_trigger = yes
		}
		trigger_if = {
			limit = {
				NOT = { government_has_flag = government_is_landless_adventurer }
			}
			is_travelling = no
		}
		scope:target = {
			is_adult = yes
			NOT = { has_trait = witch } #We don't want to expose them if they have the secret, we'll send you a special event instead where you're both exposed!
			is_imprisoned = no
			trigger_if = {
				limit = {
					scope:owner = {
						NOT = { government_has_flag = government_is_landless_adventurer }
					}
				}
				is_travelling = no
			}
		}
	}
	valid = {
		is_incapable = no
		#Have I been blocked from targeting this character?
		custom_description = { #Permanent
			text = witchcraft_scheme_blocked
			subject = scope:target

			NOT = {
				scope:target = {
					has_character_flag = block_convert_to_witchcraft
				}
			}
		}
		scope:target = {
			exists = location
		}
	}
	use_secrecy = {
		use_convert_to_witchcraft_secrecy_trigger = { OWNER = scope:owner }
	}

	odds_prediction = {
		add = base_odds_prediction_target_is_char_value
		add = odds_skill_contribution_learning_value
		add = odds_sway_scheme_misc_value
		if = {
			limit = {
				scope:owner = {
					exists = house
					house = { has_house_modifier = witch_coven }
				}
			}
			add = 20
		}
		if = {
			limit = {
				scope:target.ai_zeal < 0
			}
			add = scope:target.ai_zeal
			multiply = -1
			max = 50
		}
		min = 0
	}
	
	base_success_chance = {
		base = -40
		
		# Countermeasures.
		apply_opportunistic_scheme_success_chance_adjustments_modifier = yes
		
	
		 #########OPINION#DISABLED FOR AI TO SAVE PERFORMANCE
		opinion_modifier = {
		trigger = {
			scope:owner = {is_ai = no}
			target_knows_about_owner_trigger = yes
		} 
			desc = OBF_opinion_of_you
			who = scope:target
			opinion_target = scope:owner  
			max = 100  
			min = -100    
			multiplier = 1   
		}
		
		modifier = {
			desc = OBF_opinion_of_you
			add = -60
			scope:owner = {is_ai = no}
			target_knows_about_owner_trigger = no
			desc = OBF_doesnt_know_you
		}
		
		#Opinion FOR AI 
		opinion_modifier = {
		trigger = {
			scope:owner = {is_ai = yes}
		} 
			who = scope:target
			opinion_target = scope:owner
			min = -50
			max = 50
			multiplier = 1.5
			step = 5
		}

		#Learning based
		compare_modifier = {
			target = scope:owner
			value = learning
			multiplier = 1.5
			desc = "SCHEME_LEARNING_MODIFIER"
		}
		
		#Diplomacy also counts...
		compare_modifier = { 
			desc = sway_my_diplomacy
			target = scope:owner 
			value = diplomacy
			multiplier = 0.75
		}
		
		#...and your subterfuge ability
		compare_modifier = { 
			desc = OBF_MY_INTRIGUE
			target = scope:owner
			value = intrigue
			multiplier = 0.75
		} 
			
		#TARGET LEARNING  
		witch_learning_modifier = yes 

		#PERSONAL AND DINASTY PRESTIGE
		witch_prestige_modifier = yes
		
		#STRESS LEVELS
		witch_stress_modifier = yes
		
		#LANGUAGE AND CULTURE
		witch_language_and_culture_modifier = yes
		
		#DIPLOMACY, MILITARY STRENGTH, HOSTILITIES AND CLAIMS
		witch_hostility_modifier = yes
		
		#RELIGION AND SECRETS
		witch_religion_modifier = yes 
		   
		#LIFESTYLE AND EDUCATION
		witch_lifestyle_and_education_modifier = yes
		 
		#RELATIONS AND EASE OF CONTACT 
		witch_relation_to_target_modifier = yes
		
		#CHARACTER_MODIFIERS
		witch_char_mods_modifier = yes
		
		#TRAITS AND PERSONALITY
		witch_traits_and_personality_modifier = yes
		
		#HARD MODIFIERS ON TARGET OPINION OF OWNER 
		witch_target_opinion_modifier = yes

		# House Personal Scheme Success Chance on Cultural Parameter
		modifier = {
			add = cultural_house_personal_scheme_success_chance
			desc = KIN_PARAMETER_DESC
			exists = scope:owner.house
			exists = scope:target.house
			scope:owner.culture = {
				has_cultural_parameter = cultural_house_personal_scheme_success_chance
			}
			scope:target.house = scope:owner.house
		}
		# Modifiers
		# 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
		}
	}
	base_secrecy = {
		add = secrecy_base_value
		add = countermeasure_apply_secrecy_maluses_value
	}
	
	# On Actions
	on_start = {
		set_variable = {
			name = apply_countermeasures
			value = flag:opportunistic
		}
		#They're already a witch? Let's expose you to each other!
		save_scope_as = scheme
		scheme_owner = { save_scope_as = owner }
		scheme_target_character = { save_scope_as = target }
		if = {
			limit = {
				scope:target = { any_secret = { secret_type = secret_witch } }
			}
			scope:owner = {
				trigger_event = {
					id = witch.2006
					days = { 7 14 }
				}
			}
		}

		if = {
			limit = {
					distance_from_owner_to_target = { DISTANCE = 150000}
					both_work_on_same_court = no
			}
			scope:scheme = {
			add_scheme_progress = 5
			}
		}
		if = {
			limit = {
					distance_from_owner_to_target = { DISTANCE = 120000}
					both_work_on_same_court = no
			}
			scope:scheme = {
			add_scheme_progress = 5
			}
		}
		if = {
			limit = {
					distance_from_owner_to_target = { DISTANCE = 90000}
					both_work_on_same_court = no
			}
			scope:scheme = {
			add_scheme_progress = 5
			}
		}
		if = {
			limit = {
					distance_from_owner_to_target = { DISTANCE = 60000}
					both_work_on_same_court = no
			}
			scope:scheme = {
			add_scheme_progress = 5
			}
		}
		if = {
			limit = {
					distance_from_owner_to_target = { DISTANCE = 30000}
					both_work_on_same_court = no
			}
			scope:scheme = {
			add_scheme_progress = 10
			}
		}
		if = {
			limit = {
					distance_from_owner_to_target = { DISTANCE = 20000}
					both_work_on_same_court = no
			}
			scope:scheme = {
			add_scheme_progress = 10
			}
		}
		if = {
			limit = {
						both_work_on_same_court = yes
			}
			scope:scheme = {
			add_scheme_progress = 60
			}
		}
	}
	on_phase_completed = {
		# Grab our scopes.
		save_scope_as = scheme
		scheme_target_character = { save_scope_as = target }
		scheme_owner = { save_scope_as = owner }
		scope:scheme = {
			scheme_owner = {
				#DISCOVERY ROLL
				save_scope_value_as = {
					name = discovery_chance
					value = {
						value = 100
						subtract = scope:scheme.scheme_secrecy
					}
				}
				random = {
					chance = scope:discovery_chance
					save_scope_value_as = {
						name = scheme_discovered
						value = yes
					}
				}

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

					save_scope_value_as = {
						name = scheme_successful
						value = yes
					}
				}

				#SECRECY: DO I WANT TO PROCEED?
				if = {
					limit = {
						use_convert_to_witchcraft_secrecy_trigger = { OWNER = scope:scheme.scheme_owner }
					}
					trigger_event = witch.2001
				}
				#CONTINUE WITH OUTCOME ON_ACTION FOR AI TARGETS
				else_if = {
					limit = { scope:target = { is_ai = yes } }

					if = {
						limit = { exists = scope:scheme_successful }
						trigger_event = witch.2003 #Success event
					}
					else = {
						trigger_event = {
							on_action = convert_to_witchcraft_failure_outcome
						}
					}
				}
				#FOR PLAYER CHARACTER, SEND CHOICE EVENT
				else = {
					scope:target = {
						trigger_event = witch.2002
					}
				}
			}
		}
	}
	on_monthly = {
		hostile_scheme_monthly_discovery_chance_effect = yes
	}
	on_invalidated = {
		scheme_target_character = {
			save_scope_as = target
		}
		scheme_owner = {
			save_scope_as = owner
		}
		if = {
			limit = {
				scope:target = { is_alive = no }
			}
			scope:owner = {
				send_interface_toast = {
					title = convert_to_witchcraft_scheme_invalidated_title
					left_icon = scope:target
					custom_description_no_bullet = {
						object = scope:target
						text = scheme_target_died
					}
				}
			}
		}
		if = {
			limit = {
				scope:target = { is_imprisoned = yes }
			}
			scope:target.imprisoner = {
				save_scope_as = other_imprisoner
			}
			scope:owner = {
				send_interface_toast = {
					title = convert_to_witchcraft_scheme_invalidated_title
					left_icon = scope:target
					right_icon = scope:other_imprisoner
					custom_description_no_bullet = {
						subject = scope:other_imprisoner
						object = scope:target
						text = scheme_target_imprisoned_by_other
					}
				}
			}
		}
	}
}

