#include <stdio.h>
#include "structs.h"
#include <osbind.h>




loadtemplate(fname)
char *fname;
{
FILE *datfile;
int i,j,color,SA;
int np,npoly,nlines,nspheres,npoints;
POLY *pp;
LINE *lp;
SPHERE *sp;
POINT *ptp;
long pt;
int t;
char out[80];
Point3 *ref;
long rad;
long mlen;


datfile=fopen(fname,"rb");
if (!datfile) {
        form_alert(1,"[2][That file doesn't exist  | you idiot! ][ Sorry Boss! ]");
	v_clsvwk(vhandle);
	Mfree(block);
	appl_exit();
        exit(0);
        }

fscanf(datfile,"%d",&np);

fscanf(datfile,"%d",&npoly);

fscanf(datfile,"%d",&nlines);
fscanf(datfile,"%d",&nspheres);

fscanf(datfile,"%d",&npoints); 


NTEMPLATE++;
t=NTEMPLATE;

mlen=(long)sizeof(TEMPLATE);

templist[t]=(TEMPLATE *)malloc(mlen);

templist[t]->np=np;
templist[t]->npoly=npoly;

mlen=(long)sizeof(POLY)*(long)npoly;

if (npoly) templist[t]->polylist=(POLY *)malloc(mlen);
        else templist[t]->polylist=0;


templist[t]->nlines=nlines;
if (nlines) templist[t]->linelist=(LINE *)malloc(sizeof(LINE)*nlines);
        else templist[t]->linelist=0;

templist[t]->nspheres=nspheres;
if (nspheres) templist[t]->spherelist=(SPHERE *)malloc(sizeof(SPHERE)*nspheres);
        else templist[t]->spherelist=0;

templist[t]->npoints=npoints;
if (npoints) templist[t]->pointlist=(POINT *)malloc(sizeof(POINT)*npoints);
        else templist[t]->pointlist=0;

templist[t]->bsphere=20;

mlen=(long)np*(long)sizeof(Point3);

templist[t]->points=(Point3 *)malloc(mlen);

ref=templist[t]->points;

for (i=0;i<np;i++)
        {
       fscanf(datfile,"%ld",&pt);
         (ref+i)->x=pt;
       fscanf(datfile,"%ld",&pt); 
        (ref+i)->y=pt;
       fscanf(datfile,"%ld",&pt);
        (ref+i)->z=pt;
        }

if (npoly)
       {
        pp=templist[t]->polylist;
        for (i=0;i<npoly;i++)
          {
            fscanf(datfile,"%d",&np);
            (pp+i)->np=np;
        
            fscanf(datfile,"%d",&color);
            (pp+i)->color=color;
        
            fscanf(datfile,"%d",&SA);
            (pp+i)->SA=SA;
        
            for (j=0;j<np;j++)
                fscanf(datfile,"%d",&(pp+i)->pts[j]);
        }
       }


if (nlines)
        {
        lp=templist[t]->linelist;
        for (i=0;i<nlines;i++)
          {
            fscanf(datfile,"%d",&(lp+i)->color);
            fscanf(datfile,"%d",&(lp+i)->SA);
            fscanf(datfile,"%d",&(lp+i)->bp);
            fscanf(datfile,"%d",&(lp+i)->ep);
         }
        }

if (nspheres)
        {
        sp=templist[t]->spherelist;
        for (i=0;i<nspheres;i++)
        {
        fscanf(datfile,"%d",&(sp+i)->color);
        fscanf(datfile,"%d",&(sp+i)->SA);
        fscanf(datfile,"%d",&(sp+i)->center);
        fscanf(datfile,"%ld",&(sp+i)->radius);
        }
        }

if (npoints)
        {
        ptp=templist[t]->pointlist;
        for (i=0;i<npoints;i++)
        {
        fscanf(datfile,"%d",&(ptp+i)->color);
        fscanf(datfile,"%d",&(ptp+i)->SA);
        fscanf(datfile,"%d",&(ptp+i)->center);
        }
        }


fclose(datfile);
return (t);

}


void loadscene(fname)
char *fname;
{
FILE *tfile;
char f2[15];
long x,y,z;
int t,p,r;
int tem=0;
int temnums[50];
Point3 center;
int objnum;

tfile=fopen(fname,"rb");
if (!tfile)
        {
        form_alert(1,"[2][No such Scenery file  | you idiot! ][ Sorry Boss! ]");
        exit(0);
        }

while(1)
{
fscanf(tfile,"%s",f2);
if (f2[0]=='~') goto next;
temnums[tem++]=loadtemplate(f2);
}

next:
;

while(1)
{
fscanf(tfile,"%d %ld %ld %ld %d %d %d",&tem,&x,&y,&z,&t,&p,&r);
if ((tem==-1) || (feof(tfile))) goto done;
center.x=x;
center.y=y;
center.z=z;

objnum=createobject(center,temnums[tem]);
objectlist[objnum]->theta=t;
objectlist[objnum]->phi=p;
objectlist[objnum]->rho=r;
}

done:
;

fclose(tfile);
}


