############################################
#
# Assassins decisions for Monks and Mystics
#
############################################

society_decisions = {

	assassins_borrow_money = {
		ai_check_interval = 80

		only_playable = yes

		potential = {
			society_rank = {
				society = the_assassins
				rank < 4
			}

			NOT = { has_character_modifier = assassins_debt_modifier }
		}

		allow = {
			custom_tooltip = {
				text = assassins_borrow_money_tooltip

				society = {
					leader = {
						trigger_if = {
							limit = {
								ai = yes
								has_landed_title = d_fedayeen
							}

							wealth >= 50
						}
					}
				}
			}
		}

		effect = {
			letter_event = {
				id = MNM.6000
				tooltip = assassins_borrow_money_effect_tooltip
			}
		}

		ai_will_do = {
			factor = 0.5

			trigger = {
				wealth < 0
			}

			mult_modifier = {
				factor = 0.1
				war = no
			}
		}
	}

	assassins_pay_debt = {
		ai_check_interval = 18

		only_playable = yes

		potential = {
			has_character_modifier = assassins_debt_modifier
			NOT = { has_character_flag = block_payback }

			the_assassins = {
				leader = { always = yes }
			}
		}

		allow = {
			wealth >= assassins_debt
		}

		effect = {
			pay_back_assassins_effect = yes
			remove_assassins_debt_effect = yes
		}

		ai_will_do = {
			factor = 0.5

			trigger = {
				NOT = { has_character_flag = assassins_refused_to_pay_debt }

				trigger_if = {
					limit = { monthly_income < 0 }
					wealth > twice_assassins_debt
				}
			}

			mult_modifier = {
				factor = 0.1
				trait = greedy
			}

			mult_modifier = {
				factor = 0.1
				war = yes
			}
		}
	}

	assassins_donate = {
		ai_check_interval = 120

		only_playable = yes

		potential = {
			society_member_of = the_assassins
			society_rank < 4
		}

		allow = {
			wealth >= 200
		}

		effect = {
			tooltip = {
				wealth = -200
				add_society_currency_minor_effect = yes
			}

			letter_event = { id = MNM.6005 }
		}

		ai_will_do = {
			factor = 0.1

			trigger = {
				wealth >= 300
			}

			mult_modifier = {
				factor = 0.1
				trait = greedy
			}

			mult_modifier = {
				factor = 0.1
				war = yes
			}
		}
	}

	assassins_raise_troops = {
		ai_check_interval = 24

		is_high_prio = yes
		is_mercenary = yes

		only_playable = yes

		potential = {
			society_rank = {
				society = the_assassins
				rank == 4
			}
		}

		allow = {
			war = yes
			has_society_currency_major_trigger = yes

			trigger_if = {
				limit = { has_character_modifier = assassins_raise_troops_cooldown }

				custom_tooltip = {
					text = assassins_raise_troops_cooldown_TT
					always = no
				}
			}
		}

		effect = {
			sound_effect = generic_click_01
			detract_society_currency_major_effect = yes

			spawn_unit = {
				owner = ROOT
				province = capital_scope
				home = capital_scope
				attrition = 1.0
				disband_on_peace = yes
				maintenance_multiplier = 1
				earmark = assassin_troops

				troops = {
					archers = { 1500 1500 }
					light_infantry = { 2500 2500 }
					light_cavalry = { 1000 1000 }
				}
			}

			hidden_effect = {
				add_character_modifier = {
					name = assassins_raise_troops_cooldown
					years = 1
					hidden = yes
				}
			}
		}

		ai_will_do = {
			factor = 0 # decisions with "is_mercenary = yes" are not calculated from script
		}
	}

	assassins_raise_ships = {
		ai_check_interval = 24

		is_high_prio = yes
		is_mercenary = yes

		only_playable = yes

		potential = {
			society_rank = {
				society = the_assassins
				rank >= 3
			}
		}

		allow = {
			war = yes
			has_society_currency_medium_trigger = yes
		}

		effect = {
			sound_effect = generic_click_01
			detract_society_currency_medium_effect = yes

			spawn_fleet = {
				province = closest
				owner = ROOT
				disband_on_peace = yes
				earmark = assassin_ships

				troops = {
					galleys = { 45 45 }
				}
			}
		}

		ai_will_do = {
			factor = 0 # decisions with "is_mercenary = yes" are not calculated from script
		}
	}

	assassins_drug_effect = {
		ai = no

		potential = {
			society_member_of = the_assassins
			NOT = { has_character_flag = assassins_taking_drugs }
		}

		allow = {
			has_society_currency_minor_trigger = yes
		}

		effect = {
			detract_society_currency_minor_effect = yes

			custom_tooltip = {
				text = assassins_drug_decision_tt
				remove_character_modifier = withdrawal_symptoms
			}

			character_event = { id = MNM.6800 }
		}

		ai_will_do = {
			factor = 0
		}
	}
}

targeted_decisions = {

	# Action: recruit assassins
	assassins_recruit = {
		ai_target_filter = realm
		ai_check_interval = 24

		is_in_society = yes

		from_potential = {
			society_member_of = the_assassins
			prisoner = no
			is_incapable = no
			NOT = { has_character_modifier = recruitment_timer }

			# Only level 2 and 3 AIs recruit
			trigger_if = {
				limit = { ai = yes }

				OR = {
					society_rank == 2
					society_rank == 3
				}
			}
		}

		potential = {
			OR = {
				FROM = { ai = no }
				ai = no
			}

			is_adult = yes
			prisoner = no
			is_incapable = no
			is_in_society = no

			trigger_if = {
				limit = { is_female = yes }
				has_gender_equal_game_rule_trigger = yes
			}

			trigger_if = {
				limit = { FROM = { ai = yes } }

				# CleanSlate: The below is never relevant as one party must be a player
				# AI cannot invite other AIs when society is full
				# trigger_if = {
				#	limit = { ai = yes }
				#	can_join_society = the_assassins
				#
				#	NAND = {
				#		trait = zealous
				#		NOT = { true_religion_group = muslim }
				#	}
				# }

				# Stopping the ai from making stupid choices
				OR = {
					is_friend = FROM
					is_lover = FROM
					is_close_relative = FROM
					interested_in_society = the_assassins
				}
			}
			# AI only checks inside realm, so is by definition in range
			trigger_else = {
				is_within_diplo_range = FROM
			}
		}

		allow = {
			FROM = {
				custom_tooltip = {
					text = recruit_delay_timer_tooltip

					NOT = { has_character_modifier = recruitment_delay_timer }
				}
			}

			ROOT = {
				has_assassins_prerequisites = yes
				is_inaccessible_trigger = no

				custom_tooltip = {
					text = assassins_recruit_block_tooltip

					trigger_if = {
						limit = { has_character_flag = assassins_attempted_recruitment }
						interested_in_society = the_assassins
					}

					NOT = { has_character_flag = ongoing_recruitment }
				}
			}
		}

		effect = {
			hidden_effect = {
				set_character_flag = assassins_attempted_recruitment
				set_character_flag = ongoing_recruitment

				FROM = {
					if = {
						limit = { ai = yes }

						add_character_modifier = {
							name = recruitment_timer
							years = 2
							hidden = yes
						}
					}

					add_character_modifier = {
						name = recruitment_delay_timer
						months = 3
						hidden = yes
					}

					save_event_target_as = assassins_recruiter
				}

				if = {
					limit = { religion = shiite }

					character_event = {
						id = MNM.6022
						days = 3
					}
				}
				else = {
					character_event = {
						id = MNM.6020
						days = 3
					}
				}

				letter_event = {
					id = MNM.7030
					years = 1
				}
			}
		}

		ai_will_do = {
			factor = 0.1

			trigger = {
				# Don't recruit characters interested in something else
				trigger_if = {
					limit = { is_interested_in_any_society = yes }
					interested_in_society = the_assassins
				}
			}

			mult_modifier = {
				factor = 10 # recruit interested parties
				interested_in_society = the_assassins
			}

			mult_modifier = {
				factor = 0.1
				NOT = { religion_group = muslim }
			}
		}
	}

	# Power: scare ruler
	assassins_scare_ruler = {
		ai_target_filter = realm
		ai_check_interval = 60

		is_in_society = yes
		only_playable = yes

		from_potential = {
			has_dlc = "Conclave"

			society_rank = {
				society = the_assassins
				rank >= 2
			}

			is_adult = yes
			is_incapable = no
			prisoner = no
		}

		potential = {
			is_ruler = yes
			age >= 10

			# Ai only checks realm, so by definition in range
			trigger_if = {
				limit = { FROM = { ai = no } }
				is_within_diplo_range = FROM
			}
		}

		allow = {
			ROOT = {
				prisoner = no
				is_inaccessible_trigger = no

				NOR = {
					society_member_of = the_assassins
					owes_favor_to = FROM

					has_opinion_modifier = {
						name = opinion_threatened_me
						who = FROM
					}
				}
			}

			FROM = {
				has_society_currency_medium_trigger = yes
				is_inaccessible_trigger = no

				OR = {
					society_rank == 4
					same_realm = ROOT
				}
			}
		}

		effect = {
			FROM = {
				show_scope_change = no

				detract_society_currency_medium_effect = yes
			}

			random_list = {
				50 = {
					mult_modifier = {
						factor = 9
						FROM = { society_rank == 4 }
					}

					mult_modifier = {
						factor = 3
						FROM = { society_rank == 3 }
					}

					character_event = {
						id = MNM.6840 # Success
						days = 1
					}

					reverse_add_favor = FROM
				}

				50 = {
					# Makes it harder to threaten someone above you
					mult_modifier = {
						factor = 3
						higher_real_tier_than = FROM
					}

					# Makes it even harder to threaten someone 2 steps above you
					mult_modifier = {
						factor = 3

						trigger_if = {
							limit = { real_tier = EMPEROR }
							FROM = { lower_real_tier_than = KING }
						}
						trigger_else = {
							real_tier = KING
							FROM = { lower_real_tier_than = DUKE }
						}
					}

					custom_tooltip = {
						text = assassins_scare_ruler_fail

						FROM = {
							character_event = {
								id = MNM.6980 # Failure
								days = 1
							}
						}

						opinion = {
							name = opinion_threatened_me
							who = FROM
							years = 5
						}
					}
				}
			}
		}

		ai_will_do = {
			factor = 0.5

			trigger = {
				OR = {
					vassal_of = FROM
					is_liege_of = FROM
				}

				NOR = {
					is_friend = FROM
					is_lover = FROM
				}
			}

			mult_modifier = {
				factor = 0.1
				FROM = { is_benevolent_trigger = yes }
			}

			mult_modifier = {
				factor = 2
				FROM = { is_evil_trigger = yes }
			}
		}
	}

	# Power: mark for death
	assassins_mark_for_death = {
		ai_target_filter = realm
		ai_check_interval = 24

		is_in_society = yes
		only_playable = yes

		from_potential = {
			society_rank = {
				society = the_assassins
				rank == 4
			}

			is_adult = yes
			prisoner = no
		}

		potential = {
			is_adult = yes

			# Limits for performance and sense-making
			trigger_if = {
				limit = { FROM = { ai = yes } }

				OR = {
					is_foe = FROM

					reverse_opinion = {
						who = FROM
						value < -30
					}

					holds_favor_on = FROM

					# Unfaithful spouse
					trigger_if = {
						limit = { is_married = FROM }

						any_lover = {
							NOT = { character = FROM }
						}
					}

					# Lover of lover or spouse
					any_lover = {
						OR = {
							is_married = FROM
							is_lover = FROM
						}
					}

					# Someone sharing claims
					any_claim = { claimed_by = FROM }

					trigger_if = {
						limit = { is_ruler = yes }

						NOT = { true_religion = shiite }

						OR = {
							trait = zealous
							trait = cruel
							trait = arbitrary
						}
					}
				}
			}
			trigger_else = {
				is_within_diplo_range = FROM
			}
		}

		allow = {
			custom_tooltip = {
				text = assassins_mark_for_death_allow_tooltip

				FROM = {
					has_society_currency_medium_trigger = yes
					NOT = { has_character_flag = assassins_marked_someone_for_death }
				}
			}
		}

		effect = {
			custom_tooltip = { text = assassins_mark_for_death_effect_tooltip }

			set_character_flag = assassins_target_marked_for_death # TODO: Convert to persistent event target of the society?

			FROM = {
				show_scope_change = no

				detract_society_currency_medium_effect = yes
				set_character_flag = assassins_marked_someone_for_death
				character_event = { id = MNM.6842 }
			}
		}

		ai_will_do = {
			factor = 0.2

			trigger = {
				NOR = {
					is_friend = FROM
					is_lover = FROM
				}
			}

			mult_modifier = {
				factor = 0.1
				FROM = { is_benevolent_trigger = yes }
			}

			mult_modifier = {
				factor = 4
				FROM = { is_evil_trigger = yes }
			}

			mult_modifier = {
				factor = 2
				FROM = { trait = ambitious }
			}
		}
	}

	# Action: Clear marked for death
	assassins_clear_marked_for_death_target = {
		ai = no

		from_potential = {
			society_rank = {
				society = the_assassins
				rank == 4
			}

			is_adult = yes
			prisoner = no
		}

		potential = {
			has_character_flag = assassins_target_marked_for_death
		}

		allow = {
			custom_tooltip = {
				text = assassins_clear_marked_for_death_target_allow_tooltip
				always = yes
			}
		}

		effect = {
			custom_tooltip = {
				text = assassins_clear_marked_for_death_target_effect_tooltip

				clr_character_flag = assassins_target_marked_for_death
				FROM = { clr_character_flag = assassins_marked_someone_for_death }
			}
		}

		ai_will_do = {
			factor = 0
		}
	}

	# Action: (Mission only: Train Protege)
	train_assassin_protege = {
		filter = home_court
		ai_target_filter = home_court
		ai_check_interval = 36

		is_high_prio = yes

		is_in_society = yes
		only_playable = yes

		from_potential = {
			society_member_of = the_assassins
			has_quest = quest_the_assassins_protege
			prisoner = no
			has_regent = no
			NOT = { has_character_flag = training_protege }
		}

		potential = {
			prisoner = no
		}

		allow = {
			# host = { character = FROM }
			age >= 12
			age < 21
			is_ruler = no
			is_abroad = no
			is_married = no

			trigger_if = {
				limit = { is_in_society = yes }
				is_in_society = no
			}

			is_inaccessible_trigger = no
			FROM = { is_inaccessible_trigger = no }

			custom_tooltip = {
				text = characters_are_not_busy_tt

				war = no
				NOT = { has_character_flag = do_not_disturb } # TODO: redundant with is_inaccessible_trigger

				FROM = {
					war = no
					NOT = { has_character_flag = do_not_disturb }
				}
			}
		}

		effect = {
			save_event_target_as = assassin_protege

			FROM = {
				set_character_flag = is_using_train_protege_decision
				character_event = { id = MNM.6250 }
			}
		}

		ai_will_do = {
			factor = 0.3

			# If the target has already recruited someone
			trigger = {
				NOT = {
					any_child = {
						same_society_as = ROOT
					}
				}
			}
		}
	}
}