#########################################
#
# Galactic Features Events
#
# Written by Henrik Eklund
#
#########################################

namespace = galactic_features


### Guardians of Zanaam
ship_event = {
	id = galactic_features.2
	title = "galactic_features.2.name"
	desc = "galactic_features.2.desc"
	
	diplomatic = yes
	
	is_triggered_only = yes
	
	picture_event_data = {
		room = no_video_feed_room
	}
	
	trigger = {
		FROM = { has_star_flag = guardians_of_zanaam }
		owner = {
			NOT = { has_country_flag = guardians_zanaam }
		}
		NOT = { has_global_flag = guardians_zanaam_destroyed }
	}
	
	immediate = {
		owner = { set_country_flag = guardians_zanaam }
		random_country = {
			limit = {
				is_country_type = faction
				has_country_flag = zanaam_country
			}
			establish_contact = { who = root.owner }
			establish_communications_no_message = root.owner
		}
	}
	
	option = {
		name = "galactic_features.2.a"
	}
}

# Guardians destroyed
country_event = {
	id = galactic_features.3
	hide_window = yes
	
	is_triggered_only = yes
	
	trigger = {
		NOT = { has_global_flag = guardians_zanaam_destroyed }
		FROMFROM = { has_fleet_flag = zanaam_fleet }
	}
	
	immediate = {
		set_global_flag = guardians_zanaam_destroyed
	}
}

# Medieval Civilization
country_event = {
	id = galactic_features.101
	title = "primitive.1.name"
	desc = "galactic_features.101.desc"
	picture = GFX_evt_medieval_alien_civilization
	location = FROM
	
	is_triggered_only = yes
	
	option = {
		name = INTERESTING
	}
}

# Industrial Civilization
country_event = {
	id = galactic_features.102
	title = "primitive.1.name"
	desc = "galactic_features.102.desc"
	picture = GFX_evt_metropolis
	location = FROM
	
	is_triggered_only = yes
	
	option = {
		name = INTERESTING
	}
}

# Early Space Age Civilization
country_event = {
	id = galactic_features.103
	title = "primitive.1.name"
	desc = "galactic_features.103.desc"
	picture = GFX_evt_metropolis
	location = FROM
	
	is_triggered_only = yes
	
	option = {
		name = INTERESTING
	}
}

# Nuclear Wasteland
ship_event = {
	id = galactic_features.104
	title = "anomaly.670.name"
	desc = "galactic_features.104.desc"
	picture = GFX_evt_city_ruins
	location = FROM
	
	is_triggered_only = yes
	
	immediate = {
		create_species = {
			name = "Cockroach"
			plural = "Cockroaches"
			class = "ART"
			portrait = pre_art11
			traits = {
				ideal_planet_class = "pc_nuked"
				trait = trait_adaptive
				trait = random_presentient_trait
			}
			homeworld = FROM
			sentient = no
		}
		FROM = {
			best_tile_for_pop = {
				create_pop = { species = last_created }
			}
			best_tile_for_pop = {
				create_pop = { species = last_created }
			}
			best_tile_for_pop = {
				create_pop = { species = last_created }
			}
		}
	}
	
	option = {
		name = INTERESTING
	}
}

# Shielded World (Fallen Empire)
ship_event = {
	id = galactic_features.300
	title = "galactic_features.300"
	desc = "galactic_features.300.desc"
	picture = GFX_evt_ship_in_orbit
	location = FROM
	
	is_triggered_only = yes
	
	trigger = {
		FROM = {
			is_planet_class = pc_shielded
			has_planet_flag = fallen_shield_world
		}
	}
	
	option = {
		name = "galactic_features.300.a"
		from = {
			enable_special_project = {
				name = "FALLEN_SHIELD_PROJECT"
				location = this
				owner = root.owner
			}
		}
	}
	option = {
		name = "galactic_features.300.b"
	}
}

# Mothballed Fleet
ship_event = {
	id = galactic_features.301
	title = "galactic_features.301"
	desc = "galactic_features.301.desc"
	picture = GFX_evt_federation_fleet
	location = FROMFROM
	
	is_triggered_only = yes
	
	immediate = {
		FROMFROM = {
			random_list = {
				50 = { change_pc = pc_barren }
				50 = { change_pc = pc_barren_cold }
			}
		}
		if = {
			limit = {
				FROMFROM = {
					solar_system = { has_star_flag = fallen_shielded_1 }
				}
			}
			random_country = {
				limit = {
					is_country_type = fallen_empire
					has_country_flag = fallen_empire_1
				}
				save_event_target_as = fallen_empire
			}
			break = yes
		}
		if = {
			limit = {
				FROMFROM = {
					solar_system = { has_star_flag = fallen_shielded_2 }
				}
			}
			random_country = {
				limit = {
					is_country_type = fallen_empire
					has_country_flag = fallen_empire_2
				}
				save_event_target_as = fallen_empire
			}
			break = yes
		}
		if = {
			limit = {
				FROMFROM = {
					solar_system = { has_star_flag = fallen_shielded_3 }
				}
			}
			random_country = {
				limit = {
					is_country_type = fallen_empire
					has_country_flag = fallen_empire_3
				}
				save_event_target_as = fallen_empire
			}
			break = yes
		}
	}
	
	option = {
		name = "galactic_features.301.a"
		hidden_effect = {
			owner = {
				create_fleet = { }
				last_created_fleet = {
					set_owner = event_target:fallen_empire
					create_ship = {
						name = random
						design = "Avatar"
						graphical_culture = "fallen_empire_01"
					}
					create_ship = {
						name = random
						design = "Avatar"
						graphical_culture = "fallen_empire_01"
					}
					create_ship = {
						name = random
						design = "Avatar"
						graphical_culture = "fallen_empire_01"
					}
					create_ship = {
						name = random
						design = "Avatar"
						graphical_culture = "fallen_empire_01"
					}
					create_ship = {
						name = random
						design = "Eclipse"
						graphical_culture = "fallen_empire_01"
					}
					create_ship = {
						name = random
						design = "Eclipse"
						graphical_culture = "fallen_empire_01"
					}
					create_ship = {
						name = random
						design = "Eclipse"
						graphical_culture = "fallen_empire_01"
					}
					create_ship = {
						name = random
						design = "Eclipse"
						graphical_culture = "fallen_empire_01"
					}
					create_ship = {
						name = random
						design = "Eclipse"
						graphical_culture = "fallen_empire_01"
					}
					create_ship = {
						name = random
						design = "Eclipse"
						graphical_culture = "fallen_empire_01"
					}
					set_owner = PREV
					set_location = FROMFROMF
				}
			}
		}
	}
}

# Tech Vault
ship_event = {
	id = galactic_features.302
	title = "galactic_features.302"
	desc = "galactic_features.302.desc"
	picture = GFX_evt_outpost
	location = FROMFROM
	
	is_triggered_only = yes
	
	immediate = {
		random_country = {
			limit = {
				is_country_type = fallen_empire
				has_country_flag = fallen_empire_1
			}
			save_event_target_as = fallen_empire
		}
		FROMFROM = { 
			random_list = {
				50 = { change_pc = pc_barren }
				50 = { change_pc = pc_barren_cold }
			}
		}
	}
	
	option = {
		name = "galactic_features.302.a"
	}
}

# Isolated Hut
ship_event = {
	id = galactic_features.303
	title = "galactic_features.303"
	desc = "galactic_features.303.desc"
	picture = GFX_evt_alien_planet
	location = FROMFROM
	
	is_triggered_only = yes
	
	immediate = {
		random_country = {
			limit = {
				is_country_type = fallen_empire
				has_country_flag = fallen_empire_1
			}
			save_event_target_as = fallen_empire
			owner_species = { save_event_target_as = fallen_empire_species }
		}
		FROMFROM = { 
			save_event_target_as = shield_planet 
			change_pc = pc_continental # Should be random habitable
		}
	}
	
	option = {
		name = "galactic_features.303.a"
		hidden_effect = {
			owner = {
				country_event = { id = galactic_features.304 }
			}
		}
	}
}

# The Admiral
country_event = {
	id = galactic_features.304
	title = "TRANSMISSION"
	desc = "galactic_features.304.desc"
	
	diplomatic = yes
	
	picture_event_data = {
		portrait = event_target:fallen_empire
		planet_background = event_target:shield_planet
		city_level = event_target:shield_planet
	}
	
	is_triggered_only = yes
	
	option = {
		name = "galactic_features.304.a"
		response_text = "galactic_features.304.a.response"
		is_dialog_only = yes
	}
	option = {
		name = "galactic_features.304.b"
		response_text = "galactic_features.304.b.response"
		is_dialog_only = yes
	}
	option = {
		name = "galactic_features.304.c"
		response_text = "galactic_features.304.c.response"
		hidden_effect = {
			create_leader = {
				type = admiral
				species = event_target:fallen_empire_species
				name = "Tuborek"
				skill = 5000
				traits = {
					trait = leader_trait_gale_speed
					trait = leader_trait_fleet_logistician
				}
			}
			last_created_leader = {
				set_age = 35 ### Should be older... need some way to control lifespan.
			}
		}
	}
	option = {
		name = "galactic_features.304.d"
		response_text = "galactic_features.304.d.response"
		hidden_effect = { event_target:shield_planet = { change_pc = pc_shielded } }
	}
}

# Freedom
ship_event = {
	id = galactic_features.305
	title = "galactic_features.305"
	desc = "galactic_features.305.desc"
	picture = GFX_evt_alien_planet
	location = FROMFROM
	
	is_triggered_only = yes
	
	immediate = {
		random_country = {
			limit = {
				is_country_type = fallen_empire
				has_country_flag = fallen_empire_1
			}
			save_event_target_as = fallen_empire
		}
		FROMFROM = { 
			random_list = {
				50 = { change_pc = pc_barren }
				50 = { change_pc = pc_barren_cold }
			}
		}
		
		create_country = {
			name = "Old One"
			type = cloud
		}
		last_created_country = {
			create_fleet = { name = "Exiled One" }
			last_created_fleet = {
				set_owner = PREV
				create_ship = {
					name = "XELMAGGUS"
					design = "Cloud Entity"
				}
				set_location = FROMFROM
		
				queue_actions = {
					repeat = {
						find_random_system = {
							trigger = {
								id = "galactic_features.305.trigger.1"
								has_planet_class = "pc_gas_giant"
								distance = {
									source = FROMFROM
									max_distance = 200
									min_distance = 100
								}
							}
							found_system = {
								move_to = THIS
							}	
						}
						find_closest_planet = {
							trigger = {
								id = "galactic_features.305.trigger.2"
								is_planet_class = "pc_gas_giant"
							}
							found_planet = {
								orbit_planet = THIS
								wait = {
									duration = 200
									random = 150
								}
							}
						}
					}
				}
			}
			
			create_fleet = { name = "Exiled One" }
			last_created_fleet = {
				set_owner = PREV
				create_ship = {
					name = "XILNIAK"
					design = "Cloud Entity"
				}
				set_location = FROMFROM
		
				queue_actions = {
					repeat = {
						find_random_system = {
							trigger = {
								id = "galactic_features.305.trigger.3"
								has_planet_class = "pc_gas_giant"
								distance = {
									source = FROMFROMFROM
									max_distance = 200
									min_distance = 100
								}
							}
							found_system = {
								move_to = THIS
							}	
						}
						find_closest_planet = {
							trigger = {
								id = "galactic_features.305.trigger.4"
								is_planet_class = "pc_gas_giant"
							}
							found_planet = {
								orbit_planet = THIS
								wait = {
									duration = 200
									random = 150
								}
							}
						}
					}
				}
			}
			
			create_fleet = { name = "Exiled One" }
			last_created_fleet = {
				set_owner = PREV
				create_ship = {
					name = "XADRA"
					design = "Cloud Entity"
				}
				set_location = FROMFROM
		
				queue_actions = {
					repeat = {
						find_random_system = {
							trigger = {
								id = "galactic_features.305.trigger.5"
								has_planet_class = "pc_gas_giant"
								distance = {
									source = FROMFROM
									max_distance = 200
									min_distance = 100
								}
							}
							found_system = {
								move_to = THIS
							}	
						}
						find_closest_planet = {
							trigger = {
								id = "galactic_features.305.trigger.6"
								is_planet_class = "pc_gas_giant"
							}
							found_planet = {
								orbit_planet = THIS
								wait = {
									duration = 200
									random = 150
								}
							}
						}
					}
				}
			}
			
			create_fleet = { name = "Exiled One" }
			last_created_fleet = {
				set_owner = PREV
				create_ship = {
					name = "XUNIA"
					design = "Cloud Entity"
				}
				set_location = FROMFROM
		
				queue_actions = {
					repeat = {
						find_random_system = {
							trigger = {
								id = "galactic_features.305.trigger.7"
								has_planet_class = "pc_gas_giant"
								distance = {
									source = FROMFROM
									max_distance = 200
									min_distance = 100
								}
							}
							found_system = {
								move_to = THIS
							}	
						}
						find_closest_planet = {
							trigger = {
								id = "galactic_features.305.trigger.8"
								is_planet_class = "pc_gas_giant"
							}
							found_planet = {
								orbit_planet = THIS
								wait = {
									duration = 200
									random = 150
								}
							}
						}
					}
				}
			}
		}
	}
	
	option = {
		name = "galactic_features.305.a"
	}
}

# The Sanctuary
ship_event = {
	id = galactic_features.350
	title = "galactic_features.350"
	desc = "galactic_features.350.desc"
	picture = GFX_evt_star_chart
	location = ROOT
	
	is_triggered_only = yes
	
	trigger = {
		FROM = { 
			has_star_flag = sanctuary_system
			any_ship_in_system = {
				fleet = { has_fleet_flag = sanctuary_core }
			}
			any_planet = {
				exists = owner
				owner = { is_country_type = primitive }
			}
		}
		NOT = { owner = { has_country_flag = sanctuary_system_visited } }
		
	}
	
	immediate = {
		owner = { set_country_flag = sanctuary_system_visited }
	}
	
	option = {
		name = "ONSCREEN"
		hidden_effect = {
			owner = { country_event = { id = galactic_features.351 } }
		}
	}
	option = {
		name = "galactic_features.350.b"
	}
}

# Sanctuary Transmission
country_event = {
	id = galactic_features.351
	title = "TRANSMISSION"
	desc = "galactic_features.351.desc"
	
	diplomatic = yes
	
	picture_event_data = {
		room = no_video_feed_room
	}
	
	is_triggered_only = yes
	
	option = {
		name = "galactic_features.351.a"
		response_text = "galactic_features.351.a.response"
		is_dialog_only = yes
	}
	option = {
		name = "galactic_features.351.b"
		response_text = "galactic_features.351.b.response"
		is_dialog_only = yes
	}
	option = {
		name = "galactic_features.351.c"
		response_text = "galactic_features.351.c.response"
	}
}

# If Void Cloud fleet destroys two fleets, a new Void Cloud is created
country_event = {
	id = galactic_features.360
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = from
		is_country_type = cloud
	}

	immediate = {
		from = { save_event_target_as = notify_country }
		fromfrom = {
			solar_system = {
				random_system_planet = {
					limit = { is_star = yes }
					save_event_target_as = parent_cloud
				}
			}
			if = {
				limit = {
					check_variable = { 
						which = "cloud_strength" 
						value < 1
			 		}
			 	}
			 	set_variable = {
			 		which = "cloud_strength"
			 		value = 1
			 	}
			 	else = {
					change_variable = {
						which = "cloud_strength"
						value = 1
					}
			 		if = {
			 			limit = {
			 				check_variable = {
			 					which = "cloud_strength"
			 					value = 2
			 				}
			 			}
			 			random_list = {
			 				25 = {
			 					create_fleet = { name = "Effluvial Offspring"}
			 					last_created_fleet = {
			 						set_owner = root
			 						create_ship = {
			 							name = "SHAB"
			 							design = "Cloud Entity"
			 						}
			 						set_location = event_target:parent_cloud
			 					}
			 					event_target:notify_country = { country_event = { id = galactic_features.361 } }
			 				}
			 				25 = {
			 					create_fleet = { name = "Effluvial Offspring"}
			 					last_created_fleet = {
			 						set_owner = root
			 						create_ship = {
			 							name = "TAHA"
			 							design = "Cloud Entity"
			 						}
			 						set_location = event_target:parent_cloud
			 					}
			 					event_target:notify_country = { country_event = { id = galactic_features.361 } }
			 				}
			 				25 = {
			 					create_fleet = { name = "Effluvial Offspring"}
			 					last_created_fleet = {
			 						set_owner = root
			 						create_ship = {
			 							name = "TANT"
			 							design = "Cloud Entity"
			 						}
			 						set_location = event_target:parent_cloud
			 					}
			 					event_target:notify_country = { country_event = { id = galactic_features.361 } }
			 				}
			 				25 = {
			 					create_fleet = { name = "Effluvial Offspring"}
			 					last_created_fleet = {
			 						set_owner = root
			 						create_ship = {
			 							name = "KASH"
			 							design = "Cloud Entity"
			 						}
			 						set_location = event_target:parent_cloud
			 					}
			 					event_target:notify_country = { country_event = { id = galactic_features.361 } }
			 				}
			 			}
			 			change_variable = {
			 				which = "cloud_strength"
			 				value = -2
			 			}
			 		}
			 	}
			}
		}
	}
}

# Void Cloud reproduction notification event
country_event = {
	id = galactic_features.361
	title = galactic_features.361.name
	desc = galactic_features.361.desc
	picture = GFX_evt_wormhole
	location = fromfromfrom

	is_triggered_only = yes

	trigger = {
		is_ai = no
		is_country_type = default
		has_communications = from
	}

	option = {
		name = galactic_features.361.a
		trigger = {
			OR = {
				has_ethic = ethic_fanatic_pacifist
				NOT = {
					has_ethic = ethic_fanatic_pacifist
					has_ethic = ethic_fanatic_militarist
					has_ethic = ethic_fanatic_xenophobe
					has_ethic = ethic_fanatic_xenophile
					has_ethic = ethic_fanatic_spiritualist
					has_ethic = ethic_fanatic_materialist
				}
			}
		}
	}

	option = {
		name = galactic_features.361.a.milit
		trigger = { has_ethic = ethic_fanatic_militarist }
	}

	option = {
		name = galactic_features.361.a.phobe
		trigger = { has_ethic = ethic_fanatic_xenophobe }
	}

	option = {
		name = galactic_features.361.a.phile
		trigger = { has_ethic = ethic_fanatic_xenophile }
	}

	option = {
		name = galactic_features.361.a.spiri
		trigger = { has_ethic = ethic_fanatic_spiritualist }
	}

	option = {
		name = galactic_features.361.a.mater
		trigger = { has_ethic = ethic_fanatic_materialist }
	}
}

# Space Monster respawn
event = {
	id = galactic_features.362
	hide_window = yes

	is_triggered_only = yes
 
	immediate = {
		random_system = {
			limit = {
				has_star_flag = blue_system
				NOT = {
					any_ship_in_system = {
						exists = owner
						owner = { is_country_type = crystal }
					}
					any_country = {
						intel_level = {
							level > low
							system = prev
						}
					}
				}
			}

			random_system_planet = {
				limit = { is_star = no }
				create_crystal_country = yes
				event_target:crystal_country = {
					create_fleet = { name = "Crystalline Entity" }
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = self
						set_aggro_range = 150
						set_owner = event_target:crystal_country
						create_ship = {
							name = "Delta"
							design = "Large Crystal Entity Blue"
						}
						create_ship = {
							name = "Epsilon"
							design = "Medium Crystal Entity Blue"
						}
						create_ship = {
							name = "Omega"
							design = "Small Crystal Entity Blue"
						}
						set_location = PREVPREV
					}
				}
			}
		}

		random_system = {
			limit = {
				has_star_flag = blue2_system
				NOT = {
					any_ship_in_system = {
						exists = owner
						owner = { is_country_type = crystal }
					}
					any_country = {
						intel_level = {
							level > low
							system = prev
						}
					}
				}
			}
			
			random_system_planet = {
				limit = {
					is_star = no
				}
				save_event_target_as = blue2_trigger_1
			}
			random_system_planet = {
				limit = {
					is_star = no
					NOT = {
						is_same_value = event_target:blue2_trigger_1
					}
				}
				save_event_target_as = blue2_trigger_2
			}
			random_system_planet = {
				limit = {
					is_star = no
					NOT = {
						is_same_value = event_target:blue2_trigger_1
						is_same_value = event_target:blue2_trigger_2
					}
				}
				save_event_target_as = blue2_trigger_3
				create_crystal_country = yes
				event_target:crystal_country = {
					create_fleet = { name = "Crystalline Entity" }
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = self
						set_aggro_range = 150
						set_owner = event_target:crystal_country
						create_ship = {
							name = "Delta"
							design = "Large Crystal Entity Blue"
						}
						create_ship = {
							name = "Epsilon"
							design = "Medium Crystal Entity Blue"
						}
						create_ship = {
							name = "Omega"
							design = "Small Crystal Entity Blue"
						}
						set_location = PREVPREV
					}
					create_fleet = { name = "Crystalline Entity" }
					last_created_fleet = {
						set_owner = event_target:crystal_country
						create_ship = {
							name = "Delta"
							design = "Large Crystal Entity Blue"
						}
						create_ship = {
							name = "Epsilon"
							design = "Medium Crystal Entity Blue"
						}
						create_ship = {
							name = "Omega"
							design = "Small Crystal Entity Blue"
						}
						set_location = event_target:blue2_trigger_2
					}
					create_fleet = { name = "Crystalline Entity" }
					last_created_fleet = {
						set_owner = event_target:crystal_country
						create_ship = {
							name = "Delta"
							design = "Large Crystal Entity Blue"
						}
						create_ship = {
							name = "Epsilon"
							design = "Medium Crystal Entity Blue"
						}
						create_ship = {
							name = "Omega"
							design = "Small Crystal Entity Blue"
						}
						set_location = event_target:blue2_trigger_1
					}
				}
			}
		}

		random_system = {
			limit = {
				has_star_flag = green_system
				NOT = {
					any_ship_in_system = {
						exists = owner
						owner = { is_country_type = crystal }
					}
					any_country = {
						intel_level = {
							level > low
							system = prev
						}
					}
				}
			}

			random_system_planet = {
				limit = {
					is_star = no
					is_moon = no
				}
				save_event_target_as = green_trigger_1
			}
			random_system_planet = {
				limit = {
					is_star = no
					is_moon = no
					NOT = {
						is_same_value = event_target:green_trigger_1
					}
				}
				save_event_target_as = green_trigger_2
			}
			random_system_planet = {
				limit = {
					is_star = no
					is_moon = no
					NOT = {
						is_same_value = event_target:green_trigger_1
						is_same_value = event_target:green_trigger_2
					}
				}
				save_event_target_as = green_trigger_3
				create_crystal_country = yes
				event_target:crystal_country = {
					create_fleet = { name = "Crystalline Entity" }
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = self
						set_aggro_range = 100
						set_owner = event_target:crystal_country
						create_ship = {
							name = "Delta"
							design = "Large Crystal Entity Green"
						}
						create_ship = {
							name = "Epsilon"
							design = "Medium Crystal Entity Green"
						}
						create_ship = {
							name = "Omega"
							design = "Small Crystal Entity Green"
						}
						set_location = PREVPREV
						queue_actions = {
							repeat = {
								find_closest_planet = {
									trigger = {
										id = "greenC.x1.trigger.1"
										is_planet = event_target:green_trigger_1
									}
									found_planet = {
										orbit_planet = THIS
									}
								}
								find_closest_planet = {
									trigger = {
										id = "greenC.x1.trigger.2"
										is_planet = event_target:green_trigger_2
									}
									found_planet = {
										orbit_planet = THIS
									}
								}
								find_closest_planet = {
									trigger = {
										id = "greenC.x1.trigger.3"
										is_planet = event_target:green_trigger_3
									}
									found_planet = {
										orbit_planet = THIS
									}
								}
							}
						}
					}
				}
			}
		}

		random_system = {
			limit = {
				has_star_flag = green2_system
				NOT = {
					any_ship_in_system = {
						exists = owner
						owner = { is_country_type = crystal }
					}
					any_country = {
						intel_level = {
							level > low
							system = prev
						}
					}
				}
			}

			random_system_planet = {
				limit = {
					is_star = no
				}
				save_event_target_as = green2_trigger_1
			}
			random_system_planet = {
				limit = {
					is_star = no
					NOT = {
						is_same_value = event_target:green2_trigger_1
					}
				}
				save_event_target_as = green2_trigger_2
			}
			random_system_planet = {
				limit = {
					is_star = no
					NOT = {
						is_same_value = event_target:green2_trigger_1
						is_same_value = event_target:green2_trigger_2
					}
				}
				save_event_target_as = green2_trigger_3
				create_crystal_country = yes
				event_target:crystal_country = {
					save_event_target_as = green2_owner
					create_fleet = { name = "Crystalline Entity" }
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = return_point
						set_aggro_range = 40
						set_owner = event_target:crystal_country
						create_ship = {
							name = "Omega"
							design = "Small Crystal Entity Green"
						}
						create_ship = {
							name = "Iota"
							design = "Small Crystal Entity Green"
						}
						create_ship = {
							name = "Mu"
							design = "Small Crystal Entity Green"
						}
						set_location = event_target:green2_trigger_3
						queue_actions = {
							repeat = {
								find_closest_planet = {
									trigger = {
										id = "greenC2.x1.trigger.1"
										is_planet = event_target:green2_trigger_1
									}
									found_planet = {
										orbit_planet = THIS
									}
								}
								find_closest_planet = {
									trigger = {
										id = "greenC2.x1.trigger.2"
										is_planet = event_target:green2_trigger_2
									}
									found_planet = {
										orbit_planet = THIS
									}
								}
								find_closest_planet = {
									trigger = {
										id = "greenC2.x1.trigger.3"
										is_planet = event_target:green2_trigger_3
									}
									found_planet = {
										orbit_planet = THIS
									}
								}
							}
						}
					}
					create_fleet = { name = "Crystalline Entity" }
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = self
						set_aggro_range = 40
						set_owner = event_target:crystal_country
						create_ship = {
							name = "Omega"
							design = "Small Crystal Entity Green"
						}
						create_ship = {
							name = "Iota"
							design = "Small Crystal Entity Green"
						}
						create_ship = {
							name = "Mu"
							design = "Small Crystal Entity Green"
						}
						set_location = event_target:green2_trigger_1
						queue_actions = {
							repeat = {
								find_closest_planet = {
									trigger = {
										id = "greenC2.x2.trigger.2"
										is_planet = event_target:green2_trigger_2
									}
									found_planet = {
										orbit_planet = THIS
									}
								}
								find_closest_planet = {
									trigger = {
										id = "greenC2.x2.trigger.3"
										is_planet = event_target:green2_trigger_3
									}
									found_planet = {
										orbit_planet = THIS
									}
								}
								find_closest_planet = {
									trigger = {
										id = "greenC2.x2.trigger.1"
										is_planet = event_target:green2_trigger_1
									}
									found_planet = {
										orbit_planet = THIS
									}
								}
							}
						}
					}
					create_fleet = { name = "Crystalline Entity" }
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = self
						set_aggro_range = 40
						set_owner = event_target:crystal_country
						create_ship = {
							name = "Omega"
							design = "Small Crystal Entity Green"
						}
						create_ship = {
							name = "Iota"
							design = "Small Crystal Entity Green"
						}
						create_ship = {
							name = "Mu"
							design = "Small Crystal Entity Green"
						}
						set_location = event_target:green2_trigger_2
						queue_actions = {
							repeat = {
								find_closest_planet = {
									trigger = {
										id = "greenC2.x3.trigger.3"
										is_planet = event_target:green2_trigger_3
									}
									found_planet = {
										orbit_planet = THIS
									}
								}
								find_closest_planet = {
									trigger = {
										id = "greenC2.x3.trigger.1"
										is_planet = event_target:green2_trigger_1
									}
									found_planet = {
										orbit_planet = THIS
									}
								}
								find_closest_planet = {
									trigger = {
										id = "greenC2.x3.trigger.2"
										is_planet = event_target:green2_trigger_2
									}
									found_planet = {
										orbit_planet = THIS
									}
								}
							}
						}
					}
				}
			}
		}

		random_system = {
			limit = {
				has_star_flag = red_system
				NOT = {
					any_ship_in_system = {
						exists = owner
						owner = { is_country_type = crystal }
					}
					any_country = {
						intel_level = {
							level > low
							system = prev
						}
					}
				}
			}

			random_system_planet = {
				limit = {
					is_star = no
					is_moon = no
				}
				save_event_target_as = red_trigger_1
			}
			random_system_planet = {
				limit = {
					is_star = no
					is_moon = no
					NOT = {
						is_same_value = event_target:red_trigger_1
					}
				}
				save_event_target_as = red_trigger_2
			}
			random_system_planet = {
				limit = {
					is_star = no
					is_moon = no
					NOT = {
						is_same_value = event_target:red_trigger_1
						is_same_value = event_target:red_trigger_2
					}
				}
				save_event_target_as = red_trigger_3
			}
			random_system_planet = {
				limit = {
					is_star = no
					is_moon = no
					NOT = {
						is_same_value = event_target:red_trigger_1
						is_same_value = event_target:red_trigger_2
						is_same_value = event_target:red_trigger_3
					}
				}
				save_event_target_as = red_trigger_4
				create_crystal_country = yes
				event_target:crystal_country = {
					create_fleet = { name = "Crystalline Entity" }
					save_event_target_as = red_crystals
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = return_point
						set_aggro_range = 40
						set_owner = event_target:crystal_country
						create_ship = {
							name = "Delta"
							design = "Large Crystal Entity Red"
						}
						set_location = event_target:red_trigger_1
						queue_actions = {
							repeat = {
								wait = {
									duration = 9999
								}
							}
						}
					}
					create_fleet = { name = "Crystalline Entity" }
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = return_point
						set_aggro_range = 30
						set_owner = event_target:crystal_country
						create_ship = {
							name = "Epsilon"
							design = "Large Crystal Entity Red"
						}
						set_location = event_target:red_trigger_2
						queue_actions = {
							repeat = {
								wait = {
									duration = 9999
								}
							}
						}
					}
					create_fleet = { name = "Crystalline Entity" }
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = return_point
						set_aggro_range = 40
						set_owner = event_target:crystal_country
						create_ship = {
							name = "Omega"
							design = "Large Crystal Entity Red"
						}
						set_location = event_target:red_trigger_3
						queue_actions = {
							repeat = {
								wait = {
									duration = 9999
								}
							}
						}
					}
					create_fleet = { name = "Crystalline Entity" }
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = return_point
						set_aggro_range = 30
						set_owner = event_target:crystal_country
						create_ship = {
							name = "Alpha"
							design = "Large Crystal Entity Red"
						}
						set_location = event_target:red_trigger_4
						queue_actions = {
							repeat = {
								wait = {
									duration = 9999
								}
							}
						}
					}
				}
			}
		}

		random_system = {
			limit = {
				has_star_flag = red2_system
				NOT = {
					any_ship_in_system = {
						exists = owner
						owner = { is_country_type = crystal }
					}
					any_country = {
						intel_level = {
							level > low
							system = prev
						}
					}
				}
			}

			random_system_planet = {
				limit = {
					is_star = no
					is_moon = no
				}
				save_event_target_as = red2_trigger_1
			}
			random_system_planet = {
				limit = {
					is_star = no
					is_moon = no
					NOT = {
						is_same_value = event_target:red2_trigger_1
					}
				}
				save_event_target_as = red2_trigger_2
			}
			random_system_planet = {
				limit = {
					is_star = no
					is_moon = no
					NOT = {
						is_same_value = event_target:red2_trigger_1
						is_same_value = event_target:red2_trigger_2
					}
				}
				save_event_target_as = red2_trigger_3
			}
			random_system_planet = {
				limit = {
					is_star = no
					is_moon = no
					NOT = {
						is_same_value = event_target:red2_trigger_1
						is_same_value = event_target:red2_trigger_2
						is_same_value = event_target:red2_trigger_3
					}
				}
				save_event_target_as = red2_trigger_4
				create_crystal_country = yes
				event_target:crystal_country = {
					create_fleet = { name = "Crystalline Entity" }
					save_event_target_as = red2_crystals
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = return_point
						set_aggro_range = 100
						set_owner = event_target:crystal_country
						create_ship = {
							name = "Delta"
							design = "Large Crystal Entity Red"
						}
						create_ship = {
							name = "Omicron"
							design = "Medium Crystal Entity Red"
						}
						create_ship = {
							name = "Kappa"
							design = "Medium Crystal Entity Red"
						}
						create_ship = {
							name = "Lambda"
							design = "Small Crystal Entity Red"
						}
						create_ship = {
							name = "Tau"
							design = "Small Crystal Entity Red"
						}
						set_location = event_target:red2_trigger_1
						queue_actions = {
							repeat = {
								wait = {
									duration = 9999
								}
							}
						}
					}
					create_fleet = { name = "Crystalline Entity" }
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = return_point
						set_aggro_range = 100
						set_owner = event_target:crystal_country
						create_ship = {
							name = "Epsilon"
							design = "Large Crystal Entity Red"
						}
						create_ship = {
							name = "Omicron"
							design = "Medium Crystal Entity Red"
						}
						create_ship = {
							name = "Kappa"
							design = "Medium Crystal Entity Red"
						}
						create_ship = {
							name = "Lambda"
							design = "Small Crystal Entity Red"
						}
						create_ship = {
							name = "Tau"
							design = "Small Crystal Entity Red"
						}
						set_location = event_target:red2_trigger_2
						queue_actions = {
							repeat = {
								wait = {
									duration = 9999
								}
							}
						}
					}
					create_fleet = { name = "Crystalline Entity" }
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = return_point
						set_aggro_range = 100
						set_owner = event_target:crystal_country
						create_ship = {
							name = "Omega"
							design = "Large Crystal Entity Red"
						}
						create_ship = {
							name = "Omicron"
							design = "Medium Crystal Entity Red"
						}
						create_ship = {
							name = "Kappa"
							design = "Medium Crystal Entity Red"
						}
						create_ship = {
							name = "Lambda"
							design = "Small Crystal Entity Red"
						}
						create_ship = {
							name = "Tau"
							design = "Small Crystal Entity Red"
						}
						set_location = event_target:red2_trigger_3
						queue_actions = {
							repeat = {
								wait = {
									duration = 9999
								}
							}
						}
					}
					create_fleet = { name = "Crystalline Entity" }
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = return_point
						set_aggro_range = 100
						set_owner = event_target:crystal_country
						create_ship = {
							name = "Alpha"
							design = "Large Crystal Entity Red"
						}
						create_ship = {
							name = "Omicron"
							design = "Medium Crystal Entity Red"
						}
						create_ship = {
							name = "Kappa"
							design = "Medium Crystal Entity Red"
						}
						create_ship = {
							name = "Lambda"
							design = "Small Crystal Entity Red"
						}
						create_ship = {
							name = "Tau"
							design = "Small Crystal Entity Red"
						}
						set_location = event_target:red2_trigger_4
						queue_actions = {
							repeat = {
								wait = {
									duration = 9999
								}
							}
						}
					}
				}
			}
		}

		random_system = {
			limit = {
				has_star_flag = elite_system
				NOT = {
					any_ship_in_system = {
						exists = owner
						owner = { is_country_type = crystal }
					}
					any_country = {
						intel_level = {
							level > low
							system = prev
						}
					}
				}
			}
			
			random_system_planet = {
				limit = { is_star = yes }
				create_crystal_country = yes
				event_target:crystal_country = {
					save_event_target_as = elite1_owner
					create_fleet = { name = "Crystalline Elites" }
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = self
						set_aggro_range = 500
						set_owner = event_target:crystal_country
						create_ship = {
							name = "Delta"
							design = "Large Crystal Entity Red Elite"
						}
						create_ship = {
							name = "Epsilon"
							design = "Medium Crystal Entity Blue Elite"
						}
						create_ship = {
							name = "Omega"
							design = "Small Crystal Entity Yellow Elite"
						}
						create_ship = {
							name = "Beta"
							design = "Large Crystal Entity Blue Elite"
						}
						create_ship = {
							name = "Gamma"
							design = "Medium Crystal Entity Yellow Elite"
						}
						create_ship = {
							name = "Psi"
							design = "Small Crystal Entity Red Elite"
						}
						set_location = PREVPREV
					}
					create_fleet = { name = "Crystalline Elites" }
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = self
						set_aggro_range = 500
						set_owner = event_target:crystal_country
						create_ship = {
							name = "Delta"
							design = "Large Crystal Entity Yellow Elite"
						}
						create_ship = {
							name = "Epsilon"
							design = "Medium Crystal Entity Red Elite"
						}
						create_ship = {
							name = "Omega"
							design = "Small Crystal Entity Blue Elite"
						}
						create_ship = {
							name = "Beta"
							design = "Large Crystal Entity Red Elite"
						}
						create_ship = {
							name = "Gamma"
							design = "Medium Crystal Entity Yellow Elite"
						}
						create_ship = {
							name = "Psi"
							design = "Small Crystal Entity Blue Elite"
						}
						set_location = PREVPREV
					}
					create_fleet = { name = "Crystalline Elites" }
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = self
						set_aggro_range = 500
						set_owner = event_target:crystal_country
						create_ship = {
							name = "Delta"
							design = "Large Crystal Entity Blue Elite"
						}
						create_ship = {
							name = "Epsilon"
							design = "Medium Crystal Entity Red Elite"
						}
						create_ship = {
							name = "Omega"
							design = "Small Crystal Entity Yellow Elite"
						}
						create_ship = {
							name = "Beta"
							design = "Large Crystal Entity Yellow Elite"
						}
						create_ship = {
							name = "Gamma"
							design = "Medium Crystal Entity Blue Elite"
						}
						create_ship = {
							name = "Psi"
							design = "Small Crystal Entity Red Elite"
						}
						set_location = PREVPREV
					}
				}
			}
		}

		random_system = {
			limit = {
				has_star_flag = void_system
				NOT = {
					any_ship_in_system = {
						exists = owner
						owner = { is_country_type = cloud }
					}
					any_country = {
						intel_level = {
							level > low
							system = prev
						}
					}
				}
			}

			random_system_planet = {
				limit = { is_star = no }
				create_cloud_country = yes
				event_target:cloud_country = {
					set_aggro_range = 500
					create_fleet = { name = "Void Cloud" }
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = self
						set_aggro_range = 500
						set_owner = event_target:cloud_country
						create_ship = {
							name = "XUUL"
							design = "Cloud Entity"
						}
						set_location = PREVPREV
					}
				}
			}
		}

		random_system = {
			limit = {
				has_star_flag = void2_system
				NOT = {
					any_ship_in_system = {
						exists = owner
						owner = { is_country_type = cloud }
					}
					any_country = {
						intel_level = {
							level > low
							system = prev
						}
					}
				}
			}

			random_system_planet = {
				limit = { is_star = yes }
				save_event_target_as = voidhome_star
				create_cloud_country = yes
				event_target:cloud_country = {
					save_event_target_as = voidhome_owner
					create_fleet = { name = "Abyssal Host" }
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = return_point
						set_aggro_range = 150
						set_owner = event_target:cloud_country
						create_ship = {
							name = "YAQU"
							design = "Cloud Entity"
						}
						create_ship = {
							name = "SHAR"
							design = "Cloud Entity"
						}
						create_ship = {
							name = "APED"
							design = "Cloud Entity"
						}
						create_ship = {
							name = "HAPI"
							design = "Cloud Entity"
						}
						set_location = event_target:voidhome_star
					}
					create_fleet = { name = "Chasmal Horde" }
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = return_point
						set_aggro_range = 150
						set_owner = event_target:cloud_country
						create_ship = {
							name = "ANED"
							design = "Cloud Entity"
						}
						create_ship = {
							name = "DJER"
							design = "Cloud Entity"
						}
						create_ship = {
							name = "SEME"
							design = "Cloud Entity"
						}
						create_ship = {
							name = "MERN"
							design = "Cloud Entity"
						}
						set_location = event_target:voidhome_star
					}
				}
			}
		}

		random_system = {
			limit = {
				has_star_flag = amoeba_system
				NOT = {
					any_ship_in_system = {
						exists = owner
						owner = { is_country_type = amoeba }
					}
					any_country = {
						intel_level = {
							level > low
							system = prev
						}
					}
				}
			}

			random_system_planet = {
				limit = { is_star = no }
				create_amoeba_country = yes
				event_target:amoeba_country = {
					create_fleet = { name = "Space Amoeba" }
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = self
						set_aggro_range = 100
						set_owner = event_target:amoeba_country
						create_ship = {
							name = "Ortus"
							design = "Large Space Organism"
						}
						create_ship = {
							name = "Oriens"
							design = "Large Space Organism"
						}
						create_ship = {
							name = "Cortus"
							design = "Large Space Organism"
						}
						set_location = PREVPREV
					}
				}
			}
		}


		random_system = {
			limit = {
				has_star_flag = amoeba2_system
				NOT = {
					any_ship_in_system = {
						exists = owner
						owner = { is_country_type = amoeba }
					}
					any_country = {
						intel_level = {
							level > low
							system = prev
						}
					}
				}
			}

			random_system_planet = {
				limit = { is_star = no }
				save_event_target_as = grazing_grounds
				create_amoeba_country = yes
				event_target:amoeba_country = {
					create_fleet = { name = "Space Amoeba" }
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = self
						set_aggro_range = 20
						set_owner = event_target:amoeba_country
						random_list = {
							10 = {
								set_fleet_flag = empire_visitor
							}
							20 = {
								set_fleet_flag = tiyanki_hunter
							}
							70 = {
								set_fleet_flag = empty_system
							}
						}
						create_ship = {
							name = "Ortus"
							design = "Great Space Organism"
						}
						set_location = PREVPREV

						if = {
							limit = { has_fleet_flag = empire_visitor }
							queue_actions = {
								repeat = {
									find_random_system = {
										trigger = {
											id = amoeba.x3.trigger.1
											any_planet = { is_capital = yes }
										}
										found_system = {
											move_to = this
										}
									}
									find_closest_planet = {
										trigger = {
											id = amoeba.x3.trigger.2
											is_star = yes
										}
										found_planet = {
											orbit_planet = this
											wait = {
												duration = 30
												random = 10
											}
										}
									}
									find_random_system = {
										trigger = {
											id = amoeba.x3.trigger.3
											closest_system = {
												any_planet = {
													is_planet = event_target:grazing_grounds
												}
											}
										}
									}
									find_closest_planet = {
										trigger = {
											id = amoeba.x3.trigger.4
											is_planet = event_target:grazing_grounds
										}
										found_planet = {
											orbit_planet = this
											wait = {
												duration = 400
												random = 150
											}
										}
									}
								}
							}
							else = {
								if = {
									limit = { has_fleet_flag = tiyanki_hunter }
									save_event_target_as = hunting_amoeba
									queue_actions = {
										repeat = {
											find_random_system = {
												trigger = {
													id = amoeba.x2.trigger.1
													closest_system = {
														any_ship_in_system = {
															is_ship_size = space_whale_3
															# hunt calves
														}
													}
												}
												found_system = {
													move_to = this
												}
											}
											find_closest_planet = {
												trigger = {
													id = amoeba.x2.trigger.2
													is_star = yes
												}
												found_planet = {
													effect = { 
														id = amoeba.x2.effect.1
														event_target:hunting_amoeba = { set_aggro_range = 500 }
													}
													orbit_planet = this
													wait = {
														duration = 300
														random = 10
													}
												}
											}
											find_random_system = {
												trigger = {
													id = amoeba.x2.trigger.3
													closest_system = {
														any_planet = {
															is_planet = event_target:grazing_grounds
														}
													}
												}
												found_system = {
													move_to = this
												}
											}
											find_closest_planet = {
												trigger = {
													id = amoeba.x2.trigger.4
													is_planet = event_target:grazing_grounds
												}
												found_planet = {
													effect = { 
														id = amoeba.x2.effect.2
														event_target:hunting_amoeba = { set_aggro_range = 40 }
													}
													orbit_planet = this
													wait = {
														duration = 400
														random = 150
													}
												}
											}
										}
									}
									else = {
										queue_actions = {
											repeat = {
												find_random_system = {
													trigger = {
														id = amoeba.x1.trigger.1
														closest_system = {
															NOT = { has_star_flag = hostile_system }
														}
													}
													found_system = {
														move_to = this
													}
												}
												find_closest_planet = {
													trigger = {
														id = amoeba.x1.trigger.2
														is_star = yes
													}
													found_planet = {
														orbit_planet = this
														wait = {
															duration = 100
															random = 50
														}
													}
												}
												find_random_system = {
													trigger = {
														id = amoeba.x1.trigger.3
														closest_system = {
															any_planet = {
																is_planet = event_target:grazing_grounds
															}
														}
													}
												}
												find_closest_planet = {
													trigger = {
														id = amoeba.x1.trigger.4
														is_planet = event_target:grazing_grounds
													}
													found_planet = {
														orbit_planet = this
														wait = {
															duration = 300
															random = 50
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}


		random_system = {
			limit = {
				has_star_flag = drone_system
				NOT = {
					any_ship_in_system = {
						exists = owner
						owner = { is_country_type = drone }
					}
					any_country = {
						intel_level = {
							level > low
							system = prev
						}
					}
				}
			}

			random_system_planet = {
				limit = { has_deposit_for = shipclass_mining_station }
				create_drone_country = yes
				event_target:drone_country = {
					save_event_target_as = mining_drones
					set_graphical_culture = ancient_drone_01
					create_fleet = { name = "Ancient Mining Drones" }
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = return_point
						set_aggro_range = 150
						set_owner = event_target:drone_country
						create_ship = {
							name = "Drone 2432-D"
							design = "Mining"
						}
						create_ship = {
							name = "Drone 7731-A"
							design = "Mining"
						}
						create_ship = {
							name = "Drone 4119-X"
							design = "Mining"
						}
						set_location = PREVPREV
						queue_actions = {
							repeat = {
								wait = {
									duration = 999
									random = 10
								}
							}
						}
					}
				}
				create_mining_station = {
					owner = event_target:mining_drones
				}
			}
		}

		random_system = {
			limit = {
				has_star_flag = drone2_system
				NOT = {
					any_ship_in_system = {
						exists = owner
						owner = { is_country_type = drone }
					}
					any_country = {
						intel_level = {
							level > low
							system = prev
						}
					}
				}
			}

			every_system_planet = {
				limit = { has_deposit_for = shipclass_mining_station }
				create_drone_country = yes
				event_target:drone_country = {
					save_event_target_as = mining_drones2
					set_graphical_culture = ancient_drone_01
					create_fleet = { name = "Ancient Mining Drones" }
					last_created_fleet = {
						set_fleet_stance = aggressive
						set_aggro_range_measure_from = return_point
						set_aggro_range = 200
						set_owner = event_target:drone_country
						create_ship = {
							name = "Drone 1832-D"
							design = "Mining"
						}
						create_ship = {
							name = "Drone 0931-F"
							design = "Mining"
						}
						create_ship = {
							name = "Drone 1119-X"
							design = "Mining"
						}
						set_location = PREVPREV
					}
				}
				create_mining_station = {
					owner = event_target:mining_drones2
				}
			}
		}
	}
}