/* em.cc --- the routines tyrrell's simulated enviroment call

Joanna Bryson, Nov 1 1997

$Log: em.cc,v $
Revision 1.4  1997/11/15 15:36:05  jo
the "final" version for the paper

Revision 1.1  1997/11/03 23:34:20  jo
Initial revision


*/

#include <stdio.h>
#include <stream.h>
#include <GetOpt.h>
#include <signal.h>
#include "em.hh"                // c++ stuff, see asm_decs.h below
#include "brain-objs.hh"
#include "brain.def"            // define the static/class variables

#include "asm_defs.h"
#include "asm_mydecs.h"         // tyrrell's code, includes defs for this file
#include "animal_defs.h"        // #defs for actions, directions

#include "em-dirs.hh"
#include "em.def"

static char rcsid[] =  "$Id: em.cc,v 1.4 1997/11/15 15:36:05 jo Exp jo $";

char *LOG_PATH = "../emfiles";
char *IOF_PATH = "../emfiles";	// lap and landmark files

int GlobalTickClock;  // how we tell time... set here!

Drive * life;  // has to be a global since we aren't the main loop

double P1SEE, P2SEE, P1SNIFF, P2SNIFF, AVOID, SHELTER;
extern "C" void asmain (int argc, char * argv[]);
extern "C" double rnd (double range);

main (int argc, char *argv[])
{
  char *lapfilename = "fuck.lap";  // argh, have to hard code!
  
  if (!(lapfilename)) {
      cerr << "Error:  in main, no lapfile provided (argument -l) \n";
    }


  // now we need to read in the LAPs...

  cerr << "\n LAP reading brains... ";
  initprims();                         // add the primitives (senses and acts)
  read_brains(lapfilename, IOF_PATH);  // does its own error checking

  cerr << "\n Control to slave. ";
  asmain(argc,argv);
}


void em_initialize() {

  // generate our parameters we're testing
  /* 
     P1SEE = .55 + rnd(0.2);
     P2SEE = .55 + rnd(0.2);
     P1SNIFF = .5 + rnd(0.2);
     P2SNIFF = .4 + rnd(0.2);
     AVOID = .45 + rnd(0.2);
     SHELTER = .6 + rnd(0.2);
     */
  P1SEE = .45;
  P2SEE = .72;
  P1SNIFF = .51;
  P2SNIFF = .45;
  AVOID = .35;
  SHELTER = .78;

  cerr << "\n getting life... ";
  life = Drive::get_drive("life");
  life = (Drive *) life->instantiate();

  GlobalTickClock = 0;  // start time as well as life :-)

  cerr << "Starting up... life =>\n";

}  // em_initialize()


int em_select_action() {
  static int res;
  action = -1;

  GlobalTickClock++;
  while ( (action < 0) && ((res = life->execute()) == CONTINUE) );

  if (action >= 0) 
    return (action);

  cerr << "Life ended!!!\n";             // should never happen...

  switch (res) {
  case SUCCESS:
    cout << "Yaaay, we won!\n"; break;
  case FAILURE:
    cout << "Boooo, we lost!\n"; break;
  default:
    cout << "Huh?";
  } // switch res
  exit (-4);
} // em_select_action()

