namespace = aap

#Fix species - add psionic and cyborg to all organic pops if you have both of them
country_event = {
	id = aap.01
	title = aap.01.title
	is_triggered_only = yes
	hide_window = yes
	immediate = {
		if = {
			limit = { aap_can_psionic_cyborg_assimilate = yes }
			every_owned_species = {
				limit = {
					aap_species_to_check_for_psi_cyber_base = yes
					NAND = {
						has_trait = trait_psionic
						has_trait = trait_cybernetic
					}
				}
				save_event_target_as = oldSpecies
				modify_species = {
					species = event_target:oldSpecies
					remove_trait = trait_limited_cybernetic
					add_trait = trait_psionic
					add_trait = trait_cybernetic
					effect = { save_event_target_as = newSpecies }
				}
				root = { aap_switch_pop_species = yes }
			}
		}
		else_if = {
			limit = { aap_can_cyborg_assimilate = yes }
			every_owned_species = {
				limit = {
					aap_species_to_check_for_psi_cyber_base = yes
					NAND = {
						has_trait = trait_cybernetic
					}
				}
				save_event_target_as = oldSpecies
				modify_species = {
					species = event_target:oldSpecies
					remove_trait = trait_limited_cybernetic
					add_trait = trait_cybernetic
					effect = { save_event_target_as = newSpecies }
				}
				root = { aap_switch_pop_species = yes }
			}
		}
		else_if = {
			limit = { aap_can_psionic_assimilate = yes }
			every_owned_species = {
				limit = {
					aap_species_to_check_for_psi_cyber_base = yes
					NAND = {
						has_trait = trait_psionic
					}
				}
				save_event_target_as = oldSpecies
				modify_species = {
					species = event_target:oldSpecies
					remove_trait = trait_limited_cybernetic
					add_trait = trait_psionic
					effect = { save_event_target_as = newSpecies }
				}
				root = { aap_switch_pop_species = yes }
			}
		}

		# deal with virtual trait
		country_event = { id = aap.12 }
	}
}

#Fix leaders - add psionic, cyborg and erudite to leaders if you have them
country_event = { 
	id = aap.02
	title = aap.02.title
	is_triggered_only = yes
	hide_window = yes
	immediate = {
		every_owned_leader = {
			limit = {
				species = { has_trait = trait_cybernetic }
				NOR = {
					has_trait = leader_trait_cyborg
					has_trait = leader_trait_curator
				}
			}
			add_trait = { trait = leader_trait_cyborg }
		}
		every_owned_leader = {
			limit = {
				species = { has_trait = trait_erudite }
				NOR = {
					has_trait = leader_trait_erudite
					has_trait = leader_trait_curator
				}
			}
			add_trait = { trait = leader_trait_erudite }
		}
		every_owned_leader = {
			limit = {
				OR = {
					species = { has_trait = trait_psionic }
					# catcher because the psionic ascension does not go thorugh and modify leader species.
					AND = {
						species = { 
							is_same_species = root 
							NOT = { has_trait = trait_mechanical }
						}
						root = {
							aap_can_psionic_assimilate = yes
						}
					}
				}
				NOT = {
					has_trait =	leader_trait_psionic
				}
			}
			add_trait = { trait = leader_trait_psionic }
		}
		every_owned_leader = {
			limit = {
				species = { has_trait = trait_mechanical }
				NOR = {
					has_trait = leader_trait_synthetic
				}
			}
			add_trait = { trait = leader_trait_synthetic }
		}

		#pool leaders (identical save loop)
		every_pool_leader = {
			limit = {
				species = { has_trait = trait_cybernetic }
				NOR = {
					has_trait = leader_trait_cyborg
					has_trait = leader_trait_curator
				}
			}
			add_trait = { trait = leader_trait_cyborg }
		}
		every_pool_leader = {
			limit = {
				species = { has_trait = trait_erudite }
				NOR = {
					has_trait = leader_trait_erudite
					has_trait = leader_trait_curator
				}
			}
			add_trait = { trait = leader_trait_erudite }
		}
		every_pool_leader = {
			limit = {
				OR = {
					species = { has_trait = trait_psionic }
					# catcher because the psionic ascension does not go thorugh and modify leader species.
					AND = {
						species = { 
							is_same_species = root 
							NOT = { has_trait = trait_mechanical }
						}
						root = {
                            aap_can_psionic_assimilate = yes
						}
					}
				}
				NOT = {
					has_trait =	leader_trait_psionic
				}
			}
			add_trait = { trait = leader_trait_psionic }
		}
		every_pool_leader = {
			limit = {
				species = { has_trait = trait_mechanical }
				NOR = {
					has_trait = leader_trait_synthetic
				}
			}
			add_trait = { trait = leader_trait_synthetic }
		}
	}
}


#Fix leader species
country_event = { 
	id = aap.03
	title = aap.03.title
	is_triggered_only = yes
	hide_window = yes
	immediate = {
		if = {
			limit = {
				any_owned_pop_group = {
					species = {
						 has_trait = trait_cybernetic 
						 has_trait = trait_erudite 
						 has_trait = trait_psionic
						 is_same_species = root 
					}
				}
			}
			random_owned_pop_group = {
				limit = {
					species = {
						has_trait = trait_cybernetic 
						has_trait = trait_erudite 
						has_trait = trait_psionic
						is_same_species = root 
					}
				}
				species = {
					save_event_target_as = uberspecies
				}
			}
			every_owned_leader = {
				limit = {
					species = {
						is_same_species = root 
						NOT = { is_same_value = event_target:uberspecies }
					}				
				}
				change_species = event_target:uberspecies
			}
			every_pool_leader = {
				limit = {
					species = {
						is_same_species = root 
						NOT = { is_same_value = event_target:uberspecies }
					}				
				}
				change_species = event_target:uberspecies
			}
		}
		country_event = {
			id = aap.02
		}
	}
}

#set bio dominant
country_event = { 
	id = aap.04
	title = aap.04.title
	is_triggered_only = yes
	hide_window = yes
	immediate = {
		if = {
			limit = {
				any_owned_pop_group = {
					species = {
						 has_trait = trait_cybernetic 
						 has_trait = trait_erudite 
						 has_trait = trait_psionic
						 is_same_species = root 
					}
				}
			}
			random_owned_pop_group = {
				limit = {
					species = {
						has_trait = trait_cybernetic 
						has_trait = trait_erudite 
						has_trait = trait_psionic
						is_same_species = root 
					}
				}
				species = {
					save_event_target_as = uberspecies
				}
			}
			change_dominant_species = {
				species = event_target:uberspecies
			}
			country_event = {
				id = aap.06
			}
		}
	}
}

#set synth dominant
country_event = { 
	id = aap.05
	title = aap.05.title
	is_triggered_only = yes
	hide_window = yes
	immediate = {
		if = {
			limit = {
				any_owned_pop_group = {
					species = {
						 has_trait = trait_mechanical
						 is_same_species = root 
					}
				}
			}
			random_owned_pop_group = {
				limit = {
					species = {
						has_trait = trait_mechanical
						is_same_species = root 
					}
				}
				species = {
					save_event_target_as = uberspecies
				}
			}
			change_dominant_species = {
				species = event_target:uberspecies
			}
			country_event = {
				id = aap.06
			}
		}
	}
}


country_event = {
	id = aap.06
	title = aap.06.title
	desc = aap.06.desc
	picture = GFX_evt_ancient_databank
	is_triggered_only = yes

	option = {
		name = OK
	}
}


# Synth Ascension Question
country_event = { 
	id = aap.10
	title = aap.10.title
	desc = aap.10.desc
	picture = GFX_evt_ancient_databank
	is_triggered_only = yes

	immediate = {
		if = {
			limit = {
				has_country_flag = aap_no_machine_age
			}
			root.owner_species = {
				save_event_target_as = oldSpecies
			}
		}
		else = {
			root.owner_species = {
				save_global_event_target_as = aap_oldspecies@root.owner
			}
		}
	}
	
	option = {
		name = aap.10.0.synths
		hidden_effect = {
			capital_scope = {
				set_variable = { which = bio_percentage value = 0 }
			}
		}
	}
	option = {
		name = aap.10.25.synths
		hidden_effect = {
			capital_scope = {
				set_variable = { which = bio_percentage value = 0.25 }
			}
		}
	}	
	option = {
		name = aap.10.50.synths
		hidden_effect = {
			capital_scope = {
				set_variable = { which = bio_percentage value = 0.5 }
			}
		}
	}
	option = {
		name = aap.10.75.synths
		hidden_effect = {
			capital_scope = {
				set_variable = { which = bio_percentage value = 0.75 }
			}
		}
	}
	option = {
		name = aap.10.25.bio
		hidden_effect = {
			capital_scope = {
				set_variable = { which = bio_percentage value = 0.25 }
			}
			set_country_flag = bio_leaders
		}
	}
	option = {
		name = aap.10.50.bio
		hidden_effect = {
			capital_scope = {
				set_variable = { which = bio_percentage value = 0.5 }
			}
			set_country_flag = bio_leaders
		}
	}	
	option = {
		name = aap.10.75.bio
		hidden_effect = {
			capital_scope = {
				set_variable = { which = bio_percentage value = 0.75 }
			}
			set_country_flag = bio_leaders
		}
	}
	option = {
		name = aap.10.90.bio
		hidden_effect = {
			capital_scope = {
				set_variable = { which = bio_percentage value = 0.90 }
			}
			set_country_flag = bio_leaders
		}
	}
	after = {
		if = {
			limit = {
				has_country_flag = aap_no_machine_age
			}
			country_event = {
				id = utopia.2551
			}
			hidden_effect = {
				country_event = {
					id = aap.11
					days = 1
				}
			}
		}
	}
}

# Create Bio Pops
country_event = {
	id = aap.11
	title = aap.11.title
	is_triggered_only = yes
	hide_window = yes

	immediate = {
		if = {
			limit = { capital_scope = { check_variable = { which = bio_percentage value > 0 } } }
			every_owned_planet = {
				# Find a pop group with the right assimilation type
				every_owned_pop_group = {
					limit = {
						has_trait = trait_mechanical
						pop_group_size > 10
					}
					save_event_target_as = old_pop_group
					planet = {
						create_pop_group = {
							pop_group = event_target:old_pop_group
							species = event_target:oldSpecies
							size = 0
							effect = {
								transfer_pop_amount = {
									source = event_target:old_pop_group
									target = this
									percentage = owner.capital_scope.bio_percentage
								}
							}
						}
					}
				}
			}
			clear_variable = capital_scope.bio_percentage
		}

		# if we have bio leaders flip species and remove synth trait
		if = {
			limit = { has_country_flag = bio_leaders }

			# bio leaders = we assume dominant species should remain bio
			change_dominant_species = { species = event_target:oldSpecies}

			every_owned_leader = {
				limit = {
					NOR = {
						has_leader_flag = renowned_leader
						has_leader_flag = legendary_leader
                        is_leader_tier = leader_tier_renowned
                        is_leader_tier = leader_tier_legendary
                        is_event_leader = yes
					}
				}
				change_species = event_target:oldSpecies
				change_leader_portrait = species
				remove_trait = leader_trait_synthetic
			}
			every_pool_leader = {
				change_species = event_target:oldSpecies
				change_leader_portrait = species
				remove_trait = leader_trait_synthetic
			}
		}
		# else if we have synthetic leaders, they probably won't have lost their special traits
		# as those get added to the leader directly
		# so de-bio them
		else = {
			every_owned_leader = {
				limit = {
					NOR = {
						has_leader_flag = renowned_leader
						has_leader_flag = legendary_leader
					}
				}
				remove_trait = leader_trait_cyborg
				remove_trait = leader_trait_limited_cyborg
				remove_trait = leader_trait_psionic
				remove_trait = leader_trait_erudite
			}
			every_pool_leader = {
				remove_trait = leader_trait_cyborg
				remove_trait = leader_trait_limited_cyborg
				remove_trait = leader_trait_psionic
				remove_trait = leader_trait_erudite	
			}
		}
	}
}



# Fix virtual synthetics
country_event = {
	id = aap.12
	is_triggered_only = yes
	hide_window = yes

	immediate = {
		every_owned_species = {
			limit = {
				is_same_species = root
				has_trait = trait_robot_digital_1
				NOT = { has_trait = trait_mechanical}
			}
			change_species_characteristics = { remove_trait = trait_robot_digital_1 }
		}
		owner_main_species = {
			if = {
				limit = {
					OR = {
						has_trait = trait_psionic
						has_trait = trait_cybernetic
					}
					has_trait = trait_robot_digital_1
				}
				change_species_characteristics = { remove_trait = trait_robot_digital_1 }
			}
		}
		every_owned_species = {
			limit = {
				is_same_species = root
				has_trait = trait_mechanical
				NOT = {  has_trait = trait_robot_digital_1 }
			}
			change_species_characteristics = { add_trait = trait_robot_digital_1 }
		}
		refresh_portraits = character
	}
}


country_event = {
	id = aap.20
	title = aap.20.title
	desc = {
		exclusive_trigger = { num_tradition_categories < @max_tradition_trees }
		text = aap.20.desc
	}
	desc = {
		exclusive_trigger = { num_tradition_categories >= @max_tradition_trees }
		text = aap.20.desc.manytrads
	}
	picture = GFX_evt_ancient_databank
	is_triggered_only = yes

	trigger = { has_machine_age_dlc = yes }

	option = {
		name = EXIT
		default_hide_option = yes
	}

	option = {
		trigger = { NOT = { has_tradition = tr_nanotech_adopt } }
		name = machine_age.4005.a.nanotech.tt
		set_country_flag = nanotech_traditions_unlocked
		add_tradition = tr_nanotech_adopt
		hidden_effect = {
			set_variable = {
				which = nanotech_worlds_created
				value = 0
			}
		}
	}

	option = {
		trigger = { NOT = { has_tradition = tr_virtuality_adopt } }
		name = machine_age.4005.a.virtuality.tt
		set_country_flag = virtuality_traditions_unlocked
		add_tradition = tr_virtuality_adopt
		hidden_effect = {
			refresh_portraits = character
		}
	}

	option = {
		trigger = { NOT = { has_tradition = tr_modularity_adopt } }
		name = machine_age.4005.a.modularity.tt
		set_country_flag = modularity_traditions_unlocked
		add_tradition = tr_modularity_adopt
	}
}



country_event = {
	id = aap.30
	title = aap.30.title
	desc = {
		exclusive_trigger = { num_tradition_categories < @max_tradition_trees }
		text = aap.30.desc
	}
	desc = {
		exclusive_trigger = { num_tradition_categories >= @max_tradition_trees }
		text = aap.30.desc.manytrads
	}
	picture = GFX_evt_ancient_databank
	is_triggered_only = yes

	trigger = { has_biogenesis_dlc = yes }

	immediate = {
		if = {
			limit = { NOT = { has_country_flag = aap_multi_option_screen_first_time } }
			aap_remove_flexible_unlock_flags = yes
			switch = {
				trigger = has_tradition
				tr_cloning_adopt = {
					give_cloning_unlocks_effect = yes
					give_cloning_unlocks_effect = yes
					give_cloning_unlocks_effect = yes
				}
				tr_mutation_adopt = {
					give_mutation_unlocks_effect = yes
					give_mutation_unlocks_effect = yes
					give_mutation_unlocks_effect = yes
				}
				tr_purity_adopt = {
					give_purity_unlocks_effect = yes
					give_purity_unlocks_effect = yes
					give_purity_unlocks_effect = yes
				}
			}
			set_country_flag = aap_multi_option_screen_first_time
		}
	}

	option = {
		name = EXIT
		default_hide_option = yes
	}

	option = {
		trigger = {
			NOR = {
				has_tradition = tr_cloning_adopt
			}
		}
		name = tradition_cloning
		set_country_flag = cloning_tradition_unlocked
		add_tradition = tr_cloning_adopt
		aap_fix_flexible_tradition_flags = yes
		aap_set_multi_tradition = yes
		hidden_effect = {
			give_cloning_unlocks_effect = yes
			give_cloning_unlocks_effect = yes
			give_cloning_unlocks_effect = yes
		}
	}

	option = {
		trigger = {
			NOR = {
				has_tradition = tr_mutation_adopt
			}
		}
		name = tradition_mutation
		set_country_flag = mutation_tradition_unlocked
		add_tradition = tr_mutation_adopt
		aap_fix_flexible_tradition_flags = yes
		aap_set_multi_tradition = yes
		hidden_effect = {
			give_mutation_unlocks_effect = yes
			give_mutation_unlocks_effect = yes
			give_mutation_unlocks_effect = yes
		}
	}

	option = {
		trigger = {
			NOR = {
				has_tradition = tr_purity_adopt
			}
		}
		name = tradition_purity
		set_country_flag = purity_tradition_unlocked
		add_tradition = tr_purity_adopt

		aap_fix_flexible_tradition_flags = yes
		aap_set_multi_tradition = yes
		hidden_effect = {
			give_purity_unlocks_effect = yes
			give_purity_unlocks_effect = yes
			give_purity_unlocks_effect = yes
		}
	}
}


country_event = {
	id = aap.40
	title = aap.40.title
	desc =  aap.40.desc
	picture = GFX_evt_ancient_databank
	is_triggered_only = yes

	trigger = {
		OR = {
			has_biogenesis_dlc = yes
			has_machine_age_dlc = yes
            has_shroud_dlc = yes
		}
		is_gestalt = no
	}

	option = {
		name = aap.40.no.change
		default_hide_option = yes
	}

	option = {
		name = aap.40.default.government
		aap_remove_government_flags = yes
		advanced_authority_refresh = yes
	}

    option = {
        allow = { has_tradition = tr_psionics_shroud_finish }
        name = aap.40.shroud.corporeal
        aap_remove_government_flags = yes
        country_event = { id = shroud.7121 }
    }

    option = {
        allow = { has_tradition = tr_psionics_shroud_finish }
        name = aap.40.shroud.transcendent
        aap_remove_government_flags = yes
        country_event = { id = shroud.7120 }
    }

	option = {
		allow = { has_tradition = tr_cybernetics_finish }
		name = aap.40.cyber.individualist
		aap_remove_government_flags = yes
		set_country_flag = cyber_individualist
		hidden_effect = {
			advanced_authority_refresh = yes
		}
		if = {
			limit = {
				is_imperial_authority = yes
				NOT = { has_relic = r_the_rulerchip }
			}
			add_relic = r_the_rulerchip
		}
	}

	option = {
		allow = { has_tradition = tr_cybernetics_finish }
		name = aap.40.cyber.collectivist
		aap_remove_government_flags = yes
		set_country_flag = cyber_collectivist
		hidden_effect = {
			advanced_authority_refresh = yes
		}
	}


	option = {
		allow = { has_tradition = tr_cloning_finish }
		name = aap.40.cloning
		custom_tooltip = bio.175.a.tt
		aap_remove_government_flags = yes
		owner = {
			hidden_effect = {
				set_country_flag = bio_cloning
				advanced_authority_refresh = yes
				give_backup_clones_effect = yes
			}
		}
	}

	option = {
		allow = { has_tradition = tr_mutation_finish }
		name = aap.40.mutation
		custom_tooltip = bio.180.a.tt
		aap_remove_government_flags = yes
		owner = {
			hidden_effect = {
				set_country_flag = bio_mutation
				if = {
					limit = {
						has_origin = origin_evolutionary_predators
						is_democratic_authority = yes
					}
					country_event = { id = bio.1075 }
				}
				else_if = {
					limit = {
						has_origin = origin_evolutionary_predators
						is_dictatorial_authority = yes
					}
					ruler = {
						add_trait = { trait = trait_ruler_apex_predator }
					}
				}
				else_if = {
					limit = {
						has_origin = origin_evolutionary_predators
						is_oligarchic_authority= yes
					}
					every_owned_leader = {
						limit = {
							is_councilor = yes
							is_organic_species = yes
						}
						set_immortal = yes
						set_leader_flag = evopred_oligarch_immortal
					}
				}
				if = {
					limit = {
						has_origin = origin_evolutionary_predators
					}
					every_owned_species = {
						limit = {
							OR = {
								is_archetype = PRESAPIENT
								is_archetype = BIOLOGICAL
								is_archetype = LITHOID
							}
						}
						set_species_traits_evopred_count_variable = yes
					}
					if = {
						limit = {
							is_dictatorial_authority = yes
						}
						set_ruler_title_male = apex_predator_male
						set_ruler_title_female = apex_predator_female
					}
					if = {
						limit = {
							is_imperial_authority = yes
						}
						set_ruler_title_male = "eternal_ancestor_male"
						set_ruler_title_female = "eternal_ancestor_female"
					}
				}
				advanced_authority_refresh = yes
			}
		}
	}

	option = {
		allow = { has_tradition = tr_purity_finish }
		name = aap.40.purity
		aap_remove_government_flags = yes
		owner = {
			hidden_effect = {
				set_country_flag = bio_purity
				advanced_authority_refresh = yes
			}
		}
		custom_tooltip = bio.170.a.tt
		hidden_effect = {
			every_owned_species = {
				limit = {
					OR = {
						is_archetype = PRESAPIENT
						is_archetype = BIOLOGICAL
						is_archetype = LITHOID
					}
				}
				set_species_traits_purity_count_variable = yes
			}
			if = {
				limit = {
					is_imperial_authority = yes
				}
				every_owned_leader = {
					limit = {
						is_same_species = root.ruler
					}
					add_trait = { trait = trait_imperial_heir }
				}
				every_pool_leader = {
					limit = {
						is_same_species = root.ruler
					}
					add_trait = { trait = trait_imperial_heir }
				}
			}
			else_if = {
				limit = {
					is_hive_empire = no
					is_megacorp = no
				}
				every_owned_leader = {
					limit = {
						species = {
							is_robotic_species = no
						}
						NOT = { has_trait = trait_leader_genetic_purist }
					}
					give_trait_no_notify_ignore_autocracy_restriction = {
						TRAIT = trait_leader_genetic_purist
					}
				}
				every_pool_leader = {
					limit = {
						species = {
							is_robotic_species = no
						}
						NOT = { has_trait = trait_leader_genetic_purist }
					}
					give_trait_no_notify_ignore_autocracy_restriction = {
						TRAIT = trait_leader_genetic_purist
					}
				}
			}
		}
	}
}
