// Mike Gordon

/**************** PVM version of this piece of code ***************************/


#include <stdio.h>
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "glaux.h"
#include "/usr/pvm3/include/pvm3.h"

#define MAXNUMCOL 700 //maximun size of result array

#define MAXNUMROW 700 // max number of tasks
#define MAXTASKS MAXNUMROW
  
#include "missile.cpp"

/******************************************************************************/
/************************** main **********************************************/
/******************************************************************************/

//global variables (sorry, it just works better)
float xdist,ydist,minx,miny;
int nproc,ncols;

double scalerow(int y)
{
  return ( (ydist*y/(nproc-1)) + miny); // nproc = number of rows
}

double scalecol(int x)
{
  return ( (xdist*x/(ncols-1)) + minx);
}

void main()
{
  int mytid = pvm_mytid();

  // arrays
  int tids[MAXTASKS];
  int result[MAXNUMCOL];

  // vars
  int me; // my row number
  int i,x; // loop control
  int master; // id of calling process
  int msgtype; // the type of message
  float dist,range;
  double time;
  double tstep = 0.1;
  int iter;

  // get message
  msgtype = 0;
  pvm_recv(-1,msgtype);
  pvm_upkint(&nproc,1,1); // number of rows / processes
  pvm_upkint(&ncols,1,1); // number of colums
  pvm_upkint(tids,nproc,1); // tid array
  pvm_upkfloat(&dist,1,1); //dist 
  pvm_upkfloat(&range,1,1); // range
  pvm_upkfloat(&xdist,1,1); // total change in velocity to be calculated
  pvm_upkfloat(&minx,1,1); // min velocity
  pvm_upkfloat(&ydist,1,1); // total change in angle to be calculated
  pvm_upkfloat(&miny,1,1); // min angle

  // find row number
  for(i=0;i<nproc;i++)
  {
    if (mytid == tids[i])
    {
      me = i;
      break;
    }
  }

  Missile.getdist(dist);
  Missile.getrange(range);
  Missile.gettheta( scalerow(me) );

  for (x=0;x<ncols;x++) // for all pixls in row
  {
    Missile.getvelocity( scalecol(x) * 10 ); //x axis is in 10's of units
    iter = 0;
    time = 0.0 + tstep;
    while ( Missile.yval(time) > 0.0 )
    {
      time += tstep;
      iter++;
    }
    if (Missile.test(Missile.xval(time)))
    {
      result[x]=iter;
    }
    else
    {
      result[x]=0;
    }
  }

  // send results back
  pvm_initsend(PvmDataDefault);
  pvm_pkint(&me,1,1);
  pvm_pkint(result,ncols,1);
  msgtype=5;
  master=pvm_parent();
  pvm_send(master,msgtype);

  pvm_exit();
} 

/******************************************************************************/
/****************************** end of main ***********************************/
/******************************************************************************/

