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

#define SIZE 16
#define true 1
#define false 0

/* piece encodings */
#define white 0
#define black 1
#define empty 0
#define man 2
#define crown 4
#define edge 8
#define taken 16
#define invalid 51

int board[52];

struct _color {
  int r;
  int g;
  int b;
};

struct _color image[SIZE*10][SIZE*10],c,k;
void read_board(char *buffer,FILE *in,int xx,int yy)
{
    int x,y,p=0,mp;
    for(y=0;y<yy;y++) {
         for(x=0;x<xx;x++) {
            if ((x+y)%2==1) {
                mp=p;
                board[mp]=empty;
                if (buffer[2*x]=='w' || buffer[2*x]=='W') board[mp]|=white;
                if (buffer[2*x]=='b' || buffer[2*x]=='B') board[mp]|=black;
                if (buffer[2*x+1]=='O' || buffer[2*x+1]=='w' || buffer[2*x+1]=='b') board[mp]|=man;
                if (buffer[2*x+1]=='X' || buffer[2*x+1]=='W' || buffer[2*x+1]=='B') board[mp]|=crown;
                p++;
                }
            }
        fgets(buffer,4000,in);
        }
}



void read_list(char *buffer,int color)
{
    int p,n,str=2;
    int piece;

    do {
        n=sscanf(buffer+str,"%i",&p);
        do str++; while(buffer[str]!=','); str++;
        piece=man; if (buffer[str-2]=='*') piece=crown;
        if (n>0) board[p-1]=piece|color;
        } while(n>0);
}

int load_board(char *filename)
{
    int p;
    FILE *in;
    char buffer[4000];

    in=fopen(filename,"r");
    if (in==NULL) {printf("file not found\n"); return(false);}
    for(p=0;p<50;p++) board[p]=empty;
    do {
        fgets(buffer,100,in);
        if (buffer[0]==' ') read_board(buffer,in,10,10);
        else if (buffer[0]=='W' && buffer[1]==':') read_list(buffer,white);
        else if (buffer[0]=='B' && buffer[1]==':') read_list(buffer,black);
        else if (buffer[0]=='W' && buffer[1]==' ') printf("%s\n",buffer);
        else if (buffer[0]=='B' && buffer[1]==' ') printf("%s\n",buffer);
        } while (!feof(in));
    fclose(in);
    return(true);
}

void set_field(int x,int y,int p)
{
    int i,j,u,v;
    double r1;

    if (board[p]==(white|man) || board[p]==(white|crown))
      {k.r=255; k.g=255; k.b=255;} else {k.r=0; k.g=0; k.b=0;}
    if ((x+y)%2==0) {c.r=200; c.g=200; c.b=255;}
    else {c.r=128; c.g=128; c.b=255;}

    for(i=0;i<SIZE;i++) for(j=0;j<SIZE;j++) {
      u=x*SIZE+i;
      v=y*SIZE+j;
      image[u][v].r=c.r;
      image[u][v].g=c.g;
      image[u][v].b=c.b;
      r1=sqrt((i+.5-SIZE/2)*(i+.5-SIZE/2)+(j+.5-SIZE/2)*(j+.5-SIZE/2));
      if (board[p]!=empty && (x+y)%2==1 && ((board[p]&crown)==0 || r1>SIZE/5.4)) {
	if (r1<SIZE/2.4) {
	  image[u][v].r=k.r;
	  image[u][v].g=k.g;
	  image[u][v].b=k.b;

	}

      }
    }
}

int
main (int argc, char *argv[])
{
  int Narg,i,x,y,p;
  char infile[512],outfile[512],povfile[512]={""},color;
  FILE *out,*pov;

  pov=NULL;
  /* read command line */
  i=1; if (argc>1) do {
    /* get number of arguments */
    Narg=0;
    do {
      Narg++;
      if ((i+Narg)>=argc) break;
      if (argv[i+Narg][0]=='-') break;
    } while(true);
    Narg--;

    if (strcmp(argv[i],"-in")==0) {
      strcpy(infile,argv[++i]);
    }
    if (strcmp(argv[i],"-out")==0) {
      strcpy(outfile,argv[++i]);
    }
    if (strcmp(argv[i],"-pov")==0) {
      strcpy(povfile,argv[++i]);
    }

    i++;
  } while(i<argc);

  if (load_board(infile)==false) exit(1);
  out=fopen(outfile,"wb"); if (out==NULL) {printf("error\n"); exit(1);}
  if (povfile[0]!=0) {
      pov=fopen(povfile,"wb");
       if (out==NULL) {printf("error\n"); exit(1);}
  }
  fprintf(out,"P6\n");
  fprintf(out,"# generated by draughts2pbm\n");
  fprintf(out,"# Michel Grimminck 1995\n");
  fprintf(out,"# \n");
  fprintf(out,"%i %i\n",SIZE*10,SIZE*10);
  fprintf(out,"255\n");
  p=0;
  for(y=0;y<10;y++) for(x=0;x<10;x++) {
    if (pov!=NULL && ((x+y)%2==1)) {
        if (board[p]!=empty) {
            if (board[p]==(white|man) || board[p]==(white|crown)) color='w';
            else color='b';
            fprintf(pov,"    object {%cpiece rotate <0,%.0f,0> translate <%f,0,%f>}\n",color,180*cos(p*10.0),8*(x-4.5),8*(y-4.5) );
            if ((board[p]&crown)!=0) fprintf(pov,"    object {%cpiece rotate <0,%.0f,0> translate <%f,1.6,%f>}\n",color,180*cos(p*20.0),8*(x-4.5),8*(y-4.5) );
            }
        }
    set_field(x,y,p);
    if ((x+y)%2==1) p++;
    }
  for(y=0;y<10*SIZE;y++) for(x=0;x<10*SIZE;x++) fprintf(out,"%c%c%c",image[x][y].r,image[x][y].g,image[x][y].b);
  fclose(out);
  if (pov!=NULL) fclose(pov);
  return(0);
}



