#This Effect generates A Cluster of Stars totaly isolated from any other cluster.
#It marks every system in the clusster with the star flag f93_current_cluster.
#use this flag to target the systems for additional effects then remove it from all systems.
#
f93_random_cluster_generator = {
	random_list = {
		50 = { # up to 3 systems
			while = {
				count = 3
				random_neighbor_system_euclidean = {
					limit = {
						has_star_flag = f93_shroud_region_flag
						f93_is_in_a_cluster = no
						distance = {
							source = prev
							max_distance <= 80
							type = euclidean
						}
					}
					if = {
						limit = { NOT = { has_hyperlane_to = prev } }
						add_hyperlane = { from = this to = prev }
					}
					set_star_flag = f93_shrouded_cluster
					set_star_flag = f93_current_cluster
				}
			}
		}
		50 = { # up to 4 systems
			while = {
				count = 4
				random_neighbor_system_euclidean = {
					limit = {
						has_star_flag = f93_shroud_region_flag
						f93_is_in_a_cluster = no
						distance = {
							source = prev
							max_distance <= 80
							type = euclidean
						}
					}
					if = {
						limit = { NOT = { has_hyperlane_to = prev } }
						add_hyperlane = { from = this to = prev }
					}
					set_star_flag = f93_shrouded_cluster
					set_star_flag = f93_current_cluster
				}
			}
		}
		15 = { #up to 6 systems
			while = {
				count = 3
				random_neighbor_system_euclidean = {
					limit = {
						has_star_flag = f93_shroud_region_flag
						f93_is_in_a_cluster = no
						distance = {
							source = prev
							max_distance <= 120
							type = euclidean
						}
					}
					if = {
						limit = { NOT = { has_hyperlane_to = prev } }
						add_hyperlane = { from = this to = prev }
					}
					set_star_flag = f93_current_cluster
					set_star_flag = f93_shrouded_cluster
					random_neighbor_system_euclidean = {
						limit = {
							has_star_flag = f93_shroud_region_flag
							calc_true_if = {
								amount = 1
								NOT = { has_star_flag = f93_shrouded_cluster }
								has_star_flag = f93_current_cluster
							}
							distance = {
								source = prev
								max_distance <= 80
								type = euclidean
							}
						}
						if = {
							limit = { NOT = { has_hyperlane_to = prev } }
							add_hyperlane = { from = this to = prev }
						}
						set_star_flag = f93_current_cluster
						set_star_flag = f93_shrouded_cluster
					}
				}
			}
		}
		25 = { #up to 5 systems
			while = {
				count = 5
				random_neighbor_system_euclidean = {
					limit = {
						has_star_flag = f93_shroud_region_flag
						f93_is_in_a_cluster = no
						distance = {
							source = prev
							max_distance <= 80
							type = euclidean
						}
					}
					if = {
						limit = { NOT = { has_hyperlane_to = prev } }
						add_hyperlane = { from = this to = prev }
					}
					set_star_flag = f93_shrouded_cluster
					set_star_flag = f93_current_cluster
				}
			}
		}
		10 = { #up to 8 systems
			while = {
				count = 2
				random_neighbor_system_euclidean = {
					limit = {
						has_star_flag = f93_shroud_region_flag
						f93_is_in_a_cluster = no
						distance = {
							source = prev
							max_distance <= 120
							type = euclidean
						}
					}
					if = {
						limit = { NOT = { has_hyperlane_to = prev } }
						add_hyperlane = { from = this to = prev }
					}
					set_star_flag = f93_current_cluster
					set_star_flag = f93_shrouded_cluster
					while = {
						count = 2
						random_neighbor_system_euclidean = {
							limit = {
								has_star_flag = f93_shroud_region_flag
								calc_true_if = {
									amount = 1
									NOT = { has_star_flag = f93_shrouded_cluster }
									has_star_flag = f93_current_cluster
								}
								distance = {
									source = prev
									max_distance <= 80
									type = euclidean
								}
							}
							if = {
								limit = { NOT = { has_hyperlane_to = prev } }
								add_hyperlane = { from = this to = prev }
							}
							set_star_flag = f93_current_cluster
							set_star_flag = f93_shrouded_cluster
						}
					}
					random_neighbor_system_euclidean = {
						limit = {
							has_star_flag = f93_shroud_region_flag
							calc_true_if = {
								amount = 1
								NOT = { has_star_flag = f93_shrouded_cluster }
								has_star_flag = f93_current_cluster
							}
							distance = {
								source = prev
								max_distance <= 80
								type = euclidean
							}
						}
						if = {
							limit = { NOT = { has_hyperlane_to = prev } }
							add_hyperlane = { from = this to = prev }
						}
						set_star_flag = f93_current_cluster
						set_star_flag = f93_shrouded_cluster
						random_neighbor_system_euclidean = {
							limit = {
								has_star_flag = f93_shroud_region_flag
								calc_true_if = {
									amount = 1
									NOT = { has_star_flag = f93_shrouded_cluster }
									has_star_flag = f93_current_cluster
								}
								distance = {
									source = prev
									max_distance <= 80
									type = euclidean
								}
							}
							if = {
								limit = { NOT = { has_hyperlane_to = prev } }
								add_hyperlane = { from = this to = prev }
							}
							set_star_flag = f93_current_cluster
							set_star_flag = f93_shrouded_cluster
						}
					}
				}
			}
		}
	}
}
f93_place_shroud_gate_node = {
	set_star_flag = Womrhole_maker_1
	spawn_natural_wormhole = {
		bypass_type = ralisgate
		random_pos = yes
		orbit_angle = 360
	}
	set_star_flag = f93_ralisgate
	set_star_flag = f93_got_shroud_nexus_node
}
f93_place_shroud_gate_nexus = {
	set_star_flag = Womrhole_maker_2
	spawn_natural_wormhole = {
		bypass_type = ralisgate
		random_pos = yes
		orbit_angle = 360
	}
	set_star_flag = f93_raliscluster1
	set_star_flag = f93_got_shroud_nexus_node
}

f93_place_shroud_gate_node_and_cluster = {
	f93_random_cluster_generator = yes
	random_system = {
		limit = { has_star_flag = f93_current_cluster }
	set_star_flag = Womrhole_maker_3
		spawn_natural_wormhole = {
			bypass_type = ralisgate
			random_pos = yes
			orbit_angle = 360
		}
		set_star_flag = f93_ralisgate
		set_star_flag = f93_ralisgate_cluster
	}
	every_system = {
		limit = { has_star_flag = f93_current_cluster }
		remove_star_flag = f93_current_cluster
		remove_star_flag = f93_shrouded_cluster
		set_star_flag = f93_ralisgate_cluster
	}
}

f93_place_shroud_hyperlane_in_isolated_wormhole_region = {
	every_system = {
		limit = {
			f93_is_in_a_cluster = no
			has_natural_wormhole = yes
		}
		set_star_flag = f93_current_cluster
		random_neighbor_system_euclidean = {
			limit = {
				distance = {
					source = prev
					max_distance <= 80
					type = euclidean
				}
			}
			f93_place_shroud_gate_node_and_cluster = yes
			if = {
				limit = { NOT = { has_hyperlane_to = prev } }
				add_hyperlane = { from = this to = prev }
			}
		}
	}
}
f93_connect_system_with_GalCro_within_20_steps_and_generate_cluster = {
	set_star_flag = f93_shrouded_cluster
	f93_random_cluster_generator = yes
	random_system = {
		limit = { has_star_flag = f93_current_cluster }
		random_system = {
			limit = {
				NOT = { exists = owner }
				# f93_system_winthin_20_steps_of_galcro = yes
				has_natural_wormhole = no
				has_star_flag = normal_region
			}
			prev = {
				set_star_flag = Womrhole_maker_4
				spawn_natural_wormhole = {
					bypass_type = wormhole
					random_pos = yes
					orbit_angle = 360
				}
			}
			set_star_flag = Womrhole_maker_5
			spawn_natural_wormhole = {
				bypass_type = wormhole
				random_pos = yes
				orbit_angle = 360
			}
			link_wormholes = prev
		}
	}
	every_system = { remove_star_flag = f93_current_cluster }
}

f93_connect_current_system_with_GalCro_within_20_steps_back_up = {
	solar_system = {
		random_system = {
			limit = {
				NOT = { exists = owner }
				f93_system_winthin_20_steps_of_galcro = yes
				has_natural_wormhole = no
			}
			prev = {
				set_star_flag = Womrhole_maker_6
				spawn_natural_wormhole = {
					bypass_type = wormhole
					random_pos = yes
					orbit_angle = 360
				}
			}
			set_star_flag = Womrhole_maker_7
			spawn_natural_wormhole = {
				bypass_type = wormhole
				random_pos = yes
				orbit_angle = 360
			}
			link_wormholes = prev
		}
	}
}

f93_connect_SPECIFIC_system_with_GalCro_within_20_steps_and_generate_cluster = {
	set_star_flag = f93_shrouded_cluster
	f93_random_cluster_generator = yes
	random_system = {
		limit = { has_star_flag = f93_current_cluster }
		event_target:connection_origin = {
			prev = {
				set_star_flag = Womrhole_maker_8
				spawn_natural_wormhole = {
					bypass_type = wormhole
					random_pos = yes
					orbit_angle = 360
				}
			}
			set_star_flag = Womrhole_maker_9
			spawn_natural_wormhole = {
				bypass_type = wormhole
				random_pos = yes
				orbit_angle = 360
			}
			link_wormholes = prev
		}
	}
	every_system = { remove_star_flag = f93_current_cluster }
}

f93_mark_highway_systems = {
	every_system = {
		limit = {
			f93_system_has_ancient_relay = yes
		}
		set_star_flag = normal_region
		every_neighbor_system = {
			limit = {
				NOT = { has_star_flag = normal_region }
			}
			set_star_flag = normal_region
			every_neighbor_system = {
				limit = {
					NOT = { has_star_flag = normal_region }
				}
				set_star_flag = normal_region
				every_neighbor_system = {
					limit = {
						NOT = { has_star_flag = normal_region }
					}
					set_star_flag = normal_region
					every_neighbor_system = {
						limit = {
							NOT = { has_star_flag = normal_region }
						}
						set_star_flag = normal_region
						every_neighbor_system = {
							limit = {
								NOT = { has_star_flag = normal_region }
							}
							set_star_flag = normal_region
							every_neighbor_system = {
								limit = {
									NOT = { has_star_flag = normal_region }
								}
								set_star_flag = normal_region
								every_neighbor_system = {
									limit = {
										NOT = { has_star_flag = normal_region }
									}
									set_star_flag = normal_region
									every_neighbor_system = {
										limit = {
											NOT = { has_star_flag = normal_region }
										}
										set_star_flag = normal_region
										every_neighbor_system = {
											limit = {
												NOT = { has_star_flag = normal_region }
											}
											set_star_flag = normal_region
											every_neighbor_system = {
												limit = {
													NOT = { has_star_flag = normal_region }
												}
												set_star_flag = normal_region
												every_neighbor_system = {
													limit = {
														NOT = { has_star_flag = normal_region }
													}
													set_star_flag = normal_region
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}