
namespace = action
# Assimilate Pops on Yearly Pulse
event = {
	id = action.64
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_playable_country = {
			num_unique_species > 1
			any_owned_species = {
				has_citizenship_type = {
					type = citizenship_assimilation
					country = prev
				}
			}
		}
	}

	immediate = {
		log = "action.64"
		set_update_modifiers_batch = begin
		every_playable_country = {
			limit = {
				num_unique_species > 1
				any_owned_species = {
					has_citizenship_type = {
						type = citizenship_assimilation
						country = prev
					}
				}
			}
			log = "action.64.epc"

			if = { # ascended synths
				limit = {
					NOT = { has_country_flag = synthetic_empire }
				}
				every_owned_species = {
					limit = {
						has_citizenship_type = {
							type = citizenship_assimilation
							country = prev
						}
					}
					if = { # machines assimlating machines
						limit = {
							OR = {
								has_trait = trait_mechanical
								has_trait = trait_machine_unit
							}
							prev = {
								has_authority = auth_machine_intelligence
								has_active_tradition = tr_synthetics_adopt_machine
							}
						}
						# Deliberately do nothing here, see assimilation_effect
					}
					else_if = { # assimilator empire or cyborg empire
						limit = {
							prev = {
								OR = {
									AND = {
										is_hive_empire = no
										has_tradition = tr_cybernetics_transubstantiation_synthesis
									}
									AND = {
										has_authority = auth_machine_intelligence
										OR = {
                                            has_civic = civic_machine_assimilator
                                            has_ascension_perk = ap_organo_machine_interfacing_assimilator
                                        }
									}
								}
							}
						}
						modify_species = {
							species = this
							add_trait = trait_cybernetic
							add_traits_at_start_of_list = yes
							remove_trait = trait_hive_mind
							remove_trait = trait_self_modified
							remove_trait = trait_latent_psionic
							remove_trait = trait_psionic
							change_scoped_species = no

							effect = {
								set_timed_species_flag = {
									flag = assimilation_species_of_species@prev
									days = 30
								}
								set_timed_species_flag = {
									flag = assimilation_species_of_empire@prevprev
									days = 30
								}
							}
						}
					}
					else_if = { # genetic hive mind
						limit = {
							prev = {
								has_authority = auth_hive_mind
								has_tradition = tr_genetics_creation
								NOT = {
									has_civic = civic_hive_devouring_swarm
								}
							}
						}
						modify_species = {
							species = this
							add_trait = trait_hive_mind
							remove_trait = trait_self_modified
							remove_trait = trait_latent_psionic
							remove_trait = trait_psionic
							change_scoped_species = no

							effect = {
								set_timed_species_flag = {
									flag = assimilation_species_of_species@prev
									days = 30
								}
								set_timed_species_flag = {
									flag = assimilation_species_of_empire@prevprev
									days = 30
								}
							}
						}
					}
					else_if = { # cyborg hive mind
						limit = {
							prev = {
								has_authority = auth_hive_mind
								has_tradition = tr_cybernetics_transubstantiation_synthesis
								NOT = {
									has_civic = civic_hive_devouring_swarm
								}
							}
						}
						modify_species = {
							species = this
							add_trait = trait_cybernetic
							add_trait = trait_hive_mind
							add_traits_at_start_of_list = yes
							remove_trait = trait_self_modified
							remove_trait = trait_latent_psionic
							remove_trait = trait_psionic
							change_scoped_species = no

							effect = {
								set_timed_species_flag = {
									flag = assimilation_species_of_species@prev
									days = 30
								}
								set_timed_species_flag = {
									flag = assimilation_species_of_empire@prevprev
									days = 30
								}
							}
						}
					}
					else_if = {
						limit = {
							prev = {
								NOR = {
									has_ethic = ethic_gestalt_consciousness
									has_tradition = tr_cybernetics_transubstantiation_synthesis
								}
							}
							has_trait = trait_hive_mind
						}
						modify_species = {
							species = this
							remove_trait = trait_hive_mind
							change_scoped_species = no

							effect = {
								set_timed_species_flag = {
									flag = assimilation_species_of_species@prev
									days = 30
								}
								set_timed_species_flag = {
									flag = assimilation_species_of_empire@prevprev
									days = 30
								}
							}
						}
					}
					else_if = { #### Psionic Assimilation ADD OTHER RANDOM OPTIONS TOO
						limit = {
							prev = {
								has_tradition = tr_psionics_psionic_assimilation
							}
						}
						modify_species = {
							species = this
							add_trait = trait_psionic
							add_traits_at_start_of_list = yes
							remove_trait = trait_latent_psionic
							remove_trait = trait_cybernetic
							inline_script = {
								script = traits/remove_all_cybernetic_traits
							}

							effect = {
								set_timed_species_flag = {
									flag = assimilation_species_of_species@prev
									days = 30
								}
								set_timed_species_flag = {
									flag = assimilation_species_of_empire@prevprev
									days = 30
								}
							}
						}
					}
				}
			}

			every_owned_planet = {
				planet_event = { id = action.65 }
			}
		}
		set_update_modifiers_batch = end
	}
}