# from = country
# this = species
citizenship_assimilation = {
	is_assimilation = yes

	potential = {
		exists = from
		from = {
			OR = {
				is_homicidal = no
				AND = {
					is_homicidal = yes
					root = {
						is_same_species = prev
					}
				}
				AND = {
					has_civic = civic_machine_terminator
					root = {
						is_robotic_species = yes
					}
				}
			}
			OR = {
				AND = { # machine cyborg assimilator
					is_machine_empire = yes
					has_civic = civic_machine_assimilator
					OR = {
						AND = {
							root = {
								is_robotic_species = yes
							}
						}
						root = {
							NOR = {
								has_trait = trait_mechanical
								has_trait = trait_machine_unit
								has_trait = trait_cybernetic
							}
						}
					}
				}
				AND = { # machine synthetic assimilator
					is_machine_empire = yes
					root = {
						is_robotic_species = yes
					}
				}
				AND = {
					OR = {
						AND = {
							# non-devouring hive mind
							is_hive_empire = yes
							NOT = { has_civic = civic_hive_devouring_swarm }
						}
						# Regular empire
                        AND = {
						NOT = { has_ethic = ethic_gestalt_consciousness }
                            is_individual_machine = no
                        }
					}
					OR = {
                        can_perform_genetic_assimilation = yes
						has_active_tradition = tr_cybernetics_transubstantiation_synthesis # cybernetic assimilation
						AND = {
							has_origin = origin_nomadic_purger
							overlord? = { can_perform_genetic_assimilation = yes }
						}
						AND = {
							has_origin = origin_nomadic_purger
							overlord? = { has_active_tradition = tr_cybernetics_transubstantiation_synthesis }
							ROOT = { NOT = { has_trait = trait_cybernetic } }
						}
					}
					ROOT = { is_robotic_species = no }
				}
				AND = {
					has_country_flag = synth_assimilation # synth assimilation
					is_machine_empire = no
				}
                AND = {
                    OR = {
                        has_active_tradition = tr_psionics_psionic_assimilation # psionic assimilation
                        has_active_tradition = tr_psionics_shroud_great_awakening # psionic assimilation (shroud)
                    }
                    OR = {
                        is_machine_empire = no
						is_individual_machine = no
						ROOT = { is_robotic_species = no }
                    }
                }
				AND = {
					is_individual_machine = yes
					ROOT = { is_robotic_species = yes }
				}
				AND = {	# Mindwarden Anti-Psionics
					has_modifier = destroyers_of_the_lost_modifier
					ROOT = { has_psionic_species_trait = yes }
				}
			}
            OR = {
				NOT = { has_origin = origin_nomadic_purger }
                # Purge vassals inherit their overlord's civics, so a Driven Assimilator's vassal is itself a machine
                # assimilator and should assimilate rather than kill-purge; nomads.11 hands the assimilated pops up.
                # Robotic pops are excluded: the vassal has no way to run that assimilation to completion, so they are
                # integrated as machines and handed up with the rest instead of stalling the vassal forever.
                AND = {
                    has_valid_civic = civic_machine_assimilator
                    ROOT = { is_robotic_species = no }
                }
            }
		}
		NAND = {
			from = {
				has_origin = origin_forever_cruise
				NOT = { has_country_flag = forever_cruise_over }
			}
			OR = {
				is_founding_cruise_crew = yes
				is_founding_cruise_passenger = yes
			}
		}
	}

	allow = {
		hidden_trigger = { exists = from }
		custom_tooltip = {
			fail_text = MUST_NOT_BE_DOMINANT_SPECIES
			NOT = { is_exact_same_species = from }
		}
		custom_tooltip = {
			fail_text = "pre_sapients_citizenship"
			is_sapient = yes
		}
		custom_tooltip = {
			fail_text = "WILDERNESS_DEAD_WORLD"
			if = {
				limit = { from = { is_wilderness_empire = no } }
				NOT = { has_trait = trait_wilderness }
			}
		}
		custom_tooltip = {
			fail_text = "lonely_hive_mind_pops_kill_themselves_citizenship_assimilation"
			if = {
				limit = { is_lonely_hive_mind_pop = yes }
				from = {
                    can_assimilate_hive_minded_pops = yes
				}
			}
		}
        custom_tooltip = {
            fail_text = "assimilate_shroud_forged_tt"
            NOT = { has_trait = trait_shroud_forged }
        }
		custom_tooltip = {
			fail_text = "disconnected_machine_pops_collapse_citizenship"
			OR = {
				from = {
					has_country_flag = synth_assimilation 
				}
				is_disconnected_machine_pop = no
			}
		}
		# stop synth assimilation from busting with other types
		if = {
			limit = {
				from = {
					is_ai = no
					has_country_flag = synth_assimilation 
				}
			}
			custom_tooltip = {
				fail_text = EVERYTHING_GOES_TO_SYTH
				always = yes
			}
		}
		else_if = {
			limit = {
				from = {
					is_machine_empire = yes
				}
			}
			custom_tooltip = { # machine assimilator
				fail_text = MACHINE_SPECIES_MUST_BE_ORGANIC
			}
			custom_tooltip = { 
				fail_text = requires_tradition_tr_synthetics_adopt
			}
		}
		else_if = {
			limit = {
				from = {
					is_hive_empire = yes
					is_wilderness_empire = no
					can_perform_genetic_assimilation = yes
				}
			}
			custom_tooltip = { # non-devouring hive mind
				fail_text = HIVE_SPECIES_MUST_BE_NON_HIVE
				NOR = {
					has_trait = trait_hive_mind
					has_trait = trait_mechanical
					has_trait = trait_machine_unit
				}
			}
		}
		else_if = {
			limit = {
				from = {
					is_wilderness_empire = yes
					can_perform_genetic_assimilation = yes
				}
			}
			custom_tooltip = { # non-devouring hive mind
				fail_text = HIVE_SPECIES_MUST_BE_ORGANIC
				NOR = {
					has_trait = trait_mechanical
					has_trait = trait_machine_unit
					has_trait = trait_wilderness
				}
			}
		}
		else_if = {
			limit = {
				from = {
					is_hive_empire = yes
					has_active_tradition = tr_cybernetics_transubstantiation_synthesis
				}
			}
			custom_tooltip = { # non-devouring hive mind
				fail_text = HIVE_SPECIES_MUST_BE_NON_HIVE
				NOR = {
					has_trait = trait_mechanical
					has_trait = trait_machine_unit
					AND = {
						has_trait = trait_hive_mind
						has_trait = trait_cybernetic
					}
				}
			}
		}
		else_if = {
			limit = {
				from = {
					is_hive_empire = no
					has_active_tradition = tr_cybernetics_transubstantiation_synthesis
				}
			}
			custom_tooltip = { # cyborg empire
				fail_text = CYBORG_SPECIES_MUST_BE_ORGANIC
				OR = {
                    # if you are cyborg poking a hive mind, you must also be able to genetic assimilate them
					AND = {
						has_trait = trait_hive_mind
						from = {
							NOR = {
								is_hive_empire = yes
                                can_perform_genetic_assimilation = yes
							}
						}
					}
					AND = {
						NOT = {
							has_trait = trait_hive_mind
						}
						from = {
							is_hive_empire = yes
                            can_perform_genetic_assimilation = no
						}
					}
                    # if they are already cyborgs, you need to be able to psionic assimilate them
                    AND = {
						has_trait = trait_cybernetic
                        from = { aap_can_psionic_assimilate = yes }
                    }
                    # you can't cyborg assimilate robots
					NOR = {
						has_trait = trait_mechanical
						has_trait = trait_machine_unit
                        # you didn't pass the psionic check above, so you cannot re-assimilate cyborgs!
                        has_trait = trait_cybernetic
					}
				}
			}
		}
		else_if = {
            # Note:  Be careful this is the "you do not have the Shadows of Shroud DLC" Path.
            # It CANNOT be combined with the SoS path under any circumstance.
            limit = {
                from = {
                    has_active_tradition = tr_psionics_psionic_assimilation
                }
            }
			custom_tooltip = { # species must be organic to transcend
				fail_text = TRANSCENDING_SPECIES_MUST_BE_ORGANIC
				is_robotic_species = no
			}
			custom_tooltip = { # species already has psionic trait
				fail_text = TRANSCENDING_SPECIES_IS_ALREADY_PSIONIC
                is_psionic_species = no
			}
			#
			# Allow assimilation if they have the genetics tree
			#
			if = {
				limit = {
					has_trait = trait_hive_mind
				}
				custom_tooltip = { # transcending species cannot be gestalt consciousness
					fail_text = TRANSCENDING_SPECIES_CANT_BE_HIVE
                    from = { can_perform_genetic_assimilation = yes }
				}
			}
		}
        else_if = {
            limit = {
                from = { has_active_tradition = tr_psionics_shroud_great_awakening }
            }
            # non organic and gestalt species are allowed in Shroud
            custom_tooltip = { # species already has psionic trait
                fail_text = TRANSCENDING_SPECIES_IS_ALREADY_PSIONIC
                is_psionic_species = no
            }
        }
        else_if = {
            limit = {
                from = { can_perform_genetic_assimilation = yes }
            }
            custom_tooltip = {
                fail_text = CAN_ONLY_ASSIMILATE_HIVES
                has_trait = trait_hive_mind
            }
        }
	}

	ai_will_do = {
		factor = 100
        modifier = {
            # Nomad purge-vassal of a Driven Assimilator: strongly prefer assimilating valid targets over purging them
            add = 1000
            from = {
                has_origin = origin_nomadic_purger
                has_valid_civic = civic_machine_assimilator
            }
        }
		modifier = {
			factor = 0
			from = {
				OR = {
					has_valid_civic = civic_hive_devouring_swarm
					has_valid_civic = civic_fanatic_purifiers
					has_valid_civic = civic_machine_terminator
				}
			}
		}
        modifier = {
            factor = 0
            has_species_flag = original_main_species
            from = { has_origin = origin_shroud_forged }
        }
        modifier = {
            factor = 0.3
            from = {
                is_individual_machine = yes
            }
        }
	}
}