/* This is the body of the model of parasitism and mutualism.

At this point, I'm working on the basic outline of the program, so
others can fill in the pieces.

When there is a piece left undone, that needs future attention, I mark
it with a  ***

6/18/94  CM.

*/


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "types.h"


/* Headers  ------------------------------------------------------*/
#include "main.h"
#include "init.h"

/* Constants -----------------------------------------------------*/
#define LOOP 1


/* Main ----------------------------------------------------------*/

   /* The main program body does three important things:
        1. It initializes the model.
	2. It runs the population.
	3. It cleans up after itself.  (gool little model!)
   */

void main(int argc, char **argv)
{
/*
Context *the_context;
Population *the_population;
Stats *stats;
Output_file *output_file;
*/
int error;

  int time_step;

  if (argc < 2)
     {
     printf(" Usage: %s <parameter file>\n", argv[0]);
     return;
     }  

  the_context = initialize_context(argv[1]);
  if (the_context == NULL) return;

  output_file = init_output_files(the_context);
  if (output_file == NULL) return;

  error = init_random(the_context, output_file);
  if (error == ERROR) return;

  the_population = initialize_population();
  if (the_population == NULL) return;

  stats = initialize_stats();

  for(time_step=0; 
      time_step < the_context->max_steps &&
      the_population->current_size > 0; 
      time_step++)
    {
      the_context->time_step =  time_step;
#ifdef LOOP
   printf(" time_step = %d\n", the_context->time_step);
#endif
      run_population_once(the_context, the_population);
      gather_statistics();  /*** calls write_each_timestep */
      save_important_info(the_context, the_population);
    }

  clean_up(the_context, the_population);

}


/* Procedures -----------------------------------------------------*/

  /* This is the heart of the model.  It handles all the dynamics of */
  /* the population for one time step.  This means:

	1. Letting the critters associate
	   A. Breaking attatchments.
	   B. Giving unattatched critters a chance to attatch
	   themselves.

	2. Letting the critters metabolize their resources.
	   This means looping through the population once for each
	   reaction in the critters' metabolisms.
	   *** If we are clever we won't have to re-examine critters
	   who have finished metabolizing this turn ***
	   Food is inserted into the environment in this step also.

	3. Letting critters reproduce.  (mother and daugther each get
	half of the mother's energy and gut contents) Some information
	will need to be saved here, like new metabolisms and the
	genealogy of the critters.

	4. Updating the population (is this different from updating
	the environment?): age critters, tax them, etc.

	5. Removing dead critters from the population. (being careful
	not to lose important info)

   */

void run_population_once(Context *the_context, Population
			 *the_population)
{
#ifdef DEBUG
  printf(" ---in run_population_once---\n");
#endif

  handle_associations(the_population);
  let_critters_metabolize(the_population);
  multiply_and_be_fruitful(the_population); /***This will require the */
					    /***stats object.*/
  update_population(the_population);
  weed_out_the_dead(the_population);

#ifdef DEBUG
  printf(" ---end run_population_once---\n");
#endif
}



void save_important_info(Context *the_context, Population
			 *the_population)
{
#ifdef DEBUG
  printf(" ---in save_important_info---\n");
#endif
#ifdef DEBUG
  printf(" ---end save_important_info---\n");
#endif
}




void clean_up(Context *the_context, Population *the_population)
{
#ifdef DEBUG
  printf(" ---in clean_up---\n");
#endif
  free(the_context);
  free(the_population);
#ifdef DEBUG
  printf(" ---end clean_up---\n");
#endif
}



