#include "head.h"

void covered(
	     location points[], /* All the info for all the points */
	     long length             /* Number of points in process */
	     )
{
  long temp,size,i,j,nextcount,counter;
  long lastcount,llastcount,lchild,rightdad;

  temp=length;
  for(size=0;temp>0;++size)       /* Find out length of a row */
    temp -= pow(2,(double) size);
  size-=1;

/* Deal with points other than end points first */
  lastcount=-1;
  counter = pow(2,(double) size)-1;
  nextcount=counter+pow(2,(double) size-1);

  for(i=1;i<counter;++i){          /* Loop for point in bottom row */
    rightdad=counter+1+(i+(i%2))/2;    /* minus end points. */

    if(points[i].life.max > 0)
      points[i].XplusG.max = 
	points[i-1].XplusG.max = points[i+1].XplusG.max = 
	points[rightdad-1].XplusG.max = 
	points[rightdad].XplusG.max = 1;

    if(points[i].life.min > 0)
      points[i].XplusG.min = points[i-1].XplusG.min = 
	points[i+1].XplusG.min = points[rightdad-1].XplusG.min = 
	points[rightdad].XplusG.min = 1;
  }

/* Now deal with end points */
  if(points[0].life.max > 0)
      points[0].XplusG.max = points[1].XplusG.max = 
	points[counter].XplusG.max = 
	points[counter+1].XplusG.max = 
	points[nextcount].XplusG.max = 1;

  if(points[0].life.min > 0)
      points[0].XplusG.min = points[1].XplusG.min = 
	points[counter].XplusG.min = 
	points[counter+1].XplusG.min = 
	points[nextcount].XplusG.min = 1;

  if(points[counter].life.max > 0)
      points[0].XplusG.max = points[counter-1].XplusG.max = 
	points[counter].XplusG.max = 
	points[counter+1].XplusG.max = 
	points[nextcount].XplusG.max = 1;

  if(points[counter].life.min > 0)
      points[0].XplusG.min = points[counter-1].XplusG.min = 
	points[counter].XplusG.min = 
	points[counter+1].XplusG.min = 
	points[nextcount].XplusG.min = 1;

/* The middle bit. */
  for(j=1;j<size-1;++j){
    llastcount=lastcount;
    lastcount=counter;
    counter=nextcount;
    nextcount+=pow(2,(double) (size-j-1));

/* The middle bit minus end points. */
    for(i=(lastcount+2);i<counter;++i){
      rightdad=counter+1+(i-lastcount-1+(i%2))/2;
      lchild=llastcount+(i-lastcount)*2-1;

      if(points[i].life.max > 0){
	points[i-1].XplusG.max = points[i+1].XplusG.max = 
	  points[rightdad-1].XplusG.max = 
	  points[rightdad].XplusG.max = 1;
	points[i].XplusG.max = points[lchild].XplusG.max = 
	  points[lchild+1].XplusG.max = 1;
	points[lchild+2].XplusG.max = 
	  points[lchild-1].XplusG.max = 1;
      }

      if(points[i].life.min > 0){
	points[i-1].XplusG.min=points[i+1].XplusG.min
	  =points[rightdad-1].XplusG.min = 
	  points[rightdad].XplusG.min = 1;
	points[i].XplusG.min = points[lchild].XplusG.min = 
	  points[lchild+1].XplusG.min = 1;
	points[lchild+2].XplusG.min = 
	  points[lchild-1].XplusG.min = 1;
      }
    }

/* Now deal with the end points in the middle bit. */
    if(points[lastcount+1].life.max > 0)
      points[lastcount+1].XplusG.max = 
	points[lastcount+2].XplusG.max = 
	points[lastcount].XplusG.max = 
	points[counter].XplusG.max = 
	points[llastcount+1].XplusG.max = 
	points[llastcount+2].XplusG.max	= 
	points[llastcount+3].XplusG.max = 
	points[counter+1].XplusG.max = 
	points[nextcount].XplusG.max = 1;

    if(points[lastcount+1].life.min > 0)
      points[lastcount+1].XplusG.min = 
	points[lastcount+2].XplusG.min = 
	points[lastcount].XplusG.min = 
	points[counter].XplusG.min = 
	points[llastcount+1].XplusG.min = 
	points[llastcount+2].XplusG.min	= 
	points[llastcount+3].XplusG.min = 
	points[counter+1].XplusG.min = 
	points[nextcount].XplusG.min = 1;

    if(points[counter].life.max > 0)
      points[lastcount+1].XplusG.max = 
	points[counter-1].XplusG.max = 
	points[lastcount].XplusG.max = 
	points[counter].XplusG.max = 
	points[llastcount+1].XplusG.max = 
	points[lastcount-1].XplusG.max = 
	points[lastcount-2].XplusG.max = 
	points[counter+1].XplusG.max = 
	points[nextcount].XplusG.max = 1;

    if(points[counter].life.min > 0)
      points[lastcount+1].XplusG.min = 
	points[counter-1].XplusG.min = 
	points[lastcount].XplusG.min = 
	points[counter].XplusG.min = 
	points[llastcount+1].XplusG.min = 
	points[lastcount-1].XplusG.min = 
	points[lastcount-2].XplusG.min = 
	points[counter+1].XplusG.min = 
	points[nextcount].XplusG.min = 1;
  }

/* Finally, deal with the last three points. */
  llastcount=lastcount;
  lastcount=counter;
  counter=nextcount;
  nextcount+=pow(2,(double) (size-j-1));

  if((points[lastcount+1].life.max > 0) || 
     (points[counter].life.max > 0)){
    points[lastcount+1].XplusG.max = 
      points[lastcount].XplusG.max = 
      points[counter].XplusG.max = 
      points[llastcount+1].XplusG.max = 
      points[llastcount+2].XplusG.max = 
      points[llastcount+3].XplusG.max = 
      points[nextcount].XplusG.max = 1;
  }

  if((points[lastcount+1].life.min > 0) || 
     (points[counter].life.min > 0)){
    points[lastcount+1].XplusG.min = 
      points[lastcount].XplusG.min = 
      points[counter].XplusG.min = 
      points[llastcount+1].XplusG.min = 
      points[llastcount+2].XplusG.min = 
      points[llastcount+3].XplusG.min = 
      points[nextcount].XplusG.min = 1;
  }

  if(points[nextcount].life.max > 0)
    points[nextcount].XplusG.max = 
      points[counter].XplusG.max = 
      points[lastcount+1].XplusG.max = 1;

  if(points[nextcount].life.min > 0)
    points[nextcount].XplusG.min = 
      points[counter].XplusG.min = 
      points[lastcount+1].XplusG.min = 1;
}
