#########################################
#
# Assimilation Events
#
#########################################

# Each type of assimilation uses a separate X00 block of namespace to future proof this.

namespace = assimilation

# A pop amount has been grown, try to transfer it to assimilation target if applicable
# This = Planet scope
# From = Country
# FromFrom = Pop group
# local_pop_amount = variable of how much was grown
carrier_event = {
	id = assimilation.40
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		check_variable = {
			which = local_pop_amount
			value > 0
		}
		fromfrom = {
			species = {
				has_citizenship_type = {
					type = citizenship_assimilation
					country = root.from
				}
				any_galaxy_species = {
					has_species_flag = assimilation_species_of_species@prev
					has_species_flag = assimilation_species_of_empire@root.from
					is_same_species = prev
				}
			}
			pop_group_size > 0
		}
	}

	immediate = {
		fromfrom = {
			save_event_target_as = old_pop_group
			species = {
				save_event_target_as = old_species
			}
		}
		random_galaxy_species = {
			limit = {
				has_species_flag = assimilation_species_of_species@event_target:old_species
				has_species_flag = assimilation_species_of_empire@root.from
				is_same_species = event_target:old_species
			}
			save_event_target_as = target_species
		}
		if = {
			limit = {
				check_variable = {
					which = local_pop_amount
					value > 0
				}
				event_target:old_pop_group = {
					pop_group_size > 0
				}
			}
			create_pop_group = {
				pop_group = event_target:old_pop_group
				species = event_target:target_species
				size = 0
				effect = {
					transfer_pop_amount = {
						source = event_target:old_pop_group
						target = this
						amount = root.local_pop_amount
						growth_category = "GROWTH_CAT_GROWTH"
					}
					apply_post_assimilation_effects = yes
				}
			}
		}
	}
}

# A pop amount has been assembled, try to transfer it to assimilation target if applicable
# This = Planet scope
# From = Country
# FromFrom = Pop group
# local_pop_amount = variable of how much was assembled
carrier_event = {
	id = assimilation.41
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		check_variable = {
			which = local_pop_amount
			value > 0
		}
		fromfrom = {
			species = {
				has_citizenship_type = {
					type = citizenship_assimilation
					country = root.from
				}
				any_galaxy_species = {
					has_species_flag = assimilation_species_of_species@prev
					has_species_flag = assimilation_species_of_empire@root.from
					is_same_species = prev
				}
			}
			pop_group_size > 0
		}
	}

	immediate = {
		fromfrom = {
			save_event_target_as = old_pop_group
			species = {
				save_event_target_as = old_species
			}
		}
		random_galaxy_species = {
			limit = {
				has_species_flag = assimilation_species_of_species@event_target:old_species
				has_species_flag = assimilation_species_of_empire@root.from
				is_same_species = event_target:old_species
			}
			save_event_target_as = target_species
		}
		from = {
			create_pop_group = {
				pop_group = event_target:old_pop_group
				species = event_target:target_species
				size = 0
				effect = {
					transfer_pop_amount = {
						source = event_target:old_pop_group
						target = this
						amount = root.local_pop_amount
						growth_category = "GROWTH_CAT_ASSEMBLY"
					}
					apply_post_assimilation_effects = yes
				}
			}
		}
	}
}

# Pops Sub-Species integration on Monthly Pulse
event = {
	id = assimilation.50
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_playable_country = {
			any_owned_species = {
				is_being_integrated_by = prev
			}
		}
	}

	immediate = {
		every_playable_country = {
			limit = {
				any_owned_species = {
					is_being_integrated_by = prev
				}
			}
			integrate_species = yes
		}
	}
}

# Assimilate Pops on Monthly Pulse
event = {
	id = assimilation.100
	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 = {
		every_playable_country = {
			limit = {
				num_unique_species > 1
				any_owned_species = {
					has_citizenship_type = {
						type = citizenship_assimilation
						country = prev
					}
				}
			}
			# Cyborg Assimilation - Driven Assimilator or Regular Empire
			if = {
				limit = {
					is_hive_empire = no
					num_unique_species > 1
					any_owned_species = {
						has_cyborg_assimilation_living_standard = {
							COUNTRY = prev
						}
					}
				}
				every_owned_colony = {
					limit = {
						NOT = {
							is_planet_class = pc_cosmogenesis_world
						}
					}
					carrier_event = {
						id = assimilation.200
					}
				}
			}
			# Cyborg Assimilation - Hive Mind
			else_if = {
				limit = {
					is_hive_empire = yes
					num_unique_species > 1
					any_owned_species = {
						has_cyborg_assimilation_living_standard = {
							COUNTRY = prev
						}
					}
				}
				every_owned_colony = {
					limit = {
						NOT = {
							is_planet_class = pc_cosmogenesis_world
						}
					}
					carrier_event = {
						id = assimilation.210
					}
				}
			}
			# Psionic Assimilation - Default Empires
			if = {
				limit = {
					is_hive_empire = no
					is_machine_empire = no
					any_owned_species = {
						has_psionic_assimilation_living_standard = {
							COUNTRY = prev
						}
					}
				}
				every_owned_colony = {
					limit = {
						NOT = {
							is_planet_class = pc_cosmogenesis_world
						}
					}
					carrier_event = {
						id = assimilation.300
					}
				}
			}
			# Psionic Assimilation - Hive Mind
			if = {
				limit = {
					is_hive_empire = yes
					any_owned_species = {
						has_psionic_assimilation_living_standard = {
							COUNTRY = prev
						}
					}
				}
				every_owned_colony = {
					limit = {
						NOT = {
							is_planet_class = pc_cosmogenesis_world
						}
					}
					carrier_event = {
						id = assimilation.310
					}
				}
			}
			# Psionic Assimilation - Machine
			if = {
				limit = {
					is_machine_empire = yes
					any_owned_species = {
						has_psionic_assimilation_living_standard = {
							COUNTRY = prev
						}
					}
				}
				every_owned_colony = {
					limit = {
						NOT = {
							is_planet_class = pc_cosmogenesis_world
						}
					}
					carrier_event = {
						id = assimilation.320
					}
				}
			}
			# Synthetic/Machine Assimilation
			if = {
				limit = {
					num_unique_species > 1
					any_owned_species = {
						has_synthetic_assimilation_living_standard = {
							COUNTRY = prev
						}
					}
				}
				every_owned_colony = {
					limit = {
						NOT = {
							is_planet_class = pc_cosmogenesis_world
						}
					}
					carrier_event = {
						id = assimilation.400
					}
				}
			}
			if = {
				limit = {
					is_robotic_species = yes
					has_tradition = tr_synthetics_transubstatiation_synthesis
					num_unique_species > 1
					any_owned_species = {
						is_robotic = yes
						has_citizenship_type = {
							type = citizenship_assimilation
							country = prev
						}
					}
				}
				every_owned_colony = {
					limit = {
						NOT = {
							is_planet_class = pc_cosmogenesis_world
						}
					}
					carrier_event = {
						id = assimilation.415
					}
				}
			}
			# Hive Dissociation Assimilation
			if = {
				limit = {
					num_unique_species > 1
					any_owned_species = {
						has_dissociation_assimilation_living_standard = {
							COUNTRY = prev
						}
					}
				}
				every_owned_colony = {
					limit = {
						NOT = {
							is_planet_class = pc_cosmogenesis_world
						}
					}
					carrier_event = {
						id = assimilation.500
					}
				}
			}
			# Hive Mind Ego Assimilation
			if = {
				limit = {
					is_wilderness_empire = no
					num_unique_species > 1
					any_owned_species = {
						has_ego_assimilation_living_standard = {
							COUNTRY = prev
						}
					}
				}
				every_owned_colony = {
					limit = {
						NOT = {
							is_planet_class = pc_cosmogenesis_world
						}
					}
					carrier_event = {
						id = assimilation.600
					}
				}
			}
			# Wilderness Ego Assimilation
			if = {
				limit = {
					is_wilderness_empire = yes
					num_unique_species > 1
					any_owned_species = {
						has_ego_assimilation_living_standard = {
							COUNTRY = prev
						}
					}
				}
				every_owned_colony = {
					limit = {
						NOT = {
							is_planet_class = pc_cosmogenesis_world
						}
					}
					carrier_event = {
						id = assimilation.610
					}
				}
			}
			# Shroud Inoculation Assimilation - Mindwardens
			if = {
				limit = {
					is_hive_empire = no
					num_unique_species > 1
					any_owned_species = {
						has_anti_psionic_assimilation_living_standard = {
							COUNTRY = prev
						}
					}
				}
				every_owned_colony = {
					limit = {
						NOT = {
							is_planet_class = pc_cosmogenesis_world
						}
					}
					carrier_event = {
						id = assimilation.700
					}
				}
			}
		}
	}
}

# Pops Similar Sub-Species merge on Monthly Pulse
event = {
	id = assimilation.150
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		every_playable_country = {
			merge_species = yes
		}
	}
}

# Cyborg Assimilation - Driven Assimilator or Regular Empire
carrier_event = {
	id = assimilation.200
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_owned_species = {
			has_cyborg_assimilation_living_standard = {
				COUNTRY = root.owner
			}
		}
	}

	immediate = {
		inline_script = {
			script = assimilation/run
			TYPE = cyborg
			TARGET_SPECIES = create_assimilation_species
			TRAITS = "
				add_trait = trait_cybernetic
				remove_trait = trait_self_modified
				inline_script = traits/remove_all_psionic_traits
				remove_trait = trait_hive_mind
			"
			TRANSFER = assimilate_pop_group_effect
		}
	}
}

# Cyborg Assimilation - Hive Mind
carrier_event = {
	id = assimilation.210
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_owned_species = {
			has_cyborg_assimilation_living_standard = {
				COUNTRY = root.owner
			}
		}
	}

	immediate = {
		inline_script = {
			script = assimilation/run
			TYPE = cyborg
			TARGET_SPECIES = create_assimilation_species
			TRAITS = "
				add_trait = trait_cybernetic
				remove_trait = trait_self_modified
				inline_script = traits/remove_all_psionic_traits
				add_trait = trait_hive_mind
			"
			TRANSFER = assimilate_pop_group_effect
		}

		# grant timed modifiers from cyber hive
		if = {
			limit = {
				owner = {
					OR = {
						has_tradition = tr_cybernetics_finish
						AND = {
							has_origin = origin_nomadic_purger
							overlord? = {
								has_tradition = tr_cybernetics_finish
							}
						}
					}
				}
			}
			# A purge vassal disbands, so the empire-wide node boost goes to its Overlord; the planet boost stays.
			if = {
				limit = {
					owner = {
						has_origin = origin_nomadic_purger
					}
				}
				owner = {
					overlord? = {
						add_modifier = {
							modifier = cyber_hive_node_booster
							days = 90
						}
					}
				}
			}
			else = {
				owner = {
					add_modifier = {
						modifier = cyber_hive_node_booster
						days = 90
					}
				}
			}
			add_modifier = {
				modifier = cyber_hive_planet_booster
				days = 90
			}
		}
	}
}

# Psionic Assimilation
carrier_event = {
	id = assimilation.300
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_owned_species = {
			has_psionic_assimilation_living_standard = {
				COUNTRY = root.owner
			}
		}
	}

	immediate = {
		inline_script = {
			script = assimilation/run
			TYPE = psionic
			TARGET_SPECIES = create_assimilation_species
			TRAITS = "
				add_trait = trait_psionic
				remove_trait = trait_self_modified
				remove_trait = trait_latent_psionic
				remove_trait = trait_robot_latent_psionic
				remove_trait = trait_cybernetic
				inline_script = traits/remove_all_cybernetic_traits
				remove_trait = trait_hive_mind
			"
			TRANSFER = assimilate_pop_group_effect
		}
	}
}


# Psionic Assimilation - Hive Mind
carrier_event = {
	id = assimilation.310
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_owned_species = {
			has_psionic_assimilation_living_standard = {
				COUNTRY = root.owner
			}
		}
	}

	immediate = {
		inline_script = {
			script = assimilation/run
			TYPE = psionic
			TARGET_SPECIES = create_assimilation_species
			TRAITS = "
				add_trait = trait_psionic
				remove_trait = trait_self_modified
				remove_trait = trait_latent_psionic
				remove_trait = trait_robot_latent_psionic
				remove_trait = trait_cybernetic
				inline_script = traits/remove_all_cybernetic_traits
				add_trait = trait_hive_mind
			"
			TRANSFER = assimilate_pop_group_effect
		}
	}
}

# Psionic Assimilation - Machine
carrier_event = {
	id = assimilation.320
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_owned_species = {
			has_psionic_assimilation_living_standard = {
				COUNTRY = root.owner
			}
		}
	}

	immediate = {
		inline_script = {
			script = assimilation/run
			TYPE = psionic
			TARGET_SPECIES = use_founder_species_as_assimilation_target
			TRAITS = ""
			TRANSFER = assimilate_pop_group_effect
		}
	}
}

# Synthetic/Machine Assimilation
carrier_event = {
	id = assimilation.400
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_owned_species = {
			has_synthetic_assimilation_living_standard = {
				COUNTRY = root.owner
			}
		}
	}

	immediate = {
		inline_script = {
			script = assimilation/run
			TYPE = synthetic
			TARGET_SPECIES = use_founder_species_as_assimilation_target
			TRAITS = ""
			TRANSFER = assimilate_pop_group_effect
		}
	}
}

carrier_event = {
	id = assimilation.415
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_owned_species = {
			is_robotic = yes
			has_citizenship_type = {
				type = citizenship_assimilation
				country = root.owner
			}
		}
	}

	immediate = {
		set_assimilation_counter_variable = yes
		set_update_modifiers_batch = begin			# Freeze modifier calculation for performance

		while = {
			count = assimilation_counter

			random_owned_pop_group = {
				limit = {
					pop_group_size > 0
					NOT = {
						has_pop_group_flag = already_assimilated
					}
					is_robotic_species = yes
					has_citizenship_type = {
						type = citizenship_assimilation
						country = root.owner
					}
				}
				save_event_target_as = old_pop_group
				species = {
					save_event_target_as = old_species
				}
				# Synthetic/Machine Assimilation just turns you into the founder species
				root.owner_species = {
					save_event_target_as = target_species
					if = {
						limit = {
							root.owner = {
								has_modifier = synthetic_dawn
							}
							NOT = {
								has_trait = trait_robot_synthetic_dawn
							}
						}
						create_species = {
							is_mod = yes
							name = this
							plural = this
							class = this
							portrait = this
							traits = this
							can_be_modified = this
							homeworld = this
							namelist = this
							gender = this
							effect = {
								modify_species = {
									add_trait = trait_robot_synthetic_dawn
									effect = {
										save_event_target_as = target_species
									}
								}
							}
						}
					}
				}
				assimilate_pop_group_effect = yes
			}
		}

		finish_assimilation_effect = yes

		set_update_modifiers_batch = end			# Unfreeze modifier calculation for performance
		clear_assimilation_variables = yes
	}
}

# Hive Dissociation Assimilation
carrier_event = {
	id = assimilation.500
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_owned_species = {
			has_dissociation_assimilation_living_standard = {
				COUNTRY = root.owner
			}
		}
	}

	immediate = {
		inline_script = {
			script = assimilation/run
			TYPE = dissociation
			TARGET_SPECIES = create_assimilation_species
			TRAITS = "
				remove_trait = trait_self_modified
				remove_trait = trait_hive_mind
			"
			TRANSFER = assimilate_pop_group_effect
		}
	}
}

# Hive Mind Ego Assimilation
carrier_event = {
	id = assimilation.600
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_owned_species = {
			has_ego_assimilation_living_standard = {
				COUNTRY = root.owner
			}
		}
	}

	immediate = {
		inline_script = {
			script = assimilation/run
			TYPE = ego
			TARGET_SPECIES = create_assimilation_species
			TRAITS = "
				remove_trait = trait_self_modified
				inline_script = traits/remove_all_psionic_traits
				add_trait = trait_hive_mind
			"
			TRANSFER = assimilate_pop_group_effect
		}
	}
}

# Wilderness Ego Assimilation
carrier_event = {
	id = assimilation.610
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_owned_species = {
			has_ego_assimilation_living_standard = {
				COUNTRY = root.owner
			}
		}
	}

	immediate = {
		inline_script = {
			script = assimilation/run
			TYPE = ego
			TARGET_SPECIES = create_assimilation_species
			TRAITS = "
				remove_trait = trait_self_modified
				remove_trait = trait_incubator
				remove_trait = trait_nomadic
				remove_trait = trait_sedentary
				remove_trait = trait_vat_grown
				remove_trait = trait_nascent_stage
				remove_trait = trait_auto_mod_biological
				remove_trait = trait_auto_mod_overtuned
				remove_trait = trait_auto_mod_cyborg
				remove_trait = trait_adaptive_mutations
				remove_trait = trait_malleable_genes
				inline_script = traits/remove_all_psionic_traits
				add_trait = trait_hive_mind
				add_trait = trait_wilderness
			"
			TRANSFER = assimilate_wilderness_pop_group_effect
		}
	}
}

# Shroud Inoculation Assimilation - Mindwardens
carrier_event = {
	id = assimilation.700
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_owned_species = {
			has_anti_psionic_assimilation_living_standard = {
				COUNTRY = root.owner
			}
		}
	}

	immediate = {
		inline_script = {
			script = assimilation/run
			TYPE = anti_psionic
			TARGET_SPECIES = create_assimilation_species
			TRAITS = "
				remove_trait = trait_cybernetic
				remove_trait = trait_self_modified
				remove_trait = trait_latent_psionic
				remove_trait = trait_psionic
			"
			TRANSFER = assimilate_pop_group_effect
		}
	}
}
