﻿######################################
# Regula Character Interactions (AI) #
######################################################
# This file has the interactions that the AI can do (or that are for the AI to complete)
##
# Regula Court Interactions
## regula_abice_maritus - Wife takes land from husband, similar to regula_enact_abice_maritus_interaction
## regula_instiga_discordia - Instigate Discord, makes an AI character start a scheme against their liege to damage relations between them and one of their powerful vassals
## start_rapta_maritus - Wife starts scheme to abduct their husband and send them to you
## revela_secretum - Make an AI character reveal secrets from their court
## regula_ai_join_against_servitude_war - AI ruler joins against a servitude war targeting one of their vassals.
######################################################

# Wife takes husbands titles.  NOTE: As the game doesn't look at non-landed characters this is written inverted, with the husband as the actor.
regula_abice_maritus = {
	interface_priority = 69
	category = interaction_category_religion

	common_interaction = yes
	use_diplomatic_range = no
	ignores_pending_interaction_block = yes

	is_shown = {
		scope:recipient = {
			is_imprisoned = no
			character_has_regula_holy_effect_abice_martius = yes
			piety >= 500
		}
		scope:actor = {
			is_ai = yes
			is_at_war = no
			is_spouse_of = scope:recipient
			is_male = yes
			is_ruler = yes
			NOT = { # Can't become head of a holy order. Mercenary company OK.
				any_held_title = {
					is_holy_order = yes
				}
			}
			NOT = { has_trait = magister_trait_group }
		}
	}

	auto_accept = yes

	on_accept = {
		scope:actor = {
			create_title_and_vassal_change = {
				type = usurped
				save_scope_as = change
				add_claim_on_loss = no
			}
			every_held_title = {
				change_title_holder_include_vassals = {
					holder = scope:recipient
					change = scope:change
				}
			}
			resolve_title_and_vassal_change = scope:change
		}

		scope:actor = {
			scope:recipient = {
				add_piety = -500
				if = {
					limit = {
						scope:recipient.top_liege = { # This is going to be of limited utility for foreign Mulsa.
							any_vassal_or_below = {
								has_government = theocracy_government
								primary_title.tier > tier_barony
								faith = scope:recipient.faith
							}
						}
					}
					scope:recipient.top_liege = {
						random_vassal_or_below = {
							limit = {
								has_government = theocracy_government
								primary_title.tier > tier_barony
								faith = scope:recipient.faith
							}
							save_scope_as = theocratic_travel_target
						}
					}
					visit_court_of = scope:theocratic_travel_target
				}
				else_if = {
					limit = {
						exists = scope:recipient.capital_province
					}
					move_to_pool_at = scope:recipient.capital_province
				}
				kick_from_court_interaction_warning_tooltip_effect = yes
			}
			scope:actor = {
				add_opinion = {
					modifier = demanded_taking_vows
					target = scope:recipient
				}
				if = {
					limit = { gold > 0 }
					pay_short_term_gold = {
						target = scope:recipient
						gold = scope:actor.gold
					}
				}
				add_trait = devoted
				add_piety_level = 1
				if = {
					limit = { is_ruler = yes }
					depose = yes
				}
				if = {
					limit = {
						is_married = yes
					}
					every_spouse = {
						divorce = scope:actor
					}
				}
				if = {
					limit = {
						exists = betrothed
					}
					break_betrothal = betrothed
				}
				if = {
					limit = {
						is_concubine = yes
					}
					this.concubinist = {
						remove_concubine = scope:actor
					}
				}
				if = {
					limit = {
						number_of_concubines > 0
					}
					every_concubine = {
						scope:recipient = {
							remove_concubine = prev
						}
					}
				}
			}
		}
		if = { # Notify the player if this is particularly relevant.
			limit = {
				OR = {
					scope:recipient = {
						OR = {
							is_independent_ruler = yes
							top_liege = global_var:magister_character
						}
					}
					scope:actor = { # Fallback
						OR = {
							is_independent_ruler = yes
							top_liege = global_var:magister_character
						}
					}
				}
			}
			if = {
				limit = {
					global_var:magister_character.faith = {
						OR = {
							this = faith:regula_mediterranean
							this = faith:regula_byzantine
						}
					}
				}
				global_var:magister_character = {
					send_interface_message = {  # Tells the player the outcome.
						type = event_spouse_good
						title = regula_abice_maritus_effect_title
						desc = regula_abice_maritus_effect.desc_mediterranean
						right_icon = scope:actor
						left_icon = scope:recipient
					}
				}
			}
			else_if = {
				limit = {
					global_var:magister_character.faith = {
						OR = {
							this = faith:regula_europe
							this = faith:regula_britannia
							this = faith:regula_francia
							this = faith:regula_hispania
							this = faith:regula_baltic
							this = faith:regula_europe_pirate
							this = faith:regula_scandinavia
						}
					}
				}
				global_var:magister_character = {
					send_interface_message = {  # Tells the player the outcome.
						type = event_spouse_good
						title = regula_abice_maritus_effect_title
						desc = regula_abice_maritus_effect.desc_european
						right_icon = scope:actor
						left_icon = scope:recipient
					}
				}
			}
			else_if = {
				limit = {
					global_var:magister_character.faith = {
						OR = {
							this = faith:regula_asia
							this = faith:regula_rajasthan
							this = faith:regula_persia
						}
					}
				}
				global_var:magister_character = {
					send_interface_message = {  # Tells the player the outcome.
						type = event_spouse_good
						title = regula_abice_maritus_effect_title
						desc = regula_abice_maritus_effect.desc_asia
						right_icon = scope:actor
						left_icon = scope:recipient
					}
				}
			}
			else_if = {
				limit = {
					global_var:magister_character.faith = {
						OR = {
							this = faith:regula_africa
							this = faith:regula_mali
						}
					}
				}
				global_var:magister_character = {
					send_interface_message = {  # Tells the player the outcome.
						type = event_spouse_good
						title = regula_abice_maritus_effect_title
						desc = regula_abice_maritus_effect.desc_africa
						right_icon = scope:actor
						left_icon = scope:recipient
					}
				}
			}
			else = {
				global_var:magister_character = {
					send_interface_message = {  # Tells the player the outcome.
						type = event_spouse_good
						title = regula_abice_maritus_effect_title
						desc = regula_abice_maritus_effect.desc_generic
						right_icon = scope:actor
						left_icon = scope:recipient
					}
				}
			}
		}
	}

	ai_targets = {
		ai_recipients = spouses
	}

	ai_targets = {
		ai_recipients = liege
	}

	ai_potential = {
		is_adult = yes
	}

	ai_frequency = 6

	ai_will_do = { # Better to link the effect to ai's piety than traits.  Way less balancing required.
		base = 100
	}
}

# Instigate Discord
regula_instiga_discordia = {
	category = interaction_category_religion
	common_interaction = yes
	use_diplomatic_range = no
	ignores_pending_interaction_block = yes
	interface_priority = 71

	cooldown_against_recipient = { years = 2 } # Can run through all mulsa, but can't just repeatedly run the event against one mulsa.

	cost = {
		piety = {
			value = 150
		}
	}

	is_shown = {
		scope:actor = { has_trait = magister_trait_group }
		scope:recipient = { # Mulsa must be foreign.
			has_trait = devoted_trait_group
			is_independent_ruler = no
			OR = {
				is_ruler = yes
				is_married = yes
			}
			NOT = { is_spouse_of = scope:actor }
			NOT = { is_in_the_same_court_as_or_guest = scope:actor }
		}
		NOR = {
			scope:recipient.top_liege = scope:actor
			scope:recipient.liege = scope:actor
			scope:recipient = scope:actor
			scope:recipient = { is_imprisoned = yes }
		}
	}

	is_valid_showing_failures_only = {
		scope:actor = {
			custom_description = {
				text = magister_trait_3_required_trigger
				has_trait_rank = {
					trait = magister_trait_group
					rank >= 3
				}
			}
		}

		scope:recipient = {
			custom_description = {
				text = regula_instiga_discordia_in_progress_trigger
				can_start_scheme = {
					type = regula_instiga_discordia
					target_character = scope:recipient.liege
				}
			}
		}

		# Check if target has any vassals to rival
		scope:recipient.liege = {
			custom_description = {
				text = regula_instiga_discordia_has_vassals_to_rival_trigger
				number_of_powerful_vassals >= 1
			}
		}

	}

	desc = regula_instiga_discordia_interaction_desc

	auto_accept = yes

	on_accept = {
		scope:recipient = {
			if = {
				limit = {
					NOT = { has_trait = regula_gossip }
				}
				add_trait = regula_gossip
			}
			scope:recipient.liege = {
				save_scope_as = target
			}
		}

		scope:actor = {
			send_interface_toast = {
				title = start_regula_instiga_discordia_notification

				left_icon = scope:recipient
				right_icon = scope:target

				scope:recipient = {
					start_scheme = {
						type = regula_instiga_discordia
						target_character = scope:target
					}
				}
			}
		}
	}
	ai_will_do = {
		base = 100
	}
}

# Abduct Husband
start_rapta_maritus = {
	interface_priority = 65
	category = interaction_category_religion
	common_interaction = yes
	icon = prison
	ignores_pending_interaction_block = yes

	cooldown = { years = 3 }

	is_shown = {
		scope:actor = { has_trait = magister_trait_group }
		scope:recipient = {
			has_trait = devoted_trait_group
			is_married = yes
			NOT = { is_spouse_of = scope:actor }
			NOT = { is_vassal_or_below_of = scope:actor }
		}
		NOR = {
			scope:recipient = scope:actor
			scope:recipient = { is_imprisoned = yes }
		}
	}

	is_valid_showing_failures_only = {  # UPDATE - Need to add explanatory text. And a something that states they're still working.
		scope:actor = {
			custom_description = {
				text = magister_trait_4_required_trigger
				has_trait_rank = {
					trait = magister_trait_group
					rank >= 4
				}
			}
		}
		scope:recipient = {
			custom_description = {
				text = rapta_maritus_in_progress_trigger
				can_start_scheme = {
					type = rapta_maritus
					target_character = scope:recipient.primary_spouse
				}
			}
		}
	}

	cost = {
		piety = {
			value = 500
		}
	}

	desc = {
		triggered_desc = {
			trigger = {
				scope:actor = {
					can_start_scheme = {
						type = rapta_maritus
						target_character = scope:recipient.primary_spouse
					}
				}
			}
			desc = scheme_interaction_tt_rapta_maritus_approved
		}
	}

	on_accept = {
		scope:recipient = {
			stress_impact = {
				compassionate = medium_stress_impact_gain
				honest = minor_stress_impact_gain
				just = minor_stress_impact_gain
			}
			add_intrigue_skill = 3
		}
		scope:actor = {
			send_interface_toast = {
				title = start_rapta_maritus_notification

				left_icon = scope:recipient
				right_icon = scope:recipient.primary_spouse

				scope:recipient = {
					start_scheme = {
						type = rapta_maritus
						target_character = scope:recipient.primary_spouse
					}
				}
			}
		}
	}

	auto_accept = yes
}

# Reveal Secrets
revela_secretum = {
	category = interaction_category_religion
	common_interaction = yes
	use_diplomatic_range = no
	ignores_pending_interaction_block = yes
	interface_priority = 60
	icon = debug_secret

	cooldown_against_recipient = { years = 2 } # Can run through all mulsa, but can't just repeatedly run the event against one mulsa.

	cost = {
		piety = {
			value = 50
		}
	}

	is_shown = {
		scope:actor = { has_trait = magister_trait_group }
		scope:recipient = { # Mulsa must be foreign.
			has_trait = devoted_trait_group
			is_independent_ruler = no
			NOT = { is_spouse_of = scope:actor }
			NOT = { is_in_the_same_court_as_or_guest = scope:actor }
		}
		NOR = {
			scope:recipient.top_liege = scope:actor
			scope:recipient.liege = scope:actor
			scope:recipient = scope:actor
			scope:recipient = { is_imprisoned = yes }
		}
	}

	is_valid_showing_failures_only = {
		scope:actor = {
			custom_description = {
				text = magister_trait_2_required_trigger
				has_trait_rank = {
					trait = magister_trait_group
					rank >= 2
				}
			}
		}
	}

	desc = revela_secretum_interaction_desc

	on_accept = {
		custom_tooltip = revela_secretum_interaction_tooltip
		scope:recipient = {
			save_scope_as = revela_recipient
			if = {
				limit = {
					any_secret = { # There's something to discover
						NOR = {
							is_known_by = scope:actor
							secret_target ?= scope:recipient
						}
					}
				}
				random_secret = {
					limit = {
						NOR = {
							is_known_by = scope:actor
							secret_target ?= scope:recipient
						} # Hilarious as it is, they shouldn't be revealing their own secrets.
					}
					weight = {
						base = -1
						modifier = {
							add = 5
							is_criminal_for = secret_owner
						}
					}
					save_scope_as = secret_to_reveal
				}
			}
		}
		if = {
			limit = {
				exists = scope:secret_to_reveal
			}
			scope:actor = {
				trigger_event = {
					id = revela_secretum.0001 # Move to decision
					days = { 2 10 }
				}
			}
		}
		else = {
			if = {
				limit = {
					scope:recipient = {
						has_perk = truth_is_relative_perk # Mulsa is inventive
					}
				}
				scope:recipient = {
					hidden_effect = {
						# Start by adding everyone that this character could trick to a list
						add_to_list = secret_setup
						every_spouse = {
							add_to_list = secret_setup
						}
						every_vassal_or_below = {
							add_to_list = secret_setup
						}
						every_close_family_member = {
							add_to_list = secret_setup
						}
						every_courtier_or_guest = {
						    add_to_list = secret_setup
						}
						every_relation = {
						        type = potential_friend
							add_to_list = secret_setup
						}
						random_in_list = { # Target a vassal or liege. They must be of age.
							list = secret_setup
							limit = {
								NOR = {
									has_trait = devoted_trait_group
									is_adult = no
								}
							}
							save_scope_as = revela_target
							random = {
								chance = 20
								save_scope_value_as = {
									name = strong_hook_against_ruler
									value = yes
								}
							}
						}
					}
				}
				scope:actor = {
					trigger_event = {
						on_action = revela_secretum_fabricate_effect
						days = { 10 30 }
					}
				}
			}
			else = {
				scope:recipient = {
					hidden_effect = {
						# Start by adding everyone that this character could trick to a list
						add_to_list = secret_setup
						every_spouse = {
							add_to_list = secret_setup
						}
						every_vassal_or_below = {
							add_to_list = secret_setup
						}
						every_close_family_member = {
							add_to_list = secret_setup
						}
						every_courtier_or_guest = {
						    add_to_list = secret_setup
						}
						every_relation = {
						        type = potential_friend
							add_to_list = secret_setup
						}
					random_in_list = { # Target a vassal or liege
						list = secret_setup
						limit = {
							NOR = {
								has_trait = devoted_trait_group
								is_adult = no
							}
						}
						save_scope_as = revela_target
							random = {
								chance = 20
								save_scope_value_as = {
									name = strong_hook_against_ruler
									value = yes
								}
							}
						}
					}
				}
				scope:actor = {
					trigger_event = {
						id = revela_secretum.0002 # Take nothing or alter mulsa.
						days = { 10 30 }
					}
				}
			}
		}
	}
	auto_accept = yes
}
