﻿is_available_for_activity_trigger = {
	NOT = { exists = involved_activity }
	is_adult = yes
	OR = {
		is_ai = no
		is_busy_in_events_unlocalised = no
	}
	is_at_war = no
	is_imprisoned = no
	is_healthy = yes
	NOR = {
		exists = var:booked_for_a_party
		exists = var:engaged_in_single_combat
	}
}

is_available_for_child_activity_trigger = {
	NOT = { exists = involved_activity }
	is_adult = no
	age >= 4
	age < less_than_two_years_to_adulthood_value #To avoid being away when Coming of Age-event triggers
	OR = {
		is_ai = no
		is_busy_in_events_unlocalised = no
	}
	is_at_war = no
	is_imprisoned = no
	is_hostage = no
	is_healthy = yes
	NOR = {
		exists = var:hostage_travelling_to_warden
		exists = var:ward_travelling_to_guardian
	}
}

is_available_in_activity_trigger = {
	exists = involved_activity
	is_adult = yes
	OR = {
		is_ai = no
		is_busy_in_events_unlocalised = no
	}
	is_at_war = no
	is_imprisoned = no
	is_healthy = yes
}

### Witch Ritual
activity_witch_ritual_valid_trigger = {
	exists = house
	house = {
		has_house_modifier = witch_coven
		any_house_member = {
			NOT = { this = root }
			is_witch_known_by_trigger = { CHARACTER = root }
		}
	}
}

### Petition Liege triggers

# Council Seat
petition_liege_council_seat_liege_trigger = {
	liege ?= {
		OR = {
			cp:councillor_chancellor ?= {
				NOT = {
					is_blocked_from_being_fired_from_council_trigger = { COURT_OWNER = root.liege }
				}
			}
			cp:councillor_marshal ?= {
				NOT = {
					is_blocked_from_being_fired_from_council_trigger = { COURT_OWNER = root.liege }
				}
			}
			cp:councillor_steward ?= {
				NOT = {
					is_blocked_from_being_fired_from_council_trigger = { COURT_OWNER = root.liege }
				}
			}
			cp:councillor_spymaster ?= {
				NOT = {
					is_blocked_from_being_fired_from_council_trigger = { COURT_OWNER = root.liege }
				}
			}
			trigger_if = {
				limit = { root.faith = root.liege.faith }
				root.faith = { has_doctrine = doctrine_clerical_succession_temporal_appointment }
				cp:councillor_court_chaplain ?= {
					NOT = {
						is_blocked_from_being_fired_from_council_trigger = { COURT_OWNER = root.liege }
					}
				}
			}
			NOT = { exists = cp:councillor_chancellor }
			NOT = { exists = cp:councillor_marshal }
			NOT = { exists = cp:councillor_steward }
			NOT = { exists = cp:councillor_spymaster }
			trigger_if = {
				limit = { root.faith = root.liege.faith }
				root.faith = { has_doctrine = doctrine_clerical_succession_temporal_appointment }
				NOT = { exists = cp:councillor_court_chaplain }
			}
		}
	}
}

petition_liege_council_chaplain_appointable_trigger = {
	$LIEGE$.faith = { has_doctrine = doctrine_clerical_succession_temporal_appointment }
	$LIEGE$.faith = $JOB_TAKER$.faith
}

petition_liege_councillor_second_trigger = {
	NOT = { $JOB_TAKER$.var:petition_liege_council_first ?= flag:$ROLE$ }
	trigger_if = {
		limit = { exists = $LIEGE$.cp:councillor_$ROLE$ }
		OR = {
			$JOB_TAKER$.$SKILL$ >= $LIEGE$.cp:councillor_$ROLE$.$SKILL$
			$JOB_TAKER$ = { has_trait = education_$SKILL$ }
		}
	}
	trigger_else = { $JOB_TAKER$.$SKILL$ >= mediocre_skill_rating }
}

petition_liege_councillor_blocked_trigger = {
	$JOB_TAKER$ = {
		can_be_$ROLE$_trigger = { COURT_OWNER = $LIEGE$ }
	}
	$LIEGE$.cp:councillor_$ROLE$ ?= {
		NOT = {
			is_blocked_from_being_fired_from_council_trigger = { COURT_OWNER = $LIEGE$ }
		}
		trigger_if = {
			limit = { exists = scope:eunuch }
			NOT = { is_close_family_of = scope:eunuch }
		}
	}
}

# Pay Debt
petition_liege_pay_debt_trigger = {
	debt_level > -1
	liege.debt_level = -1
}

# War Aid
petition_liege_war_aid_trigger = {
	is_at_war = yes
	save_temporary_scope_as = vassal
	any_character_war = {
		scope:vassal = { is_leader_in_war = prev }
		trigger_if = {
			limit = {
				scope:vassal = { is_attacker_in_war = prev }
			}
			defender_war_score >= petition_war_aid_score_value
		}
		trigger_else = { attacker_war_score >= petition_war_aid_score_value }
	}
}

petition_liege_war_liege_involvement_trigger = {
	liege = {
		NOR = {
			any_character_war = {
				root.liege = { is_leader_in_war = prev }
				trigger_if = {
					limit = {
						root.liege = { is_defender_in_war = prev }
					}
					OR = {
						primary_attacker.primary_title.tier >= root.liege.primary_title.tier
						petition_liege_attacker_strength_value > petition_war_aid_liege_ally_advantage_value
					}
				}
				trigger_else = {
					OR = {
						primary_attacker.primary_title.tier >= root.liege.primary_title.tier
						petition_liege_attacker_strength_value > petition_war_aid_liege_ally_advantage_value
					}
				}
			}
			is_in_ongoing_great_holy_war = yes
		}
	}
}

petition_liege_war_aid_share_trigger = {
	NOT = {
		any_character_war = {
			root = { is_leader_in_war = prev }
			any_war_participant = { this = root.liege }
		}
	}
}

# Vassal War
petition_liege_vassal_war_trigger = {
	any_character_war = {
		root = { is_leader_in_war = prev } # root is a war leader
		primary_attacker.liege = primary_defender.liege # War is between vassals of the same liege
		NOT = {
			root.liege = { is_participant_in_war = prev } # Liege is not involved in the war
		}
	}
}

# Control
petition_liege_county_control_trigger = {
	tier = tier_county
	trigger_if = {
		limit = { tier = tier_county }
		county_control < medium_county_control
	}
}

# Convert
petition_liege_county_convert_trigger = {
	tier = tier_county
	trigger_if = {
		limit = { tier = tier_county }
		NOT = { county.faith = holder.faith }
	}
}

# Opinion
petition_liege_county_opinion_trigger = {
	tier = tier_county
	trigger_if = {
		limit = { tier = tier_county }
		county_opinion <= petition_county_opinion_value
	}
}

# Fortify
petition_liege_fortify_border_trigger = {
	tier = tier_county
	trigger_if = {
		limit = { tier = tier_county }
		county = {
			save_temporary_scope_as = county_scope
			any_title_to_title_neighboring_county = {
				NOT = { holder.top_liege = scope:county_scope.holder.top_liege } # petition_liege
				NOT = {
					holder.top_liege = { is_allied_to = scope:county_scope.holder.top_liege } # petition_liege
				}
			}
		}
	}
}

# Liege Title
petition_liege_title_liege_trigger = {
	holder = prev.liege
	NOT = { THIS = prev.liege.primary_title }
	tier < prev.liege.primary_title.tier
	trigger_if = {
		limit =  { exists = county }
		NOT = { county = holder.capital_county }
	}
	OR = {
		tier > tier_county
		holder = {
			any_held_title = {
				count > 1
				tier = tier_county
			}
		}
	}
}

# Vassal Title
petition_liege_title_vassal_trigger = {
	exists = holder
	holder = { save_temporary_scope_as = second_temp }
	OR = {
		scope:second_temp.liege = prev.liege
		scope:second_temp.liege.liege = prev.liege
		scope:second_temp.liege.liege.liege = prev.liege
	}
	NOR = {
		scope:second_temp = prev.liege
		has_variable = petition_title_cooldown
		scope:second_temp = { is_at_war = yes }
		prev.liege = {
			any_targeting_faction = {
				any_faction_member = { this = scope:second_temp }
			}
		}
	}
	#trigger_if = {
	#	limit =  { exists = county }
	#	NOT = { county = holder.capital_county }
	#}
	scope:second_temp = {
		trigger_if = {
			limit = { government_has_flag = government_is_feudal }
			NOT = { vassal_contract_obligation_level_can_be_decreased = title_revocation_rights }
		}
	}
	OR = {
		tier > tier_county
		scope:second_temp = {
			any_held_title = {
				count > 1
				tier = tier_county
			}
		}
	}
}

# Construct City
petition_liege_construct_city_trigger = {
	county_has_empty_province_trigger = yes
	OR = {
		county_has_no_city_trigger = yes
		county_has_all_holding_types = yes
	}
}

# Construct Temple
petition_liege_construct_temple_trigger = {
	county_has_empty_province_trigger = yes
	OR = {
		county_has_no_church_trigger = yes
		county_has_all_holding_types = yes
	}
}

# AI weightings
petition_liege_medium_cost_trigger = {
	exists = scope:petition_vassal
	scope:petition_vassal = { has_variable = petition_type }
	OR = {
		trigger_if = {
			limit = { scope:petition_vassal.var:petition_type = flag:pay_debt }
			scope:petition_vassal.debt_level < 3
		}
		scope:petition_vassal.var:petition_type = flag:war_aid
		scope:petition_vassal.var:petition_type = flag:vassal_war
		scope:petition_vassal.var:petition_type = flag:title_vassal
		trigger_if = {
			limit = { scope:petition_vassal.var:petition_type = flag:war_aid }
			petitioner_war_aid_levies_cost_value > scope:petition_liege.medium_gold_value
		}
	}						
}

petition_liege_high_cost_trigger = {
	exists = scope:petition_vassal
	scope:petition_vassal = { has_variable = petition_type }
	OR = {
		trigger_if = {
			limit = { scope:petition_vassal.var:petition_type = flag:pay_debt }
			scope:petition_vassal.debt_level >= 3
		}
		scope:petition_vassal.var:petition_type = flag:title_liege
		scope:petition_vassal.var:petition_type = flag:construct_city
		scope:petition_vassal.var:petition_type = flag:construct_temple
		scope:petition_vassal.var:petition_type = flag:development
		trigger_if = {
			limit = { scope:petition_vassal.var:petition_type = flag:war_aid }
			petitioner_war_aid_levies_cost_value > scope:petition_liege.major_gold_value
		}
	}						
}

petition_liege_likes_trigger = {
	exists = $COMPARE$
	OR = {
		has_relation_friend = $COMPARE$
		has_relation_best_friend = $COMPARE$
		has_relation_soulmate = $COMPARE$
		has_relation_lover = $COMPARE$
		is_close_family_of = $COMPARE$
		opinion = {
			target = $COMPARE$
			value >= 50
		}
	}
}

petition_liege_dislikes_trigger = {
	exists = $COMPARE$
	OR = {
		has_relation_rival = $COMPARE$
		has_relation_nemesis = $COMPARE$
		opinion = {
			target = $COMPARE$
			value <= -50
		}
	}
}

# Character is not already involved in the event e.g. claim target, incumbent councillor, etc.
petition_liege_skiller_trigger = {
	is_available_adult = yes
	
	trigger_if = {
		limit = { exists = scope:second_party }
		NOT = { this = scope:second_party }
	}
	trigger_if = {
		limit = { exists = scope:vassal_claimant }
		NOT = { this = scope:vassal_claimant }
	}
	trigger_if = {
		limit = { exists = scope:second_councillor }
		NOT = { this = scope:second_councillor }
	}
	trigger_if = {
		limit = { exists = scope:diplomacy_check }
		NOT = { this = scope:diplomacy_check }
	}
	trigger_if = {
		limit = { exists = scope:martial_check }
		NOT = { this = scope:martial_check }
	}
	trigger_if = {
		limit = { exists = scope:stewardship_check }
		NOT = { this = scope:stewardship_check }
	}
	trigger_if = {
		limit = { exists = scope:intrigue_check }
		NOT = { this = scope:intrigue_check }
	}
	trigger_if = {
		limit = { exists = scope:learning_check }
		NOT = { this = scope:learning_check }
	}
	trigger_if = {
		limit = { exists = scope:petition_vassal }
		NOT = { this = scope:petition_vassal }
	}
}

# Has a councillor or spouse who is not involved in the event already
petition_liege_has_main_skiller_trigger = { 
	scope:petition_liege = {
		OR = {
			trigger_if = {
				limit = { exists = cp:councillor_chancellor }
				cp:councillor_chancellor = { petition_liege_skiller_trigger = yes }
			}
			trigger_else = { always = no }
			
			trigger_if = {
				limit = { exists = cp:councillor_marshal }
				cp:councillor_marshal = { petition_liege_skiller_trigger = yes }
			}
			trigger_else = { always = no }

			trigger_if = {
				limit = { exists = cp:councillor_steward }
				cp:councillor_steward = { petition_liege_skiller_trigger = yes }
			}
			trigger_else = { always = no }

			trigger_if = {
				limit = { exists = cp:councillor_spymaster }
				cp:councillor_spymaster = { petition_liege_skiller_trigger = yes }
			}
			trigger_else = { always = no }

			trigger_if = {
				limit = { exists = cp:councillor_court_chaplain }
				cp:councillor_court_chaplain = { petition_liege_skiller_trigger = yes }
			}
			trigger_else = { always = no }

			trigger_if = {
				limit = {
					is_married = yes
					exists = primary_spouse
				}
				primary_spouse = {
					is_playable_character = no 
					petition_liege_skiller_trigger = yes
					exists = scope:petition_vassal
					NOT = { has_any_scripted_relation = scope:petition_vassal }
				}
			}
			trigger_else = { always = no }
		}
	}
}

# Character can be a wildcard, due to their existing relationship with petitioner
petition_liege_wildcard_trigger = {
	OR = {
		has_relation_friend = scope:petition_vassal
		has_relation_best_friend = scope:petition_vassal
		has_relation_lover = scope:petition_vassal
		has_relation_soulmate = scope:petition_vassal
		opinion = {
			target = scope:petition_vassal
			value >= 50
		}
	}
}

# No more than 2 skill check options already exist
petition_liege_skillcheck_limit = {
	calc_true_if = {
		amount < 2
		exists = scope:diplomacy_check
		exists = scope:martial_check
		exists = scope:stewardship_check
		exists = scope:intrigue_check
		exists = scope:learning_check
	}
}

# Character is valid councillor and invalid wildcard
petition_liege_skiller_no_wildcard_trigger = {
	petition_liege_skiller_trigger = yes
	petition_liege_wildcard_trigger = no
}

# Character has some way to be placated by liege
petition_liege_relation_favor_trigger = {
	can_add_hook = {
		type = favor_hook
		target = scope:petition_liege
	}
	OR = {
		has_relation_friend = scope:petition_liege
		has_relation_best_friend = scope:petition_liege
		has_relation_lover = scope:petition_liege
		has_relation_soulmate = scope:petition_liege
	}
}

# For picking which council position to offer
petition_liege_councillor_replace_weight_trigger = {
	AND = {
		scope:petition_vassal = { has_variable = petition_liege_council_first }
		scope:petition_vassal.var:petition_liege_council_first = flag:$JOB$
		OR = {
			NOT = { has_councillor_for_skill = $SKILL$ }
			AND = {
				exists = scope:second_party
				scope:petition_vassal.$SKILL$ > scope:second_party.$SKILL$
			}
		}
	}
}

# Petition is still valid
petition_liege_valid_trigger = {
	is_alive = yes
	is_imprisoned = no
	has_variable = petition_liege_scope
	liege = var:petition_liege_scope
	var:petition_liege_scope = {
		is_imprisoned = no
		has_royal_court = yes
	}
}

adventurer_potential_destination_distance_check = {
	#This is made to be used inside set_adventure_location_effect - it relies on the scope it is being run in being the adventurer
	#Also expects $REGION$
	any_county_in_region = {
		region = $REGION$
		trigger_if = {
			limit = {
				prev.adventure_inspiration_average_skill_value >= high_inspiration_skill
			}
			NOT = {
				prev.location = {
					geographical_region = $REGION$
				}
			} #Highly skilled adventurers can travel anywhere - and will not pick the region they are already in
		}
		trigger_else_if = {
			limit = {
				prev.adventure_inspiration_average_skill_value >= medium_inspiration_skill
			}
			squared_distance = {
				target = prev.location
				value <= medium_adventure_max_length
			}
		}
		trigger_else = {
			#low_inspiration_skill
			squared_distance = {
				target = prev.location
				value < short_adventure_max_length
			}
		}
	}
}

activity_invite_rule_trigger = {
	is_adult = yes
	is_imprisoned = no
	in_diplomatic_range = root
}

considers_social_presence_a_gift_to_other_trigger = {
	has_trait = improvident
	OR = {
		has_trait = gregarious
		has_trait = arrogant
	}
}

not_hosting_a_murder_feast = {
	trigger_if = {
		limit = {
			this = scope:host
			scope:activity = {
				has_activity_type = activity_feast
			}
		}
		scope:activity = {
			NOT = {
				has_activity_option = {
					category = special_type
					option = feast_type_murder
				}
			}
		}
	}
	trigger_else = {
		always = yes
	}
}

factor_zero_if_entitled_to_freebie_activity_trigger = {
	dynasty ?= { has_dynasty_perk = ep2_activities_legacy_5 }
	this = house.house_head
	NOT = { has_variable = ep2_activities_legacy_5_had_freebie }
}

# to be run in activity scope
has_attending_activity_guests = {
	# We have more than one attending character
	# Host always attends so we ignore them
	any_attending_character = {
		count > 1 # 1 includes the host who always attends
	}
}

any_journey_point_of_interest_trigger = {
	has_special_building = yes
	NOR = {
		has_building_or_higher = generic_university
		has_building_or_higher = hall_of_heroes_01
		has_building_or_higher = special_sicilian_parliament_01
		has_building_or_higher = special_greenhouse_01
		has_building_or_higher = holy_site_cathedral_01
		has_building_or_higher = holy_site_mosque_01
		has_building_or_higher = holy_site_fire_temple_01
		has_building_or_higher = holy_site_pagan_grand_temple_01
		has_building_or_higher = holy_site_indian_grand_temple_01
		has_building_or_higher = holy_site_other_grand_temple_01
		has_building_or_higher = generic_copper_mines_01
		has_building_or_higher = generic_silver_mines_01
		has_building_or_higher = generic_gold_mines_01
	}
}

