#################################
#
# Leviathan & Enclave Events
# by Maximilian Olbers, Niclas Karlsson,
# Rufus Tronde & Henrik Thyrwall
#
#################################

namespace = leviathans

### STELLARITE ###
# Stellarite spawns as an empire enters its system
fleet_event = {
	id = leviathans.999
	title = leviathans.999.name
	desc = leviathans.999.desc
	picture = GFX_evt_star_yellow
	show_sound = event_radio_chatter
	location = root

	is_triggered_only = yes

	trigger = {
		from = {
			has_star_flag = guardians_stellarite_system
			NOT = {
				has_star_flag = guardians_stellarite_about_to_spawn
			}
		}
	}

	immediate = {
		from = {
			set_star_flag = guardians_stellarite_about_to_spawn
			save_global_event_target_as = stellarite_system
			set_star_flag = stellarite_present
			every_system_planet = {
				limit = {
					is_star = no
					is_asteroid = no
				}
				add_modifier = {
					modifier = stellarite_low_temp
					days = -1
				}
			}
		}
	}

	option = {
		name = leviathans.999.a
		hidden_effect = {
			owner = {
				country_event = { id = leviathans.1000 days = 7 }
			}
		}
	}
}
# Stellarite spawns
country_event = {
	id = leviathans.1000
	title = leviathans.1000.name
	desc = leviathans.1000.desc
	picture = GFX_evt_stellarites
	show_sound = event_radio_chatter
	location = fromfrom

	is_triggered_only = yes

	trigger = {
		NOT = { has_country_flag = guardians_stellarite_encountered }
		any_system = {
			has_star_flag = guardians_stellarite_about_to_spawn
			has_star_flag = guardians_stellarite_system
		}
	}

	option = {
		name = BATTLESTATIONS
		hidden_effect = {
			set_country_flag = guardians_stellarite_encountered
			country_event = { id = story.8 days = 15 }
			event_target:stellarite_system = {
				random_system_planet = {
					limit = {
						is_star = yes
					}
					change_planet_size = -5
					spawn_sun_temperature_effect = yes
					create_country = {
						name = "NAME_Stellarite"
						type = guardian_stellarite
						flag = {
							icon = {
								category = "zoological"
								file = "flag_zoological_1.dds"
							}
							background= {
								category = "backgrounds"
								file = "00_solid.dds"
							}
							colors={
								"black"
								"black"
								"null"
								"null"
							}
						}
					}
					last_created_country = {
						save_global_event_target_as = stellarite_country
						set_country_flag = guardian_stellarite_country
						every_country = {
							limit = { is_country_type = enclave }
							set_faction_hostility = {
								target = prev
								set_hostile = no
								set_neutral = yes
							}
						}
						create_fleet = {
							name = "NAME_Stellarite_Devourer"
							settings = {
								spawn_debris = no
								is_boss = yes
							}
							effect = {
								set_owner = event_target:stellarite_country
								create_ship = {
									name = "NAME_Stellar_Devourer"
									design = "NAME_Stellarite"
									effect = {
										set_ship_flag = stellarite_ship
										save_global_event_target_as = stellarite
									}
								}
								set_location = PREVPREV
								set_fleet_stance = aggressive
								set_aggro_range_measure_from = self
								set_aggro_range = 100
							}
						}
					}
				}
			}
			establish_communications_no_message = event_target:stellarite_country
		}
	}
}

# Players who haven't encountered the stellarite gets a message
fleet_event = {
	id = leviathans.1001
	title = leviathans.1000.name
	desc = {
		trigger = {
			solar_system = {
				has_star_flag = guardians_stellarite_system
			}
		}
		text = leviathans.1001.desc.stellarite_system
	}
	desc = {
		trigger = {
			solar_system = {
				NOT = { has_star_flag = guardians_stellarite_system }
			}
		}
		text = leviathans.1001.desc
	}
	picture = GFX_evt_stellarites
	show_sound = event_radio_chatter
	location = root

	is_triggered_only = yes

	trigger = {
		owner = {
			is_ai = no
			NOT = { has_country_flag = guardians_stellarite_encountered }
		}
		from = {
			any_fleet_in_system = {
				exists = owner
				owner = {
					is_country_type = guardian_stellarite
					has_country_flag = guardian_stellarite_country
				}
			}
		}
	}

	immediate = {
		owner = {
			country_event = { id = story.8 days = 15 }
			set_country_flag = guardians_stellarite_encountered
			establish_communications_no_message = event_target:stellarite_country
		}
	}

	option = {
		name = DISCONCERTING
	}
}

# Stellarite enter system - temperature change
fleet_event = {
	id = leviathans.1005
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = owner
		owner = {
			is_country_type = guardian_stellarite
			has_country_flag = guardian_stellarite_country
		}
		solar_system = {
			NOT = { has_star_flag = guardians_stellarite_system }
		}
	}

	immediate = {
		if = {
			limit = {
				any_system = {
					has_star_flag = stellarite_present
					NOT = {
						any_fleet_in_system = {
							owner = {
								has_country_flag = guardian_stellarite_country
							}
						}
					}
				}
			}
			random_system = {
				limit = {
					has_star_flag = stellarite_present
				}
				remove_star_flag = stellarite_present
				every_system_planet = {
					limit = {
						has_modifier = stellarite_high_temp
					}
					remove_modifier = stellarite_high_temp
				}
			}
		}
		solar_system = {
			set_star_flag = stellarite_present
			every_system_planet = {
				limit = {
					is_star = no
					is_asteroid = no
				}
				add_modifier = {
					modifier = stellarite_high_temp
					days = -1
				}
			}
		}
	}
}
# Stellarite behaviour - chase aggressor setup - from destroyed fleet
country_event = {
	id = leviathans.1009
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		fromfrom = {
			owner = {
				is_country_type = guardian_stellarite
			}
			any_controlled_ship = {
				has_ship_flag = stellarite_ship
			}
			has_hp_percentage < 0.995
			has_hp_percentage > 0.33
		}
	}

	immediate = {
		from = {
			save_event_target_as = attacker_country
			set_timed_country_flag = { flag = stellarite_in_pursuit days = 90 } #set so the stellarite finds them
		}
		fromfrom = {
			clear_fleet_actions = this
			save_event_target_as = stellarite_fleet
			set_timed_fleet_flag = { flag = stellarite_in_pursuit days = 90 } #set so the stellarite knows when to stop chase
			queue_actions = {
				repeat = {
					while = {
						id = leviathans.1009.trigger.1
						has_hp_percentage > 0.33
						has_fleet_flag = stellarite_in_pursuit
					}
					find_closest_system = {
						trigger = {
							id = leviathans.1009.trigger.2
							exists = space_owner
							space_owner = { has_country_flag = stellarite_in_pursuit }
							any_system_planet = {
								OR = {
									has_mining_station = yes
									has_research_station = yes
								}
								is_owned_by = event_target:attacker_country
							}
						}
						found_system = {
							move_to = this
							find_closest_planet = {
								trigger = {
									id = leviathans.1009.trigger.3
									OR = {
										has_mining_station = yes
										has_research_station = yes
									}
									is_owned_by = event_target:attacker_country
								}
								found_planet = {
									move_to = this
									effect = {
										id = leviathans.1009.effect.3
										event_target:stellarite_fleet = {
											fleet_event = { id = leviathans.1011 }
											owner = { country_event = { id = leviathans.1015 } }
										}
									}
								}
								failed = {
									effect = {
										id = leviathans.1009.effect.2
										event_target:stellarite_fleet = {
											set_timed_fleet_flag = { flag = returning_home days = 7 }
											owner = { country_event = { id = leviathans.1015 } }
										}
									}
								}
							}
						}
						failed = {
							effect = {
								id = leviathans.1009.effect.1
								event_target:stellarite_fleet = {
									set_timed_fleet_flag = { flag = returning_home days = 7 }
									owner = { country_event = { id = leviathans.1015 } }
								}
							}
						}
					}
				}
			}
		}
	}
}
# Stellarite behaviour - chase aggressor setup - from emergency ftl
fleet_event = {
	id = leviathans.1010
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_combatant_fleet = {
			owner = {
				is_country_type = guardian_stellarite
			}
			any_controlled_ship = {
				has_ship_flag = stellarite_ship
			}
			has_hp_percentage < 0.995
			has_hp_percentage > 0.33
		}
	}

	immediate = {
		owner = {
			save_event_target_as = attacker_country
			set_timed_country_flag = { flag = stellarite_in_pursuit days = 90 } #set so the stellarite finds them
		}
		random_combatant_fleet = {
			limit = {
				any_controlled_ship = {
					has_ship_flag = stellarite_ship
				}
			}
			clear_fleet_actions = this
			save_event_target_as = stellarite_fleet
			set_timed_fleet_flag = { flag = stellarite_in_pursuit days = 90 } #set so the stellarite knows when to stop chase
			queue_actions = {
				repeat = {
					while = {
						id = leviathans.1010.trigger.1
						has_hp_percentage > 0.33
						has_fleet_flag = stellarite_in_pursuit
					}
					find_closest_system = {
						trigger = {
							id = leviathans.1010.trigger.2
							exists = space_owner
							space_owner = { has_country_flag = stellarite_in_pursuit }
							any_system_planet = {
								OR = {
									has_mining_station = yes
									has_research_station = yes
								}
								is_owned_by = event_target:attacker_country
							}
						}
						found_system = {
							move_to = this
							find_closest_planet = {
								trigger = {
									id = leviathans.1010.trigger.3
									OR = {
										has_mining_station = yes
										has_research_station = yes
									}
									is_owned_by = event_target:attacker_country
								}
								found_planet = {
									move_to = this
									effect = {
										id = leviathans.1010.effect.3
										event_target:stellarite_fleet = {
											fleet_event = { id = leviathans.1011 }
											owner = { country_event = { id = leviathans.1015 } }
										}
									}
								}
								failed = {
									effect = {
										id = leviathans.1010.effect.2
										event_target:stellarite_fleet = {
											set_timed_fleet_flag = { flag = returning_home days = 7 }
											owner = { country_event = { id = leviathans.1015 } }
										}
									}
								}
							}
						}
						failed = {
							effect = {
								id = leviathans.1010.effect.1
								event_target:stellarite_fleet = {
									set_timed_fleet_flag = { flag = returning_home days = 7 }
									owner = { country_event = { id = leviathans.1015 } }
								}
							}
						}
					}
				}
			}
		}
	}
}

# Stellarite behaviour - chase aggressor continuation
fleet_event = {
	id = leviathans.1011
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		owner = {
			is_country_type = guardian_stellarite
		}
		has_fleet_flag = stellarite_in_pursuit
		has_hp_percentage < 0.995
		has_hp_percentage > 0.33
	}

	immediate = {
		clear_fleet_actions = this
		save_event_target_as = stellarite_fleet
		queue_actions = {
			repeat = {
				while = {
					id = leviathans.1011.trigger.1
					has_hp_percentage > 0.33
					has_fleet_flag = stellarite_in_pursuit
				}
				find_closest_system = {
					trigger = {
						id = leviathans.1011.trigger.2
						exists = space_owner
						space_owner = { has_country_flag = stellarite_in_pursuit }
						any_system_planet = {
							OR = {
								has_mining_station = yes
								has_research_station = yes
							}
							is_owned_by = event_target:attacker_country
						}
					}
					found_system = {
						move_to = this
						find_closest_planet = {
							trigger = {
								id = leviathans.1011.trigger.3
								OR = {
									has_mining_station = yes
									has_research_station = yes
								}
								is_owned_by = event_target:attacker_country
							}
							found_planet = {
								move_to = this
								effect = {
									id = leviathans.1011.effect.3
									event_target:stellarite_fleet = {
										fleet_event = { id = leviathans.1011 }
										owner = { country_event = { id = leviathans.1015 } }
									}
								}
							}
							failed = {
								effect = {
									id = leviathans.1011.effect.2
									event_target:stellarite_fleet = {
										set_timed_fleet_flag = { flag = returning_home days = 7 }
										owner = { country_event = { id = leviathans.1015 } }
									}
								}
							}
						}
					}
					failed = {
						effect = {
							id = leviathans.1011.effect.1
							event_target:stellarite_fleet = {
								set_timed_fleet_flag = { flag = returning_home days = 7 }
								owner = { country_event = { id = leviathans.1015 } }
							}
						}
					}
				}
			}
		}
	}
}
# Stellarite behaviour - chase aggressor continuation from combat
country_event = {
	id = leviathans.1012
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_country_type = guardian_stellarite
		any_controlled_ship = {
			has_ship_flag = stellarite_ship
			fleet = {
				has_hp_percentage < 0.995
				has_hp_percentage > 0.33
			}
		}
	}

	immediate = {
		from = {
			if = {
				limit = {
					NOT = { has_country_flag = stellarite_in_pursuit }
				}
				set_timed_country_flag = { flag = stellarite_in_pursuit days = 90 }
			}
		}
		fromfrom = {
			set_timed_fleet_flag = { flag = stellarite_in_pursuit days = 90 }
			save_event_target_as = stellarite_fleet
			clear_fleet_actions = this
			queue_actions = {
				repeat = {
					while = {
						id = leviathans.1012.trigger.1
						has_hp_percentage > 0.33
						has_fleet_flag = stellarite_in_pursuit
					}
					find_closest_system = {
						trigger = {
							id = leviathans.1012.trigger.2
							exists = space_owner
							space_owner = { has_country_flag = stellarite_in_pursuit }
							any_system_planet = {
								OR = {
									has_mining_station = yes
									has_research_station = yes
								}
								is_owned_by = event_target:attacker_country
							}
						}
						found_system = {
							move_to = this
							find_closest_planet = {
								trigger = {
									id = leviathans.1012.trigger.3
									OR = {
										has_mining_station = yes
										has_research_station = yes
									}
									is_owned_by = event_target:attacker_country
								}
								found_planet = {
									move_to = this
									effect = {
										id = leviathans.1012.effect.3
										event_target:stellarite_fleet = {
											fleet_event = { id = leviathans.1011 }
											owner = { country_event = { id = leviathans.1015 } }
										}
									}
								}
								failed = {
									effect = {
										id = leviathans.1012.effect.2
										event_target:stellarite_fleet = {
											set_timed_fleet_flag = { flag = returning_home days = 7 }
											owner = { country_event = { id = leviathans.1015 } }
										}
									}
								}
							}
						}
						failed = {
							effect = {
								id = leviathans.1012.effect.1
								event_target:stellarite_fleet = {
									set_timed_fleet_flag = { flag = returning_home days = 7 }
									owner = { country_event = { id = leviathans.1015 } }
								}
							}
						}
					}
				}
			}
		}
	}
}

# Stellarite behaviour - chase aggressor setup
fleet_event = {
	id = leviathans.1013
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_combatant_fleet = {
			any_controlled_ship = {
				has_ship_flag = stellarite_ship
			}
			has_hp_percentage < 0.995
			has_hp_percentage > 0.33
		}
	}

	immediate = {
		random_combatant_fleet = {
			owner = { country_event = { id = leviathans.1015 } }
		}
	}
}

# Stellarite behavior - chase aggressor setup
fleet_event = {
	id = leviathans.1014
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_combatant_fleet = {
			any_controlled_ship = {
				has_ship_flag = stellarite_ship
			}
			has_hp_percentage < 0.995
			has_hp_percentage > 0.33
		}
	}

	immediate = {
		random_combatant_fleet = {
			limit = {
				any_controlled_ship = {
					has_ship_flag = stellarite_ship
				}
			}
			fleet_event = { id = leviathans.1011 }
		}
	}
}
# Stellarite behavior - return home if bored with weak opponent
fleet_event = {
	id = leviathans.1020
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_combatant_fleet = {
			any_controlled_ship = {
				has_ship_flag = stellarite_ship
			}
			has_hp_percentage > 0.995
			has_hp_percentage < 0.33
		}
	}

	immediate = {
		random_combatant_fleet = {
			limit = {
				any_controlled_ship = {
					has_ship_flag = stellarite_ship
				}
			}
			owner = { country_event = { id = leviathans.1015 } }
		}
	}
}

# Stellarite behavior - return home to feed
country_event = {
	id = leviathans.1015
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_controlled_fleet = {
			any_controlled_ship = { has_ship_flag = stellarite_ship }
			OR = {
				has_hp_percentage < 0.33
				has_fleet_flag = returning_home
			}
		}
	}

	immediate = {
		random_controlled_fleet = {
			limit = {
				any_controlled_ship = { has_ship_flag = stellarite_ship }
			}
			clear_fleet_actions = this
			queue_actions = {
				repeat = {
					max_iterations = 1
					find_random_system = {
						trigger = {
							id = leviathans.1004.trigger.1
							has_star_flag = guardians_stellarite_system
						}
						found_system = {
							move_to = this
							find_random_planet = {
								trigger = {
									id = leviathans.1004.trigger.2
									is_star = yes
								}
								found_planet = {
									orbit_planet = this
								}
							}
						}
					}
					repeat = {
						while = {
							id = leviathans.1004.trigger.3
							has_hp_percentage < 1.0
						}
						wait = {
							duration = 30
						}
						effect = {
							id = leviathans.1004.trigger.4
							repair_percentage = 0.075
						}
					}
				}
			}
		}
	}
}

# Stellarite dies and creates special project
country_event = {
	id = leviathans.1016
	title = leviathans.1000.name
	desc = {
		text = leviathans.1016.desc
		trigger = { NOT = { has_ethic = ethic_gestalt_consciousness } }
	}
	desc = {
		text = leviathans.1016.desc.gesta
		trigger = { has_ethic = ethic_gestalt_consciousness }
	}
	picture = GFX_evt_stellarites
	show_sound = event_radio_chatter
	location = event_target:stellarite_project_object
	trackable = yes

	is_triggered_only = yes

	trigger = {
		is_country_type = default
		from = { is_country_type = guardian_stellarite }
		fromfromfrom = {
			has_ship_flag = stellarite_ship
			solar_system = {
				has_star_flag = stellarite_present
				NOT = {
					has_star_flag = stellarite_slain
				}
			}
		}
	}

	immediate = {
		fromfromfrom = {
			solar_system = {
				save_event_target_as = slain_guardian_system
			}
			create_ambient_object = {
				type = stellarite_object
				location = this
			}
		}
		every_country = {
			limit = {
				is_country_type = default
				has_event_chain = curator_poi_chain
				event_target:slain_guardian_system = {
					is_point_of_interest = {
						id = curator_poi_stellarite
						event_chain = curator_poi_chain
						owner = prev
					}
				}
			}
			remove_point_of_interest = curator_poi_stellarite
			end_curator_chain = yes
		}
		last_created_ambient_object = {
			save_event_target_as = stellarite_project_object
			set_ambient_object_flag = stellarite_project_object
			solar_system = {
				set_star_flag = stellarite_slain
			}
		}

		random_system = {
			limit = {
				has_star_flag = stellarite_present
			}
			every_system_planet = {
				limit = {
					has_modifier = stellarite_high_temp
				}
				remove_modifier = stellarite_high_temp
			}
		}
	}

	option = {
		name = leviathans.1016.a
		#hidden_effect = {
			event_target:stellarite_project_object = {
				enable_special_project = {
					name = "STELLARITE_DEAD_PROJECT"
					location = this
					owner = root
				}
			}
		#}
	}
	#option = {
	#	name = leviathans.1016.b
	#	trigger = {
	#		OR = {
	#			has_ethic = ethic_militarist
	#			has_ethic = ethic_fanatic_militarist
	#			has_ethic = ethic_xenophobe
	#			has_ethic = ethic_fanatic_xenophobe
	#		}
	#	}
	#	add_resource = { influence = 500 }
	#	capital_scope = {
	#		add_deposit = d_stellarite_trophy
	#	}
	#	hidden_effect = {
	#		set_country_flag = stellarite_trophy_allowed
	#		random_ambient_object = {
	#			limit = {
	#				has_ambient_object_flag = stellarite_project_object
	#			}
	#			destroy_ambient_object = this
	#		}
	#	}
	#}

	option = {
		name = leviathans.1016.c
		if = {
			limit = { num_owned_planets > 0 }
			generate_parade_city = yes
			start_situation = {
				type = leviathan_celebration_opportunity
				target = event_target:parade_city
				effect = {
					set_situation_flag = celebration_stellar_devourer
					set_situation_flag = standard_unity_reward
				}
			}
		}
	}
	after = {
		hidden_effect = {
			random_system = {
				limit = {
					has_star_flag = stellarite_present
				}
				remove_star_flag = stellarite_present
			}
		}
	}
}

# Stellarite sun project to increase temperature
ship_event = {
	id = leviathans.1017
	title = leviathans.1000.name
	desc = {
		text = leviathans.1017.desc
		trigger = {
			NOT = { owner = { has_ethic = ethic_gestalt_consciousness } }
		}
	}
	desc = {
		text = leviathans.1017.desc.gesta
		trigger = {
			owner = { has_ethic = ethic_gestalt_consciousness }
		}
	}
	picture = GFX_evt_star_yellow
	show_sound = event_radio_chatter
	location = root
	trackable = yes

	is_triggered_only = yes

	immediate = {
		solar_system = {
			random_system_ambient_object = {
				limit = { has_ambient_object_flag = stellarite_project_object }
				destroy_ambient_object = this
			}
		}
		random_system = {
			limit = {
				has_star_flag = guardians_stellarite_system
			}
			save_event_target_as = guardians_stellarite_home_system
		}
	}

	option = {
		name = leviathans.1017.a
		random_system = {
			limit = {
				has_star_flag = guardians_stellarite_system
			}
			random_system_planet = {
				limit = {
					is_star = yes
				}
				enable_special_project = {
					name = "STELLARITE_STAR_PROJECT"
					location = this
					owner = root.owner
				}
			}
		}
	}
	option = {
		name = leviathans.1017.b
		owner = {
			add_monthly_resource_mult = {
				resource = energy
				value = @tier4materialreward
				min = @tier4materialmin
				max = @tier4materialmax
			}
			add_monthly_resource_mult = {
				resource = minerals
				value = @tier2materialreward
				min = @tier2materialmin
				max = @tier2materialmax
			}
			add_monthly_resource_mult = {
				resource = influence
				value = @tier2influencereward
				min = @tier2influencemin
				max = @tier2influencemax
			}
		}
	}
}

# Stellarite system is updated and Gaia world unlocks
ship_event = {
	id = leviathans.1018
	title = leviathans.1000.name
	desc = {
		trigger = {
			hidden:owner = {
				switch = {
					trigger = has_country_flag
					stellarite_project_success = { text = leviathans.1018.desc }
					default = { text = leviathans.1018.alt.desc }
				}
			}
		}
	}
	picture = GFX_evt_star_yellow
	show_sound = event_radio_chatter
	location = root

	is_triggered_only = yes

	immediate = {
		random_system = {
			limit = {
				has_star_flag = guardians_stellarite_system
			}
			save_event_target_as = guardians_stellarite_home_system
			random_system_planet = {
				limit = {
					is_planet_class = pc_frozen
				}
				save_event_target_as = stellarite_rich_planet
			}
		}
		random_list = {
			70 = { owner = { set_country_flag = stellarite_project_success } }
			30 = {
				root = {
					kill_leader = { class = scientist show_notification = no }
					fleet = {
						destroy_fleet = this
					}
				}
			}
		}
	}

	option = {
		name = EXCELLENT
		custom_tooltip = leviathans.1018.c.tooltip
		trigger = {
			owner = {
				has_country_flag = stellarite_project_success
			}
		}
		hidden_effect = {
			owner = { remove_country_flag = stellarite_project_success }
			random_system = {
				limit = {
					has_star_flag = guardians_stellarite_system
				}
				random_system_planet = {
					limit = {
						is_star = yes
					}
					change_planet_size = 12
					spawn_sun_temperature_effect = yes
				}
				every_system_planet = {
					limit = {
						is_star = no
						is_asteroid = no
					}
					spawn_planet_temperature_effect = yes
					remove_modifier = stellarite_low_temp
					if = {
						limit = {
							is_planet_class = pc_frozen
							NOT = { has_planet_flag = stellarite_terraformed }
							solar_system = {
								NOT = { has_star_flag = gaia_planet_gained }
							}
						}
						change_pc = pc_gaia
						set_planet_flag = stellarite_terraformed
						reroll_planet = yes
						solar_system = { set_star_flag = gaia_planet_gained }
					}
					else_if = {
						limit = {
							is_planet_class = pc_frozen
							NOT = { has_planet_flag = stellarite_terraformed }
							solar_system = {
								has_star_flag = gaia_planet_gained
							}
						}
						increase_planet_temperature = yes
						set_planet_flag = stellarite_terraformed
					}
					else = {
						increase_planet_temperature = yes
						set_planet_flag = stellarite_terraformed
					}
				}
			}
		}
	}
	option = {
		name = UNFORTUNATE
		custom_tooltip = leviathans.1018.d.tooltip
		trigger = {
			owner = {
				NOT = { has_country_flag = stellarite_project_success }
			}
		}
		hidden_effect = {
			random_system = {
				limit = {
					has_star_flag = guardians_stellarite_system
				}
				random_system_planet = {
					limit = {
						is_star = yes
					}
					change_planet_size = 8
					carrier_event = { id = leviathans.1019 }
				}
				every_system_planet = {
					limit = {
						is_star = no
						is_asteroid = no
					}
					spawn_planet_temperature_effect = yes
					remove_modifier = stellarite_low_temp
					if = {
						limit = {
							is_planet_class = pc_frozen
							NOT = { has_planet_flag = stellarite_terraformed }
						}
						change_pc = pc_tundra
						set_planet_flag = stellarite_terraformed
						reroll_planet = yes
					}
					else_if = {
						limit = {
							is_planet_class = pc_frozen
							NOT = { has_planet_flag = stellarite_terraformed }
							solar_system = {
								has_star_flag = gaia_planet_gained
							}
						}
						increase_planet_temperature = yes
						set_planet_flag = stellarite_terraformed
						reroll_planet = yes
					}
					else = {
						increase_planet_temperature = yes
						set_planet_flag = stellarite_terraformed
						reroll_planet = yes
					}
				}
			}
		}
	}
}

carrier_event = {
	id = leviathans.1019
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		spawn_sun_temperature_effect = yes
	}
}

### TECHNOSPHERE ###
#Encountering Technosphere for the first time
fleet_event = {
	id = leviathans.1030
	title = leviathans.1030.name
	desc = {
		trigger = {
			hidden:from = {
				switch = {
					trigger = has_star_flag
					guardians_technosphere_system = { text = leviathans.1030.desc }
					default = { text = leviathans.1030.desc.alt }
				}
			}
		}
	}
	picture = GFX_evt_technosphere
	show_sound = event_yellow_alert
	location = from

	is_triggered_only = yes

	trigger = {
		from = {
			any_fleet_in_system = {
				exists = owner
				owner = {
					is_country_type = guardian_sphere
				}
			}
		}
		owner = {
			is_ai = no
			NOR = {
				has_country_flag = guardians_technosphere_encountered
				has_country_flag = worm_events_available
			}
		}
	}

	immediate = {
		owner = {
			country_event = { id = story.8 days = 15 }
			set_country_flag = guardians_technosphere_encountered
			establish_communications_no_message = event_target:guardian_technosphere_country
		}
	}

	option = {
		name = INTERESTING
		owner = {
			begin_event_chain = {
				event_chain = technosphere_chain
				target = root.owner
			}
		}
		hidden_effect = {
			owner = {
				create_point_of_interest = {
					id = technosphere_chain_poi
					name = technosphere_chain_title
					desc = technosphere_chain_desc
					event_chain = technosphere_chain
					location = root.solar_system
				}
				country_event = { id = leviathans.1050 days = 520 random = 200 }
			}
		}
	}
}

#Technosphere return to black hole to heal
fleet_event = {
	id = leviathans.1031
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_combatant_fleet = {
			has_fleet_flag = technosphere_fleet
		}
	}

	immediate = {
		random_combatant_fleet = {
			limit = {
				has_fleet_flag = technosphere_fleet
			}
			clear_fleet_actions = this
			queue_actions = {
				repeat = {
					max_iterations = 1
					find_random_system = {
						trigger = {
							id = leviathans.1031.trigger.1
							has_star_flag = guardians_technosphere_system
						}
						found_system = {
							move_to = this
							#find_random_planet = {
							#	trigger = {
							#		id = leviathans.1031.trigger.2
							#		is_star = yes
							#	}
							#	found_planet = {
							#		orbit_planet = this
							#	}
							#}
						}
					}
					repeat = {
						while = {
							id = leviathans.1031.trigger.3
							has_hp_percentage < 1.0
						}
						wait = {
							duration = 30
						}
						effect = {
							id = leviathans.1031.trigger.4
							repair_percentage = 0.075
						}
					}
				}
			}
		}
	}
}

country_event = {
	id = leviathans.1032
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		fromfrom = {
			has_fleet_flag = technosphere_fleet
		}
	}

	immediate = {
		fromfrom = {
			clear_fleet_actions = this
			queue_actions = {
				repeat = {
					max_iterations = 1
					find_random_system = {
						trigger = {
							id = leviathans.1032.trigger.1
							has_star_flag = guardians_technosphere_system
						}
						found_system = {
							move_to = this
							find_random_planet = {
								trigger = {
									id = leviathans.1032.trigger.2
									is_star = yes
								}
								found_planet = {
									orbit_planet = this
								}
							}
						}
					}
					repeat = {
						while = {
							id = leviathans.1032.trigger.3
							has_hp_percentage < 1.0
						}
						wait = {
							duration = 30
						}
						effect = {
							id = leviathans.1032.trigger.4
							repair_percentage = 0.075
						}
					}
				}
			}
		}
	}
}

#Killing the Technosphere
country_event = {
	id = leviathans.1039
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		every_country = {
			limit = {
				is_country_type = default
				has_event_chain = curator_poi_chain
				event_target:slain_guardian_system = {
					is_point_of_interest = {
						id = curator_poi_technosphere
						event_chain = curator_poi_chain
						owner = prev
					}
				}
			}
			remove_point_of_interest = curator_poi_technosphere
			end_curator_chain = yes
		}
		every_country = {
			limit = {
				is_country_type = default
				has_event_chain = technosphere_chain
			}
			end_event_chain = technosphere_chain
		}
		every_country = {
			limit = {
				NOR = {
					is_same_value = root
					is_hostile = event_target:guardian_technosphere_country
				}
				is_country_type = default
				OR = {
					has_modifier = infinity_calculations
					has_modifier = technosphere_praising
					has_country_flag = guardians_technosphere_encountered
				}
			}
			country_event = { id = leviathans.1041 }
		}
	}

	after = {
		if = {
			limit = {
				has_modifier = infinity_calculations
			}
			remove_modifier = infinity_calculations
		}
		if = {
			limit = {
				has_modifier = technosphere_praising
			}
			remove_modifier = technosphere_praising
		}
	}
}

country_event = {
	id = leviathans.1040
	title = leviathans.1030.name
	desc = {
		trigger = {
			switch = {
				trigger = has_country_flag
				technosphere_sabotaged = { text = leviathans.1040.desc.alt }
				technosphere_studied = { text = leviathans.1040.desc.detail }
				default = { text = leviathans.1040.desc }
			}
		}
	}
	picture = GFX_evt_technosphere
	show_sound = event_super_explosion
	location = fromfrom

	is_triggered_only = yes

	trigger = {
		OR = {
			AND = {
				has_country_flag = technosphere_sabotaged
				fromfrom = { owner = { is_country_type = guardian_sphere } }
			}
			AND = {
				from = {
					is_country_type = guardian_sphere
				}
			}
		}
	}

	immediate = {
		fromfrom = {
			solar_system = {
				save_event_target_as = technosphere_location
				save_event_target_as = slain_guardian_system
			}
		}
		every_country = {
			limit = {
				is_country_type = default
				has_event_chain = curator_poi_chain
				event_target:slain_guardian_system = {
					is_point_of_interest = {
						id = curator_poi_technosphere
						event_chain = curator_poi_chain
						owner = prev
					}
				}
			}
			remove_point_of_interest = curator_poi_technosphere
			end_curator_chain = yes
		}
		every_country = {
			limit = {
				is_country_type = default
				has_event_chain = technosphere_chain
			}
			end_event_chain = technosphere_chain
		}
		every_country = {
			limit = {
				NOR = {
					is_same_value = root
					is_hostile = event_target:guardian_technosphere_country
				}
				is_country_type = default
				OR = {
					has_modifier = infinity_calculations
					has_modifier = technosphere_praising
					has_country_flag = guardians_technosphere_encountered
				}
			}
			country_event = { id = leviathans.1041 }
		}
	}

	after = {
		if = {
			limit = {
				has_modifier = infinity_calculations
			}
			remove_modifier = infinity_calculations
		}
		if = {
			limit = {
				has_modifier = technosphere_praising
			}
			remove_modifier = technosphere_praising
		}
	}

	option = {
		name = leviathans.1040.a
		add_monthly_resource_mult = {
			resource = influence
			value = @tier5influencereward
			min = @tier5influencemin
			max = @tier5influencemax
		}
		add_monthly_resource_mult = {
			resource = minerals
			value = @tier5materialreward
			min = @tier5materialmin
			max = @tier5materialmax
		}
	}
	option = {
		name = leviathans.1040.c
		trigger = {
			NOR = {
				has_ethic = ethic_gestalt_consciousness
				has_technology = tech_robotic_workers
				has_technology = tech_droid_workers
				has_technology = tech_synthetic_workers
				has_technology = tech_synthetic_leaders
			}
			can_research_technology = tech_robotic_workers
		}
		add_research_option = tech_robotic_workers
		add_tech_progress = {
			tech = tech_robotic_workers
			progress = 0.50
		}
	}
	option = {
		name = leviathans.1040.c
		trigger = {
			has_technology = tech_robotic_workers
			NOR = {
				has_ethic = ethic_gestalt_consciousness
				has_technology = tech_droid_workers
				has_technology = tech_synthetic_workers
				has_technology = tech_synthetic_leaders
			}
			can_research_technology = tech_droid_workers
		}
		add_research_option = tech_droid_workers
		add_tech_progress = {
			tech = tech_droid_workers
			progress = 0.50
		}
	}
	option = {
		name = leviathans.1040.c
		trigger = {
			has_technology = tech_robotic_workers
			has_technology = tech_droid_workers
			NOR = {
				has_ethic = ethic_gestalt_consciousness
				has_technology = tech_synthetic_workers
				has_technology = tech_synthetic_leaders
			}
			can_research_technology = tech_synthetic_workers
		}
		add_research_option = tech_synthetic_workers
		add_tech_progress = {
			tech = tech_synthetic_workers
			progress = 0.50
		}
	}
	option = {
		name = leviathans.1040.c
		trigger = {
			has_technology = tech_robotic_workers
			has_technology = tech_droid_workers
			has_technology = tech_synthetic_workers
			NOR = {
				has_ethic = ethic_gestalt_consciousness
				has_technology = tech_synthetic_leaders
			}
			can_research_technology = tech_synthetic_leaders
		}
		add_research_option = tech_synthetic_leaders
		add_tech_progress = {
			tech = tech_synthetic_leaders
			progress = 0.50
		}
	}
	option = {
		name = leviathans.1040.d
		trigger = {
			can_harvest_parts = yes
		}
		set_country_flag = harvested_sphere_parts
		add_research_option = tech_leviathan_techgenesis
	}
}

country_event = {
	id = leviathans.1041
	title = leviathans.1030.name
	desc = leviathans.1041.desc
	picture = GFX_evt_technosphere
	show_sound = event_super_explosion
	location = event_target:technosphere_ship

	is_triggered_only = yes

	after = {
		if = {
			limit = {
				has_modifier = infinity_calculations
			}
			remove_modifier = infinity_calculations
		}
		if = {
			limit = {
				has_modifier = technosphere_praising
			}
			remove_modifier = technosphere_praising
		}
	}

	option = {
		name = TERRIBLE
		trigger = {
			NOR = {
				is_militarist = yes
				is_materialist = yes
			}
		}
	}
	option = {
		name = CURSES
		trigger = {
			OR = {
				is_militarist = yes
				is_materialist = yes
			}
		}
	}
}

#Investigating the Technosphere
country_event = {
	id = leviathans.1050
	title = leviathans.1050.name
	desc = leviathans.1050.desc
	picture = GFX_evt_technosphere
	show_sound = event_sensor_ping
	location = event_target:technosphere_ship
	trackable = yes

	is_triggered_only = yes

	trigger = {
		exists = event_target:technosphere_ship
		is_ai = no
		has_country_flag = guardians_technosphere_encountered
		any_system = {
			has_star_flag = guardians_technosphere_system
			any_fleet_in_system = {
				exists = owner
				owner = {
					is_country_type = guardian_sphere
				}
			}
		}
		NOR = {
			has_country_flag = worm_events_available
			event_target:guardian_technosphere_country = {
				is_hostile = root
			}
		}
	}

	immediate = {
		event_target:guardian_technosphere_country = {
			random_controlled_ship = {
				limit = {
					is_ship_size = sphere
				}
				save_event_target_as = technosphere_location
			}
		}
		end_event_chain = technosphere_chain
	}

	option = {
		name = FASCINATING
		enable_special_project = {
			name = "TECHNOSPHERE_PROJECT"
			location = event_target:technosphere_ship
			owner = root
		}
	}
	option = {
		name = NOTIME
		add_resource = { influence = 50 }
	}
}

#Succeeded in researching the Technosphere
country_event = {
	id = leviathans.1051
	title = leviathans.1050.name
	desc = leviathans.1051.desc
	picture = GFX_evt_technosphere
	show_sound = event_mystic_reveal
	location = event_target:technosphere_ship
	trackable = yes

	is_triggered_only = yes

	trigger = {
		exists = event_target:technosphere_ship
		NOT = { has_country_flag = worm_events_available }
	}

	immediate = {
		set_country_flag = technosphere_studied
		event_target:guardian_technosphere_country = {
			random_controlled_ship = {
				limit = {
					is_ship_size = sphere
				}
				save_event_target_as = technosphere_location
			}
		}
	}

	option = {
		name = leviathans.1051.a
		trigger = {
			NOT = { has_ethic = ethic_gestalt_consciousness }
		}
		hidden_effect = {
			country_event = { id = leviathans.1055 }
		}
	}
	option = {
		name = leviathans.1051.c #praise it as your god
		trigger = {
			is_spiritualist = yes
		}
		add_modifier = {
			modifier = technosphere_praising
			days = -1
		}
	}
	option = {
		name = leviathans.1051.b #hack it
		trigger = {
			is_materialist = yes
		}
		enable_special_project = {
			name = TECHNOSPHERE_HACKING
			location = event_target:technosphere_ship
			owner = root
		}
	}
	option = {
		name = leviathans.1051.d #sabotage it
		#custom_tooltip = leviathans.1051.d.tooltip
		trigger = {
			is_militarist = yes
		}
		hidden_effect = {
			set_country_flag = technosphere_sabotaged
		}
		event_target:technosphere_ship = {
			enable_special_project = {
				name = TECHNOSPHERE_DESTRUCTION
				location = this
				owner = root
			}
		}
	}
	option = {
		name = leviathans.1051.e #hack it - Gestalt Consciousness
		trigger = {
			has_ethic = ethic_gestalt_consciousness
		}
		enable_special_project = {
			name = TECHNOSPHERE_HACKING
			location = event_target:technosphere_ship
			owner = root
		}
	}
}

#Hacking the technosphere - success
country_event = {
	id = leviathans.1052
	title = leviathans.1050.name
	desc = {
		trigger = {
			NOT = { has_ethic = ethic_gestalt_consciousness }
		}
		text = leviathans.1052.desc
	}
	desc = {
		trigger = { has_ethic = ethic_gestalt_consciousness }
		text = leviathans.1052.gestalt.desc
	}
	picture = GFX_evt_technosphere
	show_sound = event_ai_started
	location = event_target:technosphere_ship
	is_triggered_only = yes

	trigger = {
		exists = event_target:technosphere_ship
	}

	immediate = {
		event_target:guardian_technosphere_country = {
			random_controlled_ship = {
				limit = {
					is_ship_size = sphere
				}
				save_event_target_as = technosphere_location
			}
		}
	}
	after = {
		add_modifier = {
			modifier = infinity_calculations_hacked
			days = -1
		}
		event_target:technosphere_location = {
			solar_system = {
				save_event_target_as = slain_guardian_system
			}
			fleet = {
				destroy_fleet = this
			}
		}
		hidden_effect = {
			country_event = { id = leviathans.1039 }
		}
	}
	option = {
		name = leviathans.1052.a
		if = {
			limit = {
				has_ethic = ethic_gestalt_consciousness
			}
			give_technology = { tech = tech_positronic_implants }
		}
		else = {
			give_technology = { tech = tech_sapient_ai }
		}
		trigger = {
			NOR = {
				has_technology = tech_sapient_ai
				has_technology = tech_positronic_implants
			}
		}
	}
	option = {
		name = leviathans.1052.a
		trigger = {
			OR = {
				has_technology = tech_sapient_ai
				has_technology = tech_positronic_implants
			}
			NOT = {
				has_technology = tech_zero_point_power
			}
		}
		give_technology = { tech = tech_zero_point_power }
	}
	option = {
		name = leviathans.1052.a
		trigger = {
			OR = {
				has_technology = tech_sapient_ai
				has_technology = tech_positronic_implants
			}
			has_technology = tech_zero_point_power
		}
		add_monthly_resource_mult = {
			resource = society_research
			value = 24
			min = 1000
			max = 5000
		}
	}
}

#Hacking the technosphere - fail
country_event = {
	id = leviathans.1053
	title = sphere
	desc = leviathans.1053.desc
	is_triggered_only = yes
	diplomatic = yes

	trigger = { exists = event_target:technosphere_ship }

	picture_event_data = {
		room = technosphere_room
	}

	option = {
		name = leviathans.1053.a
		response_text = leviathans.1053.a.reply
		default_hide_option = yes
	}
	option = {
		name = leviathans.1053.b
		response_text = leviathans.1053.b.reply
	}
}

#Contacting the technosphere
country_event = {
	id = leviathans.1055
	title = sphere
	desc = leviathans.1055.desc
	is_triggered_only = yes
	diplomatic = yes

	trigger = {
		exists = event_target:technosphere_ship
		NOT = { has_country_flag = worm_events_available }
	}

	picture_event_data = {
		room = technosphere_room
	}

	option = {
		name = leviathans.1055.a
		#default_hide_option = yes
		hidden_effect = {
			country_event = { id = leviathans.1056 }
		}
	}
}

#Dialogue with the Technosphere
country_event = {
	id = leviathans.1056
	title = sphere
	desc = leviathans.1056.desc
	is_triggered_only = yes
	diplomatic = yes

	trigger = { exists = event_target:technosphere_ship }

	picture_event_data = {
		room = technosphere_room
	}

	option = {
		name = leviathans.1056.a
		response_text = leviathans.1056.a.reply
		is_dialog_only = yes
	}
	option = {
		name = leviathans.1056.b
		response_text = leviathans.1056.b.reply
		is_dialog_only = yes
	}
	option = {
		name = leviathans.1056.c
		response_text = leviathans.1056.c.reply
		is_dialog_only = yes
		trigger = {
			OR = {
				is_militarist = yes
				is_fanatic_egalitarian = yes
			}
		}
	}
	option = {
		name = leviathans.1056.d
		hidden_effect = {
			country_event = { id = leviathans.1060 }
		}
	}
	option = {
		name = leviathans.1056.e
		response_text = leviathans.1056.e.reply
		add_resource = { influence = 100 }
	}
}

#Offer your services to the Technosphere
country_event = {
	id = leviathans.1060
	title = sphere
	desc = leviathans.1060.desc
	is_triggered_only = yes
	diplomatic = yes
	trackable = yes

	trigger = { exists = event_target:technosphere_ship }

	picture_event_data = {
		room = technosphere_room
	}

	immediate = {
		random_system = {
			limit = {
				has_star_flag = guardians_technosphere_system
			}
			random_system_planet = {
				limit = {
					is_star = yes
				}
				save_event_target_as = technosphere_blackhole
			}
		}
	}

	option = {
		name = leviathans.1060.a
		response_text = leviathans.1060.a.reply
		enable_special_project = {
			name = TECHNOSPHERE_BLACKHOLE
			location = event_target:technosphere_blackhole
			owner = root
		}
	}
	option = {
		name = leviathans.1056.e #GOODBYE
		response_text = leviathans.1056.e.reply
		add_resource = { influence = 100 }
	}
}

#Researching the black hole - success
country_event = {
	id = leviathans.1061
	title = sphere
	desc = leviathans.1061.desc
	is_triggered_only = yes
	diplomatic = yes

	trigger = { exists = event_target:technosphere_ship }

	picture_event_data = {
		room = technosphere_room
	}

	immediate = {
		random_system = {
			limit = {
				has_star_flag = guardians_technosphere_system
			}
			random_system_planet = {
				limit = {
					is_star = yes
				}
				save_event_target_as = technosphere_blackhole
			}
		}
	}

	option = {
		name = leviathans.1061.a
		response_text = leviathans.1061.a.reply
		if = {
			limit = {
				has_ethic = ethic_gestalt_consciousness
			}
			give_technology = { tech = tech_positronic_implants }
		}
		else = {
			give_technology = { tech = tech_sapient_ai }
		}
		add_resource = { influence = 150 }
		trigger = {
			NOR = {
				has_technology = tech_sapient_ai
				has_technology = tech_positronic_implants
			}
		}
	}
	option = {
		name = leviathans.1061.a
		response_text = leviathans.1061.a.reply
		give_technology = { tech = tech_zero_point_power }
		add_resource = { influence = 150 }
		trigger = {
			OR = {
				has_technology = tech_sapient_ai
				has_technology = tech_positronic_implants
			}
			NOT = {
				has_technology = tech_zero_point_power
			}
		}
	}
	option = {
		name = leviathans.1061.b
		response_text = leviathans.1061.b.reply
		add_modifier = {
			modifier = infinity_calculations
			days = -1
		}
		trigger = {
			NOR = {
				has_technology = tech_sapient_ai
				has_technology = tech_positronic_implants
				has_technology = tech_zero_point_power
			}
		}
	}
	option = {
		name = leviathans.1061.b.alt
		response_text = leviathans.1061.b.reply
		add_modifier = {
			modifier = infinity_calculations
			days = -1
		}
		trigger = {
			OR = {
				has_technology = tech_sapient_ai
				has_technology = tech_positronic_implants
			}
			has_technology = tech_zero_point_power
		}
	}
}

#Researching the black hole - submerge check (fail or special event)
country_event = {
	id = leviathans.1062
	title = sphere
	desc = leviathans.1062.desc
	show_sound = event_mystic_reveal
	location = event_target:technosphere_ship
	diplomatic = yes

	trigger = { exists = event_target:technosphere_ship }

	picture_event_data = {
		room = technosphere_room
	}

	is_triggered_only = yes

	immediate = {
		set_country_flag = technosphere_studied
		event_target:guardian_technosphere_country = {
			random_controlled_ship = {
				limit = {
					is_ship_size = sphere
				}
				save_event_target_as = technosphere_location
			}
		}

		random_system = {
			limit = {
				has_star_flag = guardians_technosphere_system
			}
			random_system_planet = {
				limit = {
					is_star = yes
				}
				save_event_target_as = technosphere_blackhole
			}
		}
	}

	option = {
		name = leviathans.1062.a
		hidden_effect = {
			event_target:guardian_technosphere_country = {
				random_controlled_ship = {
					limit = {
						is_ship_size = sphere
					}
					fleet = {
						queue_actions = {
							find_closest_planet = {
								trigger = {
									id = leviathans.1062.trigger.1
									is_planet_class = pc_black_hole
									solar_system = {
										has_star_flag = guardians_technosphere_system
									}
								}
								found_planet = {
									move_to = this
									effect = {
										id = leviathans.1062.effect.1
										root = {
											if = {
												limit = {
													has_country_flag = technosphere_special
												}
												remove_country_flag = technosphere_special
												country_event = { id = leviathans.1065 }
											}
											else = {
												country_event = { id = leviathans.1063 }
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}

#The technosphere submerges itself into the hole - FAIL
country_event = {
	id = leviathans.1063
	title = sphere
	desc = leviathans.1063.desc
	picture = GFX_evt_technosphere
	show_sound = event_mystic_reveal
	location = event_target:technosphere_ship

	is_triggered_only = yes

	trigger = { exists = event_target:technosphere_ship }

	option = {
		name = PECULIAR
		custom_tooltip = leviathans.1063.a.tooltip
		add_resource = { influence = 150 }
		if = {
			limit = {
				NOR = {
					has_technology = tech_sapient_ai
					has_technology = tech_positronic_implants
				}
			}
			if = {
				limit = {
					has_ethic = ethic_gestalt_consciousness
				}
				give_technology = { tech = tech_positronic_implants }
			}
			else = {
				give_technology = { tech = tech_sapient_ai }
			}
		}
		else_if = {
			limit = {
				NOT = { has_technology = tech_zero_point_power }
			}
			give_technology = { tech = tech_zero_point_power }
		}
		else = {
			add_resource = { influence = 150 }
		}
		hidden_effect = {
			event_target:technosphere_ship = {
				solar_system = {
					random_system_planet = {
						limit = { is_star = yes }
						add_deposit = d_physics_4
						add_deposit = d_engineering_4
						add_deposit = d_society_4
					}
				}
			}
			event_target:technosphere_ship = {
				solar_system = {
					save_event_target_as = slain_guardian_system
				}
				delete_fleet = this
			}
			country_event = { id = leviathans.1039 }
		}
	}
}

#The technosphere submerges itself into the hole - secret
country_event = {
	id = leviathans.1065
	title = sphere
	desc = leviathans.1065.desc
	picture = GFX_evt_technosphere
	show_sound = event_mystic_reveal
	location = event_target:technosphere_ship

	is_triggered_only = yes

	trigger = { exists = event_target:technosphere_ship }

	immediate = {
		set_country_flag = pantagruel
	}

	option = {
		name = AMAZING
		custom_tooltip = leviathans.1065.a.tooltip
		add_modifier = {
			modifier = black_hole_pantagruel_research
			days = -1
		}
		add_resource = { influence = 200 }
		hidden_effect = {

			event_target:technosphere_ship.solar_system = {
				random_system_planet = {
					limit = { is_star = yes }
					solar_system = {
						set_name = "NAME_Pantagruel"
						set_star_flag = pantagruel
					}
					set_name = "NAME_Pantagruel"
					change_planet_size = -30
					create_ambient_object = {
						type = explosion_particle_object
						location = this
						duration = 5

						use_3d_location = yes

						entity_offset = { min = 0 max = 0 }
						entity_offset_angle = { min = 0 max = 0 }
						entity_offset_height = { min = 0 max = 0 }

						entity_scale_to_size = yes
						scale = 100
					}
					add_deposit = d_physics_10
					add_deposit = d_engineering_10
					add_deposit = d_society_10
					add_modifier = {
						modifier = black_hole_pantagruel
						days = -1
					}
					if = {
						limit = {
							exists = space_owner
							space_owner = { is_same_value = root }
						}
						space_owner = {
							add_modifier = {
								modifier = black_hole_pantagruel_research
								days = -1
							}
						}
					}
					if = {
						limit = {
							is_surveyed = {
								who = root
								status = no
							}
						}
						set_surveyed = {
							surveyed = yes
							surveyor = root
						}
					}
				}
			}
			event_target:technosphere_ship = {
				solar_system = {
					save_event_target_as = slain_guardian_system
				}
				#fleet = {
					delete_fleet = this
				#}
			}
			country_event = { id = leviathans.1039 }
		}
	}
}

#############################################################

# wraith won't be discovered past the midgame
event = {
	id = leviathans.20101
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		any_system = {
			any_system_planet = { has_planet_flag = guardians_wraith_pulsar }
		}
	}

	immediate = {
		random_galaxy_planet = {
			limit = { has_planet_flag = guardians_wraith_pulsar }
			remove_planet_flag = guardians_wraith_pulsar
		}
	}
}

### WRAITH ###
# First scan of the neutron star, sets Wraith spawn timer
ship_event = {
	id = leviathans.2010
	title = "leviathans.2010.name"
	desc = {
		text = leviathans.2010.desc
		trigger = {
			owner = { NOT = { has_ethic = ethic_gestalt_consciousness } }
		}
	}
	desc = {
		text = leviathans.2010.desc.gesta
		trigger = {
			owner = { has_ethic = ethic_gestalt_consciousness }
		}
	}
	picture = GFX_evt_star_pulsar
	show_sound = event_mystic_reveal

	is_triggered_only = yes

	trigger = {
		from = { has_planet_flag = guardians_wraith_pulsar }
	}

	immediate = {
		from = {
			remove_planet_flag = guardians_wraith_pulsar
			add_modifier = {
				modifier = germinating_wraith
				days = -1
			}
		}
	}

	option = {
		name = leviathans.2010.a
		custom_tooltip = leviathans.2010.c
	}
}

# Wraith is born
event = {
	id = leviathans.2011
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		any_system = {
			any_system_planet = { has_modifier = germinating_wraith }
		}
	}

	immediate = {
		random_galaxy_planet = {
			limit = { has_modifier = germinating_wraith }
			save_event_target_as = wraith_star
			remove_modifier = germinating_wraith
			create_country = {
				name = "NAME_Wraith"
				type = guardian_wraith
				flag = {
					icon = {
						category = "zoological"
						file = "flag_zoological_5.dds"
					}
					background= {
						category = "backgrounds"
						file = "00_solid.dds"
					}
					colors={
						"red"
						"red"
						"null"
						"null"
					}
				}
			}
			last_created_country = {
				set_country_flag = wraith_country
				save_global_event_target_as = wraith_country
				create_fleet = {
					name = "NAME_Specter"
					settings = {
						spawn_debris = no
						is_boss = yes
					}
					effect = {
						set_owner = event_target:wraith_country
						random_list = {
							33 = {
								create_ship = {
									name = "NAME_Wraith_650THz"
									design = "NAME_Spectral_Wraith_650THz"
									effect = {
										set_ship_flag = wraith_ship
										set_ship_flag = wraith_freq_blue
									}
								}
							}
							33 = {
								create_ship = {
									name = "NAME_Wraith_450THz"
									design = "NAME_Spectral_Wraith_450THz"
									effect = {
										set_ship_flag = wraith_ship
										set_ship_flag = wraith_freq_red
									}
								}
							}
							33 = {
								create_ship = {
									name = "NAME_Wraith_520THz"
									design = "NAME_Spectral_Wraith_520THz"
									effect = {
										set_ship_flag = wraith_ship
										set_ship_flag = wraith_freq_yellow
									}
								}
							}
						}
						save_global_event_target_as = wraith_fleet
						set_fleet_flag = wraith_fleet
						set_location = event_target:wraith_star
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = self
						set_aggro_range = 20
						queue_actions = {
							repeat = {
								find_closest_system = {
									trigger = {
										id = wraith.1.trigger.1
										NOR = {
											exists = space_owner
											any_fleet_in_system = {
												is_same_value = event_target:wraith_fleet
											}
											has_star_flag = guardians_wraith_visited
										}
									}
									found_system = {
										move_to = this
										find_closest_planet = {
											trigger = {
												id = wraith.1.trigger.2
												is_star = yes
											}
											found_planet = {
												move_to = this
												#orbit_planet = this
												wait = {
													duration = 10
												}
											}
										}
									}
								}
							}
						}
					}
				}
				every_country = {
					limit = {
						is_non_hostile_to_wraith = yes
					}
					set_faction_hostility = {
						target = prev
						set_hostile = no
						set_neutral = yes
						set_friendly = no
					}
				}
			}
			if = {
				limit = { exists = space_owner }
				space_owner = {
					country_event = { id = leviathans.2012 }
				}
			}
		}
	}
}

# Wraith Spawned in Territory
country_event = {
	id = leviathans.2012
	title = "leviathans.2012.name"
	desc = {
		text = leviathans.2012.desc
		trigger = {
			NOT = { has_ethic = ethic_gestalt_consciousness }
		}
	}
	desc = {
		text = leviathans.2012.desc.gesta
		trigger = {
			has_ethic = ethic_gestalt_consciousness
		}
	}
	picture = GFX_evt_wraith
	show_sound = event_space_whale

	is_triggered_only = yes

	trigger = {
		NOT = { has_country_flag = guardians_wraith_encountered }
	}

	immediate = {
		country_event = { id = story.8 days = 15 }
		establish_communications_no_message = event_target:wraith_country
		set_country_flag = guardians_wraith_encountered
	}

	option = {
		name = leviathans.2012.a
	}
}

# Wraith Enters Territory
country_event = {
	id = leviathans.2013
	title = "leviathans.2012.name"
	desc = leviathans.2013.desc
	picture = GFX_evt_wraith
	show_sound = event_space_whale

	is_triggered_only = yes

	trigger = {
		FROM.OWNER = { has_country_flag = wraith_country }
		NOT = { has_country_flag = guardians_wraith_encountered }
	}

	immediate = {
		country_event = { id = story.8 days = 15 }
		establish_communications_no_message = event_target:wraith_country
		set_country_flag = guardians_wraith_encountered
	}

	option = {
		name = leviathans.2013.a
	}
}

# Wraith killed
country_event = {
	id = leviathans.2014
	title = "leviathans.2014.name"
	desc = {
		text = leviathans.2014.desc
		trigger = {
			NOT = { has_ethic = ethic_gestalt_consciousness }
		}
	}
	desc = {
		text = leviathans.2014.desc.gesta
		trigger = {
			has_ethic = ethic_gestalt_consciousness
		}
	}
	picture = GFX_evt_wraith
	show_sound = event_space_whale

	is_triggered_only = yes

	trigger = {
		from = { has_country_flag = wraith_country }
	}

	immediate = {
		set_country_flag = killed_wraith
		fromfrom.solar_system = {
			save_event_target_as = slain_guardian_system
		}
		every_country = {
			limit = {
				is_country_type = default
				has_event_chain = curator_poi_chain
				event_target:slain_guardian_system = {
					is_point_of_interest = {
						id = curator_poi_wraith
						event_chain = curator_poi_chain
						owner = prev
					}
				}
			}
			remove_point_of_interest = curator_poi_wraith
			end_curator_chain = yes
		}
	}

	option = {
		name = leviathans.2014.a
		add_modifier = {
			modifier = "spectral_residue_studies"
			days = -1
		}
	}
	after = {
		if = {
			limit = { num_owned_planets > 0 }
			generate_parade_city = yes
			start_situation = {
				type = leviathan_celebration_opportunity
				target = event_target:parade_city
				effect = {
					set_situation_flag = celebration_spectral_wraith
					set_situation_flag = standard_unity_reward
				}
			}
		}
	}
}

# Wraith Frequency Modifier Mechanics
fleet_event = {
	id = leviathans.2061
	hide_window = yes
	is_triggered_only = yes

	trigger = { has_fleet_flag = wraith_fleet }

	immediate = {
		solar_system = {
			set_star_flag = guardians_wraith_visited
			if = {
				limit = {
					exists = space_owner
					space_owner = { NOT = { has_country_flag = guardians_wraith_encountered } }
				}
				space_owner = {
					country_event = { id = leviathans.2013 }
				}
			}
		}
		random_controlled_ship = {
			if = {
				limit = { has_modifier = wraith_frequency_sync }
				# Removes the modifier when the right star class is not present
				if = {
					limit = {
						has_ship_flag = wraith_freq_blue
						NOT = {
							solar_system = {
								OR = {
									is_star_class = sc_b
									is_star_class = sc_a
								}
							}
						}
					}
					remove_modifier = "wraith_frequency_sync"
				}
				if = {
					limit = {
						has_ship_flag = wraith_freq_yellow
						NOT = {
							solar_system = {
								is_star_class = sc_g
							}
						}
					}
					remove_modifier = "wraith_frequency_sync"
				}
				if = {
					limit = {
						has_ship_flag = wraith_freq_red
						NOT = {
							solar_system = {
								is_star_class = sc_m
							}
						}
					}
					remove_modifier = "wraith_frequency_sync"
				}
			}
			if = {
				limit = { NOT = { has_modifier = wraith_frequency_sync } }
				# Adds the modifier when the right star class is present
				if = {
					limit = {
						has_ship_flag = wraith_freq_blue
						solar_system = {
							OR = {
								is_star_class = sc_b
								is_star_class = sc_a
							}
						}
					}
					add_modifier = { modifier = "wraith_frequency_sync" days = -1 }
				}
				if = {
					limit = {
						has_ship_flag = wraith_freq_yellow
						solar_system = {
							is_star_class = sc_g
						}
					}
					add_modifier = { modifier = "wraith_frequency_sync" days = -1 }
				}
				if = {
					limit = {
						has_ship_flag = wraith_freq_red
						solar_system = {
							is_star_class = sc_m
						}
					}
					add_modifier = { modifier = "wraith_frequency_sync" days = -1 }
				}
			}
		}
	}
}

# Enigmatic Fortress Disabled (HIDDEN)
ship_event = {
	id = leviathans.2100
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		has_ship_flag = fortress_vault
		from.owner = { is_country_type = default }
	}

	immediate = {
		from.owner = { country_event = { id = leviathans.2101 } }
	}
}

# Ship destroyed perp port
country_event = {
	id = leviathans.2099
	title = "leviathans.2101.name"
	desc = {
		text = "leviathans.2101.desc"
		trigger = { NOT = { has_ethic = ethic_gestalt_consciousness } }
	}
	desc = {
		text = "leviathans.2101.desc.gesta"
		trigger = { has_ethic = ethic_gestalt_consciousness }
	}
	picture = GFX_evt_enigmatic_fortress
	show_sound = event_space_battle
	location = fromfrom

	is_triggered_only = yes

	trigger = {
		exists = from
		exists = event_target:fortress_country
		from = { is_same_value = event_target:fortress_country }
		exists = event_target:fortress_vault_core
		event_target:fortress_country = {
			count_controlled_ship = { # Check that all ships are disabled
				limit = { is_disabled = no }
				count < 1
			}
		}
	}

	immediate = {
		country_event = { id = story.8 days = 15 }
		establish_communications_no_message = event_target:fortress_country
		#event_target:fortress_country = {
			event_target:fortress_vault_core = {
				#limit = { has_ship_flag = fortress_vault }
				save_global_event_target_as = disabled_enigmatic_fortress
			}
		#}
		every_playable_country = {
			limit = { has_event_chain = "enigmatic_fortress_chain" }
			end_event_chain = "enigmatic_fortress_chain"
		}
	}

	option = {
		name = "leviathans.2101.a"
		begin_event_chain = {
			event_chain = "enigmatic_fortress_chain"
			target = root
		}
		hidden_effect = { country_event = { id = leviathans.2111 } }
		trigger = { NOT = { has_ethic = ethic_gestalt_consciousness } }
	}

	option = {
		name = "leviathans.2101.b"
		hidden_effect = { country_event = { id = leviathans.2160 days = 360 } }
		trigger = { NOT = { has_ethic = ethic_gestalt_consciousness } }
	}

	option = {
		name = leviathans.2101.c.gesta
		trigger = { has_ethic = ethic_gestalt_consciousness }
		add_monthly_resource_mult = {
			resource = engineering_research
			value = 24
			min = 1000
			max = 5000
		}
		add_monthly_resource_mult = {
			resource = physics_research
			value = 24
			min = 1000
			max = 5000
		}
		add_monthly_resource_mult = {
			resource = society_research
			value = 24
			min = 1000
			max = 5000
		}
		add_research_option = tech_enigmatic_encoder
		add_tech_progress = {
			tech = tech_enigmatic_encoder
			progress = 0.30
		}
		add_research_option = tech_enigmatic_decoder
		add_tech_progress = {
			tech = tech_enigmatic_decoder
			progress = 0.30
		}
		hidden_effect = {
			# scuttle fortress
			event_target:fortress_vault_core = {
				fleet = {
					set_event_locked = no
					destroy_fleet = this
				}
			}
			#event_target:fortress_country = {
			#	every_controlled_fleet = {
			#		delete_fleet = this
			#	}
			#}
			#event_target:fortress_vault_core = {
			#	solar_system = {
			#		random_system_planet = {
			#			limit = { is_star = yes }
			#			create_ambient_object = {
			#				type = "dead_enigmatic_fortress_object"
			#				location = solar_system
			#			}
			#			last_created_ambient_object = {
			#				save_global_event_target_as = fortress_ambient
			#				set_location = {
			#					target = PREV
			#					distance = 110
			#					angle = 110
			#				}
			#			}
			#		}
			#	}
			#	fleet = {
			#		delete_fleet = this
			#	}
			#}
		}
		if = {
			limit = { num_owned_planets > 0 }
			generate_parade_city = yes
			start_situation = {
				type = leviathan_celebration_opportunity
				target = event_target:parade_city
				effect = {
					set_situation_flag = celebration_enigmatic_fortress
					set_situation_flag = standard_unity_reward
				}
			}
		}
	}
	after = {
		set_country_flag = disabled_enigmatic_fortress
	}
}



# The Enigmatic Fortress
country_event = {
	id = leviathans.2101
	title = "leviathans.2101.name"
	desc = "leviathans.2101.desc"
	picture = GFX_evt_enigmatic_fortress
	show_sound = event_space_battle
	location = from

	is_triggered_only = yes

	trigger = {
		from.owner = { is_same_value = event_target:fortress_country }
		event_target:fortress_country = {
			count_controlled_ship = { # Check that all ships are disabled
				limit = { is_disabled = no }
				count < 1
			}
		}
	}

	immediate = {
		if = {
			limit = {
				NOT = { has_communications = event_target:fortress_country }
			}
			country_event = { id = story.8 days = 15 }
			establish_communications_no_message = event_target:fortress_country
		}
		event_target:fortress_country = {
			random_controlled_ship = {
				limit = { has_ship_flag = fortress_vault }
				save_global_event_target_as = disabled_enigmatic_fortress
			}
		}
	}

	option = {
		name = "leviathans.2101.a"
		if = {
			limit = {
				NOT = { has_event_chain = enigmatic_fortress_chain }
			}
			begin_event_chain = {
				event_chain = "enigmatic_fortress_chain"
				target = root
			}
		}
		hidden_effect = { country_event = { id = leviathans.2111 } }
	}

	option = {
		name = "leviathans.2101.b"
		hidden_effect = { country_event = { id = leviathans.2160 days = 360 } }
	}

	option = {
		name = "leviathans.2101.c"
		trigger = {
			can_harvest_parts = yes
		}
		set_country_flag = harvested_fortress_parts
		add_research_option = tech_leviathan_techgenesis
	}

}

# Entering System
ship_event = {
	id = leviathans.2105
	title = "leviathans.2105.name"
	desc = "leviathans.2105.desc"
	picture = GFX_evt_enigmatic_fortress
	show_sound = event_ship_bridge
	location = FROM

	is_triggered_only = yes

	trigger = {
		FROM = {
			has_star_flag = guardians_fortress_system
			any_ship_in_system = {
				AND = {
					is_ship_size = station_xl
					is_disabled = no
				}
			}
		}
		owner = {
			is_ai = no
		}
	}

	immediate = {
		FROM = { save_event_target_as = fortress_system }
	}

	option = {
		name = "leviathans.2105.a"
	}
}

# Entering the Fortress
country_event = {
	id = leviathans.2111
	title = "leviathans.2111.name"
	desc = "leviathans.2111.desc"
	picture = GFX_evt_enigmatic_fortress
	show_sound = event_mystic_reveal
	location = event_target:disabled_enigmatic_fortress
	trackable = yes

	is_triggered_only = yes

	option = {
		name = "leviathans.2111.a"
		custom_tooltip = leviathans.2111.a.tooltip
		hidden_effect = {
			event_target:disabled_enigmatic_fortress = {
				enable_special_project = {
					name = "ENIGMATIC_FORTRESS_1A"
					location = this
					owner = ROOT
				}
			}
			event_target:disabled_enigmatic_fortress = {
				enable_special_project = {
					name = "ENIGMATIC_FORTRESS_1B"
					location = this
					owner = ROOT
				}
			}
		}
	}
}

# Uncontrolled Demolition
ship_event = {
	id = leviathans.2112
	title = "leviathans.2112.name"
	desc = "leviathans.2112.desc"
	picture = GFX_evt_enigmatic_fortress
	show_sound = event_ground_battle
	location = fromfrom

	is_triggered_only = yes

	option = {
		name = "leviathans.2112.a"
		custom_tooltip = leviathans.2112.a.tooltip
		hidden_effect = {
			owner = { country_event = { id = leviathans.2131 days = 30 } }
			fleet = { destroy_fleet = this }
		}
	}
}

# Adjusting Energy Supply
ship_event = {
	id = leviathans.2113
	title = "leviathans.2113.name"
	desc = "leviathans.2113.desc"
	picture = GFX_evt_enigmatic_fortress
	show_sound = event_laboratory_sound
	location = fromfrom

	is_triggered_only = yes

	option = {
		name = "leviathans.2113.a"
		trigger = { owner = { resource_stockpile_compare = { resource = energy value >= 500 } } }
		owner = {
			add_resource = { energy = -500 }
			hidden_effect = { country_event = { id = leviathans.2114 } }
		}
	}
	option = {
		name = "leviathans.2113.b"
		owner = {
			add_resource = { energy = -50 }
			hidden_effect = { country_event = { id = leviathans.2115 } }
		}
	}
}

# Fortress Repowers (Energy Surge)
country_event = {
	id = leviathans.2114
	title = "leviathans.2114.name"
	desc = "leviathans.2114.desc"
	picture = GFX_evt_enigmatic_fortress
	show_sound = event_activating_unknown_technology
	location = event_target:disabled_enigmatic_fortress

	is_triggered_only = yes

	option = {
		name = "leviathans.2114.a"
		hidden_effect = {
			event_target:disabled_enigmatic_fortress = {
				set_disabled = no
				repair_ship = yes
			}
		}
		end_event_chain = "enigmatic_fortress_chain"
	}
}

# Airlock Opened
country_event = {
	id = leviathans.2115
	title = "leviathans.2115.name"
	desc = "leviathans.2115.desc"
	picture = GFX_evt_enigmatic_fortress
	show_sound = event_airlock
	location = event_target:disabled_enigmatic_fortress

	is_triggered_only = yes

	option = {
		name = "leviathans.2115.a"
		custom_tooltip = leviathans.2112.a.tooltip
		hidden_effect = { country_event = { id = leviathans.2131 days = 30 } }
	}
}

# The Tower
country_event = {
	id = leviathans.2131
	title = "leviathans.2131.name"
	desc = "leviathans.2131.desc"
	picture = GFX_evt_enigmatic_fortress
	show_sound = event_mystic_reveal
	location = event_target:disabled_enigmatic_fortress

	is_triggered_only = yes

	option = {
		name = "leviathans.2131.a"
		hidden_effect = { country_event = { id = leviathans.2132 } }
	}
	option = {
		name = "leviathans.2131.b"
		hidden_effect = { country_event = { id = leviathans.2132 } }
	}
	option = {
		name = "leviathans.2131.c"
		hidden_effect = { country_event = { id = leviathans.2133 } }
	}
	option = {
		name = "leviathans.2131.d"
		hidden_effect = { country_event = { id = leviathans.2132 } }
	}
}

# Disturbing Noises
country_event = {
	id = leviathans.2132
	title = "leviathans.2132.name"
	desc = "leviathans.2132.desc"
	picture = GFX_evt_enigmatic_fortress
	show_sound = event_activating_unknown_technology
	location = event_target:disabled_enigmatic_fortress

	is_triggered_only = yes

	option = {
		name = "leviathans.2132.a"
		hidden_effect = {
			event_target:disabled_enigmatic_fortress = {
				set_disabled = no
				repair_ship = yes
			}
		}
		end_event_chain = "enigmatic_fortress_chain"
	}
}

# Tower Rebuilt
country_event = {
	id = leviathans.2133
	title = "leviathans.2133.name"
	desc = "leviathans.2133.desc"
	picture = GFX_evt_enigmatic_fortress
	show_sound = event_radio_chatter
	location = event_target:disabled_enigmatic_fortress

	is_triggered_only = yes

	option = {
		name = "leviathans.2133.a"
		custom_tooltip = guardian.fortress.continue
		hidden_effect = {
			country_event = { id = leviathans.2150 days = 30 }
		}
	}
}

# The Pivot
country_event = {
	id = leviathans.2150
	title = "leviathans.2150.name"
	desc = "leviathans.2150.desc"
	picture = GFX_evt_enigmatic_fortress
	show_sound = event_mystic_reveal
	location = event_target:disabled_enigmatic_fortress
	trackable = yes

	is_triggered_only = yes

	option = {
		name = "leviathans.2150.a"
		event_target:disabled_enigmatic_fortress = {
			solar_system = {
				random_system_planet = {
					limit = { is_star = yes }
					enable_special_project = {
						name = "ENIGMATIC_FORTRESS_5B"
						location = this
						owner = ROOT
					}
				}
			}
			if = {
				limit = {
					ROOT = {
						has_country_resource = {
							type = sr_dark_matter
							amount > 10
						}
					}
				}
				enable_special_project = {
					name = "ENIGMATIC_FORTRESS_5C"
					location = this
					owner = ROOT
				}
			}
			enable_special_project = {
				name = "ENIGMATIC_FORTRESS_5D"
				location = this
				owner = ROOT
			}
		}
		add_resource = { sr_dark_matter = -10 }
		capital_scope = {
			closest_system = {
				limit = {
					has_access_fleet = ROOT
					has_planet_class = pc_black_hole
				}
				random_system_planet = {
					limit = { is_planet_class = pc_black_hole }
					enable_special_project = {
						name = "ENIGMATIC_FORTRESS_5A"
						location = this
						owner = ROOT
					}
				}
			}
		}
	}
}

# The Heart of Dark Matter
country_event = {
	id = leviathans.2151
	title = "leviathans.2151.name"
	desc = "leviathans.2151.desc"
	picture = GFX_evt_enigmatic_fortress
	show_sound = event_laboratory_sound
	location = from

	is_triggered_only = yes

	immediate = {
		set_country_flag = fortress_solved
		event_target:disabled_enigmatic_fortress = {
			save_event_target_as = slain_guardian_system
		}
		every_country = {
			limit = {
				is_country_type = default
				has_event_chain = curator_poi_chain
				event_target:slain_guardian_system = {
					is_point_of_interest = {
						id = curator_poi_fortress
						event_chain = curator_poi_chain
						owner = prev
					}
				}
			}
			remove_point_of_interest = curator_poi_fortress
			end_curator_chain = yes
		}
		event_target:disabled_enigmatic_fortress = {
			solar_system = {
				random_system_planet = {
					limit = { is_star = yes }
					create_ambient_object = {
						type = "dead_enigmatic_fortress_object"
						location = solar_system
					}
					last_created_ambient_object = {
						save_global_event_target_as = fortress_ambient
						set_location = {
							target = PREV
							distance = 110
							angle = 110
						}
					}
				}

			}
			fleet = {
				delete_fleet = this
			}
		}
	}

	option = {
		name = "leviathans.2151.a"
		add_monthly_resource_mult = {
			resource = engineering_research
			value = 24
			min = 1000
			max = 5000
		}
		add_monthly_resource_mult = {
			resource = physics_research
			value = 24
			min = 1000
			max = 5000
		}
		add_monthly_resource_mult = {
			resource = society_research
			value = 24
			min = 1000
			max = 5000
		}
		add_research_option = tech_enigmatic_encoder
		add_tech_progress = {
			tech = tech_enigmatic_encoder
			progress = 0.30
		}
		add_research_option = tech_enigmatic_decoder
		add_tech_progress = {
			tech = tech_enigmatic_decoder
			progress = 0.30
		}
		end_event_chain = "enigmatic_fortress_chain"
	}
	after = {
		if = {
			limit = { num_owned_planets > 0 }
			generate_parade_city = yes
			start_situation = {
				type = leviathan_celebration_opportunity
				target = event_target:parade_city
				effect = {
					set_situation_flag = celebration_enigmatic_fortress
					set_situation_flag = standard_unity_reward
				}
			}
		}
	}
}

# Our Star Witness
country_event = {
	id = leviathans.2152
	title = "leviathans.2152.name"
	desc = "leviathans.2152.desc"
	picture = GFX_evt_enigmatic_fortress
	show_sound = event_alien_signal
	location = event_target:disabled_enigmatic_fortress

	is_triggered_only = yes

	option = {
		name = "leviathans.2152.a"
		hidden_effect = { country_event = { id = leviathans.2153 } }
	}
	option = {
		name = "leviathans.2152.b"
		hidden_effect = { country_event = { id = leviathans.2154 } }
	}
	option = {
		name = "leviathans.2152.c"
		hidden_effect = { country_event = { id = leviathans.2155 } }
	}
}

# Fruitless Beginnings
country_event = {
	id = leviathans.2153
	title = "leviathans.2153.name"
	desc = "leviathans.2153.desc"
	picture = GFX_evt_enigmatic_fortress
	show_sound = event_activating_unknown_technology
	location = event_target:disabled_enigmatic_fortress

	is_triggered_only = yes

	option = {
		name = "leviathans.2153.a"
		end_event_chain = "enigmatic_fortress_chain"
		hidden_effect = {
			event_target:disabled_enigmatic_fortress = {
				set_disabled = no
				repair_ship = yes
			}
		}
	}
}

# The Middle Path
country_event = {
	id = leviathans.2154
	title = "leviathans.2154.name"
	desc = "leviathans.2154.desc"
	picture = GFX_evt_enigmatic_fortress
	show_sound = event_laboratory_sound
	location = event_target:disabled_enigmatic_fortress

	is_triggered_only = yes

	immediate = {
		set_country_flag = fortress_solved
		event_target:disabled_enigmatic_fortress = {
			save_event_target_as = slain_guardian_system
		}
		every_country = {
			limit = {
				is_country_type = default
				has_event_chain = curator_poi_chain
				event_target:slain_guardian_system = {
					is_point_of_interest = {
						id = curator_poi_fortress
						event_chain = curator_poi_chain
						owner = prev
					}
				}
			}
			remove_point_of_interest = curator_poi_fortress
			end_curator_chain = yes
		}
		event_target:disabled_enigmatic_fortress = {
			solar_system = {
				random_system_planet = {
					limit = { is_star = yes }
					create_ambient_object = {
						type = "dead_enigmatic_fortress_object"
						location = solar_system
					}
					last_created_ambient_object = {
						save_global_event_target_as = fortress_ambient
						set_location = {
							target = PREV
							distance = 110
							angle = 110
						}
					}
					#create_ambient_object = {
					#	type = explosion_particle_object
					#	location = this
					#	use_3d_location = yes
					#	duration = 10
					#	scale = 20
					#}
					#last_created_ambient_object = {
					#	set_location = {
					#		target = PREV
					#		distance = 110
					#		angle = 110
					#	}
					#}
				}
			}
			fleet = {
				delete_fleet = this
			}
		}
	}

	option = {
		name = "leviathans.2154.a"
		add_monthly_resource_mult = {
			resource = engineering_research
			value = 24
			min = 1000
			max = 5000
		}
		add_monthly_resource_mult = {
			resource = physics_research
			value = 24
			min = 1000
			max = 5000
		}
		add_monthly_resource_mult = {
			resource = society_research
			value = 24
			min = 1000
			max = 5000
		}
		add_research_option = tech_enigmatic_encoder
		add_tech_progress = {
			tech = tech_enigmatic_encoder
			progress = 0.30
		}
		add_research_option = tech_enigmatic_decoder
		add_tech_progress = {
			tech = tech_enigmatic_decoder
			progress = 0.30
		}
		end_event_chain = "enigmatic_fortress_chain"
	}
	after = {
		if = {
			limit = { num_owned_planets > 0 }
			generate_parade_city = yes
			start_situation = {
				type = leviathan_celebration_opportunity
				target = event_target:parade_city
				effect = {
					set_situation_flag = celebration_enigmatic_fortress
					set_situation_flag = standard_unity_reward
				}
			}
		}
	}
}

# The End
country_event = {
	id = leviathans.2155
	title = "leviathans.2155.name"
	desc = "leviathans.2155.desc"
	picture = GFX_evt_enigmatic_fortress
	show_sound = event_activating_unknown_technology
	location = event_target:disabled_enigmatic_fortress

	is_triggered_only = yes

	option = {
		name = "leviathans.2155.a"
		end_event_chain = "enigmatic_fortress_chain"
		hidden_effect = {
			event_target:disabled_enigmatic_fortress = {
				set_disabled = no
				repair_ship = yes
			}
		}
	}
}

# Dark Matter Gambit
country_event = {
	id = leviathans.2156
	title = "leviathans.2156.name"
	desc = "leviathans.2156.desc"
	picture = GFX_evt_enigmatic_fortress
	show_sound = event_laboratory_sound
	location = event_target:disabled_enigmatic_fortress

	is_triggered_only = yes

	immediate = {
		set_country_flag = fortress_solved
		event_target:disabled_enigmatic_fortress = {
			save_event_target_as = slain_guardian_system
		}
		every_country = {
			limit = {
				is_country_type = default
				has_event_chain = curator_poi_chain
				event_target:slain_guardian_system = {
					is_point_of_interest = {
						id = curator_poi_fortress
						event_chain = curator_poi_chain
						owner = prev
					}
				}
			}
			remove_point_of_interest = curator_poi_fortress
			end_curator_chain = yes
		}
		event_target:disabled_enigmatic_fortress = {
			solar_system = {
				random_system_planet = {
					limit = { is_star = yes }
					create_ambient_object = {
						type = "dead_enigmatic_fortress_object"
						location = solar_system
					}
					last_created_ambient_object = {
						save_global_event_target_as = fortress_ambient
						set_location = {
							target = PREV
							distance = 110
							angle = 110
						}
					}
				}

			}
			fleet = {
				delete_fleet = this
			}
		}
	}

	option = {
		name = "leviathans.2156.a"
		add_monthly_resource_mult = {
			resource = engineering_research
			value = 24
			min = 1000
			max = 5000
		}
		add_monthly_resource_mult = {
			resource = physics_research
			value = 24
			min = 1000
			max = 5000
		}
		add_monthly_resource_mult = {
			resource = society_research
			value = 24
			min = 1000
			max = 5000
		}
		add_research_option = tech_enigmatic_encoder
		add_tech_progress = {
			tech = tech_enigmatic_encoder
			progress = 0.30
		}
		add_research_option = tech_enigmatic_decoder
		add_tech_progress = {
			tech = tech_enigmatic_decoder
			progress = 0.30
		}
		end_event_chain = "enigmatic_fortress_chain"
	}
	after = {
		if = {
			limit = { num_owned_planets > 0 }
			generate_parade_city = yes
			start_situation = {
				type = leviathan_celebration_opportunity
				target = event_target:parade_city
				effect = {
					set_situation_flag = celebration_enigmatic_fortress
					set_situation_flag = standard_unity_reward
				}
			}
		}
	}
}

# Shot Through the Heart
country_event = {
	id = leviathans.2157
	title = "leviathans.2157.name"
	desc = "leviathans.2157.desc"
	picture = GFX_evt_exploding_ship
	show_sound = event_super_explosion
	location = event_target:disabled_enigmatic_fortress

	is_triggered_only = yes

	immediate = {
		event_target:disabled_enigmatic_fortress = {
			save_event_target_as = slain_guardian_system
		}
		every_country = {
			limit = {
				is_country_type = default
				has_event_chain = curator_poi_chain
				event_target:slain_guardian_system = {
					is_point_of_interest = {
						id = curator_poi_fortress
						event_chain = curator_poi_chain
						owner = prev
					}
				}
			}
			remove_point_of_interest = curator_poi_fortress
			end_curator_chain = yes
		}
		event_target:disabled_enigmatic_fortress = {
			solar_system = {
				random_system_planet = {
					limit = { is_star = yes }
					create_ambient_object = {
						type = "large_debris_object" # Actually explosion
						location = solar_system
					}
					last_created_ambient_object = {
						save_global_event_target_as = fortress_ambient
						set_location = {
							target = PREV
							distance = 110
							angle = 110
						}
					}
				}

			}
			create_ambient_object = {
				type = explosion_particle_object
				location = this
				use_3d_location = yes
				duration = 10
				scale = 30
			}
			fleet = {
				delete_fleet = this
			}
		}
	}

	option = {
		name = "leviathans.2157.a"
		custom_tooltip = leviathans.2157.a.tooltip
		end_event_chain = "enigmatic_fortress_chain"
		hidden_effect = {
			event_target:disabled_enigmatic_fortress = {
				solar_system = {
					every_system_planet = {
						limit = {
							habitable_planet = yes
							NOR = {
								is_planet_class = pc_ringworld_habitable
								is_planet_class = pc_shattered_ring_habitable
								is_planet_class = pc_habitat
							}
						}
						create_ambient_object = {
							type = explosion_particle_object
							location = this
							use_3d_location = yes
							duration = 10
							scale = 20
						}
						change_pc = pc_nuked
						set_planet_flag = nuked_planet_anomalies_disabled
						reset_planet = yes
					}
					every_system_planet = {
						limit = {
							habitable_planet = no
							is_asteroid = no
							is_star = no
							NOT = { is_planet_class = pc_gas_giant }
						}
						create_ambient_object = {
							type = explosion_particle_object
							location = this
							use_3d_location = yes
							duration = 10
							scale = 20
						}
						change_pc = pc_molten
						reset_planet = yes
					}
					every_fleet_in_system = { destroy_fleet = this }
				}
			}
		}
	}
}

# Fortress Repowers (Generic)
country_event = {
	id = leviathans.2160
	title = "leviathans.2160.name"
	desc = "leviathans.2160.desc"
	picture = GFX_evt_enigmatic_fortress
	show_sound = event_activating_unknown_technology
	location = event_target:disabled_enigmatic_fortress

	is_triggered_only = yes

	option = {
		name = "leviathans.2160.a"
		end_event_chain = "enigmatic_fortress_chain"
		hidden_effect = {
			random_country = {
				limit = { is_country_type = guardian_fortress }
				random_controlled_ship = {
					limit = { has_ship_flag = fortress_vault }
					save_event_target_as = disabled_enigmatic_fortress
				}
			}
			event_target:disabled_enigmatic_fortress = {
				set_disabled = no
				repair_ship = yes
			}
		}
	}
}

# 3000 DIMENSIONAL HORROR

# Entering System
ship_event = {
	id = leviathans.3000
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		FROM = {
			has_star_flag = guardians_horror_system
			NOT = { has_star_flag = guardians_horror_slain }
			OR = {
				NOT = { has_star_flag = horror_spawned }
				any_fleet_in_system = { is_ship_size = dimensional_horror }
			}
		}
	}

	immediate = {
		if = {
			limit = {
				solar_system = {
					NOT = { has_star_flag = horror_spawned }
				}
			}
			solar_system = {
				set_star_flag = horror_spawned
				save_event_target_as = horror_system
				random_system_planet = {
					limit = { is_planet_class = pc_black_hole }
					create_country = {
						name = "NAME_Dimensional_Horror"
						type = guardian_horror
						flag = {
							icon = {
								category = "zoological"
								file = "flag_zoological_3.dds"
							}
							background = {
								category = "backgrounds"
								file = "00_solid.dds"
							}
							colors={
								"red"
								"red"
								"null"
								"null"
							}
						}
					}
					last_created_country = {
						save_global_event_target_as = dimensional_horror_country
						set_country_flag = dimensional_horror_country
					}
					create_fleet = {
						name = "NAME_Dimensional_Horror"
						settings = {
							spawn_debris = no
							is_boss = yes
						}
						effect = {
							set_owner = event_target:dimensional_horror_country
							save_global_event_target_as = dimensional_horror
							create_ship = {
								name = "NAME_Dimensional_Horror"
								design = "NAME_Dimensional_Horror"
								effect = { set_disabled = yes }
							}
							set_location = {
								target = PREV
								distance = 100
								angle = random
							}
						}
					}
					last_created_fleet = {
						create_ambient_object = {
							type = "horror_spawn_object"
							location = THIS
							use_3d_location = yes
							duration = 10
						}
						fleet_event = { id = leviathans.3220 days = 8 }
					}
				}
			}
		}
		solar_system = { save_event_target_as = horror_system }
		owner = {
			country_event = { id = story.8 days = 15 }
			establish_communications_no_message = event_target:dimensional_horror_country
			country_event = { id = leviathans.3001 }
		}
	}
}

# Entering System
country_event = {
	id = leviathans.3001
	title = "leviathans.3001"
	desc = "leviathans.3001.desc"
	picture = GFX_evt_dimensional_horror
	show_sound = event_activating_unknown_technology
	location = event_target:horror_system

	is_triggered_only = yes

	option = {
		name = "leviathans.3001.a"
	}
}

# Horror Destroyed (HIDDEN)
country_event = {
	id = leviathans.3002
	hide_window = yes

	fire_only_once = yes
	is_triggered_only = yes

	trigger = {
		is_country_type = guardian_horror
		OR = {
			has_country_flag = dimensional_horror_country
			has_country_flag = eldritch_horror_country
		}
		FROMFROM = { is_ship_size = dimensional_horror }
	}

	immediate = {
		FROMFROM = {
			solar_system = {
				save_event_target_as = guardians_horror_system
				set_star_flag = guardians_horror_slain
			}
		}
		create_ambient_object = {
			type = "horror_energy_object"
			location = FROMFROM
		}
		last_created_ambient_object = {
			save_event_target_as = horror_energy
			set_ambient_object_flag = horror_energy
		}
		FROM = {
			country_event = { id = leviathans.3003 days = 6 }
		}
		destroy_country = yes
		every_country = {
			limit = {
				is_country_type = default
				has_modifier = dimensional_horror_weak_points
			}
			remove_modifier = "dimensional_horror_weak_points"
		}
		every_country = {
			limit = {
				is_country_type = default
				has_event_chain = curator_poi_chain
				event_target:guardians_horror_system = {
					is_point_of_interest = {
						id = curator_poi_horror
						event_chain = curator_poi_chain
						owner = prev
					}
				}
			}
			remove_point_of_interest = curator_poi_horror
			end_curator_chain = yes
		}
	}
}

# Horror Destroyed
country_event = {
	id = leviathans.3003
	title = "leviathans.3003"
	desc = "leviathans.3003.desc"
	picture = GFX_evt_dimensional_horror
	show_sound = event_super_explosion
	location = event_target:guardians_horror_system
	trackable = yes

	is_triggered_only = yes

	immediate = { set_country_flag = horror_killed }

	option = {
		name = "leviathans.3003.a"
		event_target:horror_energy = {
			enable_special_project = {
				name = "DIMENSIONAL_HORROR_PROJECT"
				location = THIS
				owner = ROOT
			}
		}
	}
	after = {
		if = {
			limit = {
				NOT = { has_country_flag = horrific_celebration }
				num_owned_planets > 0
			}
			set_country_flag = horrific_celebration
			generate_parade_city = yes
			start_situation = {
				type = leviathan_celebration_opportunity
				target = event_target:parade_city
				effect = {
					set_situation_flag = celebration_dimensional_horror
					set_situation_flag = standard_unity_reward
				}
			}
		}
	}
}

# Special Project Completed
ship_event = {
	id = leviathans.3004
	title = "PROJECT_COMPLETE"
	picture = GFX_evt_dimensional_horror
	show_sound = event_laboratory_sound

	desc = {
		text = "leviathans.3004a.desc"
		trigger = {
			owner = {
				NOR = {
					has_technology = tech_jump_drive_1
					has_technology = tech_psi_jump_drive_1
				}
			}
		}
	}
	desc = {
		text = "leviathans.3004b.desc"
		trigger = {
			owner = {
				OR = {
					has_technology = tech_jump_drive_1
					has_technology = tech_psi_jump_drive_1
				}
			}
		}
	}

	is_triggered_only = yes

	immediate = {
		random_ambient_object = {
			limit = { has_ambient_object_flag = horror_energy }
			destroy_ambient_object = this
		}
	}

	option = {
		name = "EXCELLENT"
		trigger = {
			owner = {
				NOR = {
					has_technology = tech_jump_drive_1
					has_technology = tech_psi_jump_drive_1
				}
			}
		}
		owner = {
			add_research_option = tech_jump_drive_1
			add_tech_progress = {
				tech = tech_jump_drive_1
				progress = 0.5
			}
			add_monthly_resource_mult = {
				resource = physics_research
				value = 24
				min = 4000
				max = 6000
			}
			add_monthly_resource_mult = {
				resource = society_research
				value = 24
				min = 4000
				max = 6000
			}
		}
	}
	option = {
		name = "EXCELLENT"
		trigger = {
			owner = {
				OR = {
					has_technology = tech_jump_drive_1
					has_technology = tech_psi_jump_drive_1
				}
			}
		}
		owner = {
			add_monthly_resource_mult = {
				resource = physics_research
				value = 24
				min = 4000
				max = 6000
			}
			add_monthly_resource_mult = {
				resource = society_research
				value = 24
				min = 4000
				max = 6000
			}
		}
	}
}

# 3100 AUTOMATED DREADNOUGHT

# Entering System
ship_event = {
	id = leviathans.3100
	title = "leviathans.3100"
	desc = {
		trigger = { owner = { is_machine_empire = no } }
		text = leviathans.3100.desc
	}
	desc = {
		trigger = { owner = { is_machine_empire = yes } }
		text = leviathans.3100.desc.mach
	}
	picture = GFX_evt_automated_dreadnought
	show_sound = event_radio_chatter
	location = FROM

	is_triggered_only = yes

	trigger = {
		owner = {
			NOT = { has_country_flag = encountered_dreadnought }
		}
		FROM = {
			has_star_flag = guardians_dreadnought_system
			any_fleet_in_system = {
				is_ship_size = npc_warship_01
				owner = { is_country_type = guardian_dreadnought }
			}
		}
	}

	immediate = {
		FROM = { save_event_target_as = dreadnought_system }
		owner = {
			set_country_flag = encountered_dreadnought
		}
	}

	option = {
		name = "leviathans.3100.a"
	}
}

# Dreadnought Disabled (HIDDEN)
country_event = {
	id = leviathans.3101
	hide_window = yes

	fire_only_once = yes
	is_triggered_only = yes

	trigger = {
		is_country_type = guardian_dreadnought
		has_country_flag = automated_dreadnought_country
		FROMFROM = { is_ship_size = npc_warship_01 }
		FROM = { is_ai = no }
	}

	immediate = {
		FROMFROM = {
			solar_system = { save_event_target_as = guardians_dreadnought_system }
			create_ambient_object = {
				type = "dreadnought_object"
				location = THIS
			}
			last_created_ambient_object = { save_global_event_target_as = disabled_dreadnought_object }
			create_ambient_object = {
				type = "large_debris_object"
				location = THIS
			}
			last_created_ambient_object = { set_ambient_object_flag = dreadnought_debris }
		}
		FROM = {
			country_event = { id = leviathans.3102 }
		}
		destroy_country = yes
		every_country = {
			limit = {
				is_country_type = default
				has_modifier = automated_dreadnought_weak_points
			}
			remove_modifier = "automated_dreadnought_weak_points"
		}
		every_country = {
			limit = {
				is_country_type = default
				has_event_chain = curator_poi_chain
				event_target:guardians_dreadnought_system = {
					is_point_of_interest = {
						id = curator_poi_dreadnought
						event_chain = curator_poi_chain
						owner = prev
					}
				}
			}
			remove_point_of_interest = curator_poi_dreadnought
			end_curator_chain = yes
		}
	}
}

# Dreadnought Disabled
country_event = {
	id = leviathans.3102
	title = "leviathans.3102"
	desc = "leviathans.3102.desc"
	picture = GFX_evt_automated_dreadnought
	show_sound = event_ship_bridge
	location = event_target:guardians_dreadnought_system
	trackable = yes

	is_triggered_only = yes

	option = {
		name = "leviathans.3102.a"
		event_target:disabled_dreadnought_object = {
			enable_special_project = {
				name = "DISABLED_DREADNOUGHT_PROJECT"
				location = THIS
				owner = ROOT
			}
		}
	}
	option = {
		name = "leviathans.3102.b"
		add_resource = {
			alloys = 1000
			energy = 5000
		}
		add_monthly_resource_mult = {
			resource = engineering_research
			value = 30
			min = 1000
			max = 5000
		}
	}
	option = {
		name = "leviathans.3102.c"

		trigger = {
			can_harvest_parts = yes
		}

		set_country_flag = harvested_dreadnought_parts
		add_research_option = tech_leviathan_techgenesis
	}
	after = {
		if = {
			limit = {
				num_owned_planets > 0
			}
			generate_parade_city = yes
			start_situation = {
				type = leviathan_celebration_opportunity
				target = event_target:parade_city
				effect = {
					set_situation_flag = celebration_automated_dreadnought
					set_situation_flag = standard_unity_reward
				}
			}
		}
	}
}

# Dreadnought Rebuilt
ship_event = {
	id = leviathans.3103
	title = "leviathans.3103"
	desc = {
		trigger = { owner = { is_machine_empire = no } }
		text = leviathans.3103.desc
	}
	desc = {
		trigger = { owner = { is_machine_empire = yes } }
		text = leviathans.3103.desc.mach
	}
	picture = GFX_evt_automated_dreadnought
	show_sound = event_construction
	location = event_target:guardians_dreadnought_system

	is_triggered_only = yes

	immediate = {
		owner = {
			save_event_target_as = project_owner
			set_country_flag = restored_dreadnought
		}
	}

	option = {
		name = "leviathans.3103.a"

		# Psi Jump Dreadnought if owner has it
		if = {
			limit = {
				owner = { has_technology = "tech_psi_jump_drive_1" }
			}
			create_fleet = {
				name = "NAME_Dreadnought"
				effect = {
					set_owner = event_target:project_owner
					create_ship = {
						name = random
						design = "NAME_Type_37" # Psi Jump Dreadnought
						graphical_culture = "npf_01"
						upgradable = no
					}
					set_location = {
						target = event_target:disabled_dreadnought_object
						distance = 0
						angle = random
					}
				}
			}
			hidden_effect = {
				event_target:disabled_dreadnought_object = { destroy_ambient_object = this }
				solar_system = {
					random_system_ambient_object = {
						limit = { has_ambient_object_flag = dreadnought_debris }
						destroy_ambient_object = this
					}
				}
			}
			break = yes
		}
		# Jump Dreadnought if owner has it
		create_fleet = {
			name = "NAME_Dreadnought"
			effect = {
				set_owner = event_target:project_owner
				create_ship = {
					name = random
					design = "NAME_Type_46" # Jump Dreadnought
					graphical_culture = "npf_01"
					upgradable = no
				}
				set_location = {
					target = event_target:disabled_dreadnought_object
					distance = 0
					angle = random
				}
			}
		}
		hidden_effect = {
			event_target:disabled_dreadnought_object = { destroy_ambient_object = this }
			solar_system = {
				random_system_ambient_object = {
					limit = { has_ambient_object_flag = dreadnought_debris }
					destroy_ambient_object = this
				}
			}
		}
		break = yes
	}
}

# 3200 HIVE ASTEROID

# Built Hive Mine (HIDDEN)
ship_event = {
	id = leviathans.3200
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		FROM = {
			is_planet_class = pc_crystal_asteroid
		}
	}

	immediate = {
		owner = {
			set_country_flag = built_hiver_mine
		}
	}
}

# Hivers Appear (HIDDEN)
country_event = {
	id = leviathans.3210
	hide_window = yes

	fire_only_once = yes

	trigger = {
		is_country_type = default
		has_country_flag = built_hiver_mine
		NOT = { has_country_flag = pacified_hiver_mine }
		any_planet_within_border = {
			OR = {
				has_planet_flag = hiver_asteroid_1
				has_planet_flag = hiver_asteroid_2
				has_planet_flag = hiver_asteroid_3
				has_planet_flag = hiver_asteroid_4
				has_planet_flag = hiver_asteroid_5
			}
			has_mining_station = yes
		}
	}

	mean_time_to_happen = {
		years = 100

		modifier = {
			factor = 0.3
			any_planet_within_border = {
				has_mining_station = yes
				has_planet_flag = hiver_asteroid_1
			}
		}
		modifier = {
			factor = 0.3
			any_planet_within_border = {
				has_mining_station = yes
				has_planet_flag = hiver_asteroid_2
			}
		}
		modifier = {
			factor = 0.3
			any_planet_within_border = {
				has_mining_station = yes
				has_planet_flag = hiver_asteroid_3
			}
		}
		modifier = {
			factor = 0.3
			any_planet_within_border = {
				has_mining_station = yes
				has_planet_flag = hiver_asteroid_4
			}
		}
		modifier = {
			factor = 0.3
			any_planet_within_border = {
				has_mining_station = yes
				has_planet_flag = hiver_asteroid_5
			}
		}
	}

	immediate = {
		random_system = {
			limit = { has_star_flag = guardians_hive_system }
			save_event_target_as = hiver_system
		}
		country_event = { id = leviathans.3201 }
		set_global_flag = hivers_appeared
	}
}

# Hivers Appear
country_event = {
	id = leviathans.3201
	title = "leviathans.3201"
	desc = {
		trigger = {
			NOT = { has_valid_civic = civic_void_hive }
		}
		text = "leviathans.3201.desc"
	}
	desc = {
		trigger = {
			has_valid_civic = civic_void_hive
		}
		text = "leviathans.3201.desc.void_hive"
	}
	picture = GFX_evt_hive
	show_sound = event_red_alert
	location = event_target:hiver_system

	is_triggered_only = yes

	immediate = {
		event_target:hiver_system = {
			random_system_ambient_object = {
				limit = { has_ambient_object_flag = hive_asteroid_1 }
				save_event_target_as = ambient_hive_1
			}
			random_system_ambient_object = {
				limit = { has_ambient_object_flag = hive_asteroid_2 }
				save_event_target_as = ambient_hive_2
			}
			random_system_ambient_object = {
				limit = { has_ambient_object_flag = hive_asteroid_3 }
				save_event_target_as = ambient_hive_3
			}
			random_system_ambient_object = {
				limit = { has_ambient_object_flag = hive_asteroid_4 }
				save_event_target_as = ambient_hive_4
			}
			random_system_ambient_object = {
				limit = { has_ambient_object_flag = hive_asteroid_5 }
				save_event_target_as = ambient_hive_5
			}
			random_system_ambient_object = {
				limit = { has_ambient_object_flag = hive_asteroid_6 }
				save_event_target_as = ambient_hive_6
			}
		}
		create_country = {
			name = "NAME_Alien_Hive"
			type = guardian_hiver
			flag = {
				icon = {
					category = "spherical"
					file = "flag_spherical_6.dds"
				}
				background= {
					category = "backgrounds"
					file = "00_solid.dds"
				}
				colors={
					"red"
					"red"
					"null"
					"null"
				}
			}
		}
		last_created_country = {
			save_global_event_target_as = hive_country
			set_country_flag = hive_country
		}
		every_playable_country = {
			establish_communications_no_message = event_target:hive_country
		}
		create_fleet = {
			name = "NAME_Hiver_Nest"
			settings = { spawn_debris = no }
			effect = {
				set_owner = event_target:hive_country
				create_ship = {
					name = "NAME_Hiver_Nest"
					design = "NAME_Hive_Asteroid"
				}
				set_location = event_target:ambient_hive_1
			}
		}
		event_target:ambient_hive_1 = { destroy_ambient_object = this }
		create_fleet = {
			name = "NAME_Hiver_Nest"
			settings = { spawn_debris = no }
			effect = {
				set_owner = event_target:hive_country
				create_ship = {
					name = "NAME_Hiver_Nest"
					design = "NAME_Hive_Asteroid"
				}
				set_location = event_target:ambient_hive_2
			}
		}
		event_target:ambient_hive_2 = { destroy_ambient_object = this }
		create_fleet = {
			name = "NAME_Hiver_Nest"
			settings = { spawn_debris = no }
			effect = {
				set_owner = event_target:hive_country
				create_ship = {
					name = "NAME_Hiver_Nest"
					design = "NAME_Hive_Asteroid"
				}
				set_location = event_target:ambient_hive_3
			}
		}
		event_target:ambient_hive_3 = { destroy_ambient_object = this }
		create_fleet = {
			name = "NAME_Hiver_Nest"
			settings = { spawn_debris = no }
			effect = {
				set_owner = event_target:hive_country
				create_ship = {
					name = "NAME_Hiver_Nest"
					design = "NAME_Hive_Asteroid"
				}
				set_location = event_target:ambient_hive_4
			}
		}
		event_target:ambient_hive_4 = { destroy_ambient_object = this }
		create_fleet = {
			name = "NAME_Hiver_Nest"
			settings = { spawn_debris = no }
			effect = {
				set_owner = event_target:hive_country
				create_ship = {
					name = "NAME_Hiver_Nest"
					design = "NAME_Hive_Asteroid"
				}
				set_location = event_target:ambient_hive_5
			}
		}
		event_target:ambient_hive_5 = { destroy_ambient_object = this }
		create_fleet = {
			name = "NAME_Hiver_Nest"
			settings = { spawn_debris = no }
			effect = {
				set_owner = event_target:hive_country
				create_ship = {
					name = "NAME_Hiver_Nest"
					design = "NAME_Hive_Asteroid"
				}
				set_location = event_target:ambient_hive_6
			}
		}
		event_target:ambient_hive_6 = { destroy_ambient_object = this }
		set_country_flag = provoked_hivers
	}

	option = {
		name = "leviathans.3201.a"
	}
}

# Entering System
ship_event = {
	id = leviathans.3202
	title = "leviathans.3202"
	desc = {
		trigger = {
			owner = {
				NOT = {
					has_valid_civic = civic_void_hive
				}
			}
		}
		text = leviathans.3202.desc
	}
	desc = {
		trigger = {
			owner = {
				has_valid_civic = civic_void_hive
			}
		}
		text = leviathans.3202.desc.void_hive
	}
	picture = GFX_evt_hive
	show_sound = event_ship_bridge
	location = FROM

	is_triggered_only = yes

	trigger = {
		NOT = { has_global_flag = hivers_appeared }
		owner = {
			is_ai = no
		}
		FROM = {
			has_star_flag = guardians_hive_system
			NOT = { has_star_flag = hivers_defeated }
		}
	}

	immediate = {
		FROM = { save_event_target_as = hiver_system }
	}

	option = {
		trigger = {
			NOT = { owner = { has_valid_civic = civic_void_hive } }
		}
		name = "leviathans.3202.a"
	}
	option = {
		trigger = {
			owner = { has_valid_civic = civic_void_hive }
		}
		name = "leviathans.3202.b"
		solar_system = {
			random_system_planet = {
				limit = {
					is_asteroid = yes
				}
				enable_special_project = {
					name = EXPLOIT_HIVERS
					location = this
					owner = root.owner
				}
			}
		}
		owner = {
			set_timed_country_flag = {
				flag = pacified_hiver_mine
				years = 3
			}
		}
	}
	option = {
		trigger = {
			owner = { has_valid_civic = civic_void_hive }
		}
		name = "leviathans.3202.c"
	}
}

# Hivers Destroyed (HIDDEN)
country_event = {
	id = leviathans.3203
	hide_window = yes

	fire_only_once = yes
	is_triggered_only = yes

	trigger = {
		is_country_type = guardian_hiver
		has_country_flag = hive_country
		count_controlled_ship = {
			limit = { is_ship_size = hive_asteroid }
			count < 1 # count_controlled_ships no longer counts dead ships
		}
		FROM = {
			has_country_flag = provoked_hivers
			is_ai = no
		}
	}

	immediate = {
		random_system = {
			limit = { has_star_flag = guardians_hive_system }
			save_event_target_as = hive_system
			set_star_flag = hivers_defeated
		}
		create_ambient_object = {
			type = "dormant_hive_base_2_object"
			location = fromfrom
			effect = {
				set_ambient_object_flag = boardable_hive_asteroid
				save_event_target_as = boardable_hive_asteroid
			}
		}
		FROM = {
			country_event = { id = leviathans.3204 }
		}
		every_country = {
			limit = {
				is_country_type = default
				has_modifier = asteroid_hive_weak_points
			}
			remove_modifier = "asteroid_hive_weak_points"
		}
		every_country = {
			limit = {
				is_country_type = default
				has_event_chain = curator_poi_chain
				event_target:hive_system = {
					is_point_of_interest = {
						id = curator_poi_hiver
						event_chain = curator_poi_chain
						owner = prev
					}
				}
			}
			remove_point_of_interest = curator_poi_hiver
			end_curator_chain = yes
		}
	}
}

# Hivers Destroyed
country_event = {
	id = leviathans.3204
	title = "leviathans.3204"
	desc = "leviathans.3204.desc"
	picture = GFX_evt_hive
	show_sound = event_scanner
	location = event_target:hive_system
	trackable = yes

	is_triggered_only = yes

	option = {
		name = "leviathans.3204.a"
		event_target:boardable_hive_asteroid = {
			enable_special_project = {
				name = "HIVE_ASTEROID_PROJECT"
				location = THIS
				owner = ROOT
			}
		}
	}
}

# Special Project Completed
ship_event = {
	id = leviathans.3205
	title = "PROJECT_COMPLETE"
	desc = "leviathans.3205.desc"
	picture = GFX_evt_hive
	show_sound = event_airlock
	location = event_target:boardable_hive_asteroid

	is_triggered_only = yes

	option = {
		name = "EXCELLENT"
		owner = {
			add_resource = {
				society_research = 2000
				energy = 500
			}
		}
		hidden_effect = {
			random_ambient_object = {
				limit = { has_ambient_object_flag = boardable_hive_asteroid }
				destroy_ambient_object = this
			}
		}
	}
}

ship_event = {
	id = leviathans.3230
	title = leviathans.3230.name
	desc = leviathans.3230.desc
	picture = GFX_evt_hive
	show_sound = event_crystal_ship
	location = from

	is_triggered_only = yes

	immediate = {
		set_global_flag = hivers_appeared
		event_target:hiver_system = {
			random_system_ambient_object = {
				limit = { has_ambient_object_flag = hive_asteroid_1 }
				save_event_target_as = ambient_hive_1
			}
			random_system_ambient_object = {
				limit = { has_ambient_object_flag = hive_asteroid_2 }
				save_event_target_as = ambient_hive_2
			}
			random_system_ambient_object = {
				limit = { has_ambient_object_flag = hive_asteroid_3 }
				save_event_target_as = ambient_hive_3
			}
			random_system_ambient_object = {
				limit = { has_ambient_object_flag = hive_asteroid_4 }
				save_event_target_as = ambient_hive_4
			}
			random_system_ambient_object = {
				limit = { has_ambient_object_flag = hive_asteroid_5 }
				save_event_target_as = ambient_hive_5
			}
			random_system_ambient_object = {
				limit = { has_ambient_object_flag = hive_asteroid_6 }
				save_event_target_as = ambient_hive_6
			}
		}
		create_country = {
			name = "NAME_Alien_Hive"
			type = guardian_hiver
			flag = {
				icon = {
					category = "spherical"
					file = "flag_spherical_6.dds"
				}
				background= {
					category = "backgrounds"
					file = "00_solid.dds"
				}
				colors={
					"red"
					"red"
					"null"
					"null"
				}
			}
		}
		last_created_country = {
			save_global_event_target_as = hive_country
			set_country_flag = hive_country
			set_faction_hostility = {
				target = root.owner
				set_hostile = no
				set_friendly = yes
			}
		}
		every_playable_country = {
			establish_communications_no_message = event_target:hive_country
		}
		create_fleet = {
			name = "NAME_Hiver_Nest"
			settings = { spawn_debris = no }
			effect = {
				set_owner = event_target:hive_country
				create_ship = {
					name = "NAME_Hiver_Nest"
					design = "NAME_Hive_Asteroid"
				}
				set_location = event_target:ambient_hive_1
			}
		}
		event_target:ambient_hive_1 = { destroy_ambient_object = this }
		create_fleet = {
			name = "NAME_Hiver_Nest"
			settings = { spawn_debris = no }
			effect = {
				set_owner = event_target:hive_country
				create_ship = {
					name = "NAME_Hiver_Nest"
					design = "NAME_Hive_Asteroid"
				}
				set_location = event_target:ambient_hive_2
			}
		}
		event_target:ambient_hive_2 = { destroy_ambient_object = this }
		create_fleet = {
			name = "NAME_Hiver_Nest"
			settings = { spawn_debris = no }
			effect = {
				set_owner = event_target:hive_country
				create_ship = {
					name = "NAME_Hiver_Nest"
					design = "NAME_Hive_Asteroid"
				}
				set_location = event_target:ambient_hive_3
			}
		}
		event_target:ambient_hive_3 = { destroy_ambient_object = this }
		create_fleet = {
			name = "NAME_Hiver_Nest"
			settings = { spawn_debris = no }
			effect = {
				set_owner = event_target:hive_country
				create_ship = {
					name = "NAME_Hiver_Nest"
					design = "NAME_Hive_Asteroid"
				}
				set_location = event_target:ambient_hive_4
			}
		}
		event_target:ambient_hive_4 = { destroy_ambient_object = this }
		create_fleet = {
			name = "NAME_Hiver_Nest"
			settings = { spawn_debris = no }
			effect = {
				set_owner = event_target:hive_country
				create_ship = {
					name = "NAME_Hiver_Nest"
					design = "NAME_Hive_Asteroid"
				}
				set_location = event_target:ambient_hive_5
			}
		}
		event_target:ambient_hive_5 = { destroy_ambient_object = this }
		create_fleet = {
			name = "NAME_Hiver_Nest"
			settings = { spawn_debris = no }
			effect = {
				set_owner = event_target:hive_country
				create_ship = {
					name = "NAME_Hiver_Nest"
					design = "NAME_Hive_Asteroid"
				}
				set_location = event_target:ambient_hive_6
			}
		}
		event_target:ambient_hive_6 = { destroy_ambient_object = this }
	}

	option = {
		name = EXCELLENT
	}
}

# Undisable Horror
fleet_event = {
	id = leviathans.3220
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		owner = {
			random_controlled_ship = {
				set_disabled = no
			}
		}
	}
}
