﻿action_dying = {
	type = alert

	check_create_action = {
		
		if = {
			limit = {
				health <= death_chance_dying_health
			}
			try_create_important_action = {
				important_action_type = action_dying
				actor = root
			}
		}
		
	}

	effect = {
		scope:actor = {
			open_view_data = {
				view = character
			}
		}
	}
}

action_can_assign_guardian = {
	priority = 350
	combine_into_one = yes
	check_create_action = {
		every_close_or_extended_family_member = {
			limit = {
				is_adult = no
				OR = {
					is_child_of = root
					is_grandchild_of = root
					is_great_grandchild_of = root
				}
				num_of_relation_guardian = 0
				age >= childhood_education_start_age
				liege ?= root
				root = {
					is_character_interaction_valid = {
						recipient = prev
						interaction = educate_child_interaction
					}
				}
				trigger_if = {
					limit = {
						root.primary_title ?= {
							place_in_line_of_succession = {
								target = prev
								value >= 3
							}
						}
					}
					NOR = {
						exists = root.court_position:court_tutor_court_position
						exists = root.court_position:court_guru_court_position
					}
				}
			}
			try_create_important_action = {
				important_action_type = action_can_assign_guardian
				actor = root
				recipient = this
				secondary_recipient = this
			}
		}
		every_warden_hostage = {
			limit = {
				is_adult = no
				num_of_relation_guardian = 0
				age >= childhood_education_start_age
				root = {
					is_character_interaction_valid = {
						recipient = prev
						interaction = educate_child_interaction
					}
				}
			}
			try_create_important_action = {
				important_action_type = action_can_assign_guardian
				actor = root
				recipient = this
				secondary_recipient = this
			}
		}
	}

	unimportant = {
		root.primary_title ?= {
			place_in_line_of_succession = {
				target = scope:recipient
				value > 3
			}
		}
	}

	effect = {
		open_interaction_window = {
			interaction = educate_child_interaction
			actor = scope:actor
			recipient = scope:actor
			secondary_recipient = scope:secondary_recipient
		}
	}
}

action_can_ransom = {
	combine_into_one = yes
	check_create_action = {
		#Unop Limit to rulers without fp1_pillage_legacy_3
		if = {
			limit = {
				OR = {
					NOT = { exists = dynasty }
					AND = {
						exists = dynasty
						dynasty = { NOT = { has_dynasty_perk = fp1_pillage_legacy_3 } }
					}
				}
			}
			every_prisoner = {
				limit = {
					OR = {
						AND = {
							OR = {
								is_pool_guest = yes
								is_ruler = yes
							}
							gold > 10
						}
						AND = {
							is_ruler = no
							liege ?= {
								gold > 10
							}
						}
					}
					NOT = { is_at_war_with = root }
					root = {
						is_character_interaction_potentially_accepted = {
							recipient = prev
							interaction = ransom_interaction
						}
					}
					# Can't ransom to self
					NOT = { liege ?= root }
				}
				try_create_important_action = {
					important_action_type = action_can_ransom
					actor = root
					recipient = this
				}
			}
		}
	}

	effect = {
		open_interaction_window = {
			interaction = ransom_interaction
			actor = scope:actor
			recipient = scope:recipient
		}
	}
}

#Unop Add action_can_ransom_increased
action_can_ransom_increased = {
	combine_into_one = yes
	check_create_action = {
		if = {
			limit = {
				exists = dynasty
				dynasty = { has_dynasty_perk = fp1_pillage_legacy_3 }
			}
			every_prisoner = {
				limit = {
					OR = {
						AND = {
							OR = {
								is_pool_guest = yes
								is_ruler = yes
							}
							gold > 10
						}
						AND = {
							is_ruler = no
							exists = liege
							liege = {
								gold > 10
							}
						}
					}
					NOT = { is_at_war_with = root }
					root = {
						is_character_interaction_potentially_accepted = {
							recipient = prev
							interaction = ransom_interaction
						}
					}
					# Can't ransom to self
					NOT = { liege ?= root }
				}
				try_create_important_action = {
					important_action_type = action_can_ransom_increased
					actor = root
					recipient = this
				}
			}
		}
	}

	effect = {
		open_interaction_window = {
			interaction = ransom_interaction
			actor = scope:actor
			recipient = scope:recipient
		}
	}
}

action_should_pay_ransom = {
	combine_into_one = yes
	is_dangerous = yes
	check_create_action = {
		every_close_family_member = { 
			limit = {
				is_imprisoned = yes
				root.primary_title ?= {
					place_in_line_of_succession = {
						target = prev
						value <= 3
					}
				}
				# Don't pay ransoms of own prisoners
				imprisoner != root
			}
			try_create_important_action = {
				important_action_type = action_should_pay_ransom
				actor = root
				recipient = this.imprisoner
				secondary_recipient = this
			}
		}
	}

	effect = {
		open_interaction_window = {
			interaction = pay_ransom_interaction
			actor = scope:actor
			recipient = scope:recipient
			secondary_recipient = scope:secondary_recipient
		}
	}
}

action_vassal_ransom_gives_hook = {
	combine_into_one = yes
	check_create_action = {
		every_vassal = {
			limit = {
				is_imprisoned = yes
				government_has_flag = government_is_feudal
				subject_contract_is_blocked_from_modification = no
				is_ai = yes
				ai_honor >= 25
				ai_greed <= 0
				root = {
					can_add_hook = {
						target = prev
						type = favor_hook
					}
				}
				# Don't pay ransoms of own prisoners
				imprisoner != root
			}
			try_create_important_action = {
				important_action_type = action_vassal_ransom_gives_hook
				actor = root
				recipient = this.imprisoner
				secondary_recipient = this
			}
		}
	}
	
	unimportant = {
		always = yes
	}

	effect = {
		open_interaction_window = {
			interaction = pay_ransom_interaction
			actor = scope:actor
			recipient = scope:recipient
			secondary_recipient = scope:secondary_recipient
		}
	}
}

action_can_vassalize = {
	priority = 900
	combine_into_one = yes
	check_create_action = {
		every_neighboring_top_liege_realm_owner = {
			limit = {
				root = {
					is_character_interaction_potentially_accepted = {
						recipient = prev
						interaction = offer_vassalization_interaction
					}
				}
				NOT = { has_global_variable = tutorial_current_situation_gradual_reveal_1 }
			}
			try_create_important_action = {
				important_action_type = action_can_vassalize
				actor = root
				recipient = this
			}
		}
		
	}

	effect = {
		open_interaction_window = {
			interaction = offer_vassalization_interaction
			actor = scope:actor
			recipient = scope:recipient
		}
	}
}

action_can_propose_alliance_to_family_member = {
	priority = 300
	combine_into_one = yes
	check_create_action = {
		every_close_or_extended_family_member = {
			if = {
				limit = {
					root = {
						is_character_interaction_potentially_accepted = {
							recipient = prev
							interaction = negotiate_alliance_interaction
						}
					}
				}
				try_create_important_action = {
					important_action_type = action_can_propose_alliance_to_family_member
					actor = root
					recipient = this
				}
			}
			every_spouse = {
				if = {
					limit = {
						root = {
							is_character_interaction_potentially_accepted = {
								recipient = prev
								interaction = negotiate_alliance_interaction
							}
						}
					}
					try_create_important_action = {
						important_action_type = action_can_propose_alliance_to_family_member
						actor = root
						recipient = this
					}
				}
			}
		}
	}

	unimportant = {
		any_ally = {
			count >= 2
		}
	}

	effect = {
		open_interaction_window = {
			interaction = negotiate_alliance_interaction
			actor = scope:actor
			recipient = scope:recipient
		}
		scope:recipient = {
			open_view_data = {
				view = character
			}
		}
	}
}

action_can_potentially_propose_alliance_to_family_member = {
	priority = 300
	combine_into_one = yes
	check_create_action = {
		every_close_or_extended_family_member = {
			if = {
				limit = {
					root = {
						is_character_interaction_valid = {
							recipient = prev
							interaction = negotiate_alliance_interaction
						} 
						NOT = {
							is_character_interaction_potentially_accepted = {
								recipient = prev
								interaction = negotiate_alliance_interaction
							}
						}
					}
				}
				try_create_important_action = {
					important_action_type = action_can_potentially_propose_alliance_to_family_member
					actor = root
					recipient = this
				}
			}
			every_spouse = {
				if = {
					limit = {
						root = {
							is_character_interaction_valid = {
								recipient = prev
								interaction = negotiate_alliance_interaction
							} 
							NOT = {
								is_character_interaction_potentially_accepted = {
									recipient = prev
									interaction = negotiate_alliance_interaction
								}
							}
						}
					}
					try_create_important_action = {
						important_action_type = action_can_potentially_propose_alliance_to_family_member
						actor = root
						recipient = this
					}
				}
			}
		}
	}

	unimportant = {
		any_ally = {
			count >= 2
		}
	}

	effect = {
		open_interaction_window = {
			interaction = negotiate_alliance_interaction
			actor = scope:actor
			recipient = scope:recipient
		}
		scope:recipient = {
			open_view_data = {
				view = character
			}
		}
	}
}

action_can_demand_payment = {
	combine_into_one = yes
	check_create_action = {
		every_hooked_character = {
			limit = {
				root = {
					is_character_interaction_potentially_accepted = {
						recipient = prev
						interaction = demand_payment_interaction
					}
				}
			}
			try_create_important_action = {
				important_action_type = action_can_demand_payment
				actor = root
				recipient = this
			}
		}
	}

	effect = {
		open_interaction_window = {
			interaction = demand_payment_interaction
			actor = scope:actor
			recipient = scope:recipient
		}
	}
}

action_can_ask_hof_for_money = {
	priority = 900
	check_create_action = {
		if = {
			limit = {
				exists = faith.religious_head
				faith.religious_head = {
					root = {
						is_character_interaction_potentially_accepted = {
							recipient = prev
							interaction = hof_ask_for_gold_interaction
						}
					}
				}
			}
			faith.religious_head = {
				try_create_important_action = {
					important_action_type = action_can_ask_hof_for_money
					actor = root
					recipient = this
				}
			}
		}
	}

	effect = {
		open_interaction_window = {
			interaction = hof_ask_for_gold_interaction
			actor = scope:actor
			recipient = scope:recipient
		}
	}
}

action_can_ask_hof_for_claim = {
	priority = 900
	combine_into_one = yes
	
	check_create_action = {
		if = {
			limit = {
				exists = faith.religious_head
			}
			if = {
				limit = {
					any_kingdom = {
						exists = holder
						root = {
							is_character_interaction_potentially_accepted = {
								recipient = faith.religious_head
								secondary_recipient = prev.holder
								target_title = prev
								interaction = hof_ask_for_claim_interaction
							}
						}
					}
				}
				every_kingdom = {
					limit = {
						holder ?= { in_diplomatic_range = root }
						root = {
							is_character_interaction_potentially_accepted = {
								recipient = faith.religious_head
								secondary_recipient = prev.holder
								target_title = prev
								interaction = hof_ask_for_claim_interaction
							}
						}
					}

					try_create_important_action = {
						important_action_type = action_can_ask_hof_for_claim
						actor = root
						recipient = holder
						secondary_recipient = root.faith.religious_head
						target_title = this
					}
				}
			}
			if = {
				limit = {
					top_liege != this
					top_liege ?= {
						any_sub_realm_title = {
							tier > tier_county
							exists = holder
							holder != root
							root = {
								is_character_interaction_potentially_accepted = {
									recipient = faith.religious_head
									secondary_recipient = prev.holder
									target_title = prev
									interaction = hof_ask_for_claim_interaction
								}
							}
						}
					}
				}
				top_liege = {
					every_sub_realm_title = {
						limit = {
							tier > tier_county
							exists = holder
							holder != root
							root = {
								is_character_interaction_potentially_accepted = {
									recipient = faith.religious_head
									secondary_recipient = prev.holder
									target_title = prev
									interaction = hof_ask_for_claim_interaction
								}
							}
						}

						try_create_important_action = {
							important_action_type = action_can_ask_hof_for_claim
							actor = root
							recipient = holder
							secondary_recipient = root.faith.religious_head
							target_title = this
						}
					}
				}
			}
			if = {
				limit = {
					any_neighboring_top_liege_realm_owner = {
						any_held_title = {
							title_tier > county
							root = {
								is_character_interaction_potentially_accepted = {
									recipient = faith.religious_head
									secondary_recipient = prev.holder
									target_title = prev
									interaction = hof_ask_for_claim_interaction
								}
							}
						}
					}
				}
				every_neighboring_top_liege_realm_owner = {
					every_held_title = {
						title_tier > county
						limit = {
							root = {
								is_character_interaction_potentially_accepted = {
									recipient = faith.religious_head
									secondary_recipient = prev.holder
									target_title = prev
									interaction = hof_ask_for_claim_interaction
								}
							}
						}

						try_create_important_action = {
							important_action_type = action_can_ask_hof_for_claim
							actor = root
							recipient = holder
							secondary_recipient = root.faith.religious_head
							target_title = this
						}
					}
				}
			}
			if = {
				limit = {
					any_neighboring_top_liege_realm_owner = {
						any_sub_realm_title = {
							tier > tier_county
							root = {
								is_character_interaction_potentially_accepted = {
									recipient = faith.religious_head
									secondary_recipient = prev.holder
									target_title = prev
									interaction = hof_ask_for_claim_interaction
								}
							}
						}
					}
				}
				every_neighboring_top_liege_realm_owner = {
					every_sub_realm_title = {
						limit = {
							tier > tier_county
							root = {
								is_character_interaction_potentially_accepted = {
									recipient = faith.religious_head
									secondary_recipient = prev.holder
									target_title = prev
									interaction = hof_ask_for_claim_interaction
								}
							}
						}

						try_create_important_action = {
							important_action_type = action_can_ask_hof_for_claim
							actor = root
							recipient = holder
							secondary_recipient = root.faith.religious_head
							target_title = this
						}
					}
				}
			}
		}
	}
	effect = {
		open_interaction_window = {
			interaction = hof_ask_for_claim_interaction
			actor = scope:actor
			recipient = scope:secondary_recipient
			secondary_recipient = scope:recipient
			target_title = scope:target_title
		}
	}
}

action_hook_expiring = {
	combine_into_one = yes
	check_create_action = {
		every_hooked_character = {
			limit = {
				root = {
					time_to_hook_expiry = {
						target = prev
						value < 90
					}
				}
			}
			try_create_important_action = {
				important_action_type = action_hook_expiring
				actor = root
				recipient = this
			}
		}
	}

	effect = {
		scope:recipient = {
			open_view_data = {
				view = character
			}
		}
	}
}
