﻿call_ally_interaction = {
	category = interaction_category_diplomacy
	desc = call_ally_interaction_desc
	interface_priority = 60
	interface = call_ally
	special_interaction = call_ally_interaction
	popup_on_receive = yes
	pause_on_receive = yes

	greeting = positive
	notification_text = CALL_ALLY_NOTIFICATION

	is_shown = {
		scope:actor = {
			is_at_war = yes
		}
		scope:recipient = {
			is_allied_to = scope:actor
		}
	}
	
	has_valid_target = {
	}

	has_valid_target_showing_failures_only = {
		exists = scope:target

		###Triggers to make sure the recipient doesn't end up in a war with someone they already have a war with###

		#Set the primary enemy
		trigger_if = {
			limit = { scope:target = { primary_attacker = scope:actor } }
			scope:target = { primary_defender = { save_temporary_scope_as = primary_enemy } }
		}
		trigger_else = {
			scope:target = { primary_attacker = { save_temporary_scope_as = primary_enemy } }
		}

		scope:target = {
			is_war_leader = scope:actor
		}
		
		##custom_description = {
			##text = cannot_call_liege_ally_against_fellow_vassal
			##scope:primary_enemy = {
				##NOT = {
					##target_is_liege_or_above = scope:recipient # Vassals shouldn't be able to call in their liege against fellow vassals...
				##}
			##}
		##}

		scope:recipient = {
			#recipient can't already be at war with the primary_enemy
			custom_description = {
				text = "is_already_war_enemy_of_primary_enemy"
				subject = scope:recipient
				object = scope:primary_enemy
				NOT = {
					any_war_enemy = { this = scope:primary_enemy }
				}
			}
			#recipient can't already be at war with another enemy in target war
			custom_description = {
				text = "is_war_enemy_with_another_target_war_enemy"
				subject = scope:recipient
				NOT = {
					any_war_enemy = {
						NOT = { this = scope:primary_enemy } #Already handled above
						OR = {
							AND = {
								scope:target = { primary_defender = scope:primary_enemy }
								is_defender_in_war = scope:target
							}
							AND = {
								scope:target = { primary_attacker = scope:primary_enemy }
								is_attacker_in_war = scope:target
							}
						}
					}
				}
			}
			#recipient can't have a war enemy who is on actor's side in this war
			custom_description = {
				text = "is_war_enemy_with_my_war_ally"
				subject = scope:recipient
				NOT = {
					any_war_enemy = {
						OR  = {
							AND = {
								scope:target = { primary_defender = scope:actor }
								is_defender_in_war = scope:target
							}
							AND = {
								scope:target = { primary_attacker = scope:actor }
								is_attacker_in_war = scope:target
							}
						}
					}
				}
			}
			#recipient can't be on the same side in another war with primary_enemy
			custom_description = {
				text = "is_war_ally_of_primary_enemy"
				subject = scope:recipient
				object = scope:primary_enemy
				NOT = {
					any_character_war = {
						OR = {
							AND = {
								is_defender = scope:recipient
								any_war_defender = { this = scope:primary_enemy }
							}
							AND = {
								is_attacker = scope:recipient
								any_war_attacker = { this = scope:primary_enemy }
							}
						}
					}
				}
			}
			#recipient can't be on the same side in another war with another enemy in the target war
			custom_description = {
				text = "is_war_ally_of_another_target_enemy"
				subject = scope:recipient
				NOT = {
					any_war_ally = {
						NOT = { this = scope:primary_enemy } #Already handled above
						OR = {
							AND = {
								scope:target = { primary_defender = scope:primary_enemy }
								is_defender_in_war = scope:target
							}
							AND = {
								scope:target = { primary_attacker = scope:primary_enemy }
								is_attacker_in_war = scope:target
							}
						}
					}
				}
			}
		}
	}
	
	is_valid_showing_failures_only = {
		
		scope:recipient = {
			is_ruler = yes
		}

		# Only War Leaders can call allies
		scope:actor = {
			custom_description = {
				text = must_be_war_leader
				any_character_war = {
					is_war_leader = scope:actor
				}
			}
		}

		# Cannot call the same character multiple times
		scope:actor = {
			custom_description = {
				text = cannot_call_same_character_multiple_times
				subject = scope:recipient
				any_character_war = {
					ALL_FALSE = {
						was_called = scope:recipient
						is_participant = scope:recipient
					}
				}
			}
		}
		##custom_description = { ## YJ - Liege CAN call allied vassal to war
			##text = cannot_call_vassal_to_war
			##scope:recipient = {
				##NOT = {
					##target_is_liege_or_above = scope:actor
				##}
			##}
		##}
	}

	can_be_picked = {
		yj_liege_vassal_check_trigger = yes
	  }

	on_auto_accept = {
		scope:recipient = {
			trigger_event = call_ally.0001
		}
	}
		
	on_accept = {
		call_ally_interaction_effect = yes

		save_scope_value_as = {
			name = call_ally_interaction
			value = yes
		}

		scope:actor = {
			hidden_effect = { #To nudge friendship
				if = {
					limit = {
						NOR = {
							has_relation_friend = scope:recipient
							has_relation_potential_friend = scope:recipient
						}
					}
					set_relation_potential_friend = scope:recipient
				}
			}
			trigger_event = call_ally.0100
		}
	}
	
	on_decline = {
		#The war could theoretically end on the day the decline is sent
		if = {
			limit = {
				exists = scope:target
			}
			scope:target = {
				if = {
					limit = {
						is_attacker = scope:actor
					}
					scope:actor = {
						add_opinion = {
							modifier = rejected_call_to_offensive_war
							target = scope:recipient
						}
					}
				}
				else = {
					scope:actor = {
						add_opinion = {
							modifier = rejected_call_to_defensive_war
							target = scope:recipient
						}
					}
				}
				hidden_effect = {
					if = {
						limit = {
							NOT = { was_called = scope:recipient }
						}
						set_called_to = scope:recipient
					}
				}
			}
			scope:actor = {
				trigger_event = call_ally.0101
			}
		}
	}

	auto_accept = {
		OR = {
			# Always accept a call from a Spouse
			custom_description = {
				text = "recipient_is_spouse"
				subject = scope:actor
				object = scope:recipient
				scope:recipient = {
					is_spouse_of = scope:actor
					is_ai = yes
				}
			}
			# Always accept a call from the Heir
			custom_description = {
				text = "is_recipients_player_heir"
				subject = scope:actor
				object = scope:recipient
				
				exists = scope:recipient.player_heir
				scope:recipient = {
					player_heir = scope:actor
					is_close_or_extended_family_of = scope:actor # Only for family
					is_ai = yes
				}
			}
			# Always accept a call from the one you are heir of
			custom_description = {
				text = "is_player_heir"
				subject = scope:recipient
				object = scope:actor

				exists = scope:actor.player_heir
				scope:actor.player_heir = scope:recipient
				scope:actor = { # Shouldn't be able to force your liege to join just because you've not got any family...
					is_close_or_extended_family_of = scope:recipient
				}
				scope:recipient = {
					is_ai = yes
				}
			}
		}
	}
	
	ai_accept = {
		base = 20

		modifier = {  # Refuse call against Heir
			add = -1000
			exists = scope:recipient.player_heir
			trigger_if = {
				limit = { scope:target.casus_belli.primary_defender = scope:actor }
				scope:target.casus_belli.primary_attacker = scope:recipient.player_heir
			}
			trigger_else = {
				scope:target.casus_belli.primary_defender = scope:recipient.player_heir
			}
			desc = WONT_FIGHT_HEIR_REASON
		}

		modifier = {  # Refuse call against Spouse
			add = -1000
			scope:recipient = {
				trigger_if = {
					limit = { scope:target.casus_belli.primary_defender = scope:actor }
					any_spouse = {
						this = scope:target.casus_belli.primary_attacker
					}
				}
				trigger_else = {
					any_spouse = {
						this = scope:target.casus_belli.primary_defender
					}
				}
				
			}
			desc = WONT_FIGHT_SPOUSE_REASON
		}

		opinion_modifier = { # Opinion Factor
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 1.0
			desc = AI_OPINION_REASON
		}

		# Honor factor
		ai_value_modifier = {
			ai_honor = 1
			min = 0
		}

		modifier = {  # Tends to join defensive wars
			add = 50
			scope:target.casus_belli.primary_defender = scope:actor
			desc = DEFENSIVE_WAR_REASON
		}

		compare_modifier = { # Likes fighting infidels
			trigger = {
				scope:recipient.faith = scope:actor.faith
				OR = {
					AND = {
						scope:target.casus_belli.primary_attacker = {
							this = scope:actor
							faith = {
								faith_hostility_level = {
									target = scope:target.casus_belli.primary_defender.faith
									value >= religious_cb_enabled_hostility_level
								}
							}
						}
					}
					AND = {
						scope:target.casus_belli.primary_defender = {
							this = scope:actor
							faith = {
								faith_hostility_level = {
									target = scope:target.casus_belli.primary_defender.faith
									value >= religious_cb_enabled_hostility_level
								}
							}
						}
					}
				}
			}
			target = scope:recipient
			value = ai_zeal
			desc = "ZEAL_AGAINST_INFIDELS"
			min = 0
			multiplier = 0.5
		}

		modifier = {  # Reluctant to attack another ally
			add = -50
			scope:target.casus_belli.primary_attacker = scope:actor
			scope:recipient = {
				is_allied_to = scope:target.casus_belli.primary_defender
			}
			desc = ATTACK_ON_ALLY_REASON
		}

		modifier = {  # Reluctant to defend against another ally
			add = -25
			scope:target.casus_belli.primary_defender = scope:actor
			scope:recipient = {
				is_allied_to = scope:target.casus_belli.primary_attacker
			}
			desc = WAR_WITH_ALLY_REASON
		}

		modifier = {  # Reluctant to join wars against religious brethren.
			add = -50
			NOT = { scope:recipient.faith = scope:actor.faith }
			scope:target.casus_belli.primary_attacker = scope:actor
			scope:target.casus_belli.primary_defender.faith = scope:recipient.faith
			scope:target.casus_belli.war = {
				OR = {
					using_cb = minor_religious_war
					using_cb = religious_war
					using_cb = major_religious_war
					using_cb = undirected_great_holy_war
					using_cb = directed_great_holy_war
				}
			}
			desc = WONT_ATTACK_RELIGIOUS_BRETHREN_REASON
		}
	}
}