/* crystal */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

double ax,ay,az,bx,by,bz,cx,cy,cz;
long dim,i;
char hs[128],name[16],fn[128];
FILE *out;

main() {
 printf("\ncrystal V0.4(20 November 1994) - ©1994 by A.Maschke");
 printf("\nname: ");scanf("%s",&name);
 printf("\nax: ");scanf("%s",&hs);ax=strtod(hs,NULL);
 printf("ay: ");scanf("%s",&hs);ay=strtod(hs,NULL);
 printf("az: ");scanf("%s",&hs);az=strtod(hs,NULL);
 printf("\nbx: ");scanf("%s",&hs);bx=strtod(hs,NULL);
 printf("by: ");scanf("%s",&hs);by=strtod(hs,NULL);
 printf("bz: ");scanf("%s",&hs);bz=strtod(hs,NULL);
 printf("\ncx: ");scanf("%s",&hs);cx=strtod(hs,NULL);
 printf("cy: ");scanf("%s",&hs);cy=strtod(hs,NULL);
 printf("cz: ");scanf("%s",&hs);cz=strtod(hs,NULL);
 dim=1;
 printf("\n");
 while(dim<3) {
 printf("dim: ");scanf("%d",&dim);
 }
 strcpy(fn,name);
 strcat(fn,".bat");
 out=fopen(fn,"w");
 if (out!=NULL) {
  /* load objects */
  fprintf(out,"load(1,crystal/atom)");
  fprintf(out,"\nload(2,crystal/bond)");
  /* create bonds */
  fprintf(out,"\ncopy(2,12)");
  fprintf(out,"\ncopy(2,13)");
  fprintf(out,"\ncopy(2,14)");
  fprintf(out,"\nsetpoint(12,3,%f,%f,%f)",ax+2.0379,ay-1.1766,az);
  fprintf(out,"\nsetpoint(12,4,%f,%f,%f)",ax,ay+2.3532,az);
  fprintf(out,"\nsetpoint(12,5,%f,%f,%f)",ax-2.0379,ay-1.1766,az);
  fprintf(out,"\nsetpoint(13,3,%f,%f,%f)",bx+2.0379,by-1.1766,bz);
  fprintf(out,"\nsetpoint(13,4,%f,%f,%f)",bx,by+2.3532,bz);
  fprintf(out,"\nsetpoint(13,5,%f,%f,%f)",bx-2.0379,by-1.1766,bz);
  fprintf(out,"\nsetpoint(14,3,%f,%f,%f)",cx+2.0379,cy-1.1766,cz);
  fprintf(out,"\nsetpoint(14,4,%f,%f,%f)",cx,cy+2.3532,cz);
  fprintf(out,"\nsetpoint(14,5,%f,%f,%f)",cx-2.0379,cy-1.1766,cz);
  fprintf(out,"\nsave(12,vec1)");
  fprintf(out,"\nsave(13,vec2)");
  fprintf(out,"\nsave(14,vec3)");
  fprintf(out,"\nexit");

  fprintf(out,"\nload(1,crystal/atom)");
  fprintf(out,"\nload(2,crystal/bond)");
  fprintf(out,"\nload(12,vec1)");
  fprintf(out,"\nload(13,vec2)");
  fprintf(out,"\nload(14,vec3)");
  /* create atoms */
  fprintf(out,"\ncopy(1,3)");
  fprintf(out,"\ncopy(1,4)");
  for(i=2;i<=dim;i++) {
   fprintf(out,"\ntranslate(3,%f,x)",ax);
   fprintf(out,"\ntranslate(3,%f,y)",ay);
   fprintf(out,"\ntranslate(3,%f,z)",az);
   fprintf(out,"\njoin(3,4,5)");
   fprintf(out,"\ncopy(5,4)");
  }
  fprintf(out,"\ncopy(5,3)");
  fprintf(out,"\ncopy(5,4)");
  for(i=2;i<=dim;i++) {
   fprintf(out,"\ntranslate(3,%f,x)",bx);
   fprintf(out,"\ntranslate(3,%f,y)",by);
   fprintf(out,"\ntranslate(3,%f,z)",bz);
   fprintf(out,"\njoin(3,4,5)");
   fprintf(out,"\ncopy(5,4)");
  }
  fprintf(out,"\ncopy(5,3)");
  fprintf(out,"\ncopy(5,4)");
  for(i=2;i<=dim;i++) {
   fprintf(out,"\ntranslate(3,%f,x)",cx);
   fprintf(out,"\ntranslate(3,%f,y)",cy);
   fprintf(out,"\ntranslate(3,%f,z)",cz);
   fprintf(out,"\njoin(3,4,5)");
   fprintf(out,"\ncopy(5,4)");
  }
  fprintf(out,"\ncopy(5,10)");
  fprintf(out,"\nkill(3)");
  fprintf(out,"\nkill(4)");
  fprintf(out,"\nkill(5)");
  /* b1*/
  fprintf(out,"\ncopy(12,1)");
  fprintf(out,"\ncopy(1,3)");
  fprintf(out,"\ncopy(1,4)");
  for(i=3;i<=dim;i++) {
   fprintf(out,"\ntranslate(3,%f,x)",ax);
   fprintf(out,"\ntranslate(3,%f,y)",ay);
   fprintf(out,"\ntranslate(3,%f,z)",az);
   fprintf(out,"\njoin(3,4,5)");
   fprintf(out,"\ncopy(5,4)");
  }
  fprintf(out,"\ncopy(5,3)");
  fprintf(out,"\ncopy(5,4)");
  for(i=2;i<=dim;i++) {
   fprintf(out,"\ntranslate(3,%f,x)",bx);
   fprintf(out,"\ntranslate(3,%f,y)",by);
   fprintf(out,"\ntranslate(3,%f,z)",bz);
   fprintf(out,"\njoin(3,4,5)");
   fprintf(out,"\ncopy(5,4)");
  }
  fprintf(out,"\ncopy(5,3)");
  fprintf(out,"\ncopy(5,4)");
  for(i=2;i<=dim;i++) {
   fprintf(out,"\ntranslate(3,%f,x)",cx);
   fprintf(out,"\ntranslate(3,%f,y)",cy);
   fprintf(out,"\ntranslate(3,%f,z)",cz);
   fprintf(out,"\njoin(3,4,5)");
   fprintf(out,"\ncopy(5,4)");
  }
  fprintf(out,"\ncopy(5,20)");
  fprintf(out,"\nkill(3)");
  fprintf(out,"\nkill(4)");
  fprintf(out,"\nkill(5)");
  /* b2*/
  fprintf(out,"\ncopy(13,1)");
  fprintf(out,"\ncopy(1,3)");
  fprintf(out,"\ncopy(1,4)");
  for(i=2;i<=dim;i++) {
   fprintf(out,"\ntranslate(3,%f,x)",ax);
   fprintf(out,"\ntranslate(3,%f,y)",ay);
   fprintf(out,"\ntranslate(3,%f,z)",az);
   fprintf(out,"\njoin(3,4,5)");
   fprintf(out,"\ncopy(5,4)");
  }
  fprintf(out,"\ncopy(5,3)");
  fprintf(out,"\ncopy(5,4)");
  for(i=3;i<=dim;i++) {
   fprintf(out,"\ntranslate(3,%f,x)",bx);
   fprintf(out,"\ntranslate(3,%f,y)",by);
   fprintf(out,"\ntranslate(3,%f,z)",bz);
   fprintf(out,"\njoin(3,4,5)");
   fprintf(out,"\ncopy(5,4)");
  }
  fprintf(out,"\ncopy(5,3)");
  fprintf(out,"\ncopy(5,4)");
  for(i=2;i<=dim;i++) {
   fprintf(out,"\ntranslate(3,%f,x)",cx);
   fprintf(out,"\ntranslate(3,%f,y)",cy);
   fprintf(out,"\ntranslate(3,%f,z)",cz);
   fprintf(out,"\njoin(3,4,5)");
   fprintf(out,"\ncopy(5,4)");
  }
  fprintf(out,"\ncopy(5,21)");
  fprintf(out,"\nkill(3)");
  fprintf(out,"\nkill(4)");
  fprintf(out,"\nkill(5)");
  /* b3*/
  fprintf(out,"\ncopy(14,1)");
  fprintf(out,"\ncopy(1,3)");
  fprintf(out,"\ncopy(1,4)");
  for(i=2;i<=dim;i++) {
   fprintf(out,"\ntranslate(3,%f,x)",ax);
   fprintf(out,"\ntranslate(3,%f,y)",ay);
   fprintf(out,"\ntranslate(3,%f,z)",az);
   fprintf(out,"\njoin(3,4,5)");
   fprintf(out,"\ncopy(5,4)");
  }
  fprintf(out,"\ncopy(5,3)");
  fprintf(out,"\ncopy(5,4)");
  for(i=2;i<=dim;i++) {
   fprintf(out,"\ntranslate(3,%f,x)",bx);
   fprintf(out,"\ntranslate(3,%f,y)",by);
   fprintf(out,"\ntranslate(3,%f,z)",bz);
   fprintf(out,"\njoin(3,4,5)");
   fprintf(out,"\ncopy(5,4)");
  }
  fprintf(out,"\ncopy(5,3)");
  fprintf(out,"\ncopy(5,4)");
  for(i=3;i<=dim;i++) {
   fprintf(out,"\ntranslate(3,%f,x)",cx);
   fprintf(out,"\ntranslate(3,%f,y)",cy);
   fprintf(out,"\ntranslate(3,%f,z)",cz);
   fprintf(out,"\njoin(3,4,5)");
   fprintf(out,"\ncopy(5,4)");
  }
  fprintf(out,"\ncopy(5,22)");
  fprintf(out,"\nkill(3)");
  fprintf(out,"\nkill(4)");
  fprintf(out,"\nkill(5)"); 
  /*join all*/
  fprintf(out,"\njoin(20,21,23)");
  fprintf(out,"\njoin(22,23,24)");
  fprintf(out,"\ncopy(10,1)");
  fprintf(out,"\ncopy(24,2)");
  fprintf(out,"\nkill(10)");
  fprintf(out,"\nkill(12)");
  fprintf(out,"\nkill(13)");
  fprintf(out,"\nkill(14)");
  fprintf(out,"\nkill(20)");
  fprintf(out,"\nkill(21)");
  fprintf(out,"\nkill(22)");
  fprintf(out,"\nkill(23)");
  fprintf(out,"\nkill(24)");
  
  /*save it */
  fprintf(out,"\njoin(1,2,3)");
  fprintf(out,"\nsave(3,%s.obj)",name);
  fprintf(out,"\n"); 
  fclose(out);
 }
 else {
  printf("\nCouldn't open file.");
 }
 printf("\n");
}
