
# makeps - make Postscript-files of the archetypes listed in text file whose
# filename is passed in 'input'
# Variables passed in:
#   archdir - root of crossfire-src, with a trailing slash
#   libdir  - where archetypes etc. is found

BEGIN {
    # Set colour to 1 if you want colour postscript.
    colour = 1;
    # IF you have giftrans installed and want transparent gifs, set
    # appropriately.  IT looks much nicer if you can do it.
    giftrans = 1;

    if (colour) {
      xpm2ps = "xpmtoppm | pnmdepth 255 | ppmtogif"
      ppm2ps = "pnmdepth 255 | ppmtogif"
    } else {
      xpm2ps = "xpmtoppm | pnmdepth 255 | ppmtopgm | pnmtops"
      ppm2ps = "pnmdepth 255 | ppmtopgm | pnmtops"
    }

    bmaps = libdir "/bmaps";
    bmappaths = libdir "/bmaps.paths";

    while ((getline < bmappaths) == 1)
	if ($1 != "#")
	    bmappath[$1] = $2;
    close(bmappaths)

    while ((getline < bmaps) == 1)
        if (("\\" $1) in bmappath)
            bmap[$2] = bmappath["\\" $1];
    close(bmaps);


    # An array listing which archetypes files need fixing, the value
    # is the file where it is used. There must be at least one character
    # between the ~~spec~~'s.

    oldFS=FS; FS="~~"; oldOFS=OFS; OFS="";
    while ((getline < input) == 1) {
	if (NF > 1) {
            work_todo = 1;
	    for (i = 2; i < NF; i += 2)
		if ($i != "")  
			makeps[$i] = $1;
	}
    }
    close(input);
    FS=oldFS; OFS=oldOFS;

    if (!work_todo)
	exit;

    # An array to reduce the size of the bitmap exponentially.
    # A 4x8 bitmap will be reduced to 60% of its full size.
    size_mul[1] = 1;
    for (i = 2; i <= 12; i++)	    # Max input is 12x12, a *large* bitmap ;-)
        size_mul[i] = size_mul[i-1] * 0.9;

}

$1 == "Object"	{
    if (interesting) {
        faces[x,y] = face;
        if (!More && makeps[obj] != 1)
            makeps[obj] = assemble();
    }

    # Get ready for next archetype
    if (!More) {
        xmin = xmax = ymin = ymax = 0;
	obj = $2;
	interesting = obj in makeps;
    }
    x = y = 0;
    More = 0;
}

$1 == "face"    { face = $2 }
$1 == "x"       { x = $2; if (x > xmax) xmax = x; else if (x < xmin) xmin = x; }
$1 == "y"       { y = $2; if (y > ymax) ymax = y; else if (y < ymin) ymin = y; }
$1 == "More"    { More = 1; }
$1 == "msg"	{ do { getline } while ($1 != "endmsg"); }

END {
    # Remember to check the last archetype also...
    if (interesting) {
        faces[x,y] = face;
        if (makeps[obj] != 1)
            makeps[obj] = assemble();
    }

    system("rm -f work.pbm tmp.pbm");	# clean up a little

    # We've created a number of Postscript-files - now we need to
    # patch the filenames and sizes into the TeX-files.

    FS="~~"; OFS="";
    while ((getline < input) == 1) {
	    if (NF > 1) {
		for (i = 2; i <= NF; i += 2)
		    if ($i in makeps)
			$i = makeps[$i];
	    }
	    print;
    }
    close(input);
}

function assemble(   w, h, ppm, buff, i, j, bmap_file, ps_file) {
    bmap_file = archdir bmap[faces[0,0]] ".xpm";
    ps_file =  faces[0,0] ".gif";
    gsub(/[_ ]/, "-", ps_file);

    w = xmax - xmin + 1; h = ymax - ymin + 1;
    if ((getline buff < ps_file) != -1)
        close (ps_file);
    else {
        if (w == 1 && h == 1) {
	    if (giftrans) {
              system(sprintf("sed 's/[Nn]one/#fafbfc/g' < %s | " xpm2ps " > tmp.gif", bmap_file));
	      system(sprintf("giftrans -t \\#fafbfc tmp.gif > %s", ps_file));
	    } else {
              system(sprintf("sed 's/[Nn]one/white/g' < %s | " xpm2ps " > %s",
    	                   bmap_file, ps_file));
	    }
        } else {
            ppm = sprintf("%dx%d.ppm", w, h);
		print ppm >>"/dev/stderr";
            if ((getline buff < ppm) != -1)
                close(ppm);
            else {
		printf("pnmscale -xsc %d -ysc %d < empty.pbm | pgmtoppm white > %s\n", w, h, ppm) >>"/dev/stderr";
                system(sprintf("pnmscale -xsc %d -ysc %d < empty.pbm | pgmtoppm white > %s",
                        w, h, ppm));

	    }

            system("cp " ppm " work.ppm"); ppm = "work.ppm";

            for (i = xmin; i <= xmax; i++)
                for (j = ymin; j <= ymax; j++) {
		    print "Processing " bmap[faces[i,j]] >> "/dev/stderr";
		    if (giftrans) {
	                    system(sprintf("sed 's/[Nn]one/#fafbfc/g' < %s | xpmtoppm > tmp.ppm; pnmpaste tmp.ppm %d %d %s > tmp2.ppm; mv tmp2.ppm %s",
                                   archdir bmap[faces[i,j]] ".xpm",
                                   (i - xmin) * 24, (j - ymin) * 24, ppm, ppm));
		    } else {
	                    system(sprintf("sed 's/[Nn]one/white/g' < %s | xpmtoppm > tmp.ppm; pnmpaste tmp.ppm %d %d %s > tmp2.ppm; mv tmp2.ppm %s",
                                   archdir bmap[faces[i,j]] ".xpm",
                                   (i - xmin) * 24, (j - ymin) * 24, ppm, ppm));
		    }
		}
	    if (giftrans) {
                system(sprintf("cat %s | " ppm2ps " > tmp.gif", ppm));
	        system(sprintf("giftrans -t \\#fafbfc tmp.gif > %s", ps_file));
	    } else {
                system(sprintf("cat %s | " ppm2ps " > %s", ppm, ps_file));
	    }
        }
    }
    mul = size_mul[int(sqrt(w * h))] * size;
    ps = sprintf("<img src=" ps_file ">");
    return ps;
}
