Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "planet.h"
- #define _USE_MATH_DEFINES
- #include <math.h>
- #undef _USE_MATH_DEFINES
- #include <stdio.h>
- #include <stdlib.h>
- #include "rand.h"
- // densitΓ© planete tellurique: entre 4 et 4.5
- Planet planet_create(void) {
- Planet p = {
- .areaTotal = 0.0,
- .livableArea = 0.0,
- .people = 0,
- .isColony = false
- };
- choseRandomPlanetType(&p);
- choseRandomSpecies(&p);
- choseRandomEconomy(&p);
- setRadius(&p);
- generateWorld(&p);
- if (p.isHabitable) {
- setPeople(&p);
- }
- return p;
- }
- Planet planet_createSun(void) {
- Planet p = {
- .isColony = false,
- .type = P_TYPE_STAR,
- .isGiant = false
- };
- printf("Type: %s\n", g_planetTypeName[4]);
- setRadius(&p);
- return p;
- }
- void planet_setDistanceOfStar(Planet *planet, float distance) {
- planet->distanceOfNearestStar = distance;
- }
- void planet_setDistanceOfShip(Planet *planet, float distance) {
- planet->distanceOfShip = distance;
- }
- void planet_showStats(Planet planet) {
- if (planet.type == P_TYPE_STAR) {
- printf("Est une etoile\n");
- printf("Rayon: %.3fkm", planet.radius);
- printf("\nSurface totale: %lfmillions de km^2", planet.areaTotal);
- }
- else {
- printf("Est une colonie: %s\n", (planet.isColony) ? "oui" : "non");
- printf("Rayon: %.3fkm", planet.radius);
- printf("\nSurface totale: %.3lf millions de km^2", planet.areaTotal);
- if (planet.isHabitable) {
- printf("\nHabitants: %d millions", planet.people);
- printf("\nEst habitee par des %s", g_speciesType[planet.specie]);
- printf("\nSurface habitable: %.3lf%%", planet.stat.percentageLivableArea);
- printf("\nSurface non habitable: %.3lf%%\n", 100 - planet.stat.percentageLivableArea);
- printf("\nRichesse de la population: %s", g_economyName[planet.economy]);
- }
- }
- printf("\n");
- }
- static void generateWorld(Planet *planet) {
- if (planet->type == P_TYPE_TERRESRTIAL) {
- if (genNonHabitableArea(planet, CONDITION_OCEAN, rand_float(40.f, 95.f), 7)) {
- planet->hasWater = true;
- planet->isHabitable = true;
- planet->canCommerce = true;
- genNonHabitableArea(planet, CONDITION_DESERT, rand_float(5.f, 40.f), 4);
- genNonHabitableArea(planet, CONDITION_ICE, rand_float(5.f, 20.f), 3);
- genNonHabitableArea(planet, CONDITION_HUGE_FOREST, rand_float(5.f, 20.f), 0);
- }
- else if (CHANCE(2)) {
- planet->isColony = true;
- planet->isHabitable = true;
- planet->canCommerce = true;
- genNonHabitableArea(planet, CONDITION_UNKNOW, 0, 0);
- }
- else {
- planet->isHabitable = false;
- }
- }
- else {
- printf("Non habitable\n");
- planet->isHabitable = false;
- }
- }
- static double setPercentageOfArea(double value, int offset) {
- if (offset != 0)
- return (value - value * offset / 100);
- else
- return value;
- }
- static bool genNonHabitableArea(Planet *planet, PlanetCondition pCondition, float percentage, int chance) {
- if (CHANCE(chance)) {
- float areaNonHabitable;
- float areaHabitable = (float)planet->areaTotal;
- if (planet->isColony)
- areaNonHabitable = rand_float(70.f, 95.f);
- else
- areaNonHabitable = rand_float(0.f, percentage);
- areaHabitable = setPercentageOfArea(planet->livableArea, areaNonHabitable);
- planet->livableArea = areaHabitable;
- planet->stat.percentageLivableArea = planet->livableArea * 100 / planet->areaTotal;
- return true;
- }
- return false;
- }
- static void choseRandomSpecies(Planet *planet) {
- int specie;
- static const int bornMin[] = {
- 0,
- 13,
- 15,
- 16
- };
- const int bornMax[] = {
- bornMin[1] - 1,
- bornMin[2] - 1,
- bornMin[3] - 1,
- bornMin[3] + 1
- };
- specie = rand_born(0, 19);
- #define BORN(s, a) (s >= bornMin[a] && s <= bornMax[a])
- if (BORN(specie, S_TYPE_HUMAN)) {
- specie = S_TYPE_HUMAN;
- }
- else if (BORN(specie, S_TYPE_DROID)) {
- specie = S_TYPE_DROID;
- }
- else if (BORN(specie, S_TYPE_ALIEN)) {
- specie = S_TYPE_ALIEN;
- }
- else if (BORN(specie, S_TYPE_ROCK)) {
- specie = S_TYPE_ROCK;
- }
- else
- specie = S_TYPE_HUMAN;
- #undef BORN
- planet->specie = specie;
- }
- static void choseRandomEconomy(Planet *planet) {
- int eco = rand_born(E_TYPE_RICH, 3);
- planet->economy = eco;
- }
- static void choseRandomPlanetType(Planet *planet) {
- planet->type = rand_born(0, P_TYPE_LAST);
- planet->isGiant = false;
- if (planet->type == P_TYPE_GASEOUS) {
- if (CHANCE(5)) {
- planet->isGiant = true;
- planet->type = P_TYPE_GASEOUS;
- }
- }
- }
- static void choseRandomGovernementType(Planet *planet) {
- planet->governementType = rand_born(0, G_TYPE_LAST);
- }
- static void setPeople(Planet *planet) {
- double surface = planet->livableArea; // km^2
- int peoplePerSquareKm;
- if (!planet->isColony) {
- peoplePerSquareKm = rand_born(254, 381);
- }
- else {
- peoplePerSquareKm = rand_born((int)254 / 2, (int)381 / 2);
- }
- planet->people = (peoplePerSquareKm * surface) / 10;
- }
- static void setRadius(Planet *planet) {
- double radius;
- if (planet->type == P_TYPE_STAR) {
- radius = (double)(rand_born(5, 9) + rand_float(0, 1));
- radius *= 100000;
- }
- else if (planet->isGiant && planet->type == P_TYPE_GASEOUS) {
- radius = (double)(rand_born(2, 9) + rand_float(0, 1));
- radius *= 10000;
- }
- else {
- radius = (double)(rand_born(1001, 10000) + rand_float(0, 1));
- }
- planet->radius = radius;
- planet->areaTotal = (4 * M_PI * (radius * radius)) / 1000000;
- planet->livableArea = planet->areaTotal;
- }
- static void setDensity(Planet *planet) {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement