#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wgt4.h>
#include <io.h>
#include <printer.h>
#include <conio.h>

extern unsigned _stklen = 16000U;

static int oldmode;


block dummy;
block virtscreen;
block screenbuf;
color scpalette[256];
color oldpalette[256];
wgtfont tiny;
wgtfont little;
char *filesname;
int singlefile;
char singlepath[255];
int filestart,fileend,stopp;
FILE *fr;
char origpath[255];
int origdisk;
int dotmatrix;
int laser;
int pr;
/*filestoprint 0-79 WORLD*/
/*filestoprint 80-87 DUNGEON*/

static char *wfilestoprint[96]={
			"worldmap\\MAP1A.PCX",
			"worldmap\\MAP1B.PCX",
			"worldmap\\MAP1C.PCX",
			"worldmap\\MAP1D.PCX",
			"worldmap\\MAP1E.PCX",
			"worldmap\\MAP1F.PCX",
			"worldmap\\MAP1G.PCX",
			"worldmap\\MAP1H.PCX",
			"worldmap\\MAP1I.PCX",
			"worldmap\\MAP1J.PCX",
			"worldmap\\MAP1K.PCX",
			"worldmap\\MAP1L.PCX",
			"worldmap\\MAP1M.PCX",
			"worldmap\\MAP1N.PCX",
			"worldmap\\MAP1O.PCX",
			"worldmap\\MAP1P.PCX",
			"worldmap\\MAP2A.PCX",
			"worldmap\\MAP2B.PCX",
			"worldmap\\MAP2C.PCX",
			"worldmap\\MAP2D.PCX",
			"worldmap\\MAP2E.PCX",
			"worldmap\\MAP2F.PCX",
			"worldmap\\MAP2G.PCX",
			"worldmap\\MAP2H.PCX",
			"worldmap\\MAP2I.PCX",
			"worldmap\\MAP2J.PCX",
			"worldmap\\MAP2K.PCX",
			"worldmap\\MAP2L.PCX",
			"worldmap\\MAP2M.PCX",
			"worldmap\\MAP2N.PCX",
			"worldmap\\MAP2O.PCX",
			"worldmap\\MAP2P.PCX",
			"worldmap\\MAP3A.PCX",
			"worldmap\\MAP3B.PCX",
			"worldmap\\MAP3C.PCX",
			"worldmap\\MAP3D.PCX",
			"worldmap\\MAP3E.PCX",
			"worldmap\\MAP3F.PCX",
			"worldmap\\MAP3G.PCX",
			"worldmap\\MAP3H.PCX",
			"worldmap\\MAP3I.PCX",
			"worldmap\\MAP3J.PCX",
			"worldmap\\MAP3K.PCX",
			"worldmap\\MAP3L.PCX",
			"worldmap\\MAP3M.PCX",
			"worldmap\\MAP3N.PCX",
			"worldmap\\MAP3O.PCX",
			"worldmap\\MAP3P.PCX",
			"worldmap\\MAP4A.PCX",
			"worldmap\\MAP4B.PCX",
			"worldmap\\MAP4C.PCX",
			"worldmap\\MAP4D.PCX",
			"worldmap\\MAP4E.PCX",
			"worldmap\\MAP4F.PCX",
			"worldmap\\MAP4G.PCX",
			"worldmap\\MAP4H.PCX",
			"worldmap\\MAP4I.PCX",
			"worldmap\\MAP4J.PCX",
			"worldmap\\MAP4K.PCX",
			"worldmap\\MAP4L.PCX",
			"worldmap\\MAP4M.PCX",
			"worldmap\\MAP4N.PCX",
			"worldmap\\MAP4O.PCX",
			"worldmap\\MAP4P.PCX",
			"worldmap\\LEGMAP1.PCX",
			"worldmap\\LEGMAP2.PCX",
			"worldmap\\LEGMAP3.PCX",
			"worldmap\\LEGMAP4.PCX",
			"worldmap\\LEGMAP5.PCX",
			"worldmap\\LEGMAP6.PCX",
			"worldmap\\LEGMAP7.PCX",
			"worldmap\\LEGMAP8.PCX",
			"worldmap\\LEGMAP9.PCX",
			"worldmap\\LEGMAP10.PCX",
			"worldmap\\LEGMAP11.PCX",
			"worldmap\\LEGMAP12.PCX",
			"worldmap\\LEGMAP13.PCX",
			"worldmap\\LEGMAP14.PCX",
			"worldmap\\LEGMAP15.PCX",
			"worldmap\\LEGMAP16.PCX",
			"dunmap\\dmapa.pcx",
			"dunmap\\dmapb.pcx",
			"dunmap\\dmapc.pcx",
			"dunmap\\dmapd.pcx",
			"dunmap\\dmape.pcx",
			"dunmap\\dmapf.pcx",
			"dunmap\\dmapg.pcx",
			"dunmap\\dmaph.pcx",
			"citymap\\cmapa.pcx",
			"citymap\\cmapb.pcx",
			"citymap\\cmapc.pcx",
			"citymap\\cmapd.pcx",
			"citymap\\legmap1.pcx",
			"citymap\\legmap2.pcx",
			"citymap\\legmap3.pcx",
			"citymap\\legmap4.pcx" };

void nofiles()
{
moff();
wcls(0);
wtextcolor(255);
wouttextxy(90,100,NULL,"No files to print");
wouttextxy(90,175,NULL,"*Press any Key*");
getch();
pr=0;
mon();
}

void getstartend()
{

stopp=0;
wcls(0);
	dummy=wloadpcx256("prcdw.pcx",scpalette);
	wsetpalette(0,255,scpalette);
	wputblock(0,0,dummy,0);
	wfreeblock(dummy);
mon();

	while(stopp==0){
		if(but==1){
			noclick();
			if((mx>89)&&(mx<193)&&(my>67)&&(my<96)){
							filestart=88;
							fileend=95;
							stopp=1;
			 if((fr=fopen("citymap\\cmapa.pcx","rb"))==NULL){
						filestart=0;
						fileend=0;
						stopp=1;
						fclose(fr);
						nofiles();
						}
							}/*CITY*/
			if((mx>89)&&(mx<193)&&(my>104)&&(my<141)){
							 stopp=1;
							 filestart=80;
							 fileend=87;
			 if((fr=fopen("dunmap\\dmapa.pcx","rb"))==NULL){
						filestart=0;
						fileend=0;
						stopp=1;
						fclose(fr);
						nofiles();
						}
							 }/*DUNGEON*/
			if((mx>89)&&(mx<193)&&(my>150)&&(my<183)){
							 stopp=1;
							 filestart=0;
							 fileend=79;
			 if((fr=fopen("worldmap\\map1a.pcx","rb"))==NULL){
						filestart=0;
						fileend=0;
						stopp=1;
						fclose(fr);
						nofiles();
						}
							 }/*WORLD*/
		}
	}
moff();         /*turn mouse off*/
stopp=0;
wcls(0);
}

void printimage()
{

	/* install defaults */
	Verbose = FALSE;        /* not verbose */
	Brightness = 0; /* use calculated brightness */
	PrintMode = POSPRINT;   /* print whites as white */
	Matrix = BayerMatrix;   /* use BayerMatrix */


/* Print the image displayed on the screen */
PrtScreen(PrintMode,1);  /*1=320x200,2=640x200,3=640x480*/

}

void textprint(char *filep)
{
FILE *outf;
int ln,s,page;
char hardline[81];
s=0;
page=1;
ln=1;
wtextcolor(255);
   if((outf=fopen(filep,"r"))!=NULL){
       wouttextxy(106,100,NULL,"Printing Text");
       wfade_out(0,254,30,scpalette);
       if(laser==1) {                fprintf(stdprn,"\x1B E"); /*laser reset*/
				     fprintf(stdprn,"\x1B*rB");/*default*/
				     fprintf(stdprn,"\x1B(8U");/*roman-8*/
				     fprintf(stdprn,"\x1B(s6T");/*gothic*/
				     }
       if(dotmatrix==1) fprintf(stdprn,"\x1B\x40");
	while(s==0){
		if(feof(outf)) s=1;
		fgets(hardline,80,outf);
		if(feof(outf)) s=1;
		if(laser==1){
				fprintf(stdprn,"%s\r",hardline);
			    }
		if(dotmatrix==1){
				fprintf(stdprn,"%s",hardline);
				}
		ln++;
		if(ln>54){
		  if(dotmatrix==1){
		  fprintf(stdprn,"\n\n");
		  fprintf(stdprn,
		    "                                 page [%d]\n",page);
		  fprintf(stdprn,"\f");
		  delay(2000);
		  ln=1;
		  page++;
		  }
		  if(laser==1){
		  fprintf(stdprn,"\n\r\n\r");
		  fprintf(stdprn,
		    "                                 page [%d]\n\r",page);
		  fprintf(stdprn,"\f");
		  delay(2000);
		  ln=1;
		  page++;
		  }
		}
	}/*end while*/
	fclose(outf);
	fprintf(stdprn,"\f");

      if(laser==1) {                 fprintf(stdprn,"\x1B E"); /*laser reset*/
				     fprintf(stdprn,"\x1B*rB");/*default*/
				     fprintf(stdprn,"\x1B(8U");/*roman-8*/
				     }

      if(dotmatrix==1) fprintf(stdprn,"\x1B\x40");
    }
    else {
	  pr=0;
	  nofiles();
	 }
    wcls(0);
}


void main()
{
  int advance;
  int fln;

  int music;
  int prindiv,prall;
  int prcity,prdungeon,prworld;

  prcity=0;
  prdungeon=0;
  prworld=0;
  prindiv=0;
  prall=0;
  pr=0;
  filestart=0;
  fileend=0;
  dotmatrix=0;
  laser=0;
  stopp=0;

  vga256();
  wcls(0);                     /* Clear the screen */


  origdisk=getdisk();           /* Find current disk drive */
  getcwd(origpath,255);         /* And the current pathname */

	/*setpassword("orcus");*/
	fr=fopen("data\\world.cfg","r");
	fscanf(fr,"%d",&music);   /*1=music 0=no music*/
	fscanf(fr,"%d",&pr);   /*1=laser 0=dotmatrix*/
	fclose(fr);
	if(pr==1) laser=1;
	if(pr==0) dotmatrix=1;

	pr=0; /*no quit set*/

  setlib("graphics\\hardcopy.lib");
  tiny = wloadfont ("tiny.wfn");    /* Load our font for selector */
  little= wloadfont("little.wfn");
  filefont = little;                    /* Set the font variable */

	wloadpalette("default.pal",oldpalette);
	dummy=wloadpcx256("hardcopy.pcx",scpalette);
	wsetpalette(0,255,scpalette);
	wputblock(0,0,dummy,0);
	wfreeblock(dummy);

	minit();

msetbounds(0,0,319,199);
mon();      /*turn mouse on*/
	while(stopp==0){
		if(but==1){
			noclick();
			if((mx>86)&&(mx<149)&&(my>157)&&(my<176)){
							pr=1; /*print!*/
							stopp=1;
							}
			if((mx>154)&&(mx<218)&&(my>157)&&(my<176)){
							 stopp=1;/*quit!*/
							 }
		}
	}
moff();         /*turn mouse off*/

  if(pr==1){
	wcls(0);
	dummy=wloadpcx256("mappr.pcx",scpalette);
	wsetpalette(0,255,scpalette);
	wputblock(0,0,dummy,0);
	wfreeblock(dummy);
	msetbounds(0,0,319,199);
	stopp=0;
	mon();      /*turn mouse on*/
	while(stopp==0){
		if(but==1){
			noclick();
			if((mx>20)&&(mx<96)&&(my>130)&&(my<141)){
							prcity=1;
							prdungeon=0;
							prworld=0;
							stopp=1;
							}/*print city*/
			if((mx>102)&&(mx<210)&&(my>131)&&(my<141)){
							prdungeon=1;
							prcity=0;
							prworld=0;
							stopp=1;
							}/*print dungeon*/
			if((mx>117)&&(mx<302)&&(my>131)&&(my<141)){
							prworld=1;
							prdungeon=0;
							prcity=0;
							stopp=1;
							}/*print world*/
			if((mx>59)&&(mx<258)&&(my>44)&&(my<76)){
							prall=1;
							pr=1;
							stopp=1;
							}/*print all*/
			if((mx>133)&&(mx<183)&&(my>178)&&(my<194)){
							prall=0;
							pr=0;
							prcity=0;
							prdungeon=0;
							prworld=0;
							stopp=1;
							} /*EXIT*/

			if((mx>60)&&(mx<259)&&(my>82)&&(my<111)){
				wcls(0);
				wsetpalette(0,255,oldpalette);
				screenbuf=wnewblock(0,0,319,199);

				filesname=wfilesel ("pcx",
				"Load MAP File", 70, 10, screenbuf);
				prindiv=1;
				pr=1;
				singlefile=getdisk();
				getcwd(singlepath,255);
				if(filesname==NULL){
						    prindiv=0;
						    pr=0;
						    prall=0;
						    stopp=1;
						    setdisk (origdisk);
						    chdir (origpath);
						    }
				stopp=1;
				wfreeblock(screenbuf);
			}
		}
	}
  moff();         /*turn mouse off*/

  } /*end checking to see if print individual or all*/
if(stopp==1) stopp=0;
/*PRINT TEXT*/
if(prcity==1){   textprint("text\\city.txt");
		 pr=0;
		 stopp=1;
	     }
if(prdungeon==1){ textprint("text\\dungeon.txt");
		 pr=0;
		 stopp=1;
	     }
if(prworld==1){  textprint("text\\legend.txt");
		 pr=0;
		 stopp=1;
	     }


if((pr==1)&&(prall==1)) getstartend();
if(pr==1) setlib(NULL);

  wcls(0);
 if(pr==1){
  virtscreen=wnewblock(0,0,319,199);
  wtextcolor(255);
  if(prall==1) wouttextxy(106,100,NULL,"Printing Maps");
  if(prindiv==1) wouttextxy(106,100,NULL,"Printing Map");
  wtextcolor(0);
  wsetscreen(virtscreen);

  if(prall==1){
    for(fln=filestart;fln<=fileend;fln++){
    setdisk (origdisk);
    chdir (origpath);
      wcls(0);
	dummy=wloadpcx256(wfilestoprint[fln],scpalette);
	wsetpalette(0,255,scpalette);
	wputblock(0,0,dummy,0);
	wfreeblock(dummy);
	wtextcolor(1);
	wsetcolor(0);
	wbar(135,193,235,199);
	wouttextxy(140,194,tiny,wfilestoprint[fln]);
     if(dotmatrix==1){
	printimage();
	delay(300);
	fprintf(stdprn,"\f");
	delay(2500);
	}/*end if dot matrix*/
     if(laser==1){
	laser_print(319,199);
	}

    } /*end of for loop list of files*/
 } /*end if print all==1*/

  if(prindiv==1){
   wcls(0);
      setdisk (singlefile);
      chdir (singlepath);
   dummy=wloadpcx256(filesname,scpalette);
	wsetpalette(0,255,scpalette);
	wputblock(0,0,dummy,0);
	wfreeblock(dummy);
	wsetcolor(0);
	wbar(135,193,235,199);
	wtextcolor(1);
	wouttextxy(140,194,tiny,filesname);

    if(dotmatrix==1){
	printimage();
	fprintf(stdprn,"\f");
	}
     if(laser==1){
	laser_print(319,199);
	}

  }/*end if prindiv==1*/
 } /*end if pr==1 quit*/

 if((pr==1)&&(dotmatrix==1)) fprintf(stdprn,"\x1B\x40");

 wfreeblock(virtscreen);
 wnormscreen();
 wcls(0);

 mdeinit();
      setdisk (origdisk);
      chdir (origpath);
	wfreefont(tiny);
	wfreefont(little);

	execv("utility",NULL);

}   /*end of Main*/


