/* These procedures handle the updating of the contents of the */
/* environment.  They will need to be recompiled if a new dynamic is */
/* desired.  At the start we will just have a constant amount of type */
/* 1 metabolite introduced into the environment each time step (with */
/* slight random variations).  Later we may consider implementing */
/* fluxuating resources with a sine wave type function.*/

#include <stdio.h>
#include "types.h"
#include "random.h"
#include "update_env.h"
#include "debug.h"


#define ENVINPUT1 15  /*The number of type 1 metabolites added every */
		      /*time step.*/
 
#define UPDAT 1 /*debugging const*/
#undef UPDAT



int num_metabolites(int popsize, int total)
{
  double pop, mets, remainder;
  int amount;

#ifdef UPDAT
   printf(" ---in num_metabolites---\n");
#endif

  amount = total/popsize; /*integer division*/
  
  pop = (double) popsize;
  mets = (double) total;

  remainder = (mets/pop) - amount;

#ifdef UPDAT
   printf(" ---end num_metabolites---\n");
#endif

  if (knuth_random() < remainder) return amount + 1;
  else return amount;
}


  /*This gives the "functions" for the input to the environment every */
  /*time step.  This can be a function of the time step itself.  At */
  /*the moment we are inputting a constant amount of type 1 */
  /*metabolite, but later may add "seasons" to the model by making */
  /*this some sort of fluctuating periodic function.*/

int get_metabolite_update(int timestep, int met_type)
{

#ifdef UPDAT
   printf(" ---getting the metabolite update---\n");
#endif

  switch(met_type)
    {
    case 1: return ENVINPUT1;
    default: return 0;
    }
}

void get_env_update(int timestep, Gut *env_input)
{
  int i;

#ifdef UPDAT
   printf(" ---in get_env_update---\n");
#endif

  for(i=0; i < NUM_METABOLITES; i++)
    env_input[i] = get_metabolite_update(timestep, i);

#ifdef UPDAT
   print_gut(env_input);
   printf(" ---end get_env_update---\n");
#endif
}

void update_environment(Gut *env_input, Gut *the_env, int popsize)
{
  int i;

#ifdef UPDAT
   printf(" ---in update_environment---\n");
#endif

  for(i=0; i < NUM_METABOLITES; i++)
    the_env[i] += num_metabolites(popsize, env_input[i]);

#ifdef UPDAT
   printf(" ---end update_environment---\n");
#endif
}
  
/*  For debugging...

void main()
{
  Gut *env_stuff;
  Gut *the_environment;
  int i, j, popsize, timestep;

  popsize = 10;
  timestep = 999;
  
  seed_random(-1);

  env_stuff = (Gut *) calloc(NUM_METABOLITES, sizeof(Gut));

  the_environment = (Gut *) calloc(NUM_METABOLITES, sizeof(Gut));
  for(i=0; i < NUM_METABOLITES; i++)
    the_environment[i] = 0;
  
  for(i=0; i < 3; i++)
    {
      get_env_update(timestep, env_stuff);
      print_gut(env_stuff);
      printf("----------------------\n");
      for(j=0; j < popsize; j++)
	{
	  the_environment = update_environment(env_stuff,
					       the_environment,
					       popsize); 
	  print_gut(the_environment);
	}
      printf("\n");
    }
}

*/
