#######################
# Events - BioGenesis #
#######################

namespace = bio

##################################################################
# Evolutionary Predators Events - Namespace: bio.1000 - bio.1999 #
##################################################################

country_event = {
	id = bio.1000

	is_triggered_only = yes
	hide_window = yes

	immediate = {
		set_variable = {
			which = num_evopred_situations_ran
			value = 1
		}
		set_variable = {
			which = smorgasblorg_phenotypes
			value = 0
		}
		start_situation = {
			type = evolutionary_predators_situation
			target = root
		}
		capital_scope = {
			add_modifier = { modifier = genetic_soup }
		}
	}
}

situation_event = {
	id = bio.1001

	is_triggered_only = yes
	hide_window = yes

	immediate = {
		owner = { set_evopred_flags = yes }
	}
}


situation_event = {
	id = bio.1005
	title = bio.1005.name
	desc = bio.1005.desc
	picture = GFX_evt_evolutionary_predators
	show_sound = event_dangerous_animals
	situation = this

	is_triggered_only = yes

	immediate = {
		owner = {
			set_evopred_flags = yes
			while = {
				count = 3
				generate_evolutionary_predator_choices = yes
			}
		}
	}

	option = {
		trigger = { owner = { has_country_flag = pop_growth_evopred } }
		name = bio.1005.pop_growth
		custom_tooltip = bio.1005.tt

		owner = {
			if = {
				limit = {
					has_active_tradition = tr_mutation_adopt_evo_pred
				}
				add_monthly_resource_mult = {
					resource = society_research
					value = @tier3researchreward
					min = @tier3researchmin
					max = @tier3researchmax
				}
			}
		}

		hidden_effect = {
			owner = {
				add_random_trait_evopred = { TAG = pop_growth }
				country_event = {
					id = timeline.90
				}
			}
		}
	}

	option = {
		trigger = { owner = { has_country_flag = pop_output_evopred } }
		name = bio.1005.pop_output
		custom_tooltip = bio.1005.tt

		owner = {
			if = {
				limit = {
					has_active_tradition = tr_mutation_adopt_evo_pred
				}
				add_monthly_resource_mult = {
					resource = society_research
					value = @tier3researchreward
					min = @tier3researchmin
					max = @tier3researchmax
				}
			}
		}

		hidden_effect = {
			owner = {
				add_random_trait_evopred = { TAG = pop_output }
				country_event = {
					id = timeline.90
				}
			}
		}
	}

	option = {
		trigger = { owner = { has_country_flag = leader_evopred } }
		name = bio.1005.leader
		custom_tooltip = bio.1005.tt

		owner = {
			if = {
				limit = {
					has_active_tradition = tr_mutation_adopt_evo_pred
				}
				add_monthly_resource_mult = {
					resource = society_research
					value = @tier3researchreward
					min = @tier3researchmin
					max = @tier3researchmax
				}
			}
		}

		hidden_effect = {
			owner = {
				add_random_trait_evopred = { TAG = leader }
				country_event = {
					id = timeline.90
				}
			}
		}
	}

	option = {
		trigger = { owner = { has_country_flag = habitability_evopred } }
		name = bio.1005.habitability
		custom_tooltip = bio.1005.tt

		owner = {
			if = {
				limit = {
					has_active_tradition = tr_mutation_adopt_evo_pred
				}
				add_monthly_resource_mult = {
					resource = society_research
					value = @tier3researchreward
					min = @tier3researchmin
					max = @tier3researchmax
				}
			}
		}

		hidden_effect = {
			owner = {
				add_random_trait_evopred = { TAG = habitability }
				country_event = {
					id = timeline.90
				}
			}
		}
	}

	option = {
		trigger = { owner = { has_country_flag = upkeep_evopred } }
		name = bio.1005.upkeep
		custom_tooltip = bio.1005.tt

		owner = {
			if = {
				limit = {
					has_active_tradition = tr_mutation_adopt_evo_pred
				}
				add_monthly_resource_mult = {
					resource = society_research
					value = @tier3researchreward
					min = @tier3researchmin
					max = @tier3researchmax
				}
			}
		}

		hidden_effect = {
			owner = {
				add_random_trait_evopred = { TAG = upkeep }
				country_event = {
					id = timeline.90
				}
			}
		}
	}

	option = {
		trigger = { owner = { has_country_flag = humanoid_evopred } }
		name = bio.1005.humanoid
		custom_tooltip = bio.1005.tt

		owner = {
			if = {
				limit = {
					has_active_tradition = tr_mutation_adopt_evo_pred
				}
				add_monthly_resource_mult = {
					resource = society_research
					value = @tier3researchreward
					min = @tier3researchmin
					max = @tier3researchmax
				}
			}
		}

		hidden_effect = {
			owner = {
				add_random_trait_evopred = { TAG = humanoid SPECIES = yes }
				country_event = {
					id = timeline.90
				}
			}
		}
	}

	option = {
		trigger = { owner = { has_country_flag = mammalian_evopred } }
		name = bio.1005.mammalian
		custom_tooltip = bio.1005.tt

		owner = {
			if = {
				limit = {
					has_active_tradition = tr_mutation_adopt_evo_pred
				}
				add_monthly_resource_mult = {
					resource = society_research
					value = @tier3researchreward
					min = @tier3researchmin
					max = @tier3researchmax
				}
			}
		}

		hidden_effect = {
			owner = {
				add_random_trait_evopred = { TAG = mammalian SPECIES = yes }
				country_event = {
					id = timeline.90
				}
			}
		}
	}

	option = {
		trigger = { owner = { has_country_flag = reptilian_evopred } }
		name = bio.1005.reptilian
		custom_tooltip = bio.1005.tt

		owner = {
			if = {
				limit = {
					has_active_tradition = tr_mutation_adopt_evo_pred
				}
				add_monthly_resource_mult = {
					resource = society_research
					value = @tier3researchreward
					min = @tier3researchmin
					max = @tier3researchmax
				}
			}
		}

		hidden_effect = {
			owner = {
				add_random_trait_evopred = { TAG = reptilian SPECIES = yes }
				country_event = {
					id = timeline.90
				}
			}
		}
	}

	option = {
		trigger = { owner = { has_country_flag = avian_evopred } }
		name = bio.1005.avian
		custom_tooltip = bio.1005.tt

		owner = {
			if = {
				limit = {
					has_active_tradition = tr_mutation_adopt_evo_pred
				}
				add_monthly_resource_mult = {
					resource = society_research
					value = @tier3researchreward
					min = @tier3researchmin
					max = @tier3researchmax
				}
			}
		}

		hidden_effect = {
			owner = {
				add_random_trait_evopred = { TAG = avian SPECIES = yes }
				country_event = {
					id = timeline.90
				}
			}
		}
	}

	option = {
		trigger = { owner = { has_country_flag = arthropoid_evopred } }
		name = bio.1005.arthropoid
		custom_tooltip = bio.1005.tt

		owner = {
			if = {
				limit = {
					has_active_tradition = tr_mutation_adopt_evo_pred
				}
				add_monthly_resource_mult = {
					resource = society_research
					value = @tier3researchreward
					min = @tier3researchmin
					max = @tier3researchmax
				}
			}
		}

		hidden_effect = {
			owner = {
				add_random_trait_evopred = { TAG = arthropoid SPECIES = yes }
				country_event = {
					id = timeline.90
				}
			}
		}
	}

	option = {
		trigger = { owner = { has_country_flag = molluscoid_evopred } }
		name = bio.1005.molluscoid
		custom_tooltip = bio.1005.tt

		owner = {
			if = {
				limit = {
					has_active_tradition = tr_mutation_adopt_evo_pred
				}
				add_monthly_resource_mult = {
					resource = society_research
					value = @tier3researchreward
					min = @tier3researchmin
					max = @tier3researchmax
				}
			}
		}

		hidden_effect = {
			owner = {
				add_random_trait_evopred = { TAG = molluscoid SPECIES = yes }
				country_event = {
					id = timeline.90
				}
			}
		}
	}

	option = {
		trigger = { owner = { has_country_flag = fungoid_evopred } }
		name = bio.1005.fungoid
		custom_tooltip = bio.1005.tt

		owner = {
			if = {
				limit = {
					has_active_tradition = tr_mutation_adopt_evo_pred
				}
				add_monthly_resource_mult = {
					resource = society_research
					value = @tier3researchreward
					min = @tier3researchmin
					max = @tier3researchmax
				}
			}
		}

		hidden_effect = {
			owner = {
				add_random_trait_evopred = { TAG = fungoid SPECIES = yes }
				country_event = {
					id = timeline.90
				}
			}
		}
	}

	option = {
		trigger = { owner = { has_country_flag = plantoid_evopred } }
		name = bio.1005.plantoid
		custom_tooltip = bio.1005.tt

		owner = {
			if = {
				limit = {
					has_active_tradition = tr_mutation_adopt_evo_pred
				}
				add_monthly_resource_mult = {
					resource = society_research
					value = @tier3researchreward
					min = @tier3researchmin
					max = @tier3researchmax
				}
			}
		}

		hidden_effect = {
			owner = {
				add_random_trait_evopred = { TAG = plantoid SPECIES = yes }
				country_event = {
					id = timeline.90
				}
			}
		}
	}

	option = {
		trigger = { owner = { has_country_flag = lithoid_evopred } }
		name = bio.1005.lithoid
		custom_tooltip = bio.1005.tt

		owner = {
			if = {
				limit = {
					has_active_tradition = tr_mutation_adopt_evo_pred
				}
				add_monthly_resource_mult = {
					resource = society_research
					value = @tier3researchreward
					min = @tier3researchmin
					max = @tier3researchmax
				}
			}
		}

		hidden_effect = {
			owner = {
				add_random_trait_evopred = { TAG = lithoid SPECIES = yes }
				country_event = {
					id = timeline.90
				}
			}
		}
	}

	option = {
		trigger = { owner = { has_country_flag = necroid_evopred } }
		name = bio.1005.necroid
		custom_tooltip = bio.1005.tt

		owner = {
			if = {
				limit = {
					has_active_tradition = tr_mutation_adopt_evo_pred
				}
				add_monthly_resource_mult = {
					resource = society_research
					value = @tier3researchreward
					min = @tier3researchmin
					max = @tier3researchmax
				}
			}
		}

		hidden_effect = {
			owner = {
				add_random_trait_evopred = { TAG = necroid SPECIES = yes }
				country_event = {
					id = timeline.90
				}
			}
		}
	}

	option = {
		trigger = { owner = { has_country_flag = aquatic_evopred } }
		name = bio.1005.aquatic
		custom_tooltip = bio.1005.tt

		owner = {
			if = {
				limit = {
					has_active_tradition = tr_mutation_adopt_evo_pred
				}
				add_monthly_resource_mult = {
					resource = society_research
					value = @tier3researchreward
					min = @tier3researchmin
					max = @tier3researchmax
				}
			}
		}

		hidden_effect = {
			owner = {
				add_random_trait_evopred = { TAG = aquatic SPECIES = yes }
				country_event = {
					id = timeline.90
				}
			}
		}
	}

	option = {
		trigger = { owner = { has_country_flag = toxoid_evopred } }
		name = bio.1005.toxoid
		custom_tooltip = bio.1005.tt

		owner = {
			if = {
				limit = {
					has_active_tradition = tr_mutation_adopt_evo_pred
				}
				add_monthly_resource_mult = {
					resource = society_research
					value = @tier3researchreward
					min = @tier3researchmin
					max = @tier3researchmax
				}
			}
		}

		hidden_effect = {
			owner = {
				add_random_trait_evopred = { TAG = toxoid SPECIES = yes }
				country_event = {
					id = timeline.90
				}
			}
		}
	}

	option = {
		trigger = { owner = { has_country_flag = infernal_evopred } }
		name = bio.1005.infernal
		custom_tooltip = bio.1005.tt

		owner = {
			if = {
				limit = {
					has_active_tradition = tr_mutation_adopt_evo_pred
				}
				add_monthly_resource_mult = {
					resource = society_research
					value = @tier3researchreward
					min = @tier3researchmin
					max = @tier3researchmax
				}
			}
		}

		hidden_effect = {
			owner = {
				add_random_trait_evopred = { TAG = infernal SPECIES = yes }
				country_event = {
					id = timeline.90
				}
			}
		}
	}

	option = {
		trigger = { owner = { has_country_flag = plasmic_evopred } }
		name = bio.1005.plasmic
		custom_tooltip = bio.1005.tt

		owner = {
			if = {
				limit = {
					has_active_tradition = tr_mutation_adopt_evo_pred
				}
				add_monthly_resource_mult = {
					resource = society_research
					value = @tier3researchreward
					min = @tier3researchmin
					max = @tier3researchmax
				}
			}
		}

		hidden_effect = {
			owner = {
				modify_species = {
					species = species
					add_trait = trait_plasmic
					effect = { save_event_target_as = target_species }
				}
				set_country_flag = gained_plasmic_trait
				create_message = {
					type = PLASMIC_TRAIT_ADDED
					localization = MESSAGE_PLASMIC_TRAIT_ADDED
					days = 30
					variable = {
						type = name
						localization = SPECIES
						scope = species
					}
				}
			}
		}
	}

	option = {
		trigger = { owner = { has_country_flag = psionic_evopred } }
		name = bio.1005.psionic
		custom_tooltip = bio.1005.tt

		owner = {
			if = {
				limit = {
					has_active_tradition = tr_mutation_adopt_evo_pred
				}
				add_monthly_resource_mult = {
					resource = society_research
					value = @tier3researchreward
					min = @tier3researchmin
					max = @tier3researchmax
				}
			}
		}

		hidden_effect = {
			owner = {
				add_random_trait_evopred = { TAG = psionic SPECIES = yes }
			}
		}
	}

	option = {
		trigger = { owner = { has_country_flag = leviathan_evo_pred } }
		name = bio.1005.leviathan
		custom_tooltip = bio.1005.tt

		icon = {
			icon = GFX_tradition_category_icon_tradition_mutation
			text = has_tr_mutation_nucleotide_isolation_evo_pred_tt
		}

		owner = {
			if = {
				limit = {
					has_active_tradition = tr_mutation_adopt_evo_pred
				}
				add_monthly_resource_mult = {
					resource = society_research
					value = @tier3researchreward
					min = @tier3researchmin
					max = @tier3researchmax
				}
			}
		}

		hidden_effect = {
			owner = {
				random_list = {
					0 = {
						modifier = {
							add = 10
							has_country_flag = harvested_dragon_dna
							NOT = { has_country_flag = gained_dragon_trait }
						}
						modify_species = {
							species = species
							add_trait = trait_drake_scaled
							effect = { save_event_target_as = target_species }
						}
						set_country_flag = gained_dragon_trait
						create_message = {
							type = LEVIATHAN_TRAIT_ADDED
							localization = MESSAGE_DRAGON_TRAIT_ADDED
							days = 30
							variable = {
								type = name
								localization = SPECIES
								scope = species
							}
						}
					}
					0 = {
						modifier = {
							add = 10
							has_country_flag = harvested_voidspawn_dna
							NOT = { has_country_flag = gained_voidspawn_trait }
						}
						modify_species = {
							species = species
							add_trait = trait_voidling
							effect = { save_event_target_as = target_species }
						}
						set_country_flag = gained_voidspawn_trait
						create_message = {
							type = LEVIATHAN_TRAIT_ADDED
							localization = MESSAGE_VOIDSPAWN_TRAIT_ADDED
							days = 30
							variable = {
								type = name
								localization = SPECIES
								scope = species
							}
						}
					}
					0 = {
						modifier = {
							add = 10
							has_country_flag = harvested_tiyanki_dna
							NOT = { has_country_flag = gained_tiyanki_trait }
						}
						modify_species = {
							species = species
							add_trait = trait_tiyanki
							effect = { save_event_target_as = target_species }
						}
						set_country_flag = gained_tiyanki_trait
						create_message = {
							type = LEVIATHAN_TRAIT_ADDED
							localization = MESSAGE_TIYANKI_TRAIT_ADDED
							days = 30
							variable = {
								type = name
								localization = SPECIES
								scope = species
							}
						}
					}
				}
			}
		}
	}

	option = {
		trigger = {
			owner = {
				species = {
					any_trait_available_for_species = {
						trait_has_all_tags = { organic positive }
						NOT = { trait_has_any_tag = { special species cybernetic presapient drawbacks } }
						OR = {
							NOT = { trait_has_any_tag = { genetic_ascension } }
							root.owner = { has_tradition = tr_genetics_resequencing }
						}
						OR = {
							NOT = { trait_has_any_tag = { individual } }
							root.owner = { is_gestalt = no }
						}
					}
				}
			}
		}
		name = bio.1005.random
		custom_tooltip = bio.1005.tt

		owner = {
			if = {
				limit = {
					has_active_tradition = tr_mutation_adopt_evo_pred
				}
				add_monthly_resource_mult = {
					resource = society_research
					value = @tier3researchreward
					min = @tier3researchmin
					max = @tier3researchmax
				}
			}
		}

		hidden_effect = {
			owner = {
				modify_species = {
					species = species
					add_random_species_trait = {
						trait_has_all_tags = { organic positive }
						NOT = { trait_has_any_tag = { special species cybernetic presapient drawbacks } }
						OR = {
							NOT = { trait_has_any_tag = { genetic_ascension } }
							root = {
								owner_can_do_advanced_gene_modding = yes
							}
						}
						OR = {
							NOT = { trait_has_any_tag = { individual } }
							root.owner = { is_gestalt = no }
						}
					}
					effect = { save_event_target_as = target_species }
					on_random_trait_added = { # Trait is the current scope, species is From, prev is owner
						prev = {
							create_message = {
								type = RANDOM_TRAIT_ADDED
								localization = MESSAGE_RANDOM_TRAIT_ADDED
								days = 30
								variable = {
									type = name
									localization = TRAIT
									scope = prev
								}
								variable = {
									type = name
									localization = SPECIES
									scope = species
								}
							}
						}
					}
				}
			}
		}
	}

	option = {
		trigger = {
			owner = {
				has_active_tradition = tr_mutation_unnatural_selection_evo_pred
				species = {
					any_trait_available_for_species = {
						trait_has_all_tags = { organic positive species }
						NOT = { trait_has_any_tag = { special cybernetic presapient drawbacks } }
						OR = {
							NOT = { trait_has_any_tag = { genetic_ascension } }
							root.owner = { has_tradition = tr_genetics_resequencing }
						}
						OR = {
							NOT = { trait_has_any_tag = { individual } }
							root.owner = { is_gestalt = no }
						}
					}
				}
			}
		}
		name = bio.1005.unnatural_selection
		custom_tooltip = bio.1005.tt
		icon = {
			icon = GFX_tradition_category_icon_tradition_mutation
			text = has_tr_mutation_unnatural_selection_evo_pred_tt
		}

		owner = {
			if = {
				limit = {
					has_active_tradition = tr_mutation_adopt_evo_pred
				}
				add_monthly_resource_mult = {
					resource = society_research
					value = @tier3researchreward
					min = @tier3researchmin
					max = @tier3researchmax
				}
			}
		}

		hidden_effect = {
			owner = {
				modify_species = {
					species = species
					add_random_species_trait = {
						trait_has_all_tags = { organic positive species }
						NOT = { trait_has_any_tag = { special cybernetic presapient drawbacks } }
						OR = {
							NOT = { trait_has_any_tag = { genetic_ascension } }
							root = {
								owner_can_do_advanced_gene_modding = yes
							}
						}
						OR = {
							NOT = { trait_has_any_tag = { individual } }
							root.owner = { is_gestalt = no }
						}
					}
					effect = { save_event_target_as = target_species }
					on_random_trait_added = { # Trait is the current scope, species is From, prev is owner
						prev = {
							create_message = {
								type = RANDOM_TRAIT_ADDED
								localization = MESSAGE_RANDOM_TRAIT_ADDED
								days = 30
								variable = {
									type = name
									localization = TRAIT
									scope = prev
								}
								variable = {
									type = name
									localization = SPECIES
									scope = species
								}
							}
						}
					}
				}
			}
		}
	}

	option = {
		name = bio.1005.no_reward
		hidden_effect = {
			set_situation_flag = no_choice_evopred
		}
		owner = {
			add_monthly_resource_mult = {
				resource = society_research
				value = @tier4researchreward
				min = @tier4researchmin
				max = @tier4researchmax
			}
		}
	}

	after = {
		hidden_effect = {
			if = {
				limit = {
					NOT = { has_situation_flag = no_choice_evopred }
				}
				owner = {
					change_dominant_species = { species = event_target:target_species change_all = yes }
					unify_evopred_species = yes
					every_controlled_ship = {
						limit = {
							is_ship_class = shipclass_colonizer
							species = {
								is_same_species = prev.species
							}
						}
						change_species = event_target:target_species
					}
					every_pool_leader = {
						limit = {
							is_same_species = prev.species
						}
						change_species = event_target:target_species
					}
					update_node_portraits_if_gestalt_effect = yes
				}
			}
			owner = {
				remove_country_flag = toxoid_evopred
				remove_country_flag = infernal_evopred
				remove_country_flag = aquatic_evopred
				remove_country_flag = necroid_evopred
				remove_country_flag = lithoid_evopred
				remove_country_flag = plantoid_evopred
				remove_country_flag = fungoid_evopred
				remove_country_flag = molluscoid_evopred
				remove_country_flag = arthropoid_evopred
				remove_country_flag = avian_evopred
				remove_country_flag = reptilian_evopred
				remove_country_flag = mammalian_evopred
				remove_country_flag = humanoid_evopred
				remove_country_flag = upkeep_evopred
				remove_country_flag = habitability_evopred
				remove_country_flag = leader_evopred
				remove_country_flag = pop_output_evopred
				remove_country_flag = pop_growth_evopred
				remove_country_flag = plasmic_evopred
				remove_country_flag = psionic_evopred
				remove_country_flag = leviathan_evo_pred
				change_variable = {
					which = num_evopred_situations_ran
					value = 1
				}
				start_situation = {
					type = evolutionary_predators_situation
					target = this
				}
				if = {
					limit = {
						is_mutation_authority = yes
					}
					species = { set_species_traits_evopred_count_variable = yes }
				}
				every_owned_leader = {
					limit = {
						is_exact_same_species = owner.species
					}
					update_leader_after_modification = yes
				}
				every_pool_leader = {
					limit = {
						is_exact_same_species = owner.species
					}
					update_leader_after_modification = yes
				}
			}
			destroy_situation = this
		}
	}
}

# Newly colonized planets gains modifier
carrier_event = {
	id = bio.1010
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		has_global_flag = game_started
		is_colony = yes
		owner = {
			OR = {
				has_origin = origin_evolutionary_predators
				AND = {
					has_origin = origin_nomadic_purger
					overlord? = { has_origin = origin_evolutionary_predators }
				}
			}
		}
		habitable_planet_not_urban = yes
		NOT = { has_modifier = genetic_soup }
	}

	immediate = {
		# A nomad purge vassal harvests for its overlord, so the harvest is tracked against the overlord instead.
		if = {
			limit = {
				owner = { has_origin = origin_nomadic_purger }
			}
			evopred_biosphere_harvest_effect = { HARVESTER = owner.overlord SCHEDULE_DOWNGRADE = yes }
		}
		else = {
			evopred_biosphere_harvest_effect = { HARVESTER = owner SCHEDULE_DOWNGRADE = yes }
		}
	}
}

carrier_event = {
	id = bio.1015 #removes the modifier if you lose the planet
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_colony = yes
		OR = {
			has_modifier = fresh_biosphere
			has_modifier = exhausted_biosphere
			has_modifier = genetic_soup
		}
	}

	immediate = {
		remove_modifier = fresh_biosphere
		remove_modifier = exhausted_biosphere
		remove_modifier = genetic_soup
		if = {
			limit = {
				from = { has_origin = origin_evolutionary_predators }
			}
			evopred_biosphere_harvest_effect = { HARVESTER = from }
		}
		# A nomad purge vassal harvests for its overlord, so the harvest is tracked against the overlord instead.
		else_if = {
			limit = {
				from = {
					has_origin = origin_nomadic_purger
					overlord? = { has_origin = origin_evolutionary_predators }
				}
			}
			evopred_biosphere_harvest_effect = { HARVESTER = from.overlord }
		}
	}
}

#downgrades the modifier after years equal to the planet size
carrier_event = {
	id = bio.1020
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		exists = owner
		owner = { is_same_value = from }
	}

	immediate = {
		remove_modifier = fresh_biosphere
		add_modifier = { modifier = exhausted_biosphere }
	}
}

#removes Biosphere Modifiers when abandoning colonies
carrier_event = {
	id = bio.1025
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		OR = {
			has_modifier = fresh_biosphere
			has_modifier = exhausted_biosphere
			has_modifier = genetic_soup
		}
	}
	immediate = {
		remove_modifier = fresh_biosphere
		remove_modifier = exhausted_biosphere
		remove_modifier = genetic_soup
	}
}

# Procuce DNA Operation

espionage_operation_event = {
	id = bio.1050
	title = bio.1050.name
	espionage_operation = yes
	desc = bio.1050.desc
	picture = GFX_evt_spy_network
	show_sound = event_default
	is_triggered_only = yes

	option = { name = ACKNOWLEDGED }
}

# Target identified
espionage_operation_event = {
	id = bio.1055
	title = bio.1055.name
	desc = bio.1055.desc
	espionage_operation = yes
	picture = GFX_evt_analyzing_artifacts
	show_sound = event_default
	is_triggered_only = yes

	option = { name = GOOD }
}
# Result
espionage_operation_event = {
	id = bio.1060
	title =bio.1060.name
	desc = bio.1060.desc
	espionage_operation = yes
	picture = GFX_evt_analyzing_artifacts
	show_sound = event_default
	is_triggered_only = yes

	option = {
		name = LAUNCH_OPERATION
		hidden_effect = {
			owner = {
				country_event = { id = bio.1065 days = 1 }
			}
		}
	}

	after = {
		set_espionage_operation_progress_locked = no
	}
}
# Finalé | from = operation
country_event = {
	id = bio.1065
	title = bio.1065.name
	desc = bio.1065.desc
	picture = GFX_evt_genetic_modification
	location = from.target.capital_scope
	show_sound = event_nem_asset_acquired_positive
	is_triggered_only = yes

	option = {
		name = EXCELLENT
		from.target.species = {
			switch = {
				trigger = is_species_class
				HUM = {
					root = { give_evolutionary_predator_situation_progress_and_dna = { AMOUNT = small TYPE = humanoid } }
				}
				MAM = {
					root = { give_evolutionary_predator_situation_progress_and_dna = { AMOUNT = small TYPE = mammalian } }
				}
				REP = {
					root = { give_evolutionary_predator_situation_progress_and_dna = { AMOUNT = small TYPE = reptilian } }
				}
				AVI = {
					root = { give_evolutionary_predator_situation_progress_and_dna = { AMOUNT = small TYPE = avian } }
				}
				ART = {
					root = { give_evolutionary_predator_situation_progress_and_dna = { AMOUNT = small TYPE = arthropoid } }
				}
				MOL = {
					root = { give_evolutionary_predator_situation_progress_and_dna = { AMOUNT = small TYPE = molluscoid } }
				}
				FUN = {
					root = { give_evolutionary_predator_situation_progress_and_dna = { AMOUNT = small TYPE = fungoid } }
				}
				PLANT = {
					root = { give_evolutionary_predator_situation_progress_and_dna = { AMOUNT = small TYPE = plantoid } }
				}
				LITHOID = {
					root = { give_evolutionary_predator_situation_progress_and_dna = { AMOUNT = small TYPE = lithoid } }
				}
				NECROID = {
					root = { give_evolutionary_predator_situation_progress_and_dna = { AMOUNT = small TYPE = necroid } }
				}
				AQUATIC = {
					root = { give_evolutionary_predator_situation_progress_and_dna = { AMOUNT = small TYPE = aquatic } }
				}
				TOX = {
					root = { give_evolutionary_predator_situation_progress_and_dna = { AMOUNT = small TYPE = toxoid } }
				}
				MINDWARDEN = {
					root = { give_evolutionary_predator_situation_progress_and_dna = { AMOUNT = small TYPE = reptilian } }
				}
				INF = {
					root = { give_evolutionary_predator_situation_progress_and_dna = { AMOUNT = small TYPE = infernal } }
				}
			}
		}
	}

	after = {
		hidden_effect = {
			owner = {
				if = {
					limit = {
						NOT = { has_country_flag = operation_complete_procure_DNA }
					}
					change_variable = {
						which = tinker_tailor_soldier_blorg_achievement_count
						value = 1
					}
					set_country_flag = operation_complete_procure_DNA
				}
			}
		}
		destroy_espionage_operation = from
	}
}

# A pop has finished purging
# This = Planet scope
# From = Country
# FromFrom = Pop
carrier_event = {
	id = bio.1070
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		from = {
			OR = {
				has_origin = origin_evolutionary_predators
				AND = {
					has_origin = origin_nomadic_purger
					overlord? = { has_origin = origin_evolutionary_predators }
				}
			}
		}
		fromfrom = {
			OR = {
				is_archetype = BIOLOGICAL
				is_archetype = LITHOID
				is_archetype = PRESAPIENT
			}
		}
	}

	immediate = {
		if = {
			limit = {
				from = {
					has_origin = origin_nomadic_purger
				}
			}
			fromfrom = {
				switch = {
					trigger = is_species_class
					HUM = {
						from.overlord = { set_evopred_flag = { TYPE = humanoid } }
					}
					MAM = {
						from.overlord = { set_evopred_flag = { TYPE = mammalian } }
					}
					REP = {
						from.overlord = { set_evopred_flag = { TYPE = reptilian } }
					}
					AVI = {
						from.overlord = { set_evopred_flag = { TYPE = avian } }
					}
					ART = {
						from.overlord = { set_evopred_flag = { TYPE = arthropoid } }
					}
					MOL = {
						from.overlord = { set_evopred_flag = { TYPE = molluscoid } }
					}
					FUN = {
						from.overlord = { set_evopred_flag = { TYPE = fungoid } }
					}
					PLANT = {
						from.overlord = { set_evopred_flag = { TYPE = plantoid } }
					}
					LITHOID = {
						from.overlord = { set_evopred_flag = { TYPE = lithoid } }
					}
					NECROID = {
						from.overlord = { set_evopred_flag = { TYPE = necroid } }
					}
					AQUATIC = {
						from.overlord = { set_evopred_flag = { TYPE = aquatic } }
					}
					TOX = {
						from.overlord = { set_evopred_flag = { TYPE = toxoid } }
					}
					INF = {
						from.overlord = { set_evopred_flag = { TYPE = infernal } }
					}
					PRE_MAM = {
						from.overlord = { set_evopred_flag = { TYPE = mammalian } }
					}
					PRE_REP = {
						from.overlord = { set_evopred_flag = { TYPE = reptilian } }
					}
					PRE_AVI = {
						from.overlord = { set_evopred_flag = { TYPE = avian } }
					}
					PRE_ART = {
						from.overlord = { set_evopred_flag = { TYPE = arthropoid } }
					}
					PRE_MOL = {
						from.overlord = { set_evopred_flag = { TYPE = molluscoid } }
					}
					PRE_FUN = {
						from.overlord = { set_evopred_flag = { TYPE = fungoid } }
					}
					PRE_PLANT = {
						from.overlord = { set_evopred_flag = { TYPE = plantoid } }
					}
					PRE_LITHOID = {
						from.overlord = { set_evopred_flag = { TYPE = lithoid } }
					}
					PRE_AQUATIC = {
						from.overlord = { set_evopred_flag = { TYPE = aquatic } }
					}
					PRE_TOX = {
						from.overlord = { set_evopred_flag = { TYPE = toxoid } }
					}
					PSIONIC = {
						from.overlord = { set_evopred_flag = { TYPE = psionic } }
					}
					PRE_INF = {
						from.overlord = { set_evopred_flag = { TYPE = infernal } }
					}
				}
			}
		}
		else = {
			fromfrom = {
				switch = {
					trigger = is_species_class
					HUM = {
						from = { set_evopred_flag = { TYPE = humanoid } }
					}
					MAM = {
						from = { set_evopred_flag = { TYPE = mammalian } }
					}
					REP = {
						from = { set_evopred_flag = { TYPE = reptilian } }
					}
					AVI = {
						from = { set_evopred_flag = { TYPE = avian } }
					}
					ART = {
						from = { set_evopred_flag = { TYPE = arthropoid } }
					}
					MOL = {
						from = { set_evopred_flag = { TYPE = molluscoid } }
					}
					FUN = {
						from = { set_evopred_flag = { TYPE = fungoid } }
					}
					PLANT = {
						from = { set_evopred_flag = { TYPE = plantoid } }
					}
					LITHOID = {
						from = { set_evopred_flag = { TYPE = lithoid } }
					}
					NECROID = {
						from = { set_evopred_flag = { TYPE = necroid } }
					}
					AQUATIC = {
						from = { set_evopred_flag = { TYPE = aquatic } }
					}
					TOX = {
						from = { set_evopred_flag = { TYPE = toxoid } }
					}
					INF = {
						from = { set_evopred_flag = { TYPE = infernal } }
					}
					PRE_MAM = {
						from = { set_evopred_flag = { TYPE = mammalian } }
					}
					PRE_REP = {
						from = { set_evopred_flag = { TYPE = reptilian } }
					}
					PRE_AVI = {
						from = { set_evopred_flag = { TYPE = avian } }
					}
					PRE_ART = {
						from = { set_evopred_flag = { TYPE = arthropoid } }
					}
					PRE_MOL = {
						from = { set_evopred_flag = { TYPE = molluscoid } }
					}
					PRE_FUN = {
						from = { set_evopred_flag = { TYPE = fungoid } }
					}
					PRE_PLANT = {
						from = { set_evopred_flag = { TYPE = plantoid } }
					}
					PRE_LITHOID = {
						from = { set_evopred_flag = { TYPE = lithoid } }
					}
					PRE_AQUATIC = {
						from = { set_evopred_flag = { TYPE = aquatic } }
					}
					PRE_TOX = {
						from = { set_evopred_flag = { TYPE = toxoid } }
					}
					PSIONIC = {
						from = { set_evopred_flag = { TYPE = psionic } }
					}
					PRE_INF = {
						from = { set_evopred_flag = { TYPE = infernal } }
					}
				}
			}
		}
	}
}

country_event = {
	id = bio.1075
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_origin = origin_evolutionary_predators
		is_democratic_authority = yes
		is_mutation_authority = yes
	}

	immediate = {
		remove_country_flag = imperialist_leading
		remove_country_flag = isolationist_leading
		remove_country_flag = progressive_leading
		remove_country_flag = prosperity_leading
		remove_country_flag = supremacist_leading
		remove_country_flag = technologist_leading
		remove_country_flag = totalitarian_leading
		remove_country_flag = traditionalist_leading
		remove_country_flag = xenoist_leading
		remove_country_flag = manifesti_leading
		ordered_pop_faction = {
			position = 0
			order_by = trigger:pop_amount
			switch = {
				trigger = is_pop_faction_type
				imperialist = {
					root = { set_country_flag = imperialist_leading }
				}
				isolationist = {
					root = { set_country_flag = isolationist_leading }
				}
				progressive = {
					root = { set_country_flag = progressive_leading }
				}
				prosperity = {
					root = { set_country_flag = prosperity_leading }
				}
				supremacist = {
					root = { set_country_flag = supremacist_leading }
				}
				technologist = {
					root = { set_country_flag = technologist_leading }
				}
				totalitarian = {
					root = { set_country_flag = totalitarian_leading }
				}
				traditionalist = {
					root = { set_country_flag = traditionalist_leading }
				}
				xenoist = {
					root = { set_country_flag = xenoist_leading }
				}
				manifesti = {
					root = { set_country_flag = manifesti_leading }
				}
			}
		}
	}
}

# A war has been won
# Root = Winner Warleader
# From = Loser Warleader
# FromFrom = War
country_event = {
	id = bio.1080
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_origin = origin_evolutionary_predators
		is_mutation_authority = yes
		is_dictatorial_authority = yes
	}

	immediate = {
		from.species = {
			apex_predator_switch = {
				COUNTRY = root
			}
		}
	}
}

# A galcom resolution failed to pass
# this/root = proposer
# from = target if valid
country_event = {
	id = bio.1085
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_playable_country = {
			has_origin = origin_evolutionary_predators
			is_mutation_authority = yes
			is_dictatorial_authority = yes
			position_on_last_resolution = oppose
		}
	}

	immediate = {
		every_playable_country = {
			limit = {
				has_origin = origin_evolutionary_predators
				is_mutation_authority = yes
				is_dictatorial_authority = yes
				position_on_last_resolution = oppose
			}
			root.species = {
				apex_predator_switch = {
					COUNTRY = prev
				}
			}
		}
	}
}

# This = subject
# From = subject's overlord
country_event = {
	id = bio.1090
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		from = {
			has_origin = origin_evolutionary_predators
			is_mutation_authority = yes
			is_dictatorial_authority = yes
		}
	}

	immediate = {
		species = {
			apex_predator_switch = {
				COUNTRY = from
			}
		}
	}
}

# Executed when a ruler is created. Called after the leader is set as ruler of the country
# This = Country
# FROM = Leader
country_event = {
	id = bio.1095
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_origin = origin_evolutionary_predators
		is_mutation_authority = yes
		is_dictatorial_authority = yes
		ruler = {
			NOT = { has_trait = trait_ruler_apex_predator }
		}
	}

	immediate = {
		ruler = {
			add_trait = {
				trait = trait_ruler_apex_predator
			}
		}
		if = {
			limit = {
				is_overlord = yes
			}
			every_subject = {
				country_event = {
					id = bio.1090
					days = 5
					scopes = {
						from = root
					}
				}
			}
		}
	}
}

# Executed just as country is changing its government, after the new one is applied
# THIS = country

country_event = {
	id = bio.1100
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_origin = origin_evolutionary_predators
		is_mutation_authority = yes
		is_dictatorial_authority = no
		any_owned_leader = {
			has_trait = trait_ruler_apex_predator
		}
	}

	immediate = {
		every_owned_leader = {
			limit = {
				has_trait = trait_ruler_apex_predator
			}
			remove_trait = trait_ruler_apex_predator
		}
	}
}

country_event = {
	id = bio.1105
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		if = {
			limit = {
				capital_scope = {
					NOR = {
						is_planet_class = pc_hive
						has_carrier_flag = no_evopred_hive_world
					}
					colonizable_planet = yes
					habitable_structure = no
					is_terraforming = no
				}
			}
			capital_scope = { start_terraform_process = pc_hive }
			create_message = {
				type = EVOPRED_TERRAFORM_STARTED
				localization = MESSAGE_EVOPRED_TERRAFORM_STARTED
				target = capital_scope
				days = 30
				variable = {
					type = name
					localization = PLANET
					scope = capital_scope
				}
			}
		}
		else = {
			ordered_owned_planet = {
				limit = {
					NOR = {
						is_planet_class = pc_hive
						has_carrier_flag = no_evopred_hive_world
					}
					colonizable_planet = yes
					habitable_structure = no
					is_terraforming = no
				}
				position = 0
				order_by = trigger:pop_amount
				start_terraform_process = pc_hive
				root = {
					create_message = {
						type = EVOPRED_TERRAFORM_STARTED
						localization = MESSAGE_EVOPRED_TERRAFORM_STARTED
						target = prev
						days = 30
						variable = {
							type = name
							localization = PLANET
							scope = prev
						}
					}
				}
			}
		}
		country_event = {
			id = bio.1105
			years = 5
		}
	}
}

# Fired whenever a new owner is set for a planet, following a war or through trade
# This = Planet scope
# From = Country scope (new owner)
# FromFrom = Country scope (previous owner, if any)

carrier_event = {
	id = bio.1110
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_carrier_flag = no_evopred_hive_world
	}

	immediate = {
		remove_carrier_flag = no_evopred_hive_world
	}
}

# Scope: Leader (after assignment)
leader_event = {
	id = bio.1115
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_councilor = yes
		is_immortal = no
		is_organic_species = yes
		owner = {
			has_origin = origin_evolutionary_predators
			is_mutation_authority = yes
			is_oligarchic_authority = yes
		}
	}

	immediate = {
		set_immortal = yes
		set_leader_flag = evopred_oligarch_immortal
	}
}

# Scope: Leader (just before unassignment)
# Fires if a leader is unassigned from their position for any reason (including being assigned elsewhere)
leader_event = {
	id = bio.1120
	is_triggered_only = yes
	hide_window = yes

	trigger = {
		is_councilor = yes
		is_immortal = yes
		has_leader_flag = evopred_oligarch_immortal
	}

	immediate = {
		leader_event = { id = bio.1125 days = 1 }
	}
}

leader_event = {
	id = bio.1125
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_councilor = no
	}

	immediate = {
		set_immortal = no
		remove_leader_flag = evopred_oligarch_immortal
	}
}

# Executed just as country is changing its government, after the new one is applied
# THIS = country
country_event = {
	id = bio.1130
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_origin = origin_evolutionary_predators
		is_mutation_authority = yes
		is_oligarchic_authority = no
		any_owned_leader = {
			has_leader_flag = evopred_oligarch_immortal
		}
	}

	immediate = {
		every_owned_leader = {
			limit = {
				has_leader_flag = evopred_oligarch_immortal
			}
			set_immortal = no
			remove_leader_flag = evopred_oligarch_immortal
		}
	}
}

country_event = {
	id = bio.1131
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_origin = origin_evolutionary_predators
		is_mutation_authority = yes
		is_oligarchic_authority = yes
	}

	immediate = {
		every_owned_leader = {
			limit = {
				is_councilor = yes
				is_immortal = no
				is_organic_species = yes
			}
			set_immortal = yes
			set_leader_flag = evopred_oligarch_immortal
		}
	}
}

# Refreshes the tally of what the nomad purge vassals are purging on this empire's behalf. Only a nomad ever gets
# purge vassals. Runs even without any so that the tally drops back to zero once the last one is gone.
country_event = {
	id = bio.1135
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_origin = origin_evolutionary_predators
		is_nomadic = yes
	}

	immediate = {
		evopred_count_purge_vassal_yields_effect = yes
	}
}


###########################################################
# Starlit Citadel Events - Namespace: bio.2000 - bio.2500 #
###########################################################

# Sister System Event Chain
# Unusual Fluctuations
country_event = {
	id = bio.2005
	title = bio.2005.name
	desc = bio.2005.desc
	picture = GFX_evt_kaleidoscopic_lights
	location = root.capital_scope.star
	show_sound = event_bad_omen
	event_chain = abandoned_hatchery_chain_1

	is_triggered_only = yes

	immediate = {
		country_event = {
			id = bio.2010
			days = 90
			random = 180
		}
		begin_event_chain = {
			event_chain = "abandoned_hatchery_chain_1"
			target = this
		}
	}

	option = {
		name = DISTURBING
		tooltip = {
			begin_event_chain = {
				event_chain = "abandoned_hatchery_chain_1"
				target = this
			}
		}
	}
}

#Spawn BioShips in sister system and have them travel through the wormhole
country_event = {
	id = bio.2010
	is_triggered_only = yes
	hide_window = yes

	immediate = {
		#Create New Bioship Fleet in the Sealed System
		random_system = {
			limit = {
				has_star_flag = starlit_sealed_system_flag
			}
			save_global_event_target_as = starlit_sealed_system
			create_fleet = {
				settings = {
				}
				effect = {
					set_owner = event_target:bioship_hatchery
					create_ship_design = { design = "NAME_CHARGER_1" }
					while = {
						count = root.value:hatchery_fleet_mauler_count
						create_ship = {
							name = random
							design = last_created_design
							graphical_culture = "biogenesis_01"
							prefix = no
						}
					}
					create_ship_design = { design = "NAME_MOTH_1" }
					while = {
						count = root.value:hatchery_fleet_weaver_count
						create_ship = {
							name = random
							design = last_created_design
							graphical_culture = "biogenesis_01"
							prefix = no
						}
					}
					set_location = {
						target = event_target:abandoned_hatchery_base
						distance = 5
					}
					set_fleet_stance = aggressive
					set_aggro_range = 1000
					set_aggro_range_measure_from = self
					set_fleet_bombardment_stance = indiscriminate
					set_fleet_flag = first_starlit_attackers

					if = {
						limit = { exists = root.home_planet }
						queue_actions = { orbit_planet = root.home_planet }
					}
				}
			}
		}
		# Increase the number of BioShips by one
		change_variable = {
			which = hatchery_fleet_visits
			value = 1
		}
		# Schedule another invasion for 5 to 10 years time
		country_event = {
			id = bio.2035
			days = 1800
			random = 1800
		}
	}
}

# Remains of the Enemy - Player destroys first wave of bioships
# This = owner of fleet 1 (Player)
# From = owner of fleet 2 (Abandoned Hatchery)
# Fromfromfrom = fleet 2 (Attacking bioships)
country_event = {
	id = bio.2015
	title = bio.2015.name
	desc = bio.2015.desc
	picture = GFX_evt_bio_ships_orbiting
	location = event_target:bioship_debris
	show_sound = event_space_battle
	event_chain = abandoned_hatchery_chain_1

	is_triggered_only = yes

	trigger = {
		NOT = {
			has_country_flag = defeated_first_wave
		}
		this = {
			has_origin = origin_starlit_citadel
		}
		fromfromfrom = {
			has_fleet_flag = first_starlit_attackers
			NOT = {
				has_fleet_flag = abandoned_hatchery_fleet
			}
		}
	}

	immediate = {
		fromfromfrom = {
			create_ambient_object = {
				type = small_debris_object
				scale = 0.1
			}
			last_created_ambient_object = {
				save_event_target_as = bioship_debris
			}
		}
	}

	option = {
		name = bio.2015.a
		enable_special_project = {
			name = PROJECT_HATCHERY_SHIP_REMAINS
			location = event_target:bioship_debris
		}
	}

	after = {
		set_country_flag = defeated_first_wave
	}
}

#Enemy Analyzed - Player completes special project
country_event = {
	id = bio.2020
	title = bio.2020.name
	desc = bio.2020.desc
	picture = GFX_evt_bio_ships_orbiting
	location = root.capital_scope.star
	show_sound = event_space_battle
	event_chain = abandoned_hatchery_chain_1

	is_triggered_only = yes

	option = {
		name = EXCELLENT
		if = {
			limit = {
				NOT = {
					has_technology = tech_wormhole_stabilization
				}
			}
			add_tech_option_or_research_effect = {
				TECH = tech_wormhole_stabilization
				PROGRESS = 0.15
				CATEGORY = physics_research
			}
		}
		else = {
			add_monthly_resource_mult = {
				resource = society_research
				value = @tier3researchreward
				min = @tier3researchmin
				max = @tier3researchmax
			}
		}
		hidden_effect = {
			abort_special_project = {
				type = PROJECT_HATCHERY_SHIP_REMAINS
				location = capital_scope
			}
			end_event_chain = abandoned_hatchery_chain_1
			begin_event_chain = {
				event_chain = "abandoned_hatchery_chain_2"
				target = this
			}
		}
	}
}

# Cradle of the Enemy - Player researches wormhole tech
country_event = {
	id = bio.2024
	is_triggered_only = yes
	hide_window = yes

	trigger = {
		has_origin = origin_starlit_citadel
		last_increased_tech = tech_wormhole_stabilization
		NOR = {
			has_country_flag = explored_starlit_wormhole
			has_country_flag = hatchery_destroyed
		}
	}

	immediate = {
		country_event = {
			id = bio.2025
			days = 5
		}
	}
}

country_event = {
	id = bio.2025
	title = bio.2025.name
	desc = bio.2025.desc
	picture = GFX_evt_deep_space_citadel
	location = event_target:starlit_sealed_system
	show_sound = event_space_battle
	event_chain = abandoned_hatchery_chain_3
	is_triggered_only = yes

	immediate = {
		set_country_flag = explored_starlit_wormhole
	}

	option = {
		name = DISTURBING
		hidden_effect = {
			end_event_chain = abandoned_hatchery_chain_2
			begin_event_chain = {
				event_chain = "abandoned_hatchery_chain_3"
				target = this
			}
		}
	}
}

# Abandoned Hatchery defenses destroyed
# Called via on_ship_destroyed_perp
# This = owner of ship 1 (combatant)
# From = owner of ship 2 (destroyed)
# FromFrom = ship 1
# FromFromFrom = ship 2
country_event = {
	id = bio.2026
	is_triggered_only = yes
	hide_window = yes

	trigger = {
		from = {
			has_country_flag = abandoned_hatchery_country_flag
		}
		fromfromfrom = {
			fleet = {
				NOR = {
					has_fleet_flag = abandoned_hatchery_fleet
					has_fleet_flag = first_starlit_attackers
					has_fleet_flag = starlit_attackers
				}
			}
		}
		NOT = {
			has_country_flag = destroyed_abandoned_hatchery
		}
	}

	immediate = {
		set_country_flag = destroyed_abandoned_hatchery
		fromfromfrom.solar_system = {
			random_system_planet = {
				limit = {
					has_planet_flag = biomechanical_world
				}
				save_global_event_target_as = biomechanical_world_target
			}
		}
		every_system = {
			limit = {
				has_star_flag = starlit_nexus_system
			}
			root = {
				set_visited = prev
			}
		}
		if = {
			limit = {
				has_origin = origin_starlit_citadel
			}
			country_event = {
				id = bio.2027 # Twilight of Fear
				days = 1
			}
		}
		every_playable_country = {
			limit = {
				has_origin = origin_starlit_citadel
				NOT = {
					has_country_flag = destroyed_abandoned_hatchery
				}
			}
			country_event = {
				id = bio.2040 # Enemy Lost
			}
			set_country_flag = hatchery_destroyed
		}
	}
}

#Twilight of Fear
country_event = {
	id = bio.2027
	title = bio.2027.name
	desc = bio.2027.desc
	picture = GFX_evt_astral_rift_garden_2_fire
	location = event_target:starlit_sealed_system
	show_sound = event_space_battle
	event_chain = abandoned_hatchery_chain_4

	is_triggered_only = yes

	immediate = {
		hidden_effect = {
			end_event_chain = abandoned_hatchery_chain_3
			begin_event_chain = {
				event_chain = "abandoned_hatchery_chain_4"
				target = this
			}
			set_global_flag = open_hatchery
		}
	}

	option = {
		name = bio.2027.a
		enable_special_project = {
			name = PROJECT_BIOMECHANICAL_WORLD
			location = event_target:biomechanical_world_target
		}
		if = {
			limit = {
				is_machine_empire = no
			}
			add_modifier = {
				modifier = citadel_siege_lifted
				days = 3600
			}
		}
		else = {
			add_modifier = {
				modifier = citadel_siege_lifted_machine
				days = 3600
			}
		}
		hidden_effect = {
			country_event = {
				id = timeline.92
			}
			event_target:bioship_hatchery = {
				destroy_country = yes
			}
		}
	}
}

# BioMechanical Plague
country_event = {
	id = bio.2030
	title = bio.2030.name
	desc = bio.2030.desc
	picture = GFX_evt_astral_rift_garden_2
	location = event_target:biomechanical_world_target
	show_sound = event_space_battle
	event_chain = abandoned_hatchery_chain_4

	is_triggered_only = yes

	option = {
		name = bio.2030.a
		if = {
			limit = {
				is_gestalt = no
			}
			custom_tooltip = bio.2030.tt
			hidden_effect = {
				set_country_flag = unlocks_warden_of_the_citadel
			}
		}
		else = {
			add_modifier = {
				modifier = siege_node
				days = -1
			}
			random_owned_leader = {
				limit = {
					is_councilor_type = councilor_gestalt_cognitive
				}
				if = {
					limit = {
						NOT = {
							has_trait = leader_trait_expertise_military_theory
						}
					}
					add_trait = {
						trait = leader_trait_expertise_military_theory
					}
				}
				else_if = {
					limit = {
						has_trait = leader_trait_expertise_military_theory
					}
					add_trait = {
						trait = leader_trait_expertise_military_theory_2
					}
				}
				else_if = {
					limit = {
						has_trait = leader_trait_expertise_military_theory_2
					}
					add_trait = {
						trait = leader_trait_expertise_military_theory_3
					}
				}
				else = {
					add_experience = 400
				}
			}
		}
		add_monthly_resource_mult = {
			resource = society_research
			value = @tier3researchreward
			min = @tier3researchmin
			max = @tier3researchmax
		}
		end_event_chain = abandoned_hatchery_chain_4
	}
}

#Spawn BioShips in sister system and have them travel through the wormhole every few years
country_event = {
	id = bio.2035
	is_triggered_only = yes
	hide_window = yes

	trigger = {
		has_origin = origin_starlit_citadel
		NOT = {
			has_country_flag = destroyed_abandoned_hatchery
		}
	}

	immediate = {
		set_country_flag = seen_waves
		#Create New Bioship Fleet in the Sealed System
		event_target:starlit_sealed_system = {
			create_fleet = {
				settings = {
				}
				effect = {
					set_owner = event_target:bioship_hatchery
					create_ship_design = { design = "NAME_CHARGER_1" }
					while = {
						count = root.value:hatchery_fleet_mauler_count
						create_ship = {
							name = random
							design = last_created_design
							graphical_culture = "biogenesis_01"
							prefix = no
						}
					}
					create_ship_design = { design = "NAME_MOTH_1" }
					while = {
						count = root.value:hatchery_fleet_weaver_count
						create_ship = {
							name = random
							design = last_created_design
							graphical_culture = "biogenesis_01"
							prefix = no
						}
					}
					set_location = {
						target = event_target:abandoned_hatchery_base
						distance = 5
					}
					set_fleet_stance = aggressive
					set_aggro_range = 1000
					set_aggro_range_measure_from = self
					set_fleet_bombardment_stance = indiscriminate
					set_fleet_flag = starlit_attackers

					if = {
						limit = { exists = root.home_planet }
						queue_actions = { orbit_planet = root.home_planet }
					}
				}
			}
		}
		#Increase potency of BioShips
		event_target:bioship_hatchery = {
			add_modifier = {
				modifier = smarter_hatchery
				multiplier = root.hatchery_fleet_visits
			}
		}
		# Increase the number of BioShips by one and improve them against the player
		change_variable = {
			which = hatchery_fleet_visits
			value = 1
		}
		# Schedule another invasion for 5 to 10 years time
		country_event = {
			id = bio.2035
			days = 1800
			random = 1800
		}
	}
}

#They Learn
# on_fleet_destroyed_perp
# This = owner of fleet 1 (combatant)
# From = owner of fleet 2 (destroyed)
# FromFrom = fleet 1
# FromFromFrom = fleet 2
country_event = {
	id = bio.2036
	title = bio.2036.name
	desc = bio.2036.desc
	picture = GFX_evt_deep_space_citadel
	location = root.capital_scope
	show_sound = event_space_battle
	event_chain = abandoned_hatchery_chain_1

	is_triggered_only = yes

	trigger = {
		NOT = {
			has_country_flag = they_learn
		}
		this = {
			has_origin = origin_starlit_citadel
			has_country_flag = seen_waves
		}
		fromfromfrom = {
			has_fleet_flag = starlit_attackers
			NOT = {
				has_fleet_flag = abandoned_hatchery_fleet
			}
		}
	}

	immediate = {
		set_country_flag = they_learn
	}

	option = {
		trigger = {
			owner = {
				is_spiritualist = yes
			}
		}
		custom_tooltip = bio.2036.tt
		name = bio.2036.a
	}
	option = {
		trigger = {
			owner = {
				is_spiritualist = no
			}
		}
		custom_tooltip = bio.2036.tt
		name = bio.2036.b
	}
}

#Enemy Lost
# Fired by bio.2026
country_event = {
	id = bio.2040
	title = bio.2040.name
	desc = bio.2040.desc
	picture = GFX_evt_astral_rift_microverse
	location = event_target:starlit_sealed_system
	show_sound = event_space_battle
	event_chain = abandoned_hatchery_chain_1

	is_triggered_only = yes

	option = {
		name = bio.2027.a
		end_event_chain = abandoned_hatchery_chain_1
		if = {
			limit = {
				is_machine_empire = no
			}
			add_modifier = {
				modifier = citadel_siege_cancelled
				days = 1800
			}
		}
		else = {
			add_modifier = {
				modifier = citadel_siege_cancelled_machine
				days = 1800
			}
		}
		hidden_effect = { #Done to end Chain no matter which stage it is
			end_event_chain = abandoned_hatchery_chain_2
			end_event_chain = abandoned_hatchery_chain_3
			end_event_chain = abandoned_hatchery_chain_4
		}
	}
}
