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