home *** CD-ROM | disk | FTP | other *** search
/ Doom Extras / DExtras.iso / accessor / dos / deu521 / source / levels.c < prev    next >
C/C++ Source or Header  |  1994-05-17  |  30KB  |  1,053 lines

  1. /*
  2.    Doom Editor Utility, by Brendon Wyber and Raphaël Quinet.
  3.  
  4.    You are allowed to use any parts of this code in another program, as
  5.    long as you give credits to the authors in the documentation and in
  6.    the program itself.  Read the file README.1ST for more information.
  7.  
  8.    This program comes with absolutely no warranty.
  9.  
  10.    LEVELS.C - Level loading and saving routines.
  11. */
  12.  
  13. /* the includes */
  14. #include "deu.h"
  15. #include "wstructs.h"
  16. #include "things.h"
  17.  
  18. /* external function from objects.c */
  19. extern Bool CreateNodes( NPtr *, int *, SEPtr); /* SWAP - needs Vertexes */
  20.  
  21. /* the global data */
  22. MDirPtr Level = NULL;        /* master dictionary entry for the level */
  23. int NumThings = 0;        /* number of things */
  24. TPtr Things;            /* things data */
  25. int NumLineDefs = 0;        /* number of line defs */
  26. LDPtr LineDefs;            /* line defs data */
  27. int NumSideDefs = 0;        /* number of side defs */
  28. SDPtr SideDefs;            /* side defs data */
  29. int NumVertexes = 0;        /* number of vertexes */
  30. VPtr Vertexes;            /* vertex data */
  31. int NumSectors = 0;        /* number of sectors */
  32. SPtr Sectors;            /* sectors data */
  33. int NumSegs = 0;        /* number of segments */
  34. SEPtr Segs = NULL;        /* list of segments */
  35. SEPtr LastSeg = NULL;        /* last segment in the list */
  36. int NumSSectors = 0;        /* number of subsectors */
  37. SSPtr SSectors = NULL;        /* list of subsectors */
  38. SSPtr LastSSector = NULL;    /* last subsector in the list */
  39. int NumNodes = 0;        /* number of Nodes */
  40. NPtr Nodes = NULL;        /* nodes tree */
  41. int NumWTexture = 0;        /* number of wall textures */
  42. char **WTexture;        /* array of wall texture names */
  43. int NumFTexture = 0;        /* number of floor/ceiling textures */
  44. char **FTexture;        /* array of texture names */
  45. int MapMaxX = -32767;        /* maximum X value of map */
  46. int MapMaxY = -32767;        /* maximum Y value of map */
  47. int MapMinX = 32767;        /* minimum X value of map */
  48. int MapMinY = 32767;        /* minimum Y value of map */
  49. Bool MadeChanges = FALSE;    /* made changes? */
  50. Bool MadeMapChanges = FALSE;    /* made changes that need rebuilding? */
  51.  
  52.  
  53. /*
  54.    read in the level data
  55. */
  56.  
  57. void ReadLevelData( int episode, int mission) /* SWAP! */
  58. {
  59.    MDirPtr dir;
  60.    char name[ 7];
  61.    int n, m;
  62.    int val;
  63.    int OldNumVertexes;
  64.    int *VertexUsed;
  65.  
  66.    /* No objects are needed: they may be swapped after they have been read */
  67.    ObjectsNeeded( 0);
  68.  
  69.    /* find the various level information from the master directory */
  70.    sprintf( name, "E%dM%d", episode, mission);
  71.    DisplayMessage( -1, -1, "Reading data for level %s...", name);
  72.    Level = FindMasterDir( MasterDir, name);
  73.    if (!Level)
  74.       ProgError( "level data not found");
  75.  
  76.    /* get the number of Vertices */
  77.    dir = FindMasterDir( Level, "VERTEXES");
  78.    if (dir != NULL)
  79.       OldNumVertexes = (int) (dir->dir.size / 4L);
  80.    else
  81.       OldNumVertexes = 0;
  82.    if (OldNumVertexes > 0)
  83.    {
  84.       VertexUsed = GetMemory( OldNumVertexes * sizeof( int));
  85.       for (n = 0; n < OldNumVertexes; n++)
  86.      VertexUsed[ n] = FALSE;
  87.    }
  88.  
  89.    /* read in the Things data */
  90.    dir = FindMasterDir( Level, "THINGS");
  91.    if (dir != NULL)
  92.       NumThings = (int) (dir->dir.size / 10L);
  93.    else
  94.       NumThings = 0;
  95.    if (NumThings > 0)
  96.    {
  97.       Things = GetFarMemory( (unsigned long) NumThings * sizeof( struct Thing));
  98.       BasicWadSeek( dir->wadfile, dir->dir.start);
  99.       for (n = 0; n < NumThings; n++)
  100.       {
  101.      BasicWadRead( dir->wadfile, &(Things[ n].xpos), 2);
  102.      BasicWadRead( dir->wadfile, &(Things[ n].ypos), 2);
  103.      BasicWadRead( dir->wadfile, &(Things[ n].angle), 2);
  104.      BasicWadRead( dir->wadfile, &(Things[ n].type), 2);
  105.      BasicWadRead( dir->wadfile, &(Things[ n].when), 2);
  106.       }
  107.    }
  108.  
  109.    /* read in the LineDef information */
  110.    dir = FindMasterDir( Level, "LINEDEFS");
  111.    if (dir != NULL)
  112.       NumLineDefs = (int) (dir->dir.size / 14L);
  113.    else
  114.       NumLineDefs = 0;
  115.    if (NumLineDefs > 0)
  116.    {
  117.       LineDefs = GetFarMemory( (unsigned long) NumLineDefs * sizeof( struct LineDef));
  118.       BasicWadSeek( dir->wadfile, dir->dir.start);
  119.       for (n = 0; n < NumLineDefs; n++)
  120.       {
  121.      BasicWadRead( dir->wadfile, &(LineDefs[ n].start), 2);
  122.      VertexUsed[ LineDefs[ n].start] = TRUE;
  123.      BasicWadRead( dir->wadfile, &(LineDefs[ n].end), 2);
  124.      VertexUsed[ LineDefs[ n].end] = TRUE;
  125.      BasicWadRead( dir->wadfile, &(LineDefs[ n].flags), 2);
  126.      BasicWadRead( dir->wadfile, &(LineDefs[ n].type), 2);
  127.      BasicWadRead( dir->wadfile, &(LineDefs[ n].tag), 2);
  128.      BasicWadRead( dir->wadfile, &(LineDefs[ n].sidedef1), 2);
  129.      BasicWadRead( dir->wadfile, &(LineDefs[ n].sidedef2), 2);
  130.       }
  131.    }
  132.  
  133.    /* read in the SideDef information */
  134.    dir = FindMasterDir( Level, "SIDEDEFS");
  135.    if (dir != NULL)
  136.       NumSideDefs = (int) (dir->dir.size / 30L);
  137.    else
  138.       NumSideDefs = 0;
  139.    if (NumSideDefs > 0)
  140.    {
  141.       SideDefs = GetFarMemory( (unsigned long) NumSideDefs * sizeof( struct SideDef));
  142.       BasicWadSeek( dir->wadfile, dir->dir.start);
  143.       for (n = 0; n < NumSideDefs; n++)
  144.       {
  145.      BasicWadRead( dir->wadfile, &(SideDefs[ n].xoff), 2);
  146.      BasicWadRead( dir->wadfile, &(SideDefs[ n].yoff), 2);
  147.      BasicWadRead( dir->wadfile, &(SideDefs[ n].tex1), 8);
  148.      BasicWadRead( dir->wadfile, &(SideDefs[ n].tex2), 8);
  149.      BasicWadRead( dir->wadfile, &(SideDefs[ n].tex3), 8);
  150.      BasicWadRead( dir->wadfile, &(SideDefs[ n].sector), 2);
  151.       }
  152.    }
  153.  
  154.    /* read in the Vertices which are all the corners of the level, but ignore the */
  155.    /* Vertices not used in any LineDef (they usually are at the end of the list). */
  156.    NumVertexes = 0;
  157.    for (n = 0; n < OldNumVertexes; n++)
  158.       if (VertexUsed[ n])
  159.      NumVertexes++;
  160.    if (NumVertexes > 0)
  161.    {
  162.       Vertexes = GetFarMemory( (unsigned long) NumVertexes * sizeof( struct Vertex));
  163.       dir = FindMasterDir( Level, "VERTEXES");
  164.       BasicWadSeek( dir->wadfile, dir->dir.start);
  165.       MapMaxX = -32767;
  166.       MapMaxY = -32767;
  167.       MapMinX = 32767;
  168.       MapMinY = 32767;
  169.       m = 0;
  170.       for (n = 0; n < OldNumVertexes; n++)
  171.       {
  172.      BasicWadRead( dir->wadfile, &val, 2);
  173.      if (VertexUsed[ n])
  174.      {
  175.         if (val < MapMinX)
  176.            MapMinX = val;
  177.         if (val > MapMaxX)
  178.            MapMaxX = val;
  179.         Vertexes[ m].x = val;
  180.      }
  181.      BasicWadRead( dir->wadfile, &val, 2);
  182.      if (VertexUsed[ n])
  183.      {
  184.         if (val < MapMinY)
  185.            MapMinY = val;
  186.         if (val > MapMaxY)
  187.            MapMaxY = val;
  188.         Vertexes[ m].y = val;
  189.         m++;
  190.      }
  191.       }
  192.       if (m != NumVertexes)
  193.      ProgError("inconsistency in the Vertexes data\n");
  194.    }
  195.  
  196.    if (OldNumVertexes > 0)
  197.    {
  198.       /* update the Vertex numbers in the LineDefs (not really necessary, but...) */
  199.       m = 0;
  200.       for (n = 0; n < OldNumVertexes; n++)
  201.      if (VertexUsed[ n])
  202.         VertexUsed[ n] = m++;
  203.       ObjectsNeeded( OBJ_LINEDEFS, 0);
  204.       for (n = 0; n < NumLineDefs; n++)
  205.       {
  206.      LineDefs[ n].start = VertexUsed[ LineDefs[ n].start];
  207.      LineDefs[ n].end = VertexUsed[ LineDefs[ n].end];
  208.       }
  209.       ObjectsNeeded( 0);
  210.       FreeMemory( VertexUsed);
  211.    }
  212.  
  213.    /* ignore the Segs, SSectors and Nodes */
  214.  
  215.    /* read in the Sectors information */
  216.    dir = FindMasterDir( Level, "SECTORS");
  217.    if (dir != NULL)
  218.       NumSectors = (int) (dir->dir.size / 26L);
  219.    else
  220.       NumSectors = 0;
  221.    if (NumSectors > 0)
  222.    {
  223.       Sectors = GetFarMemory( (unsigned long) NumSectors * sizeof( struct Sector));
  224.       BasicWadSeek( dir->wadfile, dir->dir.start);
  225.       for (n = 0; n < NumSectors; n++)
  226.       {
  227.      BasicWadRead( dir->wadfile, &(Sectors[ n].floorh), 2);
  228.      BasicWadRead( dir->wadfile, &(Sectors[ n].ceilh), 2);
  229.      BasicWadRead( dir->wadfile, &(Sectors[ n].floort), 8);
  230.      BasicWadRead( dir->wadfile, &(Sectors[ n].ceilt), 8);
  231.      BasicWadRead( dir->wadfile, &(Sectors[ n].light), 2);
  232.      BasicWadRead( dir->wadfile, &(Sectors[ n].special), 2);
  233.      BasicWadRead( dir->wadfile, &(Sectors[ n].tag), 2);
  234.       }
  235.    }
  236.  
  237.    /* ignore the last entries (Reject & BlockMap) */
  238. }
  239.  
  240.  
  241.  
  242. /*
  243.    forget the level data
  244. */
  245.  
  246. void ForgetLevelData() /* SWAP! */
  247. {
  248.    /* forget the Things */
  249.    ObjectsNeeded( OBJ_THINGS, 0);
  250.    NumThings = 0;
  251.    if (Things)
  252.       FreeFarMemory( Things);
  253.    Things = NULL;
  254.  
  255.    /* forget the Vertices */
  256.    ObjectsNeeded( OBJ_VERTEXES, 0);
  257.    NumVertexes = 0;
  258.    if (Vertexes)
  259.       FreeFarMemory( Vertexes);
  260.    Vertexes = NULL;
  261.  
  262.    /* forget the LineDefs */
  263.    ObjectsNeeded( OBJ_LINEDEFS, 0);
  264.    NumLineDefs = 0;
  265.    if (LineDefs)
  266.       FreeFarMemory( LineDefs);
  267.    LineDefs = NULL;
  268.  
  269.    /* forget the SideDefs */
  270.    ObjectsNeeded( OBJ_SIDEDEFS, 0);
  271.    NumSideDefs = 0;
  272.    if (SideDefs)
  273.       FreeFarMemory( SideDefs);
  274.    SideDefs = NULL;
  275.  
  276.    /* forget the Sectors */
  277.    ObjectsNeeded( OBJ_SECTORS, 0);
  278.    NumSectors = 0;
  279.    if (Sectors)
  280.       FreeFarMemory( Sectors);
  281.    Sectors = NULL;
  282.    ObjectsNeeded( 0);
  283. }
  284.  
  285.  
  286.  
  287. /*
  288.    recursively save the Nodes data to a PWAD file
  289. */
  290.  
  291. void SaveNodes( FILE *file, NPtr node)
  292. {
  293.    /* Nodes tree walk: save child1, save child2, save parent */
  294.    if ((node->child1 & 0x8000) == 0)
  295.    {
  296.       SaveNodes( file, node->node1);
  297.       node->child1 = node->node1->num;
  298.    }
  299.    if ((node->child2 & 0x8000) == 0)
  300.    {
  301.       SaveNodes( file, node->node2);
  302.       node->child2 = node->node2->num;
  303.    }
  304.    WriteBytes( file, &(node->x), 2L);
  305.    WriteBytes( file, &(node->y), 2L);
  306.    WriteBytes( file, &(node->dx), 2L);
  307.    WriteBytes( file, &(node->dy), 2L);
  308.    WriteBytes( file, &(node->maxy1), 2L);
  309.    WriteBytes( file, &(node->miny1), 2L);
  310.    WriteBytes( file, &(node->minx1), 2L);
  311.    WriteBytes( file, &(node->maxx1), 2L);
  312.    WriteBytes( file, &(node->maxy2), 2L);
  313.    WriteBytes( file, &(node->miny2), 2L);
  314.    WriteBytes( file, &(node->minx2), 2L);
  315.    WriteBytes( file, &(node->maxx2), 2L);
  316.    WriteBytes( file, &(node->child1), 2L);
  317.    WriteBytes( file, &(node->child2), 2L);
  318.    node->num = NumNodes++;
  319. }
  320.  
  321.  
  322.  
  323. /*
  324.    forget the Nodes
  325. */
  326.  
  327. void ForgetNodes( NPtr node)
  328. {
  329.    if ((node->child1 & 0x8000) == 0)
  330.       ForgetNodes( node->node1);
  331.    if ((node->child2 & 0x8000) == 0)
  332.       ForgetNodes( node->node2);
  333.    FreeFarMemory( node);
  334. }
  335.  
  336.  
  337.  
  338. /*
  339.    save the level data to a PWAD file
  340. */
  341.  
  342. void SaveLevelData( char *outfile) /* SWAP! */
  343. {
  344.    FILE   *file;
  345.    MDirPtr dir;
  346.    long    counter = 11;
  347.    int     n, i, j;
  348.    void   *data;
  349.    long    size;
  350.    long    dirstart;
  351.    int    *blockptr;
  352.    long    blocksize;
  353.    int     blockcount;
  354.    long    oldpos;
  355.    Bool    newnodes;
  356.    long    rejectsize;
  357.    int     oldNumVertexes;
  358.  
  359.    DisplayMessage( -1, -1, "Saving data to \"%s\"...", outfile);
  360.    LogMessage( ": Saving data to \"%s\"...\n", outfile);
  361.    oldNumVertexes = NumVertexes;
  362.    /* open the file */
  363.    if ((file = fopen( outfile, "wb")) == NULL)
  364.       ProgError( "Unable to open file \"%s\"", outfile);
  365.    WriteBytes( file, "PWAD", 4L);     /* PWAD file */
  366.    WriteBytes( file, &counter, 4L);   /* 11 entries */
  367.    WriteBytes( file, &counter, 4L);   /* fix this up later */
  368.    counter = 12L;
  369.    dir = Level->next;
  370.  
  371.    /* output the things data */
  372.    ObjectsNeeded( OBJ_THINGS, 0);
  373.    for (n = 0; n < NumThings; n++)
  374.    {
  375.       WriteBytes( file, &(Things[ n].xpos), 2L);
  376.       WriteBytes( file, &(Things[ n].ypos), 2L);
  377.       WriteBytes( file, &(Things[ n].angle), 2L);
  378.       WriteBytes( file, &(Things[ n].type), 2L);
  379.       WriteBytes( file, &(Things[ n].when), 2L);
  380.       counter += 10L;
  381.    }
  382.    dir = dir->next;
  383.  
  384.    /* update MapMinX, MapMinY, MapMaxX, MapMaxY */
  385.    ObjectsNeeded( OBJ_VERTEXES, 0);
  386.    MapMaxX = -32767;
  387.    MapMaxY = -32767;
  388.    MapMinX = 32767;
  389.    MapMinY = 32767;
  390.    for (n = 0; n < NumVertexes; n++)
  391.    {
  392.       if (Vertexes[ n].x < MapMinX)
  393.      MapMinX = Vertexes[ n].x;
  394.       if (Vertexes[ n].x > MapMaxX)
  395.      MapMaxX = Vertexes[ n].x;
  396.       if (Vertexes[ n].y < MapMinY)
  397.      MapMinY = Vertexes[ n].y;
  398.       if (Vertexes[ n].y > MapMaxY)
  399.      MapMaxY = Vertexes[ n].y;
  400.    }
  401.  
  402.    /* do we need to rebuild the Nodes, Segs and SSectors? */
  403.    if (MadeMapChanges && (Expert || Confirm( -1, 270, "Do you want to rebuild the NODES, SEGS, SSECTORS, REJECT and BLOCKMAP?",
  404.                               "WARNING: You won't be able to use your level if you don't do this...")))
  405.    {
  406.       SEPtr seglist;
  407.  
  408.       if (UseMouse)
  409.      HideMousePointer();
  410.       ClearScreen();
  411.       DrawScreenBox3D( 218, 0, ScrMaxX, 55);
  412.       SetColor( WHITE);
  413.       DrawScreenText( 225, 10, "Rebuilding the NODES...");
  414.       DrawScreenBoxHollow( 225, 28, ScrMaxX - 10, 48);
  415.       DrawScreenMeter( 225, 28, ScrMaxX - 10, 48, 0.0);
  416.       if (UseMouse)
  417.      ShowMousePointer();
  418.       seglist = NULL;
  419.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_VERTEXES, 0);
  420.       for (n = 0; n < NumLineDefs; n++)
  421.       {
  422.      if (LineDefs[ n].sidedef1 >= 0)
  423.      {
  424.         if (seglist)
  425.         {
  426.            LastSeg->next = GetMemory( sizeof( struct Seg));
  427.            LastSeg = LastSeg->next;
  428.         }
  429.         else
  430.         {
  431.            seglist = GetMemory( sizeof( struct Seg));
  432.            LastSeg = seglist;
  433.         }
  434.         LastSeg->next = NULL;
  435.         LastSeg->start = LineDefs[ n].start;
  436.         LastSeg->end = LineDefs[ n].end;
  437.         LastSeg->angle = ComputeAngle(Vertexes[ LineDefs[ n].end].x - Vertexes[ LineDefs[ n].start].x,
  438.                       Vertexes[ LineDefs[ n].end].y - Vertexes[ LineDefs[ n].start].y);
  439.         LastSeg->linedef = n;
  440.         LastSeg->flip = 0;
  441.         LastSeg->dist = 0;
  442.      }
  443.      if (LineDefs[ n].sidedef2 >= 0)
  444.      {
  445.         if (seglist)
  446.         {
  447.            LastSeg->next = GetMemory( sizeof( struct Seg));
  448.            LastSeg = LastSeg->next;
  449.         }
  450.         else
  451.         {
  452.            seglist = GetMemory( sizeof( struct Seg));
  453.            LastSeg = seglist;
  454.         }
  455.         LastSeg->next = NULL;
  456.         LastSeg->start = LineDefs[ n].end;
  457.         LastSeg->end = LineDefs[ n].start;
  458.         LastSeg->angle = ComputeAngle(Vertexes[ LineDefs[ n].start].x - Vertexes[ LineDefs[ n].end].x,
  459.                       Vertexes[ LineDefs[ n].start].y - Vertexes[ LineDefs[ n].end].y);
  460.         LastSeg->linedef = n;
  461.         LastSeg->flip = 1;
  462.         LastSeg->dist = 0;
  463.      }
  464.       }
  465.       ShowProgress( OBJ_VERTEXES);
  466.       ShowProgress( OBJ_SIDEDEFS);
  467.       LogMessage( ": Starting Nodes builder...\n");
  468.       LogMessage( "\tNumber of Vertices: %d\n", NumVertexes);
  469.       LogMessage( "\tNumber of Segs:     %d\n", NumSegs);
  470.       ObjectsNeeded( OBJ_VERTEXES, 0);
  471.       if (CreateNodes( &Nodes, &n, seglist) == FALSE)
  472.       {
  473.      Beep();
  474.      Beep();
  475.      Beep();
  476.          LogMessage( "\nError: CreateNodes failed!\n\n");
  477.      Beep();
  478.      Beep();
  479.      Beep();
  480.       }
  481.       LogMessage( ": Nodes created OK.\n");
  482.       LogMessage( "\tNumber of Vertices: %d\n", NumVertexes);
  483.       LogMessage( "\tNumber of SideDefs: %d\n", NumSideDefs);
  484.       LogMessage( "\tNumber of Segs:     %d\n", NumSegs);
  485.       LogMessage( "\tNumber of SSectors: %d\n", NumSSectors);
  486.       if (UseMouse)
  487.      HideMousePointer();
  488.       DrawScreenMeter( 225, 28, ScrMaxX - 10, 48, 1.0);
  489.       if (UseMouse)
  490.      ShowMousePointer();
  491.       newnodes = TRUE;
  492.    }
  493.    else
  494.       newnodes = FALSE;
  495.  
  496.    /* output the LineDefs */
  497.    ObjectsNeeded( OBJ_LINEDEFS, 0);
  498.    for (n = 0; n < NumLineDefs; n++)
  499.    {
  500.       WriteBytes( file, &(LineDefs[ n].start), 2L);
  501.       WriteBytes( file, &(LineDefs[ n].end), 2L);
  502.       WriteBytes( file, &(LineDefs[ n].flags), 2L);
  503.       WriteBytes( file, &(LineDefs[ n].type), 2L);
  504.       WriteBytes( file, &(LineDefs[ n].tag), 2L);
  505.       WriteBytes( file, &(LineDefs[ n].sidedef1), 2L);
  506.       WriteBytes( file, &(LineDefs[ n].sidedef2), 2L);
  507.       counter += 14L;
  508.    }
  509.    dir = dir->next;
  510.  
  511.    /* output the SideDefs */
  512.    ObjectsNeeded( OBJ_SIDEDEFS, 0);
  513.    for (n = 0; n < NumSideDefs; n++)
  514.    {
  515.       WriteBytes( file, &(SideDefs[ n].xoff), 2L);
  516.       WriteBytes( file, &(SideDefs[ n].yoff), 2L);
  517.       WriteBytes( file, &(SideDefs[ n].tex1), 8L);
  518.       WriteBytes( file, &(SideDefs[ n].tex2), 8L);
  519.       WriteBytes( file, &(SideDefs[ n].tex3), 8L);
  520.       WriteBytes( file, &(SideDefs[ n].sector), 2L);
  521.       counter += 30L;
  522.    }
  523.    dir = dir->next;
  524.  
  525.    if (MadeMapChanges)
  526.    {
  527.       /* output the Vertices */
  528.       ObjectsNeeded( OBJ_VERTEXES, 0);
  529.       for (n = 0; n < NumVertexes; n++)
  530.       {
  531.      WriteBytes( file, &(Vertexes[ n].x), 2L);
  532.      WriteBytes( file, &(Vertexes[ n].y), 2L);
  533.      counter += 4L;
  534.       }
  535.    }
  536.    else
  537.    {
  538.       /* copy the Vertices */
  539.       ObjectsNeeded( 0);
  540.       size = dir->dir.size;
  541.       counter += size;
  542.       BasicWadSeek( dir->wadfile, dir->dir.start);
  543.       CopyBytes( file, dir->wadfile->fileinfo, size);
  544.    }
  545.    dir = dir->next;
  546.  
  547.    if (newnodes)
  548.    {
  549.       SEPtr curse, oldse;
  550.       SSPtr curss, oldss;
  551.  
  552.       ObjectsNeeded( 0);
  553.       /* output and forget the Segments */
  554.       curse = Segs;
  555.       while (curse)
  556.       {
  557.      WriteBytes( file, &(curse->start), 2L);
  558.      WriteBytes( file, &(curse->end), 2L);
  559.      WriteBytes( file, &(curse->angle), 2L);
  560.      WriteBytes( file, &(curse->linedef), 2L);
  561.      WriteBytes( file, &(curse->flip), 2L);
  562.      WriteBytes( file, &(curse->dist), 2L);
  563.      oldse = curse;
  564.      curse = curse->next;
  565.      FreeFarMemory( oldse);
  566.      counter += 12L;
  567.       }
  568.       Segs = NULL;
  569.       dir = dir->next;
  570.  
  571.       /* output and forget the SSectors */
  572.       curss = SSectors;
  573.       while (curss)
  574.       {
  575.      WriteBytes( file, &(curss->num), 2L);
  576.      WriteBytes( file, &(curss->first), 2L);
  577.      oldss = curss;
  578.      curss = curss->next;
  579.      FreeFarMemory( oldss);
  580.      counter += 4L;
  581.       }
  582.       SSectors = NULL;
  583.       dir = dir->next;
  584.  
  585.       /* output the Nodes */
  586.       NumNodes = 0;
  587.       SaveNodes( file, Nodes);
  588.       counter += (long) NumNodes * 28L;
  589.       dir = dir->next;
  590.  
  591.       /* forget the Nodes */
  592.       ForgetNodes( Nodes);
  593.       Nodes = NULL;
  594.    }
  595.    else
  596.    {
  597.       /* copy the Segs, SSectors and Nodes */
  598.       for (n = 0; n < 3; n++)
  599.       {
  600.      size = dir->dir.size;
  601.      counter += size;
  602.      BasicWadSeek( dir->wadfile, dir->dir.start);
  603.      CopyBytes( file, dir->wadfile->fileinfo, size);
  604.      dir = dir->next;
  605.       }
  606.    }
  607.  
  608.    /* output the Sectors */
  609.    ObjectsNeeded( OBJ_SECTORS, 0);
  610.    for (n = 0; n < NumSectors; n++)
  611.    {
  612.       WriteBytes( file, &(Sectors[ n].floorh), 2L);
  613.       WriteBytes( file, &(Sectors[ n].ceilh), 2L);
  614.       WriteBytes( file, &(Sectors[ n].floort), 8L);
  615.       WriteBytes( file, &(Sectors[ n].ceilt), 8L);
  616.       WriteBytes( file, &(Sectors[ n].light), 2L);
  617.       WriteBytes( file, &(Sectors[ n].special), 2L);
  618.       WriteBytes( file, &(Sectors[ n].tag), 2L);
  619.       counter += 26L;
  620.    }
  621.    dir = dir->next;
  622.  
  623.    if (newnodes)
  624.    {
  625.       /* create and output the reject data */
  626.       ObjectsNeeded( OBJ_SECTORS, 0); /* !!! */
  627.       if (UseMouse)
  628.      HideMousePointer();
  629.       DrawScreenBox3D( 218, 80, ScrMaxX, 135);
  630.       SetColor( WHITE);
  631.       DrawScreenText( 225, 90, "Rebuilding the REJECT data...");
  632.       DrawScreenBoxHollow( 225, 108, ScrMaxX - 10, 128);
  633.       DrawScreenMeter( 225, 108, ScrMaxX - 10, 128, 0.0);
  634.       if (UseMouse)
  635.      ShowMousePointer();
  636.       rejectsize = ((long) NumSectors * (long) NumSectors + 7L) / 8L;
  637.       data = GetMemory( (size_t) rejectsize);
  638.       for (i = 0; i < rejectsize; i++)
  639.      ((char *) data)[ i] = 0;
  640.       for (i = 0; i < NumSectors; i++)
  641.       {
  642.      if (UseMouse)
  643.         HideMousePointer();
  644.      DrawScreenMeter( 225, 108, ScrMaxX - 10, 128, (float) i / (float) NumSectors);
  645.      if (UseMouse)
  646.         ShowMousePointer();
  647.      for (j = 0; j < NumSectors; j++)
  648.      {
  649. /*
  650.         if (Reject( i, j))
  651.            data[ (i * NumSectors + j) / 8] |= 1 <<
  652. */
  653.      }
  654.       }
  655.       if (UseMouse)
  656.      HideMousePointer();
  657.       DrawScreenMeter( 225, 108, ScrMaxX - 10, 128, 1.0);
  658.       if (UseMouse)
  659.      ShowMousePointer();
  660.       WriteBytes( file, data, rejectsize);
  661.       counter += rejectsize;
  662.       dir = dir->next;
  663.       FreeMemory( data);
  664.    }
  665.    else
  666.    {
  667.       /* copy the Reject data */
  668.       ObjectsNeeded( 0);
  669.       rejectsize = dir->dir.size;
  670.       size = rejectsize;
  671.       counter += size;
  672.       BasicWadSeek( dir->wadfile, dir->dir.start);
  673.       CopyBytes( file, dir->wadfile->fileinfo, size);
  674.       dir = dir->next;
  675.    }
  676.  
  677.    if (newnodes)
  678.    {
  679.       int mminx, mminy, mnumx, mnumy;
  680.  
  681.       /* create and output the blockmap */
  682.       ObjectsNeeded( OBJ_LINEDEFS, OBJ_VERTEXES, 0);
  683.       if (UseMouse)
  684.      HideMousePointer();
  685.       DrawScreenBox3D( 218, 160, ScrMaxX, 215);
  686.       SetColor( WHITE);
  687.       DrawScreenText( 225, 170, "Rebuilding the BLOCKMAP...");
  688.       DrawScreenBoxHollow( 225, 188, ScrMaxX - 10, 208);
  689.       DrawScreenMeter( 225, 188, ScrMaxX - 10, 208, 0.0);
  690.       if (UseMouse)
  691.      ShowMousePointer();
  692.       mminx = (int) (MapMinX / 8 - 8) * 8;
  693.       WriteBytes( file, &mminx, 2L);
  694.       mminy = (int) (MapMinY / 8 - 8) * 8;
  695.       WriteBytes( file, &mminy, 2L);
  696.       mnumx = MapMaxX / 128 - mminx / 128 + 2;
  697.       WriteBytes( file, &mnumx, 2L);
  698.       mnumy = MapMaxY / 128 - mminy / 128 + 2;
  699.       WriteBytes( file, &mnumy, 2L);
  700.       counter += 8L;
  701.       oldpos = ftell( file);
  702.       blocksize = (long) (mnumx * mnumy * sizeof( int));
  703.       blockptr = GetMemory( blocksize);
  704.       WriteBytes( file, blockptr, blocksize);
  705.       blocksize += 8L;
  706.       counter += blocksize - 7L;
  707.       blockcount = mnumx * mnumy + 4;
  708.       for (i = 0; i < mnumy; i++)
  709.       {
  710.      if (UseMouse)
  711.         HideMousePointer();
  712.      DrawScreenMeter( 225, 188, ScrMaxX - 10, 208, (float) i / (float) mnumy);
  713.      if (UseMouse)
  714.         ShowMousePointer();
  715.      for (j = 0; j < mnumx; j++)
  716.      {
  717.         blockptr[ mnumx * i + j] = blockcount;
  718.         n = 0;
  719.         WriteBytes( file, &n, 2L);
  720.         counter += 2L;
  721.         blocksize += 2L;
  722.         blockcount++;
  723.         for (n = 0; n < NumLineDefs; n++)
  724.            if (IsLineDefInside( n, mminx + j * 128, mminy + i * 128, mminx + 127 + j * 128, mminy + 127 + i * 128))
  725.            {
  726.           WriteBytes( file, &n, 2L);
  727.           counter += 2L;
  728.           blocksize += 2L;
  729.           blockcount++;
  730.            }
  731.         n = -1;
  732.         WriteBytes( file, &n, 2L);
  733.         counter += 2L;
  734.         blocksize += 2L;
  735.         blockcount++;
  736.      }
  737.       }
  738.       if (UseMouse)
  739.      HideMousePointer();
  740.       DrawScreenMeter( 225, 188, ScrMaxX - 10, 208, 1.0);
  741.       if (UseMouse)
  742.      ShowMousePointer();
  743.       size = ftell( file);
  744.       fseek( file, oldpos, SEEK_SET);
  745.       WriteBytes( file, blockptr, (long) (mnumx * mnumy * sizeof( int)));
  746.       fseek( file, size, SEEK_SET);
  747.       if (FindMasterDir( dir, "P2_END"))
  748.      counter--;
  749.       FreeMemory( blockptr);
  750.    }
  751.    else
  752.    {
  753.       /* copy the blockmap data */
  754.       ObjectsNeeded( 0);
  755.       blocksize = dir->dir.size;
  756.       size = blocksize;
  757.       counter += size;
  758.       BasicWadSeek( dir->wadfile, dir->dir.start);
  759.       CopyBytes( file, dir->wadfile->fileinfo, size);
  760.       dir = dir->next;
  761.    }
  762.  
  763.  
  764.    /* output the actual directory */
  765.    dirstart = counter;
  766.    counter = 12L;
  767.    size = 0L;
  768.    dir = Level;
  769.    WriteBytes( file, &counter, 4L);
  770.    WriteBytes( file, &size, 4L);
  771.    WriteBytes( file, &(dir->dir.name), 8L);
  772.    dir = dir->next;
  773.  
  774.    size = (long) NumThings * 10L;
  775.    WriteBytes( file, &counter, 4L);
  776.    WriteBytes( file, &size, 4L);
  777.    WriteBytes( file, "THINGS\0\0", 8L);
  778.    counter += size;
  779.    dir = dir->next;
  780.  
  781.    size = (long) NumLineDefs * 14L;
  782.    WriteBytes( file, &counter, 4L);
  783.    WriteBytes( file, &size, 4L);
  784.    WriteBytes( file, "LINEDEFS", 8L);
  785.    counter += size;
  786.    dir = dir->next;
  787.  
  788.    size = (long) NumSideDefs * 30L;
  789.    WriteBytes( file, &counter, 4L);
  790.    WriteBytes( file, &size, 4L);
  791.    WriteBytes( file, "SIDEDEFS", 8L);
  792.    counter += size;
  793.    dir = dir->next;
  794.  
  795.    if (MadeMapChanges)
  796.       size = (long) NumVertexes * 4L;
  797.    else
  798.       size = dir->dir.size;
  799.    WriteBytes( file, &counter, 4L);
  800.    WriteBytes( file, &size, 4L);
  801.    WriteBytes( file, "VERTEXES", 8L);
  802.    counter += size;
  803.    dir = dir->next;
  804.  
  805.    if (newnodes)
  806.       size = (long) NumSegs * 12L;
  807.    else
  808.       size = dir->dir.size;
  809.    WriteBytes( file, &counter, 4L);
  810.    WriteBytes( file, &size, 4L);
  811.    WriteBytes( file, "SEGS\0\0\0\0", 8L);
  812.    counter += size;
  813.    dir = dir->next;
  814.  
  815.    if (newnodes)
  816.       size = (long) NumSSectors * 4L;
  817.    else
  818.       size = dir->dir.size;
  819.    WriteBytes( file, &counter, 4L);
  820.    WriteBytes( file, &size, 4L);
  821.    WriteBytes( file, "SSECTORS", 8L);
  822.    counter += size;
  823.    dir = dir->next;
  824.  
  825.    if (newnodes)
  826.       size = (long) NumNodes * 28L;
  827.    else
  828.       size = dir->dir.size;
  829.    WriteBytes( file, &counter, 4L);
  830.    WriteBytes( file, &size, 4L);
  831.    WriteBytes( file, "NODES\0\0\0", 8L);
  832.    counter += size;
  833.    dir = dir->next;
  834.  
  835.    size = (long) NumSectors * 26L;
  836.    WriteBytes( file, &counter, 4L);
  837.    WriteBytes( file, &size, 4L);
  838.    WriteBytes( file, "SECTORS\0", 8L);
  839.    counter += size;
  840.    dir = dir->next;
  841.  
  842.    size = rejectsize;
  843.    WriteBytes( file, &counter, 4L);
  844.    WriteBytes( file, &size, 4L);
  845.    WriteBytes( file, "REJECT\0\0", 8L);
  846.    counter += size;
  847.    dir = dir->next;
  848.  
  849.    size = blocksize;
  850.    WriteBytes( file, &counter, 4L);
  851.    WriteBytes( file, &size, 4L);
  852.    WriteBytes( file, "BLOCKMAP", 8L);
  853.    counter += size;
  854.    dir = dir->next;
  855.  
  856.    /* fix up the directory start information */
  857.    if (fseek( file, 8L, SEEK_SET))
  858.       ProgError( "error writing to file");
  859.    WriteBytes( file, &dirstart, 4L);
  860.  
  861.    /* close the file */
  862.    fclose( file);
  863.  
  864.    NumSegs = 0;
  865.    NumSSectors = 0;
  866.    NumNodes = 0;
  867.  
  868.    /* delete the vertices added by the Nodes builder */
  869.    if (NumVertexes != oldNumVertexes)
  870.    {
  871.       ObjectsNeeded( OBJ_VERTEXES, 0);
  872.       NumVertexes = oldNumVertexes;
  873.       ResizeFarMemory( Vertexes, NumVertexes * sizeof( struct Vertex));
  874.    }
  875.  
  876.    /* the file is now up to date */
  877.    MadeChanges = FALSE;
  878.    if (newnodes)
  879.       MadeMapChanges = FALSE;
  880.    ObjectsNeeded( 0);
  881.  
  882.    /* update pointers in Master Directory */
  883.    OpenPatchWad( outfile);
  884.  
  885.    /* this should free the old "*.BAK" file */
  886.    CloseUnusedWadFiles();
  887.  
  888. }
  889.  
  890.  
  891.  
  892. /*
  893.    function used by qsort to sort the texture names
  894. */
  895. int SortTextures( const void *a, const void *b)
  896. {
  897.    return strcmp( *((char **)a), *((char **)b));
  898. }
  899.  
  900.  
  901.  
  902. /*
  903.    read in the wall texture names
  904. */
  905.  
  906. void ReadWTextureNames()
  907. {
  908.    MDirPtr dir;
  909.    long *offsets;
  910.    int n;
  911.    long val;
  912.  
  913.    printf("Reading wall texture names\n");
  914.    dir = FindMasterDir( MasterDir, "TEXTURE1");
  915.    BasicWadSeek( dir->wadfile, dir->dir.start);
  916.    BasicWadRead( dir->wadfile, &val, 4);
  917.    NumWTexture = (int) val + 1;
  918.    /* read in the offsets for texture1 names */
  919.    offsets = GetMemory( NumWTexture * sizeof( long));
  920.    for (n = 1; n < NumWTexture; n++)
  921.       BasicWadRead( dir->wadfile, &(offsets[ n]), 4);
  922.    /* read in the actual names */
  923.    WTexture = GetMemory( NumWTexture * sizeof( char *));
  924.    WTexture[ 0] = GetMemory( 9 * sizeof( char));
  925.    strcpy(WTexture[ 0], "-");
  926.    for (n = 1; n < NumWTexture; n++)
  927.    {
  928.       WTexture[ n] = GetMemory( 9 * sizeof( char));
  929.       BasicWadSeek( dir->wadfile, dir->dir.start + offsets[ n]);
  930.       BasicWadRead( dir->wadfile, WTexture[ n], 8);
  931.       WTexture[ n][ 8] = '\0';
  932.    }
  933.    FreeMemory( offsets);
  934.    if (Registered)
  935.    {
  936.       dir = FindMasterDir( MasterDir, "TEXTURE2");
  937.       BasicWadSeek( dir->wadfile, dir->dir.start);
  938.       BasicWadRead( dir->wadfile, &val, 4);
  939.       /* read in the offsets for texture2 names */
  940.       offsets = GetMemory( val * sizeof( long));
  941.       for (n = 0; n < val; n++)
  942.      BasicWadRead( dir->wadfile, &(offsets[ n]), 4);
  943.       /* read in the actual names */
  944.       WTexture = ResizeMemory( WTexture, (NumWTexture + val) * sizeof( char *));
  945.       for (n = 0; n < val; n++)
  946.       {
  947.      WTexture[ NumWTexture + n] = GetMemory( 9 * sizeof( char));
  948.      BasicWadSeek( dir->wadfile, dir->dir.start + offsets[ n]);
  949.      BasicWadRead( dir->wadfile, WTexture[ NumWTexture + n], 8);
  950.      WTexture[ NumWTexture + n][ 8] = '\0';
  951.       }
  952.       NumWTexture += val;
  953.       FreeMemory( offsets);
  954.    }
  955.    /* sort the names */
  956.    qsort( WTexture, NumWTexture, sizeof( char *), SortTextures);
  957. }
  958.  
  959.  
  960.  
  961. /*
  962.    forget the wall texture names
  963. */
  964.  
  965. void ForgetWTextureNames()
  966. {
  967.    int n;
  968.  
  969.    /* forget all names */
  970.    for (n = 0; n < NumWTexture; n++)
  971.       FreeMemory( WTexture[ n]);
  972.  
  973.    /* forget the array */
  974.    NumWTexture = 0;
  975.    FreeMemory( WTexture);
  976. }
  977.  
  978.  
  979.  
  980. /*
  981.    read in the floor/ceiling texture names
  982. */
  983.  
  984. void ReadFTextureNames()
  985. {
  986.    MDirPtr dir;
  987.    int n, m;
  988.  
  989.    printf("Reading floor/ceiling texture names\n");
  990.    /* count the names */
  991.    dir = FindMasterDir( MasterDir, "F1_START");
  992.    dir = dir->next;
  993.    for (n = 0; dir && strcmp(dir->dir.name, "F1_END"); n++)
  994.       dir = dir->next;
  995.    NumFTexture = n;
  996.    /* get the actual names from master dir. */
  997.    dir = FindMasterDir( MasterDir, "F1_START");
  998.    dir = dir->next;
  999.    FTexture = GetMemory( NumFTexture * sizeof( char *));
  1000.    for (n = 0; n < NumFTexture; n++)
  1001.    {
  1002.       FTexture[ n] = GetMemory( 9 * sizeof( char));
  1003.       strncpy( FTexture[ n], dir->dir.name, 8);
  1004.       FTexture[ n][ 8] = '\0';
  1005.       dir = dir->next;
  1006.    }
  1007.    if (Registered)
  1008.    {
  1009.       /* count the names */
  1010.       dir = FindMasterDir( MasterDir, "F2_START");
  1011.       dir = dir->next;
  1012.       for (n = 0; dir && strcmp(dir->dir.name, "F2_END"); n++)
  1013.      dir = dir->next;
  1014.       /* get the actual names from master dir. */
  1015.       dir = FindMasterDir( MasterDir, "F2_START");
  1016.       dir = dir->next;
  1017.       FTexture = ResizeMemory( FTexture, (NumFTexture + n) * sizeof( char *));
  1018.       for (m = NumFTexture; m < NumFTexture + n; m++)
  1019.       {
  1020.      FTexture[ m] = GetMemory( 9 * sizeof( char));
  1021.      strncpy( FTexture[ m], dir->dir.name, 8);
  1022.      FTexture[ m][ 8] = '\0';
  1023.      dir = dir->next;
  1024.       }
  1025.       NumFTexture += n;
  1026.    }
  1027.    /* sort the names */
  1028.    qsort( FTexture, NumFTexture, sizeof( char *), SortTextures);
  1029. }
  1030.  
  1031.  
  1032.  
  1033. /*
  1034.    forget the floor/ceiling texture names
  1035. */
  1036.  
  1037. void ForgetFTextureNames()
  1038. {
  1039.    int n;
  1040.  
  1041.    /* forget all names */
  1042.    for (n = 0; n < NumFTexture; n++)
  1043.       FreeMemory( FTexture[ n]);
  1044.  
  1045.    /* forget the array */
  1046.    NumFTexture = 0;
  1047.    FreeMemory( FTexture);
  1048. }
  1049.  
  1050.  
  1051.  
  1052. /* end of file */
  1053.