Advertisement
Le_BuG63

planet.c

Aug 10th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.36 KB | None | 0 0
  1. #include "planet.h"
  2.  
  3. #define _USE_MATH_DEFINES
  4. #include <math.h>
  5. #undef _USE_MATH_DEFINES
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9.  
  10. #include "rand.h"
  11.  
  12. // densitΓ© planete tellurique: entre 4 et 4.5
  13.  
  14. Planet      planet_create(void) {
  15.     Planet  p = {
  16.         .areaTotal = 0.0,
  17.         .livableArea = 0.0,
  18.         .people = 0,
  19.         .isColony = false
  20.     };
  21.  
  22.     choseRandomPlanetType(&p);
  23.     choseRandomSpecies(&p);
  24.     choseRandomEconomy(&p);
  25.  
  26.     setRadius(&p);
  27.  
  28.     generateWorld(&p);
  29.  
  30.     if (p.isHabitable) {
  31.         setPeople(&p);
  32.     }
  33.  
  34.     return p;
  35. }
  36.  
  37. Planet      planet_createSun(void) {
  38.     Planet  p = {
  39.         .isColony = false,
  40.         .type = P_TYPE_STAR,
  41.         .isGiant = false
  42.     };
  43.  
  44.     printf("Type: %s\n", g_planetTypeName[4]);
  45.  
  46.     setRadius(&p);
  47.  
  48.     return p;
  49. }
  50.  
  51. void        planet_setDistanceOfStar(Planet *planet, float distance) {
  52.     planet->distanceOfNearestStar = distance;
  53. }
  54. void        planet_setDistanceOfShip(Planet *planet, float distance) {
  55.     planet->distanceOfShip = distance;
  56. }
  57.  
  58. void        planet_showStats(Planet planet) {
  59.     if (planet.type == P_TYPE_STAR) {
  60.         printf("Est une etoile\n");
  61.         printf("Rayon: %.3fkm", planet.radius);
  62.         printf("\nSurface totale: %lfmillions de km^2", planet.areaTotal);
  63.     }
  64.     else {
  65.         printf("Est une colonie: %s\n", (planet.isColony) ? "oui" : "non");
  66.         printf("Rayon: %.3fkm", planet.radius);
  67.         printf("\nSurface totale: %.3lf millions de km^2", planet.areaTotal);
  68.         if (planet.isHabitable) {
  69.             printf("\nHabitants: %d millions", planet.people);
  70.             printf("\nEst habitee par des %s", g_speciesType[planet.specie]);
  71.             printf("\nSurface habitable: %.3lf%%", planet.stat.percentageLivableArea);
  72.             printf("\nSurface non habitable: %.3lf%%\n", 100 - planet.stat.percentageLivableArea);
  73.             printf("\nRichesse de la population: %s", g_economyName[planet.economy]);
  74.         }
  75.     }
  76.     printf("\n");
  77. }
  78.  
  79. static void generateWorld(Planet *planet) {
  80.     if (planet->type == P_TYPE_TERRESRTIAL) {
  81.         if (genNonHabitableArea(planet, CONDITION_OCEAN, rand_float(40.f, 95.f), 7)) {
  82.             planet->hasWater = true;
  83.             planet->isHabitable = true;
  84.             planet->canCommerce = true;
  85.  
  86.             genNonHabitableArea(planet, CONDITION_DESERT, rand_float(5.f, 40.f), 4);
  87.             genNonHabitableArea(planet, CONDITION_ICE, rand_float(5.f, 20.f), 3);
  88.             genNonHabitableArea(planet, CONDITION_HUGE_FOREST, rand_float(5.f, 20.f), 0);
  89.         }
  90.         else if (CHANCE(2)) {
  91.             planet->isColony = true;
  92.             planet->isHabitable = true;
  93.             planet->canCommerce = true;
  94.  
  95.             genNonHabitableArea(planet, CONDITION_UNKNOW, 0, 0);
  96.         }
  97.         else {
  98.             planet->isHabitable = false;
  99.         }
  100.     }
  101.     else {
  102.         printf("Non habitable\n");
  103.         planet->isHabitable = false;
  104.     }
  105. }
  106.  
  107. static double setPercentageOfArea(double value, int offset) {
  108.     if (offset != 0)
  109.         return (value - value * offset / 100);
  110.     else
  111.         return value;
  112. }
  113.  
  114. static bool genNonHabitableArea(Planet *planet, PlanetCondition pCondition, float percentage, int chance) {
  115.     if (CHANCE(chance)) {
  116.         float   areaNonHabitable;
  117.         float   areaHabitable = (float)planet->areaTotal;
  118.  
  119.         if (planet->isColony)
  120.             areaNonHabitable = rand_float(70.f, 95.f);
  121.         else
  122.             areaNonHabitable = rand_float(0.f, percentage);
  123.  
  124.         areaHabitable = setPercentageOfArea(planet->livableArea, areaNonHabitable);
  125.  
  126.         planet->livableArea = areaHabitable;
  127.         planet->stat.percentageLivableArea = planet->livableArea * 100 / planet->areaTotal;
  128.  
  129.         return true;
  130.     }
  131.     return false;
  132. }
  133.  
  134. static void choseRandomSpecies(Planet *planet) {
  135.     int specie;
  136.  
  137.     static const int bornMin[] = {
  138.         0,
  139.         13,
  140.         15,
  141.         16
  142.     };
  143.  
  144.     const int bornMax[] = {
  145.         bornMin[1] - 1,
  146.         bornMin[2] - 1,
  147.         bornMin[3] - 1,
  148.         bornMin[3] + 1
  149.     };
  150.  
  151.     specie = rand_born(0, 19);
  152.  
  153. #define BORN(s, a) (s >= bornMin[a] && s <= bornMax[a])
  154.     if (BORN(specie, S_TYPE_HUMAN)) {
  155.         specie = S_TYPE_HUMAN;
  156.     }
  157.     else if (BORN(specie, S_TYPE_DROID)) {
  158.         specie = S_TYPE_DROID;
  159.     }
  160.     else if (BORN(specie, S_TYPE_ALIEN)) {
  161.         specie = S_TYPE_ALIEN;
  162.     }
  163.     else if (BORN(specie, S_TYPE_ROCK)) {
  164.         specie = S_TYPE_ROCK;
  165.     }
  166.     else
  167.         specie = S_TYPE_HUMAN;
  168. #undef BORN
  169.  
  170.     planet->specie = specie;
  171. }
  172.  
  173. static void choseRandomEconomy(Planet *planet) {
  174.     int eco = rand_born(E_TYPE_RICH, 3);
  175.  
  176.     planet->economy = eco;
  177. }
  178.  
  179. static void choseRandomPlanetType(Planet *planet) {
  180.     planet->type = rand_born(0, P_TYPE_LAST);
  181.  
  182.     planet->isGiant = false;
  183.  
  184.     if (planet->type == P_TYPE_GASEOUS) {
  185.         if (CHANCE(5)) {
  186.             planet->isGiant = true;
  187.             planet->type = P_TYPE_GASEOUS;
  188.         }
  189.     }
  190. }
  191.  
  192. static void choseRandomGovernementType(Planet *planet) {
  193.     planet->governementType = rand_born(0, G_TYPE_LAST);
  194. }
  195.  
  196. static void setPeople(Planet *planet) {
  197.     double  surface = planet->livableArea; // km^2
  198.     int     peoplePerSquareKm;
  199.  
  200.     if (!planet->isColony) {
  201.         peoplePerSquareKm = rand_born(254, 381);
  202.     }
  203.     else {
  204.         peoplePerSquareKm = rand_born((int)254 / 2, (int)381 / 2);
  205.     }
  206.  
  207.     planet->people = (peoplePerSquareKm * surface) / 10;
  208. }
  209.  
  210. static void setRadius(Planet *planet) {
  211.     double radius;
  212.  
  213.     if (planet->type == P_TYPE_STAR) {
  214.         radius = (double)(rand_born(5, 9) + rand_float(0, 1));
  215.         radius *= 100000;
  216.     }
  217.     else if (planet->isGiant && planet->type == P_TYPE_GASEOUS) {
  218.         radius = (double)(rand_born(2, 9) + rand_float(0, 1));
  219.         radius *= 10000;
  220.     }
  221.     else {
  222.         radius = (double)(rand_born(1001, 10000) + rand_float(0, 1));
  223.     }
  224.  
  225.     planet->radius = radius;
  226.     planet->areaTotal = (4 * M_PI * (radius * radius)) / 1000000;
  227.     planet->livableArea = planet->areaTotal;
  228. }
  229.  
  230. static void setDensity(Planet *planet) {
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement