#Events handling regula_instiga_discordia outcomes

namespace = regula_instiga_discordia_outcome

##################################
# MAINTENANCE EVENTS 0000 - 0099
##################################

regula_instiga_discordia_outcome.0001 = {
	
	hidden = yes

	immediate = {
		add_character_flag = { #Only resolve one abduction in the same day
			flag = is_in_event_regula_instiga_discordia_outcome_0001
			days = 1
		}

		# Find someone to make the target's rival
		scope:target = {
			every_vassal = {
				limit = {
					is_powerful_vassal = yes
					NOT = { has_relation_rival = scope:target }
				}
				add_to_temporary_list = potential_new_rival
			}
			if = {
				limit = {
					is_independent_ruler = no
				}
				scope:target.liege = {
					add_to_temporary_list = potential_new_rival
				}
			}
		}
		random_in_list = {
			list = potential_new_rival
			save_scope_as = new_rival
		}

		### Roll to see if the plan succeeds.
		#SUCCESS ROLL
		random = {
			chance = scope:scheme.scheme_success_chance

			save_scope_value_as = {
				name = scheme_successful
				value = yes
			}
		}

		#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
			}
		}
		global_var:magister_character = {
			if = {
				limit = {
					exists = scope:scheme_successful
				}
				trigger_event = {
					on_action = regula_instiga_discordia_success
				}
			}
			else = {
				trigger_event = {
					on_action = regula_instiga_discordia_failure
				}
			}
		}
	}
}


#################
# SUCCESS EVENTS
# 1000-1999
#################

regula_instiga_discordia_outcome.1001 = {
	type = character_event
	title = regula_instiga_discordia_outcome.1001.t
	desc = {
		desc = regula_instiga_discordia_outcome.1001.opening
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:scheme_discovered
				}
				desc = regula_instiga_discordia_outcome.1001.owner_discovered
			}
			desc = regula_instiga_discordia_outcome.1001.owner_secret
		}
	}
	theme = diplomacy
	left_portrait = {
		character = scope:target
		animation = personality_dishonorable  ### UPDATE
	}
	right_portrait = {
		character = scope:new_rival
		animation = rage
	}

	immediate = {
		if = {
			limit = {
				exists = scope:scheme_discovered
			}
			regula_instiga_discordia_discovered_success_effect = yes
		}
		scope:new_rival = {
			add_character_flag = {  ### Find a way to make them bottomless only?
				flag = is_naked
				days = 180
			}
		}
	}

	option = { # Abandon scheme
		name = regula_instiga_discordia_outcome.1001.a

		scope:target = {
			hidden_effect = {
				if = {
					limit = {
						has_relation_potential_rival = scope:new_rival
					}
					remove_relation_potential_rival = scope:new_rival
				}
			}
			set_relation_rival = scope:new_rival
		}
		scope:scheme = {
			end_scheme = yes
		}
	}
	after = {
		scope:new_rival = {
			remove_character_flag = is_naked
		}
	}
}


#################################
# FAILURE OUTCOMES 2000 - 2999
#################################
regula_instiga_discordia_outcome.2001 = {
	type = character_event
	title = regula_instiga_discordia_outcome.2001.t
	desc = {
		desc = regula_instiga_discordia_outcome.2001.opening
		first_valid = {
			triggered_desc = {
				trigger = {
					exists = scope:scheme_discovered
				}
				desc = regula_instiga_discordia_outcome.2001.owner_discovered
			}
			desc = regula_instiga_discordia_outcome.2001.owner_secret
		}
	}
	theme = diplomacy
	left_portrait = {
		character = scope:target
		triggered_animation = {
			trigger = {
				exists = scope:scheme_discovered
			}
			animation = stress  # Upset
		}
		animation = disapproval  # Wondering what on earth happened.
	}
	
	
	immediate = {
		if = {
			limit = {
				exists = scope:scheme_discovered
			}
			regula_instiga_discordia_discovered_failure_effect = yes
		}
	}

	option = { # Abandon scheme
		name = regula_instiga_discordia_outcome.2001.a
		scope:scheme = {
			end_scheme = yes
		}
	}
}

###################################
# MAGISTER SUCCESS NOTIFICATIONS
###################################

### Not being run.  The success/failure events fire for the magister instead.

# regula_instiga_discordia_outcome.7001 = {
# 	type = letter_event
	
# 	opening = {
# 		desc = regula_instiga_discordia_outcome.7001.opening
# 	}
# 	desc = regula_instiga_discordia_outcome.7001.desc
# 	sender = {
# 		character = scope:owner
# 		animation = worry ### UPDATE - Put something relevant here.
# 	}

# 	# immediate ={
# 	# 	show_as_tooltip = { ransom_interaction_effect = yes }
# 	# }

# 	option = {
# 		name = regula_instiga_discordia_outcome.7001.a
# 	}
# }



# ###################################
# # MAGISTER FAILURE NOTIFICATIONS
# ###################################

# regula_instiga_discordia_outcome.7002 = {
# 	type = letter_event
# 	opening = {
# 		desc = regula_instiga_discordia_outcome.7002.opening
# 	}
# 	desc = regula_instiga_discordia_outcome.7002.desc
	
# 	sender = scope:owner

# 	# immediate ={
# 	# 	show_as_tooltip = { ransom_interaction_effect = yes }
# 	# }

# 	option = {
# 		name = regula_instiga_discordia_outcome.7002.a
# 	}
# }