f93_set_galaxy_size = {
	set_variable = { which = f93_gal_size value = 4 }
	# if = { limit = { galaxy_size = tiny } set_variable = { which = f93_gal_size value = 3 } }
	# if = { limit = { galaxy_size = small } set_variable = { which = f93_gal_size value = 3 } }
	# if = { limit = { galaxy_size = medium } set_variable = { which = f93_gal_size value = 4 } }
	# if = { limit = { galaxy_size = large } set_variable = { which = f93_gal_size value = 5 } }
	# if = { limit = { galaxy_size = huge } set_variable = { which = f93_gal_size value = 6 } }
	# else = {
		export_trigger_value_to_variable = {
			trigger = count_system
			variable = f93_num_systems
		}
		set_variable = { which = f93_gal_size value = f93_num_systems }
		divide_variable = { which = f93_gal_size value = 160 }
		round_variable = f93_gal_size
	# }
}

f93_spawn_ancient_relay = { #spawn ancient relay
	if = {
		limit = {
			NOT = { has_megastructure = dandelion_relay }
		}
		if = {
			limit = {
				has_overlord_dlc = yes
			}
			spawn_megastructure = {
				type = dandelion_relay
				random_pos = yes
				orbit_angle = 360		# range of the randomization
			}
		}
		set_star_flag = f93_ancient_relay_system # to repair it later with events
	}
}

f93_replace_relay_with_relay_star = {
	every_system_megastructure = {
		limit = { is_megastructure_type = dandelion_relay }
		remove_megastructure = this
	}
	if = {
		limit = {
			has_overlord_dlc = yes
		}
		spawn_megastructure = {
			type = dandelion_relay_star
			# planet = star
			orbit_angle = 360		# range of the randomization
		}
	}
}

highway_connect_relay_only = {
	set_star_flag = dd_highway_system_flag@this
	f93_spawn_ancient_relay = yes
}
highway_connect = {
	f93_spawn_ancient_relay = yes
	if = {
		limit = { exists = event_target:new_highway_system exists = event_target:old_highway_system }
		event_target:new_highway_system = {
			if = {
				limit = { NOT = { any_neighbor_system = { is_same_value = event_target:old_highway_system } } }
				add_hyperlane = { from = event_target:old_highway_system to = event_target:new_highway_system }
			}
		}
	}
	save_global_event_target_as = old_highway_system
}
# This will connect 2 Chosen points in the galaxy with a Hyper Relay string.
highway_create_system = {
	if = {
		limit = {
			exists = event_target:system_highway_start
			exists = event_target:system_highway_end
		}
		save_global_event_target_as = old_highway_system
		while = {
			# count = @steps
			limit = {
				NOT = { event_target:system_highway_end = { any_neighbor_system = { is_same_value = event_target:old_highway_system } } } 
			}
			event_target:system_highway_end = {
				closest_system = {
					limit = {
						NOT = { has_star_flag = dd_highway_system_flag }
						or = {
							distance = {
								source = event_target:old_highway_system
								max_jumps = 1
								type = hyperlane
							}
						}
					}
					save_global_event_target_as = new_highway_system
					highway_connect = yes
				}
			}
			# if = {
			# 	limit = { NOT = { exists = event_target:new_highway_system } }
			# 	spawn_highway_system = yes
			# }
			clear_global_event_target = new_highway_system
			if = {
				limit = { exists = event_target:system_highway_end exists = event_target:old_highway_system }
				event_target:system_highway_end = {
					if = {
						limit = {
							NOT = { any_neighbor_system = { is_same_value = event_target:old_highway_system } }
							distance = {
								source = event_target:old_highway_system
								max_distance < 30
								type = euclidean
							}
						}
						add_hyperlane = { from = event_target:old_highway_system to = event_target:system_highway_end }
					}
				}
			}
		}
		if = {
			limit = { event_target:system_highway_end = { any_neighbor_system = { is_same_value = event_target:old_highway_system } } }
			event_target:system_highway_end = { highway_connect_relay_only = yes }
		}
	}
}