From d3c38d90f7e7db8fbe5ec5ffbeaa97d38e177803 Mon Sep 17 00:00:00 2001 From: Allanis Date: Sat, 12 Apr 2014 20:32:54 +0100 Subject: [PATCH] [Add] Economy now dynamically flutuates slightly. --- src/economy.c | 47 ++++++++++++++++++++++++++++++++++------------- src/space.c | 3 +++ src/space.h | 3 ++- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/economy.c b/src/economy.c index d4118ed..9c5f2b7 100644 --- a/src/economy.c +++ b/src/economy.c @@ -22,6 +22,7 @@ #include "pilot.h" #include "rng.h" #include "space.h" +#include "ltime.h" #include "economy.h" #define XML_COMMODITY_ID "Commodities" /* XML section identifier. */ @@ -33,6 +34,7 @@ #define ECON_SELF_RES 3. /**< Additional resistance for the self node. */ #define ECON_FACTION_MOD 0.1 /**< Modifier on Base for faction standings. */ #define ECON_PROD_MODIFIER 500000. /**< Production modifier, divide production by this amount. */ +#define ECON_PROD_VAR 0.01 /**< Define the variability of production. */ /* Commodity stack. */ static Commodity* commodity_stack = NULL; @@ -277,18 +279,39 @@ static double econ_calcSysI(unsigned int dt, StarSystem* sys, int price) { (void)price; int i; double I; - double p; + double prodfactor, p, pp; + double ddt; Planet* planet; + ddt = (double)(dt / LTIME_UNIT_LENGTH); + /* Calculate production level. */ p = 0.; for(i = 0; i < sys->nplanets; i++) { planet = sys->planets[i]; - if(planet_hasService(planet, PLANET_SERVICE_BASIC)) - p += planet->prodfactor * sqrt(planet->population); + if(planet_hasService(planet, PLANET_SERVICE_BASIC)) { + /* Calculate production. + * + * We base off the current production. + */ + prodfactor = planet->cur_prodfactor; + /* Add a variability factor based on the gaussian distribution. */ + prodfactor += ECON_PROD_VAR * + NormalInverse(RNGF()*0.90 + 0.05)*ddt; + /* Add a tendency to return to the planets base production. */ + prodfactor -= ECON_PROD_VAR * + (planet->cur_prodfactor - prodfactor)*ddt; + /* Save for next iteration. */ + planet->cur_prodfactor = prodfactor; + /* We base off the sqrt of the population otherwise it changes too fast. */ + p += prodfactor * sqrt(planet->population); + + /* Add it to the current system production. */ + p += pp; + } } - /* Intensity is the production minus the consumption. */ + /* The intensity is basically the modified production. */ I = p / ECON_PROD_MODIFIER; return I; @@ -322,7 +345,7 @@ static int econ_createGMatrix(void) { R = 1./R; /* Must be inverted. */ Rsum += R; - /* Matrix is symetrical. */ + /* Matrix is symetrical and non-diagonal is negative. */ ret = cs_entry(M, i, sys->jumps[j], -R); if(ret != 1) WARN("Unable to enter CSparse Matrix Cell."); @@ -332,7 +355,8 @@ static int econ_createGMatrix(void) { } /* Set the diagonal. */ - cs_entry(M, i, i, Rsum + 1./ECON_SELF_RES); + Rsum += 1./ECON_SELF_RES; /* We add a resistence for dampening. */ + cs_entry(M, i, i, Rsum); } /* Compress M matrix and put into G. */ @@ -350,6 +374,7 @@ static int econ_createGMatrix(void) { /** * @brief Initializes the economy. + * @return 0 on success. */ int economy_init(void) { int i; @@ -361,16 +386,12 @@ int economy_init(void) { systems_stack[i].prices = calloc(econ_nprices, sizeof(double)); } - /* Create the resistance matrix. */ - if(econ_createGMatrix()) - return -1; - - /* Initialize the prices. */ - economy_update(0); - /* Mark economy as initialized. */ econ_initialized = 1; + /* Refresh economy. */ + economy_refresh(); + return 0; } diff --git a/src/space.c b/src/space.c index f2bee9b..0979aab 100644 --- a/src/space.c +++ b/src/space.c @@ -762,6 +762,9 @@ static int planet_parse(Planet* planet, const xmlNodePtr parent) { } } while(xml_nextNode(node)); + /* Some postprocessing. */ + planet->cur_prodfactor = planet->prodfactor; + /* Verification. */ #define MELEMENT(o,s) if(o) WARN("Planet '%s' missing '"s"' element", planet->name) MELEMENT(planet->gfx_space==NULL, "GFX space"); diff --git a/src/space.h b/src/space.h index 00a66d9..77d41e8 100644 --- a/src/space.h +++ b/src/space.h @@ -66,7 +66,8 @@ typedef struct Planet_ { int faction; /**< Planet faction. */ int population; /**< Population of the planet. */ - double prodfactor; /**< Production factor of the planet. */ + double prodfactor; /**< Default production factor of the planet. */ + double cur_prodfactor; /**< Current real production factor of the planet. */ /* Landing details. */ char* description; /**< Planet description. */