# Written by Exakan.
# My other mods: Stellaris Killcounter, DarkSpace, Replicators (Midgame Crisis), Cellaris


namespace = elec


# Free slots: elec.23

# Adds global flag to mark the mod as installed. Can be used by other modders for trigger checks.
event = {
	id = elec.1
	hide_window = yes
	is_triggered_only = yes
	fire_only_once = yes
	immediate = {
		set_global_flag = electroids_installed
		event_target:global_event_country = { set_variable = { which = Electroids_Version value = 2.40 } }
		set_global_flag = Electroids_2_40_a
	}
}

# Aurora Habitat Complete Notification
country_event = {
	id = elec.2
	title = "elec.2.name"
	desc = "elec.2.desc"
	show_sound = event_mystic_reveal
	picture = GFX_evt_star_yellow
	is_triggered_only = yes
	location = event_target:habitat_planet
	option = { name = EXCELLENT }
}

# Adds the starting districts & initializer setups.
country_event = {
	id = elec.3
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		OR = {
			has_origin = origin_elec_sun_empire
			has_origin = origin_elec_supernova
			has_origin = origin_elec_endless_horizon
			has_origin = origin_elec_nebula_of_creation
			has_civic = civic_elec_symbiosis
			has_civic = civic_elec_h_symbiosis
			has_civic = civic_elec_mobile_solarflare
			has_civic = civic_elec_h_mobile_solarflare
		}
	}
	immediate = {
# Basic districts
		if = {
			limit = {
				NOT = { has_authority = auth_corporate }
			}
			capital_scope = {
				clear_blockers = yes
				clear_deposits = yes
				remove_all_districts = yes
				add_deposit = d_electroids_start
				add_district = district_aurora_city
				add_district = district_aurora_energy
				add_district = district_aurora_energy
				add_district = district_aurora_industrial
				add_district = district_aurora_minerals
				add_district = district_aurora_minerals
			}
		}
		if = {
			limit = {
				has_authority = auth_corporate
			}
			capital_scope = {
				clear_blockers = yes
				clear_deposits = yes
				remove_all_districts = yes
				add_deposit = d_electroids_start
				add_district = district_aurora_city
				add_district = district_aurora_trade
				add_district = district_aurora_trade
				add_district = district_aurora_industrial
				add_district = district_aurora_minerals
				add_district = district_aurora_minerals
			}
		}
# To have the starting production in a stable state & counter bonuses of other origins (doesnt have any other economy modifier).
		if = {
			limit = {
				is_gestalt = no
			}
			capital_scope = {
				while = {
					count = 1
					create_pop = {
						species = prev
						ethos = prev
					}
				}
				add_district = district_aurora_city
				add_district = district_aurora_city
				add_district = district_aurora_city
				every_owned_pop = { clear_pop_category = yes }
				check_planet_employment = yes
			}
		}
		if = {
			limit = {
				is_gestalt = yes
			}
			capital_scope = {
				while = {
					count = 2
					create_pop = {
						species = prev
						ethos = prev
					}
				}
				#add_building = building_elec_food_1
				add_district = district_aurora_energy
				add_district = district_aurora_energy
				add_district = district_aurora_energy
				add_district = district_aurora_energy
				every_owned_pop = { clear_pop_category = yes }
				check_planet_employment = yes
			}
		}
# Nuclear Anthem civic = adds planet flag to capital for decisions
		if = {
			limit = {
				OR = {
					has_civic = civic_elec_nuclear_anthem
					has_civic = civic_elec_h_nuclear_anthem
				}
			}
			capital_scope = {
				set_variable = { which = elec_aurora_capital_var value = 0 }
				set_planet_flag = elec_aurora_capital
			}
		}
		owner_main_species = { change_species_characteristics = { remove_trait = trait_pc_continental_preference } }
		capital_scope = { remove_building = building_spawning_pool } # Can not be assembled right now, uses a trait modifier instead.
# Symbiosis civic = adds tech as option
		if = {
			limit = {
				OR = {
					has_civic = civic_elec_symbiosis
					has_civic = civic_elec_h_symbiosis
				}
			}
			add_research_option = tech_elec_dm_cell
			set_variable = { which = elec_symbiosis_var value = 0 }
		}
# Solarflare civic = adds tech as option
		if = {
			limit = {
				OR = {
					has_civic = civic_elec_mobile_solarflare
					has_civic = civic_elec_h_mobile_solarflare
				}
			}
			add_tech_progress = { tech = tech_jump_drive_1 progress = 0.75 }
		}
	}
}

# Burnout Ascension Perk - random pops with the 'Proton Decay' trait CAN die, if unlucky.
country_event = {
	id = elec.4
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		every_owned_pop = {
			limit = { has_trait = trait_elec_burnout }
			random_list = {
				95 = { } # Nothing.
				5 = { kill_pop = yes }
			}
		}
		country_event = { id = elec.4 days = 1460 random = 365 }
	}
}

# Detonating a star to turn it into a black hole.
planet_event = {
	id = elec.5
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		solar_system = {
			random_system_planet = {
				limit = { has_modifier = holy_planet }
				planet_event = { id = planet_destruction.605 }
			}
			set_star_flag = star_cracked
			set_star_class = sc_black_hole
			every_system_planet = {
				limit = { is_star = yes }
				create_ambient_object = {
					type = "star_explosion"
					play_animation_once = yes
					location = this
				}
				last_created_ambient_object = {
					set_location = {
						target = prev
						distance = 0
						angle = random
					}
				}
				change_pc = pc_black_hole
				reroll_deposits = yes
			}
			every_system_planet = {
				limit = {
					is_star = no
					is_asteroid = no
					NOR = {
						is_planet_class = pc_broken
						is_planet_class = pc_shattered
						is_planet_class = pc_shattered_2
						is_planet_class = pc_shielded
						is_planet_class = pc_ringworld_habitable_damaged
						is_planet_class = pc_ringworld_tech_damaged
						is_planet_class = pc_ringworld_seam_damaged
						is_planet_class = pc_ringworld_habitable
					}
				}			
				if = {
					limit = { is_planet_class = pc_habitat }
					spawn_habitat_cracker_effect = yes
					remove_planet = yes
				}			
				if = {
					limit = { is_colony = yes }
					if = {
						limit = {
							exists = orbital_defence
						}
						destroy_fleet = orbital_defence
					}
					remove_all_buildings = yes
					destroy_colony = yes
					if = {
						limit = { is_planet_class = pc_habitat }
						spawn_habitat_cracker_effect = yes
						remove_planet = yes
					}
					else = {
						change_pc = pc_broken	#rl_elec_ignite_planets
					}
				}
			}
			every_system_planet = {
				limit = {
					exists = orbital_station
				}
				orbital_station = { dismantle = yes }
			}
		}
	}
}

# Supernova origin: Adds flag & starts event chain (6. fired by on_action).
# Following events end the cycle in 45-55 years.
event = {
	id = elec.6
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		every_playable_country = {
			limit = { has_origin = origin_elec_supernova }
			country_event = { id = elec.7 days = 1 }
		}
	}
}

# 0 Years - Start modifier + warning
country_event = {
	id = elec.7
	title = elec.7.name
	desc = elec.7.desc
	picture = GFX_evt_star_red_elec
	show_sound = event_wind_ruins
	is_triggered_only = yes
	immediate = {
		save_event_target_as = supernova_owner
		capital_scope = {
			set_planet_flag = doomed_star_origin
			add_modifier = {
				modifier = elec_supernova_mod
			}
			planet_event = { id = elec.8 days = 1825 }
			solar_system = {
				set_star_flag = doomed_star_origin_star
			}
		}
	}
	option = { name = elec.7.a }
}

# 5 Years - 2x modifier
planet_event = {
	id = elec.8
	title = elec.8.name
	desc = elec.8.desc
	picture = GFX_evt_star_red_elec
	show_sound = event_wind_ruins
	location = root
	is_triggered_only = yes
	immediate = {
		remove_modifier = elec_supernova_mod
		add_modifier = {
			modifier = elec_supernova_mod
			mult = 2
		}
		planet_event = { id = elec.9 days = 1825 }
	}
	option = { name = elec.8.a }
}

# 10 Years - 3x modifier
planet_event = {
	id = elec.9
	title = elec.9.name
	desc = elec.9.desc
	picture = GFX_evt_star_red_elec
	show_sound = event_wind_ruins
	location = root
	is_triggered_only = yes
	immediate = {
		remove_modifier = elec_supernova_mod
		add_modifier = {
			modifier = elec_supernova_mod
			mult = 3
		}
		planet_event = { id = elec.10 days = 1825 }
	}
	option = { name = elec.9.a }
}

# 15 Years - 4x modifier + solar storm 1 + devastation
planet_event = {
	id = elec.10
	title = elec.10.name
	desc = elec.10.desc
	picture = GFX_evt_supernova
	show_sound = event_wind_ruins
	location = root
	is_triggered_only = yes
	immediate = {
		remove_modifier = elec_supernova_mod
		add_modifier = {
			modifier = elec_supernova_mod
			mult = 4
		}
		add_planet_devastation = 10
		planet_event = { id = elec.11 days = 1825 }
	}
	option = { name = elec.10.a }
}

# 20 Years - 5x modifier
planet_event = {
	id = elec.11
	title = elec.11.name
	desc = elec.11.desc
	picture = GFX_evt_star_red_elec
	show_sound = event_wind_ruins
	location = root
	is_triggered_only = yes
	immediate = {
		remove_modifier = elec_supernova_mod
		add_modifier = {
			modifier = elec_supernova_mod
			mult = 5
		}
		planet_event = { id = elec.12 days = 1825 }	
	}
	option = { name = elec.11.a }
}

# 25 Years - 6x modifier + solar storm 2 + devastation + solar storm entity
planet_event = {
	id = elec.12
	title = elec.12.name
	desc = elec.12.desc
	picture = GFX_evt_supernova
	show_sound = event_wind_ruins
	location = root
	is_triggered_only = yes
	immediate = {
		remove_modifier = elec_supernova_mod
		add_modifier = {
			modifier = elec_supernova_mod
			mult = 6
		}		
		while = {
			limit = { has_district = district_aurora_city }
			remove_district = district_aurora_city
		}
		add_planet_devastation = 10
		planet_event = { id = elec.13 days = 1825 }
		solar_system = {
			create_ambient_object = {
				type = "space_storm_2"
				location = this
			}
			last_created_ambient_object = {
				set_location = {
					target = prev
					distance = 0
					angle = random
				}
				set_ambient_object_flag = elec_supernova_solarstorm
			}
		}		
	}
	option = { name = elec.12.a }
}

# 30 Years - 7x modifier
planet_event = {
	id = elec.13
	title = elec.13.name
	desc = elec.13.desc
	picture = GFX_evt_star_red_elec
	show_sound = event_wind_ruins
	location = root
	is_triggered_only = yes
	immediate = {
		remove_modifier = elec_supernova_mod
		add_modifier = {
			modifier = elec_supernova_mod
			mult = 7
		}
		planet_event = { id = elec.14 days = 1825 }	
	}
	option = { name = elec.13.a }
}

# 35 Years - 8x modifier + solar storm 3 + devastation
planet_event = {
	id = elec.14
	title = elec.14.name
	desc = elec.14.desc
	picture = GFX_evt_supernova
	show_sound = event_wind_ruins
	location = root
	is_triggered_only = yes
	immediate = {
		random_owned_pop = { kill_pop = yes }
		remove_modifier = elec_supernova_mod
		add_modifier = {
			modifier = elec_supernova_mod
			mult = 8
		}
		add_planet_devastation = 15
		if = {
			limit = { owner = { NOT = { has_authority = auth_corporate } } }
			while = {
				count = 1
				limit = { has_district = district_aurora_energy }
				remove_district = district_aurora_energy
			}
		}
		else_if = {
			limit = { owner = { has_authority = auth_corporate } }
			while = {
				count = 1
				limit = { has_district = district_aurora_trade }
				remove_district = district_aurora_trade
			}
		}
		planet_event = { id = elec.15 days = 1825 }	
	}
	option = { name = elec.14.a }
}

# 40 Years - 9x modifier
planet_event = {
	id = elec.15
	title = elec.15.name
	desc = elec.15.desc
	picture = GFX_evt_star_red_elec
	show_sound = event_wind_ruins
	location = root
	is_triggered_only = yes
	immediate = {
		remove_modifier = elec_supernova_mod
		add_modifier = {
			modifier = elec_supernova_mod
			mult = 9
		}
		planet_event = { id = elec.16 days = 1825 }
		
	}
	option = { name = elec.15.a }
}

# 45 Years - 10x modifier + solar storm 3 + devastation + end is near msg
planet_event = {
	id = elec.16
	title = elec.16.name
	desc = elec.16.desc
	picture = GFX_evt_supernova
	show_sound = event_red_alert
	location = root
	is_triggered_only = yes
	immediate = {
		remove_modifier = elec_supernova_mod
		add_modifier = {
			modifier = elec_supernova_mod
			mult = 10
		}
		add_planet_devastation = 15
		planet_event = { id = elec.17 days = 365 random = 3285 }
	}
	option = { name = elec.16.a }
}

# End is near! Appears randomly after 360-3650 days.
# Gives 250-365 days time to leave.
planet_event = {
	id = elec.17
	title = elec.17.name
	desc = elec.17.desc
	picture = GFX_evt_star_red_elec
	show_sound = event_red_alert
	location = root
	is_triggered_only = yes
	immediate = {
		add_planet_devastation = 15		
		planet_event = { id = elec.18 days = 250 random = 365 }
	}
	option = { name = elec.17.a }
}

# Destroys capital + homesystem
planet_event = {
	id = elec.18
	title = elec.18.name
	desc = elec.18.desc
	picture = GFX_evt_supernova
	show_sound = event_screams
	location = root
	is_triggered_only = yes
	immediate = {
# Destroy capital
		remove_modifier = elec_supernova_mod
		every_owned_pop = { kill_pop = yes }
		destroy_colony = yes
		spawn_habitat_cracker_effect = yes
		remove_planet = yes
# Destroy homesystem
		solar_system = {
			set_star_flag = star_cracked
			set_star_class = sc_black_hole
			every_system_planet = {
				limit = { is_star = yes }
				create_ambient_object = {
					type = "star_explosion"
					play_animation_once = yes
					location = this
				}
				last_created_ambient_object = {
					set_location = {
						target = prev
						distance = 0
						angle = random
					}
				}
				change_pc = pc_black_hole
				remove_planet_flag = has_megastructure
			}
			every_system_planet = {
				limit = { is_star = yes }
				set_deposit = d_elec_supernova_star
			}
			every_system_planet = {
				limit = {
					is_star = no
					is_asteroid = no
					NOR = {
						is_planet_class = pc_broken
						is_planet_class = pc_shattered
						is_planet_class = pc_shattered_2
						is_planet_class = pc_shielded
						is_planet_class = pc_ringworld_habitable_damaged
						is_planet_class = pc_ringworld_tech_damaged
						is_planet_class = pc_ringworld_seam_damaged
						is_planet_class = pc_ringworld_habitable
					}
				}			
				if = {
					limit = { is_planet_class = pc_habitat }
					spawn_habitat_cracker_effect = yes
					remove_planet = yes
				}			
				if = {
					limit = { is_colony = yes }
					if = {
						limit = {
							exists = orbital_defence
						}
						destroy_fleet = orbital_defence
					}
					remove_all_buildings = yes
					destroy_colony = yes
					if = {
						limit = { is_planet_class = pc_habitat }
						spawn_habitat_cracker_effect = yes
						remove_planet = yes
					}
					else = {
						change_pc = pc_broken	#rl_elec_ignite_planets
					}
				}
			}
			every_system_planet = {
				limit = {
					exists = orbital_station
				}
				orbital_station = { dismantle = yes }
			}
			every_fleet_in_system = { set_mia = mia_return_home }
		}
		solar_system = {
			every_system_planet = {
				limit = { is_planet_class = pc_elec_aurora }
				remove_planet = yes
			}
		}
# Neighboring systems get hit too.
		solar_system = {	
			every_neighbor_system = {
				every_system_planet = {
					limit = { is_colony = yes }
					add_planet_devastation = 25
				}
				every_fleet_in_system = { set_mia = mia_return_home }
			}		
			every_neighbor_system = {
				every_system_planet = {
					limit = { is_colony = no is_star = no }
					random_list = {
						# Some planets get nothing, otherwise would be too much.
						10 = { add_deposit =	d_elec_supernova_star_neighbor_1 }
						10 = { }
						10 = { add_deposit =	d_elec_supernova_star_neighbor_2 }
						10 = { }
						10 = { add_deposit =	d_elec_supernova_star_neighbor_3 }
						10 = { }
						10 = { add_deposit =	d_elec_supernova_star_neighbor_4 }
						10 = { }
						10 = { add_deposit =	d_elec_supernova_star_neighbor_5 }
						10 = { }
						10 = { add_deposit =	d_elec_supernova_star_neighbor_6 }
						10 = { }
						10 = { add_deposit =	d_elec_supernova_star_neighbor_7 }
						10 = { }
						10 = { add_deposit =	d_elec_supernova_star_neighbor_8 }
						10 = { }
						10 = { add_deposit =	d_elec_supernova_star_neighbor_9 }
						10 = { }
						10 = { add_deposit =	d_elec_supernova_star_neighbor_10 }
						10 = { }
					}
				}
			}
		}
		solar_system = {
			every_system_ambient_object = {
				limit = { has_ambient_object_flag = elec_supernova_solarstorm }
				destroy_ambient_object = this
			}
		}
	}
	option = {
		name = elec.18.a
		custom_tooltip = elec.18.a.tooltip
		hidden_effect = {
			event_target:supernova_owner = {
				add_research_option = tech_dark_matter_deflector
				add_research_option = tech_dark_matter_power_core
				add_research_option = tech_dark_matter_propulsion
				add_modifier = { modifier = elec_supernova_unhappy_mod days = 3650 }
			}
		}
	}
	option = {
		name = elec.18.b
		custom_tooltip = elec.18.b.tooltip
		hidden_effect = {
			event_target:supernova_owner = {
				add_modifier = { modifier = elec_supernova_happy_mod }
			}
		}
	}
}

# Destroying planets or colonies with the Focus colossus.
# this/root = planet that have been fired upon
# from = fleet that fired
planet_event = {
	id = elec.19
	hide_window = yes
	is_triggered_only = yes
	immediate = {

# Sets default variable for planet.
		set_variable = { which = elec_target_pops value = 0 }

# Counts planet (ROOT) pops.
		every_owned_pop = { ROOT = { change_variable = { which = elec_target_pops value = 1 } } }

# Copies variable from (ROOT) to fleetowner (FROM).
		from.owner = { set_variable = { which = elec_target_pop_harvest value = ROOT.elec_target_pops } }

# Now both have the variable - only FROM.OWNER needs it now.
# FROM.OWNER receives unity dependent on ROOT pops.
		from.owner = {
			add_resource = {
				unity = 50
				mult = elec_target_pop_harvest
			}
		}

# Adds pops to the "Stellaris Killcounter"-mod (made by me).
		from.owner = {
			if = {
				limit = { has_event_chain = "ds_killcounter" }
				add_event_chain_counter = {
					event_chain = "ds_killcounter"
					counter = "ds_kk_pops_own_kills"
					amount = elec_target_pop_harvest
				}
			}
		}
		every_country = {
			limit = { has_event_chain = "ds_killcounter" }
			set_variable = { which = ds_kk_pop_val_global value = ROOT.elec_target_pops }
			add_event_chain_counter = {
				event_chain = "ds_killcounter"
				counter = "ds_kk_pops_global_losses"
				amount = ds_kk_pop_val_global
			}
		}

# FROM.OWNER receives 0.25 pops for each killed pops - spawn on capital.
# Resets variables for coming attacks - even on the planets in case they get "repaired" (mods/commands).
		from.owner = {
			multiply_variable = { which = elec_target_pop_harvest value = 0.25 }
			capital_scope = {
				set_variable = { which = elec_target_pop_harvest_cap value = FROM.OWNER.elec_target_pop_harvest }
				while = {
					count = elec_target_pop_harvest_cap
					create_pop = {
						species = prev
						ethos = prev
					}
				}
				set_variable = { which = elec_target_pop_harvest_cap value = 0 }
			}
			set_variable = { which = elec_target_pop_harvest value = 0 }			
		}
	set_variable = { which = elec_target_pops value = 0 }

# Typical "killed a colony" stuff.
		if = {
			limit = { has_observation_outpost = yes }
			observation_outpost_owner = { country_event = { id = planet_destruction.1000 days = 1 } }
		}
		if = {
			limit = {
				exists = owner
				owner = {
					NOT = { is_same_value = from.owner }
					OR = {
						is_country_type = default
						is_country_type = fallen_empire
						is_country_type = awakened_fallen_empire
					}
				}
			}
			# Generate threat
			add_threat = { who = from.owner amount = 3 }
			# modifier for allies + those upset by genocide
			every_country = {
				limit = {
					NOR = {
						is_same_value = from.owner
						is_same_value = root.owner
						AND = {
							has_federation = yes
							is_in_federation_with = from.owner
						}
					}
					OR = {
						has_communications = from.owner
						has_communications = root.owner
					}
					OR = {
						AND = {
							has_federation = yes
							is_in_federation_with = root.owner
						}
						has_ai_personality = awakened_fallen_empire_xenophile
						AND = {
							is_country_type = default
							OR = {
								is_egalitarian = yes
								is_xenophile = yes
								is_spiritualist = yes
							}
						}
					}
				}
				add_opinion_modifier = {
					modifier = opinion_swept_a_world
					who = from.owner
				}
			}
			# modifiers for victim
			owner = {
				add_opinion_modifier = {
					modifier = opinion_swept_my_world
					who = from.owner
				}
				if = {
					limit = { NOT = { has_ethic = ethic_gestalt_consciousness } }
					add_modifier = {
						modifier = colossus_victim
						days = 3650 # 10 years
					}
				}
			}
		}
		else_if = {
			limit = {
				exists = owner
				owner = { is_country_type = primitive }
			}
			# modifier for allies + those upset by genocide
			every_country = {
				limit = {
					NOT = { is_same_value = from.owner }
					OR = {
						has_communications = from.owner
						has_communications = root.owner
					}
					is_country_type = default
					NOR = {
						is_xenophobe = yes
						is_homicidal = yes
						is_xenophile = yes
					}
				}
				add_opinion_modifier = {
					modifier = opinion_swept_a_primitive_world
					who = from.owner
				}
			}
			every_country = {
				limit = {
					NOT = { is_same_value = from.owner }
					OR = {
						has_communications = from.owner
						has_communications = root.owner
					}
					OR = {
						has_ai_personality = awakened_fallen_empire_xenophile
						AND = {
							is_country_type = default
							is_xenophile = yes
						}
					}
				}
				add_opinion_modifier = {
					modifier = opinion_swept_a_primitive_world_phile
					who = from.owner
				}
			}
		}
		if = {
			limit = {
				is_planet_class = pc_habitat
			}
			spawn_habitat_neutron_effect = yes
		}
		else_if = {
			limit = {
				is_planet_class = pc_ringworld_habitable
			}
			spawn_ringworld_neutron_effect = yes
		}

# War exhaustion & destroys the colony (done as last for previous trigger checks).
		if = {
			limit = { exists = owner }
			owner = {
				if = {
					limit = { #isn't the case when the crisis bombards planets, for instance
						is_at_war_with = from.fleet.owner
					}
					add_static_war_exhaustion = {
						attacker = from.fleet.owner
						location = root
						value_for_planet_destruction = 1.0
					}
				}
			}
		}
		destroy_colony = yes
		clear_planet_modifiers = yes

# Destroys the entire planet if its a normal one. Adds a harsh modifier if artificial (habitats, ringworlds, modded planets like from Gigastructures + DarkSpace).
		if = {
			limit = { is_artificial = no }
			change_pc = pc_shattered
		}
		if = {
			limit = { is_artificial = yes }
			add_modifier = { modifier = elec_focusfire_mod years = 25 }
		}

	}
}

# Colony nuked with Neutron-Waves.
planet_event = {
	id = elec.20
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		has_orbital_bombardment_stance = elec_neutron
		habitable_planet = yes
	}
	immediate = {
		owner = {
			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 }
			}
			else = {
				country_event = { id = origin.3265 }
			}
		}
		from = { country_event = { id = action.83 } }
		change_pc = pc_nuked
		set_planet_flag = nuked_planet_anomalies_disabled
		remove_all_buildings = yes
		reroll_deposits = yes
		remove_modifier = "natural_beauty"
		remove_modifier = "atmospheric_aphrodisiac"
		remove_modifier = "atmospheric_hallucinogen"
		remove_modifier = "lush_planet"
		remove_modifier = "dangerous_wildlife"
		set_planet_flag = armageddon_nuked
	}
}

# Killing pops with Neutron-Waves generates energy.
planet_event = {
	id = elec.21
	hide_window = yes
	is_triggered_only = yes
	trigger = { has_orbital_bombardment_stance = elec_neutron }
	immediate = {
		from = {
			add_resource = { energy = 100 }
			add_resource = { unity = 50 }
		}
	}
}

# Extradimensionals-origin: Spawns the cluster outside of the galaxy.
country_event = {
	id = elec.100
	hide_window = yes
	is_triggered_only = yes
	trigger = { has_origin = origin_elec_extradimensionals }
	immediate = {
		capital_scope = {
			save_event_target_as = previous_home_planet
			solar_system = {
				save_global_event_target_as = electroid_wormhole_system
				save_event_target_as = previous_home_system
			}
			if = {
				limit = { exists = sector }
				sector = {
					leader = {
						unassign_leader = this
						save_event_target_as = electroids_gov
					}
				}
			}
			else = {
				root = {
					random_owned_leader = {
						limit = { leader_class = governor }
						save_event_target_as = electroids_gov
					}
				}
			}		
		}
		no_scope = {
			set_spawn_system_batch = begin
				random_rim_system = {
					spawn_system = {
						min_distance >= 50
						max_distance <= 75
						direction = rimwards
						initializer = elec_extradimensionals_init
						hyperlane = no
						effect = { save_event_target_as = elec_cluster_1 }
					}
				}
				event_target:elec_cluster_1 = {
					spawn_system = {
						min_distance >= 18
						max_distance <= 21
						min_orientation_angle = 104
						max_orientation_angle = 105
						initializer = elec_extradimensionals_2_init
						hyperlane = no
						effect = { save_event_target_as = elec_cluster_2 }
					}
				}
				event_target:elec_cluster_1 = {
					spawn_system = {
						min_distance >= 11
						max_distance <= 12
						min_orientation_angle = 124
						max_orientation_angle = 125
						initializer = elec_extradimensionals_3_init
						hyperlane = no
						effect = { save_event_target_as = elec_cluster_3 }
					}
				}
				event_target:elec_cluster_1 = {
					spawn_system = {
						min_distance >= 18
						max_distance <= 21
						min_orientation_angle = 149
						max_orientation_angle = 150
						initializer = elec_extradimensionals_4_init
						hyperlane = no
						effect = { save_event_target_as = elec_cluster_4 }
					}
				}
				# Add hyperlanes.
				event_target:elec_cluster_1 = {
					add_hyperlane = {
						from = event_target:elec_cluster_1
						to = event_target:elec_cluster_2
					}
				}
				event_target:elec_cluster_1 = {
					add_hyperlane = {
						from = event_target:elec_cluster_1
						to = event_target:elec_cluster_4
					}
				}
				event_target:elec_cluster_1 = {
					add_hyperlane = {
						from = event_target:elec_cluster_1
						to = event_target:elec_cluster_3
					}
				}
				event_target:elec_cluster_2 = {
					add_hyperlane = {
						from = event_target:elec_cluster_2
						to = event_target:elec_cluster_4
					}
				}
			set_spawn_system_batch = end
		}
		event_target:electroids_core_system = {
			set_star_flag = empire_cluster
			set_star_flag = empire_home_system
			# Adds a starbase
			create_starbase = {
				size = "starbase_starport"
				module = "shipyard"
				building = "crew_quarters"
				owner = root
				effect = { save_event_target_as = electroids_starbase }
			}
		}
		event_target:electroids_planet = {
			set_owner = root
			set_controller = root
			set_capital = yes
			set_sector_capital = yes
#			sector = { assign_leader = event_target:electroids_gov }
		}
		every_owned_fleet = {
			limit = {
				any_owned_ship = {
					OR = {
						is_ship_size = constructor
						is_ship_size = corvette
						is_ship_size = science
					}
				}
			}
			set_location = event_target:electroids_starbase
			if = {
				limit = { any_owned_ship = { is_ship_size = corvette } }
				set_home_base = event_target:electroids_starbase
			}
		}
		event_target:previous_home_planet = {
			every_owned_pop = {
				unemploy_pop = yes
				resettle_pop = { pop = this planet = event_target:electroids_planet } 
			}
			change_pc = pc_nuked
			destroy_colony = yes
			reroll_planet = yes
		}
		event_target:electroids_planet = {
			every_owned_pop = {
				unemploy_pop = yes
				clear_pop_category = yes
			}
			check_planet_employment = yes
		}
		event_target:previous_home_system = {
			every_fleet_in_system = { delete_fleet = this }
			remove_star_flag = empire_home_system
			remove_star_flag = empire_cluster
			every_neighbor_system = {
				remove_star_flag = empire_cluster
				every_neighbor_system = { remove_star_flag = empire_cluster }
			}			
		}
		owner_species = { set_species_homeworld = event_target:electroids_planet }
		every_owned_pop_species = { set_species_homeworld = event_target:electroids_planet }		
	}
	after = { country_event = { id = elec.200 }}
}

# Extradimensionals-origin: Capital buildings & districts.
country_event = {
	id = elec.200
	hide_window = yes
	is_triggered_only = yes
    immediate = {
# Basic districts
		if = {
			limit = { NOT = { has_authority = auth_corporate } }
			capital_scope = {
				set_variable = { which = elec_aurora_capital_var value = 0 }
				set_planet_flag = elec_aurora_capital
				clear_blockers = yes
				clear_deposits = yes
				remove_all_districts = yes
				add_deposit = d_electroids_start
				add_district = district_aurora_city
				add_district = district_aurora_energy
				add_district = district_aurora_energy
				add_district = district_aurora_energy
				add_district = district_aurora_industrial
				add_district = district_aurora_minerals
				add_district = district_aurora_minerals
				add_building = building_research_lab_1
			}
		}
		if = {
			limit = { has_authority = auth_corporate }
			capital_scope = {
				set_variable = { which = elec_aurora_capital_var value = 0 }
				set_planet_flag = elec_aurora_capital
				clear_blockers = yes
				clear_deposits = yes
				remove_all_districts = yes
				add_deposit = d_electroids_start
				add_district = district_aurora_city
				add_district = district_aurora_trade
				add_district = district_aurora_trade
				add_district = district_aurora_trade
				add_district = district_aurora_industrial
				add_district = district_aurora_minerals
				add_district = district_aurora_minerals
				add_building = building_research_lab_1
			}
		}
# To have the starting production in a stable state & counter bonuses of other origins (doesnt have any other economy modifier).
		if = {
			limit = { is_gestalt = no is_spiritualist = no }
			capital_scope = {
				while = {
					count = 2
					create_pop = {
						species = prev
						ethos = prev
					}
				}
				remove_building = building_deployment_post
				add_building = building_capital
				add_building = building_bureaucratic_1
				add_building = building_commercial_zone
				every_owned_pop = { clear_pop_category = yes }
				check_planet_employment = yes
				add_district = district_aurora_city
			}
		}
		if = {
			limit = { is_gestalt = no is_spiritualist = yes }
			capital_scope = {
				while = {
					count = 2
					create_pop = {
						species = prev
						ethos = prev
					}
				}
				remove_building = building_deployment_post
				add_building = building_capital
				add_building = building_temple
				add_building = building_temple
				every_owned_pop = { clear_pop_category = yes }
				check_planet_employment = yes
				add_district = district_aurora_city
			}
		}
		if = {
			limit = { is_gestalt = yes }
			capital_scope = {
				while = {
					count = 3
					create_pop = {
						species = prev
						ethos = prev
					}
				}
				remove_building = building_deployment_post
				add_building = building_elec_food_1
				add_building = building_hive_capital
				add_deposit = d_electroids_start
#				add_building = building_spawning_pool # Can not be assembled right now, uses a trait modifier instead.
				add_building = building_hive_node
				add_district = district_aurora_energy
				add_district = district_aurora_energy
				every_owned_pop = { clear_pop_category = yes }
				check_planet_employment = yes
			}
		}
		give_tech_no_error_effect = { MESSAGE = no TECH = tech_wormhole_stabilization }
		capital_scope = {
			set_variable = { which = elec_aurora_capital_var value = 0 }
			set_planet_flag = elec_aurora_capital
		}
		owner_main_species = { change_species_characteristics = { remove_trait = trait_pc_continental_preference } }
	}
}

# Tutorial situation-log
country_event = {
	id = elec.22
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		if = {
			limit = {
				OR = {
					has_origin = origin_elec_sun_empire
					has_origin = origin_elec_supernova
					has_origin = origin_elec_endless_horizon
					has_origin = origin_elec_extradimensionals
					has_origin = origin_elec_nebula_of_creation
				}
			}
			begin_event_chain = {
				event_chain = elec_tutorial_chain
				target = root
			}
		}
		if = {
			limit = {
				OR = {
					has_civic = civic_elec_starlifters
					has_civic = civic_elec_h_starlifters
				}
			}
			enable_special_project = {
				name = "ELEC_STELLAR_DEVOURERS"
				owner = root
			}
		}
	}
}

# free slot elec.23

# Nebula of Creation origin - adds the different pops.
country_event = {
	id = elec.24
	hide_window = yes
	is_triggered_only = yes
	trigger = { has_origin = origin_elec_nebula_of_creation }
	immediate = {
		while = {
			count = 16
			random_owned_pop = { kill_pop = yes }
		}
		capital_scope = {
			create_species = {
				name = "Zira"
				class = ELECTROID
				portrait = "electroid_1"
				#homeworld = this
				traits = {
					trait = trait_elec_electroid
					trait = trait_elec_extraterrestrial_physics
				}
				effect = {
					if = {
						limit = {
							root.owner = {
								OR = {
									has_ethic = ethic_fanatic_xenophobe
									has_ethic = ethic_xenophobe
								}
							}
						}
						set_citizenship_type = {
							country = root.owner
							type = citizenship_limited
						}
					}
				}
			}
			while = {
				count = 2
				create_pop = { species = last_created_species }
			}
			create_species = {
				name = "Areku"
				class = ELECTROID
				portrait = "electroid_2"
				#homeworld = this
				traits = {
					trait = trait_elec_electroid
					trait = trait_elec_natural_ionisation
				}
				effect = {
					if = {
						limit = {
							root.owner = {
								OR = {
									has_ethic = ethic_fanatic_xenophobe
									has_ethic = ethic_xenophobe
								}
							}
						}
						set_citizenship_type = {
							country = root.owner
							type = citizenship_limited
						}
					}
				}
			}
			while = {
				count = 2
				create_pop = { species = last_created_species }
			}
			create_species = {
				name = "Yuno"
				class = ELECTROID
				portrait = "electroid_3"
				#homeworld = this
				traits = {
					trait = trait_elec_electroid
					trait = trait_elec_unstable_element
				}
				effect = {
					if = {
						limit = {
							root.owner = {
								OR = {
									has_ethic = ethic_fanatic_xenophobe
									has_ethic = ethic_xenophobe
								}
							}
						}
						set_citizenship_type = {
							country = root.owner
							type = citizenship_limited
						}
					}
				}
			}
			while = {
				count = 2
				create_pop = { species = last_created_species }
			}
			create_species = {
				name = "Xiee"
				class = ELECTROID
				portrait = "electroid_4"
				#homeworld = this
				traits = {
					trait = trait_elec_electroid
					trait = trait_elec_dark_nucleus
				}
				effect = {
					if = {
						limit = {
							root.owner = {
								OR = {
									has_ethic = ethic_fanatic_xenophobe
									has_ethic = ethic_xenophobe
								}
							}
						}
						set_citizenship_type = {
							country = root.owner
							type = citizenship_limited
						}
					}
				}
			}
			while = {
				count = 2
				create_pop = { species = last_created_species }
			}
			create_species = {
				name = "Reijins"
				class = ELECTROID
				portrait = "electroid_5"
				#homeworld = this
				traits = {
					trait = trait_elec_electroid
					trait = trait_elec_optimal_conduits
				}
				effect = {
					if = {
						limit = {
							root.owner = {
								OR = {
									has_ethic = ethic_fanatic_xenophobe
									has_ethic = ethic_xenophobe
								}
							}
						}
						set_citizenship_type = {
							country = root.owner
							type = citizenship_limited
						}
					}
				}
			}
			while = {
				count = 2
				create_pop = { species = last_created_species }
			}
			create_species = {
				name = "Semira"
				class = ELECTROID
				portrait = "electroid_6"
				#homeworld = this
				traits = {
					trait = trait_elec_electroid
					trait = trait_elec_overcharging
				}
				effect = {
					if = {
						limit = {
							root.owner = {
								OR = {
									has_ethic = ethic_fanatic_xenophobe
									has_ethic = ethic_xenophobe
								}
							}
						}
						set_citizenship_type = {
							country = root.owner
							type = citizenship_limited
						}
					}
				}
			}
			while = {
				count = 2
				create_pop = { species = last_created_species }
			}
			create_species = {
				name = "Kera"
				class = ELECTROID
				portrait = "electroid_7"
				#homeworld = this
				traits = {
					trait = trait_elec_electroid
					trait = trait_elec_overheating
				}
				effect = {
					if = {
						limit = {
							root.owner = {
								OR = {
									has_ethic = ethic_fanatic_xenophobe
									has_ethic = ethic_xenophobe
								}
							}
						}
						set_citizenship_type = {
							country = root.owner
							type = citizenship_limited
						}
					}
				}
			}
			while = {
				count = 2
				create_pop = { species = last_created_species }
			}
		}
	}
}

# Civic Source of Ignition: rotates between the warm & cold cycle - 6 months.
# Starts with elec.3, delayed by 90 days.
# Adds the "warm" period.
country_event = {
	id = elec.25
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		remove_modifier = elec_source_of_ignition_cold
		add_modifier = { modifier = elec_source_of_ignition_warm }
		country_event = { id = elec.26 days = 180 }
	}
}

# Civic Soure of Ignition: rotates between the warm & cold cycle - 6 months.
# Adds the "cold" period.
country_event = {
	id = elec.26
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		remove_modifier = elec_source_of_ignition_warm
		add_modifier = { modifier = elec_source_of_ignition_cold }
		country_event = { id = elec.25 days = 180 }
	}
}

# Extradimensionals-origin: Informs the player to leave the cluster with the already available FTL-jumpdrive + a hyperlane will be created when reaching crisis level 4 or 5.
country_event = {
	id = elec.27
	title = "elec.27.name"
	desc = "elec.27.desc"
	show_sound = event_mystic_reveal
	picture = GFX_evt_black_hole
	is_triggered_only = yes
	trigger = { has_origin = origin_elec_extradimensionals }
	immediate = {}
	option = { name = OK }
}

# Electroid FE: receive more fleets when the crisis spawns, its their way of "awakening".
country_event = {
	id = elec.28
	hide_window = yes
	is_triggered_only = yes
	fire_only_once = yes
	trigger = {
		is_country_type = fallen_empire
		has_country_flag = electroid_fe_country
	}
	immediate = {
		country_event = { id = elec.280 days = 18000 }
#		set_global_flag = elec_fe_awakened # TESTING ONLY
	}
}

country_event = {
	id = elec.280
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		is_country_type = fallen_empire
		has_country_flag = electroid_fe_country
	}
	immediate = {
		if = {
			limit = {
				OR = {
					has_global_flag = ai_invasion_ongoing
					has_global_flag = ongoing_prethoryn_invasion
#					has_global_flag = extradimensional_invasion_happened # This flag gets never removed, so it would spam the fleets even after the crisis got defeated. Somehow only the Unbidden don't have a dynamic flag.
					any_country = { has_country_flag = unbidden }
					has_global_flag = elec_fe_awakened
				}
			}
			create_elec_fe_fleet = yes # additional_crisis_strength
			country_event = { id = elec.280 days = 1800 }
		}
		else = { country_event = { id = elec.280 days = 1800 } }
	}
}

country_event = {
	id = elec.29
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		OR = {
			has_civic = civic_elec_source_of_ignition
			has_civic = civic_elec_h_source_of_ignition
		}
	}
	immediate = {
		add_modifier = { modifier = elec_source_of_ignition_cold }
		country_event = { id = elec.25 days = 90 } # 3 months because the game starts with 01.01.2200 - "Warm-Cycle" is in the mid of the year.
	}
}

# Finishing an Artificial Quasar.
country_event = {
	id = elec.30
	title = elec.30.name
	desc = elec.30.desc
	picture = GFX_evt_star_neutron
	show_sound = event_spy_orb_build_complete
	is_triggered_only = yes
	immediate = {}
	option = { name = CONFIRM }
}

# Sends ship MIA when player tries to destroy a Quasar planet.
planet_event = {
	id = elec.31
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		OR = {
			has_planet_flag = elec_artificial_quasar_star
			has_planet_flag = elec_artificial_quasar
		}
	}
	immediate = {
		from = { set_mia = mia_return_home }
		from.owner = { country_event = { id= elec.33 } }
	}
}
planet_event = {
	id = elec.32
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		OR = {
			has_planet_flag = elec_artificial_quasar_star
			has_planet_flag = elec_artificial_quasar
		}
	}
	immediate = {
		from = { set_mia = mia_return_home }
		from.owner = { country_event = { id= elec.33 } }
	}
}
# Informs the player that the Quasar can not be destroyed.
country_event = {
	id = elec.33
	title = elec.33.name
	desc = elec.33.desc
	picture = GFX_evt_small_space_battle
	show_sound = event_red_alert
	is_triggered_only = yes
	immediate = {}
	option = { name = UNFORTUNATE }
}

# Symbiosis Civic - Ship gets lost, adding to internal death counter.
country_event = {
	id = elec.34
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		OR = {
			has_civic = civic_elec_symbiosis
			has_civic = civic_elec_h_symbiosis
		}
		fromfrom = { has_component = ELEC_SYMBIOSIS }
	}
	immediate = {
		fromfrom = {
			set_variable = { which = elec_symbiosis_ship_var value = 0 }
			export_trigger_value_to_variable = {
				trigger = ship_size_multiplier
				variable = elec_symbiosis_ship_var
			}
			multiply_variable = { which = elec_symbiosis_ship_var value = 0.01 }
			owner = { change_variable = { which = elec_symbiosis_var value = prev.elec_symbiosis_ship_var } }
		}
		if = {
			limit = { check_variable = { which = elec_symbiosis_var value >= 5 } }
			random_owned_pop = { kill_pop = yes }
			set_variable = { which = elec_symbiosis_var value = 0 } # Not accurate, but funnier/easier for the player.
		}
	}
}

# Origin Empire of the Sun (AI) via on_initialize_advanced_colony + on_become_advanced_empire
# Not used right now.
country_event = {
	id = elec.3000
	hide_window = yes
	is_triggered_only = yes
	trigger = { has_origin = origin_elec_sun_empire_ai }
	immediate = {
	owner_species = { save_event_target_as = old_owner_species }
		create_species = {
			name = random
			class = ELECTROID
			portrait = "electroid_1"
			traits = {
				trait = trait_elec_electroid
				trait = trait_elec_overcharging
			}
			effect = {
				if = {
					limit = {
						root.owner = {
							OR = {
								has_ethic = ethic_fanatic_xenophobe
								has_ethic = ethic_xenophobe
							}
						}
					}
					set_citizenship_type = {
						country = root.owner
						type = citizenship_limited
					}
				}
			}
		}
		capital_scope = {
			clear_blockers = yes
			clear_deposits = yes
			remove_all_districts = yes
			add_deposit = d_electroids_start
			add_district = district_aurora_city
			add_district = district_aurora_energy
			add_district = district_aurora_energy
			add_district = district_aurora_industrial
			add_district = district_aurora_minerals
			add_district = district_aurora_minerals
		}
		if = {
			limit = { is_gestalt = no }
			capital_scope = {
				while = {
					count = 1
					create_pop = {
						species = prev
						ethos = prev
					}
				}
				add_district = district_aurora_city
				every_owned_pop = { clear_pop_category = yes }
				check_planet_employment = yes
			}
		}
		if = {
			limit = { is_gestalt = yes }
			capital_scope = {
				while = {
					count = 2
					create_pop = {
						species = prev
						ethos = prev
					}
				}
				add_building = building_elec_food_1
				add_district = district_aurora_energy
				add_district = district_aurora_energy
				every_owned_pop = { clear_pop_category = yes }
				check_planet_employment = yes
			}
		}
		country_event = { id = elec.31 days = 5 } # So the stuff of the Vanilla events have enough time to work.
	}
}

# Origin Empire of the Sun (AI) - Advanced AI start fix
country_event = {
	id = elec.3100
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		if = {
			limit = { has_country_flag = is_advanced_start_AI }
	# effect
		}
		else = {}
	}
}