namespace = action

# 64
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
				}
				count_species_pop = { count > 0 }
			}
		}
	}

	immediate = {
		set_update_modifiers_batch = begin
		every_playable_country = {
			limit = {
				num_unique_species > 1
				any_owned_species = {
					has_citizenship_type = {
						type = citizenship_assimilation
						country = prev
					}
					count_species_pop = { count > 0 }
				}
			}

			if = { # ascended synths
				limit = {
					OR = {
						NOT = { has_country_flag = synthetic_empire }
						# All Ascension Paths
						inline_script = {
							script = merger_of_rules/toggled_code
							code = "
								has_policy_flag = synth_assimilation_bio
								has_policy_flag = synth_assimilation_targeted
							"
							toggle = @aap_mod
						}
					}
				}
				every_owned_species = {
					limit = {
						has_citizenship_type = {
							type = citizenship_assimilation
							country = prev
						}
						count_species_pop = { count > 0 }
					}
					if = { # machines assimlating machines
						limit = {
							OR = {
								has_trait = trait_mechanical
								has_trait = trait_machine_unit
							}
							prev = {
								is_machine_empire = yes
								has_active_tradition = tr_synthetics_adopt_machine
							}
						}
						# Deliberately do nothing here, see assimilation_effect
						# All Ascension Paths
						inline_script = {
							script = merger_of_rules/toggled_code
							code = "
								set_timed_species_flag = {
									flag = assimilate_into_synths
									days = 30
								}
							"
							toggle = @aap_mod
						}
					}
					# All Ascension Paths
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							else_if = { # stop targetted bio ascension creating psionic robots
								limit = {
									OR = {
										has_trait = trait_mechanical
										has_trait = trait_machine_unit
									}
									prev = { has_country_flag = synthetic_empire }
								}
								set_timed_species_flag = {
									flag = assimilate_into_synths
									days = 30
								}
							}
							else_if = {
								limit = { ga_assimilation_event_suitable_species = yes }
								ga_tag_species = yes
							}
							else_if = {
								limit = {
									prev = { has_policy_flag = synth_assimilation_targeted }
									OR = {
										has_living_standard = {
											type = living_standard_tech_assimilation
											country = prev
										}
										has_living_standard = {
											type = living_standard_cyborg_assimilation
											country = prev
										}
										has_living_standard = {
											type = living_standard_cyborg_assimilation_psionic
											country = prev
										}
									}
								}
								set_timed_species_flag = {
									flag = assimilate_into_synths
									days = 30
								}
							}
							else_if = {
								limit = {
									prev = {
										OR = {
											AND = {
												is_hive_empire = no
												has_tradition = tr_cybernetics_transubstantiation_synthesis
												has_tradition = tr_psionics_psionic_assimilation
											}
											AND = {
												is_machine_empire = yes
												has_civic = civic_machine_assimilator
												has_tradition = tr_psionics_psionic_assimilation
											}
										}
									}
								}
								modify_species = {
									species = this
									add_trait = trait_cybernetic
									add_trait = trait_psionic
									add_traits_at_start_of_list = yes
									remove_trait = trait_hive_mind
									remove_trait = trait_self_modified
									remove_trait = trait_latent_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
										}
									}
								}
							}
						"
						toggle = @aap_mod
					}
					# Sartek TAP Merger - Ascension Path Fusion
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							else_if = { # cyborg and psionic
								limit = {
									prev = {
										OR = {
											AND = {
												is_hive_empire = no
												has_tradition = tr_cybernetics_transubstantiation_synthesis
												has_tradition = tr_psionics_psionic_assimilation
											}
											AND = {
												has_authority = auth_machine_intelligence
												has_civic = civic_machine_assimilator
												has_tradition = tr_psionics_psionic_assimilation
											}
										}
									}
								}
								modify_species = {
									species = this
									add_trait = trait_cybernetic
									add_trait = trait_psionic
									add_traits_at_start_of_list = yes
									remove_trait = trait_hive_mind
									remove_trait = trait_self_modified
									remove_trait = trait_latent_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
										}
									}
								}
							}
						"
						toggle = @sartek_afusion_mod_active
					}
					# Vanilla
					else_if = { # assimilator empire or cyborg empire
						limit = {
							prev = {
								OR = {
									AND = {
										is_hive_empire = no
										has_tradition = tr_cybernetics_transubstantiation_synthesis
									}
									AND = {
										is_machine_empire = yes
										has_civic = civic_machine_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
								}
							}
						}
					}
					# All Ascension Paths
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							else_if = {
								limit = {
									prev = {
										is_hive_empire = yes
										has_tradition = tr_genetics_creation
										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
									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
										}
									}
								}
							}
						"
						toggle = @aap_mod
					}
					# Ethics and Civics Bug Branch
					inline_script = {
						script = merger_of_rules/toggled_code
						code = "
							else_if = { # assimilating hive bug branch
								limit = {
									prev = {
										is_hive_empire = yes
										has_valid_civic = civic_bugged_hive_aggressive_proliferation
									}
								}
								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
										}
									}
								}
							}
						"
						toggle = @has_bug_branch
					}
					# Vanilla
					else_if = { # genetic hive mind
						limit = {
							prev = {
								is_hive_empire = yes
								OR = {
									has_tradition = tr_genetics_creation
									# Empires Expanded
									inline_script = {
										script = merger_of_rules/toggled_code
										code = "
											has_civic = civic_hive_assimilators
										"
										toggle = @has_emex_mod
									}
								}
								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 = {
								is_hive_empire = yes
								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
	}
}

# 81
# Planet is turned into tomb world by special bombing stance
planet_event = {
	id = action.81
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		OR = {
			has_orbital_bombardment_stance = armageddon
			has_orbital_bombardment_stance = pox
			# Ariphaos Patch
			has_planet_flag = ariphaos_patch_flag_tomb_bombardment
			# Expanded Traits, Civics, Pops, and More
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					has_orbital_bombardment_stance = death
				"
				toggle = @expops_compatch_var
			}
		}
		from = {
			OR = {
				is_default_or_fallen = yes
				# Ariphaos Patch
				is_country_type = vol
			}
		}
		habitable_planet = yes
	}

	immediate = {
		owner = {
			if = {
				limit = { is_at_war_with = from } # STU/PD
				add_static_war_exhaustion = {
					attacker = from
					location = root
					value_for_planet_destruction = 0.5
				}
			}
			if = {
				limit = {
					NAND = {
						has_origin = origin_progenitor_hive
						root = { is_capital = yes }
					}
				}
				country_event = { id = action.82 }
			}
			# Expanded Traits, Civics, Pops, and More
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					else_if = {
						limit = {
							NAND = {
								has_origin = origin_master_computer
								root = { is_capital = yes }
							}
						}
						country_event = { id = civicsexpanded.105 }
					}
				"
				toggle = @expops_compatch_var
			}
			# Vanilla
			else = {
				country_event = { id = origin.3265 }
			}
		}
		from = { country_event = { id = action.83 } }
		# Planetary Diversity
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				if = {
					limit = {
						pd_is_planet_class_cave = yes
					}
					change_pc = pc_pd_nuked_cave
				}
				if = {
					limit = {
						pd_is_planet_class_superhabitable = yes
					}
					change_pc = pc_pd_nuked_superhabitable
				}
				if = {
					limit = {
						pd_is_planet_class_tidally_locked = yes
					}
					change_pc = pc_pd_nuked_tidally_locked
				}
				if = {
					limit = {
						is_planet_class = pc_city
					}
					change_pc = pc_relic
					add_modifier = {
						modifier = pc_pd_nuked_arcology
						days = -1
					}
					set_planet_entity = {
						picture = pc_pd_nuked_relic
						entity = nuked_relic_world_01_entity
					}
				}
				if = {
					limit = {
						is_planet_class = pc_relic
					}
					add_modifier = {
						modifier = pc_pd_nuked_arcology
						days = -1
					}
					set_planet_entity = {
						picture = pc_pd_nuked_relic
						entity = nuked_relic_world_01_entity
					}
				}
				if = {
					limit = {
						NOR = {
							is_planet_class = pc_city
							is_planet_class = pc_relic
							pd_is_planet_class_cave = yes
							pd_is_planet_class_superhabitable = yes
							pd_is_planet_class_tidally_locked = yes
						}
					}
					change_pc = pc_nuked
				}
			"
			toggle = @has_planetary_diversity
		}
		# Vanilla
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				change_pc = pc_nuked
			"
			toggle = @[ 1 - has_planetary_diversity ]
		}
		# Vanilla
		set_planet_flag = nuked_planet_anomalies_disabled
		remove_all_buildings = yes
		remove_all_districts = yes # Ariphaos Patch
		reroll_deposits = yes
		tomb_world_modifier_cleanup = yes
		# GPM
		inline_script = {
			script = merger_of_rules/toggled_code
			code = "
				gpm_purge_all_planetary_modifiers = yes
				gpm_random_modifier_crisis_purge = yes
				gpm_remove_planetary_features = yes
				gpm_roll_planetary_features = yes
			"
			toggle = @has_guillis_planet_modifiers
		}
		# Vanilla
		set_planet_flag = armageddon_nuked
		# Expanded Traits, Civics, Pops, and More
		if = {
			limit = {
				merg_has_expanded_pops = yes
				has_modifier = holy_planet
				any_country = {
					has_ai_personality_behaviour = holy_planets # not awakened
					NOT = { is_at_war_with = from }
				}
			}
			random_country = {
				limit = {
					has_ai_personality_behaviour = holy_planets # not awakened
					NOT = { is_at_war_with = from }
				}
				save_event_target_as = outraged_FE
			}
			from = {
				save_event_target_as = FE_outrager
				country_event = { id = planet_destruction.610 days = 5 }
			}
		}
	}
}

# 90
event = {
	id = action.90
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		any_playable_country = {
			# Ethics and Civics Classic
			inline_script = {
				script = merger_of_rules/toggled_code
				code = "
					has_technology = tech_starbase_1
				"
				toggle = @has_ecc_cog
			}
			# Vanilla
			any_owned_planet = {
				is_controlled_by = prev
				solar_system = { NOT = { exists = starbase } }
			}
		}
	}
	immediate = {
		every_system = {
			limit = {
				NOT = { exists = starbase }
				any_system_colony = {
					exists = owner
					has_ground_combat = no
					owner = {
						OR = {
							merg_is_default_empire = yes
							is_fallen_empire = yes
						}
					}
					solar_system = {
						NOR = {
							any_fleet_in_system = {
								exists = owner
								owner = {
									NOT = { is_same_empire = prevprevprev.owner }
									OR = {
										is_hostile = prevprevprev.owner
										is_at_war_with = prevprevprev.owner
									}
								}
							}
							any_fleet_in_system = {
								exists = controller
								controller = {
									NOT = { is_same_empire = prevprevprev.owner }
									OR = {
										is_hostile = prevprevprev.owner
										is_at_war_with = prevprevprev.owner
									}
								}
							}
							any_system_colony = {
								exists = owner
								NOT = { is_planet = prevprev }
								owner = {
									NOT = { is_same_empire = prevprevprev.owner }
									OR = {
										merg_is_default_empire = yes
										is_fallen_empire = yes
									}
									OR = {
										is_hostile = prevprevprev.owner
										is_at_war_with = prevprevprev.owner
									}
								}
								NAND = {
									exists = controller
									controller = { is_same_empire = prevprevprev.owner }
								}
							}
						}
						OR = {
							is_capital_system = yes
							prev = {
								OR = {
									is_homeworld = yes
									has_modifier = instability_join_revolt
									owner = { has_origin = origin_demonic_incursion }
									owner = { has_country_flag = former_owner@prevprevprev }
								}
							}
							NOR = {
								prev = { is_occupied_flag = yes }
								any_system_colony = {
									exists = owner
									NOT = { is_planet = prevprev }
									OR = {
										merg_is_habitat = no
										prevprev = { merg_is_habitat = yes }
									}
									owner = {
										NOR = {
											is_same_empire = prevprevprev.owner
											has_country_flag = former_owner@prev
										}
										OR = {
											merg_is_default_empire = yes
											is_fallen_empire = yes
										}
									}
									NAND = {
										exists = controller
										controller = { is_same_empire = prevprevprev.owner }
									}
								}
							}
						}
					}
				}
			}
			random_system_colony = {
				limit = {
					exists = owner
					has_ground_combat = no
					owner = {
						OR = {
							merg_is_default_empire = yes
							is_fallen_empire = yes
						}
					}
					solar_system = {
						NOR = {
							any_fleet_in_system = {
								exists = owner
								owner = {
									NOT = { is_same_empire = prevprevprev.owner }
									OR = {
										is_hostile = prevprevprev.owner
										is_at_war_with = prevprevprev.owner
									}
								}
							}
							any_fleet_in_system = {
								exists = controller
								controller = {
									NOT = { is_same_empire = prevprevprev.owner }
									OR = {
										is_hostile = prevprevprev.owner
										is_at_war_with = prevprevprev.owner
									}
								}
							}
							any_system_colony = {
								exists = owner
								NOT = { is_planet = prevprev }
								owner = {
									NOT = { is_same_empire = prevprevprev.owner }
									OR = {
										merg_is_default_empire = yes
										is_fallen_empire = yes
									}
									OR = {
										is_hostile = prevprevprev.owner
										is_at_war_with = prevprevprev.owner
									}
								}
								NAND = {
									exists = controller
									controller = { is_same_empire = prevprevprev.owner }
								}
							}
						}
						OR = {
							is_capital_system = yes
							prev = {
								OR = {
									is_homeworld = yes
									has_modifier = instability_join_revolt
									owner = { has_origin = origin_demonic_incursion }
									owner = { has_country_flag = former_owner@prevprevprev }
								}
							}
							NOR = {
								prev = { is_occupied_flag = yes }
								any_system_colony = {
									exists = owner
									NOT = { is_planet = prevprev }
									OR = {
										merg_is_habitat = no
										prevprev = { merg_is_habitat = yes }
									}
									owner = {
										NOR = {
											is_same_empire = prevprevprev.owner
											has_country_flag = former_owner@prev
										}
										OR = {
											merg_is_default_empire = yes
											is_fallen_empire = yes
										}
									}
									NAND = {
										exists = controller
										controller = { is_same_empire = prevprevprev.owner }
									}
								}
							}
						}
					}
				}
				# Fruitful Origin - the outpost is being created for the first time
				if = {
					limit = {
						has_planet_flag = seed_pods_colonization
					}
					planet_event = { id = origin.7400 }
				}
				# else, fire the normal "Outpost Rebuilt!" event
				else = {
					planet_event = { id = action.91 }
				}
			}
		}
	}
}
