Refactor; Optimize simulation
This commit is contained in:
parent
8581cf6fb8
commit
0ef5652cea
16 changed files with 508 additions and 651 deletions
|
|
@ -7,12 +7,12 @@ public interface IGenerator
|
|||
|
||||
public Universe InitializeEmptyUniverse(Vector3I universeSize, Vector3 sectorSize);
|
||||
public Universe GenerateUniverse();
|
||||
public Sector GenerateSector(Vector3I coordinates);
|
||||
public Sector GenerateSector(Vector3I coordinates, RandomNumberGenerator rng);
|
||||
|
||||
public Star GenerateStar(Sector sector, Vector3 localCoordinates);
|
||||
public Star GenerateStar(Sector sector);
|
||||
public Star GenerateStar(Sector sector, RandomNumberGenerator rng);
|
||||
public Vessel GenerateShip(Sector sector, Vector3 localCoordinates);
|
||||
public Vessel GenerateShip(Sector sector);
|
||||
public Vessel GenerateShip(Sector sector, RandomNumberGenerator rng);
|
||||
public Vessel GenerateStation(Sector sector, Vector3 localCoordinates);
|
||||
public Vessel GenerateStation(Sector sector);
|
||||
public Vessel GenerateStation(Sector sector, RandomNumberGenerator rng);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,21 +1,16 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Godot;
|
||||
|
||||
public class TestGenerator : IGenerator
|
||||
{
|
||||
public static Vector3I UNIVERSE_SIZE = new(10, 10, 10);
|
||||
public static Vector3 SECTOR_SIZE = new(50, 50, 50);
|
||||
public static int STARS_PER_SECTOR = 1;
|
||||
public static int STARS_PER_SECTOR_VARIANCE = 0;
|
||||
public static int SHIPS_PER_SECTOR = 0;
|
||||
public static int SHIPS_PER_SECTOR_VARIANCE = 0;
|
||||
public static int STATIONS_PER_SECTOR = 0;
|
||||
public static int STATIONS_PER_SECTOR_VARIANCE = 0;
|
||||
|
||||
private readonly int threads = 16;
|
||||
|
||||
private RandomNumberGenerator rng;
|
||||
public readonly static Vector3I UNIVERSE_SIZE = new(10, 10, 10);
|
||||
public readonly static Vector3 SECTOR_SIZE = new(50, 50, 50);
|
||||
public readonly static int STARS_PER_SECTOR = 1;
|
||||
public readonly static int STARS_PER_SECTOR_VARIANCE = 0;
|
||||
public readonly static int SHIPS_PER_SECTOR = 0;
|
||||
public readonly static int SHIPS_PER_SECTOR_VARIANCE = 0;
|
||||
public readonly static int STATIONS_PER_SECTOR = 0;
|
||||
public readonly static int STATIONS_PER_SECTOR_VARIANCE = 0;
|
||||
|
||||
public Vector3I GetUniverseSize()
|
||||
{
|
||||
|
|
@ -27,7 +22,7 @@ public class TestGenerator : IGenerator
|
|||
return SECTOR_SIZE;
|
||||
}
|
||||
|
||||
public Vector3I GetSectorOffset(Vector3I universeSize)
|
||||
public static Vector3I GetSectorOffset(Vector3I universeSize)
|
||||
{
|
||||
return new(
|
||||
(int)Mathf.Floor(universeSize.X / 2),
|
||||
|
|
@ -36,7 +31,7 @@ public class TestGenerator : IGenerator
|
|||
);
|
||||
}
|
||||
|
||||
public Vector3I GetSectorOffset()
|
||||
public static Vector3I GetSectorOffset()
|
||||
{
|
||||
return GetSectorOffset(UNIVERSE_SIZE);
|
||||
}
|
||||
|
|
@ -52,9 +47,7 @@ public class TestGenerator : IGenerator
|
|||
for (int z = 0; z < universeSize.Z; z++)
|
||||
{
|
||||
universe.Sectors[x, y, z] = new Sector(
|
||||
new(x, y, z),
|
||||
GetSectorOffset(universeSize),
|
||||
sectorSize
|
||||
new(x, y, z)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -65,50 +58,33 @@ public class TestGenerator : IGenerator
|
|||
|
||||
public Universe GenerateUniverse()
|
||||
{
|
||||
rng = new();
|
||||
|
||||
List<Task> tasks = [];
|
||||
|
||||
Universe universe = new(UNIVERSE_SIZE);
|
||||
int countX = Mathf.Clamp(UNIVERSE_SIZE.X / threads, 1, int.MaxValue);
|
||||
|
||||
for (int x = 0; x < UNIVERSE_SIZE.X; x += countX)
|
||||
Parallel.For(0, UNIVERSE_SIZE.X, x =>
|
||||
{
|
||||
int taskStartX = x;
|
||||
int taskCountX = countX;
|
||||
|
||||
Task clusteredTask = Task.Run(() =>
|
||||
{
|
||||
GenerateClustered(universe, taskStartX, taskCountX);
|
||||
});
|
||||
|
||||
tasks.Add(clusteredTask);
|
||||
}
|
||||
|
||||
Task.WaitAll([.. tasks]);
|
||||
GenerateClustered(universe, x);
|
||||
});
|
||||
|
||||
return universe;
|
||||
}
|
||||
|
||||
private void GenerateClustered(Universe universe, int startX, int countX)
|
||||
private void GenerateClustered(Universe universe, int x)
|
||||
{
|
||||
int endX = Mathf.Clamp(startX + countX, 0, UNIVERSE_SIZE.X);
|
||||
RandomNumberGenerator rng = new();
|
||||
rng.Randomize();
|
||||
|
||||
for (int x = startX; x < endX; x++)
|
||||
for (int y = 0; y < UNIVERSE_SIZE.Y; y++)
|
||||
{
|
||||
for (int y = 0; y < UNIVERSE_SIZE.Y; y++)
|
||||
for (int z = 0; z < UNIVERSE_SIZE.Z; z++)
|
||||
{
|
||||
for (int z = 0; z < UNIVERSE_SIZE.Z; z++)
|
||||
{
|
||||
universe.Sectors[x, y, z] = GenerateSector(new(x, y, z));
|
||||
}
|
||||
universe.Sectors[x, y, z] = GenerateSector(new(x, y, z), rng);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Sector GenerateSector(Vector3I coordinates)
|
||||
public Sector GenerateSector(Vector3I coordinates, RandomNumberGenerator rng)
|
||||
{
|
||||
Sector sector = new(coordinates, GetSectorOffset(), SECTOR_SIZE);
|
||||
Sector sector = new(coordinates);
|
||||
|
||||
int starCount = rng.RandiRange(
|
||||
STARS_PER_SECTOR - STARS_PER_SECTOR_VARIANCE,
|
||||
|
|
@ -120,7 +96,7 @@ public class TestGenerator : IGenerator
|
|||
{
|
||||
if (coordinates.X == 5 && coordinates.Y == 5 && coordinates.Z == 5)
|
||||
{
|
||||
Vector3 localCoordinates = GenerateLocalCoordinates();
|
||||
Vector3 localCoordinates = GenerateLocalCoordinates(rng);
|
||||
Star star = GenerateStar(sector, localCoordinates);
|
||||
|
||||
sector.GameObjects.Add(star);
|
||||
|
|
@ -135,7 +111,7 @@ public class TestGenerator : IGenerator
|
|||
|
||||
for (int i = 0; i < shipCount; i++)
|
||||
{
|
||||
Vector3 localCoordinates = GenerateLocalCoordinates();
|
||||
Vector3 localCoordinates = GenerateLocalCoordinates(rng);
|
||||
Vessel ship = GenerateShip(sector, localCoordinates);
|
||||
|
||||
sector.GameObjects.Add(ship);
|
||||
|
|
@ -149,7 +125,7 @@ public class TestGenerator : IGenerator
|
|||
|
||||
for (int i = 0; i < stationCount; i++)
|
||||
{
|
||||
Vector3 localCoordinates = GenerateLocalCoordinates();
|
||||
Vector3 localCoordinates = GenerateLocalCoordinates(rng);
|
||||
Vessel station = GenerateStation(sector, localCoordinates);
|
||||
|
||||
sector.GameObjects.Add(station);
|
||||
|
|
@ -163,9 +139,9 @@ public class TestGenerator : IGenerator
|
|||
return new Star(sector, localCoordinates);
|
||||
}
|
||||
|
||||
public Star GenerateStar(Sector sector)
|
||||
public Star GenerateStar(Sector sector, RandomNumberGenerator rng)
|
||||
{
|
||||
return GenerateStar(sector, GenerateLocalCoordinates());
|
||||
return GenerateStar(sector, GenerateLocalCoordinates(rng));
|
||||
}
|
||||
|
||||
public Vessel GenerateShip(Sector sector, Vector3 localCoordinates)
|
||||
|
|
@ -173,9 +149,9 @@ public class TestGenerator : IGenerator
|
|||
return new Vessel(sector, localCoordinates);
|
||||
}
|
||||
|
||||
public Vessel GenerateShip(Sector sector)
|
||||
public Vessel GenerateShip(Sector sector, RandomNumberGenerator rng)
|
||||
{
|
||||
return GenerateShip(sector, GenerateLocalCoordinates());
|
||||
return GenerateShip(sector, GenerateLocalCoordinates(rng));
|
||||
}
|
||||
|
||||
public Vessel GenerateStation(Sector sector, Vector3 localCoordinates)
|
||||
|
|
@ -183,12 +159,12 @@ public class TestGenerator : IGenerator
|
|||
return new Vessel(sector, localCoordinates);
|
||||
}
|
||||
|
||||
public Vessel GenerateStation(Sector sector)
|
||||
public Vessel GenerateStation(Sector sector, RandomNumberGenerator rng)
|
||||
{
|
||||
return GenerateStation(sector, GenerateLocalCoordinates());
|
||||
return GenerateStation(sector, GenerateLocalCoordinates(rng));
|
||||
}
|
||||
|
||||
public Vector3 GenerateLocalCoordinates()
|
||||
public static Vector3 GenerateLocalCoordinates(RandomNumberGenerator rng)
|
||||
{
|
||||
double x = (rng.Randf() - 0.5) * SECTOR_SIZE.X;
|
||||
double y = (rng.Randf() - 0.5) * SECTOR_SIZE.Y;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue