home *** CD-ROM | disk | FTP | other *** search
/ Doom Fever / Doom_Fever-1995_Maple_Media.iso / wad / source.zip / NODES.C < prev    next >
C/C++ Source or Header  |  1994-05-18  |  18KB  |  496 lines

  1. /*
  2.    Nodes builder by Raphaël Quinet <quinet@montefiore.ulg.ac.be>
  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.    *------- PLEASE READ THE COMMENT AT THE END OF THIS FILE. -------*
  11.    | If you use the algorithm or even some of the ideas taken from  |
  12.    | this file, you must put a message in your program, so that the |
  13.    | user knows that all or part of the algorithm comes from DEU.   |
  14.    *------- PLEASE READ THE COMMENT AT THE END OF THIS FILE. -------*
  15.  
  16.    NODES.C - automatic builder for Nodes, Segs and SSectors.
  17. */
  18.  
  19. /* the includes */
  20. #include "deu.h"
  21. #include "levels.h"
  22.  
  23.  
  24.  
  25. /*
  26.    display some informations while the user is waiting
  27. */
  28.  
  29. void ShowProgress( int objtype)
  30. {
  31.    static int SavedNumVertexes = 0;
  32.  
  33.    if (UseMouse)
  34.       HideMousePointer();
  35.    switch (objtype)
  36.    {
  37.    case OBJ_VERTEXES:
  38.       DrawScreenBox3D( 0, 0, 203, 22);
  39.       DrawScreenText( 10, 8, "Number of Vertices: %d", NumVertexes);
  40.       break;
  41.    case OBJ_SIDEDEFS:
  42.       DrawScreenBox3D( 0, 30, 203, 52);
  43.       DrawScreenText( 10, 38, "Number of SideDefs: %d", NumSideDefs);
  44.       SavedNumVertexes = NumVertexes;
  45.       break;
  46.    case OBJ_SSECTORS:
  47.       DrawScreenBox3D( 0, 60, 203, 92);
  48.       DrawScreenText( 10, 68, "Number of Segs:     %d", NumSegs);
  49.       DrawScreenText( 10, 78, "Number of SSectors: %d", NumSSectors);
  50.       DrawScreenMeter( 225, 28, ScrMaxX - 10, 48, (float) NumSegs / (float) (NumSideDefs + NumVertexes - SavedNumVertexes));
  51.       break;
  52.    }
  53.    if (UseMouse)
  54.       ShowMousePointer();
  55. }
  56.  
  57.  
  58.  
  59. /*
  60.    find the point of intersection for two lines (return FALSE if there is none)
  61. */
  62.  
  63. Bool ComputeIntersection( int *x, int *y, SEPtr seg1, SEPtr seg2) /* SWAP - needs Vertexes */
  64. {
  65.    /* floating-point required because long integers cause errors */
  66.    double x1  = Vertexes[ seg1->start].x;
  67.    double y1  = Vertexes[ seg1->start].y;
  68.    double dx1 = Vertexes[ seg1->end].x - Vertexes[ seg1->start].x;
  69.    double dy1 = Vertexes[ seg1->end].y - Vertexes[ seg1->start].y;
  70.    double x2  = Vertexes[ seg2->start].x;
  71.    double y2  = Vertexes[ seg2->start].y;
  72.    double dx2 = Vertexes[ seg2->end].x - Vertexes[ seg2->start].x;
  73.    double dy2 = Vertexes[ seg2->end].y - Vertexes[ seg2->start].y;
  74.    double d;
  75.  
  76.    d = dy1 * dx2 - dx1 * dy2;
  77.    if (d != 0.0)
  78.    {
  79.       x1 = y1 * dx1 - x1 * dy1;
  80.       x2 = y2 * dx2 - x2 * dy2;
  81.       /* (*x, *y) = intersection */
  82.       *x = (int) ((dx1 * x2 - dx2 * x1) / d);
  83.       *y = (int) ((dy1 * x2 - dy2 * x1) / d);
  84.       /* check if the intersection is not at one end of a Seg (vertex grid = 8*8) */
  85.       if (*x >= Vertexes[ seg1->start].x - 7 && *x <= Vertexes[ seg1->start].x + 7 && *y >= Vertexes[ seg1->start].y - 7 && *y <= Vertexes[ seg1->start].y + 7)
  86.       {
  87.      return FALSE; /* not a real intersection point (round-off error in a previous operation) */
  88.       }
  89.       if (*x >= Vertexes[ seg1->end].x - 7   && *x <= Vertexes[ seg1->end].x + 7   && *y >= Vertexes[ seg1->end].y - 7   && *y <= Vertexes[ seg1->end].y + 7  )
  90.       {
  91.      return FALSE; /* not a real intersection point (round-off error in a previous operation) */
  92.       }
  93.       if (*x >= Vertexes[ seg2->start].x - 7 && *x <= Vertexes[ seg2->start].x + 7 && *y >= Vertexes[ seg2->start].y - 7 && *y <= Vertexes[ seg2->start].y + 7)
  94.       {
  95.      return FALSE; /* not a real intersection point (round-off error in a previous operation) */
  96.       }
  97.       if (*x >= Vertexes[ seg2->end].x - 7   && *x <= Vertexes[ seg2->end].x + 7   && *y >= Vertexes[ seg2->end].y - 7   && *y <= Vertexes[ seg2->end].y + 7  )
  98.       {
  99.      return FALSE; /* not a real intersection point (round-off error in a previous operation) */
  100.       }
  101.       return TRUE; /* intersection OK */
  102.    }
  103.    else
  104.       return FALSE; /* parallel lines */
  105. }
  106.  
  107.  
  108.  
  109. /*
  110.    choose a nodeline amongst the list of Segs
  111. */
  112.  
  113. SEPtr FindNodeLine( SEPtr seglist) /* SWAP - needs Vertexes */
  114. {
  115.    int   splits;
  116. #ifdef OLD_ALGORITHM
  117.    int   minsplits = 32767;
  118. #endif /* OLD_ALGORITHM */
  119.    int   mindiff = 32767;
  120.    int   num1, num2;
  121.    SEPtr nodeline, bestnodeline;
  122.    SEPtr curseg;
  123.    long  x, y;
  124.    long  dx, dy;
  125.    long  a, b, c, d;
  126.    int   dummyx, dummyy;
  127.    /* ***DEBUG*** */
  128.    static SEPtr lastnodeline = NULL;
  129.  
  130.    /* find nodeline - brute force: try with all Segs */
  131.    bestnodeline = NULL;
  132.    for (nodeline = seglist; nodeline; nodeline = nodeline->next)
  133.    {
  134.       /* compute x, y, dx, dy */
  135.       x = Vertexes[ nodeline->start].x;
  136.       y = Vertexes[ nodeline->start].y;
  137.       dx = Vertexes[ nodeline->end].x - Vertexes[ nodeline->start].x;
  138.       dy = Vertexes[ nodeline->end].y - Vertexes[ nodeline->start].y;
  139.       /* compute number of splits */
  140.       if (dx == 0 || dy == 0)
  141.      splits = 0;
  142.       else
  143.      splits = 1; /* small penalty for oblique lines */
  144.       num1 = 0;
  145.       num2 = 0;
  146.       for (curseg = seglist; curseg; curseg = curseg->next)
  147.       {
  148.      if (curseg == nodeline)
  149.      {
  150.         num1++;
  151.         continue;
  152.      }
  153.      /* you love maths, don't you? */
  154.      a = ((long) Vertexes[ curseg->start].x - x) * dy;
  155.      b = ((long) Vertexes[ curseg->start].y - y) * dx;
  156.      c = ((long) Vertexes[ curseg->end].x - x) * dy;
  157.      d = ((long) Vertexes[ curseg->end].y - y) * dx;
  158.      if ((a != b) && (c != d) && ((a > b) != (c > d)) && ComputeIntersection( &dummyx, &dummyy, nodeline, curseg))
  159.      {
  160.         splits++; /* one more split */
  161.         num1++;
  162.         num2++;
  163.      }
  164.      else if ((a > b) || ((a == b) && (c > d))
  165.           || ((a == b) && (c == d) && ((dx > 0) == ((Vertexes[ curseg->end].x - Vertexes[ curseg->start].x) > 0)) && ((dy > 0) == ((Vertexes[ curseg->end].y - Vertexes[ curseg->start].y) > 0))))
  166.         num1++; /* one more Seg on the first (right) side */
  167.      else
  168.         num2++; /* one more Seg on the second (left) side */
  169. #ifdef OLD_ALGORITHM
  170.      if (splits > minsplits)
  171.         break;  /* don't waste time */
  172. #else
  173.      if (max( num1, num2) + SplitFactor * splits > mindiff)
  174.         break;  /* don't waste time */
  175. #endif /* OLD_ALGORITHM */
  176.       }
  177.  
  178.       /* there must be at least one Seg on each side */
  179.       if (num1 > 0 && num2 > 0)
  180.       {
  181. #ifdef OLD_ALGORITHM
  182.      /* now, num1 = difference in number of Segs between two sides */
  183.      if (num1 > num2)
  184.         num1 = num1 - num2;
  185.      else
  186.         num1 = num2 - num1;
  187.      /* minimal number of splits = candidate for nodeline */
  188.      if (splits < minsplits || (splits == minsplits && num1 < mindiff))
  189.      {
  190.         minsplits = splits; /* minimal number of splits */
  191.         mindiff = num1; /* minimal difference between the two sides */
  192.         bestnodeline = nodeline; /* save the nodeline */
  193.      }
  194. #else
  195.      /* now, num1 = rating for this nodeline */
  196.      num1 = max( num1, num2) + SplitFactor * splits;
  197.      /* this nodeline is better than the previous one */
  198.      if (num1 < mindiff)
  199.      {
  200.         mindiff = num1; /* save the rating */
  201.         bestnodeline = nodeline; /* save the nodeline */
  202.      }
  203. #endif /* OLD_ALGORITHM */
  204.       }
  205.    }
  206.  
  207.    /* ***DEBUG*** */
  208.    if (bestnodeline && bestnodeline == lastnodeline)
  209.       ProgError( "nodeline picked twice (this is a BUG!)");
  210.    lastnodeline = nodeline;
  211.  
  212.    return bestnodeline;
  213. }
  214.  
  215.  
  216.  
  217. /*
  218.    Move a Seg into a list and update the bounding box
  219. */
  220.  
  221. void StoreInSegList( SEPtr seg, SEPtr *seglist, SEPtr *slistend) /* SWAP - needs Vertexes */
  222. {
  223.    if (*seglist)
  224.    {
  225.       (*slistend)->next = seg;
  226.       *slistend = (*slistend)->next;
  227.    }
  228.    else
  229.    {
  230.       *seglist = seg;
  231.       *slistend = *seglist;
  232.    }
  233.    (*slistend)->next = NULL;
  234. }
  235.  
  236.  
  237.  
  238. /*
  239.    compute the bounding box (limits on X, Y) for a list of Segs
  240. */
  241.  
  242. void ComputeBoundingBox( SEPtr seglist, int *minx, int *maxx, int *miny, int *maxy) /* SWAP - needs Vertexes */
  243. {
  244.    SEPtr curseg;
  245.  
  246.    *maxx = -32767;
  247.    *maxy = -32767;
  248.    *minx = 32767;
  249.    *miny = 32767;
  250.    for (curseg = seglist; curseg; curseg = curseg->next)
  251.    {
  252.       if (Vertexes[ curseg->start].x < *minx)
  253.      *minx = Vertexes[ curseg->start].x;
  254.       if (Vertexes[ curseg->start].x > *maxx)
  255.      *maxx = Vertexes[ curseg->start].x;
  256.       if (Vertexes[ curseg->start].y < *miny)
  257.      *miny = Vertexes[ curseg->start].y;
  258.       if (Vertexes[ curseg->start].y > *maxy)
  259.      *maxy = Vertexes[ curseg->start].y;
  260.  
  261.       if (Vertexes[ curseg->end].x < *minx)
  262.      *minx = Vertexes[ curseg->end].x;
  263.       if (Vertexes[ curseg->end].x > *maxx)
  264.      *maxx = Vertexes[ curseg->end].x;
  265.       if (Vertexes[ curseg->end].y < *miny)
  266.      *miny = Vertexes[ curseg->end].y;
  267.       if (Vertexes[ curseg->end].y > *maxy)
  268.      *maxy = Vertexes[ curseg->end].y;
  269.    }
  270. }
  271.  
  272.  
  273.  
  274. /*
  275.    create a SSector from a list of Segs
  276. */
  277.  
  278. int CreateSSector( SEPtr seglist)
  279. {
  280.    /* update the SSectors list */
  281.    NumSSectors++;
  282.    if (SSectors)
  283.    {
  284.       LastSSector->next = GetMemory( sizeof( struct SSector));
  285.       LastSSector = LastSSector->next;
  286.    }
  287.    else
  288.    {
  289.       SSectors = GetMemory( sizeof( struct SSector));
  290.       LastSSector = SSectors;
  291.    }
  292.    LastSSector->next = NULL;
  293.    /* number of first Segment in this SubSector */
  294.    LastSSector->first = NumSegs;
  295.    /* update the Segs list */
  296.    if (Segs == NULL)
  297.       Segs = seglist;
  298.    else
  299.       LastSeg->next = seglist;
  300.    NumSegs++;
  301.    for (LastSeg = seglist; LastSeg->next; LastSeg = LastSeg->next)
  302.       NumSegs++;
  303.    /* total number of Segments in this SubSector */
  304.    LastSSector->num = NumSegs - LastSSector->first;
  305.    /* while the user is waiting... */
  306.    ShowProgress( OBJ_SSECTORS);
  307.  
  308.    /* return the number of this SubSector */
  309.    return NumSSectors - 1;
  310. }
  311.  
  312.  
  313.  
  314. /*
  315.    create all Nodes from a list of Segs
  316. */
  317.  
  318. Bool CreateNodes( NPtr *node_r, int *ssector_r, SEPtr seglist) /* SWAP - needs Vertexes */
  319. {
  320.    NPtr         node;
  321.    SEPtr        segs1, segs2;
  322.    static SEPtr nodeline, curseg;
  323.    static long  a, b, c, d;
  324.    static SEPtr lastseg1, lastseg2;
  325.  
  326.    /* new Node */
  327.    node = GetMemory( sizeof( struct Node));
  328.  
  329.    /* find the best nodeline */
  330.    nodeline = FindNodeLine( seglist);
  331.  
  332.    /* nodeline could not be found: return a SSector */
  333.    if (nodeline == NULL)
  334.    {
  335.       *node_r = NULL;
  336.       *ssector_r = CreateSSector( seglist) | 0x8000;
  337.       return FALSE;
  338.    }
  339.  
  340.    /* compute x, y, dx, dy */
  341.    node->x = Vertexes[ nodeline->start].x;
  342.    node->y = Vertexes[ nodeline->start].y;
  343.    node->dx = Vertexes[ nodeline->end].x - node->x;
  344.    node->dy = Vertexes[ nodeline->end].y - node->y;
  345.  
  346.    /* split seglist in segs1 and segs2 */
  347.    segs1 = NULL;
  348.    segs2 = NULL;
  349.    while (seglist)
  350.    {
  351.       curseg = seglist;
  352.       seglist = seglist->next;
  353.       /* now, where is that old book about analytic geometry? */
  354.       a = (long) (Vertexes[ curseg->start].x - node->x) * (long) (node->dy);
  355.       b = (long) (Vertexes[ curseg->start].y - node->y) * (long) (node->dx);
  356.       c = (long) (Vertexes[ curseg->end].x - node->x) * (long) (node->dy);
  357.       d = (long) (Vertexes[ curseg->end].y - node->y) * (long) (node->dx);
  358.       /* check if starting vertex is on the right side of the nodeline, */
  359.       /* or if starting vertex is on the nodeline and ending vertex on the right side, */
  360.       /* or if both are on the nodeline and the Seg has the same orientation as the nodeline. */
  361.       if ((a > b) || ((a == b) && (c > d))
  362.       || ((a == b) && (c == d) && ((node->dx > 0) == ((Vertexes[ curseg->end].x - Vertexes[ curseg->start].x) > 0)) && ((node->dy > 0) == ((Vertexes[ curseg->end].y - Vertexes[ curseg->start].y) > 0))))
  363.       {
  364.      /* the starting Vertex is on the first side (right) of the nodeline */
  365.      StoreInSegList( curseg, &segs1, &lastseg1);
  366.      if (c < d)
  367.      {
  368.         int newx, newy;
  369.  
  370.         /* the ending Vertex is on the other side: split the Seg in two */
  371.         if (ComputeIntersection( &newx, &newy, nodeline, curseg))
  372.         {
  373.            InsertObject( OBJ_VERTEXES, -2, newx, newy);
  374.            StoreInSegList( GetFarMemory( sizeof( struct Seg)), &segs2, &lastseg2);
  375.            lastseg2->start = NumVertexes - 1;
  376.            lastseg2->end = lastseg1->end;
  377.            lastseg2->angle = lastseg1->angle;
  378.            lastseg2->linedef = lastseg1->linedef;
  379.            lastseg2->flip = lastseg1->flip;
  380.            lastseg2->dist = lastseg1->dist + ComputeDist( newx - Vertexes[ lastseg1->start].x, newy - Vertexes[ lastseg1->start].y);
  381.            lastseg1->end = NumVertexes - 1;
  382.            ShowProgress( OBJ_VERTEXES);
  383.         }
  384.      }
  385.       }
  386.       else
  387.       {
  388.      /* the starting Vertex is on the second side (left) of the nodeline */
  389.      StoreInSegList( curseg, &segs2, &lastseg2);
  390.      if (c > d)
  391.      {
  392.         int newx, newy;
  393.  
  394.         /* the ending Vertex is on the other side: split the Seg in two */
  395.         if (ComputeIntersection( &newx, &newy, nodeline, curseg))
  396.         {
  397.            InsertObject( OBJ_VERTEXES, -2, newx, newy);
  398.            StoreInSegList( GetFarMemory( sizeof( struct Seg)), &segs1, &lastseg1);
  399.            lastseg1->start = NumVertexes - 1;
  400.            lastseg1->end = lastseg2->end;
  401.            lastseg1->angle = lastseg2->angle;
  402.            lastseg1->linedef = lastseg2->linedef;
  403.            lastseg1->flip = lastseg2->flip;
  404.            lastseg1->dist = lastseg2->dist + ComputeDist( newx - Vertexes[ lastseg2->start].x, newy - Vertexes[ lastseg2->start].y);
  405.            lastseg2->end = NumVertexes - 1;
  406.            ShowProgress( OBJ_VERTEXES);
  407.         }
  408.      }
  409.       }
  410.    }
  411.  
  412.    /* now, we should have all the Segs in segs1 and segs2 (seglist is empty) */
  413.    if (segs1 == NULL || segs2 == NULL)
  414.       ProgError("could not split the Segs list (this is a BUG!)");
  415.  
  416.    /* compute bounding box limits for segs1 */
  417.    ComputeBoundingBox( segs1, &(node->minx1), &(node->maxx1), &(node->miny1), &(node->maxy1));
  418.  
  419.    /* create Nodes or SSectors from segs1 */
  420.    CreateNodes( &(node->node1), &(node->child1), segs1);
  421.  
  422.    /* compute bounding box limits for segs2 */
  423.    ComputeBoundingBox( segs2, &(node->minx2), &(node->maxx2), &(node->miny2), &(node->maxy2));
  424.  
  425.    /* create Nodes or SSectors from segs2 */
  426.    CreateNodes( &(node->node2), &(node->child2), segs2);
  427.  
  428.    /* this Node is OK */
  429.    *node_r = node;
  430.    *ssector_r = 0;
  431.    return TRUE;
  432. }
  433.  
  434.  
  435. /*
  436.    IF YOU ARE WRITING A DOOM EDITOR OR ANOTHER ADD-ON, PLEASE READ THIS:
  437.  
  438.    I spent a lot of time writing the Nodes builder.  There may be some bugs in
  439.    it, but most of the code is OK.  If you steal any ideas from this program,
  440.    put a prominent message in your own editor (i.e. it must be displayed when
  441.    the program starts or in an "about" box) to make it CLEAR that some
  442.    original ideas were taken from DEU.  You need not credit me.  Just credit
  443.    DEU and its contributors.  Thanks.
  444.  
  445.    While everyone was talking about LineDefs, I had the idea of taking only
  446.    the Segs into account, and creating the Segs directly from the SideDefs.
  447.    Also, dividing the list of Segs in two after each call to CreateNodes makes
  448.    the algorithm faster.  I use several other tricks, such as looking at the
  449.    two ends of a Seg to see on which side of the nodeline it lies or if it
  450.    should be split in two.  I took me a lot of time and efforts to do this.
  451.  
  452.    I give this algorithm to whoever wants to use it, but with this condition:
  453.    if your program uses SOME of the IDEAS from DEU or the whole ALGORITHM, you
  454.    MUST tell it to the user.  And if you post a message with all or parts of
  455.    this algorithm in it, please post THIS NOTICE also.  I don't want to speak
  456.    legalese; I hope that you understand me...  I kindly give the sources of my
  457.    program to you: please be kind with me...
  458.  
  459.    If you need more information about this, here is my E-mail address:
  460.    quinet@montefiore.ulg.ac.be (Raphaël Quinet).
  461.  
  462.    Short description of the algorithm:
  463.      1 - Create one Seg for each SideDef: pick each LineDef in turn.  If it
  464.      has a "first" SideDef, then create a normal Seg.  If it has a
  465.      "second" SideDef, then create a flipped Seg.
  466.      2 - Call CreateNodes with the current list of Segs.  The list of Segs is
  467.      the only argument to CreateNodes.
  468.      3 - Save the Nodes, Segs and SSectors to disk.  Start with the leaves of
  469.      the Nodes tree and continue up to the root (last Node).
  470.  
  471.    CreateNodes does the following:
  472.      1 - Pick a nodeline amongst the Segs (minimize the number of splits and
  473.      keep the tree as balanced as possible).
  474.      2 - Move all Segs on the right of the nodeline in a list (segs1) and
  475.      move all Segs on the left of the nodeline in another list (segs2).
  476.      3 - If the first list (segs1) contains references to more than one
  477.      Sector or if the angle between two adjacent Segs is greater than
  478.      180°, then call CreateNodes with this (smaller) list.  Else, create
  479.      a SubSector with all these Segs.
  480.      4 - Do the same for the second list (segs2).
  481.      5 - Return the new node (its two children are already OK).
  482.  
  483.    Each time CreateSSector is called, the Segs are put in a global list.
  484.    When there is no more Seg in CreateNodes' list, then they are all in the
  485.    global list and ready to be saved to disk.
  486.  
  487.    Note: now that the nice guys at Id software have released their algorithm,
  488.    I have changed the way CreateNodes work.  Instead of checking if the Segs
  489.    list should be split, I try to find a nodeline.  If I found one, then I
  490.    split the list of Segs and call CreateNodes on both lists.  Else, I just
  491.    return a SSector which contains the list of Segs.
  492. */
  493.  
  494.  
  495. /* end of file */
  496.