#############################################
# Shadows of the Shroud - Scripted Triggers #
#############################################

# scope = country
is_psionic = {
	OR = {
		has_adopted_psionic_tradition = yes
		owner_main_species = {
			has_psionic_species_trait = yes
		}
	}
}

# scope = species
is_psionic_portrait = {
	OR = {
		species_portrait = psionic_01
		species_portrait = psionic_02
		species_portrait = psionic_03
		species_portrait = psionic_04
		species_portrait = psionic_05
		species_portrait = psionic_06
		species_portrait = psionic_07
		species_portrait = psionic_08
		species_portrait = psionic_09
		species_portrait = psionic_10
		species_portrait = psionic_11
		species_portrait = psionic_12
	}
}

can_receive_psionic_leader_trait = {
	NOR = {
		has_trait = leader_trait_psionic
	}
	is_leader_tier = leader_tier_default
	is_gestalt_node = no
}

has_adopted_psionic_tradition = {
	OR = {
		has_tradition = tr_psionics_adopt
		has_tradition = tr_psionics_shroud_adopt
	}
}

has_psi_corps_tradition = {
	OR = {
		has_tradition = tr_psionics_psi_corps
		has_tradition = tr_psionics_shroud_psi_corps
	}
}

has_active_psi_corps = {
	OR = {
		any_owned_colony = {
			OR = {
				has_active_building = building_psi_corps
				orbital_defence.starbase? = { has_starbase_building = ring_psi_corps }
			}
		}
		any_owned_fleet = { starbase? = { has_starbase_building = arkship_psi_corps } }
	}
}

has_finished_psionic_tradition = {
	OR = {
		has_tradition = tr_psionics_finish
		has_tradition = tr_psionics_shroud_finish
	}
}

#############
# Covenants #
#############

has_any_covenant = {
	OR = {
		has_covenant_with_the_instrument_of_desire = yes
		has_covenant_with_the_eater_of_worlds = yes
		has_covenant_with_the_composer_of_strands = yes
		has_covenant_with_the_cradle_of_souls = yes
		has_covenant_with_whisperers_in_the_void = yes
		has_covenant_with_end_of_the_cycle = yes
	}
}

has_covenant_with_the_instrument_of_desire = {
	OR = {
		# Utopia DLC check
		has_modifier = covenant_instrument_of_desire_0
		has_modifier = covenant_instrument_of_desire
		# Shroud DLC check
		has_covenant = the_instrument_of_desire
	}
}

has_covenant_with_the_eater_of_worlds = {
	OR = {
		# Utopia DLC check
		has_modifier = covenant_eater_of_worlds_0
		has_modifier = covenant_eater_of_worlds
		# Shroud DLC check
		has_covenant = the_eater_of_worlds
	}
}

has_covenant_with_the_composer_of_strands = {
	OR = {
		# Utopia DLC check
		has_modifier = covenant_composer_of_strands_0
		has_modifier = covenant_composer_of_strands
		# Shroud DLC check
		has_covenant = the_composer_of_strands
	}
}

has_covenant_with_the_cradle_of_souls = {
	OR = {
		# Not present in Utopia DLC
		# Shroud DLC check
		has_covenant = the_cradle_of_souls
	}
}

has_covenant_with_whisperers_in_the_void = {
	OR = {
		# Utopia DLC check
		has_modifier = covenant_whisperers_in_the_void_0
		has_modifier = covenant_whisperers_in_the_void
		# Shroud DLC check
		has_covenant = whisperers_in_the_void
	}
}

has_covenant_with_end_of_the_cycle = {
	OR = {
		# Utopia DLC check
		has_modifier = covenant_end_of_the_cycle
		# Shroud DLC check
		has_covenant = end_of_the_cycle
	}
}

has_patron_disciple_leader_trait = {
	OR = {
		has_trait = leader_trait_disciple_of_the_cradle
		has_trait = leader_trait_disciple_of_the_eater
		has_trait = leader_trait_disciple_of_the_instrument
		has_trait = leader_trait_disciple_of_the_composer
		has_trait = leader_trait_disciple_of_the_whisperers
		has_trait = leader_trait_disciple_of_the_end
	}
}

##########
# Civics #
##########

is_entropy_drinkers_empire = {
	OR = {
		has_civic = civic_entropy_drinkers
		has_civic = civic_entropy_drinkers_machine
		has_civic = civic_entropy_drinkers_hive_mind
		has_civic = civic_entropy_drinkers_corporate
	}
}

is_superstitious_beliefs_empire = {
	OR = {
		has_valid_civic = civic_superstitious_beliefs
		has_valid_civic = civic_hive_superstitious_beliefs
		has_valid_civic = civic_machine_superstitious_beliefs
		has_valid_civic = civic_corporate_superstitious_beliefs
	}
}

is_twisted_experimenters_empire = {
	OR = {
		has_valid_civic = civic_twisted_experimenters
		has_valid_civic = civic_twisted_experimenters_hive_mind
		has_valid_civic = civic_twisted_experimenters_corporate
	}
}

is_secret_societies_empire = {
	OR = {
		has_valid_civic = civic_secret_societies
		has_valid_civic = civic_secret_societies_corporate
	}
}

is_candidate_for_early_contact = {
	is_country_type = default
	NOR = {
		is_unfriendly = yes
		has_civic = civic_hive_bodysnatcher
		has_civic = civic_machine_assimilator
		has_civic = civic_criminal_heritage
		is_fanatic_xenophobe = yes
	}
}

is_chosen_empire = {
	OR = {
		has_valid_civic = civic_chosen
		has_valid_civic = civic_hive_chosen
		has_valid_civic = civic_machine_chosen
		has_valid_civic = civic_corporate_chosen
	}
}

is_tankbound_empire = {
	OR = {
		has_valid_civic = civic_tankbound
		has_valid_civic = civic_tankbound_corporate
	}
}

############
# Edicts #
############

has_galactic_dowsing_edict = {
	OR = {
		has_edict = galactic_dowsing
		has_edict = galactic_dowsing_gestalt
	}
}

##########
# Cycles #
##########

has_cycle_of_fortune = {
	has_country_flag = cycle_of_fortune
}

has_cycle_of_omens = {
	has_country_flag = cycle_of_omens
}

has_cycle_of_growth = {
	has_country_flag = cycle_of_growth
}

has_cycle_of_prosperity = {
	has_country_flag = cycle_of_prosperity
}

has_cycle_of_conflict = {
	has_country_flag = cycle_of_conflict
}

has_cycle_of_harmony = {
	has_country_flag = cycle_of_harmony
}

has_cycle_of_knowledge = {
	has_country_flag = cycle_of_knowledge
}

has_cycle_synergy = {
	OR = {
		AND = {
			is_in_domain = the_eater_of_worlds
			has_cycle_of_conflict = yes
		}
		AND = {
			is_in_domain = the_composer_of_strands
			has_cycle_of_growth = yes
		}
		AND = {
			is_in_domain = the_cradle_of_souls
			has_cycle_of_harmony = yes
		}
		AND = {
			is_in_domain = the_instrument_of_desire
			has_cycle_of_prosperity = yes
		}
		AND = {
			is_in_domain = whisperers_in_the_void
			has_cycle_of_knowledge = yes
		}
	}
}

##########
# Events #
##########

# This = planet
sb_district_event_trigger = {
	is_artificial = no
	is_homeworld = no
	has_any_$CATEGORY$_district_or_building = yes
}

# This = planet
is_anglers_newfound_fertility = {
	OR = {
		is_planet_class = pc_gaia
		is_planet_class = pc_ocean
	}
	owner? = {
		is_anglers_empire = yes
	}
}

# Prev = Country
# This = System
sb_favorable_trine_system_trigger = {
	owner? = {
		is_same_value = prevprev
	}
	NOT = {
		has_star_flag = favorable_trine
	}
	any_system_planet_colony = {
		sb_aligment_colony_trigger = {
			OWNER = prevprevprev
		}
		solar_system = {
			count_system_planet = {
				count >= 3
				limit = {
					NOT = {
						is_same_value = prevprev
					}
					sb_aligment_planet_trigger = yes
				}
			}
		}
	}
}

# This = Planet
sb_aligment_colony_trigger = {
	is_artificial = no
	exists = owner
	owner = {
		is_same_value = $OWNER$
	}
}

# This = Planet
sb_aligment_planet_trigger = {
	is_star = no
	is_asteroid = no
	is_artificial = no
	is_astral_scar = no
}

# This = System
sb_luminary_system_trigger = {
	NOR = {
		is_star_class = sc_black_hole
		any_system_megastructure = {
			OR = {
				is_megastructure_type = dyson_sphere_0
				is_megastructure_type = dyson_sphere_1
				is_megastructure_type = dyson_sphere_2
				is_megastructure_type = dyson_sphere_3
				is_megastructure_type = dyson_sphere_4
				is_megastructure_type = dyson_sphere_5
				is_megastructure_type = dyson_sphere_ruined
				is_megastructure_type = dyson_sphere_restored
				is_megastructure_type = dyson_swarm_1
				is_megastructure_type = dyson_swarm_2
				is_megastructure_type = dyson_swarm_3
			}
		}
	}
	OR = {
		count_system_planet_colony = {
			count > 0
		}
		count_system_ship_colony = {
			count > 0
		}
	}
}

# This = Planet
sb_radiant_luminary_planet_trigger = {
	num_districts = {
		type = district_generator
		value >= 1
	}
}

# This = Leader
sb_councilor_harmony_leader_trigger = {
	is_councilor = yes
	NOT = {
		has_leader_flag = councilor_harmony
	}
}

# This = Fleet
sb_naval_collision_fleet_trigger = {
	is_in_combat = no
	is_ship_class = shipclass_military
	count_controlled_ship = {
		count >= 5
		limit = {
			sb_naval_collision_ship_trigger = yes
		}
	}
}

sb_naval_collision_ship_trigger = {
	is_space_fauna = no
	is_battleship_or_above = no
}

sb_extreme_solar_flare_system_trigger = {
	NOR = {
		is_star_class = sc_black_hole
		any_system_planet = {
			is_planet_class = pc_habitat
		}
		any_system_megastructure = {
			OR = {
				is_megastructure_type = dyson_sphere_0
				is_megastructure_type = dyson_sphere_1
				is_megastructure_type = dyson_sphere_2
				is_megastructure_type = dyson_sphere_3
				is_megastructure_type = dyson_sphere_4
				is_megastructure_type = dyson_sphere_5
				is_megastructure_type = dyson_sphere_ruined
				is_megastructure_type = dyson_sphere_restored
				is_megastructure_type = dyson_swarm_1
				is_megastructure_type = dyson_swarm_2
				is_megastructure_type = dyson_swarm_3
			}
		}
	}
	any_system_planet = {
		OR = {
			has_deposit_for = shipclass_mining_station
			has_deposit_for = shipclass_research_station
			has_observation_outpost = yes
		}
	}
}

sb_extreme_solar_flare_ship_trigger = {
	exists = owner
	owner = {
		is_same_value = root
	}
	OR = {
		is_ship_size = mining_station
		is_ship_size = research_station
		is_ship_size = observation_station
	}
}

sb_high_profile_traffic_accident_leader_trigger = {
	is_ruler = no
	is_heir = no
	is_gestalt_node = no
	is_leader_in_combat = no
	is_leader_mia_or_in_transit = no
	is_leader_excavating_site = no
	NOT = { has_leader_flag = leader_death_events_blocked }
}

sb_massive_sinkholes_planet_trigger = {
	is_artificial = no
	is_homeworld = no
	num_buildings = {
		type = any
		value >= 2
	}
}

sb_spontaneous_uproar_planet_trigger = {
	is_homeworld = no
	is_planet_revolting = no
	pop_amount >= 200
}

# Prev = Country
# This = System
sb_calamitous_quartile_system_trigger = {
	exists = owner
	owner = {
		is_same_value = prevprev
	}
	any_system_planet_colony = {
		sb_aligment_colony_trigger = {
			OWNER = prevprevprev
		}
		solar_system = {
			count_system_planet = {
				count >= 4
				limit = {
					NOT = {
						is_same_value = prevprev
					}
					sb_aligment_planet_trigger = yes
				}
			}
		}
	}
}

# This = Leader
sb_discordant_council_leader_trigger = {
	is_councilor = yes
	NOT = {
		has_leader_flag = discordant_council
	}
}

############
# Factions #
############

is_masquerade_pop_faction = {
	OR = {
		is_pop_faction_type = "the_masquerade_xenophile"
		is_pop_faction_type = "the_masquerade_materialist"
		is_pop_faction_type = "the_masquerade_spiritualist"
		is_pop_faction_type = "the_masquerade_pacifist"
		is_pop_faction_type = "the_masquerade_militarist"
		is_pop_faction_type = "the_masquerade_egalitarian"
		is_pop_faction_type = "the_masquerade_authoritarian"
	}
}

is_curtain_pop_faction = {
	OR = {
		is_pop_faction_type = "the_curtain_xenophobe"
		is_pop_faction_type = "the_curtain_materialist"
		is_pop_faction_type = "the_curtain_spiritualist"
		is_pop_faction_type = "the_curtain_pacifist"
		is_pop_faction_type = "the_curtain_militarist"
		is_pop_faction_type = "the_curtain_authoritarian"
		is_pop_faction_type = "the_curtain_egalitarian"
	}
}

is_grand_dance_pop_faction = {
	OR = {
		is_pop_faction_type = "the_grand_dance_xenophile"
		is_pop_faction_type = "the_grand_dance_xenophobe"
		is_pop_faction_type = "the_grand_dance_materialist"
		is_pop_faction_type = "the_grand_dance_pacifist"
		is_pop_faction_type = "the_grand_dance_militarist"
		is_pop_faction_type = "the_grand_dance_authoritarian"
		is_pop_faction_type = "the_grand_dance_egalitarian"
	}
}

###########
# Patrons #
###########

# This = Planet
# Root = Country
potential_shrouded_planet_for_patron_revenge = {
	is_moon = no
	NOR = {
		is_planet_class = pc_shrouded
		is_planet_class = pc_asteroid
	}
	solar_system = {
		distance = {
			source = root.capital_scope
			min_jumps = @replace_patron_shrouded_world_min_jumps
			max_jumps = @replace_patron_shrouded_world_max_jumps
			use_bypasses = no
		}
	}
}

has_contact_with_patron_or_no_relation = {
	NOT = {
		has_patron_relation = {
			patron = $PATRON$
			contact = none
		}
	}
}

has_contacted_all_major_patrons = {
	has_contact_with_patron_or_no_relation = {
		PATRON = the_eater_of_worlds
	}
	has_contact_with_patron_or_no_relation = {
		PATRON = the_cradle_of_souls
	}
	has_contact_with_patron_or_no_relation = {
		PATRON = the_instrument_of_desire
	}
	has_contact_with_patron_or_no_relation = {
		PATRON = the_composer_of_strands
	}
}

has_contacted_all_minor_patrons = {
	has_contact_with_patron_or_no_relation = {
		PATRON = the_animator_of_clay
	}
	has_contact_with_patron_or_no_relation = {
		PATRON = the_outsider
	}
	has_contact_with_patron_or_no_relation = {
		PATRON = the_endless_tide
	}
	has_contact_with_patron_or_no_relation = {
		PATRON = the_pale_mountain
	}
	has_contact_with_patron_or_no_relation = {
		PATRON = the_golden_dream
	}
	has_contact_with_patron_or_no_relation = {
		PATRON = the_hollow_point
	}
	has_contact_with_patron_or_no_relation = {
		PATRON = the_vacant_throne
	}
	has_contact_with_patron_or_no_relation = {
		PATRON = the_black_river
	}
}

#####################
# Patron Objectives #
#####################

apply_pop_template_pop_check = {
	check_variable = {
		which = po_apply_pop_template_counter
		value >= @po_apply_pop_template_tier$TIER$_nb_pop
	}
}

###########
# Species #
###########

is_machine_species = {
	species = {
		OR = {
			uplift_is_archetype = MACHINE
			is_archetype = MACHINE
		}
	}
}

founder_species_is_organic = {
	OR = {
		founder_species_is_biological = yes
		founder_species_is_lithoid = yes
	}
}

founder_species_is_biological = {
	founder_species = {
		OR = {
			uplift_is_archetype = BIOLOGICAL
			is_archetype = BIOLOGICAL
		}
	}
}

founder_species_is_lithoid = {
	founder_species = {
		OR = {
			uplift_is_archetype = LITHOID
			is_archetype = LITHOID
		}
	}
}

founder_species_is_machine = {
	founder_species = {
		OR = {
			uplift_is_archetype = MACHINE
			is_archetype = MACHINE
		}
	}
}

founder_species_is_robotic = {
	founder_species = {
		is_robotic = yes
	}
}

founder_species_is_toxoid = {
	founder_species = {
		OR = {
			is_species_class = PRE_TOX
			is_species_class = TOX
		}
	}
}

founder_species_is_fungoid = {
	founder_species = {
		OR = {
			is_species_class = PRE_FUN
			is_species_class = FUN
		}
	}
}

founder_species_is_plantoid = {
	founder_species = {
		OR = {
			is_species_class = PRE_PLANT
			is_species_class = PLANT
		}
	}
}

founder_species_is_avian = {
	founder_species = {
		OR = {
			is_species_class = PRE_AVI
			is_species_class = AVI
		}
	}
}

founder_species_is_reptilian = {
	founder_species = {
		OR = {
			is_species_class = PRE_REP
			is_species_class = REP
		}
	}
}

founder_species_is_mammalian = {
	founder_species = {
		OR = {
			is_species_class = PRE_MAM
			is_species_class = MAM
		}
	}
}

founder_species_is_molluscoid = {
	founder_species = {
		OR = {
			is_species_class = PRE_MOL
			is_species_class = MOL
		}
	}
}

founder_species_is_arthropoid = {
	founder_species = {
		OR = {
			is_species_class = PRE_ART
			is_species_class = ART
		}
	}
}

founder_species_is_aquatic = {
	founder_species = {
		OR = {
			is_species_class = PRE_AQUATIC
			is_species_class = AQUATIC
		}
	}
}

founder_species_is_infernal = {
	founder_species = {
		OR = {
			is_species_class = INF
		}
	}
}

#########
# Techs #
#########

has_all_psionic_components = {
	has_technology = tech_psionic_shield
	has_technology = tech_precognition_interface
	has_technology = tech_psi_jump_drive_1
	has_technology = tech_zro_launcher
	has_technology = tech_psionic_lightning
	has_technology = tech_psionic_disruptor
	has_technology = tech_psionic_bombers
	if = {
		limit = {
			has_first_contact_dlc = yes
		}
		has_technology = tech_cloaking_psi
	}
}

# Techs prerequisites PHYSICS
has_tech_lasers_5_prerequisites = {
	has_technology = tech_lasers_4
}
has_tech_lasers_4_prerequisites = {
	has_technology = tech_lasers_3
}
has_tech_lasers_3_prerequisites = {
	has_technology = tech_lasers_2
}
has_tech_lasers_2_prerequisites = {
	has_technology = tech_lasers_1
}

has_tech_neuro_quantum_links_prerequisites = {
	has_technology = tech_basic_science_lab_2
	has_technology = tech_integrated_cybernetics
}

has_tech_consumer_good_refinement_2_prerequisites = {
	has_technology = tech_luxuries_2
	has_technology = tech_consumer_good_refinement_1
	has_technology = tech_self_aware_logic
}
has_tech_consumer_good_refinement_1_prerequisites = {
	has_technology = tech_luxuries_1
	has_technology = tech_administrative_ai
}

has_tech_shields_5_prerequisites = {
	has_technology = tech_shields_4
}
has_tech_shields_4_prerequisites = {
	has_technology = tech_shields_3
}
has_tech_shields_3_prerequisites = {
	has_technology = tech_shields_2
}
has_tech_shields_2_prerequisites = {
	has_technology = tech_shields_1
}

has_tech_plasma_3_prerequisites = {
	has_technology = tech_plasma_2
}
has_tech_plasma_2_prerequisites = {
	has_technology = tech_plasma_1
}
has_tech_plasma_1_prerequisites = {
	has_technology = tech_lasers_2
}

has_tech_energy_lance_2_prerequisites = {
	has_technology = tech_lasers_5
	has_technology = tech_energy_lance_1
}
has_tech_energy_lance_1_prerequisites = {
	has_technology = tech_lasers_4
	OR = {
		has_technology = tech_battleships
		has_technology = tech_stingers
	}
}

has_tech_disruptors_3_prerequisites = {
	has_technology = tech_disruptors_2
}
has_tech_disruptors_2_prerequisites = {
	has_technology = tech_disruptors_1
}
has_tech_disruptors_1_prerequisites = {
	has_technology = tech_lasers_2
}

has_tech_arc_emitter_2_prerequisites = {
	has_technology = tech_arc_emitter_1
}
has_tech_arc_emitter_1_prerequisites = {
	has_technology = tech_disruptors_3
	OR = {
		has_technology = tech_battleships
		has_technology = tech_stingers
	}
}

has_tech_energy_torpedoes_2_prerequisites = {
	has_technology = tech_energy_torpedoes_1
}
has_tech_energy_torpedoes_1_prerequisites = {
	has_technology = tech_lasers_4
	has_technology = tech_torpedoes_1
}

has_tech_sapient_ai_prerequisites = {
	has_technology = tech_self_aware_logic
}
has_tech_self_aware_logic_prerequisites = {
	has_technology = tech_administrative_ai
}
has_tech_administrative_ai_prerequisites = {
	has_technology = tech_basic_science_lab_1
}

has_tech_cryostasis_2_prerequisites = {
	has_technology = tech_self_aware_logic
	has_technology = tech_cryostasis_1
}
has_tech_cryostasis_1_prerequisites = {
	has_technology = tech_administrative_ai
	has_technology = tech_colonization_1
}

has_tech_combat_computers_3_prerequisites = {
	has_technology = tech_combat_computers_2
	has_technology = tech_sapient_ai
}
has_tech_combat_computers_2_prerequisites = {
	has_technology = tech_combat_computers_1
	has_technology = tech_self_aware_logic
}
has_tech_combat_computers_1_prerequisites = {
	has_technology = tech_administrative_ai
}

# Techs prerequisites SOCIETY
has_tech_nutrient_replication_prerequisites = {
	has_technology = tech_nano_vitality_crops
}
has_tech_nano_vitality_crops_prerequisites = {
	has_technology = tech_gene_crops
}
has_tech_gene_crops_prerequisites = {
	has_technology = tech_eco_simulation
}
has_tech_eco_simulation_prerequisites = {
	has_technology = tech_industrial_farming
}

has_tech_food_processing_2_prerequisites = {
	has_technology = tech_food_processing_1
}
has_tech_food_processing_1_prerequisites = {
	has_technology = tech_eco_simulation
}

has_tech_hydroponics_prerequisites = {
	has_technology = tech_industrial_farming
}

has_tech_tomb_world_adaption_prerequisites = {
	has_technology = tech_colonization_3
}

has_tech_colonization_5_prerequisites = {
	has_technology = tech_colonization_4
}
has_tech_colonization_4_prerequisites = {
	has_technology = tech_colonization_3
}
has_tech_colonization_3_prerequisites = {
	has_technology = tech_colonization_2
}
has_tech_colonization_2_prerequisites = {
	has_technology = tech_colonization_1
}

has_tech_frontier_hospital_prerequisites = {
	has_technology = tech_frontier_health
	has_technology = tech_colonial_centralization
}
has_tech_frontier_health_prerequisites = {
	has_technology = tech_genome_mapping
}

has_tech_gene_banks_prerequisites = {
	has_technology = tech_cloning
}
has_tech_cloning_prerequisites = {
	has_technology = tech_genome_mapping
}

has_tech_epigenetic_triggers_prerequisites = {
	has_technology = tech_genome_mapping
}

has_tech_vitality_boosters_prerequisites = {
	has_technology = tech_genome_mapping
}

has_tech_genome_mapping_prerequisites = {
	always = yes
}

has_tech_gene_seed_purification_prerequisites = {
	has_technology = tech_gene_tailoring
}

has_tech_glandular_acclimation_prerequisites = {
	has_technology = tech_gene_tailoring
}
has_tech_gene_tailoring_prerequisites = {
	has_technology = tech_genome_mapping
}

has_tech_morphogenetic_field_mastery_prerequisites = {
	has_technology = tech_epigenetic_triggers
}

has_tech_gene_expressions_prerequisites = {
	has_technology = tech_gene_tailoring
}

has_tech_thought_enforcement_prerequisites = {
	has_technology = tech_telepathy
}
has_tech_precognition_interface_prerequisites = {
	has_technology = tech_telepathy
}
has_tech_telepathy_prerequisites = {
	has_technology = tech_psionic_theory
}
has_tech_psionic_theory_prerequisites = {
	always = yes
}

has_tech_tracking_implants_prerequisites = {
	has_technology = tech_subdermal_stimulation
	has_technology = tech_galactic_bureaucracy
}
has_tech_subdermal_stimulation_prerequisites = {
	has_technology = tech_genome_mapping
}

has_tech_synthetic_thought_patterns_prerequisites = {
	has_technology = tech_artificial_moral_codes
}
has_tech_artificial_moral_codes_prerequisites = {
	always = yes
}

has_tech_neural_implants_prerequisites = {
	always = yes
}

# Techs prerequisites ENGINEERING
has_tech_ship_armor_5_prerequisites = {
	has_technology = tech_ship_armor_4
}
has_tech_ship_armor_4_prerequisites = {
	has_technology = tech_ship_armor_3
}
has_tech_ship_armor_3_prerequisites = {
	has_technology = tech_ship_armor_2
}
has_tech_ship_armor_2_prerequisites = {
	has_technology = tech_ship_armor_1
}

has_tech_synthetic_leaders_prerequisites = {
	has_technology = tech_synthetic_workers
}
has_tech_synthetic_workers_prerequisites = {
	has_technology = tech_droid_workers
	has_technology = tech_galactic_administration
	has_technology = tech_sapient_ai
}
has_tech_droid_workers_prerequisites = {
	has_technology = tech_robotic_workers
	has_technology = tech_colonial_centralization
}
has_tech_robotic_workers_prerequisites = {
	has_technology = tech_powered_exoskeletons
}
has_tech_powered_exoskeletons_prerequisites = {
	has_technology = tech_basic_industry
}

has_tech_integrated_cybernetics_prerequisites = {
	has_technology = tech_powered_exoskeletons
	has_technology = tech_colonial_centralization
}

has_tech_housing_2_prerequisites = {
	has_technology = tech_housing_1
}
has_tech_housing_1_prerequisites = {
	always = yes
}

has_tech_kinetic_artillery_2_prerequisites = {
	has_technology = tech_mass_drivers_5
	has_technology = tech_kinetic_artillery_1
}
has_tech_kinetic_artillery_1_prerequisites = {
	has_technology = tech_mass_drivers_4
}

has_tech_mass_drivers_5_prerequisites = {
	has_technology = tech_mass_drivers_4
}
has_tech_mass_drivers_4_prerequisites = {
	has_technology = tech_mass_drivers_3
}
has_tech_mass_drivers_3_prerequisites = {
	has_technology = tech_mass_drivers_2
}
has_tech_mass_drivers_2_prerequisites = {
	has_technology = tech_mass_drivers_1
}
has_tech_mass_drivers_1_prerequisites = {
	always = yes
}

has_tech_mass_accelerator_2_prerequisites = {
	has_technology = tech_mass_drivers_5
	has_technology = tech_mass_accelerator_1
}
has_tech_mass_accelerator_1_prerequisites = {
	has_technology = tech_mass_drivers_4
	OR = {
		has_technology = tech_battleships
		has_technology = tech_stingers
	}
}

has_tech_missiles_5_prerequisites = {
	has_technology = tech_missiles_4
}
has_tech_missiles_4_prerequisites = {
	has_technology = tech_missiles_3
}
has_tech_missiles_3_prerequisites = {
	has_technology = tech_missiles_2
}
has_tech_missiles_2_prerequisites = {
	has_technology = tech_missiles_1
	has_technology = tech_fusion_power
}

has_tech_flak_batteries_3_prerequisites = {
	has_technology = tech_flak_batteries_2
}
has_tech_flak_batteries_2_prerequisites = {
	has_technology = tech_flak_batteries_1
}

has_tech_autocannons_3_prerequisites = {
	has_technology = tech_autocannons_2
}
has_tech_autocannons_2_prerequisites = {
	has_technology = tech_autocannons_1
}
has_tech_autocannons_1_prerequisites = {
	has_technology = tech_mass_drivers_2
}

has_tech_swarmer_missiles_2_prerequisites = {
	has_technology = tech_swarmer_missiles_1
}
has_tech_swarmer_missiles_1_prerequisites = {
	has_technology = tech_missiles_2
}

has_tech_torpedoes_3_prerequisites = {
	has_technology = tech_torpedoes_2
}
has_tech_torpedoes_2_prerequisites = {
	has_technology = tech_torpedoes_1
}
has_tech_torpedoes_1_prerequisites = {
	has_technology = tech_corvettes
}

has_unlocked_pleasure_worlds = {
	has_country_flag = bliss_accord
}

#Root = country, attacker
#This = country, target
can_declare_devour_war = {
	has_communications = root
	NOR = {
		is_at_war_with = root
		is_loyal_to = root
		has_overlord = root
		is_in_federation_with = root
		is_country_type = fallen_empire
		is_country_type = dormant_marauders
		is_country_type = marauder_raiders
		is_country_type = awakened_marauders
	}
}

has_breached_shroud = {
	has_country_flag = breached_shroud
}

can_access_shroud = {
	has_country_flag = can_access_shroud
}

#################
# Psionic Auras #
#################

# scope = starbase
has_starbase_aura_emitter = {
	OR = {
		has_starbase_building = aura_emitter
		has_starbase_building = ring_aura_emitter
		has_starbase_module = waystation_aura_emitter
	}
}

# scope = system
can_system_generate_psionic_aura = {
	if = {
		limit = {
			exists = owner
			owner = { is_same_value = $OWNER$ }
		}
		OR = {
			any_system_planet_colony = {
				is_owned_by = $OWNER$
				any_owned_pop_group = {
					has_psionic_species_trait = yes
				}
			}
			any_system_ship_colony = {
				is_owned_by = $OWNER$
				any_owned_pop_group = {
					has_psionic_species_trait = yes
				}
			}
			any_starbase_in_system = {
				is_waystation_starbase = no
				has_starbase_aura_emitter = yes
			}
		}
	}
	else = {
		OR = {
			any_system_planet_colony = {
				NOT = { is_owned_by = $OWNER$ }
				has_branch_office = yes
				has_holding = {
					holding = building_psionic_offices
					owner = $OWNER$
				}
			}
			any_system_ship_colony = {
				NOT = { is_owned_by = $OWNER$ }
				has_branch_office = yes
				has_holding = {
					holding = building_psionic_offices
					owner = $OWNER$
				}
			}
			any_starbase_in_system = {
				is_waystation_starbase = yes
				has_starbase_aura_emitter = yes
			}
			any_ship_in_system = {
				colony? = {
					is_owned_by = $OWNER$
					any_owned_pop_group = {
						has_psionic_species_trait = yes
					}
				}
			}
		}
	}
}

# scope = system
check_aura_suppression_buildings = {
	OR = {
		any_system_planet_colony = {
			OR = {
				has_active_building = building_psionic_suppressor
				has_active_building = building_ancient_ward_2
			}
		}
		any_system_ship_colony = {
			OR = {
				has_active_building = building_psionic_suppressor
				has_active_building = building_ancient_ward_2
			}
		}
		has_megastructure = shroud_seal
	}
}

# scope = system
is_psionic_aura_suppressed = {
	OR = {
		any_system_planet_colony = {
			OR = {
				AND = {
					has_active_building = building_psionic_suppressor
					exists = owner
					owner = {
						has_resource = { type = sr_zro amount > 0 }
					}
				}
				has_active_building = building_ancient_ward_2
			}
		}
		any_system_ship_colony = {
			OR = {
				AND = {
					has_active_building = building_psionic_suppressor
					exists = owner
					owner = {
						has_resource = { type = sr_zro amount > 0 }
					}
				}
				has_active_building = building_ancient_ward_2
			}
		}
		has_megastructure = shroud_seal
		check_variable_arithmetic = { which = value:count_neighbor_shroud_seals value > 0 }
	}
}

can_aura_spread_in_system = {
	has_psionic_aura = no
	is_psionic_aura_suppressed = no
	is_close_to_psionic_aura_generator_system = { OWNER = $OWNER$ }
}

is_close_to_psionic_aura_generator_system = {
	closest_system = {
		use_bypasses = no
		min_steps = 1
		max_steps = value:psionic_aura_hyperlane_range|OWNER|$OWNER$|
		valid = {
			has_psionic_aura = yes
			aura_owner = { is_same_value = $OWNER$ }
		}
		limit = { can_system_generate_psionic_aura = { OWNER = $OWNER$ } }
	}
}

has_encountered_psionic_auras = {
	has_country_flag = has_encountered_psionic_auras
}

is_hostile_towards_aura = {
	OR = {
		$PSIONIC_OWNER$ = {
			OR = {
				has_patron_aura = end_of_the_cycle
				is_rival = prev
				is_at_war_with = prev
				OR = {
					is_hostile_to = prev
					is_hostile = prev
				}
			}
		}
		OR = {
			is_pirate = yes
			is_crisis_faction = yes
		}
		# Mindwardens special case
		AND = {
			has_origin = origin_mindwardens
			$PSIONIC_OWNER$ = { is_country_type = exiled }
		}
	}
}

# this = tested system
aura_clash_can_attacker_spread_in_system = {
	if = {
		limit = { $ATTACKER_SYSTEM$ = { has_psionic_aura = end_aura } }
		always = yes # EotC aura goes where it pleases
	}
	else = {
		is_close_to_psionic_aura_generator_system = { OWNER = $ATTACKER_SYSTEM$.aura_owner }
		OR = {
			NOT = { exists = owner }
			owner = { is_same_value = $ATTACKER_SYSTEM$.aura_owner }
			AND = {
				owner = {
					is_hostile_towards_aura = {
						PSIONIC_OWNER = $ATTACKER_SYSTEM$.aura_owner
					}
				}
				NOT = { can_system_generate_psionic_aura = { OWNER = owner } }
			}
		}
	}
}

system_has_hostile_aura = {
	has_psionic_aura = yes
	aura_owner = {
		NOT = { is_same_value = $COUNTRY$ }
		OR = {
			is_at_war_with = $COUNTRY$
			is_rival = $COUNTRY$
			has_covenant_with_end_of_the_cycle = yes
			is_country_type = exiled
		}
	}
}

########################
# Advanced Governments #
########################

is_transcendent_authority = {
	has_country_flag = transcendent_government
}

is_corporeal_authority = {
	has_country_flag = corporeal_government
}

is_eligible_for_advanced_government = {
	check_variable = {
		which = $TYPE$_government_weight
		value > 1
	}
}

###########
# Accords #
###########

can_be_chosen_for_hindsight = {
	solar_system = {
		has_psionic_aura = yes
		aura_owner = { is_same_value = root }
	}
	is_artificial = no
	NOR = {
		is_planet_class = pc_relic
		is_planet_class = pc_gaia
		is_planet_class = pc_city
	}
}

can_be_chosen_for_hindsight_nomad = {
	is_planet_class = pc_ark
	solar_system = {
		has_psionic_aura = yes
		aura_owner = { is_same_value = root }
	}
	NOT = {
		has_deposit = d_relic_arkship
	}
}

can_be_chosen_for_hindsight_any = {
	OR = {
		can_be_chosen_for_hindsight = yes
		can_be_chosen_for_hindsight_nomad = yes
	}
}

#########
# Ships #
#########

is_battleship_or_above = {
	ship_size_multiplier >= @battleship_size_multiplier
}

###########
# Planets #
###########

# This = Planet
is_planet_revolting = {
	any_targeting_situation = {
		is_situation_type = situation_planetary_revolt
	}
}

has_zro_deposit = {
	OR = {
		has_deposit = d_zro_deposit_1
		has_deposit = d_zro_deposit_2
		has_deposit = d_zro_deposit_3
		has_deposit = d_zro_deposit_4
		has_deposit = d_zro_deposit_5
	}
}

###########
# Leaders #
###########

# This = Leader
is_leader_mia_or_in_transit = {
	exists = fleet
	if = {
		limit = {
			exists = fleet
		}
		fleet = {
			NOT = {
				exists = solar_system
			}
		}
	}
}

# This = Leader
is_leader_in_combat = {
	exists = fleet
	if = {
		limit = {
			exists = fleet
		}
		fleet = {
			is_in_combat = yes
		}
	}
}

# This = Leader
is_leader_excavating_site = { # Archaeological Site
	exists = fleet
	if = {
		limit = {
			exists = fleet
		}
		fleet = {
			any_archaeological_site = {
				is_site_locked = no
				is_current_excavator_fleet = prev
			}
		}
	}
}

disciple_of_the_cradle_councilors_count = {
	count_owned_leader = {
		limit = {
			is_councilor = yes
			has_trait = leader_trait_disciple_of_the_cradle
		}
	}
}

############
# Enclaves #
############

potential_shroudwalker_enclave = {
	OR = {
		has_overlord_dlc = yes
		has_shroud_dlc = yes
	}
}

shroudwalker_can_perform_delving_ritual = {
	OR = {
		NOR = {
			has_technology = tech_psionic_shield
			has_tech_option = tech_psionic_shield
		}
		NOR = {
			has_technology = tech_precognition_interface
			has_tech_option = tech_precognition_interface
		}
		NOR = {
			has_technology = tech_psi_jump_drive_1
			has_tech_option = tech_psi_jump_drive_1
		}
		NOR = {
			has_technology = tech_zro_launcher
			has_tech_option = tech_zro_launcher
		}
		NOR = {
			has_technology = tech_psionic_lightning
			has_tech_option = tech_psionic_lightning
		}
		NOR = {
			has_technology = tech_psionic_disruptor
			has_tech_option = tech_psionic_disruptor
		}
		NOR = {
			has_technology = tech_psionic_bombers
			has_tech_option = tech_psionic_bombers
		}
	}
}

shroudwalker_no_dialog = {
	OR = {
		is_hostile = event_target:shroudwalker_enclave_country
		is_homicidal = yes
		has_origin = origin_mindwardens
		AND = {
			exists = event_target:mindwarden_enclave_country
			event_target:mindwarden_enclave_country = {
				trust = {
					who = prev
					value >= 100
				}
			}
		}
		has_covenant_with_end_of_the_cycle = yes
	}
}

is_mindwardens_story_system = {
	OR = {
		has_star_flag = mindwarden_system
		has_star_flag = mindwarden_buffer_system
		has_star_flag = exiled_system
	}
}

# This = Fleet
is_mindwarden_enclave_founding_allowed = {
	owner = {
		has_origin = origin_mindwardens
		custom_tooltip = {
			fail_text = cannot_found_mindwarden_enclave
			has_country_flag = can_found_mindwarden_enclave
		}
	}
	is_enclave_founding_allowed = {
		FLEET_SIZE = 100
		STARBASE_SHIP_SIZE = enclave_mindwarden_station
	}
}

is_enclave_country = {
	OR = {
		is_country_type = enclave
		is_country_type = mindwarden_enclave
		is_country_type = enclave_mercenary
	}
}

# This = Fleet
is_enclave_founding_allowed = {
	custom_tooltip = {
		fail_text = enclave.2.tooltip.a
		success_text = enclave.2.tooltip.aa
		exists = owner
		exists = leader
		is_ship_class = shipclass_military
	}
	fleet_size >= $FLEET_SIZE$
	custom_tooltip = {
		fail_text = enclave.2.tooltip.cloak
		count_owned_ship = {
			limit = { has_cloaking_strength > 0 }
			count = 0
		}
	}
	if = {
		limit = {
			owner = { is_nomadic = yes }
		}
		custom_tooltip = {
			fail_text = enclave.2.tooltip.c.nomads
			success_text = enclave.2.tooltip.cc.nomads
			exists = solar_system
			solar_system = {
				any_starbase_in_system = {
					exists = owner
					owner = {
						is_same_value = root.owner
						is_same_value = root.controller
					}
					is_waystation_starbase = yes
					owner = { is_nomadic = yes }
				}
				NOT = {
					any_fleet_in_system = {
						owner = {
							is_hostile = root.owner
						}
					}
				}
			}
		}
	}
	else = {
		custom_tooltip = {
			fail_text = enclave.2.tooltip.c
			success_text = enclave.2.tooltip.cc
			exists = solar_system
			solar_system = {
				exists = owner
				owner = {
					is_same_value = root.owner
					is_same_value = root.controller
				}
				NOT = {
					any_fleet_in_system = {
						owner = {
							is_hostile = root.owner
						}
					}
				}
			}
		}
	}

	custom_tooltip = {
		fail_text = enclave.2.tooltip.b
		success_text = enclave.2.tooltip.bb
		exists = solar_system
		solar_system = {
			NOR = {
				any_fleet_in_system = {
					is_ship_size = $STARBASE_SHIP_SIZE$
				}
				has_star_flag = enclave
			}
		}
	}
	custom_tooltip = {
		fail_text = enclave.2.tooltip.e
		success_text = enclave.2.tooltip.ee
		exists = solar_system
		solar_system = {
			OR = {
				any_system_planet = {
					is_moon = yes
				}
				any_system_planet = {
					is_asteroid = yes
				}
			}
		}
	}
	custom_tooltip = {
		fail_text = enclave.2.tooltip.d
		success_text = enclave.2.tooltip.dd
		controller = {
			enclave_capacity_left >= 1
		}
	}
	custom_tooltip = {
		fail_text = enclave.2.tooltip.f
		NOR = {
			has_ship_owner_type = federation
			has_ship_owner_type = galactic_community
		}
	}
	custom_tooltip = {
		fail_text = enclave.2.tooltip.g
		NOT = {
			any_owned_ship = {
				is_space_fauna = yes
			}
		}
	}
}

# This = Country
is_mercenary_mindwarden_enclave = {
	is_country_type = mindwarden_enclave
	any_situation = {
		is_situation_type = mindwardens_dividends
	}
}

# This = Country
is_mercenary = {
	OR = {
		has_civic = civic_mercenary_enclave
		is_mercenary_mindwarden_enclave = yes
	}
}

# This = Fleet
is_mercenary_station = {
	OR = {
		is_ship_size = enclave_mercenary_station
		is_ship_size = enclave_mindwarden_station
	}
}

# This = Country
has_mercenary_enclave_reached_lvl = {
	OR = {
		has_modifier = mercenary_fleet_size_upgrade_$LVL$
		has_country_flag = mindwardens_upgrade_$LVL$
	}
}

########
# Jobs #
########

bureaucrat_is_superstitious_beliefs_priest = {
	exists = owner
	owner = {
		is_superstitious_beliefs_empire = yes
		is_death_cult_empire = no
		is_megacorp = no
	}
}

bureaucrat_is_superstitious_beliefs_priest_corporate = {
	exists = owner
	owner = {
		is_superstitious_beliefs_empire = yes
		is_death_cult_empire = no
		is_megacorp = yes
	}
}

politician_is_superstitious_beliefs_high_priest = {
	exists = owner
	owner = {
		is_superstitious_beliefs_empire = yes
		is_megacorp = no
	}
}

politician_is_superstitious_beliefs_high_priest_corporate = {
	exists = owner
	owner = {
		is_superstitious_beliefs_empire = yes
		is_megacorp = yes
	}
}

bureaucrat_is_corpse_reader = {
	exists = owner
	owner = {
		is_superstitious_beliefs_empire = yes
		is_death_cult_empire = yes
		is_megacorp = no
	}
}

bureaucrat_is_corpse_broker = {
	exists = owner
	owner = {
		is_superstitious_beliefs_empire = yes
		is_death_cult_empire = yes
		is_megacorp = yes
	}
}
