﻿

###vanilla overides
this_is_martial_society_trigger = {
	NOR = {
		faith = { has_doctrine = tenet_pacifism }
		faith = { has_doctrine = tenet_dharmic_pacifism }
	}
	OR = {
		has_government = clan_government
		has_government = tribal_government
		has_government = mercenary_government
		has_government = holy_order_government
		has_government = military_command ###imperial mod
		faith = {
			OR = {
				has_doctrine_parameter = great_holy_wars_active
				has_doctrine_parameter = great_holy_wars_active_if_reformed
				has_doctrine_parameter = cheaper_holy_wars_active
			}
		}
	}
}
ere_elective_politician_titles_trigger = { #Used in Byzantine Elective to determine AI behavior.
	OR = {
		has_council_position = councillor_chancellor
		has_council_position = councillor_marshal
		has_council_position = councillor_steward
		has_council_position = councillor_spymaster
		has_council_position = councillor_court_chaplain
	}
}
byzantine_elective_deformed_candidate_trigger = {
	OR = {
		has_trait = disfigured
		has_trait = blind
		has_trait = eunuch
		has_trait = dwarf
		has_trait = clubfooted
		has_trait = inbred
	}
}
title_revocation_is_tyrannical_trigger = {
	NOR = {
		has_revoke_title_reason = scope:recipient
		has_claim_on = scope:landed_title
		faith = {
			OR = {
				AND = {								
					has_doctrine_parameter = pluralism_righteous_revocation_tyranny_reduced
					faith_hostility_level = {
						target = scope:recipient.faith
						value = faith_evil_level
					}
				}
				AND = {								
					has_doctrine_parameter = pluralism_fundamentalist_revocation_tyranny_minimal
					faith_hostility_level = {
						target = scope:recipient.faith
						value >= faith_hostile_level
					}
				}
			}
		}
		
		scope:recipient = {  ###imperial mod
			OR = {
				has_government = military_command 
				has_government = patrician_government
			}	
		} 
	}
}
can_be_granted_titles_by = { #This is set up with trigger-ifs to allow for nice tooltip printouts in grant title interaction
	save_temporary_scope_as = title_grantee
	#Gender check
	trigger_if = {
		limit = {
			is_ruler = no #Rulers can always get additional titles
			is_theocratic_lessee = no #Theocratic lessees use other gender checks
			NOT = {
				$RULER$.faith = {
					has_dominant_ruling_gender = scope:title_grantee
				}
			}
		}
		$RULER$.faith = {
			has_dominant_ruling_gender = scope:title_grantee
		}
	}
	trigger_else_if = { #Military command check
		limit = { 
			is_ruler = no
			$RULER$ = { 
				OR = {
					has_government = military_command
					has_government = imperial_government
					has_government = patrician_government
				}
			}
		}
		eligible_for_military_appointment_trigger = { APPOINTEE = scope:title_grantee }
	}
	trigger_else_if = {
		limit = { is_theocratic_lessee = yes }
		is_theocratic_lessee = yes
		liege = $RULER$
		#If this is ever visible anywhere, we need to write a custom desc "Theocratic lessees cannot only be given titles by their ruler"
	}
	trigger_else = {
		always = yes
	}
}
imprisonment_retribution_start_faction_war_if_valid = {
	scope:recipient = {
		custom_tooltip = FACTION_IMPRISONMENT_RETALIATION_WARNING
	}
	hidden_effect = {
		if = {
			limit = {
				faction_power > faction_power_threshold
				NOT = {	faction_is_type = populist_faction }
			}

			if = {
				limit = {
					faction_is_type = claimant_faction
				}
				special_title = {
					save_scope_as = target_title
				}
				faction_start_war = {
					title = scope:target_title
				}
			}
			else_if = {
				limit = {
					faction_is_type = military_faction
				}
				faction_target = {
					primary_title = {
						save_scope_as = target_title
					}
				}	
				faction_start_war = {
					title = scope:target_title
				}
			}
			else = {
				faction_start_war = {}
			}
			every_faction_member = {
				trigger_event = faction_demand.9001
			}
		}
	}
}
# can_be_chancellor_trigger = {
	# can_be_councillor_basics_trigger = yes
	# NOT = { has_council_position = councillor_spouse }
	# OR = {
		# is_ruler = yes
		# AND = {
			# is_female = yes
			# $COURT_OWNER$.faith = { NOT = { has_doctrine_parameter = male_dominated_council } }
		# }
		# AND = {
			# is_male = yes
			# $COURT_OWNER$.faith = { NOT = { has_doctrine_parameter = female_dominated_council } }
		# }
	# }
	
	# eligible_for_imperial_council_position_trigger = yes
# }

# can_be_steward_trigger = {
	# can_be_councillor_basics_trigger = yes
	# NOT = { has_council_position = councillor_spouse }
	# OR = {
		# is_ruler = yes
		# AND = {
			# is_female = yes
			# $COURT_OWNER$.faith = { NOT = { has_doctrine_parameter = male_dominated_council } }
		# }
		# AND = {
			# is_male = yes
			# $COURT_OWNER$.faith = { NOT = { has_doctrine_parameter = female_dominated_council } }
		# }
	# }
	
	# eligible_for_imperial_council_position_trigger = yes
# }

# can_be_marshal_trigger = { #Strategos can be marshal
	# can_be_councillor_basics_trigger = yes
	# NOT = { has_council_position = councillor_spouse }
	# OR = {
		# is_ruler = yes
		# AND = {
			# is_female = yes
			# $COURT_OWNER$.faith = { NOT = { has_doctrine_parameter = male_dominated_council } }
		# }
		# AND = {
			# is_male = yes
			# $COURT_OWNER$.faith = { NOT = { has_doctrine_parameter = female_dominated_council } }
		# }
	# }
# }

# can_be_spymaster_trigger = {
	# can_be_councillor_basics_trigger = yes
	# NOT = { has_council_position = councillor_spouse }
	# exists = $COURT_OWNER$ #so we can have the argument but get no errors
	
	# eligible_for_imperial_council_position_trigger = yes
# }

can_have_confederate_partition_succession_law_trigger = {
	OR = {
		has_government = feudal_government
		has_government = tribal_government
		has_government = clan_government
		has_government = imperial_government
	}
}
can_have_partition_succession_law_trigger = {
	culture = {
		has_innovation = innovation_hereditary_rule
	}
	custom_description = {
		text = succession_laws_must_have_organised_government_tooltip
		OR = {
			has_government = feudal_government
			has_government = clan_government
			has_government = imperial_government
		}
	}
}
can_have_high_partition_succession_law_trigger = {
	trigger_if = {
		limit = {
			OR = {
				has_culture = culture:occitan
				has_culture = culture:basque
				has_culture = culture:aragonese
				has_culture = culture:catalan
			}
			culture = {
				NOT = { has_innovation = innovation_heraldry }
			}
		}
		culture = { has_innovation = innovation_visigothic_codes }
	}
	trigger_else = {
		culture = { has_innovation = innovation_heraldry }
	}
	custom_description = {
		text = succession_laws_must_have_organised_government_tooltip
		OR = {
			has_government = feudal_government
			has_government = clan_government
			has_government = imperial_government
		}
	}
}

can_have_single_heir_succession_law_trigger = {
	trigger_if = {
		limit = { 
			OR = {
				has_government = military_command
				has_government = patrician_government 
			}	
		}
		always = yes
	}
	trigger_else = {	
		culture = {
			has_innovation = innovation_primogeniture
		}
		# Non-tribals only
		custom_description = {
			text = succession_laws_must_have_organised_government_tooltip
			OR = {
				has_government = feudal_government
				has_government = clan_government
				has_government = imperial_government
			}
		}
	}	
}

can_have_single_heir_youngest_succession_law_trigger = {
	culture = {
		has_innovation = innovation_primogeniture
	}
	# Non-tribals only
	custom_description = {
		text = succession_laws_must_have_organised_government_tooltip
		OR = {
			has_government = feudal_government
			has_government = clan_government
			has_government = imperial_government
		}
	}
}

can_have_single_heir_dynasty_house_trigger = {
	exists = house
	trigger_if = {
		limit = { has_government = patrician_government }
		always = yes
	}
	trigger_else_if = {
		limit = {
			OR = {
				has_culture = culture:czech
				has_culture = culture:slovien
			}
			culture = {
				NOT = {
					has_innovation = innovation_heraldry
				}
			}
		}
		culture = {
			has_innovation = innovation_table_of_princes
		}
	}
	trigger_else = {
		culture = {
			has_innovation = innovation_heraldry
		}
	}
	custom_description = {
		text = succession_laws_must_have_organised_government_tooltip
		OR = {
			has_government = feudal_government
			has_government = clan_government
			has_government = patrician_government
			has_government = imperial_government
		}
	}
}
can_designate_heir_trigger = {	
	trigger_if = {
		limit = { 
			has_government = patrician_government 	
		}
		always = yes
	}
	trigger_else = {
		custom_description = {
			text = "has_law_allowing_designate_heir"
			has_realm_law_flag = can_designate_heirs
		}
	}
	custom_description = {
		text = "dynasty_member_leads_election"
		NAND = {
			primary_title = {
				has_title_law_flag = elective_succession_law
			}
			dynasty = primary_heir.dynasty
		}
	}	
}
# This trigger is a bit contrived in some way to ensure absolutely everyone gets evaluated without any early-outing
# It should always return true
# We use "count = all" and trigger_ifs to ensure that all have to be evaluated, and that all return
build_murder_targets_trigger = {
	any_relation = { # Rivals are good targets for murder schemes
		type = rival
		count = all
		trigger_if = {
			limit = {
				root = {
					ai_honor <= low_positive_ai_value
					can_start_scheme = {
						target = prev
						type = murder
					}
				}
			}
			add_to_temporary_list = murder_targets
		}
	}
	any_relation = { # Same with nemeses
		type = nemesis
		count = all
		trigger_if = {
			limit = {
				root = {
					ai_honor <= medium_positive_ai_value
					can_start_scheme = {
						target = prev
						type = murder
					}
				}
			}
			add_to_temporary_list = murder_targets
		}
	}
	trigger_if = { # Greedy people have no qualms murdering their way up the succession...
		limit = {
			ai_greed >= low_positive_ai_value
			exists = dynasty
		}
		dynasty = {
			any_dynasty_member = {
				count = all
				trigger_if = {
					limit = {
						is_good_familial_murder_target_trigger = yes
					}
					add_to_temporary_list = murder_targets
				}
			}
		}
		any_close_or_extended_family_member = {
			count = all
			trigger_if = {
				limit = {
				NOT = { is_in_list = murder_targets }
					is_good_familial_murder_target_trigger = yes
				}
				add_to_temporary_list = murder_targets
			}
		}
	}
	any_consort = { # Unfaithful spouses
		trigger_if = {
			limit = {
				root = {
					ai_compassion <= 0
					can_start_scheme = {
						target = prev
						type = murder
					}
				}
				exposed_cheating_on_spouse_trigger = { SPOUSE = root }
				add_to_temporary_list = murder_targets
			}
		}
	}
	trigger_if = { # Certain vassals will plot against their liege under certain conditions
		limit = {
			highest_held_title_tier > tier_barony
			exists = liege
			liege = {
				tyranny >= medium_tyranny
			}
			opinion = {
				target = liege
				value <= high_negative_opinion
			}
			ai_rationality >= medium_positive_ai_value
		}
		liege = {
			add_to_temporary_list = murder_targets
		}
	}
	trigger_if = { # #Imperial mod: Greedy people may want to murder out of jealousy
		limit = {
			is_independent_ruler = no
			exists = liege
			top_liege = { has_government = imperial_government }
			ai_greed >= medium_positive_ai_value
			is_councillor = no			
			NOT = { highest_held_title_tier >= tier_kingdom }
		}
		liege = {
			any_vassal = {
				trigger_if = {
					limit = {
						OR = {
							is_councillor = yes
							AND = {
								has_government = military_command
								highest_held_title_tier > root.tier
							}	
						}
						reverse_opinion = { target = root value < low_negative_opinion }
						NOT = { this = root }
						add_to_temporary_list = murder_targets
					}
				}				
			}
			any_courtier = {
				trigger_if = {
					limit = {
						is_councillor = yes
						is_special_councillor = no
						is_clergy = no
						reverse_opinion = { target = root value < low_negative_opinion }
						NOT = { this = root }
						add_to_temporary_list = murder_targets
					}
				}				
			}
		}
	}
}
###

# eligible_for_imperial_council_position_trigger = { #Only unlanded characters can hold some council positions in imperial administrations
	# OR = {
		# $COURT_OWNER$ = { NOT = { has_government = imperial_government } }
		# is_ruler = no
		# #patrician government
	# }
# }
military_appointment_martial_conditions_trigger = {
	custom_description = {
		text = eligible_for_military_appointment_trigger_BASE
		OR = {
			has_martial_lifestyle_trait_trigger = yes
			#martial > medium_skill_rating
			has_education_martial_trigger = yes
			has_character_modifier = imperial_military_rank_gained
		}
	}	
}
eligible_for_military_appointment_trigger = {
	military_appointment_martial_conditions_trigger = yes
	custom_description = {
		text = eligible_for_military_appointment_trigger_ADULT
		is_physically_able_adult = yes
	}	
	custom_description = {
		text = eligible_for_military_appointment_trigger_COMMANDER
		OR = {
			this = $APPOINTEE$
			can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = $APPOINTEE$ }
		}
		trigger_if = {
			limit = { faith = { has_doctrine_parameter = clergy_can_fight } }
			always = yes
		}
		trigger_else = {
			NOR = {
				is_clergy = yes
				has_trait = devoted
			}
		}
	}	
	NOT = { has_trait = infirm }
	NOT = { has_trait = blind }
}
eligible_for_military_career_trigger = {
	custom_description = {
		text = "eligible_for_military_career_trigger_KNIGHT"
		trigger_if = {
			limit = { is_ai = yes }
			can_be_knight_trigger = {
				ARMY_OWNER = root.top_liege
			}
		}
		trigger_else = {
			can_be_warrior_trigger = {
				ARMY_OWNER = root.top_liege
			}
		}
	}	
	is_physically_able_adult = yes
	NOT = { has_trait = infirm }
	NOT = { has_trait = blind }
	age < 40
	OR = {
		culture_group = top_liege.culture_group
		religion = top_liege.religion
		top_liege = { accepts_non_faith_commanders_trigger = yes }
	}
	NOT = { has_character_modifier = imperial_military_rank_gained }
	NOT = { has_character_flag = has_asked_for_military_career }
	custom_description = {
		text = "eligible_for_military_career_trigger_COMMANDER"
		NAND = {
			OR = {
				has_government = imperial_government
				has_government = military_command
				has_government = feudal_government
				has_government = clan_government
			}	
			is_ruler = yes
		}
	}	
	NAND = { #unrelated courtier is already an eligible commander anyway
		is_ruler = no
		NOT = { house = liege.house }
		military_appointment_martial_conditions_trigger = yes
	}
}
accepts_non_faith_commanders_trigger = {
	faith = {
		OR = {
			AND = { #Covers ashari/ismali
				has_doctrine = tenet_tax_nonbelievers 
				NOT = { has_doctrine = doctrine_pluralism_fundamentalist }
			}
			has_doctrine = doctrine_pluralism_pluralistic
		}
	}	
}
imperial_elective_potential_landed_candidate_trigger = {

	#Either the candidate is in the realm, or the title is independent, or the realm of the title does NOT have protected inheritance.
	OR = {
		any_liege_or_above = {
			this = scope:holder
		}
		scope:holder = { is_independent_ruler = yes }
		scope:holder = {
			NOT = {
				has_realm_law_flag = titles_cannot_leave_realm_on_succession
			}
		}
	}
	OR = {
		highest_held_title_tier > 2 #Or Duke-tier... (if title is King or Emperor tier)
		AND = {
			scope:holder = {
				highest_held_title_tier = 3
			}
			highest_held_title_tier > 1 #...Or Count-tier under a Duchy...
		}
		AND = {
			scope:holder = {
				highest_held_title_tier < 3
			}
			is_ruler = yes #...Or Baron-tier under a County.
			is_landed = yes
		}
	}
	OR = { #If the title controls the faith, candidates must be of the same faith.
		scope:holder = {
			NAND = {
				exists = faith.religious_head
				this = faith.religious_head #TODO_CD:It might need to be changed to check only for the title rather than the holder. (controlled_faith = scope:holder.faith)
			}
		}
		this.faith = scope:holder.faith
	}
	has_unelectable_trait_trigger = no
	has_unelectable_government_trigger = no
}

military_appointment_potential_landed_candidate_trigger = {
	eligible_for_military_appointment_trigger = { APPOINTEE = scope:holder }

	has_same_government = scope:holder
	#Candidate is within top liege realm
	top_liege = {
		OR = {
			this = scope:holder
			is_liege_or_above_of = scope:holder
		}	
	}
	
	OR = { #If the title controls the faith, candidates must be of the same faith.
		scope:holder = {
			NAND = {
				exists = faith.religious_head
				this = faith.religious_head #TODO_CD:It might need to be changed to check only for the title rather than the holder. (controlled_faith = scope:holder.faith)
			}
		}
		this.faith = scope:holder.faith
	}
	has_unelectable_trait_trigger = no
	has_unelectable_government_trigger = no
}

military_appointment_potential_landless_claimant_candidate_trigger = {
	eligible_for_military_appointment_trigger = { APPOINTEE = scope:holder }
	
	#Candidate is within top liege realm
	top_liege = {
		OR = {
			this = scope:holder
			is_liege_or_above_of = scope:holder
		}	
	}

	NOT = { #Landless characters should not be heirs to Theocracies/Republics.
		any_heir_title = {
			exists = holder
			holder = {
				has_unelectable_government_trigger = yes
			}
		}
	}
	OR = { #If the title controls the faith, candidates must be of the same faith.
		scope:holder = {
			NAND = {
				exists = faith.religious_head
				this = scope:holder.faith.religious_head #TODO_CD:It might need to be changed to check only for the title rather than the holder. (controlled_faith = scope:holder.faith)
			}
		}
		faith = scope:holder.faith
	}
	has_unelectable_trait_trigger = no
	has_unelectable_government_trigger = no
}

military_appointment_potential_landless_dynastic_candidate_trigger = {
	eligible_for_military_appointment_trigger = { APPOINTEE = scope:holder }
	
	#Candidate is within top liege realm
	top_liege = {
		OR = {
			this = scope:holder
			is_liege_or_above_of = scope:holder
		}	
	}
	
	NOT = { #Landless characters should not be heirs to Theocracies/Republics.
		any_heir_title = {
			exists = holder
			holder = {
				has_unelectable_government_trigger = yes
			}
		}
	}
	OR = { #If the title controls the faith, candidates must be of the same faith.
		scope:holder = {
			NAND = {
				exists = faith.religious_head
				this = scope:holder.faith.religious_head #TODO_CD:It might need to be changed to check only for the title rather than the holder. (controlled_faith = scope:holder.faith)
			}
		}
		faith = scope:holder.faith
	}
	has_unelectable_trait_trigger = no
	has_unelectable_government_trigger = no
}
can_get_military_command_trigger = {
	any_liege_or_above = { has_government = imperial_government }
	NOT = { 
		any_liege_or_above = { 
			OR = {
				has_government = feudal_government
				has_government = clan_government
			}
			primary_title.tier <= tier_kingdom
		} 
	}
	NOT = { vassal_contract_has_flag = imperial_autonomy_full }
	NOT = { primary_title.tier = tier_empire }
	NOT = { has_government = imperial_government }
	NOT = { has_character_flag = prevent_military_command }
	can_get_patrician_government_trigger = no	
}
is_landless_patrician_title_trigger = {
	exists = var:patrician_house
	var:patrician_house = 1 
}
can_get_patrician_government_trigger = {
	#primary_title = { tier < tier_county }
	primary_title = { 
		is_landless_patrician_title_trigger = yes
	}
	NOT = { 
		any_held_title = {
			tier >= tier_duchy
			NOT = { is_landless_patrician_title_trigger = yes }		
		}
	}		
}
military_faction_duke_promotion_candidate_trigger = {
	OR = {
		AND = {
			has_government = military_command
			highest_held_title_tier = tier_county
		}	
		has_government = patrician_government
	}
	domain_size = 1	
	primary_title.duchy = {
		OR = {
			holder = $FACTION_TARGET$
			AND = {
				NOT = { exists = holder }
				$FACTION_TARGET$ = { completely_controls = PREV }
			}
		}
		NOT = { this = $FACTION_TARGET$.capital_county.duchy } #not capital duchy
	}	
}
has_estates_trigger = {
	OR = {
		has_character_modifier = patrician_estate_modifier_6
		has_character_modifier = patrician_estate_modifier_5
		has_character_modifier = patrician_estate_modifier_4
		has_character_modifier = patrician_estate_modifier_3
		has_character_modifier = patrician_estate_modifier_2
		has_character_modifier = patrician_estate_modifier_1
	}
}
below_patrician_limit_trigger = {
	NOT = {
		any_vassal = {
			has_government = patrician_government
			count >= 8
		}
	}
}
can_retire_military_command_trigger = {
	OR = {
		age >= 60
		has_trait = incapable
		has_trait = maimed
		has_trait = infirm
		has_trait = blind
		custom_description = {
			text = retire_decision_DISEASE
			age >= 40
			OR = {
				has_trait = consumption #2
				has_trait = cancer #3
				has_trait = great_pox #1		
				has_trait = gout_ridden #1.5 	
				has_trait = leper #1.5	  						
			}
		}	
		custom_description = {
			text = retire_decision_DISGRACED
			trigger_if = { #is a prison negotiation
				limit = { 
					exists = scope:actor
				}
				always = yes
			}	
			trigger_else = {
				stress >= high_stress
			}
			OR = {
				AND = {
					has_trait = witch
					faith = { has_doctrine_parameter = witchcraft_illegal }
				}
				AND = {
					has_trait = cannibal
					NOT = { faith = { has_doctrine = tenet_ritual_cannibalism } }
				}
				AND = {
					has_trait = sodomite
					faith = { has_doctrine_parameter = homosexuality_illegal }
				}
				AND = {
					has_trait = deviant
					faith = { has_doctrine_parameter = deviancy_illegal }
				}
				AND = {
					OR = {
						has_trait = adulterer
						has_trait = fornicator
					}
					OR = {
						AND = {
							faith = { has_doctrine_parameter = adultery_female_crime }
							is_female = yes
						}
						AND = {
							faith = { has_doctrine_parameter = adultery_male_crime }
							is_male = yes
						}
					}
				}
			}
			
		}
	}
}
