/*->c.data */
/* Panorama      (c) D. J. Pilling,  December 1990                     */
/*                          Main Section Code                          */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <time.h>
#include <locale.h>
#include <math.h>


#include "h.os"
#include "h.wimp"
#include "h.sprite"
#include "h.wimpt"
#include "h.bbc"
#include "h.akbd"
#include "h.werr"
#include "h.flex"

#include "h.DrawLevel0"


#include "h.wos"
#include "h.ram"
#include "h.file"
#include "h.main"
#include "h.DrawLib"
#include "h.pic"
#include "h.mym"


#include "h.pan"
#include "h.data"



char * dataname[7]=
{
 "<Panorama$Path>.data.coast",
 "<Panorama$Path>.data.island",
 "<Panorama$Path>.data.lake",
 "<Panorama$Path>.data.river",
 "<Panorama$Path>.data.country",
 "<Panorama$Path>.data.state",
 "<Panorama$Path>.data.city"

};





char * dataname2[7]=
{
 "<Panorama$Path>.data2.coast",
 "<Panorama$Path>.data2.island",
 "<Panorama$Path>.data2.lake",
 "<Panorama$Path>.data2.river",
 "<Panorama$Path>.data2.country",
 "<Panorama$Path>.data2.state",
 "<Panorama$Path>.data2.city"

};




FILE * fp;

int    pollcount;
int    dataset;

#define POLLEVERY 200


/* get a point from current data set, return 0 on end */


//#define SQUASH


#ifdef SQUASH





int getpoint(point * p)
{
 short int x[3];

 if(abended) return(0);
 if(feof(fp)) return(0);

 if(fread(x,sizeof(short),3,fp)!=3) return(0);

 p->type=x[0];
 p->y=x[1];
 p->x=x[2];

/*
 if(pollcount++>POLLEVERY) 
 {
  pollcount=0;
  poll(0);
 }
*/

 return(1);
}



int opendataset(int i)
{
 if(abended) return(0);

 dataset=i;
 fp=fopen(dataname2[i],"rb");
 return(fp!=NULL);
}


#else


int getpoint(point * p)
{
 short  temp;
 short  x[2];
 static point p2;
 int    mask;
 int    hdr;
 int    byte;

 if(abended) return(0);
 if(feof(fp)) return(0);

 if((byte=getc(fp))==EOF) return(0);

 mask=byte & 0xC0;
 hdr =byte & 0x3F;

 if(!mask)
 {
  hdr=(getc(fp))+(byte<<8);
 }

 if(hdr>=1000)  /* normal header */
 {

/*  if(dataset==USASTATE && hdr>=4107) return(0); */ /* a cut */

  if(fread(x,sizeof(short),2,fp)!=2) return(0);
  p->y=x[0];
  p->x=x[1];

 }
 else
 if(mask==(0x40))
 {
  if((byte=getc(fp))==EOF) return(0);

  p->y=p2.y-(byte>>4)+8;
  p->x=p2.x-(byte & 0xF)+8;
 }
 else
 if(mask==(0x80))
 {
  if((byte=getc(fp))==EOF) return(0);
  p->y=p2.y-byte+128;
  byte=getc(fp);
  p->x=p2.x-byte+128;
 }
 else
 {
  if(fread(x,sizeof(short),2,fp)!=2) return(0);
  p->y=x[0];
  p->x=x[1];
 }

 p->type=hdr;
 p2=*p;

/* dprintf(0,"hdr=%d x=%d y=%d mask=%d",hdr,p2.x,p2.y,mask);  */

 if(pollcount++>POLLEVERY) 
 {
  pollcount=0;
  poll(0);
 }

 return(1);
}


int opendataset(int i)
{
 if(abended) return(0);

 dataset=i;
 fp=fopen(dataname[i],"rb");
 return(fp!=NULL);
}


#endif




void closedataset(void)
{

 fclose(fp);
}




void squash(void)
{
 int   i;
 point p;
 point p2;
 int   temp;

 FILE  * fout;

 for(i=0;i<6;i++)
 {
  if(opendataset(i))
  {
   fout=fopen(dataname2[i],"wb");

   while(getpoint(&p))
   {
    if(p.type>=1000)
    {
     fputc(p.type >>   8,fout);
     fputc(p.type & 0xFF,fout);

     fputc(p.y    & 0xFF,fout);
     fputc(p.y    >>   8,fout);
     fputc(p.x    & 0xFF,fout);
     fputc(p.x    >>   8,fout);
    }
    else
    if(abs(p2.x-p.x)<8 && abs(p2.y-p.y)<8)
    {
     temp=p.type|(0x40);
     fputc(temp,fout);
     fputc( (((p2.x-p.x)+8) & 0xF)+
            ((((p2.y-p.y)+8) & 0xF)<<4),fout);
    }
    else 
    if(abs(p2.x-p.x)<128 && abs(p2.y-p.y)<128)
    {
     temp=p.type|(0x80);
     fputc(temp,fout);
     fputc((p2.y-p.y)+128,fout);
     fputc((p2.x-p.x)+128,fout);
    }
    else
    {
     fputc(p.type >>   8,fout);
     fputc(p.type & 0xFF,fout);

     fputc(p.y    & 0xFF,fout);
     fputc(p.y    >>   8,fout);
     fputc(p.x    & 0xFF,fout);
     fputc(p.x    >>   8,fout);
    }
    p2=p;
   }

   fclose(fout);

   closedataset();
  }
 }
}




/* get a city from current data set, return 0 on end */


int getcity(city * p)
{
 float  x;
 float  y;
 int    type;
 char   string[64];
 char * star;
 int    len;
 char * slash;

 while(1)
 {
  if(abended) return(0);
  if(feof(fp)) return(0);
  if(ferror(fp)) return(0);

  if(fscanf(fp,"%f%f%d",&y,&x,&type)==3)
  {
   p->type=type;
   p->x=(int)(x*60);
   p->y=(int)(y*60);

   fgets(string,64,fp);
   len=strlen(string);

   if(len && string[len-1]=='\n') string[len-1]=0;

   slash=strchr(string,'/');

   if(slash)
   {
    *slash=0;

    strcpy(p->name,string);

    star=strchr(slash+1,'*');

    if(star)
    {
     strcpy(p->country,star+1);
     *star=0;
     strcpy(p->state,slash+1);
    }
    else
    {
     p->country[0]=0;
     strcpy(p->state,slash+1);
    }
   }
   else
   {
    star=strchr(string,'*');

    if(star)
    {
     strcpy(p->country,star+1);
     *star=0;
     strcpy(p->name,string);
     p->state[0]=0;
    }
    else
    {
     p->country[0]=0;
     p->state[0]=0;
     strcpy(p->name,string);
    }
   }

   if(pollcount++>POLLEVERY) 
   {
    pollcount=0;
    poll(0);
   }

   return(1);
  }
  else fscanf(fp,"%s",string);
 }
}



static char * inputnames[]=
{
 "VMAP1/MAP",
 "WALES/PNT",
 "SCOTLAND/PNT",

};




void squash2(void)
{
 int     i;
 int     j;
 point   p;
 point   p2;
 int     temp;
 FILE  * fout[6];
 FILE  * fo;
 int     type;


 for(i=0;i<6;i++)
 {
  dprintf(0,"i=%d",i);
  fout[i]=fopen(dataname[i],"wb");
 }

 for(j=0;j<3;j++)
 {
  dprintf(1,"j=%d",j);
  fp=fopen(inputnames[j],"rb");

  fo=fout[0];

  while(getpoint(&p))
  {
   if(p.type>=1000)
   {
    type=p.type/1000;
    if(type==1) fo=fout[0];    /* coastlines  */
    else
    if(type==2) fo=fout[4];    /* countries */
    else
    if(type==3) fo=fout[5];    /* canadian provinces */
    else
    if(type==4) fo=fout[5];    /* US states  */
    else
    if(type==5) fo=fout[1];    /* Islands */
    else
    if(type==6) fo=fout[2];    /* Lakes */
    else
    if(type==7) fo=fout[3];    /* Rivers */
    else
    if(type==8) fo=fout[5];    /* Oz states */
    else        dprintf(0,"bad type %d",type);

    fputc(p.type >>   8,fo);
    fputc(p.type & 0xFF,fo);

    fputc(p.y    & 0xFF,fo);
    fputc(p.y    >>   8,fo);
    fputc(p.x    & 0xFF,fo);
    fputc(p.x    >>   8,fo);
   }
   else
   if(abs(p2.x-p.x)<8 && abs(p2.y-p.y)<8)
   {
    temp=p.type|(0x40);
    fputc(temp,fo);
    fputc( (((p2.x-p.x)+8) & 0xF)+
            ((((p2.y-p.y)+8) & 0xF)<<4),fo);
   }
   else 
   if(abs(p2.x-p.x)<128 && abs(p2.y-p.y)<128)
   {
    temp=p.type|(0x80);
    fputc(temp,fo);
    fputc((p2.y-p.y)+128,fo);
    fputc((p2.x-p.x)+128,fo);
   }
   else
   {
    fputc(p.type >>   8,fo);
    fputc(p.type & 0xFF,fo);

    fputc(p.y    & 0xFF,fo);
    fputc(p.y    >>   8,fo);
    fputc(p.x    & 0xFF,fo);
    fputc(p.x    >>   8,fo);
   }
   p2=p;
  }

  fclose(fp);
 }

 for(i=0;i<6;i++)
 {
  fclose(fout[i]);
 }
}




