##############################
# Xenoist Faction
##############################

xenoist = {
	election_header = "GFX_faction_header_yellow"
	guiding_ethic = ethic_xenophile
	visual_ethic = "xenoist_visual_ethic"

	unique = yes
	support_multiplier = 1

	resources = {
		category = pop_factions
		inline_script = pop_faction_types/shared_unity_production
		produces = {
			trigger = {
				owner = {
					OR = {
						has_valid_civic = civic_crowdsourcing
						has_valid_civic = civic_corporate_crowdsourcing
					}
				}
			}
			society_research = @faction_major_output
			physics_research = @faction_minor_output
			mult = @crowdsourcing_multiplier
		}
	}

	extortion_resource = {
		category = pop_factions
		produces = {
			trigger = {
				owner = {
					NOR = {
						has_valid_civic = civic_crowdsourcing
						has_valid_civic = civic_corporate_crowdsourcing
					}
				}
			}
			energy = @faction_base_output
			mult = @faction_extortion_energy_ratio
		}
		produces = {
			trigger = {
				owner = {
					OR = {
						has_valid_civic = civic_crowdsourcing
						has_valid_civic = civic_corporate_crowdsourcing
					}
				}
			}
			society_research = @faction_major_output
			physics_research = @faction_minor_output
			mult = @faction_extortion_crowdsourcing_ratio
		}
	}

	is_potential = {
		is_faction_allowed = yes
	}

	can_join_pre_triggers = {
		is_enslaved = no
		is_being_purged = no
	}

	can_join_faction = {
		inline_script = pop_faction_types/can_join_faction/base
	}

	inline_script = pop_faction_types/attraction/promote_suppress_tags
	pop_attraction_tag = {
		desc = POP_ATTRACTION_EGALITARIAN_POS
		trigger = {
			owner? = {
				is_egalitarian = yes
			}
		}
	}
	pop_attraction_tag = {
		desc = POP_FACTION_ATTRACTION_XENO_POS
		trigger = {
			owner? = {
				any_owned_species = {
					NOT = {
						is_same_species = prev
					}
				}
			}
		}
	}

	attraction = {
		base = 150

		inline_script = pop_faction_types/attraction/promote_suppress_attraction
		inline_script = pop_faction_types/attraction/leader_attraction

		modifier = {
			factor = 1.25
			desc = OWNER_IS_EGALITARIAN
			owner? = { is_egalitarian = yes }
		}

		modifier = {
			factor = 2.0
			NOT = { is_same_species = owner_species }
		}
	}

	leader = {
		base = 100

		modifier = {
			factor = 0.5
			leader_class = scientist
		}

		modifier = {
			factor = 2.0
			NOT = { is_same_species = owner_species }
		}

		modifier = {
			factor = 0.0
			is_event_leader = yes
		}
	}

	# DEMAND: Faction Outraged (by a GalCom Resolution)
	inline_script = {
		script = pop_faction_types/demand_faction_outraged
		FLAG = galcom_resolution_passed_anti_xenoist
	}

	# DEMAND: Faction Pleased (by a GalCom Resolution)
	inline_script = {
		script = pop_faction_types/demand_faction_pleased
		FLAG = galcom_resolution_passed_pro_xenoist
	}

	demand = {
		title = "XENOIST_PASSIVE_STUDY"
		unfulfilled_title = "XENOIST_NO_PASSIVE_STUDY"
		desc = "XENOIST_PASSIVE_STUDY_DESC"

		fulfilled_effect = 10
		unfulfilled_effect = -5

		potential = {
			exists = owner
			owner = {
				any_relation = {
					is_country_type = primitive
				}
			}
		}

		trigger = {
			owner = {
				OR = {
					has_policy_flag = interference_not_allowed
					has_policy_flag = interference_subtle
				}
			}
		}
	}

	demand = { # "XENOIST_NO_SLAVES"
		title = "XENOIST_NO_SLAVES"
		unfulfilled_title = "XENOIST_SLAVES"
		desc = "XENOIST_NO_SLAVES_DESC"

		unfulfilled_effect = -25
		fulfilled_effect = 0.001

		potential = {
			exists = owner
			owner = {
				any_owned_species = {
					NOR = {
						is_same_species = prev
						is_robotic = yes
						AND = {
							is_sapient = no
							NOT = { has_trait = trait_nascent_stage }
						}
					}
				}
			}
		}

		trigger = {
			owner = {
				NOT = {
					#has_policy_flag = slavery_allowed
					any_owned_species = {
						has_citizenship_type = { country = prev type = citizenship_slavery }
					}
				}
			}
		}
	}

	demand = { # "XENOIST_NO_PURGE"
		title = "XENOIST_NO_PURGE"
		unfulfilled_title = "XENOIST_PURGE"
		desc = "XENOIST_NO_PURGE_DESC"

		unfulfilled_effect = -25
		fulfilled_effect = 0.001

		potential = {
			exists = owner
		}

		trigger = {
			owner = {
				NOR = {
					has_policy_flag = purge_displacement_only
					has_policy_flag = purge_allowed
				}
			}
		}
	}

	demand = { # "XENOIST_MULTICULTURALISM"
		title = "XENOIST_MULTICULTURALISM"
		unfulfilled_title = "XENOIST_NO_MULTICULTURALISM"
		desc = "XENOIST_MULTICULTURALISM_DESC"

		fulfilled_effect = 10
		unfulfilled_effect = -0.001

		potential = {
			exists = owner
			owner = {
				num_communications > 3
				any_owned_species = {
					NOR = {
						is_same_species = prev
						has_trait = trait_mechanical
						has_trait = trait_machine_unit
						has_trait = trait_hive_mind
					}
				}
			}
		}

		trigger = {
			owner = {
				num_species > 3
			}
		}
	}

	demand = { # "XENOIST_REFUGEES_WELCOME"
		title = "XENOIST_REFUGEES_WELCOME"
		unfulfilled_title = "XENOIST_REFUGEES_UNWELCOME"
		desc = "XENOIST_REFUGEES_WELCOME_DESC"

		fulfilled_effect = 10
		unfulfilled_effect = -10

		potential = {
			exists = owner
			owner = {
				any_relation = {
					is_country_type = default
					has_communications = prev
				}
			}
		}

		trigger = {
			owner = {
				has_policy_flag = refugees_allowed
			}
		}
	}

	demand = {
		title = "XENOIST_ENCLAVE_BORDERS"
		unfulfilled_title = "XENOIST_NO_ENCLAVE_BORDERS"
		desc = "XENOIST_ENCLAVE_BORDERS_DESC"

		fulfilled_effect = 5
		unfulfilled_effect = -0.001

		potential = {
			exists = owner
			owner = {
				is_nomadic = no
				any_country = {
					is_enclave_country = yes
					has_communications = root.owner
				}
			}
		}

		trigger = {
			owner = {
				any_system_within_border = {
					has_star_flag = enclave
					any_fleet_in_system = {
						exists = owner
						owner = { is_enclave_country = yes }
					}
				}
			}
		}
	}

	demand = {
		title = "XENOIST_ENCLAVE_BORDERS_NOMAD"
		unfulfilled_title = "XENOIST_NO_ENCLAVE_BORDERS"
		desc = "XENOIST_ENCLAVE_BORDERS_NOMAD_DESC"

		fulfilled_effect = 5
		unfulfilled_effect = -0.001

		potential = {
			exists = owner
			owner = {
				is_nomadic = yes
				any_country = {
					is_enclave_country = yes
					has_communications = root.owner
				}
			}
		}

		trigger = {
			owner = {
				any_owned_nonprimary_starbase = {
					is_waystation_starbase = yes
					solar_system? = {
						has_star_flag = enclave
						any_fleet_in_system = {
							exists = owner
							owner = { is_enclave_country = yes }
						}
					}
				}
			}
		}
	}

	demand = {
		title = "XENOIST_RECENT_UPLIFT"
		unfulfilled_title = "XENOIST_NO_RECENT_UPLIFT"
		desc = "XENOIST_RECENT_UPLIFT_DESC"

		fulfilled_effect = 10
		unfulfilled_effect = -0.001

		potential = {
			exists = owner
			owner = {
				has_technology = tech_epigenetic_triggers
			}
		}

		trigger = {
			owner = {
				has_country_flag = recent_uplift_timed
			}
		}
	}

	demand = {
		title = "XENOIST_RECENT_ENLIGHTENMENT"
		unfulfilled_title = "XENOIST_NO_RECENT_ENLIGHTENMENT"
		desc = "XENOIST_RECENT_ENLIGHTENMENT_DESC"

		fulfilled_effect = 10
		unfulfilled_effect = -0.001

		potential = {
			exists = owner
			owner = {
				OR = {
					any_controlled_ship = { is_ship_size = observation_station }
					has_country_flag = recent_enlightenment_timed
					# fixes demand disappearing when fulfilled (station dismantles)
				}
			}
		}

		trigger = {
			owner = {
				has_country_flag = recent_enlightenment_timed
			}
		}
	}

	demand = {
		title = "XENOIST_PRE_SAPIENT_SURVIVAL"
		unfulfilled_title = "XENOIST_NO_PRE_SAPIENT_SURVIVAL"
		desc = "XENOIST_PRE_SAPIENT_SURVIVAL_DESC"

		unfulfilled_effect = -10
		fulfilled_effect = 0.001

		potential = {
			exists = owner
			owner = {
				OR = {
					has_policy_flag = pre_sapients_allow
					has_policy_flag = pre_sapients_purge
				}
				OR = {
					has_country_flag = found_presapients
					any_planet_within_border = {
						any_owned_pop_group = {
							is_sapient = no
							NOT = { has_trait = trait_nascent_stage }
						}
					}
				}
			}
		}

		trigger = {
			owner = {
				has_policy_flag = pre_sapients_allow
			}
		}
	}

	demand = {
		title = "XENOIST_NO_PRIMITIVE_INVASION" # Landgrab Opposition
		unfulfilled_title = "XENOIST_NO_PRIMITIVE_INVASION"
		desc = "XENOIST_NO_PRIMITIVE_INVASION_DESC"

		unfulfilled_effect = -15
		fulfilled_effect = 0.001

		potential = {
			exists = owner
			owner = {
				has_country_flag = encountered_first_primitive
				NOT = { has_origin = origin_necrophage }
			}
		}

		trigger = {
			owner = {
				NOT = { has_country_flag = recent_primitive_invasion }
			}
		}
	}

	demand = {
		title = "XENOIST_FEDERATED"
		unfulfilled_title = "XENOIST_NOT_FEDERATED"
		desc = "XENOIST_FEDERATED_DESC"

		fulfilled_effect = 20
		unfulfilled_effect = -0.001

		potential = {
			exists = owner
			owner = {
				num_communications > 3
			}
		}

		trigger = {
			owner = {
				has_federation = yes
			}
		}
	}

	demand = {
		title = "XENOIST_RECENT_COMMS"
		unfulfilled_title = "XENOIST_NO_RECENT_COMMS"
		desc = "XENOIST_RECENT_COMMS_DESC"

		fulfilled_effect = 5
		unfulfilled_effect = -0.001

		potential = {
			exists = owner
			owner = {
				any_country = {
					NOT = { has_communications = prev}
					OR = {
						is_country_type = default
						is_country_type = fallen_empire
						is_country_type = awakened_fallen_empire
						is_country_type = primitive
						is_country_type = marauder_raiders
						is_enclave_country = yes
					}
				}
			}
		}

		trigger = {
			owner = {
				has_country_flag = recent_comms_timed
			}
		}
	}

	demand = {
		title = "XENOIST_CELEBRATE_DIVERSITY"
		unfulfilled_title = "XENOIST_NO_CELEBRATE_DIVERSITY"
		desc = "XENOIST_CELEBRATE_DIVERSITY_DESC"

		fulfilled_effect = 5
		unfulfilled_effect = -0.001

		potential = {
			exists = owner
			owner = {
				has_modifier = celebrate_diversity
			}
		}

		trigger = {
			owner = {
				has_modifier = celebrate_diversity
			}
		}
	}

	demand = {
		title = "XENOIST_PROCLAIM_SUPREMACY"
		unfulfilled_title = "XENOIST_NO_PROCLAIM_SUPREMACY"
		desc = "XENOIST_PROCLAIM_SUPREMACY_DESC"

		unfulfilled_effect = -5
		fulfilled_effect = 0.001

		potential = {
			exists = owner
			owner = {
				has_modifier = proclaim_superiority
			}
		}

		trigger = {
			owner = {
				NOT= { has_modifier = proclaim_superiority }
			}
		}
	}

	demand = {
		title = "XENOIST_PROACTIVE_FIRST_CONTACT_POLICY"
		unfulfilled_title = "XENOIST_ADOPT_PROACTIVE_FIRST_CONTACT_POLICY"
		desc = "XENOIST_PROACTIVE_FIRST_CONTACT_POLICY_DESC"

		fulfilled_effect = 10
		unfulfilled_effect = -0.001

		potential = {
			exists = owner
			owner = {
				NOT = { has_policy_flag = first_contact_attack_allowed }
				OR = {
					years_passed <= 75
					has_country_flag = recent_first_contact_process_started
				}
			}
		}

		trigger = {
			owner = {
				has_policy_flag = first_contact_proactive
			}
		}
	}

	demand = {
		title = "XENOIST_AGGRESSIVE_FIRST_CONTACT_POLICY"
		unfulfilled_title = "XENOIST_NO_AGGRESSIVE_FIRST_CONTACT_POLICY"
		desc = "XENOIST_AGGRESSIVE_FIRST_CONTACT_POLICY_DESC"

		unfulfilled_effect = -10
		fulfilled_effect = 0.001

		potential = {
			exists = owner
			owner = {
				has_policy_flag = first_contact_attack_allowed
				OR = {
					years_passed <= 75
					has_country_flag = recent_first_contact_process_started
				}
			}
		}

		trigger = {
			owner = {
				NOT = { has_policy_flag = first_contact_attack_allowed }
			}
		}
	}

	demand = {
		title = "COOPERATIVE_DIPLO_STANCE"
		unfulfilled_title = "COOPERATIVE_DIPLO_STANCE"
		desc = "COOPERATIVE_DIPLO_STANCE_DESC"

		fulfilled_effect = 10
		unfulfilled_effect = -0.001

		potential = {
			exists = owner
			owner = {
				is_homicidal = no
				is_country_type = default
				has_policy_flag = diplo_stance_cooperative
			}
		}

		trigger = {
			owner = {
				has_policy_flag = diplo_stance_cooperative
			}
		}
	}

	demand = {
		title = "XENOIST_OPPRESSIVE_SUBJUGATION_POLICY"
		unfulfilled_title = "XENOIST_NO_OPPRESSIVE_SUBJUGATION_POLICY"
		desc = "XENOIST_OPPRESSIVE_SUBJUGATION_POLICY_DESC"

		unfulfilled_effect = -10
		fulfilled_effect = 0.001

		potential = {
			exists = owner
			owner = {
				is_subject = no
				has_policy_flag = oppressive_terms
			}
		}

		trigger = {
			owner = {
				is_subject = no
				NOT = { has_policy_flag = oppressive_terms }
			}
		}
	}

	demand = {
		title = "XENOIST_BENEVOLENT_SUBJUGATION_POLICY"
		unfulfilled_title = "XENOIST_ADOPT_BENEVOLENT_SUBJUGATION_POLICY"
		desc = "XENOIST_BENEVOLENT_SUBJUGATION_POLICY_DESC"

		fulfilled_effect = 10
		unfulfilled_effect = -0.001

		potential = {
			exists = owner
			owner = {
				is_subject = no
				NOT = { has_policy_flag = oppressive_terms }
			}
		}

		trigger = {
			owner = {
				has_policy_flag = benevolent_terms
			}
		}
	}

	demand = { # Unplugged: De-Cyberize
		title = XENOIST_UNPLUGGED_DECYBERIZE
		unfulfilled_title = XENOIST_UNPLUGGED_NOT_DECYBERIZE
		desc = XENOIST_UNPLUGGED_DECYBERIZE_DESC

		unfulfilled_effect = -10
		fulfilled_effect = 5

		potential = {
			exists = owner
			owner = {
				has_origin = origin_unplugged
				any_situation = {
					is_situation_type = situation_unplugged_identity
				}
			}
		}

		trigger = {
			owner = {
				any_situation = {
					is_situation_type = situation_unplugged_identity
					OR = {
						AND = { # safely in the De-Cyberize stage
							current_stage = unplugged_identity_stage_1
							situation_monthly_progress < 0
						}
						AND = { # trending towards De-Cyberization and our Approach is aligned
							situation_monthly_progress < 0.05
							OR = {
								current_situation_approach = situation_unplugged_identity_discourage_change
								current_situation_approach = situation_unplugged_identity_embrace_opinion
							}
						}
					}
				}
			}
		}
	}

	# Global Faction Demands.
	inline_script = {
		script = "pop_faction_types/global_faction_demands"
		ETHIC = ethic_xenophile
		ETHIC_FANATIC = ethic_fanatic_xenophile
		ETHIC_OPPOSED = ethic_xenophobe
	}

	demand = { # Alien Zoo (for conservation)
		title = "XENOIST_XENOLOGICAL_CONSERVATION"
		unfulfilled_title = "XENOIST_NO_XENOLOGICAL_CONSERVATION"
		desc = "XENOIST_XENOLOGICAL_CONSERVATION_DESC"

		fulfilled_effect = 5
		unfulfilled_effect = -0.001

		potential = {
			exists = owner
			owner = {
				has_technology = tech_alien_life_studies
				NOT = { has_valid_civic = civic_warrior_culture }
			}
		}

		trigger = {
			owner = {
				count_owned_colony = {
					limit = {
						has_active_building = building_xeno_zoo
					}
					count > 0
				}
			}
		}
	}

	demand = { # "XENOIST_NO_NOMAD_PURGE"
		title = "XENOIST_NO_NOMAD_PURGE"
		unfulfilled_title = "XENOIST_NOMAD_PURGE"
		desc = "XENOIST_NO_NOMAD_PURGE_DESC"

		fulfilled_effect = 0.001
		unfulfilled_effect = -30

		potential = {
			exists = owner
			owner = {
				is_nomadic = no
				any_country = {
					is_nomadic = yes
					has_communications = root.owner
				}
			}
		}

		trigger = {
			owner = {
				NOT = { has_policy_flag = arkship_raid_purge_pops }
			}
		}
	}

	demand = { # "XENOIST_NO_NOMAD_CONSCRIPTION"
		title = "XENOIST_NO_NOMAD_CONSCRIPTION"
		unfulfilled_title = "XENOIST_NOMAD_CONSCRIPTION"
		desc = "XENOIST_NO_NOMAD_CONSCRIPTION_DESC"

		fulfilled_effect = 0.001
		unfulfilled_effect = -15

		potential = {
			exists = owner
			owner = {
				is_nomadic = no
				any_country = {
					is_nomadic = yes
					has_communications = root.owner
				}
			}
		}

		trigger = {
			owner = {
				NOT = { has_policy_flag = arkship_raid_relocate_pops }
			}
		}
	}

	on_create = {
		pop_faction_event = { id = factions.0 }
	}

	on_destroy = {
		inline_script = {
			script = pop_faction_types/effect/promote_on_destroy
			ETHIC = xenophile
		}
		inline_script = {
			script = pop_faction_types/effect/suppress_on_destroy
			ETHIC = xenophile
		}
	}

	actions = {
		embrace_faction = {
			title = "EMBRACE_FACTION"
			description = "EMBRACE_FACTION_DESC"

			cost = {
				unity = 5000
			}

			potential = {
				exists = owner
				owner = {
					OR = {
						is_subject = no
						NOT = { any_agreement = { agreement_preset = preset_dominion } }
					}
				}
			}

			valid = {
				custom_tooltip = {
					fail_text = EMBRACE_FACTION_COOLDOWN
					owner = {
						NOT = { has_modifier = embraced_faction_timer }
					}
				}
				support > 0.20
				owner = {
					NOT = { has_ethic = "ethic_fanatic_xenophile" }
				}
			}

			effect = {
				add_modifier = { modifier = embraced_faction days = 3600 }
				owner = {
					set_country_flag = has_embraced_faction
					shift_ethic = ethic_xenophile
					hidden_effect = {
						add_modifier = { modifier = embraced_faction_timer days = 3600 }
						every_pop_faction = {
							limit = { NOT = { is_same_value = root } }
							add_modifier = { modifier = embraced_another_faction days = 3600 }
						}
					}
				}
				hidden_effect = {
					save_event_target_as = TargetFaction
					owner = {
						every_relation = {
							limit = {
								is_ai = no
								is_country_type = default
								has_communications = prev
								has_intel_level = {
									who = prev
									category = government
									level >= 1
								}
							}
							country_event = { id = factions.2000 }
						}
					}
				}
			}

			ai_weight = {
				base = 1
				modifier = {
					factor = 0
					support < 0.50
					owner = {
						has_ethic = ethic_xenophile
					}
				}
				modifier = {
					factor = 0
					owner = { has_valid_civic = civic_fanatic_purifiers }
				}
				modifier = {
					factor = 0
					owner = {
						count_pop_faction = {
							count < 4
						}
					}
				}
			}
		}
	}
	inline_script = {
		script = pop_faction_types/actions/promote_faction
		ETHIC = xenophile
	}
	inline_script = {
		script = pop_faction_types/actions/suppress_faction
		ETHIC = xenophile
	}
	actions = {
		# Faction Extortion Buttons
		extort_faction = {
			title = "EXTORT_FACTION"
			description = "EXTORT_FACTION_DESC"

			potential = {
				exists = owner
				owner = {
					is_oligarchic_authority = yes
					has_country_flag = cyber_collectivist
					NOR = {
						has_valid_civic = civic_crowdsourcing
						has_valid_civic = civic_corporate_crowdsourcing
					}
				}
				is_faction_extorted = no
			}

			effect = {
				set_faction_extorted = yes
				custom_tooltip = energy_extortion_tt
				custom_tooltip = ONLY_ONE_EXTORTED_FACTION_TT
			}

			ai_weight = {
				base = 0
			}
		}
		extort_faction_crowdsourcing = { #Crowdsourcing version
			title = "EXTORT_FACTION"
			description = "EXTORT_FACTION_DESC"

			potential = {
				exists = owner
				owner = {
					is_oligarchic_authority = yes
					has_country_flag = cyber_collectivist
					OR = {
						has_valid_civic = civic_crowdsourcing
						has_valid_civic = civic_corporate_crowdsourcing
					}
				}
				is_faction_extorted = no
			}

			effect = {
				set_faction_extorted = yes
				custom_tooltip = crowdsourced_extortion_tt
				custom_tooltip = ONLY_ONE_EXTORTED_FACTION_TT
			}

			ai_weight = {
				base = 0
			}
		}
		cancel_extort_faction = {
			title = "CANCEL_EXTORT_FACTION"
			description = "CANCEL_EXTORT_FACTION_DESC"

			potential = {
				exists = owner
				owner = {
					is_oligarchic_authority = yes
					has_country_flag = cyber_collectivist
				}
				is_faction_extorted = yes
			}

			effect = {
				set_faction_extorted = no
			}

			ai_weight = {
				base = 0
			}
		}
	}
}
