Guest User

Untitled

a guest
Dec 17th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.45 KB | None | 0 0
  1. package scripts;
  2.  
  3. import objects.StarCluster;
  4. import objects.Star;
  5.  
  6. public class UniverseGenerator {
  7.  
  8.     public UniverseGenerator(int lightYearsX, int lightYearsY, double densityOfStars) {
  9.         int numberOfStarClusters;
  10.         int checkedStars = 0;
  11.         int generatedStars = 0;
  12.         int pointsX = lightYearsX * 10; //coord system - 1 light year = 10 coords
  13.         int pointsY = lightYearsY * 10;
  14.         int capacity = lightYearsX * lightYearsY * 10; //coords in the map
  15.         int numberOfStars = (int) Math.rint(capacity * densityOfStars);
  16.         boolean starInDeadZone = true;
  17.         Star[] stars = new Star[numberOfStars]; //creating instance of class Star
  18.  
  19.         int starRadius;
  20.         int temperature;
  21.         int starType;
  22.         int imageOfStar;
  23.         int spectralClass;
  24.         int differenceX;
  25.         int starCoordX;
  26.         int starCoordY;
  27.         numberOfStarClusters = (int) Math.ceil(numberOfStars * 0.05);
  28.         StarCluster[] clusters = new StarCluster[numberOfStarClusters]; //sreating instance of class StarCluster
  29.         /*          
  30.          * Generating star clusters
  31.          * Number of stars must be larger than 32
  32.          */
  33.         if (numberOfStars >= 32) {
  34.             int generatedStarClusters;
  35.             generatedStarClusters = 0;
  36.  
  37.             /*
  38.              * Here is generating star clusters cycle
  39.              */
  40.  
  41.             while (generatedStarClusters <= numberOfStarClusters) {
  42.  
  43.                 int starsInCluster;
  44.  
  45.                 //generating coord X and Y / the scale is all map width and height
  46.                 int clusterCoordX = (int) Math.round(Math.random() * pointsX);
  47.                 int clusterCoordY = (int) Math.round(Math.random() * pointsY);
  48.                 starsInCluster = (int) Math.round(Math.random() * 8 + 8); //generating number of stars in cluster - scale is 8 - 16
  49.                 //radius depends on number of stars in cluster
  50.                 int radius = (int) Math.round(Math.random() * starsInCluster * 2.5 + 1.5 * starsInCluster);
  51.  
  52.                 int checkedClusters = 0;
  53.                 boolean clusterInDeadZone = true;
  54.                 /*
  55.                  * Now checking if the cluster isn't in conflict with another clusters.
  56.                  * No stars are generated. So they don't have to be included in this checking
  57.                  */
  58.  
  59.                 while (checkedClusters <= numberOfStarClusters) {
  60.  
  61.                     /*
  62.                      * Checking if the cluster is remoted 3 light years minimaly
  63.                      */
  64.                     if (generatedStarClusters > 0) {
  65.                         int differenceClusterAxisX = Math.abs(clusters[checkedClusters].clusterCoordX - clusterCoordX);
  66.                         int differenceClusterAxisY = Math.abs(clusters[checkedClusters].clusterCoordY - clusterCoordY);
  67.                         if (Math.abs(differenceClusterAxisX) < 30 || Math.abs(differenceClusterAxisY) < 30) {
  68.                             clusterInDeadZone = true;
  69.                             break;
  70.                             //If a conflict found, the checking cycle stops
  71.                         }
  72.                     }
  73.                     checkedClusters++;
  74.                     if (checkedClusters == numberOfStarClusters) {
  75.                         clusterInDeadZone = false;
  76.                         break;
  77.                         //If checked all clusters because of conflict, the checking cycle stops too
  78.                         //Only because of make sure that it won't still run
  79.  
  80.                     }
  81.                 }
  82.  
  83.                 /*
  84.                  * Checking for conflict cycle ends now
  85.                  *
  86.                  * If everithing's OK, cycle generating stars in cluster starts now.
  87.                  */
  88.                 if (clusterCoordX > radius + 5 && clusterCoordY > radius + 5
  89.                         && clusterCoordX + radius + 5 < pointsX && clusterCoordY + radius + 5 < pointsX
  90.                         && clusterInDeadZone == false) {
  91.                     while (generatedStars <= starsInCluster) {
  92.  
  93.                         /*
  94.                          * Setting the scale for stars
  95.                          */
  96.                         int maxGeneratedX = clusterCoordX + radius;
  97.                         int minGeneratedX = clusterCoordX - radius;
  98.                         int maxGeneratedY = clusterCoordY + radius;
  99.                         int minGeneratedY = clusterCoordY - radius;
  100.  
  101.                         //Generating star's coord X
  102.                         starCoordX = (int) Math.round(Math.random() * (minGeneratedX - maxGeneratedX) + (maxGeneratedX - minGeneratedX + radius * 2));
  103.  
  104.                         int valueXYchangedBy = Math.abs(clusterCoordX - starCoordX) - 1;
  105.  
  106.                         /*
  107.                          * Editing scale which depends on star's coord X value
  108.                          */
  109.                         maxGeneratedY = maxGeneratedY - valueXYchangedBy;
  110.                         minGeneratedY = minGeneratedY + valueXYchangedBy;
  111.  
  112.                         //Generating star's coord Y
  113.                         starCoordY = (int) Math.round(Math.random() * (minGeneratedY - maxGeneratedY) + (maxGeneratedY - minGeneratedY + radius * 2));
  114.  
  115.                         /*
  116.                          * Now checking stars in cluster if they aren't in conflict with another stars in cluster
  117.                          */
  118.  
  119.                         while (checkedStars <= starsInCluster) {
  120.                             if (generatedStars > 0) {
  121.                                 int differenceStarAxisX = Math.abs(stars[checkedStars].starCoordX - clusterCoordX);
  122.                                 int differenceStarAxisY = Math.abs(stars[checkedStars].starCoordY - clusterCoordY);
  123.                                 if (Math.abs(differenceStarAxisX) < 5 || Math.abs(differenceStarAxisY) < 5) {
  124.                                     starInDeadZone = true;
  125.                                     break;
  126.                                 }
  127.                             }
  128.                             checkedStars++;
  129.                             if (checkedClusters == numberOfStarClusters) {
  130.                                 starInDeadZone = false;
  131.                                 break;
  132.                             }
  133.                         }
  134.                         /*
  135.                          * Checking stars ends now.
  136.                          *
  137.                          * If everithing OK, stars are generated.
  138.                          */
  139.                         if (starInDeadZone == false) {
  140.  
  141.                             stars[generatedStars] = new Star(generatedStars, starCoordX, starCoordY);
  142.  
  143.                             generatedStars++;
  144.  
  145.                         }
  146.                     }
  147.                     /*
  148.                      * End of generating stars cycle
  149.                      *
  150.                      * If everithing's OK, clusters are generated.
  151.                      */
  152.  
  153.                     clusters[generatedStarClusters] = new StarCluster(generatedStarClusters, clusterCoordX, clusterCoordY, radius);
  154.  
  155.                     generatedStarClusters++;
  156.  
  157.                 }
  158.             }
  159.         }
  160.         System.out.println("Program bezi po generovani star clusters");
  161.         /*
  162.          * Generating star clusters ends now.
  163.          *
  164.          * Generating general stars (not in clusters)
  165.          */
  166.         while (generatedStars <= numberOfStars) {
  167.             starCoordX = (int) Math.round(Math.random() * pointsX);
  168.             starCoordY = (int) Math.round(Math.random() * pointsY);
  169.  
  170.             //-------------------------------------------------------
  171.             while (checkedStars <= numberOfStars) {
  172.                 int differenceStarAxisX;
  173.                 int differenceStarAxisY;
  174.                 int checkedClusters = 0;
  175.  
  176.                 /*
  177.                  * Checking if the cluster is remoted 3 light years minimaly
  178.                  */
  179.                 differenceStarAxisX = Math.abs(stars[checkedStars].starCoordX - starCoordX);
  180.                 differenceStarAxisY = Math.abs(stars[checkedStars].starCoordY - starCoordY);
  181.                 int differenceClusterAxisX = Math.abs(clusters[checkedClusters].clusterCoordX - starCoordX);
  182.                 int differenceClusterAxisY = Math.abs(clusters[checkedClusters].clusterCoordY - starCoordY);
  183.                 double clusterRadius = clusters[checkedClusters].radius;
  184.  
  185.                 if (differenceStarAxisX < 5 || differenceStarAxisY < 5
  186.                         || differenceClusterAxisX + clusterRadius < 30
  187.                         || differenceClusterAxisY + clusterRadius < 30) {
  188.                     starInDeadZone = true;
  189.                     break;
  190.                     //If a conflict found, the checking cycle stops
  191.                 }
  192.                 checkedStars++;
  193.                 checkedClusters++;
  194.                 if (checkedStars == numberOfStars) {
  195.                     starInDeadZone = false;
  196.                     break;
  197.                     //If checked all stars because of conflict, the checking cycle stops too
  198.                     //Only because of make sure that it won't still run
  199.                 }
  200.                 if (starInDeadZone == false) {
  201.                     stars[generatedStars] = new Star(generatedStars, starCoordX, starCoordY);
  202.                     generatedStars++;
  203.                 }
  204.             }
  205.         }
  206.     }
  207.  
  208.     public static void main(String[] args) {
  209.         new UniverseGenerator(10, 10, 0.5);
  210.  
  211.     }
  212. }
Add Comment
Please, Sign In to add comment