### Federations Scripted Triggers

scion_losing_defensive_war = {
	any_war = {
		any_defender = {
			is_same_value = root
		}
		any_attacker = {
			OR = {
				relative_power = {
					who = root
					category = fleet
					value = overwhelming
				}
				relative_power = {
					who = root
					category = fleet
					value = superior
				}
			}
		}
		defender_war_exhaustion > 0.8
		attacker_war_exhaustion < 0.6
	}
}

scion_losing_offensive_war = {
	any_war = {
		any_attacker = {
			is_same_value = root
		}
		any_defender = {
			OR = {
				relative_power = {
					who = root
					category = fleet
					value = overwhelming
				}
				relative_power = {
					who = root
					category = fleet
					value = superior
				}
			}
		}
		attacker_war_exhaustion > 0.8
		defender_war_exhaustion < 0.6
	}
}

scion_losing_defensive_war_combatant = {
	any_war = {
		any_defender = {
			is_same_value = root
		}
		any_attacker = {
			is_same_value = prevprev
			OR = {
				relative_power = {
					who = root
					category = fleet
					value = overwhelming
				}
				relative_power = {
					who = root
					category = fleet
					value = superior
				}
			}
		}
		defender_war_exhaustion > 0.8
		attacker_war_exhaustion < 0.6
	}
}

scion_losing_offensive_war_combatant = {
	any_war = {
		any_attacker = {
			is_same_value = root
		}
		any_defender = {
			is_same_value = prevprev
			OR = {
				relative_power = {
					who = root
					category = fleet
					value = overwhelming
				}
				relative_power = {
					who = root
					category = fleet
					value = superior
				}
			}
		}
		attacker_war_exhaustion > 0.8
		defender_war_exhaustion < 0.6
	}
}

is_doomsday_planet = {
	OR = {
		has_modifier = doomsday_1
		has_modifier = doomsday_2
		has_modifier = doomsday_3
		has_modifier = doomsday_4
		has_modifier = doomsday_5
	}
}

has_make_spiritualist_perk = {
	exists = federation
	federation = { has_federation_perk = make_spiritualist_1 }
}

federation_election_underway = {
	OR = {
		has_federation_flag = conclave_underway
		has_federation_flag = bidding_war_underway
		has_federation_flag = arena_combat_underway
		has_federation_flag = psionic_battle_underway
		has_federation_flag = thesis_underway
	}
}

has_habitat_farming_designation = {
	OR = {
		has_designation = col_habitat_farming
		has_designation = col_habitat_gestalt_farming
	}
}

has_orbital_trade_deposit = {
	OR = {
		has_deposit = d_trade_value_1
		has_deposit = d_trade_value_2
		has_deposit = d_trade_value_3
		has_deposit = d_trade_value_4
		has_deposit = d_trade_value_5
		has_deposit = d_trade_value_6
		has_deposit = d_trade_value_7
		has_deposit = d_trade_value_8
		has_deposit = d_trade_value_9
		has_deposit = d_trade_value_10
	}
}

has_orbital_energy_deposit = {
	OR = {
		has_deposit = d_energy_1
		has_deposit = d_energy_2
		has_deposit = d_energy_3
		has_deposit = d_energy_4
		has_deposit = d_energy_5
		has_deposit = d_energy_6
		has_deposit = d_energy_7
		has_deposit = d_energy_8
		has_deposit = d_energy_9
		has_deposit = d_energy_10
		has_deposit = d_guardian_dragon_hoard
		has_deposit = d_protostar
	}
}

has_orbital_mining_deposit = {
	OR = {
		has_deposit = d_minerals_1
		has_deposit = d_minerals_2
		has_deposit = d_minerals_3
		has_deposit = d_minerals_4
		has_deposit = d_minerals_5
		has_deposit = d_minerals_6
		has_deposit = d_minerals_7
		has_deposit = d_minerals_8
		has_deposit = d_minerals_9
		has_deposit = d_minerals_10
		has_deposit = d_alloys_1
		has_deposit = d_alloys_2
		has_deposit = d_alloys_3
		has_deposit = d_alloys_4
		has_deposit = d_alloys_5
		has_deposit = d_alloys_10
		has_deposit = d_alloys_25
		has_deposit = d_guardian_dragon_hoard
		has_deposit = d_consumer_goods_obsessional_directive
		has_deposit = d_arc_furnace_1
		has_deposit = d_arc_furnace_2
		has_deposit = d_arc_furnace_3
		has_deposit = d_arc_furnace_4
	}
}

has_orbital_mineral_deposit = {
	OR = {
		has_deposit = d_minerals_1
		has_deposit = d_minerals_2
		has_deposit = d_minerals_3
		has_deposit = d_minerals_4
		has_deposit = d_minerals_5
		has_deposit = d_minerals_6
		has_deposit = d_minerals_7
		has_deposit = d_minerals_8
		has_deposit = d_minerals_9
		has_deposit = d_minerals_10
		has_deposit = d_guardian_dragon_hoard
		has_deposit = d_arc_furnace_1
		has_deposit = d_arc_furnace_2
		has_deposit = d_arc_furnace_3
		has_deposit = d_arc_furnace_4
	}
}

has_orbital_physics_deposit = {
	OR = {
		has_deposit = d_physics_1
		has_deposit = d_physics_2
		has_deposit = d_physics_3
		has_deposit = d_physics_4
		has_deposit = d_physics_5
		has_deposit = d_physics_6
		has_deposit = d_physics_7
		has_deposit = d_physics_8
		has_deposit = d_physics_9
		has_deposit = d_physics_10
		has_deposit = d_physics_25
	}
}

has_orbital_society_deposit = {
	OR = {
		has_deposit = d_society_1
		has_deposit = d_society_2
		has_deposit = d_society_3
		has_deposit = d_society_4
		has_deposit = d_society_5
		has_deposit = d_society_6
		has_deposit = d_society_7
		has_deposit = d_society_8
		has_deposit = d_society_9
		has_deposit = d_society_10
		has_deposit = d_society_15
		has_deposit = d_society_25
	}
}

has_orbital_engineering_deposit = {
	OR = {
		has_deposit = d_engineering_1
		has_deposit = d_engineering_2
		has_deposit = d_engineering_3
		has_deposit = d_engineering_4
		has_deposit = d_engineering_5
		has_deposit = d_engineering_6
		has_deposit = d_engineering_7
		has_deposit = d_engineering_8
		has_deposit = d_engineering_9
		has_deposit = d_engineering_10
		has_deposit = d_engineering_25
	}
}

has_orbital_research_deposit = {
	OR = {
		has_orbital_physics_deposit = yes
		has_orbital_society_deposit = yes
		has_orbital_engineering_deposit = yes
	}
}

has_orbital_alloys_deposit = {
	OR = {
		has_deposit = d_alloys_1
		has_deposit = d_alloys_2
		has_deposit = d_alloys_3
		has_deposit = d_alloys_4
		has_deposit = d_alloys_5
		has_deposit = d_alloys_10
		has_deposit = d_alloys_25
		has_deposit = d_arc_furnace_3
		has_deposit = d_arc_furnace_4
	}
}

habitat_owner_has_tech_for_deposit = {
	if = {
		limit = {
			has_resource = {
				type = volatile_motes
				amount > 0
			}
		}
		root.owner = {
			has_technology = tech_mine_volatile_motes
		}
	}
	if = {
		limit = {
			has_resource = {
				type = exotic_gases
				amount > 0
			}
		}
		root.owner = {
			has_technology = tech_mine_exotic_gases
		}
	}
	if = {
		limit = {
			has_resource = {
				type = rare_crystals
				amount > 0
			}
		}
		root.owner = {
			has_technology = tech_mine_rare_crystals
		}
	}
	if = {
		limit = {
			has_resource = {
				type = sr_living_metal
				amount > 0
			}
		}
		root.owner = {
			has_technology = tech_mine_living_metal
		}
	}
	if = {
		limit = {
			has_resource = {
				type = sr_zro
				amount > 0
			}
		}
		root.owner = {
			has_technology = tech_mine_zro
		}
	}
	if = {
		limit = {
			has_resource = {
				type = sr_dark_matter
				amount > 0
			}
		}
		root.owner = {
			has_technology = tech_mine_dark_matter
		}
	}
	if = {
		limit = {
			has_resource = {
				type = astral_threads
				amount > 0
			}
		}
		root.owner = {
			has_technology = tech_astral_harvesting
		}
	}
}

# Checks if an empire can form a federation with another empire
# this = Country scope, the other empire that is being checked as a potential federation partner
# prev = Country scope, the original empire that checks if they can form a federation with the other empire
can_form_federation_with_empire = {
	this = { is_country_type = default } # The other country should be normal - we know that WE are
	NOR = {
		AND = { # We are a Determined Exterminator or Fanatic Purifiers and they are not the same species as us or a machine empire
			prev = {
				OR = {
					has_valid_civic = civic_machine_terminator
					has_valid_civic = civic_fanatic_purifiers
				}
			}
			this = {
				NOR = {
					is_same_species = from
					OR = {
						has_country_flag = synthetic_empire
						is_machine_empire = yes
						is_individual_machine = yes
					}
				}
			}
		}
		AND = { # As above, but THEY are a Determined Exterminator or Fanatic Purifiers
			prev = {
				NOR = {
					is_same_species = from
					OR = {
						has_country_flag = synthetic_empire
						is_machine_empire = yes
						is_individual_machine = yes
					}
				}
			}
			this = {
				OR = {
					has_valid_civic = civic_machine_terminator
					has_valid_civic = civic_fanatic_purifiers
				}
			 }
		}
	}
	NOR = { # Devouring Swarms are too hangry to form federations
		prev = { has_valid_civic = civic_hive_devouring_swarm }
		this = { has_valid_civic = civic_hive_devouring_swarm }
	}
	NOR = { # Inward Perfectionists would never consider joining
		prev = { has_valid_civic = civic_inwards_perfection }
		this = { has_valid_civic = civic_inwards_perfection }
	}
	NOR = { # Endbringers want to destroy everything
		prev = { has_origin = origin_endbringers }
		this = { has_origin = origin_endbringers }
	}
}

galcom_has_astral_studies_network_or_greater_trigger = {
	OR = {
		is_active_resolution = "resolution_galacticstudies_astral_studies_network"
		is_active_resolution = "resolution_galacticstudies_advanced_xenostudies"
		is_active_resolution = "resolution_galacticstudies_ethical_guideline_refactoring"
		is_active_resolution = "resolution_galacticstudies_extradimensional_experimentation"
	}
}

# True if this country shares a federation with $TARGET$ and that federation grants its members free,
# automatic Commercial Pacts (via the Trade League's passive perk or the Commercial Pacts federation law).
# Usage: has_free_commercial_pact_with = { TARGET = <country scope> }
has_free_commercial_pact_with = {
	federation? = {
		any_member = { is_same_value = $TARGET$ }
		OR = {
			has_federation_perk = trade_federation_passive
			has_federation_law = commercial_pact_yes
		}
	}
}

# True if this country shares a federation with $TARGET$ and that federation grants its members free,
# automatic Research Agreements (via the Research Cooperative's passive perk or the Research Agreements law).
# Usage: has_free_research_pact_with = { TARGET = <country scope> }
has_free_research_pact_with = {
	federation? = {
		any_member = { is_same_value = $TARGET$ }
		OR = {
			has_federation_perk = research_federation_passive
			has_federation_law = research_pact_yes
		}
	}
}

# True if this country shares a federation with $TARGET$ and that federation grants its members free,
# automatic Wayline Pacts (via the Trade League's passive perk or the Commercial Pacts federation law,
# which also covers Wayline Pacts). Wayline pacts are a Nomads mechanic, so this is gated on the Nomads DLC.
# Usage: has_free_waystation_pact_with = { TARGET = <country scope> }
has_free_waystation_pact_with = {
	has_nomads_dlc = yes
	federation? = {
		any_member = { is_same_value = $TARGET$ }
		OR = {
			has_federation_perk = trade_federation_passive
			has_federation_law = commercial_pact_yes
		}
	}
}
