home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the DOOM Programming Gurus / Tricks_of_the_Doom_Programming_Gurus.iso / bonus / utils / doom_ps / doom_ps.c next >
Encoding:
C/C++ Source or Header  |  1994-05-12  |  25.6 KB  |  743 lines

  1. /*
  2.      Doom PostScript map generator. V1.2
  3.      Copyright James Bonfield, 3/3/94
  4.  
  5.      Modified for PC by Gerhard Karnik (g.karnik@att.com) 3/29/94
  6.      Enhancements (V1.2) by Gerhard Karnik (g.karnik@att.com) 5/12/94
  7.  
  8. Description
  9. -----------
  10.  
  11. This program produces PostScript maps of Doom levels, extracted from the Wad
  12. file. The program is written for the UNIX operating system, and so you need a
  13. method of transfering your doom.wad file, (or perhaps having it visible by,
  14. say, mounting your DOS partition).
  15.  
  16. Thick lines represent solid walls on the map.
  17. Circles represent objects.
  18. Arrows represent the enemy. The direction of the arrow is the direction the
  19. creature is facing.
  20. Gray lines represent secret passages.
  21.  
  22. USAGE: doom_ps [-o] [-m] [-dm] [wadfile] [level]
  23.  
  24. -o              Do not display objects
  25. -m              Do not display monsters
  26. -dm             Display deathmatch items
  27. wadfile         Selects another wad file (default is "DOOM.WAD")
  28. level           Level number (default is all levels), can use "E1M1" or "11"
  29.  
  30. The postscript will then be displayed to stdout.
  31.  
  32. Compiled using Borland C++ 4.0 using 'bcc -mh'
  33.  
  34. */
  35.  
  36. #include <io.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <errno.h>
  40. #include <string.h>
  41. #include <share.h>
  42. #include <fcntl.h>
  43. #include <dos.h>
  44. #include <dir.h>
  45. #include <alloc.h>
  46.  
  47. unsigned nread;         /* throw away variable for _dos_read() */
  48. char wadfile[80], level[80];
  49.  
  50. typedef long int4;
  51. typedef short int2;
  52.  
  53. struct wad_header {
  54.         int4 magic;
  55.         int4 dir_size;
  56.         int4 dir_off;
  57. };
  58.  
  59. struct directory {
  60.         int4 start;
  61.         int4 length;
  62.         char name[8];
  63. };
  64.  
  65. typedef struct linedef_ {
  66.         int2 from_vertex;
  67.         int2 to_vertex;
  68.         int2 attrib;
  69.         int2 type;
  70.         int2 trigger;
  71.         int2 sidedef1;
  72.         int2 sidedef2;
  73. } linedef;
  74.  
  75. /* linedef attrib bits */
  76. #define LI_IMPASS 0x01
  77. #define LI_SECRET 0x20
  78.  
  79. typedef struct vertex_ {
  80.         int2 x;
  81.         int2 y;
  82. } vertex;
  83.  
  84. typedef struct sidedef_ {
  85.         int2 x;
  86.         int2 y;
  87.         char wall_above[8];
  88.         char wall_below[8];
  89.         char wall_part[8];
  90.         int2 sector;
  91. } sidedef;
  92.  
  93. typedef struct ssector_ {
  94.         int2 num;
  95.         int2 seg;
  96. } ssector;
  97.  
  98. typedef struct sector_ {
  99.         int2 bot_pos;
  100.         int2 top_pos;
  101.         char bot_texture[8];
  102.         char top_texture[8];
  103.         int2 brightness;
  104.         int2 special;
  105.         int2 trigger;
  106. } sector;
  107.  
  108. typedef struct thing_ {
  109.         int2 x;
  110.         int2 y;
  111.         int2 angle;
  112.         int2 type;
  113.         int2 attrib;
  114. } thing;
  115.  
  116. /* thing 'type' bits */
  117. #define TH_SKILL1  0x01
  118. #define TH_SKILL2  0x01
  119. #define TH_SKILL3  0x02
  120. #define TH_SKILL4  0x04
  121. #define TH_MULTI   0x10
  122.  
  123. typedef int2 blockmap;
  124.  
  125. void _ExceptInit(void) {};  /* reduces size of borland C++ 4.0 executable */
  126.  
  127. struct directory *open_wad(char *file, int *fd, long *size);
  128. long get_index(struct directory *d, long size, char *name, long st);
  129. linedef *read_linedefs(int fd, struct directory *d, long size, long start, long *num);
  130. vertex *read_vertexes(int fd, struct directory *d, long size, long start, long *num);
  131. blockmap *read_blockmap(int fd, struct directory *d, long size, long start, long *num);
  132. sidedef *read_sidedefs(int fd, struct directory *d, long size, long start, long *num);
  133. sector *read_sectors(int fd, struct directory *d, long size, long start, long *num);
  134. thing *read_things(int fd, struct directory *d, long size, long start, long *num);
  135. void usage(void);
  136. int doit(int fd, struct directory *dir, long size, int objs, int mons, int deathmatch);
  137.  
  138. /*
  139.  * Paper is 8 1/2" by 11"
  140.  * We allow 1/2" border.
  141.  */
  142. #define PAPER_X (72 * 8.5)
  143. #define PAPER_Y (72 * 11)
  144. #define PAPER_BORDER (72 * 0.5)
  145.  
  146.  
  147. void usage()
  148. {
  149.         fprintf(stderr, "Doom PostScript map generator V1.2\n");
  150.         fprintf(stderr, "Produces PostScript maps of Doom levels extracted from a WAD file.\n");
  151.         fprintf(stderr, "\n");
  152.         fprintf(stderr, "USAGE: doom_ps [-o] [-m] [-dm] [wadfile] [level]\n");
  153.         fprintf(stderr, "  -o              Do not display objects\n");
  154.         fprintf(stderr, "  -m              Do not display monsters\n");
  155.         fprintf(stderr, "  -dm             Display deathmatch items\n");
  156.         fprintf(stderr, "  -? or -h        This help screen\n");
  157.         fprintf(stderr, "  wadfile         Selects another wad file \(default is \"DOOM.WAD\"\)\n");
  158.         fprintf(stderr, "  level           Level number \(default is all levels\), can use \"E1M1\" or \"11\"\n");
  159.         fprintf(stderr, "\n");
  160.         fprintf(stderr, "Map legend:\n");
  161.         fprintf(stderr, "  Thick line     = Solid walls on the map\n");
  162.         fprintf(stderr, "  Gray line      = Secret passages\n");
  163.         fprintf(stderr, "  Shaded circle  = Invisibility, radiation suit, computer map, or lite goggles\n");
  164.         fprintf(stderr, "  Arrow          = Monster facing the direction of the arrow\n");
  165.         fprintf(stderr, "  3D box         = Box of ammo\n");
  166.         fprintf(stderr, "  Smiley face    = Invulnerability or Soulsphere\n");
  167.         fprintf(stderr, "  Cross          = Stimpak, medikit, health bonus, or berserk strength\n");
  168.         fprintf(stderr, "  Shirt          = Green armor 100%% or blue armor 200%%\n");
  169.         exit(-1);
  170. }
  171.  
  172.  
  173. int main(int argc, char **argv)
  174. {
  175.         struct directory *d;
  176.         struct ffblk ffblk;
  177.         int fd, i, ep, lv;
  178.         long size;
  179.         int objs = 1, mons = 1, deathmatch=0;
  180.         int done;
  181.         char path[80];
  182.  
  183.         for(i=1; i<=argc; i++) {
  184.                 if(argv[i][0] == '/')        argv[i][0] = '-';
  185.                 if(!strcmpi(argv[i], "-DM")) deathmatch = 1;
  186.                 if(!strcmpi(argv[i], "-O"))  objs = 0;
  187.                 if(!strcmpi(argv[i], "-M"))  mons = 0;
  188.                 if(!strcmpi(argv[i], "-?"))  usage();
  189.                 if(!strcmpi(argv[i], "-H"))  usage();
  190.                 if(argv[i][0] != '-')        break; /* no more params */
  191.         }
  192.  
  193.         switch( argc-i ) {
  194.         case 0: /* no more params */
  195.                 strcpy(path, "DOOM.WAD");
  196.                 strcpy(level, "");
  197.                 break;
  198.  
  199.         case 1: /* only wad name */
  200.                 strcpy(path, argv[i]);
  201.                 strcpy(level, "");
  202.                 break;
  203.  
  204.         case 2: /* wad name + level */
  205.                 strcpy(path, argv[i]);
  206.                 strcpy(level, argv[i+1]);
  207.  
  208.                 if(strlen(level) == 2) {        /* convert '11' to 'E1M1' */
  209.                         level[4] = 0;
  210.                         level[3] = level[1];
  211.                         level[2] = 'M';
  212.                         level[1] = level[0];
  213.                         level[0] = 'E';
  214.                 }
  215.                 break;
  216.  
  217.         default:
  218.                 usage();
  219.         }
  220.  
  221.         if(strlen(path)>12)
  222.                 fprintf(stderr, "WARNING: Can only get WADs in current directory.\n");
  223.  
  224.         done = findfirst(path, &ffblk, 0);
  225.         if(done) {
  226.                 fprintf(stderr, "Failed to open wad file \"%s\".\n", path);
  227.                 return -1;
  228.         }
  229.  
  230.         while(!done) {
  231.                 strcpy(wadfile, ffblk.ff_name);
  232.  
  233.                 if((d = open_wad(wadfile, &fd, &size)) != NULL) {
  234.  
  235.                         if(!level[0]) { /* do all levels */
  236.                             for(ep=1; ep<=3; ep++)
  237.                                 for(lv=1; lv<=9; lv++) {
  238.                                     sprintf(level, "E%dM%d", ep, lv);
  239.                                     doit(fd, d, size, objs, mons, deathmatch);
  240.                                 }
  241.                             strcpy(level, "");
  242.                         }
  243.                         else
  244.                             if(doit(fd, d, size, objs, mons, deathmatch) == -1)
  245.                                 fprintf(stderr, "Unknown level: %s\n", level);
  246.  
  247.                         _dos_close(fd);
  248.                 }
  249.  
  250.                 done = findnext(&ffblk);
  251.         }
  252.  
  253.         return 0;
  254. }
  255.  
  256.  
  257. int doit(int fd, struct directory *dir, long size, int objs, int mons, int deathmatch)
  258. {
  259.         linedef *linedefs;
  260.         vertex *vertexes;
  261.         blockmap *blocks;
  262.         sidedef *sidedefs;
  263.         sector *sectors;
  264.         thing *things;
  265.         long numline, numvert, numblock, numside, numsect, numthing;
  266.         long lev_index;
  267.         long xorigin, yorigin, xsize, ysize;
  268.         int2 sector;
  269.         double xscale, yscale;
  270.         long i, j;
  271.  
  272.         /* find level index */
  273.         lev_index = get_index(dir, size, level, 0);
  274.         if(lev_index == -1) return(-1);  /* level not found */
  275.  
  276.         /* load relevent arrays for this level */
  277.  
  278.         linedefs = read_linedefs(fd, dir, size, lev_index, &numline);
  279.         vertexes = read_vertexes(fd, dir, size, lev_index, &numvert);
  280.         blocks   = read_blockmap(fd, dir, size, lev_index, &numblock);
  281.         sidedefs = read_sidedefs(fd, dir, size, lev_index, &numside);
  282.         sectors  = read_sectors(fd, dir, size, lev_index, &numsect);
  283.         things   = read_things(fd, dir, size, lev_index, &numthing);
  284.  
  285.         /* calculate scaling info */
  286.         xorigin = blocks[0];
  287.         yorigin = blocks[1];
  288.         xsize   = blocks[2] * 0x80;
  289.         ysize   = blocks[3] * 0x80;
  290.         xscale  = (double) (PAPER_Y - 2 * PAPER_BORDER) / xsize;
  291.         yscale  = (double) (PAPER_X - 2 * PAPER_BORDER) / ysize;
  292.  
  293.         if(xscale > yscale)
  294.                 xscale = yscale;
  295.         else
  296.                 yscale = xscale;
  297.  
  298.         /* output postscript header */
  299.         printf("%%!\n");
  300.         printf("newpath\n");
  301.         printf("\n");
  302.         printf("1 setlinecap\n");
  303.         printf("%f %f translate\n", PAPER_BORDER, PAPER_Y - PAPER_BORDER);
  304.  
  305.         printf("/Times-Roman findfont 14 scalefont setfont\n");
  306.         printf("0 setgray 20 0 moveto (%s - %s) show\n", wadfile, level);
  307.  
  308.         printf("-90 rotate\n");
  309.         printf("%f %f scale\n", xscale, yscale);
  310.         printf("%ld %ld translate\n", -xorigin, -yorigin);
  311.         printf("%f setlinewidth\n", (double) .5 / xscale);
  312.         printf("\n");
  313.         printf("/Times-Roman findfont %f scalefont setfont\n", 5 / xscale);
  314.         printf("/Text {\n");
  315.         printf("    gsave\n");
  316.         printf("    0 setgray moveto show\n");
  317.         printf("    grestore\n");
  318.         printf("} def\n");
  319.         printf("\n");
  320.         printf("/l {\n");
  321.         printf("    setgray setlinewidth moveto lineto stroke\n");
  322.         printf("} def\n");
  323.         printf("\n");
  324.         printf("/a {\n");
  325.         printf("    0.5 setgray\n");
  326.         printf("    5 0 360 arc stroke\n");
  327.         printf("} def\n");
  328.         printf("\n");
  329.         printf("/m {\n");
  330.         printf("    gsave\n");
  331.         printf("    0 setgray\n");
  332.         printf("    1 setlinewidth\n");
  333.         printf("    moveto rotate\n");
  334.         printf("    0 -13 rmoveto\n");
  335.         printf("    0 25 rlineto\n");
  336.         printf("    -10 -10 rlineto\n");
  337.         printf("    10 10 rmoveto\n");
  338.         printf("    10 -10 rlineto\n");
  339.         printf("    stroke\n");
  340.         printf("    grestore\n");
  341.         printf("} def\n");
  342.         printf("\n");
  343.         printf("/ammo {\n");
  344.         printf("    gsave\n");
  345.         printf("    0 setgray\n");
  346.         printf("    1 setlinewidth\n");
  347.         printf("    moveto\n");
  348.         printf("    18 0 rlineto\n");
  349.         printf("    0 -18 rlineto\n");
  350.         printf("    -18 0 rlineto\n");
  351.         printf("    0 18 rlineto\n");
  352.         printf("    9 9 rlineto\n");
  353.         printf("    18 0 rlineto\n");
  354.         printf("    -9 -9 rlineto\n");
  355.         printf("    9 9 rmoveto\n");
  356.         printf("    0 -18 rlineto\n");
  357.         printf("    -8 -9 rlineto\n");
  358.         printf("    stroke\n");
  359.         printf("    grestore\n");
  360.         printf("} def\n");
  361.         printf("\n");
  362.         printf("/armor {\n");
  363.         printf("    gsave\n");
  364.         printf("    0 setgray\n");
  365.         printf("    1 setlinewidth\n");
  366.         printf("    translate\n");
  367.         printf("    1.5 1.5 scale\n");
  368.         printf("    0 -5 moveto\n");
  369.         printf("    newpath\n");
  370.         printf("    9 10 8 30 150 arc\n");
  371.         printf("    -9 10 8 30 150 arc\n");
  372.         printf("    -10 9 4 150 20 arcn\n");
  373.         printf("    0 -12 rlineto\n");
  374.         printf("    12 0 rlineto\n");
  375.         printf("    0 12 rlineto\n");
  376.         printf("    10 9 4 140 30 arcn\n");
  377.         printf("    closepath\n");
  378.         printf("    stroke\n");
  379.         printf("    grestore\n");
  380.         printf("} def\n");
  381.         printf("\n");
  382.         printf("/health {\n");
  383.         printf("    gsave\n");
  384.         printf("    0 setgray\n");
  385.         printf("    2 setlinewidth\n");
  386.         printf("    moveto\n");
  387.         printf("    8 0 rlineto\n");
  388.         printf("    -16 0 rlineto\n");
  389.         printf("    8 0 rmoveto\n");
  390.         printf("    0 8 rlineto\n");
  391.         printf("    0 -16 rlineto\n");
  392.         printf("    stroke\n");
  393.         printf("    grestore\n");
  394.         printf("} def\n");
  395.         printf("\n");
  396.         printf("/smiley {\n");
  397.         printf("    gsave\n");
  398.         printf("    0 setgray\n");
  399.         printf("    1 setlinewidth\n");
  400.         printf("    translate\n");
  401.         printf("    newpath\n");
  402.         printf("    0 0 15 90 89 arc\n");
  403.         printf("    closepath\n");
  404.         printf("    -6 -9 rmoveto\n");
  405.         printf("    3 0 rlineto\n");
  406.         printf("    6 0 rmoveto\n");
  407.         printf("    3 0 rlineto\n");
  408.         printf("    -7 -6 rmoveto\n");
  409.         printf("    2 0 rlineto\n");
  410.         printf("    -5 -8 rmoveto\n");
  411.         printf("    8 0 rlineto\n");
  412.         printf("    stroke\n");
  413.         printf("    grestore\n");
  414.         printf("} def\n");
  415.         printf("\n");
  416.  
  417.         /* Display the walls. Secret passages are done in grey. */
  418.         for(i=0; i<numline; i++) {
  419.                 sector = sidedefs[linedefs[i].sidedef1].sector;
  420.  
  421.                 if(linedefs[i].type == 11 ||   /* level exit */
  422.                    linedefs[i].type == 51 ||
  423.                    linedefs[i].type == 52)
  424.                         printf("[1 15] 1 setdash\n");
  425.  
  426.                 printf("%d %d %d %d ",
  427.                        vertexes[linedefs[i].to_vertex].x,
  428.                        vertexes[linedefs[i].to_vertex].y,
  429.                        vertexes[linedefs[i].from_vertex].x,
  430.                        vertexes[linedefs[i].from_vertex].y);
  431.  
  432.                 if (linedefs[i].attrib & LI_IMPASS)
  433.                         printf("%f ", 1.2 / xscale);
  434.                 else
  435.                         printf("%f ", 0.5 / xscale);
  436.  
  437.                 if (linedefs[i].attrib & LI_SECRET || sectors[sector].special == 9)
  438.                         printf(".5 l\n");
  439.                 else
  440.                         printf("0 l\n");
  441.  
  442.                 if(linedefs[i].type == 11 ||   /* level exit */
  443.                    linedefs[i].type == 51 ||
  444.                    linedefs[i].type == 52)
  445.                         printf("[] 0 setdash\n");
  446.         }
  447.  
  448.         /* Do we need to display objects or monsters? */
  449.         if(objs || mons) {
  450.                 printf("    %f setlinewidth\n", 1 / xscale);
  451.  
  452.                 for(i=0; i<numthing; i++) {
  453.  
  454.                         /* filter out skill level 4 & 5 stuff */
  455.                         if(!(things[i].attrib & (TH_SKILL2 + TH_SKILL3)))
  456.                                 continue;
  457.  
  458.                         /* filter out deathmatch items */
  459.                         if((things[i].attrib & TH_MULTI) && !deathmatch)
  460.                                 continue;
  461.  
  462.                         switch(things[i].type) {
  463.  
  464.                         /* monsters */
  465.                         case 7:        /* spider    */
  466.                         case 9:        /* sergeant  */
  467.                         case 16:       /* cyber     */
  468.                         case 58:       /* invis     */
  469.                         case 3001:     /* imp       */
  470.                         case 3002:     /* demon     */
  471.                         case 3003:     /* minotaur  */
  472.                         case 3004:     /* human     */
  473.                         case 3005:     /* beholder  */
  474.                         case 3006:     /* skull     */
  475.                                 if(mons) {
  476.                                         printf("%d %d %d m\n",
  477.                                                things[i].angle, things[i].x, things[i].y);
  478.                                 }
  479.                                 break;
  480.  
  481.                         /* weapons */
  482.                         case 2001:     /* Shotgun           */
  483.                                 printf("(ShotG) %d %d Text\n", things[i].x - 50, things[i].y);
  484.                                 break;
  485.  
  486.                         case 2002:     /* Chaingun          */
  487.                                 printf("(ChainG) %d %d Text\n", things[i].x - 50, things[i].y);
  488.                                 break;
  489.  
  490.                         case 2003:     /* Rocket launcher   */
  491.                                 printf("(Rocket) %d %d Text\n", things[i].x - 50, things[i].y);
  492.                                 break;
  493.  
  494.                         case 2004:     /* Plasma gun        */
  495.                                 printf("(Plasma) %d %d Text\n", things[i].x - 50, things[i].y);
  496.                                 break;
  497.  
  498.                         case 2005:     /* Chainsaw          */
  499.                                 printf("(ChainS) %d %d Text\n", things[i].x - 50, things[i].y);
  500.                                 break;
  501.  
  502.                         case 2006:     /* BFG9000           */
  503.                                 printf("(BFG) %d %d Text\n", things[i].x - 30, things[i].y);
  504.                                 break;
  505.  
  506.                         /* Good Ammo */
  507.                         case 8:        /* Backpack          */
  508.                         case 17:       /* Cell charge pack  */
  509.                         case 2010:     /* 1 rocket          */
  510.                         case 2046:     /* Box of Rockets    */
  511.                         case 2047:     /* Cell charge       */
  512.                         case 2048:     /* Box of Ammo       */
  513.                         case 2049:     /* Box of Shells     */
  514.                                 if(objs) {
  515.                                         printf("%d %d ammo\n", things[i].x, things[i].y);
  516.                                 }
  517.                                 break;
  518.  
  519.                         case 2022:     /* Invulnerability   */
  520.                         case 2013:     /* Soulsphere, Supercharge, +100% health */
  521.                                 if(objs) {
  522.                                         printf("%d %d smiley\n", things[i].x, things[i].y);
  523.                                 }
  524.                                 break;
  525.  
  526.                         /* Health */
  527.                         case 2011:     /* Stimpak           */
  528.                         case 2012:     /* Medikit           */
  529.                         case 2023:     /* Berserk Strength  */
  530.                                 if(objs) {
  531.                                         printf("%d %d health\n", things[i].x, things[i].y);
  532.                                 }
  533.                                 break;
  534.  
  535.                         /* Armour */
  536.                         case 2018:     /* Green armor 100%  */
  537.                         case 2019:     /* Blue armor 200%   */
  538.                                 if(objs) {
  539.                                         printf("%d %d armor\n", things[i].x, things[i].y);
  540.                                 }
  541.                                 break;
  542.  
  543.                         /* other good stuff */
  544.                         case 2024:     /* Invisibility      */
  545.                         case 2025:     /* Radiation suit    */
  546.                         case 2026:     /* Computer map      */
  547.                         case 2045:     /* Lite goggles      */
  548.                                 if(objs) {
  549.                                         printf("%d %d a\n", things[i].x, things[i].y);
  550.                                 }
  551.                                 break;
  552.  
  553.                         /* Another type of object */
  554.                         case 2007:     /* Ammo clip         */
  555.                         case 2008:     /* 4 shotgun shells  */
  556.                         case 2014:     /* Health bonus      */
  557.                         case 2015:     /* Armor bonus       */
  558.  
  559.                         case 5:        /* Blue keycard      */
  560.                         case 6:        /* Yellow keycard    */
  561.                         case 13:       /* Red Keycard       */
  562.                         case 38:       /* Red skullkey      */
  563.                         case 39:       /* Yellow skullkey   */
  564.                         case 40:       /* Blue skullkey     */
  565.                         default:
  566.                                 break;
  567.                         }
  568.                 }
  569.         }
  570.         puts("showpage");
  571.  
  572.         farfree(linedefs);
  573.         farfree(vertexes);
  574.         farfree(blocks);
  575.         farfree(sidedefs);
  576.         farfree(sectors);
  577.         farfree(things);
  578.  
  579.         return(0);
  580. }
  581.  
  582.  
  583. struct directory *open_wad(char *file, int *fd, long *size)
  584. {
  585.         struct wad_header h;
  586.         struct directory *d;
  587.         char *s;
  588.  
  589.         if(_dos_open(file, O_RDONLY, fd) != 0) {
  590.                 perror(file);
  591.                 _dos_close(*fd);
  592.                 return (struct directory *)0;
  593.         }
  594.  
  595.         _dos_read(*fd, &h, sizeof(h), &nread);
  596.  
  597.         s = (char *)&h.magic;
  598.  
  599.         if(s[1] != 'W' || s[2] != 'A' || s[3] != 'D') {
  600.                 fprintf(stderr, "Not a WAD file: \"%s\"\n", file);
  601.                 _dos_close(*fd);
  602.                 return (struct directory *) 0;
  603.         }
  604.  
  605.         if(NULL == (d = (struct directory *)farcalloc(h.dir_size, sizeof(*d)))) {
  606.                 fprintf(stderr, "Could not allocated %ld bytes.\n",
  607.                         h.dir_size * sizeof(*d));
  608.                 _dos_close(*fd);
  609.                 return (struct directory *) 0;
  610.         }
  611.         *size = h.dir_size;
  612.  
  613.         lseek(*fd, h.dir_off, SEEK_SET);
  614.         _dos_read(*fd, d, *size * sizeof(*d), &nread);
  615.  
  616.         return d;
  617. }
  618.  
  619. long get_index(struct directory *d, long size, char *name, long start)
  620. {
  621.         long i;
  622.  
  623.         for(i=start; i<size; i++)
  624.                 if(_fstrncmp(d[i].name, name, 8) == 0) return i;
  625.  
  626.         return -1;
  627. }
  628.  
  629. linedef *read_linedefs(int fd, struct directory *d, long size, long start, long *num)
  630. {
  631.         long index;
  632.         linedef *indexp;
  633.         long i;
  634.  
  635.         index = get_index(d, size, "LINEDEFS", start);
  636.         indexp = (linedef *) farmalloc(d[index].length);
  637.         if(!indexp) {
  638.                 printf("Could not allocate %ld bytes\n",d[index].length);
  639.                 exit(-1);
  640.         }
  641.         lseek(fd, d[index].start, SEEK_SET);
  642.         _dos_read(fd, indexp, d[index].length, &nread);
  643.         *num = d[index].length / sizeof(linedef);
  644.  
  645.         return indexp;
  646. }
  647.  
  648. vertex *read_vertexes(int fd, struct directory *d, long size, long start, long *num)
  649. {
  650.         long index;
  651.         vertex *indexp;
  652.         long i;
  653.  
  654.         index = get_index(d, size, "VERTEXES", start);
  655.         indexp = (vertex *) farmalloc(d[index].length);
  656.         if(!indexp) {
  657.                 printf("Could not allocate %ld bytes\n",d[index].length);
  658.                 exit(-1);
  659.         }
  660.         lseek(fd, d[index].start, SEEK_SET);
  661.         _dos_read(fd, indexp, d[index].length, &nread);
  662.         *num = d[index].length / sizeof(vertex);
  663.  
  664.         return indexp;
  665. }
  666.  
  667. blockmap *read_blockmap(int fd, struct directory *d, long size, long start, long *num)
  668. {
  669.         long index;
  670.         blockmap *indexp;
  671.         long i;
  672.  
  673.         index = get_index(d, size, "BLOCKMAP", start);
  674.         indexp = (blockmap *) farmalloc(d[index].length);
  675.         if(!indexp) {
  676.                 printf("Could not allocate %ld bytes\n",d[index].length);
  677.                 exit(-1);
  678.         }
  679.         lseek(fd, d[index].start, SEEK_SET);
  680.         _dos_read(fd, indexp, d[index].length, &nread);
  681.         *num = d[index].length / sizeof(blockmap);
  682.  
  683.         return indexp;
  684. }
  685.  
  686. sidedef *read_sidedefs(int fd, struct directory *d, long size, long start, long *num)
  687. {
  688.         long index;
  689.         sidedef *indexp;
  690.         long i;
  691.  
  692.         index = get_index(d, size, "SIDEDEFS", start);
  693.         indexp = (sidedef *) farmalloc(d[index].length);
  694.         if(!indexp) {
  695.                 printf("Could not allocate %ld bytes\n",d[index].length);
  696.                 exit(-1);
  697.         }
  698.         lseek(fd, d[index].start, SEEK_SET);
  699.         _dos_read(fd, indexp, d[index].length, &nread);
  700.         *num = d[index].length / sizeof(sidedef);
  701.  
  702.         return indexp;
  703. }
  704.  
  705. sector *read_sectors(int fd, struct directory *d, long size, long start, long *num)
  706. {
  707.         long index;
  708.         sector *indexp;
  709.         long i;
  710.  
  711.         index = get_index(d, size, "SECTORS", start);
  712.         indexp = (sector *) farmalloc(d[index].length);
  713.         if(!indexp) {
  714.                 printf("Could not allocate %ld bytes\n",d[index].length);
  715.                 exit(-1);
  716.         }
  717.         lseek(fd, d[index].start, SEEK_SET);
  718.         _dos_read(fd, indexp, d[index].length, &nread);
  719.         *num = d[index].length / sizeof(sector);
  720.  
  721.         return indexp;
  722. }
  723.  
  724. thing *read_things(int fd, struct directory *d, long size, long start, long *num)
  725. {
  726.         long index;
  727.         thing *indexp;
  728.         long i;
  729.  
  730.         index = get_index(d, size, "THINGS", start);
  731.         indexp = (thing *) farmalloc(d[index].length);
  732.         if(!indexp) {
  733.                 printf("Could not allocate %ld bytes\n",d[index].length);
  734.                 exit(-1);
  735.         }
  736.         lseek(fd, d[index].start, SEEK_SET);
  737.         _dos_read(fd, indexp, d[index].length, &nread);
  738.         *num = d[index].length / sizeof(thing);
  739.  
  740.         return indexp;
  741. }
  742.  
  743.