Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package scripts;
- import objects.StarCluster;
- import objects.Star;
- public class UniverseGenerator {
- public UniverseGenerator(int lightYearsX, int lightYearsY, double densityOfStars) {
- int numberOfStarClusters;
- int checkedStars = 0;
- int generatedStars = 0;
- int pointsX = lightYearsX * 10; //coord system - 1 light year = 10 coords
- int pointsY = lightYearsY * 10;
- int capacity = lightYearsX * lightYearsY * 10; //coords in the map
- int numberOfStars = (int) Math.rint(capacity * densityOfStars);
- boolean starInDeadZone = true;
- Star[] stars = new Star[numberOfStars]; //creating instance of class Star
- int starRadius;
- int temperature;
- int starType;
- int imageOfStar;
- int spectralClass;
- int differenceX;
- int starCoordX;
- int starCoordY;
- numberOfStarClusters = (int) Math.ceil(numberOfStars * 0.05);
- StarCluster[] clusters = new StarCluster[numberOfStarClusters]; //sreating instance of class StarCluster
- /*
- * Generating star clusters
- * Number of stars must be larger than 32
- */
- if (numberOfStars >= 32) {
- int generatedStarClusters;
- generatedStarClusters = 0;
- /*
- * Here is generating star clusters cycle
- */
- while (generatedStarClusters <= numberOfStarClusters) {
- int starsInCluster;
- //generating coord X and Y / the scale is all map width and height
- int clusterCoordX = (int) Math.round(Math.random() * pointsX);
- int clusterCoordY = (int) Math.round(Math.random() * pointsY);
- starsInCluster = (int) Math.round(Math.random() * 8 + 8); //generating number of stars in cluster - scale is 8 - 16
- //radius depends on number of stars in cluster
- int radius = (int) Math.round(Math.random() * starsInCluster * 2.5 + 1.5 * starsInCluster);
- int checkedClusters = 0;
- boolean clusterInDeadZone = true;
- /*
- * Now checking if the cluster isn't in conflict with another clusters.
- * No stars are generated. So they don't have to be included in this checking
- */
- while (checkedClusters <= numberOfStarClusters) {
- /*
- * Checking if the cluster is remoted 3 light years minimaly
- */
- if (generatedStarClusters > 0) {
- int differenceClusterAxisX = Math.abs(clusters[checkedClusters].clusterCoordX - clusterCoordX);
- int differenceClusterAxisY = Math.abs(clusters[checkedClusters].clusterCoordY - clusterCoordY);
- if (Math.abs(differenceClusterAxisX) < 30 || Math.abs(differenceClusterAxisY) < 30) {
- clusterInDeadZone = true;
- break;
- //If a conflict found, the checking cycle stops
- }
- }
- checkedClusters++;
- if (checkedClusters == numberOfStarClusters) {
- clusterInDeadZone = false;
- break;
- //If checked all clusters because of conflict, the checking cycle stops too
- //Only because of make sure that it won't still run
- }
- }
- /*
- * Checking for conflict cycle ends now
- *
- * If everithing's OK, cycle generating stars in cluster starts now.
- */
- if (clusterCoordX > radius + 5 && clusterCoordY > radius + 5
- && clusterCoordX + radius + 5 < pointsX && clusterCoordY + radius + 5 < pointsX
- && clusterInDeadZone == false) {
- while (generatedStars <= starsInCluster) {
- /*
- * Setting the scale for stars
- */
- int maxGeneratedX = clusterCoordX + radius;
- int minGeneratedX = clusterCoordX - radius;
- int maxGeneratedY = clusterCoordY + radius;
- int minGeneratedY = clusterCoordY - radius;
- //Generating star's coord X
- starCoordX = (int) Math.round(Math.random() * (minGeneratedX - maxGeneratedX) + (maxGeneratedX - minGeneratedX + radius * 2));
- int valueXYchangedBy = Math.abs(clusterCoordX - starCoordX) - 1;
- /*
- * Editing scale which depends on star's coord X value
- */
- maxGeneratedY = maxGeneratedY - valueXYchangedBy;
- minGeneratedY = minGeneratedY + valueXYchangedBy;
- //Generating star's coord Y
- starCoordY = (int) Math.round(Math.random() * (minGeneratedY - maxGeneratedY) + (maxGeneratedY - minGeneratedY + radius * 2));
- /*
- * Now checking stars in cluster if they aren't in conflict with another stars in cluster
- */
- while (checkedStars <= starsInCluster) {
- if (generatedStars > 0) {
- int differenceStarAxisX = Math.abs(stars[checkedStars].starCoordX - clusterCoordX);
- int differenceStarAxisY = Math.abs(stars[checkedStars].starCoordY - clusterCoordY);
- if (Math.abs(differenceStarAxisX) < 5 || Math.abs(differenceStarAxisY) < 5) {
- starInDeadZone = true;
- break;
- }
- }
- checkedStars++;
- if (checkedClusters == numberOfStarClusters) {
- starInDeadZone = false;
- break;
- }
- }
- /*
- * Checking stars ends now.
- *
- * If everithing OK, stars are generated.
- */
- if (starInDeadZone == false) {
- stars[generatedStars] = new Star(generatedStars, starCoordX, starCoordY);
- generatedStars++;
- }
- }
- /*
- * End of generating stars cycle
- *
- * If everithing's OK, clusters are generated.
- */
- clusters[generatedStarClusters] = new StarCluster(generatedStarClusters, clusterCoordX, clusterCoordY, radius);
- generatedStarClusters++;
- }
- }
- }
- System.out.println("Program bezi po generovani star clusters");
- /*
- * Generating star clusters ends now.
- *
- * Generating general stars (not in clusters)
- */
- while (generatedStars <= numberOfStars) {
- starCoordX = (int) Math.round(Math.random() * pointsX);
- starCoordY = (int) Math.round(Math.random() * pointsY);
- //-------------------------------------------------------
- while (checkedStars <= numberOfStars) {
- int differenceStarAxisX;
- int differenceStarAxisY;
- int checkedClusters = 0;
- /*
- * Checking if the cluster is remoted 3 light years minimaly
- */
- differenceStarAxisX = Math.abs(stars[checkedStars].starCoordX - starCoordX);
- differenceStarAxisY = Math.abs(stars[checkedStars].starCoordY - starCoordY);
- int differenceClusterAxisX = Math.abs(clusters[checkedClusters].clusterCoordX - starCoordX);
- int differenceClusterAxisY = Math.abs(clusters[checkedClusters].clusterCoordY - starCoordY);
- double clusterRadius = clusters[checkedClusters].radius;
- if (differenceStarAxisX < 5 || differenceStarAxisY < 5
- || differenceClusterAxisX + clusterRadius < 30
- || differenceClusterAxisY + clusterRadius < 30) {
- starInDeadZone = true;
- break;
- //If a conflict found, the checking cycle stops
- }
- checkedStars++;
- checkedClusters++;
- if (checkedStars == numberOfStars) {
- starInDeadZone = false;
- break;
- //If checked all stars because of conflict, the checking cycle stops too
- //Only because of make sure that it won't still run
- }
- if (starInDeadZone == false) {
- stars[generatedStars] = new Star(generatedStars, starCoordX, starCoordY);
- generatedStars++;
- }
- }
- }
- }
- public static void main(String[] args) {
- new UniverseGenerator(10, 10, 0.5);
- }
- }
Add Comment
Please, Sign In to add comment