/* I'm getting a segmentation fault after the end of init population. */
/* So I had better put in some ifdefs.*/


/* These procedures translate a binary chomosome for the metabolism */
/* into a fast metabolism, consisting of a linked list of Reaction */
/* structures.  The chromosome is coded in unary.  Each number */
/* separated by a 0.  A pair of numbers makes up a reaction.  If the */
/* first number is > than the second number it is a "breakdown" */
/* reaction, otherwise it is a "creation" reaction.  A 00 indicates */
/* the end of the coding section.*/


#include <stdio.h>
#include <stdlib.h>
#include "types.h"
#include "trans_metab.h"

/*#define TRANS 1 */


void genes_to_metab(Critter *offspring)
{
  int index;
  Boolean found_end, found_start;
  int first, second;
  Reaction *a_reaction, *prev_reaction;

#ifdef TRANS
   printf(" ---in genes_to_metab---\n");
#endif
  
  offspring->fast_metabolism = NULL;

  for(index=0, found_end=FALSE, found_start=FALSE; 
      index < offspring->meta_len && !found_end;)
    {
      first = get_next_reactant(offspring->metabolism, 
				index, offspring->meta_len);
      index += first + 1; /*increment index past the first */
                          /*reactant*/
#ifdef TRANS
   printf("index = %d, first = %d\n", index, first);
#endif

      if (first != 0 && index < offspring->meta_len) 
	/*If we haven't finished yet...*/
	{
	  second = get_next_reactant(offspring->metabolism, 
				     index,
				     offspring->meta_len);
	  if (second == 0) found_end = TRUE;
	  else 
	    { /*We've found a viable reaction, go ahead and make the */
	      /*reaction, linking it into the fast_metabolism.*/
	      index += second + 1;
	      a_reaction = make_reaction(first, second);
	      
	      if (!found_start) /*The first reaction must be linked */
				/*into the critter via "fast_metabolism"*/
		{
		  found_start = TRUE;
		  offspring->fast_metabolism = a_reaction;
		}
	      else prev_reaction->next_reaction = a_reaction;

	      prev_reaction = a_reaction;
	    }
	}
      else found_end = TRUE;
    }

  if (found_start) a_reaction->next_reaction = NULL;

#ifdef TRANS
   printf(" ---end genes_to_metab---\n");
#endif
}



  /* Given a chromosome of a metabolism and a starting point in that */
  /* metabolism, this returns the next number, coded in unary in the */
  /* chromosome, or 0 if it finds the end of the chromosome (coded by */
  /* 00).  We assume that a leading 0 has already been passed over, so */
  /* the index should point to a 1.  If it points to a 0, then we know */
  /* that two 0's were encountered.*/

int get_next_reactant(Chromosome the_metabolism, int index, int meta_length)
{
  int accumulate;
  
#ifdef TRANS
   printf(" ---in get_next_reactant---\n");
#endif

  for(accumulate=0; index < meta_length && the_metabolism[index] != 0;
      index++, accumulate++);

#ifdef TRANS
   printf("index = %d \t accumulate = %d\n", index, accumulate);
   printf(" ---end get_next_reactant---\n");
#endif

  if (accumulate >= NUM_METABOLITES) return 0;
  else return accumulate;
}


  /* This takes a chromosome of the metabolism and returns the next */
  /* reaction structure after the "index" position, or NULL if there */
  /* are no more reactions.*/


Reaction *make_reaction(int first, int second)
{
  Reaction *a_reaction;

#ifdef TRANS
   printf(" ---in make_reaction---\n");
#endif

  a_reaction = (Reaction *) malloc(sizeof(Reaction));

  if (first <= second) a_reaction->reaction_type = CREATION;
  else a_reaction->reaction_type = BREAKDOWN;

  a_reaction->first_reactant = first;
  a_reaction->second_reactant = second;

#ifdef TRANS
   printf(" ---end make_reaction---\n");
#endif

  return a_reaction;
}


  

